xref: /reactos/drivers/usb/usbstor/usbstor.h (revision 3adf4508)
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 NTSTATUS
219 NTAPI
220 USBSTOR_SyncForwardIrp(
221     IN PDEVICE_OBJECT DeviceObject,
222     IN OUT PIRP Irp);
223 
224 NTSTATUS
225 NTAPI
226 USBSTOR_GetBusInterface(
227     IN PDEVICE_OBJECT DeviceObject,
228     OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface);
229 
230 PVOID
231 AllocateItem(
232     IN POOL_TYPE PoolType,
233     IN ULONG ItemSize);
234 
235 VOID
236 FreeItem(
237     IN PVOID Item);
238 
239 NTSTATUS
240 USBSTOR_SyncUrbRequest(
241     IN PDEVICE_OBJECT DeviceObject,
242     OUT PURB UrbRequest);
243 
244 NTSTATUS
245 USBSTOR_GetMaxLUN(
246     IN PDEVICE_OBJECT DeviceObject,
247     IN PFDO_DEVICE_EXTENSION DeviceExtension);
248 
249 NTSTATUS
250 NTAPI
251 USBSTOR_SyncForwardIrpCompletionRoutine(
252     PDEVICE_OBJECT DeviceObject,
253     PIRP Irp,
254     PVOID Context);
255 
256 NTSTATUS
257 USBSTOR_ResetDevice(
258     IN PDEVICE_OBJECT DeviceObject,
259     IN PFDO_DEVICE_EXTENSION DeviceExtension);
260 
261 //---------------------------------------------------------------------
262 //
263 // descriptor.c routines
264 //
265 
266 NTSTATUS
267 USBSTOR_GetDescriptors(
268     IN PDEVICE_OBJECT DeviceObject);
269 
270 NTSTATUS
271 USBSTOR_SelectConfigurationAndInterface(
272     IN PDEVICE_OBJECT DeviceObject,
273     IN PFDO_DEVICE_EXTENSION DeviceExtension);
274 
275 NTSTATUS
276 USBSTOR_GetPipeHandles(
277     IN PFDO_DEVICE_EXTENSION DeviceExtension);
278 
279 //---------------------------------------------------------------------
280 //
281 // scsi.c routines
282 //
283 NTSTATUS
284 USBSTOR_HandleExecuteSCSI(
285     IN PDEVICE_OBJECT DeviceObject,
286     IN PIRP Irp);
287 
288 NTSTATUS
289 USBSTOR_SendCSWRequest(
290     PFDO_DEVICE_EXTENSION FDODeviceExtension,
291     PIRP Irp);
292 
293 
294 //---------------------------------------------------------------------
295 //
296 // disk.c routines
297 //
298 NTSTATUS
299 USBSTOR_HandleInternalDeviceControl(
300     IN PDEVICE_OBJECT DeviceObject,
301     IN PIRP Irp);
302 
303 NTSTATUS
304 USBSTOR_HandleDeviceControl(
305     IN PDEVICE_OBJECT DeviceObject,
306     IN PIRP Irp);
307 
308 //---------------------------------------------------------------------
309 //
310 // queue.c routines
311 //
312 VOID
313 NTAPI
314 USBSTOR_StartIo(
315     PDEVICE_OBJECT DeviceObject,
316     PIRP Irp);
317 
318 VOID
319 USBSTOR_QueueWaitForPendingRequests(
320     IN PDEVICE_OBJECT DeviceObject);
321 
322 VOID
323 USBSTOR_QueueRelease(
324     IN PDEVICE_OBJECT DeviceObject);
325 
326 BOOLEAN
327 USBSTOR_QueueAddIrp(
328     IN PDEVICE_OBJECT DeviceObject,
329     IN PIRP Irp);
330 
331 VOID
332 NTAPI
333 USBSTOR_CancelIo(
334     IN  PDEVICE_OBJECT DeviceObject,
335     IN  PIRP Irp);
336 
337 VOID
338 USBSTOR_QueueInitialize(
339     PFDO_DEVICE_EXTENSION FDODeviceExtension);
340 
341 VOID
342 USBSTOR_QueueNextRequest(
343     IN PDEVICE_OBJECT DeviceObject);
344 
345 VOID
346 USBSTOR_QueueTerminateRequest(
347     IN PDEVICE_OBJECT DeviceObject,
348     IN PIRP Irp);
349 
350 //---------------------------------------------------------------------
351 //
352 // error.c routines
353 //
354 NTSTATUS
355 USBSTOR_GetEndpointStatus(
356     IN PDEVICE_OBJECT DeviceObject,
357     IN UCHAR bEndpointAddress,
358     OUT PUSHORT Value);
359 
360 NTSTATUS
361 USBSTOR_ResetPipeWithHandle(
362     IN PDEVICE_OBJECT DeviceObject,
363     IN USBD_PIPE_HANDLE PipeHandle);
364 
365 VOID
366 NTAPI
367 USBSTOR_TimerRoutine(
368     PDEVICE_OBJECT DeviceObject,
369      PVOID Context);
370 
371 VOID
372 NTAPI
373 USBSTOR_QueueResetPipe(
374     IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
375 
376 VOID
377 NTAPI
378 USBSTOR_QueueResetDevice(
379     IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
380 
381 #endif // _USBSTOR_H_
382