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