Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / include / linux / usb / composite.h
bloba316fba73518f368244baa3efdac87435faaa6df
1 /*
2 * composite.h -- framework for usb gadgets which are composite devices
4 * Copyright (C) 2006-2008 David Brownell
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.
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.
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
21 #ifndef __LINUX_USB_COMPOSITE_H
22 #define __LINUX_USB_COMPOSITE_H
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.
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.
37 #include <linux/usb/ch9.h>
38 #include <linux/usb/gadget.h>
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().
47 #define USB_GADGET_DELAYED_STATUS 0x7fff /* Impossibly large value */
49 struct usb_configuration;
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 * @ss_descriptors: Table of super speed descriptors, using interface and
63 * string identifiers assigned during @bind(). If this
64 * pointer is null after initiation, the function will not
65 * be available at super speed.
66 * @config: assigned when @usb_add_function() is called; this is the
67 * configuration with which this function is associated.
68 * @bind: Before the gadget can register, all of its functions bind() to the
69 * available resources including string and interface identifiers used
70 * in interface or class descriptors; endpoints; I/O buffers; and so on.
71 * @unbind: Reverses @bind; called as a side effect of unregistering the
72 * driver which added this function.
73 * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
74 * initialize usb_ep.driver data at this time (when it is used).
75 * Note that setting an interface to its current altsetting resets
76 * interface state, and that all interfaces have a disabled state.
77 * @get_alt: Returns the active altsetting. If this is not provided,
78 * then only altsetting zero is supported.
79 * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
80 * include host resetting or reconfiguring the gadget, and disconnection.
81 * @setup: Used for interface-specific control requests.
82 * @suspend: Notifies functions when the host stops sending USB traffic.
83 * @resume: Notifies functions when the host restarts USB traffic.
84 * @get_status: Returns function status as a reply to
85 * GetStatus() request when the recepient is Interface.
86 * @func_suspend: callback to be called when
87 * SetFeature(FUNCTION_SUSPEND) is reseived
89 * A single USB function uses one or more interfaces, and should in most
90 * cases support operation at both full and high speeds. Each function is
91 * associated by @usb_add_function() with a one configuration; that function
92 * causes @bind() to be called so resources can be allocated as part of
93 * setting up a gadget driver. Those resources include endpoints, which
94 * should be allocated using @usb_ep_autoconfig().
96 * To support dual speed operation, a function driver provides descriptors
97 * for both high and full speed operation. Except in rare cases that don't
98 * involve bulk endpoints, each speed needs different endpoint descriptors.
100 * Function drivers choose their own strategies for managing instance data.
101 * The simplest strategy just declares it "static', which means the function
102 * can only be activated once. If the function needs to be exposed in more
103 * than one configuration at a given speed, it needs to support multiple
104 * usb_function structures (one for each configuration).
106 * A more complex strategy might encapsulate a @usb_function structure inside
107 * a driver-specific instance structure to allows multiple activations. An
108 * example of multiple activations might be a CDC ACM function that supports
109 * two or more distinct instances within the same configuration, providing
110 * several independent logical data links to a USB host.
112 struct usb_function {
113 const char *name;
114 struct usb_gadget_strings **strings;
115 struct usb_descriptor_header **descriptors;
116 struct usb_descriptor_header **hs_descriptors;
117 struct usb_descriptor_header **ss_descriptors;
119 struct usb_configuration *config;
121 /* REVISIT: bind() functions can be marked __init, which
122 * makes trouble for section mismatch analysis. See if
123 * we can't restructure things to avoid mismatching.
124 * Related: unbind() may kfree() but bind() won't...
127 /* configuration management: bind/unbind */
128 int (*bind)(struct usb_configuration *,
129 struct usb_function *);
130 void (*unbind)(struct usb_configuration *,
131 struct usb_function *);
133 /* runtime state management */
134 int (*set_alt)(struct usb_function *,
135 unsigned interface, unsigned alt);
136 int (*get_alt)(struct usb_function *,
137 unsigned interface);
138 void (*disable)(struct usb_function *);
139 int (*setup)(struct usb_function *,
140 const struct usb_ctrlrequest *);
141 void (*suspend)(struct usb_function *);
142 void (*resume)(struct usb_function *);
144 /* USB 3.0 additions */
145 int (*get_status)(struct usb_function *);
146 int (*func_suspend)(struct usb_function *,
147 u8 suspend_opt);
148 /* private: */
149 /* internals */
150 struct list_head list;
151 DECLARE_BITMAP(endpoints, 32);
154 int usb_add_function(struct usb_configuration *, struct usb_function *);
156 int usb_function_deactivate(struct usb_function *);
157 int usb_function_activate(struct usb_function *);
159 int usb_interface_id(struct usb_configuration *, struct usb_function *);
161 int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f,
162 struct usb_ep *_ep);
164 #define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
167 * struct usb_configuration - represents one gadget configuration
168 * @label: For diagnostics, describes the configuration.
169 * @strings: Tables of strings, keyed by identifiers assigned during @bind()
170 * and by language IDs provided in control requests.
171 * @descriptors: Table of descriptors preceding all function descriptors.
172 * Examples include OTG and vendor-specific descriptors.
173 * @unbind: Reverses @bind; called as a side effect of unregistering the
174 * driver which added this configuration.
175 * @setup: Used to delegate control requests that aren't handled by standard
176 * device infrastructure or directed at a specific interface.
177 * @bConfigurationValue: Copied into configuration descriptor.
178 * @iConfiguration: Copied into configuration descriptor.
179 * @bmAttributes: Copied into configuration descriptor.
180 * @bMaxPower: Copied into configuration descriptor.
181 * @cdev: assigned by @usb_add_config() before calling @bind(); this is
182 * the device associated with this configuration.
184 * Configurations are building blocks for gadget drivers structured around
185 * function drivers. Simple USB gadgets require only one function and one
186 * configuration, and handle dual-speed hardware by always providing the same
187 * functionality. Slightly more complex gadgets may have more than one
188 * single-function configuration at a given speed; or have configurations
189 * that only work at one speed.
191 * Composite devices are, by definition, ones with configurations which
192 * include more than one function.
194 * The lifecycle of a usb_configuration includes allocation, initialization
195 * of the fields described above, and calling @usb_add_config() to set up
196 * internal data and bind it to a specific device. The configuration's
197 * @bind() method is then used to initialize all the functions and then
198 * call @usb_add_function() for them.
200 * Those functions would normally be independent of each other, but that's
201 * not mandatory. CDC WMC devices are an example where functions often
202 * depend on other functions, with some functions subsidiary to others.
203 * Such interdependency may be managed in any way, so long as all of the
204 * descriptors complete by the time the composite driver returns from
205 * its bind() routine.
207 struct usb_configuration {
208 const char *label;
209 struct usb_gadget_strings **strings;
210 const struct usb_descriptor_header **descriptors;
212 /* REVISIT: bind() functions can be marked __init, which
213 * makes trouble for section mismatch analysis. See if
214 * we can't restructure things to avoid mismatching...
217 /* configuration management: unbind/setup */
218 void (*unbind)(struct usb_configuration *);
219 int (*setup)(struct usb_configuration *,
220 const struct usb_ctrlrequest *);
222 /* fields in the config descriptor */
223 u8 bConfigurationValue;
224 u8 iConfiguration;
225 u8 bmAttributes;
226 u8 bMaxPower;
228 struct usb_composite_dev *cdev;
230 /* private: */
231 /* internals */
232 struct list_head list;
233 struct list_head functions;
234 u8 next_interface_id;
235 unsigned superspeed:1;
236 unsigned highspeed:1;
237 unsigned fullspeed:1;
238 struct usb_function *interface[MAX_CONFIG_INTERFACES];
241 int usb_add_config(struct usb_composite_dev *,
242 struct usb_configuration *,
243 int (*)(struct usb_configuration *));
246 * struct usb_composite_driver - groups configurations into a gadget
247 * @name: For diagnostics, identifies the driver.
248 * @iProduct: Used as iProduct override if @dev->iProduct is not set.
249 * If NULL value of @name is taken.
250 * @iManufacturer: Used as iManufacturer override if @dev->iManufacturer is
251 * not set. If NULL a default "<system> <release> with <udc>" value
252 * will be used.
253 * @dev: Template descriptor for the device, including default device
254 * identifiers.
255 * @strings: tables of strings, keyed by identifiers assigned during bind()
256 * and language IDs provided in control requests
257 * @max_speed: Highest speed the driver supports.
258 * @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.
260 * @unbind: Reverses bind; called as a side effect of unregistering
261 * this driver.
262 * @disconnect: optional driver disconnect method
263 * @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
268 * Devices default to reporting self powered operation. Devices which rely
269 * on bus powered operation should report this in their @bind() method.
271 * Before returning from bind, various fields in the template descriptor
272 * 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.
279 struct usb_composite_driver {
280 const char *name;
281 const char *iProduct;
282 const char *iManufacturer;
283 const struct usb_device_descriptor *dev;
284 struct usb_gadget_strings **strings;
285 enum usb_device_speed max_speed;
286 unsigned needs_serial:1;
288 int (*unbind)(struct usb_composite_dev *);
290 void (*disconnect)(struct usb_composite_dev *);
292 /* global suspend hooks */
293 void (*suspend)(struct usb_composite_dev *);
294 void (*resume)(struct usb_composite_dev *);
297 extern int usb_composite_probe(struct usb_composite_driver *driver,
298 int (*bind)(struct usb_composite_dev *cdev));
299 extern void usb_composite_unregister(struct usb_composite_driver *driver);
300 extern void usb_composite_setup_continue(struct usb_composite_dev *cdev);
304 * struct usb_composite_device - represents one composite usb gadget
305 * @gadget: read-only, abstracts the gadget's usb peripheral controller
306 * @req: used for control responses; buffer is pre-allocated
307 * @bufsiz: size of buffer pre-allocated in @req
308 * @config: the currently active configuration
310 * One of these devices is allocated and initialized before the
311 * associated device driver's bind() is called.
313 * OPEN ISSUE: it appears that some WUSB devices will need to be
314 * built by combining a normal (wired) gadget with a wireless one.
315 * This revision of the gadget framework should probably try to make
316 * sure doing that won't hurt too much.
318 * One notion for how to handle Wireless USB devices involves:
319 * (a) a second gadget here, discovery mechanism TBD, but likely
320 * needing separate "register/unregister WUSB gadget" calls;
321 * (b) updates to usb_gadget to include flags "is it wireless",
322 * "is it wired", plus (presumably in a wrapper structure)
323 * bandgroup and PHY info;
324 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
325 * wireless-specific parameters like maxburst and maxsequence;
326 * (d) configurations that are specific to wireless links;
327 * (e) function drivers that understand wireless configs and will
328 * support wireless for (additional) function instances;
329 * (f) a function to support association setup (like CBAF), not
330 * necessarily requiring a wireless adapter;
331 * (g) composite device setup that can create one or more wireless
332 * configs, including appropriate association setup support;
333 * (h) more, TBD.
335 struct usb_composite_dev {
336 struct usb_gadget *gadget;
337 struct usb_request *req;
338 unsigned bufsiz;
340 struct usb_configuration *config;
342 /* private: */
343 /* internals */
344 unsigned int suspended:1;
345 struct usb_device_descriptor desc;
346 struct list_head configs;
347 struct usb_composite_driver *driver;
348 u8 next_string_id;
349 u8 manufacturer_override;
350 u8 product_override;
351 u8 serial_override;
353 /* the gadget driver won't enable the data pullup
354 * while the deactivation count is nonzero.
356 unsigned deactivations;
358 /* the composite driver won't complete the control transfer's
359 * data/status stages till delayed_status is zero.
361 int delayed_status;
363 /* protects deactivations and delayed_status counts*/
364 spinlock_t lock;
367 extern int usb_string_id(struct usb_composite_dev *c);
368 extern int usb_string_ids_tab(struct usb_composite_dev *c,
369 struct usb_string *str);
370 extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
373 /* messaging utils */
374 #define DBG(d, fmt, args...) \
375 dev_dbg(&(d)->gadget->dev , fmt , ## args)
376 #define VDBG(d, fmt, args...) \
377 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
378 #define ERROR(d, fmt, args...) \
379 dev_err(&(d)->gadget->dev , fmt , ## args)
380 #define WARNING(d, fmt, args...) \
381 dev_warn(&(d)->gadget->dev , fmt , ## args)
382 #define INFO(d, fmt, args...) \
383 dev_info(&(d)->gadget->dev , fmt , ## args)
385 #endif /* __LINUX_USB_COMPOSITE_H */