Skip to content

Commit

Permalink
backport offbrand adapter fix from mainline
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilNarayana committed Jul 11, 2020
1 parent 509f63b commit 9a7cb66
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions Source/Core/InputCommon/GCAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static Common::Event s_rumble_data_available;
static std::mutex s_init_mutex;
static std::thread s_adapter_detect_thread;
static Common::Flag s_adapter_detect_thread_running;
static Common::Event s_hotplug_event;

static std::function<void(void)> s_detect_callback;

Expand Down Expand Up @@ -162,11 +163,8 @@ static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotpl
{
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
{
if (s_handle == nullptr && CheckDeviceAccess(dev))
{
std::lock_guard<std::mutex> lk(s_init_mutex);
AddGCAdapter(dev);
}
if (s_handle == nullptr)
s_hotplug_event.Set();
}
else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
{
Expand Down Expand Up @@ -199,22 +197,16 @@ static void ScanThreadFunc()

while (s_adapter_detect_thread_running.IsSet())
{
if (s_libusb_hotplug_enabled)
if (s_handle == nullptr)
{
static timeval tv = { 0, 500000 };
libusb_handle_events_timeout(s_libusb_context, &tv);
std::lock_guard<std::mutex> lk(s_init_mutex);
Setup();
}

if (s_libusb_hotplug_enabled)
s_hotplug_event.Wait();
else
{
if (s_handle == nullptr)
{
std::lock_guard<std::mutex> lk(s_init_mutex);
Setup();
if (s_detected && s_detect_callback != nullptr)
s_detect_callback();
}
Common::SleepCurrentThread(500);
}
}
NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread stopped");
}
Expand Down Expand Up @@ -267,6 +259,7 @@ void StopScanThread()
{
if (s_adapter_detect_thread_running.TestAndClear())
{
s_hotplug_event.Set();
s_adapter_detect_thread.join();
}
}
Expand Down Expand Up @@ -349,6 +342,12 @@ static bool CheckDeviceAccess(libusb_device* device)
ERROR_LOG(SERIALINTERFACE, "libusb_detach_kernel_driver failed with error: %d", ret);
}
}
// This call makes Nyko-brand (and perhaps other) adapters work.
// However it returns LIBUSB_ERROR_PIPE with Mayflash adapters.
const int transfer = libusb_control_transfer(s_handle, 0x21, 11, 0x0001, 0, nullptr, 0, 1000);
if (transfer < 0)
WARN_LOG(SERIALINTERFACE, "libusb_control_transfer failed with error: %d", transfer);

// 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)
Expand Down Expand Up @@ -472,9 +471,9 @@ GCPadStatus Input(int chan)
if (payload_size != sizeof(controller_payload_copy) ||
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,
controller_payload_copy[0]);
Reset();
}
else
{
Expand Down

0 comments on commit 9a7cb66

Please sign in to comment.