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 /* Open up a real live device for serial I/O. */
105 hardwire_open (struct serial
*scb
, const char *name
)
107 scb
->fd
= gdb_open_cloexec (name
, O_RDWR
, 0);
115 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
118 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
125 if (ioctl (scb
->fd
, TCGETA
, &state
->termio
) < 0)
131 if (ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
) < 0)
133 if (ioctl (scb
->fd
, TIOCGETC
, &state
->tc
) < 0)
135 if (ioctl (scb
->fd
, TIOCGLTC
, &state
->ltc
) < 0)
137 if (ioctl (scb
->fd
, TIOCLGET
, &state
->lmode
) < 0)
145 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
148 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
155 if (ioctl (scb
->fd
, TCSETA
, &state
->termio
) < 0)
161 if (ioctl (scb
->fd
, TIOCSETN
, &state
->sgttyb
) < 0)
163 if (ioctl (scb
->fd
, TIOCSETC
, &state
->tc
) < 0)
165 if (ioctl (scb
->fd
, TIOCSLTC
, &state
->ltc
) < 0)
167 if (ioctl (scb
->fd
, TIOCLSET
, &state
->lmode
) < 0)
174 static serial_ttystate
175 hardwire_get_tty_state (struct serial
*scb
)
177 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
179 if (get_tty_state (scb
, state
))
185 return (serial_ttystate
) state
;
188 static serial_ttystate
189 hardwire_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
191 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
193 *state
= *(struct hardwire_ttystate
*) ttystate
;
195 return (serial_ttystate
) state
;
199 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
201 struct hardwire_ttystate
*state
;
203 state
= (struct hardwire_ttystate
*) ttystate
;
205 return set_tty_state (scb
, state
);
209 hardwire_noflush_set_tty_state (struct serial
*scb
,
210 serial_ttystate new_ttystate
,
211 serial_ttystate old_ttystate
)
213 struct hardwire_ttystate new_state
;
215 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
218 new_state
= *(struct hardwire_ttystate
*) new_ttystate
;
220 /* Don't change in or out of raw mode; we don't want to flush input.
221 termio and termios have no such restriction; for them flushing input
222 is separate from setting the attributes. */
225 if (state
->sgttyb
.sg_flags
& RAW
)
226 new_state
.sgttyb
.sg_flags
|= RAW
;
228 new_state
.sgttyb
.sg_flags
&= ~RAW
;
230 /* I'm not sure whether this is necessary; the manpage just mentions
232 if (state
->sgttyb
.sg_flags
& CBREAK
)
233 new_state
.sgttyb
.sg_flags
|= CBREAK
;
235 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
238 return set_tty_state (scb
, &new_state
);
242 hardwire_print_tty_state (struct serial
*scb
,
243 serial_ttystate ttystate
,
244 struct ui_file
*stream
)
246 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
250 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
251 (int) state
->termios
.c_iflag
,
252 (int) state
->termios
.c_oflag
);
253 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
254 (int) state
->termios
.c_cflag
,
255 (int) state
->termios
.c_lflag
);
257 /* This not in POSIX, and is not really documented by those systems
258 which have it (at least not Sun). */
259 fprintf_filtered (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
261 fprintf_filtered (stream
, "c_cc: ");
262 for (i
= 0; i
< NCCS
; i
+= 1)
263 fprintf_filtered (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
264 fprintf_filtered (stream
, "\n");
268 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
269 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
270 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
271 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
272 state
->termio
.c_line
);
273 fprintf_filtered (stream
, "c_cc: ");
274 for (i
= 0; i
< NCC
; i
+= 1)
275 fprintf_filtered (stream
, "0x%x ", state
->termio
.c_cc
[i
]);
276 fprintf_filtered (stream
, "\n");
280 fprintf_filtered (stream
, "sgttyb.sg_flags = 0x%x.\n",
281 state
->sgttyb
.sg_flags
);
283 fprintf_filtered (stream
, "tchars: ");
284 for (i
= 0; i
< (int) sizeof (struct tchars
); i
++)
285 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->tc
)[i
]);
286 fprintf_filtered (stream
, "\n");
288 fprintf_filtered (stream
, "ltchars: ");
289 for (i
= 0; i
< (int) sizeof (struct ltchars
); i
++)
290 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->ltc
)[i
]);
291 fprintf_filtered (stream
, "\n");
293 fprintf_filtered (stream
, "lmode: 0x%x\n", state
->lmode
);
297 /* Wait for the output to drain away, as opposed to flushing
301 hardwire_drain_output (struct serial
*scb
)
304 return tcdrain (scb
->fd
);
308 return ioctl (scb
->fd
, TCSBRK
, 1);
312 /* Get the current state and then restore it using TIOCSETP,
313 which should cause the output to drain and pending input
316 struct hardwire_ttystate state
;
318 if (get_tty_state (scb
, &state
))
324 return (ioctl (scb
->fd
, TIOCSETP
, &state
.sgttyb
));
331 hardwire_flush_output (struct serial
*scb
)
334 return tcflush (scb
->fd
, TCOFLUSH
);
338 return ioctl (scb
->fd
, TCFLSH
, 1);
342 /* This flushes both input and output, but we can't do better. */
343 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
348 hardwire_flush_input (struct serial
*scb
)
350 ser_base_flush_input (scb
);
353 return tcflush (scb
->fd
, TCIFLUSH
);
357 return ioctl (scb
->fd
, TCFLSH
, 0);
361 /* This flushes both input and output, but we can't do better. */
362 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
367 hardwire_send_break (struct serial
*scb
)
370 return tcsendbreak (scb
->fd
, 0);
374 return ioctl (scb
->fd
, TCSBRK
, 0);
381 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
383 /* Can't use usleep; it doesn't exist in BSD 4.2. */
384 /* Note that if this gdb_select() is interrupted by a signal it will not
385 wait the full length of time. I think that is OK. */
387 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
394 hardwire_raw (struct serial
*scb
)
396 struct hardwire_ttystate state
;
398 if (get_tty_state (scb
, &state
))
399 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n",
400 safe_strerror (errno
));
403 state
.termios
.c_iflag
= 0;
404 state
.termios
.c_oflag
= 0;
405 state
.termios
.c_lflag
= 0;
406 state
.termios
.c_cflag
&= ~CSIZE
;
407 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
409 /* h/w flow control. */
411 state
.termios
.c_cflag
|= CRTSCTS
;
413 state
.termios
.c_cflag
&= ~CRTSCTS
;
416 state
.termios
.c_cflag
|= CRTS_IFLOW
;
418 state
.termios
.c_cflag
&= ~CRTS_IFLOW
;
421 state
.termios
.c_cc
[VMIN
] = 0;
422 state
.termios
.c_cc
[VTIME
] = 0;
426 state
.termio
.c_iflag
= 0;
427 state
.termio
.c_oflag
= 0;
428 state
.termio
.c_lflag
= 0;
429 state
.termio
.c_cflag
&= ~CSIZE
;
430 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
431 state
.termio
.c_cc
[VMIN
] = 0;
432 state
.termio
.c_cc
[VTIME
] = 0;
436 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
437 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
440 if (set_tty_state (scb
, &state
))
441 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n",
442 safe_strerror (errno
));
453 /* Translate baud rates from integers to damn B_codes. Unix should
454 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
554 rate_to_code (int rate
)
558 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
560 /* test for perfect macth. */
561 if (rate
== baudtab
[i
].rate
)
562 return baudtab
[i
].code
;
565 /* check if it is in between valid values. */
566 if (rate
< baudtab
[i
].rate
)
570 warning (_("Invalid baud rate %d. "
571 "Closest values are %d and %d."),
572 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
576 warning (_("Invalid baud rate %d. Minimum value is %d."),
577 rate
, baudtab
[0].rate
);
584 /* The requested speed was too large. */
585 warning (_("Invalid baud rate %d. Maximum value is %d."),
586 rate
, baudtab
[i
- 1].rate
);
591 hardwire_setbaudrate (struct serial
*scb
, int rate
)
593 struct hardwire_ttystate state
;
594 int baud_code
= rate_to_code (rate
);
598 /* The baud rate was not valid.
599 A warning has already been issued. */
604 if (get_tty_state (scb
, &state
))
608 cfsetospeed (&state
.termios
, baud_code
);
609 cfsetispeed (&state
.termios
, baud_code
);
617 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
618 state
.termio
.c_cflag
|= baud_code
;
622 state
.sgttyb
.sg_ispeed
= baud_code
;
623 state
.sgttyb
.sg_ospeed
= baud_code
;
626 return set_tty_state (scb
, &state
);
630 hardwire_setstopbits (struct serial
*scb
, int num
)
632 struct hardwire_ttystate state
;
635 if (get_tty_state (scb
, &state
))
640 case SERIAL_1_STOPBITS
:
643 case SERIAL_1_AND_A_HALF_STOPBITS
:
644 case SERIAL_2_STOPBITS
:
653 state
.termios
.c_cflag
&= ~CSTOPB
;
655 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
660 state
.termio
.c_cflag
&= ~CSTOPB
;
662 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
666 return 0; /* sgtty doesn't support this */
669 return set_tty_state (scb
, &state
);
672 /* Implement the "setparity" serial_ops callback. */
675 hardwire_setparity (struct serial
*scb
, int parity
)
677 struct hardwire_ttystate state
;
680 if (get_tty_state (scb
, &state
))
689 newparity
= PARENB
| PARODD
;
695 internal_warning (__FILE__
, __LINE__
,
696 "Incorrect parity value: %d", parity
);
701 state
.termios
.c_cflag
&= ~(PARENB
| PARODD
);
702 state
.termios
.c_cflag
|= newparity
;
706 state
.termio
.c_cflag
&= ~(PARENB
| PARODD
);
707 state
.termio
.c_cflag
|= newparity
;
711 return 0; /* sgtty doesn't support this */
713 return set_tty_state (scb
, &state
);
718 hardwire_close (struct serial
*scb
)
729 /* The hardwire ops. */
731 static const struct serial_ops hardwire_ops
=
739 hardwire_flush_output
,
740 hardwire_flush_input
,
743 hardwire_get_tty_state
,
744 hardwire_copy_tty_state
,
745 hardwire_set_tty_state
,
746 hardwire_print_tty_state
,
747 hardwire_noflush_set_tty_state
,
748 hardwire_setbaudrate
,
749 hardwire_setstopbits
,
751 hardwire_drain_output
,
758 _initialize_ser_hardwire (void)
760 serial_add_interface (&hardwire_ops
);
764 add_setshow_boolean_cmd ("remoteflow", no_class
,
766 Set use of hardware flow control for remote serial I/O."), _("\
767 Show use of hardware flow control for remote serial I/O."), _("\
768 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
769 when debugging using remote targets."),
772 &setlist
, &showlist
);
778 ser_unix_read_prim (struct serial
*scb
, size_t count
)
780 return read (scb
->fd
, scb
->buf
, count
);
784 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
786 return write (scb
->fd
, buf
, len
);