1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright 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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
29 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include "gdb_string.h"
38 struct hardwire_ttystate
40 struct termios termios;
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. */
51 struct hardwire_ttystate
58 struct hardwire_ttystate
63 /* Line discipline flags. */
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,
76 static int rate_to_code (int rate);
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,
87 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
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);
95 void _initialize_ser_hardwire (void);
97 /* Open up a real live device for serial I/O */
100 hardwire_open (struct serial *scb, const char *name)
102 scb->fd = open (name, O_RDWR);
110 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
113 if (tcgetattr (scb->fd, &state->termios) < 0)
120 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
126 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
128 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
130 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
132 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
140 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
143 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
150 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
156 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
158 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
160 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
162 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
169 static serial_ttystate
170 hardwire_get_tty_state (struct serial *scb)
172 struct hardwire_ttystate *state;
174 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
176 if (get_tty_state (scb, state))
179 return (serial_ttystate) state;
183 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
185 struct hardwire_ttystate *state;
187 state = (struct hardwire_ttystate *) ttystate;
189 return set_tty_state (scb, state);
193 hardwire_noflush_set_tty_state (struct serial *scb,
194 serial_ttystate new_ttystate,
195 serial_ttystate old_ttystate)
197 struct hardwire_ttystate new_state;
199 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
202 new_state = *(struct hardwire_ttystate *) new_ttystate;
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. */
209 if (state->sgttyb.sg_flags & RAW)
210 new_state.sgttyb.sg_flags |= RAW;
212 new_state.sgttyb.sg_flags &= ~RAW;
214 /* I'm not sure whether this is necessary; the manpage just mentions
216 if (state->sgttyb.sg_flags & CBREAK)
217 new_state.sgttyb.sg_flags |= CBREAK;
219 new_state.sgttyb.sg_flags &= ~CBREAK;
222 return set_tty_state (scb, &new_state);
226 hardwire_print_tty_state (struct serial *scb,
227 serial_ttystate ttystate,
228 struct ui_file *stream)
230 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
234 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
235 (int) state->termios.c_iflag,
236 (int) state->termios.c_oflag);
237 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
238 (int) state->termios.c_cflag,
239 (int) state->termios.c_lflag);
241 /* This not in POSIX, and is not really documented by those systems
242 which have it (at least not Sun). */
243 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
245 fprintf_filtered (stream, "c_cc: ");
246 for (i = 0; i < NCCS; i += 1)
247 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
248 fprintf_filtered (stream, "\n");
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: ");
258 for (i = 0; i < NCC; i += 1)
259 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
260 fprintf_filtered (stream, "\n");
264 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
265 state->sgttyb.sg_flags);
267 fprintf_filtered (stream, "tchars: ");
268 for (i = 0; i < (int) sizeof (struct tchars); i++)
269 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
270 fprintf_filtered (stream, "\n");
272 fprintf_filtered (stream, "ltchars: ");
273 for (i = 0; i < (int) sizeof (struct ltchars); i++)
274 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
275 fprintf_filtered (stream, "\n");
277 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
281 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
284 hardwire_drain_output (struct serial *scb)
287 return tcdrain (scb->fd);
291 return ioctl (scb->fd, TCSBRK, 1);
295 /* Get the current state and then restore it using TIOCSETP,
296 which should cause the output to drain and pending input
299 struct hardwire_ttystate state;
300 if (get_tty_state (scb, &state))
306 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
313 hardwire_flush_output (struct serial *scb)
316 return tcflush (scb->fd, TCOFLUSH);
320 return ioctl (scb->fd, TCFLSH, 1);
324 /* This flushes both input and output, but we can't do better. */
325 return ioctl (scb->fd, TIOCFLUSH, 0);
330 hardwire_flush_input (struct serial *scb)
332 ser_base_flush_input (scb);
335 return tcflush (scb->fd, TCIFLUSH);
339 return ioctl (scb->fd, TCFLSH, 0);
343 /* This flushes both input and output, but we can't do better. */
344 return ioctl (scb->fd, TIOCFLUSH, 0);
349 hardwire_send_break (struct serial *scb)
352 return tcsendbreak (scb->fd, 0);
356 return ioctl (scb->fd, TCSBRK, 0);
362 struct timeval timeout;
364 status = ioctl (scb->fd, TIOCSBRK, 0);
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. */
370 timeout.tv_usec = 250000;
371 select (0, 0, 0, 0, &timeout);
372 status = ioctl (scb->fd, TIOCCBRK, 0);
379 hardwire_raw (struct serial *scb)
381 struct hardwire_ttystate state;
383 if (get_tty_state (scb, &state))
384 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
387 state.termios.c_iflag = 0;
388 state.termios.c_oflag = 0;
389 state.termios.c_lflag = 0;
390 state.termios.c_cflag &= ~(CSIZE | PARENB);
391 state.termios.c_cflag |= CLOCAL | CS8;
392 state.termios.c_cc[VMIN] = 0;
393 state.termios.c_cc[VTIME] = 0;
397 state.termio.c_iflag = 0;
398 state.termio.c_oflag = 0;
399 state.termio.c_lflag = 0;
400 state.termio.c_cflag &= ~(CSIZE | PARENB);
401 state.termio.c_cflag |= CLOCAL | CS8;
402 state.termio.c_cc[VMIN] = 0;
403 state.termio.c_cc[VTIME] = 0;
407 state.sgttyb.sg_flags |= RAW | ANYP;
408 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
411 scb->current_timeout = 0;
413 if (set_tty_state (scb, &state))
414 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
417 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
418 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
420 For termio{s}, we actually just setup VTIME if necessary, and let the
421 timeout occur in the read() in hardwire_read().
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
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. */
434 wait_for (struct serial *scb, int timeout)
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. */
451 FD_SET (scb->fd, &readfds);
454 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
456 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
460 return SERIAL_TIMEOUT;
461 else if (errno == EINTR)
464 return SERIAL_ERROR; /* Got an error from select or poll */
468 #endif /* HAVE_SGTTY */
470 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
471 if (timeout == scb->current_timeout)
474 scb->current_timeout = timeout;
477 struct hardwire_ttystate state;
479 if (get_tty_state (scb, &state))
480 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
486 state.termios.c_cc[VTIME] = 0;
487 state.termios.c_cc[VMIN] = 1;
491 state.termios.c_cc[VMIN] = 0;
492 state.termios.c_cc[VTIME] = timeout * 10;
493 if (state.termios.c_cc[VTIME] != timeout * 10)
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
500 scb->current_timeout = 12;
501 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
502 scb->timeout_remaining = timeout - scb->current_timeout;
511 state.termio.c_cc[VTIME] = 0;
512 state.termio.c_cc[VMIN] = 1;
516 state.termio.c_cc[VMIN] = 0;
517 state.termio.c_cc[VTIME] = timeout * 10;
518 if (state.termio.c_cc[VTIME] != timeout * 10)
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
524 scb->current_timeout = 12;
525 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
526 scb->timeout_remaining = timeout - scb->current_timeout;
531 if (set_tty_state (scb, &state))
532 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
536 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
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). */
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
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
555 do_hardwire_readchar (struct serial *scb, int timeout)
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.
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. */
570 delta = (timeout == 0 ? 0 : 1);
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
577 someone else might have freed it. The
578 deprecated_ui_loop_hook signals that we should exit by
581 if (deprecated_ui_loop_hook)
582 detach = deprecated_ui_loop_hook (0);
585 return SERIAL_TIMEOUT;
587 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
588 status = wait_for (scb, delta);
593 status = read (scb->fd, scb->buf, BUFSIZ);
599 /* Zero characters means timeout (it could also be EOF, but
600 we don't (yet at least) distinguish). */
601 if (scb->timeout_remaining > 0)
603 timeout = scb->timeout_remaining;
606 else if (scb->timeout_remaining < 0)
609 return SERIAL_TIMEOUT;
611 else if (errno == EINTR)
614 return SERIAL_ERROR; /* Got an error from read */
617 scb->bufcnt = status;
619 scb->bufp = scb->buf;
625 hardwire_readchar (struct serial *scb, int timeout)
627 return generic_readchar (scb, timeout, do_hardwire_readchar);
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. */
740 rate_to_code (int rate)
744 for (i = 0; baudtab[i].rate != -1; i++)
746 /* test for perfect macth. */
747 if (rate == baudtab[i].rate)
748 return baudtab[i].code;
751 /* check if it is in between valid values. */
752 if (rate < baudtab[i].rate)
756 warning (_("Invalid baud rate %d. Closest values are %d and %d."),
757 rate, baudtab[i - 1].rate, baudtab[i].rate);
761 warning (_("Invalid baud rate %d. Minimum value is %d."),
762 rate, baudtab[0].rate);
769 /* The requested speed was too large. */
770 warning (_("Invalid baud rate %d. Maximum value is %d."),
771 rate, baudtab[i - 1].rate);
776 hardwire_setbaudrate (struct serial *scb, int rate)
778 struct hardwire_ttystate state;
779 int baud_code = rate_to_code (rate);
783 /* The baud rate was not valid.
784 A warning has already been issued. */
789 if (get_tty_state (scb, &state))
793 cfsetospeed (&state.termios, baud_code);
794 cfsetispeed (&state.termios, baud_code);
802 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
803 state.termio.c_cflag |= baud_code;
807 state.sgttyb.sg_ispeed = baud_code;
808 state.sgttyb.sg_ospeed = baud_code;
811 return set_tty_state (scb, &state);
815 hardwire_setstopbits (struct serial *scb, int num)
817 struct hardwire_ttystate state;
820 if (get_tty_state (scb, &state))
825 case SERIAL_1_STOPBITS:
828 case SERIAL_1_AND_A_HALF_STOPBITS:
829 case SERIAL_2_STOPBITS:
838 state.termios.c_cflag &= ~CSTOPB;
840 state.termios.c_cflag |= CSTOPB; /* two bits */
845 state.termio.c_cflag &= ~CSTOPB;
847 state.termio.c_cflag |= CSTOPB; /* two bits */
851 return 0; /* sgtty doesn't support this */
854 return set_tty_state (scb, &state);
858 hardwire_close (struct serial *scb)
868 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
869 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
872 ser_unix_wait_for (struct serial *scb, int timeout)
878 fd_set readfds, exceptfds;
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. */
888 FD_ZERO (&exceptfds);
889 FD_SET (scb->fd, &readfds);
890 FD_SET (scb->fd, &exceptfds);
893 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
895 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
900 return SERIAL_TIMEOUT;
901 else if (errno == EINTR)
904 return SERIAL_ERROR; /* Got an error from select or poll */
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). */
917 do_unix_readchar (struct serial *scb, int timeout)
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.
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. */
929 delta = (timeout == 0 ? 0 : 1);
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
936 someone else might have freed it. The
937 deprecated_ui_loop_hook signals that we should exit by
940 if (deprecated_ui_loop_hook)
942 if (deprecated_ui_loop_hook (0))
943 return SERIAL_TIMEOUT;
946 status = ser_unix_wait_for (scb, delta);
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. */
953 if (status != SERIAL_TIMEOUT)
958 /* If we have exhausted the original timeout, then generate
959 a SERIAL_TIMEOUT, and pass it out of the loop. */
961 else if (timeout == 0)
963 status = SERIAL_TIMEOUT;
973 status = read (scb->fd, scb->buf, BUFSIZ);
974 if (status != -1 || errno != EINTR)
981 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
982 distinguish between EOF & timeouts
985 return SERIAL_ERROR; /* Got an error from read */
988 scb->bufcnt = status;
990 scb->bufp = scb->buf;
994 /* Perform operations common to both old and new readchar. */
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
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())
1009 generic_readchar (struct serial *scb, int timeout,
1010 int (do_readchar) (struct serial *scb, int timeout))
1013 if (scb->bufcnt > 0)
1019 else if (scb->bufcnt < 0)
1021 /* Some errors/eof are are sticky. */
1026 ch = do_readchar (scb, timeout);
1029 switch ((enum serial_rc) ch)
1033 /* Make the error/eof stick. */
1036 case SERIAL_TIMEOUT:
1047 ser_unix_readchar (struct serial *scb, int timeout)
1049 return generic_readchar (scb, timeout, do_unix_readchar);
1053 _initialize_ser_hardwire (void)
1055 struct serial_ops *ops = XMALLOC (struct serial_ops);
1056 memset (ops, 0, sizeof (struct serial_ops));
1057 ops->name = "hardwire";
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
1064 ops->readchar = hardwire_readchar;
1065 ops->write = ser_base_write;
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;
1077 ops->async = ser_base_async;
1078 serial_add_interface (ops);