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