Skip to content

Commit f2c2e71

Browse files
xairyfelipebalbi
authored andcommitted
usb: gadget: add raw-gadget interface
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. 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: 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. 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. 3. Raw Gadget provides a way to select a UDC device/driver to bind to, while GadgetFS currently binds to the first available UDC. 4. Raw Gadget uses predictable endpoint names (handles) across different UDCs (as long as UDCs have enough endpoints of each required transfer type). 5. Raw Gadget has ioctl-based interface instead of a filesystem-based one. Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Signed-off-by: Felipe Balbi <balbi@kernel.org>
1 parent 1a0808c commit f2c2e71

6 files changed

Lines changed: 1319 additions & 0 deletions

File tree

‎Documentation/usb/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ USB support
2222
misc_usbsevseg
2323
mtouchusb
2424
ohci
25+
raw-gadget
2526
usbip_protocol
2627
usbmon
2728
usb-serial

‎Documentation/usb/raw-gadget.rst‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
==============
2+
USB Raw Gadget
3+
==============
4+
5+
USB Raw Gadget is a kernel module that provides a userspace interface for
6+
the USB Gadget subsystem. Essentially it allows to emulate USB devices
7+
from userspace. Enabled with CONFIG_USB_RAW_GADGET. Raw Gadget is
8+
currently a strictly debugging feature and shouldn't be used in
9+
production, use GadgetFS instead.
10+
11+
Comparison to GadgetFS
12+
~~~~~~~~~~~~~~~~~~~~~~
13+
14+
Raw Gadget is similar to GadgetFS, but provides a more low-level and
15+
direct access to the USB Gadget layer for the userspace. The key
16+
differences are:
17+
18+
1. 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+
23+
2. 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+
27+
3. 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+
30+
4. Raw Gadget uses predictable endpoint names (handles) across different
31+
UDCs (as long as UDCs have enough endpoints of each required transfer
32+
type).
33+
34+
5. Raw Gadget has ioctl-based interface instead of a filesystem-based one.
35+
36+
Userspace interface
37+
~~~~~~~~~~~~~~~~~~~
38+
39+
To create a Raw Gadget instance open /dev/raw-gadget. Multiple raw-gadget
40+
instances (bound to different UDCs) can be used at the same time. The
41+
interaction with the opened file happens through the ioctl() calls, see
42+
comments in include/uapi/linux/usb/raw_gadget.h for details.
43+
44+
The typical usage of Raw Gadget looks like:
45+
46+
1. Open Raw Gadget instance via /dev/raw-gadget.
47+
2. Initialize the instance via USB_RAW_IOCTL_INIT.
48+
3. Launch the instance with USB_RAW_IOCTL_RUN.
49+
4. In a loop issue USB_RAW_IOCTL_EVENT_FETCH calls to receive events from
50+
Raw Gadget and react to those depending on what kind of USB device
51+
needs to be emulated.
52+
53+
Potential future improvements
54+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55+
56+
- Implement ioctl's for setting/clearing halt status on endpoints.
57+
58+
- Reporting more events (suspend, resume, etc.) through
59+
USB_RAW_IOCTL_EVENT_FETCH.
60+
61+
- Support O_NONBLOCK I/O.

‎drivers/usb/gadget/legacy/Kconfig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,15 @@ config USB_G_WEBCAM
516516
Say "y" to link the driver statically, or "m" to build a
517517
dynamically linked module called "g_webcam".
518518

519+
config USB_RAW_GADGET
520+
tristate "USB Raw Gadget"
521+
help
522+
USB Raw Gadget is a kernel module that provides a userspace interface
523+
for the USB Gadget subsystem. Essentially it allows to emulate USB
524+
devices from userspace. See Documentation/usb/raw-gadget.rst for
525+
details.
526+
527+
Say "y" to link the driver statically, or "m" to build a
528+
dynamically linked module called "raw_gadget".
529+
519530
endchoice

‎drivers/usb/gadget/legacy/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o
4343
obj-$(CONFIG_USB_G_NCM) += g_ncm.o
4444
obj-$(CONFIG_USB_G_ACM_MS) += g_acm_ms.o
4545
obj-$(CONFIG_USB_GADGET_TARGET) += tcm_usb_gadget.o
46+
obj-$(CONFIG_USB_RAW_GADGET) += raw_gadget.o

0 commit comments

Comments
 (0)