*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2003,
4 2004, 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include "defs.h"
22 #include "serial.h"
23 #include "ser-base.h"
24 #include "ser-unix.h"
25
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include "terminal.h"
29 #include <sys/socket.h>
30 #include <sys/time.h>
31
32 #include "gdb_select.h"
33 #include "gdb_string.h"
34 #include "gdbcmd.h"
35
36 #ifdef HAVE_TERMIOS
37
38 struct hardwire_ttystate
39 {
40 struct termios termios;
41 };
42
43 #ifdef CRTSCTS
44 /* Boolean to explicitly enable or disable h/w flow control. */
45 static int serial_hwflow;
46 static void
47 show_serial_hwflow (struct ui_file *file, int from_tty,
48 struct cmd_list_element *c, const char *value)
49 {
50 fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
51 }
52 #endif
53
54 #endif /* termios */
55
56 #ifdef HAVE_TERMIO
57
58 /* It is believed that all systems which have added job control to SVR3
59 (e.g. sco) have also added termios. Even if not, trying to figure out
60 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
61 bewildering. So we don't attempt it. */
62
63 struct hardwire_ttystate
64 {
65 struct termio termio;
66 };
67 #endif /* termio */
68
69 #ifdef HAVE_SGTTY
70 struct hardwire_ttystate
71 {
72 struct sgttyb sgttyb;
73 struct tchars tc;
74 struct ltchars ltc;
75 /* Line discipline flags. */
76 int lmode;
77 };
78 #endif /* sgtty */
79
80 static int hardwire_open (struct serial *scb, const char *name);
81 static void hardwire_raw (struct serial *scb);
82 static int wait_for (struct serial *scb, int timeout);
83 static int hardwire_readchar (struct serial *scb, int timeout);
84 static int do_hardwire_readchar (struct serial *scb, int timeout);
85 static int rate_to_code (int rate);
86 static int hardwire_setbaudrate (struct serial *scb, int rate);
87 static void hardwire_close (struct serial *scb);
88 static int get_tty_state (struct serial *scb,
89 struct hardwire_ttystate * state);
90 static int set_tty_state (struct serial *scb,
91 struct hardwire_ttystate * state);
92 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
93 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
94 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
95 serial_ttystate);
96 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
97 struct ui_file *);
98 static int hardwire_drain_output (struct serial *);
99 static int hardwire_flush_output (struct serial *);
100 static int hardwire_flush_input (struct serial *);
101 static int hardwire_send_break (struct serial *);
102 static int hardwire_setstopbits (struct serial *, int);
103
104 void _initialize_ser_hardwire (void);
105
106 /* Open up a real live device for serial I/O. */
107
108 static int
109 hardwire_open (struct serial *scb, const char *name)
110 {
111 scb->fd = open (name, O_RDWR);
112 if (scb->fd < 0)
113 return -1;
114
115 return 0;
116 }
117
118 static int
119 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
120 {
121 #ifdef HAVE_TERMIOS
122 if (tcgetattr (scb->fd, &state->termios) < 0)
123 return -1;
124
125 return 0;
126 #endif
127
128 #ifdef HAVE_TERMIO
129 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
130 return -1;
131 return 0;
132 #endif
133
134 #ifdef HAVE_SGTTY
135 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
136 return -1;
137 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
138 return -1;
139 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
140 return -1;
141 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
142 return -1;
143
144 return 0;
145 #endif
146 }
147
148 static int
149 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
150 {
151 #ifdef HAVE_TERMIOS
152 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
153 return -1;
154
155 return 0;
156 #endif
157
158 #ifdef HAVE_TERMIO
159 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
160 return -1;
161 return 0;
162 #endif
163
164 #ifdef HAVE_SGTTY
165 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
166 return -1;
167 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
168 return -1;
169 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
170 return -1;
171 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
172 return -1;
173
174 return 0;
175 #endif
176 }
177
178 static serial_ttystate
179 hardwire_get_tty_state (struct serial *scb)
180 {
181 struct hardwire_ttystate *state;
182
183 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
184
185 if (get_tty_state (scb, state))
186 return NULL;
187
188 return (serial_ttystate) state;
189 }
190
191 static int
192 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
193 {
194 struct hardwire_ttystate *state;
195
196 state = (struct hardwire_ttystate *) ttystate;
197
198 return set_tty_state (scb, state);
199 }
200
201 static int
202 hardwire_noflush_set_tty_state (struct serial *scb,
203 serial_ttystate new_ttystate,
204 serial_ttystate old_ttystate)
205 {
206 struct hardwire_ttystate new_state;
207 #ifdef HAVE_SGTTY
208 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
209 #endif
210
211 new_state = *(struct hardwire_ttystate *) new_ttystate;
212
213 /* Don't change in or out of raw mode; we don't want to flush input.
214 termio and termios have no such restriction; for them flushing input
215 is separate from setting the attributes. */
216
217 #ifdef HAVE_SGTTY
218 if (state->sgttyb.sg_flags & RAW)
219 new_state.sgttyb.sg_flags |= RAW;
220 else
221 new_state.sgttyb.sg_flags &= ~RAW;
222
223 /* I'm not sure whether this is necessary; the manpage just mentions
224 RAW not CBREAK. */
225 if (state->sgttyb.sg_flags & CBREAK)
226 new_state.sgttyb.sg_flags |= CBREAK;
227 else
228 new_state.sgttyb.sg_flags &= ~CBREAK;
229 #endif
230
231 return set_tty_state (scb, &new_state);
232 }
233
234 static void
235 hardwire_print_tty_state (struct serial *scb,
236 serial_ttystate ttystate,
237 struct ui_file *stream)
238 {
239 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
240 int i;
241
242 #ifdef HAVE_TERMIOS
243 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
244 (int) state->termios.c_iflag,
245 (int) state->termios.c_oflag);
246 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
247 (int) state->termios.c_cflag,
248 (int) state->termios.c_lflag);
249 #if 0
250 /* This not in POSIX, and is not really documented by those systems
251 which have it (at least not Sun). */
252 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
253 #endif
254 fprintf_filtered (stream, "c_cc: ");
255 for (i = 0; i < NCCS; i += 1)
256 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
257 fprintf_filtered (stream, "\n");
258 #endif
259
260 #ifdef HAVE_TERMIO
261 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
262 state->termio.c_iflag, state->termio.c_oflag);
263 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
264 state->termio.c_cflag, state->termio.c_lflag,
265 state->termio.c_line);
266 fprintf_filtered (stream, "c_cc: ");
267 for (i = 0; i < NCC; i += 1)
268 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
269 fprintf_filtered (stream, "\n");
270 #endif
271
272 #ifdef HAVE_SGTTY
273 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
274 state->sgttyb.sg_flags);
275
276 fprintf_filtered (stream, "tchars: ");
277 for (i = 0; i < (int) sizeof (struct tchars); i++)
278 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
279 fprintf_filtered (stream, "\n");
280
281 fprintf_filtered (stream, "ltchars: ");
282 for (i = 0; i < (int) sizeof (struct ltchars); i++)
283 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
284 fprintf_filtered (stream, "\n");
285
286 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
287 #endif
288 }
289
290 /* Wait for the output to drain away, as opposed to flushing
291 (discarding) it. */
292
293 static int
294 hardwire_drain_output (struct serial *scb)
295 {
296 #ifdef HAVE_TERMIOS
297 return tcdrain (scb->fd);
298 #endif
299
300 #ifdef HAVE_TERMIO
301 return ioctl (scb->fd, TCSBRK, 1);
302 #endif
303
304 #ifdef HAVE_SGTTY
305 /* Get the current state and then restore it using TIOCSETP,
306 which should cause the output to drain and pending input
307 to be discarded. */
308 {
309 struct hardwire_ttystate state;
310
311 if (get_tty_state (scb, &state))
312 {
313 return (-1);
314 }
315 else
316 {
317 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
318 }
319 }
320 #endif
321 }
322
323 static int
324 hardwire_flush_output (struct serial *scb)
325 {
326 #ifdef HAVE_TERMIOS
327 return tcflush (scb->fd, TCOFLUSH);
328 #endif
329
330 #ifdef HAVE_TERMIO
331 return ioctl (scb->fd, TCFLSH, 1);
332 #endif
333
334 #ifdef HAVE_SGTTY
335 /* This flushes both input and output, but we can't do better. */
336 return ioctl (scb->fd, TIOCFLUSH, 0);
337 #endif
338 }
339
340 static int
341 hardwire_flush_input (struct serial *scb)
342 {
343 ser_base_flush_input (scb);
344
345 #ifdef HAVE_TERMIOS
346 return tcflush (scb->fd, TCIFLUSH);
347 #endif
348
349 #ifdef HAVE_TERMIO
350 return ioctl (scb->fd, TCFLSH, 0);
351 #endif
352
353 #ifdef HAVE_SGTTY
354 /* This flushes both input and output, but we can't do better. */
355 return ioctl (scb->fd, TIOCFLUSH, 0);
356 #endif
357 }
358
359 static int
360 hardwire_send_break (struct serial *scb)
361 {
362 #ifdef HAVE_TERMIOS
363 return tcsendbreak (scb->fd, 0);
364 #endif
365
366 #ifdef HAVE_TERMIO
367 return ioctl (scb->fd, TCSBRK, 0);
368 #endif
369
370 #ifdef HAVE_SGTTY
371 {
372 int status;
373
374 status = ioctl (scb->fd, TIOCSBRK, 0);
375
376 /* Can't use usleep; it doesn't exist in BSD 4.2. */
377 /* Note that if this gdb_select() is interrupted by a signal it will not
378 wait the full length of time. I think that is OK. */
379 gdb_usleep (250000);
380 status = ioctl (scb->fd, TIOCCBRK, 0);
381 return status;
382 }
383 #endif
384 }
385
386 static void
387 hardwire_raw (struct serial *scb)
388 {
389 struct hardwire_ttystate state;
390
391 if (get_tty_state (scb, &state))
392 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
393 safe_strerror (errno));
394
395 #ifdef HAVE_TERMIOS
396 state.termios.c_iflag = 0;
397 state.termios.c_oflag = 0;
398 state.termios.c_lflag = 0;
399 state.termios.c_cflag &= ~(CSIZE | PARENB);
400 state.termios.c_cflag |= CLOCAL | CS8;
401 #ifdef CRTSCTS
402 /* h/w flow control. */
403 if (serial_hwflow)
404 state.termios.c_cflag |= CRTSCTS;
405 else
406 state.termios.c_cflag &= ~CRTSCTS;
407 #ifdef CRTS_IFLOW
408 if (serial_hwflow)
409 state.termios.c_cflag |= CRTS_IFLOW;
410 else
411 state.termios.c_cflag &= ~CRTS_IFLOW;
412 #endif
413 #endif
414 state.termios.c_cc[VMIN] = 0;
415 state.termios.c_cc[VTIME] = 0;
416 #endif
417
418 #ifdef HAVE_TERMIO
419 state.termio.c_iflag = 0;
420 state.termio.c_oflag = 0;
421 state.termio.c_lflag = 0;
422 state.termio.c_cflag &= ~(CSIZE | PARENB);
423 state.termio.c_cflag |= CLOCAL | CS8;
424 state.termio.c_cc[VMIN] = 0;
425 state.termio.c_cc[VTIME] = 0;
426 #endif
427
428 #ifdef HAVE_SGTTY
429 state.sgttyb.sg_flags |= RAW | ANYP;
430 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
431 #endif
432
433 scb->current_timeout = 0;
434
435 if (set_tty_state (scb, &state))
436 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
437 safe_strerror (errno));
438 }
439
440 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
441 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
442
443 For termio{s}, we actually just setup VTIME if necessary, and let the
444 timeout occur in the read() in hardwire_read(). */
445
446 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
447 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
448 flushed. . */
449
450 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
451 possible values of the TIMEOUT parameter are ONE and ZERO.
452 Consequently all the code that tries to handle the possability of
453 an overflowed timer is unnecessary. */
454
455 static int
456 wait_for (struct serial *scb, int timeout)
457 {
458 #ifdef HAVE_SGTTY
459 while (1)
460 {
461 struct timeval tv;
462 fd_set readfds;
463 int numfds;
464
465 /* NOTE: Some OS's can scramble the READFDS when the select()
466 call fails (ex the kernel with Red Hat 5.2). Initialize all
467 arguments before each call. */
468
469 tv.tv_sec = timeout;
470 tv.tv_usec = 0;
471
472 FD_ZERO (&readfds);
473 FD_SET (scb->fd, &readfds);
474
475 if (timeout >= 0)
476 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv);
477 else
478 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0);
479
480 if (numfds <= 0)
481 if (numfds == 0)
482 return SERIAL_TIMEOUT;
483 else if (errno == EINTR)
484 continue;
485 else
486 return SERIAL_ERROR; /* Got an error from select or poll. */
487
488 return 0;
489 }
490 #endif /* HAVE_SGTTY */
491
492 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
493 if (timeout == scb->current_timeout)
494 return 0;
495
496 scb->current_timeout = timeout;
497
498 {
499 struct hardwire_ttystate state;
500
501 if (get_tty_state (scb, &state))
502 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
503 safe_strerror (errno));
504
505 #ifdef HAVE_TERMIOS
506 if (timeout < 0)
507 {
508 /* No timeout. */
509 state.termios.c_cc[VTIME] = 0;
510 state.termios.c_cc[VMIN] = 1;
511 }
512 else
513 {
514 state.termios.c_cc[VMIN] = 0;
515 state.termios.c_cc[VTIME] = timeout * 10;
516 if (state.termios.c_cc[VTIME] != timeout * 10)
517 {
518
519 /* If c_cc is an 8-bit signed character, we can't go
520 bigger than this. If it is always unsigned, we could use
521 25. */
522
523 scb->current_timeout = 12;
524 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
525 scb->timeout_remaining = timeout - scb->current_timeout;
526 }
527 }
528 #endif
529
530 #ifdef HAVE_TERMIO
531 if (timeout < 0)
532 {
533 /* No timeout. */
534 state.termio.c_cc[VTIME] = 0;
535 state.termio.c_cc[VMIN] = 1;
536 }
537 else
538 {
539 state.termio.c_cc[VMIN] = 0;
540 state.termio.c_cc[VTIME] = timeout * 10;
541 if (state.termio.c_cc[VTIME] != timeout * 10)
542 {
543 /* If c_cc is an 8-bit signed character, we can't go
544 bigger than this. If it is always unsigned, we could use
545 25. */
546
547 scb->current_timeout = 12;
548 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
549 scb->timeout_remaining = timeout - scb->current_timeout;
550 }
551 }
552 #endif
553
554 if (set_tty_state (scb, &state))
555 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
556 safe_strerror (errno));
557
558 return 0;
559 }
560 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
561 }
562
563 /* Read a character with user-specified timeout. TIMEOUT is number of
564 seconds to wait, or -1 to wait forever. Use timeout of 0 to effect
565 a poll. Returns char if successful. Returns SERIAL_TIMEOUT if
566 timeout expired, EOF if line dropped dead, or SERIAL_ERROR for any
567 other error (see errno in that case). */
568
569 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
570 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
571 flushed. */
572
573 /* NOTE: cagney/1999-09-16: This function is not identical to
574 ser_base_readchar() as part of replacing it with ser_base*()
575 merging will be required - this code handles the case where read()
576 times out due to no data while ser_base_readchar() doesn't expect
577 that. */
578
579 static int
580 do_hardwire_readchar (struct serial *scb, int timeout)
581 {
582 int status, delta;
583 int detach = 0;
584
585 if (timeout > 0)
586 timeout++;
587
588 /* We have to be able to keep the GUI alive here, so we break the
589 original timeout into steps of 1 second, running the "keep the
590 GUI alive" hook each time through the loop.
591
592 Also, timeout = 0 means to poll, so we just set the delta to 0,
593 so we will only go through the loop once. */
594
595 delta = (timeout == 0 ? 0 : 1);
596 while (1)
597 {
598
599 /* N.B. The UI may destroy our world (for instance by calling
600 remote_stop,) in which case we want to get out of here as
601 quickly as possible. It is not safe to touch scb, since
602 someone else might have freed it. The
603 deprecated_ui_loop_hook signals that we should exit by
604 returning 1. */
605
606 if (deprecated_ui_loop_hook)
607 detach = deprecated_ui_loop_hook (0);
608
609 if (detach)
610 return SERIAL_TIMEOUT;
611
612 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
613 status = wait_for (scb, delta);
614
615 if (status < 0)
616 return status;
617
618 status = read (scb->fd, scb->buf, BUFSIZ);
619
620 if (status <= 0)
621 {
622 if (status == 0)
623 {
624 /* Zero characters means timeout (it could also be EOF, but
625 we don't (yet at least) distinguish). */
626 if (scb->timeout_remaining > 0)
627 {
628 timeout = scb->timeout_remaining;
629 continue;
630 }
631 else if (scb->timeout_remaining < 0)
632 continue;
633 else
634 return SERIAL_TIMEOUT;
635 }
636 else if (errno == EINTR)
637 continue;
638 else
639 return SERIAL_ERROR; /* Got an error from read. */
640 }
641
642 scb->bufcnt = status;
643 scb->bufcnt--;
644 scb->bufp = scb->buf;
645 return *scb->bufp++;
646 }
647 }
648
649 static int
650 hardwire_readchar (struct serial *scb, int timeout)
651 {
652 return generic_readchar (scb, timeout, do_hardwire_readchar);
653 }
654
655
656 #ifndef B19200
657 #define B19200 EXTA
658 #endif
659
660 #ifndef B38400
661 #define B38400 EXTB
662 #endif
663
664 /* Translate baud rates from integers to damn B_codes. Unix should
665 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
666
667 static struct
668 {
669 int rate;
670 int code;
671 }
672 baudtab[] =
673 {
674 {
675 50, B50
676 }
677 ,
678 {
679 75, B75
680 }
681 ,
682 {
683 110, B110
684 }
685 ,
686 {
687 134, B134
688 }
689 ,
690 {
691 150, B150
692 }
693 ,
694 {
695 200, B200
696 }
697 ,
698 {
699 300, B300
700 }
701 ,
702 {
703 600, B600
704 }
705 ,
706 {
707 1200, B1200
708 }
709 ,
710 {
711 1800, B1800
712 }
713 ,
714 {
715 2400, B2400
716 }
717 ,
718 {
719 4800, B4800
720 }
721 ,
722 {
723 9600, B9600
724 }
725 ,
726 {
727 19200, B19200
728 }
729 ,
730 {
731 38400, B38400
732 }
733 ,
734 #ifdef B57600
735 {
736 57600, B57600
737 }
738 ,
739 #endif
740 #ifdef B115200
741 {
742 115200, B115200
743 }
744 ,
745 #endif
746 #ifdef B230400
747 {
748 230400, B230400
749 }
750 ,
751 #endif
752 #ifdef B460800
753 {
754 460800, B460800
755 }
756 ,
757 #endif
758 {
759 -1, -1
760 }
761 ,
762 };
763
764 static int
765 rate_to_code (int rate)
766 {
767 int i;
768
769 for (i = 0; baudtab[i].rate != -1; i++)
770 {
771 /* test for perfect macth. */
772 if (rate == baudtab[i].rate)
773 return baudtab[i].code;
774 else
775 {
776 /* check if it is in between valid values. */
777 if (rate < baudtab[i].rate)
778 {
779 if (i)
780 {
781 warning (_("Invalid baud rate %d. "
782 "Closest values are %d and %d."),
783 rate, baudtab[i - 1].rate, baudtab[i].rate);
784 }
785 else
786 {
787 warning (_("Invalid baud rate %d. Minimum value is %d."),
788 rate, baudtab[0].rate);
789 }
790 return -1;
791 }
792 }
793 }
794
795 /* The requested speed was too large. */
796 warning (_("Invalid baud rate %d. Maximum value is %d."),
797 rate, baudtab[i - 1].rate);
798 return -1;
799 }
800
801 static int
802 hardwire_setbaudrate (struct serial *scb, int rate)
803 {
804 struct hardwire_ttystate state;
805 int baud_code = rate_to_code (rate);
806
807 if (baud_code < 0)
808 {
809 /* The baud rate was not valid.
810 A warning has already been issued. */
811 errno = EINVAL;
812 return -1;
813 }
814
815 if (get_tty_state (scb, &state))
816 return -1;
817
818 #ifdef HAVE_TERMIOS
819 cfsetospeed (&state.termios, baud_code);
820 cfsetispeed (&state.termios, baud_code);
821 #endif
822
823 #ifdef HAVE_TERMIO
824 #ifndef CIBAUD
825 #define CIBAUD CBAUD
826 #endif
827
828 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
829 state.termio.c_cflag |= baud_code;
830 #endif
831
832 #ifdef HAVE_SGTTY
833 state.sgttyb.sg_ispeed = baud_code;
834 state.sgttyb.sg_ospeed = baud_code;
835 #endif
836
837 return set_tty_state (scb, &state);
838 }
839
840 static int
841 hardwire_setstopbits (struct serial *scb, int num)
842 {
843 struct hardwire_ttystate state;
844 int newbit;
845
846 if (get_tty_state (scb, &state))
847 return -1;
848
849 switch (num)
850 {
851 case SERIAL_1_STOPBITS:
852 newbit = 0;
853 break;
854 case SERIAL_1_AND_A_HALF_STOPBITS:
855 case SERIAL_2_STOPBITS:
856 newbit = 1;
857 break;
858 default:
859 return 1;
860 }
861
862 #ifdef HAVE_TERMIOS
863 if (!newbit)
864 state.termios.c_cflag &= ~CSTOPB;
865 else
866 state.termios.c_cflag |= CSTOPB; /* two bits */
867 #endif
868
869 #ifdef HAVE_TERMIO
870 if (!newbit)
871 state.termio.c_cflag &= ~CSTOPB;
872 else
873 state.termio.c_cflag |= CSTOPB; /* two bits */
874 #endif
875
876 #ifdef HAVE_SGTTY
877 return 0; /* sgtty doesn't support this */
878 #endif
879
880 return set_tty_state (scb, &state);
881 }
882
883 static void
884 hardwire_close (struct serial *scb)
885 {
886 if (scb->fd < 0)
887 return;
888
889 close (scb->fd);
890 scb->fd = -1;
891 }
892 \f
893 \f
894 void
895 _initialize_ser_hardwire (void)
896 {
897 struct serial_ops *ops = XMALLOC (struct serial_ops);
898
899 memset (ops, 0, sizeof (struct serial_ops));
900 ops->name = "hardwire";
901 ops->next = 0;
902 ops->open = hardwire_open;
903 ops->close = hardwire_close;
904 /* FIXME: Don't replace this with the equivalent ser_base*() until
905 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
906 1999-09-16. */
907 ops->readchar = hardwire_readchar;
908 ops->write = ser_base_write;
909 ops->flush_output = hardwire_flush_output;
910 ops->flush_input = hardwire_flush_input;
911 ops->send_break = hardwire_send_break;
912 ops->go_raw = hardwire_raw;
913 ops->get_tty_state = hardwire_get_tty_state;
914 ops->set_tty_state = hardwire_set_tty_state;
915 ops->print_tty_state = hardwire_print_tty_state;
916 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
917 ops->setbaudrate = hardwire_setbaudrate;
918 ops->setstopbits = hardwire_setstopbits;
919 ops->drain_output = hardwire_drain_output;
920 ops->async = ser_base_async;
921 ops->read_prim = ser_unix_read_prim;
922 ops->write_prim = ser_unix_write_prim;
923 serial_add_interface (ops);
924
925 #ifdef HAVE_TERMIOS
926 #ifdef CRTSCTS
927 add_setshow_boolean_cmd ("remoteflow", no_class,
928 &serial_hwflow, _("\
929 Set use of hardware flow control for remote serial I/O."), _("\
930 Show use of hardware flow control for remote serial I/O."), _("\
931 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
932 when debugging using remote targets."),
933 NULL,
934 show_serial_hwflow,
935 &setlist, &showlist);
936 #endif
937 #endif
938 }
939
940 int
941 ser_unix_read_prim (struct serial *scb, size_t count)
942 {
943 int status;
944
945 while (1)
946 {
947 status = read (scb->fd, scb->buf, count);
948 if (status != -1 || errno != EINTR)
949 break;
950 }
951 return status;
952 }
953
954 int
955 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
956 {
957 /* ??? Historically, GDB has not retried calls to "write" that
958 result in EINTR. */
959 return write (scb->fd, buf, len);
960 }
This page took 0.048719 seconds and 4 git commands to generate.