Changeset 34281 in vbox
- Timestamp:
- Nov 23, 2010 1:32:08 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/VBoxUSB/win/USBLib-win.cpp
r34274 r34281 543 543 }; 544 544 545 static ULONG TotalDevicesConnected; 546 547 static BOOL gDoConfigDesc = TRUE; 548 static int TotalHubs; 549 static PUSBDEVICE *ppDeviceList = NULL; 545 static ULONG g_TotalDevicesConnected; 546 static BOOL g_DoConfigDesc = TRUE; 547 static int g_TotalHubs; 548 static PUSBDEVICE *g_ppDeviceList = NULL; 550 549 551 550 PCHAR ConnectionStatuses[]; … … 620 619 pStrDesc = pStrDesc->Next; 621 620 } 622 if (* ppDeviceList == NULL)621 if (*g_ppDeviceList == NULL) 623 622 { 624 623 pDevice->pNext = NULL; 625 * ppDeviceList = pDevice;624 *g_ppDeviceList = pDevice; 626 625 } 627 626 else 628 627 { 629 pDevice->pNext = * ppDeviceList;630 * ppDeviceList = pDevice;628 pDevice->pNext = *g_ppDeviceList; 629 *g_ppDeviceList = pDevice; 631 630 } 632 631 } … … 699 698 PCHAR leafName; 700 699 701 TotalDevicesConnected = 0;702 TotalHubs = 0;703 ppDeviceList = ppDevices;700 g_TotalDevicesConnected = 0; 701 g_TotalHubs = 0; 702 g_ppDeviceList = ppDevices; 704 703 705 704 // Iterate over some Host Controller names and try to open them. … … 726 725 } 727 726 728 ppDeviceList = NULL;729 *DevicesConnected = TotalDevicesConnected -TotalHubs;727 g_ppDeviceList = NULL; 728 *DevicesConnected = g_TotalDevicesConnected - g_TotalHubs; 730 729 } 731 730 … … 1034 1033 // 1035 1034 if (connectionInfo->ConnectionStatus == DeviceConnected) 1036 { 1037 TotalDevicesConnected++; 1038 } 1035 g_TotalDevicesConnected++; 1039 1036 else 1040 1037 { … … 1044 1041 1045 1042 if (connectionInfo->DeviceIsHub) 1046 { 1047 TotalHubs++; 1048 } 1043 g_TotalHubs++; 1049 1044 1050 1045 // If there is a device connected, get the Device Description … … 1072 1067 // Configuration Descriptor from the device. 1073 1068 // 1074 if (g DoConfigDesc &&1069 if (g_DoConfigDesc && 1075 1070 connectionInfo->ConnectionStatus == DeviceConnected) 1076 1071 { … … 2695 2690 2696 2691 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); 2692 static void usbLibLogHostDevices(PUSBDEVICE pDevice) 2693 { 2717 2694 #ifdef LOG_ENABLED 2718 Log(("usbLibGetDevices: Detected %d host devices\n", cHostDevices));2719 pDevice = pHostDevices;2720 2695 int iDevice = 0; 2721 2696 while (pDevice) 2722 2697 { 2723 2698 iDevice++; 2724 Log(("Detected hostdevice: #%d\n", iDevice));2699 Log(("Detected device: #%d\n", iDevice)); 2725 2700 Log((" Vendor Id: 0x%04X\n", pDevice->idVendor)); 2726 2701 Log((" Product Id: 0x%04X\n", pDevice->idProduct)); 2727 2702 Log((" Revision: 0x%04X\n", pDevice->bcdDevice)); 2728 2703 Log((" Address: %s\n", pDevice->pszAddress)); 2704 Log((" AltAddress: %s\n", pDevice->pszAltAddress)); 2729 2705 Log((" HubName: %s\n", pDevice->pszHubName)); 2730 2706 Log((" Port: %u\n", pDevice->bPort)); … … 2755 2731 break; 2756 2732 } 2757 2758 2733 pDevice = pDevice->pNext; 2759 2734 } 2760 2735 #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 */ 2746 USBLIB_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; 2763 2771 *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++) 2772 2773 { 2773 2774 /* … … 2777 2778 if (!DeviceIoControl(g_hUSBMonitor, SUPUSBFLT_IOCTL_GET_NUM_DEVICES, NULL, 0, &numdev, sizeof(numdev), &cbReturned, NULL)) 2778 2779 { 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; 2781 2803 } 2782 2804 2783 2805 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) 2785 2809 break; 2786 2810 … … 2801 2825 *ppDevices = pHostDevices; 2802 2826 *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)); 2804 2828 return VINF_SUCCESS; 2805 2829 } 2806 2830 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 */ 2814 2835 uint32_t cCaptured = 0; 2815 2836 for (uint32_t i = 0; i < numdev.cUSBDevices; i++) … … 2818 2839 HANDLE hDev; 2819 2840 2820 char *pszDev name = usblibQueryDeviceName(i);2821 hDev = CreateFile(pszDev name, 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, 2822 2843 OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL); 2823 2844 … … 2834 2855 else 2835 2856 { 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)); 2853 2892 } 2854 2855 2893 CloseHandle(hDev); 2856 2894 } 2857 2895 else 2858 AssertMsgFailed(("Unexpected failure to open %s. LastError=%Rwa\n", pszDev name, GetLastError()));2896 AssertMsgFailed(("Unexpected failure to open %s. LastError=%Rwa\n", pszDevName, GetLastError())); 2859 2897 } 2860 2898 … … 2864 2902 *ppDevices = pHostDevices; 2865 2903 *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)); 2867 2905 return VINF_SUCCESS; 2868 2906 } 2869 2907 2870 /* 3: Go through the list of captured devices, lookup the corresponding device2871 * 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 else2902 {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 }2913 2908 *pcDevices = cHostDevices; 2914 2909 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 2976 2913 *ppDevices = pHostDevices; 2977 2914 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;3018 2915 } 3019 2916
Note:
See TracChangeset
for help on using the changeset viewer.