* remote.c (remote_open): Use SERIAL_OPEN instead of serial_open.
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/time.h>
25
26 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
27 #define HAVE_SGTTY
28 #endif
29
30 #ifdef HAVE_TERMIOS
31 #include <termios.h>
32 #include <unistd.h>
33 #endif
34 #ifdef HAVE_TERMIO
35 #include <sys/termio.h>
36 #endif
37 #ifdef HAVE_SGTTY
38 #include <sgtty.h>
39 #endif
40
41 /* Open up a real live device for serial I/O */
42
43 static int
44 hardwire_open(scb, name)
45 serial_t scb;
46 const char *name;
47 {
48 scb->fd = open (name, O_RDWR);
49 if (scb->fd < 0)
50 return -1;
51
52 return 0;
53 }
54
55 static void
56 hardwire_raw(scb)
57 serial_t scb;
58 {
59 #ifdef HAVE_TERMIOS
60 struct termios termios;
61
62 if (tcgetattr(scb->fd, &termios))
63 {
64 fprintf(stderr, "tcgetattr failed: %s\n", safe_strerror(errno));
65 }
66
67 termios.c_iflag = 0;
68 termios.c_oflag = 0;
69 termios.c_lflag = 0;
70 termios.c_cflag &= ~(CSIZE|PARENB);
71 termios.c_cflag |= CS8;
72 termios.c_cc[VMIN] = 0;
73 termios.c_cc[VTIME] = 0;
74
75 if (tcsetattr(scb->fd, TCSANOW, &termios))
76 {
77 fprintf(stderr, "tcsetattr failed: %s\n", safe_strerror(errno));
78 }
79 #endif
80
81 #ifdef HAVE_TERMIO
82 struct termio termio;
83
84 if (ioctl (scb->fd, TCGETA, &termio))
85 {
86 fprintf(stderr, "TCGETA failed: %s\n", safe_strerror(errno));
87 }
88
89 termio.c_iflag = 0;
90 termio.c_oflag = 0;
91 termio.c_lflag = 0;
92 termio.c_cflag &= ~(CSIZE|PARENB);
93 termio.c_cflag |= CS8;
94 termio.c_cc[VMIN] = 0;
95 termio.c_cc[VTIME] = 0;
96
97 if (ioctl (scb->fd, TCSETA, &termio))
98 {
99 fprintf(stderr, "TCSETA failed: %s\n", safe_strerror(errno));
100 }
101 #endif
102
103 #ifdef HAVE_SGTTY
104 struct sgttyb sgttyb;
105
106 if (ioctl (scb->fd, TIOCGETP, &sgttyb))
107 fprintf(stderr, "TIOCGETP failed: %s\n", safe_strerror(errno));
108
109 sgttyb.sg_flags |= RAW | ANYP;
110 sgttyb.sg_flags &= ~(CBREAK | ECHO);
111
112 if (ioctl (scb->fd, TIOCSETP, &sgttyb))
113 fprintf(stderr, "TIOCSETP failed: %s\n", safe_strerror(errno));
114 #endif
115 }
116
117 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
118 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
119 char if successful. Returns -2 if timeout expired, EOF if line dropped
120 dead, or -3 for any other error (see errno in that case). */
121
122 static int
123 hardwire_readchar(scb, timeout)
124 serial_t scb;
125 int timeout;
126 {
127 int numfds;
128 struct timeval tv;
129 fd_set readfds;
130
131 if (scb->bufcnt-- > 0)
132 return *scb->bufp++;
133
134 FD_ZERO (&readfds);
135
136 tv.tv_sec = timeout;
137 tv.tv_usec = 0;
138
139 FD_SET(scb->fd, &readfds);
140
141 if (timeout >= 0)
142 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
143 else
144 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
145
146 if (numfds <= 0)
147 if (numfds == 0)
148 return SERIAL_TIMEOUT;
149 else
150 return SERIAL_ERROR; /* Got an error from select */
151
152 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
153
154 if (scb->bufcnt <= 0)
155 if (scb->bufcnt == 0)
156 return SERIAL_EOF; /* 0 chars means end of file */
157 else
158 return SERIAL_ERROR; /* Got an error from read */
159
160 scb->bufcnt--;
161 scb->bufp = scb->buf;
162 return *scb->bufp++;
163 }
164
165 #ifndef B19200
166 #define B19200 EXTA
167 #endif
168
169 #ifndef B38400
170 #define B38400 EXTB
171 #endif
172
173 /* Translate baud rates from integers to damn B_codes. Unix should
174 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
175
176 static struct
177 {
178 int rate;
179 int code;
180 }
181 baudtab[] =
182 {
183 {50, B50},
184 {75, B75},
185 {110, B110},
186 {134, B134},
187 {150, B150},
188 {200, B200},
189 {300, B300},
190 {600, B600},
191 {1200, B1200},
192 {1800, B1800},
193 {2400, B2400},
194 {4800, B4800},
195 {9600, B9600},
196 {19200, B19200},
197 {38400, B38400},
198 {-1, -1},
199 };
200
201 static int
202 rate_to_code(rate)
203 int rate;
204 {
205 int i;
206
207 for (i = 0; baudtab[i].rate != -1; i++)
208 if (rate == baudtab[i].rate)
209 return baudtab[i].code;
210
211 return -1;
212 }
213
214 static int
215 hardwire_setbaudrate(scb, rate)
216 serial_t scb;
217 int rate;
218 {
219 #ifdef HAVE_TERMIOS
220 struct termios termios;
221
222 if (tcgetattr (scb->fd, &termios))
223 return -1;
224
225 cfsetospeed (&termios, rate_to_code (rate));
226 cfsetispeed (&termios, rate_to_code (rate));
227
228 if (tcsetattr (scb->fd, TCSANOW, &termios))
229 return -1;
230 #endif
231
232 #ifdef HAVE_TERMIO
233 struct termio termio;
234
235 if (ioctl (scb->fd, TCGETA, &termio))
236 return -1;
237
238 #ifndef CIBAUD
239 #define CIBAUD CBAUD
240 #endif
241
242 termio.c_cflag &= ~(CBAUD | CIBAUD);
243 termio.c_cflag |= rate_to_code (rate);
244
245 if (ioctl (scb->fd, TCSETA, &termio))
246 return -1;
247 #endif
248
249 #ifdef HAVE_SGTTY
250 struct sgttyb sgttyb;
251
252 if (ioctl (scb->fd, TIOCGETP, &sgttyb))
253 return -1;
254
255 sgttyb.sg_ispeed = rate_to_code (rate);
256 sgttyb.sg_ospeed = rate_to_code (rate);
257
258 if (ioctl (scb->fd, TIOCSETP, &sgttyb))
259 return -1;
260 #endif
261 return 0;
262 }
263
264 static int
265 hardwire_write(scb, str, len)
266 serial_t scb;
267 const char *str;
268 int len;
269 {
270 int cc;
271
272 while (len > 0)
273 {
274 cc = write(scb->fd, str, len);
275
276 if (cc < 0)
277 return 1;
278 len -= cc;
279 str += cc;
280 }
281 return 0;
282 }
283
284 static void
285 hardwire_restore(scb)
286 serial_t scb;
287 {
288 }
289
290 static void
291 hardwire_close(scb)
292 serial_t scb;
293 {
294 if (scb->fd < 0)
295 return;
296
297 close(scb->fd);
298 scb->fd = -1;
299 }
300
301 static struct serial_ops hardwire_ops =
302 {
303 "hardwire",
304 0,
305 hardwire_open,
306 hardwire_close,
307 hardwire_readchar,
308 hardwire_write,
309 hardwire_raw,
310 hardwire_restore,
311 hardwire_setbaudrate
312 };
313
314 _initialize_ser_hardwire ()
315 {
316 serial_add_interface (&hardwire_ops);
317 }
This page took 0.049377 seconds and 5 git commands to generate.