Skip to content

Commit d486d52

Browse files
JohnAZoidbergclaude
andcommitted
Fix device cleanup to run on device disable
The cleanup callback was incorrectly registered on the driver object instead of the device object. This meant that when disabling the device in Device Manager, FrameworkArgbEvtDriverContextCleanup was never called (it only runs on driver unload, not device disable). Additionally, the driver cleanup was calling GetDeviceContext() on a WDFDRIVER object, which is incorrect - that function is for WDFDEVICE. Changes: - Add FrameworkArgbEvtDeviceCleanup callback for device-level cleanup - Register it on the device attributes in FrameworkArgbCreateDevice - Move EC handle cleanup from driver to device cleanup - Keep only WPP tracing cleanup in driver cleanup This fixes issues when disabling the device while Windows Dynamic Lighting is using it (timer keeps firing, reboot popup, etc). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 907af91 commit d486d52

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

FrameworkArgb/Device.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ Return Value:
314314
WdfFdoInitSetFilter(DeviceInit);
315315

316316
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, DEVICE_CONTEXT);
317+
deviceAttributes.EvtCleanupCallback = FrameworkArgbEvtDeviceCleanup;
317318

318319
status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device);
319320

@@ -735,3 +736,39 @@ Return Value:
735736

736737
return status;
737738
}
739+
740+
VOID
741+
FrameworkArgbEvtDeviceCleanup(
742+
_In_ WDFOBJECT DeviceObject
743+
)
744+
/*++
745+
Routine Description:
746+
747+
Free all the resources allocated for the device.
748+
Called when the device is removed or disabled.
749+
750+
Arguments:
751+
752+
DeviceObject - handle to a WDF Device object.
753+
754+
Return Value:
755+
756+
VOID.
757+
758+
--*/
759+
{
760+
PDEVICE_CONTEXT DeviceContext;
761+
762+
TraceInformation("%!FUNC! Entry");
763+
764+
DeviceContext = GetDeviceContext(DeviceObject);
765+
766+
// Close handle to EC driver
767+
if (DeviceContext && DeviceContext->CrosEcHandle && DeviceContext->CrosEcHandle != INVALID_HANDLE_VALUE) {
768+
CloseHandle(DeviceContext->CrosEcHandle);
769+
DeviceContext->CrosEcHandle = INVALID_HANDLE_VALUE;
770+
TraceInformation("%!FUNC! Closed CrosEc Handle");
771+
}
772+
773+
TraceInformation("%!FUNC! Exit");
774+
}

FrameworkArgb/Device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef UCHAR HID_REPORT_DESCRIPTOR, * PHID_REPORT_DESCRIPTOR;
2727
DRIVER_INITIALIZE DriverEntry;
2828
EVT_WDF_DRIVER_DEVICE_ADD EvtDeviceAdd;
2929
EVT_WDF_TIMER EvtTimerFunc;
30+
EVT_WDF_OBJECT_CONTEXT_CLEANUP FrameworkArgbEvtDeviceCleanup;
3031

3132
#define MAX_LAMPARRAY_LAMP_COUNT 256
3233
//#define LAMPARRAY_WIDTH 80000 // 80mm

FrameworkArgb/Driver.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ FrameworkArgbEvtDriverContextCleanup(
145145
Routine Description:
146146
147147
Free all the resources allocated in DriverEntry.
148+
Note: Device-specific cleanup (like EC handle) is done in
149+
FrameworkArgbEvtDeviceCleanup, not here.
148150
149151
Arguments:
150152
@@ -156,19 +158,8 @@ Return Value:
156158
157159
--*/
158160
{
159-
PDEVICE_CONTEXT DeviceContext;
160-
161161
TraceInformation("%!FUNC! Entry");
162162

163-
DeviceContext = GetDeviceContext(DriverObject);
164-
// Close handle to EC driver
165-
if (DeviceContext && DeviceContext->CrosEcHandle && DeviceContext->CrosEcHandle != INVALID_HANDLE_VALUE) {
166-
CloseHandle(DeviceContext->CrosEcHandle);
167-
DeviceContext->CrosEcHandle = INVALID_HANDLE_VALUE;
168-
TraceError("%!FUNC! Failed to close CrosEc Handle");
169-
}
170-
TraceInformation("%!FUNC! Closed CrosEc Handle");
171-
172163
//
173164
// Stop WPP Tracing
174165
//
@@ -177,4 +168,6 @@ Return Value:
177168
#else
178169
WPP_CLEANUP(WdfDriverWdmGetDriverObject((WDFDRIVER)DriverObject));
179170
#endif
171+
172+
TraceInformation("%!FUNC! Exit");
180173
}

0 commit comments

Comments
 (0)