VirtualBox

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

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

Devices/Parallel: DrvHostParallel.cpp fixed compilation errors on Windows.

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

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