USBProxyService.h revision b52c24239bed9dc9f284d829b6059073afcb1894
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/* $Id$ */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/** @file
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * VirtualBox USB Proxy Service (base) class.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/*
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Copyright (C) 2006-2007 Oracle Corporation
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe *
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * This file is part of VirtualBox Open Source Edition (OSE), as
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * available from http://www.virtualbox.org. This file is free software;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * you can redistribute it and/or modify it under the terms of the GNU
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * General Public License (GPL) as published by the Free Software
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Foundation, in version 2 as it comes in the "COPYING" file of the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#ifndef ____H_USBPROXYSERVICE
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#define ____H_USBPROXYSERVICE
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <VBox/usb.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include <VBox/usbfilter.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include "VirtualBoxBase.h"
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include "VirtualBoxImpl.h"
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#include "HostUSBDeviceImpl.h"
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweclass Host;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/**
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Base class for the USB Proxy service.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweclass USBProxyService
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe : public VirtualBoxTranslatable
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowepublic:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe USBProxyService(Host *aHost);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual HRESULT init(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual ~USBProxyService();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /**
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * Override of the default locking class to be used for validating lock
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * order with the standard member lock handle.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual VBoxLockingClass getLockingClass() const
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe {
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // the USB proxy service uses the Host object lock, so return the
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe // same locking class as the host
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe return LOCKCLASS_HOSTOBJECT;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe }
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool isActive(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int getLastError(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT getLastErrorMessage(BSTR *aError);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe RWLockHandle *lockHandle() const;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** @name Host Interfaces
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * @{ */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT getDeviceCollection(ComSafeArrayOut(IHostUSBDevice *, aUSBDevices));
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** @} */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** @name SessionMachine Interfaces
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * @{ */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT captureDeviceForVM(SessionMachine *aMachine, IN_GUID aId);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT detachDeviceFromVM(SessionMachine *aMachine, IN_GUID aId, bool aDone);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT autoCaptureDevicesForVM(SessionMachine *aMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT detachAllDevicesFromVM(SessionMachine *aMachine, bool aDone, bool aAbnormal);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** @} */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** @name Interface for the USBController and the Host object.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * @{ */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void *insertFilter(PCUSBFILTER aFilter);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void removeFilter(void *aId);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** @} */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** @name Interfaces for the HostUSBDevice
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * @{ */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int captureDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void captureDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void detachingDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int releaseDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void releaseDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** @} */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprotected:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int start(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int stop(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void serviceThreadInit(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void serviceThreadTerm(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int wait(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int interruptWait(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual PUSBDEVICE getDevices(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void deviceRemoved(ComObjPtr<HostUSBDevice> &aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void deviceChanged(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList *pllOpenedMachines, SessionMachine *aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool updateDeviceStateFake(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual bool updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ComObjPtr<HostUSBDevice> findDeviceById(IN_GUID aId);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe static HRESULT setError(HRESULT aResultCode, const char *aText, ...);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe static void initFilterFromDevice(PUSBFILTER aFilter, HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe static void freeDeviceMembers(PUSBDEVICE pDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowepublic:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe static void freeDevice(PUSBDEVICE pDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprivate:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT runAllFiltersOnDevice(ComObjPtr<HostUSBDevice> &aDevice,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe SessionMachinesList &llOpenedMachines,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe SessionMachine *aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool runMachineFilters(SessionMachine *aMachine, ComObjPtr<HostUSBDevice> &aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe void processChanges(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe static DECLCALLBACK(int) serviceThread(RTTHREAD Thread, void *pvUser);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprotected:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Pointer to the Host object. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe Host *mHost;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Thread handle of the service thread. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe RTTHREAD mThread;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Flag which stop() sets to cause serviceThread to return. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool volatile mTerminate;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** VBox status code of the last failure.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * (Only used by start(), stop() and the child constructors.) */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int mLastError;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Optional error message to complement mLastError. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe Bstr mLastErrorMessage;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** List of smart HostUSBDevice pointers. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe typedef std::list<ComObjPtr<HostUSBDevice> > HostUSBDeviceList;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** List of the known USB devices. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HostUSBDeviceList mDevices;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe};
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# ifdef RT_OS_DARWIN
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# include <VBox/param.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# undef PAGE_SHIFT
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# undef PAGE_SIZE
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# define OSType Carbon_OSType
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# include <Carbon/Carbon.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# undef OSType
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/**
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The Darwin hosted USB Proxy Service.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweclass USBProxyServiceDarwin : public USBProxyService
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowepublic:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe USBProxyServiceDarwin(Host *aHost);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT init(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ~USBProxyServiceDarwin();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#ifdef VBOX_WITH_NEW_USB_CODE_ON_DARWIN
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void *insertFilter(PCUSBFILTER aFilter);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void removeFilter(void *aId);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#endif
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int captureDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void captureDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void detachingDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int releaseDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void releaseDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprotected:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int wait(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int interruptWait (void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual PUSBDEVICE getDevices (void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void serviceThreadInit (void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void serviceThreadTerm (void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual bool updateDeviceState (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprivate:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Reference to the runloop of the service thread.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * This is NULL if the service thread isn't running. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe CFRunLoopRef mServiceRunLoopRef;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** The opaque value returned by DarwinSubscribeUSBNotifications. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe void *mNotifyOpaque;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** A hack to work around the problem with the usb device enumeration
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * not including newly attached devices. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool mWaitABitNextTime;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#ifndef VBOX_WITH_NEW_USB_CODE_ON_DARWIN
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Whether we've got a fake async event and should return without entering the runloop. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool volatile mFakeAsync;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#endif
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Whether we've successfully initialized the USBLib and should call USBLibTerm in the destructor. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool mUSBLibInitialized;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe};
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# endif /* RT_OS_DARWIN */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# ifdef RT_OS_LINUX
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# include <stdio.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# ifdef VBOX_USB_WITH_SYSFS
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# include <HostHardwareLinux.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# endif
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/**
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The Linux hosted USB Proxy Service.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweclass USBProxyServiceLinux
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe : public USBProxyService
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowepublic:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe USBProxyServiceLinux(Host *aHost,
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe const char *aUsbfsRoot = "/proc/bus/usb");
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT init(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ~USBProxyServiceLinux();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int captureDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int releaseDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprotected:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int initUsbfs(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int initSysfs(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe void doUsbfsCleanupAsNeeded(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int wait(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int interruptWait(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual PUSBDEVICE getDevices(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe PUSBDEVICE getDevicesFromUsbfs(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe PUSBDEVICE getDevicesFromSysfs(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int addDeviceToChain(PUSBDEVICE pDev, PUSBDEVICE *ppFirst, PUSBDEVICE **pppNext, int rc);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual bool updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprivate:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int waitUsbfs(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int waitSysfs(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprivate:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** File handle to the '/proc/bus/usb/devices' file. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe RTFILE mFile;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Stream for mFile. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe FILE *mStream;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Pipe used to interrupt wait(), the read end. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe RTFILE mWakeupPipeR;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Pipe used to interrupt wait(), the write end. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe RTFILE mWakeupPipeW;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** The root of usbfs. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe Utf8Str mUsbfsRoot;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Whether we're using <mUsbfsRoot>/devices or /sys/whatever. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool mUsingUsbfsDevices;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Number of 500ms polls left to do. See usbDeterminState for details. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe unsigned mUdevPolls;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# ifdef VBOX_USB_WITH_SYSFS
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Object used for querying device information from hal. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe VBoxMainUSBDeviceInfo mDeviceList;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Object used for polling for hotplug events from hal. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe VBoxMainHotplugWaiter mWaiter;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# endif
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe};
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# endif /* RT_OS_LINUX */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# ifdef RT_OS_OS2
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# include <usbcalls.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/**
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The Linux hosted USB Proxy Service.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweclass USBProxyServiceOs2 : public USBProxyService
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowepublic:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe USBProxyServiceOs2 (Host *aHost);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /// @todo virtual HRESULT init(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ~USBProxyServiceOs2();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int captureDevice (HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int releaseDevice (HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprotected:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int wait(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int interruptWait(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual PUSBDEVICE getDevices(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int addDeviceToChain(PUSBDEVICE pDev, PUSBDEVICE *ppFirst, PUSBDEVICE **pppNext, int rc);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual bool updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprivate:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** The notification event semaphore */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HEV mhev;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** The notification id. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe USBNOTIFY mNotifyId;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** The usbcalls.dll handle. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HMODULE mhmod;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** UsbRegisterChangeNotification */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe APIRET (APIENTRY *mpfnUsbRegisterChangeNotification)(PUSBNOTIFY, HEV, HEV);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** UsbDeregisterNotification */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe APIRET (APIENTRY *mpfnUsbDeregisterNotification)(USBNOTIFY);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** UsbQueryNumberDevices */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe APIRET (APIENTRY *mpfnUsbQueryNumberDevices)(PULONG);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** UsbQueryDeviceReport */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe APIRET (APIENTRY *mpfnUsbQueryDeviceReport)(ULONG, PULONG, PVOID);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe};
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# endif /* RT_OS_LINUX */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# ifdef RT_OS_SOLARIS
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# include <libdevinfo.h>
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/**
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The Solaris hosted USB Proxy Service.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweclass USBProxyServiceSolaris : public USBProxyService
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowepublic:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe USBProxyServiceSolaris(Host *aHost);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT init(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ~USBProxyServiceSolaris();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void *insertFilter (PCUSBFILTER aFilter);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void removeFilter (void *aID);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int captureDevice (HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int releaseDevice (HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void captureDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void releaseDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprotected:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int wait(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int interruptWait(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual PUSBDEVICE getDevices(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual bool updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprivate:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe RTSEMEVENT mNotifyEventSem;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe /** Whether we've successfully initialized the USBLib and should call USBLibTerm in the destructor. */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe bool mUSBLibInitialized;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe};
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#endif /* RT_OS_SOLARIS */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# ifdef RT_OS_WINDOWS
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/**
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The Windows hosted USB Proxy Service.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweclass USBProxyServiceWindows : public USBProxyService
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowepublic:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe USBProxyServiceWindows(Host *aHost);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT init(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ~USBProxyServiceWindows();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void *insertFilter (PCUSBFILTER aFilter);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void removeFilter (void *aID);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int captureDevice (HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int releaseDevice (HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprotected:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int wait(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int interruptWait(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual PUSBDEVICE getDevices(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual bool updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprivate:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HANDLE mhEventInterrupt;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe};
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# endif /* RT_OS_WINDOWS */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# ifdef RT_OS_FREEBSD
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/**
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe * The FreeBSD hosted USB Proxy Service.
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweclass USBProxyServiceFreeBSD : public USBProxyService
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe{
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowepublic:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe USBProxyServiceFreeBSD(Host *aHost);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe HRESULT init(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe ~USBProxyServiceFreeBSD();
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int captureDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int releaseDevice(HostUSBDevice *aDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprotected:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int initUsbfs(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int initSysfs(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int wait(RTMSINTERVAL aMillies);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual int interruptWait(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual PUSBDEVICE getDevices(void);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe int addDeviceToChain(PUSBDEVICE pDev, PUSBDEVICE *ppFirst, PUSBDEVICE **pppNext, int rc);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual void deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe virtual bool updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine);
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Loweprivate:
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe RTSEMEVENT mNotifyEventSem;
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe};
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe# endif /* RT_OS_FREEBSD */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe#endif /* !____H_USBPROXYSERVICE */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe/* vi: set tabstop=4 shiftwidth=4 expandtab: */
10d63b7db37a83b39c7f511cf9426c9d03ea0760Richard Lowe