blob: bb1eb93bd20e2c268f1fa57fb4040864a5468906 [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
Lukasz Majewski1dc6a672012-05-02 17:47:02 +020024#include <linux/usb/ch9.h>
25#include <linux/usb/gadget.h>
Lukasz Majewski80d353c2018-11-23 17:36:19 +010026#include <linux/bitmap.h>
Lukasz Majewski1dc6a672012-05-02 17:47:02 +020027
Kishon Vijay Abraham Ie7e17092015-02-23 18:40:01 +053028/*
29 * USB function drivers should return USB_GADGET_DELAYED_STATUS if they
30 * wish to delay the data/status stages of the control transfer till they
31 * are ready. The control transfer will then be kept from completing till
32 * all the function drivers that requested for USB_GADGET_DELAYED_STAUS
33 * invoke usb_composite_setup_continue().
34 */
35#define USB_GADGET_DELAYED_STATUS 0x7fff /* Impossibly large value */
36
Lukasz Majewski1dc6a672012-05-02 17:47:02 +020037struct usb_configuration;
38
39/**
Li Jun68bda3a2021-01-25 21:43:49 +080040 * struct usb_os_desc_ext_prop - describes one "Extended Property"
41 * @entry: used to keep a list of extended properties
42 * @type: Extended Property type
43 * @name_len: Extended Property unicode name length, including terminating '\0'
44 * @name: Extended Property name
45 * @data_len: Length of Extended Property blob (for unicode store double len)
46 * @data: Extended Property blob
47 */
48struct usb_os_desc_ext_prop {
49 struct list_head entry;
50 u8 type;
51 int name_len;
52 char *name;
53 int data_len;
54 char *data;
55};
56
57/**
58 * struct usb_os_desc - describes OS descriptors associated with one interface
59 * @ext_compat_id: 16 bytes of "Compatible ID" and "Subcompatible ID"
60 * @ext_prop: Extended Properties list
61 * @ext_prop_len: Total length of Extended Properties blobs
62 * @ext_prop_count: Number of Extended Properties
63 */
64struct usb_os_desc {
65 char *ext_compat_id;
66 struct list_head ext_prop;
67 int ext_prop_len;
68 int ext_prop_count;
69};
70
71/**
72 * struct usb_os_desc_table - describes OS descriptors associated with one
73 * interface of a usb_function
74 * @if_id: Interface id
75 * @os_desc: "Extended Compatibility ID" and "Extended Properties" of the
76 * interface
77 *
78 * Each interface can have at most one "Extended Compatibility ID" and a
79 * number of "Extended Properties".
80 */
81struct usb_os_desc_table {
82 int if_id;
83 struct usb_os_desc *os_desc;
84};
85
86/**
Lukasz Majewski1dc6a672012-05-02 17:47:02 +020087 * struct usb_function - describes one function of a configuration
88 * @name: For diagnostics, identifies the function.
89 * @strings: tables of strings, keyed by identifiers assigned during bind()
90 * and by language IDs provided in control requests
91 * @descriptors: Table of full (or low) speed descriptors, using interface and
92 * string identifiers assigned during @bind(). If this pointer is null,
93 * the function will not be available at full speed (or at low speed).
94 * @hs_descriptors: Table of high speed descriptors, using interface and
95 * string identifiers assigned during @bind(). If this pointer is null,
96 * the function will not be available at high speed.
97 * @config: assigned when @usb_add_function() is called; this is the
98 * configuration with which this function is associated.
Li Jun68bda3a2021-01-25 21:43:49 +080099 * @os_desc_table: Table of (interface id, os descriptors) pairs. The function
100 * can expose more than one interface. If an interface is a member of
101 * an IAD, only the first interface of IAD has its entry in the table.
102 * @os_desc_n: Number of entries in os_desc_table
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200103 * @bind: Before the gadget can register, all of its functions bind() to the
104 * available resources including string and interface identifiers used
105 * in interface or class descriptors; endpoints; I/O buffers; and so on.
106 * @unbind: Reverses @bind; called as a side effect of unregistering the
107 * driver which added this function.
108 * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
109 * initialize usb_ep.driver data at this time (when it is used).
110 * Note that setting an interface to its current altsetting resets
111 * interface state, and that all interfaces have a disabled state.
112 * @get_alt: Returns the active altsetting. If this is not provided,
113 * then only altsetting zero is supported.
114 * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
115 * include host resetting or reconfiguring the gadget, and disconnection.
116 * @setup: Used for interface-specific control requests.
117 * @suspend: Notifies functions when the host stops sending USB traffic.
118 * @resume: Notifies functions when the host restarts USB traffic.
119 *
120 * A single USB function uses one or more interfaces, and should in most
121 * cases support operation at both full and high speeds. Each function is
122 * associated by @usb_add_function() with a one configuration; that function
123 * causes @bind() to be called so resources can be allocated as part of
124 * setting up a gadget driver. Those resources include endpoints, which
125 * should be allocated using @usb_ep_autoconfig().
126 *
127 * To support dual speed operation, a function driver provides descriptors
128 * for both high and full speed operation. Except in rare cases that don't
129 * involve bulk endpoints, each speed needs different endpoint descriptors.
130 *
131 * Function drivers choose their own strategies for managing instance data.
132 * The simplest strategy just declares it "static', which means the function
133 * can only be activated once. If the function needs to be exposed in more
134 * than one configuration at a given speed, it needs to support multiple
135 * usb_function structures (one for each configuration).
136 *
137 * A more complex strategy might encapsulate a @usb_function structure inside
138 * a driver-specific instance structure to allows multiple activations. An
139 * example of multiple activations might be a CDC ACM function that supports
140 * two or more distinct instances within the same configuration, providing
141 * several independent logical data links to a USB host.
142 */
143struct usb_function {
144 const char *name;
145 struct usb_gadget_strings **strings;
146 struct usb_descriptor_header **descriptors;
147 struct usb_descriptor_header **hs_descriptors;
Li Junadb1c322021-01-25 21:43:54 +0800148 struct usb_descriptor_header **ss_descriptors;
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200149
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;
Li Junadb1c322021-01-25 21:43:54 +0800282 unsigned superspeed:1;
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200283 struct usb_function *interface[MAX_CONFIG_INTERFACES];
284};
285
286int usb_add_config(struct usb_composite_dev *,
287 struct usb_configuration *);
288
289/**
290 * struct usb_composite_driver - groups configurations into a gadget
291 * @name: For diagnostics, identifies the driver.
292 * @dev: Template descriptor for the device, including default device
293 * identifiers.
294 * @strings: tables of strings, keyed by identifiers assigned during bind()
295 * and language IDs provided in control requests
Li Junadb1c322021-01-25 21:43:54 +0800296 * @max_speed: Highest speed the driver supports.
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200297 * @bind: (REQUIRED) Used to allocate resources that are shared across the
298 * whole device, such as string IDs, and add its configurations using
299 * @usb_add_config(). This may fail by returning a negative errno
300 * value; it should return zero on successful initialization.
301 * @unbind: Reverses @bind(); called as a side effect of unregistering
302 * this driver.
303 * @disconnect: optional driver disconnect method
304 * @suspend: Notifies when the host stops sending USB traffic,
305 * after function notifications
306 * @resume: Notifies configuration when the host restarts USB traffic,
307 * before function notifications
308 *
309 * Devices default to reporting self powered operation. Devices which rely
310 * on bus powered operation should report this in their @bind() method.
311 *
312 * Before returning from @bind, various fields in the template descriptor
313 * may be overridden. These include the idVendor/idProduct/bcdDevice values
314 * normally to bind the appropriate host side driver, and the three strings
315 * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
316 * meaningful device identifiers. (The strings will not be defined unless
317 * they are defined in @dev and @strings.) The correct ep0 maxpacket size
318 * is also reported, as defined by the underlying controller driver.
319 */
320struct usb_composite_driver {
321 const char *name;
322 const struct usb_device_descriptor *dev;
323 struct usb_gadget_strings **strings;
Li Junadb1c322021-01-25 21:43:54 +0800324 enum usb_device_speed max_speed;
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200325
326 /* REVISIT: bind() functions can be marked __init, which
327 * makes trouble for section mismatch analysis. See if
328 * we can't restructure things to avoid mismatching...
329 */
330
331 int (*bind)(struct usb_composite_dev *);
332 int (*unbind)(struct usb_composite_dev *);
333
334 void (*disconnect)(struct usb_composite_dev *);
335
336 /* global suspend hooks */
337 void (*suspend)(struct usb_composite_dev *);
338 void (*resume)(struct usb_composite_dev *);
339};
340
341extern int usb_composite_register(struct usb_composite_driver *);
342extern void usb_composite_unregister(struct usb_composite_driver *);
343
Li Jun2a163542021-01-25 21:43:46 +0800344#define OS_STRING_QW_SIGN_LEN 14
345#define OS_STRING_IDX 0xEE
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200346
347/**
348 * struct usb_composite_device - represents one composite usb gadget
349 * @gadget: read-only, abstracts the gadget's usb peripheral controller
350 * @req: used for control responses; buffer is pre-allocated
351 * @bufsiz: size of buffer pre-allocated in @req
Li Jun68bda3a2021-01-25 21:43:49 +0800352 * @os_desc_req: used for OS descriptors responses; buffer is pre-allocated
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200353 * @config: the currently active configuration
Li Jun2a163542021-01-25 21:43:46 +0800354 * @qw_sign: qwSignature part of the OS string
355 * @b_vendor_code: bMS_VendorCode part of the OS string
356 * @use_os_string: false by default, interested gadgets set it
Li Jun68bda3a2021-01-25 21:43:49 +0800357 * @os_desc_config: the configuration to be used with OS descriptors
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200358 *
359 * One of these devices is allocated and initialized before the
360 * associated device driver's bind() is called.
361 *
362 * OPEN ISSUE: it appears that some WUSB devices will need to be
363 * built by combining a normal (wired) gadget with a wireless one.
364 * This revision of the gadget framework should probably try to make
365 * sure doing that won't hurt too much.
366 *
367 * One notion for how to handle Wireless USB devices involves:
368 * (a) a second gadget here, discovery mechanism TBD, but likely
369 * needing separate "register/unregister WUSB gadget" calls;
370 * (b) updates to usb_gadget to include flags "is it wireless",
371 * "is it wired", plus (presumably in a wrapper structure)
372 * bandgroup and PHY info;
373 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
374 * wireless-specific parameters like maxburst and maxsequence;
375 * (d) configurations that are specific to wireless links;
376 * (e) function drivers that understand wireless configs and will
377 * support wireless for (additional) function instances;
378 * (f) a function to support association setup (like CBAF), not
379 * necessarily requiring a wireless adapter;
380 * (g) composite device setup that can create one or more wireless
381 * configs, including appropriate association setup support;
382 * (h) more, TBD.
383 */
384struct usb_composite_dev {
385 struct usb_gadget *gadget;
386 struct usb_request *req;
387 unsigned bufsiz;
388
389 struct usb_configuration *config;
390
Li Jun2a163542021-01-25 21:43:46 +0800391 /* OS String is a custom (yet popular) extension to the USB standard. */
392 u8 qw_sign[OS_STRING_QW_SIGN_LEN];
393 u8 b_vendor_code;
Li Jun68bda3a2021-01-25 21:43:49 +0800394 struct usb_configuration *os_desc_config;
Li Jun2a163542021-01-25 21:43:46 +0800395 unsigned int use_os_string:1;
396
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200397 /* private: */
398 /* internals */
399 unsigned int suspended:1;
Heiko Schocher19bb2452013-06-27 10:04:57 +0200400 struct usb_device_descriptor __aligned(CONFIG_SYS_CACHELINE_SIZE) desc;
Lukasz Majewski1dc6a672012-05-02 17:47:02 +0200401 struct list_head configs;
402 struct usb_composite_driver *driver;
403 u8 next_string_id;
404
405 /* the gadget driver won't enable the data pullup
406 * while the deactivation count is nonzero.
407 */
408 unsigned deactivations;
409};
410
411extern int usb_string_id(struct usb_composite_dev *c);
412extern int usb_string_ids_tab(struct usb_composite_dev *c,
413 struct usb_string *str);
414extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
415
416#endif /* __LINUX_USB_COMPOSITE_H */