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