Skip to content

Commit 37de6d7

Browse files
committed
refactor(enum): Removed parent_dev_hdl and parent_port_num, replaced with node uid
1 parent 6f8f13f commit 37de6d7

File tree

5 files changed

+104
-158
lines changed

5 files changed

+104
-158
lines changed

host/usb/private_include/enum.h

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -44,31 +44,9 @@ typedef enum {
4444
} enum_event_t;
4545

4646
typedef struct {
47-
enum_event_t event; /**< Enumerator driver event */
48-
union {
49-
struct {
50-
unsigned int uid; /**< Device unique ID */
51-
usb_device_handle_t parent_dev_hdl; /**< Parent of the enumerating device */
52-
uint8_t parent_port_num; /**< Parent port number of the enumerating device */
53-
} started; /**< ENUM_EVENT_STARTED specific data */
54-
55-
struct {
56-
usb_device_handle_t parent_dev_hdl; /**< Parent of the enumerating device */
57-
uint8_t parent_port_num; /**< Parent port number of the enumerating device */
58-
} reset_req; /**< ENUM_EVENT_RESET_REQUIRED specific data */
59-
60-
struct {
61-
usb_device_handle_t parent_dev_hdl; /**< Parent of the enumerating device */
62-
uint8_t parent_port_num; /**< Parent port number of the enumerating device */
63-
usb_device_handle_t dev_hdl; /**< Handle of the enumerating device */
64-
uint8_t dev_addr; /**< Address of the enumerating device */
65-
} complete; /**< ENUM_EVENT_COMPLETED specific data */
66-
67-
struct {
68-
usb_device_handle_t parent_dev_hdl; /**< Parent of the enumerating device */
69-
uint8_t parent_port_num; /**< Parent port number of the enumerating device */
70-
} canceled; /**< ENUM_EVENT_CANCELED specific data */
71-
};
47+
enum_event_t event; /**< Enumerator driver event */
48+
unsigned int node_uid; /**< Unique node ID */
49+
usb_device_handle_t dev_hdl; /**< Handle of the enumerating device */
7250
} enum_event_data_t;
7351

7452
// ---------------------------- Callbacks --------------------------------------

host/usb/private_include/hub.h

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -131,66 +131,67 @@ esp_err_t hub_root_stop(void);
131131
/**
132132
* @brief Indicate to the Hub driver that a device's port can be recycled
133133
*
134-
* The device connected to the port has been freed. The Hub driver can now recycled the port
134+
* The device connected to the port has been freed.
135+
* The Hub driver can now recycle the port, related to the node.
135136
*
136137
* @note This function should only be called from the Host Library task
137138
*
138-
* @param[in] parent_dev_hdl Parent device handle (is used to get the External Hub handle)
139-
* @param[in] parent_port_num Parent number (is used to specify the External Port)
140-
* @param[in] dev_uid Device's unique ID
139+
* @param[in] node_uid Device's node unique ID
141140
*
142141
* @return
143-
* - ESP_OK: device's port can be recycled
142+
* - ESP_OK: Device's port can be recycled
144143
* - ESP_ERR_INVALID_STATE: Hub driver is not installed
145144
* - ESP_ERR_NOT_SUPPORTED: Recycling External Port is not available (External Hub support disabled),
146145
* or ext hub port error
146+
* - ESP_ERR_NOT_FOUND: Device's node is not found
147147
*/
148-
esp_err_t hub_port_recycle(usb_device_handle_t parent_dev_hdl, uint8_t parent_port_num, unsigned int dev_uid);
148+
esp_err_t hub_node_recycle(unsigned int node_uid);
149149

150150
/**
151-
* @brief Reset the port
151+
* @brief Reset the device in the port, related to the specific Device Tree node
152152
*
153153
* @note This function should only be called from the Host Library task
154154
*
155-
* @param[in] parent_dev_hdl Parent device handle (is used to get the External Hub handle)
156-
* @param[in] parent_port_num Parent number (is used to specify the External Port)
155+
* @param[in] node_uid Device's node unique ID
156+
*
157157
* @return
158-
* - ESP_OK: Port reset successful
158+
* - ESP_OK: If port reset was successful
159159
* - ESP_ERR_INVALID_STATE: Hub driver is not installed
160160
* - ESP_ERR_NOT_SUPPORTED: Resetting External Port is not available (External Hub support disabled),
161161
* or ext hub port error
162+
* - ESP_ERR_NOT_FOUND: Device's node is not found
162163
*/
163-
esp_err_t hub_port_reset(usb_device_handle_t parent_dev_hdl, uint8_t parent_port_num);
164+
esp_err_t hub_node_reset(unsigned int node_uid);
164165

165166
/**
166-
* @brief Activate the port
167+
* @brief Port, related to the specific Device Tree node, has an active device
167168
*
168169
* @note This function should only be called from the Host Library task
169170
*
170-
* @param[in] parent_dev_hdl Parent device handle (is used to get the External Hub handle)
171-
* @param[in] parent_port_num Parent number (is used to specify the External Port)
171+
* @param[in] node_uid Device's node unique ID
172172
*
173173
* @return
174-
* - ESP_OK: Port activated successfully
175-
* - ESP_ERR_NOT_SUPPORTED: Activating External Port is not available (External Hub support disabled),
176-
* or ext hub port error
174+
* - ESP_OK: If Port, related to the Device Tree node was activated successfully
175+
* - ESP_ERR_NOT_SUPPORTED: If activating Port is not available (External Hub support disabled),
176+
* or ext hub port error
177+
* - ESP_ERR_NOT_FOUND: If Device's node is not found
177178
*/
178-
esp_err_t hub_port_active(usb_device_handle_t parent_dev_hdl, uint8_t parent_port_num);
179+
esp_err_t hub_node_active(unsigned int node_uid);
179180

180181
/**
181-
* @brief Disable the port
182+
* @brief Disable the port, related to the specific Device Tree node
182183
*
183184
* @note This function should only be called from the Host Library task
184185
*
185-
* @param[in] parent_dev_hdl Parent device handle (is used to get the External Hub handle)
186-
* @param[in] parent_port_num Parent number (is used to specify the External Port)
186+
* @param[in] node_uid Device's node unique ID
187187
*
188188
* @return
189189
* - ESP_OK: Port has been disabled without error
190190
* - ESP_ERR_INVALID_STATE: Port can't be disabled in current state
191-
* - ESP_ERR_NOT_SUPPORTED: This function is not support by the selected port
191+
* - ESP_ERR_NOT_SUPPORTED: This function is not supported by the selected port
192+
* - ESP_ERR_NOT_FOUND: Device's node is not found
192193
*/
193-
esp_err_t hub_port_disable(usb_device_handle_t parent_dev_hdl, uint8_t parent_port_num);
194+
esp_err_t hub_node_disable(unsigned int node_uid);
194195

195196
/**
196197
* @brief Notify Hub driver that new device has been attached

host/usb/src/enum.c

Lines changed: 24 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -132,12 +132,8 @@ typedef struct {
132132
typedef struct {
133133
struct {
134134
// Device related objects, initialized at start of a particular enumeration
135-
unsigned int dev_uid; /**< Unique device ID being enumerated */
135+
unsigned int node_uid; /**< Unique node ID of device being enumerated */
136136
usb_device_handle_t dev_hdl; /**< Handle of device being enumerated */
137-
// Parent info for optimization and more clean debug output
138-
usb_device_handle_t parent_dev_hdl; /**< Device's parent handle */
139-
uint8_t parent_dev_addr; /**< Device's parent address */
140-
uint8_t parent_port_num; /**< Device's parent port number */
141137
// Parameters, updated during enumeration
142138
enum_stage_t stage; /**< Current enumeration stage */
143139
enum_device_params_t dev_params; /**< Parameters of device under enumeration */
@@ -253,12 +249,11 @@ static esp_err_t select_active_configuration(void)
253249

254250
// User's request NOT to enumerate the USB device
255251
if (!enum_proceed) {
256-
ESP_LOGW(ENUM_TAG, "[%d:%d] Abort request of enumeration process (%#x:%#x)",
257-
p_enum_driver->single_thread.parent_dev_addr,
258-
p_enum_driver->single_thread.parent_port_num,
252+
ESP_LOGW(ENUM_TAG, "Canceled enumeration of the device %#x:%#x, configuration value=%d",
259253
dev_desc->idProduct,
260-
dev_desc->idVendor);
261-
enum_cancel(p_enum_driver->single_thread.dev_uid);
254+
dev_desc->idVendor,
255+
bConfigurationValue);
256+
enum_cancel(p_enum_driver->single_thread.node_uid);
262257
return ESP_OK;
263258
}
264259

@@ -279,13 +274,9 @@ static esp_err_t second_reset_request(void)
279274
// Notify USB Host
280275
enum_event_data_t event_data = {
281276
.event = ENUM_EVENT_RESET_REQUIRED,
282-
.reset_req = {
283-
.parent_dev_hdl = p_enum_driver->single_thread.parent_dev_hdl,
284-
.parent_port_num = p_enum_driver->single_thread.parent_port_num,
285-
},
277+
.node_uid = p_enum_driver->single_thread.node_uid,
286278
};
287279
p_enum_driver->constant.enum_event_cb(&event_data, p_enum_driver->constant.enum_event_cb_arg);
288-
289280
return ESP_OK;
290281
}
291282

@@ -739,10 +730,8 @@ static esp_err_t control_request(enum_stage_t stage)
739730

740731
ret = usbh_dev_submit_ctrl_urb(p_enum_driver->single_thread.dev_hdl, p_enum_driver->constant.urb);
741732
if (ret != ESP_OK) {
742-
ESP_LOGE(ENUM_TAG, "[%d:%d] Control transfer submit error (%#x), stage '%s'",
743-
p_enum_driver->single_thread.parent_dev_addr,
744-
p_enum_driver->single_thread.parent_port_num,
745-
ret,
733+
ESP_LOGE(ENUM_TAG, "Control transfer submit error %s, stage '%s'",
734+
esp_err_to_name(ret),
746735
enum_stage_strings[stage]);
747736
}
748737

@@ -781,9 +770,7 @@ static esp_err_t control_response_handling(enum_stage_t stage)
781770

782771
// Check Control IN transfer returned the expected correct number of bytes
783772
if (expected_num_bytes != 0 && expected_num_bytes != ctrl_xfer->actual_num_bytes) {
784-
ESP_LOGW(ENUM_TAG, "[%d:%d] Unexpected (%d) device response length (expected %d)",
785-
p_enum_driver->single_thread.parent_dev_addr,
786-
p_enum_driver->single_thread.parent_port_num,
773+
ESP_LOGW(ENUM_TAG, "Unexpected (%d) device response length (expected %d)",
787774
ctrl_xfer->actual_num_bytes,
788775
expected_num_bytes);
789776
if (ctrl_xfer->actual_num_bytes < expected_num_bytes) {
@@ -847,31 +834,24 @@ static esp_err_t control_response_handling(enum_stage_t stage)
847834
static esp_err_t stage_cancel(void)
848835
{
849836
// There should be device under enumeration
837+
const unsigned int node_uid = p_enum_driver->single_thread.node_uid;
850838
usb_device_handle_t dev_hdl = p_enum_driver->single_thread.dev_hdl;
851-
usb_device_handle_t parent_dev_hdl = p_enum_driver->single_thread.parent_dev_hdl;
852-
uint8_t parent_port_num = p_enum_driver->single_thread.parent_port_num;
853839

854840
if (dev_hdl) {
855841
ESP_ERROR_CHECK(usbh_dev_enum_unlock(dev_hdl));
856842
ESP_ERROR_CHECK(usbh_dev_close(dev_hdl));
857843
}
858844

859845
// Clean up variables device from enumerator
860-
p_enum_driver->single_thread.dev_uid = 0;
846+
p_enum_driver->single_thread.node_uid = 0;
861847
p_enum_driver->single_thread.dev_hdl = NULL;
862-
p_enum_driver->single_thread.parent_dev_hdl = NULL;
863-
p_enum_driver->single_thread.parent_dev_addr = 0;
864-
p_enum_driver->single_thread.parent_port_num = 0;
865848

866849
p_enum_driver->constant.urb->transfer.context = NULL;
867850

868851
// Propagate the event
869852
enum_event_data_t event_data = {
870853
.event = ENUM_EVENT_CANCELED,
871-
.canceled = {
872-
.parent_dev_hdl = parent_dev_hdl,
873-
.parent_port_num = parent_port_num,
874-
},
854+
.node_uid = node_uid,
875855
};
876856
p_enum_driver->constant.enum_event_cb(&event_data, p_enum_driver->constant.enum_event_cb_arg);
877857
return ESP_OK;
@@ -884,10 +864,8 @@ static esp_err_t stage_cancel(void)
884864
*/
885865
static esp_err_t stage_complete(void)
886866
{
867+
unsigned int node_uid = p_enum_driver->single_thread.node_uid;
887868
usb_device_handle_t dev_hdl = p_enum_driver->single_thread.dev_hdl;
888-
usb_device_handle_t parent_dev_hdl = p_enum_driver->single_thread.parent_dev_hdl;
889-
uint8_t parent_dev_addr = p_enum_driver->single_thread.parent_dev_addr;
890-
uint8_t parent_port_num = p_enum_driver->single_thread.parent_port_num;
891869
uint8_t dev_addr = 0;
892870
ESP_ERROR_CHECK(usbh_dev_get_addr(dev_hdl, &dev_addr));
893871

@@ -896,11 +874,8 @@ static esp_err_t stage_complete(void)
896874
ESP_ERROR_CHECK(usbh_dev_close(dev_hdl));
897875

898876
// Release device from enumerator
899-
p_enum_driver->single_thread.dev_uid = 0;
877+
p_enum_driver->single_thread.node_uid = 0;
900878
p_enum_driver->single_thread.dev_hdl = NULL;
901-
p_enum_driver->single_thread.parent_dev_hdl = NULL;
902-
p_enum_driver->single_thread.parent_dev_addr = 0;
903-
p_enum_driver->single_thread.parent_port_num = 0;
904879

905880
// Release device from enumerator
906881
p_enum_driver->constant.urb->transfer.context = NULL;
@@ -912,19 +887,12 @@ static esp_err_t stage_complete(void)
912887
// Increase device address to use new value during the next enumeration process
913888
get_next_dev_addr();
914889

915-
ESP_LOGD(ENUM_TAG, "[%d:%d] Processing complete, new device address %d",
916-
parent_dev_addr,
917-
parent_port_num,
918-
dev_addr);
890+
ESP_LOGD(ENUM_TAG, "Processing complete, new device address %d", dev_addr);
919891

920892
enum_event_data_t event_data = {
921893
.event = ENUM_EVENT_COMPLETED,
922-
.complete = {
923-
.dev_hdl = dev_hdl,
924-
.dev_addr = dev_addr,
925-
.parent_dev_hdl = parent_dev_hdl,
926-
.parent_port_num = parent_port_num,
927-
},
894+
.node_uid = node_uid,
895+
.dev_hdl = dev_hdl,
928896
};
929897
p_enum_driver->constant.enum_event_cb(&event_data, p_enum_driver->constant.enum_event_cb_arg);
930898
return ESP_OK;
@@ -1005,10 +973,7 @@ static bool set_next_stage(bool last_stage_pass)
1005973
// Find the next stage
1006974
if (last_stage_pass) {
1007975
// Last stage was successful
1008-
ESP_LOGD(ENUM_TAG, "[%d:%d] %s OK",
1009-
p_enum_driver->single_thread.parent_dev_addr,
1010-
p_enum_driver->single_thread.parent_port_num,
1011-
enum_stage_strings[last_stage]);
976+
ESP_LOGD(ENUM_TAG, "%s OK", enum_stage_strings[last_stage]);
1012977
// Get next stage
1013978
if (last_stage == ENUM_STAGE_COMPLETE ||
1014979
last_stage == ENUM_STAGE_CANCEL) {
@@ -1055,10 +1020,7 @@ static bool set_next_stage(bool last_stage_pass)
10551020
break;
10561021
default:
10571022
// Stage is not allowed to failed. Cancel enumeration.
1058-
ESP_LOGE(ENUM_TAG, "[%d:%d] %s FAILED",
1059-
p_enum_driver->single_thread.parent_dev_addr,
1060-
p_enum_driver->single_thread.parent_port_num,
1061-
enum_stage_strings[last_stage]);
1023+
ESP_LOGE(ENUM_TAG, "%s FAILED", enum_stage_strings[last_stage]);
10621024
next_stage = ENUM_STAGE_CANCEL;
10631025
break;
10641026
}
@@ -1211,25 +1173,14 @@ esp_err_t enum_start(unsigned int uid)
12111173

12121174
// Get device info
12131175
usb_device_info_t dev_info;
1214-
uint8_t parent_dev_addr = 0;
12151176
ESP_ERROR_CHECK(usbh_dev_get_info(dev_hdl, &dev_info));
12161177

1217-
if (dev_info.parent.dev_hdl) {
1218-
ESP_ERROR_CHECK(usbh_dev_get_addr(dev_info.parent.dev_hdl, &parent_dev_addr));
1219-
}
1220-
12211178
// Stage ENUM_STAGE_GET_SHORT_DEV_DESC
1222-
ESP_LOGD(ENUM_TAG, "[%d:%d] Start processing, device address %d",
1223-
parent_dev_addr,
1224-
dev_info.parent.port_num,
1225-
0);
1179+
ESP_LOGD(ENUM_TAG, "Start processing device with address %d", 0);
12261180

12271181
p_enum_driver->single_thread.stage = ENUM_STAGE_GET_SHORT_DEV_DESC;
1228-
p_enum_driver->single_thread.dev_uid = uid;
1182+
p_enum_driver->single_thread.node_uid = uid;
12291183
p_enum_driver->single_thread.dev_hdl = dev_hdl;
1230-
p_enum_driver->single_thread.parent_dev_hdl = dev_info.parent.dev_hdl;
1231-
p_enum_driver->single_thread.parent_dev_addr = parent_dev_addr;
1232-
p_enum_driver->single_thread.parent_port_num = dev_info.parent.port_num;
12331184
// Save device handle to the URB transfer context
12341185
p_enum_driver->constant.urb->transfer.context = (void *) dev_hdl;
12351186
// Device params
@@ -1241,14 +1192,9 @@ esp_err_t enum_start(unsigned int uid)
12411192
// Notify USB Host about starting enumeration process
12421193
enum_event_data_t event_data = {
12431194
.event = ENUM_EVENT_STARTED,
1244-
.started = {
1245-
.uid = uid,
1246-
.parent_dev_hdl = dev_info.parent.dev_hdl,
1247-
.parent_port_num = dev_info.parent.port_num,
1248-
},
1195+
.node_uid = uid,
12491196
};
12501197
p_enum_driver->constant.enum_event_cb(&event_data, p_enum_driver->constant.enum_event_cb_arg);
1251-
12521198
// Request processing
12531199
p_enum_driver->constant.proc_req_cb(USB_PROC_REQ_SOURCE_ENUM, false, p_enum_driver->constant.proc_req_cb_arg);
12541200
return ret;
@@ -1276,10 +1222,7 @@ esp_err_t enum_cancel(unsigned int uid)
12761222

12771223
p_enum_driver->single_thread.stage = ENUM_STAGE_CANCEL;
12781224

1279-
ESP_LOGV(ENUM_TAG, "[%d:%d] Cancel at %s",
1280-
p_enum_driver->single_thread.parent_dev_addr,
1281-
p_enum_driver->single_thread.parent_port_num,
1282-
enum_stage_strings[old_stage]);
1225+
ESP_LOGV(ENUM_TAG, "Cancel at %s", enum_stage_strings[old_stage]);
12831226

12841227
if (stage_need_process(old_stage)) {
12851228
// These stages are required to trigger processing in the enum_process()

0 commit comments

Comments
 (0)