DevParallel.cpp revision 4fbca3751fe239da5934c23a783c3422618336e8
/* $Id$ */
/** @file
* VirtualBox Parallel Device Emulation.
*
* Contributed by: Alexander Eichner
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/* based on DevSerial.cpp */
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_PARALLEL
#include <iprt/semaphore.h>
#include <iprt/critsect.h>
#include "Builtins.h"
#define PARALLEL_SAVED_STATE_VERSION 1
/* defines for accessing the register bits */
#define LPT_STATUS_BUSY 0x80
#define LPT_STATUS_ACK 0x40
#define LPT_STATUS_PAPER_OUT 0x20
#define LPT_STATUS_SELECT_IN 0x10
#define LPT_STATUS_ERROR 0x08
#define LPT_STATUS_IRQ 0x04
#define LPT_STATUS_EPP_TIMEOUT 0x01
#define LPT_CONTROL_ENABLE_BIDIRECT 0x20
#define LPT_CONTROL_ENABLE_IRQ_VIA_ACK 0x10
#define LPT_CONTROL_SELECT_PRINTER 0x08
#define LPT_CONTROL_RESET 0x04
#define LPT_CONTROL_AUTO_LINEFEED 0x02
#define LPT_CONTROL_STROBE 0x01
/** mode defines for the extended control register */
#define LPT_ECP_ECR_CHIPMODE_MASK 0xe0
#define LPT_ECP_ECR_CHIPMODE_CONFIGURATION 0x07
#define LPT_ECP_ECR_CHIPMODE_FIFO_TEST 0x06
#define LPT_ECP_ECR_CHIPMODE_RESERVED 0x05
#define LPT_ECP_ECR_CHIPMODE_EPP 0x04
#define LPT_ECP_ECR_CHIPMODE_ECP_FIFO 0x03
#define LPT_ECP_ECR_CHIPMODE_PP_FIFO 0x02
#define LPT_ECP_ECR_CHIPMODE_BYTE 0x01
#define LPT_ECP_ECR_CHIPMODE_COMPAT 0x00
/** FIFO status bits in extended control register */
#define LPT_ECP_ECR_FIFO_MASK 0x03
#define LPT_ECP_ECR_FIFO_SOME_DATA 0x00
#define LPT_ECP_ECR_FIFO_FULL 0x02
#define LPT_ECP_ECR_FIFO_EMPTY 0x01
#define LPT_ECP_CONFIGA_FIFO_WITDH_MASK 0x70
#define LPT_ECP_CONFIGA_FIFO_WIDTH_16 0x00
#define LPT_ECP_CONFIGA_FIFO_WIDTH_32 0x20
#define LPT_ECP_CONFIGA_FIFO_WIDTH_8 0x10
#define LPT_ECP_FIFO_DEPTH 2
typedef struct ParallelState
{
/** Access critical section. */
/** Pointer to the device instance. */
/** Pointer to the device instance. */
#endif
/** The base interface. */
/** The host device port interface. */
/** Pointer to the attached base driver. */
/** Pointer to the attached host device. */
/** The ECP FIFO implementation*/
int act_fifo_pos_write;
int act_fifo_pos_read;
int irq;
bool fGCEnabled;
bool fR0Enabled;
bool afAlignment[6];
typedef DEVPARALLELSTATE ParallelState;
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
#define PDMIHOSTPARALLELPORT_2_PARALLELSTATE(pInstance) ( (ParallelState *)((uintptr_t)(pInterface) - RT_OFFSETOF(ParallelState, IHostParallelPort)) )
#define PDMIHOSTDEVICEPORT_2_PARALLELSTATE(pInstance) ( (ParallelState *)((uintptr_t)(pInterface) - RT_OFFSETOF(ParallelState, IHostDevicePort)) )
#define PDMIBASE_2_PARALLELSTATE(pInstance) ( (ParallelState *)((uintptr_t)(pInterface) - RT_OFFSETOF(ParallelState, IBase)) )
PDMBOTHCBDECL(int) parallelIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) parallelIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
#if 0
PDMBOTHCBDECL(int) parallelIOPortReadECP(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
PDMBOTHCBDECL(int) parallelIOPortWriteECP(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
#endif
static void parallel_set_irq(ParallelState *s)
{
if (s->reg_control & LPT_CONTROL_ENABLE_IRQ_VIA_ACK)
{
}
}
static void parallel_clear_irq(ParallelState *s)
{
}
{
unsigned char ch;
addr &= 7;
switch(addr) {
default:
case 0:
#ifndef IN_RING3
return VINF_IOM_HC_IOPORT_WRITE;
#else
if (RT_LIKELY(s->pDrvHostParallelConnector))
{
}
#endif
break;
case 1:
break;
case 2:
/** Set the reserved bits to one */
if (ch != s->reg_control) {
#ifndef IN_RING3
return VINF_IOM_HC_IOPORT_WRITE;
#else
s->reg_control = val;
#endif
}
break;
case 3:
s->reg_epp_addr = val;
break;
case 4:
s->reg_epp_data = val;
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
return VINF_SUCCESS;
}
{
*pRC = VINF_SUCCESS;
addr &= 7;
switch(addr) {
default:
case 0:
if (!(s->reg_control & LPT_CONTROL_ENABLE_BIDIRECT))
else
{
#ifndef IN_RING3
#else
if (RT_LIKELY(s->pDrvHostParallelConnector))
{
int rc = s->pDrvHostParallelConnector->pfnRead(s->pDrvHostParallelConnector, &s->reg_data, &cbRead);
}
#endif
}
break;
case 1:
#ifndef IN_RING3
#else
if (RT_LIKELY(s->pDrvHostParallelConnector))
{
}
ret = s->reg_status;
#endif
break;
case 2:
ret = s->reg_control;
break;
case 3:
ret = s->reg_epp_addr;
break;
case 4:
ret = s->reg_epp_data;
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
return ret;
}
#if 0
{
unsigned char ch;
addr &= 7;
switch(addr) {
default:
case 0:
s->act_fifo_pos_write++;
if (s->act_fifo_pos_write < LPT_ECP_FIFO_DEPTH) {
/** FIFO has some data (clear both FIFO bits) */
} else {
/** FIFO is full */
/** Clear FIFO empty bit */
s->reg_ecp_ecr &= ~LPT_ECP_ECR_FIFO_EMPTY;
/** Set FIFO full bit */
s->reg_ecp_ecr |= LPT_ECP_ECR_FIFO_FULL;
s->act_fifo_pos_write = 0;
}
} else {
s->reg_ecp_base_plus_400h = ch;
}
break;
case 1:
s->reg_ecp_config_b = ch;
break;
case 2:
/** If we change the mode clear FIFO */
/** reset the fifo */
s->act_fifo_pos_write = 0;
s->act_fifo_pos_read = 0;
/** Set FIFO empty bit */
/** Clear FIFO full bit */
s->reg_ecp_ecr &= ~LPT_ECP_ECR_FIFO_FULL;
}
/** Set new mode */
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
return VINF_SUCCESS;
}
{
*pRC = VINF_SUCCESS;
addr &= 7;
switch(addr) {
default:
case 0:
s->act_fifo_pos_read++;
if (s->act_fifo_pos_read == LPT_ECP_FIFO_DEPTH)
s->act_fifo_pos_read = 0; /** end of FIFO, start at beginning */
if (s->act_fifo_pos_read == s->act_fifo_pos_write) {
/** FIFO is empty */
/** Set FIFO empty bit */
/** Clear FIFO full bit */
s->reg_ecp_ecr &= ~LPT_ECP_ECR_FIFO_FULL;
} else {
/** FIFO has some data (clear all FIFO bits) */
}
} else {
ret = s->reg_ecp_base_plus_400h;
}
break;
case 1:
ret = s->reg_ecp_config_b;
break;
case 2:
ret = s->reg_ecp_ecr;
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
return ret;
}
#endif
#ifdef IN_RING3
{
return VINF_SUCCESS;
}
#endif /* IN_RING3 */
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
if (rc == VINF_SUCCESS)
{
}
}
else
return rc;
}
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
if (rc == VINF_SUCCESS)
{
}
}
else
return rc;
}
#if 0
/**
* Port I/O Handler for OUT operations on ECP registers.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
if (rc == VINF_SUCCESS)
{
}
}
else
return rc;
}
/**
* Port I/O Handler for IN operations on ECP registers.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument.
* @param Port Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
{
int rc = VINF_SUCCESS;
if (cb == 1)
{
if (rc == VINF_SUCCESS)
{
}
}
else
return rc;
}
#endif
#ifdef IN_RING3
/**
* Saves a state of the serial port device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to save the state to.
*/
{
}
/**
* Loads a saved serial port device state.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to the saved state.
* @param u32Version The data unit version number.
*/
{
int rc;
{
}
if (VBOX_FAILURE(rc))
return rc;
if (u32 != ~0U)
{
}
return VINF_SUCCESS;
}
/**
* @copydoc FNPDMDEVRELOCATE
*/
{
}
/** @copyfrom PIBASE::pfnqueryInterface */
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pData->IHostParallelPort;
default:
return NULL;
}
}
/**
* Destruct a device instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
return VINF_SUCCESS;
}
/**
* Construct a device instance for a VM.
*
* @returns VBox status.
* @param pDevIns The device instance data.
* If the registration structure is needed, pDevIns->pDevReg points to it.
* @param iInstance Instance number. Use this to figure out which registers and such to use.
* The device number is also found in pDevIns->iInstance, but since it's
* likely to be freqently used PDM passes it as parameter.
* @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
* of the device instance. It's also found in pDevIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
int iInstance,
{
int rc;
/*
* Validate configuration.
*/
N_("Configuration error: Unknown config key"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pData->fGCEnabled = true;
else if (VBOX_FAILURE(rc))
N_("Configuration error: Failed to get the \"GCEnabled\" value"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
pData->fR0Enabled = true;
else if (VBOX_FAILURE(rc))
N_("Configuration error: Failed to get the \"R0Enabled\" value"));
/* IBase */
/* IHostParallelPort */
/*
* Initialize critical section.
* This must of course be done before attaching drivers or anything else which can call us back..
*/
char szName[24];
if (VBOX_FAILURE(rc))
return rc;
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
irq_lvl = 7;
else if (VBOX_FAILURE(rc))
N_("Configuration error: Failed to get the \"IRQ\" value"));
if (rc == VERR_CFGM_VALUE_NOT_FOUND)
io_base = 0x378;
else if (VBOX_FAILURE(rc))
N_("Configuration error: Failed to get the \"IOBase\" value"));
/* Init parallel state */
pData->act_fifo_pos_read = 0;
pData->act_fifo_pos_write = 0;
if (VBOX_FAILURE(rc))
return rc;
#if 0
/* register ecp registers */
if (VBOX_FAILURE(rc))
return rc;
#endif
if (pData->fGCEnabled)
{
if (VBOX_FAILURE(rc))
return rc;
#if 0
if (VBOX_FAILURE(rc))
return rc;
#endif
}
if (pData->fR0Enabled)
{
if (VBOX_FAILURE(rc))
return rc;
#if 0
if (VBOX_FAILURE(rc))
return rc;
#endif
}
/* Attach the parallel port driver and get the interfaces. For now no run-time
* changes are supported. */
if (VBOX_SUCCESS(rc))
{
pData->pDrvHostParallelConnector = (PDMIHOSTPARALLELCONNECTOR *)pData->pDrvBase->pfnQueryInterface(pData->pDrvBase,
if (!pData->pDrvHostParallelConnector)
{
return VERR_PDM_MISSING_INTERFACE;
}
/** @todo provide read notification interface!!!! */
}
else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
{
}
else
{
}
/** Set compatibility mode */
pData->pDrvHostParallelConnector->pfnSetMode(pData->pDrvHostParallelConnector, PDM_PARALLEL_PORT_MODE_COMPAT);
/** Get status of control register */
pData->pDrvHostParallelConnector->pfnReadControl(pData->pDrvHostParallelConnector, &pData->reg_control);
pDevIns, /* pDevIns */
iInstance, /* u32Instance */
PARALLEL_SAVED_STATE_VERSION, /* u32Version */
sizeof (*pData), /* cbGuess */
NULL, /* pfnSavePrep */
parallelSaveExec, /* pfnSaveExec */
NULL, /* pfnSaveDone */
NULL, /* pfnLoadPrep */
parallelLoadExec, /* pfnLoadExec */
NULL /* pfnLoadDone */
);
if (VBOX_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceParallelPort =
{
/* u32Version */
/* szDeviceName */
"parallel",
/* szGCMod */
"VBoxDDGC.gc",
/* szR0Mod */
"VBoxDDR0.r0",
/* pszDescription */
"Parallel Communication Port",
/* fFlags */
PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GC | PDM_DEVREG_FLAGS_R0,
/* fClass */
/* cMaxInstances */
1,
/* cbInstance */
sizeof(ParallelState),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
};
#endif /* IN_RING3 */
#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */