PNP driver type recommendation

Hello. So we have a PCI device that supports PNP, so the user will insert/eject the device many times during the running session of the OS.

So I’ve been testing with root bus toaster, what’s recommended for the driver associated with this HWID, to be a functional driver right, or one of the filter drivers in the samples? Due to 3rd party nature of the service which will need to be associated with the INF. Another part of the requirement, will be to completely stop/cleanup the service on device removal. Such that when the device is re-plugged in, the service can start as a fresh, so to do this, I assume the driver also needs to be fully unloaded as well right, for the INF to start the service again on device insertion?

I might be doing something silly, but using the MS driver toaster sample “featured2” functional driver, I never see the IRP_MN_SURPRISE_REMOVAL or the IRP_MN_REMOVE_DEVICE, is it supposed to? Once I use the enum.exe -u commands, the output looks like below. The setup is loading the “dynambus” as the rootbus, then using “featured2” as the device that will be associated with the real HWID.

// enum.exe -p 1234 ...
IRP_MN_??? Unknown
*** IopQueryLegacyBusInformation - Driver TestBusDrv returned STATUS_SUCCESS
    for IRP_MN_QUERY_LEGACY_BUS_INFORMATION, and a NULL POINTER.
IRP_MN_QUERY_RESOURCE_REQUIREMENTS
IRP_MN_FILTER_RESOURCE_REQUIREMENTS
IRP_MN_START_DEVICE
[!] BusEnum: PDO IRP_MN_START_DEVICE IRP: 0xFFFF9382CB978530
IRP_MN_QUERY_CAPABILITIES
//enum.exe -u 1234
[!] BusEnum: Create 
[!] BusEnum: UnPlug called
[!] BusEnum: Plugging out 1234
// No PNP IRP from the functional driver for removal, but the bus driver sees the event

Does your device have its own subdevices that come and go, or does your device plug and unplug from PCIe? PCIe hotplug not only requires support from the device, but support from the BIOS, and very, very few PCIExpress BIOSes support PCIe hotplug.

@Tim_Roberts said:
Does your device have its own subdevices that come and go, or does your device plug and unplug from PCIe? PCIe hotplug not only requires support from the device, but support from the BIOS, and very, very few PCIExpress BIOSes support PCIe hotplug.

No the actual device doesn’t have subdevices (though the MS toaster sample come under there own class tree). It plugs/unplugs as you mentioned. And yes we do have support from BIOS. At the moment, the devices appears in device manager under system devices and we have control to influence the hwid/class etc if need be.

So to receive the IRP_MN_SURPRISE_REMOVAL, using the MS toaster. I’ve tried both the KMDF (modern driver samples) and WDM (DDK 7) toasters, and neither are showing the removal when using the provided enum.exe -e nnn.
“\Program Files (x86)\Windows Kits\10\tools\x64\devcon.exe” install busenum.inf root\busenum"
pnputil /add-driver featured2.inf /install
enum.exe -p 123
enum.exe -u 123
//no surprise removal irp received in featured2 functional driver, should I try using the filter drivers instead?

Okay I resolved the missing PNP IRP, from looking at random code online. Uhhh MS driver samples are missing the key IoSkipCurrentIrpStackLocation and IoCallDriver(nextDriver), in the IRP_MN_START_DEVICE, so I presume not correctly completing in that doesn’t then register you to receive the IRP removal related events.

So back the original question then.

I now have a driver that can capture the removal. And the INF that is associated with it, contains a service along with the driver. So when the real device is unplugged, the service needs to be restarted (do I need to fully unload the driver as well, or will just the AddDevice() be called again (but not DriverEntry), based on the image below). Such that the INF when loaded again, it starts both the service and driver. Or will it handle only loading one, if the other is present?

Advice for today: Don’t use WDM. I’ve written a metric shit-ton of WDM drivers, and PnP code… and I wouldn’t write your driver using WDM. Why would you?

I suggest you put the restart logic in the service itself. It can register for device interface change notification and restart itself… or whatever.

Peter