1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include "gdb_sys_time.h"
31 #include "gdb_select.h"
33 #include "filestuff.h"
34 #include "gdb_termios.h"
38 struct hardwire_ttystate
40 struct termios termios;
44 /* Boolean to explicitly enable or disable h/w flow control. */
45 static int serial_hwflow;
47 show_serial_hwflow (struct ui_file *file, int from_tty,
48 struct cmd_list_element *c, const char *value)
50 fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
58 /* It is believed that all systems which have added job control to SVR3
59 (e.g. sco) have also added termios. Even if not, trying to figure out
60 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
61 bewildering. So we don't attempt it. */
63 struct hardwire_ttystate
70 struct hardwire_ttystate
75 /* Line discipline flags. */
80 static int hardwire_open (struct serial *scb, const char *name);
81 static void hardwire_raw (struct serial *scb);
82 static int rate_to_code (int rate);
83 static int hardwire_setbaudrate (struct serial *scb, int rate);
84 static int hardwire_setparity (struct serial *scb, int parity);
85 static void hardwire_close (struct serial *scb);
86 static int get_tty_state (struct serial *scb,
87 struct hardwire_ttystate * state);
88 static int set_tty_state (struct serial *scb,
89 struct hardwire_ttystate * state);
90 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
91 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
92 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
94 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
96 static int hardwire_drain_output (struct serial *);
97 static int hardwire_flush_output (struct serial *);
98 static int hardwire_flush_input (struct serial *);
99 static int hardwire_send_break (struct serial *);
100 static int hardwire_setstopbits (struct serial *, int);
102 void _initialize_ser_hardwire (void);
104 /* Open up a real live device for serial I/O. */
107 hardwire_open (struct serial *scb, const char *name)
109 scb->fd = gdb_open_cloexec (name, O_RDWR, 0);
117 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
120 if (tcgetattr (scb->fd, &state->termios) < 0)
127 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
133 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
135 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
137 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
139 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
147 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
150 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
157 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
163 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
165 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
167 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
169 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
176 static serial_ttystate
177 hardwire_get_tty_state (struct serial *scb)
179 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
181 if (get_tty_state (scb, state))
187 return (serial_ttystate) state;
190 static serial_ttystate
191 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
193 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
195 *state = *(struct hardwire_ttystate *) ttystate;
197 return (serial_ttystate) state;
201 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
203 struct hardwire_ttystate *state;
205 state = (struct hardwire_ttystate *) ttystate;
207 return set_tty_state (scb, state);
211 hardwire_noflush_set_tty_state (struct serial *scb,
212 serial_ttystate new_ttystate,
213 serial_ttystate old_ttystate)
215 struct hardwire_ttystate new_state;
217 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
220 new_state = *(struct hardwire_ttystate *) new_ttystate;
222 /* Don't change in or out of raw mode; we don't want to flush input.
223 termio and termios have no such restriction; for them flushing input
224 is separate from setting the attributes. */
227 if (state->sgttyb.sg_flags & RAW)
228 new_state.sgttyb.sg_flags |= RAW;
230 new_state.sgttyb.sg_flags &= ~RAW;
232 /* I'm not sure whether this is necessary; the manpage just mentions
234 if (state->sgttyb.sg_flags & CBREAK)
235 new_state.sgttyb.sg_flags |= CBREAK;
237 new_state.sgttyb.sg_flags &= ~CBREAK;
240 return set_tty_state (scb, &new_state);
244 hardwire_print_tty_state (struct serial *scb,
245 serial_ttystate ttystate,
246 struct ui_file *stream)
248 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
252 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
253 (int) state->termios.c_iflag,
254 (int) state->termios.c_oflag);
255 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
256 (int) state->termios.c_cflag,
257 (int) state->termios.c_lflag);
259 /* This not in POSIX, and is not really documented by those systems
260 which have it (at least not Sun). */
261 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
263 fprintf_filtered (stream, "c_cc: ");
264 for (i = 0; i < NCCS; i += 1)
265 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
266 fprintf_filtered (stream, "\n");
270 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
271 state->termio.c_iflag, state->termio.c_oflag);
272 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
273 state->termio.c_cflag, state->termio.c_lflag,
274 state->termio.c_line);
275 fprintf_filtered (stream, "c_cc: ");
276 for (i = 0; i < NCC; i += 1)
277 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
278 fprintf_filtered (stream, "\n");
282 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
283 state->sgttyb.sg_flags);
285 fprintf_filtered (stream, "tchars: ");
286 for (i = 0; i < (int) sizeof (struct tchars); i++)
287 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
288 fprintf_filtered (stream, "\n");
290 fprintf_filtered (stream, "ltchars: ");
291 for (i = 0; i < (int) sizeof (struct ltchars); i++)
292 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
293 fprintf_filtered (stream, "\n");
295 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
299 /* Wait for the output to drain away, as opposed to flushing
303 hardwire_drain_output (struct serial *scb)
306 return tcdrain (scb->fd);
310 return ioctl (scb->fd, TCSBRK, 1);
314 /* Get the current state and then restore it using TIOCSETP,
315 which should cause the output to drain and pending input
318 struct hardwire_ttystate state;
320 if (get_tty_state (scb, &state))
326 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
333 hardwire_flush_output (struct serial *scb)
336 return tcflush (scb->fd, TCOFLUSH);
340 return ioctl (scb->fd, TCFLSH, 1);
344 /* This flushes both input and output, but we can't do better. */
345 return ioctl (scb->fd, TIOCFLUSH, 0);
350 hardwire_flush_input (struct serial *scb)
352 ser_base_flush_input (scb);
355 return tcflush (scb->fd, TCIFLUSH);
359 return ioctl (scb->fd, TCFLSH, 0);
363 /* This flushes both input and output, but we can't do better. */
364 return ioctl (scb->fd, TIOCFLUSH, 0);
369 hardwire_send_break (struct serial *scb)
372 return tcsendbreak (scb->fd, 0);
376 return ioctl (scb->fd, TCSBRK, 0);
383 status = ioctl (scb->fd, TIOCSBRK, 0);
385 /* Can't use usleep; it doesn't exist in BSD 4.2. */
386 /* Note that if this gdb_select() is interrupted by a signal it will not
387 wait the full length of time. I think that is OK. */
389 status = ioctl (scb->fd, TIOCCBRK, 0);
396 hardwire_raw (struct serial *scb)
398 struct hardwire_ttystate state;
400 if (get_tty_state (scb, &state))
401 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
402 safe_strerror (errno));
405 state.termios.c_iflag = 0;
406 state.termios.c_oflag = 0;
407 state.termios.c_lflag = 0;
408 state.termios.c_cflag &= ~CSIZE;
409 state.termios.c_cflag |= CLOCAL | CS8;
411 /* h/w flow control. */
413 state.termios.c_cflag |= CRTSCTS;
415 state.termios.c_cflag &= ~CRTSCTS;
418 state.termios.c_cflag |= CRTS_IFLOW;
420 state.termios.c_cflag &= ~CRTS_IFLOW;
423 state.termios.c_cc[VMIN] = 0;
424 state.termios.c_cc[VTIME] = 0;
428 state.termio.c_iflag = 0;
429 state.termio.c_oflag = 0;
430 state.termio.c_lflag = 0;
431 state.termio.c_cflag &= ~CSIZE;
432 state.termio.c_cflag |= CLOCAL | CS8;
433 state.termio.c_cc[VMIN] = 0;
434 state.termio.c_cc[VTIME] = 0;
438 state.sgttyb.sg_flags |= RAW | ANYP;
439 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
442 if (set_tty_state (scb, &state))
443 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
444 safe_strerror (errno));
455 /* Translate baud rates from integers to damn B_codes. Unix should
456 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
556 rate_to_code (int rate)
560 for (i = 0; baudtab[i].rate != -1; i++)
562 /* test for perfect macth. */
563 if (rate == baudtab[i].rate)
564 return baudtab[i].code;
567 /* check if it is in between valid values. */
568 if (rate < baudtab[i].rate)
572 warning (_("Invalid baud rate %d. "
573 "Closest values are %d and %d."),
574 rate, baudtab[i - 1].rate, baudtab[i].rate);
578 warning (_("Invalid baud rate %d. Minimum value is %d."),
579 rate, baudtab[0].rate);
586 /* The requested speed was too large. */
587 warning (_("Invalid baud rate %d. Maximum value is %d."),
588 rate, baudtab[i - 1].rate);
593 hardwire_setbaudrate (struct serial *scb, int rate)
595 struct hardwire_ttystate state;
596 int baud_code = rate_to_code (rate);
600 /* The baud rate was not valid.
601 A warning has already been issued. */
606 if (get_tty_state (scb, &state))
610 cfsetospeed (&state.termios, baud_code);
611 cfsetispeed (&state.termios, baud_code);
619 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
620 state.termio.c_cflag |= baud_code;
624 state.sgttyb.sg_ispeed = baud_code;
625 state.sgttyb.sg_ospeed = baud_code;
628 return set_tty_state (scb, &state);
632 hardwire_setstopbits (struct serial *scb, int num)
634 struct hardwire_ttystate state;
637 if (get_tty_state (scb, &state))
642 case SERIAL_1_STOPBITS:
645 case SERIAL_1_AND_A_HALF_STOPBITS:
646 case SERIAL_2_STOPBITS:
655 state.termios.c_cflag &= ~CSTOPB;
657 state.termios.c_cflag |= CSTOPB; /* two bits */
662 state.termio.c_cflag &= ~CSTOPB;
664 state.termio.c_cflag |= CSTOPB; /* two bits */
668 return 0; /* sgtty doesn't support this */
671 return set_tty_state (scb, &state);
674 /* Implement the "setparity" serial_ops callback. */
677 hardwire_setparity (struct serial *scb, int parity)
679 struct hardwire_ttystate state;
682 if (get_tty_state (scb, &state))
691 newparity = PARENB | PARODD;
697 internal_warning (__FILE__, __LINE__,
698 "Incorrect parity value: %d", parity);
703 state.termios.c_cflag &= ~(PARENB | PARODD);
704 state.termios.c_cflag |= newparity;
708 state.termio.c_cflag &= ~(PARENB | PARODD);
709 state.termio.c_cflag |= newparity;
713 return 0; /* sgtty doesn't support this */
715 return set_tty_state (scb, &state);
720 hardwire_close (struct serial *scb)
731 /* The hardwire ops. */
733 static const struct serial_ops hardwire_ops =
741 hardwire_flush_output,
742 hardwire_flush_input,
745 hardwire_get_tty_state,
746 hardwire_copy_tty_state,
747 hardwire_set_tty_state,
748 hardwire_print_tty_state,
749 hardwire_noflush_set_tty_state,
750 hardwire_setbaudrate,
751 hardwire_setstopbits,
753 hardwire_drain_output,
760 _initialize_ser_hardwire (void)
762 serial_add_interface (&hardwire_ops);
766 add_setshow_boolean_cmd ("remoteflow", no_class,
768 Set use of hardware flow control for remote serial I/O."), _("\
769 Show use of hardware flow control for remote serial I/O."), _("\
770 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
771 when debugging using remote targets."),
774 &setlist, &showlist);
780 ser_unix_read_prim (struct serial *scb, size_t count)
782 return read (scb->fd, scb->buf, count);
786 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
788 return write (scb->fd, buf, len);