VirtualBox

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

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

Devices/Parallel: Fixing more todos in DrvHostParallel.cpp as identified by Knut and Klaus.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 35.1 KB
Line 
1/* $Id: DrvHostParallel.cpp 42375 2012-07-25 07:24:41Z vboxsync $ */
2/** @file
3 * VirtualBox Host Parallel Port Driver.
4 *
5 * Initial Linux-only code contributed by: Alexander Eichner
6 */
7
8/*
9 * Copyright (C) 2006-2012 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_DRV_HOST_PARALLEL
24#include <VBox/vmm/pdmdrv.h>
25#include <VBox/vmm/pdmthread.h>
26#include <iprt/asm.h>
27#include <iprt/assert.h>
28#include <iprt/file.h>
29#include <iprt/pipe.h>
30#include <iprt/semaphore.h>
31#include <iprt/stream.h>
32#include <iprt/uuid.h>
33#include <iprt/cdefs.h>
34#include <iprt/ctype.h>
35
36#ifdef RT_OS_LINUX
37# include <sys/ioctl.h>
38# include <sys/types.h>
39# include <sys/stat.h>
40# include <sys/poll.h>
41# include <fcntl.h>
42# include <unistd.h>
43# include <linux/ppdev.h>
44# include <linux/parport.h>
45# include <errno.h>
46#endif
47
48
49/** @def VBOX_WITH_WIN_PARPORT_SUP *
50 * Indicates whether to use the generic direct hardware access or host specific
51 * code to access the parallel port.
52 */
53#if defined(RT_OS_LINUX)
54# undef VBOX_WITH_WIN_PARPORT_SUP
55#elif defined(RT_OS_WINDOWS)
56//# define VBOX_WITH_WIN_PARPORT_SUP
57#else
58# error "Not ported"
59#endif
60
61#if defined(VBOX_WITH_WIN_PARPORT_SUP) && defined(IN_RING0)
62# include <iprt/asm-amd64-x86.h>
63#endif
64
65#if defined(VBOX_WITH_WIN_PARPORT_SUP) && defined(IN_RING3)
66# include <iprt/mem.h>
67# include <iprt/string.h>
68#endif
69
70#include "VBoxDD.h"
71
72
73/*******************************************************************************
74* Structures and Typedefs *
75*******************************************************************************/
76/**
77 * Host parallel port driver instance data.
78 * @implements PDMIHOSTPARALLELCONNECTOR
79 */
80typedef struct DRVHOSTPARALLEL
81{
82 /** Pointer to the driver instance structure. */
83 PPDMDRVINS pDrvIns;
84 /** Pointer to the driver instance. */
85 PPDMDRVINSR3 pDrvInsR3;
86 PPDMDRVINSR0 pDrvInsR0;
87 /** Pointer to the char port interface of the driver/device above us. */
88 PPDMIHOSTPARALLELPORT pDrvHostParallelPort;
89 /** Our host device interface. */
90 PDMIHOSTPARALLELCONNECTOR IHostParallelConnector;
91 /** Our host device interface. */
92 PDMIHOSTPARALLELCONNECTOR IHostParallelConnectorR3;
93 /** Device Path */
94 char *pszDevicePath;
95 /** Device Handle */
96 RTFILE hFileDevice;
97#ifndef VBOX_WITH_WIN_PARPORT_SUP
98 /** Thread waiting for interrupts. */
99 PPDMTHREAD pMonitorThread;
100 /** Wakeup pipe read end. */
101 RTPIPE hWakeupPipeR;
102 /** Wakeup pipe write end. */
103 RTPIPE hWakeupPipeW;
104 /** Current mode the parallel port is in. */
105 PDMPARALLELPORTMODE enmModeCur;
106#endif
107
108#ifdef VBOX_WITH_WIN_PARPORT_SUP
109 /** Data register. */
110 uint32_t u32LptAddr;
111 /** Status register. */
112 uint32_t u32LptAddrStatus;
113 /** Control register. */
114 uint32_t u32LptAddrControl;
115 /** Data read buffer. */
116 uint8_t u8ReadIn;
117 /** Control read buffer. */
118 uint8_t u8ReadInControl;
119 /** Status read buffer. */
120 uint8_t u8ReadInStatus;
121 /** Parallel port name */
122 char szParportName[6];
123 /** Whether the parallel port is available or not. */
124 bool fParportAvail;
125#endif /* VBOX_WITH_WIN_PARPORT_SUP */
126} DRVHOSTPARALLEL, *PDRVHOSTPARALLEL;
127
128
129/**
130 * Ring-0 operations.
131 */
132typedef enum DRVHOSTPARALLELR0OP
133{
134 /** Invalid zero value. */
135 DRVHOSTPARALLELR0OP_INVALID = 0,
136 /** Perform R0 initialization. */
137 DRVHOSTPARALLELR0OP_INITR0STUFF,
138 /** Read data. */
139 DRVHOSTPARALLELR0OP_READ,
140 /** Read status register. */
141 DRVHOSTPARALLELR0OP_READSTATUS,
142 /** Read control register. */
143 DRVHOSTPARALLELR0OP_READCONTROL,
144 /** Write data. */
145 DRVHOSTPARALLELR0OP_WRITE,
146 /** Write control register. */
147 DRVHOSTPARALLELR0OP_WRITECONTROL,
148 /** Set port direction. */
149 DRVHOSTPARALLELR0OP_SETPORTDIRECTION
150} DRVHOSTPARALLELR0OP;
151
152/** Converts a pointer to DRVHOSTPARALLEL::IHostDeviceConnector to a PDRHOSTPARALLEL. */
153#define PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface) ( (PDRVHOSTPARALLEL)((uintptr_t)pInterface - RT_OFFSETOF(DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector))) )
154
155
156/*******************************************************************************
157* Defined Constants And Macros *
158*******************************************************************************/
159#define CTRL_REG_OFFSET 2
160#define STATUS_REG_OFFSET 1
161#define LPT_CONTROL_ENABLE_BIDIRECT 0x20
162
163
164
165#ifdef VBOX_WITH_WIN_PARPORT_SUP
166# ifdef IN_RING0
167
168/**
169 * R0 mode function to write byte value to data port.
170 * @returns VBox status code.
171 * @param pDrvIns Driver instance.
172 * @param u64Arg Data to be written to data register.
173 *
174 */
175static int drvR0HostParallelReqWrite(PPDMDRVINS pDrvIns, uint64_t u64Arg)
176{
177 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
178 LogFlowFunc(("write to data port=%#x val=%#x\n", pThis->u32LptAddr, u64Arg));
179 ASMOutU8(pThis->u32LptAddr, (uint8_t)(u64Arg));
180 return VINF_SUCCESS;
181}
182
183/**
184 * R0 mode function to write byte value to parallel port control
185 * register.
186 * @returns VBox status code.
187 * @param pDrvIns Driver instance.
188 * @param u64Arg Data to be written to control register.
189 */
190static int drvR0HostParallelReqWriteControl(PPDMDRVINS pDrvIns, uint64_t u64Arg)
191{
192 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
193 LogFlowFunc(("write to ctrl port=%#x val=%#x\n", pThis->u32LptAddrControl, u64Arg));
194 ASMOutU8(pThis->u32LptAddrControl, (uint8_t)(u64Arg));
195 return VINF_SUCCESS;
196}
197
198/**
199 * R0 mode function to ready byte value from the parallel port
200 * data register
201 * @returns VBox status code.
202 * @param pDrvIns Driver instance.
203 * @param u64Arg Not used.
204 */
205static int drvR0HostParallelReqRead(PPDMDRVINS pDrvIns, uint64_t u64Arg)
206{
207 uint8_t u8Data;
208 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
209 u8Data = ASMInU8(pThis->u32LptAddr);
210 LogFlowFunc(("read from data port=%#x val=%#x\n", pThis->u32LptAddr, u8Data));
211 pThis->u8ReadIn = u8Data;
212 return VINF_SUCCESS;
213}
214
215/**
216 * R0 mode function to ready byte value from the parallel port
217 * control register.
218 * @returns VBox status code.
219 * @param pDrvIns Driver instance.
220 * @param u64Arg Not used.
221 */
222static int drvR0HostParallelReqReadControl(PPDMDRVINS pDrvIns, uint64_t u64Arg)
223{
224 uint8_t u8Data;
225 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
226 u8Data = ASMInU8(pThis->u32LptAddrControl);
227 LogFlowFunc(("read from ctrl port=%#x val=%#x\n", pThis->u32LptAddr, u8Data));
228 pThis->u8ReadInControl = u8Data;
229 return VINF_SUCCESS;
230}
231
232/**
233 * R0 mode function to ready byte value from the parallel port
234 * status register.
235 * @returns VBox status code.
236 * @param pDrvIns Driver instance.
237 * @param u64Arg Not used.
238 */
239static int drvR0HostParallelReqReadStatus(PPDMDRVINS pDrvIns, uint64_t u64Arg)
240{
241 uint8_t u8Data;
242 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
243 u8Data = ASMInU8(pThis->u32LptAddrStatus);
244 LogFlowFunc(("read from status port=%#x val=%#x\n", pThis->u32LptAddr, u8Data));
245 pThis->u8ReadInStatus = u8Data;
246 return VINF_SUCCESS;
247}
248
249/**
250 * R0 mode function to set the direction of parallel port -
251 * operate in bidirectional mode or single direction.
252 * @returns VBox status code.
253 * @param pDrvIns Driver instance.
254 * @param u64Arg Mode.
255 */
256static int drvR0HostParallelReqSetPortDir(PPDMDRVINS pDrvIns, uint64_t u64Arg)
257{
258 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
259 uint8_t u8ReadControlVal;
260 uint8_t u8WriteControlVal;
261
262 if (u64Arg)
263 {
264 u8ReadControlVal = ASMInU8(pThis->u32LptAddrControl);
265 u8WriteControlVal = u8ReadControlVal | LPT_CONTROL_ENABLE_BIDIRECT; /* enable input direction */
266 ASMOutU8(pThis->u32LptAddrControl, u8WriteControlVal);
267 }
268 else
269 {
270 u8ReadControlVal = ASMInU8(pThis->u32LptAddrControl);
271 u8WriteControlVal = u8ReadControlVal & ~LPT_CONTROL_ENABLE_BIDIRECT; /* disable input direction */
272 ASMOutU8(pThis->u32LptAddrControl, u8WriteControlVal);
273 }
274 return VINF_SUCCESS;
275}
276
277/**
278 * @interface_method_impl{FNPDMDRVREQHANDLERR0}
279 */
280PDMBOTHCBDECL(int) drvR0HostParallelReqHandler(PPDMDRVINS pDrvIns, uint32_t uOperation, uint64_t u64Arg)
281{
282 int rc;
283
284 LogFlowFuncEnter();
285 /* I have included break after each case. Need to work on this. */
286 switch ((DRVHOSTPARALLELR0OP)uOperation)
287 {
288 case DRVHOSTPARALLELR0OP_READ:
289 rc = drvR0HostParallelReqRead(pDrvIns, u64Arg);
290 break;
291 case DRVHOSTPARALLELR0OP_READSTATUS:
292 rc = drvR0HostParallelReqReadStatus(pDrvIns, u64Arg);
293 break;
294 case DRVHOSTPARALLELR0OP_READCONTROL:
295 rc = drvR0HostParallelReqReadControl(pDrvIns, u64Arg);
296 break;
297 case DRVHOSTPARALLELR0OP_WRITE:
298 rc = drvR0HostParallelReqWrite(pDrvIns, u64Arg);
299 break;
300 case DRVHOSTPARALLELR0OP_WRITECONTROL:
301 rc = drvR0HostParallelReqWriteControl(pDrvIns, u64Arg);
302 break;
303 case DRVHOSTPARALLELR0OP_SETPORTDIRECTION:
304 rc = drvR0HostParallelReqSetPortDir(pDrvIns, u64Arg);
305 break;
306 default: /* not supported */
307 rc = VERR_NOT_SUPPORTED;
308 }
309 LogFlowFuncLeave();
310 return rc;
311}
312
313# endif /* IN_RING0 */
314#endif /* VBOX_WITH_WIN_PARPORT_SUP */
315
316#ifdef IN_RING3
317# ifdef VBOX_WITH_WIN_PARPORT_SUP
318
319/**
320 * Find IO port range for the parallel port and return the lower address.
321 *
322 * @returns parallel port IO address.
323 * @param DevInst Device Instance for parallel port.
324 */
325static uint32_t drvHostWinFindIORangeResource(const DEVINST DevInst)
326{
327 uint8_t *pBuf = NULL;
328 short wHeaderSize;
329 uint32_t u32Size;
330 CONFIGRET cmRet;
331 LOG_CONF firstLogConf;
332 LOG_CONF nextLogConf;
333 RES_DES rdPrevResDes;
334 uint32_t u32ParportAddr;
335
336 wHeaderSize = sizeof(IO_DES);
337 cmRet = CM_Get_First_Log_Conf(&firstLogConf, DevInst, ALLOC_LOG_CONF);
338 if (cmRet != CR_SUCCESS)
339 {
340 cmRet = CM_Get_First_Log_Conf(&firstLogConf, DevInst, BOOT_LOG_CONF);
341 if (cmRet != CR_SUCCESS)
342 return 0;
343 }
344 cmRet = CM_Get_Next_Res_Des(&nextLogConf, firstLogConf, 2, 0L, 0L);
345 if (cmRet != CR_SUCCESS)
346 {
347 CM_Free_Res_Des_Handle(firstLogConf);
348 return 0;
349 }
350
351 for (;;)
352 {
353 u32Size = 0;
354 if ((cmRet = CM_Get_Res_Des_Data_Size((PULONG)(&u32Size), nextLogConf, 0L)) != CR_SUCCESS
355 && !(pBuf = (uint8_t *)RTMemAlloc(u32Size + 1))
356 && (cmRet = CM_Get_Res_Des_Data(nextLogConf, pBuf, u32Size, 0L)) != CR_SUCESS
357 )
358 {
359 CM_Free_Res_Des_Handle(nextLogConf);
360 if (pBuf)
361 RtMemFree(pBuf);
362 break;
363
364 }
365 LogFlowFunc(("call GetIOResource\n"));
366 u32ParportAddr = ((IO_DES *)pBuf)->IOD_Alloc_Base;
367 LogFlowFunc(("called GetIOResource, ret=%#x\n", u32ParportAddr));
368 rdPrevResDes = 0;
369 cmRet = CM_Get_Next_Res_Des(&rdPrevResDes,
370 nextLogConf,
371 2,
372 0L,
373 0L);
374 RTMemFree(pBuf);
375 if (cmRet != CR_SUCCESS)
376 break;
377
378 CM_Free_Res_Des_Handle(nextLogConf);
379 nextLogConf = rdPrevResDes;
380 }
381 CM_Free_Res_Des_Handle(nextLogConf);
382 LogFlowFunc(("return u32ParportAddr=%#x", u32ParportAddr));
383 return u32ParportAddr;
384}
385
386/**
387 * Get Parallel port address and update the shared data
388 * structure.
389 * @returns VBox status code.
390 * @param pThis The host parallel port instance data.
391 */
392static int drvWinHostGetparportAddr(PDRVHOSTPARALLEL pThis)
393{
394 HDEVINFO hDevInfo;
395 SP_DEVINFO_DATA DeviceInfoData;
396 uint32_t u32Idx;
397 uint32_t u32ParportAddr;
398 int rc = VINF_SUCCESS;
399
400 hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
401 if (hDevInfo == INVALID_HANDLE_VALUE)
402 return VERR_INVALID_HANDLE;
403
404 /* Enumerate through all devices in Set. */
405 DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
406 for (u32Idx = 0; SetupDiEnumDeviceInfo(hDevInfo, u32Idx, &DeviceInfoData); u32Idx++)
407 {
408 DWORD dwDataType;
409 uint8_t *pBuf = NULL;
410 DWORD dwBufSize = 0;
411
412 while (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME,
413 (PDWORD)&dwDataType, (uint8_t *)pBuf,
414 dwBufSize, (PDWORD)&dwBufSize)
415 && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
416 {
417 if (pBuf)
418 RTMemFree(pBuf);
419 /* Max size will never be more than 2048 bytes */
420 pBuf = (uint8_t *)RTMemAlloc(dwBufSize * 2);
421 }
422 if(!pBuf)
423 return VERR_NO_MEMORY;
424
425 if (RTStrStr((char*)pBuf, "LPT"))
426 {
427 u32ParportAddr = drvHostWinFindIORangeResource(DeviceInfoData.DevInst);
428 if (u32ParportAddr)
429 {
430 /* Find parallel port name and update the shared data struncture */
431 char *pCh = RTStrStr((char*)pBuf, "(");
432 char *pTmpCh = RTStrStr((char *)pBuf, ")");
433 /* check for the confirmation for the availability of parallel port */
434 if (!(pCh && pTmpCh))
435 {
436 LogFlowFunc(("Parallel port Not Found. \n"));
437 return VERR_NOT_FOUND;
438
439 }
440 if (((pTmpCh - (char *)pBuf) - (pCh - (char *)pBuf)) < 0) {
441 LogFlowFunc(("Parallel port string not properly formatted.\n"));
442 return VERR_NOT_FOUND;
443 }
444 /* check for the confirmation for the availability of parallel port */
445 if (RTStrCopyEx((char *)(pThis->szParportName), sizeof(pThis->szParportName),
446 pCh+1, ((pTmpCh - (char *)pBuf) - (pCh - (char *)pBuf)) - 1))
447 {
448 LogFlowFunc(("Parallel Port Not Found.\n"));
449 return VERR_NOT_FOUND;
450 }
451 *((char *)pThis->szParportName + (pTmpCh - (char *)pBuf) - (pCh - (char *)pBuf) + 1 ) = '\0';
452
453 /* checking again to make sure that we have got a valid name and in valid format too. */
454 if (RTStrNCmp((char *)pThis->szParportName, "LPT", 3)) {
455 LogFlowFunc(("Parallel Port name \"LPT\" Not Found.\n"));
456 return VERR_NOT_FOUND;
457 }
458 if (!RTStrStr((char *)pThis->szParportName, "LPT")
459 || !(pThis->szParportName[3] >= '0'
460 && pThis->szParportName[3] <= '9'))
461 {
462 RT_BZERO(pThis->szParportName, sizeof(pThis->szParportName));
463 LogFlowFunc(("Printer Port Name Not Found.\n"));
464 return VERR_NOT_FOUND;
465 }
466 pThis->fParportAvail = true;
467 pThis->u32LptAddr = u32ParportAddr;
468 pThis->u32LptAddrControl = pThis->u32LptAddr + CTRL_REG_OFFSET;
469 pThis->u32LptAddrStatus = pThis->u32LptAddr + STATUS_REG_OFFSET;
470 }
471 if (pThis->fParportAvail)
472 break;
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 = 0;
977 pThis->u32LptAddrControl = 0;
978 pThis->u32LptAddrStatus = 0;
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 available
983 */
984 if (pThis->szParportName)
985 {
986 LogFlowFunc(("Get the Handle to Printer Port =%s\n", (char *)pThis->szParportName));
987 /** @todo r=klaus convert to IPRT */
988 hPort = CreateFile((char *)pThis->szParportName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
989 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
990 }
991 /** @todo amakkar: handle the case if hPort is NULL */
992# if 0
993 if (hPort == INVALID_HANDLE_VALUE)
994 {
995 LogFlow(("Failed to get exclusive access to parallel port\n"));
996 rc = VERR_INVALID_HANDLE;
997 }*/
998# endif /* VBOX_WITH_WIN_PARPORT_SUP */
999#endif
1000 return VINF_SUCCESS;
1001}
1002
1003
1004/**
1005 * Char driver registration record.
1006 */
1007const PDMDRVREG g_DrvHostParallel =
1008{
1009 /* u32Version */
1010 PDM_DRVREG_VERSION,
1011 /* szName */
1012 "HostParallel",
1013 /* szRCMod */
1014 "",
1015 /* szR0Mod */
1016 "VBoxDDR0.r0",
1017 /* pszDescription */
1018 "Parallel host driver.",
1019 /* fFlags */
1020# if defined(VBOX_WITH_WIN_PARPORT_SUP)
1021 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DRVREG_FLAGS_R0,
1022# else
1023 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1024# endif
1025 /* fClass. */
1026 PDM_DRVREG_CLASS_CHAR,
1027 /* cMaxInstances */
1028 ~0U,
1029 /* cbInstance */
1030 sizeof(DRVHOSTPARALLEL),
1031 /* pfnConstruct */
1032 drvHostParallelConstruct,
1033 /* pfnDestruct */
1034 drvHostParallelDestruct,
1035 /* pfnRelocate */
1036 NULL,
1037 /* pfnIOCtl */
1038 NULL,
1039 /* pfnPowerOn */
1040 NULL,
1041 /* pfnReset */
1042 NULL,
1043 /* pfnSuspend */
1044 NULL,
1045 /* pfnResume */
1046 NULL,
1047 /* pfnAttach */
1048 NULL,
1049 /* pfnDetach */
1050 NULL,
1051 /* pfnPowerOff */
1052 NULL,
1053 /* pfnSoftReset */
1054 NULL,
1055 /* u32EndVersion */
1056 PDM_DRVREG_VERSION
1057};
1058#endif /*IN_RING3*/
1059
1060
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