PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/include/linux/usb/composite.h

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