VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/solaris/USBProxyBackendSolaris.cpp@ 62485

Last change on this file since 62485 was 62485, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/* $Id: USBProxyBackendSolaris.cpp 62485 2016-07-22 18:36:43Z vboxsync $ */
2/** @file
3 * VirtualBox USB Proxy Service, Solaris Specialization.
4 */
5
6/*
7 * Copyright (C) 2005-2016 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "USBProxyBackend.h"
23#include "Logging.h"
24
25#include <VBox/usb.h>
26#include <VBox/usblib.h>
27#include <VBox/err.h>
28#include <iprt/semaphore.h>
29#include <iprt/path.h>
30
31#include <sys/usb/usba.h>
32#include <syslog.h>
33
34
35/*********************************************************************************************************************************
36* Internal Functions *
37*********************************************************************************************************************************/
38static int solarisWalkDeviceNode(di_node_t Node, void *pvArg);
39static void solarisFreeUSBDevice(PUSBDEVICE pDevice);
40static USBDEVICESTATE solarisDetermineUSBDeviceState(PUSBDEVICE pDevice, di_node_t Node);
41
42
43/*********************************************************************************************************************************
44* Structures and Typedefs *
45*********************************************************************************************************************************/
46typedef struct USBDEVICELIST
47{
48 PUSBDEVICE pHead;
49 PUSBDEVICE pTail;
50} USBDEVICELIST;
51typedef USBDEVICELIST *PUSBDEVICELIST;
52
53
54/**
55 * Initialize data members.
56 */
57USBProxyBackendSolaris::USBProxyBackendSolaris()
58 : USBProxyBackend(), mNotifyEventSem(NIL_RTSEMEVENT), mUSBLibInitialized(false)
59{
60 LogFlowThisFunc(("\n"));
61}
62
63USBProxyBackendSolaris::~USBProxyBackendSolaris()
64{
65}
66
67/**
68 * Initializes the object (called right after construction).
69 *
70 * @returns VBox status code.
71 */
72int USBProxyBackendSolaris::init(USBProxyService *aUsbProxyService, const com::Utf8Str &strId, const com::Utf8Str &strAddress)
73{
74 USBProxyBackend::init(aUsbProxyService, strId, strAddress);
75
76 unconst(m_strBackend) = Utf8Str("host");
77
78 /*
79 * Create semaphore.
80 */
81 int rc = RTSemEventCreate(&mNotifyEventSem);
82 if (RT_FAILURE(rc))
83 return rc;
84
85 /*
86 * Initialize the USB library.
87 */
88 rc = USBLibInit();
89 if (RT_FAILURE(rc))
90 {
91 /* mNotifyEventSem will be destroyed in uninit */
92 return rc;
93 }
94
95 mUSBLibInitialized = true;
96
97 /*
98 * Start the poller thread.
99 */
100 start();
101 return VINF_SUCCESS;
102}
103
104
105/**
106 * Stop all service threads and free the device chain.
107 */
108void USBProxyBackendSolaris::uninit()
109{
110 LogFlowThisFunc(("destruct\n"));
111
112 /*
113 * Stop the service.
114 */
115 if (isActive())
116 stop();
117
118 /*
119 * Terminate the USB library
120 */
121 if (mUSBLibInitialized)
122 {
123 USBLibTerm();
124 mUSBLibInitialized = false;
125 }
126
127 if (mNotifyEventSem != NIL_RTSEMEVENT)
128 {
129 RTSemEventDestroy(mNotifyEventSem);
130 mNotifyEventSem = NIL_RTSEMEVENT;
131 }
132}
133
134
135void *USBProxyBackendSolaris::insertFilter(PCUSBFILTER aFilter)
136{
137 return USBLibAddFilter(aFilter);
138}
139
140
141void USBProxyBackendSolaris::removeFilter(void *pvID)
142{
143 USBLibRemoveFilter(pvID);
144}
145
146
147int USBProxyBackendSolaris::wait(RTMSINTERVAL aMillies)
148{
149 return RTSemEventWait(mNotifyEventSem, aMillies < 1000 ? 1000 : RT_MIN(aMillies, 5000));
150}
151
152
153int USBProxyBackendSolaris::interruptWait(void)
154{
155 return RTSemEventSignal(mNotifyEventSem);
156}
157
158
159PUSBDEVICE USBProxyBackendSolaris::getDevices(void)
160{
161 USBDEVICELIST DevList;
162 DevList.pHead = NULL;
163 DevList.pTail = NULL;
164 di_node_t RootNode = di_init("/", DINFOCPYALL);
165 if (RootNode != DI_NODE_NIL)
166 di_walk_node(RootNode, DI_WALK_CLDFIRST, &DevList, solarisWalkDeviceNode);
167
168 di_fini(RootNode);
169 return DevList.pHead;
170}
171
172
173static int solarisWalkDeviceNode(di_node_t Node, void *pvArg)
174{
175 PUSBDEVICELIST pList = (PUSBDEVICELIST)pvArg;
176 AssertPtrReturn(pList, DI_WALK_TERMINATE);
177
178 /*
179 * Check if it's a USB device in the first place.
180 */
181 bool fUSBDevice = false;
182 char *pszCompatNames = NULL;
183 int cCompatNames = di_compatible_names(Node, &pszCompatNames);
184 for (int i = 0; i < cCompatNames; i++, pszCompatNames += strlen(pszCompatNames) + 1)
185 if (!strncmp(pszCompatNames, RT_STR_TUPLE("usb")))
186 {
187 fUSBDevice = true;
188 break;
189 }
190
191 if (!fUSBDevice)
192 return DI_WALK_CONTINUE;
193
194 /*
195 * Check if it's a device node or interface.
196 */
197 int *pInt = NULL;
198 char *pStr = NULL;
199 int rc = DI_WALK_CONTINUE;
200 if (di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "interface", &pInt) < 0)
201 {
202 /* It's a device node. */
203 char *pszDevicePath = di_devfs_path(Node);
204 PUSBDEVICE pCur = (PUSBDEVICE)RTMemAllocZ(sizeof(*pCur));
205 if (!pCur)
206 {
207 LogRel(("USBService: failed to allocate %d bytes for PUSBDEVICE.\n", sizeof(*pCur)));
208 return DI_WALK_TERMINATE;
209 }
210
211 bool fValidDevice = false;
212 do
213 {
214 AssertBreak(pszDevicePath);
215
216 char *pszDriverName = di_driver_name(Node);
217
218 /*
219 * Skip hubs
220 */
221 if ( pszDriverName
222 && !strcmp(pszDriverName, "hubd"))
223 {
224 break;
225 }
226
227 /*
228 * Mandatory.
229 * snv_85 and above have usb-dev-descriptor node properties, but older one's do not.
230 * So if we cannot obtain the entire device descriptor, we try falling back to the
231 * individual properties (those must not fail, if it does we drop the device).
232 */
233 uchar_t *pDevData = NULL;
234 int cbProp = di_prop_lookup_bytes(DDI_DEV_T_ANY, Node, "usb-dev-descriptor", &pDevData);
235 if ( cbProp > 0
236 && pDevData)
237 {
238 usb_dev_descr_t *pDeviceDescriptor = (usb_dev_descr_t *)pDevData;
239 pCur->bDeviceClass = pDeviceDescriptor->bDeviceClass;
240 pCur->bDeviceSubClass = pDeviceDescriptor->bDeviceSubClass;
241 pCur->bDeviceProtocol = pDeviceDescriptor->bDeviceProtocol;
242 pCur->idVendor = pDeviceDescriptor->idVendor;
243 pCur->idProduct = pDeviceDescriptor->idProduct;
244 pCur->bcdDevice = pDeviceDescriptor->bcdDevice;
245 pCur->bcdUSB = pDeviceDescriptor->bcdUSB;
246 pCur->bNumConfigurations = pDeviceDescriptor->bNumConfigurations;
247 pCur->fPartialDescriptor = false;
248 }
249 else
250 {
251 AssertBreak(di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "usb-vendor-id", &pInt) > 0);
252 pCur->idVendor = (uint16_t)*pInt;
253
254 AssertBreak(di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "usb-product-id", &pInt) > 0);
255 pCur->idProduct = (uint16_t)*pInt;
256
257 AssertBreak(di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "usb-revision-id", &pInt) > 0);
258 pCur->bcdDevice = (uint16_t)*pInt;
259
260 AssertBreak(di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "usb-release", &pInt) > 0);
261 pCur->bcdUSB = (uint16_t)*pInt;
262
263 pCur->fPartialDescriptor = true;
264 }
265
266 char *pszPortAddr = di_bus_addr(Node);
267 if (pszPortAddr)
268 pCur->bPort = RTStrToUInt8(pszPortAddr); /* Bus & Port are mixed up (kernel driver/userland) */
269 else
270 pCur->bPort = 0;
271
272 char szBuf[PATH_MAX + 48];
273 RTStrPrintf(szBuf, sizeof(szBuf), "%#x:%#x:%d:%s", pCur->idVendor, pCur->idProduct, pCur->bcdDevice, pszDevicePath);
274 pCur->pszAddress = RTStrDup(szBuf);
275 AssertBreak(pCur->pszAddress);
276
277 pCur->pszDevicePath = RTStrDup(pszDevicePath);
278 AssertBreak(pCur->pszDevicePath);
279
280 pCur->pszBackend = RTStrDup("host");
281 AssertBreak(pCur->pszBackend);
282
283 /*
284 * Optional (some devices don't have all these)
285 */
286 if (di_prop_lookup_strings(DDI_DEV_T_ANY, Node, "usb-product-name", &pStr) > 0)
287 {
288 pCur->pszProduct = RTStrDup(pStr);
289 USBLibPurgeEncoding(pCur->pszProduct);
290 }
291
292 if (di_prop_lookup_strings(DDI_DEV_T_ANY, Node, "usb-vendor-name", &pStr) > 0)
293 {
294 pCur->pszManufacturer = RTStrDup(pStr);
295 USBLibPurgeEncoding(pCur->pszManufacturer);
296 }
297
298 if (di_prop_lookup_strings(DDI_DEV_T_ANY, Node, "usb-serialno", &pStr) > 0)
299 {
300 pCur->pszSerialNumber = RTStrDup(pStr);
301 USBLibPurgeEncoding(pCur->pszSerialNumber);
302 }
303
304 if (pCur->bcdUSB == 0x300)
305 pCur->enmSpeed = USBDEVICESPEED_SUPER;
306 else if (di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "low-speed", &pInt) >= 0)
307 pCur->enmSpeed = USBDEVICESPEED_LOW;
308 else if (di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "high-speed", &pInt) >= 0)
309 pCur->enmSpeed = USBDEVICESPEED_HIGH;
310 else
311 pCur->enmSpeed = USBDEVICESPEED_FULL;
312
313 /* Determine state of the USB device. */
314 pCur->enmState = solarisDetermineUSBDeviceState(pCur, Node);
315
316 /*
317 * Valid device, add it to the list.
318 */
319 fValidDevice = true;
320 pCur->pPrev = pList->pTail;
321 if (pList->pTail)
322 pList->pTail = pList->pTail->pNext = pCur;
323 else
324 pList->pTail = pList->pHead = pCur;
325
326 rc = DI_WALK_CONTINUE;
327 } while (0);
328
329 di_devfs_path_free(pszDevicePath);
330 if (!fValidDevice)
331 solarisFreeUSBDevice(pCur);
332 }
333 return rc;
334}
335
336
337static USBDEVICESTATE solarisDetermineUSBDeviceState(PUSBDEVICE pDevice, di_node_t Node)
338{
339 char *pszDriverName = di_driver_name(Node);
340
341 /* Not possible unless a user explicitly unbinds the default driver. */
342 if (!pszDriverName)
343 return USBDEVICESTATE_UNUSED;
344
345 if (!strncmp(pszDriverName, RT_STR_TUPLE(VBOXUSB_DRIVER_NAME)))
346 return USBDEVICESTATE_HELD_BY_PROXY;
347
348 NOREF(pDevice);
349 return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
350}
351
352
353int USBProxyBackendSolaris::captureDevice(HostUSBDevice *aDevice)
354{
355 /*
356 * Check preconditions.
357 */
358 AssertReturn(aDevice, VERR_GENERAL_FAILURE);
359 AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
360
361 AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
362 LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
363
364 Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
365 AssertReturn(aDevice->i_getUsbData(), VERR_INVALID_POINTER);
366
367 /*
368 * Create a one-shot capture filter for the device and reset the device.
369 */
370 USBFILTER Filter;
371 USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_CAPTURE);
372 initFilterFromDevice(&Filter, aDevice);
373
374 void *pvId = USBLibAddFilter(&Filter);
375 if (!pvId)
376 {
377 LogRel(("USBService: failed to add filter\n"));
378 return VERR_GENERAL_FAILURE;
379 }
380
381 PUSBDEVICE pDev = aDevice->i_getUsbData();
382 int rc = USBLibResetDevice(pDev->pszDevicePath, true);
383 if (RT_SUCCESS(rc))
384 aDevice->i_setBackendUserData(pvId);
385 else
386 {
387 USBLibRemoveFilter(pvId);
388 pvId = NULL;
389 }
390 LogFlowThisFunc(("returns %Rrc pvId=%p\n", rc, pvId));
391 return rc;
392}
393
394
395void USBProxyBackendSolaris::captureDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
396{
397 AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
398 /*
399 * Remove the one-shot filter if necessary.
400 */
401 LogFlowThisFunc(("aDevice=%s aSuccess=%RTbool mOneShotId=%p\n", aDevice->i_getName().c_str(), aSuccess, aDevice->i_getBackendUserData()));
402 if (!aSuccess && aDevice->i_getBackendUserData())
403 USBLibRemoveFilter(aDevice->i_getBackendUserData());
404 aDevice->i_setBackendUserData(NULL);
405 USBProxyBackend::captureDeviceCompleted(aDevice, aSuccess);
406}
407
408
409int USBProxyBackendSolaris::releaseDevice(HostUSBDevice *aDevice)
410{
411 /*
412 * Check preconditions.
413 */
414 AssertReturn(aDevice, VERR_GENERAL_FAILURE);
415 AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
416
417 AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
418 LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
419
420 Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
421 AssertReturn(aDevice->i_getUsbData(), VERR_INVALID_POINTER);
422
423 /*
424 * Create a one-shot ignore filter for the device and reset it.
425 */
426 USBFILTER Filter;
427 USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_IGNORE);
428 initFilterFromDevice(&Filter, aDevice);
429
430 void *pvId = USBLibAddFilter(&Filter);
431 if (!pvId)
432 {
433 LogRel(("USBService: Adding ignore filter failed!\n"));
434 return VERR_GENERAL_FAILURE;
435 }
436
437 PUSBDEVICE pDev = aDevice->i_getUsbData();
438 int rc = USBLibResetDevice(pDev->pszDevicePath, true /* Re-attach */);
439 if (RT_SUCCESS(rc))
440 aDevice->i_setBackendUserData(pvId);
441 else
442 {
443 USBLibRemoveFilter(pvId);
444 pvId = NULL;
445 }
446 LogFlowThisFunc(("returns %Rrc pvId=%p\n", rc, pvId));
447 return rc;
448}
449
450
451void USBProxyBackendSolaris::releaseDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
452{
453 AssertReturnVoid(aDevice->isWriteLockOnCurrentThread());
454 /*
455 * Remove the one-shot filter if necessary.
456 */
457 LogFlowThisFunc(("aDevice=%s aSuccess=%RTbool mOneShotId=%p\n", aDevice->i_getName().c_str(), aSuccess, aDevice->i_getBackendUserData()));
458 if (!aSuccess && aDevice->i_getBackendUserData())
459 USBLibRemoveFilter(aDevice->i_getBackendUserData());
460 aDevice->i_setBackendUserData(NULL);
461 USBProxyBackend::releaseDeviceCompleted(aDevice, aSuccess);
462}
463
464
465/**
466 * Returns whether devices reported by this backend go through a de/re-attach
467 * and device re-enumeration cycle when they are captured or released.
468 */
469bool USBProxyBackendSolaris::i_isDevReEnumerationRequired()
470{
471 return true;
472}
473
474
475/**
476 * Wrapper called by walkDeviceNode.
477 *
478 * @param pDevice The USB device to free.
479 */
480void solarisFreeUSBDevice(PUSBDEVICE pDevice)
481{
482 USBProxyBackend::freeDevice(pDevice);
483}
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