ReactOS  0.4.15-dev-2153-g62b4c61
usbstor.h
Go to the documentation of this file.
1 #ifndef _USBSTOR_H_
2 #define _USBSTOR_H_
3 
4 #include <wdm.h>
5 #include <usbdi.h>
6 #include <usbbusif.h>
7 #include <usbdlib.h>
8 #include <classpnp.h>
9 
10 #define USB_STOR_TAG 'sbsu'
11 
12 #ifndef BooleanFlagOn
13 #define BooleanFlagOn(Flags, SingleFlag) ((BOOLEAN)((((Flags) & (SingleFlag)) != 0)))
14 #endif
15 
16 #ifndef SrbGetCdb
17 #define SrbGetCdb(srb) ((PCDB)(srb->Cdb))
18 #endif
19 
20 // Storage subclass codes
21 
22 #define USB_SUBCLASS_RBC 0x01 // Typically, flash devices
23 #define USB_SUBCLASS_8020 0x02 // CD-ROM
24 #define USB_SUBCLASS_QIC 0x03 // QIC-157 Tapes
25 #define USB_SUBCLASS_UFI 0x04 // Floppy
26 #define USB_SUBCLASS_8070 0x05 // Removable media
27 #define USB_SUBCLASS_SCSI 0x06 // Transparent
28 #define USB_SUBCLASS_LOCKABLE 0x07 // Password-protected
29 
30 #define USB_SUBCLASS_ISD200 0xF0 // ISD200 ATA
31 #define USB_SUBCLASS_CYP_ATACB 0xF1 // Cypress ATACB
32 #define USB_SUBCLASS_VENDOR 0xFF // Use vendor specific
33 
34 // Storage protocol codes
35 
36 #define USB_PROTOCOL_CBI 0x00 // Control/Bulk/Interrupt
37 #define USB_PROTOCOL_CB 0x01 // Control/Bulk w/o interrupt
38 #define USB_PROTOCOL_BULK 0x50 // bulk only
39 #define USB_PROTOCOL_UAS 0x62 // USB Attached SCSI
40 #define USB_PROTOCOL_USBAT 0x80 // SCM-ATAPI bridge
41 #define USB_PROTOCOL_EUSB_SDDR09 0x81 // SCM-SCSI bridge for SDDR-09
42 #define USB_PROTOCOL_SDDR55 0x82 // SDDR-55 (made up)
43 
44 #define USB_PROTOCOL_DPCM_USB 0xF0 // Combination CB/SDDR09
45 #define USB_PROTOCOL_FREECOM 0xF1 // Freecom
46 #define USB_PROTOCOL_DATAFAB 0xF2 // Datafab chipsets
47 #define USB_PROTOCOL_JUMPSHOT 0xF3 // Lexar Jumpshot
48 #define USB_PROTOCOL_ALAUDA 0xF4 // Alauda chipsets
49 #define USB_PROTOCOL_KARMA 0xF5 // Rio Karma
50 #define USB_PROTOCOL_VENDOR 0xFF // Use vendor specific
51 
52 // Mass storage class-specific commands
53 
54 #define USB_BULK_GET_MAX_LUN 0xFE
55 #define USB_BULK_RESET_DEVICE 0xFF
56 
57 #define USB_RECOVERABLE_ERRORS (USBD_STATUS_STALL_PID | USBD_STATUS_DEV_NOT_RESPONDING \
58  | USBD_STATUS_ENDPOINT_HALTED | USBD_STATUS_NO_BANDWIDTH)
59 
60 #define USB_MAXCHILDREN 16
61 #define MAX_LUN 0xF
62 #define USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH 0x10000
63 
64 #define CBW_SIGNATURE 0x43425355
65 #define CSW_SIGNATURE 0x53425355
66 
67 #include <pshpack1.h>
68 
69 typedef struct
70 {
71  ULONG Signature; // CBW signature
72  ULONG Tag; // CBW Tag of operation
73  ULONG DataTransferLength; // data transfer length
74  UCHAR Flags; // CBW Flags endpoint direction
75  UCHAR LUN; // lun unit
76  UCHAR CommandBlockLength; // Command block length
77  UCHAR CommandBlock[16];
78 } CBW, *PCBW;
79 
80 C_ASSERT(sizeof(CBW) == 31);
81 
82 #define CSW_STATUS_COMMAND_PASSED 0x00
83 #define CSW_STATUS_COMMAND_FAILED 0x01
84 #define CSW_STATUS_PHASE_ERROR 0x02
85 
86 typedef struct
87 {
88  ULONG Signature; // CSW signature
89  ULONG Tag; // CSW tag
90  ULONG DataResidue; // CSW data transfer diff
91  UCHAR Status; // CSW status
92 } CSW, *PCSW;
93 
94 #include <poppack.h>
95 
96 typedef struct
97 {
100  ULONG StallRetryCount; // the number of retries after receiving USBD_STATUS_STALL_PID status
101  union
102  {
105  };
109 
111 {
113 
115 
116 #define USBSTOR_FDO_FLAGS_DEVICE_RESETTING 0x00000001 // hard reset is in progress
117 #define USBSTOR_FDO_FLAGS_IRP_LIST_FREEZE 0x00000002 // the irp list is freezed
118 
119 typedef struct
120 {
121  USBSTOR_COMMON_DEVICE_EXTENSION Common; // common device extension
122 
123  PDEVICE_OBJECT FunctionalDeviceObject; // functional device object
124  PDEVICE_OBJECT PhysicalDeviceObject; // physical device object
125  PDEVICE_OBJECT LowerDeviceObject; // lower device object
126  USB_BUS_INTERFACE_USBDI_V2 BusInterface; // bus interface of device
127  PUSB_DEVICE_DESCRIPTOR DeviceDescriptor; // usb device descriptor
128  PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor; // usb configuration descriptor
129  PUSB_STRING_DESCRIPTOR SerialNumber; // usb serial number
130  PUSBD_INTERFACE_INFORMATION InterfaceInformation; // usb interface information
131  USBD_CONFIGURATION_HANDLE ConfigurationHandle; // usb configuration handle
132  UCHAR BulkInPipeIndex; // bulk in pipe index
133  UCHAR BulkOutPipeIndex; // bulk out pipe index
134  UCHAR MaxLUN; // max lun for device
135  PDEVICE_OBJECT ChildPDO[USB_MAXCHILDREN]; // max 16 child pdo devices
136  KSPIN_LOCK IrpListLock; // irp list lock
137  LIST_ENTRY IrpListHead; // irp list head
138  ULONG IrpPendingCount; // count of irp pending
139  PSCSI_REQUEST_BLOCK ActiveSrb; // stores the current active SRB
140  KEVENT NoPendingRequests; // set if no pending or in progress requests
141  PSCSI_REQUEST_BLOCK LastTimerActiveSrb; // last timer tick active srb
142  ULONG SrbErrorHandlingActive; // error handling of srb is activated
143  ULONG TimerWorkQueueEnabled; // timer work queue enabled
144  ULONG InstanceCount; // pdo instance count
150 
151 typedef struct
152 {
154 
155  UCHAR LUN; // lun id
156  BOOLEAN Claimed; // indicating if it has been claimed by upper driver
158  PDEVICE_OBJECT *PDODeviceObject; // entry in pdo list
160  // the whole structure is not stored
161  UCHAR InquiryData[INQUIRYDATABUFFERSIZE]; // USB SCSI inquiry data
163 
165 {
171 
172 // we need this to be compatible with ReactOS' classpnp (which is compiled with NTDDI_WIN8)
173 typedef struct _STORAGE_ADAPTER_DESCRIPTOR_WIN8 {
174  ULONG Version;
175  ULONG Size;
183  UCHAR BusType;
186  UCHAR SrbType;
189 
190 
191 //---------------------------------------------------------------------
192 //
193 // fdo.c routines
194 //
195 NTSTATUS
198  IN OUT PIRP Irp);
199 
200 //---------------------------------------------------------------------
201 //
202 // pdo.c routines
203 //
204 NTSTATUS
207  IN OUT PIRP Irp);
208 
209 NTSTATUS
212  IN UCHAR LUN);
213 
214 //---------------------------------------------------------------------
215 //
216 // misc.c routines
217 //
218 NTSTATUS
219 NTAPI
222  IN OUT PIRP Irp);
223 
224 NTSTATUS
225 NTAPI
228  OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface);
229 
230 PVOID
233  IN ULONG ItemSize);
234 
235 VOID
236 FreeItem(
237  IN PVOID Item);
238 
239 NTSTATUS
242  OUT PURB UrbRequest);
243 
244 NTSTATUS
247  IN PFDO_DEVICE_EXTENSION DeviceExtension);
248 
249 NTSTATUS
250 NTAPI
253  PIRP Irp,
254  PVOID Context);
255 
256 NTSTATUS
259  IN PFDO_DEVICE_EXTENSION DeviceExtension);
260 
261 //---------------------------------------------------------------------
262 //
263 // descriptor.c routines
264 //
265 
266 NTSTATUS
269 
270 NTSTATUS
273  IN PFDO_DEVICE_EXTENSION DeviceExtension);
274 
275 NTSTATUS
277  IN PFDO_DEVICE_EXTENSION DeviceExtension);
278 
279 //---------------------------------------------------------------------
280 //
281 // scsi.c routines
282 //
283 NTSTATUS
286  IN PIRP Irp);
287 
288 NTSTATUS
290  PFDO_DEVICE_EXTENSION FDODeviceExtension,
291  PIRP Irp);
292 
293 
294 //---------------------------------------------------------------------
295 //
296 // disk.c routines
297 //
298 NTSTATUS
301  IN PIRP Irp);
302 
303 NTSTATUS
306  IN PIRP Irp);
307 
308 //---------------------------------------------------------------------
309 //
310 // queue.c routines
311 //
312 VOID
313 NTAPI
316  PIRP Irp);
317 
318 VOID
321 
322 VOID
325 
326 BOOLEAN
329  IN PIRP Irp);
330 
331 VOID
332 NTAPI
335  IN PIRP Irp);
336 
337 VOID
339  PFDO_DEVICE_EXTENSION FDODeviceExtension);
340 
341 VOID
344 
345 VOID
348  IN PIRP Irp);
349 
350 //---------------------------------------------------------------------
351 //
352 // error.c routines
353 //
354 NTSTATUS
357  IN UCHAR bEndpointAddress,
358  OUT PUSHORT Value);
359 
360 NTSTATUS
364 
365 VOID
366 NTAPI
369  PVOID Context);
370 
371 VOID
372 NTAPI
374  IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
375 
376 VOID
377 NTAPI
379  IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
380 
381 #endif // _USBSTOR_H_
ULONG Tag
Definition: usbstor.h:89
ULONG DataTransferLength
Definition: usbstor.h:73
#define IN
Definition: typedefs.h:39
ULONG DataResidue
Definition: usbstor.h:90
PUSBD_INTERFACE_INFORMATION InterfaceInformation
Definition: usbstor.h:130
BOOLEAN USBSTOR_QueueAddIrp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:75
VOID USBSTOR_QueueWaitForPendingRequests(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:171
ULONG SrbErrorHandlingActive
Definition: usbstor.h:142
PDEVICE_OBJECT FunctionalDeviceObject
Definition: usbstor.h:123
PIRP Irp
Definition: usbstor.h:98
struct _ERRORHANDLER_WORKITEM_DATA * PERRORHANDLER_WORKITEM_DATA
VOID NTAPI USBSTOR_StartIo(PDEVICE_OBJECT DeviceObject, PIRP Irp)
Definition: queue.c:300
ULONG Tag
Definition: usbstor.h:72
LONG NTSTATUS
Definition: precomp.h:26
VOID USBSTOR_QueueTerminateRequest(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:186
struct _STORAGE_ADAPTER_DESCRIPTOR_WIN8 * PSTORAGE_ADAPTER_DESCRIPTOR_WIN8
LIST_ENTRY IrpListHead
Definition: usbstor.h:137
UCHAR Status
Definition: usbstor.h:91
Definition: usbstor.h:69
NTSTATUS NTAPI USBSTOR_SyncForwardIrp(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
PIO_WORKITEM ResetDeviceWorkItem
Definition: usbstor.h:146
UCHAR CommandBlockLength
Definition: usbstor.h:76
NTSTATUS USBSTOR_GetPipeHandles(IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: descriptor.c:319
Definition: usbstor.h:86
PDEVICE_OBJECT PhysicalDeviceObject
Definition: btrfs_drv.h:1155
WORK_QUEUE_ITEM WorkQueueItem
Definition: usbstor.h:168
PDEVICE_OBJECT Self
Definition: usbstor.h:159
PSCSI_REQUEST_BLOCK ActiveSrb
Definition: usbstor.h:139
USBSTOR_COMMON_DEVICE_EXTENSION Common
Definition: usbstor.h:121
NTSTATUS USBSTOR_PdoHandlePnp(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:511
PDEVICE_OBJECT DeviceObject
Definition: usbstor.h:166
UCHAR BulkOutPipeIndex
Definition: usbstor.h:133
ULONG Signature
Definition: usbstor.h:71
struct __COMMON_DEVICE_EXTENSION__ * PUSBSTOR_COMMON_DEVICE_EXTENSION
struct CSW * PCSW
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
VOID NTAPI USBSTOR_QueueResetPipe(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:135
_In_ PIRP Irp
Definition: csq.h:116
ULONG Signature
Definition: usbstor.h:88
NTSTATUS USBSTOR_ResetDevice(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: misc.c:250
UCHAR Flags
Definition: usbstor.h:74
NTSTATUS USBSTOR_ResetPipeWithHandle(IN PDEVICE_OBJECT DeviceObject, IN USBD_PIPE_HANDLE PipeHandle)
Definition: error.c:46
unsigned char BOOLEAN
ULONG ErrorIndex
Definition: usbstor.h:99
ULONG TimerWorkQueueEnabled
Definition: usbstor.h:143
#define INQUIRYDATABUFFERSIZE
Definition: cdrw_hw.h:1113
NTSTATUS USBSTOR_GetEndpointStatus(IN PDEVICE_OBJECT DeviceObject, IN UCHAR bEndpointAddress, OUT PUSHORT Value)
Definition: error.c:18
struct __COMMON_DEVICE_EXTENSION__ USBSTOR_COMMON_DEVICE_EXTENSION
C_ASSERT(sizeof(CBW)==31)
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:406
VOID USBSTOR_QueueInitialize(PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: queue.c:17
#define USB_MAXCHILDREN
Definition: usbstor.h:60
VOID NTAPI USBSTOR_QueueResetDevice(IN PFDO_DEVICE_EXTENSION FDODeviceExtension)
Definition: error.c:148
const KSDEVICE_DESCRIPTOR DeviceDescriptor
Definition: splitter.c:257
PDEVICE_OBJECT LowerDeviceObject
Definition: usbstor.h:125
NTSTATUS USBSTOR_HandleDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: disk.c:413
INT POOL_TYPE
Definition: typedefs.h:78
struct _FDO_DEVICE_EXTENSION FDO_DEVICE_EXTENSION
IRP_CONTEXT CurrentIrpContext
Definition: usbstor.h:148
NTSTATUS USBSTOR_SyncUrbRequest(IN PDEVICE_OBJECT DeviceObject, OUT PURB UrbRequest)
Definition: misc.c:107
PSCSI_REQUEST_BLOCK LastTimerActiveSrb
Definition: usbstor.h:141
NTSTATUS USBSTOR_FdoHandlePnp(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: fdo.c:277
USBSTOR_COMMON_DEVICE_EXTENSION Common
Definition: usbstor.h:153
struct IRP_CONTEXT * PIRP_CONTEXT
struct CBW * PCBW
PVOID AllocateItem(IN POOL_TYPE PoolType, IN ULONG ItemSize)
Definition: misc.c:30
SCSI_REQUEST_BLOCK SenseSrb
Definition: usbstor.h:107
NTSTATUS USBSTOR_HandleInternalDeviceControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: disk.c:81
struct _IRP_CONTEXT IRP_CONTEXT
_Must_inspect_result_ _Out_ PNDIS_STATUS _Out_ PNDIS_HANDLE ConfigurationHandle
Definition: ndis.h:3944
unsigned char UCHAR
Definition: xmlstorage.h:181
ULONG StallRetryCount
Definition: usbstor.h:100
VOID USBSTOR_QueueRelease(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:262
PUSB_STRING_DESCRIPTOR SerialNumber
Definition: usbstor.h:129
Definition: typedefs.h:119
static UCHAR ItemSize[4]
Definition: parser.c:16
UCHAR LUN
Definition: usbstor.h:75
KEVENT NoPendingRequests
Definition: usbstor.h:140
NTSTATUS USBSTOR_GetMaxLUN(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: misc.c:204
VOID NTAPI USBSTOR_TimerRoutine(PDEVICE_OBJECT DeviceObject, PVOID Context)
Definition: error.c:205
struct _PDO_DEVICE_EXTENSION PDO_DEVICE_EXTENSION
Definition: usb.h:529
unsigned short USHORT
Definition: pedump.c:61
NTSTATUS USBSTOR_CreatePDO(IN PDEVICE_OBJECT DeviceObject, IN UCHAR LUN)
Definition: pdo.c:836
KSPIN_LOCK CommonLock
Definition: usbstor.h:145
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
struct _ERRORHANDLER_WORKITEM_DATA ERRORHANDLER_WORKITEM_DATA
static HANDLE PipeHandle
Definition: dhcpcsvc.c:21
_In_ WDFCOLLECTION _In_ WDFOBJECT Item
PDEVICE_OBJECT LowerDeviceObject
Definition: usbstor.h:157
struct PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
NTSTATUS NTAPI USBSTOR_GetBusInterface(IN PDEVICE_OBJECT DeviceObject, OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface)
Definition: misc.c:57
#define OUT
Definition: typedefs.h:40
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ _Strict_type_match_ POOL_TYPE PoolType
Definition: wdfdevice.h:3810
NTSTATUS USBSTOR_GetDescriptors(IN PDEVICE_OBJECT DeviceObject)
Definition: descriptor.c:75
NTSTATUS USBSTOR_SendCSWRequest(PFDO_DEVICE_EXTENSION FDODeviceExtension, PIRP Irp)
Definition: scsi.c:242
struct FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
unsigned int ULONG
Definition: retypes.h:1
NTSTATUS USBSTOR_SelectConfigurationAndInterface(IN PDEVICE_OBJECT DeviceObject, IN PFDO_DEVICE_EXTENSION DeviceExtension)
Definition: descriptor.c:234
VOID NTAPI USBSTOR_CancelIo(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: queue.c:28
NTSTATUS NTAPI USBSTOR_SyncForwardIrpCompletionRoutine(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
Definition: misc.c:22
struct _STORAGE_ADAPTER_DESCRIPTOR_WIN8 STORAGE_ADAPTER_DESCRIPTOR_WIN8
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
USB_BUS_INTERFACE_USBDI_V2 BusInterface
Definition: usbstor.h:126
NTSTATUS USBSTOR_HandleExecuteSCSI(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: scsi.c:540
unsigned short * PUSHORT
Definition: retypes.h:2
VOID USBSTOR_QueueNextRequest(IN PDEVICE_OBJECT DeviceObject)
Definition: queue.c:220
PDEVICE_OBJECT * PDODeviceObject
Definition: usbstor.h:158
KSPIN_LOCK IrpListLock
Definition: usbstor.h:136