raw_gadget.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /*
  3. * USB Raw Gadget driver.
  4. *
  5. * See Documentation/usb/raw-gadget.rst for more details.
  6. */
  7. #ifndef _UAPI__LINUX_USB_RAW_GADGET_H
  8. #define _UAPI__LINUX_USB_RAW_GADGET_H
  9. #include <asm/ioctl.h>
  10. #include <linux/types.h>
  11. #include <linux/usb/ch9.h>
  12. /* Maximum length of driver_name/device_name in the usb_raw_init struct. */
  13. #define UDC_NAME_LENGTH_MAX 128
  14. /*
  15. * struct usb_raw_init - argument for USB_RAW_IOCTL_INIT ioctl.
  16. * @speed: The speed of the emulated USB device, takes the same values as
  17. * the usb_device_speed enum: USB_SPEED_FULL, USB_SPEED_HIGH, etc.
  18. * @driver_name: The name of the UDC driver.
  19. * @device_name: The name of a UDC instance.
  20. *
  21. * The last two fields identify a UDC the gadget driver should bind to.
  22. * For example, Dummy UDC has "dummy_udc" as its driver_name and "dummy_udc.N"
  23. * as its device_name, where N in the index of the Dummy UDC instance.
  24. * At the same time the dwc2 driver that is used on Raspberry Pi Zero, has
  25. * "20980000.usb" as both driver_name and device_name.
  26. */
  27. struct usb_raw_init {
  28. __u8 driver_name[UDC_NAME_LENGTH_MAX];
  29. __u8 device_name[UDC_NAME_LENGTH_MAX];
  30. __u8 speed;
  31. };
  32. /* The type of event fetched with the USB_RAW_IOCTL_EVENT_FETCH ioctl. */
  33. enum usb_raw_event_type {
  34. USB_RAW_EVENT_INVALID = 0,
  35. /* This event is queued when the driver has bound to a UDC. */
  36. USB_RAW_EVENT_CONNECT = 1,
  37. /* This event is queued when a new control request arrived to ep0. */
  38. USB_RAW_EVENT_CONTROL = 2,
  39. /* The list might grow in the future. */
  40. };
  41. /*
  42. * struct usb_raw_event - argument for USB_RAW_IOCTL_EVENT_FETCH ioctl.
  43. * @type: The type of the fetched event.
  44. * @length: Length of the data buffer. Updated by the driver and set to the
  45. * actual length of the fetched event data.
  46. * @data: A buffer to store the fetched event data.
  47. *
  48. * Currently the fetched data buffer is empty for USB_RAW_EVENT_CONNECT,
  49. * and contains struct usb_ctrlrequest for USB_RAW_EVENT_CONTROL.
  50. */
  51. struct usb_raw_event {
  52. __u32 type;
  53. __u32 length;
  54. __u8 data[];
  55. };
  56. #define USB_RAW_IO_FLAGS_ZERO 0x0001
  57. #define USB_RAW_IO_FLAGS_MASK 0x0001
  58. static inline int usb_raw_io_flags_valid(__u16 flags)
  59. {
  60. return (flags & ~USB_RAW_IO_FLAGS_MASK) == 0;
  61. }
  62. static inline int usb_raw_io_flags_zero(__u16 flags)
  63. {
  64. return (flags & USB_RAW_IO_FLAGS_ZERO);
  65. }
  66. /*
  67. * struct usb_raw_ep_io - argument for USB_RAW_IOCTL_EP0/EP_WRITE/READ ioctls.
  68. * @ep: Endpoint handle as returned by USB_RAW_IOCTL_EP_ENABLE for
  69. * USB_RAW_IOCTL_EP_WRITE/READ. Ignored for USB_RAW_IOCTL_EP0_WRITE/READ.
  70. * @flags: When USB_RAW_IO_FLAGS_ZERO is specified, the zero flag is set on
  71. * the submitted USB request, see include/linux/usb/gadget.h for details.
  72. * @length: Length of data.
  73. * @data: Data to send for USB_RAW_IOCTL_EP0/EP_WRITE. Buffer to store received
  74. * data for USB_RAW_IOCTL_EP0/EP_READ.
  75. */
  76. struct usb_raw_ep_io {
  77. __u16 ep;
  78. __u16 flags;
  79. __u32 length;
  80. __u8 data[];
  81. };
  82. /* Maximum number of non-control endpoints in struct usb_raw_eps_info. */
  83. #define USB_RAW_EPS_NUM_MAX 30
  84. /* Maximum length of UDC endpoint name in struct usb_raw_ep_info. */
  85. #define USB_RAW_EP_NAME_MAX 16
  86. /* Used as addr in struct usb_raw_ep_info if endpoint accepts any address. */
  87. #define USB_RAW_EP_ADDR_ANY 0xff
  88. /*
  89. * struct usb_raw_ep_caps - exposes endpoint capabilities from struct usb_ep
  90. * (technically from its member struct usb_ep_caps).
  91. */
  92. struct usb_raw_ep_caps {
  93. __u32 type_control : 1;
  94. __u32 type_iso : 1;
  95. __u32 type_bulk : 1;
  96. __u32 type_int : 1;
  97. __u32 dir_in : 1;
  98. __u32 dir_out : 1;
  99. };
  100. /*
  101. * struct usb_raw_ep_limits - exposes endpoint limits from struct usb_ep.
  102. * @maxpacket_limit: Maximum packet size value supported by this endpoint.
  103. * @max_streams: maximum number of streams supported by this endpoint
  104. * (actual number is 2^n).
  105. * @reserved: Empty, reserved for potential future extensions.
  106. */
  107. struct usb_raw_ep_limits {
  108. __u16 maxpacket_limit;
  109. __u16 max_streams;
  110. __u32 reserved;
  111. };
  112. /*
  113. * struct usb_raw_ep_info - stores information about a gadget endpoint.
  114. * @name: Name of the endpoint as it is defined in the UDC driver.
  115. * @addr: Address of the endpoint that must be specified in the endpoint
  116. * descriptor passed to USB_RAW_IOCTL_EP_ENABLE ioctl.
  117. * @caps: Endpoint capabilities.
  118. * @limits: Endpoint limits.
  119. */
  120. struct usb_raw_ep_info {
  121. __u8 name[USB_RAW_EP_NAME_MAX];
  122. __u32 addr;
  123. struct usb_raw_ep_caps caps;
  124. struct usb_raw_ep_limits limits;
  125. };
  126. /*
  127. * struct usb_raw_eps_info - argument for USB_RAW_IOCTL_EPS_INFO ioctl.
  128. * eps: Structures that store information about non-control endpoints.
  129. */
  130. struct usb_raw_eps_info {
  131. struct usb_raw_ep_info eps[USB_RAW_EPS_NUM_MAX];
  132. };
  133. /*
  134. * Initializes a Raw Gadget instance.
  135. * Accepts a pointer to the usb_raw_init struct as an argument.
  136. * Returns 0 on success or negative error code on failure.
  137. */
  138. #define USB_RAW_IOCTL_INIT _IOW('U', 0, struct usb_raw_init)
  139. /*
  140. * Instructs Raw Gadget to bind to a UDC and start emulating a USB device.
  141. * Returns 0 on success or negative error code on failure.
  142. */
  143. #define USB_RAW_IOCTL_RUN _IO('U', 1)
  144. /*
  145. * A blocking ioctl that waits for an event and returns fetched event data to
  146. * the user.
  147. * Accepts a pointer to the usb_raw_event struct.
  148. * Returns 0 on success or negative error code on failure.
  149. */
  150. #define USB_RAW_IOCTL_EVENT_FETCH _IOR('U', 2, struct usb_raw_event)
  151. /*
  152. * Queues an IN (OUT for READ) request as a response to the last setup request
  153. * received on endpoint 0 (provided that was an IN (OUT for READ) request), and
  154. * waits until the request is completed. Copies received data to user for READ.
  155. * Accepts a pointer to the usb_raw_ep_io struct as an argument.
  156. * Returns length of transferred data on success or negative error code on
  157. * failure.
  158. */
  159. #define USB_RAW_IOCTL_EP0_WRITE _IOW('U', 3, struct usb_raw_ep_io)
  160. #define USB_RAW_IOCTL_EP0_READ _IOWR('U', 4, struct usb_raw_ep_io)
  161. /*
  162. * Finds an endpoint that satisfies the parameters specified in the provided
  163. * descriptors (address, transfer type, etc.) and enables it.
  164. * Accepts a pointer to the usb_raw_ep_descs struct as an argument.
  165. * Returns enabled endpoint handle on success or negative error code on failure.
  166. */
  167. #define USB_RAW_IOCTL_EP_ENABLE _IOW('U', 5, struct usb_endpoint_descriptor)
  168. /*
  169. * Disables specified endpoint.
  170. * Accepts endpoint handle as an argument.
  171. * Returns 0 on success or negative error code on failure.
  172. */
  173. #define USB_RAW_IOCTL_EP_DISABLE _IOW('U', 6, __u32)
  174. /*
  175. * Queues an IN (OUT for READ) request as a response to the last setup request
  176. * received on endpoint usb_raw_ep_io.ep (provided that was an IN (OUT for READ)
  177. * request), and waits until the request is completed. Copies received data to
  178. * user for READ.
  179. * Accepts a pointer to the usb_raw_ep_io struct as an argument.
  180. * Returns length of transferred data on success or negative error code on
  181. * failure.
  182. */
  183. #define USB_RAW_IOCTL_EP_WRITE _IOW('U', 7, struct usb_raw_ep_io)
  184. #define USB_RAW_IOCTL_EP_READ _IOWR('U', 8, struct usb_raw_ep_io)
  185. /*
  186. * Switches the gadget into the configured state.
  187. * Returns 0 on success or negative error code on failure.
  188. */
  189. #define USB_RAW_IOCTL_CONFIGURE _IO('U', 9)
  190. /*
  191. * Constrains UDC VBUS power usage.
  192. * Accepts current limit in 2 mA units as an argument.
  193. * Returns 0 on success or negative error code on failure.
  194. */
  195. #define USB_RAW_IOCTL_VBUS_DRAW _IOW('U', 10, __u32)
  196. /*
  197. * Fills in the usb_raw_eps_info structure with information about non-control
  198. * endpoints available for the currently connected UDC.
  199. * Returns the number of available endpoints on success or negative error code
  200. * on failure.
  201. */
  202. #define USB_RAW_IOCTL_EPS_INFO _IOR('U', 11, struct usb_raw_eps_info)
  203. /*
  204. * Stalls a pending control request on endpoint 0.
  205. * Returns 0 on success or negative error code on failure.
  206. */
  207. #define USB_RAW_IOCTL_EP0_STALL _IO('U', 12)
  208. /*
  209. * Sets or clears halt or wedge status of the endpoint.
  210. * Accepts endpoint handle as an argument.
  211. * Returns 0 on success or negative error code on failure.
  212. */
  213. #define USB_RAW_IOCTL_EP_SET_HALT _IOW('U', 13, __u32)
  214. #define USB_RAW_IOCTL_EP_CLEAR_HALT _IOW('U', 14, __u32)
  215. #define USB_RAW_IOCTL_EP_SET_WEDGE _IOW('U', 15, __u32)
  216. #endif /* _UAPI__LINUX_USB_RAW_GADGET_H */