From: Ingo Rohloff <ingo.rohloff@lauterbach.com>
To: gregkh@linuxfoundation.org
Cc: viro@zeniv.linux.org.uk, nkapron@google.com, me@samcday.com,
nuno.sa@analog.com, michael.bommarito@gmail.com,
prostitisgabriel@gmail.com, kees@kernel.org,
linux-usb@vger.kernel.org,
Ingo Rohloff <ingo.rohloff@lauterbach.com>
Subject: [PATCH v2 1/1] usb: gadget: f_fs: Fix synchronous read() behavior
Date: Fri, 20 Nov 2026 15:59:37 +0100 [thread overview]
Message-ID: <20261120145937.39772-2-ingo.rohloff@lauterbach.com> (raw)
In-Reply-To: <20261120145937.39772-1-ingo.rohloff@lauterbach.com>
Some UDCs (USB Device Controllers) require, that USB OUT requests (host ->
device), have a size which is a multiple of the USB OUT Endpoint
MaxPacketSize.
If this is the case, then the flag "quirk_ep_out_aligned_size" in
"struct usb_gadget" is set.
The current implementation in "f_fs.c" for synchronous reads, behaves
differently for UDCs which have this flag set to UDCs which have this flag
NOT set, because ffs_epfile_io() uses usb_ep_align_maybe() and this
function then aligns the request size if the flag is set.
Example:
Userspace executes a read() with 128 bytes. For a HighSpeed OUT transfer
(MaxPacketSize == 512), usb_ep_align_maybe() transforms this into a 512
byte request if quirk_ep_out_aligned_size is set.
The Host sends an OUT transfer with 264 bytes (136 bytes more than
userspace requested). These extra 136 bytes are buffered in
"__ffs_epfile_read_data()" and returned for the next userspace read via
"__ffs_epfile_read_buffered()"
If "quirk_ep_out_aligned_size" is NOT set, then "ffs_epfile_io()" passes
the userspace read without modification to the UDC; so in this case the
UDC starts a 128 byte OUT request.
When the Host now sends an OUT transfer with 264 bytes, it's unclear what
happens, because this completely depends on the behavior of the UDC and
the UDC driver implementation.
Likely the extra data received from the Host gets just thrown away;
that's for example what the DWC3 UDC driver does.
So the behavior of an UDC, which sets "quirk_ep_out_aligned_size" is
different from the behavior of an UDC, which doesn't set this flag.
The first behavior is what userspace expects (no data should get thrown
away). This patch enforces the first behavior (code buffers extra data),
by **always** aligning the USB OUT request size to the Endpoints
MaxPacketSize.
Signed-off-by: Ingo Rohloff <ingo.rohloff@lauterbach.com>
---
drivers/usb/gadget/function/f_fs.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
index c64a268e98a4..53c92d149845 100644
--- a/drivers/usb/gadget/function/f_fs.c
+++ b/drivers/usb/gadget/function/f_fs.c
@@ -1078,11 +1078,12 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data)
}
data_len = iov_iter_count(&io_data->data);
/*
- * Controller may require buffer size to be aligned to
- * maxpacketsize of an out endpoint.
+ * To expose a stream like interface for read(), make sure
+ * the enqueued read request is a multiple of the maxpacketsize
+ * of an out endpoint.
*/
if (io_data->read)
- data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
+ data_len = usb_ep_align(ep->ep, data_len);
io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE;
spin_unlock_irq(&epfile->ffs->eps_lock);
--
2.55.0
next prev parent reply other threads:[~2026-11-20 14:59 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 14:59 [PATCH v2 0/1] usb: gadget: f_fs: Fix synchronous read() behavior Ingo Rohloff
2026-11-20 14:59 ` Ingo Rohloff [this message]
2026-11-20 15:09 ` [PATCH v2 1/1] " sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261120145937.39772-2-ingo.rohloff@lauterbach.com \
--to=ingo.rohloff@lauterbach.com \
--cc=gregkh@linuxfoundation.org \
--cc=kees@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=me@samcday.com \
--cc=michael.bommarito@gmail.com \
--cc=nkapron@google.com \
--cc=nuno.sa@analog.com \
--cc=prostitisgabriel@gmail.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox