Skip to content

Commit fc61875

Browse files
committed
refactor(ext_hub): Pospone the device release, if device is not IDLE
1 parent 754d357 commit fc61875

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

components/usb/ext_hub.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,32 @@ static bool _device_set_actions(ext_hub_dev_t *ext_hub_dev, uint32_t action_flag
312312

313313
static esp_err_t device_enable_int_ep(ext_hub_dev_t *ext_hub_dev)
314314
{
315-
ESP_LOGD(EXT_HUB_TAG, "[%d] Enable EP IN", ext_hub_dev->constant.dev_addr);
316-
esp_err_t ret = usbh_ep_enqueue_urb(ext_hub_dev->constant.ep_in_hdl, ext_hub_dev->constant.in_urb);
317-
if (ret != ESP_OK) {
318-
ESP_LOGE(EXT_HUB_TAG, "[%d] Failed to submit in urb: %s", ext_hub_dev->constant.dev_addr, esp_err_to_name(ret));
319-
device_error(ext_hub_dev);
315+
bool call_proc_req_cb = false;
316+
bool is_active = true;
317+
ESP_LOGD(EXT_HUB_TAG, "[%d] Device ports handle complete", ext_hub_dev->constant.dev_addr);
318+
319+
// Check if the device should be released
320+
EXT_HUB_ENTER_CRITICAL();
321+
if (ext_hub_dev->dynamic.flags.waiting_release) {
322+
is_active = false;
323+
call_proc_req_cb = _device_set_actions(ext_hub_dev, DEV_ACTION_RELEASE);
320324
}
321-
return ret;
325+
EXT_HUB_EXIT_CRITICAL();
326+
327+
if (is_active) {
328+
ESP_LOGD(EXT_HUB_TAG, "[%d] Enable IN EP", ext_hub_dev->constant.dev_addr);
329+
esp_err_t ret = usbh_ep_enqueue_urb(ext_hub_dev->constant.ep_in_hdl, ext_hub_dev->constant.in_urb);
330+
if (ret != ESP_OK) {
331+
ESP_LOGE(EXT_HUB_TAG, "[%d] Failed to submit in urb: %s", ext_hub_dev->constant.dev_addr, esp_err_to_name(ret));
332+
device_error(ext_hub_dev);
333+
}
334+
return ret;
335+
}
336+
337+
if (call_proc_req_cb) {
338+
p_ext_hub_driver->constant.proc_req_cb(false, p_ext_hub_driver->constant.proc_req_cb_arg);
339+
}
340+
return ESP_OK;
322341
}
323342

324343
static void device_has_changed(ext_hub_dev_t *ext_hub_dev)
@@ -1396,10 +1415,9 @@ esp_err_t ext_hub_dev_gone(uint8_t dev_addr)
13961415
return ret;
13971416
}
13981417

1399-
esp_err_t ext_hub_all_free(void)
1418+
void ext_hub_mark_all_free(void)
14001419
{
14011420
bool call_proc_req_cb = false;
1402-
bool wait_for_free = false;
14031421

14041422
EXT_HUB_ENTER_CRITICAL();
14051423
for (int i = 0; i < 2; i++) {
@@ -1414,11 +1432,13 @@ esp_err_t ext_hub_all_free(void)
14141432
while (hub_curr != NULL) {
14151433
hub_next = TAILQ_NEXT(hub_curr, dynamic.tailq_entry);
14161434
hub_curr->dynamic.flags.waiting_release = 1;
1417-
call_proc_req_cb = _device_set_actions(hub_curr, DEV_ACTION_EP1_FLUSH |
1418-
DEV_ACTION_EP1_DEQUEUE |
1419-
DEV_ACTION_RELEASE);
1420-
// At least one hub should be released
1421-
wait_for_free = true;
1435+
uint32_t action_flags = DEV_ACTION_EP1_FLUSH | DEV_ACTION_EP1_DEQUEUE;
1436+
// If device in the IDLE stage, add the release action
1437+
// otherwise, device will be released when the stage changed to IDLE
1438+
if (hub_curr->single_thread.stage == EXT_HUB_STAGE_IDLE) {
1439+
action_flags |= DEV_ACTION_RELEASE;
1440+
}
1441+
call_proc_req_cb = _device_set_actions(hub_curr, action_flags);
14221442
hub_curr = hub_next;
14231443
}
14241444
}
@@ -1427,8 +1447,6 @@ esp_err_t ext_hub_all_free(void)
14271447
if (call_proc_req_cb) {
14281448
p_ext_hub_driver->constant.proc_req_cb(false, p_ext_hub_driver->constant.proc_req_cb_arg);
14291449
}
1430-
1431-
return (wait_for_free) ? ESP_ERR_NOT_FINISHED : ESP_OK;
14321450
}
14331451

14341452
esp_err_t ext_hub_status_handle_complete(ext_hub_handle_t ext_hub_hdl)

components/usb/private_include/ext_hub.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,8 @@ esp_err_t ext_hub_dev_gone(uint8_t dev_addr);
131131
* Entry:
132132
* - should be called within Hub Driver when USB Host library need to be uninstalled
133133
*
134-
* @return
135-
* - ESP_OK: All devices freed
136-
* - ESP_ERR_NOT_FINISHED: Operation not finished: devices waiting children to be freed.
137134
*/
138-
esp_err_t ext_hub_all_free(void);
135+
void ext_hub_mark_all_free(void);
139136

140137
/**
141138
* @brief The External Hub or Ports statuses change completed

0 commit comments

Comments
 (0)