composite.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * composite.h -- framework for usb gadgets which are composite devices
  4. *
  5. * Copyright (C) 2006-2008 David Brownell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef __LINUX_USB_COMPOSITE_H
  22. #define __LINUX_USB_COMPOSITE_H
  23. /*
  24. * This framework is an optional layer on top of the USB Gadget interface,
  25. * making it easier to build (a) Composite devices, supporting multiple
  26. * functions within any single configuration, and (b) Multi-configuration
  27. * devices, also supporting multiple functions but without necessarily
  28. * having more than one function per configuration.
  29. *
  30. * Example: a device with a single configuration supporting both network
  31. * link and mass storage functions is a composite device. Those functions
  32. * might alternatively be packaged in individual configurations, but in
  33. * the composite model the host can use both functions at the same time.
  34. */
  35. #include <linux/bcd.h>
  36. #include <linux/version.h>
  37. #include <linux/usb/ch9.h>
  38. #include <linux/usb/gadget.h>
  39. #include <linux/log2.h>
  40. #include <linux/configfs.h>
  41. /*
  42. * USB function drivers should return USB_GADGET_DELAYED_STATUS if they
  43. * wish to delay the data/status stages of the control transfer till they
  44. * are ready. The control transfer will then be kept from completing till
  45. * all the function drivers that requested for USB_GADGET_DELAYED_STAUS
  46. * invoke usb_composite_setup_continue().
  47. */
  48. #define USB_GADGET_DELAYED_STATUS 0x7fff /* Impossibly large value */
  49. /* big enough to hold our biggest descriptor */
  50. #define USB_COMP_EP0_BUFSIZ 4096
  51. /* OS feature descriptor length <= 4kB */
  52. #define USB_COMP_EP0_OS_DESC_BUFSIZ 4096
  53. #define USB_MS_TO_HS_INTERVAL(x) (ilog2((x * 1000 / 125)) + 1)
  54. struct usb_configuration;
  55. /**
  56. * struct usb_os_desc_ext_prop - describes one "Extended Property"
  57. * @entry: used to keep a list of extended properties
  58. * @type: Extended Property type
  59. * @name_len: Extended Property unicode name length, including terminating '\0'
  60. * @name: Extended Property name
  61. * @data_len: Length of Extended Property blob (for unicode store double len)
  62. * @data: Extended Property blob
  63. * @item: Represents this Extended Property in configfs
  64. */
  65. struct usb_os_desc_ext_prop {
  66. struct list_head entry;
  67. u8 type;
  68. int name_len;
  69. char *name;
  70. int data_len;
  71. char *data;
  72. struct config_item item;
  73. };
  74. /**
  75. * struct usb_os_desc - describes OS descriptors associated with one interface
  76. * @ext_compat_id: 16 bytes of "Compatible ID" and "Subcompatible ID"
  77. * @ext_prop: Extended Properties list
  78. * @ext_prop_len: Total length of Extended Properties blobs
  79. * @ext_prop_count: Number of Extended Properties
  80. * @opts_mutex: Optional mutex protecting config data of a usb_function_instance
  81. * @group: Represents OS descriptors associated with an interface in configfs
  82. * @owner: Module associated with this OS descriptor
  83. */
  84. struct usb_os_desc {
  85. char *ext_compat_id;
  86. struct list_head ext_prop;
  87. int ext_prop_len;
  88. int ext_prop_count;
  89. struct mutex *opts_mutex;
  90. struct config_group group;
  91. struct module *owner;
  92. };
  93. /**
  94. * struct usb_os_desc_table - describes OS descriptors associated with one
  95. * interface of a usb_function
  96. * @if_id: Interface id
  97. * @os_desc: "Extended Compatibility ID" and "Extended Properties" of the
  98. * interface
  99. *
  100. * Each interface can have at most one "Extended Compatibility ID" and a
  101. * number of "Extended Properties".
  102. */
  103. struct usb_os_desc_table {
  104. int if_id;
  105. struct usb_os_desc *os_desc;
  106. };
  107. /**
  108. * struct usb_function - describes one function of a configuration
  109. * @name: For diagnostics, identifies the function.
  110. * @strings: tables of strings, keyed by identifiers assigned during bind()
  111. * and by language IDs provided in control requests
  112. * @fs_descriptors: Table of full (or low) speed descriptors, using interface and
  113. * string identifiers assigned during @bind(). If this pointer is null,
  114. * the function will not be available at full speed (or at low speed).
  115. * @hs_descriptors: Table of high speed descriptors, using interface and
  116. * string identifiers assigned during @bind(). If this pointer is null,
  117. * the function will not be available at high speed.
  118. * @ss_descriptors: Table of super speed descriptors, using interface and
  119. * string identifiers assigned during @bind(). If this
  120. * pointer is null after initiation, the function will not
  121. * be available at super speed.
  122. * @ssp_descriptors: Table of super speed plus descriptors, using
  123. * interface and string identifiers assigned during @bind(). If
  124. * this pointer is null after initiation, the function will not
  125. * be available at super speed plus.
  126. * @config: assigned when @usb_add_function() is called; this is the
  127. * configuration with which this function is associated.
  128. * @os_desc_table: Table of (interface id, os descriptors) pairs. The function
  129. * can expose more than one interface. If an interface is a member of
  130. * an IAD, only the first interface of IAD has its entry in the table.
  131. * @os_desc_n: Number of entries in os_desc_table
  132. * @bind: Before the gadget can register, all of its functions bind() to the
  133. * available resources including string and interface identifiers used
  134. * in interface or class descriptors; endpoints; I/O buffers; and so on.
  135. * @unbind: Reverses @bind; called as a side effect of unregistering the
  136. * driver which added this function.
  137. * @free_func: free the struct usb_function.
  138. * @mod: (internal) points to the module that created this structure.
  139. * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
  140. * initialize usb_ep.driver data at this time (when it is used).
  141. * Note that setting an interface to its current altsetting resets
  142. * interface state, and that all interfaces have a disabled state.
  143. * @get_alt: Returns the active altsetting. If this is not provided,
  144. * then only altsetting zero is supported.
  145. * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
  146. * include host resetting or reconfiguring the gadget, and disconnection.
  147. * @setup: Used for interface-specific control requests.
  148. * @req_match: Tests if a given class request can be handled by this function.
  149. * @suspend: Notifies functions when the host stops sending USB traffic.
  150. * @resume: Notifies functions when the host restarts USB traffic.
  151. * @get_status: Returns function status as a reply to
  152. * GetStatus() request when the recipient is Interface.
  153. * @func_suspend: callback to be called when
  154. * SetFeature(FUNCTION_SUSPEND) is reseived
  155. *
  156. * A single USB function uses one or more interfaces, and should in most
  157. * cases support operation at both full and high speeds. Each function is
  158. * associated by @usb_add_function() with a one configuration; that function
  159. * causes @bind() to be called so resources can be allocated as part of
  160. * setting up a gadget driver. Those resources include endpoints, which
  161. * should be allocated using @usb_ep_autoconfig().
  162. *
  163. * To support dual speed operation, a function driver provides descriptors
  164. * for both high and full speed operation. Except in rare cases that don't
  165. * involve bulk endpoints, each speed needs different endpoint descriptors.
  166. *
  167. * Function drivers choose their own strategies for managing instance data.
  168. * The simplest strategy just declares it "static', which means the function
  169. * can only be activated once. If the function needs to be exposed in more
  170. * than one configuration at a given speed, it needs to support multiple
  171. * usb_function structures (one for each configuration).
  172. *
  173. * A more complex strategy might encapsulate a @usb_function structure inside
  174. * a driver-specific instance structure to allows multiple activations. An
  175. * example of multiple activations might be a CDC ACM function that supports
  176. * two or more distinct instances within the same configuration, providing
  177. * several independent logical data links to a USB host.
  178. */
  179. struct usb_function {
  180. const char *name;
  181. struct usb_gadget_strings **strings;
  182. struct usb_descriptor_header **fs_descriptors;
  183. struct usb_descriptor_header **hs_descriptors;
  184. struct usb_descriptor_header **ss_descriptors;
  185. struct usb_descriptor_header **ssp_descriptors;
  186. struct usb_configuration *config;
  187. struct usb_os_desc_table *os_desc_table;
  188. unsigned os_desc_n;
  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. * Related: unbind() may kfree() but bind() won't...
  193. */
  194. /* configuration management: bind/unbind */
  195. int (*bind)(struct usb_configuration *,
  196. struct usb_function *);
  197. void (*unbind)(struct usb_configuration *,
  198. struct usb_function *);
  199. void (*free_func)(struct usb_function *f);
  200. struct module *mod;
  201. /* runtime state management */
  202. int (*set_alt)(struct usb_function *,
  203. unsigned interface, unsigned alt);
  204. int (*get_alt)(struct usb_function *,
  205. unsigned interface);
  206. void (*disable)(struct usb_function *);
  207. int (*setup)(struct usb_function *,
  208. const struct usb_ctrlrequest *);
  209. bool (*req_match)(struct usb_function *,
  210. const struct usb_ctrlrequest *,
  211. bool config0);
  212. void (*suspend)(struct usb_function *);
  213. void (*resume)(struct usb_function *);
  214. /* USB 3.0 additions */
  215. int (*get_status)(struct usb_function *);
  216. int (*func_suspend)(struct usb_function *,
  217. u8 suspend_opt);
  218. /* private: */
  219. /* internals */
  220. struct list_head list;
  221. DECLARE_BITMAP(endpoints, 32);
  222. const struct usb_function_instance *fi;
  223. unsigned int bind_deactivated:1;
  224. };
  225. int usb_add_function(struct usb_configuration *, struct usb_function *);
  226. int usb_function_deactivate(struct usb_function *);
  227. int usb_function_activate(struct usb_function *);
  228. int usb_interface_id(struct usb_configuration *, struct usb_function *);
  229. int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f,
  230. struct usb_ep *_ep);
  231. #define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
  232. /**
  233. * struct usb_configuration - represents one gadget configuration
  234. * @label: For diagnostics, describes the configuration.
  235. * @strings: Tables of strings, keyed by identifiers assigned during @bind()
  236. * and by language IDs provided in control requests.
  237. * @descriptors: Table of descriptors preceding all function descriptors.
  238. * Examples include OTG and vendor-specific descriptors.
  239. * @unbind: Reverses @bind; called as a side effect of unregistering the
  240. * driver which added this configuration.
  241. * @setup: Used to delegate control requests that aren't handled by standard
  242. * device infrastructure or directed at a specific interface.
  243. * @bConfigurationValue: Copied into configuration descriptor.
  244. * @iConfiguration: Copied into configuration descriptor.
  245. * @bmAttributes: Copied into configuration descriptor.
  246. * @MaxPower: Power consumtion in mA. Used to compute bMaxPower in the
  247. * configuration descriptor after considering the bus speed.
  248. * @cdev: assigned by @usb_add_config() before calling @bind(); this is
  249. * the device associated with this configuration.
  250. *
  251. * Configurations are building blocks for gadget drivers structured around
  252. * function drivers. Simple USB gadgets require only one function and one
  253. * configuration, and handle dual-speed hardware by always providing the same
  254. * functionality. Slightly more complex gadgets may have more than one
  255. * single-function configuration at a given speed; or have configurations
  256. * that only work at one speed.
  257. *
  258. * Composite devices are, by definition, ones with configurations which
  259. * include more than one function.
  260. *
  261. * The lifecycle of a usb_configuration includes allocation, initialization
  262. * of the fields described above, and calling @usb_add_config() to set up
  263. * internal data and bind it to a specific device. The configuration's
  264. * @bind() method is then used to initialize all the functions and then
  265. * call @usb_add_function() for them.
  266. *
  267. * Those functions would normally be independent of each other, but that's
  268. * not mandatory. CDC WMC devices are an example where functions often
  269. * depend on other functions, with some functions subsidiary to others.
  270. * Such interdependency may be managed in any way, so long as all of the
  271. * descriptors complete by the time the composite driver returns from
  272. * its bind() routine.
  273. */
  274. struct usb_configuration {
  275. const char *label;
  276. struct usb_gadget_strings **strings;
  277. const struct usb_descriptor_header **descriptors;
  278. /* REVISIT: bind() functions can be marked __init, which
  279. * makes trouble for section mismatch analysis. See if
  280. * we can't restructure things to avoid mismatching...
  281. */
  282. /* configuration management: unbind/setup */
  283. void (*unbind)(struct usb_configuration *);
  284. int (*setup)(struct usb_configuration *,
  285. const struct usb_ctrlrequest *);
  286. /* fields in the config descriptor */
  287. u8 bConfigurationValue;
  288. u8 iConfiguration;
  289. u8 bmAttributes;
  290. u16 MaxPower;
  291. struct usb_composite_dev *cdev;
  292. /* private: */
  293. /* internals */
  294. struct list_head list;
  295. struct list_head functions;
  296. u8 next_interface_id;
  297. unsigned superspeed:1;
  298. unsigned highspeed:1;
  299. unsigned fullspeed:1;
  300. unsigned superspeed_plus:1;
  301. struct usb_function *interface[MAX_CONFIG_INTERFACES];
  302. };
  303. int usb_add_config(struct usb_composite_dev *,
  304. struct usb_configuration *,
  305. int (*)(struct usb_configuration *));
  306. void usb_remove_config(struct usb_composite_dev *,
  307. struct usb_configuration *);
  308. /* predefined index for usb_composite_driver */
  309. enum {
  310. USB_GADGET_MANUFACTURER_IDX = 0,
  311. USB_GADGET_PRODUCT_IDX,
  312. USB_GADGET_SERIAL_IDX,
  313. USB_GADGET_FIRST_AVAIL_IDX,
  314. };
  315. /**
  316. * struct usb_composite_driver - groups configurations into a gadget
  317. * @name: For diagnostics, identifies the driver.
  318. * @dev: Template descriptor for the device, including default device
  319. * identifiers.
  320. * @strings: tables of strings, keyed by identifiers assigned during @bind
  321. * and language IDs provided in control requests. Note: The first entries
  322. * are predefined. The first entry that may be used is
  323. * USB_GADGET_FIRST_AVAIL_IDX
  324. * @max_speed: Highest speed the driver supports.
  325. * @needs_serial: set to 1 if the gadget needs userspace to provide
  326. * a serial number. If one is not provided, warning will be printed.
  327. * @bind: (REQUIRED) Used to allocate resources that are shared across the
  328. * whole device, such as string IDs, and add its configurations using
  329. * @usb_add_config(). This may fail by returning a negative errno
  330. * value; it should return zero on successful initialization.
  331. * @unbind: Reverses @bind; called as a side effect of unregistering
  332. * this driver.
  333. * @disconnect: optional driver disconnect method
  334. * @suspend: Notifies when the host stops sending USB traffic,
  335. * after function notifications
  336. * @resume: Notifies configuration when the host restarts USB traffic,
  337. * before function notifications
  338. * @gadget_driver: Gadget driver controlling this driver
  339. *
  340. * Devices default to reporting self powered operation. Devices which rely
  341. * on bus powered operation should report this in their @bind method.
  342. *
  343. * Before returning from @bind, various fields in the template descriptor
  344. * may be overridden. These include the idVendor/idProduct/bcdDevice values
  345. * normally to bind the appropriate host side driver, and the three strings
  346. * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
  347. * meaningful device identifiers. (The strings will not be defined unless
  348. * they are defined in @dev and @strings.) The correct ep0 maxpacket size
  349. * is also reported, as defined by the underlying controller driver.
  350. */
  351. struct usb_composite_driver {
  352. const char *name;
  353. const struct usb_device_descriptor *dev;
  354. struct usb_gadget_strings **strings;
  355. enum usb_device_speed max_speed;
  356. unsigned needs_serial:1;
  357. int (*bind)(struct usb_composite_dev *cdev);
  358. int (*unbind)(struct usb_composite_dev *);
  359. void (*disconnect)(struct usb_composite_dev *);
  360. /* global suspend hooks */
  361. void (*suspend)(struct usb_composite_dev *);
  362. void (*resume)(struct usb_composite_dev *);
  363. struct usb_gadget_driver gadget_driver;
  364. };
  365. extern int usb_composite_probe(struct usb_composite_driver *driver);
  366. extern void usb_composite_unregister(struct usb_composite_driver *driver);
  367. /**
  368. * module_usb_composite_driver() - Helper macro for registering a USB gadget
  369. * composite driver
  370. * @__usb_composite_driver: usb_composite_driver struct
  371. *
  372. * Helper macro for USB gadget composite drivers which do not do anything
  373. * special in module init/exit. This eliminates a lot of boilerplate. Each
  374. * module may only use this macro once, and calling it replaces module_init()
  375. * and module_exit()
  376. */
  377. #define module_usb_composite_driver(__usb_composite_driver) \
  378. module_driver(__usb_composite_driver, usb_composite_probe, \
  379. usb_composite_unregister)
  380. extern void usb_composite_setup_continue(struct usb_composite_dev *cdev);
  381. extern int composite_dev_prepare(struct usb_composite_driver *composite,
  382. struct usb_composite_dev *cdev);
  383. extern int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
  384. struct usb_ep *ep0);
  385. void composite_dev_cleanup(struct usb_composite_dev *cdev);
  386. static inline struct usb_composite_driver *to_cdriver(
  387. struct usb_gadget_driver *gdrv)
  388. {
  389. return container_of(gdrv, struct usb_composite_driver, gadget_driver);
  390. }
  391. #define OS_STRING_QW_SIGN_LEN 14
  392. #define OS_STRING_IDX 0xEE
  393. /**
  394. * struct usb_composite_device - represents one composite usb gadget
  395. * @gadget: read-only, abstracts the gadget's usb peripheral controller
  396. * @req: used for control responses; buffer is pre-allocated
  397. * @os_desc_req: used for OS descriptors responses; buffer is pre-allocated
  398. * @config: the currently active configuration
  399. * @qw_sign: qwSignature part of the OS string
  400. * @b_vendor_code: bMS_VendorCode part of the OS string
  401. * @use_os_string: false by default, interested gadgets set it
  402. * @os_desc_config: the configuration to be used with OS descriptors
  403. * @setup_pending: true when setup request is queued but not completed
  404. * @os_desc_pending: true when os_desc request is queued but not completed
  405. *
  406. * One of these devices is allocated and initialized before the
  407. * associated device driver's bind() is called.
  408. *
  409. * OPEN ISSUE: it appears that some WUSB devices will need to be
  410. * built by combining a normal (wired) gadget with a wireless one.
  411. * This revision of the gadget framework should probably try to make
  412. * sure doing that won't hurt too much.
  413. *
  414. * One notion for how to handle Wireless USB devices involves:
  415. *
  416. * (a) a second gadget here, discovery mechanism TBD, but likely
  417. * needing separate "register/unregister WUSB gadget" calls;
  418. * (b) updates to usb_gadget to include flags "is it wireless",
  419. * "is it wired", plus (presumably in a wrapper structure)
  420. * bandgroup and PHY info;
  421. * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
  422. * wireless-specific parameters like maxburst and maxsequence;
  423. * (d) configurations that are specific to wireless links;
  424. * (e) function drivers that understand wireless configs and will
  425. * support wireless for (additional) function instances;
  426. * (f) a function to support association setup (like CBAF), not
  427. * necessarily requiring a wireless adapter;
  428. * (g) composite device setup that can create one or more wireless
  429. * configs, including appropriate association setup support;
  430. * (h) more, TBD.
  431. */
  432. struct usb_composite_dev {
  433. struct usb_gadget *gadget;
  434. struct usb_request *req;
  435. struct usb_request *os_desc_req;
  436. struct usb_configuration *config;
  437. /* OS String is a custom (yet popular) extension to the USB standard. */
  438. u8 qw_sign[OS_STRING_QW_SIGN_LEN];
  439. u8 b_vendor_code;
  440. struct usb_configuration *os_desc_config;
  441. unsigned int use_os_string:1;
  442. /* private: */
  443. /* internals */
  444. unsigned int suspended:1;
  445. struct usb_device_descriptor desc;
  446. struct list_head configs;
  447. struct list_head gstrings;
  448. struct usb_composite_driver *driver;
  449. u8 next_string_id;
  450. char *def_manufacturer;
  451. /* the gadget driver won't enable the data pullup
  452. * while the deactivation count is nonzero.
  453. */
  454. unsigned deactivations;
  455. /* the composite driver won't complete the control transfer's
  456. * data/status stages till delayed_status is zero.
  457. */
  458. int delayed_status;
  459. /* protects deactivations and delayed_status counts*/
  460. spinlock_t lock;
  461. /* public: */
  462. unsigned int setup_pending:1;
  463. unsigned int os_desc_pending:1;
  464. };
  465. extern int usb_string_id(struct usb_composite_dev *c);
  466. extern int usb_string_ids_tab(struct usb_composite_dev *c,
  467. struct usb_string *str);
  468. extern struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
  469. struct usb_gadget_strings **sp, unsigned n_strings);
  470. extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
  471. extern void composite_disconnect(struct usb_gadget *gadget);
  472. extern int composite_setup(struct usb_gadget *gadget,
  473. const struct usb_ctrlrequest *ctrl);
  474. extern void composite_suspend(struct usb_gadget *gadget);
  475. extern void composite_resume(struct usb_gadget *gadget);
  476. /*
  477. * Some systems will need runtime overrides for the product identifiers
  478. * published in the device descriptor, either numbers or strings or both.
  479. * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  480. */
  481. struct usb_composite_overwrite {
  482. u16 idVendor;
  483. u16 idProduct;
  484. u16 bcdDevice;
  485. char *serial_number;
  486. char *manufacturer;
  487. char *product;
  488. };
  489. #define USB_GADGET_COMPOSITE_OPTIONS() \
  490. static struct usb_composite_overwrite coverwrite; \
  491. \
  492. module_param_named(idVendor, coverwrite.idVendor, ushort, S_IRUGO); \
  493. MODULE_PARM_DESC(idVendor, "USB Vendor ID"); \
  494. \
  495. module_param_named(idProduct, coverwrite.idProduct, ushort, S_IRUGO); \
  496. MODULE_PARM_DESC(idProduct, "USB Product ID"); \
  497. \
  498. module_param_named(bcdDevice, coverwrite.bcdDevice, ushort, S_IRUGO); \
  499. MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); \
  500. \
  501. module_param_named(iSerialNumber, coverwrite.serial_number, charp, \
  502. S_IRUGO); \
  503. MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); \
  504. \
  505. module_param_named(iManufacturer, coverwrite.manufacturer, charp, \
  506. S_IRUGO); \
  507. MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); \
  508. \
  509. module_param_named(iProduct, coverwrite.product, charp, S_IRUGO); \
  510. MODULE_PARM_DESC(iProduct, "USB Product string")
  511. void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
  512. struct usb_composite_overwrite *covr);
  513. static inline u16 get_default_bcdDevice(void)
  514. {
  515. u16 bcdDevice;
  516. bcdDevice = bin2bcd((LINUX_VERSION_CODE >> 16 & 0xff)) << 8;
  517. bcdDevice |= bin2bcd((LINUX_VERSION_CODE >> 8 & 0xff));
  518. return bcdDevice;
  519. }
  520. struct usb_function_driver {
  521. const char *name;
  522. struct module *mod;
  523. struct list_head list;
  524. struct usb_function_instance *(*alloc_inst)(void);
  525. struct usb_function *(*alloc_func)(struct usb_function_instance *inst);
  526. };
  527. struct usb_function_instance {
  528. struct config_group group;
  529. struct list_head cfs_list;
  530. struct usb_function_driver *fd;
  531. int (*set_inst_name)(struct usb_function_instance *inst,
  532. const char *name);
  533. void (*free_func_inst)(struct usb_function_instance *inst);
  534. };
  535. void usb_function_unregister(struct usb_function_driver *f);
  536. int usb_function_register(struct usb_function_driver *newf);
  537. void usb_put_function_instance(struct usb_function_instance *fi);
  538. void usb_put_function(struct usb_function *f);
  539. struct usb_function_instance *usb_get_function_instance(const char *name);
  540. struct usb_function *usb_get_function(struct usb_function_instance *fi);
  541. struct usb_configuration *usb_get_config(struct usb_composite_dev *cdev,
  542. int val);
  543. int usb_add_config_only(struct usb_composite_dev *cdev,
  544. struct usb_configuration *config);
  545. void usb_remove_function(struct usb_configuration *c, struct usb_function *f);
  546. #define DECLARE_USB_FUNCTION(_name, _inst_alloc, _func_alloc) \
  547. static struct usb_function_driver _name ## usb_func = { \
  548. .name = __stringify(_name), \
  549. .mod = THIS_MODULE, \
  550. .alloc_inst = _inst_alloc, \
  551. .alloc_func = _func_alloc, \
  552. }; \
  553. MODULE_ALIAS("usbfunc:"__stringify(_name));
  554. #define DECLARE_USB_FUNCTION_INIT(_name, _inst_alloc, _func_alloc) \
  555. DECLARE_USB_FUNCTION(_name, _inst_alloc, _func_alloc) \
  556. static int __init _name ## mod_init(void) \
  557. { \
  558. return usb_function_register(&_name ## usb_func); \
  559. } \
  560. static void __exit _name ## mod_exit(void) \
  561. { \
  562. usb_function_unregister(&_name ## usb_func); \
  563. } \
  564. module_init(_name ## mod_init); \
  565. module_exit(_name ## mod_exit)
  566. /* messaging utils */
  567. #define DBG(d, fmt, args...) \
  568. dev_dbg(&(d)->gadget->dev , fmt , ## args)
  569. #define VDBG(d, fmt, args...) \
  570. dev_vdbg(&(d)->gadget->dev , fmt , ## args)
  571. #define ERROR(d, fmt, args...) \
  572. dev_err(&(d)->gadget->dev , fmt , ## args)
  573. #define WARNING(d, fmt, args...) \
  574. dev_warn(&(d)->gadget->dev , fmt , ## args)
  575. #define INFO(d, fmt, args...) \
  576. dev_info(&(d)->gadget->dev , fmt , ## args)
  577. #endif /* __LINUX_USB_COMPOSITE_H */