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