user: Fix return type and argument type for GetDlgItemText{A,W}.
[wine/multimedia.git] / dlls / ntdll / serial.c
blob04f95344d296f47433a408bfa0029ac850fbb378
1 /* Main file for COMM support
3 * DEC 93 Erik Bos <erik@xs4all.nl>
4 * Copyright 1996 Marcus Meissner
5 * Copyright 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_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
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"
79 #ifdef HAVE_LINUX_SERIAL_H
80 #include <linux/serial.h>
81 #endif
83 WINE_DEFAULT_DEBUG_CHANNEL(comm);
85 static const char* iocode2str(DWORD ioc)
87 switch (ioc)
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; }
132 static NTSTATUS get_modem_status(int fd, DWORD* lpModemStat)
134 NTSTATUS status = STATUS_SUCCESS;
135 int mstat;
137 #ifdef TIOCMGET
138 if (ioctl(fd, TIOCMGET, &mstat) == -1)
140 WARN("ioctl failed\n");
141 status = FILE_GetNtStatus();
143 else
145 *lpModemStat = 0;
146 #ifdef TIOCM_CTS
147 if (mstat & TIOCM_CTS) *lpModemStat |= MS_CTS_ON;
148 #endif
149 #ifdef TIOCM_DSR
150 if (mstat & TIOCM_DSR) *lpModemStat |= MS_DSR_ON;
151 #endif
152 #ifdef TIOCM_RNG
153 if (mstat & TIOCM_RNG) *lpModemStat |= MS_RING_ON;
154 #endif
155 #ifdef TIOCM_CAR
156 /* FIXME: Not really sure about RLSD UB 990810 */
157 if (mstat & TIOCM_CAR) *lpModemStat |= MS_RLSD_ON;
158 #endif
159 TRACE("%04x -> %s%s%s%s\n", mstat,
160 (*lpModemStat & MS_RLSD_ON) ? "MS_RLSD_ON " : "",
161 (*lpModemStat & MS_RING_ON) ? "MS_RING_ON " : "",
162 (*lpModemStat & MS_DSR_ON) ? "MS_DSR_ON " : "",
163 (*lpModemStat & MS_CTS_ON) ? "MS_CTS_ON " : "");
165 #else
166 status = STATUS_NOT_SUPPORTED;
167 #endif
168 return status;
171 static NTSTATUS get_wait_mask(HANDLE hDevice, DWORD* mask)
173 NTSTATUS status;
175 SERVER_START_REQ( get_serial_info )
177 req->handle = hDevice;
178 if (!(status = wine_server_call( req )))
179 *mask = reply->eventmask;
181 SERVER_END_REQ;
182 return status;
185 static NTSTATUS purge(int fd, DWORD flags)
188 ** not exactly sure how these are different
189 ** Perhaps if we had our own internal queues, one flushes them
190 ** and the other flushes the kernel's buffers.
192 if (flags & PURGE_TXABORT) tcflush(fd, TCOFLUSH);
193 if (flags & PURGE_RXABORT) tcflush(fd, TCIFLUSH);
194 if (flags & PURGE_TXCLEAR) tcflush(fd, TCOFLUSH);
195 if (flags & PURGE_RXCLEAR) tcflush(fd, TCIFLUSH);
196 return STATUS_SUCCESS;
199 static NTSTATUS set_wait_mask(HANDLE hDevice, DWORD mask)
201 NTSTATUS status;
203 SERVER_START_REQ( set_serial_info )
205 req->handle = hDevice;
206 req->flags = SERIALINFO_SET_MASK;
207 req->eventmask = mask;
208 status = wine_server_call( req );
210 SERVER_END_REQ;
211 return status;
214 static NTSTATUS xmit_immediate(HANDLE hDevice, int fd, char* ptr)
216 /* FIXME: not perfect as it should bypass the in-queue */
217 WARN("(%p,'%c') not perfect!\n", hDevice, *ptr);
218 if (write(fd, ptr, 1) != 1)
219 return FILE_GetNtStatus();
220 return STATUS_SUCCESS;
223 /******************************************************************
224 * COMM_DeviceIoControl
228 NTSTATUS COMM_DeviceIoControl(HANDLE hDevice,
229 HANDLE hEvent, PIO_APC_ROUTINE UserApcRoutine,
230 PVOID UserApcContext,
231 PIO_STATUS_BLOCK piosb,
232 ULONG dwIoControlCode,
233 LPVOID lpInBuffer, DWORD nInBufferSize,
234 LPVOID lpOutBuffer, DWORD nOutBufferSize)
236 DWORD sz = 0, access = FILE_READ_DATA;
237 NTSTATUS status = STATUS_SUCCESS;
238 int fd;
240 TRACE("%p %s %p %ld %p %ld %p\n",
241 hDevice, iocode2str(dwIoControlCode), lpInBuffer, nInBufferSize,
242 lpOutBuffer, nOutBufferSize, piosb);
244 piosb->Information = 0;
246 if ((status = wine_server_handle_to_fd( hDevice, access, &fd, NULL ))) goto error;
248 switch (dwIoControlCode)
250 case IOCTL_SERIAL_GET_MODEMSTATUS:
251 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
253 if (!(status = get_modem_status(fd, (DWORD*)lpOutBuffer)))
254 sz = sizeof(DWORD);
256 else status = STATUS_INVALID_PARAMETER;
257 break;
258 case IOCTL_SERIAL_GET_WAIT_MASK:
259 if (lpOutBuffer && nOutBufferSize == sizeof(DWORD))
261 if (!(status = get_wait_mask(hDevice, (DWORD*)lpOutBuffer)))
262 sz = sizeof(DWORD);
264 else
265 status = STATUS_INVALID_PARAMETER;
266 break;
267 case IOCTL_SERIAL_IMMEDIATE_CHAR:
268 if (lpInBuffer && nInBufferSize == sizeof(CHAR))
269 status = xmit_immediate(hDevice, fd, lpInBuffer);
270 else
271 status = STATUS_INVALID_PARAMETER;
272 break;
273 case IOCTL_SERIAL_PURGE:
274 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
275 status = purge(fd, *(DWORD*)lpInBuffer);
276 else
277 status = STATUS_INVALID_PARAMETER;
278 break;
279 case IOCTL_SERIAL_SET_BREAK_OFF:
280 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
281 if (ioctl(fd, TIOCCBRK, 0) == -1)
283 TRACE("ioctl failed\n");
284 status = FILE_GetNtStatus();
286 #else
287 FIXME("ioctl not available\n");
288 status = STATUS_NOT_SUPPORTED;
289 #endif
290 break;
291 case IOCTL_SERIAL_SET_BREAK_ON:
292 #if defined(TIOCSBRK) && defined(TIOCCBRK) /* check if available for compilation */
293 if (ioctl(fd, TIOCSBRK, 0) == -1)
295 TRACE("ioctl failed\n");
296 status = FILE_GetNtStatus();
298 #else
299 FIXME("ioctl not available\n");
300 status = STATUS_NOT_SUPPORTED;
301 #endif
302 break;
303 case IOCTL_SERIAL_SET_WAIT_MASK:
304 if (lpInBuffer && nInBufferSize == sizeof(DWORD))
306 status = set_wait_mask(hDevice, *(DWORD*)lpInBuffer);
308 else status = STATUS_INVALID_PARAMETER;
309 break;
310 default:
311 FIXME("Unsupported IOCTL %lx (type=%lx access=%lx func=%lx meth=%lx)\n",
312 dwIoControlCode, dwIoControlCode >> 16, (dwIoControlCode >> 14) & 3,
313 (dwIoControlCode >> 2) & 0xFFF, dwIoControlCode & 3);
314 sz = 0;
315 status = STATUS_INVALID_PARAMETER;
316 break;
318 wine_server_release_fd( hDevice, fd );
319 error:
320 piosb->u.Status = status;
321 piosb->Information = sz;
322 if (hEvent) NtSetEvent(hEvent, NULL);
323 return status;