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>
39 #if defined (__FreeBSD__) || defined (__NetBSD__) || defined (__OpenBSD__) || defined (bsdi)
41 #include <sys/ioctl.h>
45 # include <sys/ioccom.h>
47 # include <sys/bpp_io.h>
49 # include <sbusdev/bpp_io.h>
55 # include <sys/ttydev.h>
58 # include <sys/ioctl.h>
60 # include <sys/filio.h>
65 # define _INCLUDE_HPUX_SOURCE
66 # include <sys/ioctl.h>
67 # undef _INCLUDE_HPUX_SOURCE
73 #define PP_TIMEOUT 1 /* seconds */
76 #define SERIAL_PREFIX "/dev/tty"
77 #define SERPORT1 "/dev/ttya"
78 #define SERPORT2 "/dev/ttyb"
79 #define PARPORT1 "/dev/bpp0"
80 #define PARPORT2 "/dev/bpp1"
84 #define SERIAL_PREFIX "/dev/tty"
85 #define SERPORT1 "/dev/tty00"
86 #define SERPORT2 "/dev/tty01"
87 #define PARPORT1 "/dev/ptr_parallel"
88 #define PARPORT2 "/dev/ptr_parallel"
92 #define SERIAL_PREFIX "/dev/ttyS"
93 #define SERPORT1 "/dev/ttyS0"
94 #define SERPORT2 "/dev/ttyS1"
95 #define PARPORT1 "/dev/par0"
96 #define PARPORT2 "/dev/par1"
99 #if defined(_WIN32) || defined (__CYGWIN__)
100 #define SERIAL_PREFIX "com"
101 #define SERPORT1 "com1"
102 #define SERPORT2 "com2"
103 #define PARPORT1 "lpt1"
104 #define PARPORT2 "lpt2"
107 #if !defined (SERIAL_PREFIX)
108 #define SERIAL_PREFIX "/dev/cuaa"
109 #define SERPORT1 "/dev/cuaa0"
110 #define SERPORT2 "/dev/cuaa1"
111 #define PARPORT1 "/dev/lpt0"
112 #define PARPORT2 "/dev/lpt1"
119 * Parallel port output pins, used for signalling to target
126 static int serpfd = -1;
127 static int parpfd = -1;
129 extern const char *Unix_MatchValidSerialDevice(const char *name)
134 /* Accept no name as the default serial port */
139 /* Look for the simple cases - 1,2,s,S,/dev/... first, and
140 * afterwards look for S=... clauses, which need parsing properly.
143 /* Accept /dev/tty* where * is limited */
144 if (strlen(name) == strlen(SERPORT1)
145 && strncmp(name, SERIAL_PREFIX, strlen (SERIAL_PREFIX)) == 0)
150 /* Accept "1" or "2" or "S" - S is equivalent to "1" */
151 if (strcmp(name, "1") == 0 ||
152 strcmp(name, "S") == 0 || strcmp(name, "s") == 0) {
155 if (strcmp(name, "2") == 0) return SERPORT2;
157 /* It wasn't one of the simple cases, so now we have to parse it
164 /* Skip over commas */
170 /* Unexpected character => error - not matched */
173 /* End of string means return whatever we have matched */
180 char ch = tolower(name[i]);
181 int j, continue_from, len;
183 /* If the next character is a comma or a NULL then this is
184 * a request for the default Serial port
186 if (name[++i] == 0 || name[i] == ',') {
192 /* Next character must be an = */
193 if (name[i] != '=') return 0;
194 /* Search for the end of the port spec. (ends in NULL or ,) */
195 for (j= ++i; name[j] != 0 && name[j] != ','; j++)
197 /* Notice whether this is the last thing to parse or not
198 * and also calaculate the length of the string
200 if (name[j] == '0') continue_from = -1;
201 else continue_from = j;
204 /* And now try to match the serial / parallel port */
207 /* Match serial port */
211 else if (name[i]=='2')
213 } else if (len==strlen(SERPORT1)) {
214 if (strncmp(name+i,SERPORT1,strlen(SERPORT1)) == 0)
216 else if (strncmp(name+i,SERPORT2,strlen(SERPORT2)) == 0)
224 /* We don't actually deal with the H case here, we just
225 * match it and allow it through.
230 if (continue_from == -1) return sername;
241 extern int Unix_IsSerialInUse(void)
249 extern int Unix_OpenSerial(const char *name)
251 #if defined(BSD) || defined(__CYGWIN__)
252 serpfd = open(name, O_RDWR);
254 serpfd = open(name, O_RDWR | O_NONBLOCK);
262 if (ioctl(serpfd, TIOCEXCL) < 0) {
264 perror("ioctl: TIOCEXCL");
272 extern void Unix_CloseSerial(void)
281 extern int Unix_ReadSerial(unsigned char *buf, int n, bool block)
288 FD_SET(serpfd, &fdset);
291 tv.tv_usec = (block ? 10000 : 0);
293 err = select(serpfd + 1, &fdset, NULL, NULL, &tv);
295 if (err < 0 && errno != EINTR)
300 panic("select failure");
303 else if (err > 0 && FD_ISSET(serpfd, &fdset))
307 s = read(serpfd, buf, n);
312 else /* err == 0 || FD_CLR(serpfd, &fdset) */
314 errno = ERRNO_FOR_BLOCKED_IO;
319 extern int Unix_WriteSerial(unsigned char *buf, int n)
321 return write(serpfd, buf, n);
324 extern void Unix_ResetSerial(void)
326 struct termios terminfo;
328 tcgetattr(serpfd, &terminfo);
329 terminfo.c_lflag &= ~(ICANON | ISIG | ECHO | IEXTEN);
330 terminfo.c_iflag &= ~(IGNCR | INPCK | ISTRIP | ICRNL | BRKINT);
331 terminfo.c_iflag |= (IXON | IXOFF | IGNBRK);
332 terminfo.c_cflag = (terminfo.c_cflag & ~CSIZE) | CS8 | CREAD;
333 terminfo.c_cflag &= ~PARENB;
334 terminfo.c_cc[VMIN] = 1;
335 terminfo.c_cc[VTIME] = 0;
336 terminfo.c_oflag &= ~OPOST;
337 tcsetattr(serpfd, TCSAFLUSH, &terminfo);
340 extern void Unix_SetSerialBaudRate(int baudrate)
342 struct termios terminfo;
344 tcgetattr(serpfd, &terminfo);
345 cfsetospeed(&terminfo, baudrate);
346 cfsetispeed(&terminfo, baudrate);
347 tcsetattr(serpfd, TCSAFLUSH, &terminfo);
350 extern void Unix_ioctlNonBlocking(void)
353 int nonblockingIO = 1;
354 (void)ioctl(serpfd, FIONBIO, &nonblockingIO);
357 (void)ioctl(parpfd, FIONBIO, &nonblockingIO);
361 extern void Unix_IsValidParallelDevice(
362 const char *portstring, char **sername, char **parname)
368 /* Do not recognise a NULL portstring */
369 if (portstring==NULL) return;
372 switch (portstring[i]) {
374 /* Skip over commas */
380 /* End of string or bad characcter means we have finished */
389 char ch = tolower(portstring[i]);
390 int j, continue_from, len;
392 /* If the next character is a comma or a NULL then this is
393 * a request for the default Serial or Parallel port
395 if (portstring[++i] == 0 || portstring[i] == ',') {
396 if (ch=='s') *sername=SERPORT1;
397 else if (ch=='p') *parname=PARPORT1;
401 /* Next character must be an = */
402 if (portstring[i] != '=') return;
403 /* Search for the end of the port spec. (ends in NULL or ,) */
404 for (j= ++i; portstring[j] != 0 && portstring[j] != ','; j++)
406 /* Notice whether this is the last thing to parse or not
407 * and also calaculate the length of the string
409 if (portstring[j] == '0') continue_from = -1;
410 else continue_from = j;
413 /* And now try to match the serial / parallel port */
416 /* Match serial port */
418 if (portstring[i]=='1') *sername=SERPORT1;
419 else if (portstring[i]=='2') *sername=SERPORT2;
420 } else if (len==strlen(SERPORT1)) {
421 if (strncmp(portstring+i,SERPORT1,strlen(SERPORT1)) == 0)
423 else if (strncmp(portstring+i,SERPORT2,strlen(SERPORT2)) == 0)
430 /* Match parallel port */
432 if (portstring[i]=='1') *parname=PARPORT1;
433 else if (portstring[i]=='2') *parname=PARPORT2;
434 } else if (len==strlen(PARPORT1)) {
435 if (strncmp(portstring+i,PARPORT1,strlen(PARPORT1)) == 0)
437 else if (strncmp(portstring+i,PARPORT2,strlen(PARPORT2)) == 0)
444 /* We don't actually deal with the H case here, we just
445 * match it and allow it through.
450 if (continue_from == -1) return;
456 return; /* Will never get here */
459 extern int Unix_IsParallelInUse(void)
467 extern int Unix_OpenParallel(const char *name)
470 parpfd = open(name, O_RDWR);
472 parpfd = open(name, O_RDWR | O_NONBLOCK);
479 sprintf(errbuf, "open %s", name);
488 extern void Unix_CloseParallel(void)
498 extern unsigned int Unix_WriteParallel(unsigned char *buf, int n)
502 if ((ngone = write(parpfd, buf, n)) < 0)
505 * we ignore errors (except for debug purposes)
510 sprintf(errbuf, "send_packet: write");
517 return (unsigned int)ngone;
522 extern void Unix_ResetParallel(void)
524 struct bpp_transfer_parms tp;
527 printf("serpar_reset\n");
531 * we need to set the parallel port up for BUSY handshaking,
532 * and select the timeout
534 if (ioctl(parpfd, BPPIOC_GETPARMS, &tp) < 0)
537 perror("ioctl(BPPIOCGETPARMS)");
539 panic("serpar_reset: cannot get BPP parameters");
542 tp.write_handshake = BPP_BUSY_HS;
543 tp.write_timeout = PP_TIMEOUT;
545 if (ioctl(parpfd, BPPIOC_SETPARMS, &tp) < 0)
548 perror("ioctl(BPPIOC_SETPARMS)");
550 panic("serpar_reset: cannot set BPP parameters");
556 /* Parallel not supported on HP */
558 extern void Unix_ResetParallel(void)