InputCommon/GCAdapter: Fix offbrand "GCAdapters" with a libusb_control_transfer call.

This commit is contained in:
Jordan Woyak 2020-07-04 20:03:20 -05:00
parent 4f98653ec6
commit f2fd5c7f90
1 changed files with 16 additions and 16 deletions

View File

@ -38,7 +38,7 @@ enum
}; };
// Current adapter status: detected/not detected/in error (holds the error code) // Current adapter status: detected/not detected/in error (holds the error code)
static int s_status = NO_ADAPTER_DETECTED; static std::atomic<int> s_status = NO_ADAPTER_DETECTED;
static libusb_device_handle* s_handle = nullptr; static libusb_device_handle* s_handle = nullptr;
static u8 s_controller_type[SerialInterface::MAX_SI_CHANNELS] = { static u8 s_controller_type[SerialInterface::MAX_SI_CHANNELS] = {
ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE,
@ -60,6 +60,7 @@ static Common::Event s_rumble_data_available;
static std::mutex s_init_mutex; static std::mutex s_init_mutex;
static std::thread s_adapter_detect_thread; static std::thread s_adapter_detect_thread;
static Common::Flag s_adapter_detect_thread_running; static Common::Flag s_adapter_detect_thread_running;
static Common::Event s_hotplug_event;
static std::function<void(void)> s_detect_callback; static std::function<void(void)> s_detect_callback;
@ -125,15 +126,8 @@ static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotpl
{ {
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
{ {
if (s_handle == nullptr && CheckDeviceAccess(dev)) if (s_handle == nullptr)
{ s_hotplug_event.Set();
std::lock_guard<std::mutex> lk(s_init_mutex);
AddGCAdapter(dev);
}
else if (s_status < 0 && s_detect_callback != nullptr)
{
s_detect_callback();
}
} }
else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
{ {
@ -175,9 +169,6 @@ static void ScanThreadFunc()
} }
#endif #endif
if (s_libusb_hotplug_enabled)
return;
while (s_adapter_detect_thread_running.IsSet()) while (s_adapter_detect_thread_running.IsSet())
{ {
if (s_handle == nullptr) if (s_handle == nullptr)
@ -185,6 +176,10 @@ static void ScanThreadFunc()
std::lock_guard<std::mutex> lk(s_init_mutex); std::lock_guard<std::mutex> lk(s_init_mutex);
Setup(); Setup();
} }
if (s_libusb_hotplug_enabled)
s_hotplug_event.Wait();
else
Common::SleepCurrentThread(500); Common::SleepCurrentThread(500);
} }
NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread stopped"); NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread stopped");
@ -228,6 +223,7 @@ void StopScanThread()
{ {
if (s_adapter_detect_thread_running.TestAndClear()) if (s_adapter_detect_thread_running.TestAndClear())
{ {
s_hotplug_event.Set();
s_adapter_detect_thread.join(); s_adapter_detect_thread.join();
} }
} }
@ -307,6 +303,10 @@ static bool CheckDeviceAccess(libusb_device* device)
ERROR_LOG(SERIALINTERFACE, "libusb_detach_kernel_driver failed with error: %d", ret); ERROR_LOG(SERIALINTERFACE, "libusb_detach_kernel_driver failed with error: %d", ret);
} }
ret = libusb_control_transfer(s_handle, 0x21, 11, 0x0001, 0, nullptr, 0, 10000);
if (ret < 0)
ERROR_LOG(SERIALINTERFACE, "libusb_control_transfer failed with error: %d", ret);
// this split is needed so that we don't avoid claiming the interface when // this split is needed so that we don't avoid claiming the interface when
// detaching the kernel driver is successful // detaching the kernel driver is successful
if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED) if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED)
@ -431,9 +431,9 @@ GCPadStatus Input(int chan)
if (payload_size != sizeof(controller_payload_copy) || if (payload_size != sizeof(controller_payload_copy) ||
controller_payload_copy[0] != LIBUSB_DT_HID) controller_payload_copy[0] != LIBUSB_DT_HID)
{ {
// This can occur for a few frames on initialization.
ERROR_LOG(SERIALINTERFACE, "error reading payload (size: %d, type: %02x)", payload_size, ERROR_LOG(SERIALINTERFACE, "error reading payload (size: %d, type: %02x)", payload_size,
controller_payload_copy[0]); controller_payload_copy[0]);
Reset();
} }
else else
{ {
@ -575,7 +575,7 @@ bool IsDetected(const char** error_message)
} }
if (error_message) if (error_message)
*error_message = libusb_strerror(static_cast<libusb_error>(s_status)); *error_message = libusb_strerror(static_cast<libusb_error>(s_status.load()));
return false; return false;
} }