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>
31 struct hardwire_ttystate
33 struct termios termios;
39 /* It is believed that all systems which have added job control to SVR3
40 (e.g. sco) have also added termios. Even if not, trying to figure out
41 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
42 bewildering. So we don't attempt it. */
44 struct hardwire_ttystate
51 /* Needed for the code which uses select(). We would include <sys/select.h>
52 too if it existed on all systems. */
55 struct hardwire_ttystate
60 /* Line discipline flags. */
65 static int hardwire_open PARAMS ((serial_t scb, const char *name));
66 static void hardwire_raw PARAMS ((serial_t scb));
67 static int wait_for PARAMS ((serial_t scb, int timeout));
68 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
69 static int rate_to_code PARAMS ((int rate));
70 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
71 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
72 static void hardwire_close PARAMS ((serial_t scb));
73 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
74 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
75 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
76 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
77 static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
79 static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate));
80 static int hardwire_flush_output PARAMS ((serial_t));
81 static int hardwire_flush_input PARAMS ((serial_t));
82 static int hardwire_send_break PARAMS ((serial_t));
83 static int hardwire_setstopbits PARAMS ((serial_t, int));
85 /* Open up a real live device for serial I/O */
88 hardwire_open(scb, name)
92 scb->fd = open (name, O_RDWR);
100 get_tty_state(scb, state)
102 struct hardwire_ttystate *state;
107 if (tcgetattr(scb->fd, &state->termios) < 0)
114 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
120 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
122 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
124 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
126 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
134 set_tty_state(scb, state)
136 struct hardwire_ttystate *state;
139 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
146 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
152 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
154 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
156 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
158 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
165 static serial_ttystate
166 hardwire_get_tty_state(scb)
169 struct hardwire_ttystate *state;
171 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
173 if (get_tty_state(scb, state))
176 return (serial_ttystate)state;
180 hardwire_set_tty_state(scb, ttystate)
182 serial_ttystate ttystate;
184 struct hardwire_ttystate *state;
186 state = (struct hardwire_ttystate *)ttystate;
188 return set_tty_state(scb, state);
192 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
194 serial_ttystate new_ttystate;
195 serial_ttystate old_ttystate;
197 struct hardwire_ttystate new_state;
199 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
202 new_state = *(struct hardwire_ttystate *)new_ttystate;
204 /* Don't change in or out of raw mode; we don't want to flush input.
205 termio and termios have no such restriction; for them flushing input
206 is separate from setting the attributes. */
209 if (state->sgttyb.sg_flags & RAW)
210 new_state.sgttyb.sg_flags |= RAW;
212 new_state.sgttyb.sg_flags &= ~RAW;
214 /* I'm not sure whether this is necessary; the manpage just mentions
216 if (state->sgttyb.sg_flags & CBREAK)
217 new_state.sgttyb.sg_flags |= CBREAK;
219 new_state.sgttyb.sg_flags &= ~CBREAK;
222 return set_tty_state (scb, &new_state);
226 hardwire_print_tty_state (scb, ttystate)
228 serial_ttystate ttystate;
230 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
234 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
235 state->termios.c_iflag, state->termios.c_oflag);
236 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
237 state->termios.c_cflag, state->termios.c_lflag);
239 /* This not in POSIX, and is not really documented by those systems
240 which have it (at least not Sun). */
241 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
243 printf_filtered ("c_cc: ");
244 for (i = 0; i < NCCS; i += 1)
245 printf_filtered ("0x%x ", state->termios.c_cc[i]);
246 printf_filtered ("\n");
250 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
251 state->termio.c_iflag, state->termio.c_oflag);
252 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
253 state->termio.c_cflag, state->termio.c_lflag,
254 state->termio.c_line);
255 printf_filtered ("c_cc: ");
256 for (i = 0; i < NCC; i += 1)
257 printf_filtered ("0x%x ", state->termio.c_cc[i]);
258 printf_filtered ("\n");
262 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
264 printf_filtered ("tchars: ");
265 for (i = 0; i < (int)sizeof (struct tchars); i++)
266 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
267 printf_filtered ("\n");
269 printf_filtered ("ltchars: ");
270 for (i = 0; i < (int)sizeof (struct ltchars); i++)
271 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
272 printf_filtered ("\n");
274 printf_filtered ("lmode: 0x%x\n", state->lmode);
279 hardwire_flush_output (scb)
283 return tcflush (scb->fd, TCOFLUSH);
287 return ioctl (scb->fd, TCFLSH, 1);
291 /* This flushes both input and output, but we can't do better. */
292 return ioctl (scb->fd, TIOCFLUSH, 0);
297 hardwire_flush_input (scb)
301 scb->bufp = scb->buf;
304 return tcflush (scb->fd, TCIFLUSH);
308 return ioctl (scb->fd, TCFLSH, 0);
312 /* This flushes both input and output, but we can't do better. */
313 return ioctl (scb->fd, TIOCFLUSH, 0);
318 hardwire_send_break (scb)
322 return tcsendbreak (scb->fd, 0);
326 return ioctl (scb->fd, TCSBRK, 0);
332 struct timeval timeout;
334 status = ioctl (scb->fd, TIOCSBRK, 0);
336 /* Can't use usleep; it doesn't exist in BSD 4.2. */
337 /* Note that if this select() is interrupted by a signal it will not wait
338 the full length of time. I think that is OK. */
340 timeout.tv_usec = 250000;
341 select (0, 0, 0, 0, &timeout);
342 status = ioctl (scb->fd, TIOCCBRK, 0);
352 struct hardwire_ttystate state;
354 if (get_tty_state(scb, &state))
355 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
358 state.termios.c_iflag = 0;
359 state.termios.c_oflag = 0;
360 state.termios.c_lflag = 0;
361 state.termios.c_cflag &= ~(CSIZE|PARENB);
362 state.termios.c_cflag |= CLOCAL | CS8;
363 state.termios.c_cc[VMIN] = 0;
364 state.termios.c_cc[VTIME] = 0;
368 state.termio.c_iflag = 0;
369 state.termio.c_oflag = 0;
370 state.termio.c_lflag = 0;
371 state.termio.c_cflag &= ~(CSIZE|PARENB);
372 state.termio.c_cflag |= CLOCAL | CS8;
373 state.termio.c_cc[VMIN] = 0;
374 state.termio.c_cc[VTIME] = 0;
378 state.sgttyb.sg_flags |= RAW | ANYP;
379 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
382 scb->current_timeout = 0;
384 if (set_tty_state (scb, &state))
385 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
388 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
389 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
391 For termio{s}, we actually just setup VTIME if necessary, and let the
392 timeout occur in the read() in hardwire_read().
396 wait_for(scb, timeout)
400 scb->timeout_remaining = 0;
412 FD_SET(scb->fd, &readfds);
419 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
421 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
425 return SERIAL_TIMEOUT;
426 else if (errno == EINTR)
429 return SERIAL_ERROR; /* Got an error from select or poll */
434 #endif /* HAVE_SGTTY */
436 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
437 if (timeout == scb->current_timeout)
440 scb->current_timeout = timeout;
443 struct hardwire_ttystate state;
445 if (get_tty_state(scb, &state))
446 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
452 state.termios.c_cc[VTIME] = 0;
453 state.termios.c_cc[VMIN] = 1;
457 state.termios.c_cc[VMIN] = 0;
458 state.termios.c_cc[VTIME] = timeout * 10;
459 if (state.termios.c_cc[VTIME] != timeout * 10)
462 /* If c_cc is an 8-bit signed character, we can't go
463 bigger than this. If it is always unsigned, we could use
466 scb->current_timeout = 12;
467 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
468 scb->timeout_remaining = timeout - scb->current_timeout;
477 state.termio.c_cc[VTIME] = 0;
478 state.termio.c_cc[VMIN] = 1;
482 state.termio.c_cc[VMIN] = 0;
483 state.termio.c_cc[VTIME] = timeout * 10;
484 if (state.termio.c_cc[VTIME] != timeout * 10)
486 /* If c_cc is an 8-bit signed character, we can't go
487 bigger than this. If it is always unsigned, we could use
490 scb->current_timeout = 12;
491 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
492 scb->timeout_remaining = timeout - scb->current_timeout;
497 if (set_tty_state (scb, &state))
498 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
502 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
505 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
506 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
507 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
508 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
511 hardwire_readchar(scb, timeout)
517 if (scb->bufcnt-- > 0)
522 status = wait_for (scb, timeout);
527 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
529 if (scb->bufcnt <= 0)
531 if (scb->bufcnt == 0)
533 /* Zero characters means timeout (it could also be EOF, but
534 we don't (yet at least) distinguish). */
535 if (scb->timeout_remaining > 0)
537 timeout = scb->timeout_remaining;
541 return SERIAL_TIMEOUT;
543 else if (errno == EINTR)
546 return SERIAL_ERROR; /* Got an error from read */
550 scb->bufp = scb->buf;
563 /* Translate baud rates from integers to damn B_codes. Unix should
564 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
597 for (i = 0; baudtab[i].rate != -1; i++)
598 if (rate == baudtab[i].rate)
599 return baudtab[i].code;
605 hardwire_setbaudrate(scb, rate)
609 struct hardwire_ttystate state;
611 if (get_tty_state(scb, &state))
615 cfsetospeed (&state.termios, rate_to_code (rate));
616 cfsetispeed (&state.termios, rate_to_code (rate));
624 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
625 state.termio.c_cflag |= rate_to_code (rate);
629 state.sgttyb.sg_ispeed = rate_to_code (rate);
630 state.sgttyb.sg_ospeed = rate_to_code (rate);
633 return set_tty_state (scb, &state);
637 hardwire_setstopbits(scb, num)
641 struct hardwire_ttystate state;
644 if (get_tty_state(scb, &state))
649 case SERIAL_1_STOPBITS:
652 case SERIAL_1_AND_A_HALF_STOPBITS:
653 case SERIAL_2_STOPBITS:
662 state.termios.c_cflag &= ~CSTOPB;
664 state.termios.c_cflag |= CSTOPB; /* two bits */
669 state.termio.c_cflag &= ~CSTOPB;
671 state.termio.c_cflag |= CSTOPB; /* two bits */
675 return 0; /* sgtty doesn't support this */
678 return set_tty_state (scb, &state);
682 hardwire_write(scb, str, len)
691 cc = write(scb->fd, str, len);
712 static struct serial_ops hardwire_ops =
720 hardwire_flush_output,
721 hardwire_flush_input,
724 hardwire_get_tty_state,
725 hardwire_set_tty_state,
726 hardwire_print_tty_state,
727 hardwire_noflush_set_tty_state,
728 hardwire_setbaudrate,
729 hardwire_setstopbits,
733 _initialize_ser_hardwire ()
735 serial_add_interface (&hardwire_ops);