VirtualBox

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

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

Devices\parallel: Compilation fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 35.2 KB
Line 
1/* $Id: DrvHostParallel.cpp 40666 2012-03-27 14:02:07Z 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); /** @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;'? */
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); /** @todo r=bird: LocalFree(pBuf) was the wrong free function! */
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 (strstr((char*)pBuf, "LPT")) /** @todo Use IPRT equivalent? */
436 {
437 u32ParportAddr = drvHostWinFindIORangeResource(DeviceInfoData.DevInst);
438 if (u32ParportAddr)
439 {
440 /* Find parallel port name and update the shared data struncture */
441 pCh = strstr((char*)pBuf, "(");
442 pTmpCh = strstr((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 /* check for the confirmation for the availability of parallel port */
451 if (!strncpy((char *)(pThis->u8ParportName), pCh+1, ((pTmpCh - (char *)pBuf) -
452 (pCh - (char *)pBuf)) - 1))
453 {
454 LogFlowFunc(("Parallel Port Not Found\n"));
455 return VERR_NOT_FOUND;
456 }
457 *((char *)pThis->u8ParportName + (pTmpCh - (char *)pBuf) - (pCh - (char *)pBuf) + 1 ) = '\0';
458 /* checking again to make sure that we have got a valid name and in valid format too. */
459 if (!strstr((char *)pThis->u8ParportName, "LPT") ||
460 !(pThis->u8ParportName[3] >= '0' && pThis->u8ParportName[3] <= '9')) {
461 pThis->u8ParportName[0] = '\0';
462 LogFlowFunc(("Printer Port Name Not Found\n"));
463 return VERR_NOT_FOUND;
464 }
465 pThis->fParportAvail = true;
466 pThis->u32LptAddr = u32ParportAddr;
467 pThis->u32LptAddrControl = pThis->u32LptAddr + CTRL_REG_OFFSET;
468 pThis->u32LptAddrStatus = pThis->u32LptAddr + STATUS_REG_OFFSET;
469 }
470 if (pThis->fParportAvail)
471 break;
472 }
473 }
474 if (pBuf)
475 RTMemFree(pBuf);
476 if (pThis->fParportAvail)
477 {
478 /* Parallel port address has been found. No need to iterate further. */
479 break;
480 }
481 }
482
483 if (GetLastError() != NO_ERROR && GetLastError() != ERROR_NO_MORE_ITEMS)
484 rc = VERR_GENERAL_FAILURE;
485
486 SetupDiDestroyDeviceInfoList(hDevInfo);
487 return rc;
488
489}
490# endif /* VBOX_WITH_WIN_PARPORT_SUP */
491
492/**
493 * Changes the current mode of the host parallel port.
494 *
495 * @returns VBox status code.
496 * @param pThis The host parallel port instance data.
497 * @param enmMode The mode to change the port to.
498 */
499static int drvHostParallelSetMode(PDRVHOSTPARALLEL pThis, PDMPARALLELPORTMODE enmMode)
500{
501 int iMode = 0;
502 int rc = VINF_SUCCESS;
503 LogFlowFunc(("mode=%d\n", enmMode));
504
505# ifndef VBOX_WITH_WIN_PARPORT_SUP
506 int rcLnx;
507 if (pThis->enmModeCur != enmMode)
508 {
509 switch (enmMode)
510 {
511 case PDM_PARALLEL_PORT_MODE_SPP:
512 iMode = IEEE1284_MODE_COMPAT;
513 break;
514 case PDM_PARALLEL_PORT_MODE_EPP_DATA:
515 iMode = IEEE1284_MODE_EPP | IEEE1284_DATA;
516 break;
517 case PDM_PARALLEL_PORT_MODE_EPP_ADDR:
518 iMode = IEEE1284_MODE_EPP | IEEE1284_ADDR;
519 break;
520 case PDM_PARALLEL_PORT_MODE_ECP:
521 case PDM_PARALLEL_PORT_MODE_INVALID:
522 default:
523 return VERR_NOT_SUPPORTED;
524 }
525
526 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPSETMODE, &iMode);
527 if (RT_UNLIKELY(rcLnx < 0))
528 rc = RTErrConvertFromErrno(errno);
529 else
530 pThis->enmModeCur = enmMode;
531 }
532
533 return rc;
534# else /* VBOX_WITH_WIN_PARPORT_SUP */
535 return VINF_SUCCESS;
536# endif /* VBOX_WITH_WIN_PARPORT_SUP */
537}
538
539/* -=-=-=-=- IBase -=-=-=-=- */
540
541/**
542 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
543 */
544static DECLCALLBACK(void *) drvHostParallelQueryInterface(PPDMIBASE pInterface, const char *pszIID)
545{
546 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
547 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
548
549 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
550 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTPARALLELCONNECTOR, &pThis->CTX_SUFF(IHostParallelConnector));
551 return NULL;
552}
553
554
555/* -=-=-=-=- IHostDeviceConnector -=-=-=-=- */
556
557/** @copydoc PDMICHARCONNECTOR::pfnWrite */
558static DECLCALLBACK(int) drvHostParallelWrite(PPDMIHOSTPARALLELCONNECTOR pInterface, const void *pvBuf, size_t cbWrite, PDMPARALLELPORTMODE enmMode)
559{
560 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
561 //PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
562 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
563 int rc = VINF_SUCCESS;
564 int rcLnx = 0;
565
566 LogFlowFunc(("pvBuf=%#p cbWrite=%d\n", pvBuf, cbWrite));
567
568 rc = drvHostParallelSetMode(pThis, enmMode);
569 if (RT_FAILURE(rc))
570 return rc;
571# ifndef VBOX_WITH_WIN_PARPORT_SUP
572 if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
573 {
574 /* Set the data lines directly. */
575 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
576 }
577 else
578 {
579 /* Use write interface. */
580 rcLnx = write(RTFileToNative(pThis->hFileDevice), pvBuf, cbWrite);
581 }
582 if (RT_UNLIKELY(rcLnx < 0))
583 rc = RTErrConvertFromErrno(errno);
584# else /* VBOX_WITH_WIN_PARPORT_SUP */
585 /** @todo r=klaus this code assumes cbWrite==1, which may not be guaranteed forever */
586 uint64_t u64Data;
587 u64Data = (uint8_t) *((uint8_t *)(pvBuf));
588 LogFlowFunc(("calling R0 to write to parallel port, data=%#x\n", u64Data));
589 if (pThis->fParportAvail)
590 {
591 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITE, u64Data);
592 AssertRC(rc);
593 }
594# endif /* VBOX_WITH_WIN_PARPORT_SUP */
595 return rc;
596}
597
598/**
599 * @interface_method_impl{PDMIBASE,pfnRead}
600 */
601static DECLCALLBACK(int) drvHostParallelRead(PPDMIHOSTPARALLELCONNECTOR pInterface, void *pvBuf, size_t cbRead, PDMPARALLELPORTMODE enmMode)
602{
603 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
604 int rc = VINF_SUCCESS;
605
606# ifndef VBOX_WITH_WIN_PARPORT_SUP
607 int rcLnx = 0;
608 LogFlowFunc(("pvBuf=%#p cbRead=%d\n", pvBuf, cbRead));
609
610 rc = drvHostParallelSetMode(pThis, enmMode);
611 if (RT_FAILURE(rc))
612 return rc;
613
614 if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
615 {
616 /* Set the data lines directly. */
617 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
618 }
619 else
620 {
621 /* Use write interface. */
622 rcLnx = read(RTFileToNative(pThis->hFileDevice), pvBuf, cbRead);
623 }
624 if (RT_UNLIKELY(rcLnx < 0))
625 rc = RTErrConvertFromErrno(errno);
626# else /* VBOX_WITH_WIN_PARPORT_SUP */
627 /** @todo r=klaus this code assumes cbRead==1, which may not be guaranteed forever */
628 *((uint8_t*)(pvBuf)) = 0; /* Initialize the buffer. */
629 LogFlowFunc(("calling R0 to read from parallel port\n"));
630 if (pThis->fParportAvail)
631 {
632 int rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READ, 0);
633 AssertRC(rc);
634 *(uint8_t *)pvBuf = (uint8_t)pThis->u8ReadIn;
635 }
636# endif /* VBOX_WITH_WIN_PARPORT_SUP */
637 return rc;
638}
639
640static DECLCALLBACK(int) drvHostParallelSetPortDirection(PPDMIHOSTPARALLELCONNECTOR pInterface, bool fForward)
641{
642 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
643 int rc = VINF_SUCCESS;
644 int iMode = 0;
645 if (!fForward)
646 iMode = 1;
647# ifndef VBOX_WITH_WIN_PARPORT_SUP
648 int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPDATADIR, &iMode);
649 if (RT_UNLIKELY(rcLnx < 0))
650 rc = RTErrConvertFromErrno(errno);
651
652# else /* VBOX_WITH_WIN_PARPORT_SUP */
653 uint64_t u64Data;
654 u64Data = (uint8_t)iMode;
655 LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", u64Data));
656 if (pThis->fParportAvail)
657 {
658 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_SETPORTDIRECTION, u64Data);
659 AssertRC(rc);
660 }
661# endif /* VBOX_WITH_WIN_PARPORT_SUP */
662 return rc;
663}
664
665/**
666 * @interface_method_impl{PDMIBASE,pfnWriteControl}
667 */
668static DECLCALLBACK(int) drvHostParallelWriteControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t fReg)
669{
670 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
671 int rc = VINF_SUCCESS;
672 int rcLnx = 0;
673
674 LogFlowFunc(("fReg=%#x\n", fReg));
675# ifndef VBOX_WITH_WIN_PARPORT_SUP
676 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWCONTROL, &fReg);
677 if (RT_UNLIKELY(rcLnx < 0))
678 rc = RTErrConvertFromErrno(errno);
679# else /* VBOX_WITH_WIN_PARPORT_SUP */
680 uint64_t u64Data;
681 u64Data = (uint8_t)fReg;
682 LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", u64Data));
683 if (pThis->fParportAvail)
684 {
685 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITECONTROL, u64Data);
686 AssertRC(rc);
687 }
688# endif /* VBOX_WITH_WIN_PARPORT_SUP */
689 return rc;
690}
691
692
693/**
694 * @interface_method_impl{PDMIBASE,pfnReadControl}
695 */
696static DECLCALLBACK(int) drvHostParallelReadControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
697{
698 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
699 int rc = VINF_SUCCESS;
700 int rcLnx = 0;
701 uint8_t fReg = 0;
702
703# ifndef VBOX_WITH_WIN_PARPORT_SUP
704 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRCONTROL, &fReg);
705 if (RT_UNLIKELY(rcLnx < 0))
706 rc = RTErrConvertFromErrno(errno);
707 else
708 {
709 LogFlowFunc(("fReg=%#x\n", fReg));
710 *pfReg = fReg;
711 }
712# else /* VBOX_WITH_WIN_PARPORT_SUP */
713 *pfReg = 0; /* Initialize the buffer*/
714 if (pThis->fParportAvail)
715 {
716 LogFlowFunc(("calling R0 to read control from parallel port\n"));
717 rc = PDMDrvHlpCallR0(pThis-> CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READCONTROL, 0);
718 AssertRC(rc);
719 *pfReg = pThis->u8ReadInControl;
720 }
721# endif /* VBOX_WITH_WIN_PARPORT_SUP */
722 return rc;
723}
724
725/**
726 * @interface_method_impl{PDMIBASE,pfnReadStatus}
727 */
728static DECLCALLBACK(int) drvHostParallelReadStatus(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
729{
730 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
731 int rc = VINF_SUCCESS;
732 int rcLnx = 0;
733 uint8_t fReg = 0;
734# ifndef VBOX_WITH_WIN_PARPORT_SUP
735 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRSTATUS, &fReg);
736 if (RT_UNLIKELY(rcLnx < 0))
737 rc = RTErrConvertFromErrno(errno);
738 else
739 {
740 LogFlowFunc(("fReg=%#x\n", fReg));
741 *pfReg = fReg;
742 }
743# else /* VBOX_WITH_WIN_PARPORT_SUP */
744 *pfReg = 0; /* Intialize the buffer. */
745 if (pThis->fParportAvail)
746 {
747 LogFlowFunc(("calling R0 to read status from parallel port\n"));
748 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READSTATUS, 0);
749 AssertRC(rc);
750 *pfReg = pThis->u8ReadInStatus;
751 }
752# endif /* VBOX_WITH_WIN_PARPORT_SUP */
753 return rc;
754}
755
756# ifndef VBOX_WITH_WIN_PARPORT_SUP
757
758static DECLCALLBACK(int) drvHostParallelMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
759{
760 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
761 struct pollfd aFDs[2];
762
763 /*
764 * We can wait for interrupts using poll on linux hosts.
765 */
766 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
767 {
768 int rc;
769
770 aFDs[0].fd = RTFileToNative(pThis->hFileDevice);
771 aFDs[0].events = POLLIN;
772 aFDs[0].revents = 0;
773 aFDs[1].fd = RTPipeToNative(pThis->hWakeupPipeR);
774 aFDs[1].events = POLLIN | POLLERR | POLLHUP;
775 aFDs[1].revents = 0;
776 rc = poll(aFDs, RT_ELEMENTS(aFDs), -1);
777 if (rc < 0)
778 {
779 AssertMsgFailed(("poll failed with rc=%d\n", RTErrConvertFromErrno(errno)));
780 return RTErrConvertFromErrno(errno);
781 }
782
783 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
784 break;
785 if (rc > 0 && aFDs[1].revents)
786 {
787 if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
788 break;
789 /* notification to terminate -- drain the pipe */
790 char ch;
791 size_t cbRead;
792 RTPipeRead(pThis->hWakeupPipeR, &ch, 1, &cbRead);
793 continue;
794 }
795
796 /* Interrupt occurred. */
797 rc = pThis->pDrvHostParallelPort->pfnNotifyInterrupt(pThis->pDrvHostParallelPort);
798 AssertRC(rc);
799 }
800
801 return VINF_SUCCESS;
802}
803
804/**
805 * Unblock the monitor thread so it can respond to a state change.
806 *
807 * @returns a VBox status code.
808 * @param pDrvIns The driver instance.
809 * @param pThread The send thread.
810 */
811static DECLCALLBACK(int) drvHostParallelWakeupMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
812{
813 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
814 size_t cbIgnored;
815 return RTPipeWrite(pThis->hWakeupPipeW, "", 1, &cbIgnored);
816}
817
818# endif /* VBOX_WITH_WIN_PARPORT_SUP */
819
820/**
821 * Destruct a host parallel driver instance.
822 *
823 * Most VM resources are freed by the VM. This callback is provided so that
824 * any non-VM resources can be freed correctly.
825 *
826 * @param pDrvIns The driver instance data.
827 */
828static DECLCALLBACK(void) drvHostParallelDestruct(PPDMDRVINS pDrvIns)
829{
830 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
831 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
832 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
833
834#ifndef VBOX_WITH_WIN_PARPORT_SUP
835
836 int rc;
837
838 if (pThis->hFileDevice != NIL_RTFILE)
839 ioctl(RTFileToNative(pThis->hFileDevice), PPRELEASE);
840
841 rc = RTPipeClose(pThis->hWakeupPipeW); AssertRC(rc);
842 pThis->hWakeupPipeW = NIL_RTPIPE;
843
844 rc = RTPipeClose(pThis->hWakeupPipeR); AssertRC(rc);
845 pThis->hWakeupPipeR = NIL_RTPIPE;
846
847 rc = RTFileClose(pThis->hFileDevice); AssertRC(rc); /** @todo r=bird: Why aren't this closed on Windows? */
848 pThis->hFileDevice = NIL_RTFILE;
849
850 if (pThis->pszDevicePath)
851 {
852 MMR3HeapFree(pThis->pszDevicePath);
853 pThis->pszDevicePath = NULL;
854 }
855#endif /* VBOX_WITH_WIN_PARPORT_SUP */
856}
857
858/**
859 * Construct a host parallel driver instance.
860 *
861 * @copydoc FNPDMDRVCONSTRUCT
862 */
863static DECLCALLBACK(int) drvHostParallelConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
864{
865 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
866 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
867
868 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
869
870 /*
871 * Init basic data members and interfaces.
872 *
873 * Must be done before returning any failure because we've got a destructor.
874 */
875 pThis->hFileDevice = NIL_RTFILE;
876#ifndef VBOX_WITH_WIN_PARPORT_SUP
877 pThis->hWakeupPipeR = NIL_RTPIPE;
878 pThis->hWakeupPipeW = NIL_RTPIPE;
879#endif
880
881 pThis->pDrvInsR3 = pDrvIns;
882#ifdef VBOX_WITH_DRVINTNET_IN_R0
883 pThis->pDrvInsR0 = PDMDRVINS_2_R0PTR(pDrvIns);
884#endif
885
886 /* IBase. */
887 pDrvIns->IBase.pfnQueryInterface = drvHostParallelQueryInterface;
888 /* IHostParallelConnector. */
889 pThis->IHostParallelConnectorR3.pfnWrite = drvHostParallelWrite;
890 pThis->IHostParallelConnectorR3.pfnRead = drvHostParallelRead;
891 pThis->IHostParallelConnectorR3.pfnSetPortDirection = drvHostParallelSetPortDirection;
892 pThis->IHostParallelConnectorR3.pfnWriteControl = drvHostParallelWriteControl;
893 pThis->IHostParallelConnectorR3.pfnReadControl = drvHostParallelReadControl;
894 pThis->IHostParallelConnectorR3.pfnReadStatus = drvHostParallelReadStatus;
895
896 /*
897 * Validate the config.
898 */
899 if (!CFGMR3AreValuesValid(pCfg, "DevicePath\0"))
900 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
901 N_("Unknown host parallel configuration option, only supports DevicePath"));
902
903 /*
904 * Query configuration.
905 */
906 /* Device */
907 int rc = CFGMR3QueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
908 if (RT_FAILURE(rc))
909 {
910 AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
911 return rc;
912 }
913
914 /*
915 * Open the device
916 */
917 rc = RTFileOpen(&pThis->hFileDevice, pThis->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
918 if (RT_FAILURE(rc))
919 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Parallel#%d could not open '%s'"),
920 pDrvIns->iInstance, pThis->pszDevicePath);
921
922#ifndef VBOX_WITH_WIN_PARPORT_SUP
923 /*
924 * Try to get exclusive access to parallel port
925 */
926 rc = ioctl(RTFileToNative(pThis->hFileDevice), PPEXCL);
927 if (rc < 0)
928 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
929 N_("Parallel#%d could not get exclusive access for parallel port '%s'"
930 "Be sure that no other process or driver accesses this port"),
931 pDrvIns->iInstance, pThis->pszDevicePath);
932
933 /*
934 * Claim the parallel port
935 */
936 rc = ioctl(RTFileToNative(pThis->hFileDevice), PPCLAIM);
937 if (rc < 0)
938 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
939 N_("Parallel#%d could not claim parallel port '%s'"
940 "Be sure that no other process or driver accesses this port"),
941 pDrvIns->iInstance, pThis->pszDevicePath);
942
943 /*
944 * Get the IHostParallelPort interface of the above driver/device.
945 */
946 pThis->pDrvHostParallelPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTPARALLELPORT);
947 if (!pThis->pDrvHostParallelPort)
948 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Parallel#%d has no parallel port interface above"),
949 pDrvIns->iInstance);
950
951 /*
952 * Create wakeup pipe.
953 */
954 rc = RTPipeCreate(&pThis->hWakeupPipeR, &pThis->hWakeupPipeW, 0 /*fFlags*/);
955 AssertRCReturn(rc, rc);
956
957 /*
958 * Start in SPP mode.
959 */
960 pThis->enmModeCur = PDM_PARALLEL_PORT_MODE_INVALID;
961 rc = drvHostParallelSetMode(pThis, PDM_PARALLEL_PORT_MODE_SPP);
962 if (RT_FAILURE(rc))
963 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot change mode of parallel mode to SPP"), pDrvIns->iInstance);
964
965 /*
966 * Start waiting for interrupts.
967 */
968 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pMonitorThread, pThis, drvHostParallelMonitorThread, drvHostParallelWakeupMonitorThread, 0,
969 RTTHREADTYPE_IO, "ParMon");
970 if (RT_FAILURE(rc))
971 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot create monitor thread"), pDrvIns->iInstance);
972
973#else /* VBOX_WITH_WIN_PARPORT_SUP */
974 HANDLE hPort;
975 pThis->fParportAvail = false;
976 pThis->u32LptAddr = 0L;
977 pThis->u32LptAddrControl = 0L;
978 pThis->u32LptAddrStatus = 0L;
979 rc = drvWinHostGetparportAddr(pThis);
980
981 /* If we have the char port availabe use it , else I am not getting exclusive access to parallel port.
982 Read and write will be done only if addresses are availabel
983 */
984 if (pThis->u8ParportName) {
985 LogFlowFunc(("Get the Handle to Printer Port =%s\n", (char *)pThis->u8ParportName));
986 /** @todo r=klaus convert to IPRT */
987 hPort = CreateFile((char *)pThis->u8ParportName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
988 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
989 }
990 /** @todo amakkar: handle the case if hPort is NULL */
991# if 0
992 if (hPort == INVALID_HANDLE_VALUE)
993 {
994 LogFlow(("Failed to get exclusive access to parallel port\n"));
995 rc = VERR_INVALID_HANDLE;
996 }*/
997# endif /* VBOX_WITH_WIN_PARPORT_SUP */
998#endif
999 return VINF_SUCCESS;
1000}
1001
1002
1003/**
1004 * Char driver registration record.
1005 */
1006const PDMDRVREG g_DrvHostParallel =
1007{
1008 /* u32Version */
1009 PDM_DRVREG_VERSION,
1010 /* szName */
1011 "HostParallel",
1012 /* szRCMod */
1013 "",
1014 /* szR0Mod */
1015 "VBoxDDR0.r0",
1016 /* pszDescription */
1017 "Parallel host driver.",
1018 /* fFlags */
1019# if defined(VBOX_WITH_WIN_PARPORT_SUP)
1020 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DRVREG_FLAGS_R0,
1021# else
1022 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1023# endif
1024 /* fClass. */
1025 PDM_DRVREG_CLASS_CHAR,
1026 /* cMaxInstances */
1027 ~0U,
1028 /* cbInstance */
1029 sizeof(DRVHOSTPARALLEL),
1030 /* pfnConstruct */
1031 drvHostParallelConstruct,
1032 /* pfnDestruct */
1033 drvHostParallelDestruct,
1034 /* pfnRelocate */
1035 NULL,
1036 /* pfnIOCtl */
1037 NULL,
1038 /* pfnPowerOn */
1039 NULL,
1040 /* pfnReset */
1041 NULL,
1042 /* pfnSuspend */
1043 NULL,
1044 /* pfnResume */
1045 NULL,
1046 /* pfnAttach */
1047 NULL,
1048 /* pfnDetach */
1049 NULL,
1050 /* pfnPowerOff */
1051 NULL,
1052 /* pfnSoftReset */
1053 NULL,
1054 /* u32EndVersion */
1055 PDM_DRVREG_VERSION
1056};
1057#endif /*IN_RING3*/
1058
1059
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