1 /* Main file for COMM support
3 * DEC 93 Erik Bos <erik@xs4all.nl>
4 * Copyright 1996 Marcus Meissner
5 * Copyright 2005,2006 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_FILIO_H
48 # include <sys/filio.h>
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
56 #ifdef HAVE_SYS_MODEM_H
57 # include <sys/modem.h>
59 #ifdef HAVE_SYS_STRTIO_H
60 # include <sys/strtio.h>
63 #define NONAMELESSUNION
64 #define NONAMELESSSTRUCT
66 #define WIN32_NO_STATUS
70 #include "ddk/ntddser.h"
71 #include "ntdll_misc.h"
72 #include "wine/server.h"
73 #include "wine/library.h"
74 #include "wine/debug.h"
76 #ifdef HAVE_LINUX_SERIAL_H
77 #ifdef HAVE_ASM_TYPES_H
78 #include <asm/types.h>
80 #include <linux/serial.h>
83 #if !defined(TIOCINQ) && defined(FIONREAD)
84 #define TIOCINQ FIONREAD
87 WINE_DEFAULT_DEBUG_CHANNEL(comm);
89 static const char* iocode2str(DWORD ioc)
93 #define X(x) case (x): return #x
94 X(IOCTL_SERIAL_CLEAR_STATS);
95 X(IOCTL_SERIAL_CLR_DTR);
96 X(IOCTL_SERIAL_CLR_RTS);
97 X(IOCTL_SERIAL_CONFIG_SIZE);
98 X(IOCTL_SERIAL_GET_BAUD_RATE);
99 X(IOCTL_SERIAL_GET_CHARS);
100 X(IOCTL_SERIAL_GET_COMMSTATUS);
101 X(IOCTL_SERIAL_GET_DTRRTS);
102 X(IOCTL_SERIAL_GET_HANDFLOW);
103 X(IOCTL_SERIAL_GET_LINE_CONTROL);
104 X(IOCTL_SERIAL_GET_MODEM_CONTROL);
105 X(IOCTL_SERIAL_GET_MODEMSTATUS);
106 X(IOCTL_SERIAL_GET_PROPERTIES);
107 X(IOCTL_SERIAL_GET_STATS);
108 X(IOCTL_SERIAL_GET_TIMEOUTS);
109 X(IOCTL_SERIAL_GET_WAIT_MASK);
110 X(IOCTL_SERIAL_IMMEDIATE_CHAR);
111 X(IOCTL_SERIAL_LSRMST_INSERT);
112 X(IOCTL_SERIAL_PURGE);
113 X(IOCTL_SERIAL_RESET_DEVICE);
114 X(IOCTL_SERIAL_SET_BAUD_RATE);
115 X(IOCTL_SERIAL_SET_BREAK_ON);
116 X(IOCTL_SERIAL_SET_BREAK_OFF);
117 X(IOCTL_SERIAL_SET_CHARS);
118 X(IOCTL_SERIAL_SET_DTR);
119 X(IOCTL_SERIAL_SET_FIFO_CONTROL);
120 X(IOCTL_SERIAL_SET_HANDFLOW);
121 X(IOCTL_SERIAL_SET_LINE_CONTROL);
122 X(IOCTL_SERIAL_SET_MODEM_CONTROL);
123 X(IOCTL_SERIAL_SET_QUEUE_SIZE);
124 X(IOCTL_SERIAL_SET_RTS);
125 X(IOCTL_SERIAL_SET_TIMEOUTS);
126 X(IOCTL_SERIAL_SET_WAIT_MASK);
127 X(IOCTL_SERIAL_SET_XOFF);
128 X(IOCTL_SERIAL_SET_XON);
129 X(IOCTL_SERIAL_WAIT_ON_MASK);
130 X(IOCTL_SERIAL_XOFF_COUNTER);
132 default: { static char tmp[32]; sprintf(tmp, "IOCTL_SERIAL_%d\n", ioc); return tmp; }
136 static NTSTATUS get_baud_rate(int fd, SERIAL_BAUD_RATE* sbr)
141 if (tcgetattr(fd, &port) == -1)
143 ERR("tcgetattr error '%s'\n", strerror(errno));
144 return FILE_GetNtStatus();
146 speed = cfgetospeed(&port);
149 case B0: sbr->BaudRate = 0; break;
150 case B50: sbr->BaudRate = 50; break;
151 case B75: sbr->BaudRate = 75; break;
152 case B110: sbr->BaudRate = 110; break;
153 case B134: sbr->BaudRate = 134; break;
154 case B150: sbr->BaudRate = 150; break;
155 case B200: sbr->BaudRate = 200; break;
156 case B300: sbr->BaudRate = 300; break;
157 case B600: sbr->BaudRate = 600; break;
158 case B1200: sbr->BaudRate = 1200; break;
159 case B1800: sbr->BaudRate = 1800; break;
160 case B2400: sbr->BaudRate = 2400; break;
161 case B4800: sbr->BaudRate = 4800; break;
162 case B9600: sbr->BaudRate = 9600; break;
163 case B19200: sbr->BaudRate = 19200; break;
164 case B38400: sbr->BaudRate = 38400; break;
166 case B57600: sbr->BaudRate = 57600; break;
169 case B115200: sbr->BaudRate = 115200; break;
172 case B230400: sbr->BaudRate = 230400; break;
175 case B460800: sbr->BaudRate = 460800; break;
178 ERR("unknown speed %x\n", speed);
179 return STATUS_INVALID_PARAMETER;
181 return STATUS_SUCCESS;
184 static NTSTATUS get_hand_flow(int fd, SERIAL_HANDFLOW* shf)
189 if (tcgetattr(fd, &port) == -1)
191 ERR("tcgetattr error '%s'\n", strerror(errno));
192 return FILE_GetNtStatus();
194 /* termios does not support DTR/DSR flow control */
195 shf->ControlHandShake = 0;
196 shf->FlowReplace = 0;
198 if (ioctl(fd, TIOCMGET, &stat) == -1)
200 WARN("ioctl error '%s'\n", strerror(errno));
201 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
202 shf->FlowReplace |= SERIAL_RTS_CONTROL;
205 WARN("Setting DTR/RTS to enabled by default\n");
206 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
207 shf->FlowReplace |= SERIAL_RTS_CONTROL;
210 if (stat & TIOCM_DTR)
212 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
214 if (port.c_cflag & CRTSCTS)
216 shf->FlowReplace |= SERIAL_RTS_CONTROL;
217 shf->ControlHandShake |= SERIAL_CTS_HANDSHAKE;
223 if (stat & TIOCM_RTS)
225 shf->FlowReplace |= SERIAL_RTS_CONTROL;
227 if (port.c_iflag & IXOFF)
228 shf->FlowReplace |= SERIAL_AUTO_RECEIVE;
229 if (port.c_iflag & IXON)
230 shf->FlowReplace |= SERIAL_AUTO_TRANSMIT;
234 return STATUS_SUCCESS;
237 static NTSTATUS get_line_control(int fd, SERIAL_LINE_CONTROL* slc)
241 if (tcgetattr(fd, &port) == -1)
243 ERR("tcgetattr error '%s'\n", strerror(errno));
244 return FILE_GetNtStatus();
248 switch (port.c_cflag & (PARENB | PARODD | CMSPAR))
250 switch (port.c_cflag & (PARENB | PARODD))
253 case 0: slc->Parity = NOPARITY; break;
254 case PARENB: slc->Parity = EVENPARITY; break;
255 case PARENB|PARODD: slc->Parity = ODDPARITY; break;
257 case PARENB|CMSPAR: slc->Parity = MARKPARITY; break;
258 case PARENB|PARODD|CMSPAR: slc->Parity = SPACEPARITY; break;
261 switch (port.c_cflag & CSIZE)
263 case CS5: slc->WordLength = 5; break;
264 case CS6: slc->WordLength = 6; break;
265 case CS7: slc->WordLength = 7; break;
266 case CS8: slc->WordLength = 8; break;
267 default: ERR("unknown size %x\n", (UINT)(port.c_cflag & CSIZE));
270 if (port.c_cflag & CSTOPB)
272 if (slc->WordLength == 5)
273 slc->StopBits = ONE5STOPBITS;
275 slc->StopBits = TWOSTOPBITS;
278 slc->StopBits = ONESTOPBIT;
280 return STATUS_SUCCESS;
283 static NTSTATUS get_modem_status(int fd, DWORD* lpModemStat)
285 NTSTATUS status = STATUS_NOT_SUPPORTED;
290 if (!ioctl(fd, TIOCMGET, &mstat))
293 if (mstat & TIOCM_CTS) *lpModemStat |= MS_CTS_ON;
296 if (mstat & TIOCM_DSR) *lpModemStat |= MS_DSR_ON;
299 if (mstat & TIOCM_RNG) *lpModemStat |= MS_RING_ON;
302 /* FIXME: Not really sure about RLSD UB 990810 */
303 if (mstat & TIOCM_CAR) *lpModemStat |= MS_RLSD_ON;
305 TRACE("%04x -> %s%s%s%s\n", mstat,
306 (*lpModemStat & MS_RLSD_ON) ? "MS_RLSD_ON " : "",
307 (*lpModemStat & MS_RING_ON) ? "MS_RING_ON " : "",
308 (*lpModemStat & MS_DSR_ON) ? "MS_DSR_ON " : "",
309 (*lpModemStat & MS_CTS_ON) ? "MS_CTS_ON " : "");
310 return STATUS_SUCCESS;
312 WARN("ioctl failed\n");
313 status = FILE_GetNtStatus();
318 static NTSTATUS get_special_chars(int fd, SERIAL_CHARS* sc)
322 if (tcgetattr(fd, &port) == -1)
324 ERR("tcgetattr error '%s'\n", strerror(errno));
325 return FILE_GetNtStatus();
327 sc->EofChar = port.c_cc[VEOF];
328 sc->ErrorChar = 0xFF;
329 sc->BreakChar = 0; /* FIXME */
330 sc->EventChar = 0; /* FIXME */
331 sc->XonChar = port.c_cc[VSTART];
332 sc->XoffChar = port.c_cc[VSTOP];
334 return STATUS_SUCCESS;
337 static NTSTATUS get_status(int fd, SERIAL_STATUS* ss)
339 NTSTATUS status = STATUS_SUCCESS;
343 ss->EofReceived = FALSE;
344 ss->WaitForImmediate = FALSE;
346 if (ioctl(fd, TIOCOUTQ, &ss->AmountInOutQueue) == -1)
348 WARN("ioctl returned error\n");
349 status = FILE_GetNtStatus();
352 ss->AmountInOutQueue = 0; /* FIXME: find a different way to find out */
356 if (ioctl(fd, TIOCINQ, &ss->AmountInInQueue))
358 WARN("ioctl returned error\n");
359 status = FILE_GetNtStatus();
362 ss->AmountInInQueue = 0; /* FIXME: find a different way to find out */
367 static NTSTATUS get_timeouts(HANDLE handle, SERIAL_TIMEOUTS* st)
370 SERVER_START_REQ( get_serial_info )
372 req->handle = wine_server_obj_handle( handle );
373 if (!(status = wine_server_call( req )))
375 st->ReadIntervalTimeout = reply->readinterval;
376 st->ReadTotalTimeoutMultiplier = reply->readmult;
377 st->ReadTotalTimeoutConstant = reply->readconst;
378 st->WriteTotalTimeoutMultiplier = reply->writemult;
379 st->WriteTotalTimeoutConstant = reply->writeconst;
386 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD* mask)
390 SERVER_START_REQ( get_serial_info )
392 req->handle = wine_server_obj_handle( hDevice );
393 if (!(status = wine_server_call( req )))
394 *mask = reply->eventmask;
400 static NTSTATUS purge(int fd, DWORD flags)
403 ** not exactly sure how these are different
404 ** Perhaps if we had our own internal queues, one flushes them
405 ** and the other flushes the kernel's buffers.
407 if (flags & PURGE_TXABORT) tcflush(fd, TCOFLUSH);
408 if (flags & PURGE_RXABORT) tcflush(fd, TCIFLUSH);
409 if (flags & PURGE_TXCLEAR) tcflush(fd, TCOFLUSH);
410 if (flags & PURGE_RXCLEAR) tcflush(fd, TCIFLUSH);
411 return STATUS_SUCCESS;
414 static NTSTATUS set_baud_rate(int fd, const SERIAL_BAUD_RATE* sbr)
418 if (tcgetattr(fd, &port) == -1)
420 ERR("tcgetattr error '%s'\n", strerror(errno));
421 return FILE_GetNtStatus();
424 switch (sbr->BaudRate)
426 case 0: cfsetospeed( &port, B0 ); break;
427 case 50: cfsetospeed( &port, B50 ); break;
428 case 75: cfsetospeed( &port, B75 ); break;
430 case CBR_110: cfsetospeed( &port, B110 ); break;
431 case 134: cfsetospeed( &port, B134 ); break;
432 case 150: cfsetospeed( &port, B150 ); break;
433 case 200: cfsetospeed( &port, B200 ); break;
435 case CBR_300: cfsetospeed( &port, B300 ); break;
437 case CBR_600: cfsetospeed( &port, B600 ); break;
439 case CBR_1200: cfsetospeed( &port, B1200 ); break;
440 case 1800: cfsetospeed( &port, B1800 ); break;
442 case CBR_2400: cfsetospeed( &port, B2400 ); break;
444 case CBR_4800: cfsetospeed( &port, B4800 ); break;
446 case CBR_9600: cfsetospeed( &port, B9600 ); break;
448 case CBR_19200: cfsetospeed( &port, B19200 ); break;
450 case CBR_38400: cfsetospeed( &port, B38400 ); break;
452 case 57600: cfsetospeed( &port, B57600 ); break;
455 case 115200: cfsetospeed( &port, B115200 ); break;
458 case 230400: cfsetospeed( &port, B230400 ); break;
461 case 460800: cfsetospeed( &port, B460800 ); break;
464 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
466 struct serial_struct nuts;
469 ioctl(fd, TIOCGSERIAL, &nuts);
470 nuts.custom_divisor = nuts.baud_base / sbr->BaudRate;
471 if (!(nuts.custom_divisor)) nuts.custom_divisor = 1;
472 arby = nuts.baud_base / nuts.custom_divisor;
473 nuts.flags &= ~ASYNC_SPD_MASK;
474 nuts.flags |= ASYNC_SPD_CUST;
475 WARN("You (or a program acting at your behest) have specified\n"
476 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
477 "which is as close as we can get by our present understanding of your\n"
478 "hardware. I hope you know what you are doing. Any disruption Wine\n"
479 "has caused to your linux system can be undone with setserial\n"
480 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
481 "reset it and it will probably recover.\n", sbr->BaudRate, arby);
482 ioctl(fd, TIOCSSERIAL, &nuts);
483 cfsetospeed( &port, B38400 );
486 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
487 ERR("baudrate %d\n", sbr->BaudRate);
488 return STATUS_NOT_SUPPORTED;
489 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
491 cfsetispeed( &port, cfgetospeed(&port) );
492 if (tcsetattr(fd, TCSANOW, &port) == -1)
494 ERR("tcsetattr error '%s'\n", strerror(errno));
495 return FILE_GetNtStatus();
497 return STATUS_SUCCESS;
500 static int whack_modem(int fd, unsigned int andy, unsigned int orrie)
503 unsigned int mstat, okay;
504 okay = ioctl(fd, TIOCMGET, &mstat);
505 if (okay) return okay;
506 if (andy) mstat &= andy;
508 return ioctl(fd, TIOCMSET, &mstat);
514 static NTSTATUS set_handflow(int fd, const SERIAL_HANDFLOW* shf)
518 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE)) ==
519 (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
520 return STATUS_NOT_SUPPORTED;
522 if (tcgetattr(fd, &port) == -1)
524 ERR("tcgetattr error '%s'\n", strerror(errno));
525 return FILE_GetNtStatus();
529 if ((shf->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
530 (shf->FlowReplace & SERIAL_RTS_HANDSHAKE))
532 port.c_cflag |= CRTSCTS;
536 port.c_cflag &= ~CRTSCTS;
539 if (shf->ControlHandShake & SERIAL_DTR_HANDSHAKE)
541 WARN("DSR/DTR flow control not supported\n");
542 } else if (!(shf->ControlHandShake & SERIAL_DTR_CONTROL))
543 whack_modem(fd, ~TIOCM_DTR, 0);
545 whack_modem(fd, 0, TIOCM_DTR);
548 if (!(shf->ControlHandShake & SERIAL_CTS_HANDSHAKE))
550 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL|SERIAL_RTS_HANDSHAKE)) == 0)
551 whack_modem(fd, ~TIOCM_RTS, 0);
553 whack_modem(fd, 0, TIOCM_RTS);
557 if (shf->FlowReplace & SERIAL_AUTO_RECEIVE)
558 port.c_iflag |= IXOFF;
560 port.c_iflag &= ~IXOFF;
561 if (shf->FlowReplace & SERIAL_AUTO_TRANSMIT)
562 port.c_iflag |= IXON;
564 port.c_iflag &= ~IXON;
565 if (tcsetattr(fd, TCSANOW, &port) == -1)
567 ERR("tcsetattr error '%s'\n", strerror(errno));
568 return FILE_GetNtStatus();
571 return STATUS_SUCCESS;
574 static NTSTATUS set_line_control(int fd, const SERIAL_LINE_CONTROL* slc)
577 unsigned bytesize, stopbits;
579 if (tcgetattr(fd, &port) == -1)
581 ERR("tcgetattr error '%s'\n", strerror(errno));
582 return FILE_GetNtStatus();
586 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK|IMAXBEL);
588 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK);
590 port.c_iflag |= IGNBRK | INPCK;
592 port.c_oflag &= ~(OPOST);
594 port.c_cflag &= ~(HUPCL);
595 port.c_cflag |= CLOCAL | CREAD;
598 * on FreeBSD, turning off ICANON does not disable IEXTEN,
599 * so we must turn it off explicitly. No harm done on Linux.
601 port.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN);
602 port.c_lflag |= NOFLSH;
604 bytesize = slc->WordLength;
605 stopbits = slc->StopBits;
608 port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
610 port.c_cflag &= ~(PARENB | PARODD);
613 /* make sure that reads don't block */
615 port.c_cc[VTIME] = 0;
619 case NOPARITY: port.c_iflag &= ~INPCK; break;
620 case ODDPARITY: port.c_cflag |= PARENB | PARODD; break;
621 case EVENPARITY: port.c_cflag |= PARENB; break;
623 /* Linux defines mark/space (stick) parity */
624 case MARKPARITY: port.c_cflag |= PARENB | CMSPAR; break;
625 case SPACEPARITY: port.c_cflag |= PARENB | PARODD | CMSPAR; break;
627 /* try the POSIX way */
629 if (slc->StopBits == ONESTOPBIT)
631 stopbits = TWOSTOPBITS;
632 port.c_iflag &= ~INPCK;
636 ERR("Cannot set MARK Parity\n");
637 return STATUS_NOT_SUPPORTED;
641 if (slc->WordLength < 8)
644 port.c_iflag &= ~INPCK;
648 ERR("Cannot set SPACE Parity\n");
649 return STATUS_NOT_SUPPORTED;
655 return STATUS_NOT_SUPPORTED;
658 port.c_cflag &= ~CSIZE;
661 case 5: port.c_cflag |= CS5; break;
662 case 6: port.c_cflag |= CS6; break;
663 case 7: port.c_cflag |= CS7; break;
664 case 8: port.c_cflag |= CS8; break;
667 return STATUS_NOT_SUPPORTED;
672 case ONESTOPBIT: port.c_cflag &= ~CSTOPB; break;
673 case ONE5STOPBITS: /* will be selected if bytesize is 5 */
674 case TWOSTOPBITS: port.c_cflag |= CSTOPB; break;
677 return STATUS_NOT_SUPPORTED;
679 /* otherwise it hangs with pending input*/
680 if (tcsetattr(fd, TCSANOW, &port) == -1)
682 ERR("tcsetattr error '%s'\n", strerror(errno));
683 return FILE_GetNtStatus();
685 return STATUS_SUCCESS;
688 static NTSTATUS set_queue_size(int fd, const SERIAL_QUEUE_SIZE* sqs)
690 FIXME("insize %d outsize %d unimplemented stub\n", sqs->InSize, sqs->OutSize);
691 return STATUS_SUCCESS;
694 static NTSTATUS set_special_chars(int fd, const SERIAL_CHARS* sc)
698 if (tcgetattr(fd, &port) == -1)
700 ERR("tcgetattr error '%s'\n", strerror(errno));
701 return FILE_GetNtStatus();
704 port.c_cc[VEOF ] = sc->EofChar;
705 /* FIXME: sc->ErrorChar is not supported */
706 /* FIXME: sc->BreakChar is not supported */
707 /* FIXME: sc->EventChar is not supported */
708 port.c_cc[VSTART] = sc->XonChar;
709 port.c_cc[VSTOP ] = sc->XoffChar;
711 if (tcsetattr(fd, TCSANOW, &port) == -1)
713 ERR("tcsetattr error '%s'\n", strerror(errno));
714 return FILE_GetNtStatus();
716 return STATUS_SUCCESS;
719 static NTSTATUS set_timeouts(HANDLE handle, const SERIAL_TIMEOUTS* st)
723 SERVER_START_REQ( set_serial_info )
725 req->handle = wine_server_obj_handle( handle );
726 req->flags = SERIALINFO_SET_TIMEOUTS;
727 req->readinterval = st->ReadIntervalTimeout ;
728 req->readmult = st->ReadTotalTimeoutMultiplier ;
729 req->readconst = st->ReadTotalTimeoutConstant ;
730 req->writemult = st->WriteTotalTimeoutMultiplier ;
731 req->writeconst = st->WriteTotalTimeoutConstant ;
732 status = wine_server_call( req );
738 static NTSTATUS set_wait_mask(HANDLE hDevice, DWORD mask)
742 SERVER_START_REQ( set_serial_info )
744 req->handle = wine_server_obj_handle( hDevice );
745 req->flags = SERIALINFO_SET_MASK;
746 req->eventmask = mask;
747 status = wine_server_call( req );
754 * does not change IXOFF but simulates that IXOFF has been received:
756 static NTSTATUS set_XOff(int fd)
758 if (tcflow(fd, TCOOFF))
760 return FILE_GetNtStatus();
762 return STATUS_SUCCESS;
766 * does not change IXON but simulates that IXON has been received:
768 static NTSTATUS set_XOn(int fd)
770 if (tcflow(fd, TCOON))
772 return FILE_GetNtStatus();
774 return STATUS_SUCCESS;
778 * local structure holding the irq values we need for WaitCommEvent()
780 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
781 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
782 * no need to carry them in the internal structure
785 typedef struct serial_irq_info
787 int rx , tx, frame, overrun, parity, brk, buf_overrun;
791 /***********************************************************************
792 * Data needed by the thread polling for the changing CommEvent
794 typedef struct async_commio
798 IO_STATUS_BLOCK* iosb;
802 serial_irq_info irq_info;
805 /***********************************************************************
806 * Get extended interrupt count info, needed for wait_on
808 static NTSTATUS get_irq_info(int fd, serial_irq_info *irq_info)
811 struct serial_icounter_struct einfo;
812 if (!ioctl(fd, TIOCGICOUNT, &einfo))
814 irq_info->rx = einfo.rx;
815 irq_info->tx = einfo.tx;
816 irq_info->frame = einfo.frame;
817 irq_info->overrun = einfo.overrun;
818 irq_info->parity = einfo.parity;
819 irq_info->brk = einfo.brk;
820 irq_info->buf_overrun = einfo.buf_overrun;
824 TRACE("TIOCGICOUNT err %s\n", strerror(errno));
825 memset(irq_info,0, sizeof(serial_irq_info));
826 return FILE_GetNtStatus();
829 memset(irq_info,0, sizeof(serial_irq_info));
830 return STATUS_NOT_IMPLEMENTED;
833 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
834 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
835 if (ioctl(fd, TIOCSERGETLSR, &irq_info->temt))
837 TRACE("TIOCSERGETLSR err %s\n", strerror(errno));
838 return FILE_GetNtStatus();
840 #elif defined(TIOCOUTQ) /* otherwise we log when the out queue gets empty */
841 if (ioctl(fd, TIOCOUTQ, &irq_info->temt))
843 TRACE("TIOCOUTQ err %s\n", strerror(errno));
844 return FILE_GetNtStatus();
848 if (irq_info->temt == 0)
852 return STATUS_SUCCESS;
856 static DWORD check_events(int fd, DWORD mask,
857 const serial_irq_info *new,
858 const serial_irq_info *old,
859 DWORD new_mstat, DWORD old_mstat)
861 DWORD ret = 0, queue;
863 TRACE("mask 0x%08x\n", mask);
864 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old->rx, new->rx);
865 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old->tx, new->tx);
866 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old->frame, new->frame);
867 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old->overrun, new->overrun);
868 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old->parity, new->parity);
869 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old->brk, new->brk);
870 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old->buf_overrun, new->buf_overrun);
872 if (old->brk != new->brk) ret |= EV_BREAK;
873 if ((old_mstat & MS_CTS_ON ) != (new_mstat & MS_CTS_ON )) ret |= EV_CTS;
874 if ((old_mstat & MS_DSR_ON ) != (new_mstat & MS_DSR_ON )) ret |= EV_DSR;
875 if ((old_mstat & MS_RING_ON) != (new_mstat & MS_RING_ON)) ret |= EV_RING;
876 if ((old_mstat & MS_RLSD_ON) != (new_mstat & MS_RLSD_ON)) ret |= EV_RLSD;
877 if (old->frame != new->frame || old->overrun != new->overrun || old->parity != new->parity) ret |= EV_ERR;
878 if (mask & EV_RXCHAR)
882 if (ioctl(fd, TIOCINQ, &queue))
883 WARN("TIOCINQ returned error\n");
888 if (mask & EV_TXEMPTY)
890 if (!old->temt && new->temt)
896 /***********************************************************************
897 * wait_for_event (INTERNAL)
899 * We need to poll for what is interesting
900 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
903 static DWORD CALLBACK wait_for_event(LPVOID arg)
905 async_commio *commio = arg;
908 if (!server_get_unix_fd( commio->hDevice, FILE_READ_DATA | FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL ))
910 serial_irq_info new_irq_info;
911 DWORD new_mstat, new_evtmask;
914 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
915 commio->hDevice, fd, commio->evtmask, commio->events, commio->hEvent, &commio->irq_info);
917 time.QuadPart = (ULONGLONG)10000;
918 time.QuadPart = -time.QuadPart;
922 * TIOCMIWAIT is not adequate
925 * We don't handle the EV_RXFLAG (the eventchar)
927 NtDelayExecution(FALSE, &time);
928 get_irq_info(fd, &new_irq_info);
929 if (get_modem_status(fd, &new_mstat))
930 TRACE("get_modem_status failed\n");
931 *commio->events = check_events(fd, commio->evtmask,
932 &new_irq_info, &commio->irq_info,
933 new_mstat, commio->mstat);
934 if (*commio->events) break;
935 get_wait_mask(commio->hDevice, &new_evtmask);
936 if (commio->evtmask != new_evtmask)
942 if (needs_close) close( fd );
944 if (commio->iosb) commio->iosb->u.Status = *commio->events ? STATUS_SUCCESS : STATUS_CANCELLED;
945 if (commio->hEvent) NtSetEvent(commio->hEvent, NULL);
946 RtlFreeHeap(GetProcessHeap(), 0, commio);
950 static NTSTATUS wait_on(HANDLE hDevice, int fd, HANDLE hEvent, PIO_STATUS_BLOCK piosb, DWORD* events)
952 async_commio* commio;
955 if ((status = NtResetEvent(hEvent, NULL)))
958 commio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio));
959 if (!commio) return STATUS_NO_MEMORY;
961 commio->hDevice = hDevice;
962 commio->events = events;
963 commio->iosb = piosb;
964 commio->hEvent = hEvent;
965 get_wait_mask(commio->hDevice, &commio->evtmask);
967 /* We may never return, if some capabilities miss
968 * Return error in that case
970 #if !defined(TIOCINQ)
971 if (commio->evtmask & EV_RXCHAR)
974 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
975 if (commio->evtmask & EV_TXEMPTY)
978 #if !defined(TIOCMGET)
979 if (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD))
982 #if !defined(TIOCM_CTS)
983 if (commio->evtmask & EV_CTS)
986 #if !defined(TIOCM_DSR)
987 if (commio->evtmask & EV_DSR)
990 #if !defined(TIOCM_RNG)
991 if (commio->evtmask & EV_RING)
994 #if !defined(TIOCM_CAR)
995 if (commio->evtmask & EV_RLSD)
998 if (commio->evtmask & EV_RXFLAG)
999 FIXME("EV_RXFLAG not handled\n");
1001 if ((status = get_irq_info(fd, &commio->irq_info)) &&
1002 (commio->evtmask & (EV_BREAK | EV_ERR)))
1005 if ((status = get_modem_status(fd, &commio->mstat)) &&
1006 (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD)))
1009 /* We might have received something or the TX buffer is delivered */
1010 *events = check_events(fd, commio->evtmask,
1011 &commio->irq_info, &commio->irq_info,
1012 commio->mstat, commio->mstat);
1015 status = STATUS_SUCCESS;
1019 /* create the worker for the task */
1020 status = RtlQueueWorkItem(wait_for_event, commio, 0 /* FIXME */);
1021 if (status != STATUS_SUCCESS) goto out_now;
1022 return STATUS_PENDING;
1024 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1026 FIXME("Returning error because of missing capabilities\n");
1027 status = STATUS_INVALID_PARAMETER;
1030 RtlFreeHeap(GetProcessHeap(), 0, commio);
1034 static NTSTATUS xmit_immediate(HANDLE hDevice, int fd, const char* ptr)
1036 /* FIXME: not perfect as it should bypass the in-queue */
1037 WARN("(%p,'%c') not perfect!\n", hDevice, *ptr);
1038 if (write(fd, ptr, 1) != 1)
1039 return FILE_GetNtStatus();
1040 return STATUS_SUCCESS;
1043 /******************************************************************
1044 * COMM_DeviceIoControl
1048 static inline NTSTATUS io_control(HANDLE hDevice,
1049 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1050 PVOID UserApcContext,
1051 PIO_STATUS_BLOCK piosb,
1052 ULONG dwIoControlCode,
1053 LPVOID lpInBuffer, DWORD nInBufferSize,
1054 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1056 DWORD sz = 0, access = FILE_READ_DATA;
1057 NTSTATUS status = STATUS_SUCCESS;
1058 int fd = -1, needs_close = 0;
1060 TRACE("%p %s %p %d %p %d %p\n",
1061 hDevice, iocode2str(dwIoControlCode), lpInBuffer, nInBufferSize,
1062 lpOutBuffer, nOutBufferSize, piosb);
1064 piosb->Information = 0;
1066 if (dwIoControlCode != IOCTL_SERIAL_GET_TIMEOUTS &&
1067 dwIoControlCode != IOCTL_SERIAL_SET_TIMEOUTS)
1069 enum server_fd_type type;
1070 if ((status = server_get_unix_fd( hDevice, access, &fd, &needs_close, &type, NULL )))
1072 if (type != FD_TYPE_SERIAL)
1074 if (needs_close) close( fd );
1075 status = STATUS_OBJECT_TYPE_MISMATCH;
1080 switch (dwIoControlCode)
1082 case IOCTL_SERIAL_CLR_DTR:
1084 if (whack_modem(fd, ~TIOCM_DTR, 0) == -1) status = FILE_GetNtStatus();
1086 status = STATUS_NOT_SUPPORTED;
1089 case IOCTL_SERIAL_CLR_RTS:
1091 if (whack_modem(fd, ~TIOCM_RTS, 0) == -1) status = FILE_GetNtStatus();
1093 status = STATUS_NOT_SUPPORTED;
1096 case IOCTL_SERIAL_GET_BAUD_RATE:
1097 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_BAUD_RATE))
1099 if (!(status = get_baud_rate(fd, lpOutBuffer)))
1100 sz = sizeof(SERIAL_BAUD_RATE);
1103 status = STATUS_INVALID_PARAMETER;
1105 case IOCTL_SERIAL_GET_CHARS:
1106 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_CHARS))
1108 if (!(status = get_special_chars(fd, lpOutBuffer)))
1109 sz = sizeof(SERIAL_CHARS);
1112 status = STATUS_INVALID_PARAMETER;
1114 case IOCTL_SERIAL_GET_COMMSTATUS:
1115 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_STATUS))
1117 if (!(status = get_status(fd, lpOutBuffer)))
1118 sz = sizeof(SERIAL_STATUS);
1120 else status = STATUS_INVALID_PARAMETER;
1122 case IOCTL_SERIAL_GET_HANDFLOW:
1123 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_HANDFLOW))
1125 if (!(status = get_hand_flow(fd, lpOutBuffer)))
1126 sz = sizeof(SERIAL_HANDFLOW);
1129 status = STATUS_INVALID_PARAMETER;
1131 case IOCTL_SERIAL_GET_LINE_CONTROL:
1132 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_LINE_CONTROL))
1134 if (!(status = get_line_control(fd, lpOutBuffer)))
1135 sz = sizeof(SERIAL_LINE_CONTROL);
1138 status = STATUS_INVALID_PARAMETER;
1140 case IOCTL_SERIAL_GET_MODEMSTATUS:
1141 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1143 if (!(status = get_modem_status(fd, lpOutBuffer)))
1146 else status = STATUS_INVALID_PARAMETER;
1148 case IOCTL_SERIAL_GET_TIMEOUTS:
1149 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_TIMEOUTS))
1151 if (!(status = get_timeouts(hDevice, lpOutBuffer)))
1152 sz = sizeof(SERIAL_TIMEOUTS);
1155 status = STATUS_INVALID_PARAMETER;
1157 case IOCTL_SERIAL_GET_WAIT_MASK:
1158 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1160 if (!(status = get_wait_mask(hDevice, lpOutBuffer)))
1164 status = STATUS_INVALID_PARAMETER;
1166 case IOCTL_SERIAL_IMMEDIATE_CHAR:
1167 if (lpInBuffer && nInBufferSize == sizeof(CHAR))
1168 status = xmit_immediate(hDevice, fd, lpInBuffer);
1170 status = STATUS_INVALID_PARAMETER;
1172 case IOCTL_SERIAL_PURGE:
1173 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1174 status = purge(fd, *(DWORD*)lpInBuffer);
1176 status = STATUS_INVALID_PARAMETER;
1178 case IOCTL_SERIAL_RESET_DEVICE:
1179 FIXME("Unsupported\n");
1181 case IOCTL_SERIAL_SET_BAUD_RATE:
1182 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_BAUD_RATE))
1183 status = set_baud_rate(fd, lpInBuffer);
1185 status = STATUS_INVALID_PARAMETER;
1187 case IOCTL_SERIAL_SET_BREAK_OFF:
1188 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1189 if (ioctl(fd, TIOCCBRK, 0) == -1)
1191 TRACE("ioctl failed\n");
1192 status = FILE_GetNtStatus();
1195 FIXME("ioctl not available\n");
1196 status = STATUS_NOT_SUPPORTED;
1199 case IOCTL_SERIAL_SET_BREAK_ON:
1200 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1201 if (ioctl(fd, TIOCSBRK, 0) == -1)
1203 TRACE("ioctl failed\n");
1204 status = FILE_GetNtStatus();
1207 FIXME("ioctl not available\n");
1208 status = STATUS_NOT_SUPPORTED;
1211 case IOCTL_SERIAL_SET_CHARS:
1212 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_CHARS))
1213 status = set_special_chars(fd, lpInBuffer);
1215 status = STATUS_INVALID_PARAMETER;
1217 case IOCTL_SERIAL_SET_DTR:
1219 if (whack_modem(fd, 0, TIOCM_DTR) == -1) status = FILE_GetNtStatus();
1221 status = STATUS_NOT_SUPPORTED;
1224 case IOCTL_SERIAL_SET_HANDFLOW:
1225 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_HANDFLOW))
1226 status = set_handflow(fd, lpInBuffer);
1228 status = STATUS_INVALID_PARAMETER;
1230 case IOCTL_SERIAL_SET_LINE_CONTROL:
1231 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_LINE_CONTROL))
1232 status = set_line_control(fd, lpInBuffer);
1234 status = STATUS_INVALID_PARAMETER;
1236 case IOCTL_SERIAL_SET_QUEUE_SIZE:
1237 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_QUEUE_SIZE))
1238 status = set_queue_size(fd, lpInBuffer);
1240 status = STATUS_INVALID_PARAMETER;
1242 case IOCTL_SERIAL_SET_RTS:
1244 if (whack_modem(fd, 0, TIOCM_RTS) == -1) status = FILE_GetNtStatus();
1246 status = STATUS_NOT_SUPPORTED;
1249 case IOCTL_SERIAL_SET_TIMEOUTS:
1250 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_TIMEOUTS))
1251 status = set_timeouts(hDevice, lpInBuffer);
1253 status = STATUS_INVALID_PARAMETER;
1255 case IOCTL_SERIAL_SET_WAIT_MASK:
1256 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1258 status = set_wait_mask(hDevice, *(DWORD*)lpInBuffer);
1260 else status = STATUS_INVALID_PARAMETER;
1262 case IOCTL_SERIAL_SET_XOFF:
1263 status = set_XOff(fd);
1265 case IOCTL_SERIAL_SET_XON:
1266 status = set_XOn(fd);
1268 case IOCTL_SERIAL_WAIT_ON_MASK:
1269 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1271 if (!(status = wait_on(hDevice, fd, hEvent, piosb, lpOutBuffer)))
1275 status = STATUS_INVALID_PARAMETER;
1278 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1279 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
1280 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
1282 status = STATUS_INVALID_PARAMETER;
1285 if (needs_close) close( fd );
1287 piosb->u.Status = status;
1288 piosb->Information = sz;
1289 if (hEvent && status != STATUS_PENDING) NtSetEvent(hEvent, NULL);
1293 NTSTATUS COMM_DeviceIoControl(HANDLE hDevice,
1294 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1295 PVOID UserApcContext,
1296 PIO_STATUS_BLOCK piosb,
1297 ULONG dwIoControlCode,
1298 LPVOID lpInBuffer, DWORD nInBufferSize,
1299 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1303 if (dwIoControlCode == IOCTL_SERIAL_WAIT_ON_MASK)
1305 HANDLE hev = hEvent;
1307 /* this is an ioctl we implement in a non blocking way if hEvent is not
1309 * so we have to explicitly wait if no hEvent is provided
1313 OBJECT_ATTRIBUTES attr;
1315 attr.Length = sizeof(attr);
1316 attr.RootDirectory = 0;
1317 attr.ObjectName = NULL;
1318 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1319 attr.SecurityDescriptor = NULL;
1320 attr.SecurityQualityOfService = NULL;
1321 status = NtCreateEvent(&hev, EVENT_ALL_ACCESS, &attr, SynchronizationEvent, FALSE);
1323 if (status) goto done;
1325 status = io_control(hDevice, hev, UserApcRoutine, UserApcContext,
1326 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1327 lpOutBuffer, nOutBufferSize);
1330 if (status == STATUS_PENDING)
1332 NtWaitForSingleObject(hev, FALSE, NULL);
1333 status = STATUS_SUCCESS;
1338 else status = io_control(hDevice, hEvent, UserApcRoutine, UserApcContext,
1339 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1340 lpOutBuffer, nOutBufferSize);