DrvHostSerial.cpp revision 3e04f45aeb634f1256d0510780bf77d4fdefd926
/** $Id: $ */
/** @file
* VBox stream I/O devices: Host serial driver
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_HOST_SERIAL
#include <iprt/semaphore.h>
#ifdef RT_OS_LINUX
# include <termios.h>
# include <fcntl.h>
# include <string.h>
# include <unistd.h>
#elif defined(RT_OS_WINDOWS)
# include <windows.h>
#endif
#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 DRVHOSTSERIAL
{
/** Pointer to the driver instance structure. */
/** Our char interface. */
/** Flag to notify the receive thread it should terminate. */
volatile bool fShutdown;
/** Receive thread ID. */
/** Send thread ID. */
/** Send event semephore */
/** the device path */
char *pszDevicePath;
/** the device handle */
/** Internal send FIFO queue */
/** Converts a pointer to DRVCHAR::IChar to a PDRVHOSTSERIAL. */
#define PDMICHAR_2_DRVHOSTSERIAL(pInterface) ( (PDRVHOSTSERIAL)((uintptr_t)pInterface - RT_OFFSETOF(DRVHOSTSERIAL, 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.
*/
static DECLCALLBACK(void *) drvHostSerialQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
case PDMINTERFACE_CHAR:
default:
return NULL;
}
}
/* -=-=-=-=- IChar -=-=-=-=- */
/** @copydoc PDMICHAR::pfnWrite */
static DECLCALLBACK(int) drvHostSerialWrite(PPDMICHAR pInterface, const void *pvBuf, size_t cbWrite)
{
{
}
return VINF_SUCCESS;
}
static DECLCALLBACK(int) drvHostSerialSetParameters(PPDMICHAR pInterface, unsigned Bps, char chParity, unsigned cDataBits, unsigned cStopBits)
{
#ifdef RT_OS_LINUX
struct termios *termiosSetup;
int baud_rate;
#elif defined(RT_OS_WINDOWS)
#endif
LogFlow(("%s: Bps=%u chParity=%c cDataBits=%u cStopBits=%u\n", __FUNCTION__, Bps, chParity, cDataBits, cStopBits));
#ifdef RT_OS_LINUX
/* Enable receiver */
switch (Bps) {
case 50:
break;
case 75:
break;
case 110:
break;
case 134:
break;
case 150:
break;
case 200:
break;
case 300:
break;
case 600:
break;
case 1200:
break;
case 1800:
break;
case 2400:
break;
case 4800:
break;
case 9600:
break;
case 19200:
break;
case 38400:
break;
case 57600:
break;
case 115200:
break;
default:
}
switch (chParity) {
case 'E':
break;
case 'O':
break;
case 'N':
break;
default:
break;
}
switch (cDataBits) {
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
default:
break;
}
switch (cStopBits) {
case 2:
default:
break;
}
/* set serial port to raw input */
#elif defined(RT_OS_WINDOWS)
switch (Bps) {
case 110:
break;
case 300:
break;
case 600:
break;
case 1200:
break;
case 2400:
break;
case 4800:
break;
case 9600:
break;
case 14400:
break;
case 19200:
break;
case 38400:
break;
case 57600:
break;
case 115200:
break;
default:
}
switch (chParity) {
case 'E':
break;
case 'O':
break;
case 'N':
break;
default:
break;
}
switch (cStopBits) {
case 1:
break;
case 2:
break;
default:
break;
}
#endif /* RT_OS_WINDOWS */
return VINF_SUCCESS;
}
/* -=-=-=-=- receive thread -=-=-=-=- */
/**
* Send thread loop.
*
* @returns VINF_SUCCESS.
* @param ThreadSelf Thread handle to this thread.
* @param pvUser User argument.
*/
{
{
if (VBOX_FAILURE(rc))
break;
/*
* Write the character to the host device.
*/
{
unsigned cbProcessed = 1;
if (VBOX_SUCCESS(rc))
{
pData->iSendQueueTail++;
}
else if (VBOX_FAILURE(rc))
{
break;
}
}
}
return VINF_SUCCESS;
}
/* -=-=-=-=- receive thread -=-=-=-=- */
/**
* Receive thread loop.
*
* This thread pushes data from the host serial device up the driver
* chain toward the serial device.
*
* @returns VINF_SUCCESS.
* @param ThreadSelf Thread handle to this thread.
* @param pvUser User argument.
*/
{
int rc = VINF_SUCCESS;
{
if (!cbRemaining)
{
/* Get a block of data from the host serial device. */
if (VBOX_FAILURE(rc))
{
break;
}
}
else
{
/* Send data to the guest. */
if (VBOX_SUCCESS(rc))
{
pbBuffer += 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. */
/*
* Query configuration.
*/
/* Device */
if (VBOX_FAILURE(rc))
{
return rc;
}
/*
* Open the device
*/
if (VBOX_FAILURE(rc)) {
switch (rc) {
case VERR_ACCESS_DENIED:
#ifdef RT_OS_LINUX
"the group settings of the current user"),
#else
"of that device"),
#endif
default:
N_("Failed to open host device '%s'"),
}
}
/* Set to non blocking I/O */
#ifdef RT_OS_LINUX
#elif defined(RT_OS_WINDOWS)
/* Set the COMMTIMEOUTS to get non blocking I/O */
#endif
/*
*/
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_("HostSerial#%d has no char port interface above"), pDrvIns->iInstance);
rc = RTThreadCreate(&pData->ReceiveThread, drvHostSerialReceiveLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "Char Receive");
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostSerial#%d cannot create receive thread"), pDrvIns->iInstance);
rc = RTThreadCreate(&pData->SendThread, drvHostSerialSendLoop, (void *)pData, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "Serial Send");
if (VBOX_FAILURE(rc))
return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, N_("HostSerial#%d cannot create send thread"), pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes written", "/Devices/HostSerial%d/Written", pDrvIns->iInstance);
PDMDrvHlpSTAMRegisterF(pDrvIns, &pData->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_BYTES, "Nr of bytes read", "/Devices/HostSerial%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 (RT_FAILURE(rc))
}
/* Empty the send queue */
{
if (RT_FAILURE(rc))
}
}
/**
* Char driver registration record.
*/
const PDMDRVREG g_DrvHostSerial =
{
/* u32Version */
/* szDriverName */
"Host Serial",
/* pszDescription */
"Host serial driver.",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVHOSTSERIAL),
/* pfnConstruct */
/* pfnDestruct */
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL,
/** pfnPowerOff */
};