Skip to content

Commit

Permalink
Partially fixes #73
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Jul 10, 2023
1 parent c441ebe commit 650c114
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 106 deletions.
12 changes: 3 additions & 9 deletions BthPS3PSM/Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ BthPS3PSM_CreateDevice(
WDFDEVICE device;
NTSTATUS status;
WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
WDFKEY key = NULL;
WDF_OBJECT_ATTRIBUTES stringAttributes;
BOOLEAN isUsb = FALSE;
BOOLEAN ret = FALSE;
Expand Down Expand Up @@ -219,7 +218,7 @@ BthPS3PSM_CreateDevice(
PLUGPLAY_REGKEY_DEVICE,
KEY_READ,
WDF_NO_OBJECT_ATTRIBUTES,
&key
&deviceContext->RegKeyDeviceNode
)))
{
TraceError(
Expand All @@ -235,7 +234,7 @@ BthPS3PSM_CreateDevice(
// Query for BthPS3PSMPatchEnabled value
//
if (!NT_SUCCESS(status = WdfRegistryQueryULong(
key,
deviceContext->RegKeyDeviceNode,
&patchPSMRegValue,
&deviceContext->IsPsmPatchingEnabled
)))
Expand Down Expand Up @@ -295,7 +294,7 @@ BthPS3PSM_CreateDevice(
// Grab symbolic link so device can be associated with radio in user-mode
//
if (!NT_SUCCESS(status = WdfRegistryQueryString(
key,
deviceContext->RegKeyDeviceNode,
&linkNameRegValue,
deviceContext->SymbolicLinkName
)))
Expand Down Expand Up @@ -347,11 +346,6 @@ BthPS3PSM_CreateDevice(
WdfObjectDelete(instanceId);
}

if (key)
{
WdfRegistryClose(key);
}

FuncExit(TRACE_DEVICE, "status=%!STATUS!", status);

return status;
Expand Down
5 changes: 5 additions & 0 deletions BthPS3PSM/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ typedef struct _DEVICE_CONTEXT
//
WDFMEMORY InstanceId;

//
// Registry handle to device node
//
WDFKEY RegKeyDeviceNode;

} DEVICE_CONTEXT, * PDEVICE_CONTEXT;

//
Expand Down
184 changes: 87 additions & 97 deletions BthPS3PSM/Sideband.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,13 @@ VOID BthPS3PSM_SidebandIoDeviceControl(
PBTHPS3PSM_DISABLE_PSM_PATCHING pDisable = NULL;
PBTHPS3PSM_GET_PSM_PATCHING pGet = NULL;
UNICODE_STRING linkName;
WDFKEY key = NULL;

DECLARE_CONST_UNICODE_STRING(patchPSMRegValue, G_PatchPSMRegValue);
WDF_WORKITEM_CONFIG wiCfg;
WDF_OBJECT_ATTRIBUTES attributes;
WDFWORKITEM workItem = NULL;

FuncEntry(TRACE_SIDEBAND);

UNREFERENCED_PARAMETER(Queue);
UNREFERENCED_PARAMETER(OutputBufferLength);
UNREFERENCED_PARAMETER(InputBufferLength);

Expand Down Expand Up @@ -328,59 +329,29 @@ VOID BthPS3PSM_SidebandIoDeviceControl(
pEnable->DeviceIndex
);

if (NT_SUCCESS(status = WdfDeviceOpenRegistryKey(
WdfIoQueueGetDevice(Queue),
/*
* Expands to e.g.:
*
* "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_XXXX&PID_XXXX\a&c9c4e92&0&4\Device Parameters"
*
*/
PLUGPLAY_REGKEY_DEVICE,
KEY_WRITE,
WDF_NO_OBJECT_ATTRIBUTES,
&key
//
// Prepare async saving at PASSIVE_LEVEL
//
WDF_WORKITEM_CONFIG_INIT(&wiCfg, BthPS3PSM_EvtSaveConfigToRegistry);
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = device;

if (!NT_SUCCESS(status = WdfWorkItemCreate(
&wiCfg,
&attributes,
&workItem
)))
{
if (!NT_SUCCESS(status = WdfRegistryAssignULong(
key,
&patchPSMRegValue,
pDevCtx->IsPsmPatchingEnabled
)))
{
TraceError(
TRACE_SIDEBAND,
"WdfRegistryAssignULong failed with status %!STATUS!",
status
);
EventWriteFailedWithNTStatus(NULL, __FUNCTION__, L"WdfRegistryAssignULong", status);
}
else
{
TraceVerbose(
TRACE_SIDEBAND,
"Settings stored"
);

const PWSTR instanceIdString = (const PWSTR)WdfMemoryGetBuffer(pDevCtx->InstanceId, NULL);

EventWriteSetPatchStatusForDeviceInstance(
NULL,
pDevCtx->IsPsmPatchingEnabled,
instanceIdString
);
}

WdfRegistryClose(key);
}
else
{
TraceError(
TRACE_SIDEBAND,
"WdfDeviceOpenRegistryKey failed with status %!STATUS!",
"WdfWorkItemCreate failed with status %!STATUS!",
status
);
EventWriteFailedWithNTStatus(NULL, __FUNCTION__, L"WdfDeviceOpenRegistryKey", status);
EventWriteFailedWithNTStatus(NULL, __FUNCTION__, L"WdfWorkItemCreate", status);
}
else
{
WdfWorkItemEnqueue(workItem);
}
}

Expand Down Expand Up @@ -434,59 +405,29 @@ VOID BthPS3PSM_SidebandIoDeviceControl(
pDisable->DeviceIndex
);

if (NT_SUCCESS(status = WdfDeviceOpenRegistryKey(
WdfIoQueueGetDevice(Queue),
/*
* Expands to e.g.:
*
* "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_XXXX&PID_XXXX\a&c9c4e92&0&4\Device Parameters"
*
*/
PLUGPLAY_REGKEY_DEVICE,
KEY_WRITE,
WDF_NO_OBJECT_ATTRIBUTES,
&key
//
// Prepare async saving at PASSIVE_LEVEL
//
WDF_WORKITEM_CONFIG_INIT(&wiCfg, BthPS3PSM_EvtSaveConfigToRegistry);
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = device;

if (!NT_SUCCESS(status = WdfWorkItemCreate(
&wiCfg,
&attributes,
&workItem
)))
{
if (!NT_SUCCESS(status = WdfRegistryAssignULong(
key,
&patchPSMRegValue,
pDevCtx->IsPsmPatchingEnabled
)))
{
TraceError(
TRACE_SIDEBAND,
"WdfRegistryAssignULong failed with status %!STATUS!",
status
);
EventWriteFailedWithNTStatus(NULL, __FUNCTION__, L"WdfRegistryAssignULong", status);
}
else
{
TraceVerbose(
TRACE_SIDEBAND,
"Settings stored"
);

const PWSTR instanceIdString = (const PWSTR)WdfMemoryGetBuffer(pDevCtx->InstanceId, NULL);

EventWriteSetPatchStatusForDeviceInstance(
NULL,
pDevCtx->IsPsmPatchingEnabled,
instanceIdString
);
}

WdfRegistryClose(key);
}
else
{
TraceError(
TRACE_SIDEBAND,
"WdfDeviceOpenRegistryKey failed with status %!STATUS!",
"WdfWorkItemCreate failed with status %!STATUS!",
status
);
EventWriteFailedWithNTStatus(NULL, __FUNCTION__, L"WdfDeviceOpenRegistryKey", status);
EventWriteFailedWithNTStatus(NULL, __FUNCTION__, L"WdfWorkItemCreate", status);
}
else
{
WdfWorkItemEnqueue(workItem);
}
}

Expand Down Expand Up @@ -596,4 +537,53 @@ VOID BthPS3PSM_SidebandIoDeviceControl(
}
#pragma warning(pop) // enable 28118 again

//
// Async operation to store changed settings to registry at PASSIVE_LEVEL
//
void BthPS3PSM_EvtSaveConfigToRegistry(
WDFWORKITEM WorkItem
)
{
NTSTATUS status;
const WDFDEVICE device = WdfWorkItemGetParentObject(WorkItem);
const PDEVICE_CONTEXT pDevCtx = DeviceGetContext(device);

DECLARE_CONST_UNICODE_STRING(patchPSMRegValue, G_PatchPSMRegValue);

FuncEntryArguments(TRACE_SIDEBAND, "IRQL=%!irql!", KeGetCurrentIrql());

if (!NT_SUCCESS(status = WdfRegistryAssignULong(
pDevCtx->RegKeyDeviceNode,
&patchPSMRegValue,
pDevCtx->IsPsmPatchingEnabled
)))
{
TraceError(
TRACE_SIDEBAND,
"WdfRegistryAssignULong failed with status %!STATUS!",
status
);
EventWriteFailedWithNTStatus(NULL, __FUNCTION__, L"WdfRegistryAssignULong", status);
}
else
{
TraceVerbose(
TRACE_SIDEBAND,
"Settings stored"
);

const PWSTR instanceIdString = (const PWSTR)WdfMemoryGetBuffer(pDevCtx->InstanceId, NULL);

EventWriteSetPatchStatusForDeviceInstance(
NULL,
pDevCtx->IsPsmPatchingEnabled,
instanceIdString
);
}

WdfObjectDelete(WorkItem);

FuncExitNoReturn(TRACE_SIDEBAND);
}

#endif
2 changes: 2 additions & 0 deletions BthPS3PSM/Sideband.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ BthPS3PSM_DeleteControlDevice(
WDFDEVICE Device
);

EVT_WDF_WORKITEM BthPS3PSM_EvtSaveConfigToRegistry;

#endif

0 comments on commit 650c114

Please sign in to comment.