]>
Commit | Line | Data |
---|---|---|
5d2b030a SG |
1 | /* Remote serial interface for local (hardwired) serial ports for GO32. |
2 | Copyright 1992, 1993 Free Software Foundation, Inc. | |
ae0ea72e | 3 | |
08c0d7b8 | 4 | This file is part of GDB. |
ae0ea72e | 5 | |
08c0d7b8 SC |
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. | |
ae0ea72e | 10 | |
08c0d7b8 SC |
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. | |
ae0ea72e | 15 | |
08c0d7b8 SC |
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. */ | |
ae0ea72e | 19 | |
ae0ea72e SC |
20 | #include "defs.h" |
21 | #include "serial.h" | |
5140562f | 22 | #include <sys/dos.h> |
ae0ea72e | 23 | |
aa48187b SC |
24 | #define disable() asm("cli") |
25 | #define enable() asm("sti") | |
26 | ||
be5e6fff | 27 | |
38dc5e12 | 28 | struct go32_ttystate |
08c0d7b8 SC |
29 | { |
30 | int bogus; | |
31 | }; | |
08c0d7b8 SC |
32 | typedef struct |
33 | { | |
34 | short jmp_op; | |
35 | short signature; | |
36 | short version; | |
37 | short buffer_start; | |
38 | short buffer_end; | |
39 | short getp; | |
40 | short putp; | |
41 | short iov; | |
aa48187b SC |
42 | short count; |
43 | short overflow; | |
44 | short buffer_size; | |
45 | short ovflushes; | |
08c0d7b8 SC |
46 | } |
47 | ASYNC_STRUCT; | |
452b4b00 | 48 | |
aa48187b SC |
49 | #define AOFF_JMP_OP 0 |
50 | #define AOFF_SIGNATURE 2 | |
51 | #define AOFF_VERSION 4 | |
52 | #define AOFF_BUFFER_START 6 | |
53 | #define AOFF_BUFFER_END 8 | |
54 | #define AOFF_GETP 10 | |
55 | #define AOFF_PUTP 12 | |
56 | #define AOFF_IOV 14 | |
57 | #define AOFF_COUNT 16 | |
58 | #define AOFF_OVERFLOW 18 | |
59 | #define AOFF_BUFFER_SIZE 20 | |
60 | #define AOFF_OVFLUSHES 22 | |
61 | ||
62 | ||
63 | static ASYNC_STRUCT a; /* Copy in our mem of the struct */ | |
64 | static long aindex; /* index into dos space of struct */ | |
65 | ||
38dc5e12 SG |
66 | static int go32_open PARAMS ((serial_t scb, const char *name)); |
67 | static void go32_raw PARAMS ((serial_t scb)); | |
38dc5e12 | 68 | static int go32_readchar PARAMS ((serial_t scb, int timeout)); |
38dc5e12 SG |
69 | static int go32_setbaudrate PARAMS ((serial_t scb, int rate)); |
70 | static int go32_write PARAMS ((serial_t scb, const char *str, int len)); | |
38dc5e12 | 71 | static void go32_close PARAMS ((serial_t scb)); |
452b4b00 | 72 | static serial_ttystate go32_get_tty_state PARAMS ((serial_t scb)); |
38dc5e12 | 73 | static int go32_set_tty_state PARAMS ((serial_t scb, serial_ttystate state)); |
aa48187b SC |
74 | static unsigned char aptr PARAMS ((short p)); |
75 | static unsigned long getivec PARAMS ((int which)); | |
38dc5e12 SG |
76 | static int dos_async_init PARAMS ((int port)); |
77 | static void dos_async_tx PARAMS ((const char c)); | |
38dc5e12 SG |
78 | static int dos_async_rx PARAMS (()); |
79 | static int dosasync_read PARAMS ((int fd, char *buf, int len, int timeout)); | |
452b4b00 | 80 | static int dosasync_write PARAMS ((int fd, const char *buf, int len)); |
38dc5e12 | 81 | |
ae0ea72e SC |
82 | #define SIGNATURE 0x4154 |
83 | #define VERSION 1 | |
84 | #define OFFSET 0x104 | |
85 | ||
be5e6fff SC |
86 | char packet[50]; |
87 | int packet_len; | |
88 | int packet_idx; | |
89 | ||
aa48187b SC |
90 | unsigned char bb; |
91 | unsigned short sb; | |
92 | unsigned long sl; | |
93 | ||
94 | #define SET_BYTE(x,y) { char _buf = y;dosmemput(&_buf,1, x);} | |
95 | #define SET_WORD(x,y) { short _buf = y;dosmemput(&_buf,2, x);} | |
96 | #define GET_BYTE(x) ( dosmemget((x),1,&bb), bb) | |
97 | ||
98 | ||
99 | #define GET_LONG(x) ( dosmemget((x),4,&sl), sl) | |
100 | ||
101 | static | |
be5e6fff | 102 | unsigned short |
aa48187b SC |
103 | GET_WORD (x) |
104 | { | |
105 | unsigned short sb; | |
106 | dosmemget ((x), 2, &sb); | |
107 | return sb; | |
108 | } | |
109 | ||
110 | static int iov[2]; | |
ae0ea72e | 111 | |
aa48187b SC |
112 | #define com_rb(n) iov[n] |
113 | #define com_tb(n) iov[n] | |
114 | #define com_ier(n) iov[n]+1 | |
115 | #define com_ifr(n) iov[n]+2 | |
116 | #define com_bfr(n) iov[n]+3 | |
117 | #define com_mcr(n) iov[n]+4 | |
118 | #define com_lsr(n) iov[n]+5 | |
119 | #define com_msr(n) iov[n]+6 | |
ae0ea72e | 120 | |
aa48187b | 121 | static unsigned char |
08c0d7b8 | 122 | aptr (p) |
5d2b030a | 123 | short p; |
ae0ea72e | 124 | { |
aa48187b | 125 | return GET_BYTE (aindex - OFFSET + p); |
ae0ea72e SC |
126 | } |
127 | ||
aa48187b | 128 | static unsigned long |
08c0d7b8 | 129 | getivec (int which) |
ae0ea72e | 130 | { |
aa48187b | 131 | long tryaindex; |
ae0ea72e | 132 | |
aa48187b | 133 | if (GET_WORD (which * 4) != OFFSET) |
ae0ea72e | 134 | return 0; |
b83bf6b3 | 135 | |
aa48187b SC |
136 | /* Find out where in memory this lives */ |
137 | tryaindex = GET_WORD (which * 4 + 2) * 16 + GET_WORD (which * 4); | |
ae0ea72e | 138 | |
aa48187b | 139 | if (GET_WORD (tryaindex + 2) != SIGNATURE) |
ae0ea72e | 140 | return 0; |
aa48187b | 141 | if (GET_WORD (tryaindex + 4) != VERSION) |
ae0ea72e | 142 | return 0; |
aa48187b | 143 | return tryaindex; |
ae0ea72e SC |
144 | } |
145 | ||
5d2b030a | 146 | static int |
08c0d7b8 | 147 | dos_async_init (port) |
b83bf6b3 | 148 | int port; |
ae0ea72e | 149 | { |
b83bf6b3 | 150 | switch (port) |
07861607 | 151 | { |
b83bf6b3 | 152 | case 1: |
aa48187b | 153 | aindex = getivec (12); |
b83bf6b3 SG |
154 | break; |
155 | case 2: | |
aa48187b | 156 | aindex = getivec (11); |
b83bf6b3 SG |
157 | break; |
158 | default: | |
159 | return 0; | |
07861607 SG |
160 | } |
161 | ||
aa48187b | 162 | if (!aindex) |
07861607 | 163 | { |
08c0d7b8 | 164 | error ("GDB cannot connect to asynctsr program, check that it is installed\n\ |
ae0ea72e SC |
165 | and that serial I/O is not being redirected (perhaps by NFS)\n\n\ |
166 | example configuration:\n\ | |
a037b21e SG |
167 | C> mode com%d:9600,n,8,1,p\n\ |
168 | C> asynctsr %d\n\ | |
169 | C> gdb \n", port, port); | |
07861607 SG |
170 | } |
171 | ||
aa48187b SC |
172 | iov[0] = GET_WORD (aindex + AOFF_IOV); |
173 | outportb (com_ier (0), 0x0f); | |
174 | outportb (com_bfr (0), 0x03); | |
175 | outportb (com_mcr (0), 0x0b); | |
b83bf6b3 | 176 | return 1; |
ae0ea72e SC |
177 | } |
178 | ||
5d2b030a | 179 | static void |
08c0d7b8 | 180 | dos_async_tx (c) |
5d2b030a | 181 | const char c; |
ae0ea72e | 182 | { |
aa48187b | 183 | while (~inportb (com_lsr (0)) & 0x20) |
08c0d7b8 | 184 | ; |
aa48187b | 185 | outportb (com_tb (0), c); |
ae0ea72e SC |
186 | } |
187 | ||
aa48187b SC |
188 | static int |
189 | dos_async_ready () | |
190 | { | |
191 | int ret; | |
192 | ||
be5e6fff SC |
193 | if (packet_idx < packet_len) |
194 | return 1; | |
195 | ||
aa48187b SC |
196 | disable (); |
197 | #if RDY_CNT | |
198 | ret = GET_WORD (aindex + AOFF_COUNT); | |
199 | #else | |
200 | ret = GET_WORD (aindex + AOFF_GETP) != GET_WORD (aindex + AOFF_PUTP); | |
201 | #endif | |
202 | enable (); | |
203 | return ret; | |
204 | ||
205 | ||
206 | } | |
ae0ea72e | 207 | |
5d2b030a | 208 | static int |
08c0d7b8 | 209 | dos_async_rx () |
ae0ea72e SC |
210 | { |
211 | char rv; | |
aa48187b | 212 | short idx; |
5d2b030a | 213 | |
be5e6fff | 214 | while (1) |
08c0d7b8 | 215 | { |
be5e6fff SC |
216 | if (packet_idx < packet_len) |
217 | { | |
218 | char x = packet[packet_idx++]; | |
219 | return x; | |
220 | } | |
221 | while (!dos_async_ready ()) | |
08c0d7b8 | 222 | { |
be5e6fff SC |
223 | if (kbhit ()) |
224 | { | |
225 | printf_unfiltered ("abort!\n"); | |
226 | return 0; | |
227 | } | |
08c0d7b8 | 228 | } |
be5e6fff SC |
229 | disable (); |
230 | { | |
231 | /* Sometimes we can read more than one char at a time | |
232 | from the buffer, which is good, cause it'll mean | |
233 | less time with interrupts turned off, which means | |
234 | less dropped characters */ | |
235 | ||
236 | /* We only do the simplest case here - not bothering with | |
237 | wrap around */ | |
238 | int len; | |
239 | ||
240 | int getp = GET_WORD (aindex + AOFF_GETP); | |
241 | int putp = GET_WORD (aindex + AOFF_PUTP); | |
242 | int endb = GET_WORD (aindex + AOFF_BUFFER_END); | |
243 | int startb = GET_WORD (aindex + AOFF_BUFFER_START); | |
244 | ||
245 | /* We'd like to grab to the end of the the input, | |
246 | but it may have wrapped, so max is to the end | |
247 | of the buffer */ | |
248 | ||
249 | if (putp > endb || putp < getp) | |
250 | putp = endb; | |
251 | ||
252 | /* Work out the length of the suck */ | |
253 | len = putp - getp; | |
254 | ||
255 | /* But only suck as many as we can hold in one go */ | |
256 | if (len > sizeof (packet)) | |
257 | len = sizeof (packet); | |
258 | ||
259 | dosmemget (aindex - OFFSET + getp, len, packet); | |
260 | ||
261 | packet_len = len; | |
262 | packet_idx = 0; | |
263 | ||
264 | if (getp + len >= endb) | |
265 | { | |
266 | getp = startb; | |
267 | } | |
268 | else | |
269 | { | |
270 | getp = getp + len; | |
271 | } | |
272 | ||
273 | SET_WORD (aindex + AOFF_GETP, getp); | |
274 | SET_WORD (aindex + AOFF_COUNT, GET_WORD (aindex + AOFF_COUNT) - len); | |
275 | } | |
276 | enable (); | |
08c0d7b8 | 277 | } |
ae0ea72e SC |
278 | } |
279 | ||
63eef03a | 280 | |
5d2b030a SG |
281 | static int |
282 | dosasync_read (fd, buf, len, timeout) | |
283 | int fd; | |
284 | char *buf; | |
285 | int len; | |
286 | int timeout; | |
ae0ea72e SC |
287 | { |
288 | long now, then; | |
08c0d7b8 | 289 | int i; |
be5e6fff | 290 | int its = 0; |
ae0ea72e | 291 | time (&now); |
5d2b030a SG |
292 | then = now + timeout; |
293 | ||
08c0d7b8 | 294 | for (i = 0; i < len; i++) |
ae0ea72e | 295 | { |
5d2b030a SG |
296 | if (timeout) |
297 | { | |
08c0d7b8 | 298 | while (!dos_async_ready ()) |
5d2b030a SG |
299 | { |
300 | time (&now); | |
be5e6fff | 301 | if (now >= then && timeout > 0 && its > 1000) |
08c0d7b8 | 302 | return i; |
be5e6fff | 303 | its++; |
5d2b030a SG |
304 | } |
305 | } | |
08c0d7b8 | 306 | *buf++ = dos_async_rx (); |
ae0ea72e | 307 | } |
5d2b030a | 308 | return len; |
ae0ea72e SC |
309 | } |
310 | ||
5d2b030a | 311 | static int |
08c0d7b8 | 312 | dosasync_write (fd, buf, len) |
5d2b030a SG |
313 | int fd; |
314 | const char *buf; | |
315 | int len; | |
316 | { | |
08c0d7b8 | 317 | int l; |
ae0ea72e | 318 | |
08c0d7b8 | 319 | for (l = 0; l < len; l++) |
5d2b030a | 320 | dos_async_tx (*buf++); |
ae0ea72e | 321 | |
5d2b030a | 322 | return len; |
ae0ea72e SC |
323 | } |
324 | ||
5d2b030a SG |
325 | static int |
326 | go32_open (scb, name) | |
327 | serial_t scb; | |
328 | const char *name; | |
b52373a2 | 329 | { |
b83bf6b3 SG |
330 | int port; |
331 | ||
332 | if (strncasecmp (name, "com", 3) != 0) | |
333 | { | |
334 | errno = ENOENT; | |
4febd102 | 335 | return -1; |
b83bf6b3 SG |
336 | } |
337 | ||
338 | port = name[3] - '0'; | |
339 | ||
340 | if ((port != 1) && (port != 2)) | |
341 | { | |
342 | errno = ENOENT; | |
4febd102 | 343 | return -11; |
b83bf6b3 SG |
344 | } |
345 | ||
08c0d7b8 | 346 | scb->fd = dos_async_init (port); |
07861607 | 347 | if (!scb->fd) |
4febd102 | 348 | return -1; |
ae0ea72e | 349 | |
5d2b030a | 350 | return 0; |
ae0ea72e SC |
351 | } |
352 | ||
c2e247c4 | 353 | static int |
704deef2 | 354 | go32_noop (scb) |
c2e247c4 JK |
355 | serial_t scb; |
356 | { | |
c2e247c4 JK |
357 | return 0; |
358 | } | |
359 | ||
5d2b030a SG |
360 | static void |
361 | go32_raw (scb) | |
362 | serial_t scb; | |
ae0ea72e | 363 | { |
5d2b030a | 364 | /* Always in raw mode */ |
ae0ea72e SC |
365 | } |
366 | ||
5d2b030a SG |
367 | static int |
368 | go32_readchar (scb, timeout) | |
369 | serial_t scb; | |
370 | int timeout; | |
ae0ea72e SC |
371 | { |
372 | char buf; | |
5d2b030a | 373 | |
08c0d7b8 SC |
374 | /* Shortcut for polling */ |
375 | if (timeout == 0) | |
376 | { | |
377 | if (dos_async_ready ()) | |
378 | { | |
379 | return dos_async_rx (); | |
380 | } | |
381 | return SERIAL_TIMEOUT; | |
382 | } | |
383 | ||
384 | if (dosasync_read (scb->fd, &buf, 1, timeout)) | |
5a6242dd | 385 | return buf; |
ae0ea72e | 386 | else |
4febd102 | 387 | return SERIAL_TIMEOUT; |
ae0ea72e SC |
388 | } |
389 | ||
38dc5e12 SG |
390 | /* go32_{get set}_tty_state() are both dummys to fill out the function |
391 | vector. Someday, they may do something real... */ | |
392 | ||
393 | static serial_ttystate | |
08c0d7b8 | 394 | go32_get_tty_state (scb) |
38dc5e12 SG |
395 | serial_t scb; |
396 | { | |
397 | struct go32_ttystate *state; | |
398 | ||
08c0d7b8 | 399 | state = (struct go32_ttystate *) xmalloc (sizeof *state); |
38dc5e12 | 400 | |
08c0d7b8 | 401 | return (serial_ttystate) state; |
38dc5e12 SG |
402 | } |
403 | ||
404 | static int | |
08c0d7b8 | 405 | go32_set_tty_state (scb, ttystate) |
38dc5e12 SG |
406 | serial_t scb; |
407 | serial_ttystate ttystate; | |
408 | { | |
38dc5e12 SG |
409 | return 0; |
410 | } | |
411 | ||
c2e247c4 JK |
412 | static int |
413 | go32_noflush_set_tty_state (scb, new_ttystate, old_ttystate) | |
414 | serial_t scb; | |
415 | serial_ttystate new_ttystate; | |
416 | serial_ttystate old_ttystate; | |
417 | { | |
418 | return 0; | |
419 | } | |
420 | ||
421 | static void | |
422 | go32_print_tty_state (scb, ttystate) | |
423 | serial_t scb; | |
424 | serial_ttystate ttystate; | |
425 | { | |
426 | /* Nothing to print. */ | |
427 | return; | |
428 | } | |
429 | ||
5d2b030a SG |
430 | static int |
431 | go32_setbaudrate (scb, rate) | |
432 | serial_t scb; | |
433 | int rate; | |
ae0ea72e SC |
434 | { |
435 | return 0; | |
436 | } | |
437 | ||
5d2b030a SG |
438 | static int |
439 | go32_write (scb, str, len) | |
440 | serial_t scb; | |
441 | const char *str; | |
442 | int len; | |
443 | { | |
08c0d7b8 | 444 | dosasync_write (scb->fd, str, len); |
4febd102 SG |
445 | |
446 | return 0; | |
5d2b030a SG |
447 | } |
448 | ||
449 | static void | |
452b4b00 SG |
450 | go32_close (scb) |
451 | serial_t scb; | |
ae0ea72e | 452 | { |
ae0ea72e SC |
453 | } |
454 | ||
5d2b030a SG |
455 | static struct serial_ops go32_ops = |
456 | { | |
457 | "hardwire", | |
458 | 0, | |
459 | go32_open, | |
460 | go32_close, | |
461 | go32_readchar, | |
462 | go32_write, | |
08c0d7b8 SC |
463 | go32_noop, /* flush output */ |
464 | go32_noop, /* flush input */ | |
465 | go32_noop, /* send break -- currently used only for nindy */ | |
5d2b030a | 466 | go32_raw, |
38dc5e12 SG |
467 | go32_get_tty_state, |
468 | go32_set_tty_state, | |
c2e247c4 JK |
469 | go32_print_tty_state, |
470 | go32_noflush_set_tty_state, | |
471 | go32_setbaudrate, | |
5d2b030a SG |
472 | }; |
473 | ||
976bb0be | 474 | void |
5d2b030a | 475 | _initialize_ser_go32 () |
ae0ea72e | 476 | { |
5d2b030a | 477 | serial_add_interface (&go32_ops); |
ae0ea72e | 478 | } |