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_FLUSH
:
172 case SRB_FUNCTION_FLUSH_QUEUE
:
174 DPRINT1("SRB_FUNCTION_FLUSH / SRB_FUNCTION_FLUSH_QUEUE\n");
177 // flush all requests
179 USBSTOR_QueueFlushIrps(PDODeviceExtension
->LowerDeviceObject
);
182 // set status success
184 Request
->SrbStatus
= SRB_STATUS_SUCCESS
;
185 Status
= STATUS_SUCCESS
;
193 Status
= STATUS_NOT_SUPPORTED
;
194 Request
->SrbStatus
= SRB_STATUS_ERROR
;
201 Irp
->IoStatus
.Status
= Status
;
202 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
207 USBSTOR_GetFieldLength(
212 ULONG LastCharacterPosition
= 0;
215 // scan the field and return last positon which contains a valid character
217 for(Index
= 0; Index
< MaxLength
; Index
++)
219 if (Name
[Index
] != ' ')
222 // trim white spaces from field
224 LastCharacterPosition
= Index
;
229 // convert from zero based index to length
231 return LastCharacterPosition
+ 1;
235 USBSTOR_HandleQueryProperty(
236 IN PDEVICE_OBJECT DeviceObject
,
239 PIO_STACK_LOCATION IoStack
;
240 PSTORAGE_PROPERTY_QUERY PropertyQuery
;
241 PSTORAGE_DESCRIPTOR_HEADER DescriptorHeader
;
242 PSTORAGE_ADAPTER_DESCRIPTOR AdapterDescriptor
;
243 ULONG FieldLengthVendor
, FieldLengthProduct
, FieldLengthRevision
, TotalLength
, FieldLengthSerialNumber
;
244 PPDO_DEVICE_EXTENSION PDODeviceExtension
;
245 PUFI_INQUIRY_RESPONSE InquiryData
;
246 PSTORAGE_DEVICE_DESCRIPTOR DeviceDescriptor
;
248 PFDO_DEVICE_EXTENSION FDODeviceExtension
;
249 UNICODE_STRING SerialNumber
;
250 ANSI_STRING AnsiString
;
253 DPRINT1("USBSTOR_HandleQueryProperty\n");
256 // get current stack location
258 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
263 ASSERT(IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
>= sizeof(STORAGE_PROPERTY_QUERY
));
264 ASSERT(Irp
->AssociatedIrp
.SystemBuffer
);
267 // get property query
269 PropertyQuery
= (PSTORAGE_PROPERTY_QUERY
)Irp
->AssociatedIrp
.SystemBuffer
;
272 // check property type
274 if (PropertyQuery
->PropertyId
!= StorageDeviceProperty
&&
275 PropertyQuery
->PropertyId
!= StorageAdapterProperty
)
278 // only device property / adapter property are supported
280 return STATUS_INVALID_PARAMETER_1
;
286 if (PropertyQuery
->QueryType
== PropertyExistsQuery
)
289 // device property / adapter property is supported
291 return STATUS_SUCCESS
;
294 if (PropertyQuery
->QueryType
!= PropertyStandardQuery
)
297 // only standard query and exists query are supported
299 return STATUS_INVALID_PARAMETER_2
;
303 // check if it is a device property
305 if (PropertyQuery
->PropertyId
== StorageDeviceProperty
)
307 DPRINT1("USBSTOR_HandleQueryProperty StorageDeviceProperty OutputBufferLength %lu\n", IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
);
310 // get device extension
312 PDODeviceExtension
= (PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
313 ASSERT(PDODeviceExtension
);
314 ASSERT(PDODeviceExtension
->Common
.IsFDO
== FALSE
);
317 // get device extension
319 FDODeviceExtension
= (PFDO_DEVICE_EXTENSION
)PDODeviceExtension
->LowerDeviceObject
->DeviceExtension
;
320 ASSERT(FDODeviceExtension
);
321 ASSERT(FDODeviceExtension
->Common
.IsFDO
);
326 InquiryData
= (PUFI_INQUIRY_RESPONSE
)PDODeviceExtension
->InquiryData
;
330 // compute extra parameters length
332 FieldLengthVendor
= USBSTOR_GetFieldLength(InquiryData
->Vendor
, 8);
333 FieldLengthProduct
= USBSTOR_GetFieldLength(InquiryData
->Product
, 16);
334 FieldLengthRevision
= USBSTOR_GetFieldLength(InquiryData
->Revision
, 4);
337 // is there a serial number
339 if (FDODeviceExtension
->SerialNumber
)
344 FieldLengthSerialNumber
= wcslen(FDODeviceExtension
->SerialNumber
->bString
);
351 FieldLengthSerialNumber
= 0;
355 // total length required is sizeof(STORAGE_DEVICE_DESCRIPTOR) + FieldLength + 4 extra null bytes - 1
356 // -1 due STORAGE_DEVICE_DESCRIPTOR contains one byte length of parameter data
358 TotalLength
= sizeof(STORAGE_DEVICE_DESCRIPTOR
) + FieldLengthVendor
+ FieldLengthProduct
+ FieldLengthRevision
+ FieldLengthSerialNumber
+ 3;
361 // check if output buffer is long enough
363 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< TotalLength
)
368 DescriptorHeader
= (PSTORAGE_DESCRIPTOR_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
369 ASSERT(IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
>= sizeof(STORAGE_DESCRIPTOR_HEADER
));
372 // return required size
374 DescriptorHeader
->Version
= TotalLength
;
375 DescriptorHeader
->Size
= TotalLength
;
377 Irp
->IoStatus
.Information
= sizeof(STORAGE_DESCRIPTOR_HEADER
);
378 return STATUS_SUCCESS
;
382 // get device descriptor
384 DeviceDescriptor
= (PSTORAGE_DEVICE_DESCRIPTOR
)Irp
->AssociatedIrp
.SystemBuffer
;
387 // initialize device descriptor
389 DeviceDescriptor
->Version
= TotalLength
;
390 DeviceDescriptor
->Size
= TotalLength
;
391 DeviceDescriptor
->DeviceType
= InquiryData
->DeviceType
;
392 DeviceDescriptor
->DeviceTypeModifier
= (InquiryData
->RMB
& 0x7F);
393 DeviceDescriptor
->RemovableMedia
= TRUE
;
394 DeviceDescriptor
->CommandQueueing
= FALSE
;
395 DeviceDescriptor
->BusType
= BusTypeUsb
;
396 DeviceDescriptor
->VendorIdOffset
= sizeof(STORAGE_DEVICE_DESCRIPTOR
) - sizeof(UCHAR
);
397 DeviceDescriptor
->ProductIdOffset
= DeviceDescriptor
->VendorIdOffset
+ FieldLengthVendor
+ 1;
398 DeviceDescriptor
->ProductRevisionOffset
= DeviceDescriptor
->ProductIdOffset
+ FieldLengthProduct
+ 1;
399 DeviceDescriptor
->SerialNumberOffset
= (FieldLengthSerialNumber
> 0 ? DeviceDescriptor
->ProductRevisionOffset
+ FieldLengthRevision
+ 1 : 0);
400 DeviceDescriptor
->RawPropertiesLength
= FieldLengthVendor
+ FieldLengthProduct
+ FieldLengthRevision
+ FieldLengthSerialNumber
+ 3 + (FieldLengthSerialNumber
> 0 ? + 1 : 0);
405 Buffer
= (PUCHAR
)((ULONG_PTR
)DeviceDescriptor
+ sizeof(STORAGE_DEVICE_DESCRIPTOR
) - sizeof(UCHAR
));
410 RtlCopyMemory(Buffer
, InquiryData
->Vendor
, FieldLengthVendor
);
411 Buffer
[FieldLengthVendor
] = '\0';
412 Buffer
+= FieldLengthVendor
+ 1;
417 RtlCopyMemory(Buffer
, InquiryData
->Product
, FieldLengthProduct
);
418 Buffer
[FieldLengthProduct
] = '\0';
419 Buffer
+= FieldLengthProduct
+ 1;
424 RtlCopyMemory(Buffer
, InquiryData
->Revision
, FieldLengthRevision
);
425 Buffer
[FieldLengthRevision
] = '\0';
426 Buffer
+= FieldLengthRevision
+ 1;
429 // copy serial number
431 if (FieldLengthSerialNumber
)
434 // init unicode string
436 RtlInitUnicodeString(&SerialNumber
, FDODeviceExtension
->SerialNumber
->bString
);
441 AnsiString
.Buffer
= (PCHAR
)Buffer
;
442 AnsiString
.Length
= 0;
443 AnsiString
.MaximumLength
= FieldLengthSerialNumber
* sizeof(WCHAR
);
446 // convert to ansi code
448 Status
= RtlUnicodeStringToAnsiString(&AnsiString
, &SerialNumber
, FALSE
);
449 ASSERT(Status
== STATUS_SUCCESS
);
453 DPRINT("Vendor %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->VendorIdOffset
));
454 DPRINT("Product %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->ProductIdOffset
));
455 DPRINT("Revision %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->ProductRevisionOffset
));
456 DPRINT("Serial %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->SerialNumberOffset
));
461 Irp
->IoStatus
.Information
= TotalLength
;
462 return STATUS_SUCCESS
;
467 // adapter property query request
469 DPRINT1("USBSTOR_HandleQueryProperty StorageAdapterProperty OutputBufferLength %lu\n", IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
);
471 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(STORAGE_ADAPTER_DESCRIPTOR
))
476 DescriptorHeader
= (PSTORAGE_DESCRIPTOR_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
477 ASSERT(IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
>= sizeof(STORAGE_DESCRIPTOR_HEADER
));
480 // return required size
482 DescriptorHeader
->Version
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
483 DescriptorHeader
->Size
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
485 Irp
->IoStatus
.Information
= sizeof(STORAGE_DESCRIPTOR_HEADER
);
486 return STATUS_SUCCESS
;
490 // get adapter descriptor, information is returned in the same buffer
492 AdapterDescriptor
= (PSTORAGE_ADAPTER_DESCRIPTOR
)Irp
->AssociatedIrp
.SystemBuffer
;
495 // fill out descriptor
497 AdapterDescriptor
->Version
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
498 AdapterDescriptor
->Size
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
499 AdapterDescriptor
->MaximumTransferLength
= MAXULONG
; //FIXME compute some sane value
500 AdapterDescriptor
->MaximumPhysicalPages
= 25; //FIXME compute some sane value
501 AdapterDescriptor
->AlignmentMask
= 0;
502 AdapterDescriptor
->AdapterUsesPio
= FALSE
;
503 AdapterDescriptor
->AdapterScansDown
= FALSE
;
504 AdapterDescriptor
->CommandQueueing
= FALSE
;
505 AdapterDescriptor
->AcceleratedTransfer
= FALSE
;
506 AdapterDescriptor
->BusType
= BusTypeUsb
;
507 AdapterDescriptor
->BusMajorVersion
= 0x2; //FIXME verify
508 AdapterDescriptor
->BusMinorVersion
= 0x00; //FIXME
511 // store returned length
513 Irp
->IoStatus
.Information
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
518 return STATUS_SUCCESS
;
523 USBSTOR_HandleDeviceControl(
524 IN PDEVICE_OBJECT DeviceObject
,
527 PIO_STACK_LOCATION IoStack
;
529 PPDO_DEVICE_EXTENSION PDODeviceExtension
;
530 PSCSI_ADAPTER_BUS_INFO BusInfo
;
531 PSCSI_INQUIRY_DATA InquiryData
;
532 PINQUIRYDATA ScsiInquiryData
;
533 PUFI_INQUIRY_RESPONSE UFIInquiryResponse
;
536 // get current stack location
538 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
540 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_STORAGE_QUERY_PROPERTY
)
545 Status
= USBSTOR_HandleQueryProperty(DeviceObject
, Irp
);
547 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_PASS_THROUGH
)
550 // query scsi pass through
552 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH NOT implemented\n");
553 Status
= STATUS_NOT_SUPPORTED
;
555 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_PASS_THROUGH_DIRECT
)
558 // query scsi pass through direct
560 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH_DIRECT NOT implemented\n");
561 Status
= STATUS_NOT_SUPPORTED
;
563 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER
)
566 // query serial number
568 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER NOT implemented\n");
569 Status
= STATUS_NOT_SUPPORTED
;
571 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_CAPABILITIES
)
573 PIO_SCSI_CAPABILITIES Capabilities
;
575 /* Legacy port capability query */
576 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
== sizeof(PVOID
))
578 Capabilities
= *((PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
) = ExAllocatePool(NonPagedPool
, sizeof(IO_SCSI_CAPABILITIES
));
579 Irp
->IoStatus
.Information
= sizeof(PVOID
);
583 Capabilities
= Irp
->AssociatedIrp
.SystemBuffer
;
584 Irp
->IoStatus
.Information
= sizeof(IO_SCSI_CAPABILITIES
);
589 Capabilities
->MaximumTransferLength
= MAXULONG
;
590 Capabilities
->MaximumPhysicalPages
= 25;
591 Capabilities
->SupportedAsynchronousEvents
= 0;
592 Capabilities
->AlignmentMask
= 0;
593 Capabilities
->TaggedQueuing
= FALSE
;
594 Capabilities
->AdapterScansDown
= FALSE
;
595 Capabilities
->AdapterUsesPio
= FALSE
;
596 Status
= STATUS_SUCCESS
;
600 Status
= STATUS_INSUFFICIENT_RESOURCES
;
603 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_INQUIRY_DATA
)
606 // get device extension
608 PDODeviceExtension
= (PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
609 ASSERT(PDODeviceExtension
);
610 ASSERT(PDODeviceExtension
->Common
.IsFDO
== FALSE
);
615 BusInfo
= Irp
->AssociatedIrp
.SystemBuffer
;
616 InquiryData
= (PSCSI_INQUIRY_DATA
)(BusInfo
+ 1);
617 ScsiInquiryData
= (PINQUIRYDATA
)InquiryData
->InquiryData
;
623 UFIInquiryResponse
= (PUFI_INQUIRY_RESPONSE
)PDODeviceExtension
->InquiryData
;
624 ASSERT(UFIInquiryResponse
);
627 BusInfo
->NumberOfBuses
= 1;
628 BusInfo
->BusData
[0].NumberOfLogicalUnits
= 1; //FIXME
629 BusInfo
->BusData
[0].InitiatorBusId
= 0;
630 BusInfo
->BusData
[0].InquiryDataOffset
= sizeof(SCSI_ADAPTER_BUS_INFO
);
632 InquiryData
->PathId
= 0;
633 InquiryData
->TargetId
= 0;
634 InquiryData
->Lun
= PDODeviceExtension
->LUN
& MAX_LUN
;
635 InquiryData
->DeviceClaimed
= PDODeviceExtension
->Claimed
;
636 InquiryData
->InquiryDataLength
= sizeof(INQUIRYDATA
);
637 InquiryData
->NextInquiryDataOffset
= 0;
639 RtlZeroMemory(ScsiInquiryData
, sizeof(INQUIRYDATA
));
640 ScsiInquiryData
->DeviceType
= UFIInquiryResponse
->DeviceType
;
641 ScsiInquiryData
->DeviceTypeQualifier
= (UFIInquiryResponse
->RMB
& 0x7F);
642 ScsiInquiryData
->RemovableMedia
= FALSE
; //HACK for IoReadPartitionTable
643 ScsiInquiryData
->Versions
= 0x04;
644 ScsiInquiryData
->ResponseDataFormat
= 0x02;
645 ScsiInquiryData
->AdditionalLength
= 31;
646 ScsiInquiryData
->SoftReset
= 0;
647 ScsiInquiryData
->CommandQueue
= 0;
648 ScsiInquiryData
->LinkedCommands
= 0;
649 ScsiInquiryData
->RelativeAddressing
= 0;
651 RtlCopyMemory(&ScsiInquiryData
->VendorId
, UFIInquiryResponse
->Vendor
, USBSTOR_GetFieldLength(UFIInquiryResponse
->Vendor
, 8));
652 RtlCopyMemory(&ScsiInquiryData
->ProductId
, UFIInquiryResponse
->Product
, USBSTOR_GetFieldLength(UFIInquiryResponse
->Product
, 16));
654 Irp
->IoStatus
.Information
= sizeof(SCSI_ADAPTER_BUS_INFO
) + sizeof(SCSI_INQUIRY_DATA
) + sizeof(INQUIRYDATA
) - 1;
655 Status
= STATUS_SUCCESS
;
657 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_ADDRESS
)
659 PSCSI_ADDRESS Address
= Irp
->AssociatedIrp
.SystemBuffer
;
661 Address
->Length
= sizeof(SCSI_ADDRESS
);
662 Address
->PortNumber
= 0;
664 Address
->TargetId
= 0;
665 Address
->Lun
= (((PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->LUN
& MAX_LUN
);
666 Irp
->IoStatus
.Information
= sizeof(SCSI_ADDRESS
);
668 Status
= STATUS_SUCCESS
;
675 DPRINT("USBSTOR_HandleDeviceControl IoControl %x not supported\n", IoStack
->Parameters
.DeviceIoControl
.IoControlCode
);
676 Status
= STATUS_NOT_SUPPORTED
;