Skip to content

Commit ba2b738

Browse files
committed
config: boards: pocketbeagle2: Enable better USB Gadget
- Use configfs API to setup Multifunction Composite Gadget - This is already done on all BeagleBoard debian images. - Allows ssh and serial over USB without any user setup. Signed-off-by: Ayush Singh <ayush@beagleboard.org>
1 parent e696c2e commit ba2b738

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

config/boards/pocketbeagle2.conf

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,55 @@ TIBOOT3_FILE="tiboot3-am62x-hs-fs-evm.bin"
1010
DEFAULT_CONSOLE="serial"
1111
KERNEL_TARGET="edge"
1212
KERNEL_TEST_TARGET="edge"
13-
SERIALCON="ttyS2,ttyGS0"
13+
SERIALCON="ttyS2"
1414
ATF_BOARD="lite"
1515
SRC_EXTLINUX="yes"
16-
SRC_CMDLINE="root=/dev/mmcblk1p2 rootwait console=ttyS2,115200n8 console=ttyGS0,115200n8 modules-load=dwc2,g_cdc"
16+
SRC_CMDLINE="root=/dev/mmcblk1p2 rootwait console=ttyS2,115200n8"
1717
BOOT_FDT_FILE="ti/k3-am6232-pocketbeagle2.dtb"
1818
OPTEE_PLATFORM="k3-am62x"
1919

20+
# USB Gadget
21+
function post_family_tweaks_bsp__pocketbeagle2_firmware() {
22+
display_alert "Setup USB Gadget for ${BOARD}" "${RELEASE}" "warn"
23+
24+
# USB Gadget Network service
25+
mkdir -p $destination/usr/local/bin/
26+
mkdir -p $destination/usr/lib/systemd/system/
27+
mkdir -p $destination/etc/initramfs-tools/scripts/init-bottom/
28+
# The service expects the name setup-usbgadget-network.sh. Depending on the board vendor, the
29+
# usbgadget configuration they would like can be wildly different. However, the same service
30+
# will work fine for almost all cases. So better to rename file rather than modifying the service.
31+
install -Dm755 $SRC/packages/bsp/usb-gadget-network/setup-usbgadget-network-multi.sh $destination/usr/local/bin/setup-usbgadget-network.sh ||
32+
exit_with_error "Failed to install USB gadget setup script"
33+
install -Dm755 $SRC/packages/bsp/usb-gadget-network/remove-usbgadget-network.sh $destination/usr/local/bin/ ||
34+
exit_with_error "Failed to install USB gadget remove script"
35+
install -Dm744 $SRC/packages/bsp/usb-gadget-network/usbgadget-rndis.service $destination/usr/lib/systemd/system/ ||
36+
exit_with_error "Failed to install USB gadget service"
37+
install -Dm755 $SRC/packages/bsp/usb-gadget-network/usb-gadget-initramfs-hook $destination/etc/initramfs-tools/hooks/usb-gadget ||
38+
exit_with_error "Failed to install USB gadget initramfs hook"
39+
install -Dm755 $SRC/packages/bsp/usb-gadget-network/usb-gadget-initramfs-premount $destination/etc/initramfs-tools/scripts/init-premount/usb-gadget ||
40+
exit_with_error "Failed to install USB gadget initramfs premount script"
41+
install -Dm755 $SRC/packages/bsp/usb-gadget-network/dropbear $destination/etc/initramfs-tools/scripts/init-premount/ ||
42+
exit_with_error "Failed to install USB gadget initramfs premount dropbear"
43+
install -Dm755 $SRC/packages/bsp/usb-gadget-network/kill-dropbear $destination/etc/initramfs-tools/scripts/init-bottom/ ||
44+
exit_with_error "Failed to install USB gadget initramfs premount kill dropbear"
45+
}
46+
47+
function post_family_tweaks__pocketbeagle2_enable_services() {
48+
display_alert "Enable Services for ${BOARD}" "${RELEASE}" "warn"
49+
50+
# We need unudhcpd from armbian repo, so enable it
51+
mv "${SDCARD}"/etc/apt/sources.list.d/armbian.sources.disabled "${SDCARD}"/etc/apt/sources.list.d/armbian.sources
52+
do_with_retries 3 chroot_sdcard_apt_get_install unudhcpd
53+
# disable armbian repo back
54+
mv "${SDCARD}"/etc/apt/sources.list.d/armbian.sources "${SDCARD}"/etc/apt/sources.list.d/armbian.sources.disabled
55+
56+
chroot_sdcard systemctl enable usbgadget-rndis.service
57+
# We want custom IPs. systemctl edit does not seem to work in chroot.
58+
mkdir -p "${SDCARD}"/etc/systemd/system/usbgadget-rndis.service.d
59+
echo -e "[Service]\nEnvironment=\"unudhcpd_host_ip=192.168.7.2\"\nEnvironment=\"unudhcpd_client_ip=192.168.7.3\"" > "${SDCARD}"/etc/systemd/system/usbgadget-rndis.service.d/override.conf
60+
}
61+
2062
function current_beagle_kernel_uboot() {
2163
declare -g KERNELSOURCE="https://github.com/beagleboard/linux" # BeagleBoard kernel
2264
declare -g KERNEL_MAJOR_MINOR="6.12"
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
deviceinfo_name="USB Gadget Network"
5+
deviceinfo_manufacturer="Armbian"
6+
#deviceinfo_usb_idVendor=
7+
#deviceinfo_usb_idProduct=
8+
#deviceinfo_usb_serialnumber=
9+
10+
exit_with_error() {
11+
echo "$1"
12+
exit 1
13+
}
14+
15+
setup_usb_network_configfs() {
16+
# See: https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt
17+
CONFIGFS=/sys/kernel/config/usb_gadget
18+
19+
if ! [ -e "$CONFIGFS" ]; then
20+
exit_with_error "$CONFIGFS does not exist, skipping configfs usb gadget"
21+
fi
22+
23+
if [ -e "$CONFIGFS/g1" ]; then
24+
echo "$CONFIGFS/g1 already exists, skipping configfs usb gadget"
25+
return
26+
fi
27+
28+
# Default values for USB-related deviceinfo variables
29+
usb_idVendor="${deviceinfo_usb_idVendor:-0x1D6B}" # Linux Foundation
30+
usb_idProduct="${deviceinfo_usb_idProduct:-0x0104}" # Multifunction Composite Gadget
31+
usb_serialnumber="${deviceinfo_usb_serialnumber:-0123456789}"
32+
usb_network_function="ncm.usb0"
33+
usb_serial_function="acm.usb0"
34+
35+
echo "Setting up an USB gadget through configfs"
36+
# Create an usb gadet configuration
37+
mkdir $CONFIGFS/g1 || exit_with_error "Couldn't create $CONFIGFS/g1"
38+
echo "$usb_idVendor" > "$CONFIGFS/g1/idVendor"
39+
echo "$usb_idProduct" > "$CONFIGFS/g1/idProduct"
40+
echo 0x0404 > "$CONFIGFS/g1/bcdDevice"
41+
echo 0x0200 > "$CONFIGFS/g1/bcdUSB"
42+
43+
# Create english (0x409) strings
44+
mkdir $CONFIGFS/g1/strings/0x409 || echo "Couldn't create $CONFIGFS/g1/strings/0x409"
45+
46+
# shellcheck disable=SC2154
47+
echo "$deviceinfo_manufacturer" > "$CONFIGFS/g1/strings/0x409/manufacturer"
48+
echo "$usb_serialnumber" > "$CONFIGFS/g1/strings/0x409/serialnumber"
49+
# shellcheck disable=SC2154
50+
echo "$deviceinfo_name" > "$CONFIGFS/g1/strings/0x409/product"
51+
52+
# Create network function.
53+
mkdir $CONFIGFS/g1/functions/"$usb_network_function" ||
54+
echo "Couldn't create $CONFIGFS/g1/functions/$usb_network_function"
55+
56+
# Create configuration instance for the gadget
57+
mkdir $CONFIGFS/g1/configs/c.1 ||
58+
echo "Couldn't create $CONFIGFS/g1/configs/c.1"
59+
echo 250 > $CONFIGFS/g1/configs/c.1/MaxPower
60+
mkdir $CONFIGFS/g1/configs/c.1/strings/0x409 ||
61+
echo "Couldn't create $CONFIGFS/g1/configs/c.1/strings/0x409"
62+
echo "NCM Configuration" > $CONFIGFS/g1/configs/c.1/strings/0x409/configuration ||
63+
echo "Couldn't write configration name"
64+
65+
# Link the network instance to the configuration
66+
ln -s $CONFIGFS/g1/functions/"$usb_network_function" $CONFIGFS/g1/configs/c.1 ||
67+
echo "Couldn't symlink $usb_network_function"
68+
69+
mkdir -p $CONFIGFS/g1/functions/"$usb_serial_function" ||
70+
echo "Couldn't create $CONFIGFS/g1/functions/$usb_serial_function"
71+
ln -s $CONFIGFS/g1/functions/"$usb_serial_function" $CONFIGFS/g1/configs/c.1 ||
72+
echo "Couldn't symlink $usb_serial_function"
73+
74+
# Check if there's an USB Device Controller
75+
if [ ! -d /sys/class/udc ] || [ -z "$(ls /sys/class/udc 2>/dev/null)" ]; then
76+
exit_with_error "No USB Device Controller available"
77+
fi
78+
79+
# Link the gadget instance to an USB Device Controller. This activates the gadget.
80+
# See also: https://github.com/postmarketOS/pmbootstrap/issues/338
81+
# shellcheck disable=SC2005
82+
ls /sys/class/udc | head -1 > $CONFIGFS/g1/UDC || exit_with_error "Couldn't write UDC"
83+
}
84+
85+
set_usbgadget_ipaddress() {
86+
local host_ip="${unudhcpd_host_ip:-172.16.42.1}"
87+
local client_ip="${unudhcpd_client_ip:-172.16.42.2}"
88+
echo "Starting dnsmasq with server ip $host_ip, client ip: $client_ip"
89+
# Get usb interface
90+
INTERFACE=""
91+
ip a add "${host_ip}/16" dev usb0 2> /dev/null && ip link set usb0 up && INTERFACE=usb0
92+
if [ -z $INTERFACE ]; then
93+
echo "Interfaces:"
94+
ip link
95+
exit_with_error "Could not find an interface to run a dhcp server on"
96+
fi
97+
98+
echo "Using interface $INTERFACE"
99+
echo "Starting the DHCP daemon"
100+
ip a show $INTERFACE > /var/log/unudhcpd.log
101+
nohup /usr/bin/unudhcpd -i "$INTERFACE" -s "$host_ip" -c "$client_ip" >> /var/log/unudhcpd.log 2>&1 &
102+
return
103+
}
104+
setup_usb_network_configfs
105+
set_usbgadget_ipaddress

0 commit comments

Comments
 (0)