Edit online

Device 侧 gadget zero

提供测试需要的 Device 设备有很多种方式,例如可用使用专门的测试 Device 里面烧录专有的测试 Firmware。节约成本的方式还是使用 Linux gadget 功能来动态模拟 USB Device 设备。针对 USB 测试,Linux 专门提供了 gadget zero 设备。

Device 创建

gadget zero 的核心是创建一个 Composite Device ,其包含了两个 Configuration ,其中一个 Configuration 0 包含 SourceSink Function/Interface ,另一个 Configuration 1 包含 Loopback Function/Interface 。某一时刻只能选择使用一个 Configuration ,通常情况下使用 Configuration 0SourceSink 的功能。

gadget zero Device 由两种方式创建:

  • 1、通过 zero_driver 创建,只要把对应驱动文件 drivers\usb\gadget\legacy\zero.c 编译进内核即可。

  • 2、通过 functionfs 动态创建,这种方式更灵活,实例命令如下:
    mount -t configfs none /sys/kernel/config
    cd /sys/kernel/config/usb_gadget
    
    mkdir g2
    cd g2
    
    echo "0x04e8" > idVendor
    echo "0x2d01" > idProduct
    
    mkdir configs/c.1
    mkdir configs/c.2
    mkdir functions/Loopback.0
    mkdir functions/SourceSink.0
    
    mkdir strings/0x409
    mkdir configs/c.1/strings/0x409
    mkdir configs/c.2/strings/0x409
    
    echo "0x0525" > idVendor
    echo "0xa4a0" > idProduct
    
    echo "0123456789" > strings/0x409/serialnumber
    echo "Samsung Inc." > strings/0x409/manufacturer
    echo "Bar Gadget" > strings/0x409/product
    
    echo "Conf 1" > configs/c.1/strings/0x409/configuration
    echo "Conf 2" > configs/c.2/strings/0x409/configuration
    echo 120 > configs/c.1/MaxPower
    
    // SourceSink:驱动 set configuration 会选取 第一个 configuration
    ln -s functions/Loopback.0 configs/c.2
    ln -s functions/SourceSink.0 configs/c.1
    
    echo 4100000.udc-controller > UDC
    
整个过程就是创建了一个 Vendor ID = 0x0525Product ID = 0xa4a0Composite Device ,在 Host 侧可以查看这个设备:
$ lsusb -s 1:3
Bus 001 Device 003: ID 0525:a4a0 Netchip Technology, Inc. Linux-USB "Gadget Zero"

$ lsusb -v -s 1:3

Bus 001 Device 003: ID 0525:a4a0 Netchip Technology, Inc. Linux-USB "Gadget Zero"
Couldn't open device, some information will be missing
Device Descriptor:
bLength                18
bDescriptorType         1
bcdUSB               2.00
bDeviceClass            0
bDeviceSubClass         0
bDeviceProtocol         0
bMaxPacketSize0        64
idVendor           0x0525 Netchip Technology, Inc.
idProduct          0xa4a0 Linux-USB "Gadget Zero"
bcdDevice            5.10
iManufacturer           1
iProduct                2
iSerial                 3
bNumConfigurations      2
Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x0045
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          4
    bmAttributes         0x80
    (Bus Powered)
    MaxPower              120mA
    Interface Descriptor:
    bLength                 9
    bDescriptorType         4
    bInterfaceNumber        0
    bAlternateSetting       0
    bNumEndpoints           2
    bInterfaceClass       255 Vendor Specific Class
    bInterfaceSubClass      0
    bInterfaceProtocol      0
    iInterface              0
    Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
        Transfer Type            Bulk
        Synch Type               None
        Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
    Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
        Transfer Type            Bulk
        Synch Type               None
        Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
    Interface Descriptor:
    bLength                 9
    bDescriptorType         4
    bInterfaceNumber        0
    bAlternateSetting       1
    bNumEndpoints           4
    bInterfaceClass       255 Vendor Specific Class
    bInterfaceSubClass      0
    bInterfaceProtocol      0
    iInterface              0
    Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
        Transfer Type            Bulk
        Synch Type               None
        Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
    Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
        Transfer Type            Bulk
        Synch Type               None
        Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
    Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            1
        Transfer Type            Isochronous
        Synch Type               None
        Usage Type               Data
        wMaxPacketSize     0x0400  1x 1024 bytes
        bInterval               4
    Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            1
        Transfer Type            Isochronous
        Synch Type               None
        Usage Type               Data
        wMaxPacketSize     0x0400  1x 1024 bytes
        bInterval               4
Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x0020
    bNumInterfaces          1
    bConfigurationValue     2
    iConfiguration          5
    bmAttributes         0x80
    (Bus Powered)
    MaxPower                2mA
    Interface Descriptor:
    bLength                 9
    bDescriptorType         4
    bInterfaceNumber        0
    bAlternateSetting       0
    bNumEndpoints           2
    bInterfaceClass       255 Vendor Specific Class
    bInterfaceSubClass      0
    bInterfaceProtocol      0
    iInterface              6
    Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
        Transfer Type            Bulk
        Synch Type               None
        Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0
    Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
        Transfer Type            Bulk
        Synch Type               None
        Usage Type               Data
        wMaxPacketSize     0x0200  1x 512 bytes
        bInterval               0