From: Maxim S. Shatskih on
> instead of looking at the driver name (in USB_Disk) of the PDO to determine
> if it is a usb disk, get the enumerator name by calling
> IoGetDeviceProperty(DevicePropertyEnumeratorName) and looks for "USBSTOR" or
> "USB" depending on which usbstor devobj you are filtering

IOCTL_STORAGE_QUERY_PROPERTY for the bus type is another possibility.

--
Maxim S. Shatskih
Windows DDK MVP
maxim(a)storagecraft.com
http://www.storagecraft.com

From: Daniel Zhang on
On Jan 17, 2:21 am, "Doron Holan [MSFT]" <dor...(a)online.microsoft.com>
wrote:
> instead of looking at the driver name (in USB_Disk) of the PDO to determine
> if it is a usb disk, get the enumerator name by calling
> IoGetDeviceProperty(DevicePropertyEnumeratorName) and looks for "USBSTOR" or
> "USB" depending on which usbstor devobj you are filtering
>
> http://msdn.microsoft.com/en-us/library/ms801223.aspx
>
> d
>
> --
> Please do not send e-mail directly to this alias. this alias is for
> newsgroup purposes only.
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
> "Daniel Zhang" <fanz...(a)gmail.com> wrote in message
>
> news:5240f735-2ee7-4979-91a8-0e31fd94e7e8(a)p2g2000prf.googlegroups.com...
>
> > Wayne,Thank you for your help. But I think I have already passed all
> > the irp to the lower driver. Through the infomation from the core dump
> > file, I felt that the system was not crashed at my filter driver.
> > Below is my code,could you please find some error for me?
>
> > #include "stddcls.h"
> > #include "driver.h"
>
> > #include <srb.h>
> > #include <scsi.h>
>
> > NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT
> > pdo);
> > VOID DriverUnload(IN PDRIVER_OBJECT fido);
> > NTSTATUS DispatchAny(IN PDEVICE_OBJECT fido, IN PIRP Irp);
> > NTSTATUS DispatchPower(IN PDEVICE_OBJECT fido, IN PIRP Irp);
> > NTSTATUS DispatchPnp(IN PDEVICE_OBJECT fido, IN PIRP Irp);
> > NTSTATUS DispatchWmi(IN PDEVICE_OBJECT fido, IN PIRP Irp);
> > ULONG GetDeviceTypeToUse(PDEVICE_OBJECT pdo);
> > NTSTATUS StartDeviceCompletionRoutine(PDEVICE_OBJECT fido, PIRP Irp,
> > PDEVICE_EXTENSION pdx);
> > NTSTATUS UsageNotificationCompletionRoutine(PDEVICE_OBJECT fido, PIRP
> > Irp, PDEVICE_EXTENSION pdx);
>
> > ///////////////////////////////////////////////////////////////////////////////
> > #pragma INITCODE
> > extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,
> > IN PUNICODE_STRING RegistryPath)
> > { // DriverEntry
> > KdPrint((DRIVERNAME " - Entering DriverEntry: DriverObject %8.8lX\n",
> > DriverObject));
>
> > // Initialize function pointers
> > DriverObject->DriverUnload = DriverUnload;
> > DriverObject->DriverExtension->AddDevice = AddDevice;
> > for (int i = 0; i < arraysize(DriverObject->MajorFunction); ++i)
> > DriverObject->MajorFunction[i] = DispatchAny;
> > DriverObject->MajorFunction[IRP_MJ_POWER] = DispatchPower;
> > DriverObject->MajorFunction[IRP_MJ_PNP] = DispatchPnp;
> > DriverObject->MajorFunction[IRP_MJ_SCSI] = DispatchForSCSI;
>
> > return STATUS_SUCCESS;
> > } // DriverEntry
>
> > #pragma PAGEDCODE
> > VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
> > { // DriverUnload
> > PAGED_CODE();
> > KdPrint((DRIVERNAME " - Entering DriverUnload: DriverObject %8.8lX
> > \n", DriverObject));
> > } // DriverUnload
>
> > BOOLEAN USB_Disk(IN PDEVICE_OBJECT pDevice)
> > {
> > UNICODE_STRING usb_disk_name;
> > RtlInitUnicodeString(&usb_disk_name,L"\\driver\\usbstor");
>
> > PDRIVER_OBJECT pDriver = pDevice->DriverObject;
> > KdPrint(("driver name:%wZ\n",&pDriver->DriverName));
> > if (!RtlCompareUnicodeString(&pDriver-
> >>DriverName,&usb_disk_name,TRUE))
> > {
> > KdPrint(("Find a USB disk device!\n"));
> > return TRUE;
> > }
> > KdPrint(("This is not a USB disk device!"));
> > return FALSE;
> > }
>
> > NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT
> > pdo)
> > { // AddDevice
> > PAGED_CODE();
> > NTSTATUS status;
>
> > PDEVICE_OBJECT fido;
>
> > if (!USB_Disk(pdo))
> > {
> > return STATUS_SUCCESS;
> > }
>
> > status = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION), NULL,
> > GetDeviceTypeToUse(pdo), 0, FALSE, &fido);
> > if (!NT_SUCCESS(status))
> > { // can't create device object
> > KdPrint((DRIVERNAME " - IoCreateDevice failed - %X\n", status));
> > return status;
> > } // can't create device object
> > PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;
>
> > do
> > { // finish initialization
> > IoInitializeRemoveLock(&pdx->RemoveLock, 0, 0, 0);
> > pdx->DeviceObject = fido;
> > pdx->Pdo = pdo;
> > //½«¹ýÂËÇý¶¯¸½¼ÓÔڵײãÇý¶¯Ö®ÉÏ
> > PDEVICE_OBJECT fdo = IoAttachDeviceToDeviceStack(fido, pdo);
> > if (!fdo)
> > { // can't attach
> > KdPrint((DRIVERNAME " - IoAttachDeviceToDeviceStack failed\n"));
> > status = STATUS_DEVICE_REMOVED;
> > break;
> > } // can't attach
> > //¼Ç¼µ×²ãÇý¶¯
> > pdx->LowerDeviceObject = fdo;
> > //ÓÉÓÚ²»ÖªµÀµ×²ãÇý¶¯ÊÇÖ±½ÓIO»¹ÊÇBufferIO£¬Òò´Ë½«±êÖ¾¶¼ÖÃÉÏ
> > fido->Flags |= fdo->Flags & (DO_DIRECT_IO | DO_BUFFERED_IO |
> > DO_POWER_PAGABLE);
> > // Clear the "initializing" flag so that we can get IRPs
> > fido->Flags &= ~DO_DEVICE_INITIALIZING;
> > } while (FALSE); // finish initialization
>
> > if (!NT_SUCCESS(status))
> > { // need to cleanup
> > if (pdx->LowerDeviceObject)
> > IoDetachDevice(pdx->LowerDeviceObject);
> > IoDeleteDevice(fido);
> > } // need to cleanup
>
> > return status;
> > } // AddDevice
>
> > ///////////////////////////////////////////////////////////////////////////////
> > #pragma LOCKEDCODE
> > NTSTATUS CompleteRequest(IN PIRP Irp, IN NTSTATUS status, IN ULONG_PTR
> > info)
> > { // CompleteRequest
> > Irp->IoStatus.Status = status;
> > Irp->IoStatus.Information = info;
> > IoCompleteRequest(Irp, IO_NO_INCREMENT);
> > return status;
> > } // CompleteRequest
>
> > NTSTATUS
> > USBSCSICompletion( IN PDEVICE_OBJECT DeviceObject,
> > IN PIRP Irp,
> > IN PVOID Context )
> > {
> > PDEVICE_EXTENSION pdx = ( PDEVICE_EXTENSION )
> > DeviceObject->DeviceExtension;
>
> > IoAcquireRemoveLock(&pdx->RemoveLock,Irp);
>
> > PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation( Irp );
>
> > PSCSI_REQUEST_BLOCK CurSrb=irpStack->Parameters.Scsi.Srb;
> > PCDB cdb = (PCDB)CurSrb->Cdb;
> > UCHAR opCode=cdb->CDB6GENERIC.OperationCode;
>
> > if(opCode==SCSIOP_MODE_SENSE && CurSrb->DataBuffer
> > && CurSrb->DataTransferLength >=
> > sizeof(MODE_PARAMETER_HEADER))
> > {
> > KdPrint(("SCSIOP_MODE_SENSE comming!\n"));
>
> > PMODE_PARAMETER_HEADER modeData = (PMODE_PARAMETER_HEADER)CurSrb-
> >>DataBuffer;
>
> > modeData->DeviceSpecificParameter |= MODE_DSP_WRITE_PROTECT;
> > }
>
> > if ( Irp->PendingReturned )
> > {
> > IoMarkIrpPending( Irp );
> > }
>
> > IoReleaseRemoveLock(&pdx->RemoveLock,Irp);
>
> > return Irp->IoStatus.Status ;
> > }
>
> > #pragma LOCKEDCODE
> > NTSTATUS DispatchForSCSI(IN PDEVICE_OBJECT fido, IN PIRP Irp)
> > {
> > // KdPrint((DRIVERNAME " - Enter DispatchForSCSI \n"));
>
> > PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;
>
> > PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
>
> > ULONG IoControlCode = irpStack-
> >>Parameters.DeviceIoControl.IoControlCode;
>
> > KdPrint((DRIVERNAME " - Enter DispatchForSCSI :%x\n",IoControlCode));
>
> > // Pass request down without additional processing
> > NTSTATUS status;
> > status = IoAcquireRemoveLock(&pdx->RemoveLock, Irp);
> > if (!NT_SUCCESS(status))
> > return CompleteRequest(Irp, status, 0);
>
> > IoCopyCurrentIrpStackLocationToNext(Irp);
>
> > IoSetCompletionRoutine( Irp,
> > USBSCSICompletion,
> > NULL,
> > TRUE,
> > TRUE,
> > TRUE );
> > status = IoCallDriver(pdx->LowerDeviceObject, Irp);
> > IoReleaseRemoveLock(&pdx->RemoveLock, Irp);
> > return status;
> > }
> > ///////////////////////////////////////////////////////////////////////////////
> > #pragma LOCKEDCODE // make no assumptions about pageability of
> > dispatch fcns
> > NTSTATUS DispatchAny(IN PDEVICE_OBJECT fido, IN PIRP Irp)
> > { // DispatchAny
> > PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;
> > PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
> > #if DBG
> > static char* irpname[] =
> > {
> > "IRP_MJ_CREATE",
> > "IRP_MJ_CREATE_NAMED_PIPE",
> > "IRP_MJ_CLOSE",
> > "IRP_MJ_READ",
> > "IRP_MJ_WRITE",
> > "IRP_MJ_QUERY_INFORMATION",
> > "IRP_MJ_SET_INFORMATION",
> > "IRP_MJ_QUERY_EA",
> > "IRP_MJ_SET_EA",
> > "IRP_MJ_FLUSH_BUFFERS",
> > "IRP_MJ_QUERY_VOLUME_INFORMATION",
> > "IRP_MJ_SET_VOLUME_INFORMATION",
> > "IRP_MJ_DIRECTORY_CONTROL",
> > "IRP_MJ_FILE_SYSTEM_CONTROL",
> > "IRP_MJ_DEVICE_CONTROL",
> > "IRP_MJ_INTERNAL_DEVICE_CONTROL",
> > "IRP_MJ_SHUTDOWN",
> > "IRP_MJ_LOCK_CONTROL",
> > "IRP_MJ_CLEANUP",
> > "IRP_MJ_CREATE_MAILSLOT",
> > "IRP_MJ_QUERY_SECURITY",
> > "IRP_MJ_SET_SECURITY",
> > "IRP_MJ_POWER",
> > "IRP_MJ_SYSTEM_CONTROL",
> > "IRP_MJ_DEVICE_CHANGE",
> > "IRP_MJ_QUERY_QUOTA",
> > "IRP_MJ_SET_QUOTA",
> > "IRP_MJ_PNP",
> > };
>
> > UCHAR type = stack->MajorFunction;
> > // if (type >= arraysize(irpname))
> > // KdPrint((DRIVERNAME " - Unknown IRP, major type %X\n", type));
> > // else
> > // KdPrint((DRIVERNAME " - %s\n", irpname[type]));
>
> > #endif
>
> > // Pass request down without additional processing
> > NTSTATUS status;
> > status = IoAcquireRemoveLock(&pdx->RemoveLock, Irp);
> > if (!NT_SUCCESS(status))
> > return CompleteRequest(Irp, status, 0);
> > IoSkipCurrentIrpStackLocation(Irp);
> > status = IoCallDriver(pdx->LowerDeviceObject, Irp);
> > IoReleaseRemoveLock(&pdx->RemoveLock, Irp);
> > return status;
> > } // DispatchAny
>
> > ///////////////////////////////////////////////////////////////////////////////
> > NTSTATUS DispatchPower(IN PDEVICE_OBJECT fido, IN PIRP Irp)
> > { // DispatchPower
> > #if DBG
> > PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
> > ULONG fcn = stack->MinorFunction;
> > static char* fcnname[] =
> > {
> > "IRP_MN_WAIT_WAKE",
> > "IRP_MN_POWER_SEQUENCE",
> > "IRP_MN_SET_POWER",
> > "IRP_MN_QUERY_POWER",
> > };
>
> > if (fcn == IRP_MN_SET_POWER || fcn == IRP_MN_QUERY_POWER)
> > {
> > static char* sysstate[] =
> > {
> > "PowerSystemUnspecified",
> > "PowerSystemWorking",
> > "PowerSystemSleeping1",
> > "PowerSystemSleeping2",
> > "PowerSystemSleeping3",
> > "PowerSystemHibernate",
> > "PowerSystemShutdown",
> > "PowerSystemMaximum",
> > };
>
> > static char* devstate[] =
> > {
> > "PowerDeviceUnspecified",
> > "PowerDeviceD0",
> > "PowerDeviceD1",
> > "PowerDeviceD2",
> > "PowerDeviceD3",
> > "PowerDeviceMaximum",
> > };
>
> > ULONG context = stack->Parameters.Power.SystemContext;
> > POWER_STATE_TYPE type = stack->Parameters.Power.Type;
> > KdPrint((DRIVERNAME " - IRP_MJ_POWER (%s)", fcnname[fcn]));
> > if (type == SystemPowerState)
> > KdPrint((", SystemPowerState = %s\n", sysstate[stack-
> >>Parameters.Power.State.SystemState]));
> > else
> > KdPrint((", DevicePowerState = %s\n", devstate[stack-
> >>Parameters.Power.State.DeviceState]));
> > }
> > else if (fcn < arraysize(fcnname))
> > KdPrint((DRIVERNAME " - IRP_MJ_POWER
>
> ...
>
> read more >>

Thank you for your help.
Even though I return STATUS_UNSUCCESSFUL in AddDevice function like
below. The system also crashed.
If anyone have a standard filter driver example which can be
lowerfilter with disk class.
NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT
pdo)
{ // AddDevice
return STATUS_UNSUCCESSFUL;
}
From: Daniel Zhang on
Thank you for your help.
Even though I return STATUS_UNSUCCESSFUL in AddDevice function like
below. The system also crashed.
If anyone have a standard filter driver example which can be
lowerfilter with disk class.
NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT
pdo)
{ // AddDevice
return STATUS_UNSUCCESSFUL;
}
From: Doron Holan [MSFT] on
return STATUS_SUCCESS without attaching

d

--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.



"Daniel Zhang" <fanzier(a)gmail.com> wrote in message
news:a9b266df-270a-4412-9489-2fac4ea505ce(a)t26g2000prh.googlegroups.com...
> Thank you for your help.
> Even though I return STATUS_UNSUCCESSFUL in AddDevice function like
> below. The system also crashed.
> If anyone have a standard filter driver example which can be
> lowerfilter with disk class.
> NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT
> pdo)
> { // AddDevice
> return STATUS_UNSUCCESSFUL;
> }

From: Daniel Zhang on
I just return STATUS_SUCCESS without attaching,but the system also
crashed.

On Jan 21, 7:28 am, "Doron Holan [MSFT]" <dor...(a)online.microsoft.com>
wrote:
> return STATUS_SUCCESS without attaching