1 | /* $Id: USBProxyBackendFreeBSD.cpp 62485 2016-07-22 18:36:43Z 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 | */
|
---|
62 | USBProxyBackendFreeBSD::USBProxyBackendFreeBSD()
|
---|
63 | : USBProxyBackend(), mNotifyEventSem(NIL_RTSEMEVENT)
|
---|
64 | {
|
---|
65 | LogFlowThisFunc(("\n"));
|
---|
66 | }
|
---|
67 |
|
---|
68 | USBProxyBackendFreeBSD::~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 | */
|
---|
78 | int 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 | */
|
---|
102 | void 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 |
|
---|
118 | int 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 |
|
---|
137 | int 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 |
|
---|
156 | int USBProxyBackendFreeBSD::wait(RTMSINTERVAL aMillies)
|
---|
157 | {
|
---|
158 | return RTSemEventWait(mNotifyEventSem, aMillies < 1000 ? 1000 : 5000);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | int USBProxyBackendFreeBSD::interruptWait(void)
|
---|
163 | {
|
---|
164 | return RTSemEventSignal(mNotifyEventSem);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Dumps a USBDEVICE structure to the log using LogLevel 3.
|
---|
170 | * @param pDev The structure to log.
|
---|
171 | * @todo This is really common code.
|
---|
172 | */
|
---|
173 | DECLINLINE(void) usbLogDevice(PUSBDEVICE pDev)
|
---|
174 | {
|
---|
175 | NOREF(pDev);
|
---|
176 |
|
---|
177 | Log3(("USB device:\n"));
|
---|
178 | Log3(("Product: %s (%x)\n", pDev->pszProduct, pDev->idProduct));
|
---|
179 | Log3(("Manufacturer: %s (Vendor ID %x)\n", pDev->pszManufacturer, pDev->idVendor));
|
---|
180 | Log3(("Serial number: %s (%llx)\n", pDev->pszSerialNumber, pDev->u64SerialHash));
|
---|
181 | Log3(("Device revision: %d\n", pDev->bcdDevice));
|
---|
182 | Log3(("Device class: %x\n", pDev->bDeviceClass));
|
---|
183 | Log3(("Device subclass: %x\n", pDev->bDeviceSubClass));
|
---|
184 | Log3(("Device protocol: %x\n", pDev->bDeviceProtocol));
|
---|
185 | Log3(("USB version number: %d\n", pDev->bcdUSB));
|
---|
186 | Log3(("Device speed: %s\n",
|
---|
187 | pDev->enmSpeed == USBDEVICESPEED_UNKNOWN ? "unknown"
|
---|
188 | : pDev->enmSpeed == USBDEVICESPEED_LOW ? "1.5 MBit/s"
|
---|
189 | : pDev->enmSpeed == USBDEVICESPEED_FULL ? "12 MBit/s"
|
---|
190 | : pDev->enmSpeed == USBDEVICESPEED_HIGH ? "480 MBit/s"
|
---|
191 | : pDev->enmSpeed == USBDEVICESPEED_VARIABLE ? "variable"
|
---|
192 | : "invalid"));
|
---|
193 | Log3(("Number of configurations: %d\n", pDev->bNumConfigurations));
|
---|
194 | Log3(("Bus number: %d\n", pDev->bBus));
|
---|
195 | Log3(("Port number: %d\n", pDev->bPort));
|
---|
196 | Log3(("Device number: %d\n", pDev->bDevNum));
|
---|
197 | Log3(("Device state: %s\n",
|
---|
198 | pDev->enmState == USBDEVICESTATE_UNSUPPORTED ? "unsupported"
|
---|
199 | : pDev->enmState == USBDEVICESTATE_USED_BY_HOST ? "in use by host"
|
---|
200 | : pDev->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE ? "in use by host, possibly capturable"
|
---|
201 | : pDev->enmState == USBDEVICESTATE_UNUSED ? "not in use"
|
---|
202 | : pDev->enmState == USBDEVICESTATE_HELD_BY_PROXY ? "held by proxy"
|
---|
203 | : pDev->enmState == USBDEVICESTATE_USED_BY_GUEST ? "used by guest"
|
---|
204 | : "invalid"));
|
---|
205 | Log3(("OS device address: %s\n", pDev->pszAddress));
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | PUSBDEVICE USBProxyBackendFreeBSD::getDevices(void)
|
---|
210 | {
|
---|
211 | PUSBDEVICE pDevices = NULL;
|
---|
212 | int FileUsb = 0;
|
---|
213 | int iBus = 0;
|
---|
214 | int iAddr = 1;
|
---|
215 | int rc = VINF_SUCCESS;
|
---|
216 | char *pszDevicePath = NULL;
|
---|
217 | uint32_t PlugTime = 0;
|
---|
218 |
|
---|
219 | for (;;)
|
---|
220 | {
|
---|
221 | rc = RTStrAPrintf(&pszDevicePath, "/dev/%s%d.%d", USB_GENERIC_NAME, iBus, iAddr);
|
---|
222 | if (RT_FAILURE(rc))
|
---|
223 | break;
|
---|
224 |
|
---|
225 | LogFlowFunc((": Opening %s\n", pszDevicePath));
|
---|
226 |
|
---|
227 | FileUsb = open(pszDevicePath, O_RDONLY);
|
---|
228 | if (FileUsb < 0)
|
---|
229 | {
|
---|
230 | RTStrFree(pszDevicePath);
|
---|
231 |
|
---|
232 | if ((errno == ENOENT) && (iAddr > 1))
|
---|
233 | {
|
---|
234 | iAddr = 1;
|
---|
235 | iBus++;
|
---|
236 | continue;
|
---|
237 | }
|
---|
238 | else if (errno == EACCES)
|
---|
239 | {
|
---|
240 | /* Skip devices without the right permission. */
|
---|
241 | iAddr++;
|
---|
242 | continue;
|
---|
243 | }
|
---|
244 | else
|
---|
245 | break;
|
---|
246 | }
|
---|
247 |
|
---|
248 | LogFlowFunc((": %s opened successfully\n", pszDevicePath));
|
---|
249 |
|
---|
250 | struct usb_device_info UsbDevInfo;
|
---|
251 | RT_ZERO(UsbDevInfo);
|
---|
252 |
|
---|
253 | rc = ioctl(FileUsb, USB_GET_DEVICEINFO, &UsbDevInfo);
|
---|
254 | if (rc < 0)
|
---|
255 | {
|
---|
256 | LogFlowFunc((": Error querying device info rc=%Rrc\n", RTErrConvertFromErrno(errno)));
|
---|
257 | close(FileUsb);
|
---|
258 | RTStrFree(pszDevicePath);
|
---|
259 | break;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /* Filter out hubs */
|
---|
263 | if (UsbDevInfo.udi_class != 0x09)
|
---|
264 | {
|
---|
265 | PUSBDEVICE pDevice = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
|
---|
266 | if (!pDevice)
|
---|
267 | {
|
---|
268 | close(FileUsb);
|
---|
269 | RTStrFree(pszDevicePath);
|
---|
270 | break;
|
---|
271 | }
|
---|
272 |
|
---|
273 | pDevice->enmState = USBDEVICESTATE_UNUSED;
|
---|
274 | pDevice->bBus = UsbDevInfo.udi_bus;
|
---|
275 | pDevice->bDeviceClass = UsbDevInfo.udi_class;
|
---|
276 | pDevice->bDeviceSubClass = UsbDevInfo.udi_subclass;
|
---|
277 | pDevice->bDeviceProtocol = UsbDevInfo.udi_protocol;
|
---|
278 | pDevice->bNumConfigurations = UsbDevInfo.udi_config_no;
|
---|
279 | pDevice->idVendor = UsbDevInfo.udi_vendorNo;
|
---|
280 | pDevice->idProduct = UsbDevInfo.udi_productNo;
|
---|
281 | pDevice->bDevNum = UsbDevInfo.udi_index;
|
---|
282 |
|
---|
283 | switch (UsbDevInfo.udi_speed)
|
---|
284 | {
|
---|
285 | case USB_SPEED_LOW:
|
---|
286 | pDevice->enmSpeed = USBDEVICESPEED_LOW;
|
---|
287 | break;
|
---|
288 | case USB_SPEED_FULL:
|
---|
289 | pDevice->enmSpeed = USBDEVICESPEED_FULL;
|
---|
290 | break;
|
---|
291 | case USB_SPEED_HIGH:
|
---|
292 | pDevice->enmSpeed = USBDEVICESPEED_HIGH;
|
---|
293 | break;
|
---|
294 | case USB_SPEED_SUPER:
|
---|
295 | case USB_SPEED_VARIABLE:
|
---|
296 | default:
|
---|
297 | pDevice->enmSpeed = USBDEVICESPEED_UNKNOWN;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (UsbDevInfo.udi_vendor[0] != '\0')
|
---|
301 | {
|
---|
302 | pDevice->pszManufacturer = RTStrDupN(UsbDevInfo.udi_vendor, sizeof(UsbDevInfo.udi_vendor));
|
---|
303 | USBLibPurgeEncoding(pDevice->pszManufacturer);
|
---|
304 | }
|
---|
305 |
|
---|
306 | if (UsbDevInfo.udi_product[0] != '\0')
|
---|
307 | {
|
---|
308 | pDevice->pszProduct = RTStrDupN(UsbDevInfo.udi_product, sizeof(UsbDevInfo.udi_product));
|
---|
309 | USBLibPurgeEncoding(pDevice->pszProduct);
|
---|
310 | }
|
---|
311 |
|
---|
312 | if (UsbDevInfo.udi_serial[0] != '\0')
|
---|
313 | {
|
---|
314 | pDevice->pszSerialNumber = RTStrDupN(UsbDevInfo.udi_serial, sizeof(UsbDevInfo.udi_serial));
|
---|
315 | USBLibPurgeEncoding(pDevice->pszSerialNumber);
|
---|
316 | pDevice->u64SerialHash = USBLibHashSerial(pDevice->pszSerialNumber);
|
---|
317 | }
|
---|
318 | rc = ioctl(FileUsb, USB_GET_PLUGTIME, &PlugTime);
|
---|
319 | if (rc == 0)
|
---|
320 | pDevice->u64SerialHash += PlugTime;
|
---|
321 |
|
---|
322 | pDevice->pszAddress = RTStrDup(pszDevicePath);
|
---|
323 | pDevice->pszBackend = RTStrDup("host");
|
---|
324 | pDevice->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
325 |
|
---|
326 | usbLogDevice(pDevice);
|
---|
327 |
|
---|
328 | pDevice->pNext = pDevices;
|
---|
329 | if (pDevices)
|
---|
330 | pDevices->pPrev = pDevice;
|
---|
331 | pDevices = pDevice;
|
---|
332 | }
|
---|
333 | close(FileUsb);
|
---|
334 | RTStrFree(pszDevicePath);
|
---|
335 | iAddr++;
|
---|
336 | }
|
---|
337 |
|
---|
338 | return pDevices;
|
---|
339 | }
|
---|
340 |
|
---|