USB Configuration

Configure Kernel Menu for USB

  1. Under the root directory of freelight-u-sdk, run the following command to enter the kernel menu configuration GUI.
    make linux-menuconfig
  2. Enter the Device Drivers menu.

  3. Enter the USB Support menu.

  4. In the USB Support menu, select the support for host-side USB, PCI based USB host interface and xHCI HCD USB 3.0 support options.

  5. In the USB Support menu, select all the options that start with Cadence USB.

  6. To enable the function of USB attached disks, in the USB Support menu, select the USB Mass Storage support and the USB Attached SCSI options.

  7. To enable the function of USB device, select the USB Gadget Support option and enter the menu.

  8. In the USB Gadget Support menu, select your target function.

    For example, to enable the configfs, functionfs, and mass storage, select the

    USB gadget functions configurable through configfs, Mass storage and Function filesystem (FunctionFS) options respectively.
  9. Save your change and exit the kernel configuration dialog.

Board-level Configuration: Configure USB Pins

The following two code snippets work together to set up the GPIO pin that turns USB power on when the board acts as a USB host.

Code snippet 1: Sets up GPIO25 as the USB VBUS power enable pin

usb_pins: usb-pins {
        drive-vbus-pin {
                sf,pins = <PAD_GPIO25>;          // Use physical pin GPIO25
                sf,pinmux = <PAD_GPIO25_FUNC_SEL 0>;  // Set to USB power-control function
                sf,pin-ioconfig = <IO(GPIO_IE(1))>;   // Input enabled (safe default)
                sf,pin-gpio-dout = <GPO_USB0_DRIVE_VBUS_IO>;  // Connect to USB power enable
                sf,pin-gpio-doen = <OEN_LOW>;     // Set as OUTPUT (to turn power on/off)
        };
};
Code snippet 2: Applies the USB pin setup and sets USB to work in Host mode
&usbdrd30 {
    pinctrl-names = "default";
    pinctrl-0 = <&usb_pins>;   // USE the GPIO25 pin setup above
    dr_mode = "host";          // Set USB to HOST mode
    status = "disabled";       // USB is OFF by default
};

Build Gadgets

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

You can build gadgets using the Linux's original gadget framework.
  1. Enable the USB Gadget Support in the kernel menu. See Configure Kernel Menu for USB.

  2. Use the configfs framework to build the gadget function.
    1. Run 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
      Table 1. Examples
      Action Command
      Configure the gadget as a mass storage device
      mount -t configfs none /sys/kernel/config
      mkdir /sys/kernel/config/usb_gadget/g1
      echo "0x1d6b " > /sys/kernel/config/usb_gadget/g1/idVendor
      echo "0x0001" > /sys/kernel/config/usb_gadget/g1/idProduct
      mkdir /sys/kernel/config/usb_gadget/g1/strings/0x409
      mkdir /sys/kernel/config/usb_gadget/g1/functions/mass_storage.usb0
      echo /dev/a.bin > /sys/kernel/config/usb_gadget/g1/functions/mass_storage.usb0/lun.0/file
      Configure the gadget as an ADB
      mkdir /sys/kernel/config/usb_gadget/g1
      echo "0x1d6b" > /sys/kernel/config/usb_gadget/g1/idVendor
      echo "0x0002" > /sys/kernel/config/usb_gadget/g1/idProduct
      mkdir /sys/kernel/config/usb_gadget/g1/strings/0x409
      echo "20181111" > /sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber
      echo "starfive" > /sys/kernel/config/usb_gadget/g1/strings/0x409/manufacturer
      Configure the gadget as both a mass storage device and an ADB
      mkdir /sys/kernel/config/usb_gadget/g1
      echo "0x1d6b" > /sys/kernel/config/usb_gadget/g1/idVendor
      echo "0x0003" > /sys/kernel/config/usb_gadget/g1/idProduct
      mkdir /sys/kernel/config/usb_gadget/g1/strings/0x409
      echo "20181111" > /sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber
      echo "stafive" > /sys/kernel/config/usb_gadget/g1/strings/0x409/manufacturer
    2. Run 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. Run the following command to build gadget functions.
      mkdir /sys/kernel/config/usb_gadget/g1/functions/<name>.<instance name>
      where,
      • name:Name of the function
      • instance name: Instance name of the function
    4. Run the following commands to build connections between the 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
      Table 2. Examples
      Action Command
      Configure the gadget as a mass storage device
      ln -s /sys/kernel/config/usb_gadget/g1/functions/mass_storage.usb0/ /sys/kernel/config/
      usb_gadget/g1/configs/c.1/
      Configure the gadget as an ADB
      ln -s /sys/kernel/config/usb_gadget/g1/functions/ffs.adb/ /sys/kernel/config/usb_gadget/g1/
      configs/c.1/ffs.adb
      Configure the gadget as both a mass storage device and an ADB
      ln -s /sys/kernel/config/usb_gadget/g1/functions/ffs.adb/ /sys/kernel/config/usb_gadget/g1/
      configs/c.1/ffs.adb
      ln -s /sys/kernel/config/usb_gadget/g1/functions/mass_storage.usb0/ /sys/kernel/config/
      usb_gadget/g1/configs/c.1/mass_storage.usb0
    5. Enable the gadget.
      echo <udc name> > UDC