1 | /* $Id: USBProxyBackendFreeBSD.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Service, FreeBSD Specialization.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_MAIN_USBPROXYBACKEND
|
---|
33 | #include "USBProxyBackend.h"
|
---|
34 | #include "LoggingNew.h"
|
---|
35 |
|
---|
36 | #include <VBox/usb.h>
|
---|
37 | #include <VBox/usblib.h>
|
---|
38 | #include <iprt/errcore.h>
|
---|
39 |
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/alloc.h>
|
---|
42 | #include <iprt/assert.h>
|
---|
43 | #include <iprt/file.h>
|
---|
44 | #include <iprt/errcore.h>
|
---|
45 | #include <iprt/mem.h>
|
---|
46 | #include <iprt/param.h>
|
---|
47 | #include <iprt/path.h>
|
---|
48 | #include <iprt/semaphore.h>
|
---|
49 |
|
---|
50 | #include <stdlib.h>
|
---|
51 | #include <string.h>
|
---|
52 | #include <stdio.h>
|
---|
53 | #include <errno.h>
|
---|
54 | #include <unistd.h>
|
---|
55 | #include <fcntl.h>
|
---|
56 | #include <sys/poll.h>
|
---|
57 | #include <dev/usb/usb.h>
|
---|
58 | #include <dev/usb/usb_ioctl.h>
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Initialize data members.
|
---|
63 | */
|
---|
64 | USBProxyBackendFreeBSD::USBProxyBackendFreeBSD()
|
---|
65 | : USBProxyBackend(), mNotifyEventSem(NIL_RTSEMEVENT)
|
---|
66 | {
|
---|
67 | LogFlowThisFunc(("\n"));
|
---|
68 | }
|
---|
69 |
|
---|
70 | USBProxyBackendFreeBSD::~USBProxyBackendFreeBSD()
|
---|
71 | {
|
---|
72 | LogFlowThisFunc(("\n"));
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Initializes the object (called right after construction).
|
---|
77 | *
|
---|
78 | * @returns S_OK on success and non-fatal failures, some COM error otherwise.
|
---|
79 | */
|
---|
80 | int USBProxyBackendFreeBSD::init(USBProxyService *pUsbProxyService, const com::Utf8Str &strId,
|
---|
81 | const com::Utf8Str &strAddress, bool fLoadingSettings)
|
---|
82 | {
|
---|
83 | USBProxyBackend::init(pUsbProxyService, strId, strAddress, fLoadingSettings);
|
---|
84 |
|
---|
85 | unconst(m_strBackend) = Utf8Str("host");
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Create semaphore.
|
---|
89 | */
|
---|
90 | int vrc = RTSemEventCreate(&mNotifyEventSem);
|
---|
91 | if (RT_FAILURE(vrc))
|
---|
92 | return vrc;
|
---|
93 |
|
---|
94 | /*
|
---|
95 | * Start the poller thread.
|
---|
96 | */
|
---|
97 | start();
|
---|
98 | return VINF_SUCCESS;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Stop all service threads and free the device chain.
|
---|
104 | */
|
---|
105 | void USBProxyBackendFreeBSD::uninit()
|
---|
106 | {
|
---|
107 | LogFlowThisFunc(("\n"));
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Stop the service.
|
---|
111 | */
|
---|
112 | if (isActive())
|
---|
113 | stop();
|
---|
114 |
|
---|
115 | RTSemEventDestroy(mNotifyEventSem);
|
---|
116 | mNotifyEventSem = NULL;
|
---|
117 | USBProxyBackend::uninit();
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | int USBProxyBackendFreeBSD::captureDevice(HostUSBDevice *aDevice)
|
---|
122 | {
|
---|
123 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
124 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
125 |
|
---|
126 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
127 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Don't think we need to do anything when the device is held... fake it.
|
---|
131 | */
|
---|
132 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
|
---|
133 | devLock.release();
|
---|
134 | interruptWait();
|
---|
135 |
|
---|
136 | return VINF_SUCCESS;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | int USBProxyBackendFreeBSD::releaseDevice(HostUSBDevice *aDevice)
|
---|
141 | {
|
---|
142 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
143 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
144 |
|
---|
145 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
146 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * We're not really holding it atm., just fake it.
|
---|
150 | */
|
---|
151 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
|
---|
152 | devLock.release();
|
---|
153 | interruptWait();
|
---|
154 |
|
---|
155 | return VINF_SUCCESS;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | bool USBProxyBackendFreeBSD::isFakeUpdateRequired()
|
---|
160 | {
|
---|
161 | return true;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | int USBProxyBackendFreeBSD::wait(RTMSINTERVAL aMillies)
|
---|
166 | {
|
---|
167 | return RTSemEventWait(mNotifyEventSem, aMillies < 1000 ? 1000 : 5000);
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | int USBProxyBackendFreeBSD::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 |
|
---|
218 | PUSBDEVICE USBProxyBackendFreeBSD::getDevices(void)
|
---|
219 | {
|
---|
220 | PUSBDEVICE pDevices = NULL;
|
---|
221 | int FileUsb = 0;
|
---|
222 | int iBus = 0;
|
---|
223 | int iAddr = 1;
|
---|
224 | int vrc = VINF_SUCCESS;
|
---|
225 | char *pszDevicePath = NULL;
|
---|
226 | uint32_t PlugTime = 0;
|
---|
227 |
|
---|
228 | for (;;)
|
---|
229 | {
|
---|
230 | vrc = RTStrAPrintf(&pszDevicePath, "/dev/%s%d.%d", USB_GENERIC_NAME, iBus, iAddr);
|
---|
231 | if (RT_FAILURE(vrc))
|
---|
232 | break;
|
---|
233 |
|
---|
234 | LogFlowFunc((": Opening %s\n", pszDevicePath));
|
---|
235 |
|
---|
236 | FileUsb = open(pszDevicePath, O_RDONLY);
|
---|
237 | if (FileUsb < 0)
|
---|
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 | RT_ZERO(UsbDevInfo);
|
---|
261 |
|
---|
262 | vrc = ioctl(FileUsb, USB_GET_DEVICEINFO, &UsbDevInfo);
|
---|
263 | if (vrc < 0)
|
---|
264 | {
|
---|
265 | LogFlowFunc((": Error querying device info vrc=%Rrc\n", RTErrConvertFromErrno(errno)));
|
---|
266 | close(FileUsb);
|
---|
267 | RTStrFree(pszDevicePath);
|
---|
268 | break;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* Filter out hubs */
|
---|
272 | if (UsbDevInfo.udi_class != 0x09)
|
---|
273 | {
|
---|
274 | PUSBDEVICE pDevice = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
|
---|
275 | if (!pDevice)
|
---|
276 | {
|
---|
277 | close(FileUsb);
|
---|
278 | RTStrFree(pszDevicePath);
|
---|
279 | break;
|
---|
280 | }
|
---|
281 |
|
---|
282 | pDevice->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
283 | pDevice->bBus = UsbDevInfo.udi_bus;
|
---|
284 | pDevice->bPort = UsbDevInfo.udi_hubport;
|
---|
285 | pDevice->bDeviceClass = UsbDevInfo.udi_class;
|
---|
286 | pDevice->bDeviceSubClass = UsbDevInfo.udi_subclass;
|
---|
287 | pDevice->bDeviceProtocol = UsbDevInfo.udi_protocol;
|
---|
288 | pDevice->bNumConfigurations = UsbDevInfo.udi_config_no;
|
---|
289 | pDevice->idVendor = UsbDevInfo.udi_vendorNo;
|
---|
290 | pDevice->idProduct = UsbDevInfo.udi_productNo;
|
---|
291 | pDevice->bDevNum = UsbDevInfo.udi_index;
|
---|
292 |
|
---|
293 | switch (UsbDevInfo.udi_speed)
|
---|
294 | {
|
---|
295 | case USB_SPEED_LOW:
|
---|
296 | pDevice->enmSpeed = USBDEVICESPEED_LOW;
|
---|
297 | break;
|
---|
298 | case USB_SPEED_FULL:
|
---|
299 | pDevice->enmSpeed = USBDEVICESPEED_FULL;
|
---|
300 | break;
|
---|
301 | case USB_SPEED_HIGH:
|
---|
302 | pDevice->enmSpeed = USBDEVICESPEED_HIGH;
|
---|
303 | break;
|
---|
304 | case USB_SPEED_SUPER:
|
---|
305 | pDevice->enmSpeed = USBDEVICESPEED_SUPER;
|
---|
306 | break;
|
---|
307 | case USB_SPEED_VARIABLE:
|
---|
308 | pDevice->enmSpeed = USBDEVICESPEED_VARIABLE;
|
---|
309 | break;
|
---|
310 | default:
|
---|
311 | pDevice->enmSpeed = USBDEVICESPEED_UNKNOWN;
|
---|
312 | break;
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (UsbDevInfo.udi_vendor[0] != '\0')
|
---|
316 | {
|
---|
317 | USBLibPurgeEncoding(UsbDevInfo.udi_vendor);
|
---|
318 | pDevice->pszManufacturer = RTStrDupN(UsbDevInfo.udi_vendor, sizeof(UsbDevInfo.udi_vendor));
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (UsbDevInfo.udi_product[0] != '\0')
|
---|
322 | {
|
---|
323 | USBLibPurgeEncoding(UsbDevInfo.udi_product);
|
---|
324 | pDevice->pszProduct = RTStrDupN(UsbDevInfo.udi_product, sizeof(UsbDevInfo.udi_product));
|
---|
325 | }
|
---|
326 |
|
---|
327 | if (UsbDevInfo.udi_serial[0] != '\0')
|
---|
328 | {
|
---|
329 | USBLibPurgeEncoding(UsbDevInfo.udi_serial);
|
---|
330 | pDevice->pszSerialNumber = RTStrDupN(UsbDevInfo.udi_serial, sizeof(UsbDevInfo.udi_serial));
|
---|
331 | pDevice->u64SerialHash = USBLibHashSerial(UsbDevInfo.udi_serial);
|
---|
332 | }
|
---|
333 | vrc = ioctl(FileUsb, USB_GET_PLUGTIME, &PlugTime);
|
---|
334 | if (vrc == 0)
|
---|
335 | pDevice->u64SerialHash += PlugTime;
|
---|
336 |
|
---|
337 | pDevice->pszAddress = RTStrDup(pszDevicePath);
|
---|
338 | pDevice->pszBackend = RTStrDup("host");
|
---|
339 |
|
---|
340 | usbLogDevice(pDevice);
|
---|
341 |
|
---|
342 | pDevice->pNext = pDevices;
|
---|
343 | if (pDevices)
|
---|
344 | pDevices->pPrev = pDevice;
|
---|
345 | pDevices = pDevice;
|
---|
346 | }
|
---|
347 | close(FileUsb);
|
---|
348 | RTStrFree(pszDevicePath);
|
---|
349 | iAddr++;
|
---|
350 | }
|
---|
351 |
|
---|
352 | return pDevices;
|
---|
353 | }
|
---|