composite.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. /* big enough to hold our biggest descriptor */
  45. #define USB_COMP_EP0_BUFSIZ 1024
  46. struct usb_configuration;
  47. /**
  48. * struct usb_function - describes one function of a configuration
  49. * @name: For diagnostics, identifies the function.
  50. * @strings: tables of strings, keyed by identifiers assigned during bind()
  51. * and by language IDs provided in control requests
  52. * @descriptors: Table of full (or low) speed descriptors, using interface and
  53. * string identifiers assigned during @bind(). If this pointer is null,
  54. * the function will not be available at full speed (or at low speed).
  55. * @hs_descriptors: Table of high speed descriptors, using interface and
  56. * string identifiers assigned during @bind(). If this pointer is null,
  57. * the function will not be available at high speed.
  58. * @ss_descriptors: Table of super speed descriptors, using interface and
  59. * string identifiers assigned during @bind(). If this
  60. * pointer is null after initiation, the function will not
  61. * be available at super speed.
  62. * @config: assigned when @usb_add_function() is called; this is the
  63. * configuration with which this function is associated.
  64. * @bind: Before the gadget can register, all of its functions bind() to the
  65. * available resources including string and interface identifiers used
  66. * in interface or class descriptors; endpoints; I/O buffers; and so on.
  67. * @unbind: Reverses @bind; called as a side effect of unregistering the
  68. * driver which added this function.
  69. * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
  70. * initialize usb_ep.driver data at this time (when it is used).
  71. * Note that setting an interface to its current altsetting resets
  72. * interface state, and that all interfaces have a disabled state.
  73. * @get_alt: Returns the active altsetting. If this is not provided,
  74. * then only altsetting zero is supported.
  75. * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
  76. * include host resetting or reconfiguring the gadget, and disconnection.
  77. * @setup: Used for interface-specific control requests.
  78. * @suspend: Notifies functions when the host stops sending USB traffic.
  79. * @resume: Notifies functions when the host restarts USB traffic.
  80. * @get_status: Returns function status as a reply to
  81. * GetStatus() request when the recepient is Interface.
  82. * @func_suspend: callback to be called when
  83. * SetFeature(FUNCTION_SUSPEND) is reseived
  84. *
  85. * A single USB function uses one or more interfaces, and should in most
  86. * cases support operation at both full and high speeds. Each function is
  87. * associated by @usb_add_function() with a one configuration; that function
  88. * causes @bind() to be called so resources can be allocated as part of
  89. * setting up a gadget driver. Those resources include endpoints, which
  90. * should be allocated using @usb_ep_autoconfig().
  91. *
  92. * To support dual speed operation, a function driver provides descriptors
  93. * for both high and full speed operation. Except in rare cases that don't
  94. * involve bulk endpoints, each speed needs different endpoint descriptors.
  95. *
  96. * Function drivers choose their own strategies for managing instance data.
  97. * The simplest strategy just declares it "static', which means the function
  98. * can only be activated once. If the function needs to be exposed in more
  99. * than one configuration at a given speed, it needs to support multiple
  100. * usb_function structures (one for each configuration).
  101. *
  102. * A more complex strategy might encapsulate a @usb_function structure inside
  103. * a driver-specific instance structure to allows multiple activations. An
  104. * example of multiple activations might be a CDC ACM function that supports
  105. * two or more distinct instances within the same configuration, providing
  106. * several independent logical data links to a USB host.
  107. */
  108. struct usb_function {
  109. const char *name;
  110. struct usb_gadget_strings **strings;
  111. struct usb_descriptor_header **descriptors;
  112. struct usb_descriptor_header **hs_descriptors;
  113. struct usb_descriptor_header **ss_descriptors;
  114. struct usb_configuration *config;
  115. /* REVISIT: bind() functions can be marked __init, which
  116. * makes trouble for section mismatch analysis. See if
  117. * we can't restructure things to avoid mismatching.
  118. * Related: unbind() may kfree() but bind() won't...
  119. */
  120. /* configuration management: bind/unbind */
  121. int (*bind)(struct usb_configuration *,
  122. struct usb_function *);
  123. void (*unbind)(struct usb_configuration *,
  124. struct usb_function *);
  125. /* runtime state management */
  126. int (*set_alt)(struct usb_function *,
  127. unsigned interface, unsigned alt);
  128. int (*get_alt)(struct usb_function *,
  129. unsigned interface);
  130. void (*disable)(struct usb_function *);
  131. int (*setup)(struct usb_function *,
  132. const struct usb_ctrlrequest *);
  133. void (*suspend)(struct usb_function *);
  134. void (*resume)(struct usb_function *);
  135. /* USB 3.0 additions */
  136. int (*get_status)(struct usb_function *);
  137. int (*func_suspend)(struct usb_function *,
  138. u8 suspend_opt);
  139. /* private: */
  140. /* internals */
  141. struct list_head list;
  142. DECLARE_BITMAP(endpoints, 32);
  143. };
  144. int usb_add_function(struct usb_configuration *, struct usb_function *);
  145. int usb_function_deactivate(struct usb_function *);
  146. int usb_function_activate(struct usb_function *);
  147. int usb_interface_id(struct usb_configuration *, struct usb_function *);
  148. int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f,
  149. struct usb_ep *_ep);
  150. #define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
  151. /**
  152. * struct usb_configuration - represents one gadget configuration
  153. * @label: For diagnostics, describes the configuration.
  154. * @strings: Tables of strings, keyed by identifiers assigned during @bind()
  155. * and by language IDs provided in control requests.
  156. * @descriptors: Table of descriptors preceding all function descriptors.
  157. * Examples include OTG and vendor-specific descriptors.
  158. * @unbind: Reverses @bind; called as a side effect of unregistering the
  159. * driver which added this configuration.
  160. * @setup: Used to delegate control requests that aren't handled by standard
  161. * device infrastructure or directed at a specific interface.
  162. * @bConfigurationValue: Copied into configuration descriptor.
  163. * @iConfiguration: Copied into configuration descriptor.
  164. * @bmAttributes: Copied into configuration descriptor.
  165. * @bMaxPower: Copied into configuration descriptor.
  166. * @cdev: assigned by @usb_add_config() before calling @bind(); this is
  167. * the device associated with this configuration.
  168. *
  169. * Configurations are building blocks for gadget drivers structured around
  170. * function drivers. Simple USB gadgets require only one function and one
  171. * configuration, and handle dual-speed hardware by always providing the same
  172. * functionality. Slightly more complex gadgets may have more than one
  173. * single-function configuration at a given speed; or have configurations
  174. * that only work at one speed.
  175. *
  176. * Composite devices are, by definition, ones with configurations which
  177. * include more than one function.
  178. *
  179. * The lifecycle of a usb_configuration includes allocation, initialization
  180. * of the fields described above, and calling @usb_add_config() to set up
  181. * internal data and bind it to a specific device. The configuration's
  182. * @bind() method is then used to initialize all the functions and then
  183. * call @usb_add_function() for them.
  184. *
  185. * Those functions would normally be independent of each other, but that's
  186. * not mandatory. CDC WMC devices are an example where functions often
  187. * depend on other functions, with some functions subsidiary to others.
  188. * Such interdependency may be managed in any way, so long as all of the
  189. * descriptors complete by the time the composite driver returns from
  190. * its bind() routine.
  191. */
  192. struct usb_configuration {
  193. const char *label;
  194. struct usb_gadget_strings **strings;
  195. const struct usb_descriptor_header **descriptors;
  196. /* REVISIT: bind() functions can be marked __init, which
  197. * makes trouble for section mismatch analysis. See if
  198. * we can't restructure things to avoid mismatching...
  199. */
  200. /* configuration management: unbind/setup */
  201. void (*unbind)(struct usb_configuration *);
  202. int (*setup)(struct usb_configuration *,
  203. const struct usb_ctrlrequest *);
  204. /* fields in the config descriptor */
  205. u8 bConfigurationValue;
  206. u8 iConfiguration;
  207. u8 bmAttributes;
  208. u8 bMaxPower;
  209. struct usb_composite_dev *cdev;
  210. /* private: */
  211. /* internals */
  212. struct list_head list;
  213. struct list_head functions;
  214. u8 next_interface_id;
  215. unsigned superspeed:1;
  216. unsigned highspeed:1;
  217. unsigned fullspeed:1;
  218. struct usb_function *interface[MAX_CONFIG_INTERFACES];
  219. };
  220. int usb_add_config(struct usb_composite_dev *,
  221. struct usb_configuration *,
  222. int (*)(struct usb_configuration *));
  223. void usb_remove_config(struct usb_composite_dev *,
  224. struct usb_configuration *);
  225. /* predefined index for usb_composite_driver */
  226. enum {
  227. USB_GADGET_MANUFACTURER_IDX = 0,
  228. USB_GADGET_PRODUCT_IDX,
  229. USB_GADGET_SERIAL_IDX,
  230. USB_GADGET_FIRST_AVAIL_IDX,
  231. };
  232. /**
  233. * struct usb_composite_driver - groups configurations into a gadget
  234. * @name: For diagnostics, identifies the driver.
  235. * @dev: Template descriptor for the device, including default device
  236. * identifiers.
  237. * @strings: tables of strings, keyed by identifiers assigned during @bind
  238. * and language IDs provided in control requests. Note: The first entries
  239. * are predefined. The first entry that may be used is
  240. * USB_GADGET_FIRST_AVAIL_IDX
  241. * @max_speed: Highest speed the driver supports.
  242. * @needs_serial: set to 1 if the gadget needs userspace to provide
  243. * a serial number. If one is not provided, warning will be printed.
  244. * @bind: (REQUIRED) Used to allocate resources that are shared across the
  245. * whole device, such as string IDs, and add its configurations using
  246. * @usb_add_config(). This may fail by returning a negative errno
  247. * value; it should return zero on successful initialization.
  248. * @unbind: Reverses @bind; called as a side effect of unregistering
  249. * this driver.
  250. * @disconnect: optional driver disconnect method
  251. * @suspend: Notifies when the host stops sending USB traffic,
  252. * after function notifications
  253. * @resume: Notifies configuration when the host restarts USB traffic,
  254. * before function notifications
  255. *
  256. * Devices default to reporting self powered operation. Devices which rely
  257. * on bus powered operation should report this in their @bind method.
  258. *
  259. * Before returning from @bind, various fields in the template descriptor
  260. * may be overridden. These include the idVendor/idProduct/bcdDevice values
  261. * normally to bind the appropriate host side driver, and the three strings
  262. * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
  263. * meaningful device identifiers. (The strings will not be defined unless
  264. * they are defined in @dev and @strings.) The correct ep0 maxpacket size
  265. * is also reported, as defined by the underlying controller driver.
  266. */
  267. struct usb_composite_driver {
  268. const char *name;
  269. const struct usb_device_descriptor *dev;
  270. struct usb_gadget_strings **strings;
  271. enum usb_device_speed max_speed;
  272. unsigned needs_serial:1;
  273. int (*bind)(struct usb_composite_dev *cdev);
  274. int (*unbind)(struct usb_composite_dev *);
  275. void (*disconnect)(struct usb_composite_dev *);
  276. /* global suspend hooks */
  277. void (*suspend)(struct usb_composite_dev *);
  278. void (*resume)(struct usb_composite_dev *);
  279. struct usb_gadget_driver gadget_driver;
  280. };
  281. extern int usb_composite_probe(struct usb_composite_driver *driver);
  282. extern void usb_composite_unregister(struct usb_composite_driver *driver);
  283. extern void usb_composite_setup_continue(struct usb_composite_dev *cdev);
  284. /**
  285. * struct usb_composite_device - represents one composite usb gadget
  286. * @gadget: read-only, abstracts the gadget's usb peripheral controller
  287. * @req: used for control responses; buffer is pre-allocated
  288. * @config: the currently active configuration
  289. *
  290. * One of these devices is allocated and initialized before the
  291. * associated device driver's bind() is called.
  292. *
  293. * OPEN ISSUE: it appears that some WUSB devices will need to be
  294. * built by combining a normal (wired) gadget with a wireless one.
  295. * This revision of the gadget framework should probably try to make
  296. * sure doing that won't hurt too much.
  297. *
  298. * One notion for how to handle Wireless USB devices involves:
  299. * (a) a second gadget here, discovery mechanism TBD, but likely
  300. * needing separate "register/unregister WUSB gadget" calls;
  301. * (b) updates to usb_gadget to include flags "is it wireless",
  302. * "is it wired", plus (presumably in a wrapper structure)
  303. * bandgroup and PHY info;
  304. * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
  305. * wireless-specific parameters like maxburst and maxsequence;
  306. * (d) configurations that are specific to wireless links;
  307. * (e) function drivers that understand wireless configs and will
  308. * support wireless for (additional) function instances;
  309. * (f) a function to support association setup (like CBAF), not
  310. * necessarily requiring a wireless adapter;
  311. * (g) composite device setup that can create one or more wireless
  312. * configs, including appropriate association setup support;
  313. * (h) more, TBD.
  314. */
  315. struct usb_composite_dev {
  316. struct usb_gadget *gadget;
  317. struct usb_request *req;
  318. struct usb_configuration *config;
  319. /* private: */
  320. /* internals */
  321. unsigned int suspended:1;
  322. struct usb_device_descriptor desc;
  323. struct list_head configs;
  324. struct usb_composite_driver *driver;
  325. u8 next_string_id;
  326. u8 manufacturer_override;
  327. /* the gadget driver won't enable the data pullup
  328. * while the deactivation count is nonzero.
  329. */
  330. unsigned deactivations;
  331. /* the composite driver won't complete the control transfer's
  332. * data/status stages till delayed_status is zero.
  333. */
  334. int delayed_status;
  335. /* protects deactivations and delayed_status counts*/
  336. spinlock_t lock;
  337. };
  338. extern int usb_string_id(struct usb_composite_dev *c);
  339. extern int usb_string_ids_tab(struct usb_composite_dev *c,
  340. struct usb_string *str);
  341. extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
  342. /*
  343. * Some systems will need runtime overrides for the product identifiers
  344. * published in the device descriptor, either numbers or strings or both.
  345. * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  346. */
  347. struct usb_composite_overwrite {
  348. u16 idVendor;
  349. u16 idProduct;
  350. u16 bcdDevice;
  351. char *serial_number;
  352. char *manufacturer;
  353. char *product;
  354. };
  355. #define USB_GADGET_COMPOSITE_OPTIONS() \
  356. static struct usb_composite_overwrite coverwrite; \
  357. \
  358. module_param_named(idVendor, coverwrite.idVendor, ushort, S_IRUGO); \
  359. MODULE_PARM_DESC(idVendor, "USB Vendor ID"); \
  360. \
  361. module_param_named(idProduct, coverwrite.idProduct, ushort, S_IRUGO); \
  362. MODULE_PARM_DESC(idProduct, "USB Product ID"); \
  363. \
  364. module_param_named(bcdDevice, coverwrite.bcdDevice, ushort, S_IRUGO); \
  365. MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); \
  366. \
  367. module_param_named(iSerialNumber, coverwrite.serial_number, charp, \
  368. S_IRUGO); \
  369. MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); \
  370. \
  371. module_param_named(iManufacturer, coverwrite.manufacturer, charp, \
  372. S_IRUGO); \
  373. MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); \
  374. \
  375. module_param_named(iProduct, coverwrite.product, charp, S_IRUGO); \
  376. MODULE_PARM_DESC(iProduct, "USB Product string")
  377. void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
  378. struct usb_composite_overwrite *covr);
  379. /* messaging utils */
  380. #define DBG(d, fmt, args...) \
  381. dev_dbg(&(d)->gadget->dev , fmt , ## args)
  382. #define VDBG(d, fmt, args...) \
  383. dev_vdbg(&(d)->gadget->dev , fmt , ## args)
  384. #define ERROR(d, fmt, args...) \
  385. dev_err(&(d)->gadget->dev , fmt , ## args)
  386. #define WARNING(d, fmt, args...) \
  387. dev_warn(&(d)->gadget->dev , fmt , ## args)
  388. #define INFO(d, fmt, args...) \
  389. dev_info(&(d)->gadget->dev , fmt , ## args)
  390. #endif /* __LINUX_USB_COMPOSITE_H */