From 1809548bb52979b30fa15231a1429b3621ddaeaa Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Fri, 11 Apr 2025 15:56:25 +0200 Subject: [PATCH 1/3] host/dwc2: cleanup transfer on device close Signed-off-by: HiFiPhile --- src/portable/synopsys/dwc2/hcd_dwc2.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/portable/synopsys/dwc2/hcd_dwc2.c b/src/portable/synopsys/dwc2/hcd_dwc2.c index b13479b020..d76fdeea7e 100644 --- a/src/portable/synopsys/dwc2/hcd_dwc2.c +++ b/src/portable/synopsys/dwc2/hcd_dwc2.c @@ -448,6 +448,12 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr) { hcd_endpoint_t* edpt = &_hcd_data.edpt[i]; if (edpt->hcchar_bm.enable && edpt->hcchar_bm.dev_addr == dev_addr) { tu_memclr(edpt, sizeof(hcd_endpoint_t)); + for (uint8_t j = 0; j < (uint8_t) DWC2_CHANNEL_COUNT_MAX; j++) { + hcd_xfer_t* xfer = &_hcd_data.xfer[j]; + if (xfer->allocated && xfer->ep_id == i) { + tu_memclr(xfer, sizeof(hcd_xfer_t)); + } + } } } } From 90cf575656a009cf4080742909090e219b25ae0f Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Fri, 18 Apr 2025 17:39:45 +0200 Subject: [PATCH 2/3] Disable channel properly. Signed-off-by: HiFiPhile --- src/portable/synopsys/dwc2/hcd_dwc2.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/portable/synopsys/dwc2/hcd_dwc2.c b/src/portable/synopsys/dwc2/hcd_dwc2.c index d76fdeea7e..50aa837d2a 100644 --- a/src/portable/synopsys/dwc2/hcd_dwc2.c +++ b/src/portable/synopsys/dwc2/hcd_dwc2.c @@ -443,15 +443,17 @@ tusb_speed_t hcd_port_speed_get(uint8_t rhport) { // HCD closes all opened endpoints belong to this device void hcd_device_close(uint8_t rhport, uint8_t dev_addr) { - (void) rhport; + dwc2_regs_t* dwc2 = DWC2_REG(rhport); for (uint8_t i = 0; i < (uint8_t) CFG_TUH_DWC2_ENDPOINT_MAX; i++) { hcd_endpoint_t* edpt = &_hcd_data.edpt[i]; if (edpt->hcchar_bm.enable && edpt->hcchar_bm.dev_addr == dev_addr) { tu_memclr(edpt, sizeof(hcd_endpoint_t)); - for (uint8_t j = 0; j < (uint8_t) DWC2_CHANNEL_COUNT_MAX; j++) { - hcd_xfer_t* xfer = &_hcd_data.xfer[j]; + for (uint8_t ch_id = 0; ch_id < (uint8_t) DWC2_CHANNEL_COUNT_MAX; ch_id++) { + hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id]; if (xfer->allocated && xfer->ep_id == i) { - tu_memclr(xfer, sizeof(hcd_xfer_t)); + dwc2_channel_t* channel = &dwc2->channel[ch_id]; + xfer->err_count = HCD_XFER_ERROR_MAX; + channel_disable(dwc2, channel); } } } From 376c1063b7184984ae44ff5a3d225130bc470d14 Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Thu, 15 May 2025 21:34:53 +0200 Subject: [PATCH 3/3] Fix transfer failed event still queued Signed-off-by: HiFiPhile --- src/portable/synopsys/dwc2/hcd_dwc2.c | 33 +++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/portable/synopsys/dwc2/hcd_dwc2.c b/src/portable/synopsys/dwc2/hcd_dwc2.c index 50aa837d2a..8821361e88 100644 --- a/src/portable/synopsys/dwc2/hcd_dwc2.c +++ b/src/portable/synopsys/dwc2/hcd_dwc2.c @@ -98,6 +98,7 @@ typedef struct { uint8_t period_split_nyet_count : 3; uint8_t halted_nyet : 1; uint8_t halted_sof_schedule : 1; + uint8_t closing : 1; // closing channel }; uint8_t result; @@ -452,8 +453,12 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr) { hcd_xfer_t* xfer = &_hcd_data.xfer[ch_id]; if (xfer->allocated && xfer->ep_id == i) { dwc2_channel_t* channel = &dwc2->channel[ch_id]; - xfer->err_count = HCD_XFER_ERROR_MAX; - channel_disable(dwc2, channel); + dwc2_channel_split_t hcsplt = {.value = channel->hcsplt}; + xfer->closing = 1; + // Channel disable must not be programmed for non-split periodic channels + if (!channel_is_periodic(channel->hcchar) || hcsplt.split_en) { + channel_disable(dwc2, channel); + } } } } @@ -915,6 +920,11 @@ static bool handle_channel_in_slave(dwc2_regs_t* dwc2, uint8_t ch_id, uint32_t h } else if (xfer->err_count == HCD_XFER_ERROR_MAX) { xfer->result = XFER_RESULT_FAILED; is_done = true; + } else if (xfer->closing) { + // channel is closing, de-allocate channel + channel_dealloc(dwc2, ch_id); + // don't send event + is_done = false; } else { // got here due to NAK or NYET channel_xfer_in_retry(dwc2, ch_id, hcint); @@ -969,6 +979,11 @@ static bool handle_channel_out_slave(dwc2_regs_t* dwc2, uint8_t ch_id, uint32_t } else if (xfer->err_count == HCD_XFER_ERROR_MAX) { xfer->result = XFER_RESULT_FAILED; is_done = true; + } else if (xfer->closing) { + // channel is closing, de-allocate channel + channel_dealloc(dwc2, ch_id); + // don't send event + is_done = false; } else { // Got here due to NAK or NYET TU_ASSERT(channel_xfer_start(dwc2, ch_id)); @@ -1068,6 +1083,13 @@ static bool handle_channel_in_dma(dwc2_regs_t* dwc2, uint8_t ch_id, uint32_t hci // retry start-split in next binterval channel_xfer_in_retry(dwc2, ch_id, hcint); } + + if (xfer->closing) { + // channel is closing, de-allocate channel + channel_dealloc(dwc2, ch_id); + // don't send event + is_done = false; + } } return is_done; @@ -1125,6 +1147,13 @@ static bool handle_channel_out_dma(dwc2_regs_t* dwc2, uint8_t ch_id, uint32_t hc channel->hcchar |= HCCHAR_CHENA; } } + + if (xfer->closing) { + // channel is closing, de-allocate channel + channel_dealloc(dwc2, ch_id); + // don't send event + is_done = false; + } } else if (hcint & HCINT_ACK) { xfer->err_count = 0; channel->hcintmsk &= ~HCINT_ACK;