blob: eaa9a599df63707d94c82f80941cfc8d4c908f44 [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
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 */
Robert Lukassendd0543e2010-03-30 14:14:01 +020039#define USB_BUFSIZ 1024
David Brownell40982be2008-06-19 17:52:58 -070040
41static 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
48static ushort idVendor;
49module_param(idVendor, ushort, 0);
50MODULE_PARM_DESC(idVendor, "USB Vendor ID");
51
52static ushort idProduct;
53module_param(idProduct, ushort, 0);
54MODULE_PARM_DESC(idProduct, "USB Product ID");
55
56static ushort bcdDevice;
57module_param(bcdDevice, ushort, 0);
58MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
59
60static char *iManufacturer;
61module_param(iManufacturer, charp, 0);
62MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
63
64static char *iProduct;
65module_param(iProduct, charp, 0);
66MODULE_PARM_DESC(iProduct, "USB Product string");
67
68static char *iSerialNumber;
69module_param(iSerialNumber, charp, 0);
70MODULE_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 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020088int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -070089 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
123done:
124 if (value)
125 DBG(config->cdev, "adding '%s'/%p --> %d\n",
126 function->name, function, value);
127 return value;
128}
129
130/**
David Brownell60beed92008-08-18 17:38:22 -0700131 * usb_function_deactivate - prevent function and gadget enumeration
132 * @function: the function that isn't yet ready to respond
133 *
134 * Blocks response of the gadget driver to host enumeration by
135 * preventing the data line pullup from being activated. This is
136 * normally called during @bind() processing to change from the
137 * initial "ready to respond" state, or when a required resource
138 * becomes available.
139 *
140 * For example, drivers that serve as a passthrough to a userspace
141 * daemon can block enumeration unless that daemon (such as an OBEX,
142 * MTP, or print server) is ready to handle host requests.
143 *
144 * Not all systems support software control of their USB peripheral
145 * data pullups.
146 *
147 * Returns zero on success, else negative errno.
148 */
149int usb_function_deactivate(struct usb_function *function)
150{
151 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200152 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700153 int status = 0;
154
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200155 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700156
157 if (cdev->deactivations == 0)
158 status = usb_gadget_disconnect(cdev->gadget);
159 if (status == 0)
160 cdev->deactivations++;
161
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200162 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700163 return status;
164}
165
166/**
167 * usb_function_activate - allow function and gadget enumeration
168 * @function: function on which usb_function_activate() was called
169 *
170 * Reverses effect of usb_function_deactivate(). If no more functions
171 * are delaying their activation, the gadget driver will respond to
172 * host enumeration procedures.
173 *
174 * Returns zero on success, else negative errno.
175 */
176int usb_function_activate(struct usb_function *function)
177{
178 struct usb_composite_dev *cdev = function->config->cdev;
179 int status = 0;
180
181 spin_lock(&cdev->lock);
182
183 if (WARN_ON(cdev->deactivations == 0))
184 status = -EINVAL;
185 else {
186 cdev->deactivations--;
187 if (cdev->deactivations == 0)
188 status = usb_gadget_connect(cdev->gadget);
189 }
190
191 spin_unlock(&cdev->lock);
192 return status;
193}
194
195/**
David Brownell40982be2008-06-19 17:52:58 -0700196 * usb_interface_id() - allocate an unused interface ID
197 * @config: configuration associated with the interface
198 * @function: function handling the interface
199 * Context: single threaded during gadget setup
200 *
201 * usb_interface_id() is called from usb_function.bind() callbacks to
202 * allocate new interface IDs. The function driver will then store that
203 * ID in interface, association, CDC union, and other descriptors. It
204 * will also handle any control requests targetted at that interface,
205 * particularly changing its altsetting via set_alt(). There may
206 * also be class-specific or vendor-specific requests to handle.
207 *
208 * All interface identifier should be allocated using this routine, to
209 * ensure that for example different functions don't wrongly assign
210 * different meanings to the same identifier. Note that since interface
211 * identifers are configuration-specific, functions used in more than
212 * one configuration (or more than once in a given configuration) need
213 * multiple versions of the relevant descriptors.
214 *
215 * Returns the interface ID which was allocated; or -ENODEV if no
216 * more interface IDs can be allocated.
217 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200218int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700219 struct usb_function *function)
220{
221 unsigned id = config->next_interface_id;
222
223 if (id < MAX_CONFIG_INTERFACES) {
224 config->interface[id] = function;
225 config->next_interface_id = id + 1;
226 return id;
227 }
228 return -ENODEV;
229}
230
231static int config_buf(struct usb_configuration *config,
232 enum usb_device_speed speed, void *buf, u8 type)
233{
234 struct usb_config_descriptor *c = buf;
235 void *next = buf + USB_DT_CONFIG_SIZE;
236 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
237 struct usb_function *f;
238 int status;
239
240 /* write the config descriptor */
241 c = buf;
242 c->bLength = USB_DT_CONFIG_SIZE;
243 c->bDescriptorType = type;
244 /* wTotalLength is written later */
245 c->bNumInterfaces = config->next_interface_id;
246 c->bConfigurationValue = config->bConfigurationValue;
247 c->iConfiguration = config->iConfiguration;
248 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700249 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700250
251 /* There may be e.g. OTG descriptors */
252 if (config->descriptors) {
253 status = usb_descriptor_fillbuf(next, len,
254 config->descriptors);
255 if (status < 0)
256 return status;
257 len -= status;
258 next += status;
259 }
260
261 /* add each function's descriptors */
262 list_for_each_entry(f, &config->functions, list) {
263 struct usb_descriptor_header **descriptors;
264
265 if (speed == USB_SPEED_HIGH)
266 descriptors = f->hs_descriptors;
267 else
268 descriptors = f->descriptors;
269 if (!descriptors)
270 continue;
271 status = usb_descriptor_fillbuf(next, len,
272 (const struct usb_descriptor_header **) descriptors);
273 if (status < 0)
274 return status;
275 len -= status;
276 next += status;
277 }
278
279 len = next - buf;
280 c->wTotalLength = cpu_to_le16(len);
281 return len;
282}
283
284static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
285{
286 struct usb_gadget *gadget = cdev->gadget;
287 struct usb_configuration *c;
288 u8 type = w_value >> 8;
289 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
290
291 if (gadget_is_dualspeed(gadget)) {
292 int hs = 0;
293
294 if (gadget->speed == USB_SPEED_HIGH)
295 hs = 1;
296 if (type == USB_DT_OTHER_SPEED_CONFIG)
297 hs = !hs;
298 if (hs)
299 speed = USB_SPEED_HIGH;
300
301 }
302
303 /* This is a lookup by config *INDEX* */
304 w_value &= 0xff;
305 list_for_each_entry(c, &cdev->configs, list) {
306 /* ignore configs that won't work at this speed */
307 if (speed == USB_SPEED_HIGH) {
308 if (!c->highspeed)
309 continue;
310 } else {
311 if (!c->fullspeed)
312 continue;
313 }
314 if (w_value == 0)
315 return config_buf(c, speed, cdev->req->buf, type);
316 w_value--;
317 }
318 return -EINVAL;
319}
320
321static int count_configs(struct usb_composite_dev *cdev, unsigned type)
322{
323 struct usb_gadget *gadget = cdev->gadget;
324 struct usb_configuration *c;
325 unsigned count = 0;
326 int hs = 0;
327
328 if (gadget_is_dualspeed(gadget)) {
329 if (gadget->speed == USB_SPEED_HIGH)
330 hs = 1;
331 if (type == USB_DT_DEVICE_QUALIFIER)
332 hs = !hs;
333 }
334 list_for_each_entry(c, &cdev->configs, list) {
335 /* ignore configs that won't work at this speed */
336 if (hs) {
337 if (!c->highspeed)
338 continue;
339 } else {
340 if (!c->fullspeed)
341 continue;
342 }
343 count++;
344 }
345 return count;
346}
347
348static void device_qual(struct usb_composite_dev *cdev)
349{
350 struct usb_qualifier_descriptor *qual = cdev->req->buf;
351
352 qual->bLength = sizeof(*qual);
353 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
354 /* POLICY: same bcdUSB and device type info at both speeds */
355 qual->bcdUSB = cdev->desc.bcdUSB;
356 qual->bDeviceClass = cdev->desc.bDeviceClass;
357 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
358 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
359 /* ASSUME same EP0 fifo size at both speeds */
360 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
361 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700362 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700363}
364
365/*-------------------------------------------------------------------------*/
366
367static void reset_config(struct usb_composite_dev *cdev)
368{
369 struct usb_function *f;
370
371 DBG(cdev, "reset config\n");
372
373 list_for_each_entry(f, &cdev->config->functions, list) {
374 if (f->disable)
375 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200376
377 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700378 }
379 cdev->config = NULL;
380}
381
382static int set_config(struct usb_composite_dev *cdev,
383 const struct usb_ctrlrequest *ctrl, unsigned number)
384{
385 struct usb_gadget *gadget = cdev->gadget;
386 struct usb_configuration *c = NULL;
387 int result = -EINVAL;
388 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
389 int tmp;
390
391 if (cdev->config)
392 reset_config(cdev);
393
394 if (number) {
395 list_for_each_entry(c, &cdev->configs, list) {
396 if (c->bConfigurationValue == number) {
397 result = 0;
398 break;
399 }
400 }
401 if (result < 0)
402 goto done;
403 } else
404 result = 0;
405
406 INFO(cdev, "%s speed config #%d: %s\n",
407 ({ char *speed;
408 switch (gadget->speed) {
409 case USB_SPEED_LOW: speed = "low"; break;
410 case USB_SPEED_FULL: speed = "full"; break;
411 case USB_SPEED_HIGH: speed = "high"; break;
412 default: speed = "?"; break;
413 } ; speed; }), number, c ? c->label : "unconfigured");
414
415 if (!c)
416 goto done;
417
418 cdev->config = c;
419
420 /* Initialize all interfaces by setting them to altsetting zero. */
421 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
422 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200423 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700424
425 if (!f)
426 break;
427
Laurent Pinchart52426582009-10-21 00:03:38 +0200428 /*
429 * Record which endpoints are used by the function. This is used
430 * to dispatch control requests targeted at that endpoint to the
431 * function's setup callback instead of the current
432 * configuration's setup callback.
433 */
434 if (gadget->speed == USB_SPEED_HIGH)
435 descriptors = f->hs_descriptors;
436 else
437 descriptors = f->descriptors;
438
439 for (; *descriptors; ++descriptors) {
440 struct usb_endpoint_descriptor *ep;
441 int addr;
442
443 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
444 continue;
445
446 ep = (struct usb_endpoint_descriptor *)*descriptors;
447 addr = ((ep->bEndpointAddress & 0x80) >> 3)
448 | (ep->bEndpointAddress & 0x0f);
449 set_bit(addr, f->endpoints);
450 }
451
David Brownell40982be2008-06-19 17:52:58 -0700452 result = f->set_alt(f, tmp, 0);
453 if (result < 0) {
454 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
455 tmp, f->name, f, result);
456
457 reset_config(cdev);
458 goto done;
459 }
460 }
461
462 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700463 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700464done:
465 usb_gadget_vbus_draw(gadget, power);
466 return result;
467}
468
469/**
470 * usb_add_config() - add a configuration to a device.
471 * @cdev: wraps the USB gadget
472 * @config: the configuration, with bConfigurationValue assigned
473 * Context: single threaded during gadget setup
474 *
475 * One of the main tasks of a composite driver's bind() routine is to
476 * add each of the configurations it supports, using this routine.
477 *
478 * This function returns the value of the configuration's bind(), which
479 * is zero for success else a negative errno value. Binding configurations
480 * assigns global resources including string IDs, and per-configuration
481 * resources such as interface IDs and endpoints.
482 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200483int usb_add_config(struct usb_composite_dev *cdev,
David Brownell40982be2008-06-19 17:52:58 -0700484 struct usb_configuration *config)
485{
486 int status = -EINVAL;
487 struct usb_configuration *c;
488
489 DBG(cdev, "adding config #%u '%s'/%p\n",
490 config->bConfigurationValue,
491 config->label, config);
492
493 if (!config->bConfigurationValue || !config->bind)
494 goto done;
495
496 /* Prevent duplicate configuration identifiers */
497 list_for_each_entry(c, &cdev->configs, list) {
498 if (c->bConfigurationValue == config->bConfigurationValue) {
499 status = -EBUSY;
500 goto done;
501 }
502 }
503
504 config->cdev = cdev;
505 list_add_tail(&config->list, &cdev->configs);
506
507 INIT_LIST_HEAD(&config->functions);
508 config->next_interface_id = 0;
509
510 status = config->bind(config);
511 if (status < 0) {
512 list_del(&config->list);
513 config->cdev = NULL;
514 } else {
515 unsigned i;
516
517 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
518 config->bConfigurationValue, config,
519 config->highspeed ? " high" : "",
520 config->fullspeed
521 ? (gadget_is_dualspeed(cdev->gadget)
522 ? " full"
523 : " full/low")
524 : "");
525
526 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
527 struct usb_function *f = config->interface[i];
528
529 if (!f)
530 continue;
531 DBG(cdev, " interface %d = %s/%p\n",
532 i, f->name, f);
533 }
534 }
535
536 /* set_alt(), or next config->bind(), sets up
537 * ep->driver_data as needed.
538 */
539 usb_ep_autoconfig_reset(cdev->gadget);
540
541done:
542 if (status)
543 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
544 config->bConfigurationValue, status);
545 return status;
546}
547
548/*-------------------------------------------------------------------------*/
549
550/* We support strings in multiple languages ... string descriptor zero
551 * says which languages are supported. The typical case will be that
552 * only one language (probably English) is used, with I18N handled on
553 * the host side.
554 */
555
556static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
557{
558 const struct usb_gadget_strings *s;
559 u16 language;
560 __le16 *tmp;
561
562 while (*sp) {
563 s = *sp;
564 language = cpu_to_le16(s->language);
565 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
566 if (*tmp == language)
567 goto repeat;
568 }
569 *tmp++ = language;
570repeat:
571 sp++;
572 }
573}
574
575static int lookup_string(
576 struct usb_gadget_strings **sp,
577 void *buf,
578 u16 language,
579 int id
580)
581{
582 struct usb_gadget_strings *s;
583 int value;
584
585 while (*sp) {
586 s = *sp++;
587 if (s->language != language)
588 continue;
589 value = usb_gadget_get_string(s, id, buf);
590 if (value > 0)
591 return value;
592 }
593 return -EINVAL;
594}
595
596static int get_string(struct usb_composite_dev *cdev,
597 void *buf, u16 language, int id)
598{
599 struct usb_configuration *c;
600 struct usb_function *f;
601 int len;
602
603 /* Yes, not only is USB's I18N support probably more than most
604 * folk will ever care about ... also, it's all supported here.
605 * (Except for UTF8 support for Unicode's "Astral Planes".)
606 */
607
608 /* 0 == report all available language codes */
609 if (id == 0) {
610 struct usb_string_descriptor *s = buf;
611 struct usb_gadget_strings **sp;
612
613 memset(s, 0, 256);
614 s->bDescriptorType = USB_DT_STRING;
615
616 sp = composite->strings;
617 if (sp)
618 collect_langs(sp, s->wData);
619
620 list_for_each_entry(c, &cdev->configs, list) {
621 sp = c->strings;
622 if (sp)
623 collect_langs(sp, s->wData);
624
625 list_for_each_entry(f, &c->functions, list) {
626 sp = f->strings;
627 if (sp)
628 collect_langs(sp, s->wData);
629 }
630 }
631
Roel Kluin417b57b2009-08-06 16:09:51 -0700632 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700633 continue;
634 if (!len)
635 return -EINVAL;
636
637 s->bLength = 2 * (len + 1);
638 return s->bLength;
639 }
640
641 /* Otherwise, look up and return a specified string. String IDs
642 * are device-scoped, so we look up each string table we're told
643 * about. These lookups are infrequent; simpler-is-better here.
644 */
645 if (composite->strings) {
646 len = lookup_string(composite->strings, buf, language, id);
647 if (len > 0)
648 return len;
649 }
650 list_for_each_entry(c, &cdev->configs, list) {
651 if (c->strings) {
652 len = lookup_string(c->strings, buf, language, id);
653 if (len > 0)
654 return len;
655 }
656 list_for_each_entry(f, &c->functions, list) {
657 if (!f->strings)
658 continue;
659 len = lookup_string(f->strings, buf, language, id);
660 if (len > 0)
661 return len;
662 }
663 }
664 return -EINVAL;
665}
666
667/**
668 * usb_string_id() - allocate an unused string ID
669 * @cdev: the device whose string descriptor IDs are being allocated
670 * Context: single threaded during gadget setup
671 *
672 * @usb_string_id() is called from bind() callbacks to allocate
673 * string IDs. Drivers for functions, configurations, or gadgets will
674 * then store that ID in the appropriate descriptors and string table.
675 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200676 * All string identifier should be allocated using this,
677 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
678 * that for example different functions don't wrongly assign different
679 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700680 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200681int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700682{
683 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200684 /* string id 0 is reserved by USB spec for list of
685 * supported languages */
686 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700687 cdev->next_string_id++;
688 return cdev->next_string_id;
689 }
690 return -ENODEV;
691}
692
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200693/**
694 * usb_string_ids() - allocate unused string IDs in batch
695 * @cdev: the device whose string descriptor IDs are being allocated
696 * @str: an array of usb_string objects to assign numbers to
697 * Context: single threaded during gadget setup
698 *
699 * @usb_string_ids() is called from bind() callbacks to allocate
700 * string IDs. Drivers for functions, configurations, or gadgets will
701 * then copy IDs from the string table to the appropriate descriptors
702 * and string table for other languages.
703 *
704 * All string identifier should be allocated using this,
705 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
706 * example different functions don't wrongly assign different meanings
707 * to the same identifier.
708 */
709int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
710{
711 int next = cdev->next_string_id;
712
713 for (; str->s; ++str) {
714 if (unlikely(next >= 254))
715 return -ENODEV;
716 str->id = ++next;
717 }
718
719 cdev->next_string_id = next;
720
721 return 0;
722}
723
724/**
725 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700726 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200727 * @n: number of string IDs to allocate
728 * Context: single threaded during gadget setup
729 *
730 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700731 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200732 * is, returns last requested ID which is now very useful information.
733 *
734 * @usb_string_ids_n() is called from bind() callbacks to allocate
735 * string IDs. Drivers for functions, configurations, or gadgets will
736 * then store that ID in the appropriate descriptors and string table.
737 *
738 * All string identifier should be allocated using this,
739 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
740 * example different functions don't wrongly assign different meanings
741 * to the same identifier.
742 */
743int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
744{
745 unsigned next = c->next_string_id;
746 if (unlikely(n > 254 || (unsigned)next + n > 254))
747 return -ENODEV;
748 c->next_string_id += n;
749 return next + 1;
750}
751
752
David Brownell40982be2008-06-19 17:52:58 -0700753/*-------------------------------------------------------------------------*/
754
755static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
756{
757 if (req->status || req->actual != req->length)
758 DBG((struct usb_composite_dev *) ep->driver_data,
759 "setup complete --> %d, %d/%d\n",
760 req->status, req->actual, req->length);
761}
762
763/*
764 * The setup() callback implements all the ep0 functionality that's
765 * not handled lower down, in hardware or the hardware driver(like
766 * device and endpoint feature flags, and their status). It's all
767 * housekeeping for the gadget function we're implementing. Most of
768 * the work is in config and function specific setup.
769 */
770static int
771composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
772{
773 struct usb_composite_dev *cdev = get_gadget_data(gadget);
774 struct usb_request *req = cdev->req;
775 int value = -EOPNOTSUPP;
776 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800777 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700778 u16 w_value = le16_to_cpu(ctrl->wValue);
779 u16 w_length = le16_to_cpu(ctrl->wLength);
780 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200781 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700782
783 /* partial re-init of the response message; the function or the
784 * gadget might need to intercept e.g. a control-OUT completion
785 * when we delegate to it.
786 */
787 req->zero = 0;
788 req->complete = composite_setup_complete;
789 req->length = USB_BUFSIZ;
790 gadget->ep0->driver_data = cdev;
791
792 switch (ctrl->bRequest) {
793
794 /* we handle all standard USB descriptors */
795 case USB_REQ_GET_DESCRIPTOR:
796 if (ctrl->bRequestType != USB_DIR_IN)
797 goto unknown;
798 switch (w_value >> 8) {
799
800 case USB_DT_DEVICE:
801 cdev->desc.bNumConfigurations =
802 count_configs(cdev, USB_DT_DEVICE);
803 value = min(w_length, (u16) sizeof cdev->desc);
804 memcpy(req->buf, &cdev->desc, value);
805 break;
806 case USB_DT_DEVICE_QUALIFIER:
807 if (!gadget_is_dualspeed(gadget))
808 break;
809 device_qual(cdev);
810 value = min_t(int, w_length,
811 sizeof(struct usb_qualifier_descriptor));
812 break;
813 case USB_DT_OTHER_SPEED_CONFIG:
814 if (!gadget_is_dualspeed(gadget))
815 break;
816 /* FALLTHROUGH */
817 case USB_DT_CONFIG:
818 value = config_desc(cdev, w_value);
819 if (value >= 0)
820 value = min(w_length, (u16) value);
821 break;
822 case USB_DT_STRING:
823 value = get_string(cdev, req->buf,
824 w_index, w_value & 0xff);
825 if (value >= 0)
826 value = min(w_length, (u16) value);
827 break;
828 }
829 break;
830
831 /* any number of configs can work */
832 case USB_REQ_SET_CONFIGURATION:
833 if (ctrl->bRequestType != 0)
834 goto unknown;
835 if (gadget_is_otg(gadget)) {
836 if (gadget->a_hnp_support)
837 DBG(cdev, "HNP available\n");
838 else if (gadget->a_alt_hnp_support)
839 DBG(cdev, "HNP on another port\n");
840 else
841 VDBG(cdev, "HNP inactive\n");
842 }
843 spin_lock(&cdev->lock);
844 value = set_config(cdev, ctrl, w_value);
845 spin_unlock(&cdev->lock);
846 break;
847 case USB_REQ_GET_CONFIGURATION:
848 if (ctrl->bRequestType != USB_DIR_IN)
849 goto unknown;
850 if (cdev->config)
851 *(u8 *)req->buf = cdev->config->bConfigurationValue;
852 else
853 *(u8 *)req->buf = 0;
854 value = min(w_length, (u16) 1);
855 break;
856
857 /* function drivers must handle get/set altsetting; if there's
858 * no get() method, we know only altsetting zero works.
859 */
860 case USB_REQ_SET_INTERFACE:
861 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
862 goto unknown;
863 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
864 break;
Bryan Wu08889512009-01-08 00:21:19 +0800865 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700866 if (!f)
867 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800868 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700869 break;
870 value = f->set_alt(f, w_index, w_value);
871 break;
872 case USB_REQ_GET_INTERFACE:
873 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
874 goto unknown;
875 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
876 break;
Bryan Wu08889512009-01-08 00:21:19 +0800877 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700878 if (!f)
879 break;
880 /* lots of interfaces only need altsetting zero... */
881 value = f->get_alt ? f->get_alt(f, w_index) : 0;
882 if (value < 0)
883 break;
884 *((u8 *)req->buf) = value;
885 value = min(w_length, (u16) 1);
886 break;
887 default:
888unknown:
889 VDBG(cdev,
890 "non-core control req%02x.%02x v%04x i%04x l%d\n",
891 ctrl->bRequestType, ctrl->bRequest,
892 w_value, w_index, w_length);
893
Laurent Pinchart52426582009-10-21 00:03:38 +0200894 /* functions always handle their interfaces and endpoints...
895 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -0700896 * configuration code.
897 *
898 * REVISIT it could make sense to let the composite device
899 * take such requests too, if that's ever needed: to work
900 * in config 0, etc.
901 */
Laurent Pinchart52426582009-10-21 00:03:38 +0200902 switch (ctrl->bRequestType & USB_RECIP_MASK) {
903 case USB_RECIP_INTERFACE:
Bryan Wu08889512009-01-08 00:21:19 +0800904 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +0200905 break;
906
907 case USB_RECIP_ENDPOINT:
908 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
909 list_for_each_entry(f, &cdev->config->functions, list) {
910 if (test_bit(endp, f->endpoints))
911 break;
912 }
913 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -0700914 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200915 break;
David Brownell40982be2008-06-19 17:52:58 -0700916 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200917
918 if (f && f->setup)
919 value = f->setup(f, ctrl);
920 else {
David Brownell40982be2008-06-19 17:52:58 -0700921 struct usb_configuration *c;
922
923 c = cdev->config;
924 if (c && c->setup)
925 value = c->setup(c, ctrl);
926 }
927
928 goto done;
929 }
930
931 /* respond with data transfer before status phase? */
932 if (value >= 0) {
933 req->length = value;
934 req->zero = value < w_length;
935 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
936 if (value < 0) {
937 DBG(cdev, "ep_queue --> %d\n", value);
938 req->status = 0;
939 composite_setup_complete(gadget->ep0, req);
940 }
941 }
942
943done:
944 /* device either stalls (value < 0) or reports success */
945 return value;
946}
947
948static void composite_disconnect(struct usb_gadget *gadget)
949{
950 struct usb_composite_dev *cdev = get_gadget_data(gadget);
951 unsigned long flags;
952
953 /* REVISIT: should we have config and device level
954 * disconnect callbacks?
955 */
956 spin_lock_irqsave(&cdev->lock, flags);
957 if (cdev->config)
958 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +0200959 if (composite->disconnect)
960 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700961 spin_unlock_irqrestore(&cdev->lock, flags);
962}
963
964/*-------------------------------------------------------------------------*/
965
Fabien Chouteauf48cf802010-04-23 14:21:26 +0200966static ssize_t composite_show_suspended(struct device *dev,
967 struct device_attribute *attr,
968 char *buf)
969{
970 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
971 struct usb_composite_dev *cdev = get_gadget_data(gadget);
972
973 return sprintf(buf, "%d\n", cdev->suspended);
974}
975
976static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
977
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200978static void
David Brownell40982be2008-06-19 17:52:58 -0700979composite_unbind(struct usb_gadget *gadget)
980{
981 struct usb_composite_dev *cdev = get_gadget_data(gadget);
982
983 /* composite_disconnect() must already have been called
984 * by the underlying peripheral controller driver!
985 * so there's no i/o concurrency that could affect the
986 * state protected by cdev->lock.
987 */
988 WARN_ON(cdev->config);
989
990 while (!list_empty(&cdev->configs)) {
991 struct usb_configuration *c;
992
993 c = list_first_entry(&cdev->configs,
994 struct usb_configuration, list);
995 while (!list_empty(&c->functions)) {
996 struct usb_function *f;
997
998 f = list_first_entry(&c->functions,
999 struct usb_function, list);
1000 list_del(&f->list);
1001 if (f->unbind) {
1002 DBG(cdev, "unbind function '%s'/%p\n",
1003 f->name, f);
1004 f->unbind(c, f);
1005 /* may free memory for "f" */
1006 }
1007 }
1008 list_del(&c->list);
1009 if (c->unbind) {
1010 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1011 c->unbind(c);
1012 /* may free memory for "c" */
1013 }
1014 }
1015 if (composite->unbind)
1016 composite->unbind(cdev);
1017
1018 if (cdev->req) {
1019 kfree(cdev->req->buf);
1020 usb_ep_free_request(gadget->ep0, cdev->req);
1021 }
1022 kfree(cdev);
1023 set_gadget_data(gadget, NULL);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001024 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001025 composite = NULL;
1026}
1027
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001028static void
David Brownell40982be2008-06-19 17:52:58 -07001029string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
1030{
1031 struct usb_string *str = tab->strings;
1032
1033 for (str = tab->strings; str->s; str++) {
1034 if (str->id == id) {
1035 str->s = s;
1036 return;
1037 }
1038 }
1039}
1040
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001041static void
David Brownell40982be2008-06-19 17:52:58 -07001042string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
1043{
1044 while (*tab) {
1045 string_override_one(*tab, id, s);
1046 tab++;
1047 }
1048}
1049
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001050static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001051{
1052 struct usb_composite_dev *cdev;
1053 int status = -ENOMEM;
1054
1055 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1056 if (!cdev)
1057 return status;
1058
1059 spin_lock_init(&cdev->lock);
1060 cdev->gadget = gadget;
1061 set_gadget_data(gadget, cdev);
1062 INIT_LIST_HEAD(&cdev->configs);
1063
1064 /* preallocate control response and buffer */
1065 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1066 if (!cdev->req)
1067 goto fail;
1068 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1069 if (!cdev->req->buf)
1070 goto fail;
1071 cdev->req->complete = composite_setup_complete;
1072 gadget->ep0->driver_data = cdev;
1073
1074 cdev->bufsiz = USB_BUFSIZ;
1075 cdev->driver = composite;
1076
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301077 /*
1078 * As per USB compliance update, a device that is actively drawing
1079 * more than 100mA from USB must report itself as bus-powered in
1080 * the GetStatus(DEVICE) call.
1081 */
1082 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1083 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001084
1085 /* interface and string IDs start at zero via kzalloc.
1086 * we force endpoints to start unassigned; few controller
1087 * drivers will zero ep->driver_data.
1088 */
1089 usb_ep_autoconfig_reset(cdev->gadget);
1090
Robert Lukassen1ab83232010-05-07 09:19:53 +02001091 /* standardized runtime overrides for device ID data */
1092 if (idVendor)
1093 cdev->desc.idVendor = cpu_to_le16(idVendor);
1094 if (idProduct)
1095 cdev->desc.idProduct = cpu_to_le16(idProduct);
1096 if (bcdDevice)
1097 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1098
David Brownell40982be2008-06-19 17:52:58 -07001099 /* composite gadget needs to assign strings for whole device (like
1100 * serial number), register function drivers, potentially update
1101 * power state and consumption, etc
1102 */
1103 status = composite->bind(cdev);
1104 if (status < 0)
1105 goto fail;
1106
1107 cdev->desc = *composite->dev;
1108 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1109
David Brownell40982be2008-06-19 17:52:58 -07001110 /* strings can't be assigned before bind() allocates the
1111 * releavnt identifiers
1112 */
1113 if (cdev->desc.iManufacturer && iManufacturer)
1114 string_override(composite->strings,
1115 cdev->desc.iManufacturer, iManufacturer);
1116 if (cdev->desc.iProduct && iProduct)
1117 string_override(composite->strings,
1118 cdev->desc.iProduct, iProduct);
1119 if (cdev->desc.iSerialNumber && iSerialNumber)
1120 string_override(composite->strings,
1121 cdev->desc.iSerialNumber, iSerialNumber);
1122
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001123 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1124 if (status)
1125 goto fail;
1126
David Brownell40982be2008-06-19 17:52:58 -07001127 INFO(cdev, "%s ready\n", composite->name);
1128 return 0;
1129
1130fail:
1131 composite_unbind(gadget);
1132 return status;
1133}
1134
1135/*-------------------------------------------------------------------------*/
1136
1137static void
1138composite_suspend(struct usb_gadget *gadget)
1139{
1140 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1141 struct usb_function *f;
1142
David Brownell89429392009-03-19 14:14:17 -07001143 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001144 * suspend/resume callbacks?
1145 */
1146 DBG(cdev, "suspend\n");
1147 if (cdev->config) {
1148 list_for_each_entry(f, &cdev->config->functions, list) {
1149 if (f->suspend)
1150 f->suspend(f);
1151 }
1152 }
David Brownell89429392009-03-19 14:14:17 -07001153 if (composite->suspend)
1154 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001155
1156 cdev->suspended = 1;
David Brownell40982be2008-06-19 17:52:58 -07001157}
1158
1159static void
1160composite_resume(struct usb_gadget *gadget)
1161{
1162 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1163 struct usb_function *f;
1164
David Brownell89429392009-03-19 14:14:17 -07001165 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001166 * suspend/resume callbacks?
1167 */
1168 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001169 if (composite->resume)
1170 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001171 if (cdev->config) {
1172 list_for_each_entry(f, &cdev->config->functions, list) {
1173 if (f->resume)
1174 f->resume(f);
1175 }
1176 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001177
1178 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001179}
1180
1181/*-------------------------------------------------------------------------*/
1182
1183static struct usb_gadget_driver composite_driver = {
1184 .speed = USB_SPEED_HIGH,
1185
1186 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001187 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001188
1189 .setup = composite_setup,
1190 .disconnect = composite_disconnect,
1191
1192 .suspend = composite_suspend,
1193 .resume = composite_resume,
1194
1195 .driver = {
1196 .owner = THIS_MODULE,
1197 },
1198};
1199
1200/**
1201 * usb_composite_register() - register a composite driver
1202 * @driver: the driver to register
1203 * Context: single threaded during gadget setup
1204 *
1205 * This function is used to register drivers using the composite driver
1206 * framework. The return value is zero, or a negative errno value.
1207 * Those values normally come from the driver's @bind method, which does
1208 * all the work of setting up the driver to match the hardware.
1209 *
1210 * On successful return, the gadget is ready to respond to requests from
1211 * the host, unless one of its components invokes usb_gadget_disconnect()
1212 * while it was binding. That would usually be done in order to wait for
1213 * some userspace participation.
1214 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001215int usb_composite_register(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001216{
1217 if (!driver || !driver->dev || !driver->bind || composite)
1218 return -EINVAL;
1219
1220 if (!driver->name)
1221 driver->name = "composite";
1222 composite_driver.function = (char *) driver->name;
1223 composite_driver.driver.name = driver->name;
1224 composite = driver;
1225
1226 return usb_gadget_register_driver(&composite_driver);
1227}
1228
1229/**
1230 * usb_composite_unregister() - unregister a composite driver
1231 * @driver: the driver to unregister
1232 *
1233 * This function is used to unregister drivers using the composite
1234 * driver framework.
1235 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001236void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001237{
1238 if (composite != driver)
1239 return;
1240 usb_gadget_unregister_driver(&composite_driver);
1241}