248496e01a36f3325b97515baa3ffeab12b752ff
[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
34 struct hardwire_ttystate
35 {
36 struct termios termios;
37 };
38 #endif
39
40 #ifdef HAVE_TERMIO
41 #include <termio.h>
42
43 struct hardwire_ttystate
44 {
45 struct termio termio;
46 };
47 #endif
48
49 #ifdef HAVE_SGTTY
50 #include <sgtty.h>
51
52 struct hardwire_ttystate
53 {
54 struct sgttyb sgttyb;
55 };
56 #endif
57
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));
71
72 /* Open up a real live device for serial I/O */
73
74 static int
75 hardwire_open(scb, name)
76 serial_t scb;
77 const char *name;
78 {
79 scb->fd = open (name, O_RDWR);
80 if (scb->fd < 0)
81 return -1;
82
83 return 0;
84 }
85
86 static int
87 get_tty_state(scb, state)
88 serial_t scb;
89 struct hardwire_ttystate *state;
90 {
91 #ifdef HAVE_TERMIOS
92 return tcgetattr(scb->fd, &state->termios);
93 #endif
94
95 #ifdef HAVE_TERMIO
96 return ioctl (scb->fd, TCGETA, &state->termio);
97 #endif
98
99 #ifdef HAVE_SGTTY
100 return ioctl (scb->fd, TIOCGETP, &state->sgttyb);
101 #endif
102 }
103
104 static int
105 set_tty_state(scb, state)
106 serial_t scb;
107 struct hardwire_ttystate *state;
108 {
109 int err;
110
111 #ifdef HAVE_TERMIOS
112 return tcsetattr(scb->fd, TCSANOW, &state->termios);
113 #endif
114
115 #ifdef HAVE_TERMIO
116 return ioctl (scb->fd, TCSETA, &state->termio);
117 #endif
118
119 #ifdef HAVE_SGTTY
120 return ioctl (scb->fd, TIOCSETP, &state->sgttyb);
121 #endif
122 }
123
124 static serial_ttystate
125 hardwire_get_tty_state(scb)
126 serial_t scb;
127 {
128 struct hardwire_ttystate *state;
129
130 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
131
132 if (get_tty_state(scb, state))
133 return NULL;
134
135 return (serial_ttystate)state;
136 }
137
138 static int
139 hardwire_set_tty_state(scb, ttystate)
140 serial_t scb;
141 serial_ttystate ttystate;
142 {
143 struct hardwire_ttystate *state;
144
145 state = (struct hardwire_ttystate *)ttystate;
146
147 return set_tty_state(scb, state);
148 }
149
150 static void
151 hardwire_raw(scb)
152 serial_t scb;
153 {
154 struct hardwire_ttystate state;
155
156 if (get_tty_state(scb, &state))
157 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
158
159 #ifdef HAVE_TERMIOS
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;
167 #endif
168
169 #ifdef HAVE_TERMIO
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;
177 #endif
178
179 #ifdef HAVE_SGTTY
180 state.sgttyb.sg_flags |= RAW | ANYP;
181 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
182 #endif
183
184 scb->current_timeout = 0;
185
186 if (set_tty_state (scb, &state))
187 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
188 }
189
190 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
191 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
192
193 For termio{s}, we actually just setup VTIME if necessary, and let the
194 timeout occur in the read() in hardwire_read().
195 */
196
197 static int
198 wait_for(scb, timeout)
199 serial_t scb;
200 int timeout;
201 {
202 int numfds;
203
204 #ifdef HAVE_SGTTY
205 struct timeval tv;
206 fd_set readfds;
207
208 FD_ZERO (&readfds);
209
210 tv.tv_sec = timeout;
211 tv.tv_usec = 0;
212
213 FD_SET(scb->fd, &readfds);
214
215 while (1)
216 {
217 if (timeout >= 0)
218 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
219 else
220 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
221
222 if (numfds <= 0)
223 if (numfds == 0)
224 return SERIAL_TIMEOUT;
225 else if (errno == EINTR)
226 continue;
227 else
228 return SERIAL_ERROR; /* Got an error from select or poll */
229
230 return 0;
231 }
232
233 #endif /* HAVE_SGTTY */
234
235 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
236 if (timeout == scb->current_timeout)
237 return 0;
238
239 {
240 struct hardwire_ttystate state;
241
242 if (get_tty_state(scb, &state))
243 fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
244
245 #ifdef HAVE_TERMIOS
246 state.termios.c_cc[VTIME] = timeout * 10;
247 #endif
248
249 #ifdef HAVE_TERMIO
250 state.termio.c_cc[VTIME] = timeout * 10;
251 #endif
252
253 scb->current_timeout = timeout;
254
255 if (set_tty_state (scb, &state))
256 fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
257
258 return 0;
259 }
260 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
261 }
262
263 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
264 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
265 char if successful. Returns -2 if timeout expired, EOF if line dropped
266 dead, or -3 for any other error (see errno in that case). */
267
268 static int
269 hardwire_readchar(scb, timeout)
270 serial_t scb;
271 int timeout;
272 {
273 int status;
274
275 if (scb->bufcnt-- > 0)
276 return *scb->bufp++;
277
278 status = wait_for(scb, timeout);
279
280 if (status < 0)
281 return status;
282
283 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
284
285 if (scb->bufcnt <= 0)
286 if (scb->bufcnt == 0)
287 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
288 distinguish between EOF & timeouts
289 someday] */
290 else
291 return SERIAL_ERROR; /* Got an error from read */
292
293 scb->bufcnt--;
294 scb->bufp = scb->buf;
295 return *scb->bufp++;
296 }
297
298 #ifndef B19200
299 #define B19200 EXTA
300 #endif
301
302 #ifndef B38400
303 #define B38400 EXTB
304 #endif
305
306 /* Translate baud rates from integers to damn B_codes. Unix should
307 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
308
309 static struct
310 {
311 int rate;
312 int code;
313 }
314 baudtab[] =
315 {
316 {50, B50},
317 {75, B75},
318 {110, B110},
319 {134, B134},
320 {150, B150},
321 {200, B200},
322 {300, B300},
323 {600, B600},
324 {1200, B1200},
325 {1800, B1800},
326 {2400, B2400},
327 {4800, B4800},
328 {9600, B9600},
329 {19200, B19200},
330 {38400, B38400},
331 {-1, -1},
332 };
333
334 static int
335 rate_to_code(rate)
336 int rate;
337 {
338 int i;
339
340 for (i = 0; baudtab[i].rate != -1; i++)
341 if (rate == baudtab[i].rate)
342 return baudtab[i].code;
343
344 return -1;
345 }
346
347 static int
348 hardwire_setbaudrate(scb, rate)
349 serial_t scb;
350 int rate;
351 {
352 struct hardwire_ttystate state;
353
354 if (get_tty_state(scb, &state))
355 return -1;
356
357 #ifdef HAVE_TERMIOS
358 cfsetospeed (&state.termios, rate_to_code (rate));
359 cfsetispeed (&state.termios, rate_to_code (rate));
360 #endif
361
362 #ifdef HAVE_TERMIO
363 #ifndef CIBAUD
364 #define CIBAUD CBAUD
365 #endif
366
367 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
368 state.termio.c_cflag |= rate_to_code (rate);
369 #endif
370
371 #ifdef HAVE_SGTTY
372 state.sgttyb.sg_ispeed = rate_to_code (rate);
373 state.sgttyb.sg_ospeed = rate_to_code (rate);
374 #endif
375
376 return set_tty_state (scb, &state);
377 }
378
379 static int
380 hardwire_write(scb, str, len)
381 serial_t scb;
382 const char *str;
383 int len;
384 {
385 int cc;
386
387 while (len > 0)
388 {
389 cc = write(scb->fd, str, len);
390
391 if (cc < 0)
392 return 1;
393 len -= cc;
394 str += cc;
395 }
396 return 0;
397 }
398
399 static void
400 hardwire_close(scb)
401 serial_t scb;
402 {
403 if (scb->fd < 0)
404 return;
405
406 close(scb->fd);
407 scb->fd = -1;
408 }
409
410 static struct serial_ops hardwire_ops =
411 {
412 "hardwire",
413 0,
414 hardwire_open,
415 hardwire_close,
416 hardwire_readchar,
417 hardwire_write,
418 hardwire_raw,
419 hardwire_get_tty_state,
420 hardwire_set_tty_state,
421 hardwire_setbaudrate,
422 };
423
424 void
425 _initialize_ser_hardwire ()
426 {
427 serial_add_interface (&hardwire_ops);
428 }