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