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