Skip to content

STM32 UDC: fix transmission on halted endpoints #93796

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 1 commit into
base: main
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
36 changes: 35 additions & 1 deletion drivers/usb/udc/udc_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,14 @@ static int udc_stm32_ep_set_halt(const struct device *dev,
return -EIO;
}

/*
* Mark non-control endpoint as halted
* to indicate stall condition
*/
if (USB_EP_GET_IDX(cfg->addr) != 0) {
cfg->stat.halted = true;
}

return 0;
}

Expand All @@ -847,6 +855,21 @@ static int udc_stm32_ep_clear_halt(const struct device *dev,
return -EIO;
}

/* Clear halted state to resume normal operation */
cfg->stat.halted = false;

/*
* If endpoint is non-control, has pending data, is IN direction,
* and not busy, trigger retransmission to continue data flow
*/
if (USB_EP_GET_IDX(cfg->addr)) {
struct net_buf *buf = udc_buf_peek(cfg);

if (buf && USB_EP_DIR_IS_IN(cfg->addr) && !udc_ep_is_busy(cfg)) {
udc_stm32_tx(dev, cfg, buf);
}
}

return 0;
}

Expand Down Expand Up @@ -880,7 +903,18 @@ static int udc_stm32_ep_enqueue(const struct device *dev,
lock_key = irq_lock();

if (USB_EP_DIR_IS_IN(epcfg->addr)) {
ret = udc_stm32_tx(dev, epcfg, buf);
if (!epcfg->stat.halted) {
/* Endpoint is not halted, safe to transmit data */
ret = udc_stm32_tx(dev, epcfg, buf);
} else {
/*
* Endpoint is halted (stalled) by the host
* Skip transmission to avoid protocol violations
* and potential data corruption
*/
LOG_DBG("ep 0x%02x halted", epcfg->addr);
ret = 0;
}
} else {
ret = udc_stm32_rx(dev, epcfg, buf);
}
Expand Down