composite.h 21 KB

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