ReactOS 0.4.15-dev-5669-g09dde2c
pdo.c File Reference
#include "usbstor.h"
#include <debug.h>
Include dependency graph for pdo.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

static LPCSTR USBSTOR_GetDeviceType (IN PINQUIRYDATA InquiryData)
 
static LPCSTR USBSTOR_GetGenericType (IN PINQUIRYDATA InquiryData)
 
static ULONG CopyField (IN PUCHAR Name, IN PCHAR Buffer, IN ULONG MaxLength)
 
static ULONG CopyFieldTruncate (IN PUCHAR Name, IN PCHAR Buffer, IN ULONG MaxLength)
 
NTSTATUS USBSTOR_PdoHandleQueryDeviceText (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 
NTSTATUS USBSTOR_PdoHandleQueryDeviceId (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
 
VOID USBSTOR_ConvertToUnicodeString (IN CHAR *Buffer, IN ULONG ResultBufferLength, IN ULONG ResultBufferOffset, OUT LPWSTR ResultBuffer, OUT PULONG NewResultBufferOffset)
 
NTSTATUS USBSTOR_PdoHandleQueryHardwareId (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_PdoHandleQueryCompatibleId (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_PdoHandleQueryInstanceId (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_PdoHandleDeviceRelations (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 
NTSTATUS USBSTOR_PdoHandlePnp (IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
 
NTSTATUS NTAPI USBSTOR_SyncCompletionRoutine (IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Ctx)
 
static NTSTATUS USBSTOR_SendInternalCdb (IN PDEVICE_OBJECT PdoDevice, IN PCDB Cdb, IN UCHAR CdbLength, IN ULONG TimeOutValue, OUT PVOID OutDataBuffer, OUT PULONG OutDataTransferLength)
 
static NTSTATUS USBSTOR_FillInquiryData (IN PDEVICE_OBJECT PDODeviceObject)
 
NTSTATUS USBSTOR_CreatePDO (IN PDEVICE_OBJECT DeviceObject, IN UCHAR LUN)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file pdo.c.

Function Documentation

◆ CopyField()

static ULONG CopyField ( IN PUCHAR  Name,
IN PCHAR  Buffer,
IN ULONG  MaxLength 
)
static

Definition at line 70 of file pdo.c.

74{
76
77 for (Index = 0; Index < MaxLength; Index++)
78 {
79 if (Name[Index] <= ' ' || Name[Index] >= 0x7F /* last printable ascii character */ || Name[Index] == ',')
80 {
81 // convert to underscore
82 Buffer[Index] = '_';
83 }
84 else
85 {
86 // just copy character
88 }
89 }
90
91 return MaxLength;
92}
Definition: bufpool.h:45
uint32_t ULONG
Definition: typedefs.h:59
_In_ WDFCOLLECTION _In_ ULONG Index

◆ CopyFieldTruncate()

static ULONG CopyFieldTruncate ( IN PUCHAR  Name,
IN PCHAR  Buffer,
IN ULONG  MaxLength 
)
static

Definition at line 96 of file pdo.c.

100{
101 ULONG Index;
102
103 for (Index = 0; Index < MaxLength; Index++)
104 {
105 if (Name[Index] == '\0')
106 {
107 break;
108 }
109 else if (Name[Index] <= ' ' || Name[Index] >= 0x7F /* last printable ascii character */ || Name[Index] == ',')
110 {
111 // convert to underscore
112 Buffer[Index] = ' ';
113 }
114 else
115 {
116 // just copy character
118 }
119 }
120
121 return Index;
122}

Referenced by USBSTOR_PdoHandleQueryDeviceText().

◆ USBSTOR_ConvertToUnicodeString()

VOID USBSTOR_ConvertToUnicodeString ( IN CHAR Buffer,
IN ULONG  ResultBufferLength,
IN ULONG  ResultBufferOffset,
OUT LPWSTR  ResultBuffer,
OUT PULONG  NewResultBufferOffset 
)

Definition at line 232 of file pdo.c.

238{
239 UNICODE_STRING DeviceString;
242
243 ASSERT(ResultBufferLength);
244 ASSERT(ResultBufferLength > ResultBufferOffset);
245
246 DPRINT("ResultBufferOffset %lu ResultBufferLength %lu Buffer %s Length %lu\n", ResultBufferOffset, ResultBufferLength, Buffer, strlen(Buffer));
247
248 // construct destination string
249 DeviceString.Buffer = &ResultBuffer[ResultBufferOffset];
250 DeviceString.Length = 0;
251 DeviceString.MaximumLength = (ResultBufferLength - ResultBufferOffset) * sizeof(WCHAR);
252
253 // initialize source string
255
258
259 // subtract consumed bytes
260 ResultBufferLength -= (DeviceString.Length + sizeof(WCHAR)) / sizeof(WCHAR);
261 ResultBufferOffset += (DeviceString.Length + sizeof(WCHAR)) / sizeof(WCHAR);
262
263 *NewResultBufferOffset = ResultBufferOffset;
264}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
LONG NTSTATUS
Definition: precomp.h:26
#define FALSE
Definition: types.h:117
@ AnsiString
Definition: dnslib.h:19
Status
Definition: gdiplustypes.h:25
PKMT_RESULTBUFFER ResultBuffer
#define ASSERT(a)
Definition: mode.c:44
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:71
USHORT MaximumLength
Definition: env_spec_w32.h:370
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by USBSTOR_PdoHandleQueryCompatibleId(), and USBSTOR_PdoHandleQueryHardwareId().

◆ USBSTOR_CreatePDO()

NTSTATUS USBSTOR_CreatePDO ( IN PDEVICE_OBJECT  DeviceObject,
IN UCHAR  LUN 
)

Definition at line 841 of file pdo.c.

844{
845 PDEVICE_OBJECT PDO;
847 PPDO_DEVICE_EXTENSION PDODeviceExtension;
848 PFDO_DEVICE_EXTENSION FDODeviceExtension;
849 PINQUIRYDATA InquiryData;
850
851 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
852
853 // create child device object
855 if (!NT_SUCCESS(Status))
856 {
857 DPRINT1("Failed to create PDO, status %x\n", Status);
858 return Status;
859 }
860
861 // patch the stack size
862 PDO->StackSize = DeviceObject->StackSize;
863
864 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)PDO->DeviceExtension;
865 InquiryData = (PINQUIRYDATA)&PDODeviceExtension->InquiryData;
866
867 // initialize device extension
868 RtlZeroMemory(PDODeviceExtension, sizeof(PDO_DEVICE_EXTENSION));
869 PDODeviceExtension->Common.IsFDO = FALSE;
870 PDODeviceExtension->LowerDeviceObject = DeviceObject;
871 PDODeviceExtension->PDODeviceObject = &FDODeviceExtension->ChildPDO[LUN];
872 PDODeviceExtension->Self = PDO;
873 PDODeviceExtension->LUN = LUN;
874
875 PDO->Flags |= DO_DIRECT_IO;
876
877 // device is initialized
878 PDO->Flags &= ~DO_DEVICE_INITIALIZING;
879
880 // output device object
881 FDODeviceExtension->ChildPDO[LUN] = PDO;
882
883 // send inquiry command by irp
885
886 if (!NT_SUCCESS(Status))
887 {
888 return Status;
889 }
890
891 if (InquiryData->DeviceType != DIRECT_ACCESS_DEVICE &&
893 {
895 }
896
897 return Status;
898}
#define DPRINT1
Definition: precomp.h:8
#define DIRECT_ACCESS_DEVICE
Definition: cdrw_hw.h:1144
#define READ_ONLY_DIRECT_ACCESS_DEVICE
Definition: cdrw_hw.h:1149
#define FILE_DEVICE_SECURE_OPEN
Definition: cdrw_usr.h:46
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
struct _FDO_DEVICE_EXTENSION * PFDO_DEVICE_EXTENSION
struct _PDO_DEVICE_EXTENSION * PPDO_DEVICE_EXTENSION
#define DO_DIRECT_IO
Definition: env_spec_w32.h:396
#define FILE_DEVICE_MASS_STORAGE
Definition: imports.h:62
#define FILE_AUTOGENERATED_DEVICE_NAME
Definition: iotypes.h:138
NTSTATUS NTAPI IoCreateDevice(IN PDRIVER_OBJECT DriverObject, IN ULONG DeviceExtensionSize, IN PUNICODE_STRING DeviceName, IN DEVICE_TYPE DeviceType, IN ULONG DeviceCharacteristics, IN BOOLEAN Exclusive, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1031
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
PVOID DeviceExtension
Definition: env_spec_w32.h:418
UCHAR DeviceType
Definition: cdrw_hw.h:1116
COMMON_DEVICE_EXTENSION Common
Definition: pci.h:59
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
static NTSTATUS USBSTOR_FillInquiryData(IN PDEVICE_OBJECT PDODeviceObject)
Definition: pdo.c:801
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055

Referenced by USBSTOR_FdoHandleStartDevice().

◆ USBSTOR_FillInquiryData()

static NTSTATUS USBSTOR_FillInquiryData ( IN PDEVICE_OBJECT  PDODeviceObject)
static

Definition at line 801 of file pdo.c.

803{
805 PPDO_DEVICE_EXTENSION PDODeviceExtension = (PPDO_DEVICE_EXTENSION)PDODeviceObject->DeviceExtension;
806 CDB Cdb;
807 ULONG DataTransferLength = INQUIRYDATABUFFERSIZE;
808 PINQUIRYDATA InquiryData = (PINQUIRYDATA)&PDODeviceExtension->InquiryData;
809
810 RtlZeroMemory(&Cdb, sizeof(Cdb));
811 Cdb.CDB6INQUIRY.OperationCode = SCSIOP_INQUIRY;
812 Cdb.CDB6INQUIRY.AllocationLength = INQUIRYDATABUFFERSIZE;
813
814 Status = USBSTOR_SendInternalCdb(PDODeviceObject, &Cdb, CDB6GENERIC_LENGTH, 20, InquiryData, &DataTransferLength);
815
816 if (!NT_SUCCESS(Status))
817 {
818 DPRINT1("USBSTOR_FillInquiryData failed with %x\n", Status);
819 return Status;
820 }
821
822 DPRINT("DeviceType %x\n", InquiryData->DeviceType);
823 DPRINT("DeviceTypeModifier %x\n", InquiryData->DeviceTypeModifier);
824 DPRINT("RemovableMedia %x\n", InquiryData->RemovableMedia);
825 DPRINT("Version %x\n", InquiryData->Versions);
826 DPRINT("Format %x\n", InquiryData->ResponseDataFormat);
827 DPRINT("Length %x\n", InquiryData->AdditionalLength);
828 DPRINT("Reserved %p\n", InquiryData->Reserved);
829 DPRINT("VendorId %c%c%c%c%c%c%c%c\n", InquiryData->VendorId[0], InquiryData->VendorId[1], InquiryData->VendorId[2], InquiryData->VendorId[3], InquiryData->VendorId[4], InquiryData->VendorId[5], InquiryData->VendorId[6], InquiryData->VendorId[7]);
830 DPRINT("ProductId %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", InquiryData->ProductId[0], InquiryData->ProductId[1], InquiryData->ProductId[2], InquiryData->ProductId[3],
831 InquiryData->ProductId[4], InquiryData->ProductId[5], InquiryData->ProductId[6], InquiryData->ProductId[7],
832 InquiryData->ProductId[8], InquiryData->ProductId[9], InquiryData->ProductId[10], InquiryData->ProductId[11],
833 InquiryData->ProductId[12], InquiryData->ProductId[13], InquiryData->ProductId[14], InquiryData->ProductId[15]);
834
835 DPRINT("Revision %c%c%c%c\n", InquiryData->ProductRevisionLevel[0], InquiryData->ProductRevisionLevel[1], InquiryData->ProductRevisionLevel[2], InquiryData->ProductRevisionLevel[3]);
836
837 return Status;
838}
#define SCSIOP_INQUIRY
Definition: cdrw_hw.h:888
#define CDB6GENERIC_LENGTH
Definition: cdrw_hw.h:830
#define INQUIRYDATABUFFERSIZE
Definition: cdrw_hw.h:1113
_In_opt_ WDFREQUEST _In_ ULONG _In_ BOOLEAN _In_ PCDB Cdb
Definition: scratch.h:159
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
Definition: cdrw_hw.h:28
struct _CDB::_CDB6INQUIRY CDB6INQUIRY
static NTSTATUS USBSTOR_SendInternalCdb(IN PDEVICE_OBJECT PdoDevice, IN PCDB Cdb, IN UCHAR CdbLength, IN ULONG TimeOutValue, OUT PVOID OutDataBuffer, OUT PULONG OutDataTransferLength)
Definition: pdo.c:667

Referenced by USBSTOR_CreatePDO().

◆ USBSTOR_GetDeviceType()

static LPCSTR USBSTOR_GetDeviceType ( IN PINQUIRYDATA  InquiryData)
static

Definition at line 20 of file pdo.c.

22{
23 switch (InquiryData->DeviceType)
24 {
26 return "Disk";
28 // sequential device, i.e magnetic tape
29 return "Sequential";
31 return "Worm";
33 return "CdRom";
34 case OPTICAL_DEVICE:
35 return "Optical";
36 case MEDIUM_CHANGER:
37 return "Changer";
38 default:
39 return "Other";
40 }
41}
#define WRITE_ONCE_READ_MULTIPLE_DEVICE
Definition: cdrw_hw.h:1148
#define OPTICAL_DEVICE
Definition: cdrw_hw.h:1151
#define SEQUENTIAL_ACCESS_DEVICE
Definition: cdrw_hw.h:1145
#define MEDIUM_CHANGER
Definition: cdrw_hw.h:1152

Referenced by USBSTOR_PdoHandleQueryCompatibleId(), USBSTOR_PdoHandleQueryDeviceId(), and USBSTOR_PdoHandleQueryHardwareId().

◆ USBSTOR_GetGenericType()

static LPCSTR USBSTOR_GetGenericType ( IN PINQUIRYDATA  InquiryData)
static

Definition at line 45 of file pdo.c.

47{
48 switch (InquiryData->DeviceType)
49 {
51 return "GenDisk";
53 // sequential device, i.e magnetic tape
54 return "GenSequential";
56 return "GenWorm";
58 return "GenCdRom";
59 case OPTICAL_DEVICE:
60 return "GenOptical";
61 case MEDIUM_CHANGER:
62 return "GenChanger";
63 default:
64 return "UsbstorOther";
65 }
66}

Referenced by USBSTOR_PdoHandleQueryHardwareId().

◆ USBSTOR_PdoHandleDeviceRelations()

NTSTATUS USBSTOR_PdoHandleDeviceRelations ( IN PDEVICE_OBJECT  DeviceObject,
IN OUT PIRP  Irp 
)

Definition at line 477 of file pdo.c.

480{
481 PDEVICE_RELATIONS DeviceRelations;
482 PIO_STACK_LOCATION IoStack;
483
484 DPRINT("USBSTOR_PdoHandleDeviceRelations\n");
485
487
488 // check if relation type is BusRelations
489 if (IoStack->Parameters.QueryDeviceRelations.Type != TargetDeviceRelation)
490 {
491 // PDO handles only target device relation
492 return Irp->IoStatus.Status;
493 }
494
496 if (!DeviceRelations)
497 {
499 }
500
501 // initialize device relations
502 DeviceRelations->Count = 1;
503 DeviceRelations->Objects[0] = DeviceObject;
505
506 Irp->IoStatus.Information = (ULONG_PTR)DeviceRelations;
507 return STATUS_SUCCESS;
508}
_In_ PIRP Irp
Definition: csq.h:116
#define ULONG_PTR
Definition: config.h:101
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PagedPool
Definition: env_spec_w32.h:308
PDEVICE_OBJECT Objects[1]
Definition: iotypes.h:2163
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:3128
#define USB_STOR_TAG
Definition: usbstor.h:10
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2793
@ TargetDeviceRelation
Definition: iotypes.h:2156
#define ObReferenceObject
Definition: obfuncs.h:204

Referenced by USBSTOR_PdoHandlePnp().

◆ USBSTOR_PdoHandlePnp()

NTSTATUS USBSTOR_PdoHandlePnp ( IN PDEVICE_OBJECT  DeviceObject,
IN OUT PIRP  Irp 
)

Definition at line 511 of file pdo.c.

514{
515 PIO_STACK_LOCATION IoStack;
516 PPDO_DEVICE_EXTENSION DeviceExtension;
519 ULONG bDelete;
520
522 DeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
523 ASSERT(DeviceExtension->Common.IsFDO == FALSE);
524
525 switch(IoStack->MinorFunction)
526 {
528 {
530 break;
531 }
533 {
535 break;
536 }
537 case IRP_MN_QUERY_ID:
538 {
539 if (IoStack->Parameters.QueryId.IdType == BusQueryDeviceID)
540 {
542 break;
543 }
544 else if (IoStack->Parameters.QueryId.IdType == BusQueryHardwareIDs)
545 {
547 break;
548 }
549 else if (IoStack->Parameters.QueryId.IdType == BusQueryInstanceID)
550 {
552 break;
553 }
554 else if (IoStack->Parameters.QueryId.IdType == BusQueryCompatibleIDs)
555 {
557 break;
558 }
559
560 DPRINT1("USBSTOR_PdoHandlePnp: IRP_MN_QUERY_ID IdType %x unimplemented\n", IoStack->Parameters.QueryId.IdType);
562 Irp->IoStatus.Information = 0;
563 break;
564 }
566 {
567 DPRINT("IRP_MN_REMOVE_DEVICE\n");
568
569 if(*DeviceExtension->PDODeviceObject != NULL)
570 {
571 *DeviceExtension->PDODeviceObject = NULL;
572 bDelete = TRUE;
573 }
574 else
575 {
576 // device object already marked for deletion
577 bDelete = FALSE;
578 }
579
580 Irp->IoStatus.Status = STATUS_SUCCESS;
582
583 if (bDelete)
584 {
586 }
587 return STATUS_SUCCESS;
588 }
590 {
591 // just forward irp to lower device
593
594 if (IoForwardIrpSynchronously(DeviceExtension->LowerDeviceObject, Irp))
595 {
596 Status = Irp->IoStatus.Status;
598
599 if (NT_SUCCESS(Status))
600 {
601 // check if no unique id
602 Caps = (PDEVICE_CAPABILITIES)IoStack->Parameters.DeviceCapabilities.Capabilities;
603 Caps->UniqueID = FALSE; // no unique id is supported
604 Caps->Removable = TRUE; //FIXME
605 }
606 }
607 break;
608 }
611 {
612 if (DeviceExtension->Claimed)
613 {
615 DPRINT1("[USBSTOR] Request %x fails because device is still claimed\n", IoStack->MinorFunction);
616 }
617 else
619 break;
620 }
622 {
623 // no-op for PDO
625 break;
626 }
628 {
630 break;
631 }
632 default:
633 {
634 // do nothing
635 Status = Irp->IoStatus.Status;
636 }
637 }
638
639 if (Status != STATUS_PENDING)
640 {
641 Irp->IoStatus.Status = Status;
643 }
644
645 return Status;
646}
#define TRUE
Definition: types.h:120
#define IRP_MN_SURPRISE_REMOVAL
Definition: ntifs_ex.h:408
VOID NTAPI IoDeleteDevice(IN PDEVICE_OBJECT DeviceObject)
Definition: device.c:1251
#define IoCompleteRequest
Definition: irp.c:1240
BOOLEAN NTAPI IoForwardIrpSynchronously(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1625
#define STATUS_PENDING
Definition: ntstatus.h:82
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
NTSTATUS USBSTOR_PdoHandleQueryInstanceId(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:434
NTSTATUS USBSTOR_PdoHandleQueryCompatibleId(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:394
NTSTATUS USBSTOR_PdoHandleQueryDeviceId(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: pdo.c:179
NTSTATUS USBSTOR_PdoHandleQueryHardwareId(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:267
NTSTATUS USBSTOR_PdoHandleDeviceRelations(IN PDEVICE_OBJECT DeviceObject, IN OUT PIRP Irp)
Definition: pdo.c:477
NTSTATUS USBSTOR_PdoHandleQueryDeviceText(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: pdo.c:125
#define IO_NO_INCREMENT
Definition: iotypes.h:598
#define IRP_MN_START_DEVICE
#define IRP_MN_QUERY_ID
#define IRP_MN_REMOVE_DEVICE
#define IRP_MN_QUERY_DEVICE_RELATIONS
#define IRP_MN_QUERY_STOP_DEVICE
#define IRP_MN_QUERY_DEVICE_TEXT
#define IRP_MN_QUERY_CAPABILITIES
* PDEVICE_CAPABILITIES
Definition: iotypes.h:965
@ BusQueryCompatibleIDs
Definition: iotypes.h:2938
@ BusQueryInstanceID
Definition: iotypes.h:2939
@ BusQueryDeviceID
Definition: iotypes.h:2936
@ BusQueryHardwareIDs
Definition: iotypes.h:2937
#define IRP_MN_QUERY_REMOVE_DEVICE

Referenced by USBSTOR_DispatchPnp().

◆ USBSTOR_PdoHandleQueryCompatibleId()

NTSTATUS USBSTOR_PdoHandleQueryCompatibleId ( IN PDEVICE_OBJECT  DeviceObject,
IN OUT PIRP  Irp 
)

Definition at line 394 of file pdo.c.

397{
398 PPDO_DEVICE_EXTENSION PDODeviceExtension;
399 PFDO_DEVICE_EXTENSION FDODeviceExtension;
400 CHAR Buffer[100] = {0};
404
405 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
406 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)PDODeviceExtension->LowerDeviceObject->DeviceExtension;
407 ASSERT(FDODeviceExtension->DeviceDescriptor);
408 DeviceType = USBSTOR_GetDeviceType((PINQUIRYDATA)&PDODeviceExtension->InquiryData);
409
410 // format instance id
411 Length = sprintf(Buffer, "USBSTOR\\%s", DeviceType) + 1;
412 // +1 for terminating \0 and another +1 for \0 at the end of REG_MULTI_SZ
413 Length += sprintf(&Buffer[Length], "USBSTOR\\%s", "RAW") + 2;
414
416 if (!InstanceId)
417 {
418 Irp->IoStatus.Information = 0;
420 }
421
424
425 InstanceId[Offset] = UNICODE_NULL; // finish the REG_MULTI_SZ
426
427 DPRINT("USBSTOR_PdoHandleQueryCompatibleId %S\n", InstanceId);
428
429 Irp->IoStatus.Information = (ULONG_PTR)InstanceId;
430 return STATUS_SUCCESS;
431}
_Must_inspect_result_ _In_opt_ PVOID _In_opt_ PVOID InstanceId
Definition: fsrtlfuncs.h:908
DeviceType
Definition: mmdrv.h:42
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define UNICODE_NULL
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
VOID USBSTOR_ConvertToUnicodeString(IN CHAR *Buffer, IN ULONG ResultBufferLength, IN ULONG ResultBufferOffset, OUT LPWSTR ResultBuffer, OUT PULONG NewResultBufferOffset)
Definition: pdo.c:232
static LPCSTR USBSTOR_GetDeviceType(IN PINQUIRYDATA InquiryData)
Definition: pdo.c:20
const char * LPCSTR
Definition: xmlstorage.h:183
WCHAR * LPWSTR
Definition: xmlstorage.h:184
char CHAR
Definition: xmlstorage.h:175

Referenced by USBSTOR_PdoHandlePnp().

◆ USBSTOR_PdoHandleQueryDeviceId()

NTSTATUS USBSTOR_PdoHandleQueryDeviceId ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 179 of file pdo.c.

182{
183 PPDO_DEVICE_EXTENSION DeviceExtension;
185 CHAR Buffer[100] = {0};
187 ULONG Offset = 0;
188 PINQUIRYDATA InquiryData;
190 UNICODE_STRING DeviceId;
191
192 DeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
193 InquiryData = (PINQUIRYDATA)&DeviceExtension->InquiryData;
194
195 DeviceType = USBSTOR_GetDeviceType(InquiryData);
196
197 // lets create device string
198 Offset = sprintf(&Buffer[Offset], "USBSTOR\\");
200 Offset += sprintf(&Buffer[Offset], "&Ven_");
201 Offset += CopyField(InquiryData->VendorId, &Buffer[Offset], 8);
202 Offset += sprintf(&Buffer[Offset], "&Prod_");
203 Offset += CopyField(InquiryData->ProductId, &Buffer[Offset], 16);
204 Offset += sprintf(&Buffer[Offset], "&Rev_");
205 Offset += CopyField(InquiryData->ProductRevisionLevel, &Buffer[Offset], 4);
206
208
209 // allocate DeviceId string
210 DeviceId.Length = 0;
211 DeviceId.MaximumLength = (USHORT)((strlen((PCHAR)Buffer) + 1) * sizeof(WCHAR));
213 if (!DeviceId.Buffer)
214 {
215 Irp->IoStatus.Information = 0;
217 }
218
220
221 if (NT_SUCCESS(Status))
222 {
223 Irp->IoStatus.Information = (ULONG_PTR)DeviceId.Buffer;
224 }
225
226 DPRINT("DeviceId %wZ Status %x\n", &DeviceId, Status);
227
228 return Status;
229}
unsigned short USHORT
Definition: pedump.c:61
static ULONG CopyField(IN PUCHAR Name, IN PCHAR Buffer, IN ULONG MaxLength, IN CHAR DefaultCharacter, IN BOOLEAN Trim)
Definition: pdo.c:124
UCHAR ProductId[16]
Definition: cdrw_hw.h:1133
UCHAR ProductRevisionLevel[4]
Definition: cdrw_hw.h:1134
UCHAR VendorId[8]
Definition: cdrw_hw.h:1132
char * PCHAR
Definition: typedefs.h:51
CONST char * PCSZ
Definition: umtypes.h:125

Referenced by USBSTOR_PdoHandlePnp().

◆ USBSTOR_PdoHandleQueryDeviceText()

NTSTATUS USBSTOR_PdoHandleQueryDeviceText ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp 
)

Definition at line 125 of file pdo.c.

128{
129 PPDO_DEVICE_EXTENSION DeviceExtension;
130 PIO_STACK_LOCATION IoStack;
131 CHAR LocalBuffer[26];
132 UINT32 Offset = 0;
133 PINQUIRYDATA InquiryData;
136
138
139 DeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
140 InquiryData = (PINQUIRYDATA)&DeviceExtension->InquiryData;
141
142 switch (IoStack->Parameters.QueryDeviceText.DeviceTextType)
143 {
146 {
147 DPRINT("USBSTOR_PdoHandleQueryDeviceText\n");
148
149 Offset += CopyFieldTruncate(InquiryData->VendorId, &LocalBuffer[Offset], sizeof(InquiryData->VendorId));
150 LocalBuffer[Offset++] = ' ';
151 Offset += CopyFieldTruncate(InquiryData->ProductId, &LocalBuffer[Offset], sizeof(InquiryData->ProductId));
152 LocalBuffer[Offset++] = '\0';
153
154 RtlInitAnsiString(&AnsiString, (PCSZ)&LocalBuffer);
155
156 DeviceDescription.Length = 0;
157 DeviceDescription.MaximumLength = (USHORT)(Offset * sizeof(WCHAR));
159 if (!DeviceDescription.Buffer)
160 {
161 Irp->IoStatus.Information = 0;
163 }
164
166
167 Irp->IoStatus.Information = (ULONG_PTR)DeviceDescription.Buffer;
168 return STATUS_SUCCESS;
169 }
170 default:
171 {
172 Irp->IoStatus.Information = 0;
173 return Irp->IoStatus.Status;
174 }
175 }
176}
unsigned int UINT32
static ULONG CopyFieldTruncate(IN PUCHAR Name, IN PCHAR Buffer, IN ULONG MaxLength)
Definition: pdo.c:96
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_ PCUNICODE_STRING DeviceDescription
Definition: wdfpdo.h:432
@ DeviceTextLocationInformation
Definition: iotypes.h:2946
@ DeviceTextDescription
Definition: iotypes.h:2945

Referenced by USBSTOR_PdoHandlePnp().

◆ USBSTOR_PdoHandleQueryHardwareId()

NTSTATUS USBSTOR_PdoHandleQueryHardwareId ( IN PDEVICE_OBJECT  DeviceObject,
IN OUT PIRP  Irp 
)

Definition at line 267 of file pdo.c.

270{
271 PPDO_DEVICE_EXTENSION PDODeviceExtension;
272 PFDO_DEVICE_EXTENSION FDODeviceExtension;
273 LPCSTR GenericType, DeviceType;
275 CHAR Id1[50], Id2[50], Id3[50], Id4[50], Id5[50], Id6[50], Id7[50];
276 ULONG Id1Length, Id2Length, Id3Length, Id4Length, Id5Length, Id6Length, Id7Length;
278 PINQUIRYDATA InquiryData;
279
280 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
281 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)PDODeviceExtension->LowerDeviceObject->DeviceExtension;
282 ASSERT(FDODeviceExtension->DeviceDescriptor);
283 InquiryData = (PINQUIRYDATA)&PDODeviceExtension->InquiryData;
284
285 DeviceType = USBSTOR_GetDeviceType(InquiryData);
286 GenericType = USBSTOR_GetGenericType(InquiryData);
287
288 ASSERT(GenericType);
289
290 // generate id 1
291 // USBSTOR\SCSIType_VendorId(8)_ProductId(16)_Revision(4)
292 RtlZeroMemory(Id1, sizeof(Id1));
293 Offset = 0;
294 Offset = sprintf(&Id1[Offset], "USBSTOR\\");
295 Offset += sprintf(&Id1[Offset], DeviceType);
296 Offset += CopyField(InquiryData->VendorId, &Id1[Offset], 8);
297 Offset += CopyField(InquiryData->ProductId, &Id1[Offset], 16);
298 Offset += CopyField(InquiryData->ProductRevisionLevel, &Id1[Offset], 4);
299 Id1Length = strlen(Id1) + 1;
300 DPRINT("USBSTOR_PdoHandleQueryHardwareId HardwareId1 %s\n", Id1);
301
302 // generate id 2
303 // USBSTOR\SCSIType_VendorId(8)_ProductId(16)
304 RtlZeroMemory(Id2, sizeof(Id2));
305 Offset = 0;
306 Offset = sprintf(&Id2[Offset], "USBSTOR\\");
307 Offset += sprintf(&Id2[Offset], DeviceType);
308 Offset += CopyField(InquiryData->VendorId, &Id2[Offset], 8);
309 Offset += CopyField(InquiryData->ProductId, &Id2[Offset], 16);
310 Id2Length = strlen(Id2) + 1;
311 DPRINT("USBSTOR_PdoHandleQueryHardwareId HardwareId2 %s\n", Id2);
312
313 // generate id 3
314 // USBSTOR\SCSIType_VendorId(8)
315 RtlZeroMemory(Id3, sizeof(Id3));
316 Offset = 0;
317 Offset = sprintf(&Id3[Offset], "USBSTOR\\");
318 Offset += sprintf(&Id3[Offset], DeviceType);
319 Offset += CopyField(InquiryData->VendorId, &Id3[Offset], 8);
320 Id3Length = strlen(Id3) + 1;
321 DPRINT("USBSTOR_PdoHandleQueryHardwareId HardwareId3 %s\n", Id3);
322
323 // generate id 4
324 // USBSTOR\SCSIType_VendorId(8)_ProductId(16)_Revision(1)
325 RtlZeroMemory(Id4, sizeof(Id4));
326 Offset = 0;
327 Offset = sprintf(&Id4[Offset], "USBSTOR\\");
328 Offset += sprintf(&Id4[Offset], DeviceType);
329 Offset += CopyField(InquiryData->VendorId, &Id4[Offset], 8);
330 Offset += CopyField(InquiryData->ProductId, &Id4[Offset], 16);
331 Offset += CopyField(InquiryData->ProductRevisionLevel, &Id4[Offset], 1);
332 Id4Length = strlen(Id4) + 1;
333 DPRINT("USBSTOR_PdoHandleQueryHardwareId HardwareId4 %s\n", Id4);
334
335 // generate id 5
336 // SCSIType_VendorId(8)_ProductId(16)_Revision(1)
337 RtlZeroMemory(Id5, sizeof(Id5));
338 Offset = 0;
340 Offset += CopyField(InquiryData->VendorId, &Id5[Offset], 8);
341 Offset += CopyField(InquiryData->ProductId, &Id5[Offset], 16);
342 Offset += CopyField(InquiryData->ProductRevisionLevel, &Id5[Offset], 1);
343 Id5Length = strlen(Id5) + 1;
344 DPRINT("USBSTOR_PdoHandleQueryHardwareId HardwareId5 %s\n", Id5);
345
346 // generate id 6
347 // USBSTOR\SCSIType
348 RtlZeroMemory(Id6, sizeof(Id6));
349 Offset = 0;
350 Offset = sprintf(&Id6[Offset], "USBSTOR\\");
351 Offset += sprintf(&Id6[Offset], GenericType);
352 Id6Length = strlen(Id6) + 1;
353 DPRINT("USBSTOR_PdoHandleQueryHardwareId HardwareId6 %s\n", Id6);
354
355 // generate id 7
356 // SCSIType
357 RtlZeroMemory(Id7, sizeof(Id7));
358 Offset = 0;
359 Offset = sprintf(&Id7[Offset], GenericType);
360 Id7Length = strlen(Id7) + 1;
361 DPRINT("USBSTOR_PdoHandleQueryHardwareId HardwareId7 %s\n", Id7);
362
363 // last +1 is for terminating \0 of REG_MULTI_SZ
364 TotalLength = Id1Length + Id2Length + Id3Length + Id4Length + Id5Length + Id6Length + Id7Length + 1;
365
367 if (!Buffer)
368 {
369 Irp->IoStatus.Information = 0;
371 }
372
373 // reset offset
374 Offset = 0;
376
384
385 Buffer[Offset] = UNICODE_NULL; // finish the REG_MULTI_SZ
386
387 ASSERT(Offset + 1 == Length);
388
389 Irp->IoStatus.Information = (ULONG_PTR)Buffer;
390 return STATUS_SUCCESS;
391}
struct _INQUIRYDATA * PINQUIRYDATA
static LPCSTR USBSTOR_GetGenericType(IN PINQUIRYDATA InquiryData)
Definition: pdo.c:45
_In_ ULONG TotalLength
Definition: usbdlib.h:158

Referenced by USBSTOR_PdoHandlePnp().

◆ USBSTOR_PdoHandleQueryInstanceId()

NTSTATUS USBSTOR_PdoHandleQueryInstanceId ( IN PDEVICE_OBJECT  DeviceObject,
IN OUT PIRP  Irp 
)

Definition at line 434 of file pdo.c.

437{
438 PPDO_DEVICE_EXTENSION PDODeviceExtension;
439 PFDO_DEVICE_EXTENSION FDODeviceExtension;
440 WCHAR Buffer[100];
443
444 PDODeviceExtension = (PPDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
445 FDODeviceExtension = (PFDO_DEVICE_EXTENSION)PDODeviceExtension->LowerDeviceObject->DeviceExtension;
446
447 // format instance id
448 if (FDODeviceExtension->SerialNumber)
449 {
450 // using serial number from device
451 swprintf(Buffer, L"%s&%c", FDODeviceExtension->SerialNumber->bString, PDODeviceExtension->LUN);
452 }
453 else
454 {
455 // use instance count and LUN
456 swprintf(Buffer, L"%04lu&%c", FDODeviceExtension->InstanceCount, PDODeviceExtension->LUN);
457 }
458
459 Length = wcslen(Buffer) + 1;
460
462 if (!InstanceId)
463 {
464 Irp->IoStatus.Information = 0;
466 }
467
469
470 DPRINT("USBSTOR_PdoHandleQueryInstanceId %S\n", InstanceId);
471
472 Irp->IoStatus.Information = (ULONG_PTR)InstanceId;
473 return STATUS_SUCCESS;
474}
#define swprintf
Definition: precomp.h:40
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define L(x)
Definition: ntvdm.h:50
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)

Referenced by USBSTOR_PdoHandlePnp().

◆ USBSTOR_SendInternalCdb()

static NTSTATUS USBSTOR_SendInternalCdb ( IN PDEVICE_OBJECT  PdoDevice,
IN PCDB  Cdb,
IN UCHAR  CdbLength,
IN ULONG  TimeOutValue,
OUT PVOID  OutDataBuffer,
OUT PULONG  OutDataTransferLength 
)
static

Definition at line 667 of file pdo.c.

674{
676 PSENSE_DATA SenseBuffer;
677 PIO_STACK_LOCATION IoStack;
679 PIRP Irp = NULL;
680 PMDL Mdl = NULL;
681 ULONG ix = 0;
683 UCHAR SrbStatus;
684
685 DPRINT("USBSTOR_SendInternalCdb SCSIOP %x\n", Cdb->CDB6GENERIC.OperationCode);
686
688 sizeof(SCSI_REQUEST_BLOCK),
690
691 if (Srb)
692 {
696
697 if (SenseBuffer)
698 {
699 Mdl = IoAllocateMdl(OutDataBuffer,
700 *OutDataTransferLength,
701 FALSE,
702 FALSE,
703 NULL);
704
705 if (!Mdl)
706 {
707 ExFreePoolWithTag(SenseBuffer, USB_STOR_TAG);
709 return Status;
710 }
711
713
714 // make 3 attempts - the device may be in STALL state after the first one
715 do
716 {
717 Irp = IoAllocateIrp(PdoDevice->StackSize, FALSE);
718
719 if (!Irp)
720 {
721 break;
722 }
723
726 IoStack->Parameters.Scsi.Srb = Srb;
727
729
730 Srb->Length = sizeof(SCSI_REQUEST_BLOCK);
732 Srb->CdbLength = CdbLength;
735 Srb->DataTransferLength = *OutDataTransferLength;
736 Srb->TimeOutValue = TimeOutValue;
737 Srb->DataBuffer = OutDataBuffer;
738 Srb->SenseInfoBuffer = SenseBuffer;
739
740 RtlCopyMemory(Srb->Cdb, Cdb, CdbLength);
741
742 Irp->MdlAddress = Mdl;
743
745
748 &Event,
749 TRUE,
750 TRUE,
751 TRUE);
752
753 if (IoCallDriver(PdoDevice, Irp) == STATUS_PENDING)
754 {
756 Executive,
758 FALSE,
759 NULL);
760 }
761
762 SrbStatus = SRB_STATUS(Srb->SrbStatus);
763
764 IoFreeIrp(Irp);
765 Irp = NULL;
766
767 if (SrbStatus == SRB_STATUS_SUCCESS ||
768 SrbStatus == SRB_STATUS_DATA_OVERRUN)
769 {
771 *OutDataTransferLength = Srb->DataTransferLength;
772 break;
773 }
774
776
777 ++ix;
778 } while (ix < 3);
779
780 if (Mdl)
781 {
782 IoFreeMdl(Mdl);
783 }
784
785 ExFreePoolWithTag(SenseBuffer, USB_STOR_TAG);
786 }
787
789 }
790
791 return Status;
792}
_In_ PSCSI_REQUEST_BLOCK Srb
Definition: cdrom.h:989
#define SENSE_BUFFER_SIZE
Definition: cdrw_hw.h:1183
struct _SCSI_REQUEST_BLOCK SCSI_REQUEST_BLOCK
#define SRB_FUNCTION_EXECUTE_SCSI
Definition: srb.h:315
#define SRB_STATUS_DATA_OVERRUN
Definition: srb.h:357
#define SRB_FLAGS_DATA_IN
Definition: srb.h:400
#define SRB_STATUS(Status)
Definition: srb.h:389
#define SRB_FLAGS_NO_QUEUE_FREEZE
Definition: srb.h:404
#define SRB_STATUS_SUCCESS
Definition: srb.h:341
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define NonPagedPool
Definition: env_spec_w32.h:307
#define IoFreeMdl
Definition: fxmdl.h:89
#define IoAllocateMdl
Definition: fxmdl.h:88
#define IoSetCompletionRoutine(_Irp, _CompletionRoutine, _Context, _InvokeOnSuccess, _InvokeOnError, _InvokeOnCancel)
Definition: irp.cpp:498
VOID NTAPI MmBuildMdlForNonPagedPool(IN PMDL Mdl)
Definition: mdlsup.c:424
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define KernelMode
Definition: asm.h:34
@ SynchronizationEvent
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
#define IoCallDriver
Definition: irp.c:1225
VOID NTAPI IoFreeIrp(IN PIRP Irp)
Definition: irp.c:1666
ULONG TimeOutValue
Definition: srb.h:262
UCHAR SenseInfoBufferLength
Definition: srb.h:259
PVOID DataBuffer
Definition: srb.h:263
UCHAR CdbLength
Definition: srb.h:258
UCHAR Cdb[16]
Definition: srb.h:279
PVOID SenseInfoBuffer
Definition: srb.h:264
UCHAR Function
Definition: srb.h:250
ULONG DataTransferLength
Definition: srb.h:261
ULONG SrbFlags
Definition: srb.h:260
USHORT Length
Definition: srb.h:249
UCHAR SrbStatus
Definition: srb.h:251
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
struct _CDB::_CDB6GENERIC CDB6GENERIC
NTSTATUS NTAPI USBSTOR_SyncCompletionRoutine(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Ctx)
Definition: pdo.c:650
_In_ WDFDEVICE _In_ PVOID _In_opt_ PMDL Mdl
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2695
#define IRP_MJ_INTERNAL_DEVICE_CONTROL
@ Executive
Definition: ketypes.h:403
unsigned char UCHAR
Definition: xmlstorage.h:181

Referenced by USBSTOR_FillInquiryData().

◆ USBSTOR_SyncCompletionRoutine()

NTSTATUS NTAPI USBSTOR_SyncCompletionRoutine ( IN PDEVICE_OBJECT  DeviceObject,
IN PIRP  Irp,
IN PVOID  Ctx 
)

Definition at line 650 of file pdo.c.

654{
657}
#define KeSetEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:476
#define STATUS_MORE_PROCESSING_REQUIRED
Definition: shellext.h:68

Referenced by USBSTOR_SendInternalCdb().