laforge submitted this change.
host_communication: send a ZLP after any message of whole packets
usb_write_cb() ends a message with a zero-length packet only when
transferred == ep_size, that is for a message of exactly one packet.
The SAM3S USB HAL reports the whole message in transferred, so a
message of 128, 192 or 256 bytes leaves the board as full packets with
no short packet behind it, and a host that reads into a larger buffer
sees it only when the board sends its next message. In card emulation
that is the RX-DATA message of a modem command with Lc 72, B2 or F2:
osmo-remsim-client-st2 submits 4096-byte bulk IN transfers, so such a
command stays on the host until the modem gives up and resets the
card, about 120 s later.
Send the ZLP whenever a non-empty message is a whole number of packets
and the transfer covered all of it. The callback after the ZLP sees
transferred == 0 and frees the buffer as before.
The folder osmocom-simtrace2-cardem-zlp of
hampushellsberg-dev/PySIM-upstream-patches on GitHub has the analysis,
a journal repro and the before/after validation.
Change-Id: Ie3cf86cb79d523d12b2993a3019b83e9b1c9c4e0
---
M firmware/libcommon/source/host_communication.c
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/firmware/libcommon/source/host_communication.c b/firmware/libcommon/source/host_communication.c
index b2faf1b..d61d977 100644
--- a/firmware/libcommon/source/host_communication.c
+++ b/firmware/libcommon/source/host_communication.c
@@ -34,7 +34,12 @@
TRACE_DEBUG("%s (EP=0x%02x)\r\n", __func__, bep->ep);
- if (((msgb_length(msg) % ep_size) == 0) && (transferred == ep_size)) {
+ /* A transfer that fills its last packet ends only at a short packet, so a
+ * message of any whole number of packets needs a ZLP, not just one of a
+ * single packet: the host reads into a larger buffer and would otherwise
+ * see this message only once the next one arrives. */
+ if (msgb_length(msg) && ((msgb_length(msg) % ep_size) == 0) &&
+ (transferred == msgb_length(msg))) {
/* terminate with ZLP; pass in 'msg' again as 'arg' so we get
* called the second time and proceed with usb_buf_free below */
USBD_Write(bep->ep, 0, 0, (TransferCallback) &usb_write_cb, msg);
To view, visit change 43973. To unsubscribe, or for help writing mail filters, visit settings.