VirtualBox

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

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

Devices/Parallel: Addressing some of todos as identified by Knut.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 35.7 KB
Line 
1/* $Id: DrvHostParallel.cpp 42349 2012-07-24 11:11:47Z vboxsync $ */
2/** @file
3 * VirtualBox Host Parallel Port Driver.
4 *
5 * Initial Linux-only code contributed by: Alexander Eichner
6 */
7
8/*
9 * Copyright (C) 2006-2012 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.215389.xyz. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_DRV_HOST_PARALLEL
24#include <VBox/vmm/pdmdrv.h>
25#include <VBox/vmm/pdmthread.h>
26#include <iprt/asm.h>
27#include <iprt/assert.h>
28#include <iprt/file.h>
29#include <iprt/pipe.h>
30#include <iprt/semaphore.h>
31#include <iprt/stream.h>
32#include <iprt/uuid.h>
33#include <iprt/cdefs.h>
34#include <iprt/ctype.h>
35
36#ifdef RT_OS_LINUX
37# include <sys/ioctl.h>
38# include <sys/types.h>
39# include <sys/stat.h>
40# include <sys/poll.h>
41# include <fcntl.h>
42# include <unistd.h>
43# include <linux/ppdev.h>
44# include <linux/parport.h>
45# include <errno.h>
46#endif
47
48
49/** @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 cmRet = CM_Get_Res_Des_Data_Size((PULONG)(&u32Size), nextLogConf, 0L);
358 if (cmRet != CR_SUCCESS)
359 {
360 CM_Free_Res_Des_Handle(nextLogConf); /** @todo r=bird: Why are you doing this twice in this code path? */
361 break;
362 }
363 pBuf = (uint8_t *)RTMemAlloc(u32Size + 1);
364 if (!pBuf)
365 {
366 CM_Free_Res_Des_Handle(nextLogConf); /** @todo r=bird: Ditto above. */
367 break;
368 }
369 cmRet = CM_Get_Res_Des_Data(nextLogConf, pBuf, u32Size, 0L);
370 if (cmRet != CR_SUCCESS)
371 {
372 CM_Free_Res_Des_Handle(nextLogConf);
373 RTMemFree(pBuf);
374 break;
375 }
376 LogFlowFunc(("call GetIOResource\n"));
377 u32ParportAddr = ((IO_DES *)pBuf)->IOD_Alloc_Base;
378 LogFlowFunc(("called GetIOResource, ret=%#x\n", u32ParportAddr));
379 rdPrevResDes = 0;
380 cmRet = CM_Get_Next_Res_Des(&rdPrevResDes,
381 nextLogConf,
382 2,
383 0L,
384 0L);
385 RTMemFree(pBuf);
386 if (cmRet != CR_SUCCESS)
387 break;
388
389 CM_Free_Res_Des_Handle(nextLogConf);
390 nextLogConf = rdPrevResDes;
391 }
392 CM_Free_Res_Des_Handle(nextLogConf);
393 LogFlowFunc(("return u32ParportAddr=%#x", u32ParportAddr));
394 return u32ParportAddr;
395}
396
397/**
398 * Get Parallel port address and update the shared data
399 * structure.
400 * @returns VBox status code.
401 * @param pThis The host parallel port instance data.
402 */
403static int drvWinHostGetparportAddr(PDRVHOSTPARALLEL pThis)
404{
405 HDEVINFO hDevInfo;
406 SP_DEVINFO_DATA DeviceInfoData;
407 uint32_t u32Idx;
408 uint32_t u32ParportAddr;
409 int rc = VINF_SUCCESS;
410
411 hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
412 if (hDevInfo == INVALID_HANDLE_VALUE)
413 return VERR_INVALID_HANDLE;
414
415 /* Enumerate through all devices in Set. */
416 DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
417 for (u32Idx = 0; SetupDiEnumDeviceInfo(hDevInfo, u32Idx, &DeviceInfoData); u32Idx++)
418 {
419 DWORD dwDataType;
420 uint8_t *pBuf = NULL;
421 DWORD dwBufSize = 0;
422
423 while (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME,
424 (PDWORD)&dwDataType, (uint8_t *)pBuf,
425 dwBufSize, (PDWORD)&dwBufSize)
426 && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
427 {
428 if (pBuf)
429 RTMemFree(pBuf);
430 /* Max size will never be more than 2048 bytes */
431 pBuf = (uint8_t *)RTMemAlloc(dwBufSize * 2);
432 }
433
434 if (pBuf) /** @todo r=bird: You're not checking errors here. */
435 {
436 if (RTStrStr((char*)pBuf, "LPT"))
437 {
438 u32ParportAddr = drvHostWinFindIORangeResource(DeviceInfoData.DevInst);
439 if (u32ParportAddr)
440 {
441 /* Find parallel port name and update the shared data struncture */
442 char *pCh = RTStrStr((char*)pBuf, "(");
443 char *pTmpCh = RTStrStr((char *)pBuf, ")");
444 /* check for the confirmation for the availability of parallel port */
445 if (!(pCh && pTmpCh))
446 {
447 LogFlowFunc(("Parallel port Not Found. \n"));
448 return VERR_NOT_FOUND;
449
450 }
451 if (((pTmpCh - (char *)pBuf) - (pCh - (char *)pBuf)) < 0) {
452 LogFlowFunc(("Parallel port string not properly formatted.\n"));
453 return VERR_NOT_FOUND;
454 }
455 /* check for the confirmation for the availability of parallel port */
456 if (RTStrCopyEx((char *)(pThis->szParportName), sizeof(pThis->szParportName),
457 pCh+1, ((pTmpCh - (char *)pBuf) - (pCh - (char *)pBuf)) - 1))
458 {
459 LogFlowFunc(("Parallel Port Not Found.\n"));
460 return VERR_NOT_FOUND;
461 }
462 *((char *)pThis->szParportName + (pTmpCh - (char *)pBuf) - (pCh - (char *)pBuf) + 1 ) = '\0';
463
464 /* checking again to make sure that we have got a valid name and in valid format too. */
465 if (RTStrNCmp((char *)pThis->szParportName, "LPT", 3)) {
466 LogFlowFunc(("Parallel Port name \"LPT\" Not Found.\n"));
467 return VERR_NOT_FOUND;
468 }
469
470 if (!RTStrStr((char *)pThis->szParportName, "LPT")
471 || !(pThis->szParportName[3] >= '0'
472 && pThis->szParportName[3] <= '9'))
473 {
474 RT_BZERO(pThis->szParportName, sizeof(pThis->szParportName));
475 LogFlowFunc(("Printer Port Name Not Found.\n"));
476 return VERR_NOT_FOUND;
477 }
478 pThis->fParportAvail = true;
479 pThis->u32LptAddr = u32ParportAddr;
480 pThis->u32LptAddrControl = pThis->u32LptAddr + CTRL_REG_OFFSET;
481 pThis->u32LptAddrStatus = pThis->u32LptAddr + STATUS_REG_OFFSET;
482 }
483 if (pThis->fParportAvail)
484 break;
485 }
486 }
487 if (pBuf)
488 RTMemFree(pBuf);
489 if (pThis->fParportAvail)
490 {
491 /* Parallel port address has been found. No need to iterate further. */
492 break;
493 }
494 }
495
496 if (GetLastError() != NO_ERROR && GetLastError() != ERROR_NO_MORE_ITEMS)
497 rc = VERR_GENERAL_FAILURE;
498
499 SetupDiDestroyDeviceInfoList(hDevInfo);
500 return rc;
501
502}
503# endif /* VBOX_WITH_WIN_PARPORT_SUP */
504
505/**
506 * Changes the current mode of the host parallel port.
507 *
508 * @returns VBox status code.
509 * @param pThis The host parallel port instance data.
510 * @param enmMode The mode to change the port to.
511 */
512static int drvHostParallelSetMode(PDRVHOSTPARALLEL pThis, PDMPARALLELPORTMODE enmMode)
513{
514 int iMode = 0;
515 int rc = VINF_SUCCESS;
516 LogFlowFunc(("mode=%d\n", enmMode));
517
518# ifndef VBOX_WITH_WIN_PARPORT_SUP
519 int rcLnx;
520 if (pThis->enmModeCur != enmMode)
521 {
522 switch (enmMode)
523 {
524 case PDM_PARALLEL_PORT_MODE_SPP:
525 iMode = IEEE1284_MODE_COMPAT;
526 break;
527 case PDM_PARALLEL_PORT_MODE_EPP_DATA:
528 iMode = IEEE1284_MODE_EPP | IEEE1284_DATA;
529 break;
530 case PDM_PARALLEL_PORT_MODE_EPP_ADDR:
531 iMode = IEEE1284_MODE_EPP | IEEE1284_ADDR;
532 break;
533 case PDM_PARALLEL_PORT_MODE_ECP:
534 case PDM_PARALLEL_PORT_MODE_INVALID:
535 default:
536 return VERR_NOT_SUPPORTED;
537 }
538
539 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPSETMODE, &iMode);
540 if (RT_UNLIKELY(rcLnx < 0))
541 rc = RTErrConvertFromErrno(errno);
542 else
543 pThis->enmModeCur = enmMode;
544 }
545
546 return rc;
547# else /* VBOX_WITH_WIN_PARPORT_SUP */
548 return VINF_SUCCESS;
549# endif /* VBOX_WITH_WIN_PARPORT_SUP */
550}
551
552/* -=-=-=-=- IBase -=-=-=-=- */
553
554/**
555 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
556 */
557static DECLCALLBACK(void *) drvHostParallelQueryInterface(PPDMIBASE pInterface, const char *pszIID)
558{
559 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
560 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
561
562 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
563 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTPARALLELCONNECTOR, &pThis->CTX_SUFF(IHostParallelConnector));
564 return NULL;
565}
566
567
568/* -=-=-=-=- IHostDeviceConnector -=-=-=-=- */
569
570/** @copydoc PDMICHARCONNECTOR::pfnWrite */
571static DECLCALLBACK(int) drvHostParallelWrite(PPDMIHOSTPARALLELCONNECTOR pInterface, const void *pvBuf, size_t cbWrite, PDMPARALLELPORTMODE enmMode)
572{
573 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
574 //PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
575 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
576 int rc = VINF_SUCCESS;
577 int rcLnx = 0;
578
579 LogFlowFunc(("pvBuf=%#p cbWrite=%d\n", pvBuf, cbWrite));
580
581 rc = drvHostParallelSetMode(pThis, enmMode);
582 if (RT_FAILURE(rc))
583 return rc;
584# ifndef VBOX_WITH_WIN_PARPORT_SUP
585 if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
586 {
587 /* Set the data lines directly. */
588 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
589 }
590 else
591 {
592 /* Use write interface. */
593 rcLnx = write(RTFileToNative(pThis->hFileDevice), pvBuf, cbWrite);
594 }
595 if (RT_UNLIKELY(rcLnx < 0))
596 rc = RTErrConvertFromErrno(errno);
597# else /* VBOX_WITH_WIN_PARPORT_SUP */
598 /** @todo r=klaus this code assumes cbWrite==1, which may not be guaranteed forever */
599 uint64_t u64Data;
600 u64Data = (uint8_t) *((uint8_t *)(pvBuf));
601 LogFlowFunc(("calling R0 to write to parallel port, data=%#x\n", u64Data));
602 if (pThis->fParportAvail)
603 {
604 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITE, u64Data);
605 AssertRC(rc);
606 }
607# endif /* VBOX_WITH_WIN_PARPORT_SUP */
608 return rc;
609}
610
611/**
612 * @interface_method_impl{PDMIBASE,pfnRead}
613 */
614static DECLCALLBACK(int) drvHostParallelRead(PPDMIHOSTPARALLELCONNECTOR pInterface, void *pvBuf, size_t cbRead, PDMPARALLELPORTMODE enmMode)
615{
616 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
617 int rc = VINF_SUCCESS;
618
619# ifndef VBOX_WITH_WIN_PARPORT_SUP
620 int rcLnx = 0;
621 LogFlowFunc(("pvBuf=%#p cbRead=%d\n", pvBuf, cbRead));
622
623 rc = drvHostParallelSetMode(pThis, enmMode);
624 if (RT_FAILURE(rc))
625 return rc;
626
627 if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
628 {
629 /* Set the data lines directly. */
630 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
631 }
632 else
633 {
634 /* Use write interface. */
635 rcLnx = read(RTFileToNative(pThis->hFileDevice), pvBuf, cbRead);
636 }
637 if (RT_UNLIKELY(rcLnx < 0))
638 rc = RTErrConvertFromErrno(errno);
639# else /* VBOX_WITH_WIN_PARPORT_SUP */
640 /** @todo r=klaus this code assumes cbRead==1, which may not be guaranteed forever */
641 *((uint8_t*)(pvBuf)) = 0; /* Initialize the buffer. */
642 LogFlowFunc(("calling R0 to read from parallel port\n"));
643 if (pThis->fParportAvail)
644 {
645 int rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READ, 0);
646 AssertRC(rc);
647 *(uint8_t *)pvBuf = (uint8_t)pThis->u8ReadIn;
648 }
649# endif /* VBOX_WITH_WIN_PARPORT_SUP */
650 return rc;
651}
652
653static DECLCALLBACK(int) drvHostParallelSetPortDirection(PPDMIHOSTPARALLELCONNECTOR pInterface, bool fForward)
654{
655 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
656 int rc = VINF_SUCCESS;
657 int iMode = 0;
658 if (!fForward)
659 iMode = 1;
660# ifndef VBOX_WITH_WIN_PARPORT_SUP
661 int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPDATADIR, &iMode);
662 if (RT_UNLIKELY(rcLnx < 0))
663 rc = RTErrConvertFromErrno(errno);
664
665# else /* VBOX_WITH_WIN_PARPORT_SUP */
666 uint64_t u64Data;
667 u64Data = (uint8_t)iMode;
668 LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", u64Data));
669 if (pThis->fParportAvail)
670 {
671 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_SETPORTDIRECTION, u64Data);
672 AssertRC(rc);
673 }
674# endif /* VBOX_WITH_WIN_PARPORT_SUP */
675 return rc;
676}
677
678/**
679 * @interface_method_impl{PDMIBASE,pfnWriteControl}
680 */
681static DECLCALLBACK(int) drvHostParallelWriteControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t fReg)
682{
683 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
684 int rc = VINF_SUCCESS;
685 int rcLnx = 0;
686
687 LogFlowFunc(("fReg=%#x\n", fReg));
688# ifndef VBOX_WITH_WIN_PARPORT_SUP
689 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWCONTROL, &fReg);
690 if (RT_UNLIKELY(rcLnx < 0))
691 rc = RTErrConvertFromErrno(errno);
692# else /* VBOX_WITH_WIN_PARPORT_SUP */
693 uint64_t u64Data;
694 u64Data = (uint8_t)fReg;
695 LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", u64Data));
696 if (pThis->fParportAvail)
697 {
698 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITECONTROL, u64Data);
699 AssertRC(rc);
700 }
701# endif /* VBOX_WITH_WIN_PARPORT_SUP */
702 return rc;
703}
704
705
706/**
707 * @interface_method_impl{PDMIBASE,pfnReadControl}
708 */
709static DECLCALLBACK(int) drvHostParallelReadControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
710{
711 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
712 int rc = VINF_SUCCESS;
713 int rcLnx = 0;
714 uint8_t fReg = 0;
715
716# ifndef VBOX_WITH_WIN_PARPORT_SUP
717 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRCONTROL, &fReg);
718 if (RT_UNLIKELY(rcLnx < 0))
719 rc = RTErrConvertFromErrno(errno);
720 else
721 {
722 LogFlowFunc(("fReg=%#x\n", fReg));
723 *pfReg = fReg;
724 }
725# else /* VBOX_WITH_WIN_PARPORT_SUP */
726 *pfReg = 0; /* Initialize the buffer*/
727 if (pThis->fParportAvail)
728 {
729 LogFlowFunc(("calling R0 to read control from parallel port\n"));
730 rc = PDMDrvHlpCallR0(pThis-> CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READCONTROL, 0);
731 AssertRC(rc);
732 *pfReg = pThis->u8ReadInControl;
733 }
734# endif /* VBOX_WITH_WIN_PARPORT_SUP */
735 return rc;
736}
737
738/**
739 * @interface_method_impl{PDMIBASE,pfnReadStatus}
740 */
741static DECLCALLBACK(int) drvHostParallelReadStatus(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
742{
743 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
744 int rc = VINF_SUCCESS;
745 int rcLnx = 0;
746 uint8_t fReg = 0;
747# ifndef VBOX_WITH_WIN_PARPORT_SUP
748 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRSTATUS, &fReg);
749 if (RT_UNLIKELY(rcLnx < 0))
750 rc = RTErrConvertFromErrno(errno);
751 else
752 {
753 LogFlowFunc(("fReg=%#x\n", fReg));
754 *pfReg = fReg;
755 }
756# else /* VBOX_WITH_WIN_PARPORT_SUP */
757 *pfReg = 0; /* Intialize the buffer. */
758 if (pThis->fParportAvail)
759 {
760 LogFlowFunc(("calling R0 to read status from parallel port\n"));
761 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READSTATUS, 0);
762 AssertRC(rc);
763 *pfReg = pThis->u8ReadInStatus;
764 }
765# endif /* VBOX_WITH_WIN_PARPORT_SUP */
766 return rc;
767}
768
769# ifndef VBOX_WITH_WIN_PARPORT_SUP
770
771static DECLCALLBACK(int) drvHostParallelMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
772{
773 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
774 struct pollfd aFDs[2];
775
776 /*
777 * We can wait for interrupts using poll on linux hosts.
778 */
779 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
780 {
781 int rc;
782
783 aFDs[0].fd = RTFileToNative(pThis->hFileDevice);
784 aFDs[0].events = POLLIN;
785 aFDs[0].revents = 0;
786 aFDs[1].fd = RTPipeToNative(pThis->hWakeupPipeR);
787 aFDs[1].events = POLLIN | POLLERR | POLLHUP;
788 aFDs[1].revents = 0;
789 rc = poll(aFDs, RT_ELEMENTS(aFDs), -1);
790 if (rc < 0)
791 {
792 AssertMsgFailed(("poll failed with rc=%d\n", RTErrConvertFromErrno(errno)));
793 return RTErrConvertFromErrno(errno);
794 }
795
796 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
797 break;
798 if (rc > 0 && aFDs[1].revents)
799 {
800 if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
801 break;
802 /* notification to terminate -- drain the pipe */
803 char ch;
804 size_t cbRead;
805 RTPipeRead(pThis->hWakeupPipeR, &ch, 1, &cbRead);
806 continue;
807 }
808
809 /* Interrupt occurred. */
810 rc = pThis->pDrvHostParallelPort->pfnNotifyInterrupt(pThis->pDrvHostParallelPort);
811 AssertRC(rc);
812 }
813
814 return VINF_SUCCESS;
815}
816
817/**
818 * Unblock the monitor thread so it can respond to a state change.
819 *
820 * @returns a VBox status code.
821 * @param pDrvIns The driver instance.
822 * @param pThread The send thread.
823 */
824static DECLCALLBACK(int) drvHostParallelWakeupMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
825{
826 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
827 size_t cbIgnored;
828 return RTPipeWrite(pThis->hWakeupPipeW, "", 1, &cbIgnored);
829}
830
831# endif /* VBOX_WITH_WIN_PARPORT_SUP */
832
833/**
834 * Destruct a host parallel driver instance.
835 *
836 * Most VM resources are freed by the VM. This callback is provided so that
837 * any non-VM resources can be freed correctly.
838 *
839 * @param pDrvIns The driver instance data.
840 */
841static DECLCALLBACK(void) drvHostParallelDestruct(PPDMDRVINS pDrvIns)
842{
843 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
844 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
845 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
846
847#ifndef VBOX_WITH_WIN_PARPORT_SUP
848
849 int rc;
850
851 if (pThis->hFileDevice != NIL_RTFILE)
852 ioctl(RTFileToNative(pThis->hFileDevice), PPRELEASE);
853
854 rc = RTPipeClose(pThis->hWakeupPipeW); AssertRC(rc);
855 pThis->hWakeupPipeW = NIL_RTPIPE;
856
857 rc = RTPipeClose(pThis->hWakeupPipeR); AssertRC(rc);
858 pThis->hWakeupPipeR = NIL_RTPIPE;
859
860 rc = RTFileClose(pThis->hFileDevice); AssertRC(rc); /** @todo r=bird: Why aren't this closed on Windows? */
861 pThis->hFileDevice = NIL_RTFILE;
862
863 if (pThis->pszDevicePath)
864 {
865 MMR3HeapFree(pThis->pszDevicePath);
866 pThis->pszDevicePath = NULL;
867 }
868#endif /* VBOX_WITH_WIN_PARPORT_SUP */
869}
870
871/**
872 * Construct a host parallel driver instance.
873 *
874 * @copydoc FNPDMDRVCONSTRUCT
875 */
876static DECLCALLBACK(int) drvHostParallelConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
877{
878 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
879 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
880
881 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
882
883 /*
884 * Init basic data members and interfaces.
885 *
886 * Must be done before returning any failure because we've got a destructor.
887 */
888 pThis->hFileDevice = NIL_RTFILE;
889#ifndef VBOX_WITH_WIN_PARPORT_SUP
890 pThis->hWakeupPipeR = NIL_RTPIPE;
891 pThis->hWakeupPipeW = NIL_RTPIPE;
892#endif
893
894 pThis->pDrvInsR3 = pDrvIns;
895#ifdef VBOX_WITH_DRVINTNET_IN_R0
896 pThis->pDrvInsR0 = PDMDRVINS_2_R0PTR(pDrvIns);
897#endif
898
899 /* IBase. */
900 pDrvIns->IBase.pfnQueryInterface = drvHostParallelQueryInterface;
901 /* IHostParallelConnector. */
902 pThis->IHostParallelConnectorR3.pfnWrite = drvHostParallelWrite;
903 pThis->IHostParallelConnectorR3.pfnRead = drvHostParallelRead;
904 pThis->IHostParallelConnectorR3.pfnSetPortDirection = drvHostParallelSetPortDirection;
905 pThis->IHostParallelConnectorR3.pfnWriteControl = drvHostParallelWriteControl;
906 pThis->IHostParallelConnectorR3.pfnReadControl = drvHostParallelReadControl;
907 pThis->IHostParallelConnectorR3.pfnReadStatus = drvHostParallelReadStatus;
908
909 /*
910 * Validate the config.
911 */
912 if (!CFGMR3AreValuesValid(pCfg, "DevicePath\0"))
913 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
914 N_("Unknown host parallel configuration option, only supports DevicePath"));
915
916 /*
917 * Query configuration.
918 */
919 /* Device */
920 int rc = CFGMR3QueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
921 if (RT_FAILURE(rc))
922 {
923 AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
924 return rc;
925 }
926
927 /*
928 * Open the device
929 */
930 rc = RTFileOpen(&pThis->hFileDevice, pThis->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
931 if (RT_FAILURE(rc))
932 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Parallel#%d could not open '%s'"),
933 pDrvIns->iInstance, pThis->pszDevicePath);
934
935#ifndef VBOX_WITH_WIN_PARPORT_SUP
936 /*
937 * Try to get exclusive access to parallel port
938 */
939 rc = ioctl(RTFileToNative(pThis->hFileDevice), PPEXCL);
940 if (rc < 0)
941 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
942 N_("Parallel#%d could not get exclusive access for parallel port '%s'"
943 "Be sure that no other process or driver accesses this port"),
944 pDrvIns->iInstance, pThis->pszDevicePath);
945
946 /*
947 * Claim the parallel port
948 */
949 rc = ioctl(RTFileToNative(pThis->hFileDevice), PPCLAIM);
950 if (rc < 0)
951 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
952 N_("Parallel#%d could not claim parallel port '%s'"
953 "Be sure that no other process or driver accesses this port"),
954 pDrvIns->iInstance, pThis->pszDevicePath);
955
956 /*
957 * Get the IHostParallelPort interface of the above driver/device.
958 */
959 pThis->pDrvHostParallelPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTPARALLELPORT);
960 if (!pThis->pDrvHostParallelPort)
961 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Parallel#%d has no parallel port interface above"),
962 pDrvIns->iInstance);
963
964 /*
965 * Create wakeup pipe.
966 */
967 rc = RTPipeCreate(&pThis->hWakeupPipeR, &pThis->hWakeupPipeW, 0 /*fFlags*/);
968 AssertRCReturn(rc, rc);
969
970 /*
971 * Start in SPP mode.
972 */
973 pThis->enmModeCur = PDM_PARALLEL_PORT_MODE_INVALID;
974 rc = drvHostParallelSetMode(pThis, PDM_PARALLEL_PORT_MODE_SPP);
975 if (RT_FAILURE(rc))
976 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot change mode of parallel mode to SPP"), pDrvIns->iInstance);
977
978 /*
979 * Start waiting for interrupts.
980 */
981 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pMonitorThread, pThis, drvHostParallelMonitorThread, drvHostParallelWakeupMonitorThread, 0,
982 RTTHREADTYPE_IO, "ParMon");
983 if (RT_FAILURE(rc))
984 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot create monitor thread"), pDrvIns->iInstance);
985
986#else /* VBOX_WITH_WIN_PARPORT_SUP */
987 HANDLE hPort;
988 pThis->fParportAvail = false;
989 pThis->u32LptAddr = 0;
990 pThis->u32LptAddrControl = 0;
991 pThis->u32LptAddrStatus = 0;
992 rc = drvWinHostGetparportAddr(pThis);
993
994 /* If we have the char port availabe use it , else I am not getting exclusive access to parallel port.
995 Read and write will be done only if addresses are available
996 */
997 if (pThis->szParportName)
998 {
999 LogFlowFunc(("Get the Handle to Printer Port =%s\n", (char *)pThis->szParportName));
1000 /** @todo r=klaus convert to IPRT */
1001 hPort = CreateFile((char *)pThis->szParportName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
1002 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1003 }
1004 /** @todo amakkar: handle the case if hPort is NULL */
1005# if 0
1006 if (hPort == INVALID_HANDLE_VALUE)
1007 {
1008 LogFlow(("Failed to get exclusive access to parallel port\n"));
1009 rc = VERR_INVALID_HANDLE;
1010 }*/
1011# endif /* VBOX_WITH_WIN_PARPORT_SUP */
1012#endif
1013 return VINF_SUCCESS;
1014}
1015
1016
1017/**
1018 * Char driver registration record.
1019 */
1020const PDMDRVREG g_DrvHostParallel =
1021{
1022 /* u32Version */
1023 PDM_DRVREG_VERSION,
1024 /* szName */
1025 "HostParallel",
1026 /* szRCMod */
1027 "",
1028 /* szR0Mod */
1029 "VBoxDDR0.r0",
1030 /* pszDescription */
1031 "Parallel host driver.",
1032 /* fFlags */
1033# if defined(VBOX_WITH_WIN_PARPORT_SUP)
1034 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DRVREG_FLAGS_R0,
1035# else
1036 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1037# endif
1038 /* fClass. */
1039 PDM_DRVREG_CLASS_CHAR,
1040 /* cMaxInstances */
1041 ~0U,
1042 /* cbInstance */
1043 sizeof(DRVHOSTPARALLEL),
1044 /* pfnConstruct */
1045 drvHostParallelConstruct,
1046 /* pfnDestruct */
1047 drvHostParallelDestruct,
1048 /* pfnRelocate */
1049 NULL,
1050 /* pfnIOCtl */
1051 NULL,
1052 /* pfnPowerOn */
1053 NULL,
1054 /* pfnReset */
1055 NULL,
1056 /* pfnSuspend */
1057 NULL,
1058 /* pfnResume */
1059 NULL,
1060 /* pfnAttach */
1061 NULL,
1062 /* pfnDetach */
1063 NULL,
1064 /* pfnPowerOff */
1065 NULL,
1066 /* pfnSoftReset */
1067 NULL,
1068 /* u32EndVersion */
1069 PDM_DRVREG_VERSION
1070};
1071#endif /*IN_RING3*/
1072
1073
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