#!/usr/bin/env bash # mount configfs mount -t configfs none /sys/kernel/config # load libcomposite module modprobe libcomposite # create a gadget mkdir /sys/kernel/config/usb_gadget/g1 # cd to its configfs node cd /sys/kernel/config/usb_gadget/g1 # configure it (vid/pid can be anything if USB Class is used for driver compat) echo 0xabcd > idVendor echo 0x1234 > idProduct # configure its serial/mfg/product mkdir strings/0x409 echo myserial > strings/0x409/serialnumber echo mymfg > strings/0x409/manufacturer echo myproduct > strings/0x409/product # create a config mkdir configs/c.1 # configure it with attributes if needed echo 120 > configs/c.1/MaxPower # ensure function is loaded modprobe usb_f_ecm # create the function (name must match a usb_f_ module such as 'ecm') mkdir functions/ecm.0 # associate function with config ln -s functions/ecm.0 configs/c.1 # enable gadget by binding it to a UDC from /sys/class/udc echo 0000:01:00.0 > UDC # to unbind it: echo "" UDC; sleep 1; rm -rf /sys/kernel/config/usb_gadget/g1