[LINUX] USB Raw Gadget


USB Raw Gadget, supported by Linux Kernel 5.7-rc1! You did it! !! You can now create USB Gadgets as an application that moves in the user space!

Docs » USB support » USB Raw Gadget

USB Raw Gadget

USB Raw Gadget is a kernel module that provides a userspace interface for the USB Gadget subsystem. Essentially it allows to emulate USB devices from userspace. Enabled with CONFIG_USB_RAW_GADGET. Raw Gadget is currently a strictly debugging feature and shouldn’t be used in production, use GadgetFS instead.

USB Raw Gadget provides a user-space interface to the USB Gadget subsystem. In essence, it allows you to emulate a USB device from user space. Please enable CONF_USB_RAW_GADGET. Raw Gadget is currently being strictly debugged and should not be used in the product. Use GadgetFS instead.

Comparison to GadgetFS

Raw Gadget is similar to GadgetFS, but provides a more low-level and direct access to the USB Gadget layer for the userspace. The key differences are:

Raw Gadget is similar to GadgetFS, but provides low-level, direct access to the USB Gadget layer from user space. The main differences are as follows.

1. Every USB request is passed to the userspace to get a response, while GadgetFS responds to some USB requests internally based on the provided descriptors. However note, that the UDC driver might respond to some requests on its own and never forward them to the Gadget layer.

  1. All USB requests are passed to user space for response. GadgetFS internally responds to some USB requests according to the provided descriptor. However, the UDC driver may respond to some requests on its own and not forward them to the Gadget Layer.

2. GadgetFS performs some sanity checks on the provided USB descriptors, while Raw Gadget allows you to provide arbitrary data as responses to USB requests.

  1. GadgetFS verifies the validity of the provided USB descriptor. Raw Gadget can provide arbitrary data in response to a USB request.

3. Raw Gadget provides a way to select a UDC device/driver to bind to, while GadgetFS currently binds to the first available UDC.

  1. Raw Gadget provides a way to select the UDC device / driver you want to bind, but GadgetFS currently binds the first valid UDC.

4. Raw Gadget uses predictable endpoint names (handles) across different UDCs (as long as UDCs have enough endpoints of each required transfer type)

  1. Raw Gadget uses a predictable endpoint name (handke) across different UDCs (provided there are enough endpoints for each transfer type. UDC requires).

5.Raw Gadget has ioctl-based interface instead of a filesystem-based one.

  1. Raw Gadge provides a uictl-base interface instead of filesystem-based.

Userspace interface

To create a Raw Gadget instance open /dev/raw-gadget. Multiple raw-gadget instances (bound to different UDCs) can be used at the same time. The interaction with the opened file happens through the ioctl() calls, see comments in include/uapi/linux/usb/raw_gadget.h for details.

To create a Raw Gadget instance, open / dev / raw-gadet. You can have multiple raw-gadget instances available at the same time (for different UDCs). Call ioctl () to interact with the opened file. See the comments in include / uapi / linux / usb / raw_gadget.h for more information.

The typical usage of Raw Gadget looks like:

The typical usage of Raw Gadget is as follows.

1. Open Raw Gadget instance via /dev/raw-gadget. 2. Initialize the instance via USB_RAW_IOCTL_INIT. 3. Launch the instance with USB_RAW_IOCTL_RUN. 4. In a loop issue USB_RAW_IOCTL_EVENT_FETCH calls to receive events from Raw Gadget and react to those depending on what kind of USB device needs to be emulated.

    1. Open the Raw Gadget instance via / dev / raw-gadet.
  1. Initialize the instance with USB_RAW_IOCTL_INIT.
    1. Launch an instance with USB_RAW_IOCTL_RUN.
  2. With RAW_IOCTL_EVENT_FETCH, it loops while receiving events from Raw Gadget and reacts like a USB device to emulate.

Potential future improvements

・ Implement ioctl ’s for setting / clearing halt status on endpoints. ・ Reporting more events (suspend, resume, etc.) through USB_RAW_IOCTL_EVENT_FETCH. ・ Support O_NONBLOCK I / O.

-Implementation of ioctl to set / clear the stop status of endpoint -Report of various events (suspend, resume, etc.) via USB_RAW_IOCTL_EVENT_FETCH. -Support for O_NONBLOCK I / O.

Originally, it is a part of the Linux Kernel source code, so it will be treated as GPLv2 (recognition that it should be).


Licensing documentation

The following describes the license of the Linux kernel source code (GPLv2), how to properly mark the license of individual files in the source tree, as well as links to the full license text.


Recommended Posts

USB Raw Gadget
raw string