1 | /* $Id: USBGetDevices.cpp 51093 2014-04-17 07:14:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Linux host USB device enumeration.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 |
|
---|
23 | #include "USBGetDevices.h"
|
---|
24 |
|
---|
25 | #include <VBox/err.h>
|
---|
26 | #include <VBox/usb.h>
|
---|
27 | #include <VBox/usblib.h>
|
---|
28 |
|
---|
29 | #include <iprt/linux/sysfs.h>
|
---|
30 | #include <iprt/cdefs.h>
|
---|
31 | #include <iprt/ctype.h>
|
---|
32 | #include <iprt/dir.h>
|
---|
33 | #include <iprt/env.h>
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/fs.h>
|
---|
36 | #include <iprt/log.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/param.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include "vector.h"
|
---|
42 |
|
---|
43 | #ifdef VBOX_WITH_LINUX_COMPILER_H
|
---|
44 | # include <linux/compiler.h>
|
---|
45 | #endif
|
---|
46 | #include <linux/usbdevice_fs.h>
|
---|
47 |
|
---|
48 | #include <sys/types.h>
|
---|
49 | #include <sys/stat.h>
|
---|
50 | #include <sys/vfs.h>
|
---|
51 |
|
---|
52 | #include <dirent.h>
|
---|
53 | #include <dlfcn.h>
|
---|
54 | #include <errno.h>
|
---|
55 | #include <fcntl.h>
|
---|
56 | #include <stdio.h>
|
---|
57 | #include <string.h>
|
---|
58 | #include <unistd.h>
|
---|
59 |
|
---|
60 | /*******************************************************************************
|
---|
61 | * Structures and Typedefs *
|
---|
62 | *******************************************************************************/
|
---|
63 | /** Suffix translation. */
|
---|
64 | typedef struct USBSUFF
|
---|
65 | {
|
---|
66 | char szSuff[4];
|
---|
67 | unsigned cchSuff;
|
---|
68 | unsigned uMul;
|
---|
69 | unsigned uDiv;
|
---|
70 | } USBSUFF, *PUSBSUFF;
|
---|
71 | typedef const USBSUFF *PCUSBSUFF;
|
---|
72 |
|
---|
73 | /** Structure describing a host USB device */
|
---|
74 | typedef struct USBDeviceInfo
|
---|
75 | {
|
---|
76 | /** The device node of the device. */
|
---|
77 | char *mDevice;
|
---|
78 | /** The system identifier of the device. Specific to the probing
|
---|
79 | * method. */
|
---|
80 | char *mSysfsPath;
|
---|
81 | /** List of interfaces as sysfs paths */
|
---|
82 | VECTOR_PTR(char *) mvecpszInterfaces;
|
---|
83 | } USBDeviceInfo;
|
---|
84 |
|
---|
85 | /*******************************************************************************
|
---|
86 | * Global Variables *
|
---|
87 | *******************************************************************************/
|
---|
88 | /**
|
---|
89 | * Suffixes for the endpoint polling interval.
|
---|
90 | */
|
---|
91 | static const USBSUFF s_aIntervalSuff[] =
|
---|
92 | {
|
---|
93 | { "ms", 2, 1, 0 },
|
---|
94 | { "us", 2, 1, 1000 },
|
---|
95 | { "ns", 2, 1, 1000000 },
|
---|
96 | { "s", 1, 1000, 0 },
|
---|
97 | { "", 0, 0, 0 } /* term */
|
---|
98 | };
|
---|
99 |
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * "reads" the number suffix. It's more like validating it and
|
---|
103 | * skipping the necessary number of chars.
|
---|
104 | */
|
---|
105 | static int usbReadSkipSuffix(char **ppszNext)
|
---|
106 | {
|
---|
107 | char *pszNext = *ppszNext;
|
---|
108 | if (!RT_C_IS_SPACE(*pszNext) && *pszNext)
|
---|
109 | {
|
---|
110 | /* skip unit */
|
---|
111 | if (pszNext[0] == 'm' && pszNext[1] == 's')
|
---|
112 | pszNext += 2;
|
---|
113 | else if (pszNext[0] == 'm' && pszNext[1] == 'A')
|
---|
114 | pszNext += 2;
|
---|
115 |
|
---|
116 | /* skip parenthesis */
|
---|
117 | if (*pszNext == '(')
|
---|
118 | {
|
---|
119 | pszNext = strchr(pszNext, ')');
|
---|
120 | if (!pszNext++)
|
---|
121 | {
|
---|
122 | AssertMsgFailed(("*ppszNext=%s\n", *ppszNext));
|
---|
123 | return VERR_PARSE_ERROR;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /* blank or end of the line. */
|
---|
128 | if (!RT_C_IS_SPACE(*pszNext) && *pszNext)
|
---|
129 | {
|
---|
130 | AssertMsgFailed(("pszNext=%s\n", pszNext));
|
---|
131 | return VERR_PARSE_ERROR;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* it's ok. */
|
---|
135 | *ppszNext = pszNext;
|
---|
136 | }
|
---|
137 |
|
---|
138 | return VINF_SUCCESS;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Reads a USB number returning the number and the position of the next character to parse.
|
---|
144 | */
|
---|
145 | static int usbReadNum(const char *pszValue, unsigned uBase, uint32_t u32Mask, PCUSBSUFF paSuffs, void *pvNum, char **ppszNext)
|
---|
146 | {
|
---|
147 | /*
|
---|
148 | * Initialize return value to zero and strip leading spaces.
|
---|
149 | */
|
---|
150 | switch (u32Mask)
|
---|
151 | {
|
---|
152 | case 0xff: *(uint8_t *)pvNum = 0; break;
|
---|
153 | case 0xffff: *(uint16_t *)pvNum = 0; break;
|
---|
154 | case 0xffffffff: *(uint32_t *)pvNum = 0; break;
|
---|
155 | }
|
---|
156 | pszValue = RTStrStripL(pszValue);
|
---|
157 | if (*pszValue)
|
---|
158 | {
|
---|
159 | /*
|
---|
160 | * Try convert the number.
|
---|
161 | */
|
---|
162 | char *pszNext;
|
---|
163 | uint32_t u32 = 0;
|
---|
164 | RTStrToUInt32Ex(pszValue, &pszNext, uBase, &u32);
|
---|
165 | if (pszNext == pszValue)
|
---|
166 | {
|
---|
167 | AssertMsgFailed(("pszValue=%d\n", pszValue));
|
---|
168 | return VERR_NO_DATA;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Check the range.
|
---|
173 | */
|
---|
174 | if (u32 & ~u32Mask)
|
---|
175 | {
|
---|
176 | AssertMsgFailed(("pszValue=%d u32=%#x lMask=%#x\n", pszValue, u32, u32Mask));
|
---|
177 | return VERR_OUT_OF_RANGE;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Validate and skip stuff following the number.
|
---|
182 | */
|
---|
183 | if (paSuffs)
|
---|
184 | {
|
---|
185 | if (!RT_C_IS_SPACE(*pszNext) && *pszNext)
|
---|
186 | {
|
---|
187 | for (PCUSBSUFF pSuff = paSuffs; pSuff->szSuff[0]; pSuff++)
|
---|
188 | {
|
---|
189 | if ( !strncmp(pSuff->szSuff, pszNext, pSuff->cchSuff)
|
---|
190 | && (!pszNext[pSuff->cchSuff] || RT_C_IS_SPACE(pszNext[pSuff->cchSuff])))
|
---|
191 | {
|
---|
192 | if (pSuff->uDiv)
|
---|
193 | u32 /= pSuff->uDiv;
|
---|
194 | else
|
---|
195 | u32 *= pSuff->uMul;
|
---|
196 | break;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | int rc = usbReadSkipSuffix(&pszNext);
|
---|
204 | if (RT_FAILURE(rc))
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 |
|
---|
208 | *ppszNext = pszNext;
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Set the value.
|
---|
212 | */
|
---|
213 | switch (u32Mask)
|
---|
214 | {
|
---|
215 | case 0xff: *(uint8_t *)pvNum = (uint8_t)u32; break;
|
---|
216 | case 0xffff: *(uint16_t *)pvNum = (uint16_t)u32; break;
|
---|
217 | case 0xffffffff: *(uint32_t *)pvNum = (uint32_t)u32; break;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | return VINF_SUCCESS;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | static int usbRead8(const char *pszValue, unsigned uBase, uint8_t *pu8, char **ppszNext)
|
---|
225 | {
|
---|
226 | return usbReadNum(pszValue, uBase, 0xff, NULL, pu8, ppszNext);
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | static int usbRead16(const char *pszValue, unsigned uBase, uint16_t *pu16, char **ppszNext)
|
---|
231 | {
|
---|
232 | return usbReadNum(pszValue, uBase, 0xffff, NULL, pu16, ppszNext);
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | #if 0
|
---|
237 | static int usbRead16Suff(const char *pszValue, unsigned uBase, PCUSBSUFF paSuffs, uint16_t *pu16, char **ppszNext)
|
---|
238 | {
|
---|
239 | return usbReadNum(pszValue, uBase, 0xffff, paSuffs, pu16, ppszNext);
|
---|
240 | }
|
---|
241 | #endif
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Reads a USB BCD number returning the number and the position of the next character to parse.
|
---|
246 | * The returned number contains the integer part in the high byte and the decimal part in the low byte.
|
---|
247 | */
|
---|
248 | static int usbReadBCD(const char *pszValue, unsigned uBase, uint16_t *pu16, char **ppszNext)
|
---|
249 | {
|
---|
250 | /*
|
---|
251 | * Initialize return value to zero and strip leading spaces.
|
---|
252 | */
|
---|
253 | *pu16 = 0;
|
---|
254 | pszValue = RTStrStripL(pszValue);
|
---|
255 | if (*pszValue)
|
---|
256 | {
|
---|
257 | /*
|
---|
258 | * Try convert the number.
|
---|
259 | */
|
---|
260 | /* integer part */
|
---|
261 | char *pszNext;
|
---|
262 | uint32_t u32Int = 0;
|
---|
263 | RTStrToUInt32Ex(pszValue, &pszNext, uBase, &u32Int);
|
---|
264 | if (pszNext == pszValue)
|
---|
265 | {
|
---|
266 | AssertMsgFailed(("pszValue=%s\n", pszValue));
|
---|
267 | return VERR_NO_DATA;
|
---|
268 | }
|
---|
269 | if (u32Int & ~0xff)
|
---|
270 | {
|
---|
271 | AssertMsgFailed(("pszValue=%s u32Int=%#x (int)\n", pszValue, u32Int));
|
---|
272 | return VERR_OUT_OF_RANGE;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* skip dot and read decimal part */
|
---|
276 | if (*pszNext != '.')
|
---|
277 | {
|
---|
278 | AssertMsgFailed(("pszValue=%s pszNext=%s (int)\n", pszValue, pszNext));
|
---|
279 | return VERR_PARSE_ERROR;
|
---|
280 | }
|
---|
281 | char *pszValue2 = RTStrStripL(pszNext + 1);
|
---|
282 | uint32_t u32Dec = 0;
|
---|
283 | RTStrToUInt32Ex(pszValue2, &pszNext, uBase, &u32Dec);
|
---|
284 | if (pszNext == pszValue)
|
---|
285 | {
|
---|
286 | AssertMsgFailed(("pszValue=%s\n", pszValue));
|
---|
287 | return VERR_NO_DATA;
|
---|
288 | }
|
---|
289 | if (u32Dec & ~0xff)
|
---|
290 | {
|
---|
291 | AssertMsgFailed(("pszValue=%s u32Dec=%#x\n", pszValue, u32Dec));
|
---|
292 | return VERR_OUT_OF_RANGE;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Validate and skip stuff following the number.
|
---|
297 | */
|
---|
298 | int rc = usbReadSkipSuffix(&pszNext);
|
---|
299 | if (RT_FAILURE(rc))
|
---|
300 | return rc;
|
---|
301 | *ppszNext = pszNext;
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Set the value.
|
---|
305 | */
|
---|
306 | *pu16 = (uint16_t)u32Int << 8 | (uint16_t)u32Dec;
|
---|
307 | }
|
---|
308 | return VINF_SUCCESS;
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Reads a string, i.e. allocates memory and copies it.
|
---|
314 | *
|
---|
315 | * We assume that a string is Utf8 and if that's not the case
|
---|
316 | * (pre-2.6.32-kernels used Latin-1, but so few devices return non-ASCII that
|
---|
317 | * this usually goes unnoticed) then we mercilessly force it to be so.
|
---|
318 | */
|
---|
319 | static int usbReadStr(const char *pszValue, const char **ppsz)
|
---|
320 | {
|
---|
321 | char *psz;
|
---|
322 |
|
---|
323 | if (*ppsz)
|
---|
324 | RTStrFree((char *)*ppsz);
|
---|
325 | psz = RTStrDup(pszValue);
|
---|
326 | if (psz)
|
---|
327 | {
|
---|
328 | RTStrPurgeEncoding(psz);
|
---|
329 | *ppsz = psz;
|
---|
330 | return VINF_SUCCESS;
|
---|
331 | }
|
---|
332 | return VERR_NO_MEMORY;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Skips the current property.
|
---|
338 | */
|
---|
339 | static char *usbReadSkip(char *pszValue)
|
---|
340 | {
|
---|
341 | char *psz = strchr(pszValue, '=');
|
---|
342 | if (psz)
|
---|
343 | psz = strchr(psz + 1, '=');
|
---|
344 | if (!psz)
|
---|
345 | return strchr(pszValue, '\0');
|
---|
346 | while (psz > pszValue && !RT_C_IS_SPACE(psz[-1]))
|
---|
347 | psz--;
|
---|
348 | Assert(psz > pszValue);
|
---|
349 | return psz;
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Determine the USB speed.
|
---|
355 | */
|
---|
356 | static int usbReadSpeed(const char *pszValue, USBDEVICESPEED *pSpd, char **ppszNext)
|
---|
357 | {
|
---|
358 | pszValue = RTStrStripL(pszValue);
|
---|
359 | /* verified with Linux 2.4.0 ... Linux 2.6.25 */
|
---|
360 | if (!strncmp(pszValue, RT_STR_TUPLE("1.5")))
|
---|
361 | *pSpd = USBDEVICESPEED_LOW;
|
---|
362 | else if (!strncmp(pszValue, RT_STR_TUPLE("12 ")))
|
---|
363 | *pSpd = USBDEVICESPEED_FULL;
|
---|
364 | else if (!strncmp(pszValue, RT_STR_TUPLE("480")))
|
---|
365 | *pSpd = USBDEVICESPEED_HIGH;
|
---|
366 | else
|
---|
367 | *pSpd = USBDEVICESPEED_UNKNOWN;
|
---|
368 | while (pszValue[0] != '\0' && !RT_C_IS_SPACE(pszValue[0]))
|
---|
369 | pszValue++;
|
---|
370 | *ppszNext = (char *)pszValue;
|
---|
371 | return VINF_SUCCESS;
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * Compare a prefix and returns pointer to the char following it if it matches.
|
---|
377 | */
|
---|
378 | static char *usbPrefix(char *psz, const char *pszPref, size_t cchPref)
|
---|
379 | {
|
---|
380 | if (strncmp(psz, pszPref, cchPref))
|
---|
381 | return NULL;
|
---|
382 | return psz + cchPref;
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Does some extra checks to improve the detected device state.
|
---|
388 | *
|
---|
389 | * We cannot distinguish between USED_BY_HOST_CAPTURABLE and
|
---|
390 | * USED_BY_GUEST, HELD_BY_PROXY all that well and it shouldn't be
|
---|
391 | * necessary either.
|
---|
392 | *
|
---|
393 | * We will however, distinguish between the device we have permissions
|
---|
394 | * to open and those we don't. This is necessary for two reasons.
|
---|
395 | *
|
---|
396 | * Firstly, because it's futile to even attempt opening a device which we
|
---|
397 | * don't have access to, it only serves to confuse the user. (That said,
|
---|
398 | * it might also be a bit confusing for the user to see that a USB device
|
---|
399 | * is grayed out with no further explanation, and no way of generating an
|
---|
400 | * error hinting at why this is the case.)
|
---|
401 | *
|
---|
402 | * Secondly and more importantly, we're racing against udevd with respect
|
---|
403 | * to permissions and group settings on newly plugged devices. When we
|
---|
404 | * detect a new device that we cannot access we will poll on it for a few
|
---|
405 | * seconds to give udevd time to fix it. The polling is actually triggered
|
---|
406 | * in the 'new device' case in the compare loop.
|
---|
407 | *
|
---|
408 | * The USBDEVICESTATE_USED_BY_HOST state is only used for this no-access
|
---|
409 | * case, while USBDEVICESTATE_UNSUPPORTED is only used in the 'hub' case.
|
---|
410 | * When it's neither of these, we set USBDEVICESTATE_UNUSED or
|
---|
411 | * USBDEVICESTATE_USED_BY_HOST_CAPTURABLE depending on whether there is
|
---|
412 | * a driver associated with any of the interfaces.
|
---|
413 | *
|
---|
414 | * All except the access check and a special idVendor == 0 precaution
|
---|
415 | * is handled at parse time.
|
---|
416 | *
|
---|
417 | * @returns The adjusted state.
|
---|
418 | * @param pDevice The device.
|
---|
419 | */
|
---|
420 | static USBDEVICESTATE usbDeterminState(PCUSBDEVICE pDevice)
|
---|
421 | {
|
---|
422 | /*
|
---|
423 | * If it's already flagged as unsupported, there is nothing to do.
|
---|
424 | */
|
---|
425 | USBDEVICESTATE enmState = pDevice->enmState;
|
---|
426 | if (enmState == USBDEVICESTATE_UNSUPPORTED)
|
---|
427 | return USBDEVICESTATE_UNSUPPORTED;
|
---|
428 |
|
---|
429 | /*
|
---|
430 | * Root hubs and similar doesn't have any vendor id, just
|
---|
431 | * refuse these device.
|
---|
432 | */
|
---|
433 | if (!pDevice->idVendor)
|
---|
434 | return USBDEVICESTATE_UNSUPPORTED;
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Check if we've got access to the device, if we haven't flag
|
---|
438 | * it as used-by-host.
|
---|
439 | */
|
---|
440 | #ifndef VBOX_USB_WITH_SYSFS
|
---|
441 | const char *pszAddress = pDevice->pszAddress;
|
---|
442 | #else
|
---|
443 | if (pDevice->pszAddress == NULL)
|
---|
444 | /* We can't do much with the device without an address. */
|
---|
445 | return USBDEVICESTATE_UNSUPPORTED;
|
---|
446 | const char *pszAddress = strstr(pDevice->pszAddress, "//device:");
|
---|
447 | pszAddress = pszAddress != NULL
|
---|
448 | ? pszAddress + sizeof("//device:") - 1
|
---|
449 | : pDevice->pszAddress;
|
---|
450 | #endif
|
---|
451 | if ( access(pszAddress, R_OK | W_OK) != 0
|
---|
452 | && errno == EACCES)
|
---|
453 | return USBDEVICESTATE_USED_BY_HOST;
|
---|
454 |
|
---|
455 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
456 | /**
|
---|
457 | * @todo Check that any other essential fields are present and mark as
|
---|
458 | * invalid if not. Particularly to catch the case where the device was
|
---|
459 | * unplugged while we were reading in its properties.
|
---|
460 | */
|
---|
461 | #endif
|
---|
462 |
|
---|
463 | return enmState;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /** Just a worker for USBProxyServiceLinux::getDevices that avoids some code duplication. */
|
---|
468 | static int addDeviceToChain(PUSBDEVICE pDev, PUSBDEVICE *ppFirst, PUSBDEVICE **pppNext, const char *pcszUsbfsRoot,
|
---|
469 | bool testfs, int rc)
|
---|
470 | {
|
---|
471 | /* usbDeterminState requires the address. */
|
---|
472 | PUSBDEVICE pDevNew = (PUSBDEVICE)RTMemDup(pDev, sizeof(*pDev));
|
---|
473 | if (pDevNew)
|
---|
474 | {
|
---|
475 | RTStrAPrintf((char **)&pDevNew->pszAddress, "%s/%03d/%03d", pcszUsbfsRoot, pDevNew->bBus, pDevNew->bDevNum);
|
---|
476 | if (pDevNew->pszAddress)
|
---|
477 | {
|
---|
478 | pDevNew->enmState = usbDeterminState(pDevNew);
|
---|
479 | if (pDevNew->enmState != USBDEVICESTATE_UNSUPPORTED || testfs)
|
---|
480 | {
|
---|
481 | if (*pppNext)
|
---|
482 | **pppNext = pDevNew;
|
---|
483 | else
|
---|
484 | *ppFirst = pDevNew;
|
---|
485 | *pppNext = &pDevNew->pNext;
|
---|
486 | }
|
---|
487 | else
|
---|
488 | deviceFree(pDevNew);
|
---|
489 | }
|
---|
490 | else
|
---|
491 | {
|
---|
492 | deviceFree(pDevNew);
|
---|
493 | rc = VERR_NO_MEMORY;
|
---|
494 | }
|
---|
495 | }
|
---|
496 | else
|
---|
497 | {
|
---|
498 | rc = VERR_NO_MEMORY;
|
---|
499 | deviceFreeMembers(pDev);
|
---|
500 | }
|
---|
501 |
|
---|
502 | return rc;
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | static int openDevicesFile(const char *pcszUsbfsRoot, FILE **ppFile)
|
---|
507 | {
|
---|
508 | char *pszPath;
|
---|
509 | FILE *pFile;
|
---|
510 | RTStrAPrintf(&pszPath, "%s/devices", pcszUsbfsRoot);
|
---|
511 | if (!pszPath)
|
---|
512 | return VERR_NO_MEMORY;
|
---|
513 | pFile = fopen(pszPath, "r");
|
---|
514 | RTStrFree(pszPath);
|
---|
515 | if (!pFile)
|
---|
516 | return RTErrConvertFromErrno(errno);
|
---|
517 | *ppFile = pFile;
|
---|
518 | return VINF_SUCCESS;
|
---|
519 | }
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * USBProxyService::getDevices() implementation for usbfs. The @a testfs flag
|
---|
523 | * tells the function to return information about unsupported devices as well.
|
---|
524 | * This is used as a sanity test to check that a devices file is really what
|
---|
525 | * we expect.
|
---|
526 | */
|
---|
527 | static PUSBDEVICE getDevicesFromUsbfs(const char *pcszUsbfsRoot, bool testfs)
|
---|
528 | {
|
---|
529 | PUSBDEVICE pFirst = NULL;
|
---|
530 | FILE *pFile = NULL;
|
---|
531 | int rc;
|
---|
532 | rc = openDevicesFile(pcszUsbfsRoot, &pFile);
|
---|
533 | if (RT_SUCCESS(rc))
|
---|
534 | {
|
---|
535 | PUSBDEVICE *ppNext = NULL;
|
---|
536 | int cHits = 0;
|
---|
537 | char szLine[1024];
|
---|
538 | USBDEVICE Dev;
|
---|
539 | RT_ZERO(Dev);
|
---|
540 | Dev.enmState = USBDEVICESTATE_UNUSED;
|
---|
541 |
|
---|
542 | /* Set close on exit and hope no one is racing us. */
|
---|
543 | rc = fcntl(fileno(pFile), F_SETFD, FD_CLOEXEC) >= 0
|
---|
544 | ? VINF_SUCCESS
|
---|
545 | : RTErrConvertFromErrno(errno);
|
---|
546 | while ( RT_SUCCESS(rc)
|
---|
547 | && fgets(szLine, sizeof(szLine), pFile))
|
---|
548 | {
|
---|
549 | char *psz;
|
---|
550 | char *pszValue;
|
---|
551 |
|
---|
552 | /* validate and remove the trailing newline. */
|
---|
553 | psz = strchr(szLine, '\0');
|
---|
554 | if (psz[-1] != '\n' && !feof(pFile))
|
---|
555 | {
|
---|
556 | AssertMsgFailed(("Line too long. (cch=%d)\n", strlen(szLine)));
|
---|
557 | continue;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /* strip */
|
---|
561 | psz = RTStrStrip(szLine);
|
---|
562 | if (!*psz)
|
---|
563 | continue;
|
---|
564 |
|
---|
565 | /*
|
---|
566 | * Interpret the line.
|
---|
567 | * (Ordered by normal occurrence.)
|
---|
568 | */
|
---|
569 | char ch = psz[0];
|
---|
570 | if (psz[1] != ':')
|
---|
571 | continue;
|
---|
572 | psz = RTStrStripL(psz + 3);
|
---|
573 | #define PREFIX(str) ( (pszValue = usbPrefix(psz, str, sizeof(str) - 1)) != NULL )
|
---|
574 | switch (ch)
|
---|
575 | {
|
---|
576 | /*
|
---|
577 | * T: Bus=dd Lev=dd Prnt=dd Port=dd Cnt=dd Dev#=ddd Spd=ddd MxCh=dd
|
---|
578 | * | | | | | | | | |__MaxChildren
|
---|
579 | * | | | | | | | |__Device Speed in Mbps
|
---|
580 | * | | | | | | |__DeviceNumber
|
---|
581 | * | | | | | |__Count of devices at this level
|
---|
582 | * | | | | |__Connector/Port on Parent for this device
|
---|
583 | * | | | |__Parent DeviceNumber
|
---|
584 | * | | |__Level in topology for this bus
|
---|
585 | * | |__Bus number
|
---|
586 | * |__Topology info tag
|
---|
587 | */
|
---|
588 | case 'T':
|
---|
589 | /* add */
|
---|
590 | AssertMsg(cHits >= 3 || cHits == 0, ("cHits=%d\n", cHits));
|
---|
591 | if (cHits >= 3)
|
---|
592 | rc = addDeviceToChain(&Dev, &pFirst, &ppNext, pcszUsbfsRoot, testfs, rc);
|
---|
593 | else
|
---|
594 | deviceFreeMembers(&Dev);
|
---|
595 |
|
---|
596 | /* Reset device state */
|
---|
597 | RT_ZERO(Dev);
|
---|
598 | Dev.enmState = USBDEVICESTATE_UNUSED;
|
---|
599 | cHits = 1;
|
---|
600 |
|
---|
601 | /* parse the line. */
|
---|
602 | while (*psz && RT_SUCCESS(rc))
|
---|
603 | {
|
---|
604 | if (PREFIX("Bus="))
|
---|
605 | rc = usbRead8(pszValue, 10, &Dev.bBus, &psz);
|
---|
606 | else if (PREFIX("Port="))
|
---|
607 | rc = usbRead8(pszValue, 10, &Dev.bPort, &psz);
|
---|
608 | else if (PREFIX("Spd="))
|
---|
609 | rc = usbReadSpeed(pszValue, &Dev.enmSpeed, &psz);
|
---|
610 | else if (PREFIX("Dev#="))
|
---|
611 | rc = usbRead8(pszValue, 10, &Dev.bDevNum, &psz);
|
---|
612 | else
|
---|
613 | psz = usbReadSkip(psz);
|
---|
614 | psz = RTStrStripL(psz);
|
---|
615 | }
|
---|
616 | break;
|
---|
617 |
|
---|
618 | /*
|
---|
619 | * Bandwidth info:
|
---|
620 | * B: Alloc=ddd/ddd us (xx%), #Int=ddd, #Iso=ddd
|
---|
621 | * | | | |__Number of isochronous requests
|
---|
622 | * | | |__Number of interrupt requests
|
---|
623 | * | |__Total Bandwidth allocated to this bus
|
---|
624 | * |__Bandwidth info tag
|
---|
625 | */
|
---|
626 | case 'B':
|
---|
627 | break;
|
---|
628 |
|
---|
629 | /*
|
---|
630 | * D: Ver=x.xx Cls=xx(sssss) Sub=xx Prot=xx MxPS=dd #Cfgs=dd
|
---|
631 | * | | | | | | |__NumberConfigurations
|
---|
632 | * | | | | | |__MaxPacketSize of Default Endpoint
|
---|
633 | * | | | | |__DeviceProtocol
|
---|
634 | * | | | |__DeviceSubClass
|
---|
635 | * | | |__DeviceClass
|
---|
636 | * | |__Device USB version
|
---|
637 | * |__Device info tag #1
|
---|
638 | */
|
---|
639 | case 'D':
|
---|
640 | while (*psz && RT_SUCCESS(rc))
|
---|
641 | {
|
---|
642 | if (PREFIX("Ver="))
|
---|
643 | rc = usbReadBCD(pszValue, 16, &Dev.bcdUSB, &psz);
|
---|
644 | else if (PREFIX("Cls="))
|
---|
645 | {
|
---|
646 | rc = usbRead8(pszValue, 16, &Dev.bDeviceClass, &psz);
|
---|
647 | if (RT_SUCCESS(rc) && Dev.bDeviceClass == 9 /* HUB */)
|
---|
648 | Dev.enmState = USBDEVICESTATE_UNSUPPORTED;
|
---|
649 | }
|
---|
650 | else if (PREFIX("Sub="))
|
---|
651 | rc = usbRead8(pszValue, 16, &Dev.bDeviceSubClass, &psz);
|
---|
652 | else if (PREFIX("Prot="))
|
---|
653 | rc = usbRead8(pszValue, 16, &Dev.bDeviceProtocol, &psz);
|
---|
654 | //else if (PREFIX("MxPS="))
|
---|
655 | // rc = usbRead16(pszValue, 10, &Dev.wMaxPacketSize, &psz);
|
---|
656 | else if (PREFIX("#Cfgs="))
|
---|
657 | rc = usbRead8(pszValue, 10, &Dev.bNumConfigurations, &psz);
|
---|
658 | else
|
---|
659 | psz = usbReadSkip(psz);
|
---|
660 | psz = RTStrStripL(psz);
|
---|
661 | }
|
---|
662 | cHits++;
|
---|
663 | break;
|
---|
664 |
|
---|
665 | /*
|
---|
666 | * P: Vendor=xxxx ProdID=xxxx Rev=xx.xx
|
---|
667 | * | | | |__Product revision number
|
---|
668 | * | | |__Product ID code
|
---|
669 | * | |__Vendor ID code
|
---|
670 | * |__Device info tag #2
|
---|
671 | */
|
---|
672 | case 'P':
|
---|
673 | while (*psz && RT_SUCCESS(rc))
|
---|
674 | {
|
---|
675 | if (PREFIX("Vendor="))
|
---|
676 | rc = usbRead16(pszValue, 16, &Dev.idVendor, &psz);
|
---|
677 | else if (PREFIX("ProdID="))
|
---|
678 | rc = usbRead16(pszValue, 16, &Dev.idProduct, &psz);
|
---|
679 | else if (PREFIX("Rev="))
|
---|
680 | rc = usbReadBCD(pszValue, 16, &Dev.bcdDevice, &psz);
|
---|
681 | else
|
---|
682 | psz = usbReadSkip(psz);
|
---|
683 | psz = RTStrStripL(psz);
|
---|
684 | }
|
---|
685 | cHits++;
|
---|
686 | break;
|
---|
687 |
|
---|
688 | /*
|
---|
689 | * String.
|
---|
690 | */
|
---|
691 | case 'S':
|
---|
692 | if (PREFIX("Manufacturer="))
|
---|
693 | rc = usbReadStr(pszValue, &Dev.pszManufacturer);
|
---|
694 | else if (PREFIX("Product="))
|
---|
695 | rc = usbReadStr(pszValue, &Dev.pszProduct);
|
---|
696 | else if (PREFIX("SerialNumber="))
|
---|
697 | {
|
---|
698 | rc = usbReadStr(pszValue, &Dev.pszSerialNumber);
|
---|
699 | if (RT_SUCCESS(rc))
|
---|
700 | Dev.u64SerialHash = USBLibHashSerial(pszValue);
|
---|
701 | }
|
---|
702 | break;
|
---|
703 |
|
---|
704 | /*
|
---|
705 | * C:* #Ifs=dd Cfg#=dd Atr=xx MPwr=dddmA
|
---|
706 | * | | | | | |__MaxPower in mA
|
---|
707 | * | | | | |__Attributes
|
---|
708 | * | | | |__ConfiguratioNumber
|
---|
709 | * | | |__NumberOfInterfaces
|
---|
710 | * | |__ "*" indicates the active configuration (others are " ")
|
---|
711 | * |__Config info tag
|
---|
712 | */
|
---|
713 | case 'C':
|
---|
714 | break;
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * I: If#=dd Alt=dd #EPs=dd Cls=xx(sssss) Sub=xx Prot=xx Driver=ssss
|
---|
718 | * | | | | | | | |__Driver name
|
---|
719 | * | | | | | | | or "(none)"
|
---|
720 | * | | | | | | |__InterfaceProtocol
|
---|
721 | * | | | | | |__InterfaceSubClass
|
---|
722 | * | | | | |__InterfaceClass
|
---|
723 | * | | | |__NumberOfEndpoints
|
---|
724 | * | | |__AlternateSettingNumber
|
---|
725 | * | |__InterfaceNumber
|
---|
726 | * |__Interface info tag
|
---|
727 | */
|
---|
728 | case 'I':
|
---|
729 | {
|
---|
730 | /* Check for thing we don't support. */
|
---|
731 | while (*psz && RT_SUCCESS(rc))
|
---|
732 | {
|
---|
733 | if (PREFIX("Driver="))
|
---|
734 | {
|
---|
735 | const char *pszDriver = NULL;
|
---|
736 | rc = usbReadStr(pszValue, &pszDriver);
|
---|
737 | if ( !pszDriver
|
---|
738 | || !*pszDriver
|
---|
739 | || !strcmp(pszDriver, "(none)")
|
---|
740 | || !strcmp(pszDriver, "(no driver)"))
|
---|
741 | /* no driver */;
|
---|
742 | else if (!strcmp(pszDriver, "hub"))
|
---|
743 | Dev.enmState = USBDEVICESTATE_UNSUPPORTED;
|
---|
744 | else if (Dev.enmState == USBDEVICESTATE_UNUSED)
|
---|
745 | Dev.enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
746 | RTStrFree((char *)pszDriver);
|
---|
747 | break; /* last attrib */
|
---|
748 | }
|
---|
749 | else if (PREFIX("Cls="))
|
---|
750 | {
|
---|
751 | uint8_t bInterfaceClass;
|
---|
752 | rc = usbRead8(pszValue, 16, &bInterfaceClass, &psz);
|
---|
753 | if (RT_SUCCESS(rc) && bInterfaceClass == 9 /* HUB */)
|
---|
754 | Dev.enmState = USBDEVICESTATE_UNSUPPORTED;
|
---|
755 | }
|
---|
756 | else
|
---|
757 | psz = usbReadSkip(psz);
|
---|
758 | psz = RTStrStripL(psz);
|
---|
759 | }
|
---|
760 | break;
|
---|
761 | }
|
---|
762 |
|
---|
763 |
|
---|
764 | /*
|
---|
765 | * E: Ad=xx(s) Atr=xx(ssss) MxPS=dddd Ivl=dddms
|
---|
766 | * | | | | |__Interval (max) between transfers
|
---|
767 | * | | | |__EndpointMaxPacketSize
|
---|
768 | * | | |__Attributes(EndpointType)
|
---|
769 | * | |__EndpointAddress(I=In,O=Out)
|
---|
770 | * |__Endpoint info tag
|
---|
771 | */
|
---|
772 | case 'E':
|
---|
773 | break;
|
---|
774 |
|
---|
775 | }
|
---|
776 | #undef PREFIX
|
---|
777 | } /* parse loop */
|
---|
778 | fclose(pFile);
|
---|
779 |
|
---|
780 | /*
|
---|
781 | * Add the current entry.
|
---|
782 | */
|
---|
783 | AssertMsg(cHits >= 3 || cHits == 0, ("cHits=%d\n", cHits));
|
---|
784 | if (cHits >= 3)
|
---|
785 | rc = addDeviceToChain(&Dev, &pFirst, &ppNext, pcszUsbfsRoot, testfs, rc);
|
---|
786 |
|
---|
787 | /*
|
---|
788 | * Success?
|
---|
789 | */
|
---|
790 | if (RT_FAILURE(rc))
|
---|
791 | {
|
---|
792 | while (pFirst)
|
---|
793 | {
|
---|
794 | PUSBDEVICE pFree = pFirst;
|
---|
795 | pFirst = pFirst->pNext;
|
---|
796 | deviceFree(pFree);
|
---|
797 | }
|
---|
798 | }
|
---|
799 | }
|
---|
800 | if (RT_FAILURE(rc))
|
---|
801 | LogFlow(("USBProxyServiceLinux::getDevices: rc=%Rrc\n", rc));
|
---|
802 | return pFirst;
|
---|
803 | }
|
---|
804 |
|
---|
805 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
806 |
|
---|
807 | static void USBDevInfoCleanup(USBDeviceInfo *pSelf)
|
---|
808 | {
|
---|
809 | RTStrFree(pSelf->mDevice);
|
---|
810 | RTStrFree(pSelf->mSysfsPath);
|
---|
811 | pSelf->mDevice = pSelf->mSysfsPath = NULL;
|
---|
812 | VEC_CLEANUP_PTR(&pSelf->mvecpszInterfaces);
|
---|
813 | }
|
---|
814 |
|
---|
815 | static int USBDevInfoInit(USBDeviceInfo *pSelf, const char *aDevice,
|
---|
816 | const char *aSystemID)
|
---|
817 | {
|
---|
818 | pSelf->mDevice = aDevice ? RTStrDup(aDevice) : NULL;
|
---|
819 | pSelf->mSysfsPath = aSystemID ? RTStrDup(aSystemID) : NULL;
|
---|
820 | VEC_INIT_PTR(&pSelf->mvecpszInterfaces, char *, RTStrFree);
|
---|
821 | if ((aDevice && !pSelf->mDevice) || (aSystemID && ! pSelf->mSysfsPath))
|
---|
822 | {
|
---|
823 | USBDevInfoCleanup(pSelf);
|
---|
824 | return 0;
|
---|
825 | }
|
---|
826 | return 1;
|
---|
827 | }
|
---|
828 |
|
---|
829 | #define USBDEVICE_MAJOR 189
|
---|
830 |
|
---|
831 | /** Calculate the bus (a.k.a root hub) number of a USB device from it's sysfs
|
---|
832 | * path. sysfs nodes representing root hubs have file names of the form
|
---|
833 | * usb<n>, where n is the bus number; other devices start with that number.
|
---|
834 | * See [http://www.linux-usb.org/FAQ.html#i6] and
|
---|
835 | * [http://www.kernel.org/doc/Documentation/usb/proc_usb_info.txt] for
|
---|
836 | * equivalent information about usbfs.
|
---|
837 | * @returns a bus number greater than 0 on success or 0 on failure.
|
---|
838 | */
|
---|
839 | static unsigned usbGetBusFromSysfsPath(const char *pcszPath)
|
---|
840 | {
|
---|
841 | const char *pcszFile = strrchr(pcszPath, '/');
|
---|
842 | if (!pcszFile)
|
---|
843 | return 0;
|
---|
844 | unsigned bus = RTStrToUInt32(pcszFile + 1);
|
---|
845 | if ( !bus
|
---|
846 | && pcszFile[1] == 'u' && pcszFile[2] == 's' && pcszFile[3] == 'b')
|
---|
847 | bus = RTStrToUInt32(pcszFile + 4);
|
---|
848 | return bus;
|
---|
849 | }
|
---|
850 |
|
---|
851 | /** Calculate the device number of a USB device. See
|
---|
852 | * drivers/usb/core/hub.c:usb_new_device as of Linux 2.6.20. */
|
---|
853 | static dev_t usbMakeDevNum(unsigned bus, unsigned device)
|
---|
854 | {
|
---|
855 | AssertReturn(bus > 0, 0);
|
---|
856 | AssertReturn(((device - 1) & ~127) == 0, 0);
|
---|
857 | AssertReturn(device > 0, 0);
|
---|
858 | return makedev(USBDEVICE_MAJOR, ((bus - 1) << 7) + device - 1);
|
---|
859 | }
|
---|
860 |
|
---|
861 | /**
|
---|
862 | * If a file @a pcszNode from /sys/bus/usb/devices is a device rather than an
|
---|
863 | * interface add an element for the device to @a pvecDevInfo.
|
---|
864 | */
|
---|
865 | static int addIfDevice(const char *pcszDevicesRoot,
|
---|
866 | const char *pcszNode,
|
---|
867 | VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo)
|
---|
868 | {
|
---|
869 | const char *pcszFile = strrchr(pcszNode, '/');
|
---|
870 | if (!pcszFile)
|
---|
871 | return VERR_INVALID_PARAMETER;
|
---|
872 | if (strchr(pcszFile, ':'))
|
---|
873 | return VINF_SUCCESS;
|
---|
874 | unsigned bus = usbGetBusFromSysfsPath(pcszNode);
|
---|
875 | if (!bus)
|
---|
876 | return VINF_SUCCESS;
|
---|
877 | int device = RTLinuxSysFsReadIntFile(10, "%s/devnum", pcszNode);
|
---|
878 | if (device < 0)
|
---|
879 | return VINF_SUCCESS;
|
---|
880 | dev_t devnum = usbMakeDevNum(bus, device);
|
---|
881 | if (!devnum)
|
---|
882 | return VINF_SUCCESS;
|
---|
883 | char szDevPath[RTPATH_MAX];
|
---|
884 | ssize_t cchDevPath;
|
---|
885 | cchDevPath = RTLinuxCheckDevicePath(devnum, RTFS_TYPE_DEV_CHAR,
|
---|
886 | szDevPath, sizeof(szDevPath),
|
---|
887 | "%s/%.3d/%.3d",
|
---|
888 | pcszDevicesRoot, bus, device);
|
---|
889 | if (cchDevPath < 0)
|
---|
890 | return VINF_SUCCESS;
|
---|
891 |
|
---|
892 | USBDeviceInfo info;
|
---|
893 | if (USBDevInfoInit(&info, szDevPath, pcszNode))
|
---|
894 | if (RT_SUCCESS(VEC_PUSH_BACK_OBJ(pvecDevInfo, USBDeviceInfo,
|
---|
895 | &info)))
|
---|
896 | return VINF_SUCCESS;
|
---|
897 | USBDevInfoCleanup(&info);
|
---|
898 | return VERR_NO_MEMORY;
|
---|
899 | }
|
---|
900 |
|
---|
901 | /** The logic for testing whether a sysfs address corresponds to an
|
---|
902 | * interface of a device. Both must be referenced by their canonical
|
---|
903 | * sysfs paths. This is not tested, as the test requires file-system
|
---|
904 | * interaction. */
|
---|
905 | static bool muiIsAnInterfaceOf(const char *pcszIface, const char *pcszDev)
|
---|
906 | {
|
---|
907 | size_t cchDev = strlen(pcszDev);
|
---|
908 |
|
---|
909 | AssertPtr(pcszIface);
|
---|
910 | AssertPtr(pcszDev);
|
---|
911 | Assert(pcszIface[0] == '/');
|
---|
912 | Assert(pcszDev[0] == '/');
|
---|
913 | Assert(pcszDev[cchDev - 1] != '/');
|
---|
914 | /* If this passes, pcszIface is at least cchDev long */
|
---|
915 | if (strncmp(pcszIface, pcszDev, cchDev))
|
---|
916 | return false;
|
---|
917 | /* If this passes, pcszIface is longer than cchDev */
|
---|
918 | if (pcszIface[cchDev] != '/')
|
---|
919 | return false;
|
---|
920 | /* In sysfs an interface is an immediate subdirectory of the device */
|
---|
921 | if (strchr(pcszIface + cchDev + 1, '/'))
|
---|
922 | return false;
|
---|
923 | /* And it always has a colon in its name */
|
---|
924 | if (!strchr(pcszIface + cchDev + 1, ':'))
|
---|
925 | return false;
|
---|
926 | /* And hopefully we have now elimitated everything else */
|
---|
927 | return true;
|
---|
928 | }
|
---|
929 |
|
---|
930 | #ifdef DEBUG
|
---|
931 | # ifdef __cplusplus
|
---|
932 | /** Unit test the logic in muiIsAnInterfaceOf in debug builds. */
|
---|
933 | class testIsAnInterfaceOf
|
---|
934 | {
|
---|
935 | public:
|
---|
936 | testIsAnInterfaceOf()
|
---|
937 | {
|
---|
938 | Assert(muiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0",
|
---|
939 | "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
|
---|
940 | Assert(!muiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-1",
|
---|
941 | "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
|
---|
942 | Assert(!muiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0/driver",
|
---|
943 | "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
|
---|
944 | }
|
---|
945 | };
|
---|
946 | static testIsAnInterfaceOf testIsAnInterfaceOfInst;
|
---|
947 | # endif /* __cplusplus */
|
---|
948 | #endif /* DEBUG */
|
---|
949 |
|
---|
950 | /**
|
---|
951 | * Tell whether a file in /sys/bus/usb/devices is an interface rather than a
|
---|
952 | * device. To be used with getDeviceInfoFromSysfs().
|
---|
953 | */
|
---|
954 | static int addIfInterfaceOf(const char *pcszNode, USBDeviceInfo *pInfo)
|
---|
955 | {
|
---|
956 | if (!muiIsAnInterfaceOf(pcszNode, pInfo->mSysfsPath))
|
---|
957 | return VINF_SUCCESS;
|
---|
958 | char *pszDup = (char *)RTStrDup(pcszNode);
|
---|
959 | if (pszDup)
|
---|
960 | if (RT_SUCCESS(VEC_PUSH_BACK_PTR(&pInfo->mvecpszInterfaces,
|
---|
961 | char *, pszDup)))
|
---|
962 | return VINF_SUCCESS;
|
---|
963 | RTStrFree(pszDup);
|
---|
964 | return VERR_NO_MEMORY;
|
---|
965 | }
|
---|
966 |
|
---|
967 | /** Helper for readFilePaths(). Adds the entries from the open directory
|
---|
968 | * @a pDir to the vector @a pvecpchDevs using either the full path or the
|
---|
969 | * realpath() and skipping hidden files and files on which realpath() fails. */
|
---|
970 | static int readFilePathsFromDir(const char *pcszPath, DIR *pDir,
|
---|
971 | VECTOR_PTR(char *) *pvecpchDevs)
|
---|
972 | {
|
---|
973 | struct dirent entry, *pResult;
|
---|
974 | int err, rc;
|
---|
975 |
|
---|
976 | for (err = readdir_r(pDir, &entry, &pResult); pResult;
|
---|
977 | err = readdir_r(pDir, &entry, &pResult))
|
---|
978 | {
|
---|
979 | char szPath[RTPATH_MAX + 1], szRealPath[RTPATH_MAX + 1], *pszPath;
|
---|
980 | if (entry.d_name[0] == '.')
|
---|
981 | continue;
|
---|
982 | if (snprintf(szPath, sizeof(szPath), "%s/%s", pcszPath,
|
---|
983 | entry.d_name) < 0)
|
---|
984 | return RTErrConvertFromErrno(errno);
|
---|
985 | if (!realpath(szPath, szRealPath))
|
---|
986 | return RTErrConvertFromErrno(errno);
|
---|
987 | pszPath = RTStrDup(szRealPath);
|
---|
988 | if (!pszPath)
|
---|
989 | return VERR_NO_MEMORY;
|
---|
990 | if (RT_FAILURE(rc = VEC_PUSH_BACK_PTR(pvecpchDevs, char *, pszPath)))
|
---|
991 | return rc;
|
---|
992 | }
|
---|
993 | return RTErrConvertFromErrno(err);
|
---|
994 | }
|
---|
995 |
|
---|
996 | /**
|
---|
997 | * Dump the names of a directory's entries into a vector of char pointers.
|
---|
998 | *
|
---|
999 | * @returns zero on success or (positive) posix error value.
|
---|
1000 | * @param pcszPath the path to dump.
|
---|
1001 | * @param pvecpchDevs an empty vector of char pointers - must be cleaned up
|
---|
1002 | * by the caller even on failure.
|
---|
1003 | * @param withRealPath whether to canonicalise the filename with realpath
|
---|
1004 | */
|
---|
1005 | static int readFilePaths(const char *pcszPath, VECTOR_PTR(char *) *pvecpchDevs)
|
---|
1006 | {
|
---|
1007 | DIR *pDir;
|
---|
1008 | int rc;
|
---|
1009 |
|
---|
1010 | AssertPtrReturn(pvecpchDevs, EINVAL);
|
---|
1011 | AssertReturn(VEC_SIZE_PTR(pvecpchDevs) == 0, EINVAL);
|
---|
1012 | AssertPtrReturn(pcszPath, EINVAL);
|
---|
1013 |
|
---|
1014 | pDir = opendir(pcszPath);
|
---|
1015 | if (!pDir)
|
---|
1016 | return RTErrConvertFromErrno(errno);
|
---|
1017 | rc = readFilePathsFromDir(pcszPath, pDir, pvecpchDevs);
|
---|
1018 | if (closedir(pDir) < 0 && RT_SUCCESS(rc))
|
---|
1019 | rc = RTErrConvertFromErrno(errno);
|
---|
1020 | return rc;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | /**
|
---|
1024 | * Logic for USBSysfsEnumerateHostDevices.
|
---|
1025 | * @param pvecDevInfo vector of device information structures to add device
|
---|
1026 | * information to
|
---|
1027 | * @param pvecpchDevs empty scratch vector which will be freed by the caller,
|
---|
1028 | * to simplify exit logic
|
---|
1029 | */
|
---|
1030 | static int doSysfsEnumerateHostDevices(const char *pcszDevicesRoot,
|
---|
1031 | VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo,
|
---|
1032 | VECTOR_PTR(char *) *pvecpchDevs)
|
---|
1033 | {
|
---|
1034 | char **ppszEntry;
|
---|
1035 | USBDeviceInfo *pInfo;
|
---|
1036 | int rc;
|
---|
1037 |
|
---|
1038 | AssertPtrReturn(pvecDevInfo, VERR_INVALID_POINTER);
|
---|
1039 | LogFlowFunc (("pvecDevInfo=%p\n", pvecDevInfo));
|
---|
1040 |
|
---|
1041 | rc = readFilePaths("/sys/bus/usb/devices", pvecpchDevs);
|
---|
1042 | if (RT_FAILURE(rc))
|
---|
1043 | return rc;
|
---|
1044 | VEC_FOR_EACH(pvecpchDevs, char *, ppszEntry)
|
---|
1045 | if (RT_FAILURE(rc = addIfDevice(pcszDevicesRoot, *ppszEntry,
|
---|
1046 | pvecDevInfo)))
|
---|
1047 | return rc;
|
---|
1048 | VEC_FOR_EACH(pvecDevInfo, USBDeviceInfo, pInfo)
|
---|
1049 | VEC_FOR_EACH(pvecpchDevs, char *, ppszEntry)
|
---|
1050 | if (RT_FAILURE(rc = addIfInterfaceOf(*ppszEntry, pInfo)))
|
---|
1051 | return rc;
|
---|
1052 | return VINF_SUCCESS;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | static int USBSysfsEnumerateHostDevices(const char *pcszDevicesRoot,
|
---|
1056 | VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo)
|
---|
1057 | {
|
---|
1058 | VECTOR_PTR(char *) vecpchDevs;
|
---|
1059 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
1060 |
|
---|
1061 | AssertReturn(VEC_SIZE_OBJ(pvecDevInfo) == 0, VERR_INVALID_PARAMETER);
|
---|
1062 | LogFlowFunc(("entered\n"));
|
---|
1063 | VEC_INIT_PTR(&vecpchDevs, char *, RTStrFree);
|
---|
1064 | rc = doSysfsEnumerateHostDevices(pcszDevicesRoot, pvecDevInfo,
|
---|
1065 | &vecpchDevs);
|
---|
1066 | VEC_CLEANUP_PTR(&vecpchDevs);
|
---|
1067 | LogFlowFunc(("rc=%Rrc\n", rc));
|
---|
1068 | return rc;
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | /**
|
---|
1072 | * Helper function for extracting the port number on the parent device from
|
---|
1073 | * the sysfs path value.
|
---|
1074 | *
|
---|
1075 | * The sysfs path is a chain of elements separated by forward slashes, and for
|
---|
1076 | * USB devices, the last element in the chain takes the form
|
---|
1077 | * <port>-<port>.[...].<port>[:<config>.<interface>]
|
---|
1078 | * where the first <port> is the port number on the root hub, and the following
|
---|
1079 | * (optional) ones are the port numbers on any other hubs between the device
|
---|
1080 | * and the root hub. The last part (:<config.interface>) is only present for
|
---|
1081 | * interfaces, not for devices. This API should only be called for devices.
|
---|
1082 | * For compatibility with usbfs, which enumerates from zero up, we subtract one
|
---|
1083 | * from the port number.
|
---|
1084 | *
|
---|
1085 | * For root hubs, the last element in the chain takes the form
|
---|
1086 | * usb<hub number>
|
---|
1087 | * and usbfs always returns port number zero.
|
---|
1088 | *
|
---|
1089 | * @returns VBox status. pu8Port is set on success.
|
---|
1090 | * @param pszPath The sysfs path to parse.
|
---|
1091 | * @param pu8Port Where to store the port number.
|
---|
1092 | */
|
---|
1093 | static int usbGetPortFromSysfsPath(const char *pszPath, uint8_t *pu8Port)
|
---|
1094 | {
|
---|
1095 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
1096 | AssertPtrReturn(pu8Port, VERR_INVALID_POINTER);
|
---|
1097 |
|
---|
1098 | /*
|
---|
1099 | * This should not be possible until we get PCs with USB as their primary bus.
|
---|
1100 | * Note: We don't assert this, as we don't expect the caller to validate the
|
---|
1101 | * sysfs path.
|
---|
1102 | */
|
---|
1103 | const char *pszLastComp = strrchr(pszPath, '/');
|
---|
1104 | if (!pszLastComp)
|
---|
1105 | {
|
---|
1106 | Log(("usbGetPortFromSysfsPath(%s): failed [1]\n", pszPath));
|
---|
1107 | return VERR_INVALID_PARAMETER;
|
---|
1108 | }
|
---|
1109 | pszLastComp++; /* skip the slash */
|
---|
1110 |
|
---|
1111 | /*
|
---|
1112 | * This API should not be called for interfaces, so the last component
|
---|
1113 | * of the path should not contain a colon. We *do* assert this, as it
|
---|
1114 | * might indicate a caller bug.
|
---|
1115 | */
|
---|
1116 | AssertMsgReturn(strchr(pszLastComp, ':') == NULL, ("%s\n", pszPath), VERR_INVALID_PARAMETER);
|
---|
1117 |
|
---|
1118 | /*
|
---|
1119 | * Look for the start of the last number.
|
---|
1120 | */
|
---|
1121 | const char *pchDash = strrchr(pszLastComp, '-');
|
---|
1122 | const char *pchDot = strrchr(pszLastComp, '.');
|
---|
1123 | if (!pchDash && !pchDot)
|
---|
1124 | {
|
---|
1125 | /* No -/. so it must be a root hub. Check that it's usb<something>. */
|
---|
1126 | if (strncmp(pszLastComp, RT_STR_TUPLE("usb")) != 0)
|
---|
1127 | {
|
---|
1128 | Log(("usbGetPortFromSysfsPath(%s): failed [2]\n", pszPath));
|
---|
1129 | return VERR_INVALID_PARAMETER;
|
---|
1130 | }
|
---|
1131 | return VERR_NOT_SUPPORTED;
|
---|
1132 | }
|
---|
1133 | else
|
---|
1134 | {
|
---|
1135 | const char *pszLastPort = pchDot != NULL
|
---|
1136 | ? pchDot + 1
|
---|
1137 | : pchDash + 1;
|
---|
1138 | int rc = RTStrToUInt8Full(pszLastPort, 10, pu8Port);
|
---|
1139 | if (rc != VINF_SUCCESS)
|
---|
1140 | {
|
---|
1141 | Log(("usbGetPortFromSysfsPath(%s): failed [3], rc=%Rrc\n", pszPath, rc));
|
---|
1142 | return VERR_INVALID_PARAMETER;
|
---|
1143 | }
|
---|
1144 | if (*pu8Port == 0)
|
---|
1145 | {
|
---|
1146 | Log(("usbGetPortFromSysfsPath(%s): failed [4]\n", pszPath));
|
---|
1147 | return VERR_INVALID_PARAMETER;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /* usbfs compatibility, 0-based port number. */
|
---|
1151 | *pu8Port -= 1;
|
---|
1152 | }
|
---|
1153 | return VINF_SUCCESS;
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 |
|
---|
1157 | /**
|
---|
1158 | * Dumps a USBDEVICE structure to the log using LogLevel 3.
|
---|
1159 | * @param pDev The structure to log.
|
---|
1160 | * @todo This is really common code.
|
---|
1161 | */
|
---|
1162 | DECLINLINE(void) usbLogDevice(PUSBDEVICE pDev)
|
---|
1163 | {
|
---|
1164 | NOREF(pDev);
|
---|
1165 |
|
---|
1166 | Log3(("USB device:\n"));
|
---|
1167 | Log3(("Product: %s (%x)\n", pDev->pszProduct, pDev->idProduct));
|
---|
1168 | Log3(("Manufacturer: %s (Vendor ID %x)\n", pDev->pszManufacturer, pDev->idVendor));
|
---|
1169 | Log3(("Serial number: %s (%llx)\n", pDev->pszSerialNumber, pDev->u64SerialHash));
|
---|
1170 | Log3(("Device revision: %d\n", pDev->bcdDevice));
|
---|
1171 | Log3(("Device class: %x\n", pDev->bDeviceClass));
|
---|
1172 | Log3(("Device subclass: %x\n", pDev->bDeviceSubClass));
|
---|
1173 | Log3(("Device protocol: %x\n", pDev->bDeviceProtocol));
|
---|
1174 | Log3(("USB version number: %d\n", pDev->bcdUSB));
|
---|
1175 | Log3(("Device speed: %s\n",
|
---|
1176 | pDev->enmSpeed == USBDEVICESPEED_UNKNOWN ? "unknown"
|
---|
1177 | : pDev->enmSpeed == USBDEVICESPEED_LOW ? "1.5 MBit/s"
|
---|
1178 | : pDev->enmSpeed == USBDEVICESPEED_FULL ? "12 MBit/s"
|
---|
1179 | : pDev->enmSpeed == USBDEVICESPEED_HIGH ? "480 MBit/s"
|
---|
1180 | : pDev->enmSpeed == USBDEVICESPEED_VARIABLE ? "variable"
|
---|
1181 | : "invalid"));
|
---|
1182 | Log3(("Number of configurations: %d\n", pDev->bNumConfigurations));
|
---|
1183 | Log3(("Bus number: %d\n", pDev->bBus));
|
---|
1184 | Log3(("Port number: %d\n", pDev->bPort));
|
---|
1185 | Log3(("Device number: %d\n", pDev->bDevNum));
|
---|
1186 | Log3(("Device state: %s\n",
|
---|
1187 | pDev->enmState == USBDEVICESTATE_UNSUPPORTED ? "unsupported"
|
---|
1188 | : pDev->enmState == USBDEVICESTATE_USED_BY_HOST ? "in use by host"
|
---|
1189 | : pDev->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE ? "in use by host, possibly capturable"
|
---|
1190 | : pDev->enmState == USBDEVICESTATE_UNUSED ? "not in use"
|
---|
1191 | : pDev->enmState == USBDEVICESTATE_HELD_BY_PROXY ? "held by proxy"
|
---|
1192 | : pDev->enmState == USBDEVICESTATE_USED_BY_GUEST ? "used by guest"
|
---|
1193 | : "invalid"));
|
---|
1194 | Log3(("OS device address: %s\n", pDev->pszAddress));
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | /**
|
---|
1198 | * In contrast to usbReadBCD() this function can handle BCD values without
|
---|
1199 | * a decimal separator. This is necessary for parsing bcdDevice.
|
---|
1200 | * @param pszBuf Pointer to the string buffer.
|
---|
1201 | * @param pu15 Pointer to the return value.
|
---|
1202 | * @returns IPRT status code.
|
---|
1203 | */
|
---|
1204 | static int convertSysfsStrToBCD(const char *pszBuf, uint16_t *pu16)
|
---|
1205 | {
|
---|
1206 | char *pszNext;
|
---|
1207 | int32_t i32;
|
---|
1208 |
|
---|
1209 | pszBuf = RTStrStripL(pszBuf);
|
---|
1210 | int rc = RTStrToInt32Ex(pszBuf, &pszNext, 16, &i32);
|
---|
1211 | if ( RT_FAILURE(rc)
|
---|
1212 | || rc == VWRN_NUMBER_TOO_BIG
|
---|
1213 | || i32 < 0)
|
---|
1214 | return VERR_NUMBER_TOO_BIG;
|
---|
1215 | if (*pszNext == '.')
|
---|
1216 | {
|
---|
1217 | if (i32 > 255)
|
---|
1218 | return VERR_NUMBER_TOO_BIG;
|
---|
1219 | int32_t i32Lo;
|
---|
1220 | rc = RTStrToInt32Ex(pszNext+1, &pszNext, 16, &i32Lo);
|
---|
1221 | if ( RT_FAILURE(rc)
|
---|
1222 | || rc == VWRN_NUMBER_TOO_BIG
|
---|
1223 | || i32Lo > 255
|
---|
1224 | || i32Lo < 0)
|
---|
1225 | return VERR_NUMBER_TOO_BIG;
|
---|
1226 | i32 = (i32 << 8) | i32Lo;
|
---|
1227 | }
|
---|
1228 | if ( i32 > 65535
|
---|
1229 | || (*pszNext != '\0' && *pszNext != ' '))
|
---|
1230 | return VERR_NUMBER_TOO_BIG;
|
---|
1231 |
|
---|
1232 | *pu16 = (uint16_t)i32;
|
---|
1233 | return VINF_SUCCESS;
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | #endif /* VBOX_USB_WITH_SYSFS */
|
---|
1237 |
|
---|
1238 | static void fillInDeviceFromSysfs(USBDEVICE *Dev, USBDeviceInfo *pInfo)
|
---|
1239 | {
|
---|
1240 | int rc;
|
---|
1241 | const char *pszSysfsPath = pInfo->mSysfsPath;
|
---|
1242 |
|
---|
1243 | /* Fill in the simple fields */
|
---|
1244 | Dev->enmState = USBDEVICESTATE_UNUSED;
|
---|
1245 | Dev->bBus = usbGetBusFromSysfsPath(pszSysfsPath);
|
---|
1246 | Dev->bDeviceClass = RTLinuxSysFsReadIntFile(16, "%s/bDeviceClass", pszSysfsPath);
|
---|
1247 | Dev->bDeviceSubClass = RTLinuxSysFsReadIntFile(16, "%s/bDeviceSubClass", pszSysfsPath);
|
---|
1248 | Dev->bDeviceProtocol = RTLinuxSysFsReadIntFile(16, "%s/bDeviceProtocol", pszSysfsPath);
|
---|
1249 | Dev->bNumConfigurations = RTLinuxSysFsReadIntFile(10, "%s/bNumConfigurations", pszSysfsPath);
|
---|
1250 | Dev->idVendor = RTLinuxSysFsReadIntFile(16, "%s/idVendor", pszSysfsPath);
|
---|
1251 | Dev->idProduct = RTLinuxSysFsReadIntFile(16, "%s/idProduct", pszSysfsPath);
|
---|
1252 | Dev->bDevNum = RTLinuxSysFsReadIntFile(10, "%s/devnum", pszSysfsPath);
|
---|
1253 |
|
---|
1254 | /* Now deal with the non-numeric bits. */
|
---|
1255 | char szBuf[1024]; /* Should be larger than anything a sane device
|
---|
1256 | * will need, and insane devices can be unsupported
|
---|
1257 | * until further notice. */
|
---|
1258 | ssize_t cchRead;
|
---|
1259 |
|
---|
1260 | /* For simplicity, we just do strcmps on the next one. */
|
---|
1261 | cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/speed",
|
---|
1262 | pszSysfsPath);
|
---|
1263 | if (cchRead <= 0 || (size_t) cchRead == sizeof(szBuf))
|
---|
1264 | Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
|
---|
1265 | else
|
---|
1266 | Dev->enmSpeed = !strcmp(szBuf, "1.5") ? USBDEVICESPEED_LOW
|
---|
1267 | : !strcmp(szBuf, "12") ? USBDEVICESPEED_FULL
|
---|
1268 | : !strcmp(szBuf, "480") ? USBDEVICESPEED_HIGH
|
---|
1269 | : USBDEVICESPEED_UNKNOWN;
|
---|
1270 |
|
---|
1271 | cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/version",
|
---|
1272 | pszSysfsPath);
|
---|
1273 | if (cchRead <= 0 || (size_t) cchRead == sizeof(szBuf))
|
---|
1274 | Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
|
---|
1275 | else
|
---|
1276 | {
|
---|
1277 | rc = convertSysfsStrToBCD(szBuf, &Dev->bcdUSB);
|
---|
1278 | if (RT_FAILURE(rc))
|
---|
1279 | {
|
---|
1280 | Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
|
---|
1281 | Dev->bcdUSB = (uint16_t)-1;
|
---|
1282 | }
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/bcdDevice",
|
---|
1286 | pszSysfsPath);
|
---|
1287 | if (cchRead <= 0 || (size_t) cchRead == sizeof(szBuf))
|
---|
1288 | Dev->bcdDevice = (uint16_t)-1;
|
---|
1289 | else
|
---|
1290 | {
|
---|
1291 | rc = convertSysfsStrToBCD(szBuf, &Dev->bcdDevice);
|
---|
1292 | if (RT_FAILURE(rc))
|
---|
1293 | Dev->bcdDevice = (uint16_t)-1;
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | /* Now do things that need string duplication */
|
---|
1297 | cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/product",
|
---|
1298 | pszSysfsPath);
|
---|
1299 | if (cchRead > 0 && (size_t) cchRead < sizeof(szBuf))
|
---|
1300 | {
|
---|
1301 | RTStrPurgeEncoding(szBuf);
|
---|
1302 | Dev->pszProduct = RTStrDup(szBuf);
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/serial",
|
---|
1306 | pszSysfsPath);
|
---|
1307 | if (cchRead > 0 && (size_t) cchRead < sizeof(szBuf))
|
---|
1308 | {
|
---|
1309 | RTStrPurgeEncoding(szBuf);
|
---|
1310 | Dev->pszSerialNumber = RTStrDup(szBuf);
|
---|
1311 | Dev->u64SerialHash = USBLibHashSerial(szBuf);
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/manufacturer",
|
---|
1315 | pszSysfsPath);
|
---|
1316 | if (cchRead > 0 && (size_t) cchRead < sizeof(szBuf))
|
---|
1317 | {
|
---|
1318 | RTStrPurgeEncoding(szBuf);
|
---|
1319 | Dev->pszManufacturer = RTStrDup(szBuf);
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | /* Work out the port number */
|
---|
1323 | if (RT_FAILURE(usbGetPortFromSysfsPath(pszSysfsPath, &Dev->bPort)))
|
---|
1324 | Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
|
---|
1325 |
|
---|
1326 | /* Check the interfaces to see if we can support the device. */
|
---|
1327 | char **ppszIf;
|
---|
1328 | VEC_FOR_EACH(&pInfo->mvecpszInterfaces, char *, ppszIf)
|
---|
1329 | {
|
---|
1330 | ssize_t cb = RTLinuxSysFsGetLinkDest(szBuf, sizeof(szBuf), "%s/driver",
|
---|
1331 | *ppszIf);
|
---|
1332 | if (cb > 0 && Dev->enmState != USBDEVICESTATE_UNSUPPORTED)
|
---|
1333 | Dev->enmState = (strcmp(szBuf, "hub") == 0)
|
---|
1334 | ? USBDEVICESTATE_UNSUPPORTED
|
---|
1335 | : USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
1336 | if (RTLinuxSysFsReadIntFile(16, "%s/bInterfaceClass",
|
---|
1337 | *ppszIf) == 9 /* hub */)
|
---|
1338 | Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | /* We use a double slash as a separator in the pszAddress field. This is
|
---|
1342 | * alright as the two paths can't contain a slash due to the way we build
|
---|
1343 | * them. */
|
---|
1344 | char *pszAddress = NULL;
|
---|
1345 | RTStrAPrintf(&pszAddress, "sysfs:%s//device:%s", pszSysfsPath,
|
---|
1346 | pInfo->mDevice);
|
---|
1347 | Dev->pszAddress = pszAddress;
|
---|
1348 |
|
---|
1349 | /* Work out from the data collected whether we can support this device. */
|
---|
1350 | Dev->enmState = usbDeterminState(Dev);
|
---|
1351 | usbLogDevice(Dev);
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | /**
|
---|
1355 | * USBProxyService::getDevices() implementation for sysfs.
|
---|
1356 | */
|
---|
1357 | static PUSBDEVICE getDevicesFromSysfs(const char *pcszDevicesRoot, bool testfs)
|
---|
1358 | {
|
---|
1359 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
1360 | /* Add each of the devices found to the chain. */
|
---|
1361 | PUSBDEVICE pFirst = NULL;
|
---|
1362 | PUSBDEVICE pLast = NULL;
|
---|
1363 | VECTOR_OBJ(USBDeviceInfo) vecDevInfo;
|
---|
1364 | USBDeviceInfo *pInfo;
|
---|
1365 | int rc;
|
---|
1366 |
|
---|
1367 | VEC_INIT_OBJ(&vecDevInfo, USBDeviceInfo, USBDevInfoCleanup);
|
---|
1368 | rc = USBSysfsEnumerateHostDevices(pcszDevicesRoot, &vecDevInfo);
|
---|
1369 | if (RT_FAILURE(rc))
|
---|
1370 | return NULL;
|
---|
1371 | VEC_FOR_EACH(&vecDevInfo, USBDeviceInfo, pInfo)
|
---|
1372 | {
|
---|
1373 | USBDEVICE *Dev = (USBDEVICE *)RTMemAllocZ(sizeof(USBDEVICE));
|
---|
1374 | if (!Dev)
|
---|
1375 | rc = VERR_NO_MEMORY;
|
---|
1376 | if (RT_SUCCESS(rc))
|
---|
1377 | {
|
---|
1378 | fillInDeviceFromSysfs(Dev, pInfo);
|
---|
1379 | }
|
---|
1380 | if ( RT_SUCCESS(rc)
|
---|
1381 | && ( Dev->enmState != USBDEVICESTATE_UNSUPPORTED
|
---|
1382 | || testfs)
|
---|
1383 | && Dev->pszAddress != NULL
|
---|
1384 | )
|
---|
1385 | {
|
---|
1386 | if (pLast != NULL)
|
---|
1387 | {
|
---|
1388 | pLast->pNext = Dev;
|
---|
1389 | pLast = pLast->pNext;
|
---|
1390 | }
|
---|
1391 | else
|
---|
1392 | pFirst = pLast = Dev;
|
---|
1393 | }
|
---|
1394 | else
|
---|
1395 | deviceFree(Dev);
|
---|
1396 | if (RT_FAILURE(rc))
|
---|
1397 | break;
|
---|
1398 | }
|
---|
1399 | if (RT_FAILURE(rc))
|
---|
1400 | deviceListFree(&pFirst);
|
---|
1401 |
|
---|
1402 | VEC_CLEANUP_OBJ(&vecDevInfo);
|
---|
1403 | return pFirst;
|
---|
1404 | #else /* !VBOX_USB_WITH_SYSFS */
|
---|
1405 | return NULL;
|
---|
1406 | #endif /* !VBOX_USB_WITH_SYSFS */
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | #ifdef UNIT_TEST
|
---|
1410 | /* Set up mock functions for USBProxyLinuxCheckDeviceRoot - here dlsym and close
|
---|
1411 | * for the inotify presence check. */
|
---|
1412 | static int testInotifyInitGood(void) { return 0; }
|
---|
1413 | static int testInotifyInitBad(void) { return -1; }
|
---|
1414 | static bool s_fHaveInotifyLibC = true;
|
---|
1415 | static bool s_fHaveInotifyKernel = true;
|
---|
1416 |
|
---|
1417 | static void *testDLSym(void *handle, const char *symbol)
|
---|
1418 | {
|
---|
1419 | Assert(handle == RTLD_DEFAULT);
|
---|
1420 | Assert(!RTStrCmp(symbol, "inotify_init"));
|
---|
1421 | if (!s_fHaveInotifyLibC)
|
---|
1422 | return NULL;
|
---|
1423 | if (s_fHaveInotifyKernel)
|
---|
1424 | return (void *)(uintptr_t)testInotifyInitGood;
|
---|
1425 | return (void *)(uintptr_t)testInotifyInitBad;
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | void TestUSBSetInotifyAvailable(bool fHaveInotifyLibC, bool fHaveInotifyKernel)
|
---|
1429 | {
|
---|
1430 | s_fHaveInotifyLibC = fHaveInotifyLibC;
|
---|
1431 | s_fHaveInotifyKernel = fHaveInotifyKernel;
|
---|
1432 | }
|
---|
1433 | # define dlsym testDLSym
|
---|
1434 | # define close(a) do {} while (0)
|
---|
1435 | #endif
|
---|
1436 |
|
---|
1437 | /** Is inotify available and working on this system? This is a requirement
|
---|
1438 | * for using USB with sysfs */
|
---|
1439 | static bool inotifyAvailable(void)
|
---|
1440 | {
|
---|
1441 | int (*inotify_init)(void);
|
---|
1442 |
|
---|
1443 | *(void **)(&inotify_init) = dlsym(RTLD_DEFAULT, "inotify_init");
|
---|
1444 | if (!inotify_init)
|
---|
1445 | return false;
|
---|
1446 | int fd = inotify_init();
|
---|
1447 | if (fd == -1)
|
---|
1448 | return false;
|
---|
1449 | close(fd);
|
---|
1450 | return true;
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | #ifdef UNIT_TEST
|
---|
1454 | # undef dlsym
|
---|
1455 | # undef close
|
---|
1456 | #endif
|
---|
1457 |
|
---|
1458 | #ifdef UNIT_TEST
|
---|
1459 | /** Unit test list of usbfs addresses of connected devices. */
|
---|
1460 | static const char **s_pacszUsbfsDeviceAddresses = NULL;
|
---|
1461 |
|
---|
1462 | static PUSBDEVICE testGetUsbfsDevices(const char *pcszUsbfsRoot, bool testfs)
|
---|
1463 | {
|
---|
1464 | const char **pcsz;
|
---|
1465 | PUSBDEVICE pList = NULL, pTail = NULL;
|
---|
1466 | for (pcsz = s_pacszUsbfsDeviceAddresses; pcsz && *pcsz; ++pcsz)
|
---|
1467 | {
|
---|
1468 | PUSBDEVICE pNext = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
|
---|
1469 | if (pNext)
|
---|
1470 | pNext->pszAddress = RTStrDup(*pcsz);
|
---|
1471 | if (!pNext || !pNext->pszAddress)
|
---|
1472 | {
|
---|
1473 | deviceListFree(&pList);
|
---|
1474 | return NULL;
|
---|
1475 | }
|
---|
1476 | if (pTail)
|
---|
1477 | pTail->pNext = pNext;
|
---|
1478 | else
|
---|
1479 | pList = pNext;
|
---|
1480 | pTail = pNext;
|
---|
1481 | }
|
---|
1482 | return pList;
|
---|
1483 | }
|
---|
1484 | # define getDevicesFromUsbfs testGetUsbfsDevices
|
---|
1485 |
|
---|
1486 | /**
|
---|
1487 | * Specify the list of devices that will appear to be available through
|
---|
1488 | * usbfs during unit testing (of USBProxyLinuxGetDevices)
|
---|
1489 | * @param pacszDeviceAddresses NULL terminated array of usbfs device addresses
|
---|
1490 | */
|
---|
1491 | void TestUSBSetAvailableUsbfsDevices(const char **pacszDeviceAddresses)
|
---|
1492 | {
|
---|
1493 | s_pacszUsbfsDeviceAddresses = pacszDeviceAddresses;
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | /** Unit test list of files reported as accessible by access(3). We only do
|
---|
1497 | * accessible or not accessible. */
|
---|
1498 | static const char **s_pacszAccessibleFiles = NULL;
|
---|
1499 |
|
---|
1500 | static int testAccess(const char *pcszPath, int mode)
|
---|
1501 | {
|
---|
1502 | const char **pcsz;
|
---|
1503 | for (pcsz = s_pacszAccessibleFiles; pcsz && *pcsz; ++pcsz)
|
---|
1504 | if (!RTStrCmp(pcszPath, *pcsz))
|
---|
1505 | return 0;
|
---|
1506 | return -1;
|
---|
1507 | }
|
---|
1508 | # define access testAccess
|
---|
1509 |
|
---|
1510 | /**
|
---|
1511 | * Specify the list of files that access will report as accessible (at present
|
---|
1512 | * we only do accessible or not accessible) during unit testing (of
|
---|
1513 | * USBProxyLinuxGetDevices)
|
---|
1514 | * @param pacszAccessibleFiles NULL terminated array of file paths to be
|
---|
1515 | * reported accessible
|
---|
1516 | */
|
---|
1517 | void TestUSBSetAccessibleFiles(const char **pacszAccessibleFiles)
|
---|
1518 | {
|
---|
1519 | s_pacszAccessibleFiles = pacszAccessibleFiles;
|
---|
1520 | }
|
---|
1521 | #endif
|
---|
1522 |
|
---|
1523 | #ifdef UNIT_TEST
|
---|
1524 | # ifdef UNIT_TEST
|
---|
1525 | /** The path we pretend the usbfs root is located at, or NULL. */
|
---|
1526 | const char *s_pcszTestUsbfsRoot;
|
---|
1527 | /** Should usbfs be accessible to the current user? */
|
---|
1528 | bool s_fTestUsbfsAccessible;
|
---|
1529 | /** The path we pretend the device node tree root is located at, or NULL. */
|
---|
1530 | const char *s_pcszTestDevicesRoot;
|
---|
1531 | /** Should the device node tree be accessible to the current user? */
|
---|
1532 | bool s_fTestDevicesAccessible;
|
---|
1533 | /** The result of the usbfs/inotify-specific init */
|
---|
1534 | int s_rcTestMethodInitResult;
|
---|
1535 | /** The value of the VBOX_USB environment variable. */
|
---|
1536 | const char *s_pcszTestEnvUsb;
|
---|
1537 | /** The value of the VBOX_USB_ROOT environment variable. */
|
---|
1538 | const char *s_pcszTestEnvUsbRoot;
|
---|
1539 | # endif
|
---|
1540 |
|
---|
1541 | /** Select which access methods will be available to the @a init method
|
---|
1542 | * during unit testing, and (hack!) what return code it will see from
|
---|
1543 | * the access method-specific initialisation. */
|
---|
1544 | void TestUSBSetupInit(const char *pcszUsbfsRoot, bool fUsbfsAccessible,
|
---|
1545 | const char *pcszDevicesRoot, bool fDevicesAccessible,
|
---|
1546 | int rcMethodInitResult)
|
---|
1547 | {
|
---|
1548 | s_pcszTestUsbfsRoot = pcszUsbfsRoot;
|
---|
1549 | s_fTestUsbfsAccessible = fUsbfsAccessible;
|
---|
1550 | s_pcszTestDevicesRoot = pcszDevicesRoot;
|
---|
1551 | s_fTestDevicesAccessible = fDevicesAccessible;
|
---|
1552 | s_rcTestMethodInitResult = rcMethodInitResult;
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | /** Specify the environment that the @a init method will see during unit
|
---|
1556 | * testing. */
|
---|
1557 | void TestUSBSetEnv(const char *pcszEnvUsb, const char *pcszEnvUsbRoot)
|
---|
1558 | {
|
---|
1559 | s_pcszTestEnvUsb = pcszEnvUsb;
|
---|
1560 | s_pcszTestEnvUsbRoot = pcszEnvUsbRoot;
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | /* For testing we redefine anything that accesses the outside world to
|
---|
1564 | * return test values. */
|
---|
1565 | # define RTEnvGet(a) \
|
---|
1566 | ( !RTStrCmp(a, "VBOX_USB") ? s_pcszTestEnvUsb \
|
---|
1567 | : !RTStrCmp(a, "VBOX_USB_ROOT") ? s_pcszTestEnvUsbRoot \
|
---|
1568 | : NULL)
|
---|
1569 | # define USBProxyLinuxCheckDeviceRoot(pcszPath, fUseNodes) \
|
---|
1570 | ( ((fUseNodes) && s_fTestDevicesAccessible \
|
---|
1571 | && !RTStrCmp(pcszPath, s_pcszTestDevicesRoot)) \
|
---|
1572 | || (!(fUseNodes) && s_fTestUsbfsAccessible \
|
---|
1573 | && !RTStrCmp(pcszPath, s_pcszTestUsbfsRoot)))
|
---|
1574 | # define RTDirExists(pcszDir) \
|
---|
1575 | ( (pcszDir) \
|
---|
1576 | && ( !RTStrCmp(pcszDir, s_pcszTestDevicesRoot) \
|
---|
1577 | || !RTStrCmp(pcszDir, s_pcszTestUsbfsRoot)))
|
---|
1578 | # define RTFileExists(pcszFile) \
|
---|
1579 | ( (pcszFile) \
|
---|
1580 | && s_pcszTestUsbfsRoot \
|
---|
1581 | && !RTStrNCmp(pcszFile, s_pcszTestUsbfsRoot, strlen(s_pcszTestUsbfsRoot)) \
|
---|
1582 | && !RTStrCmp(pcszFile + strlen(s_pcszTestUsbfsRoot), "/devices"))
|
---|
1583 | #endif
|
---|
1584 |
|
---|
1585 | /**
|
---|
1586 | * Selects the access method that will be used to access USB devices based on
|
---|
1587 | * what is available on the host and what if anything the user has specified
|
---|
1588 | * in the environment.
|
---|
1589 | * @returns iprt status value
|
---|
1590 | * @param pfUsingUsbfsDevices on success this will be set to true if
|
---|
1591 | * the prefered access method is USBFS-like and to
|
---|
1592 | * false if it is sysfs/device node-like
|
---|
1593 | * @param ppcszDevicesRoot on success the root of the tree of USBFS-like
|
---|
1594 | * device nodes will be stored here
|
---|
1595 | */
|
---|
1596 | int USBProxyLinuxChooseMethod(bool *pfUsingUsbfsDevices,
|
---|
1597 | const char **ppcszDevicesRoot)
|
---|
1598 | {
|
---|
1599 | /*
|
---|
1600 | * We have two methods available for getting host USB device data - using
|
---|
1601 | * USBFS and using sysfs. The default choice is sysfs; if that is not
|
---|
1602 | * available we fall back to USBFS.
|
---|
1603 | * In the event of both failing, an appropriate error will be returned.
|
---|
1604 | * The user may also specify a method and root using the VBOX_USB and
|
---|
1605 | * VBOX_USB_ROOT environment variables. In this case we don't check
|
---|
1606 | * the root they provide for validity.
|
---|
1607 | */
|
---|
1608 | bool fUsbfsChosen = false, fSysfsChosen = false;
|
---|
1609 | const char *pcszUsbFromEnv = RTEnvGet("VBOX_USB");
|
---|
1610 | const char *pcszUsbRoot = NULL;
|
---|
1611 | if (pcszUsbFromEnv)
|
---|
1612 | {
|
---|
1613 | bool fValidVBoxUSB = true;
|
---|
1614 |
|
---|
1615 | pcszUsbRoot = RTEnvGet("VBOX_USB_ROOT");
|
---|
1616 | if (!RTStrICmp(pcszUsbFromEnv, "USBFS"))
|
---|
1617 | {
|
---|
1618 | LogRel(("Default USB access method set to \"usbfs\" from environment\n"));
|
---|
1619 | fUsbfsChosen = true;
|
---|
1620 | }
|
---|
1621 | else if (!RTStrICmp(pcszUsbFromEnv, "SYSFS"))
|
---|
1622 | {
|
---|
1623 | LogRel(("Default USB method set to \"sysfs\" from environment\n"));
|
---|
1624 | fSysfsChosen = true;
|
---|
1625 | }
|
---|
1626 | else
|
---|
1627 | {
|
---|
1628 | LogRel(("Invalid VBOX_USB environment variable setting \"%s\"\n",
|
---|
1629 | pcszUsbFromEnv));
|
---|
1630 | fValidVBoxUSB = false;
|
---|
1631 | pcszUsbFromEnv = NULL;
|
---|
1632 | }
|
---|
1633 | if (!fValidVBoxUSB && pcszUsbRoot)
|
---|
1634 | pcszUsbRoot = NULL;
|
---|
1635 | }
|
---|
1636 | if (!pcszUsbRoot)
|
---|
1637 | {
|
---|
1638 | if ( !fUsbfsChosen
|
---|
1639 | && USBProxyLinuxCheckDeviceRoot("/dev/vboxusb", true))
|
---|
1640 | {
|
---|
1641 | fSysfsChosen = true;
|
---|
1642 | pcszUsbRoot = "/dev/vboxusb";
|
---|
1643 | }
|
---|
1644 | else if ( !fSysfsChosen
|
---|
1645 | && USBProxyLinuxCheckDeviceRoot("/proc/bus/usb", false))
|
---|
1646 | {
|
---|
1647 | fUsbfsChosen = true;
|
---|
1648 | pcszUsbRoot = "/proc/bus/usb";
|
---|
1649 | }
|
---|
1650 | }
|
---|
1651 | else if (!USBProxyLinuxCheckDeviceRoot(pcszUsbRoot, fSysfsChosen))
|
---|
1652 | pcszUsbRoot = NULL;
|
---|
1653 | if (pcszUsbRoot)
|
---|
1654 | {
|
---|
1655 | *pfUsingUsbfsDevices = fUsbfsChosen;
|
---|
1656 | *ppcszDevicesRoot = pcszUsbRoot;
|
---|
1657 | return VINF_SUCCESS;
|
---|
1658 | }
|
---|
1659 | /* else */
|
---|
1660 | return pcszUsbFromEnv ? VERR_NOT_FOUND
|
---|
1661 | : RTDirExists("/dev/vboxusb") ? VERR_VUSB_USB_DEVICE_PERMISSION
|
---|
1662 | : RTFileExists("/proc/bus/usb/devices") ? VERR_VUSB_USBFS_PERMISSION
|
---|
1663 | : VERR_NOT_FOUND;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | #ifdef UNIT_TEST
|
---|
1667 | # undef RTEnvGet
|
---|
1668 | # undef USBProxyLinuxCheckDeviceRoot
|
---|
1669 | # undef RTDirExists
|
---|
1670 | # undef RTFileExists
|
---|
1671 | #endif
|
---|
1672 |
|
---|
1673 | /**
|
---|
1674 | * Check whether a USB device tree root is usable
|
---|
1675 | * @param pcszRoot the path to the root of the device tree
|
---|
1676 | * @param fIsDeviceNodes whether this is a device node (or usbfs) tree
|
---|
1677 | * @note returns a pointer into a static array so it will stay valid
|
---|
1678 | */
|
---|
1679 | bool USBProxyLinuxCheckDeviceRoot(const char *pcszRoot, bool fIsDeviceNodes)
|
---|
1680 | {
|
---|
1681 | bool fOK = false;
|
---|
1682 | if (!fIsDeviceNodes) /* usbfs */
|
---|
1683 | {
|
---|
1684 | PUSBDEVICE pDevices;
|
---|
1685 |
|
---|
1686 | if (!access(pcszRoot, R_OK | X_OK))
|
---|
1687 | {
|
---|
1688 | fOK = true;
|
---|
1689 | pDevices = getDevicesFromUsbfs(pcszRoot, true);
|
---|
1690 | if (pDevices)
|
---|
1691 | {
|
---|
1692 | PUSBDEVICE pDevice;
|
---|
1693 |
|
---|
1694 | for (pDevice = pDevices; pDevice && fOK; pDevice = pDevice->pNext)
|
---|
1695 | if (access(pDevice->pszAddress, R_OK | W_OK))
|
---|
1696 | fOK = false;
|
---|
1697 | deviceListFree(&pDevices);
|
---|
1698 | }
|
---|
1699 | }
|
---|
1700 | }
|
---|
1701 | else /* device nodes */
|
---|
1702 | if (inotifyAvailable() && !access(pcszRoot, R_OK | X_OK))
|
---|
1703 | fOK = true;
|
---|
1704 | return fOK;
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | #ifdef UNIT_TEST
|
---|
1708 | # undef getDevicesFromUsbfs
|
---|
1709 | # undef access
|
---|
1710 | #endif
|
---|
1711 |
|
---|
1712 | /**
|
---|
1713 | * Get the list of USB devices supported by the system. Should be freed using
|
---|
1714 | * @a deviceFree or something equivalent.
|
---|
1715 | * @param pcszDevicesRoot the path to the root of the device tree
|
---|
1716 | * @param fUseSysfs whether to use sysfs (or usbfs) for enumeration
|
---|
1717 | */
|
---|
1718 | PUSBDEVICE USBProxyLinuxGetDevices(const char *pcszDevicesRoot,
|
---|
1719 | bool fUseSysfs)
|
---|
1720 | {
|
---|
1721 | if (!fUseSysfs)
|
---|
1722 | return getDevicesFromUsbfs(pcszDevicesRoot, false);
|
---|
1723 | else
|
---|
1724 | return getDevicesFromSysfs(pcszDevicesRoot, false);
|
---|
1725 | }
|
---|