composite.h 24 KB

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