@@ -677,6 +677,7 @@ static void usbh_hub_events(struct usbh_hub *hub)
677677static 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
698711void 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
739752int 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
0 commit comments