* remote.c (remote_open): Use SERIAL_OPEN instead of serial_open.
[external/binutils.git] / gdb / ser-go32.c
1 /* Remote serial interface for local (hardwired) serial ports for GO32.
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 <sys/dos.h>
23
24 #define SIGNATURE 0x4154
25 #define VERSION 1
26 #define OFFSET 0x104
27
28 #define peek(a,b) (*(unsigned short *)(0xe0000000 + (a)*16 + (b)))
29
30 typedef struct {
31   short jmp_op;
32   short signature;
33   short version;
34   short buffer_start;
35   short buffer_end;
36   short getp;
37   short putp;
38   short iov;
39 } ASYNC_STRUCT;
40
41 static ASYNC_STRUCT *async;
42 static int iov;
43 #define com_rb  iov
44 #define com_tb  iov
45 #define com_ier iov+1
46 #define com_ifr iov+2
47 #define com_bfr iov+3
48 #define com_mcr iov+4
49 #define com_lsr iov+5
50 #define com_msr iov+6
51
52 static int
53 strncasecmp(str1, str2, len)
54      char *str1, *str2;
55      register int len;
56 {
57   unsigned char c1, c2;
58
59   for (; len != 0; --len)
60     {
61       c1 = *str1++;
62       c2 = *str2++;
63
64       if (toupper(c1) != toupper(c2))
65         return toupper(c1) - toupper(c2);
66
67       if (c1 == '\0')
68         return 0;
69     }
70   return 0;
71 }
72
73 static char *
74 aptr(p)
75      short p;
76 {
77   return (char *)((unsigned)async - OFFSET + p);
78 }
79
80 static ASYNC_STRUCT *
81 getivec(int which)
82 {
83   ASYNC_STRUCT *a;
84
85   if (peek(0, which*4) != OFFSET)
86     return 0;
87
88   a = (ASYNC_STRUCT *)(0xe0000000 + peek(0, which*4+2)*16 + peek(0, which*4));
89
90   if (a->signature != SIGNATURE)
91     return 0;
92
93   if (a->version != VERSION)
94     return 0;
95
96   return a;
97 }
98
99 static int
100 dos_async_init(port)
101      int port;
102 {
103   int i;
104
105   switch (port)
106     {
107     case 1:
108       async = getivec (12);
109       break;
110     case 2:
111       async = getivec (11);
112       break;
113     default:
114       return 0;
115     }
116
117   if (!async)
118     {
119       error("GDB can not connect to asynctsr program, check that it is installed\n\
120 and that serial I/O is not being redirected (perhaps by NFS)\n\n\
121 example configuration:\n\
122 C> mode com2:9600,n,8,1,p\n\
123 C> asynctsr 2\n\
124 C> gdb \n");
125
126     }
127
128   iov = async->iov;
129   outportb(com_ier, 0x0f);
130   outportb(com_bfr, 0x03);
131   outportb(com_mcr, 0x0b);
132   async->getp = async->putp = async->buffer_start;
133   
134   return 1;
135 }
136
137 static void
138 dos_async_tx(c)
139      const char c;
140 {
141   while (~inportb(com_lsr) & 0x20);
142
143   outportb(com_tb, c);
144 }
145
146 static int
147 dos_async_ready()
148 {
149   return (async->getp != async->putp);
150 }
151
152 static int
153 dos_async_rx()
154 {
155   char rv;
156
157   while (!dos_async_ready())
158     if (kbhit())
159       {
160         printf("abort!\n");
161         return 0;
162       }
163
164   rv = *aptr(async->getp++);
165   if (async->getp >= async->buffer_end)
166     async->getp = async->buffer_start;
167
168   return rv;
169 }
170
171 static int
172 dosasync_read (fd, buf, len, timeout)
173      int fd;
174      char *buf;
175      int len;
176      int timeout;
177 {
178   long now, then;
179   int l = len;
180
181   time (&now);
182   then = now + timeout;
183
184   while (l--)
185     {
186       if (timeout)
187         {
188           while (!dos_async_ready())
189             {
190               time (&now);
191               if (now >= then)
192                 return len - l - 1;
193             }
194         }
195       *buf++ = dos_async_rx();
196     }
197
198   return len;
199 }
200
201 static int
202 dosasync_write(fd, buf, len)
203      int fd;
204      const char *buf;
205      int len;
206 {
207   int l = len;
208
209   while (l--)
210     dos_async_tx (*buf++);
211
212   return len;
213 }
214
215 static int
216 go32_open (scb, name)
217      serial_t scb;
218      const char *name;
219 {
220   int port;
221
222   if (strncasecmp (name, "com", 3) != 0)
223     {
224       errno = ENOENT;
225       return -1;
226     }
227
228   port = name[3] - '0';
229
230   if ((port != 1) && (port != 2))
231     {
232       errno = ENOENT;
233       return -11;
234     }
235
236   scb->fd = dos_async_init(port);
237   if (!scb->fd)
238     return -1;
239
240   return 0;
241 }
242
243 static void
244 go32_raw (scb)
245      serial_t scb;
246 {
247   /* Always in raw mode */
248 }
249
250 static int
251 go32_readchar (scb, timeout)
252      serial_t scb;
253      int timeout;
254 {
255   char buf;
256
257   if (dosasync_read(scb->fd, &buf, 1, timeout))  
258     return buf;
259   else
260     return SERIAL_TIMEOUT;
261 }
262
263 static int
264 go32_setbaudrate (scb, rate)
265      serial_t scb;
266      int rate;
267 {
268   return 0;
269 }
270
271 static int
272 go32_write (scb, str, len)
273      serial_t scb;
274      const char *str;
275      int len;
276 {
277   dosasync_write(scb->fd, str, len);
278
279   return 0;
280 }
281
282 static void
283 go32_close ()
284 {
285 }
286
287 static void
288 go32_restore (scb)
289      serial_t scb;
290 {
291 }
292
293 static struct serial_ops go32_ops =
294 {
295   "hardwire",
296   0,
297   go32_open,
298   go32_close,
299   go32_readchar,
300   go32_write,
301   go32_raw,
302   go32_restore,
303   go32_setbaudrate
304 };
305
306 _initialize_ser_go32 ()
307 {
308   serial_add_interface (&go32_ops);
309 }