VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/solaris/USBProxyServiceSolaris.cpp@ 59091

Last change on this file since 59091 was 59091, checked in by vboxsync, 9 years ago

USB/Solaris: Rewrite, cleanup and numerous bug fixes to get the Apple iPhone working.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette