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>
26 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
34 struct hardwire_ttystate
36 struct termios termios;
43 struct hardwire_ttystate
52 struct hardwire_ttystate
58 static int hardwire_open PARAMS ((serial_t scb, const char *name));
59 static void hardwire_raw PARAMS ((serial_t scb));
60 static int wait_for PARAMS ((serial_t scb, int timeout));
61 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
62 static int rate_to_code PARAMS ((int rate));
63 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
64 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
65 static void hardwire_restore PARAMS ((serial_t scb));
66 static void hardwire_close PARAMS ((serial_t scb));
67 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
68 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
69 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
70 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
72 /* Open up a real live device for serial I/O */
75 hardwire_open(scb, name)
79 scb->fd = open (name, O_RDWR);
87 get_tty_state(scb, state)
89 struct hardwire_ttystate *state;
92 return tcgetattr(scb->fd, &state->termios);
96 return ioctl (scb->fd, TCGETA, &state->termio);
100 return ioctl (scb->fd, TIOCGETP, &state->sgttyb);
105 set_tty_state(scb, state)
107 struct hardwire_ttystate *state;
112 return tcsetattr(scb->fd, TCSANOW, &state->termios);
116 return ioctl (scb->fd, TCSETA, &state->termio);
120 return ioctl (scb->fd, TIOCSETP, &state->sgttyb);
124 static serial_ttystate
125 hardwire_get_tty_state(scb)
128 struct hardwire_ttystate *state;
130 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
132 if (get_tty_state(scb, state))
135 return (serial_ttystate)state;
139 hardwire_set_tty_state(scb, ttystate)
141 serial_ttystate ttystate;
143 struct hardwire_ttystate *state;
145 state = (struct hardwire_ttystate *)ttystate;
147 return set_tty_state(scb, state);
154 struct hardwire_ttystate state;
156 if (get_tty_state(scb, &state))
157 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
160 state.termios.c_iflag = 0;
161 state.termios.c_oflag = 0;
162 state.termios.c_lflag = 0;
163 state.termios.c_cflag &= ~(CSIZE|PARENB);
164 state.termios.c_cflag |= CS8;
165 state.termios.c_cc[VMIN] = 0;
166 state.termios.c_cc[VTIME] = 0;
170 state.termio.c_iflag = 0;
171 state.termio.c_oflag = 0;
172 state.termio.c_lflag = 0;
173 state.termio.c_cflag &= ~(CSIZE|PARENB);
174 state.termio.c_cflag |= CS8;
175 state.termio.c_cc[VMIN] = 0;
176 state.termio.c_cc[VTIME] = 0;
180 state.sgttyb.sg_flags |= RAW | ANYP;
181 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
184 scb->current_timeout = 0;
186 if (set_tty_state (scb, &state))
187 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
190 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
191 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
193 For termio{s}, we actually just setup VTIME if necessary, and let the
194 timeout occur in the read() in hardwire_read().
198 wait_for(scb, timeout)
213 FD_SET(scb->fd, &readfds);
216 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
218 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
222 return SERIAL_TIMEOUT;
224 return SERIAL_ERROR; /* Got an error from select or poll */
228 #endif /* HAVE_SGTTY */
230 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
231 if (timeout == scb->current_timeout)
235 struct hardwire_ttystate state;
237 if (get_tty_state(scb, &state))
238 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
241 state.termios.c_cc[VTIME] = timeout * 10;
245 state.termio.c_cc[VTIME] = timeout * 10;
248 scb->current_timeout = timeout;
250 if (set_tty_state (scb, &state))
251 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
255 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
258 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
259 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
260 char if successful. Returns -2 if timeout expired, EOF if line dropped
261 dead, or -3 for any other error (see errno in that case). */
264 hardwire_readchar(scb, timeout)
270 if (scb->bufcnt-- > 0)
273 status = wait_for(scb, timeout);
278 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
280 if (scb->bufcnt <= 0)
281 if (scb->bufcnt == 0)
282 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
283 distinguish between EOF & timeouts
286 return SERIAL_ERROR; /* Got an error from read */
289 scb->bufp = scb->buf;
301 /* Translate baud rates from integers to damn B_codes. Unix should
302 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
335 for (i = 0; baudtab[i].rate != -1; i++)
336 if (rate == baudtab[i].rate)
337 return baudtab[i].code;
343 hardwire_setbaudrate(scb, rate)
347 struct hardwire_ttystate state;
349 if (get_tty_state(scb, &state))
353 cfsetospeed (&state.termios, rate_to_code (rate));
354 cfsetispeed (&state.termios, rate_to_code (rate));
362 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
363 state.termio.c_cflag |= rate_to_code (rate);
367 state.sgttyb.sg_ispeed = rate_to_code (rate);
368 state.sgttyb.sg_ospeed = rate_to_code (rate);
371 return set_tty_state (scb, &state);
375 hardwire_write(scb, str, len)
384 cc = write(scb->fd, str, len);
405 static struct serial_ops hardwire_ops =
414 hardwire_get_tty_state,
415 hardwire_set_tty_state,
416 hardwire_setbaudrate,
420 _initialize_ser_hardwire ()
422 serial_add_interface (&hardwire_ops);