Author Topic: USB CDC-ACM Class Host Driver for TI's AM335x Processor  (Read 20786 times)

ramakrishna

  • Member
  • ***
  • Posts: 10
USB CDC-ACM Class Host Driver for TI's AM335x Processor
« on: November 27, 2014, 11:51:04 am »
Dear all,
              I am developing the CDC-ACM Class (Host)Driver for 3G Device( 3G Module) which are connected to the TI's AM335x Processor. I am using the latest bare metal Starter ware SDK for developing. The current release will not support the CDC-ACM Host Class,but i am developing  by taking the reference from MSC & HID Classes.The enumeration and some part of handling he BULK End points are completed. The 3G module has 14 interface i.e 14 End points(7 Intr IN, 7 BULK IN, 7 BULK OUT).

 I have few queries regarding the some HOST functionalities and how to develop in the bare metal software.       

1.According to the USB Specification, The Host has to poll the Bus(means sending IN tokens) for interrupt IN end points.How this task is achieved in the HOST Implementation with out effecting the other devices running in the system?

2.I saw some wire shark logs of the Device (when connected to PC),found that HOST PC is polling  for the BULK IN End Points. How this state of the End point is maintained in the HOST PC for polling?Is there any approach to implement BULK IN polling ?

3.Can anyone please elaborate the ZLP?

Thanks & Regards
Rama Krishna     

Tsuneo

  • Frequent Contributor
  • ****
  • Posts: 145
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #1 on: November 28, 2014, 08:09:58 am »
Quote
1.According to the USB Specification, The Host has to poll the Bus(means sending IN tokens) for interrupt IN end points.How this task is achieved in the HOST Implementation with out effecting the other devices running in the system?

Refer to usbhhid.c, which drives the interrupt IN endpoint.
C:\ti\AM335X_StarterWare_02_00_01_01\usblib\host\usbhhid.c

1. HIDDriverOpen() assigns callback routine (HIDIntINCallback()) to the interrupt IN pipe using USBHCDPipeAllocSize().

2. The stack calls HIDIntINCallback() with USB_EVENT_SCHEDULER
- This routine starts "polling" IN at the target endpoint using USBHCDPipeSchedule()

3. When the device returns a packet at the IN endpoint, HIDIntINCallback() is called by the stack with USB_EVENT_RX_AVAILABLE.
- This routine passes USB_EVENT_RX_AVAILABLE event to the callback of the upper layer.
- The upper layer callback reads out the packet using USBHHIDGetReport(), in which the packet is actually read out by USBHCDPipeReadNonBlocking()


Quote
How this state of the End point is maintained in the HOST PC for polling?

For a "polling" bulk IN EP, you may apply the same code pattern as above "polling" interrupt EP


Quote
3.Can anyone please elaborate the ZLP?

For bulk IN/OUT transfer of your case,

On the bus, long Transfer is split into Transactions of wMaxPacketSize of the endpoint. At the end of Transfer, the last short Transactions, less than wMaxPacketSize, is sent by the sender. And then, the receiver knows the end of Transfer.

When the Transfer size is just a multiple of wMaxPacketSize, ZLP (Zero-Length Packet/ transaction) is sent to indicate the termination of a transfer, explicitly.
(Unfortunately, PC host often doesn’t follow this principle)

In this stack, to send ZLP, your host code calls USBEndpointDataSend(), without putting any data to the FIFO.

Tsuneo

ramakrishna

  • Member
  • ***
  • Posts: 10
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #2 on: February 22, 2015, 09:04:57 am »
Hello Tsueno,
                     Thanks for the valuable suggestions.

When the Transfer size is just a multiple of wMaxPacketSize,  ZLP (Zero-Length Packet/ transaction) is sent to indicate the termination of a transfer, explicitly.
(Unfortunately, PC host often doesn’t follow this principle)

we require the Zero Length Packet to end of the USB transfer, because our TI AM335x Device will work on that principle .How can we find the solution for this Zero length packet  termination?
According to USB 2.0 Specification Zero Length packet is required right.

1)Is it possible to change in the Host PC Driver(Linux & Windows), to send the Zero length Packets?

As i mentioned if it is not feasible our logic will not work. Please guide me in the correct way.


Thanks & Regards
Rama Krishna

Tsuneo

  • Frequent Contributor
  • ****
  • Posts: 145
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #3 on: February 23, 2015, 01:01:22 pm »
Quote
1)Is it possible to change in the Host PC Driver(Linux & Windows), to send the Zero length Packets?

CDC - ZLP on bulk OUT

Windows:
Once, MS had addressed on the issue in this KB, which added an update to fix it.
http://support.microsoft.com/kb/943198/en-us

By this update,
When the transfer size over WriteFile is just the multiple of 64 bytes (wMaxPacketSize), usbser.sys puts ZLP over bulk OUT pipe.

This revision was applied to WinXP SP3.
Unfortunately, this revision hadn't been applied to later OS versions.


Linux:

On Linux, cdc-acm module (cdc-acm.c) is assigned to a CDC device.
In this module, acm_tty_write() does nothing when your application calls "write(fd, NULL, 0);", because of this part of the code.

http://lxr.free-electrons.com/source/drivers/usb/class/cdc-acm.c#L669
Code: [Select]
669 static int acm_tty_write(struct tty_struct *tty,
670                                         const unsigned char *buf, int count)
671 {
672         struct acm *acm = tty->driver_data;
673         int stat;
674         unsigned long flags;
675         int wbn;
676         struct acm_wb *wb;
677
678         if (!count)        // <-----
679                 return 0;  // <-----

If you would delete above two lines, "write(fd, NULL, 0);" should put ZLP to the bulk OUT.

Tsuneo

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #4 on: February 23, 2015, 04:45:02 pm »
I'm not sure why you require zero length packets.

CDC is a stream protocol so the idea of end of transfer really doesn't mean anything. I've actually been battling a bug recently where someone expects "transfers" on CDC and it screws up all of my logic. Its a bad idea, don't do it. As you see most CDC implementations don't use transfers because they are a bad idea.

If the problem is you're not getting notified of arriving packets because its not the end of the "transfer", you may need to be making 64 byte (max packet sized) reads to the end point. A read for a single max packet sized transfer will always terminate on any received packet. A packet which is shorter than max packet sized is explicitly an end of transfer because the packet is short than max packet (no ZLP needed). A packet which is exactly max packet size will end the transfer because the transfer is now exactly satisfied (no ZLP needed). So any packet terminates the transfer (no ZLP needed).

Our CDC firmware uses max packet sized reads and that has never caused us a problem.

Tsuneo

  • Frequent Contributor
  • ****
  • Posts: 145
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #5 on: February 23, 2015, 11:08:15 pm »
As of the bulk IN of CDC-ACM drivers (module),

Windows:
usbser.sys passes 4K bytes URB to USBDI

Linux (since 2.6):
cdc-acm.c queues ACM_NR (16) number of URBs, whose size is 2 x MPS (wMaxPacketSize)

In either OS, device should terminate "Transfer" with short packet, including ZLP.

And then, asymmetry exists in bulk IN/OUT termination.
bulk IN - device requires ZLP termination.
bulk OUT - host doesn't put ZLP termination.


In the CDC-EEM spec (CDC_EEM10.pdf, http://www.usb.org/developers/docs/devclass_docs/CDC_EEM10.pdf ),
ZLP (or ZLE) termination is explicitly defined.

5.2 Transfers
As per [USB2.0] specification, section 5.8.3, the last packet of the USB transfer shall be a short packet, to mark the end of the transfer. If the buffered data would not result in the last packet being a short packet, i.e. the data length is an exact multiple of bMaxpacket, an additional packet shall be added to insure that the last packet is a short packet. This additional packet can either be a ZLP (zero length packet) or ZLE (refer to 5.1.2.3.1) packet.


On the other hand, CDC-ACM spec (PSTN120.pdf) leaves transfer termination undefined.

I have feeling that the CDC-ACM spec would make it intentionally, so that implementation could choose either option.

Tsuneo

ramakrishna

  • Member
  • ***
  • Posts: 10
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #6 on: February 23, 2015, 11:18:34 pm »
Hi Barry & Tsueno,
                              Thanks for the quick response.

 CDC is a stream protocol so the idea of end of transfer really doesn't mean anything.
We are using the TI Am335x Processor ,which has MUSB Core inbuilt. We are trying to use the CPPI4.1 DMA of AM335x for large USB Transfers. The DMA has a specific requirement like need a end of transfer either short length packet or NULL packet.
 
Tsueno:
Thanks for your valuable suggestions, I will try with the mentioned above.  

Thanks & Regards
Rama Krishna

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #7 on: February 24, 2015, 07:31:00 pm »
The DMA has a specific requirement like need a end of transfer either short length packet or NULL packet.
You mean it doesn't terminate if the transfer is exactly satisfied?

ramakrishna

  • Member
  • ***
  • Posts: 10
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #8 on: February 25, 2015, 12:12:41 am »
Hello Barry,
                   Yes,exactly. DMA is not terminating the transfer if the data size multiple's of MAX endpoint Size.
Thanks & Regards
Rama Krishna

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #9 on: February 25, 2015, 01:42:24 am »
Yes,exactly. DMA is not terminating the transfer if the data size multiple's of MAX endpoint Size.
That's a different question. Does the transfer terminate if it is exactly satisfied. So if you ask for X bytes and X bytes arrive, does the transfer terminate, even if that is a multiple of the max packet size?

ramakrishna

  • Member
  • ***
  • Posts: 10
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #10 on: February 25, 2015, 09:48:22 am »
Hello Barry,
                  At first the DMA don't know the size of data to be received.We have flexibility in the  DMA to receive specific amount of  bytes,this is not enough to our data processing logic.We need a termination for every USB Transfer(not a transaction). 
Thanks & Regards
Rama Krishna

Barry Twycross

  • Frequent Contributor
  • ****
  • Posts: 263
Re: USB CDC-ACM Class Host Driver for TI's AM335x Processor
« Reply #11 on: February 26, 2015, 02:32:07 pm »
I'm not sure if you're missing my point or if you've just thought you way into a hole you can't get out of.

Ask your DMA for Max packet sized transfers and it will work.