From 9cdd6ac5f1f791183920803d494b73cd1bfb3dae Mon Sep 17 00:00:00 2001 From: Tomas Rezucha Date: Tue, 14 Nov 2023 11:50:02 +0100 Subject: [PATCH] fix(usb/host): Do not abort on string descriptor overflow Some devices return full LANGID table, even if short LANGID table was requested. No memory overflow occurs, because we have allocated enough memory for transfers to the default pipe. So we can ignore the error and continue with string desc fetching. --- components/usb/hub.c | 13 ++++++++++--- .../usb/host/usb_host_lib/main/class_driver.c | 1 - 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/components/usb/hub.c b/components/usb/hub.c index 40e2ae1b04..519890d135 100644 --- a/components/usb/hub.c +++ b/components/usb/hub.c @@ -449,9 +449,16 @@ static bool enum_stage_transfer_check(enum_ctrl_t *enum_ctrl) return false; } // Check IN transfer returned the expected correct number of bytes - if (enum_ctrl->expect_num_bytes != 0 && enum_ctrl->expect_num_bytes != transfer->actual_num_bytes) { - ESP_LOGE(HUB_DRIVER_TAG, "Incorrect number of bytes returned %d: %s", transfer->actual_num_bytes, enum_stage_strings[enum_ctrl->stage]); - return false; + if (enum_ctrl->expect_num_bytes != 0 && transfer->actual_num_bytes != enum_ctrl->expect_num_bytes) { + if (transfer->actual_num_bytes > enum_ctrl->expect_num_bytes) { + // The device returned more bytes than requested. + // This violates the USB specs chapter 9.3.5, but we can continue + ESP_LOGW(HUB_DRIVER_TAG, "Incorrect number of bytes returned %d: %s", transfer->actual_num_bytes, enum_stage_strings[enum_ctrl->stage]); + } else { + // The device returned less bytes than requested. We cannot continue. + ESP_LOGE(HUB_DRIVER_TAG, "Incorrect number of bytes returned %d: %s", transfer->actual_num_bytes, enum_stage_strings[enum_ctrl->stage]); + return false; + } } // Stage specific checks and updates diff --git a/examples/peripherals/usb/host/usb_host_lib/main/class_driver.c b/examples/peripherals/usb/host/usb_host_lib/main/class_driver.c index 15ae3201f8..d717b6ed99 100644 --- a/examples/peripherals/usb/host/usb_host_lib/main/class_driver.c +++ b/examples/peripherals/usb/host/usb_host_lib/main/class_driver.c @@ -70,7 +70,6 @@ static void action_get_info(class_driver_t *driver_obj) ESP_ERROR_CHECK(usb_host_device_info(driver_obj->dev_hdl, &dev_info)); ESP_LOGI(TAG, "\t%s speed", (dev_info.speed == USB_SPEED_LOW) ? "Low" : "Full"); ESP_LOGI(TAG, "\tbConfigurationValue %d", dev_info.bConfigurationValue); - //Todo: Print string descriptors //Get the device descriptor next driver_obj->actions &= ~ACTION_GET_DEV_INFO;