Skip to content

Commit

Permalink
Changed sync disconnect to async (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
nefarius committed Mar 27, 2021
1 parent b837681 commit 31d6f95
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 19 deletions.
123 changes: 104 additions & 19 deletions BthPS3/BusLogic.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,14 @@ NTSTATUS BthPS3_PDO_EvtWdfDeviceD0Exit(
WDF_POWER_DEVICE_STATE TargetState
)
{
NTSTATUS status = STATUS_SUCCESS;
NTSTATUS status;
WDFDEVICE parentDevice;
WDFIOTARGET parentTarget;
WDF_MEMORY_DESCRIPTOR memDesc;
PBTHPS3_PDO_DEVICE_CONTEXT pPdoDevCtx;

WDFREQUEST dcRequest;
WDF_OBJECT_ATTRIBUTES attributes;
WDFMEMORY payload;


UNREFERENCED_PARAMETER(Device);
UNREFERENCED_PARAMETER(TargetState);
Expand All @@ -629,39 +631,98 @@ NTSTATUS BthPS3_PDO_EvtWdfDeviceD0Exit(
"Requesting device disconnect"
);

WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(
&memDesc,
&pPdoDevCtx->ClientConnection->RemoteAddress,
sizeof(BTH_ADDR)
);

/*
* Low power conditions are not supported by the remote device,
* if low power or idle was requested, instruct radio to disconnect.
*/

//
// Request parent to abandon us :'(
//
status = WdfIoTargetSendIoctlSynchronously(
parentTarget, // send to parent (FDO)
NULL, // use internal request object
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = Device;

status = WdfRequestCreate(
&attributes,
parentTarget,
&dcRequest
);
if (!NT_SUCCESS(status))
{
TraceError(
TRACE_BUSLOGIC,
"WdfRequestCreate failed with status %!STATUS!",
status
);
}

WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = dcRequest;

status = WdfMemoryCreatePreallocated(
&attributes,
&pPdoDevCtx->ClientConnection->RemoteAddress,
sizeof(BTH_ADDR),
&payload
);
if (!NT_SUCCESS(status))
{
TraceError(
TRACE_BUSLOGIC,
"WdfMemoryCreatePreallocated failed with status %!STATUS!",
status
);
}

status = WdfIoTargetFormatRequestForIoctl(
parentTarget,
dcRequest,
IOCTL_BTH_DISCONNECT_DEVICE,
&memDesc, // holds address to disconnect
payload,
NULL,
NULL,
NULL
);

if (status != STATUS_DEVICE_NOT_CONNECTED && !NT_SUCCESS(status))
if (!NT_SUCCESS(status))
{
TraceError(
TRACE_BUSLOGIC,
"WdfIoTargetSendIoctlSynchronously failed with status %!STATUS!",
"WdfIoTargetFormatRequestForIoctl failed with status %!STATUS!",
status
);
}

WdfRequestSetCompletionRoutine(
dcRequest,
BthPS3_PDO_DisconnectRequestCompleted,
NULL
);

//
// Request parent to abandon us :'(
//
if (WdfRequestSend(
dcRequest,
parentTarget,
WDF_NO_SEND_OPTIONS
) == FALSE)
{
status = WdfRequestGetStatus(dcRequest);

if (status != STATUS_DEVICE_NOT_CONNECTED && !NT_SUCCESS(status))
{
TraceError(
TRACE_BUSLOGIC,
"WdfRequestSend failed with status %!STATUS!",
status
);
}
else
{
//
// Override
//
status = STATUS_SUCCESS;
}
}

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

return status;
Expand Down Expand Up @@ -963,6 +1024,30 @@ void BthPS3_PDO_EvtWdfIoQueueIoDeviceControl(
FuncExit(TRACE_BUSLOGIC, "status=%!STATUS!", status);
}

//
// Disconnect request got completed
//
void BthPS3_PDO_DisconnectRequestCompleted(
_In_ WDFREQUEST Request,
_In_ WDFIOTARGET Target,
_In_ PWDF_REQUEST_COMPLETION_PARAMS Params,
_In_ WDFCONTEXT Context
)
{
UNREFERENCED_PARAMETER(Target);
UNREFERENCED_PARAMETER(Context);

FuncEntryArguments(
TRACE_BUSLOGIC,
"status=%!STATUS!",
Params->IoStatus.Status
);

WdfObjectDelete(Request);

FuncExitNoReturn(TRACE_BUSLOGIC);
}

//
// Set device properties
//
Expand Down
2 changes: 2 additions & 0 deletions BthPS3/BusLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ EVT_WDF_DEVICE_D0_EXIT BthPS3_PDO_EvtWdfDeviceD0Exit;

EVT_WDF_DEVICE_SELF_MANAGED_IO_INIT BthPS3_PDO_EvtWdfDeviceSelfManagedIoInit;

EVT_WDF_REQUEST_COMPLETION_ROUTINE BthPS3_PDO_DisconnectRequestCompleted;

#if KMDF_VERSION_MAJOR == 1 && KMDF_VERSION_MINOR >= 13
#if (NTDDI_VERSION < NTDDI_WIN10)
DEFINE_DEVPROPKEY(DEVPKEY_Bluetooth_LastConnectedTime,
Expand Down

0 comments on commit 31d6f95

Please sign in to comment.