1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993 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., 675 Mass Ave, Cambridge, MA 02139, 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)
158 static serial_ttystate
159 hardwire_get_tty_state(scb)
162 struct hardwire_ttystate *state;
164 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
166 if (get_tty_state(scb, state))
169 return (serial_ttystate)state;
173 hardwire_set_tty_state(scb, ttystate)
175 serial_ttystate ttystate;
177 struct hardwire_ttystate *state;
179 state = (struct hardwire_ttystate *)ttystate;
181 return set_tty_state(scb, state);
185 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
187 serial_ttystate new_ttystate;
188 serial_ttystate old_ttystate;
190 struct hardwire_ttystate new_state;
191 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
193 new_state = *(struct hardwire_ttystate *)new_ttystate;
196 /* I'm not sure whether this is necessary; the manpage makes no mention
197 of discarding input when switching to/from ICANON. */
198 if (state->termios.c_lflag & ICANON)
199 new_state.termios.c_lflag |= ICANON;
201 new_state.termios.c_lflag &= ~ICANON;
205 /* I'm not sure whether this is necessary; the manpage makes no mention
206 of discarding input when switching to/from ICANON. */
207 if (state->termio.c_lflag & ICANON)
208 new_state.termio.c_lflag |= ICANON;
210 new_state.termio.c_lflag &= ~ICANON;
214 if (state->sgttyb.sg_flags & RAW)
215 new_state.sgttyb.sg_flags |= RAW;
217 new_state.sgttyb.sg_flags &= ~RAW;
219 /* I'm not sure whether this is necessary; the manpage just mentions
221 if (state->sgttyb.sg_flags & CBREAK)
222 new_state.sgttyb.sg_flags |= CBREAK;
224 new_state.sgttyb.sg_flags &= ~CBREAK;
227 return set_tty_state (scb, &new_state);
231 hardwire_print_tty_state (scb, ttystate)
233 serial_ttystate ttystate;
235 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
239 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
240 state->termios.c_iflag, state->termios.c_oflag);
241 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
242 state->termios.c_cflag, state->termios.c_lflag);
244 /* This not in POSIX, and is not really documented by those systems
245 which have it (at least not Sun). */
246 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
248 printf_filtered ("c_cc: ");
249 for (i = 0; i < NCCS; i += 1)
250 printf_filtered ("0x%x ", state->termios.c_cc[i]);
251 printf_filtered ("\n");
255 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
256 state->termio.c_iflag, state->termio.c_oflag);
257 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
258 state->termio.c_cflag, state->termio.c_lflag,
259 state->termio.c_line);
260 printf_filtered ("c_cc: ");
261 for (i = 0; i < NCC; i += 1)
262 printf_filtered ("0x%x ", state->termio.c_cc[i]);
263 printf_filtered ("\n");
267 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
269 printf_filtered ("tchars: ");
270 for (i = 0; i < (int)sizeof (struct tchars); i++)
271 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
272 printf_filtered ("\n");
274 printf_filtered ("ltchars: ");
275 for (i = 0; i < (int)sizeof (struct ltchars); i++)
276 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
277 printf_filtered ("\n");
279 printf_filtered ("lmode: 0x%x\n", state->lmode);
284 hardwire_flush_output (scb)
288 return tcflush (scb->fd, TCOFLUSH);
292 return ioctl (scb->fd, TCFLSH, 1);
296 /* This flushes both input and output, but we can't do better. */
297 return ioctl (scb->fd, TIOCFLUSH, 0);
302 hardwire_flush_input (scb)
306 return tcflush (scb->fd, TCIFLUSH);
310 return ioctl (scb->fd, TCFLSH, 0);
314 /* This flushes both input and output, but we can't do better. */
315 return ioctl (scb->fd, TIOCFLUSH, 0);
320 hardwire_send_break (scb)
324 return tcsendbreak (scb->fd, 0);
328 return ioctl (scb->fd, TCSBRK, 0);
334 struct timeval timeout;
336 status = ioctl (scb->fd, TIOCSBRK, 0);
338 /* Can't use usleep; it doesn't exist in BSD 4.2. */
339 /* Note that if this select() is interrupted by a signal it will not wait
340 the full length of time. I think that is OK. */
342 timeout.tv_usec = 250000;
343 select (0, 0, 0, 0, &timeout);
344 status = ioctl (scb->fd, TIOCCBRK, 0);
354 struct hardwire_ttystate state;
356 if (get_tty_state(scb, &state))
357 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
360 state.termios.c_iflag = 0;
361 state.termios.c_oflag = 0;
362 state.termios.c_lflag = 0;
363 state.termios.c_cflag &= ~(CSIZE|PARENB);
364 state.termios.c_cflag |= CS8;
365 state.termios.c_cc[VMIN] = 0;
366 state.termios.c_cc[VTIME] = 0;
370 state.termio.c_iflag = 0;
371 state.termio.c_oflag = 0;
372 state.termio.c_lflag = 0;
373 state.termio.c_cflag &= ~(CSIZE|PARENB);
374 state.termio.c_cflag |= CS8;
375 state.termio.c_cc[VMIN] = 0;
376 state.termio.c_cc[VTIME] = 0;
380 state.sgttyb.sg_flags |= RAW | ANYP;
381 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
384 scb->current_timeout = 0;
386 if (set_tty_state (scb, &state))
387 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
390 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
391 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
393 For termio{s}, we actually just setup VTIME if necessary, and let the
394 timeout occur in the read() in hardwire_read().
398 wait_for(scb, timeout)
411 FD_SET(scb->fd, &readfds);
418 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
420 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
424 return SERIAL_TIMEOUT;
425 else if (errno == EINTR)
428 return SERIAL_ERROR; /* Got an error from select or poll */
433 #endif /* HAVE_SGTTY */
435 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
436 if (timeout == scb->current_timeout)
440 struct hardwire_ttystate state;
442 if (get_tty_state(scb, &state))
443 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
446 state.termios.c_cc[VTIME] = timeout * 10;
450 state.termio.c_cc[VTIME] = timeout * 10;
453 scb->current_timeout = timeout;
455 if (set_tty_state (scb, &state))
456 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
460 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
463 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
464 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
465 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
466 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
469 hardwire_readchar(scb, timeout)
475 if (scb->bufcnt-- > 0)
478 status = wait_for(scb, timeout);
483 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
485 if (scb->bufcnt <= 0)
486 if (scb->bufcnt == 0)
487 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
488 distinguish between EOF & timeouts
491 return SERIAL_ERROR; /* Got an error from read */
494 scb->bufp = scb->buf;
506 /* Translate baud rates from integers to damn B_codes. Unix should
507 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
540 for (i = 0; baudtab[i].rate != -1; i++)
541 if (rate == baudtab[i].rate)
542 return baudtab[i].code;
548 hardwire_setbaudrate(scb, rate)
552 struct hardwire_ttystate state;
554 if (get_tty_state(scb, &state))
558 cfsetospeed (&state.termios, rate_to_code (rate));
559 cfsetispeed (&state.termios, rate_to_code (rate));
567 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
568 state.termio.c_cflag |= rate_to_code (rate);
572 state.sgttyb.sg_ispeed = rate_to_code (rate);
573 state.sgttyb.sg_ospeed = rate_to_code (rate);
576 return set_tty_state (scb, &state);
580 hardwire_write(scb, str, len)
589 cc = write(scb->fd, str, len);
610 static struct serial_ops hardwire_ops =
618 hardwire_flush_output,
619 hardwire_flush_input,
622 hardwire_get_tty_state,
623 hardwire_set_tty_state,
624 hardwire_print_tty_state,
625 hardwire_noflush_set_tty_state,
626 hardwire_setbaudrate,
630 _initialize_ser_hardwire ()
632 serial_add_interface (&hardwire_ops);