kernel32: FindFirstChangeNotification needs a static IO_STATUS_BLOCK.
[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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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_STRINGS_H
31 # include <strings.h>
32 #endif
33 #ifdef HAVE_TERMIOS_H
34 #include <termios.h>
35 #endif
36 #ifdef HAVE_IO_H
37 # include <io.h>
38 #endif
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #ifdef HAVE_TERMIOS_H
43 #include <termios.h>
44 #endif
45 #include <fcntl.h>
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
48 #endif
49 #include <sys/types.h>
50 #ifdef HAVE_SYS_FILIO_H
51 # include <sys/filio.h>
52 #endif
53 #ifdef HAVE_SYS_IOCTL_H
54 #include <sys/ioctl.h>
55 #endif
56 #ifdef HAVE_SYS_POLL_H
57 # include <sys/poll.h>
58 #endif
59 #ifdef HAVE_SYS_MODEM_H
60 # include <sys/modem.h>
61 #endif
62 #ifdef HAVE_SYS_STRTIO_H
63 # include <sys/strtio.h>
64 #endif
65
66 #define NONAMELESSUNION
67 #define NONAMELESSSTRUCT
68 #include "ntstatus.h"
69 #define WIN32_NO_STATUS
70 #include "windef.h"
71 #include "winternl.h"
72 #include "winioctl.h"
73 #include "ddk/ntddser.h"
74 #include "ntdll_misc.h"
75 #include "wine/server.h"
76 #include "wine/library.h"
77 #include "wine/debug.h"
78
79 #ifdef HAVE_LINUX_SERIAL_H
80 #include <linux/serial.h>
81 #endif
82
83 WINE_DEFAULT_DEBUG_CHANNEL(comm);
84
85 static const char* iocode2str(DWORD ioc)
86 {
87     switch (ioc)
88     {
89 #define X(x)    case (x): return #x;
90         X(IOCTL_SERIAL_CLEAR_STATS);
91         X(IOCTL_SERIAL_CLR_DTR);
92         X(IOCTL_SERIAL_CLR_RTS);
93         X(IOCTL_SERIAL_CONFIG_SIZE);
94         X(IOCTL_SERIAL_GET_BAUD_RATE);
95         X(IOCTL_SERIAL_GET_CHARS);
96         X(IOCTL_SERIAL_GET_COMMSTATUS);
97         X(IOCTL_SERIAL_GET_DTRRTS);
98         X(IOCTL_SERIAL_GET_HANDFLOW);
99         X(IOCTL_SERIAL_GET_LINE_CONTROL);
100         X(IOCTL_SERIAL_GET_MODEM_CONTROL);
101         X(IOCTL_SERIAL_GET_MODEMSTATUS);
102         X(IOCTL_SERIAL_GET_PROPERTIES);
103         X(IOCTL_SERIAL_GET_STATS);
104         X(IOCTL_SERIAL_GET_TIMEOUTS);
105         X(IOCTL_SERIAL_GET_WAIT_MASK);
106         X(IOCTL_SERIAL_IMMEDIATE_CHAR);
107         X(IOCTL_SERIAL_LSRMST_INSERT);
108         X(IOCTL_SERIAL_PURGE);
109         X(IOCTL_SERIAL_RESET_DEVICE);
110         X(IOCTL_SERIAL_SET_BAUD_RATE);
111         X(IOCTL_SERIAL_SET_BREAK_ON);
112         X(IOCTL_SERIAL_SET_BREAK_OFF);
113         X(IOCTL_SERIAL_SET_CHARS);
114         X(IOCTL_SERIAL_SET_DTR);
115         X(IOCTL_SERIAL_SET_FIFO_CONTROL);
116         X(IOCTL_SERIAL_SET_HANDFLOW);
117         X(IOCTL_SERIAL_SET_LINE_CONTROL);
118         X(IOCTL_SERIAL_SET_MODEM_CONTROL);
119         X(IOCTL_SERIAL_SET_QUEUE_SIZE);
120         X(IOCTL_SERIAL_SET_RTS);
121         X(IOCTL_SERIAL_SET_TIMEOUTS);
122         X(IOCTL_SERIAL_SET_WAIT_MASK);
123         X(IOCTL_SERIAL_SET_XOFF);
124         X(IOCTL_SERIAL_SET_XON);
125         X(IOCTL_SERIAL_WAIT_ON_MASK);
126         X(IOCTL_SERIAL_XOFF_COUNTER);
127 #undef X
128     default: { static char tmp[32]; sprintf(tmp, "IOCTL_SERIAL_%ld\n", ioc); return tmp; }
129     }
130 }
131
132 /******************************************************************
133  *              COMM_DeviceIoControl
134  *
135  *
136  */
137 NTSTATUS COMM_DeviceIoControl(HANDLE hDevice, 
138                               HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
139                               PVOID UserApcContext, 
140                               PIO_STATUS_BLOCK piosb, 
141                               ULONG dwIoControlCode,
142                               LPVOID lpInBuffer, DWORD nInBufferSize,
143                               LPVOID lpOutBuffer, DWORD nOutBufferSize)
144 {
145     DWORD       sz = 0, access = FILE_READ_DATA;
146     NTSTATUS    status = STATUS_SUCCESS;
147     int         fd;
148
149     TRACE("%p %s %p %ld %p %ld %p\n",
150           hDevice, iocode2str(dwIoControlCode), lpInBuffer, nInBufferSize,
151           lpOutBuffer, nOutBufferSize, piosb);
152
153     piosb->Information = 0;
154
155     if ((status = wine_server_handle_to_fd( hDevice, access, &fd, NULL ))) goto error;
156
157     switch (dwIoControlCode)
158     {
159     case IOCTL_SERIAL_SET_BREAK_OFF:
160 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
161         if (ioctl(fd, TIOCCBRK, 0) == -1)
162         {
163             TRACE("ioctl failed\n");
164             status = FILE_GetNtStatus();
165         }
166 #else
167         FIXME("ioctl not available\n");
168         status = STATUS_NOT_SUPPORTED;
169 #endif
170         break;
171     case IOCTL_SERIAL_SET_BREAK_ON:
172 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
173         if (ioctl(fd, TIOCSBRK, 0) == -1)
174         {
175             TRACE("ioctl failed\n");
176             status = FILE_GetNtStatus();
177         }
178 #else
179         FIXME("ioctl not available\n");
180         status = STATUS_NOT_SUPPORTED;
181 #endif
182         break;
183     default:
184         FIXME("Unsupported IOCTL %lx (type=%lx access=%lx func=%lx meth=%lx)\n", 
185               dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
186               (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
187         sz = 0;
188         status = STATUS_INVALID_PARAMETER;
189         break;
190     }
191     wine_server_release_fd( hDevice, fd );
192  error:
193     piosb->u.Status = status;
194     piosb->Information = sz;
195     if (hEvent) NtSetEvent(hEvent, NULL);
196     return status;
197 }