kernel32: Make CopyFile() call CopyFileEx() instead of the other way around.
[wine] / dlls / ntdll / serial.c
1 /* Main file for COMM support
2  *
3  * DEC 93 Erik Bos <erik@xs4all.nl>
4  * Copyright 1996 Marcus Meissner
5  * Copyright 2005,2006 Eric Pouech
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
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
59
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"
72
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
79
80 #if !defined(TIOCINQ) && defined(FIONREAD)
81 #define TIOCINQ FIONREAD
82 #endif
83
84 WINE_DEFAULT_DEBUG_CHANNEL(comm);
85
86 static const char* iocode2str(DWORD ioc)
87 {
88     switch (ioc)
89     {
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; }
130     }
131 }
132
133 static NTSTATUS get_baud_rate(int fd, SERIAL_BAUD_RATE* sbr)
134 {
135     struct termios port;
136     int speed;
137     
138     if (tcgetattr(fd, &port) == -1)
139     {
140         ERR("tcgetattr error '%s'\n", strerror(errno));
141         return FILE_GetNtStatus();
142     }
143     speed = cfgetospeed(&port);
144     switch (speed)
145     {
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;
177     }
178     return STATUS_SUCCESS;
179 }
180
181 static NTSTATUS get_hand_flow(int fd, SERIAL_HANDFLOW* shf)
182 {
183     int stat = 0;
184     struct termios port;
185
186     if (tcgetattr(fd, &port) == -1)
187     {
188         ERR("tcgetattr error '%s'\n", strerror(errno));
189         return FILE_GetNtStatus();
190     }
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)
196     {
197         WARN("ioctl error '%s'\n", strerror(errno));
198         shf->ControlHandShake |= SERIAL_DTR_CONTROL;
199         shf->FlowReplace |= SERIAL_RTS_CONTROL;
200     }
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)
212     {
213         shf->FlowReplace |= SERIAL_RTS_CONTROL;
214         shf->ControlHandShake |= SERIAL_CTS_HANDSHAKE;
215     }
216     else
217 #endif
218     {
219 #ifdef TIOCM_RTS
220         if (stat & TIOCM_RTS)
221 #endif
222             shf->FlowReplace |= SERIAL_RTS_CONTROL;
223     }
224     if (port.c_iflag & IXOFF)
225         shf->FlowReplace |= SERIAL_AUTO_RECEIVE;
226     if (port.c_iflag & IXON)
227         shf->FlowReplace |= SERIAL_AUTO_TRANSMIT;
228
229     shf->XonLimit = 10;
230     shf->XoffLimit = 10;
231     return STATUS_SUCCESS;
232 }
233
234 static NTSTATUS get_line_control(int fd, SERIAL_LINE_CONTROL* slc)
235 {
236     struct termios port;
237     
238     if (tcgetattr(fd, &port) == -1)
239     {
240         ERR("tcgetattr error '%s'\n", strerror(errno));
241         return FILE_GetNtStatus();
242     }
243     
244 #ifdef CMSPAR
245     switch (port.c_cflag & (PARENB | PARODD | CMSPAR))
246 #else
247     switch (port.c_cflag & (PARENB | PARODD))
248 #endif
249     {
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
257     }
258     switch (port.c_cflag & CSIZE)
259     {
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));
265     }
266
267     if (port.c_cflag & CSTOPB)
268     {
269         if (slc->WordLength == 5)
270             slc->StopBits = ONE5STOPBITS;
271         else
272             slc->StopBits = TWOSTOPBITS;
273     }
274     else
275         slc->StopBits = ONESTOPBIT;
276
277     return STATUS_SUCCESS;
278 }
279
280 static NTSTATUS get_modem_status(int fd, DWORD* lpModemStat)
281 {
282     NTSTATUS    status = STATUS_NOT_SUPPORTED;
283     int         mstat;
284
285     *lpModemStat = 0;
286 #ifdef TIOCMGET
287     if (!ioctl(fd, TIOCMGET, &mstat))
288     {
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;
308     }
309     WARN("ioctl failed\n");
310     status = FILE_GetNtStatus();
311 #endif
312     return status;
313 }
314
315 static NTSTATUS get_special_chars(int fd, SERIAL_CHARS* sc)
316 {
317     struct termios port;
318     
319     if (tcgetattr(fd, &port) == -1)
320     {
321         ERR("tcgetattr error '%s'\n", strerror(errno));
322         return FILE_GetNtStatus();
323     }
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];
330
331     return STATUS_SUCCESS;
332 }
333
334 static NTSTATUS get_status(int fd, SERIAL_STATUS* ss)
335 {
336     NTSTATUS    status = STATUS_SUCCESS;
337
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)
344     {
345         WARN("ioctl returned error\n");
346         status = FILE_GetNtStatus();
347     }
348 #else
349     ss->AmountInOutQueue = 0; /* FIXME: find a different way to find out */
350 #endif
351
352 #ifdef TIOCINQ
353     if (ioctl(fd, TIOCINQ, &ss->AmountInInQueue))
354     {
355         WARN("ioctl returned error\n");
356         status = FILE_GetNtStatus();
357     }
358 #else
359     ss->AmountInInQueue = 0; /* FIXME: find a different way to find out */
360 #endif
361     return status;
362 }
363
364 static NTSTATUS get_timeouts(HANDLE handle, SERIAL_TIMEOUTS* st)
365 {
366     NTSTATUS    status;
367     SERVER_START_REQ( get_serial_info )
368     {
369         req->handle = wine_server_obj_handle( handle );
370         if (!(status = wine_server_call( req )))
371         {
372             st->ReadIntervalTimeout         = reply->readinterval;
373             st->ReadTotalTimeoutMultiplier  = reply->readmult;
374             st->ReadTotalTimeoutConstant    = reply->readconst;
375             st->WriteTotalTimeoutMultiplier = reply->writemult;
376             st->WriteTotalTimeoutConstant   = reply->writeconst;
377         }
378     }
379     SERVER_END_REQ;
380     return status;
381 }
382
383 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD* mask)
384 {
385     NTSTATUS    status;
386
387     SERVER_START_REQ( get_serial_info )
388     {
389         req->handle = wine_server_obj_handle( hDevice );
390         if (!(status = wine_server_call( req )))
391             *mask = reply->eventmask;
392     }
393     SERVER_END_REQ;
394     return status;
395 }
396
397 static NTSTATUS purge(int fd, DWORD flags)
398 {
399     /*
400     ** not exactly sure how these are different
401     ** Perhaps if we had our own internal queues, one flushes them
402     ** and the other flushes the kernel's buffers.
403     */
404     if (flags & PURGE_TXABORT) tcflush(fd, TCOFLUSH);
405     if (flags & PURGE_RXABORT) tcflush(fd, TCIFLUSH);
406     if (flags & PURGE_TXCLEAR) tcflush(fd, TCOFLUSH);
407     if (flags & PURGE_RXCLEAR) tcflush(fd, TCIFLUSH);
408     return STATUS_SUCCESS;
409 }
410
411 static NTSTATUS set_baud_rate(int fd, const SERIAL_BAUD_RATE* sbr)
412 {
413     struct termios port;
414
415     if (tcgetattr(fd, &port) == -1)
416     {
417         ERR("tcgetattr error '%s'\n", strerror(errno));
418         return FILE_GetNtStatus();
419     }
420
421     switch (sbr->BaudRate)
422     {
423     case 0:         cfsetospeed( &port, B0 ); break;
424     case 50:        cfsetospeed( &port, B50 ); break;
425     case 75:        cfsetospeed( &port, B75 ); break;
426     case 110:
427     case CBR_110:   cfsetospeed( &port, B110 ); break;
428     case 134:       cfsetospeed( &port, B134 ); break;
429     case 150:       cfsetospeed( &port, B150 ); break;
430     case 200:       cfsetospeed( &port, B200 ); break;
431     case 300:
432     case CBR_300:   cfsetospeed( &port, B300 ); break;
433     case 600:
434     case CBR_600:   cfsetospeed( &port, B600 ); break;
435     case 1200:
436     case CBR_1200:  cfsetospeed( &port, B1200 ); break;
437     case 1800:      cfsetospeed( &port, B1800 ); break;
438     case 2400:
439     case CBR_2400:  cfsetospeed( &port, B2400 ); break;
440     case 4800:
441     case CBR_4800:  cfsetospeed( &port, B4800 ); break;
442     case 9600:
443     case CBR_9600:  cfsetospeed( &port, B9600 ); break;
444     case 19200:
445     case CBR_19200: cfsetospeed( &port, B19200 ); break;
446     case 38400:
447     case CBR_38400: cfsetospeed( &port, B38400 ); break;
448 #ifdef B57600
449     case 57600: cfsetospeed( &port, B57600 ); break;
450 #endif
451 #ifdef B115200
452     case 115200: cfsetospeed( &port, B115200 ); break;
453 #endif
454 #ifdef B230400
455     case 230400: cfsetospeed( &port, B230400 ); break;
456 #endif
457 #ifdef B460800
458     case 460800: cfsetospeed( &port, B460800 ); break;
459 #endif
460     default:
461 #if defined (HAVE_LINUX_SERIAL_H) && defined (TIOCSSERIAL)
462         {
463             struct serial_struct nuts;
464             int arby;
465         
466             ioctl(fd, TIOCGSERIAL, &nuts);
467             nuts.custom_divisor = nuts.baud_base / sbr->BaudRate;
468             if (!(nuts.custom_divisor)) nuts.custom_divisor = 1;
469             arby = nuts.baud_base / nuts.custom_divisor;
470             nuts.flags &= ~ASYNC_SPD_MASK;
471             nuts.flags |= ASYNC_SPD_CUST;
472             WARN("You (or a program acting at your behest) have specified\n"
473                  "a non-standard baud rate %d.  Wine will set the rate to %d,\n"
474                  "which is as close as we can get by our present understanding of your\n"
475                  "hardware. I hope you know what you are doing.  Any disruption Wine\n"
476                  "has caused to your linux system can be undone with setserial\n"
477                  "(see man setserial). If you have incapacitated a Hayes type modem,\n"
478                  "reset it and it will probably recover.\n", sbr->BaudRate, arby);
479             ioctl(fd, TIOCSSERIAL, &nuts);
480             cfsetospeed( &port, B38400 );
481         }
482         break;
483 #else     /* Don't have linux/serial.h or lack TIOCSSERIAL */
484         ERR("baudrate %d\n", sbr->BaudRate);
485         return STATUS_NOT_SUPPORTED;
486 #endif    /* Don't have linux/serial.h or lack TIOCSSERIAL */
487     }
488     cfsetispeed( &port, cfgetospeed(&port) );
489     if (tcsetattr(fd, TCSANOW, &port) == -1)
490     {
491         ERR("tcsetattr error '%s'\n", strerror(errno));
492         return FILE_GetNtStatus();
493     }
494     return STATUS_SUCCESS;
495 }
496
497 static int whack_modem(int fd, unsigned int andy, unsigned int orrie)
498 {
499 #ifdef TIOCMGET
500     unsigned int mstat, okay;
501     okay = ioctl(fd, TIOCMGET, &mstat);
502     if (okay) return okay;
503     if (andy) mstat &= andy;
504     mstat |= orrie;
505     return ioctl(fd, TIOCMSET, &mstat);
506 #else
507     return 0;
508 #endif
509 }
510
511 static NTSTATUS set_handflow(int fd, const SERIAL_HANDFLOW* shf)
512 {
513     struct termios port;
514
515     if ((shf->FlowReplace & (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE)) == 
516         (SERIAL_RTS_CONTROL | SERIAL_RTS_HANDSHAKE))
517         return STATUS_NOT_SUPPORTED;
518
519     if (tcgetattr(fd, &port) == -1)
520     {
521         ERR("tcgetattr error '%s'\n", strerror(errno));
522         return FILE_GetNtStatus();
523     }
524     
525 #ifdef CRTSCTS
526     if ((shf->ControlHandShake & SERIAL_CTS_HANDSHAKE) ||
527         (shf->FlowReplace & SERIAL_RTS_HANDSHAKE))
528     {
529         port.c_cflag |= CRTSCTS;
530         TRACE("CRTSCTS\n");
531     }
532     else
533         port.c_cflag &= ~CRTSCTS;
534 #endif
535 #ifdef TIOCM_DTR
536     if (shf->ControlHandShake & SERIAL_DTR_HANDSHAKE)
537     {
538         WARN("DSR/DTR flow control not supported\n");
539     } else if (!(shf->ControlHandShake & SERIAL_DTR_CONTROL))
540         whack_modem(fd, ~TIOCM_DTR, 0);
541     else
542         whack_modem(fd, 0, TIOCM_DTR);
543 #endif
544 #ifdef TIOCM_RTS
545     if (!(shf->ControlHandShake & SERIAL_CTS_HANDSHAKE))
546     {
547         if ((shf->FlowReplace & (SERIAL_RTS_CONTROL|SERIAL_RTS_HANDSHAKE)) == 0)
548             whack_modem(fd, ~TIOCM_RTS, 0);
549         else    
550             whack_modem(fd, 0, TIOCM_RTS);
551     }
552 #endif
553
554     if (shf->FlowReplace & SERIAL_AUTO_RECEIVE)
555         port.c_iflag |= IXOFF;
556     else
557         port.c_iflag &= ~IXOFF;
558     if (shf->FlowReplace & SERIAL_AUTO_TRANSMIT)
559         port.c_iflag |= IXON;
560     else
561         port.c_iflag &= ~IXON;
562     if (tcsetattr(fd, TCSANOW, &port) == -1)
563     {
564         ERR("tcsetattr error '%s'\n", strerror(errno));
565         return FILE_GetNtStatus();
566     }
567
568     return STATUS_SUCCESS;
569 }
570
571 static NTSTATUS set_line_control(int fd, const SERIAL_LINE_CONTROL* slc)
572 {
573     struct termios port;
574     unsigned bytesize, stopbits;
575     
576     if (tcgetattr(fd, &port) == -1)
577     {
578         ERR("tcgetattr error '%s'\n", strerror(errno));
579         return FILE_GetNtStatus();
580     }
581     
582 #ifdef IMAXBEL
583     port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK|IMAXBEL);
584 #else
585     port.c_iflag &= ~(ISTRIP|BRKINT|IGNCR|ICRNL|INLCR|PARMRK);
586 #endif
587     port.c_iflag |= IGNBRK | INPCK;
588     
589     port.c_oflag &= ~(OPOST);
590     
591     port.c_cflag &= ~(HUPCL);
592     port.c_cflag |= CLOCAL | CREAD;
593     
594     /*
595      * on FreeBSD, turning off ICANON does not disable IEXTEN,
596      * so we must turn it off explicitly. No harm done on Linux.
597      */
598     port.c_lflag &= ~(ICANON|ECHO|ISIG|IEXTEN);
599     port.c_lflag |= NOFLSH;
600     
601     bytesize = slc->WordLength;
602     stopbits = slc->StopBits;
603     
604 #ifdef CMSPAR
605     port.c_cflag &= ~(PARENB | PARODD | CMSPAR);
606 #else
607     port.c_cflag &= ~(PARENB | PARODD);
608 #endif
609
610     /* make sure that reads don't block */
611     port.c_cc[VMIN] = 0;
612     port.c_cc[VTIME] = 0;
613
614     switch (slc->Parity)
615     {
616     case NOPARITY:      port.c_iflag &= ~INPCK;                         break;
617     case ODDPARITY:     port.c_cflag |= PARENB | PARODD;                break;
618     case EVENPARITY:    port.c_cflag |= PARENB;                         break;
619 #ifdef CMSPAR
620         /* Linux defines mark/space (stick) parity */
621     case MARKPARITY:    port.c_cflag |= PARENB | CMSPAR;                break;
622     case SPACEPARITY:   port.c_cflag |= PARENB | PARODD |  CMSPAR;      break;
623 #else
624         /* try the POSIX way */
625     case MARKPARITY:
626         if (slc->StopBits == ONESTOPBIT)
627         {
628             stopbits = TWOSTOPBITS;
629             port.c_iflag &= ~INPCK;
630         }
631         else
632         {
633             ERR("Cannot set MARK Parity\n");
634             return STATUS_NOT_SUPPORTED;
635         }
636         break;
637     case SPACEPARITY:
638         if (slc->WordLength < 8)
639         {
640             bytesize +=1;
641             port.c_iflag &= ~INPCK;
642         }
643         else
644         {
645             ERR("Cannot set SPACE Parity\n");
646             return STATUS_NOT_SUPPORTED;
647         }
648         break;
649 #endif
650     default:
651         ERR("Parity\n");
652         return STATUS_NOT_SUPPORTED;
653     }
654     
655     port.c_cflag &= ~CSIZE;
656     switch (bytesize)
657     {
658     case 5:     port.c_cflag |= CS5;    break;
659     case 6:     port.c_cflag |= CS6;    break;
660     case 7:     port.c_cflag |= CS7;    break;
661     case 8:     port.c_cflag |= CS8;    break;
662     default:
663         ERR("ByteSize\n");
664         return STATUS_NOT_SUPPORTED;
665     }
666     
667     switch (stopbits)
668     {
669     case ONESTOPBIT:    port.c_cflag &= ~CSTOPB;        break;
670     case ONE5STOPBITS: /* will be selected if bytesize is 5 */
671     case TWOSTOPBITS:   port.c_cflag |= CSTOPB;         break;
672     default:
673         ERR("StopBits\n");
674         return STATUS_NOT_SUPPORTED;
675     }
676     /* otherwise it hangs with pending input*/
677     if (tcsetattr(fd, TCSANOW, &port) == -1)
678     {
679         ERR("tcsetattr error '%s'\n", strerror(errno));
680         return FILE_GetNtStatus();
681     }
682     return STATUS_SUCCESS;
683 }
684
685 static NTSTATUS set_queue_size(int fd, const SERIAL_QUEUE_SIZE* sqs)
686 {
687     FIXME("insize %d outsize %d unimplemented stub\n", sqs->InSize, sqs->OutSize);
688     return STATUS_SUCCESS;
689 }
690
691 static NTSTATUS set_special_chars(int fd, const SERIAL_CHARS* sc)
692 {
693     struct termios port;
694     
695     if (tcgetattr(fd, &port) == -1)
696     {
697         ERR("tcgetattr error '%s'\n", strerror(errno));
698         return FILE_GetNtStatus();
699     }
700     
701     port.c_cc[VEOF  ] = sc->EofChar;
702     /* FIXME: sc->ErrorChar is not supported */
703     /* FIXME: sc->BreakChar is not supported */
704     /* FIXME: sc->EventChar is not supported */
705     port.c_cc[VSTART] = sc->XonChar;
706     port.c_cc[VSTOP ] = sc->XoffChar;
707     
708     if (tcsetattr(fd, TCSANOW, &port) == -1)
709     {
710         ERR("tcsetattr error '%s'\n", strerror(errno));
711         return FILE_GetNtStatus();
712     }
713     return STATUS_SUCCESS;
714 }
715
716 static NTSTATUS set_timeouts(HANDLE handle, const SERIAL_TIMEOUTS* st)
717 {
718     NTSTATUS            status;
719
720     SERVER_START_REQ( set_serial_info )
721     {
722         req->handle       = wine_server_obj_handle( handle );
723         req->flags        = SERIALINFO_SET_TIMEOUTS;
724         req->readinterval = st->ReadIntervalTimeout ;
725         req->readmult     = st->ReadTotalTimeoutMultiplier ;
726         req->readconst    = st->ReadTotalTimeoutConstant ;
727         req->writemult    = st->WriteTotalTimeoutMultiplier ;
728         req->writeconst   = st->WriteTotalTimeoutConstant ;
729         status = wine_server_call( req );
730     }
731     SERVER_END_REQ;
732     return status;
733 }
734
735 static NTSTATUS set_wait_mask(HANDLE hDevice, DWORD mask)
736 {
737     NTSTATUS status;
738
739     SERVER_START_REQ( set_serial_info )
740     {
741         req->handle    = wine_server_obj_handle( hDevice );
742         req->flags     = SERIALINFO_SET_MASK;
743         req->eventmask = mask;
744         status = wine_server_call( req );
745     }
746     SERVER_END_REQ;
747     return status;
748 }
749
750 /*
751  * does not change IXOFF but simulates that IXOFF has been received:
752  */
753 static NTSTATUS set_XOff(int fd)
754 {
755     if (tcflow(fd, TCOOFF))
756     {
757         return FILE_GetNtStatus();
758     }
759     return STATUS_SUCCESS;
760 }
761
762 /*
763  * does not change IXON but simulates that IXON has been received:
764  */
765 static NTSTATUS set_XOn(int fd)
766 {
767     if (tcflow(fd, TCOON))
768     {
769         return FILE_GetNtStatus();
770     }
771     return STATUS_SUCCESS;
772 }
773
774 /*             serial_irq_info
775  * local structure holding the irq values we need for WaitCommEvent()
776  *
777  * Stripped down from struct serial_icounter_struct, which may not be available on some systems
778  * As the modem line interrupts (cts, dsr, rng, dcd) only get updated with TIOCMIWAIT active,
779  * no need to carry them in the internal structure
780  *
781  */
782 typedef struct serial_irq_info
783 {
784     int rx , tx, frame, overrun, parity, brk, buf_overrun;
785     DWORD temt;
786 }serial_irq_info;
787
788 /***********************************************************************
789  * Data needed by the thread polling for the changing CommEvent
790  */
791 typedef struct async_commio
792 {
793     HANDLE              hDevice;
794     DWORD*              events;
795     IO_STATUS_BLOCK*    iosb;
796     HANDLE              hEvent;
797     DWORD               evtmask;
798     DWORD               mstat;
799     serial_irq_info     irq_info;
800 } async_commio;
801
802 /***********************************************************************
803  *   Get extended interrupt count info, needed for wait_on
804  */
805 static NTSTATUS get_irq_info(int fd, serial_irq_info *irq_info)
806 {
807 #ifdef TIOCGICOUNT
808     struct serial_icounter_struct einfo;
809     if (!ioctl(fd, TIOCGICOUNT, &einfo))
810     {
811         irq_info->rx          = einfo.rx;
812         irq_info->tx          = einfo.tx;
813         irq_info->frame       = einfo.frame;
814         irq_info->overrun     = einfo.overrun;
815         irq_info->parity      = einfo.parity;
816         irq_info->brk         = einfo.brk;
817         irq_info->buf_overrun = einfo.buf_overrun;
818     }
819     else
820     {
821         TRACE("TIOCGICOUNT err %s\n", strerror(errno));
822         memset(irq_info,0, sizeof(serial_irq_info));
823         return FILE_GetNtStatus();
824     }
825 #else
826     memset(irq_info,0, sizeof(serial_irq_info));
827     return STATUS_NOT_IMPLEMENTED;
828 #endif
829     irq_info->temt = 0;
830     /* Generate a single TX_TXEMPTY event when the TX Buffer turns empty*/
831 #ifdef TIOCSERGETLSR  /* prefer to log the state TIOCSERGETLSR */
832     if (ioctl(fd, TIOCSERGETLSR, &irq_info->temt))
833     {
834         TRACE("TIOCSERGETLSR err %s\n", strerror(errno));
835         return FILE_GetNtStatus();
836     }
837 #elif defined(TIOCOUTQ)  /* otherwise we log when the out queue gets empty */
838     if (ioctl(fd, TIOCOUTQ, &irq_info->temt))
839     {
840         TRACE("TIOCOUTQ err %s\n", strerror(errno));
841         return FILE_GetNtStatus();
842     }
843     else
844     {
845         if (irq_info->temt == 0)
846             irq_info->temt = 1;
847     }
848 #endif
849     return STATUS_SUCCESS;
850 }
851
852
853 static DWORD check_events(int fd, DWORD mask,
854                           const serial_irq_info *new,
855                           const serial_irq_info *old,
856                           DWORD new_mstat, DWORD old_mstat)
857 {
858     DWORD ret = 0, queue;
859
860     TRACE("mask 0x%08x\n", mask);
861     TRACE("old->rx          0x%08x vs. new->rx          0x%08x\n", old->rx, new->rx);
862     TRACE("old->tx          0x%08x vs. new->tx          0x%08x\n", old->tx, new->tx);
863     TRACE("old->frame       0x%08x vs. new->frame       0x%08x\n", old->frame, new->frame);
864     TRACE("old->overrun     0x%08x vs. new->overrun     0x%08x\n", old->overrun, new->overrun);
865     TRACE("old->parity      0x%08x vs. new->parity      0x%08x\n", old->parity, new->parity);
866     TRACE("old->brk         0x%08x vs. new->brk         0x%08x\n", old->brk, new->brk);
867     TRACE("old->buf_overrun 0x%08x vs. new->buf_overrun 0x%08x\n", old->buf_overrun, new->buf_overrun);
868
869     if (old->brk != new->brk) ret |= EV_BREAK;
870     if ((old_mstat & MS_CTS_ON ) != (new_mstat & MS_CTS_ON )) ret |= EV_CTS;
871     if ((old_mstat & MS_DSR_ON ) != (new_mstat & MS_DSR_ON )) ret |= EV_DSR;
872     if ((old_mstat & MS_RING_ON) != (new_mstat & MS_RING_ON)) ret |= EV_RING;
873     if ((old_mstat & MS_RLSD_ON) != (new_mstat & MS_RLSD_ON)) ret |= EV_RLSD;
874     if (old->frame != new->frame || old->overrun != new->overrun || old->parity != new->parity) ret |= EV_ERR;
875     if (mask & EV_RXCHAR)
876     {
877         queue = 0;
878 #ifdef TIOCINQ
879         if (ioctl(fd, TIOCINQ, &queue))
880             WARN("TIOCINQ returned error\n");
881 #endif
882         if (queue)
883             ret |= EV_RXCHAR;
884     }
885     if (mask & EV_TXEMPTY)
886     {
887         if (!old->temt && new->temt)
888             ret |= EV_TXEMPTY;
889     }
890     return ret & mask;
891 }
892
893 /***********************************************************************
894  *             wait_for_event      (INTERNAL)
895  *
896  *  We need to poll for what is interesting
897  *  TIOCMIWAIT only checks modem status line and may not be aborted by a changing mask
898  *
899  */
900 static DWORD CALLBACK wait_for_event(LPVOID arg)
901 {
902     async_commio *commio = arg;
903     int fd, needs_close;
904
905     if (!server_get_unix_fd( commio->hDevice, FILE_READ_DATA | FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL ))
906     {
907         serial_irq_info new_irq_info;
908         DWORD new_mstat, new_evtmask;
909         LARGE_INTEGER time;
910         
911         TRACE("device=%p fd=0x%08x mask=0x%08x buffer=%p event=%p irq_info=%p\n", 
912               commio->hDevice, fd, commio->evtmask, commio->events, commio->hEvent, &commio->irq_info);
913
914         time.QuadPart = (ULONGLONG)10000;
915         time.QuadPart = -time.QuadPart;
916         for (;;)
917         {
918             /*
919              * TIOCMIWAIT is not adequate
920              *
921              * FIXME:
922              * We don't handle the EV_RXFLAG (the eventchar)
923              */
924             NtDelayExecution(FALSE, &time);
925             get_irq_info(fd, &new_irq_info);
926             if (get_modem_status(fd, &new_mstat))
927                 TRACE("get_modem_status failed\n");
928             *commio->events = check_events(fd, commio->evtmask,
929                                            &new_irq_info, &commio->irq_info,
930                                            new_mstat, commio->mstat);
931             if (*commio->events) break;
932             get_wait_mask(commio->hDevice, &new_evtmask);
933             if (commio->evtmask != new_evtmask)
934             {
935                 *commio->events = 0;
936                 break;
937             }
938         }
939         if (needs_close) close( fd );
940     }
941     if (commio->iosb) commio->iosb->u.Status = *commio->events ? STATUS_SUCCESS : STATUS_CANCELLED;
942     if (commio->hEvent) NtSetEvent(commio->hEvent, NULL);
943     RtlFreeHeap(GetProcessHeap(), 0, commio);
944     return 0;
945 }
946
947 static NTSTATUS wait_on(HANDLE hDevice, int fd, HANDLE hEvent, PIO_STATUS_BLOCK piosb, DWORD* events)
948 {
949     async_commio*       commio;
950     NTSTATUS            status;
951
952     if ((status = NtResetEvent(hEvent, NULL)))
953         return status;
954
955     commio = RtlAllocateHeap(GetProcessHeap(), 0, sizeof (async_commio));
956     if (!commio) return STATUS_NO_MEMORY;
957
958     commio->hDevice = hDevice;
959     commio->events  = events;
960     commio->iosb    = piosb;
961     commio->hEvent  = hEvent;
962     get_wait_mask(commio->hDevice, &commio->evtmask);
963
964 /* We may never return, if some capabilities miss
965  * Return error in that case
966  */
967 #if !defined(TIOCINQ)
968     if (commio->evtmask & EV_RXCHAR)
969         goto error_caps;
970 #endif
971 #if !(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)
972     if (commio->evtmask & EV_TXEMPTY)
973         goto error_caps;
974 #endif
975 #if !defined(TIOCMGET)
976     if (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD))
977         goto error_caps;
978 #endif
979 #if !defined(TIOCM_CTS)
980     if (commio->evtmask & EV_CTS)
981         goto error_caps;
982 #endif
983 #if !defined(TIOCM_DSR)
984     if (commio->evtmask & EV_DSR)
985         goto error_caps;
986 #endif
987 #if !defined(TIOCM_RNG)
988     if (commio->evtmask & EV_RING)
989         goto error_caps;
990 #endif
991 #if !defined(TIOCM_CAR)
992     if (commio->evtmask & EV_RLSD)
993         goto error_caps;
994 #endif
995     if (commio->evtmask & EV_RXFLAG)
996         FIXME("EV_RXFLAG not handled\n");
997
998     if ((status = get_irq_info(fd, &commio->irq_info)) &&
999         (commio->evtmask & (EV_BREAK | EV_ERR)))
1000         goto out_now;
1001
1002     if ((status = get_modem_status(fd, &commio->mstat)) &&
1003         (commio->evtmask & (EV_CTS | EV_DSR| EV_RING| EV_RLSD)))
1004         goto out_now;
1005
1006     /* We might have received something or the TX buffer is delivered */
1007     *events = check_events(fd, commio->evtmask,
1008                                &commio->irq_info, &commio->irq_info,
1009                                commio->mstat, commio->mstat);
1010     if (*events)
1011     {
1012         status = STATUS_SUCCESS;
1013         goto out_now;
1014     }
1015
1016     /* create the worker for the task */
1017     status = RtlQueueWorkItem(wait_for_event, commio, 0 /* FIXME */);
1018     if (status != STATUS_SUCCESS) goto out_now;
1019     return STATUS_PENDING;
1020
1021 #if !defined(TIOCINQ) || (!(defined(TIOCSERGETLSR) && defined(TIOCSER_TEMT)) || !defined(TIOCINQ)) || !defined(TIOCMGET) || !defined(TIOCM_CTS) ||!defined(TIOCM_DSR) || !defined(TIOCM_RNG) || !defined(TIOCM_CAR)
1022 error_caps:
1023     FIXME("Returning error because of missing capabilities\n");
1024     status = STATUS_INVALID_PARAMETER;
1025 #endif
1026 out_now:
1027     RtlFreeHeap(GetProcessHeap(), 0, commio);
1028     return status;
1029 }
1030
1031 static NTSTATUS xmit_immediate(HANDLE hDevice, int fd, const char* ptr)
1032 {
1033     /* FIXME: not perfect as it should bypass the in-queue */
1034     WARN("(%p,'%c') not perfect!\n", hDevice, *ptr);
1035     if (write(fd, ptr, 1) != 1)
1036         return FILE_GetNtStatus();
1037     return STATUS_SUCCESS;
1038 }
1039
1040 /******************************************************************
1041  *              COMM_DeviceIoControl
1042  *
1043  *
1044  */
1045 static inline NTSTATUS io_control(HANDLE hDevice, 
1046                                   HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1047                                   PVOID UserApcContext, 
1048                                   PIO_STATUS_BLOCK piosb, 
1049                                   ULONG dwIoControlCode,
1050                                   LPVOID lpInBuffer, DWORD nInBufferSize,
1051                                   LPVOID lpOutBuffer, DWORD nOutBufferSize)
1052 {
1053     DWORD       sz = 0, access = FILE_READ_DATA;
1054     NTSTATUS    status = STATUS_SUCCESS;
1055     int         fd = -1, needs_close = 0;
1056
1057     TRACE("%p %s %p %d %p %d %p\n",
1058           hDevice, iocode2str(dwIoControlCode), lpInBuffer, nInBufferSize,
1059           lpOutBuffer, nOutBufferSize, piosb);
1060
1061     piosb->Information = 0;
1062
1063     if (dwIoControlCode != IOCTL_SERIAL_GET_TIMEOUTS &&
1064         dwIoControlCode != IOCTL_SERIAL_SET_TIMEOUTS)
1065     {
1066         enum server_fd_type type;
1067         if ((status = server_get_unix_fd( hDevice, access, &fd, &needs_close, &type, NULL )))
1068             goto error;
1069         if (type != FD_TYPE_SERIAL)
1070         {
1071             if (needs_close) close( fd );
1072             status = STATUS_OBJECT_TYPE_MISMATCH;
1073             goto error;
1074         }
1075     }
1076
1077     switch (dwIoControlCode)
1078     {
1079     case IOCTL_SERIAL_CLR_DTR:
1080 #ifdef TIOCM_DTR
1081         if (whack_modem(fd, ~TIOCM_DTR, 0) == -1) status = FILE_GetNtStatus();
1082 #else
1083         status = STATUS_NOT_SUPPORTED;
1084 #endif
1085         break;
1086     case IOCTL_SERIAL_CLR_RTS:
1087 #ifdef TIOCM_RTS
1088         if (whack_modem(fd, ~TIOCM_RTS, 0) == -1) status = FILE_GetNtStatus();
1089 #else
1090         status = STATUS_NOT_SUPPORTED;
1091 #endif
1092         break;
1093     case IOCTL_SERIAL_GET_BAUD_RATE:
1094         if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_BAUD_RATE))
1095         {
1096             if (!(status = get_baud_rate(fd, lpOutBuffer)))
1097                 sz = sizeof(SERIAL_BAUD_RATE);
1098         }
1099         else
1100             status = STATUS_INVALID_PARAMETER;
1101         break;
1102     case IOCTL_SERIAL_GET_CHARS:
1103         if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_CHARS))
1104         {
1105             if (!(status = get_special_chars(fd, lpOutBuffer)))
1106                 sz = sizeof(SERIAL_CHARS);
1107         }
1108         else
1109             status = STATUS_INVALID_PARAMETER;
1110         break;
1111      case IOCTL_SERIAL_GET_COMMSTATUS:
1112         if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_STATUS))
1113         {
1114             if (!(status = get_status(fd, lpOutBuffer)))
1115                 sz = sizeof(SERIAL_STATUS);
1116         }
1117         else status = STATUS_INVALID_PARAMETER;
1118         break;
1119     case IOCTL_SERIAL_GET_HANDFLOW:
1120         if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_HANDFLOW))
1121         {
1122             if (!(status = get_hand_flow(fd, lpOutBuffer)))
1123                 sz = sizeof(SERIAL_HANDFLOW);
1124         }
1125         else
1126             status = STATUS_INVALID_PARAMETER;
1127         break;
1128     case IOCTL_SERIAL_GET_LINE_CONTROL:
1129         if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_LINE_CONTROL))
1130         {
1131             if (!(status = get_line_control(fd, lpOutBuffer)))
1132                 sz = sizeof(SERIAL_LINE_CONTROL);
1133         }
1134         else
1135             status = STATUS_INVALID_PARAMETER;
1136         break;
1137     case IOCTL_SERIAL_GET_MODEMSTATUS:
1138         if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1139         {
1140             if (!(status = get_modem_status(fd, lpOutBuffer)))
1141                 sz = sizeof(DWORD);
1142         }
1143         else status = STATUS_INVALID_PARAMETER;
1144         break;
1145     case IOCTL_SERIAL_GET_TIMEOUTS:
1146         if (lpOutBuffer && nOutBufferSize == sizeof(SERIAL_TIMEOUTS))
1147         {
1148             if (!(status = get_timeouts(hDevice, lpOutBuffer)))
1149                 sz = sizeof(SERIAL_TIMEOUTS);
1150         }
1151         else
1152             status = STATUS_INVALID_PARAMETER;
1153         break;
1154     case IOCTL_SERIAL_GET_WAIT_MASK:
1155         if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1156         {
1157             if (!(status = get_wait_mask(hDevice, lpOutBuffer)))
1158                 sz = sizeof(DWORD);
1159         }
1160         else
1161             status = STATUS_INVALID_PARAMETER;
1162         break;
1163     case IOCTL_SERIAL_IMMEDIATE_CHAR:
1164         if (lpInBuffer && nInBufferSize == sizeof(CHAR))
1165             status = xmit_immediate(hDevice, fd, lpInBuffer);
1166         else
1167             status = STATUS_INVALID_PARAMETER;
1168         break;
1169     case IOCTL_SERIAL_PURGE:
1170         if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1171             status = purge(fd, *(DWORD*)lpInBuffer);
1172         else
1173             status = STATUS_INVALID_PARAMETER;
1174         break;
1175     case IOCTL_SERIAL_RESET_DEVICE:
1176         FIXME("Unsupported\n");
1177         break;
1178     case IOCTL_SERIAL_SET_BAUD_RATE:
1179         if (lpInBuffer && nInBufferSize == sizeof(SERIAL_BAUD_RATE))
1180             status = set_baud_rate(fd, lpInBuffer);
1181         else
1182             status = STATUS_INVALID_PARAMETER;
1183         break;
1184     case IOCTL_SERIAL_SET_BREAK_OFF:
1185 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1186         if (ioctl(fd, TIOCCBRK, 0) == -1)
1187         {
1188             TRACE("ioctl failed\n");
1189             status = FILE_GetNtStatus();
1190         }
1191 #else
1192         FIXME("ioctl not available\n");
1193         status = STATUS_NOT_SUPPORTED;
1194 #endif
1195         break;
1196     case IOCTL_SERIAL_SET_BREAK_ON:
1197 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
1198         if (ioctl(fd, TIOCSBRK, 0) == -1)
1199         {
1200             TRACE("ioctl failed\n");
1201             status = FILE_GetNtStatus();
1202         }
1203 #else
1204         FIXME("ioctl not available\n");
1205         status = STATUS_NOT_SUPPORTED;
1206 #endif
1207         break;
1208     case IOCTL_SERIAL_SET_CHARS:
1209         if (lpInBuffer && nInBufferSize == sizeof(SERIAL_CHARS))
1210             status = set_special_chars(fd, lpInBuffer);
1211         else
1212             status = STATUS_INVALID_PARAMETER;
1213         break;
1214     case IOCTL_SERIAL_SET_DTR:
1215 #ifdef TIOCM_DTR
1216         if (whack_modem(fd, 0, TIOCM_DTR) == -1) status = FILE_GetNtStatus();
1217 #else
1218         status = STATUS_NOT_SUPPORTED;
1219 #endif
1220         break;
1221     case IOCTL_SERIAL_SET_HANDFLOW:
1222         if (lpInBuffer && nInBufferSize == sizeof(SERIAL_HANDFLOW))
1223             status = set_handflow(fd, lpInBuffer);
1224         else
1225             status = STATUS_INVALID_PARAMETER;
1226         break;
1227     case IOCTL_SERIAL_SET_LINE_CONTROL:
1228         if (lpInBuffer && nInBufferSize == sizeof(SERIAL_LINE_CONTROL))
1229             status = set_line_control(fd, lpInBuffer);
1230         else
1231             status = STATUS_INVALID_PARAMETER;
1232         break;
1233     case IOCTL_SERIAL_SET_QUEUE_SIZE:
1234         if (lpInBuffer && nInBufferSize == sizeof(SERIAL_QUEUE_SIZE))
1235             status = set_queue_size(fd, lpInBuffer);
1236         else
1237             status = STATUS_INVALID_PARAMETER;
1238         break;
1239     case IOCTL_SERIAL_SET_RTS:
1240 #ifdef TIOCM_RTS
1241         if (whack_modem(fd, 0, TIOCM_RTS) == -1) status = FILE_GetNtStatus();
1242 #else
1243         status = STATUS_NOT_SUPPORTED;
1244 #endif
1245         break;
1246     case IOCTL_SERIAL_SET_TIMEOUTS:
1247         if (lpInBuffer && nInBufferSize == sizeof(SERIAL_TIMEOUTS))
1248             status = set_timeouts(hDevice, lpInBuffer);
1249         else
1250             status = STATUS_INVALID_PARAMETER;
1251         break;
1252     case IOCTL_SERIAL_SET_WAIT_MASK:
1253         if (lpInBuffer && nInBufferSize == sizeof(DWORD))
1254         {
1255             status = set_wait_mask(hDevice, *(DWORD*)lpInBuffer);
1256         }
1257         else status = STATUS_INVALID_PARAMETER;
1258         break;
1259     case IOCTL_SERIAL_SET_XOFF:
1260         status = set_XOff(fd);
1261         break;
1262     case IOCTL_SERIAL_SET_XON:
1263         status = set_XOn(fd);
1264         break;
1265     case IOCTL_SERIAL_WAIT_ON_MASK:
1266         if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
1267         {
1268             if (!(status = wait_on(hDevice, fd, hEvent, piosb, lpOutBuffer)))
1269                 sz = sizeof(DWORD);
1270         }
1271         else
1272             status = STATUS_INVALID_PARAMETER;
1273         break;
1274     default:
1275         FIXME("Unsupported IOCTL %x (type=%x access=%x func=%x meth=%x)\n", 
1276               dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
1277               (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
1278         sz = 0;
1279         status = STATUS_INVALID_PARAMETER;
1280         break;
1281     }
1282     if (needs_close) close( fd );
1283  error:
1284     piosb->u.Status = status;
1285     piosb->Information = sz;
1286     if (hEvent && status != STATUS_PENDING) NtSetEvent(hEvent, NULL);
1287     return status;
1288 }
1289
1290 NTSTATUS COMM_DeviceIoControl(HANDLE hDevice, 
1291                               HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
1292                               PVOID UserApcContext, 
1293                               PIO_STATUS_BLOCK piosb, 
1294                               ULONG dwIoControlCode,
1295                               LPVOID lpInBuffer, DWORD nInBufferSize,
1296                               LPVOID lpOutBuffer, DWORD nOutBufferSize)
1297 {
1298     NTSTATUS    status;
1299
1300     if (dwIoControlCode == IOCTL_SERIAL_WAIT_ON_MASK)
1301     {
1302         HANDLE          hev = hEvent;
1303
1304         /* this is an ioctl we implement in a non blocking way if hEvent is not
1305          * null
1306          * so we have to explicitly wait if no hEvent is provided
1307          */
1308         if (!hev)
1309         {
1310             OBJECT_ATTRIBUTES   attr;
1311             
1312             attr.Length                   = sizeof(attr);
1313             attr.RootDirectory            = 0;
1314             attr.ObjectName               = NULL;
1315             attr.Attributes               = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
1316             attr.SecurityDescriptor       = NULL;
1317             attr.SecurityQualityOfService = NULL;
1318             status = NtCreateEvent(&hev, EVENT_ALL_ACCESS, &attr, SynchronizationEvent, FALSE);
1319
1320             if (status) goto done;
1321         }
1322         status = io_control(hDevice, hev, UserApcRoutine, UserApcContext,
1323                             piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1324                             lpOutBuffer, nOutBufferSize);
1325         if (hev != hEvent)
1326         {
1327             if (status == STATUS_PENDING)
1328             {
1329                 NtWaitForSingleObject(hev, FALSE, NULL);
1330                 status = STATUS_SUCCESS;
1331             }
1332             NtClose(hev);
1333         }
1334     }
1335     else status = io_control(hDevice, hEvent, UserApcRoutine, UserApcContext,
1336                              piosb, dwIoControlCode, lpInBuffer, nInBufferSize,
1337                              lpOutBuffer, nOutBufferSize);
1338 done:
1339     return status;
1340 }