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