Release 951124
[wine/multimedia.git] / misc / comm.c
blob06501cc4c5051506f79a34821d79177a0b86f486
1 /*
2 * DEC 93 Erik Bos <erik@xs4all.nl>
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <termios.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <ctype.h>
12 #include <sys/stat.h>
13 #if defined(__NetBSD__) || defined(__FreeBSD__)
14 #include <errno.h>
15 #include <sys/ioctl.h>
16 #endif
17 #include <unistd.h>
19 #include "wine.h"
20 #include "windows.h"
21 #include "comm.h"
22 #include "stddebug.h"
23 /* #define DEBUG_COMM */
24 /* #undef DEBUG_COMM */
25 #include "debug.h"
27 int commerror = 0, eventmask = 0;
29 struct DosDeviceStruct COM[MAX_PORTS];
30 struct DosDeviceStruct LPT[MAX_PORTS];
32 void COMM_Init(void)
34 int x;
35 char option[10], temp[256], *btemp;
36 struct stat st;
38 for (x=0; x!=MAX_PORTS; x++) {
39 strcpy(option,"COMx");
40 option[3] = '1' + x;
41 option[4] = '\0';
43 GetPrivateProfileString("serialports", option, "*", temp, sizeof(temp), WINE_INI);
44 if (!strcmp(temp, "*") || *temp == '\0')
45 COM[x].devicename = NULL;
46 else {
47 btemp = index(temp,',');
48 if (btemp != NULL) {
49 *btemp++ = '\0';
50 COM[x].baudrate = atoi(btemp);
51 } else {
52 COM[x].baudrate = -1;
54 stat(temp, &st);
55 if (!S_ISCHR(st.st_mode))
56 fprintf(stderr,"comm: can 't use `%s' as %s !\n", temp, option);
57 else
58 if ((COM[x].devicename = malloc(strlen(temp)+1)) == NULL)
59 fprintf(stderr,"comm: can't malloc for device info!\n");
60 else {
61 COM[x].fd = 0;
62 strcpy(COM[x].devicename, temp);
64 dprintf_comm(stddeb,
65 "Comm_Init: %s = %s\n", option, COM[x].devicename);
68 strcpy(option, "LPTx");
69 option[3] = '1' + x;
70 option[4] = '\0';
72 GetPrivateProfileString("parallelports", option, "*", temp, sizeof(temp), WINE_INI);
73 if (!strcmp(temp, "*") || *temp == '\0')
74 LPT[x].devicename = NULL;
75 else {
76 stat(temp, &st);
77 if (!S_ISCHR(st.st_mode))
78 fprintf(stderr,"comm: can 't use `%s' as %s !\n", temp, option);
79 else
80 if ((LPT[x].devicename = malloc(strlen(temp)+1)) == NULL)
81 fprintf(stderr,"comm: can't malloc for device info!\n");
82 else {
83 LPT[x].fd = 0;
84 strcpy(LPT[x].devicename, temp);
86 dprintf_comm(stddeb,
87 "Comm_Init: %s = %s\n", option, LPT[x].devicename);
94 struct DosDeviceStruct *GetDeviceStruct(int fd)
96 int x;
98 for (x=0; x!=MAX_PORTS; x++) {
99 if (COM[x].fd == fd)
100 return &COM[x];
101 if (LPT[x].fd == fd)
102 return &LPT[x];
105 return NULL;
108 int ValidCOMPort(int x)
110 return(x < MAX_PORTS ? (int) COM[x].devicename : 0);
113 int ValidLPTPort(int x)
115 return(x < MAX_PORTS ? (int) LPT[x].devicename : 0);
118 int WinError(void)
120 dprintf_comm(stddeb, "WinError: errno = %d\n", errno);
121 switch (errno) {
122 default:
123 return CE_IOE;
127 int BuildCommDCB(LPSTR device, DCB FAR *lpdcb)
129 /* "COM1:9600,n,8,1" */
130 /* 012345 */
132 int port;
133 char *ptr, temp[256];
135 dprintf_comm(stddeb,
136 "BuildCommDCB: (%s), ptr %p\n", device, lpdcb);
137 commerror = 0;
139 if (!strncasecmp(device,"COM",3)) {
140 port = device[3] - '0';
143 if (port-- == 0) {
144 fprintf(stderr, "comm: BUG ! COM0 can't exists!.\n");
145 commerror = IE_BADID;
148 if (!ValidCOMPort(port)) {
149 commerror = IE_BADID;
150 return -1;
153 if (!COM[port].fd) {
154 OpenComm(device, 0, 0);
156 lpdcb->Id = COM[port].fd;
158 if (!*(device+4))
159 return 0;
161 if (*(device+4) != ':')
162 return -1;
164 strcpy(temp,device+5);
165 ptr = strtok(temp, ",");
167 if (COM[port].baudrate > 0)
168 lpdcb->BaudRate = COM[port].baudrate;
169 else
170 lpdcb->BaudRate = atoi(ptr);
171 dprintf_comm(stddeb,"BuildCommDCB: baudrate (%d)\n", lpdcb->BaudRate);
173 ptr = strtok(NULL, ",");
174 if (islower(*ptr))
175 *ptr = toupper(*ptr);
177 dprintf_comm(stddeb,"BuildCommDCB: parity (%c)\n", *ptr);
178 switch (*ptr) {
179 case 'N':
180 lpdcb->Parity = NOPARITY;
181 lpdcb->fParity = 0;
182 break;
184 lpdcb->fParity = 1;
186 case 'E':
187 lpdcb->Parity = EVENPARITY;
188 break;
189 case 'M':
190 lpdcb->Parity = MARKPARITY;
191 break;
192 case 'O':
193 lpdcb->Parity = ODDPARITY;
194 break;
195 default:
196 fprintf(stderr,"comm: unknown parity `%c'!\n", *ptr);
197 return -1;
200 ptr = strtok(NULL, ",");
201 dprintf_comm(stddeb, "BuildCommDCB: charsize (%c)\n", *ptr);
202 lpdcb->ByteSize = *ptr - '0';
204 ptr = strtok(NULL, ",");
205 dprintf_comm(stddeb, "BuildCommDCB: stopbits (%c)\n", *ptr);
206 switch (*ptr) {
207 case '1':
208 lpdcb->StopBits = ONESTOPBIT;
209 break;
210 case '2':
211 lpdcb->StopBits = TWOSTOPBITS;
212 break;
213 default:
214 fprintf(stderr,"comm: unknown # of stopbits `%c'!\n", *ptr);
215 return -1;
219 return 0;
222 int OpenComm(LPSTR device, UINT cbInQueue, UINT cbOutQueue)
224 int port, fd;
226 dprintf_comm(stddeb,
227 "OpenComm: %s, %d, %d\n", device, cbInQueue, cbOutQueue);
228 commerror = 0;
230 if (!strncasecmp(device,"COM",3)) {
231 port = device[3] - '0';
233 if (port-- == 0) {
234 fprintf(stderr, "comm: BUG ! COM0 doesn't exists!.\n");
235 commerror = IE_BADID;
238 dprintf_comm(stddeb,
239 "OpenComm: %s = %s\n", device, COM[port].devicename);
241 if (!ValidCOMPort(port)) {
242 commerror = IE_BADID;
243 return -1;
245 if (COM[port].fd) {
246 return COM[port].fd;
249 fd = open(COM[port].devicename, O_RDWR | O_NONBLOCK);
250 if (fd == -1) {
251 commerror = WinError();
252 return -1;
253 } else {
254 COM[port].fd = fd;
255 return fd;
258 else
259 if (!strncasecmp(device,"LPT",3)) {
260 port = device[3] - '0';
262 if (!ValidLPTPort(port)) {
263 commerror = IE_BADID;
264 return -1;
266 if (LPT[port].fd) {
267 commerror = IE_OPEN;
268 return -1;
271 fd = open(LPT[port].devicename, O_RDWR | O_NONBLOCK, 0);
272 if (fd == -1) {
273 commerror = WinError();
274 return -1;
275 } else {
276 LPT[port].fd = fd;
277 return fd;
280 return 0;
283 int CloseComm(int fd)
285 dprintf_comm(stddeb,"CloseComm: fd %d\n", fd);
286 if (close(fd) == -1) {
287 commerror = WinError();
288 return -1;
289 } else {
290 commerror = 0;
291 return 0;
295 int SetCommBreak(int fd)
297 struct DosDeviceStruct *ptr;
299 dprintf_comm(stddeb,"SetCommBreak: fd: %d\n", fd);
300 if ((ptr = GetDeviceStruct(fd)) == NULL) {
301 commerror = IE_BADID;
302 return -1;
305 ptr->suspended = 1;
306 commerror = 0;
307 return 0;
310 int ClearCommBreak(int fd)
312 struct DosDeviceStruct *ptr;
314 dprintf_comm(stddeb,"ClearCommBreak: fd: %d\n", fd);
315 if ((ptr = GetDeviceStruct(fd)) == NULL) {
316 commerror = IE_BADID;
317 return -1;
320 ptr->suspended = 0;
321 commerror = 0;
322 return 0;
325 LONG EscapeCommFunction(int fd, int nFunction)
327 int max;
328 struct termios port;
330 dprintf_comm(stddeb,
331 "EscapeCommFunction fd: %d, function: %d\n", fd, nFunction);
332 if (tcgetattr(fd, &port) == -1) {
333 commerror = WinError();
334 return -1;
337 switch (nFunction) {
338 case RESETDEV:
339 break;
341 case GETMAXCOM:
342 for (max = MAX_PORTS;!COM[max].devicename;max--)
344 return max;
345 break;
347 case GETMAXLPT:
348 for (max = MAX_PORTS;!LPT[max].devicename;max--)
350 return 0x80 + max;
351 break;
353 case CLRDTR:
354 port.c_cflag &= TIOCM_DTR;
355 break;
357 case CLRRTS:
358 port.c_cflag &= TIOCM_RTS;
359 break;
361 #ifndef __svr4__
362 case SETDTR:
363 port.c_cflag |= CRTSCTS;
364 break;
366 case SETRTS:
367 port.c_cflag |= CRTSCTS;
368 break;
369 #endif
371 case SETXOFF:
372 port.c_iflag |= IXOFF;
373 break;
375 case SETXON:
376 port.c_iflag |= IXON;
377 break;
379 default:
380 fprintf(stderr,
381 "EscapeCommFunction fd: %d, unknown function: %d\n",
382 fd, nFunction);
383 break;
386 if (tcsetattr(fd, TCSADRAIN, &port) == -1) {
387 commerror = WinError();
388 return -1;
389 } else {
390 commerror = 0;
391 return 0;
395 int FlushComm(int fd, int fnQueue)
397 int queue;
399 dprintf_comm(stddeb,"FlushComm fd: %d, queue: %d\n", fd, fnQueue);
400 switch (fnQueue) {
401 case 0:
402 queue = TCOFLUSH;
403 break;
404 case 1:
405 queue = TCIFLUSH;
406 break;
407 default:
408 fprintf(stderr,
409 "FlushComm fd: %d, UNKNOWN queue: %d\n",
410 fd, fnQueue);
411 return -1;
414 if (tcflush(fd, fnQueue)) {
415 commerror = WinError();
416 return -1;
417 } else {
418 commerror = 0;
419 return 0;
423 int GetCommError(int fd, COMSTAT FAR *lpStat)
425 int temperror;
427 dprintf_comm(stddeb,
428 "GetCommError: fd %d (current error %d)\n", fd, commerror);
429 temperror = commerror;
430 commerror = 0;
431 return(temperror);
434 UINT FAR* SetCommEventMask(int fd, UINT fuEvtMask)
436 dprintf_comm(stddeb,
437 "SetCommEventMask: fd %d, mask %d\n", fd, fuEvtMask);
438 eventmask |= fuEvtMask;
439 return (UINT *)&eventmask;
442 UINT GetCommEventMask(int fd, int fnEvtClear)
444 dprintf_comm(stddeb,
445 "GetCommEventMask: fd %d, mask %d\n", fd, fnEvtClear);
446 eventmask &= ~fnEvtClear;
447 return eventmask;
450 int SetCommState(DCB FAR *lpdcb)
452 struct termios port;
453 struct DosDeviceStruct *ptr;
455 dprintf_comm(stddeb,
456 "SetCommState: fd %d, ptr %p\n", lpdcb->Id, lpdcb);
457 if (tcgetattr(lpdcb->Id, &port) == -1) {
458 commerror = WinError();
459 return -1;
462 port.c_cc[VMIN] = 0;
463 port.c_cc[VTIME] = 1;
465 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|IMAXBEL);
466 port.c_iflag |= (IGNBRK);
468 port.c_oflag &= ~(OPOST);
470 port.c_cflag &= ~(HUPCL);
471 port.c_cflag |= CLOCAL | CREAD;
473 port.c_lflag &= ~(ICANON|ECHO|ISIG);
474 port.c_lflag |= NOFLSH;
476 if ((ptr = GetDeviceStruct(lpdcb->Id)) == NULL) {
477 commerror = IE_BADID;
478 return -1;
480 if (ptr->baudrate > 0)
481 lpdcb->BaudRate = ptr->baudrate;
482 dprintf_comm(stddeb,"SetCommState: baudrate %d\n",lpdcb->BaudRate);
483 #ifdef CBAUD
484 port.c_cflag &= ~CBAUD;
485 switch (lpdcb->BaudRate) {
486 case 110:
487 case CBR_110:
488 port.c_cflag |= B110;
489 break;
490 case 300:
491 case CBR_300:
492 port.c_cflag |= B300;
493 break;
494 case 600:
495 case CBR_600:
496 port.c_cflag |= B600;
497 break;
498 case 1200:
499 case CBR_1200:
500 port.c_cflag |= B1200;
501 break;
502 case 2400:
503 case CBR_2400:
504 port.c_cflag |= B2400;
505 break;
506 case 4800:
507 case CBR_4800:
508 port.c_cflag |= B4800;
509 break;
510 case 9600:
511 case CBR_9600:
512 port.c_cflag |= B9600;
513 break;
514 case 19200:
515 case CBR_19200:
516 port.c_cflag |= B19200;
517 break;
518 case 38400:
519 case CBR_38400:
520 port.c_cflag |= B38400;
521 break;
522 default:
523 commerror = IE_BAUDRATE;
524 return -1;
526 #else
527 switch (lpdcb->BaudRate) {
528 case 110:
529 case CBR_110:
530 port.c_ospeed = B110;
531 break;
532 case 300:
533 case CBR_300:
534 port.c_ospeed = B300;
535 break;
536 case 600:
537 case CBR_600:
538 port.c_ospeed = B600;
539 break;
540 case 1200:
541 case CBR_1200:
542 port.c_ospeed = B1200;
543 break;
544 case 2400:
545 case CBR_2400:
546 port.c_ospeed = B2400;
547 break;
548 case 4800:
549 case CBR_4800:
550 port.c_ospeed = B4800;
551 break;
552 case 9600:
553 case CBR_9600:
554 port.c_ospeed = B9600;
555 break;
556 case 19200:
557 case CBR_19200:
558 port.c_ospeed = B19200;
559 break;
560 case 38400:
561 case CBR_38400:
562 port.c_ospeed = B38400;
563 break;
564 default:
565 commerror = IE_BAUDRATE;
566 return -1;
568 port.c_ispeed = port.c_ospeed;
569 #endif
570 dprintf_comm(stddeb,"SetCommState: bytesize %d\n",lpdcb->ByteSize);
571 port.c_cflag &= ~CSIZE;
572 switch (lpdcb->ByteSize) {
573 case 5:
574 port.c_cflag |= CS5;
575 break;
576 case 6:
577 port.c_cflag |= CS6;
578 break;
579 case 7:
580 port.c_cflag |= CS7;
581 break;
582 case 8:
583 port.c_cflag |= CS8;
584 break;
585 default:
586 commerror = IE_BYTESIZE;
587 return -1;
590 dprintf_comm(stddeb,"SetCommState: parity %d\n",lpdcb->Parity);
591 port.c_cflag &= ~(PARENB | PARODD);
592 if (lpdcb->fParity)
593 switch (lpdcb->Parity) {
594 case NOPARITY:
595 port.c_iflag &= ~INPCK;
596 break;
597 case ODDPARITY:
598 port.c_cflag |= (PARENB | PARODD);
599 port.c_iflag |= INPCK;
600 break;
601 case EVENPARITY:
602 port.c_cflag |= PARENB;
603 port.c_iflag |= INPCK;
604 break;
605 default:
606 commerror = IE_BYTESIZE;
607 return -1;
611 dprintf_comm(stddeb,"SetCommState: stopbits %d\n",lpdcb->StopBits);
612 switch (lpdcb->StopBits) {
613 case ONESTOPBIT:
614 port.c_cflag &= ~CSTOPB;
615 break;
616 case TWOSTOPBITS:
617 port.c_cflag |= CSTOPB;
618 break;
619 default:
620 commerror = IE_BYTESIZE;
621 return -1;
623 #ifndef __svr4__
625 if (lpdcb->fDtrflow || lpdcb->fRtsflow || lpdcb->fOutxCtsFlow)
626 port.c_cflag |= CRTSCTS;
628 if (lpdcb->fDtrDisable)
629 port.c_cflag &= ~CRTSCTS;
630 #endif
631 if (lpdcb->fInX)
632 port.c_iflag |= IXON;
633 if (lpdcb->fOutX)
634 port.c_iflag |= IXOFF;
636 if (tcsetattr(lpdcb->Id, TCSADRAIN, &port) == -1) {
637 commerror = WinError();
638 return -1;
639 } else {
640 commerror = 0;
641 return 0;
645 int GetCommState(int fd, DCB FAR *lpdcb)
647 struct termios port;
649 dprintf_comm(stddeb,"GetCommState: fd %d, ptr %p\n", fd, lpdcb);
650 if (tcgetattr(fd, &port) == -1) {
651 commerror = WinError();
652 return -1;
655 lpdcb->Id = fd;
657 #ifdef CBAUD
658 switch (port.c_cflag & CBAUD) {
659 #else
660 switch (port.c_ospeed) {
661 #endif
662 case B110:
663 lpdcb->BaudRate = 110;
664 break;
665 case B300:
666 lpdcb->BaudRate = 300;
667 break;
668 case B600:
669 lpdcb->BaudRate = 600;
670 break;
671 case B1200:
672 lpdcb->BaudRate = 1200;
673 break;
674 case B2400:
675 lpdcb->BaudRate = 2400;
676 break;
677 case B4800:
678 lpdcb->BaudRate = 4800;
679 break;
680 case B9600:
681 lpdcb->BaudRate = 9600;
682 break;
683 case B19200:
684 lpdcb->BaudRate = 19200;
685 break;
686 case B38400:
687 lpdcb->BaudRate = 38400;
688 break;
691 switch (port.c_cflag & CSIZE) {
692 case CS5:
693 lpdcb->ByteSize = 5;
694 break;
695 case CS6:
696 lpdcb->ByteSize = 6;
697 break;
698 case CS7:
699 lpdcb->ByteSize = 7;
700 break;
701 case CS8:
702 lpdcb->ByteSize = 8;
703 break;
706 switch (port.c_cflag & ~(PARENB | PARODD)) {
707 case 0:
708 lpdcb->fParity = NOPARITY;
709 break;
710 case PARENB:
711 lpdcb->fParity = EVENPARITY;
712 break;
713 case (PARENB | PARODD):
714 lpdcb->fParity = ODDPARITY;
715 break;
718 if (port.c_cflag & CSTOPB)
719 lpdcb->StopBits = TWOSTOPBITS;
720 else
721 lpdcb->StopBits = ONESTOPBIT;
723 lpdcb->RlsTimeout = 50;
724 lpdcb->CtsTimeout = 50;
725 lpdcb->DsrTimeout = 50;
726 lpdcb->fNull = 0;
727 lpdcb->fChEvt = 0;
728 lpdcb->fBinary = 1;
729 lpdcb->fDtrDisable = 0;
731 #ifndef __svr4__
733 if (port.c_cflag & CRTSCTS) {
734 lpdcb->fDtrflow = 1;
735 lpdcb->fRtsflow = 1;
736 lpdcb->fOutxCtsFlow = 1;
737 lpdcb->fOutxDsrFlow = 1;
738 } else
739 #endif
740 lpdcb->fDtrDisable = 1;
742 if (port.c_iflag & IXON)
743 lpdcb->fInX = 1;
744 else
745 lpdcb->fInX = 0;
747 if (port.c_iflag & IXOFF)
748 lpdcb->fOutX = 1;
749 else
750 lpdcb->fOutX = 0;
752 lpdcb->XonChar =
753 lpdcb->XoffChar =
755 lpdcb->XonLim = 10;
756 lpdcb->XoffLim = 10;
758 commerror = 0;
759 return 0;
762 int TransmitCommChar(int fd, char chTransmit)
764 struct DosDeviceStruct *ptr;
766 dprintf_comm(stddeb,
767 "TransmitCommChar: fd %d, data %d \n", fd, chTransmit);
768 if ((ptr = GetDeviceStruct(fd)) == NULL) {
769 commerror = IE_BADID;
770 return -1;
773 if (ptr->suspended) {
774 commerror = IE_HARDWARE;
775 return -1;
778 if (write(fd, (void *) &chTransmit, 1) == -1) {
779 commerror = WinError();
780 return -1;
781 } else {
782 commerror = 0;
783 return 0;
787 int UngetCommChar(int fd, char chUnget)
789 struct DosDeviceStruct *ptr;
791 dprintf_comm(stddeb,"UngetCommChar: fd %d (char %d)\n", fd, chUnget);
792 if ((ptr = GetDeviceStruct(fd)) == NULL) {
793 commerror = IE_BADID;
794 return -1;
797 if (ptr->suspended) {
798 commerror = IE_HARDWARE;
799 return -1;
802 ptr->unget = 1;
803 ptr->unget_byte = chUnget;
805 commerror = 0;
806 return 0;
809 int ReadComm(int fd, LPSTR lpvBuf, int cbRead)
811 int status, length;
812 struct DosDeviceStruct *ptr;
814 dprintf_comm(stddeb,
815 "ReadComm: fd %d, ptr %p, length %d\n", fd, lpvBuf, cbRead);
816 if ((ptr = GetDeviceStruct(fd)) == NULL) {
817 commerror = IE_BADID;
818 return -1;
821 if (ptr->suspended) {
822 commerror = IE_HARDWARE;
823 return -1;
826 if (ptr->unget) {
827 *lpvBuf = ptr->unget_byte;
828 lpvBuf++;
829 ptr->unget = 0;
831 length = 1;
832 } else
833 length = 0;
835 status = read(fd, (void *) lpvBuf, cbRead);
837 if (status == -1) {
838 if (errno != EAGAIN) {
839 commerror = WinError();
840 return -1 - length;
841 } else {
842 commerror = 0;
843 return length;
845 } else {
846 commerror = 0;
847 return length + status;
851 int WriteComm(int fd, LPSTR lpvBuf, int cbWrite)
853 int x, length;
854 struct DosDeviceStruct *ptr;
856 dprintf_comm(stddeb,"WriteComm: fd %d, ptr %p, length %d\n",
857 fd, lpvBuf, cbWrite);
858 if ((ptr = GetDeviceStruct(fd)) == NULL) {
859 commerror = IE_BADID;
860 return -1;
863 if (ptr->suspended) {
864 commerror = IE_HARDWARE;
865 return -1;
868 for (x=0; x != cbWrite ; x++)
869 dprintf_comm(stddeb,"%c", *(lpvBuf + x) );
871 length = write(fd, (void *) lpvBuf, cbWrite);
873 if (length == -1) {
874 commerror = WinError();
875 return -1;
876 } else {
877 commerror = 0;
878 return length;