VirtualBox

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

Last change on this file since 90778 was 82968, checked in by vboxsync, 4 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 37.0 KB
Line 
1/* $Id: DrvHostParallel.cpp 82968 2020-02-04 10:35:17Z 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-2020 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/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#define LOG_GROUP LOG_GROUP_DRV_HOST_PARALLEL
25#include <VBox/vmm/pdmdrv.h>
26#include <VBox/vmm/pdmthread.h>
27#include <iprt/asm.h>
28#include <iprt/assert.h>
29#include <iprt/file.h>
30#include <iprt/pipe.h>
31#include <iprt/semaphore.h>
32#include <iprt/stream.h>
33#include <iprt/uuid.h>
34#include <iprt/cdefs.h>
35#include <iprt/ctype.h>
36
37#ifdef RT_OS_LINUX
38# include <sys/ioctl.h>
39# include <sys/types.h>
40# include <sys/stat.h>
41# include <sys/poll.h>
42# include <fcntl.h>
43# include <unistd.h>
44# include <linux/ppdev.h>
45# include <linux/parport.h>
46# include <errno.h>
47#endif
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(DOXYGEN_RUNNING)
54# define VBOX_WITH_WIN_PARPORT_SUP
55#endif
56#if defined(RT_OS_LINUX)
57# undef VBOX_WITH_WIN_PARPORT_SUP
58#elif defined(RT_OS_WINDOWS)
59#else
60# error "Not ported"
61#endif
62
63#if defined(VBOX_WITH_WIN_PARPORT_SUP) && defined(IN_RING0)
64# include <iprt/asm-amd64-x86.h>
65#endif
66
67#if defined(VBOX_WITH_WIN_PARPORT_SUP) && defined(IN_RING3)
68# include <iprt/win/windows.h>
69# include <iprt/win/setupapi.h>
70# include <cfgmgr32.h>
71# include <iprt/mem.h>
72# include <iprt/ctype.h>
73# include <iprt/path.h>
74# include <iprt/string.h>
75#endif
76
77#include "VBoxDD.h"
78
79
80/*********************************************************************************************************************************
81* Structures and Typedefs *
82*********************************************************************************************************************************/
83/**
84 * Host parallel port driver instance data.
85 * @implements PDMIHOSTPARALLELCONNECTOR
86 */
87typedef struct DRVHOSTPARALLEL
88{
89 /** Pointer to the driver instance structure. */
90 PPDMDRVINS pDrvIns;
91 /** Pointer to the driver instance. */
92 PPDMDRVINSR3 pDrvInsR3;
93 PPDMDRVINSR0 pDrvInsR0;
94 /** Pointer to the char port interface of the driver/device above us. */
95 PPDMIHOSTPARALLELPORT pDrvHostParallelPort;
96 /** Our host device interface. */
97 PDMIHOSTPARALLELCONNECTOR IHostParallelConnector;
98 /** Our host device interface. */
99 PDMIHOSTPARALLELCONNECTOR IHostParallelConnectorR3;
100 /** Device Path */
101 char *pszDevicePath;
102 /** Device Handle */
103 RTFILE hFileDevice;
104#ifndef VBOX_WITH_WIN_PARPORT_SUP
105 /** Thread waiting for interrupts. */
106 PPDMTHREAD pMonitorThread;
107 /** Wakeup pipe read end. */
108 RTPIPE hWakeupPipeR;
109 /** Wakeup pipe write end. */
110 RTPIPE hWakeupPipeW;
111 /** Current mode the parallel port is in. */
112 PDMPARALLELPORTMODE enmModeCur;
113#endif
114
115#ifdef VBOX_WITH_WIN_PARPORT_SUP
116 /** Data register. */
117 RTIOPORT PortDirectData;
118 /** Status register. */
119 RTIOPORT PortDirectStatus;
120 /** Control register. */
121 RTIOPORT PortDirectControl;
122 /** Control read result buffer. */
123 uint8_t bReadInControl;
124 /** Status read result buffer. */
125 uint8_t bReadInStatus;
126 /** Data buffer for reads and writes. */
127 uint8_t abDataBuf[32];
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 into the data buffer (abDataBuf). */
142 DRVHOSTPARALLELR0OP_READ,
143 /** Read status register. */
144 DRVHOSTPARALLELR0OP_READSTATUS,
145 /** Read control register. */
146 DRVHOSTPARALLELR0OP_READCONTROL,
147 /** Write data from the data buffer (abDataBuf). */
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_UOFFSETOF(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 *
174 * @returns VBox status code.
175 * @param pThis Pointer to the instance data.
176 * @param u64Arg The number of bytes to write (from abDataBuf).
177 */
178static int drvR0HostParallelReqWrite(PDRVHOSTPARALLEL pThis, uint64_t u64Arg)
179{
180 LogFlowFunc(("write %#RX64 bytes to data (%#x)\n", u64Arg, pThis->PortDirectData));
181
182 AssertReturn(u64Arg > 0 && u64Arg <= sizeof(pThis->abDataBuf), VERR_OUT_OF_RANGE);
183 uint8_t const *pbSrc = pThis->abDataBuf;
184 while (u64Arg-- > 0)
185 {
186 ASMOutU8(pThis->PortDirectData, *pbSrc);
187 pbSrc++;
188 }
189
190 return VINF_SUCCESS;
191}
192
193/**
194 * R0 mode function to write byte value to parallel port control register.
195 *
196 * @returns VBox status code.
197 * @param pThis Pointer to the instance data.
198 * @param u64Arg Data to be written to control register.
199 */
200static int drvR0HostParallelReqWriteControl(PDRVHOSTPARALLEL pThis, uint64_t u64Arg)
201{
202 LogFlowFunc(("write to ctrl port=%#x val=%#x\n", pThis->PortDirectControl, u64Arg));
203 ASMOutU8(pThis->PortDirectControl, (uint8_t)(u64Arg));
204 return VINF_SUCCESS;
205}
206
207/**
208 * R0 mode function to ready byte value from the parallel port data register.
209 *
210 * @returns VBox status code.
211 * @param pThis Pointer to the instance data.
212 * @param u64Arg The number of bytes to read into abDataBuf.
213 */
214static int drvR0HostParallelReqRead(PDRVHOSTPARALLEL pThis, uint64_t u64Arg)
215{
216 LogFlowFunc(("read %#RX64 bytes to data (%#x)\n", u64Arg, pThis->PortDirectData));
217
218 AssertReturn(u64Arg > 0 && u64Arg <= sizeof(pThis->abDataBuf), VERR_OUT_OF_RANGE);
219 uint8_t *pbDst = pThis->abDataBuf;
220 while (u64Arg-- > 0)
221 *pbDst++ = ASMInU8(pThis->PortDirectData);
222
223 return VINF_SUCCESS;
224}
225
226/**
227 * R0 mode function to ready byte value from the parallel port control register.
228 *
229 * @returns VBox status code.
230 * @param pThis Pointer to the instance data.
231 */
232static int drvR0HostParallelReqReadControl(PDRVHOSTPARALLEL pThis)
233{
234 uint8_t u8Data = ASMInU8(pThis->PortDirectControl);
235 LogFlowFunc(("read from ctrl port=%#x val=%#x\n", pThis->PortDirectControl, u8Data));
236 pThis->bReadInControl = u8Data;
237 return VINF_SUCCESS;
238}
239
240/**
241 * R0 mode function to ready byte value from the parallel port status register.
242 *
243 * @returns VBox status code.
244 * @param pThis Pointer to the instance data.
245 */
246static int drvR0HostParallelReqReadStatus(PDRVHOSTPARALLEL pThis)
247{
248 uint8_t u8Data = ASMInU8(pThis->PortDirectStatus);
249 LogFlowFunc(("read from status port=%#x val=%#x\n", pThis->PortDirectStatus, u8Data));
250 pThis->bReadInStatus = u8Data;
251 return VINF_SUCCESS;
252}
253
254/**
255 * R0 mode function to set the direction of parallel port -
256 * operate in bidirectional mode or single direction.
257 *
258 * @returns VBox status code.
259 * @param pThis Pointer to the instance data.
260 * @param u64Arg Mode.
261 */
262static int drvR0HostParallelReqSetPortDir(PDRVHOSTPARALLEL pThis, uint64_t u64Arg)
263{
264 uint8_t bCtl = ASMInU8(pThis->PortDirectControl);
265 if (u64Arg)
266 bCtl |= LPT_CONTROL_ENABLE_BIDIRECT; /* enable input direction */
267 else
268 bCtl &= ~LPT_CONTROL_ENABLE_BIDIRECT; /* disable input direction */
269 ASMOutU8(pThis->PortDirectControl, bCtl);
270
271 return VINF_SUCCESS;
272}
273
274/**
275 * @callback_method_impl{FNPDMDRVREQHANDLERR0}
276 */
277PDMBOTHCBDECL(int) drvR0HostParallelReqHandler(PPDMDRVINS pDrvIns, uint32_t uOperation, uint64_t u64Arg)
278{
279 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
280 int rc;
281 LogFlowFuncEnter();
282
283 if (pThis->PortDirectData != 0)
284 {
285 switch ((DRVHOSTPARALLELR0OP)uOperation)
286 {
287 case DRVHOSTPARALLELR0OP_READ:
288 rc = drvR0HostParallelReqRead(pThis, u64Arg);
289 break;
290 case DRVHOSTPARALLELR0OP_READSTATUS:
291 rc = drvR0HostParallelReqReadStatus(pThis);
292 break;
293 case DRVHOSTPARALLELR0OP_READCONTROL:
294 rc = drvR0HostParallelReqReadControl(pThis);
295 break;
296 case DRVHOSTPARALLELR0OP_WRITE:
297 rc = drvR0HostParallelReqWrite(pThis, u64Arg);
298 break;
299 case DRVHOSTPARALLELR0OP_WRITECONTROL:
300 rc = drvR0HostParallelReqWriteControl(pThis, u64Arg);
301 break;
302 case DRVHOSTPARALLELR0OP_SETPORTDIRECTION:
303 rc = drvR0HostParallelReqSetPortDir(pThis, u64Arg);
304 break;
305 default:
306 rc = VERR_INVALID_FUNCTION;
307 break;
308 }
309 }
310 else
311 rc = VERR_WRONG_ORDER;
312
313 LogFlowFuncLeaveRC(rc);
314 return rc;
315}
316
317# endif /* IN_RING0 */
318#endif /* VBOX_WITH_WIN_PARPORT_SUP */
319
320#ifdef IN_RING3
321# ifdef VBOX_WITH_WIN_PARPORT_SUP
322
323/**
324 * Find IO port range for the parallel port and return the lower address.
325 *
326 * @returns Parallel base I/O port.
327 * @param DevInst Device instance (dword/handle) for the parallel port.
328 */
329static RTIOPORT drvHostParallelGetWinHostIoPortsSub(const DEVINST DevInst)
330{
331 RTIOPORT PortBase = 0;
332
333 /* Get handle of the first logical configuration. */
334 LOG_CONF hFirstLogConf;
335 CONFIGRET rcCm = CM_Get_First_Log_Conf(&hFirstLogConf, DevInst, ALLOC_LOG_CONF);
336 if (rcCm != CR_SUCCESS)
337 rcCm = CM_Get_First_Log_Conf(&hFirstLogConf, DevInst, BOOT_LOG_CONF);
338 if (rcCm == CR_SUCCESS)
339 {
340 /*
341 * This loop is based on the "fact" that only one I/O resource is assigned
342 * to the LPT port. Should there ever be multiple resources, we'll pick
343 * the last one for some silly reason.
344 */
345
346 /* Get the first resource descriptor handle. */
347 LOG_CONF hCurLogConf = 0;
348 rcCm = CM_Get_Next_Res_Des(&hCurLogConf, hFirstLogConf, ResType_IO, 0, 0);
349 if (rcCm == CR_SUCCESS)
350 {
351 for (;;)
352 {
353 ULONG cbData;
354 rcCm = CM_Get_Res_Des_Data_Size(&cbData, hCurLogConf, 0);
355 if (rcCm != CR_SUCCESS)
356 cbData = 0;
357 cbData = RT_MAX(cbData, sizeof(IO_DES));
358 IO_DES *pIoDesc = (IO_DES *)RTMemAllocZ(cbData);
359 if (pIoDesc)
360 {
361 rcCm = CM_Get_Res_Des_Data(hCurLogConf, pIoDesc, cbData, 0L);
362 if (rcCm == CR_SUCCESS)
363 {
364 LogRel(("drvHostParallelGetWinHostIoPortsSub: Count=%#u Type=%#x Base=%#RX64 End=%#RX64 Flags=%#x\n",
365 pIoDesc->IOD_Count, pIoDesc->IOD_Type, (uint64_t)pIoDesc->IOD_Alloc_Base,
366 (uint64_t)pIoDesc->IOD_Alloc_End, pIoDesc->IOD_DesFlags));
367 PortBase = (RTIOPORT)pIoDesc->IOD_Alloc_Base;
368 }
369 else
370 LogRel(("drvHostParallelGetWinHostIoPortsSub: CM_Get_Res_Des_Data(,,%u,0) failed: %u\n", cbData, rcCm));
371 RTMemFree(pIoDesc);
372 }
373 else
374 LogRel(("drvHostParallelGetWinHostIoPortsSub: failed to allocate %#x bytes\n", cbData));
375
376 /* Next */
377 RES_DES hFreeResDesc = hCurLogConf;
378 rcCm = CM_Get_Next_Res_Des(&hCurLogConf, hCurLogConf, ResType_IO, 0, 0);
379 CM_Free_Res_Des_Handle(hFreeResDesc);
380 if (rcCm != CR_SUCCESS)
381 {
382 if (rcCm != CR_NO_MORE_RES_DES)
383 LogRel(("drvHostParallelGetWinHostIoPortsSub: CM_Get_Next_Res_Des failed: %u\n", rcCm));
384 break;
385 }
386 }
387 }
388 else
389 LogRel(("drvHostParallelGetWinHostIoPortsSub: Initial CM_Get_Next_Res_Des failed: %u\n", rcCm));
390 CM_Free_Log_Conf_Handle(hFirstLogConf);
391 }
392 LogFlowFunc(("return PortBase=%#x", PortBase));
393 return PortBase;
394}
395
396/**
397 * Get Parallel port address and update the shared data structure.
398 *
399 * @returns VBox status code.
400 * @param pThis The host parallel port instance data.
401 */
402static int drvHostParallelGetWinHostIoPorts(PDRVHOSTPARALLEL pThis)
403{
404 /*
405 * Assume the host device path is on the form "\\.\PIPE\LPT1", then get the "LPT1" part.
406 */
407 const char * const pszCfgPortName = RTPathFilename(pThis->pszDevicePath);
408 AssertReturn(pszCfgPortName, VERR_INTERNAL_ERROR_3);
409 size_t const cchCfgPortName = strlen(pszCfgPortName);
410 if ( cchCfgPortName != 4
411 || RTStrNICmp(pszCfgPortName, "LPT", 3) != 0
412 || !RT_C_IS_DIGIT(pszCfgPortName[3]) )
413 {
414 LogRel(("drvHostParallelGetWinHostIoPorts: The configured device name '%s' is not on the expected 'LPTx' form!\n",
415 pszCfgPortName));
416 return VERR_INVALID_NAME;
417 }
418
419 /*
420 * Get a list of devices then enumerate it looking for the LPT port we're using.
421 */
422 HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);
423 if (hDevInfo == INVALID_HANDLE_VALUE)
424 {
425 DWORD dwErr = GetLastError();
426 LogRel(("drvHostParallelGetWinHostIoPorts: SetupDiGetClassDevs failed: %u\n", dwErr));
427 return RTErrConvertFromWin32(dwErr);
428 }
429
430 int rc = VINF_SUCCESS;
431 char *pszBuf = NULL;
432 DWORD cbBuf = 0;
433 for (int32_t idxDevInfo = 0;; idxDevInfo++)
434 {
435 /*
436 * Query the next device info.
437 */
438 SP_DEVINFO_DATA DeviceInfoData;
439 DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
440 if (!SetupDiEnumDeviceInfo(hDevInfo, idxDevInfo, &DeviceInfoData))
441 {
442 DWORD dwErr = GetLastError();
443 if (dwErr != ERROR_NO_MORE_ITEMS && dwErr != NO_ERROR)
444 {
445 LogRel(("drvHostParallelGetWinHostIoPorts: SetupDiEnumDeviceInfo failed: %u\n", dwErr));
446 rc = RTErrConvertFromWin32(dwErr);
447 }
448 break;
449 }
450
451 /* Get the friendly name of the device. */
452 DWORD dwDataType;
453 DWORD cbBufActual;
454 BOOL fOk;
455 while (!(fOk = SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME,
456 &dwDataType, (PBYTE)pszBuf, cbBuf, &cbBufActual)))
457 {
458 DWORD dwErr = GetLastError();
459 if (dwErr == ERROR_INSUFFICIENT_BUFFER)
460 {
461 LogFlow(("ERROR_INSUFF_BUFF = %d. dwBufSz = %d\n", dwErr, cbBuf));
462 cbBuf = RT_MAX(RT_ALIGN_Z(cbBufActual + 16, 64), 256);
463 void *pvNew = RTMemRealloc(pszBuf, cbBuf);
464 if (pvNew)
465 pszBuf = (char *)pvNew;
466 else
467 {
468 LogFlow(("GetDevProp Error = %d & cbBufActual = %d\n", dwErr, cbBufActual));
469 break;
470 }
471 }
472 else
473 {
474 /* No need to bother about this error (in most cases its errno=13,
475 * INVALID_DATA . Just break from here and proceed to next device
476 * enumerated item
477 */
478 LogFlow(("GetDevProp Error = %d & cbBufActual = %d\n", dwErr, cbBufActual));
479 break;
480 }
481 }
482 if ( fOk
483 && pszBuf)
484 {
485 pszBuf[cbBuf - 1] = '\0';
486
487 /*
488 * Does this look like the port name we're looking for.
489 *
490 * We're expecting either "Parallel Port (LPT1)" or just "LPT1", though we'll make do
491 * with anything that includes the name we're looking for as a separate word.
492 */
493 char *pszMatch;
494 do
495 pszMatch = RTStrIStr(pszBuf, pszCfgPortName);
496 while ( pszMatch != NULL
497 && !( ( pszMatch == pszBuf
498 || pszMatch[-1] == '('
499 || RT_C_IS_BLANK(pszMatch[-1]))
500 && ( pszMatch[cchCfgPortName] == '\0'
501 || pszMatch[cchCfgPortName] == ')'
502 || RT_C_IS_BLANK(pszMatch[cchCfgPortName])) ) );
503 if (pszMatch != NULL)
504 {
505 RTIOPORT Port = drvHostParallelGetWinHostIoPortsSub(DeviceInfoData.DevInst);
506 if (Port != 0)
507 {
508 pThis->PortDirectData = Port;
509 pThis->PortDirectControl = Port + CTRL_REG_OFFSET;
510 pThis->PortDirectStatus = Port + STATUS_REG_OFFSET;
511 break;
512 }
513 LogRel(("drvHostParallelGetWinHostIoPorts: Addr not found for '%s'\n", pszBuf));
514 }
515 }
516 }
517
518 /* Cleanup. */
519 RTMemFree(pszBuf);
520 SetupDiDestroyDeviceInfoList(hDevInfo);
521 return rc;
522
523}
524# endif /* VBOX_WITH_WIN_PARPORT_SUP */
525
526/**
527 * Changes the current mode of the host parallel port.
528 *
529 * @returns VBox status code.
530 * @param pThis The host parallel port instance data.
531 * @param enmMode The mode to change the port to.
532 */
533static int drvHostParallelSetMode(PDRVHOSTPARALLEL pThis, PDMPARALLELPORTMODE enmMode)
534{
535 LogFlowFunc(("mode=%d\n", enmMode));
536# ifndef VBOX_WITH_WIN_PARPORT_SUP
537 int rc = VINF_SUCCESS;
538 int iMode = 0;
539 int rcLnx;
540 if (pThis->enmModeCur != enmMode)
541 {
542 switch (enmMode)
543 {
544 case PDM_PARALLEL_PORT_MODE_SPP:
545 iMode = IEEE1284_MODE_COMPAT;
546 break;
547 case PDM_PARALLEL_PORT_MODE_EPP_DATA:
548 iMode = IEEE1284_MODE_EPP | IEEE1284_DATA;
549 break;
550 case PDM_PARALLEL_PORT_MODE_EPP_ADDR:
551 iMode = IEEE1284_MODE_EPP | IEEE1284_ADDR;
552 break;
553 case PDM_PARALLEL_PORT_MODE_ECP:
554 case PDM_PARALLEL_PORT_MODE_INVALID:
555 default:
556 return VERR_NOT_SUPPORTED;
557 }
558
559 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPSETMODE, &iMode);
560 if (RT_UNLIKELY(rcLnx < 0))
561 rc = RTErrConvertFromErrno(errno);
562 else
563 pThis->enmModeCur = enmMode;
564 }
565
566 return rc;
567# else /* VBOX_WITH_WIN_PARPORT_SUP */
568 RT_NOREF(pThis, enmMode);
569 return VINF_SUCCESS;
570# endif /* VBOX_WITH_WIN_PARPORT_SUP */
571}
572
573/* -=-=-=-=- IBase -=-=-=-=- */
574
575/**
576 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
577 */
578static DECLCALLBACK(void *) drvHostParallelQueryInterface(PPDMIBASE pInterface, const char *pszIID)
579{
580 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
581 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
582
583 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
584 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTPARALLELCONNECTOR, &pThis->CTX_SUFF(IHostParallelConnector));
585 return NULL;
586}
587
588
589/* -=-=-=-=- IHostDeviceConnector -=-=-=-=- */
590
591/**
592 * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnWrite}
593 */
594static DECLCALLBACK(int)
595drvHostParallelWrite(PPDMIHOSTPARALLELCONNECTOR pInterface, const void *pvBuf, size_t cbWrite, PDMPARALLELPORTMODE enmMode)
596{
597 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
598 int rc = VINF_SUCCESS;
599
600 LogFlowFunc(("pvBuf=%#p cbWrite=%d\n", pvBuf, cbWrite));
601
602 rc = drvHostParallelSetMode(pThis, enmMode);
603 if (RT_FAILURE(rc))
604 return rc;
605# ifndef VBOX_WITH_WIN_PARPORT_SUP
606 int rcLnx = 0;
607 if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
608 {
609 /* Set the data lines directly. */
610 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
611 }
612 else
613 {
614 /* Use write interface. */
615 rcLnx = write(RTFileToNative(pThis->hFileDevice), pvBuf, cbWrite);
616 }
617 if (RT_UNLIKELY(rcLnx < 0))
618 rc = RTErrConvertFromErrno(errno);
619
620# else /* VBOX_WITH_WIN_PARPORT_SUP */
621 if (pThis->PortDirectData != 0)
622 {
623 while (cbWrite > 0)
624 {
625 size_t cbToWrite = RT_MIN(cbWrite, sizeof(pThis->abDataBuf));
626 LogFlowFunc(("Calling R0 to write %#zu bytes of data\n", cbToWrite));
627 memcpy(pThis->abDataBuf, pvBuf, cbToWrite);
628 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITE, cbToWrite);
629 AssertRC(rc);
630 pvBuf = (uint8_t const *)pvBuf + cbToWrite;
631 cbWrite -= cbToWrite;
632 }
633 }
634# endif /* VBOX_WITH_WIN_PARPORT_SUP */
635 return rc;
636}
637
638/**
639 * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnRead}
640 */
641static DECLCALLBACK(int)
642drvHostParallelRead(PPDMIHOSTPARALLELCONNECTOR pInterface, void *pvBuf, size_t cbRead, PDMPARALLELPORTMODE enmMode)
643{
644 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
645 int rc = VINF_SUCCESS;
646
647# ifndef VBOX_WITH_WIN_PARPORT_SUP
648 int rcLnx = 0;
649 LogFlowFunc(("pvBuf=%#p cbRead=%d\n", pvBuf, cbRead));
650
651 rc = drvHostParallelSetMode(pThis, enmMode);
652 if (RT_FAILURE(rc))
653 return rc;
654
655 if (enmMode == PDM_PARALLEL_PORT_MODE_SPP)
656 {
657 /* Set the data lines directly. */
658 rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWDATA, pvBuf);
659 }
660 else
661 {
662 /* Use write interface. */
663 rcLnx = read(RTFileToNative(pThis->hFileDevice), pvBuf, cbRead);
664 }
665 if (RT_UNLIKELY(rcLnx < 0))
666 rc = RTErrConvertFromErrno(errno);
667
668# else /* VBOX_WITH_WIN_PARPORT_SUP */
669 RT_NOREF(enmMode);
670 if (pThis->PortDirectData != 0)
671 {
672 while (cbRead > 0)
673 {
674 size_t cbToRead = RT_MIN(cbRead, sizeof(pThis->abDataBuf));
675 LogFlowFunc(("Calling R0 to read %#zu bytes of data\n", cbToRead));
676 memset(pThis->abDataBuf, 0, cbToRead);
677 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READ, cbToRead);
678 AssertRC(rc);
679 memcpy(pvBuf, pThis->abDataBuf, cbToRead);
680 pvBuf = (uint8_t *)pvBuf + cbToRead;
681 cbRead -= cbToRead;
682 }
683 }
684# endif /* VBOX_WITH_WIN_PARPORT_SUP */
685 return rc;
686}
687
688/**
689 * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnSetPortDirection}
690 */
691static DECLCALLBACK(int) drvHostParallelSetPortDirection(PPDMIHOSTPARALLELCONNECTOR pInterface, bool fForward)
692{
693 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
694 int rc = VINF_SUCCESS;
695 int iMode = 0;
696 if (!fForward)
697 iMode = 1;
698# ifndef VBOX_WITH_WIN_PARPORT_SUP
699 int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPDATADIR, &iMode);
700 if (RT_UNLIKELY(rcLnx < 0))
701 rc = RTErrConvertFromErrno(errno);
702
703# else /* VBOX_WITH_WIN_PARPORT_SUP */
704 if (pThis->PortDirectData != 0)
705 {
706 LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", iMode));
707 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_SETPORTDIRECTION, iMode);
708 AssertRC(rc);
709 }
710# endif /* VBOX_WITH_WIN_PARPORT_SUP */
711 return rc;
712}
713
714/**
715 * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnWriteControl}
716 */
717static DECLCALLBACK(int) drvHostParallelWriteControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t fReg)
718{
719 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
720 int rc = VINF_SUCCESS;
721
722 LogFlowFunc(("fReg=%#x\n", fReg));
723# ifndef VBOX_WITH_WIN_PARPORT_SUP
724 int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPWCONTROL, &fReg);
725 if (RT_UNLIKELY(rcLnx < 0))
726 rc = RTErrConvertFromErrno(errno);
727# else /* VBOX_WITH_WIN_PARPORT_SUP */
728 if (pThis->PortDirectData != 0)
729 {
730 LogFlowFunc(("calling R0 to write CTRL, data=%#x\n", fReg));
731 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_WRITECONTROL, fReg);
732 AssertRC(rc);
733 }
734# endif /* VBOX_WITH_WIN_PARPORT_SUP */
735 return rc;
736}
737
738
739/**
740 * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnReadControl}
741 */
742static DECLCALLBACK(int) drvHostParallelReadControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
743{
744 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
745 int rc = VINF_SUCCESS;
746
747# ifndef VBOX_WITH_WIN_PARPORT_SUP
748 uint8_t fReg = 0;
749 int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRCONTROL, &fReg);
750 if (RT_UNLIKELY(rcLnx < 0))
751 rc = RTErrConvertFromErrno(errno);
752 else
753 {
754 LogFlowFunc(("fReg=%#x\n", fReg));
755 *pfReg = fReg;
756 }
757# else /* VBOX_WITH_WIN_PARPORT_SUP */
758 *pfReg = 0; /* Initialize the buffer*/
759 if (pThis->PortDirectData != 0)
760 {
761 LogFlowFunc(("calling R0 to read control from parallel port\n"));
762 rc = PDMDrvHlpCallR0(pThis-> CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READCONTROL, 0);
763 AssertRC(rc);
764 *pfReg = pThis->bReadInControl;
765 }
766# endif /* VBOX_WITH_WIN_PARPORT_SUP */
767 return rc;
768}
769
770/**
771 * @interface_method_impl{PDMIHOSTPARALLELCONNECTOR,pfnReadStatus}
772 */
773static DECLCALLBACK(int) drvHostParallelReadStatus(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
774{
775 PDRVHOSTPARALLEL pThis = RT_FROM_MEMBER(pInterface, DRVHOSTPARALLEL, CTX_SUFF(IHostParallelConnector));
776 int rc = VINF_SUCCESS;
777# ifndef VBOX_WITH_WIN_PARPORT_SUP
778 uint8_t fReg = 0;
779 int rcLnx = ioctl(RTFileToNative(pThis->hFileDevice), PPRSTATUS, &fReg);
780 if (RT_UNLIKELY(rcLnx < 0))
781 rc = RTErrConvertFromErrno(errno);
782 else
783 {
784 LogFlowFunc(("fReg=%#x\n", fReg));
785 *pfReg = fReg;
786 }
787# else /* VBOX_WITH_WIN_PARPORT_SUP */
788 *pfReg = 0; /* Intialize the buffer. */
789 if (pThis->PortDirectData != 0)
790 {
791 LogFlowFunc(("calling R0 to read status from parallel port\n"));
792 rc = PDMDrvHlpCallR0(pThis->CTX_SUFF(pDrvIns), DRVHOSTPARALLELR0OP_READSTATUS, 0);
793 AssertRC(rc);
794 *pfReg = pThis->bReadInStatus;
795 }
796# endif /* VBOX_WITH_WIN_PARPORT_SUP */
797 return rc;
798}
799
800# ifndef VBOX_WITH_WIN_PARPORT_SUP
801
802static DECLCALLBACK(int) drvHostParallelMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
803{
804 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
805 struct pollfd aFDs[2];
806
807 /*
808 * We can wait for interrupts using poll on linux hosts.
809 */
810 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
811 {
812 int rc;
813
814 aFDs[0].fd = RTFileToNative(pThis->hFileDevice);
815 aFDs[0].events = POLLIN;
816 aFDs[0].revents = 0;
817 aFDs[1].fd = RTPipeToNative(pThis->hWakeupPipeR);
818 aFDs[1].events = POLLIN | POLLERR | POLLHUP;
819 aFDs[1].revents = 0;
820 rc = poll(aFDs, RT_ELEMENTS(aFDs), -1);
821 if (rc < 0)
822 {
823 AssertMsgFailed(("poll failed with rc=%d\n", RTErrConvertFromErrno(errno)));
824 return RTErrConvertFromErrno(errno);
825 }
826
827 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
828 break;
829 if (rc > 0 && aFDs[1].revents)
830 {
831 if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
832 break;
833 /* notification to terminate -- drain the pipe */
834 char ch;
835 size_t cbRead;
836 RTPipeRead(pThis->hWakeupPipeR, &ch, 1, &cbRead);
837 continue;
838 }
839
840 /* Interrupt occurred. */
841 rc = pThis->pDrvHostParallelPort->pfnNotifyInterrupt(pThis->pDrvHostParallelPort);
842 AssertRC(rc);
843 }
844
845 return VINF_SUCCESS;
846}
847
848/**
849 * Unblock the monitor thread so it can respond to a state change.
850 *
851 * @returns a VBox status code.
852 * @param pDrvIns The driver instance.
853 * @param pThread The send thread.
854 */
855static DECLCALLBACK(int) drvHostParallelWakeupMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
856{
857 RT_NOREF(pThread);
858 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
859 size_t cbIgnored;
860 return RTPipeWrite(pThis->hWakeupPipeW, "", 1, &cbIgnored);
861}
862
863# endif /* VBOX_WITH_WIN_PARPORT_SUP */
864
865/**
866 * Destruct a host parallel driver instance.
867 *
868 * Most VM resources are freed by the VM. This callback is provided so that
869 * any non-VM resources can be freed correctly.
870 *
871 * @param pDrvIns The driver instance data.
872 */
873static DECLCALLBACK(void) drvHostParallelDestruct(PPDMDRVINS pDrvIns)
874{
875 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
876 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
877
878#ifndef VBOX_WITH_WIN_PARPORT_SUP
879 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
880 if (pThis->hFileDevice != NIL_RTFILE)
881 ioctl(RTFileToNative(pThis->hFileDevice), PPRELEASE);
882
883 if (pThis->hWakeupPipeW != NIL_RTPIPE)
884 {
885 int rc = RTPipeClose(pThis->hWakeupPipeW); AssertRC(rc);
886 pThis->hWakeupPipeW = NIL_RTPIPE;
887 }
888
889 if (pThis->hWakeupPipeR != NIL_RTPIPE)
890 {
891 int rc = RTPipeClose(pThis->hWakeupPipeR); AssertRC(rc);
892 pThis->hWakeupPipeR = NIL_RTPIPE;
893 }
894
895 if (pThis->hFileDevice != NIL_RTFILE)
896 {
897 int rc = RTFileClose(pThis->hFileDevice); AssertRC(rc);
898 pThis->hFileDevice = NIL_RTFILE;
899 }
900
901 if (pThis->pszDevicePath)
902 {
903 MMR3HeapFree(pThis->pszDevicePath);
904 pThis->pszDevicePath = NULL;
905 }
906#endif /* !VBOX_WITH_WIN_PARPORT_SUP */
907}
908
909/**
910 * Construct a host parallel driver instance.
911 *
912 * @copydoc FNPDMDRVCONSTRUCT
913 */
914static DECLCALLBACK(int) drvHostParallelConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
915{
916 RT_NOREF(fFlags);
917 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
918 LogFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
919 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
920
921
922 /*
923 * Init basic data members and interfaces.
924 *
925 * Must be done before returning any failure because we've got a destructor.
926 */
927 pThis->hFileDevice = NIL_RTFILE;
928#ifndef VBOX_WITH_WIN_PARPORT_SUP
929 pThis->hWakeupPipeR = NIL_RTPIPE;
930 pThis->hWakeupPipeW = NIL_RTPIPE;
931#endif
932
933 pThis->pDrvInsR3 = pDrvIns;
934#ifdef VBOX_WITH_DRVINTNET_IN_R0
935 pThis->pDrvInsR0 = PDMDRVINS_2_R0PTR(pDrvIns);
936#endif
937
938 /* IBase. */
939 pDrvIns->IBase.pfnQueryInterface = drvHostParallelQueryInterface;
940 /* IHostParallelConnector. */
941 pThis->IHostParallelConnectorR3.pfnWrite = drvHostParallelWrite;
942 pThis->IHostParallelConnectorR3.pfnRead = drvHostParallelRead;
943 pThis->IHostParallelConnectorR3.pfnSetPortDirection = drvHostParallelSetPortDirection;
944 pThis->IHostParallelConnectorR3.pfnWriteControl = drvHostParallelWriteControl;
945 pThis->IHostParallelConnectorR3.pfnReadControl = drvHostParallelReadControl;
946 pThis->IHostParallelConnectorR3.pfnReadStatus = drvHostParallelReadStatus;
947
948 /*
949 * Validate the config.
950 */
951 if (!CFGMR3AreValuesValid(pCfg, "DevicePath\0"))
952 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
953 N_("Unknown host parallel configuration option, only supports DevicePath"));
954
955 /*
956 * Query configuration.
957 */
958 /* Device */
959 int rc = CFGMR3QueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
960 if (RT_FAILURE(rc))
961 {
962 AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
963 return rc;
964 }
965
966 /*
967 * Open the device
968 */
969 /** @todo exclusive access on windows? */
970 rc = RTFileOpen(&pThis->hFileDevice, pThis->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
971 if (RT_FAILURE(rc))
972 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Parallel#%d could not open '%s'"),
973 pDrvIns->iInstance, pThis->pszDevicePath);
974
975#ifndef VBOX_WITH_WIN_PARPORT_SUP
976 /*
977 * Try to get exclusive access to parallel port
978 */
979 rc = ioctl(RTFileToNative(pThis->hFileDevice), PPEXCL);
980 if (rc < 0)
981 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
982 N_("Parallel#%d could not get exclusive access for parallel port '%s'"
983 "Be sure that no other process or driver accesses this port"),
984 pDrvIns->iInstance, pThis->pszDevicePath);
985
986 /*
987 * Claim the parallel port
988 */
989 rc = ioctl(RTFileToNative(pThis->hFileDevice), PPCLAIM);
990 if (rc < 0)
991 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
992 N_("Parallel#%d could not claim parallel port '%s'"
993 "Be sure that no other process or driver accesses this port"),
994 pDrvIns->iInstance, pThis->pszDevicePath);
995
996 /*
997 * Get the IHostParallelPort interface of the above driver/device.
998 */
999 pThis->pDrvHostParallelPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTPARALLELPORT);
1000 if (!pThis->pDrvHostParallelPort)
1001 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Parallel#%d has no parallel port interface above"),
1002 pDrvIns->iInstance);
1003
1004 /*
1005 * Create wakeup pipe.
1006 */
1007 rc = RTPipeCreate(&pThis->hWakeupPipeR, &pThis->hWakeupPipeW, 0 /*fFlags*/);
1008 AssertRCReturn(rc, rc);
1009
1010 /*
1011 * Start in SPP mode.
1012 */
1013 pThis->enmModeCur = PDM_PARALLEL_PORT_MODE_INVALID;
1014 rc = drvHostParallelSetMode(pThis, PDM_PARALLEL_PORT_MODE_SPP);
1015 if (RT_FAILURE(rc))
1016 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot change mode of parallel mode to SPP"), pDrvIns->iInstance);
1017
1018 /*
1019 * Start waiting for interrupts.
1020 */
1021 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pMonitorThread, pThis, drvHostParallelMonitorThread, drvHostParallelWakeupMonitorThread, 0,
1022 RTTHREADTYPE_IO, "ParMon");
1023 if (RT_FAILURE(rc))
1024 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot create monitor thread"), pDrvIns->iInstance);
1025
1026#else /* VBOX_WITH_WIN_PARPORT_SUP */
1027
1028 pThis->PortDirectData = 0;
1029 pThis->PortDirectControl = 0;
1030 pThis->PortDirectStatus = 0;
1031 rc = drvHostParallelGetWinHostIoPorts(pThis);
1032 if (RT_FAILURE(rc))
1033 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1034 N_("HostParallel#%d: Could not get direct access to the host parallel port!! (rc=%Rrc)"),
1035 pDrvIns->iInstance, rc);
1036
1037#endif /* VBOX_WITH_WIN_PARPORT_SUP */
1038 return VINF_SUCCESS;
1039}
1040
1041
1042/**
1043 * Char driver registration record.
1044 */
1045const PDMDRVREG g_DrvHostParallel =
1046{
1047 /* u32Version */
1048 PDM_DRVREG_VERSION,
1049 /* szName */
1050 "HostParallel",
1051 /* szRCMod */
1052 "",
1053 /* szR0Mod */
1054 "VBoxDDR0.r0",
1055 /* pszDescription */
1056 "Parallel host driver.",
1057 /* fFlags */
1058# if defined(VBOX_WITH_WIN_PARPORT_SUP)
1059 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DRVREG_FLAGS_R0,
1060# else
1061 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1062# endif
1063 /* fClass. */
1064 PDM_DRVREG_CLASS_CHAR,
1065 /* cMaxInstances */
1066 ~0U,
1067 /* cbInstance */
1068 sizeof(DRVHOSTPARALLEL),
1069 /* pfnConstruct */
1070 drvHostParallelConstruct,
1071 /* pfnDestruct */
1072 drvHostParallelDestruct,
1073 /* pfnRelocate */
1074 NULL,
1075 /* pfnIOCtl */
1076 NULL,
1077 /* pfnPowerOn */
1078 NULL,
1079 /* pfnReset */
1080 NULL,
1081 /* pfnSuspend */
1082 NULL,
1083 /* pfnResume */
1084 NULL,
1085 /* pfnAttach */
1086 NULL,
1087 /* pfnDetach */
1088 NULL,
1089 /* pfnPowerOff */
1090 NULL,
1091 /* pfnSoftReset */
1092 NULL,
1093 /* u32EndVersion */
1094 PDM_DRVREG_VERSION
1095};
1096#endif /*IN_RING3*/
1097
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use