1 | /* $Id: DrvHostParallel.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Host Parallel Port Driver.
|
---|
4 | *
|
---|
5 | * Initial Linux-only code contributed by: Alexander Eichner
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2024 Oracle and/or its affiliates.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox base platform packages, as
|
---|
12 | * available from https://www.virtualbox.org.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or
|
---|
15 | * modify it under the terms of the GNU General Public License
|
---|
16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
17 | * License.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful, but
|
---|
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | * General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | *
|
---|
27 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
28 | */
|
---|
29 |
|
---|
30 |
|
---|
31 | /*********************************************************************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *********************************************************************************************************************************/
|
---|
34 | #define LOG_GROUP LOG_GROUP_DRV_HOST_PARALLEL
|
---|
35 | #include <VBox/vmm/pdmdrv.h>
|
---|
36 | #include <VBox/vmm/pdmthread.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/file.h>
|
---|
40 | #include <iprt/pipe.h>
|
---|
41 | #include <iprt/semaphore.h>
|
---|
42 | #include <iprt/stream.h>
|
---|
43 | #include <iprt/uuid.h>
|
---|
44 | #include <iprt/cdefs.h>
|
---|
45 | #include <iprt/ctype.h>
|
---|
46 |
|
---|
47 | #ifdef RT_OS_LINUX
|
---|
48 | # include <sys/ioctl.h>
|
---|
49 | # include <sys/types.h>
|
---|
50 | # include <sys/stat.h>
|
---|
51 | # include <sys/poll.h>
|
---|
52 | # include <fcntl.h>
|
---|
53 | # include <unistd.h>
|
---|
54 | # include <linux/ppdev.h>
|
---|
55 | # include <linux/parport.h>
|
---|
56 | # include <errno.h>
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | /** @def VBOX_WITH_WIN_PARPORT_SUP
|
---|
60 | * Indicates whether to use the generic direct hardware access or host specific
|
---|
61 | * code to access the parallel port.
|
---|
62 | */
|
---|
63 | #if defined(DOXYGEN_RUNNING)
|
---|
64 | # define VBOX_WITH_WIN_PARPORT_SUP
|
---|
65 | #endif
|
---|
66 | #if defined(RT_OS_LINUX)
|
---|
67 | # undef VBOX_WITH_WIN_PARPORT_SUP
|
---|
68 | #elif defined(RT_OS_WINDOWS)
|
---|
69 | #else
|
---|
70 | # error "Not ported"
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | #if defined(VBOX_WITH_WIN_PARPORT_SUP) && defined(IN_RING0)
|
---|
74 | # include <iprt/asm-amd64-x86.h>
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | #if defined(VBOX_WITH_WIN_PARPORT_SUP) && defined(IN_RING3)
|
---|
78 | # include <iprt/win/windows.h>
|
---|
79 | # include <iprt/win/setupapi.h>
|
---|
80 | # include <cfgmgr32.h>
|
---|
81 | # include <iprt/mem.h>
|
---|
82 | # include <iprt/ctype.h>
|
---|
83 | # include <iprt/path.h>
|
---|
84 | # include <iprt/string.h>
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #include "VBoxDD.h"
|
---|
88 |
|
---|
89 |
|
---|
90 | /*********************************************************************************************************************************
|
---|
91 | * Structures and Typedefs *
|
---|
92 | *********************************************************************************************************************************/
|
---|
93 | /**
|
---|
94 | * Host parallel port driver instance data.
|
---|
95 | * @implements PDMIHOSTPARALLELCONNECTOR
|
---|
96 | */
|
---|
97 | typedef struct DRVHOSTPARALLEL
|
---|
98 | {
|
---|
99 | /** Pointer to the driver instance structure. */
|
---|
100 | PPDMDRVINS pDrvIns;
|
---|
101 | /** Pointer to the driver instance. */
|
---|
102 | PPDMDRVINSR3 pDrvInsR3;
|
---|
103 | PPDMDRVINSR0 pDrvInsR0;
|
---|
104 | /** Pointer to the char port interface of the driver/device above us. */
|
---|
105 | PPDMIHOSTPARALLELPORT pDrvHostParallelPort;
|
---|
106 | /** Our host device interface. */
|
---|
107 | PDMIHOSTPARALLELCONNECTOR IHostParallelConnector;
|
---|
108 | /** Our host device interface. */
|
---|
109 | PDMIHOSTPARALLELCONNECTOR IHostParallelConnectorR3;
|
---|
110 | /** Device Path */
|
---|
111 | char *pszDevicePath;
|
---|
112 | /** Device Handle */
|
---|
113 | RTFILE hFileDevice;
|
---|
114 | #ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
115 | /** Thread waiting for interrupts. */
|
---|
116 | PPDMTHREAD pMonitorThread;
|
---|
117 | /** Wakeup pipe read end. */
|
---|
118 | RTPIPE hWakeupPipeR;
|
---|
119 | /** Wakeup pipe write end. */
|
---|
120 | RTPIPE hWakeupPipeW;
|
---|
121 | /** Current mode the parallel port is in. */
|
---|
122 | PDMPARALLELPORTMODE enmModeCur;
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | #ifdef VBOX_WITH_WIN_PARPORT_SUP
|
---|
126 | /** Data register. */
|
---|
127 | RTIOPORT PortDirectData;
|
---|
128 | /** Status register. */
|
---|
129 | RTIOPORT PortDirectStatus;
|
---|
130 | /** Control register. */
|
---|
131 | RTIOPORT PortDirectControl;
|
---|
132 | /** Control read result buffer. */
|
---|
133 | uint8_t bReadInControl;
|
---|
134 | /** Status read result buffer. */
|
---|
135 | uint8_t bReadInStatus;
|
---|
136 | /** Data buffer for reads and writes. */
|
---|
137 | uint8_t abDataBuf[32];
|
---|
138 | #endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
139 | } DRVHOSTPARALLEL, *PDRVHOSTPARALLEL;
|
---|
140 |
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Ring-0 operations.
|
---|
144 | */
|
---|
145 | typedef enum DRVHOSTPARALLELR0OP
|
---|
146 | {
|
---|
147 | /** Invalid zero value. */
|
---|
148 | DRVHOSTPARALLELR0OP_INVALID = 0,
|
---|
149 | /** Perform R0 initialization. */
|
---|
150 | DRVHOSTPARALLELR0OP_INITR0STUFF,
|
---|
151 | /** Read data into the data buffer (abDataBuf). */
|
---|
152 | DRVHOSTPARALLELR0OP_READ,
|
---|
153 | /** Read status register. */
|
---|
154 | DRVHOSTPARALLELR0OP_READSTATUS,
|
---|
155 | /** Read control register. */
|
---|
156 | DRVHOSTPARALLELR0OP_READCONTROL,
|
---|
157 | /** Write data from the data buffer (abDataBuf). */
|
---|
158 | DRVHOSTPARALLELR0OP_WRITE,
|
---|
159 | /** Write control register. */
|
---|
160 | DRVHOSTPARALLELR0OP_WRITECONTROL,
|
---|
161 | /** Set port direction. */
|
---|
162 | DRVHOSTPARALLELR0OP_SETPORTDIRECTION
|
---|
163 | } DRVHOSTPARALLELR0OP;
|
---|
164 |
|
---|
165 | /** Converts a pointer to DRVHOSTPARALLEL::IHostDeviceConnector to a PDRHOSTPARALLEL. */
|
---|
166 | #define PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface) ( (PDRVHOSTPARALLEL)((uintptr_t)pInterface - RT_UOFFSETOF(DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector))) )
|
---|
167 |
|
---|
168 |
|
---|
169 | /*********************************************************************************************************************************
|
---|
170 | * Defined Constants And Macros *
|
---|
171 | *********************************************************************************************************************************/
|
---|
172 | #define CTRL_REG_OFFSET 2
|
---|
173 | #define STATUS_REG_OFFSET 1
|
---|
174 | #define LPT_CONTROL_ENABLE_BIDIRECT 0x20
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 | #ifdef VBOX_WITH_WIN_PARPORT_SUP
|
---|
179 | # ifdef IN_RING0
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * R0 mode function to write byte value to data port.
|
---|
183 | *
|
---|
184 | * @returns VBox status code.
|
---|
185 | * @param pThis Pointer to the instance data.
|
---|
186 | * @param u64Arg The number of bytes to write (from abDataBuf).
|
---|
187 | */
|
---|
188 | static int drvR0HostParallelReqWrite(PDRVHOSTPARALLEL pThis, uint64_t u64Arg)
|
---|
189 | {
|
---|
190 | LogFlowFunc(("write %#RX64 bytes to data (%#x)\n", u64Arg, pThis->PortDirectData));
|
---|
191 |
|
---|
192 | AssertReturn(u64Arg > 0 && u64Arg <= sizeof(pThis->abDataBuf), VERR_OUT_OF_RANGE);
|
---|
193 | uint8_t const *pbSrc = pThis->abDataBuf;
|
---|
194 | while (u64Arg-- > 0)
|
---|
195 | {
|
---|
196 | ASMOutU8(pThis->PortDirectData, *pbSrc);
|
---|
197 | pbSrc++;
|
---|
198 | }
|
---|
199 |
|
---|
200 | return VINF_SUCCESS;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * R0 mode function to write byte value to parallel port control register.
|
---|
205 | *
|
---|
206 | * @returns VBox status code.
|
---|
207 | * @param pThis Pointer to the instance data.
|
---|
208 | * @param u64Arg Data to be written to control register.
|
---|
209 | */
|
---|
210 | static int drvR0HostParallelReqWriteControl(PDRVHOSTPARALLEL pThis, uint64_t u64Arg)
|
---|
211 | {
|
---|
212 | LogFlowFunc(("write to ctrl port=%#x val=%#x\n", pThis->PortDirectControl, u64Arg));
|
---|
213 | ASMOutU8(pThis->PortDirectControl, (uint8_t)(u64Arg));
|
---|
214 | return VINF_SUCCESS;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * R0 mode function to ready byte value from the parallel port data register.
|
---|
219 | *
|
---|
220 | * @returns VBox status code.
|
---|
221 | * @param pThis Pointer to the instance data.
|
---|
222 | * @param u64Arg The number of bytes to read into abDataBuf.
|
---|
223 | */
|
---|
224 | static int drvR0HostParallelReqRead(PDRVHOSTPARALLEL pThis, uint64_t u64Arg)
|
---|
225 | {
|
---|
226 | LogFlowFunc(("read %#RX64 bytes to data (%#x)\n", u64Arg, pThis->PortDirectData));
|
---|
227 |
|
---|
228 | AssertReturn(u64Arg > 0 && u64Arg <= sizeof(pThis->abDataBuf), VERR_OUT_OF_RANGE);
|
---|
229 | uint8_t *pbDst = pThis->abDataBuf;
|
---|
230 | while (u64Arg-- > 0)
|
---|
231 | *pbDst++ = ASMInU8(pThis->PortDirectData);
|
---|
232 |
|
---|
233 | return VINF_SUCCESS;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * R0 mode function to ready byte value from the parallel port control register.
|
---|
238 | *
|
---|
239 | * @returns VBox status code.
|
---|
240 | * @param pThis Pointer to the instance data.
|
---|
241 | */
|
---|
242 | static int drvR0HostParallelReqReadControl(PDRVHOSTPARALLEL pThis)
|
---|
243 | {
|
---|
244 | uint8_t u8Data = ASMInU8(pThis->PortDirectControl);
|
---|
245 | LogFlowFunc(("read from ctrl port=%#x val=%#x\n", pThis->PortDirectControl, u8Data));
|
---|
246 | pThis->bReadInControl = u8Data;
|
---|
247 | return VINF_SUCCESS;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * R0 mode function to ready byte value from the parallel port status register.
|
---|
252 | *
|
---|
253 | * @returns VBox status code.
|
---|
254 | * @param pThis Pointer to the instance data.
|
---|
255 | */
|
---|
256 | static int drvR0HostParallelReqReadStatus(PDRVHOSTPARALLEL pThis)
|
---|
257 | {
|
---|
258 | uint8_t u8Data = ASMInU8(pThis->PortDirectStatus);
|
---|
259 | LogFlowFunc(("read from status port=%#x val=%#x\n", pThis->PortDirectStatus, u8Data));
|
---|
260 | pThis->bReadInStatus = u8Data;
|
---|
261 | return VINF_SUCCESS;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * R0 mode function to set the direction of parallel port -
|
---|
266 | * operate in bidirectional mode or single direction.
|
---|
267 | *
|
---|
268 | * @returns VBox status code.
|
---|
269 | * @param pThis Pointer to the instance data.
|
---|
270 | * @param u64Arg Mode.
|
---|
271 | */
|
---|
272 | static int drvR0HostParallelReqSetPortDir(PDRVHOSTPARALLEL pThis, uint64_t u64Arg)
|
---|
273 | {
|
---|
274 | uint8_t bCtl = ASMInU8(pThis->PortDirectControl);
|
---|
275 | if (u64Arg)
|
---|
276 | bCtl |= LPT_CONTROL_ENABLE_BIDIRECT; /* enable input direction */
|
---|
277 | else
|
---|
278 | bCtl &= ~LPT_CONTROL_ENABLE_BIDIRECT; /* disable input direction */
|
---|
279 | ASMOutU8(pThis->PortDirectControl, bCtl);
|
---|
280 |
|
---|
281 | return VINF_SUCCESS;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * @callback_method_impl{FNPDMDRVREQHANDLERR0}
|
---|
286 | */
|
---|
287 | PDMBOTHCBDECL(int) drvR0HostParallelReqHandler(PPDMDRVINS pDrvIns, uint32_t uOperation, uint64_t u64Arg)
|
---|
288 | {
|
---|
289 | PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
|
---|
290 | int rc;
|
---|
291 | LogFlowFuncEnter();
|
---|
292 |
|
---|
293 | if (pThis->PortDirectData != 0)
|
---|
294 | {
|
---|
295 | switch ((DRVHOSTPARALLELR0OP)uOperation)
|
---|
296 | {
|
---|
297 | case DRVHOSTPARALLELR0OP_READ:
|
---|
298 | rc = drvR0HostParallelReqRead(pThis, u64Arg);
|
---|
299 | break;
|
---|
300 | case DRVHOSTPARALLELR0OP_READSTATUS:
|
---|
301 | rc = drvR0HostParallelReqReadStatus(pThis);
|
---|
302 | break;
|
---|
303 | case DRVHOSTPARALLELR0OP_READCONTROL:
|
---|
304 | rc = drvR0HostParallelReqReadControl(pThis);
|
---|
305 | break;
|
---|
306 | case DRVHOSTPARALLELR0OP_WRITE:
|
---|
307 | rc = drvR0HostParallelReqWrite(pThis, u64Arg);
|
---|
308 | break;
|
---|
309 | case DRVHOSTPARALLELR0OP_WRITECONTROL:
|
---|
310 | rc = drvR0HostParallelReqWriteControl(pThis, u64Arg);
|
---|
311 | break;
|
---|
312 | case DRVHOSTPARALLELR0OP_SETPORTDIRECTION:
|
---|
313 | rc = drvR0HostParallelReqSetPortDir(pThis, u64Arg);
|
---|
314 | break;
|
---|
315 | default:
|
---|
316 | rc = VERR_INVALID_FUNCTION;
|
---|
317 | break;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | else
|
---|
321 | rc = VERR_WRONG_ORDER;
|
---|
322 |
|
---|
323 | LogFlowFuncLeaveRC(rc);
|
---|
324 | return rc;
|
---|
325 | }
|
---|
326 |
|
---|
327 | # endif /* IN_RING0 */
|
---|
328 | #endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
329 |
|
---|
330 | #ifdef IN_RING3
|
---|
331 | # ifdef VBOX_WITH_WIN_PARPORT_SUP
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Find IO port range for the parallel port and return the lower address.
|
---|
335 | *
|
---|
336 | * @returns Parallel base I/O port.
|
---|
337 | * @param DevInst Device instance (dword/handle) for the parallel port.
|
---|
338 | */
|
---|
339 | static RTIOPORT drvHostParallelGetWinHostIoPortsSub(const DEVINST DevInst)
|
---|
340 | {
|
---|
341 | RTIOPORT PortBase = 0;
|
---|
342 |
|
---|
343 | /* Get handle of the first logical configuration. */
|
---|
344 | LOG_CONF hFirstLogConf;
|
---|
345 | CONFIGRET rcCm = CM_Get_First_Log_Conf(&hFirstLogConf, DevInst, ALLOC_LOG_CONF);
|
---|
346 | if (rcCm != CR_SUCCESS)
|
---|
347 | rcCm = CM_Get_First_Log_Conf(&hFirstLogConf, DevInst, BOOT_LOG_CONF);
|
---|
348 | if (rcCm == CR_SUCCESS)
|
---|
349 | {
|
---|
350 | /*
|
---|
351 | * This loop is based on the "fact" that only one I/O resource is assigned
|
---|
352 | * to the LPT port. Should there ever be multiple resources, we'll pick
|
---|
353 | * the last one for some silly reason.
|
---|
354 | */
|
---|
355 |
|
---|
356 | /* Get the first resource descriptor handle. */
|
---|
357 | LOG_CONF hCurLogConf = 0;
|
---|
358 | rcCm = CM_Get_Next_Res_Des(&hCurLogConf, hFirstLogConf, ResType_IO, 0, 0);
|
---|
359 | if (rcCm == CR_SUCCESS)
|
---|
360 | {
|
---|
361 | for (;;)
|
---|
362 | {
|
---|
363 | ULONG cbData;
|
---|
364 | rcCm = CM_Get_Res_Des_Data_Size(&cbData, hCurLogConf, 0);
|
---|
365 | if (rcCm != CR_SUCCESS)
|
---|
366 | cbData = 0;
|
---|
367 | cbData = RT_MAX(cbData, sizeof(IO_DES));
|
---|
368 | IO_DES *pIoDesc = (IO_DES *)RTMemAllocZ(cbData);
|
---|
369 | if (pIoDesc)
|
---|
370 | {
|
---|
371 | rcCm = CM_Get_Res_Des_Data(hCurLogConf, pIoDesc, cbData, 0L);
|
---|
372 | if (rcCm == CR_SUCCESS)
|
---|
373 | {
|
---|
374 | LogRel(("drvHostParallelGetWinHostIoPortsSub: Count=%#u Type=%#x Base=%#RX64 End=%#RX64 Flags=%#x\n",
|
---|
375 | pIoDesc->IOD_Count, pIoDesc->IOD_Type, (uint64_t)pIoDesc->IOD_Alloc_Base,
|
---|
376 | (uint64_t)pIoDesc->IOD_Alloc_End, pIoDesc->IOD_DesFlags));
|
---|
377 | PortBase = (RTIOPORT)pIoDesc->IOD_Alloc_Base;
|
---|
378 | }
|
---|
379 | else
|
---|
380 | LogRel(("drvHostParallelGetWinHostIoPortsSub: CM_Get_Res_Des_Data(,,%u,0) failed: %u\n", cbData, rcCm));
|
---|
381 | RTMemFree(pIoDesc);
|
---|
382 | }
|
---|
383 | else
|
---|
384 | LogRel(("drvHostParallelGetWinHostIoPortsSub: failed to allocate %#x bytes\n", cbData));
|
---|
385 |
|
---|
386 | /* Next */
|
---|
387 | RES_DES hFreeResDesc = hCurLogConf;
|
---|
388 | rcCm = CM_Get_Next_Res_Des(&hCurLogConf, hCurLogConf, ResType_IO, 0, 0);
|
---|
389 | CM_Free_Res_Des_Handle(hFreeResDesc);
|
---|
390 | if (rcCm != CR_SUCCESS)
|
---|
391 | {
|
---|
392 | if (rcCm != CR_NO_MORE_RES_DES)
|
---|
393 | LogRel(("drvHostParallelGetWinHostIoPortsSub: CM_Get_Next_Res_Des failed: %u\n", rcCm));
|
---|
394 | break;
|
---|
395 | }
|
---|
396 | }
|
---|
397 | }
|
---|
398 | else
|
---|
399 | LogRel(("drvHostParallelGetWinHostIoPortsSub: Initial CM_Get_Next_Res_Des failed: %u\n", rcCm));
|
---|
400 | CM_Free_Log_Conf_Handle(hFirstLogConf);
|
---|
401 | }
|
---|
402 | LogFlowFunc(("return PortBase=%#x", PortBase));
|
---|
403 | return PortBase;
|
---|
404 | }
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Get Parallel port address and update the shared data structure.
|
---|
408 | *
|
---|
409 | * @returns VBox status code.
|
---|
410 | * @param pThis The host parallel port instance data.
|
---|
411 | */
|
---|
412 | static int drvHostParallelGetWinHostIoPorts(PDRVHOSTPARALLEL pThis)
|
---|
413 | {
|
---|
414 | /*
|
---|
415 | * Assume the host device path is on the form "\\.\PIPE\LPT1", then get the "LPT1" part.
|
---|
416 | */
|
---|
417 | const char * const pszCfgPortName = RTPathFilename(pThis->pszDevicePath);
|
---|
418 | AssertReturn(pszCfgPortName, VERR_INTERNAL_ERROR_3);
|
---|
419 | size_t const cchCfgPortName = strlen(pszCfgPortName);
|
---|
420 | if ( cchCfgPortName != 4
|
---|
421 | || RTStrNICmp(pszCfgPortName, "LPT", 3) != 0
|
---|
422 | || !RT_C_IS_DIGIT(pszCfgPortName[3]) )
|
---|
423 | {
|
---|
424 | LogRel(("drvHostParallelGetWinHostIoPorts: The configured device name '%s' is not on the expected 'LPTx' form!\n",
|
---|
425 | pszCfgPortName));
|
---|
426 | return VERR_INVALID_NAME;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /*
|
---|
430 | * Get a list of devices then enumerate it looking for the LPT port we're using.
|
---|
431 | */
|
---|
432 | HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
|
---|
433 | if (hDevInfo == INVALID_HANDLE_VALUE)
|
---|
434 | {
|
---|
435 | DWORD dwErr = GetLastError();
|
---|
436 | LogRel(("drvHostParallelGetWinHostIoPorts: SetupDiGetClassDevs failed: %u\n", dwErr));
|
---|
437 | return RTErrConvertFromWin32(dwErr);
|
---|
438 | }
|
---|
439 |
|
---|
440 | int rc = VINF_SUCCESS;
|
---|
441 | char *pszBuf = NULL;
|
---|
442 | DWORD cbBuf = 0;
|
---|
443 | for (int32_t idxDevInfo = 0;; idxDevInfo++)
|
---|
444 | {
|
---|
445 | /*
|
---|
446 | * Query the next device info.
|
---|
447 | */
|
---|
448 | SP_DEVINFO_DATA DeviceInfoData;
|
---|
449 | DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
|
---|
450 | if (!SetupDiEnumDeviceInfo(hDevInfo, idxDevInfo, &DeviceInfoData))
|
---|
451 | {
|
---|
452 | DWORD dwErr = GetLastError();
|
---|
453 | if (dwErr != ERROR_NO_MORE_ITEMS && dwErr != NO_ERROR)
|
---|
454 | {
|
---|
455 | LogRel(("drvHostParallelGetWinHostIoPorts: SetupDiEnumDeviceInfo failed: %u\n", dwErr));
|
---|
456 | rc = RTErrConvertFromWin32(dwErr);
|
---|
457 | }
|
---|
458 | break;
|
---|
459 | }
|
---|
460 |
|
---|
461 | /* Get the friendly name of the device. */
|
---|
462 | DWORD dwDataType;
|
---|
463 | DWORD cbBufActual;
|
---|
464 | BOOL fOk;
|
---|
465 | while (!(fOk = SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME,
|
---|
466 | &dwDataType, (PBYTE)pszBuf, cbBuf, &cbBufActual)))
|
---|
467 | {
|
---|
468 | DWORD dwErr = GetLastError();
|
---|
469 | if (dwErr == ERROR_INSUFFICIENT_BUFFER)
|
---|
470 | {
|
---|
471 | LogFlow(("ERROR_INSUFF_BUFF = %d. dwBufSz = %d\n", dwErr, cbBuf));
|
---|
472 | cbBuf = RT_MAX(RT_ALIGN_Z(cbBufActual + 16, 64), 256);
|
---|
473 | void *pvNew = RTMemRealloc(pszBuf, cbBuf);
|
---|
474 | if (pvNew)
|
---|
475 | pszBuf = (char *)pvNew;
|
---|
476 | else
|
---|
477 | {
|
---|
478 | LogFlow(("GetDevProp Error = %d & cbBufActual = %d\n", dwErr, cbBufActual));
|
---|
479 | break;
|
---|
480 | }
|
---|
481 | }
|
---|
482 | else
|
---|
483 | {
|
---|
484 | /* No need to bother about this error (in most cases its errno=13,
|
---|
485 | * INVALID_DATA . Just break from here and proceed to next device
|
---|
486 | * enumerated item
|
---|
487 | */
|
---|
488 | LogFlow(("GetDevProp Error = %d & cbBufActual = %d\n", dwErr, cbBufActual));
|
---|
489 | break;
|
---|
490 | }
|
---|
491 | }
|
---|
492 | if ( fOk
|
---|
493 | && pszBuf)
|
---|
494 | {
|
---|
495 | pszBuf[cbBuf - 1] = '\0';
|
---|
496 |
|
---|
497 | /*
|
---|
498 | * Does this look like the port name we're looking for.
|
---|
499 | *
|
---|
500 | * We're expecting either "Parallel Port (LPT1)" or just "LPT1", though we'll make do
|
---|
501 | * with anything that includes the name we're looking for as a separate word.
|
---|
502 | */
|
---|
503 | char *pszMatch;
|
---|
504 | do
|
---|
505 | pszMatch = RTStrIStr(pszBuf, pszCfgPortName);
|
---|
506 | while ( pszMatch != NULL
|
---|
507 | && !( ( pszMatch == pszBuf
|
---|
508 | || pszMatch[-1] == '('
|
---|
509 | || RT_C_IS_BLANK(pszMatch[-1]))
|
---|
510 | && ( pszMatch[cchCfgPortName] == '\0'
|
---|
511 | || pszMatch[cchCfgPortName] == ')'
|
---|
512 | || RT_C_IS_BLANK(pszMatch[cchCfgPortName])) ) );
|
---|
513 | if (pszMatch != NULL)
|
---|
514 | {
|
---|
515 | RTIOPORT Port = drvHostParallelGetWinHostIoPortsSub(DeviceInfoData.DevInst);
|
---|
516 | if (Port != 0)
|
---|
517 | {
|
---|
518 | pThis->PortDirectData = Port;
|
---|
519 | pThis->PortDirectControl = Port + CTRL_REG_OFFSET;
|
---|
520 | pThis->PortDirectStatus = Port + STATUS_REG_OFFSET;
|
---|
521 | break;
|
---|
522 | }
|
---|
523 | LogRel(("drvHostParallelGetWinHostIoPorts: Addr not found for '%s'\n", pszBuf));
|
---|
524 | }
|
---|
525 | }
|
---|
526 | }
|
---|
527 |
|
---|
528 | /* Cleanup. */
|
---|
529 | RTMemFree(pszBuf);
|
---|
530 | SetupDiDestroyDeviceInfoList(hDevInfo);
|
---|
531 | return rc;
|
---|
532 |
|
---|
533 | }
|
---|
534 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Changes the current mode of the host parallel port.
|
---|
538 | *
|
---|
539 | * @returns VBox status code.
|
---|
540 | * @param pThis The host parallel port instance data.
|
---|
541 | * @param enmMode The mode to change the port to.
|
---|
542 | */
|
---|
543 | static int drvHostParallelSetMode(PDRVHOSTPARALLEL pThis, PDMPARALLELPORTMODE enmMode)
|
---|
544 | {
|
---|
545 | LogFlowFunc(("mode=%d\n", enmMode));
|
---|
546 | # ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
547 | int rc = VINF_SUCCESS;
|
---|
548 | int iMode = 0;
|
---|
549 | int rcLnx;
|
---|
550 | if (pThis->enmModeCur != enmMode)
|
---|
551 | {
|
---|
552 | switch (enmMode)
|
---|
553 | {
|
---|
554 | case PDM_PARALLEL_PORT_MODE_SPP:
|
---|
555 | iMode = IEEE1284_MODE_COMPAT;
|
---|
556 | break;
|
---|
557 | case PDM_PARALLEL_PORT_MODE_EPP_DATA:
|
---|
558 | iMode = IEEE1284_MODE_EPP | IEEE1284_DATA;
|
---|
559 | break;
|
---|
560 | case PDM_PARALLEL_PORT_MODE_EPP_ADDR:
|
---|
561 | iMode = IEEE1284_MODE_EPP | IEEE1284_ADDR;
|
---|
562 | break;
|
---|
563 | case PDM_PARALLEL_PORT_MODE_ECP:
|
---|
564 | case PDM_PARALLEL_PORT_MODE_INVALID:
|
---|
565 | default:
|
---|
566 | return VERR_NOT_SUPPORTED;
|
---|
567 | }
|
---|
568 |
|
---|
569 | rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPSETMODE, &iMode);
|
---|
570 | if (RT_UNLIKELY(rcLnx < 0))
|
---|
571 | rc = RTErrConvertFromErrno(errno);
|
---|
572 | else
|
---|
573 | pThis->enmModeCur = enmMode;
|
---|
574 | }
|
---|
575 |
|
---|
576 | return rc;
|
---|
577 | # else /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
578 | RT_NOREF(pThis, enmMode);
|
---|
579 | return VINF_SUCCESS;
|
---|
580 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
581 | }
|
---|
582 |
|
---|
583 | /* -=-=-=-=- IBase -=-=-=-=- */
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
587 | */
|
---|
588 | static DECLCALLBACK(void *) drvHostParallelQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
589 | {
|
---|
590 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
591 | PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
|
---|
592 |
|
---|
593 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
594 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTPARALLELCONNECTOR, &pThis->CTX_SUFF(IHostParallelConnector));
|
---|
595 | return NULL;
|
---|
596 | }
|
---|
597 |
|
---|
598 |
|
---|
599 | /* -=-=-=-=- IHostDeviceConnector -=-=-=-=- */
|
---|
600 |
|
---|
601 | /**
|
---|
602 | * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnWrite}
|
---|
603 | */
|
---|
604 | static DECLCALLBACK(int)
|
---|
605 | drvHostParallelWrite(PPDMIHOSTPARALLELCONNECTOR pInterface, const void *pvBuf, size_t cbWrite, PDMPARALLELPORTMODE enmMode)
|
---|
606 | {
|
---|
607 | PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
|
---|
608 | int rc = VINF_SUCCESS;
|
---|
609 |
|
---|
610 | LogFlowFunc(("pvBuf=%#p cbWrite=%d\n", pvBuf, cbWrite));
|
---|
611 |
|
---|
612 | rc = drvHostParallelSetMode(pThis, enmMode);
|
---|
613 | if (RT_FAILURE(rc))
|
---|
614 | return rc;
|
---|
615 | # ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
616 | int rcLnx = 0;
|
---|
617 | if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
|
---|
618 | {
|
---|
619 | /* Set the data lines directly. */
|
---|
620 | rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
|
---|
621 | }
|
---|
622 | else
|
---|
623 | {
|
---|
624 | /* Use write interface. */
|
---|
625 | rcLnx = write(RTFileToNative(pThis->hFileDevice), pvBuf, cbWrite);
|
---|
626 | }
|
---|
627 | if (RT_UNLIKELY(rcLnx < 0))
|
---|
628 | rc = RTErrConvertFromErrno(errno);
|
---|
629 |
|
---|
630 | # else /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
631 | if (pThis->PortDirectData != 0)
|
---|
632 | {
|
---|
633 | while (cbWrite > 0)
|
---|
634 | {
|
---|
635 | size_t cbToWrite = RT_MIN(cbWrite, sizeof(pThis->abDataBuf));
|
---|
636 | LogFlowFunc(("Calling R0 to write %#zu bytes of data\n", cbToWrite));
|
---|
637 | memcpy(pThis->abDataBuf, pvBuf, cbToWrite);
|
---|
638 | rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITE, cbToWrite);
|
---|
639 | AssertRC(rc);
|
---|
640 | pvBuf = (uint8_t const *)pvBuf + cbToWrite;
|
---|
641 | cbWrite -= cbToWrite;
|
---|
642 | }
|
---|
643 | }
|
---|
644 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
645 | return rc;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnRead}
|
---|
650 | */
|
---|
651 | static DECLCALLBACK(int)
|
---|
652 | drvHostParallelRead(PPDMIHOSTPARALLELCONNECTOR pInterface, void *pvBuf, size_t cbRead, PDMPARALLELPORTMODE enmMode)
|
---|
653 | {
|
---|
654 | PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
|
---|
655 | int rc = VINF_SUCCESS;
|
---|
656 |
|
---|
657 | # ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
658 | int rcLnx = 0;
|
---|
659 | LogFlowFunc(("pvBuf=%#p cbRead=%d\n", pvBuf, cbRead));
|
---|
660 |
|
---|
661 | rc = drvHostParallelSetMode(pThis, enmMode);
|
---|
662 | if (RT_FAILURE(rc))
|
---|
663 | return rc;
|
---|
664 |
|
---|
665 | if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
|
---|
666 | {
|
---|
667 | /* Set the data lines directly. */
|
---|
668 | rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
|
---|
669 | }
|
---|
670 | else
|
---|
671 | {
|
---|
672 | /* Use write interface. */
|
---|
673 | rcLnx = read(RTFileToNative(pThis->hFileDevice), pvBuf, cbRead);
|
---|
674 | }
|
---|
675 | if (RT_UNLIKELY(rcLnx < 0))
|
---|
676 | rc = RTErrConvertFromErrno(errno);
|
---|
677 |
|
---|
678 | # else /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
679 | RT_NOREF(enmMode);
|
---|
680 | if (pThis->PortDirectData != 0)
|
---|
681 | {
|
---|
682 | while (cbRead > 0)
|
---|
683 | {
|
---|
684 | size_t cbToRead = RT_MIN(cbRead, sizeof(pThis->abDataBuf));
|
---|
685 | LogFlowFunc(("Calling R0 to read %#zu bytes of data\n", cbToRead));
|
---|
686 | memset(pThis->abDataBuf, 0, cbToRead);
|
---|
687 | rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READ, cbToRead);
|
---|
688 | AssertRC(rc);
|
---|
689 | memcpy(pvBuf, pThis->abDataBuf, cbToRead);
|
---|
690 | pvBuf = (uint8_t *)pvBuf + cbToRead;
|
---|
691 | cbRead -= cbToRead;
|
---|
692 | }
|
---|
693 | }
|
---|
694 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
695 | return rc;
|
---|
696 | }
|
---|
697 |
|
---|
698 | /**
|
---|
699 | * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnSetPortDirection}
|
---|
700 | */
|
---|
701 | static DECLCALLBACK(int) drvHostParallelSetPortDirection(PPDMIHOSTPARALLELCONNECTOR pInterface, bool fForward)
|
---|
702 | {
|
---|
703 | PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
|
---|
704 | int rc = VINF_SUCCESS;
|
---|
705 | int iMode = 0;
|
---|
706 | if (!fForward)
|
---|
707 | iMode = 1;
|
---|
708 | # ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
709 | int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPDATADIR, &iMode);
|
---|
710 | if (RT_UNLIKELY(rcLnx < 0))
|
---|
711 | rc = RTErrConvertFromErrno(errno);
|
---|
712 |
|
---|
713 | # else /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
714 | if (pThis->PortDirectData != 0)
|
---|
715 | {
|
---|
716 | LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", iMode));
|
---|
717 | rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_SETPORTDIRECTION, iMode);
|
---|
718 | AssertRC(rc);
|
---|
719 | }
|
---|
720 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
721 | return rc;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnWriteControl}
|
---|
726 | */
|
---|
727 | static DECLCALLBACK(int) drvHostParallelWriteControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t fReg)
|
---|
728 | {
|
---|
729 | PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
|
---|
730 | int rc = VINF_SUCCESS;
|
---|
731 |
|
---|
732 | LogFlowFunc(("fReg=%#x\n", fReg));
|
---|
733 | # ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
734 | int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWCONTROL, &fReg);
|
---|
735 | if (RT_UNLIKELY(rcLnx < 0))
|
---|
736 | rc = RTErrConvertFromErrno(errno);
|
---|
737 | # else /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
738 | if (pThis->PortDirectData != 0)
|
---|
739 | {
|
---|
740 | LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", fReg));
|
---|
741 | rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITECONTROL, fReg);
|
---|
742 | AssertRC(rc);
|
---|
743 | }
|
---|
744 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
745 | return rc;
|
---|
746 | }
|
---|
747 |
|
---|
748 |
|
---|
749 | /**
|
---|
750 | * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnReadControl}
|
---|
751 | */
|
---|
752 | static DECLCALLBACK(int) drvHostParallelReadControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
|
---|
753 | {
|
---|
754 | PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
|
---|
755 | int rc = VINF_SUCCESS;
|
---|
756 |
|
---|
757 | # ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
758 | uint8_t fReg = 0;
|
---|
759 | int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRCONTROL, &fReg);
|
---|
760 | if (RT_UNLIKELY(rcLnx < 0))
|
---|
761 | rc = RTErrConvertFromErrno(errno);
|
---|
762 | else
|
---|
763 | {
|
---|
764 | LogFlowFunc(("fReg=%#x\n", fReg));
|
---|
765 | *pfReg = fReg;
|
---|
766 | }
|
---|
767 | # else /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
768 | *pfReg = 0; /* Initialize the buffer*/
|
---|
769 | if (pThis->PortDirectData != 0)
|
---|
770 | {
|
---|
771 | LogFlowFunc(("calling R0 to read control from parallel port\n"));
|
---|
772 | rc = PDMDrvHlpCallR0(pThis-> CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READCONTROL, 0);
|
---|
773 | AssertRC(rc);
|
---|
774 | *pfReg = pThis->bReadInControl;
|
---|
775 | }
|
---|
776 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
777 | return rc;
|
---|
778 | }
|
---|
779 |
|
---|
780 | /**
|
---|
781 | * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnReadStatus}
|
---|
782 | */
|
---|
783 | static DECLCALLBACK(int) drvHostParallelReadStatus(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
|
---|
784 | {
|
---|
785 | PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
|
---|
786 | int rc = VINF_SUCCESS;
|
---|
787 | # ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
788 | uint8_t fReg = 0;
|
---|
789 | int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRSTATUS, &fReg);
|
---|
790 | if (RT_UNLIKELY(rcLnx < 0))
|
---|
791 | rc = RTErrConvertFromErrno(errno);
|
---|
792 | else
|
---|
793 | {
|
---|
794 | LogFlowFunc(("fReg=%#x\n", fReg));
|
---|
795 | *pfReg = fReg;
|
---|
796 | }
|
---|
797 | # else /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
798 | *pfReg = 0; /* Intialize the buffer. */
|
---|
799 | if (pThis->PortDirectData != 0)
|
---|
800 | {
|
---|
801 | LogFlowFunc(("calling R0 to read status from parallel port\n"));
|
---|
802 | rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READSTATUS, 0);
|
---|
803 | AssertRC(rc);
|
---|
804 | *pfReg = pThis->bReadInStatus;
|
---|
805 | }
|
---|
806 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
807 | return rc;
|
---|
808 | }
|
---|
809 |
|
---|
810 | # ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
811 |
|
---|
812 | static DECLCALLBACK(int) drvHostParallelMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
813 | {
|
---|
814 | PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
|
---|
815 | struct pollfd aFDs[2];
|
---|
816 |
|
---|
817 | /*
|
---|
818 | * We can wait for interrupts using poll on linux hosts.
|
---|
819 | */
|
---|
820 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
821 | {
|
---|
822 | int rc;
|
---|
823 |
|
---|
824 | aFDs[0].fd = RTFileToNative(pThis->hFileDevice);
|
---|
825 | aFDs[0].events = POLLIN;
|
---|
826 | aFDs[0].revents = 0;
|
---|
827 | aFDs[1].fd = RTPipeToNative(pThis->hWakeupPipeR);
|
---|
828 | aFDs[1].events = POLLIN | POLLERR | POLLHUP;
|
---|
829 | aFDs[1].revents = 0;
|
---|
830 | rc = poll(aFDs, RT_ELEMENTS(aFDs), -1);
|
---|
831 | if (rc < 0)
|
---|
832 | {
|
---|
833 | AssertMsgFailed(("poll failed with rc=%d\n", RTErrConvertFromErrno(errno)));
|
---|
834 | return RTErrConvertFromErrno(errno);
|
---|
835 | }
|
---|
836 |
|
---|
837 | if (pThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
838 | break;
|
---|
839 | if (rc > 0 && aFDs[1].revents)
|
---|
840 | {
|
---|
841 | if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
|
---|
842 | break;
|
---|
843 | /* notification to terminate -- drain the pipe */
|
---|
844 | char ch;
|
---|
845 | size_t cbRead;
|
---|
846 | RTPipeRead(pThis->hWakeupPipeR, &ch, 1, &cbRead);
|
---|
847 | continue;
|
---|
848 | }
|
---|
849 |
|
---|
850 | /* Interrupt occurred. */
|
---|
851 | rc = pThis->pDrvHostParallelPort->pfnNotifyInterrupt(pThis->pDrvHostParallelPort);
|
---|
852 | AssertRC(rc);
|
---|
853 | }
|
---|
854 |
|
---|
855 | return VINF_SUCCESS;
|
---|
856 | }
|
---|
857 |
|
---|
858 | /**
|
---|
859 | * Unblock the monitor thread so it can respond to a state change.
|
---|
860 | *
|
---|
861 | * @returns a VBox status code.
|
---|
862 | * @param pDrvIns The driver instance.
|
---|
863 | * @param pThread The send thread.
|
---|
864 | */
|
---|
865 | static DECLCALLBACK(int) drvHostParallelWakeupMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
866 | {
|
---|
867 | RT_NOREF(pThread);
|
---|
868 | PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
|
---|
869 | size_t cbIgnored;
|
---|
870 | return RTPipeWrite(pThis->hWakeupPipeW, "", 1, &cbIgnored);
|
---|
871 | }
|
---|
872 |
|
---|
873 | # endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
874 |
|
---|
875 | /**
|
---|
876 | * Destruct a host parallel driver instance.
|
---|
877 | *
|
---|
878 | * Most VM resources are freed by the VM. This callback is provided so that
|
---|
879 | * any non-VM resources can be freed correctly.
|
---|
880 | *
|
---|
881 | * @param pDrvIns The driver instance data.
|
---|
882 | */
|
---|
883 | static DECLCALLBACK(void) drvHostParallelDestruct(PPDMDRVINS pDrvIns)
|
---|
884 | {
|
---|
885 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
886 | LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
|
---|
887 |
|
---|
888 | #ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
889 | PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
|
---|
890 | if (pThis->hFileDevice != NIL_RTFILE)
|
---|
891 | ioctl(RTFileToNative(pThis->hFileDevice), PPRELEASE);
|
---|
892 |
|
---|
893 | if (pThis->hWakeupPipeW != NIL_RTPIPE)
|
---|
894 | {
|
---|
895 | int rc = RTPipeClose(pThis->hWakeupPipeW); AssertRC(rc);
|
---|
896 | pThis->hWakeupPipeW = NIL_RTPIPE;
|
---|
897 | }
|
---|
898 |
|
---|
899 | if (pThis->hWakeupPipeR != NIL_RTPIPE)
|
---|
900 | {
|
---|
901 | int rc = RTPipeClose(pThis->hWakeupPipeR); AssertRC(rc);
|
---|
902 | pThis->hWakeupPipeR = NIL_RTPIPE;
|
---|
903 | }
|
---|
904 |
|
---|
905 | if (pThis->hFileDevice != NIL_RTFILE)
|
---|
906 | {
|
---|
907 | int rc = RTFileClose(pThis->hFileDevice); AssertRC(rc);
|
---|
908 | pThis->hFileDevice = NIL_RTFILE;
|
---|
909 | }
|
---|
910 |
|
---|
911 | if (pThis->pszDevicePath)
|
---|
912 | {
|
---|
913 | PDMDrvHlpMMHeapFree(pDrvIns, pThis->pszDevicePath);
|
---|
914 | pThis->pszDevicePath = NULL;
|
---|
915 | }
|
---|
916 | #endif /* !VBOX_WITH_WIN_PARPORT_SUP */
|
---|
917 | }
|
---|
918 |
|
---|
919 | /**
|
---|
920 | * Construct a host parallel driver instance.
|
---|
921 | *
|
---|
922 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
923 | */
|
---|
924 | static DECLCALLBACK(int) drvHostParallelConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
925 | {
|
---|
926 | RT_NOREF(fFlags);
|
---|
927 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
928 | PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
|
---|
929 | PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
|
---|
930 | LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
|
---|
931 |
|
---|
932 |
|
---|
933 | /*
|
---|
934 | * Init basic data members and interfaces.
|
---|
935 | *
|
---|
936 | * Must be done before returning any failure because we've got a destructor.
|
---|
937 | */
|
---|
938 | pThis->hFileDevice = NIL_RTFILE;
|
---|
939 | #ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
940 | pThis->hWakeupPipeR = NIL_RTPIPE;
|
---|
941 | pThis->hWakeupPipeW = NIL_RTPIPE;
|
---|
942 | #endif
|
---|
943 |
|
---|
944 | pThis->pDrvInsR3 = pDrvIns;
|
---|
945 | #ifdef VBOX_WITH_DRVINTNET_IN_R0
|
---|
946 | pThis->pDrvInsR0 = PDMDRVINS_2_R0PTR(pDrvIns);
|
---|
947 | #endif
|
---|
948 |
|
---|
949 | /* IBase. */
|
---|
950 | pDrvIns->IBase.pfnQueryInterface = drvHostParallelQueryInterface;
|
---|
951 | /* IHostParallelConnector. */
|
---|
952 | pThis->IHostParallelConnectorR3.pfnWrite = drvHostParallelWrite;
|
---|
953 | pThis->IHostParallelConnectorR3.pfnRead = drvHostParallelRead;
|
---|
954 | pThis->IHostParallelConnectorR3.pfnSetPortDirection = drvHostParallelSetPortDirection;
|
---|
955 | pThis->IHostParallelConnectorR3.pfnWriteControl = drvHostParallelWriteControl;
|
---|
956 | pThis->IHostParallelConnectorR3.pfnReadControl = drvHostParallelReadControl;
|
---|
957 | pThis->IHostParallelConnectorR3.pfnReadStatus = drvHostParallelReadStatus;
|
---|
958 |
|
---|
959 | /*
|
---|
960 | * Validate the config.
|
---|
961 | */
|
---|
962 | PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "DevicePath", "");
|
---|
963 |
|
---|
964 | /*
|
---|
965 | * Query configuration.
|
---|
966 | */
|
---|
967 | /* Device */
|
---|
968 | int rc = pHlp->pfnCFGMQueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
|
---|
969 | if (RT_FAILURE(rc))
|
---|
970 | {
|
---|
971 | AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
|
---|
972 | return rc;
|
---|
973 | }
|
---|
974 |
|
---|
975 | /*
|
---|
976 | * Open the device
|
---|
977 | */
|
---|
978 | /** @todo exclusive access on windows? */
|
---|
979 | rc = RTFileOpen(&pThis->hFileDevice, pThis->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
980 | if (RT_FAILURE(rc))
|
---|
981 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Parallel#%d could not open '%s'"),
|
---|
982 | pDrvIns->iInstance, pThis->pszDevicePath);
|
---|
983 |
|
---|
984 | #ifndef VBOX_WITH_WIN_PARPORT_SUP
|
---|
985 | /*
|
---|
986 | * Try to get exclusive access to parallel port
|
---|
987 | */
|
---|
988 | rc = ioctl(RTFileToNative(pThis->hFileDevice), PPEXCL);
|
---|
989 | if (rc < 0)
|
---|
990 | return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
|
---|
991 | N_("Parallel#%d could not get exclusive access for parallel port '%s'"
|
---|
992 | "Be sure that no other process or driver accesses this port"),
|
---|
993 | pDrvIns->iInstance, pThis->pszDevicePath);
|
---|
994 |
|
---|
995 | /*
|
---|
996 | * Claim the parallel port
|
---|
997 | */
|
---|
998 | rc = ioctl(RTFileToNative(pThis->hFileDevice), PPCLAIM);
|
---|
999 | if (rc < 0)
|
---|
1000 | return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
|
---|
1001 | N_("Parallel#%d could not claim parallel port '%s'"
|
---|
1002 | "Be sure that no other process or driver accesses this port"),
|
---|
1003 | pDrvIns->iInstance, pThis->pszDevicePath);
|
---|
1004 |
|
---|
1005 | /*
|
---|
1006 | * Get the IHostParallelPort interface of the above driver/device.
|
---|
1007 | */
|
---|
1008 | pThis->pDrvHostParallelPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTPARALLELPORT);
|
---|
1009 | if (!pThis->pDrvHostParallelPort)
|
---|
1010 | return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Parallel#%d has no parallel port interface above"),
|
---|
1011 | pDrvIns->iInstance);
|
---|
1012 |
|
---|
1013 | /*
|
---|
1014 | * Create wakeup pipe.
|
---|
1015 | */
|
---|
1016 | rc = RTPipeCreate(&pThis->hWakeupPipeR, &pThis->hWakeupPipeW, 0 /*fFlags*/);
|
---|
1017 | AssertRCReturn(rc, rc);
|
---|
1018 |
|
---|
1019 | /*
|
---|
1020 | * Start in SPP mode.
|
---|
1021 | */
|
---|
1022 | pThis->enmModeCur = PDM_PARALLEL_PORT_MODE_INVALID;
|
---|
1023 | rc = drvHostParallelSetMode(pThis, PDM_PARALLEL_PORT_MODE_SPP);
|
---|
1024 | if (RT_FAILURE(rc))
|
---|
1025 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot change mode of parallel mode to SPP"), pDrvIns->iInstance);
|
---|
1026 |
|
---|
1027 | /*
|
---|
1028 | * Start waiting for interrupts.
|
---|
1029 | */
|
---|
1030 | rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pMonitorThread, pThis, drvHostParallelMonitorThread, drvHostParallelWakeupMonitorThread, 0,
|
---|
1031 | RTTHREADTYPE_IO, "ParMon");
|
---|
1032 | if (RT_FAILURE(rc))
|
---|
1033 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot create monitor thread"), pDrvIns->iInstance);
|
---|
1034 |
|
---|
1035 | #else /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
1036 |
|
---|
1037 | pThis->PortDirectData = 0;
|
---|
1038 | pThis->PortDirectControl = 0;
|
---|
1039 | pThis->PortDirectStatus = 0;
|
---|
1040 | rc = drvHostParallelGetWinHostIoPorts(pThis);
|
---|
1041 | if (RT_FAILURE(rc))
|
---|
1042 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
1043 | N_("HostParallel#%d: Could not get direct access to the host parallel port!! (rc=%Rrc)"),
|
---|
1044 | pDrvIns->iInstance, rc);
|
---|
1045 |
|
---|
1046 | #endif /* VBOX_WITH_WIN_PARPORT_SUP */
|
---|
1047 | return VINF_SUCCESS;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 |
|
---|
1051 | /**
|
---|
1052 | * Char driver registration record.
|
---|
1053 | */
|
---|
1054 | const PDMDRVREG g_DrvHostParallel =
|
---|
1055 | {
|
---|
1056 | /* u32Version */
|
---|
1057 | PDM_DRVREG_VERSION,
|
---|
1058 | /* szName */
|
---|
1059 | "HostParallel",
|
---|
1060 | /* szRCMod */
|
---|
1061 | "",
|
---|
1062 | /* szR0Mod */
|
---|
1063 | "VBoxDDR0.r0",
|
---|
1064 | /* pszDescription */
|
---|
1065 | "Parallel host driver.",
|
---|
1066 | /* fFlags */
|
---|
1067 | # if defined(VBOX_WITH_WIN_PARPORT_SUP)
|
---|
1068 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DRVREG_FLAGS_R0,
|
---|
1069 | # else
|
---|
1070 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
1071 | # endif
|
---|
1072 | /* fClass. */
|
---|
1073 | PDM_DRVREG_CLASS_CHAR,
|
---|
1074 | /* cMaxInstances */
|
---|
1075 | ~0U,
|
---|
1076 | /* cbInstance */
|
---|
1077 | sizeof(DRVHOSTPARALLEL),
|
---|
1078 | /* pfnConstruct */
|
---|
1079 | drvHostParallelConstruct,
|
---|
1080 | /* pfnDestruct */
|
---|
1081 | drvHostParallelDestruct,
|
---|
1082 | /* pfnRelocate */
|
---|
1083 | NULL,
|
---|
1084 | /* pfnIOCtl */
|
---|
1085 | NULL,
|
---|
1086 | /* pfnPowerOn */
|
---|
1087 | NULL,
|
---|
1088 | /* pfnReset */
|
---|
1089 | NULL,
|
---|
1090 | /* pfnSuspend */
|
---|
1091 | NULL,
|
---|
1092 | /* pfnResume */
|
---|
1093 | NULL,
|
---|
1094 | /* pfnAttach */
|
---|
1095 | NULL,
|
---|
1096 | /* pfnDetach */
|
---|
1097 | NULL,
|
---|
1098 | /* pfnPowerOff */
|
---|
1099 | NULL,
|
---|
1100 | /* pfnSoftReset */
|
---|
1101 | NULL,
|
---|
1102 | /* u32EndVersion */
|
---|
1103 | PDM_DRVREG_VERSION
|
---|
1104 | };
|
---|
1105 | #endif /*IN_RING3*/
|
---|
1106 |
|
---|