天天看点

usb g_zero driver

Gadget Zero is a two-configuration device.  It either sinks and                                                                

sources bulk data; or it loops back a configurable number of                                                                          

transfers.  It also implements control requests, for "chapter 9"                                                                            

conformance.  The driver needs only two bulk-capable endpoints, so                                                                   

it can work on top of most device-side usb controllers.  It's                                                                                  

useful for testing, and is also a working example showing how                                                                                 

USB "gadget drivers" can be written.                                                                                                           

Make this be the first driver you try using on top of any new                                                                                 

 USB peripheral controller driver.  Then you can use host-side                                                                                  

test software, like the "usbtest" driver, to put your hardware                                                                               

 and its driver through a basic set of functional tests.                                                                                     

Gadget Zero also works with the host-side "usb-skeleton" driver,                                                                               

and with many kinds of host-side test software.  You may need                                                                                

 to tweak product and vendor IDs before host software knows about                                                                              

 this device, and arrange to select an appropriate configuration.

1. kernel config:

Device Drivers  --->

    [*] USB support  --->

        <*> USB Gadget Support

            <*>   USB Gadget precomposed configurations (Gadget Zero (DEVELOPMENT))  --->   Gadget Zero (DEVELOPMENT)

2. driver:

2.1 driver

  • drivers/usb/gadget/legacy/zero.c
  • drivers/usb/gadget/function/f_sourcesink.c
  • drivers/usb/gadget/function/f_loopback.c

build it will generate two ko:

drivers/usb/gadget/function/usb_f_ss_lb.ko

drivers/usb/gadget/legacy/g_zero.ko

Note, we need to insmod usb_f_ss_lb.ko first, then insmod g_zero.ko

2.2 module paramater

for g_zero.ko

1) pattern

for out data check

  • 0, all zeroes
  • 1, mod63
  • 2, none check

2.3  "loopback" configuration

drivers/usb/gadget/function/f_loopback.c

receive data from host and then send them to host.

This takes messages of various sizes written OUT to a device, and loops

them back so they can be read IN from it.  It has been used by certain

test applications.  It supports limited testing of data queueing logic.

  • support bulk in & bulk out
  • support full/high/super speed
  • support multi-queue

2.4 "source/sink" configuration

drivers/usb/gadget/function/f_loopback.c

source: writes data to the host always.

sink: reads anything from the host always.

This just sinks bulk packets OUT to the peripheral and sources them IN

to the host, optionally with specific data patterns for integrity tests.

As such it supports basic functionality and load tests.

In terms of control messaging, this supports all the standard requests

plus two that support control-OUT tests.  If the optional "autoresume"

mode is enabled, it provides good functional coverage for the "USBCV"

test harness from USB-IF.

  • support two alternative interface: source_sink_intf_alt0 and source_sink_intf_alt1
  • support full/high/super speed
  • support bulk in and bulk out(not loopback, source_sink_intf_alt0 and source_sink_intf_alt1)
  • suppoer iso in and iso out(not loopback, source_sink_intf_alt1)
  • support control out and control in, loopback, receive data and then send them.
  • support multi-queue

3. test with testusb

3.1 insmod in arm linux

# insmod ./usb_f_ss_lb.ko

# insmod ./g_zero.ko

[  348.729356] zero gadget: Gadget Zero, version: Cinco de Mayo 2008

[  348.759979] zero gadget: zero ready

3.2 plugin usb cable into ubuntu host

arm linux: 

# [  382.695297] zero gadget: high-speed config #3: source/sink

ubuntu:

usb g_zero driver

[ 6832.395014] usb 1-1: new high-speed USB device number 10 using ehci-pci

[ 6832.792045] usb 1-1: New USB device found, idVendor=0525, idProduct=a4a0

[ 6832.792052] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3

[ 6832.792056] usb 1-1: Product: Gadget Zero

[ 6832.792059] usb 1-1: Manufacturer: Linux 4.19.125 with dwc3-gadget

[ 6832.792062] usb 1-1: SerialNumber: 0123456789.0123456789.0123456789

//usbtest.ko

[ 6832.818623] usbtest 1-1:3.0: Linux gadget zero

[ 6832.818629] usbtest 1-1:3.0: high-speed {control in/out bulk-in bulk-out} tests (+alt)

3.3 run testusb on host

sudo ./testusb -D /dev/bus/usb/001/010

ba[email protected]:~/usbtest/usb$ sudo ./testusb -D /dev/bus/usb/001/010

unknown speed    /dev/bus/usb/001/010    0

/dev/bus/usb/001/010 test 0,    0.000018 secs

/dev/bus/usb/001/010 test 1 --> 71 (Protocol error)

/dev/bus/usb/001/010 test 2,    1.986916 secs

/dev/bus/usb/001/010 test 3 --> 110 (Connection timed out)

/dev/bus/usb/001/010 test 4,    1.987645 secs

/dev/bus/usb/001/010 test 5 --> 110 (Connection timed out)

/dev/bus/usb/001/010 test 6,    9.298146 secs

/dev/bus/usb/001/010 test 7 --> 110 (Connection timed out)

/dev/bus/usb/001/010 test 8,    9.060683 secs

/dev/bus/usb/001/010 test 9,   36.018188 secs

......