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"
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
43 #include <sys/types.h>
44 #ifdef HAVE_SYS_FILIO_H
45 # include <sys/filio.h>
47 #ifdef HAVE_SYS_IOCTL_H
48 #include <sys/ioctl.h>
50 #ifdef HAVE_SYS_POLL_H
51 # include <sys/poll.h>
53 #ifdef HAVE_SYS_MODEM_H
54 # include <sys/modem.h>
56 #ifdef HAVE_SYS_STRTIO_H
57 # include <sys/strtio.h>
60 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
63 #define WIN32_NO_STATUS
67 #include "ddk/ntddser.h"
68 #include "ntdll_misc.h"
69 #include "wine/server.h"
70 #include "wine/library.h"
71 #include "wine/debug.h"
73 #ifdef HAVE_LINUX_SERIAL_H
74 #ifdef HAVE_ASM_TYPES_H
75 #include <asm/types.h>
77 #include <linux/serial.h>
80 #if !defined(TIOCINQ) && defined(FIONREAD)
81 #define TIOCINQ FIONREAD
84 WINE_DEFAULT_DEBUG_CHANNEL(comm
);
86 static const char* iocode2str(DWORD ioc
)
90 #define X(x) case (x): return #x
91 X(IOCTL_SERIAL_CLEAR_STATS
);
92 X(IOCTL_SERIAL_CLR_DTR
);
93 X(IOCTL_SERIAL_CLR_RTS
);
94 X(IOCTL_SERIAL_CONFIG_SIZE
);
95 X(IOCTL_SERIAL_GET_BAUD_RATE
);
96 X(IOCTL_SERIAL_GET_CHARS
);
97 X(IOCTL_SERIAL_GET_COMMSTATUS
);
98 X(IOCTL_SERIAL_GET_DTRRTS
);
99 X(IOCTL_SERIAL_GET_HANDFLOW
);
100 X(IOCTL_SERIAL_GET_LINE_CONTROL
);
101 X(IOCTL_SERIAL_GET_MODEM_CONTROL
);
102 X(IOCTL_SERIAL_GET_MODEMSTATUS
);
103 X(IOCTL_SERIAL_GET_PROPERTIES
);
104 X(IOCTL_SERIAL_GET_STATS
);
105 X(IOCTL_SERIAL_GET_TIMEOUTS
);
106 X(IOCTL_SERIAL_GET_WAIT_MASK
);
107 X(IOCTL_SERIAL_IMMEDIATE_CHAR
);
108 X(IOCTL_SERIAL_LSRMST_INSERT
);
109 X(IOCTL_SERIAL_PURGE
);
110 X(IOCTL_SERIAL_RESET_DEVICE
);
111 X(IOCTL_SERIAL_SET_BAUD_RATE
);
112 X(IOCTL_SERIAL_SET_BREAK_ON
);
113 X(IOCTL_SERIAL_SET_BREAK_OFF
);
114 X(IOCTL_SERIAL_SET_CHARS
);
115 X(IOCTL_SERIAL_SET_DTR
);
116 X(IOCTL_SERIAL_SET_FIFO_CONTROL
);
117 X(IOCTL_SERIAL_SET_HANDFLOW
);
118 X(IOCTL_SERIAL_SET_LINE_CONTROL
);
119 X(IOCTL_SERIAL_SET_MODEM_CONTROL
);
120 X(IOCTL_SERIAL_SET_QUEUE_SIZE
);
121 X(IOCTL_SERIAL_SET_RTS
);
122 X(IOCTL_SERIAL_SET_TIMEOUTS
);
123 X(IOCTL_SERIAL_SET_WAIT_MASK
);
124 X(IOCTL_SERIAL_SET_XOFF
);
125 X(IOCTL_SERIAL_SET_XON
);
126 X(IOCTL_SERIAL_WAIT_ON_MASK
);
127 X(IOCTL_SERIAL_XOFF_COUNTER
);
129 default: { static char tmp
[32]; sprintf(tmp
, "IOCTL_SERIAL_%d\n", ioc
); return tmp
; }
133 static NTSTATUS
get_baud_rate(int fd
, SERIAL_BAUD_RATE
* sbr
)
138 if (tcgetattr(fd
, &port
) == -1)
140 ERR("tcgetattr error '%s'\n", strerror(errno
));
141 return FILE_GetNtStatus();
143 speed
= cfgetospeed(&port
);
146 case B0
: sbr
->BaudRate
= 0; break;
147 case B50
: sbr
->BaudRate
= 50; break;
148 case B75
: sbr
->BaudRate
= 75; break;
149 case B110
: sbr
->BaudRate
= 110; break;
150 case B134
: sbr
->BaudRate
= 134; break;
151 case B150
: sbr
->BaudRate
= 150; break;
152 case B200
: sbr
->BaudRate
= 200; break;
153 case B300
: sbr
->BaudRate
= 300; break;
154 case B600
: sbr
->BaudRate
= 600; break;
155 case B1200
: sbr
->BaudRate
= 1200; break;
156 case B1800
: sbr
->BaudRate
= 1800; break;
157 case B2400
: sbr
->BaudRate
= 2400; break;
158 case B4800
: sbr
->BaudRate
= 4800; break;
159 case B9600
: sbr
->BaudRate
= 9600; break;
160 case B19200
: sbr
->BaudRate
= 19200; break;
161 case B38400
: sbr
->BaudRate
= 38400; break;
163 case B57600
: sbr
->BaudRate
= 57600; break;
166 case B115200
: sbr
->BaudRate
= 115200; break;
169 case B230400
: sbr
->BaudRate
= 230400; break;
172 case B460800
: sbr
->BaudRate
= 460800; break;
175 ERR("unknown speed %x\n", speed
);
176 return STATUS_INVALID_PARAMETER
;
178 return STATUS_SUCCESS
;
181 static NTSTATUS
get_hand_flow(int fd
, SERIAL_HANDFLOW
* shf
)
186 if (tcgetattr(fd
, &port
) == -1)
188 ERR("tcgetattr error '%s'\n", strerror(errno
));
189 return FILE_GetNtStatus();
191 /* termios does not support DTR/DSR flow control */
192 shf
->ControlHandShake
= 0;
193 shf
->FlowReplace
= 0;
195 if (ioctl(fd
, TIOCMGET
, &stat
) == -1)
197 WARN("ioctl error '%s'\n", strerror(errno
));
198 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
199 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
202 WARN("Setting DTR/RTS to enabled by default\n");
203 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
204 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
207 if (stat
& TIOCM_DTR
)
209 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
211 if (port
.c_cflag
& CRTSCTS
)
213 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
214 shf
->ControlHandShake
|= SERIAL_CTS_HANDSHAKE
;
220 if (stat
& TIOCM_RTS
)
222 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
224 if (port
.c_iflag
& IXOFF
)
225 shf
->FlowReplace
|= SERIAL_AUTO_RECEIVE
;
226 if (port
.c_iflag
& IXON
)
227 shf
->FlowReplace
|= SERIAL_AUTO_TRANSMIT
;
231 return STATUS_SUCCESS
;
234 static NTSTATUS
get_line_control(int fd
, SERIAL_LINE_CONTROL
* slc
)
238 if (tcgetattr(fd
, &port
) == -1)
240 ERR("tcgetattr error '%s'\n", strerror(errno
));
241 return FILE_GetNtStatus();
245 switch (port
.c_cflag
& (PARENB
| PARODD
| CMSPAR
))
247 switch (port
.c_cflag
& (PARENB
| PARODD
))
250 case 0: slc
->Parity
= NOPARITY
; break;
251 case PARENB
: slc
->Parity
= EVENPARITY
; break;
252 case PARENB
|PARODD
: slc
->Parity
= ODDPARITY
; break;
254 case PARENB
|CMSPAR
: slc
->Parity
= MARKPARITY
; break;
255 case PARENB
|PARODD
|CMSPAR
: slc
->Parity
= SPACEPARITY
; break;
258 switch (port
.c_cflag
& CSIZE
)
260 case CS5
: slc
->WordLength
= 5; break;
261 case CS6
: slc
->WordLength
= 6; break;
262 case CS7
: slc
->WordLength
= 7; break;
263 case CS8
: slc
->WordLength
= 8; break;
264 default: ERR("unknown size %x\n", (UINT
)(port
.c_cflag
& CSIZE
));
267 if (port
.c_cflag
& CSTOPB
)
269 if (slc
->WordLength
== 5)
270 slc
->StopBits
= ONE5STOPBITS
;
272 slc
->StopBits
= TWOSTOPBITS
;
275 slc
->StopBits
= ONESTOPBIT
;
277 return STATUS_SUCCESS
;
280 static NTSTATUS
get_modem_status(int fd
, DWORD
* lpModemStat
)
282 NTSTATUS status
= STATUS_NOT_SUPPORTED
;
287 if (!ioctl(fd
, TIOCMGET
, &mstat
))
290 if (mstat
& TIOCM_CTS
) *lpModemStat
|= MS_CTS_ON
;
293 if (mstat
& TIOCM_DSR
) *lpModemStat
|= MS_DSR_ON
;
296 if (mstat
& TIOCM_RNG
) *lpModemStat
|= MS_RING_ON
;
299 /* FIXME: Not really sure about RLSD UB 990810 */
300 if (mstat
& TIOCM_CAR
) *lpModemStat
|= MS_RLSD_ON
;
302 TRACE("%04x -> %s%s%s%s\n", mstat
,
303 (*lpModemStat
& MS_RLSD_ON
) ? "MS_RLSD_ON " : "",
304 (*lpModemStat
& MS_RING_ON
) ? "MS_RING_ON " : "",
305 (*lpModemStat
& MS_DSR_ON
) ? "MS_DSR_ON " : "",
306 (*lpModemStat
& MS_CTS_ON
) ? "MS_CTS_ON " : "");
307 return STATUS_SUCCESS
;
309 WARN("TIOCMGET err %s\n", strerror(errno
));
310 status
= FILE_GetNtStatus();
315 static NTSTATUS
get_special_chars(int fd
, SERIAL_CHARS
* sc
)
319 if (tcgetattr(fd
, &port
) == -1)
321 ERR("tcgetattr error '%s'\n", strerror(errno
));
322 return FILE_GetNtStatus();
324 sc
->EofChar
= port
.c_cc
[VEOF
];
325 sc
->ErrorChar
= 0xFF;
326 sc
->BreakChar
= 0; /* FIXME */
327 sc
->EventChar
= 0; /* FIXME */
328 sc
->XonChar
= port
.c_cc
[VSTART
];
329 sc
->XoffChar
= port
.c_cc
[VSTOP
];
331 return STATUS_SUCCESS
;
334 static NTSTATUS
get_status(int fd
, SERIAL_STATUS
* ss
)
336 NTSTATUS status
= STATUS_SUCCESS
;
340 ss
->EofReceived
= FALSE
;
341 ss
->WaitForImmediate
= FALSE
;
343 if (ioctl(fd
, TIOCOUTQ
, &ss
->AmountInOutQueue
) == -1)
345 WARN("ioctl returned error\n");
346 status
= FILE_GetNtStatus();
349 ss
->AmountInOutQueue
= 0; /* FIXME: find a different way to find out */
353 if (ioctl(fd
, TIOCINQ
, &ss
->AmountInInQueue
))
355 WARN("ioctl returned error\n");
356 status
= FILE_GetNtStatus();
359 ss
->AmountInInQueue
= 0; /* FIXME: find a different way to find out */
364 static NTSTATUS
get_timeouts(HANDLE handle
, SERIAL_TIMEOUTS
* st
)
367 SERVER_START_REQ( get_serial_info
)
369 req
->handle
= wine_server_obj_handle( handle
);
371 if (!(status
= wine_server_call( req
)))
373 st
->ReadIntervalTimeout
= reply
->readinterval
;
374 st
->ReadTotalTimeoutMultiplier
= reply
->readmult
;
375 st
->ReadTotalTimeoutConstant
= reply
->readconst
;
376 st
->WriteTotalTimeoutMultiplier
= reply
->writemult
;
377 st
->WriteTotalTimeoutConstant
= reply
->writeconst
;
384 static void stop_waiting( HANDLE handle
)
388 SERVER_START_REQ( set_serial_info
)
390 req
->handle
= wine_server_obj_handle( handle
);
391 req
->flags
= SERIALINFO_PENDING_WAIT
;
392 if ((status
= wine_server_call( req
)))
393 ERR("failed to clear waiting state: %#x\n", status
);
398 static NTSTATUS
get_wait_mask(HANDLE hDevice
, DWORD
*mask
, DWORD
*cookie
, DWORD
*pending_write
, BOOL start_wait
)
402 SERVER_START_REQ( get_serial_info
)
404 req
->handle
= wine_server_obj_handle( hDevice
);
405 req
->flags
= pending_write
? SERIALINFO_PENDING_WRITE
: 0;
406 if (start_wait
) req
->flags
|= SERIALINFO_PENDING_WAIT
;
407 if (!(status
= wine_server_call( req
)))
409 *mask
= reply
->eventmask
;
410 if (cookie
) *cookie
= reply
->cookie
;
411 if (pending_write
) *pending_write
= reply
->pending_write
;
418 static NTSTATUS
purge(int fd
, DWORD flags
)
421 ** not exactly sure how these are different
422 ** Perhaps if we had our own internal queues, one flushes them
423 ** and the other flushes the kernel's buffers.
425 if (flags
& PURGE_TXABORT
) tcflush(fd
, TCOFLUSH
);
426 if (flags
& PURGE_RXABORT
) tcflush(fd
, TCIFLUSH
);
427 if (flags
& PURGE_TXCLEAR
) tcflush(fd
, TCOFLUSH
);
428 if (flags
& PURGE_RXCLEAR
) tcflush(fd
, TCIFLUSH
);
429 return STATUS_SUCCESS
;
432 static NTSTATUS
set_baud_rate(int fd
, const SERIAL_BAUD_RATE
* sbr
)
436 if (tcgetattr(fd
, &port
) == -1)
438 ERR("tcgetattr error '%s'\n", strerror(errno
));
439 return FILE_GetNtStatus();
442 switch (sbr
->BaudRate
)
444 case 0: cfsetospeed( &port
, B0
); break;
445 case 50: cfsetospeed( &port
, B50
); break;
446 case 75: cfsetospeed( &port
, B75
); break;
448 case CBR_110
: cfsetospeed( &port
, B110
); break;
449 case 134: cfsetospeed( &port
, B134
); break;
450 case 150: cfsetospeed( &port
, B150
); break;
451 case 200: cfsetospeed( &port
, B200
); break;
453 case CBR_300
: cfsetospeed( &port
, B300
); break;
455 case CBR_600
: cfsetospeed( &port
, B600
); break;
457 case CBR_1200
: cfsetospeed( &port
, B1200
); break;
458 case 1800: cfsetospeed( &port
, B1800
); break;
460 case CBR_2400
: cfsetospeed( &port
, B2400
); break;
462 case CBR_4800
: cfsetospeed( &port
, B4800
); break;
464 case CBR_9600
: cfsetospeed( &port
, B9600
); break;
466 case CBR_19200
: cfsetospeed( &port
, B19200
); break;
468 case CBR_38400
: cfsetospeed( &port
, B38400
); break;
470 case 57600: cfsetospeed( &port
, B57600
); break;
473 case 115200: cfsetospeed( &port
, B115200
); break;
476 case 230400: cfsetospeed( &port
, B230400
); break;
479 case 460800: cfsetospeed( &port
, B460800
); break;
482 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
484 struct serial_struct nuts
;
487 ioctl(fd
, TIOCGSERIAL
, &nuts
);
488 nuts
.custom_divisor
= nuts
.baud_base
/ sbr
->BaudRate
;
489 if (!(nuts
.custom_divisor
)) nuts
.custom_divisor
= 1;
490 arby
= nuts
.baud_base
/ nuts
.custom_divisor
;
491 nuts
.flags
&= ~ASYNC_SPD_MASK
;
492 nuts
.flags
|= ASYNC_SPD_CUST
;
493 WARN("You (or a program acting at your behest) have specified\n"
494 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
495 "which is as close as we can get by our present understanding of your\n"
496 "hardware. I hope you know what you are doing. Any disruption Wine\n"
497 "has caused to your linux system can be undone with setserial\n"
498 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
499 "reset it and it will probably recover.\n", sbr
->BaudRate
, arby
);
500 ioctl(fd
, TIOCSSERIAL
, &nuts
);
501 cfsetospeed( &port
, B38400
);
504 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
505 ERR("baudrate %d\n", sbr
->BaudRate
);
506 return STATUS_NOT_SUPPORTED
;
507 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
509 cfsetispeed( &port
, cfgetospeed(&port
) );
510 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
512 ERR("tcsetattr error '%s'\n", strerror(errno
));
513 return FILE_GetNtStatus();
515 return STATUS_SUCCESS
;
518 static int whack_modem(int fd
, unsigned int andy
, unsigned int orrie
)
521 unsigned int mstat
, okay
;
522 okay
= ioctl(fd
, TIOCMGET
, &mstat
);
523 if (okay
) return okay
;
524 if (andy
) mstat
&= andy
;
526 return ioctl(fd
, TIOCMSET
, &mstat
);
532 static NTSTATUS
set_handflow(int fd
, const SERIAL_HANDFLOW
* shf
)
536 if ((shf
->FlowReplace
& (SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
)) ==
537 (SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
))
538 return STATUS_NOT_SUPPORTED
;
540 if (tcgetattr(fd
, &port
) == -1)
542 ERR("tcgetattr error '%s'\n", strerror(errno
));
543 return FILE_GetNtStatus();
547 if ((shf
->ControlHandShake
& SERIAL_CTS_HANDSHAKE
) ||
548 (shf
->FlowReplace
& SERIAL_RTS_HANDSHAKE
))
550 port
.c_cflag
|= CRTSCTS
;
554 port
.c_cflag
&= ~CRTSCTS
;
557 if (shf
->ControlHandShake
& SERIAL_DTR_HANDSHAKE
)
559 WARN("DSR/DTR flow control not supported\n");
560 } else if (!(shf
->ControlHandShake
& SERIAL_DTR_CONTROL
))
561 whack_modem(fd
, ~TIOCM_DTR
, 0);
563 whack_modem(fd
, 0, TIOCM_DTR
);
566 if (!(shf
->ControlHandShake
& SERIAL_CTS_HANDSHAKE
))
568 if ((shf
->FlowReplace
& (SERIAL_RTS_CONTROL
|SERIAL_RTS_HANDSHAKE
)) == 0)
569 whack_modem(fd
, ~TIOCM_RTS
, 0);
571 whack_modem(fd
, 0, TIOCM_RTS
);
575 if (shf
->FlowReplace
& SERIAL_AUTO_RECEIVE
)
576 port
.c_iflag
|= IXOFF
;
578 port
.c_iflag
&= ~IXOFF
;
579 if (shf
->FlowReplace
& SERIAL_AUTO_TRANSMIT
)
580 port
.c_iflag
|= IXON
;
582 port
.c_iflag
&= ~IXON
;
583 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
585 ERR("tcsetattr error '%s'\n", strerror(errno
));
586 return FILE_GetNtStatus();
589 return STATUS_SUCCESS
;
592 static NTSTATUS
set_line_control(int fd
, const SERIAL_LINE_CONTROL
* slc
)
595 unsigned bytesize
, stopbits
;
597 if (tcgetattr(fd
, &port
) == -1)
599 ERR("tcgetattr error '%s'\n", strerror(errno
));
600 return FILE_GetNtStatus();
604 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|PARMRK
|IMAXBEL
);
606 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|PARMRK
);
608 port
.c_iflag
|= IGNBRK
| INPCK
;
609 port
.c_oflag
&= ~(OPOST
);
610 port
.c_cflag
&= ~(HUPCL
);
611 port
.c_cflag
|= CLOCAL
| CREAD
;
614 * on FreeBSD, turning off ICANON does not disable IEXTEN,
615 * so we must turn it off explicitly. No harm done on Linux.
617 port
.c_lflag
&= ~(ICANON
|ECHO
|ISIG
|IEXTEN
);
618 port
.c_lflag
|= NOFLSH
;
620 bytesize
= slc
->WordLength
;
621 stopbits
= slc
->StopBits
;
624 port
.c_cflag
&= ~(PARENB
| PARODD
| CMSPAR
);
626 port
.c_cflag
&= ~(PARENB
| PARODD
);
629 /* make sure that reads don't block */
631 port
.c_cc
[VTIME
] = 0;
635 case NOPARITY
: port
.c_iflag
&= ~INPCK
; break;
636 case ODDPARITY
: port
.c_cflag
|= PARENB
| PARODD
; break;
637 case EVENPARITY
: port
.c_cflag
|= PARENB
; break;
639 /* Linux defines mark/space (stick) parity */
640 case MARKPARITY
: port
.c_cflag
|= PARENB
| CMSPAR
; break;
641 case SPACEPARITY
: port
.c_cflag
|= PARENB
| PARODD
| CMSPAR
; break;
643 /* try the POSIX way */
645 if (slc
->StopBits
== ONESTOPBIT
)
647 stopbits
= TWOSTOPBITS
;
648 port
.c_iflag
&= ~INPCK
;
652 FIXME("Cannot set MARK Parity\n");
653 return STATUS_NOT_SUPPORTED
;
657 if (slc
->WordLength
< 8)
660 port
.c_iflag
&= ~INPCK
;
664 FIXME("Cannot set SPACE Parity\n");
665 return STATUS_NOT_SUPPORTED
;
670 FIXME("Parity %d is not supported\n", slc
->Parity
);
671 return STATUS_NOT_SUPPORTED
;
674 port
.c_cflag
&= ~CSIZE
;
677 case 5: port
.c_cflag
|= CS5
; break;
678 case 6: port
.c_cflag
|= CS6
; break;
679 case 7: port
.c_cflag
|= CS7
; break;
680 case 8: port
.c_cflag
|= CS8
; break;
682 FIXME("ByteSize %d is not supported\n", bytesize
);
683 return STATUS_NOT_SUPPORTED
;
688 case ONESTOPBIT
: port
.c_cflag
&= ~CSTOPB
; break;
689 case ONE5STOPBITS
: /* will be selected if bytesize is 5 */
690 case TWOSTOPBITS
: port
.c_cflag
|= CSTOPB
; break;
692 FIXME("StopBits %d is not supported\n", stopbits
);
693 return STATUS_NOT_SUPPORTED
;
695 /* otherwise it hangs with pending input*/
696 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
698 ERR("tcsetattr error '%s'\n", strerror(errno
));
699 return FILE_GetNtStatus();
701 return STATUS_SUCCESS
;
704 static NTSTATUS
set_queue_size(int fd
, const SERIAL_QUEUE_SIZE
* sqs
)
706 FIXME("insize %d outsize %d unimplemented stub\n", sqs
->InSize
, sqs
->OutSize
);
707 return STATUS_SUCCESS
;
710 static NTSTATUS
set_special_chars(int fd
, const SERIAL_CHARS
* sc
)
714 if (tcgetattr(fd
, &port
) == -1)
716 ERR("tcgetattr error '%s'\n", strerror(errno
));
717 return FILE_GetNtStatus();
720 port
.c_cc
[VEOF
] = sc
->EofChar
;
721 /* FIXME: sc->ErrorChar is not supported */
722 /* FIXME: sc->BreakChar is not supported */
723 /* FIXME: sc->EventChar is not supported */
724 port
.c_cc
[VSTART
] = sc
->XonChar
;
725 port
.c_cc
[VSTOP
] = sc
->XoffChar
;
727 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
729 ERR("tcsetattr error '%s'\n", strerror(errno
));
730 return FILE_GetNtStatus();
732 return STATUS_SUCCESS
;
735 static NTSTATUS
set_timeouts(HANDLE handle
, const SERIAL_TIMEOUTS
* st
)
739 SERVER_START_REQ( set_serial_info
)
741 req
->handle
= wine_server_obj_handle( handle
);
742 req
->flags
= SERIALINFO_SET_TIMEOUTS
;
743 req
->readinterval
= st
->ReadIntervalTimeout
;
744 req
->readmult
= st
->ReadTotalTimeoutMultiplier
;
745 req
->readconst
= st
->ReadTotalTimeoutConstant
;
746 req
->writemult
= st
->WriteTotalTimeoutMultiplier
;
747 req
->writeconst
= st
->WriteTotalTimeoutConstant
;
748 status
= wine_server_call( req
);
754 static NTSTATUS
set_wait_mask(HANDLE hDevice
, DWORD mask
)
758 SERVER_START_REQ( set_serial_info
)
760 req
->handle
= wine_server_obj_handle( hDevice
);
761 req
->flags
= SERIALINFO_SET_MASK
;
762 req
->eventmask
= mask
;
763 status
= wine_server_call( req
);
770 * does not change IXOFF but simulates that IXOFF has been received:
772 static NTSTATUS
set_XOff(int fd
)
774 if (tcflow(fd
, TCOOFF
))
776 return FILE_GetNtStatus();
778 return STATUS_SUCCESS
;
782 * does not change IXON but simulates that IXON has been received:
784 static NTSTATUS
set_XOn(int fd
)
786 if (tcflow(fd
, TCOON
))
788 return FILE_GetNtStatus();
790 return STATUS_SUCCESS
;
794 * local structure holding the irq values we need for WaitCommEvent()
796 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
797 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
798 * no need to carry them in the internal structure
801 typedef struct serial_irq_info
803 int rx
, tx
, frame
, overrun
, parity
, brk
, buf_overrun
, temt
;
806 /***********************************************************************
807 * Data needed by the thread polling for the changing CommEvent
809 typedef struct async_commio
813 IO_STATUS_BLOCK
* iosb
;
819 serial_irq_info irq_info
;
822 /***********************************************************************
823 * Get extended interrupt count info, needed for wait_on
825 static NTSTATUS
get_irq_info(int fd
, serial_irq_info
*irq_info
)
829 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCGICOUNT)
830 struct serial_icounter_struct einfo
;
831 if (!ioctl(fd
, TIOCGICOUNT
, &einfo
))
833 irq_info
->rx
= einfo
.rx
;
834 irq_info
->tx
= einfo
.tx
;
835 irq_info
->frame
= einfo
.frame
;
836 irq_info
->overrun
= einfo
.overrun
;
837 irq_info
->parity
= einfo
.parity
;
838 irq_info
->brk
= einfo
.brk
;
839 irq_info
->buf_overrun
= einfo
.buf_overrun
;
843 TRACE("TIOCGICOUNT err %s\n", strerror(errno
));
844 memset(irq_info
,0, sizeof(serial_irq_info
));
847 memset(irq_info
,0, sizeof(serial_irq_info
));
851 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
852 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
853 if (!ioctl(fd
, TIOCSERGETLSR
, &out
))
855 irq_info
->temt
= (out
& TIOCSER_TEMT
) != 0;
856 return STATUS_SUCCESS
;
859 TRACE("TIOCSERGETLSR err %s\n", strerror(errno
));
861 #ifdef TIOCOUTQ /* otherwise we log when the out queue gets empty */
862 if (!ioctl(fd
, TIOCOUTQ
, &out
))
864 irq_info
->temt
= out
== 0;
865 return STATUS_SUCCESS
;
867 TRACE("TIOCOUTQ err %s\n", strerror(errno
));
868 return FILE_GetNtStatus();
870 return STATUS_SUCCESS
;
874 static DWORD
check_events(int fd
, DWORD mask
,
875 const serial_irq_info
*new,
876 const serial_irq_info
*old
,
877 DWORD new_mstat
, DWORD old_mstat
, DWORD pending_write
)
879 DWORD ret
= 0, queue
;
881 TRACE("mask 0x%08x\n", mask
);
882 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old
->rx
, new->rx
);
883 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old
->tx
, new->tx
);
884 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old
->frame
, new->frame
);
885 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old
->overrun
, new->overrun
);
886 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old
->parity
, new->parity
);
887 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old
->brk
, new->brk
);
888 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old
->buf_overrun
, new->buf_overrun
);
889 TRACE("old->temt 0x%08x vs. new->temt 0x%08x\n", old
->temt
, new->temt
);
891 if (old
->brk
!= new->brk
) ret
|= EV_BREAK
;
892 if ((old_mstat
& MS_CTS_ON
) != (new_mstat
& MS_CTS_ON
)) ret
|= EV_CTS
;
893 if ((old_mstat
& MS_DSR_ON
) != (new_mstat
& MS_DSR_ON
)) ret
|= EV_DSR
;
894 if ((old_mstat
& MS_RING_ON
) != (new_mstat
& MS_RING_ON
)) ret
|= EV_RING
;
895 if ((old_mstat
& MS_RLSD_ON
) != (new_mstat
& MS_RLSD_ON
)) ret
|= EV_RLSD
;
896 if (old
->frame
!= new->frame
|| old
->overrun
!= new->overrun
|| old
->parity
!= new->parity
) ret
|= EV_ERR
;
897 if (mask
& EV_RXCHAR
)
901 if (ioctl(fd
, TIOCINQ
, &queue
))
902 WARN("TIOCINQ returned error\n");
907 if (mask
& EV_TXEMPTY
)
909 if ((!old
->temt
|| pending_write
) && new->temt
)
915 /***********************************************************************
916 * wait_for_event (INTERNAL)
918 * We need to poll for what is interesting
919 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
922 static DWORD CALLBACK
wait_for_event(LPVOID arg
)
924 async_commio
*commio
= arg
;
927 if (!server_get_unix_fd( commio
->hDevice
, FILE_READ_DATA
| FILE_WRITE_DATA
, &fd
, &needs_close
, NULL
, NULL
))
929 serial_irq_info new_irq_info
;
930 DWORD new_mstat
, dummy
, cookie
;
933 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
934 commio
->hDevice
, fd
, commio
->evtmask
, commio
->events
, commio
->hEvent
, &commio
->irq_info
);
936 time
.QuadPart
= (ULONGLONG
)10000;
937 time
.QuadPart
= -time
.QuadPart
;
941 * TIOCMIWAIT is not adequate
944 * We don't handle the EV_RXFLAG (the eventchar)
946 NtDelayExecution(FALSE
, &time
);
947 get_irq_info(fd
, &new_irq_info
);
948 if (get_modem_status(fd
, &new_mstat
))
950 TRACE("get_modem_status failed\n");
954 *commio
->events
= check_events(fd
, commio
->evtmask
,
955 &new_irq_info
, &commio
->irq_info
,
956 new_mstat
, commio
->mstat
, commio
->pending_write
);
957 if (*commio
->events
) break;
958 get_wait_mask(commio
->hDevice
, &dummy
, &cookie
, (commio
->evtmask
& EV_TXEMPTY
) ? &commio
->pending_write
: NULL
, FALSE
);
959 if (commio
->cookie
!= cookie
)
965 if (needs_close
) close( fd
);
971 commio
->iosb
->u
.Status
= STATUS_SUCCESS
;
972 commio
->iosb
->Information
= sizeof(DWORD
);
975 commio
->iosb
->u
.Status
= STATUS_CANCELLED
;
977 stop_waiting(commio
->hDevice
);
978 if (commio
->hEvent
) NtSetEvent(commio
->hEvent
, NULL
);
979 RtlFreeHeap(GetProcessHeap(), 0, commio
);
983 static NTSTATUS
wait_on(HANDLE hDevice
, int fd
, HANDLE hEvent
, PIO_STATUS_BLOCK piosb
, DWORD
* events
)
985 async_commio
* commio
;
988 if ((status
= NtResetEvent(hEvent
, NULL
)))
991 commio
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio
));
992 if (!commio
) return STATUS_NO_MEMORY
;
994 commio
->hDevice
= hDevice
;
995 commio
->events
= events
;
996 commio
->iosb
= piosb
;
997 commio
->hEvent
= hEvent
;
998 commio
->pending_write
= 0;
999 status
= get_wait_mask(commio
->hDevice
, &commio
->evtmask
, &commio
->cookie
, (commio
->evtmask
& EV_TXEMPTY
) ? &commio
->pending_write
: NULL
, TRUE
);
1002 RtlFreeHeap(GetProcessHeap(), 0, commio
);
1006 /* We may never return, if some capabilities miss
1007 * Return error in that case
1009 #if !defined(TIOCINQ)
1010 if (commio
->evtmask
& EV_RXCHAR
)
1013 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1014 if (commio
->evtmask
& EV_TXEMPTY
)
1017 #if !defined(TIOCMGET)
1018 if (commio
->evtmask
& (EV_CTS
| EV_DSR
| EV_RING
| EV_RLSD
))
1021 #if !defined(TIOCM_CTS)
1022 if (commio
->evtmask
& EV_CTS
)
1025 #if !defined(TIOCM_DSR)
1026 if (commio
->evtmask
& EV_DSR
)
1029 #if !defined(TIOCM_RNG)
1030 if (commio
->evtmask
& EV_RING
)
1033 #if !defined(TIOCM_CAR)
1034 if (commio
->evtmask
& EV_RLSD
)
1037 if (commio
->evtmask
& EV_RXFLAG
)
1038 FIXME("EV_RXFLAG not handled\n");
1040 if ((status
= get_irq_info(fd
, &commio
->irq_info
)) &&
1041 (commio
->evtmask
& (EV_BREAK
| EV_ERR
)))
1044 if ((status
= get_modem_status(fd
, &commio
->mstat
)) &&
1045 (commio
->evtmask
& (EV_CTS
| EV_DSR
| EV_RING
| EV_RLSD
)))
1048 /* We might have received something or the TX buffer is delivered */
1049 *events
= check_events(fd
, commio
->evtmask
,
1050 &commio
->irq_info
, &commio
->irq_info
,
1051 commio
->mstat
, commio
->mstat
, commio
->pending_write
);
1054 status
= STATUS_SUCCESS
;
1058 /* create the worker for the task */
1059 status
= RtlQueueWorkItem(wait_for_event
, commio
, 0 /* FIXME */);
1060 if (status
!= STATUS_SUCCESS
) goto out_now
;
1061 return STATUS_PENDING
;
1063 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1065 FIXME("Returning error because of missing capabilities\n");
1066 status
= STATUS_INVALID_PARAMETER
;
1069 stop_waiting(commio
->hDevice
);
1070 RtlFreeHeap(GetProcessHeap(), 0, commio
);
1074 static NTSTATUS
xmit_immediate(HANDLE hDevice
, int fd
, const char* ptr
)
1076 /* FIXME: not perfect as it should bypass the in-queue */
1077 WARN("(%p,'%c') not perfect!\n", hDevice
, *ptr
);
1078 if (write(fd
, ptr
, 1) != 1)
1079 return FILE_GetNtStatus();
1080 return STATUS_SUCCESS
;
1083 /******************************************************************
1084 * COMM_DeviceIoControl
1088 static inline NTSTATUS
io_control(HANDLE hDevice
,
1089 HANDLE hEvent
, PIO_APC_ROUTINE UserApcRoutine
,
1090 PVOID UserApcContext
,
1091 PIO_STATUS_BLOCK piosb
,
1092 ULONG dwIoControlCode
,
1093 LPVOID lpInBuffer
, DWORD nInBufferSize
,
1094 LPVOID lpOutBuffer
, DWORD nOutBufferSize
)
1096 DWORD sz
= 0, access
= FILE_READ_DATA
;
1097 NTSTATUS status
= STATUS_SUCCESS
;
1098 int fd
= -1, needs_close
= 0;
1100 TRACE("%p %s %p %d %p %d %p\n",
1101 hDevice
, iocode2str(dwIoControlCode
), lpInBuffer
, nInBufferSize
,
1102 lpOutBuffer
, nOutBufferSize
, piosb
);
1104 piosb
->Information
= 0;
1106 if (dwIoControlCode
!= IOCTL_SERIAL_GET_TIMEOUTS
&&
1107 dwIoControlCode
!= IOCTL_SERIAL_SET_TIMEOUTS
)
1109 enum server_fd_type type
;
1110 if ((status
= server_get_unix_fd( hDevice
, access
, &fd
, &needs_close
, &type
, NULL
)))
1112 if (type
!= FD_TYPE_SERIAL
)
1114 if (needs_close
) close( fd
);
1115 status
= STATUS_OBJECT_TYPE_MISMATCH
;
1120 switch (dwIoControlCode
)
1122 case IOCTL_SERIAL_CLR_DTR
:
1124 if (whack_modem(fd
, ~TIOCM_DTR
, 0) == -1) status
= FILE_GetNtStatus();
1126 status
= STATUS_NOT_SUPPORTED
;
1129 case IOCTL_SERIAL_CLR_RTS
:
1131 if (whack_modem(fd
, ~TIOCM_RTS
, 0) == -1) status
= FILE_GetNtStatus();
1133 status
= STATUS_NOT_SUPPORTED
;
1136 case IOCTL_SERIAL_GET_BAUD_RATE
:
1137 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_BAUD_RATE
))
1139 if (!(status
= get_baud_rate(fd
, lpOutBuffer
)))
1140 sz
= sizeof(SERIAL_BAUD_RATE
);
1143 status
= STATUS_INVALID_PARAMETER
;
1145 case IOCTL_SERIAL_GET_CHARS
:
1146 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_CHARS
))
1148 if (!(status
= get_special_chars(fd
, lpOutBuffer
)))
1149 sz
= sizeof(SERIAL_CHARS
);
1152 status
= STATUS_INVALID_PARAMETER
;
1154 case IOCTL_SERIAL_GET_COMMSTATUS
:
1155 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_STATUS
))
1157 if (!(status
= get_status(fd
, lpOutBuffer
)))
1158 sz
= sizeof(SERIAL_STATUS
);
1160 else status
= STATUS_INVALID_PARAMETER
;
1162 case IOCTL_SERIAL_GET_HANDFLOW
:
1163 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_HANDFLOW
))
1165 if (!(status
= get_hand_flow(fd
, lpOutBuffer
)))
1166 sz
= sizeof(SERIAL_HANDFLOW
);
1169 status
= STATUS_INVALID_PARAMETER
;
1171 case IOCTL_SERIAL_GET_LINE_CONTROL
:
1172 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_LINE_CONTROL
))
1174 if (!(status
= get_line_control(fd
, lpOutBuffer
)))
1175 sz
= sizeof(SERIAL_LINE_CONTROL
);
1178 status
= STATUS_INVALID_PARAMETER
;
1180 case IOCTL_SERIAL_GET_MODEMSTATUS
:
1181 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
1183 if (!(status
= get_modem_status(fd
, lpOutBuffer
)))
1186 else status
= STATUS_INVALID_PARAMETER
;
1188 case IOCTL_SERIAL_GET_TIMEOUTS
:
1189 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_TIMEOUTS
))
1191 if (!(status
= get_timeouts(hDevice
, lpOutBuffer
)))
1192 sz
= sizeof(SERIAL_TIMEOUTS
);
1195 status
= STATUS_INVALID_PARAMETER
;
1197 case IOCTL_SERIAL_GET_WAIT_MASK
:
1198 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
1200 if (!(status
= get_wait_mask(hDevice
, lpOutBuffer
, NULL
, NULL
, FALSE
)))
1204 status
= STATUS_INVALID_PARAMETER
;
1206 case IOCTL_SERIAL_IMMEDIATE_CHAR
:
1207 if (lpInBuffer
&& nInBufferSize
== sizeof(CHAR
))
1208 status
= xmit_immediate(hDevice
, fd
, lpInBuffer
);
1210 status
= STATUS_INVALID_PARAMETER
;
1212 case IOCTL_SERIAL_PURGE
:
1213 if (lpInBuffer
&& nInBufferSize
== sizeof(DWORD
))
1214 status
= purge(fd
, *(DWORD
*)lpInBuffer
);
1216 status
= STATUS_INVALID_PARAMETER
;
1218 case IOCTL_SERIAL_RESET_DEVICE
:
1219 FIXME("Unsupported\n");
1221 case IOCTL_SERIAL_SET_BAUD_RATE
:
1222 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_BAUD_RATE
))
1223 status
= set_baud_rate(fd
, lpInBuffer
);
1225 status
= STATUS_INVALID_PARAMETER
;
1227 case IOCTL_SERIAL_SET_BREAK_OFF
:
1228 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1229 if (ioctl(fd
, TIOCCBRK
, 0) == -1)
1231 TRACE("ioctl failed\n");
1232 status
= FILE_GetNtStatus();
1235 FIXME("ioctl not available\n");
1236 status
= STATUS_NOT_SUPPORTED
;
1239 case IOCTL_SERIAL_SET_BREAK_ON
:
1240 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1241 if (ioctl(fd
, TIOCSBRK
, 0) == -1)
1243 TRACE("ioctl failed\n");
1244 status
= FILE_GetNtStatus();
1247 FIXME("ioctl not available\n");
1248 status
= STATUS_NOT_SUPPORTED
;
1251 case IOCTL_SERIAL_SET_CHARS
:
1252 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_CHARS
))
1253 status
= set_special_chars(fd
, lpInBuffer
);
1255 status
= STATUS_INVALID_PARAMETER
;
1257 case IOCTL_SERIAL_SET_DTR
:
1259 if (whack_modem(fd
, 0, TIOCM_DTR
) == -1) status
= FILE_GetNtStatus();
1261 status
= STATUS_NOT_SUPPORTED
;
1264 case IOCTL_SERIAL_SET_HANDFLOW
:
1265 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_HANDFLOW
))
1266 status
= set_handflow(fd
, lpInBuffer
);
1268 status
= STATUS_INVALID_PARAMETER
;
1270 case IOCTL_SERIAL_SET_LINE_CONTROL
:
1271 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_LINE_CONTROL
))
1272 status
= set_line_control(fd
, lpInBuffer
);
1274 status
= STATUS_INVALID_PARAMETER
;
1276 case IOCTL_SERIAL_SET_QUEUE_SIZE
:
1277 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_QUEUE_SIZE
))
1278 status
= set_queue_size(fd
, lpInBuffer
);
1280 status
= STATUS_INVALID_PARAMETER
;
1282 case IOCTL_SERIAL_SET_RTS
:
1284 if (whack_modem(fd
, 0, TIOCM_RTS
) == -1) status
= FILE_GetNtStatus();
1286 status
= STATUS_NOT_SUPPORTED
;
1289 case IOCTL_SERIAL_SET_TIMEOUTS
:
1290 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_TIMEOUTS
))
1291 status
= set_timeouts(hDevice
, lpInBuffer
);
1293 status
= STATUS_INVALID_PARAMETER
;
1295 case IOCTL_SERIAL_SET_WAIT_MASK
:
1296 if (lpInBuffer
&& nInBufferSize
== sizeof(DWORD
))
1298 status
= set_wait_mask(hDevice
, *(DWORD
*)lpInBuffer
);
1300 else status
= STATUS_INVALID_PARAMETER
;
1302 case IOCTL_SERIAL_SET_XOFF
:
1303 status
= set_XOff(fd
);
1305 case IOCTL_SERIAL_SET_XON
:
1306 status
= set_XOn(fd
);
1308 case IOCTL_SERIAL_WAIT_ON_MASK
:
1309 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
1311 if (!(status
= wait_on(hDevice
, fd
, hEvent
, piosb
, lpOutBuffer
)))
1315 status
= STATUS_INVALID_PARAMETER
;
1318 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1319 dwIoControlCode
, dwIoControlCode
>> 16, (dwIoControlCode
>> 14) & 3,
1320 (dwIoControlCode
>> 2) & 0xFFF, dwIoControlCode
& 3);
1322 status
= STATUS_INVALID_PARAMETER
;
1325 if (needs_close
) close( fd
);
1327 piosb
->u
.Status
= status
;
1328 piosb
->Information
= sz
;
1329 if (hEvent
&& status
!= STATUS_PENDING
) NtSetEvent(hEvent
, NULL
);
1333 NTSTATUS
COMM_DeviceIoControl(HANDLE hDevice
,
1334 HANDLE hEvent
, PIO_APC_ROUTINE UserApcRoutine
,
1335 PVOID UserApcContext
,
1336 PIO_STATUS_BLOCK piosb
,
1337 ULONG dwIoControlCode
,
1338 LPVOID lpInBuffer
, DWORD nInBufferSize
,
1339 LPVOID lpOutBuffer
, DWORD nOutBufferSize
)
1343 if (dwIoControlCode
== IOCTL_SERIAL_WAIT_ON_MASK
)
1345 HANDLE hev
= hEvent
;
1347 /* this is an ioctl we implement in a non blocking way if hEvent is not
1349 * so we have to explicitly wait if no hEvent is provided
1353 OBJECT_ATTRIBUTES attr
;
1355 attr
.Length
= sizeof(attr
);
1356 attr
.RootDirectory
= 0;
1357 attr
.ObjectName
= NULL
;
1358 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
;
1359 attr
.SecurityDescriptor
= NULL
;
1360 attr
.SecurityQualityOfService
= NULL
;
1361 status
= NtCreateEvent(&hev
, EVENT_ALL_ACCESS
, &attr
, SynchronizationEvent
, FALSE
);
1363 if (status
) return status
;
1365 status
= io_control(hDevice
, hev
, UserApcRoutine
, UserApcContext
,
1366 piosb
, dwIoControlCode
, lpInBuffer
, nInBufferSize
,
1367 lpOutBuffer
, nOutBufferSize
);
1370 if (status
== STATUS_PENDING
)
1372 NtWaitForSingleObject(hev
, FALSE
, NULL
);
1373 status
= STATUS_SUCCESS
;
1378 else status
= io_control(hDevice
, hEvent
, UserApcRoutine
, UserApcContext
,
1379 piosb
, dwIoControlCode
, lpInBuffer
, nInBufferSize
,
1380 lpOutBuffer
, nOutBufferSize
);
1384 NTSTATUS
COMM_FlushBuffersFile( int fd
)
1387 while (tcdrain( fd
) == -1)
1389 if (errno
!= EINTR
) return FILE_GetNtStatus();
1391 return STATUS_SUCCESS
;
1392 #elif defined(TIOCDRAIN)
1393 while (ioctl( fd
, TIOCDRAIN
) == -1)
1395 if (errno
!= EINTR
) return FILE_GetNtStatus();
1397 return STATUS_SUCCESS
;
1398 #elif defined(TCSBRK)
1399 while (ioctl( fd
, TCSBRK
, 1 ) == -1)
1401 if (errno
!= EINTR
) return FILE_GetNtStatus();
1403 return STATUS_SUCCESS
;
1405 ERR( "not supported\n" );
1406 return STATUS_NOT_IMPLEMENTED
;