Release 1.7.39.
[wine.git] / dlls / ntdll / serial.c
blob91d9be9c8fed285c59704e86c69d8a13e9dbbc89
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 NTSTATUS get_timeouts(HANDLE handle, SERIAL_TIMEOUTS* st)
395 NTSTATUS status;
396 SERVER_START_REQ( get_serial_info )
398 req->handle = wine_server_obj_handle( handle );
399 req->flags = 0;
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;
409 SERVER_END_REQ;
410 return status;
413 static void stop_waiting( HANDLE handle )
415 NTSTATUS status;
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);
424 SERVER_END_REQ;
427 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD *mask, DWORD *cookie, DWORD *pending_write, BOOL start_wait)
429 NTSTATUS status;
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;
443 SERVER_END_REQ;
444 return status;
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)
463 struct termios port;
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;
476 case 110:
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;
481 case 300:
482 case CBR_300: cfsetospeed( &port, B300 ); break;
483 case 600:
484 case CBR_600: cfsetospeed( &port, B600 ); break;
485 case 1200:
486 case CBR_1200: cfsetospeed( &port, B1200 ); break;
487 case 1800: cfsetospeed( &port, B1800 ); break;
488 case 2400:
489 case CBR_2400: cfsetospeed( &port, B2400 ); break;
490 case 4800:
491 case CBR_4800: cfsetospeed( &port, B4800 ); break;
492 case 9600:
493 case CBR_9600: cfsetospeed( &port, B9600 ); break;
494 case 19200:
495 case CBR_19200: cfsetospeed( &port, B19200 ); break;
496 case 38400:
497 case CBR_38400: cfsetospeed( &port, B38400 ); break;
498 #ifdef B57600
499 case 57600: cfsetospeed( &port, B57600 ); break;
500 #endif
501 #ifdef B115200
502 case 115200: cfsetospeed( &port, B115200 ); break;
503 #endif
504 #ifdef B230400
505 case 230400: cfsetospeed( &port, B230400 ); break;
506 #endif
507 #ifdef B460800
508 case 460800: cfsetospeed( &port, B460800 ); break;
509 #endif
510 #ifdef B500000
511 case 500000: cfsetospeed( &port, B500000 ); break;
512 #endif
513 #ifdef B921600
514 case 921600: cfsetospeed( &port, B921600 ); break;
515 #endif
516 #ifdef B1000000
517 case 1000000: cfsetospeed( &port, B1000000 ); break;
518 #endif
519 #ifdef B1152000
520 case 1152000: cfsetospeed( &port, B1152000 ); break;
521 #endif
522 #ifdef B1500000
523 case 1500000: cfsetospeed( &port, B1500000 ); break;
524 #endif
525 #ifdef B2000000
526 case 2000000: cfsetospeed( &port, B2000000 ); break;
527 #endif
528 #ifdef B2500000
529 case 2500000: cfsetospeed( &port, B2500000 ); break;
530 #endif
531 #ifdef B3000000
532 case 3000000: cfsetospeed( &port, B3000000 ); break;
533 #endif
534 #ifdef B3500000
535 case 3500000: cfsetospeed( &port, B3500000 ); break;
536 #endif
537 #ifdef B4000000
538 case 4000000: cfsetospeed( &port, B4000000 ); break;
539 #endif
540 default:
541 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
543 struct serial_struct nuts;
544 int arby;
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 );
562 break;
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)
579 #ifdef TIOCMGET
580 unsigned int mstat, okay;
581 okay = ioctl(fd, TIOCMGET, &mstat);
582 if (okay) return okay;
583 if (andy) mstat &= andy;
584 mstat |= orrie;
585 return ioctl(fd, TIOCMSET, &mstat);
586 #else
587 return 0;
588 #endif
591 static NTSTATUS set_handflow(int fd, const SERIAL_HANDFLOW* shf)
593 struct termios port;
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();
605 #ifdef CRTSCTS
606 if ((shf->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
607 (shf->FlowReplace & SERIAL_RTS_HANDSHAKE))
609 port.c_cflag |= CRTSCTS;
610 TRACE("CRTSCTS\n");
612 else
613 port.c_cflag &= ~CRTSCTS;
614 #endif
615 #ifdef TIOCM_DTR
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);
621 else
622 whack_modem(fd, 0, TIOCM_DTR);
623 #endif
624 #ifdef TIOCM_RTS
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);
629 else
630 whack_modem(fd, 0, TIOCM_RTS);
632 #endif
634 if (shf->FlowReplace & SERIAL_AUTO_RECEIVE)
635 port.c_iflag |= IXOFF;
636 else
637 port.c_iflag &= ~IXOFF;
638 if (shf->FlowReplace & SERIAL_AUTO_TRANSMIT)
639 port.c_iflag |= IXON;
640 else
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)
653 struct termios port;
654 unsigned bytesize, stopbits;
656 if (tcgetattr(fd, &port) == -1)
658 ERR("tcgetattr error '%s'\n", strerror(errno));
659 return FILE_GetNtStatus();
662 #ifdef IMAXBEL
663 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK|IMAXBEL);
664 #else
665 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK);
666 #endif
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;
682 #ifdef CMSPAR
683 port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
684 #else
685 port.c_cflag &= ~(PARENB | PARODD);
686 #endif
688 /* make sure that reads don't block */
689 port.c_cc[VMIN] = 0;
690 port.c_cc[VTIME] = 0;
692 switch (slc->Parity)
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;
697 #ifdef CMSPAR
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;
701 #else
702 /* try the POSIX way */
703 case MARKPARITY:
704 if (slc->StopBits == ONESTOPBIT)
706 stopbits = TWOSTOPBITS;
707 port.c_iflag &= ~INPCK;
709 else
711 FIXME("Cannot set MARK Parity\n");
712 return STATUS_NOT_SUPPORTED;
714 break;
715 case SPACEPARITY:
716 if (slc->WordLength < 8)
718 bytesize +=1;
719 port.c_iflag &= ~INPCK;
721 else
723 FIXME("Cannot set SPACE Parity\n");
724 return STATUS_NOT_SUPPORTED;
726 break;
727 #endif
728 default:
729 FIXME("Parity %d is not supported\n", slc->Parity);
730 return STATUS_NOT_SUPPORTED;
733 port.c_cflag &= ~CSIZE;
734 switch (bytesize)
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;
740 default:
741 FIXME("ByteSize %d is not supported\n", bytesize);
742 return STATUS_NOT_SUPPORTED;
745 switch (stopbits)
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;
750 default:
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)
771 struct termios port;
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)
796 NTSTATUS status;
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 );
809 SERVER_END_REQ;
810 return status;
813 static NTSTATUS set_wait_mask(HANDLE hDevice, DWORD mask)
815 NTSTATUS status;
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 );
824 SERVER_END_REQ;
825 return status;
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;
852 /* serial_irq_info
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;
863 }serial_irq_info;
865 /***********************************************************************
866 * Data needed by the thread polling for the changing CommEvent
868 typedef struct async_commio
870 HANDLE hDevice;
871 DWORD* events;
872 IO_STATUS_BLOCK* iosb;
873 HANDLE hEvent;
874 DWORD evtmask;
875 DWORD cookie;
876 DWORD mstat;
877 DWORD pending_write;
878 serial_irq_info irq_info;
879 } async_commio;
881 /***********************************************************************
882 * Get extended interrupt count info, needed for wait_on
884 static NTSTATUS get_irq_info(int fd, serial_irq_info *irq_info)
886 int out;
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;
900 else
902 TRACE("TIOCGICOUNT err %s\n", strerror(errno));
903 memset(irq_info,0, sizeof(serial_irq_info));
905 #else
906 memset(irq_info,0, sizeof(serial_irq_info));
907 #endif
909 irq_info->temt = 0;
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));
919 #endif
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();
928 #endif
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)
958 queue = 0;
959 #ifdef TIOCINQ
960 if (ioctl(fd, TIOCINQ, &queue))
961 WARN("TIOCINQ returned error\n");
962 #endif
963 if (queue)
964 ret |= EV_RXCHAR;
966 if (mask & EV_TXEMPTY)
968 if ((!old->temt || pending_write) && new->temt)
969 ret |= EV_TXEMPTY;
971 return ret & mask;
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;
984 int fd, needs_close;
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;
990 LARGE_INTEGER time;
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;
997 for (;;)
1000 * TIOCMIWAIT is not adequate
1002 * FIXME:
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;
1011 break;
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;
1021 break;
1024 if (needs_close) close( fd );
1026 if (commio->iosb)
1028 if (*commio->events)
1030 commio->iosb->u.Status = STATUS_SUCCESS;
1031 commio->iosb->Information = sizeof(DWORD);
1033 else
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);
1039 return 0;
1042 static NTSTATUS wait_on(HANDLE hDevice, int fd, HANDLE hEvent, PIO_STATUS_BLOCK piosb, DWORD* events)
1044 async_commio* commio;
1045 NTSTATUS status;
1047 if ((status = NtResetEvent(hEvent, NULL)))
1048 return status;
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);
1059 if (status)
1061 RtlFreeHeap(GetProcessHeap(), 0, commio);
1062 return status;
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)
1070 goto error_caps;
1071 #endif
1072 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1073 if (commio->evtmask & EV_TXEMPTY)
1074 goto error_caps;
1075 #endif
1076 #if !defined(TIOCMGET)
1077 if (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD))
1078 goto error_caps;
1079 #endif
1080 #if !defined(TIOCM_CTS)
1081 if (commio->evtmask & EV_CTS)
1082 goto error_caps;
1083 #endif
1084 #if !defined(TIOCM_DSR)
1085 if (commio->evtmask & EV_DSR)
1086 goto error_caps;
1087 #endif
1088 #if !defined(TIOCM_RNG)
1089 if (commio->evtmask & EV_RING)
1090 goto error_caps;
1091 #endif
1092 #if !defined(TIOCM_CAR)
1093 if (commio->evtmask & EV_RLSD)
1094 goto error_caps;
1095 #endif
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)))
1101 goto out_now;
1103 if ((status = get_modem_status(fd, &commio->mstat)) &&
1104 (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD)))
1105 goto out_now;
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);
1111 if (*events)
1113 status = STATUS_SUCCESS;
1114 goto out_now;
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)
1123 error_caps:
1124 FIXME("Returning error because of missing capabilities\n");
1125 status = STATUS_INVALID_PARAMETER;
1126 #endif
1127 out_now:
1128 stop_waiting(commio->hDevice);
1129 RtlFreeHeap(GetProcessHeap(), 0, commio);
1130 return status;
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 )))
1170 goto error;
1171 if (type != FD_TYPE_SERIAL)
1173 if (needs_close) close( fd );
1174 status = STATUS_OBJECT_TYPE_MISMATCH;
1175 goto error;
1179 switch (dwIoControlCode)
1181 case IOCTL_SERIAL_CLR_DTR:
1182 #ifdef TIOCM_DTR
1183 if (whack_modem(fd, ~TIOCM_DTR, 0) == -1) status = FILE_GetNtStatus();
1184 #else
1185 status = STATUS_NOT_SUPPORTED;
1186 #endif
1187 break;
1188 case IOCTL_SERIAL_CLR_RTS:
1189 #ifdef TIOCM_RTS
1190 if (whack_modem(fd, ~TIOCM_RTS, 0) == -1) status = FILE_GetNtStatus();
1191 #else
1192 status = STATUS_NOT_SUPPORTED;
1193 #endif
1194 break;
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);
1201 else
1202 status = STATUS_INVALID_PARAMETER;
1203 break;
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);
1210 else
1211 status = STATUS_INVALID_PARAMETER;
1212 break;
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;
1220 break;
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);
1227 else
1228 status = STATUS_INVALID_PARAMETER;
1229 break;
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);
1236 else
1237 status = STATUS_INVALID_PARAMETER;
1238 break;
1239 case IOCTL_SERIAL_GET_MODEMSTATUS:
1240 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1242 if (!(status = get_modem_status(fd, lpOutBuffer)))
1243 sz = sizeof(DWORD);
1245 else status = STATUS_INVALID_PARAMETER;
1246 break;
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);
1253 else
1254 status = STATUS_INVALID_PARAMETER;
1255 break;
1256 case IOCTL_SERIAL_GET_WAIT_MASK:
1257 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1259 if (!(status = get_wait_mask(hDevice, lpOutBuffer, NULL, NULL, FALSE)))
1260 sz = sizeof(DWORD);
1262 else
1263 status = STATUS_INVALID_PARAMETER;
1264 break;
1265 case IOCTL_SERIAL_IMMEDIATE_CHAR:
1266 if (lpInBuffer && nInBufferSize == sizeof(CHAR))
1267 status = xmit_immediate(hDevice, fd, lpInBuffer);
1268 else
1269 status = STATUS_INVALID_PARAMETER;
1270 break;
1271 case IOCTL_SERIAL_PURGE:
1272 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1273 status = purge(fd, *(DWORD*)lpInBuffer);
1274 else
1275 status = STATUS_INVALID_PARAMETER;
1276 break;
1277 case IOCTL_SERIAL_RESET_DEVICE:
1278 FIXME("Unsupported\n");
1279 break;
1280 case IOCTL_SERIAL_SET_BAUD_RATE:
1281 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_BAUD_RATE))
1282 status = set_baud_rate(fd, lpInBuffer);
1283 else
1284 status = STATUS_INVALID_PARAMETER;
1285 break;
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();
1293 #else
1294 FIXME("ioctl not available\n");
1295 status = STATUS_NOT_SUPPORTED;
1296 #endif
1297 break;
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();
1305 #else
1306 FIXME("ioctl not available\n");
1307 status = STATUS_NOT_SUPPORTED;
1308 #endif
1309 break;
1310 case IOCTL_SERIAL_SET_CHARS:
1311 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_CHARS))
1312 status = set_special_chars(fd, lpInBuffer);
1313 else
1314 status = STATUS_INVALID_PARAMETER;
1315 break;
1316 case IOCTL_SERIAL_SET_DTR:
1317 #ifdef TIOCM_DTR
1318 if (whack_modem(fd, 0, TIOCM_DTR) == -1) status = FILE_GetNtStatus();
1319 #else
1320 status = STATUS_NOT_SUPPORTED;
1321 #endif
1322 break;
1323 case IOCTL_SERIAL_SET_HANDFLOW:
1324 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_HANDFLOW))
1325 status = set_handflow(fd, lpInBuffer);
1326 else
1327 status = STATUS_INVALID_PARAMETER;
1328 break;
1329 case IOCTL_SERIAL_SET_LINE_CONTROL:
1330 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_LINE_CONTROL))
1331 status = set_line_control(fd, lpInBuffer);
1332 else
1333 status = STATUS_INVALID_PARAMETER;
1334 break;
1335 case IOCTL_SERIAL_SET_QUEUE_SIZE:
1336 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_QUEUE_SIZE))
1337 status = set_queue_size(fd, lpInBuffer);
1338 else
1339 status = STATUS_INVALID_PARAMETER;
1340 break;
1341 case IOCTL_SERIAL_SET_RTS:
1342 #ifdef TIOCM_RTS
1343 if (whack_modem(fd, 0, TIOCM_RTS) == -1) status = FILE_GetNtStatus();
1344 #else
1345 status = STATUS_NOT_SUPPORTED;
1346 #endif
1347 break;
1348 case IOCTL_SERIAL_SET_TIMEOUTS:
1349 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_TIMEOUTS))
1350 status = set_timeouts(hDevice, lpInBuffer);
1351 else
1352 status = STATUS_INVALID_PARAMETER;
1353 break;
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;
1360 break;
1361 case IOCTL_SERIAL_SET_XOFF:
1362 status = set_XOff(fd);
1363 break;
1364 case IOCTL_SERIAL_SET_XON:
1365 status = set_XOn(fd);
1366 break;
1367 case IOCTL_SERIAL_WAIT_ON_MASK:
1368 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1370 if (!(status = wait_on(hDevice, fd, hEvent, piosb, lpOutBuffer)))
1371 sz = sizeof(DWORD);
1373 else
1374 status = STATUS_INVALID_PARAMETER;
1375 break;
1376 default:
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);
1380 sz = 0;
1381 status = STATUS_INVALID_PARAMETER;
1382 break;
1384 if (needs_close) close( fd );
1385 error:
1386 piosb->u.Status = status;
1387 piosb->Information = sz;
1388 if (hEvent && status != STATUS_PENDING) NtSetEvent(hEvent, NULL);
1389 return status;
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)
1400 NTSTATUS status;
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
1407 * null
1408 * so we have to explicitly wait if no hEvent is provided
1410 if (!hev)
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);
1427 if (hev != hEvent)
1429 if (status == STATUS_PENDING)
1431 NtWaitForSingleObject(hev, FALSE, NULL);
1432 status = STATUS_SUCCESS;
1434 NtClose(hev);
1437 else status = io_control(hDevice, hEvent, UserApcRoutine, UserApcContext,
1438 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1439 lpOutBuffer, nOutBufferSize);
1440 return status;
1443 NTSTATUS COMM_FlushBuffersFile( int fd )
1445 #ifdef HAVE_TCDRAIN
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;
1463 #else
1464 ERR( "not supported\n" );
1465 return STATUS_NOT_IMPLEMENTED;
1466 #endif