From aa2f16bf72473ac6cbe08cd611877d6d5d8ab8be Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Fri, 28 Nov 2025 11:41:26 +0800 Subject: [PATCH] update(class/hub): remove hport sources safely Signed-off-by: sakumisu <1203593632@qq.com> --- class/hub/usbh_hub.c | 11 +++++++++++ core/usbh_core.h | 1 + 2 files changed, 12 insertions(+) diff --git a/class/hub/usbh_hub.c b/class/hub/usbh_hub.c index a55043f6..dd348d85 100644 --- a/class/hub/usbh_hub.c +++ b/class/hub/usbh_hub.c @@ -670,7 +670,9 @@ static void usbh_hub_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV) if (ret < 0) { continue; } + usb_osal_mutex_take(bus->mutex); usbh_hub_events(hub); + usb_osal_mutex_give(bus->mutex); } } @@ -700,6 +702,12 @@ int usbh_hub_initialize(struct usbh_bus *bus) return -1; } + bus->mutex = usb_osal_mutex_create(); + if (bus->mutex == NULL) { + USB_LOG_ERR("Failed to create bus mutex\r\n"); + return -1; + } + snprintf(thread_name, 32, "usbh_hub%u", bus->busid); bus->hub_thread = usb_osal_thread_create(thread_name, CONFIG_USBHOST_PSC_STACKSIZE, CONFIG_USBHOST_PSC_PRIO, usbh_hub_thread, bus); if (bus->hub_thread == NULL) { @@ -714,6 +722,7 @@ int usbh_hub_deinitialize(struct usbh_bus *bus) struct usbh_hubport *hport; struct usbh_hub *hub; + usb_osal_mutex_take(bus->mutex); hub = &bus->hcd.roothub; for (uint8_t port = 0; port < hub->nports; port++) { hport = &hub->child[port]; @@ -726,6 +735,8 @@ int usbh_hub_deinitialize(struct usbh_bus *bus) usb_osal_thread_delete(bus->hub_thread); usb_osal_mq_delete(bus->hub_mq); + usb_osal_mutex_give(bus->mutex); + usb_osal_mutex_delete(bus->mutex); return 0; } diff --git a/core/usbh_core.h b/core/usbh_core.h index 29261b66..cabbcf20 100644 --- a/core/usbh_core.h +++ b/core/usbh_core.h @@ -170,6 +170,7 @@ struct usbh_bus { struct usbh_devaddr_map devgen; usb_osal_thread_t hub_thread; usb_osal_mq_t hub_mq; + usb_osal_mutex_t mutex; }; static inline void usbh_control_urb_fill(struct usbh_urb *urb,