diff --git a/sys/ByteArray.c b/sys/ByteArray.c deleted file mode 100644 index cba1fae..0000000 --- a/sys/ByteArray.c +++ /dev/null @@ -1,216 +0,0 @@ -/* -* Virtual Gamepad Emulation Framework - Windows kernel-mode bus driver -* -* MIT License -* -* Copyright (c) 2016-2020 Nefarius Software Solutions e.U. and Contributors -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -*/ - - -#include "ByteArray.h" - -// -// Helpers -// - -ULONG_PTR align_to_page_size(ULONG_PTR val) -{ - return (val + (PAGE_SIZE - 1)) & -PAGE_SIZE; -} - - -// -// Forward declarations -// - -NTSTATUS IncreaseCapacityByteArray(IN PBYTE_ARRAY Array, IN ULONG NumElements); - - -// -// Implementation -// - -NTSTATUS InitByteArray(IN OUT PBYTE_ARRAY Array) -{ - // - // Initialize size and default capacity - Array->Size = 0; - Array->Capacity = INITIAL_ARRAY_CAPACITY; - - // - // Allocate memory - Array->Data = (UCHAR*)ExAllocatePoolWithTag(PagedPool, Array->Capacity, ARRAY_POOL_TAG); - if (Array->Data == NULL) - return STATUS_INSUFFICIENT_RESOURCES; - - return STATUS_SUCCESS; -} - -NTSTATUS AppendElementByteArray(IN PBYTE_ARRAY Array, IN PVOID Element) -{ - // - // Make sure there is room to expand into - if (((Array->Size + 1) * sizeof(UCHAR)) > Array->Capacity) - { - // - // Increase capacity - NTSTATUS status = IncreaseCapacityByteArray(Array, sizeof(UCHAR)); - if (!NT_SUCCESS(status)) - return status; - } - - // - // Append the element and increment the size - RtlCopyMemory(Array->Data + (Array->Size * sizeof(UCHAR)), Element, sizeof(UCHAR)); - - // - // Increment size - Array->Size += 1; - - return STATUS_SUCCESS; -} - -NTSTATUS AppendElementsByteArray(IN PBYTE_ARRAY Array, IN PVOID Elements, IN ULONG NumElements) -{ - // - // Make sure there is room to expand into - if ((Array->Size + NumElements) * sizeof(UCHAR) > Array->Capacity) - { - // - // Increase capacity - NTSTATUS status = IncreaseCapacityByteArray(Array, NumElements); - if (!NT_SUCCESS(status)) - return status; - } - - // - // Append the elements and increase the size - RtlCopyMemory(Array->Data + (Array->Size * sizeof(UCHAR)), Elements, NumElements * sizeof(UCHAR)); - - // - // Increase size - Array->Size += NumElements; - - return STATUS_SUCCESS; -} - -NTSTATUS IncreaseCapacityByteArray(IN PBYTE_ARRAY Array, IN ULONG NumElements) -{ - UCHAR* NewData = NULL; - - // - // Align new size to the immediate next page boundary - Array->Capacity = align_to_page_size((Array->Size + NumElements) * sizeof(UCHAR)); - - // - // Allocate new data with new capacity - NewData = (UCHAR*)ExAllocatePoolWithTag(PagedPool, Array->Capacity, ARRAY_POOL_TAG); - if (NewData == NULL) - return STATUS_INSUFFICIENT_RESOURCES; - - // - // Copy old data over - RtlCopyMemory(NewData, Array->Data, Array->Size * sizeof(UCHAR)); - - // - // Free old data - ExFreePoolWithTag(Array->Data, ARRAY_POOL_TAG); - - // - // Set data pointer to new allocation - Array->Data = NewData; - - return STATUS_SUCCESS; -} - -NTSTATUS GetElementByteArray(IN PBYTE_ARRAY Array, IN ULONG Index, OUT PVOID Element) -{ - // - // Check array bounds - if (Index >= Array->Size || (LONG)Index < 0) - return STATUS_ARRAY_BOUNDS_EXCEEDED; - - // - // Copy data over - RtlCopyMemory(Element, Array->Data + (Index * sizeof(UCHAR)), sizeof(UCHAR)); - - return STATUS_SUCCESS; -} - -NTSTATUS GetElementsByteArray(IN PBYTE_ARRAY Array, IN ULONG Index, OUT PVOID Elements, IN ULONG NumElements) -{ - // - // Check array bounds - if (Index >= Array->Size || (LONG)Index < 0) - return STATUS_ARRAY_BOUNDS_EXCEEDED; - - // - // Copy data over - RtlCopyMemory(Elements, Array->Data + (Index * sizeof(UCHAR)), NumElements * sizeof(UCHAR)); - - return STATUS_SUCCESS; -} - -NTSTATUS SetElementByteArray(IN PBYTE_ARRAY Array, IN ULONG Index, IN PVOID Element) -{ - // - // Check array bounds - if (Index >= Array->Size || (LONG)Index < 0) - return STATUS_ARRAY_BOUNDS_EXCEEDED; - - // - // Copy data over - RtlCopyMemory(Array->Data + (Index * sizeof(UCHAR)), Element, sizeof(UCHAR)); - - return STATUS_SUCCESS; -} - -NTSTATUS SetElementsByteArray(IN PBYTE_ARRAY Array, IN ULONG Index, IN PVOID Elements, IN ULONG NumElements) -{ - // - // Check array bounds - if (Index >= Array->Size || (LONG)Index < 0) - return STATUS_ARRAY_BOUNDS_EXCEEDED; - - // - // Copy data over - RtlCopyMemory(Array->Data + (Index * sizeof(UCHAR)), Elements, NumElements * sizeof(UCHAR)); - - return STATUS_SUCCESS; -} - -NTSTATUS FreeByteArray(IN PBYTE_ARRAY Array) -{ - if (Array->Data == NULL) - return STATUS_MEMORY_NOT_ALLOCATED; - - // - // Free data - ExFreePoolWithTag(Array->Data, ARRAY_POOL_TAG); - - // - // Null out everything - Array->Data = NULL; - Array->Size = 0; - Array->Capacity = 0; - - return STATUS_SUCCESS; -} diff --git a/sys/ByteArray.h b/sys/ByteArray.h deleted file mode 100644 index 225af69..0000000 --- a/sys/ByteArray.h +++ /dev/null @@ -1,60 +0,0 @@ -/* -* Virtual Gamepad Emulation Framework - Windows kernel-mode bus driver -* -* MIT License -* -* Copyright (c) 2016-2020 Nefarius Software Solutions e.U. and Contributors -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -*/ - - -#pragma once - -#include - -EXTERN_C_START - -#define INITIAL_ARRAY_CAPACITY PAGE_SIZE -#define ARRAY_POOL_TAG 'arrA' - -typedef struct _BYTE_ARRAY -{ - UCHAR* Data; //> array of data we're storing - ULONG_PTR Size; //> slots used so far - ULONG_PTR Capacity; //> total available memory -} BYTE_ARRAY, *PBYTE_ARRAY; - -NTSTATUS InitByteArray(IN OUT PBYTE_ARRAY Array); - -NTSTATUS AppendElementByteArray(IN PBYTE_ARRAY Array, IN PVOID Element); - -NTSTATUS AppendElementsByteArray(IN PBYTE_ARRAY Array, IN PVOID Elements, IN ULONG NumElements); - -NTSTATUS GetElementByteArray(IN PBYTE_ARRAY Array, IN ULONG Index, OUT PVOID Element); - -NTSTATUS GetElementsByteArray(IN PBYTE_ARRAY Array, IN ULONG Index, OUT PVOID Elements, IN ULONG NumElements); - -NTSTATUS SetElementByteArray(IN PBYTE_ARRAY Array, IN ULONG Index, IN PVOID Element); - -NTSTATUS SetElementsByteArray(IN PBYTE_ARRAY Array, IN ULONG Index, IN PVOID Elements, IN ULONG NumElements); - -NTSTATUS FreeByteArray(IN PBYTE_ARRAY Array); - -EXTERN_C_END diff --git a/sys/Context.h b/sys/Context.h deleted file mode 100644 index dff79cb..0000000 --- a/sys/Context.h +++ /dev/null @@ -1,95 +0,0 @@ -/* -* Virtual Gamepad Emulation Framework - Windows kernel-mode bus driver -* -* MIT License -* -* Copyright (c) 2016-2020 Nefarius Software Solutions e.U. and Contributors -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -*/ - - -#pragma once - - - -// -// FDO (bus device) context data -// -typedef struct _FDO_DEVICE_DATA -{ - // - // Counter of interface references - // - LONG InterfaceReferenceCounter; - - // - // Next SessionId to assign to a file handle - // - LONG NextSessionId; - - // - // Periodic timer sweeping up orphaned requests - // - WDFTIMER PendingPluginRequestsCleanupTimer; - -} FDO_DEVICE_DATA, *PFDO_DEVICE_DATA; - -#define FDO_FIRST_SESSION_ID 100 - -WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(FDO_DEVICE_DATA, FdoGetData) - -// -// Context data associated with file objects created by user mode applications -// -typedef struct _FDO_FILE_DATA -{ - // - // SessionId associated with file handle. Used to map file handles to emulated gamepad devices - // - LONG SessionId; - -} FDO_FILE_DATA, *PFDO_FILE_DATA; - -WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(FDO_FILE_DATA, FileObjectGetData) - -// -// Context data for plugin requests -// -typedef struct _FDO_PLUGIN_REQUEST_DATA -{ - // - // Unique serial number of the device on the bus - // - ULONG Serial; - - // - // High resolution timestamp taken when this request got moved to pending state - // - LARGE_INTEGER Timestamp; - - // - // Performance counter system frequency taken upon fetching timestamp - // - LARGE_INTEGER Frequency; - -} FDO_PLUGIN_REQUEST_DATA, *PFDO_PLUGIN_REQUEST_DATA; - -WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(FDO_PLUGIN_REQUEST_DATA, PluginRequestGetData) - diff --git a/sys/Util.h b/sys/Util.h index 78d10ec..958333a 100644 --- a/sys/Util.h +++ b/sys/Util.h @@ -27,13 +27,6 @@ #pragma once -// -// Returns the current caller process id. -// -#define CURRENT_PROCESS_ID() ((DWORD)((DWORD_PTR)PsGetCurrentProcessId() & 0xFFFFFFFF)) - -#define IS_OWNER(_pdo_) (_pdo_->OwnerProcessId == CURRENT_PROCESS_ID()) - // // Represents a MAC address. // diff --git a/sys/ViGEmBus.vcxproj b/sys/ViGEmBus.vcxproj index fc5cb8c..ce0016e 100644 --- a/sys/ViGEmBus.vcxproj +++ b/sys/ViGEmBus.vcxproj @@ -188,8 +188,6 @@ - - @@ -205,7 +203,6 @@ - @@ -213,9 +210,6 @@ - - - diff --git a/sys/ViGEmBus.vcxproj.filters b/sys/ViGEmBus.vcxproj.filters index e585b1a..100aa27 100644 --- a/sys/ViGEmBus.vcxproj.filters +++ b/sys/ViGEmBus.vcxproj.filters @@ -39,15 +39,9 @@ Header Files - - Header Files - Header Files - - Header Files - Header Files\Common @@ -80,9 +74,6 @@ Source Files - - Source Files - Source Files\Targets @@ -110,7 +101,4 @@ Resource Files - - - \ No newline at end of file diff --git a/sys/busenum.h b/sys/busenum.h index 07b55d2..6381911 100644 --- a/sys/busenum.h +++ b/sys/busenum.h @@ -39,27 +39,51 @@ #include "Queue.hpp" #include #include -#include "Context.h" #include "Util.h" #pragma region Macros -#define HID_LANGUAGE_ID_LENGTH 0x04 - - - #define VIGEM_POOL_TAG 0x45476956 // "EGiV" #define DRIVERNAME "ViGEm: " -#define ORC_PC_FREQUENCY_DIVIDER 1000 -#define ORC_TIMER_START_DELAY 500 // ms -#define ORC_TIMER_PERIODIC_DUE_TIME 500 // ms -#define ORC_REQUEST_MAX_AGE 500 // ms - #pragma endregion +// +// FDO (bus device) context data +// +typedef struct _FDO_DEVICE_DATA +{ + // + // Counter of interface references + // + LONG InterfaceReferenceCounter; + + // + // Next SessionId to assign to a file handle + // + LONG NextSessionId; + +} FDO_DEVICE_DATA, * PFDO_DEVICE_DATA; + +#define FDO_FIRST_SESSION_ID 100 + +WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(FDO_DEVICE_DATA, FdoGetData) + +// +// Context data associated with file objects created by user mode applications +// +typedef struct _FDO_FILE_DATA +{ + // + // SessionId associated with file handle. Used to map file handles to emulated gamepad devices + // + LONG SessionId; + +} FDO_FILE_DATA, * PFDO_FILE_DATA; + +WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(FDO_FILE_DATA, FileObjectGetData) EXTERN_C_START @@ -78,10 +102,6 @@ EVT_WDF_CHILD_LIST_CREATE_DEVICE Bus_EvtDeviceListCreatePdo; EVT_WDF_CHILD_LIST_IDENTIFICATION_DESCRIPTION_COMPARE Bus_EvtChildListIdentificationDescriptionCompare; -EVT_WDF_DEVICE_PREPARE_HARDWARE Pdo_EvtDevicePrepareHardware; - -EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL Pdo_EvtIoInternalDeviceControl; - EVT_WDF_OBJECT_CONTEXT_CLEANUP Bus_EvtDriverContextCleanup; #pragma endregion