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