VirtualBox

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

Last change on this file since 72919 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

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