Sunday, October 27, 2024

USB notes - 2

Using Linux USB Gadget


Documentation/usb/gadget_configfs.rst details how linux USB gadget is configured through configfs


To get ethernet over USB working on beaglebone-white using NCM (Network Control Model), do something like the following (put it into a script)


#!/bin/ash

modprobe libcomposite
mount -t configfs none /sys/kernel/config/
cd /sys/kernel/config/usb_gadget/
mkdir g
cd g/
echo "0xA55A" > idVendor 
echo "0x0111" > idProduct 
mkdir strings/0x409
echo "0123" > strings/0x409/serialnumber 
echo "hello" > strings/0x409/manufacturer 
echo "ncm" > strings/0x409/product 
mkdir functions/ncm.usb0
mkdir configs/c.1
mkdir configs/c.1/strings/0x409
echo "ncm" > configs/c.1/strings/0x409/configuration 
ln -s functions/ncm.usb0 configs/c.1/
echo "musb-hdrc.0" > UDC


Now, after running the script, configure the ip from the device


ifconfig usb0 169.254.211.10 up


When the ip is up and running, you might see something like


usb0      Link encap:Ethernet  HWaddr BA:E9:68:15:C1:8D  
          inet addr:169.254.211.10  Bcast:169.254.255.255  Mask:255.255.0.0
          inet6 addr: fe80::b8e9:68ff:fe15:c18d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3027 (2.9 KiB)  TX bytes:872 (872.0 B)


from the device.


Ping it from the host


$ ping 169.254.211.10
PING 169.254.211.10 (169.254.211.10) 56(84) bytes of data.
64 bytes from 169.254.211.10: icmp_seq=1 ttl=64 time=2.43 ms
64 bytes from 169.254.211.10: icmp_seq=2 ttl=64 time=2.19 ms


Use the following to remove it


#!/bin/ash

cd /sys/kernel/config/usb_gadget/g
echo "" > UDC
rm configs/c.1/ncm.usb0
rmdir configs/c.1/strings/0x409
rmdir configs/c.1
rmdir functions/ncm.usb0
rmdir strings/0x409
cd ..
rmdir g


When you clean it up, it'll be unreachable


$ ping 169.254.211.10
PING 169.254.211.10 (169.254.211.10) 56(84) bytes of data.
64 bytes from 169.254.211.10: icmp_seq=1 ttl=64 time=2.43 ms
:
64 bytes from 169.254.211.10: icmp_seq=6 ttl=64 time=2.21 ms
From 172.17.0.1 icmp_seq=7 Destination Host Unreachable
From 172.17.0.1 icmp_seq=8 Destination Host Unreachable
:


And, here's the config fragment (usb_configfs.cfg) we used earlier


CONFIG_USB_MUSB_HDRC=m
CONFIG_USB_MUSB_OMAP2PLUS=m
CONFIG_USB_MUSB_DSPS=m
CONFIG_USB_G_NCM=n
CONFIG_USB_MASS_STORAGE=n
CONFIG_USB_CONFIGFS=m
CONFIG_USB_CONFIGFS_ACM=y
CONFIG_USB_CONFIGFS_NCM=y


NOTE: Had to use Link-Local Only option in the IPv4 on Ubuntu 20.04 to get the ip connection working

No comments: