Commit | Line | Data |
---|---|---|
4e772f44 | 1 | /* Serial interface for local (hardwired) serial ports on Un*x like systems |
63c3e305 | 2 | Copyright 1992, 1993, 1994, 1998 Free Software Foundation, Inc. |
4e772f44 SG |
3 | |
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
6c9638b4 | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
4e772f44 SG |
19 | |
20 | #include "defs.h" | |
21 | #include "serial.h" | |
22 | #include <fcntl.h> | |
23 | #include <sys/types.h> | |
dedcc91d SC |
24 | #include "terminal.h" |
25 | #ifdef HAVE_UNISTD_H | |
26 | #include <unistd.h> | |
4e772f44 SG |
27 | #endif |
28 | ||
29 | #ifdef HAVE_TERMIOS | |
38dc5e12 SG |
30 | |
31 | struct hardwire_ttystate | |
32 | { | |
33 | struct termios termios; | |
34 | }; | |
dc34b11d | 35 | #endif /* termios */ |
38dc5e12 | 36 | |
4e772f44 | 37 | #ifdef HAVE_TERMIO |
38dc5e12 | 38 | |
dc34b11d JK |
39 | /* It is believed that all systems which have added job control to SVR3 |
40 | (e.g. sco) have also added termios. Even if not, trying to figure out | |
41 | all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty | |
42 | bewildering. So we don't attempt it. */ | |
43 | ||
38dc5e12 SG |
44 | struct hardwire_ttystate |
45 | { | |
46 | struct termio termio; | |
47 | }; | |
dc34b11d | 48 | #endif /* termio */ |
38dc5e12 | 49 | |
4e772f44 | 50 | #ifdef HAVE_SGTTY |
68d2db62 JK |
51 | /* Needed for the code which uses select(). We would include <sys/select.h> |
52 | too if it existed on all systems. */ | |
53 | #include <sys/time.h> | |
54 | ||
38dc5e12 SG |
55 | struct hardwire_ttystate |
56 | { | |
57 | struct sgttyb sgttyb; | |
c2e247c4 JK |
58 | struct tchars tc; |
59 | struct ltchars ltc; | |
60 | /* Line discipline flags. */ | |
61 | int lmode; | |
38dc5e12 | 62 | }; |
dc34b11d | 63 | #endif /* sgtty */ |
4e772f44 | 64 | |
9775789d SG |
65 | static int hardwire_open PARAMS ((serial_t scb, const char *name)); |
66 | static void hardwire_raw PARAMS ((serial_t scb)); | |
67 | static int wait_for PARAMS ((serial_t scb, int timeout)); | |
68 | static int hardwire_readchar PARAMS ((serial_t scb, int timeout)); | |
69 | static int rate_to_code PARAMS ((int rate)); | |
70 | static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate)); | |
71 | static int hardwire_write PARAMS ((serial_t scb, const char *str, int len)); | |
9775789d | 72 | static void hardwire_close PARAMS ((serial_t scb)); |
38dc5e12 SG |
73 | static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state)); |
74 | static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state)); | |
75 | static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb)); | |
76 | static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state)); | |
b607efe7 FF |
77 | static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate, |
78 | serial_ttystate)); | |
79 | static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate)); | |
3ffbdf15 | 80 | static int hardwire_drain_output PARAMS ((serial_t)); |
b607efe7 FF |
81 | static int hardwire_flush_output PARAMS ((serial_t)); |
82 | static int hardwire_flush_input PARAMS ((serial_t)); | |
83 | static int hardwire_send_break PARAMS ((serial_t)); | |
84 | static int hardwire_setstopbits PARAMS ((serial_t, int)); | |
9775789d | 85 | |
4e772f44 SG |
86 | /* Open up a real live device for serial I/O */ |
87 | ||
88 | static int | |
89 | hardwire_open(scb, name) | |
90 | serial_t scb; | |
91 | const char *name; | |
92 | { | |
93 | scb->fd = open (name, O_RDWR); | |
94 | if (scb->fd < 0) | |
4febd102 | 95 | return -1; |
4e772f44 SG |
96 | |
97 | return 0; | |
98 | } | |
99 | ||
38dc5e12 SG |
100 | static int |
101 | get_tty_state(scb, state) | |
4e772f44 | 102 | serial_t scb; |
38dc5e12 | 103 | struct hardwire_ttystate *state; |
4e772f44 SG |
104 | { |
105 | #ifdef HAVE_TERMIOS | |
057c2f47 | 106 | extern int errno; |
c2e247c4 JK |
107 | |
108 | if (tcgetattr(scb->fd, &state->termios) < 0) | |
109 | return -1; | |
110 | ||
c2e247c4 | 111 | return 0; |
38dc5e12 | 112 | #endif |
4e772f44 | 113 | |
38dc5e12 | 114 | #ifdef HAVE_TERMIO |
c2e247c4 JK |
115 | if (ioctl (scb->fd, TCGETA, &state->termio) < 0) |
116 | return -1; | |
dc34b11d | 117 | return 0; |
38dc5e12 | 118 | #endif |
4e772f44 | 119 | |
38dc5e12 | 120 | #ifdef HAVE_SGTTY |
c2e247c4 JK |
121 | if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0) |
122 | return -1; | |
123 | if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0) | |
124 | return -1; | |
125 | if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0) | |
126 | return -1; | |
127 | if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0) | |
128 | return -1; | |
129 | ||
a14a8fad | 130 | return 0; |
38dc5e12 SG |
131 | #endif |
132 | } | |
4e772f44 | 133 | |
38dc5e12 SG |
134 | static int |
135 | set_tty_state(scb, state) | |
136 | serial_t scb; | |
137 | struct hardwire_ttystate *state; | |
138 | { | |
38dc5e12 | 139 | #ifdef HAVE_TERMIOS |
c2e247c4 JK |
140 | if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0) |
141 | return -1; | |
142 | ||
a14a8fad | 143 | return 0; |
4e772f44 SG |
144 | #endif |
145 | ||
146 | #ifdef HAVE_TERMIO | |
c2e247c4 JK |
147 | if (ioctl (scb->fd, TCSETA, &state->termio) < 0) |
148 | return -1; | |
c2e247c4 | 149 | return 0; |
38dc5e12 | 150 | #endif |
4e772f44 | 151 | |
38dc5e12 | 152 | #ifdef HAVE_SGTTY |
c2e247c4 JK |
153 | if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0) |
154 | return -1; | |
88cc9a42 RP |
155 | if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0) |
156 | return -1; | |
157 | if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0) | |
158 | return -1; | |
159 | if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0) | |
160 | return -1; | |
c2e247c4 | 161 | |
a14a8fad | 162 | return 0; |
38dc5e12 SG |
163 | #endif |
164 | } | |
4e772f44 | 165 | |
38dc5e12 SG |
166 | static serial_ttystate |
167 | hardwire_get_tty_state(scb) | |
168 | serial_t scb; | |
169 | { | |
170 | struct hardwire_ttystate *state; | |
4e772f44 | 171 | |
38dc5e12 | 172 | state = (struct hardwire_ttystate *)xmalloc(sizeof *state); |
4e772f44 | 173 | |
38dc5e12 SG |
174 | if (get_tty_state(scb, state)) |
175 | return NULL; | |
4e772f44 | 176 | |
38dc5e12 SG |
177 | return (serial_ttystate)state; |
178 | } | |
4e772f44 | 179 | |
38dc5e12 SG |
180 | static int |
181 | hardwire_set_tty_state(scb, ttystate) | |
182 | serial_t scb; | |
183 | serial_ttystate ttystate; | |
184 | { | |
185 | struct hardwire_ttystate *state; | |
4e772f44 | 186 | |
38dc5e12 SG |
187 | state = (struct hardwire_ttystate *)ttystate; |
188 | ||
189 | return set_tty_state(scb, state); | |
190 | } | |
191 | ||
c2e247c4 JK |
192 | static int |
193 | hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate) | |
194 | serial_t scb; | |
195 | serial_ttystate new_ttystate; | |
196 | serial_ttystate old_ttystate; | |
197 | { | |
3fe11d47 | 198 | struct hardwire_ttystate new_state; |
6b14af2b | 199 | #ifdef HAVE_SGTTY |
c2e247c4 | 200 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate; |
6b14af2b | 201 | #endif |
c2e247c4 | 202 | |
3fe11d47 JK |
203 | new_state = *(struct hardwire_ttystate *)new_ttystate; |
204 | ||
d881dd86 JK |
205 | /* Don't change in or out of raw mode; we don't want to flush input. |
206 | termio and termios have no such restriction; for them flushing input | |
207 | is separate from setting the attributes. */ | |
c2e247c4 JK |
208 | |
209 | #ifdef HAVE_SGTTY | |
210 | if (state->sgttyb.sg_flags & RAW) | |
211 | new_state.sgttyb.sg_flags |= RAW; | |
212 | else | |
213 | new_state.sgttyb.sg_flags &= ~RAW; | |
214 | ||
215 | /* I'm not sure whether this is necessary; the manpage just mentions | |
216 | RAW not CBREAK. */ | |
217 | if (state->sgttyb.sg_flags & CBREAK) | |
218 | new_state.sgttyb.sg_flags |= CBREAK; | |
219 | else | |
220 | new_state.sgttyb.sg_flags &= ~CBREAK; | |
221 | #endif | |
222 | ||
223 | return set_tty_state (scb, &new_state); | |
224 | } | |
225 | ||
226 | static void | |
227 | hardwire_print_tty_state (scb, ttystate) | |
228 | serial_t scb; | |
229 | serial_ttystate ttystate; | |
230 | { | |
231 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate; | |
232 | int i; | |
233 | ||
dc34b11d | 234 | #ifdef HAVE_TERMIOS |
c2e247c4 JK |
235 | printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n", |
236 | state->termios.c_iflag, state->termios.c_oflag); | |
a77a5278 JK |
237 | printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n", |
238 | state->termios.c_cflag, state->termios.c_lflag); | |
239 | #if 0 | |
240 | /* This not in POSIX, and is not really documented by those systems | |
241 | which have it (at least not Sun). */ | |
242 | printf_filtered ("c_line = 0x%x.\n", state->termios.c_line); | |
243 | #endif | |
c2e247c4 JK |
244 | printf_filtered ("c_cc: "); |
245 | for (i = 0; i < NCCS; i += 1) | |
246 | printf_filtered ("0x%x ", state->termios.c_cc[i]); | |
247 | printf_filtered ("\n"); | |
248 | #endif | |
249 | ||
250 | #ifdef HAVE_TERMIO | |
251 | printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n", | |
252 | state->termio.c_iflag, state->termio.c_oflag); | |
253 | printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", | |
254 | state->termio.c_cflag, state->termio.c_lflag, | |
255 | state->termio.c_line); | |
256 | printf_filtered ("c_cc: "); | |
257 | for (i = 0; i < NCC; i += 1) | |
258 | printf_filtered ("0x%x ", state->termio.c_cc[i]); | |
259 | printf_filtered ("\n"); | |
260 | #endif | |
261 | ||
262 | #ifdef HAVE_SGTTY | |
263 | printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags); | |
264 | ||
265 | printf_filtered ("tchars: "); | |
266 | for (i = 0; i < (int)sizeof (struct tchars); i++) | |
267 | printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]); | |
268 | printf_filtered ("\n"); | |
269 | ||
270 | printf_filtered ("ltchars: "); | |
271 | for (i = 0; i < (int)sizeof (struct ltchars); i++) | |
272 | printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]); | |
273 | printf_filtered ("\n"); | |
274 | ||
275 | printf_filtered ("lmode: 0x%x\n", state->lmode); | |
276 | #endif | |
277 | } | |
278 | ||
3ffbdf15 FF |
279 | /* Wait for the output to drain away, as opposed to flushing (discarding) it */ |
280 | ||
281 | static int | |
282 | hardwire_drain_output (scb) | |
283 | serial_t scb; | |
284 | { | |
285 | #ifdef HAVE_TERMIOS | |
286 | return tcdrain (scb->fd); | |
287 | #endif | |
288 | ||
289 | #ifdef HAVE_TERMIO | |
290 | return ioctl (scb->fd, TCSBRK, 1); | |
291 | #endif | |
292 | ||
293 | #ifdef HAVE_SGTTY | |
294 | /* Get the current state and then restore it using TIOCSETP, | |
295 | which should cause the output to drain and pending input | |
296 | to be discarded. */ | |
297 | { | |
298 | struct hardwire_ttystate state; | |
299 | if (get_tty_state (scb, &state)) | |
300 | { | |
301 | return (-1); | |
302 | } | |
303 | else | |
304 | { | |
305 | return (ioctl (scb->fd, TIOCSETP, &state.sgttyb)); | |
306 | } | |
307 | } | |
308 | #endif | |
309 | } | |
310 | ||
c2e247c4 JK |
311 | static int |
312 | hardwire_flush_output (scb) | |
313 | serial_t scb; | |
314 | { | |
315 | #ifdef HAVE_TERMIOS | |
316 | return tcflush (scb->fd, TCOFLUSH); | |
317 | #endif | |
318 | ||
319 | #ifdef HAVE_TERMIO | |
320 | return ioctl (scb->fd, TCFLSH, 1); | |
321 | #endif | |
322 | ||
323 | #ifdef HAVE_SGTTY | |
324 | /* This flushes both input and output, but we can't do better. */ | |
325 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
326 | #endif | |
327 | } | |
328 | ||
704deef2 JK |
329 | static int |
330 | hardwire_flush_input (scb) | |
331 | serial_t scb; | |
332 | { | |
dedcc91d SC |
333 | scb->bufcnt = 0; |
334 | scb->bufp = scb->buf; | |
335 | ||
704deef2 JK |
336 | #ifdef HAVE_TERMIOS |
337 | return tcflush (scb->fd, TCIFLUSH); | |
338 | #endif | |
339 | ||
340 | #ifdef HAVE_TERMIO | |
341 | return ioctl (scb->fd, TCFLSH, 0); | |
342 | #endif | |
343 | ||
344 | #ifdef HAVE_SGTTY | |
345 | /* This flushes both input and output, but we can't do better. */ | |
346 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
347 | #endif | |
348 | } | |
349 | ||
350 | static int | |
351 | hardwire_send_break (scb) | |
352 | serial_t scb; | |
353 | { | |
704deef2 JK |
354 | #ifdef HAVE_TERMIOS |
355 | return tcsendbreak (scb->fd, 0); | |
356 | #endif | |
357 | ||
358 | #ifdef HAVE_TERMIO | |
359 | return ioctl (scb->fd, TCSBRK, 0); | |
360 | #endif | |
361 | ||
362 | #ifdef HAVE_SGTTY | |
95a98b5e | 363 | { |
0ac0a9f6 | 364 | int status; |
95a98b5e JK |
365 | struct timeval timeout; |
366 | ||
367 | status = ioctl (scb->fd, TIOCSBRK, 0); | |
368 | ||
369 | /* Can't use usleep; it doesn't exist in BSD 4.2. */ | |
370 | /* Note that if this select() is interrupted by a signal it will not wait | |
371 | the full length of time. I think that is OK. */ | |
372 | timeout.tv_sec = 0; | |
373 | timeout.tv_usec = 250000; | |
374 | select (0, 0, 0, 0, &timeout); | |
375 | status = ioctl (scb->fd, TIOCCBRK, 0); | |
376 | return status; | |
377 | } | |
704deef2 JK |
378 | #endif |
379 | } | |
380 | ||
38dc5e12 SG |
381 | static void |
382 | hardwire_raw(scb) | |
383 | serial_t scb; | |
384 | { | |
385 | struct hardwire_ttystate state; | |
386 | ||
387 | if (get_tty_state(scb, &state)) | |
199b2450 | 388 | fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno)); |
38dc5e12 SG |
389 | |
390 | #ifdef HAVE_TERMIOS | |
391 | state.termios.c_iflag = 0; | |
392 | state.termios.c_oflag = 0; | |
393 | state.termios.c_lflag = 0; | |
394 | state.termios.c_cflag &= ~(CSIZE|PARENB); | |
2e6784a8 | 395 | state.termios.c_cflag |= CLOCAL | CS8; |
38dc5e12 SG |
396 | state.termios.c_cc[VMIN] = 0; |
397 | state.termios.c_cc[VTIME] = 0; | |
398 | #endif | |
399 | ||
400 | #ifdef HAVE_TERMIO | |
401 | state.termio.c_iflag = 0; | |
402 | state.termio.c_oflag = 0; | |
403 | state.termio.c_lflag = 0; | |
404 | state.termio.c_cflag &= ~(CSIZE|PARENB); | |
2e6784a8 | 405 | state.termio.c_cflag |= CLOCAL | CS8; |
38dc5e12 SG |
406 | state.termio.c_cc[VMIN] = 0; |
407 | state.termio.c_cc[VTIME] = 0; | |
408 | #endif | |
409 | ||
410 | #ifdef HAVE_SGTTY | |
411 | state.sgttyb.sg_flags |= RAW | ANYP; | |
412 | state.sgttyb.sg_flags &= ~(CBREAK | ECHO); | |
4e772f44 | 413 | #endif |
9e15da4a SG |
414 | |
415 | scb->current_timeout = 0; | |
38dc5e12 SG |
416 | |
417 | if (set_tty_state (scb, &state)) | |
199b2450 | 418 | fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno)); |
4e772f44 SG |
419 | } |
420 | ||
9e15da4a SG |
421 | /* Wait for input on scb, with timeout seconds. Returns 0 on success, |
422 | otherwise SERIAL_TIMEOUT or SERIAL_ERROR. | |
423 | ||
424 | For termio{s}, we actually just setup VTIME if necessary, and let the | |
425 | timeout occur in the read() in hardwire_read(). | |
426 | */ | |
4e772f44 SG |
427 | |
428 | static int | |
9775789d | 429 | wait_for(scb, timeout) |
4e772f44 SG |
430 | serial_t scb; |
431 | int timeout; | |
432 | { | |
9db58d3a JK |
433 | scb->timeout_remaining = 0; |
434 | ||
9775789d | 435 | #ifdef HAVE_SGTTY |
9db58d3a JK |
436 | { |
437 | struct timeval tv; | |
438 | fd_set readfds; | |
eca29634 | 439 | |
9db58d3a | 440 | FD_ZERO (&readfds); |
eca29634 | 441 | |
9db58d3a JK |
442 | tv.tv_sec = timeout; |
443 | tv.tv_usec = 0; | |
eca29634 | 444 | |
9db58d3a | 445 | FD_SET(scb->fd, &readfds); |
eca29634 | 446 | |
9db58d3a JK |
447 | while (1) |
448 | { | |
449 | int numfds; | |
450 | ||
451 | if (timeout >= 0) | |
452 | numfds = select(scb->fd+1, &readfds, 0, 0, &tv); | |
a037b21e | 453 | else |
9db58d3a | 454 | numfds = select(scb->fd+1, &readfds, 0, 0, 0); |
a037b21e | 455 | |
9db58d3a JK |
456 | if (numfds <= 0) |
457 | if (numfds == 0) | |
458 | return SERIAL_TIMEOUT; | |
459 | else if (errno == EINTR) | |
460 | continue; | |
461 | else | |
462 | return SERIAL_ERROR; /* Got an error from select or poll */ | |
9e15da4a | 463 | |
9db58d3a JK |
464 | return 0; |
465 | } | |
466 | } | |
9775789d | 467 | #endif /* HAVE_SGTTY */ |
4e772f44 | 468 | |
9775789d | 469 | #if defined HAVE_TERMIO || defined HAVE_TERMIOS |
9e15da4a SG |
470 | if (timeout == scb->current_timeout) |
471 | return 0; | |
4e772f44 | 472 | |
9db58d3a JK |
473 | scb->current_timeout = timeout; |
474 | ||
9e15da4a | 475 | { |
38dc5e12 | 476 | struct hardwire_ttystate state; |
eca29634 | 477 | |
38dc5e12 | 478 | if (get_tty_state(scb, &state)) |
199b2450 | 479 | fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno)); |
eca29634 | 480 | |
38dc5e12 | 481 | #ifdef HAVE_TERMIOS |
864df7e6 JK |
482 | if (timeout < 0) |
483 | { | |
484 | /* No timeout. */ | |
485 | state.termios.c_cc[VTIME] = 0; | |
486 | state.termios.c_cc[VMIN] = 1; | |
487 | } | |
488 | else | |
489 | { | |
490 | state.termios.c_cc[VMIN] = 0; | |
491 | state.termios.c_cc[VTIME] = timeout * 10; | |
492 | if (state.termios.c_cc[VTIME] != timeout * 10) | |
493 | { | |
9db58d3a JK |
494 | |
495 | /* If c_cc is an 8-bit signed character, we can't go | |
496 | bigger than this. If it is always unsigned, we could use | |
497 | 25. */ | |
498 | ||
499 | scb->current_timeout = 12; | |
500 | state.termios.c_cc[VTIME] = scb->current_timeout * 10; | |
501 | scb->timeout_remaining = timeout - scb->current_timeout; | |
864df7e6 JK |
502 | } |
503 | } | |
38dc5e12 | 504 | #endif |
9775789d | 505 | |
9e15da4a | 506 | #ifdef HAVE_TERMIO |
864df7e6 JK |
507 | if (timeout < 0) |
508 | { | |
509 | /* No timeout. */ | |
510 | state.termio.c_cc[VTIME] = 0; | |
511 | state.termio.c_cc[VMIN] = 1; | |
512 | } | |
513 | else | |
514 | { | |
515 | state.termio.c_cc[VMIN] = 0; | |
516 | state.termio.c_cc[VTIME] = timeout * 10; | |
517 | if (state.termio.c_cc[VTIME] != timeout * 10) | |
518 | { | |
9db58d3a JK |
519 | /* If c_cc is an 8-bit signed character, we can't go |
520 | bigger than this. If it is always unsigned, we could use | |
521 | 25. */ | |
522 | ||
523 | scb->current_timeout = 12; | |
5c07a10b | 524 | state.termio.c_cc[VTIME] = scb->current_timeout * 10; |
9db58d3a | 525 | scb->timeout_remaining = timeout - scb->current_timeout; |
864df7e6 JK |
526 | } |
527 | } | |
38dc5e12 | 528 | #endif |
9e15da4a | 529 | |
38dc5e12 | 530 | if (set_tty_state (scb, &state)) |
199b2450 | 531 | fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno)); |
9e15da4a | 532 | |
9e15da4a SG |
533 | return 0; |
534 | } | |
535 | #endif /* HAVE_TERMIO || HAVE_TERMIOS */ | |
9775789d SG |
536 | } |
537 | ||
538 | /* Read a character with user-specified timeout. TIMEOUT is number of seconds | |
539 | to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns | |
057c2f47 RP |
540 | char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line |
541 | dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */ | |
9775789d SG |
542 | |
543 | static int | |
544 | hardwire_readchar(scb, timeout) | |
545 | serial_t scb; | |
546 | int timeout; | |
547 | { | |
548 | int status; | |
549 | ||
550 | if (scb->bufcnt-- > 0) | |
551 | return *scb->bufp++; | |
552 | ||
9db58d3a JK |
553 | while (1) |
554 | { | |
555 | status = wait_for (scb, timeout); | |
556 | ||
557 | if (status < 0) | |
558 | return status; | |
559 | ||
560 | scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ); | |
561 | ||
562 | if (scb->bufcnt <= 0) | |
563 | { | |
564 | if (scb->bufcnt == 0) | |
565 | { | |
566 | /* Zero characters means timeout (it could also be EOF, but | |
567 | we don't (yet at least) distinguish). */ | |
568 | if (scb->timeout_remaining > 0) | |
569 | { | |
570 | timeout = scb->timeout_remaining; | |
571 | continue; | |
572 | } | |
573 | else | |
574 | return SERIAL_TIMEOUT; | |
575 | } | |
2e6784a8 SG |
576 | else if (errno == EINTR) |
577 | continue; | |
9db58d3a JK |
578 | else |
579 | return SERIAL_ERROR; /* Got an error from read */ | |
580 | } | |
581 | ||
582 | scb->bufcnt--; | |
583 | scb->bufp = scb->buf; | |
584 | return *scb->bufp++; | |
585 | } | |
4e772f44 SG |
586 | } |
587 | ||
588 | #ifndef B19200 | |
589 | #define B19200 EXTA | |
590 | #endif | |
591 | ||
592 | #ifndef B38400 | |
593 | #define B38400 EXTB | |
594 | #endif | |
595 | ||
596 | /* Translate baud rates from integers to damn B_codes. Unix should | |
597 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
598 | ||
599 | static struct | |
600 | { | |
601 | int rate; | |
602 | int code; | |
603 | } | |
604 | baudtab[] = | |
605 | { | |
606 | {50, B50}, | |
607 | {75, B75}, | |
608 | {110, B110}, | |
609 | {134, B134}, | |
610 | {150, B150}, | |
611 | {200, B200}, | |
612 | {300, B300}, | |
613 | {600, B600}, | |
614 | {1200, B1200}, | |
615 | {1800, B1800}, | |
616 | {2400, B2400}, | |
617 | {4800, B4800}, | |
618 | {9600, B9600}, | |
619 | {19200, B19200}, | |
620 | {38400, B38400}, | |
e5a940e2 SG |
621 | #ifdef B57600 |
622 | {57600, B57600}, | |
623 | #endif | |
624 | #ifdef B115200 | |
625 | {115200, B115200}, | |
626 | #endif | |
627 | #ifdef B230400 | |
628 | {230400, B230400}, | |
629 | #endif | |
630 | #ifdef B460800 | |
631 | {460800, B460800}, | |
632 | #endif | |
4e772f44 SG |
633 | {-1, -1}, |
634 | }; | |
635 | ||
636 | static int | |
637 | rate_to_code(rate) | |
638 | int rate; | |
639 | { | |
640 | int i; | |
641 | ||
642 | for (i = 0; baudtab[i].rate != -1; i++) | |
643 | if (rate == baudtab[i].rate) | |
644 | return baudtab[i].code; | |
645 | ||
646 | return -1; | |
647 | } | |
648 | ||
649 | static int | |
650 | hardwire_setbaudrate(scb, rate) | |
651 | serial_t scb; | |
652 | int rate; | |
653 | { | |
38dc5e12 | 654 | struct hardwire_ttystate state; |
4e772f44 | 655 | |
38dc5e12 | 656 | if (get_tty_state(scb, &state)) |
4febd102 | 657 | return -1; |
4e772f44 | 658 | |
38dc5e12 SG |
659 | #ifdef HAVE_TERMIOS |
660 | cfsetospeed (&state.termios, rate_to_code (rate)); | |
661 | cfsetispeed (&state.termios, rate_to_code (rate)); | |
4e772f44 SG |
662 | #endif |
663 | ||
664 | #ifdef HAVE_TERMIO | |
4e772f44 SG |
665 | #ifndef CIBAUD |
666 | #define CIBAUD CBAUD | |
667 | #endif | |
668 | ||
38dc5e12 SG |
669 | state.termio.c_cflag &= ~(CBAUD | CIBAUD); |
670 | state.termio.c_cflag |= rate_to_code (rate); | |
4e772f44 SG |
671 | #endif |
672 | ||
673 | #ifdef HAVE_SGTTY | |
38dc5e12 SG |
674 | state.sgttyb.sg_ispeed = rate_to_code (rate); |
675 | state.sgttyb.sg_ospeed = rate_to_code (rate); | |
4e772f44 | 676 | #endif |
38dc5e12 SG |
677 | |
678 | return set_tty_state (scb, &state); | |
4e772f44 SG |
679 | } |
680 | ||
85c8b135 SG |
681 | static int |
682 | hardwire_setstopbits(scb, num) | |
683 | serial_t scb; | |
684 | int num; | |
685 | { | |
686 | struct hardwire_ttystate state; | |
687 | int newbit; | |
688 | ||
689 | if (get_tty_state(scb, &state)) | |
690 | return -1; | |
691 | ||
692 | switch (num) | |
693 | { | |
694 | case SERIAL_1_STOPBITS: | |
695 | newbit = 0; | |
696 | break; | |
697 | case SERIAL_1_AND_A_HALF_STOPBITS: | |
698 | case SERIAL_2_STOPBITS: | |
699 | newbit = 1; | |
700 | break; | |
701 | default: | |
702 | return 1; | |
703 | } | |
704 | ||
705 | #ifdef HAVE_TERMIOS | |
706 | if (!newbit) | |
707 | state.termios.c_cflag &= ~CSTOPB; | |
708 | else | |
709 | state.termios.c_cflag |= CSTOPB; /* two bits */ | |
710 | #endif | |
711 | ||
712 | #ifdef HAVE_TERMIO | |
713 | if (!newbit) | |
714 | state.termio.c_cflag &= ~CSTOPB; | |
715 | else | |
716 | state.termio.c_cflag |= CSTOPB; /* two bits */ | |
717 | #endif | |
718 | ||
719 | #ifdef HAVE_SGTTY | |
720 | return 0; /* sgtty doesn't support this */ | |
721 | #endif | |
722 | ||
723 | return set_tty_state (scb, &state); | |
724 | } | |
725 | ||
4e772f44 SG |
726 | static int |
727 | hardwire_write(scb, str, len) | |
728 | serial_t scb; | |
729 | const char *str; | |
730 | int len; | |
731 | { | |
732 | int cc; | |
733 | ||
734 | while (len > 0) | |
735 | { | |
736 | cc = write(scb->fd, str, len); | |
737 | ||
738 | if (cc < 0) | |
739 | return 1; | |
740 | len -= cc; | |
741 | str += cc; | |
742 | } | |
743 | return 0; | |
744 | } | |
745 | ||
4e772f44 SG |
746 | static void |
747 | hardwire_close(scb) | |
748 | serial_t scb; | |
749 | { | |
750 | if (scb->fd < 0) | |
751 | return; | |
752 | ||
4e772f44 SG |
753 | close(scb->fd); |
754 | scb->fd = -1; | |
755 | } | |
756 | ||
757 | static struct serial_ops hardwire_ops = | |
758 | { | |
759 | "hardwire", | |
760 | 0, | |
761 | hardwire_open, | |
762 | hardwire_close, | |
763 | hardwire_readchar, | |
764 | hardwire_write, | |
c2e247c4 | 765 | hardwire_flush_output, |
704deef2 JK |
766 | hardwire_flush_input, |
767 | hardwire_send_break, | |
4e772f44 | 768 | hardwire_raw, |
38dc5e12 SG |
769 | hardwire_get_tty_state, |
770 | hardwire_set_tty_state, | |
c2e247c4 JK |
771 | hardwire_print_tty_state, |
772 | hardwire_noflush_set_tty_state, | |
38dc5e12 | 773 | hardwire_setbaudrate, |
85c8b135 | 774 | hardwire_setstopbits, |
3ffbdf15 | 775 | hardwire_drain_output, /* wait for output to drain */ |
4e772f44 SG |
776 | }; |
777 | ||
9775789d | 778 | void |
4e772f44 SG |
779 | _initialize_ser_hardwire () |
780 | { | |
781 | serial_add_interface (&hardwire_ops); | |
782 | } |