Files
ViGEmBus/sys/Queue.cpp

407 lines
11 KiB
C++
Raw Normal View History

2017-08-31 18:32:11 +02:00
/*
2018-08-25 22:17:27 +02:00
* Virtual Gamepad Emulation Framework - Windows kernel-mode bus driver
*
2020-05-22 15:26:24 +02:00
* BSD 3-Clause License
2018-08-25 22:17:27 +02:00
*
2020-05-22 15:26:24 +02:00
* Copyright (c) 2018-2020, Nefarius Software Solutions e.U. and Contributors
* All rights reserved.
2018-08-25 22:17:27 +02:00
*
2020-05-22 15:26:24 +02:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
2020-05-22 15:26:24 +02:00
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
2020-05-22 15:26:24 +02:00
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2017-08-31 18:32:11 +02:00
*/
2020-05-23 16:17:46 +02:00
#include "Driver.h"
2018-05-12 14:33:46 +02:00
#include "queue.tmh"
2017-08-31 18:32:11 +02:00
2020-05-11 17:27:15 +02:00
#include "EmulationTargetPDO.hpp"
#include "XusbPdo.hpp"
#include "Ds4Pdo.hpp"
using ViGEm::Bus::Core::PDO_IDENTIFICATION_DESCRIPTION;
using ViGEm::Bus::Core::EmulationTargetPDO;
using ViGEm::Bus::Targets::EmulationTargetXUSB;
using ViGEm::Bus::Targets::EmulationTargetDS4;
EXTERN_C_START
2017-08-31 18:32:11 +02:00
//
// Responds to I/O control requests sent to the FDO.
//
VOID Bus_EvtIoDeviceControl(
2020-05-12 17:02:34 +02:00
IN WDFQUEUE Queue,
IN WDFREQUEST Request,
IN size_t OutputBufferLength,
IN size_t InputBufferLength,
IN ULONG IoControlCode
2017-08-31 18:32:11 +02:00
)
{
2020-05-14 16:44:37 +02:00
NTSTATUS status = STATUS_INVALID_PARAMETER;
WDFDEVICE Device;
size_t length = 0;
PXUSB_SUBMIT_REPORT xusbSubmit = nullptr;
PXUSB_REQUEST_NOTIFICATION xusbNotify = nullptr;
PDS4_SUBMIT_REPORT ds4Submit = nullptr;
PDS4_REQUEST_NOTIFICATION ds4Notify = nullptr;
PVIGEM_CHECK_VERSION pCheckVersion = nullptr;
PXUSB_GET_USER_INDEX pXusbGetUserIndex = nullptr;
2020-05-12 17:02:34 +02:00
EmulationTargetPDO* pdo;
Device = WdfIoQueueGetDevice(Queue);
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "%!FUNC! Entry (device: 0x%p)", Device);
2020-05-12 17:02:34 +02:00
switch (IoControlCode)
{
2017-08-31 18:32:11 +02:00
#pragma region IOCTL_VIGEM_CHECK_VERSION
2020-05-14 16:44:37 +02:00
2020-05-12 17:02:34 +02:00
case IOCTL_VIGEM_CHECK_VERSION:
2017-08-31 18:32:11 +02:00
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "IOCTL_VIGEM_CHECK_VERSION");
2017-08-31 18:32:11 +02:00
2020-05-12 17:02:34 +02:00
status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(VIGEM_CHECK_VERSION),
2020-05-14 16:44:37 +02:00
reinterpret_cast<PVOID*>(&pCheckVersion),
2020-05-12 17:02:34 +02:00
&length
);
2017-08-31 18:32:11 +02:00
2020-05-12 17:02:34 +02:00
if (!NT_SUCCESS(status) || length != sizeof(VIGEM_CHECK_VERSION))
{
status = STATUS_INVALID_PARAMETER;
break;
}
2017-08-31 18:32:11 +02:00
2020-05-12 17:02:34 +02:00
status = (pCheckVersion->Version == VIGEM_COMMON_VERSION) ? STATUS_SUCCESS : STATUS_NOT_SUPPORTED;
2017-08-31 18:32:11 +02:00
2020-05-12 17:02:34 +02:00
TraceEvents(TRACE_LEVEL_VERBOSE,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"Requested version: 0x%04X, compiled version: 0x%04X",
pCheckVersion->Version, VIGEM_COMMON_VERSION);
2018-05-12 19:54:39 +02:00
2020-05-12 17:02:34 +02:00
break;
2020-05-14 16:44:37 +02:00
#pragma endregion
2017-08-31 18:32:11 +02:00
#pragma region IOCTL_VIGEM_PLUGIN_TARGET
2020-05-14 16:44:37 +02:00
2020-05-12 17:02:34 +02:00
case IOCTL_VIGEM_PLUGIN_TARGET:
2017-08-31 18:32:11 +02:00
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "IOCTL_VIGEM_PLUGIN_TARGET");
2017-08-31 18:32:11 +02:00
2020-05-12 17:02:34 +02:00
status = Bus_PlugInDevice(Device, Request, FALSE, &length);
2017-08-31 18:32:11 +02:00
2020-05-12 17:02:34 +02:00
break;
2020-05-14 16:44:37 +02:00
#pragma endregion
2017-08-31 18:32:11 +02:00
#pragma region IOCTL_VIGEM_UNPLUG_TARGET
2020-05-14 16:44:37 +02:00
2020-05-12 17:02:34 +02:00
case IOCTL_VIGEM_UNPLUG_TARGET:
2020-05-14 16:44:37 +02:00
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "IOCTL_VIGEM_UNPLUG_TARGET");
2017-08-31 18:32:11 +02:00
2020-05-12 17:02:34 +02:00
status = Bus_UnPlugDevice(Device, Request, FALSE, &length);
2017-08-31 18:32:11 +02:00
2020-05-12 17:02:34 +02:00
break;
2020-05-14 16:44:37 +02:00
#pragma endregion
2017-08-31 18:32:11 +02:00
#pragma region IOCTL_XUSB_SUBMIT_REPORT
2020-05-14 16:44:37 +02:00
2020-05-12 17:02:34 +02:00
case IOCTL_XUSB_SUBMIT_REPORT:
2020-05-14 16:44:37 +02:00
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "IOCTL_XUSB_SUBMIT_REPORT");
2020-05-12 17:02:34 +02:00
status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(XUSB_SUBMIT_REPORT),
2020-05-14 16:44:37 +02:00
reinterpret_cast<PVOID*>(&xusbSubmit),
2020-05-12 17:02:34 +02:00
&length
);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"WdfRequestRetrieveInputBuffer failed with status %!STATUS!",
status);
2020-05-12 17:02:34 +02:00
break;
}
if ((sizeof(XUSB_SUBMIT_REPORT) == xusbSubmit->Size) && (length == InputBufferLength))
{
// This request only supports a single PDO at a time
if (xusbSubmit->SerialNo == 0)
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"Invalid serial 0 submitted");
2020-05-12 17:02:34 +02:00
status = STATUS_INVALID_PARAMETER;
break;
}
if (!EmulationTargetPDO::GetPdoByTypeAndSerial(Device, Xbox360Wired, xusbSubmit->SerialNo, &pdo))
2020-05-12 17:02:34 +02:00
status = STATUS_DEVICE_DOES_NOT_EXIST;
else
status = pdo->SubmitReport(xusbSubmit);
}
break;
2020-05-14 16:44:37 +02:00
#pragma endregion
2017-08-31 18:32:11 +02:00
#pragma region IOCTL_XUSB_REQUEST_NOTIFICATION
2020-05-14 16:44:37 +02:00
2020-05-12 17:02:34 +02:00
case IOCTL_XUSB_REQUEST_NOTIFICATION:
2020-05-14 16:44:37 +02:00
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "IOCTL_XUSB_REQUEST_NOTIFICATION");
2020-05-12 17:02:34 +02:00
// Don't accept the request if the output buffer can't hold the results
if (OutputBufferLength < sizeof(XUSB_REQUEST_NOTIFICATION))
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"Output buffer %d too small, require at least %d",
static_cast<int>(OutputBufferLength), static_cast<int>(sizeof(XUSB_REQUEST_NOTIFICATION)));
2020-05-12 17:02:34 +02:00
break;
}
status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(XUSB_REQUEST_NOTIFICATION),
2020-05-14 16:44:37 +02:00
reinterpret_cast<PVOID*>(&xusbNotify),
2020-05-12 17:02:34 +02:00
&length
);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"WdfRequestRetrieveInputBuffer failed with status %!STATUS!",
status);
2020-05-12 17:02:34 +02:00
break;
}
if ((sizeof(XUSB_REQUEST_NOTIFICATION) == xusbNotify->Size) && (length == InputBufferLength))
{
// This request only supports a single PDO at a time
if (xusbNotify->SerialNo == 0)
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"Invalid serial 0 submitted");
2020-05-12 17:02:34 +02:00
status = STATUS_INVALID_PARAMETER;
break;
}
if (!EmulationTargetPDO::GetPdoByTypeAndSerial(Device, Xbox360Wired, xusbNotify->SerialNo, &pdo))
status = STATUS_DEVICE_DOES_NOT_EXIST;
else
{
status = pdo->EnqueueNotification(Request);
status = (NT_SUCCESS(status)) ? STATUS_PENDING : status;
}
2020-05-12 17:02:34 +02:00
}
break;
2020-05-14 16:44:37 +02:00
#pragma endregion
2017-08-31 18:32:11 +02:00
#pragma region IOCTL_DS4_SUBMIT_REPORT
2020-05-14 16:44:37 +02:00
2020-05-12 17:02:34 +02:00
case IOCTL_DS4_SUBMIT_REPORT:
2020-05-14 16:44:37 +02:00
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "IOCTL_DS4_SUBMIT_REPORT");
2020-05-12 17:02:34 +02:00
status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(DS4_SUBMIT_REPORT),
2020-05-14 16:44:37 +02:00
reinterpret_cast<PVOID*>(&ds4Submit),
2020-05-12 17:02:34 +02:00
&length
);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"WdfRequestRetrieveInputBuffer failed with status %!STATUS!",
status);
2020-05-12 17:02:34 +02:00
break;
}
if ((sizeof(DS4_SUBMIT_REPORT) == ds4Submit->Size) && (length == InputBufferLength))
{
// This request only supports a single PDO at a time
if (ds4Submit->SerialNo == 0)
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"Invalid serial 0 submitted");
2020-05-12 17:02:34 +02:00
status = STATUS_INVALID_PARAMETER;
break;
}
if (!EmulationTargetPDO::GetPdoByTypeAndSerial(Device, DualShock4Wired, ds4Submit->SerialNo, &pdo))
2020-05-12 17:02:34 +02:00
status = STATUS_DEVICE_DOES_NOT_EXIST;
else
status = pdo->SubmitReport(ds4Submit);
}
break;
2020-05-14 16:44:37 +02:00
#pragma endregion
2017-08-31 18:32:11 +02:00
#pragma region IOCTL_DS4_REQUEST_NOTIFICATION
2020-05-12 17:14:00 +02:00
2020-05-12 17:02:34 +02:00
case IOCTL_DS4_REQUEST_NOTIFICATION:
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "IOCTL_DS4_REQUEST_NOTIFICATION");
2020-05-12 17:02:34 +02:00
// Don't accept the request if the output buffer can't hold the results
if (OutputBufferLength < sizeof(DS4_REQUEST_NOTIFICATION))
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"Output buffer %d too small, require at least %d",
static_cast<int>(OutputBufferLength), static_cast<int>(sizeof(DS4_REQUEST_NOTIFICATION)));
2020-05-12 17:02:34 +02:00
break;
}
status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(DS4_REQUEST_NOTIFICATION),
2020-05-14 16:44:37 +02:00
reinterpret_cast<PVOID*>(&ds4Notify),
2020-05-12 17:02:34 +02:00
&length
);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"WdfRequestRetrieveInputBuffer failed with status %!STATUS!",
status);
2020-05-12 17:02:34 +02:00
break;
}
if ((sizeof(DS4_REQUEST_NOTIFICATION) == ds4Notify->Size) && (length == InputBufferLength))
{
// This request only supports a single PDO at a time
if (ds4Notify->SerialNo == 0)
{
TraceEvents(TRACE_LEVEL_ERROR,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"Invalid serial 0 submitted");
2020-05-12 17:02:34 +02:00
status = STATUS_INVALID_PARAMETER;
break;
}
if (!EmulationTargetPDO::GetPdoByTypeAndSerial(Device, DualShock4Wired, ds4Notify->SerialNo, &pdo))
status = STATUS_DEVICE_DOES_NOT_EXIST;
else
{
status = pdo->EnqueueNotification(Request);
status = (NT_SUCCESS(status)) ? STATUS_PENDING : status;
}
2020-05-12 17:02:34 +02:00
}
break;
2020-05-14 16:44:37 +02:00
#pragma endregion
2017-08-31 18:32:11 +02:00
#pragma region IOCTL_XUSB_GET_USER_INDEX
2020-05-14 16:44:37 +02:00
2020-05-12 17:02:34 +02:00
case IOCTL_XUSB_GET_USER_INDEX:
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "IOCTL_XUSB_GET_USER_INDEX");
2020-05-14 16:44:37 +02:00
2020-05-12 17:02:34 +02:00
// Don't accept the request if the output buffer can't hold the results
if (OutputBufferLength < sizeof(XUSB_GET_USER_INDEX))
{
KdPrint((DRIVERNAME "IOCTL_XUSB_GET_USER_INDEX: output buffer too small: %ul\n", OutputBufferLength));
break;
}
status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(XUSB_GET_USER_INDEX),
2020-05-14 16:44:37 +02:00
reinterpret_cast<PVOID*>(&pXusbGetUserIndex),
2020-05-12 17:02:34 +02:00
&length);
if (!NT_SUCCESS(status))
{
KdPrint((DRIVERNAME "WdfRequestRetrieveInputBuffer failed 0x%x\n", status));
break;
}
if ((sizeof(XUSB_GET_USER_INDEX) == pXusbGetUserIndex->Size) && (length == InputBufferLength))
{
// This request only supports a single PDO at a time
if (pXusbGetUserIndex->SerialNo == 0)
{
status = STATUS_INVALID_PARAMETER;
break;
}
if (!EmulationTargetPDO::GetPdoByTypeAndSerial(Device, Xbox360Wired, pXusbGetUserIndex->SerialNo, &pdo))
2020-05-12 17:02:34 +02:00
{
status = STATUS_DEVICE_DOES_NOT_EXIST;
break;
}
status = static_cast<EmulationTargetXUSB*>(pdo)->GetUserIndex(&pXusbGetUserIndex->UserIndex);
}
break;
2020-05-14 16:44:37 +02:00
#pragma endregion
2020-05-12 17:02:34 +02:00
default:
TraceEvents(TRACE_LEVEL_WARNING,
2020-05-14 16:44:37 +02:00
TRACE_QUEUE,
"Unknown I/O control code 0x%X", IoControlCode);
2018-05-12 19:54:39 +02:00
2020-05-12 17:02:34 +02:00
break; // default status is STATUS_INVALID_PARAMETER
}
2018-05-12 19:54:39 +02:00
2020-05-12 17:02:34 +02:00
if (status != STATUS_PENDING)
{
WdfRequestCompleteWithInformation(Request, status, length);
}
2017-08-31 18:32:11 +02:00
2020-05-12 17:14:00 +02:00
TraceDbg(TRACE_QUEUE, "%!FUNC! Exit with status %!STATUS!", status);
2017-08-31 18:32:11 +02:00
}
2020-05-11 17:27:15 +02:00
EXTERN_C_END