]>
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 | ||
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 | |
49 | struct 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 | */ | |
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; | |
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 | ||
154 | int usb_add_function(struct usb_configuration *, struct usb_function *); | |
155 | ||
60beed95 DB |
156 | int usb_function_deactivate(struct usb_function *); |
157 | int usb_function_activate(struct usb_function *); | |
158 | ||
40982be5 DB |
159 | int usb_interface_id(struct usb_configuration *, struct usb_function *); |
160 | ||
48767a4e TB |
161 | int 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 | */ | |
207 | struct 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 | ||
241 | int usb_add_config(struct usb_composite_dev *, | |
c9bfff9c UKK |
242 | struct usb_configuration *, |
243 | int (*)(struct usb_configuration *)); | |
40982be5 | 244 | |
51cce6fc BG |
245 | void 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. | |
cad4cd8f AP |
256 | * @iSerialNumber: Used as iSerialNumber override if @dev->iSerialNumber is |
257 | * not set. | |
40982be5 DB |
258 | * @dev: Template descriptor for the device, including default device |
259 | * identifiers. | |
fac3a43e | 260 | * @strings: tables of strings, keyed by identifiers assigned during @bind |
40982be5 | 261 | * and language IDs provided in control requests |
35a0e0bf | 262 | * @max_speed: Highest speed the driver supports. |
ad1a8102 MN |
263 | * @needs_serial: set to 1 if the gadget needs userspace to provide |
264 | * a serial number. If one is not provided, warning will be printed. | |
fac3a43e SAS |
265 | * @bind: (REQUIRED) Used to allocate resources that are shared across the |
266 | * whole device, such as string IDs, and add its configurations using | |
267 | * @usb_add_config(). This may fail by returning a negative errno | |
268 | * value; it should return zero on successful initialization. | |
269 | * @unbind: Reverses @bind; called as a side effect of unregistering | |
40982be5 | 270 | * this driver. |
d187abb9 | 271 | * @disconnect: optional driver disconnect method |
8942939a DB |
272 | * @suspend: Notifies when the host stops sending USB traffic, |
273 | * after function notifications | |
274 | * @resume: Notifies configuration when the host restarts USB traffic, | |
275 | * before function notifications | |
40982be5 DB |
276 | * |
277 | * Devices default to reporting self powered operation. Devices which rely | |
fac3a43e | 278 | * on bus powered operation should report this in their @bind method. |
40982be5 | 279 | * |
fac3a43e | 280 | * Before returning from @bind, various fields in the template descriptor |
40982be5 DB |
281 | * may be overridden. These include the idVendor/idProduct/bcdDevice values |
282 | * normally to bind the appropriate host side driver, and the three strings | |
283 | * (iManufacturer, iProduct, iSerialNumber) normally used to provide user | |
284 | * meaningful device identifiers. (The strings will not be defined unless | |
285 | * they are defined in @dev and @strings.) The correct ep0 maxpacket size | |
286 | * is also reported, as defined by the underlying controller driver. | |
287 | */ | |
288 | struct usb_composite_driver { | |
289 | const char *name; | |
ad1a8102 MN |
290 | const char *iProduct; |
291 | const char *iManufacturer; | |
cad4cd8f | 292 | const char *iSerialNumber; |
40982be5 DB |
293 | const struct usb_device_descriptor *dev; |
294 | struct usb_gadget_strings **strings; | |
35a0e0bf | 295 | enum usb_device_speed max_speed; |
ad1a8102 | 296 | unsigned needs_serial:1; |
40982be5 | 297 | |
fac3a43e | 298 | int (*bind)(struct usb_composite_dev *cdev); |
40982be5 | 299 | int (*unbind)(struct usb_composite_dev *); |
3f3e12d0 MN |
300 | |
301 | void (*disconnect)(struct usb_composite_dev *); | |
8942939a DB |
302 | |
303 | /* global suspend hooks */ | |
304 | void (*suspend)(struct usb_composite_dev *); | |
305 | void (*resume)(struct usb_composite_dev *); | |
40982be5 DB |
306 | }; |
307 | ||
07a18bd7 MN |
308 | extern int usb_composite_probe(struct usb_composite_driver *driver, |
309 | int (*bind)(struct usb_composite_dev *cdev)); | |
310 | extern void usb_composite_unregister(struct usb_composite_driver *driver); | |
1b9ba000 | 311 | extern void usb_composite_setup_continue(struct usb_composite_dev *cdev); |
40982be5 DB |
312 | |
313 | ||
314 | /** | |
315 | * struct usb_composite_device - represents one composite usb gadget | |
316 | * @gadget: read-only, abstracts the gadget's usb peripheral controller | |
317 | * @req: used for control responses; buffer is pre-allocated | |
318 | * @bufsiz: size of buffer pre-allocated in @req | |
319 | * @config: the currently active configuration | |
320 | * | |
321 | * One of these devices is allocated and initialized before the | |
322 | * associated device driver's bind() is called. | |
323 | * | |
324 | * OPEN ISSUE: it appears that some WUSB devices will need to be | |
325 | * built by combining a normal (wired) gadget with a wireless one. | |
326 | * This revision of the gadget framework should probably try to make | |
327 | * sure doing that won't hurt too much. | |
328 | * | |
329 | * One notion for how to handle Wireless USB devices involves: | |
330 | * (a) a second gadget here, discovery mechanism TBD, but likely | |
331 | * needing separate "register/unregister WUSB gadget" calls; | |
332 | * (b) updates to usb_gadget to include flags "is it wireless", | |
333 | * "is it wired", plus (presumably in a wrapper structure) | |
334 | * bandgroup and PHY info; | |
335 | * (c) presumably a wireless_ep wrapping a usb_ep, and reporting | |
336 | * wireless-specific parameters like maxburst and maxsequence; | |
337 | * (d) configurations that are specific to wireless links; | |
338 | * (e) function drivers that understand wireless configs and will | |
339 | * support wireless for (additional) function instances; | |
340 | * (f) a function to support association setup (like CBAF), not | |
341 | * necessarily requiring a wireless adapter; | |
342 | * (g) composite device setup that can create one or more wireless | |
343 | * configs, including appropriate association setup support; | |
344 | * (h) more, TBD. | |
345 | */ | |
346 | struct usb_composite_dev { | |
347 | struct usb_gadget *gadget; | |
348 | struct usb_request *req; | |
349 | unsigned bufsiz; | |
350 | ||
351 | struct usb_configuration *config; | |
352 | ||
cac85a8b | 353 | /* private: */ |
40982be5 | 354 | /* internals */ |
f48cf80f | 355 | unsigned int suspended:1; |
40982be5 DB |
356 | struct usb_device_descriptor desc; |
357 | struct list_head configs; | |
358 | struct usb_composite_driver *driver; | |
359 | u8 next_string_id; | |
ad1a8102 MN |
360 | u8 manufacturer_override; |
361 | u8 product_override; | |
362 | u8 serial_override; | |
40982be5 | 363 | |
60beed95 DB |
364 | /* the gadget driver won't enable the data pullup |
365 | * while the deactivation count is nonzero. | |
366 | */ | |
367 | unsigned deactivations; | |
40982be5 | 368 | |
1b9ba000 RQ |
369 | /* the composite driver won't complete the control transfer's |
370 | * data/status stages till delayed_status is zero. | |
371 | */ | |
372 | int delayed_status; | |
373 | ||
374 | /* protects deactivations and delayed_status counts*/ | |
60beed95 | 375 | spinlock_t lock; |
40982be5 DB |
376 | }; |
377 | ||
378 | extern int usb_string_id(struct usb_composite_dev *c); | |
f2adc4f8 MN |
379 | extern int usb_string_ids_tab(struct usb_composite_dev *c, |
380 | struct usb_string *str); | |
381 | extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n); | |
382 | ||
40982be5 DB |
383 | |
384 | /* messaging utils */ | |
385 | #define DBG(d, fmt, args...) \ | |
386 | dev_dbg(&(d)->gadget->dev , fmt , ## args) | |
387 | #define VDBG(d, fmt, args...) \ | |
388 | dev_vdbg(&(d)->gadget->dev , fmt , ## args) | |
389 | #define ERROR(d, fmt, args...) \ | |
390 | dev_err(&(d)->gadget->dev , fmt , ## args) | |
b6c63937 | 391 | #define WARNING(d, fmt, args...) \ |
40982be5 DB |
392 | dev_warn(&(d)->gadget->dev , fmt , ## args) |
393 | #define INFO(d, fmt, args...) \ | |
394 | dev_info(&(d)->gadget->dev , fmt , ## args) | |
395 | ||
396 | #endif /* __LINUX_USB_COMPOSITE_H */ |