1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 2003, 2004, 2005 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
29 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include "gdb_string.h"
38 struct hardwire_ttystate
40 struct termios termios
;
46 /* It is believed that all systems which have added job control to SVR3
47 (e.g. sco) have also added termios. Even if not, trying to figure out
48 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
49 bewildering. So we don't attempt it. */
51 struct hardwire_ttystate
58 struct hardwire_ttystate
63 /* Line discipline flags. */
68 static int hardwire_open (struct serial
*scb
, const char *name
);
69 static void hardwire_raw (struct serial
*scb
);
70 static int wait_for (struct serial
*scb
, int timeout
);
71 static int hardwire_readchar (struct serial
*scb
, int timeout
);
72 static int do_hardwire_readchar (struct serial
*scb
, int timeout
);
73 static int rate_to_code (int rate
);
74 static int hardwire_setbaudrate (struct serial
*scb
, int rate
);
75 static void hardwire_close (struct serial
*scb
);
76 static int get_tty_state (struct serial
*scb
,
77 struct hardwire_ttystate
* state
);
78 static int set_tty_state (struct serial
*scb
,
79 struct hardwire_ttystate
* state
);
80 static serial_ttystate
hardwire_get_tty_state (struct serial
*scb
);
81 static int hardwire_set_tty_state (struct serial
*scb
, serial_ttystate state
);
82 static int hardwire_noflush_set_tty_state (struct serial
*, serial_ttystate
,
84 static void hardwire_print_tty_state (struct serial
*, serial_ttystate
,
86 static int hardwire_drain_output (struct serial
*);
87 static int hardwire_flush_output (struct serial
*);
88 static int hardwire_flush_input (struct serial
*);
89 static int hardwire_send_break (struct serial
*);
90 static int hardwire_setstopbits (struct serial
*, int);
92 void _initialize_ser_hardwire (void);
94 /* Open up a real live device for serial I/O */
97 hardwire_open (struct serial
*scb
, const char *name
)
99 scb
->fd
= open (name
, O_RDWR
);
107 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
110 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
117 if (ioctl (scb
->fd
, TCGETA
, &state
->termio
) < 0)
123 if (ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
) < 0)
125 if (ioctl (scb
->fd
, TIOCGETC
, &state
->tc
) < 0)
127 if (ioctl (scb
->fd
, TIOCGLTC
, &state
->ltc
) < 0)
129 if (ioctl (scb
->fd
, TIOCLGET
, &state
->lmode
) < 0)
137 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
140 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
147 if (ioctl (scb
->fd
, TCSETA
, &state
->termio
) < 0)
153 if (ioctl (scb
->fd
, TIOCSETN
, &state
->sgttyb
) < 0)
155 if (ioctl (scb
->fd
, TIOCSETC
, &state
->tc
) < 0)
157 if (ioctl (scb
->fd
, TIOCSLTC
, &state
->ltc
) < 0)
159 if (ioctl (scb
->fd
, TIOCLSET
, &state
->lmode
) < 0)
166 static serial_ttystate
167 hardwire_get_tty_state (struct serial
*scb
)
169 struct hardwire_ttystate
*state
;
171 state
= (struct hardwire_ttystate
*) xmalloc (sizeof *state
);
173 if (get_tty_state (scb
, state
))
176 return (serial_ttystate
) state
;
180 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
182 struct hardwire_ttystate
*state
;
184 state
= (struct hardwire_ttystate
*) ttystate
;
186 return set_tty_state (scb
, state
);
190 hardwire_noflush_set_tty_state (struct serial
*scb
,
191 serial_ttystate new_ttystate
,
192 serial_ttystate old_ttystate
)
194 struct hardwire_ttystate new_state
;
196 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
199 new_state
= *(struct hardwire_ttystate
*) new_ttystate
;
201 /* Don't change in or out of raw mode; we don't want to flush input.
202 termio and termios have no such restriction; for them flushing input
203 is separate from setting the attributes. */
206 if (state
->sgttyb
.sg_flags
& RAW
)
207 new_state
.sgttyb
.sg_flags
|= RAW
;
209 new_state
.sgttyb
.sg_flags
&= ~RAW
;
211 /* I'm not sure whether this is necessary; the manpage just mentions
213 if (state
->sgttyb
.sg_flags
& CBREAK
)
214 new_state
.sgttyb
.sg_flags
|= CBREAK
;
216 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
219 return set_tty_state (scb
, &new_state
);
223 hardwire_print_tty_state (struct serial
*scb
,
224 serial_ttystate ttystate
,
225 struct ui_file
*stream
)
227 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
231 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
232 (int) state
->termios
.c_iflag
,
233 (int) state
->termios
.c_oflag
);
234 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
235 (int) state
->termios
.c_cflag
,
236 (int) state
->termios
.c_lflag
);
238 /* This not in POSIX, and is not really documented by those systems
239 which have it (at least not Sun). */
240 fprintf_filtered (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
242 fprintf_filtered (stream
, "c_cc: ");
243 for (i
= 0; i
< NCCS
; i
+= 1)
244 fprintf_filtered (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
245 fprintf_filtered (stream
, "\n");
249 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
250 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
251 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
252 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
253 state
->termio
.c_line
);
254 fprintf_filtered (stream
, "c_cc: ");
255 for (i
= 0; i
< NCC
; i
+= 1)
256 fprintf_filtered (stream
, "0x%x ", state
->termio
.c_cc
[i
]);
257 fprintf_filtered (stream
, "\n");
261 fprintf_filtered (stream
, "sgttyb.sg_flags = 0x%x.\n",
262 state
->sgttyb
.sg_flags
);
264 fprintf_filtered (stream
, "tchars: ");
265 for (i
= 0; i
< (int) sizeof (struct tchars
); i
++)
266 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->tc
)[i
]);
267 fprintf_filtered (stream
, "\n");
269 fprintf_filtered (stream
, "ltchars: ");
270 for (i
= 0; i
< (int) sizeof (struct ltchars
); i
++)
271 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->ltc
)[i
]);
272 fprintf_filtered (stream
, "\n");
274 fprintf_filtered (stream
, "lmode: 0x%x\n", state
->lmode
);
278 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
281 hardwire_drain_output (struct serial
*scb
)
284 return tcdrain (scb
->fd
);
288 return ioctl (scb
->fd
, TCSBRK
, 1);
292 /* Get the current state and then restore it using TIOCSETP,
293 which should cause the output to drain and pending input
296 struct hardwire_ttystate state
;
297 if (get_tty_state (scb
, &state
))
303 return (ioctl (scb
->fd
, TIOCSETP
, &state
.sgttyb
));
310 hardwire_flush_output (struct serial
*scb
)
313 return tcflush (scb
->fd
, TCOFLUSH
);
317 return ioctl (scb
->fd
, TCFLSH
, 1);
321 /* This flushes both input and output, but we can't do better. */
322 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
327 hardwire_flush_input (struct serial
*scb
)
329 ser_base_flush_input (scb
);
332 return tcflush (scb
->fd
, TCIFLUSH
);
336 return ioctl (scb
->fd
, TCFLSH
, 0);
340 /* This flushes both input and output, but we can't do better. */
341 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
346 hardwire_send_break (struct serial
*scb
)
349 return tcsendbreak (scb
->fd
, 0);
353 return ioctl (scb
->fd
, TCSBRK
, 0);
359 struct timeval timeout
;
361 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
363 /* Can't use usleep; it doesn't exist in BSD 4.2. */
364 /* Note that if this select() is interrupted by a signal it will not wait
365 the full length of time. I think that is OK. */
367 timeout
.tv_usec
= 250000;
368 select (0, 0, 0, 0, &timeout
);
369 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
376 hardwire_raw (struct serial
*scb
)
378 struct hardwire_ttystate state
;
380 if (get_tty_state (scb
, &state
))
381 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
384 state
.termios
.c_iflag
= 0;
385 state
.termios
.c_oflag
= 0;
386 state
.termios
.c_lflag
= 0;
387 state
.termios
.c_cflag
&= ~(CSIZE
| PARENB
);
388 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
389 state
.termios
.c_cc
[VMIN
] = 0;
390 state
.termios
.c_cc
[VTIME
] = 0;
394 state
.termio
.c_iflag
= 0;
395 state
.termio
.c_oflag
= 0;
396 state
.termio
.c_lflag
= 0;
397 state
.termio
.c_cflag
&= ~(CSIZE
| PARENB
);
398 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
399 state
.termio
.c_cc
[VMIN
] = 0;
400 state
.termio
.c_cc
[VTIME
] = 0;
404 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
405 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
408 scb
->current_timeout
= 0;
410 if (set_tty_state (scb
, &state
))
411 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
414 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
415 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
417 For termio{s}, we actually just setup VTIME if necessary, and let the
418 timeout occur in the read() in hardwire_read().
421 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
422 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
425 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
426 possible values of the TIMEOUT parameter are ONE and ZERO.
427 Consequently all the code that tries to handle the possability of
428 an overflowed timer is unnecessary. */
431 wait_for (struct serial
*scb
, int timeout
)
440 /* NOTE: Some OS's can scramble the READFDS when the select()
441 call fails (ex the kernel with Red Hat 5.2). Initialize all
442 arguments before each call. */
448 FD_SET (scb
->fd
, &readfds
);
451 numfds
= select (scb
->fd
+ 1, &readfds
, 0, 0, &tv
);
453 numfds
= select (scb
->fd
+ 1, &readfds
, 0, 0, 0);
457 return SERIAL_TIMEOUT
;
458 else if (errno
== EINTR
)
461 return SERIAL_ERROR
; /* Got an error from select or poll */
465 #endif /* HAVE_SGTTY */
467 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
468 if (timeout
== scb
->current_timeout
)
471 scb
->current_timeout
= timeout
;
474 struct hardwire_ttystate state
;
476 if (get_tty_state (scb
, &state
))
477 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
483 state
.termios
.c_cc
[VTIME
] = 0;
484 state
.termios
.c_cc
[VMIN
] = 1;
488 state
.termios
.c_cc
[VMIN
] = 0;
489 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
490 if (state
.termios
.c_cc
[VTIME
] != timeout
* 10)
493 /* If c_cc is an 8-bit signed character, we can't go
494 bigger than this. If it is always unsigned, we could use
497 scb
->current_timeout
= 12;
498 state
.termios
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
499 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
508 state
.termio
.c_cc
[VTIME
] = 0;
509 state
.termio
.c_cc
[VMIN
] = 1;
513 state
.termio
.c_cc
[VMIN
] = 0;
514 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
515 if (state
.termio
.c_cc
[VTIME
] != timeout
* 10)
517 /* If c_cc is an 8-bit signed character, we can't go
518 bigger than this. If it is always unsigned, we could use
521 scb
->current_timeout
= 12;
522 state
.termio
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
523 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
528 if (set_tty_state (scb
, &state
))
529 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
533 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
536 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
537 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
538 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
539 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
541 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
542 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
545 /* NOTE: cagney/1999-09-16: This function is not identical to
546 ser_base_readchar() as part of replacing it with ser_base*()
547 merging will be required - this code handles the case where read()
548 times out due to no data while ser_base_readchar() doesn't expect
552 do_hardwire_readchar (struct serial
*scb
, int timeout
)
560 /* We have to be able to keep the GUI alive here, so we break the
561 original timeout into steps of 1 second, running the "keep the
562 GUI alive" hook each time through the loop.
564 Also, timeout = 0 means to poll, so we just set the delta to 0,
565 so we will only go through the loop once. */
567 delta
= (timeout
== 0 ? 0 : 1);
571 /* N.B. The UI may destroy our world (for instance by calling
572 remote_stop,) in which case we want to get out of here as
573 quickly as possible. It is not safe to touch scb, since
574 someone else might have freed it. The
575 deprecated_ui_loop_hook signals that we should exit by
578 if (deprecated_ui_loop_hook
)
579 detach
= deprecated_ui_loop_hook (0);
582 return SERIAL_TIMEOUT
;
584 scb
->timeout_remaining
= (timeout
< 0 ? timeout
: timeout
- delta
);
585 status
= wait_for (scb
, delta
);
590 status
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
596 /* Zero characters means timeout (it could also be EOF, but
597 we don't (yet at least) distinguish). */
598 if (scb
->timeout_remaining
> 0)
600 timeout
= scb
->timeout_remaining
;
603 else if (scb
->timeout_remaining
< 0)
606 return SERIAL_TIMEOUT
;
608 else if (errno
== EINTR
)
611 return SERIAL_ERROR
; /* Got an error from read */
614 scb
->bufcnt
= status
;
616 scb
->bufp
= scb
->buf
;
622 hardwire_readchar (struct serial
*scb
, int timeout
)
624 return generic_readchar (scb
, timeout
, do_hardwire_readchar
);
636 /* Translate baud rates from integers to damn B_codes. Unix should
637 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
737 rate_to_code (int rate
)
741 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
743 /* test for perfect macth. */
744 if (rate
== baudtab
[i
].rate
)
745 return baudtab
[i
].code
;
748 /* check if it is in between valid values. */
749 if (rate
< baudtab
[i
].rate
)
753 warning (_("Invalid baud rate %d. Closest values are %d and %d."),
754 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
758 warning (_("Invalid baud rate %d. Minimum value is %d."),
759 rate
, baudtab
[0].rate
);
766 /* The requested speed was too large. */
767 warning (_("Invalid baud rate %d. Maximum value is %d."),
768 rate
, baudtab
[i
- 1].rate
);
773 hardwire_setbaudrate (struct serial
*scb
, int rate
)
775 struct hardwire_ttystate state
;
776 int baud_code
= rate_to_code (rate
);
780 /* The baud rate was not valid.
781 A warning has already been issued. */
786 if (get_tty_state (scb
, &state
))
790 cfsetospeed (&state
.termios
, baud_code
);
791 cfsetispeed (&state
.termios
, baud_code
);
799 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
800 state
.termio
.c_cflag
|= baud_code
;
804 state
.sgttyb
.sg_ispeed
= baud_code
;
805 state
.sgttyb
.sg_ospeed
= baud_code
;
808 return set_tty_state (scb
, &state
);
812 hardwire_setstopbits (struct serial
*scb
, int num
)
814 struct hardwire_ttystate state
;
817 if (get_tty_state (scb
, &state
))
822 case SERIAL_1_STOPBITS
:
825 case SERIAL_1_AND_A_HALF_STOPBITS
:
826 case SERIAL_2_STOPBITS
:
835 state
.termios
.c_cflag
&= ~CSTOPB
;
837 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
842 state
.termio
.c_cflag
&= ~CSTOPB
;
844 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
848 return 0; /* sgtty doesn't support this */
851 return set_tty_state (scb
, &state
);
855 hardwire_close (struct serial
*scb
)
866 _initialize_ser_hardwire (void)
868 struct serial_ops
*ops
= XMALLOC (struct serial_ops
);
869 memset (ops
, 0, sizeof (struct serial_ops
));
870 ops
->name
= "hardwire";
872 ops
->open
= hardwire_open
;
873 ops
->close
= hardwire_close
;
874 /* FIXME: Don't replace this with the equivalent ser_base*() until
875 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
877 ops
->readchar
= hardwire_readchar
;
878 ops
->write
= ser_base_write
;
879 ops
->flush_output
= hardwire_flush_output
;
880 ops
->flush_input
= hardwire_flush_input
;
881 ops
->send_break
= hardwire_send_break
;
882 ops
->go_raw
= hardwire_raw
;
883 ops
->get_tty_state
= hardwire_get_tty_state
;
884 ops
->set_tty_state
= hardwire_set_tty_state
;
885 ops
->print_tty_state
= hardwire_print_tty_state
;
886 ops
->noflush_set_tty_state
= hardwire_noflush_set_tty_state
;
887 ops
->setbaudrate
= hardwire_setbaudrate
;
888 ops
->setstopbits
= hardwire_setstopbits
;
889 ops
->drain_output
= hardwire_drain_output
;
890 ops
->async
= ser_base_async
;
891 ops
->read_prim
= ser_unix_read_prim
;
892 ops
->write_prim
= ser_unix_write_prim
;
893 serial_add_interface (ops
);
897 ser_unix_read_prim (struct serial
*scb
, size_t count
)
903 status
= read (scb
->fd
, scb
->buf
, count
);
904 if (status
!= -1 || errno
!= EINTR
)
911 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
913 /* ??? Historically, GDB has not retried calls to "write" that
915 return write (scb
->fd
, buf
, len
);