raw-gadget.rst 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ==============
  2. USB Raw Gadget
  3. ==============
  4. USB Raw Gadget is a gadget driver that gives userspace low-level control over
  5. the gadget's communication process.
  6. Like any other gadget driver, Raw Gadget implements USB devices via the
  7. USB gadget API. Unlike most gadget drivers, Raw Gadget does not implement
  8. any concrete USB functions itself but requires userspace to do that.
  9. Raw Gadget is currently a strictly debugging feature and should not be used
  10. in production. Use GadgetFS instead.
  11. Enabled with CONFIG_USB_RAW_GADGET.
  12. Comparison to GadgetFS
  13. ~~~~~~~~~~~~~~~~~~~~~~
  14. Raw Gadget is similar to GadgetFS but provides more direct access to the
  15. USB gadget layer for userspace. The key differences are:
  16. 1. Raw Gadget passes every USB request to userspace to get a response, while
  17. GadgetFS responds to some USB requests internally based on the provided
  18. descriptors. Note that the UDC driver might respond to some requests on
  19. its own and never forward them to the gadget layer.
  20. 2. Raw Gadget allows providing arbitrary data as responses to USB requests,
  21. while GadgetFS performs sanity checks on the provided USB descriptors.
  22. This makes Raw Gadget suitable for fuzzing by providing malformed data as
  23. responses to USB requests.
  24. 3. Raw Gadget provides a way to select a UDC device/driver to bind to,
  25. while GadgetFS currently binds to the first available UDC. This allows
  26. having multiple Raw Gadget instances bound to different UDCs.
  27. 4. Raw Gadget explicitly exposes information about endpoints addresses and
  28. capabilities. This allows the user to write UDC-agnostic gadgets.
  29. 5. Raw Gadget has an ioctl-based interface instead of a filesystem-based
  30. one.
  31. Userspace interface
  32. ~~~~~~~~~~~~~~~~~~~
  33. The user can interact with Raw Gadget by opening ``/dev/raw-gadget`` and
  34. issuing ioctl calls; see the comments in include/uapi/linux/usb/raw_gadget.h
  35. for details. Multiple Raw Gadget instances (bound to different UDCs) can be
  36. used at the same time.
  37. A typical usage scenario of Raw Gadget:
  38. 1. Create a Raw Gadget instance by opening ``/dev/raw-gadget``.
  39. 2. Initialize the instance via ``USB_RAW_IOCTL_INIT``.
  40. 3. Launch the instance with ``USB_RAW_IOCTL_RUN``.
  41. 4. In a loop issue ``USB_RAW_IOCTL_EVENT_FETCH`` to receive events from
  42. Raw Gadget and react to those depending on what kind of USB gadget must
  43. be implemented.
  44. Note that some UDC drivers have fixed addresses assigned to endpoints, and
  45. therefore arbitrary endpoint addresses cannot be used in the descriptors.
  46. Nevertheless, Raw Gadget provides a UDC-agnostic way to write USB gadgets.
  47. Once ``USB_RAW_EVENT_CONNECT`` is received via ``USB_RAW_IOCTL_EVENT_FETCH``,
  48. ``USB_RAW_IOCTL_EPS_INFO`` can be used to find out information about the
  49. endpoints that the UDC driver has. Based on that, userspace must choose UDC
  50. endpoints for the gadget and assign addresses in the endpoint descriptors
  51. correspondingly.
  52. Raw Gadget usage examples and a test suite:
  53. https://github.com/xairy/raw-gadget
  54. Internal details
  55. ~~~~~~~~~~~~~~~~
  56. Every Raw Gadget endpoint read/write ioctl submits a USB request and waits
  57. until its completion. This is done deliberately to assist with coverage-guided
  58. fuzzing by having a single syscall fully process a single USB request. This
  59. feature must be kept in the implementation.
  60. Potential future improvements
  61. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. - Report more events (suspend, resume, etc.) through
  63. ``USB_RAW_IOCTL_EVENT_FETCH``.
  64. - Support ``O_NONBLOCK`` I/O. This would be another mode of operation, where
  65. Raw Gadget would not wait until the completion of each USB request.
  66. - Support USB 3 features (accept SS endpoint companion descriptor when
  67. enabling endpoints; allow providing ``stream_id`` for bulk transfers).
  68. - Support ISO transfer features (expose ``frame_number`` for completed
  69. requests).