2 * DEC 93 Erik Bos <erik@xs4all.nl>
4 * Copyright 1996 Marcus Meissner
6 * Mar 31, 1999. Ove Kåven <ovek@arcticnet.no>
7 * - Implemented buffers and EnableCommNotification.
9 * Apr 3, 1999. Lawson Whitney <lawson_whitney@juno.com>
10 * - Fixed the modem control part of EscapeCommFunction16.
12 * Mar 3, 1999. Ove Kåven <ovek@arcticnet.no>
13 * - Use port indices instead of unixfds for win16
14 * - Moved things around (separated win16 and win32 routines)
15 * - Added some hints on how to implement buffers and EnableCommNotification.
17 * May 26, 1997. Fixes and comments by Rick Richardson <rick@dgii.com> [RER]
18 * - ptr->fd wasn't getting cleared on close.
19 * - GetCommEventMask() and GetCommError() didn't do much of anything.
20 * IMHO, they are still wrong, but they at least implement the RXCHAR
21 * event and return I/O queue sizes, which makes the app I'm interested
22 * in (analog devices EZKIT DSP development system) work.
24 * August 12, 1997. Take a bash at SetCommEventMask - Lawson Whitney
25 * <lawson_whitney@juno.com>
26 * July 6, 1998. Fixes and comments by Valentijn Sessink
27 * <vsessink@ic.uva.nl> [V]
28 * Oktober 98, Rein Klazes [RHK]
29 * A program that wants to monitor the modem status line (RLSD/DCD) may
30 * poll the modem status register in the commMask structure. I update the bit
31 * in GetCommError, waiting for an implementation of communication events.
48 #ifdef HAVE_SYS_FILIO_H
49 # include <sys/filio.h>
51 #include <sys/ioctl.h>
56 #include "wine/winuser16.h"
58 #ifdef HAVE_SYS_MODEM_H
59 # include <sys/modem.h>
61 #ifdef HAVE_SYS_STRTIO_H
62 # include <sys/strtio.h>
73 #include "debugtools.h"
75 DEFAULT_DEBUG_CHANNEL(comm
);
78 #define TIOCINQ FIONREAD
80 #define COMM_MSR_OFFSET 35 /* see knowledge base Q101417 */
83 struct DosDeviceStruct COM
[MAX_PORTS
];
84 struct DosDeviceStruct LPT
[MAX_PORTS
];
85 /* pointers to unknown(==undocumented) comm structure */
86 LPCVOID
*unknown
[MAX_PORTS
];
87 /* save terminal states */
88 static struct termios m_stat
[MAX_PORTS
];
93 char option
[10], temp
[256], *btemp
;
96 for (x
=0; x
!=MAX_PORTS
; x
++) {
97 strcpy(option
,"COMx");
101 PROFILE_GetWineIniString( "serialports", option
, "*",
102 temp
, sizeof(temp
) );
103 if (!strcmp(temp
, "*") || *temp
== '\0')
104 COM
[x
].devicename
= NULL
;
106 btemp
= strchr(temp
,',');
109 COM
[x
].baudrate
= atoi(btemp
);
111 COM
[x
].baudrate
= -1;
114 if (!S_ISCHR(st
.st_mode
))
115 WARN("Can't use `%s' as %s !\n", temp
, option
);
117 if ((COM
[x
].devicename
= malloc(strlen(temp
)+1)) == NULL
)
118 WARN("Can't malloc for device info!\n");
121 strcpy(COM
[x
].devicename
, temp
);
123 TRACE("%s = %s\n", option
, COM
[x
].devicename
);
126 strcpy(option
, "LPTx");
130 PROFILE_GetWineIniString( "parallelports", option
, "*",
131 temp
, sizeof(temp
) );
132 if (!strcmp(temp
, "*") || *temp
== '\0')
133 LPT
[x
].devicename
= NULL
;
136 if (!S_ISCHR(st
.st_mode
))
137 WARN("Can't use `%s' as %s !\n", temp
, option
);
139 if ((LPT
[x
].devicename
= malloc(strlen(temp
)+1)) == NULL
)
140 WARN("Can't malloc for device info!\n");
143 strcpy(LPT
[x
].devicename
, temp
);
145 TRACE("%s = %s\n", option
, LPT
[x
].devicename
);
152 static struct DosDeviceStruct
*GetDeviceStruct(int fd
)
154 if ((fd
&0x7F)<=MAX_PORTS
) {
155 if (!(fd
&FLAG_LPT
)) {
168 static int GetCommPort_fd(int fd
)
172 for (x
=0; x
<MAX_PORTS
; x
++) {
180 static int ValidCOMPort(int x
)
182 return(x
< MAX_PORTS
? (int) COM
[x
].devicename
: 0);
185 static int ValidLPTPort(int x
)
187 return(x
< MAX_PORTS
? (int) LPT
[x
].devicename
: 0);
190 static int WinError(void)
192 TRACE("errno = %d\n", errno
);
199 static unsigned comm_inbuf(struct DosDeviceStruct
*ptr
)
201 return ((ptr
->ibuf_tail
> ptr
->ibuf_head
) ? ptr
->ibuf_size
: 0)
202 + ptr
->ibuf_head
- ptr
->ibuf_tail
;
205 static unsigned comm_outbuf(struct DosDeviceStruct
*ptr
)
207 return ((ptr
->obuf_tail
> ptr
->obuf_head
) ? ptr
->obuf_size
: 0)
208 + ptr
->obuf_head
- ptr
->obuf_tail
;
211 static int COMM_WhackModem(int fd
, unsigned int andy
, unsigned int orrie
)
213 unsigned int mstat
, okay
;
214 okay
= ioctl(fd
, TIOCMGET
, &mstat
);
215 if (okay
) return okay
;
216 if (andy
) mstat
&= andy
;
218 return ioctl(fd
, TIOCMSET
, &mstat
);
221 static void CALLBACK
comm_notification( ULONG_PTR
private )
223 struct DosDeviceStruct
*ptr
= (struct DosDeviceStruct
*)private;
224 int prev
, bleft
, len
;
226 int cid
= GetCommPort_fd(ptr
->fd
);
228 TRACE("async notification\n");
229 /* read data from comm port */
230 prev
= comm_inbuf(ptr
);
232 bleft
= ((ptr
->ibuf_tail
> ptr
->ibuf_head
) ? (ptr
->ibuf_tail
-1) : ptr
->ibuf_size
)
234 len
= read(ptr
->fd
, ptr
->inbuf
+ ptr
->ibuf_head
, bleft
?bleft
:1);
237 ptr
->commerror
= CE_RXOVER
;
239 /* check for events */
240 if ((ptr
->eventmask
& EV_RXFLAG
) &&
241 memchr(ptr
->inbuf
+ ptr
->ibuf_head
, ptr
->evtchar
, len
)) {
242 *(WORD
*)(unknown
[cid
]) |= EV_RXFLAG
;
245 if (ptr
->eventmask
& EV_RXCHAR
) {
246 *(WORD
*)(unknown
[cid
]) |= EV_RXCHAR
;
249 /* advance buffer position */
250 ptr
->ibuf_head
+= len
;
251 if (ptr
->ibuf_head
>= ptr
->ibuf_size
)
256 /* check for notification */
257 if (ptr
->wnd
&& (ptr
->n_read
>0) && (prev
<ptr
->n_read
) &&
258 (comm_inbuf(ptr
)>=ptr
->n_read
)) {
259 /* passed the receive notification threshold */
263 /* write any TransmitCommChar character */
265 len
= write(ptr
->fd
, &(ptr
->xmit
), 1);
266 if (len
> 0) ptr
->xmit
= -1;
268 /* write from output queue */
269 prev
= comm_outbuf(ptr
);
271 bleft
= ((ptr
->obuf_tail
<= ptr
->obuf_head
) ? ptr
->obuf_head
: ptr
->obuf_size
)
273 len
= bleft
? write(ptr
->fd
, ptr
->outbuf
+ ptr
->obuf_tail
, bleft
) : 0;
275 ptr
->obuf_tail
+= len
;
276 if (ptr
->obuf_tail
>= ptr
->obuf_size
)
279 if (ptr
->obuf_tail
== ptr
->obuf_head
) {
281 SERVICE_Delete( ptr
->s_write
);
282 ptr
->s_write
= INVALID_HANDLE_VALUE
;
284 if (ptr
->eventmask
& EV_TXEMPTY
) {
285 *(WORD
*)(unknown
[cid
]) |= EV_TXEMPTY
;
291 /* check for notification */
292 if (ptr
->wnd
&& (ptr
->n_write
>0) && (prev
>=ptr
->n_write
) &&
293 (comm_outbuf(ptr
)<ptr
->n_write
)) {
294 /* passed the transmit notification threshold */
298 /* send notifications, if any */
299 if (ptr
->wnd
&& mask
) {
300 TRACE("notifying %04x: cid=%d, mask=%02x\n", ptr
->wnd
, cid
, mask
);
301 PostMessage16(ptr
->wnd
, WM_COMMNOTIFY
, cid
, mask
);
305 static void comm_waitread(struct DosDeviceStruct
*ptr
)
307 if (ptr
->s_read
!= INVALID_HANDLE_VALUE
) return;
308 ptr
->s_read
= SERVICE_AddObject( FILE_DupUnixHandle( ptr
->fd
,
309 GENERIC_READ
| SYNCHRONIZE
),
314 static void comm_waitwrite(struct DosDeviceStruct
*ptr
)
316 if (ptr
->s_write
!= INVALID_HANDLE_VALUE
) return;
317 ptr
->s_write
= SERVICE_AddObject( FILE_DupUnixHandle( ptr
->fd
,
318 GENERIC_WRITE
| SYNCHRONIZE
),
323 /**************************************************************************
324 * BuildCommDCB (USER.213)
326 BOOL16 WINAPI
BuildCommDCB16(LPCSTR device
, LPDCB16 lpdcb
)
328 /* "COM1:9600,n,8,1" */
331 char *ptr
, temp
[256];
333 TRACE("(%s), ptr %p\n", device
, lpdcb
);
335 if (!lstrncmpiA(device
,"COM",3)) {
336 port
= device
[3] - '0';
340 ERR("BUG ! COM0 can't exist!.\n");
344 if (!ValidCOMPort(port
)) {
345 FIXME("invalid COM port %d?\n",port
);
349 memset(lpdcb
, 0, sizeof(DCB16
)); /* initialize */
356 if (*(device
+4) != ':')
359 strcpy(temp
,device
+5);
360 ptr
= strtok(temp
, ", ");
362 if (COM
[port
].baudrate
> 0)
363 lpdcb
->BaudRate
= COM
[port
].baudrate
;
365 lpdcb
->BaudRate
= atoi(ptr
);
366 TRACE("baudrate (%d)\n", lpdcb
->BaudRate
);
368 ptr
= strtok(NULL
, ", ");
370 *ptr
= toupper(*ptr
);
372 TRACE("parity (%c)\n", *ptr
);
373 lpdcb
->fParity
= TRUE
;
376 lpdcb
->Parity
= NOPARITY
;
377 lpdcb
->fParity
= FALSE
;
380 lpdcb
->Parity
= EVENPARITY
;
383 lpdcb
->Parity
= MARKPARITY
;
386 lpdcb
->Parity
= ODDPARITY
;
389 WARN("Unknown parity `%c'!\n", *ptr
);
393 ptr
= strtok(NULL
, ", ");
394 TRACE("charsize (%c)\n", *ptr
);
395 lpdcb
->ByteSize
= *ptr
- '0';
397 ptr
= strtok(NULL
, ", ");
398 TRACE("stopbits (%c)\n", *ptr
);
401 lpdcb
->StopBits
= ONESTOPBIT
;
404 lpdcb
->StopBits
= TWOSTOPBITS
;
407 WARN("Unknown # of stopbits `%c'!\n", *ptr
);
415 /*****************************************************************************
416 * OpenComm (USER.200)
418 INT16 WINAPI
OpenComm16(LPCSTR device
,UINT16 cbInQueue
,UINT16 cbOutQueue
)
422 TRACE("%s, %d, %d\n", device
, cbInQueue
, cbOutQueue
);
424 if (strlen(device
) < 4)
427 port
= device
[3] - '0';
430 ERR("BUG ! COM0 or LPT0 don't exist !\n");
432 if (!lstrncmpiA(device
,"COM",3)) {
434 TRACE("%s = %s\n", device
, COM
[port
].devicename
);
436 if (!ValidCOMPort(port
))
442 fd
= open(COM
[port
].devicename
, O_RDWR
| O_NONBLOCK
);
444 ERR("error=%d\n", errno
);
447 unknown
[port
] = SEGPTR_ALLOC(40);
448 bzero(unknown
[port
],40);
450 COM
[port
].commerror
= 0;
451 COM
[port
].eventmask
= 0;
452 COM
[port
].evtchar
= 0; /* FIXME: default? */
453 /* save terminal state */
454 tcgetattr(fd
,&m_stat
[port
]);
455 /* set default parameters */
456 if(COM
[port
].baudrate
>-1){
458 GetCommState16(port
, &dcb
);
459 dcb
.BaudRate
=COM
[port
].baudrate
;
461 * databits, parity, stopbits
463 SetCommState16( &dcb
);
465 /* init priority characters */
466 COM
[port
].unget
= -1;
468 /* allocate buffers */
469 COM
[port
].ibuf_size
= cbInQueue
;
470 COM
[port
].ibuf_head
= COM
[port
].ibuf_tail
= 0;
471 COM
[port
].obuf_size
= cbOutQueue
;
472 COM
[port
].obuf_head
= COM
[port
].obuf_tail
= 0;
474 COM
[port
].inbuf
= malloc(cbInQueue
);
475 if (COM
[port
].inbuf
) {
476 COM
[port
].outbuf
= malloc(cbOutQueue
);
477 if (!COM
[port
].outbuf
)
478 free(COM
[port
].inbuf
);
479 } else COM
[port
].outbuf
= NULL
;
480 if (!COM
[port
].outbuf
) {
481 /* not enough memory */
482 tcsetattr(COM
[port
].fd
,TCSANOW
,&m_stat
[port
]);
484 ERR("out of memory");
488 COM
[port
].s_read
= INVALID_HANDLE_VALUE
;
489 COM
[port
].s_write
= INVALID_HANDLE_VALUE
;
490 comm_waitread( &COM
[port
] );
495 if (!lstrncmpiA(device
,"LPT",3)) {
497 if (!ValidLPTPort(port
))
503 fd
= open(LPT
[port
].devicename
, O_RDWR
| O_NONBLOCK
, 0);
508 LPT
[port
].commerror
= 0;
509 LPT
[port
].eventmask
= 0;
510 return port
|FLAG_LPT
;
516 /*****************************************************************************
517 * CloseComm (USER.207)
519 INT16 WINAPI
CloseComm16(INT16 cid
)
521 struct DosDeviceStruct
*ptr
;
523 TRACE("cid=%d\n", cid
);
524 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
525 FIXME("no cid=%d found!\n", cid
);
528 if (!(cid
&FLAG_LPT
)) {
530 SEGPTR_FREE(unknown
[cid
]); /* [LW] */
532 SERVICE_Delete( COM
[cid
].s_write
);
533 SERVICE_Delete( COM
[cid
].s_read
);
538 /* reset modem lines */
539 tcsetattr(ptr
->fd
,TCSANOW
,&m_stat
[cid
]);
542 if (close(ptr
->fd
) == -1) {
543 ptr
->commerror
= WinError();
544 /* FIXME: should we clear ptr->fd here? */
553 /*****************************************************************************
554 * SetCommBreak (USER.210)
556 INT16 WINAPI
SetCommBreak16(INT16 cid
)
558 struct DosDeviceStruct
*ptr
;
560 TRACE("cid=%d\n", cid
);
561 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
562 FIXME("no cid=%d found!\n", cid
);
571 /*****************************************************************************
572 * ClearCommBreak (USER.211)
574 INT16 WINAPI
ClearCommBreak16(INT16 cid
)
576 struct DosDeviceStruct
*ptr
;
578 TRACE("cid=%d\n", cid
);
579 if (!(ptr
= GetDeviceStruct(cid
))) {
580 FIXME("no cid=%d found!\n", cid
);
588 /*****************************************************************************
589 * EscapeCommFunction (USER.214)
591 LONG WINAPI
EscapeCommFunction16(UINT16 cid
,UINT16 nFunction
)
594 struct DosDeviceStruct
*ptr
;
597 TRACE("cid=%d, function=%d\n", cid
, nFunction
);
598 if ((nFunction
!= GETMAXCOM
) && (nFunction
!= GETMAXLPT
)) {
599 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
600 FIXME("no cid=%d found!\n", cid
);
603 if (tcgetattr(ptr
->fd
,&port
) == -1) {
604 TRACE("tcgetattr failed\n");
605 ptr
->commerror
=WinError();
616 TRACE("GETMAXCOM\n");
617 for (max
= MAX_PORTS
;!COM
[max
].devicename
;max
--)
623 TRACE("GETMAXLPT\n");
624 for (max
= MAX_PORTS
;!LPT
[max
].devicename
;max
--)
626 return FLAG_LPT
+ max
;
630 TRACE("GETBASEIRQ\n");
631 /* FIXME: use tables */
632 /* just fake something for now */
633 if (cid
& FLAG_LPT
) {
634 /* LPT1: irq 7, LPT2: irq 5 */
635 return (cid
& 0x7f) ? 5 : 7;
637 /* COM1: irq 4, COM2: irq 3,
638 COM3: irq 4, COM4: irq 3 */
639 return 4 - (cid
& 1);
646 return COMM_WhackModem(ptr
->fd
, ~TIOCM_DTR
, 0);
651 return COMM_WhackModem(ptr
->fd
, ~TIOCM_RTS
, 0);
657 return COMM_WhackModem(ptr
->fd
, 0, TIOCM_DTR
);
663 return COMM_WhackModem(ptr
->fd
, 0, TIOCM_RTS
);
668 port
.c_iflag
|= IXOFF
;
673 port
.c_iflag
|= IXON
;
677 WARN("(cid=%d,nFunction=%d): Unknown function\n",
682 if (tcsetattr(ptr
->fd
, TCSADRAIN
, &port
) == -1) {
683 ptr
->commerror
= WinError();
691 /*****************************************************************************
692 * FlushComm (USER.215)
694 INT16 WINAPI
FlushComm16(INT16 cid
,INT16 fnQueue
)
697 struct DosDeviceStruct
*ptr
;
699 TRACE("cid=%d, queue=%d\n", cid
, fnQueue
);
700 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
701 FIXME("no cid=%d found!\n", cid
);
707 ptr
->obuf_tail
= ptr
->obuf_head
;
711 ptr
->ibuf_head
= ptr
->ibuf_tail
;
714 WARN("(cid=%d,fnQueue=%d):Unknown queue\n",
718 if (tcflush(ptr
->fd
, queue
)) {
719 ptr
->commerror
= WinError();
727 /********************************************************************
728 * GetCommError (USER.203)
730 INT16 WINAPI
GetCommError16(INT16 cid
,LPCOMSTAT16 lpStat
)
733 struct DosDeviceStruct
*ptr
;
737 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
738 FIXME("no handle for cid = %0x!.\n",cid
);
742 WARN(" cid %d not comm port\n",cid
);
745 stol
= (unsigned char *)unknown
[cid
] + COMM_MSR_OFFSET
;
746 ioctl(ptr
->fd
,TIOCMGET
,&mstat
);
747 if( mstat
&TIOCM_CAR
)
755 lpStat
->cbOutQue
= comm_outbuf(ptr
);
756 lpStat
->cbInQue
= comm_inbuf(ptr
);
758 TRACE("cid %d, error %d, lpStat %d %d %d stol %x\n",
759 cid
, ptr
->commerror
, lpStat
->status
, lpStat
->cbInQue
,
760 lpStat
->cbOutQue
, *stol
);
763 TRACE("cid %d, error %d, lpStat NULL stol %x\n",
764 cid
, ptr
->commerror
, *stol
);
766 /* Return any errors and clear it */
767 temperror
= ptr
->commerror
;
772 /*****************************************************************************
773 * SetCommEventMask (USER.208)
775 SEGPTR WINAPI
SetCommEventMask16(INT16 cid
,UINT16 fuEvtMask
)
777 struct DosDeviceStruct
*ptr
;
782 TRACE("cid %d,mask %d\n",cid
,fuEvtMask
);
783 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
784 FIXME("no handle for cid = %0x!.\n",cid
);
788 ptr
->eventmask
= fuEvtMask
;
790 if ((cid
&FLAG_LPT
) || !ValidCOMPort(cid
)) {
791 WARN(" cid %d not comm port\n",cid
);
794 /* it's a COM port ? -> modify flags */
795 stol
= (unsigned char *)unknown
[cid
] + COMM_MSR_OFFSET
;
796 repid
= ioctl(ptr
->fd
,TIOCMGET
,&mstat
);
797 TRACE(" ioctl %d, msr %x at %p %p\n",repid
,mstat
,stol
,unknown
[cid
]);
798 if ((mstat
&TIOCM_CAR
))
803 TRACE(" modem dcd construct %x\n",*stol
);
804 return SEGPTR_GET(unknown
[cid
]);
807 /*****************************************************************************
808 * GetCommEventMask (USER.209)
810 UINT16 WINAPI
GetCommEventMask16(INT16 cid
,UINT16 fnEvtClear
)
812 struct DosDeviceStruct
*ptr
;
815 TRACE("cid %d, mask %d\n", cid
, fnEvtClear
);
816 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
817 FIXME("no handle for cid = %0x!.\n",cid
);
821 if ((cid
&FLAG_LPT
) || !ValidCOMPort(cid
)) {
822 WARN(" cid %d not comm port\n",cid
);
826 events
= *(WORD
*)(unknown
[cid
]) & fnEvtClear
;
827 *(WORD
*)(unknown
[cid
]) &= ~fnEvtClear
;
831 /*****************************************************************************
832 * SetCommState16 (USER.201)
834 INT16 WINAPI
SetCommState16(LPDCB16 lpdcb
)
837 struct DosDeviceStruct
*ptr
;
839 TRACE("cid %d, ptr %p\n", lpdcb
->Id
, lpdcb
);
840 if ((ptr
= GetDeviceStruct(lpdcb
->Id
)) == NULL
) {
841 FIXME("no handle for cid = %0x!.\n",lpdcb
->Id
);
844 if (tcgetattr(ptr
->fd
, &port
) == -1) {
845 ptr
->commerror
= WinError();
850 port
.c_cc
[VTIME
] = 1;
853 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|IMAXBEL
);
855 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
);
857 port
.c_iflag
|= (IGNBRK
);
859 port
.c_oflag
&= ~(OPOST
);
861 port
.c_cflag
&= ~(HUPCL
);
862 port
.c_cflag
|= CLOCAL
| CREAD
;
864 port
.c_lflag
&= ~(ICANON
|ECHO
|ISIG
);
865 port
.c_lflag
|= NOFLSH
;
867 TRACE("baudrate %d\n",lpdcb
->BaudRate
);
869 port
.c_cflag
&= ~CBAUD
;
870 switch (lpdcb
->BaudRate
) {
873 port
.c_cflag
|= B110
;
877 port
.c_cflag
|= B300
;
881 port
.c_cflag
|= B600
;
885 port
.c_cflag
|= B1200
;
889 port
.c_cflag
|= B2400
;
893 port
.c_cflag
|= B4800
;
897 port
.c_cflag
|= B9600
;
901 port
.c_cflag
|= B19200
;
905 port
.c_cflag
|= B38400
;
909 port
.c_cflag
|= B57600
;
914 port
.c_cflag
|= B115200
;
918 ptr
->commerror
= IE_BAUDRATE
;
921 #elif !defined(__EMX__)
922 switch (lpdcb
->BaudRate
) {
925 port
.c_ospeed
= B110
;
929 port
.c_ospeed
= B300
;
933 port
.c_ospeed
= B600
;
937 port
.c_ospeed
= B1200
;
941 port
.c_ospeed
= B2400
;
945 port
.c_ospeed
= B4800
;
949 port
.c_ospeed
= B9600
;
953 port
.c_ospeed
= B19200
;
957 port
.c_ospeed
= B38400
;
960 ptr
->commerror
= IE_BAUDRATE
;
963 port
.c_ispeed
= port
.c_ospeed
;
965 TRACE("bytesize %d\n",lpdcb
->ByteSize
);
966 port
.c_cflag
&= ~CSIZE
;
967 switch (lpdcb
->ByteSize
) {
981 ptr
->commerror
= IE_BYTESIZE
;
985 TRACE("fParity %d Parity %d\n",lpdcb
->fParity
, lpdcb
->Parity
);
986 port
.c_cflag
&= ~(PARENB
| PARODD
);
988 port
.c_iflag
|= INPCK
;
990 port
.c_iflag
&= ~INPCK
;
991 switch (lpdcb
->Parity
) {
995 port
.c_cflag
|= (PARENB
| PARODD
);
998 port
.c_cflag
|= PARENB
;
1001 ptr
->commerror
= IE_BYTESIZE
;
1006 TRACE("stopbits %d\n",lpdcb
->StopBits
);
1008 switch (lpdcb
->StopBits
) {
1010 port
.c_cflag
&= ~CSTOPB
;
1013 port
.c_cflag
|= CSTOPB
;
1016 ptr
->commerror
= IE_BYTESIZE
;
1021 if (lpdcb
->fDtrflow
|| lpdcb
->fRtsflow
|| lpdcb
->fOutxCtsFlow
)
1022 port
.c_cflag
|= CRTSCTS
;
1024 if (lpdcb
->fDtrDisable
)
1025 port
.c_cflag
&= ~CRTSCTS
;
1028 port
.c_iflag
|= IXON
;
1030 port
.c_iflag
&= ~IXON
;
1032 port
.c_iflag
|= IXOFF
;
1034 port
.c_iflag
&= ~IXOFF
;
1036 ptr
->evtchar
= lpdcb
->EvtChar
;
1038 if (tcsetattr(ptr
->fd
, TCSADRAIN
, &port
) == -1) {
1039 ptr
->commerror
= WinError();
1047 /*****************************************************************************
1048 * GetCommState (USER.202)
1050 INT16 WINAPI
GetCommState16(INT16 cid
, LPDCB16 lpdcb
)
1053 struct DosDeviceStruct
*ptr
;
1054 struct termios port
;
1056 TRACE("cid %d, ptr %p\n", cid
, lpdcb
);
1057 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
1058 FIXME("no handle for cid = %0x!.\n",cid
);
1061 if (tcgetattr(ptr
->fd
, &port
) == -1) {
1062 ptr
->commerror
= WinError();
1068 speed
= port
.c_cflag
& CBAUD
;
1070 speed
= port
.c_ospeed
;
1074 lpdcb
->BaudRate
= 110;
1077 lpdcb
->BaudRate
= 300;
1080 lpdcb
->BaudRate
= 600;
1083 lpdcb
->BaudRate
= 1200;
1086 lpdcb
->BaudRate
= 2400;
1089 lpdcb
->BaudRate
= 4800;
1092 lpdcb
->BaudRate
= 9600;
1095 lpdcb
->BaudRate
= 19200;
1098 lpdcb
->BaudRate
= 38400;
1102 lpdcb
->BaudRate
= 57600;
1107 lpdcb
->BaudRate
= 57601;
1112 switch (port
.c_cflag
& CSIZE
) {
1114 lpdcb
->ByteSize
= 5;
1117 lpdcb
->ByteSize
= 6;
1120 lpdcb
->ByteSize
= 7;
1123 lpdcb
->ByteSize
= 8;
1127 if(port
.c_iflag
& INPCK
)
1128 lpdcb
->fParity
= TRUE
;
1130 lpdcb
->fParity
= FALSE
;
1131 switch (port
.c_cflag
& (PARENB
| PARODD
)) {
1133 lpdcb
->Parity
= NOPARITY
;
1136 lpdcb
->Parity
= EVENPARITY
;
1138 case (PARENB
| PARODD
):
1139 lpdcb
->Parity
= ODDPARITY
;
1143 if (port
.c_cflag
& CSTOPB
)
1144 lpdcb
->StopBits
= TWOSTOPBITS
;
1146 lpdcb
->StopBits
= ONESTOPBIT
;
1148 lpdcb
->RlsTimeout
= 50;
1149 lpdcb
->CtsTimeout
= 50;
1150 lpdcb
->DsrTimeout
= 50;
1154 lpdcb
->fDtrDisable
= 0;
1158 if (port
.c_cflag
& CRTSCTS
) {
1159 lpdcb
->fDtrflow
= 1;
1160 lpdcb
->fRtsflow
= 1;
1161 lpdcb
->fOutxCtsFlow
= 1;
1162 lpdcb
->fOutxDsrFlow
= 1;
1165 lpdcb
->fDtrDisable
= 1;
1167 if (port
.c_iflag
& IXON
)
1172 if (port
.c_iflag
& IXOFF
)
1181 lpdcb
->XoffLim
= 10;
1183 lpdcb
->EvtChar
= ptr
->evtchar
;
1189 /*****************************************************************************
1190 * TransmitCommChar (USER.206)
1192 INT16 WINAPI
TransmitCommChar16(INT16 cid
,CHAR chTransmit
)
1194 struct DosDeviceStruct
*ptr
;
1196 TRACE("cid %d, data %d \n", cid
, chTransmit
);
1197 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
1198 FIXME("no handle for cid = %0x!.\n",cid
);
1202 if (ptr
->suspended
) {
1203 ptr
->commerror
= IE_HARDWARE
;
1207 if (ptr
->xmit
>= 0) {
1208 /* character already queued */
1209 /* FIXME: which error would Windows return? */
1210 ptr
->commerror
= CE_TXFULL
;
1214 if (ptr
->obuf_head
== ptr
->obuf_tail
) {
1215 /* transmit queue empty, try to transmit directly */
1216 if (write(ptr
->fd
, &chTransmit
, 1) == -1) {
1217 /* didn't work, queue it */
1218 ptr
->xmit
= chTransmit
;
1219 comm_waitwrite(ptr
);
1222 /* data in queue, let this char be transmitted next */
1223 ptr
->xmit
= chTransmit
;
1224 comm_waitwrite(ptr
);
1231 /*****************************************************************************
1232 * UngetCommChar (USER.212)
1234 INT16 WINAPI
UngetCommChar16(INT16 cid
,CHAR chUnget
)
1236 struct DosDeviceStruct
*ptr
;
1238 TRACE("cid %d (char %d)\n", cid
, chUnget
);
1239 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
1240 FIXME("no handle for cid = %0x!.\n",cid
);
1244 if (ptr
->suspended
) {
1245 ptr
->commerror
= IE_HARDWARE
;
1249 if (ptr
->unget
>=0) {
1250 /* character already queued */
1251 /* FIXME: which error would Windows return? */
1252 ptr
->commerror
= CE_RXOVER
;
1256 ptr
->unget
= chUnget
;
1262 /*****************************************************************************
1263 * ReadComm (USER.204)
1265 INT16 WINAPI
ReadComm16(INT16 cid
,LPSTR lpvBuf
,INT16 cbRead
)
1268 struct DosDeviceStruct
*ptr
;
1269 LPSTR orgBuf
= lpvBuf
;
1271 TRACE("cid %d, ptr %p, length %d\n", cid
, lpvBuf
, cbRead
);
1272 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
1273 FIXME("no handle for cid = %0x!.\n",cid
);
1277 if (ptr
->suspended
) {
1278 ptr
->commerror
= IE_HARDWARE
;
1282 /* read unget character */
1283 if (ptr
->unget
>=0) {
1284 *lpvBuf
++ = ptr
->unget
;
1291 /* read from receive buffer */
1292 while (length
< cbRead
) {
1293 status
= ((ptr
->ibuf_head
< ptr
->ibuf_tail
) ?
1294 ptr
->ibuf_size
: ptr
->ibuf_head
) - ptr
->ibuf_tail
;
1296 if ((cbRead
- length
) < status
)
1297 status
= cbRead
- length
;
1299 memcpy(lpvBuf
, ptr
->inbuf
+ ptr
->ibuf_tail
, status
);
1300 ptr
->ibuf_tail
+= status
;
1301 if (ptr
->ibuf_tail
>= ptr
->ibuf_size
)
1307 TRACE("%.*s\n", length
, orgBuf
);
1312 /*****************************************************************************
1313 * WriteComm (USER.205)
1315 INT16 WINAPI
WriteComm16(INT16 cid
, LPSTR lpvBuf
, INT16 cbWrite
)
1318 struct DosDeviceStruct
*ptr
;
1320 TRACE("cid %d, ptr %p, length %d\n",
1321 cid
, lpvBuf
, cbWrite
);
1322 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
1323 FIXME("no handle for cid = %0x!.\n",cid
);
1327 if (ptr
->suspended
) {
1328 ptr
->commerror
= IE_HARDWARE
;
1332 TRACE("%.*s\n", cbWrite
, lpvBuf
);
1335 while (length
< cbWrite
) {
1336 if ((ptr
->obuf_head
== ptr
->obuf_tail
) && (ptr
->xmit
< 0)) {
1337 /* no data queued, try to write directly */
1338 status
= write(ptr
->fd
, lpvBuf
, cbWrite
- length
);
1345 /* can't write directly, put into transmit buffer */
1346 status
= ((ptr
->obuf_tail
> ptr
->obuf_head
) ?
1347 (ptr
->obuf_tail
-1) : ptr
->obuf_size
) - ptr
->obuf_head
;
1349 if ((cbWrite
- length
) < status
)
1350 status
= cbWrite
- length
;
1351 memcpy(lpvBuf
, ptr
->outbuf
+ ptr
->obuf_head
, status
);
1352 ptr
->obuf_head
+= status
;
1353 if (ptr
->obuf_head
>= ptr
->obuf_size
)
1357 comm_waitwrite(ptr
);
1364 /***********************************************************************
1365 * EnableCommNotification (USER.246)
1367 BOOL16 WINAPI
EnableCommNotification16( INT16 cid
, HWND16 hwnd
,
1368 INT16 cbWriteNotify
, INT16 cbOutQueue
)
1370 struct DosDeviceStruct
*ptr
;
1372 TRACE("(%d, %x, %d, %d)\n", cid
, hwnd
, cbWriteNotify
, cbOutQueue
);
1373 if ((ptr
= GetDeviceStruct(cid
)) == NULL
) {
1374 FIXME("no handle for cid = %0x!.\n",cid
);
1375 ptr
->commerror
= IE_BADID
;
1379 ptr
->n_read
= cbWriteNotify
;
1380 ptr
->n_write
= cbOutQueue
;
1385 /**************************************************************************
1386 * BuildCommDCBA (KERNEL32.14)
1388 BOOL WINAPI
BuildCommDCBA(LPCSTR device
,LPDCB lpdcb
)
1390 return BuildCommDCBAndTimeoutsA(device
,lpdcb
,NULL
);
1393 /**************************************************************************
1394 * BuildCommDCBAndTimeoutsA (KERNEL32.15)
1396 BOOL WINAPI
BuildCommDCBAndTimeoutsA(LPCSTR device
, LPDCB lpdcb
,
1397 LPCOMMTIMEOUTS lptimeouts
)
1402 TRACE("(%s,%p,%p)\n",device
,lpdcb
,lptimeouts
);
1404 if (!lstrncmpiA(device
,"COM",3)) {
1407 ERR("BUG! COM0 can't exists!.\n");
1410 if (!ValidCOMPort(port
))
1412 if (*(device
+4)!=':')
1414 temp
=(LPSTR
)(device
+5);
1418 memset(lpdcb
, 0, sizeof(DCB
)); /* initialize */
1420 lpdcb
->DCBlength
= sizeof(DCB
);
1421 if (strchr(temp
,',')) { /* old style */
1424 char last
=temp
[strlen(temp
)-1];
1426 ret
=BuildCommDCB16(device
,&dcb16
);
1429 lpdcb
->BaudRate
= dcb16
.BaudRate
;
1430 lpdcb
->ByteSize
= dcb16
.ByteSize
;
1431 lpdcb
->fBinary
= dcb16
.fBinary
;
1432 lpdcb
->Parity
= dcb16
.Parity
;
1433 lpdcb
->fParity
= dcb16
.fParity
;
1434 lpdcb
->fNull
= dcb16
.fNull
;
1435 lpdcb
->StopBits
= dcb16
.StopBits
;
1438 lpdcb
->fOutX
= TRUE
;
1439 lpdcb
->fOutxCtsFlow
= FALSE
;
1440 lpdcb
->fOutxDsrFlow
= FALSE
;
1441 lpdcb
->fDtrControl
= DTR_CONTROL_ENABLE
;
1442 lpdcb
->fRtsControl
= RTS_CONTROL_ENABLE
;
1443 } else if (last
=='p') {
1444 lpdcb
->fInX
= FALSE
;
1445 lpdcb
->fOutX
= FALSE
;
1446 lpdcb
->fOutxCtsFlow
= TRUE
;
1447 lpdcb
->fOutxDsrFlow
= TRUE
;
1448 lpdcb
->fDtrControl
= DTR_CONTROL_HANDSHAKE
;
1449 lpdcb
->fRtsControl
= RTS_CONTROL_HANDSHAKE
;
1451 lpdcb
->fInX
= FALSE
;
1452 lpdcb
->fOutX
= FALSE
;
1453 lpdcb
->fOutxCtsFlow
= FALSE
;
1454 lpdcb
->fOutxDsrFlow
= FALSE
;
1455 lpdcb
->fDtrControl
= DTR_CONTROL_ENABLE
;
1456 lpdcb
->fRtsControl
= RTS_CONTROL_ENABLE
;
1458 lpdcb
->XonChar
= dcb16
.XonChar
;
1459 lpdcb
->XoffChar
= dcb16
.XoffChar
;
1460 lpdcb
->ErrorChar
= dcb16
.PeChar
;
1461 lpdcb
->fErrorChar
= dcb16
.fPeChar
;
1462 lpdcb
->EofChar
= dcb16
.EofChar
;
1463 lpdcb
->EvtChar
= dcb16
.EvtChar
;
1464 lpdcb
->XonLim
= dcb16
.XonLim
;
1465 lpdcb
->XoffLim
= dcb16
.XoffLim
;
1468 ptr
=strtok(temp
," ");
1473 if (!strncmp("baud=",ptr
,5)) {
1474 if (!sscanf(ptr
+5,"%ld",&x
))
1475 WARN("Couldn't parse %s\n",ptr
);
1476 lpdcb
->BaudRate
= x
;
1479 if (!strncmp("stop=",ptr
,5)) {
1480 if (!sscanf(ptr
+5,"%ld",&x
))
1481 WARN("Couldn't parse %s\n",ptr
);
1482 lpdcb
->StopBits
= x
;
1485 if (!strncmp("data=",ptr
,5)) {
1486 if (!sscanf(ptr
+5,"%ld",&x
))
1487 WARN("Couldn't parse %s\n",ptr
);
1488 lpdcb
->ByteSize
= x
;
1491 if (!strncmp("parity=",ptr
,7)) {
1492 lpdcb
->fParity
= TRUE
;
1495 lpdcb
->fParity
= FALSE
;
1496 lpdcb
->Parity
= NOPARITY
;
1499 lpdcb
->Parity
= EVENPARITY
;
1502 lpdcb
->Parity
= ODDPARITY
;
1505 lpdcb
->Parity
= MARKPARITY
;
1511 ERR("Unhandled specifier '%s', please report.\n",ptr
);
1512 ptr
=strtok(NULL
," ");
1514 if (lpdcb
->BaudRate
==110)
1515 lpdcb
->StopBits
= 2;
1519 /**************************************************************************
1520 * BuildCommDCBAndTimeoutsW (KERNEL32.16)
1522 BOOL WINAPI
BuildCommDCBAndTimeoutsW( LPCWSTR devid
, LPDCB lpdcb
,
1523 LPCOMMTIMEOUTS lptimeouts
)
1528 TRACE("(%p,%p,%p)\n",devid
,lpdcb
,lptimeouts
);
1529 devidA
= HEAP_strdupWtoA( GetProcessHeap(), 0, devid
);
1530 ret
=BuildCommDCBAndTimeoutsA(devidA
,lpdcb
,lptimeouts
);
1531 HeapFree( GetProcessHeap(), 0, devidA
);
1535 /**************************************************************************
1536 * BuildCommDCBW (KERNEL32.17)
1538 BOOL WINAPI
BuildCommDCBW(LPCWSTR devid
,LPDCB lpdcb
)
1540 return BuildCommDCBAndTimeoutsW(devid
,lpdcb
,NULL
);
1543 /*****************************************************************************
1545 * Returns a file descriptor for reading.
1546 * Make sure to close the handle afterwards!
1548 static int COMM_GetReadFd( HANDLE handle
)
1551 struct get_read_fd_request
*req
= get_req_buffer();
1552 req
->handle
= handle
;
1553 server_call_fd( REQ_GET_READ_FD
, -1, &fd
);
1557 /*****************************************************************************
1559 * Returns a file descriptor for writing.
1560 * Make sure to close the handle afterwards!
1562 static int COMM_GetWriteFd( HANDLE handle
)
1565 struct get_write_fd_request
*req
= get_req_buffer();
1566 req
->handle
= handle
;
1567 server_call_fd( REQ_GET_WRITE_FD
, -1, &fd
);
1571 /* FIXME: having these global for win32 for now */
1572 int commerror
=0,eventmask
=0;
1574 /*****************************************************************************
1575 * SetCommBreak (KERNEL32.449)
1577 BOOL WINAPI
SetCommBreak(HANDLE handle
)
1579 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1582 fd
= COMM_GetWriteFd(handle
);
1584 TRACE("COMM_GetWriteFd failed\n");
1587 result
= ioctl(fd
,TIOCSBRK
,0);
1591 TRACE("ioctl failed\n");
1592 SetLastError(ERROR_NOT_SUPPORTED
);
1597 FIXME("ioctl not available\n");
1598 SetLastError(ERROR_NOT_SUPPORTED
);
1603 /*****************************************************************************
1604 * ClearCommBreak (KERNEL32.20)
1606 BOOL WINAPI
ClearCommBreak(HANDLE handle
)
1608 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1611 fd
= COMM_GetWriteFd(handle
);
1613 TRACE("COMM_GetWriteFd failed\n");
1616 result
= ioctl(fd
,TIOCCBRK
,0);
1620 TRACE("ioctl failed\n");
1621 SetLastError(ERROR_NOT_SUPPORTED
);
1626 FIXME("ioctl not available\n");
1627 SetLastError(ERROR_NOT_SUPPORTED
);
1632 /*****************************************************************************
1633 * EscapeCommFunction (KERNEL32.214)
1635 BOOL WINAPI
EscapeCommFunction(HANDLE handle
,UINT nFunction
)
1637 int fd
,direct
=FALSE
,result
=FALSE
;
1638 struct termios port
;
1640 TRACE("handle %d, function=%d\n", handle
, nFunction
);
1641 fd
= COMM_GetWriteFd(handle
);
1643 FIXME("handle %d not found.\n",handle
);
1647 if (tcgetattr(fd
,&port
) == -1) {
1648 commerror
=WinError();
1653 switch (nFunction
) {
1662 result
= COMM_WhackModem(fd
, ~TIOCM_DTR
, 0);
1670 result
= COMM_WhackModem(fd
, ~TIOCM_RTS
, 0);
1678 result
= COMM_WhackModem(fd
, 0, TIOCM_DTR
);
1686 result
= COMM_WhackModem(fd
, 0, TIOCM_RTS
);
1692 port
.c_iflag
|= IXOFF
;
1697 port
.c_iflag
|= IXON
;
1700 TRACE("setbreak\n");
1703 result
= ioctl(fd
,TIOCSBRK
,0);
1707 TRACE("clrbreak\n");
1710 result
= ioctl(fd
,TIOCCBRK
,0);
1714 WARN("(handle=%d,nFunction=%d): Unknown function\n",
1720 if (tcsetattr(fd
, TCSADRAIN
, &port
) == -1) {
1721 commerror
= WinError();
1731 commerror
=WinError();
1740 /********************************************************************
1741 * PurgeComm (KERNEL32.557)
1743 BOOL WINAPI
PurgeComm( HANDLE handle
, DWORD flags
)
1747 TRACE("handle %d, flags %lx\n", handle
, flags
);
1749 fd
= COMM_GetWriteFd(handle
);
1751 FIXME("no handle %d found\n",handle
);
1756 ** not exactly sure how these are different
1757 ** Perhaps if we had our own internal queues, one flushes them
1758 ** and the other flushes the kernel's buffers.
1760 if(flags
&PURGE_TXABORT
)
1761 tcflush(fd
,TCOFLUSH
);
1762 if(flags
&PURGE_RXABORT
)
1763 tcflush(fd
,TCIFLUSH
);
1764 if(flags
&PURGE_TXCLEAR
)
1765 tcflush(fd
,TCOFLUSH
);
1766 if(flags
&PURGE_RXCLEAR
)
1767 tcflush(fd
,TCIFLUSH
);
1773 /*****************************************************************************
1774 * ClearCommError (KERNEL32.21)
1776 BOOL WINAPI
ClearCommError(INT handle
,LPDWORD errors
,LPCOMSTAT lpStat
)
1780 fd
=COMM_GetReadFd(handle
);
1783 FIXME("no handle %d found\n",handle
);
1792 if(ioctl(fd
, TIOCOUTQ
, &lpStat
->cbOutQue
))
1793 WARN("ioctl returned error\n");
1795 lpStat
->cbOutQue
= 0; /* FIXME: find a different way to find out */
1798 if(ioctl(fd
, TIOCINQ
, &lpStat
->cbInQue
))
1799 WARN("ioctl returned error\n");
1801 TRACE("handle %d cbInQue = %ld cbOutQue = %ld\n",
1802 handle
, lpStat
->cbInQue
, lpStat
->cbOutQue
);
1811 ** After an asynchronous write opperation, the
1812 ** app will call ClearCommError to see if the
1813 ** results are ready yet. It waits for ERROR_IO_PENDING
1815 commerror
= ERROR_IO_PENDING
;
1820 /*****************************************************************************
1821 * SetupComm (KERNEL32.676)
1823 BOOL WINAPI
SetupComm( HANDLE handle
, DWORD insize
, DWORD outsize
)
1827 FIXME("insize %ld outsize %ld unimplemented stub\n", insize
, outsize
);
1828 fd
=COMM_GetWriteFd(handle
);
1830 FIXME("handle %d not found?\n",handle
);
1837 /*****************************************************************************
1838 * GetCommMask (KERNEL32.156)
1840 BOOL WINAPI
GetCommMask(HANDLE handle
,LPDWORD evtmask
)
1844 TRACE("handle %d, mask %p\n", handle
, evtmask
);
1845 if(0>(fd
=COMM_GetReadFd(handle
)))
1847 FIXME("no handle %d found\n",handle
);
1851 *evtmask
= eventmask
;
1852 TRACE("%s%s%s%s%s%s%s%s%s\n",
1853 (eventmask
&EV_BREAK
)?"EV_BREAK":"",
1854 (eventmask
&EV_CTS
)?"EV_CTS":"",
1855 (eventmask
&EV_DSR
)?"EV_DSR":"",
1856 (eventmask
&EV_ERR
)?"EV_ERR":"",
1857 (eventmask
&EV_RING
)?"EV_RING":"",
1858 (eventmask
&EV_RLSD
)?"EV_RLSD":"",
1859 (eventmask
&EV_RXCHAR
)?"EV_RXCHAR":"",
1860 (eventmask
&EV_RXFLAG
)?"EV_RXFLAG":"",
1861 (eventmask
&EV_TXEMPTY
)?"EV_TXEMPTY":"");
1866 /*****************************************************************************
1867 * SetCommMask (KERNEL32.451)
1869 BOOL WINAPI
SetCommMask(INT handle
,DWORD evtmask
)
1873 TRACE("handle %d, mask %lx\n", handle
, evtmask
);
1874 TRACE("%s%s%s%s%s%s%s%s%s\n",
1875 (evtmask
&EV_BREAK
)?"EV_BREAK":"",
1876 (evtmask
&EV_CTS
)?"EV_CTS":"",
1877 (evtmask
&EV_DSR
)?"EV_DSR":"",
1878 (evtmask
&EV_ERR
)?"EV_ERR":"",
1879 (evtmask
&EV_RING
)?"EV_RING":"",
1880 (evtmask
&EV_RLSD
)?"EV_RLSD":"",
1881 (evtmask
&EV_RXCHAR
)?"EV_RXCHAR":"",
1882 (evtmask
&EV_RXFLAG
)?"EV_RXFLAG":"",
1883 (evtmask
&EV_TXEMPTY
)?"EV_TXEMPTY":"");
1885 if(0>(fd
=COMM_GetWriteFd(handle
))) {
1886 FIXME("no handle %d found\n",handle
);
1890 eventmask
= evtmask
;
1894 /*****************************************************************************
1895 * SetCommState (KERNEL32.452)
1897 BOOL WINAPI
SetCommState(INT handle
,LPDCB lpdcb
)
1899 struct termios port
;
1902 TRACE("handle %d, ptr %p\n", handle
, lpdcb
);
1903 TRACE("bytesize %d baudrate %ld fParity %d Parity %d stopbits %d\n",
1904 lpdcb
->ByteSize
,lpdcb
->BaudRate
,lpdcb
->fParity
, lpdcb
->Parity
,
1905 (lpdcb
->StopBits
== ONESTOPBIT
)?1:
1906 (lpdcb
->StopBits
== TWOSTOPBITS
)?2:0);
1907 TRACE("%s %s\n",(lpdcb
->fInX
)?"IXON":"~IXON",
1908 (lpdcb
->fOutX
)?"IXOFF":"~IXOFF");
1910 if ((fd
= COMM_GetWriteFd(handle
)) < 0) {
1911 FIXME("no handle %d found\n",handle
);
1915 if ((tcgetattr(fd
,&port
)) == -1) {
1916 int save_error
= errno
;
1917 commerror
= WinError();
1919 #ifdef HAVE_STRERROR
1920 ERR("tcgetattr error '%s'\n", strerror(save_error
));
1922 ERR("tcgetattr error %d\n", save_error
);
1927 port
.c_cc
[VMIN
] = 0;
1928 port
.c_cc
[VTIME
] = 1;
1931 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|IMAXBEL
);
1933 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
);
1935 port
.c_iflag
|= (IGNBRK
);
1937 port
.c_oflag
&= ~(OPOST
);
1939 port
.c_cflag
&= ~(HUPCL
);
1940 port
.c_cflag
|= CLOCAL
| CREAD
;
1942 port
.c_lflag
&= ~(ICANON
|ECHO
|ISIG
);
1943 port
.c_lflag
|= NOFLSH
;
1946 ** MJM - removed default baudrate settings
1947 ** TRACE(comm,"baudrate %ld\n",lpdcb->BaudRate);
1950 port
.c_cflag
&= ~CBAUD
;
1951 switch (lpdcb
->BaudRate
) {
1954 port
.c_cflag
|= B110
;
1958 port
.c_cflag
|= B300
;
1962 port
.c_cflag
|= B600
;
1966 port
.c_cflag
|= B1200
;
1970 port
.c_cflag
|= B2400
;
1974 port
.c_cflag
|= B4800
;
1978 port
.c_cflag
|= B9600
;
1982 port
.c_cflag
|= B19200
;
1986 port
.c_cflag
|= B38400
;
1990 port
.c_cflag
|= B57600
;
1995 port
.c_cflag
|= B115200
;
2000 port
.c_cflag
|= B230400
;
2005 port
.c_cflag
|= B460800
;
2009 commerror
= IE_BAUDRATE
;
2011 ERR("baudrate %ld\n",lpdcb
->BaudRate
);
2014 #elif !defined(__EMX__)
2015 switch (lpdcb
->BaudRate
) {
2018 port
.c_ospeed
= B110
;
2022 port
.c_ospeed
= B300
;
2026 port
.c_ospeed
= B600
;
2030 port
.c_ospeed
= B1200
;
2034 port
.c_ospeed
= B2400
;
2038 port
.c_ospeed
= B4800
;
2042 port
.c_ospeed
= B9600
;
2046 port
.c_ospeed
= B19200
;
2050 port
.c_ospeed
= B38400
;
2053 commerror
= IE_BAUDRATE
;
2055 ERR("baudrate %d \n",lpdcb
->BaudRate
);
2058 port
.c_ispeed
= port
.c_ospeed
;
2060 port
.c_cflag
&= ~CSIZE
;
2061 switch (lpdcb
->ByteSize
) {
2063 port
.c_cflag
|= CS5
;
2066 port
.c_cflag
|= CS6
;
2069 port
.c_cflag
|= CS7
;
2072 port
.c_cflag
|= CS8
;
2075 commerror
= IE_BYTESIZE
;
2081 port
.c_cflag
&= ~(PARENB
| PARODD
);
2083 port
.c_iflag
|= INPCK
;
2085 port
.c_iflag
&= ~INPCK
;
2086 switch (lpdcb
->Parity
) {
2090 port
.c_cflag
|= (PARENB
| PARODD
);
2093 port
.c_cflag
|= PARENB
;
2096 commerror
= IE_BYTESIZE
;
2103 switch (lpdcb
->StopBits
) {
2105 port
.c_cflag
&= ~CSTOPB
;
2108 port
.c_cflag
|= CSTOPB
;
2111 commerror
= IE_BYTESIZE
;
2117 if ( lpdcb
->fOutxCtsFlow
||
2118 lpdcb
->fDtrControl
== DTR_CONTROL_ENABLE
||
2119 lpdcb
->fRtsControl
== RTS_CONTROL_ENABLE
2122 port
.c_cflag
|= CRTSCTS
;
2126 if (lpdcb
->fDtrControl
== DTR_CONTROL_DISABLE
)
2128 port
.c_cflag
&= ~CRTSCTS
;
2129 TRACE("~CRTSCTS\n");
2134 port
.c_iflag
|= IXON
;
2136 port
.c_iflag
&= ~IXON
;
2138 port
.c_iflag
|= IXOFF
;
2140 port
.c_iflag
&= ~IXOFF
;
2142 if (tcsetattr(fd
,TCSANOW
,&port
)==-1) { /* otherwise it hangs with pending input*/
2143 int save_error
=errno
;
2144 commerror
= WinError();
2146 #ifdef HAVE_STRERROR
2147 ERR("tcgetattr error '%s'\n", strerror(save_error
));
2149 ERR("tcgetattr error %d\n", save_error
);
2160 /*****************************************************************************
2161 * GetCommState (KERNEL32.159)
2163 BOOL WINAPI
GetCommState(INT handle
, LPDCB lpdcb
)
2165 struct termios port
;
2168 TRACE("handle %d, ptr %p\n", handle
, lpdcb
);
2170 if ((fd
= COMM_GetReadFd(handle
)) < 0)
2172 ERR("can't get COMM_GetReadFd\n");
2175 if (tcgetattr(fd
, &port
) == -1) {
2176 int save_error
=errno
;
2177 #ifdef HAVE_STRERROR
2178 ERR("tcgetattr error '%s'\n", strerror(save_error
));
2180 ERR("tcgetattr error %d\n", save_error
);
2182 commerror
= WinError();
2189 speed
= (port
.c_cflag
& CBAUD
);
2191 speed
= (cfgetospeed(&port
));
2195 lpdcb
->BaudRate
= 110;
2198 lpdcb
->BaudRate
= 300;
2201 lpdcb
->BaudRate
= 600;
2204 lpdcb
->BaudRate
= 1200;
2207 lpdcb
->BaudRate
= 2400;
2210 lpdcb
->BaudRate
= 4800;
2213 lpdcb
->BaudRate
= 9600;
2216 lpdcb
->BaudRate
= 19200;
2219 lpdcb
->BaudRate
= 38400;
2223 lpdcb
->BaudRate
= 57600;
2228 lpdcb
->BaudRate
= 115200;
2233 lpdcb
->BaudRate
= 230400;
2238 lpdcb
->BaudRate
= 460800;
2242 ERR("unknown speed %x \n",speed
);
2245 switch (port
.c_cflag
& CSIZE
) {
2247 lpdcb
->ByteSize
= 5;
2250 lpdcb
->ByteSize
= 6;
2253 lpdcb
->ByteSize
= 7;
2256 lpdcb
->ByteSize
= 8;
2259 ERR("unknown size %x \n",port
.c_cflag
& CSIZE
);
2262 if(port
.c_iflag
& INPCK
)
2263 lpdcb
->fParity
= TRUE
;
2265 lpdcb
->fParity
= FALSE
;
2266 switch (port
.c_cflag
& (PARENB
| PARODD
)) {
2268 lpdcb
->Parity
= NOPARITY
;
2271 lpdcb
->Parity
= EVENPARITY
;
2273 case (PARENB
| PARODD
):
2274 lpdcb
->Parity
= ODDPARITY
;
2278 if (port
.c_cflag
& CSTOPB
)
2279 lpdcb
->StopBits
= TWOSTOPBITS
;
2281 lpdcb
->StopBits
= ONESTOPBIT
;
2288 if (port
.c_cflag
& CRTSCTS
) {
2289 lpdcb
->fDtrControl
= DTR_CONTROL_ENABLE
;
2290 lpdcb
->fRtsControl
= RTS_CONTROL_ENABLE
;
2291 lpdcb
->fOutxCtsFlow
= 1;
2292 lpdcb
->fOutxDsrFlow
= 1;
2296 lpdcb
->fDtrControl
= DTR_CONTROL_DISABLE
;
2297 lpdcb
->fRtsControl
= RTS_CONTROL_DISABLE
;
2299 if (port
.c_iflag
& IXON
)
2304 if (port
.c_iflag
& IXOFF
)
2313 lpdcb
->XoffLim
= 10;
2319 TRACE("bytesize %d baudrate %ld fParity %d Parity %d stopbits %d\n",
2320 lpdcb
->ByteSize
,lpdcb
->BaudRate
,lpdcb
->fParity
, lpdcb
->Parity
,
2321 (lpdcb
->StopBits
== ONESTOPBIT
)?1:
2322 (lpdcb
->StopBits
== TWOSTOPBITS
)?2:0);
2323 TRACE("%s %s\n",(lpdcb
->fInX
)?"IXON":"~IXON",
2324 (lpdcb
->fOutX
)?"IXOFF":"~IXOFF");
2326 if ( lpdcb
->fOutxCtsFlow
||
2327 lpdcb
->fDtrControl
== DTR_CONTROL_ENABLE
||
2328 lpdcb
->fRtsControl
== RTS_CONTROL_ENABLE
2332 if (lpdcb
->fDtrControl
== DTR_CONTROL_DISABLE
)
2333 TRACE("~CRTSCTS\n");
2339 /*****************************************************************************
2340 * TransmitCommChar (KERNEL32.535)
2342 BOOL WINAPI
TransmitCommChar(INT cid
,CHAR chTransmit
)
2344 struct DosDeviceStruct
*ptr
;
2346 FIXME("(%d,'%c'), use win32 handle!\n",cid
,chTransmit
);
2347 if ((ptr
= GetDeviceStruct(cid
)) == NULL
)
2348 FIXME("no handle for cid = %0x!.\n",cid
);
2351 if (ptr
->suspended
) {
2352 ptr
->commerror
= IE_HARDWARE
;
2355 if (write(ptr
->fd
, (void *) &chTransmit
, 1) == -1) {
2356 ptr
->commerror
= WinError();
2364 /*****************************************************************************
2365 * GetCommTimeouts (KERNEL32.160)
2367 BOOL WINAPI
GetCommTimeouts(HANDLE hcom
,LPCOMMTIMEOUTS lptimeouts
)
2369 FIXME("(%x,%p):stub.\n",hcom
,lptimeouts
);
2373 /*****************************************************************************
2374 * SetCommTimeouts (KERNEL32.453)
2376 BOOL WINAPI
SetCommTimeouts(HANDLE hcom
,LPCOMMTIMEOUTS lptimeouts
) {
2377 struct DosDeviceStruct
*ptr
;
2378 struct termios tios
;
2381 FIXME("(%x,%p):stub.\n",hcom
,lptimeouts
);
2383 if ((ptr = GetDeviceStruct(hcom)) == NULL) {
2384 FIXME("no handle for cid = %0x!.\n",hcom);
2389 fd
= COMM_GetWriteFd(hcom
);
2391 FIXME("no fd for cid = %0x!.\n",hcom
);
2396 FIXME("ReadIntervalTimeout %ld\n",lptimeouts
->ReadIntervalTimeout
);
2397 FIXME("ReadTotalTimeoutMultiplier %ld\n",lptimeouts
->ReadTotalTimeoutMultiplier
);
2398 FIXME("ReadTotalTimeoutConstant %ld\n",lptimeouts
->ReadTotalTimeoutConstant
);
2399 FIXME("WriteTotalTimeoutMultiplier %ld\n",lptimeouts
->WriteTotalTimeoutMultiplier
);
2400 FIXME("WriteTotalTimeoutConstant %ld\n",lptimeouts
->WriteTotalTimeoutConstant
);
2403 if (-1==tcgetattr(fd
,&tios
)) {
2404 FIXME("tcgetattr on fd %d failed!\n",fd
);
2407 /* VTIME is in 1/10 seconds */
2408 tios
.c_cc
[VTIME
]= (lptimeouts
->ReadIntervalTimeout
+99)/100;
2409 if (-1==tcsetattr(fd
,0,&tios
)) {
2410 FIXME("tcsetattr on fd %d failed!\n",fd
);
2416 /***********************************************************************
2417 * GetCommModemStatus (KERNEL32.285)
2419 BOOL WINAPI
GetCommModemStatus(HANDLE hFile
,LPDWORD lpModemStat
)
2421 int fd
,mstat
, result
=FALSE
;
2425 fd
= COMM_GetWriteFd(hFile
);
2428 result
= ioctl(fd
, TIOCMGET
, &mstat
);
2432 TRACE("ioctl failed\n");
2435 if (mstat
& TIOCM_CTS
)
2436 *lpModemStat
|= MS_CTS_ON
;
2437 if (mstat
& TIOCM_DSR
)
2438 *lpModemStat
|= MS_DSR_ON
;
2439 if (mstat
& TIOCM_RNG
)
2440 *lpModemStat
|= MS_RING_ON
;
2441 /*FIXME: Not really sure about RLSD UB 990810*/
2442 if (mstat
& TIOCM_CAR
)
2443 *lpModemStat
|= MS_RLSD_ON
;
2445 (*lpModemStat
&MS_RLSD_ON
)?"MS_RLSD_ON ":"",
2446 (*lpModemStat
&MS_RING_ON
)?"MS_RING_ON ":"",
2447 (*lpModemStat
&MS_DSR_ON
)?"MS_DSR_ON ":"",
2448 (*lpModemStat
&MS_CTS_ON
)?"MS_CTS_ON ":"");
2454 /***********************************************************************
2455 * WaitCommEvent (KERNEL32.719)
2457 BOOL WINAPI
WaitCommEvent(HANDLE hFile
,LPDWORD eventmask
,LPOVERLAPPED overlapped
)
2459 FIXME("(%d %p %p )\n",hFile
, eventmask
,overlapped
);
2463 /***********************************************************************
2464 * GetCommProperties (KERNEL32.???)
2466 BOOL WINAPI
GetCommProperties(HANDLE hFile
, LPDCB
*dcb
)
2468 FIXME("(%d %p )\n",hFile
,dcb
);
2472 /***********************************************************************
2473 * SetCommProperties (KERNEL32.???)
2475 BOOL WINAPI
SetCommProperties(HANDLE hFile
, LPDCB dcb
)
2477 FIXME("(%d %p )\n",hFile
,dcb
);