[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 Irp;
282 PIRP_CONTEXT Context;
283 WORK_QUEUE_ITEM WorkQueueItem;
284 } ERRORHANDLER_WORKITEM_DATA, *PERRORHANDLER_WORKITEM_DATA;
285
286
287 //---------------------------------------------------------------------
288 //
289 // fdo.c routines
290 //
291 NTSTATUS
292 USBSTOR_FdoHandlePnp(
293 IN PDEVICE_OBJECT DeviceObject,
294 IN OUT PIRP Irp);
295
296 //---------------------------------------------------------------------
297 //
298 // pdo.c routines
299 //
300 NTSTATUS
301 USBSTOR_PdoHandlePnp(
302 IN PDEVICE_OBJECT DeviceObject,
303 IN OUT PIRP Irp);
304
305 NTSTATUS
306 USBSTOR_CreatePDO(
307 IN PDEVICE_OBJECT DeviceObject,
308 OUT PDEVICE_OBJECT *ChildDeviceObject);
309
310 //---------------------------------------------------------------------
311 //
312 // misc.c routines
313 //
314 NTSTATUS
315 NTAPI
316 USBSTOR_SyncForwardIrp(
317 IN PDEVICE_OBJECT DeviceObject,
318 IN OUT PIRP Irp);
319
320 NTSTATUS
321 NTAPI
322 USBSTOR_GetBusInterface(
323 IN PDEVICE_OBJECT DeviceObject,
324 OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface);
325
326 PVOID
327 AllocateItem(
328 IN POOL_TYPE PoolType,
329 IN ULONG ItemSize);
330
331 VOID
332 FreeItem(
333 IN PVOID Item);
334
335 NTSTATUS
336 USBSTOR_SyncUrbRequest(
337 IN PDEVICE_OBJECT DeviceObject,
338 OUT PURB UrbRequest);
339
340 NTSTATUS
341 USBSTOR_GetMaxLUN(
342 IN PDEVICE_OBJECT DeviceObject,
343 IN PFDO_DEVICE_EXTENSION DeviceExtension);
344
345 NTSTATUS
346 NTAPI
347 USBSTOR_SyncForwardIrpCompletionRoutine(
348 PDEVICE_OBJECT DeviceObject,
349 PIRP Irp,
350 PVOID Context);
351
352
353 //---------------------------------------------------------------------
354 //
355 // descriptor.c routines
356 //
357
358 NTSTATUS
359 USBSTOR_GetDescriptors(
360 IN PDEVICE_OBJECT DeviceObject);
361
362 NTSTATUS
363 USBSTOR_SelectConfigurationAndInterface(
364 IN PDEVICE_OBJECT DeviceObject,
365 IN PFDO_DEVICE_EXTENSION DeviceExtension);
366
367 NTSTATUS
368 USBSTOR_GetPipeHandles(
369 IN PFDO_DEVICE_EXTENSION DeviceExtension);
370
371 //---------------------------------------------------------------------
372 //
373 // scsi.c routines
374 //
375 NTSTATUS
376 USBSTOR_HandleExecuteSCSI(
377 IN PDEVICE_OBJECT DeviceObject,
378 IN PIRP Irp);
379
380 NTSTATUS
381 USBSTOR_SendInquiryCmd(
382 IN PDEVICE_OBJECT DeviceObject);
383
384 //---------------------------------------------------------------------
385 //
386 // disk.c routines
387 //
388 NTSTATUS
389 USBSTOR_HandleInternalDeviceControl(
390 IN PDEVICE_OBJECT DeviceObject,
391 IN PIRP Irp);
392
393 NTSTATUS
394 USBSTOR_HandleDeviceControl(
395 IN PDEVICE_OBJECT DeviceObject,
396 IN PIRP Irp);
397
398 //---------------------------------------------------------------------
399 //
400 // queue.c routines
401 //
402 VOID
403 NTAPI
404 USBSTOR_StartIo(
405 PDEVICE_OBJECT DeviceObject,
406 PIRP Irp);
407
408 VOID
409 USBSTOR_QueueFlushIrps(
410 IN PDEVICE_OBJECT DeviceObject);
411
412 VOID
413 USBSTOR_QueueRelease(
414 IN PDEVICE_OBJECT DeviceObject);
415
416 BOOLEAN
417 USBSTOR_QueueAddIrp(
418 IN PDEVICE_OBJECT DeviceObject,
419 IN PIRP Irp);
420
421 VOID
422 NTAPI
423 USBSTOR_CancelIo(
424 IN PDEVICE_OBJECT DeviceObject,
425 IN PIRP Irp);
426
427 VOID
428 USBSTOR_QueueInitialize(
429 PFDO_DEVICE_EXTENSION FDODeviceExtension);
430
431 VOID
432 NTAPI
433 ErrorHandlerWorkItemRoutine(
434 PVOID Context);
435
436 VOID
437 USBSTOR_QueueNextRequest(
438 IN PDEVICE_OBJECT DeviceObject);
439
440 VOID
441 USBSTOR_QueueTerminateRequest(
442 IN PDEVICE_OBJECT DeviceObject,
443 IN BOOLEAN ModifySrbState);