Skip to content

Instantly share code, notes, and snippets.

@geekman
Created January 17, 2017 02:47
Show Gist options
  • Save geekman/5bdb5abdc9ec6ac91d5646de0c0c60c4 to your computer and use it in GitHub Desktop.
Save geekman/5bdb5abdc9ec6ac91d5646de0c0c60c4 to your computer and use it in GitHub Desktop.
Pi Zero multiple USB gadgets minimal example
#!/bin/bash -e
modprobe libcomposite
cd /sys/kernel/config/usb_gadget/
mkdir g && cd g
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB 2.0
echo 0xEF > bDeviceClass
echo 0x02 > bDeviceSubClass
echo 0x01 > bDeviceProtocol
mkdir -p strings/0x409
echo "deadbeef00115599" > strings/0x409/serialnumber
echo "irq5 labs" > strings/0x409/manufacturer
echo "Pi Zero Gadget" > strings/0x409/product
mkdir -p functions/acm.usb0 # serial
mkdir -p functions/rndis.usb0 # network
mkdir -p configs/c.1
echo 250 > configs/c.1/MaxPower
ln -s functions/rndis.usb0 configs/c.1/
ln -s functions/acm.usb0 configs/c.1/
# OS descriptors
echo 1 > os_desc/use
echo 0xcd > os_desc/b_vendor_code
echo MSFT100 > os_desc/qw_sign
echo RNDIS > functions/rndis.usb0/os_desc/interface.rndis/compatible_id
echo 5162001 > functions/rndis.usb0/os_desc/interface.rndis/sub_compatible_id
ln -s configs/c.1 os_desc
udevadm settle -t 5 || :
ls /sys/class/udc/ > UDC
# /usr/lib/systemd/system/myusbgadget.service
[Unit]
Description=My USB gadget
After=systemd-modules-load.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/myusbgadget
[Install]
WantedBy=sysinit.target
@dashxdr
Copy link

dashxdr commented Sep 3, 2020

Regarding my previous post, I have a new datapoint. I grabbed an older release
linux-raspberrypi-kernel_1.20170215-1.tar.gz
and ran through the steps and everything works as promised. So... something was broken between that release and 5.4.51+:
Linux version 5.4.51+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611)) #1333 Mon Aug 10 16:38:02 BST 2020
which was the kernel included in the Raspberry Pi OS (32-bit) Lite image I grabbed yesterday... released 2020-08-20 and presumably with sha-256
24342f3668f590d368ce9d57322c401cf2e57f3ca969c88cf9f4df238aaec41f

@dashxdr
Copy link

dashxdr commented Sep 4, 2020

Ok more progress. I've isolated the failure to between two releases:
linux-raspberrypi-kernel_1.20180313-1 GOOD
linux-raspberrypi-kernel_1.20180328-1 BAD

Unfortunately it seems to be a massive overhaul, just doing diff -Nur on the drivers/usb/gadget directories yields a 544kbyte file with 19,253 lines. My contribution is done now.

@hardillb
Copy link

I have an open issue against the Raspberry Pi Kernel (raspberrypi/linux#3862) to track this

@rundekugel
Copy link

rundekugel commented Jun 17, 2021

For the network part, try this:
https://github.com/rundekugel/USBGadgetNetwork

It combines CDC/ECM and RNDIS
It works for Windows10 / Linux and MACos

I didn't try to add additional cdc/acm, but I think, it should work on most OS.

@ValdikSS
Copy link

ValdikSS commented Jan 11, 2025

@nrclark

If you have a working example that provides ACM, ECM, and RNDIS (with ACM/RNDIS working on windows and ACM/ECM working on Mac), it could help me out bigtime.

See this
https://gist.github.com/Leo-PL/1ee48d132bc7a7ccd4657ea1ed7badd8

It includes two alternative configurations (USB term): one with RNDIS+ACM, another with ECM+ACM.

EDIT: nevermind, it doesn't work indeed. Windows stops recognizing ACM as soon as there are two configurations.
Interestingly, this question is about this exact issue has been fixed by using

echo "0xEF" > ${g}/bDeviceClass
echo "0x02" > ${g}/bDeviceSubClass
echo "0x01" > ${g}/bDeviceProtocol

which is already present in the script above. I've tried the snippet from the question with the fix, and it does not work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment