1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993, 1994 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>
25 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
33 struct hardwire_ttystate
35 struct termios termios;
42 /* It is believed that all systems which have added job control to SVR3
43 (e.g. sco) have also added termios. Even if not, trying to figure out
44 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
45 bewildering. So we don't attempt it. */
47 struct hardwire_ttystate
54 /* Needed for the code which uses select(). We would include <sys/select.h>
55 too if it existed on all systems. */
60 struct hardwire_ttystate
65 /* Line discipline flags. */
70 static int hardwire_open PARAMS ((serial_t scb, const char *name));
71 static void hardwire_raw PARAMS ((serial_t scb));
72 static int wait_for PARAMS ((serial_t scb, int timeout));
73 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
74 static int rate_to_code PARAMS ((int rate));
75 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
76 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
77 /* FIXME: static void hardwire_restore PARAMS ((serial_t scb)); */
78 static void hardwire_close PARAMS ((serial_t scb));
79 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
80 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
81 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
82 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
84 /* Open up a real live device for serial I/O */
87 hardwire_open(scb, name)
91 scb->fd = open (name, O_RDWR);
99 get_tty_state(scb, state)
101 struct hardwire_ttystate *state;
106 if (tcgetattr(scb->fd, &state->termios) < 0)
113 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
119 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
121 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
123 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
125 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
133 set_tty_state(scb, state)
135 struct hardwire_ttystate *state;
138 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
145 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
151 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
153 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
155 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
157 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
164 static serial_ttystate
165 hardwire_get_tty_state(scb)
168 struct hardwire_ttystate *state;
170 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
172 if (get_tty_state(scb, state))
175 return (serial_ttystate)state;
179 hardwire_set_tty_state(scb, ttystate)
181 serial_ttystate ttystate;
183 struct hardwire_ttystate *state;
185 state = (struct hardwire_ttystate *)ttystate;
187 return set_tty_state(scb, state);
191 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
193 serial_ttystate new_ttystate;
194 serial_ttystate old_ttystate;
196 struct hardwire_ttystate new_state;
198 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
201 new_state = *(struct hardwire_ttystate *)new_ttystate;
203 /* Don't change in or out of raw mode; we don't want to flush input.
204 termio and termios have no such restriction; for them flushing input
205 is separate from setting the attributes. */
208 if (state->sgttyb.sg_flags & RAW)
209 new_state.sgttyb.sg_flags |= RAW;
211 new_state.sgttyb.sg_flags &= ~RAW;
213 /* I'm not sure whether this is necessary; the manpage just mentions
215 if (state->sgttyb.sg_flags & CBREAK)
216 new_state.sgttyb.sg_flags |= CBREAK;
218 new_state.sgttyb.sg_flags &= ~CBREAK;
221 return set_tty_state (scb, &new_state);
225 hardwire_print_tty_state (scb, ttystate)
227 serial_ttystate ttystate;
229 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
233 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
234 state->termios.c_iflag, state->termios.c_oflag);
235 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
236 state->termios.c_cflag, state->termios.c_lflag);
238 /* This not in POSIX, and is not really documented by those systems
239 which have it (at least not Sun). */
240 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
242 printf_filtered ("c_cc: ");
243 for (i = 0; i < NCCS; i += 1)
244 printf_filtered ("0x%x ", state->termios.c_cc[i]);
245 printf_filtered ("\n");
249 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
250 state->termio.c_iflag, state->termio.c_oflag);
251 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
252 state->termio.c_cflag, state->termio.c_lflag,
253 state->termio.c_line);
254 printf_filtered ("c_cc: ");
255 for (i = 0; i < NCC; i += 1)
256 printf_filtered ("0x%x ", state->termio.c_cc[i]);
257 printf_filtered ("\n");
261 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
263 printf_filtered ("tchars: ");
264 for (i = 0; i < (int)sizeof (struct tchars); i++)
265 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
266 printf_filtered ("\n");
268 printf_filtered ("ltchars: ");
269 for (i = 0; i < (int)sizeof (struct ltchars); i++)
270 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
271 printf_filtered ("\n");
273 printf_filtered ("lmode: 0x%x\n", state->lmode);
278 hardwire_flush_output (scb)
282 return tcflush (scb->fd, TCOFLUSH);
286 return ioctl (scb->fd, TCFLSH, 1);
290 /* This flushes both input and output, but we can't do better. */
291 return ioctl (scb->fd, TIOCFLUSH, 0);
296 hardwire_flush_input (scb)
300 return tcflush (scb->fd, TCIFLUSH);
304 return ioctl (scb->fd, TCFLSH, 0);
308 /* This flushes both input and output, but we can't do better. */
309 return ioctl (scb->fd, TIOCFLUSH, 0);
314 hardwire_send_break (scb)
318 return tcsendbreak (scb->fd, 0);
322 return ioctl (scb->fd, TCSBRK, 0);
328 struct timeval timeout;
330 status = ioctl (scb->fd, TIOCSBRK, 0);
332 /* Can't use usleep; it doesn't exist in BSD 4.2. */
333 /* Note that if this select() is interrupted by a signal it will not wait
334 the full length of time. I think that is OK. */
336 timeout.tv_usec = 250000;
337 select (0, 0, 0, 0, &timeout);
338 status = ioctl (scb->fd, TIOCCBRK, 0);
348 struct hardwire_ttystate state;
350 if (get_tty_state(scb, &state))
351 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
354 state.termios.c_iflag = 0;
355 state.termios.c_oflag = 0;
356 state.termios.c_lflag = 0;
357 state.termios.c_cflag &= ~(CSIZE|PARENB);
358 state.termios.c_cflag |= CLOCAL | CS8;
359 state.termios.c_cc[VMIN] = 0;
360 state.termios.c_cc[VTIME] = 0;
364 state.termio.c_iflag = 0;
365 state.termio.c_oflag = 0;
366 state.termio.c_lflag = 0;
367 state.termio.c_cflag &= ~(CSIZE|PARENB);
368 state.termio.c_cflag |= CLOCAL | CS8;
369 state.termio.c_cc[VMIN] = 0;
370 state.termio.c_cc[VTIME] = 0;
374 state.sgttyb.sg_flags |= RAW | ANYP;
375 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
378 scb->current_timeout = 0;
380 if (set_tty_state (scb, &state))
381 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
384 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
385 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
387 For termio{s}, we actually just setup VTIME if necessary, and let the
388 timeout occur in the read() in hardwire_read().
392 wait_for(scb, timeout)
396 scb->timeout_remaining = 0;
408 FD_SET(scb->fd, &readfds);
415 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
417 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
421 return SERIAL_TIMEOUT;
422 else if (errno == EINTR)
425 return SERIAL_ERROR; /* Got an error from select or poll */
430 #endif /* HAVE_SGTTY */
432 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
433 if (timeout == scb->current_timeout)
436 scb->current_timeout = timeout;
439 struct hardwire_ttystate state;
441 if (get_tty_state(scb, &state))
442 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
448 state.termios.c_cc[VTIME] = 0;
449 state.termios.c_cc[VMIN] = 1;
453 state.termios.c_cc[VMIN] = 0;
454 state.termios.c_cc[VTIME] = timeout * 10;
455 if (state.termios.c_cc[VTIME] != timeout * 10)
458 /* If c_cc is an 8-bit signed character, we can't go
459 bigger than this. If it is always unsigned, we could use
462 scb->current_timeout = 12;
463 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
464 scb->timeout_remaining = timeout - scb->current_timeout;
473 state.termio.c_cc[VTIME] = 0;
474 state.termio.c_cc[VMIN] = 1;
478 state.termio.c_cc[VMIN] = 0;
479 state.termio.c_cc[VTIME] = timeout * 10;
480 if (state.termio.c_cc[VTIME] != timeout * 10)
482 /* If c_cc is an 8-bit signed character, we can't go
483 bigger than this. If it is always unsigned, we could use
486 scb->current_timeout = 12;
487 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
488 scb->timeout_remaining = timeout - scb->current_timeout;
493 if (set_tty_state (scb, &state))
494 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
498 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
501 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
502 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
503 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
504 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
507 hardwire_readchar(scb, timeout)
513 if (scb->bufcnt-- > 0)
518 status = wait_for (scb, timeout);
523 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
525 if (scb->bufcnt <= 0)
527 if (scb->bufcnt == 0)
529 /* Zero characters means timeout (it could also be EOF, but
530 we don't (yet at least) distinguish). */
531 if (scb->timeout_remaining > 0)
533 timeout = scb->timeout_remaining;
537 return SERIAL_TIMEOUT;
539 else if (errno == EINTR)
542 return SERIAL_ERROR; /* Got an error from read */
546 scb->bufp = scb->buf;
559 /* Translate baud rates from integers to damn B_codes. Unix should
560 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
593 for (i = 0; baudtab[i].rate != -1; i++)
594 if (rate == baudtab[i].rate)
595 return baudtab[i].code;
601 hardwire_setbaudrate(scb, rate)
605 struct hardwire_ttystate state;
607 if (get_tty_state(scb, &state))
611 cfsetospeed (&state.termios, rate_to_code (rate));
612 cfsetispeed (&state.termios, rate_to_code (rate));
620 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
621 state.termio.c_cflag |= rate_to_code (rate);
625 state.sgttyb.sg_ispeed = rate_to_code (rate);
626 state.sgttyb.sg_ospeed = rate_to_code (rate);
629 return set_tty_state (scb, &state);
633 hardwire_setstopbits(scb, num)
637 struct hardwire_ttystate state;
640 if (get_tty_state(scb, &state))
645 case SERIAL_1_STOPBITS:
648 case SERIAL_1_AND_A_HALF_STOPBITS:
649 case SERIAL_2_STOPBITS:
658 state.termios.c_cflag &= ~CSTOPB;
660 state.termios.c_cflag |= CSTOPB; /* two bits */
665 state.termio.c_cflag &= ~CSTOPB;
667 state.termio.c_cflag |= CSTOPB; /* two bits */
671 return 0; /* sgtty doesn't support this */
674 return set_tty_state (scb, &state);
678 hardwire_write(scb, str, len)
687 cc = write(scb->fd, str, len);
708 static struct serial_ops hardwire_ops =
716 hardwire_flush_output,
717 hardwire_flush_input,
720 hardwire_get_tty_state,
721 hardwire_set_tty_state,
722 hardwire_print_tty_state,
723 hardwire_noflush_set_tty_state,
724 hardwire_setbaudrate,
725 hardwire_setstopbits,
729 _initialize_ser_hardwire ()
731 serial_add_interface (&hardwire_ops);