USBProxyDevice-linux.cpp revision 9576fc6de57fa406fdcc1600a55480e997b27444
/* $Id$ */
/** @file
* USB device proxy - the Linux backend.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Define NO_PORT_RESET to skip the slow and broken linux port reset.
* Resetting will break PalmOne. */
#define NO_PORT_RESET
/** Define NO_LOGICAL_RECONNECT to skip the broken logical reconnect handling. */
#define NO_LOGICAL_RECONNECT
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_USBPROXY
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifdef VBOX_WITH_LINUX_COMPILER_H
# include <linux/compiler.h>
#endif
#include <linux/usbdevice_fs.h>
/*
* Backlevel 2.4 headers doesn't have these two defines.
* They were added some time between 2.4.21 and 2.4.26, probably in 2.4.23.
*/
#ifndef USBDEVFS_DISCONNECT
#endif
#ifndef USBDEVFS_URB_SHORT_NOT_OK
# define USBDEVFS_URB_SHORT_NOT_OK 0 /* rhel3 doesn't have this. darn! */
#endif
/* FedoraCore 4 does not have the bit defined by default. */
#ifndef POLLWRNORM
# define POLLWRNORM 0x0100
#endif
#ifndef RDESKTOP
#else
# define RTCRITSECT void *
static inline int rtcsNoop() { return VINF_SUCCESS; }
# define RTCritSectInit(a) rtcsNoop()
# define RTCritSectDelete(a) rtcsNoop()
# define RTCritSectEnter(a) rtcsNoop()
# define RTCritSectLeave(a) rtcsNoop()
#endif
#if defined(NO_PORT_RESET) && !defined(NO_LOGICAL_RECONNECT)
#endif
#include "../USBProxyDevice.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Wrapper around the linux urb request structure.
* This is required to track in-flight and landed URBs.
*/
typedef struct USBPROXYURBLNX
{
/** The kernel URB data */
struct usbdevfs_urb KUrb;
/** Space filler for the isochronous packets. */
/** The millisecond timestamp when this URB was submitted. */
/** Pointer to the next linux URB. */
struct USBPROXYURBLNX *pNext;
/** Pointer to the previous linux URB. */
struct USBPROXYURBLNX *pPrev;
/** If we've split the VUSBURB up into multiple linux URBs, this is points to the head. */
struct USBPROXYURBLNX *pSplitHead;
/** The next linux URB if split up. */
struct USBPROXYURBLNX *pSplitNext;
/** Whether it has timed out and should be shot down on the next failing reap call. */
bool fTimedOut;
/** Indicates that this URB has been canceled by timeout and should return an CRC error. */
bool fCanceledByTimedOut;
/** Don't report these back. */
bool fCanceledBySubmit;
/** This split element is reaped. */
bool fSplitElementReaped;
/** Size to transfer in remaining fragments of a split URB */
/**
* Data for the linux usb proxy backend.
*/
typedef struct USBPROXYDEVLNX
{
/** The open file. */
/** Critical section protecting the two lists. */
/** The list of free linux URBs. Singly linked. */
/** The list of active linux URBs. Doubly linked.
* We must maintain this so we can properly reap URBs of a detached device.
* Only the split head will appear in this list. */
/** The list of landed linux URBs. Doubly linked.
* Only the split head will appear in this list. */
/** The tail of the landed linux URBs. */
/** Are we using sysfs to find the active configuration? */
bool fUsingSysfs;
/** Pipe handle for waiking up - writing end. */
/** Pipe handle for waiking up - reading end. */
* Used to figure out the configuration after a reset. */
char *pszPath;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int usbProxyLinuxDoIoCtl(PUSBPROXYDEV pProxyDev, unsigned long iCmd, void *pvArg, bool fHandleNoDev, uint32_t cTries);
static int usbProxyLinuxFindActiveConfig(PUSBPROXYDEV pProxyDev, const char *pszPath, int *piFirstCfg);
/**
* Wrapper for the ioctl call.
*
* This wrapper will repeat the call if we get an EINTR or EAGAIN. It can also
* handle ENODEV (detached device) errors.
*
* @returns whatever ioctl returns.
* @param pProxyDev The proxy device.
* @param iCmd The ioctl command / function.
* @param pvArg The ioctl argument / data.
* @param fHandleNoDev Whether to handle ENODEV.
* @param cTries The number of retries. Use UINT32_MAX for (kind of) indefinite retries.
* @internal
*/
static int usbProxyLinuxDoIoCtl(PUSBPROXYDEV pProxyDev, unsigned long iCmd, void *pvArg, bool fHandleNoDev, uint32_t cTries)
{
int rc;
do
{
do
{
if (rc >= 0)
return rc;
{
break;
}
break;
} while (cTries-- > 0);
return rc;
}
/**
* The device has been unplugged.
* Cancel all in-flight URBs and put them up for reaping.
*/
{
/*
* Shoot down all flying URBs.
*/
while (pUrbLnx)
{
ioctl(RTFileToNative(pDevLnx->hFile), USBDEVFS_DISCARDURB, &pCur->KUrb); /* not sure if this is required.. */
/* insert into the taxing list. */
if ( !pCur->pSplitHead
{
if (pUrbTaxing)
pUrbTaxing = pCur;
}
else
}
/* Append the URBs we shot down to the taxing queue. */
if (pUrbTaxing)
{
if (pUrbTaxing->pPrev)
else
}
}
/**
* Set the connect state seen by kernel drivers
* @internal
*/
{
if ( iIf >= 32
{
struct usbdevfs_ioctl IoCtl;
if (!fQuiet)
LogFlow(("usbProxyLinuxSetConnected: pProxyDev=%s iIf=%#x fConnect=%s\n",
&& !fQuiet)
Log(("usbProxyLinuxSetConnected: failure, errno=%d. pProxyDev=%s\n",
}
}
/**
* Allocates a linux URB request structure.
* @returns Pointer to an active URB request.
* @returns NULL on failure.
* @param pProxyDev The proxy device instance.
* @param pSplitHead The split list head if allocating for a split list.
*/
{
/*
* Try remove a linux URB from the free list, if none there allocate a new one.
*/
if (pUrbLnx)
else
{
if (!pUrbLnx)
return NULL;
}
pUrbLnx->fCanceledByTimedOut = false;
pUrbLnx->fCanceledBySubmit = false;
pUrbLnx->fSplitElementReaped = false;
/*
* Link it into the active list
*/
if (!pSplitHead)
{
}
else
return pUrbLnx;
}
/**
* Frees a linux URB request structure.
*
* @param pProxyDev The proxy device instance.
* @param pUrbLnx The linux URB to free.
*/
{
/*
* Remove from the active list.
*/
if ( !pUrbLnx->pSplitHead
{
else
}
/*
* Link it into the free list.
*/
}
/**
* Frees split list of a linux URB request structure.
*
* @param pProxyDev The proxy device instance.
* @param pUrbLnx A linux URB to in the split list to be freed.
*/
{
while (pUrbLnx)
{
}
}
/**
* the config with an asterix.
*
* @returns The Cfg#.
* @returns -1 if no active config.
* @param pszDevNode The path to the device. We infere the location of
* the devices file, which bus and device number we're
* looking for.
* @param iFirstCfg The first configuration. (optional)
* @internal
*/
static int usbProxyLinuxFindActiveConfigUsbfs(PUSBPROXYDEV pProxyDev, const char *pszDevNode, int *piFirstCfg)
{
/*
* Set return defaults.
*/
int iActiveCfg = -1;
if (piFirstCfg)
*piFirstCfg = 1;
/*
* Parse the usbfs device node path and turn it into a path to the "devices" file,
* picking up the device number and bus along the way.
*/
/* the device number */
while (*psz != '/')
psz--;
if (RT_SUCCESS(rc))
{
/* the bus number */
*psz-- = '\0';
while (*psz != '/')
psz--;
if (RT_SUCCESS(rc))
{
/*
* Open and scan the devices file.
* We're ASSUMING that each device starts off with a 'T:' line.
*/
if (RT_SUCCESS(rc))
{
char szLine[1024];
{
/* we're only interested in 'T:' lines. */
continue;
/* Skip ahead to 'Bus' and compare */
char *pszNext;
uint32_t u;
if (RT_FAILURE(rc))
continue;
if (u != uBus)
continue;
/* Skip ahead to 'Dev#' and compare */
if (!psz)
continue;
if (RT_FAILURE(rc))
continue;
if (u != uDev)
continue;
/*
* Ok, we've found the device.
* Scan until we find a selected configuration, the next device, or EOF.
*/
{
if (psz[0] == 'T')
break;
continue;
if (!fActive && !piFirstCfg)
continue;
/* Get the 'Cfg#' value. */
if (psz)
{
if (RT_SUCCESS(rc))
{
if (piFirstCfg)
{
*piFirstCfg = u;
piFirstCfg = NULL;
}
if (fActive)
iActiveCfg = u;
}
}
if (fActive)
break;
}
break;
}
}
}
}
return iActiveCfg;
}
/**
* This finds the active configuration from sysfs.
*
* @returns The Cfg#.
* @returns -1 if no active config.
* @param pszPath The sysfs path for the device.
* @param piFirstCfg The first configuration. (optional)
* @internal
*/
static int usbProxyLinuxFindActiveConfigSysfs(PUSBPROXYDEV pProxyDev, const char *pszPath, int *piFirstCfg)
{
#ifdef VBOX_USB_WITH_SYSFS
if (piFirstCfg != NULL)
: 1;
#else /* !VBOX_USB_WITH_SYSFS */
return -1;
#endif /* !VBOX_USB_WITH_SYSFS */
}
/**
* This finds the active configuration.
*
* @returns The Cfg#.
* @returns -1 if no active config.
* @param pszPath The sysfs path for the device, or the usbfs device
* node path.
* @param iFirstCfg The first configuration. (optional)
* @internal
*/
static int usbProxyLinuxFindActiveConfig(PUSBPROXYDEV pProxyDev, const char *pszPath, int *piFirstCfg)
{
if (pDevLnx->fUsingSysfs)
}
/**
* Extracts the Linux file descriptor associated with the kernel USB device.
* This is used by rdesktop-vrdp for polling for events.
* @returns the FD, or asserts and returns -1 on error
* @param pProxyDev The device instance
*/
{
}
/**
* Opens the device file.
*
* @returns VBox status code.
* @param pProxyDev The device instance.
* @param pszAddress If we are using usbfs, this is the path to the
* device. If we are using sysfs, this is a string of
* the form "sysfs:<sysfs path>//device:<device node>".
* In the second case, the two paths are guaranteed
* not to contain the substring "//".
* @param pvBackend Backend specific pointer, unused for the linux backend.
*/
static DECLCALLBACK(int) usbProxyLinuxOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend)
{
const char *pszDevNode;
const char *pszPath;
bool fUsingSysfs;
/*
* Are we using sysfs or usbfs?
*/
#ifdef VBOX_USB_WITH_SYSFS
if (fUsingSysfs)
{
if (!pszDevNode)
{
return VERR_INVALID_PARAMETER;
}
}
else
#endif /* VBOX_USB_WITH_SYSFS */
{
#ifndef VBOX_USB_WITH_SYSFS
fUsingSysfs = false;
#endif
}
/*
* Try open the device node.
*/
if (RT_SUCCESS(rc))
{
/*
* Initialize the linux backend data.
*/
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
LogFlow(("usbProxyLinuxOpen(%p, %s): returns successfully File=%RTfile iActiveCfg=%d\n",
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
}
else if (rc == VERR_ACCESS_DENIED)
RTErrGetShort(rc)));
return rc;
}
/**
* Claims all the interfaces and figures out the
* current configuration.
*
* @returns VINF_SUCCESS.
* @param pProxyDev The proxy device.
*/
{
/*
* Brute force rulez.
* usbProxyLinuxSetConnected check for masked interfaces.
*/
unsigned iIf;
/*
* Determine the active configuration.
*
* If there isn't any active configuration, we will get EHOSTUNREACH (113) errors
* when trying to read the device descriptors in usbProxyDevCreate. So, we'll make
* the first one active (usually 1) then.
*/
int iFirstCfg;
{
Log(("usbProxyLinuxInit: No active config! Tried to set %d: iActiveCfg=%d\n", iFirstCfg, pProxyDev->iActiveCfg));
}
else
return VINF_SUCCESS;
}
/**
* Closes the proxy device.
*/
{
/*
* Try put the device in a state which linux can cope with before we release it.
* Resetting it would be a nice start, although we must remember
* that it might have been disconnected...
*
* Don't reset if we're masking interfaces or if construction failed.
*/
{
/* ASSUMES: thread == EMT */
if ( pProxyDev->fMaskedIfs
{
/* Connect drivers. */
unsigned iIf;
}
else
}
/*
* Now we can free all the resources and close the device.
*/
{
if (pUrbLnx->pSplitHead)
{
while (pCur)
{
if ( !pFree->fSplitElementReaped
}
}
else
}
{
}
LogFlow(("usbProxyLinuxClose: returns\n"));
}
#if defined(NO_PORT_RESET) && !defined(NO_LOGICAL_RECONNECT)
/**
* Look for the logically reconnected device.
* After 5 seconds we'll give up.
*
* @returns VBox status code.
* @thread Reset thread or EMT.
*/
{
Log2(("usb_reset_logical_reconnect: pDev=%p:{.bBus=%#x, .bDevNum=%#x, .idVendor=%#x, .idProduct=%#x, .bcdDevice=%#x, .u64SerialHash=%#llx .bDevNumParent=%#x .bPort=%#x .bLevel=%#x}\n",
pDev, pDev->Info.bBus, pDev->Info.bDevNum, pDev->Info.idVendor, pDev->Info.idProduct, pDev->Info.bcdDevice,
/* First, let hubd get a chance to logically reconnect the device. */
if (!RTThreadYield())
RTThreadSleep(1);
/*
* Search for the new device address.
*/
pFile = get_devices_file();
if (!pFile)
return VERR_FILE_NOT_FOUND;
/*
* Loop until found or 5seconds have elapsed.
*/
for (;;) {
int rc;
char buf[512];
int got = 0;
struct usb_dev_entry id = {0};
/*
* Since this is kernel ABI we don't need to be too fussy about
* the parsing.
*/
AssertMsgFailed(("usb_reset_logical_reconnect: Line to long!!\n"));
break;
}
*psz = '\0';
switch ( buf[0] ) {
case 'T': /* topology */
/* Check if we've got enough for a device. */
if (got >= 2) {
Log2(("usb_reset_logical_reconnect: {.bBus=%#x, .bDevNum=%#x, .idVendor=%#x, .idProduct=%#x, .bcdDevice=%#x, .u64SerialHash=%#llx, .bDevNumParent=%#x, .bPort=%#x, .bLevel=%#x}\n",
id.bBus, id.bDevNum, id.idVendor, id.idProduct, id.bcdDevice, id.u64SerialHash, id.bDevNumParent, id.bPort, id.bLevel));
goto l_found;
}
}
/* restart */
got = 0;
/*T: Bus=04 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#= 3 Spd=1.5 MxCh= 0*/
break;
while ( *psz == ' ' )
psz++;
break;
break;
break;
break;
got++;
break;
case 'P': /* product */
break;
break;
break;
break;
got++;
break;
case 'S': /* String descriptor */
/* Skip past "S:" and then the whitespace */
if ( !RT_C_IS_SPACE(*psz) )
break;
/* If it is a serial number string, skip past
* "SerialNumber="
*/
break;
break;
}
}
/*
* Check last.
*/
if ( got >= 2
/* close the existing file descriptor. */
/* open stuff at the new address. */
return VINF_SUCCESS;
break;
}
/*
* Wait for a while and then check the file again.
*/
break; /* done */
if (rc < 0) {
}
} /* for loop */
return VERR_GENERAL_FAILURE;
}
#endif /* !NO_PORT_RESET && !NO_LOGICAL_RECONNECT */
/**
* Reset a device.
*
* @returns VBox status code.
* @param pDev The device to reset.
*/
{
#ifdef NO_PORT_RESET
/*
* Specific device resets are NOPs.
* Root hub resets that affects all devices are executed.
*
* The reasoning is that when a root hub reset is done, the guest shouldn't
* will have to re enumerate the devices after doing this kind of reset.
* So, it doesn't really matter if a device is 'logically disconnected'.
*/
if ( !fResetOnLinux
|| pProxyDev->fMaskedIfs)
else
{
{
Log(("usb-linux: Reset failed, rc=%s errno=%d.\n",
return RTErrConvertFromErrno(rc);
}
/* find the active config - damn annoying. */
}
#else /* !NO_PORT_RESET */
/*
* This is the alternative, we will always reset when asked to do so.
*
* The problem we're facing here is that on reset failure linux will do
* a 'logical reconnect' on the device. This will invalidate the current
* handle and we'll have to reopen the device. This is problematic to say
* the least, especially since it happens pretty often.
*/
# ifndef NO_LOGICAL_RECONNECT
# endif
{
# ifndef NO_LOGICAL_RECONNECT
{
/*
* This usually happens because of a 'logical disconnection'.
* So, we're in for a real treat from our excellent OS now...
*/
if (RT_FAILURE(rc2))
if (RT_SUCCESS(rc2))
{
LogFlow(("usbProxyLinuxReset: returns success (after recovering disconnected device!)\n"));
return VINF_SUCCESS;
}
}
# endif /* NO_LOGICAL_RECONNECT */
Log(("usb-linux: Reset failed, rc=%s errno=%d.\n",
return RTErrConvertFromErrno(rc);
}
# ifndef NO_LOGICAL_RECONNECT
# endif
LogFlow(("usbProxyLinuxReset: returns success\n"));
#endif /* !NO_PORT_RESET */
return VINF_SUCCESS;
}
/**
* SET_CONFIGURATION.
*
* The caller makes sure that it's not called first time after open or reset
* with the active interface.
*
* @returns success indicator.
* @param pProxyDev The device instance data.
* @param iCfg The configuration to set.
*/
{
LogFlow(("usbProxyLinuxSetConfig: pProxyDev=%s cfg=%#x\n",
{
return false;
}
return true;
}
/**
* Claims an interface.
* @returns success indicator.
*/
{
LogFlow(("usbProxyLinuxClaimInterface: pProxyDev=%s ifnum=%#x\n", usbProxyGetName(pProxyDev), iIf));
{
return false;
}
return true;
}
/**
* Releases an interface.
* @returns success indicator.
*/
{
LogFlow(("usbProxyLinuxReleaseInterface: pProxyDev=%s ifnum=%#x\n", usbProxyGetName(pProxyDev), iIf));
{
return false;
}
return true;
}
/**
* SET_INTERFACE.
*
* @returns success indicator.
*/
{
struct usbdevfs_setinterface SetIf;
{
return false;
}
return true;
}
/**
* Clears the halted endpoint 'EndPt'.
*/
{
LogFlow(("usbProxyLinuxClearHaltedEp: pProxyDev=%s EndPt=%u\n", usbProxyGetName(pProxyDev), EndPt));
{
/*
* Unfortunately this doesn't work on control pipes.
* Windows doing this on the default endpoint and possibly other pipes too,
* so we'll feign success for ENOENT errors.
*/
{
Log(("usb-linux: clear_halted_ep failed errno=%d. pProxyDev=%s ep=%d - IGNORED\n",
return true;
}
Log(("usb-linux: clear_halted_ep failed errno=%d. pProxyDev=%s ep=%d\n",
return false;
}
return true;
}
/**
* Setup packet byte-swapping routines.
*/
{
}
/**
* Clean up after a failed URB submit.
*/
static void usbProxyLinuxCleanupFailedSubmit(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx, PUSBPROXYURBLNX pCur, PVUSBURB pUrb, bool *pfUnplugged)
{
/* discard and reap later (walking with pUrbLnx). */
{
for (;;)
{
pUrbLnx->fCanceledBySubmit = true;
{
*pfUnplugged = true;
pUrbLnx->fSplitElementReaped = true;
else
LogRel(("USB: Failed to discard %p! errno=%d (pUrb=%p)\n", pUrbLnx->KUrb.usercontext, errno, pUrb)); /* serious! */
}
{
break;
}
}
}
/* free the unsubmitted ones. */
while (pCur)
{
}
/* send unplug event if we failed with ENODEV originally. */
if (*pfUnplugged)
}
/**
* Submit one URB through the usbfs IOCTL interface, with
* retries
*
* @returns true / false.
*/
static bool usbProxyLinuxSubmitURB(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pCur, PVUSBURB pUrb, bool *pfUnplugged)
{
unsigned cTries = 0;
{
continue;
{
*pfUnplugged = true;
return false;
}
Log(("usb-linux: Submit URB %p -> %d!!! type=%d ep=%#x buffer_length=%#x cTries=%d\n",
{
continue;
}
return false;
}
return true;
}
/** The split size. 16K in known Linux kernel versions. */
#define SPLIT_SIZE 0x4000
/**
* Create a URB fragment of up to SPLIT_SIZE size and hook it
* into the list of fragments.
*
* @returns pointer to newly allocated URB fragment or NULL.
*/
static PUSBPROXYURBLNX usbProxyLinuxSplitURBFragment(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pHead, PUSBPROXYURBLNX pCur)
{
if (!pNew)
{
return NULL;
}
return pNew;
}
/**
* Try splitting up a VUSB URB into smaller URBs which the
* linux kernel (usbfs) can deal with.
*
* NB: For ShortOK reads things get a little tricky - we don't
* know how much data is going to arrive and not all the
* fragment URBs might be filled. We can only safely set up one
* URB at a time -> worse performance but correct behaviour.
*
* @returns true / false.
* @param pProxyDev The proxy device.
* @param pUrbLnx The linux URB which was rejected because of being too big.
* @param pUrb The VUSB URB.
*/
static int usbProxyLinuxUrbQueueSplit(PUSBPROXYDEV pProxyDev, PUSBPROXYURBLNX pUrbLnx, PVUSBURB pUrb)
{
/*
* Split it up into SPLIT_SIZE sized blocks.
*/
/* the first one (already allocated) */
{
default: /* shut up gcc */
case VUSBXFERTYPE_ISOC:
AssertMsgFailed(("We can't split isochronous URBs!\n"));
return false;
}
bool fSucceeded = false;
bool fUnplugged = false;
{
/* Subsequent fragments will be queued only after the previous fragment is reaped
* and only if necessary.
*/
fSucceeded = true;
Log(("usb-linux: Large ShortOK read, only queuing first fragment.\n"));
}
else
{
/* the rest. */
unsigned i;
for (i = 1; i < cKUrbs; i++)
{
if (!pCur)
{
return false;
}
}
/* Submit the blocks. */
{
if (!fSucceeded)
break;
}
}
if (fSucceeded)
{
LogFlow(("usbProxyLinuxUrbQueueSplit: ok\n"));
return true;
}
return false;
}
/**
* @copydoc USBPROXYBACK::pfnUrbQueue
*/
{
unsigned cTries;
#ifndef RDESKTOP
#else
#endif
LogFlow(("usbProxyLinuxUrbQueue: pProxyDev=%s pUrb=%p EndPt=%d cbData=%d\n",
/*
* Allocate a linux urb.
*/
if (!pUrbLnx)
return false;
{
case VUSBXFERTYPE_MSG:
{
return false;
}
LogFlow(("usbProxyLinuxUrbQueue: message\n"));
break;
case VUSBXFERTYPE_BULK:
break;
case VUSBXFERTYPE_ISOC:
unsigned i;
{
}
break;
case VUSBXFERTYPE_INTR:
break;
default:
goto l_err;
}
/*
* Submit it.
*/
cTries = 0;
{
continue;
{
return false;
}
/*
* usbfs has or used to have a low buffer limit (16KB) in order to prevent
* processes wasting kmalloc'ed memory. It will return EINVAL if break that
* limit, and we'll have to split the VUSB URB up into multiple linux URBs.
*
* Since this is a limit which is subject to change, we cannot check for it
* before submitting the URB. We just have to try and fail.
*/
Log(("usb-linux: Queue URB %p -> %d!!! type=%d ep=%#x buffer_length=%#x cTries=%d\n",
continue;
return false;
}
LogFlow(("usbProxyLinuxUrbQueue: ok\n"));
return true;
}
/**
* Check if any or the in-flight URBs are taking too long and should be cancelled.
*
* Cancelling is done in three turns, first a URB is marked for timeout if it's
* exceeding a certain time limit. Then the next time it's encountered it is actually
* cancelled. The idea now is that it's supposed to be reaped and returned in the next
* round of calls.
*
* @param pProxyDev The proxy device.
* @param pDevLnx The linux backend data.
*
* @todo Make the HCI do proper timeout handling! Current timeout is 3 min and 20 seconds
* as not to break bloomberg which queues IN packages with 3 min timeouts.
*/
{
pCur;
{
{
if (pCur->pSplitHead)
{
/* split */
unsigned cFailures = 0;
{
if (pCur2->fSplitElementReaped)
continue;
pCur2->fCanceledByTimedOut = true;
Log(("vusbProxyLinuxUrbDoTimeouts: pUrb=%p failed errno=%d (!!split!!)\n", pCur2->KUrb.usercontext, errno));
else
goto l_leave; /* ENODEV means break and everything cancelled elsewhere. */
}
LogRel(("USB: Cancelled URB (%p) after %llums!! (cFailures=%d)\n",
}
else
{
/* unsplit */
{
pCur->fCanceledByTimedOut = true;
LogRel(("USB: Cancelled URB (%p) after %llums!!\n", pCur->KUrb.usercontext, (long long unsigned) u64MilliTS - pCur->u64SubmitTS));
}
else
goto l_leave; /* ENODEV means break and everything cancelled elsewhere. */
}
}
#if 0
/* Disabled for the time being as some USB devices have URBs pending for an unknown amount of time.
* One example is the OmniKey CardMan 3821. */
else if (u64MilliTS - pCur->u64SubmitTS >= 200*1000 /* 200 sec (180 sec has been observed with XP) */)
#endif
}
}
/**
* Translate the linux status to a VUSB status.
*
* sitd_complete+itd_complete in ehci-sched.c, and qtd_copy_status in
* ehci-q.c.
*/
{
switch (iStatus)
{
/** @todo VUSBSTATUS_NOT_ACCESSED */
case -EXDEV: /* iso transfer, partial result. */
case 0:
return VUSBSTATUS_OK;
case -EILSEQ:
return VUSBSTATUS_CRC;
case -EREMOTEIO: /* ehci and ohci uses this for underflow error. */
return VUSBSTATUS_DATA_UNDERRUN;
case -EOVERFLOW:
return VUSBSTATUS_DATA_OVERRUN;
case -ETIME:
case -ENODEV:
return VUSBSTATUS_DNR;
//case -ECOMM:
// return VUSBSTATUS_BUFFER_OVERRUN;
//case -ENOSR:
// return VUSBSTATUS_BUFFER_UNDERRUN;
//case -EPROTO:
// return VUSBSTATUS_BIT_STUFFING;
case -EPIPE:
return VUSBSTATUS_STALL;
case -ESHUTDOWN:
Log(("vusbProxyLinuxStatusToVUsbStatus: SHUTDOWN!!\n"));
return VUSBSTATUS_STALL;
default:
return VUSBSTATUS_STALL;
}
}
/**
* Get and translates the linux status to a VUSB status.
*/
{
if ( pUrbLnx->fCanceledByTimedOut
return VUSBSTATUS_CRC;
}
/**
* Reap URBs in-flight on a device.
*
* @returns Pointer to a completed URB.
* @returns NULL if no URB was completed.
* @param pProxyDev The device.
* @param cMillies Number of milliseconds to wait. Use 0 to not wait at all.
*/
{
/*
* Any URBs pending delivery?
*/
if (pDevLnx->pTaxingHead)
{
if (pUrbLnx)
{
/* unlink from the pending delivery list */
{
}
else
/* temporarily into the active list, so free works right. */
}
}
if (!pUrbLnx)
{
/*
* Don't block if nothing is in the air.
*/
if (!pDevLnx->pInFlightHead)
{
LogFlow(("Nothing in flight, going to sleep\n"));
if (rc >= 1)
{
/* Drain pipe. */
}
return NULL;
}
/*
* Block for requested period.
*
* It seems to me that the path of poll() is shorter and
* involves less semaphores than ioctl() on usbfs. So, we'll
* do a poll regardless of whether cMillies == 0 or not.
*/
if (cMillies)
{
for (;;)
{
if (rc >= 1)
{
/* If the pipe caused the return drain it. */
{
}
break;
}
if (rc >= 0 /*|| errno == ETIMEOUT*/)
{
return NULL;
}
{
Log(("usb-linux: Reap URB - poll -> %d errno=%d pProxyDev=%s\n", rc, errno, usbProxyGetName(pProxyDev)));
return NULL;
}
Log(("usbProxyLinuxUrbReap: poll again - weird!!!\n"));
}
}
/*
* Reap URBs, non-blocking.
*/
for (;;)
{
struct usbdevfs_urb *pKUrb;
{
else
return NULL;
}
/* split list: Is the entire split list done yet? */
if (pUrbLnx->pSplitHead)
{
pUrbLnx->fSplitElementReaped = true;
/* for variable size URBs, we may need to queue more if the just-reaped URB was completely filled */
if (pUrbLnx->cbSplitRemaining && (pKUrb->actual_length == pKUrb->buffer_length) && !pUrbLnx->pSplitNext)
{
bool fUnplugged = false;
bool fSucceeded;
if (!pNew)
{
Log(("usb-linux: Allocating URB fragment failed. errno=%d pProxyDev=%s\n", errno, usbProxyGetName(pProxyDev)));
return NULL;
}
if (fUnplugged)
if (!fSucceeded)
return NULL;
continue; /* try reaping another URB */
}
if (!pCur->fSplitElementReaped)
{
break;
}
if (!pUrbLnx)
continue;
}
break;
}
}
/*
* Ok, we got one!
*/
if ( pUrb
&& !pUrbLnx->fCanceledBySubmit)
{
if (pUrbLnx->pSplitHead)
{
/* split - find the end byte and the first error status. */
{
}
}
else
{
/* unsplit. */
{
unsigned i, off;
{
pUrb->aIsocPkts[i].enmStatus = vusbProxyLinuxStatusToVUsbStatus(pUrbLnx->KUrb.iso_frame_desc[i].status);
}
}
}
/* some adjustments for message transfers. */
{
}
}
else
{
}
return pUrb;
}
/**
* Cancels the URB.
* The URB requires reaping, so we don't change its state.
*/
{
#ifndef RDESKTOP
#else
#endif
if (pUrbLnx->pSplitHead)
{
/* split */
{
if (pCur->fSplitElementReaped)
continue;
continue;
break;
Log(("usb-linux: Discard URB %p failed, errno=%d. pProxyDev=%s!!! (split)\n",
}
}
else
{
/* unsplit */
Log(("usb-linux: Discard URB %p failed, errno=%d. pProxyDev=%s!!!\n",
}
}
{
}
/**
* The Linux USB Proxy Backend.
*/
const USBPROXYBACK g_USBProxyDeviceHost =
{
/* pszName */
"host",
/* cbBackend */
sizeof(USBPROXYDEVLNX),
0
};
/*
* Local Variables:
* mode: c
* c-file-style: "bsd"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: s
* End:
*/