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