Author: janderwald
Date: Wed May 4 20:19:33 2011
New Revision: 51580
URL:
http://svn.reactos.org/svn/reactos?rev=51580&view=rev
Log:
[USBSTOR]
- Rewrite hardware id / instance id matching
- Implement support for compatible id
- UsbStor still not attached to disk.sys, needs more research on how to get this done
Modified:
branches/usb-bringup/drivers/usb/usbstor/pdo.c
branches/usb-bringup/drivers/usb/usbstor/usbstor.c
Modified: branches/usb-bringup/drivers/usb/usbstor/pdo.c
URL:
http://svn.reactos.org/svn/reactos/branches/usb-bringup/drivers/usb/usbstor…
==============================================================================
--- branches/usb-bringup/drivers/usb/usbstor/pdo.c [iso-8859-1] (original)
+++ branches/usb-bringup/drivers/usb/usbstor/pdo.c [iso-8859-1] Wed May 4 20:19:33 2011
@@ -160,14 +160,100 @@
for(Index = 0; Index < MaxLength; Index++)
{
- if (Name[Index] == '\0')
- return Index;
-
- Buffer[Index] = Name[Index];
+ if (Name[Index] <= ' ' || Name[Index] >= 0x7F /* last printable
ascii character */ || Name[Index] == ',')
+ {
+ //
+ // convert to underscore
+ //
+ Buffer[Index] = '_';
+ }
+ else
+ {
+ //
+ // just copy character
+ //
+ Buffer[Index] = Name[Index];
+ }
}
return MaxLength;
}
+
+NTSTATUS
+USBSTOR_PdoHandleQueryDeviceText(
+ IN PDEVICE_OBJECT DeviceObject,
+ IN PIRP Irp)
+{
+ //PPDO_DEVICE_EXTENSION DeviceExtension;
+ PIO_STACK_LOCATION IoStack;
+ LPWSTR Buffer;
+ static WCHAR DeviceText[] = L"USB Mass Storage Device";
+
+ //
+ // get current stack location
+ //
+ IoStack = IoGetCurrentIrpStackLocation(Irp);
+
+ if (IoStack->Parameters.QueryDeviceText.DeviceTextType == DeviceTextDescription)
+ {
+ DPRINT1("USBSTOR_PdoHandleQueryDeviceText DeviceTextDescription\n");
+
+ //
+ // allocate item
+ //
+ Buffer = (LPWSTR)AllocateItem(PagedPool, sizeof(DeviceText));
+ if (!Buffer)
+ {
+ //
+ // no memory
+ //
+ Irp->IoStatus.Information = 0;
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ //
+ // copy buffer
+ //
+ wcscpy(Buffer, DeviceText);
+
+ //
+ // save result
+ //
+ Irp->IoStatus.Information = (ULONG_PTR)Buffer;
+ return STATUS_SUCCESS;
+ }
+ else
+ {
+ DPRINT1("USBSTOR_PdoHandleQueryDeviceText
DeviceTextLocationInformation\n");
+
+ //
+ // allocate item
+ //
+ Buffer = (LPWSTR)AllocateItem(PagedPool, sizeof(DeviceText));
+ if (!Buffer)
+ {
+ //
+ // no memory
+ //
+ Irp->IoStatus.Information = 0;
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ //
+ // copy buffer
+ //
+ wcscpy(Buffer, DeviceText);
+
+ //
+ // save result
+ //
+ Irp->IoStatus.Information = (ULONG_PTR)Buffer;
+ return STATUS_SUCCESS;
+ }
+
+}
+
+
NTSTATUS
@@ -179,7 +265,7 @@
NTSTATUS Status;
UCHAR Buffer[100];
LPCSTR DeviceType;
- ULONG Offset = 0, Index;
+ ULONG Offset = 0;
PUFI_INQUIRY_RESPONSE InquiryData;
ANSI_STRING AnsiString;
UNICODE_STRING DeviceId;
@@ -214,7 +300,7 @@
//
// lets create device string
//
- Offset = sprintf(&Buffer[Offset], "USBSTOR\\%s&Ven_", DeviceType);
+ Offset = sprintf(&Buffer[Offset], "USBSTOR\\") + 1;
//
// copy vendor id
@@ -222,43 +308,14 @@
Offset += CopyField(InquiryData->Vendor, &Buffer[Offset], 8);
//
- // copy product string
- //
- Offset += sprintf(&Buffer[Offset], "&Prod_");
-
- //
// copy product identifier
//
Offset += CopyField(InquiryData->Product, &Buffer[Offset], 16);
//
- // copy revision string
- //
- Offset += sprintf(&Buffer[Offset], "&Rev_");
-
- //
// copy revision identifer
//
Offset += CopyField(InquiryData->Revision, &Buffer[Offset], 4);
-
- //
- // FIXME: device serial number
- //
- Offset +=sprintf(&Buffer[Offset], "\\00000000&%d",
DeviceExtension->LUN);
-
- //
- // now convert restricted characters to underscores
- //
- for(Index = 0; Index < Offset; Index++)
- {
- if (Buffer[Index] <= ' ' || Buffer[Index] >= 0x7F /* last printable
ascii character */ || Buffer[Index] == ',')
- {
- //
- // convert to underscore
- //
- Buffer[Index] = '_';
- }
- }
//
// now initialize ansi string
@@ -302,6 +359,46 @@
return Status;
}
+VOID
+USBSTOR_ConvertToUnicodeString(
+ IN CHAR * Buffer,
+ IN ULONG ResultBufferLength,
+ IN ULONG ResultBufferOffset,
+ OUT LPWSTR ResultBuffer,
+ OUT PULONG NewResultBufferOffset)
+{
+ UNICODE_STRING DeviceString;
+ ANSI_STRING AnsiString;
+ NTSTATUS Status;
+
+ ASSERT(ResultBufferLength);
+ ASSERT(ResultBufferLength > ResultBufferOffset);
+
+ DPRINT1("ResultBufferOffset %lu ResultBufferLength %lu Buffer %s Length
%lu\n", ResultBufferOffset, ResultBufferLength, Buffer, strlen(Buffer));
+
+ DeviceString.Buffer = &ResultBuffer[ResultBufferOffset];
+ DeviceString.Length = 0;
+ DeviceString.MaximumLength = (ResultBufferLength - ResultBufferOffset) *
sizeof(WCHAR);
+
+ RtlInitAnsiString(&AnsiString, Buffer);
+
+ Status = RtlAnsiStringToUnicodeString(&DeviceString, &AnsiString, FALSE);
+ ASSERT(Status == STATUS_SUCCESS);
+
+ //
+ // subtract consumed bytes
+ //
+ ResultBufferLength -= (DeviceString.Length + sizeof(WCHAR)) / sizeof(WCHAR);
+ ResultBufferOffset += (DeviceString.Length + sizeof(WCHAR)) / sizeof(WCHAR);
+
+ //
+ // store new offset
+ //
+ *NewResultBufferOffset = ResultBufferOffset;
+}
+
+
+
NTSTATUS
USBSTOR_PdoHandleQueryHardwareId(
IN PDEVICE_OBJECT DeviceObject,
@@ -309,12 +406,14 @@
{
PPDO_DEVICE_EXTENSION PDODeviceExtension;
PFDO_DEVICE_EXTENSION FDODeviceExtension;
- WCHAR Buffer[200];
- ULONG Length;
- LPWSTR DeviceName;
LPCWSTR GenericType;
-
- DPRINT1("USBSTOR_PdoHandleQueryInstanceId\n");
+ LPWSTR Buffer;
+ CHAR Id1[50], Id2[50], Id3[50], Id4[50], Id5[50], Id6[50];
+ ULONG Id1Length, Id2Length, Id3Length, Id4Length, Id5Length,Id6Length;
+ ULONG Offset, TotalLength, Length;
+ PUFI_INQUIRY_RESPONSE InquiryData;
+
+ DPRINT1("USBSTOR_PdoHandleQueryHardwareId\n");
//
// get PDO device extension
@@ -332,39 +431,101 @@
ASSERT(FDODeviceExtension->DeviceDescriptor);
//
+ // get inquiry data
+ //
+ InquiryData = (PUFI_INQUIRY_RESPONSE)PDODeviceExtension->InquiryData;
+
+
+ //
// get generic type
//
- GenericType =
USBSTOR_GetGenericType((PUFI_INQUIRY_RESPONSE)PDODeviceExtension->InquiryData);
+ GenericType = USBSTOR_GetGenericType(InquiryData);
ASSERT(GenericType);
//
- // zero buffer
- //
- RtlZeroMemory(Buffer, sizeof(Buffer));
-
- //
- // format hardware id
- //
- Length = swprintf(Buffer, L"USB\\VID_%04x&Pid_%04x&Rev_%04x",
FDODeviceExtension->DeviceDescriptor->idVendor,
FDODeviceExtension->DeviceDescriptor->idProduct,
FDODeviceExtension->DeviceDescriptor->bcdDevice) + 1;
- Length += swprintf(&Buffer[Length], L"USB\\VID_%04x&Pid_%04x",
FDODeviceExtension->DeviceDescriptor->idVendor,
FDODeviceExtension->DeviceDescriptor->idProduct) + 1;
- Length += swprintf(&Buffer[Length], L"USBSTOR\\%s", GenericType) + 1;
- Length += swprintf(&Buffer[Length], L"%s", GenericType) + 1;
-
- //
- // TODO: add more ids
- //
-
- Buffer[Length] = UNICODE_NULL;
- Length++;
-
- DPRINT1("Name %S\n", Buffer);
+ // generate id 1
+ // USBSTOR\SCSIType_Vendor(8)_Product(16)_Revision(4)
+ //
+ RtlZeroMemory(Id1, sizeof(Id1));
+ Offset = 0;
+ Offset = sprintf(&Id1[Offset], "USBSTOR\\");
+ Offset += sprintf(&Id1[Offset], "Disk"); //FIXME
+ Offset += CopyField(InquiryData->Vendor, &Id1[Offset], 8);
+ Offset += CopyField(InquiryData->Product, &Id1[Offset], 16);
+ Offset += CopyField(InquiryData->Revision, &Id1[Offset], 4);
+ Id1Length = strlen(Id1) + 1;
+ DPRINT1("HardwareId1 %s\n", Id1);
+
+ //
+ // generate id 2
+ // USBSTOR\SCSIType_VENDOR(8)_Product(16)
+ //
+ RtlZeroMemory(Id2, sizeof(Id2));
+ Offset = 0;
+ Offset = sprintf(&Id2[Offset], "USBSTOR\\");
+ Offset += sprintf(&Id2[Offset], "Disk"); //FIXME
+ Offset += CopyField(InquiryData->Vendor, &Id2[Offset], 8);
+ Offset += CopyField(InquiryData->Product, &Id2[Offset], 16);
+ Id2Length = strlen(Id2) + 1;
+ DPRINT1("HardwareId2 %s\n", Id2);
+
+ //
+ // generate id 3
+ // USBSTOR\SCSIType_VENDOR(8)
+ //
+ RtlZeroMemory(Id3, sizeof(Id3));
+ Offset = 0;
+ Offset = sprintf(&Id3[Offset], "USBSTOR\\");
+ Offset += sprintf(&Id3[Offset], "Disk"); //FIXME
+ Offset += CopyField(InquiryData->Vendor, &Id3[Offset], 8);
+ Id3Length = strlen(Id3) + 1;
+ DPRINT1("HardwareId3 %s\n", Id3);
+
+ //
+ // generate id 4
+ // USBSTOR\SCSIType_VENDOR(8)_Product(16)_Revision(1)
+ //
+ RtlZeroMemory(Id4, sizeof(Id4));
+ Offset = 0;
+ Offset = sprintf(&Id4[Offset], "USBSTOR\\");
+ Offset += sprintf(&Id4[Offset], "Disk"); //FIXME
+ Offset += CopyField(InquiryData->Vendor, &Id4[Offset], 8);
+ Offset += CopyField(InquiryData->Product, &Id4[Offset], 16);
+ Offset += CopyField(InquiryData->Revision, &Id4[Offset], 1);
+ Id4Length = strlen(Id4) + 1;
+ DPRINT1("HardwareId4 %s\n", Id4);
+
+ //
+ // generate id 5
+ // USBSTOR\SCSIType
+ //
+ RtlZeroMemory(Id5, sizeof(Id5));
+ Offset = 0;
+ Offset = sprintf(&Id5[Offset], "USBSTOR\\");
+ Offset += sprintf(&Id5[Offset], "GenDisk"); //FIXME
+ Id5Length = strlen(Id5) + 1;
+ DPRINT1("HardwareId5 %s\n", Id5);
+
+ //
+ // generate id 6
+ // SCSIType
+ //
+ RtlZeroMemory(Id6, sizeof(Id6));
+ Offset = 0;
+ Offset = sprintf(&Id6[Offset], "GenDisk"); //FIXME
+ Id6Length = strlen(Id6) + 1;
+ DPRINT1("HardwareId6 %s\n", Id6);
+
+ //
+ // compute total length
+ //
+ TotalLength = Id1Length + Id2Length + Id3Length + Id4Length + Id5Length + Id6Length +
1;
//
// allocate buffer
//
- DeviceName = (LPWSTR)AllocateItem(PagedPool, Length * sizeof(WCHAR));
-
- if (!DeviceName)
+ Buffer = (LPWSTR)AllocateItem(PagedPool, TotalLength * sizeof(WCHAR));
+ if (!Buffer)
{
//
// no memory
@@ -374,14 +535,24 @@
}
//
- // copy device name
- //
- RtlMoveMemory(DeviceName, Buffer, Length * sizeof(WCHAR));
+ // reset offset
+ //
+ Offset = 0;
+ Length = TotalLength;
+
+ USBSTOR_ConvertToUnicodeString(Id1, Length, Offset, Buffer, &Offset);
+ USBSTOR_ConvertToUnicodeString(Id2, Length, Offset, Buffer, &Offset);
+ USBSTOR_ConvertToUnicodeString(Id3, Length, Offset, Buffer, &Offset);
+ USBSTOR_ConvertToUnicodeString(Id4, Length, Offset, Buffer, &Offset);
+ USBSTOR_ConvertToUnicodeString(Id5, Length, Offset, Buffer, &Offset);
+ USBSTOR_ConvertToUnicodeString(Id6, Length, Offset, Buffer, &Offset);
+
+ DPRINT1("Offset %lu Length %lu\n", Offset, Length);
//
// store result
//
- Irp->IoStatus.Information = (ULONG_PTR)DeviceName;
+ Irp->IoStatus.Information = (ULONG_PTR)Buffer;
//
// done
@@ -390,7 +561,7 @@
}
NTSTATUS
-USBSTOR_PdoHandleQueryInstanceId(
+USBSTOR_PdoHandleQueryCompatibleId(
IN PDEVICE_OBJECT DeviceObject,
IN OUT PIRP Irp)
{
@@ -399,6 +570,96 @@
WCHAR Buffer[100];
ULONG Length;
LPWSTR InstanceId;
+ LPCSTR DeviceType;
+
+ DPRINT1("USBSTOR_PdoHandleQueryCompatibleId\n");
+
+ //
+ // get PDO device extension
+ //
+ PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
+
+ //
+ // get FDO device extension
+ //
+ FDODeviceExtension =
(PFDO_DEVICE_EXTENSION)PDODeviceExtension->LowerDeviceObject->DeviceExtension;
+
+ //
+ // sanity check
+ //
+ ASSERT(FDODeviceExtension->DeviceDescriptor);
+
+ //
+ // get target device type
+ //
+ DeviceType =
USBSTOR_GetDeviceType((PUFI_INQUIRY_RESPONSE)PDODeviceExtension->InquiryData);
+
+ //
+ // zero memory
+ //
+ RtlZeroMemory(Buffer, sizeof(Buffer));
+
+ //
+ // format instance id
+ //
+ Length = swprintf(Buffer, L"USBSTOR\\%s", L"Disk") + 1;
+ Length += swprintf(&Buffer[Length], L"USBSTOR\\%s", L"RAW") +
2;
+
+ //
+ // verify this
+ //
+ // Length += swprintf(&Buffer[Length], L"USBSTOR\\RAW") + 1;
+
+ //Buffer[Length] = UNICODE_NULL;
+ //Buffer[Length+1] = UNICODE_NULL;
+ //Length++;
+
+ //
+ // calculate length
+ //
+
+
+ //
+ // allocate instance id
+ //
+ InstanceId = (LPWSTR)AllocateItem(PagedPool, Length * sizeof(WCHAR));
+ if (!InstanceId)
+ {
+ //
+ // no memory
+ //
+ Irp->IoStatus.Information = 0;
+ return STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ //
+ // copy instance id
+ //
+ wcscpy(InstanceId, Buffer);
+
+ DPRINT1("USBSTOR_PdoHandleQueryInstanceId %S\n", InstanceId);
+
+ //
+ // store result
+ //
+ Irp->IoStatus.Information = (ULONG_PTR)InstanceId;
+
+ //
+ // completed successfully
+ //
+ return STATUS_SUCCESS;
+}
+
+NTSTATUS
+USBSTOR_PdoHandleQueryInstanceId(
+ IN PDEVICE_OBJECT DeviceObject,
+ IN OUT PIRP Irp)
+{
+ PPDO_DEVICE_EXTENSION PDODeviceExtension;
+ PFDO_DEVICE_EXTENSION FDODeviceExtension;
+ WCHAR Buffer[100];
+ ULONG Length;
+ LPWSTR InstanceId;
DPRINT1("USBSTOR_PdoHandleQueryInstanceId\n");
@@ -420,7 +681,7 @@
//
// format instance id
//
- swprintf(Buffer, L"USB\\VID_%04x&PID_%04x\\%s",
FDODeviceExtension->DeviceDescriptor->idVendor,
FDODeviceExtension->DeviceDescriptor->idProduct, L"00000000");
+ swprintf(Buffer, L"USB\\VID_%04x&PID_%04x\\%s",
FDODeviceExtension->DeviceDescriptor->idVendor,
FDODeviceExtension->DeviceDescriptor->idProduct, L"09188212515A");
//
// calculate length
@@ -548,9 +809,10 @@
break;
}
case IRP_MN_QUERY_DEVICE_TEXT:
- DPRINT1("USBSTOR_PdoHandlePnp: IRP_MN_QUERY_DEVICE_TEXT
unimplemented\n");
- Status = STATUS_NOT_SUPPORTED;
+ {
+ Status = USBSTOR_PdoHandleQueryDeviceText(DeviceObject, Irp);
break;
+ }
case IRP_MN_QUERY_ID:
{
if (IoStack->Parameters.QueryId.IdType == BusQueryDeviceID)
@@ -577,6 +839,14 @@
Status = USBSTOR_PdoHandleQueryInstanceId(DeviceObject, Irp);
break;
}
+ else if (IoStack->Parameters.QueryId.IdType == BusQueryCompatibleIDs)
+ {
+ //
+ // handle instance id
+ //
+ Status = USBSTOR_PdoHandleQueryCompatibleId(DeviceObject, Irp);
+ break;
+ }
DPRINT1("USBSTOR_PdoHandlePnp: IRP_MN_QUERY_ID IdType %x
unimplemented\n", IoStack->Parameters.QueryId.IdType);
Status = STATUS_NOT_SUPPORTED;
@@ -593,6 +863,7 @@
// just forward irp to lower device
//
Status = USBSTOR_SyncForwardIrp(DeviceExtension->LowerDeviceObject, Irp);
+ ASSERT(Status == STATUS_SUCCESS);
if (NT_SUCCESS(Status))
{
@@ -600,7 +871,7 @@
// check if no unique id
//
Caps =
(PDEVICE_CAPABILITIES)IoStack->Parameters.DeviceCapabilities.Capabilities;
- Caps->UniqueID = FALSE; //FIXME
+ Caps->UniqueID = TRUE; //FIXME
Caps->Removable = TRUE; //FIXME
}
break;
@@ -656,7 +927,7 @@
//
// create child device object
//
- Status = IoCreateDevice(DeviceObject->DriverObject, sizeof(PDO_DEVICE_EXTENSION),
NULL, FILE_DEVICE_MASS_STORAGE, 0, FALSE, &PDO);
+ Status = IoCreateDevice(DeviceObject->DriverObject, sizeof(PDO_DEVICE_EXTENSION),
NULL, FILE_DEVICE_MASS_STORAGE, FILE_AUTOGENERATED_DEVICE_NAME, FALSE, &PDO);
if (!NT_SUCCESS(Status))
{
//
@@ -685,7 +956,7 @@
//
// set device flags
//
- PDO->Flags |= DO_BUFFERED_IO | DO_POWER_PAGABLE;
+ PDO->Flags |= DO_DIRECT_IO | DO_MAP_IO_BUFFER;
//
// device is initialized
Modified: branches/usb-bringup/drivers/usb/usbstor/usbstor.c
URL:
http://svn.reactos.org/svn/reactos/branches/usb-bringup/drivers/usb/usbstor…
==============================================================================
--- branches/usb-bringup/drivers/usb/usbstor/usbstor.c [iso-8859-1] (original)
+++ branches/usb-bringup/drivers/usb/usbstor/usbstor.c [iso-8859-1] Wed May 4 20:19:33
2011
@@ -29,7 +29,7 @@
//
// lets create the device
//
- Status = IoCreateDevice(DriverObject, sizeof(FDO_DEVICE_EXTENSION), 0,
FILE_DEVICE_BUS_EXTENDER, 0, FALSE, &DeviceObject);
+ Status = IoCreateDevice(DriverObject, sizeof(FDO_DEVICE_EXTENSION), 0,
FILE_DEVICE_BUS_EXTENDER, FILE_AUTOGENERATED_DEVICE_NAME | FILE_DEVICE_SECURE_OPEN, FALSE,
&DeviceObject);
//
// check for success
@@ -120,6 +120,7 @@
//
// function always succeeds ;)
//
+ DPRINT1("USBSTOR_DispatchScsi\n");
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
@@ -133,8 +134,7 @@
PDEVICE_OBJECT DeviceObject,
PIRP Irp)
{
- UNIMPLEMENTED
-
+ DPRINT1("USBSTOR_DispatchDeviceControl\n");
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
@@ -148,7 +148,7 @@
PDEVICE_OBJECT DeviceObject,
PIRP Irp)
{
- UNIMPLEMENTED
+ DPRINT1("USBSTOR_DispatchScsi\n");
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = STATUS_SUCCESS;
@@ -165,6 +165,7 @@
//
// read write ioctl is not supported
//
+ DPRINT1("USBSTOR_DispatchReadWrite\n");
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
IoCompleteRequest(Irp, IO_NO_INCREMENT);