composite.h 16 KB

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