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)
43 static int hardwire_open PARAMS ((serial_t scb, const char *name));
44 static void hardwire_raw PARAMS ((serial_t scb));
45 static int wait_for PARAMS ((serial_t scb, int timeout));
46 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
47 static int rate_to_code PARAMS ((int rate));
48 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
49 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
50 static void hardwire_restore PARAMS ((serial_t scb));
51 static void hardwire_close PARAMS ((serial_t scb));
53 /* Open up a real live device for serial I/O */
56 hardwire_open(scb, name)
60 scb->fd = open (name, O_RDWR);
72 struct termios termios;
74 if (tcgetattr(scb->fd, &termios))
76 fprintf(stderr, "tcgetattr failed: %s\n", safe_strerror(errno));
82 termios.c_cflag &= ~(CSIZE|PARENB);
83 termios.c_cflag |= CS8;
84 termios.c_cc[VMIN] = 0;
85 termios.c_cc[VTIME] = 0;
87 if (tcsetattr(scb->fd, TCSANOW, &termios))
89 fprintf(stderr, "tcsetattr failed: %s\n", safe_strerror(errno));
96 if (ioctl (scb->fd, TCGETA, &termio))
98 fprintf(stderr, "TCGETA failed: %s\n", safe_strerror(errno));
104 termio.c_cflag &= ~(CSIZE|PARENB);
105 termio.c_cflag |= CS8;
106 termio.c_cc[VMIN] = 0;
107 termio.c_cc[VTIME] = 0;
109 if (ioctl (scb->fd, TCSETA, &termio))
111 fprintf(stderr, "TCSETA failed: %s\n", safe_strerror(errno));
116 struct sgttyb sgttyb;
118 if (ioctl (scb->fd, TIOCGETP, &sgttyb))
119 fprintf(stderr, "TIOCGETP failed: %s\n", safe_strerror(errno));
121 sgttyb.sg_flags |= RAW | ANYP;
122 sgttyb.sg_flags &= ~(CBREAK | ECHO);
124 if (ioctl (scb->fd, TIOCSETP, &sgttyb))
125 fprintf(stderr, "TIOCSETP failed: %s\n", safe_strerror(errno));
129 /* Wait for input on scb, with timeout seconds */
132 wait_for(scb, timeout)
147 FD_SET(scb->fd, &readfds);
150 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
152 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
154 #endif /* HAVE_SGTTY */
156 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
157 struct pollfd pollfd;
160 pollfd.events = POLLIN | POLLPRI;
165 numfds = poll(&pollfd, 1, timeout);
167 #endif /* defined HAVE_TERMIO || defined HAVE_TERMIOS */
171 return SERIAL_TIMEOUT;
173 return SERIAL_ERROR; /* Got an error from select or poll */
176 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
177 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
178 char if successful. Returns -2 if timeout expired, EOF if line dropped
179 dead, or -3 for any other error (see errno in that case). */
182 hardwire_readchar(scb, timeout)
188 if (scb->bufcnt-- > 0)
191 status = wait_for(scb, timeout);
196 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
198 if (scb->bufcnt <= 0)
199 if (scb->bufcnt == 0)
200 return SERIAL_EOF; /* 0 chars means end of file */
202 return SERIAL_ERROR; /* Got an error from read */
205 scb->bufp = scb->buf;
217 /* Translate baud rates from integers to damn B_codes. Unix should
218 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
251 for (i = 0; baudtab[i].rate != -1; i++)
252 if (rate == baudtab[i].rate)
253 return baudtab[i].code;
259 hardwire_setbaudrate(scb, rate)
264 struct termios termios;
266 if (tcgetattr (scb->fd, &termios))
269 cfsetospeed (&termios, rate_to_code (rate));
270 cfsetispeed (&termios, rate_to_code (rate));
272 if (tcsetattr (scb->fd, TCSANOW, &termios))
277 struct termio termio;
279 if (ioctl (scb->fd, TCGETA, &termio))
286 termio.c_cflag &= ~(CBAUD | CIBAUD);
287 termio.c_cflag |= rate_to_code (rate);
289 if (ioctl (scb->fd, TCSETA, &termio))
294 struct sgttyb sgttyb;
296 if (ioctl (scb->fd, TIOCGETP, &sgttyb))
299 sgttyb.sg_ispeed = rate_to_code (rate);
300 sgttyb.sg_ospeed = rate_to_code (rate);
302 if (ioctl (scb->fd, TIOCSETP, &sgttyb))
309 hardwire_write(scb, str, len)
318 cc = write(scb->fd, str, len);
329 hardwire_restore(scb)
345 static struct serial_ops hardwire_ops =
359 _initialize_ser_hardwire ()
361 serial_add_interface (&hardwire_ops);