VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostUSBDeviceImpl.cpp@ 42699

Last change on this file since 42699 was 41813, checked in by vboxsync, 13 years ago

Main/HostUSBDevice: forgotten lock fix which happens only on platforms which truly grab devices

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 94.3 KB
Line 
1/* $Id: HostUSBDeviceImpl.cpp 41813 2012-06-18 12:11:28Z vboxsync $ */
2/** @file
3 * VirtualBox IHostUSBDevice COM interface implementation.
4 */
5
6/*
7 * Copyright (C) 2005-2012 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#include <iprt/types.h> /* for UINT64_C */
19
20#include "HostUSBDeviceImpl.h"
21#include "MachineImpl.h"
22#include "HostImpl.h"
23#include "VirtualBoxErrorInfoImpl.h"
24#include "USBProxyService.h"
25
26#include "AutoCaller.h"
27#include "Logging.h"
28
29#include <VBox/err.h>
30#include <iprt/cpp/utils.h>
31
32// constructor / destructor
33/////////////////////////////////////////////////////////////////////////////
34
35DEFINE_EMPTY_CTOR_DTOR(HostUSBDevice)
36
37HRESULT HostUSBDevice::FinalConstruct()
38{
39 mUSBProxyService = NULL;
40 mUsb = NULL;
41
42 return BaseFinalConstruct();
43}
44
45void HostUSBDevice::FinalRelease()
46{
47 uninit();
48 BaseFinalRelease();
49}
50
51// public initializer/uninitializer for internal purposes only
52/////////////////////////////////////////////////////////////////////////////
53
54/**
55 * Initializes the USB device object.
56 *
57 * @returns COM result indicator
58 * @param aUsb Pointer to the usb device structure for which the object is to be a wrapper.
59 * This structure is now fully owned by the HostUSBDevice object and will be
60 * freed when it is destructed.
61 * @param aUSBProxyService Pointer to the USB Proxy Service object.
62 */
63HRESULT HostUSBDevice::init(PUSBDEVICE aUsb, USBProxyService *aUSBProxyService)
64{
65 ComAssertRet(aUsb, E_INVALIDARG);
66
67 /* Enclose the state transition NotReady->InInit->Ready */
68 AutoInitSpan autoInitSpan(this);
69 AssertReturn(autoInitSpan.isOk(), E_FAIL);
70
71 /*
72 * We need a unique ID for this VBoxSVC session.
73 * The UUID isn't stored anywhere.
74 */
75 unconst(mId).create();
76
77 /*
78 * Set the initial device state.
79 */
80 AssertMsgReturn( aUsb->enmState >= USBDEVICESTATE_UNSUPPORTED
81 && aUsb->enmState < USBDEVICESTATE_USED_BY_GUEST, /* used-by-guest is not a legal initial state. */
82 ("%d\n", aUsb->enmState), E_FAIL);
83 mUniState = (HostUSBDeviceState)aUsb->enmState;
84 mUniSubState = kHostUSBDeviceSubState_Default;
85 mPendingUniState = kHostUSBDeviceState_Invalid;
86 mPrevUniState = mUniState;
87 mIsPhysicallyDetached = false;
88
89 /* Other data members */
90 mUSBProxyService = aUSBProxyService;
91 mUsb = aUsb;
92
93 /* Set the name. */
94 mNameObj = getName();
95 mName = mNameObj.c_str();
96
97 /* Confirm the successful initialization */
98 autoInitSpan.setSucceeded();
99
100 return S_OK;
101}
102
103/**
104 * Uninitializes the instance and sets the ready flag to FALSE.
105 * Called either from FinalRelease() or by the parent when it gets destroyed.
106 */
107void HostUSBDevice::uninit()
108{
109 /* Enclose the state transition Ready->InUninit->NotReady */
110 AutoUninitSpan autoUninitSpan(this);
111 if (autoUninitSpan.uninitDone())
112 return;
113
114 if (mUsb != NULL)
115 {
116 USBProxyService::freeDevice(mUsb);
117 mUsb = NULL;
118 }
119
120 mUSBProxyService = NULL;
121 mUniState = kHostUSBDeviceState_Invalid;
122}
123
124// IUSBDevice properties
125/////////////////////////////////////////////////////////////////////////////
126
127STDMETHODIMP HostUSBDevice::COMGETTER(Id)(BSTR *aId)
128{
129 CheckComArgOutPointerValid(aId);
130
131 AutoCaller autoCaller(this);
132 if (FAILED(autoCaller.rc())) return autoCaller.rc();
133
134 /* mId is constant during life time, no need to lock */
135 mId.toUtf16().cloneTo(aId);
136
137 return S_OK;
138}
139
140STDMETHODIMP HostUSBDevice::COMGETTER(VendorId)(USHORT *aVendorId)
141{
142 CheckComArgOutPointerValid(aVendorId);
143
144 AutoCaller autoCaller(this);
145 if (FAILED(autoCaller.rc())) return autoCaller.rc();
146
147 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
148
149 *aVendorId = mUsb->idVendor;
150
151 return S_OK;
152}
153
154STDMETHODIMP HostUSBDevice::COMGETTER(ProductId)(USHORT *aProductId)
155{
156 CheckComArgOutPointerValid(aProductId);
157
158 AutoCaller autoCaller(this);
159 if (FAILED(autoCaller.rc())) return autoCaller.rc();
160
161 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
162
163 *aProductId = mUsb->idProduct;
164
165 return S_OK;
166}
167
168STDMETHODIMP HostUSBDevice::COMGETTER(Revision)(USHORT *aRevision)
169{
170 CheckComArgOutPointerValid(aRevision);
171
172 AutoCaller autoCaller(this);
173 if (FAILED(autoCaller.rc())) return autoCaller.rc();
174
175 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
176
177 *aRevision = mUsb->bcdDevice;
178
179 return S_OK;
180}
181
182STDMETHODIMP HostUSBDevice::COMGETTER(Manufacturer)(BSTR *aManufacturer)
183{
184 CheckComArgOutPointerValid(aManufacturer);
185
186 AutoCaller autoCaller(this);
187 if (FAILED(autoCaller.rc())) return autoCaller.rc();
188
189 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
190
191 Bstr(mUsb->pszManufacturer).cloneTo(aManufacturer);
192
193 return S_OK;
194}
195
196STDMETHODIMP HostUSBDevice::COMGETTER(Product)(BSTR *aProduct)
197{
198 CheckComArgOutPointerValid(aProduct);
199
200 AutoCaller autoCaller(this);
201 if (FAILED(autoCaller.rc())) return autoCaller.rc();
202
203 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
204
205 Bstr(mUsb->pszProduct).cloneTo(aProduct);
206
207 return S_OK;
208}
209
210STDMETHODIMP HostUSBDevice::COMGETTER(SerialNumber)(BSTR *aSerialNumber)
211{
212 CheckComArgOutPointerValid(aSerialNumber);
213
214 AutoCaller autoCaller(this);
215 if (FAILED(autoCaller.rc())) return autoCaller.rc();
216
217 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
218
219 Bstr(mUsb->pszSerialNumber).cloneTo(aSerialNumber);
220
221 return S_OK;
222}
223
224STDMETHODIMP HostUSBDevice::COMGETTER(Address)(BSTR *aAddress)
225{
226 CheckComArgOutPointerValid(aAddress);
227
228 AutoCaller autoCaller(this);
229 if (FAILED(autoCaller.rc())) return autoCaller.rc();
230
231 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
232
233 Bstr(mUsb->pszAddress).cloneTo(aAddress);
234
235 return S_OK;
236}
237
238STDMETHODIMP HostUSBDevice::COMGETTER(Port)(USHORT *aPort)
239{
240 CheckComArgOutPointerValid(aPort);
241
242 AutoCaller autoCaller(this);
243 if (FAILED(autoCaller.rc())) return autoCaller.rc();
244
245 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
246
247#if !defined(RT_OS_WINDOWS) /// @todo check up the bPort value on Windows before enabling this.
248 *aPort = mUsb->bPort;
249#else
250 *aPort = 0;
251#endif
252
253 return S_OK;
254}
255
256STDMETHODIMP HostUSBDevice::COMGETTER(Version)(USHORT *aVersion)
257{
258 CheckComArgOutPointerValid(aVersion);
259
260 AutoCaller autoCaller(this);
261 if (FAILED(autoCaller.rc())) return autoCaller.rc();
262
263 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
264
265 *aVersion = mUsb->bcdUSB >> 8;
266
267 return S_OK;
268}
269
270STDMETHODIMP HostUSBDevice::COMGETTER(PortVersion)(USHORT *aPortVersion)
271{
272 CheckComArgOutPointerValid(aPortVersion);
273
274 AutoCaller autoCaller(this);
275 if (FAILED(autoCaller.rc())) return autoCaller.rc();
276
277 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
278
279 /* Port version is 2 (EHCI) if and only if the device runs at high speed;
280 * if speed is unknown, fall back to the old and inaccurate method.
281 */
282 if (mUsb->enmSpeed == USBDEVICESPEED_UNKNOWN)
283 *aPortVersion = mUsb->bcdUSB >> 8;
284 else
285 *aPortVersion = (mUsb->enmSpeed == USBDEVICESPEED_HIGH) ? 2 : 1;
286
287 return S_OK;
288}
289
290STDMETHODIMP HostUSBDevice::COMGETTER(Remote)(BOOL *aRemote)
291{
292 CheckComArgOutPointerValid(aRemote);
293
294 AutoCaller autoCaller(this);
295 if (FAILED(autoCaller.rc())) return autoCaller.rc();
296
297 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
298
299 *aRemote = FALSE;
300
301 return S_OK;
302}
303
304// IHostUSBDevice properties
305/////////////////////////////////////////////////////////////////////////////
306
307STDMETHODIMP HostUSBDevice::COMGETTER(State)(USBDeviceState_T *aState)
308{
309 CheckComArgOutPointerValid(aState);
310
311 AutoCaller autoCaller(this);
312 if (FAILED(autoCaller.rc())) return autoCaller.rc();
313
314 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
315
316 *aState = canonicalState();
317
318 return S_OK;
319}
320
321
322// public methods only for internal purposes
323////////////////////////////////////////////////////////////////////////////////
324
325/**
326 * @note Locks this object for reading.
327 */
328Utf8Str HostUSBDevice::getName()
329{
330 Utf8Str name;
331
332 AutoCaller autoCaller(this);
333 AssertComRCReturn(autoCaller.rc(), name);
334
335 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
336
337 bool haveManufacturer = mUsb->pszManufacturer && *mUsb->pszManufacturer;
338 bool haveProduct = mUsb->pszProduct && *mUsb->pszProduct;
339 if (haveManufacturer && haveProduct)
340 name = Utf8StrFmt("%s %s", mUsb->pszManufacturer, mUsb->pszProduct);
341 else if (haveManufacturer)
342 name = Utf8StrFmt("%s", mUsb->pszManufacturer);
343 else if (haveProduct)
344 name = Utf8StrFmt("%s", mUsb->pszProduct);
345 else
346 name = "<unknown>";
347
348 return name;
349}
350
351/**
352 * Requests the USB proxy service capture the device (from the host)
353 * and attach it to a VM.
354 *
355 * As a convenience, this method will operate like attachToVM() if the device
356 * is already held by the proxy. Note that it will then perform IPC to the VM
357 * process, which means it will temporarily release all locks. (Is this a good idea?)
358 *
359 * @param aMachine Machine this device should be attach to.
360 * @param aSetError Whether to set full error message or not to bother.
361 * @param aMaskedIfs The interfaces to hide from the guest.
362 *
363 * @returns Status indicating whether it was successfully captured and/or attached.
364 * @retval S_OK on success.
365 * @retval E_UNEXPECTED if the device state doesn't permit for any attaching.
366 * @retval E_* as appropriate.
367 */
368HRESULT HostUSBDevice::requestCaptureForVM(SessionMachine *aMachine, bool aSetError, ULONG aMaskedIfs /* = 0*/)
369{
370 /*
371 * Validate preconditions and input.
372 */
373 AssertReturn(aMachine, E_INVALIDARG);
374 AssertReturn(!isWriteLockOnCurrentThread(), E_FAIL);
375 AssertReturn(!aMachine->isWriteLockOnCurrentThread(), E_FAIL);
376
377 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
378 LogFlowThisFunc(("{%s} aMachine=%p aMaskedIfs=%#x\n", mName, aMachine, aMaskedIfs));
379
380 if (aSetError)
381 {
382 if (mUniState == kHostUSBDeviceState_Unsupported)
383 return setError(E_INVALIDARG,
384 tr("USB device '%s' with UUID {%RTuuid} cannot be accessed by guest computers"),
385 mName, mId.raw());
386 if (mUniState == kHostUSBDeviceState_UsedByHost)
387 return setError(E_INVALIDARG,
388 tr("USB device '%s' with UUID {%RTuuid} is being exclusively used by the host computer"),
389 mName, mId.raw());
390 if (mUniState == kHostUSBDeviceState_UsedByVM)
391 {
392 /* Machine::name() requires a read lock */
393 alock.release();
394 AutoReadLock machLock(mMachine COMMA_LOCKVAL_SRC_POS);
395 return setError(E_INVALIDARG,
396 tr("USB device '%s' with UUID {%RTuuid} is already captured by the virtual machine '%s'"),
397 mName, mId.raw(), mMachine->getName().c_str());
398 }
399 if (mUniState >= kHostUSBDeviceState_FirstTransitional)
400 return setError(E_INVALIDARG,
401 tr("USB device '%s' with UUID {%RTuuid} is busy with a previous request. Please try again later"),
402 mName, mId.raw());
403 if ( mUniState != kHostUSBDeviceState_Unused
404 && mUniState != kHostUSBDeviceState_HeldByProxy
405 && mUniState != kHostUSBDeviceState_Capturable)
406 return setError(E_INVALIDARG,
407 tr("USB device '%s' with UUID {%RTuuid} is not in the right state for capturing (%s)"),
408 mName, mId.raw(), getStateName());
409 }
410
411 AssertReturn( mUniState == kHostUSBDeviceState_HeldByProxy
412 || mUniState == kHostUSBDeviceState_Unused
413 || mUniState == kHostUSBDeviceState_Capturable,
414 E_UNEXPECTED);
415 Assert(mMachine.isNull());
416
417 /*
418 * If it's already held by the proxy, we'll simply call
419 * attachToVM synchronously.
420 */
421 if (mUniState == kHostUSBDeviceState_HeldByProxy)
422 {
423 alock.release();
424 HRESULT hrc = attachToVM(aMachine, aMaskedIfs);
425 return SUCCEEDED(hrc);
426 }
427
428 /*
429 * Need to capture the device before it can be used.
430 *
431 * The device will be attached to the VM by the USB proxy service thread
432 * when the request succeeds (i.e. asynchronously).
433 */
434 LogFlowThisFunc(("{%s} capturing the device.\n", mName));
435#if (defined(RT_OS_DARWIN) && defined(VBOX_WITH_NEW_USB_CODE_ON_DARWIN)) /* PORTME */ \
436 || defined(RT_OS_WINDOWS) || defined(RT_OS_SOLARIS)
437 setState(kHostUSBDeviceState_Capturing, kHostUSBDeviceState_UsedByVM, kHostUSBDeviceSubState_AwaitingDetach);
438#else
439 setState(kHostUSBDeviceState_Capturing, kHostUSBDeviceState_UsedByVM);
440#endif
441 mMachine = aMachine;
442 mMaskedIfs = aMaskedIfs;
443 alock.release();
444 int rc = mUSBProxyService->captureDevice(this);
445 if (RT_FAILURE(rc))
446 {
447 alock.acquire();
448 failTransition();
449 mMachine.setNull();
450 if (rc == VERR_SHARING_VIOLATION)
451 return setError(E_FAIL,
452 tr("USB device '%s' with UUID {%RTuuid} is in use by someone else"),
453 mName, mId.raw());
454 return E_FAIL;
455 }
456
457 return S_OK;
458}
459
460/**
461 * Attempts to attach the USB device to a VM.
462 *
463 * The device must be in the HeldByProxy state or about to exit the
464 * Capturing state.
465 *
466 * This method will make an IPC to the VM process and do the actual
467 * attaching. While in the IPC locks will be abandond.
468 *
469 * @returns Status indicating whether it was successfully attached or not.
470 * @retval S_OK on success.
471 * @retval E_UNEXPECTED if the device state doesn't permit for any attaching.
472 * @retval E_* as appropriate.
473 *
474 * @param aMachine Machine this device should be attach to.
475 * @param aMaskedIfs The interfaces to hide from the guest.
476 */
477HRESULT HostUSBDevice::attachToVM(SessionMachine *aMachine, ULONG aMaskedIfs /* = 0*/)
478{
479 AssertReturn(!isWriteLockOnCurrentThread(), E_FAIL);
480 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
481 /*
482 * Validate and update the state.
483 */
484 AssertReturn( mUniState == kHostUSBDeviceState_Capturing
485 || mUniState == kHostUSBDeviceState_HeldByProxy
486 || mUniState == kHostUSBDeviceState_AttachingToVM,
487 E_UNEXPECTED);
488 setState(kHostUSBDeviceState_AttachingToVM, kHostUSBDeviceState_UsedByVM);
489
490 /*
491 * The VM process will query the object, so grab a reference to ourselves and release the locks.
492 */
493 ComPtr<IUSBDevice> d = this;
494
495 /*
496 * Call the VM process (IPC) and request it to attach the device.
497 *
498 * There are many reasons for this to fail, so, as a consequence we don't
499 * assert the return code as it will crash the daemon and annoy the heck
500 * out of people.
501 */
502 LogFlowThisFunc(("{%s} Calling machine->onUSBDeviceAttach()...\n", mName));
503 alock.release();
504 HRESULT hrc = aMachine->onUSBDeviceAttach(d, NULL, aMaskedIfs);
505 LogFlowThisFunc(("{%s} Done machine->onUSBDeviceAttach()=%08X\n", mName, hrc));
506
507 /*
508 * As we re-acquire the lock, we'll have to check if the device was
509 * physically detached while we were busy.
510 */
511 alock.acquire();
512
513 if (SUCCEEDED(hrc))
514 {
515 mMachine = aMachine;
516 if (!mIsPhysicallyDetached)
517 setState(kHostUSBDeviceState_UsedByVM);
518 else
519 {
520 alock.release();
521 detachFromVM(kHostUSBDeviceState_PhysDetached);
522 hrc = E_UNEXPECTED;
523 }
524 }
525 else
526 {
527 mMachine.setNull();
528 if (!mIsPhysicallyDetached)
529 {
530 setState(kHostUSBDeviceState_HeldByProxy);
531 if (hrc == E_UNEXPECTED)
532 hrc = E_FAIL; /* No confusion. */
533 }
534 else
535 {
536 alock.release();
537 onPhysicalDetachedInternal();
538 hrc = E_UNEXPECTED;
539 }
540 }
541 return hrc;
542}
543
544
545/**
546 * Detaches the device from the VM.
547 *
548 * This is used for a special scenario in attachToVM() and from
549 * onPhysicalDetachedInternal().
550 *
551 * @param aFinalState The final state (PhysDetached).
552 */
553void HostUSBDevice::detachFromVM(HostUSBDeviceState aFinalState)
554{
555 NOREF(aFinalState);
556
557 /*
558 * Assert preconditions.
559 */
560 Assert(aFinalState == kHostUSBDeviceState_PhysDetached);
561 AssertReturnVoid(!isWriteLockOnCurrentThread());
562 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
563 Assert( mUniState == kHostUSBDeviceState_AttachingToVM
564 || mUniState == kHostUSBDeviceState_UsedByVM);
565 Assert(!mMachine.isNull());
566
567 /*
568 * Change the state and abandon the locks. The VM may query
569 * data and we don't want to deadlock - the state protects us,
570 * so, it's not a bit issue here.
571 */
572 setState(kHostUSBDeviceState_PhysDetachingFromVM, kHostUSBDeviceState_PhysDetached);
573
574 /*
575 * Call the VM process (IPC) and request it to detach the device.
576 *
577 * There are many reasons for this to fail, so, as a consequence we don't
578 * assert the return code as it will crash the daemon and annoy the heck
579 * out of people.
580 */
581 alock.release();
582 LogFlowThisFunc(("{%s} Calling machine->onUSBDeviceDetach()...\n", mName));
583 HRESULT hrc = mMachine->onUSBDeviceDetach(mId.toUtf16().raw(), NULL);
584 LogFlowThisFunc(("{%s} Done machine->onUSBDeviceDetach()=%Rhrc\n", mName, hrc));
585 NOREF(hrc);
586
587 /*
588 * Re-acquire the locks and complete the transition.
589 */
590 alock.acquire();
591 advanceTransition();
592}
593
594/**
595 * Called when the VM process to inform us about the device being
596 * detached from it.
597 *
598 * This is NOT called when we detach the device via onUSBDeviceDetach.
599 *
600 *
601 * @param[in] aMachine The machine making the request.
602 * This must be the machine this device is currently attached to.
603 * @param[in] aDone When set to false, the VM just informs us that it's about
604 * to detach this device but hasn't done it just yet.
605 * When set to true, the VM informs us that it has completed
606 * the detaching of this device.
607 * @param[out] aRunFilters Whether to run filters.
608 * @param[in] aAbnormal Set if we're cleaning up after a crashed VM.
609 *
610 * @returns S_OK on success, and E_UNEXPECTED if the device isn't in the right state.
611 *
612 * @note Must be called from under the object write lock.
613 */
614HRESULT HostUSBDevice::onDetachFromVM(SessionMachine *aMachine, bool aDone, bool *aRunFilters, bool aAbnormal /*= true*/)
615{
616 LogFlowThisFunc(("{%s} state=%s aDone=%RTbool aAbnormal=%RTbool\n", mName, getStateName(), aDone, aAbnormal));
617
618 /*
619 * Validate preconditions.
620 */
621 AssertPtrReturn(aRunFilters, E_INVALIDARG);
622 AssertReturn(isWriteLockOnCurrentThread(), E_FAIL);
623 if (!aDone)
624 {
625 if (mUniState != kHostUSBDeviceState_UsedByVM)
626 return setError(E_INVALIDARG,
627 tr("USB device '%s' with UUID {%RTuuid} is busy (state '%s'). Please try again later"),
628 mName, mId.raw(), getStateName());
629 }
630 else
631 AssertMsgReturn( mUniState == kHostUSBDeviceState_DetachingFromVM /** @todo capturing for VM ends up here on termination. */
632 || (mUniState == kHostUSBDeviceState_UsedByVM && aAbnormal),
633 ("{%s} %s\n", mName, getStateName()), E_UNEXPECTED);
634 AssertMsgReturn((mMachine == aMachine), ("%p != %p\n", (void *)mMachine, aMachine), E_FAIL);
635
636 /*
637 * Change the state.
638 */
639 if (!aDone)
640 {
641 *aRunFilters = startTransition(kHostUSBDeviceState_DetachingFromVM, kHostUSBDeviceState_HeldByProxy);
642 /* PORTME: This might require host specific changes if you re-enumerate the device. */
643 }
644 else if (aAbnormal && mUniState == kHostUSBDeviceState_UsedByVM)
645 {
646 /* Fast forward thru the DetachingFromVM state and on to HeldByProxy. */
647 /** @todo need to update the state machine to handle crashed VMs. */
648 startTransition(kHostUSBDeviceState_DetachingFromVM, kHostUSBDeviceState_HeldByProxy);
649 *aRunFilters = advanceTransition();
650 mMachine.setNull();
651 /* PORTME: ditto / trouble if you depend on the VM process to do anything. */
652 }
653 else
654 {
655 /* normal completion. */
656 Assert(mUniSubState == kHostUSBDeviceSubState_Default); /* PORTME: ditto */
657 *aRunFilters = advanceTransition();
658 mMachine.setNull();
659 }
660
661 return S_OK;
662}
663
664/**
665 * Requests the USB proxy service to release the device back to the host.
666 *
667 * This method will ignore (not assert) calls for devices that already
668 * belong to the host because it simplifies the usage a bit.
669 *
670 * @returns COM status code.
671 * @retval S_OK on success.
672 * @retval E_UNEXPECTED on bad state.
673 * @retval E_* as appropriate.
674 *
675 * @note Must be called without holding the object lock.
676 */
677HRESULT HostUSBDevice::requestReleaseToHost()
678{
679 /*
680 * Validate preconditions.
681 */
682 AssertReturn(!isWriteLockOnCurrentThread(), E_FAIL);
683 Assert(mMachine.isNull());
684
685 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
686 LogFlowThisFunc(("{%s}\n", mName));
687 if ( mUniState == kHostUSBDeviceState_Unused
688 || mUniState == kHostUSBDeviceState_Capturable)
689 return S_OK;
690 AssertMsgReturn(mUniState == kHostUSBDeviceState_HeldByProxy, ("{%s} %s\n", mName, getStateName()), E_UNEXPECTED);
691
692 /*
693 * Try release it.
694 */
695#if (defined(RT_OS_DARWIN) && defined(VBOX_WITH_NEW_USB_CODE_ON_DARWIN)) /* PORTME */ \
696 || defined(RT_OS_WINDOWS)
697 startTransition(kHostUSBDeviceState_ReleasingToHost, kHostUSBDeviceState_Unused, kHostUSBDeviceSubState_AwaitingDetach);
698#else
699 startTransition(kHostUSBDeviceState_ReleasingToHost, kHostUSBDeviceState_Unused);
700#endif
701 alock.release();
702 int rc = mUSBProxyService->releaseDevice(this);
703 if (RT_FAILURE(rc))
704 {
705 alock.acquire();
706 failTransition();
707 return E_FAIL;
708 }
709 return S_OK;
710}
711
712/**
713 * Requests the USB proxy service to capture and hold the device.
714 *
715 * The device must be owned by the host at the time of the call. But for
716 * the callers convenience, calling this method on a device that is already
717 * being held will success without any assertions.
718 *
719 * @returns COM status code.
720 * @retval S_OK on success.
721 * @retval E_UNEXPECTED on bad state.
722 * @retval E_* as appropriate.
723 *
724 * @note Must be called without holding the object lock.
725 */
726HRESULT HostUSBDevice::requestHold()
727{
728 /*
729 * Validate preconditions.
730 */
731 AssertReturn(!isWriteLockOnCurrentThread(), E_FAIL);
732 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
733 LogFlowThisFunc(("{%s}\n", mName));
734 AssertMsgReturn( mUniState == kHostUSBDeviceState_Unused
735 || mUniState == kHostUSBDeviceState_Capturable
736 || mUniState == kHostUSBDeviceState_HeldByProxy,
737 ("{%s} %s\n", mName, getStateName()),
738 E_UNEXPECTED);
739
740 Assert(mMachine.isNull());
741 mMachine.setNull();
742
743 if (mUniState == kHostUSBDeviceState_HeldByProxy)
744 return S_OK;
745
746 /*
747 * Do the job.
748 */
749#if (defined(RT_OS_DARWIN) && defined(VBOX_WITH_NEW_USB_CODE_ON_DARWIN)) /* PORTME */ \
750 || defined(RT_OS_WINDOWS)
751 startTransition(kHostUSBDeviceState_Capturing, kHostUSBDeviceState_HeldByProxy, kHostUSBDeviceSubState_AwaitingDetach);
752#else
753 startTransition(kHostUSBDeviceState_Capturing, kHostUSBDeviceState_HeldByProxy);
754#endif
755 alock.release();
756 int rc = mUSBProxyService->captureDevice(this);
757 if (RT_FAILURE(rc))
758 {
759 alock.acquire();
760 failTransition();
761 return E_FAIL;
762 }
763 return S_OK;
764}
765
766
767/**
768 * Check a detach detected by the USB Proxy Service to see if
769 * it's a real one or just a logical following a re-enumeration.
770 *
771 * This will work the internal sub state of the device and do time
772 * outs, so it does more than just querying data!
773 *
774 * @returns true if it was actually detached, false if it's just a re-enumeration.
775 */
776bool HostUSBDevice::wasActuallyDetached()
777{
778 /*
779 * This only applies to the detach and re-attach states.
780 */
781 switch (mUniState)
782 {
783 case kHostUSBDeviceState_Capturing:
784 case kHostUSBDeviceState_ReleasingToHost:
785 case kHostUSBDeviceState_AttachingToVM:
786 case kHostUSBDeviceState_DetachingFromVM:
787 switch (mUniSubState)
788 {
789 /*
790 * If we're awaiting a detach, the this has now occurred
791 * and the state should be advanced.
792 */
793 case kHostUSBDeviceSubState_AwaitingDetach:
794 advanceTransition();
795 return false; /* not physically detached. */
796
797 /*
798 * Check for timeouts.
799 */
800 case kHostUSBDeviceSubState_AwaitingReAttach:
801 {
802#ifndef RT_OS_WINDOWS /* check the implementation details here. */
803 uint64_t elapsedNanoseconds = RTTimeNanoTS() - mLastStateChangeTS;
804 if (elapsedNanoseconds > UINT64_C(60000000000)) /* 60 seconds */
805 {
806 LogRel(("USB: Async operation timed out for device %s (state: %s)\n", mName, getStateName()));
807 failTransition();
808 }
809#endif
810 return false; /* not physically detached. */
811 }
812
813 /* not applicable.*/
814 case kHostUSBDeviceSubState_Default:
815 break;
816 }
817 break;
818
819 /* not applicable. */
820 case kHostUSBDeviceState_Unsupported:
821 case kHostUSBDeviceState_UsedByHost:
822 case kHostUSBDeviceState_Capturable:
823 case kHostUSBDeviceState_Unused:
824 case kHostUSBDeviceState_HeldByProxy:
825 case kHostUSBDeviceState_UsedByVM:
826 case kHostUSBDeviceState_PhysDetachingFromVM:
827 case kHostUSBDeviceState_PhysDetached:
828 break;
829
830 default:
831 AssertLogRelMsgFailed(("this=%p %s\n", this, getStateName()));
832 break;
833 }
834
835 /* It was detached. */
836 return true;
837}
838
839/**
840 * Notification from the USB Proxy that the device was physically detached.
841 *
842 * If a transition is pending, mIsPhysicallyDetached will be set and
843 * handled when the transition advances forward.
844 *
845 * Otherwise the device will be detached from any VM currently using it - this
846 * involves IPC and will temporarily abandon locks - and all the device data
847 * reset.
848 */
849void HostUSBDevice::onPhysicalDetached()
850{
851 AssertReturnVoid(!isWriteLockOnCurrentThread());
852 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
853 LogFlowThisFunc(("{%s}\n", mName));
854
855 mIsPhysicallyDetached = true;
856 if (mUniState < kHostUSBDeviceState_FirstTransitional)
857 {
858 alock.release();
859 onPhysicalDetachedInternal();
860 }
861}
862
863
864/**
865 * Do the physical detach work for a device in a stable state or
866 * at a transition state change.
867 *
868 * See onPhysicalDetach() for details.
869 */
870void HostUSBDevice::onPhysicalDetachedInternal()
871{
872 AssertReturnVoid(!isWriteLockOnCurrentThread());
873 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
874 LogFlowThisFunc(("{%s}\n", mName));
875 Assert(mIsPhysicallyDetached);
876
877 /*
878 * Do we need to detach it from the VM first?
879 */
880 if ( !mMachine.isNull()
881 && ( mUniState == kHostUSBDeviceState_UsedByVM
882 || mUniState == kHostUSBDeviceState_AttachingToVM))
883 {
884 alock.release();
885 detachFromVM(kHostUSBDeviceState_PhysDetached);
886 alock.acquire();
887 }
888 else
889 AssertMsg(mMachine.isNull(), ("%s\n", getStateName()));
890
891 /*
892 * Reset the data and enter the final state.
893 */
894 mMachine.setNull();
895 setState(kHostUSBDeviceState_PhysDetached);
896}
897
898
899/**
900 * Returns true if this device matches the given filter data.
901 *
902 * @note It is assumed, that the filter data owner is appropriately
903 * locked before calling this method.
904 *
905 * @note
906 * This method MUST correlate with
907 * USBController::hasMatchingFilter (IUSBDevice *)
908 * in the sense of the device matching logic.
909 *
910 * @note Locks this object for reading.
911 */
912bool HostUSBDevice::isMatch(const USBDeviceFilter::Data &aData)
913{
914 AutoCaller autoCaller(this);
915 AssertComRCReturn(autoCaller.rc(), false);
916
917 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
918
919 if (!aData.mActive)
920 return false;
921
922 if (!aData.mRemote.isMatch(FALSE))
923 return false;
924
925 if (!USBFilterMatchDevice(&aData.mUSBFilter, mUsb))
926 return false;
927
928 /* Don't match busy devices with a 100% wildcard filter - this will
929 later become a filter prop (ring-3 only). */
930 if ( mUsb->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE
931 && !USBFilterHasAnySubstatialCriteria(&aData.mUSBFilter))
932 return false;
933
934 LogFlowThisFunc(("returns true\n"));
935 return true;
936}
937
938/**
939 * Compares this device with a USBDEVICE and decides if the match or which comes first.
940 *
941 * This will take into account device re-attaching and omit the bits
942 * that may change during a device re-enumeration.
943 *
944 * @param aDev2 Device 2.
945 *
946 * @returns < 0 if this should come before aDev2.
947 * @returns 0 if this and aDev2 are equal.
948 * @returns > 0 if this should come after aDev2.
949 *
950 * @note Must be called from under the object write lock.
951 */
952int HostUSBDevice::compare(PCUSBDEVICE aDev2)
953{
954 AssertReturn(isWriteLockOnCurrentThread(), -1);
955 //Log3(("%Rfn: %p {%s}\n", __PRETTY_FUNCTION__, this, mName));
956 return compare(mUsb, aDev2,
957 mUniSubState == kHostUSBDeviceSubState_AwaitingDetach /* (In case we don't get the detach notice.) */
958 || mUniSubState == kHostUSBDeviceSubState_AwaitingReAttach);
959}
960
961/**
962 * Compares two USBDEVICE structures and decides if the match or which comes first.
963 *
964 * @param aDev1 Device 1.
965 * @param aDev2 Device 2.
966 * @param aIsAwaitingReAttach Whether to omit bits that will change in a device
967 * re-enumeration (true) or not (false).
968 *
969 * @returns < 0 if aDev1 should come before aDev2.
970 * @returns 0 if aDev1 and aDev2 are equal.
971 * @returns > 0 if aDev1 should come after aDev2.
972 */
973/*static*/
974int HostUSBDevice::compare(PCUSBDEVICE aDev1, PCUSBDEVICE aDev2, bool aIsAwaitingReAttach /*= false */)
975{
976 /*
977 * Things that stays the same everywhere.
978 *
979 * The more uniquely these properties identifies a device the less the chance
980 * that we mix similar devices during re-enumeration. Bus+port would help
981 * provide ~99.8% accuracy if the host can provide those attributes.
982 */
983 int iDiff = aDev1->idVendor - aDev2->idVendor;
984 if (iDiff)
985 return iDiff;
986
987 iDiff = aDev1->idProduct - aDev2->idProduct;
988 if (iDiff)
989 return iDiff;
990
991 iDiff = aDev1->bcdDevice - aDev2->bcdDevice;
992 if (iDiff)
993 {
994 //Log3(("compare: bcdDevice: %#x != %#x\n", aDev1->bcdDevice, aDev2->bcdDevice));
995 return iDiff;
996 }
997
998#ifdef RT_OS_WINDOWS /* the string query may fail on windows during replugging, ignore serial mismatch if this is the case. */
999 if ( aDev1->u64SerialHash != aDev2->u64SerialHash
1000 && ( !aIsAwaitingReAttach
1001 || (aDev2->pszSerialNumber && *aDev2->pszSerialNumber)
1002 || (aDev2->pszManufacturer && *aDev2->pszManufacturer)
1003 || (aDev2->pszProduct && *aDev2->pszProduct))
1004 )
1005#else
1006 if (aDev1->u64SerialHash != aDev2->u64SerialHash)
1007#endif
1008 {
1009 //Log3(("compare: u64SerialHash: %#llx != %#llx\n", aDev1->u64SerialHash, aDev2->u64SerialHash));
1010 return aDev1->u64SerialHash < aDev2->u64SerialHash ? -1 : 1;
1011 }
1012
1013 /* The hub/bus + port should help a lot in a re-attach situation. */
1014#ifdef RT_OS_WINDOWS
1015 iDiff = strcmp(aDev1->pszHubName, aDev2->pszHubName);
1016 if (iDiff)
1017 {
1018 //Log3(("compare: HubName: %s != %s\n", aDev1->pszHubName, aDev2->pszHubName));
1019 return iDiff;
1020 }
1021#else
1022 iDiff = aDev1->bBus - aDev2->bBus;
1023 if (iDiff)
1024 {
1025 //Log3(("compare: bBus: %#x != %#x\n", aDev1->bBus, aDev2->bBus));
1026 return iDiff;
1027 }
1028#endif
1029
1030 iDiff = aDev1->bPort - aDev2->bPort; /* shouldn't change anywhere and help pinpoint it very accurately. */
1031 if (iDiff)
1032 {
1033 //Log3(("compare: bPort: %#x != %#x\n", aDev1->bPort, aDev2->bPort));
1034 return iDiff;
1035 }
1036
1037 /*
1038 * Things that usually doesn't stay the same when re-enumerating
1039 * a device. The fewer things in the category the better chance
1040 * that we avoid messing up when more than one device of the same
1041 * kind is attached.
1042 */
1043 if (aIsAwaitingReAttach)
1044 {
1045 //Log3(("aDev1=%p == aDev2=%p\n", aDev1, aDev2));
1046 return 0;
1047 }
1048 /* device number always changes. */
1049 return strcmp(aDev1->pszAddress, aDev2->pszAddress);
1050}
1051
1052/**
1053 * Updates the state of the device.
1054 *
1055 * If this method returns @c true, Host::onUSBDeviceStateChanged() will be
1056 * called to process the state change (complete the state change request,
1057 * inform the VM process etc.).
1058 *
1059 * If this method returns @c false, it is assumed that the given state change
1060 * is "minor": it doesn't require any further action other than update the
1061 * mState field with the actual state value.
1062 *
1063 * Regardless of the return value, this method always takes ownership of the
1064 * new USBDEVICE structure passed in and updates the pNext and pPrev fiends in
1065 * it using the values of the old structure.
1066 *
1067 * @param[in] aDev The current device state as seen by the proxy backend.
1068 * @param[out] aRunFilters Whether the state change should be accompanied by
1069 * running filters on the device.
1070 * @param[out] aIgnoreMachine Machine to ignore when running filters.
1071 *
1072 * @returns Whether the Host object should be bothered with this state change.
1073 *
1074 * @todo Just do everything here, that is, call filter runners and everything that
1075 * works by state change. Using 3 return codes/parameters is just plain ugly.
1076 */
1077bool HostUSBDevice::updateState(PCUSBDEVICE aDev, bool *aRunFilters, SessionMachine **aIgnoreMachine)
1078{
1079 *aRunFilters = false;
1080 *aIgnoreMachine = NULL;
1081
1082 /*
1083 * Locking.
1084 */
1085 AssertReturn(!isWriteLockOnCurrentThread(), false);
1086 AutoCaller autoCaller(this);
1087 AssertComRCReturn(autoCaller.rc(), false);
1088 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1089
1090 /*
1091 * Replace the existing structure by the new one.
1092 */
1093 const USBDEVICESTATE enmOldState = mUsb->enmState; NOREF(enmOldState);
1094 if (mUsb != aDev)
1095 {
1096#ifdef RT_OS_WINDOWS
1097 /* we used this logic of string comparison in HostUSBDevice::compare
1098 * now we need to preserve strings from the old device if the new device has zero strings
1099 * this ensures the device is correctly matched later on
1100 * otherwise we may end up with a phantom misconfigured device instance */
1101 if ((mUniSubState == kHostUSBDeviceSubState_AwaitingDetach /* (In case we don't get the detach notice.) */
1102 || mUniSubState == kHostUSBDeviceSubState_AwaitingReAttach)
1103 && (!aDev->pszSerialNumber || !*aDev->pszSerialNumber)
1104 && (!aDev->pszManufacturer || !*aDev->pszManufacturer)
1105 && (!aDev->pszProduct || !*aDev->pszProduct))
1106 {
1107 aDev->u64SerialHash = mUsb->u64SerialHash;
1108
1109 if (mUsb->pszSerialNumber && *mUsb->pszSerialNumber)
1110 {
1111 if (aDev->pszSerialNumber)
1112 RTStrFree((char *)aDev->pszSerialNumber);
1113
1114 /* since we're going to free old device later on,
1115 * we can just assign the string from it to the new device
1116 * and zero up the string filed for the old device */
1117 aDev->pszSerialNumber = mUsb->pszSerialNumber;
1118 mUsb->pszSerialNumber = NULL;
1119 }
1120
1121 if (mUsb->pszManufacturer && *mUsb->pszManufacturer)
1122 {
1123 if (aDev->pszManufacturer)
1124 RTStrFree((char *)aDev->pszManufacturer);
1125
1126 /* since we're going to free old device later on,
1127 * we can just assign the string from it to the new device
1128 * and zero up the string filed for the old device */
1129 aDev->pszManufacturer = mUsb->pszManufacturer;
1130 mUsb->pszManufacturer = NULL;
1131 }
1132
1133 if (mUsb->pszProduct && *mUsb->pszProduct)
1134 {
1135 if (aDev->pszProduct)
1136 RTStrFree((char *)aDev->pszProduct);
1137
1138 /* since we're going to free old device later on,
1139 * we can just assign the string from it to the new device
1140 * and zero up the string filed for the old device */
1141 aDev->pszProduct = mUsb->pszProduct;
1142 mUsb->pszProduct = NULL;
1143 }
1144 }
1145#endif
1146 aDev->pNext = mUsb->pNext;
1147 aDev->pPrev = mUsb->pPrev;
1148 USBProxyService::freeDevice(mUsb);
1149 mUsb = aDev;
1150 }
1151
1152/** @def HOSTUSBDEVICE_FUZZY_STATE
1153 * Defined on hosts where we have a driver that keeps proper device states.
1154 */
1155# if defined(RT_OS_LINUX) || defined(DOXYGEN_RUNNING)
1156# define HOSTUSBDEVICE_FUZZY_STATE 1
1157# else
1158# undef HOSTUSBDEVICE_FUZZY_STATE
1159# endif
1160 /*
1161 * For some hosts we'll have to be pretty careful here because
1162 * they don't always have a clue what is going on. This is
1163 * particularly true on linux and solaris, while windows and
1164 * darwin generally knows a bit more.
1165 */
1166 bool fIsImportant = false;
1167 if (enmOldState != mUsb->enmState)
1168 {
1169 LogFlowThisFunc(("%p {%s} %s\n", this, mName, getStateName()));
1170 switch (mUsb->enmState)
1171 {
1172 /*
1173 * Little fuzziness here, except where we fake capture.
1174 */
1175 case USBDEVICESTATE_USED_BY_HOST:
1176 switch (mUniState)
1177 {
1178 /* Host drivers installed, that's fine. */
1179 case kHostUSBDeviceState_Capturable:
1180 case kHostUSBDeviceState_Unused:
1181 LogThisFunc(("{%s} %s -> %s\n", mName, getStateName(), stateName(kHostUSBDeviceState_UsedByHost)));
1182 *aRunFilters = setState(kHostUSBDeviceState_UsedByHost);
1183 break;
1184 case kHostUSBDeviceState_UsedByHost:
1185 break;
1186
1187 /* Can only mean that we've failed capturing it. */
1188 case kHostUSBDeviceState_Capturing:
1189 LogThisFunc(("{%s} capture failed!\n", mName));
1190 mUSBProxyService->captureDeviceCompleted(this, false /* aSuccess */);
1191 *aRunFilters = failTransition();
1192 mMachine.setNull();
1193 break;
1194
1195 /* Guess we've successfully released it. */
1196 case kHostUSBDeviceState_ReleasingToHost:
1197 LogThisFunc(("{%s} %s -> %s\n", mName, getStateName(), stateName(kHostUSBDeviceState_UsedByHost)));
1198 mUSBProxyService->releaseDeviceCompleted(this, true /* aSuccess */);
1199 *aRunFilters = setState(kHostUSBDeviceState_UsedByHost);
1200 break;
1201
1202 /* These are IPC states and should be left alone. */
1203 case kHostUSBDeviceState_AttachingToVM:
1204 case kHostUSBDeviceState_DetachingFromVM:
1205 case kHostUSBDeviceState_PhysDetachingFromVM:
1206 LogThisFunc(("{%s} %s - changed to USED_BY_HOST...\n", mName, getStateName()));
1207 break;
1208
1209#ifdef HOSTUSBDEVICE_FUZZY_STATE
1210 /* Fake: We can't prevent anyone from grabbing it. */
1211 case kHostUSBDeviceState_HeldByProxy:
1212 LogThisFunc(("{%s} %s -> %s!\n", mName, getStateName(), stateName(kHostUSBDeviceState_UsedByHost)));
1213 *aRunFilters = setState(kHostUSBDeviceState_UsedByHost);
1214 break;
1215 //case kHostUSBDeviceState_UsedByVM:
1216 // /** @todo needs to be detached from the VM. */
1217 // break;
1218#endif
1219 /* Not supposed to happen... */
1220#ifndef HOSTUSBDEVICE_FUZZY_STATE
1221 case kHostUSBDeviceState_HeldByProxy:
1222#endif
1223 case kHostUSBDeviceState_UsedByVM:
1224 case kHostUSBDeviceState_PhysDetached:
1225 case kHostUSBDeviceState_Unsupported:
1226 default:
1227 AssertMsgFailed(("{%s} %s\n", mName, getStateName()));
1228 break;
1229 }
1230 break;
1231
1232 /*
1233 * It changed to capturable. Fuzzy hosts might easily
1234 * confuse UsedByVM with this one.
1235 */
1236 case USBDEVICESTATE_USED_BY_HOST_CAPTURABLE:
1237 switch (mUniState)
1238 {
1239 /* No change. */
1240#ifdef HOSTUSBDEVICE_FUZZY_STATE
1241 case kHostUSBDeviceState_HeldByProxy:
1242 case kHostUSBDeviceState_UsedByVM:
1243#endif
1244 case kHostUSBDeviceState_Capturable:
1245 break;
1246
1247 /* Changed! */
1248 case kHostUSBDeviceState_UsedByHost:
1249 fIsImportant = true;
1250 case kHostUSBDeviceState_Unused:
1251 LogThisFunc(("{%s} %s -> %s\n", mName, getStateName(), stateName(kHostUSBDeviceState_Capturable)));
1252 *aRunFilters = setState(kHostUSBDeviceState_Capturable);
1253 break;
1254
1255 /* Can only mean that we've failed capturing it. */
1256 case kHostUSBDeviceState_Capturing:
1257 LogThisFunc(("{%s} capture failed!\n", mName));
1258 mUSBProxyService->captureDeviceCompleted(this, false /* aSuccess */);
1259 *aRunFilters = failTransition();
1260 mMachine.setNull();
1261 break;
1262
1263 /* Guess we've successfully released it. */
1264 case kHostUSBDeviceState_ReleasingToHost:
1265 LogThisFunc(("{%s} %s -> %s\n", mName, getStateName(), stateName(kHostUSBDeviceState_Capturable)));
1266 mUSBProxyService->releaseDeviceCompleted(this, true /* aSuccess */);
1267 *aRunFilters = setState(kHostUSBDeviceState_Capturable);
1268 break;
1269
1270 /* These are IPC states and should be left alone. */
1271 case kHostUSBDeviceState_AttachingToVM:
1272 case kHostUSBDeviceState_DetachingFromVM:
1273 case kHostUSBDeviceState_PhysDetachingFromVM:
1274 LogThisFunc(("{%s} %s - changed to USED_BY_HOST_CAPTURABLE...\n", mName, getStateName()));
1275 break;
1276
1277 /* Not supposed to happen*/
1278#ifndef HOSTUSBDEVICE_FUZZY_STATE
1279 case kHostUSBDeviceState_HeldByProxy:
1280 case kHostUSBDeviceState_UsedByVM:
1281#endif
1282 case kHostUSBDeviceState_Unsupported:
1283 case kHostUSBDeviceState_PhysDetached:
1284 default:
1285 AssertMsgFailed(("{%s} %s\n", mName, getStateName()));
1286 break;
1287 }
1288 break;
1289
1290
1291 /*
1292 * It changed to capturable. Fuzzy hosts might easily
1293 * confuse UsedByVM and HeldByProxy with this one.
1294 */
1295 case USBDEVICESTATE_UNUSED:
1296 switch (mUniState)
1297 {
1298 /* No change. */
1299#ifdef HOSTUSBDEVICE_FUZZY_STATE
1300 case kHostUSBDeviceState_HeldByProxy:
1301 case kHostUSBDeviceState_UsedByVM:
1302#endif
1303 case kHostUSBDeviceState_Unused:
1304 break;
1305
1306 /* Changed! */
1307 case kHostUSBDeviceState_UsedByHost:
1308 case kHostUSBDeviceState_Capturable:
1309 fIsImportant = true;
1310 LogThisFunc(("{%s} %s -> %s\n", mName, getStateName(), stateName(kHostUSBDeviceState_Unused)));
1311 *aRunFilters = setState(kHostUSBDeviceState_Unused);
1312 break;
1313
1314 /* Can mean that we've failed capturing it, but on windows it is the detach signal. */
1315 case kHostUSBDeviceState_Capturing:
1316#if defined(RT_OS_WINDOWS)
1317 if (mUniSubState == kHostUSBDeviceSubState_AwaitingDetach)
1318 {
1319 LogThisFunc(("{%s} capture advancing thru UNUSED...\n", mName));
1320 *aRunFilters = advanceTransition();
1321 }
1322 else
1323#endif
1324 {
1325 LogThisFunc(("{%s} capture failed!\n", mName));
1326 mUSBProxyService->captureDeviceCompleted(this, false /* aSuccess */);
1327 *aRunFilters = failTransition();
1328 mMachine.setNull();
1329 }
1330 break;
1331
1332 /* Guess we've successfully released it. */
1333 case kHostUSBDeviceState_ReleasingToHost:
1334 LogThisFunc(("{%s} %s -> %s\n", mName, getStateName(), stateName(kHostUSBDeviceState_Unused)));
1335 mUSBProxyService->releaseDeviceCompleted(this, true /* aSuccess */);
1336 *aRunFilters = setState(kHostUSBDeviceState_Unused);
1337 break;
1338
1339 /* These are IPC states and should be left alone. */
1340 case kHostUSBDeviceState_AttachingToVM:
1341 case kHostUSBDeviceState_DetachingFromVM:
1342 case kHostUSBDeviceState_PhysDetachingFromVM:
1343 LogThisFunc(("{%s} %s - changed to UNUSED...\n", mName, getStateName()));
1344 break;
1345
1346 /* Not supposed to happen*/
1347#ifndef HOSTUSBDEVICE_FUZZY_STATE
1348 case kHostUSBDeviceState_HeldByProxy:
1349 case kHostUSBDeviceState_UsedByVM:
1350#endif
1351 case kHostUSBDeviceState_Unsupported:
1352 case kHostUSBDeviceState_PhysDetached:
1353 default:
1354 AssertMsgFailed(("{%s} %s\n", mName, getStateName()));
1355 break;
1356 }
1357 break;
1358
1359 /*
1360 * This is pretty straight forward, except that everyone
1361 * might sometimes confuse this and the UsedByVM state.
1362 */
1363 case USBDEVICESTATE_HELD_BY_PROXY:
1364 switch (mUniState)
1365 {
1366 /* No change. */
1367 case kHostUSBDeviceState_HeldByProxy:
1368 break;
1369 case kHostUSBDeviceState_UsedByVM:
1370 LogThisFunc(("{%s} %s - changed to HELD_BY_PROXY...\n", mName, getStateName()));
1371 break;
1372
1373 /* Guess we've successfully captured it. */
1374 case kHostUSBDeviceState_Capturing:
1375 LogThisFunc(("{%s} capture succeeded!\n", mName));
1376 mUSBProxyService->captureDeviceCompleted(this, true /* aSuccess */);
1377 *aRunFilters = advanceTransition(true /* fast forward thru re-attach */);
1378
1379 /* Take action if we're supposed to attach it to a VM. */
1380 if (mUniState == kHostUSBDeviceState_AttachingToVM)
1381 {
1382 alock.release();
1383 attachToVM(mMachine, mMaskedIfs);
1384 alock.acquire();
1385 }
1386 break;
1387
1388 /* Can only mean that we've failed capturing it. */
1389 case kHostUSBDeviceState_ReleasingToHost:
1390 LogThisFunc(("{%s} %s failed!\n", mName, getStateName()));
1391 mUSBProxyService->releaseDeviceCompleted(this, false /* aSuccess */);
1392 *aRunFilters = setState(kHostUSBDeviceState_HeldByProxy);
1393 break;
1394
1395 /* These are IPC states and should be left alone. */
1396 case kHostUSBDeviceState_AttachingToVM:
1397 case kHostUSBDeviceState_DetachingFromVM:
1398 case kHostUSBDeviceState_PhysDetachingFromVM:
1399 LogThisFunc(("{%s} %s - changed to HELD_BY_PROXY...\n", mName, getStateName()));
1400 break;
1401
1402 /* Not supposed to happen. */
1403 case kHostUSBDeviceState_Unsupported:
1404 case kHostUSBDeviceState_UsedByHost:
1405 case kHostUSBDeviceState_Capturable:
1406 case kHostUSBDeviceState_Unused:
1407 case kHostUSBDeviceState_PhysDetached:
1408 default:
1409 AssertMsgFailed(("{%s} %s\n", mName, getStateName()));
1410 break;
1411 }
1412 break;
1413
1414 /*
1415 * This is very straight forward and only Darwin implements it.
1416 */
1417 case USBDEVICESTATE_USED_BY_GUEST:
1418 switch (mUniState)
1419 {
1420 /* No change. */
1421 case kHostUSBDeviceState_HeldByProxy:
1422 LogThisFunc(("{%s} %s - changed to USED_BY_GUEST...\n", mName, getStateName()));
1423 break;
1424 case kHostUSBDeviceState_UsedByVM:
1425 break;
1426
1427 /* These are IPC states and should be left alone. */
1428 case kHostUSBDeviceState_AttachingToVM:
1429 case kHostUSBDeviceState_DetachingFromVM:
1430 case kHostUSBDeviceState_PhysDetachingFromVM:
1431 LogThisFunc(("{%s} %s - changed to USED_BY_GUEST...\n", mName, getStateName()));
1432 break;
1433
1434 /* Not supposed to happen. */
1435 case kHostUSBDeviceState_Unsupported:
1436 case kHostUSBDeviceState_Capturable:
1437 case kHostUSBDeviceState_Unused:
1438 case kHostUSBDeviceState_UsedByHost:
1439 case kHostUSBDeviceState_PhysDetached:
1440 case kHostUSBDeviceState_ReleasingToHost:
1441 case kHostUSBDeviceState_Capturing:
1442 default:
1443 AssertMsgFailed(("{%s} %s\n", mName, getStateName()));
1444 break;
1445 }
1446 break;
1447
1448 /*
1449 * This is not supposed to happen and indicates a bug in the backend!
1450 */
1451 case USBDEVICESTATE_UNSUPPORTED:
1452 AssertMsgFailed(("enmOldState=%d {%s} %s\n", enmOldState, mName, getStateName()));
1453 break;
1454 default:
1455 AssertMsgFailed(("enmState=%d {%s} %s\n", mUsb->enmState, mName, getStateName()));
1456 break;
1457 }
1458 }
1459 else if ( mUniSubState == kHostUSBDeviceSubState_AwaitingDetach
1460 && hasAsyncOperationTimedOut())
1461 {
1462 LogRel(("USB: timeout in %s for {%RTuuid} / {%s}\n",
1463 getStateName(), mId.raw(), mName));
1464 *aRunFilters = failTransition();
1465 fIsImportant = true;
1466 }
1467 else
1468 {
1469 LogFlowThisFunc(("%p {%s} %s - no change %d\n", this, mName, getStateName(), enmOldState));
1470 /** @todo might have to handle some stuff here too if we cannot make the release/capture handling deal with that above ... */
1471 }
1472
1473 return fIsImportant;
1474}
1475
1476
1477/**
1478 * Updates the state of the device, checking for cases which we fake.
1479 *
1480 * See HostUSBDevice::updateState() for details.
1481 *
1482 * @param[in] aDev See HostUSBDevice::updateState().
1483 * @param[out] aRunFilters See HostUSBDevice::updateState()
1484 * @param[out] aIgnoreMachine See HostUSBDevice::updateState()
1485 *
1486 * @returns See HostUSBDevice::updateState()
1487 */
1488bool HostUSBDevice::updateStateFake(PCUSBDEVICE aDev, bool *aRunFilters, SessionMachine **aIgnoreMachine)
1489{
1490 Assert(!isWriteLockOnCurrentThread());
1491 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1492 const HostUSBDeviceState enmState = mUniState;
1493 switch (enmState)
1494 {
1495 case kHostUSBDeviceState_Capturing:
1496 case kHostUSBDeviceState_ReleasingToHost:
1497 {
1498 *aIgnoreMachine = mUniState == kHostUSBDeviceState_ReleasingToHost ? mMachine : NULL;
1499 *aRunFilters = advanceTransition();
1500 LogThisFunc(("{%s} %s\n", mName, getStateName()));
1501
1502 if (mUsb != aDev)
1503 {
1504 aDev->pNext = mUsb->pNext;
1505 aDev->pPrev = mUsb->pPrev;
1506 USBProxyService::freeDevice(mUsb);
1507 mUsb = aDev;
1508 }
1509
1510 /* call the completion method */
1511 if (enmState == kHostUSBDeviceState_Capturing)
1512 mUSBProxyService->captureDeviceCompleted(this, true /* aSuccess */);
1513 else
1514 mUSBProxyService->releaseDeviceCompleted(this, true /* aSuccess */);
1515
1516 /* Take action if we're supposed to attach it to a VM. */
1517 if (mUniState == kHostUSBDeviceState_AttachingToVM)
1518 {
1519 alock.release();
1520 attachToVM(mMachine, mMaskedIfs);
1521 }
1522 return true;
1523 }
1524
1525 default:
1526 alock.release();
1527 return updateState(aDev, aRunFilters, aIgnoreMachine);
1528 }
1529}
1530
1531
1532/**
1533 * Checks if there is a pending asynchronous operation and whether
1534 * it has timed out or not.
1535 *
1536 * @returns true on timeout, false if not.
1537 *
1538 * @note Caller must have read or write locked the object before calling.
1539 */
1540bool HostUSBDevice::hasAsyncOperationTimedOut() const
1541{
1542 switch (mUniSubState)
1543 {
1544#ifndef RT_OS_WINDOWS /* no timeouts on windows yet since I don't have all the details here... */
1545 case kHostUSBDeviceSubState_AwaitingDetach:
1546 case kHostUSBDeviceSubState_AwaitingReAttach:
1547 {
1548 uint64_t elapsedNanoseconds = RTTimeNanoTS() - mLastStateChangeTS;
1549 return elapsedNanoseconds > UINT64_C(60000000000); /* 60 seconds */ /* PORTME */
1550 }
1551#endif
1552 default:
1553 return false;
1554 }
1555}
1556
1557
1558/**
1559 * Translate the state into
1560 *
1561 * @returns
1562 * @param aState
1563 * @param aSubState
1564 * @param aPendingState
1565 */
1566/*static*/ const char *HostUSBDevice::stateName(HostUSBDeviceState aState,
1567 HostUSBDeviceState aPendingState /*= kHostUSBDeviceState_Invalid*/,
1568 HostUSBDeviceSubState aSubState /*= kHostUSBDeviceSubState_Default*/)
1569{
1570 switch (aState)
1571 {
1572 case kHostUSBDeviceState_Unsupported:
1573 AssertReturn(aPendingState == kHostUSBDeviceState_Invalid, "Unsupported{bad}");
1574 AssertReturn(aSubState == kHostUSBDeviceSubState_Default, "Unsupported[bad]");
1575 return "Unsupported";
1576
1577 case kHostUSBDeviceState_UsedByHost:
1578 AssertReturn(aPendingState == kHostUSBDeviceState_Invalid, "UsedByHost{bad}");
1579 AssertReturn(aSubState == kHostUSBDeviceSubState_Default, "UsedByHost[bad]");
1580 return "UsedByHost";
1581
1582 case kHostUSBDeviceState_Capturable:
1583 AssertReturn(aPendingState == kHostUSBDeviceState_Invalid, "Capturable{bad}");
1584 AssertReturn(aSubState == kHostUSBDeviceSubState_Default, "Capturable[bad]");
1585 return "Capturable";
1586
1587 case kHostUSBDeviceState_Unused:
1588 AssertReturn(aPendingState == kHostUSBDeviceState_Invalid, "Unused{bad}");
1589 AssertReturn(aSubState == kHostUSBDeviceSubState_Default, "Unused[bad]");
1590 return "Unused";
1591
1592 case kHostUSBDeviceState_HeldByProxy:
1593 AssertReturn(aPendingState == kHostUSBDeviceState_Invalid, "HeldByProxy{bad}");
1594 AssertReturn(aSubState == kHostUSBDeviceSubState_Default, "HeldByProxy[bad]");
1595 return "HeldByProxy";
1596
1597 case kHostUSBDeviceState_UsedByVM:
1598 AssertReturn(aPendingState == kHostUSBDeviceState_Invalid, "UsedByVM{bad}");
1599 AssertReturn(aSubState == kHostUSBDeviceSubState_Default, "UsedByVM[bad]");
1600 return "UsedByVM";
1601
1602 case kHostUSBDeviceState_PhysDetached:
1603 AssertReturn(aPendingState == kHostUSBDeviceState_Invalid, "PhysDetached{bad}");
1604 AssertReturn(aSubState == kHostUSBDeviceSubState_Default, "PhysDetached[bad]");
1605 return "PhysDetached";
1606
1607 case kHostUSBDeviceState_Capturing:
1608 switch (aPendingState)
1609 {
1610 case kHostUSBDeviceState_UsedByVM:
1611 switch (aSubState)
1612 {
1613 case kHostUSBDeviceSubState_Default:
1614 return "CapturingForVM";
1615 case kHostUSBDeviceSubState_AwaitingDetach:
1616 return "CapturingForVM[Detach]";
1617 case kHostUSBDeviceSubState_AwaitingReAttach:
1618 return "CapturingForVM[Attach]";
1619 default:
1620 AssertFailedReturn("CapturingForVM[bad]");
1621 }
1622 break;
1623
1624 case kHostUSBDeviceState_HeldByProxy:
1625 switch (aSubState)
1626 {
1627 case kHostUSBDeviceSubState_Default:
1628 return "CapturingForProxy";
1629 case kHostUSBDeviceSubState_AwaitingDetach:
1630 return "CapturingForProxy[Detach]";
1631 case kHostUSBDeviceSubState_AwaitingReAttach:
1632 return "CapturingForProxy[Attach]";
1633 default:
1634 AssertFailedReturn("CapturingForProxy[bad]");
1635 }
1636 break;
1637
1638 default:
1639 AssertFailedReturn("Capturing{bad}");
1640 }
1641 break;
1642
1643 case kHostUSBDeviceState_ReleasingToHost:
1644 switch (aPendingState)
1645 {
1646 case kHostUSBDeviceState_Unused:
1647 switch (aSubState)
1648 {
1649 case kHostUSBDeviceSubState_Default:
1650 return "ReleasingToHost";
1651 case kHostUSBDeviceSubState_AwaitingDetach:
1652 return "ReleasingToHost[Detach]";
1653 case kHostUSBDeviceSubState_AwaitingReAttach:
1654 return "ReleasingToHost[Attach]";
1655 default:
1656 AssertFailedReturn("ReleasingToHost[bad]");
1657 }
1658 break;
1659 default:
1660 AssertFailedReturn("ReleasingToHost{bad}");
1661 }
1662 break;
1663
1664 case kHostUSBDeviceState_DetachingFromVM:
1665 switch (aPendingState)
1666 {
1667 case kHostUSBDeviceState_HeldByProxy:
1668 switch (aSubState)
1669 {
1670 case kHostUSBDeviceSubState_Default:
1671 return "DetatchingFromVM>Proxy";
1672 case kHostUSBDeviceSubState_AwaitingDetach:
1673 return "DetatchingFromVM>Proxy[Detach]";
1674 case kHostUSBDeviceSubState_AwaitingReAttach:
1675 return "DetatchingFromVM>Proxy[Attach]";
1676 default:
1677 AssertFailedReturn("DetatchingFromVM>Proxy[bad]");
1678 }
1679 break;
1680
1681 case kHostUSBDeviceState_Unused:
1682 switch (aSubState)
1683 {
1684 case kHostUSBDeviceSubState_Default:
1685 return "DetachingFromVM>Host";
1686 case kHostUSBDeviceSubState_AwaitingDetach:
1687 return "DetachingFromVM>Host[Detach]";
1688 case kHostUSBDeviceSubState_AwaitingReAttach:
1689 return "DetachingFromVM>Host[Attach]";
1690 default:
1691 AssertFailedReturn("DetachingFromVM>Host[bad]");
1692 }
1693 break;
1694
1695 default:
1696 AssertFailedReturn("DetachingFromVM{bad}");
1697 }
1698 break;
1699
1700 case kHostUSBDeviceState_AttachingToVM:
1701 switch (aPendingState)
1702 {
1703 case kHostUSBDeviceState_UsedByVM:
1704 switch (aSubState)
1705 {
1706 case kHostUSBDeviceSubState_Default:
1707 return "AttachingToVM";
1708 case kHostUSBDeviceSubState_AwaitingDetach:
1709 return "AttachingToVM[Detach]";
1710 case kHostUSBDeviceSubState_AwaitingReAttach:
1711 return "AttachingToVM[Attach]";
1712 default:
1713 AssertFailedReturn("AttachingToVM[bad]");
1714 }
1715 break;
1716
1717 default:
1718 AssertFailedReturn("AttachingToVM{bad}");
1719 }
1720 break;
1721
1722
1723 case kHostUSBDeviceState_PhysDetachingFromVM:
1724 switch (aPendingState)
1725 {
1726 case kHostUSBDeviceState_PhysDetached:
1727 switch (aSubState)
1728 {
1729 case kHostUSBDeviceSubState_Default:
1730 return "PhysDetachingFromVM";
1731 default:
1732 AssertFailedReturn("AttachingToVM[bad]");
1733 }
1734 break;
1735
1736 default:
1737 AssertFailedReturn("AttachingToVM{bad}");
1738 }
1739 break;
1740
1741 default:
1742 AssertFailedReturn("BadState");
1743
1744 }
1745
1746 AssertFailedReturn("shouldn't get here");
1747}
1748
1749/**
1750 * Set the device state.
1751 *
1752 * This method will verify that the state transition is a legal one
1753 * according to the statemachine. It will also take care of the
1754 * associated house keeping and determine if filters needs to be applied.
1755 *
1756 * @param aNewState The new state.
1757 * @param aNewPendingState The final state of a transition when applicable.
1758 * @param aNewSubState The new sub-state when applicable.
1759 *
1760 * @returns true if filters should be applied to the device, false if not.
1761 *
1762 * @note The caller must own the write lock for this object.
1763 */
1764bool HostUSBDevice::setState(HostUSBDeviceState aNewState, HostUSBDeviceState aNewPendingState /*= kHostUSBDeviceState_Invalid*/,
1765 HostUSBDeviceSubState aNewSubState /*= kHostUSBDeviceSubState_Default*/)
1766{
1767 Assert(isWriteLockOnCurrentThread());
1768 Assert( aNewSubState == kHostUSBDeviceSubState_Default
1769 || aNewSubState == kHostUSBDeviceSubState_AwaitingDetach
1770 || aNewSubState == kHostUSBDeviceSubState_AwaitingReAttach);
1771
1772 /*
1773 * If the state is unchanged, then don't bother going
1774 * thru the validation and setting. This saves a bit of code.
1775 */
1776 if ( aNewState == mUniState
1777 && aNewPendingState == mPendingUniState
1778 && aNewSubState == mUniSubState)
1779 return false;
1780
1781 /*
1782 * Welcome to the switch orgies!
1783 * You're welcome to check out the ones in startTransition(),
1784 * advanceTransition(), failTransition() and getStateName() too. Enjoy!
1785 */
1786
1787 bool fFilters = false;
1788 HostUSBDeviceState NewPrevState = mUniState;
1789 switch (mUniState)
1790 {
1791 /*
1792 * Not much can be done with a device in this state.
1793 */
1794 case kHostUSBDeviceState_Unsupported:
1795 switch (aNewState)
1796 {
1797 case kHostUSBDeviceState_PhysDetached:
1798 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
1799 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
1800 break;
1801 default:
1802 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1803 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1804 }
1805 break;
1806
1807 /*
1808 * Only the host OS (or the user) can make changes
1809 * that'll make a device get out of this state.
1810 */
1811 case kHostUSBDeviceState_UsedByHost:
1812 switch (aNewState)
1813 {
1814 case kHostUSBDeviceState_Capturable:
1815 case kHostUSBDeviceState_Unused:
1816 fFilters = true;
1817 case kHostUSBDeviceState_PhysDetached:
1818 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
1819 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
1820 break;
1821 default:
1822 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1823 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1824 }
1825 break;
1826
1827 /*
1828 * Now it gets interesting.
1829 */
1830 case kHostUSBDeviceState_Capturable:
1831 switch (aNewState)
1832 {
1833 /* Host changes. */
1834 case kHostUSBDeviceState_Unused:
1835 fFilters = true; /* Wildcard only... */
1836 case kHostUSBDeviceState_UsedByHost:
1837 case kHostUSBDeviceState_PhysDetached:
1838 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
1839 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
1840 break;
1841
1842 /* VBox actions */
1843 case kHostUSBDeviceState_Capturing:
1844 switch (aNewPendingState)
1845 {
1846 case kHostUSBDeviceState_HeldByProxy:
1847 case kHostUSBDeviceState_UsedByVM:
1848 break;
1849 default:
1850 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1851 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1852 }
1853 break;
1854 default:
1855 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1856 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1857 }
1858 break;
1859
1860 case kHostUSBDeviceState_Unused:
1861 switch (aNewState)
1862 {
1863 /* Host changes. */
1864 case kHostUSBDeviceState_PhysDetached:
1865 case kHostUSBDeviceState_UsedByHost:
1866 case kHostUSBDeviceState_Capturable:
1867 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
1868 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
1869 break;
1870
1871 /* VBox actions */
1872 case kHostUSBDeviceState_Capturing:
1873 switch (aNewPendingState)
1874 {
1875 case kHostUSBDeviceState_HeldByProxy:
1876 case kHostUSBDeviceState_UsedByVM:
1877 break;
1878 default:
1879 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1880 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1881 }
1882 break;
1883 default:
1884 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1885 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1886 }
1887 break;
1888
1889 /*
1890 * VBox owns this device now, what's next...
1891 */
1892 case kHostUSBDeviceState_HeldByProxy:
1893 switch (aNewState)
1894 {
1895 /* Host changes. */
1896 case kHostUSBDeviceState_PhysDetached:
1897 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
1898 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
1899 break;
1900
1901 /* VBox actions */
1902 case kHostUSBDeviceState_AttachingToVM:
1903 switch (aNewPendingState)
1904 {
1905 case kHostUSBDeviceState_UsedByVM:
1906 break;
1907 default:
1908 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1909 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1910 }
1911 break;
1912 case kHostUSBDeviceState_ReleasingToHost:
1913 switch (aNewPendingState)
1914 {
1915 case kHostUSBDeviceState_Unused: /* Only this! */
1916 break;
1917 default:
1918 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1919 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1920 }
1921 break;
1922 default:
1923 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1924 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1925 }
1926 break;
1927
1928
1929 case kHostUSBDeviceState_UsedByVM:
1930 switch (aNewState)
1931 {
1932 /* Host changes. */
1933 case kHostUSBDeviceState_PhysDetachingFromVM:
1934 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
1935 Assert(aNewPendingState == kHostUSBDeviceState_PhysDetached);
1936 break;
1937
1938 /* VBox actions */
1939 case kHostUSBDeviceState_DetachingFromVM:
1940 switch (aNewPendingState)
1941 {
1942 case kHostUSBDeviceState_HeldByProxy:
1943 case kHostUSBDeviceState_Unused: /* Only this! */
1944 break;
1945 default:
1946 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1947 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1948 }
1949 break;
1950 default:
1951 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1952 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1953 }
1954 break;
1955
1956 /*
1957 * The final state.
1958 */
1959 case kHostUSBDeviceState_PhysDetached:
1960 switch (mUniState)
1961 {
1962 case kHostUSBDeviceState_Unsupported:
1963 case kHostUSBDeviceState_UsedByHost:
1964 case kHostUSBDeviceState_Capturable:
1965 case kHostUSBDeviceState_Unused:
1966 case kHostUSBDeviceState_HeldByProxy:
1967 case kHostUSBDeviceState_PhysDetachingFromVM:
1968 case kHostUSBDeviceState_DetachingFromVM: // ??
1969 case kHostUSBDeviceState_Capturing:
1970 case kHostUSBDeviceState_ReleasingToHost:
1971 break;
1972
1973 case kHostUSBDeviceState_AttachingToVM: // ??
1974 case kHostUSBDeviceState_UsedByVM:
1975 default:
1976 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
1977 stateName(aNewState, aNewPendingState, aNewSubState)), false);
1978 }
1979 break;
1980
1981
1982 /*
1983 * The transitional states.
1984 */
1985 case kHostUSBDeviceState_Capturing:
1986 NewPrevState = mPrevUniState;
1987 switch (aNewState)
1988 {
1989 /* Sub state advance. */
1990 case kHostUSBDeviceState_Capturing:
1991 switch (aNewSubState)
1992 {
1993 case kHostUSBDeviceSubState_AwaitingReAttach:
1994 Assert(mUniSubState == kHostUSBDeviceSubState_AwaitingDetach);
1995 Assert(aNewPendingState == mPendingUniState);
1996 break;
1997 default:
1998 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
1999 }
2000 break;
2001
2002 /* Host/User/Failure. */
2003 case kHostUSBDeviceState_PhysDetached:
2004 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2005 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2006 break;
2007 case kHostUSBDeviceState_UsedByHost:
2008 case kHostUSBDeviceState_Capturable:
2009 case kHostUSBDeviceState_Unused:
2010 Assert(aNewState == mPrevUniState);
2011 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2012 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2013 break;
2014
2015 /* VBox */
2016 case kHostUSBDeviceState_HeldByProxy:
2017 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2018 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2019 Assert( mPendingUniState == kHostUSBDeviceState_HeldByProxy
2020 || mPendingUniState == kHostUSBDeviceState_UsedByVM /* <- failure */ );
2021 break;
2022 case kHostUSBDeviceState_AttachingToVM:
2023 Assert(aNewPendingState == kHostUSBDeviceState_UsedByVM);
2024 NewPrevState = kHostUSBDeviceState_HeldByProxy;
2025 break;
2026
2027 default:
2028 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
2029 stateName(aNewState, aNewPendingState, aNewSubState)), false);
2030 }
2031 break;
2032
2033 case kHostUSBDeviceState_ReleasingToHost:
2034 Assert(mPrevUniState == kHostUSBDeviceState_HeldByProxy);
2035 NewPrevState = mPrevUniState;
2036 switch (aNewState)
2037 {
2038 /* Sub state advance. */
2039 case kHostUSBDeviceState_ReleasingToHost:
2040 switch (aNewSubState)
2041 {
2042 case kHostUSBDeviceSubState_AwaitingReAttach:
2043 Assert(mUniSubState == kHostUSBDeviceSubState_AwaitingDetach);
2044 Assert(aNewPendingState == mPendingUniState);
2045 break;
2046 default:
2047 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2048 }
2049 break;
2050
2051 /* Host/Failure. */
2052 case kHostUSBDeviceState_PhysDetached:
2053 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2054 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2055 break;
2056 case kHostUSBDeviceState_HeldByProxy:
2057 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2058 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2059 Assert(mPendingUniState == kHostUSBDeviceState_Unused);
2060 break;
2061
2062 /* Success */
2063 case kHostUSBDeviceState_UsedByHost:
2064 case kHostUSBDeviceState_Capturable:
2065 case kHostUSBDeviceState_Unused:
2066 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2067 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2068 Assert(mPendingUniState == kHostUSBDeviceState_Unused);
2069 break;
2070
2071 default:
2072 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
2073 stateName(aNewState, aNewPendingState, aNewSubState)), false);
2074 }
2075 break;
2076
2077 case kHostUSBDeviceState_AttachingToVM:
2078 Assert(mPrevUniState == kHostUSBDeviceState_HeldByProxy);
2079 NewPrevState = mPrevUniState;
2080 switch (aNewState)
2081 {
2082 /* Host/Failure. */
2083 case kHostUSBDeviceState_PhysDetachingFromVM:
2084 Assert(aNewPendingState == kHostUSBDeviceState_PhysDetached);
2085 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2086 break;
2087 case kHostUSBDeviceState_HeldByProxy:
2088 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2089 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2090 Assert(mPendingUniState == kHostUSBDeviceState_Unused);
2091 break;
2092
2093 /* Success */
2094 case kHostUSBDeviceState_UsedByVM:
2095 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2096 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2097 Assert(mPendingUniState == kHostUSBDeviceState_UsedByVM);
2098 break;
2099
2100 default:
2101 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
2102 stateName(aNewState, aNewPendingState, aNewSubState)), false);
2103 }
2104 break;
2105
2106 case kHostUSBDeviceState_DetachingFromVM:
2107 Assert(mPrevUniState == kHostUSBDeviceState_UsedByVM);
2108 NewPrevState = mPrevUniState;
2109 switch (aNewState)
2110 {
2111 /* Host/Failure. */
2112 case kHostUSBDeviceState_PhysDetached: //??
2113 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2114 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2115 break;
2116 case kHostUSBDeviceState_PhysDetachingFromVM:
2117 Assert(aNewPendingState == kHostUSBDeviceState_PhysDetached);
2118 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2119 break;
2120
2121 /* Success */
2122 case kHostUSBDeviceState_HeldByProxy:
2123 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2124 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2125 Assert(mPendingUniState == kHostUSBDeviceState_HeldByProxy);
2126 fFilters = true;
2127 break;
2128
2129 case kHostUSBDeviceState_ReleasingToHost:
2130 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2131 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2132 Assert(mPendingUniState == kHostUSBDeviceState_Unused);
2133 NewPrevState = kHostUSBDeviceState_HeldByProxy;
2134 break;
2135
2136 default:
2137 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
2138 stateName(aNewState, aNewPendingState, aNewSubState)), false);
2139 }
2140 break;
2141
2142 case kHostUSBDeviceState_PhysDetachingFromVM:
2143 Assert( mPrevUniState == kHostUSBDeviceState_DetachingFromVM
2144 || mPrevUniState == kHostUSBDeviceState_AttachingToVM
2145 || mPrevUniState == kHostUSBDeviceState_UsedByVM);
2146 NewPrevState = mPrevUniState; /* preserving it is more useful. */
2147 switch (aNewState)
2148 {
2149 case kHostUSBDeviceState_PhysDetached:
2150 Assert(aNewPendingState == kHostUSBDeviceState_Invalid);
2151 Assert(aNewSubState == kHostUSBDeviceSubState_Default);
2152 break;
2153 default:
2154 AssertLogRelMsgFailedReturn(("this=%p %s -X-> %s\n", this, getStateName(),
2155 stateName(aNewState, aNewPendingState, aNewSubState)), false);
2156 }
2157 break;
2158
2159 default:
2160 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2161 }
2162
2163 /*
2164 * Make the state change.
2165 */
2166 if (NewPrevState != mPrevUniState)
2167 LogFlowThisFunc(("%s -> %s (prev: %s -> %s) [%s]\n",
2168 getStateName(), stateName(aNewState, aNewPendingState, aNewSubState),
2169 stateName(mPrevUniState), stateName(NewPrevState), mName));
2170 else
2171 LogFlowThisFunc(("%s -> %s (prev: %s) [%s]\n",
2172 getStateName(), stateName(aNewState, aNewPendingState, aNewSubState), stateName(NewPrevState), mName));
2173 mPrevUniState = NewPrevState;
2174 mUniState = aNewState;
2175 mUniSubState = aNewSubState;
2176 mPendingUniState = aNewPendingState;
2177 mLastStateChangeTS = RTTimeNanoTS();
2178
2179 return fFilters;
2180}
2181
2182
2183/**
2184 * A convenience for entering a transitional state.
2185
2186 * @param aNewState The new state (transitional).
2187 * @param aFinalSubState The final state of the transition (non-transitional).
2188 * @param aNewSubState The new sub-state when applicable.
2189 *
2190 * @returns Always false because filters are never applied for the start of a transition.
2191 *
2192 * @note The caller must own the write lock for this object.
2193 */
2194bool HostUSBDevice::startTransition(HostUSBDeviceState aNewState, HostUSBDeviceState aFinalState,
2195 HostUSBDeviceSubState aNewSubState /*= kHostUSBDeviceSubState_Default*/)
2196{
2197 AssertReturn(isWriteLockOnCurrentThread(), false);
2198 /*
2199 * A quick prevalidation thing. Not really necessary since setState
2200 * verifies this too, but it's very easy here.
2201 */
2202 switch (mUniState)
2203 {
2204 case kHostUSBDeviceState_Unsupported:
2205 case kHostUSBDeviceState_UsedByHost:
2206 case kHostUSBDeviceState_Capturable:
2207 case kHostUSBDeviceState_Unused:
2208 case kHostUSBDeviceState_HeldByProxy:
2209 case kHostUSBDeviceState_UsedByVM:
2210 break;
2211
2212 case kHostUSBDeviceState_DetachingFromVM:
2213 case kHostUSBDeviceState_Capturing:
2214 case kHostUSBDeviceState_ReleasingToHost:
2215 case kHostUSBDeviceState_AttachingToVM:
2216 case kHostUSBDeviceState_PhysDetachingFromVM:
2217 AssertMsgFailedReturn(("this=%p %s is a transitional state.\n", this, getStateName()), false);
2218
2219 case kHostUSBDeviceState_PhysDetached:
2220 default:
2221 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2222 }
2223
2224 return setState(aNewState, aFinalState, aNewSubState);
2225}
2226
2227
2228/**
2229 * A convenience for advancing a transitional state forward.
2230 *
2231 * @param aSkipReAttach Fast forwards thru the re-attach substate if
2232 * applicable.
2233 *
2234 * @returns true if filters should be applied to the device, false if not.
2235 *
2236 * @note The caller must own the write lock for this object.
2237 */
2238bool HostUSBDevice::advanceTransition(bool aSkipReAttach /* = false */)
2239{
2240 AssertReturn(isWriteLockOnCurrentThread(), false);
2241 HostUSBDeviceState enmPending = mPendingUniState;
2242 HostUSBDeviceSubState enmSub = mUniSubState;
2243 HostUSBDeviceState enmState = mUniState;
2244 switch (enmState)
2245 {
2246 case kHostUSBDeviceState_Capturing:
2247 switch (enmSub)
2248 {
2249 case kHostUSBDeviceSubState_AwaitingDetach:
2250 enmSub = kHostUSBDeviceSubState_AwaitingReAttach;
2251 break;
2252 case kHostUSBDeviceSubState_AwaitingReAttach:
2253 enmSub = kHostUSBDeviceSubState_Default;
2254 /* fall thru */
2255 case kHostUSBDeviceSubState_Default:
2256 switch (enmPending)
2257 {
2258 case kHostUSBDeviceState_UsedByVM:
2259 enmState = kHostUSBDeviceState_AttachingToVM;
2260 break;
2261 case kHostUSBDeviceState_HeldByProxy:
2262 enmState = enmPending;
2263 enmPending = kHostUSBDeviceState_Invalid;
2264 break;
2265 default:
2266 AssertMsgFailedReturn(("this=%p invalid pending state %d: %s\n", this, enmPending, getStateName()), false);
2267 }
2268 break;
2269 default:
2270 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2271 }
2272 break;
2273
2274 case kHostUSBDeviceState_ReleasingToHost:
2275 switch (enmSub)
2276 {
2277 case kHostUSBDeviceSubState_AwaitingDetach:
2278 enmSub = kHostUSBDeviceSubState_AwaitingReAttach;
2279 break;
2280 case kHostUSBDeviceSubState_AwaitingReAttach:
2281 enmSub = kHostUSBDeviceSubState_Default;
2282 /* fall thru */
2283 case kHostUSBDeviceSubState_Default:
2284 switch (enmPending)
2285 {
2286 /* Use Unused here since it implies that filters has been applied
2287 and will make sure they aren't applied if the final state really
2288 is Capturable. */
2289 case kHostUSBDeviceState_Unused:
2290 enmState = enmPending;
2291 enmPending = kHostUSBDeviceState_Invalid;
2292 break;
2293 default:
2294 AssertMsgFailedReturn(("this=%p invalid pending state %d: %s\n", this, enmPending, getStateName()), false);
2295 }
2296 break;
2297 default:
2298 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2299 }
2300 break;
2301
2302 case kHostUSBDeviceState_AttachingToVM:
2303 switch (enmSub)
2304 {
2305 case kHostUSBDeviceSubState_AwaitingDetach:
2306 enmSub = kHostUSBDeviceSubState_AwaitingReAttach;
2307 break;
2308 case kHostUSBDeviceSubState_AwaitingReAttach:
2309 enmSub = kHostUSBDeviceSubState_Default;
2310 /* fall thru */
2311 case kHostUSBDeviceSubState_Default:
2312 switch (enmPending)
2313 {
2314 case kHostUSBDeviceState_UsedByVM:
2315 enmState = enmPending;
2316 enmPending = kHostUSBDeviceState_Invalid;
2317 break;
2318 default:
2319 AssertMsgFailedReturn(("this=%p invalid pending state %d: %s\n", this, enmPending, getStateName()), false);
2320 }
2321 break;
2322 default:
2323 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2324 }
2325 break;
2326
2327 case kHostUSBDeviceState_DetachingFromVM:
2328 switch (enmSub)
2329 {
2330 case kHostUSBDeviceSubState_AwaitingDetach:
2331 enmSub = kHostUSBDeviceSubState_AwaitingReAttach;
2332 break;
2333 case kHostUSBDeviceSubState_AwaitingReAttach:
2334 enmSub = kHostUSBDeviceSubState_Default;
2335 /* fall thru */
2336 case kHostUSBDeviceSubState_Default:
2337 switch (enmPending)
2338 {
2339 case kHostUSBDeviceState_HeldByProxy:
2340 enmState = enmPending;
2341 enmPending = kHostUSBDeviceState_Invalid;
2342 break;
2343 case kHostUSBDeviceState_Unused:
2344 enmState = kHostUSBDeviceState_ReleasingToHost;
2345 break;
2346 default:
2347 AssertMsgFailedReturn(("this=%p invalid pending state %d: %s\n", this, enmPending, getStateName()), false);
2348 }
2349 break;
2350 default:
2351 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2352 }
2353 break;
2354
2355 case kHostUSBDeviceState_PhysDetachingFromVM:
2356 switch (enmSub)
2357 {
2358 case kHostUSBDeviceSubState_Default:
2359 switch (enmPending)
2360 {
2361 case kHostUSBDeviceState_PhysDetached:
2362 enmState = enmPending;
2363 enmPending = kHostUSBDeviceState_Invalid;
2364 break;
2365 default:
2366 AssertMsgFailedReturn(("this=%p invalid pending state %d: %s\n", this, enmPending, getStateName()), false);
2367 }
2368 break;
2369 default:
2370 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2371 }
2372 break;
2373
2374 case kHostUSBDeviceState_Unsupported:
2375 case kHostUSBDeviceState_UsedByHost:
2376 case kHostUSBDeviceState_Capturable:
2377 case kHostUSBDeviceState_Unused:
2378 case kHostUSBDeviceState_HeldByProxy:
2379 case kHostUSBDeviceState_UsedByVM:
2380 AssertMsgFailedReturn(("this=%p %s is not transitional\n", this, getStateName()), false);
2381 case kHostUSBDeviceState_PhysDetached:
2382 default:
2383 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, enmState), false);
2384
2385 }
2386
2387 bool fRc = setState(enmState, enmPending, enmSub);
2388 if (aSkipReAttach && mUniSubState == kHostUSBDeviceSubState_AwaitingReAttach)
2389 fRc |= advanceTransition(false /* don't fast forward re-attach */);
2390 return fRc;
2391}
2392
2393/**
2394 * A convenience for failing a transitional state.
2395 *
2396 * @return true if filters should be applied to the device, false if not.
2397 *
2398 * @note The caller must own the write lock for this object.
2399 */
2400bool HostUSBDevice::failTransition()
2401{
2402 AssertReturn(isWriteLockOnCurrentThread(), false);
2403 HostUSBDeviceSubState enmSub = mUniSubState;
2404 HostUSBDeviceState enmState = mUniState;
2405 switch (enmState)
2406 {
2407 /*
2408 * There are just two cases, either we got back to the
2409 * previous state (assumes Capture+Attach-To-VM updates it)
2410 * or we assume the device has been unplugged (physically).
2411 */
2412 case kHostUSBDeviceState_DetachingFromVM:
2413 case kHostUSBDeviceState_Capturing:
2414 case kHostUSBDeviceState_ReleasingToHost:
2415 case kHostUSBDeviceState_AttachingToVM:
2416 switch (enmSub)
2417 {
2418 case kHostUSBDeviceSubState_AwaitingDetach:
2419 enmSub = kHostUSBDeviceSubState_Default;
2420 /* fall thru */
2421 case kHostUSBDeviceSubState_Default:
2422 enmState = mPrevUniState;
2423 break;
2424 case kHostUSBDeviceSubState_AwaitingReAttach:
2425 enmSub = kHostUSBDeviceSubState_Default;
2426 enmState = kHostUSBDeviceState_PhysDetached;
2427 break;
2428 default:
2429 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2430 }
2431 break;
2432
2433 case kHostUSBDeviceState_PhysDetachingFromVM:
2434 AssertMsgFailedReturn(("this=%p %s shall not fail\n", this, getStateName()), false);
2435
2436 case kHostUSBDeviceState_Unsupported:
2437 case kHostUSBDeviceState_UsedByHost:
2438 case kHostUSBDeviceState_Capturable:
2439 case kHostUSBDeviceState_Unused:
2440 case kHostUSBDeviceState_HeldByProxy:
2441 case kHostUSBDeviceState_UsedByVM:
2442 AssertMsgFailedReturn(("this=%p %s is not transitional\n", this, getStateName()), false);
2443 case kHostUSBDeviceState_PhysDetached:
2444 default:
2445 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), false);
2446
2447 }
2448
2449 return setState(enmState, kHostUSBDeviceState_Invalid, enmSub);
2450}
2451
2452
2453/**
2454 * Determines the canonical state of the device.
2455 *
2456 * @returns canonical state.
2457 *
2458 * @note The caller must own the read (or write) lock for this object.
2459 */
2460USBDeviceState_T HostUSBDevice::canonicalState() const
2461{
2462 switch (mUniState)
2463 {
2464 /*
2465 * Straight forward.
2466 */
2467 case kHostUSBDeviceState_Unsupported:
2468 return USBDeviceState_NotSupported;
2469
2470 case kHostUSBDeviceState_UsedByHost:
2471 return USBDeviceState_Unavailable;
2472
2473 case kHostUSBDeviceState_Capturable:
2474 return USBDeviceState_Busy;
2475
2476 case kHostUSBDeviceState_Unused:
2477 return USBDeviceState_Available;
2478
2479 case kHostUSBDeviceState_HeldByProxy:
2480 return USBDeviceState_Held;
2481
2482 case kHostUSBDeviceState_UsedByVM:
2483 return USBDeviceState_Captured;
2484
2485 /*
2486 * Pretend we've reached the final state.
2487 */
2488 case kHostUSBDeviceState_Capturing:
2489 Assert( mPendingUniState == kHostUSBDeviceState_UsedByVM
2490 || mPendingUniState == kHostUSBDeviceState_HeldByProxy);
2491 return mPendingUniState == kHostUSBDeviceState_UsedByVM
2492 ? (USBDeviceState_T)USBDeviceState_Captured
2493 : (USBDeviceState_T)USBDeviceState_Held;
2494 /* The cast ^^^^ is because xidl is using different enums for
2495 each of the values. *Very* nice idea... :-) */
2496
2497 case kHostUSBDeviceState_AttachingToVM:
2498 return USBDeviceState_Captured;
2499
2500 /*
2501 * Return the previous state.
2502 */
2503 case kHostUSBDeviceState_ReleasingToHost:
2504 Assert( mPrevUniState == kHostUSBDeviceState_UsedByVM
2505 || mPrevUniState == kHostUSBDeviceState_HeldByProxy);
2506 return mPrevUniState == kHostUSBDeviceState_UsedByVM
2507 ? (USBDeviceState_T)USBDeviceState_Captured
2508 : (USBDeviceState_T)USBDeviceState_Held;
2509 /* The cast ^^^^ is because xidl is using different enums for
2510 each of the values. *Very* nice idea... :-) */
2511
2512 case kHostUSBDeviceState_DetachingFromVM:
2513 return USBDeviceState_Captured;
2514 case kHostUSBDeviceState_PhysDetachingFromVM:
2515 return USBDeviceState_Captured;
2516
2517 case kHostUSBDeviceState_PhysDetached:
2518 default:
2519 AssertReleaseMsgFailedReturn(("this=%p mUniState=%d\n", this, mUniState), USBDeviceState_NotSupported);
2520 }
2521 /* won't ever get here. */
2522}
2523/* 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