| 1 | /* Serial interface for local (hardwired) serial ports on Un*x like systems |
| 2 | Copyright 1992, 1993, 1994, 1998 Free Software Foundation, Inc. |
| 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 | /* Open up a real live device for serial I/O */ |
| 87 | |
| 88 | static int |
| 89 | hardwire_open(scb, name) |
| 90 | serial_t scb; |
| 91 | const char *name; |
| 92 | { |
| 93 | scb->fd = open (name, O_RDWR); |
| 94 | if (scb->fd < 0) |
| 95 | return -1; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | get_tty_state(scb, state) |
| 102 | serial_t scb; |
| 103 | struct hardwire_ttystate *state; |
| 104 | { |
| 105 | #ifdef HAVE_TERMIOS |
| 106 | extern int errno; |
| 107 | |
| 108 | if (tcgetattr(scb->fd, &state->termios) < 0) |
| 109 | return -1; |
| 110 | |
| 111 | return 0; |
| 112 | #endif |
| 113 | |
| 114 | #ifdef HAVE_TERMIO |
| 115 | if (ioctl (scb->fd, TCGETA, &state->termio) < 0) |
| 116 | return -1; |
| 117 | return 0; |
| 118 | #endif |
| 119 | |
| 120 | #ifdef HAVE_SGTTY |
| 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 | |
| 130 | return 0; |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | static int |
| 135 | set_tty_state(scb, state) |
| 136 | serial_t scb; |
| 137 | struct hardwire_ttystate *state; |
| 138 | { |
| 139 | #ifdef HAVE_TERMIOS |
| 140 | if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0) |
| 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 | |
| 166 | static serial_ttystate |
| 167 | hardwire_get_tty_state(scb) |
| 168 | serial_t scb; |
| 169 | { |
| 170 | struct hardwire_ttystate *state; |
| 171 | |
| 172 | state = (struct hardwire_ttystate *)xmalloc(sizeof *state); |
| 173 | |
| 174 | if (get_tty_state(scb, state)) |
| 175 | return NULL; |
| 176 | |
| 177 | return (serial_ttystate)state; |
| 178 | } |
| 179 | |
| 180 | static int |
| 181 | hardwire_set_tty_state(scb, ttystate) |
| 182 | serial_t scb; |
| 183 | serial_ttystate ttystate; |
| 184 | { |
| 185 | struct hardwire_ttystate *state; |
| 186 | |
| 187 | state = (struct hardwire_ttystate *)ttystate; |
| 188 | |
| 189 | return set_tty_state(scb, state); |
| 190 | } |
| 191 | |
| 192 | static int |
| 193 | hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate) |
| 194 | serial_t scb; |
| 195 | serial_ttystate new_ttystate; |
| 196 | serial_ttystate old_ttystate; |
| 197 | { |
| 198 | struct hardwire_ttystate new_state; |
| 199 | #ifdef HAVE_SGTTY |
| 200 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate; |
| 201 | #endif |
| 202 | |
| 203 | new_state = *(struct hardwire_ttystate *)new_ttystate; |
| 204 | |
| 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. */ |
| 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 | |
| 226 | static void |
| 227 | hardwire_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 | |
| 234 | #ifdef HAVE_TERMIOS |
| 235 | printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n", |
| 236 | state->termios.c_iflag, state->termios.c_oflag); |
| 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 |
| 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 | |
| 279 | /* Wait for the output to drain away, as opposed to flushing (discarding) it */ |
| 280 | |
| 281 | static int |
| 282 | hardwire_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 | |
| 311 | static int |
| 312 | hardwire_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 | |
| 329 | static int |
| 330 | hardwire_flush_input (scb) |
| 331 | serial_t scb; |
| 332 | { |
| 333 | scb->bufcnt = 0; |
| 334 | scb->bufp = scb->buf; |
| 335 | |
| 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 | |
| 350 | static int |
| 351 | hardwire_send_break (scb) |
| 352 | serial_t scb; |
| 353 | { |
| 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 |
| 363 | { |
| 364 | int status; |
| 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 | } |
| 378 | #endif |
| 379 | } |
| 380 | |
| 381 | static void |
| 382 | hardwire_raw(scb) |
| 383 | serial_t scb; |
| 384 | { |
| 385 | struct hardwire_ttystate state; |
| 386 | |
| 387 | if (get_tty_state(scb, &state)) |
| 388 | fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno)); |
| 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); |
| 395 | state.termios.c_cflag |= CLOCAL | CS8; |
| 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); |
| 405 | state.termio.c_cflag |= CLOCAL | CS8; |
| 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); |
| 413 | #endif |
| 414 | |
| 415 | scb->current_timeout = 0; |
| 416 | |
| 417 | if (set_tty_state (scb, &state)) |
| 418 | fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno)); |
| 419 | } |
| 420 | |
| 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 | */ |
| 427 | |
| 428 | static int |
| 429 | wait_for(scb, timeout) |
| 430 | serial_t scb; |
| 431 | int timeout; |
| 432 | { |
| 433 | scb->timeout_remaining = 0; |
| 434 | |
| 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 | |
| 543 | static int |
| 544 | hardwire_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 | |
| 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 | } |
| 576 | else if (errno == EINTR) |
| 577 | continue; |
| 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 | } |
| 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 | |
| 599 | static struct |
| 600 | { |
| 601 | int rate; |
| 602 | int code; |
| 603 | } |
| 604 | baudtab[] = |
| 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 | #ifdef B57600 |
| 622 | {57600, B57600}, |
| 623 | #endif |
| 624 | #ifdef B115200 |
| 625 | {115200, B115200}, |
| 626 | #endif |
| 627 | #ifdef B230400 |
| 628 | {230400, B230400}, |
| 629 | #endif |
| 630 | #ifdef B460800 |
| 631 | {460800, B460800}, |
| 632 | #endif |
| 633 | {-1, -1}, |
| 634 | }; |
| 635 | |
| 636 | static int |
| 637 | rate_to_code(rate) |
| 638 | int rate; |
| 639 | { |
| 640 | int i; |
| 641 | |
| 642 | for (i = 0; baudtab[i].rate != -1; i++) |
| 643 | if (rate == baudtab[i].rate) |
| 644 | return baudtab[i].code; |
| 645 | |
| 646 | return -1; |
| 647 | } |
| 648 | |
| 649 | static int |
| 650 | hardwire_setbaudrate(scb, rate) |
| 651 | serial_t scb; |
| 652 | int rate; |
| 653 | { |
| 654 | struct hardwire_ttystate state; |
| 655 | |
| 656 | if (get_tty_state(scb, &state)) |
| 657 | return -1; |
| 658 | |
| 659 | #ifdef HAVE_TERMIOS |
| 660 | cfsetospeed (&state.termios, rate_to_code (rate)); |
| 661 | cfsetispeed (&state.termios, rate_to_code (rate)); |
| 662 | #endif |
| 663 | |
| 664 | #ifdef HAVE_TERMIO |
| 665 | #ifndef CIBAUD |
| 666 | #define CIBAUD CBAUD |
| 667 | #endif |
| 668 | |
| 669 | state.termio.c_cflag &= ~(CBAUD | CIBAUD); |
| 670 | state.termio.c_cflag |= rate_to_code (rate); |
| 671 | #endif |
| 672 | |
| 673 | #ifdef HAVE_SGTTY |
| 674 | state.sgttyb.sg_ispeed = rate_to_code (rate); |
| 675 | state.sgttyb.sg_ospeed = rate_to_code (rate); |
| 676 | #endif |
| 677 | |
| 678 | return set_tty_state (scb, &state); |
| 679 | } |
| 680 | |
| 681 | static int |
| 682 | hardwire_setstopbits(scb, num) |
| 683 | serial_t scb; |
| 684 | int num; |
| 685 | { |
| 686 | struct hardwire_ttystate state; |
| 687 | int newbit; |
| 688 | |
| 689 | if (get_tty_state(scb, &state)) |
| 690 | return -1; |
| 691 | |
| 692 | switch (num) |
| 693 | { |
| 694 | case SERIAL_1_STOPBITS: |
| 695 | newbit = 0; |
| 696 | break; |
| 697 | case SERIAL_1_AND_A_HALF_STOPBITS: |
| 698 | case SERIAL_2_STOPBITS: |
| 699 | newbit = 1; |
| 700 | break; |
| 701 | default: |
| 702 | return 1; |
| 703 | } |
| 704 | |
| 705 | #ifdef HAVE_TERMIOS |
| 706 | if (!newbit) |
| 707 | state.termios.c_cflag &= ~CSTOPB; |
| 708 | else |
| 709 | state.termios.c_cflag |= CSTOPB; /* two bits */ |
| 710 | #endif |
| 711 | |
| 712 | #ifdef HAVE_TERMIO |
| 713 | if (!newbit) |
| 714 | state.termio.c_cflag &= ~CSTOPB; |
| 715 | else |
| 716 | state.termio.c_cflag |= CSTOPB; /* two bits */ |
| 717 | #endif |
| 718 | |
| 719 | #ifdef HAVE_SGTTY |
| 720 | return 0; /* sgtty doesn't support this */ |
| 721 | #endif |
| 722 | |
| 723 | return set_tty_state (scb, &state); |
| 724 | } |
| 725 | |
| 726 | static int |
| 727 | hardwire_write(scb, str, len) |
| 728 | serial_t scb; |
| 729 | const char *str; |
| 730 | int len; |
| 731 | { |
| 732 | int cc; |
| 733 | |
| 734 | while (len > 0) |
| 735 | { |
| 736 | cc = write(scb->fd, str, len); |
| 737 | |
| 738 | if (cc < 0) |
| 739 | return 1; |
| 740 | len -= cc; |
| 741 | str += cc; |
| 742 | } |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static void |
| 747 | hardwire_close(scb) |
| 748 | serial_t scb; |
| 749 | { |
| 750 | if (scb->fd < 0) |
| 751 | return; |
| 752 | |
| 753 | close(scb->fd); |
| 754 | scb->fd = -1; |
| 755 | } |
| 756 | |
| 757 | static struct serial_ops hardwire_ops = |
| 758 | { |
| 759 | "hardwire", |
| 760 | 0, |
| 761 | hardwire_open, |
| 762 | hardwire_close, |
| 763 | hardwire_readchar, |
| 764 | hardwire_write, |
| 765 | hardwire_flush_output, |
| 766 | hardwire_flush_input, |
| 767 | hardwire_send_break, |
| 768 | hardwire_raw, |
| 769 | hardwire_get_tty_state, |
| 770 | hardwire_set_tty_state, |
| 771 | hardwire_print_tty_state, |
| 772 | hardwire_noflush_set_tty_state, |
| 773 | hardwire_setbaudrate, |
| 774 | hardwire_setstopbits, |
| 775 | hardwire_drain_output, /* wait for output to drain */ |
| 776 | }; |
| 777 | |
| 778 | void |
| 779 | _initialize_ser_hardwire () |
| 780 | { |
| 781 | serial_add_interface (&hardwire_ops); |
| 782 | } |