]>
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> | |
24 | #include <sys/time.h> | |
25 | ||
26 | #if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY) | |
27 | #define HAVE_SGTTY | |
28 | #endif | |
29 | ||
30 | #ifdef HAVE_TERMIOS | |
31 | #include <termios.h> | |
32 | #include <unistd.h> | |
33 | #endif | |
34 | #ifdef HAVE_TERMIO | |
35 | #include <sys/termio.h> | |
36 | #endif | |
37 | #ifdef HAVE_SGTTY | |
38 | #include <sgtty.h> | |
39 | #endif | |
40 | ||
41 | /* Open up a real live device for serial I/O */ | |
42 | ||
43 | static int | |
44 | hardwire_open(scb, name) | |
45 | serial_t scb; | |
46 | const char *name; | |
47 | { | |
48 | scb->fd = open (name, O_RDWR); | |
49 | if (scb->fd < 0) | |
50 | return errno; | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | static void | |
56 | hardwire_raw(scb) | |
57 | serial_t scb; | |
58 | { | |
59 | #ifdef HAVE_TERMIOS | |
60 | struct termios termios; | |
61 | ||
62 | if (tcgetattr(scb->fd, &termios)) | |
63 | { | |
64 | fprintf(stderr, "tcgetattr failed: %s\n", safe_strerror(errno)); | |
65 | } | |
66 | ||
67 | termios.c_iflag = 0; | |
68 | termios.c_oflag = 0; | |
69 | termios.c_lflag = 0; | |
70 | termios.c_cflag &= ~(CSIZE|PARENB); | |
71 | termios.c_cflag |= CS8; | |
72 | termios.c_cc[VMIN] = 0; | |
73 | termios.c_cc[VTIME] = 0; | |
74 | ||
75 | if (tcsetattr(scb->fd, TCSANOW, &termios)) | |
76 | { | |
77 | fprintf(stderr, "tcsetattr failed: %s\n", safe_strerror(errno)); | |
78 | } | |
79 | #endif | |
80 | ||
81 | #ifdef HAVE_TERMIO | |
82 | struct termio termio; | |
83 | ||
84 | if (ioctl (scb->fd, TCGETA, &termio)) | |
85 | { | |
86 | fprintf(stderr, "TCGETA failed: %s\n", safe_strerror(errno)); | |
87 | } | |
88 | ||
89 | termio.c_iflag = 0; | |
90 | termio.c_oflag = 0; | |
91 | termio.c_lflag = 0; | |
92 | termio.c_cflag &= ~(CSIZE|PARENB); | |
93 | termio.c_cflag |= CS8; | |
94 | termio.c_cc[VMIN] = 0; | |
95 | termio.c_cc[VTIME] = 0; | |
96 | ||
97 | if (ioctl (scb->fd, TCSETA, &termio)) | |
98 | { | |
99 | fprintf(stderr, "TCSETA failed: %s\n", safe_strerror(errno)); | |
100 | } | |
101 | #endif | |
102 | ||
103 | #ifdef HAVE_SGTTY | |
104 | struct sgttyb sgttyb; | |
105 | ||
106 | if (ioctl (scb->fd, TIOCGETP, &sgttyb)) | |
107 | fprintf(stderr, "TIOCGETP failed: %s\n", safe_strerror(errno)); | |
108 | ||
109 | sgttyb.sg_flags |= RAW | ANYP; | |
110 | sgttyb.sg_flags &= ~(CBREAK | ECHO); | |
111 | ||
112 | if (ioctl (scb->fd, TIOCSETP, &sgttyb)) | |
113 | fprintf(stderr, "TIOCSETP failed: %s\n", safe_strerror(errno)); | |
114 | #endif | |
115 | } | |
116 | ||
117 | /* Read a character with user-specified timeout. TIMEOUT is number of seconds | |
118 | to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns | |
119 | char if successful. Returns -2 if timeout expired, EOF if line dropped | |
120 | dead, or -3 for any other error (see errno in that case). */ | |
121 | ||
122 | static int | |
123 | hardwire_readchar(scb, timeout) | |
124 | serial_t scb; | |
125 | int timeout; | |
126 | { | |
127 | int numfds; | |
128 | struct timeval tv; | |
129 | fd_set readfds; | |
130 | ||
131 | if (scb->bufcnt-- > 0) | |
132 | return *scb->bufp++; | |
133 | ||
134 | FD_ZERO (&readfds); | |
135 | ||
136 | tv.tv_sec = timeout; | |
137 | tv.tv_usec = 0; | |
138 | ||
139 | FD_SET(scb->fd, &readfds); | |
140 | ||
141 | if (timeout >= 0) | |
142 | numfds = select(scb->fd+1, &readfds, 0, 0, &tv); | |
143 | else | |
144 | numfds = select(scb->fd+1, &readfds, 0, 0, 0); | |
145 | ||
146 | if (numfds <= 0) | |
147 | if (numfds == 0) | |
148 | return -2; /* Timeout */ | |
149 | else | |
150 | return -3; /* Got an error from select */ | |
151 | ||
152 | scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ); | |
153 | ||
154 | if (scb->bufcnt <= 0) | |
155 | if (scb->bufcnt == 0) | |
156 | return EOF; /* 0 chars means end of file */ | |
157 | else | |
158 | return -3; /* Got an error from read */ | |
159 | ||
160 | scb->bufcnt--; | |
161 | scb->bufp = scb->buf; | |
162 | return *scb->bufp++; | |
163 | } | |
164 | ||
165 | #ifndef B19200 | |
166 | #define B19200 EXTA | |
167 | #endif | |
168 | ||
169 | #ifndef B38400 | |
170 | #define B38400 EXTB | |
171 | #endif | |
172 | ||
173 | /* Translate baud rates from integers to damn B_codes. Unix should | |
174 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
175 | ||
176 | static struct | |
177 | { | |
178 | int rate; | |
179 | int code; | |
180 | } | |
181 | baudtab[] = | |
182 | { | |
183 | {50, B50}, | |
184 | {75, B75}, | |
185 | {110, B110}, | |
186 | {134, B134}, | |
187 | {150, B150}, | |
188 | {200, B200}, | |
189 | {300, B300}, | |
190 | {600, B600}, | |
191 | {1200, B1200}, | |
192 | {1800, B1800}, | |
193 | {2400, B2400}, | |
194 | {4800, B4800}, | |
195 | {9600, B9600}, | |
196 | {19200, B19200}, | |
197 | {38400, B38400}, | |
198 | {-1, -1}, | |
199 | }; | |
200 | ||
201 | static int | |
202 | rate_to_code(rate) | |
203 | int rate; | |
204 | { | |
205 | int i; | |
206 | ||
207 | for (i = 0; baudtab[i].rate != -1; i++) | |
208 | if (rate == baudtab[i].rate) | |
209 | return baudtab[i].code; | |
210 | ||
211 | return -1; | |
212 | } | |
213 | ||
214 | static int | |
215 | hardwire_setbaudrate(scb, rate) | |
216 | serial_t scb; | |
217 | int rate; | |
218 | { | |
219 | #ifdef HAVE_TERMIOS | |
220 | struct termios termios; | |
221 | ||
222 | if (tcgetattr (scb->fd, &termios)) | |
223 | error("hardwire_setbaudrate: tcgetattr failed: %s\n", safe_strerror(errno)); | |
224 | ||
225 | cfsetospeed (&termios, rate_to_code (rate)); | |
226 | cfsetispeed (&termios, rate_to_code (rate)); | |
227 | ||
228 | if (tcsetattr (scb->fd, TCSANOW, &termios)) | |
229 | error ("hardwire_setbaudrate: tcsetattr failed: %s\n", safe_strerror(errno)); | |
230 | ||
231 | return 1; | |
232 | #endif | |
233 | ||
234 | #ifdef HAVE_TERMIO | |
235 | struct termio termio; | |
236 | ||
237 | if (ioctl (scb->fd, TCGETA, &termio)) | |
238 | { | |
239 | fprintf(stderr, "TCGETA failed: %s\n", safe_strerror(errno)); | |
240 | } | |
241 | ||
242 | #ifndef CIBAUD | |
243 | #define CIBAUD CBAUD | |
244 | #endif | |
245 | ||
246 | termio.c_cflag &= ~(CBAUD | CIBAUD); | |
247 | termio.c_cflag |= rate_to_code (rate); | |
248 | ||
249 | if (ioctl (scb->fd, TCSETA, &termio)) | |
250 | { | |
251 | fprintf(stderr, "TCSETA failed: %s\n", safe_strerror(errno)); | |
252 | } | |
253 | #endif | |
254 | ||
255 | #ifdef HAVE_SGTTY | |
256 | struct sgttyb sgttyb; | |
257 | ||
258 | if (ioctl (scb->fd, TIOCGETP, &sgttyb)) | |
259 | fprintf (stderr, "TIOCGETP failed: %s\n", safe_strerror (errno)); | |
260 | ||
261 | sgttyb.sg_ispeed = rate_to_code (rate); | |
262 | sgttyb.sg_ospeed = rate_to_code (rate); | |
263 | ||
264 | if (ioctl (scb->fd, TIOCSETP, &sgttyb)) | |
265 | fprintf (stderr, "TIOCSETP failed: %s\n", safe_strerror (errno)); | |
266 | #endif | |
267 | } | |
268 | ||
269 | static int | |
270 | hardwire_write(scb, str, len) | |
271 | serial_t scb; | |
272 | const char *str; | |
273 | int len; | |
274 | { | |
275 | int cc; | |
276 | ||
277 | while (len > 0) | |
278 | { | |
279 | cc = write(scb->fd, str, len); | |
280 | ||
281 | if (cc < 0) | |
282 | return 1; | |
283 | len -= cc; | |
284 | str += cc; | |
285 | } | |
286 | return 0; | |
287 | } | |
288 | ||
289 | static void | |
290 | hardwire_restore(scb) | |
291 | serial_t scb; | |
292 | { | |
293 | } | |
294 | ||
295 | static void | |
296 | hardwire_close(scb) | |
297 | serial_t scb; | |
298 | { | |
299 | if (scb->fd < 0) | |
300 | return; | |
301 | ||
302 | SERIAL_RESTORE(scb); | |
303 | ||
304 | close(scb->fd); | |
305 | scb->fd = -1; | |
306 | } | |
307 | ||
308 | static struct serial_ops hardwire_ops = | |
309 | { | |
310 | "hardwire", | |
311 | 0, | |
312 | hardwire_open, | |
313 | hardwire_close, | |
314 | hardwire_readchar, | |
315 | hardwire_write, | |
316 | hardwire_raw, | |
317 | hardwire_restore, | |
318 | hardwire_setbaudrate | |
319 | }; | |
320 | ||
321 | _initialize_ser_hardwire () | |
322 | { | |
323 | serial_add_interface (&hardwire_ops); | |
324 | } |