]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ser-unix.c
Fix Credit.
[thirdparty/binutils-gdb.git] / gdb / ser-unix.c
CommitLineData
c906108c 1/* Serial interface for local (hardwired) serial ports on Un*x like systems
b6ba6518
KB
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
3 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
9 the Free Software Foundation; either version 2 of the License, or
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
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "serial.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
32#include "gdb_string.h"
33#include "event-loop.h"
34
c906108c
SS
35#ifdef HAVE_TERMIOS
36
37struct hardwire_ttystate
c5aa993b
JM
38 {
39 struct termios termios;
40 };
c906108c
SS
41#endif /* termios */
42
43#ifdef HAVE_TERMIO
44
45/* It is believed that all systems which have added job control to SVR3
46 (e.g. sco) have also added termios. Even if not, trying to figure out
47 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
48 bewildering. So we don't attempt it. */
49
50struct hardwire_ttystate
c5aa993b
JM
51 {
52 struct termio termio;
53 };
c906108c
SS
54#endif /* termio */
55
56#ifdef HAVE_SGTTY
c906108c 57struct hardwire_ttystate
c5aa993b
JM
58 {
59 struct sgttyb sgttyb;
60 struct tchars tc;
61 struct ltchars ltc;
62 /* Line discipline flags. */
63 int lmode;
64 };
c906108c
SS
65#endif /* sgtty */
66
c2c6d25f
JM
67static int hardwire_open (serial_t scb, const char *name);
68static void hardwire_raw (serial_t scb);
69static int wait_for (serial_t scb, int timeout);
70static int hardwire_readchar (serial_t scb, int timeout);
2acceee2
JM
71static int do_hardwire_readchar (serial_t scb, int timeout);
72static int generic_readchar (serial_t scb, int timeout, int (*do_readchar) (serial_t scb, int timeout));
c2c6d25f
JM
73static int rate_to_code (int rate);
74static int hardwire_setbaudrate (serial_t scb, int rate);
c2c6d25f
JM
75static void hardwire_close (serial_t scb);
76static int get_tty_state (serial_t scb, struct hardwire_ttystate * state);
77static int set_tty_state (serial_t scb, struct hardwire_ttystate * state);
78static serial_ttystate hardwire_get_tty_state (serial_t scb);
79static int hardwire_set_tty_state (serial_t scb, serial_ttystate state);
80static int hardwire_noflush_set_tty_state (serial_t, serial_ttystate,
81 serial_ttystate);
d9fcf2fb 82static void hardwire_print_tty_state (serial_t, serial_ttystate, struct ui_file *);
c2c6d25f
JM
83static int hardwire_drain_output (serial_t);
84static int hardwire_flush_output (serial_t);
85static int hardwire_flush_input (serial_t);
86static int hardwire_send_break (serial_t);
87static int hardwire_setstopbits (serial_t, int);
88
2acceee2
JM
89static int do_unix_readchar (serial_t scb, int timeout);
90static timer_handler_func push_event;
91static handler_func fd_event;
92static void reschedule (serial_t scb);
93
c2c6d25f
JM
94void _initialize_ser_hardwire (void);
95
96extern int (*ui_loop_hook) (int);
c906108c
SS
97
98/* Open up a real live device for serial I/O */
99
100static int
c2c6d25f 101hardwire_open (serial_t scb, const char *name)
c906108c
SS
102{
103 scb->fd = open (name, O_RDWR);
104 if (scb->fd < 0)
105 return -1;
106
107 return 0;
108}
109
110static int
c2c6d25f 111get_tty_state (serial_t scb, struct hardwire_ttystate *state)
c906108c
SS
112{
113#ifdef HAVE_TERMIOS
c5aa993b 114 if (tcgetattr (scb->fd, &state->termios) < 0)
c906108c
SS
115 return -1;
116
117 return 0;
118#endif
119
120#ifdef HAVE_TERMIO
121 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
122 return -1;
123 return 0;
124#endif
125
126#ifdef HAVE_SGTTY
127 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
128 return -1;
129 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
130 return -1;
131 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
132 return -1;
133 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
134 return -1;
135
136 return 0;
137#endif
138}
139
140static int
c2c6d25f 141set_tty_state (serial_t scb, struct hardwire_ttystate *state)
c906108c
SS
142{
143#ifdef HAVE_TERMIOS
c5aa993b 144 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
c906108c
SS
145 return -1;
146
147 return 0;
148#endif
149
150#ifdef HAVE_TERMIO
151 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
152 return -1;
153 return 0;
154#endif
155
156#ifdef HAVE_SGTTY
157 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
158 return -1;
159 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
160 return -1;
161 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
162 return -1;
163 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
164 return -1;
165
166 return 0;
167#endif
168}
169
170static serial_ttystate
c2c6d25f 171hardwire_get_tty_state (serial_t scb)
c906108c
SS
172{
173 struct hardwire_ttystate *state;
174
c5aa993b 175 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
c906108c 176
c5aa993b 177 if (get_tty_state (scb, state))
c906108c
SS
178 return NULL;
179
c5aa993b 180 return (serial_ttystate) state;
c906108c
SS
181}
182
183static int
c2c6d25f 184hardwire_set_tty_state (serial_t scb, serial_ttystate ttystate)
c906108c
SS
185{
186 struct hardwire_ttystate *state;
187
c5aa993b 188 state = (struct hardwire_ttystate *) ttystate;
c906108c 189
c5aa993b 190 return set_tty_state (scb, state);
c906108c
SS
191}
192
193static int
c2c6d25f
JM
194hardwire_noflush_set_tty_state (serial_t scb,
195 serial_ttystate new_ttystate,
196 serial_ttystate old_ttystate)
c906108c
SS
197{
198 struct hardwire_ttystate new_state;
199#ifdef HAVE_SGTTY
200 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
201#endif
202
c5aa993b 203 new_state = *(struct hardwire_ttystate *) new_ttystate;
c906108c
SS
204
205 /* Don't change in or out of raw mode; we don't want to flush input.
206 termio and termios have no such restriction; for them flushing input
207 is separate from setting the attributes. */
208
209#ifdef HAVE_SGTTY
210 if (state->sgttyb.sg_flags & RAW)
211 new_state.sgttyb.sg_flags |= RAW;
212 else
213 new_state.sgttyb.sg_flags &= ~RAW;
214
215 /* I'm not sure whether this is necessary; the manpage just mentions
216 RAW not CBREAK. */
217 if (state->sgttyb.sg_flags & CBREAK)
218 new_state.sgttyb.sg_flags |= CBREAK;
219 else
220 new_state.sgttyb.sg_flags &= ~CBREAK;
221#endif
222
223 return set_tty_state (scb, &new_state);
224}
225
226static void
c2c6d25f
JM
227hardwire_print_tty_state (serial_t scb,
228 serial_ttystate ttystate,
d9fcf2fb 229 struct ui_file *stream)
c906108c
SS
230{
231 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
232 int i;
233
234#ifdef HAVE_TERMIOS
c2c6d25f 235 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
2acceee2
JM
236 (int) state->termios.c_iflag,
237 (int) state->termios.c_oflag);
c2c6d25f 238 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
2acceee2
JM
239 (int) state->termios.c_cflag,
240 (int) state->termios.c_lflag);
c906108c
SS
241#if 0
242 /* This not in POSIX, and is not really documented by those systems
243 which have it (at least not Sun). */
c2c6d25f 244 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
c906108c 245#endif
c2c6d25f 246 fprintf_filtered (stream, "c_cc: ");
c906108c 247 for (i = 0; i < NCCS; i += 1)
c2c6d25f
JM
248 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
249 fprintf_filtered (stream, "\n");
c906108c
SS
250#endif
251
252#ifdef HAVE_TERMIO
c2c6d25f
JM
253 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
254 state->termio.c_iflag, state->termio.c_oflag);
255 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
256 state->termio.c_cflag, state->termio.c_lflag,
257 state->termio.c_line);
258 fprintf_filtered (stream, "c_cc: ");
c906108c 259 for (i = 0; i < NCC; i += 1)
c2c6d25f
JM
260 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
261 fprintf_filtered (stream, "\n");
c906108c
SS
262#endif
263
264#ifdef HAVE_SGTTY
c2c6d25f
JM
265 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
266 state->sgttyb.sg_flags);
c906108c 267
c2c6d25f 268 fprintf_filtered (stream, "tchars: ");
c5aa993b 269 for (i = 0; i < (int) sizeof (struct tchars); i++)
c2c6d25f 270 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
64122a8b 271 fprintf_filtered (stream, "\n");
c906108c 272
c2c6d25f 273 fprintf_filtered (stream, "ltchars: ");
c5aa993b 274 for (i = 0; i < (int) sizeof (struct ltchars); i++)
c2c6d25f
JM
275 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
276 fprintf_filtered (stream, "\n");
c906108c 277
c2c6d25f 278 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
c906108c
SS
279#endif
280}
281
282/* Wait for the output to drain away, as opposed to flushing (discarding) it */
283
284static int
c2c6d25f 285hardwire_drain_output (serial_t scb)
c906108c
SS
286{
287#ifdef HAVE_TERMIOS
288 return tcdrain (scb->fd);
289#endif
290
291#ifdef HAVE_TERMIO
292 return ioctl (scb->fd, TCSBRK, 1);
293#endif
294
295#ifdef HAVE_SGTTY
296 /* Get the current state and then restore it using TIOCSETP,
297 which should cause the output to drain and pending input
298 to be discarded. */
299 {
300 struct hardwire_ttystate state;
301 if (get_tty_state (scb, &state))
302 {
303 return (-1);
304 }
305 else
306 {
307 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
308 }
309 }
c5aa993b 310#endif
c906108c
SS
311}
312
313static int
c2c6d25f 314hardwire_flush_output (serial_t scb)
c906108c
SS
315{
316#ifdef HAVE_TERMIOS
317 return tcflush (scb->fd, TCOFLUSH);
318#endif
319
320#ifdef HAVE_TERMIO
321 return ioctl (scb->fd, TCFLSH, 1);
322#endif
323
324#ifdef HAVE_SGTTY
325 /* This flushes both input and output, but we can't do better. */
326 return ioctl (scb->fd, TIOCFLUSH, 0);
c5aa993b 327#endif
c906108c
SS
328}
329
330static int
c2c6d25f 331hardwire_flush_input (serial_t scb)
c906108c 332{
2acceee2 333 ser_unix_flush_input (scb);
c906108c
SS
334
335#ifdef HAVE_TERMIOS
336 return tcflush (scb->fd, TCIFLUSH);
337#endif
338
339#ifdef HAVE_TERMIO
340 return ioctl (scb->fd, TCFLSH, 0);
341#endif
342
343#ifdef HAVE_SGTTY
344 /* This flushes both input and output, but we can't do better. */
345 return ioctl (scb->fd, TIOCFLUSH, 0);
c5aa993b 346#endif
c906108c
SS
347}
348
349static int
c2c6d25f 350hardwire_send_break (serial_t scb)
c906108c
SS
351{
352#ifdef HAVE_TERMIOS
353 return tcsendbreak (scb->fd, 0);
354#endif
355
356#ifdef HAVE_TERMIO
357 return ioctl (scb->fd, TCSBRK, 0);
358#endif
359
360#ifdef HAVE_SGTTY
361 {
362 int status;
363 struct timeval timeout;
364
365 status = ioctl (scb->fd, TIOCSBRK, 0);
366
367 /* Can't use usleep; it doesn't exist in BSD 4.2. */
368 /* Note that if this select() is interrupted by a signal it will not wait
369 the full length of time. I think that is OK. */
370 timeout.tv_sec = 0;
371 timeout.tv_usec = 250000;
372 select (0, 0, 0, 0, &timeout);
373 status = ioctl (scb->fd, TIOCCBRK, 0);
374 return status;
375 }
c5aa993b 376#endif
c906108c
SS
377}
378
379static void
c2c6d25f 380hardwire_raw (serial_t scb)
c906108c
SS
381{
382 struct hardwire_ttystate state;
383
c5aa993b
JM
384 if (get_tty_state (scb, &state))
385 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
386
387#ifdef HAVE_TERMIOS
388 state.termios.c_iflag = 0;
389 state.termios.c_oflag = 0;
390 state.termios.c_lflag = 0;
c5aa993b 391 state.termios.c_cflag &= ~(CSIZE | PARENB);
c906108c
SS
392 state.termios.c_cflag |= CLOCAL | CS8;
393 state.termios.c_cc[VMIN] = 0;
394 state.termios.c_cc[VTIME] = 0;
395#endif
396
397#ifdef HAVE_TERMIO
398 state.termio.c_iflag = 0;
399 state.termio.c_oflag = 0;
400 state.termio.c_lflag = 0;
c5aa993b 401 state.termio.c_cflag &= ~(CSIZE | PARENB);
c906108c
SS
402 state.termio.c_cflag |= CLOCAL | CS8;
403 state.termio.c_cc[VMIN] = 0;
404 state.termio.c_cc[VTIME] = 0;
405#endif
406
407#ifdef HAVE_SGTTY
408 state.sgttyb.sg_flags |= RAW | ANYP;
409 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
410#endif
411
412 scb->current_timeout = 0;
413
414 if (set_tty_state (scb, &state))
c5aa993b 415 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
416}
417
418/* Wait for input on scb, with timeout seconds. Returns 0 on success,
419 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
420
421 For termio{s}, we actually just setup VTIME if necessary, and let the
422 timeout occur in the read() in hardwire_read().
423 */
424
2acceee2
JM
425/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
426 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
427 flushed. . */
428
429/* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
430 possible values of the TIMEOUT parameter are ONE and ZERO.
431 Consequently all the code that tries to handle the possability of
432 an overflowed timer is unnecessary. */
c2c6d25f 433
c906108c 434static int
c2c6d25f 435wait_for (serial_t scb, int timeout)
c906108c 436{
c906108c 437#ifdef HAVE_SGTTY
ab5ba170
AC
438 while (1)
439 {
440 struct timeval tv;
441 fd_set readfds;
442 int numfds;
c906108c 443
ab5ba170
AC
444 /* NOTE: Some OS's can scramble the READFDS when the select()
445 call fails (ex the kernel with Red Hat 5.2). Initialize all
446 arguments before each call. */
c906108c 447
ab5ba170
AC
448 tv.tv_sec = timeout;
449 tv.tv_usec = 0;
c906108c 450
ab5ba170
AC
451 FD_ZERO (&readfds);
452 FD_SET (scb->fd, &readfds);
c906108c 453
ab5ba170
AC
454 if (timeout >= 0)
455 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
456 else
457 numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
c906108c 458
ab5ba170
AC
459 if (numfds <= 0)
460 if (numfds == 0)
461 return SERIAL_TIMEOUT;
462 else if (errno == EINTR)
463 continue;
c906108c 464 else
ab5ba170 465 return SERIAL_ERROR; /* Got an error from select or poll */
c906108c 466
ab5ba170
AC
467 return 0;
468 }
c5aa993b 469#endif /* HAVE_SGTTY */
c906108c
SS
470
471#if defined HAVE_TERMIO || defined HAVE_TERMIOS
472 if (timeout == scb->current_timeout)
473 return 0;
474
475 scb->current_timeout = timeout;
476
477 {
478 struct hardwire_ttystate state;
479
c5aa993b
JM
480 if (get_tty_state (scb, &state))
481 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
482
483#ifdef HAVE_TERMIOS
484 if (timeout < 0)
485 {
486 /* No timeout. */
487 state.termios.c_cc[VTIME] = 0;
488 state.termios.c_cc[VMIN] = 1;
489 }
490 else
491 {
492 state.termios.c_cc[VMIN] = 0;
493 state.termios.c_cc[VTIME] = timeout * 10;
494 if (state.termios.c_cc[VTIME] != timeout * 10)
495 {
496
497 /* If c_cc is an 8-bit signed character, we can't go
498 bigger than this. If it is always unsigned, we could use
499 25. */
500
501 scb->current_timeout = 12;
502 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
503 scb->timeout_remaining = timeout - scb->current_timeout;
504 }
505 }
506#endif
507
508#ifdef HAVE_TERMIO
509 if (timeout < 0)
510 {
511 /* No timeout. */
512 state.termio.c_cc[VTIME] = 0;
513 state.termio.c_cc[VMIN] = 1;
514 }
515 else
516 {
517 state.termio.c_cc[VMIN] = 0;
518 state.termio.c_cc[VTIME] = timeout * 10;
519 if (state.termio.c_cc[VTIME] != timeout * 10)
520 {
521 /* If c_cc is an 8-bit signed character, we can't go
522 bigger than this. If it is always unsigned, we could use
523 25. */
524
525 scb->current_timeout = 12;
526 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
527 scb->timeout_remaining = timeout - scb->current_timeout;
528 }
529 }
530#endif
531
532 if (set_tty_state (scb, &state))
c5aa993b 533 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
c906108c
SS
534
535 return 0;
536 }
c5aa993b 537#endif /* HAVE_TERMIO || HAVE_TERMIOS */
c906108c
SS
538}
539
540/* Read a character with user-specified timeout. TIMEOUT is number of seconds
541 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
542 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
543 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
c2c6d25f
JM
544
545/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
546 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
547 flushed. */
548
549/* NOTE: cagney/1999-09-16: This function is not identical to
550 ser_unix_readchar() as part of replacing it with ser_unix*()
551 merging will be required - this code handles the case where read()
552 times out due to no data while ser_unix_readchar() doesn't expect
553 that. */
554
c906108c 555static int
2acceee2 556do_hardwire_readchar (serial_t scb, int timeout)
c906108c 557{
7a292a7a
SS
558 int status, delta;
559 int detach = 0;
c906108c 560
c906108c
SS
561 if (timeout > 0)
562 timeout++;
c906108c 563
7a292a7a
SS
564 /* We have to be able to keep the GUI alive here, so we break the original
565 timeout into steps of 1 second, running the "keep the GUI alive" hook
566 each time through the loop.
567 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
568 will only go through the loop once. */
c5aa993b 569
7a292a7a 570 delta = (timeout == 0 ? 0 : 1);
c906108c
SS
571 while (1)
572 {
c906108c 573
7a292a7a
SS
574 /* N.B. The UI may destroy our world (for instance by calling
575 remote_stop,) in which case we want to get out of here as
576 quickly as possible. It is not safe to touch scb, since
577 someone else might have freed it. The ui_loop_hook signals that
578 we should exit by returning 1. */
579
c906108c 580 if (ui_loop_hook)
c5aa993b 581 detach = ui_loop_hook (0);
7a292a7a
SS
582
583 if (detach)
584 return SERIAL_TIMEOUT;
585
586 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
587 status = wait_for (scb, delta);
588
c906108c
SS
589 if (status < 0)
590 return status;
591
2acceee2 592 status = read (scb->fd, scb->buf, BUFSIZ);
c906108c 593
2acceee2 594 if (status <= 0)
c906108c 595 {
2acceee2 596 if (status == 0)
c906108c
SS
597 {
598 /* Zero characters means timeout (it could also be EOF, but
c5aa993b 599 we don't (yet at least) distinguish). */
c906108c
SS
600 if (scb->timeout_remaining > 0)
601 {
602 timeout = scb->timeout_remaining;
603 continue;
604 }
c5aa993b
JM
605 else if (scb->timeout_remaining < 0)
606 continue;
c906108c
SS
607 else
608 return SERIAL_TIMEOUT;
609 }
610 else if (errno == EINTR)
611 continue;
612 else
613 return SERIAL_ERROR; /* Got an error from read */
614 }
615
2acceee2 616 scb->bufcnt = status;
c906108c
SS
617 scb->bufcnt--;
618 scb->bufp = scb->buf;
619 return *scb->bufp++;
620 }
621}
622
2acceee2
JM
623static int
624hardwire_readchar (serial_t scb, int timeout)
625{
626 return generic_readchar (scb, timeout, do_hardwire_readchar);
627}
628
629
c906108c
SS
630#ifndef B19200
631#define B19200 EXTA
632#endif
633
634#ifndef B38400
635#define B38400 EXTB
636#endif
637
638/* Translate baud rates from integers to damn B_codes. Unix should
639 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
640
641static struct
642{
643 int rate;
644 int code;
645}
646baudtab[] =
647{
c5aa993b
JM
648 {
649 50, B50
650 }
651 ,
652 {
653 75, B75
654 }
655 ,
656 {
657 110, B110
658 }
659 ,
660 {
661 134, B134
662 }
663 ,
664 {
665 150, B150
666 }
667 ,
668 {
669 200, B200
670 }
671 ,
672 {
673 300, B300
674 }
675 ,
676 {
677 600, B600
678 }
679 ,
680 {
681 1200, B1200
682 }
683 ,
684 {
685 1800, B1800
686 }
687 ,
688 {
689 2400, B2400
690 }
691 ,
692 {
693 4800, B4800
694 }
695 ,
696 {
697 9600, B9600
698 }
699 ,
700 {
701 19200, B19200
702 }
703 ,
704 {
705 38400, B38400
706 }
707 ,
c906108c 708#ifdef B57600
c5aa993b
JM
709 {
710 57600, B57600
711 }
712 ,
c906108c
SS
713#endif
714#ifdef B115200
c5aa993b
JM
715 {
716 115200, B115200
717 }
718 ,
c906108c
SS
719#endif
720#ifdef B230400
c5aa993b
JM
721 {
722 230400, B230400
723 }
724 ,
c906108c
SS
725#endif
726#ifdef B460800
c5aa993b
JM
727 {
728 460800, B460800
729 }
730 ,
c906108c 731#endif
c5aa993b
JM
732 {
733 -1, -1
734 }
735 ,
c906108c
SS
736};
737
c5aa993b 738static int
c2c6d25f 739rate_to_code (int rate)
c906108c
SS
740{
741 int i;
742
743 for (i = 0; baudtab[i].rate != -1; i++)
08b4f080
FN
744 {
745 /* test for perfect macth. */
746 if (rate == baudtab[i].rate)
747 return baudtab[i].code;
748 else
749 {
750 /* check if it is in between valid values. */
751 if (rate < baudtab[i].rate)
752 {
753 if (i)
754 {
755 warning ("Invalid baud rate %d. Closest values are %d and %d.",
756 rate, baudtab[i - 1].rate, baudtab[i].rate);
757 }
758 else
759 {
760 warning ("Invalid baud rate %d. Minimum value is %d.",
761 rate, baudtab[0].rate);
762 }
763 return -1;
764 }
765 }
766 }
767
768 /* The requested speed was too large. */
769 warning ("Invalid baud rate %d. Maximum value is %d.",
770 rate, baudtab[i - 1].rate);
c906108c
SS
771 return -1;
772}
773
774static int
c2c6d25f 775hardwire_setbaudrate (serial_t scb, int rate)
c906108c
SS
776{
777 struct hardwire_ttystate state;
08b4f080
FN
778 int baud_code = rate_to_code (rate);
779
780 if (baud_code < 0)
781 {
782 /* The baud rate was not valid.
783 A warning has already been issued. */
784 errno = EINVAL;
785 return -1;
786 }
c906108c 787
c5aa993b 788 if (get_tty_state (scb, &state))
c906108c
SS
789 return -1;
790
791#ifdef HAVE_TERMIOS
08b4f080
FN
792 cfsetospeed (&state.termios, baud_code);
793 cfsetispeed (&state.termios, baud_code);
c906108c
SS
794#endif
795
796#ifdef HAVE_TERMIO
797#ifndef CIBAUD
798#define CIBAUD CBAUD
799#endif
800
801 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
08b4f080 802 state.termio.c_cflag |= baud_code;
c906108c
SS
803#endif
804
805#ifdef HAVE_SGTTY
08b4f080
FN
806 state.sgttyb.sg_ispeed = baud_code;
807 state.sgttyb.sg_ospeed = baud_code;
c906108c
SS
808#endif
809
810 return set_tty_state (scb, &state);
811}
812
813static int
fba45db2 814hardwire_setstopbits (serial_t scb, int num)
c906108c
SS
815{
816 struct hardwire_ttystate state;
817 int newbit;
818
c5aa993b 819 if (get_tty_state (scb, &state))
c906108c
SS
820 return -1;
821
822 switch (num)
823 {
824 case SERIAL_1_STOPBITS:
825 newbit = 0;
826 break;
827 case SERIAL_1_AND_A_HALF_STOPBITS:
828 case SERIAL_2_STOPBITS:
829 newbit = 1;
830 break;
831 default:
832 return 1;
833 }
834
835#ifdef HAVE_TERMIOS
836 if (!newbit)
837 state.termios.c_cflag &= ~CSTOPB;
838 else
c5aa993b 839 state.termios.c_cflag |= CSTOPB; /* two bits */
c906108c
SS
840#endif
841
842#ifdef HAVE_TERMIO
843 if (!newbit)
844 state.termio.c_cflag &= ~CSTOPB;
845 else
c5aa993b 846 state.termio.c_cflag |= CSTOPB; /* two bits */
c906108c
SS
847#endif
848
849#ifdef HAVE_SGTTY
850 return 0; /* sgtty doesn't support this */
851#endif
852
853 return set_tty_state (scb, &state);
854}
855
c906108c 856static void
c2c6d25f 857hardwire_close (serial_t scb)
c906108c
SS
858{
859 if (scb->fd < 0)
860 return;
861
c5aa993b 862 close (scb->fd);
c906108c
SS
863 scb->fd = -1;
864}
865
c2c6d25f
JM
866\f
867/* Generic operations used by all UNIX/FD based serial interfaces. */
868
869serial_ttystate
870ser_unix_nop_get_tty_state (serial_t scb)
871{
872 /* allocate a dummy */
873 return (serial_ttystate) XMALLOC (int);
874}
875
876int
877ser_unix_nop_set_tty_state (serial_t scb, serial_ttystate ttystate)
878{
879 return 0;
880}
881
882void
883ser_unix_nop_raw (serial_t scb)
884{
885 return; /* Always in raw mode */
886}
887
888/* Wait for input on scb, with timeout seconds. Returns 0 on success,
889 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
890
891int
892ser_unix_wait_for (serial_t scb, int timeout)
893{
ab5ba170
AC
894 while (1)
895 {
896 int numfds;
897 struct timeval tv;
898 fd_set readfds, exceptfds;
c2c6d25f 899
ab5ba170
AC
900 /* NOTE: Some OS's can scramble the READFDS when the select()
901 call fails (ex the kernel with Red Hat 5.2). Initialize all
902 arguments before each call. */
c2c6d25f 903
ab5ba170
AC
904 tv.tv_sec = timeout;
905 tv.tv_usec = 0;
c2c6d25f 906
ab5ba170
AC
907 FD_ZERO (&readfds);
908 FD_ZERO (&exceptfds);
909 FD_SET (scb->fd, &readfds);
910 FD_SET (scb->fd, &exceptfds);
c2c6d25f 911
c2c6d25f
JM
912 if (timeout >= 0)
913 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
914 else
915 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
916
917 if (numfds <= 0)
918 {
919 if (numfds == 0)
920 return SERIAL_TIMEOUT;
921 else if (errno == EINTR)
922 continue;
923 else
924 return SERIAL_ERROR; /* Got an error from select or poll */
925 }
926
927 return 0;
928 }
929}
930
931/* Read a character with user-specified timeout. TIMEOUT is number of seconds
932 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
933 char if successful. Returns -2 if timeout expired, EOF if line dropped
934 dead, or -3 for any other error (see errno in that case). */
935
2acceee2
JM
936static int
937do_unix_readchar (serial_t scb, int timeout)
c2c6d25f
JM
938{
939 int status;
940 int delta;
941
c2c6d25f
JM
942 /* We have to be able to keep the GUI alive here, so we break the original
943 timeout into steps of 1 second, running the "keep the GUI alive" hook
944 each time through the loop.
945
946 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
9e294fb8 947 will only go through the loop once. */
c2c6d25f 948
9e294fb8 949 delta = (timeout == 0 ? 0 : 1);
c2c6d25f
JM
950 while (1)
951 {
952
953 /* N.B. The UI may destroy our world (for instance by calling
954 remote_stop,) in which case we want to get out of here as
955 quickly as possible. It is not safe to touch scb, since
956 someone else might have freed it. The ui_loop_hook signals that
957 we should exit by returning 1. */
958
959 if (ui_loop_hook)
960 {
961 if (ui_loop_hook (0))
962 return SERIAL_TIMEOUT;
963 }
964
9e294fb8 965 status = ser_unix_wait_for (scb, delta);
faa5effd
FN
966 if (timeout > 0)
967 timeout -= delta;
c2c6d25f 968
9e294fb8
AC
969 /* If we got a character or an error back from wait_for, then we can
970 break from the loop before the timeout is completed. */
c2c6d25f 971
9e294fb8
AC
972 if (status != SERIAL_TIMEOUT)
973 {
974 break;
975 }
c2c6d25f 976
9e294fb8
AC
977 /* If we have exhausted the original timeout, then generate
978 a SERIAL_TIMEOUT, and pass it out of the loop. */
c2c6d25f 979
9e294fb8
AC
980 else if (timeout == 0)
981 {
982 status = SERIAL_TIMEOUT;
983 break;
c2c6d25f 984 }
9e294fb8 985 }
c2c6d25f 986
9e294fb8
AC
987 if (status < 0)
988 return status;
989
990 while (1)
991 {
992 status = read (scb->fd, scb->buf, BUFSIZ);
993 if (status != -1 || errno != EINTR)
994 break;
995 }
996
997 if (status <= 0)
998 {
999 if (status == 0)
1000 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
1001 distinguish between EOF & timeouts
1002 someday] */
1003 else
1004 return SERIAL_ERROR; /* Got an error from read */
c2c6d25f 1005 }
9e294fb8
AC
1006
1007 scb->bufcnt = status;
1008 scb->bufcnt--;
1009 scb->bufp = scb->buf;
1010 return *scb->bufp++;
c2c6d25f
JM
1011}
1012
2acceee2
JM
1013/* Perform operations common to both old and new readchar. */
1014
1015/* Return the next character from the input FIFO. If the FIFO is
1016 empty, call the SERIAL specific routine to try and read in more
1017 characters.
1018
1019 Initially data from the input FIFO is returned (fd_event()
1020 pre-reads the input into that FIFO. Once that has been emptied,
1021 further data is obtained by polling the input FD using the device
1022 specific readchar() function. Note: reschedule() is called after
1023 every read. This is because there is no guarentee that the lower
1024 level fd_event() poll_event() code (which also calls reschedule())
1025 will be called. */
1026
1027static int
1028generic_readchar (serial_t scb, int timeout,
1029 int (do_readchar) (serial_t scb, int timeout))
1030{
1031 int ch;
1032 if (scb->bufcnt > 0)
1033 {
1034 ch = *scb->bufp;
1035 scb->bufcnt--;
1036 scb->bufp++;
1037 }
1038 else if (scb->bufcnt < 0)
1039 {
1040 /* Some errors/eof are are sticky. */
1041 ch = scb->bufcnt;
1042 }
1043 else
1044 {
1045 ch = do_readchar (scb, timeout);
1046 if (ch < 0)
1047 {
1048 switch ((enum serial_rc) ch)
1049 {
1050 case SERIAL_EOF:
1051 case SERIAL_ERROR:
1052 /* Make the error/eof stick. */
1053 scb->bufcnt = ch;
1054 break;
1055 case SERIAL_TIMEOUT:
1056 scb->bufcnt = 0;
1057 break;
1058 }
1059 }
1060 }
1061 reschedule (scb);
1062 return ch;
1063}
1064
1065int
1066ser_unix_readchar (serial_t scb, int timeout)
1067{
1068 return generic_readchar (scb, timeout, do_unix_readchar);
1069}
1070
c2c6d25f
JM
1071int
1072ser_unix_nop_noflush_set_tty_state (serial_t scb,
1073 serial_ttystate new_ttystate,
1074 serial_ttystate old_ttystate)
1075{
1076 return 0;
1077}
1078
1079void
1080ser_unix_nop_print_tty_state (serial_t scb,
1081 serial_ttystate ttystate,
d9fcf2fb 1082 struct ui_file *stream)
c2c6d25f
JM
1083{
1084 /* Nothing to print. */
1085 return;
1086}
1087
1088int
1089ser_unix_nop_setbaudrate (serial_t scb, int rate)
1090{
1091 return 0; /* Never fails! */
1092}
1093
1094int
1095ser_unix_nop_setstopbits (serial_t scb, int num)
1096{
1097 return 0; /* Never fails! */
1098}
1099
1100int
1101ser_unix_write (serial_t scb, const char *str, int len)
1102{
1103 int cc;
1104
1105 while (len > 0)
1106 {
1107 cc = write (scb->fd, str, len);
1108
1109 if (cc < 0)
1110 return 1;
1111 len -= cc;
1112 str += cc;
1113 }
1114 return 0;
1115}
1116
1117int
1118ser_unix_nop_flush_output (serial_t scb)
1119{
1120 return 0;
1121}
1122
1123int
2acceee2 1124ser_unix_flush_input (serial_t scb)
c2c6d25f 1125{
2acceee2
JM
1126 if (scb->bufcnt >= 0)
1127 {
1128 scb->bufcnt = 0;
1129 scb->bufp = scb->buf;
1130 return 0;
1131 }
1132 else
1133 return SERIAL_ERROR;
c2c6d25f
JM
1134}
1135
1136int
1137ser_unix_nop_send_break (serial_t scb)
1138{
1139 return 0;
1140}
1141
1142int
1143ser_unix_nop_drain_output (serial_t scb)
1144{
1145 return 0;
1146}
1147
2acceee2
JM
1148
1149\f
1150/* Event handling for ASYNC serial code.
1151
1152 At any time the SERIAL device either: has an empty FIFO and is
1153 waiting on a FD event; or has a non-empty FIFO/error condition and
1154 is constantly scheduling timer events.
1155
1156 ASYNC only stops pestering its client when it is de-async'ed or it
1157 is told to go away. */
1158
1159/* Value of scb->async_state: */
1160enum {
1161 /* >= 0 (TIMER_SCHEDULED) */
1162 /* The ID of the currently scheduled timer event. This state is
1163 rarely encountered. Timer events are one-off so as soon as the
1164 event is delivered the state is shanged to NOTHING_SCHEDULED. */
1165 FD_SCHEDULED = -1,
1166 /* The fd_event() handler is scheduled. It is called when ever the
1167 file descriptor becomes ready. */
1168 NOTHING_SCHEDULED = -2
1169 /* Either no task is scheduled (just going into ASYNC mode) or a
1170 timer event has just gone off and the current state has been
1171 forced into nothing scheduled. */
1172};
1173
1174/* Identify and schedule the next ASYNC task based on scb->async_state
1175 and scb->buf* (the input FIFO). A state machine is used to avoid
1176 the need to make redundant calls into the event-loop - the next
1177 scheduled task is only changed when needed. */
1178
c2c6d25f 1179static void
2acceee2
JM
1180reschedule (serial_t scb)
1181{
1182 if (SERIAL_IS_ASYNC_P (scb))
1183 {
1184 int next_state;
1185 switch (scb->async_state)
1186 {
1187 case FD_SCHEDULED:
1188 if (scb->bufcnt == 0)
1189 next_state = FD_SCHEDULED;
1190 else
1191 {
1192 delete_file_handler (scb->fd);
1193 next_state = create_timer (0, push_event, scb);
1194 }
1195 break;
1196 case NOTHING_SCHEDULED:
1197 if (scb->bufcnt == 0)
1198 {
1199 add_file_handler (scb->fd, fd_event, scb);
1200 next_state = FD_SCHEDULED;
1201 }
1202 else
1203 {
1204 next_state = create_timer (0, push_event, scb);
1205 }
1206 break;
1207 default: /* TIMER SCHEDULED */
1208 if (scb->bufcnt == 0)
1209 {
1210 delete_timer (scb->async_state);
1211 add_file_handler (scb->fd, fd_event, scb);
1212 next_state = FD_SCHEDULED;
1213 }
1214 else
1215 next_state = scb->async_state;
1216 break;
1217 }
1218 if (SERIAL_DEBUG_P (scb))
1219 {
1220 switch (next_state)
1221 {
1222 case FD_SCHEDULED:
1223 if (scb->async_state != FD_SCHEDULED)
1224 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
1225 scb->fd);
1226 break;
1227 default: /* TIMER SCHEDULED */
1228 if (scb->async_state == FD_SCHEDULED)
1229 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
1230 scb->fd);
1231 break;
1232 }
1233 }
1234 scb->async_state = next_state;
1235 }
1236}
1237
1238/* FD_EVENT: This is scheduled when the input FIFO is empty (and there
1239 is no pending error). As soon as data arrives, it is read into the
1240 input FIFO and the client notified. The client should then drain
1241 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
1242 push_event() is used to nag the client until it is. */
1243
1244static void
1245fd_event (int error, void *context)
1246{
1247 serial_t scb = context;
1248 if (error != 0)
1249 {
1250 scb->bufcnt = SERIAL_ERROR;
1251 }
1252 else if (scb->bufcnt == 0)
1253 {
1254 /* Prime the input FIFO. The readchar() function is used to
1255 pull characters out of the buffer. See also
1256 generic_readchar(). */
1257 int nr;
1258 do
1259 {
1260 nr = read (scb->fd, scb->buf, BUFSIZ);
1261 }
1262 while (nr == -1 && errno == EINTR);
1263 if (nr == 0)
1264 {
1265 scb->bufcnt = SERIAL_EOF;
1266 }
1267 else if (nr > 0)
1268 {
1269 scb->bufcnt = nr;
1270 scb->bufp = scb->buf;
1271 }
1272 else
1273 {
1274 scb->bufcnt = SERIAL_ERROR;
1275 }
1276 }
1277 scb->async_handler (scb, scb->async_context);
1278 reschedule (scb);
1279}
1280
1281/* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
1282 error). Nag the client until all the data has been read. In the
1283 case of errors, the client will need to close or de-async the
1284 device before naging stops. */
1285
1286static void
1287push_event (void *context)
c2c6d25f
JM
1288{
1289 serial_t scb = context;
2acceee2
JM
1290 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
1291 scb->async_handler (scb, scb->async_context);
1292 /* re-schedule */
1293 reschedule (scb);
c2c6d25f
JM
1294}
1295
2acceee2
JM
1296/* Put the SERIAL device into/out-of ASYNC mode. */
1297
c2c6d25f
JM
1298void
1299ser_unix_async (serial_t scb,
1300 int async_p)
1301{
1302 if (async_p)
1303 {
2acceee2
JM
1304 /* Force a re-schedule. */
1305 scb->async_state = NOTHING_SCHEDULED;
1306 if (SERIAL_DEBUG_P (scb))
1307 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
1308 scb->fd);
1309 reschedule (scb);
c2c6d25f
JM
1310 }
1311 else
1312 {
2acceee2
JM
1313 if (SERIAL_DEBUG_P (scb))
1314 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
1315 scb->fd);
8e1a459b 1316 /* De-schedule whatever tasks are currently scheduled. */
2acceee2
JM
1317 switch (scb->async_state)
1318 {
1319 case FD_SCHEDULED:
1320 delete_file_handler (scb->fd);
1321 break;
1322 NOTHING_SCHEDULED:
1323 break;
1324 default: /* TIMER SCHEDULED */
1325 delete_timer (scb->async_state);
1326 break;
1327 }
c2c6d25f
JM
1328 }
1329}
c906108c
SS
1330
1331void
c2c6d25f 1332_initialize_ser_hardwire (void)
c906108c 1333{
c2c6d25f
JM
1334 struct serial_ops *ops = XMALLOC (struct serial_ops);
1335 memset (ops, sizeof (struct serial_ops), 0);
1336 ops->name = "hardwire";
1337 ops->next = 0;
1338 ops->open = hardwire_open;
1339 ops->close = hardwire_close;
1340 /* FIXME: Don't replace this with the equivalent ser_unix*() until
1341 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1342 1999-09-16. */
1343 ops->readchar = hardwire_readchar;
2acceee2 1344 ops->write = ser_unix_write;
c2c6d25f
JM
1345 ops->flush_output = hardwire_flush_output;
1346 ops->flush_input = hardwire_flush_input;
1347 ops->send_break = hardwire_send_break;
1348 ops->go_raw = hardwire_raw;
1349 ops->get_tty_state = hardwire_get_tty_state;
1350 ops->set_tty_state = hardwire_set_tty_state;
1351 ops->print_tty_state = hardwire_print_tty_state;
1352 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1353 ops->setbaudrate = hardwire_setbaudrate;
1354 ops->setstopbits = hardwire_setstopbits;
1355 ops->drain_output = hardwire_drain_output;
1356 ops->async = ser_unix_async;
1357 serial_add_interface (ops);
c906108c 1358}