2020-02-25 00:13:03 +08:00
|
|
|
==============
|
|
|
|
USB Raw Gadget
|
|
|
|
==============
|
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
USB Raw Gadget is a gadget driver that gives userspace low-level control over
|
|
|
|
the gadget's communication process.
|
|
|
|
|
|
|
|
Like any other gadget driver, Raw Gadget implements USB devices via the
|
|
|
|
USB gadget API. Unlike most gadget drivers, Raw Gadget does not implement
|
|
|
|
any concrete USB functions itself but requires userspace to do that.
|
|
|
|
|
|
|
|
Raw Gadget is currently a strictly debugging feature and should not be used
|
|
|
|
in production. Use GadgetFS instead.
|
|
|
|
|
|
|
|
Enabled with CONFIG_USB_RAW_GADGET.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
|
|
|
Comparison to GadgetFS
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
Raw Gadget is similar to GadgetFS but provides more direct access to the
|
|
|
|
USB gadget layer for userspace. The key differences are:
|
2020-02-25 00:13:03 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
1. Raw Gadget passes every USB request to userspace to get a response, while
|
2020-02-25 00:13:03 +08:00
|
|
|
GadgetFS responds to some USB requests internally based on the provided
|
2021-01-24 03:14:17 +08:00
|
|
|
descriptors. Note that the UDC driver might respond to some requests on
|
|
|
|
its own and never forward them to the gadget layer.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
2. Raw Gadget allows providing arbitrary data as responses to USB requests,
|
|
|
|
while GadgetFS performs sanity checks on the provided USB descriptors.
|
|
|
|
This makes Raw Gadget suitable for fuzzing by providing malformed data as
|
|
|
|
responses to USB requests.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
|
|
|
3. Raw Gadget provides a way to select a UDC device/driver to bind to,
|
2021-01-24 03:14:17 +08:00
|
|
|
while GadgetFS currently binds to the first available UDC. This allows
|
|
|
|
having multiple Raw Gadget instances bound to different UDCs.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
2020-05-08 01:06:56 +08:00
|
|
|
4. Raw Gadget explicitly exposes information about endpoints addresses and
|
2021-01-24 03:14:17 +08:00
|
|
|
capabilities. This allows the user to write UDC-agnostic gadgets.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
5. Raw Gadget has an ioctl-based interface instead of a filesystem-based
|
|
|
|
one.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
|
|
|
Userspace interface
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
The user can interact with Raw Gadget by opening ``/dev/raw-gadget`` and
|
|
|
|
issuing ioctl calls; see the comments in include/uapi/linux/usb/raw_gadget.h
|
|
|
|
for details. Multiple Raw Gadget instances (bound to different UDCs) can be
|
|
|
|
used at the same time.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
A typical usage scenario of Raw Gadget:
|
2020-02-25 00:13:03 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
1. Create a Raw Gadget instance by opening ``/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`` to receive events from
|
|
|
|
Raw Gadget and react to those depending on what kind of USB gadget must
|
|
|
|
be implemented.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
Note that some UDC drivers have fixed addresses assigned to endpoints, and
|
|
|
|
therefore arbitrary endpoint addresses cannot be used in the descriptors.
|
|
|
|
Nevertheless, Raw Gadget provides a UDC-agnostic way to write USB gadgets.
|
|
|
|
Once ``USB_RAW_EVENT_CONNECT`` is received via ``USB_RAW_IOCTL_EVENT_FETCH``,
|
|
|
|
``USB_RAW_IOCTL_EPS_INFO`` can be used to find out information about the
|
|
|
|
endpoints that the UDC driver has. Based on that, userspace must choose UDC
|
|
|
|
endpoints for the gadget and assign addresses in the endpoint descriptors
|
|
|
|
correspondingly.
|
2020-05-08 01:06:58 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
Raw Gadget usage examples and a test suite:
|
2020-05-08 01:06:58 +08:00
|
|
|
|
|
|
|
https://github.com/xairy/raw-gadget
|
|
|
|
|
|
|
|
Internal details
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
Every Raw Gadget endpoint read/write ioctl submits a USB request and waits
|
|
|
|
until its completion. This is done deliberately to assist with coverage-guided
|
|
|
|
fuzzing by having a single syscall fully process a single USB request. This
|
|
|
|
feature must be kept in the implementation.
|
2020-05-08 01:06:58 +08:00
|
|
|
|
2020-02-25 00:13:03 +08:00
|
|
|
Potential future improvements
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
- Report more events (suspend, resume, etc.) through
|
|
|
|
``USB_RAW_IOCTL_EVENT_FETCH``.
|
2020-02-25 00:13:03 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
- Support ``O_NONBLOCK`` I/O. This would be another mode of operation, where
|
|
|
|
Raw Gadget would not wait until the completion of each USB request.
|
2020-05-08 01:06:58 +08:00
|
|
|
|
|
|
|
- Support USB 3 features (accept SS endpoint companion descriptor when
|
2021-01-24 03:14:17 +08:00
|
|
|
enabling endpoints; allow providing ``stream_id`` for bulk transfers).
|
2020-05-08 01:06:58 +08:00
|
|
|
|
2021-01-24 03:14:17 +08:00
|
|
|
- Support ISO transfer features (expose ``frame_number`` for completed
|
|
|
|
requests).
|