1==============
2USB Raw Gadget
3==============
4
5USB Raw Gadget is a kernel module that provides a userspace interface for
6the USB Gadget subsystem. Essentially it allows to emulate USB devices
7from userspace. Enabled with CONFIG_USB_RAW_GADGET. Raw Gadget is
8currently a strictly debugging feature and shouldn't be used in
9production, use GadgetFS instead.
10
11Comparison to GadgetFS
12~~~~~~~~~~~~~~~~~~~~~~
13
14Raw Gadget is similar to GadgetFS, but provides a more low-level and
15direct access to the USB Gadget layer for the userspace. The key
16differences are:
17
181. Every USB request is passed to the userspace to get a response, while
19   GadgetFS responds to some USB requests internally based on the provided
20   descriptors. However note, that the UDC driver might respond to some
21   requests on its own and never forward them to the Gadget layer.
22
232. GadgetFS performs some sanity checks on the provided USB descriptors,
24   while Raw Gadget allows you to provide arbitrary data as responses to
25   USB requests.
26
273. Raw Gadget provides a way to select a UDC device/driver to bind to,
28   while GadgetFS currently binds to the first available UDC.
29
304. Raw Gadget explicitly exposes information about endpoints addresses and
31   capabilities allowing a user to write UDC-agnostic gadgets.
32
335. Raw Gadget has ioctl-based interface instead of a filesystem-based one.
34
35Userspace interface
36~~~~~~~~~~~~~~~~~~~
37
38To create a Raw Gadget instance open /dev/raw-gadget. Multiple raw-gadget
39instances (bound to different UDCs) can be used at the same time. The
40interaction with the opened file happens through the ioctl() calls, see
41comments in include/uapi/linux/usb/raw_gadget.h for details.
42
43The typical usage of Raw Gadget looks like:
44
451. Open Raw Gadget instance via /dev/raw-gadget.
462. Initialize the instance via USB_RAW_IOCTL_INIT.
473. Launch the instance with USB_RAW_IOCTL_RUN.
484. In a loop issue USB_RAW_IOCTL_EVENT_FETCH calls to receive events from
49   Raw Gadget and react to those depending on what kind of USB device
50   needs to be emulated.
51
52Note, that some UDC drivers have fixed addresses assigned to endpoints, and
53therefore arbitrary endpoint addresses can't be used in the descriptors.
54Nevertheles, Raw Gadget provides a UDC-agnostic way to write USB gadgets.
55Once a USB_RAW_EVENT_CONNECT event is received via USB_RAW_IOCTL_EVENT_FETCH,
56the USB_RAW_IOCTL_EPS_INFO ioctl can be used to find out information about
57endpoints that the UDC driver has. Based on that information, the user must
58chose UDC endpoints that will be used for the gadget being emulated, and
59properly assign addresses in endpoint descriptors.
60
61You can find usage examples (along with a test suite) here:
62
63https://github.com/xairy/raw-gadget
64
65Internal details
66~~~~~~~~~~~~~~~~
67
68Currently every endpoint read/write ioctl submits a USB request and waits until
69its completion. This is the desired mode for coverage-guided fuzzing (as we'd
70like all USB request processing happen during the lifetime of a syscall),
71and must be kept in the implementation. (This might be slow for real world
72applications, thus the O_NONBLOCK improvement suggestion below.)
73
74Potential future improvements
75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76
77- Report more events (suspend, resume, etc.) through USB_RAW_IOCTL_EVENT_FETCH.
78
79- Support O_NONBLOCK I/O.
80
81- Support USB 3 features (accept SS endpoint companion descriptor when
82  enabling endpoints; allow providing stream_id for bulk transfers).
83
84- Support ISO transfer features (expose frame_number for completed requests).
85