From 411e5481e79fd7ee815c7d74de8d5e83cb9649dc Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Fri, 23 Feb 2024 04:44:01 +0800 Subject: [PATCH] refactor(usb): Remove USBH control transfer callback This commit merges the USBH control transfer callback into the USBH event callback. This simplifies the code as the USBH now uses a single callback. --- components/usb/private_include/usbh.h | 13 +++++-------- components/usb/usb_host.c | 28 +++++++++++++-------------- components/usb/usbh.c | 13 ++++++++----- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/components/usb/private_include/usbh.h b/components/usb/private_include/usbh.h index c14eee8658..1d683809bb 100644 --- a/components/usb/private_include/usbh.h +++ b/components/usb/private_include/usbh.h @@ -33,6 +33,7 @@ typedef struct usbh_ep_handle_s *usbh_ep_handle_t; * @brief Enumerator for various USBH events */ typedef enum { + USBH_EVENT_CTRL_XFER, /**< A control transfer has completed */ USBH_EVENT_NEW_DEV, /**< A new device has been enumerated and added to the device pool */ USBH_EVENT_DEV_GONE, /**< A device is gone. Clients should close the device */ USBH_EVENT_ALL_FREE, /**< All devices have been freed */ @@ -44,6 +45,10 @@ typedef enum { typedef struct { usbh_event_t event; union { + struct { + usb_device_handle_t dev_hdl; + urb_t *urb; + } ctrl_xfer_data; struct { uint8_t dev_addr; } new_dev_data; @@ -118,12 +123,6 @@ typedef enum { // ---------------------- Callbacks ------------------------ -/** - * @brief Callback used to indicate completion of control transfers submitted usbh_dev_submit_ctrl_urb() - * @note This callback is called from within usbh_process() - */ -typedef void (*usbh_ctrl_xfer_cb_t)(usb_device_handle_t dev_hdl, urb_t *urb, void *arg); - /** * @brief Callback used to indicate that the USBH has an event * @@ -166,8 +165,6 @@ typedef struct { typedef struct { usb_proc_req_cb_t proc_req_cb; /**< Processing request callback */ void *proc_req_cb_arg; /**< Processing request callback argument */ - usbh_ctrl_xfer_cb_t ctrl_xfer_cb; /**< Control transfer callback */ - void *ctrl_xfer_cb_arg; /**< Control transfer callback argument */ usbh_event_cb_t event_cb; /**< USBH event callback */ void *event_cb_arg; /**< USBH event callback argument */ } usbh_config_t; diff --git a/components/usb/usb_host.c b/components/usb/usb_host.c index 080fd8a36d..79a1952404 100644 --- a/components/usb/usb_host.c +++ b/components/usb/usb_host.c @@ -262,22 +262,22 @@ static bool proc_req_callback(usb_proc_req_source_t source, bool in_isr, void *a return yield; } -static void ctrl_xfer_callback(usb_device_handle_t dev_hdl, urb_t *urb, void *arg) -{ - assert(urb->usb_host_client != NULL); - // Redistribute done control transfer to the clients that submitted them - client_t *client_obj = (client_t *)urb->usb_host_client; - - HOST_ENTER_CRITICAL(); - TAILQ_INSERT_TAIL(&client_obj->dynamic.done_ctrl_xfer_tailq, urb, tailq_entry); - client_obj->dynamic.num_done_ctrl_xfer++; - _unblock_client(client_obj, false); - HOST_EXIT_CRITICAL(); -} - static void usbh_event_callback(usbh_event_data_t *event_data, void *arg) { switch (event_data->event) { + case USBH_EVENT_CTRL_XFER: { + assert(event_data->ctrl_xfer_data.urb != NULL); + assert(event_data->ctrl_xfer_data.urb->usb_host_client != NULL); + // Redistribute done control transfer to the clients that submitted them + client_t *client_obj = (client_t *)event_data->ctrl_xfer_data.urb->usb_host_client; + + HOST_ENTER_CRITICAL(); + TAILQ_INSERT_TAIL(&client_obj->dynamic.done_ctrl_xfer_tailq, event_data->ctrl_xfer_data.urb, tailq_entry); + client_obj->dynamic.num_done_ctrl_xfer++; + _unblock_client(client_obj, false); + HOST_EXIT_CRITICAL(); + break; + } case USBH_EVENT_NEW_DEV: { // Prepare a NEW_DEV client event message, the send it to all clients usb_host_client_event_msg_t event_msg = { @@ -394,8 +394,6 @@ esp_err_t usb_host_install(const usb_host_config_t *config) usbh_config_t usbh_config = { .proc_req_cb = proc_req_callback, .proc_req_cb_arg = NULL, - .ctrl_xfer_cb = ctrl_xfer_callback, - .ctrl_xfer_cb_arg = NULL, .event_cb = usbh_event_callback, .event_cb_arg = NULL, }; diff --git a/components/usb/usbh.c b/components/usb/usbh.c index b4297bbd88..456b3dc4e5 100644 --- a/components/usb/usbh.c +++ b/components/usb/usbh.c @@ -110,8 +110,6 @@ typedef struct { void *hub_req_cb_arg; usbh_event_cb_t event_cb; void *event_cb_arg; - usbh_ctrl_xfer_cb_t ctrl_xfer_cb; - void *ctrl_xfer_cb_arg; SemaphoreHandle_t mux_lock; } constant; } usbh_t; @@ -482,7 +480,14 @@ static inline void handle_ep0_dequeue(device_t *dev_obj) urb_t *urb = hcd_urb_dequeue(dev_obj->constant.default_pipe); while (urb != NULL) { num_urbs++; - p_usbh_obj->constant.ctrl_xfer_cb((usb_device_handle_t)dev_obj, urb, p_usbh_obj->constant.ctrl_xfer_cb_arg); + usbh_event_data_t event_data = { + .event = USBH_EVENT_CTRL_XFER, + .ctrl_xfer_data = { + .dev_hdl = (usb_device_handle_t)dev_obj, + .urb = urb, + }, + }; + p_usbh_obj->constant.event_cb(&event_data, p_usbh_obj->constant.event_cb_arg); urb = hcd_urb_dequeue(dev_obj->constant.default_pipe); } USBH_ENTER_CRITICAL(); @@ -589,8 +594,6 @@ esp_err_t usbh_install(const usbh_config_t *usbh_config) usbh_obj->constant.proc_req_cb_arg = usbh_config->proc_req_cb_arg; usbh_obj->constant.event_cb = usbh_config->event_cb; usbh_obj->constant.event_cb_arg = usbh_config->event_cb_arg; - usbh_obj->constant.ctrl_xfer_cb = usbh_config->ctrl_xfer_cb; - usbh_obj->constant.ctrl_xfer_cb_arg = usbh_config->ctrl_xfer_cb_arg; usbh_obj->constant.mux_lock = mux_lock; // Assign USBH object pointer