gadget.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * <linux/usb/gadget.h>
  3. *
  4. * We call the USB code inside a Linux-based peripheral device a "gadget"
  5. * driver, except for the hardware-specific bus glue. One USB host can
  6. * master many USB gadgets, but the gadgets are only slaved to one host.
  7. *
  8. *
  9. * (C) Copyright 2002-2004 by David Brownell
  10. * All Rights Reserved.
  11. *
  12. * This software is licensed under the GNU GPL version 2.
  13. */
  14. #ifndef __LINUX_USB_GADGET_H
  15. #define __LINUX_USB_GADGET_H
  16. #include <linux/device.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/slab.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/types.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/usb/ch9.h>
  25. #define UDC_TRACE_STR_MAX 512
  26. struct usb_ep;
  27. /**
  28. * struct usb_request - describes one i/o request
  29. * @buf: Buffer used for data. Always provide this; some controllers
  30. * only use PIO, or don't use DMA for some endpoints.
  31. * @dma: DMA address corresponding to 'buf'. If you don't set this
  32. * field, and the usb controller needs one, it is responsible
  33. * for mapping and unmapping the buffer.
  34. * @sg: a scatterlist for SG-capable controllers.
  35. * @num_sgs: number of SG entries
  36. * @num_mapped_sgs: number of SG entries mapped to DMA (internal)
  37. * @length: Length of that data
  38. * @stream_id: The stream id, when USB3.0 bulk streams are being used
  39. * @no_interrupt: If true, hints that no completion irq is needed.
  40. * Helpful sometimes with deep request queues that are handled
  41. * directly by DMA controllers.
  42. * @zero: If true, when writing data, makes the last packet be "short"
  43. * by adding a zero length packet as needed;
  44. * @short_not_ok: When reading data, makes short packets be
  45. * treated as errors (queue stops advancing till cleanup).
  46. * @complete: Function called when request completes, so this request and
  47. * its buffer may be re-used. The function will always be called with
  48. * interrupts disabled, and it must not sleep.
  49. * Reads terminate with a short packet, or when the buffer fills,
  50. * whichever comes first. When writes terminate, some data bytes
  51. * will usually still be in flight (often in a hardware fifo).
  52. * Errors (for reads or writes) stop the queue from advancing
  53. * until the completion function returns, so that any transfers
  54. * invalidated by the error may first be dequeued.
  55. * @context: For use by the completion callback
  56. * @list: For use by the gadget driver.
  57. * @status: Reports completion code, zero or a negative errno.
  58. * Normally, faults block the transfer queue from advancing until
  59. * the completion callback returns.
  60. * Code "-ESHUTDOWN" indicates completion caused by device disconnect,
  61. * or when the driver disabled the endpoint.
  62. * @actual: Reports bytes transferred to/from the buffer. For reads (OUT
  63. * transfers) this may be less than the requested length. If the
  64. * short_not_ok flag is set, short reads are treated as errors
  65. * even when status otherwise indicates successful completion.
  66. * Note that for writes (IN transfers) some data bytes may still
  67. * reside in a device-side FIFO when the request is reported as
  68. * complete.
  69. *
  70. * These are allocated/freed through the endpoint they're used with. The
  71. * hardware's driver can add extra per-request data to the memory it returns,
  72. * which often avoids separate memory allocations (potential failures),
  73. * later when the request is queued.
  74. *
  75. * Request flags affect request handling, such as whether a zero length
  76. * packet is written (the "zero" flag), whether a short read should be
  77. * treated as an error (blocking request queue advance, the "short_not_ok"
  78. * flag), or hinting that an interrupt is not required (the "no_interrupt"
  79. * flag, for use with deep request queues).
  80. *
  81. * Bulk endpoints can use any size buffers, and can also be used for interrupt
  82. * transfers. interrupt-only endpoints can be much less functional.
  83. *
  84. * NOTE: this is analogous to 'struct urb' on the host side, except that
  85. * it's thinner and promotes more pre-allocation.
  86. */
  87. struct usb_request {
  88. void *buf;
  89. unsigned length;
  90. dma_addr_t dma;
  91. struct scatterlist *sg;
  92. unsigned num_sgs;
  93. unsigned num_mapped_sgs;
  94. unsigned stream_id:16;
  95. unsigned no_interrupt:1;
  96. unsigned zero:1;
  97. unsigned short_not_ok:1;
  98. void (*complete)(struct usb_ep *ep,
  99. struct usb_request *req);
  100. void *context;
  101. struct list_head list;
  102. int status;
  103. unsigned actual;
  104. };
  105. /*-------------------------------------------------------------------------*/
  106. /* endpoint-specific parts of the api to the usb controller hardware.
  107. * unlike the urb model, (de)multiplexing layers are not required.
  108. * (so this api could slash overhead if used on the host side...)
  109. *
  110. * note that device side usb controllers commonly differ in how many
  111. * endpoints they support, as well as their capabilities.
  112. */
  113. struct usb_ep_ops {
  114. int (*enable) (struct usb_ep *ep,
  115. const struct usb_endpoint_descriptor *desc);
  116. int (*disable) (struct usb_ep *ep);
  117. struct usb_request *(*alloc_request) (struct usb_ep *ep,
  118. gfp_t gfp_flags);
  119. void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  120. int (*queue) (struct usb_ep *ep, struct usb_request *req,
  121. gfp_t gfp_flags);
  122. int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  123. int (*set_halt) (struct usb_ep *ep, int value);
  124. int (*set_wedge) (struct usb_ep *ep);
  125. int (*fifo_status) (struct usb_ep *ep);
  126. void (*fifo_flush) (struct usb_ep *ep);
  127. };
  128. /**
  129. * struct usb_ep_caps - endpoint capabilities description
  130. * @type_control:Endpoint supports control type (reserved for ep0).
  131. * @type_iso:Endpoint supports isochronous transfers.
  132. * @type_bulk:Endpoint supports bulk transfers.
  133. * @type_int:Endpoint supports interrupt transfers.
  134. * @dir_in:Endpoint supports IN direction.
  135. * @dir_out:Endpoint supports OUT direction.
  136. */
  137. struct usb_ep_caps {
  138. unsigned type_control:1;
  139. unsigned type_iso:1;
  140. unsigned type_bulk:1;
  141. unsigned type_int:1;
  142. unsigned dir_in:1;
  143. unsigned dir_out:1;
  144. };
  145. #define USB_EP_CAPS_TYPE_CONTROL 0x01
  146. #define USB_EP_CAPS_TYPE_ISO 0x02
  147. #define USB_EP_CAPS_TYPE_BULK 0x04
  148. #define USB_EP_CAPS_TYPE_INT 0x08
  149. #define USB_EP_CAPS_TYPE_ALL \
  150. (USB_EP_CAPS_TYPE_ISO | USB_EP_CAPS_TYPE_BULK | USB_EP_CAPS_TYPE_INT)
  151. #define USB_EP_CAPS_DIR_IN 0x01
  152. #define USB_EP_CAPS_DIR_OUT 0x02
  153. #define USB_EP_CAPS_DIR_ALL (USB_EP_CAPS_DIR_IN | USB_EP_CAPS_DIR_OUT)
  154. #define USB_EP_CAPS(_type, _dir) \
  155. { \
  156. .type_control = !!(_type & USB_EP_CAPS_TYPE_CONTROL), \
  157. .type_iso = !!(_type & USB_EP_CAPS_TYPE_ISO), \
  158. .type_bulk = !!(_type & USB_EP_CAPS_TYPE_BULK), \
  159. .type_int = !!(_type & USB_EP_CAPS_TYPE_INT), \
  160. .dir_in = !!(_dir & USB_EP_CAPS_DIR_IN), \
  161. .dir_out = !!(_dir & USB_EP_CAPS_DIR_OUT), \
  162. }
  163. /**
  164. * struct usb_ep - device side representation of USB endpoint
  165. * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk"
  166. * @ops: Function pointers used to access hardware-specific operations.
  167. * @ep_list:the gadget's ep_list holds all of its endpoints
  168. * @caps:The structure describing types and directions supported by endoint.
  169. * @maxpacket:The maximum packet size used on this endpoint. The initial
  170. * value can sometimes be reduced (hardware allowing), according to
  171. * the endpoint descriptor used to configure the endpoint.
  172. * @maxpacket_limit:The maximum packet size value which can be handled by this
  173. * endpoint. It's set once by UDC driver when endpoint is initialized, and
  174. * should not be changed. Should not be confused with maxpacket.
  175. * @max_streams: The maximum number of streams supported
  176. * by this EP (0 - 16, actual number is 2^n)
  177. * @mult: multiplier, 'mult' value for SS Isoc EPs
  178. * @maxburst: the maximum number of bursts supported by this EP (for usb3)
  179. * @driver_data:for use by the gadget driver.
  180. * @address: used to identify the endpoint when finding descriptor that
  181. * matches connection speed
  182. * @desc: endpoint descriptor. This pointer is set before the endpoint is
  183. * enabled and remains valid until the endpoint is disabled.
  184. * @comp_desc: In case of SuperSpeed support, this is the endpoint companion
  185. * descriptor that is used to configure the endpoint
  186. *
  187. * the bus controller driver lists all the general purpose endpoints in
  188. * gadget->ep_list. the control endpoint (gadget->ep0) is not in that list,
  189. * and is accessed only in response to a driver setup() callback.
  190. */
  191. struct usb_ep {
  192. void *driver_data;
  193. const char *name;
  194. const struct usb_ep_ops *ops;
  195. struct list_head ep_list;
  196. struct usb_ep_caps caps;
  197. bool claimed;
  198. bool enabled;
  199. unsigned maxpacket:16;
  200. unsigned maxpacket_limit:16;
  201. unsigned max_streams:16;
  202. unsigned mult:2;
  203. unsigned maxburst:5;
  204. u8 address;
  205. const struct usb_endpoint_descriptor *desc;
  206. const struct usb_ss_ep_comp_descriptor *comp_desc;
  207. };
  208. /*-------------------------------------------------------------------------*/
  209. #if IS_ENABLED(CONFIG_USB_GADGET)
  210. void usb_ep_set_maxpacket_limit(struct usb_ep *ep, unsigned maxpacket_limit);
  211. int usb_ep_enable(struct usb_ep *ep);
  212. int usb_ep_disable(struct usb_ep *ep);
  213. struct usb_request *usb_ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
  214. void usb_ep_free_request(struct usb_ep *ep, struct usb_request *req);
  215. int usb_ep_queue(struct usb_ep *ep, struct usb_request *req, gfp_t gfp_flags);
  216. int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req);
  217. int usb_ep_set_halt(struct usb_ep *ep);
  218. int usb_ep_clear_halt(struct usb_ep *ep);
  219. int usb_ep_set_wedge(struct usb_ep *ep);
  220. int usb_ep_fifo_status(struct usb_ep *ep);
  221. void usb_ep_fifo_flush(struct usb_ep *ep);
  222. #else
  223. static inline void usb_ep_set_maxpacket_limit(struct usb_ep *ep,
  224. unsigned maxpacket_limit)
  225. { }
  226. static inline int usb_ep_enable(struct usb_ep *ep)
  227. { return 0; }
  228. static inline int usb_ep_disable(struct usb_ep *ep)
  229. { return 0; }
  230. static inline struct usb_request *usb_ep_alloc_request(struct usb_ep *ep,
  231. gfp_t gfp_flags)
  232. { return NULL; }
  233. static inline void usb_ep_free_request(struct usb_ep *ep,
  234. struct usb_request *req)
  235. { }
  236. static inline int usb_ep_queue(struct usb_ep *ep, struct usb_request *req,
  237. gfp_t gfp_flags)
  238. { return 0; }
  239. static inline int usb_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  240. { return 0; }
  241. static inline int usb_ep_set_halt(struct usb_ep *ep)
  242. { return 0; }
  243. static inline int usb_ep_clear_halt(struct usb_ep *ep)
  244. { return 0; }
  245. static inline int usb_ep_set_wedge(struct usb_ep *ep)
  246. { return 0; }
  247. static inline int usb_ep_fifo_status(struct usb_ep *ep)
  248. { return 0; }
  249. static inline void usb_ep_fifo_flush(struct usb_ep *ep)
  250. { }
  251. #endif /* USB_GADGET */
  252. /*-------------------------------------------------------------------------*/
  253. struct usb_dcd_config_params {
  254. __u8 bU1devExitLat; /* U1 Device exit Latency */
  255. #define USB_DEFAULT_U1_DEV_EXIT_LAT 0x01 /* Less then 1 microsec */
  256. __le16 bU2DevExitLat; /* U2 Device exit Latency */
  257. #define USB_DEFAULT_U2_DEV_EXIT_LAT 0x1F4 /* Less then 500 microsec */
  258. };
  259. struct usb_gadget;
  260. struct usb_gadget_driver;
  261. struct usb_udc;
  262. /* the rest of the api to the controller hardware: device operations,
  263. * which don't involve endpoints (or i/o).
  264. */
  265. struct usb_gadget_ops {
  266. int (*get_frame)(struct usb_gadget *);
  267. int (*wakeup)(struct usb_gadget *);
  268. int (*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
  269. int (*vbus_session) (struct usb_gadget *, int is_active);
  270. int (*vbus_draw) (struct usb_gadget *, unsigned mA);
  271. int (*pullup) (struct usb_gadget *, int is_on);
  272. int (*ioctl)(struct usb_gadget *,
  273. unsigned code, unsigned long param);
  274. void (*get_config_params)(struct usb_dcd_config_params *);
  275. int (*udc_start)(struct usb_gadget *,
  276. struct usb_gadget_driver *);
  277. int (*udc_stop)(struct usb_gadget *);
  278. struct usb_ep *(*match_ep)(struct usb_gadget *,
  279. struct usb_endpoint_descriptor *,
  280. struct usb_ss_ep_comp_descriptor *);
  281. };
  282. /**
  283. * struct usb_gadget - represents a usb slave device
  284. * @work: (internal use) Workqueue to be used for sysfs_notify()
  285. * @udc: struct usb_udc pointer for this gadget
  286. * @ops: Function pointers used to access hardware-specific operations.
  287. * @ep0: Endpoint zero, used when reading or writing responses to
  288. * driver setup() requests
  289. * @ep_list: List of other endpoints supported by the device.
  290. * @speed: Speed of current connection to USB host.
  291. * @max_speed: Maximal speed the UDC can handle. UDC must support this
  292. * and all slower speeds.
  293. * @state: the state we are now (attached, suspended, configured, etc)
  294. * @name: Identifies the controller hardware type. Used in diagnostics
  295. * and sometimes configuration.
  296. * @dev: Driver model state for this abstract device.
  297. * @out_epnum: last used out ep number
  298. * @in_epnum: last used in ep number
  299. * @mA: last set mA value
  300. * @otg_caps: OTG capabilities of this gadget.
  301. * @sg_supported: true if we can handle scatter-gather
  302. * @is_otg: True if the USB device port uses a Mini-AB jack, so that the
  303. * gadget driver must provide a USB OTG descriptor.
  304. * @is_a_peripheral: False unless is_otg, the "A" end of a USB cable
  305. * is in the Mini-AB jack, and HNP has been used to switch roles
  306. * so that the "A" device currently acts as A-Peripheral, not A-Host.
  307. * @a_hnp_support: OTG device feature flag, indicating that the A-Host
  308. * supports HNP at this port.
  309. * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host
  310. * only supports HNP on a different root port.
  311. * @b_hnp_enable: OTG device feature flag, indicating that the A-Host
  312. * enabled HNP support.
  313. * @hnp_polling_support: OTG device feature flag, indicating if the OTG device
  314. * in peripheral mode can support HNP polling.
  315. * @host_request_flag: OTG device feature flag, indicating if A-Peripheral
  316. * or B-Peripheral wants to take host role.
  317. * @quirk_ep_out_aligned_size: epout requires buffer size to be aligned to
  318. * MaxPacketSize.
  319. * @quirk_avoids_skb_reserve: udc/platform wants to avoid skb_reserve() in
  320. * u_ether.c to improve performance.
  321. * @is_selfpowered: if the gadget is self-powered.
  322. * @deactivated: True if gadget is deactivated - in deactivated state it cannot
  323. * be connected.
  324. * @connected: True if gadget is connected.
  325. *
  326. * Gadgets have a mostly-portable "gadget driver" implementing device
  327. * functions, handling all usb configurations and interfaces. Gadget
  328. * drivers talk to hardware-specific code indirectly, through ops vectors.
  329. * That insulates the gadget driver from hardware details, and packages
  330. * the hardware endpoints through generic i/o queues. The "usb_gadget"
  331. * and "usb_ep" interfaces provide that insulation from the hardware.
  332. *
  333. * Except for the driver data, all fields in this structure are
  334. * read-only to the gadget driver. That driver data is part of the
  335. * "driver model" infrastructure in 2.6 (and later) kernels, and for
  336. * earlier systems is grouped in a similar structure that's not known
  337. * to the rest of the kernel.
  338. *
  339. * Values of the three OTG device feature flags are updated before the
  340. * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before
  341. * driver suspend() calls. They are valid only when is_otg, and when the
  342. * device is acting as a B-Peripheral (so is_a_peripheral is false).
  343. */
  344. struct usb_gadget {
  345. struct work_struct work;
  346. struct usb_udc *udc;
  347. /* readonly to gadget driver */
  348. const struct usb_gadget_ops *ops;
  349. struct usb_ep *ep0;
  350. struct list_head ep_list; /* of usb_ep */
  351. enum usb_device_speed speed;
  352. enum usb_device_speed max_speed;
  353. enum usb_device_state state;
  354. const char *name;
  355. struct device dev;
  356. unsigned out_epnum;
  357. unsigned in_epnum;
  358. unsigned mA;
  359. struct usb_otg_caps *otg_caps;
  360. unsigned sg_supported:1;
  361. unsigned is_otg:1;
  362. unsigned is_a_peripheral:1;
  363. unsigned b_hnp_enable:1;
  364. unsigned a_hnp_support:1;
  365. unsigned a_alt_hnp_support:1;
  366. unsigned hnp_polling_support:1;
  367. unsigned host_request_flag:1;
  368. unsigned quirk_ep_out_aligned_size:1;
  369. unsigned quirk_altset_not_supp:1;
  370. unsigned quirk_stall_not_supp:1;
  371. unsigned quirk_zlp_not_supp:1;
  372. unsigned quirk_avoids_skb_reserve:1;
  373. unsigned is_selfpowered:1;
  374. unsigned deactivated:1;
  375. unsigned connected:1;
  376. };
  377. #define work_to_gadget(w) (container_of((w), struct usb_gadget, work))
  378. static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
  379. { dev_set_drvdata(&gadget->dev, data); }
  380. static inline void *get_gadget_data(struct usb_gadget *gadget)
  381. { return dev_get_drvdata(&gadget->dev); }
  382. static inline struct usb_gadget *dev_to_usb_gadget(struct device *dev)
  383. {
  384. return container_of(dev, struct usb_gadget, dev);
  385. }
  386. /* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */
  387. #define gadget_for_each_ep(tmp, gadget) \
  388. list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
  389. /**
  390. * usb_ep_align - returns @len aligned to ep's maxpacketsize.
  391. * @ep: the endpoint whose maxpacketsize is used to align @len
  392. * @len: buffer size's length to align to @ep's maxpacketsize
  393. *
  394. * This helper is used to align buffer's size to an ep's maxpacketsize.
  395. */
  396. static inline size_t usb_ep_align(struct usb_ep *ep, size_t len)
  397. {
  398. int max_packet_size = (size_t)usb_endpoint_maxp(ep->desc) & 0x7ff;
  399. return round_up(len, max_packet_size);
  400. }
  401. /**
  402. * usb_ep_align_maybe - returns @len aligned to ep's maxpacketsize if gadget
  403. * requires quirk_ep_out_aligned_size, otherwise returns len.
  404. * @g: controller to check for quirk
  405. * @ep: the endpoint whose maxpacketsize is used to align @len
  406. * @len: buffer size's length to align to @ep's maxpacketsize
  407. *
  408. * This helper is used in case it's required for any reason to check and maybe
  409. * align buffer's size to an ep's maxpacketsize.
  410. */
  411. static inline size_t
  412. usb_ep_align_maybe(struct usb_gadget *g, struct usb_ep *ep, size_t len)
  413. {
  414. return g->quirk_ep_out_aligned_size ? usb_ep_align(ep, len) : len;
  415. }
  416. /**
  417. * gadget_is_altset_supported - return true iff the hardware supports
  418. * altsettings
  419. * @g: controller to check for quirk
  420. */
  421. static inline int gadget_is_altset_supported(struct usb_gadget *g)
  422. {
  423. return !g->quirk_altset_not_supp;
  424. }
  425. /**
  426. * gadget_is_stall_supported - return true iff the hardware supports stalling
  427. * @g: controller to check for quirk
  428. */
  429. static inline int gadget_is_stall_supported(struct usb_gadget *g)
  430. {
  431. return !g->quirk_stall_not_supp;
  432. }
  433. /**
  434. * gadget_is_zlp_supported - return true iff the hardware supports zlp
  435. * @g: controller to check for quirk
  436. */
  437. static inline int gadget_is_zlp_supported(struct usb_gadget *g)
  438. {
  439. return !g->quirk_zlp_not_supp;
  440. }
  441. /**
  442. * gadget_avoids_skb_reserve - return true iff the hardware would like to avoid
  443. * skb_reserve to improve performance.
  444. * @g: controller to check for quirk
  445. */
  446. static inline int gadget_avoids_skb_reserve(struct usb_gadget *g)
  447. {
  448. return g->quirk_avoids_skb_reserve;
  449. }
  450. /**
  451. * gadget_is_dualspeed - return true iff the hardware handles high speed
  452. * @g: controller that might support both high and full speeds
  453. */
  454. static inline int gadget_is_dualspeed(struct usb_gadget *g)
  455. {
  456. return g->max_speed >= USB_SPEED_HIGH;
  457. }
  458. /**
  459. * gadget_is_superspeed() - return true if the hardware handles superspeed
  460. * @g: controller that might support superspeed
  461. */
  462. static inline int gadget_is_superspeed(struct usb_gadget *g)
  463. {
  464. return g->max_speed >= USB_SPEED_SUPER;
  465. }
  466. /**
  467. * gadget_is_superspeed_plus() - return true if the hardware handles
  468. * superspeed plus
  469. * @g: controller that might support superspeed plus
  470. */
  471. static inline int gadget_is_superspeed_plus(struct usb_gadget *g)
  472. {
  473. return g->max_speed >= USB_SPEED_SUPER_PLUS;
  474. }
  475. /**
  476. * gadget_is_otg - return true iff the hardware is OTG-ready
  477. * @g: controller that might have a Mini-AB connector
  478. *
  479. * This is a runtime test, since kernels with a USB-OTG stack sometimes
  480. * run on boards which only have a Mini-B (or Mini-A) connector.
  481. */
  482. static inline int gadget_is_otg(struct usb_gadget *g)
  483. {
  484. #ifdef CONFIG_USB_OTG
  485. return g->is_otg;
  486. #else
  487. return 0;
  488. #endif
  489. }
  490. /*-------------------------------------------------------------------------*/
  491. #if IS_ENABLED(CONFIG_USB_GADGET)
  492. int usb_gadget_frame_number(struct usb_gadget *gadget);
  493. int usb_gadget_wakeup(struct usb_gadget *gadget);
  494. int usb_gadget_set_selfpowered(struct usb_gadget *gadget);
  495. int usb_gadget_clear_selfpowered(struct usb_gadget *gadget);
  496. int usb_gadget_vbus_connect(struct usb_gadget *gadget);
  497. int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA);
  498. int usb_gadget_vbus_disconnect(struct usb_gadget *gadget);
  499. int usb_gadget_connect(struct usb_gadget *gadget);
  500. int usb_gadget_disconnect(struct usb_gadget *gadget);
  501. int usb_gadget_deactivate(struct usb_gadget *gadget);
  502. int usb_gadget_activate(struct usb_gadget *gadget);
  503. #else
  504. static inline int usb_gadget_frame_number(struct usb_gadget *gadget)
  505. { return 0; }
  506. static inline int usb_gadget_wakeup(struct usb_gadget *gadget)
  507. { return 0; }
  508. static inline int usb_gadget_set_selfpowered(struct usb_gadget *gadget)
  509. { return 0; }
  510. static inline int usb_gadget_clear_selfpowered(struct usb_gadget *gadget)
  511. { return 0; }
  512. static inline int usb_gadget_vbus_connect(struct usb_gadget *gadget)
  513. { return 0; }
  514. static inline int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
  515. { return 0; }
  516. static inline int usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
  517. { return 0; }
  518. static inline int usb_gadget_connect(struct usb_gadget *gadget)
  519. { return 0; }
  520. static inline int usb_gadget_disconnect(struct usb_gadget *gadget)
  521. { return 0; }
  522. static inline int usb_gadget_deactivate(struct usb_gadget *gadget)
  523. { return 0; }
  524. static inline int usb_gadget_activate(struct usb_gadget *gadget)
  525. { return 0; }
  526. #endif /* CONFIG_USB_GADGET */
  527. /*-------------------------------------------------------------------------*/
  528. /**
  529. * struct usb_gadget_driver - driver for usb 'slave' devices
  530. * @function: String describing the gadget's function
  531. * @max_speed: Highest speed the driver handles.
  532. * @setup: Invoked for ep0 control requests that aren't handled by
  533. * the hardware level driver. Most calls must be handled by
  534. * the gadget driver, including descriptor and configuration
  535. * management. The 16 bit members of the setup data are in
  536. * USB byte order. Called in_interrupt; this may not sleep. Driver
  537. * queues a response to ep0, or returns negative to stall.
  538. * @disconnect: Invoked after all transfers have been stopped,
  539. * when the host is disconnected. May be called in_interrupt; this
  540. * may not sleep. Some devices can't detect disconnect, so this might
  541. * not be called except as part of controller shutdown.
  542. * @bind: the driver's bind callback
  543. * @unbind: Invoked when the driver is unbound from a gadget,
  544. * usually from rmmod (after a disconnect is reported).
  545. * Called in a context that permits sleeping.
  546. * @suspend: Invoked on USB suspend. May be called in_interrupt.
  547. * @resume: Invoked on USB resume. May be called in_interrupt.
  548. * @reset: Invoked on USB bus reset. It is mandatory for all gadget drivers
  549. * and should be called in_interrupt.
  550. * @driver: Driver model state for this driver.
  551. * @udc_name: A name of UDC this driver should be bound to. If udc_name is NULL,
  552. * this driver will be bound to any available UDC.
  553. * @pending: UDC core private data used for deferred probe of this driver.
  554. * @match_existing_only: If udc is not found, return an error and don't add this
  555. * gadget driver to list of pending driver
  556. *
  557. * Devices are disabled till a gadget driver successfully bind()s, which
  558. * means the driver will handle setup() requests needed to enumerate (and
  559. * meet "chapter 9" requirements) then do some useful work.
  560. *
  561. * If gadget->is_otg is true, the gadget driver must provide an OTG
  562. * descriptor during enumeration, or else fail the bind() call. In such
  563. * cases, no USB traffic may flow until both bind() returns without
  564. * having called usb_gadget_disconnect(), and the USB host stack has
  565. * initialized.
  566. *
  567. * Drivers use hardware-specific knowledge to configure the usb hardware.
  568. * endpoint addressing is only one of several hardware characteristics that
  569. * are in descriptors the ep0 implementation returns from setup() calls.
  570. *
  571. * Except for ep0 implementation, most driver code shouldn't need change to
  572. * run on top of different usb controllers. It'll use endpoints set up by
  573. * that ep0 implementation.
  574. *
  575. * The usb controller driver handles a few standard usb requests. Those
  576. * include set_address, and feature flags for devices, interfaces, and
  577. * endpoints (the get_status, set_feature, and clear_feature requests).
  578. *
  579. * Accordingly, the driver's setup() callback must always implement all
  580. * get_descriptor requests, returning at least a device descriptor and
  581. * a configuration descriptor. Drivers must make sure the endpoint
  582. * descriptors match any hardware constraints. Some hardware also constrains
  583. * other descriptors. (The pxa250 allows only configurations 1, 2, or 3).
  584. *
  585. * The driver's setup() callback must also implement set_configuration,
  586. * and should also implement set_interface, get_configuration, and
  587. * get_interface. Setting a configuration (or interface) is where
  588. * endpoints should be activated or (config 0) shut down.
  589. *
  590. * (Note that only the default control endpoint is supported. Neither
  591. * hosts nor devices generally support control traffic except to ep0.)
  592. *
  593. * Most devices will ignore USB suspend/resume operations, and so will
  594. * not provide those callbacks. However, some may need to change modes
  595. * when the host is not longer directing those activities. For example,
  596. * local controls (buttons, dials, etc) may need to be re-enabled since
  597. * the (remote) host can't do that any longer; or an error state might
  598. * be cleared, to make the device behave identically whether or not
  599. * power is maintained.
  600. */
  601. struct usb_gadget_driver {
  602. char *function;
  603. enum usb_device_speed max_speed;
  604. int (*bind)(struct usb_gadget *gadget,
  605. struct usb_gadget_driver *driver);
  606. void (*unbind)(struct usb_gadget *);
  607. int (*setup)(struct usb_gadget *,
  608. const struct usb_ctrlrequest *);
  609. void (*disconnect)(struct usb_gadget *);
  610. void (*suspend)(struct usb_gadget *);
  611. void (*resume)(struct usb_gadget *);
  612. void (*reset)(struct usb_gadget *);
  613. /* FIXME support safe rmmod */
  614. struct device_driver driver;
  615. char *udc_name;
  616. struct list_head pending;
  617. unsigned match_existing_only:1;
  618. };
  619. /*-------------------------------------------------------------------------*/
  620. /* driver modules register and unregister, as usual.
  621. * these calls must be made in a context that can sleep.
  622. *
  623. * these will usually be implemented directly by the hardware-dependent
  624. * usb bus interface driver, which will only support a single driver.
  625. */
  626. /**
  627. * usb_gadget_probe_driver - probe a gadget driver
  628. * @driver: the driver being registered
  629. * Context: can sleep
  630. *
  631. * Call this in your gadget driver's module initialization function,
  632. * to tell the underlying usb controller driver about your driver.
  633. * The @bind() function will be called to bind it to a gadget before this
  634. * registration call returns. It's expected that the @bind() function will
  635. * be in init sections.
  636. */
  637. int usb_gadget_probe_driver(struct usb_gadget_driver *driver);
  638. /**
  639. * usb_gadget_unregister_driver - unregister a gadget driver
  640. * @driver:the driver being unregistered
  641. * Context: can sleep
  642. *
  643. * Call this in your gadget driver's module cleanup function,
  644. * to tell the underlying usb controller that your driver is
  645. * going away. If the controller is connected to a USB host,
  646. * it will first disconnect(). The driver is also requested
  647. * to unbind() and clean up any device state, before this procedure
  648. * finally returns. It's expected that the unbind() functions
  649. * will in in exit sections, so may not be linked in some kernels.
  650. */
  651. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver);
  652. extern int usb_add_gadget_udc_release(struct device *parent,
  653. struct usb_gadget *gadget, void (*release)(struct device *dev));
  654. extern int usb_add_gadget_udc(struct device *parent, struct usb_gadget *gadget);
  655. extern void usb_del_gadget_udc(struct usb_gadget *gadget);
  656. extern char *usb_get_gadget_udc_name(void);
  657. /*-------------------------------------------------------------------------*/
  658. /* utility to simplify dealing with string descriptors */
  659. /**
  660. * struct usb_string - wraps a C string and its USB id
  661. * @id:the (nonzero) ID for this string
  662. * @s:the string, in UTF-8 encoding
  663. *
  664. * If you're using usb_gadget_get_string(), use this to wrap a string
  665. * together with its ID.
  666. */
  667. struct usb_string {
  668. u8 id;
  669. const char *s;
  670. };
  671. /**
  672. * struct usb_gadget_strings - a set of USB strings in a given language
  673. * @language:identifies the strings' language (0x0409 for en-us)
  674. * @strings:array of strings with their ids
  675. *
  676. * If you're using usb_gadget_get_string(), use this to wrap all the
  677. * strings for a given language.
  678. */
  679. struct usb_gadget_strings {
  680. u16 language; /* 0x0409 for en-us */
  681. struct usb_string *strings;
  682. };
  683. struct usb_gadget_string_container {
  684. struct list_head list;
  685. u8 *stash[0];
  686. };
  687. /* put descriptor for string with that id into buf (buflen >= 256) */
  688. int usb_gadget_get_string(struct usb_gadget_strings *table, int id, u8 *buf);
  689. /*-------------------------------------------------------------------------*/
  690. /* utility to simplify managing config descriptors */
  691. /* write vector of descriptors into buffer */
  692. int usb_descriptor_fillbuf(void *, unsigned,
  693. const struct usb_descriptor_header **);
  694. /* build config descriptor from single descriptor vector */
  695. int usb_gadget_config_buf(const struct usb_config_descriptor *config,
  696. void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
  697. /* copy a NULL-terminated vector of descriptors */
  698. struct usb_descriptor_header **usb_copy_descriptors(
  699. struct usb_descriptor_header **);
  700. /**
  701. * usb_free_descriptors - free descriptors returned by usb_copy_descriptors()
  702. * @v: vector of descriptors
  703. */
  704. static inline void usb_free_descriptors(struct usb_descriptor_header **v)
  705. {
  706. kfree(v);
  707. }
  708. struct usb_function;
  709. int usb_assign_descriptors(struct usb_function *f,
  710. struct usb_descriptor_header **fs,
  711. struct usb_descriptor_header **hs,
  712. struct usb_descriptor_header **ss,
  713. struct usb_descriptor_header **ssp);
  714. void usb_free_all_descriptors(struct usb_function *f);
  715. struct usb_descriptor_header *usb_otg_descriptor_alloc(
  716. struct usb_gadget *gadget);
  717. int usb_otg_descriptor_init(struct usb_gadget *gadget,
  718. struct usb_descriptor_header *otg_desc);
  719. /*-------------------------------------------------------------------------*/
  720. /* utility to simplify map/unmap of usb_requests to/from DMA */
  721. extern int usb_gadget_map_request_by_dev(struct device *dev,
  722. struct usb_request *req, int is_in);
  723. extern int usb_gadget_map_request(struct usb_gadget *gadget,
  724. struct usb_request *req, int is_in);
  725. extern void usb_gadget_unmap_request_by_dev(struct device *dev,
  726. struct usb_request *req, int is_in);
  727. extern void usb_gadget_unmap_request(struct usb_gadget *gadget,
  728. struct usb_request *req, int is_in);
  729. /*-------------------------------------------------------------------------*/
  730. /* utility to set gadget state properly */
  731. extern void usb_gadget_set_state(struct usb_gadget *gadget,
  732. enum usb_device_state state);
  733. /*-------------------------------------------------------------------------*/
  734. /* utility to tell udc core that the bus reset occurs */
  735. extern void usb_gadget_udc_reset(struct usb_gadget *gadget,
  736. struct usb_gadget_driver *driver);
  737. /*-------------------------------------------------------------------------*/
  738. /* utility to give requests back to the gadget layer */
  739. extern void usb_gadget_giveback_request(struct usb_ep *ep,
  740. struct usb_request *req);
  741. /*-------------------------------------------------------------------------*/
  742. /* utility to find endpoint by name */
  743. extern struct usb_ep *gadget_find_ep_by_name(struct usb_gadget *g,
  744. const char *name);
  745. /*-------------------------------------------------------------------------*/
  746. /* utility to check if endpoint caps match descriptor needs */
  747. extern int usb_gadget_ep_match_desc(struct usb_gadget *gadget,
  748. struct usb_ep *ep, struct usb_endpoint_descriptor *desc,
  749. struct usb_ss_ep_comp_descriptor *ep_comp);
  750. /*-------------------------------------------------------------------------*/
  751. /* utility to update vbus status for udc core, it may be scheduled */
  752. extern void usb_udc_vbus_handler(struct usb_gadget *gadget, bool status);
  753. /*-------------------------------------------------------------------------*/
  754. /* utility wrapping a simple endpoint selection policy */
  755. extern struct usb_ep *usb_ep_autoconfig(struct usb_gadget *,
  756. struct usb_endpoint_descriptor *);
  757. extern struct usb_ep *usb_ep_autoconfig_ss(struct usb_gadget *,
  758. struct usb_endpoint_descriptor *,
  759. struct usb_ss_ep_comp_descriptor *);
  760. extern void usb_ep_autoconfig_release(struct usb_ep *);
  761. extern void usb_ep_autoconfig_reset(struct usb_gadget *);
  762. #endif /* __LINUX_USB_GADGET_H */