gadget.h 36 KB

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