Skip to content

host/dwc2: cleanup transfer on device close #3076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/portable/synopsys/dwc2/hcd_dwc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ typedef struct {
uint8_t err_count : 3;
uint8_t period_split_nyet_count : 3;
uint8_t halted_nyet : 1;
uint8_t closing : 1; // closing channel
};
uint8_t result;

Expand Down Expand Up @@ -452,11 +453,23 @@ 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 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) {
dwc2_channel_t* channel = &dwc2->channel[ch_id];
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);
}
}
}
}
}
}
Expand Down Expand Up @@ -942,6 +955,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);
Expand Down Expand Up @@ -1002,6 +1020,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));
Expand Down Expand Up @@ -1118,6 +1141,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;
Expand Down Expand Up @@ -1179,6 +1209,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;
Expand Down
Loading