blob: 37352fc4601af58080edb1bc848960fc030ff7b3 [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
Mike Lockwood28acc1a2010-06-28 16:19:32 -040050struct usb_composite_dev;
David Brownell40982be2008-06-19 17:52:58 -070051struct usb_configuration;
52
53/**
54 * struct usb_function - describes one function of a configuration
55 * @name: For diagnostics, identifies the function.
56 * @strings: tables of strings, keyed by identifiers assigned during bind()
57 * and by language IDs provided in control requests
58 * @descriptors: Table of full (or low) speed descriptors, using interface and
59 * string identifiers assigned during @bind(). If this pointer is null,
60 * the function will not be available at full speed (or at low speed).
61 * @hs_descriptors: Table of high speed descriptors, using interface and
62 * string identifiers assigned during @bind(). If this pointer is null,
63 * the function will not be available at high speed.
64 * @config: assigned when @usb_add_function() is called; this is the
65 * configuration with which this function is associated.
66 * @bind: Before the gadget can register, all of its functions bind() to the
67 * available resources including string and interface identifiers used
68 * in interface or class descriptors; endpoints; I/O buffers; and so on.
69 * @unbind: Reverses @bind; called as a side effect of unregistering the
70 * driver which added this function.
71 * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
72 * initialize usb_ep.driver data at this time (when it is used).
73 * Note that setting an interface to its current altsetting resets
74 * interface state, and that all interfaces have a disabled state.
75 * @get_alt: Returns the active altsetting. If this is not provided,
76 * then only altsetting zero is supported.
77 * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
78 * include host resetting or reconfiguring the gadget, and disconnection.
79 * @setup: Used for interface-specific control requests.
80 * @suspend: Notifies functions when the host stops sending USB traffic.
81 * @resume: Notifies functions when the host restarts USB traffic.
82 *
83 * A single USB function uses one or more interfaces, and should in most
84 * cases support operation at both full and high speeds. Each function is
85 * associated by @usb_add_function() with a one configuration; that function
86 * causes @bind() to be called so resources can be allocated as part of
87 * setting up a gadget driver. Those resources include endpoints, which
88 * should be allocated using @usb_ep_autoconfig().
89 *
90 * To support dual speed operation, a function driver provides descriptors
91 * for both high and full speed operation. Except in rare cases that don't
92 * involve bulk endpoints, each speed needs different endpoint descriptors.
93 *
94 * Function drivers choose their own strategies for managing instance data.
95 * The simplest strategy just declares it "static', which means the function
96 * can only be activated once. If the function needs to be exposed in more
97 * than one configuration at a given speed, it needs to support multiple
98 * usb_function structures (one for each configuration).
99 *
100 * A more complex strategy might encapsulate a @usb_function structure inside
101 * a driver-specific instance structure to allows multiple activations. An
102 * example of multiple activations might be a CDC ACM function that supports
103 * two or more distinct instances within the same configuration, providing
104 * several independent logical data links to a USB host.
105 */
106struct usb_function {
107 const char *name;
108 struct usb_gadget_strings **strings;
109 struct usb_descriptor_header **descriptors;
110 struct usb_descriptor_header **hs_descriptors;
111
112 struct usb_configuration *config;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400113
114 /* disabled is zero if the function is enabled */
115 int disabled;
David Brownell40982be2008-06-19 17:52:58 -0700116
117 /* REVISIT: bind() functions can be marked __init, which
118 * makes trouble for section mismatch analysis. See if
119 * we can't restructure things to avoid mismatching.
120 * Related: unbind() may kfree() but bind() won't...
121 */
122
123 /* configuration management: bind/unbind */
124 int (*bind)(struct usb_configuration *,
125 struct usb_function *);
126 void (*unbind)(struct usb_configuration *,
127 struct usb_function *);
128
129 /* runtime state management */
130 int (*set_alt)(struct usb_function *,
131 unsigned interface, unsigned alt);
132 int (*get_alt)(struct usb_function *,
133 unsigned interface);
134 void (*disable)(struct usb_function *);
135 int (*setup)(struct usb_function *,
136 const struct usb_ctrlrequest *);
137 void (*suspend)(struct usb_function *);
138 void (*resume)(struct usb_function *);
139
Randy Dunlapcac85a82009-04-29 21:04:19 -0700140 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700141 /* internals */
142 struct list_head list;
Laurent Pinchart52426582009-10-21 00:03:38 +0200143 DECLARE_BITMAP(endpoints, 32);
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500144 struct device *dev;
David Brownell40982be2008-06-19 17:52:58 -0700145};
146
147int usb_add_function(struct usb_configuration *, struct usb_function *);
148
David Brownell60beed92008-08-18 17:38:22 -0700149int usb_function_deactivate(struct usb_function *);
150int usb_function_activate(struct usb_function *);
151
David Brownell40982be2008-06-19 17:52:58 -0700152int usb_interface_id(struct usb_configuration *, struct usb_function *);
153
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400154void usb_function_set_enabled(struct usb_function *, int);
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400155void usb_composite_force_reset(struct usb_composite_dev *);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400156
David Brownell40982be2008-06-19 17:52:58 -0700157/**
158 * ep_choose - select descriptor endpoint at current device speed
159 * @g: gadget, connected and running at some speed
160 * @hs: descriptor to use for high speed operation
161 * @fs: descriptor to use for full or low speed operation
162 */
163static inline struct usb_endpoint_descriptor *
164ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
165 struct usb_endpoint_descriptor *fs)
166{
167 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
168 return hs;
169 return fs;
170}
171
172#define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
173
174/**
175 * struct usb_configuration - represents one gadget configuration
176 * @label: For diagnostics, describes the configuration.
177 * @strings: Tables of strings, keyed by identifiers assigned during @bind()
178 * and by language IDs provided in control requests.
179 * @descriptors: Table of descriptors preceding all function descriptors.
180 * Examples include OTG and vendor-specific descriptors.
David Brownell40982be2008-06-19 17:52:58 -0700181 * @unbind: Reverses @bind; called as a side effect of unregistering the
182 * driver which added this configuration.
183 * @setup: Used to delegate control requests that aren't handled by standard
184 * device infrastructure or directed at a specific interface.
185 * @bConfigurationValue: Copied into configuration descriptor.
186 * @iConfiguration: Copied into configuration descriptor.
187 * @bmAttributes: Copied into configuration descriptor.
188 * @bMaxPower: Copied into configuration descriptor.
189 * @cdev: assigned by @usb_add_config() before calling @bind(); this is
190 * the device associated with this configuration.
191 *
192 * Configurations are building blocks for gadget drivers structured around
193 * function drivers. Simple USB gadgets require only one function and one
194 * configuration, and handle dual-speed hardware by always providing the same
195 * functionality. Slightly more complex gadgets may have more than one
196 * single-function configuration at a given speed; or have configurations
197 * that only work at one speed.
198 *
199 * Composite devices are, by definition, ones with configurations which
200 * include more than one function.
201 *
202 * The lifecycle of a usb_configuration includes allocation, initialization
203 * of the fields described above, and calling @usb_add_config() to set up
204 * internal data and bind it to a specific device. The configuration's
205 * @bind() method is then used to initialize all the functions and then
206 * call @usb_add_function() for them.
207 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300208 * Those functions would normally be independent of each other, but that's
David Brownell40982be2008-06-19 17:52:58 -0700209 * not mandatory. CDC WMC devices are an example where functions often
210 * depend on other functions, with some functions subsidiary to others.
211 * Such interdependency may be managed in any way, so long as all of the
212 * descriptors complete by the time the composite driver returns from
213 * its bind() routine.
214 */
215struct usb_configuration {
216 const char *label;
217 struct usb_gadget_strings **strings;
218 const struct usb_descriptor_header **descriptors;
219
220 /* REVISIT: bind() functions can be marked __init, which
221 * makes trouble for section mismatch analysis. See if
222 * we can't restructure things to avoid mismatching...
223 */
224
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200225 /* configuration management: unbind/setup */
David Brownell40982be2008-06-19 17:52:58 -0700226 void (*unbind)(struct usb_configuration *);
227 int (*setup)(struct usb_configuration *,
228 const struct usb_ctrlrequest *);
229
230 /* fields in the config descriptor */
231 u8 bConfigurationValue;
232 u8 iConfiguration;
233 u8 bmAttributes;
234 u8 bMaxPower;
235
236 struct usb_composite_dev *cdev;
237
Randy Dunlapcac85a82009-04-29 21:04:19 -0700238 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700239 /* internals */
240 struct list_head list;
241 struct list_head functions;
242 u8 next_interface_id;
243 unsigned highspeed:1;
244 unsigned fullspeed:1;
245 struct usb_function *interface[MAX_CONFIG_INTERFACES];
246};
247
248int usb_add_config(struct usb_composite_dev *,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200249 struct usb_configuration *,
250 int (*)(struct usb_configuration *));
David Brownell40982be2008-06-19 17:52:58 -0700251
252/**
253 * struct usb_composite_driver - groups configurations into a gadget
254 * @name: For diagnostics, identifies the driver.
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200255 * @iProduct: Used as iProduct override if @dev->iProduct is not set.
256 * If NULL value of @name is taken.
257 * @iManufacturer: Used as iManufacturer override if @dev->iManufacturer is
258 * not set. If NULL a default "<system> <release> with <udc>" value
259 * will be used.
David Brownell40982be2008-06-19 17:52:58 -0700260 * @dev: Template descriptor for the device, including default device
261 * identifiers.
262 * @strings: tables of strings, keyed by identifiers assigned during bind()
263 * and language IDs provided in control requests
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200264 * @needs_serial: set to 1 if the gadget needs userspace to provide
265 * a serial number. If one is not provided, warning will be printed.
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200266 * @unbind: Reverses bind; called as a side effect of unregistering
David Brownell40982be2008-06-19 17:52:58 -0700267 * this driver.
Randy Dunlapd187abb2010-08-11 12:07:13 -0700268 * @disconnect: optional driver disconnect method
David Brownell89429392009-03-19 14:14:17 -0700269 * @suspend: Notifies when the host stops sending USB traffic,
270 * after function notifications
271 * @resume: Notifies configuration when the host restarts USB traffic,
272 * before function notifications
David Brownell40982be2008-06-19 17:52:58 -0700273 *
274 * Devices default to reporting self powered operation. Devices which rely
275 * on bus powered operation should report this in their @bind() method.
276 *
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200277 * Before returning from bind, various fields in the template descriptor
David Brownell40982be2008-06-19 17:52:58 -0700278 * may be overridden. These include the idVendor/idProduct/bcdDevice values
279 * normally to bind the appropriate host side driver, and the three strings
280 * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
281 * meaningful device identifiers. (The strings will not be defined unless
282 * they are defined in @dev and @strings.) The correct ep0 maxpacket size
283 * is also reported, as defined by the underlying controller driver.
284 */
285struct usb_composite_driver {
286 const char *name;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200287 const char *iProduct;
288 const char *iManufacturer;
David Brownell40982be2008-06-19 17:52:58 -0700289 const struct usb_device_descriptor *dev;
290 struct usb_gadget_strings **strings;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200291 unsigned needs_serial:1;
David Brownell40982be2008-06-19 17:52:58 -0700292
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500293 struct class *class;
294 atomic_t function_count;
295
David Brownell40982be2008-06-19 17:52:58 -0700296 int (*unbind)(struct usb_composite_dev *);
David Brownell89429392009-03-19 14:14:17 -0700297
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +0200298 void (*disconnect)(struct usb_composite_dev *);
299
David Brownell89429392009-03-19 14:14:17 -0700300 /* global suspend hooks */
301 void (*suspend)(struct usb_composite_dev *);
302 void (*resume)(struct usb_composite_dev *);
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500303
304 void (*enable_function)(struct usb_function *f, int enable);
David Brownell40982be2008-06-19 17:52:58 -0700305};
306
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +0200307extern int usb_composite_probe(struct usb_composite_driver *driver,
308 int (*bind)(struct usb_composite_dev *cdev));
309extern void usb_composite_unregister(struct usb_composite_driver *driver);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300310extern void usb_composite_setup_continue(struct usb_composite_dev *cdev);
David Brownell40982be2008-06-19 17:52:58 -0700311
312
313/**
314 * struct usb_composite_device - represents one composite usb gadget
315 * @gadget: read-only, abstracts the gadget's usb peripheral controller
316 * @req: used for control responses; buffer is pre-allocated
317 * @bufsiz: size of buffer pre-allocated in @req
318 * @config: the currently active configuration
319 *
320 * One of these devices is allocated and initialized before the
321 * associated device driver's bind() is called.
322 *
323 * OPEN ISSUE: it appears that some WUSB devices will need to be
324 * built by combining a normal (wired) gadget with a wireless one.
325 * This revision of the gadget framework should probably try to make
326 * sure doing that won't hurt too much.
327 *
328 * One notion for how to handle Wireless USB devices involves:
329 * (a) a second gadget here, discovery mechanism TBD, but likely
330 * needing separate "register/unregister WUSB gadget" calls;
331 * (b) updates to usb_gadget to include flags "is it wireless",
332 * "is it wired", plus (presumably in a wrapper structure)
333 * bandgroup and PHY info;
334 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
335 * wireless-specific parameters like maxburst and maxsequence;
336 * (d) configurations that are specific to wireless links;
337 * (e) function drivers that understand wireless configs and will
338 * support wireless for (additional) function instances;
339 * (f) a function to support association setup (like CBAF), not
340 * necessarily requiring a wireless adapter;
341 * (g) composite device setup that can create one or more wireless
342 * configs, including appropriate association setup support;
343 * (h) more, TBD.
344 */
345struct usb_composite_dev {
346 struct usb_gadget *gadget;
347 struct usb_request *req;
348 unsigned bufsiz;
349
350 struct usb_configuration *config;
351
Randy Dunlapcac85a82009-04-29 21:04:19 -0700352 /* private: */
David Brownell40982be2008-06-19 17:52:58 -0700353 /* internals */
Fabien Chouteauf48cf802010-04-23 14:21:26 +0200354 unsigned int suspended:1;
David Brownell40982be2008-06-19 17:52:58 -0700355 struct usb_device_descriptor desc;
356 struct list_head configs;
357 struct usb_composite_driver *driver;
358 u8 next_string_id;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200359 u8 manufacturer_override;
360 u8 product_override;
361 u8 serial_override;
David Brownell40982be2008-06-19 17:52:58 -0700362
David Brownell60beed92008-08-18 17:38:22 -0700363 /* the gadget driver won't enable the data pullup
364 * while the deactivation count is nonzero.
365 */
366 unsigned deactivations;
David Brownell40982be2008-06-19 17:52:58 -0700367
Roger Quadros1b9ba002011-05-09 13:08:06 +0300368 /* the composite driver won't complete the control transfer's
369 * data/status stages till delayed_status is zero.
370 */
371 int delayed_status;
372
373 /* protects deactivations and delayed_status counts*/
David Brownell60beed92008-08-18 17:38:22 -0700374 spinlock_t lock;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400375
376 struct switch_dev sdev;
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400377 /* used by usb_composite_force_reset to avoid signalling switch changes */
378 bool mute_switch;
Mike Lockwood8da4cc82010-06-27 20:05:55 -0400379 struct work_struct switch_work;
David Brownell40982be2008-06-19 17:52:58 -0700380};
381
382extern int usb_string_id(struct usb_composite_dev *c);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200383extern int usb_string_ids_tab(struct usb_composite_dev *c,
384 struct usb_string *str);
385extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
386
David Brownell40982be2008-06-19 17:52:58 -0700387
388/* messaging utils */
389#define DBG(d, fmt, args...) \
390 dev_dbg(&(d)->gadget->dev , fmt , ## args)
391#define VDBG(d, fmt, args...) \
392 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
393#define ERROR(d, fmt, args...) \
394 dev_err(&(d)->gadget->dev , fmt , ## args)
Arjan van de Venb6c63932008-07-25 01:45:52 -0700395#define WARNING(d, fmt, args...) \
David Brownell40982be2008-06-19 17:52:58 -0700396 dev_warn(&(d)->gadget->dev , fmt , ## args)
397#define INFO(d, fmt, args...) \
398 dev_info(&(d)->gadget->dev , fmt , ## args)
399
400#endif /* __LINUX_USB_COMPOSITE_H */