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 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;
112 pid_t new_process_group;
114 if (tcgetattr(scb->fd, &state->termios) < 0)
120 new_process_group = tcgetpgrp (scb->fd);
121 if (new_process_group == (pid_t)-1)
123 state->process_group = new_process_group;
128 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
134 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
136 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
138 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
140 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
146 return ioctl (scb->fd, TIOCGPGRP, &state->process_group);
151 set_tty_state(scb, state)
153 struct hardwire_ttystate *state;
156 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
162 return tcsetpgrp (scb->fd, state->process_group);
166 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
172 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
178 return ioctl (scb->fd, TIOCSPGRP, &state->process_group);
182 static serial_ttystate
183 hardwire_get_tty_state(scb)
186 struct hardwire_ttystate *state;
188 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
190 if (get_tty_state(scb, state))
193 return (serial_ttystate)state;
197 hardwire_set_tty_state(scb, ttystate)
199 serial_ttystate ttystate;
201 struct hardwire_ttystate *state;
203 state = (struct hardwire_ttystate *)ttystate;
205 return set_tty_state(scb, state);
209 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
211 serial_ttystate new_ttystate;
212 serial_ttystate old_ttystate;
214 struct hardwire_ttystate new_state;
215 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
217 new_state = *(struct hardwire_ttystate *)new_ttystate;
220 /* I'm not sure whether this is necessary; the manpage makes no mention
221 of discarding input when switching to/from ICANON. */
222 if (state->termios.c_lflag & ICANON)
223 new_state.termios.c_lflag |= ICANON;
225 new_state.termios.c_lflag &= ~ICANON;
229 /* I'm not sure whether this is necessary; the manpage makes no mention
230 of discarding input when switching to/from ICANON. */
231 if (state->termio.c_lflag & ICANON)
232 new_state.termio.c_lflag |= ICANON;
234 new_state.termio.c_lflag &= ~ICANON;
238 if (state->sgttyb.sg_flags & RAW)
239 new_state.sgttyb.sg_flags |= RAW;
241 new_state.sgttyb.sg_flags &= ~RAW;
243 /* I'm not sure whether this is necessary; the manpage just mentions
245 if (state->sgttyb.sg_flags & CBREAK)
246 new_state.sgttyb.sg_flags |= CBREAK;
248 new_state.sgttyb.sg_flags &= ~CBREAK;
251 return set_tty_state (scb, &new_state);
255 hardwire_print_tty_state (scb, ttystate)
257 serial_ttystate ttystate;
259 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
263 printf_filtered ("Process group = %d\n", state->process_group);
265 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
266 state->termios.c_iflag, state->termios.c_oflag);
267 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
268 state->termios.c_cflag, state->termios.c_lflag);
270 /* This not in POSIX, and is not really documented by those systems
271 which have it (at least not Sun). */
272 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
274 printf_filtered ("c_cc: ");
275 for (i = 0; i < NCCS; i += 1)
276 printf_filtered ("0x%x ", state->termios.c_cc[i]);
277 printf_filtered ("\n");
281 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
282 state->termio.c_iflag, state->termio.c_oflag);
283 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
284 state->termio.c_cflag, state->termio.c_lflag,
285 state->termio.c_line);
286 printf_filtered ("c_cc: ");
287 for (i = 0; i < NCC; i += 1)
288 printf_filtered ("0x%x ", state->termio.c_cc[i]);
289 printf_filtered ("\n");
293 printf_filtered ("Process group = %d\n", state->process_group);
295 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
297 printf_filtered ("tchars: ");
298 for (i = 0; i < (int)sizeof (struct tchars); i++)
299 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
300 printf_filtered ("\n");
302 printf_filtered ("ltchars: ");
303 for (i = 0; i < (int)sizeof (struct ltchars); i++)
304 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
305 printf_filtered ("\n");
307 printf_filtered ("lmode: 0x%x\n", state->lmode);
312 hardwire_flush_output (scb)
316 return tcflush (scb->fd, TCOFLUSH);
320 return ioctl (scb->fd, TCFLSH, 1);
324 /* This flushes both input and output, but we can't do better. */
325 return ioctl (scb->fd, TIOCFLUSH, 0);
330 hardwire_flush_input (scb)
334 return tcflush (scb->fd, TCIFLUSH);
338 return ioctl (scb->fd, TCFLSH, 0);
342 /* This flushes both input and output, but we can't do better. */
343 return ioctl (scb->fd, TIOCFLUSH, 0);
348 hardwire_send_break (scb)
354 return tcsendbreak (scb->fd, 0);
358 return ioctl (scb->fd, TCSBRK, 0);
363 struct timeval timeout;
365 status = ioctl (scb->fd, TIOCSBRK, 0);
367 /* Can't use usleep; it doesn't exist in BSD 4.2. */
368 /* Note that if this select() is interrupted by a signal it will not wait
369 the full length of time. I think that is OK. */
371 timeout.tv_usec = 250000;
372 select (0, 0, 0, 0, &timeout);
373 status = ioctl (scb->fd, TIOCCBRK, 0);
383 struct hardwire_ttystate state;
385 if (get_tty_state(scb, &state))
386 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
389 state.termios.c_iflag = 0;
390 state.termios.c_oflag = 0;
391 state.termios.c_lflag = 0;
392 state.termios.c_cflag &= ~(CSIZE|PARENB);
393 state.termios.c_cflag |= CS8;
394 state.termios.c_cc[VMIN] = 0;
395 state.termios.c_cc[VTIME] = 0;
399 state.termio.c_iflag = 0;
400 state.termio.c_oflag = 0;
401 state.termio.c_lflag = 0;
402 state.termio.c_cflag &= ~(CSIZE|PARENB);
403 state.termio.c_cflag |= CS8;
404 state.termio.c_cc[VMIN] = 0;
405 state.termio.c_cc[VTIME] = 0;
409 state.sgttyb.sg_flags |= RAW | ANYP;
410 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
413 scb->current_timeout = 0;
415 if (set_tty_state (scb, &state))
416 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
419 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
420 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
422 For termio{s}, we actually just setup VTIME if necessary, and let the
423 timeout occur in the read() in hardwire_read().
427 wait_for(scb, timeout)
442 FD_SET(scb->fd, &readfds);
447 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
449 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
453 return SERIAL_TIMEOUT;
454 else if (errno == EINTR)
457 return SERIAL_ERROR; /* Got an error from select or poll */
462 #endif /* HAVE_SGTTY */
464 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
465 if (timeout == scb->current_timeout)
469 struct hardwire_ttystate state;
471 if (get_tty_state(scb, &state))
472 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
475 state.termios.c_cc[VTIME] = timeout * 10;
479 state.termio.c_cc[VTIME] = timeout * 10;
482 scb->current_timeout = timeout;
484 if (set_tty_state (scb, &state))
485 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
489 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
492 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
493 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
494 char if successful. Returns -2 if timeout expired, EOF if line dropped
495 dead, or -3 for any other error (see errno in that case). */
498 hardwire_readchar(scb, timeout)
504 if (scb->bufcnt-- > 0)
507 status = wait_for(scb, timeout);
512 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
514 if (scb->bufcnt <= 0)
515 if (scb->bufcnt == 0)
516 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
517 distinguish between EOF & timeouts
520 return SERIAL_ERROR; /* Got an error from read */
523 scb->bufp = scb->buf;
535 /* Translate baud rates from integers to damn B_codes. Unix should
536 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
569 for (i = 0; baudtab[i].rate != -1; i++)
570 if (rate == baudtab[i].rate)
571 return baudtab[i].code;
577 hardwire_setbaudrate(scb, rate)
581 struct hardwire_ttystate state;
583 if (get_tty_state(scb, &state))
587 cfsetospeed (&state.termios, rate_to_code (rate));
588 cfsetispeed (&state.termios, rate_to_code (rate));
596 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
597 state.termio.c_cflag |= rate_to_code (rate);
601 state.sgttyb.sg_ispeed = rate_to_code (rate);
602 state.sgttyb.sg_ospeed = rate_to_code (rate);
605 return set_tty_state (scb, &state);
609 hardwire_set_process_group (scb, ttystate, group)
611 serial_ttystate ttystate;
614 #if defined (HAVE_SGTTY) || defined (HAVE_TERMIOS)
615 ((struct hardwire_ttystate *)ttystate)->process_group = group;
621 hardwire_write(scb, str, len)
630 cc = write(scb->fd, str, len);
651 static struct serial_ops hardwire_ops =
659 hardwire_flush_output,
660 hardwire_flush_input,
663 hardwire_get_tty_state,
664 hardwire_set_tty_state,
665 hardwire_print_tty_state,
666 hardwire_noflush_set_tty_state,
667 hardwire_setbaudrate,
668 hardwire_set_process_group
672 #if defined (HAVE_TERMIOS)
676 /* This is here because this is where we figure out whether we (probably)
677 have job control. Just using job_control only does part of it because
678 setpgid or setpgrp might not exist on a system without job control.
679 It might be considered misplaced (on the other hand, process groups and
680 job control are closely related to ttys).
682 For a more clean implementation, in libiberty, put a setpgid which merely
683 calls setpgrp and a setpgrp which does nothing (any system with job control
684 will have one or the other). */
691 #if defined (NEED_POSIX_SETPGID) || defined (HAVE_TERMIOS)
692 /* Do all systems with termios have setpgid? I hope so. */
693 /* setpgid (0, 0) is supposed to work and mean the same thing as
694 this, but on Ultrix 4.2A it fails with EPERM (and
695 setpgid (getpid (), getpid ()) succeeds). */
696 retval = setpgid (getpid (), getpid ());
698 #if defined (TIOCGPGRP)
699 #if defined(USG) && !defined(SETPGRP_ARGS)
702 retval = setpgrp (getpid (), getpid ());
704 #endif /* TIOCGPGRP. */
705 #endif /* NEED_POSIX_SETPGID */
711 _initialize_ser_hardwire ()
713 serial_add_interface (&hardwire_ops);
715 /* OK, figure out whether we have job control. */
717 #if defined (HAVE_TERMIOS)
718 /* Do all systems with termios have the POSIX way of identifying job
719 control? I hope so. */
720 #ifdef _POSIX_JOB_CONTROL
723 job_control = sysconf (_SC_JOB_CONTROL);
728 /* See comment at top of file about trying to support process groups
738 #endif /* TIOCGPGRP */