VirtualBox

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

Last change on this file since 40754 was 40707, checked in by vboxsync, 12 years ago

Device\Parallel: Convert string functions to iprt ones. Introduced additional checks for detecting LPT port.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use