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