Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@hardillb

This comment has been minimized.

Copy link

@hardillb hardillb commented Jan 10, 2018

I can't get this to work with a mac, it works fine on Windows and Linux.

No matter what I try I can't seam to find a combination that will work with all Windows/Mac/Linux if I want more than just g_ether (I want a mass_storage to server up a set of tools that talk to the pi via the ethernet connection)

@jadonk

This comment has been minimized.

Copy link

@jadonk jadonk commented Mar 11, 2018

On a mac, use ecm, rather than rndis.

@bousqi

This comment has been minimized.

Copy link

@bousqi bousqi commented Feb 8, 2019

Thanks ! it helps a lot...

@lorenzo95

This comment has been minimized.

Copy link

@lorenzo95 lorenzo95 commented Jun 14, 2019

Oh Sweet thank you. Works like a charm with Windows 10.

@badbyt3

This comment has been minimized.

Copy link

@badbyt3 badbyt3 commented Feb 24, 2020

Copied and pasted everything, /dev/ttyACM0 is simply not being created...

@aurelihein

This comment has been minimized.

Copy link

@aurelihein aurelihein commented Apr 20, 2020

Working fine for Windows 10 OS !
@hardillb : RNDIS is not supported under MAC OS, you can try g_cdc instead of g_ether (do not forget use_eem=0)
@badbyt3 : /dev/ttyACM0 is on the host part, on the embedded device part it is /dev/ttyGS0
Thank you

@teafella

This comment has been minimized.

Copy link

@teafella teafella commented Apr 27, 2020

Could anyone provide a working example of the myusbgadget file with MacOS? Cant get this to work with Catalina.

using g_ether disables acm

Edit: I was able to get this working by using ecm, installing dnsmasq and issuing ifup usb0

@maxiwoj

This comment has been minimized.

Copy link

@maxiwoj maxiwoj commented Jun 14, 2020

@teafella I've been working on creating a configuration that would work both on Windows and OS X Catalina (and Linux ofc as well). The trick is to create separate configurations for RNDIS and ECM with libcomposite, then link appropriate functions:

# CDC ECM
mkdir -p configs/c.2/strings/0x409
echo "ECM" > configs/c.2/strings/0x409/configuration
echo 250 > configs/c.2/MaxPower

mkdir -p functions/ecm.usb0
echo $HOST > functions/ecm.usb0/host_addr
echo $SELF1 > functions/ecm.usb0/dev_addr

ln -s functions/ecm.usb0 configs/c.2

This way it works also on OS X

@nrclark

This comment has been minimized.

Copy link

@nrclark nrclark commented Jul 28, 2020

@maxiwoj Did you ever get your composite USB script working?

I've been trying to make a composite device that provides an ACM serial-port, an ECM network interface (for mac/Linux), and an RNDIS interface (for Windows). For some reason, Windows doesn't want to show the ACM interface. I can make the ACM and ECM show up on MacOS just fine, but Windows won't show the ACM if I have more than one config. I haven't figured out why yet. :(

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.

@jdevelop

This comment has been minimized.

Copy link

@jdevelop jdevelop commented Aug 16, 2020

Somehow I can't make it working on Windows 10.

The RNDIS device appears under "Other devices", but there's no driver available.

@maxiwoj

This comment has been minimized.

Copy link

@maxiwoj maxiwoj commented Aug 24, 2020

@nrclark I couldn't make Windows recognize RNDIS device in a composite device setup. The only thing I could do was either skip the second device (Mass Storage in my case) on Windows or make it recognize as a composite device but not install appropriate drivers for the RNDIS (a composite device with 2 subdevices is detected, but the required drivers are not installed and devices are not recognized properly).

The first (working) configuration (just RNDIS for Windows and a composite device for other systems) I made available here in my project Ethsploiter.

Since Windows requires a particular pair of IdVendor/idProduct to determine the driver for RNDIS, the configuration of placing RNDIS inside a composite setup might not work without providing a custom INF file stating the information about the devices in the composite setup. However, I haven't confirmed that so If anyone makes it work without the INF file or confirms that it is not possible, I'd be grateful for such information!

@dashxdr

This comment has been minimized.

Copy link

@dashxdr dashxdr commented Sep 3, 2020

I can confirm the sequence described in the original article http://irq5.io/2016/12/22/raspberry-pi-zero-as-multiple-usb-gadgets/ does not work with the current Raspberry PI os, or an even later one linux-rpi-5.4.y I grabbed yesterday (I'm writing this Sept 3 2020).

Individual usb gadget devices work fine, but as soon as I try to have 2, neither one works. I also tried eem instead of th rndis, no improvement.

I'm wondering which version of the pi linux kernel did work, I'll be happy to rollback to that. This is all on a pi zero, btw.

@dashxdr

This comment has been minimized.

Copy link

@dashxdr 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

This comment has been minimized.

Copy link

@dashxdr 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

This comment has been minimized.

Copy link

@hardillb hardillb commented Sep 27, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.