Build a USB Gadget With configfs and libcomposite
Turn a Linux 6.12 board with a USB device controller into a USB serial and Ethernet gadget using configfs and libcomposite, with the exact directory steps.

Most USB work on Linux is host-side: the machine is the one that enumerates devices. Boards with a USB device controller, such as a Raspberry Pi Zero or a BeagleBone, can be the other end and pretend to be a USB device: a serial port, a network adapter, a storage drive. The kernel mechanism for this is the gadget framework, configured at run time through configfs and the libcomposite module, with no C code. This post builds a composite gadget with a serial port and an Ethernet function on Linux 6.12, explaining each directory you create and why the order matters. The descriptors it writes are the ones described in how USB works, generated from files instead of from a chip.
The authoritative text is the kernel's USB gadget configfs documentation.
What you need
Three kernel pieces have to be present. First, a USB device controller (UDC) driver for your hardware, for example dwc2 on a Raspberry Pi or musb on a BeagleBone. A normal PC's xHCI controller is host-only and cannot do this, so the steps below are for an embedded board. Second, CONFIG_USB_CONFIGFS with the function options you want, such as CONFIG_USB_CONFIGFS_ACM for serial and CONFIG_USB_CONFIGFS_ECM for Ethernet. Third, configfs mounted, which most systems do at boot:
mount -t configfs none /sys/kernel/config 2>/dev/null
modprobe libcomposite
ls /sys/kernel/config/usb_gadget
ls /sys/class/udcfe980000.usbThe second ls lists the available UDC, and its name is what you bind the gadget to in the last step. If the directory is empty, no controller driver is loaded and nothing below will work. The first ls prints nothing for a fresh system, since no gadget exists yet.
The structure of a gadget in configfs
A gadget is a tree of directories you create with mkdir, and files you write values into. The tree mirrors the USB descriptor hierarchy:
| Directory | Role |
|---|---|
g1/ | The gadget, which carries the device descriptor fields |
g1/strings/0x409/ | Strings in US English: manufacturer, product, serial |
g1/functions/acm.usb0/ | A function instance: here a serial (ACM) port |
g1/configs/c.1/ | A configuration, as in the descriptor tree |
g1/configs/c.1/acm.usb0 | A symlink that puts the function into the configuration |
Functions are created inside functions/ as <type>.<instance>, and they only become part of the device when you symlink them into a configuration. This is how a composite device gets several interfaces: link several functions into one configuration.
Create the gadget
Run the following as root, in one script. The vendor and product IDs below are a placeholder pair meant for testing. For anything you ship, use IDs you are entitled to use:
set -e
cd /sys/kernel/config/usb_gadget
mkdir g1
cd g1
echo 0x1d6b > idVendor
echo 0x0104 > idProduct
echo 0x0100 > bcdDevice
echo 0x0200 > bcdUSB
mkdir -p strings/0x409
echo "0123456789" > strings/0x409/serialnumber
echo "Example Corp" > strings/0x409/manufacturer
echo "Sample Gadget" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Serial and Ethernet" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower
mkdir functions/acm.usb0
mkdir functions/ecm.usb0
ln -s functions/acm.usb0 configs/c.1/
ln -s functions/ecm.usb0 configs/c.1/
echo fe980000.usb > UDCThe last line is the switch. Writing the UDC name makes the gadget visible on the bus, and the board now appears to its host as a USB device. Until that write, everything is configuration with nothing on the wire.
The kernel log of the board reports the bind:
configfs-gadget.g1 gadget.0: bound to fe980000.usb
dwc2 fe980000.usb: bound driver configfs-gadget.g1The first line names your gadget and the controller it bound to. On the other side, the host computer enumerates a new device and, for the two functions, creates a /dev/ttyACM0 serial port and a network interface.
Why the order matters
Two ordering rules cause most failures.
The function symlinks must exist before you write the UDC file. The descriptors are built when the gadget binds, so a function linked afterward does not appear. To change a live gadget, write an empty line to UDC first, make the change, and write the controller name again.
Directories can be removed only in reverse order. Deleting a gadget means unbinding it, removing the symlinks, removing the configuration strings and configuration, removing the functions and strings, and finally removing the gadget directory. rmdir on a directory that still has a child fails with Directory not empty. A teardown script should be the reverse of the setup, line by line.
Function instances and their options
Each function type has its own directory and files. A few worth knowing:
| Function | Name | Result on the host | Main settings |
|---|---|---|---|
| Serial | acm.<n> | /dev/ttyACM* | None needed |
| Ethernet (Linux, macOS) | ecm.<n> | A network interface | host_addr, dev_addr |
| Ethernet (Windows friendly) | rndis.<n> | A network interface | Needs OS descriptors |
| Mass storage | mass_storage.<n> | A drive | lun.0/file, lun.0/ro |
| HID | hid.<n> | A keyboard or mouse | protocol, subclass, report_desc |
Set MAC addresses on the Ethernet function so they stay stable across reboots. A random MAC on each boot makes the host treat the gadget as a new network card each time, which breaks static IP configuration there:
echo 02:00:00:00:00:01 > functions/ecm.usb0/host_addr
echo 02:00:00:00:00:02 > functions/ecm.usb0/dev_addrThese must be written before the symlink and the bind. The first byte 02 marks a locally administered unicast address, which is the right choice for an address you assign yourself.
On the board, the other end of the Ethernet function is a normal interface named usb0, which you configure like any other:
ip addr add 192.168.7.2/24 dev usb0
ip link set usb0 upA serial console on the gadget
The acm function gives the board a serial port toward the host. To log in to the board through it, start a getty on the gadget side of the port. The gadget end appears as /dev/ttyGS0 (GS for gadget serial), and a service such as serial-getty@ttyGS0 attaches a login prompt. From the host, open /dev/ttyACM0 with any terminal program. That gives a console cable made of the USB cable you already have, which is why this setup is common on small boards.
Descriptors and identification
Two details affect how hosts see your device. A composite device with more than one function that sets class, subclass and protocol in the device descriptor to 0xef, 0x02, 0x01 (the "interface association" triple) is the correct form on modern hosts, and the kernel sets it automatically when you link several functions. Writing the three values yourself is only needed for unusual host stacks.
Windows chooses a driver by matching IDs and class descriptors. Linux and macOS use the standard CDC classes for acm and ecm without extra files, but Windows needs the RNDIS function or a signed driver for ECM, which is why many gadget recipes use rndis when the host is Windows. That is a host-side constraint and does not change how you build the gadget.
Teardown
To remove the gadget cleanly, run the reverse sequence:
cd /sys/kernel/config/usb_gadget/g1
echo "" > UDC
rm configs/c.1/acm.usb0 configs/c.1/ecm.usb0
rmdir configs/c.1/strings/0x409
rmdir configs/c.1
rmdir functions/acm.usb0 functions/ecm.usb0
rmdir strings/0x409
cd ..
rmdir g1After the first line the gadget disappears from the host's bus, and the host logs a disconnect. This is also the sequence a systemd unit should use as its ExecStop.


