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