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
24 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 * This framework is an optional layer on top of the USB Gadget interface,
28 * making it easier to build (a) Composite devices, supporting multiple
29 * functions within any single configuration, and (b) Multi-configuration
30 * devices, also supporting multiple functions but without necessarily
31 * having more than one function per configuration.
33 * Example: a device with a single configuration supporting both network
34 * link and mass storage functions is a composite device. Those functions
35 * might alternatively be packaged in individual configurations, but in
36 * the composite model the host can use both functions at the same time.
40 #include <usb/gadget.h>
43 struct usb_configuration
;
46 * struct usb_function - describes one function of a configuration
47 * @name: For diagnostics, identifies the function.
48 * @strings: tables of strings, keyed by identifiers assigned during bind()
49 * and by language IDs provided in control requests
50 * @descriptors: Table of full (or low) speed descriptors, using interface and
51 * string identifiers assigned during @bind(). If this pointer is null,
52 * the function will not be available at full speed (or at low speed).
53 * @hs_descriptors: Table of high speed descriptors, using interface and
54 * string identifiers assigned during @bind(). If this pointer is null,
55 * the function will not be available at high speed.
56 * @config: assigned when @usb_add_function() is called; this is the
57 * configuration with which this function is associated.
58 * @bind: Before the gadget can register, all of its functions bind() to the
59 * available resources including string and interface identifiers used
60 * in interface or class descriptors; endpoints; I/O buffers; and so on.
61 * @unbind: Reverses @bind; called as a side effect of unregistering the
62 * driver which added this function.
63 * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
64 * initialize usb_ep.driver data at this time (when it is used).
65 * Note that setting an interface to its current altsetting resets
66 * interface state, and that all interfaces have a disabled state.
67 * @get_alt: Returns the active altsetting. If this is not provided,
68 * then only altsetting zero is supported.
69 * @disable: (REQUIRED) Indicates the function should be disabled. Reasons
70 * include host resetting or reconfiguring the gadget, and disconnection.
71 * @setup: Used for interface-specific control requests.
72 * @suspend: Notifies functions when the host stops sending USB traffic.
73 * @resume: Notifies functions when the host restarts USB traffic.
75 * A single USB function uses one or more interfaces, and should in most
76 * cases support operation at both full and high speeds. Each function is
77 * associated by @usb_add_function() with a one configuration; that function
78 * causes @bind() to be called so resources can be allocated as part of
79 * setting up a gadget driver. Those resources include endpoints, which
80 * should be allocated using @usb_ep_autoconfig().
82 * To support dual speed operation, a function driver provides descriptors
83 * for both high and full speed operation. Except in rare cases that don't
84 * involve bulk endpoints, each speed needs different endpoint descriptors.
86 * Function drivers choose their own strategies for managing instance data.
87 * The simplest strategy just declares it "static', which means the function
88 * can only be activated once. If the function needs to be exposed in more
89 * than one configuration at a given speed, it needs to support multiple
90 * usb_function structures (one for each configuration).
92 * A more complex strategy might encapsulate a @usb_function structure inside
93 * a driver-specific instance structure to allows multiple activations. An
94 * example of multiple activations might be a CDC ACM function that supports
95 * two or more distinct instances within the same configuration, providing
96 * several independent logical data links to a USB host.
100 struct usb_gadget_strings
**strings
;
101 struct usb_descriptor_header
**descriptors
;
102 struct usb_descriptor_header
**hs_descriptors
;
104 struct usb_configuration
*config
;
106 /* REVISIT: bind() functions can be marked __init, which
107 * makes trouble for section mismatch analysis. See if
108 * we can't restructure things to avoid mismatching.
109 * Related: unbind() may kfree() but bind() won't...
112 /* configuration management: bind/unbind */
113 int (*bind
)(struct usb_configuration
*,
114 struct usb_function
*);
115 void (*unbind
)(struct usb_configuration
*,
116 struct usb_function
*);
118 /* runtime state management */
119 int (*set_alt
)(struct usb_function
*,
120 unsigned interface
, unsigned alt
);
121 int (*get_alt
)(struct usb_function
*,
123 void (*disable
)(struct usb_function
*);
124 int (*setup
)(struct usb_function
*,
125 const struct usb_ctrlrequest
*);
126 void (*suspend
)(struct usb_function
*);
127 void (*resume
)(struct usb_function
*);
131 struct list_head list
;
134 int usb_add_function(struct usb_configuration
*, struct usb_function
*);
136 int usb_function_deactivate(struct usb_function
*);
137 int usb_function_activate(struct usb_function
*);
139 int usb_interface_id(struct usb_configuration
*, struct usb_function
*);
142 * ep_choose - select descriptor endpoint at current device speed
143 * @g: gadget, connected and running at some speed
144 * @hs: descriptor to use for high speed operation
145 * @fs: descriptor to use for full or low speed operation
147 static inline struct usb_endpoint_descriptor
*
148 ep_choose(struct usb_gadget
*g
, struct usb_endpoint_descriptor
*hs
,
149 struct usb_endpoint_descriptor
*fs
)
151 if (gadget_is_dualspeed(g
) && g
->speed
== USB_SPEED_HIGH
)
156 #define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */
159 * struct usb_configuration - represents one gadget configuration
160 * @label: For diagnostics, describes the configuration.
161 * @strings: Tables of strings, keyed by identifiers assigned during @bind()
162 * and by language IDs provided in control requests.
163 * @descriptors: Table of descriptors preceding all function descriptors.
164 * Examples include OTG and vendor-specific descriptors.
165 * @bind: Called from @usb_add_config() to allocate resources unique to this
166 * configuration and to call @usb_add_function() for each function used.
167 * @unbind: Reverses @bind; called as a side effect of unregistering the
168 * driver which added this configuration.
169 * @setup: Used to delegate control requests that aren't handled by standard
170 * device infrastructure or directed at a specific interface.
171 * @bConfigurationValue: Copied into configuration descriptor.
172 * @iConfiguration: Copied into configuration descriptor.
173 * @bmAttributes: Copied into configuration descriptor.
174 * @bMaxPower: Copied into configuration descriptor.
175 * @cdev: assigned by @usb_add_config() before calling @bind(); this is
176 * the device associated with this configuration.
178 * Configurations are building blocks for gadget drivers structured around
179 * function drivers. Simple USB gadgets require only one function and one
180 * configuration, and handle dual-speed hardware by always providing the same
181 * functionality. Slightly more complex gadgets may have more than one
182 * single-function configuration at a given speed; or have configurations
183 * that only work at one speed.
185 * Composite devices are, by definition, ones with configurations which
186 * include more than one function.
188 * The lifecycle of a usb_configuration includes allocation, initialization
189 * of the fields described above, and calling @usb_add_config() to set up
190 * internal data and bind it to a specific device. The configuration's
191 * @bind() method is then used to initialize all the functions and then
192 * call @usb_add_function() for them.
194 * Those functions would normally be independant of each other, but that's
195 * not mandatory. CDC WMC devices are an example where functions often
196 * depend on other functions, with some functions subsidiary to others.
197 * Such interdependency may be managed in any way, so long as all of the
198 * descriptors complete by the time the composite driver returns from
199 * its bind() routine.
201 struct usb_configuration
{
203 struct usb_gadget_strings
**strings
;
204 const struct usb_descriptor_header
**descriptors
;
206 /* REVISIT: bind() functions can be marked __init, which
207 * makes trouble for section mismatch analysis. See if
208 * we can't restructure things to avoid mismatching...
211 /* configuration management: bind/unbind */
212 int (*bind
)(struct usb_configuration
*);
213 void (*unbind
)(struct usb_configuration
*);
214 int (*setup
)(struct usb_configuration
*,
215 const struct usb_ctrlrequest
*);
217 /* fields in the config descriptor */
218 u8 bConfigurationValue
;
223 struct usb_composite_dev
*cdev
;
227 struct list_head list
;
228 struct list_head functions
;
229 u8 next_interface_id
;
230 unsigned highspeed
:1;
231 unsigned fullspeed
:1;
232 struct usb_function
*interface
[MAX_CONFIG_INTERFACES
];
235 int usb_add_config(struct usb_composite_dev
*,
236 struct usb_configuration
*);
239 * struct usb_composite_driver - groups configurations into a gadget
240 * @name: For diagnostics, identifies the driver.
241 * @dev: Template descriptor for the device, including default device
243 * @strings: tables of strings, keyed by identifiers assigned during bind()
244 * and language IDs provided in control requests
245 * @bind: (REQUIRED) Used to allocate resources that are shared across the
246 * whole device, such as string IDs, and add its configurations using
247 * @usb_add_config(). This may fail by returning a negative errno
248 * value; it should return zero on successful initialization.
249 * @unbind: Reverses @bind(); called as a side effect of unregistering
251 * @suspend: Notifies when the host stops sending USB traffic,
252 * after function notifications
253 * @resume: Notifies configuration when the host restarts USB traffic,
254 * before function notifications
256 * Devices default to reporting self powered operation. Devices which rely
257 * on bus powered operation should report this in their @bind() method.
259 * Before returning from @bind, various fields in the template descriptor
260 * may be overridden. These include the idVendor/idProduct/bcdDevice values
261 * normally to bind the appropriate host side driver, and the three strings
262 * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
263 * meaningful device identifiers. (The strings will not be defined unless
264 * they are defined in @dev and @strings.) The correct ep0 maxpacket size
265 * is also reported, as defined by the underlying controller driver.
267 struct usb_composite_driver
{
269 const struct usb_device_descriptor
*dev
;
270 struct usb_gadget_strings
**strings
;
272 /* REVISIT: bind() functions can be marked __init, which
273 * makes trouble for section mismatch analysis. See if
274 * we can't restructure things to avoid mismatching...
277 int (*bind
)(struct usb_composite_dev
*);
278 int (*unbind
)(struct usb_composite_dev
*);
280 /* global suspend hooks */
281 void (*suspend
)(struct usb_composite_dev
*);
282 void (*resume
)(struct usb_composite_dev
*);
285 extern int usb_composite_register(struct usb_composite_driver
*);
286 extern void usb_composite_unregister(struct usb_composite_driver
*);
290 * struct usb_composite_device - represents one composite usb gadget
291 * @gadget: read-only, abstracts the gadget's usb peripheral controller
292 * @req: used for control responses; buffer is pre-allocated
293 * @bufsiz: size of buffer pre-allocated in @req
294 * @config: the currently active configuration
296 * One of these devices is allocated and initialized before the
297 * associated device driver's bind() is called.
299 * OPEN ISSUE: it appears that some WUSB devices will need to be
300 * built by combining a normal (wired) gadget with a wireless one.
301 * This revision of the gadget framework should probably try to make
302 * sure doing that won't hurt too much.
304 * One notion for how to handle Wireless USB devices involves:
305 * (a) a second gadget here, discovery mechanism TBD, but likely
306 * needing separate "register/unregister WUSB gadget" calls;
307 * (b) updates to usb_gadget to include flags "is it wireless",
308 * "is it wired", plus (presumably in a wrapper structure)
309 * bandgroup and PHY info;
310 * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
311 * wireless-specific parameters like maxburst and maxsequence;
312 * (d) configurations that are specific to wireless links;
313 * (e) function drivers that understand wireless configs and will
314 * support wireless for (additional) function instances;
315 * (f) a function to support association setup (like CBAF), not
316 * necessarily requiring a wireless adapter;
317 * (g) composite device setup that can create one or more wireless
318 * configs, including appropriate association setup support;
321 struct usb_composite_dev
{
322 struct usb_gadget
*gadget
;
323 struct usb_request
*req
;
326 struct usb_configuration
*config
;
330 struct usb_device_descriptor desc
;
331 struct list_head configs
;
332 struct usb_composite_driver
*driver
;
335 /* the gadget driver won't enable the data pullup
336 * while the deactivation count is nonzero.
338 unsigned deactivations
;
341 extern int usb_string_id(struct usb_composite_dev
*c
);
343 /* messaging utils */
344 #define DBG(d, fmt, args...)
345 #define VDBG(d, fmt, args...)
346 #define ERROR(d, fmt, args...)
347 #define WARNING(d, fmt, args...)
348 #define INFO(d, fmt, args...)
350 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
352 #endif /* __LINUX_USB_COMPOSITE_H */