Commit | Line | Data |
---|---|---|
4e772f44 SG |
1 | /* Serial interface for local (hardwired) serial ports on Un*x like systems |
2 | Copyright 1992, 1993 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "serial.h" | |
22 | #include <fcntl.h> | |
23 | #include <sys/types.h> | |
4e772f44 SG |
24 | |
25 | #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY) | |
26 | #define HAVE_SGTTY | |
27 | #endif | |
28 | ||
29 | #ifdef HAVE_TERMIOS | |
30 | #include <termios.h> | |
31 | #include <unistd.h> | |
38dc5e12 SG |
32 | |
33 | struct hardwire_ttystate | |
34 | { | |
35 | struct termios termios; | |
36 | }; | |
4e772f44 | 37 | #endif |
38dc5e12 | 38 | |
4e772f44 | 39 | #ifdef HAVE_TERMIO |
ebd99d54 | 40 | #include <termio.h> |
38dc5e12 SG |
41 | |
42 | struct hardwire_ttystate | |
43 | { | |
44 | struct termio termio; | |
45 | }; | |
4e772f44 | 46 | #endif |
38dc5e12 | 47 | |
4e772f44 | 48 | #ifdef HAVE_SGTTY |
68d2db62 JK |
49 | /* Needed for the code which uses select(). We would include <sys/select.h> |
50 | too if it existed on all systems. */ | |
51 | #include <sys/time.h> | |
52 | ||
4e772f44 | 53 | #include <sgtty.h> |
38dc5e12 SG |
54 | |
55 | struct hardwire_ttystate | |
56 | { | |
57 | struct sgttyb sgttyb; | |
58 | }; | |
4e772f44 SG |
59 | #endif |
60 | ||
9775789d SG |
61 | static int hardwire_open PARAMS ((serial_t scb, const char *name)); |
62 | static void hardwire_raw PARAMS ((serial_t scb)); | |
63 | static int wait_for PARAMS ((serial_t scb, int timeout)); | |
64 | static int hardwire_readchar PARAMS ((serial_t scb, int timeout)); | |
65 | static int rate_to_code PARAMS ((int rate)); | |
66 | static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate)); | |
67 | static int hardwire_write PARAMS ((serial_t scb, const char *str, int len)); | |
68 | static void hardwire_restore PARAMS ((serial_t scb)); | |
69 | static void hardwire_close PARAMS ((serial_t scb)); | |
38dc5e12 SG |
70 | static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state)); |
71 | static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state)); | |
72 | static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb)); | |
73 | static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state)); | |
9775789d | 74 | |
4e772f44 SG |
75 | /* Open up a real live device for serial I/O */ |
76 | ||
77 | static int | |
78 | hardwire_open(scb, name) | |
79 | serial_t scb; | |
80 | const char *name; | |
81 | { | |
82 | scb->fd = open (name, O_RDWR); | |
83 | if (scb->fd < 0) | |
4febd102 | 84 | return -1; |
4e772f44 SG |
85 | |
86 | return 0; | |
87 | } | |
88 | ||
38dc5e12 SG |
89 | static int |
90 | get_tty_state(scb, state) | |
4e772f44 | 91 | serial_t scb; |
38dc5e12 | 92 | struct hardwire_ttystate *state; |
4e772f44 SG |
93 | { |
94 | #ifdef HAVE_TERMIOS | |
38dc5e12 SG |
95 | return tcgetattr(scb->fd, &state->termios); |
96 | #endif | |
4e772f44 | 97 | |
38dc5e12 SG |
98 | #ifdef HAVE_TERMIO |
99 | return ioctl (scb->fd, TCGETA, &state->termio); | |
100 | #endif | |
4e772f44 | 101 | |
38dc5e12 SG |
102 | #ifdef HAVE_SGTTY |
103 | return ioctl (scb->fd, TIOCGETP, &state->sgttyb); | |
104 | #endif | |
105 | } | |
4e772f44 | 106 | |
38dc5e12 SG |
107 | static int |
108 | set_tty_state(scb, state) | |
109 | serial_t scb; | |
110 | struct hardwire_ttystate *state; | |
111 | { | |
112 | int err; | |
113 | ||
114 | #ifdef HAVE_TERMIOS | |
115 | return tcsetattr(scb->fd, TCSANOW, &state->termios); | |
4e772f44 SG |
116 | #endif |
117 | ||
118 | #ifdef HAVE_TERMIO | |
38dc5e12 SG |
119 | return ioctl (scb->fd, TCSETA, &state->termio); |
120 | #endif | |
4e772f44 | 121 | |
38dc5e12 SG |
122 | #ifdef HAVE_SGTTY |
123 | return ioctl (scb->fd, TIOCSETP, &state->sgttyb); | |
124 | #endif | |
125 | } | |
4e772f44 | 126 | |
38dc5e12 SG |
127 | static serial_ttystate |
128 | hardwire_get_tty_state(scb) | |
129 | serial_t scb; | |
130 | { | |
131 | struct hardwire_ttystate *state; | |
4e772f44 | 132 | |
38dc5e12 | 133 | state = (struct hardwire_ttystate *)xmalloc(sizeof *state); |
4e772f44 | 134 | |
38dc5e12 SG |
135 | if (get_tty_state(scb, state)) |
136 | return NULL; | |
4e772f44 | 137 | |
38dc5e12 SG |
138 | return (serial_ttystate)state; |
139 | } | |
4e772f44 | 140 | |
38dc5e12 SG |
141 | static int |
142 | hardwire_set_tty_state(scb, ttystate) | |
143 | serial_t scb; | |
144 | serial_ttystate ttystate; | |
145 | { | |
146 | struct hardwire_ttystate *state; | |
4e772f44 | 147 | |
38dc5e12 SG |
148 | state = (struct hardwire_ttystate *)ttystate; |
149 | ||
150 | return set_tty_state(scb, state); | |
151 | } | |
152 | ||
153 | static void | |
154 | hardwire_raw(scb) | |
155 | serial_t scb; | |
156 | { | |
157 | struct hardwire_ttystate state; | |
158 | ||
159 | if (get_tty_state(scb, &state)) | |
160 | fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno)); | |
161 | ||
162 | #ifdef HAVE_TERMIOS | |
163 | state.termios.c_iflag = 0; | |
164 | state.termios.c_oflag = 0; | |
165 | state.termios.c_lflag = 0; | |
166 | state.termios.c_cflag &= ~(CSIZE|PARENB); | |
167 | state.termios.c_cflag |= CS8; | |
168 | state.termios.c_cc[VMIN] = 0; | |
169 | state.termios.c_cc[VTIME] = 0; | |
170 | #endif | |
171 | ||
172 | #ifdef HAVE_TERMIO | |
173 | state.termio.c_iflag = 0; | |
174 | state.termio.c_oflag = 0; | |
175 | state.termio.c_lflag = 0; | |
176 | state.termio.c_cflag &= ~(CSIZE|PARENB); | |
177 | state.termio.c_cflag |= CS8; | |
178 | state.termio.c_cc[VMIN] = 0; | |
179 | state.termio.c_cc[VTIME] = 0; | |
180 | #endif | |
181 | ||
182 | #ifdef HAVE_SGTTY | |
183 | state.sgttyb.sg_flags |= RAW | ANYP; | |
184 | state.sgttyb.sg_flags &= ~(CBREAK | ECHO); | |
4e772f44 | 185 | #endif |
9e15da4a SG |
186 | |
187 | scb->current_timeout = 0; | |
38dc5e12 SG |
188 | |
189 | if (set_tty_state (scb, &state)) | |
190 | fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno)); | |
4e772f44 SG |
191 | } |
192 | ||
9e15da4a SG |
193 | /* Wait for input on scb, with timeout seconds. Returns 0 on success, |
194 | otherwise SERIAL_TIMEOUT or SERIAL_ERROR. | |
195 | ||
196 | For termio{s}, we actually just setup VTIME if necessary, and let the | |
197 | timeout occur in the read() in hardwire_read(). | |
198 | */ | |
4e772f44 SG |
199 | |
200 | static int | |
9775789d | 201 | wait_for(scb, timeout) |
4e772f44 SG |
202 | serial_t scb; |
203 | int timeout; | |
204 | { | |
9775789d | 205 | int numfds; |
4e772f44 | 206 | |
9775789d SG |
207 | #ifdef HAVE_SGTTY |
208 | struct timeval tv; | |
209 | fd_set readfds; | |
eca29634 | 210 | |
9775789d | 211 | FD_ZERO (&readfds); |
eca29634 | 212 | |
9775789d SG |
213 | tv.tv_sec = timeout; |
214 | tv.tv_usec = 0; | |
eca29634 | 215 | |
9775789d | 216 | FD_SET(scb->fd, &readfds); |
eca29634 | 217 | |
a037b21e SG |
218 | while (1) |
219 | { | |
220 | if (timeout >= 0) | |
221 | numfds = select(scb->fd+1, &readfds, 0, 0, &tv); | |
222 | else | |
223 | numfds = select(scb->fd+1, &readfds, 0, 0, 0); | |
224 | ||
225 | if (numfds <= 0) | |
226 | if (numfds == 0) | |
227 | return SERIAL_TIMEOUT; | |
228 | else if (errno == EINTR) | |
229 | continue; | |
230 | else | |
231 | return SERIAL_ERROR; /* Got an error from select or poll */ | |
232 | ||
233 | return 0; | |
234 | } | |
9e15da4a | 235 | |
9775789d | 236 | #endif /* HAVE_SGTTY */ |
4e772f44 | 237 | |
9775789d | 238 | #if defined HAVE_TERMIO || defined HAVE_TERMIOS |
9e15da4a SG |
239 | if (timeout == scb->current_timeout) |
240 | return 0; | |
4e772f44 | 241 | |
9e15da4a | 242 | { |
38dc5e12 | 243 | struct hardwire_ttystate state; |
eca29634 | 244 | |
38dc5e12 SG |
245 | if (get_tty_state(scb, &state)) |
246 | fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno)); | |
eca29634 | 247 | |
38dc5e12 SG |
248 | #ifdef HAVE_TERMIOS |
249 | state.termios.c_cc[VTIME] = timeout * 10; | |
250 | #endif | |
9775789d | 251 | |
9e15da4a | 252 | #ifdef HAVE_TERMIO |
38dc5e12 SG |
253 | state.termio.c_cc[VTIME] = timeout * 10; |
254 | #endif | |
9e15da4a | 255 | |
38dc5e12 | 256 | scb->current_timeout = timeout; |
9e15da4a | 257 | |
38dc5e12 SG |
258 | if (set_tty_state (scb, &state)) |
259 | fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno)); | |
9e15da4a | 260 | |
9e15da4a SG |
261 | return 0; |
262 | } | |
263 | #endif /* HAVE_TERMIO || HAVE_TERMIOS */ | |
9775789d SG |
264 | } |
265 | ||
266 | /* Read a character with user-specified timeout. TIMEOUT is number of seconds | |
267 | to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns | |
268 | char if successful. Returns -2 if timeout expired, EOF if line dropped | |
269 | dead, or -3 for any other error (see errno in that case). */ | |
270 | ||
271 | static int | |
272 | hardwire_readchar(scb, timeout) | |
273 | serial_t scb; | |
274 | int timeout; | |
275 | { | |
276 | int status; | |
277 | ||
278 | if (scb->bufcnt-- > 0) | |
279 | return *scb->bufp++; | |
280 | ||
281 | status = wait_for(scb, timeout); | |
282 | ||
283 | if (status < 0) | |
284 | return status; | |
4e772f44 | 285 | |
9775789d | 286 | scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ); |
4e772f44 SG |
287 | |
288 | if (scb->bufcnt <= 0) | |
289 | if (scb->bufcnt == 0) | |
9e15da4a SG |
290 | return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to |
291 | distinguish between EOF & timeouts | |
292 | someday] */ | |
4e772f44 | 293 | else |
4febd102 | 294 | return SERIAL_ERROR; /* Got an error from read */ |
4e772f44 SG |
295 | |
296 | scb->bufcnt--; | |
297 | scb->bufp = scb->buf; | |
298 | return *scb->bufp++; | |
299 | } | |
300 | ||
301 | #ifndef B19200 | |
302 | #define B19200 EXTA | |
303 | #endif | |
304 | ||
305 | #ifndef B38400 | |
306 | #define B38400 EXTB | |
307 | #endif | |
308 | ||
309 | /* Translate baud rates from integers to damn B_codes. Unix should | |
310 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
311 | ||
312 | static struct | |
313 | { | |
314 | int rate; | |
315 | int code; | |
316 | } | |
317 | baudtab[] = | |
318 | { | |
319 | {50, B50}, | |
320 | {75, B75}, | |
321 | {110, B110}, | |
322 | {134, B134}, | |
323 | {150, B150}, | |
324 | {200, B200}, | |
325 | {300, B300}, | |
326 | {600, B600}, | |
327 | {1200, B1200}, | |
328 | {1800, B1800}, | |
329 | {2400, B2400}, | |
330 | {4800, B4800}, | |
331 | {9600, B9600}, | |
332 | {19200, B19200}, | |
333 | {38400, B38400}, | |
334 | {-1, -1}, | |
335 | }; | |
336 | ||
337 | static int | |
338 | rate_to_code(rate) | |
339 | int rate; | |
340 | { | |
341 | int i; | |
342 | ||
343 | for (i = 0; baudtab[i].rate != -1; i++) | |
344 | if (rate == baudtab[i].rate) | |
345 | return baudtab[i].code; | |
346 | ||
347 | return -1; | |
348 | } | |
349 | ||
350 | static int | |
351 | hardwire_setbaudrate(scb, rate) | |
352 | serial_t scb; | |
353 | int rate; | |
354 | { | |
38dc5e12 | 355 | struct hardwire_ttystate state; |
4e772f44 | 356 | |
38dc5e12 | 357 | if (get_tty_state(scb, &state)) |
4febd102 | 358 | return -1; |
4e772f44 | 359 | |
38dc5e12 SG |
360 | #ifdef HAVE_TERMIOS |
361 | cfsetospeed (&state.termios, rate_to_code (rate)); | |
362 | cfsetispeed (&state.termios, rate_to_code (rate)); | |
4e772f44 SG |
363 | #endif |
364 | ||
365 | #ifdef HAVE_TERMIO | |
4e772f44 SG |
366 | #ifndef CIBAUD |
367 | #define CIBAUD CBAUD | |
368 | #endif | |
369 | ||
38dc5e12 SG |
370 | state.termio.c_cflag &= ~(CBAUD | CIBAUD); |
371 | state.termio.c_cflag |= rate_to_code (rate); | |
4e772f44 SG |
372 | #endif |
373 | ||
374 | #ifdef HAVE_SGTTY | |
38dc5e12 SG |
375 | state.sgttyb.sg_ispeed = rate_to_code (rate); |
376 | state.sgttyb.sg_ospeed = rate_to_code (rate); | |
4e772f44 | 377 | #endif |
38dc5e12 SG |
378 | |
379 | return set_tty_state (scb, &state); | |
4e772f44 SG |
380 | } |
381 | ||
382 | static int | |
383 | hardwire_write(scb, str, len) | |
384 | serial_t scb; | |
385 | const char *str; | |
386 | int len; | |
387 | { | |
388 | int cc; | |
389 | ||
390 | while (len > 0) | |
391 | { | |
392 | cc = write(scb->fd, str, len); | |
393 | ||
394 | if (cc < 0) | |
395 | return 1; | |
396 | len -= cc; | |
397 | str += cc; | |
398 | } | |
399 | return 0; | |
400 | } | |
401 | ||
4e772f44 SG |
402 | static void |
403 | hardwire_close(scb) | |
404 | serial_t scb; | |
405 | { | |
406 | if (scb->fd < 0) | |
407 | return; | |
408 | ||
4e772f44 SG |
409 | close(scb->fd); |
410 | scb->fd = -1; | |
411 | } | |
412 | ||
413 | static struct serial_ops hardwire_ops = | |
414 | { | |
415 | "hardwire", | |
416 | 0, | |
417 | hardwire_open, | |
418 | hardwire_close, | |
419 | hardwire_readchar, | |
420 | hardwire_write, | |
421 | hardwire_raw, | |
38dc5e12 SG |
422 | hardwire_get_tty_state, |
423 | hardwire_set_tty_state, | |
424 | hardwire_setbaudrate, | |
4e772f44 SG |
425 | }; |
426 | ||
9775789d | 427 | void |
4e772f44 SG |
428 | _initialize_ser_hardwire () |
429 | { | |
430 | serial_add_interface (&hardwire_ops); | |
431 | } |