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