VirtualBox

source: vbox/trunk/src/VBox/Main/include/HostUSBDeviceImpl.h@ 47167

Last change on this file since 47167 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/* $Id: HostUSBDeviceImpl.h 44528 2013-02-04 14:27:54Z vboxsync $ */
2/** @file
3 * VirtualBox IHostUSBDevice COM interface implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ____H_HOSTUSBDEVICEIMPL
19#define ____H_HOSTUSBDEVICEIMPL
20
21#include "VirtualBoxBase.h"
22#include "USBDeviceFilterImpl.h"
23/* #include "USBProxyService.h" circular on Host/HostUSBDevice, the includer
24 * must include this. */
25
26#include <VBox/usb.h>
27#include "Logging.h"
28
29class SessionMachine;
30class USBProxyService;
31
32/**
33 * The unified state machine of HostUSBDevice.
34 *
35 * This is a super set of USBDEVICESTATE / USBDeviceState_T that
36 * includes additional states for tracking state transitions.
37 *
38 * @remarks
39 * The CapturingForVM and CapturingForProxy states have been merged
40 * into Capturing with a destination state (AttachingToVM or HeldByProxy).
41 *
42 * The DetachingFromVM state is a merge of DetachingFromVMToProxy and
43 * DetachingFromVMToHost and uses the destination state (HeldByProxy
44 * or ReleasingToHost) like Capturing.
45 *
46 * The *AwaitingDetach and *AwaitingReattach substates (optionally used
47 * in Capturing, AttachingToVM, DetachingFromVM and ReleasingToHost) are
48 * implemented via a substate kHostUSBDeviceSubState.
49 */
50typedef enum
51{
52 /** The device is unsupported (HUB).
53 * Next Host: PhysDetached.
54 * Next VBox: No change permitted.
55 */
56 kHostUSBDeviceState_Unsupported = USBDEVICESTATE_UNSUPPORTED,
57 /** The device is used exclusivly by the host or is inaccessible for some other reason.
58 * Next Host: Capturable, Unused, PhysDetached.
59 * Run filters.
60 * Next VBox: No change permitted.
61 */
62 kHostUSBDeviceState_UsedByHost = USBDEVICESTATE_USED_BY_HOST,
63 /** The device is used by the host but can be captured.
64 * Next Host: Unsupported, UsedByHost, Unused, PhysDetached.
65 * Run filters if Unused (for wildcard filters).
66 * Next VBox: CapturingForVM, CapturingForProxy.
67 */
68 kHostUSBDeviceState_Capturable = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE,
69 /** The device is not used by the host and can be captured.
70 * Next Host: UsedByHost, Capturable, PhysDetached
71 * Don't run any filters (done on state entry).
72 * Next VBox: CapturingForVM, CapturingForProxy.
73 */
74 kHostUSBDeviceState_Unused = USBDEVICESTATE_UNUSED,
75 /** The device is held captive by the proxy.
76 * Next Host: PhysDetached
77 * Next VBox: ReleasingHeld, AttachingToVM
78 */
79 kHostUSBDeviceState_HeldByProxy = USBDEVICESTATE_HELD_BY_PROXY,
80 /** The device is in use by a VM.
81 * Next Host: PhysDetachingFromVM
82 * Next VBox: DetachingFromVM
83 */
84 kHostUSBDeviceState_UsedByVM = USBDEVICESTATE_USED_BY_GUEST,
85 /** The device has been detach from both the host and VMs.
86 * This is the final state. */
87 kHostUSBDeviceState_PhysDetached = 9,
88
89
90 /** The start of the transitional states. */
91 kHostUSBDeviceState_FirstTransitional,
92
93 /** The device is being seized from the host, either for HeldByProxy or for AttachToVM.
94 *
95 * On some hosts we will need to re-enumerate the in which case the sub-state
96 * is employed to track this progress. On others, this is synchronous or faked, and
97 * will will then leave the device in this state and poke the service thread to do
98 * the completion state change.
99 *
100 * Next Host: PhysDetached.
101 * Next VBox: HeldByProxy or AttachingToVM on success,
102 * previous state (Unused or Capturable) or UsedByHost on failure.
103 */
104 kHostUSBDeviceState_Capturing = kHostUSBDeviceState_FirstTransitional,
105
106 /** The device is being released back to the host, following VM or Proxy usage.
107 * Most hosts needs to re-enumerate the device and will therefore employ the
108 * sub-state as during capturing. On the others we'll just leave it to the usb
109 * service thread to advance the device state.
110 *
111 * Next Host: Unused, UsedByHost, Capturable.
112 * No filters.
113 * Next VBox: PhysDetached (timeout), HeldByProxy (failure).
114 */
115 kHostUSBDeviceState_ReleasingToHost,
116
117 /** The device is being attached to a VM.
118 *
119 * This requires IPC to the VM and we will not advance the state until
120 * that completes.
121 *
122 * Next Host: PhysDetachingFromVM.
123 * Next VBox: UsedByGuest, HeldByProxy (failure).
124 */
125 kHostUSBDeviceState_AttachingToVM,
126
127 /** The device is being detached from a VM and will be returned to the proxy or host.
128 *
129 * This involves IPC and may or may not also require re-enumeration of the
130 * device. Which means that it might transition directly into the ReleasingToHost state
131 * because the client (VM) will do the actual re-enumeration.
132 *
133 * Next Host: PhysDetachingFromVM (?) or just PhysDetached.
134 * Next VBox: ReleasingToHost, HeldByProxy.
135 */
136 kHostUSBDeviceState_DetachingFromVM,
137
138 /** The device has been physically removed while a VM used it.
139 *
140 * This is the device state while VBoxSVC is doing IPC to the client (VM) telling it
141 * to detach it.
142 *
143 * Next Host: None.
144 * Next VBox: PhysDetached
145 */
146 kHostUSBDeviceState_PhysDetachingFromVM,
147
148 /** Just an invalid state value for use as default for some methods. */
149 kHostUSBDeviceState_Invalid = 0x7fff
150} HostUSBDeviceState;
151
152
153/**
154 * Sub-state for dealing with device re-enumeration.
155 */
156typedef enum
157{
158 /** Not in any sub-state. */
159 kHostUSBDeviceSubState_Default = 0,
160 /** Awaiting a logical device detach following a device re-enumeration. */
161 kHostUSBDeviceSubState_AwaitingDetach,
162 /** Awaiting a logical device re-attach following a device re-enumeration. */
163 kHostUSBDeviceSubState_AwaitingReAttach
164} HostUSBDeviceSubState;
165
166
167/**
168 * Object class used to hold Host USB Device properties.
169 */
170class ATL_NO_VTABLE HostUSBDevice :
171 public VirtualBoxBase,
172 VBOX_SCRIPTABLE_IMPL(IHostUSBDevice)
173{
174public:
175
176 VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(HostUSBDevice, IHostUSBDevice)
177
178 DECLARE_NOT_AGGREGATABLE(HostUSBDevice)
179
180 DECLARE_PROTECT_FINAL_CONSTRUCT()
181
182 BEGIN_COM_MAP(HostUSBDevice)
183 VBOX_DEFAULT_INTERFACE_ENTRIES(IHostUSBDevice)
184 COM_INTERFACE_ENTRY(IUSBDevice)
185 END_COM_MAP()
186
187 DECLARE_EMPTY_CTOR_DTOR (HostUSBDevice)
188
189 HRESULT FinalConstruct();
190 void FinalRelease();
191
192 // public initializer/uninitializer for internal purposes only
193 HRESULT init(PUSBDEVICE aUsb, USBProxyService *aUSBProxyService);
194 void uninit();
195
196 // IUSBDevice properties
197 STDMETHOD(COMGETTER(Id))(BSTR *aId);
198 STDMETHOD(COMGETTER(VendorId))(USHORT *aVendorId);
199 STDMETHOD(COMGETTER(ProductId))(USHORT *aProductId);
200 STDMETHOD(COMGETTER(Revision))(USHORT *aRevision);
201 STDMETHOD(COMGETTER(Manufacturer))(BSTR *aManufacturer);
202 STDMETHOD(COMGETTER(Product))(BSTR *aProduct);
203 STDMETHOD(COMGETTER(SerialNumber))(BSTR *aSerialNumber);
204 STDMETHOD(COMGETTER(Address))(BSTR *aAddress);
205 STDMETHOD(COMGETTER(Port))(USHORT *aPort);
206 STDMETHOD(COMGETTER(Version))(USHORT *aVersion);
207 STDMETHOD(COMGETTER(PortVersion))(USHORT *aPortVersion);
208 STDMETHOD(COMGETTER(Remote))(BOOL *aRemote);
209
210 // IHostUSBDevice properties
211 STDMETHOD(COMGETTER(State))(USBDeviceState_T *aState);
212
213 // public methods only for internal purposes
214
215 /** @note Must be called from under the object read lock. */
216 const Guid& getId() const { return mId; }
217
218 /** @note Must be called from under the object read lock. */
219 HostUSBDeviceState getUnistate() const { return mUniState; }
220
221 /** @note Must be called from under the object read lock. */
222 const char *getStateName() { return stateName (mUniState, mPendingUniState, mUniSubState); }
223
224 /** @note Must be called from under the object read lock. */
225 bool isCapturableOrHeld()
226 {
227 return mUniState == kHostUSBDeviceState_Unused
228 || mUniState == kHostUSBDeviceState_Capturable
229 || mUniState == kHostUSBDeviceState_HeldByProxy;
230 }
231
232 /** @note Must be called from under the object read lock. */
233 ComObjPtr<SessionMachine> &getMachine() { return mMachine; }
234
235 /** @note Must be called from under the object read lock. */
236 PCUSBDEVICE getUsbData() const { return mUsb; }
237
238 Utf8Str getName();
239
240 HRESULT requestCaptureForVM(SessionMachine *aMachine, bool aSetError, ULONG aMaskedIfs = 0);
241 HRESULT onDetachFromVM(SessionMachine *aMachine, bool aDone, bool *aRunFilters, bool aAbnormal = false);
242 HRESULT requestReleaseToHost();
243 HRESULT requestHold();
244 bool wasActuallyDetached();
245 void onPhysicalDetached();
246
247 bool isMatch(const USBDeviceFilter::Data &aData);
248 int compare(PCUSBDEVICE aDev2);
249 static int compare(PCUSBDEVICE aDev1, PCUSBDEVICE aDev2, bool aIsAwaitingReAttach = false);
250
251 bool updateState(PCUSBDEVICE aDev, bool *aRunFilters, SessionMachine **aIgnoreMachine);
252 bool updateStateFake(PCUSBDEVICE aDev, bool *aRunFilters, SessionMachine **aIgnoreMachine);
253
254 static const char *stateName(HostUSBDeviceState aState,
255 HostUSBDeviceState aPendingState = kHostUSBDeviceState_Invalid,
256 HostUSBDeviceSubState aSubState = kHostUSBDeviceSubState_Default);
257
258protected:
259 HRESULT attachToVM(SessionMachine *aMachine, ULONG aMaskedIfs = 0);
260 void detachFromVM(HostUSBDeviceState aFinalState);
261 void onPhysicalDetachedInternal();
262 bool hasAsyncOperationTimedOut() const;
263
264 bool setState (HostUSBDeviceState aNewState, HostUSBDeviceState aNewPendingState = kHostUSBDeviceState_Invalid,
265 HostUSBDeviceSubState aNewSubState = kHostUSBDeviceSubState_Default);
266 bool startTransition (HostUSBDeviceState aNewState, HostUSBDeviceState aFinalState,
267 HostUSBDeviceSubState aNewSubState = kHostUSBDeviceSubState_Default);
268 bool advanceTransition(bool aSkipReAttach = false);
269 bool failTransition();
270 USBDeviceState_T canonicalState() const;
271
272private:
273
274 const Guid mId;
275
276 /** @name The state machine variables
277 * Only setState(), init() and uninit() will modify these members!
278 * @{ */
279 /** The RTTimeNanoTS() corresponding to the last state change.
280 *
281 * Old state machine: RTTimeNanoTS() of when mIsStatePending was set or mDetaching changed
282 * from kNotDetaching. For operations that cannot be canceled it's 0. */
283 uint64_t mLastStateChangeTS;
284 /** Current state. */
285 HostUSBDeviceState mUniState;
286 /** Sub-state for tracking re-enumeration. */
287 HostUSBDeviceSubState mUniSubState;
288 /** The final state of an pending transition.
289 * This is mainly a measure to reduce the number of HostUSBDeviceState values. */
290 HostUSBDeviceState mPendingUniState;
291 /** Previous state.
292 * This is used for bailing out when a transition like capture fails. */
293 HostUSBDeviceState mPrevUniState;
294 /** Indicator set by onDetachedPhys and check when advancing a transitional state. */
295 bool mIsPhysicallyDetached;
296 /** @} */
297
298 /** The machine the usb device is (being) attached to. */
299 ComObjPtr<SessionMachine> mMachine;
300 /** Pointer to the USB Proxy Service instance. */
301 USBProxyService *mUSBProxyService;
302 /** Pointer to the USB Device structure owned by this device.
303 * Only used for host devices. */
304 PUSBDEVICE mUsb;
305 /** The interface mask to be used in the pending capture.
306 * This is a filter property. */
307 ULONG mMaskedIfs;
308 /** The name of this device. */
309 Utf8Str mNameObj;
310 /** The name of this device (for logging purposes).
311 * This points to the string in mNameObj. */
312 const char *mName;
313
314 friend class USBProxyService;
315#ifdef RT_OS_SOLARIS
316 friend class USBProxyServiceSolaris;
317
318 /** One-shot filter id only for new code */
319 void *mOneShotId;
320#endif
321#ifdef RT_OS_LINUX
322 friend class USBProxyServiceLinux;
323#endif
324#ifdef RT_OS_DARWIN
325 /** One-shot filter id. */
326 void *mOneShotId;
327
328 friend class USBProxyServiceDarwin;
329#endif
330#ifdef RT_OS_FreeBSD
331 friend class USBProxyServiceFreeBSD;
332#endif
333};
334
335#endif // ____H_HOSTUSBDEVICEIMPL
336/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette