Skip to content

USB Gadget Configuration

How to configure a Linux device-side USB gadget and validate its functions.

This English edition retains the original diagrams and practical code or command examples.

Reference diagram 1

Reference diagram 2

Reference diagram 3

Commands and examples

#!/bin/sh

# Mount ConfigFS if not already mounted
if [ ! -d /sys/kernel/config/usb_gadget ]; then
    mount -t configfs none /sys/kernel/config
fi


mkdir /sys/kernel/config/usb_gadget/g1 0770

# write USB Device
echo "0x18d1" > /sys/kernel/config/usb_gadget/g1/idVendor
echo "0x0440" > /sys/kernel/config/usb_gadget/g1/idProduct
mkdir /sys/kernel/config/usb_gadget/g1/strings/0x409
echo "Microhard" > /sys/kernel/config/usb_gadget/g1/strings/0x409/manufacturer
echo "product" > /sys/kernel/config/usb_gadget/g1/strings/0x409/product

# gadget configurations
mkdir /sys/kernel/config/usb_gadget/g1/configs/c.1
echo 0xc0 > /sys/kernel/config/usb_gadget/g1/configs/c.1/bmAttributes
echo 500 > /sys/kernel/config/usb_gadget/g1/configs/c.1/MaxPower
mkdir /sys/kernel/config/usb_gadget/g1/configs/c.1/strings/0x409

# create functions
mkdir /sys/kernel/config/usb_gadget/g1/functions/mass_storage.0

# ln function to config
ln -s /sys/kernel/config/usb_gadget/g1/functions/mass_storage.0 /sys/kernel/config/
usb_gadget/g1/configs/c.1


# enable Gadget
echo "$(ls /sys/class/udc)"  > UDC
#!/bin/sh

# Mount ConfigFS if not already mounted
if [ ! -d /sys/kernel/config/usb_gadget ]; then
    mount -t configfs none /sys/kernel/config
fi

# Create a new gadget
mkdir -p /sys/kernel/config/usb_gadget/g1
cd /sys/kernel/config/usb_gadget/g1

# Set vendor and product ID
echo 0x1d6b > idVendor  # Linux Foundation
echo 0x0104 > idProduct  # Multifunction Composite Gadget

# Set device information
mkdir -p strings/0x409
echo "0123456789" > strings/0x409/serialnumber
echo "Hywelstar" > strings/0x409/manufacturer
echo "Hywelstar's Upan" > strings/0x409/product

# Create configuration
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ACM and Mass Storage" > configs/c.1/strings/0x409/configuration
echo 120 > configs/c.1/MaxPower

# Add functions
mkdir -p functions/acm.usb0
mkdir -p functions/mass_storage.0
echo /dev/mmcblk0 > functions/mass_storage.0/lun.0/file

# Link functions to configuration
ln -s functions/acm.usb0 configs/c.1/
ln -s functions/mass_storage.0 configs/c.1/

# Enable UDC (USB Device Controller)
echo "$(ls /sys/class/udc)" > UDC
# 需要块设备( /dev/mmcblk0)设置为一个大容量存储
echo /dev/mmcblk0 > functions/mass_storage.0/lun.0/file

# 关于这个ID 号,根据公司申请的是什么就填写什么
echo 0x1d6b > idVendor  # Linux Foundation
echo 0x0104 > idProduct  # Multifunction Composite Gadget
#!/bin/sh

# Mount ConfigFS if not already mounted
if [ ! -d /sys/kernel/config/usb_gadget ]; then
    mount -t configfs none /sys/kernel/config
fi

# Create a new gadget
mkdir -p /sys/kernel/config/usb_gadget/g1
cd /sys/kernel/config/usb_gadget/g1

# Set vendor and product ID
echo 0x1d6b > idVendor  # Linux Foundation
echo 0x0104 > idProduct  # Multifunction Composite Gadget

# Set device information
mkdir -p strings/0x409
echo "0123456789" > strings/0x409/serialnumber
echo "Hywelstar" > strings/0x409/manufacturer
echo "Hywelstar's Upan" > strings/0x409/product

# Create configuration
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ACM, Mass Storage, and RNDIS" > configs/c.1/strings/0x409/configuration
echo 120 > configs/c.1/MaxPower

# Add functions
mkdir -p functions/acm.usb0
mkdir -p functions/mass_storage.0
mkdir -p functions/rndis.usb0

# Configure Mass Storage
echo /dev/mmcblk0 > functions/mass_storage.0/lun.0/file

# Configure RNDIS
echo "Ethernet Network" > functions/rndis.usb0/host_addr
echo "00:11:22:33:44:55" > functions/rndis.usb0/dev_addr

# Link functions to configuration
ln -s functions/acm.usb0 configs/c.1/
ln -s functions/mass_storage.0 configs/c.1/
ln -s functions/rndis.usb0 configs/c.1/

# Enable UDC (USB Device Controller)
echo "$(ls /sys/class/udc)" > UDC

Practical notes

Validate pin multiplexing, clocks, power sequencing, kernel configuration, and device-tree bindings on the target board before drawing conclusions from a test result.