From f333131063a6369ac3446dca9cf991be67e787a8 Mon Sep 17 00:00:00 2001 From: Roman Leonov Date: Fri, 20 Sep 2024 13:33:28 +0200 Subject: [PATCH] feat(hub): Added notification when hubs support disabled --- components/usb/ext_hub.c | 2 +- components/usb/hub.c | 35 +++++++++++++++++++++++++--- components/usb/private_include/hub.h | 4 +++- components/usb/usb_host.c | 10 ++++---- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/components/usb/ext_hub.c b/components/usb/ext_hub.c index 1c821677f4..8b02f4191f 100644 --- a/components/usb/ext_hub.c +++ b/components/usb/ext_hub.c @@ -528,7 +528,7 @@ static esp_err_t device_alloc(device_config_t *config, ext_hub_dev_t **ext_hub_d usb_device_info_t dev_info; ESP_ERROR_CHECK(usbh_dev_get_info(config->dev_hdl, &dev_info)); if (dev_info.parent.dev_hdl) { - ESP_LOGW(EXT_HUB_TAG, "Multiple Hubs not supported, use menuconfig to enable feature"); + ESP_LOGW(EXT_HUB_TAG, "Multiple Hubs support disabled, Hub device was not initialized"); ret = ESP_ERR_NOT_SUPPORTED; goto fail; } diff --git a/components/usb/hub.c b/components/usb/hub.c index 2d328bcf39..e39e741d69 100644 --- a/components/usb/hub.c +++ b/components/usb/hub.c @@ -778,14 +778,35 @@ esp_err_t hub_port_disable(usb_device_handle_t parent_dev_hdl, uint8_t parent_po return ret; } -#if ENABLE_USB_HUBS esp_err_t hub_notify_new_dev(uint8_t dev_addr) { HUB_DRIVER_ENTER_CRITICAL(); HUB_DRIVER_CHECK_FROM_CRIT(p_hub_driver_obj != NULL, ESP_ERR_INVALID_STATE); HUB_DRIVER_EXIT_CRITICAL(); - return ext_hub_new_dev(dev_addr); + esp_err_t ret; +#if ENABLE_USB_HUBS + ret = ext_hub_new_dev(dev_addr); +#else + // Verify the device descriptor and if the bDeviceClass is a Hub class, + // show the warning message, that Hub support feature is not enabled + usb_device_handle_t dev_hdl = NULL; + const usb_device_desc_t *device_desc = NULL; + // Open device + if (usbh_devs_open(dev_addr, &dev_hdl) == ESP_OK) { + // Get Device Descriptor + if (usbh_dev_get_desc(dev_hdl, &device_desc) == ESP_OK) { + if (device_desc->bDeviceClass == USB_CLASS_HUB) { + ESP_LOGW(HUB_DRIVER_TAG, "External Hubs support disabled, Hub device was not initialized"); + } + } + // Close device + usbh_dev_close(dev_hdl); + } + // Logic should not stop the flow, so no error to return + ret = ESP_OK; +#endif // ENABLE_USB_HUBS + return ret; } esp_err_t hub_notify_dev_gone(uint8_t dev_addr) @@ -794,9 +815,17 @@ esp_err_t hub_notify_dev_gone(uint8_t dev_addr) HUB_DRIVER_CHECK_FROM_CRIT(p_hub_driver_obj != NULL, ESP_ERR_INVALID_STATE); HUB_DRIVER_EXIT_CRITICAL(); - return ext_hub_dev_gone(dev_addr); + esp_err_t ret; +#if ENABLE_USB_HUBS + ret = ext_hub_dev_gone(dev_addr); +#else + // Nothing to do, while Hubs support is not enabled + ret = ESP_OK; +#endif // ENABLE_USB_HUBS + return ret; } +#if (ENABLE_USB_HUBS) esp_err_t hub_notify_all_free(void) { HUB_DRIVER_ENTER_CRITICAL(); diff --git a/components/usb/private_include/hub.h b/components/usb/private_include/hub.h index e3efaac9bf..f563a56f28 100644 --- a/components/usb/private_include/hub.h +++ b/components/usb/private_include/hub.h @@ -191,13 +191,13 @@ esp_err_t hub_port_active(usb_device_handle_t parent_dev_hdl, uint8_t parent_por */ esp_err_t hub_port_disable(usb_device_handle_t parent_dev_hdl, uint8_t parent_port_num); -#if ENABLE_USB_HUBS /** * @brief Notify Hub driver that new device has been attached * * If device is has a HUB class, then it will be added as External Hub to Hub Driver. * * @note This function should only be called from the Host Library task + * @note If the Hub support feature is disabled and device has a Hub class, only the warning message will be shown. * * @param[in] dev_addr Device bus address * @@ -213,6 +213,7 @@ esp_err_t hub_notify_new_dev(uint8_t dev_addr); * If the device was an External Hub, then it will be removed from the Hub Driver. * * @note This function should only be called from the Host Library task + * @note If the Hub support feature is disabled, no additional logic requires here. * * @param[in] dev_addr Device bus address * @@ -222,6 +223,7 @@ esp_err_t hub_notify_new_dev(uint8_t dev_addr); */ esp_err_t hub_notify_dev_gone(uint8_t dev_addr); +#if ENABLE_USB_HUBS /** * @brief Notify Hub driver that all devices should be freed * diff --git a/components/usb/usb_host.c b/components/usb/usb_host.c index bc41bab908..9e5f3feea3 100644 --- a/components/usb/usb_host.c +++ b/components/usb/usb_host.c @@ -309,21 +309,21 @@ static void usbh_event_callback(usbh_event_data_t *event_data, void *arg) break; } case USBH_EVENT_NEW_DEV: { + // Internal client + hub_notify_new_dev(event_data->new_dev_data.dev_addr); + // External clients // Prepare a NEW_DEV client event message, the send it to all clients usb_host_client_event_msg_t event_msg = { .event = USB_HOST_CLIENT_EVENT_NEW_DEV, .new_dev.address = event_data->new_dev_data.dev_addr, }; send_event_msg_to_clients(&event_msg, true, 0); -#if ENABLE_USB_HUBS - hub_notify_new_dev(event_data->new_dev_data.dev_addr); -#endif // ENABLE_USB_HUBS break; } case USBH_EVENT_DEV_GONE: { -#if ENABLE_USB_HUBS + // Internal client hub_notify_dev_gone(event_data->new_dev_data.dev_addr); -#endif // ENABLE_USB_HUBS + // External clients // Prepare event msg, send only to clients that have opened the device usb_host_client_event_msg_t event_msg = { .event = USB_HOST_CLIENT_EVENT_DEV_GONE,