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"
37 struct hardwire_ttystate
39 struct termios termios
;
43 /* Boolean to explicitly enable or disable h/w flow control. */
44 static int serial_hwflow
;
46 show_serial_hwflow (struct ui_file
*file
, int from_tty
,
47 struct cmd_list_element
*c
, const char *value
)
49 fprintf_filtered (file
, _("Hardware flow control is %s.\n"), value
);
57 /* It is believed that all systems which have added job control to SVR3
58 (e.g. sco) have also added termios. Even if not, trying to figure out
59 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
60 bewildering. So we don't attempt it. */
62 struct hardwire_ttystate
69 struct hardwire_ttystate
74 /* Line discipline flags. */
79 static int hardwire_open (struct serial
*scb
, const char *name
);
80 static void hardwire_raw (struct serial
*scb
);
81 static int rate_to_code (int rate
);
82 static int hardwire_setbaudrate (struct serial
*scb
, int rate
);
83 static int hardwire_setparity (struct serial
*scb
, int parity
);
84 static void hardwire_close (struct serial
*scb
);
85 static int get_tty_state (struct serial
*scb
,
86 struct hardwire_ttystate
* state
);
87 static int set_tty_state (struct serial
*scb
,
88 struct hardwire_ttystate
* state
);
89 static serial_ttystate
hardwire_get_tty_state (struct serial
*scb
);
90 static int hardwire_set_tty_state (struct serial
*scb
, serial_ttystate state
);
91 static int hardwire_noflush_set_tty_state (struct serial
*, serial_ttystate
,
93 static void hardwire_print_tty_state (struct serial
*, serial_ttystate
,
95 static int hardwire_drain_output (struct serial
*);
96 static int hardwire_flush_output (struct serial
*);
97 static int hardwire_flush_input (struct serial
*);
98 static int hardwire_send_break (struct serial
*);
99 static int hardwire_setstopbits (struct serial
*, int);
101 void _initialize_ser_hardwire (void);
103 /* Open up a real live device for serial I/O. */
106 hardwire_open (struct serial
*scb
, const char *name
)
108 scb
->fd
= gdb_open_cloexec (name
, O_RDWR
, 0);
116 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
119 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
126 if (ioctl (scb
->fd
, TCGETA
, &state
->termio
) < 0)
132 if (ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
) < 0)
134 if (ioctl (scb
->fd
, TIOCGETC
, &state
->tc
) < 0)
136 if (ioctl (scb
->fd
, TIOCGLTC
, &state
->ltc
) < 0)
138 if (ioctl (scb
->fd
, TIOCLGET
, &state
->lmode
) < 0)
146 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
149 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
156 if (ioctl (scb
->fd
, TCSETA
, &state
->termio
) < 0)
162 if (ioctl (scb
->fd
, TIOCSETN
, &state
->sgttyb
) < 0)
164 if (ioctl (scb
->fd
, TIOCSETC
, &state
->tc
) < 0)
166 if (ioctl (scb
->fd
, TIOCSLTC
, &state
->ltc
) < 0)
168 if (ioctl (scb
->fd
, TIOCLSET
, &state
->lmode
) < 0)
175 static serial_ttystate
176 hardwire_get_tty_state (struct serial
*scb
)
178 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
180 if (get_tty_state (scb
, state
))
186 return (serial_ttystate
) state
;
189 static serial_ttystate
190 hardwire_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
192 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
194 *state
= *(struct hardwire_ttystate
*) ttystate
;
196 return (serial_ttystate
) state
;
200 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
202 struct hardwire_ttystate
*state
;
204 state
= (struct hardwire_ttystate
*) ttystate
;
206 return set_tty_state (scb
, state
);
210 hardwire_noflush_set_tty_state (struct serial
*scb
,
211 serial_ttystate new_ttystate
,
212 serial_ttystate old_ttystate
)
214 struct hardwire_ttystate new_state
;
216 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
219 new_state
= *(struct hardwire_ttystate
*) new_ttystate
;
221 /* Don't change in or out of raw mode; we don't want to flush input.
222 termio and termios have no such restriction; for them flushing input
223 is separate from setting the attributes. */
226 if (state
->sgttyb
.sg_flags
& RAW
)
227 new_state
.sgttyb
.sg_flags
|= RAW
;
229 new_state
.sgttyb
.sg_flags
&= ~RAW
;
231 /* I'm not sure whether this is necessary; the manpage just mentions
233 if (state
->sgttyb
.sg_flags
& CBREAK
)
234 new_state
.sgttyb
.sg_flags
|= CBREAK
;
236 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
239 return set_tty_state (scb
, &new_state
);
243 hardwire_print_tty_state (struct serial
*scb
,
244 serial_ttystate ttystate
,
245 struct ui_file
*stream
)
247 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
251 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
252 (int) state
->termios
.c_iflag
,
253 (int) state
->termios
.c_oflag
);
254 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
255 (int) state
->termios
.c_cflag
,
256 (int) state
->termios
.c_lflag
);
258 /* This not in POSIX, and is not really documented by those systems
259 which have it (at least not Sun). */
260 fprintf_filtered (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
262 fprintf_filtered (stream
, "c_cc: ");
263 for (i
= 0; i
< NCCS
; i
+= 1)
264 fprintf_filtered (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
265 fprintf_filtered (stream
, "\n");
269 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
270 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
271 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
272 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
273 state
->termio
.c_line
);
274 fprintf_filtered (stream
, "c_cc: ");
275 for (i
= 0; i
< NCC
; i
+= 1)
276 fprintf_filtered (stream
, "0x%x ", state
->termio
.c_cc
[i
]);
277 fprintf_filtered (stream
, "\n");
281 fprintf_filtered (stream
, "sgttyb.sg_flags = 0x%x.\n",
282 state
->sgttyb
.sg_flags
);
284 fprintf_filtered (stream
, "tchars: ");
285 for (i
= 0; i
< (int) sizeof (struct tchars
); i
++)
286 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->tc
)[i
]);
287 fprintf_filtered (stream
, "\n");
289 fprintf_filtered (stream
, "ltchars: ");
290 for (i
= 0; i
< (int) sizeof (struct ltchars
); i
++)
291 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->ltc
)[i
]);
292 fprintf_filtered (stream
, "\n");
294 fprintf_filtered (stream
, "lmode: 0x%x\n", state
->lmode
);
298 /* Wait for the output to drain away, as opposed to flushing
302 hardwire_drain_output (struct serial
*scb
)
305 return tcdrain (scb
->fd
);
309 return ioctl (scb
->fd
, TCSBRK
, 1);
313 /* Get the current state and then restore it using TIOCSETP,
314 which should cause the output to drain and pending input
317 struct hardwire_ttystate state
;
319 if (get_tty_state (scb
, &state
))
325 return (ioctl (scb
->fd
, TIOCSETP
, &state
.sgttyb
));
332 hardwire_flush_output (struct serial
*scb
)
335 return tcflush (scb
->fd
, TCOFLUSH
);
339 return ioctl (scb
->fd
, TCFLSH
, 1);
343 /* This flushes both input and output, but we can't do better. */
344 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
349 hardwire_flush_input (struct serial
*scb
)
351 ser_base_flush_input (scb
);
354 return tcflush (scb
->fd
, TCIFLUSH
);
358 return ioctl (scb
->fd
, TCFLSH
, 0);
362 /* This flushes both input and output, but we can't do better. */
363 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
368 hardwire_send_break (struct serial
*scb
)
371 return tcsendbreak (scb
->fd
, 0);
375 return ioctl (scb
->fd
, TCSBRK
, 0);
382 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
384 /* Can't use usleep; it doesn't exist in BSD 4.2. */
385 /* Note that if this gdb_select() is interrupted by a signal it will not
386 wait the full length of time. I think that is OK. */
388 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
395 hardwire_raw (struct serial
*scb
)
397 struct hardwire_ttystate state
;
399 if (get_tty_state (scb
, &state
))
400 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n",
401 safe_strerror (errno
));
404 state
.termios
.c_iflag
= 0;
405 state
.termios
.c_oflag
= 0;
406 state
.termios
.c_lflag
= 0;
407 state
.termios
.c_cflag
&= ~CSIZE
;
408 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
410 /* h/w flow control. */
412 state
.termios
.c_cflag
|= CRTSCTS
;
414 state
.termios
.c_cflag
&= ~CRTSCTS
;
417 state
.termios
.c_cflag
|= CRTS_IFLOW
;
419 state
.termios
.c_cflag
&= ~CRTS_IFLOW
;
422 state
.termios
.c_cc
[VMIN
] = 0;
423 state
.termios
.c_cc
[VTIME
] = 0;
427 state
.termio
.c_iflag
= 0;
428 state
.termio
.c_oflag
= 0;
429 state
.termio
.c_lflag
= 0;
430 state
.termio
.c_cflag
&= ~CSIZE
;
431 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
432 state
.termio
.c_cc
[VMIN
] = 0;
433 state
.termio
.c_cc
[VTIME
] = 0;
437 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
438 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
441 if (set_tty_state (scb
, &state
))
442 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n",
443 safe_strerror (errno
));
454 /* Translate baud rates from integers to damn B_codes. Unix should
455 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
555 rate_to_code (int rate
)
559 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
561 /* test for perfect macth. */
562 if (rate
== baudtab
[i
].rate
)
563 return baudtab
[i
].code
;
566 /* check if it is in between valid values. */
567 if (rate
< baudtab
[i
].rate
)
571 warning (_("Invalid baud rate %d. "
572 "Closest values are %d and %d."),
573 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
577 warning (_("Invalid baud rate %d. Minimum value is %d."),
578 rate
, baudtab
[0].rate
);
585 /* The requested speed was too large. */
586 warning (_("Invalid baud rate %d. Maximum value is %d."),
587 rate
, baudtab
[i
- 1].rate
);
592 hardwire_setbaudrate (struct serial
*scb
, int rate
)
594 struct hardwire_ttystate state
;
595 int baud_code
= rate_to_code (rate
);
599 /* The baud rate was not valid.
600 A warning has already been issued. */
605 if (get_tty_state (scb
, &state
))
609 cfsetospeed (&state
.termios
, baud_code
);
610 cfsetispeed (&state
.termios
, baud_code
);
618 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
619 state
.termio
.c_cflag
|= baud_code
;
623 state
.sgttyb
.sg_ispeed
= baud_code
;
624 state
.sgttyb
.sg_ospeed
= baud_code
;
627 return set_tty_state (scb
, &state
);
631 hardwire_setstopbits (struct serial
*scb
, int num
)
633 struct hardwire_ttystate state
;
636 if (get_tty_state (scb
, &state
))
641 case SERIAL_1_STOPBITS
:
644 case SERIAL_1_AND_A_HALF_STOPBITS
:
645 case SERIAL_2_STOPBITS
:
654 state
.termios
.c_cflag
&= ~CSTOPB
;
656 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
661 state
.termio
.c_cflag
&= ~CSTOPB
;
663 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
667 return 0; /* sgtty doesn't support this */
670 return set_tty_state (scb
, &state
);
673 /* Implement the "setparity" serial_ops callback. */
676 hardwire_setparity (struct serial
*scb
, int parity
)
678 struct hardwire_ttystate state
;
681 if (get_tty_state (scb
, &state
))
690 newparity
= PARENB
| PARODD
;
696 internal_warning (__FILE__
, __LINE__
,
697 "Incorrect parity value: %d", parity
);
702 state
.termios
.c_cflag
&= ~(PARENB
| PARODD
);
703 state
.termios
.c_cflag
|= newparity
;
707 state
.termio
.c_cflag
&= ~(PARENB
| PARODD
);
708 state
.termio
.c_cflag
|= newparity
;
712 return 0; /* sgtty doesn't support this */
714 return set_tty_state (scb
, &state
);
719 hardwire_close (struct serial
*scb
)
730 /* The hardwire ops. */
732 static const struct serial_ops hardwire_ops
=
740 hardwire_flush_output
,
741 hardwire_flush_input
,
744 hardwire_get_tty_state
,
745 hardwire_copy_tty_state
,
746 hardwire_set_tty_state
,
747 hardwire_print_tty_state
,
748 hardwire_noflush_set_tty_state
,
749 hardwire_setbaudrate
,
750 hardwire_setstopbits
,
752 hardwire_drain_output
,
759 _initialize_ser_hardwire (void)
761 serial_add_interface (&hardwire_ops
);
765 add_setshow_boolean_cmd ("remoteflow", no_class
,
767 Set use of hardware flow control for remote serial I/O."), _("\
768 Show use of hardware flow control for remote serial I/O."), _("\
769 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
770 when debugging using remote targets."),
773 &setlist
, &showlist
);
779 ser_unix_read_prim (struct serial
*scb
, size_t count
)
781 return read (scb
->fd
, scb
->buf
, count
);
785 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
787 return write (scb
->fd
, buf
, len
);