VirtualBox

source: vbox/trunk/src/VBox/Main/solaris/USBProxyServiceSolaris.cpp@ 33155

Last change on this file since 33155 was 31911, checked in by vboxsync, 14 years ago

make the VBOX_WITH_NEW_USB_CODE_ON_SOLARIS code mandatory

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
1/* $Id: USBProxyServiceSolaris.cpp 31911 2010-08-24 11:56:45Z vboxsync $ */
2/** @file
3 * VirtualBox USB Proxy Service, Solaris Specialization.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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*******************************************************************************/
37static int solarisWalkDeviceNode(di_node_t Node, void *pvArg);
38static void solarisFreeUSBDevice(PUSBDEVICE pDevice);
39static USBDEVICESTATE solarisDetermineUSBDeviceState(PUSBDEVICE pDevice, di_node_t Node);
40
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45typedef struct USBDEVICELIST
46{
47 PUSBDEVICE pHead;
48 PUSBDEVICE pTail;
49} USBDEVICELIST;
50typedef USBDEVICELIST *PUSBDEVICELIST;
51
52
53/**
54 * Initialize data members.
55 */
56USBProxyServiceSolaris::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 */
68HRESULT USBProxyServiceSolaris::init(void)
69{
70 /*
71 * Call the superclass method first.
72 */
73 HRESULT hrc = USBProxyService::init();
74 AssertComRCReturn(hrc, hrc);
75
76 /*
77 * Create semaphore.
78 */
79 int rc = RTSemEventCreate(&mNotifyEventSem);
80 if (RT_FAILURE(rc))
81 {
82 mLastError = rc;
83 return E_FAIL;
84 }
85
86 /*
87 * Initialize the USB library.
88 */
89 rc = USBLibInit();
90 if (RT_FAILURE(rc))
91 {
92 mLastError = rc;
93 return S_OK;
94 }
95 mUSBLibInitialized = true;
96
97 /*
98 * Start the poller thread.
99 */
100 start();
101 return S_OK;
102}
103
104
105/**
106 * Stop all service threads and free the device chain.
107 */
108USBProxyServiceSolaris::~USBProxyServiceSolaris()
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 RTSemEventDestroy(mNotifyEventSem);
128 mNotifyEventSem = NULL;
129}
130
131
132void *USBProxyServiceSolaris::insertFilter(PCUSBFILTER aFilter)
133{
134 return USBLibAddFilter(aFilter);
135}
136
137
138void USBProxyServiceSolaris::removeFilter(void *pvID)
139{
140 USBLibRemoveFilter(pvID);
141}
142
143
144int USBProxyServiceSolaris::wait(RTMSINTERVAL aMillies)
145{
146 return RTSemEventWait(mNotifyEventSem, aMillies < 1000 ? 1000 : RT_MIN(aMillies, 5000));
147}
148
149
150int USBProxyServiceSolaris::interruptWait(void)
151{
152 return RTSemEventSignal(mNotifyEventSem);
153}
154
155
156PUSBDEVICE USBProxyServiceSolaris::getDevices(void)
157{
158 USBDEVICELIST DevList;
159 DevList.pHead = NULL;
160 DevList.pTail = NULL;
161 di_node_t RootNode = di_init("/", DINFOCPYALL);
162 if (RootNode != DI_NODE_NIL)
163 di_walk_node(RootNode, DI_WALK_CLDFIRST, &DevList, solarisWalkDeviceNode);
164
165 di_fini(RootNode);
166 return DevList.pHead;
167}
168
169#if 0
170static int solarisWalkMinor(di_node_t Node, di_minor_t Minor, void *pvArg)
171{
172 char *pszDevFsPath = di_devfs_path(Node);
173 char *pszMinorName = di_minor_name(Minor);
174 PUSBDEVICE pDev = (PUSBDEVICE)pvArg;
175
176 AssertRelease(pDev);
177
178 if (!pszDevFsPath || !pszMinorName)
179 return DI_WALK_CONTINUE;
180
181 RTStrAPrintf(&pDev->pszApId, "/devices%s:%s", pszDevFsPath, pszMinorName);
182 di_devfs_path_free(pszDevFsPath);
183
184 syslog(LOG_ERR, "VBoxUsbApId:%s\n", pDev->pszApId);
185 return DI_WALK_TERMINATE;
186}
187
188static bool solarisGetApId(PUSBDEVICE pDev, char *pszDevicePath, di_node_t RootNode)
189{
190 pDev->pszApId = NULL;
191
192 /* Skip "/devices" prefix if any */
193 char achDevicesDir[] = "/devices/";
194 if (strncmp(pszDevicePath, achDevicesDir, sizeof(achDevicesDir)) == 0)
195 pszDevicePath += sizeof(achDevicesDir);
196
197 char *pszPhysical = RTStrDup(pszDevicePath);
198 char *pszTmp = NULL;
199
200 /* Remove dynamic component "::" if any */
201 if ((pszTmp = strstr(pszPhysical, "::")) != NULL)
202 *pszTmp = '\0';
203
204 /* Remove minor name if any */
205 if ((pszTmp = strrchr(pszPhysical, ':')) != NULL)
206 *pszTmp = '\0';
207
208 /* Walk device tree */
209// di_node_t RootNode = di_init("/", DINFOCPYALL);
210// if (RootNode != DI_NODE_NIL)
211// {
212// di_node_t MinorNode = di_lookup_node(RootNode, pszPhysical);
213// if (MinorNode != DI_NODE_NIL)
214 {
215 di_walk_minor(RootNode, NULL, DI_CHECK_ALIAS | DI_CHECK_INTERNAL_PATH, pDev, solarisWalkMinor);
216 return true;
217 }
218// di_fini(RootNode);
219// }
220
221 return false;
222}
223#endif
224
225static int solarisWalkDeviceNode(di_node_t Node, void *pvArg)
226{
227 PUSBDEVICELIST pList = (PUSBDEVICELIST)pvArg;
228 AssertPtrReturn(pList, DI_WALK_TERMINATE);
229
230 /*
231 * Check if it's a USB device in the first place.
232 */
233 bool fUSBDevice = false;
234 char *pszCompatNames = NULL;
235 int cCompatNames = di_compatible_names(Node, &pszCompatNames);
236 for (int i = 0; i < cCompatNames; i++, pszCompatNames += strlen(pszCompatNames) + 1)
237 if (!strncmp(pszCompatNames, "usb", 3))
238 {
239 fUSBDevice = true;
240 break;
241 }
242
243 if (!fUSBDevice)
244 return DI_WALK_CONTINUE;
245
246 /*
247 * Check if it's a device node or interface.
248 */
249 int *pInt = NULL;
250 char *pStr = NULL;
251 int rc = DI_WALK_CONTINUE;
252 if (di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "interface", &pInt) < 0)
253 {
254 /* It's a device node. */
255 char *pszDevicePath = di_devfs_path(Node);
256 PUSBDEVICE pCur = (PUSBDEVICE)RTMemAllocZ(sizeof(*pCur));
257 if (!pCur)
258 {
259 LogRel(("USBService: failed to allocate %d bytes for PUSBDEVICE.\n", sizeof(*pCur)));
260 return DI_WALK_TERMINATE;
261 }
262
263 bool fValidDevice = false;
264 do
265 {
266 AssertBreak(pszDevicePath);
267
268 char *pszDriverName = di_driver_name(Node);
269
270 /*
271 * Skip hubs
272 */
273 if ( pszDriverName
274 && !strcmp(pszDriverName, "hubd"))
275 {
276 break;
277 }
278
279 /*
280 * Mandatory.
281 * snv_85 and above have usb-dev-descriptor node properties, but older one's do not.
282 * So if we cannot obtain the entire device descriptor, we try falling back to the
283 * invidividual properties (those must not fail, if it does we drop the device).
284 */
285 uchar_t *pDevData = NULL;
286 int cbProp = di_prop_lookup_bytes(DDI_DEV_T_ANY, Node, "usb-dev-descriptor", &pDevData);
287 if ( cbProp > 0
288 && pDevData)
289 {
290 usb_dev_descr_t *pDeviceDescriptor = (usb_dev_descr_t *)pDevData;
291 pCur->bDeviceClass = pDeviceDescriptor->bDeviceClass;
292 pCur->bDeviceSubClass = pDeviceDescriptor->bDeviceSubClass;
293 pCur->bDeviceProtocol = pDeviceDescriptor->bDeviceProtocol;
294 pCur->idVendor = pDeviceDescriptor->idVendor;
295 pCur->idProduct = pDeviceDescriptor->idProduct;
296 pCur->bcdDevice = pDeviceDescriptor->bcdDevice;
297 pCur->bcdUSB = pDeviceDescriptor->bcdUSB;
298 pCur->bNumConfigurations = pDeviceDescriptor->bNumConfigurations;
299 pCur->fPartialDescriptor = false;
300 }
301 else
302 {
303 AssertBreak(di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "usb-vendor-id", &pInt) > 0);
304 pCur->idVendor = (uint16_t)*pInt;
305
306 AssertBreak(di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "usb-product-id", &pInt) > 0);
307 pCur->idProduct = (uint16_t)*pInt;
308
309 AssertBreak(di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "usb-revision-id", &pInt) > 0);
310 pCur->bcdDevice = (uint16_t)*pInt;
311
312 AssertBreak(di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "usb-release", &pInt) > 0);
313 pCur->bcdUSB = (uint16_t)*pInt;
314
315 pCur->fPartialDescriptor = true;
316 }
317
318 char *pszPortAddr = di_bus_addr(Node);
319 if (pszPortAddr)
320 pCur->bPort = RTStrToUInt8(pszPortAddr); /* Bus & Port are mixed up (kernel driver/userland) */
321 else
322 pCur->bPort = 0;
323
324#if 0
325 /*
326 * Obtain the dev_t of the device.
327 */
328 di_minor_t Minor = di_minor_next(Node, DI_MINOR_NIL);
329 AssertBreak(Minor != DI_MINOR_NIL);
330 dev_t DeviceNum = di_minor_devt(Minor);
331
332 int DevInstance = 0;
333 rc = solarisUSBGetInstance(pszDevicePath, &DevInstance);
334
335 char szAddress[PATH_MAX + 128];
336 RTStrPrintf(szAddress, sizeof(szAddress), "/dev/usb/%x.%x|%s", pCur->idVendor, pCur->idProduct, pszDevicePath);
337 /* @todo after binding ugen we need to append the instance number to the address. Not yet sure how we can update PUSBDEVICE at that time. */
338
339 pCur->pszAddress = RTStrDup(szAddress);
340 AssertBreak(pCur->pszAddress);
341#endif
342
343 char pathBuf[PATH_MAX];
344 RTStrPrintf(pathBuf, sizeof(pathBuf), "%s", pszDevicePath);
345 RTPathStripFilename(pathBuf);
346
347 char szBuf[PATH_MAX + 48];
348 RTStrPrintf(szBuf, sizeof(szBuf), "%#x:%#x:%d:%s", pCur->idVendor, pCur->idProduct, pCur->bcdDevice, pathBuf);
349 pCur->pszAddress = RTStrDup(szBuf);
350
351 pCur->pszDevicePath = RTStrDup(pszDevicePath);
352 AssertBreak(pCur->pszDevicePath);
353
354 /*
355 * Optional (some devices don't have all these)
356 */
357 if (di_prop_lookup_strings(DDI_DEV_T_ANY, Node, "usb-product-name", &pStr) > 0)
358 pCur->pszProduct = RTStrDup(pStr);
359
360 if (di_prop_lookup_strings(DDI_DEV_T_ANY, Node, "usb-vendor-name", &pStr) > 0)
361 pCur->pszManufacturer = RTStrDup(pStr);
362
363 if (di_prop_lookup_strings(DDI_DEV_T_ANY, Node, "usb-serialno", &pStr) > 0)
364 pCur->pszSerialNumber = RTStrDup(pStr);
365
366 if (di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "low-speed", &pInt) >= 0)
367 pCur->enmSpeed = USBDEVICESPEED_LOW;
368 else if (di_prop_lookup_ints(DDI_DEV_T_ANY, Node, "high-speed", &pInt) >= 0)
369 pCur->enmSpeed = USBDEVICESPEED_HIGH;
370 else
371 pCur->enmSpeed = USBDEVICESPEED_FULL;
372
373 /* Determine state of the USB device. */
374 pCur->enmState = solarisDetermineUSBDeviceState(pCur, Node);
375
376// fValidDevice = solarisGetApId(pCur, pszDevicePath, Node);
377 fValidDevice = true;
378
379 /*
380 * Valid device, add it to the list.
381 */
382 if (fValidDevice)
383 {
384 pCur->pPrev = pList->pTail;
385 if (pList->pTail)
386 pList->pTail = pList->pTail->pNext = pCur;
387 else
388 pList->pTail = pList->pHead = pCur;
389 }
390 rc = DI_WALK_CONTINUE;
391 } while(0);
392
393 di_devfs_path_free(pszDevicePath);
394 if (!fValidDevice)
395 solarisFreeUSBDevice(pCur);
396 }
397 return rc;
398}
399
400
401static USBDEVICESTATE solarisDetermineUSBDeviceState(PUSBDEVICE pDevice, di_node_t Node)
402{
403 char *pszDriverName = di_driver_name(Node);
404
405 /* Not possible unless a user explicitly unbinds the default driver. */
406 if (!pszDriverName)
407 return USBDEVICESTATE_UNUSED;
408
409 if (!strncmp(pszDriverName, VBOXUSB_DRIVER_NAME, sizeof(VBOXUSB_DRIVER_NAME) - 1))
410 return USBDEVICESTATE_HELD_BY_PROXY;
411
412 NOREF(pDevice);
413 return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
414}
415
416
417int USBProxyServiceSolaris::captureDevice(HostUSBDevice *aDevice)
418{
419 /*
420 * Check preconditions.
421 */
422 AssertReturn(aDevice, VERR_GENERAL_FAILURE);
423 LogFlowThisFunc(("aDevice=%s\n", aDevice->getName().c_str()));
424 AssertReturn(aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
425 Assert(aDevice->getUnistate() == kHostUSBDeviceState_Capturing);
426 AssertReturn(aDevice->mUsb, VERR_INVALID_POINTER);
427
428 /*
429 * Create a one-shot capture filter for the device and reset the device.
430 */
431 USBFILTER Filter;
432 USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_CAPTURE);
433 initFilterFromDevice(&Filter, aDevice);
434
435 void *pvId = USBLibAddFilter(&Filter);
436 if (!pvId)
437 {
438 LogRel(("USBService: failed to add filter\n"));
439 return VERR_GENERAL_FAILURE;
440 }
441
442 PUSBDEVICE pDev = aDevice->mUsb;
443 int rc = USBLibResetDevice(pDev->pszDevicePath, true);
444 if (RT_SUCCESS(rc))
445 aDevice->mOneShotId = pvId;
446 else
447 {
448 USBLibRemoveFilter(pvId);
449 pvId = NULL;
450 }
451 LogFlowThisFunc(("returns %Rrc pvId=%p\n", rc, pvId));
452 return rc;
453}
454
455
456void USBProxyServiceSolaris::captureDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
457{
458 /*
459 * Remove the one-shot filter if necessary.
460 */
461 LogFlowThisFunc(("aDevice=%s aSuccess=%RTbool mOneShotId=%p\n", aDevice->getName().c_str(), aSuccess, aDevice->mOneShotId));
462 if (!aSuccess && aDevice->mOneShotId)
463 USBLibRemoveFilter(aDevice->mOneShotId);
464 aDevice->mOneShotId = NULL;
465}
466
467
468int USBProxyServiceSolaris::releaseDevice(HostUSBDevice *aDevice)
469{
470 /*
471 * Check preconditions.
472 */
473 AssertReturn(aDevice, VERR_GENERAL_FAILURE);
474 LogFlowThisFunc(("aDevice=%s\n", aDevice->getName().c_str()));
475 AssertReturn(aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
476 Assert(aDevice->getUnistate() == kHostUSBDeviceState_ReleasingToHost);
477 AssertReturn(aDevice->mUsb, VERR_INVALID_POINTER);
478
479 /*
480 * Create a one-shot ignore filter for the device and reset it.
481 */
482 USBFILTER Filter;
483 USBFilterInit(&Filter, USBFILTERTYPE_ONESHOT_IGNORE);
484 initFilterFromDevice(&Filter, aDevice);
485
486 void *pvId = USBLibAddFilter(&Filter);
487 if (!pvId)
488 {
489 LogRel(("USBService: Adding ignore filter failed!\n"));
490 return VERR_GENERAL_FAILURE;
491 }
492
493 PUSBDEVICE pDev = aDevice->mUsb;
494 int rc = USBLibResetDevice(pDev->pszDevicePath, true /* Re-attach */);
495 if (RT_SUCCESS(rc))
496 aDevice->mOneShotId = pvId;
497 else
498 {
499 USBLibRemoveFilter(pvId);
500 pvId = NULL;
501 }
502 LogFlowThisFunc(("returns %Rrc pvId=%p\n", rc, pvId));
503 return rc;
504}
505
506
507void USBProxyServiceSolaris::releaseDeviceCompleted(HostUSBDevice *aDevice, bool aSuccess)
508{
509 /*
510 * Remove the one-shot filter if necessary.
511 */
512 LogFlowThisFunc(("aDevice=%s aSuccess=%RTbool mOneShotId=%p\n", aDevice->getName().c_str(), aSuccess, aDevice->mOneShotId));
513 if (!aSuccess && aDevice->mOneShotId)
514 USBLibRemoveFilter(aDevice->mOneShotId);
515 aDevice->mOneShotId = NULL;
516}
517
518
519bool USBProxyServiceSolaris::updateDeviceState(HostUSBDevice *aDevice, PUSBDEVICE aUSBDevice, bool *aRunFilters, SessionMachine **aIgnoreMachine)
520{
521 return USBProxyService::updateDeviceState(aDevice, aUSBDevice, aRunFilters, aIgnoreMachine);
522}
523
524/**
525 * Wrapper called by walkDeviceNode.
526 *
527 * @param pDevice The USB device to free.
528 */
529void solarisFreeUSBDevice(PUSBDEVICE pDevice)
530{
531 USBProxyService::freeDevice(pDevice);
532}
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