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, Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
31 struct hardwire_ttystate
33 struct termios termios;
39 /* It is believed that all systems which have added job control to SVR3
40 (e.g. sco) have also added termios. Even if not, trying to figure out
41 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
42 bewildering. So we don't attempt it. */
44 struct hardwire_ttystate
51 /* Needed for the code which uses select(). We would include <sys/select.h>
52 too if it existed on all systems. */
55 struct hardwire_ttystate
60 /* Line discipline flags. */
65 static int hardwire_open PARAMS ((serial_t scb, const char *name));
66 static void hardwire_raw PARAMS ((serial_t scb));
67 static int wait_for PARAMS ((serial_t scb, int timeout));
68 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
69 static int rate_to_code PARAMS ((int rate));
70 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
71 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
72 static void hardwire_close PARAMS ((serial_t scb));
73 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
74 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
75 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
76 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
77 static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
79 static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate));
80 static int hardwire_drain_output PARAMS ((serial_t));
81 static int hardwire_flush_output PARAMS ((serial_t));
82 static int hardwire_flush_input PARAMS ((serial_t));
83 static int hardwire_send_break PARAMS ((serial_t));
84 static int hardwire_setstopbits PARAMS ((serial_t, int));
86 void _initialize_ser_hardwire PARAMS ((void));
88 extern int (*ui_loop_hook) PARAMS ((int));
90 /* Open up a real live device for serial I/O */
93 hardwire_open(scb, name)
97 scb->fd = open (name, O_RDWR);
105 get_tty_state (scb, state)
107 struct hardwire_ttystate *state;
110 if (tcgetattr(scb->fd, &state->termios) < 0)
117 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
123 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
125 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
127 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
129 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
137 set_tty_state(scb, state)
139 struct hardwire_ttystate *state;
142 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
149 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
155 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
157 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
159 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
161 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
168 static serial_ttystate
169 hardwire_get_tty_state(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(scb, ttystate)
185 serial_ttystate ttystate;
187 struct hardwire_ttystate *state;
189 state = (struct hardwire_ttystate *)ttystate;
191 return set_tty_state(scb, state);
195 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
197 serial_ttystate new_ttystate;
198 serial_ttystate old_ttystate;
200 struct hardwire_ttystate new_state;
202 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
205 new_state = *(struct hardwire_ttystate *)new_ttystate;
207 /* Don't change in or out of raw mode; we don't want to flush input.
208 termio and termios have no such restriction; for them flushing input
209 is separate from setting the attributes. */
212 if (state->sgttyb.sg_flags & RAW)
213 new_state.sgttyb.sg_flags |= RAW;
215 new_state.sgttyb.sg_flags &= ~RAW;
217 /* I'm not sure whether this is necessary; the manpage just mentions
219 if (state->sgttyb.sg_flags & CBREAK)
220 new_state.sgttyb.sg_flags |= CBREAK;
222 new_state.sgttyb.sg_flags &= ~CBREAK;
225 return set_tty_state (scb, &new_state);
229 hardwire_print_tty_state (scb, ttystate)
231 serial_ttystate ttystate;
233 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
237 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
238 state->termios.c_iflag, state->termios.c_oflag);
239 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
240 state->termios.c_cflag, state->termios.c_lflag);
242 /* This not in POSIX, and is not really documented by those systems
243 which have it (at least not Sun). */
244 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
246 printf_filtered ("c_cc: ");
247 for (i = 0; i < NCCS; i += 1)
248 printf_filtered ("0x%x ", state->termios.c_cc[i]);
249 printf_filtered ("\n");
253 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
254 state->termio.c_iflag, state->termio.c_oflag);
255 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
256 state->termio.c_cflag, state->termio.c_lflag,
257 state->termio.c_line);
258 printf_filtered ("c_cc: ");
259 for (i = 0; i < NCC; i += 1)
260 printf_filtered ("0x%x ", state->termio.c_cc[i]);
261 printf_filtered ("\n");
265 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
267 printf_filtered ("tchars: ");
268 for (i = 0; i < (int)sizeof (struct tchars); i++)
269 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
270 printf_filtered ("\n");
272 printf_filtered ("ltchars: ");
273 for (i = 0; i < (int)sizeof (struct ltchars); i++)
274 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
275 printf_filtered ("\n");
277 printf_filtered ("lmode: 0x%x\n", state->lmode);
281 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
284 hardwire_drain_output (scb)
288 return tcdrain (scb->fd);
292 return ioctl (scb->fd, TCSBRK, 1);
296 /* Get the current state and then restore it using TIOCSETP,
297 which should cause the output to drain and pending input
300 struct hardwire_ttystate state;
301 if (get_tty_state (scb, &state))
307 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
314 hardwire_flush_output (scb)
318 return tcflush (scb->fd, TCOFLUSH);
322 return ioctl (scb->fd, TCFLSH, 1);
326 /* This flushes both input and output, but we can't do better. */
327 return ioctl (scb->fd, TIOCFLUSH, 0);
332 hardwire_flush_input (scb)
336 scb->bufp = scb->buf;
339 return tcflush (scb->fd, TCIFLUSH);
343 return ioctl (scb->fd, TCFLSH, 0);
347 /* This flushes both input and output, but we can't do better. */
348 return ioctl (scb->fd, TIOCFLUSH, 0);
353 hardwire_send_break (scb)
357 return tcsendbreak (scb->fd, 0);
361 return ioctl (scb->fd, TCSBRK, 0);
367 struct timeval timeout;
369 status = ioctl (scb->fd, TIOCSBRK, 0);
371 /* Can't use usleep; it doesn't exist in BSD 4.2. */
372 /* Note that if this select() is interrupted by a signal it will not wait
373 the full length of time. I think that is OK. */
375 timeout.tv_usec = 250000;
376 select (0, 0, 0, 0, &timeout);
377 status = ioctl (scb->fd, TIOCCBRK, 0);
387 struct hardwire_ttystate state;
389 if (get_tty_state(scb, &state))
390 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
393 state.termios.c_iflag = 0;
394 state.termios.c_oflag = 0;
395 state.termios.c_lflag = 0;
396 state.termios.c_cflag &= ~(CSIZE|PARENB);
397 state.termios.c_cflag |= CLOCAL | CS8;
398 state.termios.c_cc[VMIN] = 0;
399 state.termios.c_cc[VTIME] = 0;
403 state.termio.c_iflag = 0;
404 state.termio.c_oflag = 0;
405 state.termio.c_lflag = 0;
406 state.termio.c_cflag &= ~(CSIZE|PARENB);
407 state.termio.c_cflag |= CLOCAL | CS8;
408 state.termio.c_cc[VMIN] = 0;
409 state.termio.c_cc[VTIME] = 0;
413 state.sgttyb.sg_flags |= RAW | ANYP;
414 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
417 scb->current_timeout = 0;
419 if (set_tty_state (scb, &state))
420 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
423 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
424 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
426 For termio{s}, we actually just setup VTIME if necessary, and let the
427 timeout occur in the read() in hardwire_read().
431 wait_for(scb, timeout)
445 FD_SET(scb->fd, &readfds);
452 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
454 numfds = 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 */
467 #endif /* HAVE_SGTTY */
469 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
470 if (timeout == scb->current_timeout)
473 scb->current_timeout = timeout;
476 struct hardwire_ttystate state;
478 if (get_tty_state(scb, &state))
479 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
485 state.termios.c_cc[VTIME] = 0;
486 state.termios.c_cc[VMIN] = 1;
490 state.termios.c_cc[VMIN] = 0;
491 state.termios.c_cc[VTIME] = timeout * 10;
492 if (state.termios.c_cc[VTIME] != timeout * 10)
495 /* If c_cc is an 8-bit signed character, we can't go
496 bigger than this. If it is always unsigned, we could use
499 scb->current_timeout = 12;
500 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
501 scb->timeout_remaining = timeout - scb->current_timeout;
510 state.termio.c_cc[VTIME] = 0;
511 state.termio.c_cc[VMIN] = 1;
515 state.termio.c_cc[VMIN] = 0;
516 state.termio.c_cc[VTIME] = timeout * 10;
517 if (state.termio.c_cc[VTIME] != timeout * 10)
519 /* If c_cc is an 8-bit signed character, we can't go
520 bigger than this. If it is always unsigned, we could use
523 scb->current_timeout = 12;
524 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
525 scb->timeout_remaining = timeout - scb->current_timeout;
530 if (set_tty_state (scb, &state))
531 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
535 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
538 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
539 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
540 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
541 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
543 hardwire_readchar (scb, timeout)
550 if (scb->bufcnt-- > 0)
556 /* We have to be able to keep the GUI alive here, so we break the original
557 timeout into steps of 1 second, running the "keep the GUI alive" hook
558 each time through the loop.
559 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
560 will only go through the loop once. */
562 delta = (timeout == 0 ? 0 : 1);
566 /* N.B. The UI may destroy our world (for instance by calling
567 remote_stop,) in which case we want to get out of here as
568 quickly as possible. It is not safe to touch scb, since
569 someone else might have freed it. The ui_loop_hook signals that
570 we should exit by returning 1. */
573 detach = ui_loop_hook (0);
576 return SERIAL_TIMEOUT;
578 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
579 status = wait_for (scb, delta);
584 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
586 if (scb->bufcnt <= 0)
588 if (scb->bufcnt == 0)
590 /* Zero characters means timeout (it could also be EOF, but
591 we don't (yet at least) distinguish). */
592 if (scb->timeout_remaining > 0)
594 timeout = scb->timeout_remaining;
597 else if (scb->timeout_remaining < 0)
600 return SERIAL_TIMEOUT;
602 else if (errno == EINTR)
605 return SERIAL_ERROR; /* Got an error from read */
609 scb->bufp = scb->buf;
622 /* Translate baud rates from integers to damn B_codes. Unix should
623 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
668 for (i = 0; baudtab[i].rate != -1; i++)
669 if (rate == baudtab[i].rate)
670 return baudtab[i].code;
676 hardwire_setbaudrate(scb, rate)
680 struct hardwire_ttystate state;
682 if (get_tty_state(scb, &state))
686 cfsetospeed (&state.termios, rate_to_code (rate));
687 cfsetispeed (&state.termios, rate_to_code (rate));
695 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
696 state.termio.c_cflag |= rate_to_code (rate);
700 state.sgttyb.sg_ispeed = rate_to_code (rate);
701 state.sgttyb.sg_ospeed = rate_to_code (rate);
704 return set_tty_state (scb, &state);
708 hardwire_setstopbits(scb, num)
712 struct hardwire_ttystate state;
715 if (get_tty_state(scb, &state))
720 case SERIAL_1_STOPBITS:
723 case SERIAL_1_AND_A_HALF_STOPBITS:
724 case SERIAL_2_STOPBITS:
733 state.termios.c_cflag &= ~CSTOPB;
735 state.termios.c_cflag |= CSTOPB; /* two bits */
740 state.termio.c_cflag &= ~CSTOPB;
742 state.termio.c_cflag |= CSTOPB; /* two bits */
746 return 0; /* sgtty doesn't support this */
749 return set_tty_state (scb, &state);
753 hardwire_write(scb, str, len)
762 cc = write(scb->fd, str, len);
783 static struct serial_ops hardwire_ops =
791 hardwire_flush_output,
792 hardwire_flush_input,
795 hardwire_get_tty_state,
796 hardwire_set_tty_state,
797 hardwire_print_tty_state,
798 hardwire_noflush_set_tty_state,
799 hardwire_setbaudrate,
800 hardwire_setstopbits,
801 hardwire_drain_output, /* wait for output to drain */
805 _initialize_ser_hardwire ()
807 serial_add_interface (&hardwire_ops);