VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/freebsd/USBProxyBackendFreeBSD.cpp@ 65539

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

Main/USBProxyBackendFreeBSD: Fixes to make it work again, contributed by Hans Petter Selasky, thanks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 KB
Line 
1/* $Id: USBProxyBackendFreeBSD.cpp 65539 2017-01-31 12:30:31Z vboxsync $ */
2/** @file
3 * VirtualBox USB Proxy Service, FreeBSD 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
29#include <iprt/string.h>
30#include <iprt/alloc.h>
31#include <iprt/assert.h>
32#include <iprt/file.h>
33#include <iprt/err.h>
34#include <iprt/mem.h>
35#include <iprt/param.h>
36#include <iprt/path.h>
37#include <iprt/semaphore.h>
38
39#include <stdlib.h>
40#include <string.h>
41#include <stdio.h>
42#include <errno.h>
43#include <unistd.h>
44#include <fcntl.h>
45#include <sys/poll.h>
46#include <dev/usb/usb.h>
47#include <dev/usb/usb_ioctl.h>
48
49
50/*********************************************************************************************************************************
51* Structures and Typedefs *
52*********************************************************************************************************************************/
53
54
55/*********************************************************************************************************************************
56* Global Variables *
57*********************************************************************************************************************************/
58
59/**
60 * Initialize data members.
61 */
62USBProxyBackendFreeBSD::USBProxyBackendFreeBSD()
63 : USBProxyBackend(), mNotifyEventSem(NIL_RTSEMEVENT)
64{
65 LogFlowThisFunc(("\n"));
66}
67
68USBProxyBackendFreeBSD::~USBProxyBackendFreeBSD()
69{
70 LogFlowThisFunc(("\n"));
71}
72
73/**
74 * Initializes the object (called right after construction).
75 *
76 * @returns S_OK on success and non-fatal failures, some COM error otherwise.
77 */
78int USBProxyBackendFreeBSD::init(USBProxyService *pUsbProxyService, const com::Utf8Str &strId, const com::Utf8Str &strAddress)
79{
80 USBProxyBackend::init(pUsbProxyService, strId, strAddress);
81
82 unconst(m_strBackend) = Utf8Str("host");
83
84 /*
85 * Create semaphore.
86 */
87 int rc = RTSemEventCreate(&mNotifyEventSem);
88 if (RT_FAILURE(rc))
89 return rc;
90
91 /*
92 * Start the poller thread.
93 */
94 start();
95 return VINF_SUCCESS;
96}
97
98
99/**
100 * Stop all service threads and free the device chain.
101 */
102void USBProxyBackendFreeBSD::uninit()
103{
104 LogFlowThisFunc(("\n"));
105
106 /*
107 * Stop the service.
108 */
109 if (isActive())
110 stop();
111
112 RTSemEventDestroy(mNotifyEventSem);
113 mNotifyEventSem = NULL;
114 USBProxyBackend::uninit();
115}
116
117
118int USBProxyBackendFreeBSD::captureDevice(HostUSBDevice *aDevice)
119{
120 AssertReturn(aDevice, VERR_GENERAL_FAILURE);
121 AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
122
123 AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
124 LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
125
126 /*
127 * Don't think we need to do anything when the device is held... fake it.
128 */
129 Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
130 devLock.release();
131 interruptWait();
132
133 return VINF_SUCCESS;
134}
135
136
137int USBProxyBackendFreeBSD::releaseDevice(HostUSBDevice *aDevice)
138{
139 AssertReturn(aDevice, VERR_GENERAL_FAILURE);
140 AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
141
142 AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
143 LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
144
145 /*
146 * We're not really holding it atm., just fake it.
147 */
148 Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
149 devLock.release();
150 interruptWait();
151
152 return VINF_SUCCESS;
153}
154
155
156bool USBProxyBackendFreeBSD::isFakeUpdateRequired()
157{
158 return true;
159}
160
161
162int USBProxyBackendFreeBSD::wait(RTMSINTERVAL aMillies)
163{
164 return RTSemEventWait(mNotifyEventSem, aMillies < 1000 ? 1000 : 5000);
165}
166
167
168int USBProxyBackendFreeBSD::interruptWait(void)
169{
170 return RTSemEventSignal(mNotifyEventSem);
171}
172
173
174/**
175 * Dumps a USBDEVICE structure to the log using LogLevel 3.
176 * @param pDev The structure to log.
177 * @todo This is really common code.
178 */
179DECLINLINE(void) usbLogDevice(PUSBDEVICE pDev)
180{
181 NOREF(pDev);
182
183 Log3(("USB device:\n"));
184 Log3(("Product: %s (%x)\n", pDev->pszProduct, pDev->idProduct));
185 Log3(("Manufacturer: %s (Vendor ID %x)\n", pDev->pszManufacturer, pDev->idVendor));
186 Log3(("Serial number: %s (%llx)\n", pDev->pszSerialNumber, pDev->u64SerialHash));
187 Log3(("Device revision: %d\n", pDev->bcdDevice));
188 Log3(("Device class: %x\n", pDev->bDeviceClass));
189 Log3(("Device subclass: %x\n", pDev->bDeviceSubClass));
190 Log3(("Device protocol: %x\n", pDev->bDeviceProtocol));
191 Log3(("USB version number: %d\n", pDev->bcdUSB));
192 Log3(("Device speed: %s\n",
193 pDev->enmSpeed == USBDEVICESPEED_UNKNOWN ? "unknown"
194 : pDev->enmSpeed == USBDEVICESPEED_LOW ? "1.5 MBit/s"
195 : pDev->enmSpeed == USBDEVICESPEED_FULL ? "12 MBit/s"
196 : pDev->enmSpeed == USBDEVICESPEED_HIGH ? "480 MBit/s"
197 : pDev->enmSpeed == USBDEVICESPEED_VARIABLE ? "variable"
198 : "invalid"));
199 Log3(("Number of configurations: %d\n", pDev->bNumConfigurations));
200 Log3(("Bus number: %d\n", pDev->bBus));
201 Log3(("Port number: %d\n", pDev->bPort));
202 Log3(("Device number: %d\n", pDev->bDevNum));
203 Log3(("Device state: %s\n",
204 pDev->enmState == USBDEVICESTATE_UNSUPPORTED ? "unsupported"
205 : pDev->enmState == USBDEVICESTATE_USED_BY_HOST ? "in use by host"
206 : pDev->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE ? "in use by host, possibly capturable"
207 : pDev->enmState == USBDEVICESTATE_UNUSED ? "not in use"
208 : pDev->enmState == USBDEVICESTATE_HELD_BY_PROXY ? "held by proxy"
209 : pDev->enmState == USBDEVICESTATE_USED_BY_GUEST ? "used by guest"
210 : "invalid"));
211 Log3(("OS device address: %s\n", pDev->pszAddress));
212}
213
214
215PUSBDEVICE USBProxyBackendFreeBSD::getDevices(void)
216{
217 PUSBDEVICE pDevices = NULL;
218 int FileUsb = 0;
219 int iBus = 0;
220 int iAddr = 1;
221 int rc = VINF_SUCCESS;
222 char *pszDevicePath = NULL;
223 uint32_t PlugTime = 0;
224
225 for (;;)
226 {
227 rc = RTStrAPrintf(&pszDevicePath, "/dev/%s%d.%d", USB_GENERIC_NAME, iBus, iAddr);
228 if (RT_FAILURE(rc))
229 break;
230
231 LogFlowFunc((": Opening %s\n", pszDevicePath));
232
233 FileUsb = open(pszDevicePath, O_RDONLY);
234 if (FileUsb < 0)
235 {
236 RTStrFree(pszDevicePath);
237
238 if ((errno == ENOENT) && (iAddr > 1))
239 {
240 iAddr = 1;
241 iBus++;
242 continue;
243 }
244 else if (errno == EACCES)
245 {
246 /* Skip devices without the right permission. */
247 iAddr++;
248 continue;
249 }
250 else
251 break;
252 }
253
254 LogFlowFunc((": %s opened successfully\n", pszDevicePath));
255
256 struct usb_device_info UsbDevInfo;
257 RT_ZERO(UsbDevInfo);
258
259 rc = ioctl(FileUsb, USB_GET_DEVICEINFO, &UsbDevInfo);
260 if (rc < 0)
261 {
262 LogFlowFunc((": Error querying device info rc=%Rrc\n", RTErrConvertFromErrno(errno)));
263 close(FileUsb);
264 RTStrFree(pszDevicePath);
265 break;
266 }
267
268 /* Filter out hubs */
269 if (UsbDevInfo.udi_class != 0x09)
270 {
271 PUSBDEVICE pDevice = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
272 if (!pDevice)
273 {
274 close(FileUsb);
275 RTStrFree(pszDevicePath);
276 break;
277 }
278
279 pDevice->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
280 pDevice->bBus = UsbDevInfo.udi_bus;
281 pDevice->bPort = UsbDevInfo.udi_hubport;
282 pDevice->bDeviceClass = UsbDevInfo.udi_class;
283 pDevice->bDeviceSubClass = UsbDevInfo.udi_subclass;
284 pDevice->bDeviceProtocol = UsbDevInfo.udi_protocol;
285 pDevice->bNumConfigurations = UsbDevInfo.udi_config_no;
286 pDevice->idVendor = UsbDevInfo.udi_vendorNo;
287 pDevice->idProduct = UsbDevInfo.udi_productNo;
288 pDevice->bDevNum = UsbDevInfo.udi_index;
289
290 switch (UsbDevInfo.udi_speed)
291 {
292 case USB_SPEED_LOW:
293 pDevice->enmSpeed = USBDEVICESPEED_LOW;
294 break;
295 case USB_SPEED_FULL:
296 pDevice->enmSpeed = USBDEVICESPEED_FULL;
297 break;
298 case USB_SPEED_HIGH:
299 pDevice->enmSpeed = USBDEVICESPEED_HIGH;
300 break;
301 case USB_SPEED_SUPER:
302 pDevice->enmSpeed = USBDEVICESPEED_SUPER;
303 break;
304 case USB_SPEED_VARIABLE:
305 pDevice->enmSpeed = USBDEVICESPEED_VARIABLE;
306 break;
307 default:
308 pDevice->enmSpeed = USBDEVICESPEED_UNKNOWN;
309 break;
310 }
311
312 if (UsbDevInfo.udi_vendor[0] != '\0')
313 {
314 USBLibPurgeEncoding(UsbDevInfo.udi_vendor);
315 pDevice->pszManufacturer = RTStrDupN(UsbDevInfo.udi_vendor, sizeof(UsbDevInfo.udi_vendor));
316 }
317
318 if (UsbDevInfo.udi_product[0] != '\0')
319 {
320 USBLibPurgeEncoding(UsbDevInfo.udi_product);
321 pDevice->pszProduct = RTStrDupN(UsbDevInfo.udi_product, sizeof(UsbDevInfo.udi_product));
322 }
323
324 if (UsbDevInfo.udi_serial[0] != '\0')
325 {
326 USBLibPurgeEncoding(UsbDevInfo.udi_serial);
327 pDevice->pszSerialNumber = RTStrDupN(UsbDevInfo.udi_serial, sizeof(UsbDevInfo.udi_serial));
328 pDevice->u64SerialHash = USBLibHashSerial(UsbDevInfo.udi_serial);
329 }
330 rc = ioctl(FileUsb, USB_GET_PLUGTIME, &PlugTime);
331 if (rc == 0)
332 pDevice->u64SerialHash += PlugTime;
333
334 pDevice->pszAddress = RTStrDup(pszDevicePath);
335 pDevice->pszBackend = RTStrDup("host");
336
337 usbLogDevice(pDevice);
338
339 pDevice->pNext = pDevices;
340 if (pDevices)
341 pDevices->pPrev = pDevice;
342 pDevices = pDevice;
343 }
344 close(FileUsb);
345 RTStrFree(pszDevicePath);
346 iAddr++;
347 }
348
349 return pDevices;
350}
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