[USBSTOR]
[reactos.git] / drivers / usb / usbstor / usbstor.h
1
2 #pragma once
3
4 #include <ntddk.h>
5 #define NDEBUG
6 #include <debug.h>
7 #include <usbdi.h>
8 #include <hubbusif.h>
9 #include <usbbusif.h>
10 #include <usbioctl.h>
11 #include <usbiodef.h>
12 #include <usb.h>
13 #include <usbdlib.h>
14 #include <stdio.h>
15 #include <wdmguid.h>
16 #include <classpnp.h>
17 #include <scsi.h>
18
19 #define USB_STOR_TAG 'sbsu'
20 #define USB_MAXCHILDREN (16)
21
22
23
24 #define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
25 #define NTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
26
27 #define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
28 ((((unsigned long)(n) & 0xFF00)) << 8) | \
29 ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
30 ((((unsigned long)(n) & 0xFF000000)) >> 24))
31
32
33 #define NTOHL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
34 ((((unsigned long)(n) & 0xFF00)) << 8) | \
35 ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
36 ((((unsigned long)(n) & 0xFF000000)) >> 24))
37
38 #define USB_RECOVERABLE_ERRORS (USBD_STATUS_STALL_PID | USBD_STATUS_DEV_NOT_RESPONDING \
39 | USBD_STATUS_ENDPOINT_HALTED | USBD_STATUS_NO_BANDWIDTH)
40
41 typedef struct __COMMON_DEVICE_EXTENSION__
42 {
43 BOOLEAN IsFDO;
44
45 }USBSTOR_COMMON_DEVICE_EXTENSION, *PUSBSTOR_COMMON_DEVICE_EXTENSION;
46
47 typedef struct
48 {
49 USBSTOR_COMMON_DEVICE_EXTENSION Common; // common device extension
50
51 PDEVICE_OBJECT FunctionalDeviceObject; // functional device object
52 PDEVICE_OBJECT PhysicalDeviceObject; // physical device object
53 PDEVICE_OBJECT LowerDeviceObject; // lower device object
54 USB_BUS_INTERFACE_USBDI_V2 BusInterface; // bus interface of device
55 PUSB_DEVICE_DESCRIPTOR DeviceDescriptor; // usb device descriptor
56 PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor; // usb configuration descriptor
57 PUSB_STRING_DESCRIPTOR SerialNumber; // usb serial number
58 PUSBD_INTERFACE_INFORMATION InterfaceInformation; // usb interface information
59 USBD_CONFIGURATION_HANDLE ConfigurationHandle; // usb configuration handle
60 UCHAR BulkInPipeIndex; // bulk in pipe index
61 UCHAR BulkOutPipeIndex; // bulk out pipe index
62 UCHAR MaxLUN; // max lun for device
63 PDEVICE_OBJECT ChildPDO[16]; // max 16 child pdo devices
64 KSPIN_LOCK IrpListLock; // irp list lock
65 LIST_ENTRY IrpListHead; // irp list head
66 BOOLEAN IrpListFreeze; // if true the irp list is freezed
67 BOOLEAN ResetInProgress; // if hard reset is in progress
68 ULONG IrpPendingCount; // count of irp pending
69 BOOLEAN SrbActive; // debug field if srb is pending
70 }FDO_DEVICE_EXTENSION, *PFDO_DEVICE_EXTENSION;
71
72 typedef struct
73 {
74 USBSTOR_COMMON_DEVICE_EXTENSION Common;
75 PDEVICE_OBJECT LowerDeviceObject; // points to FDO
76 UCHAR LUN; // lun id
77 PVOID InquiryData; // USB SCSI inquiry data
78 UCHAR Claimed; // indicating if it has been claimed by upper driver
79 ULONG BlockLength; // length of block
80 ULONG LastLogicBlockAddress; // last block address
81 }PDO_DEVICE_EXTENSION, *PPDO_DEVICE_EXTENSION;
82
83 //
84 // max lun command identifier
85 //
86 #define USB_BULK_GET_MAX_LUN 0xFE
87
88 #include <pshpack1.h>
89 typedef struct
90 {
91 ULONG Signature; // CBW signature
92 ULONG Tag; // CBW Tag of operation
93 ULONG DataTransferLength; // data transfer length
94 UCHAR Flags; // CBW Flags endpoint direction
95 UCHAR LUN; // lun unit
96 UCHAR CommandBlockLength; // Command block length
97 UCHAR CommandBlock[16];
98 }CBW, *PCBW;
99
100 C_ASSERT(sizeof(CBW) == 31);
101
102
103 #define CBW_SIGNATURE 0x43425355
104 #define MAX_LUN 0xF
105
106 typedef struct
107 {
108 ULONG Signature; // CSW signature
109 ULONG Tag; // CSW tag
110 ULONG DataResidue; // CSW data transfer diff
111 UCHAR Status; // CSW status
112 }CSW, *PCSW;
113
114 //--------------------------------------------------------------------------------------------------------------------------------------------
115 //
116 // UFI INQUIRY command
117 //
118 typedef struct
119 {
120 UCHAR Code; // operation code 0x12
121 UCHAR LUN; // lun address
122 UCHAR PageCode; // product data information, always 0x00
123 UCHAR Reserved; // reserved 0x00
124 UCHAR AllocationLength; // length of inquiry data to be returned, default 36 bytes
125 UCHAR Reserved1[7]; //reserved bytes 0x00
126 }UFI_INQUIRY_CMD, *PUFI_INQUIRY_CMD;
127
128 C_ASSERT(sizeof(UFI_INQUIRY_CMD) == 12);
129
130 #define UFI_INQUIRY_CMD_LEN 0x6
131
132 //
133 // UFI INQUIRY command response
134 //
135 typedef struct
136 {
137 UCHAR DeviceType; // device type
138 UCHAR RMB; // removable media bit
139 UCHAR Version; // contains version 0x00
140 UCHAR Format; // response format
141 UCHAR Length; // additional length
142 UCHAR Reserved[3]; // reserved
143 UCHAR Vendor[8]; // vendor identification string
144 UCHAR Product[16]; // product identification string
145 UCHAR Revision[4]; // product revision code
146 }UFI_INQUIRY_RESPONSE, *PUFI_INQUIRY_RESPONSE;
147
148 C_ASSERT(sizeof(UFI_INQUIRY_RESPONSE) == 36);
149
150 //--------------------------------------------------------------------------------------------------------------------------------------------
151 //
152 // UFI read cmd
153 //
154 typedef struct
155 {
156 UCHAR Code; // operation code
157 UCHAR LUN; // lun
158 UCHAR LogicalBlockByte0; // lba byte 0
159 UCHAR LogicalBlockByte1; // lba byte 1
160 UCHAR LogicalBlockByte2; // lba byte 2
161 UCHAR LogicalBlockByte3; // lba byte 3
162 UCHAR Reserved; // reserved 0x00
163 UCHAR ContiguousLogicBlocksByte0; // msb contigious logic blocks byte
164 UCHAR ContiguousLogicBlocksByte1; // msb contigious logic blocks
165 UCHAR Reserved1[3]; // reserved 0x00
166 }UFI_READ_WRITE_CMD;
167
168 C_ASSERT(sizeof(UFI_READ_WRITE_CMD) == 12);
169
170 #define UFI_READ_WRITE_CMD_LEN (0xA)
171
172 //--------------------------------------------------------------------------------------------------------------------------------------------
173 //
174 // UFI read capacity cmd
175 //
176 typedef struct
177 {
178 UCHAR Code; // operation code 0x25
179 UCHAR LUN; // lun address
180 UCHAR LBA[4]; // logical block address, should be zero
181 UCHAR Reserved1[2]; // reserved 0x00
182 UCHAR PMI; // PMI = 0x00
183 UCHAR Reserved2[3]; // reserved 0x00
184 }UFI_CAPACITY_CMD, *PUFI_CAPACITY_CMD;
185
186 C_ASSERT(sizeof(UFI_CAPACITY_CMD) == 12);
187
188 #define UFI_CAPACITY_CMD_LEN 0xA //FIXME support length 16 too if requested
189
190 //
191 // UFI Read Capcacity command response
192 //
193 typedef struct
194 {
195 ULONG LastLogicalBlockAddress; // last logical block address
196 ULONG BlockLength; // block length in bytes
197 }UFI_CAPACITY_RESPONSE, *PUFI_CAPACITY_RESPONSE;
198
199 C_ASSERT(sizeof(UFI_CAPACITY_RESPONSE) == 8);
200
201 //--------------------------------------------------------------------------------------------------------------------------------------------
202 //
203 // UFI sense mode cmd
204 //
205 typedef struct
206 {
207 UCHAR Code; // operation code
208 UCHAR LUN; // lun address
209 UCHAR PageCode:6; // page code selector
210 UCHAR PC:2; // type of parameters to be returned
211 UCHAR Reserved[4]; // reserved 0x00
212 USHORT AllocationLength; // parameters length
213 UCHAR Reserved1[3];
214 }UFI_SENSE_CMD, *PUFI_SENSE_CMD;
215
216 C_ASSERT(sizeof(UFI_SENSE_CMD) == 12);
217
218 #define UFI_SENSE_CMD_LEN (6)
219
220 typedef struct
221 {
222 USHORT ModeDataLength; // length of parameters for sense cmd
223 UCHAR MediumTypeCode; // 00 for mass storage, 0x94 for floppy
224 UCHAR WP:1; // write protect bit
225 UCHAR Reserved1:2; // reserved 00
226 UCHAR DPOFUA:1; // should be zero
227 UCHAR Reserved2:4; // reserved
228 UCHAR Reserved[4]; // reserved
229 }UFI_MODE_PARAMETER_HEADER, *PUFI_MODE_PARAMETER_HEADER;
230
231
232 C_ASSERT(sizeof(UFI_MODE_PARAMETER_HEADER) == 8);
233
234 typedef struct
235 {
236 UCHAR PC;
237 UCHAR PageLength;
238 UCHAR Reserved1;
239 UCHAR ITM;
240 UCHAR Flags;
241 UCHAR Reserved[3];
242 }UFI_TIMER_PROTECT_PAGE, *PUFI_TIMER_PROTECT_PAGE;
243 C_ASSERT(sizeof(UFI_TIMER_PROTECT_PAGE) == 8);
244
245 //--------------------------------------------------------------------------------------------------------------------------------------------
246 //
247 // UFI test unit command
248 //
249
250 typedef struct
251 {
252 UCHAR Code; // operation code 0x00
253 UCHAR LUN; // lun
254 UCHAR Reserved[10]; // reserved 0x00
255 }UFI_TEST_UNIT_CMD, *PUFI_TEST_UNIT_CMD;
256
257 C_ASSERT(sizeof(UFI_TEST_UNIT_CMD) == 12);
258
259 #define UFI_TEST_UNIT_CMD_LEN (6)
260
261 typedef struct
262 {
263 union
264 {
265 PCBW cbw;
266 PCSW csw;
267 };
268 URB Urb;
269 PIRP Irp;
270 ULONG TransferDataLength;
271 PUCHAR TransferData;
272 PFDO_DEVICE_EXTENSION FDODeviceExtension;
273 PPDO_DEVICE_EXTENSION PDODeviceExtension;
274 PMDL TransferBufferMDL;
275 PKEVENT Event;
276 }IRP_CONTEXT, *PIRP_CONTEXT;
277
278 typedef struct _ERRORHANDLER_WORKITEM_DATA
279 {
280 PDEVICE_OBJECT DeviceObject;
281 PIRP_CONTEXT Context;
282 WORK_QUEUE_ITEM WorkQueueItem;
283 } ERRORHANDLER_WORKITEM_DATA, *PERRORHANDLER_WORKITEM_DATA;
284
285
286 //---------------------------------------------------------------------
287 //
288 // fdo.c routines
289 //
290 NTSTATUS
291 USBSTOR_FdoHandlePnp(
292 IN PDEVICE_OBJECT DeviceObject,
293 IN OUT PIRP Irp);
294
295 //---------------------------------------------------------------------
296 //
297 // pdo.c routines
298 //
299 NTSTATUS
300 USBSTOR_PdoHandlePnp(
301 IN PDEVICE_OBJECT DeviceObject,
302 IN OUT PIRP Irp);
303
304 NTSTATUS
305 USBSTOR_CreatePDO(
306 IN PDEVICE_OBJECT DeviceObject,
307 OUT PDEVICE_OBJECT *ChildDeviceObject);
308
309 //---------------------------------------------------------------------
310 //
311 // misc.c routines
312 //
313 NTSTATUS
314 NTAPI
315 USBSTOR_SyncForwardIrp(
316 IN PDEVICE_OBJECT DeviceObject,
317 IN OUT PIRP Irp);
318
319 NTSTATUS
320 NTAPI
321 USBSTOR_GetBusInterface(
322 IN PDEVICE_OBJECT DeviceObject,
323 OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface);
324
325 PVOID
326 AllocateItem(
327 IN POOL_TYPE PoolType,
328 IN ULONG ItemSize);
329
330 VOID
331 FreeItem(
332 IN PVOID Item);
333
334 NTSTATUS
335 USBSTOR_SyncUrbRequest(
336 IN PDEVICE_OBJECT DeviceObject,
337 OUT PURB UrbRequest);
338
339 NTSTATUS
340 USBSTOR_GetMaxLUN(
341 IN PDEVICE_OBJECT DeviceObject,
342 IN PFDO_DEVICE_EXTENSION DeviceExtension);
343
344 NTSTATUS
345 NTAPI
346 USBSTOR_SyncForwardIrpCompletionRoutine(
347 PDEVICE_OBJECT DeviceObject,
348 PIRP Irp,
349 PVOID Context);
350
351
352 //---------------------------------------------------------------------
353 //
354 // descriptor.c routines
355 //
356
357 NTSTATUS
358 USBSTOR_GetDescriptors(
359 IN PDEVICE_OBJECT DeviceObject);
360
361 NTSTATUS
362 USBSTOR_SelectConfigurationAndInterface(
363 IN PDEVICE_OBJECT DeviceObject,
364 IN PFDO_DEVICE_EXTENSION DeviceExtension);
365
366 NTSTATUS
367 USBSTOR_GetPipeHandles(
368 IN PFDO_DEVICE_EXTENSION DeviceExtension);
369
370 //---------------------------------------------------------------------
371 //
372 // scsi.c routines
373 //
374 NTSTATUS
375 USBSTOR_HandleExecuteSCSI(
376 IN PDEVICE_OBJECT DeviceObject,
377 IN PIRP Irp);
378
379 NTSTATUS
380 USBSTOR_SendInquiryCmd(
381 IN PDEVICE_OBJECT DeviceObject);
382
383 //---------------------------------------------------------------------
384 //
385 // disk.c routines
386 //
387 NTSTATUS
388 USBSTOR_HandleInternalDeviceControl(
389 IN PDEVICE_OBJECT DeviceObject,
390 IN PIRP Irp);
391
392 NTSTATUS
393 USBSTOR_HandleDeviceControl(
394 IN PDEVICE_OBJECT DeviceObject,
395 IN PIRP Irp);
396
397 //---------------------------------------------------------------------
398 //
399 // queue.c routines
400 //
401 VOID
402 NTAPI
403 USBSTOR_StartIo(
404 PDEVICE_OBJECT DeviceObject,
405 PIRP Irp);
406
407 VOID
408 USBSTOR_QueueFlushIrps(
409 IN PDEVICE_OBJECT DeviceObject);
410
411 VOID
412 USBSTOR_QueueRelease(
413 IN PDEVICE_OBJECT DeviceObject);
414
415 BOOLEAN
416 USBSTOR_QueueAddIrp(
417 IN PDEVICE_OBJECT DeviceObject,
418 IN PIRP Irp);
419
420 VOID
421 NTAPI
422 USBSTOR_CancelIo(
423 IN PDEVICE_OBJECT DeviceObject,
424 IN PIRP Irp);
425
426 VOID
427 USBSTOR_QueueInitialize(
428 PFDO_DEVICE_EXTENSION FDODeviceExtension);
429
430 VOID
431 NTAPI
432 ErrorHandlerWorkItemRoutine(
433 PVOID Context);
434
435 VOID
436 USBSTOR_QueueNextRequest(
437 IN PDEVICE_OBJECT DeviceObject);
438
439 VOID
440 USBSTOR_QueueTerminateRequest(
441 IN PDEVICE_OBJECT DeviceObject,
442 IN BOOLEAN ModifySrbState);