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