Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.5 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. CREATE.C
  5. Abstract:
  6. This module contains the code to Find and Create files to generic USB
  7. devices
  8. Environment:
  9. User mode
  10. Revision History:
  11. Sept-01 : created by Kenneth Ray
  12. --*/
  13. #include <stdlib.h>
  14. #include <wtypes.h>
  15. #include <setupapi.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <winioctl.h>
  19. #include "genusbio.h"
  20. #include "umgusb.h"
  21. BOOL __stdcall
  22. GenUSB_FindKnownDevices (
  23. IN GENUSB_FIND_KNOWN_DEVICES_FILTER Filter,
  24. IN PVOID Context,
  25. OUT PGENUSB_DEVICE * Devices, // A array of struct _HID_DEVICE
  26. OUT PULONG NumberDevices // the length of this array.
  27. )
  28. /*++
  29. Routine Description:
  30. Do the required PnP things in order to find all the devices in
  31. the system at this time.
  32. --*/
  33. {
  34. HDEVINFO hardwareDeviceInfo = NULL;
  35. SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
  36. ULONG predictedLength = 0;
  37. ULONG requiredLength = 0, bytes=0;
  38. ULONG i, current;
  39. HKEY regkey;
  40. DWORD Err;
  41. //
  42. // Open a handle to the device interface information set of all
  43. // present toaster class interfaces.
  44. //
  45. *Devices = NULL;
  46. *NumberDevices = 0;
  47. hardwareDeviceInfo = SetupDiGetClassDevs (
  48. (LPGUID)&GUID_DEVINTERFACE_GENUSB,
  49. NULL, // Define no enumerator (global)
  50. NULL, // Define no parent
  51. (DIGCF_PRESENT | // Only Devices present
  52. DIGCF_DEVICEINTERFACE)); // Function class devices.
  53. if(INVALID_HANDLE_VALUE == hardwareDeviceInfo)
  54. {
  55. goto GenUSB_FIND_KNOWN_DEVICES_REJECT;
  56. }
  57. //
  58. // Enumerate devices
  59. //
  60. deviceInterfaceData.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
  61. for (i=0; TRUE; i++)
  62. {
  63. if (!SetupDiEnumDeviceInterfaces (
  64. hardwareDeviceInfo,
  65. 0, // No care about specific PDOs
  66. (LPGUID)&GUID_DEVINTERFACE_GENUSB,
  67. i, //
  68. &deviceInterfaceData))
  69. {
  70. if (ERROR_NO_MORE_ITEMS == GetLastError ())
  71. {
  72. break;
  73. }
  74. else
  75. {
  76. goto GenUSB_FIND_KNOWN_DEVICES_REJECT;
  77. }
  78. }
  79. }
  80. *NumberDevices = i;
  81. *Devices = malloc (sizeof (PGENUSB_DEVICE) * i);
  82. if (NULL == *Devices)
  83. {
  84. SetLastError (ERROR_NOT_ENOUGH_MEMORY);
  85. goto GenUSB_FIND_KNOWN_DEVICES_REJECT;
  86. }
  87. ZeroMemory (*Devices, (sizeof (PGENUSB_DEVICE) * i));
  88. for (i=0, current=0; i < *NumberDevices; i++, current++)
  89. {
  90. if (!SetupDiEnumDeviceInterfaces (
  91. hardwareDeviceInfo,
  92. 0, // No care about specific PDOs
  93. (LPGUID)&GUID_DEVINTERFACE_GENUSB,
  94. i, //
  95. &deviceInterfaceData))
  96. {
  97. goto GenUSB_FIND_KNOWN_DEVICES_REJECT;
  98. }
  99. regkey = SetupDiOpenDeviceInterfaceRegKey (
  100. hardwareDeviceInfo,
  101. &deviceInterfaceData,
  102. 0, // reserved
  103. STANDARD_RIGHTS_READ);
  104. if (INVALID_HANDLE_VALUE == regkey)
  105. {
  106. current--;
  107. continue;
  108. }
  109. if (!(*Filter)(regkey, Context))
  110. {
  111. current--;
  112. RegCloseKey (regkey);
  113. continue;
  114. }
  115. RegCloseKey (regkey);
  116. //
  117. // First find out required length of the buffer
  118. //
  119. SetupDiGetDeviceInterfaceDetail (
  120. hardwareDeviceInfo,
  121. &deviceInterfaceData,
  122. NULL, // probing so no output buffer yet
  123. 0, // probing so output buffer length of zero
  124. &requiredLength,
  125. NULL); // not interested in the specific dev-node
  126. Err = GetLastError();
  127. predictedLength = requiredLength;
  128. (*Devices)[current].DetailData = malloc (predictedLength);
  129. if (!(*Devices)[current].DetailData)
  130. {
  131. goto GenUSB_FIND_KNOWN_DEVICES_REJECT;
  132. }
  133. ((*Devices)[current].DetailData)->cbSize =
  134. sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
  135. if (! SetupDiGetDeviceInterfaceDetail (
  136. hardwareDeviceInfo,
  137. &deviceInterfaceData,
  138. (*Devices)[current].DetailData,
  139. predictedLength,
  140. &requiredLength,
  141. NULL))
  142. {
  143. Err = GetLastError();
  144. goto GenUSB_FIND_KNOWN_DEVICES_REJECT;
  145. }
  146. }
  147. *NumberDevices = current;
  148. SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
  149. hardwareDeviceInfo = NULL;
  150. return TRUE;
  151. GenUSB_FIND_KNOWN_DEVICES_REJECT:
  152. if (hardwareDeviceInfo)
  153. {
  154. SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
  155. }
  156. if (*Devices)
  157. {
  158. for (i=0; i < (*NumberDevices); i++)
  159. {
  160. if ((*Devices)[i].DetailData)
  161. {
  162. free ((*Devices)[i].DetailData);
  163. }
  164. }
  165. free (*Devices);
  166. }
  167. *Devices = NULL;
  168. *NumberDevices = 0;
  169. return FALSE;
  170. }