wined3d: Correctly destroy the adapter on format initialization failure in no3d mode.
[wine/zf.git] / dlls / ntdll / unix / serial.c
blob9454185e172299c840f937bb63ae29a627cb65a8
1 /*
2 * Serial device support
4 * Copyright 1993 Erik Bos
5 * Copyright 1996 Marcus Meissner
6 * Copyright 2005,2006 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #if 0
24 #pragma makedep unix
25 #endif
27 #include "config.h"
28 #include "wine/port.h"
30 #include <errno.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #ifdef HAVE_TERMIOS_H
36 #include <termios.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 #endif
45 #include <sys/types.h>
46 #ifdef HAVE_SYS_FILIO_H
47 # include <sys/filio.h>
48 #endif
49 #ifdef HAVE_SYS_IOCTL_H
50 #include <sys/ioctl.h>
51 #endif
52 #ifdef HAVE_SYS_POLL_H
53 # include <sys/poll.h>
54 #endif
55 #ifdef HAVE_SYS_MODEM_H
56 # include <sys/modem.h>
57 #endif
58 #ifdef HAVE_SYS_STRTIO_H
59 # include <sys/strtio.h>
60 #endif
62 #include "ntstatus.h"
63 #define WIN32_NO_STATUS
64 #define NONAMELESSUNION
65 #include "windef.h"
66 #include "winternl.h"
67 #include "winioctl.h"
68 #include "ddk/ntddser.h"
69 #include "wine/server.h"
70 #include "unix_private.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 errno_to_status( errno );
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 #ifdef B500000
175 case B500000: sbr->BaudRate = 500000; break;
176 #endif
177 #ifdef B921600
178 case B921600: sbr->BaudRate = 921600; break;
179 #endif
180 #ifdef B1000000
181 case B1000000: sbr->BaudRate = 1000000; break;
182 #endif
183 #ifdef B1152000
184 case B1152000: sbr->BaudRate = 1152000; break;
185 #endif
186 #ifdef B1500000
187 case B1500000: sbr->BaudRate = 1500000; break;
188 #endif
189 #ifdef B2000000
190 case B2000000: sbr->BaudRate = 2000000; break;
191 #endif
192 #ifdef B2500000
193 case B2500000: sbr->BaudRate = 2500000; break;
194 #endif
195 #ifdef B3000000
196 case B3000000: sbr->BaudRate = 3000000; break;
197 #endif
198 #ifdef B3500000
199 case B3500000: sbr->BaudRate = 3500000; break;
200 #endif
201 #ifdef B4000000
202 case B4000000: sbr->BaudRate = 4000000; break;
203 #endif
204 default:
205 ERR("unknown speed %x\n", speed);
206 return STATUS_INVALID_PARAMETER;
208 return STATUS_SUCCESS;
211 static NTSTATUS get_hand_flow(int fd, SERIAL_HANDFLOW* shf)
213 int stat = 0;
214 struct termios port;
216 if (tcgetattr(fd, &port) == -1)
218 ERR("tcgetattr error '%s'\n", strerror(errno));
219 return errno_to_status( errno );
221 /* termios does not support DTR/DSR flow control */
222 shf->ControlHandShake = 0;
223 shf->FlowReplace = 0;
224 #ifdef TIOCMGET
225 if (ioctl(fd, TIOCMGET, &stat) == -1)
227 WARN("ioctl error '%s'\n", strerror(errno));
228 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
229 shf->FlowReplace |= SERIAL_RTS_CONTROL;
231 #else
232 WARN("Setting DTR/RTS to enabled by default\n");
233 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
234 shf->FlowReplace |= SERIAL_RTS_CONTROL;
235 #endif
236 #ifdef TIOCM_DTR
237 if (stat & TIOCM_DTR)
238 #endif
239 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
240 #ifdef CRTSCTS
241 if (port.c_cflag & CRTSCTS)
243 shf->FlowReplace |= SERIAL_RTS_CONTROL;
244 shf->ControlHandShake |= SERIAL_CTS_HANDSHAKE;
246 else
247 #endif
249 #ifdef TIOCM_RTS
250 if (stat & TIOCM_RTS)
251 #endif
252 shf->FlowReplace |= SERIAL_RTS_CONTROL;
254 if (port.c_iflag & IXOFF)
255 shf->FlowReplace |= SERIAL_AUTO_RECEIVE;
256 if (port.c_iflag & IXON)
257 shf->FlowReplace |= SERIAL_AUTO_TRANSMIT;
259 shf->XonLimit = 10;
260 shf->XoffLimit = 10;
261 return STATUS_SUCCESS;
264 static NTSTATUS get_line_control(int fd, SERIAL_LINE_CONTROL* slc)
266 struct termios port;
268 if (tcgetattr(fd, &port) == -1)
270 ERR("tcgetattr error '%s'\n", strerror(errno));
271 return errno_to_status( errno );
274 #ifdef CMSPAR
275 switch (port.c_cflag & (PARENB | PARODD | CMSPAR))
276 #else
277 switch (port.c_cflag & (PARENB | PARODD))
278 #endif
280 case 0: slc->Parity = NOPARITY; break;
281 case PARENB: slc->Parity = EVENPARITY; break;
282 case PARENB|PARODD: slc->Parity = ODDPARITY; break;
283 #ifdef CMSPAR
284 case PARENB|PARODD|CMSPAR: slc->Parity = MARKPARITY; break;
285 case PARENB|CMSPAR: slc->Parity = SPACEPARITY; break;
286 #endif
288 switch (port.c_cflag & CSIZE)
290 case CS5: slc->WordLength = 5; break;
291 case CS6: slc->WordLength = 6; break;
292 case CS7: slc->WordLength = 7; break;
293 case CS8: slc->WordLength = 8; break;
294 default: ERR("unknown size %x\n", (UINT)(port.c_cflag & CSIZE));
297 if (port.c_cflag & CSTOPB)
299 if (slc->WordLength == 5)
300 slc->StopBits = ONE5STOPBITS;
301 else
302 slc->StopBits = TWOSTOPBITS;
304 else
305 slc->StopBits = ONESTOPBIT;
307 return STATUS_SUCCESS;
310 static NTSTATUS get_modem_status(int fd, DWORD* lpModemStat)
312 NTSTATUS status = STATUS_NOT_SUPPORTED;
313 int mstat;
315 *lpModemStat = 0;
316 #ifdef TIOCMGET
317 if (!ioctl(fd, TIOCMGET, &mstat))
319 #ifdef TIOCM_CTS
320 if (mstat & TIOCM_CTS) *lpModemStat |= MS_CTS_ON;
321 #endif
322 #ifdef TIOCM_DSR
323 if (mstat & TIOCM_DSR) *lpModemStat |= MS_DSR_ON;
324 #endif
325 #ifdef TIOCM_RNG
326 if (mstat & TIOCM_RNG) *lpModemStat |= MS_RING_ON;
327 #endif
328 #ifdef TIOCM_CAR
329 /* FIXME: Not really sure about RLSD UB 990810 */
330 if (mstat & TIOCM_CAR) *lpModemStat |= MS_RLSD_ON;
331 #endif
332 TRACE("%04x -> %s%s%s%s\n", mstat,
333 (*lpModemStat & MS_RLSD_ON) ? "MS_RLSD_ON " : "",
334 (*lpModemStat & MS_RING_ON) ? "MS_RING_ON " : "",
335 (*lpModemStat & MS_DSR_ON) ? "MS_DSR_ON " : "",
336 (*lpModemStat & MS_CTS_ON) ? "MS_CTS_ON " : "");
337 return STATUS_SUCCESS;
339 WARN("TIOCMGET err %s\n", strerror(errno));
340 status = errno_to_status( errno );
341 #endif
342 return status;
345 static NTSTATUS get_properties(int fd, SERIAL_COMMPROP *prop)
347 /* FIXME: get actual properties from the device */
348 memset( prop, 0, sizeof(*prop) );
349 prop->PacketLength = 1;
350 prop->PacketVersion = 1;
351 prop->ServiceMask = SP_SERIALCOMM;
352 prop->MaxTxQueue = 4096;
353 prop->MaxRxQueue = 4096;
354 prop->MaxBaud = BAUD_115200;
355 prop->ProvSubType = PST_RS232;
356 prop->ProvCapabilities = PCF_DTRDSR | PCF_PARITY_CHECK | PCF_RTSCTS | PCF_TOTALTIMEOUTS | PCF_INTTIMEOUTS;
357 prop->SettableParams = SP_BAUD | SP_DATABITS | SP_HANDSHAKING |
358 SP_PARITY | SP_PARITY_CHECK | SP_STOPBITS ;
359 prop->SettableBaud = BAUD_075 | BAUD_110 | BAUD_134_5 | BAUD_150 |
360 BAUD_300 | BAUD_600 | BAUD_1200 | BAUD_1800 | BAUD_2400 | BAUD_4800 |
361 BAUD_9600 | BAUD_19200 | BAUD_38400 | BAUD_57600 | BAUD_115200 ;
362 prop->SettableData = DATABITS_5 | DATABITS_6 | DATABITS_7 | DATABITS_8 ;
363 prop->SettableStopParity = STOPBITS_10 | STOPBITS_15 | STOPBITS_20 |
364 PARITY_NONE | PARITY_ODD |PARITY_EVEN | PARITY_MARK | PARITY_SPACE;
365 prop->CurrentTxQueue = prop->MaxTxQueue;
366 prop->CurrentRxQueue = prop->MaxRxQueue;
367 return STATUS_SUCCESS;
370 static NTSTATUS get_special_chars(int fd, SERIAL_CHARS* sc)
372 struct termios port;
374 if (tcgetattr(fd, &port) == -1)
376 ERR("tcgetattr error '%s'\n", strerror(errno));
377 return errno_to_status( errno );
379 sc->EofChar = port.c_cc[VEOF];
380 sc->ErrorChar = 0xFF;
381 sc->BreakChar = 0; /* FIXME */
382 sc->EventChar = 0; /* FIXME */
383 sc->XonChar = port.c_cc[VSTART];
384 sc->XoffChar = port.c_cc[VSTOP];
386 return STATUS_SUCCESS;
389 static NTSTATUS get_status(int fd, SERIAL_STATUS* ss)
391 NTSTATUS status = STATUS_SUCCESS;
393 ss->Errors = 0;
394 ss->HoldReasons = 0;
395 ss->EofReceived = FALSE;
396 ss->WaitForImmediate = FALSE;
397 #ifdef TIOCOUTQ
398 if (ioctl(fd, TIOCOUTQ, &ss->AmountInOutQueue) == -1)
400 WARN("ioctl returned error\n");
401 status = errno_to_status( errno );
403 #else
404 ss->AmountInOutQueue = 0; /* FIXME: find a different way to find out */
405 #endif
407 #ifdef TIOCINQ
408 if (ioctl(fd, TIOCINQ, &ss->AmountInInQueue))
410 WARN("ioctl returned error\n");
411 status = errno_to_status( errno );
413 #else
414 ss->AmountInInQueue = 0; /* FIXME: find a different way to find out */
415 #endif
416 return status;
419 static void stop_waiting( HANDLE handle )
421 NTSTATUS status;
423 SERVER_START_REQ( set_serial_info )
425 req->handle = wine_server_obj_handle( handle );
426 req->flags = SERIALINFO_PENDING_WAIT;
427 if ((status = wine_server_call( req )))
428 ERR("failed to clear waiting state: %#x\n", status);
430 SERVER_END_REQ;
433 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD *mask, DWORD *cookie, DWORD *pending_write, BOOL start_wait)
435 NTSTATUS status;
437 SERVER_START_REQ( get_serial_info )
439 req->handle = wine_server_obj_handle( hDevice );
440 req->flags = pending_write ? SERIALINFO_PENDING_WRITE : 0;
441 if (start_wait) req->flags |= SERIALINFO_PENDING_WAIT;
442 if (!(status = wine_server_call( req )))
444 *mask = reply->eventmask;
445 if (cookie) *cookie = reply->cookie;
446 if (pending_write) *pending_write = reply->pending_write;
449 SERVER_END_REQ;
450 return status;
453 static NTSTATUS purge(int fd, DWORD flags)
456 ** not exactly sure how these are different
457 ** Perhaps if we had our own internal queues, one flushes them
458 ** and the other flushes the kernel's buffers.
460 if (flags & PURGE_TXABORT) tcflush(fd, TCOFLUSH);
461 if (flags & PURGE_RXABORT) tcflush(fd, TCIFLUSH);
462 if (flags & PURGE_TXCLEAR) tcflush(fd, TCOFLUSH);
463 if (flags & PURGE_RXCLEAR) tcflush(fd, TCIFLUSH);
464 return STATUS_SUCCESS;
467 static NTSTATUS set_baud_rate(int fd, const SERIAL_BAUD_RATE* sbr)
469 struct termios port;
471 if (tcgetattr(fd, &port) == -1)
473 ERR("tcgetattr error '%s'\n", strerror(errno));
474 return errno_to_status( errno );
477 switch (sbr->BaudRate)
479 case 0: cfsetospeed( &port, B0 ); break;
480 case 50: cfsetospeed( &port, B50 ); break;
481 case 75: cfsetospeed( &port, B75 ); break;
482 case 110:
483 case CBR_110: cfsetospeed( &port, B110 ); break;
484 case 134: cfsetospeed( &port, B134 ); break;
485 case 150: cfsetospeed( &port, B150 ); break;
486 case 200: cfsetospeed( &port, B200 ); break;
487 case 300:
488 case CBR_300: cfsetospeed( &port, B300 ); break;
489 case 600:
490 case CBR_600: cfsetospeed( &port, B600 ); break;
491 case 1200:
492 case CBR_1200: cfsetospeed( &port, B1200 ); break;
493 case 1800: cfsetospeed( &port, B1800 ); break;
494 case 2400:
495 case CBR_2400: cfsetospeed( &port, B2400 ); break;
496 case 4800:
497 case CBR_4800: cfsetospeed( &port, B4800 ); break;
498 case 9600:
499 case CBR_9600: cfsetospeed( &port, B9600 ); break;
500 case 19200:
501 case CBR_19200: cfsetospeed( &port, B19200 ); break;
502 case 38400:
503 case CBR_38400: cfsetospeed( &port, B38400 ); break;
504 #ifdef B57600
505 case 57600: cfsetospeed( &port, B57600 ); break;
506 #endif
507 #ifdef B115200
508 case 115200: cfsetospeed( &port, B115200 ); break;
509 #endif
510 #ifdef B230400
511 case 230400: cfsetospeed( &port, B230400 ); break;
512 #endif
513 #ifdef B460800
514 case 460800: cfsetospeed( &port, B460800 ); break;
515 #endif
516 #ifdef B500000
517 case 500000: cfsetospeed( &port, B500000 ); break;
518 #endif
519 #ifdef B921600
520 case 921600: cfsetospeed( &port, B921600 ); break;
521 #endif
522 #ifdef B1000000
523 case 1000000: cfsetospeed( &port, B1000000 ); break;
524 #endif
525 #ifdef B1152000
526 case 1152000: cfsetospeed( &port, B1152000 ); break;
527 #endif
528 #ifdef B1500000
529 case 1500000: cfsetospeed( &port, B1500000 ); break;
530 #endif
531 #ifdef B2000000
532 case 2000000: cfsetospeed( &port, B2000000 ); break;
533 #endif
534 #ifdef B2500000
535 case 2500000: cfsetospeed( &port, B2500000 ); break;
536 #endif
537 #ifdef B3000000
538 case 3000000: cfsetospeed( &port, B3000000 ); break;
539 #endif
540 #ifdef B3500000
541 case 3500000: cfsetospeed( &port, B3500000 ); break;
542 #endif
543 #ifdef B4000000
544 case 4000000: cfsetospeed( &port, B4000000 ); break;
545 #endif
546 default:
547 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
549 struct serial_struct nuts;
550 int arby;
552 ioctl(fd, TIOCGSERIAL, &nuts);
553 nuts.custom_divisor = nuts.baud_base / sbr->BaudRate;
554 if (!(nuts.custom_divisor)) nuts.custom_divisor = 1;
555 arby = nuts.baud_base / nuts.custom_divisor;
556 nuts.flags &= ~ASYNC_SPD_MASK;
557 nuts.flags |= ASYNC_SPD_CUST;
558 WARN("You (or a program acting at your behest) have specified\n"
559 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
560 "which is as close as we can get by our present understanding of your\n"
561 "hardware. I hope you know what you are doing. Any disruption Wine\n"
562 "has caused to your linux system can be undone with setserial\n"
563 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
564 "reset it and it will probably recover.\n", sbr->BaudRate, arby);
565 ioctl(fd, TIOCSSERIAL, &nuts);
566 cfsetospeed( &port, B38400 );
568 break;
569 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
570 ERR("baudrate %d\n", sbr->BaudRate);
571 return STATUS_NOT_SUPPORTED;
572 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
574 cfsetispeed( &port, cfgetospeed(&port) );
575 if (tcsetattr(fd, TCSANOW, &port) == -1)
577 ERR("tcsetattr error '%s'\n", strerror(errno));
578 return errno_to_status( errno );
580 return STATUS_SUCCESS;
583 static int whack_modem(int fd, unsigned int andy, unsigned int orrie)
585 #ifdef TIOCMGET
586 unsigned int mstat, okay;
587 okay = ioctl(fd, TIOCMGET, &mstat);
588 if (okay) return okay;
589 if (andy) mstat &= andy;
590 mstat |= orrie;
591 return ioctl(fd, TIOCMSET, &mstat);
592 #else
593 return 0;
594 #endif
597 static NTSTATUS set_handflow(int fd, const SERIAL_HANDFLOW* shf)
599 struct termios port;
601 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE)) ==
602 (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
603 return STATUS_NOT_SUPPORTED;
605 if (tcgetattr(fd, &port) == -1)
607 ERR("tcgetattr error '%s'\n", strerror(errno));
608 return errno_to_status( errno );
611 #ifdef CRTSCTS
612 if ((shf->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
613 (shf->FlowReplace & SERIAL_RTS_HANDSHAKE))
615 port.c_cflag |= CRTSCTS;
616 TRACE("CRTSCTS\n");
618 else
619 port.c_cflag &= ~CRTSCTS;
620 #endif
621 #ifdef TIOCM_DTR
622 if (shf->ControlHandShake & SERIAL_DTR_HANDSHAKE)
624 WARN("DSR/DTR flow control not supported\n");
625 } else if (!(shf->ControlHandShake & SERIAL_DTR_CONTROL))
626 whack_modem(fd, ~TIOCM_DTR, 0);
627 else
628 whack_modem(fd, 0, TIOCM_DTR);
629 #endif
630 #ifdef TIOCM_RTS
631 if (!(shf->ControlHandShake & SERIAL_CTS_HANDSHAKE))
633 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL|SERIAL_RTS_HANDSHAKE)) == 0)
634 whack_modem(fd, ~TIOCM_RTS, 0);
635 else
636 whack_modem(fd, 0, TIOCM_RTS);
638 #endif
640 if (shf->FlowReplace & SERIAL_AUTO_RECEIVE)
641 port.c_iflag |= IXOFF;
642 else
643 port.c_iflag &= ~IXOFF;
644 if (shf->FlowReplace & SERIAL_AUTO_TRANSMIT)
645 port.c_iflag |= IXON;
646 else
647 port.c_iflag &= ~IXON;
648 if (tcsetattr(fd, TCSANOW, &port) == -1)
650 ERR("tcsetattr error '%s'\n", strerror(errno));
651 return errno_to_status( errno );
654 return STATUS_SUCCESS;
657 static NTSTATUS set_line_control(int fd, const SERIAL_LINE_CONTROL* slc)
659 struct termios port;
660 unsigned bytesize, stopbits;
662 if (tcgetattr(fd, &port) == -1)
664 ERR("tcgetattr error '%s'\n", strerror(errno));
665 return errno_to_status( errno );
668 #ifdef IMAXBEL
669 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK|IMAXBEL);
670 #else
671 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK);
672 #endif
673 port.c_iflag |= IGNBRK | INPCK;
674 port.c_oflag &= ~(OPOST);
675 port.c_cflag &= ~(HUPCL);
676 port.c_cflag |= CLOCAL | CREAD;
679 * on FreeBSD, turning off ICANON does not disable IEXTEN,
680 * so we must turn it off explicitly. No harm done on Linux.
682 port.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN);
683 port.c_lflag |= NOFLSH;
685 bytesize = slc->WordLength;
686 stopbits = slc->StopBits;
688 #ifdef CMSPAR
689 port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
690 #else
691 port.c_cflag &= ~(PARENB | PARODD);
692 #endif
694 /* make sure that reads don't block */
695 port.c_cc[VMIN] = 0;
696 port.c_cc[VTIME] = 0;
698 switch (slc->Parity)
700 case NOPARITY: port.c_iflag &= ~INPCK; break;
701 case ODDPARITY: port.c_cflag |= PARENB | PARODD; break;
702 case EVENPARITY: port.c_cflag |= PARENB; break;
703 #ifdef CMSPAR
704 /* Linux defines mark/space (stick) parity */
705 case MARKPARITY: port.c_cflag |= PARENB | PARODD | CMSPAR; break;
706 case SPACEPARITY: port.c_cflag |= PARENB | CMSPAR; break;
707 #else
708 /* try the POSIX way */
709 case MARKPARITY:
710 if (slc->StopBits == ONESTOPBIT)
712 stopbits = TWOSTOPBITS;
713 port.c_iflag &= ~INPCK;
715 else
717 FIXME("Cannot set MARK Parity\n");
718 return STATUS_NOT_SUPPORTED;
720 break;
721 case SPACEPARITY:
722 if (slc->WordLength < 8)
724 bytesize +=1;
725 port.c_iflag &= ~INPCK;
727 else
729 FIXME("Cannot set SPACE Parity\n");
730 return STATUS_NOT_SUPPORTED;
732 break;
733 #endif
734 default:
735 FIXME("Parity %d is not supported\n", slc->Parity);
736 return STATUS_NOT_SUPPORTED;
739 port.c_cflag &= ~CSIZE;
740 switch (bytesize)
742 case 5: port.c_cflag |= CS5; break;
743 case 6: port.c_cflag |= CS6; break;
744 case 7: port.c_cflag |= CS7; break;
745 case 8: port.c_cflag |= CS8; break;
746 default:
747 FIXME("ByteSize %d is not supported\n", bytesize);
748 return STATUS_NOT_SUPPORTED;
751 switch (stopbits)
753 case ONESTOPBIT: port.c_cflag &= ~CSTOPB; break;
754 case ONE5STOPBITS: /* will be selected if bytesize is 5 */
755 case TWOSTOPBITS: port.c_cflag |= CSTOPB; break;
756 default:
757 FIXME("StopBits %d is not supported\n", stopbits);
758 return STATUS_NOT_SUPPORTED;
760 /* otherwise it hangs with pending input*/
761 if (tcsetattr(fd, TCSANOW, &port) == -1)
763 ERR("tcsetattr error '%s'\n", strerror(errno));
764 return errno_to_status( errno );
766 return STATUS_SUCCESS;
769 static NTSTATUS set_queue_size(int fd, const SERIAL_QUEUE_SIZE* sqs)
771 FIXME("insize %d outsize %d unimplemented stub\n", sqs->InSize, sqs->OutSize);
772 return STATUS_SUCCESS;
775 static NTSTATUS set_special_chars(int fd, const SERIAL_CHARS* sc)
777 struct termios port;
779 if (tcgetattr(fd, &port) == -1)
781 ERR("tcgetattr error '%s'\n", strerror(errno));
782 return errno_to_status( errno );
785 port.c_cc[VEOF ] = sc->EofChar;
786 /* FIXME: sc->ErrorChar is not supported */
787 /* FIXME: sc->BreakChar is not supported */
788 /* FIXME: sc->EventChar is not supported */
789 port.c_cc[VSTART] = sc->XonChar;
790 port.c_cc[VSTOP ] = sc->XoffChar;
792 if (tcsetattr(fd, TCSANOW, &port) == -1)
794 ERR("tcsetattr error '%s'\n", strerror(errno));
795 return errno_to_status( errno );
797 return STATUS_SUCCESS;
801 * does not change IXOFF but simulates that IXOFF has been received:
803 static NTSTATUS set_XOff(int fd)
805 if (tcflow(fd, TCOOFF))
807 return errno_to_status( errno );
809 return STATUS_SUCCESS;
813 * does not change IXON but simulates that IXON has been received:
815 static NTSTATUS set_XOn(int fd)
817 if (tcflow(fd, TCOON))
819 return errno_to_status( errno );
821 return STATUS_SUCCESS;
824 /* serial_irq_info
825 * local structure holding the irq values we need for WaitCommEvent()
827 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
828 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
829 * no need to carry them in the internal structure
832 typedef struct serial_irq_info
834 int rx, tx, frame, overrun, parity, brk, buf_overrun, temt;
835 }serial_irq_info;
837 /***********************************************************************
838 * Data needed by the thread polling for the changing CommEvent
840 typedef struct async_commio
842 HANDLE hDevice;
843 DWORD* events;
844 IO_STATUS_BLOCK* iosb;
845 HANDLE hEvent;
846 DWORD evtmask;
847 DWORD cookie;
848 DWORD mstat;
849 DWORD pending_write;
850 serial_irq_info irq_info;
851 } async_commio;
853 /***********************************************************************
854 * Get extended interrupt count info, needed for wait_on
856 static NTSTATUS get_irq_info(int fd, serial_irq_info *irq_info)
858 int out;
860 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCGICOUNT)
861 struct serial_icounter_struct einfo;
862 if (!ioctl(fd, TIOCGICOUNT, &einfo))
864 irq_info->rx = einfo.rx;
865 irq_info->tx = einfo.tx;
866 irq_info->frame = einfo.frame;
867 irq_info->overrun = einfo.overrun;
868 irq_info->parity = einfo.parity;
869 irq_info->brk = einfo.brk;
870 irq_info->buf_overrun = einfo.buf_overrun;
872 else
874 TRACE("TIOCGICOUNT err %s\n", strerror(errno));
875 memset(irq_info,0, sizeof(serial_irq_info));
877 #else
878 memset(irq_info,0, sizeof(serial_irq_info));
879 #endif
881 irq_info->temt = 0;
882 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
883 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
884 if (!ioctl(fd, TIOCSERGETLSR, &out))
886 irq_info->temt = (out & TIOCSER_TEMT) != 0;
887 return STATUS_SUCCESS;
890 TRACE("TIOCSERGETLSR err %s\n", strerror(errno));
891 #endif
892 #ifdef TIOCOUTQ /* otherwise we log when the out queue gets empty */
893 if (!ioctl(fd, TIOCOUTQ, &out))
895 irq_info->temt = out == 0;
896 return STATUS_SUCCESS;
898 TRACE("TIOCOUTQ err %s\n", strerror(errno));
899 return errno_to_status( errno );
900 #endif
901 return STATUS_SUCCESS;
905 static DWORD check_events(int fd, DWORD mask,
906 const serial_irq_info *new,
907 const serial_irq_info *old,
908 DWORD new_mstat, DWORD old_mstat, DWORD pending_write)
910 DWORD ret = 0, queue;
912 TRACE("mask 0x%08x\n", mask);
913 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old->rx, new->rx);
914 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old->tx, new->tx);
915 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old->frame, new->frame);
916 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old->overrun, new->overrun);
917 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old->parity, new->parity);
918 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old->brk, new->brk);
919 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old->buf_overrun, new->buf_overrun);
920 TRACE("old->temt 0x%08x vs. new->temt 0x%08x\n", old->temt, new->temt);
922 if (old->brk != new->brk) ret |= EV_BREAK;
923 if ((old_mstat & MS_CTS_ON ) != (new_mstat & MS_CTS_ON )) ret |= EV_CTS;
924 if ((old_mstat & MS_DSR_ON ) != (new_mstat & MS_DSR_ON )) ret |= EV_DSR;
925 if ((old_mstat & MS_RING_ON) != (new_mstat & MS_RING_ON)) ret |= EV_RING;
926 if ((old_mstat & MS_RLSD_ON) != (new_mstat & MS_RLSD_ON)) ret |= EV_RLSD;
927 if (old->frame != new->frame || old->overrun != new->overrun || old->parity != new->parity) ret |= EV_ERR;
928 if (mask & EV_RXCHAR)
930 queue = 0;
931 #ifdef TIOCINQ
932 if (ioctl(fd, TIOCINQ, &queue))
933 WARN("TIOCINQ returned error\n");
934 #endif
935 if (queue)
936 ret |= EV_RXCHAR;
938 if (mask & EV_TXEMPTY)
940 if ((!old->temt || pending_write) && new->temt)
941 ret |= EV_TXEMPTY;
943 return ret & mask;
946 /***********************************************************************
947 * wait_for_event (INTERNAL)
949 * We need to poll for what is interesting
950 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
953 static void CALLBACK wait_for_event(LPVOID arg)
955 async_commio *commio = arg;
956 int fd, needs_close;
958 if (!server_get_unix_fd( commio->hDevice, FILE_READ_DATA | FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL ))
960 serial_irq_info new_irq_info;
961 DWORD new_mstat, dummy, cookie;
962 LARGE_INTEGER time;
964 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
965 commio->hDevice, fd, commio->evtmask, commio->events, commio->hEvent, &commio->irq_info);
967 time.QuadPart = (ULONGLONG)10000;
968 time.QuadPart = -time.QuadPart;
969 for (;;)
972 * TIOCMIWAIT is not adequate
974 * FIXME:
975 * We don't handle the EV_RXFLAG (the eventchar)
977 NtDelayExecution(FALSE, &time);
978 get_irq_info(fd, &new_irq_info);
979 if (get_modem_status(fd, &new_mstat))
981 TRACE("get_modem_status failed\n");
982 *commio->events = 0;
983 break;
985 *commio->events = check_events(fd, commio->evtmask,
986 &new_irq_info, &commio->irq_info,
987 new_mstat, commio->mstat, commio->pending_write);
988 if (*commio->events) break;
989 get_wait_mask(commio->hDevice, &dummy, &cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, FALSE);
990 if (commio->cookie != cookie)
992 *commio->events = 0;
993 break;
996 if (needs_close) close( fd );
998 if (commio->iosb)
1000 if (*commio->events)
1002 commio->iosb->u.Status = STATUS_SUCCESS;
1003 commio->iosb->Information = sizeof(DWORD);
1005 else
1006 commio->iosb->u.Status = STATUS_CANCELLED;
1008 stop_waiting(commio->hDevice);
1009 if (commio->hEvent) NtSetEvent(commio->hEvent, NULL);
1010 free( commio );
1011 NtTerminateThread( GetCurrentThread(), 0 );
1014 static NTSTATUS wait_on(HANDLE hDevice, int fd, HANDLE hEvent, PIO_STATUS_BLOCK piosb, DWORD* events)
1016 async_commio* commio;
1017 NTSTATUS status;
1018 HANDLE handle;
1020 if ((status = NtResetEvent(hEvent, NULL)))
1021 return status;
1023 commio = malloc( sizeof(async_commio) );
1024 if (!commio) return STATUS_NO_MEMORY;
1026 commio->hDevice = hDevice;
1027 commio->events = events;
1028 commio->iosb = piosb;
1029 commio->hEvent = hEvent;
1030 commio->pending_write = 0;
1031 status = get_wait_mask(commio->hDevice, &commio->evtmask, &commio->cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, TRUE);
1032 if (status)
1034 free( commio );
1035 return status;
1038 /* We may never return, if some capabilities miss
1039 * Return error in that case
1041 #if !defined(TIOCINQ)
1042 if (commio->evtmask & EV_RXCHAR)
1043 goto error_caps;
1044 #endif
1045 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1046 if (commio->evtmask & EV_TXEMPTY)
1047 goto error_caps;
1048 #endif
1049 #if !defined(TIOCMGET)
1050 if (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD))
1051 goto error_caps;
1052 #endif
1053 #if !defined(TIOCM_CTS)
1054 if (commio->evtmask & EV_CTS)
1055 goto error_caps;
1056 #endif
1057 #if !defined(TIOCM_DSR)
1058 if (commio->evtmask & EV_DSR)
1059 goto error_caps;
1060 #endif
1061 #if !defined(TIOCM_RNG)
1062 if (commio->evtmask & EV_RING)
1063 goto error_caps;
1064 #endif
1065 #if !defined(TIOCM_CAR)
1066 if (commio->evtmask & EV_RLSD)
1067 goto error_caps;
1068 #endif
1069 if (commio->evtmask & EV_RXFLAG)
1070 FIXME("EV_RXFLAG not handled\n");
1072 if ((status = get_irq_info(fd, &commio->irq_info)) &&
1073 (commio->evtmask & (EV_BREAK | EV_ERR)))
1074 goto out_now;
1076 if ((status = get_modem_status(fd, &commio->mstat)) &&
1077 (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD)))
1078 goto out_now;
1080 /* We might have received something or the TX buffer is delivered */
1081 *events = check_events(fd, commio->evtmask,
1082 &commio->irq_info, &commio->irq_info,
1083 commio->mstat, commio->mstat, commio->pending_write);
1084 if (*events)
1086 status = STATUS_SUCCESS;
1087 goto out_now;
1090 /* create the worker thread for the task */
1091 /* FIXME: should use async I/O instead */
1092 status = NtCreateThreadEx( &handle, THREAD_ALL_ACCESS, NULL, GetCurrentProcess(),
1093 wait_for_event, commio, 0, 0, 0, 0, NULL );
1094 if (status != STATUS_SUCCESS) goto out_now;
1095 NtClose( handle );
1096 return STATUS_PENDING;
1098 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1099 error_caps:
1100 FIXME("Returning error because of missing capabilities\n");
1101 status = STATUS_INVALID_PARAMETER;
1102 #endif
1103 out_now:
1104 stop_waiting(commio->hDevice);
1105 free( commio );
1106 return status;
1109 static NTSTATUS xmit_immediate(HANDLE hDevice, int fd, const char* ptr)
1111 /* FIXME: not perfect as it should bypass the in-queue */
1112 WARN("(%p,'%c') not perfect!\n", hDevice, *ptr);
1113 if (write(fd, ptr, 1) != 1)
1114 return errno_to_status( errno );
1115 return STATUS_SUCCESS;
1118 static NTSTATUS io_control( HANDLE device, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1119 IO_STATUS_BLOCK *io, ULONG code, void *in_buffer,
1120 ULONG in_size, void *out_buffer, ULONG out_size )
1122 DWORD sz = 0, access = FILE_READ_DATA;
1123 NTSTATUS status = STATUS_SUCCESS;
1124 int fd = -1, needs_close = 0;
1125 enum server_fd_type type;
1127 TRACE("%p %s %p %d %p %d %p\n",
1128 device, iocode2str(code), in_buffer, in_size, out_buffer, out_size, io);
1130 switch (code)
1132 case IOCTL_SERIAL_GET_TIMEOUTS:
1133 case IOCTL_SERIAL_SET_TIMEOUTS:
1134 case IOCTL_SERIAL_GET_WAIT_MASK:
1135 case IOCTL_SERIAL_SET_WAIT_MASK:
1136 /* these are handled on the server side */
1137 return STATUS_NOT_SUPPORTED;
1140 io->Information = 0;
1142 if ((status = server_get_unix_fd( device, access, &fd, &needs_close, &type, NULL ))) goto error;
1143 if (type != FD_TYPE_SERIAL)
1145 if (needs_close) close( fd );
1146 status = STATUS_OBJECT_TYPE_MISMATCH;
1147 goto error;
1150 switch (code)
1152 case IOCTL_SERIAL_CLR_DTR:
1153 #ifdef TIOCM_DTR
1154 if (whack_modem(fd, ~TIOCM_DTR, 0) == -1) status = errno_to_status( errno );
1155 #else
1156 status = STATUS_NOT_SUPPORTED;
1157 #endif
1158 break;
1159 case IOCTL_SERIAL_CLR_RTS:
1160 #ifdef TIOCM_RTS
1161 if (whack_modem(fd, ~TIOCM_RTS, 0) == -1) status = errno_to_status( errno );
1162 #else
1163 status = STATUS_NOT_SUPPORTED;
1164 #endif
1165 break;
1166 case IOCTL_SERIAL_GET_BAUD_RATE:
1167 if (out_buffer && out_size == sizeof(SERIAL_BAUD_RATE))
1169 if (!(status = get_baud_rate(fd, out_buffer)))
1170 sz = sizeof(SERIAL_BAUD_RATE);
1172 else
1173 status = STATUS_INVALID_PARAMETER;
1174 break;
1175 case IOCTL_SERIAL_GET_CHARS:
1176 if (out_buffer && out_size == sizeof(SERIAL_CHARS))
1178 if (!(status = get_special_chars(fd, out_buffer)))
1179 sz = sizeof(SERIAL_CHARS);
1181 else
1182 status = STATUS_INVALID_PARAMETER;
1183 break;
1184 case IOCTL_SERIAL_GET_COMMSTATUS:
1185 if (out_buffer && out_size == sizeof(SERIAL_STATUS))
1187 if (!(status = get_status(fd, out_buffer)))
1188 sz = sizeof(SERIAL_STATUS);
1190 else status = STATUS_INVALID_PARAMETER;
1191 break;
1192 case IOCTL_SERIAL_GET_HANDFLOW:
1193 if (out_buffer && out_size == sizeof(SERIAL_HANDFLOW))
1195 if (!(status = get_hand_flow(fd, out_buffer)))
1196 sz = sizeof(SERIAL_HANDFLOW);
1198 else
1199 status = STATUS_INVALID_PARAMETER;
1200 break;
1201 case IOCTL_SERIAL_GET_LINE_CONTROL:
1202 if (out_buffer && out_size == sizeof(SERIAL_LINE_CONTROL))
1204 if (!(status = get_line_control(fd, out_buffer)))
1205 sz = sizeof(SERIAL_LINE_CONTROL);
1207 else
1208 status = STATUS_INVALID_PARAMETER;
1209 break;
1210 case IOCTL_SERIAL_GET_MODEMSTATUS:
1211 if (out_buffer && out_size == sizeof(DWORD))
1213 if (!(status = get_modem_status(fd, out_buffer)))
1214 sz = sizeof(DWORD);
1216 else status = STATUS_INVALID_PARAMETER;
1217 break;
1218 case IOCTL_SERIAL_GET_PROPERTIES:
1219 if (out_buffer && out_size == sizeof(SERIAL_COMMPROP))
1221 if (!(status = get_properties(fd, out_buffer)))
1222 sz = sizeof(SERIAL_COMMPROP);
1224 else status = STATUS_INVALID_PARAMETER;
1225 break;
1226 case IOCTL_SERIAL_IMMEDIATE_CHAR:
1227 if (in_buffer && in_size == sizeof(CHAR))
1228 status = xmit_immediate(device, fd, in_buffer);
1229 else
1230 status = STATUS_INVALID_PARAMETER;
1231 break;
1232 case IOCTL_SERIAL_PURGE:
1233 if (in_buffer && in_size == sizeof(DWORD))
1234 status = purge(fd, *(DWORD*)in_buffer);
1235 else
1236 status = STATUS_INVALID_PARAMETER;
1237 break;
1238 case IOCTL_SERIAL_RESET_DEVICE:
1239 FIXME("Unsupported\n");
1240 break;
1241 case IOCTL_SERIAL_SET_BAUD_RATE:
1242 if (in_buffer && in_size == sizeof(SERIAL_BAUD_RATE))
1243 status = set_baud_rate(fd, in_buffer);
1244 else
1245 status = STATUS_INVALID_PARAMETER;
1246 break;
1247 case IOCTL_SERIAL_SET_BREAK_OFF:
1248 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1249 if (ioctl(fd, TIOCCBRK, 0) == -1)
1251 TRACE("ioctl failed\n");
1252 status = errno_to_status( errno );
1254 #else
1255 FIXME("ioctl not available\n");
1256 status = STATUS_NOT_SUPPORTED;
1257 #endif
1258 break;
1259 case IOCTL_SERIAL_SET_BREAK_ON:
1260 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1261 if (ioctl(fd, TIOCSBRK, 0) == -1)
1263 TRACE("ioctl failed\n");
1264 status = errno_to_status( errno );
1266 #else
1267 FIXME("ioctl not available\n");
1268 status = STATUS_NOT_SUPPORTED;
1269 #endif
1270 break;
1271 case IOCTL_SERIAL_SET_CHARS:
1272 if (in_buffer && in_size == sizeof(SERIAL_CHARS))
1273 status = set_special_chars(fd, in_buffer);
1274 else
1275 status = STATUS_INVALID_PARAMETER;
1276 break;
1277 case IOCTL_SERIAL_SET_DTR:
1278 #ifdef TIOCM_DTR
1279 if (whack_modem(fd, 0, TIOCM_DTR) == -1) status = errno_to_status( errno );
1280 #else
1281 status = STATUS_NOT_SUPPORTED;
1282 #endif
1283 break;
1284 case IOCTL_SERIAL_SET_HANDFLOW:
1285 if (in_buffer && in_size == sizeof(SERIAL_HANDFLOW))
1286 status = set_handflow(fd, in_buffer);
1287 else
1288 status = STATUS_INVALID_PARAMETER;
1289 break;
1290 case IOCTL_SERIAL_SET_LINE_CONTROL:
1291 if (in_buffer && in_size == sizeof(SERIAL_LINE_CONTROL))
1292 status = set_line_control(fd, in_buffer);
1293 else
1294 status = STATUS_INVALID_PARAMETER;
1295 break;
1296 case IOCTL_SERIAL_SET_QUEUE_SIZE:
1297 if (in_buffer && in_size == sizeof(SERIAL_QUEUE_SIZE))
1298 status = set_queue_size(fd, in_buffer);
1299 else
1300 status = STATUS_INVALID_PARAMETER;
1301 break;
1302 case IOCTL_SERIAL_SET_RTS:
1303 #ifdef TIOCM_RTS
1304 if (whack_modem(fd, 0, TIOCM_RTS) == -1) status = errno_to_status( errno );
1305 #else
1306 status = STATUS_NOT_SUPPORTED;
1307 #endif
1308 break;
1309 case IOCTL_SERIAL_SET_XOFF:
1310 status = set_XOff(fd);
1311 break;
1312 case IOCTL_SERIAL_SET_XON:
1313 status = set_XOn(fd);
1314 break;
1315 case IOCTL_SERIAL_WAIT_ON_MASK:
1316 if (out_buffer && out_size == sizeof(DWORD))
1318 if (!(status = wait_on(device, fd, event, io, out_buffer)))
1319 sz = sizeof(DWORD);
1321 else
1322 status = STATUS_INVALID_PARAMETER;
1323 break;
1324 default:
1325 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1326 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xFFF, code & 3);
1327 sz = 0;
1328 status = STATUS_INVALID_PARAMETER;
1329 break;
1331 if (needs_close) close( fd );
1332 error:
1333 io->u.Status = status;
1334 io->Information = sz;
1335 if (event && status != STATUS_PENDING) NtSetEvent(event, NULL);
1336 return status;
1339 /******************************************************************
1340 * serial_DeviceIoControl
1342 NTSTATUS serial_DeviceIoControl( HANDLE device, HANDLE event, PIO_APC_ROUTINE apc, void *apc_user,
1343 IO_STATUS_BLOCK *io, ULONG code, void *in_buffer,
1344 ULONG in_size, void *out_buffer, ULONG out_size )
1346 NTSTATUS status;
1348 if (code == IOCTL_SERIAL_WAIT_ON_MASK)
1350 HANDLE hev = event;
1352 /* this is an ioctl we implement in a non blocking way if event is not null
1353 * so we have to explicitly wait if no event is provided
1355 if (!hev)
1357 OBJECT_ATTRIBUTES attr;
1359 attr.Length = sizeof(attr);
1360 attr.RootDirectory = 0;
1361 attr.ObjectName = NULL;
1362 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1363 attr.SecurityDescriptor = NULL;
1364 attr.SecurityQualityOfService = NULL;
1365 status = NtCreateEvent(&hev, EVENT_ALL_ACCESS, &attr, SynchronizationEvent, FALSE);
1367 if (status) return status;
1369 status = io_control( device, hev, apc, apc_user, io, code,
1370 in_buffer, in_size, out_buffer, out_size );
1371 if (hev != event)
1373 if (status == STATUS_PENDING)
1375 NtWaitForSingleObject(hev, FALSE, NULL);
1376 status = STATUS_SUCCESS;
1378 NtClose(hev);
1381 else status = io_control( device, event, apc, apc_user, io, code,
1382 in_buffer, in_size, out_buffer, out_size );
1383 return status;
1386 NTSTATUS serial_FlushBuffersFile( int fd )
1388 #ifdef HAVE_TCDRAIN
1389 while (tcdrain( fd ) == -1)
1391 if (errno != EINTR) return errno_to_status( errno );
1393 return STATUS_SUCCESS;
1394 #elif defined(TIOCDRAIN)
1395 while (ioctl( fd, TIOCDRAIN ) == -1)
1397 if (errno != EINTR) return errno_to_status( errno );
1399 return STATUS_SUCCESS;
1400 #elif defined(TCSBRK)
1401 while (ioctl( fd, TCSBRK, 1 ) == -1)
1403 if (errno != EINTR) return errno_to_status( errno );
1405 return STATUS_SUCCESS;
1406 #else
1407 ERR( "not supported\n" );
1408 return STATUS_NOT_IMPLEMENTED;
1409 #endif