Remove device in Usb filter driver

I am writing a USB upper filter driver.
If I plug a Smart card reader or a dongle to USB connector, the filter driver gets the following PNP Irp:
IRP_MN_QUERY_DEVICE_RELATIONS (Parameters.QueryDeviceRelations.Type = BusRelations) and then
IRP_MJ_POWER

In IRP_MN_QUERY_DEVICE_RELATIONS
I use IoSetCompletionRoutine and in competion routine
get pointer (PDEVICE_OBJECT) to new object device(pNewDev)

In IRP_MJ_POWER
By using function IoGetDeviceAttachmentBaseRef(pNewDev), IoGetDeviceProperty ()
I can get PID and VID of the inserted device.
Now I want to disable the device (or remove) for predefined VID and PID.
I did not succeeded doing it.

I tried to do it in three different ways:

  1. IoDeleteDevice.
  2. InvalidateDeviceRelations.,
  3. IoBuildSynchronousFsdReques(IRP_MJ_PNP ) MinorFunction = IRP_MN_REMOVE_DEVICE
    IoCallDriver(pNewDev, irp );
    IoCallDriver return STATUS_SUCCESS.

But device is not removed and I can still see the smart card reader in the device manager.
The Device manger can disable device.
How can I disable (remove) device in filter driver?

Thanks for any help
Best regards
mark

You can’t delete pnp device objects when you want. You can only do so at the right time. You can’t send pnp state changing requests (that includes pnp remove) from your driver, only the pnp manager is allowed to do that.

If you want the device to disappear from device manager completely, you need to remove it from the list of device objects reported in the device relations (and ObDeref it since it was added with a ref). this assumes you can query the vid/pid from the device object at QDR/BusRelations time.

If you want the FDO not to load, you should alter the reported hardware IDs so there is no INF match to load the driver

If it is OK for the FDO to load and then fail (and shows up as !'ed out in device manager), fail IRP_MN_START_DEVICE, which will make the device non functional and subsequently unload the FDO.

d

From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Mark Shnaider
Sent: Thursday, September 10, 2015 7:04 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Remove device in Usb filter driver

I am writing a USB upper filter driver.
If I plug a Smart card reader or a dongle to USB connector, the filter driver gets the following PNP Irp:
IRP_MN_QUERY_DEVICE_RELATIONS (Parameters.QueryDeviceRelations.Type = BusRelations) and then
IRP_MJ_POWER

In IRP_MN_QUERY_DEVICE_RELATIONS
I use IoSetCompletionRoutine and in competion routine
get pointer (PDEVICE_OBJECT) to new object device(pNewDev)

In IRP_MJ_POWER
By using function IoGetDeviceAttachmentBaseRef(pNewDev), IoGetDeviceProperty ()
I can get PID and VID of the inserted device.
Now I want to disable the device (or remove) for predefined VID and PID.
I did not succeeded doing it.

I tried to do it in three different ways:

1. IoDeleteDevice.
2. InvalidateDeviceRelations.,
3. IoBuildSynchronousFsdReques(IRP_MJ_PNP ) MinorFunction = IRP_MN_REMOVE_DEVICE
IoCallDriver(pNewDev, irp );
IoCallDriver return STATUS_SUCCESS.

But device is not removed and I can still see the smart card reader in the device manager.
The Device manger can disable device.
How can I disable (remove) device in filter driver?

Thanks for any help
Best regards
mark


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Mark Shnaider wrote:

I am writing a USB upper filter driver.

Upper filter driver to what, specifically? To USB hubs?

In IRP_MN_QUERY_DEVICE_RELATIONS

I use IoSetCompletionRoutine and in competion routine

get pointer (PDEVICE_OBJECT) to new object device(pNewDev)

Now, do you understand that the response to a BusRelations request
includes ALL of the child devices of that bus? It doesn’t just include
new devices. PnP figures out which devices are new and which are
dropped by comparing this list to the previous answer.

In IRP_MJ_POWER

By using function IoGetDeviceAttachmentBaseRef(pNewDev),
IoGetDeviceProperty ()

I can get PID and VID of the inserted device.

You shouldn’t need IoGetDeviceAttachmentBaseRef. What you are receiving
here is a PDO – it already is the bottom of the stack. It’s a little
dangerous to extract the VID and PID from the hardware ID; it’s true
that the rules for forming user Hardware IDs have never changed, but the
format is not guaranteed. Also, some of the USB emulators (like
USB-over-Ethernet) don’t follow the rules exactly. It may be more
reliable to go read the Device Descriptor.

Now I want to disable the device (or remove) for predefined VID and
PID.

I did not succeeded doing it.

I tried to do it in three different ways:

  1. IoDeleteDevice.

This will cause a blue screen, because you don’t own the device object.
The device object is owned by the hub, and he still thinks the object is
alive.

  1. InvalidateDeviceRelations.,

This won’t do any good, because the hub still thinks the device is alive
and working.

  1. IoBuildSynchronousFsdReques(IRP_MJ_PNP ) MinorFunction =
    IRP_MN_REMOVE_DEVICE

IoCallDriver(pNewDev, irp );

IoCallDriver return STATUS_SUCCESS.

You can’t do that because you are not PnP. PnP requests have to be fed
in from the top of the stack and managed properly. Plus, as soon as you
remove the device, the bus is going to notice that it’s back again, and
will recreate it.

But device is not removed and I can still see the smart card reader in
the device manager.

The Device manger can disable device.

How can I disable (remove) device in filter driver?

You can’t. You just aren’t in the right position to force that kind of
a change. It is the responsibility of the driver to expose a device’s
functions, not to establish policy about what’s allowed and what is not.

The disable/enable status is a user-mode thing. You could write a
Windows service that uses RegisterDeviceNotification to watch for new
USB devices to arrive. When you see one arrive, you can use the SetupDi
APIs to disable that device.

By the way, this is exactly the kind of thing that can be controlled by
Windows group policy. There are tens of thousands of options with very
fine-grained control over what operations are allowed and what are not.
You might pick up one of the books on group policy.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

I would add to this: there is also IoInvalidateDeviceState which allows the devnode to commit suicide without user mode involvement.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com
“Doron Holan” wrote in message news:xxxxx@ntdev…
You can’t delete pnp device objects when you want. You can only do so at the right time. You can’t send pnp state changing requests (that includes pnp remove) from your driver, only the pnp manager is allowed to do that.

If you want the device to disappear from device manager completely, you need to remove it from the list of device objects reported in the device relations (and ObDeref it since it was added with a ref). this assumes you can query the vid/pid from the device object at QDR/BusRelations time.

If you want the FDO not to load, you should alter the reported hardware IDs so there is no INF match to load the driver

If it is OK for the FDO to load and then fail (and shows up as !'ed out in device manager), fail IRP_MN_START_DEVICE, which will make the device non functional and subsequently unload the FDO.

d

From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Mark Shnaider
Sent: Thursday, September 10, 2015 7:04 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Remove device in Usb filter driver

I am writing a USB upper filter driver.

If I plug a Smart card reader or a dongle to USB connector, the filter driver gets the following PNP Irp:

IRP_MN_QUERY_DEVICE_RELATIONS (Parameters.QueryDeviceRelations.Type = BusRelations) and then

IRP_MJ_POWER

In IRP_MN_QUERY_DEVICE_RELATIONS

I use IoSetCompletionRoutine and in competion routine

get pointer (PDEVICE_OBJECT) to new object device(pNewDev)

In IRP_MJ_POWER

By using function IoGetDeviceAttachmentBaseRef(pNewDev), IoGetDeviceProperty ()

I can get PID and VID of the inserted device.

Now I want to disable the device (or remove) for predefined VID and PID.

I did not succeeded doing it.

I tried to do it in three different ways:

1. IoDeleteDevice.

2. InvalidateDeviceRelations.,

3. IoBuildSynchronousFsdReques(IRP_MJ_PNP ) MinorFunction = IRP_MN_REMOVE_DEVICE

IoCallDriver(pNewDev, irp );

IoCallDriver return STATUS_SUCCESS.

But device is not removed and I can still see the smart card reader in the device manager.

The Device manger can disable device.

How can I disable (remove) device in filter driver?

Thanks for any help

Best regards

mark


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

That only works after the stack has been started

From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Thursday, September 10, 2015 2:09 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Remove device in Usb filter driver

I would add to this: there is also IoInvalidateDeviceState which allows the devnode to commit suicide without user mode involvement.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.commailto:xxxxx
http://www.storagecraft.com
“Doron Holan” > wrote in message news:xxxxx@ntdev…
You can’t delete pnp device objects when you want. You can only do so at the right time. You can’t send pnp state changing requests (that includes pnp remove) from your driver, only the pnp manager is allowed to do that.

If you want the device to disappear from device manager completely, you need to remove it from the list of device objects reported in the device relations (and ObDeref it since it was added with a ref). this assumes you can query the vid/pid from the device object at QDR/BusRelations time.

If you want the FDO not to load, you should alter the reported hardware IDs so there is no INF match to load the driver

If it is OK for the FDO to load and then fail (and shows up as !'ed out in device manager), fail IRP_MN_START_DEVICE, which will make the device non functional and subsequently unload the FDO.

d

From: xxxxx@lists.osr.commailto:xxxxx [mailto:xxxxx@lists.osr.com] On Behalf Of Mark Shnaider
Sent: Thursday, September 10, 2015 7:04 AM
To: Windows System Software Devs Interest List >
Subject: [ntdev] Remove device in Usb filter driver

I am writing a USB upper filter driver.
If I plug a Smart card reader or a dongle to USB connector, the filter driver gets the following PNP Irp:
IRP_MN_QUERY_DEVICE_RELATIONS (Parameters.QueryDeviceRelations.Type = BusRelations) and then
IRP_MJ_POWER

In IRP_MN_QUERY_DEVICE_RELATIONS
I use IoSetCompletionRoutine and in competion routine
get pointer (PDEVICE_OBJECT) to new object device(pNewDev)

In IRP_MJ_POWER
By using function IoGetDeviceAttachmentBaseRef(pNewDev), IoGetDeviceProperty ()
I can get PID and VID of the inserted device.
Now I want to disable the device (or remove) for predefined VID and PID.
I did not succeeded doing it.

I tried to do it in three different ways:

1. IoDeleteDevice.
2. InvalidateDeviceRelations.,
3. IoBuildSynchronousFsdReques(IRP_MJ_PNP ) MinorFunction = IRP_MN_REMOVE_DEVICE
IoCallDriver(pNewDev, irp );
IoCallDriver return STATUS_SUCCESS.

But device is not removed and I can still see the smart card reader in the device manager.
The Device manger can disable device.
How can I disable (remove) device in filter driver?

Thanks for any help
Best regards
mark


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</mailto:xxxxx></mailto:xxxxx>

Hello,
Doron and Tim thanks for your explanations
My driver - USB class upper filter driver
Class {36FC9E60-C465-11CF-8056-444553540000}

If part of the usb device (dongle, smart card reader) is pluged, the filter driver gets only
IRP_MN_QUERY_DEVICE_RELATIONS((Parameters.QueryDeviceRelations.Type = BusRelations)
IRP_MJ_POWER
In the competion routine for IRP_MN_QUERY_DEVICE_RELATIONS for new insertion device
IoGetDeviceProperty return status STATUS_INVALID_DEVICE_REQUEST (0xC0000010L)
In this case I cannot recognize VID and PID and remove device from the list of device object.

For example: a dongle is inserted before reboot and after reboot I insert a smart card reader,
IoGetDeviceProperty return status SUCCESS for the dongle device, and return status STATUS_INVALID_DEVICE_REQUEST for the smart card reader device.

Best regards
Mark

From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Doron Holan
Sent: Friday, September 11, 2015 02:24
To: Windows System Software Devs Interest List
Subject: RE: Re:[ntdev] Remove device in Usb filter driver

That only works after the stack has been started

From: xxxxx@lists.osr.commailto:xxxxx [mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Thursday, September 10, 2015 2:09 PM
To: Windows System Software Devs Interest List >
Subject: Re:[ntdev] Remove device in Usb filter driver

I would add to this: there is also IoInvalidateDeviceState which allows the devnode to commit suicide without user mode involvement.


Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.commailto:xxxxx
http://www.storagecraft.com
“Doron Holan” > wrote in message news:xxxxx@ntdev…
You can’t delete pnp device objects when you want. You can only do so at the right time. You can’t send pnp state changing requests (that includes pnp remove) from your driver, only the pnp manager is allowed to do that.

If you want the device to disappear from device manager completely, you need to remove it from the list of device objects reported in the device relations (and ObDeref it since it was added with a ref). this assumes you can query the vid/pid from the device object at QDR/BusRelations time.

If you want the FDO not to load, you should alter the reported hardware IDs so there is no INF match to load the driver

If it is OK for the FDO to load and then fail (and shows up as !'ed out in device manager), fail IRP_MN_START_DEVICE, which will make the device non functional and subsequently unload the FDO.

d

From: xxxxx@lists.osr.commailto:xxxxx [mailto:xxxxx@lists.osr.com] On Behalf Of Mark Shnaider
Sent: Thursday, September 10, 2015 7:04 AM
To: Windows System Software Devs Interest List >
Subject: [ntdev] Remove device in Usb filter driver

I am writing a USB upper filter driver.
If I plug a Smart card reader or a dongle to USB connector, the filter driver gets the following PNP Irp:
IRP_MN_QUERY_DEVICE_RELATIONS (Parameters.QueryDeviceRelations.Type = BusRelations) and then
IRP_MJ_POWER

In IRP_MN_QUERY_DEVICE_RELATIONS
I use IoSetCompletionRoutine and in competion routine
get pointer (PDEVICE_OBJECT) to new object device(pNewDev)

In IRP_MJ_POWER
By using function IoGetDeviceAttachmentBaseRef(pNewDev), IoGetDeviceProperty ()
I can get PID and VID of the inserted device.
Now I want to disable the device (or remove) for predefined VID and PID.
I did not succeeded doing it.

I tried to do it in three different ways:

1. IoDeleteDevice.
2. InvalidateDeviceRelations.,
3. IoBuildSynchronousFsdReques(IRP_MJ_PNP ) MinorFunction = IRP_MN_REMOVE_DEVICE
IoCallDriver(pNewDev, irp );
IoCallDriver return STATUS_SUCCESS.

But device is not removed and I can still see the smart card reader in the device manager.
The Device manger can disable device.
How can I disable (remove) device in filter driver?

Thanks for any help
Best regards
mark


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</mailto:xxxxx></mailto:xxxxx></mailto:xxxxx>

Mark Shnaider wrote:

My driver - USB class upper filter driver

Class {36FC9E60-C465-11CF-8056-444553540000}

It is a common misconception that this means “all USB devices”. It does
not. That represents all of the devices in Device Manager listed under
“Universal Serial Bus controllers”. That means USB host controllers and
USB hubs.

If part of the usb device (dongle, smart card reader) is pluged, the
filter driver gets only

IRP_MN_QUERY_DEVICE_RELATIONS((Parameters.QueryDeviceRelations.Type =
BusRelations)

IRP_MJ_POWER

In the competion routine for IRP_MN_QUERY_DEVICE_RELATIONS for new
insertion device

IoGetDeviceProperty return status STATUS_INVALID_DEVICE_REQUEST
(0xC0000010L)

In this case I cannot recognize VID and PID and remove device from the
list of device object.

What property are you fetching?

If you have a multifunction device – meaning a device with several
interfaces – the hub sees that as one device, so you’ll only get one
QueryDeviceRelations call. The driver that gets loaded to handle that
device is likely to create several more device objects, one for each
interface, but those devices are in a different class, so your filter
will not be involved.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.