ReactOS  0.4.15-dev-1018-g0695ecb
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 
173 //---------------------------------------------------------------------
174 //
175 // fdo.c routines
176 //
177 NTSTATUS
180  IN OUT PIRP Irp);
181 
182 //---------------------------------------------------------------------
183 //
184 // pdo.c routines
185 //
186 NTSTATUS
189  IN OUT PIRP Irp);
190 
191 NTSTATUS
194  IN UCHAR LUN);
195 
196 //---------------------------------------------------------------------
197 //
198 // misc.c routines
199 //
200 NTSTATUS
201 NTAPI
204  IN OUT PIRP Irp);
205 
206 NTSTATUS
207 NTAPI
210  OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface);
211 
212 PVOID
215  IN ULONG ItemSize);
216 
217 VOID
218 FreeItem(
219  IN PVOID Item);
220 
221 NTSTATUS
224  OUT PURB UrbRequest);
225 
226 NTSTATUS
229  IN PFDO_DEVICE_EXTENSION DeviceExtension);
230 
231 NTSTATUS
232 NTAPI
235  PIRP Irp,
236  PVOID Context);
237 
238 NTSTATUS
241  IN PFDO_DEVICE_EXTENSION DeviceExtension);
242 
243 //---------------------------------------------------------------------
244 //
245 // descriptor.c routines
246 //
247 
248 NTSTATUS
251 
252 NTSTATUS
255  IN PFDO_DEVICE_EXTENSION DeviceExtension);
256 
257 NTSTATUS
259  IN PFDO_DEVICE_EXTENSION DeviceExtension);
260 
261 //---------------------------------------------------------------------
262 //
263 // scsi.c routines
264 //
265 NTSTATUS
268  IN PIRP Irp);
269 
270 NTSTATUS
272  PFDO_DEVICE_EXTENSION FDODeviceExtension,
273  PIRP Irp);
274 
275 
276 //---------------------------------------------------------------------
277 //
278 // disk.c routines
279 //
280 NTSTATUS
283  IN PIRP Irp);
284 
285 NTSTATUS
288  IN PIRP Irp);
289 
290 //---------------------------------------------------------------------
291 //
292 // queue.c routines
293 //
294 VOID
295 NTAPI
298  PIRP Irp);
299 
300 VOID
303 
304 VOID
307 
308 BOOLEAN
311  IN PIRP Irp);
312 
313 VOID
314 NTAPI
317  IN PIRP Irp);
318 
319 VOID
321  PFDO_DEVICE_EXTENSION FDODeviceExtension);
322 
323 VOID
326 
327 VOID
330  IN PIRP Irp);
331 
332 //---------------------------------------------------------------------
333 //
334 // error.c routines
335 //
336 NTSTATUS
339  IN UCHAR bEndpointAddress,
340  OUT PUSHORT Value);
341 
342 NTSTATUS
346 
347 VOID
348 NTAPI
351  PVOID Context);
352 
353 VOID
354 NTAPI
356  IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
357 
358 VOID
359 NTAPI
361  IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
362 
363 #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
_In_ PIRP Irp
Definition: csq.h:116
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
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
IN UCHAR Value
Definition: halp.h:394
UCHAR BulkOutPipeIndex
Definition: usbstor.h:133
ULONG Signature
Definition: usbstor.h:71
struct __COMMON_DEVICE_EXTENSION__ * PUSBSTOR_COMMON_DEVICE_EXTENSION
struct CSW * PCSW
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
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)
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:417
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
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1569
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
NTSTATUS USBSTOR_CreatePDO(IN PDEVICE_OBJECT DeviceObject, IN UCHAR LUN)
Definition: pdo.c:843
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
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
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
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
USB_BUS_INTERFACE_USBDI_V2 BusInterface
Definition: usbstor.h:126
_Must_inspect_result_ _In_ FLT_CONTEXT_TYPE _In_ SIZE_T _In_ POOL_TYPE PoolType
Definition: fltkernel.h:1444
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