blob: 717de39627c782e23eef1fd203d2fafada8dc7ab [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>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020027#include <linux/utsname.h>
David Brownell40982be2008-06-19 17:52:58 -070028
29#include <linux/usb/composite.h>
30
31
32/*
33 * The code in this file is utility code, used to build a gadget driver
34 * from one or more "function" drivers, one or more "configuration"
35 * objects, and a "usb_composite_driver" by gluing them together along
36 * with the relevant device-wide data.
37 */
38
39/* big enough to hold our biggest descriptor */
Robert Lukassendd0543e2010-03-30 14:14:01 +020040#define USB_BUFSIZ 1024
David Brownell40982be2008-06-19 17:52:58 -070041
42static struct usb_composite_driver *composite;
43
44/* Some systems will need runtime overrides for the product identifers
45 * published in the device descriptor, either numbers or strings or both.
46 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
47 */
48
49static ushort idVendor;
50module_param(idVendor, ushort, 0);
51MODULE_PARM_DESC(idVendor, "USB Vendor ID");
52
53static ushort idProduct;
54module_param(idProduct, ushort, 0);
55MODULE_PARM_DESC(idProduct, "USB Product ID");
56
57static ushort bcdDevice;
58module_param(bcdDevice, ushort, 0);
59MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
60
61static char *iManufacturer;
62module_param(iManufacturer, charp, 0);
63MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
64
65static char *iProduct;
66module_param(iProduct, charp, 0);
67MODULE_PARM_DESC(iProduct, "USB Product string");
68
69static char *iSerialNumber;
70module_param(iSerialNumber, charp, 0);
71MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
72
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020073static char composite_manufacturer[50];
74
David Brownell40982be2008-06-19 17:52:58 -070075/*-------------------------------------------------------------------------*/
76
77/**
78 * usb_add_function() - add a function to a configuration
79 * @config: the configuration
80 * @function: the function being added
81 * Context: single threaded during gadget setup
82 *
83 * After initialization, each configuration must have one or more
84 * functions added to it. Adding a function involves calling its @bind()
85 * method to allocate resources such as interface and string identifiers
86 * and endpoints.
87 *
88 * This function returns the value of the function's bind(), which is
89 * zero for success else a negative errno value.
90 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020091int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -070092 struct usb_function *function)
93{
94 int value = -EINVAL;
95
96 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
97 function->name, function,
98 config->label, config);
99
100 if (!function->set_alt || !function->disable)
101 goto done;
102
103 function->config = config;
104 list_add_tail(&function->list, &config->functions);
105
106 /* REVISIT *require* function->bind? */
107 if (function->bind) {
108 value = function->bind(config, function);
109 if (value < 0) {
110 list_del(&function->list);
111 function->config = NULL;
112 }
113 } else
114 value = 0;
115
116 /* We allow configurations that don't work at both speeds.
117 * If we run into a lowspeed Linux system, treat it the same
118 * as full speed ... it's the function drivers that will need
119 * to avoid bulk and ISO transfers.
120 */
121 if (!config->fullspeed && function->descriptors)
122 config->fullspeed = true;
123 if (!config->highspeed && function->hs_descriptors)
124 config->highspeed = true;
125
126done:
127 if (value)
128 DBG(config->cdev, "adding '%s'/%p --> %d\n",
129 function->name, function, value);
130 return value;
131}
132
133/**
David Brownell60beed92008-08-18 17:38:22 -0700134 * usb_function_deactivate - prevent function and gadget enumeration
135 * @function: the function that isn't yet ready to respond
136 *
137 * Blocks response of the gadget driver to host enumeration by
138 * preventing the data line pullup from being activated. This is
139 * normally called during @bind() processing to change from the
140 * initial "ready to respond" state, or when a required resource
141 * becomes available.
142 *
143 * For example, drivers that serve as a passthrough to a userspace
144 * daemon can block enumeration unless that daemon (such as an OBEX,
145 * MTP, or print server) is ready to handle host requests.
146 *
147 * Not all systems support software control of their USB peripheral
148 * data pullups.
149 *
150 * Returns zero on success, else negative errno.
151 */
152int usb_function_deactivate(struct usb_function *function)
153{
154 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200155 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700156 int status = 0;
157
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200158 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700159
160 if (cdev->deactivations == 0)
161 status = usb_gadget_disconnect(cdev->gadget);
162 if (status == 0)
163 cdev->deactivations++;
164
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200165 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700166 return status;
167}
168
169/**
170 * usb_function_activate - allow function and gadget enumeration
171 * @function: function on which usb_function_activate() was called
172 *
173 * Reverses effect of usb_function_deactivate(). If no more functions
174 * are delaying their activation, the gadget driver will respond to
175 * host enumeration procedures.
176 *
177 * Returns zero on success, else negative errno.
178 */
179int usb_function_activate(struct usb_function *function)
180{
181 struct usb_composite_dev *cdev = function->config->cdev;
182 int status = 0;
183
184 spin_lock(&cdev->lock);
185
186 if (WARN_ON(cdev->deactivations == 0))
187 status = -EINVAL;
188 else {
189 cdev->deactivations--;
190 if (cdev->deactivations == 0)
191 status = usb_gadget_connect(cdev->gadget);
192 }
193
194 spin_unlock(&cdev->lock);
195 return status;
196}
197
198/**
David Brownell40982be2008-06-19 17:52:58 -0700199 * usb_interface_id() - allocate an unused interface ID
200 * @config: configuration associated with the interface
201 * @function: function handling the interface
202 * Context: single threaded during gadget setup
203 *
204 * usb_interface_id() is called from usb_function.bind() callbacks to
205 * allocate new interface IDs. The function driver will then store that
206 * ID in interface, association, CDC union, and other descriptors. It
207 * will also handle any control requests targetted at that interface,
208 * particularly changing its altsetting via set_alt(). There may
209 * also be class-specific or vendor-specific requests to handle.
210 *
211 * All interface identifier should be allocated using this routine, to
212 * ensure that for example different functions don't wrongly assign
213 * different meanings to the same identifier. Note that since interface
214 * identifers are configuration-specific, functions used in more than
215 * one configuration (or more than once in a given configuration) need
216 * multiple versions of the relevant descriptors.
217 *
218 * Returns the interface ID which was allocated; or -ENODEV if no
219 * more interface IDs can be allocated.
220 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200221int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700222 struct usb_function *function)
223{
224 unsigned id = config->next_interface_id;
225
226 if (id < MAX_CONFIG_INTERFACES) {
227 config->interface[id] = function;
228 config->next_interface_id = id + 1;
229 return id;
230 }
231 return -ENODEV;
232}
233
234static int config_buf(struct usb_configuration *config,
235 enum usb_device_speed speed, void *buf, u8 type)
236{
237 struct usb_config_descriptor *c = buf;
238 void *next = buf + USB_DT_CONFIG_SIZE;
239 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
240 struct usb_function *f;
241 int status;
242
243 /* write the config descriptor */
244 c = buf;
245 c->bLength = USB_DT_CONFIG_SIZE;
246 c->bDescriptorType = type;
247 /* wTotalLength is written later */
248 c->bNumInterfaces = config->next_interface_id;
249 c->bConfigurationValue = config->bConfigurationValue;
250 c->iConfiguration = config->iConfiguration;
251 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700252 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700253
254 /* There may be e.g. OTG descriptors */
255 if (config->descriptors) {
256 status = usb_descriptor_fillbuf(next, len,
257 config->descriptors);
258 if (status < 0)
259 return status;
260 len -= status;
261 next += status;
262 }
263
264 /* add each function's descriptors */
265 list_for_each_entry(f, &config->functions, list) {
266 struct usb_descriptor_header **descriptors;
267
268 if (speed == USB_SPEED_HIGH)
269 descriptors = f->hs_descriptors;
270 else
271 descriptors = f->descriptors;
272 if (!descriptors)
273 continue;
274 status = usb_descriptor_fillbuf(next, len,
275 (const struct usb_descriptor_header **) descriptors);
276 if (status < 0)
277 return status;
278 len -= status;
279 next += status;
280 }
281
282 len = next - buf;
283 c->wTotalLength = cpu_to_le16(len);
284 return len;
285}
286
287static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
288{
289 struct usb_gadget *gadget = cdev->gadget;
290 struct usb_configuration *c;
291 u8 type = w_value >> 8;
292 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
293
294 if (gadget_is_dualspeed(gadget)) {
295 int hs = 0;
296
297 if (gadget->speed == USB_SPEED_HIGH)
298 hs = 1;
299 if (type == USB_DT_OTHER_SPEED_CONFIG)
300 hs = !hs;
301 if (hs)
302 speed = USB_SPEED_HIGH;
303
304 }
305
306 /* This is a lookup by config *INDEX* */
307 w_value &= 0xff;
308 list_for_each_entry(c, &cdev->configs, list) {
309 /* ignore configs that won't work at this speed */
310 if (speed == USB_SPEED_HIGH) {
311 if (!c->highspeed)
312 continue;
313 } else {
314 if (!c->fullspeed)
315 continue;
316 }
317 if (w_value == 0)
318 return config_buf(c, speed, cdev->req->buf, type);
319 w_value--;
320 }
321 return -EINVAL;
322}
323
324static int count_configs(struct usb_composite_dev *cdev, unsigned type)
325{
326 struct usb_gadget *gadget = cdev->gadget;
327 struct usb_configuration *c;
328 unsigned count = 0;
329 int hs = 0;
330
331 if (gadget_is_dualspeed(gadget)) {
332 if (gadget->speed == USB_SPEED_HIGH)
333 hs = 1;
334 if (type == USB_DT_DEVICE_QUALIFIER)
335 hs = !hs;
336 }
337 list_for_each_entry(c, &cdev->configs, list) {
338 /* ignore configs that won't work at this speed */
339 if (hs) {
340 if (!c->highspeed)
341 continue;
342 } else {
343 if (!c->fullspeed)
344 continue;
345 }
346 count++;
347 }
348 return count;
349}
350
351static void device_qual(struct usb_composite_dev *cdev)
352{
353 struct usb_qualifier_descriptor *qual = cdev->req->buf;
354
355 qual->bLength = sizeof(*qual);
356 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
357 /* POLICY: same bcdUSB and device type info at both speeds */
358 qual->bcdUSB = cdev->desc.bcdUSB;
359 qual->bDeviceClass = cdev->desc.bDeviceClass;
360 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
361 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
362 /* ASSUME same EP0 fifo size at both speeds */
363 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
364 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700365 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700366}
367
368/*-------------------------------------------------------------------------*/
369
370static void reset_config(struct usb_composite_dev *cdev)
371{
372 struct usb_function *f;
373
374 DBG(cdev, "reset config\n");
375
376 list_for_each_entry(f, &cdev->config->functions, list) {
377 if (f->disable)
378 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200379
380 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700381 }
382 cdev->config = NULL;
383}
384
385static int set_config(struct usb_composite_dev *cdev,
386 const struct usb_ctrlrequest *ctrl, unsigned number)
387{
388 struct usb_gadget *gadget = cdev->gadget;
389 struct usb_configuration *c = NULL;
390 int result = -EINVAL;
391 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
392 int tmp;
393
394 if (cdev->config)
395 reset_config(cdev);
396
397 if (number) {
398 list_for_each_entry(c, &cdev->configs, list) {
399 if (c->bConfigurationValue == number) {
400 result = 0;
401 break;
402 }
403 }
404 if (result < 0)
405 goto done;
406 } else
407 result = 0;
408
409 INFO(cdev, "%s speed config #%d: %s\n",
410 ({ char *speed;
411 switch (gadget->speed) {
412 case USB_SPEED_LOW: speed = "low"; break;
413 case USB_SPEED_FULL: speed = "full"; break;
414 case USB_SPEED_HIGH: speed = "high"; break;
415 default: speed = "?"; break;
416 } ; speed; }), number, c ? c->label : "unconfigured");
417
418 if (!c)
419 goto done;
420
421 cdev->config = c;
422
423 /* Initialize all interfaces by setting them to altsetting zero. */
424 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
425 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200426 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700427
428 if (!f)
429 break;
430
Laurent Pinchart52426582009-10-21 00:03:38 +0200431 /*
432 * Record which endpoints are used by the function. This is used
433 * to dispatch control requests targeted at that endpoint to the
434 * function's setup callback instead of the current
435 * configuration's setup callback.
436 */
437 if (gadget->speed == USB_SPEED_HIGH)
438 descriptors = f->hs_descriptors;
439 else
440 descriptors = f->descriptors;
441
442 for (; *descriptors; ++descriptors) {
443 struct usb_endpoint_descriptor *ep;
444 int addr;
445
446 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
447 continue;
448
449 ep = (struct usb_endpoint_descriptor *)*descriptors;
450 addr = ((ep->bEndpointAddress & 0x80) >> 3)
451 | (ep->bEndpointAddress & 0x0f);
452 set_bit(addr, f->endpoints);
453 }
454
David Brownell40982be2008-06-19 17:52:58 -0700455 result = f->set_alt(f, tmp, 0);
456 if (result < 0) {
457 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
458 tmp, f->name, f, result);
459
460 reset_config(cdev);
461 goto done;
462 }
463 }
464
465 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700466 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700467done:
468 usb_gadget_vbus_draw(gadget, power);
469 return result;
470}
471
472/**
473 * usb_add_config() - add a configuration to a device.
474 * @cdev: wraps the USB gadget
475 * @config: the configuration, with bConfigurationValue assigned
476 * Context: single threaded during gadget setup
477 *
478 * One of the main tasks of a composite driver's bind() routine is to
479 * add each of the configurations it supports, using this routine.
480 *
481 * This function returns the value of the configuration's bind(), which
482 * is zero for success else a negative errno value. Binding configurations
483 * assigns global resources including string IDs, and per-configuration
484 * resources such as interface IDs and endpoints.
485 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200486int usb_add_config(struct usb_composite_dev *cdev,
David Brownell40982be2008-06-19 17:52:58 -0700487 struct usb_configuration *config)
488{
489 int status = -EINVAL;
490 struct usb_configuration *c;
491
492 DBG(cdev, "adding config #%u '%s'/%p\n",
493 config->bConfigurationValue,
494 config->label, config);
495
496 if (!config->bConfigurationValue || !config->bind)
497 goto done;
498
499 /* Prevent duplicate configuration identifiers */
500 list_for_each_entry(c, &cdev->configs, list) {
501 if (c->bConfigurationValue == config->bConfigurationValue) {
502 status = -EBUSY;
503 goto done;
504 }
505 }
506
507 config->cdev = cdev;
508 list_add_tail(&config->list, &cdev->configs);
509
510 INIT_LIST_HEAD(&config->functions);
511 config->next_interface_id = 0;
512
513 status = config->bind(config);
514 if (status < 0) {
515 list_del(&config->list);
516 config->cdev = NULL;
517 } else {
518 unsigned i;
519
520 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
521 config->bConfigurationValue, config,
522 config->highspeed ? " high" : "",
523 config->fullspeed
524 ? (gadget_is_dualspeed(cdev->gadget)
525 ? " full"
526 : " full/low")
527 : "");
528
529 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
530 struct usb_function *f = config->interface[i];
531
532 if (!f)
533 continue;
534 DBG(cdev, " interface %d = %s/%p\n",
535 i, f->name, f);
536 }
537 }
538
539 /* set_alt(), or next config->bind(), sets up
540 * ep->driver_data as needed.
541 */
542 usb_ep_autoconfig_reset(cdev->gadget);
543
544done:
545 if (status)
546 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
547 config->bConfigurationValue, status);
548 return status;
549}
550
551/*-------------------------------------------------------------------------*/
552
553/* We support strings in multiple languages ... string descriptor zero
554 * says which languages are supported. The typical case will be that
555 * only one language (probably English) is used, with I18N handled on
556 * the host side.
557 */
558
559static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
560{
561 const struct usb_gadget_strings *s;
562 u16 language;
563 __le16 *tmp;
564
565 while (*sp) {
566 s = *sp;
567 language = cpu_to_le16(s->language);
568 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
569 if (*tmp == language)
570 goto repeat;
571 }
572 *tmp++ = language;
573repeat:
574 sp++;
575 }
576}
577
578static int lookup_string(
579 struct usb_gadget_strings **sp,
580 void *buf,
581 u16 language,
582 int id
583)
584{
585 struct usb_gadget_strings *s;
586 int value;
587
588 while (*sp) {
589 s = *sp++;
590 if (s->language != language)
591 continue;
592 value = usb_gadget_get_string(s, id, buf);
593 if (value > 0)
594 return value;
595 }
596 return -EINVAL;
597}
598
599static int get_string(struct usb_composite_dev *cdev,
600 void *buf, u16 language, int id)
601{
602 struct usb_configuration *c;
603 struct usb_function *f;
604 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200605 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700606
607 /* Yes, not only is USB's I18N support probably more than most
608 * folk will ever care about ... also, it's all supported here.
609 * (Except for UTF8 support for Unicode's "Astral Planes".)
610 */
611
612 /* 0 == report all available language codes */
613 if (id == 0) {
614 struct usb_string_descriptor *s = buf;
615 struct usb_gadget_strings **sp;
616
617 memset(s, 0, 256);
618 s->bDescriptorType = USB_DT_STRING;
619
620 sp = composite->strings;
621 if (sp)
622 collect_langs(sp, s->wData);
623
624 list_for_each_entry(c, &cdev->configs, list) {
625 sp = c->strings;
626 if (sp)
627 collect_langs(sp, s->wData);
628
629 list_for_each_entry(f, &c->functions, list) {
630 sp = f->strings;
631 if (sp)
632 collect_langs(sp, s->wData);
633 }
634 }
635
Roel Kluin417b57b2009-08-06 16:09:51 -0700636 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700637 continue;
638 if (!len)
639 return -EINVAL;
640
641 s->bLength = 2 * (len + 1);
642 return s->bLength;
643 }
644
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200645 /* Otherwise, look up and return a specified string. First
646 * check if the string has not been overridden.
647 */
648 if (cdev->manufacturer_override == id)
649 str = iManufacturer ?: composite->iManufacturer ?:
650 composite_manufacturer;
651 else if (cdev->product_override == id)
652 str = iProduct ?: composite->iProduct;
653 else if (cdev->serial_override == id)
654 str = iSerialNumber;
655 else
656 str = NULL;
657 if (str) {
658 struct usb_gadget_strings strings = {
659 .language = language,
660 .strings = &(struct usb_string) { 0xff, str }
661 };
662 return usb_gadget_get_string(&strings, 0xff, buf);
663 }
664
665 /* String IDs are device-scoped, so we look up each string
666 * table we're told about. These lookups are infrequent;
667 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700668 */
669 if (composite->strings) {
670 len = lookup_string(composite->strings, buf, language, id);
671 if (len > 0)
672 return len;
673 }
674 list_for_each_entry(c, &cdev->configs, list) {
675 if (c->strings) {
676 len = lookup_string(c->strings, buf, language, id);
677 if (len > 0)
678 return len;
679 }
680 list_for_each_entry(f, &c->functions, list) {
681 if (!f->strings)
682 continue;
683 len = lookup_string(f->strings, buf, language, id);
684 if (len > 0)
685 return len;
686 }
687 }
688 return -EINVAL;
689}
690
691/**
692 * usb_string_id() - allocate an unused string ID
693 * @cdev: the device whose string descriptor IDs are being allocated
694 * Context: single threaded during gadget setup
695 *
696 * @usb_string_id() is called from bind() callbacks to allocate
697 * string IDs. Drivers for functions, configurations, or gadgets will
698 * then store that ID in the appropriate descriptors and string table.
699 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200700 * All string identifier should be allocated using this,
701 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
702 * that for example different functions don't wrongly assign different
703 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700704 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200705int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700706{
707 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200708 /* string id 0 is reserved by USB spec for list of
709 * supported languages */
710 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700711 cdev->next_string_id++;
712 return cdev->next_string_id;
713 }
714 return -ENODEV;
715}
716
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200717/**
718 * usb_string_ids() - allocate unused string IDs in batch
719 * @cdev: the device whose string descriptor IDs are being allocated
720 * @str: an array of usb_string objects to assign numbers to
721 * Context: single threaded during gadget setup
722 *
723 * @usb_string_ids() is called from bind() callbacks to allocate
724 * string IDs. Drivers for functions, configurations, or gadgets will
725 * then copy IDs from the string table to the appropriate descriptors
726 * and string table for other languages.
727 *
728 * All string identifier should be allocated using this,
729 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
730 * example different functions don't wrongly assign different meanings
731 * to the same identifier.
732 */
733int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
734{
735 int next = cdev->next_string_id;
736
737 for (; str->s; ++str) {
738 if (unlikely(next >= 254))
739 return -ENODEV;
740 str->id = ++next;
741 }
742
743 cdev->next_string_id = next;
744
745 return 0;
746}
747
748/**
749 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700750 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200751 * @n: number of string IDs to allocate
752 * Context: single threaded during gadget setup
753 *
754 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700755 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200756 * is, returns last requested ID which is now very useful information.
757 *
758 * @usb_string_ids_n() is called from bind() callbacks to allocate
759 * string IDs. Drivers for functions, configurations, or gadgets will
760 * then store that ID in the appropriate descriptors and string table.
761 *
762 * All string identifier should be allocated using this,
763 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
764 * example different functions don't wrongly assign different meanings
765 * to the same identifier.
766 */
767int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
768{
769 unsigned next = c->next_string_id;
770 if (unlikely(n > 254 || (unsigned)next + n > 254))
771 return -ENODEV;
772 c->next_string_id += n;
773 return next + 1;
774}
775
776
David Brownell40982be2008-06-19 17:52:58 -0700777/*-------------------------------------------------------------------------*/
778
779static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
780{
781 if (req->status || req->actual != req->length)
782 DBG((struct usb_composite_dev *) ep->driver_data,
783 "setup complete --> %d, %d/%d\n",
784 req->status, req->actual, req->length);
785}
786
787/*
788 * The setup() callback implements all the ep0 functionality that's
789 * not handled lower down, in hardware or the hardware driver(like
790 * device and endpoint feature flags, and their status). It's all
791 * housekeeping for the gadget function we're implementing. Most of
792 * the work is in config and function specific setup.
793 */
794static int
795composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
796{
797 struct usb_composite_dev *cdev = get_gadget_data(gadget);
798 struct usb_request *req = cdev->req;
799 int value = -EOPNOTSUPP;
800 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800801 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700802 u16 w_value = le16_to_cpu(ctrl->wValue);
803 u16 w_length = le16_to_cpu(ctrl->wLength);
804 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200805 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700806
807 /* partial re-init of the response message; the function or the
808 * gadget might need to intercept e.g. a control-OUT completion
809 * when we delegate to it.
810 */
811 req->zero = 0;
812 req->complete = composite_setup_complete;
813 req->length = USB_BUFSIZ;
814 gadget->ep0->driver_data = cdev;
815
816 switch (ctrl->bRequest) {
817
818 /* we handle all standard USB descriptors */
819 case USB_REQ_GET_DESCRIPTOR:
820 if (ctrl->bRequestType != USB_DIR_IN)
821 goto unknown;
822 switch (w_value >> 8) {
823
824 case USB_DT_DEVICE:
825 cdev->desc.bNumConfigurations =
826 count_configs(cdev, USB_DT_DEVICE);
827 value = min(w_length, (u16) sizeof cdev->desc);
828 memcpy(req->buf, &cdev->desc, value);
829 break;
830 case USB_DT_DEVICE_QUALIFIER:
831 if (!gadget_is_dualspeed(gadget))
832 break;
833 device_qual(cdev);
834 value = min_t(int, w_length,
835 sizeof(struct usb_qualifier_descriptor));
836 break;
837 case USB_DT_OTHER_SPEED_CONFIG:
838 if (!gadget_is_dualspeed(gadget))
839 break;
840 /* FALLTHROUGH */
841 case USB_DT_CONFIG:
842 value = config_desc(cdev, w_value);
843 if (value >= 0)
844 value = min(w_length, (u16) value);
845 break;
846 case USB_DT_STRING:
847 value = get_string(cdev, req->buf,
848 w_index, w_value & 0xff);
849 if (value >= 0)
850 value = min(w_length, (u16) value);
851 break;
852 }
853 break;
854
855 /* any number of configs can work */
856 case USB_REQ_SET_CONFIGURATION:
857 if (ctrl->bRequestType != 0)
858 goto unknown;
859 if (gadget_is_otg(gadget)) {
860 if (gadget->a_hnp_support)
861 DBG(cdev, "HNP available\n");
862 else if (gadget->a_alt_hnp_support)
863 DBG(cdev, "HNP on another port\n");
864 else
865 VDBG(cdev, "HNP inactive\n");
866 }
867 spin_lock(&cdev->lock);
868 value = set_config(cdev, ctrl, w_value);
869 spin_unlock(&cdev->lock);
870 break;
871 case USB_REQ_GET_CONFIGURATION:
872 if (ctrl->bRequestType != USB_DIR_IN)
873 goto unknown;
874 if (cdev->config)
875 *(u8 *)req->buf = cdev->config->bConfigurationValue;
876 else
877 *(u8 *)req->buf = 0;
878 value = min(w_length, (u16) 1);
879 break;
880
881 /* function drivers must handle get/set altsetting; if there's
882 * no get() method, we know only altsetting zero works.
883 */
884 case USB_REQ_SET_INTERFACE:
885 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
886 goto unknown;
887 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
888 break;
Bryan Wu08889512009-01-08 00:21:19 +0800889 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700890 if (!f)
891 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800892 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700893 break;
894 value = f->set_alt(f, w_index, w_value);
895 break;
896 case USB_REQ_GET_INTERFACE:
897 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
898 goto unknown;
899 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
900 break;
Bryan Wu08889512009-01-08 00:21:19 +0800901 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700902 if (!f)
903 break;
904 /* lots of interfaces only need altsetting zero... */
905 value = f->get_alt ? f->get_alt(f, w_index) : 0;
906 if (value < 0)
907 break;
908 *((u8 *)req->buf) = value;
909 value = min(w_length, (u16) 1);
910 break;
911 default:
912unknown:
913 VDBG(cdev,
914 "non-core control req%02x.%02x v%04x i%04x l%d\n",
915 ctrl->bRequestType, ctrl->bRequest,
916 w_value, w_index, w_length);
917
Laurent Pinchart52426582009-10-21 00:03:38 +0200918 /* functions always handle their interfaces and endpoints...
919 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -0700920 * configuration code.
921 *
922 * REVISIT it could make sense to let the composite device
923 * take such requests too, if that's ever needed: to work
924 * in config 0, etc.
925 */
Laurent Pinchart52426582009-10-21 00:03:38 +0200926 switch (ctrl->bRequestType & USB_RECIP_MASK) {
927 case USB_RECIP_INTERFACE:
Bryan Wu08889512009-01-08 00:21:19 +0800928 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +0200929 break;
930
931 case USB_RECIP_ENDPOINT:
932 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
933 list_for_each_entry(f, &cdev->config->functions, list) {
934 if (test_bit(endp, f->endpoints))
935 break;
936 }
937 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -0700938 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200939 break;
David Brownell40982be2008-06-19 17:52:58 -0700940 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200941
942 if (f && f->setup)
943 value = f->setup(f, ctrl);
944 else {
David Brownell40982be2008-06-19 17:52:58 -0700945 struct usb_configuration *c;
946
947 c = cdev->config;
948 if (c && c->setup)
949 value = c->setup(c, ctrl);
950 }
951
952 goto done;
953 }
954
955 /* respond with data transfer before status phase? */
956 if (value >= 0) {
957 req->length = value;
958 req->zero = value < w_length;
959 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
960 if (value < 0) {
961 DBG(cdev, "ep_queue --> %d\n", value);
962 req->status = 0;
963 composite_setup_complete(gadget->ep0, req);
964 }
965 }
966
967done:
968 /* device either stalls (value < 0) or reports success */
969 return value;
970}
971
972static void composite_disconnect(struct usb_gadget *gadget)
973{
974 struct usb_composite_dev *cdev = get_gadget_data(gadget);
975 unsigned long flags;
976
977 /* REVISIT: should we have config and device level
978 * disconnect callbacks?
979 */
980 spin_lock_irqsave(&cdev->lock, flags);
981 if (cdev->config)
982 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +0200983 if (composite->disconnect)
984 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700985 spin_unlock_irqrestore(&cdev->lock, flags);
986}
987
988/*-------------------------------------------------------------------------*/
989
Fabien Chouteauf48cf802010-04-23 14:21:26 +0200990static ssize_t composite_show_suspended(struct device *dev,
991 struct device_attribute *attr,
992 char *buf)
993{
994 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
995 struct usb_composite_dev *cdev = get_gadget_data(gadget);
996
997 return sprintf(buf, "%d\n", cdev->suspended);
998}
999
1000static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1001
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001002static void
David Brownell40982be2008-06-19 17:52:58 -07001003composite_unbind(struct usb_gadget *gadget)
1004{
1005 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1006
1007 /* composite_disconnect() must already have been called
1008 * by the underlying peripheral controller driver!
1009 * so there's no i/o concurrency that could affect the
1010 * state protected by cdev->lock.
1011 */
1012 WARN_ON(cdev->config);
1013
1014 while (!list_empty(&cdev->configs)) {
1015 struct usb_configuration *c;
1016
1017 c = list_first_entry(&cdev->configs,
1018 struct usb_configuration, list);
1019 while (!list_empty(&c->functions)) {
1020 struct usb_function *f;
1021
1022 f = list_first_entry(&c->functions,
1023 struct usb_function, list);
1024 list_del(&f->list);
1025 if (f->unbind) {
1026 DBG(cdev, "unbind function '%s'/%p\n",
1027 f->name, f);
1028 f->unbind(c, f);
1029 /* may free memory for "f" */
1030 }
1031 }
1032 list_del(&c->list);
1033 if (c->unbind) {
1034 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1035 c->unbind(c);
1036 /* may free memory for "c" */
1037 }
1038 }
1039 if (composite->unbind)
1040 composite->unbind(cdev);
1041
1042 if (cdev->req) {
1043 kfree(cdev->req->buf);
1044 usb_ep_free_request(gadget->ep0, cdev->req);
1045 }
1046 kfree(cdev);
1047 set_gadget_data(gadget, NULL);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001048 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001049 composite = NULL;
1050}
1051
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001052static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001053{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001054 if (!*desc) {
1055 int ret = usb_string_id(cdev);
1056 if (unlikely(ret < 0))
1057 WARNING(cdev, "failed to override string ID\n");
1058 else
1059 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001060 }
David Brownell40982be2008-06-19 17:52:58 -07001061
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001062 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001063}
1064
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001065static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001066{
1067 struct usb_composite_dev *cdev;
1068 int status = -ENOMEM;
1069
1070 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1071 if (!cdev)
1072 return status;
1073
1074 spin_lock_init(&cdev->lock);
1075 cdev->gadget = gadget;
1076 set_gadget_data(gadget, cdev);
1077 INIT_LIST_HEAD(&cdev->configs);
1078
1079 /* preallocate control response and buffer */
1080 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1081 if (!cdev->req)
1082 goto fail;
1083 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1084 if (!cdev->req->buf)
1085 goto fail;
1086 cdev->req->complete = composite_setup_complete;
1087 gadget->ep0->driver_data = cdev;
1088
1089 cdev->bufsiz = USB_BUFSIZ;
1090 cdev->driver = composite;
1091
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301092 /*
1093 * As per USB compliance update, a device that is actively drawing
1094 * more than 100mA from USB must report itself as bus-powered in
1095 * the GetStatus(DEVICE) call.
1096 */
1097 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1098 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001099
1100 /* interface and string IDs start at zero via kzalloc.
1101 * we force endpoints to start unassigned; few controller
1102 * drivers will zero ep->driver_data.
1103 */
1104 usb_ep_autoconfig_reset(cdev->gadget);
1105
Robert Lukassen1ab83232010-05-07 09:19:53 +02001106 /* standardized runtime overrides for device ID data */
1107 if (idVendor)
1108 cdev->desc.idVendor = cpu_to_le16(idVendor);
1109 if (idProduct)
1110 cdev->desc.idProduct = cpu_to_le16(idProduct);
1111 if (bcdDevice)
1112 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1113
David Brownell40982be2008-06-19 17:52:58 -07001114 /* composite gadget needs to assign strings for whole device (like
1115 * serial number), register function drivers, potentially update
1116 * power state and consumption, etc
1117 */
1118 status = composite->bind(cdev);
1119 if (status < 0)
1120 goto fail;
1121
1122 cdev->desc = *composite->dev;
1123 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1124
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001125 /* stirng overrides */
1126 if (iManufacturer || !cdev->desc.iManufacturer) {
1127 if (!iManufacturer && !composite->iManufacturer &&
1128 !*composite_manufacturer)
1129 snprintf(composite_manufacturer,
1130 sizeof composite_manufacturer,
1131 "%s %s with %s",
1132 init_utsname()->sysname,
1133 init_utsname()->release,
1134 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001135
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001136 cdev->manufacturer_override =
1137 override_id(cdev, &cdev->desc.iManufacturer);
1138 }
1139
1140 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1141 cdev->product_override =
1142 override_id(cdev, &cdev->desc.iProduct);
1143
1144 if (iSerialNumber)
1145 cdev->serial_override =
1146 override_id(cdev, &cdev->desc.iSerialNumber);
1147
1148 /* has userspace failed to provide a serial number? */
1149 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1150 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1151
1152 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001153 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1154 if (status)
1155 goto fail;
1156
David Brownell40982be2008-06-19 17:52:58 -07001157 INFO(cdev, "%s ready\n", composite->name);
1158 return 0;
1159
1160fail:
1161 composite_unbind(gadget);
1162 return status;
1163}
1164
1165/*-------------------------------------------------------------------------*/
1166
1167static void
1168composite_suspend(struct usb_gadget *gadget)
1169{
1170 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1171 struct usb_function *f;
1172
David Brownell89429392009-03-19 14:14:17 -07001173 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001174 * suspend/resume callbacks?
1175 */
1176 DBG(cdev, "suspend\n");
1177 if (cdev->config) {
1178 list_for_each_entry(f, &cdev->config->functions, list) {
1179 if (f->suspend)
1180 f->suspend(f);
1181 }
1182 }
David Brownell89429392009-03-19 14:14:17 -07001183 if (composite->suspend)
1184 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001185
1186 cdev->suspended = 1;
David Brownell40982be2008-06-19 17:52:58 -07001187}
1188
1189static void
1190composite_resume(struct usb_gadget *gadget)
1191{
1192 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1193 struct usb_function *f;
1194
David Brownell89429392009-03-19 14:14:17 -07001195 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001196 * suspend/resume callbacks?
1197 */
1198 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001199 if (composite->resume)
1200 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001201 if (cdev->config) {
1202 list_for_each_entry(f, &cdev->config->functions, list) {
1203 if (f->resume)
1204 f->resume(f);
1205 }
1206 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001207
1208 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001209}
1210
1211/*-------------------------------------------------------------------------*/
1212
1213static struct usb_gadget_driver composite_driver = {
1214 .speed = USB_SPEED_HIGH,
1215
1216 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001217 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001218
1219 .setup = composite_setup,
1220 .disconnect = composite_disconnect,
1221
1222 .suspend = composite_suspend,
1223 .resume = composite_resume,
1224
1225 .driver = {
1226 .owner = THIS_MODULE,
1227 },
1228};
1229
1230/**
1231 * usb_composite_register() - register a composite driver
1232 * @driver: the driver to register
1233 * Context: single threaded during gadget setup
1234 *
1235 * This function is used to register drivers using the composite driver
1236 * framework. The return value is zero, or a negative errno value.
1237 * Those values normally come from the driver's @bind method, which does
1238 * all the work of setting up the driver to match the hardware.
1239 *
1240 * On successful return, the gadget is ready to respond to requests from
1241 * the host, unless one of its components invokes usb_gadget_disconnect()
1242 * while it was binding. That would usually be done in order to wait for
1243 * some userspace participation.
1244 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001245int usb_composite_register(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001246{
1247 if (!driver || !driver->dev || !driver->bind || composite)
1248 return -EINVAL;
1249
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001250 if (!driver->iProduct)
1251 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001252 if (!driver->name)
1253 driver->name = "composite";
1254 composite_driver.function = (char *) driver->name;
1255 composite_driver.driver.name = driver->name;
1256 composite = driver;
1257
1258 return usb_gadget_register_driver(&composite_driver);
1259}
1260
1261/**
1262 * usb_composite_unregister() - unregister a composite driver
1263 * @driver: the driver to unregister
1264 *
1265 * This function is used to unregister drivers using the composite
1266 * driver framework.
1267 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001268void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001269{
1270 if (composite != driver)
1271 return;
1272 usb_gadget_unregister_driver(&composite_driver);
1273}