firehose/usb: Explicitly handle ZLP on USB read transfers #162
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


The previous assumption that every other transfer in Firehose reads is null was incorrect. A Zero-Length Packet (ZLP) is only sent when the received chunk size exactly matches the USB IN endpoint's Max Packet Size.
In such cases, the device sends a ZLP to indicate end-of-transfer, but libusb does not implicitly consume it because it only reads the number of bytes requested. Therefore, we must explicitly read and discard the ZLP, similar to what we already do for USB writes. example: read 512/512 (data) + read 0/512 (ZLP)
Unconditionally expecting an empty packet was not an issue with USB 2.0, where the typical 512-byte sector size matches the bulk IN endpoint's Max Packet Size. However, with USB SuperSpeed (Max Packet Size = 1024), attempting to read an empty packet after a 512/1024-byte transfer is unnecessary and fails (timeout), resulting in errors during storage device sector size discovery:
Fix and move ZLP handling to bus-specific code (usb.c).
Reported-by: Danilo Leo d.leo@arduino.cc