GDB (API)
|
00001 /* Serial interface for local (hardwired) serial ports on Un*x like systems 00002 00003 Copyright (C) 1992-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "serial.h" 00022 #include "ser-base.h" 00023 #include "ser-unix.h" 00024 00025 #include <fcntl.h> 00026 #include <sys/types.h> 00027 #include "terminal.h" 00028 #include <sys/socket.h> 00029 #include <sys/time.h> 00030 00031 #include "gdb_select.h" 00032 #include "gdb_string.h" 00033 #include "gdbcmd.h" 00034 #include "filestuff.h" 00035 00036 #ifdef HAVE_TERMIOS 00037 00038 struct hardwire_ttystate 00039 { 00040 struct termios termios; 00041 }; 00042 00043 #ifdef CRTSCTS 00044 /* Boolean to explicitly enable or disable h/w flow control. */ 00045 static int serial_hwflow; 00046 static void 00047 show_serial_hwflow (struct ui_file *file, int from_tty, 00048 struct cmd_list_element *c, const char *value) 00049 { 00050 fprintf_filtered (file, _("Hardware flow control is %s.\n"), value); 00051 } 00052 #endif 00053 00054 #endif /* termios */ 00055 00056 #ifdef HAVE_TERMIO 00057 00058 /* It is believed that all systems which have added job control to SVR3 00059 (e.g. sco) have also added termios. Even if not, trying to figure out 00060 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty 00061 bewildering. So we don't attempt it. */ 00062 00063 struct hardwire_ttystate 00064 { 00065 struct termio termio; 00066 }; 00067 #endif /* termio */ 00068 00069 #ifdef HAVE_SGTTY 00070 struct hardwire_ttystate 00071 { 00072 struct sgttyb sgttyb; 00073 struct tchars tc; 00074 struct ltchars ltc; 00075 /* Line discipline flags. */ 00076 int lmode; 00077 }; 00078 #endif /* sgtty */ 00079 00080 static int hardwire_open (struct serial *scb, const char *name); 00081 static void hardwire_raw (struct serial *scb); 00082 static int wait_for (struct serial *scb, int timeout); 00083 static int hardwire_readchar (struct serial *scb, int timeout); 00084 static int do_hardwire_readchar (struct serial *scb, int timeout); 00085 static int rate_to_code (int rate); 00086 static int hardwire_setbaudrate (struct serial *scb, int rate); 00087 static void hardwire_close (struct serial *scb); 00088 static int get_tty_state (struct serial *scb, 00089 struct hardwire_ttystate * state); 00090 static int set_tty_state (struct serial *scb, 00091 struct hardwire_ttystate * state); 00092 static serial_ttystate hardwire_get_tty_state (struct serial *scb); 00093 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state); 00094 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate, 00095 serial_ttystate); 00096 static void hardwire_print_tty_state (struct serial *, serial_ttystate, 00097 struct ui_file *); 00098 static int hardwire_drain_output (struct serial *); 00099 static int hardwire_flush_output (struct serial *); 00100 static int hardwire_flush_input (struct serial *); 00101 static int hardwire_send_break (struct serial *); 00102 static int hardwire_setstopbits (struct serial *, int); 00103 00104 void _initialize_ser_hardwire (void); 00105 00106 /* Open up a real live device for serial I/O. */ 00107 00108 static int 00109 hardwire_open (struct serial *scb, const char *name) 00110 { 00111 scb->fd = gdb_open_cloexec (name, O_RDWR, 0); 00112 if (scb->fd < 0) 00113 return -1; 00114 00115 return 0; 00116 } 00117 00118 static int 00119 get_tty_state (struct serial *scb, struct hardwire_ttystate *state) 00120 { 00121 #ifdef HAVE_TERMIOS 00122 if (tcgetattr (scb->fd, &state->termios) < 0) 00123 return -1; 00124 00125 return 0; 00126 #endif 00127 00128 #ifdef HAVE_TERMIO 00129 if (ioctl (scb->fd, TCGETA, &state->termio) < 0) 00130 return -1; 00131 return 0; 00132 #endif 00133 00134 #ifdef HAVE_SGTTY 00135 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0) 00136 return -1; 00137 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0) 00138 return -1; 00139 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0) 00140 return -1; 00141 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0) 00142 return -1; 00143 00144 return 0; 00145 #endif 00146 } 00147 00148 static int 00149 set_tty_state (struct serial *scb, struct hardwire_ttystate *state) 00150 { 00151 #ifdef HAVE_TERMIOS 00152 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0) 00153 return -1; 00154 00155 return 0; 00156 #endif 00157 00158 #ifdef HAVE_TERMIO 00159 if (ioctl (scb->fd, TCSETA, &state->termio) < 0) 00160 return -1; 00161 return 0; 00162 #endif 00163 00164 #ifdef HAVE_SGTTY 00165 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0) 00166 return -1; 00167 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0) 00168 return -1; 00169 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0) 00170 return -1; 00171 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0) 00172 return -1; 00173 00174 return 0; 00175 #endif 00176 } 00177 00178 static serial_ttystate 00179 hardwire_get_tty_state (struct serial *scb) 00180 { 00181 struct hardwire_ttystate *state; 00182 00183 state = (struct hardwire_ttystate *) xmalloc (sizeof *state); 00184 00185 if (get_tty_state (scb, state)) 00186 { 00187 xfree (state); 00188 return NULL; 00189 } 00190 00191 return (serial_ttystate) state; 00192 } 00193 00194 static serial_ttystate 00195 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate) 00196 { 00197 struct hardwire_ttystate *state; 00198 00199 state = (struct hardwire_ttystate *) xmalloc (sizeof *state); 00200 *state = *(struct hardwire_ttystate *) ttystate; 00201 00202 return (serial_ttystate) state; 00203 } 00204 00205 static int 00206 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate) 00207 { 00208 struct hardwire_ttystate *state; 00209 00210 state = (struct hardwire_ttystate *) ttystate; 00211 00212 return set_tty_state (scb, state); 00213 } 00214 00215 static int 00216 hardwire_noflush_set_tty_state (struct serial *scb, 00217 serial_ttystate new_ttystate, 00218 serial_ttystate old_ttystate) 00219 { 00220 struct hardwire_ttystate new_state; 00221 #ifdef HAVE_SGTTY 00222 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate; 00223 #endif 00224 00225 new_state = *(struct hardwire_ttystate *) new_ttystate; 00226 00227 /* Don't change in or out of raw mode; we don't want to flush input. 00228 termio and termios have no such restriction; for them flushing input 00229 is separate from setting the attributes. */ 00230 00231 #ifdef HAVE_SGTTY 00232 if (state->sgttyb.sg_flags & RAW) 00233 new_state.sgttyb.sg_flags |= RAW; 00234 else 00235 new_state.sgttyb.sg_flags &= ~RAW; 00236 00237 /* I'm not sure whether this is necessary; the manpage just mentions 00238 RAW not CBREAK. */ 00239 if (state->sgttyb.sg_flags & CBREAK) 00240 new_state.sgttyb.sg_flags |= CBREAK; 00241 else 00242 new_state.sgttyb.sg_flags &= ~CBREAK; 00243 #endif 00244 00245 return set_tty_state (scb, &new_state); 00246 } 00247 00248 static void 00249 hardwire_print_tty_state (struct serial *scb, 00250 serial_ttystate ttystate, 00251 struct ui_file *stream) 00252 { 00253 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate; 00254 int i; 00255 00256 #ifdef HAVE_TERMIOS 00257 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", 00258 (int) state->termios.c_iflag, 00259 (int) state->termios.c_oflag); 00260 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n", 00261 (int) state->termios.c_cflag, 00262 (int) state->termios.c_lflag); 00263 #if 0 00264 /* This not in POSIX, and is not really documented by those systems 00265 which have it (at least not Sun). */ 00266 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line); 00267 #endif 00268 fprintf_filtered (stream, "c_cc: "); 00269 for (i = 0; i < NCCS; i += 1) 00270 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]); 00271 fprintf_filtered (stream, "\n"); 00272 #endif 00273 00274 #ifdef HAVE_TERMIO 00275 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", 00276 state->termio.c_iflag, state->termio.c_oflag); 00277 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", 00278 state->termio.c_cflag, state->termio.c_lflag, 00279 state->termio.c_line); 00280 fprintf_filtered (stream, "c_cc: "); 00281 for (i = 0; i < NCC; i += 1) 00282 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]); 00283 fprintf_filtered (stream, "\n"); 00284 #endif 00285 00286 #ifdef HAVE_SGTTY 00287 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n", 00288 state->sgttyb.sg_flags); 00289 00290 fprintf_filtered (stream, "tchars: "); 00291 for (i = 0; i < (int) sizeof (struct tchars); i++) 00292 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]); 00293 fprintf_filtered (stream, "\n"); 00294 00295 fprintf_filtered (stream, "ltchars: "); 00296 for (i = 0; i < (int) sizeof (struct ltchars); i++) 00297 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]); 00298 fprintf_filtered (stream, "\n"); 00299 00300 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode); 00301 #endif 00302 } 00303 00304 /* Wait for the output to drain away, as opposed to flushing 00305 (discarding) it. */ 00306 00307 static int 00308 hardwire_drain_output (struct serial *scb) 00309 { 00310 #ifdef HAVE_TERMIOS 00311 return tcdrain (scb->fd); 00312 #endif 00313 00314 #ifdef HAVE_TERMIO 00315 return ioctl (scb->fd, TCSBRK, 1); 00316 #endif 00317 00318 #ifdef HAVE_SGTTY 00319 /* Get the current state and then restore it using TIOCSETP, 00320 which should cause the output to drain and pending input 00321 to be discarded. */ 00322 { 00323 struct hardwire_ttystate state; 00324 00325 if (get_tty_state (scb, &state)) 00326 { 00327 return (-1); 00328 } 00329 else 00330 { 00331 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb)); 00332 } 00333 } 00334 #endif 00335 } 00336 00337 static int 00338 hardwire_flush_output (struct serial *scb) 00339 { 00340 #ifdef HAVE_TERMIOS 00341 return tcflush (scb->fd, TCOFLUSH); 00342 #endif 00343 00344 #ifdef HAVE_TERMIO 00345 return ioctl (scb->fd, TCFLSH, 1); 00346 #endif 00347 00348 #ifdef HAVE_SGTTY 00349 /* This flushes both input and output, but we can't do better. */ 00350 return ioctl (scb->fd, TIOCFLUSH, 0); 00351 #endif 00352 } 00353 00354 static int 00355 hardwire_flush_input (struct serial *scb) 00356 { 00357 ser_base_flush_input (scb); 00358 00359 #ifdef HAVE_TERMIOS 00360 return tcflush (scb->fd, TCIFLUSH); 00361 #endif 00362 00363 #ifdef HAVE_TERMIO 00364 return ioctl (scb->fd, TCFLSH, 0); 00365 #endif 00366 00367 #ifdef HAVE_SGTTY 00368 /* This flushes both input and output, but we can't do better. */ 00369 return ioctl (scb->fd, TIOCFLUSH, 0); 00370 #endif 00371 } 00372 00373 static int 00374 hardwire_send_break (struct serial *scb) 00375 { 00376 #ifdef HAVE_TERMIOS 00377 return tcsendbreak (scb->fd, 0); 00378 #endif 00379 00380 #ifdef HAVE_TERMIO 00381 return ioctl (scb->fd, TCSBRK, 0); 00382 #endif 00383 00384 #ifdef HAVE_SGTTY 00385 { 00386 int status; 00387 00388 status = ioctl (scb->fd, TIOCSBRK, 0); 00389 00390 /* Can't use usleep; it doesn't exist in BSD 4.2. */ 00391 /* Note that if this gdb_select() is interrupted by a signal it will not 00392 wait the full length of time. I think that is OK. */ 00393 gdb_usleep (250000); 00394 status = ioctl (scb->fd, TIOCCBRK, 0); 00395 return status; 00396 } 00397 #endif 00398 } 00399 00400 static void 00401 hardwire_raw (struct serial *scb) 00402 { 00403 struct hardwire_ttystate state; 00404 00405 if (get_tty_state (scb, &state)) 00406 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", 00407 safe_strerror (errno)); 00408 00409 #ifdef HAVE_TERMIOS 00410 state.termios.c_iflag = 0; 00411 state.termios.c_oflag = 0; 00412 state.termios.c_lflag = 0; 00413 state.termios.c_cflag &= ~(CSIZE | PARENB); 00414 state.termios.c_cflag |= CLOCAL | CS8; 00415 #ifdef CRTSCTS 00416 /* h/w flow control. */ 00417 if (serial_hwflow) 00418 state.termios.c_cflag |= CRTSCTS; 00419 else 00420 state.termios.c_cflag &= ~CRTSCTS; 00421 #ifdef CRTS_IFLOW 00422 if (serial_hwflow) 00423 state.termios.c_cflag |= CRTS_IFLOW; 00424 else 00425 state.termios.c_cflag &= ~CRTS_IFLOW; 00426 #endif 00427 #endif 00428 state.termios.c_cc[VMIN] = 0; 00429 state.termios.c_cc[VTIME] = 0; 00430 #endif 00431 00432 #ifdef HAVE_TERMIO 00433 state.termio.c_iflag = 0; 00434 state.termio.c_oflag = 0; 00435 state.termio.c_lflag = 0; 00436 state.termio.c_cflag &= ~(CSIZE | PARENB); 00437 state.termio.c_cflag |= CLOCAL | CS8; 00438 state.termio.c_cc[VMIN] = 0; 00439 state.termio.c_cc[VTIME] = 0; 00440 #endif 00441 00442 #ifdef HAVE_SGTTY 00443 state.sgttyb.sg_flags |= RAW | ANYP; 00444 state.sgttyb.sg_flags &= ~(CBREAK | ECHO); 00445 #endif 00446 00447 scb->current_timeout = 0; 00448 00449 if (set_tty_state (scb, &state)) 00450 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", 00451 safe_strerror (errno)); 00452 } 00453 00454 /* Wait for input on scb, with timeout seconds. Returns 0 on success, 00455 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. 00456 00457 For termio{s}, we actually just setup VTIME if necessary, and let the 00458 timeout occur in the read() in hardwire_read(). */ 00459 00460 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent 00461 ser_base*() until the old TERMIOS/SGTTY/... timer code has been 00462 flushed. . */ 00463 00464 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only 00465 possible values of the TIMEOUT parameter are ONE and ZERO. 00466 Consequently all the code that tries to handle the possability of 00467 an overflowed timer is unnecessary. */ 00468 00469 static int 00470 wait_for (struct serial *scb, int timeout) 00471 { 00472 #ifdef HAVE_SGTTY 00473 while (1) 00474 { 00475 struct timeval tv; 00476 fd_set readfds; 00477 int numfds; 00478 00479 /* NOTE: Some OS's can scramble the READFDS when the select() 00480 call fails (ex the kernel with Red Hat 5.2). Initialize all 00481 arguments before each call. */ 00482 00483 tv.tv_sec = timeout; 00484 tv.tv_usec = 0; 00485 00486 FD_ZERO (&readfds); 00487 FD_SET (scb->fd, &readfds); 00488 00489 if (timeout >= 0) 00490 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv); 00491 else 00492 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0); 00493 00494 if (numfds <= 0) 00495 if (numfds == 0) 00496 return SERIAL_TIMEOUT; 00497 else if (errno == EINTR) 00498 continue; 00499 else 00500 return SERIAL_ERROR; /* Got an error from select or poll. */ 00501 00502 return 0; 00503 } 00504 #endif /* HAVE_SGTTY */ 00505 00506 #if defined HAVE_TERMIO || defined HAVE_TERMIOS 00507 if (timeout == scb->current_timeout) 00508 return 0; 00509 00510 scb->current_timeout = timeout; 00511 00512 { 00513 struct hardwire_ttystate state; 00514 00515 if (get_tty_state (scb, &state)) 00516 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", 00517 safe_strerror (errno)); 00518 00519 #ifdef HAVE_TERMIOS 00520 if (timeout < 0) 00521 { 00522 /* No timeout. */ 00523 state.termios.c_cc[VTIME] = 0; 00524 state.termios.c_cc[VMIN] = 1; 00525 } 00526 else 00527 { 00528 state.termios.c_cc[VMIN] = 0; 00529 state.termios.c_cc[VTIME] = timeout * 10; 00530 if (state.termios.c_cc[VTIME] != timeout * 10) 00531 { 00532 00533 /* If c_cc is an 8-bit signed character, we can't go 00534 bigger than this. If it is always unsigned, we could use 00535 25. */ 00536 00537 scb->current_timeout = 12; 00538 state.termios.c_cc[VTIME] = scb->current_timeout * 10; 00539 scb->timeout_remaining = timeout - scb->current_timeout; 00540 } 00541 } 00542 #endif 00543 00544 #ifdef HAVE_TERMIO 00545 if (timeout < 0) 00546 { 00547 /* No timeout. */ 00548 state.termio.c_cc[VTIME] = 0; 00549 state.termio.c_cc[VMIN] = 1; 00550 } 00551 else 00552 { 00553 state.termio.c_cc[VMIN] = 0; 00554 state.termio.c_cc[VTIME] = timeout * 10; 00555 if (state.termio.c_cc[VTIME] != timeout * 10) 00556 { 00557 /* If c_cc is an 8-bit signed character, we can't go 00558 bigger than this. If it is always unsigned, we could use 00559 25. */ 00560 00561 scb->current_timeout = 12; 00562 state.termio.c_cc[VTIME] = scb->current_timeout * 10; 00563 scb->timeout_remaining = timeout - scb->current_timeout; 00564 } 00565 } 00566 #endif 00567 00568 if (set_tty_state (scb, &state)) 00569 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", 00570 safe_strerror (errno)); 00571 00572 return 0; 00573 } 00574 #endif /* HAVE_TERMIO || HAVE_TERMIOS */ 00575 } 00576 00577 /* Read a character with user-specified timeout. TIMEOUT is number of 00578 seconds to wait, or -1 to wait forever. Use timeout of 0 to effect 00579 a poll. Returns char if successful. Returns SERIAL_TIMEOUT if 00580 timeout expired, EOF if line dropped dead, or SERIAL_ERROR for any 00581 other error (see errno in that case). */ 00582 00583 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent 00584 ser_base*() until the old TERMIOS/SGTTY/... timer code has been 00585 flushed. */ 00586 00587 /* NOTE: cagney/1999-09-16: This function is not identical to 00588 ser_base_readchar() as part of replacing it with ser_base*() 00589 merging will be required - this code handles the case where read() 00590 times out due to no data while ser_base_readchar() doesn't expect 00591 that. */ 00592 00593 static int 00594 do_hardwire_readchar (struct serial *scb, int timeout) 00595 { 00596 int status, delta; 00597 int detach = 0; 00598 00599 if (timeout > 0) 00600 timeout++; 00601 00602 /* We have to be able to keep the GUI alive here, so we break the 00603 original timeout into steps of 1 second, running the "keep the 00604 GUI alive" hook each time through the loop. 00605 00606 Also, timeout = 0 means to poll, so we just set the delta to 0, 00607 so we will only go through the loop once. */ 00608 00609 delta = (timeout == 0 ? 0 : 1); 00610 while (1) 00611 { 00612 00613 /* N.B. The UI may destroy our world (for instance by calling 00614 remote_stop,) in which case we want to get out of here as 00615 quickly as possible. It is not safe to touch scb, since 00616 someone else might have freed it. The 00617 deprecated_ui_loop_hook signals that we should exit by 00618 returning 1. */ 00619 00620 if (deprecated_ui_loop_hook) 00621 detach = deprecated_ui_loop_hook (0); 00622 00623 if (detach) 00624 return SERIAL_TIMEOUT; 00625 00626 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta); 00627 status = wait_for (scb, delta); 00628 00629 if (status < 0) 00630 return status; 00631 00632 status = read (scb->fd, scb->buf, BUFSIZ); 00633 00634 if (status <= 0) 00635 { 00636 if (status == 0) 00637 { 00638 /* Zero characters means timeout (it could also be EOF, but 00639 we don't (yet at least) distinguish). */ 00640 if (scb->timeout_remaining > 0) 00641 { 00642 timeout = scb->timeout_remaining; 00643 continue; 00644 } 00645 else if (scb->timeout_remaining < 0) 00646 continue; 00647 else 00648 return SERIAL_TIMEOUT; 00649 } 00650 else if (errno == EINTR) 00651 continue; 00652 else 00653 return SERIAL_ERROR; /* Got an error from read. */ 00654 } 00655 00656 scb->bufcnt = status; 00657 scb->bufcnt--; 00658 scb->bufp = scb->buf; 00659 return *scb->bufp++; 00660 } 00661 } 00662 00663 static int 00664 hardwire_readchar (struct serial *scb, int timeout) 00665 { 00666 return generic_readchar (scb, timeout, do_hardwire_readchar); 00667 } 00668 00669 00670 #ifndef B19200 00671 #define B19200 EXTA 00672 #endif 00673 00674 #ifndef B38400 00675 #define B38400 EXTB 00676 #endif 00677 00678 /* Translate baud rates from integers to damn B_codes. Unix should 00679 have outgrown this crap years ago, but even POSIX wouldn't buck it. */ 00680 00681 static struct 00682 { 00683 int rate; 00684 int code; 00685 } 00686 baudtab[] = 00687 { 00688 { 00689 50, B50 00690 } 00691 , 00692 { 00693 75, B75 00694 } 00695 , 00696 { 00697 110, B110 00698 } 00699 , 00700 { 00701 134, B134 00702 } 00703 , 00704 { 00705 150, B150 00706 } 00707 , 00708 { 00709 200, B200 00710 } 00711 , 00712 { 00713 300, B300 00714 } 00715 , 00716 { 00717 600, B600 00718 } 00719 , 00720 { 00721 1200, B1200 00722 } 00723 , 00724 { 00725 1800, B1800 00726 } 00727 , 00728 { 00729 2400, B2400 00730 } 00731 , 00732 { 00733 4800, B4800 00734 } 00735 , 00736 { 00737 9600, B9600 00738 } 00739 , 00740 { 00741 19200, B19200 00742 } 00743 , 00744 { 00745 38400, B38400 00746 } 00747 , 00748 #ifdef B57600 00749 { 00750 57600, B57600 00751 } 00752 , 00753 #endif 00754 #ifdef B115200 00755 { 00756 115200, B115200 00757 } 00758 , 00759 #endif 00760 #ifdef B230400 00761 { 00762 230400, B230400 00763 } 00764 , 00765 #endif 00766 #ifdef B460800 00767 { 00768 460800, B460800 00769 } 00770 , 00771 #endif 00772 { 00773 -1, -1 00774 } 00775 , 00776 }; 00777 00778 static int 00779 rate_to_code (int rate) 00780 { 00781 int i; 00782 00783 for (i = 0; baudtab[i].rate != -1; i++) 00784 { 00785 /* test for perfect macth. */ 00786 if (rate == baudtab[i].rate) 00787 return baudtab[i].code; 00788 else 00789 { 00790 /* check if it is in between valid values. */ 00791 if (rate < baudtab[i].rate) 00792 { 00793 if (i) 00794 { 00795 warning (_("Invalid baud rate %d. " 00796 "Closest values are %d and %d."), 00797 rate, baudtab[i - 1].rate, baudtab[i].rate); 00798 } 00799 else 00800 { 00801 warning (_("Invalid baud rate %d. Minimum value is %d."), 00802 rate, baudtab[0].rate); 00803 } 00804 return -1; 00805 } 00806 } 00807 } 00808 00809 /* The requested speed was too large. */ 00810 warning (_("Invalid baud rate %d. Maximum value is %d."), 00811 rate, baudtab[i - 1].rate); 00812 return -1; 00813 } 00814 00815 static int 00816 hardwire_setbaudrate (struct serial *scb, int rate) 00817 { 00818 struct hardwire_ttystate state; 00819 int baud_code = rate_to_code (rate); 00820 00821 if (baud_code < 0) 00822 { 00823 /* The baud rate was not valid. 00824 A warning has already been issued. */ 00825 errno = EINVAL; 00826 return -1; 00827 } 00828 00829 if (get_tty_state (scb, &state)) 00830 return -1; 00831 00832 #ifdef HAVE_TERMIOS 00833 cfsetospeed (&state.termios, baud_code); 00834 cfsetispeed (&state.termios, baud_code); 00835 #endif 00836 00837 #ifdef HAVE_TERMIO 00838 #ifndef CIBAUD 00839 #define CIBAUD CBAUD 00840 #endif 00841 00842 state.termio.c_cflag &= ~(CBAUD | CIBAUD); 00843 state.termio.c_cflag |= baud_code; 00844 #endif 00845 00846 #ifdef HAVE_SGTTY 00847 state.sgttyb.sg_ispeed = baud_code; 00848 state.sgttyb.sg_ospeed = baud_code; 00849 #endif 00850 00851 return set_tty_state (scb, &state); 00852 } 00853 00854 static int 00855 hardwire_setstopbits (struct serial *scb, int num) 00856 { 00857 struct hardwire_ttystate state; 00858 int newbit; 00859 00860 if (get_tty_state (scb, &state)) 00861 return -1; 00862 00863 switch (num) 00864 { 00865 case SERIAL_1_STOPBITS: 00866 newbit = 0; 00867 break; 00868 case SERIAL_1_AND_A_HALF_STOPBITS: 00869 case SERIAL_2_STOPBITS: 00870 newbit = 1; 00871 break; 00872 default: 00873 return 1; 00874 } 00875 00876 #ifdef HAVE_TERMIOS 00877 if (!newbit) 00878 state.termios.c_cflag &= ~CSTOPB; 00879 else 00880 state.termios.c_cflag |= CSTOPB; /* two bits */ 00881 #endif 00882 00883 #ifdef HAVE_TERMIO 00884 if (!newbit) 00885 state.termio.c_cflag &= ~CSTOPB; 00886 else 00887 state.termio.c_cflag |= CSTOPB; /* two bits */ 00888 #endif 00889 00890 #ifdef HAVE_SGTTY 00891 return 0; /* sgtty doesn't support this */ 00892 #endif 00893 00894 return set_tty_state (scb, &state); 00895 } 00896 00897 static void 00898 hardwire_close (struct serial *scb) 00899 { 00900 if (scb->fd < 0) 00901 return; 00902 00903 close (scb->fd); 00904 scb->fd = -1; 00905 } 00906 00907 00908 void 00909 _initialize_ser_hardwire (void) 00910 { 00911 struct serial_ops *ops = XMALLOC (struct serial_ops); 00912 00913 memset (ops, 0, sizeof (struct serial_ops)); 00914 ops->name = "hardwire"; 00915 ops->next = 0; 00916 ops->open = hardwire_open; 00917 ops->close = hardwire_close; 00918 /* FIXME: Don't replace this with the equivalent ser_base*() until 00919 the old TERMIOS/SGTTY/... timer code has been flushed. cagney 00920 1999-09-16. */ 00921 ops->readchar = hardwire_readchar; 00922 ops->write = ser_base_write; 00923 ops->flush_output = hardwire_flush_output; 00924 ops->flush_input = hardwire_flush_input; 00925 ops->send_break = hardwire_send_break; 00926 ops->go_raw = hardwire_raw; 00927 ops->get_tty_state = hardwire_get_tty_state; 00928 ops->copy_tty_state = hardwire_copy_tty_state; 00929 ops->set_tty_state = hardwire_set_tty_state; 00930 ops->print_tty_state = hardwire_print_tty_state; 00931 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state; 00932 ops->setbaudrate = hardwire_setbaudrate; 00933 ops->setstopbits = hardwire_setstopbits; 00934 ops->drain_output = hardwire_drain_output; 00935 ops->async = ser_base_async; 00936 ops->read_prim = ser_unix_read_prim; 00937 ops->write_prim = ser_unix_write_prim; 00938 serial_add_interface (ops); 00939 00940 #ifdef HAVE_TERMIOS 00941 #ifdef CRTSCTS 00942 add_setshow_boolean_cmd ("remoteflow", no_class, 00943 &serial_hwflow, _("\ 00944 Set use of hardware flow control for remote serial I/O."), _("\ 00945 Show use of hardware flow control for remote serial I/O."), _("\ 00946 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\ 00947 when debugging using remote targets."), 00948 NULL, 00949 show_serial_hwflow, 00950 &setlist, &showlist); 00951 #endif 00952 #endif 00953 } 00954 00955 int 00956 ser_unix_read_prim (struct serial *scb, size_t count) 00957 { 00958 int status; 00959 00960 while (1) 00961 { 00962 status = read (scb->fd, scb->buf, count); 00963 if (status != -1 || errno != EINTR) 00964 break; 00965 } 00966 return status; 00967 } 00968 00969 int 00970 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len) 00971 { 00972 /* ??? Historically, GDB has not retried calls to "write" that 00973 result in EINTR. */ 00974 return write (scb->fd, buf, len); 00975 }