Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Serial interface for local (hardwired) serial ports on Un*x like systems |
1e4728e7 | 2 | |
6aba47ca | 3 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2003, |
4c38e0a4 | 4 | 2004, 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
20 | |
21 | #include "defs.h" | |
22 | #include "serial.h" | |
3eb25fda | 23 | #include "ser-base.h" |
c2c6d25f JM |
24 | #include "ser-unix.h" |
25 | ||
c906108c SS |
26 | #include <fcntl.h> |
27 | #include <sys/types.h> | |
28 | #include "terminal.h" | |
c2c6d25f JM |
29 | #include <sys/socket.h> |
30 | #include <sys/time.h> | |
31 | ||
0ea3f30e | 32 | #include "gdb_select.h" |
c2c6d25f | 33 | #include "gdb_string.h" |
23776285 | 34 | #include "gdbcmd.h" |
c2c6d25f | 35 | |
c906108c SS |
36 | #ifdef HAVE_TERMIOS |
37 | ||
38 | struct hardwire_ttystate | |
c5aa993b JM |
39 | { |
40 | struct termios termios; | |
41 | }; | |
23776285 MR |
42 | |
43 | #ifdef CRTSCTS | |
44 | /* Boolean to explicitly enable or disable h/w flow control. */ | |
45 | static int serial_hwflow; | |
46 | static void | |
47 | show_serial_hwflow (struct ui_file *file, int from_tty, | |
48 | struct cmd_list_element *c, const char *value) | |
49 | { | |
50 | fprintf_filtered (file, _("Hardware flow control is %s.\n"), value); | |
51 | } | |
52 | #endif | |
53 | ||
c906108c SS |
54 | #endif /* termios */ |
55 | ||
56 | #ifdef HAVE_TERMIO | |
57 | ||
58 | /* It is believed that all systems which have added job control to SVR3 | |
59 | (e.g. sco) have also added termios. Even if not, trying to figure out | |
60 | all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty | |
61 | bewildering. So we don't attempt it. */ | |
62 | ||
63 | struct hardwire_ttystate | |
c5aa993b JM |
64 | { |
65 | struct termio termio; | |
66 | }; | |
c906108c SS |
67 | #endif /* termio */ |
68 | ||
69 | #ifdef HAVE_SGTTY | |
c906108c | 70 | struct hardwire_ttystate |
c5aa993b JM |
71 | { |
72 | struct sgttyb sgttyb; | |
73 | struct tchars tc; | |
74 | struct ltchars ltc; | |
75 | /* Line discipline flags. */ | |
76 | int lmode; | |
77 | }; | |
c906108c SS |
78 | #endif /* sgtty */ |
79 | ||
819cc324 AC |
80 | static int hardwire_open (struct serial *scb, const char *name); |
81 | static void hardwire_raw (struct serial *scb); | |
82 | static int wait_for (struct serial *scb, int timeout); | |
83 | static int hardwire_readchar (struct serial *scb, int timeout); | |
84 | static int do_hardwire_readchar (struct serial *scb, int timeout); | |
c2c6d25f | 85 | static int rate_to_code (int rate); |
819cc324 AC |
86 | static int hardwire_setbaudrate (struct serial *scb, int rate); |
87 | static void hardwire_close (struct serial *scb); | |
88 | static int get_tty_state (struct serial *scb, | |
89 | struct hardwire_ttystate * state); | |
90 | static int set_tty_state (struct serial *scb, | |
91 | struct hardwire_ttystate * state); | |
92 | static serial_ttystate hardwire_get_tty_state (struct serial *scb); | |
93 | static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state); | |
94 | static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate, | |
95 | serial_ttystate); | |
96 | static void hardwire_print_tty_state (struct serial *, serial_ttystate, | |
97 | struct ui_file *); | |
98 | static int hardwire_drain_output (struct serial *); | |
99 | static int hardwire_flush_output (struct serial *); | |
100 | static int hardwire_flush_input (struct serial *); | |
101 | static int hardwire_send_break (struct serial *); | |
102 | static int hardwire_setstopbits (struct serial *, int); | |
103 | ||
c2c6d25f JM |
104 | void _initialize_ser_hardwire (void); |
105 | ||
c906108c SS |
106 | /* Open up a real live device for serial I/O */ |
107 | ||
108 | static int | |
819cc324 | 109 | hardwire_open (struct serial *scb, const char *name) |
c906108c SS |
110 | { |
111 | scb->fd = open (name, O_RDWR); | |
112 | if (scb->fd < 0) | |
113 | return -1; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | static int | |
819cc324 | 119 | get_tty_state (struct serial *scb, struct hardwire_ttystate *state) |
c906108c SS |
120 | { |
121 | #ifdef HAVE_TERMIOS | |
c5aa993b | 122 | if (tcgetattr (scb->fd, &state->termios) < 0) |
c906108c SS |
123 | return -1; |
124 | ||
125 | return 0; | |
126 | #endif | |
127 | ||
128 | #ifdef HAVE_TERMIO | |
129 | if (ioctl (scb->fd, TCGETA, &state->termio) < 0) | |
130 | return -1; | |
131 | return 0; | |
132 | #endif | |
133 | ||
134 | #ifdef HAVE_SGTTY | |
135 | if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0) | |
136 | return -1; | |
137 | if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0) | |
138 | return -1; | |
139 | if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0) | |
140 | return -1; | |
141 | if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0) | |
142 | return -1; | |
143 | ||
144 | return 0; | |
145 | #endif | |
146 | } | |
147 | ||
148 | static int | |
819cc324 | 149 | set_tty_state (struct serial *scb, struct hardwire_ttystate *state) |
c906108c SS |
150 | { |
151 | #ifdef HAVE_TERMIOS | |
c5aa993b | 152 | if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0) |
c906108c SS |
153 | return -1; |
154 | ||
155 | return 0; | |
156 | #endif | |
157 | ||
158 | #ifdef HAVE_TERMIO | |
159 | if (ioctl (scb->fd, TCSETA, &state->termio) < 0) | |
160 | return -1; | |
161 | return 0; | |
162 | #endif | |
163 | ||
164 | #ifdef HAVE_SGTTY | |
165 | if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0) | |
166 | return -1; | |
167 | if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0) | |
168 | return -1; | |
169 | if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0) | |
170 | return -1; | |
171 | if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0) | |
172 | return -1; | |
173 | ||
174 | return 0; | |
175 | #endif | |
176 | } | |
177 | ||
178 | static serial_ttystate | |
819cc324 | 179 | hardwire_get_tty_state (struct serial *scb) |
c906108c SS |
180 | { |
181 | struct hardwire_ttystate *state; | |
182 | ||
c5aa993b | 183 | state = (struct hardwire_ttystate *) xmalloc (sizeof *state); |
c906108c | 184 | |
c5aa993b | 185 | if (get_tty_state (scb, state)) |
c906108c SS |
186 | return NULL; |
187 | ||
c5aa993b | 188 | return (serial_ttystate) state; |
c906108c SS |
189 | } |
190 | ||
191 | static int | |
819cc324 | 192 | hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate) |
c906108c SS |
193 | { |
194 | struct hardwire_ttystate *state; | |
195 | ||
c5aa993b | 196 | state = (struct hardwire_ttystate *) ttystate; |
c906108c | 197 | |
c5aa993b | 198 | return set_tty_state (scb, state); |
c906108c SS |
199 | } |
200 | ||
201 | static int | |
819cc324 | 202 | hardwire_noflush_set_tty_state (struct serial *scb, |
c2c6d25f JM |
203 | serial_ttystate new_ttystate, |
204 | serial_ttystate old_ttystate) | |
c906108c SS |
205 | { |
206 | struct hardwire_ttystate new_state; | |
207 | #ifdef HAVE_SGTTY | |
208 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate; | |
209 | #endif | |
210 | ||
c5aa993b | 211 | new_state = *(struct hardwire_ttystate *) new_ttystate; |
c906108c SS |
212 | |
213 | /* Don't change in or out of raw mode; we don't want to flush input. | |
214 | termio and termios have no such restriction; for them flushing input | |
215 | is separate from setting the attributes. */ | |
216 | ||
217 | #ifdef HAVE_SGTTY | |
218 | if (state->sgttyb.sg_flags & RAW) | |
219 | new_state.sgttyb.sg_flags |= RAW; | |
220 | else | |
221 | new_state.sgttyb.sg_flags &= ~RAW; | |
222 | ||
223 | /* I'm not sure whether this is necessary; the manpage just mentions | |
224 | RAW not CBREAK. */ | |
225 | if (state->sgttyb.sg_flags & CBREAK) | |
226 | new_state.sgttyb.sg_flags |= CBREAK; | |
227 | else | |
228 | new_state.sgttyb.sg_flags &= ~CBREAK; | |
229 | #endif | |
230 | ||
231 | return set_tty_state (scb, &new_state); | |
232 | } | |
233 | ||
234 | static void | |
819cc324 | 235 | hardwire_print_tty_state (struct serial *scb, |
c2c6d25f | 236 | serial_ttystate ttystate, |
d9fcf2fb | 237 | struct ui_file *stream) |
c906108c SS |
238 | { |
239 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate; | |
240 | int i; | |
241 | ||
242 | #ifdef HAVE_TERMIOS | |
c2c6d25f | 243 | fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", |
2acceee2 JM |
244 | (int) state->termios.c_iflag, |
245 | (int) state->termios.c_oflag); | |
c2c6d25f | 246 | fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n", |
2acceee2 JM |
247 | (int) state->termios.c_cflag, |
248 | (int) state->termios.c_lflag); | |
c906108c SS |
249 | #if 0 |
250 | /* This not in POSIX, and is not really documented by those systems | |
251 | which have it (at least not Sun). */ | |
c2c6d25f | 252 | fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line); |
c906108c | 253 | #endif |
c2c6d25f | 254 | fprintf_filtered (stream, "c_cc: "); |
c906108c | 255 | for (i = 0; i < NCCS; i += 1) |
c2c6d25f JM |
256 | fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]); |
257 | fprintf_filtered (stream, "\n"); | |
c906108c SS |
258 | #endif |
259 | ||
260 | #ifdef HAVE_TERMIO | |
c2c6d25f JM |
261 | fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", |
262 | state->termio.c_iflag, state->termio.c_oflag); | |
263 | fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", | |
264 | state->termio.c_cflag, state->termio.c_lflag, | |
265 | state->termio.c_line); | |
266 | fprintf_filtered (stream, "c_cc: "); | |
c906108c | 267 | for (i = 0; i < NCC; i += 1) |
c2c6d25f JM |
268 | fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]); |
269 | fprintf_filtered (stream, "\n"); | |
c906108c SS |
270 | #endif |
271 | ||
272 | #ifdef HAVE_SGTTY | |
c2c6d25f JM |
273 | fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n", |
274 | state->sgttyb.sg_flags); | |
c906108c | 275 | |
c2c6d25f | 276 | fprintf_filtered (stream, "tchars: "); |
c5aa993b | 277 | for (i = 0; i < (int) sizeof (struct tchars); i++) |
c2c6d25f | 278 | fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]); |
64122a8b | 279 | fprintf_filtered (stream, "\n"); |
c906108c | 280 | |
c2c6d25f | 281 | fprintf_filtered (stream, "ltchars: "); |
c5aa993b | 282 | for (i = 0; i < (int) sizeof (struct ltchars); i++) |
c2c6d25f JM |
283 | fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]); |
284 | fprintf_filtered (stream, "\n"); | |
c906108c | 285 | |
c2c6d25f | 286 | fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode); |
c906108c SS |
287 | #endif |
288 | } | |
289 | ||
290 | /* Wait for the output to drain away, as opposed to flushing (discarding) it */ | |
291 | ||
292 | static int | |
819cc324 | 293 | hardwire_drain_output (struct serial *scb) |
c906108c SS |
294 | { |
295 | #ifdef HAVE_TERMIOS | |
296 | return tcdrain (scb->fd); | |
297 | #endif | |
298 | ||
299 | #ifdef HAVE_TERMIO | |
300 | return ioctl (scb->fd, TCSBRK, 1); | |
301 | #endif | |
302 | ||
303 | #ifdef HAVE_SGTTY | |
304 | /* Get the current state and then restore it using TIOCSETP, | |
305 | which should cause the output to drain and pending input | |
306 | to be discarded. */ | |
307 | { | |
308 | struct hardwire_ttystate state; | |
433759f7 | 309 | |
c906108c SS |
310 | if (get_tty_state (scb, &state)) |
311 | { | |
312 | return (-1); | |
313 | } | |
314 | else | |
315 | { | |
316 | return (ioctl (scb->fd, TIOCSETP, &state.sgttyb)); | |
317 | } | |
318 | } | |
c5aa993b | 319 | #endif |
c906108c SS |
320 | } |
321 | ||
322 | static int | |
819cc324 | 323 | hardwire_flush_output (struct serial *scb) |
c906108c SS |
324 | { |
325 | #ifdef HAVE_TERMIOS | |
326 | return tcflush (scb->fd, TCOFLUSH); | |
327 | #endif | |
328 | ||
329 | #ifdef HAVE_TERMIO | |
330 | return ioctl (scb->fd, TCFLSH, 1); | |
331 | #endif | |
332 | ||
333 | #ifdef HAVE_SGTTY | |
334 | /* This flushes both input and output, but we can't do better. */ | |
335 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
c5aa993b | 336 | #endif |
c906108c SS |
337 | } |
338 | ||
339 | static int | |
819cc324 | 340 | hardwire_flush_input (struct serial *scb) |
c906108c | 341 | { |
dd5da072 | 342 | ser_base_flush_input (scb); |
c906108c SS |
343 | |
344 | #ifdef HAVE_TERMIOS | |
345 | return tcflush (scb->fd, TCIFLUSH); | |
346 | #endif | |
347 | ||
348 | #ifdef HAVE_TERMIO | |
349 | return ioctl (scb->fd, TCFLSH, 0); | |
350 | #endif | |
351 | ||
352 | #ifdef HAVE_SGTTY | |
353 | /* This flushes both input and output, but we can't do better. */ | |
354 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
c5aa993b | 355 | #endif |
c906108c SS |
356 | } |
357 | ||
358 | static int | |
819cc324 | 359 | hardwire_send_break (struct serial *scb) |
c906108c SS |
360 | { |
361 | #ifdef HAVE_TERMIOS | |
362 | return tcsendbreak (scb->fd, 0); | |
363 | #endif | |
364 | ||
365 | #ifdef HAVE_TERMIO | |
366 | return ioctl (scb->fd, TCSBRK, 0); | |
367 | #endif | |
368 | ||
369 | #ifdef HAVE_SGTTY | |
370 | { | |
371 | int status; | |
c906108c SS |
372 | |
373 | status = ioctl (scb->fd, TIOCSBRK, 0); | |
374 | ||
375 | /* Can't use usleep; it doesn't exist in BSD 4.2. */ | |
dcb626be JB |
376 | /* Note that if this gdb_select() is interrupted by a signal it will not |
377 | wait the full length of time. I think that is OK. */ | |
378 | gdb_usleep (250000); | |
c906108c SS |
379 | status = ioctl (scb->fd, TIOCCBRK, 0); |
380 | return status; | |
381 | } | |
c5aa993b | 382 | #endif |
c906108c SS |
383 | } |
384 | ||
385 | static void | |
819cc324 | 386 | hardwire_raw (struct serial *scb) |
c906108c SS |
387 | { |
388 | struct hardwire_ttystate state; | |
389 | ||
c5aa993b JM |
390 | if (get_tty_state (scb, &state)) |
391 | fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno)); | |
c906108c SS |
392 | |
393 | #ifdef HAVE_TERMIOS | |
394 | state.termios.c_iflag = 0; | |
395 | state.termios.c_oflag = 0; | |
396 | state.termios.c_lflag = 0; | |
c5aa993b | 397 | state.termios.c_cflag &= ~(CSIZE | PARENB); |
c906108c | 398 | state.termios.c_cflag |= CLOCAL | CS8; |
23776285 MR |
399 | #ifdef CRTSCTS |
400 | /* h/w flow control. */ | |
401 | if (serial_hwflow) | |
402 | state.termios.c_cflag |= CRTSCTS; | |
403 | else | |
404 | state.termios.c_cflag &= ~CRTSCTS; | |
405 | #ifdef CRTS_IFLOW | |
406 | if (serial_hwflow) | |
407 | state.termios.c_cflag |= CRTS_IFLOW; | |
408 | else | |
409 | state.termios.c_cflag &= ~CRTS_IFLOW; | |
410 | #endif | |
411 | #endif | |
c906108c SS |
412 | state.termios.c_cc[VMIN] = 0; |
413 | state.termios.c_cc[VTIME] = 0; | |
414 | #endif | |
415 | ||
416 | #ifdef HAVE_TERMIO | |
417 | state.termio.c_iflag = 0; | |
418 | state.termio.c_oflag = 0; | |
419 | state.termio.c_lflag = 0; | |
c5aa993b | 420 | state.termio.c_cflag &= ~(CSIZE | PARENB); |
c906108c SS |
421 | state.termio.c_cflag |= CLOCAL | CS8; |
422 | state.termio.c_cc[VMIN] = 0; | |
423 | state.termio.c_cc[VTIME] = 0; | |
424 | #endif | |
425 | ||
426 | #ifdef HAVE_SGTTY | |
427 | state.sgttyb.sg_flags |= RAW | ANYP; | |
428 | state.sgttyb.sg_flags &= ~(CBREAK | ECHO); | |
429 | #endif | |
430 | ||
431 | scb->current_timeout = 0; | |
432 | ||
433 | if (set_tty_state (scb, &state)) | |
c5aa993b | 434 | fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno)); |
c906108c SS |
435 | } |
436 | ||
437 | /* Wait for input on scb, with timeout seconds. Returns 0 on success, | |
438 | otherwise SERIAL_TIMEOUT or SERIAL_ERROR. | |
439 | ||
440 | For termio{s}, we actually just setup VTIME if necessary, and let the | |
441 | timeout occur in the read() in hardwire_read(). | |
442 | */ | |
443 | ||
2acceee2 | 444 | /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent |
b4505029 | 445 | ser_base*() until the old TERMIOS/SGTTY/... timer code has been |
2acceee2 JM |
446 | flushed. . */ |
447 | ||
448 | /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only | |
449 | possible values of the TIMEOUT parameter are ONE and ZERO. | |
450 | Consequently all the code that tries to handle the possability of | |
451 | an overflowed timer is unnecessary. */ | |
c2c6d25f | 452 | |
c906108c | 453 | static int |
819cc324 | 454 | wait_for (struct serial *scb, int timeout) |
c906108c | 455 | { |
c906108c | 456 | #ifdef HAVE_SGTTY |
ab5ba170 AC |
457 | while (1) |
458 | { | |
459 | struct timeval tv; | |
460 | fd_set readfds; | |
461 | int numfds; | |
c906108c | 462 | |
ab5ba170 AC |
463 | /* NOTE: Some OS's can scramble the READFDS when the select() |
464 | call fails (ex the kernel with Red Hat 5.2). Initialize all | |
465 | arguments before each call. */ | |
c906108c | 466 | |
ab5ba170 AC |
467 | tv.tv_sec = timeout; |
468 | tv.tv_usec = 0; | |
c906108c | 469 | |
ab5ba170 AC |
470 | FD_ZERO (&readfds); |
471 | FD_SET (scb->fd, &readfds); | |
c906108c | 472 | |
ab5ba170 | 473 | if (timeout >= 0) |
0ea3f30e | 474 | numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv); |
ab5ba170 | 475 | else |
0ea3f30e | 476 | numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0); |
c906108c | 477 | |
ab5ba170 AC |
478 | if (numfds <= 0) |
479 | if (numfds == 0) | |
480 | return SERIAL_TIMEOUT; | |
481 | else if (errno == EINTR) | |
482 | continue; | |
c906108c | 483 | else |
ab5ba170 | 484 | return SERIAL_ERROR; /* Got an error from select or poll */ |
c906108c | 485 | |
ab5ba170 AC |
486 | return 0; |
487 | } | |
c5aa993b | 488 | #endif /* HAVE_SGTTY */ |
c906108c SS |
489 | |
490 | #if defined HAVE_TERMIO || defined HAVE_TERMIOS | |
491 | if (timeout == scb->current_timeout) | |
492 | return 0; | |
493 | ||
494 | scb->current_timeout = timeout; | |
495 | ||
496 | { | |
497 | struct hardwire_ttystate state; | |
498 | ||
c5aa993b JM |
499 | if (get_tty_state (scb, &state)) |
500 | fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno)); | |
c906108c SS |
501 | |
502 | #ifdef HAVE_TERMIOS | |
503 | if (timeout < 0) | |
504 | { | |
505 | /* No timeout. */ | |
506 | state.termios.c_cc[VTIME] = 0; | |
507 | state.termios.c_cc[VMIN] = 1; | |
508 | } | |
509 | else | |
510 | { | |
511 | state.termios.c_cc[VMIN] = 0; | |
512 | state.termios.c_cc[VTIME] = timeout * 10; | |
513 | if (state.termios.c_cc[VTIME] != timeout * 10) | |
514 | { | |
515 | ||
516 | /* If c_cc is an 8-bit signed character, we can't go | |
517 | bigger than this. If it is always unsigned, we could use | |
518 | 25. */ | |
519 | ||
520 | scb->current_timeout = 12; | |
521 | state.termios.c_cc[VTIME] = scb->current_timeout * 10; | |
522 | scb->timeout_remaining = timeout - scb->current_timeout; | |
523 | } | |
524 | } | |
525 | #endif | |
526 | ||
527 | #ifdef HAVE_TERMIO | |
528 | if (timeout < 0) | |
529 | { | |
530 | /* No timeout. */ | |
531 | state.termio.c_cc[VTIME] = 0; | |
532 | state.termio.c_cc[VMIN] = 1; | |
533 | } | |
534 | else | |
535 | { | |
536 | state.termio.c_cc[VMIN] = 0; | |
537 | state.termio.c_cc[VTIME] = timeout * 10; | |
538 | if (state.termio.c_cc[VTIME] != timeout * 10) | |
539 | { | |
540 | /* If c_cc is an 8-bit signed character, we can't go | |
541 | bigger than this. If it is always unsigned, we could use | |
542 | 25. */ | |
543 | ||
544 | scb->current_timeout = 12; | |
545 | state.termio.c_cc[VTIME] = scb->current_timeout * 10; | |
546 | scb->timeout_remaining = timeout - scb->current_timeout; | |
547 | } | |
548 | } | |
549 | #endif | |
550 | ||
551 | if (set_tty_state (scb, &state)) | |
c5aa993b | 552 | fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno)); |
c906108c SS |
553 | |
554 | return 0; | |
555 | } | |
c5aa993b | 556 | #endif /* HAVE_TERMIO || HAVE_TERMIOS */ |
c906108c SS |
557 | } |
558 | ||
559 | /* Read a character with user-specified timeout. TIMEOUT is number of seconds | |
560 | to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns | |
561 | char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line | |
562 | dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */ | |
c2c6d25f JM |
563 | |
564 | /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent | |
b4505029 | 565 | ser_base*() until the old TERMIOS/SGTTY/... timer code has been |
c2c6d25f JM |
566 | flushed. */ |
567 | ||
568 | /* NOTE: cagney/1999-09-16: This function is not identical to | |
b4505029 | 569 | ser_base_readchar() as part of replacing it with ser_base*() |
c2c6d25f | 570 | merging will be required - this code handles the case where read() |
b4505029 | 571 | times out due to no data while ser_base_readchar() doesn't expect |
c2c6d25f JM |
572 | that. */ |
573 | ||
c906108c | 574 | static int |
819cc324 | 575 | do_hardwire_readchar (struct serial *scb, int timeout) |
c906108c | 576 | { |
7a292a7a SS |
577 | int status, delta; |
578 | int detach = 0; | |
c906108c | 579 | |
c906108c SS |
580 | if (timeout > 0) |
581 | timeout++; | |
c906108c | 582 | |
2c1ab592 MS |
583 | /* We have to be able to keep the GUI alive here, so we break the |
584 | original timeout into steps of 1 second, running the "keep the | |
585 | GUI alive" hook each time through the loop. | |
586 | ||
587 | Also, timeout = 0 means to poll, so we just set the delta to 0, | |
588 | so we will only go through the loop once. */ | |
c5aa993b | 589 | |
7a292a7a | 590 | delta = (timeout == 0 ? 0 : 1); |
c906108c SS |
591 | while (1) |
592 | { | |
c906108c | 593 | |
7a292a7a SS |
594 | /* N.B. The UI may destroy our world (for instance by calling |
595 | remote_stop,) in which case we want to get out of here as | |
596 | quickly as possible. It is not safe to touch scb, since | |
98bbd631 AC |
597 | someone else might have freed it. The |
598 | deprecated_ui_loop_hook signals that we should exit by | |
599 | returning 1. */ | |
7a292a7a | 600 | |
98bbd631 AC |
601 | if (deprecated_ui_loop_hook) |
602 | detach = deprecated_ui_loop_hook (0); | |
7a292a7a SS |
603 | |
604 | if (detach) | |
605 | return SERIAL_TIMEOUT; | |
606 | ||
607 | scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta); | |
608 | status = wait_for (scb, delta); | |
609 | ||
c906108c SS |
610 | if (status < 0) |
611 | return status; | |
612 | ||
2acceee2 | 613 | status = read (scb->fd, scb->buf, BUFSIZ); |
c906108c | 614 | |
2acceee2 | 615 | if (status <= 0) |
c906108c | 616 | { |
2acceee2 | 617 | if (status == 0) |
c906108c SS |
618 | { |
619 | /* Zero characters means timeout (it could also be EOF, but | |
c5aa993b | 620 | we don't (yet at least) distinguish). */ |
c906108c SS |
621 | if (scb->timeout_remaining > 0) |
622 | { | |
623 | timeout = scb->timeout_remaining; | |
624 | continue; | |
625 | } | |
c5aa993b JM |
626 | else if (scb->timeout_remaining < 0) |
627 | continue; | |
c906108c SS |
628 | else |
629 | return SERIAL_TIMEOUT; | |
630 | } | |
631 | else if (errno == EINTR) | |
632 | continue; | |
633 | else | |
634 | return SERIAL_ERROR; /* Got an error from read */ | |
635 | } | |
636 | ||
2acceee2 | 637 | scb->bufcnt = status; |
c906108c SS |
638 | scb->bufcnt--; |
639 | scb->bufp = scb->buf; | |
640 | return *scb->bufp++; | |
641 | } | |
642 | } | |
643 | ||
2acceee2 | 644 | static int |
819cc324 | 645 | hardwire_readchar (struct serial *scb, int timeout) |
2acceee2 JM |
646 | { |
647 | return generic_readchar (scb, timeout, do_hardwire_readchar); | |
648 | } | |
649 | ||
650 | ||
c906108c SS |
651 | #ifndef B19200 |
652 | #define B19200 EXTA | |
653 | #endif | |
654 | ||
655 | #ifndef B38400 | |
656 | #define B38400 EXTB | |
657 | #endif | |
658 | ||
659 | /* Translate baud rates from integers to damn B_codes. Unix should | |
660 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
661 | ||
662 | static struct | |
663 | { | |
664 | int rate; | |
665 | int code; | |
666 | } | |
667 | baudtab[] = | |
668 | { | |
c5aa993b JM |
669 | { |
670 | 50, B50 | |
671 | } | |
672 | , | |
673 | { | |
674 | 75, B75 | |
675 | } | |
676 | , | |
677 | { | |
678 | 110, B110 | |
679 | } | |
680 | , | |
681 | { | |
682 | 134, B134 | |
683 | } | |
684 | , | |
685 | { | |
686 | 150, B150 | |
687 | } | |
688 | , | |
689 | { | |
690 | 200, B200 | |
691 | } | |
692 | , | |
693 | { | |
694 | 300, B300 | |
695 | } | |
696 | , | |
697 | { | |
698 | 600, B600 | |
699 | } | |
700 | , | |
701 | { | |
702 | 1200, B1200 | |
703 | } | |
704 | , | |
705 | { | |
706 | 1800, B1800 | |
707 | } | |
708 | , | |
709 | { | |
710 | 2400, B2400 | |
711 | } | |
712 | , | |
713 | { | |
714 | 4800, B4800 | |
715 | } | |
716 | , | |
717 | { | |
718 | 9600, B9600 | |
719 | } | |
720 | , | |
721 | { | |
722 | 19200, B19200 | |
723 | } | |
724 | , | |
725 | { | |
726 | 38400, B38400 | |
727 | } | |
728 | , | |
c906108c | 729 | #ifdef B57600 |
c5aa993b JM |
730 | { |
731 | 57600, B57600 | |
732 | } | |
733 | , | |
c906108c SS |
734 | #endif |
735 | #ifdef B115200 | |
c5aa993b JM |
736 | { |
737 | 115200, B115200 | |
738 | } | |
739 | , | |
c906108c SS |
740 | #endif |
741 | #ifdef B230400 | |
c5aa993b JM |
742 | { |
743 | 230400, B230400 | |
744 | } | |
745 | , | |
c906108c SS |
746 | #endif |
747 | #ifdef B460800 | |
c5aa993b JM |
748 | { |
749 | 460800, B460800 | |
750 | } | |
751 | , | |
c906108c | 752 | #endif |
c5aa993b JM |
753 | { |
754 | -1, -1 | |
755 | } | |
756 | , | |
c906108c SS |
757 | }; |
758 | ||
c5aa993b | 759 | static int |
c2c6d25f | 760 | rate_to_code (int rate) |
c906108c SS |
761 | { |
762 | int i; | |
763 | ||
764 | for (i = 0; baudtab[i].rate != -1; i++) | |
08b4f080 FN |
765 | { |
766 | /* test for perfect macth. */ | |
767 | if (rate == baudtab[i].rate) | |
768 | return baudtab[i].code; | |
769 | else | |
770 | { | |
771 | /* check if it is in between valid values. */ | |
772 | if (rate < baudtab[i].rate) | |
773 | { | |
774 | if (i) | |
775 | { | |
8a3fe4f8 | 776 | warning (_("Invalid baud rate %d. Closest values are %d and %d."), |
08b4f080 FN |
777 | rate, baudtab[i - 1].rate, baudtab[i].rate); |
778 | } | |
779 | else | |
780 | { | |
8a3fe4f8 | 781 | warning (_("Invalid baud rate %d. Minimum value is %d."), |
08b4f080 FN |
782 | rate, baudtab[0].rate); |
783 | } | |
784 | return -1; | |
785 | } | |
786 | } | |
787 | } | |
788 | ||
789 | /* The requested speed was too large. */ | |
8a3fe4f8 | 790 | warning (_("Invalid baud rate %d. Maximum value is %d."), |
08b4f080 | 791 | rate, baudtab[i - 1].rate); |
c906108c SS |
792 | return -1; |
793 | } | |
794 | ||
795 | static int | |
819cc324 | 796 | hardwire_setbaudrate (struct serial *scb, int rate) |
c906108c SS |
797 | { |
798 | struct hardwire_ttystate state; | |
08b4f080 FN |
799 | int baud_code = rate_to_code (rate); |
800 | ||
801 | if (baud_code < 0) | |
802 | { | |
803 | /* The baud rate was not valid. | |
804 | A warning has already been issued. */ | |
805 | errno = EINVAL; | |
806 | return -1; | |
807 | } | |
c906108c | 808 | |
c5aa993b | 809 | if (get_tty_state (scb, &state)) |
c906108c SS |
810 | return -1; |
811 | ||
812 | #ifdef HAVE_TERMIOS | |
08b4f080 FN |
813 | cfsetospeed (&state.termios, baud_code); |
814 | cfsetispeed (&state.termios, baud_code); | |
c906108c SS |
815 | #endif |
816 | ||
817 | #ifdef HAVE_TERMIO | |
818 | #ifndef CIBAUD | |
819 | #define CIBAUD CBAUD | |
820 | #endif | |
821 | ||
822 | state.termio.c_cflag &= ~(CBAUD | CIBAUD); | |
08b4f080 | 823 | state.termio.c_cflag |= baud_code; |
c906108c SS |
824 | #endif |
825 | ||
826 | #ifdef HAVE_SGTTY | |
08b4f080 FN |
827 | state.sgttyb.sg_ispeed = baud_code; |
828 | state.sgttyb.sg_ospeed = baud_code; | |
c906108c SS |
829 | #endif |
830 | ||
831 | return set_tty_state (scb, &state); | |
832 | } | |
833 | ||
834 | static int | |
819cc324 | 835 | hardwire_setstopbits (struct serial *scb, int num) |
c906108c SS |
836 | { |
837 | struct hardwire_ttystate state; | |
838 | int newbit; | |
839 | ||
c5aa993b | 840 | if (get_tty_state (scb, &state)) |
c906108c SS |
841 | return -1; |
842 | ||
843 | switch (num) | |
844 | { | |
845 | case SERIAL_1_STOPBITS: | |
846 | newbit = 0; | |
847 | break; | |
848 | case SERIAL_1_AND_A_HALF_STOPBITS: | |
849 | case SERIAL_2_STOPBITS: | |
850 | newbit = 1; | |
851 | break; | |
852 | default: | |
853 | return 1; | |
854 | } | |
855 | ||
856 | #ifdef HAVE_TERMIOS | |
857 | if (!newbit) | |
858 | state.termios.c_cflag &= ~CSTOPB; | |
859 | else | |
c5aa993b | 860 | state.termios.c_cflag |= CSTOPB; /* two bits */ |
c906108c SS |
861 | #endif |
862 | ||
863 | #ifdef HAVE_TERMIO | |
864 | if (!newbit) | |
865 | state.termio.c_cflag &= ~CSTOPB; | |
866 | else | |
c5aa993b | 867 | state.termio.c_cflag |= CSTOPB; /* two bits */ |
c906108c SS |
868 | #endif |
869 | ||
870 | #ifdef HAVE_SGTTY | |
871 | return 0; /* sgtty doesn't support this */ | |
872 | #endif | |
873 | ||
874 | return set_tty_state (scb, &state); | |
875 | } | |
876 | ||
c906108c | 877 | static void |
819cc324 | 878 | hardwire_close (struct serial *scb) |
c906108c SS |
879 | { |
880 | if (scb->fd < 0) | |
881 | return; | |
882 | ||
c5aa993b | 883 | close (scb->fd); |
c906108c SS |
884 | scb->fd = -1; |
885 | } | |
c2c6d25f | 886 | \f |
2acceee2 | 887 | \f |
c906108c | 888 | void |
c2c6d25f | 889 | _initialize_ser_hardwire (void) |
c906108c | 890 | { |
c2c6d25f | 891 | struct serial_ops *ops = XMALLOC (struct serial_ops); |
433759f7 | 892 | |
2fdbdd39 | 893 | memset (ops, 0, sizeof (struct serial_ops)); |
c2c6d25f JM |
894 | ops->name = "hardwire"; |
895 | ops->next = 0; | |
896 | ops->open = hardwire_open; | |
897 | ops->close = hardwire_close; | |
b4505029 | 898 | /* FIXME: Don't replace this with the equivalent ser_base*() until |
c2c6d25f JM |
899 | the old TERMIOS/SGTTY/... timer code has been flushed. cagney |
900 | 1999-09-16. */ | |
901 | ops->readchar = hardwire_readchar; | |
dd5da072 | 902 | ops->write = ser_base_write; |
c2c6d25f JM |
903 | ops->flush_output = hardwire_flush_output; |
904 | ops->flush_input = hardwire_flush_input; | |
905 | ops->send_break = hardwire_send_break; | |
906 | ops->go_raw = hardwire_raw; | |
907 | ops->get_tty_state = hardwire_get_tty_state; | |
908 | ops->set_tty_state = hardwire_set_tty_state; | |
909 | ops->print_tty_state = hardwire_print_tty_state; | |
910 | ops->noflush_set_tty_state = hardwire_noflush_set_tty_state; | |
911 | ops->setbaudrate = hardwire_setbaudrate; | |
912 | ops->setstopbits = hardwire_setstopbits; | |
913 | ops->drain_output = hardwire_drain_output; | |
dd5da072 | 914 | ops->async = ser_base_async; |
b4505029 MM |
915 | ops->read_prim = ser_unix_read_prim; |
916 | ops->write_prim = ser_unix_write_prim; | |
c2c6d25f | 917 | serial_add_interface (ops); |
23776285 MR |
918 | |
919 | #ifdef HAVE_TERMIOS | |
920 | #ifdef CRTSCTS | |
921 | add_setshow_boolean_cmd ("remoteflow", no_class, | |
922 | &serial_hwflow, _("\ | |
923 | Set use of hardware flow control for remote serial I/O."), _("\ | |
924 | Show use of hardware flow control for remote serial I/O."), _("\ | |
925 | Enable or disable hardware flow control (RTS/CTS) on the serial port\n\ | |
926 | when debugging using remote targets."), | |
927 | NULL, | |
928 | show_serial_hwflow, | |
929 | &setlist, &showlist); | |
930 | #endif | |
931 | #endif | |
c906108c | 932 | } |
b4505029 MM |
933 | |
934 | int | |
935 | ser_unix_read_prim (struct serial *scb, size_t count) | |
936 | { | |
937 | int status; | |
938 | ||
939 | while (1) | |
940 | { | |
941 | status = read (scb->fd, scb->buf, count); | |
942 | if (status != -1 || errno != EINTR) | |
943 | break; | |
944 | } | |
945 | return status; | |
946 | } | |
947 | ||
948 | int | |
949 | ser_unix_write_prim (struct serial *scb, const void *buf, size_t len) | |
950 | { | |
951 | /* ??? Historically, GDB has not retried calls to "write" that | |
952 | result in EINTR. */ | |
953 | return write (scb->fd, buf, len); | |
954 | } |