Problems on WdfDeviceCreate

Hello,

I am writing a WDF PCI driver. It worked well in the past, but recently I need to get informed in the driver when the customer opens and closes the file handle. So I added the following code in my driver before calling WdfDeviceCreate. But after the following code is added, WdfDeviceCreate returns an error code:STATUS_INVALID_DEVICE_REQUEST.

WDF_FILEOBJECT_CONFIG_INIT(&fileConfig, OnFileCreate, OnFileClose, NULL);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&fileAttribute, FileContext);
WdfDeviceInitSetFileObjectConfig(DeviceInit, &fileConfig, &fileAttribute);

After some google, I changed the argument to WdfDeviceCreate from WdfSynchronizationScopeDevice to WdfSynchronizationScopeNone. It worked again.

Now, I am confused the difference between WdfSynchronizationScopeNone and WdfSynchronizationScopeDevice. Will WdfSynchronizationScopeNone cause other potential problems.

Thanks in advance.

The documentation talks about this, although it is admittedly a bit opaque.

Sounds like you might be doing more than you need to. This is all I have in my PCI device driver. Are your OnFileCreate and OnFileClose functions are declared correctly?

NTSTATUS
UnifiedCreateDevice(
    _Inout_ PWDFDEVICE_INIT DeviceInit
)
{
    WDF_OBJECT_ATTRIBUTES deviceAttributes;
    PDEVICE_CONTEXT deviceContext;
    WDFDEVICE device;
    NTSTATUS status;
    WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
    WDF_OBJECT_ATTRIBUTES fileAttributes;
    WDF_FILEOBJECT_CONFIG fileObjectConfig;
    ULONG buffer = 0;

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "%!FUNC! Entry");

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, DEVICE_CONTEXT);
    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&fileAttributes, FILE_CONTEXT);

    WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
    pnpPowerCallbacks.EvtDevicePrepareHardware = UnifiedPrepareHardware;
    pnpPowerCallbacks.EvtDeviceReleaseHardware = UnifiedReleaseHardware;
    WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);

    WDF_FILEOBJECT_CONFIG_INIT(&fileObjectConfig, nullptr, UnifiedFileClose, nullptr);
    WdfDeviceInitSetFileObjectConfig(DeviceInit, &fileObjectConfig, &fileAttributes);

    status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device);
    ...
}

Thanks GrimBeaver for sharing your code snippet, that’s helpful context.

I’m wondering if the issue seablue1023 hit is specifically tied to synchronization scope and how it interacts with file object callbacks. From what I’ve seen in the docs, using WdfSynchronizationScopeDevice causes the framework to serialize all I/O queues and file object callbacks at the device level. So if the callbacks (like OnFileCreate) try to access resources that might be involved in other I/O paths, it could introduce conflicts that don’t occur under WdfSynchronizationScopeNone.

@seablue1023 — are your OnFileCreate and OnFileClose callbacks doing anything that might acquire locks or reference objects shared elsewhere in the driver? That could explain why switching to WdfSynchronizationScopeNone seems to “fix” it. But you’d need to make sure your callbacks themselves handle synchronization if you go that route.

Curious if anyone else has run into specific issues combining file object callbacks with WdfSynchronizationScopeDevice?

Ask ChatGPT