1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993, 1994 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
31 struct hardwire_ttystate
33 struct termios termios
;
39 /* It is believed that all systems which have added job control to SVR3
40 (e.g. sco) have also added termios. Even if not, trying to figure out
41 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
42 bewildering. So we don't attempt it. */
44 struct hardwire_ttystate
51 /* Needed for the code which uses select(). We would include <sys/select.h>
52 too if it existed on all systems. */
55 struct hardwire_ttystate
60 /* Line discipline flags. */
65 static int hardwire_open
PARAMS ((serial_t scb
, const char *name
));
66 static void hardwire_raw
PARAMS ((serial_t scb
));
67 static int wait_for
PARAMS ((serial_t scb
, int timeout
));
68 static int hardwire_readchar
PARAMS ((serial_t scb
, int timeout
));
69 static int rate_to_code
PARAMS ((int rate
));
70 static int hardwire_setbaudrate
PARAMS ((serial_t scb
, int rate
));
71 static int hardwire_write
PARAMS ((serial_t scb
, const char *str
, int len
));
72 /* FIXME: static void hardwire_restore PARAMS ((serial_t scb)); */
73 static void hardwire_close
PARAMS ((serial_t scb
));
74 static int get_tty_state
PARAMS ((serial_t scb
, struct hardwire_ttystate
*state
));
75 static int set_tty_state
PARAMS ((serial_t scb
, struct hardwire_ttystate
*state
));
76 static serial_ttystate hardwire_get_tty_state
PARAMS ((serial_t scb
));
77 static int hardwire_set_tty_state
PARAMS ((serial_t scb
, serial_ttystate state
));
79 /* Open up a real live device for serial I/O */
82 hardwire_open(scb
, name
)
86 scb
->fd
= open (name
, O_RDWR
);
94 get_tty_state(scb
, state
)
96 struct hardwire_ttystate
*state
;
101 if (tcgetattr(scb
->fd
, &state
->termios
) < 0)
108 if (ioctl (scb
->fd
, TCGETA
, &state
->termio
) < 0)
114 if (ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
) < 0)
116 if (ioctl (scb
->fd
, TIOCGETC
, &state
->tc
) < 0)
118 if (ioctl (scb
->fd
, TIOCGLTC
, &state
->ltc
) < 0)
120 if (ioctl (scb
->fd
, TIOCLGET
, &state
->lmode
) < 0)
128 set_tty_state(scb
, state
)
130 struct hardwire_ttystate
*state
;
133 if (tcsetattr(scb
->fd
, TCSANOW
, &state
->termios
) < 0)
140 if (ioctl (scb
->fd
, TCSETA
, &state
->termio
) < 0)
146 if (ioctl (scb
->fd
, TIOCSETN
, &state
->sgttyb
) < 0)
148 if (ioctl (scb
->fd
, TIOCSETC
, &state
->tc
) < 0)
150 if (ioctl (scb
->fd
, TIOCSLTC
, &state
->ltc
) < 0)
152 if (ioctl (scb
->fd
, TIOCLSET
, &state
->lmode
) < 0)
159 static serial_ttystate
160 hardwire_get_tty_state(scb
)
163 struct hardwire_ttystate
*state
;
165 state
= (struct hardwire_ttystate
*)xmalloc(sizeof *state
);
167 if (get_tty_state(scb
, state
))
170 return (serial_ttystate
)state
;
174 hardwire_set_tty_state(scb
, ttystate
)
176 serial_ttystate ttystate
;
178 struct hardwire_ttystate
*state
;
180 state
= (struct hardwire_ttystate
*)ttystate
;
182 return set_tty_state(scb
, state
);
186 hardwire_noflush_set_tty_state (scb
, new_ttystate
, old_ttystate
)
188 serial_ttystate new_ttystate
;
189 serial_ttystate old_ttystate
;
191 struct hardwire_ttystate new_state
;
193 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
196 new_state
= *(struct hardwire_ttystate
*)new_ttystate
;
198 /* Don't change in or out of raw mode; we don't want to flush input.
199 termio and termios have no such restriction; for them flushing input
200 is separate from setting the attributes. */
203 if (state
->sgttyb
.sg_flags
& RAW
)
204 new_state
.sgttyb
.sg_flags
|= RAW
;
206 new_state
.sgttyb
.sg_flags
&= ~RAW
;
208 /* I'm not sure whether this is necessary; the manpage just mentions
210 if (state
->sgttyb
.sg_flags
& CBREAK
)
211 new_state
.sgttyb
.sg_flags
|= CBREAK
;
213 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
216 return set_tty_state (scb
, &new_state
);
220 hardwire_print_tty_state (scb
, ttystate
)
222 serial_ttystate ttystate
;
224 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
228 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
229 state
->termios
.c_iflag
, state
->termios
.c_oflag
);
230 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
231 state
->termios
.c_cflag
, state
->termios
.c_lflag
);
233 /* This not in POSIX, and is not really documented by those systems
234 which have it (at least not Sun). */
235 printf_filtered ("c_line = 0x%x.\n", state
->termios
.c_line
);
237 printf_filtered ("c_cc: ");
238 for (i
= 0; i
< NCCS
; i
+= 1)
239 printf_filtered ("0x%x ", state
->termios
.c_cc
[i
]);
240 printf_filtered ("\n");
244 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
245 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
246 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
247 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
248 state
->termio
.c_line
);
249 printf_filtered ("c_cc: ");
250 for (i
= 0; i
< NCC
; i
+= 1)
251 printf_filtered ("0x%x ", state
->termio
.c_cc
[i
]);
252 printf_filtered ("\n");
256 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state
->sgttyb
.sg_flags
);
258 printf_filtered ("tchars: ");
259 for (i
= 0; i
< (int)sizeof (struct tchars
); i
++)
260 printf_filtered ("0x%x ", ((unsigned char *)&state
->tc
)[i
]);
261 printf_filtered ("\n");
263 printf_filtered ("ltchars: ");
264 for (i
= 0; i
< (int)sizeof (struct ltchars
); i
++)
265 printf_filtered ("0x%x ", ((unsigned char *)&state
->ltc
)[i
]);
266 printf_filtered ("\n");
268 printf_filtered ("lmode: 0x%x\n", state
->lmode
);
273 hardwire_flush_output (scb
)
277 return tcflush (scb
->fd
, TCOFLUSH
);
281 return ioctl (scb
->fd
, TCFLSH
, 1);
285 /* This flushes both input and output, but we can't do better. */
286 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
291 hardwire_flush_input (scb
)
295 scb
->bufp
= scb
->buf
;
298 return tcflush (scb
->fd
, TCIFLUSH
);
302 return ioctl (scb
->fd
, TCFLSH
, 0);
306 /* This flushes both input and output, but we can't do better. */
307 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
312 hardwire_send_break (scb
)
316 return tcsendbreak (scb
->fd
, 0);
320 return ioctl (scb
->fd
, TCSBRK
, 0);
326 struct timeval timeout
;
328 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
330 /* Can't use usleep; it doesn't exist in BSD 4.2. */
331 /* Note that if this select() is interrupted by a signal it will not wait
332 the full length of time. I think that is OK. */
334 timeout
.tv_usec
= 250000;
335 select (0, 0, 0, 0, &timeout
);
336 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
346 struct hardwire_ttystate state
;
348 if (get_tty_state(scb
, &state
))
349 fprintf_unfiltered(gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror(errno
));
352 state
.termios
.c_iflag
= 0;
353 state
.termios
.c_oflag
= 0;
354 state
.termios
.c_lflag
= 0;
355 state
.termios
.c_cflag
&= ~(CSIZE
|PARENB
);
356 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
357 state
.termios
.c_cc
[VMIN
] = 0;
358 state
.termios
.c_cc
[VTIME
] = 0;
362 state
.termio
.c_iflag
= 0;
363 state
.termio
.c_oflag
= 0;
364 state
.termio
.c_lflag
= 0;
365 state
.termio
.c_cflag
&= ~(CSIZE
|PARENB
);
366 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
367 state
.termio
.c_cc
[VMIN
] = 0;
368 state
.termio
.c_cc
[VTIME
] = 0;
372 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
373 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
376 scb
->current_timeout
= 0;
378 if (set_tty_state (scb
, &state
))
379 fprintf_unfiltered(gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror(errno
));
382 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
383 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
385 For termio{s}, we actually just setup VTIME if necessary, and let the
386 timeout occur in the read() in hardwire_read().
390 wait_for(scb
, timeout
)
394 scb
->timeout_remaining
= 0;
406 FD_SET(scb
->fd
, &readfds
);
413 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, &tv
);
415 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, 0);
419 return SERIAL_TIMEOUT
;
420 else if (errno
== EINTR
)
423 return SERIAL_ERROR
; /* Got an error from select or poll */
428 #endif /* HAVE_SGTTY */
430 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
431 if (timeout
== scb
->current_timeout
)
434 scb
->current_timeout
= timeout
;
437 struct hardwire_ttystate state
;
439 if (get_tty_state(scb
, &state
))
440 fprintf_unfiltered(gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror(errno
));
446 state
.termios
.c_cc
[VTIME
] = 0;
447 state
.termios
.c_cc
[VMIN
] = 1;
451 state
.termios
.c_cc
[VMIN
] = 0;
452 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
453 if (state
.termios
.c_cc
[VTIME
] != timeout
* 10)
456 /* If c_cc is an 8-bit signed character, we can't go
457 bigger than this. If it is always unsigned, we could use
460 scb
->current_timeout
= 12;
461 state
.termios
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
462 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
471 state
.termio
.c_cc
[VTIME
] = 0;
472 state
.termio
.c_cc
[VMIN
] = 1;
476 state
.termio
.c_cc
[VMIN
] = 0;
477 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
478 if (state
.termio
.c_cc
[VTIME
] != timeout
* 10)
480 /* If c_cc is an 8-bit signed character, we can't go
481 bigger than this. If it is always unsigned, we could use
484 scb
->current_timeout
= 12;
485 state
.termio
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
486 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
491 if (set_tty_state (scb
, &state
))
492 fprintf_unfiltered(gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror(errno
));
496 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
499 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
500 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
501 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
502 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
505 hardwire_readchar(scb
, timeout
)
511 if (scb
->bufcnt
-- > 0)
516 status
= wait_for (scb
, timeout
);
521 scb
->bufcnt
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
523 if (scb
->bufcnt
<= 0)
525 if (scb
->bufcnt
== 0)
527 /* Zero characters means timeout (it could also be EOF, but
528 we don't (yet at least) distinguish). */
529 if (scb
->timeout_remaining
> 0)
531 timeout
= scb
->timeout_remaining
;
535 return SERIAL_TIMEOUT
;
537 else if (errno
== EINTR
)
540 return SERIAL_ERROR
; /* Got an error from read */
544 scb
->bufp
= scb
->buf
;
557 /* Translate baud rates from integers to damn B_codes. Unix should
558 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
591 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
592 if (rate
== baudtab
[i
].rate
)
593 return baudtab
[i
].code
;
599 hardwire_setbaudrate(scb
, rate
)
603 struct hardwire_ttystate state
;
605 if (get_tty_state(scb
, &state
))
609 cfsetospeed (&state
.termios
, rate_to_code (rate
));
610 cfsetispeed (&state
.termios
, rate_to_code (rate
));
618 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
619 state
.termio
.c_cflag
|= rate_to_code (rate
);
623 state
.sgttyb
.sg_ispeed
= rate_to_code (rate
);
624 state
.sgttyb
.sg_ospeed
= rate_to_code (rate
);
627 return set_tty_state (scb
, &state
);
631 hardwire_setstopbits(scb
, num
)
635 struct hardwire_ttystate state
;
638 if (get_tty_state(scb
, &state
))
643 case SERIAL_1_STOPBITS
:
646 case SERIAL_1_AND_A_HALF_STOPBITS
:
647 case SERIAL_2_STOPBITS
:
656 state
.termios
.c_cflag
&= ~CSTOPB
;
658 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
663 state
.termio
.c_cflag
&= ~CSTOPB
;
665 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
669 return 0; /* sgtty doesn't support this */
672 return set_tty_state (scb
, &state
);
676 hardwire_write(scb
, str
, len
)
685 cc
= write(scb
->fd
, str
, len
);
706 static struct serial_ops hardwire_ops
=
714 hardwire_flush_output
,
715 hardwire_flush_input
,
718 hardwire_get_tty_state
,
719 hardwire_set_tty_state
,
720 hardwire_print_tty_state
,
721 hardwire_noflush_set_tty_state
,
722 hardwire_setbaudrate
,
723 hardwire_setstopbits
,
727 _initialize_ser_hardwire ()
729 serial_add_interface (&hardwire_ops
);