Author Topic: zero length packet with Bulk transfer  (Read 15963 times)

nvd

  • Member
  • ***
  • Posts: 42
zero length packet with Bulk transfer
« on: January 19, 2013, 03:15:34 am »
Hi,
Need some suggestions on handing zero length packet with Bulk transfer.

Some protocols which use USB require an extra zero length data packet to signal end-of-transfer on bulk endpoints, if the last data packet is exactly wMaxPacketSize bytes long. (e.g. VCP)
Others, where the transfer size is known do not need ZLP (e.g. Mass storage)

To come up with USB device stack which can support both the requirements, I plan to provide a flag which indicates if ZLP should be sent or not, when the last data packet is exactly wMaxPacketSize bytes long.

This flag allows applications/class drivers to inform about this requirement, so that the underlying USB stack can handle the issue transparently.

Question: Is this a good approach for this situation? or please recommend other/better options.

If I go with this approach, I would like to know, what time is appropriate to inform the underlying driver about this flag.
It can be done at the configuration time, when ENDPOINT is being configured. That makes it a one time activity, meaning all the transfers will use ZLP (when required) if the flag was set at the EP configuration time.
Or the flag can be provided everytime when data buffer is provided to the driver. underlying driver will decide on the fly if the ZLP should be sent or not.

Question: I would like to know, Is there a possibility with Bulk transfers that the ENDPOINT needs to use ZLP or does not need to use zlp on the fly.
 My understanding is that on the device side once the EP is configured for particular class/protocol, the configuration will remain same throughout.

If the same implementation need to be done on the USB stack for the host side, I guess the Indication flag to use ZLP or not use ZLP should be done everytime when data buffer is provided.

Question: Is this a good approach for Host stack?

Thanks and regards,
NVD



nvd

  • Member
  • ***
  • Posts: 42
Re: zero length packet with Bulk transfer
« Reply #1 on: January 20, 2013, 11:40:26 am »
Additional Question.

Is the question of terminating wMaxPacketSize length packet with ZLP or not terminating it applicable to Interrupt transfers as well?

I haven't come across a device which uses interrupt endpoint to transfer data more than wMaxPacketSize.
But USB2.0 spec does mention about it in section 5.7.3

Comments/suggestions are much appreciated.

Regards,
NVD

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: zero length packet with Bulk transfer
« Reply #2 on: January 20, 2013, 12:50:28 pm »
Yes, the HID spec says that all reports except the longest which exceed wMaxPacketSize for the endpoint
must terminate with a short packet (which would be a ZLP if the report is an example multiple of wMaxPacketSize).

After enumeration, a driver can select a different configuration. Even if this is very rare, it seems safest to tie the requirement to the current class or vendor protocol, updating it as needed if the configuration changes.

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: zero length packet with Bulk transfer
« Reply #3 on: January 21, 2013, 01:43:25 pm »
Our stack allows the driver to specify a zero length transfer (bulk or interrupt), then leaves it up to the driver to schedule the extra transfer if it thinks it needs it. That makes for a simple implementation for the USB driver, but does put some extra work on the class driver.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: zero length packet with Bulk transfer
« Reply #4 on: January 21, 2013, 01:49:18 pm »
Our stack allows the driver to specify a zero length transfer (bulk or interrupt), then leaves it up to the driver to schedule the extra transfer if it thinks it needs it. That makes for a simple implementation for the USB driver, but does put some extra work on the class driver.

This way makes the most sense to me since the class driver is the one that determines whether a ZLP is needed.

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: zero length packet with Bulk transfer
« Reply #5 on: January 21, 2013, 07:43:17 pm »
nvd's approach makes a lot of sense to me. The class driver is still the one making the decision that a ZLP is needed, it just doesn't need to do the messy arithmetic to work out if one is actually needed for the particular transfer.

The logic can be implemented and debugged once in the USB driver and then all class drivers take advantage of that, rather than each class driver needing to implement (and debug) it.

Our way makes the USB Driver easy.

nvd

  • Member
  • ***
  • Posts: 42
Re: zero length packet with Bulk transfer
« Reply #6 on: January 22, 2013, 09:39:36 am »
Thanks for the comments Jan and Barry.

I went ahead and implemented the flag to indicate USB driver to use ZLP or not at the endpoint configuration time.
Though the decision to use ZLP or not is class specific, the motivation to handle generation of ZLP in USB drivers was
1) The USB Core that we have, has a special arrangement to generate extra ZLP at the end of transfer if configured. So USB driver is not affected much.
2) Makes it easy for the new class implementers (customers).

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: zero length packet with Bulk transfer
« Reply #7 on: January 22, 2013, 02:15:26 pm »
I would also say you should make sure that your stack will accept an explicit request for a transfer of zero bytes, and generate a ZLP in that case. We have managed to flag such requests as errors in the past, and no good came of that. Its rather easy to forget to include zero as a legitimate value in your bounds checking. You might find some driver logic relies on the capability to explicitly send a ZLP, not tied to any other transfer.

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: zero length packet with Bulk transfer
« Reply #8 on: January 22, 2013, 02:17:35 pm »
Also, I'd definitely make the flag per transfer, not per endpoint.