This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new d277be2a929 arch/xtensa/esp32s3: fix USB OTG request lifecycle on SMP
d277be2a929 is described below
commit d277be2a9291ab6b1fbe0166a7d228713bd20fad
Author: nope <[email protected]>
AuthorDate: Thu Nov 12 14:49:07 2026 +0800
arch/xtensa/esp32s3: fix USB OTG request lifecycle on SMP
The IN endpoint completed a request as soon as all bytes had been
written to the TxFIFO, before the corresponding XFRC interrupt. The
class driver could then release or reuse the request while the
controller was still transmitting it. On SMP, the request queue and
active state could also be changed concurrently from the interrupt and
task contexts.
Keep IN requests in flight until XFRC, account the bytes completed by
the controller, and complete the request only when all bytes have been
transmitted. Clear active/in-flight state when requests are cancelled,
including the path where the hardware has already disabled the IN
endpoint, so the next submit can re-arm the endpoint. Return cancelled
requests through esp32s3_req_complete() to preserve EP0 stall
semantics, and protect the request queues with a controller lock.
Without this, an SMP build can hang when a host opens the CDC ACM port
while the target is continuously sending IN data.
Fixes: #18541
Assisted-by: DeepSeek (deepseek-flash)
Signed-off-by: liu <[email protected]>
---
arch/xtensa/src/esp32s3/esp32s3_otg_device.c | 107 +++++++++++++++++++++++++--
1 file changed, 101 insertions(+), 6 deletions(-)
diff --git a/arch/xtensa/src/esp32s3/esp32s3_otg_device.c
b/arch/xtensa/src/esp32s3/esp32s3_otg_device.c
index b296bd48e91..0ee951e869e 100644
--- a/arch/xtensa/src/esp32s3/esp32s3_otg_device.c
+++ b/arch/xtensa/src/esp32s3/esp32s3_otg_device.c
@@ -42,6 +42,7 @@
#include <nuttx/usb/usbdev_trace.h>
#include <nuttx/irq.h>
+#include <nuttx/spinlock.h>
#include <arch/board/board.h>
#include "xtensa.h"
@@ -414,6 +415,7 @@ struct esp32s3_req_s
{
struct usbdev_req_s req; /* Standard USB request */
struct esp32s3_req_s *flink; /* Supports a singly linked list */
+ uint32_t sent; /* Bytes completed by XFRC */
};
/* This is the internal representation of an endpoint */
@@ -439,6 +441,11 @@ struct esp32s3_ep_s
uint8_t isin:1; /* 1: IN Endpoint */
uint8_t odd:1; /* 1: Odd frame */
uint8_t zlp:1; /* 1: Transmit a zero-length-packet (IN EPs
only) */
+
+ /* Request whose packet is in the FIFO */
+
+ struct esp32s3_req_s *inflight;
+ uint32_t inflight_len; /* Length of the in-flight packet */
};
/* This structure retains the state of the USB device controller */
@@ -456,6 +463,10 @@ struct esp32s3_usbdev_s
struct usbdevclass_driver_s *driver;
+ /* Protects the per-endpoint request queues and active state. */
+
+ spinlock_t lock;
+
/* ESP32-S3-specific fields */
int cpu; /* CPU ID */
@@ -924,7 +935,11 @@ static void esp32s3_putreg(uint32_t val, uint32_t addr)
static struct esp32s3_req_s *esp32s3_req_remfirst(
struct esp32s3_ep_s *privep)
{
- struct esp32s3_req_s *ret = privep->head;
+ struct esp32s3_req_s *ret;
+ irqstate_t flags;
+
+ flags = spin_lock_irqsave(&privep->dev->lock);
+ ret = privep->head;
if (ret)
{
@@ -937,6 +952,7 @@ static struct esp32s3_req_s *esp32s3_req_remfirst(
ret->flink = NULL;
}
+ spin_unlock_irqrestore(&privep->dev->lock, flags);
return ret;
}
@@ -951,7 +967,11 @@ static struct esp32s3_req_s *esp32s3_req_remfirst(
static bool esp32s3_req_addlast(struct esp32s3_ep_s *privep,
struct esp32s3_req_s *req)
{
- bool is_empty = !privep->head;
+ bool is_empty;
+ irqstate_t flags;
+
+ flags = spin_lock_irqsave(&privep->dev->lock);
+ is_empty = !privep->head;
req->flink = NULL;
if (is_empty)
@@ -965,6 +985,7 @@ static bool esp32s3_req_addlast(struct esp32s3_ep_s *privep,
privep->tail = req;
}
+ spin_unlock_irqrestore(&privep->dev->lock, flags);
return is_empty;
}
@@ -1228,6 +1249,28 @@ static void esp32s3_epin_request(struct esp32s3_usbdev_s
*priv,
int nwords;
int bytesleft;
+ if (privep->active)
+ {
+ return;
+ }
+
+ /* If the FIFO still contains stale bytes from a disabled transfer, drop
+ * them before programming the next request.
+ */
+
+ {
+ uint32_t depth = esp32s3_getreg(ESP32S3_OTG_DIEPTXF(privep->epphy));
+ uint32_t free = esp32s3_getreg(ESP32S3_OTG_DTXFSTS(privep->epphy));
+
+ depth &= OTG_DIEPTXF_INEPTXFD_MASK;
+ depth >>= OTG_DIEPTXF_INEPTXFD_SHIFT;
+
+ if (depth > (free & OTG_DTXFSTS_MASK))
+ {
+ esp32s3_txfifo_flush(OTG_GRSTCTL_TXFNUM_D(privep->epphy));
+ }
+ }
+
/* We get here in one of four possible ways. From three interrupting
* events:
*
@@ -1256,6 +1299,19 @@ static void esp32s3_epin_request(struct esp32s3_usbdev_s
*priv,
{
usbtrace(TRACE_DEVERROR(ESP32S3_TRACEERR_EPINREQEMPTY), privep->epphy);
+ {
+ uint32_t depth = esp32s3_getreg(ESP32S3_OTG_DIEPTXF(privep->epphy));
+ uint32_t free = esp32s3_getreg(ESP32S3_OTG_DTXFSTS(privep->epphy));
+
+ depth &= OTG_DIEPTXF_INEPTXFD_MASK;
+ depth >>= OTG_DIEPTXF_INEPTXFD_SHIFT;
+
+ if (depth > (free & OTG_DTXFSTS_MASK))
+ {
+ esp32s3_txfifo_flush(OTG_GRSTCTL_TXFNUM_D(privep->epphy));
+ }
+ }
+
/* There is no TX transfer in progress and no new pending TX requests
* to send. To stop transmitting any data on a particular IN endpoint,
* the application must set the IN NAK bit. To set this bit, the
@@ -1414,6 +1470,12 @@ static void esp32s3_epin_request(struct esp32s3_usbdev_s
*priv,
* data.
*/
+ if (privep->epphy != EP0)
+ {
+ privep->inflight = privreq;
+ privep->inflight_len = nbytes;
+ }
+
privep->active = true;
/* EP0 is a special case */
@@ -1434,13 +1496,12 @@ static void esp32s3_epin_request(struct
esp32s3_usbdev_s *priv,
* transfer
*/
- if (privreq->req.xfrd >= privreq->req.len && !privep->zlp)
+ if (privep->epphy == EP0 &&
+ privreq->req.xfrd >= privreq->req.len && !privep->zlp)
{
usbtrace(TRACE_COMPLETE(privep->epphy), privreq->req.xfrd);
- /* We are finished with the request (although the transfer has not yet
- * completed).
- */
+ /* EP0 control transfers keep the original early completion path. */
esp32s3_req_complete(privep, OK);
}
@@ -1903,6 +1964,10 @@ static void esp32s3_req_complete(struct esp32s3_ep_s
*privep,
privreq = esp32s3_req_remfirst(privep);
DEBUGASSERT(privreq != NULL);
+ if (privreq == NULL)
+ {
+ return;
+ }
/* If endpoint 0, temporarily reflect the state of protocol stalled in the
* callback.
@@ -1949,6 +2014,10 @@ static void esp32s3_req_cancel(struct esp32s3_ep_s
*privep, int16_t status)
(esp32s3_rqpeek(privep))->req.xfrd);
esp32s3_req_complete(privep, status);
}
+
+ privep->active = false;
+ privep->inflight = NULL;
+ privep->inflight_len = 0;
}
/****************************************************************************
@@ -2080,6 +2149,8 @@ static void esp32s3_usbreset(struct esp32s3_usbdev_s
*priv)
privep->stalled = false;
privep->active = false;
privep->zlp = false;
+ privep->inflight = NULL;
+ privep->inflight_len = 0;
/* Return read requests to the class implementation */
@@ -3051,8 +3122,26 @@ static inline void esp32s3_epin_interrupt(struct
esp32s3_usbdev_s *priv)
esp32s3_putreg(empty, ESP32S3_OTG_DIEPEMPMSK);
esp32s3_putreg(OTG_DIEPINT_XFRC, ESP32S3_OTG_DIEPINT(epno));
+ /* Update the completed-byte count only after XFRC. */
+
+ if (epno != 0 && priv->epin[epno].inflight != NULL)
+ {
+ struct esp32s3_req_s *req = priv->epin[epno].inflight;
+
+ req->sent += priv->epin[epno].inflight_len;
+ req->req.xfrd = req->sent;
+ priv->epin[epno].inflight = NULL;
+ priv->epin[epno].inflight_len = 0;
+
+ if (req->sent >= req->req.len && !priv->epin[epno].zlp)
+ {
+ esp32s3_req_complete(&priv->epin[epno], OK);
+ }
+ }
+
/* IN transfer complete */
+ priv->epin[epno].active = false;
esp32s3_epin(priv, epno);
}
@@ -4266,6 +4355,7 @@ static void esp32s3_epin_disable(struct esp32s3_ep_s
*privep)
regval = esp32s3_getreg(regaddr);
if ((regval & OTG_DIEPCTL_USBAEP) == 0)
{
+ esp32s3_req_cancel(privep, -ESHUTDOWN);
return;
}
@@ -4336,6 +4426,8 @@ static void esp32s3_epin_disable(struct esp32s3_ep_s
*privep)
/* Cancel any queued write requests */
+ privep->inflight = NULL;
+ privep->inflight_len = 0;
esp32s3_req_cancel(privep, -ESHUTDOWN);
leave_critical_section(flags);
}
@@ -4524,6 +4616,7 @@ static int esp32s3_ep_submit(struct usbdev_ep_s *ep,
req->result = -EINPROGRESS;
req->xfrd = 0;
+ privreq->sent = 0;
/* Disable Interrupts */
@@ -5244,6 +5337,8 @@ static void esp32s3_swinitialize(struct esp32s3_usbdev_s
*priv)
memset(priv, 0, sizeof(struct esp32s3_usbdev_s));
+ spin_lock_init(&priv->lock);
+
priv->usbdev.ops = &g_devops;
priv->usbdev.ep0 = &priv->epin[EP0].ep;