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