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)
41 static int hardwire_open PARAMS ((serial_t scb, const char *name));
42 static void hardwire_raw PARAMS ((serial_t scb));
43 static int wait_for PARAMS ((serial_t scb, int timeout));
44 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
45 static int rate_to_code PARAMS ((int rate));
46 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
47 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
48 static void hardwire_restore PARAMS ((serial_t scb));
49 static void hardwire_close PARAMS ((serial_t scb));
51 /* Open up a real live device for serial I/O */
54 hardwire_open(scb, name)
58 scb->fd = open (name, O_RDWR);
70 struct termios termios;
72 if (tcgetattr(scb->fd, &termios))
74 fprintf(stderr, "tcgetattr failed: %s\n", safe_strerror(errno));
80 termios.c_cflag &= ~(CSIZE|PARENB);
81 termios.c_cflag |= CS8;
82 termios.c_cc[VMIN] = 0;
83 termios.c_cc[VTIME] = 0;
85 if (tcsetattr(scb->fd, TCSANOW, &termios))
87 fprintf(stderr, "tcsetattr failed: %s\n", safe_strerror(errno));
94 if (ioctl (scb->fd, TCGETA, &termio))
96 fprintf(stderr, "TCGETA failed: %s\n", safe_strerror(errno));
102 termio.c_cflag &= ~(CSIZE|PARENB);
103 termio.c_cflag |= CS8;
104 termio.c_cc[VMIN] = 0;
105 termio.c_cc[VTIME] = 0;
107 if (ioctl (scb->fd, TCSETA, &termio))
109 fprintf(stderr, "TCSETA failed: %s\n", safe_strerror(errno));
114 struct sgttyb sgttyb;
116 if (ioctl (scb->fd, TIOCGETP, &sgttyb))
117 fprintf(stderr, "TIOCGETP failed: %s\n", safe_strerror(errno));
119 sgttyb.sg_flags |= RAW | ANYP;
120 sgttyb.sg_flags &= ~(CBREAK | ECHO);
122 if (ioctl (scb->fd, TIOCSETP, &sgttyb))
123 fprintf(stderr, "TIOCSETP failed: %s\n", safe_strerror(errno));
126 scb->current_timeout = 0;
129 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
130 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
132 For termio{s}, we actually just setup VTIME if necessary, and let the
133 timeout occur in the read() in hardwire_read().
137 wait_for(scb, timeout)
152 FD_SET(scb->fd, &readfds);
155 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
157 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
161 return SERIAL_TIMEOUT;
163 return SERIAL_ERROR; /* Got an error from select or poll */
167 #endif /* HAVE_SGTTY */
169 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
170 if (timeout == scb->current_timeout)
175 struct termios termios;
177 if (tcgetattr(scb->fd, &termios))
178 fprintf(stderr, "wait_for() tcgetattr failed: %s\n", safe_strerror(errno));
180 termios.c_cc[VTIME] = timeout * 10;
182 if (tcsetattr(scb->fd, TCSANOW, &termios))
183 fprintf(stderr, "wait_for() tcsetattr failed: %s\n", safe_strerror(errno));
184 #endif /* HAVE_TERMIOS */
187 struct termio termio;
189 if (ioctl (scb->fd, TCGETA, &termio))
190 fprintf(stderr, "wait_for() TCGETA failed: %s\n", safe_strerror(errno));
192 termio.c_cc[VTIME] = timeout * 10;
194 if (ioctl (scb->fd, TCSETA, &termio))
195 fprintf(stderr, "TCSETA failed: %s\n", safe_strerror(errno));
196 #endif /* HAVE_TERMIO */
198 scb->current_timeout = timeout;
201 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
204 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
205 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
206 char if successful. Returns -2 if timeout expired, EOF if line dropped
207 dead, or -3 for any other error (see errno in that case). */
210 hardwire_readchar(scb, timeout)
216 if (scb->bufcnt-- > 0)
219 status = wait_for(scb, timeout);
224 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
226 if (scb->bufcnt <= 0)
227 if (scb->bufcnt == 0)
228 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
229 distinguish between EOF & timeouts
232 return SERIAL_ERROR; /* Got an error from read */
235 scb->bufp = scb->buf;
247 /* Translate baud rates from integers to damn B_codes. Unix should
248 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
281 for (i = 0; baudtab[i].rate != -1; i++)
282 if (rate == baudtab[i].rate)
283 return baudtab[i].code;
289 hardwire_setbaudrate(scb, rate)
294 struct termios termios;
296 if (tcgetattr (scb->fd, &termios))
299 cfsetospeed (&termios, rate_to_code (rate));
300 cfsetispeed (&termios, rate_to_code (rate));
302 if (tcsetattr (scb->fd, TCSANOW, &termios))
307 struct termio termio;
309 if (ioctl (scb->fd, TCGETA, &termio))
316 termio.c_cflag &= ~(CBAUD | CIBAUD);
317 termio.c_cflag |= rate_to_code (rate);
319 if (ioctl (scb->fd, TCSETA, &termio))
324 struct sgttyb sgttyb;
326 if (ioctl (scb->fd, TIOCGETP, &sgttyb))
329 sgttyb.sg_ispeed = rate_to_code (rate);
330 sgttyb.sg_ospeed = rate_to_code (rate);
332 if (ioctl (scb->fd, TIOCSETP, &sgttyb))
339 hardwire_write(scb, str, len)
348 cc = write(scb->fd, str, len);
359 hardwire_restore(scb)
375 static struct serial_ops hardwire_ops =
389 _initialize_ser_hardwire ()
391 serial_add_interface (&hardwire_ops);