GCAdapter: Use LibusbUtils::ErrorWrap in log messages

This commit is contained in:
Pokechu22 2022-06-02 13:56:17 -07:00
parent 457fcbaf5e
commit 6823b4d7a0
1 changed files with 81 additions and 39 deletions

View File

@ -189,12 +189,14 @@ static void Read()
{ {
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
int payload_size = 0; int payload_size = 0;
const int err = const int error =
libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(), libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(),
CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16); CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16);
if (err) if (error != LIBUSB_SUCCESS)
ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}", {
libusb_error_name(err)); ERROR_LOG_FMT(CONTROLLERINTERFACE, "Read: libusb_interrupt_transfer failed: {}",
LibusbUtils::ErrorWrap(error));
}
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
const int payload_size = env->CallStaticIntMethod(s_adapter_class, input_func); const int payload_size = env->CallStaticIntMethod(s_adapter_class, input_func);
jbyte* const java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); jbyte* const java_data = env->GetByteArrayElements(*java_controller_payload, nullptr);
@ -255,12 +257,12 @@ static void Write()
if (write_size) if (write_size)
{ {
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
const int err = libusb_interrupt_transfer( const int error = libusb_interrupt_transfer(
s_handle, s_endpoint_out, s_controller_write_payload.data(), write_size, &size, 16); s_handle, s_endpoint_out, s_controller_write_payload.data(), write_size, &size, 16);
if (err != 0) if (error != LIBUSB_SUCCESS)
{ {
ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}", ERROR_LOG_FMT(CONTROLLERINTERFACE, "Write: libusb_interrupt_transfer failed: {}",
libusb_error_name(err)); LibusbUtils::ErrorWrap(error));
} }
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
const jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE); const jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE);
@ -322,16 +324,23 @@ static void ScanThreadFunc()
#endif #endif
if (s_libusb_hotplug_enabled) if (s_libusb_hotplug_enabled)
{ {
if (libusb_hotplug_register_callback( const int error = libusb_hotplug_register_callback(
*s_libusb_context, *s_libusb_context,
(libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback, LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback,
nullptr, &s_hotplug_handle) != LIBUSB_SUCCESS) nullptr, &s_hotplug_handle);
s_libusb_hotplug_enabled = false; if (error == LIBUSB_SUCCESS)
if (s_libusb_hotplug_enabled) {
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Using libUSB hotplug detection"); NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Using libUSB hotplug detection");
} }
else
{
s_libusb_hotplug_enabled = false;
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Failed to add libUSB hotplug detection callback: {}",
LibusbUtils::ErrorWrap(error));
}
}
#endif #endif
while (s_adapter_detect_thread_running.IsSet()) while (s_adapter_detect_thread_running.IsSet())
@ -483,10 +492,11 @@ static bool CheckDeviceAccess(libusb_device* device)
{ {
libusb_device_descriptor desc; libusb_device_descriptor desc;
int ret = libusb_get_device_descriptor(device, &desc); int ret = libusb_get_device_descriptor(device, &desc);
if (ret != 0) if (ret != LIBUSB_SUCCESS)
{ {
// could not acquire the descriptor, no point in trying to use it. // could not acquire the descriptor, no point in trying to use it.
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_device_descriptor failed with error: {}", ret); ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_device_descriptor failed: {}",
LibusbUtils::ErrorWrap(ret));
return false; return false;
} }
@ -505,23 +515,23 @@ static bool CheckDeviceAccess(libusb_device* device)
const u8 bus = libusb_get_bus_number(device); const u8 bus = libusb_get_bus_number(device);
const u8 port = libusb_get_device_address(device); const u8 port = libusb_get_device_address(device);
ret = libusb_open(device, &s_handle); ret = libusb_open(device, &s_handle);
if (ret != LIBUSB_SUCCESS)
{
if (ret == LIBUSB_ERROR_ACCESS) if (ret == LIBUSB_ERROR_ACCESS)
{ {
ERROR_LOG_FMT( ERROR_LOG_FMT(CONTROLLERINTERFACE,
CONTROLLERINTERFACE, "Dolphin does not have access to this device: Bus {:03d} Device {:03d}: ID "
"Dolphin does not have access to this device: Bus {:03d} Device {:03d}: ID {:04X}:{:04X}.", "{:04X}:{:04X}.",
bus, port, desc.idVendor, desc.idProduct); bus, port, desc.idVendor, desc.idProduct);
return false;
} }
if (ret != 0) ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_open failed to open device: {}",
{ LibusbUtils::ErrorWrap(ret));
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_open failed to open device with error = {}", ret);
return false; return false;
} }
bool detach_failed = false; bool detach_failed = false;
ret = libusb_kernel_driver_active(s_handle, 0); ret = libusb_kernel_driver_active(s_handle, 0);
if (ret == 1) if (ret == 1) // 1: kernel driver is active
{ {
// On macos detaching would fail without root or entitlement. // On macos detaching would fail without root or entitlement.
// We assume user is using GCAdapterDriver and therefor don't want to detach anything // We assume user is using GCAdapterDriver and therefor don't want to detach anything
@ -530,14 +540,26 @@ static bool CheckDeviceAccess(libusb_device* device)
detach_failed = ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND && ret != LIBUSB_ERROR_NOT_SUPPORTED; detach_failed = ret < 0 && ret != LIBUSB_ERROR_NOT_FOUND && ret != LIBUSB_ERROR_NOT_SUPPORTED;
#endif #endif
if (detach_failed) if (detach_failed)
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_detach_kernel_driver failed with error: {}", ret); {
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_detach_kernel_driver failed: {}",
LibusbUtils::ErrorWrap(ret));
}
}
else if (ret != 0) // 0: kernel driver is not active, but otherwise no error.
{
// Neither 0 nor 1 means an error occured.
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_kernel_driver_active failed: {}",
LibusbUtils::ErrorWrap(ret));
} }
// This call makes Nyko-brand (and perhaps other) adapters work. // This call makes Nyko-brand (and perhaps other) adapters work.
// However it returns LIBUSB_ERROR_PIPE with Mayflash adapters. // However it returns LIBUSB_ERROR_PIPE with Mayflash adapters.
const int transfer = libusb_control_transfer(s_handle, 0x21, 11, 0x0001, 0, nullptr, 0, 1000); const int transfer = libusb_control_transfer(s_handle, 0x21, 11, 0x0001, 0, nullptr, 0, 1000);
if (transfer < 0) if (transfer < 0)
WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_control_transfer failed with error: {}", transfer); {
WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_control_transfer failed: {}",
LibusbUtils::ErrorWrap(transfer));
}
// 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
@ -549,9 +571,10 @@ static bool CheckDeviceAccess(libusb_device* device)
} }
ret = libusb_claim_interface(s_handle, 0); ret = libusb_claim_interface(s_handle, 0);
if (ret != 0) if (ret != LIBUSB_SUCCESS)
{ {
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_claim_interface failed with error: {}", ret); ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_claim_interface failed: {}",
LibusbUtils::ErrorWrap(ret));
libusb_close(s_handle); libusb_close(s_handle);
s_handle = nullptr; s_handle = nullptr;
return false; return false;
@ -566,7 +589,11 @@ static bool CheckDeviceAccess(libusb_device* device)
static void AddGCAdapter(libusb_device* device) static void AddGCAdapter(libusb_device* device)
{ {
libusb_config_descriptor* config = nullptr; libusb_config_descriptor* config = nullptr;
libusb_get_config_descriptor(device, 0, &config); if (const int error = libusb_get_config_descriptor(device, 0, &config); error != LIBUSB_SUCCESS)
{
WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_config_descriptor failed: {}",
LibusbUtils::ErrorWrap(error));
}
for (u8 ic = 0; ic < config->bNumInterfaces; ic++) for (u8 ic = 0; ic < config->bNumInterfaces; ic++)
{ {
const libusb_interface* interfaceContainer = &config->interface[ic]; const libusb_interface* interfaceContainer = &config->interface[ic];
@ -586,8 +613,13 @@ static void AddGCAdapter(libusb_device* device)
int size = 0; int size = 0;
std::array<u8, CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE> payload = {0x13}; std::array<u8, CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE> payload = {0x13};
libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(), const int error = libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(),
CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, 16); CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, 16);
if (error != LIBUSB_SUCCESS)
{
WARN_LOG_FMT(CONTROLLERINTERFACE, "AddGCAdapter: libusb_interrupt_transfer failed: {}",
LibusbUtils::ErrorWrap(error));
}
s_read_adapter_thread_running.Set(true); s_read_adapter_thread_running.Set(true);
s_read_adapter_thread = std::thread(Read); s_read_adapter_thread = std::thread(Read);
@ -646,7 +678,12 @@ static void Reset()
if (s_handle) if (s_handle)
{ {
libusb_release_interface(s_handle, 0); const int error = libusb_release_interface(s_handle, 0);
if (error != LIBUSB_SUCCESS)
{
WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_release_interface failed: {}",
LibusbUtils::ErrorWrap(error));
}
libusb_close(s_handle); libusb_close(s_handle);
s_handle = nullptr; s_handle = nullptr;
} }
@ -817,8 +854,13 @@ static void ResetRumbleLockNeeded()
s_controller_rumble[3]}; s_controller_rumble[3]};
int size = 0; int size = 0;
libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble.data(), const int error = libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble.data(),
CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16); CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16);
if (error != LIBUSB_SUCCESS)
{
WARN_LOG_FMT(CONTROLLERINTERFACE, "ResetRumbleLockNeeded: libusb_interrupt_transfer failed: {}",
LibusbUtils::ErrorWrap(error));
}
INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset"); INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset");
} }