VirtualBox

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

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

Main: Fixed: HostUSBDevice takes ownership of the new USBDEVICE structure to have the address field properly updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 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.virtualbox.org. 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. Note
247 * that pDevices will be always adopted by the HostUSBDevice
248 * object (whose updateState() method must be always called by
249 * updateDeviceState()) and therefore there is no reason to
250 * free pDevices here.
251 */
252 PUSBDEVICE pNext = pDevices->pNext; /* treated as singly linked */
253 if (updateDeviceState (DevPtr, pDevices))
254 {
255 Log (("USBProxyService::processChanges: state change %p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"} state=%d%s\n",
256 (HostUSBDevice *)DevPtr, pDevices->idVendor, pDevices->idProduct, pDevices->pszProduct, pDevices->pszManufacturer, DevPtr->state(), DevPtr->isStatePending() ? " (pending async op)" : ""));
257 mHost->onUSBDeviceStateChanged (DevPtr);
258 }
259 It++;
260 pDevices = pNext;
261 }
262 else
263 {
264 if (iDiff > 0)
265 {
266 /*
267 * Head of pDevices was attached.
268 */
269 PUSBDEVICE pNew = pDevices;
270 pDevices = pDevices->pNext;
271 pNew->pPrev = pNew->pNext = NULL;
272
273 ComObjPtr <HostUSBDevice> NewObj;
274 NewObj.createObject();
275 NewObj->init (pNew, this);
276 Log (("USBProxyService::processChanges: attached %p/%p:{.idVendor=%#06x, .idProduct=%#06x, .pszProduct=\"%s\", .pszManufacturer=\"%s\"}\n",
277 (HostUSBDevice *)NewObj, pNew, pNew->idVendor, pNew->idProduct, pNew->pszProduct, pNew->pszManufacturer));
278
279 /* not really necessary to lock here, but make Assert
280 * checks happy */
281 AutoLock newDevLock (NewObj);
282
283 mDevices.insert (It, NewObj);
284 mHost->onUSBDeviceAttached (NewObj);
285 }
286 else
287 {
288 /*
289 * DevPtr was detached, unless there is a pending async request.
290 */
291 /** @todo add a timeout here. */
292 if (!DevPtr->isStatePending())
293 {
294 It = mDevices.erase (It);
295 mHost->onUSBDeviceDetached (DevPtr);
296 Log (("USBProxyService::processChanges: detached %p\n",
297 (HostUSBDevice *)DevPtr)); /** @todo add details .*/
298
299 /* from now on, the object is no more valid,
300 * uninitialize to avoid abuse */
301 devCaller.release();
302 DevPtr->uninit();
303 }
304 else
305 {
306 /* a state change (re-cycle) request is pending, go
307 * to the next device */
308 It++;
309 Log (("USBProxyService::processChanges: detached but pending %d %p\n",
310 DevPtr->pendingState(), (HostUSBDevice *)DevPtr));
311 }
312 }
313 }
314 } /* while */
315 }
316 else
317 {
318 /* we need to lock the host object for writing because
319 * a) the subsequent code may call Host methods that require a write
320 * lock
321 * b) we will lock HostUSBDevice objects below and want to make sure
322 * the lock order is always the same (Host, HostUSBDevice, as
323 * expected by Host) to avoid cross-deadlocks */
324
325 AutoLock hostLock (mHost);
326
327 /* All devices were detached */
328 HostUSBDeviceList::iterator It = this->mDevices.begin();
329 while (It != mDevices.end())
330 {
331 ComObjPtr <HostUSBDevice> DevPtr = *It;
332
333 /* assert that the object is still alive (we still reference it in
334 * the collection and we're the only one who calls uninit() on it */
335 HostUSBDevice::AutoCaller devCaller (DevPtr);
336 AssertComRC (devCaller.rc());
337
338 AutoLock devLock (DevPtr);
339
340 /*
341 * DevPtr was detached.
342 */
343 It = mDevices.erase (It);
344 mHost->onUSBDeviceDetached (DevPtr);
345 Log (("USBProxyService::processChanges: detached %p\n",
346 (HostUSBDevice *)DevPtr)); /** @todo add details .*/
347
348 /* from now on, the object is no more valid,
349 * uninitialize to avoid abuse */
350 devCaller.release();
351 DevPtr->uninit();
352 }
353 }
354
355 LogFlowThisFunc (("returns void\n"));
356}
357
358
359/*static*/ DECLCALLBACK (int) USBProxyService::serviceThread (RTTHREAD Thread, void *pvUser)
360{
361 USBProxyService *pThis = (USBProxyService *)pvUser;
362 LogFlowFunc (("pThis=%p\n", pThis));
363 pThis->serviceThreadInit();
364
365 /*
366 * Processing loop.
367 */
368 for (;;)
369 {
370 pThis->wait (RT_INDEFINITE_WAIT);
371 if (pThis->mTerminate)
372 break;
373 pThis->processChanges();
374 }
375
376 pThis->serviceThreadTerm();
377 LogFlowFunc (("returns VINF_SUCCESS\n"));
378 return VINF_SUCCESS;
379}
380
381
382/*static*/ void USBProxyService::freeDevice (PUSBDEVICE pDevice)
383{
384 PUSBCONFIG pCfg = pDevice->paConfigurations;
385 unsigned cCfgs = pDevice->bNumConfigurations;
386 while (cCfgs-- > 0)
387 {
388 PUSBINTERFACE pIf = pCfg->paInterfaces;
389 unsigned cIfs = pCfg->bNumInterfaces;
390 while (cIfs-- > 0)
391 {
392 RTMemFree (pIf->paEndpoints);
393 pIf->paEndpoints = NULL;
394 RTStrFree ((char *)pIf->pszDriver);
395 pIf->pszDriver = NULL;
396 RTStrFree ((char *)pIf->pszInterface);
397 pIf->pszInterface = NULL;
398 /* next */
399 pIf++;
400 }
401 RTMemFree (pCfg->paInterfaces);
402 pCfg->paInterfaces = NULL;
403 RTStrFree ((char *)pCfg->pszConfiguration);
404 pCfg->pszConfiguration = NULL;
405
406 /* next */
407 pCfg++;
408 }
409 RTMemFree (pDevice->paConfigurations);
410 pDevice->paConfigurations = NULL;
411
412 RTStrFree ((char *)pDevice->pszManufacturer);
413 pDevice->pszManufacturer = NULL;
414 RTStrFree ((char *)pDevice->pszProduct);
415 pDevice->pszProduct = NULL;
416 RTStrFree ((char *)pDevice->pszSerialNumber);
417 pDevice->pszSerialNumber = NULL;
418
419 RTStrFree ((char *)pDevice->pszAddress);
420 pDevice->pszAddress = NULL;
421
422 RTMemFree (pDevice);
423
424}
425
426
427/* static */ uint64_t USBProxyService::calcSerialHash (const char *aSerial)
428{
429 if (!aSerial)
430 aSerial = "";
431
432 register const uint8_t *pu8 = (const uint8_t *)aSerial;
433 register uint64_t u64 = 14695981039346656037ULL;
434 for (;;)
435 {
436 register uint8_t u8 = *pu8;
437 if (!u8)
438 break;
439 u64 = (u64 * 1099511628211ULL) ^ u8;
440 pu8++;
441 }
442
443 return u64;
444}
445
446
447bool USBProxyService::updateDeviceStateFake (HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice)
448{
449 AssertReturn (aDevice, false);
450 AssertReturn (aDevice->isLockedOnCurrentThread(), false);
451
452 if (aDevice->isStatePending())
453 {
454 switch (aDevice->pendingState())
455 {
456 /* @todo USBDEVICESTATE_USED_BY_GUEST seems not to be used anywhere in the proxy code; it's
457 * quite logical because the proxy doesn't know anything about guest VMs. We use HELD_BY_PROXY
458 * instead -- it is sufficient and is what Main expects. */
459 case USBDeviceState_USBDeviceCaptured: aUSBDevice->enmState = USBDEVICESTATE_HELD_BY_PROXY; break;
460 case USBDeviceState_USBDeviceHeld: aUSBDevice->enmState = USBDEVICESTATE_HELD_BY_PROXY; break;
461 case USBDeviceState_USBDeviceAvailable: aUSBDevice->enmState = USBDEVICESTATE_UNUSED; break;
462 case USBDeviceState_USBDeviceUnavailable: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_HOST; break;
463 case USBDeviceState_USBDeviceBusy: aUSBDevice->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; break;
464 default:
465 AssertMsgFailed(("%d\n", aDevice->pendingState()));
466 break;
467 }
468 }
469
470 return USBProxyService::updateDeviceState (aDevice, aUSBDevice);
471}
472
473
474
475/* Stubs which the host specific classes overrides: */
476
477
478int USBProxyService::wait (unsigned aMillies)
479{
480 return RTThreadSleep (250);
481}
482
483
484int USBProxyService::interruptWait (void)
485{
486 return VERR_NOT_IMPLEMENTED;
487}
488
489
490PUSBDEVICE USBProxyService::getDevices (void)
491{
492 return NULL;
493}
494
495
496void USBProxyService::serviceThreadInit (void)
497{
498}
499
500
501void USBProxyService::serviceThreadTerm (void)
502{
503}
504
505
506/**
507 * The default implementation returns non-NULL to emulate successful insertions
508 * for those subclasses that don't reimplement this method.
509 */
510void *USBProxyService::insertFilter (IUSBDeviceFilter * /* aFilter */)
511{
512 // return non-NULL to prevent failed assertions in Main
513 return (void *) 1;
514}
515
516
517void USBProxyService::removeFilter (void * /* aID */)
518{
519}
520
521
522int USBProxyService::captureDevice (HostUSBDevice *pDevice)
523{
524 return VERR_NOT_IMPLEMENTED;
525}
526
527
528int USBProxyService::holdDevice (HostUSBDevice *pDevice)
529{
530 return VERR_NOT_IMPLEMENTED;
531}
532
533
534int USBProxyService::releaseDevice (HostUSBDevice *pDevice)
535{
536 return VERR_NOT_IMPLEMENTED;
537}
538
539
540int USBProxyService::resetDevice (HostUSBDevice *pDevice)
541{
542 return VERR_NOT_IMPLEMENTED;
543}
544
545
546bool USBProxyService::updateDeviceState (HostUSBDevice *pDevice, PUSBDEVICE pUSBDevice)
547{
548 AssertReturn (pDevice, false);
549 AssertReturn (pDevice->isLockedOnCurrentThread(), false);
550
551 return pDevice->updateState (pUSBDevice);
552}
553
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