2 * Copyright (C) 1995 Advanced RISC Machines Limited. All rights reserved.
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
17 # define _POSIX_SOURCE 1
25 # define _TERMIOS_INCLUDED
26 # include <sys/termio.h>
27 # undef _TERMIOS_INCLUDED
36 #include <sys/types.h>
40 # include <sys/ioccom.h>
42 # include <sys/bpp_io.h>
44 # include <sbusdev/bpp_io.h>
50 # include <sys/ttydev.h>
53 # include <sys/ioctl.h>
55 # include <sys/filio.h>
60 # define _INCLUDE_HPUX_SOURCE
61 # include <sys/ioctl.h>
62 # undef _INCLUDE_HPUX_SOURCE
68 #define PP_TIMEOUT 1 /* seconds */
71 #define SERPORT1 "/dev/ttya"
72 #define SERPORT2 "/dev/ttyb"
73 #define PARPORT1 "/dev/bpp0"
74 #define PARPORT2 "/dev/bpp1"
78 #define SERPORT1 "/dev/tty00"
79 #define SERPORT2 "/dev/tty01"
80 #define PARPORT1 "/dev/ptr_parallel"
81 #define PARPORT2 "/dev/ptr_parallel"
85 #define SERPORT1 "/dev/cua0"
86 #define SERPORT2 "/dev/cua1"
87 #define PARPORT1 "/dev/par0"
88 #define PARPORT2 "/dev/par1"
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"
99 #define SERIAL_PREFIX "/dev/tty"
100 #if defined(_WIN32) || defined (__CYGWIN__)
101 #define SERPORT1 "com1"
102 #define SERPORT2 "com2"
103 #define PARPORT1 "lpt1"
104 #define PARPORT2 "lpt2"
106 #define SERIAL_PREFIX "com"
112 * Parallel port output pins, used for signalling to target
119 static int serpfd
= -1;
120 static int parpfd
= -1;
122 extern const char *Unix_MatchValidSerialDevice(const char *name
)
127 /* Accept no name as the default serial port */
132 /* Look for the simple cases - 1,2,s,S,/dev/... first, and
133 * afterwards look for S=... clauses, which need parsing properly.
136 /* Accept /dev/tty* where * is limited */
137 if (strlen(name
) == strlen(SERPORT1
)
138 && strncmp(name
, SERIAL_PREFIX
, strlen (SERIAL_PREFIX
)) == 0)
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) {
148 if (strcmp(name
, "2") == 0) return SERPORT2
;
150 /* It wasn't one of the simple cases, so now we have to parse it
157 /* Skip over commas */
163 /* Unexpected character => error - not matched */
166 /* End of string means return whatever we have matched */
173 char ch
= tolower(name
[i
]);
174 int j
, continue_from
, len
;
176 /* If the next character is a comma or a NULL then this is
177 * a request for the default Serial port
179 if (name
[++i
] == 0 || name
[i
] == ',') {
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
++)
190 /* Notice whether this is the last thing to parse or not
191 * and also calaculate the length of the string
193 if (name
[j
] == '0') continue_from
= -1;
194 else continue_from
= j
;
197 /* And now try to match the serial / parallel port */
200 /* Match serial port */
204 else if (name
[i
]=='2')
206 } else if (len
==strlen(SERPORT1
)) {
207 if (strncmp(name
+i
,SERPORT1
,strlen(SERPORT1
)) == 0)
209 else if (strncmp(name
+i
,SERPORT2
,strlen(SERPORT2
)) == 0)
217 /* We don't actually deal with the H case here, we just
218 * match it and allow it through.
223 if (continue_from
== -1) return sername
;
234 extern int Unix_IsSerialInUse(void)
242 extern int Unix_OpenSerial(const char *name
)
244 #if defined(BSD) || defined(__CYGWIN__)
245 serpfd
= open(name
, O_RDWR
);
247 serpfd
= open(name
, O_RDWR
| O_NONBLOCK
);
258 extern void Unix_CloseSerial(void)
267 extern int Unix_ReadSerial(unsigned char *buf
, int n
, bool block
)
274 FD_SET(serpfd
, &fdset
);
277 tv
.tv_usec
= (block
? 10000 : 0);
279 err
= select(serpfd
+ 1, &fdset
, NULL
, NULL
, &tv
);
281 if (err
< 0 && errno
!= EINTR
)
286 panic("select failure");
289 else if (err
> 0 && FD_ISSET(serpfd
, &fdset
))
290 return read(serpfd
, buf
, n
);
291 else /* err == 0 || FD_CLR(serpfd, &fdset) */
293 errno
= ERRNO_FOR_BLOCKED_IO
;
298 extern int Unix_WriteSerial(unsigned char *buf
, int n
)
300 return write(serpfd
, buf
, n
);
303 extern void Unix_ResetSerial(void)
305 struct termios terminfo
;
307 tcgetattr(serpfd
, &terminfo
);
309 /* Expedient, but it works. */
310 terminfo
.c_iflag
= 0;
311 terminfo
.c_oflag
= 0;
312 terminfo
.c_cflag
= 48;
313 terminfo
.c_lflag
= 0;
314 terminfo
.c_cc
[VMIN
] = 0;
315 terminfo
.c_cc
[VTIME
] = 1;
317 terminfo
.c_lflag
&= ~(ICANON
| ISIG
| ECHO
| IEXTEN
);
318 terminfo
.c_iflag
&= ~(IGNCR
| INPCK
| ISTRIP
| ICRNL
| BRKINT
);
319 terminfo
.c_iflag
|= (IXON
| IXOFF
| IGNBRK
);
320 terminfo
.c_cflag
= (terminfo
.c_cflag
& ~CSIZE
) | CS8
| CREAD
;
321 terminfo
.c_cflag
&= ~PARENB
;
322 terminfo
.c_cc
[VMIN
] = 1;
323 terminfo
.c_cc
[VTIME
] = 0;
324 terminfo
.c_oflag
&= ~OPOST
;
326 tcsetattr(serpfd
, TCSAFLUSH
, &terminfo
);
329 extern void Unix_SetSerialBaudRate(int baudrate
)
331 struct termios terminfo
;
333 tcgetattr(serpfd
, &terminfo
);
334 cfsetospeed(&terminfo
, baudrate
);
335 cfsetispeed(&terminfo
, baudrate
);
336 tcsetattr(serpfd
, TCSAFLUSH
, &terminfo
);
339 extern void Unix_ioctlNonBlocking(void)
342 int nonblockingIO
= 1;
343 (void)ioctl(serpfd
, FIONBIO
, &nonblockingIO
);
346 (void)ioctl(parpfd
, FIONBIO
, &nonblockingIO
);
350 extern void Unix_IsValidParallelDevice(
351 const char *portstring
, char **sername
, char **parname
)
357 /* Do not recognise a NULL portstring */
358 if (portstring
==NULL
) return;
361 switch (portstring
[i
]) {
363 /* Skip over commas */
369 /* End of string or bad characcter means we have finished */
378 char ch
= tolower(portstring
[i
]);
379 int j
, continue_from
, len
;
381 /* If the next character is a comma or a NULL then this is
382 * a request for the default Serial or Parallel port
384 if (portstring
[++i
] == 0 || portstring
[i
] == ',') {
385 if (ch
=='s') *sername
=SERPORT1
;
386 else if (ch
=='p') *parname
=PARPORT1
;
390 /* Next character must be an = */
391 if (portstring
[i
] != '=') return;
392 /* Search for the end of the port spec. (ends in NULL or ,) */
393 for (j
= ++i
; portstring
[j
] != 0 && portstring
[j
] != ','; j
++)
395 /* Notice whether this is the last thing to parse or not
396 * and also calaculate the length of the string
398 if (portstring
[j
] == '0') continue_from
= -1;
399 else continue_from
= j
;
402 /* And now try to match the serial / parallel port */
405 /* Match serial port */
407 if (portstring
[i
]=='1') *sername
=SERPORT1
;
408 else if (portstring
[i
]=='2') *sername
=SERPORT2
;
409 } else if (len
==strlen(SERPORT1
)) {
410 if (strncmp(portstring
+i
,SERPORT1
,strlen(SERPORT1
)) == 0)
412 else if (strncmp(portstring
+i
,SERPORT2
,strlen(SERPORT2
)) == 0)
419 /* Match parallel port */
421 if (portstring
[i
]=='1') *parname
=PARPORT1
;
422 else if (portstring
[i
]=='2') *parname
=PARPORT2
;
423 } else if (len
==strlen(PARPORT1
)) {
424 if (strncmp(portstring
+i
,PARPORT1
,strlen(PARPORT1
)) == 0)
426 else if (strncmp(portstring
+i
,PARPORT2
,strlen(PARPORT2
)) == 0)
433 /* We don't actually deal with the H case here, we just
434 * match it and allow it through.
439 if (continue_from
== -1) return;
445 return; /* Will never get here */
448 extern int Unix_IsParallelInUse(void)
456 extern int Unix_OpenParallel(const char *name
)
459 parpfd
= open(name
, O_RDWR
);
461 parpfd
= open(name
, O_RDWR
| O_NONBLOCK
);
468 sprintf(errbuf
, "open %s", name
);
477 extern void Unix_CloseParallel(void)
487 extern unsigned int Unix_WriteParallel(unsigned char *buf
, int n
)
491 if ((ngone
= write(parpfd
, buf
, n
)) < 0)
494 * we ignore errors (except for debug purposes)
499 sprintf(errbuf
, "send_packet: write");
506 return (unsigned int)ngone
;
511 extern void Unix_ResetParallel(void)
513 struct bpp_transfer_parms tp
;
516 printf("serpar_reset\n");
520 * we need to set the parallel port up for BUSY handshaking,
521 * and select the timeout
523 if (ioctl(parpfd
, BPPIOC_GETPARMS
, &tp
) < 0)
526 perror("ioctl(BPPIOCGETPARMS)");
528 panic("serpar_reset: cannot get BPP parameters");
531 tp
.write_handshake
= BPP_BUSY_HS
;
532 tp
.write_timeout
= PP_TIMEOUT
;
534 if (ioctl(parpfd
, BPPIOC_SETPARMS
, &tp
) < 0)
537 perror("ioctl(BPPIOC_SETPARMS)");
539 panic("serpar_reset: cannot set BPP parameters");
545 /* Parallel not supported on HP */
547 extern void Unix_ResetParallel(void)
This page took 0.040116 seconds and 4 git commands to generate.