wined3d: Return void from wined3d_device_set_light_enable().
[wine.git] / dlls / ntdll / serial.c
blob66fb840167bbd11cdbb14ff92fd14766b4bbe623
1 /* Main file for COMM support
3 * DEC 93 Erik Bos <erik@xs4all.nl>
4 * Copyright 1996 Marcus Meissner
5 * Copyright 2005,2006 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <errno.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #ifdef HAVE_TERMIOS_H
31 #include <termios.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <fcntl.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
39 #endif
40 #include <sys/types.h>
41 #ifdef HAVE_SYS_FILIO_H
42 # include <sys/filio.h>
43 #endif
44 #ifdef HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
46 #endif
47 #ifdef HAVE_SYS_POLL_H
48 # include <sys/poll.h>
49 #endif
50 #ifdef HAVE_SYS_MODEM_H
51 # include <sys/modem.h>
52 #endif
53 #ifdef HAVE_SYS_STRTIO_H
54 # include <sys/strtio.h>
55 #endif
57 #include "ntstatus.h"
58 #define WIN32_NO_STATUS
59 #define NONAMELESSUNION
60 #include "windef.h"
61 #include "winternl.h"
62 #include "winioctl.h"
63 #include "ddk/ntddser.h"
64 #include "ntdll_misc.h"
65 #include "wine/server.h"
66 #include "wine/library.h"
67 #include "wine/debug.h"
69 #ifdef HAVE_LINUX_SERIAL_H
70 #ifdef HAVE_ASM_TYPES_H
71 #include <asm/types.h>
72 #endif
73 #include <linux/serial.h>
74 #endif
76 #if !defined(TIOCINQ) && defined(FIONREAD)
77 #define TIOCINQ FIONREAD
78 #endif
80 WINE_DEFAULT_DEBUG_CHANNEL(comm);
82 static const char* iocode2str(DWORD ioc)
84 switch (ioc)
86 #define X(x) case (x): return #x
87 X(IOCTL_SERIAL_CLEAR_STATS);
88 X(IOCTL_SERIAL_CLR_DTR);
89 X(IOCTL_SERIAL_CLR_RTS);
90 X(IOCTL_SERIAL_CONFIG_SIZE);
91 X(IOCTL_SERIAL_GET_BAUD_RATE);
92 X(IOCTL_SERIAL_GET_CHARS);
93 X(IOCTL_SERIAL_GET_COMMSTATUS);
94 X(IOCTL_SERIAL_GET_DTRRTS);
95 X(IOCTL_SERIAL_GET_HANDFLOW);
96 X(IOCTL_SERIAL_GET_LINE_CONTROL);
97 X(IOCTL_SERIAL_GET_MODEM_CONTROL);
98 X(IOCTL_SERIAL_GET_MODEMSTATUS);
99 X(IOCTL_SERIAL_GET_PROPERTIES);
100 X(IOCTL_SERIAL_GET_STATS);
101 X(IOCTL_SERIAL_GET_TIMEOUTS);
102 X(IOCTL_SERIAL_GET_WAIT_MASK);
103 X(IOCTL_SERIAL_IMMEDIATE_CHAR);
104 X(IOCTL_SERIAL_LSRMST_INSERT);
105 X(IOCTL_SERIAL_PURGE);
106 X(IOCTL_SERIAL_RESET_DEVICE);
107 X(IOCTL_SERIAL_SET_BAUD_RATE);
108 X(IOCTL_SERIAL_SET_BREAK_ON);
109 X(IOCTL_SERIAL_SET_BREAK_OFF);
110 X(IOCTL_SERIAL_SET_CHARS);
111 X(IOCTL_SERIAL_SET_DTR);
112 X(IOCTL_SERIAL_SET_FIFO_CONTROL);
113 X(IOCTL_SERIAL_SET_HANDFLOW);
114 X(IOCTL_SERIAL_SET_LINE_CONTROL);
115 X(IOCTL_SERIAL_SET_MODEM_CONTROL);
116 X(IOCTL_SERIAL_SET_QUEUE_SIZE);
117 X(IOCTL_SERIAL_SET_RTS);
118 X(IOCTL_SERIAL_SET_TIMEOUTS);
119 X(IOCTL_SERIAL_SET_WAIT_MASK);
120 X(IOCTL_SERIAL_SET_XOFF);
121 X(IOCTL_SERIAL_SET_XON);
122 X(IOCTL_SERIAL_WAIT_ON_MASK);
123 X(IOCTL_SERIAL_XOFF_COUNTER);
124 #undef X
125 default: { static char tmp[32]; sprintf(tmp, "IOCTL_SERIAL_%d\n", ioc); return tmp; }
129 static NTSTATUS get_baud_rate(int fd, SERIAL_BAUD_RATE* sbr)
131 struct termios port;
132 int speed;
134 if (tcgetattr(fd, &port) == -1)
136 ERR("tcgetattr error '%s'\n", strerror(errno));
137 return FILE_GetNtStatus();
139 speed = cfgetospeed(&port);
140 switch (speed)
142 case B0: sbr->BaudRate = 0; break;
143 case B50: sbr->BaudRate = 50; break;
144 case B75: sbr->BaudRate = 75; break;
145 case B110: sbr->BaudRate = 110; break;
146 case B134: sbr->BaudRate = 134; break;
147 case B150: sbr->BaudRate = 150; break;
148 case B200: sbr->BaudRate = 200; break;
149 case B300: sbr->BaudRate = 300; break;
150 case B600: sbr->BaudRate = 600; break;
151 case B1200: sbr->BaudRate = 1200; break;
152 case B1800: sbr->BaudRate = 1800; break;
153 case B2400: sbr->BaudRate = 2400; break;
154 case B4800: sbr->BaudRate = 4800; break;
155 case B9600: sbr->BaudRate = 9600; break;
156 case B19200: sbr->BaudRate = 19200; break;
157 case B38400: sbr->BaudRate = 38400; break;
158 #ifdef B57600
159 case B57600: sbr->BaudRate = 57600; break;
160 #endif
161 #ifdef B115200
162 case B115200: sbr->BaudRate = 115200; break;
163 #endif
164 #ifdef B230400
165 case B230400: sbr->BaudRate = 230400; break;
166 #endif
167 #ifdef B460800
168 case B460800: sbr->BaudRate = 460800; break;
169 #endif
170 #ifdef B500000
171 case B500000: sbr->BaudRate = 500000; break;
172 #endif
173 #ifdef B921600
174 case B921600: sbr->BaudRate = 921600; break;
175 #endif
176 #ifdef B1000000
177 case B1000000: sbr->BaudRate = 1000000; break;
178 #endif
179 #ifdef B1152000
180 case B1152000: sbr->BaudRate = 1152000; break;
181 #endif
182 #ifdef B1500000
183 case B1500000: sbr->BaudRate = 1500000; break;
184 #endif
185 #ifdef B2000000
186 case B2000000: sbr->BaudRate = 2000000; break;
187 #endif
188 #ifdef B2500000
189 case B2500000: sbr->BaudRate = 2500000; break;
190 #endif
191 #ifdef B3000000
192 case B3000000: sbr->BaudRate = 3000000; break;
193 #endif
194 #ifdef B3500000
195 case B3500000: sbr->BaudRate = 3500000; break;
196 #endif
197 #ifdef B4000000
198 case B4000000: sbr->BaudRate = 4000000; break;
199 #endif
200 default:
201 ERR("unknown speed %x\n", speed);
202 return STATUS_INVALID_PARAMETER;
204 return STATUS_SUCCESS;
207 static NTSTATUS get_hand_flow(int fd, SERIAL_HANDFLOW* shf)
209 int stat = 0;
210 struct termios port;
212 if (tcgetattr(fd, &port) == -1)
214 ERR("tcgetattr error '%s'\n", strerror(errno));
215 return FILE_GetNtStatus();
217 /* termios does not support DTR/DSR flow control */
218 shf->ControlHandShake = 0;
219 shf->FlowReplace = 0;
220 #ifdef TIOCMGET
221 if (ioctl(fd, TIOCMGET, &stat) == -1)
223 WARN("ioctl error '%s'\n", strerror(errno));
224 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
225 shf->FlowReplace |= SERIAL_RTS_CONTROL;
227 #else
228 WARN("Setting DTR/RTS to enabled by default\n");
229 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
230 shf->FlowReplace |= SERIAL_RTS_CONTROL;
231 #endif
232 #ifdef TIOCM_DTR
233 if (stat & TIOCM_DTR)
234 #endif
235 shf->ControlHandShake |= SERIAL_DTR_CONTROL;
236 #ifdef CRTSCTS
237 if (port.c_cflag & CRTSCTS)
239 shf->FlowReplace |= SERIAL_RTS_CONTROL;
240 shf->ControlHandShake |= SERIAL_CTS_HANDSHAKE;
242 else
243 #endif
245 #ifdef TIOCM_RTS
246 if (stat & TIOCM_RTS)
247 #endif
248 shf->FlowReplace |= SERIAL_RTS_CONTROL;
250 if (port.c_iflag & IXOFF)
251 shf->FlowReplace |= SERIAL_AUTO_RECEIVE;
252 if (port.c_iflag & IXON)
253 shf->FlowReplace |= SERIAL_AUTO_TRANSMIT;
255 shf->XonLimit = 10;
256 shf->XoffLimit = 10;
257 return STATUS_SUCCESS;
260 static NTSTATUS get_line_control(int fd, SERIAL_LINE_CONTROL* slc)
262 struct termios port;
264 if (tcgetattr(fd, &port) == -1)
266 ERR("tcgetattr error '%s'\n", strerror(errno));
267 return FILE_GetNtStatus();
270 #ifdef CMSPAR
271 switch (port.c_cflag & (PARENB | PARODD | CMSPAR))
272 #else
273 switch (port.c_cflag & (PARENB | PARODD))
274 #endif
276 case 0: slc->Parity = NOPARITY; break;
277 case PARENB: slc->Parity = EVENPARITY; break;
278 case PARENB|PARODD: slc->Parity = ODDPARITY; break;
279 #ifdef CMSPAR
280 case PARENB|PARODD|CMSPAR: slc->Parity = MARKPARITY; break;
281 case PARENB|CMSPAR: slc->Parity = SPACEPARITY; break;
282 #endif
284 switch (port.c_cflag & CSIZE)
286 case CS5: slc->WordLength = 5; break;
287 case CS6: slc->WordLength = 6; break;
288 case CS7: slc->WordLength = 7; break;
289 case CS8: slc->WordLength = 8; break;
290 default: ERR("unknown size %x\n", (UINT)(port.c_cflag & CSIZE));
293 if (port.c_cflag & CSTOPB)
295 if (slc->WordLength == 5)
296 slc->StopBits = ONE5STOPBITS;
297 else
298 slc->StopBits = TWOSTOPBITS;
300 else
301 slc->StopBits = ONESTOPBIT;
303 return STATUS_SUCCESS;
306 static NTSTATUS get_modem_status(int fd, DWORD* lpModemStat)
308 NTSTATUS status = STATUS_NOT_SUPPORTED;
309 int mstat;
311 *lpModemStat = 0;
312 #ifdef TIOCMGET
313 if (!ioctl(fd, TIOCMGET, &mstat))
315 #ifdef TIOCM_CTS
316 if (mstat & TIOCM_CTS) *lpModemStat |= MS_CTS_ON;
317 #endif
318 #ifdef TIOCM_DSR
319 if (mstat & TIOCM_DSR) *lpModemStat |= MS_DSR_ON;
320 #endif
321 #ifdef TIOCM_RNG
322 if (mstat & TIOCM_RNG) *lpModemStat |= MS_RING_ON;
323 #endif
324 #ifdef TIOCM_CAR
325 /* FIXME: Not really sure about RLSD UB 990810 */
326 if (mstat & TIOCM_CAR) *lpModemStat |= MS_RLSD_ON;
327 #endif
328 TRACE("%04x -> %s%s%s%s\n", mstat,
329 (*lpModemStat & MS_RLSD_ON) ? "MS_RLSD_ON " : "",
330 (*lpModemStat & MS_RING_ON) ? "MS_RING_ON " : "",
331 (*lpModemStat & MS_DSR_ON) ? "MS_DSR_ON " : "",
332 (*lpModemStat & MS_CTS_ON) ? "MS_CTS_ON " : "");
333 return STATUS_SUCCESS;
335 WARN("TIOCMGET err %s\n", strerror(errno));
336 status = FILE_GetNtStatus();
337 #endif
338 return status;
341 static NTSTATUS get_properties(int fd, SERIAL_COMMPROP *prop)
343 /* FIXME: get actual properties from the device */
344 memset( prop, 0, sizeof(*prop) );
345 prop->PacketLength = 1;
346 prop->PacketVersion = 1;
347 prop->ServiceMask = SP_SERIALCOMM;
348 prop->MaxTxQueue = 4096;
349 prop->MaxRxQueue = 4096;
350 prop->MaxBaud = BAUD_115200;
351 prop->ProvSubType = PST_RS232;
352 prop->ProvCapabilities = PCF_DTRDSR | PCF_PARITY_CHECK | PCF_RTSCTS | PCF_TOTALTIMEOUTS | PCF_INTTIMEOUTS;
353 prop->SettableParams = SP_BAUD | SP_DATABITS | SP_HANDSHAKING |
354 SP_PARITY | SP_PARITY_CHECK | SP_STOPBITS ;
355 prop->SettableBaud = BAUD_075 | BAUD_110 | BAUD_134_5 | BAUD_150 |
356 BAUD_300 | BAUD_600 | BAUD_1200 | BAUD_1800 | BAUD_2400 | BAUD_4800 |
357 BAUD_9600 | BAUD_19200 | BAUD_38400 | BAUD_57600 | BAUD_115200 ;
358 prop->SettableData = DATABITS_5 | DATABITS_6 | DATABITS_7 | DATABITS_8 ;
359 prop->SettableStopParity = STOPBITS_10 | STOPBITS_15 | STOPBITS_20 |
360 PARITY_NONE | PARITY_ODD |PARITY_EVEN | PARITY_MARK | PARITY_SPACE;
361 prop->CurrentTxQueue = prop->MaxTxQueue;
362 prop->CurrentRxQueue = prop->MaxRxQueue;
363 return STATUS_SUCCESS;
366 static NTSTATUS get_special_chars(int fd, SERIAL_CHARS* sc)
368 struct termios port;
370 if (tcgetattr(fd, &port) == -1)
372 ERR("tcgetattr error '%s'\n", strerror(errno));
373 return FILE_GetNtStatus();
375 sc->EofChar = port.c_cc[VEOF];
376 sc->ErrorChar = 0xFF;
377 sc->BreakChar = 0; /* FIXME */
378 sc->EventChar = 0; /* FIXME */
379 sc->XonChar = port.c_cc[VSTART];
380 sc->XoffChar = port.c_cc[VSTOP];
382 return STATUS_SUCCESS;
385 static NTSTATUS get_status(int fd, SERIAL_STATUS* ss)
387 NTSTATUS status = STATUS_SUCCESS;
389 ss->Errors = 0;
390 ss->HoldReasons = 0;
391 ss->EofReceived = FALSE;
392 ss->WaitForImmediate = FALSE;
393 #ifdef TIOCOUTQ
394 if (ioctl(fd, TIOCOUTQ, &ss->AmountInOutQueue) == -1)
396 WARN("ioctl returned error\n");
397 status = FILE_GetNtStatus();
399 #else
400 ss->AmountInOutQueue = 0; /* FIXME: find a different way to find out */
401 #endif
403 #ifdef TIOCINQ
404 if (ioctl(fd, TIOCINQ, &ss->AmountInInQueue))
406 WARN("ioctl returned error\n");
407 status = FILE_GetNtStatus();
409 #else
410 ss->AmountInInQueue = 0; /* FIXME: find a different way to find out */
411 #endif
412 return status;
415 static void stop_waiting( HANDLE handle )
417 NTSTATUS status;
419 SERVER_START_REQ( set_serial_info )
421 req->handle = wine_server_obj_handle( handle );
422 req->flags = SERIALINFO_PENDING_WAIT;
423 if ((status = wine_server_call( req )))
424 ERR("failed to clear waiting state: %#x\n", status);
426 SERVER_END_REQ;
429 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD *mask, DWORD *cookie, DWORD *pending_write, BOOL start_wait)
431 NTSTATUS status;
433 SERVER_START_REQ( get_serial_info )
435 req->handle = wine_server_obj_handle( hDevice );
436 req->flags = pending_write ? SERIALINFO_PENDING_WRITE : 0;
437 if (start_wait) req->flags |= SERIALINFO_PENDING_WAIT;
438 if (!(status = wine_server_call( req )))
440 *mask = reply->eventmask;
441 if (cookie) *cookie = reply->cookie;
442 if (pending_write) *pending_write = reply->pending_write;
445 SERVER_END_REQ;
446 return status;
449 static NTSTATUS purge(int fd, DWORD flags)
452 ** not exactly sure how these are different
453 ** Perhaps if we had our own internal queues, one flushes them
454 ** and the other flushes the kernel's buffers.
456 if (flags & PURGE_TXABORT) tcflush(fd, TCOFLUSH);
457 if (flags & PURGE_RXABORT) tcflush(fd, TCIFLUSH);
458 if (flags & PURGE_TXCLEAR) tcflush(fd, TCOFLUSH);
459 if (flags & PURGE_RXCLEAR) tcflush(fd, TCIFLUSH);
460 return STATUS_SUCCESS;
463 static NTSTATUS set_baud_rate(int fd, const SERIAL_BAUD_RATE* sbr)
465 struct termios port;
467 if (tcgetattr(fd, &port) == -1)
469 ERR("tcgetattr error '%s'\n", strerror(errno));
470 return FILE_GetNtStatus();
473 switch (sbr->BaudRate)
475 case 0: cfsetospeed( &port, B0 ); break;
476 case 50: cfsetospeed( &port, B50 ); break;
477 case 75: cfsetospeed( &port, B75 ); break;
478 case 110:
479 case CBR_110: cfsetospeed( &port, B110 ); break;
480 case 134: cfsetospeed( &port, B134 ); break;
481 case 150: cfsetospeed( &port, B150 ); break;
482 case 200: cfsetospeed( &port, B200 ); break;
483 case 300:
484 case CBR_300: cfsetospeed( &port, B300 ); break;
485 case 600:
486 case CBR_600: cfsetospeed( &port, B600 ); break;
487 case 1200:
488 case CBR_1200: cfsetospeed( &port, B1200 ); break;
489 case 1800: cfsetospeed( &port, B1800 ); break;
490 case 2400:
491 case CBR_2400: cfsetospeed( &port, B2400 ); break;
492 case 4800:
493 case CBR_4800: cfsetospeed( &port, B4800 ); break;
494 case 9600:
495 case CBR_9600: cfsetospeed( &port, B9600 ); break;
496 case 19200:
497 case CBR_19200: cfsetospeed( &port, B19200 ); break;
498 case 38400:
499 case CBR_38400: cfsetospeed( &port, B38400 ); break;
500 #ifdef B57600
501 case 57600: cfsetospeed( &port, B57600 ); break;
502 #endif
503 #ifdef B115200
504 case 115200: cfsetospeed( &port, B115200 ); break;
505 #endif
506 #ifdef B230400
507 case 230400: cfsetospeed( &port, B230400 ); break;
508 #endif
509 #ifdef B460800
510 case 460800: cfsetospeed( &port, B460800 ); break;
511 #endif
512 #ifdef B500000
513 case 500000: cfsetospeed( &port, B500000 ); break;
514 #endif
515 #ifdef B921600
516 case 921600: cfsetospeed( &port, B921600 ); break;
517 #endif
518 #ifdef B1000000
519 case 1000000: cfsetospeed( &port, B1000000 ); break;
520 #endif
521 #ifdef B1152000
522 case 1152000: cfsetospeed( &port, B1152000 ); break;
523 #endif
524 #ifdef B1500000
525 case 1500000: cfsetospeed( &port, B1500000 ); break;
526 #endif
527 #ifdef B2000000
528 case 2000000: cfsetospeed( &port, B2000000 ); break;
529 #endif
530 #ifdef B2500000
531 case 2500000: cfsetospeed( &port, B2500000 ); break;
532 #endif
533 #ifdef B3000000
534 case 3000000: cfsetospeed( &port, B3000000 ); break;
535 #endif
536 #ifdef B3500000
537 case 3500000: cfsetospeed( &port, B3500000 ); break;
538 #endif
539 #ifdef B4000000
540 case 4000000: cfsetospeed( &port, B4000000 ); break;
541 #endif
542 default:
543 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
545 struct serial_struct nuts;
546 int arby;
548 ioctl(fd, TIOCGSERIAL, &nuts);
549 nuts.custom_divisor = nuts.baud_base / sbr->BaudRate;
550 if (!(nuts.custom_divisor)) nuts.custom_divisor = 1;
551 arby = nuts.baud_base / nuts.custom_divisor;
552 nuts.flags &= ~ASYNC_SPD_MASK;
553 nuts.flags |= ASYNC_SPD_CUST;
554 WARN("You (or a program acting at your behest) have specified\n"
555 "a non-standard baud rate %d. Wine will set the rate to %d,\n"
556 "which is as close as we can get by our present understanding of your\n"
557 "hardware. I hope you know what you are doing. Any disruption Wine\n"
558 "has caused to your linux system can be undone with setserial\n"
559 "(see man setserial). If you have incapacitated a Hayes type modem,\n"
560 "reset it and it will probably recover.\n", sbr->BaudRate, arby);
561 ioctl(fd, TIOCSSERIAL, &nuts);
562 cfsetospeed( &port, B38400 );
564 break;
565 #else /* Don't have linux/serial.h or lack TIOCSSERIAL */
566 ERR("baudrate %d\n", sbr->BaudRate);
567 return STATUS_NOT_SUPPORTED;
568 #endif /* Don't have linux/serial.h or lack TIOCSSERIAL */
570 cfsetispeed( &port, cfgetospeed(&port) );
571 if (tcsetattr(fd, TCSANOW, &port) == -1)
573 ERR("tcsetattr error '%s'\n", strerror(errno));
574 return FILE_GetNtStatus();
576 return STATUS_SUCCESS;
579 static int whack_modem(int fd, unsigned int andy, unsigned int orrie)
581 #ifdef TIOCMGET
582 unsigned int mstat, okay;
583 okay = ioctl(fd, TIOCMGET, &mstat);
584 if (okay) return okay;
585 if (andy) mstat &= andy;
586 mstat |= orrie;
587 return ioctl(fd, TIOCMSET, &mstat);
588 #else
589 return 0;
590 #endif
593 static NTSTATUS set_handflow(int fd, const SERIAL_HANDFLOW* shf)
595 struct termios port;
597 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE)) ==
598 (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
599 return STATUS_NOT_SUPPORTED;
601 if (tcgetattr(fd, &port) == -1)
603 ERR("tcgetattr error '%s'\n", strerror(errno));
604 return FILE_GetNtStatus();
607 #ifdef CRTSCTS
608 if ((shf->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
609 (shf->FlowReplace & SERIAL_RTS_HANDSHAKE))
611 port.c_cflag |= CRTSCTS;
612 TRACE("CRTSCTS\n");
614 else
615 port.c_cflag &= ~CRTSCTS;
616 #endif
617 #ifdef TIOCM_DTR
618 if (shf->ControlHandShake & SERIAL_DTR_HANDSHAKE)
620 WARN("DSR/DTR flow control not supported\n");
621 } else if (!(shf->ControlHandShake & SERIAL_DTR_CONTROL))
622 whack_modem(fd, ~TIOCM_DTR, 0);
623 else
624 whack_modem(fd, 0, TIOCM_DTR);
625 #endif
626 #ifdef TIOCM_RTS
627 if (!(shf->ControlHandShake & SERIAL_CTS_HANDSHAKE))
629 if ((shf->FlowReplace & (SERIAL_RTS_CONTROL|SERIAL_RTS_HANDSHAKE)) == 0)
630 whack_modem(fd, ~TIOCM_RTS, 0);
631 else
632 whack_modem(fd, 0, TIOCM_RTS);
634 #endif
636 if (shf->FlowReplace & SERIAL_AUTO_RECEIVE)
637 port.c_iflag |= IXOFF;
638 else
639 port.c_iflag &= ~IXOFF;
640 if (shf->FlowReplace & SERIAL_AUTO_TRANSMIT)
641 port.c_iflag |= IXON;
642 else
643 port.c_iflag &= ~IXON;
644 if (tcsetattr(fd, TCSANOW, &port) == -1)
646 ERR("tcsetattr error '%s'\n", strerror(errno));
647 return FILE_GetNtStatus();
650 return STATUS_SUCCESS;
653 static NTSTATUS set_line_control(int fd, const SERIAL_LINE_CONTROL* slc)
655 struct termios port;
656 unsigned bytesize, stopbits;
658 if (tcgetattr(fd, &port) == -1)
660 ERR("tcgetattr error '%s'\n", strerror(errno));
661 return FILE_GetNtStatus();
664 #ifdef IMAXBEL
665 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK|IMAXBEL);
666 #else
667 port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK);
668 #endif
669 port.c_iflag |= IGNBRK | INPCK;
670 port.c_oflag &= ~(OPOST);
671 port.c_cflag &= ~(HUPCL);
672 port.c_cflag |= CLOCAL | CREAD;
675 * on FreeBSD, turning off ICANON does not disable IEXTEN,
676 * so we must turn it off explicitly. No harm done on Linux.
678 port.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN);
679 port.c_lflag |= NOFLSH;
681 bytesize = slc->WordLength;
682 stopbits = slc->StopBits;
684 #ifdef CMSPAR
685 port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
686 #else
687 port.c_cflag &= ~(PARENB | PARODD);
688 #endif
690 /* make sure that reads don't block */
691 port.c_cc[VMIN] = 0;
692 port.c_cc[VTIME] = 0;
694 switch (slc->Parity)
696 case NOPARITY: port.c_iflag &= ~INPCK; break;
697 case ODDPARITY: port.c_cflag |= PARENB | PARODD; break;
698 case EVENPARITY: port.c_cflag |= PARENB; break;
699 #ifdef CMSPAR
700 /* Linux defines mark/space (stick) parity */
701 case MARKPARITY: port.c_cflag |= PARENB | PARODD | CMSPAR; break;
702 case SPACEPARITY: port.c_cflag |= PARENB | CMSPAR; break;
703 #else
704 /* try the POSIX way */
705 case MARKPARITY:
706 if (slc->StopBits == ONESTOPBIT)
708 stopbits = TWOSTOPBITS;
709 port.c_iflag &= ~INPCK;
711 else
713 FIXME("Cannot set MARK Parity\n");
714 return STATUS_NOT_SUPPORTED;
716 break;
717 case SPACEPARITY:
718 if (slc->WordLength < 8)
720 bytesize +=1;
721 port.c_iflag &= ~INPCK;
723 else
725 FIXME("Cannot set SPACE Parity\n");
726 return STATUS_NOT_SUPPORTED;
728 break;
729 #endif
730 default:
731 FIXME("Parity %d is not supported\n", slc->Parity);
732 return STATUS_NOT_SUPPORTED;
735 port.c_cflag &= ~CSIZE;
736 switch (bytesize)
738 case 5: port.c_cflag |= CS5; break;
739 case 6: port.c_cflag |= CS6; break;
740 case 7: port.c_cflag |= CS7; break;
741 case 8: port.c_cflag |= CS8; break;
742 default:
743 FIXME("ByteSize %d is not supported\n", bytesize);
744 return STATUS_NOT_SUPPORTED;
747 switch (stopbits)
749 case ONESTOPBIT: port.c_cflag &= ~CSTOPB; break;
750 case ONE5STOPBITS: /* will be selected if bytesize is 5 */
751 case TWOSTOPBITS: port.c_cflag |= CSTOPB; break;
752 default:
753 FIXME("StopBits %d is not supported\n", stopbits);
754 return STATUS_NOT_SUPPORTED;
756 /* otherwise it hangs with pending input*/
757 if (tcsetattr(fd, TCSANOW, &port) == -1)
759 ERR("tcsetattr error '%s'\n", strerror(errno));
760 return FILE_GetNtStatus();
762 return STATUS_SUCCESS;
765 static NTSTATUS set_queue_size(int fd, const SERIAL_QUEUE_SIZE* sqs)
767 FIXME("insize %d outsize %d unimplemented stub\n", sqs->InSize, sqs->OutSize);
768 return STATUS_SUCCESS;
771 static NTSTATUS set_special_chars(int fd, const SERIAL_CHARS* sc)
773 struct termios port;
775 if (tcgetattr(fd, &port) == -1)
777 ERR("tcgetattr error '%s'\n", strerror(errno));
778 return FILE_GetNtStatus();
781 port.c_cc[VEOF ] = sc->EofChar;
782 /* FIXME: sc->ErrorChar is not supported */
783 /* FIXME: sc->BreakChar is not supported */
784 /* FIXME: sc->EventChar is not supported */
785 port.c_cc[VSTART] = sc->XonChar;
786 port.c_cc[VSTOP ] = sc->XoffChar;
788 if (tcsetattr(fd, TCSANOW, &port) == -1)
790 ERR("tcsetattr error '%s'\n", strerror(errno));
791 return FILE_GetNtStatus();
793 return STATUS_SUCCESS;
797 * does not change IXOFF but simulates that IXOFF has been received:
799 static NTSTATUS set_XOff(int fd)
801 if (tcflow(fd, TCOOFF))
803 return FILE_GetNtStatus();
805 return STATUS_SUCCESS;
809 * does not change IXON but simulates that IXON has been received:
811 static NTSTATUS set_XOn(int fd)
813 if (tcflow(fd, TCOON))
815 return FILE_GetNtStatus();
817 return STATUS_SUCCESS;
820 /* serial_irq_info
821 * local structure holding the irq values we need for WaitCommEvent()
823 * Stripped down from struct serial_icounter_struct, which may not be available on some systems
824 * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
825 * no need to carry them in the internal structure
828 typedef struct serial_irq_info
830 int rx, tx, frame, overrun, parity, brk, buf_overrun, temt;
831 }serial_irq_info;
833 /***********************************************************************
834 * Data needed by the thread polling for the changing CommEvent
836 typedef struct async_commio
838 HANDLE hDevice;
839 DWORD* events;
840 IO_STATUS_BLOCK* iosb;
841 HANDLE hEvent;
842 DWORD evtmask;
843 DWORD cookie;
844 DWORD mstat;
845 DWORD pending_write;
846 serial_irq_info irq_info;
847 } async_commio;
849 /***********************************************************************
850 * Get extended interrupt count info, needed for wait_on
852 static NTSTATUS get_irq_info(int fd, serial_irq_info *irq_info)
854 int out;
856 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCGICOUNT)
857 struct serial_icounter_struct einfo;
858 if (!ioctl(fd, TIOCGICOUNT, &einfo))
860 irq_info->rx = einfo.rx;
861 irq_info->tx = einfo.tx;
862 irq_info->frame = einfo.frame;
863 irq_info->overrun = einfo.overrun;
864 irq_info->parity = einfo.parity;
865 irq_info->brk = einfo.brk;
866 irq_info->buf_overrun = einfo.buf_overrun;
868 else
870 TRACE("TIOCGICOUNT err %s\n", strerror(errno));
871 memset(irq_info,0, sizeof(serial_irq_info));
873 #else
874 memset(irq_info,0, sizeof(serial_irq_info));
875 #endif
877 irq_info->temt = 0;
878 /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
879 #ifdef TIOCSERGETLSR /* prefer to log the state TIOCSERGETLSR */
880 if (!ioctl(fd, TIOCSERGETLSR, &out))
882 irq_info->temt = (out & TIOCSER_TEMT) != 0;
883 return STATUS_SUCCESS;
886 TRACE("TIOCSERGETLSR err %s\n", strerror(errno));
887 #endif
888 #ifdef TIOCOUTQ /* otherwise we log when the out queue gets empty */
889 if (!ioctl(fd, TIOCOUTQ, &out))
891 irq_info->temt = out == 0;
892 return STATUS_SUCCESS;
894 TRACE("TIOCOUTQ err %s\n", strerror(errno));
895 return FILE_GetNtStatus();
896 #endif
897 return STATUS_SUCCESS;
901 static DWORD check_events(int fd, DWORD mask,
902 const serial_irq_info *new,
903 const serial_irq_info *old,
904 DWORD new_mstat, DWORD old_mstat, DWORD pending_write)
906 DWORD ret = 0, queue;
908 TRACE("mask 0x%08x\n", mask);
909 TRACE("old->rx 0x%08x vs. new->rx 0x%08x\n", old->rx, new->rx);
910 TRACE("old->tx 0x%08x vs. new->tx 0x%08x\n", old->tx, new->tx);
911 TRACE("old->frame 0x%08x vs. new->frame 0x%08x\n", old->frame, new->frame);
912 TRACE("old->overrun 0x%08x vs. new->overrun 0x%08x\n", old->overrun, new->overrun);
913 TRACE("old->parity 0x%08x vs. new->parity 0x%08x\n", old->parity, new->parity);
914 TRACE("old->brk 0x%08x vs. new->brk 0x%08x\n", old->brk, new->brk);
915 TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old->buf_overrun, new->buf_overrun);
916 TRACE("old->temt 0x%08x vs. new->temt 0x%08x\n", old->temt, new->temt);
918 if (old->brk != new->brk) ret |= EV_BREAK;
919 if ((old_mstat & MS_CTS_ON ) != (new_mstat & MS_CTS_ON )) ret |= EV_CTS;
920 if ((old_mstat & MS_DSR_ON ) != (new_mstat & MS_DSR_ON )) ret |= EV_DSR;
921 if ((old_mstat & MS_RING_ON) != (new_mstat & MS_RING_ON)) ret |= EV_RING;
922 if ((old_mstat & MS_RLSD_ON) != (new_mstat & MS_RLSD_ON)) ret |= EV_RLSD;
923 if (old->frame != new->frame || old->overrun != new->overrun || old->parity != new->parity) ret |= EV_ERR;
924 if (mask & EV_RXCHAR)
926 queue = 0;
927 #ifdef TIOCINQ
928 if (ioctl(fd, TIOCINQ, &queue))
929 WARN("TIOCINQ returned error\n");
930 #endif
931 if (queue)
932 ret |= EV_RXCHAR;
934 if (mask & EV_TXEMPTY)
936 if ((!old->temt || pending_write) && new->temt)
937 ret |= EV_TXEMPTY;
939 return ret & mask;
942 /***********************************************************************
943 * wait_for_event (INTERNAL)
945 * We need to poll for what is interesting
946 * TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
949 static DWORD CALLBACK wait_for_event(LPVOID arg)
951 async_commio *commio = arg;
952 int fd, needs_close;
954 if (!server_get_unix_fd( commio->hDevice, FILE_READ_DATA | FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL ))
956 serial_irq_info new_irq_info;
957 DWORD new_mstat, dummy, cookie;
958 LARGE_INTEGER time;
960 TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n",
961 commio->hDevice, fd, commio->evtmask, commio->events, commio->hEvent, &commio->irq_info);
963 time.QuadPart = (ULONGLONG)10000;
964 time.QuadPart = -time.QuadPart;
965 for (;;)
968 * TIOCMIWAIT is not adequate
970 * FIXME:
971 * We don't handle the EV_RXFLAG (the eventchar)
973 NtDelayExecution(FALSE, &time);
974 get_irq_info(fd, &new_irq_info);
975 if (get_modem_status(fd, &new_mstat))
977 TRACE("get_modem_status failed\n");
978 *commio->events = 0;
979 break;
981 *commio->events = check_events(fd, commio->evtmask,
982 &new_irq_info, &commio->irq_info,
983 new_mstat, commio->mstat, commio->pending_write);
984 if (*commio->events) break;
985 get_wait_mask(commio->hDevice, &dummy, &cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, FALSE);
986 if (commio->cookie != cookie)
988 *commio->events = 0;
989 break;
992 if (needs_close) close( fd );
994 if (commio->iosb)
996 if (*commio->events)
998 commio->iosb->u.Status = STATUS_SUCCESS;
999 commio->iosb->Information = sizeof(DWORD);
1001 else
1002 commio->iosb->u.Status = STATUS_CANCELLED;
1004 stop_waiting(commio->hDevice);
1005 if (commio->hEvent) NtSetEvent(commio->hEvent, NULL);
1006 RtlFreeHeap(GetProcessHeap(), 0, commio);
1007 return 0;
1010 static NTSTATUS wait_on(HANDLE hDevice, int fd, HANDLE hEvent, PIO_STATUS_BLOCK piosb, DWORD* events)
1012 async_commio* commio;
1013 NTSTATUS status;
1015 if ((status = NtResetEvent(hEvent, NULL)))
1016 return status;
1018 commio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio));
1019 if (!commio) return STATUS_NO_MEMORY;
1021 commio->hDevice = hDevice;
1022 commio->events = events;
1023 commio->iosb = piosb;
1024 commio->hEvent = hEvent;
1025 commio->pending_write = 0;
1026 status = get_wait_mask(commio->hDevice, &commio->evtmask, &commio->cookie, (commio->evtmask & EV_TXEMPTY) ? &commio->pending_write : NULL, TRUE);
1027 if (status)
1029 RtlFreeHeap(GetProcessHeap(), 0, commio);
1030 return status;
1033 /* We may never return, if some capabilities miss
1034 * Return error in that case
1036 #if !defined(TIOCINQ)
1037 if (commio->evtmask & EV_RXCHAR)
1038 goto error_caps;
1039 #endif
1040 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
1041 if (commio->evtmask & EV_TXEMPTY)
1042 goto error_caps;
1043 #endif
1044 #if !defined(TIOCMGET)
1045 if (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD))
1046 goto error_caps;
1047 #endif
1048 #if !defined(TIOCM_CTS)
1049 if (commio->evtmask & EV_CTS)
1050 goto error_caps;
1051 #endif
1052 #if !defined(TIOCM_DSR)
1053 if (commio->evtmask & EV_DSR)
1054 goto error_caps;
1055 #endif
1056 #if !defined(TIOCM_RNG)
1057 if (commio->evtmask & EV_RING)
1058 goto error_caps;
1059 #endif
1060 #if !defined(TIOCM_CAR)
1061 if (commio->evtmask & EV_RLSD)
1062 goto error_caps;
1063 #endif
1064 if (commio->evtmask & EV_RXFLAG)
1065 FIXME("EV_RXFLAG not handled\n");
1067 if ((status = get_irq_info(fd, &commio->irq_info)) &&
1068 (commio->evtmask & (EV_BREAK | EV_ERR)))
1069 goto out_now;
1071 if ((status = get_modem_status(fd, &commio->mstat)) &&
1072 (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD)))
1073 goto out_now;
1075 /* We might have received something or the TX buffer is delivered */
1076 *events = check_events(fd, commio->evtmask,
1077 &commio->irq_info, &commio->irq_info,
1078 commio->mstat, commio->mstat, commio->pending_write);
1079 if (*events)
1081 status = STATUS_SUCCESS;
1082 goto out_now;
1085 /* create the worker for the task */
1086 status = RtlQueueWorkItem(wait_for_event, commio, 0 /* FIXME */);
1087 if (status != STATUS_SUCCESS) goto out_now;
1088 return STATUS_PENDING;
1090 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1091 error_caps:
1092 FIXME("Returning error because of missing capabilities\n");
1093 status = STATUS_INVALID_PARAMETER;
1094 #endif
1095 out_now:
1096 stop_waiting(commio->hDevice);
1097 RtlFreeHeap(GetProcessHeap(), 0, commio);
1098 return status;
1101 static NTSTATUS xmit_immediate(HANDLE hDevice, int fd, const char* ptr)
1103 /* FIXME: not perfect as it should bypass the in-queue */
1104 WARN("(%p,'%c') not perfect!\n", hDevice, *ptr);
1105 if (write(fd, ptr, 1) != 1)
1106 return FILE_GetNtStatus();
1107 return STATUS_SUCCESS;
1110 /******************************************************************
1111 * COMM_DeviceIoControl
1115 static inline NTSTATUS io_control(HANDLE hDevice,
1116 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1117 PVOID UserApcContext,
1118 PIO_STATUS_BLOCK piosb,
1119 ULONG dwIoControlCode,
1120 LPVOID lpInBuffer, DWORD nInBufferSize,
1121 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1123 DWORD sz = 0, access = FILE_READ_DATA;
1124 NTSTATUS status = STATUS_SUCCESS;
1125 int fd = -1, needs_close = 0;
1126 enum server_fd_type type;
1128 TRACE("%p %s %p %d %p %d %p\n",
1129 hDevice, iocode2str(dwIoControlCode), lpInBuffer, nInBufferSize,
1130 lpOutBuffer, nOutBufferSize, piosb);
1132 switch (dwIoControlCode)
1134 case IOCTL_SERIAL_GET_TIMEOUTS:
1135 case IOCTL_SERIAL_SET_TIMEOUTS:
1136 case IOCTL_SERIAL_GET_WAIT_MASK:
1137 case IOCTL_SERIAL_SET_WAIT_MASK:
1138 /* these are handled on the server side */
1139 return STATUS_NOT_SUPPORTED;
1142 piosb->Information = 0;
1144 if ((status = server_get_unix_fd( hDevice, access, &fd, &needs_close, &type, NULL )))
1145 goto error;
1146 if (type != FD_TYPE_SERIAL)
1148 if (needs_close) close( fd );
1149 status = STATUS_OBJECT_TYPE_MISMATCH;
1150 goto error;
1153 switch (dwIoControlCode)
1155 case IOCTL_SERIAL_CLR_DTR:
1156 #ifdef TIOCM_DTR
1157 if (whack_modem(fd, ~TIOCM_DTR, 0) == -1) status = FILE_GetNtStatus();
1158 #else
1159 status = STATUS_NOT_SUPPORTED;
1160 #endif
1161 break;
1162 case IOCTL_SERIAL_CLR_RTS:
1163 #ifdef TIOCM_RTS
1164 if (whack_modem(fd, ~TIOCM_RTS, 0) == -1) status = FILE_GetNtStatus();
1165 #else
1166 status = STATUS_NOT_SUPPORTED;
1167 #endif
1168 break;
1169 case IOCTL_SERIAL_GET_BAUD_RATE:
1170 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_BAUD_RATE))
1172 if (!(status = get_baud_rate(fd, lpOutBuffer)))
1173 sz = sizeof(SERIAL_BAUD_RATE);
1175 else
1176 status = STATUS_INVALID_PARAMETER;
1177 break;
1178 case IOCTL_SERIAL_GET_CHARS:
1179 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_CHARS))
1181 if (!(status = get_special_chars(fd, lpOutBuffer)))
1182 sz = sizeof(SERIAL_CHARS);
1184 else
1185 status = STATUS_INVALID_PARAMETER;
1186 break;
1187 case IOCTL_SERIAL_GET_COMMSTATUS:
1188 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_STATUS))
1190 if (!(status = get_status(fd, lpOutBuffer)))
1191 sz = sizeof(SERIAL_STATUS);
1193 else status = STATUS_INVALID_PARAMETER;
1194 break;
1195 case IOCTL_SERIAL_GET_HANDFLOW:
1196 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_HANDFLOW))
1198 if (!(status = get_hand_flow(fd, lpOutBuffer)))
1199 sz = sizeof(SERIAL_HANDFLOW);
1201 else
1202 status = STATUS_INVALID_PARAMETER;
1203 break;
1204 case IOCTL_SERIAL_GET_LINE_CONTROL:
1205 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_LINE_CONTROL))
1207 if (!(status = get_line_control(fd, lpOutBuffer)))
1208 sz = sizeof(SERIAL_LINE_CONTROL);
1210 else
1211 status = STATUS_INVALID_PARAMETER;
1212 break;
1213 case IOCTL_SERIAL_GET_MODEMSTATUS:
1214 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1216 if (!(status = get_modem_status(fd, lpOutBuffer)))
1217 sz = sizeof(DWORD);
1219 else status = STATUS_INVALID_PARAMETER;
1220 break;
1221 case IOCTL_SERIAL_GET_PROPERTIES:
1222 if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_COMMPROP))
1224 if (!(status = get_properties(fd, lpOutBuffer)))
1225 sz = sizeof(SERIAL_COMMPROP);
1227 else status = STATUS_INVALID_PARAMETER;
1228 break;
1229 case IOCTL_SERIAL_IMMEDIATE_CHAR:
1230 if (lpInBuffer && nInBufferSize == sizeof(CHAR))
1231 status = xmit_immediate(hDevice, fd, lpInBuffer);
1232 else
1233 status = STATUS_INVALID_PARAMETER;
1234 break;
1235 case IOCTL_SERIAL_PURGE:
1236 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1237 status = purge(fd, *(DWORD*)lpInBuffer);
1238 else
1239 status = STATUS_INVALID_PARAMETER;
1240 break;
1241 case IOCTL_SERIAL_RESET_DEVICE:
1242 FIXME("Unsupported\n");
1243 break;
1244 case IOCTL_SERIAL_SET_BAUD_RATE:
1245 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_BAUD_RATE))
1246 status = set_baud_rate(fd, lpInBuffer);
1247 else
1248 status = STATUS_INVALID_PARAMETER;
1249 break;
1250 case IOCTL_SERIAL_SET_BREAK_OFF:
1251 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1252 if (ioctl(fd, TIOCCBRK, 0) == -1)
1254 TRACE("ioctl failed\n");
1255 status = FILE_GetNtStatus();
1257 #else
1258 FIXME("ioctl not available\n");
1259 status = STATUS_NOT_SUPPORTED;
1260 #endif
1261 break;
1262 case IOCTL_SERIAL_SET_BREAK_ON:
1263 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1264 if (ioctl(fd, TIOCSBRK, 0) == -1)
1266 TRACE("ioctl failed\n");
1267 status = FILE_GetNtStatus();
1269 #else
1270 FIXME("ioctl not available\n");
1271 status = STATUS_NOT_SUPPORTED;
1272 #endif
1273 break;
1274 case IOCTL_SERIAL_SET_CHARS:
1275 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_CHARS))
1276 status = set_special_chars(fd, lpInBuffer);
1277 else
1278 status = STATUS_INVALID_PARAMETER;
1279 break;
1280 case IOCTL_SERIAL_SET_DTR:
1281 #ifdef TIOCM_DTR
1282 if (whack_modem(fd, 0, TIOCM_DTR) == -1) status = FILE_GetNtStatus();
1283 #else
1284 status = STATUS_NOT_SUPPORTED;
1285 #endif
1286 break;
1287 case IOCTL_SERIAL_SET_HANDFLOW:
1288 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_HANDFLOW))
1289 status = set_handflow(fd, lpInBuffer);
1290 else
1291 status = STATUS_INVALID_PARAMETER;
1292 break;
1293 case IOCTL_SERIAL_SET_LINE_CONTROL:
1294 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_LINE_CONTROL))
1295 status = set_line_control(fd, lpInBuffer);
1296 else
1297 status = STATUS_INVALID_PARAMETER;
1298 break;
1299 case IOCTL_SERIAL_SET_QUEUE_SIZE:
1300 if (lpInBuffer && nInBufferSize == sizeof(SERIAL_QUEUE_SIZE))
1301 status = set_queue_size(fd, lpInBuffer);
1302 else
1303 status = STATUS_INVALID_PARAMETER;
1304 break;
1305 case IOCTL_SERIAL_SET_RTS:
1306 #ifdef TIOCM_RTS
1307 if (whack_modem(fd, 0, TIOCM_RTS) == -1) status = FILE_GetNtStatus();
1308 #else
1309 status = STATUS_NOT_SUPPORTED;
1310 #endif
1311 break;
1312 case IOCTL_SERIAL_SET_XOFF:
1313 status = set_XOff(fd);
1314 break;
1315 case IOCTL_SERIAL_SET_XON:
1316 status = set_XOn(fd);
1317 break;
1318 case IOCTL_SERIAL_WAIT_ON_MASK:
1319 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1321 if (!(status = wait_on(hDevice, fd, hEvent, piosb, lpOutBuffer)))
1322 sz = sizeof(DWORD);
1324 else
1325 status = STATUS_INVALID_PARAMETER;
1326 break;
1327 default:
1328 FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n",
1329 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
1330 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
1331 sz = 0;
1332 status = STATUS_INVALID_PARAMETER;
1333 break;
1335 if (needs_close) close( fd );
1336 error:
1337 piosb->u.Status = status;
1338 piosb->Information = sz;
1339 if (hEvent && status != STATUS_PENDING) NtSetEvent(hEvent, NULL);
1340 return status;
1343 NTSTATUS COMM_DeviceIoControl(HANDLE hDevice,
1344 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1345 PVOID UserApcContext,
1346 PIO_STATUS_BLOCK piosb,
1347 ULONG dwIoControlCode,
1348 LPVOID lpInBuffer, DWORD nInBufferSize,
1349 LPVOID lpOutBuffer, DWORD nOutBufferSize)
1351 NTSTATUS status;
1353 if (dwIoControlCode == IOCTL_SERIAL_WAIT_ON_MASK)
1355 HANDLE hev = hEvent;
1357 /* this is an ioctl we implement in a non blocking way if hEvent is not
1358 * null
1359 * so we have to explicitly wait if no hEvent is provided
1361 if (!hev)
1363 OBJECT_ATTRIBUTES attr;
1365 attr.Length = sizeof(attr);
1366 attr.RootDirectory = 0;
1367 attr.ObjectName = NULL;
1368 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1369 attr.SecurityDescriptor = NULL;
1370 attr.SecurityQualityOfService = NULL;
1371 status = NtCreateEvent(&hev, EVENT_ALL_ACCESS, &attr, SynchronizationEvent, FALSE);
1373 if (status) return status;
1375 status = io_control(hDevice, hev, UserApcRoutine, UserApcContext,
1376 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1377 lpOutBuffer, nOutBufferSize);
1378 if (hev != hEvent)
1380 if (status == STATUS_PENDING)
1382 NtWaitForSingleObject(hev, FALSE, NULL);
1383 status = STATUS_SUCCESS;
1385 NtClose(hev);
1388 else status = io_control(hDevice, hEvent, UserApcRoutine, UserApcContext,
1389 piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1390 lpOutBuffer, nOutBufferSize);
1391 return status;
1394 NTSTATUS COMM_FlushBuffersFile( int fd )
1396 #ifdef HAVE_TCDRAIN
1397 while (tcdrain( fd ) == -1)
1399 if (errno != EINTR) return FILE_GetNtStatus();
1401 return STATUS_SUCCESS;
1402 #elif defined(TIOCDRAIN)
1403 while (ioctl( fd, TIOCDRAIN ) == -1)
1405 if (errno != EINTR) return FILE_GetNtStatus();
1407 return STATUS_SUCCESS;
1408 #elif defined(TCSBRK)
1409 while (ioctl( fd, TCSBRK, 1 ) == -1)
1411 if (errno != EINTR) return FILE_GetNtStatus();
1413 return STATUS_SUCCESS;
1414 #else
1415 ERR( "not supported\n" );
1416 return STATUS_NOT_IMPLEMENTED;
1417 #endif