Gadget Configuration

Gadgets refer to USB devices with a USB device controller embedded. Based on their configuration, gadgets can be connected to PC, mass storage devices, and UAC devices.

Linux has an original gadget framework, and it can be generally configured in the following procedure:
  1. Enable USB Gadget Support following the procedure in Kernel Menu Configuration.
  2. Use the configfs framework to build the gadget function.
    1. Use the following commands to build gadgets and write gadget PID, VID and sequence number.
      mkdir /sys/kernel/config/usb_gadget/g1
      echo "VID" > /sys/kernel/config/usb_gadget/g1/idVendor
      echo "PID" > /sys/kernel/config/usb_gadget/g1/idProduct
      mkdir /sys/kernel/config/usb_gadget/g1/strings/0x409
      echo "manufacturer" > /sys/kernel/config/usb_gadget/g1/strings/0x409/manufacturer
      echo "product" > /sys/kernel/config/usb_gadget/g1/strings/0x409/product
      
    2. Use the following commands to build gadget relevant configuration.
      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
      
    3. Use the following commands to build gadget functions.
      mkdir /sys/kernel/config/usb_gadget/g1/functions/<name>.<instance name>
      The above code block contains the following parameters.
      • name: Name of the function.
      • instance name: Input any string as the instance name.
    4. Use the following commands to build connections between a function and its configuration.
      ln -s /sys/kernel/config/usb_gadget/g1/functions/<name>.<instance name> /sys/kernel/config/
      usb_gadget/g1/configs/c.1
      
    5. Use the following command to enable a gadget.
      echo <udc name> > UDC