diff options
-rw-r--r-- | Documentation/DocBook/gadget.tmpl | 35 | ||||
-rw-r--r-- | drivers/usb/gadget/composite.c | 1040 | ||||
-rw-r--r-- | include/linux/usb/composite.h | 338 |
3 files changed, 1413 insertions, 0 deletions
diff --git a/Documentation/DocBook/gadget.tmpl b/Documentation/DocBook/gadget.tmpl index 5a8ffa761e09..478bfe16a19d 100644 --- a/Documentation/DocBook/gadget.tmpl +++ b/Documentation/DocBook/gadget.tmpl | |||
@@ -524,6 +524,41 @@ These utilities include endpoint autoconfiguration. | |||
524 | <!-- !Edrivers/usb/gadget/epautoconf.c --> | 524 | <!-- !Edrivers/usb/gadget/epautoconf.c --> |
525 | </sect1> | 525 | </sect1> |
526 | 526 | ||
527 | <sect1 id="composite"><title>Composite Device Framework</title> | ||
528 | |||
529 | <para>The core API is sufficient for writing drivers for composite | ||
530 | USB devices (with more than one function in a given configuration), | ||
531 | and also multi-configuration devices (also more than one function, | ||
532 | but not necessarily sharing a given configuration). | ||
533 | There is however an optional framework which makes it easier to | ||
534 | reuse and combine functions. | ||
535 | </para> | ||
536 | |||
537 | <para>Devices using this framework provide a <emphasis>struct | ||
538 | usb_composite_driver</emphasis>, which in turn provides one or | ||
539 | more <emphasis>struct usb_configuration</emphasis> instances. | ||
540 | Each such configuration includes at least one | ||
541 | <emphasis>struct usb_function</emphasis>, which packages a user | ||
542 | visible role such as "network link" or "mass storage device". | ||
543 | Management functions may also exist, such as "Device Firmware | ||
544 | Upgrade". | ||
545 | </para> | ||
546 | |||
547 | !Iinclude/linux/usb/composite.h | ||
548 | !Edrivers/usb/gadget/composite.c | ||
549 | |||
550 | </sect1> | ||
551 | |||
552 | <sect1 id="functions"><title>Composite Device Functions</title> | ||
553 | |||
554 | <para>At this writing, a few of the current gadget drivers have | ||
555 | been converted to this framework. | ||
556 | Near-term plans include converting all of them, except for "gadgetfs". | ||
557 | </para> | ||
558 | |||
559 | </sect1> | ||
560 | |||
561 | |||
527 | </chapter> | 562 | </chapter> |
528 | 563 | ||
529 | <chapter id="controllers"><title>Peripheral Controller Drivers</title> | 564 | <chapter id="controllers"><title>Peripheral Controller Drivers</title> |
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c new file mode 100644 index 000000000000..b3d302a6a081 --- /dev/null +++ b/drivers/usb/gadget/composite.c | |||
@@ -0,0 +1,1040 @@ | |||
1 | /* | ||
2 | * composite.c - infrastructure for Composite USB Gadgets | ||
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | */ | ||
20 | |||
21 | /* #define VERBOSE_DEBUG */ | ||
22 | |||
23 | #include <linux/kallsyms.h> | ||
24 | #include <linux/kernel.h> | ||
25 | #include <linux/slab.h> | ||
26 | #include <linux/device.h> | ||
27 | |||
28 | #include <linux/usb/composite.h> | ||
29 | |||
30 | |||
31 | /* | ||
32 | * The code in this file is utility code, used to build a gadget driver | ||
33 | * from one or more "function" drivers, one or more "configuration" | ||
34 | * objects, and a "usb_composite_driver" by gluing them together along | ||
35 | * with the relevant device-wide data. | ||
36 | */ | ||
37 | |||
38 | /* big enough to hold our biggest descriptor */ | ||
39 | #define USB_BUFSIZ 512 | ||
40 | |||
41 | static struct usb_composite_driver *composite; | ||
42 | |||
43 | /* Some systems will need runtime overrides for the product identifers | ||
44 | * published in the device descriptor, either numbers or strings or both. | ||
45 | * String parameters are in UTF-8 (superset of ASCII's 7 bit characters). | ||
46 | */ | ||
47 | |||
48 | static ushort idVendor; | ||
49 | module_param(idVendor, ushort, 0); | ||
50 | MODULE_PARM_DESC(idVendor, "USB Vendor ID"); | ||
51 | |||
52 | static ushort idProduct; | ||
53 | module_param(idProduct, ushort, 0); | ||
54 | MODULE_PARM_DESC(idProduct, "USB Product ID"); | ||
55 | |||
56 | static ushort bcdDevice; | ||
57 | module_param(bcdDevice, ushort, 0); | ||
58 | MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)"); | ||
59 | |||
60 | static char *iManufacturer; | ||
61 | module_param(iManufacturer, charp, 0); | ||
62 | MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string"); | ||
63 | |||
64 | static char *iProduct; | ||
65 | module_param(iProduct, charp, 0); | ||
66 | MODULE_PARM_DESC(iProduct, "USB Product string"); | ||
67 | |||
68 | static char *iSerialNumber; | ||
69 | module_param(iSerialNumber, charp, 0); | ||
70 | MODULE_PARM_DESC(iSerialNumber, "SerialNumber string"); | ||
71 | |||
72 | /*-------------------------------------------------------------------------*/ | ||
73 | |||
74 | /** | ||
75 | * usb_add_function() - add a function to a configuration | ||
76 | * @config: the configuration | ||
77 | * @function: the function being added | ||
78 | * Context: single threaded during gadget setup | ||
79 | * | ||
80 | * After initialization, each configuration must have one or more | ||
81 | * functions added to it. Adding a function involves calling its @bind() | ||
82 | * method to allocate resources such as interface and string identifiers | ||
83 | * and endpoints. | ||
84 | * | ||
85 | * This function returns the value of the function's bind(), which is | ||
86 | * zero for success else a negative errno value. | ||
87 | */ | ||
88 | int __init usb_add_function(struct usb_configuration *config, | ||
89 | struct usb_function *function) | ||
90 | { | ||
91 | int value = -EINVAL; | ||
92 | |||
93 | DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n", | ||
94 | function->name, function, | ||
95 | config->label, config); | ||
96 | |||
97 | if (!function->set_alt || !function->disable) | ||
98 | goto done; | ||
99 | |||
100 | function->config = config; | ||
101 | list_add_tail(&function->list, &config->functions); | ||
102 | |||
103 | /* REVISIT *require* function->bind? */ | ||
104 | if (function->bind) { | ||
105 | value = function->bind(config, function); | ||
106 | if (value < 0) { | ||
107 | list_del(&function->list); | ||
108 | function->config = NULL; | ||
109 | } | ||
110 | } else | ||
111 | value = 0; | ||
112 | |||
113 | /* We allow configurations that don't work at both speeds. | ||
114 | * If we run into a lowspeed Linux system, treat it the same | ||
115 | * as full speed ... it's the function drivers that will need | ||
116 | * to avoid bulk and ISO transfers. | ||
117 | */ | ||
118 | if (!config->fullspeed && function->descriptors) | ||
119 | config->fullspeed = true; | ||
120 | if (!config->highspeed && function->hs_descriptors) | ||
121 | config->highspeed = true; | ||
122 | |||
123 | done: | ||
124 | if (value) | ||
125 | DBG(config->cdev, "adding '%s'/%p --> %d\n", | ||
126 | function->name, function, value); | ||
127 | return value; | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * usb_interface_id() - allocate an unused interface ID | ||
132 | * @config: configuration associated with the interface | ||
133 | * @function: function handling the interface | ||
134 | * Context: single threaded during gadget setup | ||
135 | * | ||
136 | * usb_interface_id() is called from usb_function.bind() callbacks to | ||
137 | * allocate new interface IDs. The function driver will then store that | ||
138 | * ID in interface, association, CDC union, and other descriptors. It | ||
139 | * will also handle any control requests targetted at that interface, | ||
140 | * particularly changing its altsetting via set_alt(). There may | ||
141 | * also be class-specific or vendor-specific requests to handle. | ||
142 | * | ||
143 | * All interface identifier should be allocated using this routine, to | ||
144 | * ensure that for example different functions don't wrongly assign | ||
145 | * different meanings to the same identifier. Note that since interface | ||
146 | * identifers are configuration-specific, functions used in more than | ||
147 | * one configuration (or more than once in a given configuration) need | ||
148 | * multiple versions of the relevant descriptors. | ||
149 | * | ||
150 | * Returns the interface ID which was allocated; or -ENODEV if no | ||
151 | * more interface IDs can be allocated. | ||
152 | */ | ||
153 | int __init usb_interface_id(struct usb_configuration *config, | ||
154 | struct usb_function *function) | ||
155 | { | ||
156 | unsigned id = config->next_interface_id; | ||
157 | |||
158 | if (id < MAX_CONFIG_INTERFACES) { | ||
159 | config->interface[id] = function; | ||
160 | config->next_interface_id = id + 1; | ||
161 | return id; | ||
162 | } | ||
163 | return -ENODEV; | ||
164 | } | ||
165 | |||
166 | static int config_buf(struct usb_configuration *config, | ||
167 | enum usb_device_speed speed, void *buf, u8 type) | ||
168 | { | ||
169 | struct usb_config_descriptor *c = buf; | ||
170 | void *next = buf + USB_DT_CONFIG_SIZE; | ||
171 | int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; | ||
172 | struct usb_function *f; | ||
173 | int status; | ||
174 | |||
175 | /* write the config descriptor */ | ||
176 | c = buf; | ||
177 | c->bLength = USB_DT_CONFIG_SIZE; | ||
178 | c->bDescriptorType = type; | ||
179 | /* wTotalLength is written later */ | ||
180 | c->bNumInterfaces = config->next_interface_id; | ||
181 | c->bConfigurationValue = config->bConfigurationValue; | ||
182 | c->iConfiguration = config->iConfiguration; | ||
183 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; | ||
184 | c->bMaxPower = config->bMaxPower; | ||
185 | |||
186 | /* There may be e.g. OTG descriptors */ | ||
187 | if (config->descriptors) { | ||
188 | status = usb_descriptor_fillbuf(next, len, | ||
189 | config->descriptors); | ||
190 | if (status < 0) | ||
191 | return status; | ||
192 | len -= status; | ||
193 | next += status; | ||
194 | } | ||
195 | |||
196 | /* add each function's descriptors */ | ||
197 | list_for_each_entry(f, &config->functions, list) { | ||
198 | struct usb_descriptor_header **descriptors; | ||
199 | |||
200 | if (speed == USB_SPEED_HIGH) | ||
201 | descriptors = f->hs_descriptors; | ||
202 | else | ||
203 | descriptors = f->descriptors; | ||
204 | if (!descriptors) | ||
205 | continue; | ||
206 | status = usb_descriptor_fillbuf(next, len, | ||
207 | (const struct usb_descriptor_header **) descriptors); | ||
208 | if (status < 0) | ||
209 | return status; | ||
210 | len -= status; | ||
211 | next += status; | ||
212 | } | ||
213 | |||
214 | len = next - buf; | ||
215 | c->wTotalLength = cpu_to_le16(len); | ||
216 | return len; | ||
217 | } | ||
218 | |||
219 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) | ||
220 | { | ||
221 | struct usb_gadget *gadget = cdev->gadget; | ||
222 | struct usb_configuration *c; | ||
223 | u8 type = w_value >> 8; | ||
224 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; | ||
225 | |||
226 | if (gadget_is_dualspeed(gadget)) { | ||
227 | int hs = 0; | ||
228 | |||
229 | if (gadget->speed == USB_SPEED_HIGH) | ||
230 | hs = 1; | ||
231 | if (type == USB_DT_OTHER_SPEED_CONFIG) | ||
232 | hs = !hs; | ||
233 | if (hs) | ||
234 | speed = USB_SPEED_HIGH; | ||
235 | |||
236 | } | ||
237 | |||
238 | /* This is a lookup by config *INDEX* */ | ||
239 | w_value &= 0xff; | ||
240 | list_for_each_entry(c, &cdev->configs, list) { | ||
241 | /* ignore configs that won't work at this speed */ | ||
242 | if (speed == USB_SPEED_HIGH) { | ||
243 | if (!c->highspeed) | ||
244 | continue; | ||
245 | } else { | ||
246 | if (!c->fullspeed) | ||
247 | continue; | ||
248 | } | ||
249 | if (w_value == 0) | ||
250 | return config_buf(c, speed, cdev->req->buf, type); | ||
251 | w_value--; | ||
252 | } | ||
253 | return -EINVAL; | ||
254 | } | ||
255 | |||
256 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) | ||
257 | { | ||
258 | struct usb_gadget *gadget = cdev->gadget; | ||
259 | struct usb_configuration *c; | ||
260 | unsigned count = 0; | ||
261 | int hs = 0; | ||
262 | |||
263 | if (gadget_is_dualspeed(gadget)) { | ||
264 | if (gadget->speed == USB_SPEED_HIGH) | ||
265 | hs = 1; | ||
266 | if (type == USB_DT_DEVICE_QUALIFIER) | ||
267 | hs = !hs; | ||
268 | } | ||
269 | list_for_each_entry(c, &cdev->configs, list) { | ||
270 | /* ignore configs that won't work at this speed */ | ||
271 | if (hs) { | ||
272 | if (!c->highspeed) | ||
273 | continue; | ||
274 | } else { | ||
275 | if (!c->fullspeed) | ||
276 | continue; | ||
277 | } | ||
278 | count++; | ||
279 | } | ||
280 | return count; | ||
281 | } | ||
282 | |||
283 | static void device_qual(struct usb_composite_dev *cdev) | ||
284 | { | ||
285 | struct usb_qualifier_descriptor *qual = cdev->req->buf; | ||
286 | |||
287 | qual->bLength = sizeof(*qual); | ||
288 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; | ||
289 | /* POLICY: same bcdUSB and device type info at both speeds */ | ||
290 | qual->bcdUSB = cdev->desc.bcdUSB; | ||
291 | qual->bDeviceClass = cdev->desc.bDeviceClass; | ||
292 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; | ||
293 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; | ||
294 | /* ASSUME same EP0 fifo size at both speeds */ | ||
295 | qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0; | ||
296 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); | ||
297 | } | ||
298 | |||
299 | /*-------------------------------------------------------------------------*/ | ||
300 | |||
301 | static void reset_config(struct usb_composite_dev *cdev) | ||
302 | { | ||
303 | struct usb_function *f; | ||
304 | |||
305 | DBG(cdev, "reset config\n"); | ||
306 | |||
307 | list_for_each_entry(f, &cdev->config->functions, list) { | ||
308 | if (f->disable) | ||
309 | f->disable(f); | ||
310 | } | ||
311 | cdev->config = NULL; | ||
312 | } | ||
313 | |||
314 | static int set_config(struct usb_composite_dev *cdev, | ||
315 | const struct usb_ctrlrequest *ctrl, unsigned number) | ||
316 | { | ||
317 | struct usb_gadget *gadget = cdev->gadget; | ||
318 | struct usb_configuration *c = NULL; | ||
319 | int result = -EINVAL; | ||
320 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; | ||
321 | int tmp; | ||
322 | |||
323 | if (cdev->config) | ||
324 | reset_config(cdev); | ||
325 | |||
326 | if (number) { | ||
327 | list_for_each_entry(c, &cdev->configs, list) { | ||
328 | if (c->bConfigurationValue == number) { | ||
329 | result = 0; | ||
330 | break; | ||
331 | } | ||
332 | } | ||
333 | if (result < 0) | ||
334 | goto done; | ||
335 | } else | ||
336 | result = 0; | ||
337 | |||
338 | INFO(cdev, "%s speed config #%d: %s\n", | ||
339 | ({ char *speed; | ||
340 | switch (gadget->speed) { | ||
341 | case USB_SPEED_LOW: speed = "low"; break; | ||
342 | case USB_SPEED_FULL: speed = "full"; break; | ||
343 | case USB_SPEED_HIGH: speed = "high"; break; | ||
344 | default: speed = "?"; break; | ||
345 | } ; speed; }), number, c ? c->label : "unconfigured"); | ||
346 | |||
347 | if (!c) | ||
348 | goto done; | ||
349 | |||
350 | cdev->config = c; | ||
351 | |||
352 | /* Initialize all interfaces by setting them to altsetting zero. */ | ||
353 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { | ||
354 | struct usb_function *f = c->interface[tmp]; | ||
355 | |||
356 | if (!f) | ||
357 | break; | ||
358 | |||
359 | result = f->set_alt(f, tmp, 0); | ||
360 | if (result < 0) { | ||
361 | DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", | ||
362 | tmp, f->name, f, result); | ||
363 | |||
364 | reset_config(cdev); | ||
365 | goto done; | ||
366 | } | ||
367 | } | ||
368 | |||
369 | /* when we return, be sure our power usage is valid */ | ||
370 | power = 2 * c->bMaxPower; | ||
371 | done: | ||
372 | usb_gadget_vbus_draw(gadget, power); | ||
373 | return result; | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * usb_add_config() - add a configuration to a device. | ||
378 | * @cdev: wraps the USB gadget | ||
379 | * @config: the configuration, with bConfigurationValue assigned | ||
380 | * Context: single threaded during gadget setup | ||
381 | * | ||
382 | * One of the main tasks of a composite driver's bind() routine is to | ||
383 | * add each of the configurations it supports, using this routine. | ||
384 | * | ||
385 | * This function returns the value of the configuration's bind(), which | ||
386 | * is zero for success else a negative errno value. Binding configurations | ||
387 | * assigns global resources including string IDs, and per-configuration | ||
388 | * resources such as interface IDs and endpoints. | ||
389 | */ | ||
390 | int __init usb_add_config(struct usb_composite_dev *cdev, | ||
391 | struct usb_configuration *config) | ||
392 | { | ||
393 | int status = -EINVAL; | ||
394 | struct usb_configuration *c; | ||
395 | |||
396 | DBG(cdev, "adding config #%u '%s'/%p\n", | ||
397 | config->bConfigurationValue, | ||
398 | config->label, config); | ||
399 | |||
400 | if (!config->bConfigurationValue || !config->bind) | ||
401 | goto done; | ||
402 | |||
403 | /* Prevent duplicate configuration identifiers */ | ||
404 | list_for_each_entry(c, &cdev->configs, list) { | ||
405 | if (c->bConfigurationValue == config->bConfigurationValue) { | ||
406 | status = -EBUSY; | ||
407 | goto done; | ||
408 | } | ||
409 | } | ||
410 | |||
411 | config->cdev = cdev; | ||
412 | list_add_tail(&config->list, &cdev->configs); | ||
413 | |||
414 | INIT_LIST_HEAD(&config->functions); | ||
415 | config->next_interface_id = 0; | ||
416 | |||
417 | status = config->bind(config); | ||
418 | if (status < 0) { | ||
419 | list_del(&config->list); | ||
420 | config->cdev = NULL; | ||
421 | } else { | ||
422 | unsigned i; | ||
423 | |||
424 | DBG(cdev, "cfg %d/%p speeds:%s%s\n", | ||
425 | config->bConfigurationValue, config, | ||
426 | config->highspeed ? " high" : "", | ||
427 | config->fullspeed | ||
428 | ? (gadget_is_dualspeed(cdev->gadget) | ||
429 | ? " full" | ||
430 | : " full/low") | ||
431 | : ""); | ||
432 | |||
433 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { | ||
434 | struct usb_function *f = config->interface[i]; | ||
435 | |||
436 | if (!f) | ||
437 | continue; | ||
438 | DBG(cdev, " interface %d = %s/%p\n", | ||
439 | i, f->name, f); | ||
440 | } | ||
441 | } | ||
442 | |||
443 | /* set_alt(), or next config->bind(), sets up | ||
444 | * ep->driver_data as needed. | ||
445 | */ | ||
446 | usb_ep_autoconfig_reset(cdev->gadget); | ||
447 | |||
448 | done: | ||
449 | if (status) | ||
450 | DBG(cdev, "added config '%s'/%u --> %d\n", config->label, | ||
451 | config->bConfigurationValue, status); | ||
452 | return status; | ||
453 | } | ||
454 | |||
455 | /*-------------------------------------------------------------------------*/ | ||
456 | |||
457 | /* We support strings in multiple languages ... string descriptor zero | ||
458 | * says which languages are supported. The typical case will be that | ||
459 | * only one language (probably English) is used, with I18N handled on | ||
460 | * the host side. | ||
461 | */ | ||
462 | |||
463 | static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) | ||
464 | { | ||
465 | const struct usb_gadget_strings *s; | ||
466 | u16 language; | ||
467 | __le16 *tmp; | ||
468 | |||
469 | while (*sp) { | ||
470 | s = *sp; | ||
471 | language = cpu_to_le16(s->language); | ||
472 | for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { | ||
473 | if (*tmp == language) | ||
474 | goto repeat; | ||
475 | } | ||
476 | *tmp++ = language; | ||
477 | repeat: | ||
478 | sp++; | ||
479 | } | ||
480 | } | ||
481 | |||
482 | static int lookup_string( | ||
483 | struct usb_gadget_strings **sp, | ||
484 | void *buf, | ||
485 | u16 language, | ||
486 | int id | ||
487 | ) | ||
488 | { | ||
489 | struct usb_gadget_strings *s; | ||
490 | int value; | ||
491 | |||
492 | while (*sp) { | ||
493 | s = *sp++; | ||
494 | if (s->language != language) | ||
495 | continue; | ||
496 | value = usb_gadget_get_string(s, id, buf); | ||
497 | if (value > 0) | ||
498 | return value; | ||
499 | } | ||
500 | return -EINVAL; | ||
501 | } | ||
502 | |||
503 | static int get_string(struct usb_composite_dev *cdev, | ||
504 | void *buf, u16 language, int id) | ||
505 | { | ||
506 | struct usb_configuration *c; | ||
507 | struct usb_function *f; | ||
508 | int len; | ||
509 | |||
510 | /* Yes, not only is USB's I18N support probably more than most | ||
511 | * folk will ever care about ... also, it's all supported here. | ||
512 | * (Except for UTF8 support for Unicode's "Astral Planes".) | ||
513 | */ | ||
514 | |||
515 | /* 0 == report all available language codes */ | ||
516 | if (id == 0) { | ||
517 | struct usb_string_descriptor *s = buf; | ||
518 | struct usb_gadget_strings **sp; | ||
519 | |||
520 | memset(s, 0, 256); | ||
521 | s->bDescriptorType = USB_DT_STRING; | ||
522 | |||
523 | sp = composite->strings; | ||
524 | if (sp) | ||
525 | collect_langs(sp, s->wData); | ||
526 | |||
527 | list_for_each_entry(c, &cdev->configs, list) { | ||
528 | sp = c->strings; | ||
529 | if (sp) | ||
530 | collect_langs(sp, s->wData); | ||
531 | |||
532 | list_for_each_entry(f, &c->functions, list) { | ||
533 | sp = f->strings; | ||
534 | if (sp) | ||
535 | collect_langs(sp, s->wData); | ||
536 | } | ||
537 | } | ||
538 | |||
539 | for (len = 0; s->wData[len] && len <= 126; len++) | ||
540 | continue; | ||
541 | if (!len) | ||
542 | return -EINVAL; | ||
543 | |||
544 | s->bLength = 2 * (len + 1); | ||
545 | return s->bLength; | ||
546 | } | ||
547 | |||
548 | /* Otherwise, look up and return a specified string. String IDs | ||
549 | * are device-scoped, so we look up each string table we're told | ||
550 | * about. These lookups are infrequent; simpler-is-better here. | ||
551 | */ | ||
552 | if (composite->strings) { | ||
553 | len = lookup_string(composite->strings, buf, language, id); | ||
554 | if (len > 0) | ||
555 | return len; | ||
556 | } | ||
557 | list_for_each_entry(c, &cdev->configs, list) { | ||
558 | if (c->strings) { | ||
559 | len = lookup_string(c->strings, buf, language, id); | ||
560 | if (len > 0) | ||
561 | return len; | ||
562 | } | ||
563 | list_for_each_entry(f, &c->functions, list) { | ||
564 | if (!f->strings) | ||
565 | continue; | ||
566 | len = lookup_string(f->strings, buf, language, id); | ||
567 | if (len > 0) | ||
568 | return len; | ||
569 | } | ||
570 | } | ||
571 | return -EINVAL; | ||
572 | } | ||
573 | |||
574 | /** | ||
575 | * usb_string_id() - allocate an unused string ID | ||
576 | * @cdev: the device whose string descriptor IDs are being allocated | ||
577 | * Context: single threaded during gadget setup | ||
578 | * | ||
579 | * @usb_string_id() is called from bind() callbacks to allocate | ||
580 | * string IDs. Drivers for functions, configurations, or gadgets will | ||
581 | * then store that ID in the appropriate descriptors and string table. | ||
582 | * | ||
583 | * All string identifier should be allocated using this routine, to | ||
584 | * ensure that for example different functions don't wrongly assign | ||
585 | * different meanings to the same identifier. | ||
586 | */ | ||
587 | int __init usb_string_id(struct usb_composite_dev *cdev) | ||
588 | { | ||
589 | if (cdev->next_string_id < 254) { | ||
590 | /* string id 0 is reserved */ | ||
591 | cdev->next_string_id++; | ||
592 | return cdev->next_string_id; | ||
593 | } | ||
594 | return -ENODEV; | ||
595 | } | ||
596 | |||
597 | /*-------------------------------------------------------------------------*/ | ||
598 | |||
599 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) | ||
600 | { | ||
601 | if (req->status || req->actual != req->length) | ||
602 | DBG((struct usb_composite_dev *) ep->driver_data, | ||
603 | "setup complete --> %d, %d/%d\n", | ||
604 | req->status, req->actual, req->length); | ||
605 | } | ||
606 | |||
607 | /* | ||
608 | * The setup() callback implements all the ep0 functionality that's | ||
609 | * not handled lower down, in hardware or the hardware driver(like | ||
610 | * device and endpoint feature flags, and their status). It's all | ||
611 | * housekeeping for the gadget function we're implementing. Most of | ||
612 | * the work is in config and function specific setup. | ||
613 | */ | ||
614 | static int | ||
615 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) | ||
616 | { | ||
617 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
618 | struct usb_request *req = cdev->req; | ||
619 | int value = -EOPNOTSUPP; | ||
620 | u16 w_index = le16_to_cpu(ctrl->wIndex); | ||
621 | u16 w_value = le16_to_cpu(ctrl->wValue); | ||
622 | u16 w_length = le16_to_cpu(ctrl->wLength); | ||
623 | struct usb_function *f = NULL; | ||
624 | |||
625 | /* partial re-init of the response message; the function or the | ||
626 | * gadget might need to intercept e.g. a control-OUT completion | ||
627 | * when we delegate to it. | ||
628 | */ | ||
629 | req->zero = 0; | ||
630 | req->complete = composite_setup_complete; | ||
631 | req->length = USB_BUFSIZ; | ||
632 | gadget->ep0->driver_data = cdev; | ||
633 | |||
634 | switch (ctrl->bRequest) { | ||
635 | |||
636 | /* we handle all standard USB descriptors */ | ||
637 | case USB_REQ_GET_DESCRIPTOR: | ||
638 | if (ctrl->bRequestType != USB_DIR_IN) | ||
639 | goto unknown; | ||
640 | switch (w_value >> 8) { | ||
641 | |||
642 | case USB_DT_DEVICE: | ||
643 | cdev->desc.bNumConfigurations = | ||
644 | count_configs(cdev, USB_DT_DEVICE); | ||
645 | value = min(w_length, (u16) sizeof cdev->desc); | ||
646 | memcpy(req->buf, &cdev->desc, value); | ||
647 | break; | ||
648 | case USB_DT_DEVICE_QUALIFIER: | ||
649 | if (!gadget_is_dualspeed(gadget)) | ||
650 | break; | ||
651 | device_qual(cdev); | ||
652 | value = min_t(int, w_length, | ||
653 | sizeof(struct usb_qualifier_descriptor)); | ||
654 | break; | ||
655 | case USB_DT_OTHER_SPEED_CONFIG: | ||
656 | if (!gadget_is_dualspeed(gadget)) | ||
657 | break; | ||
658 | /* FALLTHROUGH */ | ||
659 | case USB_DT_CONFIG: | ||
660 | value = config_desc(cdev, w_value); | ||
661 | if (value >= 0) | ||
662 | value = min(w_length, (u16) value); | ||
663 | break; | ||
664 | case USB_DT_STRING: | ||
665 | value = get_string(cdev, req->buf, | ||
666 | w_index, w_value & 0xff); | ||
667 | if (value >= 0) | ||
668 | value = min(w_length, (u16) value); | ||
669 | break; | ||
670 | } | ||
671 | break; | ||
672 | |||
673 | /* any number of configs can work */ | ||
674 | case USB_REQ_SET_CONFIGURATION: | ||
675 | if (ctrl->bRequestType != 0) | ||
676 | goto unknown; | ||
677 | if (gadget_is_otg(gadget)) { | ||
678 | if (gadget->a_hnp_support) | ||
679 | DBG(cdev, "HNP available\n"); | ||
680 | else if (gadget->a_alt_hnp_support) | ||
681 | DBG(cdev, "HNP on another port\n"); | ||
682 | else | ||
683 | VDBG(cdev, "HNP inactive\n"); | ||
684 | } | ||
685 | spin_lock(&cdev->lock); | ||
686 | value = set_config(cdev, ctrl, w_value); | ||
687 | spin_unlock(&cdev->lock); | ||
688 | break; | ||
689 | case USB_REQ_GET_CONFIGURATION: | ||
690 | if (ctrl->bRequestType != USB_DIR_IN) | ||
691 | goto unknown; | ||
692 | if (cdev->config) | ||
693 | *(u8 *)req->buf = cdev->config->bConfigurationValue; | ||
694 | else | ||
695 | *(u8 *)req->buf = 0; | ||
696 | value = min(w_length, (u16) 1); | ||
697 | break; | ||
698 | |||
699 | /* function drivers must handle get/set altsetting; if there's | ||
700 | * no get() method, we know only altsetting zero works. | ||
701 | */ | ||
702 | case USB_REQ_SET_INTERFACE: | ||
703 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) | ||
704 | goto unknown; | ||
705 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) | ||
706 | break; | ||
707 | f = cdev->config->interface[w_index]; | ||
708 | if (!f) | ||
709 | break; | ||
710 | if (w_value && !f->get_alt) | ||
711 | break; | ||
712 | value = f->set_alt(f, w_index, w_value); | ||
713 | break; | ||
714 | case USB_REQ_GET_INTERFACE: | ||
715 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) | ||
716 | goto unknown; | ||
717 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) | ||
718 | break; | ||
719 | f = cdev->config->interface[w_index]; | ||
720 | if (!f) | ||
721 | break; | ||
722 | /* lots of interfaces only need altsetting zero... */ | ||
723 | value = f->get_alt ? f->get_alt(f, w_index) : 0; | ||
724 | if (value < 0) | ||
725 | break; | ||
726 | *((u8 *)req->buf) = value; | ||
727 | value = min(w_length, (u16) 1); | ||
728 | break; | ||
729 | default: | ||
730 | unknown: | ||
731 | VDBG(cdev, | ||
732 | "non-core control req%02x.%02x v%04x i%04x l%d\n", | ||
733 | ctrl->bRequestType, ctrl->bRequest, | ||
734 | w_value, w_index, w_length); | ||
735 | |||
736 | /* functions always handle their interfaces ... punt other | ||
737 | * recipients (endpoint, other, WUSB, ...) to the current | ||
738 | * configuration code. | ||
739 | * | ||
740 | * REVISIT it could make sense to let the composite device | ||
741 | * take such requests too, if that's ever needed: to work | ||
742 | * in config 0, etc. | ||
743 | */ | ||
744 | if ((ctrl->bRequestType & USB_RECIP_MASK) | ||
745 | == USB_RECIP_INTERFACE) { | ||
746 | f = cdev->config->interface[w_index]; | ||
747 | if (f && f->setup) | ||
748 | value = f->setup(f, ctrl); | ||
749 | else | ||
750 | f = NULL; | ||
751 | } | ||
752 | if (value < 0 && !f) { | ||
753 | struct usb_configuration *c; | ||
754 | |||
755 | c = cdev->config; | ||
756 | if (c && c->setup) | ||
757 | value = c->setup(c, ctrl); | ||
758 | } | ||
759 | |||
760 | goto done; | ||
761 | } | ||
762 | |||
763 | /* respond with data transfer before status phase? */ | ||
764 | if (value >= 0) { | ||
765 | req->length = value; | ||
766 | req->zero = value < w_length; | ||
767 | value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC); | ||
768 | if (value < 0) { | ||
769 | DBG(cdev, "ep_queue --> %d\n", value); | ||
770 | req->status = 0; | ||
771 | composite_setup_complete(gadget->ep0, req); | ||
772 | } | ||
773 | } | ||
774 | |||
775 | done: | ||
776 | /* device either stalls (value < 0) or reports success */ | ||
777 | return value; | ||
778 | } | ||
779 | |||
780 | static void composite_disconnect(struct usb_gadget *gadget) | ||
781 | { | ||
782 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
783 | unsigned long flags; | ||
784 | |||
785 | /* REVISIT: should we have config and device level | ||
786 | * disconnect callbacks? | ||
787 | */ | ||
788 | spin_lock_irqsave(&cdev->lock, flags); | ||
789 | if (cdev->config) | ||
790 | reset_config(cdev); | ||
791 | spin_unlock_irqrestore(&cdev->lock, flags); | ||
792 | } | ||
793 | |||
794 | /*-------------------------------------------------------------------------*/ | ||
795 | |||
796 | static void /* __init_or_exit */ | ||
797 | composite_unbind(struct usb_gadget *gadget) | ||
798 | { | ||
799 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
800 | |||
801 | /* composite_disconnect() must already have been called | ||
802 | * by the underlying peripheral controller driver! | ||
803 | * so there's no i/o concurrency that could affect the | ||
804 | * state protected by cdev->lock. | ||
805 | */ | ||
806 | WARN_ON(cdev->config); | ||
807 | |||
808 | while (!list_empty(&cdev->configs)) { | ||
809 | struct usb_configuration *c; | ||
810 | |||
811 | c = list_first_entry(&cdev->configs, | ||
812 | struct usb_configuration, list); | ||
813 | while (!list_empty(&c->functions)) { | ||
814 | struct usb_function *f; | ||
815 | |||
816 | f = list_first_entry(&c->functions, | ||
817 | struct usb_function, list); | ||
818 | list_del(&f->list); | ||
819 | if (f->unbind) { | ||
820 | DBG(cdev, "unbind function '%s'/%p\n", | ||
821 | f->name, f); | ||
822 | f->unbind(c, f); | ||
823 | /* may free memory for "f" */ | ||
824 | } | ||
825 | } | ||
826 | list_del(&c->list); | ||
827 | if (c->unbind) { | ||
828 | DBG(cdev, "unbind config '%s'/%p\n", c->label, c); | ||
829 | c->unbind(c); | ||
830 | /* may free memory for "c" */ | ||
831 | } | ||
832 | } | ||
833 | if (composite->unbind) | ||
834 | composite->unbind(cdev); | ||
835 | |||
836 | if (cdev->req) { | ||
837 | kfree(cdev->req->buf); | ||
838 | usb_ep_free_request(gadget->ep0, cdev->req); | ||
839 | } | ||
840 | kfree(cdev); | ||
841 | set_gadget_data(gadget, NULL); | ||
842 | composite = NULL; | ||
843 | } | ||
844 | |||
845 | static void __init | ||
846 | string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s) | ||
847 | { | ||
848 | struct usb_string *str = tab->strings; | ||
849 | |||
850 | for (str = tab->strings; str->s; str++) { | ||
851 | if (str->id == id) { | ||
852 | str->s = s; | ||
853 | return; | ||
854 | } | ||
855 | } | ||
856 | } | ||
857 | |||
858 | static void __init | ||
859 | string_override(struct usb_gadget_strings **tab, u8 id, const char *s) | ||
860 | { | ||
861 | while (*tab) { | ||
862 | string_override_one(*tab, id, s); | ||
863 | tab++; | ||
864 | } | ||
865 | } | ||
866 | |||
867 | static int __init composite_bind(struct usb_gadget *gadget) | ||
868 | { | ||
869 | struct usb_composite_dev *cdev; | ||
870 | int status = -ENOMEM; | ||
871 | |||
872 | cdev = kzalloc(sizeof *cdev, GFP_KERNEL); | ||
873 | if (!cdev) | ||
874 | return status; | ||
875 | |||
876 | spin_lock_init(&cdev->lock); | ||
877 | cdev->gadget = gadget; | ||
878 | set_gadget_data(gadget, cdev); | ||
879 | INIT_LIST_HEAD(&cdev->configs); | ||
880 | |||
881 | /* preallocate control response and buffer */ | ||
882 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); | ||
883 | if (!cdev->req) | ||
884 | goto fail; | ||
885 | cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL); | ||
886 | if (!cdev->req->buf) | ||
887 | goto fail; | ||
888 | cdev->req->complete = composite_setup_complete; | ||
889 | gadget->ep0->driver_data = cdev; | ||
890 | |||
891 | cdev->bufsiz = USB_BUFSIZ; | ||
892 | cdev->driver = composite; | ||
893 | |||
894 | usb_gadget_set_selfpowered(gadget); | ||
895 | |||
896 | /* interface and string IDs start at zero via kzalloc. | ||
897 | * we force endpoints to start unassigned; few controller | ||
898 | * drivers will zero ep->driver_data. | ||
899 | */ | ||
900 | usb_ep_autoconfig_reset(cdev->gadget); | ||
901 | |||
902 | /* composite gadget needs to assign strings for whole device (like | ||
903 | * serial number), register function drivers, potentially update | ||
904 | * power state and consumption, etc | ||
905 | */ | ||
906 | status = composite->bind(cdev); | ||
907 | if (status < 0) | ||
908 | goto fail; | ||
909 | |||
910 | cdev->desc = *composite->dev; | ||
911 | cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; | ||
912 | |||
913 | /* standardized runtime overrides for device ID data */ | ||
914 | if (idVendor) | ||
915 | cdev->desc.idVendor = cpu_to_le16(idVendor); | ||
916 | if (idProduct) | ||
917 | cdev->desc.idProduct = cpu_to_le16(idProduct); | ||
918 | if (bcdDevice) | ||
919 | cdev->desc.bcdDevice = cpu_to_le16(bcdDevice); | ||
920 | |||
921 | /* strings can't be assigned before bind() allocates the | ||
922 | * releavnt identifiers | ||
923 | */ | ||
924 | if (cdev->desc.iManufacturer && iManufacturer) | ||
925 | string_override(composite->strings, | ||
926 | cdev->desc.iManufacturer, iManufacturer); | ||
927 | if (cdev->desc.iProduct && iProduct) | ||
928 | string_override(composite->strings, | ||
929 | cdev->desc.iProduct, iProduct); | ||
930 | if (cdev->desc.iSerialNumber && iSerialNumber) | ||
931 | string_override(composite->strings, | ||
932 | cdev->desc.iSerialNumber, iSerialNumber); | ||
933 | |||
934 | INFO(cdev, "%s ready\n", composite->name); | ||
935 | return 0; | ||
936 | |||
937 | fail: | ||
938 | composite_unbind(gadget); | ||
939 | return status; | ||
940 | } | ||
941 | |||
942 | /*-------------------------------------------------------------------------*/ | ||
943 | |||
944 | static void | ||
945 | composite_suspend(struct usb_gadget *gadget) | ||
946 | { | ||
947 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
948 | struct usb_function *f; | ||
949 | |||
950 | /* REVISIT: should we have config and device level | ||
951 | * suspend/resume callbacks? | ||
952 | */ | ||
953 | DBG(cdev, "suspend\n"); | ||
954 | if (cdev->config) { | ||
955 | list_for_each_entry(f, &cdev->config->functions, list) { | ||
956 | if (f->suspend) | ||
957 | f->suspend(f); | ||
958 | } | ||
959 | } | ||
960 | } | ||
961 | |||
962 | static void | ||
963 | composite_resume(struct usb_gadget *gadget) | ||
964 | { | ||
965 | struct usb_composite_dev *cdev = get_gadget_data(gadget); | ||
966 | struct usb_function *f; | ||
967 | |||
968 | /* REVISIT: should we have config and device level | ||
969 | * suspend/resume callbacks? | ||
970 | */ | ||
971 | DBG(cdev, "resume\n"); | ||
972 | if (cdev->config) { | ||
973 | list_for_each_entry(f, &cdev->config->functions, list) { | ||
974 | if (f->resume) | ||
975 | f->resume(f); | ||
976 | } | ||
977 | } | ||
978 | } | ||
979 | |||
980 | /*-------------------------------------------------------------------------*/ | ||
981 | |||
982 | static struct usb_gadget_driver composite_driver = { | ||
983 | .speed = USB_SPEED_HIGH, | ||
984 | |||
985 | .bind = composite_bind, | ||
986 | .unbind = __exit_p(composite_unbind), | ||
987 | |||
988 | .setup = composite_setup, | ||
989 | .disconnect = composite_disconnect, | ||
990 | |||
991 | .suspend = composite_suspend, | ||
992 | .resume = composite_resume, | ||
993 | |||
994 | .driver = { | ||
995 | .owner = THIS_MODULE, | ||
996 | }, | ||
997 | }; | ||
998 | |||
999 | /** | ||
1000 | * usb_composite_register() - register a composite driver | ||
1001 | * @driver: the driver to register | ||
1002 | * Context: single threaded during gadget setup | ||
1003 | * | ||
1004 | * This function is used to register drivers using the composite driver | ||
1005 | * framework. The return value is zero, or a negative errno value. | ||
1006 | * Those values normally come from the driver's @bind method, which does | ||
1007 | * all the work of setting up the driver to match the hardware. | ||
1008 | * | ||
1009 | * On successful return, the gadget is ready to respond to requests from | ||
1010 | * the host, unless one of its components invokes usb_gadget_disconnect() | ||
1011 | * while it was binding. That would usually be done in order to wait for | ||
1012 | * some userspace participation. | ||
1013 | */ | ||
1014 | int __init usb_composite_register(struct usb_composite_driver *driver) | ||
1015 | { | ||
1016 | if (!driver || !driver->dev || !driver->bind || composite) | ||
1017 | return -EINVAL; | ||
1018 | |||
1019 | if (!driver->name) | ||
1020 | driver->name = "composite"; | ||
1021 | composite_driver.function = (char *) driver->name; | ||
1022 | composite_driver.driver.name = driver->name; | ||
1023 | composite = driver; | ||
1024 | |||
1025 | return usb_gadget_register_driver(&composite_driver); | ||
1026 | } | ||
1027 | |||
1028 | /** | ||
1029 | * usb_composite_unregister() - unregister a composite driver | ||
1030 | * @driver: the driver to unregister | ||
1031 | * | ||
1032 | * This function is used to unregister drivers using the composite | ||
1033 | * driver framework. | ||
1034 | */ | ||
1035 | void __exit usb_composite_unregister(struct usb_composite_driver *driver) | ||
1036 | { | ||
1037 | if (composite != driver) | ||
1038 | return; | ||
1039 | usb_gadget_unregister_driver(&composite_driver); | ||
1040 | } | ||
diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h new file mode 100644 index 000000000000..747c3a49cdc9 --- /dev/null +++ b/include/linux/usb/composite.h | |||
@@ -0,0 +1,338 @@ | |||
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 | |||
127 | /* internals */ | ||
128 | struct list_head list; | ||
129 | }; | ||
130 | |||
131 | int usb_add_function(struct usb_configuration *, struct usb_function *); | ||
132 | |||
133 | int usb_interface_id(struct usb_configuration *, struct usb_function *); | ||
134 | |||
135 | /** | ||
136 | * ep_choose - select descriptor endpoint at current device speed | ||
137 | * @g: gadget, connected and running at some speed | ||
138 | * @hs: descriptor to use for high speed operation | ||
139 | * @fs: descriptor to use for full or low speed operation | ||
140 | */ | ||
141 | static inline struct usb_endpoint_descriptor * | ||
142 | ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs, | ||
143 | struct usb_endpoint_descriptor *fs) | ||
144 | { | ||
145 | if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | ||
146 | return hs; | ||
147 | return fs; | ||
148 | } | ||
149 | |||
150 | #define MAX_CONFIG_INTERFACES 16 /* arbitrary; max 255 */ | ||
151 | |||
152 | /** | ||
153 | * struct usb_configuration - represents one gadget configuration | ||
154 | * @label: For diagnostics, describes the configuration. | ||
155 | * @strings: Tables of strings, keyed by identifiers assigned during @bind() | ||
156 | * and by language IDs provided in control requests. | ||
157 | * @descriptors: Table of descriptors preceding all function descriptors. | ||
158 | * Examples include OTG and vendor-specific descriptors. | ||
159 | * @bind: Called from @usb_add_config() to allocate resources unique to this | ||
160 | * configuration and to call @usb_add_function() for each function used. | ||
161 | * @unbind: Reverses @bind; called as a side effect of unregistering the | ||
162 | * driver which added this configuration. | ||
163 | * @setup: Used to delegate control requests that aren't handled by standard | ||
164 | * device infrastructure or directed at a specific interface. | ||
165 | * @bConfigurationValue: Copied into configuration descriptor. | ||
166 | * @iConfiguration: Copied into configuration descriptor. | ||
167 | * @bmAttributes: Copied into configuration descriptor. | ||
168 | * @bMaxPower: Copied into configuration descriptor. | ||
169 | * @cdev: assigned by @usb_add_config() before calling @bind(); this is | ||
170 | * the device associated with this configuration. | ||
171 | * | ||
172 | * Configurations are building blocks for gadget drivers structured around | ||
173 | * function drivers. Simple USB gadgets require only one function and one | ||
174 | * configuration, and handle dual-speed hardware by always providing the same | ||
175 | * functionality. Slightly more complex gadgets may have more than one | ||
176 | * single-function configuration at a given speed; or have configurations | ||
177 | * that only work at one speed. | ||
178 | * | ||
179 | * Composite devices are, by definition, ones with configurations which | ||
180 | * include more than one function. | ||
181 | * | ||
182 | * The lifecycle of a usb_configuration includes allocation, initialization | ||
183 | * of the fields described above, and calling @usb_add_config() to set up | ||
184 | * internal data and bind it to a specific device. The configuration's | ||
185 | * @bind() method is then used to initialize all the functions and then | ||
186 | * call @usb_add_function() for them. | ||
187 | * | ||
188 | * Those functions would normally be independant of each other, but that's | ||
189 | * not mandatory. CDC WMC devices are an example where functions often | ||
190 | * depend on other functions, with some functions subsidiary to others. | ||
191 | * Such interdependency may be managed in any way, so long as all of the | ||
192 | * descriptors complete by the time the composite driver returns from | ||
193 | * its bind() routine. | ||
194 | */ | ||
195 | struct usb_configuration { | ||
196 | const char *label; | ||
197 | struct usb_gadget_strings **strings; | ||
198 | const struct usb_descriptor_header **descriptors; | ||
199 | |||
200 | /* REVISIT: bind() functions can be marked __init, which | ||
201 | * makes trouble for section mismatch analysis. See if | ||
202 | * we can't restructure things to avoid mismatching... | ||
203 | */ | ||
204 | |||
205 | /* configuration management: bind/unbind */ | ||
206 | int (*bind)(struct usb_configuration *); | ||
207 | void (*unbind)(struct usb_configuration *); | ||
208 | int (*setup)(struct usb_configuration *, | ||
209 | const struct usb_ctrlrequest *); | ||
210 | |||
211 | /* fields in the config descriptor */ | ||
212 | u8 bConfigurationValue; | ||
213 | u8 iConfiguration; | ||
214 | u8 bmAttributes; | ||
215 | u8 bMaxPower; | ||
216 | |||
217 | struct usb_composite_dev *cdev; | ||
218 | |||
219 | /* internals */ | ||
220 | struct list_head list; | ||
221 | struct list_head functions; | ||
222 | u8 next_interface_id; | ||
223 | unsigned highspeed:1; | ||
224 | unsigned fullspeed:1; | ||
225 | struct usb_function *interface[MAX_CONFIG_INTERFACES]; | ||
226 | }; | ||
227 | |||
228 | int usb_add_config(struct usb_composite_dev *, | ||
229 | struct usb_configuration *); | ||
230 | |||
231 | /** | ||
232 | * struct usb_composite_driver - groups configurations into a gadget | ||
233 | * @name: For diagnostics, identifies the driver. | ||
234 | * @dev: Template descriptor for the device, including default device | ||
235 | * identifiers. | ||
236 | * @strings: tables of strings, keyed by identifiers assigned during bind() | ||
237 | * and language IDs provided in control requests | ||
238 | * @bind: (REQUIRED) Used to allocate resources that are shared across the | ||
239 | * whole device, such as string IDs, and add its configurations using | ||
240 | * @usb_add_config(). This may fail by returning a negative errno | ||
241 | * value; it should return zero on successful initialization. | ||
242 | * @unbind: Reverses @bind(); called as a side effect of unregistering | ||
243 | * this driver. | ||
244 | * | ||
245 | * Devices default to reporting self powered operation. Devices which rely | ||
246 | * on bus powered operation should report this in their @bind() method. | ||
247 | * | ||
248 | * Before returning from @bind, various fields in the template descriptor | ||
249 | * may be overridden. These include the idVendor/idProduct/bcdDevice values | ||
250 | * normally to bind the appropriate host side driver, and the three strings | ||
251 | * (iManufacturer, iProduct, iSerialNumber) normally used to provide user | ||
252 | * meaningful device identifiers. (The strings will not be defined unless | ||
253 | * they are defined in @dev and @strings.) The correct ep0 maxpacket size | ||
254 | * is also reported, as defined by the underlying controller driver. | ||
255 | */ | ||
256 | struct usb_composite_driver { | ||
257 | const char *name; | ||
258 | const struct usb_device_descriptor *dev; | ||
259 | struct usb_gadget_strings **strings; | ||
260 | |||
261 | /* REVISIT: bind() functions can be marked __init, which | ||
262 | * makes trouble for section mismatch analysis. See if | ||
263 | * we can't restructure things to avoid mismatching... | ||
264 | */ | ||
265 | |||
266 | int (*bind)(struct usb_composite_dev *); | ||
267 | int (*unbind)(struct usb_composite_dev *); | ||
268 | }; | ||
269 | |||
270 | extern int usb_composite_register(struct usb_composite_driver *); | ||
271 | extern void usb_composite_unregister(struct usb_composite_driver *); | ||
272 | |||
273 | |||
274 | /** | ||
275 | * struct usb_composite_device - represents one composite usb gadget | ||
276 | * @gadget: read-only, abstracts the gadget's usb peripheral controller | ||
277 | * @req: used for control responses; buffer is pre-allocated | ||
278 | * @bufsiz: size of buffer pre-allocated in @req | ||
279 | * @config: the currently active configuration | ||
280 | * | ||
281 | * One of these devices is allocated and initialized before the | ||
282 | * associated device driver's bind() is called. | ||
283 | * | ||
284 | * OPEN ISSUE: it appears that some WUSB devices will need to be | ||
285 | * built by combining a normal (wired) gadget with a wireless one. | ||
286 | * This revision of the gadget framework should probably try to make | ||
287 | * sure doing that won't hurt too much. | ||
288 | * | ||
289 | * One notion for how to handle Wireless USB devices involves: | ||
290 | * (a) a second gadget here, discovery mechanism TBD, but likely | ||
291 | * needing separate "register/unregister WUSB gadget" calls; | ||
292 | * (b) updates to usb_gadget to include flags "is it wireless", | ||
293 | * "is it wired", plus (presumably in a wrapper structure) | ||
294 | * bandgroup and PHY info; | ||
295 | * (c) presumably a wireless_ep wrapping a usb_ep, and reporting | ||
296 | * wireless-specific parameters like maxburst and maxsequence; | ||
297 | * (d) configurations that are specific to wireless links; | ||
298 | * (e) function drivers that understand wireless configs and will | ||
299 | * support wireless for (additional) function instances; | ||
300 | * (f) a function to support association setup (like CBAF), not | ||
301 | * necessarily requiring a wireless adapter; | ||
302 | * (g) composite device setup that can create one or more wireless | ||
303 | * configs, including appropriate association setup support; | ||
304 | * (h) more, TBD. | ||
305 | */ | ||
306 | struct usb_composite_dev { | ||
307 | struct usb_gadget *gadget; | ||
308 | struct usb_request *req; | ||
309 | unsigned bufsiz; | ||
310 | |||
311 | struct usb_configuration *config; | ||
312 | |||
313 | /* internals */ | ||
314 | struct usb_device_descriptor desc; | ||
315 | struct list_head configs; | ||
316 | struct usb_composite_driver *driver; | ||
317 | u8 next_string_id; | ||
318 | |||
319 | spinlock_t lock; | ||
320 | |||
321 | /* REVISIT use and existence of lock ... */ | ||
322 | }; | ||
323 | |||
324 | extern int usb_string_id(struct usb_composite_dev *c); | ||
325 | |||
326 | /* messaging utils */ | ||
327 | #define DBG(d, fmt, args...) \ | ||
328 | dev_dbg(&(d)->gadget->dev , fmt , ## args) | ||
329 | #define VDBG(d, fmt, args...) \ | ||
330 | dev_vdbg(&(d)->gadget->dev , fmt , ## args) | ||
331 | #define ERROR(d, fmt, args...) \ | ||
332 | dev_err(&(d)->gadget->dev , fmt , ## args) | ||
333 | #define WARN(d, fmt, args...) \ | ||
334 | dev_warn(&(d)->gadget->dev , fmt , ## args) | ||
335 | #define INFO(d, fmt, args...) \ | ||
336 | dev_info(&(d)->gadget->dev , fmt , ## args) | ||
337 | |||
338 | #endif /* __LINUX_USB_COMPOSITE_H */ | ||