KMDF PCI Bus Filter Driver

I want to write a PCI bus filter driver. To read PCI config space and manipulate some registers. I've seen many post prior on this topi here. now I am little confuse, I want to know is it possible to write a PCI bus filter driver KMDF way??
because I have written a filter driver to PCI but unable to install it for *PNP0a03, may be I've not written the INF correctly.
can some one please help.
"
[Version]
Signature=“$WINDOWS NT$”
Class=System
ClassGuid={4D36E97D-E325-11CE-BFC1-08002BE10318}
Provider=%TEST%
CatalogFile=PCIFltr.cat
DriverVer = 07/14/2025,1.0.0.1
PnpLockdown=1

;*************************
; Source file information
;*************************

[SourceDisksNames]
1 = %DiskName%,“”

[SourceDisksFiles]
PCIFltr.sys = 1,

[DestinationDirs]
DefaultDestDir = 12 ;system32/drivers

[ControlFlags]
ExcludeFromSelect=*

;*****************************************
; filter Install Section
;*****************************************

[Manufacturer]
%TEST%=Standard,NTamd64

[Standard.NTamd64]
%filter.DeviceDesc%=FilterInstall, *PNP0a03

[FilterInstall]
Include=machine.inf
Needs=PCI_DRV
CopyFiles=FilterInstall.Copy

[FilterInstall.Copy]
PCIFltr.sys

[FilterInstall.HW]
Include=machine.inf
Needs=PCI_DRV.HW
AddReg=FilterInstall.HW.AddReg

[FilterInstall.HW.AddReg]
HKR,“UpperFilters”,0x00010008,“PCIFltr”

[FilterInstall.Services]
;Do not specify SPSVCINST_ASSOCSERVICE on filter drivers.
Include=machine.inf
Needs=PCI_DRV.Services
AddService=PCIFltr,ufilter_Service_Inst

[ufilter_Service_Inst]
DisplayName = %ufilter.SvcName%
Description = %ufilter.SvcDesc%
ServiceType = 1 ; SERVICE_KERNEL_DRIVER
StartType = 3 ; SERVICE_DEMAND_START
ErrorControl = 1 ; SERVICE_ERROR_NORMAL
ServiceBinary = %12%\PCIFltr.sys
LoadOrderGroup = “PnP Filter”

[Strings]
TEST=“Storage Systems Inc.”
DiskName = “Filter Installation Disk”
filter.DeviceDesc = “PCI Upstream Port”
ufilter.SvcName = “PCI Upper Filter”
ufilter.SvcDesc = “PCI Upper Filter Service”

It has been a very long time since I've run my pci bus filter, but at the time it was PCI_DRV_ROOT, not PCI_DRV that you needed to include.

1 Like

“Thanks for clarifying that, Mark. I was wondering the same thing about PCI_DRV vs PCI_DRV_ROOT. So to confirm, the INF should use PCI_DRV_ROOT in the Include and Needs entries instead of PCI_DRV for a PCI bus filter?”

1 Like

Yes replace all the PCI_DRV entries.

Thank you Mark for pointing out the mistake.
After fix, the driver is installing successfully.
In my AddDevice Routine I am trying to fetch the capabilityPtr from
DECLSPEC_ALIGN(MEMORY_ALLOCATION_ALIGNMENT)
UCHAR buffer[sizeof(PCI_COMMON_CONFIG)];
PPCI_COMMON_CONFIG pPciConfig = (PPCI_COMMON_CONFIG)buffer;
NTSTATUS Status = STATUS_SUCCESS;

RtlZeroMemory(buffer, sizeof(buffer));

// 1) Read the first pointer at 0x34
bytesRead = DeviceContext->PciBusInterface.GetBusData(  DeviceContext->PciBusInterface.Context,
                                                        PCI_WHICHSPACE_CONFIG,
                                                        &buffer,
                                                        FIELD_OFFSET(PCI_COMMON_CONFIG, u.type0.CapabilitiesPtr),
                                                        0xe2);
if (bytesRead != sizeof(PCI_COMMON_CONFIG)) {
    KdPrint(("Failed to read capability list pointer: %u bytes\n", bytesRead));
    return STATUS_INVALID_DEVICE_REQUEST;
}

but the call always returned with 0, bytes why?
is it not possible in KMDF ? if yes then why if not shall I write a WDM for this?
the similar thing I can see works perfectly fine in MSDN PCI sample for NIC.

Thank you for the help.

You're asking to read 0xE2 bytes. The capabilities pointer is at offset 0x34. 0x34 + 0xE2 is 0x116, which is beyond the size of the configuration data.

I suspect you only wanted 4 bytes here.

1 Like