USBProxyServiceLinux.cpp revision bb0bf5f0d07a55839823656d524b6af71abac5c7
/** @file
*
* VBox frontends: Basic Frontend (BFE):
* Implementation of USBProxyServiceLinux class
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung 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 *
*******************************************************************************/
#include "USBProxyService.h"
#include "Logging.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#ifndef NO_COMPILER_H
# include <linux/compiler.h>
#endif
#include <linux/usbdevice_fs.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Suffix translation. */
typedef struct USBSUFF
{
char szSuff[4];
unsigned cchSuff;
unsigned uMul;
unsigned uDiv;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Suffixes for the endpoint polling interval.
*/
static const USBSUFF s_aIntervalSuff[] =
{
{ "ms", 2, 1, 0 },
{ "us", 2, 1, 1000 },
{ "ns", 2, 1, 1000000 },
{ "s", 1, 1000, 0 },
{ "", 0, 0, 0 } /* term */
};
/**
* Initialize data members.
*/
USBProxyServiceLinux::USBProxyServiceLinux (HostUSB *aHost, const char *aUsbfsRoot /* = "/proc/bus/usb" */)
{
LogFlowMember (("USBProxyServiceLinux::USBProxyServiceLinux: aHost=%p aUsbfsRoot=%p:{%s}\n", aHost, aUsbfsRoot, aUsbfsRoot));
/*
* Open the devices file.
*/
int rc = VERR_NO_MEMORY;
char *pszDevices;
if (pszDevices)
{
if (VBOX_SUCCESS (rc))
{
/*
* Check that we're actually on the usbfs.
*/
{
{
int pipes[2];
{
mWakeupPipeR = pipes[0];
if (mStream)
{
/*
* Start the poller thread.
*/
if (VBOX_SUCCESS (rc))
{
LogFlowMember (("USBProxyServiceLinux::USBProxyServiceLinux: returns successfully - mFile=%d mStream=%p mWakeupPipeR/W=%d/%d\n",
return;
}
mFile = NIL_RTFILE;
}
}
}
else
{
Log (("USBProxyServiceLinux::USBProxyServiceLinux: StFS.f_type=%d expected=%d\n", StFS.f_type, USBDEVICE_SUPER_MAGIC));
}
}
else
{
}
RTFileClose (mFile);
mFile = NIL_RTFILE;
}
else
{
#ifndef DEBUG_fm3
/* I'm currently using Linux with disabled USB support */
#endif
}
}
else
Log (("USBProxyServiceLinux::USBProxyServiceLinux: out of memory!\n"));
mLastError = rc;
}
/**
* Stop all service threads and free the device chain.
*/
{
LogFlowMember (("USBProxyServiceLinux::~USBProxyServiceLinux:\n"));
/*
* Stop the service.
*/
if (isActive())
stop();
/*
* Free resources.
*/
if (mStream)
{
mFile = NIL_RTFILE;
}
else if (mFile != NIL_RTFILE)
{
RTFileClose (mFile);
mFile = NIL_RTFILE;
}
}
{
/*
* Don't think we need to do anything when the device is held...
*/
return VINF_SUCCESS;
}
{
/*
* This isn't really implemented, we can usually wrestle
* any user when we need it... Anyway, I don't have anywhere to store
* any info per device atm.
*/
return VINF_SUCCESS;
}
{
/*
* We're not really holding it atm.
*/
return VINF_SUCCESS;
}
{
/*
* We don't dare reset anything, but the USB Proxy Device
* will reset upon detach, so this should be ok.
*/
return VINF_SUCCESS;
}
{
if (rc == 0)
return VERR_TIMEOUT;
if (rc > 0)
return VINF_SUCCESS;
return RTErrConvertFromErrno (errno);
}
int USBProxyServiceLinux::interruptWait (void)
{
if (VBOX_SUCCESS (rc))
return rc;
}
/**
* "reads" the number suffix. It's more like validating it and
* skipping the necessary number of chars.
*/
static int usbReadSkipSuffix (char **ppszNext)
{
{
/* skip unit */
pszNext += 2;
pszNext += 2;
/* skip parenthesis */
if (*pszNext == '(')
{
if (!pszNext++)
{
return VERR_PARSE_ERROR;
}
}
/* blank or end of the line. */
{
return VERR_PARSE_ERROR;
}
/* it's ok. */
}
return VINF_SUCCESS;
}
/**
* Reads a USB number returning the number and the position of the next character to parse.
*/
static int usbReadNum (const char *pszValue, unsigned uBase, uint32_t u32Mask, PCUSBSUFF paSuffs, void *pvNum, char **ppszNext)
{
/*
* Initialize return value to zero and strip leading spaces.
*/
switch (u32Mask)
{
}
if (*pszValue)
{
/*
* Try convert the number.
*/
char *pszNext;
{
return VERR_NO_DATA;
}
/*
* Check the range.
*/
{
return VERR_OUT_OF_RANGE;
}
/*
* Validate and skip stuff following the number.
*/
if (paSuffs)
{
{
{
{
else
break;
}
}
}
}
else
{
if (VBOX_FAILURE (rc))
return rc;
}
/*
* Set the value.
*/
switch (u32Mask)
{
}
}
return VINF_SUCCESS;
}
{
}
{
}
static int usbRead16Suff (const char *pszValue, unsigned uBase, PCUSBSUFF paSuffs, uint16_t *pu16, char **ppszNext)
{
}
/**
* Reads a USB BCD number returning the number and the position of the next character to parse.
* The returned number contains the integer part in the high byte and the decimal part in the low byte.
*/
{
/*
* Initialize return value to zero and strip leading spaces.
*/
*pu16 = 0;
if (*pszValue)
{
/*
* Try convert the number.
*/
/* integer part */
char *pszNext;
{
return VERR_NO_DATA;
}
if (u32Int & ~0xff)
{
return VERR_OUT_OF_RANGE;
}
/* skip dot and read decimal part */
if (*pszNext != '.')
{
return VERR_PARSE_ERROR;
}
{
return VERR_NO_DATA;
}
if (u32Dec & ~0xff)
{
return VERR_OUT_OF_RANGE;
}
/*
* Validate and skip stuff following the number.
*/
if (VBOX_FAILURE (rc))
return rc;
/*
* Set the value.
*/
}
return VINF_SUCCESS;
}
/**
* Reads a string, i.e. allocates memory and copies it.
*
* We assume that a string is pure ASCII, if that's not the case
* tell me how to figure out the codeset please.
*/
{
if (*ppsz)
if (*ppsz)
return VINF_SUCCESS;
return VERR_NO_MEMORY;
}
/**
* Skips the current property.
*/
static char * usbReadSkip (const char *pszValue)
{
if (psz)
if (!psz)
psz--;
return psz;
}
/**
* Compare a prefix and returns pointer to the char following it if it matches.
*/
{
return NULL;
}
/**
* Checks which state the device is in.
*/
{
return USBDEVICESTATE_UNSUPPORTED;
/*
* We cannot distinguish between USED_BY_HOST_CAPTURABLE and
* USED_BY_GUEST, HELD_BY_PROXY all that well and it shouldn't be
* necessary either.
*/
{
if (pszDriver)
{
{
break;
}
}
}
return enmState;
}
{
if (mStream)
{
int cHits = 0;
int iCfg = 0;
int iEp = 0;
char szLine[1024];
int rc = VINF_SUCCESS;
while ( VBOX_SUCCESS (rc)
{
char *psz;
char *pszValue;
/* validate and remove the trailing newline. */
{
continue;
}
/* strip */
if (!*psz)
continue;
/*
* Interpret the line.
* (Ordered by normal occurence.)
*/
continue;
switch (ch)
{
/*
* T: Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=ddd MxCh=dd
* | | | | | | | | |__MaxChildren
* | | | | | | | |__Device Speed in Mbps
* | | | | | | |__DeviceNumber
* | | | | | |__Count of devices at this level
* | | | |__Parent DeviceNumber
* | | |__Level in topology for this bus
* | |__Bus number
* |__Topology info tag
*/
case 'T':
/* add */
if (cHits >= 3)
{
if (pDev)
{
{
RTStrAPrintf((char **)&pDev->pszAddress, "%s/%03d/%03d", mUsbfsRoot.c_str(), pDev->bBus, pDev->bDevNum);
if (pDev->pszAddress)
{
if (ppNext)
else
}
else
{
freeDevice (pDev);
rc = VERR_NO_MEMORY;
}
}
else
freeDevice (pDev);
}
else
rc = VERR_NO_MEMORY;
}
/* Reset device state */
cHits = 1;
iCfg = 0;
iEp = 0;
/* parse the line. */
{
if (PREFIX ("Bus="))
else if (PREFIX ("Lev="))
else if (PREFIX ("Dev#="))
else if (PREFIX ("Prnt="))
else if (PREFIX ("Port="))
else if (PREFIX ("Cnt="))
//else if (PREFIX ("Spd="))
// rc = usbReadSpeed (pszValue, &Dev.cbSpeed, &psz);
else if (PREFIX ("MxCh="))
else
}
break;
/*
* Bandwidth info:
* | | | |__Number of isochronous requests
* | | |__Number of interrupt requests
* | |__Total Bandwidth allocated to this bus
* |__Bandwidth info tag
*/
case 'B':
break;
/*
* D: Ver=x.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd
* | | | | | | |__NumberConfigurations
* | | | | | |__MaxPacketSize of Default Endpoint
* | | | | |__DeviceProtocol
* | | | |__DeviceSubClass
* | | |__DeviceClass
* | |__Device USB version
* |__Device info tag #1
*/
case 'D':
{
if (PREFIX ("Ver="))
else if (PREFIX ("Cls="))
else if (PREFIX ("Sub="))
else if (PREFIX ("Prot="))
//else if (PREFIX ("MxPS="))
// rc = usbRead16 (pszValue, 10, &Dev.wMaxPacketSize, &psz);
else if (PREFIX ("#Cfgs="))
else
}
cHits++;
break;
/*
* P: Vendor=xxxx ProdID=xxxx Rev=xx.xx
* | | | |__Product revision number
* | | |__Product ID code
* | |__Vendor ID code
* |__Device info tag #2
*/
case 'P':
{
if (PREFIX ("Vendor="))
else if (PREFIX ("ProdID="))
else if (PREFIX ("Rev="))
else
}
cHits++;
break;
/*
* String.
*/
case 'S':
if (PREFIX ("Manufacturer="))
else if (PREFIX ("Product="))
else if (PREFIX ("SerialNumber="))
{
if (VBOX_SUCCESS (rc))
}
break;
/*
* C:* #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA
* | | | | | |__MaxPower in mA
* | | | | |__Attributes
* | | | |__ConfiguratioNumber
* | | |__NumberOfInterfaces
* | |__ "*" indicates the active configuration (others are " ")
* |__Config info tag
*/
case 'C':
{
{
if (PREFIX ("#Ifs="))
else if (PREFIX ("Cfg#="))
else if (PREFIX ("Atr="))
else if (PREFIX ("MxPwr="))
else
}
if (VBOX_SUCCESS (rc))
{
{
/* Add the config. */
if (!Dev.paConfigurations)
{
if (pCfg)
{
iCfg = 1;
}
else
rc = VERR_NO_MEMORY;
}
else
{
iCfg++;
}
}
else
{
}
}
/* new config, so, start anew with interfaces and endpoints. */
iEp = 0;
break;
}
/*
* I: If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=ssss
* | | | | | | | |__Driver name
* | | | | | | | or "(none)"
* | | | | | | |__InterfaceProtocol
* | | | | | |__InterfaceSubClass
* | | | | |__InterfaceClass
* | | | |__NumberOfEndpoints
* | | |__AlternateSettingNumber
* | |__InterfaceNumber
* |__Interface info tag
*/
case 'I':
{
USBINTERFACE If = {0};
{
if (PREFIX ("If#="))
else if (PREFIX ("Alt="))
else if (PREFIX ("#EPs="))
else if (PREFIX ("Cls="))
else if (PREFIX ("Sub="))
else if (PREFIX ("Prot="))
else if (PREFIX ("Driver="))
{
{
}
break;
}
else
}
if (VBOX_SUCCESS (rc))
{
{
/* Add the config. */
if (!pCfg->paInterfaces)
{
if (pIf)
{
}
else
rc = VERR_NO_MEMORY;
}
else
{
/*
* Alternate settings makes life *difficult*!
* ASSUMES: ORDER ASC bInterfaceNumber, bAlternateSetting
*/
if (!If.bAlternateSetting)
else
{
if (paAlts)
{
}
else
rc = VERR_NO_MEMORY;
}
}
}
else
{
AssertMsgFailed (("iCfg=%d bInterfaceNumber=%d bNumInterfaces=%d\n", iCfg, If.bInterfaceNumber, pCfg->bNumInterfaces));
}
}
/* start anew with endpoints. */
iEp = 0;
break;
}
/*
* E: Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=dddms
* | | | | |__Interval (max) between transfers
* | | | |__EndpointMaxPacketSize
* | | |__Attributes(EndpointType)
* | |__EndpointAddress(I=In,O=Out)
* |__Endpoint info tag
*/
case 'E':
{
USBENDPOINT Ep = {0};
{
if (PREFIX ("Ad="))
else if (PREFIX ("Atr="))
else if (PREFIX ("MxPS="))
else if (PREFIX ("Ivl="))
else
}
if (VBOX_SUCCESS (rc))
{
{
/* Add the config. */
if (!pIf->paEndpoints)
{
if (pEp)
{
iEp = 1;
}
else
rc = VERR_NO_MEMORY;
}
else
{
iEp++;
}
}
else
{
AssertMsgFailed (("iCfg=%d bInterfaceNumber=%d iEp=%d bNumInterfaces=%d\n", iCfg, pIf->bInterfaceNumber, iEp, pIf->bNumEndpoints));
}
}
break;
}
}
} /* parse loop */
/*
* Add the current entry.
*/
if (cHits >= 3)
{
if (pDev)
{
{
RTStrAPrintf((char **)&pDev->pszAddress, "%s/%03d/%03d", mUsbfsRoot.c_str(), pDev->bBus, pDev->bDevNum);
if (pDev->pszAddress)
{
if (ppNext)
else
}
else
{
rc = VERR_NO_MEMORY;
freeDevice (pDev);
}
}
else
freeDevice (pDev);
}
else
rc = VERR_NO_MEMORY;
}
/*
* Success?
*/
if (VBOX_FAILURE (rc))
{
while (pFirst)
{
freeDevice (pFree);
}
}
}
return pFirst;
}