Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Serial interface for local (hardwired) serial ports on Un*x like systems |
1e4728e7 | 2 | |
61baf725 | 3 | Copyright (C) 1992-2017 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
21 | #include "serial.h" | |
3eb25fda | 22 | #include "ser-base.h" |
c2c6d25f JM |
23 | #include "ser-unix.h" |
24 | ||
c906108c SS |
25 | #include <fcntl.h> |
26 | #include <sys/types.h> | |
27 | #include "terminal.h" | |
c2c6d25f | 28 | #include <sys/socket.h> |
438e1e42 | 29 | #include "gdb_sys_time.h" |
c2c6d25f | 30 | |
0ea3f30e | 31 | #include "gdb_select.h" |
23776285 | 32 | #include "gdbcmd.h" |
614c279d | 33 | #include "filestuff.h" |
be628ab8 | 34 | #include "gdb_termios.h" |
c2c6d25f | 35 | |
c906108c SS |
36 | #ifdef HAVE_TERMIOS |
37 | ||
38 | struct hardwire_ttystate | |
c5aa993b JM |
39 | { |
40 | struct termios termios; | |
41 | }; | |
23776285 MR |
42 | |
43 | #ifdef CRTSCTS | |
44 | /* Boolean to explicitly enable or disable h/w flow control. */ | |
45 | static int serial_hwflow; | |
46 | static void | |
47 | show_serial_hwflow (struct ui_file *file, int from_tty, | |
48 | struct cmd_list_element *c, const char *value) | |
49 | { | |
50 | fprintf_filtered (file, _("Hardware flow control is %s.\n"), value); | |
51 | } | |
52 | #endif | |
53 | ||
c906108c SS |
54 | #endif /* termios */ |
55 | ||
56 | #ifdef HAVE_TERMIO | |
57 | ||
58 | /* It is believed that all systems which have added job control to SVR3 | |
59 | (e.g. sco) have also added termios. Even if not, trying to figure out | |
60 | all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty | |
61 | bewildering. So we don't attempt it. */ | |
62 | ||
63 | struct hardwire_ttystate | |
c5aa993b JM |
64 | { |
65 | struct termio termio; | |
66 | }; | |
c906108c SS |
67 | #endif /* termio */ |
68 | ||
69 | #ifdef HAVE_SGTTY | |
c906108c | 70 | struct hardwire_ttystate |
c5aa993b JM |
71 | { |
72 | struct sgttyb sgttyb; | |
73 | struct tchars tc; | |
74 | struct ltchars ltc; | |
75 | /* Line discipline flags. */ | |
76 | int lmode; | |
77 | }; | |
c906108c SS |
78 | #endif /* sgtty */ |
79 | ||
819cc324 AC |
80 | static int hardwire_open (struct serial *scb, const char *name); |
81 | static void hardwire_raw (struct serial *scb); | |
c2c6d25f | 82 | static int rate_to_code (int rate); |
819cc324 | 83 | static int hardwire_setbaudrate (struct serial *scb, int rate); |
236af5e3 | 84 | static int hardwire_setparity (struct serial *scb, int parity); |
819cc324 AC |
85 | static void hardwire_close (struct serial *scb); |
86 | static int get_tty_state (struct serial *scb, | |
87 | struct hardwire_ttystate * state); | |
88 | static int set_tty_state (struct serial *scb, | |
89 | struct hardwire_ttystate * state); | |
90 | static serial_ttystate hardwire_get_tty_state (struct serial *scb); | |
91 | static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state); | |
92 | static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate, | |
93 | serial_ttystate); | |
94 | static void hardwire_print_tty_state (struct serial *, serial_ttystate, | |
95 | struct ui_file *); | |
96 | static int hardwire_drain_output (struct serial *); | |
97 | static int hardwire_flush_output (struct serial *); | |
98 | static int hardwire_flush_input (struct serial *); | |
99 | static int hardwire_send_break (struct serial *); | |
100 | static int hardwire_setstopbits (struct serial *, int); | |
101 | ||
c378eb4e | 102 | /* Open up a real live device for serial I/O. */ |
c906108c SS |
103 | |
104 | static int | |
819cc324 | 105 | hardwire_open (struct serial *scb, const char *name) |
c906108c | 106 | { |
614c279d | 107 | scb->fd = gdb_open_cloexec (name, O_RDWR, 0); |
c906108c SS |
108 | if (scb->fd < 0) |
109 | return -1; | |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
114 | static int | |
819cc324 | 115 | get_tty_state (struct serial *scb, struct hardwire_ttystate *state) |
c906108c SS |
116 | { |
117 | #ifdef HAVE_TERMIOS | |
c5aa993b | 118 | if (tcgetattr (scb->fd, &state->termios) < 0) |
c906108c SS |
119 | return -1; |
120 | ||
121 | return 0; | |
122 | #endif | |
123 | ||
124 | #ifdef HAVE_TERMIO | |
125 | if (ioctl (scb->fd, TCGETA, &state->termio) < 0) | |
126 | return -1; | |
127 | return 0; | |
128 | #endif | |
129 | ||
130 | #ifdef HAVE_SGTTY | |
131 | if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0) | |
132 | return -1; | |
133 | if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0) | |
134 | return -1; | |
135 | if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0) | |
136 | return -1; | |
137 | if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0) | |
138 | return -1; | |
139 | ||
140 | return 0; | |
141 | #endif | |
142 | } | |
143 | ||
144 | static int | |
819cc324 | 145 | set_tty_state (struct serial *scb, struct hardwire_ttystate *state) |
c906108c SS |
146 | { |
147 | #ifdef HAVE_TERMIOS | |
c5aa993b | 148 | if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0) |
c906108c SS |
149 | return -1; |
150 | ||
151 | return 0; | |
152 | #endif | |
153 | ||
154 | #ifdef HAVE_TERMIO | |
155 | if (ioctl (scb->fd, TCSETA, &state->termio) < 0) | |
156 | return -1; | |
157 | return 0; | |
158 | #endif | |
159 | ||
160 | #ifdef HAVE_SGTTY | |
161 | if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0) | |
162 | return -1; | |
163 | if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0) | |
164 | return -1; | |
165 | if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0) | |
166 | return -1; | |
167 | if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0) | |
168 | return -1; | |
169 | ||
170 | return 0; | |
171 | #endif | |
172 | } | |
173 | ||
174 | static serial_ttystate | |
819cc324 | 175 | hardwire_get_tty_state (struct serial *scb) |
c906108c | 176 | { |
8d749320 | 177 | struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate); |
c906108c | 178 | |
c5aa993b | 179 | if (get_tty_state (scb, state)) |
0b2381f5 MS |
180 | { |
181 | xfree (state); | |
182 | return NULL; | |
183 | } | |
c906108c | 184 | |
c5aa993b | 185 | return (serial_ttystate) state; |
c906108c SS |
186 | } |
187 | ||
1e182ce8 UW |
188 | static serial_ttystate |
189 | hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate) | |
190 | { | |
8d749320 | 191 | struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate); |
1e182ce8 | 192 | |
1e182ce8 UW |
193 | *state = *(struct hardwire_ttystate *) ttystate; |
194 | ||
195 | return (serial_ttystate) state; | |
196 | } | |
197 | ||
c906108c | 198 | static int |
819cc324 | 199 | hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate) |
c906108c SS |
200 | { |
201 | struct hardwire_ttystate *state; | |
202 | ||
c5aa993b | 203 | state = (struct hardwire_ttystate *) ttystate; |
c906108c | 204 | |
c5aa993b | 205 | return set_tty_state (scb, state); |
c906108c SS |
206 | } |
207 | ||
208 | static int | |
819cc324 | 209 | hardwire_noflush_set_tty_state (struct serial *scb, |
c2c6d25f JM |
210 | serial_ttystate new_ttystate, |
211 | serial_ttystate old_ttystate) | |
c906108c SS |
212 | { |
213 | struct hardwire_ttystate new_state; | |
214 | #ifdef HAVE_SGTTY | |
215 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate; | |
216 | #endif | |
217 | ||
c5aa993b | 218 | new_state = *(struct hardwire_ttystate *) new_ttystate; |
c906108c SS |
219 | |
220 | /* Don't change in or out of raw mode; we don't want to flush input. | |
221 | termio and termios have no such restriction; for them flushing input | |
222 | is separate from setting the attributes. */ | |
223 | ||
224 | #ifdef HAVE_SGTTY | |
225 | if (state->sgttyb.sg_flags & RAW) | |
226 | new_state.sgttyb.sg_flags |= RAW; | |
227 | else | |
228 | new_state.sgttyb.sg_flags &= ~RAW; | |
229 | ||
230 | /* I'm not sure whether this is necessary; the manpage just mentions | |
231 | RAW not CBREAK. */ | |
232 | if (state->sgttyb.sg_flags & CBREAK) | |
233 | new_state.sgttyb.sg_flags |= CBREAK; | |
234 | else | |
235 | new_state.sgttyb.sg_flags &= ~CBREAK; | |
236 | #endif | |
237 | ||
238 | return set_tty_state (scb, &new_state); | |
239 | } | |
240 | ||
241 | static void | |
819cc324 | 242 | hardwire_print_tty_state (struct serial *scb, |
c2c6d25f | 243 | serial_ttystate ttystate, |
d9fcf2fb | 244 | struct ui_file *stream) |
c906108c SS |
245 | { |
246 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate; | |
247 | int i; | |
248 | ||
249 | #ifdef HAVE_TERMIOS | |
c2c6d25f | 250 | fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", |
2acceee2 JM |
251 | (int) state->termios.c_iflag, |
252 | (int) state->termios.c_oflag); | |
c2c6d25f | 253 | fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n", |
2acceee2 JM |
254 | (int) state->termios.c_cflag, |
255 | (int) state->termios.c_lflag); | |
c906108c SS |
256 | #if 0 |
257 | /* This not in POSIX, and is not really documented by those systems | |
258 | which have it (at least not Sun). */ | |
c2c6d25f | 259 | fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line); |
c906108c | 260 | #endif |
c2c6d25f | 261 | fprintf_filtered (stream, "c_cc: "); |
c906108c | 262 | for (i = 0; i < NCCS; i += 1) |
c2c6d25f JM |
263 | fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]); |
264 | fprintf_filtered (stream, "\n"); | |
c906108c SS |
265 | #endif |
266 | ||
267 | #ifdef HAVE_TERMIO | |
c2c6d25f JM |
268 | fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n", |
269 | state->termio.c_iflag, state->termio.c_oflag); | |
270 | fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", | |
271 | state->termio.c_cflag, state->termio.c_lflag, | |
272 | state->termio.c_line); | |
273 | fprintf_filtered (stream, "c_cc: "); | |
c906108c | 274 | for (i = 0; i < NCC; i += 1) |
c2c6d25f JM |
275 | fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]); |
276 | fprintf_filtered (stream, "\n"); | |
c906108c SS |
277 | #endif |
278 | ||
279 | #ifdef HAVE_SGTTY | |
c2c6d25f JM |
280 | fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n", |
281 | state->sgttyb.sg_flags); | |
c906108c | 282 | |
c2c6d25f | 283 | fprintf_filtered (stream, "tchars: "); |
c5aa993b | 284 | for (i = 0; i < (int) sizeof (struct tchars); i++) |
c2c6d25f | 285 | fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]); |
64122a8b | 286 | fprintf_filtered (stream, "\n"); |
c906108c | 287 | |
c2c6d25f | 288 | fprintf_filtered (stream, "ltchars: "); |
c5aa993b | 289 | for (i = 0; i < (int) sizeof (struct ltchars); i++) |
c2c6d25f JM |
290 | fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]); |
291 | fprintf_filtered (stream, "\n"); | |
c906108c | 292 | |
c2c6d25f | 293 | fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode); |
c906108c SS |
294 | #endif |
295 | } | |
296 | ||
c378eb4e MS |
297 | /* Wait for the output to drain away, as opposed to flushing |
298 | (discarding) it. */ | |
c906108c SS |
299 | |
300 | static int | |
819cc324 | 301 | hardwire_drain_output (struct serial *scb) |
c906108c SS |
302 | { |
303 | #ifdef HAVE_TERMIOS | |
304 | return tcdrain (scb->fd); | |
305 | #endif | |
306 | ||
307 | #ifdef HAVE_TERMIO | |
308 | return ioctl (scb->fd, TCSBRK, 1); | |
309 | #endif | |
310 | ||
311 | #ifdef HAVE_SGTTY | |
312 | /* Get the current state and then restore it using TIOCSETP, | |
313 | which should cause the output to drain and pending input | |
c378eb4e | 314 | to be discarded. */ |
c906108c SS |
315 | { |
316 | struct hardwire_ttystate state; | |
433759f7 | 317 | |
c906108c SS |
318 | if (get_tty_state (scb, &state)) |
319 | { | |
320 | return (-1); | |
321 | } | |
322 | else | |
323 | { | |
324 | return (ioctl (scb->fd, TIOCSETP, &state.sgttyb)); | |
325 | } | |
326 | } | |
c5aa993b | 327 | #endif |
c906108c SS |
328 | } |
329 | ||
330 | static int | |
819cc324 | 331 | hardwire_flush_output (struct serial *scb) |
c906108c SS |
332 | { |
333 | #ifdef HAVE_TERMIOS | |
334 | return tcflush (scb->fd, TCOFLUSH); | |
335 | #endif | |
336 | ||
337 | #ifdef HAVE_TERMIO | |
338 | return ioctl (scb->fd, TCFLSH, 1); | |
339 | #endif | |
340 | ||
341 | #ifdef HAVE_SGTTY | |
342 | /* This flushes both input and output, but we can't do better. */ | |
343 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
c5aa993b | 344 | #endif |
c906108c SS |
345 | } |
346 | ||
347 | static int | |
819cc324 | 348 | hardwire_flush_input (struct serial *scb) |
c906108c | 349 | { |
dd5da072 | 350 | ser_base_flush_input (scb); |
c906108c SS |
351 | |
352 | #ifdef HAVE_TERMIOS | |
353 | return tcflush (scb->fd, TCIFLUSH); | |
354 | #endif | |
355 | ||
356 | #ifdef HAVE_TERMIO | |
357 | return ioctl (scb->fd, TCFLSH, 0); | |
358 | #endif | |
359 | ||
360 | #ifdef HAVE_SGTTY | |
361 | /* This flushes both input and output, but we can't do better. */ | |
362 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
c5aa993b | 363 | #endif |
c906108c SS |
364 | } |
365 | ||
366 | static int | |
819cc324 | 367 | hardwire_send_break (struct serial *scb) |
c906108c SS |
368 | { |
369 | #ifdef HAVE_TERMIOS | |
370 | return tcsendbreak (scb->fd, 0); | |
371 | #endif | |
372 | ||
373 | #ifdef HAVE_TERMIO | |
374 | return ioctl (scb->fd, TCSBRK, 0); | |
375 | #endif | |
376 | ||
377 | #ifdef HAVE_SGTTY | |
378 | { | |
379 | int status; | |
c906108c SS |
380 | |
381 | status = ioctl (scb->fd, TIOCSBRK, 0); | |
382 | ||
383 | /* Can't use usleep; it doesn't exist in BSD 4.2. */ | |
dcb626be JB |
384 | /* Note that if this gdb_select() is interrupted by a signal it will not |
385 | wait the full length of time. I think that is OK. */ | |
386 | gdb_usleep (250000); | |
c906108c SS |
387 | status = ioctl (scb->fd, TIOCCBRK, 0); |
388 | return status; | |
389 | } | |
c5aa993b | 390 | #endif |
c906108c SS |
391 | } |
392 | ||
393 | static void | |
819cc324 | 394 | hardwire_raw (struct serial *scb) |
c906108c SS |
395 | { |
396 | struct hardwire_ttystate state; | |
397 | ||
c5aa993b | 398 | if (get_tty_state (scb, &state)) |
3e43a32a MS |
399 | fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", |
400 | safe_strerror (errno)); | |
c906108c SS |
401 | |
402 | #ifdef HAVE_TERMIOS | |
403 | state.termios.c_iflag = 0; | |
404 | state.termios.c_oflag = 0; | |
405 | state.termios.c_lflag = 0; | |
236af5e3 | 406 | state.termios.c_cflag &= ~CSIZE; |
c906108c | 407 | state.termios.c_cflag |= CLOCAL | CS8; |
23776285 MR |
408 | #ifdef CRTSCTS |
409 | /* h/w flow control. */ | |
410 | if (serial_hwflow) | |
411 | state.termios.c_cflag |= CRTSCTS; | |
412 | else | |
413 | state.termios.c_cflag &= ~CRTSCTS; | |
414 | #ifdef CRTS_IFLOW | |
415 | if (serial_hwflow) | |
416 | state.termios.c_cflag |= CRTS_IFLOW; | |
417 | else | |
418 | state.termios.c_cflag &= ~CRTS_IFLOW; | |
419 | #endif | |
420 | #endif | |
c906108c SS |
421 | state.termios.c_cc[VMIN] = 0; |
422 | state.termios.c_cc[VTIME] = 0; | |
423 | #endif | |
424 | ||
425 | #ifdef HAVE_TERMIO | |
426 | state.termio.c_iflag = 0; | |
427 | state.termio.c_oflag = 0; | |
428 | state.termio.c_lflag = 0; | |
236af5e3 | 429 | state.termio.c_cflag &= ~CSIZE; |
c906108c SS |
430 | state.termio.c_cflag |= CLOCAL | CS8; |
431 | state.termio.c_cc[VMIN] = 0; | |
432 | state.termio.c_cc[VTIME] = 0; | |
433 | #endif | |
434 | ||
435 | #ifdef HAVE_SGTTY | |
436 | state.sgttyb.sg_flags |= RAW | ANYP; | |
437 | state.sgttyb.sg_flags &= ~(CBREAK | ECHO); | |
438 | #endif | |
439 | ||
c906108c | 440 | if (set_tty_state (scb, &state)) |
3e43a32a MS |
441 | fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", |
442 | safe_strerror (errno)); | |
c906108c SS |
443 | } |
444 | ||
c906108c SS |
445 | #ifndef B19200 |
446 | #define B19200 EXTA | |
447 | #endif | |
448 | ||
449 | #ifndef B38400 | |
450 | #define B38400 EXTB | |
451 | #endif | |
452 | ||
453 | /* Translate baud rates from integers to damn B_codes. Unix should | |
454 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
455 | ||
456 | static struct | |
457 | { | |
458 | int rate; | |
459 | int code; | |
460 | } | |
461 | baudtab[] = | |
462 | { | |
c5aa993b JM |
463 | { |
464 | 50, B50 | |
465 | } | |
466 | , | |
467 | { | |
468 | 75, B75 | |
469 | } | |
470 | , | |
471 | { | |
472 | 110, B110 | |
473 | } | |
474 | , | |
475 | { | |
476 | 134, B134 | |
477 | } | |
478 | , | |
479 | { | |
480 | 150, B150 | |
481 | } | |
482 | , | |
483 | { | |
484 | 200, B200 | |
485 | } | |
486 | , | |
487 | { | |
488 | 300, B300 | |
489 | } | |
490 | , | |
491 | { | |
492 | 600, B600 | |
493 | } | |
494 | , | |
495 | { | |
496 | 1200, B1200 | |
497 | } | |
498 | , | |
499 | { | |
500 | 1800, B1800 | |
501 | } | |
502 | , | |
503 | { | |
504 | 2400, B2400 | |
505 | } | |
506 | , | |
507 | { | |
508 | 4800, B4800 | |
509 | } | |
510 | , | |
511 | { | |
512 | 9600, B9600 | |
513 | } | |
514 | , | |
515 | { | |
516 | 19200, B19200 | |
517 | } | |
518 | , | |
519 | { | |
520 | 38400, B38400 | |
521 | } | |
522 | , | |
c906108c | 523 | #ifdef B57600 |
c5aa993b JM |
524 | { |
525 | 57600, B57600 | |
526 | } | |
527 | , | |
c906108c SS |
528 | #endif |
529 | #ifdef B115200 | |
c5aa993b JM |
530 | { |
531 | 115200, B115200 | |
532 | } | |
533 | , | |
c906108c SS |
534 | #endif |
535 | #ifdef B230400 | |
c5aa993b JM |
536 | { |
537 | 230400, B230400 | |
538 | } | |
539 | , | |
c906108c SS |
540 | #endif |
541 | #ifdef B460800 | |
c5aa993b JM |
542 | { |
543 | 460800, B460800 | |
544 | } | |
545 | , | |
c906108c | 546 | #endif |
c5aa993b JM |
547 | { |
548 | -1, -1 | |
549 | } | |
550 | , | |
c906108c SS |
551 | }; |
552 | ||
c5aa993b | 553 | static int |
c2c6d25f | 554 | rate_to_code (int rate) |
c906108c SS |
555 | { |
556 | int i; | |
557 | ||
558 | for (i = 0; baudtab[i].rate != -1; i++) | |
08b4f080 | 559 | { |
c378eb4e | 560 | /* test for perfect macth. */ |
08b4f080 FN |
561 | if (rate == baudtab[i].rate) |
562 | return baudtab[i].code; | |
563 | else | |
564 | { | |
c378eb4e | 565 | /* check if it is in between valid values. */ |
08b4f080 FN |
566 | if (rate < baudtab[i].rate) |
567 | { | |
568 | if (i) | |
569 | { | |
3e43a32a MS |
570 | warning (_("Invalid baud rate %d. " |
571 | "Closest values are %d and %d."), | |
572 | rate, baudtab[i - 1].rate, baudtab[i].rate); | |
08b4f080 FN |
573 | } |
574 | else | |
575 | { | |
8a3fe4f8 | 576 | warning (_("Invalid baud rate %d. Minimum value is %d."), |
3e43a32a | 577 | rate, baudtab[0].rate); |
08b4f080 FN |
578 | } |
579 | return -1; | |
580 | } | |
581 | } | |
582 | } | |
583 | ||
c378eb4e | 584 | /* The requested speed was too large. */ |
8a3fe4f8 | 585 | warning (_("Invalid baud rate %d. Maximum value is %d."), |
08b4f080 | 586 | rate, baudtab[i - 1].rate); |
c906108c SS |
587 | return -1; |
588 | } | |
589 | ||
590 | static int | |
819cc324 | 591 | hardwire_setbaudrate (struct serial *scb, int rate) |
c906108c SS |
592 | { |
593 | struct hardwire_ttystate state; | |
08b4f080 FN |
594 | int baud_code = rate_to_code (rate); |
595 | ||
596 | if (baud_code < 0) | |
597 | { | |
598 | /* The baud rate was not valid. | |
c378eb4e | 599 | A warning has already been issued. */ |
08b4f080 FN |
600 | errno = EINVAL; |
601 | return -1; | |
602 | } | |
c906108c | 603 | |
c5aa993b | 604 | if (get_tty_state (scb, &state)) |
c906108c SS |
605 | return -1; |
606 | ||
607 | #ifdef HAVE_TERMIOS | |
08b4f080 FN |
608 | cfsetospeed (&state.termios, baud_code); |
609 | cfsetispeed (&state.termios, baud_code); | |
c906108c SS |
610 | #endif |
611 | ||
612 | #ifdef HAVE_TERMIO | |
613 | #ifndef CIBAUD | |
614 | #define CIBAUD CBAUD | |
615 | #endif | |
616 | ||
617 | state.termio.c_cflag &= ~(CBAUD | CIBAUD); | |
08b4f080 | 618 | state.termio.c_cflag |= baud_code; |
c906108c SS |
619 | #endif |
620 | ||
621 | #ifdef HAVE_SGTTY | |
08b4f080 FN |
622 | state.sgttyb.sg_ispeed = baud_code; |
623 | state.sgttyb.sg_ospeed = baud_code; | |
c906108c SS |
624 | #endif |
625 | ||
626 | return set_tty_state (scb, &state); | |
627 | } | |
628 | ||
629 | static int | |
819cc324 | 630 | hardwire_setstopbits (struct serial *scb, int num) |
c906108c SS |
631 | { |
632 | struct hardwire_ttystate state; | |
633 | int newbit; | |
634 | ||
c5aa993b | 635 | if (get_tty_state (scb, &state)) |
c906108c SS |
636 | return -1; |
637 | ||
638 | switch (num) | |
639 | { | |
640 | case SERIAL_1_STOPBITS: | |
641 | newbit = 0; | |
642 | break; | |
643 | case SERIAL_1_AND_A_HALF_STOPBITS: | |
644 | case SERIAL_2_STOPBITS: | |
645 | newbit = 1; | |
646 | break; | |
647 | default: | |
648 | return 1; | |
649 | } | |
650 | ||
651 | #ifdef HAVE_TERMIOS | |
652 | if (!newbit) | |
653 | state.termios.c_cflag &= ~CSTOPB; | |
654 | else | |
c5aa993b | 655 | state.termios.c_cflag |= CSTOPB; /* two bits */ |
c906108c SS |
656 | #endif |
657 | ||
658 | #ifdef HAVE_TERMIO | |
659 | if (!newbit) | |
660 | state.termio.c_cflag &= ~CSTOPB; | |
661 | else | |
c5aa993b | 662 | state.termio.c_cflag |= CSTOPB; /* two bits */ |
c906108c SS |
663 | #endif |
664 | ||
665 | #ifdef HAVE_SGTTY | |
666 | return 0; /* sgtty doesn't support this */ | |
667 | #endif | |
668 | ||
669 | return set_tty_state (scb, &state); | |
670 | } | |
671 | ||
236af5e3 YG |
672 | /* Implement the "setparity" serial_ops callback. */ |
673 | ||
674 | static int | |
675 | hardwire_setparity (struct serial *scb, int parity) | |
676 | { | |
677 | struct hardwire_ttystate state; | |
678 | int newparity = 0; | |
679 | ||
680 | if (get_tty_state (scb, &state)) | |
681 | return -1; | |
682 | ||
683 | switch (parity) | |
684 | { | |
685 | case GDBPARITY_NONE: | |
686 | newparity = 0; | |
687 | break; | |
688 | case GDBPARITY_ODD: | |
689 | newparity = PARENB | PARODD; | |
690 | break; | |
691 | case GDBPARITY_EVEN: | |
692 | newparity = PARENB; | |
693 | break; | |
694 | default: | |
695 | internal_warning (__FILE__, __LINE__, | |
8a4506c0 | 696 | "Incorrect parity value: %d", parity); |
236af5e3 YG |
697 | return -1; |
698 | } | |
699 | ||
700 | #ifdef HAVE_TERMIOS | |
701 | state.termios.c_cflag &= ~(PARENB | PARODD); | |
702 | state.termios.c_cflag |= newparity; | |
703 | #endif | |
704 | ||
705 | #ifdef HAVE_TERMIO | |
706 | state.termio.c_cflag &= ~(PARENB | PARODD); | |
707 | state.termio.c_cflag |= newparity; | |
708 | #endif | |
709 | ||
710 | #ifdef HAVE_SGTTY | |
711 | return 0; /* sgtty doesn't support this */ | |
712 | #endif | |
713 | return set_tty_state (scb, &state); | |
714 | } | |
715 | ||
716 | ||
c906108c | 717 | static void |
819cc324 | 718 | hardwire_close (struct serial *scb) |
c906108c SS |
719 | { |
720 | if (scb->fd < 0) | |
721 | return; | |
722 | ||
c5aa993b | 723 | close (scb->fd); |
c906108c SS |
724 | scb->fd = -1; |
725 | } | |
c2c6d25f | 726 | \f |
2acceee2 | 727 | \f |
433759f7 | 728 | |
12e8c7d7 TT |
729 | /* The hardwire ops. */ |
730 | ||
731 | static const struct serial_ops hardwire_ops = | |
732 | { | |
733 | "hardwire", | |
734 | hardwire_open, | |
735 | hardwire_close, | |
736 | NULL, | |
9bcbdca8 | 737 | ser_base_readchar, |
12e8c7d7 TT |
738 | ser_base_write, |
739 | hardwire_flush_output, | |
740 | hardwire_flush_input, | |
741 | hardwire_send_break, | |
742 | hardwire_raw, | |
743 | hardwire_get_tty_state, | |
744 | hardwire_copy_tty_state, | |
745 | hardwire_set_tty_state, | |
746 | hardwire_print_tty_state, | |
747 | hardwire_noflush_set_tty_state, | |
748 | hardwire_setbaudrate, | |
749 | hardwire_setstopbits, | |
236af5e3 | 750 | hardwire_setparity, |
12e8c7d7 TT |
751 | hardwire_drain_output, |
752 | ser_base_async, | |
753 | ser_unix_read_prim, | |
754 | ser_unix_write_prim | |
755 | }; | |
756 | ||
757 | void | |
758 | _initialize_ser_hardwire (void) | |
759 | { | |
760 | serial_add_interface (&hardwire_ops); | |
23776285 MR |
761 | |
762 | #ifdef HAVE_TERMIOS | |
763 | #ifdef CRTSCTS | |
764 | add_setshow_boolean_cmd ("remoteflow", no_class, | |
765 | &serial_hwflow, _("\ | |
766 | Set use of hardware flow control for remote serial I/O."), _("\ | |
767 | Show use of hardware flow control for remote serial I/O."), _("\ | |
768 | Enable or disable hardware flow control (RTS/CTS) on the serial port\n\ | |
769 | when debugging using remote targets."), | |
770 | NULL, | |
771 | show_serial_hwflow, | |
772 | &setlist, &showlist); | |
773 | #endif | |
774 | #endif | |
c906108c | 775 | } |
b4505029 MM |
776 | |
777 | int | |
778 | ser_unix_read_prim (struct serial *scb, size_t count) | |
779 | { | |
75ee5925 | 780 | return read (scb->fd, scb->buf, count); |
b4505029 MM |
781 | } |
782 | ||
783 | int | |
784 | ser_unix_write_prim (struct serial *scb, const void *buf, size_t len) | |
785 | { | |
b4505029 MM |
786 | return write (scb->fd, buf, len); |
787 | } |