advapi32: Don't shadow keychain_item in mac_write_credential.
[wine/multimedia.git] / dlls / ntdll / serial.c
blobb03ac612f5afb077437ae240e42678fd5fedbf96
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 #define NONAMELESSUNION
61 #define NONAMELESSSTRUCT
62 #include "ntstatus.h"
63 #define WIN32_NO_STATUS
64 #include "windef.h"
65 #include "winternl.h"
66 #include "winioctl.h"
67 #include "ddk/ntddser.h"
68 #include "ntdll_misc.h"
69 #include "wine/server.h"
70 #include "wine/library.h"
71 #include "wine/debug.h"
73 #ifdef HAVE_LINUX_SERIAL_H
74 #ifdef HAVE_ASM_TYPES_H
75 #include <asm/types.h>
76 #endif
77 #include <linux/serial.h>
78 #endif
80 #if !defined(TIOCINQ) && defined(FIONREAD)
81 #define TIOCINQ FIONREAD
82 #endif
84 WINE_DEFAULT_DEBUG_CHANNEL(comm);
86 static const char* iocode2str(DWORD ioc)
88 switch (ioc)
90 #define X(x) case (x): return #x
91 X(IOCTL_SERIAL_CLEAR_STATS);
92 X(IOCTL_SERIAL_CLR_DTR);
93 X(IOCTL_SERIAL_CLR_RTS);
94 X(IOCTL_SERIAL_CONFIG_SIZE);
95 X(IOCTL_SERIAL_GET_BAUD_RATE);
96 X(IOCTL_SERIAL_GET_CHARS);
97 X(IOCTL_SERIAL_GET_COMMSTATUS);
98 X(IOCTL_SERIAL_GET_DTRRTS);
99 X(IOCTL_SERIAL_GET_HANDFLOW);
100 X(IOCTL_SERIAL_GET_LINE_CONTROL);
101 X(IOCTL_SERIAL_GET_MODEM_CONTROL);
102 X(IOCTL_SERIAL_GET_MODEMSTATUS);
103 X(IOCTL_SERIAL_GET_PROPERTIES);
104 X(IOCTL_SERIAL_GET_STATS);
105 X(IOCTL_SERIAL_GET_TIMEOUTS);
106 X(IOCTL_SERIAL_GET_WAIT_MASK);
107 X(IOCTL_SERIAL_IMMEDIATE_CHAR);
108 X(IOCTL_SERIAL_LSRMST_INSERT);
109 X(IOCTL_SERIAL_PURGE);
110 X(IOCTL_SERIAL_RESET_DEVICE);
111 X(IOCTL_SERIAL_SET_BAUD_RATE);
112 X(IOCTL_SERIAL_SET_BREAK_ON);
113 X(IOCTL_SERIAL_SET_BREAK_OFF);
114 X(IOCTL_SERIAL_SET_CHARS);
115 X(IOCTL_SERIAL_SET_DTR);
116 X(IOCTL_SERIAL_SET_FIFO_CONTROL);
117 X(IOCTL_SERIAL_SET_HANDFLOW);
118 X(IOCTL_SERIAL_SET_LINE_CONTROL);
119 X(IOCTL_SERIAL_SET_MODEM_CONTROL);
120 X(IOCTL_SERIAL_SET_QUEUE_SIZE);
121 X(IOCTL_SERIAL_SET_RTS);
122 X(IOCTL_SERIAL_SET_TIMEOUTS);
123 X(IOCTL_SERIAL_SET_WAIT_MASK);
124 X(IOCTL_SERIAL_SET_XOFF);
125 X(IOCTL_SERIAL_SET_XON);
126 X(IOCTL_SERIAL_WAIT_ON_MASK);
127 X(IOCTL_SERIAL_XOFF_COUNTER);
128 #undef X
129 default: { static char tmp[32]; sprintf(tmp, "IOCTL_SERIAL_%d\n", ioc); return tmp; }
133 static NTSTATUS get_baud_rate(int fd, SERIAL_BAUD_RATE* sbr)
135 struct termios port;
136 int speed;
138 if (tcgetattr(fd, &port) == -1)
140 ERR("tcgetattr error '%s'\n", strerror(errno));
141 return FILE_GetNtStatus();
143 speed = cfgetospeed(&port);
144 switch (speed)
146 case B0: sbr->BaudRate = 0; break;
147 case B50: sbr->BaudRate = 50; break;
148 case B75: sbr->BaudRate = 75; break;
149 case B110: sbr->BaudRate = 110; break;
150 case B134: sbr->BaudRate = 134; break;
151 case B150: sbr->BaudRate = 150; break;
152 case B200: sbr->BaudRate = 200; break;
153 case B300: sbr->BaudRate = 300; break;
154 case B600: sbr->BaudRate = 600; break;
155 case B1200: sbr->BaudRate = 1200; break;
156 case B1800: sbr->BaudRate = 1800; break;
157 case B2400: sbr->BaudRate = 2400; break;
158 case B4800: sbr->BaudRate = 4800; break;
159 case B9600: sbr->BaudRate = 9600; break;
160 case B19200: sbr->BaudRate = 19200; break;
161 case B38400: sbr->BaudRate = 38400; break;
162 #ifdef B57600
163 case B57600: sbr->BaudRate = 57600; break;
164 #endif
165 #ifdef B115200
166 case B115200: sbr->BaudRate = 115200; break;
167 #endif
168 #ifdef B230400
169 case B230400: sbr->BaudRate = 230400; break;
170 #endif
171 #ifdef B460800
172 case B460800: sbr->BaudRate = 460800; break;
173 #endif
174 default:
175 ERR("unknown speed %x\n", speed);
176 return STATUS_INVALID_PARAMETER;
178 return STATUS_SUCCESS;
181 static NTSTATUS get_hand_flow(int fd, SERIAL_HANDFLOW* shf)
183 int stat = 0;
184 struct termios port;
186 if (tcgetattr(fd, &port) == -1)
188 ERR("tcgetattr error '%s'\n", strerror(errno));
189 return FILE_GetNtStatus();
191 /* termios does not support DTR/DSR flow control */
192 shf->ControlHandShake = 0;
193 shf->FlowReplace = 0;
194 #ifdef TIOCMGET
195 if (ioctl(fd, TIOCMGET, &stat) == -1)
197 WARN("ioctl error '%s'\n", strerror(errno));
198 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
199 shf->FlowReplace |= SERIAL_RTS_CONTROL;
201 #else
202 WARN("Setting DTR/RTS to enabled by default\n");
203 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
204 shf->FlowReplace |= SERIAL_RTS_CONTROL;
205 #endif
206 #ifdef TIOCM_DTR
207 if (stat & TIOCM_DTR)
208 #endif
209 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
210 #ifdef CRTSCTS
211 if (port.c_cflag & CRTSCTS)
213 shf->FlowReplace |= SERIAL_RTS_CONTROL;
214 shf->ControlHandShake |= SERIAL_CTS_HANDSHAKE;
216 else
217 #endif
219 #ifdef TIOCM_RTS
220 if (stat & TIOCM_RTS)
221 #endif
222 shf->FlowReplace |= SERIAL_RTS_CONTROL;
224 if (port.c_iflag & IXOFF)
225 shf->FlowReplace |= SERIAL_AUTO_RECEIVE;
226 if (port.c_iflag & IXON)
227 shf->FlowReplace |= SERIAL_AUTO_TRANSMIT;
229 shf->XonLimit = 10;
230 shf->XoffLimit = 10;
231 return STATUS_SUCCESS;
234 static NTSTATUS get_line_control(int fd, SERIAL_LINE_CONTROL* slc)
236 struct termios port;
238 if (tcgetattr(fd, &port) == -1)
240 ERR("tcgetattr error '%s'\n", strerror(errno));
241 return FILE_GetNtStatus();
244 #ifdef CMSPAR
245 switch (port.c_cflag & (PARENB | PARODD | CMSPAR))
246 #else
247 switch (port.c_cflag & (PARENB | PARODD))
248 #endif
250 case 0: slc->Parity = NOPARITY; break;
251 case PARENB: slc->Parity = EVENPARITY; break;
252 case PARENB|PARODD: slc->Parity = ODDPARITY; break;
253 #ifdef CMSPAR
254 case PARENB|CMSPAR: slc->Parity = MARKPARITY; break;
255 case PARENB|PARODD|CMSPAR: slc->Parity = SPACEPARITY; break;
256 #endif
258 switch (port.c_cflag & CSIZE)
260 case CS5: slc->WordLength = 5; break;
261 case CS6: slc->WordLength = 6; break;
262 case CS7: slc->WordLength = 7; break;
263 case CS8: slc->WordLength = 8; break;
264 default: ERR("unknown size %x\n", (UINT)(port.c_cflag & CSIZE));
267 if (port.c_cflag & CSTOPB)
269 if (slc->WordLength == 5)
270 slc->StopBits = ONE5STOPBITS;
271 else
272 slc->StopBits = TWOSTOPBITS;
274 else
275 slc->StopBits = ONESTOPBIT;
277 return STATUS_SUCCESS;
280 static NTSTATUS get_modem_status(int fd, DWORD* lpModemStat)
282 NTSTATUS status = STATUS_NOT_SUPPORTED;
283 int mstat;
285 *lpModemStat = 0;
286 #ifdef TIOCMGET
287 if (!ioctl(fd, TIOCMGET, &mstat))
289 #ifdef TIOCM_CTS
290 if (mstat & TIOCM_CTS) *lpModemStat |= MS_CTS_ON;
291 #endif
292 #ifdef TIOCM_DSR
293 if (mstat & TIOCM_DSR) *lpModemStat |= MS_DSR_ON;
294 #endif
295 #ifdef TIOCM_RNG
296 if (mstat & TIOCM_RNG) *lpModemStat |= MS_RING_ON;
297 #endif
298 #ifdef TIOCM_CAR
299 /* FIXME: Not really sure about RLSD UB 990810 */
300 if (mstat & TIOCM_CAR) *lpModemStat |= MS_RLSD_ON;
301 #endif
302 TRACE("%04x -> %s%s%s%s\n", mstat,
303 (*lpModemStat & MS_RLSD_ON) ? "MS_RLSD_ON " : "",
304 (*lpModemStat & MS_RING_ON) ? "MS_RING_ON " : "",
305 (*lpModemStat & MS_DSR_ON) ? "MS_DSR_ON " : "",
306 (*lpModemStat & MS_CTS_ON) ? "MS_CTS_ON " : "");
307 return STATUS_SUCCESS;
309 WARN("TIOCMGET err %s\n", strerror(errno));
310 status = FILE_GetNtStatus();
311 #endif
312 return status;
315 static NTSTATUS get_special_chars(int fd, SERIAL_CHARS* sc)
317 struct termios port;
319 if (tcgetattr(fd, &port) == -1)
321 ERR("tcgetattr error '%s'\n", strerror(errno));
322 return FILE_GetNtStatus();
324 sc->EofChar = port.c_cc[VEOF];
325 sc->ErrorChar = 0xFF;
326 sc->BreakChar = 0; /* FIXME */
327 sc->EventChar = 0; /* FIXME */
328 sc->XonChar = port.c_cc[VSTART];
329 sc->XoffChar = port.c_cc[VSTOP];
331 return STATUS_SUCCESS;
334 static NTSTATUS get_status(int fd, SERIAL_STATUS* ss)
336 NTSTATUS status = STATUS_SUCCESS;
338 ss->Errors = 0;
339 ss->HoldReasons = 0;
340 ss->EofReceived = FALSE;
341 ss->WaitForImmediate = FALSE;
342 #ifdef TIOCOUTQ
343 if (ioctl(fd, TIOCOUTQ, &ss->AmountInOutQueue) == -1)
345 WARN("ioctl returned error\n");
346 status = FILE_GetNtStatus();
348 #else
349 ss->AmountInOutQueue = 0; /* FIXME: find a different way to find out */
350 #endif
352 #ifdef TIOCINQ
353 if (ioctl(fd, TIOCINQ, &ss->AmountInInQueue))
355 WARN("ioctl returned error\n");
356 status = FILE_GetNtStatus();
358 #else
359 ss->AmountInInQueue = 0; /* FIXME: find a different way to find out */
360 #endif
361 return status;
364 static NTSTATUS get_timeouts(HANDLE handle, SERIAL_TIMEOUTS* st)
366 NTSTATUS status;
367 SERVER_START_REQ( get_serial_info )
369 req->handle = wine_server_obj_handle( handle );
370 req->flags = 0;
371 if (!(status = wine_server_call( req )))
373 st->ReadIntervalTimeout = reply->readinterval;
374 st->ReadTotalTimeoutMultiplier = reply->readmult;
375 st->ReadTotalTimeoutConstant = reply->readconst;
376 st->WriteTotalTimeoutMultiplier = reply->writemult;
377 st->WriteTotalTimeoutConstant = reply->writeconst;
380 SERVER_END_REQ;
381 return status;
384 static void stop_waiting( HANDLE handle )
386 NTSTATUS status;
388 SERVER_START_REQ( set_serial_info )
390 req->handle = wine_server_obj_handle( handle );
391 req->flags = SERIALINFO_PENDING_WAIT;
392 if ((status = wine_server_call( req )))
393 ERR("failed to clear waiting state: %#x\n", status);
395 SERVER_END_REQ;
398 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD *mask, DWORD *cookie, DWORD *pending_write, BOOL start_wait)
400 NTSTATUS status;
402 SERVER_START_REQ( get_serial_info )
404 req->handle = wine_server_obj_handle( hDevice );
405 req->flags = pending_write ? SERIALINFO_PENDING_WRITE : 0;
406 if (start_wait) req->flags |= SERIALINFO_PENDING_WAIT;
407 if (!(status = wine_server_call( req )))
409 *mask = reply->eventmask;
410 if (cookie) *cookie = reply->cookie;
411 if (pending_write) *pending_write = reply->pending_write;
414 SERVER_END_REQ;
415 return status;
418 static NTSTATUS purge(int fd, DWORD flags)
421 ** not exactly sure how these are different
422 ** Perhaps if we had our own internal queues, one flushes them
423 ** and the other flushes the kernel's buffers.
425 if (flags & PURGE_TXABORT) tcflush(fd, TCOFLUSH);
426 if (flags & PURGE_RXABORT) tcflush(fd, TCIFLUSH);
427 if (flags & PURGE_TXCLEAR) tcflush(fd, TCOFLUSH);
428 if (flags & PURGE_RXCLEAR) tcflush(fd, TCIFLUSH);
429 return STATUS_SUCCESS;
432 static NTSTATUS set_baud_rate(int fd, const SERIAL_BAUD_RATE* sbr)
434 struct termios port;
436 if (tcgetattr(fd, &port) == -1)
438 ERR("tcgetattr error '%s'\n", strerror(errno));
439 return FILE_GetNtStatus();
442 switch (sbr->BaudRate)
444 case 0: cfsetospeed( &port, B0 ); break;
445 case 50: cfsetospeed( &port, B50 ); break;
446 case 75: cfsetospeed( &port, B75 ); break;
447 case 110:
448 case CBR_110: cfsetospeed( &port, B110 ); break;
449 case 134: cfsetospeed( &port, B134 ); break;
450 case 150: cfsetospeed( &port, B150 ); break;
451 case 200: cfsetospeed( &port, B200 ); break;
452 case 300:
453 case CBR_300: cfsetospeed( &port, B300 ); break;
454 case 600:
455 case CBR_600: cfsetospeed( &port, B600 ); break;
456 case 1200:
457 case CBR_1200: cfsetospeed( &port, B1200 ); break;
458 case 1800: cfsetospeed( &port, B1800 ); break;
459 case 2400:
460 case CBR_2400: cfsetospeed( &port, B2400 ); break;
461 case 4800:
462 case CBR_4800: cfsetospeed( &port, B4800 ); break;
463 case 9600:
464 case CBR_9600: cfsetospeed( &port, B9600 ); break;
465 case 19200:
466 case CBR_19200: cfsetospeed( &port, B19200 ); break;
467 case 38400:
468 case CBR_38400: cfsetospeed( &port, B38400 ); break;
469 #ifdef B57600
470 case 57600: cfsetospeed( &port, B57600 ); break;
471 #endif
472 #ifdef B115200
473 case 115200: cfsetospeed( &port, B115200 ); break;
474 #endif
475 #ifdef B230400
476 case 230400: cfsetospeed( &port, B230400 ); break;
477 #endif
478 #ifdef B460800
479 case 460800: cfsetospeed( &port, B460800 ); break;
480 #endif
481 default:
482 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
484 struct serial_struct nuts;
485 int arby;
487 ioctl(fd, TIOCGSERIAL, &nuts);
488 nuts.custom_divisor = nuts.baud_base / sbr->BaudRate;
489 if (!(nuts.custom_divisor)) nuts.custom_divisor = 1;
490 arby = nuts.baud_base / nuts.custom_divisor;
491 nuts.flags &= ~ASYNC_SPD_MASK;
492 nuts.flags |= ASYNC_SPD_CUST;
493 WARN("You (or a program acting at your behest) have specified\n"
494 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
495 "which is as close as we can get by our present understanding of your\n"
496 "hardware. I hope you know what you are doing. Any disruption Wine\n"
497 "has caused to your linux system can be undone with setserial\n"
498 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
499 "reset it and it will probably recover.\n", sbr->BaudRate, arby);
500 ioctl(fd, TIOCSSERIAL, &nuts);
501 cfsetospeed( &port, B38400 );
503 break;
504 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
505 ERR("baudrate %d\n", sbr->BaudRate);
506 return STATUS_NOT_SUPPORTED;
507 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
509 cfsetispeed( &port, cfgetospeed(&port) );
510 if (tcsetattr(fd, TCSANOW, &port) == -1)
512 ERR("tcsetattr error '%s'\n", strerror(errno));
513 return FILE_GetNtStatus();
515 return STATUS_SUCCESS;
518 static int whack_modem(int fd, unsigned int andy, unsigned int orrie)
520 #ifdef TIOCMGET
521 unsigned int mstat, okay;
522 okay = ioctl(fd, TIOCMGET, &mstat);
523 if (okay) return okay;
524 if (andy) mstat &= andy;
525 mstat |= orrie;
526 return ioctl(fd, TIOCMSET, &mstat);
527 #else
528 return 0;
529 #endif
532 static NTSTATUS set_handflow(int fd, const SERIAL_HANDFLOW* shf)
534 struct termios port;
536 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE)) ==
537 (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
538 return STATUS_NOT_SUPPORTED;
540 if (tcgetattr(fd, &port) == -1)
542 ERR("tcgetattr error '%s'\n", strerror(errno));
543 return FILE_GetNtStatus();
546 #ifdef CRTSCTS
547 if ((shf->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
548 (shf->FlowReplace & SERIAL_RTS_HANDSHAKE))
550 port.c_cflag |= CRTSCTS;
551 TRACE("CRTSCTS\n");
553 else
554 port.c_cflag &= ~CRTSCTS;
555 #endif
556 #ifdef TIOCM_DTR
557 if (shf->ControlHandShake & SERIAL_DTR_HANDSHAKE)
559 WARN("DSR/DTR flow control not supported\n");
560 } else if (!(shf->ControlHandShake & SERIAL_DTR_CONTROL))
561 whack_modem(fd, ~TIOCM_DTR, 0);
562 else
563 whack_modem(fd, 0, TIOCM_DTR);
564 #endif
565 #ifdef TIOCM_RTS
566 if (!(shf->ControlHandShake & SERIAL_CTS_HANDSHAKE))
568 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL|SERIAL_RTS_HANDSHAKE)) == 0)
569 whack_modem(fd, ~TIOCM_RTS, 0);
570 else
571 whack_modem(fd, 0, TIOCM_RTS);
573 #endif
575 if (shf->FlowReplace & SERIAL_AUTO_RECEIVE)
576 port.c_iflag |= IXOFF;
577 else
578 port.c_iflag &= ~IXOFF;
579 if (shf->FlowReplace & SERIAL_AUTO_TRANSMIT)
580 port.c_iflag |= IXON;
581 else
582 port.c_iflag &= ~IXON;
583 if (tcsetattr(fd, TCSANOW, &port) == -1)
585 ERR("tcsetattr error '%s'\n", strerror(errno));
586 return FILE_GetNtStatus();
589 return STATUS_SUCCESS;
592 static NTSTATUS set_line_control(int fd, const SERIAL_LINE_CONTROL* slc)
594 struct termios port;
595 unsigned bytesize, stopbits;
597 if (tcgetattr(fd, &port) == -1)
599 ERR("tcgetattr error '%s'\n", strerror(errno));
600 return FILE_GetNtStatus();
603 #ifdef IMAXBEL
604 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK|IMAXBEL);
605 #else
606 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK);
607 #endif
608 port.c_iflag |= IGNBRK | INPCK;
609 port.c_oflag &= ~(OPOST);
610 port.c_cflag &= ~(HUPCL);
611 port.c_cflag |= CLOCAL | CREAD;
614 * on FreeBSD, turning off ICANON does not disable IEXTEN,
615 * so we must turn it off explicitly. No harm done on Linux.
617 port.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN);
618 port.c_lflag |= NOFLSH;
620 bytesize = slc->WordLength;
621 stopbits = slc->StopBits;
623 #ifdef CMSPAR
624 port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
625 #else
626 port.c_cflag &= ~(PARENB | PARODD);
627 #endif
629 /* make sure that reads don't block */
630 port.c_cc[VMIN] = 0;
631 port.c_cc[VTIME] = 0;
633 switch (slc->Parity)
635 case NOPARITY: port.c_iflag &= ~INPCK; break;
636 case ODDPARITY: port.c_cflag |= PARENB | PARODD; break;
637 case EVENPARITY: port.c_cflag |= PARENB; break;
638 #ifdef CMSPAR
639 /* Linux defines mark/space (stick) parity */
640 case MARKPARITY: port.c_cflag |= PARENB | CMSPAR; break;
641 case SPACEPARITY: port.c_cflag |= PARENB | PARODD | CMSPAR; break;
642 #else
643 /* try the POSIX way */
644 case MARKPARITY:
645 if (slc->StopBits == ONESTOPBIT)
647 stopbits = TWOSTOPBITS;
648 port.c_iflag &= ~INPCK;
650 else
652 FIXME("Cannot set MARK Parity\n");
653 return STATUS_NOT_SUPPORTED;
655 break;
656 case SPACEPARITY:
657 if (slc->WordLength < 8)
659 bytesize +=1;
660 port.c_iflag &= ~INPCK;
662 else
664 FIXME("Cannot set SPACE Parity\n");
665 return STATUS_NOT_SUPPORTED;
667 break;
668 #endif
669 default:
670 FIXME("Parity %d is not supported\n", slc->Parity);
671 return STATUS_NOT_SUPPORTED;
674 port.c_cflag &= ~CSIZE;
675 switch (bytesize)
677 case 5: port.c_cflag |= CS5; break;
678 case 6: port.c_cflag |= CS6; break;
679 case 7: port.c_cflag |= CS7; break;
680 case 8: port.c_cflag |= CS8; break;
681 default:
682 FIXME("ByteSize %d is not supported\n", bytesize);
683 return STATUS_NOT_SUPPORTED;
686 switch (stopbits)
688 case ONESTOPBIT: port.c_cflag &= ~CSTOPB; break;
689 case ONE5STOPBITS: /* will be selected if bytesize is 5 */
690 case TWOSTOPBITS: port.c_cflag |= CSTOPB; break;
691 default:
692 FIXME("StopBits %d is not supported\n", stopbits);
693 return STATUS_NOT_SUPPORTED;
695 /* otherwise it hangs with pending input*/
696 if (tcsetattr(fd, TCSANOW, &port) == -1)
698 ERR("tcsetattr error '%s'\n", strerror(errno));
699 return FILE_GetNtStatus();
701 return STATUS_SUCCESS;
704 static NTSTATUS set_queue_size(int fd, const SERIAL_QUEUE_SIZE* sqs)
706 FIXME("insize %d outsize %d unimplemented stub\n", sqs->InSize, sqs->OutSize);
707 return STATUS_SUCCESS;
710 static NTSTATUS set_special_chars(int fd, const SERIAL_CHARS* sc)
712 struct termios port;
714 if (tcgetattr(fd, &port) == -1)
716 ERR("tcgetattr error '%s'\n", strerror(errno));
717 return FILE_GetNtStatus();
720 port.c_cc[VEOF ] = sc->EofChar;
721 /* FIXME: sc->ErrorChar is not supported */
722 /* FIXME: sc->BreakChar is not supported */
723 /* FIXME: sc->EventChar is not supported */
724 port.c_cc[VSTART] = sc->XonChar;
725 port.c_cc[VSTOP ] = sc->XoffChar;
727 if (tcsetattr(fd, TCSANOW, &port) == -1)
729 ERR("tcsetattr error '%s'\n", strerror(errno));
730 return FILE_GetNtStatus();
732 return STATUS_SUCCESS;
735 static NTSTATUS set_timeouts(HANDLE handle, const SERIAL_TIMEOUTS* st)
737 NTSTATUS status;
739 SERVER_START_REQ( set_serial_info )
741 req->handle = wine_server_obj_handle( handle );
742 req->flags = SERIALINFO_SET_TIMEOUTS;
743 req->readinterval = st->ReadIntervalTimeout ;
744 req->readmult = st->ReadTotalTimeoutMultiplier ;
745 req->readconst = st->ReadTotalTimeoutConstant ;
746 req->writemult = st->WriteTotalTimeoutMultiplier ;
747 req->writeconst = st->WriteTotalTimeoutConstant ;
748 status = wine_server_call( req );
750 SERVER_END_REQ;
751 return status;
754 static NTSTATUS set_wait_mask(HANDLE hDevice, DWORD mask)
756 NTSTATUS status;
758 SERVER_START_REQ( set_serial_info )
760 req->handle = wine_server_obj_handle( hDevice );
761 req->flags = SERIALINFO_SET_MASK;
762 req->eventmask = mask;
763 status = wine_server_call( req );
765 SERVER_END_REQ;
766 return status;
770 * does not change IXOFF but simulates that IXOFF has been received:
772 static NTSTATUS set_XOff(int fd)
774 if (tcflow(fd, TCOOFF))
776 return FILE_GetNtStatus();
778 return STATUS_SUCCESS;
782 * does not change IXON but simulates that IXON has been received:
784 static NTSTATUS set_XOn(int fd)
786 if (tcflow(fd, TCOON))
788 return FILE_GetNtStatus();
790 return STATUS_SUCCESS;
793 /* serial_irq_info
794 * local structure holding the irq values we need for WaitCommEvent()
796 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
797 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
798 * no need to carry them in the internal structure
801 typedef struct serial_irq_info
803 int rx, tx, frame, overrun, parity, brk, buf_overrun, temt;
804 }serial_irq_info;
806 /***********************************************************************
807 * Data needed by the thread polling for the changing CommEvent
809 typedef struct async_commio
811 HANDLE hDevice;
812 DWORD* events;
813 IO_STATUS_BLOCK* iosb;
814 HANDLE hEvent;
815 DWORD evtmask;
816 DWORD cookie;
817 DWORD mstat;
818 DWORD pending_write;
819 serial_irq_info irq_info;
820 } async_commio;
822 /***********************************************************************
823 * Get extended interrupt count info, needed for wait_on
825 static NTSTATUS get_irq_info(int fd, serial_irq_info *irq_info)
827 int out;
829 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCGICOUNT)
830 struct serial_icounter_struct einfo;
831 if (!ioctl(fd, TIOCGICOUNT, &einfo))
833 irq_info->rx = einfo.rx;
834 irq_info->tx = einfo.tx;
835 irq_info->frame = einfo.frame;
836 irq_info->overrun = einfo.overrun;
837 irq_info->parity = einfo.parity;
838 irq_info->brk = einfo.brk;
839 irq_info->buf_overrun = einfo.buf_overrun;
841 else
843 TRACE("TIOCGICOUNT err %s\n", strerror(errno));
844 memset(irq_info,0, sizeof(serial_irq_info));
846 #else
847 memset(irq_info,0, sizeof(serial_irq_info));
848 #endif
850 irq_info->temt = 0;
851 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
852 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
853 if (!ioctl(fd, TIOCSERGETLSR, &out))
855 irq_info->temt = (out & TIOCSER_TEMT) != 0;
856 return STATUS_SUCCESS;
859 TRACE("TIOCSERGETLSR err %s\n", strerror(errno));
860 #endif
861 #ifdef TIOCOUTQ /* otherwise we log when the out queue gets empty */
862 if (!ioctl(fd, TIOCOUTQ, &out))
864 irq_info->temt = out == 0;
865 return STATUS_SUCCESS;
867 TRACE("TIOCOUTQ err %s\n", strerror(errno));
868 return FILE_GetNtStatus();
869 #endif
870 return STATUS_SUCCESS;
874 static DWORD check_events(int fd, DWORD mask,
875 const serial_irq_info *new,
876 const serial_irq_info *old,
877 DWORD new_mstat, DWORD old_mstat, DWORD pending_write)
879 DWORD ret = 0, queue;
881 TRACE("mask 0x%08x\n", mask);
882 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old->rx, new->rx);
883 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old->tx, new->tx);
884 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old->frame, new->frame);
885 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old->overrun, new->overrun);
886 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old->parity, new->parity);
887 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old->brk, new->brk);
888 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old->buf_overrun, new->buf_overrun);
889 TRACE("old->temt 0x%08x vs. new->temt 0x%08x\n", old->temt, new->temt);
891 if (old->brk != new->brk) ret |= EV_BREAK;
892 if ((old_mstat & MS_CTS_ON ) != (new_mstat & MS_CTS_ON )) ret |= EV_CTS;
893 if ((old_mstat & MS_DSR_ON ) != (new_mstat & MS_DSR_ON )) ret |= EV_DSR;
894 if ((old_mstat & MS_RING_ON) != (new_mstat & MS_RING_ON)) ret |= EV_RING;
895 if ((old_mstat & MS_RLSD_ON) != (new_mstat & MS_RLSD_ON)) ret |= EV_RLSD;
896 if (old->frame != new->frame || old->overrun != new->overrun || old->parity != new->parity) ret |= EV_ERR;
897 if (mask & EV_RXCHAR)
899 queue = 0;
900 #ifdef TIOCINQ
901 if (ioctl(fd, TIOCINQ, &queue))
902 WARN("TIOCINQ returned error\n");
903 #endif
904 if (queue)
905 ret |= EV_RXCHAR;
907 if (mask & EV_TXEMPTY)
909 if ((!old->temt || pending_write) && new->temt)
910 ret |= EV_TXEMPTY;
912 return ret & mask;
915 /***********************************************************************
916 * wait_for_event (INTERNAL)
918 * We need to poll for what is interesting
919 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
922 static DWORD CALLBACK wait_for_event(LPVOID arg)
924 async_commio *commio = arg;
925 int fd, needs_close;
927 if (!server_get_unix_fd( commio->hDevice, FILE_READ_DATA | FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL ))
929 serial_irq_info new_irq_info;
930 DWORD new_mstat, dummy, cookie;
931 LARGE_INTEGER time;
933 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
934 commio->hDevice, fd, commio->evtmask, commio->events, commio->hEvent, &commio->irq_info);
936 time.QuadPart = (ULONGLONG)10000;
937 time.QuadPart = -time.QuadPart;
938 for (;;)
941 * TIOCMIWAIT is not adequate
943 * FIXME:
944 * We don't handle the EV_RXFLAG (the eventchar)
946 NtDelayExecution(FALSE, &time);
947 get_irq_info(fd, &new_irq_info);
948 if (get_modem_status(fd, &new_mstat))
950 TRACE("get_modem_status failed\n");
951 *commio->events = 0;
952 break;
954 *commio->events = check_events(fd, commio->evtmask,
955 &new_irq_info, &commio->irq_info,
956 new_mstat, commio->mstat, commio->pending_write);
957 if (*commio->events) break;
958 get_wait_mask(commio->hDevice, &dummy, &cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, FALSE);
959 if (commio->cookie != cookie)
961 *commio->events = 0;
962 break;
965 if (needs_close) close( fd );
967 if (commio->iosb)
969 if (*commio->events)
971 commio->iosb->u.Status = STATUS_SUCCESS;
972 commio->iosb->Information = sizeof(DWORD);
974 else
975 commio->iosb->u.Status = STATUS_CANCELLED;
977 stop_waiting(commio->hDevice);
978 if (commio->hEvent) NtSetEvent(commio->hEvent, NULL);
979 RtlFreeHeap(GetProcessHeap(), 0, commio);
980 return 0;
983 static NTSTATUS wait_on(HANDLE hDevice, int fd, HANDLE hEvent, PIO_STATUS_BLOCK piosb, DWORD* events)
985 async_commio* commio;
986 NTSTATUS status;
988 if ((status = NtResetEvent(hEvent, NULL)))
989 return status;
991 commio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio));
992 if (!commio) return STATUS_NO_MEMORY;
994 commio->hDevice = hDevice;
995 commio->events = events;
996 commio->iosb = piosb;
997 commio->hEvent = hEvent;
998 commio->pending_write = 0;
999 status = get_wait_mask(commio->hDevice, &commio->evtmask, &commio->cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, TRUE);
1000 if (status)
1002 RtlFreeHeap(GetProcessHeap(), 0, commio);
1003 return status;
1006 /* We may never return, if some capabilities miss
1007 * Return error in that case
1009 #if !defined(TIOCINQ)
1010 if (commio->evtmask & EV_RXCHAR)
1011 goto error_caps;
1012 #endif
1013 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1014 if (commio->evtmask & EV_TXEMPTY)
1015 goto error_caps;
1016 #endif
1017 #if !defined(TIOCMGET)
1018 if (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD))
1019 goto error_caps;
1020 #endif
1021 #if !defined(TIOCM_CTS)
1022 if (commio->evtmask & EV_CTS)
1023 goto error_caps;
1024 #endif
1025 #if !defined(TIOCM_DSR)
1026 if (commio->evtmask & EV_DSR)
1027 goto error_caps;
1028 #endif
1029 #if !defined(TIOCM_RNG)
1030 if (commio->evtmask & EV_RING)
1031 goto error_caps;
1032 #endif
1033 #if !defined(TIOCM_CAR)
1034 if (commio->evtmask & EV_RLSD)
1035 goto error_caps;
1036 #endif
1037 if (commio->evtmask & EV_RXFLAG)
1038 FIXME("EV_RXFLAG not handled\n");
1040 if ((status = get_irq_info(fd, &commio->irq_info)) &&
1041 (commio->evtmask & (EV_BREAK | EV_ERR)))
1042 goto out_now;
1044 if ((status = get_modem_status(fd, &commio->mstat)) &&
1045 (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD)))
1046 goto out_now;
1048 /* We might have received something or the TX buffer is delivered */
1049 *events = check_events(fd, commio->evtmask,
1050 &commio->irq_info, &commio->irq_info,
1051 commio->mstat, commio->mstat, commio->pending_write);
1052 if (*events)
1054 status = STATUS_SUCCESS;
1055 goto out_now;
1058 /* create the worker for the task */
1059 status = RtlQueueWorkItem(wait_for_event, commio, 0 /* FIXME */);
1060 if (status != STATUS_SUCCESS) goto out_now;
1061 return STATUS_PENDING;
1063 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1064 error_caps:
1065 FIXME("Returning error because of missing capabilities\n");
1066 status = STATUS_INVALID_PARAMETER;
1067 #endif
1068 out_now:
1069 stop_waiting(commio->hDevice);
1070 RtlFreeHeap(GetProcessHeap(), 0, commio);
1071 return status;
1074 static NTSTATUS xmit_immediate(HANDLE hDevice, int fd, const char* ptr)
1076 /* FIXME: not perfect as it should bypass the in-queue */
1077 WARN("(%p,'%c') not perfect!\n", hDevice, *ptr);
1078 if (write(fd, ptr, 1) != 1)
1079 return FILE_GetNtStatus();
1080 return STATUS_SUCCESS;
1083 /******************************************************************
1084 * COMM_DeviceIoControl
1088 static inline NTSTATUS io_control(HANDLE hDevice,
1089 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1090 PVOID UserApcContext,
1091 PIO_STATUS_BLOCK piosb,
1092 ULONG dwIoControlCode,
1093 LPVOID lpInBuffer, DWORD nInBufferSize,
1094 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1096 DWORD sz = 0, access = FILE_READ_DATA;
1097 NTSTATUS status = STATUS_SUCCESS;
1098 int fd = -1, needs_close = 0;
1100 TRACE("%p %s %p %d %p %d %p\n",
1101 hDevice, iocode2str(dwIoControlCode), lpInBuffer, nInBufferSize,
1102 lpOutBuffer, nOutBufferSize, piosb);
1104 piosb->Information = 0;
1106 if (dwIoControlCode != IOCTL_SERIAL_GET_TIMEOUTS &&
1107 dwIoControlCode != IOCTL_SERIAL_SET_TIMEOUTS)
1109 enum server_fd_type type;
1110 if ((status = server_get_unix_fd( hDevice, access, &fd, &needs_close, &type, NULL )))
1111 goto error;
1112 if (type != FD_TYPE_SERIAL)
1114 if (needs_close) close( fd );
1115 status = STATUS_OBJECT_TYPE_MISMATCH;
1116 goto error;
1120 switch (dwIoControlCode)
1122 case IOCTL_SERIAL_CLR_DTR:
1123 #ifdef TIOCM_DTR
1124 if (whack_modem(fd, ~TIOCM_DTR, 0) == -1) status = FILE_GetNtStatus();
1125 #else
1126 status = STATUS_NOT_SUPPORTED;
1127 #endif
1128 break;
1129 case IOCTL_SERIAL_CLR_RTS:
1130 #ifdef TIOCM_RTS
1131 if (whack_modem(fd, ~TIOCM_RTS, 0) == -1) status = FILE_GetNtStatus();
1132 #else
1133 status = STATUS_NOT_SUPPORTED;
1134 #endif
1135 break;
1136 case IOCTL_SERIAL_GET_BAUD_RATE:
1137 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_BAUD_RATE))
1139 if (!(status = get_baud_rate(fd, lpOutBuffer)))
1140 sz = sizeof(SERIAL_BAUD_RATE);
1142 else
1143 status = STATUS_INVALID_PARAMETER;
1144 break;
1145 case IOCTL_SERIAL_GET_CHARS:
1146 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_CHARS))
1148 if (!(status = get_special_chars(fd, lpOutBuffer)))
1149 sz = sizeof(SERIAL_CHARS);
1151 else
1152 status = STATUS_INVALID_PARAMETER;
1153 break;
1154 case IOCTL_SERIAL_GET_COMMSTATUS:
1155 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_STATUS))
1157 if (!(status = get_status(fd, lpOutBuffer)))
1158 sz = sizeof(SERIAL_STATUS);
1160 else status = STATUS_INVALID_PARAMETER;
1161 break;
1162 case IOCTL_SERIAL_GET_HANDFLOW:
1163 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_HANDFLOW))
1165 if (!(status = get_hand_flow(fd, lpOutBuffer)))
1166 sz = sizeof(SERIAL_HANDFLOW);
1168 else
1169 status = STATUS_INVALID_PARAMETER;
1170 break;
1171 case IOCTL_SERIAL_GET_LINE_CONTROL:
1172 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_LINE_CONTROL))
1174 if (!(status = get_line_control(fd, lpOutBuffer)))
1175 sz = sizeof(SERIAL_LINE_CONTROL);
1177 else
1178 status = STATUS_INVALID_PARAMETER;
1179 break;
1180 case IOCTL_SERIAL_GET_MODEMSTATUS:
1181 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1183 if (!(status = get_modem_status(fd, lpOutBuffer)))
1184 sz = sizeof(DWORD);
1186 else status = STATUS_INVALID_PARAMETER;
1187 break;
1188 case IOCTL_SERIAL_GET_TIMEOUTS:
1189 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_TIMEOUTS))
1191 if (!(status = get_timeouts(hDevice, lpOutBuffer)))
1192 sz = sizeof(SERIAL_TIMEOUTS);
1194 else
1195 status = STATUS_INVALID_PARAMETER;
1196 break;
1197 case IOCTL_SERIAL_GET_WAIT_MASK:
1198 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1200 if (!(status = get_wait_mask(hDevice, lpOutBuffer, NULL, NULL, FALSE)))
1201 sz = sizeof(DWORD);
1203 else
1204 status = STATUS_INVALID_PARAMETER;
1205 break;
1206 case IOCTL_SERIAL_IMMEDIATE_CHAR:
1207 if (lpInBuffer && nInBufferSize == sizeof(CHAR))
1208 status = xmit_immediate(hDevice, fd, lpInBuffer);
1209 else
1210 status = STATUS_INVALID_PARAMETER;
1211 break;
1212 case IOCTL_SERIAL_PURGE:
1213 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1214 status = purge(fd, *(DWORD*)lpInBuffer);
1215 else
1216 status = STATUS_INVALID_PARAMETER;
1217 break;
1218 case IOCTL_SERIAL_RESET_DEVICE:
1219 FIXME("Unsupported\n");
1220 break;
1221 case IOCTL_SERIAL_SET_BAUD_RATE:
1222 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_BAUD_RATE))
1223 status = set_baud_rate(fd, lpInBuffer);
1224 else
1225 status = STATUS_INVALID_PARAMETER;
1226 break;
1227 case IOCTL_SERIAL_SET_BREAK_OFF:
1228 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1229 if (ioctl(fd, TIOCCBRK, 0) == -1)
1231 TRACE("ioctl failed\n");
1232 status = FILE_GetNtStatus();
1234 #else
1235 FIXME("ioctl not available\n");
1236 status = STATUS_NOT_SUPPORTED;
1237 #endif
1238 break;
1239 case IOCTL_SERIAL_SET_BREAK_ON:
1240 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1241 if (ioctl(fd, TIOCSBRK, 0) == -1)
1243 TRACE("ioctl failed\n");
1244 status = FILE_GetNtStatus();
1246 #else
1247 FIXME("ioctl not available\n");
1248 status = STATUS_NOT_SUPPORTED;
1249 #endif
1250 break;
1251 case IOCTL_SERIAL_SET_CHARS:
1252 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_CHARS))
1253 status = set_special_chars(fd, lpInBuffer);
1254 else
1255 status = STATUS_INVALID_PARAMETER;
1256 break;
1257 case IOCTL_SERIAL_SET_DTR:
1258 #ifdef TIOCM_DTR
1259 if (whack_modem(fd, 0, TIOCM_DTR) == -1) status = FILE_GetNtStatus();
1260 #else
1261 status = STATUS_NOT_SUPPORTED;
1262 #endif
1263 break;
1264 case IOCTL_SERIAL_SET_HANDFLOW:
1265 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_HANDFLOW))
1266 status = set_handflow(fd, lpInBuffer);
1267 else
1268 status = STATUS_INVALID_PARAMETER;
1269 break;
1270 case IOCTL_SERIAL_SET_LINE_CONTROL:
1271 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_LINE_CONTROL))
1272 status = set_line_control(fd, lpInBuffer);
1273 else
1274 status = STATUS_INVALID_PARAMETER;
1275 break;
1276 case IOCTL_SERIAL_SET_QUEUE_SIZE:
1277 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_QUEUE_SIZE))
1278 status = set_queue_size(fd, lpInBuffer);
1279 else
1280 status = STATUS_INVALID_PARAMETER;
1281 break;
1282 case IOCTL_SERIAL_SET_RTS:
1283 #ifdef TIOCM_RTS
1284 if (whack_modem(fd, 0, TIOCM_RTS) == -1) status = FILE_GetNtStatus();
1285 #else
1286 status = STATUS_NOT_SUPPORTED;
1287 #endif
1288 break;
1289 case IOCTL_SERIAL_SET_TIMEOUTS:
1290 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_TIMEOUTS))
1291 status = set_timeouts(hDevice, lpInBuffer);
1292 else
1293 status = STATUS_INVALID_PARAMETER;
1294 break;
1295 case IOCTL_SERIAL_SET_WAIT_MASK:
1296 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1298 status = set_wait_mask(hDevice, *(DWORD*)lpInBuffer);
1300 else status = STATUS_INVALID_PARAMETER;
1301 break;
1302 case IOCTL_SERIAL_SET_XOFF:
1303 status = set_XOff(fd);
1304 break;
1305 case IOCTL_SERIAL_SET_XON:
1306 status = set_XOn(fd);
1307 break;
1308 case IOCTL_SERIAL_WAIT_ON_MASK:
1309 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1311 if (!(status = wait_on(hDevice, fd, hEvent, piosb, lpOutBuffer)))
1312 sz = sizeof(DWORD);
1314 else
1315 status = STATUS_INVALID_PARAMETER;
1316 break;
1317 default:
1318 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1319 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
1320 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
1321 sz = 0;
1322 status = STATUS_INVALID_PARAMETER;
1323 break;
1325 if (needs_close) close( fd );
1326 error:
1327 piosb->u.Status = status;
1328 piosb->Information = sz;
1329 if (hEvent && status != STATUS_PENDING) NtSetEvent(hEvent, NULL);
1330 return status;
1333 NTSTATUS COMM_DeviceIoControl(HANDLE hDevice,
1334 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1335 PVOID UserApcContext,
1336 PIO_STATUS_BLOCK piosb,
1337 ULONG dwIoControlCode,
1338 LPVOID lpInBuffer, DWORD nInBufferSize,
1339 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1341 NTSTATUS status;
1343 if (dwIoControlCode == IOCTL_SERIAL_WAIT_ON_MASK)
1345 HANDLE hev = hEvent;
1347 /* this is an ioctl we implement in a non blocking way if hEvent is not
1348 * null
1349 * so we have to explicitly wait if no hEvent is provided
1351 if (!hev)
1353 OBJECT_ATTRIBUTES attr;
1355 attr.Length = sizeof(attr);
1356 attr.RootDirectory = 0;
1357 attr.ObjectName = NULL;
1358 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1359 attr.SecurityDescriptor = NULL;
1360 attr.SecurityQualityOfService = NULL;
1361 status = NtCreateEvent(&hev, EVENT_ALL_ACCESS, &attr, SynchronizationEvent, FALSE);
1363 if (status) return status;
1365 status = io_control(hDevice, hev, UserApcRoutine, UserApcContext,
1366 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1367 lpOutBuffer, nOutBufferSize);
1368 if (hev != hEvent)
1370 if (status == STATUS_PENDING)
1372 NtWaitForSingleObject(hev, FALSE, NULL);
1373 status = STATUS_SUCCESS;
1375 NtClose(hev);
1378 else status = io_control(hDevice, hEvent, UserApcRoutine, UserApcContext,
1379 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1380 lpOutBuffer, nOutBufferSize);
1381 return status;
1384 NTSTATUS COMM_FlushBuffersFile( int fd )
1386 #ifdef HAVE_TCDRAIN
1387 while (tcdrain( fd ) == -1)
1389 if (errno != EINTR) return FILE_GetNtStatus();
1391 return STATUS_SUCCESS;
1392 #elif defined(TIOCDRAIN)
1393 while (ioctl( fd, TIOCDRAIN ) == -1)
1395 if (errno != EINTR) return FILE_GetNtStatus();
1397 return STATUS_SUCCESS;
1398 #elif defined(TCSBRK)
1399 while (ioctl( fd, TCSBRK, 1 ) == -1)
1401 if (errno != EINTR) return FILE_GetNtStatus();
1403 return STATUS_SUCCESS;
1404 #else
1405 ERR( "not supported\n" );
1406 return STATUS_NOT_IMPLEMENTED;
1407 #endif