mirror of
https://github.com/armbian/build
synced 2025-09-24 19:47:06 +07:00
usbgaget-network: toggle to NCM and run inside initramfs
NCM has advantages over RNDIS and postmarketOS has toggled to it 2 years ago. And I have tested it can work with common linux and window10. I also made some changes to let this script run inside intramfs. And the systemd service can run well after rootfs is loaded.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
deviceinfo_name="Armbian USB Gadget Network"
|
||||
deviceinfo_name="USB Gadget Network"
|
||||
deviceinfo_manufacturer="Armbian"
|
||||
#deviceinfo_usb_idVendor=
|
||||
#deviceinfo_usb_idProduct=
|
||||
@@ -15,17 +15,24 @@ setup_usb_network_configfs() {
|
||||
return
|
||||
fi
|
||||
|
||||
if [ -e "$CONFIGFS/g1" ]; then
|
||||
echo " $CONFIGFS/g1 already exists, skipping configfs usb gadget"
|
||||
return
|
||||
fi
|
||||
|
||||
# Default values for USB-related deviceinfo variables
|
||||
usb_idVendor="${deviceinfo_usb_idVendor:-0x18D1}" # default: Google Inc.
|
||||
usb_idProduct="${deviceinfo_usb_idProduct:-0xD001}" # default: Nexus 4 (fastboot)
|
||||
usb_serialnumber="${deviceinfo_usb_serialnumber:-postmarketOS}"
|
||||
usb_network_function="rndis.0"
|
||||
usb_idVendor="${deviceinfo_usb_idVendor:-0x1D6B}" # Linux Foundation
|
||||
usb_idProduct="${deviceinfo_usb_idProduct:-0x0103}" # NCM (Ethernet) Gadget
|
||||
usb_serialnumber="${deviceinfo_usb_serialnumber:-0123456789}"
|
||||
usb_network_function="ncm.usb0"
|
||||
|
||||
echo " Setting up an USB gadget through configfs"
|
||||
# Create an usb gadet configuration
|
||||
mkdir $CONFIGFS/g1 || echo " Couldn't create $CONFIGFS/g1"
|
||||
echo "$usb_idVendor" > "$CONFIGFS/g1/idVendor"
|
||||
echo "$usb_idProduct" > "$CONFIGFS/g1/idProduct"
|
||||
echo 0x0100 > "$CONFIGFS/g1/bcdDevice"
|
||||
echo 0x0200 > "$CONFIGFS/g1/bcdUSB"
|
||||
|
||||
# Create english (0x409) strings
|
||||
mkdir $CONFIGFS/g1/strings/0x409 || echo " Couldn't create $CONFIGFS/g1/strings/0x409"
|
||||
@@ -43,28 +50,16 @@ setup_usb_network_configfs() {
|
||||
# Create configuration instance for the gadget
|
||||
mkdir $CONFIGFS/g1/configs/c.1 ||
|
||||
echo " Couldn't create $CONFIGFS/g1/configs/c.1"
|
||||
echo 250 > $CONFIGFS/g1/configs/c.1/MaxPower
|
||||
mkdir $CONFIGFS/g1/configs/c.1/strings/0x409 ||
|
||||
echo " Couldn't create $CONFIGFS/g1/configs/c.1/strings/0x409"
|
||||
echo "USB network" > $CONFIGFS/g1/configs/c.1/strings/0x409/configuration ||
|
||||
echo "NCM Configuration" > $CONFIGFS/g1/configs/c.1/strings/0x409/configuration ||
|
||||
echo " Couldn't write configration name"
|
||||
|
||||
# Link the network instance to the configuration
|
||||
ln -s $CONFIGFS/g1/functions/"$usb_network_function" $CONFIGFS/g1/configs/c.1 ||
|
||||
echo " Couldn't symlink $usb_network_function"
|
||||
|
||||
echo 0xEF > $CONFIGFS/g1/functions/"$usb_network_function"/class ||
|
||||
echo " Couldn't write class"
|
||||
echo 0x04 > $CONFIGFS/g1/functions/"$usb_network_function"/subclass ||
|
||||
echo " Couldn't write subclass"
|
||||
echo 0x01 > $CONFIGFS/g1/functions/"$usb_network_function"/protocol ||
|
||||
echo " Couldn't write protocol"
|
||||
echo 0xEF > $CONFIGFS/g1/bDeviceClass ||
|
||||
echo " Couldn't write g1 class"
|
||||
echo 0x04 > $CONFIGFS/g1/bDeviceSubClass ||
|
||||
echo " Couldn't write g1 subclass"
|
||||
echo 0x01 > $CONFIGFS/g1/bDeviceProtocol ||
|
||||
echo " Couldn't write g1 protocol"
|
||||
|
||||
# Check if there's an USB Device Controller
|
||||
if [ -z "$(ls /sys/class/udc)" ]; then
|
||||
echo " No USB Device Controller available"
|
||||
@@ -80,13 +75,15 @@ setup_usb_network_configfs() {
|
||||
set_usbgadget_ipaddress() {
|
||||
local host_ip="${unudhcpd_host_ip:-172.16.42.1}"
|
||||
local client_ip="${unudhcpd_client_ip:-172.16.42.2}"
|
||||
unudhcpd_pid=$(pgrep unudhcpd)
|
||||
if [ "x$unudhcpd_pid" != "x" ]; then
|
||||
echo "unudhcpd process already exists, skip setting usb gadget ip, unudhcpd_pid is $unudhcpd_pid"
|
||||
return
|
||||
fi
|
||||
echo "Starting dnsmasq with server ip $host_ip, client ip: $client_ip"
|
||||
# Get usb interface
|
||||
INTERFACE=""
|
||||
ip a add "${host_ip}/255.255.0.0" dev rndis0 2> /dev/null && ip link set rndis0 up && INTERFACE=rndis0
|
||||
if [ -z $INTERFACE ]; then
|
||||
ip a add "${host_ip}/255.255.0.0" dev usb0 2> /dev/null && ip link set usb0 up && INTERFACE=usb0
|
||||
fi
|
||||
ip a add "${host_ip}/255.255.0.0" dev usb0 2> /dev/null && ip link set usb0 up && INTERFACE=usb0
|
||||
if [ -z $INTERFACE ]; then
|
||||
ip a add "${host_ip}/255.255.0.0" dev eth0 2> /dev/null && eth0 && INTERFACE=eth0
|
||||
fi
|
||||
@@ -101,7 +98,8 @@ set_usbgadget_ipaddress() {
|
||||
echo " Using interface $INTERFACE"
|
||||
echo " Starting the DHCP daemon"
|
||||
ip a show $INTERFACE > /var/log/unudhcpd.log
|
||||
nohup /usr/bin/unudhcpd -i "$INTERFACE" -s "$host_ip" -c "$client_ip" 2>&1 >> /var/log/unudhcpd.log &
|
||||
nohup /usr/bin/unudhcpd -i "$INTERFACE" -s "$host_ip" -c "$client_ip" >> /var/log/unudhcpd.log 2>&1 &
|
||||
return
|
||||
}
|
||||
setup_usb_network_configfs
|
||||
set_usbgadget_ipaddress
|
||||
|
||||
Reference in New Issue
Block a user