When do USB Hosts require a zero-length IN packet at the end of a Control Read Transfer?

旧街凉风 提交于 2019-11-27 20:53:44
MBR

Read carefully USB specification:

The Data stage of a control transfer from an endpoint to the host is complete when the endpoint does one of the following:

  • Has transferred exactly the amount of data specified during the Setup stage
  • Transfers a packet with a payload size less than wMaxPacketSize or transfers a zero-length packet

So, in your case, when wLength == transfer size, answer is NO, you don't need ZLP.

In case wLength > transfer size, and (transfer size % ep0 size) == 0 answer is YES, you need ZLP.

In general, USB uses a less-than-max-length packet to demarcate an end-of-transfer. So in the case of a transfer which is an integer multiple of max-packet-length, a ZLP is used for demarcation.

You see this in bulk pipes a lot. For example, if you have a 4096 byte transfer, that will be broken down into an integer number of max-length packets plus one zero-length-packet. If the SW driver has a big enough receive buffer set up, higher-level SW receives the entire transfer at once, when the ZLP occurs.

Control transfers are a special case because they have the wLength field, so ZLP isn't strictly necessary.

But I'd strongly suggest SW be flexible to both, as you may see variations with different USB host silicon or low-level HCD drivers.

I would like to expand on MBR's answer. The USB specification 2.0, in section 5.5.3, says:

The Data stage of a control transfer from an endpoint to the host is complete when the endpoint does one of the following:

  • Has transferred exactly the amount of data specified during the Setup stage
  • Transfers a packet with a payload size less than wMaxPacketSize or transfers a zero-length packet

When a Data stage is complete, the Host Controller advances to the Status stage instead of continuing on with another data transaction. If the Host Controller does not advance to the Status stage when the Data stage is complete, the endpoint halts the pipe as was outlined in Section 5.3.2. If a larger-than-expected data payload is received from the endpoint, the IRP for the control transfer will be aborted/retired.

I added emphasis to one of the sentences in that quote because it seems to specifically say what the device should do: it should "halt" the pipe if the host tries to continue the data phase after it was done, and it is done if all the requested data has been transmitted (i.e. the number of bytes transferred is greater than or equal to wLength). I think halting refers to sending a STALL packet.

In other words, the device does not need a zero-length packet in this situation and in fact the USB specification says it should not provide one.

You don't have to. (*)

The whole point of wLength is to tell the host the maximum number of bytes it should attempt to read (but it might read less !)

(*) I have seen devices that crash when IN/OUT requests were made at incorrect time during control transfers (when debugging our host solution). So any host doing what you are worried about, would of killed those devices and is hopefully not in the market.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!