usb gadget配置记录

linux 配置

Device Drivers > Network device support  > USB Network Adapters
     [*]  Multi-purpose USB Networking Framework
     [*] Simple USB Network Links (CDC Ethernet subset)

Device Drivers > USB support > USB Gadget Support
     [*]   Serial gadget console support 
     <*>   USB Gadget functions configurable through configfs
        [*]     Abstract Control Model (CDC ACM) 
        [*]     Ethernet Control Model (CDC ECM) 
        [*]     Ethernet Control Model (CDC ECM) subset
        [*]     Mass storage
        [*]     HID function

其余默认就行

usb网口

目标板

#mount configfs
> mount -t configfs none /sys/kernel/config

# config usb g_ecm
> cd /sys/kernel/config/usb_gadget
> mkdir g_ecm
> cd g_ecm/
> ls
UDC              bMaxPacketSize0  functions        os_desc
bDeviceClass     bcdDevice        idProduct        strings
bDeviceProtocol  bcdUSB           idVendor
bDeviceSubClass  configs          max_speed
> echo "64" > bMaxPacketSize0
> echo "0x200" > bcdUSB
> echo "0x100" > bcdDevice
> echo "0x03FD" > idVendor
> echo "0x0502" > idProduct
> mkdir functions/ecm.0
[  208.169899] using random self ethernet address
[  208.174733] using random host ethernet address
> ls functions/ecm.0/
dev_addr   host_addr  ifname     qmult

# add configs
> mkdir configs/c1.1
> ls configs/c1.1
MaxPower      bmAttributes  strings
> ln -s functions/ecm.0 configs/c1.1
> ls configs/c1.1/
MaxPower      bmAttributes  ecm.0         strings

# enable usb-eth
> ls /sys/class/udc/
b0016000.usbdev
> echo "b0016000.usbdev" > UDC
[  456.256633] usb0: HOST MAC 0e:f6:64:33:06:fe
[  456.263799] usb0: MAC 42:16:61:a2:25:a2
> ifconfig -a
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

sit0      Link encap:IPv6-in-IPv4
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

usb0      Link encap:Ethernet  HWaddr 42:16:61:A2:25:A2
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)


# config ip address
> ifconfig usb0 192.168.1.37

# plug to usb-host
[  586.181841] IPv6: ADDRCONF(NETDEV_CHANGE): usb0: link becomes ready

> ping 192.168.1.222
PING 192.168.1.222 (192.168.1.222): 56 data bytes
64 bytes from 192.168.1.222: seq=0 ttl=64 time=1.642 ms
64 bytes from 192.168.1.222: seq=1 ttl=64 time=1.758 ms
64 bytes from 192.168.1.222: seq=2 ttl=64 time=1.825 ms
64 bytes from 192.168.1.222: seq=3 ttl=64 time=1.666 ms
64 bytes from 192.168.1.222: seq=4 ttl=64 time=1.659 ms



# close usb-ethernet
# echo "" > UDC

linux主机端配置比较简单,插上USB后

[ 2572.014146] usb 1-1: new high-speed USB device number 4 using ehci-pci
[ 2572.349019] usb 1-1: New USB device found, idVendor=03fd, idProduct=0502, bcdDevice= 1.00
[ 2572.349025] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[ 2572.365879] cdc_ether 1-1:1.0 usb0: register 'cdc_ether' at usb-0000:02:03.0-1, CDC Ethernet Device, 0e:f6:64:33:06:fe
[ 2572.371630] cdc_ether 1-1:1.0 enx0ef6643306fe: renamed from usb0

> ifconfig
enx0ef6643306fe: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 0e:f6:64:33:06:fe  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 105  bytes 8428 (8.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 105  bytes 8428 (8.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

> sudo ifconfig enx0ef6643306fe 192.168.1.222

> ping 192.168.1.37
PING 192.168.1.37 (192.168.1.37) 56(84) bytes of data.
64 bytes from 192.168.1.37: icmp_seq=1 ttl=64 time=1.52 ms
64 bytes from 192.168.1.37: icmp_seq=2 ttl=64 time=1.50 ms
64 bytes from 192.168.1.37: icmp_seq=3 ttl=64 time=1.95 ms
64 bytes from 192.168.1.37: icmp_seq=4 ttl=64 time=1.44 ms
64 bytes from 192.168.1.37: icmp_seq=5 ttl=64 time=0.968 ms
64 bytes from 192.168.1.37: icmp_seq=6 ttl=64 time=1.45 ms
64 bytes from 192.168.1.37: icmp_seq=7 ttl=64 time=0.945 ms
64 bytes from 192.168.1.37: icmp_seq=8 ttl=64 time=1.43 ms
64 bytes from 192.168.1.37: icmp_seq=9 ttl=64 time=1.53 ms

USB串口

以下为创建3个USB虚拟串口的示例

#mount configfs
> mount -t configfs none /sys/kernel/config

# config usb g_acm
> cd /sys/kernel/config/usb_gadget
> mkdir g_serial
> cd g_serial/
> mkdir functions/acm.gs0
> mkdir functions/acm.gs1
> mkdir functions/acm.gs2

# make configs
> mkdir configs/c1.1
> ln -s functions/acm.gs0 configs/c1.1
> ln -s functions/acm.gs1 configs/c1.1
> ln -s functions/acm.gs2 configs/c1.1

# enable
> echo "b0016000.usbdev" > UDC

完成后,插到电脑主机,可以看到新出现的三个串口

USB读卡器

# mount configfs
> mount -t configfs none /sys/kernel/config

# config usb infomation
> cd /sys/kernel/config/usb_gadget
> mkdir g_storage
> cd g_storage
> echo "0xABCD" >idVendor 
> echo "0x1017" >idProduct
> mkdir strings/0x409
> echo "012345678ABCDEF" > strings/0x409/serialnumber 
> echo "Dragon" > strings/0x409/manufacturer 
> echo "DragonMSC" > strings/0x409/product 

# config USB mass-storage function
> mkdir functions/mass_storage.s0
[  200.979374] Mass Storage Function, version: 2009/09/11
[  200.984899] LUN: removable file: (no medium)
> echo /dev/mmcblk0 > functions/mass_storage.s0/lun.0/file

# make configs
mkdir configs/c.1
mkdir configs/c.1/strings/0x409
echo "abc" > configs/c.1/strings/0x409/configuration 
cat configs/c.1/strings/0x409/configuration
> ln -s functions/mass_storage.s0 configs/c.1

# enable
> echo "b0016000.usbdev" > UDC
posted @ 2024-06-16 22:04  坚持梦想的蜗牛  阅读(43)  评论(0编辑  收藏  举报