DrvChar.cpp revision aa32d4906f2f685992091893d5abdf27a2352a85
/** @file
*
* VBox stream I/O devices:
* Generic char driver
*/
/*
* 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 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_CHAR
#include <iprt/semaphore.h>
#include "Builtins.h"
/** Size of the send fifo queue (in bytes) */
#define CHAR_MAX_SEND_QUEUE 0x80
#define CHAR_MAX_SEND_QUEUE_MASK 0x7f
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Char driver instance data.
*/
typedef struct DRVCHAR
{
/** Pointer to the driver instance structure. */
/** Pointer to the stream interface of the driver below us. */
/** Our char interface. */
/** Flag to notify the receive thread it should terminate. */
volatile bool fShutdown;
/** Receive thread ID. */
/** Send thread ID. */
/** Send event semephore */
/** Internal send FIFO queue */
/** Converts a pointer to DRVCHAR::IChar to a PDRVCHAR. */
#define PDMICHAR_2_DRVCHAR(pInterface) ( (PDRVCHAR)((uintptr_t)pInterface - RT_OFFSETOF(DRVCHAR, IChar)) )
/* -=-=-=-=- IBase -=-=-=-=- */
/**
* Queries an interface to the driver.
*
* @returns Pointer to interface.
* @returns NULL if the interface was not supported by the driver.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
*/
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
case PDMINTERFACE_CHAR:
default:
return NULL;
}
}
/* -=-=-=-=- IChar -=-=-=-=- */
/** @copydoc PDMICHAR::pfnWrite */
{
{
}
return VINF_SUCCESS;
}
/** @copydoc PDMICHAR::pfnSetParameters */
static DECLCALLBACK(int) drvCharSetParameters(PPDMICHAR pInterface, int speed, int parity, int data_bits, int stop_bits)
{
LogFlow(("%s: speed=%d parity=%c data_bits=%d stop_bits=%d\n", __FUNCTION__, speed, parity, data_bits, stop_bits));
return VINF_SUCCESS;
}
/* -=-=-=-=- receive thread -=-=-=-=- */
/**
* Send thread loop.
*
* @returns 0 on success.
* @param ThreadSelf Thread handle to this thread.
* @param pvUser User argument.
*/
{
for(;;)
{
if (VBOX_FAILURE(rc))
break;
/*
* Write the character to the attached stream (if present).
*/
&& pData->pDrvStream)
{
{
rc = pData->pDrvStream->pfnWrite(pData->pDrvStream, &pData->aSendQueue[pData->iSendQueueTail], &cbProcessed);
if (VBOX_SUCCESS(rc))
{
pData->iSendQueueTail++;
}
else if (rc == VERR_TIMEOUT)
{
/* Normal case, just means that the stream didn't accept a new
* character before the timeout elapsed. Just retry. */
rc = VINF_SUCCESS;
}
else
{
break;
}
}
}
else
break;
}
return VINF_SUCCESS;
}
/* -=-=-=-=- receive thread -=-=-=-=- */
/**
* Receive thread loop.
*
* @returns 0 on success.
* @param ThreadSelf Thread handle to this thread.
* @param pvUser User argument.
*/
{
int rc;
cbRemaining = 0;
{
if (!cbRemaining)
{
/* Get block of data from stream driver. */
if (pData->pDrvStream)
{
cbRemaining = sizeof(aBuffer);
if (VBOX_FAILURE(rc))
{
break;
}
}
else
{
cbRemaining = 0;
RTThreadSleep(100);
}
}
else
{
/* Send data to guest. */
if (VBOX_SUCCESS(rc))
{
pBuffer += cbProcessed;
}
else if (rc == VERR_TIMEOUT)
{
/* Normal case, just means that the guest didn't accept a new
* character before the timeout elapsed. Just retry. */
rc = VINF_SUCCESS;
}
else
{
break;
}
}
}
return VINF_SUCCESS;
}
/* -=-=-=-=- driver interface -=-=-=-=- */
/**
* Construct a char driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed,
* pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to
* obtain the configuration of the driver instance. It's
* also found in pDrvIns->pCfgHandle as it's expected to
* be used frequently in this function.
*/
{
/*
* Init basic data members and interfaces.
*/
/* IBase. */
/* IChar. */
/*
*/
pData->pDrvCharPort = (PPDMICHARPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_CHAR_PORT);
if (!pData->pDrvCharPort)
return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE, RT_SRC_POS, N_("Char#%d has no char port interface above"), pDrvIns->iInstance);
/*
* Attach driver below and query its stream interface.
*/
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d failed to attach driver below"), pDrvIns->iInstance);
if (!pData->pDrvStream)
return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW, RT_SRC_POS, N_("Char#%d has no stream interface below"), pDrvIns->iInstance);
rc = RTThreadCreate(&pData->ReceiveThread, drvCharReceiveLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "Char Receive");
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create receive thread"), pDrvIns->iInstance);
rc = RTThreadCreate(&pData->SendThread, drvCharSendLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "Char Send");
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("Char#%d cannot create send thread"), pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes written", "/Devices/Char%d/Written", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes read", "/Devices/Char%d/Read", pDrvIns->iInstance);
return VINF_SUCCESS;
}
/**
* Destruct a char driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that
* any non-VM resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
{
if (pData->ReceiveThread)
{
}
/* Empty the send queue */
if (pData->SendThread)
{
}
}
/**
* Char driver registration record.
*/
{
/* u32Version */
/* szDriverName */
"Char",
/* pszDescription */
"Generic char driver.",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVCHAR),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/** pfnPowerOff */
};