blob: d75a0bc4c4914dcf814c3ba3314a4f6544714d1d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Lukasz Majewski1dc6a672012-05-02 17:47:02 +02002/*
3 * composite.h -- framework for usb gadgets which are composite devices
4 *
5 * Copyright (C) 2006-2008 David Brownell
Lukasz Majewski1dc6a672012-05-02 17:47:02 +02006 */
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>
Lukasz Majewski80d353c2018-11-23 17:36:19 +010027#include <linux/bitmap.h>
Lukasz Majewski1dc6a672012-05-02 17:47:02 +020028
Kishon Vijay Abraham Ie7e17092015-02-23 18:40:01 +053029/*
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
Lukasz Majewski1dc6a672012-05-02 17:47:02 +020038struct usb_configuration;
39
40/**
Li Jun68bda3a2021-01-25 21:43:49 +080041 * 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 */
49struct 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 */
65struct 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 */
82struct usb_os_desc_table {
83 int if_id;
84 struct usb_os_desc *os_desc;
85};
86
87/**
Lukasz Majewski1dc6a672012-05-02 17:47:02 +020088 * 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.
Li Jun68bda3a2021-01-25 21:43:49 +0800100 * @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
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200104 * @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 */
144struct 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
150 struct usb_configuration *config;
151
Li Jun68bda3a2021-01-25 21:43:49 +0800152 struct usb_os_desc_table *os_desc_table;
153 unsigned os_desc_n;
154
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200155 /* REVISIT: bind() functions can be marked __init, which
156 * makes trouble for section mismatch analysis. See if
157 * we can't restructure things to avoid mismatching.
158 * Related: unbind() may kfree() but bind() won't...
159 */
160
161 /* configuration management: bind/unbind */
162 int (*bind)(struct usb_configuration *,
163 struct usb_function *);
164 void (*unbind)(struct usb_configuration *,
165 struct usb_function *);
166
167 /* runtime state management */
168 int (*set_alt)(struct usb_function *,
169 unsigned interface, unsigned alt);
170 int (*get_alt)(struct usb_function *,
171 unsigned interface);
172 void (*disable)(struct usb_function *);
173 int (*setup)(struct usb_function *,
174 const struct usb_ctrlrequest *);
175 void (*suspend)(struct usb_function *);
176 void (*resume)(struct usb_function *);
177
178 /* private: */
179 /* internals */
180 struct list_head list;
181 DECLARE_BITMAP(endpoints, 32);
182};
183
184int usb_add_function(struct usb_configuration *, struct usb_function *);
185
186int usb_function_deactivate(struct usb_function *);
187int usb_function_activate(struct usb_function *);
188
189int usb_interface_id(struct usb_configuration *, struct usb_function *);
190
191/**
192 * ep_choose - select descriptor endpoint at current device speed
193 * @g: gadget, connected and running at some speed
194 * @hs: descriptor to use for high speed operation
195 * @fs: descriptor to use for full or low speed operation
196 */
197static inline struct usb_endpoint_descriptor *
198ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
199 struct usb_endpoint_descriptor *fs)
200{
201 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
202 return hs;
203 return fs;
204}
205
206#define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
207
208/**
209 * struct usb_configuration - represents one gadget configuration
210 * @label: For diagnostics, describes the configuration.
211 * @strings: Tables of strings, keyed by identifiers assigned during @bind()
212 * and by language IDs provided in control requests.
213 * @descriptors: Table of descriptors preceding all function descriptors.
214 * Examples include OTG and vendor-specific descriptors.
215 * @bind: Called from @usb_add_config() to allocate resources unique to this
216 * configuration and to call @usb_add_function() for each function used.
217 * @unbind: Reverses @bind; called as a side effect of unregistering the
218 * driver which added this configuration.
219 * @setup: Used to delegate control requests that aren't handled by standard
220 * device infrastructure or directed at a specific interface.
221 * @bConfigurationValue: Copied into configuration descriptor.
222 * @iConfiguration: Copied into configuration descriptor.
223 * @bmAttributes: Copied into configuration descriptor.
224 * @bMaxPower: Copied into configuration descriptor.
225 * @cdev: assigned by @usb_add_config() before calling @bind(); this is
226 * the device associated with this configuration.
227 *
228 * Configurations are building blocks for gadget drivers structured around
229 * function drivers. Simple USB gadgets require only one function and one
230 * configuration, and handle dual-speed hardware by always providing the same
231 * functionality. Slightly more complex gadgets may have more than one
232 * single-function configuration at a given speed; or have configurations
233 * that only work at one speed.
234 *
235 * Composite devices are, by definition, ones with configurations which
236 * include more than one function.
237 *
238 * The lifecycle of a usb_configuration includes allocation, initialization
239 * of the fields described above, and calling @usb_add_config() to set up
240 * internal data and bind it to a specific device. The configuration's
241 * @bind() method is then used to initialize all the functions and then
242 * call @usb_add_function() for them.
243 *
244 * Those functions would normally be independant of each other, but that's
245 * not mandatory. CDC WMC devices are an example where functions often
246 * depend on other functions, with some functions subsidiary to others.
247 * Such interdependency may be managed in any way, so long as all of the
248 * descriptors complete by the time the composite driver returns from
249 * its bind() routine.
250 */
251struct usb_configuration {
252 const char *label;
253 struct usb_gadget_strings **strings;
254 const struct usb_descriptor_header **descriptors;
255
256 /* REVISIT: bind() functions can be marked __init, which
257 * makes trouble for section mismatch analysis. See if
258 * we can't restructure things to avoid mismatching...
259 */
260
261 /* configuration management: bind/unbind */
262 int (*bind)(struct usb_configuration *);
263 void (*unbind)(struct usb_configuration *);
264 int (*setup)(struct usb_configuration *,
265 const struct usb_ctrlrequest *);
266
267 /* fields in the config descriptor */
268 u8 bConfigurationValue;
269 u8 iConfiguration;
270 u8 bmAttributes;
271 u8 bMaxPower;
272
273 struct usb_composite_dev *cdev;
274
275 /* private: */
276 /* internals */
277 struct list_head list;
278 struct list_head functions;
279 u8 next_interface_id;
280 unsigned highspeed:1;
281 unsigned fullspeed:1;
282 struct usb_function *interface[MAX_CONFIG_INTERFACES];
283};
284
285int usb_add_config(struct usb_composite_dev *,
286 struct usb_configuration *);
287
288/**
289 * struct usb_composite_driver - groups configurations into a gadget
290 * @name: For diagnostics, identifies the driver.
291 * @dev: Template descriptor for the device, including default device
292 * identifiers.
293 * @strings: tables of strings, keyed by identifiers assigned during bind()
294 * and language IDs provided in control requests
295 * @bind: (REQUIRED) Used to allocate resources that are shared across the
296 * whole device, such as string IDs, and add its configurations using
297 * @usb_add_config(). This may fail by returning a negative errno
298 * value; it should return zero on successful initialization.
299 * @unbind: Reverses @bind(); called as a side effect of unregistering
300 * this driver.
301 * @disconnect: optional driver disconnect method
302 * @suspend: Notifies when the host stops sending USB traffic,
303 * after function notifications
304 * @resume: Notifies configuration when the host restarts USB traffic,
305 * before function notifications
306 *
307 * Devices default to reporting self powered operation. Devices which rely
308 * on bus powered operation should report this in their @bind() method.
309 *
310 * Before returning from @bind, various fields in the template descriptor
311 * may be overridden. These include the idVendor/idProduct/bcdDevice values
312 * normally to bind the appropriate host side driver, and the three strings
313 * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
314 * meaningful device identifiers. (The strings will not be defined unless
315 * they are defined in @dev and @strings.) The correct ep0 maxpacket size
316 * is also reported, as defined by the underlying controller driver.
317 */
318struct usb_composite_driver {
319 const char *name;
320 const struct usb_device_descriptor *dev;
321 struct usb_gadget_strings **strings;
322
323 /* REVISIT: bind() functions can be marked __init, which
324 * makes trouble for section mismatch analysis. See if
325 * we can't restructure things to avoid mismatching...
326 */
327
328 int (*bind)(struct usb_composite_dev *);
329 int (*unbind)(struct usb_composite_dev *);
330
331 void (*disconnect)(struct usb_composite_dev *);
332
333 /* global suspend hooks */
334 void (*suspend)(struct usb_composite_dev *);
335 void (*resume)(struct usb_composite_dev *);
336};
337
338extern int usb_composite_register(struct usb_composite_driver *);
339extern void usb_composite_unregister(struct usb_composite_driver *);
340
Li Jun2a163542021-01-25 21:43:46 +0800341#define OS_STRING_QW_SIGN_LEN 14
342#define OS_STRING_IDX 0xEE
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200343
344/**
345 * struct usb_composite_device - represents one composite usb gadget
346 * @gadget: read-only, abstracts the gadget's usb peripheral controller
347 * @req: used for control responses; buffer is pre-allocated
348 * @bufsiz: size of buffer pre-allocated in @req
Li Jun68bda3a2021-01-25 21:43:49 +0800349 * @os_desc_req: used for OS descriptors responses; buffer is pre-allocated
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200350 * @config: the currently active configuration
Li Jun2a163542021-01-25 21:43:46 +0800351 * @qw_sign: qwSignature part of the OS string
352 * @b_vendor_code: bMS_VendorCode part of the OS string
353 * @use_os_string: false by default, interested gadgets set it
Li Jun68bda3a2021-01-25 21:43:49 +0800354 * @os_desc_config: the configuration to be used with OS descriptors
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200355 *
356 * One of these devices is allocated and initialized before the
357 * associated device driver's bind() is called.
358 *
359 * OPEN ISSUE: it appears that some WUSB devices will need to be
360 * built by combining a normal (wired) gadget with a wireless one.
361 * This revision of the gadget framework should probably try to make
362 * sure doing that won't hurt too much.
363 *
364 * One notion for how to handle Wireless USB devices involves:
365 * (a) a second gadget here, discovery mechanism TBD, but likely
366 * needing separate "register/unregister WUSB gadget" calls;
367 * (b) updates to usb_gadget to include flags "is it wireless",
368 * "is it wired", plus (presumably in a wrapper structure)
369 * bandgroup and PHY info;
370 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
371 * wireless-specific parameters like maxburst and maxsequence;
372 * (d) configurations that are specific to wireless links;
373 * (e) function drivers that understand wireless configs and will
374 * support wireless for (additional) function instances;
375 * (f) a function to support association setup (like CBAF), not
376 * necessarily requiring a wireless adapter;
377 * (g) composite device setup that can create one or more wireless
378 * configs, including appropriate association setup support;
379 * (h) more, TBD.
380 */
381struct usb_composite_dev {
382 struct usb_gadget *gadget;
383 struct usb_request *req;
384 unsigned bufsiz;
385
386 struct usb_configuration *config;
387
Li Jun2a163542021-01-25 21:43:46 +0800388 /* OS String is a custom (yet popular) extension to the USB standard. */
389 u8 qw_sign[OS_STRING_QW_SIGN_LEN];
390 u8 b_vendor_code;
Li Jun68bda3a2021-01-25 21:43:49 +0800391 struct usb_configuration *os_desc_config;
Li Jun2a163542021-01-25 21:43:46 +0800392 unsigned int use_os_string:1;
393
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200394 /* private: */
395 /* internals */
396 unsigned int suspended:1;
Heiko Schocher19bb2452013-06-27 10:04:57 +0200397 struct usb_device_descriptor __aligned(CONFIG_SYS_CACHELINE_SIZE) desc;
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200398 struct list_head configs;
399 struct usb_composite_driver *driver;
400 u8 next_string_id;
401
402 /* the gadget driver won't enable the data pullup
403 * while the deactivation count is nonzero.
404 */
405 unsigned deactivations;
406};
407
408extern int usb_string_id(struct usb_composite_dev *c);
409extern int usb_string_ids_tab(struct usb_composite_dev *c,
410 struct usb_string *str);
411extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
412
413#endif /* __LINUX_USB_COMPOSITE_H */