How to create an USB 2.0 / USB 3.0 ADB Gadget
This document demonstrates how to create an USB 2.0 / USB 3.0 ADB gadget on SP7350 platforms.
Table of Contents
Compiling code for Buildroot root file-system
Please refer to Downloading and Compiling Code to download the source code. Use the following command to set up the compilation environment. In “Select rootfs“, “5“ should be selected for Buildroot root file-system.
# make config
adbd utility for the target
Use the following command to enter Buildroot Configuration and select submenus for the adbd utility for the target : Target Packages > System tools. Select [*] for “android-tools” (it selects “adbd” by default).
# make bconfig
Select submenus for killing the adbd process if needed : Target Packages > System tools. Select [*] for “procps-ng”.
kernel Setup
Enter the kernel configuration by running the following command in the top directory of project.
# make kconfig
Select submenus for USB 2.0 controller : Device Drivers > USB support > USB Gadget Support > USB Peripheral Controller. Select <*> for “Sunplus USB 2.0 Device Controller“.
Select submenus for USB OTG Transceive driver : Device Drivers > USB support > USB Physical Layer drivers. Select <*> for “Sunplus USB OTG Transceive driver“.
Select submenus for USB 3.0 controller : Device Drivers > USB support. Select <*> for “DesignWare USB3 DRD Core Support“ and select “Dual Role mode“ for “Deware USB3 DRD Core Support“.
Select submenus for USB Gadget configfs : Device Drivers > USB support > USB Gadget Support. Select <M> for “USB Gadget functions configurable through configfs“ and [*] for “Function filesystem (FunctionFS)“.
Execute the make command to build the Linux image.
# make
ConfigFS
Configure the adb gadget through configfs saved as a file named setup_adb.
#!/bin/bash
# enable USB2 or USB3
ADB_EN=USB2
KERNEL_PATH=/lib/modules/`uname -r`/kernel
GADGET_PATH=$KERNEL_PATH/drivers/usb/gadget
FUNCTION_PATH=$GADGET_PATH/function
COMPOSITE=$GADGET_PATH/libcomposite.ko
U_FFS=$FUNCTION_PATH/usb_f_fs.ko
insmod $COMPOSITE
insmod $U_FFS
# disable debug message
echo 0 > /sys/module/sunplus_udc/parameters/dmsg
if [ ! -d /dev/pts ]
then
mkdir /dev/pts
mount -t devpts none /dev/pts
fi
if [ ! -d /sys/kernel/config ]
then
mkdir -p /sys/kernel/config
fi
if ! mountpoint -q /sys/kernel/config
then
mount -t configfs none /sys/kernel/config
fi
cd /sys/kernel/config/usb_gadget
mkdir g_adb
cd g_adb
echo "0x1d6b" > idVendor
echo "0x0105" > idProduct
mkdir configs/c.1
mkdir functions/ffs.adb
mkdir strings/0x409
mkdir configs/c.1/strings/0x409
echo "20240807" > strings/0x409/serialnumber
echo "Sunplus" > strings/0x409/manufacturer
echo "FunctionFS gadget (adb)" > strings/0x409/product
echo "adb" > configs/c.1/strings/0x409/configuration
echo 120 > configs/c.1/MaxPower
mkdir -p functions/ffs.adb
ln -s functions/ffs.adb configs/c.1
mkdir -p /dev/usb-ffs/adb
mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb
adbd&
sleep 1
if [ $ADB_EN = USB2 ]
then
echo "f8102800.usb" > UDC
elif [ $ADB_EN = USB3 ]
then
echo "f80a1000.dwc3" > UDC
fi
Implementation
After updating the image to the SP7350 platform and booting the system, use sh setup_adb command to configure USB 2.0 or USB 3.0 as an adb device.
After connecting the adb gadget to a Windows PC through an USB cable, “FunctionFS gadget (adb)”will show up in “Universal Serial Bus devices” of “Device Manager”.
Download the “Minimal ADB and Fastboot” application and install it on your Windows computer. After the installation, open the Windows Command Prompt. After issuing the command of “adb devices”, you will see a device (ADB gadget).
Re-connection
If the ADB gadget re-connects to the Windows pc after disconnection, the following command should be issued after the re-connection.
echo "f8102800.usb" > /sys/kernel/config/usb_gadget/g_adb/UDC
Clear ConfigFS Configuration
The following script can be used to clear configfs configuration of the gadget.
#!/bin/sh
GADGET=/sys/kernel/config/usb_gadget
CONFIG=configs/c.1
if [ -d $GADGET/g_adb ]
then
if [ -d $GADGET/g_adb/$CONFIG ]
then
if [ -d $GADGET/g_adb/$CONFIG/ffs.adb ]
then
rm $GADGET/g_adb/$CONFIG/ffs.adb
fi
if [ -d $GADGET/g_adb/$CONFIG/strings/0x409 ]
then
rmdir $GADGET/g_adb/$CONFIG/strings/0x409
fi
rmdir $GADGET/g_adb/$CONFIG
fi
if [ -d $GADGET/g_adb/functions/ffs.adb ]
then
rmdir $GADGET/g_adb/functions/ffs.adb
fi
if [ -d $GADGET/g_adb/strings/0x409 ]
then
rmdir $GADGET/g_adb/strings/0x409
fi
rmdir $GADGET/g_adb
fi
if pgrep -x "adbd" > /dev/null
then
pkill adbd
fi
if mountpoint -q /dev/usb-ffs/adb
then
umount /dev/usb-ffs/adb
fi
if [ -d /dev/usb-ffs/adb ]
then
rmdir /dev/usb-ffs/adb
fi
if grep -q "^"usb_f_fs"" /proc/modules
then
rmmod usb_f_fs
fi
if grep -q "^"libcomposite"" /proc/modules
then
rmmod libcomposite
fi