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

commit d396d8c2e283472c0ca2f0be55bf62f959841e8b
Author: yangsong8 <[email protected]>
AuthorDate: Wed Sep 2 17:20:48 2026 +0800

    drivers/usbdev/usbmsc: fill full bulk-IN request buffer per transfer
    
    usbmsc_cmdreadstate capped each EP_SUBMIT at one maxpacket, so a
    device->host READ issued one USB transfer per maxpacket packet, capping
    read throughput.
    
    Fill each bulk-IN request up to its full allocated buffer before
    submitting and let the controller DMA split it into maxpacket-sized USB
    packets, cutting the number of EP_SUBMIT / completion-IRQ / worker
    round-trips per transfer.
    
    The fill length is taken from priv->bulkinreqlen, recorded at bind time
    from the same expression used to allocate the request buffer
    (CONFIG_USBMSC_BULKINREQLEN for HS/FS, SSBULKMAXPACKET*(MAXBURST+1) for
    SuperSpeed), so it can never exceed the buffer.  It is also floored at
    one maxpacket so a sub-maxpacket packet is never emitted mid-transfer.
    
    Signed-off-by: yangsong8 <[email protected]>
---
 drivers/usbdev/Kconfig       |  2 ++
 drivers/usbdev/usbmsc.c      | 45 ++++++++++++++++++++++++++++++--------------
 drivers/usbdev/usbmsc.h      |  3 +++
 drivers/usbdev/usbmsc_scsi.c |  5 ++---
 4 files changed, 38 insertions(+), 17 deletions(-)

diff --git a/drivers/usbdev/Kconfig b/drivers/usbdev/Kconfig
index a175ac5edd8..bfa81189598 100644
--- a/drivers/usbdev/Kconfig
+++ b/drivers/usbdev/Kconfig
@@ -1104,6 +1104,8 @@ config USBMSC_BULKINREQLEN
        int "Bulk IN request size"
        default 512 if USBDEV_DUALSPEED
        default 64  if !USBDEV_DUALSPEED
+       range 512 65536 if USBDEV_DUALSPEED
+       range 64 65536 if !USBDEV_DUALSPEED
        ---help---
                The size of the buffer in each WRITE request.  This value 
should to be
                at least as large as the endpoint maxpacket size .  Most DCDs 
can divide
diff --git a/drivers/usbdev/usbmsc.c b/drivers/usbdev/usbmsc.c
index 1d381b49a74..2d489809ee2 100644
--- a/drivers/usbdev/usbmsc.c
+++ b/drivers/usbdev/usbmsc.c
@@ -368,25 +368,32 @@ static int usbmsc_bind(FAR struct usbdevclass_driver_s 
*driver,
       reqcontainer->req->callback = usbmsc_rdcomplete;
     }
 
+  /* Compute the bulk IN request buffer size once (SS vs non-SS) and use it
+   * both to allocate the requests below and (via priv->bulkinreqlen) to let
+   * usbmsc_cmdreadstate fill a full request per bulk-IN transfer instead of
+   * one maxpacket.  Single source of truth for the buffer size.
+   */
+
+#ifdef CONFIG_USBDEV_SUPERSPEED
+  if (dev->speed == USB_SPEED_SUPER ||
+      dev->speed == USB_SPEED_SUPER_PLUS)
+    {
+      priv->bulkinreqlen = USBMSC_SSBULKMAXPACKET *
+                           (USBMSC_SSBULKMAXBURST + 1);
+    }
+  else
+#endif
+    {
+      priv->bulkinreqlen = CONFIG_USBMSC_BULKINREQLEN;
+    }
+
   /* Pre-allocate write request containers and put in a free list */
 
   for (i = 0; i < CONFIG_USBMSC_NWRREQS; i++)
     {
       reqcontainer = &priv->wrreqs[i];
-#ifdef CONFIG_USBDEV_SUPERSPEED
-      if (dev->speed == USB_SPEED_SUPER ||
-          dev->speed == USB_SPEED_SUPER_PLUS)
-        {
-          reqcontainer->req = usbdev_allocreq(priv->epbulkin,
-                                              USBMSC_SSBULKMAXPACKET *
-                                              (USBMSC_SSBULKMAXBURST + 1));
-        }
-      else
-#endif
-        {
-          reqcontainer->req = usbdev_allocreq(priv->epbulkin,
-                                              CONFIG_USBMSC_BULKINREQLEN);
-        }
+      reqcontainer->req = usbdev_allocreq(priv->epbulkin,
+                                          priv->bulkinreqlen);
 
       if (reqcontainer->req == NULL)
         {
@@ -1054,6 +1061,16 @@ int usbmsc_setconfig(FAR struct usbmsc_dev_s *priv, 
uint8_t config)
 
   priv->epbulkout->priv = priv;
 
+  /* The per-request buffers were allocated at bind time from
+   * priv->bulkinreqlen.  The bulk-IN fill length
+   * (MAX(bulkinreqlen, maxpacket) in usbmsc_cmdreadstate) and
+   * assume each buffer holds at least one maxpacket;
+   * a configured REQLEN smaller than maxpacket would overflow req->buf.
+   * Catch such a misconfiguration early.
+   */
+
+  DEBUGASSERT(priv->bulkinreqlen >= priv->epbulkin->maxpacket);
+
   /* Queue read requests in the bulk OUT endpoint */
 
   for (i = 0; i < CONFIG_USBMSC_NRDREQS; i++)
diff --git a/drivers/usbdev/usbmsc.h b/drivers/usbdev/usbmsc.h
index 3eaee06a1a8..fb88616a5b8 100644
--- a/drivers/usbdev/usbmsc.h
+++ b/drivers/usbdev/usbmsc.h
@@ -399,6 +399,9 @@ struct usbmsc_dev_s
   FAR struct usbdev_ep_s  *epbulkin;  /* Bulk IN endpoint structure */
   FAR struct usbdev_ep_s  *epbulkout; /* Bulk OUT endpoint structure */
   FAR struct usbdev_req_s *ctrlreq;   /* Control request (for ep0 setup 
responses) */
+  uint32_t          bulkinreqlen;     /* Allocated size of each bulk IN request
+                                       * buffer (== alloc expr; used to fill a
+                                       * full request per bulk-IN transfer) */
 
   /* SCSI command processing */
 
diff --git a/drivers/usbdev/usbmsc_scsi.c b/drivers/usbdev/usbmsc_scsi.c
index 0932cf123ee..2b7410601b1 100644
--- a/drivers/usbdev/usbmsc_scsi.c
+++ b/drivers/usbdev/usbmsc_scsi.c
@@ -2304,8 +2304,7 @@ static int usbmsc_cmdreadstate(FAR struct usbmsc_dev_s 
*priv)
       src    = &priv->iobuffer[lun->sectorsize - priv->nsectbytes];
       dest   = &req->buf[priv->nreqbytes];
 
-      nbytes = MIN(priv->epbulkin->maxpacket - priv->nreqbytes,
-                   priv->nsectbytes);
+      nbytes = MIN(priv->bulkinreqlen - priv->nreqbytes, priv->nsectbytes);
 
       /* Copy the data from the sector buffer to the USB request and update
        * counts
@@ -2320,7 +2319,7 @@ static int usbmsc_cmdreadstate(FAR struct usbmsc_dev_s 
*priv)
        * then submit the request
        */
 
-      if (priv->nreqbytes >= priv->epbulkin->maxpacket ||
+      if (priv->nreqbytes >= priv->bulkinreqlen ||
           (priv->u.xfrlen <= 0 && priv->nsectbytes <= 0))
         {
           /* Submit the request to the bulk IN endpoint */

Reply via email to