VirtualBox

source: vbox/trunk/src/VBox/Devices/Parallel/DrvHostParallel.cpp@ 40662

Last change on this file since 40662 was 40662, checked in by vboxsync, 13 years ago

to fix the compilation issue on Linux system.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette