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/ttyS0"
86 #define SERPORT2 "/dev/ttyS1"
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 (__CYGWIN32__)
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(__CYGWIN32__)
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))
293 s = read(serpfd, buf, n);
298 else /* err == 0 || FD_CLR(serpfd, &fdset) */
300 errno = ERRNO_FOR_BLOCKED_IO;
305 extern int Unix_WriteSerial(unsigned char *buf, int n)
307 return write(serpfd, buf, n);
310 extern void Unix_ResetSerial(void)
312 struct termios terminfo;
314 tcgetattr(serpfd, &terminfo);
315 terminfo.c_lflag &= ~(ICANON | ISIG | ECHO | IEXTEN);
316 terminfo.c_iflag &= ~(IGNCR | INPCK | ISTRIP | ICRNL | BRKINT);
317 terminfo.c_iflag |= (IXON | IXOFF | IGNBRK);
318 terminfo.c_cflag = (terminfo.c_cflag & ~CSIZE) | CS8 | CREAD;
319 terminfo.c_cflag &= ~PARENB;
320 terminfo.c_cc[VMIN] = 1;
321 terminfo.c_cc[VTIME] = 0;
322 terminfo.c_oflag &= ~OPOST;
323 tcsetattr(serpfd, TCSAFLUSH, &terminfo);
326 extern void Unix_SetSerialBaudRate(int baudrate)
328 struct termios terminfo;
330 tcgetattr(serpfd, &terminfo);
331 cfsetospeed(&terminfo, baudrate);
332 cfsetispeed(&terminfo, baudrate);
333 tcsetattr(serpfd, TCSAFLUSH, &terminfo);
336 extern void Unix_ioctlNonBlocking(void)
339 int nonblockingIO = 1;
340 (void)ioctl(serpfd, FIONBIO, &nonblockingIO);
343 (void)ioctl(parpfd, FIONBIO, &nonblockingIO);
347 extern void Unix_IsValidParallelDevice(
348 const char *portstring, char **sername, char **parname)
354 /* Do not recognise a NULL portstring */
355 if (portstring==NULL) return;
358 switch (portstring[i]) {
360 /* Skip over commas */
366 /* End of string or bad characcter means we have finished */
375 char ch = tolower(portstring[i]);
376 int j, continue_from, len;
378 /* If the next character is a comma or a NULL then this is
379 * a request for the default Serial or Parallel port
381 if (portstring[++i] == 0 || portstring[i] == ',') {
382 if (ch=='s') *sername=SERPORT1;
383 else if (ch=='p') *parname=PARPORT1;
387 /* Next character must be an = */
388 if (portstring[i] != '=') return;
389 /* Search for the end of the port spec. (ends in NULL or ,) */
390 for (j= ++i; portstring[j] != 0 && portstring[j] != ','; j++)
392 /* Notice whether this is the last thing to parse or not
393 * and also calaculate the length of the string
395 if (portstring[j] == '0') continue_from = -1;
396 else continue_from = j;
399 /* And now try to match the serial / parallel port */
402 /* Match serial port */
404 if (portstring[i]=='1') *sername=SERPORT1;
405 else if (portstring[i]=='2') *sername=SERPORT2;
406 } else if (len==strlen(SERPORT1)) {
407 if (strncmp(portstring+i,SERPORT1,strlen(SERPORT1)) == 0)
409 else if (strncmp(portstring+i,SERPORT2,strlen(SERPORT2)) == 0)
416 /* Match parallel port */
418 if (portstring[i]=='1') *parname=PARPORT1;
419 else if (portstring[i]=='2') *parname=PARPORT2;
420 } else if (len==strlen(PARPORT1)) {
421 if (strncmp(portstring+i,PARPORT1,strlen(PARPORT1)) == 0)
423 else if (strncmp(portstring+i,PARPORT2,strlen(PARPORT2)) == 0)
430 /* We don't actually deal with the H case here, we just
431 * match it and allow it through.
436 if (continue_from == -1) return;
442 return; /* Will never get here */
445 extern int Unix_IsParallelInUse(void)
453 extern int Unix_OpenParallel(const char *name)
456 parpfd = open(name, O_RDWR);
458 parpfd = open(name, O_RDWR | O_NONBLOCK);
465 sprintf(errbuf, "open %s", name);
474 extern void Unix_CloseParallel(void)
484 extern unsigned int Unix_WriteParallel(unsigned char *buf, int n)
488 if ((ngone = write(parpfd, buf, n)) < 0)
491 * we ignore errors (except for debug purposes)
496 sprintf(errbuf, "send_packet: write");
503 return (unsigned int)ngone;
508 extern void Unix_ResetParallel(void)
510 struct bpp_transfer_parms tp;
513 printf("serpar_reset\n");
517 * we need to set the parallel port up for BUSY handshaking,
518 * and select the timeout
520 if (ioctl(parpfd, BPPIOC_GETPARMS, &tp) < 0)
523 perror("ioctl(BPPIOCGETPARMS)");
525 panic("serpar_reset: cannot get BPP parameters");
528 tp.write_handshake = BPP_BUSY_HS;
529 tp.write_timeout = PP_TIMEOUT;
531 if (ioctl(parpfd, BPPIOC_SETPARMS, &tp) < 0)
534 perror("ioctl(BPPIOC_SETPARMS)");
536 panic("serpar_reset: cannot set BPP parameters");
542 /* Parallel not supported on HP */
544 extern void Unix_ResetParallel(void)