From 1cd6413e246a9a8d43257088b88c01222e0b7cae Mon Sep 17 00:00:00 2001 From: revvv <8734113+revvv@users.noreply.github.com> Date: Tue, 29 Mar 2022 19:12:30 +0200 Subject: [PATCH] Wii U: Fix USB get_device_name(), don't truncate to three chars --- input/connect/joypad_connection.c | 5 ++--- input/drivers_hid/wiiu_hid.c | 11 +++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/input/connect/joypad_connection.c b/input/connect/joypad_connection.c index b387fd38bc..9489ed5b37 100644 --- a/input/connect/joypad_connection.c +++ b/input/connect/joypad_connection.c @@ -164,10 +164,9 @@ joypad_connection_entry_t *find_connection_entry(uint16_t vid, uint16_t pid, con name_match = has_name ? strstr(pad_map[i].name, name) : NULL; - if (has_name && strlen(name) == 3) + if (has_name && strlen(name) < 19) { - /* Wii U: Argument 'name' is only the prefix of the device name!? - * This is not enough for a reliable name match! */ + /* Wii U: Argument 'name' may be truncated. This is not enough for a reliable name match! */ RARCH_ERR("find_connection_entry(0x%04x,0x%04x): device name '%s' too short: assuming controller '%s'\n", SWAP_IF_BIG(vid), SWAP_IF_BIG(pid), name, pad_map[i].name); } diff --git a/input/drivers_hid/wiiu_hid.c b/input/drivers_hid/wiiu_hid.c index e8f1a5b9b3..639874e4ed 100644 --- a/input/drivers_hid/wiiu_hid.c +++ b/input/drivers_hid/wiiu_hid.c @@ -894,13 +894,20 @@ static void delete_adapter(wiiu_adapter_t *adapter) static void get_device_name(HIDDevice *device, wiiu_attach_event *event) { int32_t result; - uint8_t *name_buffer = alloc_zeroed(4, device->max_packet_size_rx); + uint8_t name_buffer_size = 46; /* enough to detect WiiU Pro controller */ + uint8_t *name_buffer = alloc_zeroed(4, name_buffer_size); uint8_t *top = &event->device_name[0]; if(name_buffer == NULL) { return; } - result = HIDGetDescriptor(device->handle, 3, 2, 0, name_buffer, device->max_packet_size_rx, NULL, NULL); + + /* HIDGetDescriptor() fills name_buffer in this way: + * - First two bytes are empty + * - Every second byte is empty + * - Maximum name_buffer size is unknown (with 63 it starts to fail with one of my controllers) + * - Truncates device names if name_buffer is too small */ + result = HIDGetDescriptor(device->handle, 3, 2, 0, name_buffer, name_buffer_size, NULL, NULL); if(result > 0) { for(int i = 2; i < result; i += 2) { top[0] = name_buffer[i];