1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2003, 2004, 2005 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include "gdb_select.h"
35 #include "gdb_string.h"
39 struct hardwire_ttystate
41 struct termios termios;
47 /* It is believed that all systems which have added job control to SVR3
48 (e.g. sco) have also added termios. Even if not, trying to figure out
49 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
50 bewildering. So we don't attempt it. */
52 struct hardwire_ttystate
59 struct hardwire_ttystate
64 /* Line discipline flags. */
69 static int hardwire_open (struct serial *scb, const char *name);
70 static void hardwire_raw (struct serial *scb);
71 static int wait_for (struct serial *scb, int timeout);
72 static int hardwire_readchar (struct serial *scb, int timeout);
73 static int do_hardwire_readchar (struct serial *scb, int timeout);
74 static int rate_to_code (int rate);
75 static int hardwire_setbaudrate (struct serial *scb, int rate);
76 static void hardwire_close (struct serial *scb);
77 static int get_tty_state (struct serial *scb,
78 struct hardwire_ttystate * state);
79 static int set_tty_state (struct serial *scb,
80 struct hardwire_ttystate * state);
81 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
82 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
83 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
85 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
87 static int hardwire_drain_output (struct serial *);
88 static int hardwire_flush_output (struct serial *);
89 static int hardwire_flush_input (struct serial *);
90 static int hardwire_send_break (struct serial *);
91 static int hardwire_setstopbits (struct serial *, int);
93 void _initialize_ser_hardwire (void);
95 /* Open up a real live device for serial I/O */
98 hardwire_open (struct serial *scb, const char *name)
100 scb->fd = open (name, O_RDWR);
108 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
111 if (tcgetattr (scb->fd, &state->termios) < 0)
118 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
124 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
126 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
128 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
130 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
138 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
141 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
148 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
154 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
156 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
158 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
160 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
167 static serial_ttystate
168 hardwire_get_tty_state (struct serial *scb)
170 struct hardwire_ttystate *state;
172 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
174 if (get_tty_state (scb, state))
177 return (serial_ttystate) state;
181 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
183 struct hardwire_ttystate *state;
185 state = (struct hardwire_ttystate *) ttystate;
187 return set_tty_state (scb, state);
191 hardwire_noflush_set_tty_state (struct serial *scb,
192 serial_ttystate new_ttystate,
193 serial_ttystate old_ttystate)
195 struct hardwire_ttystate new_state;
197 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
200 new_state = *(struct hardwire_ttystate *) new_ttystate;
202 /* Don't change in or out of raw mode; we don't want to flush input.
203 termio and termios have no such restriction; for them flushing input
204 is separate from setting the attributes. */
207 if (state->sgttyb.sg_flags & RAW)
208 new_state.sgttyb.sg_flags |= RAW;
210 new_state.sgttyb.sg_flags &= ~RAW;
212 /* I'm not sure whether this is necessary; the manpage just mentions
214 if (state->sgttyb.sg_flags & CBREAK)
215 new_state.sgttyb.sg_flags |= CBREAK;
217 new_state.sgttyb.sg_flags &= ~CBREAK;
220 return set_tty_state (scb, &new_state);
224 hardwire_print_tty_state (struct serial *scb,
225 serial_ttystate ttystate,
226 struct ui_file *stream)
228 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
232 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
233 (int) state->termios.c_iflag,
234 (int) state->termios.c_oflag);
235 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
236 (int) state->termios.c_cflag,
237 (int) state->termios.c_lflag);
239 /* This not in POSIX, and is not really documented by those systems
240 which have it (at least not Sun). */
241 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
243 fprintf_filtered (stream, "c_cc: ");
244 for (i = 0; i < NCCS; i += 1)
245 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
246 fprintf_filtered (stream, "\n");
250 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
251 state->termio.c_iflag, state->termio.c_oflag);
252 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
253 state->termio.c_cflag, state->termio.c_lflag,
254 state->termio.c_line);
255 fprintf_filtered (stream, "c_cc: ");
256 for (i = 0; i < NCC; i += 1)
257 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
258 fprintf_filtered (stream, "\n");
262 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
263 state->sgttyb.sg_flags);
265 fprintf_filtered (stream, "tchars: ");
266 for (i = 0; i < (int) sizeof (struct tchars); i++)
267 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
268 fprintf_filtered (stream, "\n");
270 fprintf_filtered (stream, "ltchars: ");
271 for (i = 0; i < (int) sizeof (struct ltchars); i++)
272 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
273 fprintf_filtered (stream, "\n");
275 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
279 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
282 hardwire_drain_output (struct serial *scb)
285 return tcdrain (scb->fd);
289 return ioctl (scb->fd, TCSBRK, 1);
293 /* Get the current state and then restore it using TIOCSETP,
294 which should cause the output to drain and pending input
297 struct hardwire_ttystate state;
298 if (get_tty_state (scb, &state))
304 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
311 hardwire_flush_output (struct serial *scb)
314 return tcflush (scb->fd, TCOFLUSH);
318 return ioctl (scb->fd, TCFLSH, 1);
322 /* This flushes both input and output, but we can't do better. */
323 return ioctl (scb->fd, TIOCFLUSH, 0);
328 hardwire_flush_input (struct serial *scb)
330 ser_base_flush_input (scb);
333 return tcflush (scb->fd, TCIFLUSH);
337 return ioctl (scb->fd, TCFLSH, 0);
341 /* This flushes both input and output, but we can't do better. */
342 return ioctl (scb->fd, TIOCFLUSH, 0);
347 hardwire_send_break (struct serial *scb)
350 return tcsendbreak (scb->fd, 0);
354 return ioctl (scb->fd, TCSBRK, 0);
360 struct timeval timeout;
362 status = ioctl (scb->fd, TIOCSBRK, 0);
364 /* Can't use usleep; it doesn't exist in BSD 4.2. */
365 /* Note that if this select() is interrupted by a signal it will not wait
366 the full length of time. I think that is OK. */
368 timeout.tv_usec = 250000;
369 gdb_select (0, 0, 0, 0, &timeout);
370 status = ioctl (scb->fd, TIOCCBRK, 0);
377 hardwire_raw (struct serial *scb)
379 struct hardwire_ttystate state;
381 if (get_tty_state (scb, &state))
382 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
385 state.termios.c_iflag = 0;
386 state.termios.c_oflag = 0;
387 state.termios.c_lflag = 0;
388 state.termios.c_cflag &= ~(CSIZE | PARENB);
389 state.termios.c_cflag |= CLOCAL | CS8;
390 state.termios.c_cc[VMIN] = 0;
391 state.termios.c_cc[VTIME] = 0;
395 state.termio.c_iflag = 0;
396 state.termio.c_oflag = 0;
397 state.termio.c_lflag = 0;
398 state.termio.c_cflag &= ~(CSIZE | PARENB);
399 state.termio.c_cflag |= CLOCAL | CS8;
400 state.termio.c_cc[VMIN] = 0;
401 state.termio.c_cc[VTIME] = 0;
405 state.sgttyb.sg_flags |= RAW | ANYP;
406 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
409 scb->current_timeout = 0;
411 if (set_tty_state (scb, &state))
412 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
415 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
416 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
418 For termio{s}, we actually just setup VTIME if necessary, and let the
419 timeout occur in the read() in hardwire_read().
422 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
423 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
426 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
427 possible values of the TIMEOUT parameter are ONE and ZERO.
428 Consequently all the code that tries to handle the possability of
429 an overflowed timer is unnecessary. */
432 wait_for (struct serial *scb, int timeout)
441 /* NOTE: Some OS's can scramble the READFDS when the select()
442 call fails (ex the kernel with Red Hat 5.2). Initialize all
443 arguments before each call. */
449 FD_SET (scb->fd, &readfds);
452 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv);
454 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0);
458 return SERIAL_TIMEOUT;
459 else if (errno == EINTR)
462 return SERIAL_ERROR; /* Got an error from select or poll */
466 #endif /* HAVE_SGTTY */
468 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
469 if (timeout == scb->current_timeout)
472 scb->current_timeout = timeout;
475 struct hardwire_ttystate state;
477 if (get_tty_state (scb, &state))
478 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
484 state.termios.c_cc[VTIME] = 0;
485 state.termios.c_cc[VMIN] = 1;
489 state.termios.c_cc[VMIN] = 0;
490 state.termios.c_cc[VTIME] = timeout * 10;
491 if (state.termios.c_cc[VTIME] != timeout * 10)
494 /* If c_cc is an 8-bit signed character, we can't go
495 bigger than this. If it is always unsigned, we could use
498 scb->current_timeout = 12;
499 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
500 scb->timeout_remaining = timeout - scb->current_timeout;
509 state.termio.c_cc[VTIME] = 0;
510 state.termio.c_cc[VMIN] = 1;
514 state.termio.c_cc[VMIN] = 0;
515 state.termio.c_cc[VTIME] = timeout * 10;
516 if (state.termio.c_cc[VTIME] != timeout * 10)
518 /* If c_cc is an 8-bit signed character, we can't go
519 bigger than this. If it is always unsigned, we could use
522 scb->current_timeout = 12;
523 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
524 scb->timeout_remaining = timeout - scb->current_timeout;
529 if (set_tty_state (scb, &state))
530 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
534 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
537 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
538 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
539 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
540 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
542 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
543 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
546 /* NOTE: cagney/1999-09-16: This function is not identical to
547 ser_base_readchar() as part of replacing it with ser_base*()
548 merging will be required - this code handles the case where read()
549 times out due to no data while ser_base_readchar() doesn't expect
553 do_hardwire_readchar (struct serial *scb, int timeout)
561 /* We have to be able to keep the GUI alive here, so we break the
562 original timeout into steps of 1 second, running the "keep the
563 GUI alive" hook each time through the loop.
565 Also, timeout = 0 means to poll, so we just set the delta to 0,
566 so we will only go through the loop once. */
568 delta = (timeout == 0 ? 0 : 1);
572 /* N.B. The UI may destroy our world (for instance by calling
573 remote_stop,) in which case we want to get out of here as
574 quickly as possible. It is not safe to touch scb, since
575 someone else might have freed it. The
576 deprecated_ui_loop_hook signals that we should exit by
579 if (deprecated_ui_loop_hook)
580 detach = deprecated_ui_loop_hook (0);
583 return SERIAL_TIMEOUT;
585 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
586 status = wait_for (scb, delta);
591 status = read (scb->fd, scb->buf, BUFSIZ);
597 /* Zero characters means timeout (it could also be EOF, but
598 we don't (yet at least) distinguish). */
599 if (scb->timeout_remaining > 0)
601 timeout = scb->timeout_remaining;
604 else if (scb->timeout_remaining < 0)
607 return SERIAL_TIMEOUT;
609 else if (errno == EINTR)
612 return SERIAL_ERROR; /* Got an error from read */
615 scb->bufcnt = status;
617 scb->bufp = scb->buf;
623 hardwire_readchar (struct serial *scb, int timeout)
625 return generic_readchar (scb, timeout, do_hardwire_readchar);
637 /* Translate baud rates from integers to damn B_codes. Unix should
638 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
738 rate_to_code (int rate)
742 for (i = 0; baudtab[i].rate != -1; i++)
744 /* test for perfect macth. */
745 if (rate == baudtab[i].rate)
746 return baudtab[i].code;
749 /* check if it is in between valid values. */
750 if (rate < baudtab[i].rate)
754 warning (_("Invalid baud rate %d. Closest values are %d and %d."),
755 rate, baudtab[i - 1].rate, baudtab[i].rate);
759 warning (_("Invalid baud rate %d. Minimum value is %d."),
760 rate, baudtab[0].rate);
767 /* The requested speed was too large. */
768 warning (_("Invalid baud rate %d. Maximum value is %d."),
769 rate, baudtab[i - 1].rate);
774 hardwire_setbaudrate (struct serial *scb, int rate)
776 struct hardwire_ttystate state;
777 int baud_code = rate_to_code (rate);
781 /* The baud rate was not valid.
782 A warning has already been issued. */
787 if (get_tty_state (scb, &state))
791 cfsetospeed (&state.termios, baud_code);
792 cfsetispeed (&state.termios, baud_code);
800 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
801 state.termio.c_cflag |= baud_code;
805 state.sgttyb.sg_ispeed = baud_code;
806 state.sgttyb.sg_ospeed = baud_code;
809 return set_tty_state (scb, &state);
813 hardwire_setstopbits (struct serial *scb, int num)
815 struct hardwire_ttystate state;
818 if (get_tty_state (scb, &state))
823 case SERIAL_1_STOPBITS:
826 case SERIAL_1_AND_A_HALF_STOPBITS:
827 case SERIAL_2_STOPBITS:
836 state.termios.c_cflag &= ~CSTOPB;
838 state.termios.c_cflag |= CSTOPB; /* two bits */
843 state.termio.c_cflag &= ~CSTOPB;
845 state.termio.c_cflag |= CSTOPB; /* two bits */
849 return 0; /* sgtty doesn't support this */
852 return set_tty_state (scb, &state);
856 hardwire_close (struct serial *scb)
867 _initialize_ser_hardwire (void)
869 struct serial_ops *ops = XMALLOC (struct serial_ops);
870 memset (ops, 0, sizeof (struct serial_ops));
871 ops->name = "hardwire";
873 ops->open = hardwire_open;
874 ops->close = hardwire_close;
875 /* FIXME: Don't replace this with the equivalent ser_base*() until
876 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
878 ops->readchar = hardwire_readchar;
879 ops->write = ser_base_write;
880 ops->flush_output = hardwire_flush_output;
881 ops->flush_input = hardwire_flush_input;
882 ops->send_break = hardwire_send_break;
883 ops->go_raw = hardwire_raw;
884 ops->get_tty_state = hardwire_get_tty_state;
885 ops->set_tty_state = hardwire_set_tty_state;
886 ops->print_tty_state = hardwire_print_tty_state;
887 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
888 ops->setbaudrate = hardwire_setbaudrate;
889 ops->setstopbits = hardwire_setstopbits;
890 ops->drain_output = hardwire_drain_output;
891 ops->async = ser_base_async;
892 ops->read_prim = ser_unix_read_prim;
893 ops->write_prim = ser_unix_write_prim;
894 serial_add_interface (ops);
898 ser_unix_read_prim (struct serial *scb, size_t count)
904 status = read (scb->fd, scb->buf, count);
905 if (status != -1 || errno != EINTR)
912 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
914 /* ??? Historically, GDB has not retried calls to "write" that
916 return write (scb->fd, buf, len);