1/* Serial interface for local (hardwired) serial ports on Un*x like systems 2 3 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 4 2003, 2004 Free Software Foundation, Inc. 5 6 This file is part of GDB. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place - Suite 330, 21 Boston, MA 02111-1307, USA. */ 22 23#include "defs.h" 24#include "serial.h" 25#include "ser-unix.h" 26 27#include <fcntl.h> 28#include <sys/types.h> 29#include "terminal.h" 30#include <sys/socket.h> 31#include <sys/time.h> 32 33#include "gdb_string.h" 34#include "event-loop.h" 35 36#ifdef HAVE_TERMIOS 37 38struct hardwire_ttystate 39 { 40 struct termios termios; 41 }; 42#endif /* termios */ 43 44#ifdef HAVE_TERMIO 45 46/* It is believed that all systems which have added job control to SVR3 47 (e.g. sco) have also added termios. Even if not, trying to figure out 48 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty 49 bewildering. So we don't attempt it. */ 50 51struct hardwire_ttystate 52 { 53 struct termio termio; 54 }; 55#endif /* termio */ 56 57#ifdef HAVE_SGTTY 58struct hardwire_ttystate 59 { 60 struct sgttyb sgttyb; 61 struct tchars tc; 62 struct ltchars ltc; 63 /* Line discipline flags. */ 64 int lmode; 65 }; 66#endif /* sgtty */ 67 68static int hardwire_open (struct serial *scb, const char *name); 69static void hardwire_raw (struct serial *scb); 70static int wait_for (struct serial *scb, int timeout); 71static int hardwire_readchar (struct serial *scb, int timeout); 72static int do_hardwire_readchar (struct serial *scb, int timeout); 73static int generic_readchar (struct serial *scb, int timeout, 74 int (*do_readchar) (struct serial *scb, 75 int timeout)); 76static int rate_to_code (int rate); 77static int hardwire_setbaudrate (struct serial *scb, int rate); 78static void hardwire_close (struct serial *scb); 79static int get_tty_state (struct serial *scb, 80 struct hardwire_ttystate * state); 81static int set_tty_state (struct serial *scb, 82 struct hardwire_ttystate * state); 83static serial_ttystate hardwire_get_tty_state (struct serial *scb); 84static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state); 85static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate, 86 serial_ttystate); 87static void hardwire_print_tty_state (struct serial *, serial_ttystate, 88 struct ui_file *); 89static int hardwire_drain_output (struct serial *); 90static int hardwire_flush_output (struct serial *); 91static int hardwire_flush_input (struct serial *); 92static int hardwire_send_break (struct serial *); 93static int hardwire_setstopbits (struct serial *, int); 94 95static int do_unix_readchar (struct serial *scb, int timeout); 96static timer_handler_func push_event; 97static handler_func fd_event; 98static void reschedule (struct serial *scb); 99 100void _initialize_ser_hardwire (void); 101 102extern int (*ui_loop_hook) (int); 103 104/* Open up a real live device for serial I/O */ 105 106static int 107hardwire_open (struct serial *scb, const char *name) 108{ 109 scb->fd = open (name, O_RDWR); 110 if (scb->fd < 0) 111 return -1; 112 113 return 0; 114} 115 116static int 117get_tty_state (struct serial *scb, struct hardwire_ttystate *state) 118{ 119#ifdef HAVE_TERMIOS 120 if (tcgetattr (scb->fd, &state->termios) < 0) 121 return -1; 122 123 return 0; 124#endif 125 126#ifdef HAVE_TERMIO 127 if (ioctl (scb->fd, TCGETA, &state->termio) < 0) 128 return -1; 129 return 0; 130#endif 131 132#ifdef HAVE_SGTTY 133 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0) 134 return -1; 135 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0) 136 return -1; 137 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0) 138 return -1; 139 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0) 140 return -1; 141 142 return 0; 143#endif 144} 145 146static int 147set_tty_state (struct serial *scb, struct hardwire_ttystate *state) 148{ 149#ifdef HAVE_TERMIOS 150 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0) 151 return -1; 152 153 return 0; 154#endif 155 156#ifdef HAVE_TERMIO 157 if (ioctl (scb->fd, TCSETA, &state->termio) < 0) 158 return -1; 159 return 0; 160#endif 161 162#ifdef HAVE_SGTTY 163 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0) 164 return -1; 165 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0) 166 return -1; 167 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0) 168 return -1; 169 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0) 170 return -1; 171 172 return 0; 173#endif 174} 175 176static serial_ttystate 177hardwire_get_tty_state (struct serial *scb) 178{ 179 struct hardwire_ttystate *state; 180 181 state = (struct hardwire_ttystate *) xmalloc (sizeof *state); 182 183 if (get_tty_state (scb, state)) 184 return NULL; 185 186 return (serial_ttystate) state; 187} 188 189static int 190hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate) 191{ 192 struct hardwire_ttystate *state; 193 194 state = (struct hardwire_ttystate *) ttystate; 195 196 return set_tty_state (scb, state); 197} 198 199static int 200hardwire_noflush_set_tty_state (struct serial *scb, 201 serial_ttystate new_ttystate, 202 serial_ttystate old_ttystate) 203{ 204 struct hardwire_ttystate new_state; 205#ifdef HAVE_SGTTY 206 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate; 207#endif 208 209 new_state = *(struct hardwire_ttystate *) new_ttystate; 210 211 /* Don't change in or out of raw mode; we don't want to flush input. 212 termio and termios have no such restriction; for them flushing input 213 is separate from setting the attributes. */ 214 215#ifdef HAVE_SGTTY 216 if (state->sgttyb.sg_flags & RAW) 217 new_state.sgttyb.sg_flags |= RAW; 218 else 219 new_state.sgttyb.sg_flags &= ~RAW; 220 221 /* I'm not sure whether this is necessary; the manpage just mentions 222 RAW not CBREAK. */ 223 if (state->sgttyb.sg_flags & CBREAK) 224 new_state.sgttyb.sg_flags |= CBREAK; 225 else 226 new_state.sgttyb.sg_flags &= ~CBREAK; 227#endif 228 229 return set_tty_state (scb, &new_state); 230} 231 232static void 233hardwire_print_tty_state (struct serial *scb, 234 serial_ttystate ttystate, 235 struct ui_file *stream) 236{ 237 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate; 238 int i; 239 240#ifdef HAVE_TERMIOS 241 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", 242 (int) state->termios.c_iflag, 243 (int) state->termios.c_oflag); 244 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n", 245 (int) state->termios.c_cflag, 246 (int) state->termios.c_lflag); 247#if 0 248 /* This not in POSIX, and is not really documented by those systems 249 which have it (at least not Sun). */ 250 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line); 251#endif 252 fprintf_filtered (stream, "c_cc: "); 253 for (i = 0; i < NCCS; i += 1) 254 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]); 255 fprintf_filtered (stream, "\n"); 256#endif 257 258#ifdef HAVE_TERMIO 259 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", 260 state->termio.c_iflag, state->termio.c_oflag); 261 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", 262 state->termio.c_cflag, state->termio.c_lflag, 263 state->termio.c_line); 264 fprintf_filtered (stream, "c_cc: "); 265 for (i = 0; i < NCC; i += 1) 266 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]); 267 fprintf_filtered (stream, "\n"); 268#endif 269 270#ifdef HAVE_SGTTY 271 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n", 272 state->sgttyb.sg_flags); 273 274 fprintf_filtered (stream, "tchars: "); 275 for (i = 0; i < (int) sizeof (struct tchars); i++) 276 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]); 277 fprintf_filtered (stream, "\n"); 278 279 fprintf_filtered (stream, "ltchars: "); 280 for (i = 0; i < (int) sizeof (struct ltchars); i++) 281 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]); 282 fprintf_filtered (stream, "\n"); 283 284 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode); 285#endif 286} 287 288/* Wait for the output to drain away, as opposed to flushing (discarding) it */ 289 290static int 291hardwire_drain_output (struct serial *scb) 292{ 293#ifdef HAVE_TERMIOS 294 return tcdrain (scb->fd); 295#endif 296 297#ifdef HAVE_TERMIO 298 return ioctl (scb->fd, TCSBRK, 1); 299#endif 300 301#ifdef HAVE_SGTTY 302 /* Get the current state and then restore it using TIOCSETP, 303 which should cause the output to drain and pending input 304 to be discarded. */ 305 { 306 struct hardwire_ttystate state; 307 if (get_tty_state (scb, &state)) 308 { 309 return (-1); 310 } 311 else 312 { 313 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb)); 314 } 315 } 316#endif 317} 318 319static int 320hardwire_flush_output (struct serial *scb) 321{ 322#ifdef HAVE_TERMIOS 323 return tcflush (scb->fd, TCOFLUSH); 324#endif 325 326#ifdef HAVE_TERMIO 327 return ioctl (scb->fd, TCFLSH, 1); 328#endif 329 330#ifdef HAVE_SGTTY 331 /* This flushes both input and output, but we can't do better. */ 332 return ioctl (scb->fd, TIOCFLUSH, 0); 333#endif 334} 335 336static int 337hardwire_flush_input (struct serial *scb) 338{ 339 ser_unix_flush_input (scb); 340 341#ifdef HAVE_TERMIOS 342 return tcflush (scb->fd, TCIFLUSH); 343#endif 344 345#ifdef HAVE_TERMIO 346 return ioctl (scb->fd, TCFLSH, 0); 347#endif 348 349#ifdef HAVE_SGTTY 350 /* This flushes both input and output, but we can't do better. */ 351 return ioctl (scb->fd, TIOCFLUSH, 0); 352#endif 353} 354 355static int 356hardwire_send_break (struct serial *scb) 357{ 358#ifdef HAVE_TERMIOS 359 return tcsendbreak (scb->fd, 0); 360#endif 361 362#ifdef HAVE_TERMIO 363 return ioctl (scb->fd, TCSBRK, 0); 364#endif 365 366#ifdef HAVE_SGTTY 367 { 368 int status; 369 struct timeval timeout; 370 371 status = ioctl (scb->fd, TIOCSBRK, 0); 372 373 /* Can't use usleep; it doesn't exist in BSD 4.2. */ 374 /* Note that if this select() is interrupted by a signal it will not wait 375 the full length of time. I think that is OK. */ 376 timeout.tv_sec = 0; 377 timeout.tv_usec = 250000; 378 select (0, 0, 0, 0, &timeout); 379 status = ioctl (scb->fd, TIOCCBRK, 0); 380 return status; 381 } 382#endif 383} 384 385static void 386hardwire_raw (struct serial *scb) 387{ 388 struct hardwire_ttystate state; 389 390 if (get_tty_state (scb, &state)) 391 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno)); 392 393#ifdef HAVE_TERMIOS 394 state.termios.c_iflag = 0; 395 state.termios.c_oflag = 0; 396 state.termios.c_lflag = 0; 397 state.termios.c_cflag &= ~(CSIZE | PARENB); 398 state.termios.c_cflag |= CLOCAL | CS8; 399 state.termios.c_cc[VMIN] = 0; 400 state.termios.c_cc[VTIME] = 0; 401#endif 402 403#ifdef HAVE_TERMIO 404 state.termio.c_iflag = 0; 405 state.termio.c_oflag = 0; 406 state.termio.c_lflag = 0; 407 state.termio.c_cflag &= ~(CSIZE | PARENB); 408 state.termio.c_cflag |= CLOCAL | CS8; 409 state.termio.c_cc[VMIN] = 0; 410 state.termio.c_cc[VTIME] = 0; 411#endif 412 413#ifdef HAVE_SGTTY 414 state.sgttyb.sg_flags |= RAW | ANYP; 415 state.sgttyb.sg_flags &= ~(CBREAK | ECHO); 416#endif 417 418 scb->current_timeout = 0; 419 420 if (set_tty_state (scb, &state)) 421 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno)); 422} 423 424/* Wait for input on scb, with timeout seconds. Returns 0 on success, 425 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. 426 427 For termio{s}, we actually just setup VTIME if necessary, and let the 428 timeout occur in the read() in hardwire_read(). 429 */ 430 431/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent 432 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been 433 flushed. . */ 434 435/* NOTE: cagney/1999-09-30: Much of the code below is dead. The only 436 possible values of the TIMEOUT parameter are ONE and ZERO. 437 Consequently all the code that tries to handle the possability of 438 an overflowed timer is unnecessary. */ 439 440static int 441wait_for (struct serial *scb, int timeout) 442{ 443#ifdef HAVE_SGTTY 444 while (1) 445 { 446 struct timeval tv; 447 fd_set readfds; 448 int numfds; 449 450 /* NOTE: Some OS's can scramble the READFDS when the select() 451 call fails (ex the kernel with Red Hat 5.2). Initialize all 452 arguments before each call. */ 453 454 tv.tv_sec = timeout; 455 tv.tv_usec = 0; 456 457 FD_ZERO (&readfds); 458 FD_SET (scb->fd, &readfds); 459 460 if (timeout >= 0) 461 numfds = select (scb->fd + 1, &readfds, 0, 0, &tv); 462 else 463 numfds = select (scb->fd + 1, &readfds, 0, 0, 0); 464 465 if (numfds <= 0) 466 if (numfds == 0) 467 return SERIAL_TIMEOUT; 468 else if (errno == EINTR) 469 continue; 470 else 471 return SERIAL_ERROR; /* Got an error from select or poll */ 472 473 return 0; 474 } 475#endif /* HAVE_SGTTY */ 476 477#if defined HAVE_TERMIO || defined HAVE_TERMIOS 478 if (timeout == scb->current_timeout) 479 return 0; 480 481 scb->current_timeout = timeout; 482 483 { 484 struct hardwire_ttystate state; 485 486 if (get_tty_state (scb, &state)) 487 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno)); 488 489#ifdef HAVE_TERMIOS 490 if (timeout < 0) 491 { 492 /* No timeout. */ 493 state.termios.c_cc[VTIME] = 0; 494 state.termios.c_cc[VMIN] = 1; 495 } 496 else 497 { 498 state.termios.c_cc[VMIN] = 0; 499 state.termios.c_cc[VTIME] = timeout * 10; 500 if (state.termios.c_cc[VTIME] != timeout * 10) 501 { 502 503 /* If c_cc is an 8-bit signed character, we can't go 504 bigger than this. If it is always unsigned, we could use 505 25. */ 506 507 scb->current_timeout = 12; 508 state.termios.c_cc[VTIME] = scb->current_timeout * 10; 509 scb->timeout_remaining = timeout - scb->current_timeout; 510 } 511 } 512#endif 513 514#ifdef HAVE_TERMIO 515 if (timeout < 0) 516 { 517 /* No timeout. */ 518 state.termio.c_cc[VTIME] = 0; 519 state.termio.c_cc[VMIN] = 1; 520 } 521 else 522 { 523 state.termio.c_cc[VMIN] = 0; 524 state.termio.c_cc[VTIME] = timeout * 10; 525 if (state.termio.c_cc[VTIME] != timeout * 10) 526 { 527 /* If c_cc is an 8-bit signed character, we can't go 528 bigger than this. If it is always unsigned, we could use 529 25. */ 530 531 scb->current_timeout = 12; 532 state.termio.c_cc[VTIME] = scb->current_timeout * 10; 533 scb->timeout_remaining = timeout - scb->current_timeout; 534 } 535 } 536#endif 537 538 if (set_tty_state (scb, &state)) 539 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno)); 540 541 return 0; 542 } 543#endif /* HAVE_TERMIO || HAVE_TERMIOS */ 544} 545 546/* Read a character with user-specified timeout. TIMEOUT is number of seconds 547 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns 548 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line 549 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */ 550 551/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent 552 ser_unix*() until the old TERMIOS/SGTTY/... timer code has been 553 flushed. */ 554 555/* NOTE: cagney/1999-09-16: This function is not identical to 556 ser_unix_readchar() as part of replacing it with ser_unix*() 557 merging will be required - this code handles the case where read() 558 times out due to no data while ser_unix_readchar() doesn't expect 559 that. */ 560 561static int 562do_hardwire_readchar (struct serial *scb, int timeout) 563{ 564 int status, delta; 565 int detach = 0; 566 567 if (timeout > 0) 568 timeout++; 569 570 /* We have to be able to keep the GUI alive here, so we break the original 571 timeout into steps of 1 second, running the "keep the GUI alive" hook 572 each time through the loop. 573 Also, timeout = 0 means to poll, so we just set the delta to 0, so we 574 will only go through the loop once. */ 575 576 delta = (timeout == 0 ? 0 : 1); 577 while (1) 578 { 579 580 /* N.B. The UI may destroy our world (for instance by calling 581 remote_stop,) in which case we want to get out of here as 582 quickly as possible. It is not safe to touch scb, since 583 someone else might have freed it. The ui_loop_hook signals that 584 we should exit by returning 1. */ 585 586 if (ui_loop_hook) 587 detach = ui_loop_hook (0); 588 589 if (detach) 590 return SERIAL_TIMEOUT; 591 592 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta); 593 status = wait_for (scb, delta); 594 595 if (status < 0) 596 return status; 597 598 status = read (scb->fd, scb->buf, BUFSIZ); 599 600 if (status <= 0) 601 { 602 if (status == 0) 603 { 604 /* Zero characters means timeout (it could also be EOF, but 605 we don't (yet at least) distinguish). */ 606 if (scb->timeout_remaining > 0) 607 { 608 timeout = scb->timeout_remaining; 609 continue; 610 } 611 else if (scb->timeout_remaining < 0) 612 continue; 613 else 614 return SERIAL_TIMEOUT; 615 } 616 else if (errno == EINTR) 617 continue; 618 else 619 return SERIAL_ERROR; /* Got an error from read */ 620 } 621 622 scb->bufcnt = status; 623 scb->bufcnt--; 624 scb->bufp = scb->buf; 625 return *scb->bufp++; 626 } 627} 628 629static int 630hardwire_readchar (struct serial *scb, int timeout) 631{ 632 return generic_readchar (scb, timeout, do_hardwire_readchar); 633} 634 635 636#ifndef B19200 637#define B19200 EXTA 638#endif 639 640#ifndef B38400 641#define B38400 EXTB 642#endif 643 644/* Translate baud rates from integers to damn B_codes. Unix should 645 have outgrown this crap years ago, but even POSIX wouldn't buck it. */ 646 647static struct 648{ 649 int rate; 650 int code; 651} 652baudtab[] = 653{ 654 { 655 50, B50 656 } 657 , 658 { 659 75, B75 660 } 661 , 662 { 663 110, B110 664 } 665 , 666 { 667 134, B134 668 } 669 , 670 { 671 150, B150 672 } 673 , 674 { 675 200, B200 676 } 677 , 678 { 679 300, B300 680 } 681 , 682 { 683 600, B600 684 } 685 , 686 { 687 1200, B1200 688 } 689 , 690 { 691 1800, B1800 692 } 693 , 694 { 695 2400, B2400 696 } 697 , 698 { 699 4800, B4800 700 } 701 , 702 { 703 9600, B9600 704 } 705 , 706 { 707 19200, B19200 708 } 709 , 710 { 711 38400, B38400 712 } 713 , 714#ifdef B57600 715 { 716 57600, B57600 717 } 718 , 719#endif 720#ifdef B115200 721 { 722 115200, B115200 723 } 724 , 725#endif 726#ifdef B230400 727 { 728 230400, B230400 729 } 730 , 731#endif 732#ifdef B460800 733 { 734 460800, B460800 735 } 736 , 737#endif 738 { 739 -1, -1 740 } 741 , 742}; 743 744static int 745rate_to_code (int rate) 746{ 747 int i; 748 749 for (i = 0; baudtab[i].rate != -1; i++) 750 { 751 /* test for perfect macth. */ 752 if (rate == baudtab[i].rate) 753 return baudtab[i].code; 754 else 755 { 756 /* check if it is in between valid values. */ 757 if (rate < baudtab[i].rate) 758 { 759 if (i) 760 { 761 warning ("Invalid baud rate %d. Closest values are %d and %d.", 762 rate, baudtab[i - 1].rate, baudtab[i].rate); 763 } 764 else 765 { 766 warning ("Invalid baud rate %d. Minimum value is %d.", 767 rate, baudtab[0].rate); 768 } 769 return -1; 770 } 771 } 772 } 773 774 /* The requested speed was too large. */ 775 warning ("Invalid baud rate %d. Maximum value is %d.", 776 rate, baudtab[i - 1].rate); 777 return -1; 778} 779 780static int 781hardwire_setbaudrate (struct serial *scb, int rate) 782{ 783 struct hardwire_ttystate state; 784 int baud_code = rate_to_code (rate); 785 786 if (baud_code < 0) 787 { 788 /* The baud rate was not valid. 789 A warning has already been issued. */ 790 errno = EINVAL; 791 return -1; 792 } 793 794 if (get_tty_state (scb, &state)) 795 return -1; 796 797#ifdef HAVE_TERMIOS 798 cfsetospeed (&state.termios, baud_code); 799 cfsetispeed (&state.termios, baud_code); 800#endif 801 802#ifdef HAVE_TERMIO 803#ifndef CIBAUD 804#define CIBAUD CBAUD 805#endif 806 807 state.termio.c_cflag &= ~(CBAUD | CIBAUD); 808 state.termio.c_cflag |= baud_code; 809#endif 810 811#ifdef HAVE_SGTTY 812 state.sgttyb.sg_ispeed = baud_code; 813 state.sgttyb.sg_ospeed = baud_code; 814#endif 815 816 return set_tty_state (scb, &state); 817} 818 819static int 820hardwire_setstopbits (struct serial *scb, int num) 821{ 822 struct hardwire_ttystate state; 823 int newbit; 824 825 if (get_tty_state (scb, &state)) 826 return -1; 827 828 switch (num) 829 { 830 case SERIAL_1_STOPBITS: 831 newbit = 0; 832 break; 833 case SERIAL_1_AND_A_HALF_STOPBITS: 834 case SERIAL_2_STOPBITS: 835 newbit = 1; 836 break; 837 default: 838 return 1; 839 } 840 841#ifdef HAVE_TERMIOS 842 if (!newbit) 843 state.termios.c_cflag &= ~CSTOPB; 844 else 845 state.termios.c_cflag |= CSTOPB; /* two bits */ 846#endif 847 848#ifdef HAVE_TERMIO 849 if (!newbit) 850 state.termio.c_cflag &= ~CSTOPB; 851 else 852 state.termio.c_cflag |= CSTOPB; /* two bits */ 853#endif 854 855#ifdef HAVE_SGTTY 856 return 0; /* sgtty doesn't support this */ 857#endif 858 859 return set_tty_state (scb, &state); 860} 861 862static void 863hardwire_close (struct serial *scb) 864{ 865 if (scb->fd < 0) 866 return; 867 868 close (scb->fd); 869 scb->fd = -1; 870} 871 872 873/* Generic operations used by all UNIX/FD based serial interfaces. */ 874 875serial_ttystate 876ser_unix_nop_get_tty_state (struct serial *scb) 877{ 878 /* allocate a dummy */ 879 return (serial_ttystate) XMALLOC (int); 880} 881 882int 883ser_unix_nop_set_tty_state (struct serial *scb, serial_ttystate ttystate) 884{ 885 return 0; 886} 887 888void 889ser_unix_nop_raw (struct serial *scb) 890{ 891 return; /* Always in raw mode */ 892} 893 894/* Wait for input on scb, with timeout seconds. Returns 0 on success, 895 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */ 896 897int 898ser_unix_wait_for (struct serial *scb, int timeout) 899{ 900 while (1) 901 { 902 int numfds; 903 struct timeval tv; 904 fd_set readfds, exceptfds; 905 906 /* NOTE: Some OS's can scramble the READFDS when the select() 907 call fails (ex the kernel with Red Hat 5.2). Initialize all 908 arguments before each call. */ 909 910 tv.tv_sec = timeout; 911 tv.tv_usec = 0; 912 913 FD_ZERO (&readfds); 914 FD_ZERO (&exceptfds); 915 FD_SET (scb->fd, &readfds); 916 FD_SET (scb->fd, &exceptfds); 917 918 if (timeout >= 0) 919 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv); 920 else 921 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0); 922 923 if (numfds <= 0) 924 { 925 if (numfds == 0) 926 return SERIAL_TIMEOUT; 927 else if (errno == EINTR) 928 continue; 929 else 930 return SERIAL_ERROR; /* Got an error from select or poll */ 931 } 932 933 return 0; 934 } 935} 936 937/* Read a character with user-specified timeout. TIMEOUT is number of seconds 938 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns 939 char if successful. Returns -2 if timeout expired, EOF if line dropped 940 dead, or -3 for any other error (see errno in that case). */ 941 942static int 943do_unix_readchar (struct serial *scb, int timeout) 944{ 945 int status; 946 int delta; 947 948 /* We have to be able to keep the GUI alive here, so we break the original 949 timeout into steps of 1 second, running the "keep the GUI alive" hook 950 each time through the loop. 951 952 Also, timeout = 0 means to poll, so we just set the delta to 0, so we 953 will only go through the loop once. */ 954 955 delta = (timeout == 0 ? 0 : 1); 956 while (1) 957 { 958 959 /* N.B. The UI may destroy our world (for instance by calling 960 remote_stop,) in which case we want to get out of here as 961 quickly as possible. It is not safe to touch scb, since 962 someone else might have freed it. The ui_loop_hook signals that 963 we should exit by returning 1. */ 964 965 if (ui_loop_hook) 966 { 967 if (ui_loop_hook (0)) 968 return SERIAL_TIMEOUT; 969 } 970 971 status = ser_unix_wait_for (scb, delta); 972 if (timeout > 0) 973 timeout -= delta; 974 975 /* If we got a character or an error back from wait_for, then we can 976 break from the loop before the timeout is completed. */ 977 978 if (status != SERIAL_TIMEOUT) 979 { 980 break; 981 } 982 983 /* If we have exhausted the original timeout, then generate 984 a SERIAL_TIMEOUT, and pass it out of the loop. */ 985 986 else if (timeout == 0) 987 { 988 status = SERIAL_TIMEOUT; 989 break; 990 } 991 } 992 993 if (status < 0) 994 return status; 995 996 while (1) 997 { 998 status = read (scb->fd, scb->buf, BUFSIZ); 999 if (status != -1 || errno != EINTR) 1000 break; 1001 } 1002 1003 if (status <= 0) 1004 { 1005 if (status == 0) 1006 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to 1007 distinguish between EOF & timeouts 1008 someday] */ 1009 else 1010 return SERIAL_ERROR; /* Got an error from read */ 1011 } 1012 1013 scb->bufcnt = status; 1014 scb->bufcnt--; 1015 scb->bufp = scb->buf; 1016 return *scb->bufp++; 1017} 1018 1019/* Perform operations common to both old and new readchar. */ 1020 1021/* Return the next character from the input FIFO. If the FIFO is 1022 empty, call the SERIAL specific routine to try and read in more 1023 characters. 1024 1025 Initially data from the input FIFO is returned (fd_event() 1026 pre-reads the input into that FIFO. Once that has been emptied, 1027 further data is obtained by polling the input FD using the device 1028 specific readchar() function. Note: reschedule() is called after 1029 every read. This is because there is no guarentee that the lower 1030 level fd_event() poll_event() code (which also calls reschedule()) 1031 will be called. */ 1032 1033static int 1034generic_readchar (struct serial *scb, int timeout, 1035 int (do_readchar) (struct serial *scb, int timeout)) 1036{ 1037 int ch; 1038 if (scb->bufcnt > 0) 1039 { 1040 ch = *scb->bufp; 1041 scb->bufcnt--; 1042 scb->bufp++; 1043 } 1044 else if (scb->bufcnt < 0) 1045 { 1046 /* Some errors/eof are are sticky. */ 1047 ch = scb->bufcnt; 1048 } 1049 else 1050 { 1051 ch = do_readchar (scb, timeout); 1052 if (ch < 0) 1053 { 1054 switch ((enum serial_rc) ch) 1055 { 1056 case SERIAL_EOF: 1057 case SERIAL_ERROR: 1058 /* Make the error/eof stick. */ 1059 scb->bufcnt = ch; 1060 break; 1061 case SERIAL_TIMEOUT: 1062 scb->bufcnt = 0; 1063 break; 1064 } 1065 } 1066 } 1067 reschedule (scb); 1068 return ch; 1069} 1070 1071int 1072ser_unix_readchar (struct serial *scb, int timeout) 1073{ 1074 return generic_readchar (scb, timeout, do_unix_readchar); 1075} 1076 1077int 1078ser_unix_nop_noflush_set_tty_state (struct serial *scb, 1079 serial_ttystate new_ttystate, 1080 serial_ttystate old_ttystate) 1081{ 1082 return 0; 1083} 1084 1085void 1086ser_unix_nop_print_tty_state (struct serial *scb, 1087 serial_ttystate ttystate, 1088 struct ui_file *stream) 1089{ 1090 /* Nothing to print. */ 1091 return; 1092} 1093 1094int 1095ser_unix_nop_setbaudrate (struct serial *scb, int rate) 1096{ 1097 return 0; /* Never fails! */ 1098} 1099 1100int 1101ser_unix_nop_setstopbits (struct serial *scb, int num) 1102{ 1103 return 0; /* Never fails! */ 1104} 1105 1106int 1107ser_unix_write (struct serial *scb, const char *str, int len) 1108{ 1109 int cc; 1110 1111 while (len > 0) 1112 { 1113 cc = write (scb->fd, str, len); 1114 1115 if (cc < 0) 1116 return 1; 1117 len -= cc; 1118 str += cc; 1119 } 1120 return 0; 1121} 1122 1123int 1124ser_unix_nop_flush_output (struct serial *scb) 1125{ 1126 return 0; 1127} 1128 1129int 1130ser_unix_flush_input (struct serial *scb) 1131{ 1132 if (scb->bufcnt >= 0) 1133 { 1134 scb->bufcnt = 0; 1135 scb->bufp = scb->buf; 1136 return 0; 1137 } 1138 else 1139 return SERIAL_ERROR; 1140} 1141 1142int 1143ser_unix_nop_send_break (struct serial *scb) 1144{ 1145 return 0; 1146} 1147 1148int 1149ser_unix_nop_drain_output (struct serial *scb) 1150{ 1151 return 0; 1152} 1153 1154 1155 1156/* Event handling for ASYNC serial code. 1157 1158 At any time the SERIAL device either: has an empty FIFO and is 1159 waiting on a FD event; or has a non-empty FIFO/error condition and 1160 is constantly scheduling timer events. 1161 1162 ASYNC only stops pestering its client when it is de-async'ed or it 1163 is told to go away. */ 1164 1165/* Value of scb->async_state: */ 1166enum { 1167 /* >= 0 (TIMER_SCHEDULED) */ 1168 /* The ID of the currently scheduled timer event. This state is 1169 rarely encountered. Timer events are one-off so as soon as the 1170 event is delivered the state is shanged to NOTHING_SCHEDULED. */ 1171 FD_SCHEDULED = -1, 1172 /* The fd_event() handler is scheduled. It is called when ever the 1173 file descriptor becomes ready. */ 1174 NOTHING_SCHEDULED = -2 1175 /* Either no task is scheduled (just going into ASYNC mode) or a 1176 timer event has just gone off and the current state has been 1177 forced into nothing scheduled. */ 1178}; 1179 1180/* Identify and schedule the next ASYNC task based on scb->async_state 1181 and scb->buf* (the input FIFO). A state machine is used to avoid 1182 the need to make redundant calls into the event-loop - the next 1183 scheduled task is only changed when needed. */ 1184 1185static void 1186reschedule (struct serial *scb) 1187{ 1188 if (serial_is_async_p (scb)) 1189 { 1190 int next_state; 1191 switch (scb->async_state) 1192 { 1193 case FD_SCHEDULED: 1194 if (scb->bufcnt == 0) 1195 next_state = FD_SCHEDULED; 1196 else 1197 { 1198 delete_file_handler (scb->fd); 1199 next_state = create_timer (0, push_event, scb); 1200 } 1201 break; 1202 case NOTHING_SCHEDULED: 1203 if (scb->bufcnt == 0) 1204 { 1205 add_file_handler (scb->fd, fd_event, scb); 1206 next_state = FD_SCHEDULED; 1207 } 1208 else 1209 { 1210 next_state = create_timer (0, push_event, scb); 1211 } 1212 break; 1213 default: /* TIMER SCHEDULED */ 1214 if (scb->bufcnt == 0) 1215 { 1216 delete_timer (scb->async_state); 1217 add_file_handler (scb->fd, fd_event, scb); 1218 next_state = FD_SCHEDULED; 1219 } 1220 else 1221 next_state = scb->async_state; 1222 break; 1223 } 1224 if (serial_debug_p (scb)) 1225 { 1226 switch (next_state) 1227 { 1228 case FD_SCHEDULED: 1229 if (scb->async_state != FD_SCHEDULED) 1230 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n", 1231 scb->fd); 1232 break; 1233 default: /* TIMER SCHEDULED */ 1234 if (scb->async_state == FD_SCHEDULED) 1235 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n", 1236 scb->fd); 1237 break; 1238 } 1239 } 1240 scb->async_state = next_state; 1241 } 1242} 1243 1244/* FD_EVENT: This is scheduled when the input FIFO is empty (and there 1245 is no pending error). As soon as data arrives, it is read into the 1246 input FIFO and the client notified. The client should then drain 1247 the FIFO using readchar(). If the FIFO isn't immediatly emptied, 1248 push_event() is used to nag the client until it is. */ 1249 1250static void 1251fd_event (int error, void *context) 1252{ 1253 struct serial *scb = context; 1254 if (error != 0) 1255 { 1256 scb->bufcnt = SERIAL_ERROR; 1257 } 1258 else if (scb->bufcnt == 0) 1259 { 1260 /* Prime the input FIFO. The readchar() function is used to 1261 pull characters out of the buffer. See also 1262 generic_readchar(). */ 1263 int nr; 1264 do 1265 { 1266 nr = read (scb->fd, scb->buf, BUFSIZ); 1267 } 1268 while (nr == -1 && errno == EINTR); 1269 if (nr == 0) 1270 { 1271 scb->bufcnt = SERIAL_EOF; 1272 } 1273 else if (nr > 0) 1274 { 1275 scb->bufcnt = nr; 1276 scb->bufp = scb->buf; 1277 } 1278 else 1279 { 1280 scb->bufcnt = SERIAL_ERROR; 1281 } 1282 } 1283 scb->async_handler (scb, scb->async_context); 1284 reschedule (scb); 1285} 1286 1287/* PUSH_EVENT: The input FIFO is non-empty (or there is a pending 1288 error). Nag the client until all the data has been read. In the 1289 case of errors, the client will need to close or de-async the 1290 device before naging stops. */ 1291 1292static void 1293push_event (void *context) 1294{ 1295 struct serial *scb = context; 1296 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */ 1297 scb->async_handler (scb, scb->async_context); 1298 /* re-schedule */ 1299 reschedule (scb); 1300} 1301 1302/* Put the SERIAL device into/out-of ASYNC mode. */ 1303 1304void 1305ser_unix_async (struct serial *scb, 1306 int async_p) 1307{ 1308 if (async_p) 1309 { 1310 /* Force a re-schedule. */ 1311 scb->async_state = NOTHING_SCHEDULED; 1312 if (serial_debug_p (scb)) 1313 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n", 1314 scb->fd); 1315 reschedule (scb); 1316 } 1317 else 1318 { 1319 if (serial_debug_p (scb)) 1320 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n", 1321 scb->fd); 1322 /* De-schedule whatever tasks are currently scheduled. */ 1323 switch (scb->async_state) 1324 { 1325 case FD_SCHEDULED: 1326 delete_file_handler (scb->fd); 1327 break; 1328 case NOTHING_SCHEDULED: 1329 break; 1330 default: /* TIMER SCHEDULED */ 1331 delete_timer (scb->async_state); 1332 break; 1333 } 1334 } 1335} 1336 1337void 1338_initialize_ser_hardwire (void) 1339{ 1340 struct serial_ops *ops = XMALLOC (struct serial_ops); 1341 memset (ops, 0, sizeof (struct serial_ops)); 1342 ops->name = "hardwire"; 1343 ops->next = 0; 1344 ops->open = hardwire_open; 1345 ops->close = hardwire_close; 1346 /* FIXME: Don't replace this with the equivalent ser_unix*() until 1347 the old TERMIOS/SGTTY/... timer code has been flushed. cagney 1348 1999-09-16. */ 1349 ops->readchar = hardwire_readchar; 1350 ops->write = ser_unix_write; 1351 ops->flush_output = hardwire_flush_output; 1352 ops->flush_input = hardwire_flush_input; 1353 ops->send_break = hardwire_send_break; 1354 ops->go_raw = hardwire_raw; 1355 ops->get_tty_state = hardwire_get_tty_state; 1356 ops->set_tty_state = hardwire_set_tty_state; 1357 ops->print_tty_state = hardwire_print_tty_state; 1358 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state; 1359 ops->setbaudrate = hardwire_setbaudrate; 1360 ops->setstopbits = hardwire_setstopbits; 1361 ops->drain_output = hardwire_drain_output; 1362 ops->async = ser_unix_async; 1363 serial_add_interface (ops); 1364} 1365