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