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