fix: separate host expected bytes from device intended bytes #5109
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.
Here's a few bugfixes for issues I came across working on the nrf52840 usb msc support in #5011. I kept chasing a failure to return a CSW (post-command status report) after the host requested one of a few different unsupported commands. Fixing one broke the other and so I did some digging into places where the code was diverging from the original TinyUSB reference logic. I'm still working on the nrf52840 bit, but I want to make sure the msc library itself is correct and testable so I don't just shift the bug around.
This first commit solves a bug I introduced while porting the TinyUSB stack over. I naively assumed
p_msc->cbw.total_bytes(the host's expected transfer size) andp_msc->total_len(the device's expected transfer size) could be combined into one variable, originallym.totalBytes. As it turns out, the device's transfer size is routinely smaller than what the host provides for, such as in the case of inquiry commands where the host sends a large expected transfer size to give the device plenty of room to provide all the data it needs, while less complex devices like this usb msc stack will only return a small fraction of that, and the difference (referred to as residue in usb) needs to be kept track of in case of errors. This change separates the usage more clearly, always referencing the host's expected total byte value through the CBW.This one handles the situation where the device is returning less data than the host expects such as during inquiry or vpd commands where the returned output can vary greatly in length depending on the device.
This one's pretty self-explanatory, just ensure we don't override an error state set earlier when a CSW is sent after the endpoints are un-stalled.