USB Gadget-Source/Sink驱动
2025-09-11
0
0
关于同步传输的示例在f_sourcesink.c中。其编译后依旧位于usb_f_ss_lb.ko中
应用可以通过如下代码来创建设备
root@ATK-IMX6U:/lib/modules/4.1.15# cat fs_lc.sh
#!/bin/bash
modprobe libcomposite
modprobe usb_f_ss_lb
mount -t configfs none /sys/kernel/config
mkdir -p /sys/kernel/config/usb_gadget/g1
cd /sys/kernel/config/usb_gadget/g1
echo "0x1d6b" > idVendor
echo "0x2d01" > idProduct
echo "0x200" > bcdUSB
mkdir configs/c.1
#mkdir configs/c.2
mkdir strings/0x409
mkdir configs/c.1/strings/0x409
echo "0123456789" > strings/0x409/serialnumber
echo "usbzh.com" > strings/0x409/manufacturer
echo "USBZH Gadget" > strings/0x409/product
echo "Conf 1" > configs/c.1/strings/0x409/configuration
echo 120 > configs/c.1/MaxPower
mkdir functions/SourceSink.0
#mkdir functions/Loopback.0
#ln -s functions/Loopback.0 configs/c.2
ln -s functions/SourceSink.0 configs/c.1
# Enable the gadget
udc=$(ls /sys/class/udc/)
echo "$udc" > UDC
创建的设备有1个接口描述符
- 接口0-alt0
- bulk端点in-0x81
- bulk端点out-0x01
- 接口0-alt1
- bulk端点in-0x81
- bulk端点out-0x01
- 同步端点in-0x82
- 同步端点out-0x02
故选择接品alt1时,可以使用同步端点。
其usb描述符信息如下:
=========================== USB Port3 ===========================
Connection Status : 0x01 (Device is connected)
Port Chain : 2-3
Properties : 0x00
IsUserConnectable : no
PortIsDebugCapable : no
PortHasMultiCompanions : no
PortConnectorIsTypeC : no
ConnectionIndex : 0x03 (Port 3)
========================== Summary =========================
Vendor ID : 0x1D6B (The Linux Foundation)
Product ID : 0x2D01
Manufacturer String : The Linux Foundation
Product String : USBZH Gadget
Serial : 0123456789
USB Version : 2.0
Port maximum Speed : High-Speed
Device maximum Speed : High-Speed
Device Connection Speed : High-Speed
Self powered : no
Demanded Current : 120 mA
Used Endpoints : 3
======================== USB Device ========================
+++++++++++++++++ Device Information ++++++++++++++++++
Friendly Name : USBZH Gadget
Device Description : USBZH Gadget
Device Path 1 : \\?\USB#VID_1D6B&PID_2D01#0123456789#{a5dcbf10-6530-11d2-901f-00c04fb951ed} (GUID_DEVINTERFACE_USB_DEVICE)
Device Path 2 : \\?\USB#VID_1D6B&PID_2D01#0123456789#{95ea06b3-7817-4897-8e39-5875cf494374}
Device Path 3 : \\?\USB#VID_1D6B&PID_2D01#0123456789#{dee824ef-729b-4a0e-9c14-b7117d33a817} (GUID_DEVINTERFACE_WINUSB)
Kernel Name : \Device\USBPDO-8
Device ID : USB\VID_1D6B&PID_2D01\0123456789
Hardware IDs : USB\VID_1D6B&PID_2D01&REV_0401 USB\VID_1D6B&PID_2D01
Driver KeyName : {88bae032-5a81-49f0-bc3d-a4ff138216d6}\0013 (GUID_DEVCLASS_USBDEVICE)
Driver : \SystemRoot\System32\drivers\WinUsb.sys (Version: 10.0.18362.1 Date: 2019-03-19 Company: Microsoft Corporation)
Driver Inf : C:\Windows\inf\oem99.inf
Legacy BusType : PNPBus
Class : USBDevice
Class GUID : {88bae032-5a81-49f0-bc3d-a4ff138216d6} (GUID_DEVCLASS_USBDEVICE)
Service : WinUSB
Enumerator : USB
Location Info : Port_#0003.Hub_#0001
Address : 3
Location IDs : PCIROOT(0)#PCI(1400)#USBROOT(0)#USB(3), ACPI(_SB_)#ACPI(PCI0)#ACPI(XHC_)#ACPI(RHUB)#ACPI(HS03)
Container ID : {0be54a71-2522-59e9-85e7-a7f773826dc9}
Manufacturer Info : Linux Foundation
Capabilities : 0x14 (Removable, UniqueID)
Status : 0x0180600A (DN_DRIVER_LOADED, DN_STARTED, DN_DISABLEABLE, DN_REMOVABLE, DN_NT_ENUMERATOR, DN_NT_DRIVER)
Problem Code : 0
EnhancedPowerMgmtEnabled : 0
Power State : D0 (supported: D0, D3, wake from D0)
+++++++++++++++++ Registry USB Flags +++++++++++++++++
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\usbflags\1D6B2D010401
osvc : REG_BINARY 00 00
---------------- Connection Information ---------------
Connection Index : 0x03 (Port 3)
Connection Status : 0x01 (DeviceConnected)
Current Config Value : 0x01 (Configuration 1)
Device Address : 0x31 (49)
Is Hub : 0x00 (no)
Device Bus Speed : 0x02 (High-Speed)
Number of open Pipes : 0x02 (2 pipes to data endpoints)
Pipe[0] : EndpointID=1 Direction=IN ScheduleOffset=0 Type=Bulk wMaxPacketSize=0x200 bInterval=0
Pipe[1] : EndpointID=1 Direction=OUT ScheduleOffset=0 Type=Bulk wMaxPacketSize=0x200 bInterval=0
Data (HexDump) : 03 00 00 00 12 01 00 02 00 00 00 40 6B 1D 01 2D ...........@k..-
01 04 01 02 03 01 01 02 00 31 00 02 00 00 00 01 .........1......
00 00 00 07 05 81 02 00 02 00 00 00 00 00 07 05 ................
01 02 00 02 00 00 00 00 00 .........
--------------- Connection Information V2 -------------
Connection Index : 0x03 (3)
Length : 0x10 (16 bytes)
SupportedUsbProtocols : 0x03
Usb110 : 1 (yes, port supports USB 1.1)
Usb200 : 1 (yes, port supports USB 2.0)
Usb300 : 0 (no, port not supports USB 3.0)
ReservedMBZ : 0x00
Flags : 0x00
DevIsOpAtSsOrHigher : 0 (Device is not operating at SuperSpeed or higher)
DevIsSsCapOrHigher : 0 (Device is not SuperSpeed capable or higher)
DevIsOpAtSsPlusOrHigher : 0 (Device is not operating at SuperSpeedPlus or higher)
DevIsSsPlusCapOrHigher : 0 (Device is not SuperSpeedPlus capable or higher)
ReservedMBZ : 0x00
Data (HexDump) : 03 00 00 00 10 00 00 00 03 00 00 00 00 00 00 00 ................
---------------------- Device Descriptor ----------------------
bLength : 0x12 (18 bytes)
bDescriptorType : 0x01 (Device Descriptor)
bcdUSB : 0x200 (USB Version 2.0)
bDeviceClass : 0x00 (defined by the interface descriptors)
bDeviceSubClass : 0x00
bDeviceProtocol : 0x00
bMaxPacketSize0 : 0x40 (64 bytes)
idVendor : 0x1D6B (The Linux Foundation)
idProduct : 0x2D01
bcdDevice : 0x0401
iManufacturer : 0x01 (String Descriptor 1)
Language 0x0409 : "usbzh.com"
iProduct : 0x02 (String Descriptor 2)
Language 0x0409 : "USBZH Gadget"
iSerialNumber : 0x03 (String Descriptor 3)
Language 0x0409 : "0123456789"
bNumConfigurations : 0x01 (1 Configuration)
Data (HexDump) : 12 01 00 02 00 00 00 40 6B 1D 01 2D 01 04 01 02 .......@k..-....
03 01 ..
------------------ Configuration Descriptor -------------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x02 (Configuration Descriptor)
wTotalLength : 0x0045 (69 bytes)
bNumInterfaces : 0x01 (1 Interface)
bConfigurationValue : 0x01 (Configuration 1)
iConfiguration : 0x04 (String Descriptor 4)
Language 0x0409 : "Conf 1"
bmAttributes : 0x80
D7: Reserved, set 1 : 0x01
D6: Self Powered : 0x00 (no)
D5: Remote Wakeup : 0x00 (no)
D4..0: Reserved, set 0 : 0x00
MaxPower : 0x3C (120 mA)
Data (HexDump) : 09 02 45 00 01 01 04 80 3C 09 04 00 00 02 FF 00 ..E.....<.......
00 00 07 05 81 02 00 02 00 07 05 01 02 00 02 00 ................
09 04 00 01 04 FF 00 00 00 07 05 81 02 00 02 00 ................
07 05 01 02 00 02 00 07 05 82 01 00 04 04 07 05 ................
02 01 00 04 04 .....
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x00 (Interface 0)
bAlternateSetting : 0x00
bNumEndpoints : 0x02 (2 Endpoints)
bInterfaceClass : 0xFF (Vendor Specific)
bInterfaceSubClass : 0x00
bInterfaceProtocol : 0x00
iInterface : 0x00 (No String Descriptor)
Data (HexDump) : 09 04 00 00 02 FF 00 00 00 .........
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x81 (Direction=IN EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0200 (max 512 bytes)
bInterval : 0x00 (never NAKs)
Data (HexDump) : 07 05 81 02 00 02 00 .......
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x01 (Direction=OUT EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0200 (max 512 bytes)
bInterval : 0x00 (never NAKs)
Data (HexDump) : 07 05 01 02 00 02 00 .......
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x00 (Interface 0)
bAlternateSetting : 0x01
bNumEndpoints : 0x04 (4 Endpoints)
bInterfaceClass : 0xFF (Vendor Specific)
bInterfaceSubClass : 0x00
bInterfaceProtocol : 0x00
iInterface : 0x00 (No String Descriptor)
Data (HexDump) : 09 04 00 01 04 FF 00 00 00 .........
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x81 (Direction=IN EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0200 (max 512 bytes)
bInterval : 0x00 (never NAKs)
Data (HexDump) : 07 05 81 02 00 02 00 .......
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x01 (Direction=OUT EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0200 (max 512 bytes)
bInterval : 0x00 (never NAKs)
Data (HexDump) : 07 05 01 02 00 02 00 .......
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x82 (Direction=IN EndpointID=2)
bmAttributes : 0x01 (TransferType=Isochronous SyncType=None EndpointType=Data)
wMaxPacketSize : 0x0400
Bits 15..13 : 0x00 (reserved, must be zero)
Bits 12..11 : 0x00 (0 additional transactions per microframe -> allows 1..1024 bytes per packet)
Bits 10..0 : 0x400 (1024 bytes per packet)
bInterval : 0x04 (8 microframes -> 1 ms)
Data (HexDump) : 07 05 82 01 00 04 04 .......
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x02 (Direction=OUT EndpointID=2)
bmAttributes : 0x01 (TransferType=Isochronous SyncType=None EndpointType=Data)
wMaxPacketSize : 0x0400
Bits 15..13 : 0x00 (reserved, must be zero)
Bits 12..11 : 0x00 (0 additional transactions per microframe -> allows 1..1024 bytes per packet)
Bits 10..0 : 0x400 (1024 bytes per packet)
bInterval : 0x04 (8 microframes -> 1 ms)
Data (HexDump) : 07 05 02 01 00 04 04 .......
--------- Device Qualifier Descriptor (for Full-Speed) --------
bLength : 0x0A (10 bytes)
bDescriptorType : 0x06 (Device_qualifier Descriptor)
bcdUSB : 0x200 (USB Version 2.0)bDeviceClass : 0x00 (defined by the interface descriptors)
bDeviceSubClass : 0x00
bDeviceProtocol : 0x00
bMaxPacketSize0 : 0x40 (64 Bytes)
bNumConfigurations : 0x01 (1 other-speed configuration)
bReserved : 0x00
Data (HexDump) : 0A 06 00 02 00 00 00 40 01 00 .......@..
------------ Other Speed Configuration Descriptor -------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x07 (Other_speed_configuration Descriptor)
wTotalLength : 0x0045 (69 bytes)
bNumInterfaces : 0x01 (1 Interface)
bConfigurationValue : 0x01 (Configuration 1)
iConfiguration : 0x04 (String Descriptor 4)
Language 0x0409 : "Conf 1"
bmAttributes : 0x80
D7: Reserved, set 1 : 0x01
D6: Self Powered : 0x00 (no)
D5: Remote Wakeup : 0x00 (no)
D4..0: Reserved, set 0 : 0x00
MaxPower : 0x3C (120 mA)
Data (HexDump) : 09 07 45 00 01 01 04 80 3C 09 04 00 00 02 FF 00 ..E.....<.......
00 00 07 05 01 02 40 00 00 07 05 81 02 40 00 00 ......@......@..
09 04 00 01 04 FF 00 00 00 07 05 01 02 40 00 00 .............@..
07 05 81 02 40 00 00 07 05 02 01 FF 03 04 07 05 ....@...........
82 01 FF 03 04 .....
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x00 (Interface 0)
bAlternateSetting : 0x00
bNumEndpoints : 0x02 (2 Endpoints)
bInterfaceClass : 0xFF (Vendor Specific)
bInterfaceSubClass : 0x00
bInterfaceProtocol : 0x00
iInterface : 0x00 (No String Descriptor)
Data (HexDump) : 09 04 00 00 02 FF 00 00 00 .........
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x01 (Direction=OUT EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0040 (64 bytes)
bInterval : 0x00 (ignored)
Data (HexDump) : 07 05 01 02 40 00 00 ....@..
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x81 (Direction=IN EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0040 (64 bytes)
bInterval : 0x00 (ignored)
Data (HexDump) : 07 05 81 02 40 00 00 ....@..
---------------- Interface Descriptor -----------------
bLength : 0x09 (9 bytes)
bDescriptorType : 0x04 (Interface Descriptor)
bInterfaceNumber : 0x00 (Interface 0)
bAlternateSetting : 0x01
bNumEndpoints : 0x04 (4 Endpoints)
bInterfaceClass : 0xFF (Vendor Specific)
bInterfaceSubClass : 0x00
bInterfaceProtocol : 0x00
iInterface : 0x00 (No String Descriptor)
Data (HexDump) : 09 04 00 01 04 FF 00 00 00 .........
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x01 (Direction=OUT EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0040 (64 bytes)
bInterval : 0x00 (ignored)
Data (HexDump) : 07 05 01 02 40 00 00 ....@..
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x81 (Direction=IN EndpointID=1)
bmAttributes : 0x02 (TransferType=Bulk)
wMaxPacketSize : 0x0040 (64 bytes)
bInterval : 0x00 (ignored)
Data (HexDump) : 07 05 81 02 40 00 00 ....@..
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x02 (Direction=OUT EndpointID=2)
bmAttributes : 0x01 (TransferType=Isochronous SyncType=None EndpointType=Data)
wMaxPacketSize : 0x03FF (1023 bytes)
bInterval : 0x04 (4 ms)
Data (HexDump) : 07 05 02 01 FF 03 04 .......
----------------- Endpoint Descriptor -----------------
bLength : 0x07 (7 bytes)
bDescriptorType : 0x05 (Endpoint Descriptor)
bEndpointAddress : 0x82 (Direction=IN EndpointID=2)
bmAttributes : 0x01 (TransferType=Isochronous SyncType=None EndpointType=Data)
wMaxPacketSize : 0x03FF (1023 bytes)
bInterval : 0x04 (4 ms)
Data (HexDump) : 07 05 82 01 FF 03 04 .......
-------------------- String Descriptors -------------------
------ String Descriptor 0 ------
bLength : 0x04 (4 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language ID[0] : 0x0409 (English - United States)
Data (HexDump) : 04 03 09 04 ....
------ String Descriptor 1 ------
bLength : 0x14 (20 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "usbzh.com"
Data (HexDump) : 14 03 75 00 73 00 62 00 7A 00 68 00 2E 00 63 00 ..u.s.b.z.h...c.
6F 00 6D 00 o.m.
------ String Descriptor 2 ------
bLength : 0x1A (26 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "USBZH Gadget"
Data (HexDump) : 1A 03 55 00 53 00 42 00 5A 00 48 00 20 00 47 00 ..U.S.B.Z.H. .G.
61 00 64 00 67 00 65 00 74 00 a.d.g.e.t.
------ String Descriptor 3 ------
bLength : 0x16 (22 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "0123456789"
Data (HexDump) : 16 03 30 00 31 00 32 00 33 00 34 00 35 00 36 00 ..0.1.2.3.4.5.6.
37 00 38 00 39 00 7.8.9.
------ String Descriptor 4 ------
bLength : 0x0E (14 bytes)
bDescriptorType : 0x03 (String Descriptor)
Language 0x0409 : "Conf 1"
Data (HexDump) : 0E 03 43 00 6F 00 6E 00 66 00 20 00 31 00 ..C.o.n.f. .1.