2 * DEC 93 Erik Bos (erik@trashcan.hacktic.nl)
13 #if defined(__NetBSD__) || defined(__FreeBSD__)
15 #include <sys/ioctl.h>
22 /* #define DEBUG_COMM /* */
26 int commerror = 0, eventmask = 0;
28 struct DosDeviceStruct {
29 char *devicename; /* /dev/cua1 */
36 struct DosDeviceStruct COM[MAX_PORTS];
37 struct DosDeviceStruct LPT[MAX_PORTS];
39 void Comm_DeInit(void);
43 int x, serial = 0, parallel = 0;
44 char option[10], temp[256];
47 for (x=0; x!=MAX_PORTS; x++) {
48 strcpy(option,"COMx");
52 GetPrivateProfileString("serialports", option, "*", temp, sizeof(temp), WINE_INI);
53 if (!strcmp(temp, "*") || *temp == '\0')
54 COM[serial].devicename = NULL;
57 if (!S_ISCHR(st.st_mode))
58 fprintf(stderr,"comm: can 't use `%s' as COM%d !\n", temp, x);
60 if ((COM[serial].devicename = malloc(strlen(temp)+1)) == NULL)
61 fprintf(stderr,"comm: can't malloc for device info!\n");
64 strcpy(COM[serial].devicename, temp);
69 strcpy(option, "LPTx");
73 GetPrivateProfileString("parallelports", option, "*", temp, sizeof(temp), WINE_INI);
74 if (!strcmp(temp, "*") || *temp == '\0')
75 LPT[parallel].devicename = NULL;
78 if (!S_ISCHR(st.st_mode))
79 fprintf(stderr,"comm: can 't use `%s' as LPT%d !\n", temp, x);
81 if ((LPT[parallel].devicename = malloc(strlen(temp)+1)) == NULL)
82 fprintf(stderr,"comm: can't malloc for device info!\n");
85 strcpy(LPT[parallel].devicename, temp);
93 void Comm_DeInit(void)
97 for (x=0; x!=MAX_PORTS; x++) {
99 if (COM[x].devicename) {
102 free(COM[x].devicename);
104 if (LPT[x].devicename) {
107 free(LPT[x].devicename);
112 struct DosDeviceStruct *GetDeviceStruct(int fd)
116 for (x=0; x!=MAX_PORTS; x++) {
126 int ValidCOMPort(int x)
128 return(x < MAX_PORTS ? (int) COM[x].devicename : 0);
131 int ValidLPTPort(int x)
133 return(x < MAX_PORTS ? (int) LPT[x].devicename : 0);
145 int BuildCommDCB(LPSTR device, DCB FAR *lpdcb)
147 /* "COM1:9600,n,8,1" */
151 char *ptr, temp[256];
154 fprintf(stderr,"BuildCommDCB: (%s), ptr %d\n", device, (long) lpdcb);
158 if (!strncasecmp(device,"COM",3)) {
159 port = device[3] - '0';
163 fprintf(stderr, "comm: BUG ! COM0 can't exists!.\n");
164 commerror = IE_BADID;
167 if (!ValidCOMPort(port)) {
168 commerror = IE_BADID;
173 OpenComm(device, 0, 0);
175 lpdcb->Id = COM[port].fd;
180 if (*(device+4) != ':')
183 strcpy(temp,device+5);
184 ptr = strtok(temp, ",");
186 fprintf(stderr,"BuildCommDCB: baudrate (%s)\n", ptr);
187 lpdcb->BaudRate = atoi(ptr);
189 ptr = strtok(NULL, ",");
191 *ptr = toupper(*ptr);
193 fprintf(stderr,"BuildCommDCB: parity (%c)\n", *ptr);
196 lpdcb->Parity = NOPARITY;
203 lpdcb->Parity = EVENPARITY;
206 lpdcb->Parity = MARKPARITY;
209 lpdcb->Parity = ODDPARITY;
212 fprintf(stderr,"comm: unknown parity `%c'!\n", *ptr);
216 ptr = strtok(NULL, ",");
217 fprintf(stderr, "BuildCommDCB: charsize (%c)\n", *ptr);
218 lpdcb->ByteSize = *ptr - '0';
220 ptr = strtok(NULL, ",");
221 fprintf(stderr, "BuildCommDCB: stopbits (%c)\n", *ptr);
224 lpdcb->StopBits = ONESTOPBIT;
227 lpdcb->StopBits = TWOSTOPBITS;
230 fprintf(stderr,"comm: unknown # of stopbits `%c'!\n", *ptr);
238 int OpenComm(LPSTR device, UINT cbInQueue, UINT cbOutQueue)
243 fprintf(stderr,"OpenComm: %s, %d, %d\n", device, cbInQueue, cbOutQueue);
248 if (!strncasecmp(device,"COM",3)) {
249 port = device[3] - '0';
252 fprintf(stderr, "comm: BUG ! COM0 doesn't exists!.\n");
253 commerror = IE_BADID;
256 if (!ValidCOMPort(port)) {
257 commerror = IE_BADID;
264 fd = open(COM[port].devicename, O_RDWR | O_NONBLOCK, 0);
266 commerror = WinError();
274 if (!strncasecmp(device,"LPT",3)) {
275 port = device[3] - '0';
277 if (!ValidLPTPort(port)) {
278 commerror = IE_BADID;
286 fd = open(LPT[port].devicename, O_RDWR | O_NONBLOCK, 0);
288 commerror = WinError();
298 int CloseComm(int fd)
301 fprintf(stderr,"CloseComm: fd %d\n", fd);
304 if (close(fd) == -1) {
305 commerror = WinError();
313 int SetCommBreak(int fd)
315 struct DosDeviceStruct *ptr;
318 fprintf(stderr,"SetCommBreak: fd: %d\n", fd);
321 if ((ptr = GetDeviceStruct(fd)) == NULL) {
322 commerror = IE_BADID;
331 int ClearCommBreak(int fd)
333 struct DosDeviceStruct *ptr;
336 fprintf(stderr,"ClearCommBreak: fd: %d\n", fd);
339 if ((ptr = GetDeviceStruct(fd)) == NULL) {
340 commerror = IE_BADID;
349 LONG EscapeCommFunction(int fd, int nFunction)
355 fprintf(stderr,"EscapeCommFunction fd: %d, function: %d\n", fd, nFunction);
358 if (tcgetattr(fd, &port) == -1) {
359 commerror = WinError();
368 for (max = 0;COM[max].devicename;max++)
374 for (max = 0;LPT[max].devicename;max++)
380 port.c_cflag &= TIOCM_DTR;
384 port.c_cflag &= TIOCM_RTS;
388 port.c_cflag |= CRTSCTS;
392 port.c_cflag |= CRTSCTS;
396 port.c_iflag |= IXOFF;
400 port.c_iflag |= IXON;
404 fprintf(stderr,"EscapeCommFunction fd: %d, unknown function: %d\n", fd, nFunction);
408 if (tcsetattr(fd, TCSADRAIN, &port) == -1) {
409 commerror = WinError();
417 int FlushComm(int fd, int fnQueue)
422 fprintf(stderr,"FlushComm fd: %d, queue: %d\n", fd, fnQueue);
433 fprintf(stderr,"FlushComm fd: %d, UNKNOWN queue: %d\n", fd, fnQueue);
437 if (tcflush(fd, fnQueue)) {
438 commerror = WinError();
446 int GetCommError(int fd, COMSTAT FAR *lpStat)
449 fprintf(stderr,"GetCommError: fd %d (current error %d)\n", fd, commerror);
455 UINT FAR* SetCommEventMask(int fd, UINT fuEvtMask)
458 fprintf(stderr,"SetCommEventMask: fd %d, mask %d\n", fd, fuEvtMask);
461 eventmask |= fuEvtMask;
462 return (UINT *)&eventmask;
465 UINT GetCommEventMask(int fd, int fnEvtClear)
468 fprintf(stderr,"GetCommEventMask: fd %d, mask %d\n", fd, fnEvtClear);
470 eventmask &= ~fnEvtClear;
474 int SetCommState(DCB FAR *lpdcb)
479 fprintf(stderr,"SetCommState: fd %d, ptr %d\n", lpdcb->Id, (long) lpdcb);
482 if (tcgetattr(lpdcb->Id, &port) == -1) {
483 commerror = WinError();
488 port.c_cc[VTIME] = 1;
490 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|IMAXBEL);
491 port.c_iflag |= (IGNBRK);
493 port.c_oflag &= ~(OPOST);
495 port.c_cflag &= ~(HUPCL);
496 port.c_cflag |= CLOCAL | CREAD;
498 port.c_lflag &= ~(ICANON|ECHO|ISIG);
499 port.c_lflag |= NOFLSH;
501 fprintf(stderr,"SetCommState: baudrate %d\n",lpdcb->BaudRate);
503 port.c_cflag &= ~CBAUD;
504 switch (lpdcb->BaudRate) {
507 port.c_cflag |= B110;
511 port.c_cflag |= B300;
515 port.c_cflag |= B600;
519 port.c_cflag |= B1200;
523 port.c_cflag |= B2400;
527 port.c_cflag |= B4800;
531 port.c_cflag |= B9600;
535 port.c_cflag |= B19200;
539 port.c_cflag |= B38400;
542 commerror = IE_BAUDRATE;
546 switch (lpdcb->BaudRate) {
549 port.c_ospeed = B110;
553 port.c_ospeed = B300;
557 port.c_ospeed = B600;
561 port.c_ospeed = B1200;
565 port.c_ospeed = B2400;
569 port.c_ospeed = B4800;
573 port.c_ospeed = B9600;
577 port.c_ospeed = B19200;
581 port.c_ospeed = B38400;
584 commerror = IE_BAUDRATE;
587 port.c_ispeed = port.c_ospeed;
589 fprintf(stderr,"SetCommState: bytesize %d\n",lpdcb->ByteSize);
590 port.c_cflag &= ~CSIZE;
591 switch (lpdcb->ByteSize) {
605 commerror = IE_BYTESIZE;
609 fprintf(stderr,"SetCommState: parity %d\n",lpdcb->Parity);
610 port.c_cflag &= ~(PARENB | PARODD);
612 switch (lpdcb->Parity) {
614 port.c_iflag &= ~INPCK;
617 port.c_cflag |= (PARENB | PARODD);
618 port.c_iflag |= INPCK;
621 port.c_cflag |= PARENB;
622 port.c_iflag |= INPCK;
625 commerror = IE_BYTESIZE;
630 fprintf(stderr,"SetCommState: stopbits %d\n",lpdcb->StopBits);
631 switch (lpdcb->StopBits) {
633 port.c_cflag &= ~CSTOPB;
636 port.c_cflag |= CSTOPB;
639 commerror = IE_BYTESIZE;
643 if (lpdcb->fDtrflow || lpdcb->fRtsflow || lpdcb->fOutxCtsFlow)
644 port.c_cflag |= CRTSCTS;
646 if (lpdcb->fDtrDisable)
647 port.c_cflag &= ~CRTSCTS;
650 port.c_iflag |= IXON;
652 port.c_iflag |= IXOFF;
654 if (tcsetattr(lpdcb->Id, TCSADRAIN, &port) == -1) {
655 commerror = WinError();
663 int GetCommState(int fd, DCB FAR *lpdcb)
668 fprintf(stderr,"GetCommState: fd %d, ptr %d\n", fd, (long) lpdcb);
671 if (tcgetattr(fd, &port) == -1) {
672 commerror = WinError();
679 switch (port.c_cflag & CBAUD) {
681 switch (port.c_ospeed) {
684 lpdcb->BaudRate = 110;
687 lpdcb->BaudRate = 300;
690 lpdcb->BaudRate = 600;
693 lpdcb->BaudRate = 1200;
696 lpdcb->BaudRate = 2400;
699 lpdcb->BaudRate = 4800;
702 lpdcb->BaudRate = 9600;
705 lpdcb->BaudRate = 19200;
708 lpdcb->BaudRate = 38400;
712 switch (port.c_cflag & CSIZE) {
727 switch (port.c_cflag & ~(PARENB | PARODD)) {
729 lpdcb->fParity = NOPARITY;
732 lpdcb->fParity = EVENPARITY;
734 case (PARENB | PARODD):
735 lpdcb->fParity = ODDPARITY;
739 if (port.c_cflag & CSTOPB)
740 lpdcb->StopBits = TWOSTOPBITS;
742 lpdcb->StopBits = ONESTOPBIT;
744 lpdcb->RlsTimeout = 50;
745 lpdcb->CtsTimeout = 50;
746 lpdcb->DsrTimeout = 50;
751 lpdcb->fDtrDisable = 0;
752 if (port.c_cflag & CRTSCTS) {
755 lpdcb->fOutxCtsFlow = 1;
756 lpdcb->fOutxDsrFlow = 1;
758 lpdcb->fDtrDisable = 1;
760 if (port.c_iflag & IXON)
765 if (port.c_iflag & IXOFF)
780 int TransmitCommChar(int fd, char chTransmit)
782 struct DosDeviceStruct *ptr;
785 fprintf(stderr,"TransmitCommChar: fd %d, data %d \n", fd, chTransmit);
788 if ((ptr = GetDeviceStruct(fd)) == NULL) {
789 commerror = IE_BADID;
793 if (ptr->suspended) {
794 commerror = IE_HARDWARE;
798 if (write(fd, (void *) &chTransmit, 1) == -1) {
799 commerror = WinError();
807 int UngetCommChar(int fd, char chUnget)
809 struct DosDeviceStruct *ptr;
812 fprintf(stderr,"UngetCommChar: fd %d (char %d)\n", fd, chUnget);
815 if ((ptr = GetDeviceStruct(fd)) == NULL) {
816 commerror = IE_BADID;
820 if (ptr->suspended) {
821 commerror = IE_HARDWARE;
826 ptr->unget_byte = chUnget;
832 int ReadComm(int fd, LPSTR lpvBuf, int cbRead)
835 struct DosDeviceStruct *ptr;
838 fprintf(stderr,"ReadComm: fd %d, ptr %d, length %d\n", fd, (long) lpvBuf, cbRead);
841 if ((ptr = GetDeviceStruct(fd)) == NULL) {
842 commerror = IE_BADID;
846 if (ptr->suspended) {
847 commerror = IE_HARDWARE;
852 *lpvBuf = ptr->unget_byte;
860 status = read(fd, (void *) lpvBuf, cbRead);
863 commerror = WinError();
867 return length + status;
871 int WriteComm(int fd, LPSTR lpvBuf, int cbWrite)
874 struct DosDeviceStruct *ptr;
877 fprintf(stderr,"WriteComm: fd %d, ptr %d, length %d\n", fd, (long) lpvBuf, cbWrite);
880 if ((ptr = GetDeviceStruct(fd)) == NULL) {
881 commerror = IE_BADID;
885 if (ptr->suspended) {
886 commerror = IE_HARDWARE;
891 for (x=0; x != cbWrite ; x++)
892 fprintf(stderr,"%c", *(lpvBuf + x) );
895 length = write(fd, (void *) lpvBuf, cbWrite);
898 commerror = WinError();