VirtualBox

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

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

Devices/Parallel: Reverted the changes related to extra debug statement for debugging (r 80854).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 34.9 KB
Line 
1/* $Id: DrvHostParallel.cpp 43393 2012-09-21 10:54: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/** @def VBOX_WITH_WIN_PARPORT_SUP *
49 * Indicates whether to use the generic direct hardware access or host specific
50 * code to access the parallel port.
51 */
52#if defined(RT_OS_LINUX)
53# undef VBOX_WITH_WIN_PARPORT_SUP
54#elif defined(RT_OS_WINDOWS)
55#else
56# error "Not ported"
57#endif
58
59#if defined(VBOX_WITH_WIN_PARPORT_SUP) && defined(IN_RING0)
60# include <iprt/asm-amd64-x86.h>
61#endif
62
63#if defined(VBOX_WITH_WIN_PARPORT_SUP) && defined(IN_RING3)
64# include <Windows.h>
65# include <setupapi.h>
66# include <cfgmgr32.h>
67# include <iprt/mem.h>
68# include <iprt/string.h>
69#endif
70
71#include "VBoxDD.h"
72
73
74/*******************************************************************************
75* Structures and Typedefs *
76*******************************************************************************/
77/**
78 * Host parallel port driver instance data.
79 * @implements PDMIHOSTPARALLELCONNECTOR
80 */
81typedef struct DRVHOSTPARALLEL
82{
83 /** Pointer to the driver instance structure. */
84 PPDMDRVINS pDrvIns;
85 /** Pointer to the driver instance. */
86 PPDMDRVINSR3 pDrvInsR3;
87 PPDMDRVINSR0 pDrvInsR0;
88 /** Pointer to the char port interface of the driver/device above us. */
89 PPDMIHOSTPARALLELPORT pDrvHostParallelPort;
90 /** Our host device interface. */
91 PDMIHOSTPARALLELCONNECTOR IHostParallelConnector;
92 /** Our host device interface. */
93 PDMIHOSTPARALLELCONNECTOR IHostParallelConnectorR3;
94 /** Device Path */
95 char *pszDevicePath;
96 /** Device Handle */
97 RTFILE hFileDevice;
98#ifndef VBOX_WITH_WIN_PARPORT_SUP
99 /** Thread waiting for interrupts. */
100 PPDMTHREAD pMonitorThread;
101 /** Wakeup pipe read end. */
102 RTPIPE hWakeupPipeR;
103 /** Wakeup pipe write end. */
104 RTPIPE hWakeupPipeW;
105 /** Current mode the parallel port is in. */
106 PDMPARALLELPORTMODE enmModeCur;
107#endif
108
109#ifdef VBOX_WITH_WIN_PARPORT_SUP
110 /** Data register. */
111 uint32_t u32LptAddr;
112 /** Status register. */
113 uint32_t u32LptAddrStatus;
114 /** Control register. */
115 uint32_t u32LptAddrControl;
116 /** Data read buffer. */
117 uint8_t u8ReadIn;
118 /** Control read buffer. */
119 uint8_t u8ReadInControl;
120 /** Status read buffer. */
121 uint8_t u8ReadInStatus;
122 /** Parallel port name */
123 char szParportName[6];
124 /** Whether the parallel port is available or not. */
125 bool fParportAvail;
126 /** Device Handle */
127 RTFILE hWinFileDevice;
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 if (pThis->fParportAvail)
589 {
590 for (size_t i = 0; i < cbWrite; i++)
591 {
592 uint64_t u64Data = (uint8_t) *((uint8_t *)(pvBuf) + i);
593 LogFlowFunc(("calling R0 to write to parallel port, data=%#x\n", u64Data));
594 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITE, u64Data);
595 AssertRC(rc);
596 }
597 }
598# endif /* VBOX_WITH_WIN_PARPORT_SUP */
599 return rc;
600}
601
602/**
603 * @interface_method_impl{PDMIBASE,pfnRead}
604 */
605static DECLCALLBACK(int) drvHostParallelRead(PPDMIHOSTPARALLELCONNECTOR pInterface, void *pvBuf, size_t cbRead, PDMPARALLELPORTMODE enmMode)
606{
607 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
608 int rc = VINF_SUCCESS;
609
610# ifndef VBOX_WITH_WIN_PARPORT_SUP
611 int rcLnx = 0;
612 LogFlowFunc(("pvBuf=%#p cbRead=%d\n", pvBuf, cbRead));
613
614 rc = drvHostParallelSetMode(pThis, enmMode);
615 if (RT_FAILURE(rc))
616 return rc;
617
618 if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
619 {
620 /* Set the data lines directly. */
621 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
622 }
623 else
624 {
625 /* Use write interface. */
626 rcLnx = read(RTFileToNative(pThis->hFileDevice), pvBuf, cbRead);
627 }
628 if (RT_UNLIKELY(rcLnx < 0))
629 rc = RTErrConvertFromErrno(errno);
630# else /* VBOX_WITH_WIN_PARPORT_SUP */
631 if (pThis->fParportAvail)
632 {
633 *((uint8_t*)(pvBuf)) = 0; /* Initialize the buffer. */
634 for (size_t i = 0; i < cbRead; i++)
635 {
636 LogFlowFunc(("calling R0 to read from parallel port\n"));
637 int rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READ, 0);
638 AssertRC(rc);
639 *((uint8_t *)pvBuf + i) = (uint8_t)pThis->u8ReadIn;
640 }
641 }
642# endif /* VBOX_WITH_WIN_PARPORT_SUP */
643 return rc;
644}
645
646static DECLCALLBACK(int) drvHostParallelSetPortDirection(PPDMIHOSTPARALLELCONNECTOR pInterface, bool fForward)
647{
648 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
649 int rc = VINF_SUCCESS;
650 int iMode = 0;
651 if (!fForward)
652 iMode = 1;
653# ifndef VBOX_WITH_WIN_PARPORT_SUP
654 int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPDATADIR, &iMode);
655 if (RT_UNLIKELY(rcLnx < 0))
656 rc = RTErrConvertFromErrno(errno);
657
658# else /* VBOX_WITH_WIN_PARPORT_SUP */
659 uint64_t u64Data;
660 u64Data = (uint8_t)iMode;
661 LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", u64Data));
662 if (pThis->fParportAvail)
663 {
664 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_SETPORTDIRECTION, u64Data);
665 AssertRC(rc);
666 }
667# endif /* VBOX_WITH_WIN_PARPORT_SUP */
668 return rc;
669}
670
671/**
672 * @interface_method_impl{PDMIBASE,pfnWriteControl}
673 */
674static DECLCALLBACK(int) drvHostParallelWriteControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t fReg)
675{
676 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
677 int rc = VINF_SUCCESS;
678 int rcLnx = 0;
679
680 LogFlowFunc(("fReg=%#x\n", fReg));
681# ifndef VBOX_WITH_WIN_PARPORT_SUP
682 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWCONTROL, &fReg);
683 if (RT_UNLIKELY(rcLnx < 0))
684 rc = RTErrConvertFromErrno(errno);
685# else /* VBOX_WITH_WIN_PARPORT_SUP */
686 uint64_t u64Data;
687 u64Data = (uint8_t)fReg;
688 LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", u64Data));
689 if (pThis->fParportAvail)
690 {
691 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITECONTROL, u64Data);
692 AssertRC(rc);
693 }
694# endif /* VBOX_WITH_WIN_PARPORT_SUP */
695 return rc;
696}
697
698
699/**
700 * @interface_method_impl{PDMIBASE,pfnReadControl}
701 */
702static DECLCALLBACK(int) drvHostParallelReadControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
703{
704 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
705 int rc = VINF_SUCCESS;
706 int rcLnx = 0;
707 uint8_t fReg = 0;
708
709# ifndef VBOX_WITH_WIN_PARPORT_SUP
710 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRCONTROL, &fReg);
711 if (RT_UNLIKELY(rcLnx < 0))
712 rc = RTErrConvertFromErrno(errno);
713 else
714 {
715 LogFlowFunc(("fReg=%#x\n", fReg));
716 *pfReg = fReg;
717 }
718# else /* VBOX_WITH_WIN_PARPORT_SUP */
719 *pfReg = 0; /* Initialize the buffer*/
720 if (pThis->fParportAvail)
721 {
722 LogFlowFunc(("calling R0 to read control from parallel port\n"));
723 rc = PDMDrvHlpCallR0(pThis-> CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READCONTROL, 0);
724 AssertRC(rc);
725 *pfReg = pThis->u8ReadInControl;
726 }
727# endif /* VBOX_WITH_WIN_PARPORT_SUP */
728 return rc;
729}
730
731/**
732 * @interface_method_impl{PDMIBASE,pfnReadStatus}
733 */
734static DECLCALLBACK(int) drvHostParallelReadStatus(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
735{
736 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
737 int rc = VINF_SUCCESS;
738 int rcLnx = 0;
739 uint8_t fReg = 0;
740# ifndef VBOX_WITH_WIN_PARPORT_SUP
741 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRSTATUS, &fReg);
742 if (RT_UNLIKELY(rcLnx < 0))
743 rc = RTErrConvertFromErrno(errno);
744 else
745 {
746 LogFlowFunc(("fReg=%#x\n", fReg));
747 *pfReg = fReg;
748 }
749# else /* VBOX_WITH_WIN_PARPORT_SUP */
750 *pfReg = 0; /* Intialize the buffer. */
751 if (pThis->fParportAvail)
752 {
753 LogFlowFunc(("calling R0 to read status from parallel port\n"));
754 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READSTATUS, 0);
755 AssertRC(rc);
756 *pfReg = pThis->u8ReadInStatus;
757 }
758# endif /* VBOX_WITH_WIN_PARPORT_SUP */
759 return rc;
760}
761
762# ifndef VBOX_WITH_WIN_PARPORT_SUP
763
764static DECLCALLBACK(int) drvHostParallelMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
765{
766 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
767 struct pollfd aFDs[2];
768
769 /*
770 * We can wait for interrupts using poll on linux hosts.
771 */
772 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
773 {
774 int rc;
775
776 aFDs[0].fd = RTFileToNative(pThis->hFileDevice);
777 aFDs[0].events = POLLIN;
778 aFDs[0].revents = 0;
779 aFDs[1].fd = RTPipeToNative(pThis->hWakeupPipeR);
780 aFDs[1].events = POLLIN | POLLERR | POLLHUP;
781 aFDs[1].revents = 0;
782 rc = poll(aFDs, RT_ELEMENTS(aFDs), -1);
783 if (rc < 0)
784 {
785 AssertMsgFailed(("poll failed with rc=%d\n", RTErrConvertFromErrno(errno)));
786 return RTErrConvertFromErrno(errno);
787 }
788
789 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
790 break;
791 if (rc > 0 && aFDs[1].revents)
792 {
793 if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
794 break;
795 /* notification to terminate -- drain the pipe */
796 char ch;
797 size_t cbRead;
798 RTPipeRead(pThis->hWakeupPipeR, &ch, 1, &cbRead);
799 continue;
800 }
801
802 /* Interrupt occurred. */
803 rc = pThis->pDrvHostParallelPort->pfnNotifyInterrupt(pThis->pDrvHostParallelPort);
804 AssertRC(rc);
805 }
806
807 return VINF_SUCCESS;
808}
809
810/**
811 * Unblock the monitor thread so it can respond to a state change.
812 *
813 * @returns a VBox status code.
814 * @param pDrvIns The driver instance.
815 * @param pThread The send thread.
816 */
817static DECLCALLBACK(int) drvHostParallelWakeupMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
818{
819 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
820 size_t cbIgnored;
821 return RTPipeWrite(pThis->hWakeupPipeW, "", 1, &cbIgnored);
822}
823
824# endif /* VBOX_WITH_WIN_PARPORT_SUP */
825
826/**
827 * Destruct a host parallel driver instance.
828 *
829 * Most VM resources are freed by the VM. This callback is provided so that
830 * any non-VM resources can be freed correctly.
831 *
832 * @param pDrvIns The driver instance data.
833 */
834static DECLCALLBACK(void) drvHostParallelDestruct(PPDMDRVINS pDrvIns)
835{
836 int rc;
837 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
838 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
839 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
840
841#ifndef VBOX_WITH_WIN_PARPORT_SUP
842 if (pThis->hFileDevice != NIL_RTFILE)
843 ioctl(RTFileToNative(pThis->hFileDevice), PPRELEASE);
844
845 rc = RTPipeClose(pThis->hWakeupPipeW); AssertRC(rc);
846 pThis->hWakeupPipeW = NIL_RTPIPE;
847
848 rc = RTPipeClose(pThis->hWakeupPipeR); AssertRC(rc);
849 pThis->hWakeupPipeR = NIL_RTPIPE;
850
851 rc = RTFileClose(pThis->hFileDevice); AssertRC(rc);
852 pThis->hFileDevice = NIL_RTFILE;
853
854 if (pThis->pszDevicePath)
855 {
856 MMR3HeapFree(pThis->pszDevicePath);
857 pThis->pszDevicePath = NULL;
858 }
859#else /* VBOX_WITH_WIN_PARPORT_SUP */
860 if (pThis->hWinFileDevice != NIL_RTFILE)
861 rc = RTFileClose(pThis->hWinFileDevice); AssertRC(rc);
862#endif /* VBOX_WITH_WIN_PARPORT_SUP */
863}
864
865/**
866 * Construct a host parallel driver instance.
867 *
868 * @copydoc FNPDMDRVCONSTRUCT
869 */
870static DECLCALLBACK(int) drvHostParallelConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
871{
872 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
873 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
874
875 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
876
877 /*
878 * Init basic data members and interfaces.
879 *
880 * Must be done before returning any failure because we've got a destructor.
881 */
882 pThis->hFileDevice = NIL_RTFILE;
883#ifndef VBOX_WITH_WIN_PARPORT_SUP
884 pThis->hWakeupPipeR = NIL_RTPIPE;
885 pThis->hWakeupPipeW = NIL_RTPIPE;
886#else /* VBOX_WITH_WIN_PARPORT_SUP */
887 pThis->hWinFileDevice = NIL_RTFILE;
888#endif
889
890 pThis->pDrvInsR3 = pDrvIns;
891#ifdef VBOX_WITH_DRVINTNET_IN_R0
892 pThis->pDrvInsR0 = PDMDRVINS_2_R0PTR(pDrvIns);
893#endif
894
895 /* IBase. */
896 pDrvIns->IBase.pfnQueryInterface = drvHostParallelQueryInterface;
897 /* IHostParallelConnector. */
898 pThis->IHostParallelConnectorR3.pfnWrite = drvHostParallelWrite;
899 pThis->IHostParallelConnectorR3.pfnRead = drvHostParallelRead;
900 pThis->IHostParallelConnectorR3.pfnSetPortDirection = drvHostParallelSetPortDirection;
901 pThis->IHostParallelConnectorR3.pfnWriteControl = drvHostParallelWriteControl;
902 pThis->IHostParallelConnectorR3.pfnReadControl = drvHostParallelReadControl;
903 pThis->IHostParallelConnectorR3.pfnReadStatus = drvHostParallelReadStatus;
904
905 /*
906 * Validate the config.
907 */
908 if (!CFGMR3AreValuesValid(pCfg, "DevicePath\0"))
909 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
910 N_("Unknown host parallel configuration option, only supports DevicePath"));
911
912 /*
913 * Query configuration.
914 */
915 /* Device */
916 int rc = CFGMR3QueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
917 if (RT_FAILURE(rc))
918 {
919 AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
920 return rc;
921 }
922
923 /*
924 * Open the device
925 */
926 rc = RTFileOpen(&pThis->hFileDevice, pThis->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
927 if (RT_FAILURE(rc))
928 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Parallel#%d could not open '%s'"),
929 pDrvIns->iInstance, pThis->pszDevicePath);
930
931#ifndef VBOX_WITH_WIN_PARPORT_SUP
932 /*
933 * Try to get exclusive access to parallel port
934 */
935 rc = ioctl(RTFileToNative(pThis->hFileDevice), PPEXCL);
936 if (rc < 0)
937 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
938 N_("Parallel#%d could not get exclusive access for parallel port '%s'"
939 "Be sure that no other process or driver accesses this port"),
940 pDrvIns->iInstance, pThis->pszDevicePath);
941
942 /*
943 * Claim the parallel port
944 */
945 rc = ioctl(RTFileToNative(pThis->hFileDevice), PPCLAIM);
946 if (rc < 0)
947 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
948 N_("Parallel#%d could not claim parallel port '%s'"
949 "Be sure that no other process or driver accesses this port"),
950 pDrvIns->iInstance, pThis->pszDevicePath);
951
952 /*
953 * Get the IHostParallelPort interface of the above driver/device.
954 */
955 pThis->pDrvHostParallelPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTPARALLELPORT);
956 if (!pThis->pDrvHostParallelPort)
957 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Parallel#%d has no parallel port interface above"),
958 pDrvIns->iInstance);
959
960 /*
961 * Create wakeup pipe.
962 */
963 rc = RTPipeCreate(&pThis->hWakeupPipeR, &pThis->hWakeupPipeW, 0 /*fFlags*/);
964 AssertRCReturn(rc, rc);
965
966 /*
967 * Start in SPP mode.
968 */
969 pThis->enmModeCur = PDM_PARALLEL_PORT_MODE_INVALID;
970 rc = drvHostParallelSetMode(pThis, PDM_PARALLEL_PORT_MODE_SPP);
971 if (RT_FAILURE(rc))
972 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot change mode of parallel mode to SPP"), pDrvIns->iInstance);
973
974 /*
975 * Start waiting for interrupts.
976 */
977 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pMonitorThread, pThis, drvHostParallelMonitorThread, drvHostParallelWakeupMonitorThread, 0,
978 RTTHREADTYPE_IO, "ParMon");
979 if (RT_FAILURE(rc))
980 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot create monitor thread"), pDrvIns->iInstance);
981
982#else /* VBOX_WITH_WIN_PARPORT_SUP */
983 HANDLE hPort;
984 pThis->fParportAvail = false;
985 pThis->u32LptAddr = 0;
986 pThis->u32LptAddrControl = 0;
987 pThis->u32LptAddrStatus = 0;
988 rc = drvWinHostGetparportAddr(pThis);
989
990 /* If we have the char port availabe use it , else I am not getting exclusive access to parallel port.
991 Read and write will be done only if addresses are available
992 */
993 if (pThis->szParportName)
994 {
995 rc = RTFileOpen(&pThis->hWinFileDevice, (char *)pThis->szParportName,
996 RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
997 }
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.

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