Fix gcc -Wall warnings. See ChangeLog for details.
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
CommitLineData
4e772f44 1/* Serial interface for local (hardwired) serial ports on Un*x like systems
5c07a10b 2 Copyright 1992, 1993, 1994 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
4e772f44
SG
86/* Open up a real live device for serial I/O */
87
88static int
89hardwire_open(scb, name)
90 serial_t scb;
91 const char *name;
92{
93 scb->fd = open (name, O_RDWR);
94 if (scb->fd < 0)
4febd102 95 return -1;
4e772f44
SG
96
97 return 0;
98}
99
38dc5e12
SG
100static int
101get_tty_state(scb, state)
4e772f44 102 serial_t scb;
38dc5e12 103 struct hardwire_ttystate *state;
4e772f44
SG
104{
105#ifdef HAVE_TERMIOS
057c2f47 106 extern int errno;
c2e247c4
JK
107
108 if (tcgetattr(scb->fd, &state->termios) < 0)
109 return -1;
110
c2e247c4 111 return 0;
38dc5e12 112#endif
4e772f44 113
38dc5e12 114#ifdef HAVE_TERMIO
c2e247c4
JK
115 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
116 return -1;
dc34b11d 117 return 0;
38dc5e12 118#endif
4e772f44 119
38dc5e12 120#ifdef HAVE_SGTTY
c2e247c4
JK
121 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
122 return -1;
123 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
124 return -1;
125 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
126 return -1;
127 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
128 return -1;
129
a14a8fad 130 return 0;
38dc5e12
SG
131#endif
132}
4e772f44 133
38dc5e12
SG
134static int
135set_tty_state(scb, state)
136 serial_t scb;
137 struct hardwire_ttystate *state;
138{
38dc5e12 139#ifdef HAVE_TERMIOS
c2e247c4
JK
140 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
141 return -1;
142
a14a8fad 143 return 0;
4e772f44
SG
144#endif
145
146#ifdef HAVE_TERMIO
c2e247c4
JK
147 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
148 return -1;
c2e247c4 149 return 0;
38dc5e12 150#endif
4e772f44 151
38dc5e12 152#ifdef HAVE_SGTTY
c2e247c4
JK
153 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
154 return -1;
88cc9a42
RP
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;
c2e247c4 161
a14a8fad 162 return 0;
38dc5e12
SG
163#endif
164}
4e772f44 165
38dc5e12
SG
166static serial_ttystate
167hardwire_get_tty_state(scb)
168 serial_t scb;
169{
170 struct hardwire_ttystate *state;
4e772f44 171
38dc5e12 172 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
4e772f44 173
38dc5e12
SG
174 if (get_tty_state(scb, state))
175 return NULL;
4e772f44 176
38dc5e12
SG
177 return (serial_ttystate)state;
178}
4e772f44 179
38dc5e12
SG
180static int
181hardwire_set_tty_state(scb, ttystate)
182 serial_t scb;
183 serial_ttystate ttystate;
184{
185 struct hardwire_ttystate *state;
4e772f44 186
38dc5e12
SG
187 state = (struct hardwire_ttystate *)ttystate;
188
189 return set_tty_state(scb, state);
190}
191
c2e247c4
JK
192static int
193hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
194 serial_t scb;
195 serial_ttystate new_ttystate;
196 serial_ttystate old_ttystate;
197{
3fe11d47 198 struct hardwire_ttystate new_state;
6b14af2b 199#ifdef HAVE_SGTTY
c2e247c4 200 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
6b14af2b 201#endif
c2e247c4 202
3fe11d47
JK
203 new_state = *(struct hardwire_ttystate *)new_ttystate;
204
d881dd86
JK
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. */
c2e247c4
JK
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
227hardwire_print_tty_state (scb, ttystate)
228 serial_t scb;
229 serial_ttystate ttystate;
230{
231 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
232 int i;
233
dc34b11d 234#ifdef HAVE_TERMIOS
c2e247c4
JK
235 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
236 state->termios.c_iflag, state->termios.c_oflag);
a77a5278
JK
237 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
238 state->termios.c_cflag, state->termios.c_lflag);
239#if 0
240 /* This not in POSIX, and is not really documented by those systems
241 which have it (at least not Sun). */
242 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
243#endif
c2e247c4
JK
244 printf_filtered ("c_cc: ");
245 for (i = 0; i < NCCS; i += 1)
246 printf_filtered ("0x%x ", state->termios.c_cc[i]);
247 printf_filtered ("\n");
248#endif
249
250#ifdef HAVE_TERMIO
251 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
252 state->termio.c_iflag, state->termio.c_oflag);
253 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
254 state->termio.c_cflag, state->termio.c_lflag,
255 state->termio.c_line);
256 printf_filtered ("c_cc: ");
257 for (i = 0; i < NCC; i += 1)
258 printf_filtered ("0x%x ", state->termio.c_cc[i]);
259 printf_filtered ("\n");
260#endif
261
262#ifdef HAVE_SGTTY
263 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
264
265 printf_filtered ("tchars: ");
266 for (i = 0; i < (int)sizeof (struct tchars); i++)
267 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
268 printf_filtered ("\n");
269
270 printf_filtered ("ltchars: ");
271 for (i = 0; i < (int)sizeof (struct ltchars); i++)
272 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
273 printf_filtered ("\n");
274
275 printf_filtered ("lmode: 0x%x\n", state->lmode);
276#endif
277}
278
3ffbdf15
FF
279/* Wait for the output to drain away, as opposed to flushing (discarding) it */
280
281static int
282hardwire_drain_output (scb)
283 serial_t scb;
284{
285#ifdef HAVE_TERMIOS
286 return tcdrain (scb->fd);
287#endif
288
289#ifdef HAVE_TERMIO
290 return ioctl (scb->fd, TCSBRK, 1);
291#endif
292
293#ifdef HAVE_SGTTY
294 /* Get the current state and then restore it using TIOCSETP,
295 which should cause the output to drain and pending input
296 to be discarded. */
297 {
298 struct hardwire_ttystate state;
299 if (get_tty_state (scb, &state))
300 {
301 return (-1);
302 }
303 else
304 {
305 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
306 }
307 }
308#endif
309}
310
c2e247c4
JK
311static int
312hardwire_flush_output (scb)
313 serial_t scb;
314{
315#ifdef HAVE_TERMIOS
316 return tcflush (scb->fd, TCOFLUSH);
317#endif
318
319#ifdef HAVE_TERMIO
320 return ioctl (scb->fd, TCFLSH, 1);
321#endif
322
323#ifdef HAVE_SGTTY
324 /* This flushes both input and output, but we can't do better. */
325 return ioctl (scb->fd, TIOCFLUSH, 0);
326#endif
327}
328
704deef2
JK
329static int
330hardwire_flush_input (scb)
331 serial_t scb;
332{
dedcc91d
SC
333 scb->bufcnt = 0;
334 scb->bufp = scb->buf;
335
704deef2
JK
336#ifdef HAVE_TERMIOS
337 return tcflush (scb->fd, TCIFLUSH);
338#endif
339
340#ifdef HAVE_TERMIO
341 return ioctl (scb->fd, TCFLSH, 0);
342#endif
343
344#ifdef HAVE_SGTTY
345 /* This flushes both input and output, but we can't do better. */
346 return ioctl (scb->fd, TIOCFLUSH, 0);
347#endif
348}
349
350static int
351hardwire_send_break (scb)
352 serial_t scb;
353{
704deef2
JK
354#ifdef HAVE_TERMIOS
355 return tcsendbreak (scb->fd, 0);
356#endif
357
358#ifdef HAVE_TERMIO
359 return ioctl (scb->fd, TCSBRK, 0);
360#endif
361
362#ifdef HAVE_SGTTY
95a98b5e 363 {
0ac0a9f6 364 int status;
95a98b5e
JK
365 struct timeval timeout;
366
367 status = ioctl (scb->fd, TIOCSBRK, 0);
368
369 /* Can't use usleep; it doesn't exist in BSD 4.2. */
370 /* Note that if this select() is interrupted by a signal it will not wait
371 the full length of time. I think that is OK. */
372 timeout.tv_sec = 0;
373 timeout.tv_usec = 250000;
374 select (0, 0, 0, 0, &timeout);
375 status = ioctl (scb->fd, TIOCCBRK, 0);
376 return status;
377 }
704deef2
JK
378#endif
379}
380
38dc5e12
SG
381static void
382hardwire_raw(scb)
383 serial_t scb;
384{
385 struct hardwire_ttystate state;
386
387 if (get_tty_state(scb, &state))
199b2450 388 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
38dc5e12
SG
389
390#ifdef HAVE_TERMIOS
391 state.termios.c_iflag = 0;
392 state.termios.c_oflag = 0;
393 state.termios.c_lflag = 0;
394 state.termios.c_cflag &= ~(CSIZE|PARENB);
2e6784a8 395 state.termios.c_cflag |= CLOCAL | CS8;
38dc5e12
SG
396 state.termios.c_cc[VMIN] = 0;
397 state.termios.c_cc[VTIME] = 0;
398#endif
399
400#ifdef HAVE_TERMIO
401 state.termio.c_iflag = 0;
402 state.termio.c_oflag = 0;
403 state.termio.c_lflag = 0;
404 state.termio.c_cflag &= ~(CSIZE|PARENB);
2e6784a8 405 state.termio.c_cflag |= CLOCAL | CS8;
38dc5e12
SG
406 state.termio.c_cc[VMIN] = 0;
407 state.termio.c_cc[VTIME] = 0;
408#endif
409
410#ifdef HAVE_SGTTY
411 state.sgttyb.sg_flags |= RAW | ANYP;
412 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
4e772f44 413#endif
9e15da4a
SG
414
415 scb->current_timeout = 0;
38dc5e12
SG
416
417 if (set_tty_state (scb, &state))
199b2450 418 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
4e772f44
SG
419}
420
9e15da4a
SG
421/* Wait for input on scb, with timeout seconds. Returns 0 on success,
422 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
423
424 For termio{s}, we actually just setup VTIME if necessary, and let the
425 timeout occur in the read() in hardwire_read().
426 */
4e772f44
SG
427
428static int
9775789d 429wait_for(scb, timeout)
4e772f44
SG
430 serial_t scb;
431 int timeout;
432{
9db58d3a
JK
433 scb->timeout_remaining = 0;
434
9775789d 435#ifdef HAVE_SGTTY
9db58d3a
JK
436 {
437 struct timeval tv;
438 fd_set readfds;
eca29634 439
9db58d3a 440 FD_ZERO (&readfds);
eca29634 441
9db58d3a
JK
442 tv.tv_sec = timeout;
443 tv.tv_usec = 0;
eca29634 444
9db58d3a 445 FD_SET(scb->fd, &readfds);
eca29634 446
9db58d3a
JK
447 while (1)
448 {
449 int numfds;
450
451 if (timeout >= 0)
452 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
a037b21e 453 else
9db58d3a 454 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
a037b21e 455
9db58d3a
JK
456 if (numfds <= 0)
457 if (numfds == 0)
458 return SERIAL_TIMEOUT;
459 else if (errno == EINTR)
460 continue;
461 else
462 return SERIAL_ERROR; /* Got an error from select or poll */
9e15da4a 463
9db58d3a
JK
464 return 0;
465 }
466 }
9775789d 467#endif /* HAVE_SGTTY */
4e772f44 468
9775789d 469#if defined HAVE_TERMIO || defined HAVE_TERMIOS
9e15da4a
SG
470 if (timeout == scb->current_timeout)
471 return 0;
4e772f44 472
9db58d3a
JK
473 scb->current_timeout = timeout;
474
9e15da4a 475 {
38dc5e12 476 struct hardwire_ttystate state;
eca29634 477
38dc5e12 478 if (get_tty_state(scb, &state))
199b2450 479 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
eca29634 480
38dc5e12 481#ifdef HAVE_TERMIOS
864df7e6
JK
482 if (timeout < 0)
483 {
484 /* No timeout. */
485 state.termios.c_cc[VTIME] = 0;
486 state.termios.c_cc[VMIN] = 1;
487 }
488 else
489 {
490 state.termios.c_cc[VMIN] = 0;
491 state.termios.c_cc[VTIME] = timeout * 10;
492 if (state.termios.c_cc[VTIME] != timeout * 10)
493 {
9db58d3a
JK
494
495 /* If c_cc is an 8-bit signed character, we can't go
496 bigger than this. If it is always unsigned, we could use
497 25. */
498
499 scb->current_timeout = 12;
500 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
501 scb->timeout_remaining = timeout - scb->current_timeout;
864df7e6
JK
502 }
503 }
38dc5e12 504#endif
9775789d 505
9e15da4a 506#ifdef HAVE_TERMIO
864df7e6
JK
507 if (timeout < 0)
508 {
509 /* No timeout. */
510 state.termio.c_cc[VTIME] = 0;
511 state.termio.c_cc[VMIN] = 1;
512 }
513 else
514 {
515 state.termio.c_cc[VMIN] = 0;
516 state.termio.c_cc[VTIME] = timeout * 10;
517 if (state.termio.c_cc[VTIME] != timeout * 10)
518 {
9db58d3a
JK
519 /* If c_cc is an 8-bit signed character, we can't go
520 bigger than this. If it is always unsigned, we could use
521 25. */
522
523 scb->current_timeout = 12;
5c07a10b 524 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
9db58d3a 525 scb->timeout_remaining = timeout - scb->current_timeout;
864df7e6
JK
526 }
527 }
38dc5e12 528#endif
9e15da4a 529
38dc5e12 530 if (set_tty_state (scb, &state))
199b2450 531 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
9e15da4a 532
9e15da4a
SG
533 return 0;
534 }
535#endif /* HAVE_TERMIO || HAVE_TERMIOS */
9775789d
SG
536}
537
538/* Read a character with user-specified timeout. TIMEOUT is number of seconds
539 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
057c2f47
RP
540 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
541 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
9775789d
SG
542
543static int
544hardwire_readchar(scb, timeout)
545 serial_t scb;
546 int timeout;
547{
548 int status;
549
550 if (scb->bufcnt-- > 0)
551 return *scb->bufp++;
552
9db58d3a
JK
553 while (1)
554 {
555 status = wait_for (scb, timeout);
556
557 if (status < 0)
558 return status;
559
560 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
561
562 if (scb->bufcnt <= 0)
563 {
564 if (scb->bufcnt == 0)
565 {
566 /* Zero characters means timeout (it could also be EOF, but
567 we don't (yet at least) distinguish). */
568 if (scb->timeout_remaining > 0)
569 {
570 timeout = scb->timeout_remaining;
571 continue;
572 }
573 else
574 return SERIAL_TIMEOUT;
575 }
2e6784a8
SG
576 else if (errno == EINTR)
577 continue;
9db58d3a
JK
578 else
579 return SERIAL_ERROR; /* Got an error from read */
580 }
581
582 scb->bufcnt--;
583 scb->bufp = scb->buf;
584 return *scb->bufp++;
585 }
4e772f44
SG
586}
587
588#ifndef B19200
589#define B19200 EXTA
590#endif
591
592#ifndef B38400
593#define B38400 EXTB
594#endif
595
596/* Translate baud rates from integers to damn B_codes. Unix should
597 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
598
599static struct
600{
601 int rate;
602 int code;
603}
604baudtab[] =
605{
606 {50, B50},
607 {75, B75},
608 {110, B110},
609 {134, B134},
610 {150, B150},
611 {200, B200},
612 {300, B300},
613 {600, B600},
614 {1200, B1200},
615 {1800, B1800},
616 {2400, B2400},
617 {4800, B4800},
618 {9600, B9600},
619 {19200, B19200},
620 {38400, B38400},
621 {-1, -1},
622};
623
624static int
625rate_to_code(rate)
626 int rate;
627{
628 int i;
629
630 for (i = 0; baudtab[i].rate != -1; i++)
631 if (rate == baudtab[i].rate)
632 return baudtab[i].code;
633
634 return -1;
635}
636
637static int
638hardwire_setbaudrate(scb, rate)
639 serial_t scb;
640 int rate;
641{
38dc5e12 642 struct hardwire_ttystate state;
4e772f44 643
38dc5e12 644 if (get_tty_state(scb, &state))
4febd102 645 return -1;
4e772f44 646
38dc5e12
SG
647#ifdef HAVE_TERMIOS
648 cfsetospeed (&state.termios, rate_to_code (rate));
649 cfsetispeed (&state.termios, rate_to_code (rate));
4e772f44
SG
650#endif
651
652#ifdef HAVE_TERMIO
4e772f44
SG
653#ifndef CIBAUD
654#define CIBAUD CBAUD
655#endif
656
38dc5e12
SG
657 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
658 state.termio.c_cflag |= rate_to_code (rate);
4e772f44
SG
659#endif
660
661#ifdef HAVE_SGTTY
38dc5e12
SG
662 state.sgttyb.sg_ispeed = rate_to_code (rate);
663 state.sgttyb.sg_ospeed = rate_to_code (rate);
4e772f44 664#endif
38dc5e12
SG
665
666 return set_tty_state (scb, &state);
4e772f44
SG
667}
668
85c8b135
SG
669static int
670hardwire_setstopbits(scb, num)
671 serial_t scb;
672 int num;
673{
674 struct hardwire_ttystate state;
675 int newbit;
676
677 if (get_tty_state(scb, &state))
678 return -1;
679
680 switch (num)
681 {
682 case SERIAL_1_STOPBITS:
683 newbit = 0;
684 break;
685 case SERIAL_1_AND_A_HALF_STOPBITS:
686 case SERIAL_2_STOPBITS:
687 newbit = 1;
688 break;
689 default:
690 return 1;
691 }
692
693#ifdef HAVE_TERMIOS
694 if (!newbit)
695 state.termios.c_cflag &= ~CSTOPB;
696 else
697 state.termios.c_cflag |= CSTOPB; /* two bits */
698#endif
699
700#ifdef HAVE_TERMIO
701 if (!newbit)
702 state.termio.c_cflag &= ~CSTOPB;
703 else
704 state.termio.c_cflag |= CSTOPB; /* two bits */
705#endif
706
707#ifdef HAVE_SGTTY
708 return 0; /* sgtty doesn't support this */
709#endif
710
711 return set_tty_state (scb, &state);
712}
713
4e772f44
SG
714static int
715hardwire_write(scb, str, len)
716 serial_t scb;
717 const char *str;
718 int len;
719{
720 int cc;
721
722 while (len > 0)
723 {
724 cc = write(scb->fd, str, len);
725
726 if (cc < 0)
727 return 1;
728 len -= cc;
729 str += cc;
730 }
731 return 0;
732}
733
4e772f44
SG
734static void
735hardwire_close(scb)
736 serial_t scb;
737{
738 if (scb->fd < 0)
739 return;
740
4e772f44
SG
741 close(scb->fd);
742 scb->fd = -1;
743}
744
745static struct serial_ops hardwire_ops =
746{
747 "hardwire",
748 0,
749 hardwire_open,
750 hardwire_close,
751 hardwire_readchar,
752 hardwire_write,
c2e247c4 753 hardwire_flush_output,
704deef2
JK
754 hardwire_flush_input,
755 hardwire_send_break,
4e772f44 756 hardwire_raw,
38dc5e12
SG
757 hardwire_get_tty_state,
758 hardwire_set_tty_state,
c2e247c4
JK
759 hardwire_print_tty_state,
760 hardwire_noflush_set_tty_state,
38dc5e12 761 hardwire_setbaudrate,
85c8b135 762 hardwire_setstopbits,
3ffbdf15 763 hardwire_drain_output, /* wait for output to drain */
4e772f44
SG
764};
765
9775789d 766void
4e772f44
SG
767_initialize_ser_hardwire ()
768{
769 serial_add_interface (&hardwire_ops);
770}
This page took 0.346302 seconds and 4 git commands to generate.