VirtualBox

Ignore:
Timestamp:
Oct 25, 2019 2:35:50 PM (5 years ago)
Author:
vboxsync
Message:

USB/win: Continuing enumeration code updates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/VBoxUSB/win/lib/VBoxUsbLib-win.cpp

    r81452 r81547  
    114114
    115115
     116static void usbLibVuFreeDevices(PVBOXUSB_DEV pDevInfos)
     117{
     118    while (pDevInfos)
     119    {
     120        PVBOXUSB_DEV pNext = pDevInfos->pNext;
     121        RTMemFree(pDevInfos);
     122        pDevInfos = pNext;
     123    }
     124}
     125
     126/* Check that a proxied device responds the way we expect it to. */
    116127static int usbLibVuDeviceValidate(PVBOXUSB_DEV pVuDev)
    117128{
     
    125136    {
    126137        dwErr = GetLastError();
    127         AssertMsgFailed(("CreateFile FAILED to open %s, dwErr=%u\n", pVuDev->szName, dwErr));
     138        AssertFailed();
    128139        LogRelFunc(("Failed to open `%s' (dwErr=%u)!\n", pVuDev->szName, dwErr));
    129140        return VERR_GENERAL_FAILURE;
     
    139150        {
    140151            dwErr = GetLastError();
    141             AssertMsgFailed(("DeviceIoControl SUPUSB_IOCTL_GET_VERSION failed with LastError=%Rwa\n", dwErr));
     152            AssertFailed();
    142153            LogRelFunc(("SUPUSB_IOCTL_GET_VERSION failed on `%s' (dwErr=%u)!\n", pVuDev->szName, dwErr));
    143154            break;
     
    150161           )
    151162        {
    152             AssertMsgFailed(("Invalid version %d:%d vs %d:%d\n", version.u32Major, version.u32Minor, USBDRV_MAJOR_VERSION, USBDRV_MINOR_VERSION));
     163            AssertFailed();
    153164            LogRelFunc(("Invalid version %d:%d (%s) vs %d:%d (library)!\n", version.u32Major, version.u32Minor, pVuDev->szName, USBDRV_MAJOR_VERSION, USBDRV_MINOR_VERSION));
    154165            break;
     
    158169        {
    159170            dwErr = GetLastError();
    160             AssertMsgFailed(("DeviceIoControl SUPUSB_IOCTL_IS_OPERATIONAL failed with LastError=%Rwa\n", dwErr));
     171            AssertFailed();
    161172            LogRelFunc(("SUPUSB_IOCTL_IS_OPERATIONAL failed on `%s' (dwErr=%u)!\n", pVuDev->szName, dwErr));
    162173            break;
     
    170181}
    171182
     183#ifndef VBOX_WITH_NEW_USB_ENUM
    172184static int usbLibVuDevicePopulate(PVBOXUSB_DEV pVuDev, HDEVINFO hDevInfo, PSP_DEVICE_INTERFACE_DATA pIfData)
    173185{
     
    211223        strncpy(pVuDev->szName, pIfDetailData->DevicePath, sizeof (pVuDev->szName));
    212224
    213 #ifdef VBOX_WITH_NEW_USB_ENUM
    214         if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DevInfoData, SPDRP_LOCATION_INFORMATION,
    215 #else
    216225        if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DevInfoData, SPDRP_DRIVER,
    217 #endif
    218226            NULL, /* OUT PDWORD PropertyRegDataType */
    219227            (PBYTE)pVuDev->szDriverRegName,
     
    234242    RTMemFree(pIfDetailData);
    235243    return rc;
    236 }
    237 
    238 static void usbLibVuFreeDevices(PVBOXUSB_DEV pDevInfos)
    239 {
    240     while (pDevInfos)
    241     {
    242         PVBOXUSB_DEV pNext = pDevInfos->pNext;
    243         RTMemFree(pDevInfos);
    244         pDevInfos = pNext;
    245     }
    246244}
    247245
     
    306304}
    307305
    308 #ifndef VBOX_WITH_NEW_USB_ENUM
    309306static int usbLibDevPopulate(PUSBDEVICE pDev, PUSB_NODE_CONNECTION_INFORMATION_EX pConInfo, ULONG iPort, LPCSTR lpszDrvKeyName, LPCSTR lpszHubName, PVBOXUSB_STRING_DR_ENTRY pDrList)
    310307{
     
    387384}
    388385#else
     386
     387static PSP_DEVICE_INTERFACE_DETAIL_DATA usbLibGetDevDetail(HDEVINFO InfoSet, PSP_DEVICE_INTERFACE_DATA InterfaceData, PSP_DEVINFO_DATA DevInfoData);
     388static void *usbLibGetRegistryProperty(HDEVINFO InfoSet, const PSP_DEVINFO_DATA DevData, DWORD Property);
     389
     390/* Populate the data for a single proxied USB device. */
     391static int usbLibVUsbDevicePopulate(PVBOXUSB_DEV pVuDev, HDEVINFO InfoSet, PSP_DEVICE_INTERFACE_DATA InterfaceData)
     392{
     393    PSP_DEVICE_INTERFACE_DETAIL_DATA    DetailData = NULL;
     394    SP_DEVINFO_DATA                     DeviceData;
     395    LPCSTR                              Location;
     396    int                                 rc = VINF_SUCCESS;
     397
     398    memset(&DeviceData, 0, sizeof(DeviceData));
     399    DeviceData.cbSize = sizeof(DeviceData);
     400    /* The interface detail includes the device path. */
     401    DetailData = usbLibGetDevDetail(InfoSet, InterfaceData, &DeviceData);
     402    if (DetailData)
     403    {
     404        strncpy(pVuDev->szName, DetailData->DevicePath, sizeof(pVuDev->szName));
     405
     406        /* The location is used as a unique identifier for cross-referencing the two lists. */
     407        Location = (LPCSTR)usbLibGetRegistryProperty(InfoSet, &DeviceData, SPDRP_LOCATION_INFORMATION);
     408        if (Location)
     409        {
     410            strncpy(pVuDev->szDriverRegName, Location, sizeof(pVuDev->szDriverRegName));
     411            rc = usbLibVuDeviceValidate(pVuDev);
     412            LogRelFunc(("Found VBoxUSB on `%s' (rc=%d)\n", pVuDev->szName, rc));
     413            AssertRC(rc);
     414
     415            RTMemFree((void *)Location);
     416        }
     417        else
     418        {
     419            /* Errors will be logged by usbLibGetRegistryProperty(). */
     420            rc = VERR_GENERAL_FAILURE;
     421        }
     422
     423        RTMemFree(DetailData);
     424    }
     425    else
     426    {
     427        /* Errors will be logged by usbLibGetDevDetail(). */
     428        rc = VERR_GENERAL_FAILURE;
     429    }
     430
     431
     432    return rc;
     433}
     434
     435/* Enumerate proxied USB devices (with VBoxUSB.sys loaded). */
     436static int usbLibEnumVUsbDevices(PVBOXUSB_DEV *ppVuDevs, uint32_t *pcVuDevs)
     437{
     438    SP_DEVICE_INTERFACE_DATA    InterfaceData;
     439    HDEVINFO                    InfoSet;
     440    DWORD                       DeviceIndex;
     441    DWORD                       dwErr;
     442
     443    *ppVuDevs = NULL;
     444    *pcVuDevs = 0;
     445
     446    /* Enumerate all present devices which support the GUID_CLASS_VBOXUSB interface. */
     447    InfoSet = SetupDiGetClassDevs(&GUID_CLASS_VBOXUSB, NULL, NULL,
     448                                  (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
     449    if (InfoSet == INVALID_HANDLE_VALUE)
     450    {
     451        DWORD dwErr = GetLastError();
     452        LogRelFunc(("SetupDiGetClassDevs for GUID_CLASS_VBOXUSB failed (dwErr=%u)\n", dwErr));
     453        AssertFailed();
     454        return VERR_GENERAL_FAILURE;
     455    }
     456
     457    memset(&InterfaceData, 0, sizeof(InterfaceData));
     458    InterfaceData.cbSize = sizeof(InterfaceData);
     459    DeviceIndex = 0;
     460
     461    /* Loop over the enumerated list. */
     462    while (SetupDiEnumDeviceInterfaces(InfoSet, NULL, &GUID_CLASS_VBOXUSB, DeviceIndex, &InterfaceData))
     463    {
     464        /* we've now got the IfData */
     465        PVBOXUSB_DEV pVuDev = (PVBOXUSB_DEV)RTMemAllocZ(sizeof (*pVuDev));
     466        if (!pVuDev)
     467        {
     468            AssertFailed();
     469            LogRelFunc(("RTMemAllocZ failed\n"));
     470            break;
     471        }
     472
     473        int rc = usbLibVUsbDevicePopulate(pVuDev, InfoSet, &InterfaceData);
     474        if (RT_SUCCESS(rc))
     475        {
     476            pVuDev->pNext = *ppVuDevs;
     477            *ppVuDevs = pVuDev;
     478            ++*pcVuDevs;
     479        }
     480        else    /* Skip this device but continue enumerating. */
     481            AssertMsgFailed(("usbLibVuDevicePopulate failed, rc=%d\n", rc));
     482
     483        memset(&InterfaceData, 0, sizeof(InterfaceData));
     484        InterfaceData.cbSize = sizeof(InterfaceData);
     485        ++DeviceIndex;
     486    }
     487
     488    /* Paranoia. */
     489    dwErr = GetLastError();
     490    if (dwErr != ERROR_NO_MORE_ITEMS)
     491    {
     492        LogRelFunc(("SetupDiEnumDeviceInterfaces failed (dwErr=%u)\n", dwErr));
     493        AssertFailed();
     494    }
     495
     496    SetupDiDestroyDeviceInfoList(InfoSet);
     497
     498    return VINF_SUCCESS;
     499}
     500
    389501static int usbLibDevPopulate(PUSBDEVICE pDev, PUSB_NODE_CONNECTION_INFORMATION_EX pConInfo, ULONG iPort, LPCSTR lpszLocation, LPCSTR lpszDrvKeyName, LPCSTR lpszHubName, PVBOXUSB_STRING_DR_ENTRY pDrList)
    390502{
     
    10281140}
    10291141
    1030 /* Given a HDEVINFO and SP_DEVICE_INTERFACE_DATA, get the interface detail data. */
    1031 static PSP_DEVICE_INTERFACE_DETAIL_DATA usbLibGetDevDetail(HDEVINFO InfoSet, PSP_DEVICE_INTERFACE_DATA InterfaceData)
     1142/* Given a HDEVINFO and SP_DEVICE_INTERFACE_DATA, get the interface detail data and optionally device info data. */
     1143static PSP_DEVICE_INTERFACE_DETAIL_DATA usbLibGetDevDetail(HDEVINFO InfoSet, PSP_DEVICE_INTERFACE_DATA InterfaceData, PSP_DEVINFO_DATA DevInfoData)
    10321144{
    10331145    BOOL                                rc;
     
    10351147    PSP_DEVICE_INTERFACE_DETAIL_DATA    DetailData;
    10361148
    1037     rc = SetupDiGetDeviceInterfaceDetail(InfoSet, InterfaceData, NULL, 0, &dwReqLen, NULL);
     1149    rc = SetupDiGetDeviceInterfaceDetail(InfoSet, InterfaceData, NULL, 0, &dwReqLen, DevInfoData);
    10381150    if (!rc && (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
    10391151    {
     
    10491161    DetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    10501162
    1051     rc = SetupDiGetDeviceInterfaceDetail(InfoSet, InterfaceData, DetailData, dwReqLen, &dwReqLen, NULL);
     1163    rc = SetupDiGetDeviceInterfaceDetail(InfoSet, InterfaceData, DetailData, dwReqLen, &dwReqLen, DevInfoData);
    10521164    if (!rc)
    10531165    {
     
    10911203    }
    10921204
    1093     DetailData = usbLibGetDevDetail(InfoSet, &InterfaceData);
     1205    DetailData = usbLibGetDevDetail(InfoSet, &InterfaceData, NULL);
    10941206    if (!DetailData)
    10951207    {
     
    12981410    LPCSTR              DriverKey;
    12991411
    1300     /* Ask for the USB PnP enumerator for all it has. */
     1412    /* Ask the USB PnP enumerator for all it has. */
    13011413    InfoSet = SetupDiGetClassDevs(NULL, "USB", NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
    13021414
     
    14291541#ifdef VBOX_WITH_ANNOYING_USB_ASSERTIONS
    14301542                /* ERROR_DEVICE_NOT_CONNECTED -> device was removed just now */
    1431                 AssertMsg(dwErr == ERROR_DEVICE_NOT_CONNECTED, (__FUNCTION__": DeviceIoControl failed dwErr (%u)\n", dwErr));
     1543                AsserFailed();
    14321544#endif
    14331545                LogRelFunc(("SUPUSB_IOCTL_GET_DEVICE failed on '%s' (dwErr=%u)!\n", pDevInfos->szName, dwErr));
     
    14451557                DWORD dwErr = GetLastError();
    14461558                /* ERROR_DEVICE_NOT_CONNECTED -> device was removed just now */
    1447                 AssertMsgFailed(("Monitor DeviceIoControl failed dwErr (%u)\n", dwErr));
     1559                AssertFailed();
    14481560                LogRelFunc(("SUPUSBFLT_IOCTL_GET_DEVICE failed for '%s' (hDevice=%p, dwErr=%u)!\n", pDevInfos->szName, hDevice, dwErr));
    14491561                CloseHandle(hDev);
     
    15011613        PVBOXUSB_DEV pDevInfos = NULL;
    15021614        uint32_t cDevInfos = 0;
     1615#ifdef VBOX_WITH_NEW_USB_ENUM
     1616        rc = usbLibEnumVUsbDevices(&pDevInfos, &cDevInfos);
     1617#else
    15031618        rc = usbLibVuGetDevices(&pDevInfos, &cDevInfos);
     1619#endif
    15041620        AssertRC(rc);
    15051621        if (RT_SUCCESS(rc))
     
    16031719    {
    16041720        DWORD dwErr = GetLastError();
    1605         AssertMsgFailed(("DeviceIoControl failed with dwErr (%u)\n", dwErr));
     1721        AssertFailed();
    16061722        LogRelFunc(("SUPUSBFLT_IOCTL_ADD_FILTER failed (dwErr=%u)!\n", dwErr));
    16071723        return NULL;
     
    16101726    if (RT_FAILURE(FltAddRc.rc))
    16111727    {
    1612         AssertMsgFailed(("Adding filter failed with %d\n", FltAddRc.rc));
     1728        AssertFailed();
    16131729        LogRelFunc(("Adding a USB filter failed with rc=%d!\n", FltAddRc.rc));
    16141730        return NULL;
     
    16451761    {
    16461762        DWORD dwErr = GetLastError();
    1647         AssertMsgFailed(("DeviceIoControl failed with LastError=%Rwa\n", dwErr));
     1763        AssertFailed();
    16481764        LogRelFunc(("SUPUSBFLT_IOCTL_REMOVE_FILTER failed (dwErr=%u)!\n", dwErr));
    16491765    }
     
    16641780    {
    16651781        DWORD dwErr = GetLastError();
    1666         AssertMsgFailed(("DeviceIoControl failed with dwErr (%u)\n", dwErr));
     1782        AssertFailed();
    16671783        LogRelFunc(("SUPUSBFLT_IOCTL_RUN_FILTERS failed (dwErr=%u)!\n", dwErr));
    16681784        return RTErrConvertFromWin32(dwErr);
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