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