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.

311 lines
8.7 KiB

  1. /*++
  2. Module Name:
  3. RECONFIG.C
  4. Abstract:
  5. This source file contains routines for exercising the I82930.SYS
  6. test driver.
  7. Environment:
  8. user mode
  9. Copyright (c) 1996-2001 Microsoft Corporation. All Rights Reserved.
  10. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  11. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  13. PURPOSE.
  14. --*/
  15. //*****************************************************************************
  16. // I N C L U D E S
  17. //*****************************************************************************
  18. #include <windows.h>
  19. #include <basetyps.h>
  20. #include <setupapi.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <devioctl.h>
  24. #include <string.h>
  25. #include <initguid.h>
  26. #include <usb100.h>
  27. #include "ioctl.h"
  28. #pragma intrinsic(strlen, strcpy)
  29. //*****************************************************************************
  30. // T Y P E D E F S
  31. //*****************************************************************************
  32. typedef struct _DEVICENODE
  33. {
  34. struct _DEVICENODE *Next;
  35. CHAR DevicePath[0];
  36. } DEVICENODE, *PDEVICENODE;
  37. //*****************************************************************************
  38. // F U N C T I O N P R O T O T Y P E S
  39. //*****************************************************************************
  40. PDEVICENODE
  41. EnumDevices (
  42. LPGUID Guid
  43. );
  44. VOID ReconfigureDevice (
  45. PCHAR DevicePath,
  46. ULONG MPSCount,
  47. ULONG MPS[]
  48. );
  49. //*****************************************************************************
  50. //
  51. // main()
  52. //
  53. //*****************************************************************************
  54. int _cdecl
  55. main(
  56. int argc,
  57. char *argv[]
  58. )
  59. {
  60. PDEVICENODE deviceNode;
  61. PDEVICENODE deviceNodeNext;
  62. ULONG devInstance;
  63. ULONG devCount;
  64. ULONG mpsCount;
  65. ULONG mps[15];
  66. devInstance = 1; // set this with cmd line arg
  67. for (mpsCount = 0;
  68. (mpsCount < (ULONG)(argc-1)) && (mpsCount < 15);
  69. mpsCount++)
  70. {
  71. mps[mpsCount] = atoi(argv[mpsCount+1]);
  72. }
  73. deviceNode = EnumDevices((LPGUID)&GUID_CLASS_I82930);
  74. devCount = 0;
  75. while (deviceNode)
  76. {
  77. devCount++;
  78. if (devCount == devInstance)
  79. {
  80. ReconfigureDevice(deviceNode->DevicePath,
  81. mpsCount,
  82. mps);
  83. }
  84. deviceNodeNext = deviceNode->Next;
  85. GlobalFree(deviceNode);
  86. deviceNode = deviceNodeNext;
  87. }
  88. return 0;
  89. }
  90. //*****************************************************************************
  91. //
  92. // EnumDevices()
  93. //
  94. //*****************************************************************************
  95. PDEVICENODE
  96. EnumDevices (
  97. LPGUID Guid
  98. )
  99. {
  100. HDEVINFO deviceInfo;
  101. SP_DEVICE_INTERFACE_DATA deviceInfoData;
  102. PSP_DEVICE_INTERFACE_DETAIL_DATA deviceDetailData;
  103. ULONG index;
  104. ULONG requiredLength;
  105. PDEVICENODE deviceNode;
  106. PDEVICENODE deviceNodeHead;
  107. deviceNodeHead = NULL;
  108. deviceInfo = SetupDiGetClassDevs(Guid,
  109. NULL,
  110. NULL,
  111. (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
  112. deviceInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  113. for (index=0;
  114. SetupDiEnumDeviceInterfaces(deviceInfo,
  115. 0,
  116. Guid,
  117. index,
  118. &deviceInfoData);
  119. index++)
  120. {
  121. SetupDiGetDeviceInterfaceDetail(deviceInfo,
  122. &deviceInfoData,
  123. NULL,
  124. 0,
  125. &requiredLength,
  126. NULL);
  127. deviceDetailData = GlobalAlloc(GPTR, requiredLength);
  128. deviceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  129. SetupDiGetDeviceInterfaceDetail(deviceInfo,
  130. &deviceInfoData,
  131. deviceDetailData,
  132. requiredLength,
  133. &requiredLength,
  134. NULL);
  135. requiredLength = sizeof(DEVICENODE) +
  136. strlen(deviceDetailData->DevicePath) + 1;
  137. deviceNode = GlobalAlloc(GPTR, requiredLength);
  138. strcpy(deviceNode->DevicePath, deviceDetailData->DevicePath);
  139. deviceNode->Next = deviceNodeHead;
  140. deviceNodeHead = deviceNode;
  141. GlobalFree(deviceDetailData);
  142. }
  143. SetupDiDestroyDeviceInfoList(deviceInfo);
  144. return deviceNodeHead;
  145. }
  146. //*****************************************************************************
  147. //
  148. // ShowDeviceInfo()
  149. //
  150. //*****************************************************************************
  151. VOID ReconfigureDevice (
  152. PCHAR DevicePath,
  153. ULONG MPSCount,
  154. ULONG MPS[]
  155. )
  156. {
  157. HANDLE devHandle;
  158. BOOL success;
  159. int size;
  160. int nBytes;
  161. PUSB_CONFIGURATION_DESCRIPTOR configDesc;
  162. PUSB_INTERFACE_DESCRIPTOR interfaceDesc;
  163. PUSB_ENDPOINT_DESCRIPTOR endpointDesc;
  164. ULONG i;
  165. devHandle = CreateFile(DevicePath,
  166. GENERIC_WRITE | GENERIC_READ,
  167. FILE_SHARE_WRITE | FILE_SHARE_READ,
  168. NULL,
  169. OPEN_EXISTING,
  170. 0,
  171. NULL);
  172. if (devHandle == INVALID_HANDLE_VALUE)
  173. {
  174. printf("Unable to open device:%s\n", DevicePath);
  175. return;
  176. }
  177. else
  178. {
  179. printf("Device: %s\n", DevicePath);
  180. }
  181. for (i = 0; i < MPSCount; i++)
  182. {
  183. printf("MPS[%2d] = %4d\n", i, MPS[i]);
  184. }
  185. size = sizeof(USB_CONFIGURATION_DESCRIPTOR) +
  186. sizeof(USB_INTERFACE_DESCRIPTOR) +
  187. sizeof(USB_ENDPOINT_DESCRIPTOR) * MPSCount;
  188. configDesc = GlobalAlloc(GPTR, size);
  189. if (configDesc == NULL)
  190. {
  191. return;
  192. }
  193. //
  194. // Initialize the Configuration Descriptor
  195. //
  196. configDesc->bLength = sizeof(USB_CONFIGURATION_DESCRIPTOR);
  197. configDesc->bDescriptorType = USB_CONFIGURATION_DESCRIPTOR_TYPE;
  198. configDesc->wTotalLength = (USHORT)size;
  199. configDesc->bNumInterfaces = 1;
  200. configDesc->bConfigurationValue = 1;
  201. configDesc->iConfiguration = 0;
  202. configDesc->bmAttributes = USB_CONFIG_BUS_POWERED |
  203. USB_CONFIG_SELF_POWERED;
  204. configDesc->MaxPower = 0;
  205. //
  206. // Initialize the Interface Descriptor
  207. //
  208. interfaceDesc = (PUSB_INTERFACE_DESCRIPTOR)(configDesc + 1);
  209. interfaceDesc->bLength = sizeof(USB_INTERFACE_DESCRIPTOR);
  210. interfaceDesc->bDescriptorType = USB_INTERFACE_DESCRIPTOR_TYPE;
  211. interfaceDesc->bInterfaceNumber = 0;
  212. interfaceDesc->bAlternateSetting = 0;
  213. interfaceDesc->bNumEndpoints = (UCHAR)MPSCount;
  214. interfaceDesc->bInterfaceClass = 0xFF;
  215. interfaceDesc->bInterfaceSubClass = 0xFF;
  216. interfaceDesc->bInterfaceProtocol = 0xFF;
  217. interfaceDesc->iInterface = 0;
  218. //
  219. // Initialize the Endpoint Descriptors
  220. //
  221. endpointDesc = (PUSB_ENDPOINT_DESCRIPTOR)(interfaceDesc + 1);
  222. for (i = 0; i < MPSCount; i++)
  223. {
  224. endpointDesc->bLength = sizeof(USB_ENDPOINT_DESCRIPTOR);
  225. endpointDesc->bDescriptorType = USB_ENDPOINT_DESCRIPTOR_TYPE;
  226. endpointDesc->bEndpointAddress = (UCHAR)(i + 1);
  227. endpointDesc->bmAttributes = USB_ENDPOINT_TYPE_ISOCHRONOUS;
  228. endpointDesc->wMaxPacketSize = (USHORT)MPS[i];
  229. endpointDesc->bInterval = 0;
  230. endpointDesc++;
  231. }
  232. //
  233. // Set Configuration Descriptor
  234. //
  235. success = DeviceIoControl(devHandle,
  236. IOCTL_I82930_SET_CONFIG_DESCRIPTOR,
  237. configDesc,
  238. size,
  239. NULL,
  240. 0,
  241. &nBytes,
  242. NULL);
  243. if (success)
  244. {
  245. printf("Reconfigured device\n");
  246. }
  247. GlobalFree(configDesc);
  248. CloseHandle(devHandle);
  249. }