blob: 134d3d50e350027f763a47fecc545b7773071211 [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;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +020043static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
David Brownell40982be2008-06-19 17:52:58 -070044
Lucas De Marchi25985ed2011-03-30 22:57:33 -030045/* Some systems will need runtime overrides for the product identifiers
David Brownell40982be2008-06-19 17:52:58 -070046 * published in the device descriptor, either numbers or strings or both.
47 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
48 */
49
50static ushort idVendor;
51module_param(idVendor, ushort, 0);
52MODULE_PARM_DESC(idVendor, "USB Vendor ID");
53
54static ushort idProduct;
55module_param(idProduct, ushort, 0);
56MODULE_PARM_DESC(idProduct, "USB Product ID");
57
58static ushort bcdDevice;
59module_param(bcdDevice, ushort, 0);
60MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
61
62static char *iManufacturer;
63module_param(iManufacturer, charp, 0);
64MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
65
66static char *iProduct;
67module_param(iProduct, charp, 0);
68MODULE_PARM_DESC(iProduct, "USB Product string");
69
70static char *iSerialNumber;
71module_param(iSerialNumber, charp, 0);
72MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
73
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020074static char composite_manufacturer[50];
75
David Brownell40982be2008-06-19 17:52:58 -070076/*-------------------------------------------------------------------------*/
77
78/**
79 * usb_add_function() - add a function to a configuration
80 * @config: the configuration
81 * @function: the function being added
82 * Context: single threaded during gadget setup
83 *
84 * After initialization, each configuration must have one or more
85 * functions added to it. Adding a function involves calling its @bind()
86 * method to allocate resources such as interface and string identifiers
87 * and endpoints.
88 *
89 * This function returns the value of the function's bind(), which is
90 * zero for success else a negative errno value.
91 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020092int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -070093 struct usb_function *function)
94{
95 int value = -EINVAL;
96
97 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
98 function->name, function,
99 config->label, config);
100
101 if (!function->set_alt || !function->disable)
102 goto done;
103
104 function->config = config;
105 list_add_tail(&function->list, &config->functions);
106
107 /* REVISIT *require* function->bind? */
108 if (function->bind) {
109 value = function->bind(config, function);
110 if (value < 0) {
111 list_del(&function->list);
112 function->config = NULL;
113 }
114 } else
115 value = 0;
116
117 /* We allow configurations that don't work at both speeds.
118 * If we run into a lowspeed Linux system, treat it the same
119 * as full speed ... it's the function drivers that will need
120 * to avoid bulk and ISO transfers.
121 */
122 if (!config->fullspeed && function->descriptors)
123 config->fullspeed = true;
124 if (!config->highspeed && function->hs_descriptors)
125 config->highspeed = true;
126
127done:
128 if (value)
129 DBG(config->cdev, "adding '%s'/%p --> %d\n",
130 function->name, function, value);
131 return value;
132}
133
134/**
David Brownell60beed92008-08-18 17:38:22 -0700135 * usb_function_deactivate - prevent function and gadget enumeration
136 * @function: the function that isn't yet ready to respond
137 *
138 * Blocks response of the gadget driver to host enumeration by
139 * preventing the data line pullup from being activated. This is
140 * normally called during @bind() processing to change from the
141 * initial "ready to respond" state, or when a required resource
142 * becomes available.
143 *
144 * For example, drivers that serve as a passthrough to a userspace
145 * daemon can block enumeration unless that daemon (such as an OBEX,
146 * MTP, or print server) is ready to handle host requests.
147 *
148 * Not all systems support software control of their USB peripheral
149 * data pullups.
150 *
151 * Returns zero on success, else negative errno.
152 */
153int usb_function_deactivate(struct usb_function *function)
154{
155 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200156 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700157 int status = 0;
158
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200159 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700160
161 if (cdev->deactivations == 0)
162 status = usb_gadget_disconnect(cdev->gadget);
163 if (status == 0)
164 cdev->deactivations++;
165
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200166 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700167 return status;
168}
169
170/**
171 * usb_function_activate - allow function and gadget enumeration
172 * @function: function on which usb_function_activate() was called
173 *
174 * Reverses effect of usb_function_deactivate(). If no more functions
175 * are delaying their activation, the gadget driver will respond to
176 * host enumeration procedures.
177 *
178 * Returns zero on success, else negative errno.
179 */
180int usb_function_activate(struct usb_function *function)
181{
182 struct usb_composite_dev *cdev = function->config->cdev;
183 int status = 0;
184
185 spin_lock(&cdev->lock);
186
187 if (WARN_ON(cdev->deactivations == 0))
188 status = -EINVAL;
189 else {
190 cdev->deactivations--;
191 if (cdev->deactivations == 0)
192 status = usb_gadget_connect(cdev->gadget);
193 }
194
195 spin_unlock(&cdev->lock);
196 return status;
197}
198
199/**
David Brownell40982be2008-06-19 17:52:58 -0700200 * usb_interface_id() - allocate an unused interface ID
201 * @config: configuration associated with the interface
202 * @function: function handling the interface
203 * Context: single threaded during gadget setup
204 *
205 * usb_interface_id() is called from usb_function.bind() callbacks to
206 * allocate new interface IDs. The function driver will then store that
207 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300208 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700209 * particularly changing its altsetting via set_alt(). There may
210 * also be class-specific or vendor-specific requests to handle.
211 *
212 * All interface identifier should be allocated using this routine, to
213 * ensure that for example different functions don't wrongly assign
214 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300215 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700216 * one configuration (or more than once in a given configuration) need
217 * multiple versions of the relevant descriptors.
218 *
219 * Returns the interface ID which was allocated; or -ENODEV if no
220 * more interface IDs can be allocated.
221 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200222int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700223 struct usb_function *function)
224{
225 unsigned id = config->next_interface_id;
226
227 if (id < MAX_CONFIG_INTERFACES) {
228 config->interface[id] = function;
229 config->next_interface_id = id + 1;
230 return id;
231 }
232 return -ENODEV;
233}
234
235static int config_buf(struct usb_configuration *config,
236 enum usb_device_speed speed, void *buf, u8 type)
237{
238 struct usb_config_descriptor *c = buf;
239 void *next = buf + USB_DT_CONFIG_SIZE;
240 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
241 struct usb_function *f;
242 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500243 int interfaceCount = 0;
David Brownell40982be2008-06-19 17:52:58 -0700244
245 /* write the config descriptor */
246 c = buf;
247 c->bLength = USB_DT_CONFIG_SIZE;
248 c->bDescriptorType = type;
249 /* wTotalLength is written later */
250 c->bNumInterfaces = config->next_interface_id;
251 c->bConfigurationValue = config->bConfigurationValue;
252 c->iConfiguration = config->iConfiguration;
253 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700254 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700255
256 /* There may be e.g. OTG descriptors */
257 if (config->descriptors) {
258 status = usb_descriptor_fillbuf(next, len,
259 config->descriptors);
260 if (status < 0)
261 return status;
262 len -= status;
263 next += status;
264 }
265
266 /* add each function's descriptors */
267 list_for_each_entry(f, &config->functions, list) {
268 struct usb_descriptor_header **descriptors;
269
270 if (speed == USB_SPEED_HIGH)
271 descriptors = f->hs_descriptors;
272 else
273 descriptors = f->descriptors;
Mike Lockwoodc7ba16a2009-12-11 11:24:07 -0500274 if (f->hidden || !descriptors || descriptors[0] == NULL) {
Jared Suttles646bb862009-07-30 16:13:27 -0500275 for (; f != config->interface[interfaceCount];) {
276 interfaceCount++;
277 c->bNumInterfaces--;
278 }
David Brownell40982be2008-06-19 17:52:58 -0700279 continue;
Jared Suttles646bb862009-07-30 16:13:27 -0500280 }
281 for (; f != config->interface[interfaceCount];)
282 interfaceCount++;
283
David Brownell40982be2008-06-19 17:52:58 -0700284 status = usb_descriptor_fillbuf(next, len,
285 (const struct usb_descriptor_header **) descriptors);
286 if (status < 0)
287 return status;
288 len -= status;
289 next += status;
290 }
291
292 len = next - buf;
293 c->wTotalLength = cpu_to_le16(len);
294 return len;
295}
296
297static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
298{
299 struct usb_gadget *gadget = cdev->gadget;
300 struct usb_configuration *c;
301 u8 type = w_value >> 8;
302 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
303
304 if (gadget_is_dualspeed(gadget)) {
305 int hs = 0;
306
307 if (gadget->speed == USB_SPEED_HIGH)
308 hs = 1;
309 if (type == USB_DT_OTHER_SPEED_CONFIG)
310 hs = !hs;
311 if (hs)
312 speed = USB_SPEED_HIGH;
313
314 }
315
316 /* This is a lookup by config *INDEX* */
317 w_value &= 0xff;
318 list_for_each_entry(c, &cdev->configs, list) {
319 /* ignore configs that won't work at this speed */
320 if (speed == USB_SPEED_HIGH) {
321 if (!c->highspeed)
322 continue;
323 } else {
324 if (!c->fullspeed)
325 continue;
326 }
327 if (w_value == 0)
328 return config_buf(c, speed, cdev->req->buf, type);
329 w_value--;
330 }
331 return -EINVAL;
332}
333
334static int count_configs(struct usb_composite_dev *cdev, unsigned type)
335{
336 struct usb_gadget *gadget = cdev->gadget;
337 struct usb_configuration *c;
338 unsigned count = 0;
339 int hs = 0;
340
341 if (gadget_is_dualspeed(gadget)) {
342 if (gadget->speed == USB_SPEED_HIGH)
343 hs = 1;
344 if (type == USB_DT_DEVICE_QUALIFIER)
345 hs = !hs;
346 }
347 list_for_each_entry(c, &cdev->configs, list) {
348 /* ignore configs that won't work at this speed */
349 if (hs) {
350 if (!c->highspeed)
351 continue;
352 } else {
353 if (!c->fullspeed)
354 continue;
355 }
356 count++;
357 }
358 return count;
359}
360
361static void device_qual(struct usb_composite_dev *cdev)
362{
363 struct usb_qualifier_descriptor *qual = cdev->req->buf;
364
365 qual->bLength = sizeof(*qual);
366 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
367 /* POLICY: same bcdUSB and device type info at both speeds */
368 qual->bcdUSB = cdev->desc.bcdUSB;
369 qual->bDeviceClass = cdev->desc.bDeviceClass;
370 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
371 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
372 /* ASSUME same EP0 fifo size at both speeds */
373 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
374 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700375 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700376}
377
378/*-------------------------------------------------------------------------*/
379
380static void reset_config(struct usb_composite_dev *cdev)
381{
382 struct usb_function *f;
383
384 DBG(cdev, "reset config\n");
385
386 list_for_each_entry(f, &cdev->config->functions, list) {
387 if (f->disable)
388 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200389
390 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700391 }
392 cdev->config = NULL;
393}
394
395static int set_config(struct usb_composite_dev *cdev,
396 const struct usb_ctrlrequest *ctrl, unsigned number)
397{
398 struct usb_gadget *gadget = cdev->gadget;
399 struct usb_configuration *c = NULL;
400 int result = -EINVAL;
401 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
402 int tmp;
403
404 if (cdev->config)
405 reset_config(cdev);
406
407 if (number) {
408 list_for_each_entry(c, &cdev->configs, list) {
409 if (c->bConfigurationValue == number) {
410 result = 0;
411 break;
412 }
413 }
414 if (result < 0)
415 goto done;
416 } else
417 result = 0;
418
419 INFO(cdev, "%s speed config #%d: %s\n",
420 ({ char *speed;
421 switch (gadget->speed) {
422 case USB_SPEED_LOW: speed = "low"; break;
423 case USB_SPEED_FULL: speed = "full"; break;
424 case USB_SPEED_HIGH: speed = "high"; break;
425 default: speed = "?"; break;
426 } ; speed; }), number, c ? c->label : "unconfigured");
427
428 if (!c)
429 goto done;
430
431 cdev->config = c;
432
433 /* Initialize all interfaces by setting them to altsetting zero. */
434 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
435 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200436 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700437
438 if (!f)
439 break;
440
Laurent Pinchart52426582009-10-21 00:03:38 +0200441 /*
442 * Record which endpoints are used by the function. This is used
443 * to dispatch control requests targeted at that endpoint to the
444 * function's setup callback instead of the current
445 * configuration's setup callback.
446 */
447 if (gadget->speed == USB_SPEED_HIGH)
448 descriptors = f->hs_descriptors;
449 else
450 descriptors = f->descriptors;
451
452 for (; *descriptors; ++descriptors) {
453 struct usb_endpoint_descriptor *ep;
454 int addr;
455
456 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
457 continue;
458
459 ep = (struct usb_endpoint_descriptor *)*descriptors;
460 addr = ((ep->bEndpointAddress & 0x80) >> 3)
461 | (ep->bEndpointAddress & 0x0f);
462 set_bit(addr, f->endpoints);
463 }
464
David Brownell40982be2008-06-19 17:52:58 -0700465 result = f->set_alt(f, tmp, 0);
466 if (result < 0) {
467 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
468 tmp, f->name, f, result);
469
470 reset_config(cdev);
471 goto done;
472 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300473
474 if (result == USB_GADGET_DELAYED_STATUS) {
475 DBG(cdev,
476 "%s: interface %d (%s) requested delayed status\n",
477 __func__, tmp, f->name);
478 cdev->delayed_status++;
479 DBG(cdev, "delayed_status count %d\n",
480 cdev->delayed_status);
481 }
David Brownell40982be2008-06-19 17:52:58 -0700482 }
483
484 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700485 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700486done:
487 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300488 if (result >= 0 && cdev->delayed_status)
489 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700490 return result;
491}
492
493/**
494 * usb_add_config() - add a configuration to a device.
495 * @cdev: wraps the USB gadget
496 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200497 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700498 * Context: single threaded during gadget setup
499 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200500 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700501 * add each of the configurations it supports, using this routine.
502 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200503 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700504 * is zero for success else a negative errno value. Binding configurations
505 * assigns global resources including string IDs, and per-configuration
506 * resources such as interface IDs and endpoints.
507 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200508int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200509 struct usb_configuration *config,
510 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700511{
512 int status = -EINVAL;
513 struct usb_configuration *c;
514
515 DBG(cdev, "adding config #%u '%s'/%p\n",
516 config->bConfigurationValue,
517 config->label, config);
518
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200519 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700520 goto done;
521
522 /* Prevent duplicate configuration identifiers */
523 list_for_each_entry(c, &cdev->configs, list) {
524 if (c->bConfigurationValue == config->bConfigurationValue) {
525 status = -EBUSY;
526 goto done;
527 }
528 }
529
530 config->cdev = cdev;
531 list_add_tail(&config->list, &cdev->configs);
532
533 INIT_LIST_HEAD(&config->functions);
534 config->next_interface_id = 0;
535
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200536 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700537 if (status < 0) {
538 list_del(&config->list);
539 config->cdev = NULL;
540 } else {
541 unsigned i;
542
543 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
544 config->bConfigurationValue, config,
545 config->highspeed ? " high" : "",
546 config->fullspeed
547 ? (gadget_is_dualspeed(cdev->gadget)
548 ? " full"
549 : " full/low")
550 : "");
551
552 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
553 struct usb_function *f = config->interface[i];
554
555 if (!f)
556 continue;
557 DBG(cdev, " interface %d = %s/%p\n",
558 i, f->name, f);
559 }
560 }
561
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200562 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700563 * ep->driver_data as needed.
564 */
565 usb_ep_autoconfig_reset(cdev->gadget);
566
567done:
568 if (status)
569 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
570 config->bConfigurationValue, status);
571 return status;
572}
573
574/*-------------------------------------------------------------------------*/
575
576/* We support strings in multiple languages ... string descriptor zero
577 * says which languages are supported. The typical case will be that
578 * only one language (probably English) is used, with I18N handled on
579 * the host side.
580 */
581
582static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
583{
584 const struct usb_gadget_strings *s;
585 u16 language;
586 __le16 *tmp;
587
588 while (*sp) {
589 s = *sp;
590 language = cpu_to_le16(s->language);
591 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
592 if (*tmp == language)
593 goto repeat;
594 }
595 *tmp++ = language;
596repeat:
597 sp++;
598 }
599}
600
601static int lookup_string(
602 struct usb_gadget_strings **sp,
603 void *buf,
604 u16 language,
605 int id
606)
607{
608 struct usb_gadget_strings *s;
609 int value;
610
611 while (*sp) {
612 s = *sp++;
613 if (s->language != language)
614 continue;
615 value = usb_gadget_get_string(s, id, buf);
616 if (value > 0)
617 return value;
618 }
619 return -EINVAL;
620}
621
622static int get_string(struct usb_composite_dev *cdev,
623 void *buf, u16 language, int id)
624{
625 struct usb_configuration *c;
626 struct usb_function *f;
627 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200628 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700629
630 /* Yes, not only is USB's I18N support probably more than most
631 * folk will ever care about ... also, it's all supported here.
632 * (Except for UTF8 support for Unicode's "Astral Planes".)
633 */
634
635 /* 0 == report all available language codes */
636 if (id == 0) {
637 struct usb_string_descriptor *s = buf;
638 struct usb_gadget_strings **sp;
639
640 memset(s, 0, 256);
641 s->bDescriptorType = USB_DT_STRING;
642
643 sp = composite->strings;
644 if (sp)
645 collect_langs(sp, s->wData);
646
647 list_for_each_entry(c, &cdev->configs, list) {
648 sp = c->strings;
649 if (sp)
650 collect_langs(sp, s->wData);
651
652 list_for_each_entry(f, &c->functions, list) {
653 sp = f->strings;
654 if (sp)
655 collect_langs(sp, s->wData);
656 }
657 }
658
Roel Kluin417b57b2009-08-06 16:09:51 -0700659 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700660 continue;
661 if (!len)
662 return -EINVAL;
663
664 s->bLength = 2 * (len + 1);
665 return s->bLength;
666 }
667
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200668 /* Otherwise, look up and return a specified string. First
669 * check if the string has not been overridden.
670 */
671 if (cdev->manufacturer_override == id)
672 str = iManufacturer ?: composite->iManufacturer ?:
673 composite_manufacturer;
674 else if (cdev->product_override == id)
675 str = iProduct ?: composite->iProduct;
676 else if (cdev->serial_override == id)
677 str = iSerialNumber;
678 else
679 str = NULL;
680 if (str) {
681 struct usb_gadget_strings strings = {
682 .language = language,
683 .strings = &(struct usb_string) { 0xff, str }
684 };
685 return usb_gadget_get_string(&strings, 0xff, buf);
686 }
687
688 /* String IDs are device-scoped, so we look up each string
689 * table we're told about. These lookups are infrequent;
690 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700691 */
692 if (composite->strings) {
693 len = lookup_string(composite->strings, buf, language, id);
694 if (len > 0)
695 return len;
696 }
697 list_for_each_entry(c, &cdev->configs, list) {
698 if (c->strings) {
699 len = lookup_string(c->strings, buf, language, id);
700 if (len > 0)
701 return len;
702 }
703 list_for_each_entry(f, &c->functions, list) {
704 if (!f->strings)
705 continue;
706 len = lookup_string(f->strings, buf, language, id);
707 if (len > 0)
708 return len;
709 }
710 }
711 return -EINVAL;
712}
713
714/**
715 * usb_string_id() - allocate an unused string ID
716 * @cdev: the device whose string descriptor IDs are being allocated
717 * Context: single threaded during gadget setup
718 *
719 * @usb_string_id() is called from bind() callbacks to allocate
720 * string IDs. Drivers for functions, configurations, or gadgets will
721 * then store that ID in the appropriate descriptors and string table.
722 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200723 * All string identifier should be allocated using this,
724 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
725 * that for example different functions don't wrongly assign different
726 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700727 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200728int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700729{
730 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200731 /* string id 0 is reserved by USB spec for list of
732 * supported languages */
733 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700734 cdev->next_string_id++;
735 return cdev->next_string_id;
736 }
737 return -ENODEV;
738}
739
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200740/**
741 * usb_string_ids() - allocate unused string IDs in batch
742 * @cdev: the device whose string descriptor IDs are being allocated
743 * @str: an array of usb_string objects to assign numbers to
744 * Context: single threaded during gadget setup
745 *
746 * @usb_string_ids() is called from bind() callbacks to allocate
747 * string IDs. Drivers for functions, configurations, or gadgets will
748 * then copy IDs from the string table to the appropriate descriptors
749 * and string table for other languages.
750 *
751 * All string identifier should be allocated using this,
752 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
753 * example different functions don't wrongly assign different meanings
754 * to the same identifier.
755 */
756int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
757{
758 int next = cdev->next_string_id;
759
760 for (; str->s; ++str) {
761 if (unlikely(next >= 254))
762 return -ENODEV;
763 str->id = ++next;
764 }
765
766 cdev->next_string_id = next;
767
768 return 0;
769}
770
771/**
772 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700773 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200774 * @n: number of string IDs to allocate
775 * Context: single threaded during gadget setup
776 *
777 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700778 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200779 * is, returns last requested ID which is now very useful information.
780 *
781 * @usb_string_ids_n() is called from bind() callbacks to allocate
782 * string IDs. Drivers for functions, configurations, or gadgets will
783 * then store that ID in the appropriate descriptors and string table.
784 *
785 * All string identifier should be allocated using this,
786 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
787 * example different functions don't wrongly assign different meanings
788 * to the same identifier.
789 */
790int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
791{
792 unsigned next = c->next_string_id;
793 if (unlikely(n > 254 || (unsigned)next + n > 254))
794 return -ENODEV;
795 c->next_string_id += n;
796 return next + 1;
797}
798
799
David Brownell40982be2008-06-19 17:52:58 -0700800/*-------------------------------------------------------------------------*/
801
802static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
803{
804 if (req->status || req->actual != req->length)
805 DBG((struct usb_composite_dev *) ep->driver_data,
806 "setup complete --> %d, %d/%d\n",
807 req->status, req->actual, req->length);
808}
809
810/*
811 * The setup() callback implements all the ep0 functionality that's
812 * not handled lower down, in hardware or the hardware driver(like
813 * device and endpoint feature flags, and their status). It's all
814 * housekeeping for the gadget function we're implementing. Most of
815 * the work is in config and function specific setup.
816 */
817static int
818composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
819{
820 struct usb_composite_dev *cdev = get_gadget_data(gadget);
821 struct usb_request *req = cdev->req;
822 int value = -EOPNOTSUPP;
823 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800824 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700825 u16 w_value = le16_to_cpu(ctrl->wValue);
826 u16 w_length = le16_to_cpu(ctrl->wLength);
827 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200828 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700829
830 /* partial re-init of the response message; the function or the
831 * gadget might need to intercept e.g. a control-OUT completion
832 * when we delegate to it.
833 */
834 req->zero = 0;
835 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530836 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700837 gadget->ep0->driver_data = cdev;
838
839 switch (ctrl->bRequest) {
840
841 /* we handle all standard USB descriptors */
842 case USB_REQ_GET_DESCRIPTOR:
843 if (ctrl->bRequestType != USB_DIR_IN)
844 goto unknown;
845 switch (w_value >> 8) {
846
847 case USB_DT_DEVICE:
848 cdev->desc.bNumConfigurations =
849 count_configs(cdev, USB_DT_DEVICE);
850 value = min(w_length, (u16) sizeof cdev->desc);
851 memcpy(req->buf, &cdev->desc, value);
852 break;
853 case USB_DT_DEVICE_QUALIFIER:
854 if (!gadget_is_dualspeed(gadget))
855 break;
856 device_qual(cdev);
857 value = min_t(int, w_length,
858 sizeof(struct usb_qualifier_descriptor));
859 break;
860 case USB_DT_OTHER_SPEED_CONFIG:
861 if (!gadget_is_dualspeed(gadget))
862 break;
863 /* FALLTHROUGH */
864 case USB_DT_CONFIG:
865 value = config_desc(cdev, w_value);
866 if (value >= 0)
867 value = min(w_length, (u16) value);
868 break;
869 case USB_DT_STRING:
870 value = get_string(cdev, req->buf,
871 w_index, w_value & 0xff);
872 if (value >= 0)
873 value = min(w_length, (u16) value);
874 break;
875 }
876 break;
877
878 /* any number of configs can work */
879 case USB_REQ_SET_CONFIGURATION:
880 if (ctrl->bRequestType != 0)
881 goto unknown;
882 if (gadget_is_otg(gadget)) {
883 if (gadget->a_hnp_support)
884 DBG(cdev, "HNP available\n");
885 else if (gadget->a_alt_hnp_support)
886 DBG(cdev, "HNP on another port\n");
887 else
888 VDBG(cdev, "HNP inactive\n");
889 }
890 spin_lock(&cdev->lock);
891 value = set_config(cdev, ctrl, w_value);
892 spin_unlock(&cdev->lock);
893 break;
894 case USB_REQ_GET_CONFIGURATION:
895 if (ctrl->bRequestType != USB_DIR_IN)
896 goto unknown;
Jared Suttles258d1032009-08-07 18:57:49 -0500897 if (cdev->config) {
David Brownell40982be2008-06-19 17:52:58 -0700898 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Jared Suttles258d1032009-08-07 18:57:49 -0500899 value = min(w_length, (u16) 1);
900 } else {
David Brownell40982be2008-06-19 17:52:58 -0700901 *(u8 *)req->buf = 0;
Jared Suttles258d1032009-08-07 18:57:49 -0500902 }
David Brownell40982be2008-06-19 17:52:58 -0700903 break;
904
905 /* function drivers must handle get/set altsetting; if there's
906 * no get() method, we know only altsetting zero works.
907 */
908 case USB_REQ_SET_INTERFACE:
909 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
910 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900911 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700912 break;
Bryan Wu08889512009-01-08 00:21:19 +0800913 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700914 if (!f)
915 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800916 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700917 break;
918 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300919 if (value == USB_GADGET_DELAYED_STATUS) {
920 DBG(cdev,
921 "%s: interface %d (%s) requested delayed status\n",
922 __func__, intf, f->name);
923 cdev->delayed_status++;
924 DBG(cdev, "delayed_status count %d\n",
925 cdev->delayed_status);
926 }
David Brownell40982be2008-06-19 17:52:58 -0700927 break;
928 case USB_REQ_GET_INTERFACE:
929 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
930 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900931 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700932 break;
Bryan Wu08889512009-01-08 00:21:19 +0800933 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700934 if (!f)
935 break;
936 /* lots of interfaces only need altsetting zero... */
937 value = f->get_alt ? f->get_alt(f, w_index) : 0;
938 if (value < 0)
939 break;
940 *((u8 *)req->buf) = value;
941 value = min(w_length, (u16) 1);
942 break;
943 default:
944unknown:
945 VDBG(cdev,
946 "non-core control req%02x.%02x v%04x i%04x l%d\n",
947 ctrl->bRequestType, ctrl->bRequest,
948 w_value, w_index, w_length);
949
Laurent Pinchart52426582009-10-21 00:03:38 +0200950 /* functions always handle their interfaces and endpoints...
951 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -0700952 * configuration code.
953 *
954 * REVISIT it could make sense to let the composite device
955 * take such requests too, if that's ever needed: to work
956 * in config 0, etc.
957 */
Laurent Pinchart52426582009-10-21 00:03:38 +0200958 switch (ctrl->bRequestType & USB_RECIP_MASK) {
959 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +0900960 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +0530961 break;
962 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +0200963 break;
964
965 case USB_RECIP_ENDPOINT:
966 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
967 list_for_each_entry(f, &cdev->config->functions, list) {
968 if (test_bit(endp, f->endpoints))
969 break;
970 }
971 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -0700972 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200973 break;
David Brownell40982be2008-06-19 17:52:58 -0700974 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200975
976 if (f && f->setup)
977 value = f->setup(f, ctrl);
978 else {
David Brownell40982be2008-06-19 17:52:58 -0700979 struct usb_configuration *c;
980
981 c = cdev->config;
982 if (c && c->setup)
983 value = c->setup(c, ctrl);
984 }
985
986 goto done;
987 }
988
989 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +0300990 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -0700991 req->length = value;
992 req->zero = value < w_length;
993 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
994 if (value < 0) {
995 DBG(cdev, "ep_queue --> %d\n", value);
996 req->status = 0;
997 composite_setup_complete(gadget->ep0, req);
998 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300999 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1000 WARN(cdev,
1001 "%s: Delayed status not supported for w_length != 0",
1002 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001003 }
1004
1005done:
1006 /* device either stalls (value < 0) or reports success */
1007 return value;
1008}
1009
1010static void composite_disconnect(struct usb_gadget *gadget)
1011{
1012 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1013 unsigned long flags;
1014
1015 /* REVISIT: should we have config and device level
1016 * disconnect callbacks?
1017 */
1018 spin_lock_irqsave(&cdev->lock, flags);
1019 if (cdev->config)
1020 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001021 if (composite->disconnect)
1022 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001023 spin_unlock_irqrestore(&cdev->lock, flags);
1024}
1025
1026/*-------------------------------------------------------------------------*/
1027
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001028static ssize_t composite_show_suspended(struct device *dev,
1029 struct device_attribute *attr,
1030 char *buf)
1031{
1032 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1033 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1034
1035 return sprintf(buf, "%d\n", cdev->suspended);
1036}
1037
1038static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1039
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001040static void
David Brownell40982be2008-06-19 17:52:58 -07001041composite_unbind(struct usb_gadget *gadget)
1042{
1043 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1044
1045 /* composite_disconnect() must already have been called
1046 * by the underlying peripheral controller driver!
1047 * so there's no i/o concurrency that could affect the
1048 * state protected by cdev->lock.
1049 */
1050 WARN_ON(cdev->config);
1051
1052 while (!list_empty(&cdev->configs)) {
1053 struct usb_configuration *c;
1054
1055 c = list_first_entry(&cdev->configs,
1056 struct usb_configuration, list);
1057 while (!list_empty(&c->functions)) {
1058 struct usb_function *f;
1059
1060 f = list_first_entry(&c->functions,
1061 struct usb_function, list);
1062 list_del(&f->list);
1063 if (f->unbind) {
1064 DBG(cdev, "unbind function '%s'/%p\n",
1065 f->name, f);
1066 f->unbind(c, f);
1067 /* may free memory for "f" */
1068 }
1069 }
1070 list_del(&c->list);
1071 if (c->unbind) {
1072 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1073 c->unbind(c);
1074 /* may free memory for "c" */
1075 }
1076 }
1077 if (composite->unbind)
1078 composite->unbind(cdev);
1079
1080 if (cdev->req) {
1081 kfree(cdev->req->buf);
1082 usb_ep_free_request(gadget->ep0, cdev->req);
1083 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301084 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001085 kfree(cdev);
1086 set_gadget_data(gadget, NULL);
1087 composite = NULL;
1088}
1089
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001090static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001091{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001092 if (!*desc) {
1093 int ret = usb_string_id(cdev);
1094 if (unlikely(ret < 0))
1095 WARNING(cdev, "failed to override string ID\n");
1096 else
1097 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001098 }
David Brownell40982be2008-06-19 17:52:58 -07001099
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001100 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001101}
1102
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001103static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001104{
1105 struct usb_composite_dev *cdev;
1106 int status = -ENOMEM;
1107
1108 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1109 if (!cdev)
1110 return status;
1111
1112 spin_lock_init(&cdev->lock);
1113 cdev->gadget = gadget;
1114 set_gadget_data(gadget, cdev);
1115 INIT_LIST_HEAD(&cdev->configs);
1116
1117 /* preallocate control response and buffer */
1118 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1119 if (!cdev->req)
1120 goto fail;
1121 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1122 if (!cdev->req->buf)
1123 goto fail;
1124 cdev->req->complete = composite_setup_complete;
1125 gadget->ep0->driver_data = cdev;
1126
1127 cdev->bufsiz = USB_BUFSIZ;
1128 cdev->driver = composite;
1129
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301130 /*
1131 * As per USB compliance update, a device that is actively drawing
1132 * more than 100mA from USB must report itself as bus-powered in
1133 * the GetStatus(DEVICE) call.
1134 */
1135 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1136 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001137
1138 /* interface and string IDs start at zero via kzalloc.
1139 * we force endpoints to start unassigned; few controller
1140 * drivers will zero ep->driver_data.
1141 */
1142 usb_ep_autoconfig_reset(cdev->gadget);
1143
1144 /* composite gadget needs to assign strings for whole device (like
1145 * serial number), register function drivers, potentially update
1146 * power state and consumption, etc
1147 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001148 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001149 if (status < 0)
1150 goto fail;
1151
1152 cdev->desc = *composite->dev;
1153 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1154
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001155 /* standardized runtime overrides for device ID data */
1156 if (idVendor)
1157 cdev->desc.idVendor = cpu_to_le16(idVendor);
1158 if (idProduct)
1159 cdev->desc.idProduct = cpu_to_le16(idProduct);
1160 if (bcdDevice)
1161 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1162
Marek Belisko78bff3c2010-10-27 10:19:01 +02001163 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001164 if (iManufacturer || !cdev->desc.iManufacturer) {
1165 if (!iManufacturer && !composite->iManufacturer &&
1166 !*composite_manufacturer)
1167 snprintf(composite_manufacturer,
1168 sizeof composite_manufacturer,
1169 "%s %s with %s",
1170 init_utsname()->sysname,
1171 init_utsname()->release,
1172 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001173
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001174 cdev->manufacturer_override =
1175 override_id(cdev, &cdev->desc.iManufacturer);
1176 }
1177
1178 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1179 cdev->product_override =
1180 override_id(cdev, &cdev->desc.iProduct);
1181
1182 if (iSerialNumber)
1183 cdev->serial_override =
1184 override_id(cdev, &cdev->desc.iSerialNumber);
1185
1186 /* has userspace failed to provide a serial number? */
1187 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1188 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1189
1190 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001191 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1192 if (status)
1193 goto fail;
1194
David Brownell40982be2008-06-19 17:52:58 -07001195 INFO(cdev, "%s ready\n", composite->name);
1196 return 0;
1197
1198fail:
1199 composite_unbind(gadget);
1200 return status;
1201}
1202
1203/*-------------------------------------------------------------------------*/
1204
1205static void
1206composite_suspend(struct usb_gadget *gadget)
1207{
1208 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1209 struct usb_function *f;
1210
David Brownell89429392009-03-19 14:14:17 -07001211 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001212 * suspend/resume callbacks?
1213 */
1214 DBG(cdev, "suspend\n");
1215 if (cdev->config) {
1216 list_for_each_entry(f, &cdev->config->functions, list) {
1217 if (f->suspend)
1218 f->suspend(f);
1219 }
1220 }
David Brownell89429392009-03-19 14:14:17 -07001221 if (composite->suspend)
1222 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001223
1224 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001225
1226 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001227}
1228
1229static void
1230composite_resume(struct usb_gadget *gadget)
1231{
1232 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1233 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001234 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001235
David Brownell89429392009-03-19 14:14:17 -07001236 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001237 * suspend/resume callbacks?
1238 */
1239 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001240 if (composite->resume)
1241 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001242 if (cdev->config) {
1243 list_for_each_entry(f, &cdev->config->functions, list) {
1244 if (f->resume)
1245 f->resume(f);
1246 }
Hao Wub23f2f92010-11-29 15:17:03 +08001247
1248 maxpower = cdev->config->bMaxPower;
1249
1250 usb_gadget_vbus_draw(gadget, maxpower ?
1251 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001252 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001253
1254 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001255}
1256
1257/*-------------------------------------------------------------------------*/
1258
1259static struct usb_gadget_driver composite_driver = {
1260 .speed = USB_SPEED_HIGH,
1261
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001262 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001263
1264 .setup = composite_setup,
1265 .disconnect = composite_disconnect,
1266
1267 .suspend = composite_suspend,
1268 .resume = composite_resume,
1269
1270 .driver = {
1271 .owner = THIS_MODULE,
1272 },
1273};
1274
1275/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001276 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001277 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001278 * @bind: the callback used to allocate resources that are shared across the
1279 * whole device, such as string IDs, and add its configurations using
1280 * @usb_add_config(). This may fail by returning a negative errno
1281 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001282 * Context: single threaded during gadget setup
1283 *
1284 * This function is used to register drivers using the composite driver
1285 * framework. The return value is zero, or a negative errno value.
1286 * Those values normally come from the driver's @bind method, which does
1287 * all the work of setting up the driver to match the hardware.
1288 *
1289 * On successful return, the gadget is ready to respond to requests from
1290 * the host, unless one of its components invokes usb_gadget_disconnect()
1291 * while it was binding. That would usually be done in order to wait for
1292 * some userspace participation.
1293 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001294int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001295 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001296{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001297 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001298 return -EINVAL;
1299
1300 if (!driver->name)
1301 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001302 if (!driver->iProduct)
1303 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001304 composite_driver.function = (char *) driver->name;
1305 composite_driver.driver.name = driver->name;
1306 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001307 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001308
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001309 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001310}
1311
1312/**
1313 * usb_composite_unregister() - unregister a composite driver
1314 * @driver: the driver to unregister
1315 *
1316 * This function is used to unregister drivers using the composite
1317 * driver framework.
1318 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001319void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001320{
1321 if (composite != driver)
1322 return;
1323 usb_gadget_unregister_driver(&composite_driver);
1324}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001325
1326/**
1327 * usb_composite_setup_continue() - Continue with the control transfer
1328 * @cdev: the composite device who's control transfer was kept waiting
1329 *
1330 * This function must be called by the USB function driver to continue
1331 * with the control transfer's data/status stage in case it had requested to
1332 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1333 * can request the composite framework to delay the setup request's data/status
1334 * stages by returning USB_GADGET_DELAYED_STATUS.
1335 */
1336void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1337{
1338 int value;
1339 struct usb_request *req = cdev->req;
1340 unsigned long flags;
1341
1342 DBG(cdev, "%s\n", __func__);
1343 spin_lock_irqsave(&cdev->lock, flags);
1344
1345 if (cdev->delayed_status == 0) {
1346 WARN(cdev, "%s: Unexpected call\n", __func__);
1347
1348 } else if (--cdev->delayed_status == 0) {
1349 DBG(cdev, "%s: Completing delayed status\n", __func__);
1350 req->length = 0;
1351 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1352 if (value < 0) {
1353 DBG(cdev, "ep_queue --> %d\n", value);
1354 req->status = 0;
1355 composite_setup_complete(cdev->gadget->ep0, req);
1356 }
1357 }
1358
1359 spin_unlock_irqrestore(&cdev->lock, flags);
1360}
1361