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>
61 #define WIN32_NO_STATUS
62 #define NONAMELESSUNION
66 #include "ddk/ntddser.h"
67 #include "ntdll_misc.h"
68 #include "wine/server.h"
69 #include "wine/library.h"
70 #include "wine/debug.h"
72 #ifdef HAVE_LINUX_SERIAL_H
73 #ifdef HAVE_ASM_TYPES_H
74 #include <asm/types.h>
76 #include <linux/serial.h>
79 #if !defined(TIOCINQ) && defined(FIONREAD)
80 #define TIOCINQ FIONREAD
83 WINE_DEFAULT_DEBUG_CHANNEL(comm
);
85 static const char* iocode2str(DWORD ioc
)
89 #define X(x) case (x): return #x
90 X(IOCTL_SERIAL_CLEAR_STATS
);
91 X(IOCTL_SERIAL_CLR_DTR
);
92 X(IOCTL_SERIAL_CLR_RTS
);
93 X(IOCTL_SERIAL_CONFIG_SIZE
);
94 X(IOCTL_SERIAL_GET_BAUD_RATE
);
95 X(IOCTL_SERIAL_GET_CHARS
);
96 X(IOCTL_SERIAL_GET_COMMSTATUS
);
97 X(IOCTL_SERIAL_GET_DTRRTS
);
98 X(IOCTL_SERIAL_GET_HANDFLOW
);
99 X(IOCTL_SERIAL_GET_LINE_CONTROL
);
100 X(IOCTL_SERIAL_GET_MODEM_CONTROL
);
101 X(IOCTL_SERIAL_GET_MODEMSTATUS
);
102 X(IOCTL_SERIAL_GET_PROPERTIES
);
103 X(IOCTL_SERIAL_GET_STATS
);
104 X(IOCTL_SERIAL_GET_TIMEOUTS
);
105 X(IOCTL_SERIAL_GET_WAIT_MASK
);
106 X(IOCTL_SERIAL_IMMEDIATE_CHAR
);
107 X(IOCTL_SERIAL_LSRMST_INSERT
);
108 X(IOCTL_SERIAL_PURGE
);
109 X(IOCTL_SERIAL_RESET_DEVICE
);
110 X(IOCTL_SERIAL_SET_BAUD_RATE
);
111 X(IOCTL_SERIAL_SET_BREAK_ON
);
112 X(IOCTL_SERIAL_SET_BREAK_OFF
);
113 X(IOCTL_SERIAL_SET_CHARS
);
114 X(IOCTL_SERIAL_SET_DTR
);
115 X(IOCTL_SERIAL_SET_FIFO_CONTROL
);
116 X(IOCTL_SERIAL_SET_HANDFLOW
);
117 X(IOCTL_SERIAL_SET_LINE_CONTROL
);
118 X(IOCTL_SERIAL_SET_MODEM_CONTROL
);
119 X(IOCTL_SERIAL_SET_QUEUE_SIZE
);
120 X(IOCTL_SERIAL_SET_RTS
);
121 X(IOCTL_SERIAL_SET_TIMEOUTS
);
122 X(IOCTL_SERIAL_SET_WAIT_MASK
);
123 X(IOCTL_SERIAL_SET_XOFF
);
124 X(IOCTL_SERIAL_SET_XON
);
125 X(IOCTL_SERIAL_WAIT_ON_MASK
);
126 X(IOCTL_SERIAL_XOFF_COUNTER
);
128 default: { static char tmp
[32]; sprintf(tmp
, "IOCTL_SERIAL_%d\n", ioc
); return tmp
; }
132 static NTSTATUS
get_baud_rate(int fd
, SERIAL_BAUD_RATE
* sbr
)
137 if (tcgetattr(fd
, &port
) == -1)
139 ERR("tcgetattr error '%s'\n", strerror(errno
));
140 return FILE_GetNtStatus();
142 speed
= cfgetospeed(&port
);
145 case B0
: sbr
->BaudRate
= 0; break;
146 case B50
: sbr
->BaudRate
= 50; break;
147 case B75
: sbr
->BaudRate
= 75; break;
148 case B110
: sbr
->BaudRate
= 110; break;
149 case B134
: sbr
->BaudRate
= 134; break;
150 case B150
: sbr
->BaudRate
= 150; break;
151 case B200
: sbr
->BaudRate
= 200; break;
152 case B300
: sbr
->BaudRate
= 300; break;
153 case B600
: sbr
->BaudRate
= 600; break;
154 case B1200
: sbr
->BaudRate
= 1200; break;
155 case B1800
: sbr
->BaudRate
= 1800; break;
156 case B2400
: sbr
->BaudRate
= 2400; break;
157 case B4800
: sbr
->BaudRate
= 4800; break;
158 case B9600
: sbr
->BaudRate
= 9600; break;
159 case B19200
: sbr
->BaudRate
= 19200; break;
160 case B38400
: sbr
->BaudRate
= 38400; break;
162 case B57600
: sbr
->BaudRate
= 57600; break;
165 case B115200
: sbr
->BaudRate
= 115200; break;
168 case B230400
: sbr
->BaudRate
= 230400; break;
171 case B460800
: sbr
->BaudRate
= 460800; break;
174 case B500000
: sbr
->BaudRate
= 500000; break;
177 case B921600
: sbr
->BaudRate
= 921600; break;
180 case B1000000
: sbr
->BaudRate
= 1000000; break;
183 case B1152000
: sbr
->BaudRate
= 1152000; break;
186 case B1500000
: sbr
->BaudRate
= 1500000; break;
189 case B2000000
: sbr
->BaudRate
= 2000000; break;
192 case B2500000
: sbr
->BaudRate
= 2500000; break;
195 case B3000000
: sbr
->BaudRate
= 3000000; break;
198 case B3500000
: sbr
->BaudRate
= 3500000; break;
201 case B4000000
: sbr
->BaudRate
= 4000000; break;
204 ERR("unknown speed %x\n", speed
);
205 return STATUS_INVALID_PARAMETER
;
207 return STATUS_SUCCESS
;
210 static NTSTATUS
get_hand_flow(int fd
, SERIAL_HANDFLOW
* shf
)
215 if (tcgetattr(fd
, &port
) == -1)
217 ERR("tcgetattr error '%s'\n", strerror(errno
));
218 return FILE_GetNtStatus();
220 /* termios does not support DTR/DSR flow control */
221 shf
->ControlHandShake
= 0;
222 shf
->FlowReplace
= 0;
224 if (ioctl(fd
, TIOCMGET
, &stat
) == -1)
226 WARN("ioctl error '%s'\n", strerror(errno
));
227 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
228 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
231 WARN("Setting DTR/RTS to enabled by default\n");
232 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
233 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
236 if (stat
& TIOCM_DTR
)
238 shf
->ControlHandShake
|= SERIAL_DTR_CONTROL
;
240 if (port
.c_cflag
& CRTSCTS
)
242 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
243 shf
->ControlHandShake
|= SERIAL_CTS_HANDSHAKE
;
249 if (stat
& TIOCM_RTS
)
251 shf
->FlowReplace
|= SERIAL_RTS_CONTROL
;
253 if (port
.c_iflag
& IXOFF
)
254 shf
->FlowReplace
|= SERIAL_AUTO_RECEIVE
;
255 if (port
.c_iflag
& IXON
)
256 shf
->FlowReplace
|= SERIAL_AUTO_TRANSMIT
;
260 return STATUS_SUCCESS
;
263 static NTSTATUS
get_line_control(int fd
, SERIAL_LINE_CONTROL
* slc
)
267 if (tcgetattr(fd
, &port
) == -1)
269 ERR("tcgetattr error '%s'\n", strerror(errno
));
270 return FILE_GetNtStatus();
274 switch (port
.c_cflag
& (PARENB
| PARODD
| CMSPAR
))
276 switch (port
.c_cflag
& (PARENB
| PARODD
))
279 case 0: slc
->Parity
= NOPARITY
; break;
280 case PARENB
: slc
->Parity
= EVENPARITY
; break;
281 case PARENB
|PARODD
: slc
->Parity
= ODDPARITY
; break;
283 case PARENB
|CMSPAR
: slc
->Parity
= MARKPARITY
; break;
284 case PARENB
|PARODD
|CMSPAR
: slc
->Parity
= SPACEPARITY
; break;
287 switch (port
.c_cflag
& CSIZE
)
289 case CS5
: slc
->WordLength
= 5; break;
290 case CS6
: slc
->WordLength
= 6; break;
291 case CS7
: slc
->WordLength
= 7; break;
292 case CS8
: slc
->WordLength
= 8; break;
293 default: ERR("unknown size %x\n", (UINT
)(port
.c_cflag
& CSIZE
));
296 if (port
.c_cflag
& CSTOPB
)
298 if (slc
->WordLength
== 5)
299 slc
->StopBits
= ONE5STOPBITS
;
301 slc
->StopBits
= TWOSTOPBITS
;
304 slc
->StopBits
= ONESTOPBIT
;
306 return STATUS_SUCCESS
;
309 static NTSTATUS
get_modem_status(int fd
, DWORD
* lpModemStat
)
311 NTSTATUS status
= STATUS_NOT_SUPPORTED
;
316 if (!ioctl(fd
, TIOCMGET
, &mstat
))
319 if (mstat
& TIOCM_CTS
) *lpModemStat
|= MS_CTS_ON
;
322 if (mstat
& TIOCM_DSR
) *lpModemStat
|= MS_DSR_ON
;
325 if (mstat
& TIOCM_RNG
) *lpModemStat
|= MS_RING_ON
;
328 /* FIXME: Not really sure about RLSD UB 990810 */
329 if (mstat
& TIOCM_CAR
) *lpModemStat
|= MS_RLSD_ON
;
331 TRACE("%04x -> %s%s%s%s\n", mstat
,
332 (*lpModemStat
& MS_RLSD_ON
) ? "MS_RLSD_ON " : "",
333 (*lpModemStat
& MS_RING_ON
) ? "MS_RING_ON " : "",
334 (*lpModemStat
& MS_DSR_ON
) ? "MS_DSR_ON " : "",
335 (*lpModemStat
& MS_CTS_ON
) ? "MS_CTS_ON " : "");
336 return STATUS_SUCCESS
;
338 WARN("TIOCMGET err %s\n", strerror(errno
));
339 status
= FILE_GetNtStatus();
344 static NTSTATUS
get_special_chars(int fd
, SERIAL_CHARS
* sc
)
348 if (tcgetattr(fd
, &port
) == -1)
350 ERR("tcgetattr error '%s'\n", strerror(errno
));
351 return FILE_GetNtStatus();
353 sc
->EofChar
= port
.c_cc
[VEOF
];
354 sc
->ErrorChar
= 0xFF;
355 sc
->BreakChar
= 0; /* FIXME */
356 sc
->EventChar
= 0; /* FIXME */
357 sc
->XonChar
= port
.c_cc
[VSTART
];
358 sc
->XoffChar
= port
.c_cc
[VSTOP
];
360 return STATUS_SUCCESS
;
363 static NTSTATUS
get_status(int fd
, SERIAL_STATUS
* ss
)
365 NTSTATUS status
= STATUS_SUCCESS
;
369 ss
->EofReceived
= FALSE
;
370 ss
->WaitForImmediate
= FALSE
;
372 if (ioctl(fd
, TIOCOUTQ
, &ss
->AmountInOutQueue
) == -1)
374 WARN("ioctl returned error\n");
375 status
= FILE_GetNtStatus();
378 ss
->AmountInOutQueue
= 0; /* FIXME: find a different way to find out */
382 if (ioctl(fd
, TIOCINQ
, &ss
->AmountInInQueue
))
384 WARN("ioctl returned error\n");
385 status
= FILE_GetNtStatus();
388 ss
->AmountInInQueue
= 0; /* FIXME: find a different way to find out */
393 static NTSTATUS
get_timeouts(HANDLE handle
, SERIAL_TIMEOUTS
* st
)
396 SERVER_START_REQ( get_serial_info
)
398 req
->handle
= wine_server_obj_handle( handle
);
400 if (!(status
= wine_server_call( req
)))
402 st
->ReadIntervalTimeout
= reply
->readinterval
;
403 st
->ReadTotalTimeoutMultiplier
= reply
->readmult
;
404 st
->ReadTotalTimeoutConstant
= reply
->readconst
;
405 st
->WriteTotalTimeoutMultiplier
= reply
->writemult
;
406 st
->WriteTotalTimeoutConstant
= reply
->writeconst
;
413 static void stop_waiting( HANDLE handle
)
417 SERVER_START_REQ( set_serial_info
)
419 req
->handle
= wine_server_obj_handle( handle
);
420 req
->flags
= SERIALINFO_PENDING_WAIT
;
421 if ((status
= wine_server_call( req
)))
422 ERR("failed to clear waiting state: %#x\n", status
);
427 static NTSTATUS
get_wait_mask(HANDLE hDevice
, DWORD
*mask
, DWORD
*cookie
, DWORD
*pending_write
, BOOL start_wait
)
431 SERVER_START_REQ( get_serial_info
)
433 req
->handle
= wine_server_obj_handle( hDevice
);
434 req
->flags
= pending_write
? SERIALINFO_PENDING_WRITE
: 0;
435 if (start_wait
) req
->flags
|= SERIALINFO_PENDING_WAIT
;
436 if (!(status
= wine_server_call( req
)))
438 *mask
= reply
->eventmask
;
439 if (cookie
) *cookie
= reply
->cookie
;
440 if (pending_write
) *pending_write
= reply
->pending_write
;
447 static NTSTATUS
purge(int fd
, DWORD flags
)
450 ** not exactly sure how these are different
451 ** Perhaps if we had our own internal queues, one flushes them
452 ** and the other flushes the kernel's buffers.
454 if (flags
& PURGE_TXABORT
) tcflush(fd
, TCOFLUSH
);
455 if (flags
& PURGE_RXABORT
) tcflush(fd
, TCIFLUSH
);
456 if (flags
& PURGE_TXCLEAR
) tcflush(fd
, TCOFLUSH
);
457 if (flags
& PURGE_RXCLEAR
) tcflush(fd
, TCIFLUSH
);
458 return STATUS_SUCCESS
;
461 static NTSTATUS
set_baud_rate(int fd
, const SERIAL_BAUD_RATE
* sbr
)
465 if (tcgetattr(fd
, &port
) == -1)
467 ERR("tcgetattr error '%s'\n", strerror(errno
));
468 return FILE_GetNtStatus();
471 switch (sbr
->BaudRate
)
473 case 0: cfsetospeed( &port
, B0
); break;
474 case 50: cfsetospeed( &port
, B50
); break;
475 case 75: cfsetospeed( &port
, B75
); break;
477 case CBR_110
: cfsetospeed( &port
, B110
); break;
478 case 134: cfsetospeed( &port
, B134
); break;
479 case 150: cfsetospeed( &port
, B150
); break;
480 case 200: cfsetospeed( &port
, B200
); break;
482 case CBR_300
: cfsetospeed( &port
, B300
); break;
484 case CBR_600
: cfsetospeed( &port
, B600
); break;
486 case CBR_1200
: cfsetospeed( &port
, B1200
); break;
487 case 1800: cfsetospeed( &port
, B1800
); break;
489 case CBR_2400
: cfsetospeed( &port
, B2400
); break;
491 case CBR_4800
: cfsetospeed( &port
, B4800
); break;
493 case CBR_9600
: cfsetospeed( &port
, B9600
); break;
495 case CBR_19200
: cfsetospeed( &port
, B19200
); break;
497 case CBR_38400
: cfsetospeed( &port
, B38400
); break;
499 case 57600: cfsetospeed( &port
, B57600
); break;
502 case 115200: cfsetospeed( &port
, B115200
); break;
505 case 230400: cfsetospeed( &port
, B230400
); break;
508 case 460800: cfsetospeed( &port
, B460800
); break;
511 case 500000: cfsetospeed( &port
, B500000
); break;
514 case 921600: cfsetospeed( &port
, B921600
); break;
517 case 1000000: cfsetospeed( &port
, B1000000
); break;
520 case 1152000: cfsetospeed( &port
, B1152000
); break;
523 case 1500000: cfsetospeed( &port
, B1500000
); break;
526 case 2000000: cfsetospeed( &port
, B2000000
); break;
529 case 2500000: cfsetospeed( &port
, B2500000
); break;
532 case 3000000: cfsetospeed( &port
, B3000000
); break;
535 case 3500000: cfsetospeed( &port
, B3500000
); break;
538 case 4000000: cfsetospeed( &port
, B4000000
); break;
541 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
543 struct serial_struct nuts
;
546 ioctl(fd
, TIOCGSERIAL
, &nuts
);
547 nuts
.custom_divisor
= nuts
.baud_base
/ sbr
->BaudRate
;
548 if (!(nuts
.custom_divisor
)) nuts
.custom_divisor
= 1;
549 arby
= nuts
.baud_base
/ nuts
.custom_divisor
;
550 nuts
.flags
&= ~ASYNC_SPD_MASK
;
551 nuts
.flags
|= ASYNC_SPD_CUST
;
552 WARN("You (or a program acting at your behest) have specified\n"
553 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
554 "which is as close as we can get by our present understanding of your\n"
555 "hardware. I hope you know what you are doing. Any disruption Wine\n"
556 "has caused to your linux system can be undone with setserial\n"
557 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
558 "reset it and it will probably recover.\n", sbr
->BaudRate
, arby
);
559 ioctl(fd
, TIOCSSERIAL
, &nuts
);
560 cfsetospeed( &port
, B38400
);
563 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
564 ERR("baudrate %d\n", sbr
->BaudRate
);
565 return STATUS_NOT_SUPPORTED
;
566 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
568 cfsetispeed( &port
, cfgetospeed(&port
) );
569 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
571 ERR("tcsetattr error '%s'\n", strerror(errno
));
572 return FILE_GetNtStatus();
574 return STATUS_SUCCESS
;
577 static int whack_modem(int fd
, unsigned int andy
, unsigned int orrie
)
580 unsigned int mstat
, okay
;
581 okay
= ioctl(fd
, TIOCMGET
, &mstat
);
582 if (okay
) return okay
;
583 if (andy
) mstat
&= andy
;
585 return ioctl(fd
, TIOCMSET
, &mstat
);
591 static NTSTATUS
set_handflow(int fd
, const SERIAL_HANDFLOW
* shf
)
595 if ((shf
->FlowReplace
& (SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
)) ==
596 (SERIAL_RTS_CONTROL
| SERIAL_RTS_HANDSHAKE
))
597 return STATUS_NOT_SUPPORTED
;
599 if (tcgetattr(fd
, &port
) == -1)
601 ERR("tcgetattr error '%s'\n", strerror(errno
));
602 return FILE_GetNtStatus();
606 if ((shf
->ControlHandShake
& SERIAL_CTS_HANDSHAKE
) ||
607 (shf
->FlowReplace
& SERIAL_RTS_HANDSHAKE
))
609 port
.c_cflag
|= CRTSCTS
;
613 port
.c_cflag
&= ~CRTSCTS
;
616 if (shf
->ControlHandShake
& SERIAL_DTR_HANDSHAKE
)
618 WARN("DSR/DTR flow control not supported\n");
619 } else if (!(shf
->ControlHandShake
& SERIAL_DTR_CONTROL
))
620 whack_modem(fd
, ~TIOCM_DTR
, 0);
622 whack_modem(fd
, 0, TIOCM_DTR
);
625 if (!(shf
->ControlHandShake
& SERIAL_CTS_HANDSHAKE
))
627 if ((shf
->FlowReplace
& (SERIAL_RTS_CONTROL
|SERIAL_RTS_HANDSHAKE
)) == 0)
628 whack_modem(fd
, ~TIOCM_RTS
, 0);
630 whack_modem(fd
, 0, TIOCM_RTS
);
634 if (shf
->FlowReplace
& SERIAL_AUTO_RECEIVE
)
635 port
.c_iflag
|= IXOFF
;
637 port
.c_iflag
&= ~IXOFF
;
638 if (shf
->FlowReplace
& SERIAL_AUTO_TRANSMIT
)
639 port
.c_iflag
|= IXON
;
641 port
.c_iflag
&= ~IXON
;
642 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
644 ERR("tcsetattr error '%s'\n", strerror(errno
));
645 return FILE_GetNtStatus();
648 return STATUS_SUCCESS
;
651 static NTSTATUS
set_line_control(int fd
, const SERIAL_LINE_CONTROL
* slc
)
654 unsigned bytesize
, stopbits
;
656 if (tcgetattr(fd
, &port
) == -1)
658 ERR("tcgetattr error '%s'\n", strerror(errno
));
659 return FILE_GetNtStatus();
663 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|PARMRK
|IMAXBEL
);
665 port
.c_iflag
&= ~(ISTRIP
|BRKINT
|IGNCR
|ICRNL
|INLCR
|PARMRK
);
667 port
.c_iflag
|= IGNBRK
| INPCK
;
668 port
.c_oflag
&= ~(OPOST
);
669 port
.c_cflag
&= ~(HUPCL
);
670 port
.c_cflag
|= CLOCAL
| CREAD
;
673 * on FreeBSD, turning off ICANON does not disable IEXTEN,
674 * so we must turn it off explicitly. No harm done on Linux.
676 port
.c_lflag
&= ~(ICANON
|ECHO
|ISIG
|IEXTEN
);
677 port
.c_lflag
|= NOFLSH
;
679 bytesize
= slc
->WordLength
;
680 stopbits
= slc
->StopBits
;
683 port
.c_cflag
&= ~(PARENB
| PARODD
| CMSPAR
);
685 port
.c_cflag
&= ~(PARENB
| PARODD
);
688 /* make sure that reads don't block */
690 port
.c_cc
[VTIME
] = 0;
694 case NOPARITY
: port
.c_iflag
&= ~INPCK
; break;
695 case ODDPARITY
: port
.c_cflag
|= PARENB
| PARODD
; break;
696 case EVENPARITY
: port
.c_cflag
|= PARENB
; break;
698 /* Linux defines mark/space (stick) parity */
699 case MARKPARITY
: port
.c_cflag
|= PARENB
| CMSPAR
; break;
700 case SPACEPARITY
: port
.c_cflag
|= PARENB
| PARODD
| CMSPAR
; break;
702 /* try the POSIX way */
704 if (slc
->StopBits
== ONESTOPBIT
)
706 stopbits
= TWOSTOPBITS
;
707 port
.c_iflag
&= ~INPCK
;
711 FIXME("Cannot set MARK Parity\n");
712 return STATUS_NOT_SUPPORTED
;
716 if (slc
->WordLength
< 8)
719 port
.c_iflag
&= ~INPCK
;
723 FIXME("Cannot set SPACE Parity\n");
724 return STATUS_NOT_SUPPORTED
;
729 FIXME("Parity %d is not supported\n", slc
->Parity
);
730 return STATUS_NOT_SUPPORTED
;
733 port
.c_cflag
&= ~CSIZE
;
736 case 5: port
.c_cflag
|= CS5
; break;
737 case 6: port
.c_cflag
|= CS6
; break;
738 case 7: port
.c_cflag
|= CS7
; break;
739 case 8: port
.c_cflag
|= CS8
; break;
741 FIXME("ByteSize %d is not supported\n", bytesize
);
742 return STATUS_NOT_SUPPORTED
;
747 case ONESTOPBIT
: port
.c_cflag
&= ~CSTOPB
; break;
748 case ONE5STOPBITS
: /* will be selected if bytesize is 5 */
749 case TWOSTOPBITS
: port
.c_cflag
|= CSTOPB
; break;
751 FIXME("StopBits %d is not supported\n", stopbits
);
752 return STATUS_NOT_SUPPORTED
;
754 /* otherwise it hangs with pending input*/
755 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
757 ERR("tcsetattr error '%s'\n", strerror(errno
));
758 return FILE_GetNtStatus();
760 return STATUS_SUCCESS
;
763 static NTSTATUS
set_queue_size(int fd
, const SERIAL_QUEUE_SIZE
* sqs
)
765 FIXME("insize %d outsize %d unimplemented stub\n", sqs
->InSize
, sqs
->OutSize
);
766 return STATUS_SUCCESS
;
769 static NTSTATUS
set_special_chars(int fd
, const SERIAL_CHARS
* sc
)
773 if (tcgetattr(fd
, &port
) == -1)
775 ERR("tcgetattr error '%s'\n", strerror(errno
));
776 return FILE_GetNtStatus();
779 port
.c_cc
[VEOF
] = sc
->EofChar
;
780 /* FIXME: sc->ErrorChar is not supported */
781 /* FIXME: sc->BreakChar is not supported */
782 /* FIXME: sc->EventChar is not supported */
783 port
.c_cc
[VSTART
] = sc
->XonChar
;
784 port
.c_cc
[VSTOP
] = sc
->XoffChar
;
786 if (tcsetattr(fd
, TCSANOW
, &port
) == -1)
788 ERR("tcsetattr error '%s'\n", strerror(errno
));
789 return FILE_GetNtStatus();
791 return STATUS_SUCCESS
;
794 static NTSTATUS
set_timeouts(HANDLE handle
, const SERIAL_TIMEOUTS
* st
)
798 SERVER_START_REQ( set_serial_info
)
800 req
->handle
= wine_server_obj_handle( handle
);
801 req
->flags
= SERIALINFO_SET_TIMEOUTS
;
802 req
->readinterval
= st
->ReadIntervalTimeout
;
803 req
->readmult
= st
->ReadTotalTimeoutMultiplier
;
804 req
->readconst
= st
->ReadTotalTimeoutConstant
;
805 req
->writemult
= st
->WriteTotalTimeoutMultiplier
;
806 req
->writeconst
= st
->WriteTotalTimeoutConstant
;
807 status
= wine_server_call( req
);
813 static NTSTATUS
set_wait_mask(HANDLE hDevice
, DWORD mask
)
817 SERVER_START_REQ( set_serial_info
)
819 req
->handle
= wine_server_obj_handle( hDevice
);
820 req
->flags
= SERIALINFO_SET_MASK
;
821 req
->eventmask
= mask
;
822 status
= wine_server_call( req
);
829 * does not change IXOFF but simulates that IXOFF has been received:
831 static NTSTATUS
set_XOff(int fd
)
833 if (tcflow(fd
, TCOOFF
))
835 return FILE_GetNtStatus();
837 return STATUS_SUCCESS
;
841 * does not change IXON but simulates that IXON has been received:
843 static NTSTATUS
set_XOn(int fd
)
845 if (tcflow(fd
, TCOON
))
847 return FILE_GetNtStatus();
849 return STATUS_SUCCESS
;
853 * local structure holding the irq values we need for WaitCommEvent()
855 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
856 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
857 * no need to carry them in the internal structure
860 typedef struct serial_irq_info
862 int rx
, tx
, frame
, overrun
, parity
, brk
, buf_overrun
, temt
;
865 /***********************************************************************
866 * Data needed by the thread polling for the changing CommEvent
868 typedef struct async_commio
872 IO_STATUS_BLOCK
* iosb
;
878 serial_irq_info irq_info
;
881 /***********************************************************************
882 * Get extended interrupt count info, needed for wait_on
884 static NTSTATUS
get_irq_info(int fd
, serial_irq_info
*irq_info
)
888 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCGICOUNT)
889 struct serial_icounter_struct einfo
;
890 if (!ioctl(fd
, TIOCGICOUNT
, &einfo
))
892 irq_info
->rx
= einfo
.rx
;
893 irq_info
->tx
= einfo
.tx
;
894 irq_info
->frame
= einfo
.frame
;
895 irq_info
->overrun
= einfo
.overrun
;
896 irq_info
->parity
= einfo
.parity
;
897 irq_info
->brk
= einfo
.brk
;
898 irq_info
->buf_overrun
= einfo
.buf_overrun
;
902 TRACE("TIOCGICOUNT err %s\n", strerror(errno
));
903 memset(irq_info
,0, sizeof(serial_irq_info
));
906 memset(irq_info
,0, sizeof(serial_irq_info
));
910 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
911 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
912 if (!ioctl(fd
, TIOCSERGETLSR
, &out
))
914 irq_info
->temt
= (out
& TIOCSER_TEMT
) != 0;
915 return STATUS_SUCCESS
;
918 TRACE("TIOCSERGETLSR err %s\n", strerror(errno
));
920 #ifdef TIOCOUTQ /* otherwise we log when the out queue gets empty */
921 if (!ioctl(fd
, TIOCOUTQ
, &out
))
923 irq_info
->temt
= out
== 0;
924 return STATUS_SUCCESS
;
926 TRACE("TIOCOUTQ err %s\n", strerror(errno
));
927 return FILE_GetNtStatus();
929 return STATUS_SUCCESS
;
933 static DWORD
check_events(int fd
, DWORD mask
,
934 const serial_irq_info
*new,
935 const serial_irq_info
*old
,
936 DWORD new_mstat
, DWORD old_mstat
, DWORD pending_write
)
938 DWORD ret
= 0, queue
;
940 TRACE("mask 0x%08x\n", mask
);
941 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old
->rx
, new->rx
);
942 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old
->tx
, new->tx
);
943 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old
->frame
, new->frame
);
944 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old
->overrun
, new->overrun
);
945 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old
->parity
, new->parity
);
946 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old
->brk
, new->brk
);
947 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old
->buf_overrun
, new->buf_overrun
);
948 TRACE("old->temt 0x%08x vs. new->temt 0x%08x\n", old
->temt
, new->temt
);
950 if (old
->brk
!= new->brk
) ret
|= EV_BREAK
;
951 if ((old_mstat
& MS_CTS_ON
) != (new_mstat
& MS_CTS_ON
)) ret
|= EV_CTS
;
952 if ((old_mstat
& MS_DSR_ON
) != (new_mstat
& MS_DSR_ON
)) ret
|= EV_DSR
;
953 if ((old_mstat
& MS_RING_ON
) != (new_mstat
& MS_RING_ON
)) ret
|= EV_RING
;
954 if ((old_mstat
& MS_RLSD_ON
) != (new_mstat
& MS_RLSD_ON
)) ret
|= EV_RLSD
;
955 if (old
->frame
!= new->frame
|| old
->overrun
!= new->overrun
|| old
->parity
!= new->parity
) ret
|= EV_ERR
;
956 if (mask
& EV_RXCHAR
)
960 if (ioctl(fd
, TIOCINQ
, &queue
))
961 WARN("TIOCINQ returned error\n");
966 if (mask
& EV_TXEMPTY
)
968 if ((!old
->temt
|| pending_write
) && new->temt
)
974 /***********************************************************************
975 * wait_for_event (INTERNAL)
977 * We need to poll for what is interesting
978 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
981 static DWORD CALLBACK
wait_for_event(LPVOID arg
)
983 async_commio
*commio
= arg
;
986 if (!server_get_unix_fd( commio
->hDevice
, FILE_READ_DATA
| FILE_WRITE_DATA
, &fd
, &needs_close
, NULL
, NULL
))
988 serial_irq_info new_irq_info
;
989 DWORD new_mstat
, dummy
, cookie
;
992 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
993 commio
->hDevice
, fd
, commio
->evtmask
, commio
->events
, commio
->hEvent
, &commio
->irq_info
);
995 time
.QuadPart
= (ULONGLONG
)10000;
996 time
.QuadPart
= -time
.QuadPart
;
1000 * TIOCMIWAIT is not adequate
1003 * We don't handle the EV_RXFLAG (the eventchar)
1005 NtDelayExecution(FALSE
, &time
);
1006 get_irq_info(fd
, &new_irq_info
);
1007 if (get_modem_status(fd
, &new_mstat
))
1009 TRACE("get_modem_status failed\n");
1010 *commio
->events
= 0;
1013 *commio
->events
= check_events(fd
, commio
->evtmask
,
1014 &new_irq_info
, &commio
->irq_info
,
1015 new_mstat
, commio
->mstat
, commio
->pending_write
);
1016 if (*commio
->events
) break;
1017 get_wait_mask(commio
->hDevice
, &dummy
, &cookie
, (commio
->evtmask
& EV_TXEMPTY
) ? &commio
->pending_write
: NULL
, FALSE
);
1018 if (commio
->cookie
!= cookie
)
1020 *commio
->events
= 0;
1024 if (needs_close
) close( fd
);
1028 if (*commio
->events
)
1030 commio
->iosb
->u
.Status
= STATUS_SUCCESS
;
1031 commio
->iosb
->Information
= sizeof(DWORD
);
1034 commio
->iosb
->u
.Status
= STATUS_CANCELLED
;
1036 stop_waiting(commio
->hDevice
);
1037 if (commio
->hEvent
) NtSetEvent(commio
->hEvent
, NULL
);
1038 RtlFreeHeap(GetProcessHeap(), 0, commio
);
1042 static NTSTATUS
wait_on(HANDLE hDevice
, int fd
, HANDLE hEvent
, PIO_STATUS_BLOCK piosb
, DWORD
* events
)
1044 async_commio
* commio
;
1047 if ((status
= NtResetEvent(hEvent
, NULL
)))
1050 commio
= RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio
));
1051 if (!commio
) return STATUS_NO_MEMORY
;
1053 commio
->hDevice
= hDevice
;
1054 commio
->events
= events
;
1055 commio
->iosb
= piosb
;
1056 commio
->hEvent
= hEvent
;
1057 commio
->pending_write
= 0;
1058 status
= get_wait_mask(commio
->hDevice
, &commio
->evtmask
, &commio
->cookie
, (commio
->evtmask
& EV_TXEMPTY
) ? &commio
->pending_write
: NULL
, TRUE
);
1061 RtlFreeHeap(GetProcessHeap(), 0, commio
);
1065 /* We may never return, if some capabilities miss
1066 * Return error in that case
1068 #if !defined(TIOCINQ)
1069 if (commio
->evtmask
& EV_RXCHAR
)
1072 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1073 if (commio
->evtmask
& EV_TXEMPTY
)
1076 #if !defined(TIOCMGET)
1077 if (commio
->evtmask
& (EV_CTS
| EV_DSR
| EV_RING
| EV_RLSD
))
1080 #if !defined(TIOCM_CTS)
1081 if (commio
->evtmask
& EV_CTS
)
1084 #if !defined(TIOCM_DSR)
1085 if (commio
->evtmask
& EV_DSR
)
1088 #if !defined(TIOCM_RNG)
1089 if (commio
->evtmask
& EV_RING
)
1092 #if !defined(TIOCM_CAR)
1093 if (commio
->evtmask
& EV_RLSD
)
1096 if (commio
->evtmask
& EV_RXFLAG
)
1097 FIXME("EV_RXFLAG not handled\n");
1099 if ((status
= get_irq_info(fd
, &commio
->irq_info
)) &&
1100 (commio
->evtmask
& (EV_BREAK
| EV_ERR
)))
1103 if ((status
= get_modem_status(fd
, &commio
->mstat
)) &&
1104 (commio
->evtmask
& (EV_CTS
| EV_DSR
| EV_RING
| EV_RLSD
)))
1107 /* We might have received something or the TX buffer is delivered */
1108 *events
= check_events(fd
, commio
->evtmask
,
1109 &commio
->irq_info
, &commio
->irq_info
,
1110 commio
->mstat
, commio
->mstat
, commio
->pending_write
);
1113 status
= STATUS_SUCCESS
;
1117 /* create the worker for the task */
1118 status
= RtlQueueWorkItem(wait_for_event
, commio
, 0 /* FIXME */);
1119 if (status
!= STATUS_SUCCESS
) goto out_now
;
1120 return STATUS_PENDING
;
1122 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1124 FIXME("Returning error because of missing capabilities\n");
1125 status
= STATUS_INVALID_PARAMETER
;
1128 stop_waiting(commio
->hDevice
);
1129 RtlFreeHeap(GetProcessHeap(), 0, commio
);
1133 static NTSTATUS
xmit_immediate(HANDLE hDevice
, int fd
, const char* ptr
)
1135 /* FIXME: not perfect as it should bypass the in-queue */
1136 WARN("(%p,'%c') not perfect!\n", hDevice
, *ptr
);
1137 if (write(fd
, ptr
, 1) != 1)
1138 return FILE_GetNtStatus();
1139 return STATUS_SUCCESS
;
1142 /******************************************************************
1143 * COMM_DeviceIoControl
1147 static inline NTSTATUS
io_control(HANDLE hDevice
,
1148 HANDLE hEvent
, PIO_APC_ROUTINE UserApcRoutine
,
1149 PVOID UserApcContext
,
1150 PIO_STATUS_BLOCK piosb
,
1151 ULONG dwIoControlCode
,
1152 LPVOID lpInBuffer
, DWORD nInBufferSize
,
1153 LPVOID lpOutBuffer
, DWORD nOutBufferSize
)
1155 DWORD sz
= 0, access
= FILE_READ_DATA
;
1156 NTSTATUS status
= STATUS_SUCCESS
;
1157 int fd
= -1, needs_close
= 0;
1159 TRACE("%p %s %p %d %p %d %p\n",
1160 hDevice
, iocode2str(dwIoControlCode
), lpInBuffer
, nInBufferSize
,
1161 lpOutBuffer
, nOutBufferSize
, piosb
);
1163 piosb
->Information
= 0;
1165 if (dwIoControlCode
!= IOCTL_SERIAL_GET_TIMEOUTS
&&
1166 dwIoControlCode
!= IOCTL_SERIAL_SET_TIMEOUTS
)
1168 enum server_fd_type type
;
1169 if ((status
= server_get_unix_fd( hDevice
, access
, &fd
, &needs_close
, &type
, NULL
)))
1171 if (type
!= FD_TYPE_SERIAL
)
1173 if (needs_close
) close( fd
);
1174 status
= STATUS_OBJECT_TYPE_MISMATCH
;
1179 switch (dwIoControlCode
)
1181 case IOCTL_SERIAL_CLR_DTR
:
1183 if (whack_modem(fd
, ~TIOCM_DTR
, 0) == -1) status
= FILE_GetNtStatus();
1185 status
= STATUS_NOT_SUPPORTED
;
1188 case IOCTL_SERIAL_CLR_RTS
:
1190 if (whack_modem(fd
, ~TIOCM_RTS
, 0) == -1) status
= FILE_GetNtStatus();
1192 status
= STATUS_NOT_SUPPORTED
;
1195 case IOCTL_SERIAL_GET_BAUD_RATE
:
1196 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_BAUD_RATE
))
1198 if (!(status
= get_baud_rate(fd
, lpOutBuffer
)))
1199 sz
= sizeof(SERIAL_BAUD_RATE
);
1202 status
= STATUS_INVALID_PARAMETER
;
1204 case IOCTL_SERIAL_GET_CHARS
:
1205 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_CHARS
))
1207 if (!(status
= get_special_chars(fd
, lpOutBuffer
)))
1208 sz
= sizeof(SERIAL_CHARS
);
1211 status
= STATUS_INVALID_PARAMETER
;
1213 case IOCTL_SERIAL_GET_COMMSTATUS
:
1214 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_STATUS
))
1216 if (!(status
= get_status(fd
, lpOutBuffer
)))
1217 sz
= sizeof(SERIAL_STATUS
);
1219 else status
= STATUS_INVALID_PARAMETER
;
1221 case IOCTL_SERIAL_GET_HANDFLOW
:
1222 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_HANDFLOW
))
1224 if (!(status
= get_hand_flow(fd
, lpOutBuffer
)))
1225 sz
= sizeof(SERIAL_HANDFLOW
);
1228 status
= STATUS_INVALID_PARAMETER
;
1230 case IOCTL_SERIAL_GET_LINE_CONTROL
:
1231 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_LINE_CONTROL
))
1233 if (!(status
= get_line_control(fd
, lpOutBuffer
)))
1234 sz
= sizeof(SERIAL_LINE_CONTROL
);
1237 status
= STATUS_INVALID_PARAMETER
;
1239 case IOCTL_SERIAL_GET_MODEMSTATUS
:
1240 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
1242 if (!(status
= get_modem_status(fd
, lpOutBuffer
)))
1245 else status
= STATUS_INVALID_PARAMETER
;
1247 case IOCTL_SERIAL_GET_TIMEOUTS
:
1248 if (lpOutBuffer
&& nOutBufferSize
== sizeof(SERIAL_TIMEOUTS
))
1250 if (!(status
= get_timeouts(hDevice
, lpOutBuffer
)))
1251 sz
= sizeof(SERIAL_TIMEOUTS
);
1254 status
= STATUS_INVALID_PARAMETER
;
1256 case IOCTL_SERIAL_GET_WAIT_MASK
:
1257 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
1259 if (!(status
= get_wait_mask(hDevice
, lpOutBuffer
, NULL
, NULL
, FALSE
)))
1263 status
= STATUS_INVALID_PARAMETER
;
1265 case IOCTL_SERIAL_IMMEDIATE_CHAR
:
1266 if (lpInBuffer
&& nInBufferSize
== sizeof(CHAR
))
1267 status
= xmit_immediate(hDevice
, fd
, lpInBuffer
);
1269 status
= STATUS_INVALID_PARAMETER
;
1271 case IOCTL_SERIAL_PURGE
:
1272 if (lpInBuffer
&& nInBufferSize
== sizeof(DWORD
))
1273 status
= purge(fd
, *(DWORD
*)lpInBuffer
);
1275 status
= STATUS_INVALID_PARAMETER
;
1277 case IOCTL_SERIAL_RESET_DEVICE
:
1278 FIXME("Unsupported\n");
1280 case IOCTL_SERIAL_SET_BAUD_RATE
:
1281 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_BAUD_RATE
))
1282 status
= set_baud_rate(fd
, lpInBuffer
);
1284 status
= STATUS_INVALID_PARAMETER
;
1286 case IOCTL_SERIAL_SET_BREAK_OFF
:
1287 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1288 if (ioctl(fd
, TIOCCBRK
, 0) == -1)
1290 TRACE("ioctl failed\n");
1291 status
= FILE_GetNtStatus();
1294 FIXME("ioctl not available\n");
1295 status
= STATUS_NOT_SUPPORTED
;
1298 case IOCTL_SERIAL_SET_BREAK_ON
:
1299 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1300 if (ioctl(fd
, TIOCSBRK
, 0) == -1)
1302 TRACE("ioctl failed\n");
1303 status
= FILE_GetNtStatus();
1306 FIXME("ioctl not available\n");
1307 status
= STATUS_NOT_SUPPORTED
;
1310 case IOCTL_SERIAL_SET_CHARS
:
1311 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_CHARS
))
1312 status
= set_special_chars(fd
, lpInBuffer
);
1314 status
= STATUS_INVALID_PARAMETER
;
1316 case IOCTL_SERIAL_SET_DTR
:
1318 if (whack_modem(fd
, 0, TIOCM_DTR
) == -1) status
= FILE_GetNtStatus();
1320 status
= STATUS_NOT_SUPPORTED
;
1323 case IOCTL_SERIAL_SET_HANDFLOW
:
1324 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_HANDFLOW
))
1325 status
= set_handflow(fd
, lpInBuffer
);
1327 status
= STATUS_INVALID_PARAMETER
;
1329 case IOCTL_SERIAL_SET_LINE_CONTROL
:
1330 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_LINE_CONTROL
))
1331 status
= set_line_control(fd
, lpInBuffer
);
1333 status
= STATUS_INVALID_PARAMETER
;
1335 case IOCTL_SERIAL_SET_QUEUE_SIZE
:
1336 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_QUEUE_SIZE
))
1337 status
= set_queue_size(fd
, lpInBuffer
);
1339 status
= STATUS_INVALID_PARAMETER
;
1341 case IOCTL_SERIAL_SET_RTS
:
1343 if (whack_modem(fd
, 0, TIOCM_RTS
) == -1) status
= FILE_GetNtStatus();
1345 status
= STATUS_NOT_SUPPORTED
;
1348 case IOCTL_SERIAL_SET_TIMEOUTS
:
1349 if (lpInBuffer
&& nInBufferSize
== sizeof(SERIAL_TIMEOUTS
))
1350 status
= set_timeouts(hDevice
, lpInBuffer
);
1352 status
= STATUS_INVALID_PARAMETER
;
1354 case IOCTL_SERIAL_SET_WAIT_MASK
:
1355 if (lpInBuffer
&& nInBufferSize
== sizeof(DWORD
))
1357 status
= set_wait_mask(hDevice
, *(DWORD
*)lpInBuffer
);
1359 else status
= STATUS_INVALID_PARAMETER
;
1361 case IOCTL_SERIAL_SET_XOFF
:
1362 status
= set_XOff(fd
);
1364 case IOCTL_SERIAL_SET_XON
:
1365 status
= set_XOn(fd
);
1367 case IOCTL_SERIAL_WAIT_ON_MASK
:
1368 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
1370 if (!(status
= wait_on(hDevice
, fd
, hEvent
, piosb
, lpOutBuffer
)))
1374 status
= STATUS_INVALID_PARAMETER
;
1377 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1378 dwIoControlCode
, dwIoControlCode
>> 16, (dwIoControlCode
>> 14) & 3,
1379 (dwIoControlCode
>> 2) & 0xFFF, dwIoControlCode
& 3);
1381 status
= STATUS_INVALID_PARAMETER
;
1384 if (needs_close
) close( fd
);
1386 piosb
->u
.Status
= status
;
1387 piosb
->Information
= sz
;
1388 if (hEvent
&& status
!= STATUS_PENDING
) NtSetEvent(hEvent
, NULL
);
1392 NTSTATUS
COMM_DeviceIoControl(HANDLE hDevice
,
1393 HANDLE hEvent
, PIO_APC_ROUTINE UserApcRoutine
,
1394 PVOID UserApcContext
,
1395 PIO_STATUS_BLOCK piosb
,
1396 ULONG dwIoControlCode
,
1397 LPVOID lpInBuffer
, DWORD nInBufferSize
,
1398 LPVOID lpOutBuffer
, DWORD nOutBufferSize
)
1402 if (dwIoControlCode
== IOCTL_SERIAL_WAIT_ON_MASK
)
1404 HANDLE hev
= hEvent
;
1406 /* this is an ioctl we implement in a non blocking way if hEvent is not
1408 * so we have to explicitly wait if no hEvent is provided
1412 OBJECT_ATTRIBUTES attr
;
1414 attr
.Length
= sizeof(attr
);
1415 attr
.RootDirectory
= 0;
1416 attr
.ObjectName
= NULL
;
1417 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
;
1418 attr
.SecurityDescriptor
= NULL
;
1419 attr
.SecurityQualityOfService
= NULL
;
1420 status
= NtCreateEvent(&hev
, EVENT_ALL_ACCESS
, &attr
, SynchronizationEvent
, FALSE
);
1422 if (status
) return status
;
1424 status
= io_control(hDevice
, hev
, UserApcRoutine
, UserApcContext
,
1425 piosb
, dwIoControlCode
, lpInBuffer
, nInBufferSize
,
1426 lpOutBuffer
, nOutBufferSize
);
1429 if (status
== STATUS_PENDING
)
1431 NtWaitForSingleObject(hev
, FALSE
, NULL
);
1432 status
= STATUS_SUCCESS
;
1437 else status
= io_control(hDevice
, hEvent
, UserApcRoutine
, UserApcContext
,
1438 piosb
, dwIoControlCode
, lpInBuffer
, nInBufferSize
,
1439 lpOutBuffer
, nOutBufferSize
);
1443 NTSTATUS
COMM_FlushBuffersFile( int fd
)
1446 while (tcdrain( fd
) == -1)
1448 if (errno
!= EINTR
) return FILE_GetNtStatus();
1450 return STATUS_SUCCESS
;
1451 #elif defined(TIOCDRAIN)
1452 while (ioctl( fd
, TIOCDRAIN
) == -1)
1454 if (errno
!= EINTR
) return FILE_GetNtStatus();
1456 return STATUS_SUCCESS
;
1457 #elif defined(TCSBRK)
1458 while (ioctl( fd
, TCSBRK
, 1 ) == -1)
1460 if (errno
!= EINTR
) return FILE_GetNtStatus();
1462 return STATUS_SUCCESS
;
1464 ERR( "not supported\n" );
1465 return STATUS_NOT_IMPLEMENTED
;