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