2 * DEC 93 Erik Bos (erik@trashcan.hacktic.nl)
13 #if defined(__NetBSD__) || defined(__FreeBSD__)
15 #include <sys/ioctl.h>
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 (!strncmp(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 commerror
= IE_NOPEN
;
176 lpdcb
->Id
= COM
[port
].fd
;
181 if (*(device
+4) != ':')
184 strcpy(temp
,device
+5);
185 ptr
= strtok(temp
, ",");
187 fprintf(stderr
,"BuildCommDCB: baudrate (%s)\n", ptr
);
188 lpdcb
->BaudRate
= atoi(ptr
);
190 ptr
= strtok(NULL
, ",");
192 *ptr
= toupper(*ptr
);
194 fprintf(stderr
,"BuildCommDCB: parity (%c)\n", *ptr
);
197 lpdcb
->Parity
= NOPARITY
;
204 lpdcb
->Parity
= EVENPARITY
;
207 lpdcb
->Parity
= MARKPARITY
;
210 lpdcb
->Parity
= ODDPARITY
;
213 fprintf(stderr
,"comm: unknown parity `%c'!\n", *ptr
);
217 ptr
= strtok(NULL
, ",");
218 fprintf(stderr
, "BuildCommDCB: charsize (%c)\n", *ptr
);
219 lpdcb
->ByteSize
= *ptr
- '0';
221 ptr
= strtok(NULL
, ",");
222 fprintf(stderr
, "BuildCommDCB: stopbits (%c)\n", *ptr
);
225 lpdcb
->StopBits
= ONESTOPBIT
;
228 lpdcb
->StopBits
= TWOSTOPBITS
;
231 fprintf(stderr
,"comm: unknown # of stopbits `%c'!\n", *ptr
);
239 int OpenComm(LPSTR device
, UINT cbInQueue
, UINT cbOutQueue
)
244 fprintf(stderr
,"OpenComm: %s, %d, %d\n", device
, cbInQueue
, cbOutQueue
);
249 if (!strncmp(device
,"COM",3)) {
250 port
= device
[3] - '0';
253 fprintf(stderr
, "comm: BUG ! COM0 doesn't exists!.\n");
254 commerror
= IE_BADID
;
257 if (!ValidCOMPort(port
)) {
258 commerror
= IE_BADID
;
266 fd
= open(COM
[port
].devicename
, O_RDWR
| O_NONBLOCK
, 0);
268 commerror
= WinError();
276 if (!strncmp(device
,"LPT",3)) {
277 port
= device
[3] - '0';
279 if (!ValidLPTPort(port
)) {
280 commerror
= IE_BADID
;
288 fd
= open(LPT
[port
].devicename
, O_RDWR
| O_NONBLOCK
, 0);
290 commerror
= WinError();
300 int CloseComm(int fd
)
303 fprintf(stderr
,"CloseComm: fd %d\n", fd
);
306 if (close(fd
) == -1) {
307 commerror
= WinError();
315 int SetCommBreak(int fd
)
317 struct DosDeviceStruct
*ptr
;
320 fprintf(stderr
,"SetCommBreak: fd: %d\n", fd
);
323 if ((ptr
= GetDeviceStruct(fd
)) == NULL
) {
324 commerror
= IE_BADID
;
333 int ClearCommBreak(int fd
)
335 struct DosDeviceStruct
*ptr
;
338 fprintf(stderr
,"ClearCommBreak: fd: %d\n", fd
);
341 if ((ptr
= GetDeviceStruct(fd
)) == NULL
) {
342 commerror
= IE_BADID
;
351 LONG
EscapeCommFunction(int fd
, int nFunction
)
357 fprintf(stderr
,"EscapeCommFunction fd: %d, function: %d\n", fd
, nFunction
);
360 if (tcgetattr(fd
, &port
) == -1) {
361 commerror
= WinError();
370 for (max
= 0;COM
[max
].devicename
;max
++)
376 for (max
= 0;LPT
[max
].devicename
;max
++)
382 port
.c_cflag
&= TIOCM_DTR
;
386 port
.c_cflag
&= TIOCM_RTS
;
390 port
.c_cflag
|= CRTSCTS
;
394 port
.c_cflag
|= CRTSCTS
;
398 port
.c_iflag
|= IXOFF
;
402 port
.c_iflag
|= IXON
;
406 fprintf(stderr
,"EscapeCommFunction fd: %d, unknown function: %d\n", fd
, nFunction
);
410 if (tcsetattr(fd
, TCSADRAIN
, &port
) == -1) {
411 commerror
= WinError();
419 int FlushComm(int fd
, int fnQueue
)
424 fprintf(stderr
,"FlushComm fd: %d, queue: %d\n", fd
, fnQueue
);
435 fprintf(stderr
,"FlushComm fd: %d, UNKNOWN queue: %d\n", fd
, fnQueue
);
439 if (tcflush(fd
, fnQueue
)) {
440 commerror
= WinError();
448 int GetCommError(int fd
, COMSTAT FAR
*lpStat
)
451 fprintf(stderr
,"GetCommError: fd %d (current error %d)\n", fd
, commerror
);
457 UINT FAR
* SetCommEventMask(int fd
, UINT fuEvtMask
)
460 fprintf(stderr
,"SetCommEventMask: fd %d, mask %d\n", fd
, fuEvtMask
);
463 eventmask
|= fuEvtMask
;
464 return (UINT
*)&eventmask
;
467 UINT
GetCommEventMask(int fd
, int fnEvtClear
)
470 fprintf(stderr
,"GetCommEventMask: fd %d, mask %d\n", fd
, fnEvtClear
);
472 eventmask
&= ~fnEvtClear
;
476 int SetCommState(DCB FAR
*lpdcb
)
481 fprintf(stderr
,"SetCommState: fd %d, ptr %d\n", lpdcb
->Id
, (long) lpdcb
);
484 if (tcgetattr(lpdcb
->Id
, &port
) == -1) {
485 commerror
= WinError();
490 port
.c_cc
[VTIME
] = 1;
492 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|IMAXBEL
);
493 port
.c_iflag
|= (IGNBRK
);
495 port
.c_oflag
&= ~(OPOST
);
497 port
.c_cflag
&= ~(HUPCL
);
498 port
.c_cflag
|= CLOCAL
| CREAD
;
500 port
.c_lflag
&= ~(ICANON
|ECHO
|ISIG
);
501 port
.c_lflag
|= NOFLSH
;
503 fprintf(stderr
,"SetCommState: baudrate %d\n",lpdcb
->BaudRate
);
505 port
.c_cflag
&= ~CBAUD
;
506 switch (lpdcb
->BaudRate
) {
509 port
.c_cflag
|= B110
;
513 port
.c_cflag
|= B300
;
517 port
.c_cflag
|= B600
;
521 port
.c_cflag
|= B1200
;
525 port
.c_cflag
|= B2400
;
529 port
.c_cflag
|= B4800
;
533 port
.c_cflag
|= B9600
;
537 port
.c_cflag
|= B19200
;
541 port
.c_cflag
|= B38400
;
544 commerror
= IE_BAUDRATE
;
548 switch (lpdcb
->BaudRate
) {
551 port
.c_ospeed
= B110
;
555 port
.c_ospeed
= B300
;
559 port
.c_ospeed
= B600
;
563 port
.c_ospeed
= B1200
;
567 port
.c_ospeed
= B2400
;
571 port
.c_ospeed
= B4800
;
575 port
.c_ospeed
= B9600
;
579 port
.c_ospeed
= B19200
;
583 port
.c_ospeed
= B38400
;
586 commerror
= IE_BAUDRATE
;
589 port
.c_ispeed
= port
.c_ospeed
;
591 fprintf(stderr
,"SetCommState: bytesize %d\n",lpdcb
->ByteSize
);
592 port
.c_cflag
&= ~CSIZE
;
593 switch (lpdcb
->ByteSize
) {
607 commerror
= IE_BYTESIZE
;
611 fprintf(stderr
,"SetCommState: parity %d\n",lpdcb
->Parity
);
612 port
.c_cflag
&= ~(PARENB
| PARODD
);
614 switch (lpdcb
->Parity
) {
616 port
.c_iflag
&= ~INPCK
;
619 port
.c_cflag
|= (PARENB
| PARODD
);
620 port
.c_iflag
|= INPCK
;
623 port
.c_cflag
|= PARENB
;
624 port
.c_iflag
|= INPCK
;
627 commerror
= IE_BYTESIZE
;
632 fprintf(stderr
,"SetCommState: stopbits %d\n",lpdcb
->StopBits
);
633 switch (lpdcb
->StopBits
) {
635 port
.c_cflag
&= ~CSTOPB
;
638 port
.c_cflag
|= CSTOPB
;
641 commerror
= IE_BYTESIZE
;
645 if (lpdcb
->fDtrflow
|| lpdcb
->fRtsflow
|| lpdcb
->fOutxCtsFlow
)
646 port
.c_cflag
|= CRTSCTS
;
648 if (lpdcb
->fDtrDisable
)
649 port
.c_cflag
&= ~CRTSCTS
;
652 port
.c_iflag
|= IXON
;
654 port
.c_iflag
|= IXOFF
;
656 if (tcsetattr(lpdcb
->Id
, TCSADRAIN
, &port
) == -1) {
657 commerror
= WinError();
665 int GetCommState(int fd
, DCB FAR
*lpdcb
)
670 fprintf(stderr
,"GetCommState: fd %d, ptr %d\n", fd
, (long) lpdcb
);
673 if (tcgetattr(fd
, &port
) == -1) {
674 commerror
= WinError();
681 switch (port
.c_cflag
& CBAUD
) {
683 switch (port
.c_ospeed
) {
686 lpdcb
->BaudRate
= 110;
689 lpdcb
->BaudRate
= 300;
692 lpdcb
->BaudRate
= 600;
695 lpdcb
->BaudRate
= 1200;
698 lpdcb
->BaudRate
= 2400;
701 lpdcb
->BaudRate
= 4800;
704 lpdcb
->BaudRate
= 9600;
707 lpdcb
->BaudRate
= 19200;
710 lpdcb
->BaudRate
= 38400;
714 switch (port
.c_cflag
& CSIZE
) {
729 switch (port
.c_cflag
& ~(PARENB
| PARODD
)) {
731 lpdcb
->fParity
= NOPARITY
;
734 lpdcb
->fParity
= EVENPARITY
;
736 case (PARENB
| PARODD
):
737 lpdcb
->fParity
= ODDPARITY
;
741 if (port
.c_cflag
& CSTOPB
)
742 lpdcb
->StopBits
= TWOSTOPBITS
;
744 lpdcb
->StopBits
= ONESTOPBIT
;
746 lpdcb
->RlsTimeout
= 50;
747 lpdcb
->CtsTimeout
= 50;
748 lpdcb
->DsrTimeout
= 50;
753 lpdcb
->fDtrDisable
= 0;
754 if (port
.c_cflag
& CRTSCTS
) {
757 lpdcb
->fOutxCtsFlow
= 1;
758 lpdcb
->fOutxDsrFlow
= 1;
760 lpdcb
->fDtrDisable
= 1;
762 if (port
.c_iflag
& IXON
)
767 if (port
.c_iflag
& IXOFF
)
782 int TransmitCommChar(int fd
, char chTransmit
)
784 struct DosDeviceStruct
*ptr
;
787 fprintf(stderr
,"TransmitCommChar: fd %d, data %d \n", fd
, chTransmit
);
790 if ((ptr
= GetDeviceStruct(fd
)) == NULL
) {
791 commerror
= IE_BADID
;
795 if (ptr
->suspended
) {
796 commerror
= IE_HARDWARE
;
800 if (write(fd
, (void *) &chTransmit
, 1) == -1) {
801 commerror
= WinError();
809 int UngetCommChar(int fd
, char chUnget
)
811 struct DosDeviceStruct
*ptr
;
814 fprintf(stderr
,"UngetCommChar: fd %d (char %d)\n", fd
, chUnget
);
817 if ((ptr
= GetDeviceStruct(fd
)) == NULL
) {
818 commerror
= IE_BADID
;
822 if (ptr
->suspended
) {
823 commerror
= IE_HARDWARE
;
828 ptr
->unget_byte
= chUnget
;
834 int ReadComm(int fd
, LPSTR lpvBuf
, int cbRead
)
837 struct DosDeviceStruct
*ptr
;
840 fprintf(stderr
,"ReadComm: fd %d, ptr %d, length %d\n", fd
, (long) lpvBuf
, cbRead
);
843 if ((ptr
= GetDeviceStruct(fd
)) == NULL
) {
844 commerror
= IE_BADID
;
848 if (ptr
->suspended
) {
849 commerror
= IE_HARDWARE
;
854 *lpvBuf
= ptr
->unget_byte
;
862 status
= read(fd
, (void *) lpvBuf
, cbRead
);
865 commerror
= WinError();
869 return length
+ status
;
873 int WriteComm(int fd
, LPSTR lpvBuf
, int cbWrite
)
876 struct DosDeviceStruct
*ptr
;
879 fprintf(stderr
,"WriteComm: fd %d, ptr %d, length %d\n", fd
, (long) lpvBuf
, cbWrite
);
882 if ((ptr
= GetDeviceStruct(fd
)) == NULL
) {
883 commerror
= IE_BADID
;
887 if (ptr
->suspended
) {
888 commerror
= IE_HARDWARE
;
893 for (x
=0; x
!= cbWrite
; x
++)
894 fprintf(stderr
,"%c", *(lpvBuf
+ x
) );
897 length
= write(fd
, (void *) lpvBuf
, cbWrite
);
900 commerror
= WinError();