What is "zero-length packet"?
Asked Answered
G

1

6

USB standard mentions writing "zero-length" packets in some scenarios.

Can anyone please explain what is the meaning of "zero-length" packets in USB terminology ?

What is "short packet" ? (seems that both "short packet" and "zero-length packet" are mentioned together)

Gypsy answered 25/2, 2018 at 15:45 Comment(1)
And is this different from a "null packet" mentioned in the Audio, MIDI, and Debug Class specs?Lutenist
C
8

Both of the terminologies are used for transaction completion mechanism. The maximum size of data packets can be derived from bMaxPacketSize stored in Endpoint descriptors. When data packet is less than that, its called a Short Packet. Similarly when you can send a data packet with length Zero.

Now the question is "Why". Lets discuss that.

Reasons for Short packets -

1 - As per USB specification, When the host or device receives a data packet with packet length less than maximum packet size, it shall consider the transfer is complete and no more data packet is left to receive.

2 - This is always the case for Bulk transfer (Mass storage devices). You do not always ask data with length multiple of MaxPacketSize. So the last packet will always be less than max packet size. Consider you want 1025 bytes of data from bulk endpoint whose max packet size is 1024 bytes.

First Data Packet = 1024 bytes Seconds Data Packet = 1 byte (Short Packet).

This way the host will know that the data transfer is complete.

Reason for Zero length packet -

1 - Like Short packet, zero length packet is also used for transaction complete. Consider you want to send 2048 bytes of bulk data.

First data packet = 1024 bytes Second data packet = 1024 bytes.

Now the problem is, both are equal to max packet size. The device will not understand that the transfer is complete. Then you send a zero length packet.

Third data packet = 0 bytes

This will make the transfer complete and device can then start processing the data.

2 - Zero length packet is also used in control transfer during status stage to notify successful completion.

Cullie answered 26/2, 2018 at 6:46 Comment(4)
When using "libusb_bulk_transfer" (or the kernel transfer) do we care that more than one packet was sent for a one transfer or is it transparent to the user of these APIs? And in case the transfer was not complete in a call to libusb_bulk_transfer , what should we do ?Gypsy
No its not transparent. Your code needs to deal with ZLP (Zero Length packet). If you do not deal with ZLP, you may see wrong behaviors. Usually Short packets are sent as the last packet automatically because the length was not aligned to the max packet size boundary. But ZLP needs a extra transfer request from user space.Cullie
"An isochronous IN endpoint must return a zero-length packet whenever data is requested at a faster interval than the specified interval and data is not available."Lutenist
"it shall consider the transfer is complete and no more data packet is left to receive" isn't true for isochronous transfers, which only use the maxpacketsize briefly to correct for rate differences.Lutenist

© 2022 - 2024 — McMap. All rights reserved.