This guide describes how to enable USB 2.0 / USB 3.0 ethernet gadget on SP7350 platforms. The ethernet gadget includes CDC NCM, CDC ECM, CDC ECM subset, RNDIS and EEM.

Table of Contents

Kernel Setup

Enter the kernel configuration by running the following command in the top directory of project.

# make kconfig

  1. 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“.

image-20240708-071800.png

  1. Select submenus for USB OTG Transceive driver : Device Drivers > USB support > USB Physical Layer drivers. Select <*> for “Sunplus USB OTG Transceive driver“.

image-20240703-101840.png

  1. 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“.

image-20240703-083817.png

  1. Select submenus for USB Gadget configfs : Device Drivers > USB support > USB Gadget Support. Select <M> for “USB Gadget functions configurable through configfs“ and [*] for “Network Control Model (CDC NCM)“, “Ethernet Control Model (ECM)“, “Ethernet Control Model (ECM) subset“, “RNDIS“ and “Ethernet Emulation Model (EEM)“.

image-20240812-070641.png

  1. Execute the make command to build the Linux image.

ConfigFS

Configure the ethernet gadget through configfs saved as a file named setup_eth. It configures both USB 2.0 and USB 3.0 as ncm ethernet gadgets by default.

#!/bin/sh

# NCM, ECM, ECM_SUBSET, RNDIS, EEM or OFF
USB2=NCM
USB3=NCM

# IP address configuration
if [ $USB2 != OFF ]
then
        USB2_DEV_IP=192.168.10.20
fi

if [ $USB3 != OFF ]
then
        USB3_DEV_IP=192.168.20.20
fi

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_ETHER=$FUNCTION_PATH/u_ether.ko

U_NCM=$FUNCTION_PATH/usb_f_ncm.ko
U_ECM=$FUNCTION_PATH/usb_f_ecm.ko
U_ECM_SUBSET=$FUNCTION_PATH/usb_f_ecm_subset.ko
U_RNDIS=$FUNCTION_PATH/usb_f_rndis.ko
U_EEM=$FUNCTION_PATH/usb_f_eem.ko

insmod $COMPOSITE
insmod $U_ETHER

if [ $USB2 = NCM ] || [ $USB3 = NCM ]
then
        insmod $U_NCM
elif [ $USB2 = ECM ] || [ $USB3 = ECM ]
then
        insmod $U_ECM
elif [ $USB2 = ECM_SUBSET ] || [ $USB3 = ECM_SUBSET ]
then
        insmod $U_ECM_SUBSET
elif [ $USB2 = RNDIS ] || [ $USB3 = RNDIS ]
then
        insmod $U_RNDIS
elif [ $USB2 = EEM ] || [ $USB3 = EEM ]
then
        insmod $U_EEM
fi

# disable USB2 debug message
if [ $USB2 != OFF ]
then
        echo 0 > /sys/module/sunplus_udc/parameters/dmsg
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

if [ $USB2 != OFF ]
then
        UDC_NAME="f8102800.usb"

        # create gadget folder
        cd /sys/kernel/config/usb_gadget
        mkdir g1
        cd g1

        echo 64 > bMaxPacketSize0
        echo 0x200 > bcdUSB
        echo 0x100 > bcdDevice

        echo 0x0525 > idVendor
        echo 0xa4a1 > idProduct

        if [ $USB2 = NCM ]
        then
                echo 0x02 > bDeviceClass
                echo 0x0d > bDeviceSubClass
                echo 0x0 > bDeviceProtocol
        elif [ $USB2 = ECM ]
        then
                echo 0x02 > bDeviceClass
                echo 0x06 > bDeviceSubClass
                echo 0x0 > bDeviceProtocol
        elif [ $USB2 = ECM_SUBSET ]
        then
                echo 0x02 > bDeviceClass
                echo 0x06 > bDeviceSubClass
                echo 0x0 > bDeviceProtocol
        elif [ $USB2 = RNDIS ]
        then
                echo 0xEF > bDeviceClass
                echo 0x04 > bDeviceSubClass
                echo 0x01 > bDeviceProtocol
        elif [ $USB2 = EEM ]
        then
                echo 0x2 > bDeviceClass
                echo 0x0c > bDeviceSubClass
                echo 0x07 > bDeviceProtocol
        fi

        mkdir -p configs/c1.1
        mkdir -p configs/c1.1/strings/0x409
        echo "ethe" > configs/c1.1/strings/0x409/configuration

        mkdir strings/0x409
        echo "1234" > strings/0x409/serialnumber
        echo "Sunplus" > strings/0x409/manufacturer

        if [ $USB2 = NCM ]
        then
                echo "SP7350 USB2.0 NCM Ethernet Gadget" > strings/0x409/product

                mkdir functions/ncm.usb0
                ln -s functions/ncm.usb0 configs/c1.1
        elif [ $USB2 = ECM ]
        then
                echo "SP7350 USB2.0 ECM Ethernet Gadget" > strings/0x409/product

                mkdir functions/ecm.0
                ln -s functions/ecm.0 configs/c1.1
        elif [ $USB2 = ECM_SUBSET ]
        then
                echo "SP7350 USB2.0 ECM_SUBSET Ethernet Gadget" > strings/0x409/product

                mkdir functions/ecm.0
                ln -s functions/ecm.0 configs/c1.1
        elif [ $USB2 = RNDIS ]
        then
                echo "SP7350 USB2.0 RNDIS Ethernet Gadget" > strings/0x409/product

                mkdir functions/rndis.rn0
                ln -s functions/rndis.rn0 configs/c1.1
        elif [ $USB2 = EEM ]
        then
                echo "SP7350 USB2.0 EEM Ethernet Gadget" > strings/0x409/product

                mkdir functions/eem.0
                ln -s functions/eem.0 configs/c1.1
        fi

        # bind UDC
        echo $UDC_NAME > UDC

        # set ip
        ifconfig lo up
        ifconfig usb0 ${USB2_DEV_IP} netmask 255.255.255.0 up
fi

if [ $USB3 != OFF ]
then
        UDC_NAME="f80a1000.dwc3"
        DEV_IP=192.168.20.20

        # create gadget folder
        cd /sys/kernel/config/usb_gadget

        if [ $USB2 != OFF ]
        then
                mkdir g2
                cd g2
        else
                mkdir g1
                cd g1
        fi

        echo 64 > bMaxPacketSize0
        echo 0x300 > bcdUSB
        echo 0x100 > bcdDevice

        echo 0x0525 > idVendor
        echo 0xa4a1 > idProduct

        if [ $USB2 = NCM ]
        then
                echo 0x02 > bDeviceClass
                echo 0x0d > bDeviceSubClass
                echo 0x0 > bDeviceProtocol
        elif [ $USB2 = ECM ]
        then
                echo 0x02 > bDeviceClass
                echo 0x06 > bDeviceSubClass
                echo 0x0 > bDeviceProtocol
        elif [ $USB2 = ECM_SUBSET ]
        then
                echo 0x02 > bDeviceClass
                echo 0x06 > bDeviceSubClass
                echo 0x0 > bDeviceProtocol
        elif [ $USB2 = RNDIS ]
        then
                echo 0xEF > bDeviceClass
                echo 0x04 > bDeviceSubClass
                echo 0x01 > bDeviceProtocol
        elif [ $USB2 = EEM ]
        then
                echo 0x02 > bDeviceClass
                echo 0x0c > bDeviceSubClass
                echo 0x07 > bDeviceProtocol
        fi

        mkdir -p configs/c1.1
        mkdir -p configs/c1.1/strings/0x409
        echo "ethe" > configs/c1.1/strings/0x409/configuration

        mkdir strings/0x409
        echo "5678" > strings/0x409/serialnumber
        echo "Sunplus" > strings/0x409/manufacturer

        if [ $USB3 = NCM ]
        then
                echo "SP7350 USB3.0 NCM Ethernet Gadget" > strings/0x409/product

                mkdir functions/ncm.usb0
                ln -s functions/ncm.usb0 configs/c1.1
        elif [ $USB3 = ECM ]
        then
                echo "SP7350 USB3.0 ECM Ethernet Gadget" > strings/0x409/product

                mkdir functions/ecm.0
                ln -s functions/ecm.0 configs/c1.1
        elif [ $USB3 = ECM_SUBSET ]
        then
                echo "SP7350 USB3.0 ECM_SUBSET Ethernet Gadget" > strings/0x409/product

                mkdir functions/ecm.0
                ln -s functions/ecm.0 configs/c1.1
        elif [ $USB3 = RNDIS ]
        then
                echo "SP7350 USB3.0 RNDIS Ethernet Gadget" > strings/0x409/product

                mkdir functions/rndis.rn0
                ln -s functions/rndis.rn0 configs/c1.1
        elif [ $USB3 = EEM ]
        then
                echo "SP7350 USB3.0 EEM Ethernet Gadget" > strings/0x409/product

                mkdir functions/eem.0
                ln -s functions/eem.0 configs/c1.1
        fi

        # bind UDC
        echo $UDC_NAME > UDC

        # set ip
        if [ $USB2 != OFF ]
        then
                ifconfig usb1 ${USB3_DEV_IP} netmask 255.255.255.0 up
        else
                ifconfig lo up
                ifconfig usb0 ${USB3_DEV_IP} netmask 255.255.255.0 up
        fi
fi

Implementation

After updating the image to the SP7350 platform and booting the system, use sh setup_eth command to configure USB 2.0 and USB 3.0 as ncm (default) ethernet gadgets and connect them to the windows PC through USB cables. “SP7350 USB2.0 NCM Ethernet Gadget“ and “SP7350 USB3.0 NCM Ethernet Gadget“ will show up in “Other devices” of Device Manager.

image-20240812-084158.pngimage-20240709-024017.pngimage-20240801-030751.png

According to Microsoft official website, the ncm driver needs to be installed manually if the OS version is neither Windows 11 nor Windows Server 2022.

https://learn.microsoft.com/en-us/windows-hardware/drivers/usbcon/supported-usb-class

image-20240709-022647.png

After installing the ncm driver for “SP7350 USB2.0 NCM Ethernet Gadget“ and “SP7350 USB3.0 NCM Ethernet Gadget“, “UsbNcm Host Device“ and “UsbNcm Host Device #2“ will show up in “Network adapters“.

image-20240801-030852.png

Clear ConfigFS Configuration

The following script can be used to clear configfs configuration of the gadget.

#!/bin/bash

addr1="g1"
addr2="g2"
arr=("addr1" "addr2")

f1="ncm.usb0"
f2="ecm.0"
f3="rndis.rn0"
f4="eem.0"
func=("f1" "f2" "f3" "f4")

m1="usb_f_ncm"
m2="usb_f_ecm"
m3="usb_f_ecm_subset"
m4="usb_f_rndis"
m5="usb_f_eem"
module=("m1" "m2" "m3" "m4" "m5")

n0="usb0"
n1="usb1"
nic=("n0" "n1")

GADGET=/sys/kernel/config/usb_gadget
CONFIG=configs/c1.1

for i in "${nic[@]}"; do
    if [ -d "/sys/class/net/${!i}" ]
    then
        ifconfig ${!i} down
    fi
done

for i in "${arr[@]}"; do
    if [ -d $GADGET/${!i} ]
    then
        if [ -d $GADGET/${!i}/$CONFIG ]
        then
            for j in "${func[@]}"; do
                if [ -d $GADGET/${!i}/$CONFIG/${!j} ]
                then
                    rm $GADGET/${!i}/$CONFIG/${!j}
                fi
            done

            if [ -d $GADGET/${!i}/$CONFIG/strings/0x409 ]
            then
                rmdir $GADGET/${!i}/$CONFIG/strings/0x409
            fi

            rmdir $GADGET/${!i}/$CONFIG
        fi

        for j in "${func[@]}"; do
            if [ -d $GADGET/${!i}/functions/${!j} ]
            then
                rmdir $GADGET/${!i}/functions/${!j}
            fi
        done

        if [ -d $GADGET/${!i}/strings/0x409 ]
        then
            rmdir $GADGET/${!i}/strings/0x409
        fi

        rmdir $GADGET/${!i}
    fi
done

for i in "${module[@]}"; do
    if grep -q "^"${!i}"" /proc/modules
    then
        rmmod ${!i}
    fi
done

if grep -q "^"u_ether"" /proc/modules
then
    rmmod u_ether
fi

if grep -q "^"libcomposite"" /proc/modules
then
    rmmod libcomposite
fi