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>
54 #ifdef HAVE_SYS_MODEM_H
55 # include <sys/modem.h>
57 #ifdef HAVE_SYS_STRTIO_H
58 # include <sys/strtio.h>
67 #include "wine/port.h"
68 #include "wine/server.h"
73 #include "debugtools.h"
75 DEFAULT_DEBUG_CHANNEL(comm);
77 #if !defined(TIOCINQ) && defined(FIONREAD)
78 #define TIOCINQ FIONREAD
81 /* window's semi documented modem status register */
82 #define COMM_MSR_OFFSET 35
87 #define MSR_MASK (MSR_CTS|MSR_DSR|MSR_RI|MSR_RLSD)
92 #define CMSPAR 0x40000000 /* stick parity */
97 struct DosDeviceStruct {
98 char *devicename; /* /dev/ttyS0 */
105 int commerror, eventmask;
108 unsigned ibuf_size,ibuf_head,ibuf_tail;
109 unsigned obuf_size,obuf_head,obuf_tail;
111 int wnd, n_read, n_write;
112 OVERLAPPED read_ov, write_ov;
113 /* save terminal states */
115 /* pointer to unknown(==undocumented) comm structure */
120 static struct DosDeviceStruct COM[MAX_PORTS];
121 static struct DosDeviceStruct LPT[MAX_PORTS];
123 /* update window's semi documented modem status register */
124 /* see knowledge base Q101417 */
125 static void COMM_MSRUpdate( HANDLE handle, UCHAR * pMsr )
130 if(!GetCommModemStatus(handle,&mstat))
133 if(mstat & MS_CTS_ON) tmpmsr |= MSR_CTS;
134 if(mstat & MS_DSR_ON) tmpmsr |= MSR_DSR;
135 if(mstat & MS_RING_ON) tmpmsr |= MSR_RI;
136 if(mstat & MS_RLSD_ON) tmpmsr |= MSR_RLSD;
137 *pMsr = (*pMsr & ~MSR_MASK) | tmpmsr;
143 char option[10], temp[256], *btemp;
146 for (x=0; x!=MAX_PORTS; x++) {
147 strcpy(option,"COMx");
151 PROFILE_GetWineIniString( "serialports", option, "*",
152 temp, sizeof(temp) );
153 if (!strcmp(temp, "*") || *temp == '\0')
154 COM[x].devicename = NULL;
156 btemp = strchr(temp,',');
159 COM[x].baudrate = atoi(btemp);
161 COM[x].baudrate = -1;
164 if (!S_ISCHR(st.st_mode))
165 WARN("Can't use `%s' as %s !\n", temp, option);
167 if ((COM[x].devicename = malloc(strlen(temp)+1)) == NULL)
168 WARN("Can't malloc for device info!\n");
171 strcpy(COM[x].devicename, temp);
173 TRACE("%s = %s\n", option, COM[x].devicename);
176 strcpy(option, "LPTx");
180 PROFILE_GetWineIniString( "parallelports", option, "*",
181 temp, sizeof(temp) );
182 if (!strcmp(temp, "*") || *temp == '\0')
183 LPT[x].devicename = NULL;
186 if (!S_ISCHR(st.st_mode))
187 WARN("Can't use `%s' as %s !\n", temp, option);
189 if ((LPT[x].devicename = malloc(strlen(temp)+1)) == NULL)
190 WARN("Can't malloc for device info!\n");
193 strcpy(LPT[x].devicename, temp);
195 TRACE("%s = %s\n", option, LPT[x].devicename);
202 static struct DosDeviceStruct *GetDeviceStruct(int index)
204 if ((index&0x7F)<=MAX_PORTS) {
205 if (!(index&FLAG_LPT)) {
206 if (COM[index].handle)
210 if (LPT[index].handle)
218 static int GetCommPort_ov(LPOVERLAPPED ov, int write)
222 for (x=0; x<MAX_PORTS; x++) {
223 if (ov == (write?&COM[x].write_ov:&COM[x].read_ov))
230 static int ValidCOMPort(int x)
232 return(x < MAX_PORTS ? (int) COM[x].devicename : 0);
235 static int ValidLPTPort(int x)
237 return(x < MAX_PORTS ? (int) LPT[x].devicename : 0);
240 static int WinError(void)
242 TRACE("errno = %d\n", errno);
249 static unsigned comm_inbuf(struct DosDeviceStruct *ptr)
251 return ((ptr->ibuf_tail > ptr->ibuf_head) ? ptr->ibuf_size : 0)
252 + ptr->ibuf_head - ptr->ibuf_tail;
255 static unsigned comm_outbuf(struct DosDeviceStruct *ptr)
257 return ((ptr->obuf_tail > ptr->obuf_head) ? ptr->obuf_size : 0)
258 + ptr->obuf_head - ptr->obuf_tail;
261 static int COMM_WhackModem(int fd, unsigned int andy, unsigned int orrie)
263 unsigned int mstat, okay;
264 okay = ioctl(fd, TIOCMGET, &mstat);
265 if (okay) return okay;
266 if (andy) mstat &= andy;
268 return ioctl(fd, TIOCMSET, &mstat);
271 static void comm_waitread(struct DosDeviceStruct *ptr);
272 static void comm_waitwrite(struct DosDeviceStruct *ptr);
274 static VOID WINAPI COMM16_ReadComplete(DWORD status, DWORD len, LPOVERLAPPED ov)
278 int cid = GetCommPort_ov(ov,0);
279 struct DosDeviceStruct *ptr;
282 ERR("async write with bad overlapped pointer\n");
287 /* read data from comm port */
288 if (status != STATUS_SUCCESS) {
289 ERR("async read failed\n");
290 COM[cid].commerror = CE_RXOVER;
293 TRACE("async read completed %ld bytes\n",len);
295 prev = comm_inbuf(ptr);
297 /* check for events */
298 if ((ptr->eventmask & EV_RXFLAG) &&
299 memchr(ptr->inbuf + ptr->ibuf_head, ptr->evtchar, len)) {
300 *(WORD*)(COM[cid].unknown) |= EV_RXFLAG;
303 if (ptr->eventmask & EV_RXCHAR) {
304 *(WORD*)(COM[cid].unknown) |= EV_RXCHAR;
308 /* advance buffer position */
309 ptr->ibuf_head += len;
310 if (ptr->ibuf_head >= ptr->ibuf_size)
313 /* check for notification */
314 if (ptr->wnd && (ptr->n_read>0) && (prev<ptr->n_read) &&
315 (comm_inbuf(ptr)>=ptr->n_read)) {
316 /* passed the receive notification threshold */
320 /* send notifications, if any */
321 if (ptr->wnd && mask) {
322 TRACE("notifying %04x: cid=%d, mask=%02x\n", ptr->wnd, cid, mask);
323 if (Callout.PostMessageA) Callout.PostMessageA(ptr->wnd, WM_COMMNOTIFY, cid, mask);
326 /* on real windows, this could cause problems, since it is recursive */
327 /* restart the receive */
331 static VOID WINAPI COMM16_WriteComplete(DWORD status, DWORD len, LPOVERLAPPED ov)
335 int cid = GetCommPort_ov(ov,1);
336 struct DosDeviceStruct *ptr;
339 ERR("async write with bad overlapped pointer\n");
344 /* read data from comm port */
345 if (status != STATUS_SUCCESS) {
346 ERR("async write failed\n");
347 COM[cid].commerror = CE_RXOVER;
350 TRACE("async write completed %ld bytes\n",len);
352 /* update the buffer pointers */
353 prev = comm_outbuf(&COM[cid]);
354 ptr->obuf_tail += len;
355 if (ptr->obuf_tail >= ptr->obuf_size)
358 /* write any TransmitCommChar character */
360 if(!WriteFile(ptr->handle, &(ptr->xmit), 1, &len, NULL))
362 if (len > 0) ptr->xmit = -1;
365 /* write from output queue */
366 bleft = ((ptr->obuf_tail <= ptr->obuf_head) ?
367 ptr->obuf_head : ptr->obuf_size) - ptr->obuf_tail;
369 /* check for notification */
370 if (ptr->wnd && (ptr->n_write>0) && (prev>=ptr->n_write) &&
371 (comm_outbuf(ptr)<ptr->n_write)) {
372 /* passed the transmit notification threshold */
376 /* send notifications, if any */
377 if (ptr->wnd && mask) {
378 TRACE("notifying %04x: cid=%d, mask=%02x\n", ptr->wnd, cid, mask);
379 if (Callout.PostMessageA) Callout.PostMessageA(ptr->wnd, WM_COMMNOTIFY, cid, mask);
382 /* start again if necessary */
387 static void comm_waitread(struct DosDeviceStruct *ptr)
391 bleft = ((ptr->ibuf_tail > ptr->ibuf_head) ?
392 (ptr->ibuf_tail-1) : ptr->ibuf_size) - ptr->ibuf_head;
393 /* FIXME: get timeouts working properly so we can read bleft bytes */
394 ReadFileEx(ptr->handle,
395 ptr->inbuf + ptr->ibuf_head,
398 COMM16_ReadComplete);
401 static void comm_waitwrite(struct DosDeviceStruct *ptr)
405 bleft = ((ptr->obuf_tail <= ptr->obuf_head) ?
406 ptr->obuf_head : ptr->obuf_size) - ptr->obuf_tail;
407 WriteFileEx(ptr->handle,
408 ptr->outbuf + ptr->obuf_tail,
411 COMM16_WriteComplete);
414 /*****************************************************************************
415 * COMM16_DCBtoDCB16 (Internal)
417 INT16 COMM16_DCBtoDCB16(LPDCB lpdcb, LPDCB16 lpdcb16)
419 if(lpdcb->BaudRate<0x10000)
420 lpdcb16->BaudRate = lpdcb->BaudRate;
421 else if(lpdcb->BaudRate==115200)
422 lpdcb16->BaudRate = 57601;
424 WARN("Baud rate can't be converted\n");
425 lpdcb16->BaudRate = 57601;
427 lpdcb16->ByteSize = lpdcb->ByteSize;
428 lpdcb16->fParity = lpdcb->fParity;
429 lpdcb16->Parity = lpdcb->Parity;
430 lpdcb16->StopBits = lpdcb->StopBits;
432 lpdcb16->RlsTimeout = 50;
433 lpdcb16->CtsTimeout = 50;
434 lpdcb16->DsrTimeout = 50;
437 lpdcb16->fBinary = 1;
438 lpdcb16->fDtrDisable = 0;
440 lpdcb16->fDtrflow = (lpdcb->fDtrControl==DTR_CONTROL_ENABLE);
441 lpdcb16->fRtsflow = (lpdcb->fRtsControl==RTS_CONTROL_ENABLE);
442 lpdcb16->fOutxCtsFlow = lpdcb->fOutxCtsFlow;
443 lpdcb16->fOutxDsrFlow = lpdcb->fOutxDsrFlow;
444 lpdcb16->fDtrDisable = (lpdcb->fDtrControl==DTR_CONTROL_DISABLE);
446 lpdcb16->fInX = lpdcb->fInX;
448 lpdcb16->fOutX = lpdcb->fOutX;
453 lpdcb16->XonLim = 10;
454 lpdcb16->XoffLim = 10;
460 /**************************************************************************
461 * BuildCommDCB (USER.213)
463 * According to the ECMA-234 (368.3) the function will return FALSE on
464 * success, otherwise it will return -1.
466 INT16 WINAPI BuildCommDCB16(LPCSTR device, LPDCB16 lpdcb)
468 /* "COM1:96,n,8,1" */
473 TRACE("(%s), ptr %p\n", device, lpdcb);
475 if (strncasecmp(device,"COM",3))
477 port = device[3] - '0';
480 ERR("BUG ! COM0 can't exist!\n");
484 if (!ValidCOMPort(port)) {
485 FIXME("invalid COM port %d?\n",port);
489 memset(lpdcb, 0, sizeof(DCB16)); /* initialize */
492 dcb.DCBlength = sizeof(DCB);
494 if (strchr(device,'=')) /* block new style */
497 if(!BuildCommDCBA(device,&dcb))
500 return COMM16_DCBtoDCB16(&dcb, lpdcb);
503 /*****************************************************************************
504 * OpenComm (USER.200)
506 INT16 WINAPI OpenComm16(LPCSTR device,UINT16 cbInQueue,UINT16 cbOutQueue)
511 TRACE("%s, %d, %d\n", device, cbInQueue, cbOutQueue);
513 if (strlen(device) < 4)
516 port = device[3] - '0';
519 ERR("BUG ! COM0 or LPT0 don't exist !\n");
521 if (!strncasecmp(device,"COM",3)) {
523 TRACE("%s = %s\n", device, COM[port].devicename);
525 if (!ValidCOMPort(port))
528 if (COM[port].handle)
531 handle = CreateFileA(device, GENERIC_READ|GENERIC_WRITE,
532 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0 );
533 if (handle == INVALID_HANDLE_VALUE) {
534 ERR("Couldn't open %s ! (%s)\n", COM[port].devicename, strerror(errno));
537 COM[port].unknown = SEGPTR_ALLOC(40);
538 memset(COM[port].unknown, 0, 40);
539 COM[port].handle = handle;
540 COM[port].commerror = 0;
541 COM[port].eventmask = 0;
542 COM[port].evtchar = 0; /* FIXME: default? */
543 /* save terminal state */
544 GetCommState16(port,&COM[port].dcb);
545 /* set default parameters */
546 if(COM[port].baudrate>-1){
548 memcpy(&dcb,&COM[port].dcb,sizeof dcb);
549 dcb.BaudRate=COM[port].baudrate;
551 * databits, parity, stopbits
553 SetCommState16( &dcb);
555 /* init priority characters */
556 COM[port].unget = -1;
558 /* allocate buffers */
559 COM[port].ibuf_size = cbInQueue;
560 COM[port].ibuf_head = COM[port].ibuf_tail = 0;
561 COM[port].obuf_size = cbOutQueue;
562 COM[port].obuf_head = COM[port].obuf_tail = 0;
564 COM[port].inbuf = malloc(cbInQueue);
565 if (COM[port].inbuf) {
566 COM[port].outbuf = malloc(cbOutQueue);
567 if (!COM[port].outbuf)
568 free(COM[port].inbuf);
569 } else COM[port].outbuf = NULL;
570 if (!COM[port].outbuf) {
571 /* not enough memory */
572 SetCommState16(&COM[port].dcb);
573 CloseHandle(COM[port].handle);
574 ERR("out of memory\n");
578 ZeroMemory(&COM[port].read_ov,sizeof (OVERLAPPED));
579 ZeroMemory(&COM[port].write_ov,sizeof (OVERLAPPED));
580 COM[port].read_ov.hEvent = CreateEventA(NULL,0,0,NULL);
581 COM[port].write_ov.hEvent = CreateEventA(NULL,0,0,NULL);
583 comm_waitread( &COM[port] );
589 if (!strncasecmp(device,"LPT",3)) {
591 if (!ValidLPTPort(port))
594 if (LPT[port].handle)
597 handle = CreateFileA(device, GENERIC_READ|GENERIC_WRITE,
598 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0 );
599 if (handle == INVALID_HANDLE_VALUE) {
602 LPT[port].handle = handle;
603 LPT[port].commerror = 0;
604 LPT[port].eventmask = 0;
605 return port|FLAG_LPT;
611 /*****************************************************************************
612 * CloseComm (USER.207)
614 INT16 WINAPI CloseComm16(INT16 cid)
616 struct DosDeviceStruct *ptr;
618 TRACE("cid=%d\n", cid);
619 if ((ptr = GetDeviceStruct(cid)) == NULL) {
620 FIXME("no cid=%d found!\n", cid);
623 if (!(cid&FLAG_LPT)) {
625 SEGPTR_FREE(COM[cid].unknown); /* [LW] */
627 CloseHandle(COM[cid].read_ov.hEvent);
628 CloseHandle(COM[cid].write_ov.hEvent);
634 /* reset modem lines */
635 SetCommState16(&COM[cid].dcb);
638 if (!CloseHandle(ptr->handle)) {
639 ptr->commerror = WinError();
640 /* FIXME: should we clear ptr->handle here? */
649 /*****************************************************************************
650 * SetCommBreak (USER.210)
652 INT16 WINAPI SetCommBreak16(INT16 cid)
654 struct DosDeviceStruct *ptr;
656 TRACE("cid=%d\n", cid);
657 if ((ptr = GetDeviceStruct(cid)) == NULL) {
658 FIXME("no cid=%d found!\n", cid);
667 /*****************************************************************************
668 * ClearCommBreak (USER.211)
670 INT16 WINAPI ClearCommBreak16(INT16 cid)
672 struct DosDeviceStruct *ptr;
674 TRACE("cid=%d\n", cid);
675 if (!(ptr = GetDeviceStruct(cid))) {
676 FIXME("no cid=%d found!\n", cid);
684 /*****************************************************************************
685 * EscapeCommFunction (USER.214)
687 LONG WINAPI EscapeCommFunction16(UINT16 cid,UINT16 nFunction)
689 struct DosDeviceStruct *ptr;
692 TRACE("cid=%d, function=%d\n", cid, nFunction);
696 TRACE("GETMAXCOM\n");
697 for (max = MAX_PORTS;!COM[max].devicename;max--)
702 TRACE("GETMAXLPT\n");
703 for (max = MAX_PORTS;!LPT[max].devicename;max--)
705 return FLAG_LPT + max;
708 TRACE("GETBASEIRQ\n");
709 /* FIXME: use tables */
710 /* just fake something for now */
711 if (cid & FLAG_LPT) {
712 /* LPT1: irq 7, LPT2: irq 5 */
713 return (cid & 0x7f) ? 5 : 7;
715 /* COM1: irq 4, COM2: irq 3,
716 COM3: irq 4, COM4: irq 3 */
717 return 4 - (cid & 1);
721 if ((ptr = GetDeviceStruct(cid)) == NULL) {
722 FIXME("no cid=%d found!\n", cid);
734 if(EscapeCommFunction(ptr->handle,nFunction))
737 ptr->commerror = WinError();
744 WARN("(cid=%d,nFunction=%d): Unknown function\n",
750 /*****************************************************************************
751 * FlushComm (USER.215)
753 INT16 WINAPI FlushComm16(INT16 cid,INT16 fnQueue)
756 struct DosDeviceStruct *ptr;
758 TRACE("cid=%d, queue=%d\n", cid, fnQueue);
759 if ((ptr = GetDeviceStruct(cid)) == NULL) {
760 FIXME("no cid=%d found!\n", cid);
765 queue = PURGE_TXABORT;
766 ptr->obuf_tail = ptr->obuf_head;
769 queue = PURGE_RXABORT;
770 ptr->ibuf_head = ptr->ibuf_tail;
773 WARN("(cid=%d,fnQueue=%d):Unknown queue\n",
778 if (!PurgeComm(ptr->handle,queue)) {
779 ptr->commerror = WinError();
787 /********************************************************************
788 * GetCommError (USER.203)
790 INT16 WINAPI GetCommError16(INT16 cid,LPCOMSTAT16 lpStat)
793 struct DosDeviceStruct *ptr;
796 if ((ptr = GetDeviceStruct(cid)) == NULL) {
797 FIXME("no handle for cid = %0x!\n",cid);
801 WARN(" cid %d not comm port\n",cid);
804 stol = (unsigned char *)COM[cid].unknown + COMM_MSR_OFFSET;
805 COMM_MSRUpdate( ptr->handle, stol );
812 rw_events[0] = COM[cid].read_ov.hEvent;
813 rw_events[1] = COM[cid].write_ov.hEvent;
815 WaitForMultipleObjectsEx(2,&rw_events[0],FALSE,1,TRUE);
817 lpStat->cbOutQue = comm_outbuf(ptr);
818 lpStat->cbInQue = comm_inbuf(ptr);
820 TRACE("cid %d, error %d, stat %d in %d out %d, stol %x\n",
821 cid, ptr->commerror, lpStat->status, lpStat->cbInQue,
822 lpStat->cbOutQue, *stol);
825 TRACE("cid %d, error %d, lpStat NULL stol %x\n",
826 cid, ptr->commerror, *stol);
828 /* Return any errors and clear it */
829 temperror = ptr->commerror;
834 /*****************************************************************************
835 * SetCommEventMask (USER.208)
837 SEGPTR WINAPI SetCommEventMask16(INT16 cid,UINT16 fuEvtMask)
839 struct DosDeviceStruct *ptr;
842 TRACE("cid %d,mask %d\n",cid,fuEvtMask);
843 if ((ptr = GetDeviceStruct(cid)) == NULL) {
844 FIXME("no handle for cid = %0x!\n",cid);
848 ptr->eventmask = fuEvtMask;
850 if ((cid&FLAG_LPT) || !ValidCOMPort(cid)) {
851 WARN(" cid %d not comm port\n",cid);
854 /* it's a COM port ? -> modify flags */
855 stol = (unsigned char *)COM[cid].unknown + COMM_MSR_OFFSET;
856 COMM_MSRUpdate( ptr->handle, stol );
858 TRACE(" modem dcd construct %x\n",*stol);
859 return SEGPTR_GET(COM[cid].unknown);
862 /*****************************************************************************
863 * GetCommEventMask (USER.209)
865 UINT16 WINAPI GetCommEventMask16(INT16 cid,UINT16 fnEvtClear)
867 struct DosDeviceStruct *ptr;
870 TRACE("cid %d, mask %d\n", cid, fnEvtClear);
871 if ((ptr = GetDeviceStruct(cid)) == NULL) {
872 FIXME("no handle for cid = %0x!\n",cid);
876 if ((cid&FLAG_LPT) || !ValidCOMPort(cid)) {
877 WARN(" cid %d not comm port\n",cid);
881 events = *(WORD*)(COM[cid].unknown) & fnEvtClear;
882 *(WORD*)(COM[cid].unknown) &= ~fnEvtClear;
886 /*****************************************************************************
887 * SetCommState (USER.201)
889 INT16 WINAPI SetCommState16(LPDCB16 lpdcb)
891 struct DosDeviceStruct *ptr;
894 TRACE("cid %d, ptr %p\n", lpdcb->Id, lpdcb);
895 if ((ptr = GetDeviceStruct(lpdcb->Id)) == NULL) {
896 FIXME("no handle for cid = %0x!\n",lpdcb->Id);
900 memset(&dcb,0,sizeof dcb);
901 dcb.DCBlength = sizeof dcb;
902 if(lpdcb->BaudRate==57601)
903 dcb.BaudRate = 115200;
905 dcb.BaudRate = lpdcb->BaudRate;
907 dcb.ByteSize=lpdcb->ByteSize;
908 dcb.StopBits=lpdcb->StopBits;
910 dcb.fParity=lpdcb->fParity;
911 dcb.Parity=lpdcb->Parity;
913 dcb.fOutxCtsFlow = lpdcb->fOutxCtsFlow;
915 if (lpdcb->fDtrflow || lpdcb->fRtsflow)
916 dcb.fRtsControl = TRUE;
918 if (lpdcb->fDtrDisable)
919 dcb.fDtrControl = TRUE;
921 ptr->evtchar = lpdcb->EvtChar;
923 dcb.fInX = lpdcb->fInX;
924 dcb.fOutX = lpdcb->fOutX;
926 if (!SetCommState(ptr->handle,&dcb)) {
927 ptr->commerror = WinError();
935 /*****************************************************************************
936 * GetCommState (USER.202)
938 INT16 WINAPI GetCommState16(INT16 cid, LPDCB16 lpdcb)
940 struct DosDeviceStruct *ptr;
943 TRACE("cid %d, ptr %p\n", cid, lpdcb);
944 if ((ptr = GetDeviceStruct(cid)) == NULL) {
945 FIXME("no handle for cid = %0x!\n",cid);
948 if (!GetCommState(ptr->handle,&dcb)) {
949 ptr->commerror = WinError();
955 COMM16_DCBtoDCB16(&dcb,lpdcb);
957 lpdcb->EvtChar = ptr->evtchar;
963 /*****************************************************************************
964 * TransmitCommChar (USER.206)
966 INT16 WINAPI TransmitCommChar16(INT16 cid,CHAR chTransmit)
968 struct DosDeviceStruct *ptr;
970 TRACE("cid %d, data %d \n", cid, chTransmit);
971 if ((ptr = GetDeviceStruct(cid)) == NULL) {
972 FIXME("no handle for cid = %0x!\n",cid);
976 if (ptr->suspended) {
977 ptr->commerror = IE_HARDWARE;
981 if (ptr->xmit >= 0) {
982 /* character already queued */
983 /* FIXME: which error would Windows return? */
984 ptr->commerror = CE_TXFULL;
988 if (ptr->obuf_head == ptr->obuf_tail) {
989 /* transmit queue empty, try to transmit directly */
991 if(!WriteFile(ptr->handle, &chTransmit, 1, &len, NULL)) {
992 /* didn't work, queue it */
993 ptr->xmit = chTransmit;
997 /* data in queue, let this char be transmitted next */
998 ptr->xmit = chTransmit;
1006 /*****************************************************************************
1007 * UngetCommChar (USER.212)
1009 INT16 WINAPI UngetCommChar16(INT16 cid,CHAR chUnget)
1011 struct DosDeviceStruct *ptr;
1013 TRACE("cid %d (char %d)\n", cid, chUnget);
1014 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1015 FIXME("no handle for cid = %0x!\n",cid);
1019 if (ptr->suspended) {
1020 ptr->commerror = IE_HARDWARE;
1024 if (ptr->unget>=0) {
1025 /* character already queued */
1026 /* FIXME: which error would Windows return? */
1027 ptr->commerror = CE_RXOVER;
1031 ptr->unget = chUnget;
1037 /*****************************************************************************
1038 * ReadComm (USER.204)
1040 INT16 WINAPI ReadComm16(INT16 cid,LPSTR lpvBuf,INT16 cbRead)
1043 struct DosDeviceStruct *ptr;
1044 LPSTR orgBuf = lpvBuf;
1046 TRACE("cid %d, ptr %p, length %d\n", cid, lpvBuf, cbRead);
1047 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1048 FIXME("no handle for cid = %0x!\n",cid);
1052 if (ptr->suspended) {
1053 ptr->commerror = IE_HARDWARE;
1057 /* read unget character */
1058 if (ptr->unget>=0) {
1059 *lpvBuf++ = ptr->unget;
1066 /* read from receive buffer */
1067 while (length < cbRead) {
1068 status = ((ptr->ibuf_head < ptr->ibuf_tail) ?
1069 ptr->ibuf_size : ptr->ibuf_head) - ptr->ibuf_tail;
1071 if ((cbRead - length) < status)
1072 status = cbRead - length;
1074 memcpy(lpvBuf, ptr->inbuf + ptr->ibuf_tail, status);
1075 ptr->ibuf_tail += status;
1076 if (ptr->ibuf_tail >= ptr->ibuf_size)
1082 TRACE("%.*s\n", length, orgBuf);
1087 /*****************************************************************************
1088 * WriteComm (USER.205)
1090 INT16 WINAPI WriteComm16(INT16 cid, LPSTR lpvBuf, INT16 cbWrite)
1093 struct DosDeviceStruct *ptr;
1095 TRACE("cid %d, ptr %p, length %d\n",
1096 cid, lpvBuf, cbWrite);
1097 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1098 FIXME("no handle for cid = %0x!\n",cid);
1102 if (ptr->suspended) {
1103 ptr->commerror = IE_HARDWARE;
1107 TRACE("%.*s\n", cbWrite, lpvBuf );
1110 while (length < cbWrite) {
1111 if ((ptr->obuf_head == ptr->obuf_tail) && (ptr->xmit < 0)) {
1112 /* no data queued, try to write directly */
1113 if(!WriteFile(ptr->handle, lpvBuf, cbWrite - length, (LPDWORD)&status, NULL))
1121 /* can't write directly, put into transmit buffer */
1122 status = ((ptr->obuf_tail > ptr->obuf_head) ?
1123 (ptr->obuf_tail-1) : ptr->obuf_size) - ptr->obuf_head;
1125 if ((cbWrite - length) < status)
1126 status = cbWrite - length;
1127 memcpy(lpvBuf, ptr->outbuf + ptr->obuf_head, status);
1128 ptr->obuf_head += status;
1129 if (ptr->obuf_head >= ptr->obuf_size)
1133 comm_waitwrite(ptr);
1140 /***********************************************************************
1141 * EnableCommNotification (USER.245)
1143 BOOL16 WINAPI EnableCommNotification16( INT16 cid, HWND16 hwnd,
1144 INT16 cbWriteNotify, INT16 cbOutQueue )
1146 struct DosDeviceStruct *ptr;
1148 TRACE("(%d, %x, %d, %d)\n", cid, hwnd, cbWriteNotify, cbOutQueue);
1149 if ((ptr = GetDeviceStruct(cid)) == NULL) {
1150 FIXME("no handle for cid = %0x!\n",cid);
1154 ptr->n_read = cbWriteNotify;
1155 ptr->n_write = cbOutQueue;
1160 /***********************************************************************
1161 * COMM_BuildOldCommDCB (Internal)
1163 * Build a DCB using the old style settings string eg: "COMx:96,n,8,1"
1164 * We ignore the COM port index, since we can support more than 4 ports.
1166 BOOL WINAPI COMM_BuildOldCommDCB(LPCSTR device, LPDCB lpdcb)
1168 /* "COM1:96,n,8,1" */
1170 char *ptr, temp[256], last;
1173 TRACE("(%s), ptr %p\n", device, lpdcb);
1175 if (strncasecmp(device,"COM",3))
1181 if (*(device+4) != ':')
1184 strcpy(temp,device+5);
1185 last=temp[strlen(temp)-1];
1186 ptr = strtok(temp, ", ");
1188 /* DOS/Windows only compares the first two numbers
1189 * and assigns an appropriate baud rate.
1190 * You can supply 961324245, it still returns 9600 ! */
1191 if (strlen(ptr) < 2)
1193 WARN("Unknown baudrate string '%s' !\n", ptr);
1194 return FALSE; /* error: less than 2 chars */
1215 WARN("Unknown baudrate indicator %d !\n", rate);
1219 lpdcb->BaudRate = rate;
1220 TRACE("baudrate (%ld)\n", lpdcb->BaudRate);
1222 ptr = strtok(NULL, ", ");
1224 *ptr = toupper(*ptr);
1226 TRACE("parity (%c)\n", *ptr);
1227 lpdcb->fParity = TRUE;
1230 lpdcb->Parity = NOPARITY;
1231 lpdcb->fParity = FALSE;
1234 lpdcb->Parity = EVENPARITY;
1237 lpdcb->Parity = MARKPARITY;
1240 lpdcb->Parity = ODDPARITY;
1243 WARN("Unknown parity `%c'!\n", *ptr);
1247 ptr = strtok(NULL, ", ");
1248 TRACE("charsize (%c)\n", *ptr);
1249 lpdcb->ByteSize = *ptr - '0';
1251 ptr = strtok(NULL, ", ");
1252 TRACE("stopbits (%c)\n", *ptr);
1255 lpdcb->StopBits = ONESTOPBIT;
1258 lpdcb->StopBits = TWOSTOPBITS;
1261 WARN("Unknown # of stopbits `%c'!\n", *ptr);
1267 lpdcb->fOutX = TRUE;
1268 lpdcb->fOutxCtsFlow = FALSE;
1269 lpdcb->fOutxDsrFlow = FALSE;
1270 lpdcb->fDtrControl = DTR_CONTROL_ENABLE;
1271 lpdcb->fRtsControl = RTS_CONTROL_ENABLE;
1272 } else if (last=='p') {
1273 lpdcb->fInX = FALSE;
1274 lpdcb->fOutX = FALSE;
1275 lpdcb->fOutxCtsFlow = TRUE;
1276 lpdcb->fOutxDsrFlow = TRUE;
1277 lpdcb->fDtrControl = DTR_CONTROL_HANDSHAKE;
1278 lpdcb->fRtsControl = RTS_CONTROL_HANDSHAKE;
1280 lpdcb->fInX = FALSE;
1281 lpdcb->fOutX = FALSE;
1282 lpdcb->fOutxCtsFlow = FALSE;
1283 lpdcb->fOutxDsrFlow = FALSE;
1284 lpdcb->fDtrControl = DTR_CONTROL_ENABLE;
1285 lpdcb->fRtsControl = RTS_CONTROL_ENABLE;
1291 /**************************************************************************
1292 * BuildCommDCBA (KERNEL32.@)
1294 * Updates a device control block data structure with values from an
1295 * ascii device control string. The device control string has two forms
1296 * normal and extended, it must be exclusively in one or the other form.
1300 * True on success, false on an malformed control string.
1302 BOOL WINAPI BuildCommDCBA(
1303 LPCSTR device, /* [in] The ascii device control string used to update the DCB. */
1304 LPDCB lpdcb) /* [out] The device control block to be updated. */
1306 return BuildCommDCBAndTimeoutsA(device,lpdcb,NULL);
1309 /**************************************************************************
1310 * BuildCommDCBAndTimeoutsA (KERNEL32.@)
1312 * Updates a device control block data structure with values from an
1313 * ascii device control string. Taking time out values from a time outs
1314 * struct if desired by the control string.
1318 * True on success, false bad handles etc
1320 BOOL WINAPI BuildCommDCBAndTimeoutsA(
1321 LPCSTR device, /* [in] The ascii device control string. */
1322 LPDCB lpdcb, /* [out] The device control block to be updated. */
1323 LPCOMMTIMEOUTS lptimeouts) /* [in] The time outs to use if asked to set them by the control string. */
1328 TRACE("(%s,%p,%p)\n",device,lpdcb,lptimeouts);
1330 if (!strncasecmp(device,"COM",3)) {
1333 ERR("BUG! COM0 can't exist!\n");
1336 if (!ValidCOMPort(port))
1338 if (*(device+4)!=':')
1340 temp=(LPSTR)(device+5);
1344 memset(lpdcb,0,sizeof (DCB));
1345 lpdcb->DCBlength = sizeof(DCB);
1346 if (strchr(temp,',')) { /* old style */
1348 return COMM_BuildOldCommDCB(device,lpdcb);
1350 ptr=strtok(temp," ");
1355 if (!strncmp("baud=",ptr,5)) {
1356 if (!sscanf(ptr+5,"%ld",&x))
1357 WARN("Couldn't parse %s\n",ptr);
1358 lpdcb->BaudRate = x;
1361 if (!strncmp("stop=",ptr,5)) {
1362 if (!sscanf(ptr+5,"%ld",&x))
1363 WARN("Couldn't parse %s\n",ptr);
1364 lpdcb->StopBits = x;
1367 if (!strncmp("data=",ptr,5)) {
1368 if (!sscanf(ptr+5,"%ld",&x))
1369 WARN("Couldn't parse %s\n",ptr);
1370 lpdcb->ByteSize = x;
1373 if (!strncmp("parity=",ptr,7)) {
1374 lpdcb->fParity = TRUE;
1377 lpdcb->fParity = FALSE;
1378 lpdcb->Parity = NOPARITY;
1381 lpdcb->Parity = EVENPARITY;
1384 lpdcb->Parity = ODDPARITY;
1387 lpdcb->Parity = MARKPARITY;
1393 ERR("Unhandled specifier '%s', please report.\n",ptr);
1394 ptr=strtok(NULL," ");
1396 if (lpdcb->BaudRate==110)
1397 lpdcb->StopBits = 2;
1401 /**************************************************************************
1402 * BuildCommDCBAndTimeoutsW (KERNEL32.@)
1404 * Updates a device control block data structure with values from an
1405 * unicode device control string. Taking time out values from a time outs
1406 * struct if desired by the control string.
1410 * True on success, false bad handles etc.
1412 BOOL WINAPI BuildCommDCBAndTimeoutsW(
1413 LPCWSTR devid, /* [in] The unicode device control string. */
1414 LPDCB lpdcb, /* [out] The device control block to be updated. */
1415 LPCOMMTIMEOUTS lptimeouts) /* [in] The time outs to use if asked to set them by the control string. */
1420 TRACE("(%p,%p,%p)\n",devid,lpdcb,lptimeouts);
1421 devidA = HEAP_strdupWtoA( GetProcessHeap(), 0, devid );
1424 ret=BuildCommDCBAndTimeoutsA(devidA,lpdcb,lptimeouts);
1425 HeapFree( GetProcessHeap(), 0, devidA );
1430 /**************************************************************************
1431 * BuildCommDCBW (KERNEL32.@)
1433 * Updates a device control block structure with values from an
1434 * unicode device control string. The device control string has two forms
1435 * normal and extended, it must be exclusively in one or the other form.
1439 * True on success, false on an malformed control string.
1441 BOOL WINAPI BuildCommDCBW(
1442 LPCWSTR devid, /* [in] The unicode device control string. */
1443 LPDCB lpdcb) /* [out] The device control block to be updated. */
1445 return BuildCommDCBAndTimeoutsW(devid,lpdcb,NULL);
1448 /* FIXME: having these global for win32 for now */
1451 /*****************************************************************************
1452 * SetCommBreak (KERNEL32.@)
1454 * Halts the transmission of characters to a communications device.
1458 * True on success, and false if the communications device could not be found,
1459 * the control is not supported.
1463 * Only TIOCSBRK and TIOCCBRK are supported.
1465 BOOL WINAPI SetCommBreak(
1466 HANDLE handle) /* [in] The communictions device to suspend. */
1468 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1471 fd = FILE_GetUnixHandle( handle, GENERIC_READ );
1473 TRACE("FILE_GetUnixHandle failed\n");
1476 result = ioctl(fd,TIOCSBRK,0);
1480 TRACE("ioctl failed\n");
1481 SetLastError(ERROR_NOT_SUPPORTED);
1486 FIXME("ioctl not available\n");
1487 SetLastError(ERROR_NOT_SUPPORTED);
1492 /*****************************************************************************
1493 * ClearCommBreak (KERNEL32.@)
1495 * Resumes character transmission from a communication device.
1499 * True on success and false if the communications device could not be found.
1503 * Only TIOCSBRK and TIOCCBRK are supported.
1505 BOOL WINAPI ClearCommBreak(
1506 HANDLE handle) /* [in] The halted communication device whose character transmission is to be resumed. */
1508 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1511 fd = FILE_GetUnixHandle( handle, GENERIC_READ );
1513 TRACE("FILE_GetUnixHandle failed\n");
1516 result = ioctl(fd,TIOCCBRK,0);
1520 TRACE("ioctl failed\n");
1521 SetLastError(ERROR_NOT_SUPPORTED);
1526 FIXME("ioctl not available\n");
1527 SetLastError(ERROR_NOT_SUPPORTED);
1532 /*****************************************************************************
1533 * EscapeCommFunction (KERNEL32.@)
1535 * Directs a communication device to perform an extended function.
1539 * True or requested data on successful completion of the command,
1540 * false if the device is not present cannot execute the command
1541 * or the command failed.
1543 BOOL WINAPI EscapeCommFunction(
1544 HANDLE handle, /* [in] The communication device to perform the extended function. */
1545 UINT nFunction) /* [in] The extended function to be performed. */
1547 int fd,direct=FALSE,result=FALSE;
1548 struct termios port;
1550 TRACE("handle %d, function=%d\n", handle, nFunction);
1551 fd = FILE_GetUnixHandle( handle, GENERIC_READ );
1553 FIXME("handle %d not found.\n",handle);
1557 if (tcgetattr(fd,&port) == -1) {
1558 commerror=WinError();
1563 switch (nFunction) {
1572 result= COMM_WhackModem(fd, ~TIOCM_DTR, 0);
1580 result= COMM_WhackModem(fd, ~TIOCM_RTS, 0);
1588 result= COMM_WhackModem(fd, 0, TIOCM_DTR);
1596 result= COMM_WhackModem(fd, 0, TIOCM_RTS);
1602 port.c_iflag |= IXOFF;
1607 port.c_iflag |= IXON;
1610 TRACE("setbreak\n");
1613 result = ioctl(fd,TIOCSBRK,0);
1617 TRACE("clrbreak\n");
1620 result = ioctl(fd,TIOCCBRK,0);
1624 WARN("(handle=%d,nFunction=%d): Unknown function\n",
1630 if (tcsetattr(fd, TCSADRAIN, &port) == -1) {
1631 commerror = WinError();
1641 commerror=WinError();
1650 /********************************************************************
1651 * PurgeComm (KERNEL32.@)
1653 * Terminates pending operations and/or discards buffers on a
1654 * communication resource.
1658 * True on success and false if the communications handle is bad.
1660 BOOL WINAPI PurgeComm(
1661 HANDLE handle, /* [in] The communication resource to be purged. */
1662 DWORD flags) /* [in] Flags for clear pending/buffer on input/output. */
1666 TRACE("handle %d, flags %lx\n", handle, flags);
1668 fd = FILE_GetUnixHandle( handle, GENERIC_READ );
1670 FIXME("no handle %d found\n",handle);
1675 ** not exactly sure how these are different
1676 ** Perhaps if we had our own internal queues, one flushes them
1677 ** and the other flushes the kernel's buffers.
1679 if(flags&PURGE_TXABORT)
1680 tcflush(fd,TCOFLUSH);
1681 if(flags&PURGE_RXABORT)
1682 tcflush(fd,TCIFLUSH);
1683 if(flags&PURGE_TXCLEAR)
1684 tcflush(fd,TCOFLUSH);
1685 if(flags&PURGE_RXCLEAR)
1686 tcflush(fd,TCIFLUSH);
1692 /*****************************************************************************
1693 * ClearCommError (KERNEL32.@)
1695 * Enables further I/O operations on a communications resource after
1696 * supplying error and current status information.
1700 * True on success, false if the communication resource handle is bad.
1702 BOOL WINAPI ClearCommError(
1703 HANDLE handle, /* [in] The communication resource with the error. */
1704 LPDWORD errors, /* [out] Flags indicating error the resource experienced. */
1705 LPCOMSTAT lpStat) /* [out] The status of the communication resource. */
1709 fd=FILE_GetUnixHandle( handle, GENERIC_READ );
1712 FIXME("no handle %d found\n",handle);
1721 if(ioctl(fd, TIOCOUTQ, &lpStat->cbOutQue))
1722 WARN("ioctl returned error\n");
1724 lpStat->cbOutQue = 0; /* FIXME: find a different way to find out */
1728 if(ioctl(fd, TIOCINQ, &lpStat->cbInQue))
1729 WARN("ioctl returned error\n");
1732 TRACE("handle %d cbInQue = %ld cbOutQue = %ld\n",
1733 handle, lpStat->cbInQue, lpStat->cbOutQue);
1742 ** After an asynchronous write opperation, the
1743 ** app will call ClearCommError to see if the
1744 ** results are ready yet. It waits for ERROR_IO_PENDING
1746 commerror = ERROR_IO_PENDING;
1751 /*****************************************************************************
1752 * SetupComm (KERNEL32.@)
1754 * Called after CreateFile to hint to the communication resource to use
1755 * specified sizes for input and output buffers rather than the default values.
1759 * True if successful, false if the communications resource handle is bad.
1765 BOOL WINAPI SetupComm(
1766 HANDLE handle, /* [in] The just created communication resource handle. */
1767 DWORD insize, /* [in] The suggested size of the communication resources input buffer in bytes. */
1768 DWORD outsize) /* [in] The suggested size of the communication resources output buffer in bytes. */
1772 FIXME("insize %ld outsize %ld unimplemented stub\n", insize, outsize);
1773 fd=FILE_GetUnixHandle( handle, GENERIC_READ );
1775 FIXME("handle %d not found?\n",handle);
1782 /*****************************************************************************
1783 * GetCommMask (KERNEL32.@)
1785 * Obtain the events associated with a communication device that will cause a call
1786 * WaitCommEvent to return.
1790 * True on success, fail on bad device handle etc.
1792 BOOL WINAPI GetCommMask(
1793 HANDLE handle, /* [in] The communications device. */
1794 LPDWORD evtmask) /* [out] The events which cause WaitCommEvent to return. */
1798 TRACE("handle %d, mask %p\n", handle, evtmask);
1800 SERVER_START_REQ( get_serial_info )
1802 req->handle = handle;
1803 if ((ret = !SERVER_CALL_ERR()))
1805 if (evtmask) *evtmask = req->eventmask;
1812 /*****************************************************************************
1813 * SetCommMask (KERNEL32.@)
1815 * There be some things we need to hear about yon there communications device.
1816 * (Set which events associated with a communication device should cause
1817 * a call WaitCommEvent to return.)
1821 * True on success, false on bad handle etc.
1823 BOOL WINAPI SetCommMask(
1824 HANDLE handle, /* [in] The communications device. */
1825 DWORD evtmask) /* [in] The events that to be monitored. */
1829 TRACE("handle %d, mask %lx\n", handle, evtmask);
1831 SERVER_START_REQ( set_serial_info )
1833 req->handle = handle;
1834 req->flags = SERIALINFO_SET_MASK;
1835 req->eventmask = evtmask;
1836 ret = !SERVER_CALL_ERR();
1842 /*****************************************************************************
1843 * SetCommState (KERNEL32.@)
1845 * Re-initializes all hardware and control settings of a communications device,
1846 * with values from a device control block without effecting the input and output
1851 * True on success, false on failure eg if the XonChar is equal to the XoffChar.
1853 BOOL WINAPI SetCommState(
1854 HANDLE handle, /* [in] The communications device. */
1855 LPDCB lpdcb) /* [out] The device control block. */
1857 struct termios port;
1858 int fd, bytesize, stopbits;
1860 TRACE("handle %d, ptr %p\n", handle, lpdcb);
1861 TRACE("bytesize %d baudrate %ld fParity %d Parity %d stopbits %d\n",
1862 lpdcb->ByteSize,lpdcb->BaudRate,lpdcb->fParity, lpdcb->Parity,
1863 (lpdcb->StopBits == ONESTOPBIT)?1:
1864 (lpdcb->StopBits == TWOSTOPBITS)?2:0);
1865 TRACE("%s %s\n",(lpdcb->fInX)?"IXON":"~IXON",
1866 (lpdcb->fOutX)?"IXOFF":"~IXOFF");
1868 fd = FILE_GetUnixHandle( handle, GENERIC_READ );
1870 FIXME("no handle %d found\n",handle);
1874 if ((tcgetattr(fd,&port)) == -1) {
1875 int save_error = errno;
1876 commerror = WinError();
1878 ERR("tcgetattr error '%s'\n", strerror(save_error));
1882 port.c_cc[VMIN] = 0;
1883 port.c_cc[VTIME] = 1;
1886 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|IMAXBEL);
1888 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR);
1890 port.c_iflag |= (IGNBRK);
1892 port.c_oflag &= ~(OPOST);
1894 port.c_cflag &= ~(HUPCL);
1895 port.c_cflag |= CLOCAL | CREAD;
1897 port.c_lflag &= ~(ICANON|ECHO|ISIG);
1898 port.c_lflag |= NOFLSH;
1901 ** MJM - removed default baudrate settings
1902 ** TRACE(comm,"baudrate %ld\n",lpdcb->BaudRate);
1905 port.c_cflag &= ~CBAUD;
1906 switch (lpdcb->BaudRate) {
1909 port.c_cflag |= B110;
1913 port.c_cflag |= B300;
1917 port.c_cflag |= B600;
1921 port.c_cflag |= B1200;
1925 port.c_cflag |= B2400;
1929 port.c_cflag |= B4800;
1933 port.c_cflag |= B9600;
1937 port.c_cflag |= B19200;
1941 port.c_cflag |= B38400;
1945 port.c_cflag |= B57600;
1950 port.c_cflag |= B115200;
1955 port.c_cflag |= B230400;
1960 port.c_cflag |= B460800;
1964 commerror = IE_BAUDRATE;
1966 ERR("baudrate %ld\n",lpdcb->BaudRate);
1969 #elif !defined(__EMX__)
1970 switch (lpdcb->BaudRate) {
1973 port.c_ospeed = B110;
1977 port.c_ospeed = B300;
1981 port.c_ospeed = B600;
1985 port.c_ospeed = B1200;
1989 port.c_ospeed = B2400;
1993 port.c_ospeed = B4800;
1997 port.c_ospeed = B9600;
2001 port.c_ospeed = B19200;
2005 port.c_ospeed = B38400;
2008 commerror = IE_BAUDRATE;
2010 ERR("baudrate %ld\n",lpdcb->BaudRate);
2013 port.c_ispeed = port.c_ospeed;
2015 bytesize=lpdcb->ByteSize;
2016 stopbits=lpdcb->StopBits;
2019 port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
2021 port.c_cflag &= ~(PARENB | PARODD);
2024 port.c_iflag |= INPCK;
2026 port.c_iflag &= ~INPCK;
2027 switch (lpdcb->Parity) {
2031 port.c_cflag |= (PARENB | PARODD);
2034 port.c_cflag |= PARENB;
2037 /* Linux defines mark/space (stick) parity */
2039 port.c_cflag |= (PARENB | CMSPAR);
2042 port.c_cflag |= (PARENB | PARODD | CMSPAR);
2045 /* try the POSIX way */
2047 if( stopbits == ONESTOPBIT) {
2048 stopbits = TWOSTOPBITS;
2049 port.c_iflag &= ~INPCK;
2051 commerror = IE_BYTESIZE;
2053 ERR("Cannot set MARK Parity\n");
2060 port.c_iflag &= ~INPCK;
2062 commerror = IE_BYTESIZE;
2064 ERR("Cannot set SPACE Parity\n");
2070 commerror = IE_BYTESIZE;
2077 port.c_cflag &= ~CSIZE;
2080 port.c_cflag |= CS5;
2083 port.c_cflag |= CS6;
2086 port.c_cflag |= CS7;
2089 port.c_cflag |= CS8;
2092 commerror = IE_BYTESIZE;
2100 port.c_cflag &= ~CSTOPB;
2102 case ONE5STOPBITS: /* wil be selected if bytesize is 5 */
2104 port.c_cflag |= CSTOPB;
2107 commerror = IE_BYTESIZE;
2113 if ( lpdcb->fOutxCtsFlow ||
2114 lpdcb->fRtsControl == RTS_CONTROL_ENABLE
2117 port.c_cflag |= CRTSCTS;
2121 if (lpdcb->fDtrControl == DTR_CONTROL_ENABLE)
2123 port.c_cflag &= ~CRTSCTS;
2124 TRACE("~CRTSCTS\n");
2129 port.c_iflag |= IXON;
2131 port.c_iflag &= ~IXON;
2133 port.c_iflag |= IXOFF;
2135 port.c_iflag &= ~IXOFF;
2137 if (tcsetattr(fd,TCSANOW,&port)==-1) { /* otherwise it hangs with pending input*/
2138 int save_error=errno;
2139 commerror = WinError();
2141 ERR("tcsetattr error '%s'\n", strerror(save_error));
2151 /*****************************************************************************
2152 * GetCommState (KERNEL32.@)
2154 * Fills in a device control block with information from a communications device.
2158 * True on success, false if the communication device handle is bad etc
2162 * XonChar and XoffChar are not set.
2164 BOOL WINAPI GetCommState(
2165 HANDLE handle, /* [in] The communications device. */
2166 LPDCB lpdcb) /* [out] The device control block. */
2168 struct termios port;
2171 TRACE("handle %d, ptr %p\n", handle, lpdcb);
2173 fd = FILE_GetUnixHandle( handle, GENERIC_READ );
2176 ERR("FILE_GetUnixHandle failed\n");
2179 if (tcgetattr(fd, &port) == -1) {
2180 int save_error=errno;
2181 ERR("tcgetattr error '%s'\n", strerror(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;
2267 switch (port.c_cflag & (PARENB | PARODD | CMSPAR))
2269 switch (port.c_cflag & (PARENB | PARODD))
2273 lpdcb->Parity = NOPARITY;
2276 lpdcb->Parity = EVENPARITY;
2278 case (PARENB | PARODD):
2279 lpdcb->Parity = ODDPARITY;
2282 case (PARENB | CMSPAR):
2283 lpdcb->Parity = MARKPARITY;
2285 case (PARENB | PARODD | CMSPAR):
2286 lpdcb->Parity = SPACEPARITY;
2291 if (port.c_cflag & CSTOPB)
2292 if(lpdcb->ByteSize == 5)
2293 lpdcb->StopBits = ONE5STOPBITS;
2295 lpdcb->StopBits = TWOSTOPBITS;
2297 lpdcb->StopBits = ONESTOPBIT;
2304 if (port.c_cflag & CRTSCTS) {
2305 lpdcb->fDtrControl = DTR_CONTROL_ENABLE;
2306 lpdcb->fRtsControl = RTS_CONTROL_ENABLE;
2307 lpdcb->fOutxCtsFlow = 1;
2308 lpdcb->fOutxDsrFlow = 1;
2312 lpdcb->fDtrControl = DTR_CONTROL_DISABLE;
2313 lpdcb->fRtsControl = RTS_CONTROL_DISABLE;
2315 if (port.c_iflag & IXON)
2320 if (port.c_iflag & IXOFF)
2329 lpdcb->XoffLim = 10;
2335 TRACE("bytesize %d baudrate %ld fParity %d Parity %d stopbits %d\n",
2336 lpdcb->ByteSize,lpdcb->BaudRate,lpdcb->fParity, lpdcb->Parity,
2337 (lpdcb->StopBits == ONESTOPBIT)?1:
2338 (lpdcb->StopBits == TWOSTOPBITS)?2:0);
2339 TRACE("%s %s\n",(lpdcb->fInX)?"IXON":"~IXON",
2340 (lpdcb->fOutX)?"IXOFF":"~IXOFF");
2342 if ( lpdcb->fOutxCtsFlow ||
2343 lpdcb->fDtrControl == DTR_CONTROL_ENABLE||
2344 lpdcb->fRtsControl == RTS_CONTROL_ENABLE
2348 if (lpdcb->fDtrControl == DTR_CONTROL_DISABLE)
2349 TRACE("~CRTSCTS\n");
2355 /*****************************************************************************
2356 * TransmitCommChar (KERNEL32.@)
2358 * Transmits a single character in front of any pending characters in the
2359 * output buffer. Usually used to send an interrupt character to a host.
2363 * True if the call succeeded, false if the previous command character to the
2364 * same device has not been sent yet the handle is bad etc.
2370 BOOL WINAPI TransmitCommChar(
2371 HANDLE hComm, /* [in] The communication device in need of a command character. */
2372 CHAR chTransmit) /* [in] The character to transmit. */
2374 FIXME("(%x,'%c'), use win32 handle!\n",hComm,chTransmit);
2378 /*****************************************************************************
2379 * GetCommTimeouts (KERNEL32.@)
2381 * Obtains the request time out values for the communications device.
2385 * True on success, false if communications device handle is bad
2386 * or the target structure is null.
2388 BOOL WINAPI GetCommTimeouts(
2389 HANDLE hComm, /* [in] The communications device. */
2390 LPCOMMTIMEOUTS lptimeouts) /* [out] The struct of request time outs. */
2394 TRACE("(%x,%p)\n",hComm,lptimeouts);
2398 SetLastError(ERROR_INVALID_PARAMETER);
2402 SERVER_START_REQ( get_serial_info )
2404 req->handle = hComm;
2405 if ((ret = !SERVER_CALL_ERR()))
2407 lptimeouts->ReadIntervalTimeout = req->readinterval;
2408 lptimeouts->ReadTotalTimeoutMultiplier = req->readmult;
2409 lptimeouts->ReadTotalTimeoutConstant = req->readconst;
2410 lptimeouts->WriteTotalTimeoutMultiplier = req->writemult;
2411 lptimeouts->WriteTotalTimeoutConstant = req->writeconst;
2418 /*****************************************************************************
2419 * SetCommTimeouts (KERNEL32.@)
2421 * Sets the timeouts used when reading and writing data to/from COMM ports.
2423 * ReadIntervalTimeout
2424 * - converted and passes to linux kernel as c_cc[VTIME]
2425 * ReadTotalTimeoutMultiplier, ReadTotalTimeoutConstant
2426 * - used in ReadFile to calculate GetOverlappedResult's timeout
2427 * WriteTotalTimeoutMultiplier, WriteTotalTimeoutConstant
2428 * - used in WriteFile to calculate GetOverlappedResult's timeout
2432 * True if the time outs were set, false otherwise.
2434 BOOL WINAPI SetCommTimeouts(
2435 HANDLE hComm, /* [in] handle of COMM device */
2436 LPCOMMTIMEOUTS lptimeouts) /* [in] pointer to COMMTIMEOUTS structure */
2440 struct termios tios;
2442 TRACE("(%x,%p)\n",hComm,lptimeouts);
2446 SetLastError(ERROR_INVALID_PARAMETER);
2450 SERVER_START_REQ( set_serial_info )
2452 req->handle = hComm;
2453 req->flags = SERIALINFO_SET_TIMEOUTS;
2454 req->readinterval = lptimeouts->ReadIntervalTimeout ;
2455 req->readmult = lptimeouts->ReadTotalTimeoutMultiplier ;
2456 req->readconst = lptimeouts->ReadTotalTimeoutConstant ;
2457 req->writemult = lptimeouts->WriteTotalTimeoutMultiplier ;
2458 req->writeconst = lptimeouts->WriteTotalTimeoutConstant ;
2459 ret = !SERVER_CALL_ERR();
2462 if (!ret) return FALSE;
2464 /* FIXME: move this stuff to the server */
2465 fd = FILE_GetUnixHandle( hComm, GENERIC_READ );
2467 FIXME("no fd for handle = %0x!.\n",hComm);
2471 if (-1==tcgetattr(fd,&tios)) {
2472 FIXME("tcgetattr on fd %d failed!\n",fd);
2476 /* VTIME is in 1/10 seconds */
2478 unsigned int ux_timeout;
2480 if(lptimeouts->ReadIntervalTimeout == 0) /* 0 means no timeout */
2486 ux_timeout = (lptimeouts->ReadIntervalTimeout+99)/100;
2489 ux_timeout = 1; /* must be at least some timeout */
2492 tios.c_cc[VTIME] = ux_timeout;
2495 if (-1==tcsetattr(fd,0,&tios)) {
2496 FIXME("tcsetattr on fd %d failed!\n",fd);
2503 /***********************************************************************
2504 * GetCommModemStatus (KERNEL32.@)
2506 * Obtains the four control register bits if supported by the hardware.
2510 * True if the communications handle was good and for hardware that
2511 * control register access, false otherwise.
2513 BOOL WINAPI GetCommModemStatus(
2514 HANDLE hFile, /* [in] The communications device. */
2515 LPDWORD lpModemStat) /* [out] The control register bits. */
2517 int fd,mstat, result=FALSE;
2521 fd = FILE_GetUnixHandle( hFile, GENERIC_READ );
2524 result = ioctl(fd, TIOCMGET, &mstat);
2528 WARN("ioctl failed\n");
2532 if (mstat & TIOCM_CTS)
2533 *lpModemStat |= MS_CTS_ON;
2536 if (mstat & TIOCM_DSR)
2537 *lpModemStat |= MS_DSR_ON;
2540 if (mstat & TIOCM_RNG)
2541 *lpModemStat |= MS_RING_ON;
2544 /*FIXME: Not really sure about RLSD UB 990810*/
2545 if (mstat & TIOCM_CAR)
2546 *lpModemStat |= MS_RLSD_ON;
2548 TRACE("%04x -> %s%s%s%s\n", mstat,
2549 (*lpModemStat &MS_RLSD_ON)?"MS_RLSD_ON ":"",
2550 (*lpModemStat &MS_RING_ON)?"MS_RING_ON ":"",
2551 (*lpModemStat &MS_DSR_ON)?"MS_DSR_ON ":"",
2552 (*lpModemStat &MS_CTS_ON)?"MS_CTS_ON ":"");
2559 /***********************************************************************
2560 * COMM_WaitCommEventService (INTERNAL)
2562 * This function is called while the client is waiting on the
2563 * server, so we can't make any server calls here.
2565 static void COMM_WaitCommEventService(async_private *ovp, int events)
2567 LPOVERLAPPED lpOverlapped = ovp->lpOverlapped;
2569 TRACE("overlapped %p wait complete %p <- %x\n",lpOverlapped,ovp->buffer,events);
2572 lpOverlapped->Internal = STATUS_HANDLES_CLOSED;
2578 *ovp->buffer = EV_RXCHAR;
2581 lpOverlapped->Internal = STATUS_SUCCESS;
2586 /***********************************************************************
2587 * COMM_WaitCommEvent (INTERNAL)
2589 * This function must have an lpOverlapped.
2591 static BOOL COMM_WaitCommEvent(
2592 HANDLE hFile, /* [in] handle of comm port to wait for */
2593 LPDWORD lpdwEvents, /* [out] event(s) that were detected */
2594 LPOVERLAPPED lpOverlapped) /* [in/out] for Asynchronous waiting */
2601 SetLastError(ERROR_INVALID_PARAMETER);
2605 if(NtResetEvent(lpOverlapped->hEvent,NULL))
2608 lpOverlapped->Internal = STATUS_PENDING;
2609 lpOverlapped->InternalHigh = 0;
2610 lpOverlapped->Offset = 0;
2611 lpOverlapped->OffsetHigh = 0;
2613 /* start an ASYNCHRONOUS WaitCommEvent */
2614 SERVER_START_REQ( create_async )
2616 req->file_handle = hFile;
2618 req->type = ASYNC_TYPE_WAIT;
2620 ret=SERVER_CALL_ERR();
2627 fd = FILE_GetUnixHandle( hFile, GENERIC_WRITE );
2631 ovp = (async_private *) HeapAlloc(GetProcessHeap(), 0, sizeof (async_private));
2637 ovp->lpOverlapped = lpOverlapped;
2640 ovp->tv.tv_usec = 0;
2641 ovp->event = POLLIN;
2642 ovp->func = COMM_WaitCommEventService;
2643 ovp->buffer = (char *)lpdwEvents;
2646 ovp->completion_func = 0;
2648 ovp->next = NtCurrentTeb()->pending_list;
2651 ovp->next->prev=ovp;
2652 NtCurrentTeb()->pending_list = ovp;
2654 SetLastError(ERROR_IO_PENDING);
2659 /***********************************************************************
2660 * WaitCommEvent (KERNEL32.@)
2662 * Wait until something interesting happens on a COMM port.
2663 * Interesting things (events) are set by calling SetCommMask before
2664 * this function is called.
2667 * TRUE if successful
2670 * The set of detected events will be written to *lpdwEventMask
2671 * ERROR_IO_PENDING will be returned the overlapped structure was passed
2674 * Only supports EV_RXCHAR and EV_TXEMPTY
2676 BOOL WINAPI WaitCommEvent(
2677 HANDLE hFile, /* [in] handle of comm port to wait for */
2678 LPDWORD lpdwEvents, /* [out] event(s) that were detected */
2679 LPOVERLAPPED lpOverlapped) /* [in/out] for Asynchronous waiting */
2684 TRACE("(%x %p %p )\n",hFile, lpdwEvents,lpOverlapped);
2687 return COMM_WaitCommEvent(hFile, lpdwEvents, lpOverlapped);
2689 /* if there is no overlapped structure, create our own */
2690 ov.hEvent = CreateEventA(NULL,FALSE,FALSE,NULL);
2692 COMM_WaitCommEvent(hFile, lpdwEvents, &ov);
2694 if(GetLastError()!=STATUS_PENDING)
2696 CloseHandle(ov.hEvent);
2700 /* wait for the overlapped to complete */
2701 ret = GetOverlappedResult(hFile, &ov, NULL, TRUE);
2702 CloseHandle(ov.hEvent);
2707 /***********************************************************************
2708 * GetCommProperties (KERNEL32.@)
2710 * This function fills in a structure with the capabilities of the
2711 * communications port driver.
2715 * TRUE on success, FALSE on failure
2716 * If successful, the lpCommProp structure be filled in with
2717 * properties of the comm port.
2719 BOOL WINAPI GetCommProperties(
2720 HANDLE hFile, /* [in] handle of the comm port */
2721 LPCOMMPROP lpCommProp) /* [out] pointer to struct to be filled */
2723 FIXME("(%d %p )\n",hFile,lpCommProp);
2728 * These values should be valid for LINUX's serial driver
2729 * FIXME: Perhaps they deserve an #ifdef LINUX
2731 memset(lpCommProp,0,sizeof(COMMPROP));
2732 lpCommProp->wPacketLength = 1;
2733 lpCommProp->wPacketVersion = 1;
2734 lpCommProp->dwServiceMask = SP_SERIALCOMM;
2735 lpCommProp->dwReserved1 = 0;
2736 lpCommProp->dwMaxTxQueue = 4096;
2737 lpCommProp->dwMaxRxQueue = 4096;
2738 lpCommProp->dwMaxBaud = BAUD_115200;
2739 lpCommProp->dwProvSubType = PST_RS232;
2740 lpCommProp->dwProvCapabilities = PCF_DTRDSR | PCF_PARITY_CHECK | PCF_RTSCTS ;
2741 lpCommProp->dwSettableParams = SP_BAUD | SP_DATABITS | SP_HANDSHAKING |
2742 SP_PARITY | SP_PARITY_CHECK | SP_STOPBITS ;
2743 lpCommProp->dwSettableBaud = BAUD_075 | BAUD_110 | BAUD_134_5 | BAUD_150 |
2744 BAUD_300 | BAUD_600 | BAUD_1200 | BAUD_1800 | BAUD_2400 | BAUD_4800 |
2745 BAUD_9600 | BAUD_19200 | BAUD_38400 | BAUD_57600 | BAUD_115200 ;
2746 lpCommProp->wSettableData = DATABITS_5 | DATABITS_6 | DATABITS_7 | DATABITS_8 ;
2747 lpCommProp->wSettableStopParity = STOPBITS_10 | STOPBITS_15 | STOPBITS_20 |
2748 PARITY_NONE | PARITY_ODD |PARITY_EVEN | PARITY_MARK | PARITY_SPACE;
2749 lpCommProp->dwCurrentTxQueue = lpCommProp->dwMaxTxQueue;
2750 lpCommProp->dwCurrentRxQueue = lpCommProp->dwMaxRxQueue;
2755 /***********************************************************************
2757 * The functionality of CommConfigDialogA, GetDefaultCommConfig and
2758 * SetDefaultCommConfig is implemented in a DLL (usually SERIALUI.DLL).
2759 * This is dependent on the type of COMM port, but since it is doubtful
2760 * anybody will get around to implementing support for fancy serial
2761 * ports in WINE, this is hardcoded for the time being. The name of
2762 * this DLL should be stored in and read from the system registry in
2763 * the hive HKEY_LOCAL_MACHINE, key
2764 * System\\CurrentControlSet\\Services\\Class\\Ports\\????
2765 * where ???? is the port number... that is determined by PNP
2766 * The DLL should be loaded when the COMM port is opened, and closed
2767 * when the COMM port is closed. - MJM 20 June 2000
2768 ***********************************************************************/
2769 static CHAR lpszSerialUI[] = "serialui.dll";
2772 /***********************************************************************
2773 * CommConfigDialogA (KERNEL32.@)
2775 * Raises a dialog that allows the user to configure a comm port.
2776 * Fills the COMMCONFIG struct with information specified by the user.
2777 * This function should call a similar routine in the COMM driver...
2781 * TRUE on success, FALSE on failure
2782 * If successful, the lpCommConfig structure will contain a new
2783 * configuration for the comm port, as specified by the user.
2786 * The library with the CommConfigDialog code is never unloaded.
2787 * Perhaps this should be done when the comm port is closed?
2789 BOOL WINAPI CommConfigDialogA(
2790 LPCSTR lpszDevice, /* [in] name of communications device */
2791 HANDLE hWnd, /* [in] parent window for the dialog */
2792 LPCOMMCONFIG lpCommConfig) /* [out] pointer to struct to fill */
2794 FARPROC lpfnCommDialog;
2795 HMODULE hConfigModule;
2798 TRACE("(%p %x %p)\n",lpszDevice, hWnd, lpCommConfig);
2800 hConfigModule = LoadLibraryA(lpszSerialUI);
2804 lpfnCommDialog = GetProcAddress(hConfigModule, (LPCSTR)3L);
2809 r = lpfnCommDialog(lpszDevice,hWnd,lpCommConfig);
2811 /* UnloadLibrary(hConfigModule); */
2816 /***********************************************************************
2817 * CommConfigDialogW (KERNEL32.@)
2819 * see CommConfigDialogA for more info
2821 BOOL WINAPI CommConfigDialogW(
2822 LPCWSTR lpszDevice, /* [in] name of communications device */
2823 HANDLE hWnd, /* [in] parent window for the dialog */
2824 LPCOMMCONFIG lpCommConfig) /* [out] pointer to struct to fill */
2829 lpDeviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpszDevice );
2832 r = CommConfigDialogA(lpDeviceA,hWnd,lpCommConfig);
2833 HeapFree( GetProcessHeap(), 0, lpDeviceA );
2837 /***********************************************************************
2838 * GetCommConfig (KERNEL32.@)
2840 * Fill in the COMMCONFIG structure for the comm port hFile
2844 * TRUE on success, FALSE on failure
2845 * If successful, lpCommConfig contains the comm port configuration.
2849 * The signature is missing a the parameter for the size of the COMMCONFIG
2850 * structure/buffer it should be
2851 * BOOL WINAPI GetCommConfig(HANDLE hFile,LPCOMMCONFIG lpCommConfig,LPDWORD lpdwSize)
2853 BOOL WINAPI GetCommConfig(
2854 HANDLE hFile, /* [in] The communications device. */
2855 LPCOMMCONFIG lpCommConfig, /* [out] The communications configuration of the device (if it fits). */
2856 LPDWORD lpdwSize) /* [in/out] Initially the size of the configuration buffer/structure,
2857 afterwards the number of bytes copied to the buffer or
2858 the needed size of the buffer. */
2862 TRACE("(%x %p)\n",hFile,lpCommConfig);
2864 if(lpCommConfig == NULL)
2867 r = *lpdwSize < sizeof(COMMCONFIG);
2868 *lpdwSize = sizeof(COMMCONFIG);
2872 lpCommConfig->dwSize = sizeof(COMMCONFIG);
2873 lpCommConfig->wVersion = 1;
2874 lpCommConfig->wReserved = 0;
2875 r = GetCommState(hFile,&lpCommConfig->dcb);
2876 lpCommConfig->dwProviderSubType = PST_RS232;
2877 lpCommConfig->dwProviderOffset = 0;
2878 lpCommConfig->dwProviderSize = 0;
2883 /***********************************************************************
2884 * SetCommConfig (KERNEL32.@)
2886 * Sets the configuration of the commications device.
2890 * True on success, false if the handle was bad is not a communications device.
2892 BOOL WINAPI SetCommConfig(
2893 HANDLE hFile, /* [in] The communications device. */
2894 LPCOMMCONFIG lpCommConfig) /* [in] The desired configuration. */
2896 TRACE("(%x %p)\n",hFile,lpCommConfig);
2897 return SetCommState(hFile,&lpCommConfig->dcb);
2900 /***********************************************************************
2901 * SetDefaultCommConfigA (KERNEL32.@)
2903 * Initializes the default configuration for the specified communication
2908 * True if the device was found and the defaults set, false otherwise
2910 BOOL WINAPI SetDefaultCommConfigA(
2911 LPCSTR lpszDevice, /* [in] The ascii name of the device targeted for configuration. */
2912 LPCOMMCONFIG lpCommConfig, /* [in] The default configuration for the device. */
2913 DWORD dwSize) /* [in] The number of bytes in the configuration structure. */
2915 FARPROC lpfnSetDefaultCommConfig;
2916 HMODULE hConfigModule;
2919 TRACE("(%p %p %lx)\n",lpszDevice, lpCommConfig, dwSize);
2921 hConfigModule = LoadLibraryA(lpszSerialUI);
2925 lpfnSetDefaultCommConfig = GetProcAddress(hConfigModule, (LPCSTR)4L);
2927 if(! lpfnSetDefaultCommConfig)
2930 r = lpfnSetDefaultCommConfig(lpszDevice, lpCommConfig, dwSize);
2932 /* UnloadLibrary(hConfigModule); */
2938 /***********************************************************************
2939 * SetDefaultCommConfigW (KERNEL32.@)
2941 * Initializes the default configuration for the specified
2942 * communication device. (unicode)
2947 BOOL WINAPI SetDefaultCommConfigW(
2948 LPCWSTR lpszDevice, /* [in] The unicode name of the device targeted for configuration. */
2949 LPCOMMCONFIG lpCommConfig, /* [in] The default configuration for the device. */
2950 DWORD dwSize) /* [in] The number of bytes in the configuration structure. */
2955 TRACE("(%s %p %lx)\n",debugstr_w(lpszDevice),lpCommConfig,dwSize);
2957 lpDeviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpszDevice );
2960 r = SetDefaultCommConfigA(lpDeviceA,lpCommConfig,dwSize);
2961 HeapFree( GetProcessHeap(), 0, lpDeviceA );
2966 /***********************************************************************
2967 * GetDefaultCommConfigA (KERNEL32.@)
2969 * Acquires the default configuration of the specified communication device. (unicode)
2973 * True on successful reading of the default configuration,
2974 * if the device is not found or the buffer is too small.
2976 BOOL WINAPI GetDefaultCommConfigA(
2977 LPCSTR lpszName, /* [in] The ascii name of the device targeted for configuration. */
2978 LPCOMMCONFIG lpCC, /* [out] The default configuration for the device. */
2979 LPDWORD lpdwSize) /* [in/out] Initially the size of the default configuration buffer,
2980 afterwards the number of bytes copied to the buffer or
2981 the needed size of the buffer. */
2983 LPDCB lpdcb = &(lpCC->dcb);
2986 if (strncasecmp(lpszName,"COM",3)) {
2987 ERR("not implemented for <%s>\n", lpszName);
2991 if (!ValidCOMPort(lpszName[3]-'1'))
2994 TRACE("(%s %p %ld)\n", lpszName, lpCC, *lpdwSize );
2995 if (*lpdwSize < sizeof(COMMCONFIG)) {
2996 *lpdwSize = sizeof(COMMCONFIG);
3000 *lpdwSize = sizeof(COMMCONFIG);
3002 lpCC->dwSize = sizeof(COMMCONFIG);
3004 lpCC->dwProviderSubType = PST_RS232;
3005 lpCC->dwProviderOffset = 0L;
3006 lpCC->dwProviderSize = 0L;
3008 (void) sprintf( temp, "COM%c:38400,n,8,1", lpszName[3]);
3009 FIXME("setting %s as default\n", temp);
3011 return BuildCommDCBA( temp, lpdcb);
3014 /**************************************************************************
3015 * GetDefaultCommConfigW (KERNEL32.@)
3017 * Acquires the default configuration of the specified communication device. (unicode)
3021 * True on successful reading of the default configuration,
3022 * if the device is not found or the buffer is too small.
3024 BOOL WINAPI GetDefaultCommConfigW(
3025 LPCWSTR lpszName, /* [in] The unicode name of the device targeted for configuration. */
3026 LPCOMMCONFIG lpCC, /* [out] The default configuration for the device. */
3027 LPDWORD lpdwSize) /* [in/out] Initially the size of the default configuration buffer,
3028 afterwards the number of bytes copied to the buffer or
3029 the needed size of the buffer. */
3034 TRACE("(%p,%p,%ld)\n",lpszName,lpCC,*lpdwSize);
3035 lpszNameA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpszName );
3038 ret=GetDefaultCommConfigA(lpszNameA,lpCC,lpdwSize);
3039 HeapFree( GetProcessHeap(), 0, lpszNameA );