usp10/tests: A spelling fix in an ok() message.
[wine.git] / dlls / ntdll / serial.c
blob66081d286537ec1474bcfb8776529312400f309d
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_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <fcntl.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
39 #endif
40 #include <sys/types.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
43 #endif
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
46 #endif
47 #ifdef HAVE_SYS_POLL_H
48 # include <sys/poll.h>
49 #endif
50 #ifdef HAVE_SYS_MODEM_H
51 # include <sys/modem.h>
52 #endif
53 #ifdef HAVE_SYS_STRTIO_H
54 # include <sys/strtio.h>
55 #endif
57 #include "ntstatus.h"
58 #define WIN32_NO_STATUS
59 #define NONAMELESSUNION
60 #include "windef.h"
61 #include "winternl.h"
62 #include "winioctl.h"
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>
72 #endif
73 #include <linux/serial.h>
74 #endif
76 #if !defined(TIOCINQ) && defined(FIONREAD)
77 #define TIOCINQ FIONREAD
78 #endif
80 WINE_DEFAULT_DEBUG_CHANNEL(comm);
82 static const char* iocode2str(DWORD ioc)
84 switch (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);
124 #undef X
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)
131 struct termios port;
132 int speed;
134 if (tcgetattr(fd, &port) == -1)
136 ERR("tcgetattr error '%s'\n", strerror(errno));
137 return FILE_GetNtStatus();
139 speed = cfgetospeed(&port);
140 switch (speed)
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;
158 #ifdef B57600
159 case B57600: sbr->BaudRate = 57600; break;
160 #endif
161 #ifdef B115200
162 case B115200: sbr->BaudRate = 115200; break;
163 #endif
164 #ifdef B230400
165 case B230400: sbr->BaudRate = 230400; break;
166 #endif
167 #ifdef B460800
168 case B460800: sbr->BaudRate = 460800; break;
169 #endif
170 #ifdef B500000
171 case B500000: sbr->BaudRate = 500000; break;
172 #endif
173 #ifdef B921600
174 case B921600: sbr->BaudRate = 921600; break;
175 #endif
176 #ifdef B1000000
177 case B1000000: sbr->BaudRate = 1000000; break;
178 #endif
179 #ifdef B1152000
180 case B1152000: sbr->BaudRate = 1152000; break;
181 #endif
182 #ifdef B1500000
183 case B1500000: sbr->BaudRate = 1500000; break;
184 #endif
185 #ifdef B2000000
186 case B2000000: sbr->BaudRate = 2000000; break;
187 #endif
188 #ifdef B2500000
189 case B2500000: sbr->BaudRate = 2500000; break;
190 #endif
191 #ifdef B3000000
192 case B3000000: sbr->BaudRate = 3000000; break;
193 #endif
194 #ifdef B3500000
195 case B3500000: sbr->BaudRate = 3500000; break;
196 #endif
197 #ifdef B4000000
198 case B4000000: sbr->BaudRate = 4000000; break;
199 #endif
200 default:
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)
209 int stat = 0;
210 struct termios port;
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;
220 #ifdef TIOCMGET
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;
227 #else
228 WARN("Setting DTR/RTS to enabled by default\n");
229 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
230 shf->FlowReplace |= SERIAL_RTS_CONTROL;
231 #endif
232 #ifdef TIOCM_DTR
233 if (stat & TIOCM_DTR)
234 #endif
235 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
236 #ifdef CRTSCTS
237 if (port.c_cflag & CRTSCTS)
239 shf->FlowReplace |= SERIAL_RTS_CONTROL;
240 shf->ControlHandShake |= SERIAL_CTS_HANDSHAKE;
242 else
243 #endif
245 #ifdef TIOCM_RTS
246 if (stat & TIOCM_RTS)
247 #endif
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;
255 shf->XonLimit = 10;
256 shf->XoffLimit = 10;
257 return STATUS_SUCCESS;
260 static NTSTATUS get_line_control(int fd, SERIAL_LINE_CONTROL* slc)
262 struct termios port;
264 if (tcgetattr(fd, &port) == -1)
266 ERR("tcgetattr error '%s'\n", strerror(errno));
267 return FILE_GetNtStatus();
270 #ifdef CMSPAR
271 switch (port.c_cflag & (PARENB | PARODD | CMSPAR))
272 #else
273 switch (port.c_cflag & (PARENB | PARODD))
274 #endif
276 case 0: slc->Parity = NOPARITY; break;
277 case PARENB: slc->Parity = EVENPARITY; break;
278 case PARENB|PARODD: slc->Parity = ODDPARITY; break;
279 #ifdef CMSPAR
280 case PARENB|PARODD|CMSPAR: slc->Parity = MARKPARITY; break;
281 case PARENB|CMSPAR: slc->Parity = SPACEPARITY; break;
282 #endif
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;
297 else
298 slc->StopBits = TWOSTOPBITS;
300 else
301 slc->StopBits = ONESTOPBIT;
303 return STATUS_SUCCESS;
306 static NTSTATUS get_modem_status(int fd, DWORD* lpModemStat)
308 NTSTATUS status = STATUS_NOT_SUPPORTED;
309 int mstat;
311 *lpModemStat = 0;
312 #ifdef TIOCMGET
313 if (!ioctl(fd, TIOCMGET, &mstat))
315 #ifdef TIOCM_CTS
316 if (mstat & TIOCM_CTS) *lpModemStat |= MS_CTS_ON;
317 #endif
318 #ifdef TIOCM_DSR
319 if (mstat & TIOCM_DSR) *lpModemStat |= MS_DSR_ON;
320 #endif
321 #ifdef TIOCM_RNG
322 if (mstat & TIOCM_RNG) *lpModemStat |= MS_RING_ON;
323 #endif
324 #ifdef TIOCM_CAR
325 /* FIXME: Not really sure about RLSD UB 990810 */
326 if (mstat & TIOCM_CAR) *lpModemStat |= MS_RLSD_ON;
327 #endif
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();
337 #endif
338 return status;
341 static NTSTATUS get_special_chars(int fd, SERIAL_CHARS* sc)
343 struct termios port;
345 if (tcgetattr(fd, &port) == -1)
347 ERR("tcgetattr error '%s'\n", strerror(errno));
348 return FILE_GetNtStatus();
350 sc->EofChar = port.c_cc[VEOF];
351 sc->ErrorChar = 0xFF;
352 sc->BreakChar = 0; /* FIXME */
353 sc->EventChar = 0; /* FIXME */
354 sc->XonChar = port.c_cc[VSTART];
355 sc->XoffChar = port.c_cc[VSTOP];
357 return STATUS_SUCCESS;
360 static NTSTATUS get_status(int fd, SERIAL_STATUS* ss)
362 NTSTATUS status = STATUS_SUCCESS;
364 ss->Errors = 0;
365 ss->HoldReasons = 0;
366 ss->EofReceived = FALSE;
367 ss->WaitForImmediate = FALSE;
368 #ifdef TIOCOUTQ
369 if (ioctl(fd, TIOCOUTQ, &ss->AmountInOutQueue) == -1)
371 WARN("ioctl returned error\n");
372 status = FILE_GetNtStatus();
374 #else
375 ss->AmountInOutQueue = 0; /* FIXME: find a different way to find out */
376 #endif
378 #ifdef TIOCINQ
379 if (ioctl(fd, TIOCINQ, &ss->AmountInInQueue))
381 WARN("ioctl returned error\n");
382 status = FILE_GetNtStatus();
384 #else
385 ss->AmountInInQueue = 0; /* FIXME: find a different way to find out */
386 #endif
387 return status;
390 static void stop_waiting( HANDLE handle )
392 NTSTATUS status;
394 SERVER_START_REQ( set_serial_info )
396 req->handle = wine_server_obj_handle( handle );
397 req->flags = SERIALINFO_PENDING_WAIT;
398 if ((status = wine_server_call( req )))
399 ERR("failed to clear waiting state: %#x\n", status);
401 SERVER_END_REQ;
404 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD *mask, DWORD *cookie, DWORD *pending_write, BOOL start_wait)
406 NTSTATUS status;
408 SERVER_START_REQ( get_serial_info )
410 req->handle = wine_server_obj_handle( hDevice );
411 req->flags = pending_write ? SERIALINFO_PENDING_WRITE : 0;
412 if (start_wait) req->flags |= SERIALINFO_PENDING_WAIT;
413 if (!(status = wine_server_call( req )))
415 *mask = reply->eventmask;
416 if (cookie) *cookie = reply->cookie;
417 if (pending_write) *pending_write = reply->pending_write;
420 SERVER_END_REQ;
421 return status;
424 static NTSTATUS purge(int fd, DWORD flags)
427 ** not exactly sure how these are different
428 ** Perhaps if we had our own internal queues, one flushes them
429 ** and the other flushes the kernel's buffers.
431 if (flags & PURGE_TXABORT) tcflush(fd, TCOFLUSH);
432 if (flags & PURGE_RXABORT) tcflush(fd, TCIFLUSH);
433 if (flags & PURGE_TXCLEAR) tcflush(fd, TCOFLUSH);
434 if (flags & PURGE_RXCLEAR) tcflush(fd, TCIFLUSH);
435 return STATUS_SUCCESS;
438 static NTSTATUS set_baud_rate(int fd, const SERIAL_BAUD_RATE* sbr)
440 struct termios port;
442 if (tcgetattr(fd, &port) == -1)
444 ERR("tcgetattr error '%s'\n", strerror(errno));
445 return FILE_GetNtStatus();
448 switch (sbr->BaudRate)
450 case 0: cfsetospeed( &port, B0 ); break;
451 case 50: cfsetospeed( &port, B50 ); break;
452 case 75: cfsetospeed( &port, B75 ); break;
453 case 110:
454 case CBR_110: cfsetospeed( &port, B110 ); break;
455 case 134: cfsetospeed( &port, B134 ); break;
456 case 150: cfsetospeed( &port, B150 ); break;
457 case 200: cfsetospeed( &port, B200 ); break;
458 case 300:
459 case CBR_300: cfsetospeed( &port, B300 ); break;
460 case 600:
461 case CBR_600: cfsetospeed( &port, B600 ); break;
462 case 1200:
463 case CBR_1200: cfsetospeed( &port, B1200 ); break;
464 case 1800: cfsetospeed( &port, B1800 ); break;
465 case 2400:
466 case CBR_2400: cfsetospeed( &port, B2400 ); break;
467 case 4800:
468 case CBR_4800: cfsetospeed( &port, B4800 ); break;
469 case 9600:
470 case CBR_9600: cfsetospeed( &port, B9600 ); break;
471 case 19200:
472 case CBR_19200: cfsetospeed( &port, B19200 ); break;
473 case 38400:
474 case CBR_38400: cfsetospeed( &port, B38400 ); break;
475 #ifdef B57600
476 case 57600: cfsetospeed( &port, B57600 ); break;
477 #endif
478 #ifdef B115200
479 case 115200: cfsetospeed( &port, B115200 ); break;
480 #endif
481 #ifdef B230400
482 case 230400: cfsetospeed( &port, B230400 ); break;
483 #endif
484 #ifdef B460800
485 case 460800: cfsetospeed( &port, B460800 ); break;
486 #endif
487 #ifdef B500000
488 case 500000: cfsetospeed( &port, B500000 ); break;
489 #endif
490 #ifdef B921600
491 case 921600: cfsetospeed( &port, B921600 ); break;
492 #endif
493 #ifdef B1000000
494 case 1000000: cfsetospeed( &port, B1000000 ); break;
495 #endif
496 #ifdef B1152000
497 case 1152000: cfsetospeed( &port, B1152000 ); break;
498 #endif
499 #ifdef B1500000
500 case 1500000: cfsetospeed( &port, B1500000 ); break;
501 #endif
502 #ifdef B2000000
503 case 2000000: cfsetospeed( &port, B2000000 ); break;
504 #endif
505 #ifdef B2500000
506 case 2500000: cfsetospeed( &port, B2500000 ); break;
507 #endif
508 #ifdef B3000000
509 case 3000000: cfsetospeed( &port, B3000000 ); break;
510 #endif
511 #ifdef B3500000
512 case 3500000: cfsetospeed( &port, B3500000 ); break;
513 #endif
514 #ifdef B4000000
515 case 4000000: cfsetospeed( &port, B4000000 ); break;
516 #endif
517 default:
518 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
520 struct serial_struct nuts;
521 int arby;
523 ioctl(fd, TIOCGSERIAL, &nuts);
524 nuts.custom_divisor = nuts.baud_base / sbr->BaudRate;
525 if (!(nuts.custom_divisor)) nuts.custom_divisor = 1;
526 arby = nuts.baud_base / nuts.custom_divisor;
527 nuts.flags &= ~ASYNC_SPD_MASK;
528 nuts.flags |= ASYNC_SPD_CUST;
529 WARN("You (or a program acting at your behest) have specified\n"
530 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
531 "which is as close as we can get by our present understanding of your\n"
532 "hardware. I hope you know what you are doing. Any disruption Wine\n"
533 "has caused to your linux system can be undone with setserial\n"
534 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
535 "reset it and it will probably recover.\n", sbr->BaudRate, arby);
536 ioctl(fd, TIOCSSERIAL, &nuts);
537 cfsetospeed( &port, B38400 );
539 break;
540 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
541 ERR("baudrate %d\n", sbr->BaudRate);
542 return STATUS_NOT_SUPPORTED;
543 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
545 cfsetispeed( &port, cfgetospeed(&port) );
546 if (tcsetattr(fd, TCSANOW, &port) == -1)
548 ERR("tcsetattr error '%s'\n", strerror(errno));
549 return FILE_GetNtStatus();
551 return STATUS_SUCCESS;
554 static int whack_modem(int fd, unsigned int andy, unsigned int orrie)
556 #ifdef TIOCMGET
557 unsigned int mstat, okay;
558 okay = ioctl(fd, TIOCMGET, &mstat);
559 if (okay) return okay;
560 if (andy) mstat &= andy;
561 mstat |= orrie;
562 return ioctl(fd, TIOCMSET, &mstat);
563 #else
564 return 0;
565 #endif
568 static NTSTATUS set_handflow(int fd, const SERIAL_HANDFLOW* shf)
570 struct termios port;
572 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE)) ==
573 (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
574 return STATUS_NOT_SUPPORTED;
576 if (tcgetattr(fd, &port) == -1)
578 ERR("tcgetattr error '%s'\n", strerror(errno));
579 return FILE_GetNtStatus();
582 #ifdef CRTSCTS
583 if ((shf->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
584 (shf->FlowReplace & SERIAL_RTS_HANDSHAKE))
586 port.c_cflag |= CRTSCTS;
587 TRACE("CRTSCTS\n");
589 else
590 port.c_cflag &= ~CRTSCTS;
591 #endif
592 #ifdef TIOCM_DTR
593 if (shf->ControlHandShake & SERIAL_DTR_HANDSHAKE)
595 WARN("DSR/DTR flow control not supported\n");
596 } else if (!(shf->ControlHandShake & SERIAL_DTR_CONTROL))
597 whack_modem(fd, ~TIOCM_DTR, 0);
598 else
599 whack_modem(fd, 0, TIOCM_DTR);
600 #endif
601 #ifdef TIOCM_RTS
602 if (!(shf->ControlHandShake & SERIAL_CTS_HANDSHAKE))
604 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL|SERIAL_RTS_HANDSHAKE)) == 0)
605 whack_modem(fd, ~TIOCM_RTS, 0);
606 else
607 whack_modem(fd, 0, TIOCM_RTS);
609 #endif
611 if (shf->FlowReplace & SERIAL_AUTO_RECEIVE)
612 port.c_iflag |= IXOFF;
613 else
614 port.c_iflag &= ~IXOFF;
615 if (shf->FlowReplace & SERIAL_AUTO_TRANSMIT)
616 port.c_iflag |= IXON;
617 else
618 port.c_iflag &= ~IXON;
619 if (tcsetattr(fd, TCSANOW, &port) == -1)
621 ERR("tcsetattr error '%s'\n", strerror(errno));
622 return FILE_GetNtStatus();
625 return STATUS_SUCCESS;
628 static NTSTATUS set_line_control(int fd, const SERIAL_LINE_CONTROL* slc)
630 struct termios port;
631 unsigned bytesize, stopbits;
633 if (tcgetattr(fd, &port) == -1)
635 ERR("tcgetattr error '%s'\n", strerror(errno));
636 return FILE_GetNtStatus();
639 #ifdef IMAXBEL
640 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK|IMAXBEL);
641 #else
642 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK);
643 #endif
644 port.c_iflag |= IGNBRK | INPCK;
645 port.c_oflag &= ~(OPOST);
646 port.c_cflag &= ~(HUPCL);
647 port.c_cflag |= CLOCAL | CREAD;
650 * on FreeBSD, turning off ICANON does not disable IEXTEN,
651 * so we must turn it off explicitly. No harm done on Linux.
653 port.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN);
654 port.c_lflag |= NOFLSH;
656 bytesize = slc->WordLength;
657 stopbits = slc->StopBits;
659 #ifdef CMSPAR
660 port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
661 #else
662 port.c_cflag &= ~(PARENB | PARODD);
663 #endif
665 /* make sure that reads don't block */
666 port.c_cc[VMIN] = 0;
667 port.c_cc[VTIME] = 0;
669 switch (slc->Parity)
671 case NOPARITY: port.c_iflag &= ~INPCK; break;
672 case ODDPARITY: port.c_cflag |= PARENB | PARODD; break;
673 case EVENPARITY: port.c_cflag |= PARENB; break;
674 #ifdef CMSPAR
675 /* Linux defines mark/space (stick) parity */
676 case MARKPARITY: port.c_cflag |= PARENB | PARODD | CMSPAR; break;
677 case SPACEPARITY: port.c_cflag |= PARENB | CMSPAR; break;
678 #else
679 /* try the POSIX way */
680 case MARKPARITY:
681 if (slc->StopBits == ONESTOPBIT)
683 stopbits = TWOSTOPBITS;
684 port.c_iflag &= ~INPCK;
686 else
688 FIXME("Cannot set MARK Parity\n");
689 return STATUS_NOT_SUPPORTED;
691 break;
692 case SPACEPARITY:
693 if (slc->WordLength < 8)
695 bytesize +=1;
696 port.c_iflag &= ~INPCK;
698 else
700 FIXME("Cannot set SPACE Parity\n");
701 return STATUS_NOT_SUPPORTED;
703 break;
704 #endif
705 default:
706 FIXME("Parity %d is not supported\n", slc->Parity);
707 return STATUS_NOT_SUPPORTED;
710 port.c_cflag &= ~CSIZE;
711 switch (bytesize)
713 case 5: port.c_cflag |= CS5; break;
714 case 6: port.c_cflag |= CS6; break;
715 case 7: port.c_cflag |= CS7; break;
716 case 8: port.c_cflag |= CS8; break;
717 default:
718 FIXME("ByteSize %d is not supported\n", bytesize);
719 return STATUS_NOT_SUPPORTED;
722 switch (stopbits)
724 case ONESTOPBIT: port.c_cflag &= ~CSTOPB; break;
725 case ONE5STOPBITS: /* will be selected if bytesize is 5 */
726 case TWOSTOPBITS: port.c_cflag |= CSTOPB; break;
727 default:
728 FIXME("StopBits %d is not supported\n", stopbits);
729 return STATUS_NOT_SUPPORTED;
731 /* otherwise it hangs with pending input*/
732 if (tcsetattr(fd, TCSANOW, &port) == -1)
734 ERR("tcsetattr error '%s'\n", strerror(errno));
735 return FILE_GetNtStatus();
737 return STATUS_SUCCESS;
740 static NTSTATUS set_queue_size(int fd, const SERIAL_QUEUE_SIZE* sqs)
742 FIXME("insize %d outsize %d unimplemented stub\n", sqs->InSize, sqs->OutSize);
743 return STATUS_SUCCESS;
746 static NTSTATUS set_special_chars(int fd, const SERIAL_CHARS* sc)
748 struct termios port;
750 if (tcgetattr(fd, &port) == -1)
752 ERR("tcgetattr error '%s'\n", strerror(errno));
753 return FILE_GetNtStatus();
756 port.c_cc[VEOF ] = sc->EofChar;
757 /* FIXME: sc->ErrorChar is not supported */
758 /* FIXME: sc->BreakChar is not supported */
759 /* FIXME: sc->EventChar is not supported */
760 port.c_cc[VSTART] = sc->XonChar;
761 port.c_cc[VSTOP ] = sc->XoffChar;
763 if (tcsetattr(fd, TCSANOW, &port) == -1)
765 ERR("tcsetattr error '%s'\n", strerror(errno));
766 return FILE_GetNtStatus();
768 return STATUS_SUCCESS;
772 * does not change IXOFF but simulates that IXOFF has been received:
774 static NTSTATUS set_XOff(int fd)
776 if (tcflow(fd, TCOOFF))
778 return FILE_GetNtStatus();
780 return STATUS_SUCCESS;
784 * does not change IXON but simulates that IXON has been received:
786 static NTSTATUS set_XOn(int fd)
788 if (tcflow(fd, TCOON))
790 return FILE_GetNtStatus();
792 return STATUS_SUCCESS;
795 /* serial_irq_info
796 * local structure holding the irq values we need for WaitCommEvent()
798 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
799 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
800 * no need to carry them in the internal structure
803 typedef struct serial_irq_info
805 int rx, tx, frame, overrun, parity, brk, buf_overrun, temt;
806 }serial_irq_info;
808 /***********************************************************************
809 * Data needed by the thread polling for the changing CommEvent
811 typedef struct async_commio
813 HANDLE hDevice;
814 DWORD* events;
815 IO_STATUS_BLOCK* iosb;
816 HANDLE hEvent;
817 DWORD evtmask;
818 DWORD cookie;
819 DWORD mstat;
820 DWORD pending_write;
821 serial_irq_info irq_info;
822 } async_commio;
824 /***********************************************************************
825 * Get extended interrupt count info, needed for wait_on
827 static NTSTATUS get_irq_info(int fd, serial_irq_info *irq_info)
829 int out;
831 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCGICOUNT)
832 struct serial_icounter_struct einfo;
833 if (!ioctl(fd, TIOCGICOUNT, &einfo))
835 irq_info->rx = einfo.rx;
836 irq_info->tx = einfo.tx;
837 irq_info->frame = einfo.frame;
838 irq_info->overrun = einfo.overrun;
839 irq_info->parity = einfo.parity;
840 irq_info->brk = einfo.brk;
841 irq_info->buf_overrun = einfo.buf_overrun;
843 else
845 TRACE("TIOCGICOUNT err %s\n", strerror(errno));
846 memset(irq_info,0, sizeof(serial_irq_info));
848 #else
849 memset(irq_info,0, sizeof(serial_irq_info));
850 #endif
852 irq_info->temt = 0;
853 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
854 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
855 if (!ioctl(fd, TIOCSERGETLSR, &out))
857 irq_info->temt = (out & TIOCSER_TEMT) != 0;
858 return STATUS_SUCCESS;
861 TRACE("TIOCSERGETLSR err %s\n", strerror(errno));
862 #endif
863 #ifdef TIOCOUTQ /* otherwise we log when the out queue gets empty */
864 if (!ioctl(fd, TIOCOUTQ, &out))
866 irq_info->temt = out == 0;
867 return STATUS_SUCCESS;
869 TRACE("TIOCOUTQ err %s\n", strerror(errno));
870 return FILE_GetNtStatus();
871 #endif
872 return STATUS_SUCCESS;
876 static DWORD check_events(int fd, DWORD mask,
877 const serial_irq_info *new,
878 const serial_irq_info *old,
879 DWORD new_mstat, DWORD old_mstat, DWORD pending_write)
881 DWORD ret = 0, queue;
883 TRACE("mask 0x%08x\n", mask);
884 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old->rx, new->rx);
885 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old->tx, new->tx);
886 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old->frame, new->frame);
887 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old->overrun, new->overrun);
888 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old->parity, new->parity);
889 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old->brk, new->brk);
890 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old->buf_overrun, new->buf_overrun);
891 TRACE("old->temt 0x%08x vs. new->temt 0x%08x\n", old->temt, new->temt);
893 if (old->brk != new->brk) ret |= EV_BREAK;
894 if ((old_mstat & MS_CTS_ON ) != (new_mstat & MS_CTS_ON )) ret |= EV_CTS;
895 if ((old_mstat & MS_DSR_ON ) != (new_mstat & MS_DSR_ON )) ret |= EV_DSR;
896 if ((old_mstat & MS_RING_ON) != (new_mstat & MS_RING_ON)) ret |= EV_RING;
897 if ((old_mstat & MS_RLSD_ON) != (new_mstat & MS_RLSD_ON)) ret |= EV_RLSD;
898 if (old->frame != new->frame || old->overrun != new->overrun || old->parity != new->parity) ret |= EV_ERR;
899 if (mask & EV_RXCHAR)
901 queue = 0;
902 #ifdef TIOCINQ
903 if (ioctl(fd, TIOCINQ, &queue))
904 WARN("TIOCINQ returned error\n");
905 #endif
906 if (queue)
907 ret |= EV_RXCHAR;
909 if (mask & EV_TXEMPTY)
911 if ((!old->temt || pending_write) && new->temt)
912 ret |= EV_TXEMPTY;
914 return ret & mask;
917 /***********************************************************************
918 * wait_for_event (INTERNAL)
920 * We need to poll for what is interesting
921 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
924 static DWORD CALLBACK wait_for_event(LPVOID arg)
926 async_commio *commio = arg;
927 int fd, needs_close;
929 if (!server_get_unix_fd( commio->hDevice, FILE_READ_DATA | FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL ))
931 serial_irq_info new_irq_info;
932 DWORD new_mstat, dummy, cookie;
933 LARGE_INTEGER time;
935 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
936 commio->hDevice, fd, commio->evtmask, commio->events, commio->hEvent, &commio->irq_info);
938 time.QuadPart = (ULONGLONG)10000;
939 time.QuadPart = -time.QuadPart;
940 for (;;)
943 * TIOCMIWAIT is not adequate
945 * FIXME:
946 * We don't handle the EV_RXFLAG (the eventchar)
948 NtDelayExecution(FALSE, &time);
949 get_irq_info(fd, &new_irq_info);
950 if (get_modem_status(fd, &new_mstat))
952 TRACE("get_modem_status failed\n");
953 *commio->events = 0;
954 break;
956 *commio->events = check_events(fd, commio->evtmask,
957 &new_irq_info, &commio->irq_info,
958 new_mstat, commio->mstat, commio->pending_write);
959 if (*commio->events) break;
960 get_wait_mask(commio->hDevice, &dummy, &cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, FALSE);
961 if (commio->cookie != cookie)
963 *commio->events = 0;
964 break;
967 if (needs_close) close( fd );
969 if (commio->iosb)
971 if (*commio->events)
973 commio->iosb->u.Status = STATUS_SUCCESS;
974 commio->iosb->Information = sizeof(DWORD);
976 else
977 commio->iosb->u.Status = STATUS_CANCELLED;
979 stop_waiting(commio->hDevice);
980 if (commio->hEvent) NtSetEvent(commio->hEvent, NULL);
981 RtlFreeHeap(GetProcessHeap(), 0, commio);
982 return 0;
985 static NTSTATUS wait_on(HANDLE hDevice, int fd, HANDLE hEvent, PIO_STATUS_BLOCK piosb, DWORD* events)
987 async_commio* commio;
988 NTSTATUS status;
990 if ((status = NtResetEvent(hEvent, NULL)))
991 return status;
993 commio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio));
994 if (!commio) return STATUS_NO_MEMORY;
996 commio->hDevice = hDevice;
997 commio->events = events;
998 commio->iosb = piosb;
999 commio->hEvent = hEvent;
1000 commio->pending_write = 0;
1001 status = get_wait_mask(commio->hDevice, &commio->evtmask, &commio->cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, TRUE);
1002 if (status)
1004 RtlFreeHeap(GetProcessHeap(), 0, commio);
1005 return status;
1008 /* We may never return, if some capabilities miss
1009 * Return error in that case
1011 #if !defined(TIOCINQ)
1012 if (commio->evtmask & EV_RXCHAR)
1013 goto error_caps;
1014 #endif
1015 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1016 if (commio->evtmask & EV_TXEMPTY)
1017 goto error_caps;
1018 #endif
1019 #if !defined(TIOCMGET)
1020 if (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD))
1021 goto error_caps;
1022 #endif
1023 #if !defined(TIOCM_CTS)
1024 if (commio->evtmask & EV_CTS)
1025 goto error_caps;
1026 #endif
1027 #if !defined(TIOCM_DSR)
1028 if (commio->evtmask & EV_DSR)
1029 goto error_caps;
1030 #endif
1031 #if !defined(TIOCM_RNG)
1032 if (commio->evtmask & EV_RING)
1033 goto error_caps;
1034 #endif
1035 #if !defined(TIOCM_CAR)
1036 if (commio->evtmask & EV_RLSD)
1037 goto error_caps;
1038 #endif
1039 if (commio->evtmask & EV_RXFLAG)
1040 FIXME("EV_RXFLAG not handled\n");
1042 if ((status = get_irq_info(fd, &commio->irq_info)) &&
1043 (commio->evtmask & (EV_BREAK | EV_ERR)))
1044 goto out_now;
1046 if ((status = get_modem_status(fd, &commio->mstat)) &&
1047 (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD)))
1048 goto out_now;
1050 /* We might have received something or the TX buffer is delivered */
1051 *events = check_events(fd, commio->evtmask,
1052 &commio->irq_info, &commio->irq_info,
1053 commio->mstat, commio->mstat, commio->pending_write);
1054 if (*events)
1056 status = STATUS_SUCCESS;
1057 goto out_now;
1060 /* create the worker for the task */
1061 status = RtlQueueWorkItem(wait_for_event, commio, 0 /* FIXME */);
1062 if (status != STATUS_SUCCESS) goto out_now;
1063 return STATUS_PENDING;
1065 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1066 error_caps:
1067 FIXME("Returning error because of missing capabilities\n");
1068 status = STATUS_INVALID_PARAMETER;
1069 #endif
1070 out_now:
1071 stop_waiting(commio->hDevice);
1072 RtlFreeHeap(GetProcessHeap(), 0, commio);
1073 return status;
1076 static NTSTATUS xmit_immediate(HANDLE hDevice, int fd, const char* ptr)
1078 /* FIXME: not perfect as it should bypass the in-queue */
1079 WARN("(%p,'%c') not perfect!\n", hDevice, *ptr);
1080 if (write(fd, ptr, 1) != 1)
1081 return FILE_GetNtStatus();
1082 return STATUS_SUCCESS;
1085 /******************************************************************
1086 * COMM_DeviceIoControl
1090 static inline NTSTATUS io_control(HANDLE hDevice,
1091 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1092 PVOID UserApcContext,
1093 PIO_STATUS_BLOCK piosb,
1094 ULONG dwIoControlCode,
1095 LPVOID lpInBuffer, DWORD nInBufferSize,
1096 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1098 DWORD sz = 0, access = FILE_READ_DATA;
1099 NTSTATUS status = STATUS_SUCCESS;
1100 int fd = -1, needs_close = 0;
1101 enum server_fd_type type;
1103 TRACE("%p %s %p %d %p %d %p\n",
1104 hDevice, iocode2str(dwIoControlCode), lpInBuffer, nInBufferSize,
1105 lpOutBuffer, nOutBufferSize, piosb);
1107 switch (dwIoControlCode)
1109 case IOCTL_SERIAL_GET_TIMEOUTS:
1110 case IOCTL_SERIAL_SET_TIMEOUTS:
1111 case IOCTL_SERIAL_GET_WAIT_MASK:
1112 case IOCTL_SERIAL_SET_WAIT_MASK:
1113 /* these are handled on the server side */
1114 return STATUS_NOT_SUPPORTED;
1117 piosb->Information = 0;
1119 if ((status = server_get_unix_fd( hDevice, access, &fd, &needs_close, &type, NULL )))
1120 goto error;
1121 if (type != FD_TYPE_SERIAL)
1123 if (needs_close) close( fd );
1124 status = STATUS_OBJECT_TYPE_MISMATCH;
1125 goto error;
1128 switch (dwIoControlCode)
1130 case IOCTL_SERIAL_CLR_DTR:
1131 #ifdef TIOCM_DTR
1132 if (whack_modem(fd, ~TIOCM_DTR, 0) == -1) status = FILE_GetNtStatus();
1133 #else
1134 status = STATUS_NOT_SUPPORTED;
1135 #endif
1136 break;
1137 case IOCTL_SERIAL_CLR_RTS:
1138 #ifdef TIOCM_RTS
1139 if (whack_modem(fd, ~TIOCM_RTS, 0) == -1) status = FILE_GetNtStatus();
1140 #else
1141 status = STATUS_NOT_SUPPORTED;
1142 #endif
1143 break;
1144 case IOCTL_SERIAL_GET_BAUD_RATE:
1145 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_BAUD_RATE))
1147 if (!(status = get_baud_rate(fd, lpOutBuffer)))
1148 sz = sizeof(SERIAL_BAUD_RATE);
1150 else
1151 status = STATUS_INVALID_PARAMETER;
1152 break;
1153 case IOCTL_SERIAL_GET_CHARS:
1154 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_CHARS))
1156 if (!(status = get_special_chars(fd, lpOutBuffer)))
1157 sz = sizeof(SERIAL_CHARS);
1159 else
1160 status = STATUS_INVALID_PARAMETER;
1161 break;
1162 case IOCTL_SERIAL_GET_COMMSTATUS:
1163 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_STATUS))
1165 if (!(status = get_status(fd, lpOutBuffer)))
1166 sz = sizeof(SERIAL_STATUS);
1168 else status = STATUS_INVALID_PARAMETER;
1169 break;
1170 case IOCTL_SERIAL_GET_HANDFLOW:
1171 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_HANDFLOW))
1173 if (!(status = get_hand_flow(fd, lpOutBuffer)))
1174 sz = sizeof(SERIAL_HANDFLOW);
1176 else
1177 status = STATUS_INVALID_PARAMETER;
1178 break;
1179 case IOCTL_SERIAL_GET_LINE_CONTROL:
1180 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_LINE_CONTROL))
1182 if (!(status = get_line_control(fd, lpOutBuffer)))
1183 sz = sizeof(SERIAL_LINE_CONTROL);
1185 else
1186 status = STATUS_INVALID_PARAMETER;
1187 break;
1188 case IOCTL_SERIAL_GET_MODEMSTATUS:
1189 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1191 if (!(status = get_modem_status(fd, lpOutBuffer)))
1192 sz = sizeof(DWORD);
1194 else status = STATUS_INVALID_PARAMETER;
1195 break;
1196 case IOCTL_SERIAL_IMMEDIATE_CHAR:
1197 if (lpInBuffer && nInBufferSize == sizeof(CHAR))
1198 status = xmit_immediate(hDevice, fd, lpInBuffer);
1199 else
1200 status = STATUS_INVALID_PARAMETER;
1201 break;
1202 case IOCTL_SERIAL_PURGE:
1203 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1204 status = purge(fd, *(DWORD*)lpInBuffer);
1205 else
1206 status = STATUS_INVALID_PARAMETER;
1207 break;
1208 case IOCTL_SERIAL_RESET_DEVICE:
1209 FIXME("Unsupported\n");
1210 break;
1211 case IOCTL_SERIAL_SET_BAUD_RATE:
1212 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_BAUD_RATE))
1213 status = set_baud_rate(fd, lpInBuffer);
1214 else
1215 status = STATUS_INVALID_PARAMETER;
1216 break;
1217 case IOCTL_SERIAL_SET_BREAK_OFF:
1218 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1219 if (ioctl(fd, TIOCCBRK, 0) == -1)
1221 TRACE("ioctl failed\n");
1222 status = FILE_GetNtStatus();
1224 #else
1225 FIXME("ioctl not available\n");
1226 status = STATUS_NOT_SUPPORTED;
1227 #endif
1228 break;
1229 case IOCTL_SERIAL_SET_BREAK_ON:
1230 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1231 if (ioctl(fd, TIOCSBRK, 0) == -1)
1233 TRACE("ioctl failed\n");
1234 status = FILE_GetNtStatus();
1236 #else
1237 FIXME("ioctl not available\n");
1238 status = STATUS_NOT_SUPPORTED;
1239 #endif
1240 break;
1241 case IOCTL_SERIAL_SET_CHARS:
1242 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_CHARS))
1243 status = set_special_chars(fd, lpInBuffer);
1244 else
1245 status = STATUS_INVALID_PARAMETER;
1246 break;
1247 case IOCTL_SERIAL_SET_DTR:
1248 #ifdef TIOCM_DTR
1249 if (whack_modem(fd, 0, TIOCM_DTR) == -1) status = FILE_GetNtStatus();
1250 #else
1251 status = STATUS_NOT_SUPPORTED;
1252 #endif
1253 break;
1254 case IOCTL_SERIAL_SET_HANDFLOW:
1255 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_HANDFLOW))
1256 status = set_handflow(fd, lpInBuffer);
1257 else
1258 status = STATUS_INVALID_PARAMETER;
1259 break;
1260 case IOCTL_SERIAL_SET_LINE_CONTROL:
1261 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_LINE_CONTROL))
1262 status = set_line_control(fd, lpInBuffer);
1263 else
1264 status = STATUS_INVALID_PARAMETER;
1265 break;
1266 case IOCTL_SERIAL_SET_QUEUE_SIZE:
1267 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_QUEUE_SIZE))
1268 status = set_queue_size(fd, lpInBuffer);
1269 else
1270 status = STATUS_INVALID_PARAMETER;
1271 break;
1272 case IOCTL_SERIAL_SET_RTS:
1273 #ifdef TIOCM_RTS
1274 if (whack_modem(fd, 0, TIOCM_RTS) == -1) status = FILE_GetNtStatus();
1275 #else
1276 status = STATUS_NOT_SUPPORTED;
1277 #endif
1278 break;
1279 case IOCTL_SERIAL_SET_XOFF:
1280 status = set_XOff(fd);
1281 break;
1282 case IOCTL_SERIAL_SET_XON:
1283 status = set_XOn(fd);
1284 break;
1285 case IOCTL_SERIAL_WAIT_ON_MASK:
1286 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1288 if (!(status = wait_on(hDevice, fd, hEvent, piosb, lpOutBuffer)))
1289 sz = sizeof(DWORD);
1291 else
1292 status = STATUS_INVALID_PARAMETER;
1293 break;
1294 default:
1295 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1296 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
1297 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
1298 sz = 0;
1299 status = STATUS_INVALID_PARAMETER;
1300 break;
1302 if (needs_close) close( fd );
1303 error:
1304 piosb->u.Status = status;
1305 piosb->Information = sz;
1306 if (hEvent && status != STATUS_PENDING) NtSetEvent(hEvent, NULL);
1307 return status;
1310 NTSTATUS COMM_DeviceIoControl(HANDLE hDevice,
1311 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1312 PVOID UserApcContext,
1313 PIO_STATUS_BLOCK piosb,
1314 ULONG dwIoControlCode,
1315 LPVOID lpInBuffer, DWORD nInBufferSize,
1316 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1318 NTSTATUS status;
1320 if (dwIoControlCode == IOCTL_SERIAL_WAIT_ON_MASK)
1322 HANDLE hev = hEvent;
1324 /* this is an ioctl we implement in a non blocking way if hEvent is not
1325 * null
1326 * so we have to explicitly wait if no hEvent is provided
1328 if (!hev)
1330 OBJECT_ATTRIBUTES attr;
1332 attr.Length = sizeof(attr);
1333 attr.RootDirectory = 0;
1334 attr.ObjectName = NULL;
1335 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1336 attr.SecurityDescriptor = NULL;
1337 attr.SecurityQualityOfService = NULL;
1338 status = NtCreateEvent(&hev, EVENT_ALL_ACCESS, &attr, SynchronizationEvent, FALSE);
1340 if (status) return status;
1342 status = io_control(hDevice, hev, UserApcRoutine, UserApcContext,
1343 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1344 lpOutBuffer, nOutBufferSize);
1345 if (hev != hEvent)
1347 if (status == STATUS_PENDING)
1349 NtWaitForSingleObject(hev, FALSE, NULL);
1350 status = STATUS_SUCCESS;
1352 NtClose(hev);
1355 else status = io_control(hDevice, hEvent, UserApcRoutine, UserApcContext,
1356 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1357 lpOutBuffer, nOutBufferSize);
1358 return status;
1361 NTSTATUS COMM_FlushBuffersFile( int fd )
1363 #ifdef HAVE_TCDRAIN
1364 while (tcdrain( fd ) == -1)
1366 if (errno != EINTR) return FILE_GetNtStatus();
1368 return STATUS_SUCCESS;
1369 #elif defined(TIOCDRAIN)
1370 while (ioctl( fd, TIOCDRAIN ) == -1)
1372 if (errno != EINTR) return FILE_GetNtStatus();
1374 return STATUS_SUCCESS;
1375 #elif defined(TCSBRK)
1376 while (ioctl( fd, TCSBRK, 1 ) == -1)
1378 if (errno != EINTR) return FILE_GetNtStatus();
1380 return STATUS_SUCCESS;
1381 #else
1382 ERR( "not supported\n" );
1383 return STATUS_NOT_IMPLEMENTED;
1384 #endif