2 * PROJECT: ReactOS Universal Serial Bus Bulk Storage Driver
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/usb/usbstor/disk.c
5 * PURPOSE: USB block storage device driver.
8 * Michael Martin (michael.martin@reactos.org)
9 * Johannes Anderwald (johannes.anderwald@reactos.org)
15 USBSTOR_HandleInternalDeviceControl(
16 IN PDEVICE_OBJECT DeviceObject
,
19 PIO_STACK_LOCATION IoStack
;
20 PSCSI_REQUEST_BLOCK Request
;
21 PPDO_DEVICE_EXTENSION PDODeviceExtension
;
25 // get current stack location
27 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
32 Request
= (PSCSI_REQUEST_BLOCK
)IoStack
->Parameters
.Others
.Argument1
;
40 // get device extension
42 PDODeviceExtension
= (PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
47 ASSERT(PDODeviceExtension
->Common
.IsFDO
== FALSE
);
49 switch(Request
->Function
)
51 case SRB_FUNCTION_EXECUTE_SCSI
:
53 DPRINT("SRB_FUNCTION_EXECUTE_SCSI\n");
56 // check if request is valid
58 if (Request
->SrbFlags
& (SRB_FLAGS_DATA_IN
| SRB_FLAGS_DATA_OUT
))
61 // data is transferred with this irp
63 if ((Request
->SrbFlags
& (SRB_FLAGS_DATA_IN
| SRB_FLAGS_DATA_OUT
)) == (SRB_FLAGS_DATA_IN
| SRB_FLAGS_DATA_OUT
) ||
64 Request
->DataTransferLength
== 0 ||
65 Irp
->MdlAddress
== NULL
)
70 Status
= STATUS_INVALID_PARAMETER
;
77 // sense buffer request
79 if (Request
->DataTransferLength
||
80 Request
->DataBuffer
||
86 Status
= STATUS_INVALID_PARAMETER
;
94 if (!USBSTOR_QueueAddIrp(PDODeviceExtension
->LowerDeviceObject
, Irp
))
97 // irp was not added to the queue
99 IoStartPacket(PDODeviceExtension
->LowerDeviceObject
, Irp
, &Request
->QueueSortKey
, USBSTOR_CancelIo
);
105 return STATUS_PENDING
;
107 case SRB_FUNCTION_RELEASE_DEVICE
:
109 DPRINT1("SRB_FUNCTION_RELEASE_DEVICE\n");
113 ASSERT(PDODeviceExtension
->Claimed
== TRUE
);
118 PDODeviceExtension
->Claimed
= FALSE
;
119 Status
= STATUS_SUCCESS
;
122 case SRB_FUNCTION_CLAIM_DEVICE
:
124 DPRINT1("SRB_FUNCTION_CLAIM_DEVICE\n");
126 // check if the device has been claimed
128 if (PDODeviceExtension
->Claimed
)
131 // device has already been claimed
133 Status
= STATUS_DEVICE_BUSY
;
134 Request
->SrbStatus
= SRB_STATUS_BUSY
;
141 PDODeviceExtension
->Claimed
= TRUE
;
144 // output device object
146 Request
->DataBuffer
= DeviceObject
;
149 // completed successfully
151 Status
= STATUS_SUCCESS
;
154 case SRB_FUNCTION_RELEASE_QUEUE
:
156 DPRINT1("SRB_FUNCTION_RELEASE_QUEUE\n");
161 USBSTOR_QueueRelease(PDODeviceExtension
->LowerDeviceObject
);
164 // set status success
166 Request
->SrbStatus
= SRB_STATUS_SUCCESS
;
167 Status
= STATUS_SUCCESS
;
171 case SRB_FUNCTION_SHUTDOWN
:
172 case SRB_FUNCTION_FLUSH
:
173 case SRB_FUNCTION_FLUSH_QUEUE
:
175 DPRINT1("SRB_FUNCTION_FLUSH / SRB_FUNCTION_FLUSH_QUEUE / SRB_FUNCTION_SHUTDOWN\n");
177 // HACK: don't flush pending requests
178 #if 0 // we really need a proper storage stack
180 // wait for pending requests to finish
182 USBSTOR_QueueWaitForPendingRequests(PDODeviceExtension
->LowerDeviceObject
);
185 // set status success
187 Request
->SrbStatus
= SRB_STATUS_SUCCESS
;
188 Status
= STATUS_SUCCESS
;
196 Status
= STATUS_NOT_SUPPORTED
;
197 Request
->SrbStatus
= SRB_STATUS_ERROR
;
204 Irp
->IoStatus
.Status
= Status
;
205 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
210 USBSTOR_GetFieldLength(
215 ULONG LastCharacterPosition
= 0;
218 // scan the field and return last positon which contains a valid character
220 for(Index
= 0; Index
< MaxLength
; Index
++)
222 if (Name
[Index
] != ' ')
225 // trim white spaces from field
227 LastCharacterPosition
= Index
;
232 // convert from zero based index to length
234 return LastCharacterPosition
+ 1;
238 USBSTOR_HandleQueryProperty(
239 IN PDEVICE_OBJECT DeviceObject
,
242 PIO_STACK_LOCATION IoStack
;
243 PSTORAGE_PROPERTY_QUERY PropertyQuery
;
244 PSTORAGE_DESCRIPTOR_HEADER DescriptorHeader
;
245 PSTORAGE_ADAPTER_DESCRIPTOR AdapterDescriptor
;
246 ULONG FieldLengthVendor
, FieldLengthProduct
, FieldLengthRevision
, TotalLength
, FieldLengthSerialNumber
;
247 PPDO_DEVICE_EXTENSION PDODeviceExtension
;
248 PUFI_INQUIRY_RESPONSE InquiryData
;
249 PSTORAGE_DEVICE_DESCRIPTOR DeviceDescriptor
;
251 PFDO_DEVICE_EXTENSION FDODeviceExtension
;
252 UNICODE_STRING SerialNumber
;
253 ANSI_STRING AnsiString
;
256 DPRINT("USBSTOR_HandleQueryProperty\n");
259 // get current stack location
261 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
266 ASSERT(IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
>= sizeof(STORAGE_PROPERTY_QUERY
));
267 ASSERT(Irp
->AssociatedIrp
.SystemBuffer
);
270 // get property query
272 PropertyQuery
= (PSTORAGE_PROPERTY_QUERY
)Irp
->AssociatedIrp
.SystemBuffer
;
275 // check property type
277 if (PropertyQuery
->PropertyId
!= StorageDeviceProperty
&&
278 PropertyQuery
->PropertyId
!= StorageAdapterProperty
)
281 // only device property / adapter property are supported
283 return STATUS_INVALID_PARAMETER_1
;
289 if (PropertyQuery
->QueryType
== PropertyExistsQuery
)
292 // device property / adapter property is supported
294 return STATUS_SUCCESS
;
297 if (PropertyQuery
->QueryType
!= PropertyStandardQuery
)
300 // only standard query and exists query are supported
302 return STATUS_INVALID_PARAMETER_2
;
306 // check if it is a device property
308 if (PropertyQuery
->PropertyId
== StorageDeviceProperty
)
310 DPRINT("USBSTOR_HandleQueryProperty StorageDeviceProperty OutputBufferLength %lu\n", IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
);
313 // get device extension
315 PDODeviceExtension
= (PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
316 ASSERT(PDODeviceExtension
);
317 ASSERT(PDODeviceExtension
->Common
.IsFDO
== FALSE
);
320 // get device extension
322 FDODeviceExtension
= (PFDO_DEVICE_EXTENSION
)PDODeviceExtension
->LowerDeviceObject
->DeviceExtension
;
323 ASSERT(FDODeviceExtension
);
324 ASSERT(FDODeviceExtension
->Common
.IsFDO
);
329 InquiryData
= (PUFI_INQUIRY_RESPONSE
)PDODeviceExtension
->InquiryData
;
333 // compute extra parameters length
335 FieldLengthVendor
= USBSTOR_GetFieldLength(InquiryData
->Vendor
, 8);
336 FieldLengthProduct
= USBSTOR_GetFieldLength(InquiryData
->Product
, 16);
337 FieldLengthRevision
= USBSTOR_GetFieldLength(InquiryData
->Revision
, 4);
340 // is there a serial number
342 if (FDODeviceExtension
->SerialNumber
)
347 FieldLengthSerialNumber
= wcslen(FDODeviceExtension
->SerialNumber
->bString
);
354 FieldLengthSerialNumber
= 0;
358 // total length required is sizeof(STORAGE_DEVICE_DESCRIPTOR) + FieldLength + 4 extra null bytes - 1
359 // -1 due STORAGE_DEVICE_DESCRIPTOR contains one byte length of parameter data
361 TotalLength
= sizeof(STORAGE_DEVICE_DESCRIPTOR
) + FieldLengthVendor
+ FieldLengthProduct
+ FieldLengthRevision
+ FieldLengthSerialNumber
+ 3;
364 // check if output buffer is long enough
366 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< TotalLength
)
371 DescriptorHeader
= (PSTORAGE_DESCRIPTOR_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
372 ASSERT(IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
>= sizeof(STORAGE_DESCRIPTOR_HEADER
));
375 // return required size
377 DescriptorHeader
->Version
= TotalLength
;
378 DescriptorHeader
->Size
= TotalLength
;
380 Irp
->IoStatus
.Information
= sizeof(STORAGE_DESCRIPTOR_HEADER
);
381 return STATUS_SUCCESS
;
385 // get device descriptor
387 DeviceDescriptor
= (PSTORAGE_DEVICE_DESCRIPTOR
)Irp
->AssociatedIrp
.SystemBuffer
;
390 // initialize device descriptor
392 DeviceDescriptor
->Version
= TotalLength
;
393 DeviceDescriptor
->Size
= TotalLength
;
394 DeviceDescriptor
->DeviceType
= InquiryData
->DeviceType
;
395 DeviceDescriptor
->DeviceTypeModifier
= (InquiryData
->RMB
& 0x7F);
396 DeviceDescriptor
->RemovableMedia
= (InquiryData
->RMB
& 0x80) ? TRUE
: FALSE
;
397 DeviceDescriptor
->CommandQueueing
= FALSE
;
398 DeviceDescriptor
->BusType
= BusTypeUsb
;
399 DeviceDescriptor
->VendorIdOffset
= sizeof(STORAGE_DEVICE_DESCRIPTOR
) - sizeof(UCHAR
);
400 DeviceDescriptor
->ProductIdOffset
= DeviceDescriptor
->VendorIdOffset
+ FieldLengthVendor
+ 1;
401 DeviceDescriptor
->ProductRevisionOffset
= DeviceDescriptor
->ProductIdOffset
+ FieldLengthProduct
+ 1;
402 DeviceDescriptor
->SerialNumberOffset
= (FieldLengthSerialNumber
> 0 ? DeviceDescriptor
->ProductRevisionOffset
+ FieldLengthRevision
+ 1 : 0);
403 DeviceDescriptor
->RawPropertiesLength
= FieldLengthVendor
+ FieldLengthProduct
+ FieldLengthRevision
+ FieldLengthSerialNumber
+ 3 + (FieldLengthSerialNumber
> 0 ? + 1 : 0);
408 Buffer
= (PUCHAR
)((ULONG_PTR
)DeviceDescriptor
+ sizeof(STORAGE_DEVICE_DESCRIPTOR
) - sizeof(UCHAR
));
413 RtlCopyMemory(Buffer
, InquiryData
->Vendor
, FieldLengthVendor
);
414 Buffer
[FieldLengthVendor
] = '\0';
415 Buffer
+= FieldLengthVendor
+ 1;
420 RtlCopyMemory(Buffer
, InquiryData
->Product
, FieldLengthProduct
);
421 Buffer
[FieldLengthProduct
] = '\0';
422 Buffer
+= FieldLengthProduct
+ 1;
427 RtlCopyMemory(Buffer
, InquiryData
->Revision
, FieldLengthRevision
);
428 Buffer
[FieldLengthRevision
] = '\0';
429 Buffer
+= FieldLengthRevision
+ 1;
432 // copy serial number
434 if (FieldLengthSerialNumber
)
437 // init unicode string
439 RtlInitUnicodeString(&SerialNumber
, FDODeviceExtension
->SerialNumber
->bString
);
444 AnsiString
.Buffer
= (PCHAR
)Buffer
;
445 AnsiString
.Length
= 0;
446 AnsiString
.MaximumLength
= FieldLengthSerialNumber
* sizeof(WCHAR
);
449 // convert to ansi code
451 Status
= RtlUnicodeStringToAnsiString(&AnsiString
, &SerialNumber
, FALSE
);
452 ASSERT(Status
== STATUS_SUCCESS
);
456 DPRINT("Vendor %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->VendorIdOffset
));
457 DPRINT("Product %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->ProductIdOffset
));
458 DPRINT("Revision %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->ProductRevisionOffset
));
459 DPRINT("Serial %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->SerialNumberOffset
));
464 Irp
->IoStatus
.Information
= TotalLength
;
465 return STATUS_SUCCESS
;
470 // adapter property query request
472 DPRINT("USBSTOR_HandleQueryProperty StorageAdapterProperty OutputBufferLength %lu\n", IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
);
474 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(STORAGE_ADAPTER_DESCRIPTOR
))
479 DescriptorHeader
= (PSTORAGE_DESCRIPTOR_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
480 ASSERT(IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
>= sizeof(STORAGE_DESCRIPTOR_HEADER
));
483 // return required size
485 DescriptorHeader
->Version
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
486 DescriptorHeader
->Size
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
488 Irp
->IoStatus
.Information
= sizeof(STORAGE_DESCRIPTOR_HEADER
);
489 return STATUS_SUCCESS
;
493 // get adapter descriptor, information is returned in the same buffer
495 AdapterDescriptor
= (PSTORAGE_ADAPTER_DESCRIPTOR
)Irp
->AssociatedIrp
.SystemBuffer
;
498 // fill out descriptor
500 AdapterDescriptor
->Version
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
501 AdapterDescriptor
->Size
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
502 AdapterDescriptor
->MaximumTransferLength
= MAXULONG
; //FIXME compute some sane value
503 AdapterDescriptor
->MaximumPhysicalPages
= 25; //FIXME compute some sane value
504 AdapterDescriptor
->AlignmentMask
= 0;
505 AdapterDescriptor
->AdapterUsesPio
= FALSE
;
506 AdapterDescriptor
->AdapterScansDown
= FALSE
;
507 AdapterDescriptor
->CommandQueueing
= FALSE
;
508 AdapterDescriptor
->AcceleratedTransfer
= FALSE
;
509 AdapterDescriptor
->BusType
= BusTypeUsb
;
510 AdapterDescriptor
->BusMajorVersion
= 0x2; //FIXME verify
511 AdapterDescriptor
->BusMinorVersion
= 0x00; //FIXME
514 // store returned length
516 Irp
->IoStatus
.Information
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
521 return STATUS_SUCCESS
;
526 USBSTOR_HandleDeviceControl(
527 IN PDEVICE_OBJECT DeviceObject
,
530 PIO_STACK_LOCATION IoStack
;
532 PPDO_DEVICE_EXTENSION PDODeviceExtension
;
533 PSCSI_ADAPTER_BUS_INFO BusInfo
;
534 PSCSI_INQUIRY_DATA InquiryData
;
535 PINQUIRYDATA ScsiInquiryData
;
536 PUFI_INQUIRY_RESPONSE UFIInquiryResponse
;
539 // get current stack location
541 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
543 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_STORAGE_QUERY_PROPERTY
)
548 Status
= USBSTOR_HandleQueryProperty(DeviceObject
, Irp
);
550 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_PASS_THROUGH
)
553 // query scsi pass through
555 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH NOT implemented\n");
556 Status
= STATUS_NOT_SUPPORTED
;
558 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_PASS_THROUGH_DIRECT
)
561 // query scsi pass through direct
563 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH_DIRECT NOT implemented\n");
564 Status
= STATUS_NOT_SUPPORTED
;
566 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER
)
569 // query serial number
571 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER NOT implemented\n");
572 Status
= STATUS_NOT_SUPPORTED
;
574 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_CAPABILITIES
)
576 PIO_SCSI_CAPABILITIES Capabilities
;
578 /* Legacy port capability query */
579 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
== sizeof(PVOID
))
581 Capabilities
= *((PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
) = ExAllocatePool(NonPagedPool
, sizeof(IO_SCSI_CAPABILITIES
));
582 Irp
->IoStatus
.Information
= sizeof(PVOID
);
586 Capabilities
= Irp
->AssociatedIrp
.SystemBuffer
;
587 Irp
->IoStatus
.Information
= sizeof(IO_SCSI_CAPABILITIES
);
592 Capabilities
->MaximumTransferLength
= MAXULONG
;
593 Capabilities
->MaximumPhysicalPages
= 25;
594 Capabilities
->SupportedAsynchronousEvents
= 0;
595 Capabilities
->AlignmentMask
= 0;
596 Capabilities
->TaggedQueuing
= FALSE
;
597 Capabilities
->AdapterScansDown
= FALSE
;
598 Capabilities
->AdapterUsesPio
= FALSE
;
599 Status
= STATUS_SUCCESS
;
603 Status
= STATUS_INSUFFICIENT_RESOURCES
;
606 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_INQUIRY_DATA
)
609 // get device extension
611 PDODeviceExtension
= (PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
612 ASSERT(PDODeviceExtension
);
613 ASSERT(PDODeviceExtension
->Common
.IsFDO
== FALSE
);
618 BusInfo
= Irp
->AssociatedIrp
.SystemBuffer
;
619 InquiryData
= (PSCSI_INQUIRY_DATA
)(BusInfo
+ 1);
620 ScsiInquiryData
= (PINQUIRYDATA
)InquiryData
->InquiryData
;
626 UFIInquiryResponse
= (PUFI_INQUIRY_RESPONSE
)PDODeviceExtension
->InquiryData
;
627 ASSERT(UFIInquiryResponse
);
630 BusInfo
->NumberOfBuses
= 1;
631 BusInfo
->BusData
[0].NumberOfLogicalUnits
= 1; //FIXME
632 BusInfo
->BusData
[0].InitiatorBusId
= 0;
633 BusInfo
->BusData
[0].InquiryDataOffset
= sizeof(SCSI_ADAPTER_BUS_INFO
);
635 InquiryData
->PathId
= 0;
636 InquiryData
->TargetId
= 0;
637 InquiryData
->Lun
= PDODeviceExtension
->LUN
& MAX_LUN
;
638 InquiryData
->DeviceClaimed
= PDODeviceExtension
->Claimed
;
639 InquiryData
->InquiryDataLength
= sizeof(INQUIRYDATA
);
640 InquiryData
->NextInquiryDataOffset
= 0;
642 RtlZeroMemory(ScsiInquiryData
, sizeof(INQUIRYDATA
));
643 ScsiInquiryData
->DeviceType
= UFIInquiryResponse
->DeviceType
;
644 ScsiInquiryData
->DeviceTypeQualifier
= (UFIInquiryResponse
->RMB
& 0x7F);
646 /* Hack for IoReadPartitionTable call in disk.sys */
647 ScsiInquiryData
->RemovableMedia
= ((ScsiInquiryData
->DeviceType
!= DIRECT_ACCESS_DEVICE
) ? ((UFIInquiryResponse
->RMB
& 0x80) ? 1 : 0) : 0);
649 ScsiInquiryData
->Versions
= 0x04;
650 ScsiInquiryData
->ResponseDataFormat
= 0x02;
651 ScsiInquiryData
->AdditionalLength
= 31;
652 ScsiInquiryData
->SoftReset
= 0;
653 ScsiInquiryData
->CommandQueue
= 0;
654 ScsiInquiryData
->LinkedCommands
= 0;
655 ScsiInquiryData
->RelativeAddressing
= 0;
657 RtlCopyMemory(&ScsiInquiryData
->VendorId
, UFIInquiryResponse
->Vendor
, USBSTOR_GetFieldLength(UFIInquiryResponse
->Vendor
, 8));
658 RtlCopyMemory(&ScsiInquiryData
->ProductId
, UFIInquiryResponse
->Product
, USBSTOR_GetFieldLength(UFIInquiryResponse
->Product
, 16));
660 Irp
->IoStatus
.Information
= sizeof(SCSI_ADAPTER_BUS_INFO
) + sizeof(SCSI_INQUIRY_DATA
) + sizeof(INQUIRYDATA
) - 1;
661 Status
= STATUS_SUCCESS
;
663 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_ADDRESS
)
665 PSCSI_ADDRESS Address
= Irp
->AssociatedIrp
.SystemBuffer
;
667 Address
->Length
= sizeof(SCSI_ADDRESS
);
668 Address
->PortNumber
= 0;
670 Address
->TargetId
= 0;
671 Address
->Lun
= (((PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->LUN
& MAX_LUN
);
672 Irp
->IoStatus
.Information
= sizeof(SCSI_ADDRESS
);
674 Status
= STATUS_SUCCESS
;
681 DPRINT("USBSTOR_HandleDeviceControl IoControl %x not supported\n", IoStack
->Parameters
.DeviceIoControl
.IoControlCode
);
682 Status
= STATUS_NOT_SUPPORTED
;