blob: a1a54b4050c8506d93ff7c306aef166aad4d4e8b [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;
Jared Suttles646bb862009-07-30 16:13:27 -0500274 if (!descriptors || descriptors[0] == NULL) {
275 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;
897 if (cdev->config)
898 *(u8 *)req->buf = cdev->config->bConfigurationValue;
899 else
900 *(u8 *)req->buf = 0;
901 value = min(w_length, (u16) 1);
902 break;
903
904 /* function drivers must handle get/set altsetting; if there's
905 * no get() method, we know only altsetting zero works.
906 */
907 case USB_REQ_SET_INTERFACE:
908 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
909 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900910 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700911 break;
Bryan Wu08889512009-01-08 00:21:19 +0800912 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700913 if (!f)
914 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800915 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700916 break;
917 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300918 if (value == USB_GADGET_DELAYED_STATUS) {
919 DBG(cdev,
920 "%s: interface %d (%s) requested delayed status\n",
921 __func__, intf, f->name);
922 cdev->delayed_status++;
923 DBG(cdev, "delayed_status count %d\n",
924 cdev->delayed_status);
925 }
David Brownell40982be2008-06-19 17:52:58 -0700926 break;
927 case USB_REQ_GET_INTERFACE:
928 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
929 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900930 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700931 break;
Bryan Wu08889512009-01-08 00:21:19 +0800932 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700933 if (!f)
934 break;
935 /* lots of interfaces only need altsetting zero... */
936 value = f->get_alt ? f->get_alt(f, w_index) : 0;
937 if (value < 0)
938 break;
939 *((u8 *)req->buf) = value;
940 value = min(w_length, (u16) 1);
941 break;
942 default:
943unknown:
944 VDBG(cdev,
945 "non-core control req%02x.%02x v%04x i%04x l%d\n",
946 ctrl->bRequestType, ctrl->bRequest,
947 w_value, w_index, w_length);
948
Laurent Pinchart52426582009-10-21 00:03:38 +0200949 /* functions always handle their interfaces and endpoints...
950 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -0700951 * configuration code.
952 *
953 * REVISIT it could make sense to let the composite device
954 * take such requests too, if that's ever needed: to work
955 * in config 0, etc.
956 */
Laurent Pinchart52426582009-10-21 00:03:38 +0200957 switch (ctrl->bRequestType & USB_RECIP_MASK) {
958 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +0900959 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +0530960 break;
961 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +0200962 break;
963
964 case USB_RECIP_ENDPOINT:
965 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
966 list_for_each_entry(f, &cdev->config->functions, list) {
967 if (test_bit(endp, f->endpoints))
968 break;
969 }
970 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -0700971 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200972 break;
David Brownell40982be2008-06-19 17:52:58 -0700973 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200974
975 if (f && f->setup)
976 value = f->setup(f, ctrl);
977 else {
David Brownell40982be2008-06-19 17:52:58 -0700978 struct usb_configuration *c;
979
980 c = cdev->config;
981 if (c && c->setup)
982 value = c->setup(c, ctrl);
983 }
984
985 goto done;
986 }
987
988 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +0300989 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -0700990 req->length = value;
991 req->zero = value < w_length;
992 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
993 if (value < 0) {
994 DBG(cdev, "ep_queue --> %d\n", value);
995 req->status = 0;
996 composite_setup_complete(gadget->ep0, req);
997 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300998 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
999 WARN(cdev,
1000 "%s: Delayed status not supported for w_length != 0",
1001 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001002 }
1003
1004done:
1005 /* device either stalls (value < 0) or reports success */
1006 return value;
1007}
1008
1009static void composite_disconnect(struct usb_gadget *gadget)
1010{
1011 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1012 unsigned long flags;
1013
1014 /* REVISIT: should we have config and device level
1015 * disconnect callbacks?
1016 */
1017 spin_lock_irqsave(&cdev->lock, flags);
1018 if (cdev->config)
1019 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001020 if (composite->disconnect)
1021 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001022 spin_unlock_irqrestore(&cdev->lock, flags);
1023}
1024
1025/*-------------------------------------------------------------------------*/
1026
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001027static ssize_t composite_show_suspended(struct device *dev,
1028 struct device_attribute *attr,
1029 char *buf)
1030{
1031 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1032 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1033
1034 return sprintf(buf, "%d\n", cdev->suspended);
1035}
1036
1037static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1038
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001039static void
David Brownell40982be2008-06-19 17:52:58 -07001040composite_unbind(struct usb_gadget *gadget)
1041{
1042 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1043
1044 /* composite_disconnect() must already have been called
1045 * by the underlying peripheral controller driver!
1046 * so there's no i/o concurrency that could affect the
1047 * state protected by cdev->lock.
1048 */
1049 WARN_ON(cdev->config);
1050
1051 while (!list_empty(&cdev->configs)) {
1052 struct usb_configuration *c;
1053
1054 c = list_first_entry(&cdev->configs,
1055 struct usb_configuration, list);
1056 while (!list_empty(&c->functions)) {
1057 struct usb_function *f;
1058
1059 f = list_first_entry(&c->functions,
1060 struct usb_function, list);
1061 list_del(&f->list);
1062 if (f->unbind) {
1063 DBG(cdev, "unbind function '%s'/%p\n",
1064 f->name, f);
1065 f->unbind(c, f);
1066 /* may free memory for "f" */
1067 }
1068 }
1069 list_del(&c->list);
1070 if (c->unbind) {
1071 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1072 c->unbind(c);
1073 /* may free memory for "c" */
1074 }
1075 }
1076 if (composite->unbind)
1077 composite->unbind(cdev);
1078
1079 if (cdev->req) {
1080 kfree(cdev->req->buf);
1081 usb_ep_free_request(gadget->ep0, cdev->req);
1082 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301083 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001084 kfree(cdev);
1085 set_gadget_data(gadget, NULL);
1086 composite = NULL;
1087}
1088
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001089static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001090{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001091 if (!*desc) {
1092 int ret = usb_string_id(cdev);
1093 if (unlikely(ret < 0))
1094 WARNING(cdev, "failed to override string ID\n");
1095 else
1096 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001097 }
David Brownell40982be2008-06-19 17:52:58 -07001098
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001099 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001100}
1101
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001102static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001103{
1104 struct usb_composite_dev *cdev;
1105 int status = -ENOMEM;
1106
1107 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1108 if (!cdev)
1109 return status;
1110
1111 spin_lock_init(&cdev->lock);
1112 cdev->gadget = gadget;
1113 set_gadget_data(gadget, cdev);
1114 INIT_LIST_HEAD(&cdev->configs);
1115
1116 /* preallocate control response and buffer */
1117 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1118 if (!cdev->req)
1119 goto fail;
1120 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1121 if (!cdev->req->buf)
1122 goto fail;
1123 cdev->req->complete = composite_setup_complete;
1124 gadget->ep0->driver_data = cdev;
1125
1126 cdev->bufsiz = USB_BUFSIZ;
1127 cdev->driver = composite;
1128
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301129 /*
1130 * As per USB compliance update, a device that is actively drawing
1131 * more than 100mA from USB must report itself as bus-powered in
1132 * the GetStatus(DEVICE) call.
1133 */
1134 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1135 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001136
1137 /* interface and string IDs start at zero via kzalloc.
1138 * we force endpoints to start unassigned; few controller
1139 * drivers will zero ep->driver_data.
1140 */
1141 usb_ep_autoconfig_reset(cdev->gadget);
1142
1143 /* composite gadget needs to assign strings for whole device (like
1144 * serial number), register function drivers, potentially update
1145 * power state and consumption, etc
1146 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001147 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001148 if (status < 0)
1149 goto fail;
1150
1151 cdev->desc = *composite->dev;
1152 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1153
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001154 /* standardized runtime overrides for device ID data */
1155 if (idVendor)
1156 cdev->desc.idVendor = cpu_to_le16(idVendor);
1157 if (idProduct)
1158 cdev->desc.idProduct = cpu_to_le16(idProduct);
1159 if (bcdDevice)
1160 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1161
Marek Belisko78bff3c2010-10-27 10:19:01 +02001162 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001163 if (iManufacturer || !cdev->desc.iManufacturer) {
1164 if (!iManufacturer && !composite->iManufacturer &&
1165 !*composite_manufacturer)
1166 snprintf(composite_manufacturer,
1167 sizeof composite_manufacturer,
1168 "%s %s with %s",
1169 init_utsname()->sysname,
1170 init_utsname()->release,
1171 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001172
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001173 cdev->manufacturer_override =
1174 override_id(cdev, &cdev->desc.iManufacturer);
1175 }
1176
1177 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1178 cdev->product_override =
1179 override_id(cdev, &cdev->desc.iProduct);
1180
1181 if (iSerialNumber)
1182 cdev->serial_override =
1183 override_id(cdev, &cdev->desc.iSerialNumber);
1184
1185 /* has userspace failed to provide a serial number? */
1186 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1187 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1188
1189 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001190 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1191 if (status)
1192 goto fail;
1193
David Brownell40982be2008-06-19 17:52:58 -07001194 INFO(cdev, "%s ready\n", composite->name);
1195 return 0;
1196
1197fail:
1198 composite_unbind(gadget);
1199 return status;
1200}
1201
1202/*-------------------------------------------------------------------------*/
1203
1204static void
1205composite_suspend(struct usb_gadget *gadget)
1206{
1207 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1208 struct usb_function *f;
1209
David Brownell89429392009-03-19 14:14:17 -07001210 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001211 * suspend/resume callbacks?
1212 */
1213 DBG(cdev, "suspend\n");
1214 if (cdev->config) {
1215 list_for_each_entry(f, &cdev->config->functions, list) {
1216 if (f->suspend)
1217 f->suspend(f);
1218 }
1219 }
David Brownell89429392009-03-19 14:14:17 -07001220 if (composite->suspend)
1221 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001222
1223 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001224
1225 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001226}
1227
1228static void
1229composite_resume(struct usb_gadget *gadget)
1230{
1231 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1232 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001233 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001234
David Brownell89429392009-03-19 14:14:17 -07001235 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001236 * suspend/resume callbacks?
1237 */
1238 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001239 if (composite->resume)
1240 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001241 if (cdev->config) {
1242 list_for_each_entry(f, &cdev->config->functions, list) {
1243 if (f->resume)
1244 f->resume(f);
1245 }
Hao Wub23f2f92010-11-29 15:17:03 +08001246
1247 maxpower = cdev->config->bMaxPower;
1248
1249 usb_gadget_vbus_draw(gadget, maxpower ?
1250 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001251 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001252
1253 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001254}
1255
1256/*-------------------------------------------------------------------------*/
1257
1258static struct usb_gadget_driver composite_driver = {
1259 .speed = USB_SPEED_HIGH,
1260
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001261 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001262
1263 .setup = composite_setup,
1264 .disconnect = composite_disconnect,
1265
1266 .suspend = composite_suspend,
1267 .resume = composite_resume,
1268
1269 .driver = {
1270 .owner = THIS_MODULE,
1271 },
1272};
1273
1274/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001275 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001276 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001277 * @bind: the callback used to allocate resources that are shared across the
1278 * whole device, such as string IDs, and add its configurations using
1279 * @usb_add_config(). This may fail by returning a negative errno
1280 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001281 * Context: single threaded during gadget setup
1282 *
1283 * This function is used to register drivers using the composite driver
1284 * framework. The return value is zero, or a negative errno value.
1285 * Those values normally come from the driver's @bind method, which does
1286 * all the work of setting up the driver to match the hardware.
1287 *
1288 * On successful return, the gadget is ready to respond to requests from
1289 * the host, unless one of its components invokes usb_gadget_disconnect()
1290 * while it was binding. That would usually be done in order to wait for
1291 * some userspace participation.
1292 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001293int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001294 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001295{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001296 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001297 return -EINVAL;
1298
1299 if (!driver->name)
1300 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001301 if (!driver->iProduct)
1302 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001303 composite_driver.function = (char *) driver->name;
1304 composite_driver.driver.name = driver->name;
1305 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001306 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001307
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001308 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001309}
1310
1311/**
1312 * usb_composite_unregister() - unregister a composite driver
1313 * @driver: the driver to unregister
1314 *
1315 * This function is used to unregister drivers using the composite
1316 * driver framework.
1317 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001318void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001319{
1320 if (composite != driver)
1321 return;
1322 usb_gadget_unregister_driver(&composite_driver);
1323}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001324
1325/**
1326 * usb_composite_setup_continue() - Continue with the control transfer
1327 * @cdev: the composite device who's control transfer was kept waiting
1328 *
1329 * This function must be called by the USB function driver to continue
1330 * with the control transfer's data/status stage in case it had requested to
1331 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1332 * can request the composite framework to delay the setup request's data/status
1333 * stages by returning USB_GADGET_DELAYED_STATUS.
1334 */
1335void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1336{
1337 int value;
1338 struct usb_request *req = cdev->req;
1339 unsigned long flags;
1340
1341 DBG(cdev, "%s\n", __func__);
1342 spin_lock_irqsave(&cdev->lock, flags);
1343
1344 if (cdev->delayed_status == 0) {
1345 WARN(cdev, "%s: Unexpected call\n", __func__);
1346
1347 } else if (--cdev->delayed_status == 0) {
1348 DBG(cdev, "%s: Completing delayed status\n", __func__);
1349 req->length = 0;
1350 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1351 if (value < 0) {
1352 DBG(cdev, "ep_queue --> %d\n", value);
1353 req->status = 0;
1354 composite_setup_complete(cdev->gadget->ep0, req);
1355 }
1356 }
1357
1358 spin_unlock_irqrestore(&cdev->lock, flags);
1359}
1360