VirtualBox

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

Last change on this file since 25985 was 25985, checked in by vboxsync, 15 years ago

pdmifs.h: the final batch of refactored interface ID code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.7 KB
Line 
1/* $Id: DrvHostParallel.cpp 25985 2010-01-23 00:51:04Z vboxsync $ */
2/** @file
3 * VirtualBox Host Parallel Port Driver.
4 *
5 * Contributed by: Alexander Eichner
6 */
7
8/*
9 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.215389.xyz. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#define LOG_GROUP LOG_GROUP_DRV_HOST_PARALLEL
28#include <VBox/pdmdrv.h>
29#include <VBox/pdmthread.h>
30#include <iprt/asm.h>
31#include <iprt/assert.h>
32#include <iprt/file.h>
33#include <iprt/semaphore.h>
34#include <iprt/stream.h>
35#include <iprt/uuid.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#include "Builtins.h"
50
51
52/*******************************************************************************
53* Structures and Typedefs *
54*******************************************************************************/
55/**
56 * Host parallel port driver instance data.
57 * @implements PDMIHOSTPARALLELCONNECTOR
58 */
59typedef struct DRVHOSTPARALLEL
60{
61 /** Pointer to the driver instance structure. */
62 PPDMDRVINS pDrvIns;
63 /** Pointer to the char port interface of the driver/device above us. */
64 PPDMIHOSTPARALLELPORT pDrvHostParallelPort;
65 /** Our host device interface. */
66 PDMIHOSTPARALLELCONNECTOR IHostParallelConnector;
67 /** Device Path */
68 char *pszDevicePath;
69 /** Device Handle */
70 RTFILE FileDevice;
71 /** Thread waiting for interrupts. */
72 PPDMTHREAD pMonitorThread;
73 /** Wakeup pipe read end. */
74 RTFILE WakeupPipeR;
75 /** Wakeup pipe write end. */
76 RTFILE WakeupPipeW;
77} DRVHOSTPARALLEL, *PDRVHOSTPARALLEL;
78
79/** Converts a pointer to DRVHOSTPARALLEL::IHostDeviceConnector to a PDRHOSTPARALLEL. */
80#define PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface) ( (PDRVHOSTPARALLEL)((uintptr_t)pInterface - RT_OFFSETOF(DRVHOSTPARALLEL, IHostParallelConnector)) )
81
82
83/* -=-=-=-=- IBase -=-=-=-=- */
84
85/**
86 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
87 */
88static DECLCALLBACK(void *) drvHostParallelQueryInterface(PPDMIBASE pInterface, const char *pszIID)
89{
90 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
91 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
92
93 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
94 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTPARALLELCONNECTOR, &pThis->IHostParallelConnector);
95 return NULL;
96}
97
98/* -=-=-=-=- IHostDeviceConnector -=-=-=-=- */
99
100/** @copydoc PDMICHARCONNECTOR::pfnWrite */
101static DECLCALLBACK(int) drvHostParallelWrite(PPDMIHOSTPARALLELCONNECTOR pInterface, const void *pvBuf, size_t *cbWrite)
102{
103 PDRVHOSTPARALLEL pThis = PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface);
104 const unsigned char *pBuffer = (const unsigned char *)pvBuf;
105
106 LogFlow(("%s: pvBuf=%#p cbWrite=%d\n", __FUNCTION__, pvBuf, *cbWrite));
107
108 ioctl(pThis->FileDevice, PPWDATA, pBuffer);
109 *cbWrite = 1;
110
111 return VINF_SUCCESS;
112}
113
114static DECLCALLBACK(int) drvHostParallelRead(PPDMIHOSTPARALLELCONNECTOR pInterface, void *pvBuf, size_t *cbRead)
115{
116 PDRVHOSTPARALLEL pThis = PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface);
117 unsigned char *pBuffer = (unsigned char *)pvBuf;
118
119 LogFlow(("%s: pvBuf=%#p cbRead=%d\n", __FUNCTION__, pvBuf, cbRead));
120
121 ioctl(pThis->FileDevice, PPRDATA, pBuffer);
122 *cbRead = 1;
123
124 return VINF_SUCCESS;
125}
126
127static DECLCALLBACK(int) drvHostParallelSetMode(PPDMIHOSTPARALLELCONNECTOR pInterface, PDMPARALLELPORTMODE enmMode)
128{
129 PDRVHOSTPARALLEL pThis = PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface);
130 int ppdev_mode;
131
132 LogFlow(("%s: mode=%d\n", __FUNCTION__, enmMode));
133
134 switch (enmMode) {
135 case PDM_PARALLEL_PORT_MODE_COMPAT:
136 ppdev_mode = IEEE1284_MODE_COMPAT;
137 break;
138 case PDM_PARALLEL_PORT_MODE_EPP:
139 ppdev_mode = IEEE1284_MODE_EPP;
140 break;
141 case PDM_PARALLEL_PORT_MODE_ECP:
142 //ppdev_mode = IEEE1284_MODE_ECP;
143 break;
144 }
145
146 ioctl(pThis->FileDevice, PPSETMODE, &ppdev_mode);
147
148 return VINF_SUCCESS;
149}
150
151static DECLCALLBACK(int) drvHostParallelWriteControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t fReg)
152{
153 PDRVHOSTPARALLEL pThis = PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface);
154
155 LogFlow(("%s: fReg=%d\n", __FUNCTION__, fReg));
156
157 ioctl(pThis->FileDevice, PPWCONTROL, &fReg);
158
159 return VINF_SUCCESS;
160}
161
162static DECLCALLBACK(int) drvHostParallelReadControl(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
163{
164 PDRVHOSTPARALLEL pThis = PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface);
165 uint8_t fReg;
166
167 ioctl(pThis->FileDevice, PPRCONTROL, &fReg);
168
169 LogFlow(("%s: fReg=%d\n", __FUNCTION__, fReg));
170
171 *pfReg = fReg;
172
173 return VINF_SUCCESS;
174}
175
176static DECLCALLBACK(int) drvHostParallelReadStatus(PPDMIHOSTPARALLELCONNECTOR pInterface, uint8_t *pfReg)
177{
178 PDRVHOSTPARALLEL pThis = PDMIHOSTPARALLELCONNECTOR_2_DRVHOSTPARALLEL(pInterface);
179 uint8_t fReg;
180
181 ioctl(pThis->FileDevice, PPRSTATUS, &fReg);
182
183 LogFlow(("%s: fReg=%d\n", __FUNCTION__, fReg));
184
185 *pfReg = fReg;
186
187 return VINF_SUCCESS;
188}
189
190static DECLCALLBACK(int) drvHostParallelMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
191{
192 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
193 struct pollfd aFDs[2];
194
195 /*
196 * We can wait for interrupts using poll on linux hosts.
197 */
198 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
199 {
200 int rc;
201
202 aFDs[0].fd = pThis->FileDevice;
203 aFDs[0].events = POLLIN;
204 aFDs[0].revents = 0;
205 aFDs[1].fd = pThis->WakeupPipeR;
206 aFDs[1].events = POLLIN | POLLERR | POLLHUP;
207 aFDs[1].revents = 0;
208 rc = poll(aFDs, RT_ELEMENTS(aFDs), -1);
209 if (rc < 0)
210 {
211 AssertMsgFailed(("poll failed with rc=%d\n", RTErrConvertFromErrno(errno)));
212 return RTErrConvertFromErrno(errno);
213 }
214
215 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
216 break;
217 if (rc > 0 && aFDs[1].revents)
218 {
219 if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
220 break;
221 /* notification to terminate -- drain the pipe */
222 char ch;
223 size_t cbRead;
224 RTFileRead(pThis->WakeupPipeR, &ch, 1, &cbRead);
225 continue;
226 }
227
228 /* Interrupt occurred. */
229 rc = pThis->pDrvHostParallelPort->pfnNotifyInterrupt(pThis->pDrvHostParallelPort);
230 AssertRC(rc);
231 }
232
233 return VINF_SUCCESS;
234}
235
236/**
237 * Unblock the monitor thread so it can respond to a state change.
238 *
239 * @returns a VBox status code.
240 * @param pDrvIns The driver instance.
241 * @param pThread The send thread.
242 */
243static DECLCALLBACK(int) drvHostParallelWakeupMonitorThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
244{
245 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
246
247 return RTFileWrite(pThis->WakeupPipeW, "", 1, NULL);
248}
249
250/**
251 * Construct a host parallel driver instance.
252 *
253 * @copydoc FNPDMDRVCONSTRUCT
254 */
255static DECLCALLBACK(int) drvHostParallelConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
256{
257 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
258 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
259
260 /*
261 * Validate the config.
262 */
263 if (!CFGMR3AreValuesValid(pCfgHandle, "DevicePath\0"))
264 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
265 N_("Unknown host parallel configuration option, only supports DevicePath"));
266
267 /*
268 * Init basic data members and interfaces.
269 */
270
271 /* IBase. */
272 pDrvIns->IBase.pfnQueryInterface = drvHostParallelQueryInterface;
273 /* IHostParallelConnector. */
274 pThis->IHostParallelConnector.pfnWrite = drvHostParallelWrite;
275 pThis->IHostParallelConnector.pfnRead = drvHostParallelRead;
276 pThis->IHostParallelConnector.pfnSetMode = drvHostParallelSetMode;
277 pThis->IHostParallelConnector.pfnWriteControl = drvHostParallelWriteControl;
278 pThis->IHostParallelConnector.pfnReadControl = drvHostParallelReadControl;
279 pThis->IHostParallelConnector.pfnReadStatus = drvHostParallelReadStatus;
280
281 /*
282 * Query configuration.
283 */
284 /* Device */
285 int rc = CFGMR3QueryStringAlloc(pCfgHandle, "DevicePath", &pThis->pszDevicePath);
286 if (RT_FAILURE(rc))
287 {
288 AssertMsgFailed(("Configuration error: query for \"DevicePath\" string returned %Rra.\n", rc));
289 return rc;
290 }
291
292 /*
293 * Open the device
294 */
295 rc = RTFileOpen(&pThis->FileDevice, pThis->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
296 if (RT_FAILURE(rc))
297 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Parallel#%d could not open '%s'"),
298 pDrvIns->iInstance, pThis->pszDevicePath);
299
300 /*
301 * Try to get exclusive access to parallel port
302 */
303 rc = ioctl(pThis->FileDevice, PPEXCL);
304 if (rc < 0)
305 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
306 N_("Parallel#%d could not get exclusive access for parallel port '%s'"
307 "Be sure that no other process or driver accesses this port"),
308 pDrvIns->iInstance, pThis->pszDevicePath);
309
310 /*
311 * Claim the parallel port
312 */
313 rc = ioctl(pThis->FileDevice, PPCLAIM);
314 if (rc < 0)
315 return PDMDrvHlpVMSetError(pDrvIns, RTErrConvertFromErrno(errno), RT_SRC_POS,
316 N_("Parallel#%d could not claim parallel port '%s'"
317 "Be sure that no other process or driver accesses this port"),
318 pDrvIns->iInstance, pThis->pszDevicePath);
319
320 /*
321 * Get the IHostParallelPort interface of the above driver/device.
322 */
323 pThis->pDrvHostParallelPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTPARALLELPORT);
324 if (!pThis->pDrvHostParallelPort)
325 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Parallel#%d has no parallel port interface above"),
326 pDrvIns->iInstance);
327
328 /*
329 * Create wakeup pipe.
330 */
331 int aFDs[2];
332 if (pipe(aFDs) != 0)
333 {
334 rc = RTErrConvertFromErrno(errno);
335 AssertRC(rc);
336 return rc;
337 }
338 pThis->WakeupPipeR = aFDs[0];
339 pThis->WakeupPipeW = aFDs[1];
340
341 /*
342 * Start waiting for interrupts.
343 */
344 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pMonitorThread, pThis, drvHostParallelMonitorThread, drvHostParallelWakeupMonitorThread, 0,
345 RTTHREADTYPE_IO, "ParMon");
346 if (RT_FAILURE(rc))
347 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostParallel#%d cannot create monitor thread"), pDrvIns->iInstance);
348
349 return VINF_SUCCESS;
350}
351
352
353/**
354 * Destruct a host parallel driver instance.
355 *
356 * Most VM resources are freed by the VM. This callback is provided so that
357 * any non-VM resources can be freed correctly.
358 *
359 * @param pDrvIns The driver instance data.
360 */
361static DECLCALLBACK(void) drvHostParallelDestruct(PPDMDRVINS pDrvIns)
362{
363 PDRVHOSTPARALLEL pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTPARALLEL);
364
365 LogFlow(("%s: iInstance=%d\n", __FUNCTION__, pDrvIns->iInstance));
366
367 ioctl(pThis->FileDevice, PPRELEASE);
368
369 if (pThis->WakeupPipeW != NIL_RTFILE)
370 {
371 int rc = RTFileClose(pThis->WakeupPipeW);
372 AssertRC(rc);
373 pThis->WakeupPipeW = NIL_RTFILE;
374 }
375 if (pThis->WakeupPipeR != NIL_RTFILE)
376 {
377 int rc = RTFileClose(pThis->WakeupPipeR);
378 AssertRC(rc);
379 pThis->WakeupPipeR = NIL_RTFILE;
380 }
381 if (pThis->FileDevice != NIL_RTFILE)
382 {
383 int rc = RTFileClose(pThis->FileDevice);
384 AssertRC(rc);
385 pThis->FileDevice = NIL_RTFILE;
386 }
387}
388
389/**
390 * Char driver registration record.
391 */
392const PDMDRVREG g_DrvHostParallel =
393{
394 /* u32Version */
395 PDM_DRVREG_VERSION,
396 /* szDriverName */
397 "HostParallel",
398 /* szRCMod */
399 "",
400 /* szR0Mod */
401 "",
402 /* pszDescription */
403 "Parallel host driver.",
404 /* fFlags */
405 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
406 /* fClass. */
407 PDM_DRVREG_CLASS_CHAR,
408 /* cMaxInstances */
409 ~0,
410 /* cbInstance */
411 sizeof(DRVHOSTPARALLEL),
412 /* pfnConstruct */
413 drvHostParallelConstruct,
414 /* pfnDestruct */
415 drvHostParallelDestruct,
416 /* pfnRelocate */
417 NULL,
418 /* pfnIOCtl */
419 NULL,
420 /* pfnPowerOn */
421 NULL,
422 /* pfnReset */
423 NULL,
424 /* pfnSuspend */
425 NULL,
426 /* pfnResume */
427 NULL,
428 /* pfnAttach */
429 NULL,
430 /* pfnDetach */
431 NULL,
432 /* pfnPowerOff */
433 NULL,
434 /* pfnSoftReset */
435 NULL,
436 /* u32EndVersion */
437 PDM_DRVREG_VERSION
438};
439
Note: See TracBrowser for help on using the repository browser.

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