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;
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. */
61 struct hardwire_ttystate
66 /* Line discipline flags. */
70 /* This is only used for the ultra. Does it have pid_t? */
78 static int hardwire_open PARAMS ((serial_t scb, const char *name));
79 static void hardwire_raw PARAMS ((serial_t scb));
80 static int wait_for PARAMS ((serial_t scb, int timeout));
81 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
82 static int rate_to_code PARAMS ((int rate));
83 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
84 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
85 /* FIXME: static void hardwire_restore PARAMS ((serial_t scb)); */
86 static void hardwire_close PARAMS ((serial_t scb));
87 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
88 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
89 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
90 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
92 /* Open up a real live device for serial I/O */
95 hardwire_open(scb, name)
99 scb->fd = open (name, O_RDWR);
107 get_tty_state(scb, state)
109 struct hardwire_ttystate *state;
113 pid_t new_process_group;
115 if (tcgetattr(scb->fd, &state->termios) < 0)
121 /* Apparently, if a tty has no process group, then tcgetpgrp returns -1 with
122 errno == 0. In this case, set the process group to -1 so that we know to
123 omit resetting it later. */
124 new_process_group = tcgetpgrp (scb->fd);
125 if ((new_process_group == (pid_t)-1)
126 && (errno != ENOTTY))
129 state->process_group = new_process_group;
134 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
140 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
142 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
144 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
146 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
152 return ioctl (scb->fd, TIOCGPGRP, &state->process_group);
157 set_tty_state(scb, state)
159 struct hardwire_ttystate *state;
162 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
168 /* If the tty had no process group before, then do not reset it. */
169 if (state->process_group == -1)
172 return tcsetpgrp (scb->fd, state->process_group);
176 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
182 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
188 return ioctl (scb->fd, TIOCSPGRP, &state->process_group);
192 static serial_ttystate
193 hardwire_get_tty_state(scb)
196 struct hardwire_ttystate *state;
198 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
200 if (get_tty_state(scb, state))
203 return (serial_ttystate)state;
207 hardwire_set_tty_state(scb, ttystate)
209 serial_ttystate ttystate;
211 struct hardwire_ttystate *state;
213 state = (struct hardwire_ttystate *)ttystate;
215 return set_tty_state(scb, state);
219 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
221 serial_ttystate new_ttystate;
222 serial_ttystate old_ttystate;
224 struct hardwire_ttystate new_state;
225 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
227 new_state = *(struct hardwire_ttystate *)new_ttystate;
230 /* I'm not sure whether this is necessary; the manpage makes no mention
231 of discarding input when switching to/from ICANON. */
232 if (state->termios.c_lflag & ICANON)
233 new_state.termios.c_lflag |= ICANON;
235 new_state.termios.c_lflag &= ~ICANON;
239 /* I'm not sure whether this is necessary; the manpage makes no mention
240 of discarding input when switching to/from ICANON. */
241 if (state->termio.c_lflag & ICANON)
242 new_state.termio.c_lflag |= ICANON;
244 new_state.termio.c_lflag &= ~ICANON;
248 if (state->sgttyb.sg_flags & RAW)
249 new_state.sgttyb.sg_flags |= RAW;
251 new_state.sgttyb.sg_flags &= ~RAW;
253 /* I'm not sure whether this is necessary; the manpage just mentions
255 if (state->sgttyb.sg_flags & CBREAK)
256 new_state.sgttyb.sg_flags |= CBREAK;
258 new_state.sgttyb.sg_flags &= ~CBREAK;
261 return set_tty_state (scb, &new_state);
265 hardwire_print_tty_state (scb, ttystate)
267 serial_ttystate ttystate;
269 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
273 printf_filtered ("Process group = %d\n", state->process_group);
275 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
276 state->termios.c_iflag, state->termios.c_oflag);
277 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
278 state->termios.c_cflag, state->termios.c_lflag);
280 /* This not in POSIX, and is not really documented by those systems
281 which have it (at least not Sun). */
282 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
284 printf_filtered ("c_cc: ");
285 for (i = 0; i < NCCS; i += 1)
286 printf_filtered ("0x%x ", state->termios.c_cc[i]);
287 printf_filtered ("\n");
291 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
292 state->termio.c_iflag, state->termio.c_oflag);
293 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
294 state->termio.c_cflag, state->termio.c_lflag,
295 state->termio.c_line);
296 printf_filtered ("c_cc: ");
297 for (i = 0; i < NCC; i += 1)
298 printf_filtered ("0x%x ", state->termio.c_cc[i]);
299 printf_filtered ("\n");
303 printf_filtered ("Process group = %d\n", state->process_group);
305 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
307 printf_filtered ("tchars: ");
308 for (i = 0; i < (int)sizeof (struct tchars); i++)
309 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
310 printf_filtered ("\n");
312 printf_filtered ("ltchars: ");
313 for (i = 0; i < (int)sizeof (struct ltchars); i++)
314 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
315 printf_filtered ("\n");
317 printf_filtered ("lmode: 0x%x\n", state->lmode);
322 hardwire_flush_output (scb)
326 return tcflush (scb->fd, TCOFLUSH);
330 return ioctl (scb->fd, TCFLSH, 1);
334 /* This flushes both input and output, but we can't do better. */
335 return ioctl (scb->fd, TIOCFLUSH, 0);
340 hardwire_flush_input (scb)
344 return tcflush (scb->fd, TCIFLUSH);
348 return ioctl (scb->fd, TCFLSH, 0);
352 /* This flushes both input and output, but we can't do better. */
353 return ioctl (scb->fd, TIOCFLUSH, 0);
358 hardwire_send_break (scb)
362 return tcsendbreak (scb->fd, 0);
366 return ioctl (scb->fd, TCSBRK, 0);
372 struct timeval timeout;
374 status = ioctl (scb->fd, TIOCSBRK, 0);
376 /* Can't use usleep; it doesn't exist in BSD 4.2. */
377 /* Note that if this select() is interrupted by a signal it will not wait
378 the full length of time. I think that is OK. */
380 timeout.tv_usec = 250000;
381 select (0, 0, 0, 0, &timeout);
382 status = ioctl (scb->fd, TIOCCBRK, 0);
392 struct hardwire_ttystate state;
394 if (get_tty_state(scb, &state))
395 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
398 state.termios.c_iflag = 0;
399 state.termios.c_oflag = 0;
400 state.termios.c_lflag = 0;
401 state.termios.c_cflag &= ~(CSIZE|PARENB);
402 state.termios.c_cflag |= CS8;
403 state.termios.c_cc[VMIN] = 0;
404 state.termios.c_cc[VTIME] = 0;
408 state.termio.c_iflag = 0;
409 state.termio.c_oflag = 0;
410 state.termio.c_lflag = 0;
411 state.termio.c_cflag &= ~(CSIZE|PARENB);
412 state.termio.c_cflag |= CS8;
413 state.termio.c_cc[VMIN] = 0;
414 state.termio.c_cc[VTIME] = 0;
418 state.sgttyb.sg_flags |= RAW | ANYP;
419 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
422 scb->current_timeout = 0;
424 if (set_tty_state (scb, &state))
425 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
428 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
429 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
431 For termio{s}, we actually just setup VTIME if necessary, and let the
432 timeout occur in the read() in hardwire_read().
436 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)
478 struct hardwire_ttystate state;
480 if (get_tty_state(scb, &state))
481 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
484 state.termios.c_cc[VTIME] = timeout * 10;
488 state.termio.c_cc[VTIME] = timeout * 10;
491 scb->current_timeout = timeout;
493 if (set_tty_state (scb, &state))
494 fprintf(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)
516 status = wait_for(scb, timeout);
521 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
523 if (scb->bufcnt <= 0)
524 if (scb->bufcnt == 0)
525 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
526 distinguish between EOF & timeouts
529 return SERIAL_ERROR; /* Got an error from read */
532 scb->bufp = scb->buf;
544 /* Translate baud rates from integers to damn B_codes. Unix should
545 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
578 for (i = 0; baudtab[i].rate != -1; i++)
579 if (rate == baudtab[i].rate)
580 return baudtab[i].code;
586 hardwire_setbaudrate(scb, rate)
590 struct hardwire_ttystate state;
592 if (get_tty_state(scb, &state))
596 cfsetospeed (&state.termios, rate_to_code (rate));
597 cfsetispeed (&state.termios, rate_to_code (rate));
605 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
606 state.termio.c_cflag |= rate_to_code (rate);
610 state.sgttyb.sg_ispeed = rate_to_code (rate);
611 state.sgttyb.sg_ospeed = rate_to_code (rate);
614 return set_tty_state (scb, &state);
618 hardwire_set_process_group (scb, ttystate, group)
620 serial_ttystate ttystate;
623 #if defined (HAVE_SGTTY) || defined (HAVE_TERMIOS)
624 ((struct hardwire_ttystate *)ttystate)->process_group = group;
630 hardwire_write(scb, str, len)
639 cc = write(scb->fd, str, len);
660 static struct serial_ops hardwire_ops =
668 hardwire_flush_output,
669 hardwire_flush_input,
672 hardwire_get_tty_state,
673 hardwire_set_tty_state,
674 hardwire_print_tty_state,
675 hardwire_noflush_set_tty_state,
676 hardwire_setbaudrate,
677 hardwire_set_process_group
681 #if defined (HAVE_TERMIOS)
685 /* This is here because this is where we figure out whether we (probably)
686 have job control. Just using job_control only does part of it because
687 setpgid or setpgrp might not exist on a system without job control.
688 It might be considered misplaced (on the other hand, process groups and
689 job control are closely related to ttys).
691 For a more clean implementation, in libiberty, put a setpgid which merely
692 calls setpgrp and a setpgrp which does nothing (any system with job control
693 will have one or the other). */
700 #if defined (NEED_POSIX_SETPGID) || defined (HAVE_TERMIOS)
701 /* Do all systems with termios have setpgid? I hope so. */
702 /* setpgid (0, 0) is supposed to work and mean the same thing as
703 this, but on Ultrix 4.2A it fails with EPERM (and
704 setpgid (getpid (), getpid ()) succeeds). */
705 retval = setpgid (getpid (), getpid ());
707 #if defined (TIOCGPGRP)
708 #if defined(USG) && !defined(SETPGRP_ARGS)
711 retval = setpgrp (getpid (), getpid ());
713 #endif /* TIOCGPGRP. */
714 #endif /* NEED_POSIX_SETPGID */
720 _initialize_ser_hardwire ()
722 serial_add_interface (&hardwire_ops);
724 /* OK, figure out whether we have job control. */
726 #if defined (HAVE_TERMIOS)
727 /* Do all systems with termios have the POSIX way of identifying job
728 control? I hope so. */
729 #ifdef _POSIX_JOB_CONTROL
732 job_control = sysconf (_SC_JOB_CONTROL);
737 /* See comment at top of file about trying to support process groups
747 #endif /* TIOCGPGRP */