Note: Do not use Raw Gadget in production for emulating USB devices with concrete classes. Instead, use the composite framework or the legacy gadget driver modules. Raw Gadget is intended for fuzzing and exploiting USB hosts or for proxying USB devices.
Raw Gadget is a Linux kernel module that implements a low-level interface for the Linux USB Gadget subsystem. It is similar to GadgetFS, but provides greater flexibility; see all the differences here.
Raw Gadget can be used to emulate USB devices, both physical and virtual ones. Emulating physical devices requires a Linux board with a USB Device Controller (UDC), such as a Raspberry Pi. Emulating virtual devices requires no hardware and instead relies on the Dummy HCD/UDC module (such devices get connected to the kernel Raw Gadget is running on).
This repository contains instructions and examples for using Raw Gadget.
Raw Gadget has been merged into the mainline Linux kernel in
There's no need to use
5.7+ kernels; see raw_gadget and dummy_hcd for information on how to build and
insmod corresponding modules on older kernels.
The modules should be compatible with kernel versions down to
4.14; see the table below.
Building the Raw Gadget and Dummy HCD/UDC kernel modules requires kernel headers.
On desktop Ubuntu, you can get them by installing
On a Raspberry Pi, follow these instructions.
Raw Gadget requires the user to provide the UDC device and driver names. This allows using Raw Gadget with a particular UDC if a few of them are present on the system.
UDC device names can be found in
$ ls /sys/class/udc/ dummy_udc.0
The UDC driver name is usually present in
$ cat /sys/class/udc/dummy_udc.0/uevent USB_UDC_NAME=dummy_udc
Below is a table of UDCs that were tested with Raw Gadget.
|Raspberry Pi Zero||
|Raspberry Pi 4||
|USB Armory MkII||
|Orange Pi PC||
|Orange Pi PC 2||
||No, kernel too old|
There's a prototype of a Facedancer backend based on Raw Gadget.
This backend relies on a few out-of-tree Raw Gadget patches present in the dev branch. Once the backend is thoroughly tested, these patches will be submitted to the mainline.
Raw Gadget-based backend accepts a few parameters through environment variables:
||UDC driver name||
||UDC device name||
||USB device speed||
Example of using Facedancer with Raw Gadget to emulate a USB keyboard on a Raspberry Pi 4:
export BACKEND=rawgadget export RG_UDC_DRIVER=fe980000.usb export RG_UDC_DEVICE=fe980000.usb ./legacy-applets/facedancer-keyboard.py
Note: Some Facedancer examples might fail if a wrong USB speed is specified.
Failures happen either with
USB_RAW_IOCTL_EP_READ/WRITE, or can be completely random.
For example, with Dummy UDC,
Note: This backend is still a prototype. Outstanding tasks:
- Make sure that all required backend callbacks are implemented. For example,
read_from_endpointshould probably be implemented.
- Provide a common Python wrapper for Raw Gadget ioctls, and use it in the backend.
- Finalize and submit out-of-tree Raw Gadget patches to the mainline.
Note: Facedancer assumes that every backend supports non-blocking I/O, which is not the case for Raw Gadget. To work around this limitation, the backend prototype relies on timeouts. The proper solution to this issue would be to add non-blocking I/O support to Raw Gadget.
As a generic guidance to troubleshooting Raw Gadget errors:
Switch to the dev branch.
This branch contains fixes for some known issues and prints more debug output.
Enable debug output for Raw Gadget (and Dummy HCD/UDC if you're using it).
To do this, add the following line to the very beginning of
Then rebuild and reinsert the module.
Check the kernel log via
dmesgto figure out what is failing.
ENODEV, error code
ioctl(USB_RAW_IOCTL_RUN): No such device
This error means that bad UDC driver/device names were provided. Make sure that the UDC driver module is loaded. Also see USB Device Controllers about UDC names.
Endpoint operations return
ESHUTDOWN, error code
ioctl(USB_RAW_IOCTL_EP0_WRITE): Cannot send after transport endpoint shutdown
This error means that the emulated USB device tried to send data on a disabled endpoint.
This usually happens when the device emulation code does something wrong. For example, tries to perform an endpoint operation before the device was configured. Or provides an endpoint descriptor that does not match the USB device speed. As a result, either the UDC driver or the host decides to disconnect the device.
Note: During device operation, the host might decide to reconfigure the device.
The UDC driver will then issue a reset or a disconnect event (depends on which UDC driver is in use).
After this, any attempt to issue an endpoint operation will fail with
ESHUTDOWN until the device emulation code calls
USB_RAW_IOCTL_CONFIGURE again when handling a new
Getting notifications about the reset and disconnect events requires using the Raw Gadget patches from the dev branch.
Note that if an endpoint operation fails with
ESHUTDOWN, the Raw Gadget device will be put into the
STATE_DEV_FAILED state and stop functioning.
- google/syzkaller — a kernel fuzzer, uses Raw Gadget for fuzzing Linux kernel USB drivers.
- AristoChen/usb-proxy — A USB proxy based on Raw Gadget and libusb.
- blegas78/usb-sniffify — Another USB proxy based on Raw Gadget and libusb.
- patryk4815/usb-proxy — A USB proxy based on Raw Gadget and written in Go.
Other potential fixes/improvements to investigate:
ep->maxpacketin gadget drivers.
- OTG support.
- Don't pass
dev, get from
The parts of code in this repository that are derived from the Linux kernel are covered by GPL-2.0. Everything else is covered by Apache-2.0.
SPDX-License-Identifier marks the used license in each file.