2011-05-05 00:39:00 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Universal Serial Bus Bulk Storage Driver
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: drivers/usb/usbstor/disk.c
|
|
|
|
* PURPOSE: USB block storage device driver.
|
|
|
|
* PROGRAMMERS:
|
|
|
|
* James Tabor
|
|
|
|
* Michael Martin (michael.martin@reactos.org)
|
|
|
|
* Johannes Anderwald (johannes.anderwald@reactos.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "usbstor.h"
|
|
|
|
|
2013-12-23 14:50:17 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-05-05 00:39:00 +00:00
|
|
|
NTSTATUS
|
|
|
|
USBSTOR_HandleInternalDeviceControl(
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN PIRP Irp)
|
|
|
|
{
|
|
|
|
PIO_STACK_LOCATION IoStack;
|
|
|
|
PSCSI_REQUEST_BLOCK Request;
|
|
|
|
PPDO_DEVICE_EXTENSION PDODeviceExtension;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
//
|
|
|
|
// get current stack location
|
|
|
|
//
|
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
|
|
|
|
//
|
|
|
|
// get request block
|
|
|
|
//
|
|
|
|
Request = (PSCSI_REQUEST_BLOCK)IoStack->Parameters.Others.Argument1;
|
|
|
|
|
2011-05-15 14:59:16 +00:00
|
|
|
//
|
|
|
|
// sanity check
|
|
|
|
//
|
|
|
|
ASSERT(Request);
|
|
|
|
|
2011-05-05 00:39:00 +00:00
|
|
|
//
|
|
|
|
// get device extension
|
|
|
|
//
|
|
|
|
PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
|
|
|
|
|
|
|
|
//
|
|
|
|
// sanity check
|
|
|
|
//
|
2011-05-15 14:59:16 +00:00
|
|
|
ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
|
2011-05-05 00:39:00 +00:00
|
|
|
|
|
|
|
switch(Request->Function)
|
|
|
|
{
|
|
|
|
case SRB_FUNCTION_EXECUTE_SCSI:
|
|
|
|
{
|
2011-05-15 12:47:54 +00:00
|
|
|
DPRINT("SRB_FUNCTION_EXECUTE_SCSI\n");
|
2011-05-14 18:09:00 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// check if request is valid
|
|
|
|
//
|
|
|
|
if (Request->SrbFlags & (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT))
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// data is transferred with this irp
|
|
|
|
//
|
2011-05-15 12:49:17 +00:00
|
|
|
if ((Request->SrbFlags & (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT)) == (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT) ||
|
2011-05-14 18:09:00 +00:00
|
|
|
Request->DataTransferLength == 0 ||
|
|
|
|
Irp->MdlAddress == NULL)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// invalid parameter
|
|
|
|
//
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// sense buffer request
|
|
|
|
//
|
|
|
|
if (Request->DataTransferLength ||
|
|
|
|
Request->DataBuffer ||
|
|
|
|
Irp->MdlAddress)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// invalid parameter
|
|
|
|
//
|
|
|
|
Status = STATUS_INVALID_PARAMETER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// add the request
|
|
|
|
//
|
2011-05-15 14:59:16 +00:00
|
|
|
if (!USBSTOR_QueueAddIrp(PDODeviceExtension->LowerDeviceObject, Irp))
|
2011-05-14 18:09:00 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// irp was not added to the queue
|
|
|
|
//
|
|
|
|
IoStartPacket(PDODeviceExtension->LowerDeviceObject, Irp, &Request->QueueSortKey, USBSTOR_CancelIo);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// irp pending
|
|
|
|
//
|
|
|
|
return STATUS_PENDING;
|
2011-05-05 00:39:00 +00:00
|
|
|
}
|
|
|
|
case SRB_FUNCTION_RELEASE_DEVICE:
|
|
|
|
{
|
|
|
|
DPRINT1("SRB_FUNCTION_RELEASE_DEVICE\n");
|
|
|
|
//
|
|
|
|
// sanity check
|
|
|
|
//
|
|
|
|
ASSERT(PDODeviceExtension->Claimed == TRUE);
|
|
|
|
|
|
|
|
//
|
|
|
|
// release claim
|
|
|
|
//
|
2011-06-03 05:43:41 +00:00
|
|
|
PDODeviceExtension->Claimed = FALSE;
|
2011-05-05 00:39:00 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SRB_FUNCTION_CLAIM_DEVICE:
|
|
|
|
{
|
|
|
|
DPRINT1("SRB_FUNCTION_CLAIM_DEVICE\n");
|
|
|
|
//
|
|
|
|
// check if the device has been claimed
|
|
|
|
//
|
|
|
|
if (PDODeviceExtension->Claimed)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// device has already been claimed
|
|
|
|
//
|
|
|
|
Status = STATUS_DEVICE_BUSY;
|
|
|
|
Request->SrbStatus = SRB_STATUS_BUSY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// claim device
|
|
|
|
//
|
|
|
|
PDODeviceExtension->Claimed = TRUE;
|
|
|
|
|
|
|
|
//
|
|
|
|
// output device object
|
|
|
|
//
|
|
|
|
Request->DataBuffer = DeviceObject;
|
|
|
|
|
|
|
|
//
|
|
|
|
// completed successfully
|
|
|
|
//
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SRB_FUNCTION_RELEASE_QUEUE:
|
|
|
|
{
|
2011-05-14 18:09:00 +00:00
|
|
|
DPRINT1("SRB_FUNCTION_RELEASE_QUEUE\n");
|
|
|
|
|
|
|
|
//
|
|
|
|
// release queue
|
|
|
|
//
|
2011-05-15 14:59:16 +00:00
|
|
|
USBSTOR_QueueRelease(PDODeviceExtension->LowerDeviceObject);
|
2011-05-14 18:09:00 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// set status success
|
|
|
|
//
|
|
|
|
Request->SrbStatus = SRB_STATUS_SUCCESS;
|
|
|
|
Status = STATUS_SUCCESS;
|
2011-05-05 00:39:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-05-14 18:09:00 +00:00
|
|
|
|
2012-01-30 04:18:59 +00:00
|
|
|
case SRB_FUNCTION_SHUTDOWN:
|
2011-05-05 00:39:00 +00:00
|
|
|
case SRB_FUNCTION_FLUSH:
|
2011-05-14 18:09:00 +00:00
|
|
|
case SRB_FUNCTION_FLUSH_QUEUE:
|
2011-05-05 00:39:00 +00:00
|
|
|
{
|
2012-01-30 04:18:59 +00:00
|
|
|
DPRINT1("SRB_FUNCTION_FLUSH / SRB_FUNCTION_FLUSH_QUEUE / SRB_FUNCTION_SHUTDOWN\n");
|
2011-05-14 18:09:00 +00:00
|
|
|
|
2012-10-25 17:17:25 +00:00
|
|
|
// HACK: don't flush pending requests
|
|
|
|
#if 0 // we really need a proper storage stack
|
2011-05-14 18:09:00 +00:00
|
|
|
//
|
2012-01-31 15:33:32 +00:00
|
|
|
// wait for pending requests to finish
|
2011-05-14 18:09:00 +00:00
|
|
|
//
|
2012-01-31 15:33:32 +00:00
|
|
|
USBSTOR_QueueWaitForPendingRequests(PDODeviceExtension->LowerDeviceObject);
|
2012-10-25 17:17:25 +00:00
|
|
|
#endif
|
2011-05-14 18:09:00 +00:00
|
|
|
//
|
|
|
|
// set status success
|
|
|
|
//
|
|
|
|
Request->SrbStatus = SRB_STATUS_SUCCESS;
|
|
|
|
Status = STATUS_SUCCESS;
|
2011-05-05 00:39:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// not supported
|
|
|
|
//
|
|
|
|
Status = STATUS_NOT_SUPPORTED;
|
|
|
|
Request->SrbStatus = SRB_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-11 03:49:15 +00:00
|
|
|
//
|
|
|
|
// complete request
|
|
|
|
//
|
|
|
|
Irp->IoStatus.Status = Status;
|
|
|
|
IoCompleteRequest(Irp, IO_NO_INCREMENT);
|
2011-05-05 00:39:00 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2011-05-06 13:29:27 +00:00
|
|
|
ULONG
|
|
|
|
USBSTOR_GetFieldLength(
|
|
|
|
IN PUCHAR Name,
|
|
|
|
IN ULONG MaxLength)
|
|
|
|
{
|
|
|
|
ULONG Index;
|
|
|
|
ULONG LastCharacterPosition = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// scan the field and return last positon which contains a valid character
|
|
|
|
//
|
|
|
|
for(Index = 0; Index < MaxLength; Index++)
|
|
|
|
{
|
|
|
|
if (Name[Index] != ' ')
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// trim white spaces from field
|
|
|
|
//
|
|
|
|
LastCharacterPosition = Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// convert from zero based index to length
|
|
|
|
//
|
|
|
|
return LastCharacterPosition + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
USBSTOR_HandleQueryProperty(
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN PIRP Irp)
|
|
|
|
{
|
|
|
|
PIO_STACK_LOCATION IoStack;
|
|
|
|
PSTORAGE_PROPERTY_QUERY PropertyQuery;
|
|
|
|
PSTORAGE_DESCRIPTOR_HEADER DescriptorHeader;
|
|
|
|
PSTORAGE_ADAPTER_DESCRIPTOR AdapterDescriptor;
|
2011-05-15 17:57:02 +00:00
|
|
|
ULONG FieldLengthVendor, FieldLengthProduct, FieldLengthRevision, TotalLength, FieldLengthSerialNumber;
|
2011-05-06 13:29:27 +00:00
|
|
|
PPDO_DEVICE_EXTENSION PDODeviceExtension;
|
|
|
|
PUFI_INQUIRY_RESPONSE InquiryData;
|
|
|
|
PSTORAGE_DEVICE_DESCRIPTOR DeviceDescriptor;
|
|
|
|
PUCHAR Buffer;
|
2011-05-15 17:57:02 +00:00
|
|
|
PFDO_DEVICE_EXTENSION FDODeviceExtension;
|
|
|
|
UNICODE_STRING SerialNumber;
|
|
|
|
ANSI_STRING AnsiString;
|
|
|
|
NTSTATUS Status;
|
2011-05-06 13:29:27 +00:00
|
|
|
|
2012-02-12 02:53:34 +00:00
|
|
|
DPRINT("USBSTOR_HandleQueryProperty\n");
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// get current stack location
|
|
|
|
//
|
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
|
|
|
|
//
|
|
|
|
// sanity check
|
|
|
|
//
|
|
|
|
ASSERT(IoStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(STORAGE_PROPERTY_QUERY));
|
|
|
|
ASSERT(Irp->AssociatedIrp.SystemBuffer);
|
|
|
|
|
|
|
|
//
|
|
|
|
// get property query
|
|
|
|
//
|
|
|
|
PropertyQuery = (PSTORAGE_PROPERTY_QUERY)Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
|
|
|
|
//
|
|
|
|
// check property type
|
|
|
|
//
|
|
|
|
if (PropertyQuery->PropertyId != StorageDeviceProperty &&
|
|
|
|
PropertyQuery->PropertyId != StorageAdapterProperty)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// only device property / adapter property are supported
|
|
|
|
//
|
|
|
|
return STATUS_INVALID_PARAMETER_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// check query type
|
|
|
|
//
|
|
|
|
if (PropertyQuery->QueryType == PropertyExistsQuery)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// device property / adapter property is supported
|
|
|
|
//
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PropertyQuery->QueryType != PropertyStandardQuery)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// only standard query and exists query are supported
|
|
|
|
//
|
|
|
|
return STATUS_INVALID_PARAMETER_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// check if it is a device property
|
|
|
|
//
|
|
|
|
if (PropertyQuery->PropertyId == StorageDeviceProperty)
|
|
|
|
{
|
2012-02-12 02:53:34 +00:00
|
|
|
DPRINT("USBSTOR_HandleQueryProperty StorageDeviceProperty OutputBufferLength %lu\n", IoStack->Parameters.DeviceIoControl.OutputBufferLength);
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// get device extension
|
|
|
|
//
|
|
|
|
PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
|
|
|
|
ASSERT(PDODeviceExtension);
|
2011-05-15 17:57:02 +00:00
|
|
|
ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
|
|
|
|
|
|
|
|
//
|
|
|
|
// get device extension
|
|
|
|
//
|
|
|
|
FDODeviceExtension = (PFDO_DEVICE_EXTENSION)PDODeviceExtension->LowerDeviceObject->DeviceExtension;
|
|
|
|
ASSERT(FDODeviceExtension);
|
|
|
|
ASSERT(FDODeviceExtension->Common.IsFDO);
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// get inquiry data
|
|
|
|
//
|
|
|
|
InquiryData = (PUFI_INQUIRY_RESPONSE)PDODeviceExtension->InquiryData;
|
|
|
|
ASSERT(InquiryData);
|
|
|
|
|
|
|
|
//
|
|
|
|
// compute extra parameters length
|
|
|
|
//
|
|
|
|
FieldLengthVendor = USBSTOR_GetFieldLength(InquiryData->Vendor, 8);
|
|
|
|
FieldLengthProduct = USBSTOR_GetFieldLength(InquiryData->Product, 16);
|
|
|
|
FieldLengthRevision = USBSTOR_GetFieldLength(InquiryData->Revision, 4);
|
|
|
|
|
|
|
|
//
|
2011-05-15 17:57:02 +00:00
|
|
|
// is there a serial number
|
2011-05-06 13:29:27 +00:00
|
|
|
//
|
2011-05-15 17:57:02 +00:00
|
|
|
if (FDODeviceExtension->SerialNumber)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// get length
|
|
|
|
//
|
|
|
|
FieldLengthSerialNumber = wcslen(FDODeviceExtension->SerialNumber->bString);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// no serial number
|
|
|
|
//
|
|
|
|
FieldLengthSerialNumber = 0;
|
|
|
|
}
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
//
|
2011-05-15 17:57:02 +00:00
|
|
|
// total length required is sizeof(STORAGE_DEVICE_DESCRIPTOR) + FieldLength + 4 extra null bytes - 1
|
2011-05-06 13:29:27 +00:00
|
|
|
// -1 due STORAGE_DEVICE_DESCRIPTOR contains one byte length of parameter data
|
|
|
|
//
|
2011-05-15 17:57:02 +00:00
|
|
|
TotalLength = sizeof(STORAGE_DEVICE_DESCRIPTOR) + FieldLengthVendor + FieldLengthProduct + FieldLengthRevision + FieldLengthSerialNumber + 3;
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// check if output buffer is long enough
|
|
|
|
//
|
|
|
|
if (IoStack->Parameters.DeviceIoControl.OutputBufferLength < TotalLength)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// buffer too small
|
|
|
|
//
|
|
|
|
DescriptorHeader = (PSTORAGE_DESCRIPTOR_HEADER)Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
ASSERT(IoStack->Parameters.DeviceIoControl.OutputBufferLength >= sizeof(STORAGE_DESCRIPTOR_HEADER));
|
|
|
|
|
|
|
|
//
|
|
|
|
// return required size
|
|
|
|
//
|
|
|
|
DescriptorHeader->Version = TotalLength;
|
|
|
|
DescriptorHeader->Size = TotalLength;
|
|
|
|
|
|
|
|
Irp->IoStatus.Information = sizeof(STORAGE_DESCRIPTOR_HEADER);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// get device descriptor
|
|
|
|
//
|
|
|
|
DeviceDescriptor = (PSTORAGE_DEVICE_DESCRIPTOR)Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
|
|
|
|
//
|
|
|
|
// initialize device descriptor
|
|
|
|
//
|
|
|
|
DeviceDescriptor->Version = TotalLength;
|
|
|
|
DeviceDescriptor->Size = TotalLength;
|
|
|
|
DeviceDescriptor->DeviceType = InquiryData->DeviceType;
|
|
|
|
DeviceDescriptor->DeviceTypeModifier = (InquiryData->RMB & 0x7F);
|
2012-01-25 01:09:59 +00:00
|
|
|
DeviceDescriptor->RemovableMedia = (InquiryData->RMB & 0x80) ? TRUE : FALSE;
|
2011-05-06 13:29:27 +00:00
|
|
|
DeviceDescriptor->CommandQueueing = FALSE;
|
|
|
|
DeviceDescriptor->BusType = BusTypeUsb;
|
|
|
|
DeviceDescriptor->VendorIdOffset = sizeof(STORAGE_DEVICE_DESCRIPTOR) - sizeof(UCHAR);
|
|
|
|
DeviceDescriptor->ProductIdOffset = DeviceDescriptor->VendorIdOffset + FieldLengthVendor + 1;
|
|
|
|
DeviceDescriptor->ProductRevisionOffset = DeviceDescriptor->ProductIdOffset + FieldLengthProduct + 1;
|
2011-05-15 17:57:02 +00:00
|
|
|
DeviceDescriptor->SerialNumberOffset = (FieldLengthSerialNumber > 0 ? DeviceDescriptor->ProductRevisionOffset + FieldLengthRevision + 1 : 0);
|
|
|
|
DeviceDescriptor->RawPropertiesLength = FieldLengthVendor + FieldLengthProduct + FieldLengthRevision + FieldLengthSerialNumber + 3 + (FieldLengthSerialNumber > 0 ? + 1 : 0);
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// copy descriptors
|
|
|
|
//
|
|
|
|
Buffer = (PUCHAR)((ULONG_PTR)DeviceDescriptor + sizeof(STORAGE_DEVICE_DESCRIPTOR) - sizeof(UCHAR));
|
|
|
|
|
|
|
|
//
|
|
|
|
// copy vendor
|
|
|
|
//
|
|
|
|
RtlCopyMemory(Buffer, InquiryData->Vendor, FieldLengthVendor);
|
|
|
|
Buffer[FieldLengthVendor] = '\0';
|
|
|
|
Buffer += FieldLengthVendor + 1;
|
|
|
|
|
|
|
|
//
|
|
|
|
// copy product
|
|
|
|
//
|
|
|
|
RtlCopyMemory(Buffer, InquiryData->Product, FieldLengthProduct);
|
|
|
|
Buffer[FieldLengthProduct] = '\0';
|
|
|
|
Buffer += FieldLengthProduct + 1;
|
|
|
|
|
|
|
|
//
|
|
|
|
// copy revision
|
|
|
|
//
|
|
|
|
RtlCopyMemory(Buffer, InquiryData->Revision, FieldLengthRevision);
|
|
|
|
Buffer[FieldLengthRevision] = '\0';
|
|
|
|
Buffer += FieldLengthRevision + 1;
|
|
|
|
|
|
|
|
//
|
2011-05-15 17:57:02 +00:00
|
|
|
// copy serial number
|
2011-05-06 13:29:27 +00:00
|
|
|
//
|
2011-05-15 17:57:02 +00:00
|
|
|
if (FieldLengthSerialNumber)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// init unicode string
|
|
|
|
//
|
|
|
|
RtlInitUnicodeString(&SerialNumber, FDODeviceExtension->SerialNumber->bString);
|
|
|
|
|
|
|
|
//
|
|
|
|
// init ansi string
|
|
|
|
//
|
|
|
|
AnsiString.Buffer = (PCHAR)Buffer;
|
|
|
|
AnsiString.Length = 0;
|
|
|
|
AnsiString.MaximumLength = FieldLengthSerialNumber * sizeof(WCHAR);
|
|
|
|
|
|
|
|
//
|
|
|
|
// convert to ansi code
|
|
|
|
//
|
|
|
|
Status = RtlUnicodeStringToAnsiString(&AnsiString, &SerialNumber, FALSE);
|
|
|
|
ASSERT(Status == STATUS_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
DPRINT("Vendor %s\n", (LPCSTR)((ULONG_PTR)DeviceDescriptor + DeviceDescriptor->VendorIdOffset));
|
|
|
|
DPRINT("Product %s\n", (LPCSTR)((ULONG_PTR)DeviceDescriptor + DeviceDescriptor->ProductIdOffset));
|
|
|
|
DPRINT("Revision %s\n", (LPCSTR)((ULONG_PTR)DeviceDescriptor + DeviceDescriptor->ProductRevisionOffset));
|
2011-05-15 17:57:02 +00:00
|
|
|
DPRINT("Serial %s\n", (LPCSTR)((ULONG_PTR)DeviceDescriptor + DeviceDescriptor->SerialNumberOffset));
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// done
|
|
|
|
//
|
|
|
|
Irp->IoStatus.Information = TotalLength;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// adapter property query request
|
|
|
|
//
|
2012-02-12 02:53:34 +00:00
|
|
|
DPRINT("USBSTOR_HandleQueryProperty StorageAdapterProperty OutputBufferLength %lu\n", IoStack->Parameters.DeviceIoControl.OutputBufferLength);
|
2011-05-06 13:29:27 +00:00
|
|
|
|
|
|
|
if (IoStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(STORAGE_ADAPTER_DESCRIPTOR))
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// buffer too small
|
|
|
|
//
|
|
|
|
DescriptorHeader = (PSTORAGE_DESCRIPTOR_HEADER)Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
ASSERT(IoStack->Parameters.DeviceIoControl.OutputBufferLength >= sizeof(STORAGE_DESCRIPTOR_HEADER));
|
|
|
|
|
|
|
|
//
|
|
|
|
// return required size
|
|
|
|
//
|
|
|
|
DescriptorHeader->Version = sizeof(STORAGE_ADAPTER_DESCRIPTOR);
|
|
|
|
DescriptorHeader->Size = sizeof(STORAGE_ADAPTER_DESCRIPTOR);
|
|
|
|
|
|
|
|
Irp->IoStatus.Information = sizeof(STORAGE_DESCRIPTOR_HEADER);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// get adapter descriptor, information is returned in the same buffer
|
|
|
|
//
|
|
|
|
AdapterDescriptor = (PSTORAGE_ADAPTER_DESCRIPTOR)Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
|
|
|
|
//
|
|
|
|
// fill out descriptor
|
|
|
|
//
|
|
|
|
AdapterDescriptor->Version = sizeof(STORAGE_ADAPTER_DESCRIPTOR);
|
|
|
|
AdapterDescriptor->Size = sizeof(STORAGE_ADAPTER_DESCRIPTOR);
|
|
|
|
AdapterDescriptor->MaximumTransferLength = MAXULONG; //FIXME compute some sane value
|
|
|
|
AdapterDescriptor->MaximumPhysicalPages = 25; //FIXME compute some sane value
|
|
|
|
AdapterDescriptor->AlignmentMask = 0;
|
|
|
|
AdapterDescriptor->AdapterUsesPio = FALSE;
|
|
|
|
AdapterDescriptor->AdapterScansDown = FALSE;
|
|
|
|
AdapterDescriptor->CommandQueueing = FALSE;
|
|
|
|
AdapterDescriptor->AcceleratedTransfer = FALSE;
|
|
|
|
AdapterDescriptor->BusType = BusTypeUsb;
|
|
|
|
AdapterDescriptor->BusMajorVersion = 0x2; //FIXME verify
|
|
|
|
AdapterDescriptor->BusMinorVersion = 0x00; //FIXME
|
|
|
|
|
|
|
|
//
|
|
|
|
// store returned length
|
|
|
|
//
|
|
|
|
Irp->IoStatus.Information = sizeof(STORAGE_ADAPTER_DESCRIPTOR);
|
|
|
|
|
|
|
|
//
|
|
|
|
// done
|
|
|
|
//
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 00:39:00 +00:00
|
|
|
NTSTATUS
|
|
|
|
USBSTOR_HandleDeviceControl(
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN PIRP Irp)
|
|
|
|
{
|
|
|
|
PIO_STACK_LOCATION IoStack;
|
2011-05-06 16:25:01 +00:00
|
|
|
NTSTATUS Status;
|
2012-01-22 16:39:53 +00:00
|
|
|
PPDO_DEVICE_EXTENSION PDODeviceExtension;
|
|
|
|
PSCSI_ADAPTER_BUS_INFO BusInfo;
|
|
|
|
PSCSI_INQUIRY_DATA InquiryData;
|
|
|
|
PINQUIRYDATA ScsiInquiryData;
|
|
|
|
PUFI_INQUIRY_RESPONSE UFIInquiryResponse;
|
2011-05-05 00:39:00 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// get current stack location
|
|
|
|
//
|
|
|
|
IoStack = IoGetCurrentIrpStackLocation(Irp);
|
|
|
|
|
2011-05-06 13:29:27 +00:00
|
|
|
if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_STORAGE_QUERY_PROPERTY)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// query property
|
|
|
|
//
|
2011-05-06 16:25:01 +00:00
|
|
|
Status = USBSTOR_HandleQueryProperty(DeviceObject, Irp);
|
|
|
|
}
|
|
|
|
else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_SCSI_PASS_THROUGH)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// query scsi pass through
|
|
|
|
//
|
|
|
|
DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH NOT implemented\n");
|
|
|
|
Status = STATUS_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_SCSI_PASS_THROUGH_DIRECT)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// query scsi pass through direct
|
|
|
|
//
|
|
|
|
DPRINT1("USBSTOR_HandleDeviceControl IOCTL_SCSI_PASS_THROUGH_DIRECT NOT implemented\n");
|
|
|
|
Status = STATUS_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// query serial number
|
|
|
|
//
|
|
|
|
DPRINT1("USBSTOR_HandleDeviceControl IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER NOT implemented\n");
|
|
|
|
Status = STATUS_NOT_SUPPORTED;
|
|
|
|
}
|
2012-01-22 16:39:53 +00:00
|
|
|
else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_SCSI_GET_CAPABILITIES)
|
|
|
|
{
|
|
|
|
PIO_SCSI_CAPABILITIES Capabilities;
|
|
|
|
|
|
|
|
/* Legacy port capability query */
|
|
|
|
if (IoStack->Parameters.DeviceIoControl.OutputBufferLength == sizeof(PVOID))
|
|
|
|
{
|
|
|
|
Capabilities = *((PVOID *)Irp->AssociatedIrp.SystemBuffer) = ExAllocatePool(NonPagedPool, sizeof(IO_SCSI_CAPABILITIES));
|
|
|
|
Irp->IoStatus.Information = sizeof(PVOID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Capabilities = Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
Irp->IoStatus.Information = sizeof(IO_SCSI_CAPABILITIES);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Capabilities)
|
|
|
|
{
|
|
|
|
Capabilities->MaximumTransferLength = MAXULONG;
|
|
|
|
Capabilities->MaximumPhysicalPages = 25;
|
|
|
|
Capabilities->SupportedAsynchronousEvents = 0;
|
|
|
|
Capabilities->AlignmentMask = 0;
|
|
|
|
Capabilities->TaggedQueuing = FALSE;
|
|
|
|
Capabilities->AdapterScansDown = FALSE;
|
|
|
|
Capabilities->AdapterUsesPio = FALSE;
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_SCSI_GET_INQUIRY_DATA)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// get device extension
|
|
|
|
//
|
|
|
|
PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
|
|
|
|
ASSERT(PDODeviceExtension);
|
|
|
|
ASSERT(PDODeviceExtension->Common.IsFDO == FALSE);
|
|
|
|
|
|
|
|
//
|
|
|
|
// get parameters
|
|
|
|
//
|
|
|
|
BusInfo = Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
InquiryData = (PSCSI_INQUIRY_DATA)(BusInfo + 1);
|
|
|
|
ScsiInquiryData = (PINQUIRYDATA)InquiryData->InquiryData;
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// get inquiry data
|
|
|
|
//
|
|
|
|
UFIInquiryResponse = (PUFI_INQUIRY_RESPONSE)PDODeviceExtension->InquiryData;
|
|
|
|
ASSERT(UFIInquiryResponse);
|
|
|
|
|
|
|
|
|
|
|
|
BusInfo->NumberOfBuses = 1;
|
|
|
|
BusInfo->BusData[0].NumberOfLogicalUnits = 1; //FIXME
|
|
|
|
BusInfo->BusData[0].InitiatorBusId = 0;
|
|
|
|
BusInfo->BusData[0].InquiryDataOffset = sizeof(SCSI_ADAPTER_BUS_INFO);
|
|
|
|
|
|
|
|
InquiryData->PathId = 0;
|
|
|
|
InquiryData->TargetId = 0;
|
|
|
|
InquiryData->Lun = PDODeviceExtension->LUN & MAX_LUN;
|
|
|
|
InquiryData->DeviceClaimed = PDODeviceExtension->Claimed;
|
|
|
|
InquiryData->InquiryDataLength = sizeof(INQUIRYDATA);
|
|
|
|
InquiryData->NextInquiryDataOffset = 0;
|
|
|
|
|
|
|
|
RtlZeroMemory(ScsiInquiryData, sizeof(INQUIRYDATA));
|
|
|
|
ScsiInquiryData->DeviceType = UFIInquiryResponse->DeviceType;
|
|
|
|
ScsiInquiryData->DeviceTypeQualifier = (UFIInquiryResponse->RMB & 0x7F);
|
2012-01-25 00:03:18 +00:00
|
|
|
|
|
|
|
/* Hack for IoReadPartitionTable call in disk.sys */
|
2012-01-25 01:09:59 +00:00
|
|
|
ScsiInquiryData->RemovableMedia = ((ScsiInquiryData->DeviceType != DIRECT_ACCESS_DEVICE) ? ((UFIInquiryResponse->RMB & 0x80) ? 1 : 0) : 0);
|
2012-01-25 00:03:18 +00:00
|
|
|
|
2012-01-22 16:39:53 +00:00
|
|
|
ScsiInquiryData->Versions = 0x04;
|
|
|
|
ScsiInquiryData->ResponseDataFormat = 0x02;
|
|
|
|
ScsiInquiryData->AdditionalLength = 31;
|
|
|
|
ScsiInquiryData->SoftReset = 0;
|
|
|
|
ScsiInquiryData->CommandQueue = 0;
|
|
|
|
ScsiInquiryData->LinkedCommands = 0;
|
|
|
|
ScsiInquiryData->RelativeAddressing = 0;
|
|
|
|
|
|
|
|
RtlCopyMemory(&ScsiInquiryData->VendorId, UFIInquiryResponse->Vendor, USBSTOR_GetFieldLength(UFIInquiryResponse->Vendor, 8));
|
|
|
|
RtlCopyMemory(&ScsiInquiryData->ProductId, UFIInquiryResponse->Product, USBSTOR_GetFieldLength(UFIInquiryResponse->Product, 16));
|
|
|
|
|
|
|
|
Irp->IoStatus.Information = sizeof(SCSI_ADAPTER_BUS_INFO) + sizeof(SCSI_INQUIRY_DATA) + sizeof(INQUIRYDATA) - 1;
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
else if (IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_SCSI_GET_ADDRESS)
|
|
|
|
{
|
|
|
|
PSCSI_ADDRESS Address = Irp->AssociatedIrp.SystemBuffer;
|
|
|
|
|
|
|
|
Address->Length = sizeof(SCSI_ADDRESS);
|
|
|
|
Address->PortNumber = 0;
|
|
|
|
Address->PathId = 0;
|
|
|
|
Address->TargetId = 0;
|
|
|
|
Address->Lun = (((PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension)->LUN & MAX_LUN);
|
|
|
|
Irp->IoStatus.Information = sizeof(SCSI_ADDRESS);
|
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
}
|
2011-05-06 16:25:01 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// unsupported
|
|
|
|
//
|
|
|
|
DPRINT("USBSTOR_HandleDeviceControl IoControl %x not supported\n", IoStack->Parameters.DeviceIoControl.IoControlCode);
|
|
|
|
Status = STATUS_NOT_SUPPORTED;
|
2011-05-06 13:29:27 +00:00
|
|
|
}
|
|
|
|
|
2011-05-06 16:25:01 +00:00
|
|
|
return Status;
|
2011-05-05 00:39:00 +00:00
|
|
|
}
|