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.

625 lines
17 KiB

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