summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsakumisu <[email protected]>2026-11-28 22:31:36 +0800
committersakumisu <[email protected]>2026-11-28 22:31:36 +0800
commit82c4b29759e4c67aa83ccbd604a7e8d0171f6c92 (patch)
tree95ba89c6a28a6a2cc4ddc5fc09c2d6eefb55ea8a
parent1e8817b70adda37c03837d7460083156c1a1b471 (diff)
refactor(port): merge host timeout handling into usbh_kill_urb
Signed-off-by: sakumisu <[email protected]>
-rw-r--r--port/dwc2/usb_hc_dwc2.c46
-rw-r--r--port/ehci/usb_hc_ehci.c33
-rw-r--r--port/ehci/usb_hc_ehci.h1
-rw-r--r--port/musb/usb_hc_musb.c63
4 files changed, 62 insertions, 81 deletions
diff --git a/port/dwc2/usb_hc_dwc2.c b/port/dwc2/usb_hc_dwc2.c
index 5ba703eb..bb98441f 100644
--- a/port/dwc2/usb_hc_dwc2.c
+++ b/port/dwc2/usb_hc_dwc2.c
@@ -20,6 +20,7 @@ struct dwc2_chan {
uint32_t xferlen;
uint8_t chidx;
bool inuse;
+ bool is_timeout;
bool do_ssplit;
bool do_csplit;
uint8_t hub_addr;
@@ -667,13 +668,6 @@ static void dwc2_urb_waitup(struct usbh_urb *urb)
return;
}
- if (urb->errorcode == -USB_ERR_TIMEOUT) {
- urb->timeout = 0;
- usb_osal_sem_reset(chan->waitsem);
- dwc2_chan_free(chan);
- return;
- }
-
if (urb->timeout) {
urb->timeout = 0;
usb_osal_sem_give(chan->waitsem);
@@ -1054,7 +1048,7 @@ int usbh_submit_urb(struct usbh_urb *urb)
chan = &g_dwc2_hcd[bus->hcd.hcd_id].chan_pool[chidx];
chan->chidx = chidx;
chan->urb = urb;
- timeout = urb->timeout;
+ chan->is_timeout = false;
chan->do_ssplit = 0;
if (urb->hport->speed != USB_SPEED_HIGH &&
@@ -1068,6 +1062,9 @@ int usbh_submit_urb(struct usbh_urb *urb)
urb->hcpriv = chan;
urb->errorcode = -USB_ERR_BUSY;
urb->actual_length = 0;
+
+ timeout = urb->timeout;
+
usb_osal_leave_critical_section(flags);
if (urb->setup) {
@@ -1109,18 +1106,7 @@ int usbh_submit_urb(struct usbh_urb *urb)
}
return ret;
errout_timeout:
- flags = usb_osal_enter_critical_section();
- if (urb->errorcode != -USB_ERR_BUSY) {
- ret = urb->errorcode;
- usb_osal_sem_reset(chan->waitsem);
- dwc2_chan_free(chan);
- usb_osal_leave_critical_section(flags);
- USB_LOG_ERR("complete but timeout too small\r\n");
- return ret;
- }
- urb->errorcode = -USB_ERR_TIMEOUT;
- ret = urb->errorcode;
- usb_osal_leave_critical_section(flags);
+ chan->is_timeout = true;
usbh_kill_urb(urb);
return ret;
}
@@ -1144,17 +1130,23 @@ int usbh_kill_urb(struct usbh_urb *urb)
return -USB_ERR_INVAL;
}
- if (urb->errorcode != -USB_ERR_BUSY && urb->errorcode != -USB_ERR_TIMEOUT) {
- usb_osal_leave_critical_section(flags);
- return 0;
- }
-
- dwc2_halt(bus, chan->chidx);
+ /* URB is not completed yet */
if (urb->errorcode == -USB_ERR_BUSY) {
+ dwc2_halt(bus, chan->chidx);
urb->errorcode = -USB_ERR_SHUTDOWN;
+
+ if (!chan->is_timeout) {
+ dwc2_urb_waitup(urb);
+ }
+ }
+
+ /* URB is timed out */
+ if (chan->is_timeout) {
+ urb->timeout = 0;
+ usb_osal_sem_reset(chan->waitsem);
+ dwc2_chan_free(chan);
}
- dwc2_urb_waitup(urb);
usb_osal_leave_critical_section(flags);
return 0;
}
diff --git a/port/ehci/usb_hc_ehci.c b/port/ehci/usb_hc_ehci.c
index 9e664f9a..ffc52dfb 100644
--- a/port/ehci/usb_hc_ehci.c
+++ b/port/ehci/usb_hc_ehci.c
@@ -643,12 +643,6 @@ static void ehci_urb_waitup(struct usbh_bus *bus, struct usbh_urb *urb)
}
#endif
- if (urb->errorcode == -USB_ERR_TIMEOUT) {
- urb->timeout = 0;
- usb_osal_sem_reset(qh->waitsem);
- return;
- }
-
if (urb->timeout) {
urb->timeout = 0;
usb_osal_sem_give(qh->waitsem);
@@ -1261,10 +1255,15 @@ int usbh_submit_urb(struct usbh_urb *urb)
urb->hcpriv = qh;
}
- timeout = urb->timeout;
+ if (qh) {
+ qh->is_timeout = false;
+ }
+
urb->errorcode = -USB_ERR_BUSY;
urb->actual_length = 0;
+ timeout = urb->timeout;
+
usb_osal_leave_critical_section(flags);
if (urb->setup) {
@@ -1311,16 +1310,7 @@ int usbh_submit_urb(struct usbh_urb *urb)
}
return ret;
errout_timeout:
- flags = usb_osal_enter_critical_section();
- if (urb->errorcode == -USB_ERR_BUSY) {
- urb->errorcode = -USB_ERR_TIMEOUT;
- } else {
- urb->timeout = 0;
- usb_osal_sem_reset(qh->waitsem);
- USB_LOG_ERR("complete but timeout too small\r\n");
- }
- ret = urb->errorcode;
- usb_osal_leave_critical_section(flags);
+ qh->is_timeout = true;
usbh_kill_urb(urb);
return ret;
}
@@ -1332,6 +1322,7 @@ int usbh_kill_urb(struct usbh_urb *urb)
size_t flags;
uint8_t ep_type;
bool need_iaad_lock;
+ bool need_waitup = false;
int ret = 0;
if (!urb || !urb->hport || !urb->ep || !urb->hport->bus) {
@@ -1379,8 +1370,10 @@ int usbh_kill_urb(struct usbh_urb *urb)
ehci_qh_remove(&g_periodic_qh_head[bus->hcd.hcd_id], qh);
}
+ /* URB is not completed yet */
if (urb->errorcode == -USB_ERR_BUSY) {
urb->errorcode = -USB_ERR_SHUTDOWN;
+ need_waitup = true;
}
EHCI_HCOR->usbcmd |= EHCI_USBCMD_PSEN | EHCI_USBCMD_ASEN;
@@ -1396,7 +1389,11 @@ int usbh_kill_urb(struct usbh_urb *urb)
}
}
- if (urb->errorcode == -USB_ERR_SHUTDOWN || urb->errorcode == -USB_ERR_TIMEOUT) {
+ /* URB is timed out */
+ if (qh->is_timeout) {
+ urb->timeout = 0;
+ usb_osal_sem_reset(qh->waitsem);
+ } else if (need_waitup) {
ehci_urb_waitup(bus, urb);
}
ehci_qh_free(bus, qh);
diff --git a/port/ehci/usb_hc_ehci.h b/port/ehci/usb_hc_ehci.h
index 2d251325..d554ad36 100644
--- a/port/ehci/usb_hc_ehci.h
+++ b/port/ehci/usb_hc_ehci.h
@@ -60,6 +60,7 @@ struct ehci_qh_hw {
uint16_t pad[16];
#endif
bool inuse;
+ bool is_timeout;
uint32_t first_qtd;
struct usbh_urb *urb;
usb_osal_sem_t waitsem;
diff --git a/port/musb/usb_hc_musb.c b/port/musb/usb_hc_musb.c
index c8edcbf0..f089fd37 100644
--- a/port/musb/usb_hc_musb.c
+++ b/port/musb/usb_hc_musb.c
@@ -167,6 +167,7 @@ typedef enum {
struct musb_pipe {
uint8_t chidx;
bool inuse;
+ bool is_timeout;
uint32_t xfrd;
volatile uint8_t ep0_state;
usb_osal_sem_t waitsem;
@@ -595,13 +596,6 @@ static void musb_urb_waitup(struct usbh_urb *urb)
return;
}
- if (urb->errorcode == -USB_ERR_TIMEOUT) {
- urb->timeout = 0;
- usb_osal_sem_reset(pipe->waitsem);
- musb_pipe_free(pipe);
- return;
- }
-
if (urb->timeout) {
urb->timeout = 0;
usb_osal_sem_give(pipe->waitsem);
@@ -898,12 +892,14 @@ int usbh_submit_urb(struct usbh_urb *urb)
pipe = &g_musb_hcd[bus->hcd.hcd_id].pipe_pool[chidx];
pipe->chidx = chidx;
pipe->urb = urb;
- timeout = urb->timeout;
+ pipe->is_timeout = false;
urb->hcpriv = pipe;
urb->errorcode = -USB_ERR_BUSY;
urb->actual_length = 0;
+ timeout = urb->timeout;
+
switch (USB_GET_ENDPOINT_TYPE(urb->ep->bmAttributes)) {
case USB_ENDPOINT_TYPE_CONTROL:
pipe->ep0_state = USB_EP0_STATE_SETUP;
@@ -934,18 +930,7 @@ int usbh_submit_urb(struct usbh_urb *urb)
}
return ret;
errout_timeout:
- flags = usb_osal_enter_critical_section();
- if (urb->errorcode != -USB_ERR_BUSY) {
- ret = urb->errorcode;
- usb_osal_sem_reset(pipe->waitsem);
- musb_pipe_free(pipe);
- usb_osal_leave_critical_section(flags);
- USB_LOG_ERR("complete but timeout too small\r\n");
- return ret;
- }
- urb->errorcode = -USB_ERR_TIMEOUT;
- ret = urb->errorcode;
- usb_osal_leave_critical_section(flags);
+ pipe->is_timeout = true;
usbh_kill_urb(urb);
return ret;
}
@@ -969,29 +954,35 @@ int usbh_kill_urb(struct usbh_urb *urb)
return -USB_ERR_INVAL;
}
- if (urb->errorcode != -USB_ERR_BUSY && urb->errorcode != -USB_ERR_TIMEOUT) {
- usb_osal_leave_critical_section(flags);
- return 0;
- }
+ /* URB is not completed yet */
+ if (urb->errorcode == -USB_ERR_BUSY) {
+ musb_set_active_ep(bus, pipe->chidx);
- musb_set_active_ep(bus, pipe->chidx);
+ if (urb->ep->bEndpointAddress & 0x80) {
+ HWREGH(USB_BASE + MUSB_RXIE_OFFSET) &= ~(1 << pipe->chidx);
+ HWREGH(USB_BASE + MUSB_RXIS_OFFSET) = (1 << pipe->chidx);
- if (urb->ep->bEndpointAddress & 0x80) {
- HWREGH(USB_BASE + MUSB_RXIE_OFFSET) &= ~(1 << pipe->chidx);
- HWREGH(USB_BASE + MUSB_RXIS_OFFSET) = (1 << pipe->chidx);
+ musb_fifo_flush(bus, pipe->chidx | 0x80);
+ } else {
+ HWREGH(USB_BASE + MUSB_TXIE_OFFSET) &= ~(1 << pipe->chidx);
+ HWREGH(USB_BASE + MUSB_TXIS_OFFSET) = (1 << pipe->chidx);
- musb_fifo_flush(bus, pipe->chidx | 0x80);
- } else {
- HWREGH(USB_BASE + MUSB_TXIE_OFFSET) &= ~(1 << pipe->chidx);
- HWREGH(USB_BASE + MUSB_TXIS_OFFSET) = (1 << pipe->chidx);
+ musb_fifo_flush(bus, pipe->chidx);
+ }
+
+ urb->errorcode = -USB_ERR_SHUTDOWN;
- musb_fifo_flush(bus, pipe->chidx);
+ if (!pipe->is_timeout) {
+ musb_urb_waitup(urb);
+ }
}
- if (urb->errorcode == -USB_ERR_BUSY) {
- urb->errorcode = -USB_ERR_SHUTDOWN;
+ /* URB is timed out */
+ if (pipe->is_timeout) {
+ urb->timeout = 0;
+ usb_osal_sem_reset(pipe->waitsem);
+ musb_pipe_free(pipe);
}
- musb_urb_waitup(urb);
usb_osal_leave_critical_section(flags);
return 0;
}