0e490fb377f6315cd54d0b945b9015608bb0c891
[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
25 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
26 #define HAVE_SGTTY
27 #endif
28
29 #ifdef HAVE_TERMIOS
30 #include <termios.h>
31 #include <unistd.h>
32
33 struct hardwire_ttystate
34 {
35 struct termios termios;
36 };
37 #endif /* termios */
38
39 #ifdef HAVE_TERMIO
40 #include <termio.h>
41
42 /* It is believed that all systems which have added job control to SVR3
43 (e.g. sco) have also added termios. Even if not, trying to figure out
44 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
45 bewildering. So we don't attempt it. */
46
47 struct hardwire_ttystate
48 {
49 struct termio termio;
50 };
51 #endif /* termio */
52
53 #ifdef HAVE_SGTTY
54 /* Needed for the code which uses select(). We would include <sys/select.h>
55 too if it existed on all systems. */
56 #include <sys/time.h>
57
58 #include <sgtty.h>
59
60 struct hardwire_ttystate
61 {
62 struct sgttyb sgttyb;
63 struct tchars tc;
64 struct ltchars ltc;
65 /* Line discipline flags. */
66 int lmode;
67 };
68 #endif /* sgtty */
69
70 static int hardwire_open PARAMS ((serial_t scb, const char *name));
71 static void hardwire_raw PARAMS ((serial_t scb));
72 static int wait_for PARAMS ((serial_t scb, int timeout));
73 static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
74 static int rate_to_code PARAMS ((int rate));
75 static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
76 static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
77 /* FIXME: static void hardwire_restore PARAMS ((serial_t scb)); */
78 static void hardwire_close PARAMS ((serial_t scb));
79 static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
80 static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
81 static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
82 static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
83
84 /* Open up a real live device for serial I/O */
85
86 static int
87 hardwire_open(scb, name)
88 serial_t scb;
89 const char *name;
90 {
91 scb->fd = open (name, O_RDWR);
92 if (scb->fd < 0)
93 return -1;
94
95 return 0;
96 }
97
98 static int
99 get_tty_state(scb, state)
100 serial_t scb;
101 struct hardwire_ttystate *state;
102 {
103 #ifdef HAVE_TERMIOS
104 extern int errno;
105
106 if (tcgetattr(scb->fd, &state->termios) < 0)
107 return -1;
108
109 return 0;
110 #endif
111
112 #ifdef HAVE_TERMIO
113 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
114 return -1;
115 return 0;
116 #endif
117
118 #ifdef HAVE_SGTTY
119 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
120 return -1;
121 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
122 return -1;
123 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
124 return -1;
125 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
126 return -1;
127
128 return 0;
129 #endif
130 }
131
132 static int
133 set_tty_state(scb, state)
134 serial_t scb;
135 struct hardwire_ttystate *state;
136 {
137 #ifdef HAVE_TERMIOS
138 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
139 return -1;
140
141 return 0;
142 #endif
143
144 #ifdef HAVE_TERMIO
145 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
146 return -1;
147 return 0;
148 #endif
149
150 #ifdef HAVE_SGTTY
151 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
152 return -1;
153 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
154 return -1;
155 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
156 return -1;
157 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
158 return -1;
159
160 return 0;
161 #endif
162 }
163
164 static serial_ttystate
165 hardwire_get_tty_state(scb)
166 serial_t scb;
167 {
168 struct hardwire_ttystate *state;
169
170 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
171
172 if (get_tty_state(scb, state))
173 return NULL;
174
175 return (serial_ttystate)state;
176 }
177
178 static int
179 hardwire_set_tty_state(scb, ttystate)
180 serial_t scb;
181 serial_ttystate ttystate;
182 {
183 struct hardwire_ttystate *state;
184
185 state = (struct hardwire_ttystate *)ttystate;
186
187 return set_tty_state(scb, state);
188 }
189
190 static int
191 hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
192 serial_t scb;
193 serial_ttystate new_ttystate;
194 serial_ttystate old_ttystate;
195 {
196 struct hardwire_ttystate new_state;
197 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
198
199 new_state = *(struct hardwire_ttystate *)new_ttystate;
200
201 /* Don't change in or out of raw mode; we don't want to flush input.
202 termio and termios have no such restriction; for them flushing input
203 is separate from setting the attributes. */
204
205 #ifdef HAVE_SGTTY
206 if (state->sgttyb.sg_flags & RAW)
207 new_state.sgttyb.sg_flags |= RAW;
208 else
209 new_state.sgttyb.sg_flags &= ~RAW;
210
211 /* I'm not sure whether this is necessary; the manpage just mentions
212 RAW not CBREAK. */
213 if (state->sgttyb.sg_flags & CBREAK)
214 new_state.sgttyb.sg_flags |= CBREAK;
215 else
216 new_state.sgttyb.sg_flags &= ~CBREAK;
217 #endif
218
219 return set_tty_state (scb, &new_state);
220 }
221
222 static void
223 hardwire_print_tty_state (scb, ttystate)
224 serial_t scb;
225 serial_ttystate ttystate;
226 {
227 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
228 int i;
229
230 #ifdef HAVE_TERMIOS
231 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
232 state->termios.c_iflag, state->termios.c_oflag);
233 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
234 state->termios.c_cflag, state->termios.c_lflag);
235 #if 0
236 /* This not in POSIX, and is not really documented by those systems
237 which have it (at least not Sun). */
238 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
239 #endif
240 printf_filtered ("c_cc: ");
241 for (i = 0; i < NCCS; i += 1)
242 printf_filtered ("0x%x ", state->termios.c_cc[i]);
243 printf_filtered ("\n");
244 #endif
245
246 #ifdef HAVE_TERMIO
247 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
248 state->termio.c_iflag, state->termio.c_oflag);
249 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
250 state->termio.c_cflag, state->termio.c_lflag,
251 state->termio.c_line);
252 printf_filtered ("c_cc: ");
253 for (i = 0; i < NCC; i += 1)
254 printf_filtered ("0x%x ", state->termio.c_cc[i]);
255 printf_filtered ("\n");
256 #endif
257
258 #ifdef HAVE_SGTTY
259 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
260
261 printf_filtered ("tchars: ");
262 for (i = 0; i < (int)sizeof (struct tchars); i++)
263 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
264 printf_filtered ("\n");
265
266 printf_filtered ("ltchars: ");
267 for (i = 0; i < (int)sizeof (struct ltchars); i++)
268 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
269 printf_filtered ("\n");
270
271 printf_filtered ("lmode: 0x%x\n", state->lmode);
272 #endif
273 }
274
275 static int
276 hardwire_flush_output (scb)
277 serial_t scb;
278 {
279 #ifdef HAVE_TERMIOS
280 return tcflush (scb->fd, TCOFLUSH);
281 #endif
282
283 #ifdef HAVE_TERMIO
284 return ioctl (scb->fd, TCFLSH, 1);
285 #endif
286
287 #ifdef HAVE_SGTTY
288 /* This flushes both input and output, but we can't do better. */
289 return ioctl (scb->fd, TIOCFLUSH, 0);
290 #endif
291 }
292
293 static int
294 hardwire_flush_input (scb)
295 serial_t scb;
296 {
297 #ifdef HAVE_TERMIOS
298 return tcflush (scb->fd, TCIFLUSH);
299 #endif
300
301 #ifdef HAVE_TERMIO
302 return ioctl (scb->fd, TCFLSH, 0);
303 #endif
304
305 #ifdef HAVE_SGTTY
306 /* This flushes both input and output, but we can't do better. */
307 return ioctl (scb->fd, TIOCFLUSH, 0);
308 #endif
309 }
310
311 static int
312 hardwire_send_break (scb)
313 serial_t scb;
314 {
315 #ifdef HAVE_TERMIOS
316 return tcsendbreak (scb->fd, 0);
317 #endif
318
319 #ifdef HAVE_TERMIO
320 return ioctl (scb->fd, TCSBRK, 0);
321 #endif
322
323 #ifdef HAVE_SGTTY
324 {
325 int status;
326 struct timeval timeout;
327
328 status = ioctl (scb->fd, TIOCSBRK, 0);
329
330 /* Can't use usleep; it doesn't exist in BSD 4.2. */
331 /* Note that if this select() is interrupted by a signal it will not wait
332 the full length of time. I think that is OK. */
333 timeout.tv_sec = 0;
334 timeout.tv_usec = 250000;
335 select (0, 0, 0, 0, &timeout);
336 status = ioctl (scb->fd, TIOCCBRK, 0);
337 return status;
338 }
339 #endif
340 }
341
342 static void
343 hardwire_raw(scb)
344 serial_t scb;
345 {
346 struct hardwire_ttystate state;
347
348 if (get_tty_state(scb, &state))
349 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
350
351 #ifdef HAVE_TERMIOS
352 state.termios.c_iflag = 0;
353 state.termios.c_oflag = 0;
354 state.termios.c_lflag = 0;
355 state.termios.c_cflag &= ~(CSIZE|PARENB);
356 state.termios.c_cflag |= CS8;
357 state.termios.c_cc[VMIN] = 0;
358 state.termios.c_cc[VTIME] = 0;
359 #endif
360
361 #ifdef HAVE_TERMIO
362 state.termio.c_iflag = 0;
363 state.termio.c_oflag = 0;
364 state.termio.c_lflag = 0;
365 state.termio.c_cflag &= ~(CSIZE|PARENB);
366 state.termio.c_cflag |= CS8;
367 state.termio.c_cc[VMIN] = 0;
368 state.termio.c_cc[VTIME] = 0;
369 #endif
370
371 #ifdef HAVE_SGTTY
372 state.sgttyb.sg_flags |= RAW | ANYP;
373 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
374 #endif
375
376 scb->current_timeout = 0;
377
378 if (set_tty_state (scb, &state))
379 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
380 }
381
382 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
383 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
384
385 For termio{s}, we actually just setup VTIME if necessary, and let the
386 timeout occur in the read() in hardwire_read().
387 */
388
389 static int
390 wait_for(scb, timeout)
391 serial_t scb;
392 int timeout;
393 {
394 #ifdef HAVE_SGTTY
395 struct timeval tv;
396 fd_set readfds;
397
398 FD_ZERO (&readfds);
399
400 tv.tv_sec = timeout;
401 tv.tv_usec = 0;
402
403 FD_SET(scb->fd, &readfds);
404
405 while (1)
406 {
407 int numfds;
408
409 if (timeout >= 0)
410 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
411 else
412 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
413
414 if (numfds <= 0)
415 if (numfds == 0)
416 return SERIAL_TIMEOUT;
417 else if (errno == EINTR)
418 continue;
419 else
420 return SERIAL_ERROR; /* Got an error from select or poll */
421
422 return 0;
423 }
424
425 #endif /* HAVE_SGTTY */
426
427 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
428 if (timeout == scb->current_timeout)
429 return 0;
430
431 {
432 struct hardwire_ttystate state;
433
434 if (get_tty_state(scb, &state))
435 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
436
437 #ifdef HAVE_TERMIOS
438 if (timeout < 0)
439 {
440 /* No timeout. */
441 state.termios.c_cc[VTIME] = 0;
442 state.termios.c_cc[VMIN] = 1;
443 }
444 else
445 {
446 state.termios.c_cc[VMIN] = 0;
447 state.termios.c_cc[VTIME] = timeout * 10;
448 if (state.termios.c_cc[VTIME] != timeout * 10)
449 {
450 warning ("Timeout value %d too large, using %d", timeout,
451 state.termios.c_cc[VTIME] / 10);
452 }
453 }
454 #endif
455
456 #ifdef HAVE_TERMIO
457 if (timeout < 0)
458 {
459 /* No timeout. */
460 state.termio.c_cc[VTIME] = 0;
461 state.termio.c_cc[VMIN] = 1;
462 }
463 else
464 {
465 state.termio.c_cc[VMIN] = 0;
466 state.termio.c_cc[VTIME] = timeout * 10;
467 if (state.termio.c_cc[VTIME] != timeout * 10)
468 {
469 warning ("Timeout value %d too large, using %d", timeout,
470 state.termio.c_cc[VTIME] / 10);
471 }
472 }
473 #endif
474
475 scb->current_timeout = timeout;
476
477 if (set_tty_state (scb, &state))
478 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
479
480 return 0;
481 }
482 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
483 }
484
485 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
486 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
487 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
488 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
489
490 static int
491 hardwire_readchar(scb, timeout)
492 serial_t scb;
493 int timeout;
494 {
495 int status;
496
497 if (scb->bufcnt-- > 0)
498 return *scb->bufp++;
499
500 status = wait_for(scb, timeout);
501
502 if (status < 0)
503 return status;
504
505 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
506
507 if (scb->bufcnt <= 0)
508 if (scb->bufcnt == 0)
509 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
510 distinguish between EOF & timeouts
511 someday] */
512 else
513 return SERIAL_ERROR; /* Got an error from read */
514
515 scb->bufcnt--;
516 scb->bufp = scb->buf;
517 return *scb->bufp++;
518 }
519
520 #ifndef B19200
521 #define B19200 EXTA
522 #endif
523
524 #ifndef B38400
525 #define B38400 EXTB
526 #endif
527
528 /* Translate baud rates from integers to damn B_codes. Unix should
529 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
530
531 static struct
532 {
533 int rate;
534 int code;
535 }
536 baudtab[] =
537 {
538 {50, B50},
539 {75, B75},
540 {110, B110},
541 {134, B134},
542 {150, B150},
543 {200, B200},
544 {300, B300},
545 {600, B600},
546 {1200, B1200},
547 {1800, B1800},
548 {2400, B2400},
549 {4800, B4800},
550 {9600, B9600},
551 {19200, B19200},
552 {38400, B38400},
553 {-1, -1},
554 };
555
556 static int
557 rate_to_code(rate)
558 int rate;
559 {
560 int i;
561
562 for (i = 0; baudtab[i].rate != -1; i++)
563 if (rate == baudtab[i].rate)
564 return baudtab[i].code;
565
566 return -1;
567 }
568
569 static int
570 hardwire_setbaudrate(scb, rate)
571 serial_t scb;
572 int rate;
573 {
574 struct hardwire_ttystate state;
575
576 if (get_tty_state(scb, &state))
577 return -1;
578
579 #ifdef HAVE_TERMIOS
580 cfsetospeed (&state.termios, rate_to_code (rate));
581 cfsetispeed (&state.termios, rate_to_code (rate));
582 #endif
583
584 #ifdef HAVE_TERMIO
585 #ifndef CIBAUD
586 #define CIBAUD CBAUD
587 #endif
588
589 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
590 state.termio.c_cflag |= rate_to_code (rate);
591 #endif
592
593 #ifdef HAVE_SGTTY
594 state.sgttyb.sg_ispeed = rate_to_code (rate);
595 state.sgttyb.sg_ospeed = rate_to_code (rate);
596 #endif
597
598 return set_tty_state (scb, &state);
599 }
600
601 static int
602 hardwire_write(scb, str, len)
603 serial_t scb;
604 const char *str;
605 int len;
606 {
607 int cc;
608
609 while (len > 0)
610 {
611 cc = write(scb->fd, str, len);
612
613 if (cc < 0)
614 return 1;
615 len -= cc;
616 str += cc;
617 }
618 return 0;
619 }
620
621 static void
622 hardwire_close(scb)
623 serial_t scb;
624 {
625 if (scb->fd < 0)
626 return;
627
628 close(scb->fd);
629 scb->fd = -1;
630 }
631
632 static struct serial_ops hardwire_ops =
633 {
634 "hardwire",
635 0,
636 hardwire_open,
637 hardwire_close,
638 hardwire_readchar,
639 hardwire_write,
640 hardwire_flush_output,
641 hardwire_flush_input,
642 hardwire_send_break,
643 hardwire_raw,
644 hardwire_get_tty_state,
645 hardwire_set_tty_state,
646 hardwire_print_tty_state,
647 hardwire_noflush_set_tty_state,
648 hardwire_setbaudrate,
649 };
650
651 void
652 _initialize_ser_hardwire ()
653 {
654 serial_add_interface (&hardwire_ops);
655 }