From af930bc733977eb34376882c592e4418a681df9c Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Fri, 22 Apr 2022 07:05:41 -0700 Subject: [PATCH 1/7] make libusb submodule shallow --- .gitmodules | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitmodules b/.gitmodules index 8d7cfa8287..696e6e3d7e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,5 @@ [submodule "libusb"] path = Externals/libusb/libusb url = https://github.com/libusb/libusb.git + branch = master + shallow = true From 0a07c763331a4ea184f37d5ea7204572988a1f48 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Fri, 22 Apr 2022 07:07:20 -0700 Subject: [PATCH 2/7] update libusb submodule to latest --- Externals/libusb/libusb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Externals/libusb/libusb b/Externals/libusb/libusb index 4239bc3a50..ba698478af 160000 --- a/Externals/libusb/libusb +++ b/Externals/libusb/libusb @@ -1 +1 @@ -Subproject commit 4239bc3a50014b8e6a5a2a59df1fff3b7469543b +Subproject commit ba698478afc3d3a72644eef9fc4cd24ce8383a4c From f52d94832ecb65b549f784e8f42ffaaeec68a90c Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Fri, 22 Apr 2022 07:12:09 -0700 Subject: [PATCH 3/7] GCAdapter: set read/write thread names --- Source/Core/InputCommon/GCAdapter.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 973d1a1d5f..f96043d1c5 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -89,6 +89,8 @@ static std::array s_config_rumble_enable static void Read() { + Common::SetCurrentThreadName("GCAdapter Read Thread"); + int payload_size = 0; while (s_adapter_thread_running.IsSet()) { @@ -110,6 +112,8 @@ static void Read() static void Write() { + Common::SetCurrentThreadName("GCAdapter Write Thread"); + int size = 0; while (true) From 1c9dfb7bb6ed13766d92e09298f32bfa13ca6860 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Fri, 22 Apr 2022 07:36:56 -0700 Subject: [PATCH 4/7] GCAdapter: some macro cleanup --- Source/Core/InputCommon/GCAdapter.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index f96043d1c5..00a59caedc 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -25,6 +25,15 @@ #include "InputCommon/GCAdapter.h" #include "InputCommon/GCPadStatus.h" +#if defined(LIBUSB_API_VERSION) +#define LIBUSB_API_VERSION_EXIST 1 +#else +#define LIBUSB_API_VERSION_EXIST 0 +#endif + +#define LIBUSB_API_VERSION_ATLEAST(v) (LIBUSB_API_VERSION_EXIST && LIBUSB_API_VERSION >= (v)) +#define LIBUSB_API_HAS_HOTPLUG LIBUSB_API_VERSION_ATLEAST(0x01000102) + namespace GCAdapter { static bool CheckDeviceAccess(libusb_device* device); @@ -71,7 +80,7 @@ static bool s_libusb_hotplug_enabled = true; #else static bool s_libusb_hotplug_enabled = false; #endif -#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 +#if LIBUSB_API_HAS_HOTPLUG static libusb_hotplug_callback_handle s_hotplug_handle; #endif @@ -138,7 +147,7 @@ static void Write() } } -#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 +#if LIBUSB_API_HAS_HOTPLUG static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotplug_event event, void* user_data) { @@ -169,7 +178,7 @@ static void ScanThreadFunc() Common::SetCurrentThreadName("GC Adapter Scanning Thread"); NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread started"); -#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 +#if LIBUSB_API_HAS_HOTPLUG #ifndef __FreeBSD__ s_libusb_hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0; #endif @@ -402,7 +411,7 @@ static void AddGCAdapter(libusb_device* device) void Shutdown() { StopScanThread(); -#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102 +#if LIBUSB_API_HAS_HOTPLUG if (s_libusb_context->IsValid() && s_libusb_hotplug_enabled) libusb_hotplug_deregister_callback(*s_libusb_context, s_hotplug_handle); #endif From 978c90845bccfbe864473556cfafb854f901255f Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Fri, 22 Apr 2022 07:37:56 -0700 Subject: [PATCH 5/7] GCAdapter: move libusb context teardown last --- Source/Core/InputCommon/GCAdapter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 00a59caedc..fee15be7ba 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -415,8 +415,8 @@ void Shutdown() if (s_libusb_context->IsValid() && s_libusb_hotplug_enabled) libusb_hotplug_deregister_callback(*s_libusb_context, s_hotplug_handle); #endif - s_libusb_context.reset(); Reset(); + s_libusb_context.reset(); s_status = NO_ADAPTER_DETECTED; From 5cd3cf9072a6e75d5d047e66f6d3ce356ec9bf4f Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Fri, 22 Apr 2022 08:48:28 -0700 Subject: [PATCH 6/7] GCAdapter: fix retval check of libusb_detach_kernel_driver --- Source/Core/InputCommon/GCAdapter.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index fee15be7ba..6c6e4933bc 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -335,11 +335,13 @@ static bool CheckDeviceAccess(libusb_device* device) return false; } + bool detach_failed = false; ret = libusb_kernel_driver_active(s_handle, 0); if (ret == 1) { ret = libusb_detach_kernel_driver(s_handle, 0); - if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) + detach_failed = ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND && ret != LIBUSB_ERROR_NOT_SUPPORTED; + if (detach_failed) ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_detach_kernel_driver failed with error: {}", ret); } @@ -351,7 +353,7 @@ static bool CheckDeviceAccess(libusb_device* device) // this split is needed so that we don't avoid claiming the interface when // detaching the kernel driver is successful - if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) + if (detach_failed) { libusb_close(s_handle); s_handle = nullptr; From 12cd81bdb3e9d72f8a9abb430342ab9532e7dab1 Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Fri, 22 Apr 2022 08:58:38 -0700 Subject: [PATCH 7/7] GCAdapter: don't call libusb_detach_kernel_driver on apple --- Source/Core/InputCommon/GCAdapter.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/Core/InputCommon/GCAdapter.cpp b/Source/Core/InputCommon/GCAdapter.cpp index 6c6e4933bc..209594a59c 100644 --- a/Source/Core/InputCommon/GCAdapter.cpp +++ b/Source/Core/InputCommon/GCAdapter.cpp @@ -339,8 +339,12 @@ static bool CheckDeviceAccess(libusb_device* device) ret = libusb_kernel_driver_active(s_handle, 0); if (ret == 1) { + // On macos detaching would fail without root or entitlement. + // We assume user is using GCAdapterDriver and therefor don't want to detach anything +#if !defined(__APPLE__) ret = libusb_detach_kernel_driver(s_handle, 0); detach_failed = ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND && ret != LIBUSB_ERROR_NOT_SUPPORTED; +#endif if (detach_failed) ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_detach_kernel_driver failed with error: {}", ret); }