xref: /openbmc/linux/Documentation/usb/raw-gadget.rst (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
1f2c2e717SAndrey Konovalov==============
2f2c2e717SAndrey KonovalovUSB Raw Gadget
3f2c2e717SAndrey Konovalov==============
4f2c2e717SAndrey Konovalov
5*7a35a5caSAndrey KonovalovUSB Raw Gadget is a gadget driver that gives userspace low-level control over
6*7a35a5caSAndrey Konovalovthe gadget's communication process.
7*7a35a5caSAndrey Konovalov
8*7a35a5caSAndrey KonovalovLike any other gadget driver, Raw Gadget implements USB devices via the
9*7a35a5caSAndrey KonovalovUSB gadget API. Unlike most gadget drivers, Raw Gadget does not implement
10*7a35a5caSAndrey Konovalovany concrete USB functions itself but requires userspace to do that.
11*7a35a5caSAndrey Konovalov
12*7a35a5caSAndrey KonovalovRaw Gadget is currently a strictly debugging feature and should not be used
13*7a35a5caSAndrey Konovalovin production. Use GadgetFS instead.
14*7a35a5caSAndrey Konovalov
15*7a35a5caSAndrey KonovalovEnabled with CONFIG_USB_RAW_GADGET.
16f2c2e717SAndrey Konovalov
17f2c2e717SAndrey KonovalovComparison to GadgetFS
18f2c2e717SAndrey Konovalov~~~~~~~~~~~~~~~~~~~~~~
19f2c2e717SAndrey Konovalov
20*7a35a5caSAndrey KonovalovRaw Gadget is similar to GadgetFS but provides more direct access to the
21*7a35a5caSAndrey KonovalovUSB gadget layer for userspace. The key differences are:
22f2c2e717SAndrey Konovalov
23*7a35a5caSAndrey Konovalov1. Raw Gadget passes every USB request to userspace to get a response, while
24f2c2e717SAndrey Konovalov   GadgetFS responds to some USB requests internally based on the provided
25*7a35a5caSAndrey Konovalov   descriptors. Note that the UDC driver might respond to some requests on
26*7a35a5caSAndrey Konovalov   its own and never forward them to the gadget layer.
27f2c2e717SAndrey Konovalov
28*7a35a5caSAndrey Konovalov2. Raw Gadget allows providing arbitrary data as responses to USB requests,
29*7a35a5caSAndrey Konovalov   while GadgetFS performs sanity checks on the provided USB descriptors.
30*7a35a5caSAndrey Konovalov   This makes Raw Gadget suitable for fuzzing by providing malformed data as
31*7a35a5caSAndrey Konovalov   responses to USB requests.
32f2c2e717SAndrey Konovalov
33f2c2e717SAndrey Konovalov3. Raw Gadget provides a way to select a UDC device/driver to bind to,
34*7a35a5caSAndrey Konovalov   while GadgetFS currently binds to the first available UDC. This allows
35*7a35a5caSAndrey Konovalov   having multiple Raw Gadget instances bound to different UDCs.
36f2c2e717SAndrey Konovalov
3797df5e57SAndrey Konovalov4. Raw Gadget explicitly exposes information about endpoints addresses and
38*7a35a5caSAndrey Konovalov   capabilities. This allows the user to write UDC-agnostic gadgets.
39f2c2e717SAndrey Konovalov
40*7a35a5caSAndrey Konovalov5. Raw Gadget has an ioctl-based interface instead of a filesystem-based
41*7a35a5caSAndrey Konovalov   one.
42f2c2e717SAndrey Konovalov
43f2c2e717SAndrey KonovalovUserspace interface
44f2c2e717SAndrey Konovalov~~~~~~~~~~~~~~~~~~~
45f2c2e717SAndrey Konovalov
46*7a35a5caSAndrey KonovalovThe user can interact with Raw Gadget by opening ``/dev/raw-gadget`` and
47*7a35a5caSAndrey Konovalovissuing ioctl calls; see the comments in include/uapi/linux/usb/raw_gadget.h
48*7a35a5caSAndrey Konovalovfor details. Multiple Raw Gadget instances (bound to different UDCs) can be
49*7a35a5caSAndrey Konovalovused at the same time.
50f2c2e717SAndrey Konovalov
51*7a35a5caSAndrey KonovalovA typical usage scenario of Raw Gadget:
52f2c2e717SAndrey Konovalov
53*7a35a5caSAndrey Konovalov1. Create a Raw Gadget instance by opening ``/dev/raw-gadget``.
54*7a35a5caSAndrey Konovalov2. Initialize the instance via ``USB_RAW_IOCTL_INIT``.
55*7a35a5caSAndrey Konovalov3. Launch the instance with ``USB_RAW_IOCTL_RUN``.
56*7a35a5caSAndrey Konovalov4. In a loop issue ``USB_RAW_IOCTL_EVENT_FETCH`` to receive events from
57*7a35a5caSAndrey Konovalov   Raw Gadget and react to those depending on what kind of USB gadget must
58*7a35a5caSAndrey Konovalov   be implemented.
59f2c2e717SAndrey Konovalov
60*7a35a5caSAndrey KonovalovNote that some UDC drivers have fixed addresses assigned to endpoints, and
61*7a35a5caSAndrey Konovalovtherefore arbitrary endpoint addresses cannot be used in the descriptors.
62*7a35a5caSAndrey KonovalovNevertheless, Raw Gadget provides a UDC-agnostic way to write USB gadgets.
63*7a35a5caSAndrey KonovalovOnce ``USB_RAW_EVENT_CONNECT`` is received via ``USB_RAW_IOCTL_EVENT_FETCH``,
64*7a35a5caSAndrey Konovalov``USB_RAW_IOCTL_EPS_INFO`` can be used to find out information about the
65*7a35a5caSAndrey Konovalovendpoints that the UDC driver has. Based on that, userspace must choose UDC
66*7a35a5caSAndrey Konovalovendpoints for the gadget and assign addresses in the endpoint descriptors
67*7a35a5caSAndrey Konovalovcorrespondingly.
6861d2658dSAndrey Konovalov
69*7a35a5caSAndrey KonovalovRaw Gadget usage examples and a test suite:
7061d2658dSAndrey Konovalov
7161d2658dSAndrey Konovalovhttps://github.com/xairy/raw-gadget
7261d2658dSAndrey Konovalov
7361d2658dSAndrey KonovalovInternal details
7461d2658dSAndrey Konovalov~~~~~~~~~~~~~~~~
7561d2658dSAndrey Konovalov
76*7a35a5caSAndrey KonovalovEvery Raw Gadget endpoint read/write ioctl submits a USB request and waits
77*7a35a5caSAndrey Konovalovuntil its completion. This is done deliberately to assist with coverage-guided
78*7a35a5caSAndrey Konovalovfuzzing by having a single syscall fully process a single USB request. This
79*7a35a5caSAndrey Konovalovfeature must be kept in the implementation.
8061d2658dSAndrey Konovalov
81f2c2e717SAndrey KonovalovPotential future improvements
82f2c2e717SAndrey Konovalov~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83f2c2e717SAndrey Konovalov
84*7a35a5caSAndrey Konovalov- Report more events (suspend, resume, etc.) through
85*7a35a5caSAndrey Konovalov  ``USB_RAW_IOCTL_EVENT_FETCH``.
86f2c2e717SAndrey Konovalov
87*7a35a5caSAndrey Konovalov- Support ``O_NONBLOCK`` I/O. This would be another mode of operation, where
88*7a35a5caSAndrey Konovalov  Raw Gadget would not wait until the completion of each USB request.
8961d2658dSAndrey Konovalov
9061d2658dSAndrey Konovalov- Support USB 3 features (accept SS endpoint companion descriptor when
91*7a35a5caSAndrey Konovalov  enabling endpoints; allow providing ``stream_id`` for bulk transfers).
9261d2658dSAndrey Konovalov
93*7a35a5caSAndrey Konovalov- Support ISO transfer features (expose ``frame_number`` for completed
94*7a35a5caSAndrey Konovalov  requests).
95