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.

607 lines
17 KiB

  1. /*++
  2. Copyright (c) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. DBG.C
  5. Abstract:
  6. Copied from USBSTOR driver debug utility functions
  7. Environment:
  8. kernel mode
  9. Revision History:
  10. September 2001: Created by KenRay
  11. --*/
  12. //*****************************************************************************
  13. // I N C L U D E S
  14. //*****************************************************************************
  15. #include "genusb.h"
  16. #ifdef ALLOC_PRAGMA
  17. #if DBG
  18. #pragma alloc_text(PAGE, GenUSB_QueryGlobalParams)
  19. #endif
  20. #if DEBUG_LOG
  21. #pragma alloc_text(PAGE, GenUSB_LogInit)
  22. #pragma alloc_text(PAGE, GenUSB_LogUnInit)
  23. #endif
  24. #if DBG
  25. #pragma alloc_text(PAGE, DumpDeviceDesc)
  26. #pragma alloc_text(PAGE, DumpConfigDesc)
  27. #pragma alloc_text(PAGE, DumpConfigurationDescriptor)
  28. #pragma alloc_text(PAGE, DumpInterfaceDescriptor)
  29. #pragma alloc_text(PAGE, DumpEndpointDescriptor)
  30. #endif
  31. #endif
  32. //******************************************************************************
  33. //
  34. // G L O B A L S
  35. //
  36. //******************************************************************************
  37. DRIVERGLOBALS GenUSB_DriverGlobals =
  38. {
  39. 0, // DBGF_BRK_DRIVERENTRY // DebugFlags
  40. 0, // DebugLevel
  41. };
  42. //******************************************************************************
  43. //
  44. // GenUSB_QueryGlobalParams()
  45. //
  46. //******************************************************************************
  47. VOID
  48. GenUSB_QueryGlobalParams (
  49. )
  50. {
  51. RTL_QUERY_REGISTRY_TABLE paramTable[3];
  52. DBGPRINT(2, ("enter: GENUSB_QueryGlobalParams\n"));
  53. RtlZeroMemory (&paramTable[0], sizeof(paramTable));
  54. paramTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
  55. paramTable[0].Name = L"DebugFlags";
  56. paramTable[0].EntryContext = &GenUSB_DriverGlobals.DebugFlags;
  57. paramTable[0].DefaultType = REG_BINARY;
  58. paramTable[0].DefaultData = &GenUSB_DriverGlobals.DebugFlags;
  59. paramTable[0].DefaultLength = sizeof(ULONG);
  60. paramTable[1].Flags = RTL_QUERY_REGISTRY_DIRECT;
  61. paramTable[1].Name = L"DebugLevel";
  62. paramTable[1].EntryContext = &GenUSB_DriverGlobals.DebugLevel;
  63. paramTable[1].DefaultType = REG_BINARY;
  64. paramTable[1].DefaultData = &GenUSB_DriverGlobals.DebugLevel;
  65. paramTable[1].DefaultLength = sizeof(ULONG);
  66. RtlQueryRegistryValues(RTL_REGISTRY_SERVICES,
  67. L"GENUSB",
  68. &paramTable[0],
  69. NULL, // Context
  70. NULL); // Environment
  71. DBGPRINT(2, ("exit: GENUSB_QueryGlobalParams\n"));
  72. }
  73. #if DBG || DEBUG_LOG
  74. //*****************************************************************************
  75. //
  76. // GenUSB_LogInit()
  77. //
  78. //*****************************************************************************
  79. VOID
  80. GenUSB_LogInit (
  81. PDEVICE_EXTENSION DeviceExtension
  82. )
  83. {
  84. ULONG size = (1 << LOGSIZE);
  85. ULONG length = size * sizeof (GENUSB_LOG_ENTRY);
  86. DeviceExtension->LogStart = ExAllocatePool (NonPagedPool, length);
  87. if (NULL == DeviceExtension->LogStart)
  88. {
  89. // No troubles here. Just make sure that ever time we use the log
  90. // we check for null.
  91. }
  92. else
  93. {
  94. RtlZeroMemory (DeviceExtension->LogStart, length);
  95. }
  96. DeviceExtension->LogIndex = 0;
  97. DeviceExtension->LogMask = size - 1;
  98. }
  99. //*****************************************************************************
  100. //
  101. // GenUSB_LogUnInit()
  102. //
  103. //*****************************************************************************
  104. VOID
  105. GenUSB_LogUnInit (
  106. PDEVICE_EXTENSION DeviceExtension
  107. )
  108. {
  109. ExFreePool (DeviceExtension->LogStart);
  110. DeviceExtension->LogStart = 0;
  111. }
  112. //*****************************************************************************
  113. //
  114. // GenUSB_LogEntry()
  115. //
  116. //*****************************************************************************
  117. VOID
  118. GenUSB_LogEntry (
  119. IN PDEVICE_EXTENSION DeviceExtension,
  120. IN ULONG Tag,
  121. IN ULONG_PTR Info1,
  122. IN ULONG_PTR Info2,
  123. IN ULONG_PTR Info3
  124. )
  125. {
  126. //
  127. // The assumption here is that the number of log entries is an
  128. // even power of 2. Therefore we need only mask off the lower bits
  129. //
  130. ULONG index;
  131. PGENUSB_LOG_ENTRY log;
  132. if (DeviceExtension->LogStart == NULL)
  133. {
  134. return;
  135. }
  136. index = InterlockedIncrement (&DeviceExtension->LogIndex);
  137. index = (index & DeviceExtension->LogMask);
  138. log = &(DeviceExtension->LogStart[index]);
  139. log->le_tag = Tag;
  140. log->le_info1 = Info1;
  141. log->le_info2 = Info2;
  142. log->le_info3 = Info3;
  143. }
  144. #endif
  145. #if DBG
  146. //*****************************************************************************
  147. //
  148. // PnPMinorFunctionString()
  149. //
  150. // MinorFunction - The IRP_MJ_PNP minor function
  151. //
  152. //*****************************************************************************
  153. PCHAR
  154. PnPMinorFunctionString (
  155. UCHAR MinorFunction
  156. )
  157. {
  158. switch (MinorFunction)
  159. {
  160. case IRP_MN_START_DEVICE:
  161. return "IRP_MN_START_DEVICE";
  162. case IRP_MN_QUERY_REMOVE_DEVICE:
  163. return "IRP_MN_QUERY_REMOVE_DEVICE";
  164. case IRP_MN_REMOVE_DEVICE:
  165. return "IRP_MN_REMOVE_DEVICE";
  166. case IRP_MN_CANCEL_REMOVE_DEVICE:
  167. return "IRP_MN_CANCEL_REMOVE_DEVICE";
  168. case IRP_MN_STOP_DEVICE:
  169. return "IRP_MN_STOP_DEVICE";
  170. case IRP_MN_QUERY_STOP_DEVICE:
  171. return "IRP_MN_QUERY_STOP_DEVICE";
  172. case IRP_MN_CANCEL_STOP_DEVICE:
  173. return "IRP_MN_CANCEL_STOP_DEVICE";
  174. case IRP_MN_QUERY_DEVICE_RELATIONS:
  175. return "IRP_MN_QUERY_DEVICE_RELATIONS";
  176. case IRP_MN_QUERY_INTERFACE:
  177. return "IRP_MN_QUERY_INTERFACE";
  178. case IRP_MN_QUERY_CAPABILITIES:
  179. return "IRP_MN_QUERY_CAPABILITIES";
  180. case IRP_MN_QUERY_RESOURCES:
  181. return "IRP_MN_QUERY_RESOURCES";
  182. case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
  183. return "IRP_MN_QUERY_RESOURCE_REQUIREMENTS";
  184. case IRP_MN_QUERY_DEVICE_TEXT:
  185. return "IRP_MN_QUERY_DEVICE_TEXT";
  186. case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
  187. return "IRP_MN_FILTER_RESOURCE_REQUIREMENTS";
  188. case IRP_MN_READ_CONFIG:
  189. return "IRP_MN_READ_CONFIG";
  190. case IRP_MN_WRITE_CONFIG:
  191. return "IRP_MN_WRITE_CONFIG";
  192. case IRP_MN_EJECT:
  193. return "IRP_MN_EJECT";
  194. case IRP_MN_SET_LOCK:
  195. return "IRP_MN_SET_LOCK";
  196. case IRP_MN_QUERY_ID:
  197. return "IRP_MN_QUERY_ID";
  198. case IRP_MN_QUERY_PNP_DEVICE_STATE:
  199. return "IRP_MN_QUERY_PNP_DEVICE_STATE";
  200. case IRP_MN_QUERY_BUS_INFORMATION:
  201. return "IRP_MN_QUERY_BUS_INFORMATION";
  202. case IRP_MN_DEVICE_USAGE_NOTIFICATION:
  203. return "IRP_MN_DEVICE_USAGE_NOTIFICATION";
  204. case IRP_MN_SURPRISE_REMOVAL:
  205. return "IRP_MN_SURPRISE_REMOVAL";
  206. default:
  207. return "IRP_MN_?????";
  208. }
  209. }
  210. //*****************************************************************************
  211. //
  212. // PowerMinorFunctionString()
  213. //
  214. // MinorFunction - The IRP_MJ_POWER minor function
  215. //
  216. //*****************************************************************************
  217. PCHAR
  218. PowerMinorFunctionString (
  219. UCHAR MinorFunction
  220. )
  221. {
  222. switch (MinorFunction)
  223. {
  224. case IRP_MN_WAIT_WAKE:
  225. return "IRP_MN_WAIT_WAKE";
  226. case IRP_MN_POWER_SEQUENCE:
  227. return "IRP_MN_POWER_SEQUENCE";
  228. case IRP_MN_SET_POWER:
  229. return "IRP_MN_SET_POWER";
  230. case IRP_MN_QUERY_POWER:
  231. return "IRP_MN_QUERY_POWER";
  232. default:
  233. return "IRP_MN_?????";
  234. }
  235. }
  236. //*****************************************************************************
  237. //
  238. // PowerDeviceStateString()
  239. //
  240. // State - The DEVICE_POWER_STATE
  241. //
  242. //*****************************************************************************
  243. PCHAR
  244. PowerDeviceStateString (
  245. DEVICE_POWER_STATE State
  246. )
  247. {
  248. switch (State)
  249. {
  250. case PowerDeviceUnspecified:
  251. return "PowerDeviceUnspecified";
  252. case PowerDeviceD0:
  253. return "PowerDeviceD0";
  254. case PowerDeviceD1:
  255. return "PowerDeviceD1";
  256. case PowerDeviceD2:
  257. return "PowerDeviceD2";
  258. case PowerDeviceD3:
  259. return "PowerDeviceD3";
  260. case PowerDeviceMaximum:
  261. return "PowerDeviceMaximum";
  262. default:
  263. return "PowerDevice?????";
  264. }
  265. }
  266. //*****************************************************************************
  267. //
  268. // PowerSystemStateString()
  269. //
  270. // State - The SYSTEM_POWER_STATE
  271. //
  272. //*****************************************************************************
  273. PCHAR
  274. PowerSystemStateString (
  275. SYSTEM_POWER_STATE State
  276. )
  277. {
  278. switch (State)
  279. {
  280. case PowerSystemUnspecified:
  281. return "PowerSystemUnspecified";
  282. case PowerSystemWorking:
  283. return "PowerSystemWorking";
  284. case PowerSystemSleeping1:
  285. return "PowerSystemSleeping1";
  286. case PowerSystemSleeping2:
  287. return "PowerSystemSleeping2";
  288. case PowerSystemSleeping3:
  289. return "PowerSystemSleeping3";
  290. case PowerSystemHibernate:
  291. return "PowerSystemHibernate";
  292. case PowerSystemShutdown:
  293. return "PowerSystemShutdown";
  294. case PowerSystemMaximum:
  295. return "PowerSystemMaximum";
  296. default:
  297. return "PowerSystem?????";
  298. }
  299. }
  300. //*****************************************************************************
  301. //
  302. // DumpDeviceDesc()
  303. //
  304. // DeviceDesc - The Device Descriptor
  305. //
  306. //*****************************************************************************
  307. VOID
  308. DumpDeviceDesc (
  309. PUSB_DEVICE_DESCRIPTOR DeviceDesc
  310. )
  311. {
  312. DBGPRINT(3, ("------------------\n"));
  313. DBGPRINT(3, ("Device Descriptor:\n"));
  314. DBGPRINT(3, ("bcdUSB: 0x%04X\n",
  315. DeviceDesc->bcdUSB));
  316. DBGPRINT(3, ("bDeviceClass: 0x%02X\n",
  317. DeviceDesc->bDeviceClass));
  318. DBGPRINT(3, ("bDeviceSubClass: 0x%02X\n",
  319. DeviceDesc->bDeviceSubClass));
  320. DBGPRINT(3, ("bDeviceProtocol: 0x%02X\n",
  321. DeviceDesc->bDeviceProtocol));
  322. DBGPRINT(3, ("bMaxPacketSize0: 0x%02X (%d)\n",
  323. DeviceDesc->bMaxPacketSize0,
  324. DeviceDesc->bMaxPacketSize0));
  325. DBGPRINT(3, ("idVendor: 0x%04X\n",
  326. DeviceDesc->idVendor));
  327. DBGPRINT(3, ("idProduct: 0x%04X\n",
  328. DeviceDesc->idProduct));
  329. DBGPRINT(3, ("bcdDevice: 0x%04X\n",
  330. DeviceDesc->bcdDevice));
  331. DBGPRINT(3, ("iManufacturer: 0x%02X\n",
  332. DeviceDesc->iManufacturer));
  333. DBGPRINT(3, ("iProduct: 0x%02X\n",
  334. DeviceDesc->iProduct));
  335. DBGPRINT(3, ("iSerialNumber: 0x%02X\n",
  336. DeviceDesc->iSerialNumber));
  337. DBGPRINT(3, ("bNumConfigurations: 0x%02X\n",
  338. DeviceDesc->bNumConfigurations));
  339. }
  340. //*****************************************************************************
  341. //
  342. // DumpConfigDesc()
  343. //
  344. // ConfigDesc - The Configuration Descriptor, and associated Interface and
  345. // EndpointDescriptors
  346. //
  347. //*****************************************************************************
  348. VOID
  349. DumpConfigDesc (
  350. PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc
  351. )
  352. {
  353. PUCHAR descEnd;
  354. PUSB_COMMON_DESCRIPTOR commonDesc;
  355. BOOLEAN dumpUnknown;
  356. descEnd = (PUCHAR)ConfigDesc + ConfigDesc->wTotalLength;
  357. commonDesc = (PUSB_COMMON_DESCRIPTOR)ConfigDesc;
  358. while ((PUCHAR)commonDesc + sizeof(USB_COMMON_DESCRIPTOR) < descEnd &&
  359. (PUCHAR)commonDesc + commonDesc->bLength <= descEnd)
  360. {
  361. dumpUnknown = FALSE;
  362. switch (commonDesc->bDescriptorType)
  363. {
  364. case USB_CONFIGURATION_DESCRIPTOR_TYPE:
  365. if (commonDesc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR))
  366. {
  367. dumpUnknown = TRUE;
  368. break;
  369. }
  370. DumpConfigurationDescriptor((PUSB_CONFIGURATION_DESCRIPTOR)commonDesc);
  371. break;
  372. case USB_INTERFACE_DESCRIPTOR_TYPE:
  373. if (commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR))
  374. {
  375. dumpUnknown = TRUE;
  376. break;
  377. }
  378. DumpInterfaceDescriptor((PUSB_INTERFACE_DESCRIPTOR)commonDesc);
  379. break;
  380. case USB_ENDPOINT_DESCRIPTOR_TYPE:
  381. if (commonDesc->bLength != sizeof(USB_ENDPOINT_DESCRIPTOR))
  382. {
  383. dumpUnknown = TRUE;
  384. break;
  385. }
  386. DumpEndpointDescriptor((PUSB_ENDPOINT_DESCRIPTOR)commonDesc);
  387. break;
  388. default:
  389. dumpUnknown = TRUE;
  390. break;
  391. }
  392. if (dumpUnknown)
  393. {
  394. // DumpUnknownDescriptor(commonDesc);
  395. }
  396. (PUCHAR)commonDesc += commonDesc->bLength;
  397. }
  398. }
  399. //*****************************************************************************
  400. //
  401. // DumpConfigurationDescriptor()
  402. //
  403. //*****************************************************************************
  404. VOID
  405. DumpConfigurationDescriptor (
  406. PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc
  407. )
  408. {
  409. DBGPRINT(3, ("-------------------------\n"));
  410. DBGPRINT(3, ("Configuration Descriptor:\n"));
  411. DBGPRINT(3, ("wTotalLength: 0x%04X\n",
  412. ConfigDesc->wTotalLength));
  413. DBGPRINT(3, ("bNumInterfaces: 0x%02X\n",
  414. ConfigDesc->bNumInterfaces));
  415. DBGPRINT(3, ("bConfigurationValue: 0x%02X\n",
  416. ConfigDesc->bConfigurationValue));
  417. DBGPRINT(3, ("iConfiguration: 0x%02X\n",
  418. ConfigDesc->iConfiguration));
  419. DBGPRINT(3, ("bmAttributes: 0x%02X\n",
  420. ConfigDesc->bmAttributes));
  421. if (ConfigDesc->bmAttributes & 0x80)
  422. {
  423. DBGPRINT(3, (" Bus Powered\n"));
  424. }
  425. if (ConfigDesc->bmAttributes & 0x40)
  426. {
  427. DBGPRINT(3, (" Self Powered\n"));
  428. }
  429. if (ConfigDesc->bmAttributes & 0x20)
  430. {
  431. DBGPRINT(3, (" Remote Wakeup\n"));
  432. }
  433. DBGPRINT(3, ("MaxPower: 0x%02X (%d Ma)\n",
  434. ConfigDesc->MaxPower,
  435. ConfigDesc->MaxPower * 2));
  436. }
  437. //*****************************************************************************
  438. //
  439. // DumpInterfaceDescriptor()
  440. //
  441. //*****************************************************************************
  442. VOID
  443. DumpInterfaceDescriptor (
  444. PUSB_INTERFACE_DESCRIPTOR InterfaceDesc
  445. )
  446. {
  447. DBGPRINT(3, ("---------------------\n"));
  448. DBGPRINT(3, ("Interface Descriptor:\n"));
  449. DBGPRINT(3, ("bInterfaceNumber: 0x%02X\n",
  450. InterfaceDesc->bInterfaceNumber));
  451. DBGPRINT(3, ("bAlternateSetting: 0x%02X\n",
  452. InterfaceDesc->bAlternateSetting));
  453. DBGPRINT(3, ("bNumEndpoints: 0x%02X\n",
  454. InterfaceDesc->bNumEndpoints));
  455. DBGPRINT(3, ("bInterfaceClass: 0x%02X\n",
  456. InterfaceDesc->bInterfaceClass));
  457. DBGPRINT(3, ("bInterfaceSubClass: 0x%02X\n",
  458. InterfaceDesc->bInterfaceSubClass));
  459. DBGPRINT(3, ("bInterfaceProtocol: 0x%02X\n",
  460. InterfaceDesc->bInterfaceProtocol));
  461. DBGPRINT(3, ("iInterface: 0x%02X\n",
  462. InterfaceDesc->iInterface));
  463. }
  464. //*****************************************************************************
  465. //
  466. // DumpEndpointDescriptor()
  467. //
  468. //*****************************************************************************
  469. VOID
  470. DumpEndpointDescriptor (
  471. PUSB_ENDPOINT_DESCRIPTOR EndpointDesc
  472. )
  473. {
  474. DBGPRINT(3, ("--------------------\n"));
  475. DBGPRINT(3, ("Endpoint Descriptor:\n"));
  476. DBGPRINT(3, ("bEndpointAddress: 0x%02X\n",
  477. EndpointDesc->bEndpointAddress));
  478. switch (EndpointDesc->bmAttributes & 0x03)
  479. {
  480. case 0x00:
  481. DBGPRINT(3, ("Transfer Type: Control\n"));
  482. break;
  483. case 0x01:
  484. DBGPRINT(3, ("Transfer Type: Isochronous\n"));
  485. break;
  486. case 0x02:
  487. DBGPRINT(3, ("Transfer Type: Bulk\n"));
  488. break;
  489. case 0x03:
  490. DBGPRINT(3, ("Transfer Type: Interrupt\n"));
  491. break;
  492. }
  493. DBGPRINT(3, ("wMaxPacketSize: 0x%04X (%d)\n",
  494. EndpointDesc->wMaxPacketSize,
  495. EndpointDesc->wMaxPacketSize));
  496. DBGPRINT(3, ("bInterval: 0x%02X\n",
  497. EndpointDesc->bInterval));
  498. }
  499. #endif