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>
29 struct hardwire_ttystate
31 struct termios termios;
37 /* It is believed that all systems which have added job control to SVR3
38 (e.g. sco) have also added termios. Even if not, trying to figure out
39 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
40 bewildering. So we don't attempt it. */
42 struct hardwire_ttystate
49 /* Needed for the code which uses select(). We would include <sys/select.h>
50 too if it existed on all systems. */
53 struct hardwire_ttystate
58 /* Line discipline flags. */
63 static int hardwire_open PARAMS ((serial_t scb, const char *name));
64 static void hardwire_raw PARAMS ((serial_t scb));
65 static int wait_for PARAMS ((serial_t scb, int timeout));
66 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
67 static int rate_to_code PARAMS ((int rate));
68 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
69 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
70 static void hardwire_close PARAMS ((serial_t scb));
71 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate * state));
72 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate * state));
73 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
74 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
75 static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
77 static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate));
78 static int hardwire_drain_output PARAMS ((serial_t));
79 static int hardwire_flush_output PARAMS ((serial_t));
80 static int hardwire_flush_input PARAMS ((serial_t));
81 static int hardwire_send_break PARAMS ((serial_t));
82 static int hardwire_setstopbits PARAMS ((serial_t, int));
84 void _initialize_ser_hardwire PARAMS ((void));
86 extern int (*ui_loop_hook) PARAMS ((int));
88 /* Open up a real live device for serial I/O */
91 hardwire_open (scb, name)
95 scb->fd = open (name, O_RDWR);
103 get_tty_state (scb, state)
105 struct hardwire_ttystate *state;
108 if (tcgetattr (scb->fd, &state->termios) < 0)
115 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
121 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
123 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
125 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
127 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
135 set_tty_state (scb, state)
137 struct hardwire_ttystate *state;
140 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
147 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
153 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
155 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
157 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
159 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
166 static serial_ttystate
167 hardwire_get_tty_state (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 (scb, ttystate)
183 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 (scb, new_ttystate, old_ttystate)
195 serial_ttystate new_ttystate;
196 serial_ttystate old_ttystate;
198 struct hardwire_ttystate new_state;
200 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
203 new_state = *(struct hardwire_ttystate *) new_ttystate;
205 /* Don't change in or out of raw mode; we don't want to flush input.
206 termio and termios have no such restriction; for them flushing input
207 is separate from setting the attributes. */
210 if (state->sgttyb.sg_flags & RAW)
211 new_state.sgttyb.sg_flags |= RAW;
213 new_state.sgttyb.sg_flags &= ~RAW;
215 /* I'm not sure whether this is necessary; the manpage just mentions
217 if (state->sgttyb.sg_flags & CBREAK)
218 new_state.sgttyb.sg_flags |= CBREAK;
220 new_state.sgttyb.sg_flags &= ~CBREAK;
223 return set_tty_state (scb, &new_state);
227 hardwire_print_tty_state (scb, ttystate)
229 serial_ttystate ttystate;
231 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
235 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
236 state->termios.c_iflag, state->termios.c_oflag);
237 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
238 state->termios.c_cflag, state->termios.c_lflag);
240 /* This not in POSIX, and is not really documented by those systems
241 which have it (at least not Sun). */
242 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
244 printf_filtered ("c_cc: ");
245 for (i = 0; i < NCCS; i += 1)
246 printf_filtered ("0x%x ", state->termios.c_cc[i]);
247 printf_filtered ("\n");
251 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
252 state->termio.c_iflag, state->termio.c_oflag);
253 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
254 state->termio.c_cflag, state->termio.c_lflag,
255 state->termio.c_line);
256 printf_filtered ("c_cc: ");
257 for (i = 0; i < NCC; i += 1)
258 printf_filtered ("0x%x ", state->termio.c_cc[i]);
259 printf_filtered ("\n");
263 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
265 printf_filtered ("tchars: ");
266 for (i = 0; i < (int) sizeof (struct tchars); i++)
267 printf_filtered ("0x%x ", ((unsigned char *) &state->tc)[i]);
268 printf_filtered ("\n");
270 printf_filtered ("ltchars: ");
271 for (i = 0; i < (int) sizeof (struct ltchars); i++)
272 printf_filtered ("0x%x ", ((unsigned char *) &state->ltc)[i]);
273 printf_filtered ("\n");
275 printf_filtered ("lmode: 0x%x\n", state->lmode);
279 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
282 hardwire_drain_output (scb)
286 return tcdrain (scb->fd);
290 return ioctl (scb->fd, TCSBRK, 1);
294 /* Get the current state and then restore it using TIOCSETP,
295 which should cause the output to drain and pending input
298 struct hardwire_ttystate state;
299 if (get_tty_state (scb, &state))
305 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
312 hardwire_flush_output (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 (scb)
334 scb->bufp = scb->buf;
337 return tcflush (scb->fd, TCIFLUSH);
341 return ioctl (scb->fd, TCFLSH, 0);
345 /* This flushes both input and output, but we can't do better. */
346 return ioctl (scb->fd, TIOCFLUSH, 0);
351 hardwire_send_break (scb)
355 return tcsendbreak (scb->fd, 0);
359 return ioctl (scb->fd, TCSBRK, 0);
365 struct timeval timeout;
367 status = ioctl (scb->fd, TIOCSBRK, 0);
369 /* Can't use usleep; it doesn't exist in BSD 4.2. */
370 /* Note that if this select() is interrupted by a signal it will not wait
371 the full length of time. I think that is OK. */
373 timeout.tv_usec = 250000;
374 select (0, 0, 0, 0, &timeout);
375 status = ioctl (scb->fd, TIOCCBRK, 0);
385 struct hardwire_ttystate state;
387 if (get_tty_state (scb, &state))
388 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
391 state.termios.c_iflag = 0;
392 state.termios.c_oflag = 0;
393 state.termios.c_lflag = 0;
394 state.termios.c_cflag &= ~(CSIZE | PARENB);
395 state.termios.c_cflag |= CLOCAL | CS8;
396 state.termios.c_cc[VMIN] = 0;
397 state.termios.c_cc[VTIME] = 0;
401 state.termio.c_iflag = 0;
402 state.termio.c_oflag = 0;
403 state.termio.c_lflag = 0;
404 state.termio.c_cflag &= ~(CSIZE | PARENB);
405 state.termio.c_cflag |= CLOCAL | CS8;
406 state.termio.c_cc[VMIN] = 0;
407 state.termio.c_cc[VTIME] = 0;
411 state.sgttyb.sg_flags |= RAW | ANYP;
412 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
415 scb->current_timeout = 0;
417 if (set_tty_state (scb, &state))
418 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
421 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
422 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
424 For termio{s}, we actually just setup VTIME if necessary, and let the
425 timeout occur in the read() in hardwire_read().
429 wait_for (scb, timeout)
443 FD_SET (scb->fd, &readfds);
450 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
452 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
456 return SERIAL_TIMEOUT;
457 else if (errno == EINTR)
460 return SERIAL_ERROR; /* Got an error from select or poll */
465 #endif /* HAVE_SGTTY */
467 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
468 if (timeout == scb->current_timeout)
471 scb->current_timeout = timeout;
474 struct hardwire_ttystate state;
476 if (get_tty_state (scb, &state))
477 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
483 state.termios.c_cc[VTIME] = 0;
484 state.termios.c_cc[VMIN] = 1;
488 state.termios.c_cc[VMIN] = 0;
489 state.termios.c_cc[VTIME] = timeout * 10;
490 if (state.termios.c_cc[VTIME] != timeout * 10)
493 /* If c_cc is an 8-bit signed character, we can't go
494 bigger than this. If it is always unsigned, we could use
497 scb->current_timeout = 12;
498 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
499 scb->timeout_remaining = timeout - scb->current_timeout;
508 state.termio.c_cc[VTIME] = 0;
509 state.termio.c_cc[VMIN] = 1;
513 state.termio.c_cc[VMIN] = 0;
514 state.termio.c_cc[VTIME] = timeout * 10;
515 if (state.termio.c_cc[VTIME] != timeout * 10)
517 /* If c_cc is an 8-bit signed character, we can't go
518 bigger than this. If it is always unsigned, we could use
521 scb->current_timeout = 12;
522 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
523 scb->timeout_remaining = timeout - scb->current_timeout;
528 if (set_tty_state (scb, &state))
529 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
533 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
536 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
537 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
538 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
539 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
541 hardwire_readchar (scb, timeout)
548 if (scb->bufcnt-- > 0)
554 /* We have to be able to keep the GUI alive here, so we break the original
555 timeout into steps of 1 second, running the "keep the GUI alive" hook
556 each time through the loop.
557 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
558 will only go through the loop once. */
560 delta = (timeout == 0 ? 0 : 1);
564 /* N.B. The UI may destroy our world (for instance by calling
565 remote_stop,) in which case we want to get out of here as
566 quickly as possible. It is not safe to touch scb, since
567 someone else might have freed it. The ui_loop_hook signals that
568 we should exit by returning 1. */
571 detach = ui_loop_hook (0);
574 return SERIAL_TIMEOUT;
576 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
577 status = wait_for (scb, delta);
582 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
584 if (scb->bufcnt <= 0)
586 if (scb->bufcnt == 0)
588 /* Zero characters means timeout (it could also be EOF, but
589 we don't (yet at least) distinguish). */
590 if (scb->timeout_remaining > 0)
592 timeout = scb->timeout_remaining;
595 else if (scb->timeout_remaining < 0)
598 return SERIAL_TIMEOUT;
600 else if (errno == EINTR)
603 return SERIAL_ERROR; /* Got an error from read */
607 scb->bufp = scb->buf;
620 /* Translate baud rates from integers to damn B_codes. Unix should
621 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
726 for (i = 0; baudtab[i].rate != -1; i++)
727 if (rate == baudtab[i].rate)
728 return baudtab[i].code;
734 hardwire_setbaudrate (scb, rate)
738 struct hardwire_ttystate state;
740 if (get_tty_state (scb, &state))
744 cfsetospeed (&state.termios, rate_to_code (rate));
745 cfsetispeed (&state.termios, rate_to_code (rate));
753 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
754 state.termio.c_cflag |= rate_to_code (rate);
758 state.sgttyb.sg_ispeed = rate_to_code (rate);
759 state.sgttyb.sg_ospeed = rate_to_code (rate);
762 return set_tty_state (scb, &state);
766 hardwire_setstopbits (scb, num)
770 struct hardwire_ttystate state;
773 if (get_tty_state (scb, &state))
778 case SERIAL_1_STOPBITS:
781 case SERIAL_1_AND_A_HALF_STOPBITS:
782 case SERIAL_2_STOPBITS:
791 state.termios.c_cflag &= ~CSTOPB;
793 state.termios.c_cflag |= CSTOPB; /* two bits */
798 state.termio.c_cflag &= ~CSTOPB;
800 state.termio.c_cflag |= CSTOPB; /* two bits */
804 return 0; /* sgtty doesn't support this */
807 return set_tty_state (scb, &state);
811 hardwire_write (scb, str, len)
820 cc = write (scb->fd, str, len);
841 static struct serial_ops hardwire_ops =
849 hardwire_flush_output,
850 hardwire_flush_input,
853 hardwire_get_tty_state,
854 hardwire_set_tty_state,
855 hardwire_print_tty_state,
856 hardwire_noflush_set_tty_state,
857 hardwire_setbaudrate,
858 hardwire_setstopbits,
859 hardwire_drain_output, /* wait for output to drain */
863 _initialize_ser_hardwire ()
865 serial_add_interface (&hardwire_ops);