1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993, 1994, 1998 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
32 struct hardwire_ttystate
34 struct termios termios;
40 /* It is believed that all systems which have added job control to SVR3
41 (e.g. sco) have also added termios. Even if not, trying to figure out
42 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
43 bewildering. So we don't attempt it. */
45 struct hardwire_ttystate
52 /* Needed for the code which uses select(). We would include <sys/select.h>
53 too if it existed on all systems. */
56 struct hardwire_ttystate
61 /* Line discipline flags. */
66 static int hardwire_open PARAMS ((serial_t scb, const char *name));
67 static void hardwire_raw PARAMS ((serial_t scb));
68 static int wait_for PARAMS ((serial_t scb, int timeout));
69 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
70 static int rate_to_code PARAMS ((int rate));
71 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
72 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
73 static void hardwire_close PARAMS ((serial_t scb));
74 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate * state));
75 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate * state));
76 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
77 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
78 static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
80 static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate));
81 static int hardwire_drain_output PARAMS ((serial_t));
82 static int hardwire_flush_output PARAMS ((serial_t));
83 static int hardwire_flush_input PARAMS ((serial_t));
84 static int hardwire_send_break PARAMS ((serial_t));
85 static int hardwire_setstopbits PARAMS ((serial_t, int));
87 void _initialize_ser_hardwire PARAMS ((void));
89 extern int (*ui_loop_hook) PARAMS ((int));
91 /* Open up a real live device for serial I/O */
94 hardwire_open (scb, name)
98 scb->fd = open (name, O_RDWR);
106 get_tty_state (scb, state)
108 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 (scb, state)
140 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 (scb)
173 struct hardwire_ttystate *state;
175 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
177 if (get_tty_state (scb, state))
180 return (serial_ttystate) state;
184 hardwire_set_tty_state (scb, ttystate)
186 serial_ttystate ttystate;
188 struct hardwire_ttystate *state;
190 state = (struct hardwire_ttystate *) ttystate;
192 return set_tty_state (scb, state);
196 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
198 serial_ttystate new_ttystate;
199 serial_ttystate old_ttystate;
201 struct hardwire_ttystate new_state;
203 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
206 new_state = *(struct hardwire_ttystate *) new_ttystate;
208 /* Don't change in or out of raw mode; we don't want to flush input.
209 termio and termios have no such restriction; for them flushing input
210 is separate from setting the attributes. */
213 if (state->sgttyb.sg_flags & RAW)
214 new_state.sgttyb.sg_flags |= RAW;
216 new_state.sgttyb.sg_flags &= ~RAW;
218 /* I'm not sure whether this is necessary; the manpage just mentions
220 if (state->sgttyb.sg_flags & CBREAK)
221 new_state.sgttyb.sg_flags |= CBREAK;
223 new_state.sgttyb.sg_flags &= ~CBREAK;
226 return set_tty_state (scb, &new_state);
230 hardwire_print_tty_state (scb, ttystate)
232 serial_ttystate ttystate;
234 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
238 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
239 state->termios.c_iflag, state->termios.c_oflag);
240 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
241 state->termios.c_cflag, state->termios.c_lflag);
243 /* This not in POSIX, and is not really documented by those systems
244 which have it (at least not Sun). */
245 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
247 printf_filtered ("c_cc: ");
248 for (i = 0; i < NCCS; i += 1)
249 printf_filtered ("0x%x ", state->termios.c_cc[i]);
250 printf_filtered ("\n");
254 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
255 state->termio.c_iflag, state->termio.c_oflag);
256 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
257 state->termio.c_cflag, state->termio.c_lflag,
258 state->termio.c_line);
259 printf_filtered ("c_cc: ");
260 for (i = 0; i < NCC; i += 1)
261 printf_filtered ("0x%x ", state->termio.c_cc[i]);
262 printf_filtered ("\n");
266 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
268 printf_filtered ("tchars: ");
269 for (i = 0; i < (int) sizeof (struct tchars); i++)
270 printf_filtered ("0x%x ", ((unsigned char *) &state->tc)[i]);
271 printf_filtered ("\n");
273 printf_filtered ("ltchars: ");
274 for (i = 0; i < (int) sizeof (struct ltchars); i++)
275 printf_filtered ("0x%x ", ((unsigned char *) &state->ltc)[i]);
276 printf_filtered ("\n");
278 printf_filtered ("lmode: 0x%x\n", state->lmode);
282 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
285 hardwire_drain_output (scb)
289 return tcdrain (scb->fd);
293 return ioctl (scb->fd, TCSBRK, 1);
297 /* Get the current state and then restore it using TIOCSETP,
298 which should cause the output to drain and pending input
301 struct hardwire_ttystate state;
302 if (get_tty_state (scb, &state))
308 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
315 hardwire_flush_output (scb)
319 return tcflush (scb->fd, TCOFLUSH);
323 return ioctl (scb->fd, TCFLSH, 1);
327 /* This flushes both input and output, but we can't do better. */
328 return ioctl (scb->fd, TIOCFLUSH, 0);
333 hardwire_flush_input (scb)
337 scb->bufp = scb->buf;
340 return tcflush (scb->fd, TCIFLUSH);
344 return ioctl (scb->fd, TCFLSH, 0);
348 /* This flushes both input and output, but we can't do better. */
349 return ioctl (scb->fd, TIOCFLUSH, 0);
354 hardwire_send_break (scb)
358 return tcsendbreak (scb->fd, 0);
362 return ioctl (scb->fd, TCSBRK, 0);
368 struct timeval timeout;
370 status = ioctl (scb->fd, TIOCSBRK, 0);
372 /* Can't use usleep; it doesn't exist in BSD 4.2. */
373 /* Note that if this select() is interrupted by a signal it will not wait
374 the full length of time. I think that is OK. */
376 timeout.tv_usec = 250000;
377 select (0, 0, 0, 0, &timeout);
378 status = ioctl (scb->fd, TIOCCBRK, 0);
388 struct hardwire_ttystate state;
390 if (get_tty_state (scb, &state))
391 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
394 state.termios.c_iflag = 0;
395 state.termios.c_oflag = 0;
396 state.termios.c_lflag = 0;
397 state.termios.c_cflag &= ~(CSIZE | PARENB);
398 state.termios.c_cflag |= CLOCAL | CS8;
399 state.termios.c_cc[VMIN] = 0;
400 state.termios.c_cc[VTIME] = 0;
404 state.termio.c_iflag = 0;
405 state.termio.c_oflag = 0;
406 state.termio.c_lflag = 0;
407 state.termio.c_cflag &= ~(CSIZE | PARENB);
408 state.termio.c_cflag |= CLOCAL | CS8;
409 state.termio.c_cc[VMIN] = 0;
410 state.termio.c_cc[VTIME] = 0;
414 state.sgttyb.sg_flags |= RAW | ANYP;
415 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
418 scb->current_timeout = 0;
420 if (set_tty_state (scb, &state))
421 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
424 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
425 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
427 For termio{s}, we actually just setup VTIME if necessary, and let the
428 timeout occur in the read() in hardwire_read().
432 wait_for (scb, timeout)
446 FD_SET (scb->fd, &readfds);
453 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
455 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
459 return SERIAL_TIMEOUT;
460 else if (errno == EINTR)
463 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 hardwire_readchar (scb, timeout)
551 if (scb->bufcnt-- > 0)
557 /* We have to be able to keep the GUI alive here, so we break the original
558 timeout into steps of 1 second, running the "keep the GUI alive" hook
559 each time through the loop.
560 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
561 will only go through the loop once. */
563 delta = (timeout == 0 ? 0 : 1);
567 /* N.B. The UI may destroy our world (for instance by calling
568 remote_stop,) in which case we want to get out of here as
569 quickly as possible. It is not safe to touch scb, since
570 someone else might have freed it. The ui_loop_hook signals that
571 we should exit by returning 1. */
574 detach = ui_loop_hook (0);
577 return SERIAL_TIMEOUT;
579 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
580 status = wait_for (scb, delta);
585 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
587 if (scb->bufcnt <= 0)
589 if (scb->bufcnt == 0)
591 /* Zero characters means timeout (it could also be EOF, but
592 we don't (yet at least) distinguish). */
593 if (scb->timeout_remaining > 0)
595 timeout = scb->timeout_remaining;
598 else if (scb->timeout_remaining < 0)
601 return SERIAL_TIMEOUT;
603 else if (errno == EINTR)
606 return SERIAL_ERROR; /* Got an error from read */
610 scb->bufp = scb->buf;
623 /* Translate baud rates from integers to damn B_codes. Unix should
624 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
729 for (i = 0; baudtab[i].rate != -1; i++)
730 if (rate == baudtab[i].rate)
731 return baudtab[i].code;
737 hardwire_setbaudrate (scb, rate)
741 struct hardwire_ttystate state;
743 if (get_tty_state (scb, &state))
747 cfsetospeed (&state.termios, rate_to_code (rate));
748 cfsetispeed (&state.termios, rate_to_code (rate));
756 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
757 state.termio.c_cflag |= rate_to_code (rate);
761 state.sgttyb.sg_ispeed = rate_to_code (rate);
762 state.sgttyb.sg_ospeed = rate_to_code (rate);
765 return set_tty_state (scb, &state);
769 hardwire_setstopbits (scb, num)
773 struct hardwire_ttystate state;
776 if (get_tty_state (scb, &state))
781 case SERIAL_1_STOPBITS:
784 case SERIAL_1_AND_A_HALF_STOPBITS:
785 case SERIAL_2_STOPBITS:
794 state.termios.c_cflag &= ~CSTOPB;
796 state.termios.c_cflag |= CSTOPB; /* two bits */
801 state.termio.c_cflag &= ~CSTOPB;
803 state.termio.c_cflag |= CSTOPB; /* two bits */
807 return 0; /* sgtty doesn't support this */
810 return set_tty_state (scb, &state);
814 hardwire_write (scb, str, len)
823 cc = write (scb->fd, str, len);
844 static struct serial_ops hardwire_ops =
852 hardwire_flush_output,
853 hardwire_flush_input,
856 hardwire_get_tty_state,
857 hardwire_set_tty_state,
858 hardwire_print_tty_state,
859 hardwire_noflush_set_tty_state,
860 hardwire_setbaudrate,
861 hardwire_setstopbits,
862 hardwire_drain_output, /* wait for output to drain */
866 _initialize_ser_hardwire ()
868 serial_add_interface (&hardwire_ops);