composite.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * composite.h -- framework for usb gadgets which are composite devices
  3. *
  4. * Copyright (C) 2006-2008 David Brownell
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #ifndef __LINUX_USB_COMPOSITE_H
  9. #define __LINUX_USB_COMPOSITE_H
  10. /*
  11. * This framework is an optional layer on top of the USB Gadget interface,
  12. * making it easier to build (a) Composite devices, supporting multiple
  13. * functions within any single configuration, and (b) Multi-configuration
  14. * devices, also supporting multiple functions but without necessarily
  15. * having more than one function per configuration.
  16. *
  17. * Example: a device with a single configuration supporting both network
  18. * link and mass storage functions is a composite device. Those functions
  19. * might alternatively be packaged in individual configurations, but in
  20. * the composite model the host can use both functions at the same time.
  21. */
  22. #include <common.h>
  23. #include <linux/usb/ch9.h>
  24. #include <linux/usb/gadget.h>
  25. #include <usb/lin_gadget_compat.h>
  26. /*
  27. * USB function drivers should return USB_GADGET_DELAYED_STATUS if they
  28. * wish to delay the data/status stages of the control transfer till they
  29. * are ready. The control transfer will then be kept from completing till
  30. * all the function drivers that requested for USB_GADGET_DELAYED_STAUS
  31. * invoke usb_composite_setup_continue().
  32. */
  33. #define USB_GADGET_DELAYED_STATUS 0x7fff /* Impossibly large value */
  34. struct usb_configuration;
  35. /**
  36. * struct usb_function - describes one function of a configuration
  37. * @name: For diagnostics, identifies the function.
  38. * @strings: tables of strings, keyed by identifiers assigned during bind()
  39. * and by language IDs provided in control requests
  40. * @descriptors: Table of full (or low) speed descriptors, using interface and
  41. * string identifiers assigned during @bind(). If this pointer is null,
  42. * the function will not be available at full speed (or at low speed).
  43. * @hs_descriptors: Table of high speed descriptors, using interface and
  44. * string identifiers assigned during @bind(). If this pointer is null,
  45. * the function will not be available at high speed.
  46. * @config: assigned when @usb_add_function() is called; this is the
  47. * configuration with which this function is associated.
  48. * @bind: Before the gadget can register, all of its functions bind() to the
  49. * available resources including string and interface identifiers used
  50. * in interface or class descriptors; endpoints; I/O buffers; and so on.
  51. * @unbind: Reverses @bind; called as a side effect of unregistering the
  52. * driver which added this function.
  53. * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
  54. * initialize usb_ep.driver data at this time (when it is used).
  55. * Note that setting an interface to its current altsetting resets
  56. * interface state, and that all interfaces have a disabled state.
  57. * @get_alt: Returns the active altsetting. If this is not provided,
  58. * then only altsetting zero is supported.
  59. * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
  60. * include host resetting or reconfiguring the gadget, and disconnection.
  61. * @setup: Used for interface-specific control requests.
  62. * @suspend: Notifies functions when the host stops sending USB traffic.
  63. * @resume: Notifies functions when the host restarts USB traffic.
  64. *
  65. * A single USB function uses one or more interfaces, and should in most
  66. * cases support operation at both full and high speeds. Each function is
  67. * associated by @usb_add_function() with a one configuration; that function
  68. * causes @bind() to be called so resources can be allocated as part of
  69. * setting up a gadget driver. Those resources include endpoints, which
  70. * should be allocated using @usb_ep_autoconfig().
  71. *
  72. * To support dual speed operation, a function driver provides descriptors
  73. * for both high and full speed operation. Except in rare cases that don't
  74. * involve bulk endpoints, each speed needs different endpoint descriptors.
  75. *
  76. * Function drivers choose their own strategies for managing instance data.
  77. * The simplest strategy just declares it "static', which means the function
  78. * can only be activated once. If the function needs to be exposed in more
  79. * than one configuration at a given speed, it needs to support multiple
  80. * usb_function structures (one for each configuration).
  81. *
  82. * A more complex strategy might encapsulate a @usb_function structure inside
  83. * a driver-specific instance structure to allows multiple activations. An
  84. * example of multiple activations might be a CDC ACM function that supports
  85. * two or more distinct instances within the same configuration, providing
  86. * several independent logical data links to a USB host.
  87. */
  88. struct usb_function {
  89. const char *name;
  90. struct usb_gadget_strings **strings;
  91. struct usb_descriptor_header **descriptors;
  92. struct usb_descriptor_header **hs_descriptors;
  93. struct usb_configuration *config;
  94. /* REVISIT: bind() functions can be marked __init, which
  95. * makes trouble for section mismatch analysis. See if
  96. * we can't restructure things to avoid mismatching.
  97. * Related: unbind() may kfree() but bind() won't...
  98. */
  99. /* configuration management: bind/unbind */
  100. int (*bind)(struct usb_configuration *,
  101. struct usb_function *);
  102. void (*unbind)(struct usb_configuration *,
  103. struct usb_function *);
  104. /* runtime state management */
  105. int (*set_alt)(struct usb_function *,
  106. unsigned interface, unsigned alt);
  107. int (*get_alt)(struct usb_function *,
  108. unsigned interface);
  109. void (*disable)(struct usb_function *);
  110. int (*setup)(struct usb_function *,
  111. const struct usb_ctrlrequest *);
  112. void (*suspend)(struct usb_function *);
  113. void (*resume)(struct usb_function *);
  114. /* private: */
  115. /* internals */
  116. struct list_head list;
  117. DECLARE_BITMAP(endpoints, 32);
  118. };
  119. int usb_add_function(struct usb_configuration *, struct usb_function *);
  120. int usb_function_deactivate(struct usb_function *);
  121. int usb_function_activate(struct usb_function *);
  122. int usb_interface_id(struct usb_configuration *, struct usb_function *);
  123. /**
  124. * ep_choose - select descriptor endpoint at current device speed
  125. * @g: gadget, connected and running at some speed
  126. * @hs: descriptor to use for high speed operation
  127. * @fs: descriptor to use for full or low speed operation
  128. */
  129. static inline struct usb_endpoint_descriptor *
  130. ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
  131. struct usb_endpoint_descriptor *fs)
  132. {
  133. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  134. return hs;
  135. return fs;
  136. }
  137. #define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
  138. /**
  139. * struct usb_configuration - represents one gadget configuration
  140. * @label: For diagnostics, describes the configuration.
  141. * @strings: Tables of strings, keyed by identifiers assigned during @bind()
  142. * and by language IDs provided in control requests.
  143. * @descriptors: Table of descriptors preceding all function descriptors.
  144. * Examples include OTG and vendor-specific descriptors.
  145. * @bind: Called from @usb_add_config() to allocate resources unique to this
  146. * configuration and to call @usb_add_function() for each function used.
  147. * @unbind: Reverses @bind; called as a side effect of unregistering the
  148. * driver which added this configuration.
  149. * @setup: Used to delegate control requests that aren't handled by standard
  150. * device infrastructure or directed at a specific interface.
  151. * @bConfigurationValue: Copied into configuration descriptor.
  152. * @iConfiguration: Copied into configuration descriptor.
  153. * @bmAttributes: Copied into configuration descriptor.
  154. * @bMaxPower: Copied into configuration descriptor.
  155. * @cdev: assigned by @usb_add_config() before calling @bind(); this is
  156. * the device associated with this configuration.
  157. *
  158. * Configurations are building blocks for gadget drivers structured around
  159. * function drivers. Simple USB gadgets require only one function and one
  160. * configuration, and handle dual-speed hardware by always providing the same
  161. * functionality. Slightly more complex gadgets may have more than one
  162. * single-function configuration at a given speed; or have configurations
  163. * that only work at one speed.
  164. *
  165. * Composite devices are, by definition, ones with configurations which
  166. * include more than one function.
  167. *
  168. * The lifecycle of a usb_configuration includes allocation, initialization
  169. * of the fields described above, and calling @usb_add_config() to set up
  170. * internal data and bind it to a specific device. The configuration's
  171. * @bind() method is then used to initialize all the functions and then
  172. * call @usb_add_function() for them.
  173. *
  174. * Those functions would normally be independant of each other, but that's
  175. * not mandatory. CDC WMC devices are an example where functions often
  176. * depend on other functions, with some functions subsidiary to others.
  177. * Such interdependency may be managed in any way, so long as all of the
  178. * descriptors complete by the time the composite driver returns from
  179. * its bind() routine.
  180. */
  181. struct usb_configuration {
  182. const char *label;
  183. struct usb_gadget_strings **strings;
  184. const struct usb_descriptor_header **descriptors;
  185. /* REVISIT: bind() functions can be marked __init, which
  186. * makes trouble for section mismatch analysis. See if
  187. * we can't restructure things to avoid mismatching...
  188. */
  189. /* configuration management: bind/unbind */
  190. int (*bind)(struct usb_configuration *);
  191. void (*unbind)(struct usb_configuration *);
  192. int (*setup)(struct usb_configuration *,
  193. const struct usb_ctrlrequest *);
  194. /* fields in the config descriptor */
  195. u8 bConfigurationValue;
  196. u8 iConfiguration;
  197. u8 bmAttributes;
  198. u8 bMaxPower;
  199. struct usb_composite_dev *cdev;
  200. /* private: */
  201. /* internals */
  202. struct list_head list;
  203. struct list_head functions;
  204. u8 next_interface_id;
  205. unsigned highspeed:1;
  206. unsigned fullspeed:1;
  207. struct usb_function *interface[MAX_CONFIG_INTERFACES];
  208. };
  209. int usb_add_config(struct usb_composite_dev *,
  210. struct usb_configuration *);
  211. /**
  212. * struct usb_composite_driver - groups configurations into a gadget
  213. * @name: For diagnostics, identifies the driver.
  214. * @dev: Template descriptor for the device, including default device
  215. * identifiers.
  216. * @strings: tables of strings, keyed by identifiers assigned during bind()
  217. * and language IDs provided in control requests
  218. * @bind: (REQUIRED) Used to allocate resources that are shared across the
  219. * whole device, such as string IDs, and add its configurations using
  220. * @usb_add_config(). This may fail by returning a negative errno
  221. * value; it should return zero on successful initialization.
  222. * @unbind: Reverses @bind(); called as a side effect of unregistering
  223. * this driver.
  224. * @disconnect: optional driver disconnect method
  225. * @suspend: Notifies when the host stops sending USB traffic,
  226. * after function notifications
  227. * @resume: Notifies configuration when the host restarts USB traffic,
  228. * before function notifications
  229. *
  230. * Devices default to reporting self powered operation. Devices which rely
  231. * on bus powered operation should report this in their @bind() method.
  232. *
  233. * Before returning from @bind, various fields in the template descriptor
  234. * may be overridden. These include the idVendor/idProduct/bcdDevice values
  235. * normally to bind the appropriate host side driver, and the three strings
  236. * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
  237. * meaningful device identifiers. (The strings will not be defined unless
  238. * they are defined in @dev and @strings.) The correct ep0 maxpacket size
  239. * is also reported, as defined by the underlying controller driver.
  240. */
  241. struct usb_composite_driver {
  242. const char *name;
  243. const struct usb_device_descriptor *dev;
  244. struct usb_gadget_strings **strings;
  245. /* REVISIT: bind() functions can be marked __init, which
  246. * makes trouble for section mismatch analysis. See if
  247. * we can't restructure things to avoid mismatching...
  248. */
  249. int (*bind)(struct usb_composite_dev *);
  250. int (*unbind)(struct usb_composite_dev *);
  251. void (*disconnect)(struct usb_composite_dev *);
  252. /* global suspend hooks */
  253. void (*suspend)(struct usb_composite_dev *);
  254. void (*resume)(struct usb_composite_dev *);
  255. };
  256. extern int usb_composite_register(struct usb_composite_driver *);
  257. extern void usb_composite_unregister(struct usb_composite_driver *);
  258. /**
  259. * struct usb_composite_device - represents one composite usb gadget
  260. * @gadget: read-only, abstracts the gadget's usb peripheral controller
  261. * @req: used for control responses; buffer is pre-allocated
  262. * @bufsiz: size of buffer pre-allocated in @req
  263. * @config: the currently active configuration
  264. *
  265. * One of these devices is allocated and initialized before the
  266. * associated device driver's bind() is called.
  267. *
  268. * OPEN ISSUE: it appears that some WUSB devices will need to be
  269. * built by combining a normal (wired) gadget with a wireless one.
  270. * This revision of the gadget framework should probably try to make
  271. * sure doing that won't hurt too much.
  272. *
  273. * One notion for how to handle Wireless USB devices involves:
  274. * (a) a second gadget here, discovery mechanism TBD, but likely
  275. * needing separate "register/unregister WUSB gadget" calls;
  276. * (b) updates to usb_gadget to include flags "is it wireless",
  277. * "is it wired", plus (presumably in a wrapper structure)
  278. * bandgroup and PHY info;
  279. * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
  280. * wireless-specific parameters like maxburst and maxsequence;
  281. * (d) configurations that are specific to wireless links;
  282. * (e) function drivers that understand wireless configs and will
  283. * support wireless for (additional) function instances;
  284. * (f) a function to support association setup (like CBAF), not
  285. * necessarily requiring a wireless adapter;
  286. * (g) composite device setup that can create one or more wireless
  287. * configs, including appropriate association setup support;
  288. * (h) more, TBD.
  289. */
  290. struct usb_composite_dev {
  291. struct usb_gadget *gadget;
  292. struct usb_request *req;
  293. unsigned bufsiz;
  294. struct usb_configuration *config;
  295. /* private: */
  296. /* internals */
  297. unsigned int suspended:1;
  298. struct usb_device_descriptor __aligned(CONFIG_SYS_CACHELINE_SIZE) desc;
  299. struct list_head configs;
  300. struct usb_composite_driver *driver;
  301. u8 next_string_id;
  302. /* the gadget driver won't enable the data pullup
  303. * while the deactivation count is nonzero.
  304. */
  305. unsigned deactivations;
  306. };
  307. extern int usb_string_id(struct usb_composite_dev *c);
  308. extern int usb_string_ids_tab(struct usb_composite_dev *c,
  309. struct usb_string *str);
  310. extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
  311. #endif /* __LINUX_USB_COMPOSITE_H */