Files
punktfunk/packaging/windows/drivers/pf-vdisplay/src/entry.rs
T
enricobuehler 8bf40bfb2a fix(windows-drivers): STEP 3 DONE — IddCx adapter inits on-glass (Status=OK)
The all-Rust wdk-sys IddCx driver now initializes its adapter on the RTX box:
IddCxAdapterInitAsync -> 0x0, EvtIddCxAdapterInitFinished fires, device Status=OK.

ROOT CAUSE (found via cdb wt-trace of iddcx!IddCxImplAdapterInitAsync + the upstream
virtual-display-rs source): IDDCX_ENDPOINT_DIAGNOSTIC_INFO.GammaSupport was left zeroed
= IDDCX_FEATURE_IMPLEMENTATION_UNINITIALIZED (0), which the framework adapter validator
(ddivalidation.cpp:797) rejects with STATUS_INVALID_PARAMETER. Must be NONE (1).

Also required (matched to the proven-working upstream virtual-display-rs, installed +
verified Status=OK on the same box):
- caps Flags = NONE (SDR). CAN_PROCESS_FP16 needs a newer contract than UmdfExtensions=
  IddCx0102 grants; deferred to STEP 7 (HDR).
- SDR config: only the 7 required callbacks (+ DeviceIoControl for the proto control
  plane). The *2/gamma/HDR-metadata/query-target-info callbacks are FP16-obligated and
  rejected without FP16 caps; they return in STEP 7.
- device WDF context type on WdfDeviceCreate; adapter WDF context type on the init attrs.

Debugging note: cdb is reliable via live-attach (go-file gate to avoid the
IsDebuggerPresent race) but cdb -z static hangs on the VM; iddcx WPP needs the control
GUID (TMF GUIDs are not it). Diagnostics trimmed; log.rs dbglog kept for STEP 4+.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 22:39:49 +00:00

129 lines
6.8 KiB
Rust

//! DriverEntry + driver_add — the IddCx device bring-up (STEP 2/3). wdk-build links the UMDF
//! `WdfDriverStubUm` whose `FxDriverEntryUm` forwards to the exported `DriverEntry`. Adapter creation is
//! deferred to the first `EvtDeviceD0Entry` (STEP 3); monitors are created on demand by the control
//! plane (STEP 4). Instrumented with `dbglog!` for on-glass bring-up.
use wdk_iddcx::nt_success;
use wdk_sys::{
call_unsafe_wdf_function_binding, iddcx, GUID, NTSTATUS, PCUNICODE_STRING, PDRIVER_OBJECT,
PWDFDEVICE_INIT, ULONG, WDFDEVICE, WDFDRIVER, WDF_DRIVER_CONFIG, WDF_NO_HANDLE,
WDF_NO_OBJECT_ATTRIBUTES, WDF_PNPPOWER_EVENT_CALLBACKS,
};
use crate::{callbacks, size, STATUS_NOT_FOUND};
/// A WDF device context, attached to the WDFDEVICE at WdfDeviceCreate. The working virtual-display-rs +
/// oracle both create the device with a context-typed `DeviceContext` (we previously passed
/// WDF_NO_OBJECT_ATTRIBUTES). `WDF_OBJECT_CONTEXT_TYPE_INFO` holds raw pointers (Sync wrapper for the
/// `static`); `UniqueType` self-references per `WDF_DECLARE_CONTEXT_TYPE`.
#[repr(C)]
struct DeviceContext {
_device: WDFDEVICE,
}
#[repr(transparent)]
struct DevCtxInfo(wdk_sys::WDF_OBJECT_CONTEXT_TYPE_INFO);
// SAFETY: immutable 'static type metadata; the inner raw pointers are 'static and never written.
unsafe impl Sync for DevCtxInfo {}
static DEVICE_CTX: DevCtxInfo = DevCtxInfo(wdk_sys::WDF_OBJECT_CONTEXT_TYPE_INFO {
Size: core::mem::size_of::<wdk_sys::WDF_OBJECT_CONTEXT_TYPE_INFO>() as u32,
ContextName: c"PfVdDeviceCtx".as_ptr().cast(),
ContextSize: core::mem::size_of::<DeviceContext>(),
UniqueType: &DEVICE_CTX.0,
EvtDriverGetUniqueContextType: None,
});
#[unsafe(export_name = "DriverEntry")]
pub unsafe extern "system" fn driver_entry(
driver: PDRIVER_OBJECT,
registry_path: PCUNICODE_STRING,
) -> NTSTATUS {
dbglog!("[pf-vd] DriverEntry");
// SAFETY: zeroed then Size + the device-add callback set, per the WDF_DRIVER_CONFIG contract.
let mut config: WDF_DRIVER_CONFIG = unsafe { core::mem::zeroed() };
config.Size = core::mem::size_of::<WDF_DRIVER_CONFIG>() as ULONG;
config.EvtDriverDeviceAdd = Some(driver_add);
// SAFETY: driver + registry_path are loader-provided; config is valid for the call.
let st = unsafe {
call_unsafe_wdf_function_binding!(
WdfDriverCreate,
driver,
registry_path,
WDF_NO_OBJECT_ATTRIBUTES,
&mut config,
WDF_NO_HANDLE.cast::<WDFDRIVER>()
)
};
dbglog!("[pf-vd] WdfDriverCreate -> {st:#x}");
st
}
extern "C" fn driver_add(_driver: WDFDRIVER, mut init: PWDFDEVICE_INIT) -> NTSTATUS {
dbglog!("[pf-vd] driver_add");
// Defer adapter creation to the first D0 entry.
let mut pnp: WDF_PNPPOWER_EVENT_CALLBACKS = unsafe { core::mem::zeroed() };
pnp.Size = core::mem::size_of::<WDF_PNPPOWER_EVENT_CALLBACKS>() as ULONG;
pnp.EvtDeviceD0Entry = Some(callbacks::device_d0_entry);
// SAFETY: init is the framework-provided device-init; pnp is valid for the call.
unsafe {
call_unsafe_wdf_function_binding!(WdfDeviceInitSetPnpPowerEventCallbacks, init, &mut pnp);
}
// Build + size the IddCx client config (versioned size) and wire the 14 callbacks.
let Some(cfg_size) = size::idd_cx_client_config_size() else {
dbglog!("[pf-vd] config size unavailable");
return STATUS_NOT_FOUND;
};
let mut cfg: iddcx::IDD_CX_CLIENT_CONFIG = unsafe { core::mem::zeroed() };
cfg.Size = cfg_size;
cfg.EvtIddCxAdapterInitFinished = Some(callbacks::adapter_init_finished);
cfg.EvtIddCxParseMonitorDescription = Some(callbacks::parse_monitor_description);
cfg.EvtIddCxMonitorGetDefaultDescriptionModes = Some(callbacks::monitor_get_default_modes);
cfg.EvtIddCxMonitorQueryTargetModes = Some(callbacks::monitor_query_modes);
cfg.EvtIddCxAdapterCommitModes = Some(callbacks::adapter_commit_modes);
// SDR config — matches the working upstream virtual-display-rs. The *2 / gamma / HDR-metadata /
// query-target-info callbacks are FP16-OBLIGATED: registering them while caps declare no FP16 makes the
// framework's adapter Validate reject (ddivalidation.cpp:797 -> INVALID_PARAMETER). They return with
// FP16 caps under a higher UmdfExtensions binding in STEP 7 (HDR).
cfg.EvtIddCxMonitorAssignSwapChain = Some(callbacks::assign_swap_chain);
cfg.EvtIddCxMonitorUnassignSwapChain = Some(callbacks::unassign_swap_chain);
cfg.EvtIddCxDeviceIoControl = Some(callbacks::device_io_control);
// SAFETY: init is the framework device-init; cfg is fully populated + sized. (Links IddCxStub.)
let status = unsafe { wdk_iddcx::IddCxDeviceInitConfig(init, &cfg) };
dbglog!("[pf-vd] IddCxDeviceInitConfig -> {status:#x}");
if !nt_success(status) {
return status;
}
let mut device: WDFDEVICE = core::ptr::null_mut();
// Attach a device context type (like the working virtual-display-rs/oracle), not WDF_NO_OBJECT_ATTRIBUTES.
let mut dev_attr: wdk_sys::WDF_OBJECT_ATTRIBUTES = unsafe { core::mem::zeroed() };
dev_attr.Size = core::mem::size_of::<wdk_sys::WDF_OBJECT_ATTRIBUTES>() as u32;
dev_attr.ExecutionLevel = wdk_sys::_WDF_EXECUTION_LEVEL::WdfExecutionLevelInheritFromParent;
dev_attr.SynchronizationScope =
wdk_sys::_WDF_SYNCHRONIZATION_SCOPE::WdfSynchronizationScopeInheritFromParent;
dev_attr.ContextTypeInfo = &DEVICE_CTX.0;
// SAFETY: init configured above; dev_attr is a valid context-typed attributes block.
let status = unsafe {
call_unsafe_wdf_function_binding!(WdfDeviceCreate, &mut init, &mut dev_attr, &mut device)
};
dbglog!("[pf-vd] WdfDeviceCreate -> {status:#x}");
if !nt_success(status) {
return status;
}
// IddCx must be initialized on the device BEFORE other device setup (the canonical IddCx sample order).
// We previously created the device interface first — which can leave IddCx not fully ready by D0Entry,
// making IddCxAdapterInitAsync reject (INVALID_PARAMETER) despite byte-perfect caps.
// DIAGNOSTIC (STEP 3): the WdfDeviceCreateDeviceInterface call is REMOVED. Upstream virtual-display-rs
// and the MS IddCx sample create NO device interface — IddCx's control channel is EvtIddCxDeviceIoControl
// (already registered in the config). A non-IddCx device interface registered on the IddCx/IndirectKmd
// stack is a prime suspect for IddCxAdapterInitAsync -> INVALID_PARAMETER. If removing it fixes adapter
// init, the proto control plane moves to EvtIddCxDeviceIoControl (STEP 4) instead of a device interface.
let _ = pf_vdisplay_proto::interface_guid_fields; // keep the dep referenced
// SAFETY: device is the just-created WDFDEVICE.
let status = unsafe { wdk_iddcx::IddCxDeviceInitialize(device) };
dbglog!("[pf-vd] IddCxDeviceInitialize -> {status:#x}");
status
}