Enabling USB Client feature in Torizon

@jeremias.tx enabling RNDIS at boot time using systemd turned out to be easy. I think using systemd to do this on Torizon is the right way to do it, rather than doing it from inside a container. I might come back to this later as I would like my app in the container to be able to make USB changes at run-time based on user inputs, but I’ll leave that for another time.

I used the guide on this page to run a shell script at start-up using systemd:
How to automatically execute shell script at startup boot on systemd Linux - Linux Tutorials - Learn Linux Configuration

I’ll share my script here in case it is useful for other people. I created the file usb-function.service in /etc/systemd/system with this content:

[Unit]
After=network.target

[Service]
ExecStart=/usr/local/bin/usb-function.sh

[Install]
WantedBy=default.target

And I created the file usb-function.sh in /usr/local/bin with this content:

#!/bin/bash

# Create and activate an RNDIS USB gadget
# Sets the USB class and subclass to work with Windows 10 without an INF file
# Sets a static IP address of 169.254.21.151 and a subnet of 255.255.0.0
# This IP address matches RNDIS on Windows CE for backwards compatibility

echo "usb-function.sh running" > /tmp/usb-function.log
date >> /tmp/usb-function.log

cd /sys/kernel/config/usb_gadget/
mkdir g1
cd g1
echo 0x1b67 > idVendor
cho 0x400c > idProduct
mkdir strings/0x409
cat /proc/device-tree/serial-number > strings/0x409/serialnumber
echo "Toradex" > strings/0x409/manufacturer
cat /proc/device-tree/model > strings/0x409/product
mkdir configs/c.1
mkdir configs/c.1/strings/0x409
echo "USB RNDIS" > configs/c.1/strings/0x409/configuration
mkdir functions/rndis.usb0
ln -s functions/rndis.usb0 configs/c.1
echo "EF" > functions/rndis.usb0/class
echo "04" > functions/rndis.usb0/subclass
echo "01" > functions/rndis.usb0/protocol
cd ..
echo "ci_hdrc.1" > g1/UDC
ifconfig usb0 169.254.21.151 netmask 255.255.0.0 up

ifconfig usb0 >> /tmp/usb-function.log

We can consider this question to be closed now as RNDIS is working in the way I need it to.

3 Likes