VirtualBox

source: vbox/trunk/src/VBox/Main/linux/USBGetDevices.cpp@ 34941

Last change on this file since 34941 was 34716, checked in by vboxsync, 14 years ago

Main/linux/USB: more thorough checks for the different USB probing methods

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette