VirtualBox

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

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

Main/Frontends: Next step to support asynchronous USB device flow.

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