2015-05-24 06:55:12 +02:00
|
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-12-10 10:45:45 +01:00
|
|
|
|
|
2015-11-08 18:13:58 +01:00
|
|
|
|
#include <algorithm>
|
2014-12-10 10:45:45 +01:00
|
|
|
|
#include <libusb.h>
|
2015-05-27 05:44:51 +02:00
|
|
|
|
#include <mutex>
|
2014-12-10 10:45:45 +01:00
|
|
|
|
|
2017-07-22 20:25:11 +02:00
|
|
|
|
#include "Common/Event.h"
|
2015-05-08 23:28:03 +02:00
|
|
|
|
#include "Common/Flag.h"
|
2015-12-31 18:09:47 +01:00
|
|
|
|
#include "Common/Logging/Log.h"
|
2018-07-17 22:31:18 +02:00
|
|
|
|
#include "Common/ScopeGuard.h"
|
2015-05-08 23:28:03 +02:00
|
|
|
|
#include "Common/Thread.h"
|
2014-12-10 10:45:45 +01:00
|
|
|
|
#include "Core/ConfigManager.h"
|
2014-12-24 00:49:53 +01:00
|
|
|
|
#include "Core/Core.h"
|
|
|
|
|
#include "Core/CoreTiming.h"
|
2017-01-21 00:45:11 +01:00
|
|
|
|
#include "Core/HW/SI/SI.h"
|
2019-06-08 00:25:32 +02:00
|
|
|
|
#include "Core/HW/SI/SI_Device.h"
|
2014-12-24 00:49:53 +01:00
|
|
|
|
#include "Core/HW/SystemTimers.h"
|
2019-05-12 20:19:31 +02:00
|
|
|
|
#include "Core/LibusbUtils.h"
|
2016-05-12 02:07:52 +02:00
|
|
|
|
#include "Core/NetPlayProto.h"
|
2015-12-31 18:09:47 +01:00
|
|
|
|
|
|
|
|
|
#include "InputCommon/GCAdapter.h"
|
2015-05-08 23:28:03 +02:00
|
|
|
|
#include "InputCommon/GCPadStatus.h"
|
2014-12-10 10:45:45 +01:00
|
|
|
|
|
2015-12-31 18:09:47 +01:00
|
|
|
|
namespace GCAdapter
|
2014-12-10 10:45:45 +01:00
|
|
|
|
{
|
2015-04-26 23:53:36 +02:00
|
|
|
|
static bool CheckDeviceAccess(libusb_device* device);
|
|
|
|
|
static void AddGCAdapter(libusb_device* device);
|
2016-04-30 13:29:11 +02:00
|
|
|
|
static void ResetRumbleLockNeeded();
|
2016-05-19 21:52:44 +02:00
|
|
|
|
static void Reset();
|
|
|
|
|
static void Setup();
|
2015-04-26 23:53:36 +02:00
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
NO_ADAPTER_DETECTED = 0,
|
|
|
|
|
ADAPTER_DETECTED = 1,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Current adapter status: detected/not detected/in error (holds the error code)
|
2020-07-05 03:03:20 +02:00
|
|
|
|
static std::atomic<int> s_status = NO_ADAPTER_DETECTED;
|
2014-12-11 01:15:36 +01:00
|
|
|
|
static libusb_device_handle* s_handle = nullptr;
|
2019-02-01 19:47:07 +01:00
|
|
|
|
static std::array<u8, SerialInterface::MAX_SI_CHANNELS> s_controller_type = {
|
2016-01-06 07:57:08 +01:00
|
|
|
|
ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE,
|
|
|
|
|
ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE};
|
2019-02-01 19:47:07 +01:00
|
|
|
|
static std::array<u8, SerialInterface::MAX_SI_CHANNELS> s_controller_rumble{};
|
2014-12-10 10:45:45 +01:00
|
|
|
|
|
2014-12-11 01:15:36 +01:00
|
|
|
|
static std::mutex s_mutex;
|
|
|
|
|
static u8 s_controller_payload[37];
|
2015-01-05 02:43:26 +01:00
|
|
|
|
static u8 s_controller_payload_swap[37];
|
|
|
|
|
|
2016-01-23 18:30:03 +01:00
|
|
|
|
static std::atomic<int> s_controller_payload_size = {0};
|
2014-12-10 10:45:45 +01:00
|
|
|
|
|
2017-07-22 20:25:11 +02:00
|
|
|
|
static std::thread s_adapter_input_thread;
|
|
|
|
|
static std::thread s_adapter_output_thread;
|
2014-12-11 01:15:36 +01:00
|
|
|
|
static Common::Flag s_adapter_thread_running;
|
2014-12-10 10:45:45 +01:00
|
|
|
|
|
2017-07-22 20:25:11 +02:00
|
|
|
|
static Common::Event s_rumble_data_available;
|
|
|
|
|
|
2016-04-30 13:29:11 +02:00
|
|
|
|
static std::mutex s_init_mutex;
|
2015-04-26 23:53:36 +02:00
|
|
|
|
static std::thread s_adapter_detect_thread;
|
|
|
|
|
static Common::Flag s_adapter_detect_thread_running;
|
2020-07-05 03:03:20 +02:00
|
|
|
|
static Common::Event s_hotplug_event;
|
2015-04-26 23:53:36 +02:00
|
|
|
|
|
|
|
|
|
static std::function<void(void)> s_detect_callback;
|
|
|
|
|
|
2016-12-27 07:50:36 +01:00
|
|
|
|
#if defined(__FreeBSD__) && __FreeBSD__ >= 11
|
|
|
|
|
static bool s_libusb_hotplug_enabled = true;
|
|
|
|
|
#else
|
2015-04-26 23:53:36 +02:00
|
|
|
|
static bool s_libusb_hotplug_enabled = false;
|
2016-12-27 07:50:36 +01:00
|
|
|
|
#endif
|
2016-01-03 09:53:31 +01:00
|
|
|
|
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
2015-04-26 23:53:36 +02:00
|
|
|
|
static libusb_hotplug_callback_handle s_hotplug_handle;
|
2016-01-03 09:53:31 +01:00
|
|
|
|
#endif
|
2014-12-10 10:45:45 +01:00
|
|
|
|
|
2019-06-25 17:31:35 +02:00
|
|
|
|
static LibusbUtils::Context s_libusb_context;
|
|
|
|
|
|
2014-12-11 01:15:36 +01:00
|
|
|
|
static u8 s_endpoint_in = 0;
|
|
|
|
|
static u8 s_endpoint_out = 0;
|
2014-12-10 11:35:30 +01:00
|
|
|
|
|
2014-12-24 00:49:53 +01:00
|
|
|
|
static u64 s_last_init = 0;
|
|
|
|
|
|
2015-01-05 02:43:26 +01:00
|
|
|
|
static void Read()
|
|
|
|
|
{
|
2016-01-23 18:30:03 +01:00
|
|
|
|
int payload_size = 0;
|
2015-01-05 02:43:26 +01:00
|
|
|
|
while (s_adapter_thread_running.IsSet())
|
|
|
|
|
{
|
2020-05-10 09:26:31 +02:00
|
|
|
|
int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap,
|
|
|
|
|
sizeof(s_controller_payload_swap), &payload_size, 16);
|
|
|
|
|
if (err)
|
2021-05-04 22:37:06 +02:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}",
|
|
|
|
|
libusb_error_name(err));
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-11 01:15:36 +01:00
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lk(s_mutex);
|
2015-01-05 02:43:26 +01:00
|
|
|
|
std::swap(s_controller_payload_swap, s_controller_payload);
|
2016-01-23 18:30:03 +01:00
|
|
|
|
s_controller_payload_size.store(payload_size);
|
2014-12-11 01:15:36 +01:00
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-10 10:45:45 +01:00
|
|
|
|
Common::YieldCPU();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-22 20:25:11 +02:00
|
|
|
|
static void Write()
|
|
|
|
|
{
|
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
s_rumble_data_available.Wait();
|
|
|
|
|
|
|
|
|
|
if (!s_adapter_thread_running.IsSet())
|
|
|
|
|
return;
|
|
|
|
|
|
2020-10-22 23:49:29 +02:00
|
|
|
|
u8 payload[5] = {
|
|
|
|
|
0x11,
|
|
|
|
|
s_controller_rumble[0],
|
|
|
|
|
s_controller_rumble[1],
|
|
|
|
|
s_controller_rumble[2],
|
|
|
|
|
s_controller_rumble[3],
|
|
|
|
|
};
|
|
|
|
|
const int err =
|
2020-05-10 09:26:31 +02:00
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, payload, sizeof(payload), &size, 16);
|
2020-10-22 23:49:29 +02:00
|
|
|
|
if (err != 0)
|
2021-05-04 22:37:06 +02:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}",
|
|
|
|
|
libusb_error_name(err));
|
2017-07-22 20:25:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-03 09:53:31 +01:00
|
|
|
|
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
2015-04-26 23:53:36 +02:00
|
|
|
|
static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotplug_event event,
|
|
|
|
|
void* user_data)
|
|
|
|
|
{
|
|
|
|
|
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
|
|
|
|
|
{
|
2020-07-05 03:03:20 +02:00
|
|
|
|
if (s_handle == nullptr)
|
|
|
|
|
s_hotplug_event.Set();
|
2015-04-26 23:53:36 +02:00
|
|
|
|
}
|
|
|
|
|
else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
|
|
|
|
|
{
|
|
|
|
|
if (s_handle != nullptr && libusb_get_device(s_handle) == dev)
|
|
|
|
|
Reset();
|
2018-07-17 22:31:18 +02:00
|
|
|
|
|
|
|
|
|
// Reset a potential error status now that the adapter is unplugged
|
|
|
|
|
if (s_status < 0)
|
2018-07-19 08:23:16 +02:00
|
|
|
|
{
|
|
|
|
|
s_status = NO_ADAPTER_DETECTED;
|
|
|
|
|
if (s_detect_callback != nullptr)
|
|
|
|
|
s_detect_callback();
|
|
|
|
|
}
|
2015-04-26 23:53:36 +02:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-01-03 09:53:31 +01:00
|
|
|
|
#endif
|
2015-04-26 23:53:36 +02:00
|
|
|
|
|
|
|
|
|
static void ScanThreadFunc()
|
|
|
|
|
{
|
|
|
|
|
Common::SetCurrentThreadName("GC Adapter Scanning Thread");
|
2021-05-04 22:37:06 +02:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread started");
|
2015-04-26 23:53:36 +02:00
|
|
|
|
|
2016-01-03 09:53:31 +01:00
|
|
|
|
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
2016-12-27 07:50:36 +01:00
|
|
|
|
#ifndef __FreeBSD__
|
2015-04-26 23:53:36 +02:00
|
|
|
|
s_libusb_hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0;
|
2016-12-27 07:50:36 +01:00
|
|
|
|
#endif
|
2015-04-26 23:53:36 +02:00
|
|
|
|
if (s_libusb_hotplug_enabled)
|
|
|
|
|
{
|
2015-10-03 04:06:17 +02:00
|
|
|
|
if (libusb_hotplug_register_callback(
|
2019-06-25 17:31:35 +02:00
|
|
|
|
s_libusb_context,
|
2017-03-26 14:51:12 +02:00
|
|
|
|
(libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
|
|
|
|
|
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
|
2015-10-03 04:06:17 +02:00
|
|
|
|
LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback,
|
|
|
|
|
nullptr, &s_hotplug_handle) != LIBUSB_SUCCESS)
|
2015-04-26 23:53:36 +02:00
|
|
|
|
s_libusb_hotplug_enabled = false;
|
|
|
|
|
if (s_libusb_hotplug_enabled)
|
2021-05-04 22:37:06 +02:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Using libUSB hotplug detection");
|
2015-04-26 23:53:36 +02:00
|
|
|
|
}
|
2016-01-03 09:53:31 +01:00
|
|
|
|
#endif
|
2015-04-26 23:53:36 +02:00
|
|
|
|
|
|
|
|
|
while (s_adapter_detect_thread_running.IsSet())
|
|
|
|
|
{
|
2019-05-12 20:19:31 +02:00
|
|
|
|
if (s_handle == nullptr)
|
2015-04-26 23:53:36 +02:00
|
|
|
|
{
|
2019-05-12 20:19:31 +02:00
|
|
|
|
std::lock_guard<std::mutex> lk(s_init_mutex);
|
|
|
|
|
Setup();
|
2015-04-26 23:53:36 +02:00
|
|
|
|
}
|
2020-07-05 03:03:20 +02:00
|
|
|
|
|
|
|
|
|
if (s_libusb_hotplug_enabled)
|
|
|
|
|
s_hotplug_event.Wait();
|
|
|
|
|
else
|
|
|
|
|
Common::SleepCurrentThread(500);
|
2015-04-26 23:53:36 +02:00
|
|
|
|
}
|
2021-05-04 22:37:06 +02:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread stopped");
|
2015-04-26 23:53:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:49:29 +02:00
|
|
|
|
void SetAdapterCallback(std::function<void()> func)
|
2015-04-26 23:53:36 +02:00
|
|
|
|
{
|
|
|
|
|
s_detect_callback = func;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-10 10:45:45 +01:00
|
|
|
|
void Init()
|
|
|
|
|
{
|
2014-12-11 01:15:36 +01:00
|
|
|
|
if (s_handle != nullptr)
|
2014-12-10 10:45:45 +01:00
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2017-07-01 23:08:58 +02:00
|
|
|
|
if (Core::GetState() != Core::State::Uninitialized && Core::GetState() != Core::State::Starting)
|
2014-12-24 00:49:53 +01:00
|
|
|
|
{
|
|
|
|
|
if ((CoreTiming::GetTicks() - s_last_init) < SystemTimers::GetTicksPerSecond())
|
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-24 00:49:53 +01:00
|
|
|
|
s_last_init = CoreTiming::GetTicks();
|
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
s_status = NO_ADAPTER_DETECTED;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2017-02-06 23:04:14 +01:00
|
|
|
|
if (UseAdapter())
|
|
|
|
|
StartScanThread();
|
2015-04-26 23:53:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StartScanThread()
|
|
|
|
|
{
|
2015-12-31 18:09:47 +01:00
|
|
|
|
if (s_adapter_detect_thread_running.IsSet())
|
|
|
|
|
return;
|
2019-06-25 17:31:35 +02:00
|
|
|
|
if (!s_libusb_context.IsValid())
|
2017-02-06 23:04:14 +01:00
|
|
|
|
return;
|
2015-04-26 23:53:36 +02:00
|
|
|
|
s_adapter_detect_thread_running.Set(true);
|
|
|
|
|
s_adapter_detect_thread = std::thread(ScanThreadFunc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StopScanThread()
|
|
|
|
|
{
|
|
|
|
|
if (s_adapter_detect_thread_running.TestAndClear())
|
|
|
|
|
{
|
2020-07-05 03:03:20 +02:00
|
|
|
|
s_hotplug_event.Set();
|
2015-04-26 23:53:36 +02:00
|
|
|
|
s_adapter_detect_thread.join();
|
2015-01-26 13:54:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:52:44 +02:00
|
|
|
|
static void Setup()
|
2015-01-26 13:54:54 +01:00
|
|
|
|
{
|
2018-07-25 21:44:57 +02:00
|
|
|
|
int prev_status = s_status;
|
|
|
|
|
|
|
|
|
|
// Reset the error status in case the adapter gets unplugged
|
|
|
|
|
if (s_status < 0)
|
|
|
|
|
s_status = NO_ADAPTER_DETECTED;
|
|
|
|
|
|
2019-02-01 19:47:07 +01:00
|
|
|
|
s_controller_type.fill(ControllerTypes::CONTROLLER_NONE);
|
|
|
|
|
s_controller_rumble.fill(0);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2019-06-25 17:31:35 +02:00
|
|
|
|
s_libusb_context.GetDeviceList([](libusb_device* device) {
|
2015-04-26 23:53:36 +02:00
|
|
|
|
if (CheckDeviceAccess(device))
|
2016-01-20 22:20:17 +01:00
|
|
|
|
{
|
|
|
|
|
// Only connect to a single adapter in case the user has multiple connected
|
2015-04-26 23:53:36 +02:00
|
|
|
|
AddGCAdapter(device);
|
2019-05-12 20:19:31 +02:00
|
|
|
|
return false;
|
2016-01-20 22:20:17 +01:00
|
|
|
|
}
|
2019-05-12 20:19:31 +02:00
|
|
|
|
return true;
|
|
|
|
|
});
|
2018-07-25 21:44:57 +02:00
|
|
|
|
|
|
|
|
|
if (s_status != ADAPTER_DETECTED && prev_status != s_status && s_detect_callback != nullptr)
|
|
|
|
|
s_detect_callback();
|
2015-04-26 23:53:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool CheckDeviceAccess(libusb_device* device)
|
|
|
|
|
{
|
|
|
|
|
libusb_device_descriptor desc;
|
2019-01-15 22:01:48 +01:00
|
|
|
|
int ret = libusb_get_device_descriptor(device, &desc);
|
2020-10-22 23:49:29 +02:00
|
|
|
|
if (ret != 0)
|
2015-04-26 23:53:36 +02:00
|
|
|
|
{
|
|
|
|
|
// could not acquire the descriptor, no point in trying to use it.
|
2021-05-04 22:37:06 +02:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_device_descriptor failed with error: {}", ret);
|
2015-04-26 23:53:36 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2019-01-15 22:01:48 +01:00
|
|
|
|
if (desc.idVendor != 0x057e || desc.idProduct != 0x0337)
|
2015-04-26 23:53:36 +02:00
|
|
|
|
{
|
2019-01-15 22:01:48 +01:00
|
|
|
|
// This isn’t the device we are looking for.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2021-05-04 22:37:06 +02:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Found GC Adapter with Vendor: {:X} Product: {:X} Devnum: {}",
|
2020-10-22 23:49:29 +02:00
|
|
|
|
desc.idVendor, desc.idProduct, 1);
|
2019-01-15 22:01:48 +01:00
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
// In case of failure, capture the libusb error code into the adapter status
|
|
|
|
|
Common::ScopeGuard status_guard([&ret] { s_status = ret; });
|
|
|
|
|
|
2020-10-22 23:49:29 +02:00
|
|
|
|
const u8 bus = libusb_get_bus_number(device);
|
|
|
|
|
const u8 port = libusb_get_device_address(device);
|
2019-01-15 22:01:48 +01:00
|
|
|
|
ret = libusb_open(device, &s_handle);
|
|
|
|
|
if (ret == LIBUSB_ERROR_ACCESS)
|
|
|
|
|
{
|
2020-10-22 23:49:29 +02:00
|
|
|
|
ERROR_LOG_FMT(
|
2021-05-04 22:37:06 +02:00
|
|
|
|
CONTROLLERINTERFACE,
|
2020-10-22 23:49:29 +02:00
|
|
|
|
"Dolphin does not have access to this device: Bus {:03d} Device {:03d}: ID {:04X}:{:04X}.",
|
|
|
|
|
bus, port, desc.idVendor, desc.idProduct);
|
2019-01-15 22:01:48 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-10-22 23:49:29 +02:00
|
|
|
|
if (ret != 0)
|
2019-01-15 22:01:48 +01:00
|
|
|
|
{
|
2021-05-04 22:37:06 +02:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_open failed to open device with error = {}", ret);
|
2019-01-15 22:01:48 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = libusb_kernel_driver_active(s_handle, 0);
|
|
|
|
|
if (ret == 1)
|
|
|
|
|
{
|
|
|
|
|
ret = libusb_detach_kernel_driver(s_handle, 0);
|
2015-04-26 23:53:36 +02:00
|
|
|
|
if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED)
|
2021-05-04 22:37:06 +02:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_detach_kernel_driver failed with error: {}", ret);
|
2019-01-15 22:01:48 +01:00
|
|
|
|
}
|
2017-06-07 13:42:41 +02:00
|
|
|
|
|
2020-07-10 00:17:44 +02:00
|
|
|
|
// 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)
|
2021-05-04 22:37:06 +02:00
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_control_transfer failed with error: {}", transfer);
|
2020-07-05 03:03:20 +02:00
|
|
|
|
|
2019-01-15 22:01:48 +01:00
|
|
|
|
// 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)
|
2018-07-17 22:29:48 +02:00
|
|
|
|
{
|
|
|
|
|
libusb_close(s_handle);
|
|
|
|
|
s_handle = nullptr;
|
2019-01-15 22:01:48 +01:00
|
|
|
|
return false;
|
2018-07-17 22:29:48 +02:00
|
|
|
|
}
|
2019-01-15 22:01:48 +01:00
|
|
|
|
|
|
|
|
|
ret = libusb_claim_interface(s_handle, 0);
|
2020-10-22 23:49:29 +02:00
|
|
|
|
if (ret != 0)
|
2019-01-15 22:01:48 +01:00
|
|
|
|
{
|
2021-05-04 22:37:06 +02:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_claim_interface failed with error: {}", ret);
|
2018-07-17 22:29:48 +02:00
|
|
|
|
libusb_close(s_handle);
|
|
|
|
|
s_handle = nullptr;
|
2019-01-15 22:01:48 +01:00
|
|
|
|
return false;
|
2015-01-26 13:54:54 +01:00
|
|
|
|
}
|
2019-01-15 22:01:48 +01:00
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
// Updating the adapter status will be done in AddGCAdapter
|
|
|
|
|
status_guard.Dismiss();
|
|
|
|
|
|
2019-01-15 22:01:48 +01:00
|
|
|
|
return true;
|
2015-01-26 13:54:54 +01:00
|
|
|
|
}
|
2015-01-05 02:43:26 +01:00
|
|
|
|
|
2015-04-26 23:53:36 +02:00
|
|
|
|
static void AddGCAdapter(libusb_device* device)
|
2015-01-26 13:54:54 +01:00
|
|
|
|
{
|
|
|
|
|
libusb_config_descriptor* config = nullptr;
|
|
|
|
|
libusb_get_config_descriptor(device, 0, &config);
|
|
|
|
|
for (u8 ic = 0; ic < config->bNumInterfaces; ic++)
|
|
|
|
|
{
|
|
|
|
|
const libusb_interface* interfaceContainer = &config->interface[ic];
|
|
|
|
|
for (int i = 0; i < interfaceContainer->num_altsetting; i++)
|
|
|
|
|
{
|
|
|
|
|
const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i];
|
|
|
|
|
for (u8 e = 0; e < interface->bNumEndpoints; e++)
|
|
|
|
|
{
|
|
|
|
|
const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e];
|
|
|
|
|
if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN)
|
|
|
|
|
s_endpoint_in = endpoint->bEndpointAddress;
|
|
|
|
|
else
|
|
|
|
|
s_endpoint_out = endpoint->bEndpointAddress;
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-26 13:54:54 +01:00
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2015-01-26 13:54:54 +01:00
|
|
|
|
int tmp = 0;
|
|
|
|
|
unsigned char payload = 0x13;
|
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, &payload, sizeof(payload), &tmp, 16);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2015-03-05 13:30:43 +01:00
|
|
|
|
s_adapter_thread_running.Set(true);
|
2017-07-22 20:25:11 +02:00
|
|
|
|
s_adapter_input_thread = std::thread(Read);
|
|
|
|
|
s_adapter_output_thread = std::thread(Write);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
s_status = ADAPTER_DETECTED;
|
2015-04-26 23:53:36 +02:00
|
|
|
|
if (s_detect_callback != nullptr)
|
|
|
|
|
s_detect_callback();
|
2016-04-30 13:29:11 +02:00
|
|
|
|
ResetRumbleLockNeeded();
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Shutdown()
|
2015-01-26 13:54:54 +01:00
|
|
|
|
{
|
2015-04-26 23:53:36 +02:00
|
|
|
|
StopScanThread();
|
2016-01-03 09:53:31 +01:00
|
|
|
|
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
2019-06-25 17:31:35 +02:00
|
|
|
|
if (s_libusb_context.IsValid() && s_libusb_hotplug_enabled)
|
|
|
|
|
libusb_hotplug_deregister_callback(s_libusb_context, s_hotplug_handle);
|
2016-01-03 09:53:31 +01:00
|
|
|
|
#endif
|
2015-01-26 13:54:54 +01:00
|
|
|
|
Reset();
|
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
s_status = NO_ADAPTER_DETECTED;
|
2015-01-26 13:54:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:52:44 +02:00
|
|
|
|
static void Reset()
|
2014-12-10 10:45:45 +01:00
|
|
|
|
{
|
2016-04-30 13:29:11 +02:00
|
|
|
|
std::unique_lock<std::mutex> lock(s_init_mutex, std::defer_lock);
|
|
|
|
|
if (!lock.try_lock())
|
|
|
|
|
return;
|
2018-07-17 22:31:18 +02:00
|
|
|
|
if (s_status != ADAPTER_DETECTED)
|
2014-12-10 10:45:45 +01:00
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-11 01:15:36 +01:00
|
|
|
|
if (s_adapter_thread_running.TestAndClear())
|
2014-12-10 10:45:45 +01:00
|
|
|
|
{
|
2017-07-22 20:25:11 +02:00
|
|
|
|
s_rumble_data_available.Set();
|
|
|
|
|
s_adapter_input_thread.join();
|
|
|
|
|
s_adapter_output_thread.join();
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2019-02-01 19:47:07 +01:00
|
|
|
|
s_controller_type.fill(ControllerTypes::CONTROLLER_NONE);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
s_status = NO_ADAPTER_DETECTED;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2015-01-05 01:31:54 +01:00
|
|
|
|
if (s_handle)
|
|
|
|
|
{
|
2015-01-05 02:43:26 +01:00
|
|
|
|
libusb_release_interface(s_handle, 0);
|
2015-01-05 01:31:54 +01:00
|
|
|
|
libusb_close(s_handle);
|
|
|
|
|
s_handle = nullptr;
|
|
|
|
|
}
|
2015-04-26 23:53:36 +02:00
|
|
|
|
if (s_detect_callback != nullptr)
|
|
|
|
|
s_detect_callback();
|
2021-05-04 22:37:06 +02:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter detached");
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 03:16:00 +02:00
|
|
|
|
GCPadStatus Input(int chan)
|
2014-12-10 10:45:45 +01:00
|
|
|
|
{
|
2016-01-06 07:35:56 +01:00
|
|
|
|
if (!UseAdapter())
|
2016-08-02 03:16:00 +02:00
|
|
|
|
return {};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
if (s_handle == nullptr || s_status != ADAPTER_DETECTED)
|
2016-08-02 03:16:00 +02:00
|
|
|
|
return {};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2016-01-23 18:30:03 +01:00
|
|
|
|
int payload_size = 0;
|
2014-12-11 01:15:36 +01:00
|
|
|
|
u8 controller_payload_copy[37];
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-11 01:15:36 +01:00
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lk(s_mutex);
|
|
|
|
|
std::copy(std::begin(s_controller_payload), std::end(s_controller_payload),
|
|
|
|
|
std::begin(controller_payload_copy));
|
2016-01-23 18:30:03 +01:00
|
|
|
|
payload_size = s_controller_payload_size.load();
|
2014-12-11 01:15:36 +01:00
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2016-08-02 03:16:00 +02:00
|
|
|
|
GCPadStatus pad = {};
|
2016-01-23 18:30:03 +01:00
|
|
|
|
if (payload_size != sizeof(controller_payload_copy) ||
|
|
|
|
|
controller_payload_copy[0] != LIBUSB_DT_HID)
|
2014-12-10 10:45:45 +01:00
|
|
|
|
{
|
2020-07-05 03:03:20 +02:00
|
|
|
|
// This can occur for a few frames on initialization.
|
2021-05-04 22:37:06 +02:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "error reading payload (size: {}, type: {:02x})",
|
|
|
|
|
payload_size, controller_payload_copy[0]);
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-12-31 18:09:47 +01:00
|
|
|
|
bool get_origin = false;
|
2014-12-11 04:06:35 +01:00
|
|
|
|
u8 type = controller_payload_copy[1 + (9 * chan)] >> 4;
|
2016-01-06 07:57:08 +01:00
|
|
|
|
if (type != ControllerTypes::CONTROLLER_NONE &&
|
|
|
|
|
s_controller_type[chan] == ControllerTypes::CONTROLLER_NONE)
|
2015-12-31 18:09:47 +01:00
|
|
|
|
{
|
2021-05-04 22:37:06 +02:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "New device connected to Port {} of Type: {:02x}",
|
|
|
|
|
chan + 1, controller_payload_copy[1 + (9 * chan)]);
|
2015-12-31 18:09:47 +01:00
|
|
|
|
get_origin = true;
|
|
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-11 04:06:35 +01:00
|
|
|
|
s_controller_type[chan] = type;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2016-01-06 07:57:08 +01:00
|
|
|
|
if (s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE)
|
2014-12-11 04:06:35 +01:00
|
|
|
|
{
|
2014-12-13 05:04:03 +01:00
|
|
|
|
u8 b1 = controller_payload_copy[1 + (9 * chan) + 1];
|
|
|
|
|
u8 b2 = controller_payload_copy[1 + (9 * chan) + 2];
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b1 & (1 << 0))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_A;
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b1 & (1 << 1))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_B;
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b1 & (1 << 2))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_X;
|
2014-12-13 06:14:18 +01:00
|
|
|
|
if (b1 & (1 << 3))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_Y;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b1 & (1 << 4))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_LEFT;
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b1 & (1 << 5))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_RIGHT;
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b1 & (1 << 6))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_DOWN;
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b1 & (1 << 7))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_UP;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b2 & (1 << 0))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_BUTTON_START;
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b2 & (1 << 1))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_TRIGGER_Z;
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b2 & (1 << 2))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_TRIGGER_R;
|
2014-12-13 05:04:03 +01:00
|
|
|
|
if (b2 & (1 << 3))
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_TRIGGER_L;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2015-12-31 18:09:47 +01:00
|
|
|
|
if (get_origin)
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button |= PAD_GET_ORIGIN;
|
|
|
|
|
|
|
|
|
|
pad.stickX = controller_payload_copy[1 + (9 * chan) + 3];
|
|
|
|
|
pad.stickY = controller_payload_copy[1 + (9 * chan) + 4];
|
|
|
|
|
pad.substickX = controller_payload_copy[1 + (9 * chan) + 5];
|
|
|
|
|
pad.substickY = controller_payload_copy[1 + (9 * chan) + 6];
|
|
|
|
|
pad.triggerLeft = controller_payload_copy[1 + (9 * chan) + 7];
|
|
|
|
|
pad.triggerRight = controller_payload_copy[1 + (9 * chan) + 8];
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
2017-04-03 19:30:58 +02:00
|
|
|
|
else if (!Core::WantsDeterminism())
|
2015-12-31 18:09:47 +01:00
|
|
|
|
{
|
2016-07-05 20:58:44 +02:00
|
|
|
|
// This is a hack to prevent a desync due to SI devices
|
2016-05-12 02:07:52 +02:00
|
|
|
|
// being different and returning different values.
|
|
|
|
|
// The corresponding code in DeviceGCAdapter has the same check
|
2016-08-02 03:16:00 +02:00
|
|
|
|
pad.button = PAD_ERR_STATUS;
|
2015-12-31 18:09:47 +01:00
|
|
|
|
}
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
2016-08-02 03:16:00 +02:00
|
|
|
|
|
|
|
|
|
return pad;
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 18:09:47 +01:00
|
|
|
|
bool DeviceConnected(int chan)
|
|
|
|
|
{
|
2016-01-06 07:57:08 +01:00
|
|
|
|
return s_controller_type[chan] != ControllerTypes::CONTROLLER_NONE;
|
2015-12-31 18:09:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-29 12:15:58 +01:00
|
|
|
|
void ResetDeviceType(int chan)
|
|
|
|
|
{
|
|
|
|
|
s_controller_type[chan] = ControllerTypes::CONTROLLER_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 18:09:47 +01:00
|
|
|
|
bool UseAdapter()
|
|
|
|
|
{
|
2017-03-16 09:41:36 +01:00
|
|
|
|
const auto& si_devices = SConfig::GetInstance().m_SIDevice;
|
|
|
|
|
|
|
|
|
|
return std::any_of(std::begin(si_devices), std::end(si_devices), [](const auto device_type) {
|
|
|
|
|
return device_type == SerialInterface::SIDEVICE_WIIU_ADAPTER;
|
|
|
|
|
});
|
2015-12-31 18:09:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-08 18:13:58 +01:00
|
|
|
|
void ResetRumble()
|
|
|
|
|
{
|
2016-04-30 13:29:11 +02:00
|
|
|
|
std::unique_lock<std::mutex> lock(s_init_mutex, std::defer_lock);
|
|
|
|
|
if (!lock.try_lock())
|
2015-11-08 18:13:58 +01:00
|
|
|
|
return;
|
2016-04-30 13:29:11 +02:00
|
|
|
|
ResetRumbleLockNeeded();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Needs to be called when s_init_mutex is locked in order to avoid
|
|
|
|
|
// being called while the libusb state is being reset
|
|
|
|
|
static void ResetRumbleLockNeeded()
|
|
|
|
|
{
|
2018-07-17 22:31:18 +02:00
|
|
|
|
if (!UseAdapter() || (s_handle == nullptr || s_status != ADAPTER_DETECTED))
|
2016-04-30 13:29:11 +02:00
|
|
|
|
{
|
2015-11-08 18:13:58 +01:00
|
|
|
|
return;
|
2016-04-30 13:29:11 +02:00
|
|
|
|
}
|
2015-11-08 18:13:58 +01:00
|
|
|
|
|
|
|
|
|
std::fill(std::begin(s_controller_rumble), std::end(s_controller_rumble), 0);
|
|
|
|
|
|
|
|
|
|
unsigned char rumble[5] = {0x11, s_controller_rumble[0], s_controller_rumble[1],
|
|
|
|
|
s_controller_rumble[2], s_controller_rumble[3]};
|
|
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble, sizeof(rumble), &size, 16);
|
|
|
|
|
|
2021-05-04 22:37:06 +02:00
|
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset");
|
2015-11-08 18:13:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-13 05:04:03 +01:00
|
|
|
|
void Output(int chan, u8 rumble_command)
|
2014-12-10 10:45:45 +01:00
|
|
|
|
{
|
2016-01-06 07:35:56 +01:00
|
|
|
|
if (s_handle == nullptr || !UseAdapter() || !SConfig::GetInstance().m_AdapterRumble[chan])
|
2014-12-10 10:45:45 +01:00
|
|
|
|
return;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
|
2014-12-11 04:06:35 +01:00
|
|
|
|
// Skip over rumble commands if it has not changed or the controller is wireless
|
2016-01-06 07:57:08 +01:00
|
|
|
|
if (rumble_command != s_controller_rumble[chan] &&
|
|
|
|
|
s_controller_type[chan] != ControllerTypes::CONTROLLER_WIRELESS)
|
2014-12-10 10:45:45 +01:00
|
|
|
|
{
|
2014-12-13 05:04:03 +01:00
|
|
|
|
s_controller_rumble[chan] = rumble_command;
|
2017-07-22 20:25:11 +02:00
|
|
|
|
s_rumble_data_available.Set();
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
bool IsDetected(const char** error_message)
|
2014-12-10 10:45:45 +01:00
|
|
|
|
{
|
2018-07-17 22:31:18 +02:00
|
|
|
|
if (s_status >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (error_message)
|
|
|
|
|
*error_message = nullptr;
|
2014-12-10 10:45:45 +01:00
|
|
|
|
|
2018-07-17 22:31:18 +02:00
|
|
|
|
return s_status == ADAPTER_DETECTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error_message)
|
2020-07-05 03:03:20 +02:00
|
|
|
|
*error_message = libusb_strerror(static_cast<libusb_error>(s_status.load()));
|
2018-07-17 22:31:18 +02:00
|
|
|
|
|
|
|
|
|
return false;
|
2014-12-10 10:45:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 10:55:52 +02:00
|
|
|
|
} // namespace GCAdapter
|