Skip to content

Commit 660f0ba

Browse files
committed
fix(class/hub): remove bus mutex, replace with mq to exit thread
Signed-off-by: sakumisu <1203593632@qq.com>
1 parent 963a01f commit 660f0ba

3 files changed

Lines changed: 23 additions & 24 deletions

File tree

class/hub/usbh_hub.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ static void usbh_hub_events(struct usbh_hub *hub)
677677
static void usbh_hub_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
678678
{
679679
struct usbh_hub *hub;
680+
struct usbh_hubport *hport;
680681
int ret = 0;
681682

682683
struct usbh_bus *bus = (struct usbh_bus *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
@@ -689,10 +690,22 @@ static void usbh_hub_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
689690
if (ret < 0) {
690691
continue;
691692
}
692-
usb_osal_mutex_take(bus->mutex);
693+
if (hub == NULL) {
694+
break;
695+
}
693696
usbh_hub_events(hub);
694-
usb_osal_mutex_give(bus->mutex);
695697
}
698+
699+
hub = &bus->hcd.roothub;
700+
for (uint8_t port = 0; port < hub->nports; port++) {
701+
hport = &hub->child[port];
702+
703+
usbh_hubport_release(hport);
704+
}
705+
usb_hc_deinit(bus);
706+
usb_osal_mq_delete(bus->hub_mq);
707+
usb_osal_sem_give(bus->hub_sem);
708+
usb_osal_thread_delete(NULL);
696709
}
697710

698711
void usbh_hub_thread_wakeup(struct usbh_hub *hub)
@@ -721,9 +734,9 @@ int usbh_hub_initialize(struct usbh_bus *bus)
721734
return -1;
722735
}
723736

724-
bus->mutex = usb_osal_mutex_create();
725-
if (bus->mutex == NULL) {
726-
USB_LOG_ERR("Failed to create bus mutex\r\n");
737+
bus->hub_sem = usb_osal_sem_create(0);
738+
if (bus->hub_sem == NULL) {
739+
USB_LOG_ERR("Failed to create hub sem\r\n");
727740
return -1;
728741
}
729742

@@ -738,24 +751,11 @@ int usbh_hub_initialize(struct usbh_bus *bus)
738751

739752
int usbh_hub_deinitialize(struct usbh_bus *bus)
740753
{
741-
struct usbh_hubport *hport;
742-
struct usbh_hub *hub;
743-
744-
usb_osal_mutex_take(bus->mutex);
745-
hub = &bus->hcd.roothub;
746-
for (uint8_t port = 0; port < hub->nports; port++) {
747-
hport = &hub->child[port];
748-
749-
usbh_hubport_release(hport);
750-
}
751-
752-
usb_hc_deinit(bus);
753-
754-
usb_osal_thread_delete(bus->hub_thread);
755-
usb_osal_mq_delete(bus->hub_mq);
754+
usb_osal_mq_send(bus->hub_mq, (uintptr_t)NULL);
755+
usb_osal_sem_take(bus->hub_sem, USB_OSAL_WAITING_FOREVER);
756+
usb_osal_sem_delete(bus->hub_sem);
757+
bus->event_handler(bus->busid, USB_HUB_INDEX_ANY, USB_HUB_PORT_ANY, USB_INTERFACE_ANY, USBH_EVENT_DEINIT);
756758

757-
usb_osal_mutex_give(bus->mutex);
758-
usb_osal_mutex_delete(bus->mutex);
759759
return 0;
760760
}
761761

core/usbh_core.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,6 @@ int usbh_deinitialize(uint8_t busid)
667667
bus = &g_usbhost_bus[busid];
668668

669669
usbh_hub_deinitialize(bus);
670-
bus->event_handler(bus->busid, USB_HUB_INDEX_ANY, USB_HUB_PORT_ANY, USB_INTERFACE_ANY, USBH_EVENT_DEINIT);
671670

672671
usb_slist_remove(&g_bus_head, &bus->list);
673672

core/usbh_core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ struct usbh_bus {
203203
struct usbh_devaddr_map devgen;
204204
usb_osal_thread_t hub_thread;
205205
usb_osal_mq_t hub_mq;
206-
usb_osal_mutex_t mutex;
206+
usb_osal_sem_t hub_sem;
207207
usbh_event_handler_t event_handler;
208208
};
209209

0 commit comments

Comments
 (0)