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.

588 lines
15 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. private.h
  5. Abstract:
  6. Prototypes and definitions for the usb scanner device driver.
  7. Author:
  8. Environment:
  9. kernel mode only
  10. Notes:
  11. Revision History:
  12. --*/
  13. //
  14. // Includes
  15. //
  16. #include "debug.h"
  17. //
  18. // Defines
  19. //
  20. #ifndef max
  21. #define max(a,b) (((a) > (b)) ? (a) : (b))
  22. #endif
  23. #ifndef min
  24. #define min(a,b) (((a) < (b)) ? (a) : (b))
  25. #endif
  26. #define TAG_USBSCAN 0x55495453 // "STIU"
  27. #define TAG_USBD 0x44425355 // "USBD"
  28. #define USBSCAN_OBJECTNAME_A "\\\\.\\Usbscan"
  29. #define USBSCAN_OBJECTNAME_W L"\\\\.\\Usbscan"
  30. #define USBSCAN_REG_CREATEFILE L"CreateFileName"
  31. #define USBSCAN_TIMEOUT_READ 120 // 120 sec
  32. #define USBSCAN_TIMEOUT_WRITE 120 // 120 sec
  33. #define USBSCAN_TIMEOUT_EVENT 0 // no timeout
  34. #define USBSCAN_TIMEOUT_OTHER 120 // 120 sec
  35. #define USBSCAN_REG_TIMEOUT_READ L"TimeoutRead"
  36. #define USBSCAN_REG_TIMEOUT_WRITE L"TimeoutWrite"
  37. #define USBSCAN_REG_TIMEOUT_EVENT L"TimeoutEvent"
  38. //
  39. // Private IOCTL to workaround #446466 (Whistler)
  40. //
  41. #define IOCTL_SEND_USB_REQUEST_PTP CTL_CODE(FILE_DEVICE_USB_SCAN,IOCTL_INDEX+20,METHOD_BUFFERED,FILE_ANY_ACCESS)
  42. //
  43. // Wake state
  44. //
  45. typedef enum {
  46. // No outstanding Wait-Wake IRP
  47. WAKESTATE_DISARMED = 1,
  48. // Wait-Wake IRP requested, not yet seen
  49. WAKESTATE_WAITING = 2,
  50. // Wait-Wake cancelled before IRP seen again
  51. WAKESTATE_WAITING_CANCELLED = 3,
  52. // Wait-Wake IRP seen and forwarded. Device is *probably* armed
  53. WAKESTATE_ARMED = 4,
  54. // Wait-Wake IRP seen and cancelled. Hasn't reached completion yet
  55. WAKESTATE_ARMING_CANCELLED = 5,
  56. // Wait-Wake IRP has passed the completion routine
  57. WAKESTATE_COMPLETING = 7
  58. } WAKESTATE;
  59. //
  60. // Pipe buffer structure for the read pipe only.
  61. //
  62. typedef struct _PIPEBUFFER {
  63. PUCHAR pStartBuffer;
  64. PUCHAR pBuffer;
  65. PUCHAR pNextByte;
  66. ULONG RemainingData;
  67. KEVENT ReadSyncEvent;
  68. } PIPEBUFFER, *PPIPEBUFFER;
  69. //
  70. // Device Extension
  71. //
  72. typedef struct _USBSCAN_DEVICE_EXTENSION {
  73. PDEVICE_OBJECT pOwnDeviceObject;
  74. PDEVICE_OBJECT pStackDeviceObject;
  75. PDEVICE_OBJECT pPhysicalDeviceObject;
  76. ULONG DeviceInstance;
  77. UNICODE_STRING DeviceName;
  78. UNICODE_STRING SymbolicLinkName;
  79. KEVENT PendingIoEvent;
  80. ULONG PendingIoCount;
  81. BOOLEAN AcceptingRequests;
  82. BOOLEAN Stopped;
  83. //
  84. // Remote wakeup support.
  85. //
  86. KEVENT WakeCompletedEvent;
  87. LONG WakeState;
  88. PIRP pWakeIrp;
  89. BOOLEAN bEnabledForWakeup;
  90. //
  91. // USB descriptors from the device
  92. //
  93. PUSB_DEVICE_DESCRIPTOR pDeviceDescriptor;
  94. PUSB_CONFIGURATION_DESCRIPTOR pConfigurationDescriptor;
  95. PUSB_INTERFACE_DESCRIPTOR pInterfaceDescriptor;
  96. PUSB_ENDPOINT_DESCRIPTOR pEndpointDescriptor;
  97. USBD_CONFIGURATION_HANDLE ConfigurationHandle;
  98. USBD_PIPE_INFORMATION PipeInfo[MAX_NUM_PIPES];
  99. ULONG NumberOfPipes;
  100. ULONG IndexBulkIn;
  101. ULONG IndexBulkOut;
  102. ULONG IndexInterrupt;
  103. //
  104. // Name of the device interface
  105. //
  106. UNICODE_STRING InterfaceNameString;
  107. //
  108. // Read pipe buffer
  109. //
  110. PIPEBUFFER ReadPipeBuffer[MAX_NUM_PIPES];
  111. //
  112. // Power management variables
  113. //
  114. PIRP pPowerIrp;
  115. DEVICE_CAPABILITIES DeviceCapabilities;
  116. DEVICE_POWER_STATE CurrentDevicePowerState;
  117. //
  118. // For MP safe contention management.
  119. //
  120. KSPIN_LOCK SpinLock;
  121. } USBSCAN_DEVICE_EXTENSION, *PUSBSCAN_DEVICE_EXTENSION;
  122. typedef struct _TRANSFER_CONTEXT {
  123. ULONG RemainingTransferLength;
  124. ULONG ChunkSize;
  125. ULONG NBytesTransferred;
  126. PUCHAR pTransferBuffer;
  127. PUCHAR pOriginalTransferBuffer;
  128. PMDL pTransferMdl;
  129. ULONG PipeIndex;
  130. PURB pUrb;
  131. BOOLEAN fDestinedForReadBuffer;
  132. BOOLEAN fNextReadBlocked;
  133. PIRP pThisIrp;
  134. PDEVICE_OBJECT pDeviceObject;
  135. LARGE_INTEGER Timeout;
  136. KDPC TimerDpc;
  137. KTIMER Timer;
  138. } TRANSFER_CONTEXT, *PTRANSFER_CONTEXT;
  139. typedef struct _USBSCAN_FILE_CONTEXT {
  140. LONG PipeIndex;
  141. ULONG TimeoutRead;
  142. ULONG TimeoutWrite;
  143. ULONG TimeoutEvent;
  144. } USBSCAN_FILE_CONTEXT, *PUSBSCAN_FILE_CONTEXT;
  145. typedef struct _USBSCAN_PACKTES {
  146. PIRP pIrp;
  147. ULONG TimeoutCounter;
  148. BOOLEAN bCompleted;
  149. LIST_ENTRY PacketsEntry;
  150. } USBSCAN_PACKETS, *PUSBSCAN_PACKETS;
  151. #ifdef _WIN64
  152. typedef struct _IO_BLOCK_32 {
  153. IN unsigned uOffset;
  154. IN unsigned uLength;
  155. IN OUT CHAR * POINTER_32 pbyData;
  156. IN unsigned uIndex;
  157. } IO_BLOCK_32, *PIO_BLOCK_32;
  158. typedef struct _IO_BLOCK_EX_32 {
  159. IN unsigned uOffset;
  160. IN unsigned uLength;
  161. IN OUT CHAR * POINTER_32 pbyData;
  162. IN unsigned uIndex;
  163. IN UCHAR bRequest; // Specific request
  164. IN UCHAR bmRequestType; // Bitmap - charateristics of request
  165. IN UCHAR fTransferDirectionIn; // TRUE - Device-->Host; FALSE - Host-->Device
  166. } IO_BLOCK_EX_32, *PIO_BLOCK_EX_32;
  167. #endif // _WIN64
  168. //
  169. // prototypes
  170. //
  171. NTSTATUS // in usbscan9x.c
  172. DriverEntry(
  173. IN PDRIVER_OBJECT DriverObject,
  174. IN PUNICODE_STRING RegistryPath
  175. );
  176. VOID // in usbscan9x.c
  177. USUnload(
  178. IN PDRIVER_OBJECT DriverObject
  179. );
  180. VOID // in usbscan9x.c
  181. USIncrementIoCount(
  182. IN PDEVICE_OBJECT pDeviceObject
  183. );
  184. LONG // in usbscan9x.c
  185. USDecrementIoCount(
  186. IN PDEVICE_OBJECT pDeviceObject
  187. );
  188. NTSTATUS // in usbscan9x.c
  189. USDeferIrpCompletion(
  190. IN PDEVICE_OBJECT pDeviceObject,
  191. IN PIRP pIrp,
  192. IN PVOID Context
  193. );
  194. NTSTATUS // in usbscan9x.c
  195. USCreateSymbolicLink(
  196. PUSBSCAN_DEVICE_EXTENSION pde
  197. );
  198. NTSTATUS // in usbscan9x.c
  199. USDestroySymbolicLink(
  200. PUSBSCAN_DEVICE_EXTENSION pde
  201. );
  202. NTSTATUS // in usbscan9x.c
  203. USPnp(
  204. IN PDEVICE_OBJECT pDeviceObject,
  205. IN PIRP pIrp
  206. );
  207. NTSTATUS // in usbscan9x.c
  208. USPnpAddDevice(
  209. IN PDRIVER_OBJECT pDriverObject,
  210. IN OUT PDEVICE_OBJECT pPhysicalDeviceObject
  211. );
  212. NTSTATUS // in usbscan9x.c
  213. USGetUSBDeviceDescriptor(
  214. IN PDEVICE_OBJECT pDeviceObject
  215. );
  216. NTSTATUS // in usbscan9x.c
  217. USBSCAN_CallUSBD(
  218. IN PDEVICE_OBJECT pDeviceObject,
  219. IN PURB pUrb
  220. );
  221. NTSTATUS // in usbscan9x.c
  222. USConfigureDevice(
  223. IN PDEVICE_OBJECT pDeviceObject
  224. );
  225. NTSTATUS // in usbscan9x.c
  226. USUnConfigureDevice(
  227. IN PDEVICE_OBJECT pDeviceObject
  228. );
  229. NTSTATUS // in usbscan9x.c
  230. USGetDeviceCapability(
  231. IN PUSBSCAN_DEVICE_EXTENSION pde
  232. );
  233. NTSTATUS // in usbscan9x.c
  234. UsbScanReadDeviceRegistry(
  235. IN PUSBSCAN_DEVICE_EXTENSION pExtension,
  236. IN PCWSTR pKeyName,
  237. OUT PVOID *ppvData
  238. );
  239. NTSTATUS // in usbscan9x.c
  240. UsbScanWriteDeviceRegistry(
  241. IN PUSBSCAN_DEVICE_EXTENSION pExtension,
  242. IN PCWSTR pKeyName,
  243. IN ULONG Type,
  244. IN PVOID pvData,
  245. IN ULONG DataSize
  246. );
  247. PURB
  248. USCreateConfigurationRequest(
  249. IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
  250. IN OUT PUSHORT Siz
  251. );
  252. NTSTATUS
  253. USWaitWakeIoCompletionRoutine(
  254. PDEVICE_OBJECT pDeviceObject,
  255. PIRP pIrp,
  256. PVOID pContext
  257. );
  258. BOOLEAN
  259. USArmForWake(
  260. PUSBSCAN_DEVICE_EXTENSION pde,
  261. POWER_STATE SystemState
  262. );
  263. VOID
  264. USDisarmWake(
  265. PUSBSCAN_DEVICE_EXTENSION pde
  266. );
  267. VOID
  268. USWaitWakePoCompletionRoutine(
  269. PDEVICE_OBJECT pDeviceObject,
  270. UCHAR MinorFunction,
  271. POWER_STATE State,
  272. PVOID pContext,
  273. PIO_STATUS_BLOCK pIoStatus
  274. );
  275. VOID
  276. USInitializeWakeState(
  277. PUSBSCAN_DEVICE_EXTENSION pde
  278. );
  279. VOID
  280. USQueuePassiveLevelCallback(
  281. IN PDEVICE_OBJECT pDeviceObject,
  282. IN PIO_WORKITEM_ROUTINE pCallbackFunction
  283. );
  284. VOID
  285. USPassiveLevelReArmCallbackWorker(
  286. IN PDEVICE_OBJECT pDeviceObject,
  287. IN PVOID pContext
  288. );
  289. LONG
  290. MyInterlockedOr(
  291. PKSPIN_LOCK pSpinLock,
  292. LONG volatile *Destination,
  293. LONG Value
  294. );
  295. #ifdef ORIGINAL_POOLTRACK
  296. PVOID // in usbscan9x.c
  297. USAllocatePool(
  298. IN POOL_TYPE PoolType,
  299. IN ULONG ulNumberOfBytes
  300. );
  301. VOID // in usbscan9x.c
  302. USFreePool(
  303. IN PVOID pvAddress
  304. );
  305. #else // ORIGINAL_POOLTRACK
  306. #define USAllocatePool(a, b) ExAllocatePoolWithTag(a, b, NAME_POOLTAG)
  307. #define USFreePool(a) ExFreePool(a)
  308. #endif // ORIGINAL_POOLTRACK
  309. NTSTATUS // in ioctl.c
  310. USDeviceControl(
  311. IN PDEVICE_OBJECT pDeviceObject,
  312. IN PIRP pIrp
  313. );
  314. NTSTATUS // in ioctl.c
  315. USReadWriteRegisters(
  316. IN PDEVICE_OBJECT pDeviceObject,
  317. IN PIO_BLOCK pIoBlock,
  318. IN BOOLEAN fRead,
  319. IN ULONG IoBlockSize
  320. );
  321. NTSTATUS // in ioctl.c
  322. USPassThruUSBRequest(
  323. IN PDEVICE_OBJECT pDeviceObject,
  324. IN PIO_BLOCK_EX pIoBlockEx,
  325. IN ULONG InLength,
  326. IN ULONG OutLength
  327. );
  328. NTSTATUS // in ioctl.c
  329. USPassThruUSBRequestPTP(
  330. IN PDEVICE_OBJECT pDeviceObject,
  331. IN PIO_BLOCK_EX pIoBlockEx,
  332. IN ULONG InLength,
  333. IN ULONG OutLength
  334. );
  335. NTSTATUS // in ioctl.c
  336. USCancelPipe(
  337. IN PDEVICE_OBJECT pDeviceObject,
  338. IN PIRP pIrp,
  339. IN PIPE_TYPE PipeType,
  340. IN BOOLEAN fAbort
  341. );
  342. NTSTATUS // in ioctl.c
  343. USAbortResetPipe(
  344. IN PDEVICE_OBJECT pDeviceObject,
  345. IN ULONG uIndex,
  346. IN BOOLEAN fAbort
  347. );
  348. NTSTATUS // in ocrw.c
  349. USOpen(
  350. IN PDEVICE_OBJECT pDeviceObject,
  351. IN PIRP pIrp
  352. );
  353. NTSTATUS // in ocrw.c
  354. USClose(
  355. IN PDEVICE_OBJECT pDeviceObject,
  356. IN PIRP pIrp
  357. );
  358. NTSTATUS // in ocrw.c
  359. USFlush(
  360. IN PDEVICE_OBJECT pDeviceObject,
  361. IN PIRP pIrp
  362. );
  363. NTSTATUS // in ocrw.c
  364. USRead(
  365. IN PDEVICE_OBJECT pDeviceObject,
  366. IN PIRP pIrp
  367. );
  368. NTSTATUS // in ocrw.c
  369. USWrite(
  370. IN PDEVICE_OBJECT pDeviceObject,
  371. IN PIRP pIrp
  372. );
  373. NTSTATUS // in ocrw.c
  374. USTransfer(
  375. IN PDEVICE_OBJECT pDeviceObject,
  376. IN PIRP pIrp,
  377. IN ULONG PipeIndex,
  378. IN PVOID pBuffer,
  379. IN PMDL pMdl,
  380. IN ULONG TransferSize,
  381. IN PULONG pTimeout
  382. );
  383. NTSTATUS // in ocrw.c
  384. USTransferComplete(
  385. IN PDEVICE_OBJECT pDeviceObject,
  386. IN PIRP pIrp,
  387. IN PTRANSFER_CONTEXT pTransferContext
  388. );
  389. VOID // in ocrw.c
  390. USCancelIrp(
  391. IN PDEVICE_OBJECT pDeviceObject,
  392. IN PIRP pIrp
  393. );
  394. NTSTATUS // in ocrw.c
  395. USEnqueueIrp(
  396. IN PDEVICE_OBJECT pDeviceObject,
  397. IN PUSBSCAN_PACKETS pPackets
  398. );
  399. PUSBSCAN_PACKETS // in ocrw.c
  400. USDequeueIrp(
  401. IN PDEVICE_OBJECT pDeviceObject,
  402. IN PIRP pIrp
  403. );
  404. VOID // in ocrw.c
  405. USWaitThread(
  406. IN PVOID pTransferContext
  407. );
  408. ULONG
  409. USGetPipeIndexToUse(
  410. IN PDEVICE_OBJECT pDeviceObject,
  411. IN PIRP pIrp,
  412. IN ULONG PipeIndex
  413. );
  414. VOID
  415. USTimerDpc(
  416. IN PKDPC pDpc,
  417. IN PVOID pIrp,
  418. IN PVOID SystemArgument1,
  419. IN PVOID SystemArgument2
  420. );
  421. NTSTATUS // in power.c
  422. USPower(
  423. IN PDEVICE_OBJECT pDeviceObject,
  424. IN PIRP pIrp
  425. );
  426. NTSTATUS // in power.c
  427. USPoRequestCompletion(
  428. IN PDEVICE_OBJECT pPdo,
  429. IN UCHAR MinorFunction,
  430. IN POWER_STATE PowerState,
  431. IN PDEVICE_OBJECT pDeviceObject,
  432. IN PIO_STATUS_BLOCK pIoStatus
  433. );
  434. NTSTATUS // in power.c
  435. USSetDevicePowerState(
  436. IN PDEVICE_OBJECT pDeviceObject,
  437. IN DEVICE_POWER_STATE DeviceState,
  438. IN PBOOLEAN pHookIt
  439. );
  440. NTSTATUS
  441. USSystemPowerIrpComplete(
  442. IN PDEVICE_OBJECT pPdo,
  443. IN PIRP pIrp,
  444. IN PDEVICE_OBJECT pDeviceObject
  445. );
  446. NTSTATUS
  447. USDevicePowerIrpComplete(
  448. IN PDEVICE_OBJECT pPdo,
  449. IN PIRP pIrp,
  450. IN PDEVICE_OBJECT pDeviceObject
  451. );
  452. NTSTATUS
  453. USCallNextDriverSynch(
  454. IN PUSBSCAN_DEVICE_EXTENSION pde,
  455. IN PIRP pIrp
  456. );
  457. NTSTATUS
  458. UsbScanHandleInterface(
  459. PDEVICE_OBJECT DeviceObject,
  460. PUNICODE_STRING InterfaceName,
  461. BOOLEAN Create
  462. );
  463. VOID
  464. UsbScanLogError(
  465. IN PDRIVER_OBJECT DriverObject,
  466. IN PDEVICE_OBJECT DeviceObject OPTIONAL,
  467. IN ULONG SequenceNumber,
  468. IN UCHAR MajorFunctionCode,
  469. IN UCHAR RetryCount,
  470. IN ULONG UniqueErrorValue,
  471. IN NTSTATUS FinalStatus,
  472. IN NTSTATUS SpecificIOStatus
  473. );