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.

594 lines
15 KiB

  1. /*++
  2. Copyright (c) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. USBMASS.H
  5. Abstract:
  6. Header file for USBSTOR driver
  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. #ifndef KDEXTMODE
  16. #include <wdm.h>
  17. #include <usb.h>
  18. #include <usbioctl.h>
  19. #include <usbdlib.h>
  20. #endif
  21. #define __GUSB_H_KERNEL_
  22. #include "genusbio.h"
  23. struct _DEVICE_EXTENSION;
  24. #include "dbg.h"
  25. //*****************************************************************************
  26. // D E F I N E S
  27. //*****************************************************************************
  28. #define max(a,b) (((a) > (b)) ? (a) : (b))
  29. #define min(a,b) (((a) < (b)) ? (a) : (b))
  30. #define CLASS_URB(urb) urb->UrbControlVendorClassRequest
  31. #define FEATURE_URB(urb) urb->UrbControlFeatureRequest
  32. #define POOL_TAG 'UNEG'
  33. #undef ExAllocatePool
  34. #define ExAllocatePool(_type_, _length_) \
  35. ExAllocatePoolWithTag(_type_, _length_, POOL_TAG)
  36. //*****************************************************************************
  37. // Registry Strings
  38. //*****************************************************************************
  39. // driver keys
  40. // The pipe number for IRP_MJ_READ
  41. #define REGKEY_DEFAULT_READ_PIPE L"DefaultReadPipe"
  42. // The pipe number for IRP_MJ_WRITE
  43. #define REGKEY_DEFAULT_WRITE_PIPE L"DefaultWritePipe"
  44. #define USB_RECIPIENT_DEVICE URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE
  45. #define USB_RECIPIENT_INTERFACE URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE
  46. #define USB_RECIPIENT_ENDPOINT URB_FUNCTION_GET_DESCRIPTOR_FROM_ENDPOINT
  47. typedef struct _GENUSB_PIPE_INFO
  48. {
  49. USBD_PIPE_INFORMATION Info;
  50. GENUSB_PIPE_PROPERTIES Properties;
  51. LONG CurrentTimeout;
  52. ULONG OutstandingIO;
  53. } GENUSB_PIPE_INFO, *PGENUSB_PIPE_INFO;
  54. typedef struct _GENUSB_INTERFACE
  55. {
  56. UCHAR InterfaceNumber;
  57. UCHAR CurrentAlternate;
  58. UCHAR NumberOfPipes;
  59. UCHAR Reserved;
  60. USBD_INTERFACE_HANDLE Handle;
  61. GENUSB_PIPE_INFO Pipes[];
  62. } GENUSB_INTERFACE, *PGENUSB_INTERFACE;
  63. typedef struct _GENUSB_TRANSFER
  64. {
  65. GENUSB_READ_WRITE_PIPE UserCopy;
  66. PMDL UserMdl;
  67. PMDL TransferMdl;
  68. PGENUSB_READ_WRITE_PIPE SystemAddress;
  69. } GENUSB_TRANSFER, *PGENUSB_TRANSFER;
  70. typedef struct _GENUSB_PIPE_HANDLE {
  71. UCHAR InterfaceIndex;
  72. UCHAR PipeIndex;
  73. USHORT Signature;
  74. } *PGENUSB_PIPE_HANDLE;
  75. C_ASSERT (sizeof (GENUSB_PIPE_HANDLE) == sizeof (struct _GENUSB_PIPE_HANDLE));
  76. //
  77. // Note: these routines do NOT actually secure that a transaction to a Pipe
  78. // handle is no longer valid across a DeselectConfiguration, since the
  79. // new configuration Handle might fall in the same address, and the old pipe
  80. // handle might capture the same interface Index and Pipe Index. It does
  81. // however catch a few sainity checks, and will prevent the user mode piece
  82. // from manufacturing their own configuration handles (Without at least seeing
  83. // the first one from a given configuration.) This just keeps them more
  84. // honest, and doesn't really cause us any additional pain.
  85. //
  86. // In every case where we check the signatures, we also check to make sure
  87. // that the interface and pipe indices contained in the handle are also
  88. // still valid.
  89. //
  90. #define CONFIGURATION_CHECK_BITS(DeviceExtension) \
  91. ((USHORT) (((ULONG_PTR) ((DeviceExtension)->ConfigurationHandle)) >> 6))
  92. #define VERIFY_PIPE_HANDLE_SIG(Handle, DeviceExtension) \
  93. (CONFIGURATION_CHECK_BITS(DeviceExtension) == \
  94. ((PGENUSB_PIPE_HANDLE) (Handle))->Signature)
  95. //
  96. // Do something similar with the Pipe properties so that people are forced to
  97. // do a get and set of the pipe properties. This will help to ensure that
  98. // they are honest with these values and don't change other fields inadvertantly
  99. //
  100. #define PIPE_PROPERTIES_CHECK_BITS(PipeInfo) \
  101. ((USHORT) (((ULONG_PTR) &((PipeInfo)->Info)) >> 6))
  102. // ((USHORT) (((ULONG_PTR) (PipeInfo) is equiv, but we do it this other way
  103. // to check the type, by referecing the first field.
  104. #define VERIFY_PIPE_PROPERTIES_HANDLE(PipeProperty, PipeInfo) \
  105. (PIPE_PROPERTIES_CHECK_BITS(PipeInfo) == (PipeProperty)->PipePropertyHandle)
  106. // Device Extension for the FDO we attach on top of the USB enumerated PDO.
  107. //
  108. typedef struct _DEVICE_EXTENSION
  109. {
  110. // Back pointer to Device Object for this Device Extension
  111. PDEVICE_OBJECT Self;
  112. BOOLEAN IsStarted;
  113. BOOLEAN Reserved0[3];
  114. // PDO passed to AddDevice
  115. PDEVICE_OBJECT PhysicalDeviceObject;
  116. // Our FDO is attached to this device object
  117. PDEVICE_OBJECT StackDeviceObject;
  118. // Device specific log.
  119. PGENUSB_LOG_ENTRY LogStart; // Start of log buffer (older entries)
  120. ULONG LogIndex;
  121. ULONG LogMask;
  122. // lock to protect from IRP_MN_REMOVE
  123. IO_REMOVE_LOCK RemoveLock;
  124. // Current power states
  125. SYSTEM_POWER_STATE SystemPowerState;
  126. DEVICE_POWER_STATE DevicePowerState;
  127. PIRP CurrentPowerIrp;
  128. // SpinLock which protects the allocated data
  129. KSPIN_LOCK SpinLock;
  130. // Mutex to protect from overlapped changes to the configuration
  131. FAST_MUTEX ConfigMutex;
  132. // Device Descriptor retrieved from the device
  133. PUSB_DEVICE_DESCRIPTOR DeviceDescriptor;
  134. // Configuration Descriptor retrieved from the device
  135. PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor;
  136. // Serial Number String Descriptor
  137. PUSB_STRING_DESCRIPTOR SerialNumber;
  138. // track the number of Creates verses Closes
  139. ULONG OpenedCount;
  140. // A string to hold the Symbolic Link name for a device interface
  141. UNICODE_STRING DevInterfaceLinkName;
  142. // The Configuration Handle
  143. // If this value is NULL then the device is assumed to be unconfigured.
  144. USBD_CONFIGURATION_HANDLE ConfigurationHandle;
  145. // a lock to track users of a configuration so that when it is deselected
  146. // we won't delete the resouces too soon.
  147. IO_REMOVE_LOCK ConfigurationRemoveLock;
  148. // An array of Interface Information
  149. PGENUSB_INTERFACE * Interface;
  150. // The lenght of said Interface information
  151. UCHAR InterfacesFound;
  152. UCHAR TotalNumberOfPipes;
  153. // The default language ID of this device
  154. USHORT LanguageId;
  155. // The Interface and Pipe of used for IRP_MJ_READ
  156. // -1 means unconfigured
  157. UCHAR ReadInterface;
  158. UCHAR ReadPipe;
  159. // The Interface and Pipe of used for IRP_MJ_WRITE
  160. // -1 means unconfigured
  161. UCHAR WriteInterface;
  162. UCHAR WritePipe;
  163. } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
  164. typedef
  165. NTSTATUS
  166. (*PGENUSB_COMPLETION_ROUTINE) (
  167. IN PDEVICE_OBJECT DeviceObject,
  168. IN PIRP Irp,
  169. IN PVOID Context,
  170. IN USBD_STATUS UrbStatus,
  171. IN ULONG TransferLength
  172. );
  173. typedef struct _GENUSB_TRANS_RECV {
  174. PVOID Context;
  175. PGENUSB_PIPE_INFO Pipe;
  176. PGENUSB_COMPLETION_ROUTINE CompletionRoutine;
  177. struct _URB_BULK_OR_INTERRUPT_TRANSFER TransUrb;
  178. struct _URB_PIPE_REQUEST ResetUrb;
  179. } GENUSB_TRANS_RECV, *PGENUSB_TRANS_RECV;
  180. //*****************************************************************************
  181. //
  182. // F U N C T I O N P R O T O T Y P E S
  183. //
  184. //*****************************************************************************
  185. //
  186. // GENUSB.C
  187. //
  188. NTSTATUS
  189. DriverEntry (
  190. IN PDRIVER_OBJECT DriverObject,
  191. IN PUNICODE_STRING RegistryPath
  192. );
  193. VOID
  194. GenUSB_Unload (
  195. IN PDRIVER_OBJECT DriverObject
  196. );
  197. NTSTATUS
  198. GenUSB_AddDevice (
  199. IN PDRIVER_OBJECT DriverObject,
  200. IN PDEVICE_OBJECT PhysicalDeviceObject
  201. );
  202. VOID
  203. GenUSB_QueryParams (
  204. IN PDEVICE_OBJECT DeviceObject
  205. );
  206. NTSTATUS
  207. GenUSB_Pnp (
  208. IN PDEVICE_OBJECT DeviceObject,
  209. IN PIRP Irp
  210. );
  211. NTSTATUS
  212. GenUSB_StartDevice (
  213. IN PDEVICE_OBJECT DeviceObject,
  214. IN PIRP Irp
  215. );
  216. NTSTATUS
  217. GenUSB_StopDevice (
  218. IN PDEVICE_OBJECT DeviceObject,
  219. IN PIRP Irp
  220. );
  221. NTSTATUS
  222. GenUSB_RemoveDevice (
  223. IN PDEVICE_OBJECT DeviceObject,
  224. IN PIRP Irp
  225. );
  226. NTSTATUS
  227. GenUSB_QueryStopRemoveDevice (
  228. IN PDEVICE_OBJECT DeviceObject,
  229. IN PIRP Irp
  230. );
  231. NTSTATUS
  232. GenUSB_CancelStopRemoveDevice (
  233. IN PDEVICE_OBJECT DeviceObject,
  234. IN PIRP Irp
  235. );
  236. NTSTATUS
  237. GenUSB_SyncPassDownIrp (
  238. IN PDEVICE_OBJECT DeviceObject,
  239. IN PIRP Irp
  240. );
  241. NTSTATUS
  242. GenUSB_SyncSendUsbRequest (
  243. IN PDEVICE_OBJECT DeviceObject,
  244. IN PURB Urb
  245. );
  246. NTSTATUS
  247. GenUSB_SetDeviceInterface (
  248. IN PDEVICE_EXTENSION DeviceExtension,
  249. IN BOOLEAN Create,
  250. IN BOOLEAN Set
  251. );
  252. NTSTATUS
  253. GenUSB_SetDIRegValues (
  254. IN PDEVICE_EXTENSION DeviceExtension
  255. );
  256. NTSTATUS
  257. GenUSB_Power (
  258. IN PDEVICE_OBJECT DeviceObject,
  259. IN PIRP Irp
  260. );
  261. NTSTATUS
  262. GenUSB_SetPower (
  263. IN PDEVICE_EXTENSION DeviceExtension,
  264. IN PIRP Irp
  265. );
  266. VOID
  267. GenUSB_SetPowerCompletion(
  268. IN PDEVICE_OBJECT PdoDeviceObject,
  269. IN UCHAR MinorFunction,
  270. IN POWER_STATE PowerState,
  271. IN PVOID Context,
  272. IN PIO_STATUS_BLOCK IoStatus
  273. );
  274. NTSTATUS
  275. GenUSB_SetPowerD0Completion (
  276. IN PDEVICE_OBJECT DeviceObject,
  277. IN PIRP Irp,
  278. IN PVOID NotUsed
  279. );
  280. NTSTATUS
  281. GenUSB_SystemControl (
  282. IN PDEVICE_OBJECT DeviceObject,
  283. IN PIRP Irp
  284. );
  285. //
  286. // USB.C
  287. //
  288. NTSTATUS
  289. GenUSB_GetDescriptors (
  290. IN PDEVICE_OBJECT DeviceObject
  291. );
  292. NTSTATUS
  293. GenUSB_GetDescriptor (
  294. IN PDEVICE_OBJECT DeviceObject,
  295. IN UCHAR Recipient,
  296. IN UCHAR DescriptorType,
  297. IN UCHAR Index,
  298. IN USHORT LanguageId,
  299. IN ULONG RetryCount,
  300. IN ULONG DescriptorLength,
  301. OUT PUCHAR *Descriptor
  302. );
  303. GenUSB_GetStringDescriptors (
  304. IN PDEVICE_OBJECT DeviceObject
  305. );
  306. NTSTATUS
  307. GenUSB_VendorControlRequest (
  308. IN PDEVICE_OBJECT DeviceObject,
  309. IN UCHAR RequestType,
  310. IN UCHAR Request,
  311. IN USHORT Value,
  312. IN USHORT Index,
  313. IN USHORT Length,
  314. IN ULONG RetryCount,
  315. OUT PULONG UrbStatus,
  316. OUT PUSHORT ResultLength,
  317. OUT PUCHAR *Result
  318. );
  319. NTSTATUS
  320. GenUSB_SelectConfiguration (
  321. IN PDEVICE_EXTENSION DeviceExtension,
  322. IN ULONG NubmerInterfaces,
  323. IN PUSB_INTERFACE_DESCRIPTOR DesiredArray,
  324. OUT PUSB_INTERFACE_DESCRIPTOR FoundArray
  325. );
  326. NTSTATUS
  327. GenUSB_DeselectConfiguration (
  328. IN PDEVICE_EXTENSION DeviceExtension,
  329. IN BOOLEAN SendUrb
  330. );
  331. NTSTATUS
  332. GenUSB_GetSetPipe (
  333. IN PDEVICE_EXTENSION DeviceExtension,
  334. IN PUCHAR InterfaceIndex, // Optional
  335. IN PUCHAR InterfaceNumber, // Optional
  336. IN PUCHAR PipeIndex, // Optional
  337. IN PUCHAR EndpointAddress, // Optional
  338. IN PGENUSB_PIPE_PROPERTIES SetPipeProperties, // Optional
  339. OUT PGENUSB_PIPE_INFORMATION PipeInfo, // Optional
  340. OUT PGENUSB_PIPE_PROPERTIES GetPipeProperties, // Optional
  341. OUT USBD_PIPE_HANDLE * UsbdPipeHandle // Optional
  342. );
  343. NTSTATUS
  344. GenUSB_SetReadWritePipes (
  345. IN PDEVICE_EXTENSION DeviceExtension,
  346. IN PGENUSB_PIPE_HANDLE ReadPipe,
  347. IN PGENUSB_PIPE_HANDLE WritePipe
  348. );
  349. NTSTATUS
  350. GenUSB_RestartTimer (
  351. PDEVICE_EXTENSION DeviceExtension,
  352. PGENUSB_PIPE_INFO Pipe
  353. );
  354. VOID
  355. GenUSB_FreeInterfaceTable (
  356. PDEVICE_EXTENSION DeviceExtension
  357. );
  358. NTSTATUS
  359. GenUSB_TransmitReceive (
  360. IN PDEVICE_EXTENSION DeviceExtension,
  361. IN PIRP Irp,
  362. IN UCHAR InterfaceNo,
  363. IN UCHAR PipeNo,
  364. IN ULONG TransferFlags,
  365. IN PCHAR Buffer,
  366. IN PMDL BufferMDL,
  367. IN ULONG BufferLength,
  368. IN PVOID Context,
  369. IN PGENUSB_COMPLETION_ROUTINE CompletionRoutine
  370. );
  371. NTSTATUS
  372. GenUSB_ResetPipe (
  373. IN PDEVICE_EXTENSION DeviceExtension,
  374. IN USBD_PIPE_HANDLE UsbdPipeHandle,
  375. IN BOOLEAN ResetPipe,
  376. IN BOOLEAN ClearStall,
  377. IN BOOLEAN FlushData
  378. );
  379. VOID
  380. GenUSB_Timer (
  381. PDEVICE_OBJECT DeviceObject,
  382. PVOID Context
  383. );
  384. #if 0
  385. VOID
  386. GenUSB_AdjustConfigurationDescriptor (
  387. IN PDEVICE_OBJECT DeviceObject,
  388. IN PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc,
  389. OUT PUSB_INTERFACE_DESCRIPTOR *InterfaceDesc,
  390. OUT PLONG BulkInIndex,
  391. OUT PLONG BulkOutIndex,
  392. OUT PLONG InterruptInIndex
  393. );
  394. NTSTATUS
  395. GenUSB_GetPipes (
  396. IN PDEVICE_OBJECT DeviceObject
  397. );
  398. NTSTATUS
  399. GenUSB_CreateChildPDO (
  400. IN PDEVICE_OBJECT FdoDeviceObject,
  401. IN UCHAR Lun
  402. );
  403. NTSTATUS
  404. GenUSB_FdoQueryDeviceRelations (
  405. IN PDEVICE_OBJECT DeviceObject,
  406. IN PIRP Irp
  407. );
  408. VOID
  409. CopyField (
  410. IN PUCHAR Destination,
  411. IN PUCHAR Source,
  412. IN ULONG Count,
  413. IN UCHAR Change
  414. );
  415. NTSTATUS
  416. GenUSB_StringArrayToMultiSz(
  417. PUNICODE_STRING MultiString,
  418. PCSTR StringArray[]
  419. );
  420. NTSTATUS
  421. GenUSB_GetMaxLun (
  422. IN PDEVICE_OBJECT DeviceObject,
  423. OUT PUCHAR MaxLun
  424. );
  425. NTSTATUS
  426. GenUSB_AbortPipe (
  427. IN PDEVICE_OBJECT DeviceObject,
  428. IN USBD_PIPE_HANDLE Pipe
  429. );
  430. #endif
  431. //
  432. // OCRW.C
  433. //
  434. NTSTATUS
  435. GenUSB_Create (
  436. IN PDEVICE_OBJECT DeviceObject,
  437. IN PIRP Irp
  438. );
  439. NTSTATUS
  440. GenUSB_Close (
  441. IN PDEVICE_OBJECT DeviceObject,
  442. IN PIRP Irp
  443. );
  444. NTSTATUS
  445. GenUSB_Read (
  446. IN PDEVICE_OBJECT DeviceObject,
  447. IN PIRP Irp
  448. );
  449. NTSTATUS
  450. GenUSB_Write (
  451. IN PDEVICE_OBJECT DeviceObject,
  452. IN PIRP Irp
  453. );
  454. //
  455. // DEVIOCTL.C
  456. //
  457. NTSTATUS
  458. GenUSB_DeviceControl (
  459. IN PDEVICE_OBJECT DeviceObject,
  460. IN PIRP Irp
  461. );
  462. NTSTATUS
  463. GenUSB_ProbeAndSubmitTransfer (
  464. IN PIRP Irp,
  465. IN PIO_STACK_LOCATION IrpSp,
  466. IN PDEVICE_EXTENSION DeviceExtension
  467. );