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