gdb/ser-unix.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Serial interface for local (hardwired) serial ports on Un*x like systems

  2.    Copyright (C) 1992-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "serial.h"
  16. #include "ser-base.h"
  17. #include "ser-unix.h"

  18. #include <fcntl.h>
  19. #include <sys/types.h>
  20. #include "terminal.h"
  21. #include <sys/socket.h>
  22. #include <sys/time.h>

  23. #include "gdb_select.h"
  24. #include "gdbcmd.h"
  25. #include "filestuff.h"

  26. #ifdef HAVE_TERMIOS

  27. struct hardwire_ttystate
  28.   {
  29.     struct termios termios;
  30.   };

  31. #ifdef CRTSCTS
  32. /* Boolean to explicitly enable or disable h/w flow control.  */
  33. static int serial_hwflow;
  34. static void
  35. show_serial_hwflow (struct ui_file *file, int from_tty,
  36.                     struct cmd_list_element *c, const char *value)
  37. {
  38.   fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
  39. }
  40. #endif

  41. #endif /* termios */

  42. #ifdef HAVE_TERMIO

  43. /* It is believed that all systems which have added job control to SVR3
  44.    (e.g. sco) have also added termios.  Even if not, trying to figure out
  45.    all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
  46.    bewildering.  So we don't attempt it.  */

  47. struct hardwire_ttystate
  48.   {
  49.     struct termio termio;
  50.   };
  51. #endif /* termio */

  52. #ifdef HAVE_SGTTY
  53. struct hardwire_ttystate
  54.   {
  55.     struct sgttyb sgttyb;
  56.     struct tchars tc;
  57.     struct ltchars ltc;
  58.     /* Line discipline flags.  */
  59.     int lmode;
  60.   };
  61. #endif /* sgtty */

  62. static int hardwire_open (struct serial *scb, const char *name);
  63. static void hardwire_raw (struct serial *scb);
  64. static int wait_for (struct serial *scb, int timeout);
  65. static int hardwire_readchar (struct serial *scb, int timeout);
  66. static int do_hardwire_readchar (struct serial *scb, int timeout);
  67. static int rate_to_code (int rate);
  68. static int hardwire_setbaudrate (struct serial *scb, int rate);
  69. static void hardwire_close (struct serial *scb);
  70. static int get_tty_state (struct serial *scb,
  71.                           struct hardwire_ttystate * state);
  72. static int set_tty_state (struct serial *scb,
  73.                           struct hardwire_ttystate * state);
  74. static serial_ttystate hardwire_get_tty_state (struct serial *scb);
  75. static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
  76. static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
  77.                                            serial_ttystate);
  78. static void hardwire_print_tty_state (struct serial *, serial_ttystate,
  79.                                       struct ui_file *);
  80. static int hardwire_drain_output (struct serial *);
  81. static int hardwire_flush_output (struct serial *);
  82. static int hardwire_flush_input (struct serial *);
  83. static int hardwire_send_break (struct serial *);
  84. static int hardwire_setstopbits (struct serial *, int);

  85. void _initialize_ser_hardwire (void);

  86. /* Open up a real live device for serial I/O.  */

  87. static int
  88. hardwire_open (struct serial *scb, const char *name)
  89. {
  90.   scb->fd = gdb_open_cloexec (name, O_RDWR, 0);
  91.   if (scb->fd < 0)
  92.     return -1;

  93.   return 0;
  94. }

  95. static int
  96. get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
  97. {
  98. #ifdef HAVE_TERMIOS
  99.   if (tcgetattr (scb->fd, &state->termios) < 0)
  100.     return -1;

  101.   return 0;
  102. #endif

  103. #ifdef HAVE_TERMIO
  104.   if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
  105.     return -1;
  106.   return 0;
  107. #endif

  108. #ifdef HAVE_SGTTY
  109.   if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
  110.     return -1;
  111.   if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
  112.     return -1;
  113.   if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
  114.     return -1;
  115.   if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
  116.     return -1;

  117.   return 0;
  118. #endif
  119. }

  120. static int
  121. set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
  122. {
  123. #ifdef HAVE_TERMIOS
  124.   if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
  125.     return -1;

  126.   return 0;
  127. #endif

  128. #ifdef HAVE_TERMIO
  129.   if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
  130.     return -1;
  131.   return 0;
  132. #endif

  133. #ifdef HAVE_SGTTY
  134.   if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
  135.     return -1;
  136.   if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
  137.     return -1;
  138.   if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
  139.     return -1;
  140.   if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
  141.     return -1;

  142.   return 0;
  143. #endif
  144. }

  145. static serial_ttystate
  146. hardwire_get_tty_state (struct serial *scb)
  147. {
  148.   struct hardwire_ttystate *state;

  149.   state = (struct hardwire_ttystate *) xmalloc (sizeof *state);

  150.   if (get_tty_state (scb, state))
  151.     {
  152.       xfree (state);
  153.       return NULL;
  154.     }

  155.   return (serial_ttystate) state;
  156. }

  157. static serial_ttystate
  158. hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
  159. {
  160.   struct hardwire_ttystate *state;

  161.   state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
  162.   *state = *(struct hardwire_ttystate *) ttystate;

  163.   return (serial_ttystate) state;
  164. }

  165. static int
  166. hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
  167. {
  168.   struct hardwire_ttystate *state;

  169.   state = (struct hardwire_ttystate *) ttystate;

  170.   return set_tty_state (scb, state);
  171. }

  172. static int
  173. hardwire_noflush_set_tty_state (struct serial *scb,
  174.                                 serial_ttystate new_ttystate,
  175.                                 serial_ttystate old_ttystate)
  176. {
  177.   struct hardwire_ttystate new_state;
  178. #ifdef HAVE_SGTTY
  179.   struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
  180. #endif

  181.   new_state = *(struct hardwire_ttystate *) new_ttystate;

  182.   /* Don't change in or out of raw mode; we don't want to flush input.
  183.      termio and termios have no such restriction; for them flushing input
  184.      is separate from setting the attributes.  */

  185. #ifdef HAVE_SGTTY
  186.   if (state->sgttyb.sg_flags & RAW)
  187.     new_state.sgttyb.sg_flags |= RAW;
  188.   else
  189.     new_state.sgttyb.sg_flags &= ~RAW;

  190.   /* I'm not sure whether this is necessary; the manpage just mentions
  191.      RAW not CBREAK.  */
  192.   if (state->sgttyb.sg_flags & CBREAK)
  193.     new_state.sgttyb.sg_flags |= CBREAK;
  194.   else
  195.     new_state.sgttyb.sg_flags &= ~CBREAK;
  196. #endif

  197.   return set_tty_state (scb, &new_state);
  198. }

  199. static void
  200. hardwire_print_tty_state (struct serial *scb,
  201.                           serial_ttystate ttystate,
  202.                           struct ui_file *stream)
  203. {
  204.   struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
  205.   int i;

  206. #ifdef HAVE_TERMIOS
  207.   fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
  208.                     (int) state->termios.c_iflag,
  209.                     (int) state->termios.c_oflag);
  210.   fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
  211.                     (int) state->termios.c_cflag,
  212.                     (int) state->termios.c_lflag);
  213. #if 0
  214.   /* This not in POSIX, and is not really documented by those systems
  215.      which have it (at least not Sun).  */
  216.   fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
  217. #endif
  218.   fprintf_filtered (stream, "c_cc: ");
  219.   for (i = 0; i < NCCS; i += 1)
  220.     fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
  221.   fprintf_filtered (stream, "\n");
  222. #endif

  223. #ifdef HAVE_TERMIO
  224.   fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
  225.                     state->termio.c_iflag, state->termio.c_oflag);
  226.   fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
  227.                     state->termio.c_cflag, state->termio.c_lflag,
  228.                     state->termio.c_line);
  229.   fprintf_filtered (stream, "c_cc: ");
  230.   for (i = 0; i < NCC; i += 1)
  231.     fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
  232.   fprintf_filtered (stream, "\n");
  233. #endif

  234. #ifdef HAVE_SGTTY
  235.   fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
  236.                     state->sgttyb.sg_flags);

  237.   fprintf_filtered (stream, "tchars: ");
  238.   for (i = 0; i < (int) sizeof (struct tchars); i++)
  239.     fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
  240.   fprintf_filtered (stream, "\n");

  241.   fprintf_filtered (stream, "ltchars: ");
  242.   for (i = 0; i < (int) sizeof (struct ltchars); i++)
  243.     fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
  244.   fprintf_filtered (stream, "\n");

  245.   fprintf_filtered (stream, "lmode:  0x%x\n", state->lmode);
  246. #endif
  247. }

  248. /* Wait for the output to drain away, as opposed to flushing
  249.    (discarding) it.  */

  250. static int
  251. hardwire_drain_output (struct serial *scb)
  252. {
  253. #ifdef HAVE_TERMIOS
  254.   return tcdrain (scb->fd);
  255. #endif

  256. #ifdef HAVE_TERMIO
  257.   return ioctl (scb->fd, TCSBRK, 1);
  258. #endif

  259. #ifdef HAVE_SGTTY
  260.   /* Get the current state and then restore it using TIOCSETP,
  261.      which should cause the output to drain and pending input
  262.      to be discarded.  */
  263.   {
  264.     struct hardwire_ttystate state;

  265.     if (get_tty_state (scb, &state))
  266.       {
  267.         return (-1);
  268.       }
  269.     else
  270.       {
  271.         return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
  272.       }
  273.   }
  274. #endif
  275. }

  276. static int
  277. hardwire_flush_output (struct serial *scb)
  278. {
  279. #ifdef HAVE_TERMIOS
  280.   return tcflush (scb->fd, TCOFLUSH);
  281. #endif

  282. #ifdef HAVE_TERMIO
  283.   return ioctl (scb->fd, TCFLSH, 1);
  284. #endif

  285. #ifdef HAVE_SGTTY
  286.   /* This flushes both input and output, but we can't do better.  */
  287.   return ioctl (scb->fd, TIOCFLUSH, 0);
  288. #endif
  289. }

  290. static int
  291. hardwire_flush_input (struct serial *scb)
  292. {
  293.   ser_base_flush_input (scb);

  294. #ifdef HAVE_TERMIOS
  295.   return tcflush (scb->fd, TCIFLUSH);
  296. #endif

  297. #ifdef HAVE_TERMIO
  298.   return ioctl (scb->fd, TCFLSH, 0);
  299. #endif

  300. #ifdef HAVE_SGTTY
  301.   /* This flushes both input and output, but we can't do better.  */
  302.   return ioctl (scb->fd, TIOCFLUSH, 0);
  303. #endif
  304. }

  305. static int
  306. hardwire_send_break (struct serial *scb)
  307. {
  308. #ifdef HAVE_TERMIOS
  309.   return tcsendbreak (scb->fd, 0);
  310. #endif

  311. #ifdef HAVE_TERMIO
  312.   return ioctl (scb->fd, TCSBRK, 0);
  313. #endif

  314. #ifdef HAVE_SGTTY
  315.   {
  316.     int status;

  317.     status = ioctl (scb->fd, TIOCSBRK, 0);

  318.     /* Can't use usleep; it doesn't exist in BSD 4.2.  */
  319.     /* Note that if this gdb_select() is interrupted by a signal it will not
  320.        wait the full length of time.  I think that is OK.  */
  321.     gdb_usleep (250000);
  322.     status = ioctl (scb->fd, TIOCCBRK, 0);
  323.     return status;
  324.   }
  325. #endif
  326. }

  327. static void
  328. hardwire_raw (struct serial *scb)
  329. {
  330.   struct hardwire_ttystate state;

  331.   if (get_tty_state (scb, &state))
  332.     fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
  333.                         safe_strerror (errno));

  334. #ifdef HAVE_TERMIOS
  335.   state.termios.c_iflag = 0;
  336.   state.termios.c_oflag = 0;
  337.   state.termios.c_lflag = 0;
  338.   state.termios.c_cflag &= ~(CSIZE | PARENB);
  339.   state.termios.c_cflag |= CLOCAL | CS8;
  340. #ifdef CRTSCTS
  341.   /* h/w flow control.  */
  342.   if (serial_hwflow)
  343.     state.termios.c_cflag |= CRTSCTS;
  344.   else
  345.     state.termios.c_cflag &= ~CRTSCTS;
  346. #ifdef CRTS_IFLOW
  347.   if (serial_hwflow)
  348.     state.termios.c_cflag |= CRTS_IFLOW;
  349.   else
  350.     state.termios.c_cflag &= ~CRTS_IFLOW;
  351. #endif
  352. #endif
  353.   state.termios.c_cc[VMIN] = 0;
  354.   state.termios.c_cc[VTIME] = 0;
  355. #endif

  356. #ifdef HAVE_TERMIO
  357.   state.termio.c_iflag = 0;
  358.   state.termio.c_oflag = 0;
  359.   state.termio.c_lflag = 0;
  360.   state.termio.c_cflag &= ~(CSIZE | PARENB);
  361.   state.termio.c_cflag |= CLOCAL | CS8;
  362.   state.termio.c_cc[VMIN] = 0;
  363.   state.termio.c_cc[VTIME] = 0;
  364. #endif

  365. #ifdef HAVE_SGTTY
  366.   state.sgttyb.sg_flags |= RAW | ANYP;
  367.   state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
  368. #endif

  369.   scb->current_timeout = 0;

  370.   if (set_tty_state (scb, &state))
  371.     fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
  372.                         safe_strerror (errno));
  373. }

  374. /* Wait for input on scb, with timeout seconds.  Returns 0 on success,
  375.    otherwise SERIAL_TIMEOUT or SERIAL_ERROR.

  376.    For termio{s}, we actually just setup VTIME if necessary, and let the
  377.    timeout occur in the read() in hardwire_read().  */

  378. /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
  379.    ser_base*() until the old TERMIOS/SGTTY/... timer code has been
  380.    flushed. .  */

  381. /* NOTE: cagney/1999-09-30: Much of the code below is dead.  The only
  382.    possible values of the TIMEOUT parameter are ONE and ZERO.
  383.    Consequently all the code that tries to handle the possability of
  384.    an overflowed timer is unnecessary.  */

  385. static int
  386. wait_for (struct serial *scb, int timeout)
  387. {
  388. #ifdef HAVE_SGTTY
  389.   while (1)
  390.     {
  391.       struct timeval tv;
  392.       fd_set readfds;
  393.       int numfds;

  394.       /* NOTE: Some OS's can scramble the READFDS when the select()
  395.          call fails (ex the kernel with Red Hat 5.2).  Initialize all
  396.          arguments before each call.  */

  397.       tv.tv_sec = timeout;
  398.       tv.tv_usec = 0;

  399.       FD_ZERO (&readfds);
  400.       FD_SET (scb->fd, &readfds);

  401.       if (timeout >= 0)
  402.         numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv);
  403.       else
  404.         numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0);

  405.       if (numfds <= 0)
  406.         if (numfds == 0)
  407.           return SERIAL_TIMEOUT;
  408.         else if (errno == EINTR)
  409.           continue;
  410.         else
  411.           return SERIAL_ERROR;        /* Got an error from select or poll.  */

  412.       return 0;
  413.     }
  414. #endif /* HAVE_SGTTY */

  415. #if defined HAVE_TERMIO || defined HAVE_TERMIOS
  416.   if (timeout == scb->current_timeout)
  417.     return 0;

  418.   scb->current_timeout = timeout;

  419.   {
  420.     struct hardwire_ttystate state;

  421.     if (get_tty_state (scb, &state))
  422.       fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
  423.                           safe_strerror (errno));

  424. #ifdef HAVE_TERMIOS
  425.     if (timeout < 0)
  426.       {
  427.         /* No timeout.  */
  428.         state.termios.c_cc[VTIME] = 0;
  429.         state.termios.c_cc[VMIN] = 1;
  430.       }
  431.     else
  432.       {
  433.         state.termios.c_cc[VMIN] = 0;
  434.         state.termios.c_cc[VTIME] = timeout * 10;
  435.         if (state.termios.c_cc[VTIME] != timeout * 10)
  436.           {

  437.             /* If c_cc is an 8-bit signed character, we can't go
  438.                bigger than this.  If it is always unsigned, we could use
  439.                25.  */

  440.             scb->current_timeout = 12;
  441.             state.termios.c_cc[VTIME] = scb->current_timeout * 10;
  442.             scb->timeout_remaining = timeout - scb->current_timeout;
  443.           }
  444.       }
  445. #endif

  446. #ifdef HAVE_TERMIO
  447.     if (timeout < 0)
  448.       {
  449.         /* No timeout.  */
  450.         state.termio.c_cc[VTIME] = 0;
  451.         state.termio.c_cc[VMIN] = 1;
  452.       }
  453.     else
  454.       {
  455.         state.termio.c_cc[VMIN] = 0;
  456.         state.termio.c_cc[VTIME] = timeout * 10;
  457.         if (state.termio.c_cc[VTIME] != timeout * 10)
  458.           {
  459.             /* If c_cc is an 8-bit signed character, we can't go
  460.                bigger than this.  If it is always unsigned, we could use
  461.                25.  */

  462.             scb->current_timeout = 12;
  463.             state.termio.c_cc[VTIME] = scb->current_timeout * 10;
  464.             scb->timeout_remaining = timeout - scb->current_timeout;
  465.           }
  466.       }
  467. #endif

  468.     if (set_tty_state (scb, &state))
  469.       fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
  470.                           safe_strerror (errno));

  471.     return 0;
  472.   }
  473. #endif /* HAVE_TERMIO || HAVE_TERMIOS */
  474. }

  475. /* Read a character with user-specified timeoutTIMEOUT is number of
  476.    seconds to wait, or -1 to wait forever.  Use timeout of 0 to effect
  477.    a poll.  Returns char if successful.  Returns SERIAL_TIMEOUT if
  478.    timeout expired, EOF if line dropped dead, or SERIAL_ERROR for any
  479.    other error (see errno in that case).  */

  480. /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
  481.    ser_base*() until the old TERMIOS/SGTTY/... timer code has been
  482.    flushed.  */

  483. /* NOTE: cagney/1999-09-16: This function is not identical to
  484.    ser_base_readchar() as part of replacing it with ser_base*()
  485.    merging will be required - this code handles the case where read()
  486.    times out due to no data while ser_base_readchar() doesn't expect
  487.    that.  */

  488. static int
  489. do_hardwire_readchar (struct serial *scb, int timeout)
  490. {
  491.   int status, delta;
  492.   int detach = 0;

  493.   if (timeout > 0)
  494.     timeout++;

  495.   /* We have to be able to keep the GUI alive here, so we break the
  496.      original timeout into steps of 1 second, running the "keep the
  497.      GUI alive" hook each time through the loop.

  498.      Also, timeout = 0 means to poll, so we just set the delta to 0,
  499.      so we will only go through the loop once.  */

  500.   delta = (timeout == 0 ? 0 : 1);
  501.   while (1)
  502.     {

  503.       /* N.B. The UI may destroy our world (for instance by calling
  504.          remote_stop,) in which case we want to get out of here as
  505.          quickly as possible.  It is not safe to touch scb, since
  506.          someone else might have freed it.  The
  507.          deprecated_ui_loop_hook signals that we should exit by
  508.          returning 1.  */

  509.       if (deprecated_ui_loop_hook)
  510.         detach = deprecated_ui_loop_hook (0);

  511.       if (detach)
  512.         return SERIAL_TIMEOUT;

  513.       scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
  514.       status = wait_for (scb, delta);

  515.       if (status < 0)
  516.         return status;

  517.       status = read (scb->fd, scb->buf, BUFSIZ);

  518.       if (status <= 0)
  519.         {
  520.           if (status == 0)
  521.             {
  522.               /* Zero characters means timeout (it could also be EOF, but
  523.                  we don't (yet at least) distinguish).  */
  524.               if (scb->timeout_remaining > 0)
  525.                 {
  526.                   timeout = scb->timeout_remaining;
  527.                   continue;
  528.                 }
  529.               else if (scb->timeout_remaining < 0)
  530.                 continue;
  531.               else
  532.                 return SERIAL_TIMEOUT;
  533.             }
  534.           else if (errno == EINTR)
  535.             continue;
  536.           else
  537.             return SERIAL_ERROR;        /* Got an error from read.  */
  538.         }

  539.       scb->bufcnt = status;
  540.       scb->bufcnt--;
  541.       scb->bufp = scb->buf;
  542.       return *scb->bufp++;
  543.     }
  544. }

  545. static int
  546. hardwire_readchar (struct serial *scb, int timeout)
  547. {
  548.   return generic_readchar (scb, timeout, do_hardwire_readchar);
  549. }


  550. #ifndef B19200
  551. #define B19200 EXTA
  552. #endif

  553. #ifndef B38400
  554. #define B38400 EXTB
  555. #endif

  556. /* Translate baud rates from integers to damn B_codes.  Unix should
  557.    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */

  558. static struct
  559. {
  560.   int rate;
  561.   int code;
  562. }
  563. baudtab[] =
  564. {
  565.   {
  566.     50, B50
  567.   }
  568.   ,
  569.   {
  570.     75, B75
  571.   }
  572.   ,
  573.   {
  574.     110, B110
  575.   }
  576.   ,
  577.   {
  578.     134, B134
  579.   }
  580.   ,
  581.   {
  582.     150, B150
  583.   }
  584.   ,
  585.   {
  586.     200, B200
  587.   }
  588.   ,
  589.   {
  590.     300, B300
  591.   }
  592.   ,
  593.   {
  594.     600, B600
  595.   }
  596.   ,
  597.   {
  598.     1200, B1200
  599.   }
  600.   ,
  601.   {
  602.     1800, B1800
  603.   }
  604.   ,
  605.   {
  606.     2400, B2400
  607.   }
  608.   ,
  609.   {
  610.     4800, B4800
  611.   }
  612.   ,
  613.   {
  614.     9600, B9600
  615.   }
  616.   ,
  617.   {
  618.     19200, B19200
  619.   }
  620.   ,
  621.   {
  622.     38400, B38400
  623.   }
  624.   ,
  625. #ifdef B57600
  626.   {
  627.     57600, B57600
  628.   }
  629.   ,
  630. #endif
  631. #ifdef B115200
  632.   {
  633.     115200, B115200
  634.   }
  635.   ,
  636. #endif
  637. #ifdef B230400
  638.   {
  639.     230400, B230400
  640.   }
  641.   ,
  642. #endif
  643. #ifdef B460800
  644.   {
  645.     460800, B460800
  646.   }
  647.   ,
  648. #endif
  649.   {
  650.     -1, -1
  651.   }
  652.   ,
  653. };

  654. static int
  655. rate_to_code (int rate)
  656. {
  657.   int i;

  658.   for (i = 0; baudtab[i].rate != -1; i++)
  659.     {
  660.       /* test for perfect macth.  */
  661.       if (rate == baudtab[i].rate)
  662.         return baudtab[i].code;
  663.       else
  664.         {
  665.           /* check if it is in between valid values.  */
  666.           if (rate < baudtab[i].rate)
  667.             {
  668.               if (i)
  669.                 {
  670.                   warning (_("Invalid baud rate %d.  "
  671.                              "Closest values are %d and %d."),
  672.                            rate, baudtab[i - 1].rate, baudtab[i].rate);
  673.                 }
  674.               else
  675.                 {
  676.                   warning (_("Invalid baud rate %d.  Minimum value is %d."),
  677.                            rate, baudtab[0].rate);
  678.                 }
  679.               return -1;
  680.             }
  681.         }
  682.     }

  683.   /* The requested speed was too large.  */
  684.   warning (_("Invalid baud rate %d.  Maximum value is %d."),
  685.             rate, baudtab[i - 1].rate);
  686.   return -1;
  687. }

  688. static int
  689. hardwire_setbaudrate (struct serial *scb, int rate)
  690. {
  691.   struct hardwire_ttystate state;
  692.   int baud_code = rate_to_code (rate);

  693.   if (baud_code < 0)
  694.     {
  695.       /* The baud rate was not valid.
  696.          A warning has already been issued.  */
  697.       errno = EINVAL;
  698.       return -1;
  699.     }

  700.   if (get_tty_state (scb, &state))
  701.     return -1;

  702. #ifdef HAVE_TERMIOS
  703.   cfsetospeed (&state.termios, baud_code);
  704.   cfsetispeed (&state.termios, baud_code);
  705. #endif

  706. #ifdef HAVE_TERMIO
  707. #ifndef CIBAUD
  708. #define CIBAUD CBAUD
  709. #endif

  710.   state.termio.c_cflag &= ~(CBAUD | CIBAUD);
  711.   state.termio.c_cflag |= baud_code;
  712. #endif

  713. #ifdef HAVE_SGTTY
  714.   state.sgttyb.sg_ispeed = baud_code;
  715.   state.sgttyb.sg_ospeed = baud_code;
  716. #endif

  717.   return set_tty_state (scb, &state);
  718. }

  719. static int
  720. hardwire_setstopbits (struct serial *scb, int num)
  721. {
  722.   struct hardwire_ttystate state;
  723.   int newbit;

  724.   if (get_tty_state (scb, &state))
  725.     return -1;

  726.   switch (num)
  727.     {
  728.     case SERIAL_1_STOPBITS:
  729.       newbit = 0;
  730.       break;
  731.     case SERIAL_1_AND_A_HALF_STOPBITS:
  732.     case SERIAL_2_STOPBITS:
  733.       newbit = 1;
  734.       break;
  735.     default:
  736.       return 1;
  737.     }

  738. #ifdef HAVE_TERMIOS
  739.   if (!newbit)
  740.     state.termios.c_cflag &= ~CSTOPB;
  741.   else
  742.     state.termios.c_cflag |= CSTOPB;        /* two bits */
  743. #endif

  744. #ifdef HAVE_TERMIO
  745.   if (!newbit)
  746.     state.termio.c_cflag &= ~CSTOPB;
  747.   else
  748.     state.termio.c_cflag |= CSTOPB;        /* two bits */
  749. #endif

  750. #ifdef HAVE_SGTTY
  751.   return 0;                        /* sgtty doesn't support this */
  752. #endif

  753.   return set_tty_state (scb, &state);
  754. }

  755. static void
  756. hardwire_close (struct serial *scb)
  757. {
  758.   if (scb->fd < 0)
  759.     return;

  760.   close (scb->fd);
  761.   scb->fd = -1;
  762. }



  763. /* The hardwire ops.  */

  764. static const struct serial_ops hardwire_ops =
  765. {
  766.   "hardwire",
  767.   hardwire_open,
  768.   hardwire_close,
  769.   NULL,
  770.   /* FIXME: Don't replace this with the equivalent ser_base*() until
  771.      the old TERMIOS/SGTTY/... timer code has been flushed.  cagney
  772.      1999-09-16.  */
  773.   hardwire_readchar,
  774.   ser_base_write,
  775.   hardwire_flush_output,
  776.   hardwire_flush_input,
  777.   hardwire_send_break,
  778.   hardwire_raw,
  779.   hardwire_get_tty_state,
  780.   hardwire_copy_tty_state,
  781.   hardwire_set_tty_state,
  782.   hardwire_print_tty_state,
  783.   hardwire_noflush_set_tty_state,
  784.   hardwire_setbaudrate,
  785.   hardwire_setstopbits,
  786.   hardwire_drain_output,
  787.   ser_base_async,
  788.   ser_unix_read_prim,
  789.   ser_unix_write_prim
  790. };

  791. void
  792. _initialize_ser_hardwire (void)
  793. {
  794.   serial_add_interface (&hardwire_ops);

  795. #ifdef HAVE_TERMIOS
  796. #ifdef CRTSCTS
  797.   add_setshow_boolean_cmd ("remoteflow", no_class,
  798.                            &serial_hwflow, _("\
  799. Set use of hardware flow control for remote serial I/O."), _("\
  800. Show use of hardware flow control for remote serial I/O."), _("\
  801. Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
  802. when debugging using remote targets."),
  803.                            NULL,
  804.                            show_serial_hwflow,
  805.                            &setlist, &showlist);
  806. #endif
  807. #endif
  808. }

  809. int
  810. ser_unix_read_prim (struct serial *scb, size_t count)
  811. {
  812.   int status;

  813.   while (1)
  814.     {
  815.       status = read (scb->fd, scb->buf, count);
  816.       if (status != -1 || errno != EINTR)
  817.         break;
  818.     }
  819.   return status;
  820. }

  821. int
  822. ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
  823. {
  824.   /* ??? Historically, GDB has not retried calls to "write" that
  825.      result in EINTR.  */
  826.   return write (scb->fd, buf, len);
  827. }