* ldmain.c (main): Error if --gc-sections and
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
CommitLineData
4e772f44 1/* Serial interface for local (hardwired) serial ports on Un*x like systems
63c3e305 2 Copyright 1992, 1993, 1994, 1998 Free Software Foundation, Inc.
4e772f44
SG
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
6c9638b4 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
4e772f44
SG
19
20#include "defs.h"
21#include "serial.h"
22#include <fcntl.h>
23#include <sys/types.h>
dedcc91d
SC
24#include "terminal.h"
25#ifdef HAVE_UNISTD_H
26#include <unistd.h>
4e772f44
SG
27#endif
28
29#ifdef HAVE_TERMIOS
38dc5e12
SG
30
31struct hardwire_ttystate
32{
33 struct termios termios;
34};
dc34b11d 35#endif /* termios */
38dc5e12 36
4e772f44 37#ifdef HAVE_TERMIO
38dc5e12 38
dc34b11d
JK
39/* It is believed that all systems which have added job control to SVR3
40 (e.g. sco) have also added termios. Even if not, trying to figure out
41 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
42 bewildering. So we don't attempt it. */
43
38dc5e12
SG
44struct hardwire_ttystate
45{
46 struct termio termio;
47};
dc34b11d 48#endif /* termio */
38dc5e12 49
4e772f44 50#ifdef HAVE_SGTTY
68d2db62
JK
51/* Needed for the code which uses select(). We would include <sys/select.h>
52 too if it existed on all systems. */
53#include <sys/time.h>
54
38dc5e12
SG
55struct hardwire_ttystate
56{
57 struct sgttyb sgttyb;
c2e247c4
JK
58 struct tchars tc;
59 struct ltchars ltc;
60 /* Line discipline flags. */
61 int lmode;
38dc5e12 62};
dc34b11d 63#endif /* sgtty */
4e772f44 64
9775789d
SG
65static int hardwire_open PARAMS ((serial_t scb, const char *name));
66static void hardwire_raw PARAMS ((serial_t scb));
67static int wait_for PARAMS ((serial_t scb, int timeout));
68static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
69static int rate_to_code PARAMS ((int rate));
70static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
71static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
9775789d 72static void hardwire_close PARAMS ((serial_t scb));
38dc5e12
SG
73static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
74static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
75static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
76static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
b607efe7
FF
77static int hardwire_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
78 serial_ttystate));
79static void hardwire_print_tty_state PARAMS ((serial_t, serial_ttystate));
3ffbdf15 80static int hardwire_drain_output PARAMS ((serial_t));
b607efe7
FF
81static int hardwire_flush_output PARAMS ((serial_t));
82static int hardwire_flush_input PARAMS ((serial_t));
83static int hardwire_send_break PARAMS ((serial_t));
84static int hardwire_setstopbits PARAMS ((serial_t, int));
9775789d 85
21b3bc77 86#ifdef __CYGWIN32__
c2867a6c 87extern void (*ui_loop_hook) PARAMS ((int));
21b3bc77
KS
88#endif
89
4e772f44
SG
90/* Open up a real live device for serial I/O */
91
92static int
93hardwire_open(scb, name)
94 serial_t scb;
95 const char *name;
96{
97 scb->fd = open (name, O_RDWR);
98 if (scb->fd < 0)
4febd102 99 return -1;
4e772f44
SG
100
101 return 0;
102}
103
38dc5e12
SG
104static int
105get_tty_state(scb, state)
4e772f44 106 serial_t scb;
38dc5e12 107 struct hardwire_ttystate *state;
4e772f44
SG
108{
109#ifdef HAVE_TERMIOS
057c2f47 110 extern int errno;
c2e247c4
JK
111
112 if (tcgetattr(scb->fd, &state->termios) < 0)
113 return -1;
114
c2e247c4 115 return 0;
38dc5e12 116#endif
4e772f44 117
38dc5e12 118#ifdef HAVE_TERMIO
c2e247c4
JK
119 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
120 return -1;
dc34b11d 121 return 0;
38dc5e12 122#endif
4e772f44 123
38dc5e12 124#ifdef HAVE_SGTTY
c2e247c4
JK
125 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
126 return -1;
127 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
128 return -1;
129 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
130 return -1;
131 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
132 return -1;
133
a14a8fad 134 return 0;
38dc5e12
SG
135#endif
136}
4e772f44 137
38dc5e12
SG
138static int
139set_tty_state(scb, state)
140 serial_t scb;
141 struct hardwire_ttystate *state;
142{
38dc5e12 143#ifdef HAVE_TERMIOS
c2e247c4
JK
144 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
145 return -1;
146
a14a8fad 147 return 0;
4e772f44
SG
148#endif
149
150#ifdef HAVE_TERMIO
c2e247c4
JK
151 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
152 return -1;
c2e247c4 153 return 0;
38dc5e12 154#endif
4e772f44 155
38dc5e12 156#ifdef HAVE_SGTTY
c2e247c4
JK
157 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
158 return -1;
88cc9a42
RP
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;
c2e247c4 165
a14a8fad 166 return 0;
38dc5e12
SG
167#endif
168}
4e772f44 169
38dc5e12
SG
170static serial_ttystate
171hardwire_get_tty_state(scb)
172 serial_t scb;
173{
174 struct hardwire_ttystate *state;
4e772f44 175
38dc5e12 176 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
4e772f44 177
38dc5e12
SG
178 if (get_tty_state(scb, state))
179 return NULL;
4e772f44 180
38dc5e12
SG
181 return (serial_ttystate)state;
182}
4e772f44 183
38dc5e12
SG
184static int
185hardwire_set_tty_state(scb, ttystate)
186 serial_t scb;
187 serial_ttystate ttystate;
188{
189 struct hardwire_ttystate *state;
4e772f44 190
38dc5e12
SG
191 state = (struct hardwire_ttystate *)ttystate;
192
193 return set_tty_state(scb, state);
194}
195
c2e247c4
JK
196static int
197hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
198 serial_t scb;
199 serial_ttystate new_ttystate;
200 serial_ttystate old_ttystate;
201{
3fe11d47 202 struct hardwire_ttystate new_state;
6b14af2b 203#ifdef HAVE_SGTTY
c2e247c4 204 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
6b14af2b 205#endif
c2e247c4 206
3fe11d47
JK
207 new_state = *(struct hardwire_ttystate *)new_ttystate;
208
d881dd86
JK
209 /* Don't change in or out of raw mode; we don't want to flush input.
210 termio and termios have no such restriction; for them flushing input
211 is separate from setting the attributes. */
c2e247c4
JK
212
213#ifdef HAVE_SGTTY
214 if (state->sgttyb.sg_flags & RAW)
215 new_state.sgttyb.sg_flags |= RAW;
216 else
217 new_state.sgttyb.sg_flags &= ~RAW;
218
219 /* I'm not sure whether this is necessary; the manpage just mentions
220 RAW not CBREAK. */
221 if (state->sgttyb.sg_flags & CBREAK)
222 new_state.sgttyb.sg_flags |= CBREAK;
223 else
224 new_state.sgttyb.sg_flags &= ~CBREAK;
225#endif
226
227 return set_tty_state (scb, &new_state);
228}
229
230static void
231hardwire_print_tty_state (scb, ttystate)
232 serial_t scb;
233 serial_ttystate ttystate;
234{
235 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
236 int i;
237
dc34b11d 238#ifdef HAVE_TERMIOS
c2e247c4
JK
239 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
240 state->termios.c_iflag, state->termios.c_oflag);
a77a5278
JK
241 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
242 state->termios.c_cflag, state->termios.c_lflag);
243#if 0
244 /* This not in POSIX, and is not really documented by those systems
245 which have it (at least not Sun). */
246 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
247#endif
c2e247c4
JK
248 printf_filtered ("c_cc: ");
249 for (i = 0; i < NCCS; i += 1)
250 printf_filtered ("0x%x ", state->termios.c_cc[i]);
251 printf_filtered ("\n");
252#endif
253
254#ifdef HAVE_TERMIO
255 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
256 state->termio.c_iflag, state->termio.c_oflag);
257 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
258 state->termio.c_cflag, state->termio.c_lflag,
259 state->termio.c_line);
260 printf_filtered ("c_cc: ");
261 for (i = 0; i < NCC; i += 1)
262 printf_filtered ("0x%x ", state->termio.c_cc[i]);
263 printf_filtered ("\n");
264#endif
265
266#ifdef HAVE_SGTTY
267 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
268
269 printf_filtered ("tchars: ");
270 for (i = 0; i < (int)sizeof (struct tchars); i++)
271 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
272 printf_filtered ("\n");
273
274 printf_filtered ("ltchars: ");
275 for (i = 0; i < (int)sizeof (struct ltchars); i++)
276 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
277 printf_filtered ("\n");
278
279 printf_filtered ("lmode: 0x%x\n", state->lmode);
280#endif
281}
282
3ffbdf15
FF
283/* Wait for the output to drain away, as opposed to flushing (discarding) it */
284
285static int
286hardwire_drain_output (scb)
287 serial_t scb;
288{
289#ifdef HAVE_TERMIOS
290 return tcdrain (scb->fd);
291#endif
292
293#ifdef HAVE_TERMIO
294 return ioctl (scb->fd, TCSBRK, 1);
295#endif
296
297#ifdef HAVE_SGTTY
298 /* Get the current state and then restore it using TIOCSETP,
299 which should cause the output to drain and pending input
300 to be discarded. */
301 {
302 struct hardwire_ttystate state;
303 if (get_tty_state (scb, &state))
304 {
305 return (-1);
306 }
307 else
308 {
309 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
310 }
311 }
312#endif
313}
314
c2e247c4
JK
315static int
316hardwire_flush_output (scb)
317 serial_t scb;
318{
319#ifdef HAVE_TERMIOS
320 return tcflush (scb->fd, TCOFLUSH);
321#endif
322
323#ifdef HAVE_TERMIO
324 return ioctl (scb->fd, TCFLSH, 1);
325#endif
326
327#ifdef HAVE_SGTTY
328 /* This flushes both input and output, but we can't do better. */
329 return ioctl (scb->fd, TIOCFLUSH, 0);
330#endif
331}
332
704deef2
JK
333static int
334hardwire_flush_input (scb)
335 serial_t scb;
336{
dedcc91d
SC
337 scb->bufcnt = 0;
338 scb->bufp = scb->buf;
339
704deef2
JK
340#ifdef HAVE_TERMIOS
341 return tcflush (scb->fd, TCIFLUSH);
342#endif
343
344#ifdef HAVE_TERMIO
345 return ioctl (scb->fd, TCFLSH, 0);
346#endif
347
348#ifdef HAVE_SGTTY
349 /* This flushes both input and output, but we can't do better. */
350 return ioctl (scb->fd, TIOCFLUSH, 0);
351#endif
352}
353
354static int
355hardwire_send_break (scb)
356 serial_t scb;
357{
704deef2
JK
358#ifdef HAVE_TERMIOS
359 return tcsendbreak (scb->fd, 0);
360#endif
361
362#ifdef HAVE_TERMIO
363 return ioctl (scb->fd, TCSBRK, 0);
364#endif
365
366#ifdef HAVE_SGTTY
95a98b5e 367 {
0ac0a9f6 368 int status;
95a98b5e
JK
369 struct timeval timeout;
370
371 status = ioctl (scb->fd, TIOCSBRK, 0);
372
373 /* Can't use usleep; it doesn't exist in BSD 4.2. */
374 /* Note that if this select() is interrupted by a signal it will not wait
375 the full length of time. I think that is OK. */
376 timeout.tv_sec = 0;
377 timeout.tv_usec = 250000;
378 select (0, 0, 0, 0, &timeout);
379 status = ioctl (scb->fd, TIOCCBRK, 0);
380 return status;
381 }
704deef2
JK
382#endif
383}
384
38dc5e12
SG
385static void
386hardwire_raw(scb)
387 serial_t scb;
388{
389 struct hardwire_ttystate state;
390
391 if (get_tty_state(scb, &state))
199b2450 392 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
38dc5e12
SG
393
394#ifdef HAVE_TERMIOS
395 state.termios.c_iflag = 0;
396 state.termios.c_oflag = 0;
397 state.termios.c_lflag = 0;
398 state.termios.c_cflag &= ~(CSIZE|PARENB);
2e6784a8 399 state.termios.c_cflag |= CLOCAL | CS8;
38dc5e12
SG
400 state.termios.c_cc[VMIN] = 0;
401 state.termios.c_cc[VTIME] = 0;
402#endif
403
404#ifdef HAVE_TERMIO
405 state.termio.c_iflag = 0;
406 state.termio.c_oflag = 0;
407 state.termio.c_lflag = 0;
408 state.termio.c_cflag &= ~(CSIZE|PARENB);
2e6784a8 409 state.termio.c_cflag |= CLOCAL | CS8;
38dc5e12
SG
410 state.termio.c_cc[VMIN] = 0;
411 state.termio.c_cc[VTIME] = 0;
412#endif
413
414#ifdef HAVE_SGTTY
415 state.sgttyb.sg_flags |= RAW | ANYP;
416 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
4e772f44 417#endif
9e15da4a
SG
418
419 scb->current_timeout = 0;
38dc5e12
SG
420
421 if (set_tty_state (scb, &state))
199b2450 422 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
4e772f44
SG
423}
424
9e15da4a
SG
425/* Wait for input on scb, with timeout seconds. Returns 0 on success,
426 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
427
428 For termio{s}, we actually just setup VTIME if necessary, and let the
429 timeout occur in the read() in hardwire_read().
430 */
4e772f44
SG
431
432static int
9775789d 433wait_for(scb, timeout)
4e772f44
SG
434 serial_t scb;
435 int timeout;
436{
21b3bc77 437#ifndef __CYGWIN32__
9db58d3a 438 scb->timeout_remaining = 0;
21b3bc77 439#endif
9db58d3a 440
9775789d 441#ifdef HAVE_SGTTY
9db58d3a
JK
442 {
443 struct timeval tv;
444 fd_set readfds;
eca29634 445
9db58d3a 446 FD_ZERO (&readfds);
eca29634 447
9db58d3a
JK
448 tv.tv_sec = timeout;
449 tv.tv_usec = 0;
eca29634 450
9db58d3a 451 FD_SET(scb->fd, &readfds);
eca29634 452
9db58d3a
JK
453 while (1)
454 {
455 int numfds;
456
457 if (timeout >= 0)
458 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
a037b21e 459 else
9db58d3a 460 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
a037b21e 461
9db58d3a
JK
462 if (numfds <= 0)
463 if (numfds == 0)
464 return SERIAL_TIMEOUT;
465 else if (errno == EINTR)
466 continue;
467 else
468 return SERIAL_ERROR; /* Got an error from select or poll */
9e15da4a 469
9db58d3a
JK
470 return 0;
471 }
472 }
9775789d 473#endif /* HAVE_SGTTY */
4e772f44 474
9775789d 475#if defined HAVE_TERMIO || defined HAVE_TERMIOS
9e15da4a
SG
476 if (timeout == scb->current_timeout)
477 return 0;
4e772f44 478
9db58d3a
JK
479 scb->current_timeout = timeout;
480
9e15da4a 481 {
38dc5e12 482 struct hardwire_ttystate state;
eca29634 483
38dc5e12 484 if (get_tty_state(scb, &state))
199b2450 485 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
eca29634 486
38dc5e12 487#ifdef HAVE_TERMIOS
864df7e6
JK
488 if (timeout < 0)
489 {
490 /* No timeout. */
491 state.termios.c_cc[VTIME] = 0;
492 state.termios.c_cc[VMIN] = 1;
493 }
494 else
495 {
496 state.termios.c_cc[VMIN] = 0;
497 state.termios.c_cc[VTIME] = timeout * 10;
498 if (state.termios.c_cc[VTIME] != timeout * 10)
499 {
9db58d3a
JK
500
501 /* If c_cc is an 8-bit signed character, we can't go
502 bigger than this. If it is always unsigned, we could use
503 25. */
504
505 scb->current_timeout = 12;
506 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
507 scb->timeout_remaining = timeout - scb->current_timeout;
864df7e6
JK
508 }
509 }
38dc5e12 510#endif
9775789d 511
9e15da4a 512#ifdef HAVE_TERMIO
864df7e6
JK
513 if (timeout < 0)
514 {
515 /* No timeout. */
516 state.termio.c_cc[VTIME] = 0;
517 state.termio.c_cc[VMIN] = 1;
518 }
519 else
520 {
521 state.termio.c_cc[VMIN] = 0;
522 state.termio.c_cc[VTIME] = timeout * 10;
523 if (state.termio.c_cc[VTIME] != timeout * 10)
524 {
9db58d3a
JK
525 /* If c_cc is an 8-bit signed character, we can't go
526 bigger than this. If it is always unsigned, we could use
527 25. */
528
529 scb->current_timeout = 12;
5c07a10b 530 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
9db58d3a 531 scb->timeout_remaining = timeout - scb->current_timeout;
864df7e6
JK
532 }
533 }
38dc5e12 534#endif
9e15da4a 535
38dc5e12 536 if (set_tty_state (scb, &state))
199b2450 537 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
9e15da4a 538
9e15da4a
SG
539 return 0;
540 }
541#endif /* HAVE_TERMIO || HAVE_TERMIOS */
9775789d
SG
542}
543
544/* Read a character with user-specified timeout. TIMEOUT is number of seconds
545 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
057c2f47
RP
546 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
547 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
9775789d
SG
548static int
549hardwire_readchar(scb, timeout)
550 serial_t scb;
551 int timeout;
552{
21b3bc77 553 int status, t;
9775789d
SG
554
555 if (scb->bufcnt-- > 0)
556 return *scb->bufp++;
557
21b3bc77
KS
558#ifdef __CYGWIN32__
559 if (timeout > 0)
560 timeout++;
561#endif
562
9db58d3a
JK
563 while (1)
564 {
21b3bc77
KS
565#ifdef __CYGWIN32__
566 t = timeout == 0 ? 0 : 1;
567 scb->timeout_remaining = timeout < 0 ? timeout : timeout - t;
568 status = wait_for (scb, t);
569
570 /* -2 means disable timer */
571 if (ui_loop_hook)
572 ui_loop_hook (-2);
573#else
9db58d3a 574 status = wait_for (scb, timeout);
21b3bc77 575#endif
9db58d3a
JK
576 if (status < 0)
577 return status;
578
579 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
580
581 if (scb->bufcnt <= 0)
582 {
583 if (scb->bufcnt == 0)
584 {
585 /* Zero characters means timeout (it could also be EOF, but
586 we don't (yet at least) distinguish). */
587 if (scb->timeout_remaining > 0)
588 {
589 timeout = scb->timeout_remaining;
590 continue;
591 }
21b3bc77
KS
592#ifdef __CYGWIN32__
593 else if (scb->timeout_remaining < 0)
594 continue;
595#endif
9db58d3a
JK
596 else
597 return SERIAL_TIMEOUT;
598 }
2e6784a8
SG
599 else if (errno == EINTR)
600 continue;
9db58d3a
JK
601 else
602 return SERIAL_ERROR; /* Got an error from read */
603 }
604
605 scb->bufcnt--;
606 scb->bufp = scb->buf;
607 return *scb->bufp++;
608 }
4e772f44
SG
609}
610
611#ifndef B19200
612#define B19200 EXTA
613#endif
614
615#ifndef B38400
616#define B38400 EXTB
617#endif
618
619/* Translate baud rates from integers to damn B_codes. Unix should
620 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
621
622static struct
623{
624 int rate;
625 int code;
626}
627baudtab[] =
628{
629 {50, B50},
630 {75, B75},
631 {110, B110},
632 {134, B134},
633 {150, B150},
634 {200, B200},
635 {300, B300},
636 {600, B600},
637 {1200, B1200},
638 {1800, B1800},
639 {2400, B2400},
640 {4800, B4800},
641 {9600, B9600},
642 {19200, B19200},
643 {38400, B38400},
e5a940e2
SG
644#ifdef B57600
645 {57600, B57600},
646#endif
647#ifdef B115200
648 {115200, B115200},
649#endif
650#ifdef B230400
651 {230400, B230400},
652#endif
653#ifdef B460800
654 {460800, B460800},
655#endif
4e772f44
SG
656 {-1, -1},
657};
658
659static int
660rate_to_code(rate)
661 int rate;
662{
663 int i;
664
665 for (i = 0; baudtab[i].rate != -1; i++)
666 if (rate == baudtab[i].rate)
667 return baudtab[i].code;
668
669 return -1;
670}
671
672static int
673hardwire_setbaudrate(scb, rate)
674 serial_t scb;
675 int rate;
676{
38dc5e12 677 struct hardwire_ttystate state;
4e772f44 678
38dc5e12 679 if (get_tty_state(scb, &state))
4febd102 680 return -1;
4e772f44 681
38dc5e12
SG
682#ifdef HAVE_TERMIOS
683 cfsetospeed (&state.termios, rate_to_code (rate));
684 cfsetispeed (&state.termios, rate_to_code (rate));
4e772f44
SG
685#endif
686
687#ifdef HAVE_TERMIO
4e772f44
SG
688#ifndef CIBAUD
689#define CIBAUD CBAUD
690#endif
691
38dc5e12
SG
692 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
693 state.termio.c_cflag |= rate_to_code (rate);
4e772f44
SG
694#endif
695
696#ifdef HAVE_SGTTY
38dc5e12
SG
697 state.sgttyb.sg_ispeed = rate_to_code (rate);
698 state.sgttyb.sg_ospeed = rate_to_code (rate);
4e772f44 699#endif
38dc5e12
SG
700
701 return set_tty_state (scb, &state);
4e772f44
SG
702}
703
85c8b135
SG
704static int
705hardwire_setstopbits(scb, num)
706 serial_t scb;
707 int num;
708{
709 struct hardwire_ttystate state;
710 int newbit;
711
712 if (get_tty_state(scb, &state))
713 return -1;
714
715 switch (num)
716 {
717 case SERIAL_1_STOPBITS:
718 newbit = 0;
719 break;
720 case SERIAL_1_AND_A_HALF_STOPBITS:
721 case SERIAL_2_STOPBITS:
722 newbit = 1;
723 break;
724 default:
725 return 1;
726 }
727
728#ifdef HAVE_TERMIOS
729 if (!newbit)
730 state.termios.c_cflag &= ~CSTOPB;
731 else
732 state.termios.c_cflag |= CSTOPB; /* two bits */
733#endif
734
735#ifdef HAVE_TERMIO
736 if (!newbit)
737 state.termio.c_cflag &= ~CSTOPB;
738 else
739 state.termio.c_cflag |= CSTOPB; /* two bits */
740#endif
741
742#ifdef HAVE_SGTTY
743 return 0; /* sgtty doesn't support this */
744#endif
745
746 return set_tty_state (scb, &state);
747}
748
4e772f44
SG
749static int
750hardwire_write(scb, str, len)
751 serial_t scb;
752 const char *str;
753 int len;
754{
755 int cc;
756
757 while (len > 0)
758 {
759 cc = write(scb->fd, str, len);
760
761 if (cc < 0)
762 return 1;
763 len -= cc;
764 str += cc;
765 }
766 return 0;
767}
768
4e772f44
SG
769static void
770hardwire_close(scb)
771 serial_t scb;
772{
773 if (scb->fd < 0)
774 return;
775
4e772f44
SG
776 close(scb->fd);
777 scb->fd = -1;
778}
779
780static struct serial_ops hardwire_ops =
781{
782 "hardwire",
783 0,
784 hardwire_open,
785 hardwire_close,
786 hardwire_readchar,
787 hardwire_write,
c2e247c4 788 hardwire_flush_output,
704deef2
JK
789 hardwire_flush_input,
790 hardwire_send_break,
4e772f44 791 hardwire_raw,
38dc5e12
SG
792 hardwire_get_tty_state,
793 hardwire_set_tty_state,
c2e247c4
JK
794 hardwire_print_tty_state,
795 hardwire_noflush_set_tty_state,
38dc5e12 796 hardwire_setbaudrate,
85c8b135 797 hardwire_setstopbits,
3ffbdf15 798 hardwire_drain_output, /* wait for output to drain */
4e772f44
SG
799};
800
9775789d 801void
4e772f44
SG
802_initialize_ser_hardwire ()
803{
804 serial_add_interface (&hardwire_ops);
805}
This page took 0.330518 seconds and 4 git commands to generate.