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
;
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
)
364 return tcsendbreak (scb
->fd
, 0);
368 return ioctl (scb
->fd
, TCSBRK
, 0);
373 struct timeval timeout
;
375 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
377 /* Can't use usleep; it doesn't exist in BSD 4.2. */
378 /* Note that if this select() is interrupted by a signal it will not wait
379 the full length of time. I think that is OK. */
381 timeout
.tv_usec
= 250000;
382 select (0, 0, 0, 0, &timeout
);
383 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
393 struct hardwire_ttystate state
;
395 if (get_tty_state(scb
, &state
))
396 fprintf(stderr
, "get_tty_state failed: %s\n", safe_strerror(errno
));
399 state
.termios
.c_iflag
= 0;
400 state
.termios
.c_oflag
= 0;
401 state
.termios
.c_lflag
= 0;
402 state
.termios
.c_cflag
&= ~(CSIZE
|PARENB
);
403 state
.termios
.c_cflag
|= CS8
;
404 state
.termios
.c_cc
[VMIN
] = 0;
405 state
.termios
.c_cc
[VTIME
] = 0;
409 state
.termio
.c_iflag
= 0;
410 state
.termio
.c_oflag
= 0;
411 state
.termio
.c_lflag
= 0;
412 state
.termio
.c_cflag
&= ~(CSIZE
|PARENB
);
413 state
.termio
.c_cflag
|= CS8
;
414 state
.termio
.c_cc
[VMIN
] = 0;
415 state
.termio
.c_cc
[VTIME
] = 0;
419 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
420 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
423 scb
->current_timeout
= 0;
425 if (set_tty_state (scb
, &state
))
426 fprintf(stderr
, "set_tty_state failed: %s\n", safe_strerror(errno
));
429 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
430 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
432 For termio{s}, we actually just setup VTIME if necessary, and let the
433 timeout occur in the read() in hardwire_read().
437 wait_for(scb
, timeout
)
452 FD_SET(scb
->fd
, &readfds
);
457 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, &tv
);
459 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, 0);
463 return SERIAL_TIMEOUT
;
464 else if (errno
== EINTR
)
467 return SERIAL_ERROR
; /* Got an error from select or poll */
472 #endif /* HAVE_SGTTY */
474 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
475 if (timeout
== scb
->current_timeout
)
479 struct hardwire_ttystate state
;
481 if (get_tty_state(scb
, &state
))
482 fprintf(stderr
, "get_tty_state failed: %s\n", safe_strerror(errno
));
485 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
489 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
492 scb
->current_timeout
= timeout
;
494 if (set_tty_state (scb
, &state
))
495 fprintf(stderr
, "set_tty_state failed: %s\n", safe_strerror(errno
));
499 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
502 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
503 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
504 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
505 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
508 hardwire_readchar(scb
, timeout
)
514 if (scb
->bufcnt
-- > 0)
517 status
= wait_for(scb
, timeout
);
522 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
524 if (scb
->bufcnt
<= 0)
525 if (scb
->bufcnt
== 0)
526 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
527 distinguish between EOF & timeouts
530 return SERIAL_ERROR
; /* Got an error from read */
533 scb
->bufp
= scb
->buf
;
545 /* Translate baud rates from integers to damn B_codes. Unix should
546 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
579 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
580 if (rate
== baudtab
[i
].rate
)
581 return baudtab
[i
].code
;
587 hardwire_setbaudrate(scb
, rate
)
591 struct hardwire_ttystate state
;
593 if (get_tty_state(scb
, &state
))
597 cfsetospeed (&state
.termios
, rate_to_code (rate
));
598 cfsetispeed (&state
.termios
, rate_to_code (rate
));
606 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
607 state
.termio
.c_cflag
|= rate_to_code (rate
);
611 state
.sgttyb
.sg_ispeed
= rate_to_code (rate
);
612 state
.sgttyb
.sg_ospeed
= rate_to_code (rate
);
615 return set_tty_state (scb
, &state
);
619 hardwire_set_process_group (scb
, ttystate
, group
)
621 serial_ttystate ttystate
;
624 #if defined (HAVE_SGTTY) || defined (HAVE_TERMIOS)
625 ((struct hardwire_ttystate
*)ttystate
)->process_group
= group
;
631 hardwire_write(scb
, str
, len
)
640 cc
= write(scb
->fd
, str
, len
);
661 static struct serial_ops hardwire_ops
=
669 hardwire_flush_output
,
670 hardwire_flush_input
,
673 hardwire_get_tty_state
,
674 hardwire_set_tty_state
,
675 hardwire_print_tty_state
,
676 hardwire_noflush_set_tty_state
,
677 hardwire_setbaudrate
,
678 hardwire_set_process_group
682 #if defined (HAVE_TERMIOS)
686 /* This is here because this is where we figure out whether we (probably)
687 have job control. Just using job_control only does part of it because
688 setpgid or setpgrp might not exist on a system without job control.
689 It might be considered misplaced (on the other hand, process groups and
690 job control are closely related to ttys).
692 For a more clean implementation, in libiberty, put a setpgid which merely
693 calls setpgrp and a setpgrp which does nothing (any system with job control
694 will have one or the other). */
701 #if defined (NEED_POSIX_SETPGID) || defined (HAVE_TERMIOS)
702 /* Do all systems with termios have setpgid? I hope so. */
703 /* setpgid (0, 0) is supposed to work and mean the same thing as
704 this, but on Ultrix 4.2A it fails with EPERM (and
705 setpgid (getpid (), getpid ()) succeeds). */
706 retval
= setpgid (getpid (), getpid ());
708 #if defined (TIOCGPGRP)
709 #if defined(USG) && !defined(SETPGRP_ARGS)
712 retval
= setpgrp (getpid (), getpid ());
714 #endif /* TIOCGPGRP. */
715 #endif /* NEED_POSIX_SETPGID */
721 _initialize_ser_hardwire ()
723 serial_add_interface (&hardwire_ops
);
725 /* OK, figure out whether we have job control. */
727 #if defined (HAVE_TERMIOS)
728 /* Do all systems with termios have the POSIX way of identifying job
729 control? I hope so. */
730 #ifdef _POSIX_JOB_CONTROL
733 job_control
= sysconf (_SC_JOB_CONTROL
);
738 /* See comment at top of file about trying to support process groups
748 #endif /* TIOCGPGRP */