import gdb-1999-07-19 snapshot
[deliverable/binutils-gdb.git] / gdb / rdi-share / unixcomm.c
CommitLineData
c906108c
SS
1/*
2 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
3 *
4 * This software may be freely used, copied, modified, and distributed
5 * provided that the above copyright notice is preserved in all copies of the
6 * software.
7 */
8
9/* -*-C-*-
10 *
11 * $Revision$
12 * $Date$
13 *
14 */
15
16#ifdef __hpux
17# define _POSIX_SOURCE 1
18#endif
19
20#include <stdio.h>
21#include <unistd.h>
22#include <ctype.h>
23
24#ifdef __hpux
25# define _TERMIOS_INCLUDED
26# include <sys/termio.h>
27# undef _TERMIOS_INCLUDED
28#else
29# include <termios.h>
30#endif
31
32#include <string.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <stdarg.h>
36#include <sys/types.h>
37#include <sys/time.h>
38
39#ifdef sun
40# include <sys/ioccom.h>
41# ifdef __svr4__
42# include <sys/bpp_io.h>
43# else
44# include <sbusdev/bpp_io.h>
45# endif
46#endif
47
48#ifdef BSD
49# ifdef sun
50# include <sys/ttydev.h>
51# endif
52# ifdef __alpha
53# include <sys/ioctl.h>
54# else
55# include <sys/filio.h>
56# endif
57#endif
58
59#ifdef __hpux
60# define _INCLUDE_HPUX_SOURCE
61# include <sys/ioctl.h>
62# undef _INCLUDE_HPUX_SOURCE
63#endif
64
65#include "host.h"
66#include "unixcomm.h"
67
68#define PP_TIMEOUT 1 /* seconds */
69
70#ifdef sun
71#define SERPORT1 "/dev/ttya"
72#define SERPORT2 "/dev/ttyb"
73#define PARPORT1 "/dev/bpp0"
74#define PARPORT2 "/dev/bpp1"
75#endif
76
77#ifdef __hpux
78#define SERPORT1 "/dev/tty00"
79#define SERPORT2 "/dev/tty01"
80#define PARPORT1 "/dev/ptr_parallel"
81#define PARPORT2 "/dev/ptr_parallel"
82#endif
83
84#ifdef __linux__
adf40b2e
JM
85#define SERPORT1 "/dev/ttyS0"
86#define SERPORT2 "/dev/ttyS1"
c906108c
SS
87#define PARPORT1 "/dev/par0"
88#define PARPORT2 "/dev/par1"
89#endif
90
91#if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (bsdi)
92#define SERPORT1 "/dev/cuaa0"
93#define SERPORT2 "/dev/cuaa1"
94#define PARPORT1 "/dev/lpt0"
95#define PARPORT2 "/dev/lpt1"
96#endif
97
98
99#define SERIAL_PREFIX "/dev/tty"
100#if defined(_WIN32) || defined (__CYGWIN32__)
101#define SERPORT1 "com1"
102#define SERPORT2 "com2"
103#define PARPORT1 "lpt1"
104#define PARPORT2 "lpt2"
105#undef SERIAL_PREFIX
106#define SERIAL_PREFIX "com"
107#endif
108
109
110
111/*
112 * Parallel port output pins, used for signalling to target
113 */
114
115#ifdef sun
116struct bpp_pins bp;
117#endif
118
119static int serpfd = -1;
120static int parpfd = -1;
121
122extern const char *Unix_MatchValidSerialDevice(const char *name)
123{
124 int i=0;
125 char *sername=NULL;
126
127 /* Accept no name as the default serial port */
128 if (name == NULL) {
129 return SERPORT1;
130 }
131
132 /* Look for the simple cases - 1,2,s,S,/dev/... first, and
133 * afterwards look for S=... clauses, which need parsing properly.
134 */
135
136 /* Accept /dev/tty* where * is limited */
137 if (strlen(name) == strlen(SERPORT1)
138 && strncmp(name, SERIAL_PREFIX, strlen (SERIAL_PREFIX)) == 0)
139 {
140 return name;
141 }
142
143 /* Accept "1" or "2" or "S" - S is equivalent to "1" */
144 if (strcmp(name, "1") == 0 ||
145 strcmp(name, "S") == 0 || strcmp(name, "s") == 0) {
146 return SERPORT1;
147 }
148 if (strcmp(name, "2") == 0) return SERPORT2;
149
150 /* It wasn't one of the simple cases, so now we have to parse it
151 * properly
152 */
153
154 do {
155 switch (name[i]) {
156 case ',':
157 /* Skip over commas */
158 i++;
159 break;
160
161 default:
162 return 0;
163 /* Unexpected character => error - not matched */
164
165 case 0:
166 /* End of string means return whatever we have matched */
167 return sername;
168
169 case 's':
170 case 'S':
171 case 'h':
172 case 'H': {
173 char ch = tolower(name[i]);
174 int j, continue_from, len;
175
176 /* If the next character is a comma or a NULL then this is
177 * a request for the default Serial port
178 */
179 if (name[++i] == 0 || name[i] == ',') {
180 if (ch=='s')
181 sername=SERPORT1;
182 break;
183 }
184
185 /* Next character must be an = */
186 if (name[i] != '=') return 0;
187 /* Search for the end of the port spec. (ends in NULL or ,) */
188 for (j= ++i; name[j] != 0 && name[j] != ','; j++)
189 ; /* Do nothing */
190 /* Notice whether this is the last thing to parse or not
191 * and also calaculate the length of the string
192 */
193 if (name[j] == '0') continue_from = -1;
194 else continue_from = j;
195 len=(j-i);
196
197 /* And now try to match the serial / parallel port */
198 switch (ch) {
199 case 's': {
200 /* Match serial port */
201 if (len==1) {
202 if (name[i]=='1')
203 sername=SERPORT1;
204 else if (name[i]=='2')
205 sername=SERPORT2;
206 } else if (len==strlen(SERPORT1)) {
207 if (strncmp(name+i,SERPORT1,strlen(SERPORT1)) == 0)
208 sername=SERPORT1;
209 else if (strncmp(name+i,SERPORT2,strlen(SERPORT2)) == 0)
210 sername=SERPORT2;
211 }
212
213 break;
214 }
215
216 case 'h':
217 /* We don't actually deal with the H case here, we just
218 * match it and allow it through.
219 */
220 break;
221 }
222
223 if (continue_from == -1) return sername;
224 i = continue_from;
225 break;
226 }
227 }
228 } while (1);
229
230 return 0;
231}
232
233
234extern int Unix_IsSerialInUse(void)
235{
236 if (serpfd >= 0)
237 return -1;
238
239 return 0;
240}
241
242extern int Unix_OpenSerial(const char *name)
243{
244#if defined(BSD) || defined(__CYGWIN32__)
245 serpfd = open(name, O_RDWR);
246#else
247 serpfd = open(name, O_RDWR | O_NONBLOCK);
248#endif
249
250 if (serpfd < 0) {
251 perror("open");
252 return -1;
253 }
254
255 return 0;
256}
257
258extern void Unix_CloseSerial(void)
259{
260 if (serpfd >= 0)
261 {
262 (void)close(serpfd);
263 serpfd = -1;
264 }
265}
266
267extern int Unix_ReadSerial(unsigned char *buf, int n, bool block)
268{
269 fd_set fdset;
270 struct timeval tv;
271 int err;
272
273 FD_ZERO(&fdset);
274 FD_SET(serpfd, &fdset);
275
276 tv.tv_sec = 0;
277 tv.tv_usec = (block ? 10000 : 0);
278
279 err = select(serpfd + 1, &fdset, NULL, NULL, &tv);
280
281 if (err < 0 && errno != EINTR)
282 {
283#ifdef DEBUG
284 perror("select");
285#endif
286 panic("select failure");
287 return -1;
288 }
289 else if (err > 0 && FD_ISSET(serpfd, &fdset))
290 return read(serpfd, buf, n);
291 else /* err == 0 || FD_CLR(serpfd, &fdset) */
292 {
293 errno = ERRNO_FOR_BLOCKED_IO;
294 return -1;
295 }
296}
297
298extern int Unix_WriteSerial(unsigned char *buf, int n)
299{
300 return write(serpfd, buf, n);
301}
302
303extern void Unix_ResetSerial(void)
304{
305 struct termios terminfo;
306
307 tcgetattr(serpfd, &terminfo);
c906108c
SS
308 terminfo.c_lflag &= ~(ICANON | ISIG | ECHO | IEXTEN);
309 terminfo.c_iflag &= ~(IGNCR | INPCK | ISTRIP | ICRNL | BRKINT);
310 terminfo.c_iflag |= (IXON | IXOFF | IGNBRK);
311 terminfo.c_cflag = (terminfo.c_cflag & ~CSIZE) | CS8 | CREAD;
312 terminfo.c_cflag &= ~PARENB;
313 terminfo.c_cc[VMIN] = 1;
314 terminfo.c_cc[VTIME] = 0;
315 terminfo.c_oflag &= ~OPOST;
c906108c
SS
316 tcsetattr(serpfd, TCSAFLUSH, &terminfo);
317}
318
319extern void Unix_SetSerialBaudRate(int baudrate)
320{
321 struct termios terminfo;
322
323 tcgetattr(serpfd, &terminfo);
324 cfsetospeed(&terminfo, baudrate);
325 cfsetispeed(&terminfo, baudrate);
326 tcsetattr(serpfd, TCSAFLUSH, &terminfo);
327}
328
329extern void Unix_ioctlNonBlocking(void)
330{
331#if defined(BSD)
332 int nonblockingIO = 1;
333 (void)ioctl(serpfd, FIONBIO, &nonblockingIO);
334
335 if (parpfd != -1)
336 (void)ioctl(parpfd, FIONBIO, &nonblockingIO);
337#endif
338}
339
340extern void Unix_IsValidParallelDevice(
341 const char *portstring, char **sername, char **parname)
342{
343 int i=0;
344 *sername=NULL;
345 *parname=NULL;
346
347 /* Do not recognise a NULL portstring */
348 if (portstring==NULL) return;
349
350 do {
351 switch (portstring[i]) {
352 case ',':
353 /* Skip over commas */
354 i++;
355 break;
356
357 default:
358 case 0:
359 /* End of string or bad characcter means we have finished */
360 return;
361
362 case 's':
363 case 'S':
364 case 'p':
365 case 'P':
366 case 'h':
367 case 'H': {
368 char ch = tolower(portstring[i]);
369 int j, continue_from, len;
370
371 /* If the next character is a comma or a NULL then this is
372 * a request for the default Serial or Parallel port
373 */
374 if (portstring[++i] == 0 || portstring[i] == ',') {
375 if (ch=='s') *sername=SERPORT1;
376 else if (ch=='p') *parname=PARPORT1;
377 break;
378 }
379
380 /* Next character must be an = */
381 if (portstring[i] != '=') return;
382 /* Search for the end of the port spec. (ends in NULL or ,) */
383 for (j= ++i; portstring[j] != 0 && portstring[j] != ','; j++)
384 ; /* Do nothing */
385 /* Notice whether this is the last thing to parse or not
386 * and also calaculate the length of the string
387 */
388 if (portstring[j] == '0') continue_from = -1;
389 else continue_from = j;
390 len=(j-i);
391
392 /* And now try to match the serial / parallel port */
393 switch (ch) {
394 case 's': {
395 /* Match serial port */
396 if (len==1) {
397 if (portstring[i]=='1') *sername=SERPORT1;
398 else if (portstring[i]=='2') *sername=SERPORT2;
399 } else if (len==strlen(SERPORT1)) {
400 if (strncmp(portstring+i,SERPORT1,strlen(SERPORT1)) == 0)
401 *sername=SERPORT1;
402 else if (strncmp(portstring+i,SERPORT2,strlen(SERPORT2)) == 0)
403 *sername=SERPORT2;
404 }
405 break;
406 }
407
408 case 'p': {
409 /* Match parallel port */
410 if (len==1) {
411 if (portstring[i]=='1') *parname=PARPORT1;
412 else if (portstring[i]=='2') *parname=PARPORT2;
413 } else if (len==strlen(PARPORT1)) {
414 if (strncmp(portstring+i,PARPORT1,strlen(PARPORT1)) == 0)
415 *parname=PARPORT1;
416 else if (strncmp(portstring+i,PARPORT2,strlen(PARPORT2)) == 0)
417 *parname=PARPORT2;
418 }
419 break;
420 }
421
422 case 'h':
423 /* We don't actually deal with the H case here, we just
424 * match it and allow it through.
425 */
426 break;
427 }
428
429 if (continue_from == -1) return;
430 i = continue_from;
431 break;
432 }
433 }
434 } while (1);
435 return; /* Will never get here */
436}
437
438extern int Unix_IsParallelInUse(void)
439{
440 if (parpfd >= 0)
441 return -1;
442
443 return 0;
444}
445
446extern int Unix_OpenParallel(const char *name)
447{
448#if defined(BSD)
449 parpfd = open(name, O_RDWR);
450#else
451 parpfd = open(name, O_RDWR | O_NONBLOCK);
452#endif
453
454 if (parpfd < 0)
455 {
456 char errbuf[256];
457
458 sprintf(errbuf, "open %s", name);
459 perror(errbuf);
460
461 return -1;
462 }
463
464 return 0;
465}
466
467extern void Unix_CloseParallel(void)
468{
469 if (parpfd >= 0)
470 {
471 (void)close(parpfd);
472 parpfd = -1;
473 }
474}
475
476
477extern unsigned int Unix_WriteParallel(unsigned char *buf, int n)
478{
479 int ngone;
480
481 if ((ngone = write(parpfd, buf, n)) < 0)
482 {
483 /*
484 * we ignore errors (except for debug purposes)
485 */
486#ifdef DEBUG
487 char errbuf[256];
488
489 sprintf(errbuf, "send_packet: write");
490 perror(errbuf);
491#endif
492 ngone = 0;
493 }
494
495 /* finished */
496 return (unsigned int)ngone;
497}
498
499
500#ifdef sun
501extern void Unix_ResetParallel(void)
502{
503 struct bpp_transfer_parms tp;
504
505#ifdef DEBUG
506 printf("serpar_reset\n");
507#endif
508
509 /*
510 * we need to set the parallel port up for BUSY handshaking,
511 * and select the timeout
512 */
513 if (ioctl(parpfd, BPPIOC_GETPARMS, &tp) < 0)
514 {
515#ifdef DEBUG
516 perror("ioctl(BPPIOCGETPARMS)");
517#endif
518 panic("serpar_reset: cannot get BPP parameters");
519 }
520
521 tp.write_handshake = BPP_BUSY_HS;
522 tp.write_timeout = PP_TIMEOUT;
523
524 if (ioctl(parpfd, BPPIOC_SETPARMS, &tp) < 0)
525 {
526#ifdef DEBUG
527 perror("ioctl(BPPIOC_SETPARMS)");
528#endif
529 panic("serpar_reset: cannot set BPP parameters");
530 }
531}
532
533#else
534
535/* Parallel not supported on HP */
536
537extern void Unix_ResetParallel(void)
538{
539}
540
541#endif
542
This page took 0.068433 seconds and 4 git commands to generate.