xref: /dragonfly/contrib/gdb-7/gdb/ser-unix.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Serial interface for local (hardwired) serial ports on Un*x like systems
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1992-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "serial.h"
225796c8dcSSimon Schubert #include "ser-base.h"
235796c8dcSSimon Schubert #include "ser-unix.h"
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert #include <fcntl.h>
265796c8dcSSimon Schubert #include <sys/types.h>
275796c8dcSSimon Schubert #include "terminal.h"
285796c8dcSSimon Schubert #include <sys/socket.h>
295796c8dcSSimon Schubert #include <sys/time.h>
305796c8dcSSimon Schubert 
315796c8dcSSimon Schubert #include "gdb_select.h"
325796c8dcSSimon Schubert #include "gdb_string.h"
335796c8dcSSimon Schubert #include "gdbcmd.h"
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
365796c8dcSSimon Schubert 
375796c8dcSSimon Schubert struct hardwire_ttystate
385796c8dcSSimon Schubert   {
395796c8dcSSimon Schubert     struct termios termios;
405796c8dcSSimon Schubert   };
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert #ifdef CRTSCTS
435796c8dcSSimon Schubert /* Boolean to explicitly enable or disable h/w flow control.  */
445796c8dcSSimon Schubert static int serial_hwflow;
455796c8dcSSimon Schubert static void
show_serial_hwflow(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)465796c8dcSSimon Schubert show_serial_hwflow (struct ui_file *file, int from_tty,
475796c8dcSSimon Schubert 		    struct cmd_list_element *c, const char *value)
485796c8dcSSimon Schubert {
495796c8dcSSimon Schubert   fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
505796c8dcSSimon Schubert }
515796c8dcSSimon Schubert #endif
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert #endif /* termios */
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert #ifdef HAVE_TERMIO
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert /* It is believed that all systems which have added job control to SVR3
585796c8dcSSimon Schubert    (e.g. sco) have also added termios.  Even if not, trying to figure out
595796c8dcSSimon Schubert    all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
605796c8dcSSimon Schubert    bewildering.  So we don't attempt it.  */
615796c8dcSSimon Schubert 
625796c8dcSSimon Schubert struct hardwire_ttystate
635796c8dcSSimon Schubert   {
645796c8dcSSimon Schubert     struct termio termio;
655796c8dcSSimon Schubert   };
665796c8dcSSimon Schubert #endif /* termio */
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert #ifdef HAVE_SGTTY
695796c8dcSSimon Schubert struct hardwire_ttystate
705796c8dcSSimon Schubert   {
715796c8dcSSimon Schubert     struct sgttyb sgttyb;
725796c8dcSSimon Schubert     struct tchars tc;
735796c8dcSSimon Schubert     struct ltchars ltc;
745796c8dcSSimon Schubert     /* Line discipline flags.  */
755796c8dcSSimon Schubert     int lmode;
765796c8dcSSimon Schubert   };
775796c8dcSSimon Schubert #endif /* sgtty */
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert static int hardwire_open (struct serial *scb, const char *name);
805796c8dcSSimon Schubert static void hardwire_raw (struct serial *scb);
815796c8dcSSimon Schubert static int wait_for (struct serial *scb, int timeout);
825796c8dcSSimon Schubert static int hardwire_readchar (struct serial *scb, int timeout);
835796c8dcSSimon Schubert static int do_hardwire_readchar (struct serial *scb, int timeout);
845796c8dcSSimon Schubert static int rate_to_code (int rate);
855796c8dcSSimon Schubert static int hardwire_setbaudrate (struct serial *scb, int rate);
865796c8dcSSimon Schubert static void hardwire_close (struct serial *scb);
875796c8dcSSimon Schubert static int get_tty_state (struct serial *scb,
885796c8dcSSimon Schubert 			  struct hardwire_ttystate * state);
895796c8dcSSimon Schubert static int set_tty_state (struct serial *scb,
905796c8dcSSimon Schubert 			  struct hardwire_ttystate * state);
915796c8dcSSimon Schubert static serial_ttystate hardwire_get_tty_state (struct serial *scb);
925796c8dcSSimon Schubert static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
935796c8dcSSimon Schubert static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
945796c8dcSSimon Schubert 					   serial_ttystate);
955796c8dcSSimon Schubert static void hardwire_print_tty_state (struct serial *, serial_ttystate,
965796c8dcSSimon Schubert 				      struct ui_file *);
975796c8dcSSimon Schubert static int hardwire_drain_output (struct serial *);
985796c8dcSSimon Schubert static int hardwire_flush_output (struct serial *);
995796c8dcSSimon Schubert static int hardwire_flush_input (struct serial *);
1005796c8dcSSimon Schubert static int hardwire_send_break (struct serial *);
1015796c8dcSSimon Schubert static int hardwire_setstopbits (struct serial *, int);
1025796c8dcSSimon Schubert 
1035796c8dcSSimon Schubert void _initialize_ser_hardwire (void);
1045796c8dcSSimon Schubert 
105c50c785cSJohn Marino /* Open up a real live device for serial I/O.  */
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert static int
hardwire_open(struct serial * scb,const char * name)1085796c8dcSSimon Schubert hardwire_open (struct serial *scb, const char *name)
1095796c8dcSSimon Schubert {
1105796c8dcSSimon Schubert   scb->fd = open (name, O_RDWR);
1115796c8dcSSimon Schubert   if (scb->fd < 0)
1125796c8dcSSimon Schubert     return -1;
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert   return 0;
1155796c8dcSSimon Schubert }
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert static int
get_tty_state(struct serial * scb,struct hardwire_ttystate * state)1185796c8dcSSimon Schubert get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
1195796c8dcSSimon Schubert {
1205796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
1215796c8dcSSimon Schubert   if (tcgetattr (scb->fd, &state->termios) < 0)
1225796c8dcSSimon Schubert     return -1;
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert   return 0;
1255796c8dcSSimon Schubert #endif
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert #ifdef HAVE_TERMIO
1285796c8dcSSimon Schubert   if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
1295796c8dcSSimon Schubert     return -1;
1305796c8dcSSimon Schubert   return 0;
1315796c8dcSSimon Schubert #endif
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert #ifdef HAVE_SGTTY
1345796c8dcSSimon Schubert   if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
1355796c8dcSSimon Schubert     return -1;
1365796c8dcSSimon Schubert   if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
1375796c8dcSSimon Schubert     return -1;
1385796c8dcSSimon Schubert   if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
1395796c8dcSSimon Schubert     return -1;
1405796c8dcSSimon Schubert   if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
1415796c8dcSSimon Schubert     return -1;
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert   return 0;
1445796c8dcSSimon Schubert #endif
1455796c8dcSSimon Schubert }
1465796c8dcSSimon Schubert 
1475796c8dcSSimon Schubert static int
set_tty_state(struct serial * scb,struct hardwire_ttystate * state)1485796c8dcSSimon Schubert set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
1495796c8dcSSimon Schubert {
1505796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
1515796c8dcSSimon Schubert   if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
1525796c8dcSSimon Schubert     return -1;
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert   return 0;
1555796c8dcSSimon Schubert #endif
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert #ifdef HAVE_TERMIO
1585796c8dcSSimon Schubert   if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
1595796c8dcSSimon Schubert     return -1;
1605796c8dcSSimon Schubert   return 0;
1615796c8dcSSimon Schubert #endif
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert #ifdef HAVE_SGTTY
1645796c8dcSSimon Schubert   if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
1655796c8dcSSimon Schubert     return -1;
1665796c8dcSSimon Schubert   if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
1675796c8dcSSimon Schubert     return -1;
1685796c8dcSSimon Schubert   if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
1695796c8dcSSimon Schubert     return -1;
1705796c8dcSSimon Schubert   if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
1715796c8dcSSimon Schubert     return -1;
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert   return 0;
1745796c8dcSSimon Schubert #endif
1755796c8dcSSimon Schubert }
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert static serial_ttystate
hardwire_get_tty_state(struct serial * scb)1785796c8dcSSimon Schubert hardwire_get_tty_state (struct serial *scb)
1795796c8dcSSimon Schubert {
1805796c8dcSSimon Schubert   struct hardwire_ttystate *state;
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert   state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert   if (get_tty_state (scb, state))
185c50c785cSJohn Marino     {
186c50c785cSJohn Marino       xfree (state);
1875796c8dcSSimon Schubert       return NULL;
188c50c785cSJohn Marino     }
189c50c785cSJohn Marino 
190c50c785cSJohn Marino   return (serial_ttystate) state;
191c50c785cSJohn Marino }
192c50c785cSJohn Marino 
193c50c785cSJohn Marino static serial_ttystate
hardwire_copy_tty_state(struct serial * scb,serial_ttystate ttystate)194c50c785cSJohn Marino hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
195c50c785cSJohn Marino {
196c50c785cSJohn Marino   struct hardwire_ttystate *state;
197c50c785cSJohn Marino 
198c50c785cSJohn Marino   state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
199c50c785cSJohn Marino   *state = *(struct hardwire_ttystate *) ttystate;
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert   return (serial_ttystate) state;
2025796c8dcSSimon Schubert }
2035796c8dcSSimon Schubert 
2045796c8dcSSimon Schubert static int
hardwire_set_tty_state(struct serial * scb,serial_ttystate ttystate)2055796c8dcSSimon Schubert hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
2065796c8dcSSimon Schubert {
2075796c8dcSSimon Schubert   struct hardwire_ttystate *state;
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert   state = (struct hardwire_ttystate *) ttystate;
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert   return set_tty_state (scb, state);
2125796c8dcSSimon Schubert }
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert static int
hardwire_noflush_set_tty_state(struct serial * scb,serial_ttystate new_ttystate,serial_ttystate old_ttystate)2155796c8dcSSimon Schubert hardwire_noflush_set_tty_state (struct serial *scb,
2165796c8dcSSimon Schubert 				serial_ttystate new_ttystate,
2175796c8dcSSimon Schubert 				serial_ttystate old_ttystate)
2185796c8dcSSimon Schubert {
2195796c8dcSSimon Schubert   struct hardwire_ttystate new_state;
2205796c8dcSSimon Schubert #ifdef HAVE_SGTTY
2215796c8dcSSimon Schubert   struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
2225796c8dcSSimon Schubert #endif
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert   new_state = *(struct hardwire_ttystate *) new_ttystate;
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert   /* Don't change in or out of raw mode; we don't want to flush input.
2275796c8dcSSimon Schubert      termio and termios have no such restriction; for them flushing input
2285796c8dcSSimon Schubert      is separate from setting the attributes.  */
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert #ifdef HAVE_SGTTY
2315796c8dcSSimon Schubert   if (state->sgttyb.sg_flags & RAW)
2325796c8dcSSimon Schubert     new_state.sgttyb.sg_flags |= RAW;
2335796c8dcSSimon Schubert   else
2345796c8dcSSimon Schubert     new_state.sgttyb.sg_flags &= ~RAW;
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert   /* I'm not sure whether this is necessary; the manpage just mentions
2375796c8dcSSimon Schubert      RAW not CBREAK.  */
2385796c8dcSSimon Schubert   if (state->sgttyb.sg_flags & CBREAK)
2395796c8dcSSimon Schubert     new_state.sgttyb.sg_flags |= CBREAK;
2405796c8dcSSimon Schubert   else
2415796c8dcSSimon Schubert     new_state.sgttyb.sg_flags &= ~CBREAK;
2425796c8dcSSimon Schubert #endif
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert   return set_tty_state (scb, &new_state);
2455796c8dcSSimon Schubert }
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert static void
hardwire_print_tty_state(struct serial * scb,serial_ttystate ttystate,struct ui_file * stream)2485796c8dcSSimon Schubert hardwire_print_tty_state (struct serial *scb,
2495796c8dcSSimon Schubert 			  serial_ttystate ttystate,
2505796c8dcSSimon Schubert 			  struct ui_file *stream)
2515796c8dcSSimon Schubert {
2525796c8dcSSimon Schubert   struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
2535796c8dcSSimon Schubert   int i;
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
2565796c8dcSSimon Schubert   fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
2575796c8dcSSimon Schubert 		    (int) state->termios.c_iflag,
2585796c8dcSSimon Schubert 		    (int) state->termios.c_oflag);
2595796c8dcSSimon Schubert   fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
2605796c8dcSSimon Schubert 		    (int) state->termios.c_cflag,
2615796c8dcSSimon Schubert 		    (int) state->termios.c_lflag);
2625796c8dcSSimon Schubert #if 0
2635796c8dcSSimon Schubert   /* This not in POSIX, and is not really documented by those systems
2645796c8dcSSimon Schubert      which have it (at least not Sun).  */
2655796c8dcSSimon Schubert   fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
2665796c8dcSSimon Schubert #endif
2675796c8dcSSimon Schubert   fprintf_filtered (stream, "c_cc: ");
2685796c8dcSSimon Schubert   for (i = 0; i < NCCS; i += 1)
2695796c8dcSSimon Schubert     fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
2705796c8dcSSimon Schubert   fprintf_filtered (stream, "\n");
2715796c8dcSSimon Schubert #endif
2725796c8dcSSimon Schubert 
2735796c8dcSSimon Schubert #ifdef HAVE_TERMIO
2745796c8dcSSimon Schubert   fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
2755796c8dcSSimon Schubert 		    state->termio.c_iflag, state->termio.c_oflag);
2765796c8dcSSimon Schubert   fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
2775796c8dcSSimon Schubert 		    state->termio.c_cflag, state->termio.c_lflag,
2785796c8dcSSimon Schubert 		    state->termio.c_line);
2795796c8dcSSimon Schubert   fprintf_filtered (stream, "c_cc: ");
2805796c8dcSSimon Schubert   for (i = 0; i < NCC; i += 1)
2815796c8dcSSimon Schubert     fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
2825796c8dcSSimon Schubert   fprintf_filtered (stream, "\n");
2835796c8dcSSimon Schubert #endif
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert #ifdef HAVE_SGTTY
2865796c8dcSSimon Schubert   fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
2875796c8dcSSimon Schubert 		    state->sgttyb.sg_flags);
2885796c8dcSSimon Schubert 
2895796c8dcSSimon Schubert   fprintf_filtered (stream, "tchars: ");
2905796c8dcSSimon Schubert   for (i = 0; i < (int) sizeof (struct tchars); i++)
2915796c8dcSSimon Schubert     fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
2925796c8dcSSimon Schubert   fprintf_filtered (stream, "\n");
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert   fprintf_filtered (stream, "ltchars: ");
2955796c8dcSSimon Schubert   for (i = 0; i < (int) sizeof (struct ltchars); i++)
2965796c8dcSSimon Schubert     fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
2975796c8dcSSimon Schubert   fprintf_filtered (stream, "\n");
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert   fprintf_filtered (stream, "lmode:  0x%x\n", state->lmode);
3005796c8dcSSimon Schubert #endif
3015796c8dcSSimon Schubert }
3025796c8dcSSimon Schubert 
303c50c785cSJohn Marino /* Wait for the output to drain away, as opposed to flushing
304c50c785cSJohn Marino    (discarding) it.  */
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert static int
hardwire_drain_output(struct serial * scb)3075796c8dcSSimon Schubert hardwire_drain_output (struct serial *scb)
3085796c8dcSSimon Schubert {
3095796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
3105796c8dcSSimon Schubert   return tcdrain (scb->fd);
3115796c8dcSSimon Schubert #endif
3125796c8dcSSimon Schubert 
3135796c8dcSSimon Schubert #ifdef HAVE_TERMIO
3145796c8dcSSimon Schubert   return ioctl (scb->fd, TCSBRK, 1);
3155796c8dcSSimon Schubert #endif
3165796c8dcSSimon Schubert 
3175796c8dcSSimon Schubert #ifdef HAVE_SGTTY
3185796c8dcSSimon Schubert   /* Get the current state and then restore it using TIOCSETP,
3195796c8dcSSimon Schubert      which should cause the output to drain and pending input
3205796c8dcSSimon Schubert      to be discarded.  */
3215796c8dcSSimon Schubert   {
3225796c8dcSSimon Schubert     struct hardwire_ttystate state;
323cf7f2e2dSJohn Marino 
3245796c8dcSSimon Schubert     if (get_tty_state (scb, &state))
3255796c8dcSSimon Schubert       {
3265796c8dcSSimon Schubert 	return (-1);
3275796c8dcSSimon Schubert       }
3285796c8dcSSimon Schubert     else
3295796c8dcSSimon Schubert       {
3305796c8dcSSimon Schubert 	return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
3315796c8dcSSimon Schubert       }
3325796c8dcSSimon Schubert   }
3335796c8dcSSimon Schubert #endif
3345796c8dcSSimon Schubert }
3355796c8dcSSimon Schubert 
3365796c8dcSSimon Schubert static int
hardwire_flush_output(struct serial * scb)3375796c8dcSSimon Schubert hardwire_flush_output (struct serial *scb)
3385796c8dcSSimon Schubert {
3395796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
3405796c8dcSSimon Schubert   return tcflush (scb->fd, TCOFLUSH);
3415796c8dcSSimon Schubert #endif
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert #ifdef HAVE_TERMIO
3445796c8dcSSimon Schubert   return ioctl (scb->fd, TCFLSH, 1);
3455796c8dcSSimon Schubert #endif
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert #ifdef HAVE_SGTTY
3485796c8dcSSimon Schubert   /* This flushes both input and output, but we can't do better.  */
3495796c8dcSSimon Schubert   return ioctl (scb->fd, TIOCFLUSH, 0);
3505796c8dcSSimon Schubert #endif
3515796c8dcSSimon Schubert }
3525796c8dcSSimon Schubert 
3535796c8dcSSimon Schubert static int
hardwire_flush_input(struct serial * scb)3545796c8dcSSimon Schubert hardwire_flush_input (struct serial *scb)
3555796c8dcSSimon Schubert {
3565796c8dcSSimon Schubert   ser_base_flush_input (scb);
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
3595796c8dcSSimon Schubert   return tcflush (scb->fd, TCIFLUSH);
3605796c8dcSSimon Schubert #endif
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert #ifdef HAVE_TERMIO
3635796c8dcSSimon Schubert   return ioctl (scb->fd, TCFLSH, 0);
3645796c8dcSSimon Schubert #endif
3655796c8dcSSimon Schubert 
3665796c8dcSSimon Schubert #ifdef HAVE_SGTTY
3675796c8dcSSimon Schubert   /* This flushes both input and output, but we can't do better.  */
3685796c8dcSSimon Schubert   return ioctl (scb->fd, TIOCFLUSH, 0);
3695796c8dcSSimon Schubert #endif
3705796c8dcSSimon Schubert }
3715796c8dcSSimon Schubert 
3725796c8dcSSimon Schubert static int
hardwire_send_break(struct serial * scb)3735796c8dcSSimon Schubert hardwire_send_break (struct serial *scb)
3745796c8dcSSimon Schubert {
3755796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
3765796c8dcSSimon Schubert   return tcsendbreak (scb->fd, 0);
3775796c8dcSSimon Schubert #endif
3785796c8dcSSimon Schubert 
3795796c8dcSSimon Schubert #ifdef HAVE_TERMIO
3805796c8dcSSimon Schubert   return ioctl (scb->fd, TCSBRK, 0);
3815796c8dcSSimon Schubert #endif
3825796c8dcSSimon Schubert 
3835796c8dcSSimon Schubert #ifdef HAVE_SGTTY
3845796c8dcSSimon Schubert   {
3855796c8dcSSimon Schubert     int status;
3865796c8dcSSimon Schubert 
3875796c8dcSSimon Schubert     status = ioctl (scb->fd, TIOCSBRK, 0);
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert     /* Can't use usleep; it doesn't exist in BSD 4.2.  */
3905796c8dcSSimon Schubert     /* Note that if this gdb_select() is interrupted by a signal it will not
3915796c8dcSSimon Schubert        wait the full length of time.  I think that is OK.  */
3925796c8dcSSimon Schubert     gdb_usleep (250000);
3935796c8dcSSimon Schubert     status = ioctl (scb->fd, TIOCCBRK, 0);
3945796c8dcSSimon Schubert     return status;
3955796c8dcSSimon Schubert   }
3965796c8dcSSimon Schubert #endif
3975796c8dcSSimon Schubert }
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert static void
hardwire_raw(struct serial * scb)4005796c8dcSSimon Schubert hardwire_raw (struct serial *scb)
4015796c8dcSSimon Schubert {
4025796c8dcSSimon Schubert   struct hardwire_ttystate state;
4035796c8dcSSimon Schubert 
4045796c8dcSSimon Schubert   if (get_tty_state (scb, &state))
405c50c785cSJohn Marino     fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
406c50c785cSJohn Marino 			safe_strerror (errno));
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
4095796c8dcSSimon Schubert   state.termios.c_iflag = 0;
4105796c8dcSSimon Schubert   state.termios.c_oflag = 0;
4115796c8dcSSimon Schubert   state.termios.c_lflag = 0;
4125796c8dcSSimon Schubert   state.termios.c_cflag &= ~(CSIZE | PARENB);
4135796c8dcSSimon Schubert   state.termios.c_cflag |= CLOCAL | CS8;
4145796c8dcSSimon Schubert #ifdef CRTSCTS
4155796c8dcSSimon Schubert   /* h/w flow control.  */
4165796c8dcSSimon Schubert   if (serial_hwflow)
4175796c8dcSSimon Schubert     state.termios.c_cflag |= CRTSCTS;
4185796c8dcSSimon Schubert   else
4195796c8dcSSimon Schubert     state.termios.c_cflag &= ~CRTSCTS;
4205796c8dcSSimon Schubert #ifdef CRTS_IFLOW
4215796c8dcSSimon Schubert   if (serial_hwflow)
4225796c8dcSSimon Schubert     state.termios.c_cflag |= CRTS_IFLOW;
4235796c8dcSSimon Schubert   else
4245796c8dcSSimon Schubert     state.termios.c_cflag &= ~CRTS_IFLOW;
4255796c8dcSSimon Schubert #endif
4265796c8dcSSimon Schubert #endif
4275796c8dcSSimon Schubert   state.termios.c_cc[VMIN] = 0;
4285796c8dcSSimon Schubert   state.termios.c_cc[VTIME] = 0;
4295796c8dcSSimon Schubert #endif
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert #ifdef HAVE_TERMIO
4325796c8dcSSimon Schubert   state.termio.c_iflag = 0;
4335796c8dcSSimon Schubert   state.termio.c_oflag = 0;
4345796c8dcSSimon Schubert   state.termio.c_lflag = 0;
4355796c8dcSSimon Schubert   state.termio.c_cflag &= ~(CSIZE | PARENB);
4365796c8dcSSimon Schubert   state.termio.c_cflag |= CLOCAL | CS8;
4375796c8dcSSimon Schubert   state.termio.c_cc[VMIN] = 0;
4385796c8dcSSimon Schubert   state.termio.c_cc[VTIME] = 0;
4395796c8dcSSimon Schubert #endif
4405796c8dcSSimon Schubert 
4415796c8dcSSimon Schubert #ifdef HAVE_SGTTY
4425796c8dcSSimon Schubert   state.sgttyb.sg_flags |= RAW | ANYP;
4435796c8dcSSimon Schubert   state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
4445796c8dcSSimon Schubert #endif
4455796c8dcSSimon Schubert 
4465796c8dcSSimon Schubert   scb->current_timeout = 0;
4475796c8dcSSimon Schubert 
4485796c8dcSSimon Schubert   if (set_tty_state (scb, &state))
449c50c785cSJohn Marino     fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
450c50c785cSJohn Marino 			safe_strerror (errno));
4515796c8dcSSimon Schubert }
4525796c8dcSSimon Schubert 
4535796c8dcSSimon Schubert /* Wait for input on scb, with timeout seconds.  Returns 0 on success,
4545796c8dcSSimon Schubert    otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert    For termio{s}, we actually just setup VTIME if necessary, and let the
457c50c785cSJohn Marino    timeout occur in the read() in hardwire_read().  */
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
4605796c8dcSSimon Schubert    ser_base*() until the old TERMIOS/SGTTY/... timer code has been
4615796c8dcSSimon Schubert    flushed. .  */
4625796c8dcSSimon Schubert 
4635796c8dcSSimon Schubert /* NOTE: cagney/1999-09-30: Much of the code below is dead.  The only
4645796c8dcSSimon Schubert    possible values of the TIMEOUT parameter are ONE and ZERO.
4655796c8dcSSimon Schubert    Consequently all the code that tries to handle the possability of
4665796c8dcSSimon Schubert    an overflowed timer is unnecessary.  */
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert static int
wait_for(struct serial * scb,int timeout)4695796c8dcSSimon Schubert wait_for (struct serial *scb, int timeout)
4705796c8dcSSimon Schubert {
4715796c8dcSSimon Schubert #ifdef HAVE_SGTTY
4725796c8dcSSimon Schubert   while (1)
4735796c8dcSSimon Schubert     {
4745796c8dcSSimon Schubert       struct timeval tv;
4755796c8dcSSimon Schubert       fd_set readfds;
4765796c8dcSSimon Schubert       int numfds;
4775796c8dcSSimon Schubert 
4785796c8dcSSimon Schubert       /* NOTE: Some OS's can scramble the READFDS when the select()
4795796c8dcSSimon Schubert          call fails (ex the kernel with Red Hat 5.2).  Initialize all
4805796c8dcSSimon Schubert          arguments before each call.  */
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert       tv.tv_sec = timeout;
4835796c8dcSSimon Schubert       tv.tv_usec = 0;
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert       FD_ZERO (&readfds);
4865796c8dcSSimon Schubert       FD_SET (scb->fd, &readfds);
4875796c8dcSSimon Schubert 
4885796c8dcSSimon Schubert       if (timeout >= 0)
4895796c8dcSSimon Schubert 	numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv);
4905796c8dcSSimon Schubert       else
4915796c8dcSSimon Schubert 	numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0);
4925796c8dcSSimon Schubert 
4935796c8dcSSimon Schubert       if (numfds <= 0)
4945796c8dcSSimon Schubert 	if (numfds == 0)
4955796c8dcSSimon Schubert 	  return SERIAL_TIMEOUT;
4965796c8dcSSimon Schubert 	else if (errno == EINTR)
4975796c8dcSSimon Schubert 	  continue;
4985796c8dcSSimon Schubert 	else
499c50c785cSJohn Marino 	  return SERIAL_ERROR;	/* Got an error from select or poll.  */
5005796c8dcSSimon Schubert 
5015796c8dcSSimon Schubert       return 0;
5025796c8dcSSimon Schubert     }
5035796c8dcSSimon Schubert #endif /* HAVE_SGTTY */
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert #if defined HAVE_TERMIO || defined HAVE_TERMIOS
5065796c8dcSSimon Schubert   if (timeout == scb->current_timeout)
5075796c8dcSSimon Schubert     return 0;
5085796c8dcSSimon Schubert 
5095796c8dcSSimon Schubert   scb->current_timeout = timeout;
5105796c8dcSSimon Schubert 
5115796c8dcSSimon Schubert   {
5125796c8dcSSimon Schubert     struct hardwire_ttystate state;
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert     if (get_tty_state (scb, &state))
515c50c785cSJohn Marino       fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
516c50c785cSJohn Marino 			  safe_strerror (errno));
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
5195796c8dcSSimon Schubert     if (timeout < 0)
5205796c8dcSSimon Schubert       {
5215796c8dcSSimon Schubert 	/* No timeout.  */
5225796c8dcSSimon Schubert 	state.termios.c_cc[VTIME] = 0;
5235796c8dcSSimon Schubert 	state.termios.c_cc[VMIN] = 1;
5245796c8dcSSimon Schubert       }
5255796c8dcSSimon Schubert     else
5265796c8dcSSimon Schubert       {
5275796c8dcSSimon Schubert 	state.termios.c_cc[VMIN] = 0;
5285796c8dcSSimon Schubert 	state.termios.c_cc[VTIME] = timeout * 10;
5295796c8dcSSimon Schubert 	if (state.termios.c_cc[VTIME] != timeout * 10)
5305796c8dcSSimon Schubert 	  {
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert 	    /* If c_cc is an 8-bit signed character, we can't go
5335796c8dcSSimon Schubert 	       bigger than this.  If it is always unsigned, we could use
5345796c8dcSSimon Schubert 	       25.  */
5355796c8dcSSimon Schubert 
5365796c8dcSSimon Schubert 	    scb->current_timeout = 12;
5375796c8dcSSimon Schubert 	    state.termios.c_cc[VTIME] = scb->current_timeout * 10;
5385796c8dcSSimon Schubert 	    scb->timeout_remaining = timeout - scb->current_timeout;
5395796c8dcSSimon Schubert 	  }
5405796c8dcSSimon Schubert       }
5415796c8dcSSimon Schubert #endif
5425796c8dcSSimon Schubert 
5435796c8dcSSimon Schubert #ifdef HAVE_TERMIO
5445796c8dcSSimon Schubert     if (timeout < 0)
5455796c8dcSSimon Schubert       {
5465796c8dcSSimon Schubert 	/* No timeout.  */
5475796c8dcSSimon Schubert 	state.termio.c_cc[VTIME] = 0;
5485796c8dcSSimon Schubert 	state.termio.c_cc[VMIN] = 1;
5495796c8dcSSimon Schubert       }
5505796c8dcSSimon Schubert     else
5515796c8dcSSimon Schubert       {
5525796c8dcSSimon Schubert 	state.termio.c_cc[VMIN] = 0;
5535796c8dcSSimon Schubert 	state.termio.c_cc[VTIME] = timeout * 10;
5545796c8dcSSimon Schubert 	if (state.termio.c_cc[VTIME] != timeout * 10)
5555796c8dcSSimon Schubert 	  {
5565796c8dcSSimon Schubert 	    /* If c_cc is an 8-bit signed character, we can't go
5575796c8dcSSimon Schubert 	       bigger than this.  If it is always unsigned, we could use
5585796c8dcSSimon Schubert 	       25.  */
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert 	    scb->current_timeout = 12;
5615796c8dcSSimon Schubert 	    state.termio.c_cc[VTIME] = scb->current_timeout * 10;
5625796c8dcSSimon Schubert 	    scb->timeout_remaining = timeout - scb->current_timeout;
5635796c8dcSSimon Schubert 	  }
5645796c8dcSSimon Schubert       }
5655796c8dcSSimon Schubert #endif
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert     if (set_tty_state (scb, &state))
568c50c785cSJohn Marino       fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
569c50c785cSJohn Marino 			  safe_strerror (errno));
5705796c8dcSSimon Schubert 
5715796c8dcSSimon Schubert     return 0;
5725796c8dcSSimon Schubert   }
5735796c8dcSSimon Schubert #endif /* HAVE_TERMIO || HAVE_TERMIOS */
5745796c8dcSSimon Schubert }
5755796c8dcSSimon Schubert 
576c50c785cSJohn Marino /* Read a character with user-specified timeout.  TIMEOUT is number of
577c50c785cSJohn Marino    seconds to wait, or -1 to wait forever.  Use timeout of 0 to effect
578c50c785cSJohn Marino    a poll.  Returns char if successful.  Returns SERIAL_TIMEOUT if
579c50c785cSJohn Marino    timeout expired, EOF if line dropped dead, or SERIAL_ERROR for any
580c50c785cSJohn Marino    other error (see errno in that case).  */
5815796c8dcSSimon Schubert 
5825796c8dcSSimon Schubert /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
5835796c8dcSSimon Schubert    ser_base*() until the old TERMIOS/SGTTY/... timer code has been
5845796c8dcSSimon Schubert    flushed.  */
5855796c8dcSSimon Schubert 
5865796c8dcSSimon Schubert /* NOTE: cagney/1999-09-16: This function is not identical to
5875796c8dcSSimon Schubert    ser_base_readchar() as part of replacing it with ser_base*()
5885796c8dcSSimon Schubert    merging will be required - this code handles the case where read()
5895796c8dcSSimon Schubert    times out due to no data while ser_base_readchar() doesn't expect
5905796c8dcSSimon Schubert    that.  */
5915796c8dcSSimon Schubert 
5925796c8dcSSimon Schubert static int
do_hardwire_readchar(struct serial * scb,int timeout)5935796c8dcSSimon Schubert do_hardwire_readchar (struct serial *scb, int timeout)
5945796c8dcSSimon Schubert {
5955796c8dcSSimon Schubert   int status, delta;
5965796c8dcSSimon Schubert   int detach = 0;
5975796c8dcSSimon Schubert 
5985796c8dcSSimon Schubert   if (timeout > 0)
5995796c8dcSSimon Schubert     timeout++;
6005796c8dcSSimon Schubert 
6015796c8dcSSimon Schubert   /* We have to be able to keep the GUI alive here, so we break the
6025796c8dcSSimon Schubert      original timeout into steps of 1 second, running the "keep the
6035796c8dcSSimon Schubert      GUI alive" hook each time through the loop.
6045796c8dcSSimon Schubert 
6055796c8dcSSimon Schubert      Also, timeout = 0 means to poll, so we just set the delta to 0,
6065796c8dcSSimon Schubert      so we will only go through the loop once.  */
6075796c8dcSSimon Schubert 
6085796c8dcSSimon Schubert   delta = (timeout == 0 ? 0 : 1);
6095796c8dcSSimon Schubert   while (1)
6105796c8dcSSimon Schubert     {
6115796c8dcSSimon Schubert 
6125796c8dcSSimon Schubert       /* N.B. The UI may destroy our world (for instance by calling
6135796c8dcSSimon Schubert          remote_stop,) in which case we want to get out of here as
6145796c8dcSSimon Schubert          quickly as possible.  It is not safe to touch scb, since
6155796c8dcSSimon Schubert          someone else might have freed it.  The
6165796c8dcSSimon Schubert          deprecated_ui_loop_hook signals that we should exit by
6175796c8dcSSimon Schubert          returning 1.  */
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert       if (deprecated_ui_loop_hook)
6205796c8dcSSimon Schubert 	detach = deprecated_ui_loop_hook (0);
6215796c8dcSSimon Schubert 
6225796c8dcSSimon Schubert       if (detach)
6235796c8dcSSimon Schubert 	return SERIAL_TIMEOUT;
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert       scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
6265796c8dcSSimon Schubert       status = wait_for (scb, delta);
6275796c8dcSSimon Schubert 
6285796c8dcSSimon Schubert       if (status < 0)
6295796c8dcSSimon Schubert 	return status;
6305796c8dcSSimon Schubert 
6315796c8dcSSimon Schubert       status = read (scb->fd, scb->buf, BUFSIZ);
6325796c8dcSSimon Schubert 
6335796c8dcSSimon Schubert       if (status <= 0)
6345796c8dcSSimon Schubert 	{
6355796c8dcSSimon Schubert 	  if (status == 0)
6365796c8dcSSimon Schubert 	    {
6375796c8dcSSimon Schubert 	      /* Zero characters means timeout (it could also be EOF, but
6385796c8dcSSimon Schubert 	         we don't (yet at least) distinguish).  */
6395796c8dcSSimon Schubert 	      if (scb->timeout_remaining > 0)
6405796c8dcSSimon Schubert 		{
6415796c8dcSSimon Schubert 		  timeout = scb->timeout_remaining;
6425796c8dcSSimon Schubert 		  continue;
6435796c8dcSSimon Schubert 		}
6445796c8dcSSimon Schubert 	      else if (scb->timeout_remaining < 0)
6455796c8dcSSimon Schubert 		continue;
6465796c8dcSSimon Schubert 	      else
6475796c8dcSSimon Schubert 		return SERIAL_TIMEOUT;
6485796c8dcSSimon Schubert 	    }
6495796c8dcSSimon Schubert 	  else if (errno == EINTR)
6505796c8dcSSimon Schubert 	    continue;
6515796c8dcSSimon Schubert 	  else
652c50c785cSJohn Marino 	    return SERIAL_ERROR;	/* Got an error from read.  */
6535796c8dcSSimon Schubert 	}
6545796c8dcSSimon Schubert 
6555796c8dcSSimon Schubert       scb->bufcnt = status;
6565796c8dcSSimon Schubert       scb->bufcnt--;
6575796c8dcSSimon Schubert       scb->bufp = scb->buf;
6585796c8dcSSimon Schubert       return *scb->bufp++;
6595796c8dcSSimon Schubert     }
6605796c8dcSSimon Schubert }
6615796c8dcSSimon Schubert 
6625796c8dcSSimon Schubert static int
hardwire_readchar(struct serial * scb,int timeout)6635796c8dcSSimon Schubert hardwire_readchar (struct serial *scb, int timeout)
6645796c8dcSSimon Schubert {
6655796c8dcSSimon Schubert   return generic_readchar (scb, timeout, do_hardwire_readchar);
6665796c8dcSSimon Schubert }
6675796c8dcSSimon Schubert 
6685796c8dcSSimon Schubert 
6695796c8dcSSimon Schubert #ifndef B19200
6705796c8dcSSimon Schubert #define B19200 EXTA
6715796c8dcSSimon Schubert #endif
6725796c8dcSSimon Schubert 
6735796c8dcSSimon Schubert #ifndef B38400
6745796c8dcSSimon Schubert #define B38400 EXTB
6755796c8dcSSimon Schubert #endif
6765796c8dcSSimon Schubert 
6775796c8dcSSimon Schubert /* Translate baud rates from integers to damn B_codes.  Unix should
6785796c8dcSSimon Schubert    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
6795796c8dcSSimon Schubert 
6805796c8dcSSimon Schubert static struct
6815796c8dcSSimon Schubert {
6825796c8dcSSimon Schubert   int rate;
6835796c8dcSSimon Schubert   int code;
6845796c8dcSSimon Schubert }
6855796c8dcSSimon Schubert baudtab[] =
6865796c8dcSSimon Schubert {
6875796c8dcSSimon Schubert   {
6885796c8dcSSimon Schubert     50, B50
6895796c8dcSSimon Schubert   }
6905796c8dcSSimon Schubert   ,
6915796c8dcSSimon Schubert   {
6925796c8dcSSimon Schubert     75, B75
6935796c8dcSSimon Schubert   }
6945796c8dcSSimon Schubert   ,
6955796c8dcSSimon Schubert   {
6965796c8dcSSimon Schubert     110, B110
6975796c8dcSSimon Schubert   }
6985796c8dcSSimon Schubert   ,
6995796c8dcSSimon Schubert   {
7005796c8dcSSimon Schubert     134, B134
7015796c8dcSSimon Schubert   }
7025796c8dcSSimon Schubert   ,
7035796c8dcSSimon Schubert   {
7045796c8dcSSimon Schubert     150, B150
7055796c8dcSSimon Schubert   }
7065796c8dcSSimon Schubert   ,
7075796c8dcSSimon Schubert   {
7085796c8dcSSimon Schubert     200, B200
7095796c8dcSSimon Schubert   }
7105796c8dcSSimon Schubert   ,
7115796c8dcSSimon Schubert   {
7125796c8dcSSimon Schubert     300, B300
7135796c8dcSSimon Schubert   }
7145796c8dcSSimon Schubert   ,
7155796c8dcSSimon Schubert   {
7165796c8dcSSimon Schubert     600, B600
7175796c8dcSSimon Schubert   }
7185796c8dcSSimon Schubert   ,
7195796c8dcSSimon Schubert   {
7205796c8dcSSimon Schubert     1200, B1200
7215796c8dcSSimon Schubert   }
7225796c8dcSSimon Schubert   ,
7235796c8dcSSimon Schubert   {
7245796c8dcSSimon Schubert     1800, B1800
7255796c8dcSSimon Schubert   }
7265796c8dcSSimon Schubert   ,
7275796c8dcSSimon Schubert   {
7285796c8dcSSimon Schubert     2400, B2400
7295796c8dcSSimon Schubert   }
7305796c8dcSSimon Schubert   ,
7315796c8dcSSimon Schubert   {
7325796c8dcSSimon Schubert     4800, B4800
7335796c8dcSSimon Schubert   }
7345796c8dcSSimon Schubert   ,
7355796c8dcSSimon Schubert   {
7365796c8dcSSimon Schubert     9600, B9600
7375796c8dcSSimon Schubert   }
7385796c8dcSSimon Schubert   ,
7395796c8dcSSimon Schubert   {
7405796c8dcSSimon Schubert     19200, B19200
7415796c8dcSSimon Schubert   }
7425796c8dcSSimon Schubert   ,
7435796c8dcSSimon Schubert   {
7445796c8dcSSimon Schubert     38400, B38400
7455796c8dcSSimon Schubert   }
7465796c8dcSSimon Schubert   ,
7475796c8dcSSimon Schubert #ifdef B57600
7485796c8dcSSimon Schubert   {
7495796c8dcSSimon Schubert     57600, B57600
7505796c8dcSSimon Schubert   }
7515796c8dcSSimon Schubert   ,
7525796c8dcSSimon Schubert #endif
7535796c8dcSSimon Schubert #ifdef B115200
7545796c8dcSSimon Schubert   {
7555796c8dcSSimon Schubert     115200, B115200
7565796c8dcSSimon Schubert   }
7575796c8dcSSimon Schubert   ,
7585796c8dcSSimon Schubert #endif
7595796c8dcSSimon Schubert #ifdef B230400
7605796c8dcSSimon Schubert   {
7615796c8dcSSimon Schubert     230400, B230400
7625796c8dcSSimon Schubert   }
7635796c8dcSSimon Schubert   ,
7645796c8dcSSimon Schubert #endif
7655796c8dcSSimon Schubert #ifdef B460800
7665796c8dcSSimon Schubert   {
7675796c8dcSSimon Schubert     460800, B460800
7685796c8dcSSimon Schubert   }
7695796c8dcSSimon Schubert   ,
7705796c8dcSSimon Schubert #endif
7715796c8dcSSimon Schubert   {
7725796c8dcSSimon Schubert     -1, -1
7735796c8dcSSimon Schubert   }
7745796c8dcSSimon Schubert   ,
7755796c8dcSSimon Schubert };
7765796c8dcSSimon Schubert 
7775796c8dcSSimon Schubert static int
rate_to_code(int rate)7785796c8dcSSimon Schubert rate_to_code (int rate)
7795796c8dcSSimon Schubert {
7805796c8dcSSimon Schubert   int i;
7815796c8dcSSimon Schubert 
7825796c8dcSSimon Schubert   for (i = 0; baudtab[i].rate != -1; i++)
7835796c8dcSSimon Schubert     {
7845796c8dcSSimon Schubert       /* test for perfect macth.  */
7855796c8dcSSimon Schubert       if (rate == baudtab[i].rate)
7865796c8dcSSimon Schubert         return baudtab[i].code;
7875796c8dcSSimon Schubert       else
7885796c8dcSSimon Schubert         {
7895796c8dcSSimon Schubert 	  /* check if it is in between valid values.  */
7905796c8dcSSimon Schubert           if (rate < baudtab[i].rate)
7915796c8dcSSimon Schubert 	    {
7925796c8dcSSimon Schubert 	      if (i)
7935796c8dcSSimon Schubert 	        {
794c50c785cSJohn Marino 	          warning (_("Invalid baud rate %d.  "
795c50c785cSJohn Marino 			     "Closest values are %d and %d."),
7965796c8dcSSimon Schubert 			   rate, baudtab[i - 1].rate, baudtab[i].rate);
7975796c8dcSSimon Schubert 		}
7985796c8dcSSimon Schubert 	      else
7995796c8dcSSimon Schubert 	        {
8005796c8dcSSimon Schubert 	          warning (_("Invalid baud rate %d.  Minimum value is %d."),
8015796c8dcSSimon Schubert 			   rate, baudtab[0].rate);
8025796c8dcSSimon Schubert 		}
8035796c8dcSSimon Schubert 	      return -1;
8045796c8dcSSimon Schubert 	    }
8055796c8dcSSimon Schubert         }
8065796c8dcSSimon Schubert     }
8075796c8dcSSimon Schubert 
8085796c8dcSSimon Schubert   /* The requested speed was too large.  */
8095796c8dcSSimon Schubert   warning (_("Invalid baud rate %d.  Maximum value is %d."),
8105796c8dcSSimon Schubert             rate, baudtab[i - 1].rate);
8115796c8dcSSimon Schubert   return -1;
8125796c8dcSSimon Schubert }
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert static int
hardwire_setbaudrate(struct serial * scb,int rate)8155796c8dcSSimon Schubert hardwire_setbaudrate (struct serial *scb, int rate)
8165796c8dcSSimon Schubert {
8175796c8dcSSimon Schubert   struct hardwire_ttystate state;
8185796c8dcSSimon Schubert   int baud_code = rate_to_code (rate);
8195796c8dcSSimon Schubert 
8205796c8dcSSimon Schubert   if (baud_code < 0)
8215796c8dcSSimon Schubert     {
8225796c8dcSSimon Schubert       /* The baud rate was not valid.
8235796c8dcSSimon Schubert          A warning has already been issued.  */
8245796c8dcSSimon Schubert       errno = EINVAL;
8255796c8dcSSimon Schubert       return -1;
8265796c8dcSSimon Schubert     }
8275796c8dcSSimon Schubert 
8285796c8dcSSimon Schubert   if (get_tty_state (scb, &state))
8295796c8dcSSimon Schubert     return -1;
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
8325796c8dcSSimon Schubert   cfsetospeed (&state.termios, baud_code);
8335796c8dcSSimon Schubert   cfsetispeed (&state.termios, baud_code);
8345796c8dcSSimon Schubert #endif
8355796c8dcSSimon Schubert 
8365796c8dcSSimon Schubert #ifdef HAVE_TERMIO
8375796c8dcSSimon Schubert #ifndef CIBAUD
8385796c8dcSSimon Schubert #define CIBAUD CBAUD
8395796c8dcSSimon Schubert #endif
8405796c8dcSSimon Schubert 
8415796c8dcSSimon Schubert   state.termio.c_cflag &= ~(CBAUD | CIBAUD);
8425796c8dcSSimon Schubert   state.termio.c_cflag |= baud_code;
8435796c8dcSSimon Schubert #endif
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert #ifdef HAVE_SGTTY
8465796c8dcSSimon Schubert   state.sgttyb.sg_ispeed = baud_code;
8475796c8dcSSimon Schubert   state.sgttyb.sg_ospeed = baud_code;
8485796c8dcSSimon Schubert #endif
8495796c8dcSSimon Schubert 
8505796c8dcSSimon Schubert   return set_tty_state (scb, &state);
8515796c8dcSSimon Schubert }
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert static int
hardwire_setstopbits(struct serial * scb,int num)8545796c8dcSSimon Schubert hardwire_setstopbits (struct serial *scb, int num)
8555796c8dcSSimon Schubert {
8565796c8dcSSimon Schubert   struct hardwire_ttystate state;
8575796c8dcSSimon Schubert   int newbit;
8585796c8dcSSimon Schubert 
8595796c8dcSSimon Schubert   if (get_tty_state (scb, &state))
8605796c8dcSSimon Schubert     return -1;
8615796c8dcSSimon Schubert 
8625796c8dcSSimon Schubert   switch (num)
8635796c8dcSSimon Schubert     {
8645796c8dcSSimon Schubert     case SERIAL_1_STOPBITS:
8655796c8dcSSimon Schubert       newbit = 0;
8665796c8dcSSimon Schubert       break;
8675796c8dcSSimon Schubert     case SERIAL_1_AND_A_HALF_STOPBITS:
8685796c8dcSSimon Schubert     case SERIAL_2_STOPBITS:
8695796c8dcSSimon Schubert       newbit = 1;
8705796c8dcSSimon Schubert       break;
8715796c8dcSSimon Schubert     default:
8725796c8dcSSimon Schubert       return 1;
8735796c8dcSSimon Schubert     }
8745796c8dcSSimon Schubert 
8755796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
8765796c8dcSSimon Schubert   if (!newbit)
8775796c8dcSSimon Schubert     state.termios.c_cflag &= ~CSTOPB;
8785796c8dcSSimon Schubert   else
8795796c8dcSSimon Schubert     state.termios.c_cflag |= CSTOPB;	/* two bits */
8805796c8dcSSimon Schubert #endif
8815796c8dcSSimon Schubert 
8825796c8dcSSimon Schubert #ifdef HAVE_TERMIO
8835796c8dcSSimon Schubert   if (!newbit)
8845796c8dcSSimon Schubert     state.termio.c_cflag &= ~CSTOPB;
8855796c8dcSSimon Schubert   else
8865796c8dcSSimon Schubert     state.termio.c_cflag |= CSTOPB;	/* two bits */
8875796c8dcSSimon Schubert #endif
8885796c8dcSSimon Schubert 
8895796c8dcSSimon Schubert #ifdef HAVE_SGTTY
8905796c8dcSSimon Schubert   return 0;			/* sgtty doesn't support this */
8915796c8dcSSimon Schubert #endif
8925796c8dcSSimon Schubert 
8935796c8dcSSimon Schubert   return set_tty_state (scb, &state);
8945796c8dcSSimon Schubert }
8955796c8dcSSimon Schubert 
8965796c8dcSSimon Schubert static void
hardwire_close(struct serial * scb)8975796c8dcSSimon Schubert hardwire_close (struct serial *scb)
8985796c8dcSSimon Schubert {
8995796c8dcSSimon Schubert   if (scb->fd < 0)
9005796c8dcSSimon Schubert     return;
9015796c8dcSSimon Schubert 
9025796c8dcSSimon Schubert   close (scb->fd);
9035796c8dcSSimon Schubert   scb->fd = -1;
9045796c8dcSSimon Schubert }
9055796c8dcSSimon Schubert 
9065796c8dcSSimon Schubert 
9075796c8dcSSimon Schubert void
_initialize_ser_hardwire(void)9085796c8dcSSimon Schubert _initialize_ser_hardwire (void)
9095796c8dcSSimon Schubert {
9105796c8dcSSimon Schubert   struct serial_ops *ops = XMALLOC (struct serial_ops);
911cf7f2e2dSJohn Marino 
9125796c8dcSSimon Schubert   memset (ops, 0, sizeof (struct serial_ops));
9135796c8dcSSimon Schubert   ops->name = "hardwire";
9145796c8dcSSimon Schubert   ops->next = 0;
9155796c8dcSSimon Schubert   ops->open = hardwire_open;
9165796c8dcSSimon Schubert   ops->close = hardwire_close;
9175796c8dcSSimon Schubert   /* FIXME: Don't replace this with the equivalent ser_base*() until
9185796c8dcSSimon Schubert      the old TERMIOS/SGTTY/... timer code has been flushed.  cagney
9195796c8dcSSimon Schubert      1999-09-16.  */
9205796c8dcSSimon Schubert   ops->readchar = hardwire_readchar;
9215796c8dcSSimon Schubert   ops->write = ser_base_write;
9225796c8dcSSimon Schubert   ops->flush_output = hardwire_flush_output;
9235796c8dcSSimon Schubert   ops->flush_input = hardwire_flush_input;
9245796c8dcSSimon Schubert   ops->send_break = hardwire_send_break;
9255796c8dcSSimon Schubert   ops->go_raw = hardwire_raw;
9265796c8dcSSimon Schubert   ops->get_tty_state = hardwire_get_tty_state;
927c50c785cSJohn Marino   ops->copy_tty_state = hardwire_copy_tty_state;
9285796c8dcSSimon Schubert   ops->set_tty_state = hardwire_set_tty_state;
9295796c8dcSSimon Schubert   ops->print_tty_state = hardwire_print_tty_state;
9305796c8dcSSimon Schubert   ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
9315796c8dcSSimon Schubert   ops->setbaudrate = hardwire_setbaudrate;
9325796c8dcSSimon Schubert   ops->setstopbits = hardwire_setstopbits;
9335796c8dcSSimon Schubert   ops->drain_output = hardwire_drain_output;
9345796c8dcSSimon Schubert   ops->async = ser_base_async;
9355796c8dcSSimon Schubert   ops->read_prim = ser_unix_read_prim;
9365796c8dcSSimon Schubert   ops->write_prim = ser_unix_write_prim;
9375796c8dcSSimon Schubert   serial_add_interface (ops);
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert #ifdef HAVE_TERMIOS
9405796c8dcSSimon Schubert #ifdef CRTSCTS
9415796c8dcSSimon Schubert   add_setshow_boolean_cmd ("remoteflow", no_class,
9425796c8dcSSimon Schubert 			   &serial_hwflow, _("\
9435796c8dcSSimon Schubert Set use of hardware flow control for remote serial I/O."), _("\
9445796c8dcSSimon Schubert Show use of hardware flow control for remote serial I/O."), _("\
9455796c8dcSSimon Schubert Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
9465796c8dcSSimon Schubert when debugging using remote targets."),
9475796c8dcSSimon Schubert 			   NULL,
9485796c8dcSSimon Schubert 			   show_serial_hwflow,
9495796c8dcSSimon Schubert 			   &setlist, &showlist);
9505796c8dcSSimon Schubert #endif
9515796c8dcSSimon Schubert #endif
9525796c8dcSSimon Schubert }
9535796c8dcSSimon Schubert 
9545796c8dcSSimon Schubert int
ser_unix_read_prim(struct serial * scb,size_t count)9555796c8dcSSimon Schubert ser_unix_read_prim (struct serial *scb, size_t count)
9565796c8dcSSimon Schubert {
9575796c8dcSSimon Schubert   int status;
9585796c8dcSSimon Schubert 
9595796c8dcSSimon Schubert   while (1)
9605796c8dcSSimon Schubert     {
9615796c8dcSSimon Schubert       status = read (scb->fd, scb->buf, count);
9625796c8dcSSimon Schubert       if (status != -1 || errno != EINTR)
9635796c8dcSSimon Schubert 	break;
9645796c8dcSSimon Schubert     }
9655796c8dcSSimon Schubert   return status;
9665796c8dcSSimon Schubert }
9675796c8dcSSimon Schubert 
9685796c8dcSSimon Schubert int
ser_unix_write_prim(struct serial * scb,const void * buf,size_t len)9695796c8dcSSimon Schubert ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
9705796c8dcSSimon Schubert {
9715796c8dcSSimon Schubert   /* ??? Historically, GDB has not retried calls to "write" that
9725796c8dcSSimon Schubert      result in EINTR.  */
9735796c8dcSSimon Schubert   return write (scb->fd, buf, len);
9745796c8dcSSimon Schubert }
975