VirtualBox

source: vbox/trunk/src/VBox/Main/freebsd/USBProxyServiceFreeBSD.cpp@ 34575

Last change on this file since 34575 was 34014, checked in by vboxsync, 14 years ago

scm whitespace cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1/* $Id: USBProxyServiceFreeBSD.cpp 34014 2010-11-11 21:34: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 */
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 * 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 */
102USBProxyServiceFreeBSD::~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
117int 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
133int 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
149bool 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 */
160void USBProxyServiceFreeBSD::deviceAdded(ComObjPtr<HostUSBDevice> &aDevice, SessionMachinesList &llOpenedMachines, PUSBDEVICE aUSBDevice)
161{
162 USBProxyService::deviceAdded(aDevice, llOpenedMachines, aUSBDevice);
163}
164
165int USBProxyServiceFreeBSD::wait(RTMSINTERVAL aMillies)
166{
167 return RTSemEventWait(mNotifyEventSem, aMillies < 1000 ? 1000 : 5000);
168}
169
170
171int 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 */
182DECLINLINE(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
217PUSBDEVICE 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 uint32_t PlugTime = 0;
226
227 for (;;)
228 {
229 rc = RTStrAPrintf(&pszDevicePath, "/dev/%s%d.%d", USB_GENERIC_NAME, iBus, iAddr);
230 if (RT_FAILURE(rc))
231 {
232 mLastError = rc;
233 break;
234 }
235
236 LogFlowFunc((": Opening %s\n", pszDevicePath));
237
238 FileUsb = open(pszDevicePath, O_RDONLY);
239 if (FileUsb < 0)
240 {
241 if ( (errno != ENOENT)
242 && (errno != EACCES))
243 mLastError = RTErrConvertFromErrno(errno);
244
245 RTStrFree(pszDevicePath);
246
247 if ((errno == ENOENT) && (iAddr > 1))
248 {
249 iAddr = 1;
250 iBus++;
251 continue;
252 }
253 else if (errno == EACCES)
254 {
255 /* Skip devices without the right permission. */
256 iAddr++;
257 continue;
258 }
259 else
260 break;
261 }
262
263 LogFlowFunc((": %s opened successfully\n", pszDevicePath));
264
265 struct usb_device_info UsbDevInfo;
266 memset(&UsbDevInfo, 0, sizeof(struct usb_device_info));
267
268 rc = ioctl(FileUsb, USB_GET_DEVICEINFO, &UsbDevInfo);
269 if (rc < 0)
270 {
271 LogFlowFunc((": Error querying device info rc=%Rrc\n", RTErrConvertFromErrno(errno)));
272 mLastError = RTErrConvertFromErrno(errno);
273 close(FileUsb);
274 RTStrFree(pszDevicePath);
275 break;
276 }
277
278 /* Filter out hubs */
279 if (UsbDevInfo.udi_class != 0x09)
280 {
281 PUSBDEVICE pDevice = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
282 if (!pDevice)
283 {
284 mLastError = VERR_NO_MEMORY;
285 close(FileUsb);
286 RTStrFree(pszDevicePath);
287 break;
288 }
289
290 pDevice->enmState = USBDEVICESTATE_UNUSED;
291 pDevice->bBus = UsbDevInfo.udi_bus;
292 pDevice->bDeviceClass = UsbDevInfo.udi_class;
293 pDevice->bDeviceSubClass = UsbDevInfo.udi_subclass;
294 pDevice->bDeviceProtocol = UsbDevInfo.udi_protocol;
295 pDevice->bNumConfigurations = UsbDevInfo.udi_config_no;
296 pDevice->idVendor = UsbDevInfo.udi_vendorNo;
297 pDevice->idProduct = UsbDevInfo.udi_productNo;
298 pDevice->bDevNum = UsbDevInfo.udi_index;
299
300 switch (UsbDevInfo.udi_speed)
301 {
302 case USB_SPEED_LOW:
303 pDevice->enmSpeed = USBDEVICESPEED_LOW;
304 break;
305 case USB_SPEED_FULL:
306 pDevice->enmSpeed = USBDEVICESPEED_FULL;
307 break;
308 case USB_SPEED_HIGH:
309 pDevice->enmSpeed = USBDEVICESPEED_HIGH;
310 break;
311 case USB_SPEED_SUPER:
312 case USB_SPEED_VARIABLE:
313 default:
314 pDevice->enmSpeed = USBDEVICESPEED_UNKNOWN;
315 }
316
317 if (UsbDevInfo.udi_vendor[0] != '\0')
318 pDevice->pszManufacturer = RTStrDupN(UsbDevInfo.udi_vendor, sizeof(UsbDevInfo.udi_vendor));
319
320 if (UsbDevInfo.udi_product[0] != '\0')
321 pDevice->pszProduct = RTStrDupN(UsbDevInfo.udi_product, sizeof(UsbDevInfo.udi_product));
322
323 if (UsbDevInfo.udi_serial[0] != '\0')
324 {
325 pDevice->pszSerialNumber = RTStrDupN(UsbDevInfo.udi_serial, sizeof(UsbDevInfo.udi_serial));
326 pDevice->u64SerialHash = USBLibHashSerial(pDevice->pszSerialNumber);
327 }
328 rc = ioctl(FileUsb, USB_GET_PLUGTIME, &PlugTime);
329 if (rc == 0)
330 pDevice->u64SerialHash += PlugTime;
331
332 pDevice->pszAddress = RTStrDup(pszDevicePath);
333 pDevice->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
334
335 usbLogDevice(pDevice);
336
337 pDevice->pNext = pDevices;
338 if (pDevices)
339 pDevices->pPrev = pDevice;
340 pDevices = pDevice;
341 }
342 close(FileUsb);
343 RTStrFree(pszDevicePath);
344 iAddr++;
345 }
346
347 return pDevices;
348}
349
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