xref: /reactos/drivers/usb/usbstor/usbstor.h (revision 9d3c3a75)
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 {
98     PIRP Irp;
99     ULONG ErrorIndex;
100     ULONG StallRetryCount;                                            // the number of retries after receiving USBD_STATUS_STALL_PID status
101     union
102     {
103         CBW cbw;
104         CSW csw;
105     };
106     URB Urb;
107     SCSI_REQUEST_BLOCK SenseSrb;
108 } IRP_CONTEXT, *PIRP_CONTEXT;
109 
110 typedef struct __COMMON_DEVICE_EXTENSION__
111 {
112     BOOLEAN IsFDO;
113 
114 }USBSTOR_COMMON_DEVICE_EXTENSION, *PUSBSTOR_COMMON_DEVICE_EXTENSION;
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
145     KSPIN_LOCK CommonLock;
146     PIO_WORKITEM ResetDeviceWorkItem;
147     ULONG Flags;
148     IRP_CONTEXT CurrentIrpContext;
149 }FDO_DEVICE_EXTENSION, *PFDO_DEVICE_EXTENSION;
150 
151 typedef struct
152 {
153     USBSTOR_COMMON_DEVICE_EXTENSION Common;
154 
155     UCHAR LUN;                                                                           // lun id
156     BOOLEAN Claimed;                                                                     // indicating if it has been claimed by upper driver
157     PDEVICE_OBJECT LowerDeviceObject;                                                    // points to FDO
158     PDEVICE_OBJECT *PDODeviceObject;                                                     // entry in pdo list
159     PDEVICE_OBJECT Self;                                                                 // self
160     // the whole structure is not stored
161     UCHAR InquiryData[INQUIRYDATABUFFERSIZE];                                            // USB SCSI inquiry data
162 }PDO_DEVICE_EXTENSION, *PPDO_DEVICE_EXTENSION;
163 
164 typedef struct _ERRORHANDLER_WORKITEM_DATA
165 {
166     PDEVICE_OBJECT DeviceObject;
167     PIRP_CONTEXT Context;
168     WORK_QUEUE_ITEM WorkQueueItem;
169     PIRP Irp;
170 } ERRORHANDLER_WORKITEM_DATA, *PERRORHANDLER_WORKITEM_DATA;
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;
176     ULONG MaximumTransferLength;
177     ULONG MaximumPhysicalPages;
178     ULONG AlignmentMask;
179     BOOLEAN AdapterUsesPio;
180     BOOLEAN AdapterScansDown;
181     BOOLEAN CommandQueueing;
182     BOOLEAN AcceleratedTransfer;
183     UCHAR BusType;
184     USHORT BusMajorVersion;
185     USHORT BusMinorVersion;
186     UCHAR SrbType;
187     UCHAR AddressType;
188 } STORAGE_ADAPTER_DESCRIPTOR_WIN8, *PSTORAGE_ADAPTER_DESCRIPTOR_WIN8;
189 
190 
191 //---------------------------------------------------------------------
192 //
193 // fdo.c routines
194 //
195 NTSTATUS
196 USBSTOR_FdoHandlePnp(
197     IN PDEVICE_OBJECT DeviceObject,
198     IN OUT PIRP Irp);
199 
200 //---------------------------------------------------------------------
201 //
202 // pdo.c routines
203 //
204 NTSTATUS
205 USBSTOR_PdoHandlePnp(
206     IN PDEVICE_OBJECT DeviceObject,
207     IN OUT PIRP Irp);
208 
209 NTSTATUS
210 USBSTOR_CreatePDO(
211     IN PDEVICE_OBJECT DeviceObject,
212     IN UCHAR LUN);
213 
214 //---------------------------------------------------------------------
215 //
216 // misc.c routines
217 //
218 
219 NTSTATUS
220 NTAPI
221 USBSTOR_GetBusInterface(
222     IN PDEVICE_OBJECT DeviceObject,
223     OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface);
224 
225 PVOID
226 AllocateItem(
227     IN POOL_TYPE PoolType,
228     IN ULONG ItemSize);
229 
230 VOID
231 FreeItem(
232     IN PVOID Item);
233 
234 NTSTATUS
235 USBSTOR_SyncUrbRequest(
236     IN PDEVICE_OBJECT DeviceObject,
237     OUT PURB UrbRequest);
238 
239 NTSTATUS
240 USBSTOR_GetMaxLUN(
241     IN PDEVICE_OBJECT DeviceObject,
242     IN PFDO_DEVICE_EXTENSION DeviceExtension);
243 
244 NTSTATUS
245 USBSTOR_ResetDevice(
246     IN PDEVICE_OBJECT DeviceObject,
247     IN PFDO_DEVICE_EXTENSION DeviceExtension);
248 
249 //---------------------------------------------------------------------
250 //
251 // descriptor.c routines
252 //
253 
254 NTSTATUS
255 USBSTOR_GetDescriptors(
256     IN PDEVICE_OBJECT DeviceObject);
257 
258 NTSTATUS
259 USBSTOR_SelectConfigurationAndInterface(
260     IN PDEVICE_OBJECT DeviceObject,
261     IN PFDO_DEVICE_EXTENSION DeviceExtension);
262 
263 NTSTATUS
264 USBSTOR_GetPipeHandles(
265     IN PFDO_DEVICE_EXTENSION DeviceExtension);
266 
267 //---------------------------------------------------------------------
268 //
269 // scsi.c routines
270 //
271 NTSTATUS
272 USBSTOR_HandleExecuteSCSI(
273     IN PDEVICE_OBJECT DeviceObject,
274     IN PIRP Irp);
275 
276 NTSTATUS
277 USBSTOR_SendCSWRequest(
278     PFDO_DEVICE_EXTENSION FDODeviceExtension,
279     PIRP Irp);
280 
281 
282 //---------------------------------------------------------------------
283 //
284 // disk.c routines
285 //
286 NTSTATUS
287 USBSTOR_HandleInternalDeviceControl(
288     IN PDEVICE_OBJECT DeviceObject,
289     IN PIRP Irp);
290 
291 NTSTATUS
292 USBSTOR_HandleDeviceControl(
293     IN PDEVICE_OBJECT DeviceObject,
294     IN PIRP Irp);
295 
296 //---------------------------------------------------------------------
297 //
298 // queue.c routines
299 //
300 VOID
301 NTAPI
302 USBSTOR_StartIo(
303     PDEVICE_OBJECT DeviceObject,
304     PIRP Irp);
305 
306 VOID
307 USBSTOR_QueueWaitForPendingRequests(
308     IN PDEVICE_OBJECT DeviceObject);
309 
310 VOID
311 USBSTOR_QueueRelease(
312     IN PDEVICE_OBJECT DeviceObject);
313 
314 BOOLEAN
315 USBSTOR_QueueAddIrp(
316     IN PDEVICE_OBJECT DeviceObject,
317     IN PIRP Irp);
318 
319 VOID
320 NTAPI
321 USBSTOR_CancelIo(
322     IN  PDEVICE_OBJECT DeviceObject,
323     IN  PIRP Irp);
324 
325 VOID
326 USBSTOR_QueueInitialize(
327     PFDO_DEVICE_EXTENSION FDODeviceExtension);
328 
329 VOID
330 USBSTOR_QueueNextRequest(
331     IN PDEVICE_OBJECT DeviceObject);
332 
333 VOID
334 USBSTOR_QueueTerminateRequest(
335     IN PDEVICE_OBJECT DeviceObject,
336     IN PIRP Irp);
337 
338 //---------------------------------------------------------------------
339 //
340 // error.c routines
341 //
342 NTSTATUS
343 USBSTOR_GetEndpointStatus(
344     IN PDEVICE_OBJECT DeviceObject,
345     IN UCHAR bEndpointAddress,
346     OUT PUSHORT Value);
347 
348 NTSTATUS
349 USBSTOR_ResetPipeWithHandle(
350     IN PDEVICE_OBJECT DeviceObject,
351     IN USBD_PIPE_HANDLE PipeHandle);
352 
353 VOID
354 NTAPI
355 USBSTOR_TimerRoutine(
356     PDEVICE_OBJECT DeviceObject,
357      PVOID Context);
358 
359 VOID
360 NTAPI
361 USBSTOR_QueueResetPipe(
362     IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
363 
364 VOID
365 NTAPI
366 USBSTOR_QueueResetDevice(
367     IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
368 
369 #endif // _USBSTOR_H_
370