Skip to content

Commit

Permalink
feat(ext_port): Added External Port driver
Browse files Browse the repository at this point in the history
Closes #12554
  • Loading branch information
roma-jam committed Sep 26, 2024
1 parent bcfa928 commit 70f222e
Show file tree
Hide file tree
Showing 11 changed files with 1,723 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,5 @@ mainmenu "Espressif IoT Development Framework Configuration"
- CONFIG_BOOTLOADER_CACHE_32BIT_ADDR_QUAD_FLASH
- CONFIG_ESP_WIFI_EAP_TLS1_3
- CONFIG_ESP_WIFI_ENABLE_ROAMING_APP
- CONFIG_USB_HOST_EXT_PORT_SUPPORT_LS
- CONFIG_USB_HOST_EXT_PORT_RESET_ATTEMPTS
3 changes: 2 additions & 1 deletion components/usb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ if(CONFIG_SOC_USB_OTG_SUPPORTED)
endif()

if(CONFIG_USB_HOST_HUBS_SUPPORTED)
list(APPEND srcs "ext_hub.c")
list(APPEND srcs "ext_hub.c"
"ext_port.c")
endif()

idf_component_register(SRCS ${srcs}
Expand Down
45 changes: 45 additions & 0 deletions components/usb/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,51 @@ menu "USB-OTG"
help
Enables support for connecting multiple Hubs simultaneously.

menu "Downstream Port configuration"
depends on USB_HOST_HUBS_SUPPORTED

config USB_HOST_EXT_PORT_SUPPORT_LS
depends on IDF_EXPERIMENTAL_FEATURES
bool "Support LS"
default n
help
Enables support of Low-speed devices, connected through the external Hub.

config USB_HOST_EXT_PORT_RESET_ATTEMPTS
depends on IDF_EXPERIMENTAL_FEATURES
# Invisible config option
# Todo: IDF-11283
int
default 1
help
Amount of attempts to reset the device.

The default value is 1.

config USB_HOST_EXT_PORT_CUSTOM_RESET_ENABLE
bool "Custom bPwrOn2PwrGood value"
default n
help
Enables the possibility to configure custom time for the power-on sequence on a port
until power is good on that port.

When enabled, applies the custom PwrOn2PwrGood delay.
When disabled, applies the PwrOn2PwrGood value from the Hub Descriptor.

config USB_HOST_EXT_PORT_CUSTOM_RESET_MS
depends on USB_HOST_EXT_PORT_CUSTOM_RESET_ENABLE
int "PwrOn2PwrGood delay in ms"
default 100
range 0 5000
help
Custom value of delay from the time the power-on sequence begins on a port
until power is good on that port.
Value 0 is used for a hub with no power switches.

The default value is 100 ms.

endmenu #Downstream Port configuration

endmenu #Hub Driver Configuration

config USB_HOST_ENABLE_ENUM_FILTER_CALLBACK
Expand Down
29 changes: 16 additions & 13 deletions components/usb/ext_hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
*/
#include <string.h>
#include <stdint.h>
#include "sdkconfig.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "usb_private.h"
#include "ext_hub.h"
#include "ext_port.h"
#include "usb/usb_helpers.h"

typedef struct ext_port_s *ext_port_hdl_t; /* This will be implemented during ext_port driver implementation */

#define EXT_HUB_MAX_STATUS_BYTES_SIZE (sizeof(uint32_t))
#define EXT_HUB_STATUS_CHANGE_FLAG (1 << 0)
#define EXT_HUB_STATUS_PORT1_CHANGE_FLAG (1 << 1)
Expand Down Expand Up @@ -396,8 +395,15 @@ static void device_error(ext_hub_dev_t *ext_hub_dev)

static esp_err_t device_port_new(ext_hub_dev_t *ext_hub_dev, uint8_t port_idx)
{
ext_port_config_t port_config = {
.ext_hub_hdl = (ext_hub_handle_t) ext_hub_dev,
.parent_dev_hdl = ext_hub_dev->constant.dev_hdl,
.parent_port_num = port_idx + 1,
.port_power_delay_ms = ext_hub_dev->constant.hub_desc->bPwrOn2PwrGood * 2,
};

assert(p_ext_hub_driver->constant.port_driver);
esp_err_t ret = p_ext_hub_driver->constant.port_driver->new (NULL, (void**) &ext_hub_dev->constant.ports[port_idx]);
esp_err_t ret = p_ext_hub_driver->constant.port_driver->new (&port_config, (void**) &ext_hub_dev->constant.ports[port_idx]);
if (ret != ESP_OK) {
ESP_LOGE(EXT_HUB_TAG, "[%d:%d] Port allocation error: %s", ext_hub_dev->constant.dev_addr, port_idx + 1, esp_err_to_name(ret));
goto fail;
Expand All @@ -418,7 +424,7 @@ static esp_err_t device_port_free(ext_hub_dev_t *ext_hub_dev, uint8_t port_idx)

assert(ext_hub_dev->single_thread.maxchild != 0);
assert(p_ext_hub_driver->constant.port_driver);
esp_err_t ret = p_ext_hub_driver->constant.port_driver->free(ext_hub_dev->constant.ports[port_idx]);
esp_err_t ret = p_ext_hub_driver->constant.port_driver->del(ext_hub_dev->constant.ports[port_idx]);

if (ret != ESP_OK) {
ESP_LOGE(EXT_HUB_TAG, "[%d:%d] Unable to free port: %s", ext_hub_dev->constant.dev_addr, port_idx + 1, esp_err_to_name(ret));
Expand Down Expand Up @@ -1038,6 +1044,7 @@ static void handle_device(ext_hub_dev_t *ext_hub_dev)
// FSM for external Hub
switch (ext_hub_dev->single_thread.stage) {
case EXT_HUB_STAGE_IDLE:
stage_pass = true;
break;
case EXT_HUB_STAGE_GET_DEVICE_STATUS:
case EXT_HUB_STAGE_GET_HUB_DESCRIPTOR:
Expand Down Expand Up @@ -1118,8 +1125,7 @@ esp_err_t ext_hub_install(const ext_hub_config_t *config)
{
esp_err_t ret;
ext_hub_driver_t *ext_hub_drv = heap_caps_calloc(1, sizeof(ext_hub_driver_t), MALLOC_CAP_DEFAULT);
SemaphoreHandle_t mux_lock = xSemaphoreCreateMutex();
if (ext_hub_drv == NULL || mux_lock == NULL) {
if (ext_hub_drv == NULL) {
ret = ESP_ERR_NO_MEM;
goto err;
}
Expand Down Expand Up @@ -1151,9 +1157,6 @@ esp_err_t ext_hub_install(const ext_hub_config_t *config)
return ESP_OK;

err:
if (mux_lock != NULL) {
vSemaphoreDelete(mux_lock);
}
heap_caps_free(ext_hub_drv);
return ret;
}
Expand Down Expand Up @@ -1672,7 +1675,7 @@ esp_err_t ext_hub_port_get_speed(ext_hub_handle_t ext_hub_hdl, uint8_t port_num,
esp_err_t ext_hub_set_port_feature(ext_hub_handle_t ext_hub_hdl, uint8_t port_num, uint8_t feature)
{
EXT_HUB_ENTER_CRITICAL();
EXT_HUB_CHECK_FROM_CRIT(p_ext_hub_driver != NULL, ESP_ERR_INVALID_STATE);
EXT_HUB_CHECK_FROM_CRIT(p_ext_hub_driver != NULL, ESP_ERR_NOT_ALLOWED);
EXT_HUB_EXIT_CRITICAL();

esp_err_t ret;
Expand Down Expand Up @@ -1701,7 +1704,7 @@ esp_err_t ext_hub_set_port_feature(ext_hub_handle_t ext_hub_hdl, uint8_t port_nu
esp_err_t ext_hub_clear_port_feature(ext_hub_handle_t ext_hub_hdl, uint8_t port_num, uint8_t feature)
{
EXT_HUB_ENTER_CRITICAL();
EXT_HUB_CHECK_FROM_CRIT(p_ext_hub_driver != NULL, ESP_ERR_INVALID_STATE);
EXT_HUB_CHECK_FROM_CRIT(p_ext_hub_driver != NULL, ESP_ERR_NOT_ALLOWED);
EXT_HUB_EXIT_CRITICAL();

esp_err_t ret;
Expand Down Expand Up @@ -1730,7 +1733,7 @@ esp_err_t ext_hub_clear_port_feature(ext_hub_handle_t ext_hub_hdl, uint8_t port_
esp_err_t ext_hub_get_port_status(ext_hub_handle_t ext_hub_hdl, uint8_t port_num)
{
EXT_HUB_ENTER_CRITICAL();
EXT_HUB_CHECK_FROM_CRIT(p_ext_hub_driver != NULL, ESP_ERR_INVALID_STATE);
EXT_HUB_CHECK_FROM_CRIT(p_ext_hub_driver != NULL, ESP_ERR_NOT_ALLOWED);
EXT_HUB_EXIT_CRITICAL();

esp_err_t ret;
Expand Down
Loading

0 comments on commit 70f222e

Please sign in to comment.