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