hv_drv_UsbGadgetComposite.h 17 KB

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