VirtualBox

Changeset 60147 in vbox for trunk/src/VBox/Main


Ignore:
Timestamp:
Mar 23, 2016 10:58:52 AM (9 years ago)
Author:
vboxsync
Message:

USBGetDevices.cpp: prefix functions, doxygen brief style (short sentence describing function (single line), optionally followed by detailed description), space between logical blocks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-server/linux/USBGetDevices.cpp

    r60146 r60147  
    55
    66/*
    7  * Copyright (C) 2006-2012 Oracle Corporation
     7 * Copyright (C) 2006-2016 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    102102
    103103/**
    104  * "reads" the number suffix. It's more like validating it and
    105  * skipping the necessary number of chars.
     104 * "reads" the number suffix.
     105 *
     106 * It's more like validating it and skipping the necessary number of chars.
    106107 */
    107108static int usbReadSkipSuffix(char **ppszNext)
     
    188189 * Reads a USB number returning the number and the position of the next character to parse.
    189190 */
    190 static int usbReadNum(const char *pszValue, unsigned uBase, uint32_t u32Mask, PCUSBSUFF paSuffs, void *pvNum, char **ppszNext)
     191static int usbfsReadNum(const char *pszValue, unsigned uBase, uint32_t u32Mask, PCUSBSUFF paSuffs, void *pvNum, char **ppszNext)
    191192{
    192193    /*
     
    246247        else
    247248        {
    248             int rc = usbReadSkipSuffix(&pszNext);
     249            int rc = usbfsReadSkipSuffix(&pszNext);
    249250            if (RT_FAILURE(rc))
    250251                return rc;
     
    267268
    268269
    269 static int usbRead8(const char *pszValue, unsigned uBase, uint8_t *pu8, char **ppszNext)
    270 {
    271     return usbReadNum(pszValue, uBase, 0xff, NULL, pu8, ppszNext);
    272 }
    273 
    274 
    275 static int usbRead16(const char *pszValue, unsigned uBase, uint16_t *pu16, char **ppszNext)
    276 {
    277     return usbReadNum(pszValue, uBase, 0xffff, NULL, pu16, ppszNext);
     270static int usbfsRead8(const char *pszValue, unsigned uBase, uint8_t *pu8, char **ppszNext)
     271{
     272    return usbfsReadNum(pszValue, uBase, 0xff, NULL, pu8, ppszNext);
     273}
     274
     275
     276static int usbfsRead16(const char *pszValue, unsigned uBase, uint16_t *pu16, char **ppszNext)
     277{
     278    return usbfsReadNum(pszValue, uBase, 0xffff, NULL, pu16, ppszNext);
    278279}
    279280
     
    291292 * The returned number contains the integer part in the high byte and the decimal part in the low byte.
    292293 */
    293 static int usbReadBCD(const char *pszValue, unsigned uBase, uint16_t *pu16, char **ppszNext)
     294static int usbfsReadBCD(const char *pszValue, unsigned uBase, uint16_t *pu16, char **ppszNext)
    294295{
    295296    /*
     
    341342         * Validate and skip stuff following the number.
    342343         */
    343         int rc = usbReadSkipSuffix(&pszNext);
     344        int rc = usbfsReadNum(&pszNext);
    344345        if (RT_FAILURE(rc))
    345346            return rc;
     
    362363 * this usually goes unnoticed) then we mercilessly force it to be so.
    363364 */
    364 static int usbReadStr(const char *pszValue, const char **ppsz)
     365static int usbfsReadStr(const char *pszValue, const char **ppsz)
    365366{
    366367    char *psz;
     
    382383 * Skips the current property.
    383384 */
    384 static char *usbReadSkip(char *pszValue)
     385static char *usbfsReadSkip(char *pszValue)
    385386{
    386387    char *psz = strchr(pszValue, '=');
     
    399400 * Determine the USB speed.
    400401 */
    401 static int usbReadSpeed(const char *pszValue, USBDEVICESPEED *pSpd, char **ppszNext)
     402static int usbfsReadSpeed(const char *pszValue, USBDEVICESPEED *pSpd, char **ppszNext)
    402403{
    403404    pszValue = RTStrStripL(pszValue);
     
    423424 * Compare a prefix and returns pointer to the char following it if it matches.
    424425 */
    425 static char *usbPrefix(char *psz, const char *pszPref, size_t cchPref)
     426static char *usbfsPrefix(char *psz, const char *pszPref, size_t cchPref)
    426427{
    427428    if (strncmp(psz, pszPref, cchPref))
     
    513514
    514515/** Just a worker for USBProxyServiceLinux::getDevices that avoids some code duplication. */
    515 static int addDeviceToChain(PUSBDEVICE pDev, PUSBDEVICE *ppFirst, PUSBDEVICE **pppNext, const char *pcszUsbfsRoot,
    516                             bool testfs, int rc)
     516static int usbfsAddDeviceToChain(PUSBDEVICE pDev, PUSBDEVICE *ppFirst, PUSBDEVICE **pppNext, const char *pcszUsbfsRoot,
     517                                 bool testfs, int rc)
    517518{
    518519    /* usbDeterminState requires the address. */
     
    551552
    552553
    553 static int openDevicesFile(const char *pcszUsbfsRoot, FILE **ppFile)
     554static int usbfsOpenDevicesFile(const char *pcszUsbfsRoot, FILE **ppFile)
    554555{
    555556    char *pszPath;
     
    566567}
    567568
     569
    568570/**
    569571 * USBProxyService::getDevices() implementation for usbfs.  The @a testfs flag
     
    572574 * we expect.
    573575 */
    574 static PUSBDEVICE getDevicesFromUsbfs(const char *pcszUsbfsRoot, bool testfs)
     576static PUSBDEVICE usbfsGetDevices(const char *pcszUsbfsRoot, bool testfs)
    575577{
    576578    PUSBDEVICE pFirst = NULL;
    577579    FILE *pFile = NULL;
    578580    int rc;
    579     rc = openDevicesFile(pcszUsbfsRoot, &pFile);
     581    rc = usbfsOpenDevicesFile(pcszUsbfsRoot, &pFile);
    580582    if (RT_SUCCESS(rc))
    581583    {
     
    618620                continue;
    619621            psz = RTStrStripL(psz + 3);
    620 #define PREFIX(str) ( (pszValue = usbPrefix(psz, str, sizeof(str) - 1)) != NULL )
     622#define PREFIX(str) ( (pszValue = usbfsPrefix(psz, str, sizeof(str) - 1)) != NULL )
    621623            switch (ch)
    622624            {
     
    637639                    AssertMsg(cHits >= 3 || cHits == 0, ("cHits=%d\n", cHits));
    638640                    if (cHits >= 3)
    639                         rc = addDeviceToChain(&Dev, &pFirst, &ppNext, pcszUsbfsRoot, testfs, rc);
     641                        rc = usbfsAddDeviceToChain(&Dev, &pFirst, &ppNext, pcszUsbfsRoot, testfs, rc);
    640642                    else
    641643                        deviceFreeMembers(&Dev);
     
    650652                    {
    651653                        if (PREFIX("Bus="))
    652                             rc = usbRead8(pszValue, 10, &Dev.bBus, &psz);
     654                            rc = usbfsRead8(pszValue, 10, &Dev.bBus, &psz);
    653655                        else if (PREFIX("Port="))
    654                             rc = usbRead8(pszValue, 10, &Dev.bPort, &psz);
     656                            rc = usbfsRead8(pszValue, 10, &Dev.bPort, &psz);
    655657                        else if (PREFIX("Spd="))
    656                             rc = usbReadSpeed(pszValue, &Dev.enmSpeed, &psz);
     658                            rc = usbfsReadSpeed(pszValue, &Dev.enmSpeed, &psz);
    657659                        else if (PREFIX("Dev#="))
    658                             rc = usbRead8(pszValue, 10, &Dev.bDevNum, &psz);
     660                            rc = usbfsRead8(pszValue, 10, &Dev.bDevNum, &psz);
    659661                        else
    660                             psz = usbReadSkip(psz);
     662                            psz = usbfsReadSkip(psz);
    661663                        psz = RTStrStripL(psz);
    662664                    }
     
    688690                    {
    689691                        if (PREFIX("Ver="))
    690                             rc = usbReadBCD(pszValue, 16, &Dev.bcdUSB, &psz);
     692                            rc = usbfsReadBCD(pszValue, 16, &Dev.bcdUSB, &psz);
    691693                        else if (PREFIX("Cls="))
    692694                        {
    693                             rc = usbRead8(pszValue, 16, &Dev.bDeviceClass, &psz);
     695                            rc = usbfsRead8(pszValue, 16, &Dev.bDeviceClass, &psz);
    694696                            if (RT_SUCCESS(rc) && Dev.bDeviceClass == 9 /* HUB */)
    695697                                Dev.enmState = USBDEVICESTATE_UNSUPPORTED;
    696698                        }
    697699                        else if (PREFIX("Sub="))
    698                             rc = usbRead8(pszValue, 16, &Dev.bDeviceSubClass, &psz);
     700                            rc = usbfsRead8(pszValue, 16, &Dev.bDeviceSubClass, &psz);
    699701                        else if (PREFIX("Prot="))
    700                             rc = usbRead8(pszValue, 16, &Dev.bDeviceProtocol, &psz);
     702                            rc = usbfsRead8(pszValue, 16, &Dev.bDeviceProtocol, &psz);
    701703                        //else if (PREFIX("MxPS="))
    702704                        //    rc = usbRead16(pszValue, 10, &Dev.wMaxPacketSize, &psz);
    703705                        else if (PREFIX("#Cfgs="))
    704                             rc = usbRead8(pszValue, 10, &Dev.bNumConfigurations, &psz);
     706                            rc = usbfsRead8(pszValue, 10, &Dev.bNumConfigurations, &psz);
    705707                        else
    706                             psz = usbReadSkip(psz);
     708                            psz = usbfsReadSkip(psz);
    707709                        psz = RTStrStripL(psz);
    708710                    }
     
    721723                    {
    722724                        if (PREFIX("Vendor="))
    723                             rc = usbRead16(pszValue, 16, &Dev.idVendor, &psz);
     725                            rc = usbfsRead16(pszValue, 16, &Dev.idVendor, &psz);
    724726                        else if (PREFIX("ProdID="))
    725                             rc = usbRead16(pszValue, 16, &Dev.idProduct, &psz);
     727                            rc = usbfsRead16(pszValue, 16, &Dev.idProduct, &psz);
    726728                        else if (PREFIX("Rev="))
    727                             rc = usbReadBCD(pszValue, 16, &Dev.bcdDevice, &psz);
     729                            rc = usbfsReadBCD(pszValue, 16, &Dev.bcdDevice, &psz);
    728730                        else
    729                             psz = usbReadSkip(psz);
     731                            psz = usbfsReadSkip(psz);
    730732                        psz = RTStrStripL(psz);
    731733                    }
     
    738740                case 'S':
    739741                    if (PREFIX("Manufacturer="))
    740                         rc = usbReadStr(pszValue, &Dev.pszManufacturer);
     742                        rc = usbfsReadStr(pszValue, &Dev.pszManufacturer);
    741743                    else if (PREFIX("Product="))
    742                         rc = usbReadStr(pszValue, &Dev.pszProduct);
     744                        rc = usbfsReadStr(pszValue, &Dev.pszProduct);
    743745                    else if (PREFIX("SerialNumber="))
    744746                    {
    745                         rc = usbReadStr(pszValue, &Dev.pszSerialNumber);
     747                        rc = usbfsReadStr(pszValue, &Dev.pszSerialNumber);
    746748                        if (RT_SUCCESS(rc))
    747749                            Dev.u64SerialHash = USBLibHashSerial(pszValue);
     
    781783                        {
    782784                            const char *pszDriver = NULL;
    783                             rc = usbReadStr(pszValue, &pszDriver);
     785                            rc = usbfsReadStr(pszValue, &pszDriver);
    784786                            if (   !pszDriver
    785787                                || !*pszDriver
     
    797799                        {
    798800                            uint8_t bInterfaceClass;
    799                             rc = usbRead8(pszValue, 16, &bInterfaceClass, &psz);
     801                            rc = usbfsRead8(pszValue, 16, &bInterfaceClass, &psz);
    800802                            if (RT_SUCCESS(rc) && bInterfaceClass == 9 /* HUB */)
    801803                                Dev.enmState = USBDEVICESTATE_UNSUPPORTED;
    802804                        }
    803805                        else
    804                             psz = usbReadSkip(psz);
     806                            psz = usbfsReadSkip(psz);
    805807                        psz = RTStrStripL(psz);
    806808                    }
     
    830832        AssertMsg(cHits >= 3 || cHits == 0, ("cHits=%d\n", cHits));
    831833        if (cHits >= 3)
    832             rc = addDeviceToChain(&Dev, &pFirst, &ppNext, pcszUsbfsRoot, testfs, rc);
     834            rc = usbfsAddDeviceToChain(&Dev, &pFirst, &ppNext, pcszUsbfsRoot, testfs, rc);
    833835
    834836        /*
     
    852854#ifdef VBOX_USB_WITH_SYSFS
    853855
    854 static void USBDevInfoCleanup(USBDeviceInfo *pSelf)
     856static void usbsysfsCleanupDevInfo(USBDeviceInfo *pSelf)
    855857{
    856858    RTStrFree(pSelf->mDevice);
     
    860862}
    861863
    862 static int USBDevInfoInit(USBDeviceInfo *pSelf, const char *aDevice,
    863                   const char *aSystemID)
     864
     865static int usbsysfsInitDevInfo(USBDeviceInfo *pSelf, const char *aDevice, const char *aSystemID)
    864866{
    865867    pSelf->mDevice = aDevice ? RTStrDup(aDevice) : NULL;
     
    868870    if ((aDevice && !pSelf->mDevice) || (aSystemID && ! pSelf->mSysfsPath))
    869871    {
    870         USBDevInfoCleanup(pSelf);
     872        usbsysfsCleanupDevInfo(pSelf);
    871873        return 0;
    872874    }
     
    876878#define USBDEVICE_MAJOR 189
    877879
    878 /** Calculate the bus (a.k.a root hub) number of a USB device from it's sysfs
    879  * path.  sysfs nodes representing root hubs have file names of the form
     880/**
     881 * Calculate the bus (a.k.a root hub) number of a USB device from it's sysfs
     882 * path.
     883 *
     884 * sysfs nodes representing root hubs have file names of the form
    880885 * usb<n>, where n is the bus number; other devices start with that number.
    881886 * See [http://www.linux-usb.org/FAQ.html#i6] and
    882887 * [http://www.kernel.org/doc/Documentation/usb/proc_usb_info.txt] for
    883888 * equivalent information about usbfs.
     889 *
    884890 * @returns a bus number greater than 0 on success or 0 on failure.
    885891 */
    886 static unsigned usbGetBusFromSysfsPath(const char *pcszPath)
     892static unsigned usbsysfsGetBusFromPath(const char *pcszPath)
    887893{
    888894    const char *pcszFile = strrchr(pcszPath, '/');
     
    896902}
    897903
    898 /** Calculate the device number of a USB device.  See
    899  * drivers/usb/core/hub.c:usb_new_device as of Linux 2.6.20. */
    900 static dev_t usbMakeDevNum(unsigned bus, unsigned device)
     904
     905/**
     906 * Calculate the device number of a USB device.
     907 *
     908 * See drivers/usb/core/hub.c:usb_new_device as of Linux 2.6.20.
     909 */
     910static dev_t usbsysfsMakeDevNum(unsigned bus, unsigned device)
    901911{
    902912    AssertReturn(bus > 0, 0);
     
    906916}
    907917
     918
    908919/**
    909920 * If a file @a pcszNode from /sys/bus/usb/devices is a device rather than an
    910921 * interface add an element for the device to @a pvecDevInfo.
    911922 */
    912 static int addIfDevice(const char *pcszDevicesRoot,
    913                        const char *pcszNode,
    914                        VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo)
     923static int usbsysfsAddIfDevice(const char *pcszDevicesRoot, const char *pcszNode, VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo)
    915924{
    916925    const char *pcszFile = strrchr(pcszNode, '/');
     
    919928    if (strchr(pcszFile, ':'))
    920929        return VINF_SUCCESS;
    921     unsigned bus = usbGetBusFromSysfsPath(pcszNode);
     930
     931    unsigned bus = usbsysfsGetBusFromPath(pcszNode);
    922932    if (!bus)
    923933        return VINF_SUCCESS;
     934
    924935    int device = RTLinuxSysFsReadIntFile(10, "%s/devnum", pcszNode);
    925936    if (device < 0)
    926937        return VINF_SUCCESS;
    927     dev_t devnum = usbMakeDevNum(bus, device);
     938
     939    dev_t devnum = usbsysfsMakeDevNum(bus, device);
    928940    if (!devnum)
    929941        return VINF_SUCCESS;
     942
    930943    char szDevPath[RTPATH_MAX];
    931944    ssize_t cchDevPath;
     
    938951
    939952    USBDeviceInfo info;
    940     if (USBDevInfoInit(&info, szDevPath, pcszNode))
    941         if (RT_SUCCESS(VEC_PUSH_BACK_OBJ(pvecDevInfo, USBDeviceInfo,
    942                                          &info)))
     953    if (usbsysfsInitDevInfo(&info, szDevPath, pcszNode))
     954    {
     955        int rc = VEC_PUSH_BACK_OBJ(pvecDevInfo, USBDeviceInfo, &info);
     956        if (RT_SUCCESS(rc))
    943957            return VINF_SUCCESS;
    944     USBDevInfoCleanup(&info);
     958    }
     959    usbsysfsCleanupDevInfo(&info);
    945960    return VERR_NO_MEMORY;
    946961}
    947962
    948 /** The logic for testing whether a sysfs address corresponds to an
    949  * interface of a device.  Both must be referenced by their canonical
    950  * sysfs paths.  This is not tested, as the test requires file-system
    951  * interaction. */
    952 static bool muiIsAnInterfaceOf(const char *pcszIface, const char *pcszDev)
     963
     964/**
     965 * The logic for testing whether a sysfs address corresponds to an interface of
     966 * a device.
     967 *
     968 * Both must be referenced by their canonical sysfs paths.  This is not tested,
     969 * as the test requires file-system interaction.
     970 */
     971static bool usbsysfsMuiIsAnInterfaceOf(const char *pcszIface, const char *pcszDev)
    953972{
    954973    size_t cchDev = strlen(pcszDev);
     
    959978    Assert(pcszDev[0] == '/');
    960979    Assert(pcszDev[cchDev - 1] != '/');
     980
    961981    /* If this passes, pcszIface is at least cchDev long */
    962982    if (strncmp(pcszIface, pcszDev, cchDev))
    963983        return false;
     984
    964985    /* If this passes, pcszIface is longer than cchDev */
    965986    if (pcszIface[cchDev] != '/')
    966987        return false;
     988
    967989    /* In sysfs an interface is an immediate subdirectory of the device */
    968990    if (strchr(pcszIface + cchDev + 1, '/'))
    969991        return false;
     992
    970993    /* And it always has a colon in its name */
    971994    if (!strchr(pcszIface + cchDev + 1, ':'))
    972995        return false;
     996
    973997    /* And hopefully we have now elimitated everything else */
    974998    return true;
    975999}
     1000
    9761001
    9771002#ifdef DEBUG
     
    9831008    testIsAnInterfaceOf()
    9841009    {
    985         Assert(muiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0",
     1010        Assert(usbsysfsMuiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0",
    9861011               "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
    987         Assert(!muiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-1",
     1012        Assert(!usbsysfsMuiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-1",
    9881013               "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
    989         Assert(!muiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0/driver",
     1014        Assert(!usbsysfsMuiIsAnInterfaceOf("/sys/devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0/driver",
    9901015               "/sys/devices/pci0000:00/0000:00:1a.0/usb3"));
    9911016    }
     
    9951020#endif /* DEBUG */
    9961021
     1022
    9971023/**
    9981024 * Tell whether a file in /sys/bus/usb/devices is an interface rather than a
    999  * device.  To be used with getDeviceInfoFromSysfs().
    1000  */
    1001 static int addIfInterfaceOf(const char *pcszNode, USBDeviceInfo *pInfo)
    1002 {
    1003     if (!muiIsAnInterfaceOf(pcszNode, pInfo->mSysfsPath))
     1025 * device.
     1026 */
     1027static int usbsysfsAddIfInterfaceOf(const char *pcszNode, USBDeviceInfo *pInfo)
     1028{
     1029    if (!usbsysfsMuiIsAnInterfaceOf(pcszNode, pInfo->mSysfsPath))
    10041030        return VINF_SUCCESS;
     1031
    10051032    char *pszDup = (char *)RTStrDup(pcszNode);
    10061033    if (pszDup)
    1007         if (RT_SUCCESS(VEC_PUSH_BACK_PTR(&pInfo->mvecpszInterfaces,
    1008                                          char *, pszDup)))
     1034    {
     1035        int rc = VEC_PUSH_BACK_PTR(&pInfo->mvecpszInterfaces, char *, pszDup);
     1036        if (RT_SUCCESS(rc))
    10091037            return VINF_SUCCESS;
    1010     RTStrFree(pszDup);
     1038        RTStrFree(pszDup);
     1039    }
    10111040    return VERR_NO_MEMORY;
    10121041}
    10131042
    1014 /** Helper for readFilePaths().  Adds the entries from the open directory
    1015  * @a pDir to the vector @a pvecpchDevs using either the full path or the
    1016  * realpath() and skipping hidden files and files on which realpath() fails. */
    1017 static int readFilePathsFromDir(const char *pcszPath, DIR *pDir,
    1018                                 VECTOR_PTR(char *) *pvecpchDevs)
     1043
     1044/**
     1045 * Helper for usbsysfsReadFilePaths().
     1046 *
     1047 * Adds the entries from the open directory @a pDir to the vector @a pvecpchDevs
     1048 * using either the full path or the realpath() and skipping hidden files and
     1049 * files on which realpath() fails.
     1050 */
     1051static int usbsysfsReadFilePathsFromDir(const char *pcszPath, DIR *pDir, VECTOR_PTR(char *) *pvecpchDevs)
    10191052{
    10201053    struct dirent entry, *pResult;
     
    10241057         err = readdir_r(pDir, &entry, &pResult))
    10251058    {
    1026         char szPath[RTPATH_MAX + 1], szRealPath[RTPATH_MAX + 1], *pszPath;
     1059        char szPath[RTPATH_MAX + 1];
     1060        char szRealPath[RTPATH_MAX + 1];
    10271061        if (entry.d_name[0] == '.')
    10281062            continue;
    1029         if (snprintf(szPath, sizeof(szPath), "%s/%s", pcszPath,
    1030                      entry.d_name) < 0)
    1031             return RTErrConvertFromErrno(errno);
     1063        if (snprintf(szPath, sizeof(szPath), "%s/%s", pcszPath, entry.d_name) < 0)
     1064            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 pcszPath each time... */
    10321065        if (!realpath(szPath, szRealPath))
    10331066            return RTErrConvertFromErrno(errno);
    1034         pszPath = RTStrDup(szRealPath);
     1067        char *pszPath = RTStrDup(szRealPath);
    10351068        if (!pszPath)
    10361069            return VERR_NO_MEMORY;
     
    10401073    return RTErrConvertFromErrno(err);
    10411074}
     1075
    10421076
    10431077/**
     
    10501084 * @param   withRealPath  whether to canonicalise the filename with realpath
    10511085 */
    1052 static int readFilePaths(const char *pcszPath, VECTOR_PTR(char *) *pvecpchDevs)
    1053 {
    1054     DIR *pDir;
    1055     int rc;
    1056 
     1086static int usbsysfsReadFilePaths(const char *pcszPath, VECTOR_PTR(char *) *pvecpchDevs)
     1087{
    10571088    AssertPtrReturn(pvecpchDevs, EINVAL);
    10581089    AssertReturn(VEC_SIZE_PTR(pvecpchDevs) == 0, EINVAL);
    10591090    AssertPtrReturn(pcszPath, EINVAL);
    10601091
    1061     pDir = opendir(pcszPath);
     1092    DIR *pDir = opendir(pcszPath);
    10621093    if (!pDir)
    10631094        return RTErrConvertFromErrno(errno);
    1064     rc = readFilePathsFromDir(pcszPath, pDir, pvecpchDevs);
     1095    int rc = usbsysfsReadFilePathsFromDir(pcszPath, pDir, pvecpchDevs);
    10651096    if (closedir(pDir) < 0 && RT_SUCCESS(rc))
    10661097        rc = RTErrConvertFromErrno(errno);
     
    10681099}
    10691100
     1101
    10701102/**
    10711103 * Logic for USBSysfsEnumerateHostDevices.
     1104 *
    10721105 * @param pvecDevInfo  vector of device information structures to add device
    10731106 *                     information to
     
    10751108 *                     to simplify exit logic
    10761109 */
    1077 static int doSysfsEnumerateHostDevices(const char *pcszDevicesRoot,
    1078                                        VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo,
    1079                                        VECTOR_PTR(char *) *pvecpchDevs)
    1080 {
    1081     char **ppszEntry;
    1082     USBDeviceInfo *pInfo;
    1083     int rc;
     1110static int usbsysfsEnumerateHostDevicesWorker(const char *pcszDevicesRoot,
     1111                                              VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo,
     1112                                              VECTOR_PTR(char *) *pvecpchDevs)
     1113{
    10841114
    10851115    AssertPtrReturn(pvecDevInfo, VERR_INVALID_POINTER);
    10861116    LogFlowFunc (("pvecDevInfo=%p\n", pvecDevInfo));
    10871117
    1088     rc = readFilePaths("/sys/bus/usb/devices", pvecpchDevs);
     1118    int rc = usbsysfsReadFilePaths("/sys/bus/usb/devices", pvecpchDevs);
    10891119    if (RT_FAILURE(rc))
    10901120        return rc;
     1121
     1122    char **ppszEntry;
    10911123    VEC_FOR_EACH(pvecpchDevs, char *, ppszEntry)
    1092         if (RT_FAILURE(rc = addIfDevice(pcszDevicesRoot, *ppszEntry,
    1093                                         pvecDevInfo)))
     1124    {
     1125        rc = usbsysfsAddIfDevice(pcszDevicesRoot, *ppszEntry, pvecDevInfo))
     1126        if (RT_FAILURE(rc))
    10941127            return rc;
     1128    }
     1129
     1130    USBDeviceInfo *pInfo;
    10951131    VEC_FOR_EACH(pvecDevInfo, USBDeviceInfo, pInfo)
    10961132        VEC_FOR_EACH(pvecpchDevs, char *, ppszEntry)
    1097             if (RT_FAILURE(rc = addIfInterfaceOf(*ppszEntry, pInfo)))
     1133        {
     1134            rc = usbsysfsAddIfInterfaceOf(*ppszEntry, pInfo);
     1135            if (RT_FAILURE(rc))
    10981136                return rc;
     1137        }
    10991138    return VINF_SUCCESS;
    11001139}
    11011140
    1102 static int USBSysfsEnumerateHostDevices(const char *pcszDevicesRoot,
    1103                                         VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo)
     1141
     1142static int usbsysfsEnumerateHostDevices(const char *pcszDevicesRoot, VECTOR_OBJ(USBDeviceInfo) *pvecDevInfo)
    11041143{
    11051144    VECTOR_PTR(char *) vecpchDevs;
    1106     int rc = VERR_NOT_IMPLEMENTED;
    11071145
    11081146    AssertReturn(VEC_SIZE_OBJ(pvecDevInfo) == 0, VERR_INVALID_PARAMETER);
    11091147    LogFlowFunc(("entered\n"));
    11101148    VEC_INIT_PTR(&vecpchDevs, char *, RTStrFree);
    1111     rc = doSysfsEnumerateHostDevices(pcszDevicesRoot, pvecDevInfo,
    1112                                      &vecpchDevs);
     1149    int rc = usbsysfsEnumerateHostDevicesWorker(pcszDevicesRoot, pvecDevInfo, &vecpchDevs);
    11131150    VEC_CLEANUP_PTR(&vecpchDevs);
    11141151    LogFlowFunc(("rc=%Rrc\n", rc));
    11151152    return rc;
    11161153}
     1154
    11171155
    11181156/**
     
    11381176 * @param   pu8Port     Where to store the port number.
    11391177 */
    1140 static int usbGetPortFromSysfsPath(const char *pszPath, uint8_t *pu8Port)
     1178static int usbsysfsGetPortFromStr(const char *pszPath, uint8_t *pu8Port)
    11411179{
    11421180    AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
     
    11781216        return VERR_NOT_SUPPORTED;
    11791217    }
    1180     else
    1181     {
    1182         const char *pszLastPort = pchDot != NULL
    1183                                 ? pchDot  + 1
    1184                                 : pchDash + 1;
    1185         int rc = RTStrToUInt8Full(pszLastPort, 10, pu8Port);
    1186         if (rc != VINF_SUCCESS)
    1187         {
    1188             Log(("usbGetPortFromSysfsPath(%s): failed [3], rc=%Rrc\n", pszPath, rc));
    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         }
    1196 
    1197         /* usbfs compatibility, 0-based port number. */
    1198         *pu8Port -= 1;
    1199     }
     1218
     1219    const char *pszLastPort = pchDot != NULL
     1220                            ? pchDot  + 1
     1221                            : pchDash + 1;
     1222    int rc = RTStrToUInt8Full(pszLastPort, 10, pu8Port);
     1223    if (rc != VINF_SUCCESS)
     1224    {
     1225        Log(("usbGetPortFromSysfsPath(%s): failed [3], rc=%Rrc\n", pszPath, rc));
     1226        return VERR_INVALID_PARAMETER;
     1227    }
     1228    if (*pu8Port == 0)
     1229    {
     1230        Log(("usbGetPortFromSysfsPath(%s): failed [4]\n", pszPath));
     1231        return VERR_INVALID_PARAMETER;
     1232    }
     1233
     1234    /* usbfs compatibility, 0-based port number. */
     1235    *pu8Port -= 1;
    12001236    return VINF_SUCCESS;
    12011237}
     
    12071243 * @todo    This is really common code.
    12081244 */
    1209 DECLINLINE(void) usbLogDevice(PUSBDEVICE pDev)
     1245static void usbLogDevice(PUSBDEVICE pDev)
    12101246{
    12111247    NOREF(pDev);
    1212 
    1213     Log3(("USB device:\n"));
    1214     Log3(("Product: %s (%x)\n", pDev->pszProduct, pDev->idProduct));
    1215     Log3(("Manufacturer: %s (Vendor ID %x)\n", pDev->pszManufacturer, pDev->idVendor));
    1216     Log3(("Serial number: %s (%llx)\n", pDev->pszSerialNumber, pDev->u64SerialHash));
    1217     Log3(("Device revision: %d\n", pDev->bcdDevice));
    1218     Log3(("Device class: %x\n", pDev->bDeviceClass));
    1219     Log3(("Device subclass: %x\n", pDev->bDeviceSubClass));
    1220     Log3(("Device protocol: %x\n", pDev->bDeviceProtocol));
    1221     Log3(("USB version number: %d\n", pDev->bcdUSB));
    1222     Log3(("Device speed: %s\n",
    1223             pDev->enmSpeed == USBDEVICESPEED_UNKNOWN  ? "unknown"
    1224           : pDev->enmSpeed == USBDEVICESPEED_LOW      ? "1.5 MBit/s"
    1225           : pDev->enmSpeed == USBDEVICESPEED_FULL     ? "12 MBit/s"
    1226           : pDev->enmSpeed == USBDEVICESPEED_HIGH     ? "480 MBit/s"
    1227           : pDev->enmSpeed == USBDEVICESPEED_SUPER    ? "5.0 GBit/s"
    1228           : pDev->enmSpeed == USBDEVICESPEED_VARIABLE ? "variable"
    1229           :                                             "invalid"));
    1230     Log3(("Number of configurations: %d\n", pDev->bNumConfigurations));
    1231     Log3(("Bus number: %d\n", pDev->bBus));
    1232     Log3(("Port number: %d\n", pDev->bPort));
    1233     Log3(("Device number: %d\n", pDev->bDevNum));
    1234     Log3(("Device state: %s\n",
    1235             pDev->enmState == USBDEVICESTATE_UNSUPPORTED   ? "unsupported"
    1236           : pDev->enmState == USBDEVICESTATE_USED_BY_HOST  ? "in use by host"
    1237           : pDev->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE ? "in use by host, possibly capturable"
    1238           : pDev->enmState == USBDEVICESTATE_UNUSED        ? "not in use"
    1239           : pDev->enmState == USBDEVICESTATE_HELD_BY_PROXY ? "held by proxy"
    1240           : pDev->enmState == USBDEVICESTATE_USED_BY_GUEST ? "used by guest"
    1241           :                                                  "invalid"));
    1242     Log3(("OS device address: %s\n", pDev->pszAddress));
    1243 }
    1244 
    1245 /**
     1248    if (LogIs3Enabled())
     1249    {
     1250        Log3(("USB device:\n"));
     1251        Log3(("Product: %s (%x)\n", pDev->pszProduct, pDev->idProduct));
     1252        Log3(("Manufacturer: %s (Vendor ID %x)\n", pDev->pszManufacturer, pDev->idVendor));
     1253        Log3(("Serial number: %s (%llx)\n", pDev->pszSerialNumber, pDev->u64SerialHash));
     1254        Log3(("Device revision: %d\n", pDev->bcdDevice));
     1255        Log3(("Device class: %x\n", pDev->bDeviceClass));
     1256        Log3(("Device subclass: %x\n", pDev->bDeviceSubClass));
     1257        Log3(("Device protocol: %x\n", pDev->bDeviceProtocol));
     1258        Log3(("USB version number: %d\n", pDev->bcdUSB));
     1259        Log3(("Device speed: %s\n",
     1260                pDev->enmSpeed == USBDEVICESPEED_UNKNOWN  ? "unknown"
     1261              : pDev->enmSpeed == USBDEVICESPEED_LOW      ? "1.5 MBit/s"
     1262              : pDev->enmSpeed == USBDEVICESPEED_FULL     ? "12 MBit/s"
     1263              : pDev->enmSpeed == USBDEVICESPEED_HIGH     ? "480 MBit/s"
     1264              : pDev->enmSpeed == USBDEVICESPEED_SUPER    ? "5.0 GBit/s"
     1265              : pDev->enmSpeed == USBDEVICESPEED_VARIABLE ? "variable"
     1266              :                                             "invalid"));
     1267        Log3(("Number of configurations: %d\n", pDev->bNumConfigurations));
     1268        Log3(("Bus number: %d\n", pDev->bBus));
     1269        Log3(("Port number: %d\n", pDev->bPort));
     1270        Log3(("Device number: %d\n", pDev->bDevNum));
     1271        Log3(("Device state: %s\n",
     1272                pDev->enmState == USBDEVICESTATE_UNSUPPORTED   ? "unsupported"
     1273              : pDev->enmState == USBDEVICESTATE_USED_BY_HOST  ? "in use by host"
     1274              : pDev->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE ? "in use by host, possibly capturable"
     1275              : pDev->enmState == USBDEVICESTATE_UNUSED        ? "not in use"
     1276              : pDev->enmState == USBDEVICESTATE_HELD_BY_PROXY ? "held by proxy"
     1277              : pDev->enmState == USBDEVICESTATE_USED_BY_GUEST ? "used by guest"
     1278              :                                                  "invalid"));
     1279        Log3(("OS device address: %s\n", pDev->pszAddress));
     1280    }
     1281}
     1282
     1283
     1284/**
     1285 * Converts a sysfs BCD value into a uint16_t.
     1286 *
    12461287 * In contrast to usbReadBCD() this function can handle BCD values without
    12471288 * a decimal separator. This is necessary for parsing bcdDevice.
     1289 *
    12481290 * @param   pszBuf      Pointer to the string buffer.
    12491291 * @param   pu15        Pointer to the return value.
    12501292 * @returns IPRT status code.
    12511293 */
    1252 static int convertSysfsStrToBCD(const char *pszBuf, uint16_t *pu16)
     1294static int usbsysfsConvertStrToBCD(const char *pszBuf, uint16_t *pu16)
    12531295{
    12541296    char *pszNext;
     
    12841326#endif  /* VBOX_USB_WITH_SYSFS */
    12851327
    1286 static void fillInDeviceFromSysfs(USBDEVICE *Dev, USBDeviceInfo *pInfo)
     1328static void usbsysfsFillInDevice(USBDEVICE *pDev, USBDeviceInfo *pInfo)
    12871329{
    12881330    int rc;
     
    12901332
    12911333    /* Fill in the simple fields */
    1292     Dev->enmState           = USBDEVICESTATE_UNUSED;
    1293     Dev->bBus               = usbGetBusFromSysfsPath(pszSysfsPath);
    1294     Dev->bDeviceClass       = RTLinuxSysFsReadIntFile(16, "%s/bDeviceClass", pszSysfsPath);
    1295     Dev->bDeviceSubClass    = RTLinuxSysFsReadIntFile(16, "%s/bDeviceSubClass", pszSysfsPath);
    1296     Dev->bDeviceProtocol    = RTLinuxSysFsReadIntFile(16, "%s/bDeviceProtocol", pszSysfsPath);
    1297     Dev->bNumConfigurations = RTLinuxSysFsReadIntFile(10, "%s/bNumConfigurations", pszSysfsPath);
    1298     Dev->idVendor           = RTLinuxSysFsReadIntFile(16, "%s/idVendor", pszSysfsPath);
    1299     Dev->idProduct          = RTLinuxSysFsReadIntFile(16, "%s/idProduct", pszSysfsPath);
    1300     Dev->bDevNum            = RTLinuxSysFsReadIntFile(10, "%s/devnum", pszSysfsPath);
     1334    pDev->enmState           = USBDEVICESTATE_UNUSED;
     1335    pDev->bBus               = usbsysfsGetBusFromPath(pszSysfsPath);
     1336    pDev->bDeviceClass       = RTLinuxSysFsReadIntFile(16, "%s/bDeviceClass", pszSysfsPath);
     1337    pDev->bDeviceSubClass    = RTLinuxSysFsReadIntFile(16, "%s/bDeviceSubClass", pszSysfsPath);
     1338    pDev->bDeviceProtocol    = RTLinuxSysFsReadIntFile(16, "%s/bDeviceProtocol", pszSysfsPath);
     1339    pDev->bNumConfigurations = RTLinuxSysFsReadIntFile(10, "%s/bNumConfigurations", pszSysfsPath);
     1340    pDev->idVendor           = RTLinuxSysFsReadIntFile(16, "%s/idVendor", pszSysfsPath);
     1341    pDev->idProduct          = RTLinuxSysFsReadIntFile(16, "%s/idProduct", pszSysfsPath);
     1342    pDev->bDevNum            = RTLinuxSysFsReadIntFile(10, "%s/devnum", pszSysfsPath);
    13011343
    13021344    /* Now deal with the non-numeric bits. */
     
    13071349
    13081350    /* For simplicity, we just do strcmps on the next one. */
    1309     cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/speed",
    1310                                       pszSysfsPath);
     1351    cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/speed", pszSysfsPath);
    13111352    if (cchRead <= 0 || (size_t) cchRead == sizeof(szBuf))
    1312         Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
     1353        pDev->enmState = USBDEVICESTATE_UNSUPPORTED;
    13131354    else
    1314         Dev->enmSpeed =   !strcmp(szBuf, "1.5")  ? USBDEVICESPEED_LOW
    1315                         : !strcmp(szBuf, "12")   ? USBDEVICESPEED_FULL
    1316                         : !strcmp(szBuf, "480")  ? USBDEVICESPEED_HIGH
    1317                         : !strcmp(szBuf, "5000") ? USBDEVICESPEED_SUPER
    1318                         : USBDEVICESPEED_UNKNOWN;
    1319 
    1320     cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/version",
    1321                                       pszSysfsPath);
     1355        pDev->enmSpeed = !strcmp(szBuf, "1.5")  ? USBDEVICESPEED_LOW
     1356                       : !strcmp(szBuf, "12")   ? USBDEVICESPEED_FULL
     1357                       : !strcmp(szBuf, "480")  ? USBDEVICESPEED_HIGH
     1358                       : !strcmp(szBuf, "5000") ? USBDEVICESPEED_SUPER
     1359                       : USBDEVICESPEED_UNKNOWN;
     1360
     1361    cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/version", pszSysfsPath);
    13221362    if (cchRead <= 0 || (size_t) cchRead == sizeof(szBuf))
    1323         Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
     1363        pDev->enmState = USBDEVICESTATE_UNSUPPORTED;
    13241364    else
    13251365    {
    1326         rc = convertSysfsStrToBCD(szBuf, &Dev->bcdUSB);
     1366        rc = usbsysfsConvertStrToBCD(szBuf, &pDev->bcdUSB);
    13271367        if (RT_FAILURE(rc))
    13281368        {
    1329             Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
    1330             Dev->bcdUSB = (uint16_t)-1;
    1331         }
    1332     }
    1333 
    1334     cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/bcdDevice",
    1335                                       pszSysfsPath);
     1369            pDev->enmState = USBDEVICESTATE_UNSUPPORTED;
     1370            pDev->bcdUSB   = UINT16_MAX
     1371        }
     1372    }
     1373
     1374    cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/bcdDevice", pszSysfsPath);
    13361375    if (cchRead <= 0 || (size_t) cchRead == sizeof(szBuf))
    1337         Dev->bcdDevice = (uint16_t)-1;
     1376        pDev->bcdDevice = UINT16_MAX;
    13381377    else
    13391378    {
    1340         rc = convertSysfsStrToBCD(szBuf, &Dev->bcdDevice);
     1379        rc = usbsysfsConvertStrToBCD(szBuf, &pDev->bcdDevice);
    13411380        if (RT_FAILURE(rc))
    1342             Dev->bcdDevice = (uint16_t)-1;
     1381            pDev->bcdDevice = UINT16_MAX;
    13431382    }
    13441383
    13451384    /* Now do things that need string duplication */
    1346     cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/product",
    1347                                       pszSysfsPath);
     1385    cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/product", pszSysfsPath);
    13481386    if (cchRead > 0 && (size_t) cchRead < sizeof(szBuf))
    13491387    {
    13501388        usbPurgeEncoding(szBuf);
    1351         Dev->pszProduct = RTStrDup(szBuf);
    1352     }
    1353 
    1354     cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/serial",
    1355                                       pszSysfsPath);
     1389        pDev->pszProduct = RTStrDup(szBuf);
     1390    }
     1391
     1392    cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/serial", pszSysfsPath);
    13561393    if (cchRead > 0 && (size_t) cchRead < sizeof(szBuf))
    13571394    {
    13581395        usbPurgeEncoding(szBuf);
    1359         Dev->pszSerialNumber = RTStrDup(szBuf);
    1360         Dev->u64SerialHash = USBLibHashSerial(szBuf);
    1361     }
    1362 
    1363     cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/manufacturer",
    1364                                       pszSysfsPath);
     1396        pDev->pszSerialNumber = RTStrDup(szBuf);
     1397        pDev->u64SerialHash = USBLibHashSerial(szBuf);
     1398    }
     1399
     1400    cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/manufacturer", pszSysfsPath);
    13651401    if (cchRead > 0 && (size_t) cchRead < sizeof(szBuf))
    13661402    {
    13671403        usbPurgeEncoding(szBuf);
    1368         Dev->pszManufacturer = RTStrDup(szBuf);
     1404        pDev->pszManufacturer = RTStrDup(szBuf);
    13691405    }
    13701406
    13711407    /* Work out the port number */
    1372     if (RT_FAILURE(usbGetPortFromSysfsPath(pszSysfsPath, &Dev->bPort)))
    1373         Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
     1408    if (RT_FAILURE(usbsysfsGetPortFromStr(pszSysfsPath, &pDev->bPort)))
     1409        pDev->enmState = USBDEVICESTATE_UNSUPPORTED;
    13741410
    13751411    /* Check the interfaces to see if we can support the device. */
     
    13771413    VEC_FOR_EACH(&pInfo->mvecpszInterfaces, char *, ppszIf)
    13781414    {
    1379         ssize_t cb = RTLinuxSysFsGetLinkDest(szBuf, sizeof(szBuf), "%s/driver",
    1380                                              *ppszIf);
    1381         if (cb > 0 && Dev->enmState != USBDEVICESTATE_UNSUPPORTED)
    1382             Dev->enmState = (strcmp(szBuf, "hub") == 0)
    1383                           ? USBDEVICESTATE_UNSUPPORTED
    1384                           : USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
    1385         if (RTLinuxSysFsReadIntFile(16, "%s/bInterfaceClass",
    1386                                     *ppszIf) == 9 /* hub */)
    1387             Dev->enmState = USBDEVICESTATE_UNSUPPORTED;
     1415        ssize_t cb = RTLinuxSysFsGetLinkDest(szBuf, sizeof(szBuf), "%s/driver", *ppszIf);
     1416        if (cb > 0 && pDev->enmState != USBDEVICESTATE_UNSUPPORTED)
     1417            pDev->enmState = (strcmp(szBuf, "hub") == 0)
     1418                           ? USBDEVICESTATE_UNSUPPORTED
     1419                           : USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
     1420        if (RTLinuxSysFsReadIntFile(16, "%s/bInterfaceClass", *ppszIf) == 9 /* hub */)
     1421            pDev->enmState = USBDEVICESTATE_UNSUPPORTED;
    13881422    }
    13891423
     
    13921426     * them. */
    13931427    char *pszAddress = NULL;
    1394     RTStrAPrintf(&pszAddress, "sysfs:%s//device:%s", pszSysfsPath,
    1395                  pInfo->mDevice);
    1396     Dev->pszAddress = pszAddress;
    1397     Dev->pszBackend = RTStrDup("host");
     1428    RTStrAPrintf(&pszAddress, "sysfs:%s//device:%s", pszSysfsPath, pInfo->mDevice);
     1429    pDev->pszAddress = pszAddress;
     1430    pDev->pszBackend = RTStrDup("host");
    13981431
    13991432    /* Work out from the data collected whether we can support this device. */
    1400     Dev->enmState = usbDeterminState(Dev);
    1401     usbLogDevice(Dev);
    1402 }
     1433    pDev->enmState = usbDeterminState(pDev);
     1434    usbLogDevice(pDev);
     1435}
     1436
    14031437
    14041438/**
    14051439 * USBProxyService::getDevices() implementation for sysfs.
    14061440 */
    1407 static PUSBDEVICE getDevicesFromSysfs(const char *pcszDevicesRoot, bool testfs)
     1441static PUSBDEVICE usbsysfsGetDevices(const char *pcszDevicesRoot, bool testfs)
    14081442{
    14091443#ifdef VBOX_USB_WITH_SYSFS
     
    14151449    int rc;
    14161450
    1417     VEC_INIT_OBJ(&vecDevInfo, USBDeviceInfo, USBDevInfoCleanup);
    1418     rc = USBSysfsEnumerateHostDevices(pcszDevicesRoot, &vecDevInfo);
     1451    VEC_INIT_OBJ(&vecDevInfo, USBDeviceInfo, usbsysfsCleanupDevInfo);
     1452    rc = usbsysfsEnumerateHostDevices(pcszDevicesRoot, &vecDevInfo);
    14191453    if (RT_FAILURE(rc))
    14201454        return NULL;
    14211455    VEC_FOR_EACH(&vecDevInfo, USBDeviceInfo, pInfo)
    14221456    {
    1423         USBDEVICE *Dev = (USBDEVICE *)RTMemAllocZ(sizeof(USBDEVICE));
    1424         if (!Dev)
     1457        USBDEVICE *pDev = (USBDEVICE *)RTMemAllocZ(sizeof(USBDEVICE));
     1458        if (!pDev)
    14251459            rc = VERR_NO_MEMORY;
    14261460        if (RT_SUCCESS(rc))
    1427         {
    1428             fillInDeviceFromSysfs(Dev, pInfo);
    1429         }
     1461            usbsysfsFillInDevice(pDev, pInfo);
    14301462        if (   RT_SUCCESS(rc)
    1431             && (   Dev->enmState != USBDEVICESTATE_UNSUPPORTED
     1463            && (   pDev->enmState != USBDEVICESTATE_UNSUPPORTED
    14321464                || testfs)
    1433             && Dev->pszAddress != NULL
     1465            && pDev->pszAddress != NULL
    14341466           )
    14351467        {
    14361468            if (pLast != NULL)
    14371469            {
    1438                 pLast->pNext = Dev;
     1470                pLast->pNext = pDev;
    14391471                pLast = pLast->pNext;
    14401472            }
    14411473            else
    1442                 pFirst = pLast = Dev;
     1474                pFirst = pLast = pDev;
    14431475        }
    14441476        else
    1445             deviceFree(Dev);
     1477            deviceFree(pDev);
    14461478        if (RT_FAILURE(rc))
    14471479            break;
     
    14581490
    14591491#ifdef UNIT_TEST
     1492
    14601493/* Set up mock functions for USBProxyLinuxCheckDeviceRoot - here dlsym and close
    14611494 * for the inotify presence check. */
     
    14831516# define dlsym testDLSym
    14841517# define close(a) do {} while (0)
    1485 #endif
    1486 
    1487 /** Is inotify available and working on this system?  This is a requirement
    1488  * for using USB with sysfs */
    1489 static bool inotifyAvailable(void)
     1518
     1519#endif /* UNIT_TEST */
     1520
     1521/**
     1522 * Is inotify available and working on this system?
     1523 *
     1524 * This is a requirement for using USB with sysfs
     1525 */
     1526static bool usbsysfsInotifyAvailable(void)
    14901527{
    14911528    int (*inotify_init)(void);
     
    15021539
    15031540#ifdef UNIT_TEST
     1541
    15041542# undef dlsym
    15051543# undef close
    1506 #endif
    1507 
    1508 #ifdef UNIT_TEST
     1544
    15091545/** Unit test list of usbfs addresses of connected devices. */
    1510 static const char **s_pacszUsbfsDeviceAddresses = NULL;
     1546static const char **g_papszUsbfsDeviceAddresses = NULL;
    15111547
    15121548static PUSBDEVICE testGetUsbfsDevices(const char *pcszUsbfsRoot, bool testfs)
     
    15141550    const char **pcsz;
    15151551    PUSBDEVICE pList = NULL, pTail = NULL;
    1516     for (pcsz = s_pacszUsbfsDeviceAddresses; pcsz && *pcsz; ++pcsz)
     1552    for (pcsz = g_papszUsbfsDeviceAddresses; pcsz && *pcsz; ++pcsz)
    15171553    {
    15181554        PUSBDEVICE pNext = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
     
    15341570    return pList;
    15351571}
    1536 # define getDevicesFromUsbfs testGetUsbfsDevices
     1572# define usbfsGetDevices testGetUsbfsDevices
    15371573
    15381574/**
     
    15411577 * @param  pacszDeviceAddresses  NULL terminated array of usbfs device addresses
    15421578 */
    1543 void TestUSBSetAvailableUsbfsDevices(const char **pacszDeviceAddresses)
    1544 {
    1545     s_pacszUsbfsDeviceAddresses = pacszDeviceAddresses;
     1579void TestUSBSetAvailableUsbfsDevices(const char **papszDeviceAddresses)
     1580{
     1581    g_papszUsbfsDeviceAddresses = papszDeviceAddresses;
    15461582}
    15471583
    15481584/** Unit test list of files reported as accessible by access(3).  We only do
    15491585 * accessible or not accessible. */
    1550 static const char **s_pacszAccessibleFiles = NULL;
     1586static const char **g_papszAccessibleFiles = NULL;
    15511587
    15521588static int testAccess(const char *pcszPath, int mode)
    15531589{
    15541590    const char **pcsz;
    1555     for (pcsz = s_pacszAccessibleFiles; pcsz && *pcsz; ++pcsz)
     1591    for (pcsz = g_papszAccessibleFiles; pcsz && *pcsz; ++pcsz)
    15561592        if (!RTStrCmp(pcszPath, *pcsz))
    15571593            return 0;
     
    15601596# define access testAccess
    15611597
     1598
    15621599/**
    15631600 * Specify the list of files that access will report as accessible (at present
    15641601 * we only do accessible or not accessible) during unit testing (of
    15651602 * USBProxyLinuxGetDevices)
    1566  * @param  pacszAccessibleFiles  NULL terminated array of file paths to be
     1603 * @param  papszAccessibleFiles  NULL terminated array of file paths to be
    15671604 *                               reported accessible
    15681605 */
    1569 void TestUSBSetAccessibleFiles(const char **pacszAccessibleFiles)
    1570 {
    1571     s_pacszAccessibleFiles = pacszAccessibleFiles;
    1572 }
    1573 #endif
    1574 
    1575 #ifdef UNIT_TEST
     1606void TestUSBSetAccessibleFiles(const char **papszAccessibleFiles)
     1607{
     1608    g_papszAccessibleFiles = papszAccessibleFiles;
     1609}
     1610
     1611
    15761612#  ifdef UNIT_TEST
    15771613    /** The path we pretend the usbfs root is located at, or NULL. */
     
    15911627#  endif
    15921628
     1629
    15931630/** Select which access methods will be available to the @a init method
    15941631 * during unit testing, and (hack!) what return code it will see from
     
    16041641    s_rcTestMethodInitResult = rcMethodInitResult;
    16051642}
     1643
    16061644
    16071645/** Specify the environment that the @a init method will see during unit
     
    16331671     && !RTStrNCmp(pcszFile, s_pcszTestUsbfsRoot, strlen(s_pcszTestUsbfsRoot)) \
    16341672     && !RTStrCmp(pcszFile + strlen(s_pcszTestUsbfsRoot), "/devices"))
    1635 #endif
    1636 
    1637 /**
     1673
     1674#endif /* UNIT_TEST */
     1675
     1676/**
     1677 * Use USBFS-like or sysfs/device node-like access method?
     1678 *
    16381679 * Selects the access method that will be used to access USB devices based on
    16391680 * what is available on the host and what if anything the user has specified
    16401681 * in the environment.
     1682 *
    16411683 * @returns iprt status value
    16421684 * @param  pfUsingUsbfsDevices  on success this will be set to true if
     
    16461688 *                              device nodes will be stored here
    16471689 */
    1648 int USBProxyLinuxChooseMethod(bool *pfUsingUsbfsDevices,
    1649                               const char **ppcszDevicesRoot)
     1690int USBProxyLinuxChooseMethod(bool *pfUsingUsbfsDevices, const char **ppcszDevicesRoot)
    16501691{
    16511692    /*
     
    16581699     * the root they provide for validity.
    16591700     */
    1660     bool fUsbfsChosen = false, fSysfsChosen = false;
     1701    bool fUsbfsChosen = false;
     1702    bool fSysfsChosen = false;
    16611703    const char *pcszUsbFromEnv = RTEnvGet("VBOX_USB");
    16621704    const char *pcszUsbRoot = NULL;
     
    17101752    }
    17111753    /* else */
    1712     return   pcszUsbFromEnv ? VERR_NOT_FOUND
    1713            : RTDirExists("/dev/vboxusb") ? VERR_VUSB_USB_DEVICE_PERMISSION
    1714            : RTFileExists("/proc/bus/usb/devices") ? VERR_VUSB_USBFS_PERMISSION
    1715            : VERR_NOT_FOUND;
     1754    return pcszUsbFromEnv ? VERR_NOT_FOUND
     1755         : RTDirExists("/dev/vboxusb") ? VERR_VUSB_USB_DEVICE_PERMISSION
     1756         : RTFileExists("/proc/bus/usb/devices") ? VERR_VUSB_USBFS_PERMISSION
     1757         : VERR_NOT_FOUND;
    17161758}
    17171759
     
    17251767/**
    17261768 * Check whether a USB device tree root is usable
     1769 *
    17271770 * @param pcszRoot        the path to the root of the device tree
    17281771 * @param fIsDeviceNodes  whether this is a device node (or usbfs) tree
     
    17391782        {
    17401783            fOK = true;
    1741             pDevices = getDevicesFromUsbfs(pcszRoot, true);
     1784            pDevices = usbfsGetDevices(pcszRoot, true);
    17421785            if (pDevices)
    17431786            {
     
    17511794        }
    17521795    }
    1753     else  /* device nodes */
    1754         if (inotifyAvailable() && !access(pcszRoot, R_OK | X_OK))
    1755             fOK = true;
     1796    /* device nodes */
     1797    else if (usbsysfsInotifyAvailable() && !access(pcszRoot, R_OK | X_OK))
     1798        fOK = true;
    17561799    return fOK;
    17571800}
    17581801
    17591802#ifdef UNIT_TEST
    1760 # undef getDevicesFromUsbfs
     1803# undef usbfsGetDevices
    17611804# undef access
    17621805#endif
     
    17731816{
    17741817    if (!fUseSysfs)
    1775         return getDevicesFromUsbfs(pcszDevicesRoot, false);
    1776     return getDevicesFromSysfs(pcszDevicesRoot, false);
    1777 }
     1818        return usbfsGetDevices(pcszDevicesRoot, false);
     1819    return usbsysfsGetDevices(pcszDevicesRoot, false);
     1820}
Note: See TracChangeset for help on using the changeset viewer.

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