VirtualBox

source: vbox/trunk/src/VBox/Main/USBProxyService.cpp@ 3034

Last change on this file since 3034 was 3034, checked in by vboxsync, 18 years ago

Main: Fixed async USB (added proper detection of device re-cycle on Win32; changed manual release sequence so that a detach is made in VMM first, the USB proxy is notified afterwards on success; some other small fixes).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.6 KB
Line 
1/** @file
2 * VirtualBox USB Proxy Service (base) class.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.215389.xyz. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21#include "USBProxyService.h"
22#include "Logging.h"
23
24#include <VBox/err.h>
25#include <iprt/asm.h>
26#include <iprt/semaphore.h>
27
28
29
30/** @todo add the required locking. */
31
32/**
33 * Initialize data members.
34 */
35USBProxyService::USBProxyService (Host *aHost)
36 : mHost (aHost), mThread (NIL_RTTHREAD), mTerminate (false), mDevices (), mLastError (VINF_SUCCESS)
37{
38 LogFlowThisFunc (("aHost=%p\n", aHost));
39}
40
41
42/**
43 * Empty destructor.
44 */
45USBProxyService::~USBProxyService()
46{
47 LogFlowThisFunc (("\n"));
48 Assert (mThread == NIL_RTTHREAD);
49 mDevices.clear();
50 mTerminate = true;
51 mHost = NULL;
52}
53
54
55bool USBProxyService::isActive (void)
56{
57 return mThread != NIL_RTTHREAD;
58}
59
60
61int USBProxyService::getLastError (void)
62{
63 return mLastError;
64}
65
66
67int USBProxyService::start (void)
68{
69 int rc = VINF_SUCCESS;
70 if (mThread == NIL_RTTHREAD)
71 {
72 /*
73 * Force update before starting the poller thread.
74 */
75 wait (0);
76 processChanges ();
77
78 /*
79 * Create the poller thread which will look for changes.
80 */
81 mTerminate = false;
82 rc = RTThreadCreate (&mThread, USBProxyService::serviceThread, this,
83 0, RTTHREADTYPE_INFREQUENT_POLLER, RTTHREADFLAGS_WAITABLE, "USBPROXY");
84 AssertRC (rc);
85 if (VBOX_SUCCESS (rc))
86 LogFlowThisFunc (("started mThread=%RTthrd\n", mThread));
87 else
88 {
89 mThread = NIL_RTTHREAD;
90 mLastError = rc;
91 }
92 }
93 else
94 LogFlowThisFunc (("already running, mThread=%RTthrd\n", mThread));
95 return rc;
96}
97
98
99int USBProxyService::stop (void)
100{
101 int rc = VINF_SUCCESS;
102 if (mThread != NIL_RTTHREAD)
103 {
104 /*
105 * Mark the thread for termination and kick it.
106 */
107 ASMAtomicXchgSize (&mTerminate, true);
108 rc = interruptWait();
109 AssertRC (rc);
110
111 /*
112 * Wait for the thread to finish and then update the state.
113 */
114 rc = RTThreadWait (mThread, 60000, NULL);
115 if (rc == VERR_INVALID_HANDLE)
116 rc = VINF_SUCCESS;
117 if (VBOX_SUCCESS (rc))
118 {
119 LogFlowThisFunc (("stopped mThread=%RTthrd\n", mThread));
120 mThread = NIL_RTTHREAD;
121 mTerminate = false;
122 }
123 else
124 {
125 AssertRC (rc);
126 mLastError = rc;
127 }
128 }
129 else
130 LogFlowThisFunc (("not active\n"));
131
132 return rc;
133}
134
135
136/**
137 * Sort a list of USB devices.
138 *
139 * @returns Pointer to the head of the sorted doubly linked list.
140 * @param aDevices Head pointer (can be both singly and doubly linked list).
141 */
142static PUSBDEVICE sortDevices (PUSBDEVICE pDevices)
143{
144 PUSBDEVICE pHead = NULL;
145 PUSBDEVICE pTail = NULL;
146 while (pDevices)
147 {
148 /* unlink head */
149 PUSBDEVICE pDev = pDevices;
150 pDevices = pDev->pNext;
151 if (pDevices)
152 pDevices->pPrev = NULL;
153
154 /* find location. */
155 PUSBDEVICE pCur = pTail;
156 while ( pCur
157 && HostUSBDevice::compare (pCur, pDev) > 0)
158 pCur = pCur->pPrev;
159
160 /* insert (after pCur) */
161 pDev->pPrev = pCur;
162 if (pCur)
163 {
164 pDev->pNext = pCur->pNext;
165 pCur->pNext = pDev;
166 if (pDev->pNext)
167 pDev->pNext->pPrev = pDev;
168 else
169 pTail = pDev;
170 }
171 else
172 {
173 pDev->pNext = pHead;
174 if (pHead)
175 pHead->pPrev = pDev;
176 else
177 pTail = pDev;
178 pHead = pDev;
179 }
180 }
181
182 return pHead;
183}
184
185
186void USBProxyService::processChanges (void)
187{
188 LogFlowThisFunc (("\n"));
189
190 /*
191 * Get the sorted list of USB devices.
192 */
193 PUSBDEVICE pDevices = getDevices();
194 if (pDevices)
195 {
196 pDevices = sortDevices (pDevices);
197
198 /* we need to lock the host object for writing because
199 * a) the subsequent code may call Host methods that require a write
200 * lock
201 * b) we will lock HostUSBDevice objects below and want to make sure
202 * the lock order is always the same (Host, HostUSBDevice, as
203 * expected by Host) to avoid cross-deadlocks */
204
205 AutoLock hostLock (mHost);
206
207 /*
208 * Compare previous list with the previous list of devices
209 * and merge in any changes while notifying Host.
210 */
211 HostUSBDeviceList::iterator It = this->mDevices.begin();
212 while ( It != mDevices.end()
213 || pDevices)
214 {
215 ComObjPtr <HostUSBDevice> DevPtr;
216
217 if (It != mDevices.end())
218 DevPtr = *It;
219
220 /* assert that the object is still alive (we still reference it in
221 * the collection and we're the only one who calls uninit() on it */
222 HostUSBDevice::AutoCaller devCaller (DevPtr.isNull() ? NULL : DevPtr);
223 AssertComRC (devCaller.rc());
224
225 /* Lock the device object since we will read/write it's
226 * properties. All Host callbacks also imply the object is
227 * locked. */
228 AutoLock devLock (DevPtr.isNull() ? NULL : DevPtr);
229
230 /*
231 * Compare.
232 */
233 int iDiff;
234 if (DevPtr.isNull())
235 iDiff = 1;
236 else
237 {
238 if (!pDevices)
239 iDiff = -1;
240 else
241 iDiff = DevPtr->compare (pDevices);
242 }
243 if (!iDiff)
244 {
245 /*
246 * Device still there, update the state and move on.
247 */
248 if (updateDeviceState (DevPtr, pDevices))
249 {
250 Log (("USBProxyService::processChanges: state change %p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"} state=%d%s\n",
251 (HostUSBDevice *)DevPtr, pDevices->idVendor, pDevices->idProduct, pDevices->pszProduct, pDevices->pszManufacturer, DevPtr->state(), DevPtr->isStatePending() ? " (pending async op)" : ""));
252 mHost->onUSBDeviceStateChanged (DevPtr);
253 }
254 It++;
255 PUSBDEVICE pFree = pDevices;
256 pDevices = pDevices->pNext; /* treated as singly linked */
257 freeDevice (pFree);
258 }
259 else
260 {
261 if (iDiff > 0)
262 {
263 /*
264 * Head of pDevices was attached.
265 */
266 PUSBDEVICE pNew = pDevices;
267 pDevices = pDevices->pNext;
268 pNew->pPrev = pNew->pNext = NULL;
269
270 ComObjPtr <HostUSBDevice> NewObj;
271 NewObj.createObject();
272 NewObj->init (pNew, this);
273 Log (("USBProxyService::processChanges: attached %p/%p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"}\n",
274 (HostUSBDevice *)NewObj, pNew, pNew->idVendor, pNew->idProduct, pNew->pszProduct, pNew->pszManufacturer));
275
276 /* not really necessary to lock here, but make Assert
277 * checks happy */
278 AutoLock newDevLock (NewObj);
279
280 mDevices.insert (It, NewObj);
281 mHost->onUSBDeviceAttached (NewObj);
282 }
283 else
284 {
285 /*
286 * DevPtr was detached, unless there is a pending async request.
287 */
288 /** @todo add a timeout here. */
289 if (!DevPtr->isStatePending())
290 {
291 It = mDevices.erase (It);
292 mHost->onUSBDeviceDetached (DevPtr);
293 Log (("USBProxyService::processChanges: detached %p\n",
294 (HostUSBDevice *)DevPtr)); /** @todo add details .*/
295
296 /* from now on, the object is no more valid,
297 * uninitialize to avoid abuse */
298 devCaller.release();
299 DevPtr->uninit();
300 }
301 else
302 {
303 /* a state change (re-cycle) request is pending, go
304 * to the next device */
305 It++;
306 Log (("USBProxyService::processChanges: detached but pending %d %p\n",
307 DevPtr->pendingState(), (HostUSBDevice *)DevPtr));
308 }
309 }
310 }
311 } /* while */
312 }
313 else
314 {
315 /* we need to lock the host object for writing because
316 * a) the subsequent code may call Host methods that require a write
317 * lock
318 * b) we will lock HostUSBDevice objects below and want to make sure
319 * the lock order is always the same (Host, HostUSBDevice, as
320 * expected by Host) to avoid cross-deadlocks */
321
322 AutoLock hostLock (mHost);
323
324 /* All devices were detached */
325 HostUSBDeviceList::iterator It = this->mDevices.begin();
326 while (It != mDevices.end())
327 {
328 ComObjPtr <HostUSBDevice> DevPtr = *It;
329
330 /* assert that the object is still alive (we still reference it in
331 * the collection and we're the only one who calls uninit() on it */
332 HostUSBDevice::AutoCaller devCaller (DevPtr);
333 AssertComRC (devCaller.rc());
334
335 AutoLock devLock (DevPtr);
336
337 /*
338 * DevPtr was detached.
339 */
340 It = mDevices.erase (It);
341 mHost->onUSBDeviceDetached (DevPtr);
342 Log (("USBProxyService::processChanges: detached %p\n",
343 (HostUSBDevice *)DevPtr)); /** @todo add details .*/
344
345 /* from now on, the object is no more valid,
346 * uninitialize to avoid abuse */
347 devCaller.release();
348 DevPtr->uninit();
349 }
350 }
351
352 LogFlowThisFunc (("returns void\n"));
353}
354
355
356/*static*/ DECLCALLBACK (int) USBProxyService::serviceThread (RTTHREAD Thread, void *pvUser)
357{
358 USBProxyService *pThis = (USBProxyService *)pvUser;
359 LogFlowFunc (("pThis=%p\n", pThis));
360 pThis->serviceThreadInit();
361
362 /*
363 * Processing loop.
364 */
365 for (;;)
366 {
367 pThis->wait (RT_INDEFINITE_WAIT);
368 if (pThis->mTerminate)
369 break;
370 pThis->processChanges();
371 }
372
373 pThis->serviceThreadTerm();
374 LogFlowFunc (("returns VINF_SUCCESS\n"));
375 return VINF_SUCCESS;
376}
377
378
379/*static*/ void USBProxyService::freeDevice (PUSBDEVICE pDevice)
380{
381 PUSBCONFIG pCfg = pDevice->paConfigurations;
382 unsigned cCfgs = pDevice->bNumConfigurations;
383 while (cCfgs-- > 0)
384 {
385 PUSBINTERFACE pIf = pCfg->paInterfaces;
386 unsigned cIfs = pCfg->bNumInterfaces;
387 while (cIfs-- > 0)
388 {
389 RTMemFree (pIf->paEndpoints);
390 pIf->paEndpoints = NULL;
391 RTStrFree ((char *)pIf->pszDriver);
392 pIf->pszDriver = NULL;
393 RTStrFree ((char *)pIf->pszInterface);
394 pIf->pszInterface = NULL;
395 /* next */
396 pIf++;
397 }
398 RTMemFree (pCfg->paInterfaces);
399 pCfg->paInterfaces = NULL;
400 RTStrFree ((char *)pCfg->pszConfiguration);
401 pCfg->pszConfiguration = NULL;
402
403 /* next */
404 pCfg++;
405 }
406 RTMemFree (pDevice->paConfigurations);
407 pDevice->paConfigurations = NULL;
408
409 RTStrFree ((char *)pDevice->pszManufacturer);
410 pDevice->pszManufacturer = NULL;
411 RTStrFree ((char *)pDevice->pszProduct);
412 pDevice->pszProduct = NULL;
413 RTStrFree ((char *)pDevice->pszSerialNumber);
414 pDevice->pszSerialNumber = NULL;
415
416 RTStrFree ((char *)pDevice->pszAddress);
417 pDevice->pszAddress = NULL;
418
419 RTMemFree (pDevice);
420
421}
422
423
424/* static */ uint64_t USBProxyService::calcSerialHash (const char *aSerial)
425{
426 if (!aSerial)
427 aSerial = "";
428
429 register const uint8_t *pu8 = (const uint8_t *)aSerial;
430 register uint64_t u64 = 14695981039346656037ULL;
431 for (;;)
432 {
433 register uint8_t u8 = *pu8;
434 if (!u8)
435 break;
436 u64 = (u64 * 1099511628211ULL) ^ u8;
437 pu8++;
438 }
439
440 return u64;
441}
442
443
444bool USBProxyService::updateDeviceStateFake (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice)
445{
446 AssertReturn (aDevice, false);
447 AssertReturn (aDevice->isLockedOnCurrentThread(), false);
448
449 if (aDevice->isStatePending())
450 {
451 switch (aDevice->pendingState())
452 {
453 /* @todo USBDEVICESTATE_USED_BY_GUEST seems not to be used anywhere in the proxy code; it's
454 * quite logical because the proxy doesn't know anything about guest VMs. We use HELD_BY_PROXY
455 * instead -- it is sufficient and is what Main expects. */
456 case USBDeviceState_USBDeviceCaptured: aUSBDevice->enmState = USBDEVICESTATE_HELD_BY_PROXY; break;
457 case USBDeviceState_USBDeviceHeld: aUSBDevice->enmState = USBDEVICESTATE_HELD_BY_PROXY; break;
458 case USBDeviceState_USBDeviceAvailable: aUSBDevice->enmState = USBDEVICESTATE_UNUSED; break;
459 case USBDeviceState_USBDeviceUnavailable: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_HOST; break;
460 case USBDeviceState_USBDeviceBusy: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; break;
461 default:
462 AssertMsgFailed(("%d\n", aDevice->pendingState()));
463 break;
464 }
465 }
466
467 return USBProxyService::updateDeviceState (aDevice, aUSBDevice);
468}
469
470
471
472/* Stubs which the host specific classes overrides: */
473
474
475int USBProxyService::wait (unsigned aMillies)
476{
477 return RTThreadSleep (250);
478}
479
480
481int USBProxyService::interruptWait (void)
482{
483 return VERR_NOT_IMPLEMENTED;
484}
485
486
487PUSBDEVICE USBProxyService::getDevices (void)
488{
489 return NULL;
490}
491
492
493void USBProxyService::serviceThreadInit (void)
494{
495}
496
497
498void USBProxyService::serviceThreadTerm (void)
499{
500}
501
502
503/**
504 * The default implementation returns non-NULL to emulate successful insertions
505 * for those subclasses that don't reimplement this method.
506 */
507void *USBProxyService::insertFilter (IUSBDeviceFilter * /* aFilter */)
508{
509 // return non-NULL to prevent failed assertions in Main
510 return (void *) 1;
511}
512
513
514void USBProxyService::removeFilter (void * /* aID */)
515{
516}
517
518
519int USBProxyService::captureDevice (HostUSBDevice *pDevice)
520{
521 return VERR_NOT_IMPLEMENTED;
522}
523
524
525int USBProxyService::holdDevice (HostUSBDevice *pDevice)
526{
527 return VERR_NOT_IMPLEMENTED;
528}
529
530
531int USBProxyService::releaseDevice (HostUSBDevice *pDevice)
532{
533 return VERR_NOT_IMPLEMENTED;
534}
535
536
537int USBProxyService::resetDevice (HostUSBDevice *pDevice)
538{
539 return VERR_NOT_IMPLEMENTED;
540}
541
542
543bool USBProxyService::updateDeviceState (HostUSBDevice *pDevice, PUSBDEVICE pUSBDevice)
544{
545 AssertReturn (pDevice, false);
546 AssertReturn (pDevice->isLockedOnCurrentThread(), false);
547
548 return pDevice->updateState (pUSBDevice);
549}
550
Note: See TracBrowser for help on using the repository browser.

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