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"
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
40 #include <sys/types.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_POLL_H
48 # include <sys/poll.h>
50 #ifdef HAVE_SYS_MODEM_H
51 # include <sys/modem.h>
53 #ifdef HAVE_SYS_STRTIO_H
54 # include <sys/strtio.h>
58 #define WIN32_NO_STATUS
59 #define NONAMELESSUNION
63 #include "ddk/ntddser.h"
64 #include "ntdll_misc.h"
65 #include "wine/server.h"
66 #include "wine/library.h"
67 #include "wine/debug.h"
69 #ifdef HAVE_LINUX_SERIAL_H
70 #ifdef HAVE_ASM_TYPES_H
71 #include <asm/types.h>
73 #include <linux/serial.h>
76 #if !defined(TIOCINQ) && defined(FIONREAD)
77 #define TIOCINQ FIONREAD
80 WINE_DEFAULT_DEBUG_CHANNEL(comm
);
82 static const char* iocode2str(DWORD ioc
)
86 #define X(x) case (x): return #x
87 X(IOCTL_SERIAL_CLEAR_STATS
);
88 X(IOCTL_SERIAL_CLR_DTR
);
89 X(IOCTL_SERIAL_CLR_RTS
);
90 X(IOCTL_SERIAL_CONFIG_SIZE
);
91 X(IOCTL_SERIAL_GET_BAUD_RATE
);
92 X(IOCTL_SERIAL_GET_CHARS
);
93 X(IOCTL_SERIAL_GET_COMMSTATUS
);
94 X(IOCTL_SERIAL_GET_DTRRTS
);
95 X(IOCTL_SERIAL_GET_HANDFLOW
);
96 X(IOCTL_SERIAL_GET_LINE_CONTROL
);
97 X(IOCTL_SERIAL_GET_MODEM_CONTROL
);
98 X(IOCTL_SERIAL_GET_MODEMSTATUS
);
99 X(IOCTL_SERIAL_GET_PROPERTIES
);
100 X(IOCTL_SERIAL_GET_STATS
);
101 X(IOCTL_SERIAL_GET_TIMEOUTS
);
102 X(IOCTL_SERIAL_GET_WAIT_MASK
);
103 X(IOCTL_SERIAL_IMMEDIATE_CHAR
);
104 X(IOCTL_SERIAL_LSRMST_INSERT
);
105 X(IOCTL_SERIAL_PURGE
);
106 X(IOCTL_SERIAL_RESET_DEVICE
);
107 X(IOCTL_SERIAL_SET_BAUD_RATE
);
108 X(IOCTL_SERIAL_SET_BREAK_ON
);
109 X(IOCTL_SERIAL_SET_BREAK_OFF
);
110 X(IOCTL_SERIAL_SET_CHARS
);
111 X(IOCTL_SERIAL_SET_DTR
);
112 X(IOCTL_SERIAL_SET_FIFO_CONTROL
);
113 X(IOCTL_SERIAL_SET_HANDFLOW
);
114 X(IOCTL_SERIAL_SET_LINE_CONTROL
);
115 X(IOCTL_SERIAL_SET_MODEM_CONTROL
);
116 X(IOCTL_SERIAL_SET_QUEUE_SIZE
);
117 X(IOCTL_SERIAL_SET_RTS
);
118 X(IOCTL_SERIAL_SET_TIMEOUTS
);
119 X(IOCTL_SERIAL_SET_WAIT_MASK
);
120 X(IOCTL_SERIAL_SET_XOFF
);
121 X(IOCTL_SERIAL_SET_XON
);
122 X(IOCTL_SERIAL_WAIT_ON_MASK
);
123 X(IOCTL_SERIAL_XOFF_COUNTER
);
125 default: { static char tmp
[32]; sprintf(tmp
, "IOCTL_SERIAL_%d\n", ioc
); return tmp
; }
129 static NTSTATUS
get_baud_rate(int fd
, SERIAL_BAUD_RATE
* sbr
)
134 if (tcgetattr(fd
, &port
) == -1)
136 ERR("tcgetattr error '%s'\n", strerror(errno
));
137 return FILE_GetNtStatus();
139 speed
= cfgetospeed(&port
);
142 case B0
: sbr
->BaudRate
= 0; break;
143 case B50
: sbr
->BaudRate
= 50; break;
144 case B75
: sbr
->BaudRate
= 75; break;
145 case B110
: sbr
->BaudRate
= 110; break;
146 case B134
: sbr
->BaudRate
= 134; break;
147 case B150
: sbr
->BaudRate
= 150; break;
148 case B200
: sbr
->BaudRate
= 200; break;
149 case B300
: sbr
->BaudRate
= 300; break;
150 case B600
: sbr
->BaudRate
= 600; break;
151 case B1200
: sbr
->BaudRate
= 1200; break;
152 case B1800
: sbr
->BaudRate
= 1800; break;
153 case B2400
: sbr
->BaudRate
= 2400; break;
154 case B4800
: sbr
->BaudRate
= 4800; break;
155 case B9600
: sbr
->BaudRate
= 9600; break;
156 case B19200
: sbr
->BaudRate
= 19200; break;
157 case B38400
: sbr
->BaudRate
= 38400; break;
159 case B57600
: sbr
->BaudRate
= 57600; break;
162 case B115200
: sbr
->BaudRate
= 115200; break;
165 case B230400
: sbr
->BaudRate
= 230400; break;
168 case B460800
: sbr
->BaudRate
= 460800; break;
171 case B500000
: sbr
->BaudRate
= 500000; break;
174 case B921600
: sbr
->BaudRate
= 921600; break;
177 case B1000000
: sbr
->BaudRate
= 1000000; break;
180 case B1152000
: sbr
->BaudRate
= 1152000; break;
183 case B1500000
: sbr
->BaudRate
= 1500000; break;
186 case B2000000
: sbr
->BaudRate
= 2000000; break;
189 case B2500000
: sbr
->BaudRate
= 2500000; break;
192 case B3000000
: sbr
->BaudRate
= 3000000; break;
195 case B3500000
: sbr
->BaudRate
= 3500000; break;
198 case B4000000
: sbr
->BaudRate
= 4000000; break;
201 ERR("unknown speed %x\n", speed
);
202 return STATUS_INVALID_PARAMETER
;
204 return STATUS_SUCCESS
;
207 static NTSTATUS
get_hand_flow(int fd
, SERIAL_HANDFLOW
* shf
)
212 if (tcgetattr(fd
, &port
) == -1)
214 ERR("tcgetattr error '%s'\n", strerror(errno
));
215 return FILE_GetNtStatus();
217 /* termios does not support DTR/DSR flow control */
218 shf
->ControlHandShake
= 0;
219 shf
->FlowReplace
= 0;
221 if (ioctl(fd
, TIOCMGET
, &stat
) == -1)
223 WARN("ioctl error '%s'\n", strerror(errno
));
224 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
225 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
228 WARN("Setting DTR/RTS to enabled by default\n");
229 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
230 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
233 if (stat
& TIOCM_DTR
)
235 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
237 if (port
.c_cflag
& CRTSCTS
)
239 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
240 shf
->ControlHandShake
|= SERIAL_CTS_HANDSHAKE
;
246 if (stat
& TIOCM_RTS
)
248 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
250 if (port
.c_iflag
& IXOFF
)
251 shf
->FlowReplace
|= SERIAL_AUTO_RECEIVE
;
252 if (port
.c_iflag
& IXON
)
253 shf
->FlowReplace
|= SERIAL_AUTO_TRANSMIT
;
257 return STATUS_SUCCESS
;
260 static NTSTATUS
get_line_control(int fd
, SERIAL_LINE_CONTROL
* slc
)
264 if (tcgetattr(fd
, &port
) == -1)
266 ERR("tcgetattr error '%s'\n", strerror(errno
));
267 return FILE_GetNtStatus();
271 switch (port
.c_cflag
& (PARENB
| PARODD
| CMSPAR
))
273 switch (port
.c_cflag
& (PARENB
| PARODD
))
276 case 0: slc
->Parity
= NOPARITY
; break;
277 case PARENB
: slc
->Parity
= EVENPARITY
; break;
278 case PARENB
|PARODD
: slc
->Parity
= ODDPARITY
; break;
280 case PARENB
|PARODD
|CMSPAR
: slc
->Parity
= MARKPARITY
; break;
281 case PARENB
|CMSPAR
: slc
->Parity
= SPACEPARITY
; break;
284 switch (port
.c_cflag
& CSIZE
)
286 case CS5
: slc
->WordLength
= 5; break;
287 case CS6
: slc
->WordLength
= 6; break;
288 case CS7
: slc
->WordLength
= 7; break;
289 case CS8
: slc
->WordLength
= 8; break;
290 default: ERR("unknown size %x\n", (UINT
)(port
.c_cflag
& CSIZE
));
293 if (port
.c_cflag
& CSTOPB
)
295 if (slc
->WordLength
== 5)
296 slc
->StopBits
= ONE5STOPBITS
;
298 slc
->StopBits
= TWOSTOPBITS
;
301 slc
->StopBits
= ONESTOPBIT
;
303 return STATUS_SUCCESS
;
306 static NTSTATUS
get_modem_status(int fd
, DWORD
* lpModemStat
)
308 NTSTATUS status
= STATUS_NOT_SUPPORTED
;
313 if (!ioctl(fd
, TIOCMGET
, &mstat
))
316 if (mstat
& TIOCM_CTS
) *lpModemStat
|= MS_CTS_ON
;
319 if (mstat
& TIOCM_DSR
) *lpModemStat
|= MS_DSR_ON
;
322 if (mstat
& TIOCM_RNG
) *lpModemStat
|= MS_RING_ON
;
325 /* FIXME: Not really sure about RLSD UB 990810 */
326 if (mstat
& TIOCM_CAR
) *lpModemStat
|= MS_RLSD_ON
;
328 TRACE("%04x -> %s%s%s%s\n", mstat
,
329 (*lpModemStat
& MS_RLSD_ON
) ? "MS_RLSD_ON " : "",
330 (*lpModemStat
& MS_RING_ON
) ? "MS_RING_ON " : "",
331 (*lpModemStat
& MS_DSR_ON
) ? "MS_DSR_ON " : "",
332 (*lpModemStat
& MS_CTS_ON
) ? "MS_CTS_ON " : "");
333 return STATUS_SUCCESS
;
335 WARN("TIOCMGET err %s\n", strerror(errno
));
336 status
= FILE_GetNtStatus();
341 static NTSTATUS
get_properties(int fd
, SERIAL_COMMPROP
*prop
)
343 /* FIXME: get actual properties from the device */
344 memset( prop
, 0, sizeof(*prop
) );
345 prop
->PacketLength
= 1;
346 prop
->PacketVersion
= 1;
347 prop
->ServiceMask
= SP_SERIALCOMM
;
348 prop
->MaxTxQueue
= 4096;
349 prop
->MaxRxQueue
= 4096;
350 prop
->MaxBaud
= BAUD_115200
;
351 prop
->ProvSubType
= PST_RS232
;
352 prop
->ProvCapabilities
= PCF_DTRDSR
| PCF_PARITY_CHECK
| PCF_RTSCTS
| PCF_TOTALTIMEOUTS
| PCF_INTTIMEOUTS
;
353 prop
->SettableParams
= SP_BAUD
| SP_DATABITS
| SP_HANDSHAKING
|
354 SP_PARITY
| SP_PARITY_CHECK
| SP_STOPBITS
;
355 prop
->SettableBaud
= BAUD_075
| BAUD_110
| BAUD_134_5
| BAUD_150
|
356 BAUD_300
| BAUD_600
| BAUD_1200
| BAUD_1800
| BAUD_2400
| BAUD_4800
|
357 BAUD_9600
| BAUD_19200
| BAUD_38400
| BAUD_57600
| BAUD_115200
;
358 prop
->SettableData
= DATABITS_5
| DATABITS_6
| DATABITS_7
| DATABITS_8
;
359 prop
->SettableStopParity
= STOPBITS_10
| STOPBITS_15
| STOPBITS_20
|
360 PARITY_NONE
| PARITY_ODD
|PARITY_EVEN
| PARITY_MARK
| PARITY_SPACE
;
361 prop
->CurrentTxQueue
= prop
->MaxTxQueue
;
362 prop
->CurrentRxQueue
= prop
->MaxRxQueue
;
363 return STATUS_SUCCESS
;
366 static NTSTATUS
get_special_chars(int fd
, SERIAL_CHARS
* sc
)
370 if (tcgetattr(fd
, &port
) == -1)
372 ERR("tcgetattr error '%s'\n", strerror(errno
));
373 return FILE_GetNtStatus();
375 sc
->EofChar
= port
.c_cc
[VEOF
];
376 sc
->ErrorChar
= 0xFF;
377 sc
->BreakChar
= 0; /* FIXME */
378 sc
->EventChar
= 0; /* FIXME */
379 sc
->XonChar
= port
.c_cc
[VSTART
];
380 sc
->XoffChar
= port
.c_cc
[VSTOP
];
382 return STATUS_SUCCESS
;
385 static NTSTATUS
get_status(int fd
, SERIAL_STATUS
* ss
)
387 NTSTATUS status
= STATUS_SUCCESS
;
391 ss
->EofReceived
= FALSE
;
392 ss
->WaitForImmediate
= FALSE
;
394 if (ioctl(fd
, TIOCOUTQ
, &ss
->AmountInOutQueue
) == -1)
396 WARN("ioctl returned error\n");
397 status
= FILE_GetNtStatus();
400 ss
->AmountInOutQueue
= 0; /* FIXME: find a different way to find out */
404 if (ioctl(fd
, TIOCINQ
, &ss
->AmountInInQueue
))
406 WARN("ioctl returned error\n");
407 status
= FILE_GetNtStatus();
410 ss
->AmountInInQueue
= 0; /* FIXME: find a different way to find out */
415 static void stop_waiting( HANDLE handle
)
419 SERVER_START_REQ( set_serial_info
)
421 req
->handle
= wine_server_obj_handle( handle
);
422 req
->flags
= SERIALINFO_PENDING_WAIT
;
423 if ((status
= wine_server_call( req
)))
424 ERR("failed to clear waiting state: %#x\n", status
);
429 static NTSTATUS
get_wait_mask(HANDLE hDevice
, DWORD
*mask
, DWORD
*cookie
, DWORD
*pending_write
, BOOL start_wait
)
433 SERVER_START_REQ( get_serial_info
)
435 req
->handle
= wine_server_obj_handle( hDevice
);
436 req
->flags
= pending_write
? SERIALINFO_PENDING_WRITE
: 0;
437 if (start_wait
) req
->flags
|= SERIALINFO_PENDING_WAIT
;
438 if (!(status
= wine_server_call( req
)))
440 *mask
= reply
->eventmask
;
441 if (cookie
) *cookie
= reply
->cookie
;
442 if (pending_write
) *pending_write
= reply
->pending_write
;
449 static NTSTATUS
purge(int fd
, DWORD flags
)
452 ** not exactly sure how these are different
453 ** Perhaps if we had our own internal queues, one flushes them
454 ** and the other flushes the kernel's buffers.
456 if (flags
& PURGE_TXABORT
) tcflush(fd
, TCOFLUSH
);
457 if (flags
& PURGE_RXABORT
) tcflush(fd
, TCIFLUSH
);
458 if (flags
& PURGE_TXCLEAR
) tcflush(fd
, TCOFLUSH
);
459 if (flags
& PURGE_RXCLEAR
) tcflush(fd
, TCIFLUSH
);
460 return STATUS_SUCCESS
;
463 static NTSTATUS
set_baud_rate(int fd
, const SERIAL_BAUD_RATE
* sbr
)
467 if (tcgetattr(fd
, &port
) == -1)
469 ERR("tcgetattr error '%s'\n", strerror(errno
));
470 return FILE_GetNtStatus();
473 switch (sbr
->BaudRate
)
475 case 0: cfsetospeed( &port
, B0
); break;
476 case 50: cfsetospeed( &port
, B50
); break;
477 case 75: cfsetospeed( &port
, B75
); break;
479 case CBR_110
: cfsetospeed( &port
, B110
); break;
480 case 134: cfsetospeed( &port
, B134
); break;
481 case 150: cfsetospeed( &port
, B150
); break;
482 case 200: cfsetospeed( &port
, B200
); break;
484 case CBR_300
: cfsetospeed( &port
, B300
); break;
486 case CBR_600
: cfsetospeed( &port
, B600
); break;
488 case CBR_1200
: cfsetospeed( &port
, B1200
); break;
489 case 1800: cfsetospeed( &port
, B1800
); break;
491 case CBR_2400
: cfsetospeed( &port
, B2400
); break;
493 case CBR_4800
: cfsetospeed( &port
, B4800
); break;
495 case CBR_9600
: cfsetospeed( &port
, B9600
); break;
497 case CBR_19200
: cfsetospeed( &port
, B19200
); break;
499 case CBR_38400
: cfsetospeed( &port
, B38400
); break;
501 case 57600: cfsetospeed( &port
, B57600
); break;
504 case 115200: cfsetospeed( &port
, B115200
); break;
507 case 230400: cfsetospeed( &port
, B230400
); break;
510 case 460800: cfsetospeed( &port
, B460800
); break;
513 case 500000: cfsetospeed( &port
, B500000
); break;
516 case 921600: cfsetospeed( &port
, B921600
); break;
519 case 1000000: cfsetospeed( &port
, B1000000
); break;
522 case 1152000: cfsetospeed( &port
, B1152000
); break;
525 case 1500000: cfsetospeed( &port
, B1500000
); break;
528 case 2000000: cfsetospeed( &port
, B2000000
); break;
531 case 2500000: cfsetospeed( &port
, B2500000
); break;
534 case 3000000: cfsetospeed( &port
, B3000000
); break;
537 case 3500000: cfsetospeed( &port
, B3500000
); break;
540 case 4000000: cfsetospeed( &port
, B4000000
); break;
543 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
545 struct serial_struct nuts
;
548 ioctl(fd
, TIOCGSERIAL
, &nuts
);
549 nuts
.custom_divisor
= nuts
.baud_base
/ sbr
->BaudRate
;
550 if (!(nuts
.custom_divisor
)) nuts
.custom_divisor
= 1;
551 arby
= nuts
.baud_base
/ nuts
.custom_divisor
;
552 nuts
.flags
&= ~ASYNC_SPD_MASK
;
553 nuts
.flags
|= ASYNC_SPD_CUST
;
554 WARN("You (or a program acting at your behest) have specified\n"
555 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
556 "which is as close as we can get by our present understanding of your\n"
557 "hardware. I hope you know what you are doing. Any disruption Wine\n"
558 "has caused to your linux system can be undone with setserial\n"
559 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
560 "reset it and it will probably recover.\n", sbr
->BaudRate
, arby
);
561 ioctl(fd
, TIOCSSERIAL
, &nuts
);
562 cfsetospeed( &port
, B38400
);
565 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
566 ERR("baudrate %d\n", sbr
->BaudRate
);
567 return STATUS_NOT_SUPPORTED
;
568 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
570 cfsetispeed( &port
, cfgetospeed(&port
) );
571 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
573 ERR("tcsetattr error '%s'\n", strerror(errno
));
574 return FILE_GetNtStatus();
576 return STATUS_SUCCESS
;
579 static int whack_modem(int fd
, unsigned int andy
, unsigned int orrie
)
582 unsigned int mstat
, okay
;
583 okay
= ioctl(fd
, TIOCMGET
, &mstat
);
584 if (okay
) return okay
;
585 if (andy
) mstat
&= andy
;
587 return ioctl(fd
, TIOCMSET
, &mstat
);
593 static NTSTATUS
set_handflow(int fd
, const SERIAL_HANDFLOW
* shf
)
597 if ((shf
->FlowReplace
& (SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
)) ==
598 (SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
))
599 return STATUS_NOT_SUPPORTED
;
601 if (tcgetattr(fd
, &port
) == -1)
603 ERR("tcgetattr error '%s'\n", strerror(errno
));
604 return FILE_GetNtStatus();
608 if ((shf
->ControlHandShake
& SERIAL_CTS_HANDSHAKE
) ||
609 (shf
->FlowReplace
& SERIAL_RTS_HANDSHAKE
))
611 port
.c_cflag
|= CRTSCTS
;
615 port
.c_cflag
&= ~CRTSCTS
;
618 if (shf
->ControlHandShake
& SERIAL_DTR_HANDSHAKE
)
620 WARN("DSR/DTR flow control not supported\n");
621 } else if (!(shf
->ControlHandShake
& SERIAL_DTR_CONTROL
))
622 whack_modem(fd
, ~TIOCM_DTR
, 0);
624 whack_modem(fd
, 0, TIOCM_DTR
);
627 if (!(shf
->ControlHandShake
& SERIAL_CTS_HANDSHAKE
))
629 if ((shf
->FlowReplace
& (SERIAL_RTS_CONTROL
|SERIAL_RTS_HANDSHAKE
)) == 0)
630 whack_modem(fd
, ~TIOCM_RTS
, 0);
632 whack_modem(fd
, 0, TIOCM_RTS
);
636 if (shf
->FlowReplace
& SERIAL_AUTO_RECEIVE
)
637 port
.c_iflag
|= IXOFF
;
639 port
.c_iflag
&= ~IXOFF
;
640 if (shf
->FlowReplace
& SERIAL_AUTO_TRANSMIT
)
641 port
.c_iflag
|= IXON
;
643 port
.c_iflag
&= ~IXON
;
644 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
646 ERR("tcsetattr error '%s'\n", strerror(errno
));
647 return FILE_GetNtStatus();
650 return STATUS_SUCCESS
;
653 static NTSTATUS
set_line_control(int fd
, const SERIAL_LINE_CONTROL
* slc
)
656 unsigned bytesize
, stopbits
;
658 if (tcgetattr(fd
, &port
) == -1)
660 ERR("tcgetattr error '%s'\n", strerror(errno
));
661 return FILE_GetNtStatus();
665 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|PARMRK
|IMAXBEL
);
667 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|PARMRK
);
669 port
.c_iflag
|= IGNBRK
| INPCK
;
670 port
.c_oflag
&= ~(OPOST
);
671 port
.c_cflag
&= ~(HUPCL
);
672 port
.c_cflag
|= CLOCAL
| CREAD
;
675 * on FreeBSD, turning off ICANON does not disable IEXTEN,
676 * so we must turn it off explicitly. No harm done on Linux.
678 port
.c_lflag
&= ~(ICANON
|ECHO
|ISIG
|IEXTEN
);
679 port
.c_lflag
|= NOFLSH
;
681 bytesize
= slc
->WordLength
;
682 stopbits
= slc
->StopBits
;
685 port
.c_cflag
&= ~(PARENB
| PARODD
| CMSPAR
);
687 port
.c_cflag
&= ~(PARENB
| PARODD
);
690 /* make sure that reads don't block */
692 port
.c_cc
[VTIME
] = 0;
696 case NOPARITY
: port
.c_iflag
&= ~INPCK
; break;
697 case ODDPARITY
: port
.c_cflag
|= PARENB
| PARODD
; break;
698 case EVENPARITY
: port
.c_cflag
|= PARENB
; break;
700 /* Linux defines mark/space (stick) parity */
701 case MARKPARITY
: port
.c_cflag
|= PARENB
| PARODD
| CMSPAR
; break;
702 case SPACEPARITY
: port
.c_cflag
|= PARENB
| CMSPAR
; break;
704 /* try the POSIX way */
706 if (slc
->StopBits
== ONESTOPBIT
)
708 stopbits
= TWOSTOPBITS
;
709 port
.c_iflag
&= ~INPCK
;
713 FIXME("Cannot set MARK Parity\n");
714 return STATUS_NOT_SUPPORTED
;
718 if (slc
->WordLength
< 8)
721 port
.c_iflag
&= ~INPCK
;
725 FIXME("Cannot set SPACE Parity\n");
726 return STATUS_NOT_SUPPORTED
;
731 FIXME("Parity %d is not supported\n", slc
->Parity
);
732 return STATUS_NOT_SUPPORTED
;
735 port
.c_cflag
&= ~CSIZE
;
738 case 5: port
.c_cflag
|= CS5
; break;
739 case 6: port
.c_cflag
|= CS6
; break;
740 case 7: port
.c_cflag
|= CS7
; break;
741 case 8: port
.c_cflag
|= CS8
; break;
743 FIXME("ByteSize %d is not supported\n", bytesize
);
744 return STATUS_NOT_SUPPORTED
;
749 case ONESTOPBIT
: port
.c_cflag
&= ~CSTOPB
; break;
750 case ONE5STOPBITS
: /* will be selected if bytesize is 5 */
751 case TWOSTOPBITS
: port
.c_cflag
|= CSTOPB
; break;
753 FIXME("StopBits %d is not supported\n", stopbits
);
754 return STATUS_NOT_SUPPORTED
;
756 /* otherwise it hangs with pending input*/
757 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
759 ERR("tcsetattr error '%s'\n", strerror(errno
));
760 return FILE_GetNtStatus();
762 return STATUS_SUCCESS
;
765 static NTSTATUS
set_queue_size(int fd
, const SERIAL_QUEUE_SIZE
* sqs
)
767 FIXME("insize %d outsize %d unimplemented stub\n", sqs
->InSize
, sqs
->OutSize
);
768 return STATUS_SUCCESS
;
771 static NTSTATUS
set_special_chars(int fd
, const SERIAL_CHARS
* sc
)
775 if (tcgetattr(fd
, &port
) == -1)
777 ERR("tcgetattr error '%s'\n", strerror(errno
));
778 return FILE_GetNtStatus();
781 port
.c_cc
[VEOF
] = sc
->EofChar
;
782 /* FIXME: sc->ErrorChar is not supported */
783 /* FIXME: sc->BreakChar is not supported */
784 /* FIXME: sc->EventChar is not supported */
785 port
.c_cc
[VSTART
] = sc
->XonChar
;
786 port
.c_cc
[VSTOP
] = sc
->XoffChar
;
788 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
790 ERR("tcsetattr error '%s'\n", strerror(errno
));
791 return FILE_GetNtStatus();
793 return STATUS_SUCCESS
;
797 * does not change IXOFF but simulates that IXOFF has been received:
799 static NTSTATUS
set_XOff(int fd
)
801 if (tcflow(fd
, TCOOFF
))
803 return FILE_GetNtStatus();
805 return STATUS_SUCCESS
;
809 * does not change IXON but simulates that IXON has been received:
811 static NTSTATUS
set_XOn(int fd
)
813 if (tcflow(fd
, TCOON
))
815 return FILE_GetNtStatus();
817 return STATUS_SUCCESS
;
821 * local structure holding the irq values we need for WaitCommEvent()
823 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
824 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
825 * no need to carry them in the internal structure
828 typedef struct serial_irq_info
830 int rx
, tx
, frame
, overrun
, parity
, brk
, buf_overrun
, temt
;
833 /***********************************************************************
834 * Data needed by the thread polling for the changing CommEvent
836 typedef struct async_commio
840 IO_STATUS_BLOCK
* iosb
;
846 serial_irq_info irq_info
;
849 /***********************************************************************
850 * Get extended interrupt count info, needed for wait_on
852 static NTSTATUS
get_irq_info(int fd
, serial_irq_info
*irq_info
)
856 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCGICOUNT)
857 struct serial_icounter_struct einfo
;
858 if (!ioctl(fd
, TIOCGICOUNT
, &einfo
))
860 irq_info
->rx
= einfo
.rx
;
861 irq_info
->tx
= einfo
.tx
;
862 irq_info
->frame
= einfo
.frame
;
863 irq_info
->overrun
= einfo
.overrun
;
864 irq_info
->parity
= einfo
.parity
;
865 irq_info
->brk
= einfo
.brk
;
866 irq_info
->buf_overrun
= einfo
.buf_overrun
;
870 TRACE("TIOCGICOUNT err %s\n", strerror(errno
));
871 memset(irq_info
,0, sizeof(serial_irq_info
));
874 memset(irq_info
,0, sizeof(serial_irq_info
));
878 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
879 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
880 if (!ioctl(fd
, TIOCSERGETLSR
, &out
))
882 irq_info
->temt
= (out
& TIOCSER_TEMT
) != 0;
883 return STATUS_SUCCESS
;
886 TRACE("TIOCSERGETLSR err %s\n", strerror(errno
));
888 #ifdef TIOCOUTQ /* otherwise we log when the out queue gets empty */
889 if (!ioctl(fd
, TIOCOUTQ
, &out
))
891 irq_info
->temt
= out
== 0;
892 return STATUS_SUCCESS
;
894 TRACE("TIOCOUTQ err %s\n", strerror(errno
));
895 return FILE_GetNtStatus();
897 return STATUS_SUCCESS
;
901 static DWORD
check_events(int fd
, DWORD mask
,
902 const serial_irq_info
*new,
903 const serial_irq_info
*old
,
904 DWORD new_mstat
, DWORD old_mstat
, DWORD pending_write
)
906 DWORD ret
= 0, queue
;
908 TRACE("mask 0x%08x\n", mask
);
909 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old
->rx
, new->rx
);
910 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old
->tx
, new->tx
);
911 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old
->frame
, new->frame
);
912 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old
->overrun
, new->overrun
);
913 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old
->parity
, new->parity
);
914 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old
->brk
, new->brk
);
915 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old
->buf_overrun
, new->buf_overrun
);
916 TRACE("old->temt 0x%08x vs. new->temt 0x%08x\n", old
->temt
, new->temt
);
918 if (old
->brk
!= new->brk
) ret
|= EV_BREAK
;
919 if ((old_mstat
& MS_CTS_ON
) != (new_mstat
& MS_CTS_ON
)) ret
|= EV_CTS
;
920 if ((old_mstat
& MS_DSR_ON
) != (new_mstat
& MS_DSR_ON
)) ret
|= EV_DSR
;
921 if ((old_mstat
& MS_RING_ON
) != (new_mstat
& MS_RING_ON
)) ret
|= EV_RING
;
922 if ((old_mstat
& MS_RLSD_ON
) != (new_mstat
& MS_RLSD_ON
)) ret
|= EV_RLSD
;
923 if (old
->frame
!= new->frame
|| old
->overrun
!= new->overrun
|| old
->parity
!= new->parity
) ret
|= EV_ERR
;
924 if (mask
& EV_RXCHAR
)
928 if (ioctl(fd
, TIOCINQ
, &queue
))
929 WARN("TIOCINQ returned error\n");
934 if (mask
& EV_TXEMPTY
)
936 if ((!old
->temt
|| pending_write
) && new->temt
)
942 /***********************************************************************
943 * wait_for_event (INTERNAL)
945 * We need to poll for what is interesting
946 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
949 static DWORD CALLBACK
wait_for_event(LPVOID arg
)
951 async_commio
*commio
= arg
;
954 if (!server_get_unix_fd( commio
->hDevice
, FILE_READ_DATA
| FILE_WRITE_DATA
, &fd
, &needs_close
, NULL
, NULL
))
956 serial_irq_info new_irq_info
;
957 DWORD new_mstat
, dummy
, cookie
;
960 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
961 commio
->hDevice
, fd
, commio
->evtmask
, commio
->events
, commio
->hEvent
, &commio
->irq_info
);
963 time
.QuadPart
= (ULONGLONG
)10000;
964 time
.QuadPart
= -time
.QuadPart
;
968 * TIOCMIWAIT is not adequate
971 * We don't handle the EV_RXFLAG (the eventchar)
973 NtDelayExecution(FALSE
, &time
);
974 get_irq_info(fd
, &new_irq_info
);
975 if (get_modem_status(fd
, &new_mstat
))
977 TRACE("get_modem_status failed\n");
981 *commio
->events
= check_events(fd
, commio
->evtmask
,
982 &new_irq_info
, &commio
->irq_info
,
983 new_mstat
, commio
->mstat
, commio
->pending_write
);
984 if (*commio
->events
) break;
985 get_wait_mask(commio
->hDevice
, &dummy
, &cookie
, (commio
->evtmask
& EV_TXEMPTY
) ? &commio
->pending_write
: NULL
, FALSE
);
986 if (commio
->cookie
!= cookie
)
992 if (needs_close
) close( fd
);
998 commio
->iosb
->u
.Status
= STATUS_SUCCESS
;
999 commio
->iosb
->Information
= sizeof(DWORD
);
1002 commio
->iosb
->u
.Status
= STATUS_CANCELLED
;
1004 stop_waiting(commio
->hDevice
);
1005 if (commio
->hEvent
) NtSetEvent(commio
->hEvent
, NULL
);
1006 RtlFreeHeap(GetProcessHeap(), 0, commio
);
1010 static NTSTATUS
wait_on(HANDLE hDevice
, int fd
, HANDLE hEvent
, PIO_STATUS_BLOCK piosb
, DWORD
* events
)
1012 async_commio
* commio
;
1015 if ((status
= NtResetEvent(hEvent
, NULL
)))
1018 commio
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio
));
1019 if (!commio
) return STATUS_NO_MEMORY
;
1021 commio
->hDevice
= hDevice
;
1022 commio
->events
= events
;
1023 commio
->iosb
= piosb
;
1024 commio
->hEvent
= hEvent
;
1025 commio
->pending_write
= 0;
1026 status
= get_wait_mask(commio
->hDevice
, &commio
->evtmask
, &commio
->cookie
, (commio
->evtmask
& EV_TXEMPTY
) ? &commio
->pending_write
: NULL
, TRUE
);
1029 RtlFreeHeap(GetProcessHeap(), 0, commio
);
1033 /* We may never return, if some capabilities miss
1034 * Return error in that case
1036 #if !defined(TIOCINQ)
1037 if (commio
->evtmask
& EV_RXCHAR
)
1040 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1041 if (commio
->evtmask
& EV_TXEMPTY
)
1044 #if !defined(TIOCMGET)
1045 if (commio
->evtmask
& (EV_CTS
| EV_DSR
| EV_RING
| EV_RLSD
))
1048 #if !defined(TIOCM_CTS)
1049 if (commio
->evtmask
& EV_CTS
)
1052 #if !defined(TIOCM_DSR)
1053 if (commio
->evtmask
& EV_DSR
)
1056 #if !defined(TIOCM_RNG)
1057 if (commio
->evtmask
& EV_RING
)
1060 #if !defined(TIOCM_CAR)
1061 if (commio
->evtmask
& EV_RLSD
)
1064 if (commio
->evtmask
& EV_RXFLAG
)
1065 FIXME("EV_RXFLAG not handled\n");
1067 if ((status
= get_irq_info(fd
, &commio
->irq_info
)) &&
1068 (commio
->evtmask
& (EV_BREAK
| EV_ERR
)))
1071 if ((status
= get_modem_status(fd
, &commio
->mstat
)) &&
1072 (commio
->evtmask
& (EV_CTS
| EV_DSR
| EV_RING
| EV_RLSD
)))
1075 /* We might have received something or the TX buffer is delivered */
1076 *events
= check_events(fd
, commio
->evtmask
,
1077 &commio
->irq_info
, &commio
->irq_info
,
1078 commio
->mstat
, commio
->mstat
, commio
->pending_write
);
1081 status
= STATUS_SUCCESS
;
1085 /* create the worker for the task */
1086 status
= RtlQueueWorkItem(wait_for_event
, commio
, 0 /* FIXME */);
1087 if (status
!= STATUS_SUCCESS
) goto out_now
;
1088 return STATUS_PENDING
;
1090 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1092 FIXME("Returning error because of missing capabilities\n");
1093 status
= STATUS_INVALID_PARAMETER
;
1096 stop_waiting(commio
->hDevice
);
1097 RtlFreeHeap(GetProcessHeap(), 0, commio
);
1101 static NTSTATUS
xmit_immediate(HANDLE hDevice
, int fd
, const char* ptr
)
1103 /* FIXME: not perfect as it should bypass the in-queue */
1104 WARN("(%p,'%c') not perfect!\n", hDevice
, *ptr
);
1105 if (write(fd
, ptr
, 1) != 1)
1106 return FILE_GetNtStatus();
1107 return STATUS_SUCCESS
;
1110 /******************************************************************
1111 * COMM_DeviceIoControl
1115 static inline NTSTATUS
io_control(HANDLE hDevice
,
1116 HANDLE hEvent
, PIO_APC_ROUTINE UserApcRoutine
,
1117 PVOID UserApcContext
,
1118 PIO_STATUS_BLOCK piosb
,
1119 ULONG dwIoControlCode
,
1120 LPVOID lpInBuffer
, DWORD nInBufferSize
,
1121 LPVOID lpOutBuffer
, DWORD nOutBufferSize
)
1123 DWORD sz
= 0, access
= FILE_READ_DATA
;
1124 NTSTATUS status
= STATUS_SUCCESS
;
1125 int fd
= -1, needs_close
= 0;
1126 enum server_fd_type type
;
1128 TRACE("%p %s %p %d %p %d %p\n",
1129 hDevice
, iocode2str(dwIoControlCode
), lpInBuffer
, nInBufferSize
,
1130 lpOutBuffer
, nOutBufferSize
, piosb
);
1132 switch (dwIoControlCode
)
1134 case IOCTL_SERIAL_GET_TIMEOUTS
:
1135 case IOCTL_SERIAL_SET_TIMEOUTS
:
1136 case IOCTL_SERIAL_GET_WAIT_MASK
:
1137 case IOCTL_SERIAL_SET_WAIT_MASK
:
1138 /* these are handled on the server side */
1139 return STATUS_NOT_SUPPORTED
;
1142 piosb
->Information
= 0;
1144 if ((status
= server_get_unix_fd( hDevice
, access
, &fd
, &needs_close
, &type
, NULL
)))
1146 if (type
!= FD_TYPE_SERIAL
)
1148 if (needs_close
) close( fd
);
1149 status
= STATUS_OBJECT_TYPE_MISMATCH
;
1153 switch (dwIoControlCode
)
1155 case IOCTL_SERIAL_CLR_DTR
:
1157 if (whack_modem(fd
, ~TIOCM_DTR
, 0) == -1) status
= FILE_GetNtStatus();
1159 status
= STATUS_NOT_SUPPORTED
;
1162 case IOCTL_SERIAL_CLR_RTS
:
1164 if (whack_modem(fd
, ~TIOCM_RTS
, 0) == -1) status
= FILE_GetNtStatus();
1166 status
= STATUS_NOT_SUPPORTED
;
1169 case IOCTL_SERIAL_GET_BAUD_RATE
:
1170 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_BAUD_RATE
))
1172 if (!(status
= get_baud_rate(fd
, lpOutBuffer
)))
1173 sz
= sizeof(SERIAL_BAUD_RATE
);
1176 status
= STATUS_INVALID_PARAMETER
;
1178 case IOCTL_SERIAL_GET_CHARS
:
1179 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_CHARS
))
1181 if (!(status
= get_special_chars(fd
, lpOutBuffer
)))
1182 sz
= sizeof(SERIAL_CHARS
);
1185 status
= STATUS_INVALID_PARAMETER
;
1187 case IOCTL_SERIAL_GET_COMMSTATUS
:
1188 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_STATUS
))
1190 if (!(status
= get_status(fd
, lpOutBuffer
)))
1191 sz
= sizeof(SERIAL_STATUS
);
1193 else status
= STATUS_INVALID_PARAMETER
;
1195 case IOCTL_SERIAL_GET_HANDFLOW
:
1196 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_HANDFLOW
))
1198 if (!(status
= get_hand_flow(fd
, lpOutBuffer
)))
1199 sz
= sizeof(SERIAL_HANDFLOW
);
1202 status
= STATUS_INVALID_PARAMETER
;
1204 case IOCTL_SERIAL_GET_LINE_CONTROL
:
1205 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_LINE_CONTROL
))
1207 if (!(status
= get_line_control(fd
, lpOutBuffer
)))
1208 sz
= sizeof(SERIAL_LINE_CONTROL
);
1211 status
= STATUS_INVALID_PARAMETER
;
1213 case IOCTL_SERIAL_GET_MODEMSTATUS
:
1214 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
1216 if (!(status
= get_modem_status(fd
, lpOutBuffer
)))
1219 else status
= STATUS_INVALID_PARAMETER
;
1221 case IOCTL_SERIAL_GET_PROPERTIES
:
1222 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_COMMPROP
))
1224 if (!(status
= get_properties(fd
, lpOutBuffer
)))
1225 sz
= sizeof(SERIAL_COMMPROP
);
1227 else status
= STATUS_INVALID_PARAMETER
;
1229 case IOCTL_SERIAL_IMMEDIATE_CHAR
:
1230 if (lpInBuffer
&& nInBufferSize
== sizeof(CHAR
))
1231 status
= xmit_immediate(hDevice
, fd
, lpInBuffer
);
1233 status
= STATUS_INVALID_PARAMETER
;
1235 case IOCTL_SERIAL_PURGE
:
1236 if (lpInBuffer
&& nInBufferSize
== sizeof(DWORD
))
1237 status
= purge(fd
, *(DWORD
*)lpInBuffer
);
1239 status
= STATUS_INVALID_PARAMETER
;
1241 case IOCTL_SERIAL_RESET_DEVICE
:
1242 FIXME("Unsupported\n");
1244 case IOCTL_SERIAL_SET_BAUD_RATE
:
1245 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_BAUD_RATE
))
1246 status
= set_baud_rate(fd
, lpInBuffer
);
1248 status
= STATUS_INVALID_PARAMETER
;
1250 case IOCTL_SERIAL_SET_BREAK_OFF
:
1251 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1252 if (ioctl(fd
, TIOCCBRK
, 0) == -1)
1254 TRACE("ioctl failed\n");
1255 status
= FILE_GetNtStatus();
1258 FIXME("ioctl not available\n");
1259 status
= STATUS_NOT_SUPPORTED
;
1262 case IOCTL_SERIAL_SET_BREAK_ON
:
1263 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1264 if (ioctl(fd
, TIOCSBRK
, 0) == -1)
1266 TRACE("ioctl failed\n");
1267 status
= FILE_GetNtStatus();
1270 FIXME("ioctl not available\n");
1271 status
= STATUS_NOT_SUPPORTED
;
1274 case IOCTL_SERIAL_SET_CHARS
:
1275 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_CHARS
))
1276 status
= set_special_chars(fd
, lpInBuffer
);
1278 status
= STATUS_INVALID_PARAMETER
;
1280 case IOCTL_SERIAL_SET_DTR
:
1282 if (whack_modem(fd
, 0, TIOCM_DTR
) == -1) status
= FILE_GetNtStatus();
1284 status
= STATUS_NOT_SUPPORTED
;
1287 case IOCTL_SERIAL_SET_HANDFLOW
:
1288 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_HANDFLOW
))
1289 status
= set_handflow(fd
, lpInBuffer
);
1291 status
= STATUS_INVALID_PARAMETER
;
1293 case IOCTL_SERIAL_SET_LINE_CONTROL
:
1294 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_LINE_CONTROL
))
1295 status
= set_line_control(fd
, lpInBuffer
);
1297 status
= STATUS_INVALID_PARAMETER
;
1299 case IOCTL_SERIAL_SET_QUEUE_SIZE
:
1300 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_QUEUE_SIZE
))
1301 status
= set_queue_size(fd
, lpInBuffer
);
1303 status
= STATUS_INVALID_PARAMETER
;
1305 case IOCTL_SERIAL_SET_RTS
:
1307 if (whack_modem(fd
, 0, TIOCM_RTS
) == -1) status
= FILE_GetNtStatus();
1309 status
= STATUS_NOT_SUPPORTED
;
1312 case IOCTL_SERIAL_SET_XOFF
:
1313 status
= set_XOff(fd
);
1315 case IOCTL_SERIAL_SET_XON
:
1316 status
= set_XOn(fd
);
1318 case IOCTL_SERIAL_WAIT_ON_MASK
:
1319 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
1321 if (!(status
= wait_on(hDevice
, fd
, hEvent
, piosb
, lpOutBuffer
)))
1325 status
= STATUS_INVALID_PARAMETER
;
1328 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1329 dwIoControlCode
, dwIoControlCode
>> 16, (dwIoControlCode
>> 14) & 3,
1330 (dwIoControlCode
>> 2) & 0xFFF, dwIoControlCode
& 3);
1332 status
= STATUS_INVALID_PARAMETER
;
1335 if (needs_close
) close( fd
);
1337 piosb
->u
.Status
= status
;
1338 piosb
->Information
= sz
;
1339 if (hEvent
&& status
!= STATUS_PENDING
) NtSetEvent(hEvent
, NULL
);
1343 NTSTATUS
COMM_DeviceIoControl(HANDLE hDevice
,
1344 HANDLE hEvent
, PIO_APC_ROUTINE UserApcRoutine
,
1345 PVOID UserApcContext
,
1346 PIO_STATUS_BLOCK piosb
,
1347 ULONG dwIoControlCode
,
1348 LPVOID lpInBuffer
, DWORD nInBufferSize
,
1349 LPVOID lpOutBuffer
, DWORD nOutBufferSize
)
1353 if (dwIoControlCode
== IOCTL_SERIAL_WAIT_ON_MASK
)
1355 HANDLE hev
= hEvent
;
1357 /* this is an ioctl we implement in a non blocking way if hEvent is not
1359 * so we have to explicitly wait if no hEvent is provided
1363 OBJECT_ATTRIBUTES attr
;
1365 attr
.Length
= sizeof(attr
);
1366 attr
.RootDirectory
= 0;
1367 attr
.ObjectName
= NULL
;
1368 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
;
1369 attr
.SecurityDescriptor
= NULL
;
1370 attr
.SecurityQualityOfService
= NULL
;
1371 status
= NtCreateEvent(&hev
, EVENT_ALL_ACCESS
, &attr
, SynchronizationEvent
, FALSE
);
1373 if (status
) return status
;
1375 status
= io_control(hDevice
, hev
, UserApcRoutine
, UserApcContext
,
1376 piosb
, dwIoControlCode
, lpInBuffer
, nInBufferSize
,
1377 lpOutBuffer
, nOutBufferSize
);
1380 if (status
== STATUS_PENDING
)
1382 NtWaitForSingleObject(hev
, FALSE
, NULL
);
1383 status
= STATUS_SUCCESS
;
1388 else status
= io_control(hDevice
, hEvent
, UserApcRoutine
, UserApcContext
,
1389 piosb
, dwIoControlCode
, lpInBuffer
, nInBufferSize
,
1390 lpOutBuffer
, nOutBufferSize
);
1394 NTSTATUS
COMM_FlushBuffersFile( int fd
)
1397 while (tcdrain( fd
) == -1)
1399 if (errno
!= EINTR
) return FILE_GetNtStatus();
1401 return STATUS_SUCCESS
;
1402 #elif defined(TIOCDRAIN)
1403 while (ioctl( fd
, TIOCDRAIN
) == -1)
1405 if (errno
!= EINTR
) return FILE_GetNtStatus();
1407 return STATUS_SUCCESS
;
1408 #elif defined(TCSBRK)
1409 while (ioctl( fd
, TCSBRK
, 1 ) == -1)
1411 if (errno
!= EINTR
) return FILE_GetNtStatus();
1413 return STATUS_SUCCESS
;
1415 ERR( "not supported\n" );
1416 return STATUS_NOT_IMPLEMENTED
;