VirtualBox

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

Last change on this file since 3411 was 3392, checked in by vboxsync, 17 years ago

First part of fix for udevd::chmod vs. VBoxSVC race on attach.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use