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 <common.h>
25 #include <linux/usb/ch9.h>
26 #include <linux/usb/gadget.h>
27 #include <linux/bitmap.h>
28 
29 /*
30  * USB function drivers should return USB_GADGET_DELAYED_STATUS if they
31  * wish to delay the data/status stages of the control transfer till they
32  * are ready. The control transfer will then be kept from completing till
33  * all the function drivers that requested for USB_GADGET_DELAYED_STAUS
34  * invoke usb_composite_setup_continue().
35  */
36 #define	USB_GADGET_DELAYED_STATUS	0x7fff /* Impossibly large value */
37 
38 struct usb_configuration;
39 
40 /**
41  * struct usb_os_desc_ext_prop - describes one "Extended Property"
42  * @entry: used to keep a list of extended properties
43  * @type: Extended Property type
44  * @name_len: Extended Property unicode name length, including terminating '\0'
45  * @name: Extended Property name
46  * @data_len: Length of Extended Property blob (for unicode store double len)
47  * @data: Extended Property blob
48  */
49 struct usb_os_desc_ext_prop {
50 	struct list_head	entry;
51 	u8			type;
52 	int			name_len;
53 	char			*name;
54 	int			data_len;
55 	char			*data;
56 };
57 
58 /**
59  * struct usb_os_desc - describes OS descriptors associated with one interface
60  * @ext_compat_id: 16 bytes of "Compatible ID" and "Subcompatible ID"
61  * @ext_prop: Extended Properties list
62  * @ext_prop_len: Total length of Extended Properties blobs
63  * @ext_prop_count: Number of Extended Properties
64  */
65 struct usb_os_desc {
66 	char			*ext_compat_id;
67 	struct list_head	ext_prop;
68 	int			ext_prop_len;
69 	int			ext_prop_count;
70 };
71 
72 /**
73  * struct usb_os_desc_table - describes OS descriptors associated with one
74  * interface of a usb_function
75  * @if_id: Interface id
76  * @os_desc: "Extended Compatibility ID" and "Extended Properties" of the
77  *	interface
78  *
79  * Each interface can have at most one "Extended Compatibility ID" and a
80  * number of "Extended Properties".
81  */
82 struct usb_os_desc_table {
83 	int			if_id;
84 	struct usb_os_desc	*os_desc;
85 };
86 
87 /**
88  * struct usb_function - describes one function of a configuration
89  * @name: For diagnostics, identifies the function.
90  * @strings: tables of strings, keyed by identifiers assigned during bind()
91  *	and by language IDs provided in control requests
92  * @descriptors: Table of full (or low) speed descriptors, using interface and
93  *	string identifiers assigned during @bind().  If this pointer is null,
94  *	the function will not be available at full speed (or at low speed).
95  * @hs_descriptors: Table of high speed descriptors, using interface and
96  *	string identifiers assigned during @bind().  If this pointer is null,
97  *	the function will not be available at high speed.
98  * @config: assigned when @usb_add_function() is called; this is the
99  *	configuration with which this function is associated.
100  * @os_desc_table: Table of (interface id, os descriptors) pairs. The function
101  *	can expose more than one interface. If an interface is a member of
102  *	an IAD, only the first interface of IAD has its entry in the table.
103  * @os_desc_n: Number of entries in os_desc_table
104  * @bind: Before the gadget can register, all of its functions bind() to the
105  *	available resources including string and interface identifiers used
106  *	in interface or class descriptors; endpoints; I/O buffers; and so on.
107  * @unbind: Reverses @bind; called as a side effect of unregistering the
108  *	driver which added this function.
109  * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
110  *	initialize usb_ep.driver data at this time (when it is used).
111  *	Note that setting an interface to its current altsetting resets
112  *	interface state, and that all interfaces have a disabled state.
113  * @get_alt: Returns the active altsetting.  If this is not provided,
114  *	then only altsetting zero is supported.
115  * @disable: (REQUIRED) Indicates the function should be disabled.  Reasons
116  *	include host resetting or reconfiguring the gadget, and disconnection.
117  * @setup: Used for interface-specific control requests.
118  * @suspend: Notifies functions when the host stops sending USB traffic.
119  * @resume: Notifies functions when the host restarts USB traffic.
120  *
121  * A single USB function uses one or more interfaces, and should in most
122  * cases support operation at both full and high speeds.  Each function is
123  * associated by @usb_add_function() with a one configuration; that function
124  * causes @bind() to be called so resources can be allocated as part of
125  * setting up a gadget driver.  Those resources include endpoints, which
126  * should be allocated using @usb_ep_autoconfig().
127  *
128  * To support dual speed operation, a function driver provides descriptors
129  * for both high and full speed operation.  Except in rare cases that don't
130  * involve bulk endpoints, each speed needs different endpoint descriptors.
131  *
132  * Function drivers choose their own strategies for managing instance data.
133  * The simplest strategy just declares it "static', which means the function
134  * can only be activated once.  If the function needs to be exposed in more
135  * than one configuration at a given speed, it needs to support multiple
136  * usb_function structures (one for each configuration).
137  *
138  * A more complex strategy might encapsulate a @usb_function structure inside
139  * a driver-specific instance structure to allows multiple activations.  An
140  * example of multiple activations might be a CDC ACM function that supports
141  * two or more distinct instances within the same configuration, providing
142  * several independent logical data links to a USB host.
143  */
144 struct usb_function {
145 	const char			*name;
146 	struct usb_gadget_strings	**strings;
147 	struct usb_descriptor_header	**descriptors;
148 	struct usb_descriptor_header	**hs_descriptors;
149 	struct usb_descriptor_header    **ss_descriptors;
150 
151 	struct usb_configuration	*config;
152 
153 	struct usb_os_desc_table	*os_desc_table;
154 	unsigned			os_desc_n;
155 
156 	/* REVISIT:  bind() functions can be marked __init, which
157 	 * makes trouble for section mismatch analysis.  See if
158 	 * we can't restructure things to avoid mismatching.
159 	 * Related:  unbind() may kfree() but bind() won't...
160 	 */
161 
162 	/* configuration management:  bind/unbind */
163 	int			(*bind)(struct usb_configuration *,
164 					struct usb_function *);
165 	void			(*unbind)(struct usb_configuration *,
166 					struct usb_function *);
167 
168 	/* runtime state management */
169 	int			(*set_alt)(struct usb_function *,
170 					unsigned interface, unsigned alt);
171 	int			(*get_alt)(struct usb_function *,
172 					unsigned interface);
173 	void			(*disable)(struct usb_function *);
174 	int			(*setup)(struct usb_function *,
175 					const struct usb_ctrlrequest *);
176 	void			(*suspend)(struct usb_function *);
177 	void			(*resume)(struct usb_function *);
178 
179 	/* private: */
180 	/* internals */
181 	struct list_head		list;
182 	DECLARE_BITMAP(endpoints, 32);
183 };
184 
185 int usb_add_function(struct usb_configuration *, struct usb_function *);
186 
187 int usb_function_deactivate(struct usb_function *);
188 int usb_function_activate(struct usb_function *);
189 
190 int usb_interface_id(struct usb_configuration *, struct usb_function *);
191 
192 /**
193  * ep_choose - select descriptor endpoint at current device speed
194  * @g: gadget, connected and running at some speed
195  * @hs: descriptor to use for high speed operation
196  * @fs: descriptor to use for full or low speed operation
197  */
198 static inline struct usb_endpoint_descriptor *
ep_choose(struct usb_gadget * g,struct usb_endpoint_descriptor * hs,struct usb_endpoint_descriptor * fs)199 ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
200 		struct usb_endpoint_descriptor *fs)
201 {
202 	if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
203 		return hs;
204 	return fs;
205 }
206 
207 #define	MAX_CONFIG_INTERFACES		16	/* arbitrary; max 255 */
208 
209 /**
210  * struct usb_configuration - represents one gadget configuration
211  * @label: For diagnostics, describes the configuration.
212  * @strings: Tables of strings, keyed by identifiers assigned during @bind()
213  *	and by language IDs provided in control requests.
214  * @descriptors: Table of descriptors preceding all function descriptors.
215  *	Examples include OTG and vendor-specific descriptors.
216  * @bind: Called from @usb_add_config() to allocate resources unique to this
217  *	configuration and to call @usb_add_function() for each function used.
218  * @unbind: Reverses @bind; called as a side effect of unregistering the
219  *	driver which added this configuration.
220  * @setup: Used to delegate control requests that aren't handled by standard
221  *	device infrastructure or directed at a specific interface.
222  * @bConfigurationValue: Copied into configuration descriptor.
223  * @iConfiguration: Copied into configuration descriptor.
224  * @bmAttributes: Copied into configuration descriptor.
225  * @bMaxPower: Copied into configuration descriptor.
226  * @cdev: assigned by @usb_add_config() before calling @bind(); this is
227  *	the device associated with this configuration.
228  *
229  * Configurations are building blocks for gadget drivers structured around
230  * function drivers.  Simple USB gadgets require only one function and one
231  * configuration, and handle dual-speed hardware by always providing the same
232  * functionality.  Slightly more complex gadgets may have more than one
233  * single-function configuration at a given speed; or have configurations
234  * that only work at one speed.
235  *
236  * Composite devices are, by definition, ones with configurations which
237  * include more than one function.
238  *
239  * The lifecycle of a usb_configuration includes allocation, initialization
240  * of the fields described above, and calling @usb_add_config() to set up
241  * internal data and bind it to a specific device.  The configuration's
242  * @bind() method is then used to initialize all the functions and then
243  * call @usb_add_function() for them.
244  *
245  * Those functions would normally be independant of each other, but that's
246  * not mandatory.  CDC WMC devices are an example where functions often
247  * depend on other functions, with some functions subsidiary to others.
248  * Such interdependency may be managed in any way, so long as all of the
249  * descriptors complete by the time the composite driver returns from
250  * its bind() routine.
251  */
252 struct usb_configuration {
253 	const char			*label;
254 	struct usb_gadget_strings	**strings;
255 	const struct usb_descriptor_header **descriptors;
256 
257 	/* REVISIT:  bind() functions can be marked __init, which
258 	 * makes trouble for section mismatch analysis.  See if
259 	 * we can't restructure things to avoid mismatching...
260 	 */
261 
262 	/* configuration management:  bind/unbind */
263 	int			(*bind)(struct usb_configuration *);
264 	void			(*unbind)(struct usb_configuration *);
265 	int			(*setup)(struct usb_configuration *,
266 					const struct usb_ctrlrequest *);
267 
268 	/* fields in the config descriptor */
269 	u8			bConfigurationValue;
270 	u8			iConfiguration;
271 	u8			bmAttributes;
272 	u8			bMaxPower;
273 
274 	struct usb_composite_dev	*cdev;
275 
276 	/* private: */
277 	/* internals */
278 	struct list_head	list;
279 	struct list_head	functions;
280 	u8			next_interface_id;
281 	unsigned		highspeed:1;
282 	unsigned		fullspeed:1;
283 	unsigned		superspeed:1;
284 	struct usb_function	*interface[MAX_CONFIG_INTERFACES];
285 };
286 
287 int usb_add_config(struct usb_composite_dev *,
288 		struct usb_configuration *);
289 
290 /**
291  * struct usb_composite_driver - groups configurations into a gadget
292  * @name: For diagnostics, identifies the driver.
293  * @dev: Template descriptor for the device, including default device
294  *	identifiers.
295  * @strings: tables of strings, keyed by identifiers assigned during bind()
296  *	and language IDs provided in control requests
297  * @max_speed: Highest speed the driver supports.
298  * @bind: (REQUIRED) Used to allocate resources that are shared across the
299  *	whole device, such as string IDs, and add its configurations using
300  *	@usb_add_config().  This may fail by returning a negative errno
301  *	value; it should return zero on successful initialization.
302  * @unbind: Reverses @bind(); called as a side effect of unregistering
303  *	this driver.
304  * @disconnect: optional driver disconnect method
305  * @suspend: Notifies when the host stops sending USB traffic,
306  *	after function notifications
307  * @resume: Notifies configuration when the host restarts USB traffic,
308  *	before function notifications
309  *
310  * Devices default to reporting self powered operation.  Devices which rely
311  * on bus powered operation should report this in their @bind() method.
312  *
313  * Before returning from @bind, various fields in the template descriptor
314  * may be overridden.  These include the idVendor/idProduct/bcdDevice values
315  * normally to bind the appropriate host side driver, and the three strings
316  * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
317  * meaningful device identifiers.  (The strings will not be defined unless
318  * they are defined in @dev and @strings.)  The correct ep0 maxpacket size
319  * is also reported, as defined by the underlying controller driver.
320  */
321 struct usb_composite_driver {
322 	const char				*name;
323 	const struct usb_device_descriptor	*dev;
324 	struct usb_gadget_strings		**strings;
325 	enum usb_device_speed			max_speed;
326 
327 	/* REVISIT:  bind() functions can be marked __init, which
328 	 * makes trouble for section mismatch analysis.  See if
329 	 * we can't restructure things to avoid mismatching...
330 	 */
331 
332 	int			(*bind)(struct usb_composite_dev *);
333 	int			(*unbind)(struct usb_composite_dev *);
334 
335 	void			(*disconnect)(struct usb_composite_dev *);
336 
337 	/* global suspend hooks */
338 	void			(*suspend)(struct usb_composite_dev *);
339 	void			(*resume)(struct usb_composite_dev *);
340 };
341 
342 extern int usb_composite_register(struct usb_composite_driver *);
343 extern void usb_composite_unregister(struct usb_composite_driver *);
344 
345 #define OS_STRING_QW_SIGN_LEN		14
346 #define OS_STRING_IDX			0xEE
347 
348 /**
349  * struct usb_composite_device - represents one composite usb gadget
350  * @gadget: read-only, abstracts the gadget's usb peripheral controller
351  * @req: used for control responses; buffer is pre-allocated
352  * @bufsiz: size of buffer pre-allocated in @req
353  * @os_desc_req: used for OS descriptors responses; buffer is pre-allocated
354  * @config: the currently active configuration
355  * @qw_sign: qwSignature part of the OS string
356  * @b_vendor_code: bMS_VendorCode part of the OS string
357  * @use_os_string: false by default, interested gadgets set it
358  * @os_desc_config: the configuration to be used with OS descriptors
359  *
360  * One of these devices is allocated and initialized before the
361  * associated device driver's bind() is called.
362  *
363  * OPEN ISSUE:  it appears that some WUSB devices will need to be
364  * built by combining a normal (wired) gadget with a wireless one.
365  * This revision of the gadget framework should probably try to make
366  * sure doing that won't hurt too much.
367  *
368  * One notion for how to handle Wireless USB devices involves:
369  * (a) a second gadget here, discovery mechanism TBD, but likely
370  *     needing separate "register/unregister WUSB gadget" calls;
371  * (b) updates to usb_gadget to include flags "is it wireless",
372  *     "is it wired", plus (presumably in a wrapper structure)
373  *     bandgroup and PHY info;
374  * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
375  *     wireless-specific parameters like maxburst and maxsequence;
376  * (d) configurations that are specific to wireless links;
377  * (e) function drivers that understand wireless configs and will
378  *     support wireless for (additional) function instances;
379  * (f) a function to support association setup (like CBAF), not
380  *     necessarily requiring a wireless adapter;
381  * (g) composite device setup that can create one or more wireless
382  *     configs, including appropriate association setup support;
383  * (h) more, TBD.
384  */
385 struct usb_composite_dev {
386 	struct usb_gadget		*gadget;
387 	struct usb_request		*req;
388 	unsigned			bufsiz;
389 
390 	struct usb_configuration	*config;
391 
392 	/* OS String is a custom (yet popular) extension to the USB standard. */
393 	u8				qw_sign[OS_STRING_QW_SIGN_LEN];
394 	u8				b_vendor_code;
395 	struct usb_configuration	*os_desc_config;
396 	unsigned int			use_os_string:1;
397 
398 	/* private: */
399 	/* internals */
400 	unsigned int			suspended:1;
401 	struct usb_device_descriptor __aligned(CONFIG_SYS_CACHELINE_SIZE) desc;
402 	struct list_head		configs;
403 	struct usb_composite_driver	*driver;
404 	u8				next_string_id;
405 
406 	/* the gadget driver won't enable the data pullup
407 	 * while the deactivation count is nonzero.
408 	 */
409 	unsigned			deactivations;
410 };
411 
412 extern int usb_string_id(struct usb_composite_dev *c);
413 extern int usb_string_ids_tab(struct usb_composite_dev *c,
414 			      struct usb_string *str);
415 extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
416 
417 #endif	/* __LINUX_USB_COMPOSITE_H */
418