VirtualBox

Changeset 34281 in vbox


Ignore:
Timestamp:
Nov 23, 2010 1:32:08 PM (14 years ago)
Author:
vboxsync
Message:

USBLib-win: cleanup code (merge step 2 and 3)

File:
1 edited

Legend:

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

    r34274 r34281  
    543543};
    544544
    545 static ULONG TotalDevicesConnected;
    546 
    547 static BOOL gDoConfigDesc = TRUE;
    548 static int TotalHubs;
    549 static PUSBDEVICE *ppDeviceList = NULL;
     545static ULONG       g_TotalDevicesConnected;
     546static BOOL        g_DoConfigDesc = TRUE;
     547static int         g_TotalHubs;
     548static PUSBDEVICE *g_ppDeviceList = NULL;
    550549
    551550PCHAR ConnectionStatuses[];
     
    620619                pStrDesc = pStrDesc->Next;
    621620            }
    622             if (*ppDeviceList == NULL)
     621            if (*g_ppDeviceList == NULL)
    623622            {
    624623                pDevice->pNext = NULL;
    625                 *ppDeviceList = pDevice;
     624                *g_ppDeviceList = pDevice;
    626625            }
    627626            else
    628627            {
    629                 pDevice->pNext = *ppDeviceList;
    630                 *ppDeviceList = pDevice;
     628                pDevice->pNext = *g_ppDeviceList;
     629                *g_ppDeviceList = pDevice;
    631630            }
    632631        }
     
    699698    PCHAR       leafName;
    700699
    701     TotalDevicesConnected = 0;
    702     TotalHubs = 0;
    703     ppDeviceList = ppDevices;
     700    g_TotalDevicesConnected = 0;
     701    g_TotalHubs = 0;
     702    g_ppDeviceList = ppDevices;
    704703
    705704    // Iterate over some Host Controller names and try to open them.
     
    726725    }
    727726
    728     ppDeviceList = NULL;
    729     *DevicesConnected = TotalDevicesConnected - TotalHubs;
     727    g_ppDeviceList = NULL;
     728    *DevicesConnected = g_TotalDevicesConnected - g_TotalHubs;
    730729}
    731730
     
    10341033        //
    10351034        if (connectionInfo->ConnectionStatus == DeviceConnected)
    1036         {
    1037             TotalDevicesConnected++;
    1038         }
     1035            g_TotalDevicesConnected++;
    10391036        else
    10401037        {
     
    10441041
    10451042        if (connectionInfo->DeviceIsHub)
    1046         {
    1047             TotalHubs++;
    1048         }
     1043            g_TotalHubs++;
    10491044
    10501045        // If there is a device connected, get the Device Description
     
    10721067        // Configuration Descriptor from the device.
    10731068        //
    1074         if (gDoConfigDesc &&
     1069        if (g_DoConfigDesc &&
    10751070            connectionInfo->ConnectionStatus == DeviceConnected)
    10761071        {
     
    26952690
    26962691
    2697 /**
    2698  * Return all attached USB devices that are captured by the filter
    2699  *
    2700  * @returns VBox status code
    2701  * @param ppDevices         Receives pointer to list of devices
    2702  * @param pcDevices         Number of USB devices in the list
    2703  */
    2704 USBLIB_DECL(int) USBLibGetDevices(PUSBDEVICE *ppDevices, uint32_t *pcDevices)
    2705 {
    2706     USBSUP_GETNUMDEV numdev;
    2707     PUSBDEVICE pDevice = NULL;
    2708     Log(("usbLibGetDevices: enter\n"));
    2709 
    2710     if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
    2711         return VERR_NOT_SUPPORTED;
    2712 
    2713     /* 1: Enumerate all usb devices attached to the host */
    2714     PUSBDEVICE pHostDevices = NULL;
    2715     uint32_t   cHostDevices = 0;
    2716     usbLibEnumerateHostControllers(&pHostDevices, &cHostDevices);
     2692static void usbLibLogHostDevices(PUSBDEVICE pDevice)
     2693{
    27172694#ifdef LOG_ENABLED
    2718     Log(("usbLibGetDevices: Detected %d host devices\n", cHostDevices));
    2719     pDevice = pHostDevices;
    27202695    int iDevice = 0;
    27212696    while (pDevice)
    27222697    {
    27232698        iDevice++;
    2724         Log(("Detected host device: #%d\n", iDevice));
     2699        Log(("Detected device: #%d\n", iDevice));
    27252700        Log((" Vendor Id:       0x%04X\n", pDevice->idVendor));
    27262701        Log((" Product Id:      0x%04X\n", pDevice->idProduct));
    27272702        Log((" Revision:        0x%04X\n", pDevice->bcdDevice));
    27282703        Log((" Address:         %s\n", pDevice->pszAddress));
     2704        Log((" AltAddress:      %s\n", pDevice->pszAltAddress));
    27292705        Log((" HubName:         %s\n", pDevice->pszHubName));
    27302706        Log((" Port:            %u\n", pDevice->bPort));
     
    27552731            break;
    27562732        }
    2757 
    27582733        pDevice = pDevice->pNext;
    27592734    }
    27602735#endif
    2761 
    2762     *ppDevices = 0;
     2736}
     2737
     2738
     2739/**
     2740 * Return all attached USB devices that are captured by the filter
     2741 *
     2742 * @returns VBox status code
     2743 * @param ppDevices         Receives pointer to list of devices
     2744 * @param pcDevices         Number of USB devices in the list
     2745 */
     2746USBLIB_DECL(int) USBLibGetDevices(PUSBDEVICE *ppDevices, uint32_t *pcDevices)
     2747{
     2748    Log(("usbLibGetDevices: enter\n"));
     2749    int rc = VINF_SUCCESS;
     2750
     2751    if (g_hUSBMonitor == INVALID_HANDLE_VALUE)
     2752        return VERR_NOT_SUPPORTED;
     2753
     2754    /*
     2755     * 1: Enumerate all USB devices attached to the host.
     2756     */
     2757    PUSBDEVICE pHostDevices = NULL;
     2758    uint32_t   cHostDevices = 0;
     2759    usbLibEnumerateHostControllers(&pHostDevices, &cHostDevices);
     2760
     2761    Log(("usbLibGetDevices: Detected %d host devices\n", cHostDevices));
     2762    usbLibLogHostDevices(pHostDevices);
     2763
     2764    /*
     2765     * Get the return data. Note that we might be called a bit too early here.
     2766     * Give Windows time to register the new USB driver/device. It's no problem
     2767     * to block here as we're in the async usb detection thread (not EMT).
     2768     */
     2769    USBSUP_GETNUMDEV numdev;
     2770    *ppDevices = NULL;
    27632771    *pcDevices = 0;
    2764 
    2765     /*
    2766      * Get the return data.
    2767      * Note that we might be called a bit too early here. Give windows time to register the new USB driver/device.
    2768      * It's no problem to block here as we're in the async usb detection thread (not EMT)
    2769      */
    2770 
    2771     for (int i = 0; i < 100; i++)
     2772    for (uint32_t i = 0; i < 100; i++)
    27722773    {
    27732774        /*
     
    27772778        if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_GET_NUM_DEVICES, NULL, 0, &numdev, sizeof(numdev), &cbReturned, NULL))
    27782779        {
    2779             AssertMsgFailed(("DeviceIoControl failed with LastError=%Rwa\n", GetLastError()));
    2780             return RTErrConvertFromWin32(GetLastError());
     2780            DWORD LastError = GetLastError();
     2781            AssertMsgFailed(("DeviceIoControl failed with LastError=%Rwa\n", LastError));
     2782            /* Free the host devices */
     2783            PUSBDEVICE pDevice = pHostDevices;
     2784            while (pDevice)
     2785            {
     2786                PUSBDEVICE pNext = pDevice->pNext;
     2787
     2788                RTStrFree((char*)pDevice->pszAddress);
     2789                RTStrFree(pDevice->pszAltAddress);
     2790                RTStrFree(pDevice->pszHubName);
     2791                RTStrFree((char*)pDevice->pszManufacturer);
     2792                RTStrFree((char*)pDevice->pszProduct);
     2793                RTStrFree((char*)pDevice->pszSerialNumber);
     2794                RTMemFree(pDevice);
     2795
     2796                pDevice = pNext;
     2797            }
     2798
     2799            *ppDevices = NULL;
     2800            int rc = RTErrConvertFromWin32(LastError);
     2801            Log(("usbLibGetDevices: returns %Rrc\n", rc));
     2802            return rc;
    27812803        }
    27822804
    27832805        AssertMsg((int32_t)numdev.cUSBDevices >= 0, ("%d", numdev.cUSBDevices));
    2784         if ((int32_t)numdev.cUSBDevices <= 0) /** @todo why does this return -1. Happened here when detaching a captured device which hadn't yet been opened by a VM process. */
     2806        /* This can be < 0 when detaching a captured device which hadn't yet been
     2807         * opened by a VM process. */
     2808        if ((int32_t)numdev.cUSBDevices <= 0)
    27852809            break;
    27862810
     
    28012825        *ppDevices = pHostDevices;
    28022826        *pcDevices = cHostDevices;
    2803         Log(("usbLibGetDevices: returns %d host device - no captured devices\n", cHostDevices));
     2827        Log(("usbLibGetDevices: returns %d host devices - no captured devices\n", cHostDevices));
    28042828        return VINF_SUCCESS;
    28052829    }
    28062830
    2807     /* 2: Get all the USB devices that the filter has captured for us */
    2808 
    2809     /* Get the required info for each captured device */
    2810     PUSBDEVICE *ppCaptured = (PUSBDEVICE*)RTMemAllocZ(sizeof(PUSBDEVICE) * numdev.cUSBDevices);
    2811     if (!ppCaptured)
    2812         goto failure;
    2813 
     2831    /*
     2832     * 2: Get all the USB devices that the filter has captured for us. Then
     2833     *    update the corresponding device information in the host devices.
     2834     */
    28142835    uint32_t cCaptured = 0;
    28152836    for (uint32_t i = 0; i < numdev.cUSBDevices; i++)
     
    28182839        HANDLE hDev;
    28192840
    2820         char *pszDevname = usblibQueryDeviceName(i);
    2821         hDev = CreateFile(pszDevname, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL,
     2841        char *pszDevName = usblibQueryDeviceName(i);
     2842        hDev = CreateFile(pszDevName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL,
    28222843                          OPEN_EXISTING,  FILE_ATTRIBUTE_SYSTEM, NULL);
    28232844
     
    28342855            else
    28352856            {
    2836                 pDevice = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
    2837                 if (!pDevice)
    2838                     goto failure;
    2839 
    2840                 pDevice->idVendor      = dev.vid;
    2841                 pDevice->idProduct     = dev.did;
    2842                 pDevice->bcdDevice     = dev.rev;
    2843                 pDevice->bBus          = 0;
    2844                 pDevice->u64SerialHash = USBLibHashSerial(dev.serial_hash);
    2845                 pDevice->enmState      = USBDEVICESTATE_HELD_BY_PROXY;
    2846                 pDevice->pszAddress    = RTStrDup(pszDevname);
    2847                 /* The following is not 100% accurate but we only care about high-speed vs non-high-speed */
    2848                 pDevice->enmSpeed      = dev.fHiSpeed ? USBDEVICESPEED_HIGH : USBDEVICESPEED_FULL;
    2849                 Log(("usbLibGetDevices: Detected device vid=%x did=%x rev=%x hispd=%d hash=%s hash=%RX64\n", dev.vid, dev.did, dev.rev, dev.fHiSpeed, dev.serial_hash, pDevice->u64SerialHash));
    2850 
    2851                 ppCaptured[i] = pDevice;
    2852                 cCaptured++;
     2857                /* Now search the captured device in the host list and update
     2858                 * the information there */
     2859                char *pszDeviceRegPath = usblibQueryDeviceRegPath(i);
     2860                if (pszDeviceRegPath)
     2861                {
     2862                    PUSBDEVICE pDevice = pHostDevices;
     2863                    uint32_t j;
     2864                    for (j = 0; j < cHostDevices; j++)
     2865                    {
     2866                        if (!strcmp(pszDeviceRegPath, pDevice->pszAddress))
     2867                        {
     2868                            Log(("usbLibGetDevices: Found captured device in host list %s (%s)\n", pszDeviceRegPath, pszDevName));
     2869
     2870                            pDevice->enmState = USBDEVICESTATE_HELD_BY_PROXY;
     2871                            /* The following is not 100% accurate but we only care about high-speed vs. non-high-speed */
     2872                            pDevice->enmSpeed = dev.fHiSpeed ? USBDEVICESPEED_HIGH : USBDEVICESPEED_FULL;
     2873                            RTStrFree(pDevice->pszAltAddress);
     2874                            pDevice->pszAltAddress = (char *)pDevice->pszAddress;
     2875                            pDevice->pszAddress = RTStrDup(pszDevName);
     2876                        }
     2877                        pDevice = pDevice->pNext;
     2878                    }
     2879                    cCaptured++;
     2880
     2881                    if (j == cHostDevices)
     2882                    {
     2883                        Assert(!pDevice);
     2884
     2885                        /* Probably in the process of being reattached */
     2886                        Log(("usbLibGetDevices: Captured device %s not found in host device list\n", pszDeviceRegPath));
     2887                        /* do nothing */
     2888                    }
     2889                }
     2890                else
     2891                    Log(("usbLibGetDevices: Cannot determinet the path to %s\n", pszDevName));
    28532892            }
    2854 
    28552893            CloseHandle(hDev);
    28562894        }
    28572895        else
    2858             AssertMsgFailed(("Unexpected failure to open %s. LastError=%Rwa\n", pszDevname, GetLastError()));
     2896            AssertMsgFailed(("Unexpected failure to open %s. LastError=%Rwa\n", pszDevName, GetLastError()));
    28592897    }
    28602898
     
    28642902        *ppDevices = pHostDevices;
    28652903        *pcDevices = cHostDevices;
    2866         Log(("usbLibGetDevices: returns %d host device - no captured devices after all\n", cHostDevices));
     2904        Log(("usbLibGetDevices: returns %d host devices - no captured devices after all\n", cHostDevices));
    28672905        return VINF_SUCCESS;
    28682906    }
    28692907
    2870     /* 3: Go through the list of captured devices, lookup the corresponding device
    2871      *    in the list of host devices and update the information there. */
    2872     Assert(pHostDevices);
    2873     for (uint32_t i = 0; i < numdev.cUSBDevices; i++)
    2874     {
    2875         char *pszDeviceRegPath = usblibQueryDeviceRegPath(i);
    2876         Assert(pszDeviceRegPath);
    2877 
    2878         pDevice = pHostDevices;
    2879         uint32_t j;
    2880         for (j = 0; j < cHostDevices; j++)
    2881         {
    2882             if (pszDeviceRegPath)
    2883             {
    2884                 if (!strcmp(pszDeviceRegPath, pDevice->pszAddress))
    2885                 {
    2886                     Log(("usbLibGetDevices: Duplicate device %s (%s)\n", pszDeviceRegPath, usblibQueryDeviceName(i)));
    2887                     break;
    2888                 }
    2889             }
    2890             pDevice = pDevice->pNext;
    2891         }
    2892 
    2893         if (j == cHostDevices)
    2894         {
    2895             Assert(!pDevice);
    2896 
    2897             /* Probably in the process of being reattached */
    2898             Log(("usbLibGetDevices: Captured device %s not found in host device list\n", usblibQueryDeviceRegPath(i)));
    2899             /* do nothing */
    2900         }
    2901         else
    2902         {
    2903             Assert(pDevice);
    2904 
    2905             pDevice->enmState = USBDEVICESTATE_HELD_BY_PROXY;
    2906             if (ppCaptured[i])
    2907                 pDevice->enmSpeed = ppCaptured[i]->enmSpeed;    // @todo: Is that right?
    2908             RTStrFree(pDevice->pszAltAddress);
    2909             pDevice->pszAltAddress = (char *)pDevice->pszAddress;
    2910             pDevice->pszAddress = RTStrDup(usblibQueryDeviceName(i));
    2911         }
    2912     }
    29132908    *pcDevices = cHostDevices;
    29142909
    2915     /* Free captured devices list */
    2916     for (uint32_t i = 0; i < numdev.cUSBDevices; i++)
    2917     {
    2918         pDevice = ppCaptured[i];
    2919         if (pDevice)
    2920         {
    2921             RTStrFree((char*)pDevice->pszAddress);
    2922             RTStrFree(pDevice->pszAltAddress);
    2923             RTStrFree(pDevice->pszHubName);
    2924             RTStrFree((char*)pDevice->pszManufacturer);
    2925             RTStrFree((char*)pDevice->pszProduct);
    2926             RTStrFree((char*)pDevice->pszSerialNumber);
    2927             RTMemFree(pDevice);
    2928         }
    2929     }
    2930     RTMemFree(ppCaptured);
    2931 
    2932     Log(("usbLibGetDevices: returns %d devices\n", *pcDevices));
    2933 #ifdef LOG_ENABLED
    2934     pDevice = pHostDevices;
    2935     iDevice = 0;
    2936     while (pDevice)
    2937     {
    2938         iDevice++;
    2939         Log(("Detected device: #%d\n", iDevice));
    2940         Log((" Vendor Id:       0x%04X\n", pDevice->idVendor));
    2941         Log((" Product Id:      0x%04X\n", pDevice->idProduct));
    2942         Log((" Revision:        0x%04X\n", pDevice->bcdDevice));
    2943         Log((" Address:         %s\n", pDevice->pszAddress));
    2944         Log((" AltAddress:      %s\n", pDevice->pszAltAddress));
    2945         Log((" HubName:         %s\n", pDevice->pszHubName));
    2946         Log((" Port:            %u\n", pDevice->bPort));
    2947         Log((" Manufacturer:    %s\n", pDevice->pszManufacturer));
    2948         Log((" Product:         %s\n", pDevice->pszProduct));
    2949         if (pDevice->pszSerialNumber)
    2950             Log((" Serial Nr.:      %s\n", pDevice->pszSerialNumber));
    2951 
    2952         switch(pDevice->enmState)
    2953         {
    2954         case USBDEVICESTATE_UNSUPPORTED:
    2955             Log((" State            USBDEVICESTATE_UNSUPPORTED\n"));
    2956             break;
    2957         case USBDEVICESTATE_USED_BY_HOST:
    2958             Log((" State            USBDEVICESTATE_USED_BY_HOST\n"));
    2959             break;
    2960         case USBDEVICESTATE_USED_BY_HOST_CAPTURABLE:
    2961             Log((" State            USBDEVICESTATE_USED_BY_HOST_CAPTURABLE\n"));
    2962             break;
    2963         case USBDEVICESTATE_UNUSED:
    2964             Log((" State            USBDEVICESTATE_UNUSED\n"));
    2965             break;
    2966         case USBDEVICESTATE_HELD_BY_PROXY:
    2967             Log((" State            USBDEVICESTATE_HELD_BY_PROXY\n"));
    2968             break;
    2969         case USBDEVICESTATE_USED_BY_GUEST:
    2970             Log((" State            USBDEVICESTATE_USED_BY_GUEST\n"));
    2971             break;
    2972         }
    2973         pDevice = pDevice->pNext;
    2974     }
    2975 #endif
     2910    Log(("usbLibGetDevices: returns %d devices (%d captured)\n", *pcDevices, cCaptured));
     2911    usbLibLogHostDevices(pHostDevices);
     2912
    29762913    *ppDevices = pHostDevices;
    29772914    return VINF_SUCCESS;
    2978 
    2979 failure:
    2980     /* free host devices */
    2981     pDevice      = pHostDevices;
    2982     pHostDevices = NULL;
    2983     while (pDevice)
    2984     {
    2985         PUSBDEVICE pNext = pDevice->pNext;
    2986 
    2987         RTStrFree((char*)pDevice->pszAddress);
    2988         RTStrFree(pDevice->pszAltAddress);
    2989         RTStrFree(pDevice->pszHubName);
    2990         RTStrFree((char*)pDevice->pszManufacturer);
    2991         RTStrFree((char*)pDevice->pszProduct);
    2992         RTStrFree((char*)pDevice->pszSerialNumber);
    2993         RTMemFree(pDevice);
    2994 
    2995         pDevice = pNext;
    2996     }
    2997 
    2998     /* free captured devices */
    2999     for (uint32_t i = 0; i < numdev.cUSBDevices; i++)
    3000     {
    3001         pDevice = ppCaptured[i];
    3002         if (pDevice)
    3003         {
    3004             RTStrFree((char*)pDevice->pszAddress);
    3005             RTStrFree(pDevice->pszAltAddress);
    3006             RTStrFree(pDevice->pszHubName);
    3007             RTStrFree((char*)pDevice->pszManufacturer);
    3008             RTStrFree((char*)pDevice->pszProduct);
    3009             RTStrFree((char*)pDevice->pszSerialNumber);
    3010             RTMemFree(pDevice);
    3011         }
    3012     }
    3013     RTMemFree(ppCaptured);
    3014 
    3015     *ppDevices = NULL;
    3016     Log(("usbLibGetDevices: returns VERR_NO_MEMORY\n"));
    3017     return VERR_NO_MEMORY;
    30182915}
    30192916
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