[USBSTOR] Keep CBW and CSW inside an IRP context.
[reactos.git] / drivers / usb / usbstor / usbstor.h
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 #define USB_MAXCHILDREN (16)
12 #define USBSTOR_DEFAULT_MAX_TRANSFER_LENGTH 0x10000
13
14 #define HTONS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
15 #define NTOHS(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))
16
17 #define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
18 ((((unsigned long)(n) & 0xFF00)) << 8) | \
19 ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
20 ((((unsigned long)(n) & 0xFF000000)) >> 24))
21
22
23 #define NTOHL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
24 ((((unsigned long)(n) & 0xFF00)) << 8) | \
25 ((((unsigned long)(n) & 0xFF0000)) >> 8) | \
26 ((((unsigned long)(n) & 0xFF000000)) >> 24))
27
28 #define USB_RECOVERABLE_ERRORS (USBD_STATUS_STALL_PID | USBD_STATUS_DEV_NOT_RESPONDING \
29 | USBD_STATUS_ENDPOINT_HALTED | USBD_STATUS_NO_BANDWIDTH)
30
31 typedef struct __COMMON_DEVICE_EXTENSION__
32 {
33 BOOLEAN IsFDO;
34
35 }USBSTOR_COMMON_DEVICE_EXTENSION, *PUSBSTOR_COMMON_DEVICE_EXTENSION;
36
37 typedef struct
38 {
39 USBSTOR_COMMON_DEVICE_EXTENSION Common; // common device extension
40
41 PDEVICE_OBJECT FunctionalDeviceObject; // functional device object
42 PDEVICE_OBJECT PhysicalDeviceObject; // physical device object
43 PDEVICE_OBJECT LowerDeviceObject; // lower device object
44 USB_BUS_INTERFACE_USBDI_V2 BusInterface; // bus interface of device
45 PUSB_DEVICE_DESCRIPTOR DeviceDescriptor; // usb device descriptor
46 PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor; // usb configuration descriptor
47 PUSB_STRING_DESCRIPTOR SerialNumber; // usb serial number
48 PUSBD_INTERFACE_INFORMATION InterfaceInformation; // usb interface information
49 USBD_CONFIGURATION_HANDLE ConfigurationHandle; // usb configuration handle
50 UCHAR BulkInPipeIndex; // bulk in pipe index
51 UCHAR BulkOutPipeIndex; // bulk out pipe index
52 UCHAR MaxLUN; // max lun for device
53 PDEVICE_OBJECT ChildPDO[16]; // max 16 child pdo devices
54 KSPIN_LOCK IrpListLock; // irp list lock
55 LIST_ENTRY IrpListHead; // irp list head
56 BOOLEAN IrpListFreeze; // if true the irp list is freezed
57 BOOLEAN ResetInProgress; // if hard reset is in progress
58 ULONG IrpPendingCount; // count of irp pending
59 PSCSI_REQUEST_BLOCK ActiveSrb; // stores the current active SRB
60 KEVENT NoPendingRequests; // set if no pending or in progress requests
61 PSCSI_REQUEST_BLOCK LastTimerActiveSrb; // last timer tick active srb
62 ULONG SrbErrorHandlingActive; // error handling of srb is activated
63 ULONG TimerWorkQueueEnabled; // timer work queue enabled
64 ULONG InstanceCount; // pdo instance count
65 }FDO_DEVICE_EXTENSION, *PFDO_DEVICE_EXTENSION;
66
67 typedef struct
68 {
69 USBSTOR_COMMON_DEVICE_EXTENSION Common;
70 PDEVICE_OBJECT LowerDeviceObject; // points to FDO
71 UCHAR LUN; // lun id
72 PINQUIRYDATA InquiryData; // USB SCSI inquiry data
73 PUCHAR FormatData; // USB SCSI Read Format Capacity Data
74 UCHAR Claimed; // indicating if it has been claimed by upper driver
75 ULONG BlockLength; // length of block
76 ULONG LastLogicBlockAddress; // last block address
77 PDEVICE_OBJECT *PDODeviceObject; // entry in pdo list
78 PDEVICE_OBJECT Self; // self
79 UCHAR MediumTypeCode; // floppy medium type code
80 UCHAR IsFloppy; // is device floppy
81 }PDO_DEVICE_EXTENSION, *PPDO_DEVICE_EXTENSION;
82
83 //
84 // max lun command identifier
85 //
86 #define USB_BULK_GET_MAX_LUN 0xFE
87 #define USB_BULK_RESET_DEVICE 0xFF
88
89 #include <pshpack1.h>
90 typedef struct
91 {
92 ULONG Signature; // CBW signature
93 ULONG Tag; // CBW Tag of operation
94 ULONG DataTransferLength; // data transfer length
95 UCHAR Flags; // CBW Flags endpoint direction
96 UCHAR LUN; // lun unit
97 UCHAR CommandBlockLength; // Command block length
98 UCHAR CommandBlock[16];
99 }CBW, *PCBW;
100
101 C_ASSERT(sizeof(CBW) == 31);
102
103
104 #define CBW_SIGNATURE 0x43425355
105 #define CSW_SIGNATURE 0x53425355
106
107 #define MAX_LUN 0xF
108
109 #define CSW_STATUS_COMMAND_PASSED 0x00
110 #define CSW_STATUS_COMMAND_FAILED 0x01
111 #define CSW_STATUS_PHASE_ERROR 0x02
112
113 typedef struct
114 {
115 ULONG Signature; // CSW signature
116 ULONG Tag; // CSW tag
117 ULONG DataResidue; // CSW data transfer diff
118 UCHAR Status; // CSW status
119 }CSW, *PCSW;
120
121 //--------------------------------------------------------------------------------------------------------------------------------------------
122 //
123 // UFI read cmd
124 //
125 typedef struct
126 {
127 UCHAR Code; // operation code
128 UCHAR LUN; // lun
129 UCHAR LogicalBlockByte0; // lba byte 0
130 UCHAR LogicalBlockByte1; // lba byte 1
131 UCHAR LogicalBlockByte2; // lba byte 2
132 UCHAR LogicalBlockByte3; // lba byte 3
133 UCHAR Reserved; // reserved 0x00
134 UCHAR ContiguousLogicBlocksByte0; // msb contiguous logic blocks byte
135 UCHAR ContiguousLogicBlocksByte1; // msb contiguous logic blocks
136 UCHAR Reserved1[3]; // reserved 0x00
137 }UFI_READ_WRITE_CMD;
138
139 C_ASSERT(sizeof(UFI_READ_WRITE_CMD) == 12);
140
141 #define UFI_READ_WRITE_CMD_LEN (0xA)
142
143 //--------------------------------------------------------------------------------------------------------------------------------------------
144 //
145 // UFI read capacity cmd
146 //
147 typedef struct
148 {
149 UCHAR Code; // operation code 0x25
150 UCHAR LUN; // lun address
151 UCHAR LBA[4]; // logical block address, should be zero
152 UCHAR Reserved1[2]; // reserved 0x00
153 UCHAR PMI; // PMI = 0x00
154 UCHAR Reserved2[3]; // reserved 0x00
155 }UFI_CAPACITY_CMD, *PUFI_CAPACITY_CMD;
156
157 C_ASSERT(sizeof(UFI_CAPACITY_CMD) == 12);
158
159 #define UFI_CAPACITY_CMD_LEN 0xA //FIXME support length 16 too if requested
160
161 //
162 // UFI Read Capacity command response
163 //
164 typedef struct
165 {
166 ULONG LastLogicalBlockAddress; // last logical block address
167 ULONG BlockLength; // block length in bytes
168 }UFI_CAPACITY_RESPONSE, *PUFI_CAPACITY_RESPONSE;
169
170 #define UFI_READ_CAPACITY_CMD_LEN 0xA
171 C_ASSERT(sizeof(UFI_CAPACITY_RESPONSE) == 8);
172
173 //--------------------------------------------------------------------------------------------------------------------------------------------
174 //
175 // UFI sense mode cmd
176 //
177 typedef struct
178 {
179 UCHAR Code; // operation code
180 UCHAR LUN; // lun address
181 UCHAR PageCode:6; // page code selector
182 UCHAR PC:2; // type of parameters to be returned
183 UCHAR Reserved[4]; // reserved 0x00
184 USHORT AllocationLength; // parameters length
185 UCHAR Reserved1[3];
186 }UFI_SENSE_CMD, *PUFI_SENSE_CMD;
187
188 C_ASSERT(sizeof(UFI_SENSE_CMD) == 12);
189
190 #define UFI_SENSE_CMD_LEN (6)
191
192 typedef struct
193 {
194 USHORT ModeDataLength; // length of parameters for sense cmd
195 UCHAR MediumTypeCode; // 00 for mass storage, 0x94 for floppy
196 UCHAR WP:1; // write protect bit
197 UCHAR Reserved1:2; // reserved 00
198 UCHAR DPOFUA:1; // should be zero
199 UCHAR Reserved2:4; // reserved
200 UCHAR Reserved[4]; // reserved
201 }UFI_MODE_PARAMETER_HEADER, *PUFI_MODE_PARAMETER_HEADER;
202
203
204 C_ASSERT(sizeof(UFI_MODE_PARAMETER_HEADER) == 8);
205
206 typedef struct
207 {
208 UCHAR PC;
209 UCHAR PageLength;
210 UCHAR Reserved1;
211 UCHAR ITM;
212 UCHAR Flags;
213 UCHAR Reserved[3];
214 }UFI_TIMER_PROTECT_PAGE, *PUFI_TIMER_PROTECT_PAGE;
215 C_ASSERT(sizeof(UFI_TIMER_PROTECT_PAGE) == 8);
216
217 //--------------------------------------------------------------------------------------------------------------------------------------------
218 //
219 // UFI read capacity cmd
220 //
221
222 typedef struct
223 {
224 UCHAR Code;
225 UCHAR LUN;
226 UCHAR Reserved[5];
227 UCHAR AllocationLengthMsb;
228 UCHAR AllocationLengthLsb;
229 UCHAR Reserved1[3];
230 }UFI_READ_FORMAT_CAPACITY, *PUFI_READ_FORMAT_CAPACITY;
231
232 C_ASSERT(sizeof(UFI_READ_FORMAT_CAPACITY) == 12);
233
234 #define UFI_READ_FORMAT_CAPACITY_CMD_LEN (10)
235
236 typedef struct
237 {
238 UCHAR Reserved1;
239 UCHAR Reserved2;
240 UCHAR Reserved3;
241 UCHAR CapacityLength;
242 }UFI_CAPACITY_FORMAT_HEADER, *PUFI_CAPACITY_FORMAT_HEADER;
243
244 C_ASSERT(sizeof(UFI_CAPACITY_FORMAT_HEADER) == 4);
245
246 typedef struct
247 {
248 ULONG BlockCount;
249 UCHAR Code;
250 UCHAR BlockLengthByte0;
251 UCHAR BlockLengthByte1;
252 UCHAR BlockLengthByte2;
253 }UFI_CAPACITY_DESCRIPTOR, *PUFI_CAPACITY_DESCRIPTOR;
254
255 #define UNFORMATTED_MEDIA_CODE_DESCRIPTORY_TYPE (1)
256 #define FORMAT_MEDIA_CODE_DESCRIPTOR_TYPE (2)
257 #define CARTRIDGE_MEDIA_CODE_DESCRIPTOR_TYPE (3)
258
259
260
261
262 //--------------------------------------------------------------------------------------------------------------------------------------------
263 //
264 // UFI test unit command
265 //
266
267 typedef struct
268 {
269 UCHAR Code; // operation code 0x00
270 UCHAR LUN; // lun
271 UCHAR Reserved[10]; // reserved 0x00
272 }UFI_TEST_UNIT_CMD, *PUFI_TEST_UNIT_CMD;
273
274 C_ASSERT(sizeof(UFI_TEST_UNIT_CMD) == 12);
275
276 #define UFI_TEST_UNIT_CMD_LEN (6)
277
278 //-------------------------------------------------------------------------------------------------------------------------------------------
279 typedef struct
280 {
281 UCHAR Bytes[16];
282 }UFI_UNKNOWN_CMD, *PUFI_UNKNOWN_CMD;
283
284 typedef struct
285 {
286 PIRP Irp;
287 PFDO_DEVICE_EXTENSION FDODeviceExtension;
288 ULONG ErrorIndex;
289 ULONG RetryCount;
290 union
291 {
292 CBW cbw;
293 CSW csw;
294 };
295 URB Urb;
296 } IRP_CONTEXT, *PIRP_CONTEXT;
297
298 typedef struct _ERRORHANDLER_WORKITEM_DATA
299 {
300 PDEVICE_OBJECT DeviceObject;
301 PIRP_CONTEXT Context;
302 WORK_QUEUE_ITEM WorkQueueItem;
303 PIRP Irp;
304 } ERRORHANDLER_WORKITEM_DATA, *PERRORHANDLER_WORKITEM_DATA;
305
306
307 //---------------------------------------------------------------------
308 //
309 // fdo.c routines
310 //
311 NTSTATUS
312 USBSTOR_FdoHandlePnp(
313 IN PDEVICE_OBJECT DeviceObject,
314 IN OUT PIRP Irp);
315
316 //---------------------------------------------------------------------
317 //
318 // pdo.c routines
319 //
320 NTSTATUS
321 USBSTOR_PdoHandlePnp(
322 IN PDEVICE_OBJECT DeviceObject,
323 IN OUT PIRP Irp);
324
325 NTSTATUS
326 USBSTOR_CreatePDO(
327 IN PDEVICE_OBJECT DeviceObject,
328 IN UCHAR LUN);
329
330 //---------------------------------------------------------------------
331 //
332 // misc.c routines
333 //
334 NTSTATUS
335 NTAPI
336 USBSTOR_SyncForwardIrp(
337 IN PDEVICE_OBJECT DeviceObject,
338 IN OUT PIRP Irp);
339
340 NTSTATUS
341 NTAPI
342 USBSTOR_GetBusInterface(
343 IN PDEVICE_OBJECT DeviceObject,
344 OUT PUSB_BUS_INTERFACE_USBDI_V2 BusInterface);
345
346 PVOID
347 AllocateItem(
348 IN POOL_TYPE PoolType,
349 IN ULONG ItemSize);
350
351 VOID
352 FreeItem(
353 IN PVOID Item);
354
355 NTSTATUS
356 USBSTOR_SyncUrbRequest(
357 IN PDEVICE_OBJECT DeviceObject,
358 OUT PURB UrbRequest);
359
360 NTSTATUS
361 USBSTOR_GetMaxLUN(
362 IN PDEVICE_OBJECT DeviceObject,
363 IN PFDO_DEVICE_EXTENSION DeviceExtension);
364
365 NTSTATUS
366 NTAPI
367 USBSTOR_SyncForwardIrpCompletionRoutine(
368 PDEVICE_OBJECT DeviceObject,
369 PIRP Irp,
370 PVOID Context);
371
372 NTSTATUS
373 USBSTOR_ResetDevice(
374 IN PDEVICE_OBJECT DeviceObject,
375 IN PFDO_DEVICE_EXTENSION DeviceExtension);
376
377 BOOLEAN
378 USBSTOR_IsFloppy(
379 IN PUCHAR Buffer,
380 IN ULONG BufferLength,
381 OUT PUCHAR MediumTypeCode);
382
383 //---------------------------------------------------------------------
384 //
385 // descriptor.c routines
386 //
387
388 NTSTATUS
389 USBSTOR_GetDescriptors(
390 IN PDEVICE_OBJECT DeviceObject);
391
392 NTSTATUS
393 USBSTOR_SelectConfigurationAndInterface(
394 IN PDEVICE_OBJECT DeviceObject,
395 IN PFDO_DEVICE_EXTENSION DeviceExtension);
396
397 NTSTATUS
398 USBSTOR_GetPipeHandles(
399 IN PFDO_DEVICE_EXTENSION DeviceExtension);
400
401 //---------------------------------------------------------------------
402 //
403 // scsi.c routines
404 //
405 NTSTATUS
406 USBSTOR_HandleExecuteSCSI(
407 IN PDEVICE_OBJECT DeviceObject,
408 IN PIRP Irp,
409 IN ULONG RetryCount);
410
411 NTSTATUS
412 USBSTOR_SendCSWRequest(
413 PIRP_CONTEXT Context,
414 PIRP Irp);
415
416
417 //---------------------------------------------------------------------
418 //
419 // disk.c routines
420 //
421 NTSTATUS
422 USBSTOR_HandleInternalDeviceControl(
423 IN PDEVICE_OBJECT DeviceObject,
424 IN PIRP Irp);
425
426 NTSTATUS
427 USBSTOR_HandleDeviceControl(
428 IN PDEVICE_OBJECT DeviceObject,
429 IN PIRP Irp);
430
431 //---------------------------------------------------------------------
432 //
433 // queue.c routines
434 //
435 VOID
436 NTAPI
437 USBSTOR_StartIo(
438 PDEVICE_OBJECT DeviceObject,
439 PIRP Irp);
440
441 VOID
442 USBSTOR_QueueWaitForPendingRequests(
443 IN PDEVICE_OBJECT DeviceObject);
444
445 VOID
446 USBSTOR_QueueRelease(
447 IN PDEVICE_OBJECT DeviceObject);
448
449 BOOLEAN
450 USBSTOR_QueueAddIrp(
451 IN PDEVICE_OBJECT DeviceObject,
452 IN PIRP Irp);
453
454 VOID
455 NTAPI
456 USBSTOR_CancelIo(
457 IN PDEVICE_OBJECT DeviceObject,
458 IN PIRP Irp);
459
460 VOID
461 USBSTOR_QueueInitialize(
462 PFDO_DEVICE_EXTENSION FDODeviceExtension);
463
464 VOID
465 NTAPI
466 ErrorHandlerWorkItemRoutine(
467 PVOID Context);
468
469 VOID
470 NTAPI
471 ResetHandlerWorkItemRoutine(
472 PVOID Context);
473
474
475
476 VOID
477 USBSTOR_QueueNextRequest(
478 IN PDEVICE_OBJECT DeviceObject);
479
480 VOID
481 USBSTOR_QueueTerminateRequest(
482 IN PDEVICE_OBJECT DeviceObject,
483 IN PIRP Irp);
484
485 /* error.c */
486 NTSTATUS
487 USBSTOR_GetEndpointStatus(
488 IN PDEVICE_OBJECT DeviceObject,
489 IN UCHAR bEndpointAddress,
490 OUT PUSHORT Value);
491
492 NTSTATUS
493 USBSTOR_ResetPipeWithHandle(
494 IN PDEVICE_OBJECT DeviceObject,
495 IN USBD_PIPE_HANDLE PipeHandle);
496
497 VOID
498 NTAPI
499 USBSTOR_TimerRoutine(
500 PDEVICE_OBJECT DeviceObject,
501 PVOID Context);
502
503 #endif /* _USBSTOR_H_ */