1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993, 1994, 1998, 1999 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>
29 #ifdef HAVE_SYS_SELECT_H
30 #include <sys/select.h>
35 struct hardwire_ttystate
37 struct termios termios
;
43 /* It is believed that all systems which have added job control to SVR3
44 (e.g. sco) have also added termios. Even if not, trying to figure out
45 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
46 bewildering. So we don't attempt it. */
48 struct hardwire_ttystate
55 /* Needed for the code which uses select(). We would include <sys/select.h>
56 too if it existed on all systems. */
59 struct hardwire_ttystate
64 /* Line discipline flags. */
69 static int hardwire_open
PARAMS ((serial_t scb
, const char *name
));
70 static void hardwire_raw
PARAMS ((serial_t scb
));
71 static int wait_for
PARAMS ((serial_t scb
, int timeout
));
72 static int hardwire_readchar
PARAMS ((serial_t scb
, int timeout
));
73 static int rate_to_code
PARAMS ((int rate
));
74 static int hardwire_setbaudrate
PARAMS ((serial_t scb
, int rate
));
75 static int hardwire_write
PARAMS ((serial_t scb
, const char *str
, int len
));
76 static void hardwire_close
PARAMS ((serial_t scb
));
77 static int get_tty_state
PARAMS ((serial_t scb
, struct hardwire_ttystate
*state
));
78 static int set_tty_state
PARAMS ((serial_t scb
, struct hardwire_ttystate
*state
));
79 static serial_ttystate hardwire_get_tty_state
PARAMS ((serial_t scb
));
80 static int hardwire_set_tty_state
PARAMS ((serial_t scb
, serial_ttystate state
));
81 static int hardwire_noflush_set_tty_state
PARAMS ((serial_t
, serial_ttystate
,
83 static void hardwire_print_tty_state
PARAMS ((serial_t
, serial_ttystate
));
84 static int hardwire_drain_output
PARAMS ((serial_t
));
85 static int hardwire_flush_output
PARAMS ((serial_t
));
86 static int hardwire_flush_input
PARAMS ((serial_t
));
87 static int hardwire_send_break
PARAMS ((serial_t
));
88 static int hardwire_setstopbits
PARAMS ((serial_t
, int));
90 void _initialize_ser_hardwire
PARAMS ((void));
92 extern int (*ui_loop_hook
) PARAMS ((int));
94 /* Open up a real live device for serial I/O */
97 hardwire_open(scb
, name
)
101 scb
->fd
= open (name
, O_RDWR
);
109 get_tty_state (scb
, state
)
111 struct hardwire_ttystate
*state
;
114 if (tcgetattr(scb
->fd
, &state
->termios
) < 0)
121 if (ioctl (scb
->fd
, TCGETA
, &state
->termio
) < 0)
127 if (ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
) < 0)
129 if (ioctl (scb
->fd
, TIOCGETC
, &state
->tc
) < 0)
131 if (ioctl (scb
->fd
, TIOCGLTC
, &state
->ltc
) < 0)
133 if (ioctl (scb
->fd
, TIOCLGET
, &state
->lmode
) < 0)
141 set_tty_state(scb
, state
)
143 struct hardwire_ttystate
*state
;
146 if (tcsetattr(scb
->fd
, TCSANOW
, &state
->termios
) < 0)
153 if (ioctl (scb
->fd
, TCSETA
, &state
->termio
) < 0)
159 if (ioctl (scb
->fd
, TIOCSETN
, &state
->sgttyb
) < 0)
161 if (ioctl (scb
->fd
, TIOCSETC
, &state
->tc
) < 0)
163 if (ioctl (scb
->fd
, TIOCSLTC
, &state
->ltc
) < 0)
165 if (ioctl (scb
->fd
, TIOCLSET
, &state
->lmode
) < 0)
172 static serial_ttystate
173 hardwire_get_tty_state(scb
)
176 struct hardwire_ttystate
*state
;
178 state
= (struct hardwire_ttystate
*)xmalloc(sizeof *state
);
180 if (get_tty_state(scb
, state
))
183 return (serial_ttystate
)state
;
187 hardwire_set_tty_state(scb
, ttystate
)
189 serial_ttystate ttystate
;
191 struct hardwire_ttystate
*state
;
193 state
= (struct hardwire_ttystate
*)ttystate
;
195 return set_tty_state(scb
, state
);
199 hardwire_noflush_set_tty_state (scb
, new_ttystate
, old_ttystate
)
201 serial_ttystate new_ttystate
;
202 serial_ttystate old_ttystate
;
204 struct hardwire_ttystate new_state
;
206 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
209 new_state
= *(struct hardwire_ttystate
*)new_ttystate
;
211 /* Don't change in or out of raw mode; we don't want to flush input.
212 termio and termios have no such restriction; for them flushing input
213 is separate from setting the attributes. */
216 if (state
->sgttyb
.sg_flags
& RAW
)
217 new_state
.sgttyb
.sg_flags
|= RAW
;
219 new_state
.sgttyb
.sg_flags
&= ~RAW
;
221 /* I'm not sure whether this is necessary; the manpage just mentions
223 if (state
->sgttyb
.sg_flags
& CBREAK
)
224 new_state
.sgttyb
.sg_flags
|= CBREAK
;
226 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
229 return set_tty_state (scb
, &new_state
);
233 hardwire_print_tty_state (scb
, ttystate
)
235 serial_ttystate ttystate
;
237 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
241 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
242 state
->termios
.c_iflag
, state
->termios
.c_oflag
);
243 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
244 state
->termios
.c_cflag
, state
->termios
.c_lflag
);
246 /* This not in POSIX, and is not really documented by those systems
247 which have it (at least not Sun). */
248 printf_filtered ("c_line = 0x%x.\n", state
->termios
.c_line
);
250 printf_filtered ("c_cc: ");
251 for (i
= 0; i
< NCCS
; i
+= 1)
252 printf_filtered ("0x%x ", state
->termios
.c_cc
[i
]);
253 printf_filtered ("\n");
257 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
258 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
259 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
260 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
261 state
->termio
.c_line
);
262 printf_filtered ("c_cc: ");
263 for (i
= 0; i
< NCC
; i
+= 1)
264 printf_filtered ("0x%x ", state
->termio
.c_cc
[i
]);
265 printf_filtered ("\n");
269 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state
->sgttyb
.sg_flags
);
271 printf_filtered ("tchars: ");
272 for (i
= 0; i
< (int)sizeof (struct tchars
); i
++)
273 printf_filtered ("0x%x ", ((unsigned char *)&state
->tc
)[i
]);
274 printf_filtered ("\n");
276 printf_filtered ("ltchars: ");
277 for (i
= 0; i
< (int)sizeof (struct ltchars
); i
++)
278 printf_filtered ("0x%x ", ((unsigned char *)&state
->ltc
)[i
]);
279 printf_filtered ("\n");
281 printf_filtered ("lmode: 0x%x\n", state
->lmode
);
285 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
288 hardwire_drain_output (scb
)
292 return tcdrain (scb
->fd
);
296 return ioctl (scb
->fd
, TCSBRK
, 1);
300 /* Get the current state and then restore it using TIOCSETP,
301 which should cause the output to drain and pending input
304 struct hardwire_ttystate state
;
305 if (get_tty_state (scb
, &state
))
311 return (ioctl (scb
->fd
, TIOCSETP
, &state
.sgttyb
));
318 hardwire_flush_output (scb
)
322 return tcflush (scb
->fd
, TCOFLUSH
);
326 return ioctl (scb
->fd
, TCFLSH
, 1);
330 /* This flushes both input and output, but we can't do better. */
331 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
336 hardwire_flush_input (scb
)
340 scb
->bufp
= scb
->buf
;
343 return tcflush (scb
->fd
, TCIFLUSH
);
347 return ioctl (scb
->fd
, TCFLSH
, 0);
351 /* This flushes both input and output, but we can't do better. */
352 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
357 hardwire_send_break (scb
)
361 return tcsendbreak (scb
->fd
, 0);
365 return ioctl (scb
->fd
, TCSBRK
, 0);
371 struct timeval timeout
;
373 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
375 /* Can't use usleep; it doesn't exist in BSD 4.2. */
376 /* Note that if this select() is interrupted by a signal it will not wait
377 the full length of time. I think that is OK. */
379 timeout
.tv_usec
= 250000;
380 select (0, 0, 0, 0, &timeout
);
381 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
391 struct hardwire_ttystate state
;
393 if (get_tty_state(scb
, &state
))
394 fprintf_unfiltered(gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror(errno
));
397 state
.termios
.c_iflag
= 0;
398 state
.termios
.c_oflag
= 0;
399 state
.termios
.c_lflag
= 0;
400 state
.termios
.c_cflag
&= ~(CSIZE
|PARENB
);
401 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
402 state
.termios
.c_cc
[VMIN
] = 0;
403 state
.termios
.c_cc
[VTIME
] = 0;
407 state
.termio
.c_iflag
= 0;
408 state
.termio
.c_oflag
= 0;
409 state
.termio
.c_lflag
= 0;
410 state
.termio
.c_cflag
&= ~(CSIZE
|PARENB
);
411 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
412 state
.termio
.c_cc
[VMIN
] = 0;
413 state
.termio
.c_cc
[VTIME
] = 0;
417 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
418 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
421 scb
->current_timeout
= 0;
423 if (set_tty_state (scb
, &state
))
424 fprintf_unfiltered(gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror(errno
));
427 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
428 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
430 For termio{s}, we actually just setup VTIME if necessary, and let the
431 timeout occur in the read() in hardwire_read().
435 wait_for(scb
, timeout
)
449 FD_SET(scb
->fd
, &readfds
);
456 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, &tv
);
458 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, 0);
462 return SERIAL_TIMEOUT
;
463 else if (errno
== EINTR
)
466 return SERIAL_ERROR
; /* Got an error from select or poll */
471 #endif /* HAVE_SGTTY */
473 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
474 if (timeout
== scb
->current_timeout
)
477 scb
->current_timeout
= timeout
;
480 struct hardwire_ttystate state
;
482 if (get_tty_state(scb
, &state
))
483 fprintf_unfiltered(gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror(errno
));
489 state
.termios
.c_cc
[VTIME
] = 0;
490 state
.termios
.c_cc
[VMIN
] = 1;
494 state
.termios
.c_cc
[VMIN
] = 0;
495 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
496 if (state
.termios
.c_cc
[VTIME
] != timeout
* 10)
499 /* If c_cc is an 8-bit signed character, we can't go
500 bigger than this. If it is always unsigned, we could use
503 scb
->current_timeout
= 12;
504 state
.termios
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
505 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
514 state
.termio
.c_cc
[VTIME
] = 0;
515 state
.termio
.c_cc
[VMIN
] = 1;
519 state
.termio
.c_cc
[VMIN
] = 0;
520 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
521 if (state
.termio
.c_cc
[VTIME
] != timeout
* 10)
523 /* If c_cc is an 8-bit signed character, we can't go
524 bigger than this. If it is always unsigned, we could use
527 scb
->current_timeout
= 12;
528 state
.termio
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
529 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
534 if (set_tty_state (scb
, &state
))
535 fprintf_unfiltered(gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror(errno
));
539 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
542 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
543 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
544 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
545 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
547 hardwire_readchar (scb
, timeout
)
554 if (scb
->bufcnt
-- > 0)
560 /* We have to be able to keep the GUI alive here, so we break the original
561 timeout into steps of 1 second, running the "keep the GUI alive" hook
562 each time through the loop.
563 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
564 will only go through the loop once. */
566 delta
= (timeout
== 0 ? 0 : 1);
570 /* N.B. The UI may destroy our world (for instance by calling
571 remote_stop,) in which case we want to get out of here as
572 quickly as possible. It is not safe to touch scb, since
573 someone else might have freed it. The ui_loop_hook signals that
574 we should exit by returning 1. */
577 detach
= ui_loop_hook (0);
580 return SERIAL_TIMEOUT
;
582 scb
->timeout_remaining
= (timeout
< 0 ? timeout
: timeout
- delta
);
583 status
= wait_for (scb
, delta
);
588 scb
->bufcnt
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
590 if (scb
->bufcnt
<= 0)
592 if (scb
->bufcnt
== 0)
594 /* Zero characters means timeout (it could also be EOF, but
595 we don't (yet at least) distinguish). */
596 if (scb
->timeout_remaining
> 0)
598 timeout
= scb
->timeout_remaining
;
601 else if (scb
->timeout_remaining
< 0)
604 return SERIAL_TIMEOUT
;
606 else if (errno
== EINTR
)
609 return SERIAL_ERROR
; /* Got an error from read */
613 scb
->bufp
= scb
->buf
;
626 /* Translate baud rates from integers to damn B_codes. Unix should
627 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
672 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
673 if (rate
== baudtab
[i
].rate
)
674 return baudtab
[i
].code
;
680 hardwire_setbaudrate(scb
, rate
)
684 struct hardwire_ttystate state
;
686 if (get_tty_state(scb
, &state
))
690 cfsetospeed (&state
.termios
, rate_to_code (rate
));
691 cfsetispeed (&state
.termios
, rate_to_code (rate
));
699 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
700 state
.termio
.c_cflag
|= rate_to_code (rate
);
704 state
.sgttyb
.sg_ispeed
= rate_to_code (rate
);
705 state
.sgttyb
.sg_ospeed
= rate_to_code (rate
);
708 return set_tty_state (scb
, &state
);
712 hardwire_setstopbits(scb
, num
)
716 struct hardwire_ttystate state
;
719 if (get_tty_state(scb
, &state
))
724 case SERIAL_1_STOPBITS
:
727 case SERIAL_1_AND_A_HALF_STOPBITS
:
728 case SERIAL_2_STOPBITS
:
737 state
.termios
.c_cflag
&= ~CSTOPB
;
739 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
744 state
.termio
.c_cflag
&= ~CSTOPB
;
746 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
750 return 0; /* sgtty doesn't support this */
753 return set_tty_state (scb
, &state
);
757 hardwire_write(scb
, str
, len
)
766 cc
= write(scb
->fd
, str
, len
);
787 static struct serial_ops hardwire_ops
=
795 hardwire_flush_output
,
796 hardwire_flush_input
,
799 hardwire_get_tty_state
,
800 hardwire_set_tty_state
,
801 hardwire_print_tty_state
,
802 hardwire_noflush_set_tty_state
,
803 hardwire_setbaudrate
,
804 hardwire_setstopbits
,
805 hardwire_drain_output
, /* wait for output to drain */
809 _initialize_ser_hardwire ()
811 serial_add_interface (&hardwire_ops
);