VirtualBox

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

Last change on this file since 37952 was 37599, checked in by vboxsync, 13 years ago

Main/USBProxyService: implementation inheritance is not so great that we have to pretend to do it when we are not

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