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