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