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