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
23 #include "wine/port.h"
46 #ifdef HAVE_SYS_STAT_H
47 # include <sys/stat.h>
49 #include <sys/types.h>
50 #ifdef HAVE_SYS_FILIO_H
51 # include <sys/filio.h>
53 #ifdef HAVE_SYS_IOCTL_H
54 #include <sys/ioctl.h>
56 #ifdef HAVE_SYS_POLL_H
57 # include <sys/poll.h>
59 #ifdef HAVE_SYS_MODEM_H
60 # include <sys/modem.h>
62 #ifdef HAVE_SYS_STRTIO_H
63 # include <sys/strtio.h>
66 #define NONAMELESSUNION
67 #define NONAMELESSSTRUCT
69 #define WIN32_NO_STATUS
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>
83 WINE_DEFAULT_DEBUG_CHANNEL(comm
);
85 static const char* iocode2str(DWORD 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
);
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
;
138 if (ioctl(fd
, TIOCMGET
, &mstat
) == -1)
140 WARN("ioctl failed\n");
141 status
= FILE_GetNtStatus();
147 if (mstat
& TIOCM_CTS
) *lpModemStat
|= MS_CTS_ON
;
150 if (mstat
& TIOCM_DSR
) *lpModemStat
|= MS_DSR_ON
;
153 if (mstat
& TIOCM_RNG
) *lpModemStat
|= MS_RING_ON
;
156 /* FIXME: Not really sure about RLSD UB 990810 */
157 if (mstat
& TIOCM_CAR
) *lpModemStat
|= MS_RLSD_ON
;
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 " : "");
166 status
= STATUS_NOT_SUPPORTED
;
171 static NTSTATUS
get_wait_mask(HANDLE hDevice
, DWORD
* mask
)
175 SERVER_START_REQ( get_serial_info
)
177 req
->handle
= hDevice
;
178 if (!(status
= wine_server_call( req
)))
179 *mask
= reply
->eventmask
;
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
)
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
);
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
;
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
)))
256 else status
= STATUS_INVALID_PARAMETER
;
258 case IOCTL_SERIAL_GET_WAIT_MASK
:
259 if (lpOutBuffer
&& nOutBufferSize
== sizeof(DWORD
))
261 if (!(status
= get_wait_mask(hDevice
, (DWORD
*)lpOutBuffer
)))
265 status
= STATUS_INVALID_PARAMETER
;
267 case IOCTL_SERIAL_IMMEDIATE_CHAR
:
268 if (lpInBuffer
&& nInBufferSize
== sizeof(CHAR
))
269 status
= xmit_immediate(hDevice
, fd
, lpInBuffer
);
271 status
= STATUS_INVALID_PARAMETER
;
273 case IOCTL_SERIAL_PURGE
:
274 if (lpInBuffer
&& nInBufferSize
== sizeof(DWORD
))
275 status
= purge(fd
, *(DWORD
*)lpInBuffer
);
277 status
= STATUS_INVALID_PARAMETER
;
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();
287 FIXME("ioctl not available\n");
288 status
= STATUS_NOT_SUPPORTED
;
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();
299 FIXME("ioctl not available\n");
300 status
= STATUS_NOT_SUPPORTED
;
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
;
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);
315 status
= STATUS_INVALID_PARAMETER
;
318 wine_server_release_fd( hDevice
, fd
);
320 piosb
->u
.Status
= status
;
321 piosb
->Information
= sz
;
322 if (hEvent
) NtSetEvent(hEvent
, NULL
);