Introduced context space for XUSB binary blobs to use heap memory instead of stack space

This commit is contained in:
Benjamin Höglinger-Stelzer
2018-09-30 17:19:03 +02:00
parent 27065da48e
commit d07521b0e1
5 changed files with 653 additions and 519 deletions

View File

@@ -49,6 +49,7 @@ DEFINE_GUID(GUID_DEVINTERFACE_XUSB_UNKNOWN_2,
#define XUSB_LEDSET_SIZE 0x03
#define XUSB_LEDNUM_SIZE 0x01
#define XUSB_INIT_STAGE_SIZE 0x03
#define XUSB_INIT_BLOB_COUNT 0x07
#define XUSB_IS_DATA_PIPE(_x_) ((BOOLEAN)(_x_->PipeHandle == (USBD_PIPE_HANDLE)0xFFFF0081))
#define XUSB_IS_CONTROL_PIPE(_x_) ((BOOLEAN)(_x_->PipeHandle == (USBD_PIPE_HANDLE)0xFFFF0083))
@@ -98,6 +99,11 @@ typedef struct _XUSB_DEVICE_DATA
//
ULONG InterruptInitStage;
//
// Storage of binary blobs (packets) for PDO initialization
//
PVOID InterruptInitStageBlobs[XUSB_INIT_BLOB_COUNT];
} XUSB_DEVICE_DATA, *PXUSB_DEVICE_DATA;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XUSB_DEVICE_DATA, XusbGetData)
@@ -116,6 +122,7 @@ Bus_XusbSubmitReport(
//
NTSTATUS Xusb_PreparePdo(PWDFDEVICE_INIT DeviceInit, USHORT VendorId, USHORT ProductId, PUNICODE_STRING DeviceId, PUNICODE_STRING DeviceDescription);
NTSTATUS Xusb_PrepareHardware(WDFDEVICE Device);
NTSTATUS Xusb_ReleaseHardware(WDFDEVICE Device);
NTSTATUS Xusb_AssignPdoContext(WDFDEVICE Device);
VOID Xusb_GetConfigurationDescriptorType(PUCHAR Buffer, ULONG Length);
VOID Xusb_GetDeviceDescriptorType(PUSB_DEVICE_DESCRIPTOR pDescriptor, PPDO_DEVICE_DATA pCommon);

View File

@@ -93,7 +93,8 @@ EVT_WDF_CHILD_LIST_CREATE_DEVICE Bus_EvtDeviceListCreatePdo;
EVT_WDF_CHILD_LIST_IDENTIFICATION_DESCRIPTION_COMPARE Bus_EvtChildListIdentificationDescriptionCompare;
EVT_WDF_DEVICE_PREPARE_HARDWARE Bus_EvtDevicePrepareHardware;
EVT_WDF_DEVICE_PREPARE_HARDWARE Pdo_EvtDevicePrepareHardware;
EVT_WDF_DEVICE_RELEASE_HARDWARE Pdo_EvtDeviceReleaseHardware;
EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL Pdo_EvtIoInternalDeviceControl;

View File

@@ -25,7 +25,8 @@
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, Bus_CreatePdo)
#pragma alloc_text(PAGE, Bus_EvtDeviceListCreatePdo)
#pragma alloc_text(PAGE, Bus_EvtDevicePrepareHardware)
#pragma alloc_text(PAGE, Pdo_EvtDevicePrepareHardware)
#pragma alloc_text(PAGE, Pdo_EvtDeviceReleaseHardware)
#endif
NTSTATUS Bus_EvtDeviceListCreatePdo(
@@ -260,7 +261,8 @@ NTSTATUS Bus_CreatePdo(
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
pnpPowerCallbacks.EvtDevicePrepareHardware = Bus_EvtDevicePrepareHardware;
pnpPowerCallbacks.EvtDevicePrepareHardware = Pdo_EvtDevicePrepareHardware;
pnpPowerCallbacks.EvtDeviceReleaseHardware = Pdo_EvtDeviceReleaseHardware;
WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);
@@ -520,9 +522,9 @@ NTSTATUS Bus_CreatePdo(
}
//
// Exposes necessary interfaces on PDO power-up.
// PDO power-up.
//
NTSTATUS Bus_EvtDevicePrepareHardware(
NTSTATUS Pdo_EvtDevicePrepareHardware(
_In_ WDFDEVICE Device,
_In_ WDFCMRESLIST ResourcesRaw,
_In_ WDFCMRESLIST ResourcesTranslated
@@ -577,6 +579,45 @@ NTSTATUS Bus_EvtDevicePrepareHardware(
return status;
}
//
// PDO power-down.
//
_Use_decl_annotations_
NTSTATUS
Pdo_EvtDeviceReleaseHardware(
WDFDEVICE Device,
WDFCMRESLIST ResourcesTranslated
)
{
PPDO_DEVICE_DATA pdoData;
NTSTATUS status = STATUS_UNSUCCESSFUL;
PAGED_CODE();
UNREFERENCED_PARAMETER(ResourcesTranslated);
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_BUSENUM, "%!FUNC! Entry");
pdoData = PdoGetData(Device);
switch (pdoData->TargetType)
{
// Free XUSB resources
case Xbox360Wired:
status = Xusb_ReleaseHardware(Device);
break;
default:
break;
}
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_BUSPDO, "%!FUNC! Exit with status %!STATUS!", status);
return status;
}
//
// Responds to IRP_MJ_INTERNAL_DEVICE_CONTROL requests sent to PDO.
//

View File

@@ -552,48 +552,58 @@ NTSTATUS UsbPdo_BulkOrInterruptTransfer(PURB urb, WDFDEVICE Device, WDFREQUEST R
switch (xusb->InterruptInitStage)
{
case 0:
xusb->InterruptInitStage++;
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
COPY_BYTE_ARRAY(pTransfer->TransferBuffer, P99_PROTECT({
0x01, 0x03, 0x0E
}));
RtlCopyMemory(
pTransfer->TransferBuffer,
xusb->InterruptInitStageBlobs[xusb->InterruptInitStage],
XUSB_INIT_STAGE_SIZE
);
xusb->InterruptInitStage++;
return STATUS_SUCCESS;
case 1:
xusb->InterruptInitStage++;
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
COPY_BYTE_ARRAY(pTransfer->TransferBuffer, P99_PROTECT({
0x02, 0x03, 0x00
}));
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
RtlCopyMemory(
pTransfer->TransferBuffer,
xusb->InterruptInitStageBlobs[xusb->InterruptInitStage],
XUSB_INIT_STAGE_SIZE
);
xusb->InterruptInitStage++;
return STATUS_SUCCESS;
case 2:
xusb->InterruptInitStage++;
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
COPY_BYTE_ARRAY(pTransfer->TransferBuffer, P99_PROTECT({
0x03, 0x03, 0x03
}));
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
RtlCopyMemory(
pTransfer->TransferBuffer,
xusb->InterruptInitStageBlobs[xusb->InterruptInitStage],
XUSB_INIT_STAGE_SIZE
);
xusb->InterruptInitStage++;
return STATUS_SUCCESS;
case 3:
xusb->InterruptInitStage++;
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
COPY_BYTE_ARRAY(pTransfer->TransferBuffer, P99_PROTECT({
0x08, 0x03, 0x00
}));
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
RtlCopyMemory(
pTransfer->TransferBuffer,
xusb->InterruptInitStageBlobs[xusb->InterruptInitStage],
XUSB_INIT_STAGE_SIZE
);
xusb->InterruptInitStage++;
return STATUS_SUCCESS;
case 4:
xusb->InterruptInitStage++;
pTransfer->TransferBufferLength = sizeof(XUSB_INTERRUPT_IN_PACKET);
COPY_BYTE_ARRAY(pTransfer->TransferBuffer, P99_PROTECT({
0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0xe4, 0xf2,
0xb3, 0xf8, 0x49, 0xf3, 0xb0, 0xfc, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
}));
pTransfer->TransferBufferLength = sizeof(XUSB_INTERRUPT_IN_PACKET);
RtlCopyMemory(
pTransfer->TransferBuffer,
xusb->InterruptInitStageBlobs[xusb->InterruptInitStage],
sizeof(XUSB_INTERRUPT_IN_PACKET)
);
xusb->InterruptInitStage++;
return STATUS_SUCCESS;
case 5:
xusb->InterruptInitStage++;
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
COPY_BYTE_ARRAY(pTransfer->TransferBuffer, P99_PROTECT({
0x01, 0x03, 0x03
}));
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
RtlCopyMemory(
pTransfer->TransferBuffer,
xusb->InterruptInitStageBlobs[xusb->InterruptInitStage],
XUSB_INIT_STAGE_SIZE
);
xusb->InterruptInitStage++;
return STATUS_SUCCESS;
default:
/* This request is sent periodically and relies on data the "feeder"
@@ -609,10 +619,11 @@ NTSTATUS UsbPdo_BulkOrInterruptTransfer(PURB urb, WDFDEVICE Device, WDFREQUEST R
{
if (!xusb->ReportedCapabilities && pTransfer->TransferBufferLength >= XUSB_INIT_STAGE_SIZE)
{
pTransfer->TransferBufferLength = XUSB_INIT_STAGE_SIZE;
COPY_BYTE_ARRAY(pTransfer->TransferBuffer, P99_PROTECT({
0x05, 0x03, 0x00
}));
RtlCopyMemory(
pTransfer->TransferBuffer,
xusb->InterruptInitStageBlobs[0x06],
XUSB_INIT_STAGE_SIZE
);
xusb->ReportedCapabilities = TRUE;

1032
sys/xusb.c

File diff suppressed because it is too large Load Diff