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