usb: gadget: composite: Add usb_remove_config
[deliverable/linux.git] / include / linux / usb / composite.h
CommitLineData
40982be5
DB
1/*
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
1b9ba000
RQ
40/*
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 */
40982be5
DB
48
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.
bdb64d72
TB
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.
40982be5
DB
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.
bdb64d72
TB
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
40982be5
DB
88 *
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().
95 *
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.
99 *
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).
105 *
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.
111 */
112struct 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;
bdb64d72 117 struct usb_descriptor_header **ss_descriptors;
40982be5
DB
118
119 struct usb_configuration *config;
120
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...
125 */
126
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 *);
132
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 *);
143
bdb64d72
TB
144 /* USB 3.0 additions */
145 int (*get_status)(struct usb_function *);
146 int (*func_suspend)(struct usb_function *,
147 u8 suspend_opt);
cac85a8b 148 /* private: */
40982be5
DB
149 /* internals */
150 struct list_head list;
5242658d 151 DECLARE_BITMAP(endpoints, 32);
40982be5
DB
152};
153
154int usb_add_function(struct usb_configuration *, struct usb_function *);
155
60beed95
DB
156int usb_function_deactivate(struct usb_function *);
157int usb_function_activate(struct usb_function *);
158
40982be5
DB
159int usb_interface_id(struct usb_configuration *, struct usb_function *);
160
48767a4e
TB
161int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f,
162 struct usb_ep *_ep);
163
40982be5
DB
164#define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
165
166/**
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.
40982be5
DB
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.
183 *
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.
190 *
191 * Composite devices are, by definition, ones with configurations which
192 * include more than one function.
193 *
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.
199 *
25985edc 200 * Those functions would normally be independent of each other, but that's
40982be5
DB
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.
206 */
207struct usb_configuration {
208 const char *label;
209 struct usb_gadget_strings **strings;
210 const struct usb_descriptor_header **descriptors;
211
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...
215 */
216
c9bfff9c 217 /* configuration management: unbind/setup */
40982be5
DB
218 void (*unbind)(struct usb_configuration *);
219 int (*setup)(struct usb_configuration *,
220 const struct usb_ctrlrequest *);
221
222 /* fields in the config descriptor */
223 u8 bConfigurationValue;
224 u8 iConfiguration;
225 u8 bmAttributes;
226 u8 bMaxPower;
227
228 struct usb_composite_dev *cdev;
229
cac85a8b 230 /* private: */
40982be5
DB
231 /* internals */
232 struct list_head list;
233 struct list_head functions;
234 u8 next_interface_id;
bdb64d72 235 unsigned superspeed:1;
40982be5
DB
236 unsigned highspeed:1;
237 unsigned fullspeed:1;
238 struct usb_function *interface[MAX_CONFIG_INTERFACES];
239};
240
241int usb_add_config(struct usb_composite_dev *,
c9bfff9c
UKK
242 struct usb_configuration *,
243 int (*)(struct usb_configuration *));
40982be5 244
51cce6fc
BG
245void usb_remove_config(struct usb_composite_dev *,
246 struct usb_configuration *);
247
40982be5
DB
248/**
249 * struct usb_composite_driver - groups configurations into a gadget
250 * @name: For diagnostics, identifies the driver.
ad1a8102
MN
251 * @iProduct: Used as iProduct override if @dev->iProduct is not set.
252 * If NULL value of @name is taken.
253 * @iManufacturer: Used as iManufacturer override if @dev->iManufacturer is
254 * not set. If NULL a default "<system> <release> with <udc>" value
255 * will be used.
40982be5
DB
256 * @dev: Template descriptor for the device, including default device
257 * identifiers.
258 * @strings: tables of strings, keyed by identifiers assigned during bind()
259 * and language IDs provided in control requests
35a0e0bf 260 * @max_speed: Highest speed the driver supports.
ad1a8102
MN
261 * @needs_serial: set to 1 if the gadget needs userspace to provide
262 * a serial number. If one is not provided, warning will be printed.
07a18bd7 263 * @unbind: Reverses bind; called as a side effect of unregistering
40982be5 264 * this driver.
d187abb9 265 * @disconnect: optional driver disconnect method
8942939a
DB
266 * @suspend: Notifies when the host stops sending USB traffic,
267 * after function notifications
268 * @resume: Notifies configuration when the host restarts USB traffic,
269 * before function notifications
40982be5
DB
270 *
271 * Devices default to reporting self powered operation. Devices which rely
272 * on bus powered operation should report this in their @bind() method.
273 *
07a18bd7 274 * Before returning from bind, various fields in the template descriptor
40982be5
DB
275 * may be overridden. These include the idVendor/idProduct/bcdDevice values
276 * normally to bind the appropriate host side driver, and the three strings
277 * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
278 * meaningful device identifiers. (The strings will not be defined unless
279 * they are defined in @dev and @strings.) The correct ep0 maxpacket size
280 * is also reported, as defined by the underlying controller driver.
281 */
282struct usb_composite_driver {
283 const char *name;
ad1a8102
MN
284 const char *iProduct;
285 const char *iManufacturer;
40982be5
DB
286 const struct usb_device_descriptor *dev;
287 struct usb_gadget_strings **strings;
35a0e0bf 288 enum usb_device_speed max_speed;
ad1a8102 289 unsigned needs_serial:1;
40982be5 290
40982be5 291 int (*unbind)(struct usb_composite_dev *);
3f3e12d0
MN
292
293 void (*disconnect)(struct usb_composite_dev *);
8942939a
DB
294
295 /* global suspend hooks */
296 void (*suspend)(struct usb_composite_dev *);
297 void (*resume)(struct usb_composite_dev *);
40982be5
DB
298};
299
07a18bd7
MN
300extern int usb_composite_probe(struct usb_composite_driver *driver,
301 int (*bind)(struct usb_composite_dev *cdev));
302extern void usb_composite_unregister(struct usb_composite_driver *driver);
1b9ba000 303extern void usb_composite_setup_continue(struct usb_composite_dev *cdev);
40982be5
DB
304
305
306/**
307 * struct usb_composite_device - represents one composite usb gadget
308 * @gadget: read-only, abstracts the gadget's usb peripheral controller
309 * @req: used for control responses; buffer is pre-allocated
310 * @bufsiz: size of buffer pre-allocated in @req
311 * @config: the currently active configuration
312 *
313 * One of these devices is allocated and initialized before the
314 * associated device driver's bind() is called.
315 *
316 * OPEN ISSUE: it appears that some WUSB devices will need to be
317 * built by combining a normal (wired) gadget with a wireless one.
318 * This revision of the gadget framework should probably try to make
319 * sure doing that won't hurt too much.
320 *
321 * One notion for how to handle Wireless USB devices involves:
322 * (a) a second gadget here, discovery mechanism TBD, but likely
323 * needing separate "register/unregister WUSB gadget" calls;
324 * (b) updates to usb_gadget to include flags "is it wireless",
325 * "is it wired", plus (presumably in a wrapper structure)
326 * bandgroup and PHY info;
327 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
328 * wireless-specific parameters like maxburst and maxsequence;
329 * (d) configurations that are specific to wireless links;
330 * (e) function drivers that understand wireless configs and will
331 * support wireless for (additional) function instances;
332 * (f) a function to support association setup (like CBAF), not
333 * necessarily requiring a wireless adapter;
334 * (g) composite device setup that can create one or more wireless
335 * configs, including appropriate association setup support;
336 * (h) more, TBD.
337 */
338struct usb_composite_dev {
339 struct usb_gadget *gadget;
340 struct usb_request *req;
341 unsigned bufsiz;
342
343 struct usb_configuration *config;
344
cac85a8b 345 /* private: */
40982be5 346 /* internals */
f48cf80f 347 unsigned int suspended:1;
40982be5
DB
348 struct usb_device_descriptor desc;
349 struct list_head configs;
350 struct usb_composite_driver *driver;
351 u8 next_string_id;
ad1a8102
MN
352 u8 manufacturer_override;
353 u8 product_override;
354 u8 serial_override;
40982be5 355
60beed95
DB
356 /* the gadget driver won't enable the data pullup
357 * while the deactivation count is nonzero.
358 */
359 unsigned deactivations;
40982be5 360
1b9ba000
RQ
361 /* the composite driver won't complete the control transfer's
362 * data/status stages till delayed_status is zero.
363 */
364 int delayed_status;
365
366 /* protects deactivations and delayed_status counts*/
60beed95 367 spinlock_t lock;
40982be5
DB
368};
369
370extern int usb_string_id(struct usb_composite_dev *c);
f2adc4f8
MN
371extern int usb_string_ids_tab(struct usb_composite_dev *c,
372 struct usb_string *str);
373extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
374
40982be5
DB
375
376/* messaging utils */
377#define DBG(d, fmt, args...) \
378 dev_dbg(&(d)->gadget->dev , fmt , ## args)
379#define VDBG(d, fmt, args...) \
380 dev_vdbg(&(d)->gadget->dev , fmt , ## args)
381#define ERROR(d, fmt, args...) \
382 dev_err(&(d)->gadget->dev , fmt , ## args)
b6c63937 383#define WARNING(d, fmt, args...) \
40982be5
DB
384 dev_warn(&(d)->gadget->dev , fmt , ## args)
385#define INFO(d, fmt, args...) \
386 dev_info(&(d)->gadget->dev , fmt , ## args)
387
388#endif /* __LINUX_USB_COMPOSITE_H */
This page took 0.714884 seconds and 5 git commands to generate.