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