USBControllerImpl.cpp revision 6f0c02346cb86a521150bb90b064f439e03e2a6d
/** @file
*
* Implementation of IUSBController.
*/
/*
* 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.
*/
#include "USBControllerImpl.h"
#include "MachineImpl.h"
#include "VirtualBoxImpl.h"
#include "HostImpl.h"
#include "USBDeviceImpl.h"
#include "HostUSBDeviceImpl.h"
#include "Logging.h"
#include "USBProxyService.h"
#include <iprt/cpputils.h>
#include <algorithm>
// defines
/////////////////////////////////////////////////////////////////////////////
// constructor / destructor
/////////////////////////////////////////////////////////////////////////////
{
return S_OK;
}
void USBController::FinalRelease()
{
uninit();
}
// public initializer/uninitializer for internal purposes only
/////////////////////////////////////////////////////////////////////////////
/**
* Initializes the USB controller object.
*
* @returns COM result indicator.
* @param aParent Pointer to our parent object.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* mPeer is left null */
/* Confirm a successful initialization */
return S_OK;
}
/**
* Initializes the USB controller object given another USB controller object
* (a kind of copy constructor). This object shares data with
* the object passed as an argument.
*
* @returns COM result indicator.
* @param aParent Pointer to our parent object.
* @param aPeer The object to share.
*
* @note This object must be destroyed before the original object
* it shares data with is destroyed.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* create copies of all filters */
{
++ it;
}
/* Confirm a successful initialization */
return S_OK;
}
/**
* Initializes the USB controller object given another guest object
* (a kind of copy constructor). This object makes a private copy of data
* of the original object passed as an argument.
*/
{
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan (this);
/* mPeer is left null */
/* create private copies of all filters */
{
++ it;
}
/* Confirm a successful initialization */
return S_OK;
}
/**
* Uninitializes the instance and sets the ready flag to FALSE.
* Called either from FinalRelease() or by the parent when it gets destroyed.
*/
void USBController::uninit()
{
LogFlowThisFunc (("\n"));
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan (this);
if (autoUninitSpan.uninitDone())
return;
/* uninit all filters (including those still referenced by clients) */
}
// IUSBController properties
/////////////////////////////////////////////////////////////////////////////
{
if (!aEnabled)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock for safety */
}
return S_OK;
}
{
if (!aEnabled)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
{
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
{
/* leave the lock for safety */
}
return S_OK;
}
{
if (!aUSBStandard)
return E_POINTER;
AutoCaller autoCaller (this);
/* not accessing data -- no need to lock */
/** Note: This is no longer correct */
*aUSBStandard = 0x0101;
return S_OK;
}
{
if (!aDevicesFilters)
return E_POINTER;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return S_OK;
}
// IUSBController methods
/////////////////////////////////////////////////////////////////////////////
{
if (!aFilter)
return E_POINTER;
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
return S_OK;
}
{
if (!aFilter)
return E_INVALIDARG;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
if (!filter)
return setError (E_INVALIDARG,
tr ("The given USB device filter is not created within "
"this VirtualBox instance"));
return setError (E_INVALIDARG,
tr ("The given USB device filter is already in the list"));
/* backup the list before modification */
/* iterate to the position... */
{
}
else
/* ...and insert */
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
/* notify the proxy (only when the filter is active) */
#else
/* notify the proxy (only when it makes sense) */
#endif
{
#ifndef VBOX_WITH_USBFILTER
#else
#endif
}
return S_OK;
}
{
if (!aFilter)
return E_POINTER;
AutoCaller autoCaller (this);
/* the machine needs to be mutable */
if (!mDeviceFilters->size())
return setError (E_INVALIDARG,
tr ("The USB device filter list is empty"));
return setError (E_INVALIDARG,
tr ("Invalid position: %lu (must be in range [0, %lu])"),
/* backup the list before modification */
{
/* iterate to the position... */
/* ...get an element from there... */
/* ...and remove */
}
/* cancel sharing (make an independent copy of data) */
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
/* notify the proxy (only when the filter is active) */
#else
/* notify the proxy (only when it makes sense) */
#endif
{
}
return S_OK;
}
// public methods only for internal purposes
/////////////////////////////////////////////////////////////////////////////
/**
* Loads settings from the given machine node.
* May be called once right after this object creation.
*
* @param aMachineNode <Machine> node.
*
* @note Locks this object for writing.
*/
{
using namespace settings;
AutoCaller autoCaller (this);
/* Note: we assume that the default values for attributes of optional
* nodes are assigned in the Data::Data() constructor and don't do it
* here. It implies that this method may only be called after constructing
* a new BIOSSettings object while all its data fields are in the default
* values. Exceptions are fields whose creation time defaults don't match
* values that should be applied when these fields are not explicitly set
* in the settings file (for backwards compatibility reasons). This takes
* place when a setting of a newly created object must default to A while
* the same setting of an object loaded from the old settings file must
* default to B. */
/* USB Controller node (required) */
/* enabled (required) */
/* enabledEhci (optiona, defaults to false) */
{
/* required */
/* optional */
/* error info is set by init() when appropriate */
}
return S_OK;
}
/**
* Saves settings to the given machine node.
*
* @param aMachineNode <Machine> node.
*
* @note Locks this object for reading.
*/
{
using namespace settings;
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
/* first, delete the entry */
if (!controller.isNull())
controller.zap();
/* then, recreate it */
/* enabled */
/* enabledEhci */
{
/* all are optional */
#ifndef VBOX_WITH_USBFILTER
#else /* VBOX_WITH_USBFILTER */
#endif /* VBOX_WITH_USBFILTER */
if (data.mMaskedIfs)
++ it;
}
return S_OK;
}
/** @note Locks objects for reading! */
bool USBController::isModified()
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
return true;
/* see whether any of filters has changed its data */
++ it)
{
if ((*it)->isModified())
return true;
}
return false;
}
/** @note Locks objects for reading! */
bool USBController::isReallyModified()
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
if (mData.hasActualChanges())
return true;
if (!mDeviceFilters.isBackedUp())
{
/* see whether any of filters has changed its data */
++ it)
{
if ((*it)->isReallyModified())
return true;
}
return false;
}
return true;
if (mDeviceFilters->size() == 0)
return false;
/* Make copies to speed up comparison */
{
bool found = false;
{
{
found = true;
break;
}
else
++ thatIt;
}
if (found)
else
return false;
}
return false;
}
/** @note Locks objects for writing! */
bool USBController::rollback()
{
AutoCaller autoCaller (this);
/* we need the machine state */
bool dataChanged = false;
if (mData.isBackedUp())
{
/* we need to check all data to see whether anything will be changed
* after rollback */
}
if (mDeviceFilters.isBackedUp())
{
ComAssertRet (service, false);
/* uninitialize all new filters (absent in the backed up list) */
{
backedList->end())
{
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
/* notify the proxy (only when the filter is active) */
#else
/* notify the proxy (only when it makes sense) */
#endif
{
}
}
++ it;
}
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
#else
#endif
{
/* find all removed old filters (absent in the new list)
* and insert them back to the USB proxy */
{
mDeviceFilters->end())
{
/* notify the proxy (only when necessary) */
{
#ifndef VBOX_WITH_USBFILTER
#else
#endif
}
}
++ it;
}
}
/* restore the list */
}
/* here we don't depend on the machine state any more */
/* rollback any changes to filters after restoring the list */
{
if ((*it)->isModified())
{
/* call this to notify the USB proxy about changes */
}
++ it;
}
return dataChanged;
}
/** @note Locks objects for writing! */
void USBController::commit()
{
AutoCaller autoCaller (this);
if (mData.isBackedUp())
{
if (mPeer)
{
// attach new data to the peer and reshare it
}
}
bool commitFilters = false;
if (mDeviceFilters.isBackedUp())
{
// apply changes to peer
if (mPeer)
{
// commit all changes to new filters (this will reshare data with
// peers for those who have peers)
{
// look if this filter has a peer filter
if (!peer)
{
// no peer means the filter is a newly created one;
// create a peer owning data this filter share it with
peer.createObject();
}
else
{
// remove peer from the old list
}
// and add it to the new list
++ it;
}
// uninit old peer's filters that are left
{
++ it;
}
// attach new list of filters to our peer
}
else
{
// we have no peer (our parent is the newly created machine);
// just commit changes to filters
commitFilters = true;
}
}
else
{
// the list of filters itself is not changed,
// just commit changes to filters themselves
commitFilters = true;
}
if (commitFilters)
{
{
++ it;
}
}
}
/** @note Locks object for writing and that object for reading! */
{
AutoCaller autoCaller (this);
if (mParent->isRegistered())
{
/* reuse onMachineRegistered to tell USB proxy to remove all current
filters */
AssertComRCReturn (rc, (void) 0);
}
/* this will back up current data */
/* create private copies of all filters */
mDeviceFilters->clear();
++ it)
{
}
if (mParent->isRegistered())
{
/* reuse onMachineRegistered to tell USB proxy to insert all current
filters */
AssertComRCReturn (rc, (void) 0);
}
}
/**
* Called by VirtualBox when it changes the registered state
* of the machine this USB controller belongs to.
*
* @param aRegistered new registered state of the machine
*
* @note Locks nothing.
*/
{
AutoCaller autoCaller (this);
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
notifyProxy (!!aRegistered);
#endif
return S_OK;
}
/**
* Called by setter methods of all USB device filters.
*
* @note Locks nothing.
*/
{
AutoCaller autoCaller (this);
/// @todo After rewriting Win32 USB support, no more necessary;
// a candidate for removal.
#if 0
#else
/* we need the machine state */
/* nothing to do if the machine isn't running */
return S_OK;
#endif
/* we don't modify our data fields -- no need to lock */
{
if (aActiveChanged)
{
{
#ifndef VBOX_WITH_USBFILTER
#else
#endif
}
else
{
}
}
else
{
{
/* update the filter in the proxy */
#ifndef VBOX_WITH_USBFILTER
#else
#endif
}
}
}
return S_OK;
}
/**
* Returns true if the given USB device matches to at least one of
* this controller's USB device filters.
*
* A HostUSBDevice specific version.
*
* @note Locks this object for reading.
*/
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
/* Disabled USB controllers cannot actually work with USB devices */
return false;
/* apply self filters */
++ it)
{
{
return true;
}
}
return false;
}
/**
* Returns true if the given USB device matches to at least one of
* this controller's USB device filters.
*
* A generic version that accepts any IUSBDevice on input.
*
* @note
* This method MUST correlate with HostUSBDevice::isMatch()
* in the sense of the device matching logic.
*
* @note Locks this object for reading.
*/
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
/* Disabled USB controllers cannot actually work with USB devices */
return false;
/* query fields */
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
ComAssertRet (vendorId, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
ComAssertRet (productId, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
if (!manufacturer.isNull())
#endif
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
if (!serialNumber.isNull())
#endif
ComAssertComRCRet (rc, false);
ComAssertComRCRet (rc, false);
#ifdef VBOX_WITH_USBFILTER
#endif
ComAssertComRCRet (rc, false);
bool match = false;
/* apply self filters */
++ it)
{
continue;
continue;
#ifndef VBOX_WITH_USBFILTER
continue;
continue;
continue;
#if !defined (RT_OS_WINDOWS)
/* these filters are temporarily ignored on Win32 */
continue;
continue;
continue;
continue;
#endif
#else /* VBOX_WITH_USBFILTER */
continue;
#endif /* VBOX_WITH_USBFILTER */
match = true;
break;
}
return match;
}
/**
* Notifies the proxy service about all filters as requested by the
* @a aInsertFilters argument.
*
* @param aInsertFilters @c true to insert filters, @c false to remove.
*
* @note Locks this object for reading.
*/
{
AutoCaller autoCaller (this);
AutoReaderLock alock (this);
{
/* notify the proxy (only if the filter is active) */
{
if (aInsertFilters)
{
#ifndef VBOX_WITH_USBFILTER
#else
#endif
}
else
{
/* It's possible that the given filter was not inserted the proxy
* when this method gets called (as a result of an early VM
* process crash for example. So, don't assert that ID != NULL. */
{
}
}
}
++ it;
}
return S_OK;
}
// private methods
/////////////////////////////////////////////////////////////////////////////