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