raw-gadget.rst 3.4 KB

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