xref: /reactos/drivers/usb/usbstor/usbstor.h (revision 8a978a17)
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 
173 //---------------------------------------------------------------------
174 //
175 // fdo.c routines
176 //
177 NTSTATUS
178 USBSTOR_FdoHandlePnp(
179     IN PDEVICE_OBJECT DeviceObject,
180     IN OUT PIRP Irp);
181 
182 //---------------------------------------------------------------------
183 //
184 // pdo.c routines
185 //
186 NTSTATUS
187 USBSTOR_PdoHandlePnp(
188     IN PDEVICE_OBJECT DeviceObject,
189     IN OUT PIRP Irp);
190 
191 NTSTATUS
192 USBSTOR_CreatePDO(
193     IN PDEVICE_OBJECT DeviceObject,
194     IN UCHAR LUN);
195 
196 //---------------------------------------------------------------------
197 //
198 // misc.c routines
199 //
200 NTSTATUS
201 NTAPI
202 USBSTOR_SyncForwardIrp(
203     IN PDEVICE_OBJECT DeviceObject,
204     IN OUT PIRP Irp);
205 
206 NTSTATUS
207 NTAPI
208 USBSTOR_GetBusInterface(
209     IN PDEVICE_OBJECT DeviceObject,
210     OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface);
211 
212 PVOID
213 AllocateItem(
214     IN POOL_TYPE PoolType,
215     IN ULONG ItemSize);
216 
217 VOID
218 FreeItem(
219     IN PVOID Item);
220 
221 NTSTATUS
222 USBSTOR_SyncUrbRequest(
223     IN PDEVICE_OBJECT DeviceObject,
224     OUT PURB UrbRequest);
225 
226 NTSTATUS
227 USBSTOR_GetMaxLUN(
228     IN PDEVICE_OBJECT DeviceObject,
229     IN PFDO_DEVICE_EXTENSION DeviceExtension);
230 
231 NTSTATUS
232 NTAPI
233 USBSTOR_SyncForwardIrpCompletionRoutine(
234     PDEVICE_OBJECT DeviceObject,
235     PIRP Irp,
236     PVOID Context);
237 
238 NTSTATUS
239 USBSTOR_ResetDevice(
240     IN PDEVICE_OBJECT DeviceObject,
241     IN PFDO_DEVICE_EXTENSION DeviceExtension);
242 
243 //---------------------------------------------------------------------
244 //
245 // descriptor.c routines
246 //
247 
248 NTSTATUS
249 USBSTOR_GetDescriptors(
250     IN PDEVICE_OBJECT DeviceObject);
251 
252 NTSTATUS
253 USBSTOR_SelectConfigurationAndInterface(
254     IN PDEVICE_OBJECT DeviceObject,
255     IN PFDO_DEVICE_EXTENSION DeviceExtension);
256 
257 NTSTATUS
258 USBSTOR_GetPipeHandles(
259     IN PFDO_DEVICE_EXTENSION DeviceExtension);
260 
261 //---------------------------------------------------------------------
262 //
263 // scsi.c routines
264 //
265 NTSTATUS
266 USBSTOR_HandleExecuteSCSI(
267     IN PDEVICE_OBJECT DeviceObject,
268     IN PIRP Irp);
269 
270 NTSTATUS
271 USBSTOR_SendCSWRequest(
272     PFDO_DEVICE_EXTENSION FDODeviceExtension,
273     PIRP Irp);
274 
275 
276 //---------------------------------------------------------------------
277 //
278 // disk.c routines
279 //
280 NTSTATUS
281 USBSTOR_HandleInternalDeviceControl(
282     IN PDEVICE_OBJECT DeviceObject,
283     IN PIRP Irp);
284 
285 NTSTATUS
286 USBSTOR_HandleDeviceControl(
287     IN PDEVICE_OBJECT DeviceObject,
288     IN PIRP Irp);
289 
290 //---------------------------------------------------------------------
291 //
292 // queue.c routines
293 //
294 VOID
295 NTAPI
296 USBSTOR_StartIo(
297     PDEVICE_OBJECT DeviceObject,
298     PIRP Irp);
299 
300 VOID
301 USBSTOR_QueueWaitForPendingRequests(
302     IN PDEVICE_OBJECT DeviceObject);
303 
304 VOID
305 USBSTOR_QueueRelease(
306     IN PDEVICE_OBJECT DeviceObject);
307 
308 BOOLEAN
309 USBSTOR_QueueAddIrp(
310     IN PDEVICE_OBJECT DeviceObject,
311     IN PIRP Irp);
312 
313 VOID
314 NTAPI
315 USBSTOR_CancelIo(
316     IN  PDEVICE_OBJECT DeviceObject,
317     IN  PIRP Irp);
318 
319 VOID
320 USBSTOR_QueueInitialize(
321     PFDO_DEVICE_EXTENSION FDODeviceExtension);
322 
323 VOID
324 USBSTOR_QueueNextRequest(
325     IN PDEVICE_OBJECT DeviceObject);
326 
327 VOID
328 USBSTOR_QueueTerminateRequest(
329     IN PDEVICE_OBJECT DeviceObject,
330     IN PIRP Irp);
331 
332 //---------------------------------------------------------------------
333 //
334 // error.c routines
335 //
336 NTSTATUS
337 USBSTOR_GetEndpointStatus(
338     IN PDEVICE_OBJECT DeviceObject,
339     IN UCHAR bEndpointAddress,
340     OUT PUSHORT Value);
341 
342 NTSTATUS
343 USBSTOR_ResetPipeWithHandle(
344     IN PDEVICE_OBJECT DeviceObject,
345     IN USBD_PIPE_HANDLE PipeHandle);
346 
347 VOID
348 NTAPI
349 USBSTOR_TimerRoutine(
350     PDEVICE_OBJECT DeviceObject,
351      PVOID Context);
352 
353 VOID
354 NTAPI
355 USBSTOR_QueueResetPipe(
356     IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
357 
358 VOID
359 NTAPI
360 USBSTOR_QueueResetDevice(
361     IN PFDO_DEVICE_EXTENSION FDODeviceExtension);
362 
363 #endif // _USBSTOR_H_
364