mshtml/tests: Added more ownerDocument property tests.
[wine.git] / dlls / ntdll / serial.c
blob29e9fafbca20824341cf2687abc34589a59ca12d
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <errno.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_TERMIOS_H
31 #include <termios.h>
32 #endif
33 #ifdef HAVE_IO_H
34 # include <io.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <fcntl.h>
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 #endif
43 #include <sys/types.h>
44 #ifdef HAVE_SYS_FILIO_H
45 # include <sys/filio.h>
46 #endif
47 #ifdef HAVE_SYS_IOCTL_H
48 #include <sys/ioctl.h>
49 #endif
50 #ifdef HAVE_SYS_POLL_H
51 # include <sys/poll.h>
52 #endif
53 #ifdef HAVE_SYS_MODEM_H
54 # include <sys/modem.h>
55 #endif
56 #ifdef HAVE_SYS_STRTIO_H
57 # include <sys/strtio.h>
58 #endif
60 #include "ntstatus.h"
61 #define WIN32_NO_STATUS
62 #define NONAMELESSUNION
63 #include "windef.h"
64 #include "winternl.h"
65 #include "winioctl.h"
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>
75 #endif
76 #include <linux/serial.h>
77 #endif
79 #if !defined(TIOCINQ) && defined(FIONREAD)
80 #define TIOCINQ FIONREAD
81 #endif
83 WINE_DEFAULT_DEBUG_CHANNEL(comm);
85 static const char* iocode2str(DWORD ioc)
87 switch (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);
127 #undef X
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)
134 struct termios port;
135 int speed;
137 if (tcgetattr(fd, &port) == -1)
139 ERR("tcgetattr error '%s'\n", strerror(errno));
140 return FILE_GetNtStatus();
142 speed = cfgetospeed(&port);
143 switch (speed)
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;
161 #ifdef B57600
162 case B57600: sbr->BaudRate = 57600; break;
163 #endif
164 #ifdef B115200
165 case B115200: sbr->BaudRate = 115200; break;
166 #endif
167 #ifdef B230400
168 case B230400: sbr->BaudRate = 230400; break;
169 #endif
170 #ifdef B460800
171 case B460800: sbr->BaudRate = 460800; break;
172 #endif
173 #ifdef B500000
174 case B500000: sbr->BaudRate = 500000; break;
175 #endif
176 #ifdef B921600
177 case B921600: sbr->BaudRate = 921600; break;
178 #endif
179 #ifdef B1000000
180 case B1000000: sbr->BaudRate = 1000000; break;
181 #endif
182 #ifdef B1152000
183 case B1152000: sbr->BaudRate = 1152000; break;
184 #endif
185 #ifdef B1500000
186 case B1500000: sbr->BaudRate = 1500000; break;
187 #endif
188 #ifdef B2000000
189 case B2000000: sbr->BaudRate = 2000000; break;
190 #endif
191 #ifdef B2500000
192 case B2500000: sbr->BaudRate = 2500000; break;
193 #endif
194 #ifdef B3000000
195 case B3000000: sbr->BaudRate = 3000000; break;
196 #endif
197 #ifdef B3500000
198 case B3500000: sbr->BaudRate = 3500000; break;
199 #endif
200 #ifdef B4000000
201 case B4000000: sbr->BaudRate = 4000000; break;
202 #endif
203 default:
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)
212 int stat = 0;
213 struct termios port;
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;
223 #ifdef TIOCMGET
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;
230 #else
231 WARN("Setting DTR/RTS to enabled by default\n");
232 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
233 shf->FlowReplace |= SERIAL_RTS_CONTROL;
234 #endif
235 #ifdef TIOCM_DTR
236 if (stat & TIOCM_DTR)
237 #endif
238 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
239 #ifdef CRTSCTS
240 if (port.c_cflag & CRTSCTS)
242 shf->FlowReplace |= SERIAL_RTS_CONTROL;
243 shf->ControlHandShake |= SERIAL_CTS_HANDSHAKE;
245 else
246 #endif
248 #ifdef TIOCM_RTS
249 if (stat & TIOCM_RTS)
250 #endif
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;
258 shf->XonLimit = 10;
259 shf->XoffLimit = 10;
260 return STATUS_SUCCESS;
263 static NTSTATUS get_line_control(int fd, SERIAL_LINE_CONTROL* slc)
265 struct termios port;
267 if (tcgetattr(fd, &port) == -1)
269 ERR("tcgetattr error '%s'\n", strerror(errno));
270 return FILE_GetNtStatus();
273 #ifdef CMSPAR
274 switch (port.c_cflag & (PARENB | PARODD | CMSPAR))
275 #else
276 switch (port.c_cflag & (PARENB | PARODD))
277 #endif
279 case 0: slc->Parity = NOPARITY; break;
280 case PARENB: slc->Parity = EVENPARITY; break;
281 case PARENB|PARODD: slc->Parity = ODDPARITY; break;
282 #ifdef CMSPAR
283 case PARENB|CMSPAR: slc->Parity = MARKPARITY; break;
284 case PARENB|PARODD|CMSPAR: slc->Parity = SPACEPARITY; break;
285 #endif
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;
300 else
301 slc->StopBits = TWOSTOPBITS;
303 else
304 slc->StopBits = ONESTOPBIT;
306 return STATUS_SUCCESS;
309 static NTSTATUS get_modem_status(int fd, DWORD* lpModemStat)
311 NTSTATUS status = STATUS_NOT_SUPPORTED;
312 int mstat;
314 *lpModemStat = 0;
315 #ifdef TIOCMGET
316 if (!ioctl(fd, TIOCMGET, &mstat))
318 #ifdef TIOCM_CTS
319 if (mstat & TIOCM_CTS) *lpModemStat |= MS_CTS_ON;
320 #endif
321 #ifdef TIOCM_DSR
322 if (mstat & TIOCM_DSR) *lpModemStat |= MS_DSR_ON;
323 #endif
324 #ifdef TIOCM_RNG
325 if (mstat & TIOCM_RNG) *lpModemStat |= MS_RING_ON;
326 #endif
327 #ifdef TIOCM_CAR
328 /* FIXME: Not really sure about RLSD UB 990810 */
329 if (mstat & TIOCM_CAR) *lpModemStat |= MS_RLSD_ON;
330 #endif
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();
340 #endif
341 return status;
344 static NTSTATUS get_special_chars(int fd, SERIAL_CHARS* sc)
346 struct termios port;
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;
367 ss->Errors = 0;
368 ss->HoldReasons = 0;
369 ss->EofReceived = FALSE;
370 ss->WaitForImmediate = FALSE;
371 #ifdef TIOCOUTQ
372 if (ioctl(fd, TIOCOUTQ, &ss->AmountInOutQueue) == -1)
374 WARN("ioctl returned error\n");
375 status = FILE_GetNtStatus();
377 #else
378 ss->AmountInOutQueue = 0; /* FIXME: find a different way to find out */
379 #endif
381 #ifdef TIOCINQ
382 if (ioctl(fd, TIOCINQ, &ss->AmountInInQueue))
384 WARN("ioctl returned error\n");
385 status = FILE_GetNtStatus();
387 #else
388 ss->AmountInInQueue = 0; /* FIXME: find a different way to find out */
389 #endif
390 return status;
393 static void stop_waiting( HANDLE handle )
395 NTSTATUS status;
397 SERVER_START_REQ( set_serial_info )
399 req->handle = wine_server_obj_handle( handle );
400 req->flags = SERIALINFO_PENDING_WAIT;
401 if ((status = wine_server_call( req )))
402 ERR("failed to clear waiting state: %#x\n", status);
404 SERVER_END_REQ;
407 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD *mask, DWORD *cookie, DWORD *pending_write, BOOL start_wait)
409 NTSTATUS status;
411 SERVER_START_REQ( get_serial_info )
413 req->handle = wine_server_obj_handle( hDevice );
414 req->flags = pending_write ? SERIALINFO_PENDING_WRITE : 0;
415 if (start_wait) req->flags |= SERIALINFO_PENDING_WAIT;
416 if (!(status = wine_server_call( req )))
418 *mask = reply->eventmask;
419 if (cookie) *cookie = reply->cookie;
420 if (pending_write) *pending_write = reply->pending_write;
423 SERVER_END_REQ;
424 return status;
427 static NTSTATUS purge(int fd, DWORD flags)
430 ** not exactly sure how these are different
431 ** Perhaps if we had our own internal queues, one flushes them
432 ** and the other flushes the kernel's buffers.
434 if (flags & PURGE_TXABORT) tcflush(fd, TCOFLUSH);
435 if (flags & PURGE_RXABORT) tcflush(fd, TCIFLUSH);
436 if (flags & PURGE_TXCLEAR) tcflush(fd, TCOFLUSH);
437 if (flags & PURGE_RXCLEAR) tcflush(fd, TCIFLUSH);
438 return STATUS_SUCCESS;
441 static NTSTATUS set_baud_rate(int fd, const SERIAL_BAUD_RATE* sbr)
443 struct termios port;
445 if (tcgetattr(fd, &port) == -1)
447 ERR("tcgetattr error '%s'\n", strerror(errno));
448 return FILE_GetNtStatus();
451 switch (sbr->BaudRate)
453 case 0: cfsetospeed( &port, B0 ); break;
454 case 50: cfsetospeed( &port, B50 ); break;
455 case 75: cfsetospeed( &port, B75 ); break;
456 case 110:
457 case CBR_110: cfsetospeed( &port, B110 ); break;
458 case 134: cfsetospeed( &port, B134 ); break;
459 case 150: cfsetospeed( &port, B150 ); break;
460 case 200: cfsetospeed( &port, B200 ); break;
461 case 300:
462 case CBR_300: cfsetospeed( &port, B300 ); break;
463 case 600:
464 case CBR_600: cfsetospeed( &port, B600 ); break;
465 case 1200:
466 case CBR_1200: cfsetospeed( &port, B1200 ); break;
467 case 1800: cfsetospeed( &port, B1800 ); break;
468 case 2400:
469 case CBR_2400: cfsetospeed( &port, B2400 ); break;
470 case 4800:
471 case CBR_4800: cfsetospeed( &port, B4800 ); break;
472 case 9600:
473 case CBR_9600: cfsetospeed( &port, B9600 ); break;
474 case 19200:
475 case CBR_19200: cfsetospeed( &port, B19200 ); break;
476 case 38400:
477 case CBR_38400: cfsetospeed( &port, B38400 ); break;
478 #ifdef B57600
479 case 57600: cfsetospeed( &port, B57600 ); break;
480 #endif
481 #ifdef B115200
482 case 115200: cfsetospeed( &port, B115200 ); break;
483 #endif
484 #ifdef B230400
485 case 230400: cfsetospeed( &port, B230400 ); break;
486 #endif
487 #ifdef B460800
488 case 460800: cfsetospeed( &port, B460800 ); break;
489 #endif
490 #ifdef B500000
491 case 500000: cfsetospeed( &port, B500000 ); break;
492 #endif
493 #ifdef B921600
494 case 921600: cfsetospeed( &port, B921600 ); break;
495 #endif
496 #ifdef B1000000
497 case 1000000: cfsetospeed( &port, B1000000 ); break;
498 #endif
499 #ifdef B1152000
500 case 1152000: cfsetospeed( &port, B1152000 ); break;
501 #endif
502 #ifdef B1500000
503 case 1500000: cfsetospeed( &port, B1500000 ); break;
504 #endif
505 #ifdef B2000000
506 case 2000000: cfsetospeed( &port, B2000000 ); break;
507 #endif
508 #ifdef B2500000
509 case 2500000: cfsetospeed( &port, B2500000 ); break;
510 #endif
511 #ifdef B3000000
512 case 3000000: cfsetospeed( &port, B3000000 ); break;
513 #endif
514 #ifdef B3500000
515 case 3500000: cfsetospeed( &port, B3500000 ); break;
516 #endif
517 #ifdef B4000000
518 case 4000000: cfsetospeed( &port, B4000000 ); break;
519 #endif
520 default:
521 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
523 struct serial_struct nuts;
524 int arby;
526 ioctl(fd, TIOCGSERIAL, &nuts);
527 nuts.custom_divisor = nuts.baud_base / sbr->BaudRate;
528 if (!(nuts.custom_divisor)) nuts.custom_divisor = 1;
529 arby = nuts.baud_base / nuts.custom_divisor;
530 nuts.flags &= ~ASYNC_SPD_MASK;
531 nuts.flags |= ASYNC_SPD_CUST;
532 WARN("You (or a program acting at your behest) have specified\n"
533 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
534 "which is as close as we can get by our present understanding of your\n"
535 "hardware. I hope you know what you are doing. Any disruption Wine\n"
536 "has caused to your linux system can be undone with setserial\n"
537 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
538 "reset it and it will probably recover.\n", sbr->BaudRate, arby);
539 ioctl(fd, TIOCSSERIAL, &nuts);
540 cfsetospeed( &port, B38400 );
542 break;
543 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
544 ERR("baudrate %d\n", sbr->BaudRate);
545 return STATUS_NOT_SUPPORTED;
546 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
548 cfsetispeed( &port, cfgetospeed(&port) );
549 if (tcsetattr(fd, TCSANOW, &port) == -1)
551 ERR("tcsetattr error '%s'\n", strerror(errno));
552 return FILE_GetNtStatus();
554 return STATUS_SUCCESS;
557 static int whack_modem(int fd, unsigned int andy, unsigned int orrie)
559 #ifdef TIOCMGET
560 unsigned int mstat, okay;
561 okay = ioctl(fd, TIOCMGET, &mstat);
562 if (okay) return okay;
563 if (andy) mstat &= andy;
564 mstat |= orrie;
565 return ioctl(fd, TIOCMSET, &mstat);
566 #else
567 return 0;
568 #endif
571 static NTSTATUS set_handflow(int fd, const SERIAL_HANDFLOW* shf)
573 struct termios port;
575 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE)) ==
576 (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
577 return STATUS_NOT_SUPPORTED;
579 if (tcgetattr(fd, &port) == -1)
581 ERR("tcgetattr error '%s'\n", strerror(errno));
582 return FILE_GetNtStatus();
585 #ifdef CRTSCTS
586 if ((shf->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
587 (shf->FlowReplace & SERIAL_RTS_HANDSHAKE))
589 port.c_cflag |= CRTSCTS;
590 TRACE("CRTSCTS\n");
592 else
593 port.c_cflag &= ~CRTSCTS;
594 #endif
595 #ifdef TIOCM_DTR
596 if (shf->ControlHandShake & SERIAL_DTR_HANDSHAKE)
598 WARN("DSR/DTR flow control not supported\n");
599 } else if (!(shf->ControlHandShake & SERIAL_DTR_CONTROL))
600 whack_modem(fd, ~TIOCM_DTR, 0);
601 else
602 whack_modem(fd, 0, TIOCM_DTR);
603 #endif
604 #ifdef TIOCM_RTS
605 if (!(shf->ControlHandShake & SERIAL_CTS_HANDSHAKE))
607 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL|SERIAL_RTS_HANDSHAKE)) == 0)
608 whack_modem(fd, ~TIOCM_RTS, 0);
609 else
610 whack_modem(fd, 0, TIOCM_RTS);
612 #endif
614 if (shf->FlowReplace & SERIAL_AUTO_RECEIVE)
615 port.c_iflag |= IXOFF;
616 else
617 port.c_iflag &= ~IXOFF;
618 if (shf->FlowReplace & SERIAL_AUTO_TRANSMIT)
619 port.c_iflag |= IXON;
620 else
621 port.c_iflag &= ~IXON;
622 if (tcsetattr(fd, TCSANOW, &port) == -1)
624 ERR("tcsetattr error '%s'\n", strerror(errno));
625 return FILE_GetNtStatus();
628 return STATUS_SUCCESS;
631 static NTSTATUS set_line_control(int fd, const SERIAL_LINE_CONTROL* slc)
633 struct termios port;
634 unsigned bytesize, stopbits;
636 if (tcgetattr(fd, &port) == -1)
638 ERR("tcgetattr error '%s'\n", strerror(errno));
639 return FILE_GetNtStatus();
642 #ifdef IMAXBEL
643 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK|IMAXBEL);
644 #else
645 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK);
646 #endif
647 port.c_iflag |= IGNBRK | INPCK;
648 port.c_oflag &= ~(OPOST);
649 port.c_cflag &= ~(HUPCL);
650 port.c_cflag |= CLOCAL | CREAD;
653 * on FreeBSD, turning off ICANON does not disable IEXTEN,
654 * so we must turn it off explicitly. No harm done on Linux.
656 port.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN);
657 port.c_lflag |= NOFLSH;
659 bytesize = slc->WordLength;
660 stopbits = slc->StopBits;
662 #ifdef CMSPAR
663 port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
664 #else
665 port.c_cflag &= ~(PARENB | PARODD);
666 #endif
668 /* make sure that reads don't block */
669 port.c_cc[VMIN] = 0;
670 port.c_cc[VTIME] = 0;
672 switch (slc->Parity)
674 case NOPARITY: port.c_iflag &= ~INPCK; break;
675 case ODDPARITY: port.c_cflag |= PARENB | PARODD; break;
676 case EVENPARITY: port.c_cflag |= PARENB; break;
677 #ifdef CMSPAR
678 /* Linux defines mark/space (stick) parity */
679 case MARKPARITY: port.c_cflag |= PARENB | CMSPAR; break;
680 case SPACEPARITY: port.c_cflag |= PARENB | PARODD | CMSPAR; break;
681 #else
682 /* try the POSIX way */
683 case MARKPARITY:
684 if (slc->StopBits == ONESTOPBIT)
686 stopbits = TWOSTOPBITS;
687 port.c_iflag &= ~INPCK;
689 else
691 FIXME("Cannot set MARK Parity\n");
692 return STATUS_NOT_SUPPORTED;
694 break;
695 case SPACEPARITY:
696 if (slc->WordLength < 8)
698 bytesize +=1;
699 port.c_iflag &= ~INPCK;
701 else
703 FIXME("Cannot set SPACE Parity\n");
704 return STATUS_NOT_SUPPORTED;
706 break;
707 #endif
708 default:
709 FIXME("Parity %d is not supported\n", slc->Parity);
710 return STATUS_NOT_SUPPORTED;
713 port.c_cflag &= ~CSIZE;
714 switch (bytesize)
716 case 5: port.c_cflag |= CS5; break;
717 case 6: port.c_cflag |= CS6; break;
718 case 7: port.c_cflag |= CS7; break;
719 case 8: port.c_cflag |= CS8; break;
720 default:
721 FIXME("ByteSize %d is not supported\n", bytesize);
722 return STATUS_NOT_SUPPORTED;
725 switch (stopbits)
727 case ONESTOPBIT: port.c_cflag &= ~CSTOPB; break;
728 case ONE5STOPBITS: /* will be selected if bytesize is 5 */
729 case TWOSTOPBITS: port.c_cflag |= CSTOPB; break;
730 default:
731 FIXME("StopBits %d is not supported\n", stopbits);
732 return STATUS_NOT_SUPPORTED;
734 /* otherwise it hangs with pending input*/
735 if (tcsetattr(fd, TCSANOW, &port) == -1)
737 ERR("tcsetattr error '%s'\n", strerror(errno));
738 return FILE_GetNtStatus();
740 return STATUS_SUCCESS;
743 static NTSTATUS set_queue_size(int fd, const SERIAL_QUEUE_SIZE* sqs)
745 FIXME("insize %d outsize %d unimplemented stub\n", sqs->InSize, sqs->OutSize);
746 return STATUS_SUCCESS;
749 static NTSTATUS set_special_chars(int fd, const SERIAL_CHARS* sc)
751 struct termios port;
753 if (tcgetattr(fd, &port) == -1)
755 ERR("tcgetattr error '%s'\n", strerror(errno));
756 return FILE_GetNtStatus();
759 port.c_cc[VEOF ] = sc->EofChar;
760 /* FIXME: sc->ErrorChar is not supported */
761 /* FIXME: sc->BreakChar is not supported */
762 /* FIXME: sc->EventChar is not supported */
763 port.c_cc[VSTART] = sc->XonChar;
764 port.c_cc[VSTOP ] = sc->XoffChar;
766 if (tcsetattr(fd, TCSANOW, &port) == -1)
768 ERR("tcsetattr error '%s'\n", strerror(errno));
769 return FILE_GetNtStatus();
771 return STATUS_SUCCESS;
775 * does not change IXOFF but simulates that IXOFF has been received:
777 static NTSTATUS set_XOff(int fd)
779 if (tcflow(fd, TCOOFF))
781 return FILE_GetNtStatus();
783 return STATUS_SUCCESS;
787 * does not change IXON but simulates that IXON has been received:
789 static NTSTATUS set_XOn(int fd)
791 if (tcflow(fd, TCOON))
793 return FILE_GetNtStatus();
795 return STATUS_SUCCESS;
798 /* serial_irq_info
799 * local structure holding the irq values we need for WaitCommEvent()
801 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
802 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
803 * no need to carry them in the internal structure
806 typedef struct serial_irq_info
808 int rx, tx, frame, overrun, parity, brk, buf_overrun, temt;
809 }serial_irq_info;
811 /***********************************************************************
812 * Data needed by the thread polling for the changing CommEvent
814 typedef struct async_commio
816 HANDLE hDevice;
817 DWORD* events;
818 IO_STATUS_BLOCK* iosb;
819 HANDLE hEvent;
820 DWORD evtmask;
821 DWORD cookie;
822 DWORD mstat;
823 DWORD pending_write;
824 serial_irq_info irq_info;
825 } async_commio;
827 /***********************************************************************
828 * Get extended interrupt count info, needed for wait_on
830 static NTSTATUS get_irq_info(int fd, serial_irq_info *irq_info)
832 int out;
834 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCGICOUNT)
835 struct serial_icounter_struct einfo;
836 if (!ioctl(fd, TIOCGICOUNT, &einfo))
838 irq_info->rx = einfo.rx;
839 irq_info->tx = einfo.tx;
840 irq_info->frame = einfo.frame;
841 irq_info->overrun = einfo.overrun;
842 irq_info->parity = einfo.parity;
843 irq_info->brk = einfo.brk;
844 irq_info->buf_overrun = einfo.buf_overrun;
846 else
848 TRACE("TIOCGICOUNT err %s\n", strerror(errno));
849 memset(irq_info,0, sizeof(serial_irq_info));
851 #else
852 memset(irq_info,0, sizeof(serial_irq_info));
853 #endif
855 irq_info->temt = 0;
856 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
857 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
858 if (!ioctl(fd, TIOCSERGETLSR, &out))
860 irq_info->temt = (out & TIOCSER_TEMT) != 0;
861 return STATUS_SUCCESS;
864 TRACE("TIOCSERGETLSR err %s\n", strerror(errno));
865 #endif
866 #ifdef TIOCOUTQ /* otherwise we log when the out queue gets empty */
867 if (!ioctl(fd, TIOCOUTQ, &out))
869 irq_info->temt = out == 0;
870 return STATUS_SUCCESS;
872 TRACE("TIOCOUTQ err %s\n", strerror(errno));
873 return FILE_GetNtStatus();
874 #endif
875 return STATUS_SUCCESS;
879 static DWORD check_events(int fd, DWORD mask,
880 const serial_irq_info *new,
881 const serial_irq_info *old,
882 DWORD new_mstat, DWORD old_mstat, DWORD pending_write)
884 DWORD ret = 0, queue;
886 TRACE("mask 0x%08x\n", mask);
887 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old->rx, new->rx);
888 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old->tx, new->tx);
889 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old->frame, new->frame);
890 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old->overrun, new->overrun);
891 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old->parity, new->parity);
892 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old->brk, new->brk);
893 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old->buf_overrun, new->buf_overrun);
894 TRACE("old->temt 0x%08x vs. new->temt 0x%08x\n", old->temt, new->temt);
896 if (old->brk != new->brk) ret |= EV_BREAK;
897 if ((old_mstat & MS_CTS_ON ) != (new_mstat & MS_CTS_ON )) ret |= EV_CTS;
898 if ((old_mstat & MS_DSR_ON ) != (new_mstat & MS_DSR_ON )) ret |= EV_DSR;
899 if ((old_mstat & MS_RING_ON) != (new_mstat & MS_RING_ON)) ret |= EV_RING;
900 if ((old_mstat & MS_RLSD_ON) != (new_mstat & MS_RLSD_ON)) ret |= EV_RLSD;
901 if (old->frame != new->frame || old->overrun != new->overrun || old->parity != new->parity) ret |= EV_ERR;
902 if (mask & EV_RXCHAR)
904 queue = 0;
905 #ifdef TIOCINQ
906 if (ioctl(fd, TIOCINQ, &queue))
907 WARN("TIOCINQ returned error\n");
908 #endif
909 if (queue)
910 ret |= EV_RXCHAR;
912 if (mask & EV_TXEMPTY)
914 if ((!old->temt || pending_write) && new->temt)
915 ret |= EV_TXEMPTY;
917 return ret & mask;
920 /***********************************************************************
921 * wait_for_event (INTERNAL)
923 * We need to poll for what is interesting
924 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
927 static DWORD CALLBACK wait_for_event(LPVOID arg)
929 async_commio *commio = arg;
930 int fd, needs_close;
932 if (!server_get_unix_fd( commio->hDevice, FILE_READ_DATA | FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL ))
934 serial_irq_info new_irq_info;
935 DWORD new_mstat, dummy, cookie;
936 LARGE_INTEGER time;
938 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
939 commio->hDevice, fd, commio->evtmask, commio->events, commio->hEvent, &commio->irq_info);
941 time.QuadPart = (ULONGLONG)10000;
942 time.QuadPart = -time.QuadPart;
943 for (;;)
946 * TIOCMIWAIT is not adequate
948 * FIXME:
949 * We don't handle the EV_RXFLAG (the eventchar)
951 NtDelayExecution(FALSE, &time);
952 get_irq_info(fd, &new_irq_info);
953 if (get_modem_status(fd, &new_mstat))
955 TRACE("get_modem_status failed\n");
956 *commio->events = 0;
957 break;
959 *commio->events = check_events(fd, commio->evtmask,
960 &new_irq_info, &commio->irq_info,
961 new_mstat, commio->mstat, commio->pending_write);
962 if (*commio->events) break;
963 get_wait_mask(commio->hDevice, &dummy, &cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, FALSE);
964 if (commio->cookie != cookie)
966 *commio->events = 0;
967 break;
970 if (needs_close) close( fd );
972 if (commio->iosb)
974 if (*commio->events)
976 commio->iosb->u.Status = STATUS_SUCCESS;
977 commio->iosb->Information = sizeof(DWORD);
979 else
980 commio->iosb->u.Status = STATUS_CANCELLED;
982 stop_waiting(commio->hDevice);
983 if (commio->hEvent) NtSetEvent(commio->hEvent, NULL);
984 RtlFreeHeap(GetProcessHeap(), 0, commio);
985 return 0;
988 static NTSTATUS wait_on(HANDLE hDevice, int fd, HANDLE hEvent, PIO_STATUS_BLOCK piosb, DWORD* events)
990 async_commio* commio;
991 NTSTATUS status;
993 if ((status = NtResetEvent(hEvent, NULL)))
994 return status;
996 commio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio));
997 if (!commio) return STATUS_NO_MEMORY;
999 commio->hDevice = hDevice;
1000 commio->events = events;
1001 commio->iosb = piosb;
1002 commio->hEvent = hEvent;
1003 commio->pending_write = 0;
1004 status = get_wait_mask(commio->hDevice, &commio->evtmask, &commio->cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, TRUE);
1005 if (status)
1007 RtlFreeHeap(GetProcessHeap(), 0, commio);
1008 return status;
1011 /* We may never return, if some capabilities miss
1012 * Return error in that case
1014 #if !defined(TIOCINQ)
1015 if (commio->evtmask & EV_RXCHAR)
1016 goto error_caps;
1017 #endif
1018 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1019 if (commio->evtmask & EV_TXEMPTY)
1020 goto error_caps;
1021 #endif
1022 #if !defined(TIOCMGET)
1023 if (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD))
1024 goto error_caps;
1025 #endif
1026 #if !defined(TIOCM_CTS)
1027 if (commio->evtmask & EV_CTS)
1028 goto error_caps;
1029 #endif
1030 #if !defined(TIOCM_DSR)
1031 if (commio->evtmask & EV_DSR)
1032 goto error_caps;
1033 #endif
1034 #if !defined(TIOCM_RNG)
1035 if (commio->evtmask & EV_RING)
1036 goto error_caps;
1037 #endif
1038 #if !defined(TIOCM_CAR)
1039 if (commio->evtmask & EV_RLSD)
1040 goto error_caps;
1041 #endif
1042 if (commio->evtmask & EV_RXFLAG)
1043 FIXME("EV_RXFLAG not handled\n");
1045 if ((status = get_irq_info(fd, &commio->irq_info)) &&
1046 (commio->evtmask & (EV_BREAK | EV_ERR)))
1047 goto out_now;
1049 if ((status = get_modem_status(fd, &commio->mstat)) &&
1050 (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD)))
1051 goto out_now;
1053 /* We might have received something or the TX buffer is delivered */
1054 *events = check_events(fd, commio->evtmask,
1055 &commio->irq_info, &commio->irq_info,
1056 commio->mstat, commio->mstat, commio->pending_write);
1057 if (*events)
1059 status = STATUS_SUCCESS;
1060 goto out_now;
1063 /* create the worker for the task */
1064 status = RtlQueueWorkItem(wait_for_event, commio, 0 /* FIXME */);
1065 if (status != STATUS_SUCCESS) goto out_now;
1066 return STATUS_PENDING;
1068 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1069 error_caps:
1070 FIXME("Returning error because of missing capabilities\n");
1071 status = STATUS_INVALID_PARAMETER;
1072 #endif
1073 out_now:
1074 stop_waiting(commio->hDevice);
1075 RtlFreeHeap(GetProcessHeap(), 0, commio);
1076 return status;
1079 static NTSTATUS xmit_immediate(HANDLE hDevice, int fd, const char* ptr)
1081 /* FIXME: not perfect as it should bypass the in-queue */
1082 WARN("(%p,'%c') not perfect!\n", hDevice, *ptr);
1083 if (write(fd, ptr, 1) != 1)
1084 return FILE_GetNtStatus();
1085 return STATUS_SUCCESS;
1088 /******************************************************************
1089 * COMM_DeviceIoControl
1093 static inline NTSTATUS io_control(HANDLE hDevice,
1094 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1095 PVOID UserApcContext,
1096 PIO_STATUS_BLOCK piosb,
1097 ULONG dwIoControlCode,
1098 LPVOID lpInBuffer, DWORD nInBufferSize,
1099 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1101 DWORD sz = 0, access = FILE_READ_DATA;
1102 NTSTATUS status = STATUS_SUCCESS;
1103 int fd = -1, needs_close = 0;
1104 enum server_fd_type type;
1106 TRACE("%p %s %p %d %p %d %p\n",
1107 hDevice, iocode2str(dwIoControlCode), lpInBuffer, nInBufferSize,
1108 lpOutBuffer, nOutBufferSize, piosb);
1110 switch (dwIoControlCode)
1112 case IOCTL_SERIAL_GET_TIMEOUTS:
1113 case IOCTL_SERIAL_SET_TIMEOUTS:
1114 case IOCTL_SERIAL_GET_WAIT_MASK:
1115 case IOCTL_SERIAL_SET_WAIT_MASK:
1116 /* these are handled on the server side */
1117 return STATUS_NOT_SUPPORTED;
1120 piosb->Information = 0;
1122 if ((status = server_get_unix_fd( hDevice, access, &fd, &needs_close, &type, NULL )))
1123 goto error;
1124 if (type != FD_TYPE_SERIAL)
1126 if (needs_close) close( fd );
1127 status = STATUS_OBJECT_TYPE_MISMATCH;
1128 goto error;
1131 switch (dwIoControlCode)
1133 case IOCTL_SERIAL_CLR_DTR:
1134 #ifdef TIOCM_DTR
1135 if (whack_modem(fd, ~TIOCM_DTR, 0) == -1) status = FILE_GetNtStatus();
1136 #else
1137 status = STATUS_NOT_SUPPORTED;
1138 #endif
1139 break;
1140 case IOCTL_SERIAL_CLR_RTS:
1141 #ifdef TIOCM_RTS
1142 if (whack_modem(fd, ~TIOCM_RTS, 0) == -1) status = FILE_GetNtStatus();
1143 #else
1144 status = STATUS_NOT_SUPPORTED;
1145 #endif
1146 break;
1147 case IOCTL_SERIAL_GET_BAUD_RATE:
1148 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_BAUD_RATE))
1150 if (!(status = get_baud_rate(fd, lpOutBuffer)))
1151 sz = sizeof(SERIAL_BAUD_RATE);
1153 else
1154 status = STATUS_INVALID_PARAMETER;
1155 break;
1156 case IOCTL_SERIAL_GET_CHARS:
1157 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_CHARS))
1159 if (!(status = get_special_chars(fd, lpOutBuffer)))
1160 sz = sizeof(SERIAL_CHARS);
1162 else
1163 status = STATUS_INVALID_PARAMETER;
1164 break;
1165 case IOCTL_SERIAL_GET_COMMSTATUS:
1166 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_STATUS))
1168 if (!(status = get_status(fd, lpOutBuffer)))
1169 sz = sizeof(SERIAL_STATUS);
1171 else status = STATUS_INVALID_PARAMETER;
1172 break;
1173 case IOCTL_SERIAL_GET_HANDFLOW:
1174 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_HANDFLOW))
1176 if (!(status = get_hand_flow(fd, lpOutBuffer)))
1177 sz = sizeof(SERIAL_HANDFLOW);
1179 else
1180 status = STATUS_INVALID_PARAMETER;
1181 break;
1182 case IOCTL_SERIAL_GET_LINE_CONTROL:
1183 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_LINE_CONTROL))
1185 if (!(status = get_line_control(fd, lpOutBuffer)))
1186 sz = sizeof(SERIAL_LINE_CONTROL);
1188 else
1189 status = STATUS_INVALID_PARAMETER;
1190 break;
1191 case IOCTL_SERIAL_GET_MODEMSTATUS:
1192 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1194 if (!(status = get_modem_status(fd, lpOutBuffer)))
1195 sz = sizeof(DWORD);
1197 else status = STATUS_INVALID_PARAMETER;
1198 break;
1199 case IOCTL_SERIAL_IMMEDIATE_CHAR:
1200 if (lpInBuffer && nInBufferSize == sizeof(CHAR))
1201 status = xmit_immediate(hDevice, fd, lpInBuffer);
1202 else
1203 status = STATUS_INVALID_PARAMETER;
1204 break;
1205 case IOCTL_SERIAL_PURGE:
1206 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1207 status = purge(fd, *(DWORD*)lpInBuffer);
1208 else
1209 status = STATUS_INVALID_PARAMETER;
1210 break;
1211 case IOCTL_SERIAL_RESET_DEVICE:
1212 FIXME("Unsupported\n");
1213 break;
1214 case IOCTL_SERIAL_SET_BAUD_RATE:
1215 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_BAUD_RATE))
1216 status = set_baud_rate(fd, lpInBuffer);
1217 else
1218 status = STATUS_INVALID_PARAMETER;
1219 break;
1220 case IOCTL_SERIAL_SET_BREAK_OFF:
1221 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1222 if (ioctl(fd, TIOCCBRK, 0) == -1)
1224 TRACE("ioctl failed\n");
1225 status = FILE_GetNtStatus();
1227 #else
1228 FIXME("ioctl not available\n");
1229 status = STATUS_NOT_SUPPORTED;
1230 #endif
1231 break;
1232 case IOCTL_SERIAL_SET_BREAK_ON:
1233 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1234 if (ioctl(fd, TIOCSBRK, 0) == -1)
1236 TRACE("ioctl failed\n");
1237 status = FILE_GetNtStatus();
1239 #else
1240 FIXME("ioctl not available\n");
1241 status = STATUS_NOT_SUPPORTED;
1242 #endif
1243 break;
1244 case IOCTL_SERIAL_SET_CHARS:
1245 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_CHARS))
1246 status = set_special_chars(fd, lpInBuffer);
1247 else
1248 status = STATUS_INVALID_PARAMETER;
1249 break;
1250 case IOCTL_SERIAL_SET_DTR:
1251 #ifdef TIOCM_DTR
1252 if (whack_modem(fd, 0, TIOCM_DTR) == -1) status = FILE_GetNtStatus();
1253 #else
1254 status = STATUS_NOT_SUPPORTED;
1255 #endif
1256 break;
1257 case IOCTL_SERIAL_SET_HANDFLOW:
1258 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_HANDFLOW))
1259 status = set_handflow(fd, lpInBuffer);
1260 else
1261 status = STATUS_INVALID_PARAMETER;
1262 break;
1263 case IOCTL_SERIAL_SET_LINE_CONTROL:
1264 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_LINE_CONTROL))
1265 status = set_line_control(fd, lpInBuffer);
1266 else
1267 status = STATUS_INVALID_PARAMETER;
1268 break;
1269 case IOCTL_SERIAL_SET_QUEUE_SIZE:
1270 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_QUEUE_SIZE))
1271 status = set_queue_size(fd, lpInBuffer);
1272 else
1273 status = STATUS_INVALID_PARAMETER;
1274 break;
1275 case IOCTL_SERIAL_SET_RTS:
1276 #ifdef TIOCM_RTS
1277 if (whack_modem(fd, 0, TIOCM_RTS) == -1) status = FILE_GetNtStatus();
1278 #else
1279 status = STATUS_NOT_SUPPORTED;
1280 #endif
1281 break;
1282 case IOCTL_SERIAL_SET_XOFF:
1283 status = set_XOff(fd);
1284 break;
1285 case IOCTL_SERIAL_SET_XON:
1286 status = set_XOn(fd);
1287 break;
1288 case IOCTL_SERIAL_WAIT_ON_MASK:
1289 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1291 if (!(status = wait_on(hDevice, fd, hEvent, piosb, lpOutBuffer)))
1292 sz = sizeof(DWORD);
1294 else
1295 status = STATUS_INVALID_PARAMETER;
1296 break;
1297 default:
1298 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1299 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
1300 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
1301 sz = 0;
1302 status = STATUS_INVALID_PARAMETER;
1303 break;
1305 if (needs_close) close( fd );
1306 error:
1307 piosb->u.Status = status;
1308 piosb->Information = sz;
1309 if (hEvent && status != STATUS_PENDING) NtSetEvent(hEvent, NULL);
1310 return status;
1313 NTSTATUS COMM_DeviceIoControl(HANDLE hDevice,
1314 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1315 PVOID UserApcContext,
1316 PIO_STATUS_BLOCK piosb,
1317 ULONG dwIoControlCode,
1318 LPVOID lpInBuffer, DWORD nInBufferSize,
1319 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1321 NTSTATUS status;
1323 if (dwIoControlCode == IOCTL_SERIAL_WAIT_ON_MASK)
1325 HANDLE hev = hEvent;
1327 /* this is an ioctl we implement in a non blocking way if hEvent is not
1328 * null
1329 * so we have to explicitly wait if no hEvent is provided
1331 if (!hev)
1333 OBJECT_ATTRIBUTES attr;
1335 attr.Length = sizeof(attr);
1336 attr.RootDirectory = 0;
1337 attr.ObjectName = NULL;
1338 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1339 attr.SecurityDescriptor = NULL;
1340 attr.SecurityQualityOfService = NULL;
1341 status = NtCreateEvent(&hev, EVENT_ALL_ACCESS, &attr, SynchronizationEvent, FALSE);
1343 if (status) return status;
1345 status = io_control(hDevice, hev, UserApcRoutine, UserApcContext,
1346 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1347 lpOutBuffer, nOutBufferSize);
1348 if (hev != hEvent)
1350 if (status == STATUS_PENDING)
1352 NtWaitForSingleObject(hev, FALSE, NULL);
1353 status = STATUS_SUCCESS;
1355 NtClose(hev);
1358 else status = io_control(hDevice, hEvent, UserApcRoutine, UserApcContext,
1359 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1360 lpOutBuffer, nOutBufferSize);
1361 return status;
1364 NTSTATUS COMM_FlushBuffersFile( int fd )
1366 #ifdef HAVE_TCDRAIN
1367 while (tcdrain( fd ) == -1)
1369 if (errno != EINTR) return FILE_GetNtStatus();
1371 return STATUS_SUCCESS;
1372 #elif defined(TIOCDRAIN)
1373 while (ioctl( fd, TIOCDRAIN ) == -1)
1375 if (errno != EINTR) return FILE_GetNtStatus();
1377 return STATUS_SUCCESS;
1378 #elif defined(TCSBRK)
1379 while (ioctl( fd, TCSBRK, 1 ) == -1)
1381 if (errno != EINTR) return FILE_GetNtStatus();
1383 return STATUS_SUCCESS;
1384 #else
1385 ERR( "not supported\n" );
1386 return STATUS_NOT_IMPLEMENTED;
1387 #endif