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");
178 // wait for pending requests to finish
180 USBSTOR_QueueWaitForPendingRequests(PDODeviceExtension
->LowerDeviceObject
);
183 // set status success
185 Request
->SrbStatus
= SRB_STATUS_SUCCESS
;
186 Status
= STATUS_SUCCESS
;
194 Status
= STATUS_NOT_SUPPORTED
;
195 Request
->SrbStatus
= SRB_STATUS_ERROR
;
202 Irp
->IoStatus
.Status
= Status
;
203 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
208 USBSTOR_GetFieldLength(
213 ULONG LastCharacterPosition
= 0;
216 // scan the field and return last positon which contains a valid character
218 for(Index
= 0; Index
< MaxLength
; Index
++)
220 if (Name
[Index
] != ' ')
223 // trim white spaces from field
225 LastCharacterPosition
= Index
;
230 // convert from zero based index to length
232 return LastCharacterPosition
+ 1;
236 USBSTOR_HandleQueryProperty(
237 IN PDEVICE_OBJECT DeviceObject
,
240 PIO_STACK_LOCATION IoStack
;
241 PSTORAGE_PROPERTY_QUERY PropertyQuery
;
242 PSTORAGE_DESCRIPTOR_HEADER DescriptorHeader
;
243 PSTORAGE_ADAPTER_DESCRIPTOR AdapterDescriptor
;
244 ULONG FieldLengthVendor
, FieldLengthProduct
, FieldLengthRevision
, TotalLength
, FieldLengthSerialNumber
;
245 PPDO_DEVICE_EXTENSION PDODeviceExtension
;
246 PUFI_INQUIRY_RESPONSE InquiryData
;
247 PSTORAGE_DEVICE_DESCRIPTOR DeviceDescriptor
;
249 PFDO_DEVICE_EXTENSION FDODeviceExtension
;
250 UNICODE_STRING SerialNumber
;
251 ANSI_STRING AnsiString
;
254 DPRINT1("USBSTOR_HandleQueryProperty\n");
257 // get current stack location
259 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
264 ASSERT(IoStack
->Parameters
.DeviceIoControl
.InputBufferLength
>= sizeof(STORAGE_PROPERTY_QUERY
));
265 ASSERT(Irp
->AssociatedIrp
.SystemBuffer
);
268 // get property query
270 PropertyQuery
= (PSTORAGE_PROPERTY_QUERY
)Irp
->AssociatedIrp
.SystemBuffer
;
273 // check property type
275 if (PropertyQuery
->PropertyId
!= StorageDeviceProperty
&&
276 PropertyQuery
->PropertyId
!= StorageAdapterProperty
)
279 // only device property / adapter property are supported
281 return STATUS_INVALID_PARAMETER_1
;
287 if (PropertyQuery
->QueryType
== PropertyExistsQuery
)
290 // device property / adapter property is supported
292 return STATUS_SUCCESS
;
295 if (PropertyQuery
->QueryType
!= PropertyStandardQuery
)
298 // only standard query and exists query are supported
300 return STATUS_INVALID_PARAMETER_2
;
304 // check if it is a device property
306 if (PropertyQuery
->PropertyId
== StorageDeviceProperty
)
308 DPRINT1("USBSTOR_HandleQueryProperty StorageDeviceProperty OutputBufferLength %lu\n", IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
);
311 // get device extension
313 PDODeviceExtension
= (PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
314 ASSERT(PDODeviceExtension
);
315 ASSERT(PDODeviceExtension
->Common
.IsFDO
== FALSE
);
318 // get device extension
320 FDODeviceExtension
= (PFDO_DEVICE_EXTENSION
)PDODeviceExtension
->LowerDeviceObject
->DeviceExtension
;
321 ASSERT(FDODeviceExtension
);
322 ASSERT(FDODeviceExtension
->Common
.IsFDO
);
327 InquiryData
= (PUFI_INQUIRY_RESPONSE
)PDODeviceExtension
->InquiryData
;
331 // compute extra parameters length
333 FieldLengthVendor
= USBSTOR_GetFieldLength(InquiryData
->Vendor
, 8);
334 FieldLengthProduct
= USBSTOR_GetFieldLength(InquiryData
->Product
, 16);
335 FieldLengthRevision
= USBSTOR_GetFieldLength(InquiryData
->Revision
, 4);
338 // is there a serial number
340 if (FDODeviceExtension
->SerialNumber
)
345 FieldLengthSerialNumber
= wcslen(FDODeviceExtension
->SerialNumber
->bString
);
352 FieldLengthSerialNumber
= 0;
356 // total length required is sizeof(STORAGE_DEVICE_DESCRIPTOR) + FieldLength + 4 extra null bytes - 1
357 // -1 due STORAGE_DEVICE_DESCRIPTOR contains one byte length of parameter data
359 TotalLength
= sizeof(STORAGE_DEVICE_DESCRIPTOR
) + FieldLengthVendor
+ FieldLengthProduct
+ FieldLengthRevision
+ FieldLengthSerialNumber
+ 3;
362 // check if output buffer is long enough
364 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< TotalLength
)
369 DescriptorHeader
= (PSTORAGE_DESCRIPTOR_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
370 ASSERT(IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
>= sizeof(STORAGE_DESCRIPTOR_HEADER
));
373 // return required size
375 DescriptorHeader
->Version
= TotalLength
;
376 DescriptorHeader
->Size
= TotalLength
;
378 Irp
->IoStatus
.Information
= sizeof(STORAGE_DESCRIPTOR_HEADER
);
379 return STATUS_SUCCESS
;
383 // get device descriptor
385 DeviceDescriptor
= (PSTORAGE_DEVICE_DESCRIPTOR
)Irp
->AssociatedIrp
.SystemBuffer
;
388 // initialize device descriptor
390 DeviceDescriptor
->Version
= TotalLength
;
391 DeviceDescriptor
->Size
= TotalLength
;
392 DeviceDescriptor
->DeviceType
= InquiryData
->DeviceType
;
393 DeviceDescriptor
->DeviceTypeModifier
= (InquiryData
->RMB
& 0x7F);
394 DeviceDescriptor
->RemovableMedia
= (InquiryData
->RMB
& 0x80) ? TRUE
: FALSE
;
395 DeviceDescriptor
->CommandQueueing
= FALSE
;
396 DeviceDescriptor
->BusType
= BusTypeUsb
;
397 DeviceDescriptor
->VendorIdOffset
= sizeof(STORAGE_DEVICE_DESCRIPTOR
) - sizeof(UCHAR
);
398 DeviceDescriptor
->ProductIdOffset
= DeviceDescriptor
->VendorIdOffset
+ FieldLengthVendor
+ 1;
399 DeviceDescriptor
->ProductRevisionOffset
= DeviceDescriptor
->ProductIdOffset
+ FieldLengthProduct
+ 1;
400 DeviceDescriptor
->SerialNumberOffset
= (FieldLengthSerialNumber
> 0 ? DeviceDescriptor
->ProductRevisionOffset
+ FieldLengthRevision
+ 1 : 0);
401 DeviceDescriptor
->RawPropertiesLength
= FieldLengthVendor
+ FieldLengthProduct
+ FieldLengthRevision
+ FieldLengthSerialNumber
+ 3 + (FieldLengthSerialNumber
> 0 ? + 1 : 0);
406 Buffer
= (PUCHAR
)((ULONG_PTR
)DeviceDescriptor
+ sizeof(STORAGE_DEVICE_DESCRIPTOR
) - sizeof(UCHAR
));
411 RtlCopyMemory(Buffer
, InquiryData
->Vendor
, FieldLengthVendor
);
412 Buffer
[FieldLengthVendor
] = '\0';
413 Buffer
+= FieldLengthVendor
+ 1;
418 RtlCopyMemory(Buffer
, InquiryData
->Product
, FieldLengthProduct
);
419 Buffer
[FieldLengthProduct
] = '\0';
420 Buffer
+= FieldLengthProduct
+ 1;
425 RtlCopyMemory(Buffer
, InquiryData
->Revision
, FieldLengthRevision
);
426 Buffer
[FieldLengthRevision
] = '\0';
427 Buffer
+= FieldLengthRevision
+ 1;
430 // copy serial number
432 if (FieldLengthSerialNumber
)
435 // init unicode string
437 RtlInitUnicodeString(&SerialNumber
, FDODeviceExtension
->SerialNumber
->bString
);
442 AnsiString
.Buffer
= (PCHAR
)Buffer
;
443 AnsiString
.Length
= 0;
444 AnsiString
.MaximumLength
= FieldLengthSerialNumber
* sizeof(WCHAR
);
447 // convert to ansi code
449 Status
= RtlUnicodeStringToAnsiString(&AnsiString
, &SerialNumber
, FALSE
);
450 ASSERT(Status
== STATUS_SUCCESS
);
454 DPRINT("Vendor %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->VendorIdOffset
));
455 DPRINT("Product %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->ProductIdOffset
));
456 DPRINT("Revision %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->ProductRevisionOffset
));
457 DPRINT("Serial %s\n", (LPCSTR
)((ULONG_PTR
)DeviceDescriptor
+ DeviceDescriptor
->SerialNumberOffset
));
462 Irp
->IoStatus
.Information
= TotalLength
;
463 return STATUS_SUCCESS
;
468 // adapter property query request
470 DPRINT1("USBSTOR_HandleQueryProperty StorageAdapterProperty OutputBufferLength %lu\n", IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
);
472 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(STORAGE_ADAPTER_DESCRIPTOR
))
477 DescriptorHeader
= (PSTORAGE_DESCRIPTOR_HEADER
)Irp
->AssociatedIrp
.SystemBuffer
;
478 ASSERT(IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
>= sizeof(STORAGE_DESCRIPTOR_HEADER
));
481 // return required size
483 DescriptorHeader
->Version
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
484 DescriptorHeader
->Size
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
486 Irp
->IoStatus
.Information
= sizeof(STORAGE_DESCRIPTOR_HEADER
);
487 return STATUS_SUCCESS
;
491 // get adapter descriptor, information is returned in the same buffer
493 AdapterDescriptor
= (PSTORAGE_ADAPTER_DESCRIPTOR
)Irp
->AssociatedIrp
.SystemBuffer
;
496 // fill out descriptor
498 AdapterDescriptor
->Version
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
499 AdapterDescriptor
->Size
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
500 AdapterDescriptor
->MaximumTransferLength
= MAXULONG
; //FIXME compute some sane value
501 AdapterDescriptor
->MaximumPhysicalPages
= 25; //FIXME compute some sane value
502 AdapterDescriptor
->AlignmentMask
= 0;
503 AdapterDescriptor
->AdapterUsesPio
= FALSE
;
504 AdapterDescriptor
->AdapterScansDown
= FALSE
;
505 AdapterDescriptor
->CommandQueueing
= FALSE
;
506 AdapterDescriptor
->AcceleratedTransfer
= FALSE
;
507 AdapterDescriptor
->BusType
= BusTypeUsb
;
508 AdapterDescriptor
->BusMajorVersion
= 0x2; //FIXME verify
509 AdapterDescriptor
->BusMinorVersion
= 0x00; //FIXME
512 // store returned length
514 Irp
->IoStatus
.Information
= sizeof(STORAGE_ADAPTER_DESCRIPTOR
);
519 return STATUS_SUCCESS
;
524 USBSTOR_HandleDeviceControl(
525 IN PDEVICE_OBJECT DeviceObject
,
528 PIO_STACK_LOCATION IoStack
;
530 PPDO_DEVICE_EXTENSION PDODeviceExtension
;
531 PSCSI_ADAPTER_BUS_INFO BusInfo
;
532 PSCSI_INQUIRY_DATA InquiryData
;
533 PINQUIRYDATA ScsiInquiryData
;
534 PUFI_INQUIRY_RESPONSE UFIInquiryResponse
;
537 // get current stack location
539 IoStack
= IoGetCurrentIrpStackLocation(Irp
);
541 if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_STORAGE_QUERY_PROPERTY
)
546 Status
= USBSTOR_HandleQueryProperty(DeviceObject
, Irp
);
548 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_PASS_THROUGH
)
551 // query scsi pass through
553 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH NOT implemented\n");
554 Status
= STATUS_NOT_SUPPORTED
;
556 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_PASS_THROUGH_DIRECT
)
559 // query scsi pass through direct
561 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH_DIRECT NOT implemented\n");
562 Status
= STATUS_NOT_SUPPORTED
;
564 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER
)
567 // query serial number
569 DPRINT1("USBSTOR_HandleDeviceControl IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER NOT implemented\n");
570 Status
= STATUS_NOT_SUPPORTED
;
572 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_CAPABILITIES
)
574 PIO_SCSI_CAPABILITIES Capabilities
;
576 /* Legacy port capability query */
577 if (IoStack
->Parameters
.DeviceIoControl
.OutputBufferLength
== sizeof(PVOID
))
579 Capabilities
= *((PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
) = ExAllocatePool(NonPagedPool
, sizeof(IO_SCSI_CAPABILITIES
));
580 Irp
->IoStatus
.Information
= sizeof(PVOID
);
584 Capabilities
= Irp
->AssociatedIrp
.SystemBuffer
;
585 Irp
->IoStatus
.Information
= sizeof(IO_SCSI_CAPABILITIES
);
590 Capabilities
->MaximumTransferLength
= MAXULONG
;
591 Capabilities
->MaximumPhysicalPages
= 25;
592 Capabilities
->SupportedAsynchronousEvents
= 0;
593 Capabilities
->AlignmentMask
= 0;
594 Capabilities
->TaggedQueuing
= FALSE
;
595 Capabilities
->AdapterScansDown
= FALSE
;
596 Capabilities
->AdapterUsesPio
= FALSE
;
597 Status
= STATUS_SUCCESS
;
601 Status
= STATUS_INSUFFICIENT_RESOURCES
;
604 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_INQUIRY_DATA
)
607 // get device extension
609 PDODeviceExtension
= (PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
610 ASSERT(PDODeviceExtension
);
611 ASSERT(PDODeviceExtension
->Common
.IsFDO
== FALSE
);
616 BusInfo
= Irp
->AssociatedIrp
.SystemBuffer
;
617 InquiryData
= (PSCSI_INQUIRY_DATA
)(BusInfo
+ 1);
618 ScsiInquiryData
= (PINQUIRYDATA
)InquiryData
->InquiryData
;
624 UFIInquiryResponse
= (PUFI_INQUIRY_RESPONSE
)PDODeviceExtension
->InquiryData
;
625 ASSERT(UFIInquiryResponse
);
628 BusInfo
->NumberOfBuses
= 1;
629 BusInfo
->BusData
[0].NumberOfLogicalUnits
= 1; //FIXME
630 BusInfo
->BusData
[0].InitiatorBusId
= 0;
631 BusInfo
->BusData
[0].InquiryDataOffset
= sizeof(SCSI_ADAPTER_BUS_INFO
);
633 InquiryData
->PathId
= 0;
634 InquiryData
->TargetId
= 0;
635 InquiryData
->Lun
= PDODeviceExtension
->LUN
& MAX_LUN
;
636 InquiryData
->DeviceClaimed
= PDODeviceExtension
->Claimed
;
637 InquiryData
->InquiryDataLength
= sizeof(INQUIRYDATA
);
638 InquiryData
->NextInquiryDataOffset
= 0;
640 RtlZeroMemory(ScsiInquiryData
, sizeof(INQUIRYDATA
));
641 ScsiInquiryData
->DeviceType
= UFIInquiryResponse
->DeviceType
;
642 ScsiInquiryData
->DeviceTypeQualifier
= (UFIInquiryResponse
->RMB
& 0x7F);
644 /* Hack for IoReadPartitionTable call in disk.sys */
645 ScsiInquiryData
->RemovableMedia
= ((ScsiInquiryData
->DeviceType
!= DIRECT_ACCESS_DEVICE
) ? ((UFIInquiryResponse
->RMB
& 0x80) ? 1 : 0) : 0);
647 ScsiInquiryData
->Versions
= 0x04;
648 ScsiInquiryData
->ResponseDataFormat
= 0x02;
649 ScsiInquiryData
->AdditionalLength
= 31;
650 ScsiInquiryData
->SoftReset
= 0;
651 ScsiInquiryData
->CommandQueue
= 0;
652 ScsiInquiryData
->LinkedCommands
= 0;
653 ScsiInquiryData
->RelativeAddressing
= 0;
655 RtlCopyMemory(&ScsiInquiryData
->VendorId
, UFIInquiryResponse
->Vendor
, USBSTOR_GetFieldLength(UFIInquiryResponse
->Vendor
, 8));
656 RtlCopyMemory(&ScsiInquiryData
->ProductId
, UFIInquiryResponse
->Product
, USBSTOR_GetFieldLength(UFIInquiryResponse
->Product
, 16));
658 Irp
->IoStatus
.Information
= sizeof(SCSI_ADAPTER_BUS_INFO
) + sizeof(SCSI_INQUIRY_DATA
) + sizeof(INQUIRYDATA
) - 1;
659 Status
= STATUS_SUCCESS
;
661 else if (IoStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_SCSI_GET_ADDRESS
)
663 PSCSI_ADDRESS Address
= Irp
->AssociatedIrp
.SystemBuffer
;
665 Address
->Length
= sizeof(SCSI_ADDRESS
);
666 Address
->PortNumber
= 0;
668 Address
->TargetId
= 0;
669 Address
->Lun
= (((PPDO_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
)->LUN
& MAX_LUN
);
670 Irp
->IoStatus
.Information
= sizeof(SCSI_ADDRESS
);
672 Status
= STATUS_SUCCESS
;
679 DPRINT("USBSTOR_HandleDeviceControl IoControl %x not supported\n", IoStack
->Parameters
.DeviceIoControl
.IoControlCode
);
680 Status
= STATUS_NOT_SUPPORTED
;