Files
ViGEmBus/sys/EmulationTargetPDO.hpp

310 lines
8.2 KiB
C++
Raw Normal View History

2020-05-11 19:24:38 -04:00
/*
* Virtual Gamepad Emulation Framework - Windows kernel-mode bus driver
*
2020-05-22 15:26:24 +02:00
* BSD 3-Clause License
2020-05-11 19:24:38 -04:00
*
2020-05-22 15:26:24 +02:00
* Copyright (c) 2018-2020, Nefarius Software Solutions e.U. and Contributors
* All rights reserved.
2020-05-11 19:24:38 -04: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-11 19:24:38 -04:00
*
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-11 19:24:38 -04:00
*
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.
2020-05-11 19:24:38 -04:00
*/
2020-05-22 15:26:24 +02:00
#pragma once
2020-11-23 22:23:48 +01:00
#pragma warning(disable:5040)
#include <DmfModules.Library.h>
#pragma warning(default:5040)
#include <ntddk.h>
#include <wdf.h>
2020-05-10 17:03:01 +02:00
#include <ntintsafe.h>
#include <usb.h>
#include <usbbusif.h>
2020-05-10 17:03:01 +02:00
#include <ViGEm/Common.h>
//
// Some insane macro-magic =3
//
#define P99_PROTECT(...) __VA_ARGS__
#define COPY_BYTE_ARRAY(_dst_, _bytes_) do {BYTE b[] = _bytes_; \
RtlCopyMemory(_dst_, b, RTL_NUMBER_OF_V1(b)); } while (0)
namespace ViGEm::Bus::Core
{
2020-05-10 22:57:05 +02:00
typedef struct _PDO_IDENTIFICATION_DESCRIPTION* PPDO_IDENTIFICATION_DESCRIPTION;
class EmulationTargetPDO
{
public:
2020-05-11 17:27:15 +02:00
EmulationTargetPDO(ULONG Serial, LONG SessionId, USHORT VendorId, USHORT ProductId);
virtual ~EmulationTargetPDO() = default;
2020-08-29 15:52:39 +02:00
static bool GetPdoByTypeAndSerial(
IN WDFDEVICE ParentDevice,
IN VIGEM_TARGET_TYPE Type,
IN ULONG SerialNo,
OUT EmulationTargetPDO** Object
);
2020-08-29 15:52:39 +02:00
static NTSTATUS EnqueueWaitDeviceReady(
WDFDEVICE ParentDevice,
ULONG SerialNo,
WDFREQUEST Request);
static EVT_WDF_CHILD_LIST_IDENTIFICATION_DESCRIPTION_COMPARE EvtChildListIdentificationDescriptionCompare;
2020-05-11 13:54:36 +02:00
virtual NTSTATUS PdoPrepareDevice(PWDFDEVICE_INIT DeviceInit,
2020-08-29 15:52:39 +02:00
PUNICODE_STRING DeviceId,
PUNICODE_STRING DeviceDescription) = 0;
2020-05-11 13:54:36 +02:00
virtual NTSTATUS PdoPrepareHardware() = 0;
2020-05-11 13:54:36 +02:00
virtual NTSTATUS PdoInitContext() = 0;
2020-05-11 17:27:15 +02:00
NTSTATUS PdoCreateDevice(_In_ WDFDEVICE ParentDevice,
2020-08-29 15:52:39 +02:00
_In_ PWDFDEVICE_INIT DeviceInit);
2020-05-10 23:27:03 +02:00
bool operator==(EmulationTargetPDO& other) const
{
2020-05-11 17:27:15 +02:00
return (other._SerialNo == this->_SerialNo);
2020-05-10 23:27:03 +02:00
}
2020-05-11 00:06:35 +02:00
2020-05-11 20:57:14 +02:00
virtual NTSTATUS UsbGetDeviceDescriptorType(PUSB_DEVICE_DESCRIPTOR pDescriptor) = 0;
2020-05-11 17:27:15 +02:00
2020-05-11 00:06:35 +02:00
NTSTATUS UsbSelectConfiguration(PURB Urb);
2020-05-11 00:24:57 +02:00
2020-05-11 00:06:35 +02:00
void UsbAbortPipe();
2020-05-11 00:17:25 +02:00
NTSTATUS UsbGetConfigurationDescriptorType(PURB Urb);
2020-05-11 00:24:57 +02:00
2020-05-11 12:24:25 +02:00
virtual NTSTATUS UsbClassInterface(PURB Urb) = 0;
virtual NTSTATUS UsbGetDescriptorFromInterface(PURB Urb) = 0;
2020-05-11 12:37:40 +02:00
virtual NTSTATUS UsbSelectInterface(PURB Urb) = 0;
2020-05-11 13:06:33 +02:00
virtual NTSTATUS UsbGetStringDescriptorType(PURB Urb) = 0;
2020-05-11 17:27:15 +02:00
virtual NTSTATUS UsbBulkOrInterruptTransfer(struct _URB_BULK_OR_INTERRUPT_TRANSFER* pTransfer,
2020-08-29 15:52:39 +02:00
WDFREQUEST Request) = 0;
2020-05-11 13:11:02 +02:00
2020-05-11 14:09:32 +02:00
virtual NTSTATUS UsbControlTransfer(PURB Urb) = 0;
2020-05-11 17:27:15 +02:00
NTSTATUS SubmitReport(PVOID NewReport);
2020-05-11 17:27:15 +02:00
NTSTATUS EnqueueNotification(WDFREQUEST Request) const;
bool IsOwnerProcess() const;
2020-05-12 17:02:34 +02:00
VIGEM_TARGET_TYPE GetType() const;
NTSTATUS PdoPrepare(WDFDEVICE ParentDevice);
2020-08-29 15:52:39 +02:00
2020-05-11 17:27:15 +02:00
private:
static unsigned long current_process_id();
2020-05-11 21:39:43 +02:00
static EVT_WDF_DEVICE_CONTEXT_CLEANUP EvtDeviceContextCleanup;
2020-08-29 15:52:39 +02:00
static bool GetPdoBySerial(
IN WDFDEVICE ParentDevice,
IN ULONG SerialNo,
OUT EmulationTargetPDO** Object
);
NTSTATUS EnqueueWaitDeviceReady(WDFREQUEST Request);
2020-08-29 15:52:39 +02:00
HANDLE _WaitDeviceReadyCompletionWorkerThreadHandle{};
protected:
static const ULONG _maxHardwareIdLength = 0xFF;
2020-05-13 21:58:37 +02:00
static const int MAX_INSTANCE_ID_LEN = 80;
2020-08-29 15:52:39 +02:00
2020-11-23 22:23:48 +01:00
static const size_t MAX_OUT_BUFFER_QUEUE_COUNT = 64;
2020-11-23 22:23:48 +01:00
static const size_t MAX_OUT_BUFFER_QUEUE_SIZE = 128;
2020-05-10 22:57:05 +02:00
static PCWSTR _deviceLocation;
2020-05-11 13:54:36 +02:00
static BOOLEAN USB_BUSIFFN UsbInterfaceIsDeviceHighSpeed(IN PVOID BusContext);
2020-05-11 13:54:36 +02:00
static NTSTATUS USB_BUSIFFN UsbInterfaceQueryBusInformation(
IN PVOID BusContext,
IN ULONG Level,
IN OUT PVOID BusInformationBuffer,
IN OUT PULONG BusInformationBufferLength,
OUT PULONG BusInformationActualLength
);
2020-05-11 13:54:36 +02:00
static NTSTATUS USB_BUSIFFN UsbInterfaceSubmitIsoOutUrb(IN PVOID BusContext, IN PURB Urb);
2020-05-11 13:54:36 +02:00
static NTSTATUS USB_BUSIFFN UsbInterfaceQueryBusTime(IN PVOID BusContext, IN OUT PULONG CurrentUsbFrame);
2020-05-11 13:54:36 +02:00
static VOID USB_BUSIFFN UsbInterfaceGetUSBDIVersion(
IN PVOID BusContext,
IN OUT PUSBD_VERSION_INFORMATION VersionInformation,
IN OUT PULONG HcdCapabilities
);
2020-05-10 17:03:01 +02:00
2020-05-10 22:57:05 +02:00
static EVT_WDF_DEVICE_PREPARE_HARDWARE EvtDevicePrepareHardware;
static EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL EvtIoInternalDeviceControl;
2020-11-23 22:23:48 +01:00
static EVT_WDF_IO_QUEUE_STATE EvtWdfIoPendingNotificationQueueState;
2020-08-27 20:30:12 +02:00
static VOID WaitDeviceReadyCompletionWorkerRoutine(IN PVOID StartContext);
2020-08-29 15:52:39 +02:00
2020-11-23 22:23:48 +01:00
static VOID DumpAsHex(PCSTR Prefix, PVOID Buffer, ULONG BufferLength);
static VOID DmfDeviceModulesAdd(_In_ WDFDEVICE Device, _In_ PDMFMODULE_INIT DmfModuleInit);
2020-05-11 00:17:25 +02:00
virtual VOID GetConfigurationDescriptorType(PUCHAR Buffer, ULONG Length) = 0;
2020-05-11 00:24:57 +02:00
2020-05-11 00:06:35 +02:00
virtual NTSTATUS SelectConfiguration(PURB Urb) = 0;
2020-05-11 00:24:57 +02:00
2020-05-11 00:06:35 +02:00
virtual void AbortPipe() = 0;
2020-05-11 00:24:57 +02:00
virtual NTSTATUS SubmitReportImpl(PVOID NewReport) = 0;
2020-05-15 17:00:06 +02:00
2020-11-23 22:23:48 +01:00
virtual VOID ProcessPendingNotification(WDFQUEUE Queue) = 0;
virtual void DmfDeviceModulesAdd(_In_ PDMFMODULE_INIT DmfModuleInit) = 0;
//
// PNP Capabilities may differ from device to device
//
WDF_DEVICE_PNP_CAPABILITIES _PnpCapabilities;
//
// Power Capabilities may differ from device to device
//
WDF_DEVICE_POWER_CAPABILITIES _PowerCapabilities;
2020-08-29 15:52:39 +02:00
2020-05-10 22:57:05 +02:00
//
// Unique serial number of the device on the bus
//
2020-05-11 17:27:15 +02:00
ULONG _SerialNo{};
2020-05-10 22:57:05 +02:00
//
// PID of the process creating this PDO
//
2020-05-11 17:27:15 +02:00
DWORD _OwnerProcessId{};
//
// File object session ID
//
LONG _SessionId{};
2020-05-10 22:57:05 +02:00
//
// Device type this PDO is emulating
//
2020-05-11 17:27:15 +02:00
VIGEM_TARGET_TYPE _TargetType;
2020-05-10 22:57:05 +02:00
//
// If set, the vendor ID the emulated device is reporting
//
2020-05-11 17:27:15 +02:00
USHORT _VendorId{};
2020-05-10 22:57:05 +02:00
//
// If set, the product ID the emulated device is reporting
//
2020-05-11 17:27:15 +02:00
USHORT _ProductId{};
2020-05-10 22:57:05 +02:00
2020-05-13 16:36:30 +02:00
//
// Queue for blocking plugin requests
//
2020-08-27 20:30:12 +02:00
WDFQUEUE _WaitDeviceReadyRequests{};
2020-08-29 15:52:39 +02:00
2020-05-10 22:57:05 +02:00
//
// Queue for incoming data interrupt transfer
//
2020-05-11 17:27:15 +02:00
WDFQUEUE _PendingUsbInRequests{};
2020-05-10 22:57:05 +02:00
//
// Queue for inverted calls
//
2020-05-11 17:27:15 +02:00
WDFQUEUE _PendingNotificationRequests{};
2020-05-10 22:57:05 +02:00
//
// This child objects' device object
//
2020-05-11 17:27:15 +02:00
WDFDEVICE _PdoDevice{};
2020-08-29 15:52:39 +02:00
2020-05-10 22:57:05 +02:00
//
// Configuration descriptor size (populated by derived class)
2020-05-10 22:57:05 +02:00
//
ULONG _UsbConfigurationDescriptionSize{};
2020-05-11 00:17:25 +02:00
//
// Signals the bus that PDO is ready to receive data
2020-05-11 00:17:25 +02:00
//
KEVENT _PdoBootNotificationEvent;
2020-11-23 22:23:48 +01:00
//
// Queue for interrupt out requests delivered to user-land
//
DMFMODULE _UsbInterruptOutBufferQueue{};
//
// DMF event callbacks
//
DMF_EVENT_CALLBACKS _DmfEventCallbacks;
};
2020-05-10 21:19:39 +02:00
typedef struct _PDO_IDENTIFICATION_DESCRIPTION
{
2020-05-11 17:27:15 +02:00
//
// List entity header
//
2020-05-10 22:57:05 +02:00
WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER Header;
2020-05-11 00:24:57 +02:00
2020-05-11 17:27:15 +02:00
//
// Primary key to identify PDO
//
ULONG SerialNo;
//
// Session ID
//
LONG SessionId;
//
// Context object of PDO
//
2020-05-10 22:57:05 +02:00
EmulationTargetPDO* Target;
2020-08-29 15:52:39 +02:00
} PDO_IDENTIFICATION_DESCRIPTION, * PPDO_IDENTIFICATION_DESCRIPTION;
2020-05-10 22:57:05 +02:00
typedef struct _EMULATION_TARGET_PDO_CONTEXT
{
EmulationTargetPDO* Target;
2020-08-29 15:52:39 +02:00
} EMULATION_TARGET_PDO_CONTEXT, * PEMULATION_TARGET_PDO_CONTEXT;
2020-05-10 22:57:05 +02:00
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(EMULATION_TARGET_PDO_CONTEXT, EmulationTargetPdoGetContext)
}