blob: 2014d6b1babc0f7251cafcd1d43f09e87ffb5471 [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
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
Roger Quadros1b9ba002011-05-09 13:08:06 +030040/*
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 */
David Brownell40982be2008-06-19 17:52:58 -070048
49struct 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 */
104struct 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
Randy Dunlapcac85a82009-04-29 21:04:19 -0700135 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700136 /* internals */
137 struct list_head list;
Laurent Pinchart52426582009-10-21 00:03:38 +0200138 DECLARE_BITMAP(endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700139};
140
141int usb_add_function(struct usb_configuration *, struct usb_function *);
142
David Brownell60beed92008-08-18 17:38:22 -0700143int usb_function_deactivate(struct usb_function *);
144int usb_function_activate(struct usb_function *);
145
David Brownell40982be2008-06-19 17:52:58 -0700146int usb_interface_id(struct usb_configuration *, struct usb_function *);
147
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300148int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f,
149 struct usb_ep *_ep);
150
David Brownell40982be2008-06-19 17:52:58 -0700151/**
152 * ep_choose - select descriptor endpoint at current device speed
153 * @g: gadget, connected and running at some speed
154 * @hs: descriptor to use for high speed operation
155 * @fs: descriptor to use for full or low speed operation
156 */
157static inline struct usb_endpoint_descriptor *
158ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
159 struct usb_endpoint_descriptor *fs)
160{
161 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
162 return hs;
163 return fs;
164}
165
166#define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
167
168/**
169 * struct usb_configuration - represents one gadget configuration
170 * @label: For diagnostics, describes the configuration.
171 * @strings: Tables of strings, keyed by identifiers assigned during @bind()
172 * and by language IDs provided in control requests.
173 * @descriptors: Table of descriptors preceding all function descriptors.
174 * Examples include OTG and vendor-specific descriptors.
David Brownell40982be2008-06-19 17:52:58 -0700175 * @unbind: Reverses @bind; called as a side effect of unregistering the
176 * driver which added this configuration.
177 * @setup: Used to delegate control requests that aren't handled by standard
178 * device infrastructure or directed at a specific interface.
179 * @bConfigurationValue: Copied into configuration descriptor.
180 * @iConfiguration: Copied into configuration descriptor.
181 * @bmAttributes: Copied into configuration descriptor.
182 * @bMaxPower: Copied into configuration descriptor.
183 * @cdev: assigned by @usb_add_config() before calling @bind(); this is
184 * the device associated with this configuration.
185 *
186 * Configurations are building blocks for gadget drivers structured around
187 * function drivers. Simple USB gadgets require only one function and one
188 * configuration, and handle dual-speed hardware by always providing the same
189 * functionality. Slightly more complex gadgets may have more than one
190 * single-function configuration at a given speed; or have configurations
191 * that only work at one speed.
192 *
193 * Composite devices are, by definition, ones with configurations which
194 * include more than one function.
195 *
196 * The lifecycle of a usb_configuration includes allocation, initialization
197 * of the fields described above, and calling @usb_add_config() to set up
198 * internal data and bind it to a specific device. The configuration's
199 * @bind() method is then used to initialize all the functions and then
200 * call @usb_add_function() for them.
201 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300202 * Those functions would normally be independent of each other, but that's
David Brownell40982be2008-06-19 17:52:58 -0700203 * not mandatory. CDC WMC devices are an example where functions often
204 * depend on other functions, with some functions subsidiary to others.
205 * Such interdependency may be managed in any way, so long as all of the
206 * descriptors complete by the time the composite driver returns from
207 * its bind() routine.
208 */
209struct usb_configuration {
210 const char *label;
211 struct usb_gadget_strings **strings;
212 const struct usb_descriptor_header **descriptors;
213
214 /* REVISIT: bind() functions can be marked __init, which
215 * makes trouble for section mismatch analysis. See if
216 * we can't restructure things to avoid mismatching...
217 */
218
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200219 /* configuration management: unbind/setup */
David Brownell40982be2008-06-19 17:52:58 -0700220 void (*unbind)(struct usb_configuration *);
221 int (*setup)(struct usb_configuration *,
222 const struct usb_ctrlrequest *);
223
224 /* fields in the config descriptor */
225 u8 bConfigurationValue;
226 u8 iConfiguration;
227 u8 bmAttributes;
228 u8 bMaxPower;
229
230 struct usb_composite_dev *cdev;
231
Randy Dunlapcac85a82009-04-29 21:04:19 -0700232 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700233 /* internals */
234 struct list_head list;
235 struct list_head functions;
236 u8 next_interface_id;
237 unsigned highspeed:1;
238 unsigned fullspeed:1;
239 struct usb_function *interface[MAX_CONFIG_INTERFACES];
240};
241
242int usb_add_config(struct usb_composite_dev *,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200243 struct usb_configuration *,
244 int (*)(struct usb_configuration *));
David Brownell40982be2008-06-19 17:52:58 -0700245
246/**
247 * struct usb_composite_driver - groups configurations into a gadget
248 * @name: For diagnostics, identifies the driver.
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200249 * @iProduct: Used as iProduct override if @dev->iProduct is not set.
250 * If NULL value of @name is taken.
251 * @iManufacturer: Used as iManufacturer override if @dev->iManufacturer is
252 * not set. If NULL a default "<system> <release> with <udc>" value
253 * will be used.
David Brownell40982be2008-06-19 17:52:58 -0700254 * @dev: Template descriptor for the device, including default device
255 * identifiers.
256 * @strings: tables of strings, keyed by identifiers assigned during bind()
257 * and language IDs provided in control requests
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200258 * @needs_serial: set to 1 if the gadget needs userspace to provide
259 * a serial number. If one is not provided, warning will be printed.
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200260 * @unbind: Reverses bind; called as a side effect of unregistering
David Brownell40982be2008-06-19 17:52:58 -0700261 * this driver.
Randy Dunlapd187abb2010-08-11 12:07:13 -0700262 * @disconnect: optional driver disconnect method
David Brownell89429392009-03-19 14:14:17 -0700263 * @suspend: Notifies when the host stops sending USB traffic,
264 * after function notifications
265 * @resume: Notifies configuration when the host restarts USB traffic,
266 * before function notifications
David Brownell40982be2008-06-19 17:52:58 -0700267 *
268 * Devices default to reporting self powered operation. Devices which rely
269 * on bus powered operation should report this in their @bind() method.
270 *
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200271 * Before returning from bind, various fields in the template descriptor
David Brownell40982be2008-06-19 17:52:58 -0700272 * may be overridden. These include the idVendor/idProduct/bcdDevice values
273 * normally to bind the appropriate host side driver, and the three strings
274 * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
275 * meaningful device identifiers. (The strings will not be defined unless
276 * they are defined in @dev and @strings.) The correct ep0 maxpacket size
277 * is also reported, as defined by the underlying controller driver.
278 */
279struct usb_composite_driver {
280 const char *name;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200281 const char *iProduct;
282 const char *iManufacturer;
David Brownell40982be2008-06-19 17:52:58 -0700283 const struct usb_device_descriptor *dev;
284 struct usb_gadget_strings **strings;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200285 unsigned needs_serial:1;
David Brownell40982be2008-06-19 17:52:58 -0700286
David Brownell40982be2008-06-19 17:52:58 -0700287 int (*unbind)(struct usb_composite_dev *);
David Brownell89429392009-03-19 14:14:17 -0700288
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +0200289 void (*disconnect)(struct usb_composite_dev *);
290
David Brownell89429392009-03-19 14:14:17 -0700291 /* global suspend hooks */
292 void (*suspend)(struct usb_composite_dev *);
293 void (*resume)(struct usb_composite_dev *);
David Brownell40982be2008-06-19 17:52:58 -0700294};
295
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200296extern int usb_composite_probe(struct usb_composite_driver *driver,
297 int (*bind)(struct usb_composite_dev *cdev));
298extern void usb_composite_unregister(struct usb_composite_driver *driver);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300299extern void usb_composite_setup_continue(struct usb_composite_dev *cdev);
David Brownell40982be2008-06-19 17:52:58 -0700300
301
302/**
303 * struct usb_composite_device - represents one composite usb gadget
304 * @gadget: read-only, abstracts the gadget's usb peripheral controller
305 * @req: used for control responses; buffer is pre-allocated
306 * @bufsiz: size of buffer pre-allocated in @req
307 * @config: the currently active configuration
308 *
309 * One of these devices is allocated and initialized before the
310 * associated device driver's bind() is called.
311 *
312 * OPEN ISSUE: it appears that some WUSB devices will need to be
313 * built by combining a normal (wired) gadget with a wireless one.
314 * This revision of the gadget framework should probably try to make
315 * sure doing that won't hurt too much.
316 *
317 * One notion for how to handle Wireless USB devices involves:
318 * (a) a second gadget here, discovery mechanism TBD, but likely
319 * needing separate "register/unregister WUSB gadget" calls;
320 * (b) updates to usb_gadget to include flags "is it wireless",
321 * "is it wired", plus (presumably in a wrapper structure)
322 * bandgroup and PHY info;
323 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
324 * wireless-specific parameters like maxburst and maxsequence;
325 * (d) configurations that are specific to wireless links;
326 * (e) function drivers that understand wireless configs and will
327 * support wireless for (additional) function instances;
328 * (f) a function to support association setup (like CBAF), not
329 * necessarily requiring a wireless adapter;
330 * (g) composite device setup that can create one or more wireless
331 * configs, including appropriate association setup support;
332 * (h) more, TBD.
333 */
334struct usb_composite_dev {
335 struct usb_gadget *gadget;
336 struct usb_request *req;
337 unsigned bufsiz;
338
339 struct usb_configuration *config;
340
Randy Dunlapcac85a82009-04-29 21:04:19 -0700341 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700342 /* internals */
Fabien Chouteauf48cf802010-04-23 14:21:26 +0200343 unsigned int suspended:1;
David Brownell40982be2008-06-19 17:52:58 -0700344 struct usb_device_descriptor desc;
345 struct list_head configs;
346 struct usb_composite_driver *driver;
347 u8 next_string_id;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200348 u8 manufacturer_override;
349 u8 product_override;
350 u8 serial_override;
David Brownell40982be2008-06-19 17:52:58 -0700351
David Brownell60beed92008-08-18 17:38:22 -0700352 /* the gadget driver won't enable the data pullup
353 * while the deactivation count is nonzero.
354 */
355 unsigned deactivations;
David Brownell40982be2008-06-19 17:52:58 -0700356
Roger Quadros1b9ba002011-05-09 13:08:06 +0300357 /* the composite driver won't complete the control transfer's
358 * data/status stages till delayed_status is zero.
359 */
360 int delayed_status;
361
362 /* protects deactivations and delayed_status counts*/
David Brownell60beed92008-08-18 17:38:22 -0700363 spinlock_t lock;
David Brownell40982be2008-06-19 17:52:58 -0700364};
365
366extern int usb_string_id(struct usb_composite_dev *c);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200367extern int usb_string_ids_tab(struct usb_composite_dev *c,
368 struct usb_string *str);
369extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
370
David Brownell40982be2008-06-19 17:52:58 -0700371
372/* messaging utils */
373#define DBG(d, fmt, args...) \
374 dev_dbg(&(d)->gadget->dev , fmt , ## args)
375#define VDBG(d, fmt, args...) \
376 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
377#define ERROR(d, fmt, args...) \
378 dev_err(&(d)->gadget->dev , fmt , ## args)
Arjan van de Venb6c63932008-07-25 01:45:52 -0700379#define WARNING(d, fmt, args...) \
David Brownell40982be2008-06-19 17:52:58 -0700380 dev_warn(&(d)->gadget->dev , fmt , ## args)
381#define INFO(d, fmt, args...) \
382 dev_info(&(d)->gadget->dev , fmt , ## args)
383
384#endif /* __LINUX_USB_COMPOSITE_H */