1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2 Copyright 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
23 #include <sys/types.h>
25 #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
33 struct hardwire_ttystate
35 struct termios termios
;
42 struct hardwire_ttystate
49 /* Needed for the code which uses select(). We would include <sys/select.h>
50 too if it existed on all systems. */
55 struct hardwire_ttystate
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
));
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
));
75 /* Open up a real live device for serial I/O */
78 hardwire_open(scb
, name
)
82 scb
->fd
= open (name
, O_RDWR
);
90 get_tty_state(scb
, state
)
92 struct hardwire_ttystate
*state
;
95 return tcgetattr(scb
->fd
, &state
->termios
);
99 return ioctl (scb
->fd
, TCGETA
, &state
->termio
);
103 return ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
);
108 set_tty_state(scb
, state
)
110 struct hardwire_ttystate
*state
;
115 return tcsetattr(scb
->fd
, TCSANOW
, &state
->termios
);
119 return ioctl (scb
->fd
, TCSETA
, &state
->termio
);
123 return ioctl (scb
->fd
, TIOCSETP
, &state
->sgttyb
);
127 static serial_ttystate
128 hardwire_get_tty_state(scb
)
131 struct hardwire_ttystate
*state
;
133 state
= (struct hardwire_ttystate
*)xmalloc(sizeof *state
);
135 if (get_tty_state(scb
, state
))
138 return (serial_ttystate
)state
;
142 hardwire_set_tty_state(scb
, ttystate
)
144 serial_ttystate ttystate
;
146 struct hardwire_ttystate
*state
;
148 state
= (struct hardwire_ttystate
*)ttystate
;
150 return set_tty_state(scb
, state
);
157 struct hardwire_ttystate state
;
159 if (get_tty_state(scb
, &state
))
160 fprintf(stderr
, "get_tty_state failed: %s\n", safe_strerror(errno
));
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;
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;
183 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
184 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
187 scb
->current_timeout
= 0;
189 if (set_tty_state (scb
, &state
))
190 fprintf(stderr
, "set_tty_state failed: %s\n", safe_strerror(errno
));
193 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
194 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
196 For termio{s}, we actually just setup VTIME if necessary, and let the
197 timeout occur in the read() in hardwire_read().
201 wait_for(scb
, timeout
)
216 FD_SET(scb
->fd
, &readfds
);
221 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, &tv
);
223 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, 0);
227 return SERIAL_TIMEOUT
;
228 else if (errno
== EINTR
)
231 return SERIAL_ERROR
; /* Got an error from select or poll */
236 #endif /* HAVE_SGTTY */
238 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
239 if (timeout
== scb
->current_timeout
)
243 struct hardwire_ttystate state
;
245 if (get_tty_state(scb
, &state
))
246 fprintf(stderr
, "get_tty_state failed: %s\n", safe_strerror(errno
));
249 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
253 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
256 scb
->current_timeout
= timeout
;
258 if (set_tty_state (scb
, &state
))
259 fprintf(stderr
, "set_tty_state failed: %s\n", safe_strerror(errno
));
263 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
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). */
272 hardwire_readchar(scb
, timeout
)
278 if (scb
->bufcnt
-- > 0)
281 status
= wait_for(scb
, timeout
);
286 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
288 if (scb
->bufcnt
<= 0)
289 if (scb
->bufcnt
== 0)
290 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
291 distinguish between EOF & timeouts
294 return SERIAL_ERROR
; /* Got an error from read */
297 scb
->bufp
= scb
->buf
;
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. */
343 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
344 if (rate
== baudtab
[i
].rate
)
345 return baudtab
[i
].code
;
351 hardwire_setbaudrate(scb
, rate
)
355 struct hardwire_ttystate state
;
357 if (get_tty_state(scb
, &state
))
361 cfsetospeed (&state
.termios
, rate_to_code (rate
));
362 cfsetispeed (&state
.termios
, rate_to_code (rate
));
370 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
371 state
.termio
.c_cflag
|= rate_to_code (rate
);
375 state
.sgttyb
.sg_ispeed
= rate_to_code (rate
);
376 state
.sgttyb
.sg_ospeed
= rate_to_code (rate
);
379 return set_tty_state (scb
, &state
);
383 hardwire_write(scb
, str
, len
)
392 cc
= write(scb
->fd
, str
, len
);
413 static struct serial_ops hardwire_ops
=
422 hardwire_get_tty_state
,
423 hardwire_set_tty_state
,
424 hardwire_setbaudrate
,
428 _initialize_ser_hardwire ()
430 serial_add_interface (&hardwire_ops
);