VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/freebsd/USBProxyServiceFreeBSD.cpp@ 57548

Last change on this file since 57548 was 57358, checked in by vboxsync, 10 years ago

*: scm cleanup run.

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