From c846b47ec9882546470f9228840c9b8f817af72f Mon Sep 17 00:00:00 2001 From: Aladdin Bakosh Date: Thu, 27 Jul 2023 14:48:15 +0200 Subject: [PATCH] fix(usbh): robustness against unplugging usb device when it is busy --- src/host/usbh.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/host/usbh.c b/src/host/usbh.c index 49f8c78f13..98edadfbae 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -889,9 +889,20 @@ TU_ATTR_FAST_FUNC void hcd_event_handler(hcd_event_t const* event, bool in_isr) { switch (event->event_id) { +#if CFG_TUH_HUB // case HCD_EVENT_DEVICE_REMOVE: // // mark device as removing to prevent further xfer before the event is processed in usbh task // break; +#else + /* In case of a single device attached we can clear anything in the queue safely */ + /* Reset the enumaration process just in case of immature plugging/unplugging */ + case HCD_EVENT_DEVICE_REMOVE: + _dev0.enumerating = 0; + tu_memclr(&_ctrl_xfer, sizeof(_ctrl_xfer)); + tu_fifo_clear(&_usbh_q->ff); + osal_queue_send(_usbh_q, event, in_isr); + break; +#endif default: osal_queue_send(_usbh_q, event, in_isr);