]>
Commit | Line | Data |
---|---|---|
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 | ||
40 | ||
41 | struct usb_configuration; | |
42 | ||
43 | /** | |
44 | * struct usb_function - describes one function of a configuration | |
45 | * @name: For diagnostics, identifies the function. | |
46 | * @strings: tables of strings, keyed by identifiers assigned during bind() | |
47 | * and by language IDs provided in control requests | |
48 | * @descriptors: Table of full (or low) speed descriptors, using interface and | |
49 | * string identifiers assigned during @bind(). If this pointer is null, | |
50 | * the function will not be available at full speed (or at low speed). | |
51 | * @hs_descriptors: Table of high speed descriptors, using interface and | |
52 | * string identifiers assigned during @bind(). If this pointer is null, | |
53 | * the function will not be available at high speed. | |
54 | * @config: assigned when @usb_add_function() is called; this is the | |
55 | * configuration with which this function is associated. | |
56 | * @bind: Before the gadget can register, all of its functions bind() to the | |
57 | * available resources including string and interface identifiers used | |
58 | * in interface or class descriptors; endpoints; I/O buffers; and so on. | |
59 | * @unbind: Reverses @bind; called as a side effect of unregistering the | |
60 | * driver which added this function. | |
61 | * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may | |
62 | * initialize usb_ep.driver data at this time (when it is used). | |
63 | * Note that setting an interface to its current altsetting resets | |
64 | * interface state, and that all interfaces have a disabled state. | |
65 | * @get_alt: Returns the active altsetting. If this is not provided, | |
66 | * then only altsetting zero is supported. | |
67 | * @disable: (REQUIRED) Indicates the function should be disabled. Reasons | |
68 | * include host resetting or reconfiguring the gadget, and disconnection. | |
69 | * @setup: Used for interface-specific control requests. | |
70 | * @suspend: Notifies functions when the host stops sending USB traffic. | |
71 | * @resume: Notifies functions when the host restarts USB traffic. | |
72 | * | |
73 | * A single USB function uses one or more interfaces, and should in most | |
74 | * cases support operation at both full and high speeds. Each function is | |
75 | * associated by @usb_add_function() with a one configuration; that function | |
76 | * causes @bind() to be called so resources can be allocated as part of | |
77 | * setting up a gadget driver. Those resources include endpoints, which | |
78 | * should be allocated using @usb_ep_autoconfig(). | |
79 | * | |
80 | * To support dual speed operation, a function driver provides descriptors | |
81 | * for both high and full speed operation. Except in rare cases that don't | |
82 | * involve bulk endpoints, each speed needs different endpoint descriptors. | |
83 | * | |
84 | * Function drivers choose their own strategies for managing instance data. | |
85 | * The simplest strategy just declares it "static', which means the function | |
86 | * can only be activated once. If the function needs to be exposed in more | |
87 | * than one configuration at a given speed, it needs to support multiple | |
88 | * usb_function structures (one for each configuration). | |
89 | * | |
90 | * A more complex strategy might encapsulate a @usb_function structure inside | |
91 | * a driver-specific instance structure to allows multiple activations. An | |
92 | * example of multiple activations might be a CDC ACM function that supports | |
93 | * two or more distinct instances within the same configuration, providing | |
94 | * several independent logical data links to a USB host. | |
95 | */ | |
96 | struct usb_function { | |
97 | const char *name; | |
98 | struct usb_gadget_strings **strings; | |
99 | struct usb_descriptor_header **descriptors; | |
100 | struct usb_descriptor_header **hs_descriptors; | |
101 | ||
102 | struct usb_configuration *config; | |
103 | ||
104 | /* REVISIT: bind() functions can be marked __init, which | |
105 | * makes trouble for section mismatch analysis. See if | |
106 | * we can't restructure things to avoid mismatching. | |
107 | * Related: unbind() may kfree() but bind() won't... | |
108 | */ | |
109 | ||
110 | /* configuration management: bind/unbind */ | |
111 | int (*bind)(struct usb_configuration *, | |
112 | struct usb_function *); | |
113 | void (*unbind)(struct usb_configuration *, | |
114 | struct usb_function *); | |
115 | ||
116 | /* runtime state management */ | |
117 | int (*set_alt)(struct usb_function *, | |
118 | unsigned interface, unsigned alt); | |
119 | int (*get_alt)(struct usb_function *, | |
120 | unsigned interface); | |
121 | void (*disable)(struct usb_function *); | |
122 | int (*setup)(struct usb_function *, | |
123 | const struct usb_ctrlrequest *); | |
124 | void (*suspend)(struct usb_function *); | |
125 | void (*resume)(struct usb_function *); | |
126 | ||
cac85a8b | 127 | /* private: */ |
40982be5 DB |
128 | /* internals */ |
129 | struct list_head list; | |
5242658d | 130 | DECLARE_BITMAP(endpoints, 32); |
40982be5 DB |
131 | }; |
132 | ||
133 | int usb_add_function(struct usb_configuration *, struct usb_function *); | |
134 | ||
60beed95 DB |
135 | int usb_function_deactivate(struct usb_function *); |
136 | int usb_function_activate(struct usb_function *); | |
137 | ||
40982be5 DB |
138 | int usb_interface_id(struct usb_configuration *, struct usb_function *); |
139 | ||
140 | /** | |
141 | * ep_choose - select descriptor endpoint at current device speed | |
142 | * @g: gadget, connected and running at some speed | |
143 | * @hs: descriptor to use for high speed operation | |
144 | * @fs: descriptor to use for full or low speed operation | |
145 | */ | |
146 | static inline struct usb_endpoint_descriptor * | |
147 | ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, | |
148 | struct usb_endpoint_descriptor *fs) | |
149 | { | |
150 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | |
151 | return hs; | |
152 | return fs; | |
153 | } | |
154 | ||
155 | #define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */ | |
156 | ||
157 | /** | |
158 | * struct usb_configuration - represents one gadget configuration | |
159 | * @label: For diagnostics, describes the configuration. | |
160 | * @strings: Tables of strings, keyed by identifiers assigned during @bind() | |
161 | * and by language IDs provided in control requests. | |
162 | * @descriptors: Table of descriptors preceding all function descriptors. | |
163 | * Examples include OTG and vendor-specific descriptors. | |
164 | * @bind: Called from @usb_add_config() to allocate resources unique to this | |
165 | * configuration and to call @usb_add_function() for each function used. | |
166 | * @unbind: Reverses @bind; called as a side effect of unregistering the | |
167 | * driver which added this configuration. | |
168 | * @setup: Used to delegate control requests that aren't handled by standard | |
169 | * device infrastructure or directed at a specific interface. | |
170 | * @bConfigurationValue: Copied into configuration descriptor. | |
171 | * @iConfiguration: Copied into configuration descriptor. | |
172 | * @bmAttributes: Copied into configuration descriptor. | |
173 | * @bMaxPower: Copied into configuration descriptor. | |
174 | * @cdev: assigned by @usb_add_config() before calling @bind(); this is | |
175 | * the device associated with this configuration. | |
176 | * | |
177 | * Configurations are building blocks for gadget drivers structured around | |
178 | * function drivers. Simple USB gadgets require only one function and one | |
179 | * configuration, and handle dual-speed hardware by always providing the same | |
180 | * functionality. Slightly more complex gadgets may have more than one | |
181 | * single-function configuration at a given speed; or have configurations | |
182 | * that only work at one speed. | |
183 | * | |
184 | * Composite devices are, by definition, ones with configurations which | |
185 | * include more than one function. | |
186 | * | |
187 | * The lifecycle of a usb_configuration includes allocation, initialization | |
188 | * of the fields described above, and calling @usb_add_config() to set up | |
189 | * internal data and bind it to a specific device. The configuration's | |
190 | * @bind() method is then used to initialize all the functions and then | |
191 | * call @usb_add_function() for them. | |
192 | * | |
193 | * Those functions would normally be independant of each other, but that's | |
194 | * not mandatory. CDC WMC devices are an example where functions often | |
195 | * depend on other functions, with some functions subsidiary to others. | |
196 | * Such interdependency may be managed in any way, so long as all of the | |
197 | * descriptors complete by the time the composite driver returns from | |
198 | * its bind() routine. | |
199 | */ | |
200 | struct usb_configuration { | |
201 | const char *label; | |
202 | struct usb_gadget_strings **strings; | |
203 | const struct usb_descriptor_header **descriptors; | |
204 | ||
205 | /* REVISIT: bind() functions can be marked __init, which | |
206 | * makes trouble for section mismatch analysis. See if | |
207 | * we can't restructure things to avoid mismatching... | |
208 | */ | |
209 | ||
210 | /* configuration management: bind/unbind */ | |
211 | int (*bind)(struct usb_configuration *); | |
212 | void (*unbind)(struct usb_configuration *); | |
213 | int (*setup)(struct usb_configuration *, | |
214 | const struct usb_ctrlrequest *); | |
215 | ||
216 | /* fields in the config descriptor */ | |
217 | u8 bConfigurationValue; | |
218 | u8 iConfiguration; | |
219 | u8 bmAttributes; | |
220 | u8 bMaxPower; | |
221 | ||
222 | struct usb_composite_dev *cdev; | |
223 | ||
cac85a8b | 224 | /* private: */ |
40982be5 DB |
225 | /* internals */ |
226 | struct list_head list; | |
227 | struct list_head functions; | |
228 | u8 next_interface_id; | |
229 | unsigned highspeed:1; | |
230 | unsigned fullspeed:1; | |
231 | struct usb_function *interface[MAX_CONFIG_INTERFACES]; | |
232 | }; | |
233 | ||
234 | int usb_add_config(struct usb_composite_dev *, | |
235 | struct usb_configuration *); | |
236 | ||
237 | /** | |
238 | * struct usb_composite_driver - groups configurations into a gadget | |
239 | * @name: For diagnostics, identifies the driver. | |
240 | * @dev: Template descriptor for the device, including default device | |
241 | * identifiers. | |
242 | * @strings: tables of strings, keyed by identifiers assigned during bind() | |
243 | * and language IDs provided in control requests | |
244 | * @bind: (REQUIRED) Used to allocate resources that are shared across the | |
245 | * whole device, such as string IDs, and add its configurations using | |
246 | * @usb_add_config(). This may fail by returning a negative errno | |
247 | * value; it should return zero on successful initialization. | |
248 | * @unbind: Reverses @bind(); called as a side effect of unregistering | |
249 | * this driver. | |
d187abb9 | 250 | * @disconnect: optional driver disconnect method |
8942939a DB |
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 | |
40982be5 DB |
255 | * |
256 | * Devices default to reporting self powered operation. Devices which rely | |
257 | * on bus powered operation should report this in their @bind() method. | |
258 | * | |
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. | |
266 | */ | |
267 | struct usb_composite_driver { | |
268 | const char *name; | |
269 | const struct usb_device_descriptor *dev; | |
270 | struct usb_gadget_strings **strings; | |
271 | ||
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... | |
275 | */ | |
276 | ||
277 | int (*bind)(struct usb_composite_dev *); | |
278 | int (*unbind)(struct usb_composite_dev *); | |
3f3e12d0 MN |
279 | |
280 | void (*disconnect)(struct usb_composite_dev *); | |
8942939a DB |
281 | |
282 | /* global suspend hooks */ | |
283 | void (*suspend)(struct usb_composite_dev *); | |
284 | void (*resume)(struct usb_composite_dev *); | |
40982be5 DB |
285 | }; |
286 | ||
287 | extern int usb_composite_register(struct usb_composite_driver *); | |
288 | extern void usb_composite_unregister(struct usb_composite_driver *); | |
289 | ||
290 | ||
291 | /** | |
292 | * struct usb_composite_device - represents one composite usb gadget | |
293 | * @gadget: read-only, abstracts the gadget's usb peripheral controller | |
294 | * @req: used for control responses; buffer is pre-allocated | |
295 | * @bufsiz: size of buffer pre-allocated in @req | |
296 | * @config: the currently active configuration | |
297 | * | |
298 | * One of these devices is allocated and initialized before the | |
299 | * associated device driver's bind() is called. | |
300 | * | |
301 | * OPEN ISSUE: it appears that some WUSB devices will need to be | |
302 | * built by combining a normal (wired) gadget with a wireless one. | |
303 | * This revision of the gadget framework should probably try to make | |
304 | * sure doing that won't hurt too much. | |
305 | * | |
306 | * One notion for how to handle Wireless USB devices involves: | |
307 | * (a) a second gadget here, discovery mechanism TBD, but likely | |
308 | * needing separate "register/unregister WUSB gadget" calls; | |
309 | * (b) updates to usb_gadget to include flags "is it wireless", | |
310 | * "is it wired", plus (presumably in a wrapper structure) | |
311 | * bandgroup and PHY info; | |
312 | * (c) presumably a wireless_ep wrapping a usb_ep, and reporting | |
313 | * wireless-specific parameters like maxburst and maxsequence; | |
314 | * (d) configurations that are specific to wireless links; | |
315 | * (e) function drivers that understand wireless configs and will | |
316 | * support wireless for (additional) function instances; | |
317 | * (f) a function to support association setup (like CBAF), not | |
318 | * necessarily requiring a wireless adapter; | |
319 | * (g) composite device setup that can create one or more wireless | |
320 | * configs, including appropriate association setup support; | |
321 | * (h) more, TBD. | |
322 | */ | |
323 | struct usb_composite_dev { | |
324 | struct usb_gadget *gadget; | |
325 | struct usb_request *req; | |
326 | unsigned bufsiz; | |
327 | ||
328 | struct usb_configuration *config; | |
329 | ||
cac85a8b | 330 | /* private: */ |
40982be5 | 331 | /* internals */ |
f48cf80f | 332 | unsigned int suspended:1; |
40982be5 DB |
333 | struct usb_device_descriptor desc; |
334 | struct list_head configs; | |
335 | struct usb_composite_driver *driver; | |
336 | u8 next_string_id; | |
337 | ||
60beed95 DB |
338 | /* the gadget driver won't enable the data pullup |
339 | * while the deactivation count is nonzero. | |
340 | */ | |
341 | unsigned deactivations; | |
40982be5 | 342 | |
60beed95 DB |
343 | /* protects at least deactivation count */ |
344 | spinlock_t lock; | |
40982be5 DB |
345 | }; |
346 | ||
347 | extern int usb_string_id(struct usb_composite_dev *c); | |
f2adc4f8 MN |
348 | extern int usb_string_ids_tab(struct usb_composite_dev *c, |
349 | struct usb_string *str); | |
350 | extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n); | |
351 | ||
40982be5 DB |
352 | |
353 | /* messaging utils */ | |
354 | #define DBG(d, fmt, args...) \ | |
355 | dev_dbg(&(d)->gadget->dev , fmt , ## args) | |
356 | #define VDBG(d, fmt, args...) \ | |
357 | dev_vdbg(&(d)->gadget->dev , fmt , ## args) | |
358 | #define ERROR(d, fmt, args...) \ | |
359 | dev_err(&(d)->gadget->dev , fmt , ## args) | |
b6c63937 | 360 | #define WARNING(d, fmt, args...) \ |
40982be5 DB |
361 | dev_warn(&(d)->gadget->dev , fmt , ## args) |
362 | #define INFO(d, fmt, args...) \ | |
363 | dev_info(&(d)->gadget->dev , fmt , ## args) | |
364 | ||
365 | #endif /* __LINUX_USB_COMPOSITE_H */ |