blob: 0b2d2919edb266358d27d68cbadd40b6a35e3fe8 [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>
Mike Lockwoode2dc5032010-06-23 08:20:59 -040039#include <linux/switch.h>
David Brownell40982be2008-06-19 17:52:58 -070040
Roger Quadros1b9ba002011-05-09 13:08:06 +030041/*
42 * USB function drivers should return USB_GADGET_DELAYED_STATUS if they
43 * wish to delay the data/status stages of the control transfer till they
44 * are ready. The control transfer will then be kept from completing till
45 * all the function drivers that requested for USB_GADGET_DELAYED_STAUS
46 * invoke usb_composite_setup_continue().
47 */
48#define USB_GADGET_DELAYED_STATUS 0x7fff /* Impossibly large value */
David Brownell40982be2008-06-19 17:52:58 -070049
50struct usb_configuration;
51
52/**
53 * struct usb_function - describes one function of a configuration
54 * @name: For diagnostics, identifies the function.
55 * @strings: tables of strings, keyed by identifiers assigned during bind()
56 * and by language IDs provided in control requests
57 * @descriptors: Table of full (or low) speed descriptors, using interface and
58 * string identifiers assigned during @bind(). If this pointer is null,
59 * the function will not be available at full speed (or at low speed).
60 * @hs_descriptors: Table of high speed descriptors, using interface and
61 * string identifiers assigned during @bind(). If this pointer is null,
62 * the function will not be available at high speed.
63 * @config: assigned when @usb_add_function() is called; this is the
64 * configuration with which this function is associated.
65 * @bind: Before the gadget can register, all of its functions bind() to the
66 * available resources including string and interface identifiers used
67 * in interface or class descriptors; endpoints; I/O buffers; and so on.
68 * @unbind: Reverses @bind; called as a side effect of unregistering the
69 * driver which added this function.
70 * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
71 * initialize usb_ep.driver data at this time (when it is used).
72 * Note that setting an interface to its current altsetting resets
73 * interface state, and that all interfaces have a disabled state.
74 * @get_alt: Returns the active altsetting. If this is not provided,
75 * then only altsetting zero is supported.
76 * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
77 * include host resetting or reconfiguring the gadget, and disconnection.
78 * @setup: Used for interface-specific control requests.
79 * @suspend: Notifies functions when the host stops sending USB traffic.
80 * @resume: Notifies functions when the host restarts USB traffic.
81 *
82 * A single USB function uses one or more interfaces, and should in most
83 * cases support operation at both full and high speeds. Each function is
84 * associated by @usb_add_function() with a one configuration; that function
85 * causes @bind() to be called so resources can be allocated as part of
86 * setting up a gadget driver. Those resources include endpoints, which
87 * should be allocated using @usb_ep_autoconfig().
88 *
89 * To support dual speed operation, a function driver provides descriptors
90 * for both high and full speed operation. Except in rare cases that don't
91 * involve bulk endpoints, each speed needs different endpoint descriptors.
92 *
93 * Function drivers choose their own strategies for managing instance data.
94 * The simplest strategy just declares it "static', which means the function
95 * can only be activated once. If the function needs to be exposed in more
96 * than one configuration at a given speed, it needs to support multiple
97 * usb_function structures (one for each configuration).
98 *
99 * A more complex strategy might encapsulate a @usb_function structure inside
100 * a driver-specific instance structure to allows multiple activations. An
101 * example of multiple activations might be a CDC ACM function that supports
102 * two or more distinct instances within the same configuration, providing
103 * several independent logical data links to a USB host.
104 */
105struct usb_function {
106 const char *name;
107 struct usb_gadget_strings **strings;
108 struct usb_descriptor_header **descriptors;
109 struct usb_descriptor_header **hs_descriptors;
110
111 struct usb_configuration *config;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400112
113 /* disabled is zero if the function is enabled */
114 int disabled;
David Brownell40982be2008-06-19 17:52:58 -0700115
116 /* REVISIT: bind() functions can be marked __init, which
117 * makes trouble for section mismatch analysis. See if
118 * we can't restructure things to avoid mismatching.
119 * Related: unbind() may kfree() but bind() won't...
120 */
121
122 /* configuration management: bind/unbind */
123 int (*bind)(struct usb_configuration *,
124 struct usb_function *);
125 void (*unbind)(struct usb_configuration *,
126 struct usb_function *);
127
128 /* runtime state management */
129 int (*set_alt)(struct usb_function *,
130 unsigned interface, unsigned alt);
131 int (*get_alt)(struct usb_function *,
132 unsigned interface);
133 void (*disable)(struct usb_function *);
134 int (*setup)(struct usb_function *,
135 const struct usb_ctrlrequest *);
136 void (*suspend)(struct usb_function *);
137 void (*resume)(struct usb_function *);
138
Randy Dunlapcac85a82009-04-29 21:04:19 -0700139 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700140 /* internals */
141 struct list_head list;
Laurent Pinchart52426582009-10-21 00:03:38 +0200142 DECLARE_BITMAP(endpoints, 32);
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500143 struct device *dev;
David Brownell40982be2008-06-19 17:52:58 -0700144};
145
146int usb_add_function(struct usb_configuration *, struct usb_function *);
147
David Brownell60beed92008-08-18 17:38:22 -0700148int usb_function_deactivate(struct usb_function *);
149int usb_function_activate(struct usb_function *);
150
David Brownell40982be2008-06-19 17:52:58 -0700151int usb_interface_id(struct usb_configuration *, struct usb_function *);
152
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400153void usb_function_set_enabled(struct usb_function *, int);
154
David Brownell40982be2008-06-19 17:52:58 -0700155/**
156 * ep_choose - select descriptor endpoint at current device speed
157 * @g: gadget, connected and running at some speed
158 * @hs: descriptor to use for high speed operation
159 * @fs: descriptor to use for full or low speed operation
160 */
161static inline struct usb_endpoint_descriptor *
162ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
163 struct usb_endpoint_descriptor *fs)
164{
165 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
166 return hs;
167 return fs;
168}
169
170#define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
171
172/**
173 * struct usb_configuration - represents one gadget configuration
174 * @label: For diagnostics, describes the configuration.
175 * @strings: Tables of strings, keyed by identifiers assigned during @bind()
176 * and by language IDs provided in control requests.
177 * @descriptors: Table of descriptors preceding all function descriptors.
178 * Examples include OTG and vendor-specific descriptors.
David Brownell40982be2008-06-19 17:52:58 -0700179 * @unbind: Reverses @bind; called as a side effect of unregistering the
180 * driver which added this configuration.
181 * @setup: Used to delegate control requests that aren't handled by standard
182 * device infrastructure or directed at a specific interface.
183 * @bConfigurationValue: Copied into configuration descriptor.
184 * @iConfiguration: Copied into configuration descriptor.
185 * @bmAttributes: Copied into configuration descriptor.
186 * @bMaxPower: Copied into configuration descriptor.
187 * @cdev: assigned by @usb_add_config() before calling @bind(); this is
188 * the device associated with this configuration.
189 *
190 * Configurations are building blocks for gadget drivers structured around
191 * function drivers. Simple USB gadgets require only one function and one
192 * configuration, and handle dual-speed hardware by always providing the same
193 * functionality. Slightly more complex gadgets may have more than one
194 * single-function configuration at a given speed; or have configurations
195 * that only work at one speed.
196 *
197 * Composite devices are, by definition, ones with configurations which
198 * include more than one function.
199 *
200 * The lifecycle of a usb_configuration includes allocation, initialization
201 * of the fields described above, and calling @usb_add_config() to set up
202 * internal data and bind it to a specific device. The configuration's
203 * @bind() method is then used to initialize all the functions and then
204 * call @usb_add_function() for them.
205 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300206 * Those functions would normally be independent of each other, but that's
David Brownell40982be2008-06-19 17:52:58 -0700207 * not mandatory. CDC WMC devices are an example where functions often
208 * depend on other functions, with some functions subsidiary to others.
209 * Such interdependency may be managed in any way, so long as all of the
210 * descriptors complete by the time the composite driver returns from
211 * its bind() routine.
212 */
213struct usb_configuration {
214 const char *label;
215 struct usb_gadget_strings **strings;
216 const struct usb_descriptor_header **descriptors;
217
218 /* REVISIT: bind() functions can be marked __init, which
219 * makes trouble for section mismatch analysis. See if
220 * we can't restructure things to avoid mismatching...
221 */
222
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200223 /* configuration management: unbind/setup */
David Brownell40982be2008-06-19 17:52:58 -0700224 void (*unbind)(struct usb_configuration *);
225 int (*setup)(struct usb_configuration *,
226 const struct usb_ctrlrequest *);
227
228 /* fields in the config descriptor */
229 u8 bConfigurationValue;
230 u8 iConfiguration;
231 u8 bmAttributes;
232 u8 bMaxPower;
233
234 struct usb_composite_dev *cdev;
235
Randy Dunlapcac85a82009-04-29 21:04:19 -0700236 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700237 /* internals */
238 struct list_head list;
239 struct list_head functions;
240 u8 next_interface_id;
241 unsigned highspeed:1;
242 unsigned fullspeed:1;
243 struct usb_function *interface[MAX_CONFIG_INTERFACES];
244};
245
246int usb_add_config(struct usb_composite_dev *,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200247 struct usb_configuration *,
248 int (*)(struct usb_configuration *));
David Brownell40982be2008-06-19 17:52:58 -0700249
250/**
251 * struct usb_composite_driver - groups configurations into a gadget
252 * @name: For diagnostics, identifies the driver.
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200253 * @iProduct: Used as iProduct override if @dev->iProduct is not set.
254 * If NULL value of @name is taken.
255 * @iManufacturer: Used as iManufacturer override if @dev->iManufacturer is
256 * not set. If NULL a default "<system> <release> with <udc>" value
257 * will be used.
David Brownell40982be2008-06-19 17:52:58 -0700258 * @dev: Template descriptor for the device, including default device
259 * identifiers.
260 * @strings: tables of strings, keyed by identifiers assigned during bind()
261 * and language IDs provided in control requests
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200262 * @needs_serial: set to 1 if the gadget needs userspace to provide
263 * a serial number. If one is not provided, warning will be printed.
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200264 * @unbind: Reverses bind; called as a side effect of unregistering
David Brownell40982be2008-06-19 17:52:58 -0700265 * this driver.
Randy Dunlapd187abb2010-08-11 12:07:13 -0700266 * @disconnect: optional driver disconnect method
David Brownell89429392009-03-19 14:14:17 -0700267 * @suspend: Notifies when the host stops sending USB traffic,
268 * after function notifications
269 * @resume: Notifies configuration when the host restarts USB traffic,
270 * before function notifications
David Brownell40982be2008-06-19 17:52:58 -0700271 *
272 * Devices default to reporting self powered operation. Devices which rely
273 * on bus powered operation should report this in their @bind() method.
274 *
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200275 * Before returning from bind, various fields in the template descriptor
David Brownell40982be2008-06-19 17:52:58 -0700276 * may be overridden. These include the idVendor/idProduct/bcdDevice values
277 * normally to bind the appropriate host side driver, and the three strings
278 * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
279 * meaningful device identifiers. (The strings will not be defined unless
280 * they are defined in @dev and @strings.) The correct ep0 maxpacket size
281 * is also reported, as defined by the underlying controller driver.
282 */
283struct usb_composite_driver {
284 const char *name;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200285 const char *iProduct;
286 const char *iManufacturer;
David Brownell40982be2008-06-19 17:52:58 -0700287 const struct usb_device_descriptor *dev;
288 struct usb_gadget_strings **strings;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200289 unsigned needs_serial:1;
David Brownell40982be2008-06-19 17:52:58 -0700290
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500291 struct class *class;
292 atomic_t function_count;
293
David Brownell40982be2008-06-19 17:52:58 -0700294 int (*unbind)(struct usb_composite_dev *);
David Brownell89429392009-03-19 14:14:17 -0700295
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +0200296 void (*disconnect)(struct usb_composite_dev *);
297
David Brownell89429392009-03-19 14:14:17 -0700298 /* global suspend hooks */
299 void (*suspend)(struct usb_composite_dev *);
300 void (*resume)(struct usb_composite_dev *);
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500301
302 void (*enable_function)(struct usb_function *f, int enable);
David Brownell40982be2008-06-19 17:52:58 -0700303};
304
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200305extern int usb_composite_probe(struct usb_composite_driver *driver,
306 int (*bind)(struct usb_composite_dev *cdev));
307extern void usb_composite_unregister(struct usb_composite_driver *driver);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300308extern void usb_composite_setup_continue(struct usb_composite_dev *cdev);
David Brownell40982be2008-06-19 17:52:58 -0700309
310
311/**
312 * struct usb_composite_device - represents one composite usb gadget
313 * @gadget: read-only, abstracts the gadget's usb peripheral controller
314 * @req: used for control responses; buffer is pre-allocated
315 * @bufsiz: size of buffer pre-allocated in @req
316 * @config: the currently active configuration
317 *
318 * One of these devices is allocated and initialized before the
319 * associated device driver's bind() is called.
320 *
321 * OPEN ISSUE: it appears that some WUSB devices will need to be
322 * built by combining a normal (wired) gadget with a wireless one.
323 * This revision of the gadget framework should probably try to make
324 * sure doing that won't hurt too much.
325 *
326 * One notion for how to handle Wireless USB devices involves:
327 * (a) a second gadget here, discovery mechanism TBD, but likely
328 * needing separate "register/unregister WUSB gadget" calls;
329 * (b) updates to usb_gadget to include flags "is it wireless",
330 * "is it wired", plus (presumably in a wrapper structure)
331 * bandgroup and PHY info;
332 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
333 * wireless-specific parameters like maxburst and maxsequence;
334 * (d) configurations that are specific to wireless links;
335 * (e) function drivers that understand wireless configs and will
336 * support wireless for (additional) function instances;
337 * (f) a function to support association setup (like CBAF), not
338 * necessarily requiring a wireless adapter;
339 * (g) composite device setup that can create one or more wireless
340 * configs, including appropriate association setup support;
341 * (h) more, TBD.
342 */
343struct usb_composite_dev {
344 struct usb_gadget *gadget;
345 struct usb_request *req;
346 unsigned bufsiz;
347
348 struct usb_configuration *config;
349
Randy Dunlapcac85a82009-04-29 21:04:19 -0700350 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700351 /* internals */
Fabien Chouteauf48cf802010-04-23 14:21:26 +0200352 unsigned int suspended:1;
David Brownell40982be2008-06-19 17:52:58 -0700353 struct usb_device_descriptor desc;
354 struct list_head configs;
355 struct usb_composite_driver *driver;
356 u8 next_string_id;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200357 u8 manufacturer_override;
358 u8 product_override;
359 u8 serial_override;
David Brownell40982be2008-06-19 17:52:58 -0700360
David Brownell60beed92008-08-18 17:38:22 -0700361 /* the gadget driver won't enable the data pullup
362 * while the deactivation count is nonzero.
363 */
364 unsigned deactivations;
David Brownell40982be2008-06-19 17:52:58 -0700365
Roger Quadros1b9ba002011-05-09 13:08:06 +0300366 /* the composite driver won't complete the control transfer's
367 * data/status stages till delayed_status is zero.
368 */
369 int delayed_status;
370
371 /* protects deactivations and delayed_status counts*/
David Brownell60beed92008-08-18 17:38:22 -0700372 spinlock_t lock;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400373
374 struct switch_dev sdev;
David Brownell40982be2008-06-19 17:52:58 -0700375};
376
377extern int usb_string_id(struct usb_composite_dev *c);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200378extern int usb_string_ids_tab(struct usb_composite_dev *c,
379 struct usb_string *str);
380extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
381
David Brownell40982be2008-06-19 17:52:58 -0700382
383/* messaging utils */
384#define DBG(d, fmt, args...) \
385 dev_dbg(&(d)->gadget->dev , fmt , ## args)
386#define VDBG(d, fmt, args...) \
387 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
388#define ERROR(d, fmt, args...) \
389 dev_err(&(d)->gadget->dev , fmt , ## args)
Arjan van de Venb6c63932008-07-25 01:45:52 -0700390#define WARNING(d, fmt, args...) \
David Brownell40982be2008-06-19 17:52:58 -0700391 dev_warn(&(d)->gadget->dev , fmt , ## args)
392#define INFO(d, fmt, args...) \
393 dev_info(&(d)->gadget->dev , fmt , ## args)
394
395#endif /* __LINUX_USB_COMPOSITE_H */