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)
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;
197 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
199 new_state = *(struct hardwire_ttystate *)new_ttystate;
202 /* I'm not sure whether this is necessary; the manpage makes no mention
203 of discarding input when switching to/from ICANON. */
204 if (state->termios.c_lflag & ICANON)
205 new_state.termios.c_lflag |= ICANON;
207 new_state.termios.c_lflag &= ~ICANON;
211 /* I'm not sure whether this is necessary; the manpage makes no mention
212 of discarding input when switching to/from ICANON. */
213 if (state->termio.c_lflag & ICANON)
214 new_state.termio.c_lflag |= ICANON;
216 new_state.termio.c_lflag &= ~ICANON;
220 if (state->sgttyb.sg_flags & RAW)
221 new_state.sgttyb.sg_flags |= RAW;
223 new_state.sgttyb.sg_flags &= ~RAW;
225 /* I'm not sure whether this is necessary; the manpage just mentions
227 if (state->sgttyb.sg_flags & CBREAK)
228 new_state.sgttyb.sg_flags |= CBREAK;
230 new_state.sgttyb.sg_flags &= ~CBREAK;
233 return set_tty_state (scb, &new_state);
237 hardwire_print_tty_state (scb, ttystate)
239 serial_ttystate ttystate;
241 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
245 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
246 state->termios.c_iflag, state->termios.c_oflag);
247 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
248 state->termios.c_cflag, state->termios.c_lflag);
250 /* This not in POSIX, and is not really documented by those systems
251 which have it (at least not Sun). */
252 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
254 printf_filtered ("c_cc: ");
255 for (i = 0; i < NCCS; i += 1)
256 printf_filtered ("0x%x ", state->termios.c_cc[i]);
257 printf_filtered ("\n");
261 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
262 state->termio.c_iflag, state->termio.c_oflag);
263 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
264 state->termio.c_cflag, state->termio.c_lflag,
265 state->termio.c_line);
266 printf_filtered ("c_cc: ");
267 for (i = 0; i < NCC; i += 1)
268 printf_filtered ("0x%x ", state->termio.c_cc[i]);
269 printf_filtered ("\n");
273 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
275 printf_filtered ("tchars: ");
276 for (i = 0; i < (int)sizeof (struct tchars); i++)
277 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
278 printf_filtered ("\n");
280 printf_filtered ("ltchars: ");
281 for (i = 0; i < (int)sizeof (struct ltchars); i++)
282 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
283 printf_filtered ("\n");
285 printf_filtered ("lmode: 0x%x\n", state->lmode);
290 hardwire_flush_output (scb)
294 return tcflush (scb->fd, TCOFLUSH);
298 return ioctl (scb->fd, TCFLSH, 1);
302 /* This flushes both input and output, but we can't do better. */
303 return ioctl (scb->fd, TIOCFLUSH, 0);
308 hardwire_flush_input (scb)
312 return tcflush (scb->fd, TCIFLUSH);
316 return ioctl (scb->fd, TCFLSH, 0);
320 /* This flushes both input and output, but we can't do better. */
321 return ioctl (scb->fd, TIOCFLUSH, 0);
326 hardwire_send_break (scb)
330 return tcsendbreak (scb->fd, 0);
334 return ioctl (scb->fd, TCSBRK, 0);
340 struct timeval timeout;
342 status = ioctl (scb->fd, TIOCSBRK, 0);
344 /* Can't use usleep; it doesn't exist in BSD 4.2. */
345 /* Note that if this select() is interrupted by a signal it will not wait
346 the full length of time. I think that is OK. */
348 timeout.tv_usec = 250000;
349 select (0, 0, 0, 0, &timeout);
350 status = ioctl (scb->fd, TIOCCBRK, 0);
360 struct hardwire_ttystate state;
362 if (get_tty_state(scb, &state))
363 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
366 state.termios.c_iflag = 0;
367 state.termios.c_oflag = 0;
368 state.termios.c_lflag = 0;
369 state.termios.c_cflag &= ~(CSIZE|PARENB);
370 state.termios.c_cflag |= CS8;
371 state.termios.c_cc[VMIN] = 0;
372 state.termios.c_cc[VTIME] = 0;
376 state.termio.c_iflag = 0;
377 state.termio.c_oflag = 0;
378 state.termio.c_lflag = 0;
379 state.termio.c_cflag &= ~(CSIZE|PARENB);
380 state.termio.c_cflag |= CS8;
381 state.termio.c_cc[VMIN] = 0;
382 state.termio.c_cc[VTIME] = 0;
386 state.sgttyb.sg_flags |= RAW | ANYP;
387 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
390 scb->current_timeout = 0;
392 if (set_tty_state (scb, &state))
393 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
396 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
397 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
399 For termio{s}, we actually just setup VTIME if necessary, and let the
400 timeout occur in the read() in hardwire_read().
404 wait_for(scb, timeout)
417 FD_SET(scb->fd, &readfds);
424 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
426 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
430 return SERIAL_TIMEOUT;
431 else if (errno == EINTR)
434 return SERIAL_ERROR; /* Got an error from select or poll */
439 #endif /* HAVE_SGTTY */
441 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
442 if (timeout == scb->current_timeout)
446 struct hardwire_ttystate state;
448 if (get_tty_state(scb, &state))
449 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
452 state.termios.c_cc[VTIME] = timeout * 10;
456 state.termio.c_cc[VTIME] = timeout * 10;
459 scb->current_timeout = timeout;
461 if (set_tty_state (scb, &state))
462 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
466 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
469 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
470 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
471 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
472 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
475 hardwire_readchar(scb, timeout)
481 if (scb->bufcnt-- > 0)
484 status = wait_for(scb, timeout);
489 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
491 if (scb->bufcnt <= 0)
492 if (scb->bufcnt == 0)
493 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
494 distinguish between EOF & timeouts
497 return SERIAL_ERROR; /* Got an error from read */
500 scb->bufp = scb->buf;
512 /* Translate baud rates from integers to damn B_codes. Unix should
513 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
546 for (i = 0; baudtab[i].rate != -1; i++)
547 if (rate == baudtab[i].rate)
548 return baudtab[i].code;
554 hardwire_setbaudrate(scb, rate)
558 struct hardwire_ttystate state;
560 if (get_tty_state(scb, &state))
564 cfsetospeed (&state.termios, rate_to_code (rate));
565 cfsetispeed (&state.termios, rate_to_code (rate));
573 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
574 state.termio.c_cflag |= rate_to_code (rate);
578 state.sgttyb.sg_ispeed = rate_to_code (rate);
579 state.sgttyb.sg_ospeed = rate_to_code (rate);
582 return set_tty_state (scb, &state);
586 hardwire_write(scb, str, len)
595 cc = write(scb->fd, str, len);
616 static struct serial_ops hardwire_ops =
624 hardwire_flush_output,
625 hardwire_flush_input,
628 hardwire_get_tty_state,
629 hardwire_set_tty_state,
630 hardwire_print_tty_state,
631 hardwire_noflush_set_tty_state,
632 hardwire_setbaudrate,
636 _initialize_ser_hardwire ()
638 serial_add_interface (&hardwire_ops);