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.

558 lines
15 KiB

  1. /*++
  2. Module Name:
  3. CONFIG.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-1998 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 <devioctl.h>
  23. #include <string.h>
  24. #include <initguid.h>
  25. #include <usb100.h>
  26. #include "ioctl.h"
  27. #pragma intrinsic(strlen, strcpy)
  28. //*****************************************************************************
  29. // T Y P E D E F S
  30. //*****************************************************************************
  31. typedef struct _DEVICENODE
  32. {
  33. struct _DEVICENODE *Next;
  34. CHAR DevicePath[0];
  35. } DEVICENODE, *PDEVICENODE;
  36. //*****************************************************************************
  37. // F U N C T I O N P R O T O T Y P E S
  38. //*****************************************************************************
  39. PDEVICENODE
  40. EnumDevices (
  41. LPGUID Guid
  42. );
  43. VOID ShowDeviceInfo (
  44. PCHAR DevicePath
  45. );
  46. VOID
  47. ShowDeviceDesc (
  48. PUSB_DEVICE_DESCRIPTOR DeviceDesc
  49. );
  50. VOID
  51. ShowConfigDesc (
  52. PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc
  53. );
  54. VOID
  55. ShowConfigurationDescriptor (
  56. PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc
  57. );
  58. VOID
  59. ShowInterfaceDescriptor (
  60. PUSB_INTERFACE_DESCRIPTOR InterfaceDesc
  61. );
  62. VOID
  63. ShowEndpointDescriptor (
  64. PUSB_ENDPOINT_DESCRIPTOR EndpointDesc
  65. );
  66. //*****************************************************************************
  67. //
  68. // main()
  69. //
  70. //*****************************************************************************
  71. int _cdecl
  72. main(
  73. int argc,
  74. char *argv[]
  75. )
  76. {
  77. PDEVICENODE deviceNode;
  78. PDEVICENODE deviceNodeNext;
  79. deviceNode = EnumDevices((LPGUID)&GUID_CLASS_I82930);
  80. while (deviceNode)
  81. {
  82. ShowDeviceInfo(deviceNode->DevicePath);
  83. deviceNodeNext = deviceNode->Next;
  84. GlobalFree(deviceNode);
  85. deviceNode = deviceNodeNext;
  86. }
  87. return 0;
  88. }
  89. //*****************************************************************************
  90. //
  91. // EnumDevices()
  92. //
  93. //*****************************************************************************
  94. PDEVICENODE
  95. EnumDevices (
  96. LPGUID Guid
  97. )
  98. {
  99. HDEVINFO deviceInfo;
  100. SP_DEVICE_INTERFACE_DATA deviceInfoData;
  101. PSP_DEVICE_INTERFACE_DETAIL_DATA deviceDetailData;
  102. ULONG index;
  103. ULONG requiredLength;
  104. PDEVICENODE deviceNode;
  105. PDEVICENODE deviceNodeHead;
  106. deviceNodeHead = NULL;
  107. deviceInfo = SetupDiGetClassDevs(Guid,
  108. NULL,
  109. NULL,
  110. (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
  111. deviceInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  112. for (index=0;
  113. SetupDiEnumDeviceInterfaces(deviceInfo,
  114. 0,
  115. Guid,
  116. index,
  117. &deviceInfoData);
  118. index++)
  119. {
  120. SetupDiGetDeviceInterfaceDetail(deviceInfo,
  121. &deviceInfoData,
  122. NULL,
  123. 0,
  124. &requiredLength,
  125. NULL);
  126. deviceDetailData = GlobalAlloc(GPTR, requiredLength);
  127. deviceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  128. SetupDiGetDeviceInterfaceDetail(deviceInfo,
  129. &deviceInfoData,
  130. deviceDetailData,
  131. requiredLength,
  132. &requiredLength,
  133. NULL);
  134. requiredLength = sizeof(DEVICENODE) +
  135. strlen(deviceDetailData->DevicePath) + 1;
  136. deviceNode = GlobalAlloc(GPTR, requiredLength);
  137. strcpy(deviceNode->DevicePath, deviceDetailData->DevicePath);
  138. deviceNode->Next = deviceNodeHead;
  139. deviceNodeHead = deviceNode;
  140. GlobalFree(deviceDetailData);
  141. }
  142. SetupDiDestroyDeviceInfoList(deviceInfo);
  143. return deviceNodeHead;
  144. }
  145. //*****************************************************************************
  146. //
  147. // ShowDeviceInfo()
  148. //
  149. //*****************************************************************************
  150. VOID ShowDeviceInfo (
  151. PCHAR DevicePath
  152. )
  153. {
  154. HANDLE devHandle;
  155. BOOL success;
  156. int size;
  157. int nBytes;
  158. USB_DEVICE_DESCRIPTOR deviceDesc;
  159. PUSB_CONFIGURATION_DESCRIPTOR configDesc;
  160. devHandle = CreateFile(DevicePath,
  161. GENERIC_WRITE | GENERIC_READ,
  162. FILE_SHARE_WRITE | FILE_SHARE_READ,
  163. NULL,
  164. OPEN_EXISTING,
  165. 0,
  166. NULL);
  167. if (devHandle == INVALID_HANDLE_VALUE)
  168. {
  169. printf("Unable to open device:%s\n", DevicePath);
  170. return;
  171. }
  172. else
  173. {
  174. printf("Device: %s\n", DevicePath);
  175. }
  176. //
  177. // Get Device Descriptor
  178. //
  179. size = sizeof(USB_DEVICE_DESCRIPTOR);
  180. success = DeviceIoControl(devHandle,
  181. IOCTL_I82930_GET_DEVICE_DESCRIPTOR,
  182. NULL,
  183. 0,
  184. &deviceDesc,
  185. size,
  186. &nBytes,
  187. NULL);
  188. if (success)
  189. {
  190. //
  191. // Show Device Descriptor
  192. //
  193. ShowDeviceDesc(&deviceDesc);
  194. }
  195. //
  196. // Get Configuration Descriptor (just the Configuration Descriptor)
  197. //
  198. size = sizeof(USB_CONFIGURATION_DESCRIPTOR);
  199. configDesc = GlobalAlloc(GPTR, size);
  200. success = DeviceIoControl(devHandle,
  201. IOCTL_I82930_GET_CONFIG_DESCRIPTOR,
  202. NULL,
  203. 0,
  204. configDesc,
  205. size,
  206. &nBytes,
  207. NULL);
  208. if (success)
  209. {
  210. //
  211. // Get Configuration Descriptor (and Interface and Endpoint Descriptors)
  212. //
  213. size = configDesc->wTotalLength;
  214. configDesc = GlobalReAlloc(configDesc,
  215. size,
  216. GMEM_MOVEABLE | GMEM_ZEROINIT);
  217. success = DeviceIoControl(devHandle,
  218. IOCTL_I82930_GET_CONFIG_DESCRIPTOR,
  219. NULL,
  220. 0,
  221. configDesc,
  222. size,
  223. &nBytes,
  224. NULL);
  225. if (success)
  226. {
  227. //
  228. // Show Configuration Descriptor
  229. //
  230. ShowConfigDesc(configDesc);
  231. }
  232. }
  233. printf("\n");
  234. GlobalFree(configDesc);
  235. CloseHandle(devHandle);
  236. }
  237. //*****************************************************************************
  238. //
  239. // ShowDeviceDesc()
  240. //
  241. // DeviceDesc - The Device Descriptor
  242. //
  243. //*****************************************************************************
  244. VOID
  245. ShowDeviceDesc (
  246. PUSB_DEVICE_DESCRIPTOR DeviceDesc
  247. )
  248. {
  249. printf("------------------\n");
  250. printf("Device Descriptor:\n");
  251. printf("bcdUSB: 0x%04X\n",
  252. DeviceDesc->bcdUSB);
  253. printf("bDeviceClass: 0x%02X\n",
  254. DeviceDesc->bDeviceClass);
  255. printf("bDeviceSubClass: 0x%02X\n",
  256. DeviceDesc->bDeviceSubClass);
  257. printf("bDeviceProtocol: 0x%02X\n",
  258. DeviceDesc->bDeviceProtocol);
  259. printf("bMaxPacketSize0: 0x%02X (%d)\n",
  260. DeviceDesc->bMaxPacketSize0,
  261. DeviceDesc->bMaxPacketSize0);
  262. printf("idVendor: 0x%04X\n",
  263. DeviceDesc->idVendor);
  264. printf("idProduct: 0x%04X\n",
  265. DeviceDesc->idProduct);
  266. printf("bcdDevice: 0x%04X\n",
  267. DeviceDesc->bcdDevice);
  268. printf("iManufacturer: 0x%02X\n",
  269. DeviceDesc->iManufacturer);
  270. printf("iProduct: 0x%02X\n",
  271. DeviceDesc->iProduct);
  272. printf("iSerialNumber: 0x%02X\n",
  273. DeviceDesc->iSerialNumber);
  274. printf("bNumConfigurations: 0x%02X\n",
  275. DeviceDesc->bNumConfigurations);
  276. }
  277. //*****************************************************************************
  278. //
  279. // ShowConfigDesc()
  280. //
  281. // ConfigDesc - The Configuration Descriptor, and associated Interface and
  282. // EndpointDescriptors
  283. //
  284. //*****************************************************************************
  285. VOID
  286. ShowConfigDesc (
  287. PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc
  288. )
  289. {
  290. PUCHAR descEnd;
  291. PUSB_COMMON_DESCRIPTOR commonDesc;
  292. BOOLEAN ShowUnknown;
  293. descEnd = (PUCHAR)ConfigDesc + ConfigDesc->wTotalLength;
  294. commonDesc = (PUSB_COMMON_DESCRIPTOR)ConfigDesc;
  295. while ((PUCHAR)commonDesc + sizeof(USB_COMMON_DESCRIPTOR) < descEnd &&
  296. (PUCHAR)commonDesc + commonDesc->bLength <= descEnd)
  297. {
  298. ShowUnknown = FALSE;
  299. switch (commonDesc->bDescriptorType)
  300. {
  301. case USB_CONFIGURATION_DESCRIPTOR_TYPE:
  302. if (commonDesc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR))
  303. {
  304. ShowUnknown = TRUE;
  305. break;
  306. }
  307. ShowConfigurationDescriptor((PUSB_CONFIGURATION_DESCRIPTOR)commonDesc);
  308. break;
  309. case USB_INTERFACE_DESCRIPTOR_TYPE:
  310. if (commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR))
  311. {
  312. ShowUnknown = TRUE;
  313. break;
  314. }
  315. ShowInterfaceDescriptor((PUSB_INTERFACE_DESCRIPTOR)commonDesc);
  316. break;
  317. case USB_ENDPOINT_DESCRIPTOR_TYPE:
  318. if (commonDesc->bLength != sizeof(USB_ENDPOINT_DESCRIPTOR))
  319. {
  320. ShowUnknown = TRUE;
  321. break;
  322. }
  323. ShowEndpointDescriptor((PUSB_ENDPOINT_DESCRIPTOR)commonDesc);
  324. break;
  325. default:
  326. ShowUnknown = TRUE;
  327. break;
  328. }
  329. if (ShowUnknown)
  330. {
  331. // ShowUnknownDescriptor(commonDesc);
  332. }
  333. (PUCHAR)commonDesc += commonDesc->bLength;
  334. }
  335. }
  336. //*****************************************************************************
  337. //
  338. // ShowConfigurationDescriptor()
  339. //
  340. //*****************************************************************************
  341. VOID
  342. ShowConfigurationDescriptor (
  343. PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc
  344. )
  345. {
  346. printf("-------------------------\n");
  347. printf("Configuration Descriptor:\n");
  348. printf("wTotalLength: 0x%04X\n",
  349. ConfigDesc->wTotalLength);
  350. printf("bNumInterfaces: 0x%02X\n",
  351. ConfigDesc->bNumInterfaces);
  352. printf("bConfigurationValue: 0x%02X\n",
  353. ConfigDesc->bConfigurationValue);
  354. printf("iConfiguration: 0x%02X\n",
  355. ConfigDesc->iConfiguration);
  356. printf("bmAttributes: 0x%02X\n",
  357. ConfigDesc->bmAttributes);
  358. if (ConfigDesc->bmAttributes & 0x80)
  359. {
  360. printf(" Bus Powered\n");
  361. }
  362. if (ConfigDesc->bmAttributes & 0x40)
  363. {
  364. printf(" Self Powered\n");
  365. }
  366. if (ConfigDesc->bmAttributes & 0x20)
  367. {
  368. printf(" Remote Wakeup\n");
  369. }
  370. printf("MaxPower: 0x%02X (%d Ma)\n",
  371. ConfigDesc->MaxPower,
  372. ConfigDesc->MaxPower * 2);
  373. }
  374. //*****************************************************************************
  375. //
  376. // ShowInterfaceDescriptor()
  377. //
  378. //*****************************************************************************
  379. VOID
  380. ShowInterfaceDescriptor (
  381. PUSB_INTERFACE_DESCRIPTOR InterfaceDesc
  382. )
  383. {
  384. printf("---------------------\n");
  385. printf("Interface Descriptor:\n");
  386. printf("bInterfaceNumber: 0x%02X\n",
  387. InterfaceDesc->bInterfaceNumber);
  388. printf("bAlternateSetting: 0x%02X\n",
  389. InterfaceDesc->bAlternateSetting);
  390. printf("bNumEndpoints: 0x%02X\n",
  391. InterfaceDesc->bNumEndpoints);
  392. printf("bInterfaceClass: 0x%02X\n",
  393. InterfaceDesc->bInterfaceClass);
  394. printf("bInterfaceSubClass: 0x%02X\n",
  395. InterfaceDesc->bInterfaceSubClass);
  396. printf("bInterfaceProtocol: 0x%02X\n",
  397. InterfaceDesc->bInterfaceProtocol);
  398. printf("iInterface: 0x%02X\n",
  399. InterfaceDesc->iInterface);
  400. }
  401. //*****************************************************************************
  402. //
  403. // ShowEndpointDescriptor()
  404. //
  405. //*****************************************************************************
  406. VOID
  407. ShowEndpointDescriptor (
  408. PUSB_ENDPOINT_DESCRIPTOR EndpointDesc
  409. )
  410. {
  411. printf("--------------------\n");
  412. printf("Endpoint Descriptor:\n");
  413. printf("bEndpointAddress: 0x%02X\n",
  414. EndpointDesc->bEndpointAddress);
  415. switch (EndpointDesc->bmAttributes & 0x03)
  416. {
  417. case 0x00:
  418. printf("Transfer Type: Control\n");
  419. break;
  420. case 0x01:
  421. printf("Transfer Type: Isochronous\n");
  422. break;
  423. case 0x02:
  424. printf("Transfer Type: Bulk\n");
  425. break;
  426. case 0x03:
  427. printf("Transfer Type: Interrupt\n");
  428. break;
  429. }
  430. printf("wMaxPacketSize: 0x%04X (%d)\n",
  431. EndpointDesc->wMaxPacketSize,
  432. EndpointDesc->wMaxPacketSize);
  433. printf("bInterval: 0x%02X\n",
  434. EndpointDesc->bInterval);
  435. }