blob: 4c33695695c1f665a64b33c70f0821ef605cc4f9 [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>
Benoit Gobyaab96812011-04-19 20:37:33 -070028
David Brownell40982be2008-06-19 17:52:58 -070029#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 */
Manu Gautam8d887a12011-10-12 17:13:52 +053040#define USB_BUFSIZ 4096
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
Benoit Gobyaab96812011-04-19 20:37:33 -070097 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -070098 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)
Benoit Gobyaab96812011-04-19 20:37:33 -0700129 DBG(config->cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700130 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;
243
244 /* write the config descriptor */
245 c = buf;
246 c->bLength = USB_DT_CONFIG_SIZE;
247 c->bDescriptorType = type;
Benoit Gobyaab96812011-04-19 20:37:33 -0700248 /* wTotalLength is written later */
249 c->bNumInterfaces = config->next_interface_id;
David Brownell40982be2008-06-19 17:52:58 -0700250 c->bConfigurationValue = config->bConfigurationValue;
251 c->iConfiguration = config->iConfiguration;
252 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700253 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700254
255 /* There may be e.g. OTG descriptors */
256 if (config->descriptors) {
257 status = usb_descriptor_fillbuf(next, len,
258 config->descriptors);
259 if (status < 0)
260 return status;
261 len -= status;
262 next += status;
263 }
264
265 /* add each function's descriptors */
266 list_for_each_entry(f, &config->functions, list) {
267 struct usb_descriptor_header **descriptors;
268
269 if (speed == USB_SPEED_HIGH)
270 descriptors = f->hs_descriptors;
271 else
272 descriptors = f->descriptors;
Benoit Gobyaab96812011-04-19 20:37:33 -0700273 if (!descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700274 continue;
275 status = usb_descriptor_fillbuf(next, len,
276 (const struct usb_descriptor_header **) descriptors);
277 if (status < 0)
278 return status;
279 len -= status;
280 next += status;
281 }
282
283 len = next - buf;
284 c->wTotalLength = cpu_to_le16(len);
285 return len;
286}
287
288static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
289{
290 struct usb_gadget *gadget = cdev->gadget;
291 struct usb_configuration *c;
292 u8 type = w_value >> 8;
293 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
294
295 if (gadget_is_dualspeed(gadget)) {
296 int hs = 0;
297
298 if (gadget->speed == USB_SPEED_HIGH)
299 hs = 1;
300 if (type == USB_DT_OTHER_SPEED_CONFIG)
301 hs = !hs;
302 if (hs)
303 speed = USB_SPEED_HIGH;
304
305 }
306
307 /* This is a lookup by config *INDEX* */
308 w_value &= 0xff;
309 list_for_each_entry(c, &cdev->configs, list) {
310 /* ignore configs that won't work at this speed */
311 if (speed == USB_SPEED_HIGH) {
312 if (!c->highspeed)
313 continue;
314 } else {
315 if (!c->fullspeed)
316 continue;
317 }
318 if (w_value == 0)
319 return config_buf(c, speed, cdev->req->buf, type);
320 w_value--;
321 }
322 return -EINVAL;
323}
324
325static int count_configs(struct usb_composite_dev *cdev, unsigned type)
326{
327 struct usb_gadget *gadget = cdev->gadget;
328 struct usb_configuration *c;
329 unsigned count = 0;
330 int hs = 0;
331
332 if (gadget_is_dualspeed(gadget)) {
333 if (gadget->speed == USB_SPEED_HIGH)
334 hs = 1;
335 if (type == USB_DT_DEVICE_QUALIFIER)
336 hs = !hs;
337 }
338 list_for_each_entry(c, &cdev->configs, list) {
339 /* ignore configs that won't work at this speed */
340 if (hs) {
341 if (!c->highspeed)
342 continue;
343 } else {
344 if (!c->fullspeed)
345 continue;
346 }
347 count++;
348 }
349 return count;
350}
351
352static void device_qual(struct usb_composite_dev *cdev)
353{
354 struct usb_qualifier_descriptor *qual = cdev->req->buf;
355
356 qual->bLength = sizeof(*qual);
357 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
358 /* POLICY: same bcdUSB and device type info at both speeds */
359 qual->bcdUSB = cdev->desc.bcdUSB;
360 qual->bDeviceClass = cdev->desc.bDeviceClass;
361 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
362 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
363 /* ASSUME same EP0 fifo size at both speeds */
364 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
365 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700366 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700367}
368
369/*-------------------------------------------------------------------------*/
370
371static void reset_config(struct usb_composite_dev *cdev)
372{
373 struct usb_function *f;
374
375 DBG(cdev, "reset config\n");
376
377 list_for_each_entry(f, &cdev->config->functions, list) {
378 if (f->disable)
379 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200380
381 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700382 }
383 cdev->config = NULL;
384}
385
386static int set_config(struct usb_composite_dev *cdev,
387 const struct usb_ctrlrequest *ctrl, unsigned number)
388{
389 struct usb_gadget *gadget = cdev->gadget;
390 struct usb_configuration *c = NULL;
391 int result = -EINVAL;
392 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
393 int tmp;
394
395 if (cdev->config)
396 reset_config(cdev);
397
398 if (number) {
399 list_for_each_entry(c, &cdev->configs, list) {
400 if (c->bConfigurationValue == number) {
401 result = 0;
402 break;
403 }
404 }
405 if (result < 0)
406 goto done;
407 } else
408 result = 0;
409
410 INFO(cdev, "%s speed config #%d: %s\n",
411 ({ char *speed;
412 switch (gadget->speed) {
413 case USB_SPEED_LOW: speed = "low"; break;
414 case USB_SPEED_FULL: speed = "full"; break;
415 case USB_SPEED_HIGH: speed = "high"; break;
416 default: speed = "?"; break;
417 } ; speed; }), number, c ? c->label : "unconfigured");
418
419 if (!c)
420 goto done;
421
422 cdev->config = c;
423
424 /* Initialize all interfaces by setting them to altsetting zero. */
425 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
426 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200427 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700428
429 if (!f)
430 break;
431
Laurent Pinchart52426582009-10-21 00:03:38 +0200432 /*
433 * Record which endpoints are used by the function. This is used
434 * to dispatch control requests targeted at that endpoint to the
435 * function's setup callback instead of the current
436 * configuration's setup callback.
437 */
438 if (gadget->speed == USB_SPEED_HIGH)
439 descriptors = f->hs_descriptors;
440 else
441 descriptors = f->descriptors;
442
443 for (; *descriptors; ++descriptors) {
444 struct usb_endpoint_descriptor *ep;
445 int addr;
446
447 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
448 continue;
449
450 ep = (struct usb_endpoint_descriptor *)*descriptors;
451 addr = ((ep->bEndpointAddress & 0x80) >> 3)
452 | (ep->bEndpointAddress & 0x0f);
453 set_bit(addr, f->endpoints);
454 }
455
David Brownell40982be2008-06-19 17:52:58 -0700456 result = f->set_alt(f, tmp, 0);
457 if (result < 0) {
458 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
459 tmp, f->name, f, result);
460
461 reset_config(cdev);
462 goto done;
463 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300464
465 if (result == USB_GADGET_DELAYED_STATUS) {
466 DBG(cdev,
467 "%s: interface %d (%s) requested delayed status\n",
468 __func__, tmp, f->name);
469 cdev->delayed_status++;
470 DBG(cdev, "delayed_status count %d\n",
471 cdev->delayed_status);
472 }
David Brownell40982be2008-06-19 17:52:58 -0700473 }
474
475 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700476 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700477done:
478 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400479
Roger Quadros1b9ba002011-05-09 13:08:06 +0300480 if (result >= 0 && cdev->delayed_status)
481 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700482 return result;
483}
484
485/**
486 * usb_add_config() - add a configuration to a device.
487 * @cdev: wraps the USB gadget
488 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200489 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700490 * Context: single threaded during gadget setup
491 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200492 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700493 * add each of the configurations it supports, using this routine.
494 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200495 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700496 * is zero for success else a negative errno value. Binding configurations
497 * assigns global resources including string IDs, and per-configuration
498 * resources such as interface IDs and endpoints.
499 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200500int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200501 struct usb_configuration *config,
502 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700503{
504 int status = -EINVAL;
505 struct usb_configuration *c;
506
507 DBG(cdev, "adding config #%u '%s'/%p\n",
508 config->bConfigurationValue,
509 config->label, config);
510
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200511 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700512 goto done;
513
514 /* Prevent duplicate configuration identifiers */
515 list_for_each_entry(c, &cdev->configs, list) {
516 if (c->bConfigurationValue == config->bConfigurationValue) {
517 status = -EBUSY;
518 goto done;
519 }
520 }
521
522 config->cdev = cdev;
523 list_add_tail(&config->list, &cdev->configs);
524
525 INIT_LIST_HEAD(&config->functions);
526 config->next_interface_id = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700527 memset(config->interface, '\0', sizeof(config->interface));
David Brownell40982be2008-06-19 17:52:58 -0700528
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200529 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700530 if (status < 0) {
531 list_del(&config->list);
532 config->cdev = NULL;
533 } else {
534 unsigned i;
535
536 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
537 config->bConfigurationValue, config,
538 config->highspeed ? " high" : "",
539 config->fullspeed
540 ? (gadget_is_dualspeed(cdev->gadget)
541 ? " full"
542 : " full/low")
543 : "");
544
545 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
546 struct usb_function *f = config->interface[i];
547
548 if (!f)
549 continue;
550 DBG(cdev, " interface %d = %s/%p\n",
551 i, f->name, f);
552 }
553 }
554
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200555 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700556 * ep->driver_data as needed.
557 */
558 usb_ep_autoconfig_reset(cdev->gadget);
559
560done:
561 if (status)
562 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
563 config->bConfigurationValue, status);
564 return status;
565}
566
Benoit Goby94df1bd2011-05-25 23:59:43 -0700567static int remove_config(struct usb_composite_dev *cdev,
568 struct usb_configuration *config)
569{
570 while (!list_empty(&config->functions)) {
571 struct usb_function *f;
572
573 f = list_first_entry(&config->functions,
574 struct usb_function, list);
575 list_del(&f->list);
576 if (f->unbind) {
577 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
578 f->unbind(config, f);
579 /* may free memory for "f" */
580 }
581 }
582 list_del(&config->list);
583 if (config->unbind) {
584 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
585 config->unbind(config);
586 /* may free memory for "c" */
587 }
588 return 0;
589}
590
591int usb_remove_config(struct usb_composite_dev *cdev,
592 struct usb_configuration *config)
593{
594 unsigned long flags;
595
596 spin_lock_irqsave(&cdev->lock, flags);
597
598 if (cdev->config == config)
599 reset_config(cdev);
600
601 spin_unlock_irqrestore(&cdev->lock, flags);
602
603 return remove_config(cdev, config);
604}
605
David Brownell40982be2008-06-19 17:52:58 -0700606/*-------------------------------------------------------------------------*/
607
608/* We support strings in multiple languages ... string descriptor zero
609 * says which languages are supported. The typical case will be that
610 * only one language (probably English) is used, with I18N handled on
611 * the host side.
612 */
613
614static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
615{
616 const struct usb_gadget_strings *s;
617 u16 language;
618 __le16 *tmp;
619
620 while (*sp) {
621 s = *sp;
622 language = cpu_to_le16(s->language);
623 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
624 if (*tmp == language)
625 goto repeat;
626 }
627 *tmp++ = language;
628repeat:
629 sp++;
630 }
631}
632
633static int lookup_string(
634 struct usb_gadget_strings **sp,
635 void *buf,
636 u16 language,
637 int id
638)
639{
640 struct usb_gadget_strings *s;
641 int value;
642
643 while (*sp) {
644 s = *sp++;
645 if (s->language != language)
646 continue;
647 value = usb_gadget_get_string(s, id, buf);
648 if (value > 0)
649 return value;
650 }
651 return -EINVAL;
652}
653
654static int get_string(struct usb_composite_dev *cdev,
655 void *buf, u16 language, int id)
656{
657 struct usb_configuration *c;
658 struct usb_function *f;
659 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200660 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700661
662 /* Yes, not only is USB's I18N support probably more than most
663 * folk will ever care about ... also, it's all supported here.
664 * (Except for UTF8 support for Unicode's "Astral Planes".)
665 */
666
667 /* 0 == report all available language codes */
668 if (id == 0) {
669 struct usb_string_descriptor *s = buf;
670 struct usb_gadget_strings **sp;
671
672 memset(s, 0, 256);
673 s->bDescriptorType = USB_DT_STRING;
674
675 sp = composite->strings;
676 if (sp)
677 collect_langs(sp, s->wData);
678
679 list_for_each_entry(c, &cdev->configs, list) {
680 sp = c->strings;
681 if (sp)
682 collect_langs(sp, s->wData);
683
684 list_for_each_entry(f, &c->functions, list) {
685 sp = f->strings;
686 if (sp)
687 collect_langs(sp, s->wData);
688 }
689 }
690
Roel Kluin417b57b2009-08-06 16:09:51 -0700691 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700692 continue;
693 if (!len)
694 return -EINVAL;
695
696 s->bLength = 2 * (len + 1);
697 return s->bLength;
698 }
699
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200700 /* Otherwise, look up and return a specified string. First
701 * check if the string has not been overridden.
702 */
703 if (cdev->manufacturer_override == id)
704 str = iManufacturer ?: composite->iManufacturer ?:
705 composite_manufacturer;
706 else if (cdev->product_override == id)
707 str = iProduct ?: composite->iProduct;
708 else if (cdev->serial_override == id)
709 str = iSerialNumber;
710 else
711 str = NULL;
712 if (str) {
713 struct usb_gadget_strings strings = {
714 .language = language,
715 .strings = &(struct usb_string) { 0xff, str }
716 };
717 return usb_gadget_get_string(&strings, 0xff, buf);
718 }
719
720 /* String IDs are device-scoped, so we look up each string
721 * table we're told about. These lookups are infrequent;
722 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700723 */
724 if (composite->strings) {
725 len = lookup_string(composite->strings, buf, language, id);
726 if (len > 0)
727 return len;
728 }
729 list_for_each_entry(c, &cdev->configs, list) {
730 if (c->strings) {
731 len = lookup_string(c->strings, buf, language, id);
732 if (len > 0)
733 return len;
734 }
735 list_for_each_entry(f, &c->functions, list) {
736 if (!f->strings)
737 continue;
738 len = lookup_string(f->strings, buf, language, id);
739 if (len > 0)
740 return len;
741 }
742 }
743 return -EINVAL;
744}
745
746/**
747 * usb_string_id() - allocate an unused string ID
748 * @cdev: the device whose string descriptor IDs are being allocated
749 * Context: single threaded during gadget setup
750 *
751 * @usb_string_id() is called from bind() callbacks to allocate
752 * string IDs. Drivers for functions, configurations, or gadgets will
753 * then store that ID in the appropriate descriptors and string table.
754 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200755 * All string identifier should be allocated using this,
756 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
757 * that for example different functions don't wrongly assign different
758 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700759 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200760int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700761{
762 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200763 /* string id 0 is reserved by USB spec for list of
764 * supported languages */
765 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700766 cdev->next_string_id++;
767 return cdev->next_string_id;
768 }
769 return -ENODEV;
770}
771
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200772/**
773 * usb_string_ids() - allocate unused string IDs in batch
774 * @cdev: the device whose string descriptor IDs are being allocated
775 * @str: an array of usb_string objects to assign numbers to
776 * Context: single threaded during gadget setup
777 *
778 * @usb_string_ids() is called from bind() callbacks to allocate
779 * string IDs. Drivers for functions, configurations, or gadgets will
780 * then copy IDs from the string table to the appropriate descriptors
781 * and string table for other languages.
782 *
783 * All string identifier should be allocated using this,
784 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
785 * example different functions don't wrongly assign different meanings
786 * to the same identifier.
787 */
788int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
789{
790 int next = cdev->next_string_id;
791
792 for (; str->s; ++str) {
793 if (unlikely(next >= 254))
794 return -ENODEV;
795 str->id = ++next;
796 }
797
798 cdev->next_string_id = next;
799
800 return 0;
801}
802
803/**
804 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700805 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200806 * @n: number of string IDs to allocate
807 * Context: single threaded during gadget setup
808 *
809 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700810 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200811 * is, returns last requested ID which is now very useful information.
812 *
813 * @usb_string_ids_n() is called from bind() callbacks to allocate
814 * string IDs. Drivers for functions, configurations, or gadgets will
815 * then store that ID in the appropriate descriptors and string table.
816 *
817 * All string identifier should be allocated using this,
818 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
819 * example different functions don't wrongly assign different meanings
820 * to the same identifier.
821 */
822int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
823{
824 unsigned next = c->next_string_id;
825 if (unlikely(n > 254 || (unsigned)next + n > 254))
826 return -ENODEV;
827 c->next_string_id += n;
828 return next + 1;
829}
830
831
David Brownell40982be2008-06-19 17:52:58 -0700832/*-------------------------------------------------------------------------*/
833
834static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
835{
836 if (req->status || req->actual != req->length)
837 DBG((struct usb_composite_dev *) ep->driver_data,
838 "setup complete --> %d, %d/%d\n",
839 req->status, req->actual, req->length);
840}
841
842/*
843 * The setup() callback implements all the ep0 functionality that's
844 * not handled lower down, in hardware or the hardware driver(like
845 * device and endpoint feature flags, and their status). It's all
846 * housekeeping for the gadget function we're implementing. Most of
847 * the work is in config and function specific setup.
848 */
849static int
850composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
851{
852 struct usb_composite_dev *cdev = get_gadget_data(gadget);
853 struct usb_request *req = cdev->req;
854 int value = -EOPNOTSUPP;
855 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800856 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700857 u16 w_value = le16_to_cpu(ctrl->wValue);
858 u16 w_length = le16_to_cpu(ctrl->wLength);
859 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200860 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700861
Manu Gautam8d887a12011-10-12 17:13:52 +0530862
863 if (w_length > USB_BUFSIZ)
864 return value;
865
David Brownell40982be2008-06-19 17:52:58 -0700866 /* partial re-init of the response message; the function or the
867 * gadget might need to intercept e.g. a control-OUT completion
868 * when we delegate to it.
869 */
870 req->zero = 0;
871 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530872 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700873 gadget->ep0->driver_data = cdev;
874
875 switch (ctrl->bRequest) {
876
877 /* we handle all standard USB descriptors */
878 case USB_REQ_GET_DESCRIPTOR:
879 if (ctrl->bRequestType != USB_DIR_IN)
880 goto unknown;
881 switch (w_value >> 8) {
882
883 case USB_DT_DEVICE:
884 cdev->desc.bNumConfigurations =
885 count_configs(cdev, USB_DT_DEVICE);
886 value = min(w_length, (u16) sizeof cdev->desc);
887 memcpy(req->buf, &cdev->desc, value);
888 break;
889 case USB_DT_DEVICE_QUALIFIER:
890 if (!gadget_is_dualspeed(gadget))
891 break;
892 device_qual(cdev);
893 value = min_t(int, w_length,
894 sizeof(struct usb_qualifier_descriptor));
895 break;
896 case USB_DT_OTHER_SPEED_CONFIG:
897 if (!gadget_is_dualspeed(gadget))
898 break;
899 /* FALLTHROUGH */
900 case USB_DT_CONFIG:
901 value = config_desc(cdev, w_value);
902 if (value >= 0)
903 value = min(w_length, (u16) value);
904 break;
905 case USB_DT_STRING:
906 value = get_string(cdev, req->buf,
907 w_index, w_value & 0xff);
908 if (value >= 0)
909 value = min(w_length, (u16) value);
910 break;
911 }
912 break;
913
914 /* any number of configs can work */
915 case USB_REQ_SET_CONFIGURATION:
916 if (ctrl->bRequestType != 0)
917 goto unknown;
918 if (gadget_is_otg(gadget)) {
919 if (gadget->a_hnp_support)
920 DBG(cdev, "HNP available\n");
921 else if (gadget->a_alt_hnp_support)
922 DBG(cdev, "HNP on another port\n");
923 else
924 VDBG(cdev, "HNP inactive\n");
925 }
926 spin_lock(&cdev->lock);
927 value = set_config(cdev, ctrl, w_value);
928 spin_unlock(&cdev->lock);
929 break;
930 case USB_REQ_GET_CONFIGURATION:
931 if (ctrl->bRequestType != USB_DIR_IN)
932 goto unknown;
Oleg Matcovschie67dd162011-03-11 10:24:30 -0800933 if (cdev->config)
David Brownell40982be2008-06-19 17:52:58 -0700934 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Oleg Matcovschie67dd162011-03-11 10:24:30 -0800935 else
David Brownell40982be2008-06-19 17:52:58 -0700936 *(u8 *)req->buf = 0;
Oleg Matcovschie67dd162011-03-11 10:24:30 -0800937 value = min(w_length, (u16) 1);
David Brownell40982be2008-06-19 17:52:58 -0700938 break;
939
940 /* function drivers must handle get/set altsetting; if there's
941 * no get() method, we know only altsetting zero works.
942 */
943 case USB_REQ_SET_INTERFACE:
944 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
945 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900946 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700947 break;
Bryan Wu08889512009-01-08 00:21:19 +0800948 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700949 if (!f)
950 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800951 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700952 break;
953 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300954 if (value == USB_GADGET_DELAYED_STATUS) {
955 DBG(cdev,
956 "%s: interface %d (%s) requested delayed status\n",
957 __func__, intf, f->name);
958 cdev->delayed_status++;
959 DBG(cdev, "delayed_status count %d\n",
960 cdev->delayed_status);
961 }
David Brownell40982be2008-06-19 17:52:58 -0700962 break;
963 case USB_REQ_GET_INTERFACE:
964 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
965 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900966 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700967 break;
Bryan Wu08889512009-01-08 00:21:19 +0800968 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700969 if (!f)
970 break;
971 /* lots of interfaces only need altsetting zero... */
972 value = f->get_alt ? f->get_alt(f, w_index) : 0;
973 if (value < 0)
974 break;
975 *((u8 *)req->buf) = value;
976 value = min(w_length, (u16) 1);
977 break;
978 default:
979unknown:
980 VDBG(cdev,
981 "non-core control req%02x.%02x v%04x i%04x l%d\n",
982 ctrl->bRequestType, ctrl->bRequest,
983 w_value, w_index, w_length);
984
Laurent Pinchart52426582009-10-21 00:03:38 +0200985 /* functions always handle their interfaces and endpoints...
986 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -0700987 * configuration code.
988 *
989 * REVISIT it could make sense to let the composite device
990 * take such requests too, if that's ever needed: to work
991 * in config 0, etc.
992 */
Laurent Pinchart52426582009-10-21 00:03:38 +0200993 switch (ctrl->bRequestType & USB_RECIP_MASK) {
994 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +0900995 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +0530996 break;
997 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +0200998 break;
999
1000 case USB_RECIP_ENDPOINT:
1001 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1002 list_for_each_entry(f, &cdev->config->functions, list) {
1003 if (test_bit(endp, f->endpoints))
1004 break;
1005 }
1006 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001007 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001008 break;
David Brownell40982be2008-06-19 17:52:58 -07001009 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001010
1011 if (f && f->setup)
1012 value = f->setup(f, ctrl);
1013 else {
David Brownell40982be2008-06-19 17:52:58 -07001014 struct usb_configuration *c;
1015
1016 c = cdev->config;
1017 if (c && c->setup)
1018 value = c->setup(c, ctrl);
1019 }
1020
1021 goto done;
1022 }
1023
1024 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001025 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001026 req->length = value;
1027 req->zero = value < w_length;
1028 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1029 if (value < 0) {
1030 DBG(cdev, "ep_queue --> %d\n", value);
1031 req->status = 0;
1032 composite_setup_complete(gadget->ep0, req);
1033 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001034 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1035 WARN(cdev,
1036 "%s: Delayed status not supported for w_length != 0",
1037 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001038 }
1039
1040done:
1041 /* device either stalls (value < 0) or reports success */
1042 return value;
1043}
1044
1045static void composite_disconnect(struct usb_gadget *gadget)
1046{
1047 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1048 unsigned long flags;
1049
1050 /* REVISIT: should we have config and device level
1051 * disconnect callbacks?
1052 */
1053 spin_lock_irqsave(&cdev->lock, flags);
1054 if (cdev->config)
1055 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001056 if (composite->disconnect)
1057 composite->disconnect(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001058 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001059}
1060
1061/*-------------------------------------------------------------------------*/
1062
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001063static ssize_t composite_show_suspended(struct device *dev,
1064 struct device_attribute *attr,
1065 char *buf)
1066{
1067 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1068 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1069
1070 return sprintf(buf, "%d\n", cdev->suspended);
1071}
1072
1073static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1074
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001075static void
David Brownell40982be2008-06-19 17:52:58 -07001076composite_unbind(struct usb_gadget *gadget)
1077{
1078 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1079
1080 /* composite_disconnect() must already have been called
1081 * by the underlying peripheral controller driver!
1082 * so there's no i/o concurrency that could affect the
1083 * state protected by cdev->lock.
1084 */
1085 WARN_ON(cdev->config);
1086
1087 while (!list_empty(&cdev->configs)) {
1088 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001089 c = list_first_entry(&cdev->configs,
1090 struct usb_configuration, list);
Benoit Goby94df1bd2011-05-25 23:59:43 -07001091 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001092 }
1093 if (composite->unbind)
1094 composite->unbind(cdev);
1095
1096 if (cdev->req) {
1097 kfree(cdev->req->buf);
1098 usb_ep_free_request(gadget->ep0, cdev->req);
1099 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301100 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001101 kfree(cdev);
1102 set_gadget_data(gadget, NULL);
1103 composite = NULL;
1104}
1105
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001106static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001107{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001108 if (!*desc) {
1109 int ret = usb_string_id(cdev);
1110 if (unlikely(ret < 0))
1111 WARNING(cdev, "failed to override string ID\n");
1112 else
1113 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001114 }
David Brownell40982be2008-06-19 17:52:58 -07001115
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001116 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001117}
1118
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001119static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001120{
1121 struct usb_composite_dev *cdev;
1122 int status = -ENOMEM;
1123
1124 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1125 if (!cdev)
1126 return status;
1127
1128 spin_lock_init(&cdev->lock);
1129 cdev->gadget = gadget;
1130 set_gadget_data(gadget, cdev);
1131 INIT_LIST_HEAD(&cdev->configs);
1132
1133 /* preallocate control response and buffer */
1134 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1135 if (!cdev->req)
1136 goto fail;
1137 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1138 if (!cdev->req->buf)
1139 goto fail;
1140 cdev->req->complete = composite_setup_complete;
1141 gadget->ep0->driver_data = cdev;
1142
1143 cdev->bufsiz = USB_BUFSIZ;
1144 cdev->driver = composite;
1145
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301146 /*
1147 * As per USB compliance update, a device that is actively drawing
1148 * more than 100mA from USB must report itself as bus-powered in
1149 * the GetStatus(DEVICE) call.
1150 */
1151 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1152 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001153
1154 /* interface and string IDs start at zero via kzalloc.
1155 * we force endpoints to start unassigned; few controller
1156 * drivers will zero ep->driver_data.
1157 */
1158 usb_ep_autoconfig_reset(cdev->gadget);
1159
1160 /* composite gadget needs to assign strings for whole device (like
1161 * serial number), register function drivers, potentially update
1162 * power state and consumption, etc
1163 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001164 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001165 if (status < 0)
1166 goto fail;
1167
1168 cdev->desc = *composite->dev;
1169 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1170
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001171 /* standardized runtime overrides for device ID data */
1172 if (idVendor)
1173 cdev->desc.idVendor = cpu_to_le16(idVendor);
1174 if (idProduct)
1175 cdev->desc.idProduct = cpu_to_le16(idProduct);
1176 if (bcdDevice)
1177 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1178
Marek Belisko78bff3c2010-10-27 10:19:01 +02001179 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001180 if (iManufacturer || !cdev->desc.iManufacturer) {
1181 if (!iManufacturer && !composite->iManufacturer &&
1182 !*composite_manufacturer)
1183 snprintf(composite_manufacturer,
1184 sizeof composite_manufacturer,
1185 "%s %s with %s",
1186 init_utsname()->sysname,
1187 init_utsname()->release,
1188 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001189
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001190 cdev->manufacturer_override =
1191 override_id(cdev, &cdev->desc.iManufacturer);
1192 }
1193
1194 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1195 cdev->product_override =
1196 override_id(cdev, &cdev->desc.iProduct);
1197
1198 if (iSerialNumber)
1199 cdev->serial_override =
1200 override_id(cdev, &cdev->desc.iSerialNumber);
1201
1202 /* has userspace failed to provide a serial number? */
1203 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1204 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1205
1206 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001207 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1208 if (status)
1209 goto fail;
1210
David Brownell40982be2008-06-19 17:52:58 -07001211 INFO(cdev, "%s ready\n", composite->name);
1212 return 0;
1213
1214fail:
1215 composite_unbind(gadget);
1216 return status;
1217}
1218
1219/*-------------------------------------------------------------------------*/
1220
1221static void
1222composite_suspend(struct usb_gadget *gadget)
1223{
1224 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1225 struct usb_function *f;
1226
David Brownell89429392009-03-19 14:14:17 -07001227 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001228 * suspend/resume callbacks?
1229 */
1230 DBG(cdev, "suspend\n");
1231 if (cdev->config) {
1232 list_for_each_entry(f, &cdev->config->functions, list) {
1233 if (f->suspend)
1234 f->suspend(f);
1235 }
1236 }
David Brownell89429392009-03-19 14:14:17 -07001237 if (composite->suspend)
1238 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001239
1240 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001241
1242 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001243}
1244
1245static void
1246composite_resume(struct usb_gadget *gadget)
1247{
1248 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1249 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001250 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001251
David Brownell89429392009-03-19 14:14:17 -07001252 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001253 * suspend/resume callbacks?
1254 */
1255 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001256 if (composite->resume)
1257 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001258 if (cdev->config) {
1259 list_for_each_entry(f, &cdev->config->functions, list) {
1260 if (f->resume)
1261 f->resume(f);
1262 }
Hao Wub23f2f92010-11-29 15:17:03 +08001263
1264 maxpower = cdev->config->bMaxPower;
1265
1266 usb_gadget_vbus_draw(gadget, maxpower ?
1267 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001268 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001269
1270 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001271}
1272
1273/*-------------------------------------------------------------------------*/
1274
1275static struct usb_gadget_driver composite_driver = {
1276 .speed = USB_SPEED_HIGH,
1277
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001278 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001279
1280 .setup = composite_setup,
1281 .disconnect = composite_disconnect,
1282
1283 .suspend = composite_suspend,
1284 .resume = composite_resume,
1285
1286 .driver = {
1287 .owner = THIS_MODULE,
1288 },
1289};
1290
1291/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001292 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001293 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001294 * @bind: the callback used to allocate resources that are shared across the
1295 * whole device, such as string IDs, and add its configurations using
1296 * @usb_add_config(). This may fail by returning a negative errno
1297 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001298 * Context: single threaded during gadget setup
1299 *
1300 * This function is used to register drivers using the composite driver
1301 * framework. The return value is zero, or a negative errno value.
1302 * Those values normally come from the driver's @bind method, which does
1303 * all the work of setting up the driver to match the hardware.
1304 *
1305 * On successful return, the gadget is ready to respond to requests from
1306 * the host, unless one of its components invokes usb_gadget_disconnect()
1307 * while it was binding. That would usually be done in order to wait for
1308 * some userspace participation.
1309 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001310int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001311 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001312{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001313 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001314 return -EINVAL;
1315
1316 if (!driver->name)
1317 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001318 if (!driver->iProduct)
1319 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001320 composite_driver.function = (char *) driver->name;
1321 composite_driver.driver.name = driver->name;
1322 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001323 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001324
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001325 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001326}
1327
1328/**
1329 * usb_composite_unregister() - unregister a composite driver
1330 * @driver: the driver to unregister
1331 *
1332 * This function is used to unregister drivers using the composite
1333 * driver framework.
1334 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001335void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001336{
1337 if (composite != driver)
1338 return;
1339 usb_gadget_unregister_driver(&composite_driver);
1340}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001341
1342/**
1343 * usb_composite_setup_continue() - Continue with the control transfer
1344 * @cdev: the composite device who's control transfer was kept waiting
1345 *
1346 * This function must be called by the USB function driver to continue
1347 * with the control transfer's data/status stage in case it had requested to
1348 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1349 * can request the composite framework to delay the setup request's data/status
1350 * stages by returning USB_GADGET_DELAYED_STATUS.
1351 */
1352void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1353{
1354 int value;
1355 struct usb_request *req = cdev->req;
1356 unsigned long flags;
1357
1358 DBG(cdev, "%s\n", __func__);
1359 spin_lock_irqsave(&cdev->lock, flags);
1360
1361 if (cdev->delayed_status == 0) {
1362 WARN(cdev, "%s: Unexpected call\n", __func__);
1363
1364 } else if (--cdev->delayed_status == 0) {
1365 DBG(cdev, "%s: Completing delayed status\n", __func__);
1366 req->length = 0;
1367 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1368 if (value < 0) {
1369 DBG(cdev, "ep_queue --> %d\n", value);
1370 req->status = 0;
1371 composite_setup_complete(cdev->gadget->ep0, req);
1372 }
1373 }
1374
1375 spin_unlock_irqrestore(&cdev->lock, flags);
1376}
1377