blob: 75dd00038df524ec9764ae045c701353cf17f425 [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
Mike Lockwoodf041ac62010-02-06 21:53:51 -050078static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
79 char *buf)
80{
81 struct usb_function *f = dev_get_drvdata(dev);
Mike Lockwoode2dc5032010-06-23 08:20:59 -040082 return sprintf(buf, "%d\n", !f->disabled);
Mike Lockwoodf041ac62010-02-06 21:53:51 -050083}
84
85static ssize_t enable_store(
86 struct device *dev, struct device_attribute *attr,
87 const char *buf, size_t size)
88{
89 struct usb_function *f = dev_get_drvdata(dev);
90 struct usb_composite_driver *driver = f->config->cdev->driver;
91 int value;
92
93 sscanf(buf, "%d", &value);
94 if (driver->enable_function)
95 driver->enable_function(f, value);
96 else
Mike Lockwoode2dc5032010-06-23 08:20:59 -040097 usb_function_set_enabled(f, value);
Mike Lockwoodf041ac62010-02-06 21:53:51 -050098
99 return size;
100}
101
102static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
103
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400104void usb_function_set_enabled(struct usb_function *f, int enabled)
105{
106 f->disabled = !enabled;
107 kobject_uevent(&f->dev->kobj, KOBJ_CHANGE);
108}
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500109
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400110
111void usb_composite_force_reset(struct usb_composite_dev *cdev)
112{
113 unsigned long flags;
114
115 spin_lock_irqsave(&cdev->lock, flags);
116 /* force reenumeration */
Mike Lockwoode6be8942010-12-10 16:30:15 -0800117 if (cdev && cdev->gadget && cdev->gadget->speed != USB_SPEED_UNKNOWN) {
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400118 spin_unlock_irqrestore(&cdev->lock, flags);
119
120 usb_gadget_disconnect(cdev->gadget);
121 msleep(10);
122 usb_gadget_connect(cdev->gadget);
123 } else {
124 spin_unlock_irqrestore(&cdev->lock, flags);
125 }
126}
127
David Brownell40982be2008-06-19 17:52:58 -0700128/**
129 * usb_add_function() - add a function to a configuration
130 * @config: the configuration
131 * @function: the function being added
132 * Context: single threaded during gadget setup
133 *
134 * After initialization, each configuration must have one or more
135 * functions added to it. Adding a function involves calling its @bind()
136 * method to allocate resources such as interface and string identifiers
137 * and endpoints.
138 *
139 * This function returns the value of the function's bind(), which is
140 * zero for success else a negative errno value.
141 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200142int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700143 struct usb_function *function)
144{
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500145 struct usb_composite_dev *cdev = config->cdev;
David Brownell40982be2008-06-19 17:52:58 -0700146 int value = -EINVAL;
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500147 int index;
David Brownell40982be2008-06-19 17:52:58 -0700148
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500149 DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -0700150 function->name, function,
151 config->label, config);
152
153 if (!function->set_alt || !function->disable)
154 goto done;
155
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500156 index = atomic_inc_return(&cdev->driver->function_count);
157 function->dev = device_create(cdev->driver->class, NULL,
158 MKDEV(0, index), NULL, function->name);
159 if (IS_ERR(function->dev))
160 return PTR_ERR(function->dev);
161
162 value = device_create_file(function->dev, &dev_attr_enable);
163 if (value < 0) {
164 device_destroy(cdev->driver->class, MKDEV(0, index));
165 return value;
166 }
167 dev_set_drvdata(function->dev, function);
168
David Brownell40982be2008-06-19 17:52:58 -0700169 function->config = config;
170 list_add_tail(&function->list, &config->functions);
171
172 /* REVISIT *require* function->bind? */
173 if (function->bind) {
174 value = function->bind(config, function);
175 if (value < 0) {
176 list_del(&function->list);
177 function->config = NULL;
178 }
179 } else
180 value = 0;
181
182 /* We allow configurations that don't work at both speeds.
183 * If we run into a lowspeed Linux system, treat it the same
184 * as full speed ... it's the function drivers that will need
185 * to avoid bulk and ISO transfers.
186 */
187 if (!config->fullspeed && function->descriptors)
188 config->fullspeed = true;
189 if (!config->highspeed && function->hs_descriptors)
190 config->highspeed = true;
191
192done:
193 if (value)
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500194 DBG(cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700195 function->name, function, value);
196 return value;
197}
198
199/**
David Brownell60beed92008-08-18 17:38:22 -0700200 * usb_function_deactivate - prevent function and gadget enumeration
201 * @function: the function that isn't yet ready to respond
202 *
203 * Blocks response of the gadget driver to host enumeration by
204 * preventing the data line pullup from being activated. This is
205 * normally called during @bind() processing to change from the
206 * initial "ready to respond" state, or when a required resource
207 * becomes available.
208 *
209 * For example, drivers that serve as a passthrough to a userspace
210 * daemon can block enumeration unless that daemon (such as an OBEX,
211 * MTP, or print server) is ready to handle host requests.
212 *
213 * Not all systems support software control of their USB peripheral
214 * data pullups.
215 *
216 * Returns zero on success, else negative errno.
217 */
218int usb_function_deactivate(struct usb_function *function)
219{
220 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200221 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700222 int status = 0;
223
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200224 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700225
226 if (cdev->deactivations == 0)
227 status = usb_gadget_disconnect(cdev->gadget);
228 if (status == 0)
229 cdev->deactivations++;
230
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200231 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700232 return status;
233}
234
235/**
236 * usb_function_activate - allow function and gadget enumeration
237 * @function: function on which usb_function_activate() was called
238 *
239 * Reverses effect of usb_function_deactivate(). If no more functions
240 * are delaying their activation, the gadget driver will respond to
241 * host enumeration procedures.
242 *
243 * Returns zero on success, else negative errno.
244 */
245int usb_function_activate(struct usb_function *function)
246{
247 struct usb_composite_dev *cdev = function->config->cdev;
248 int status = 0;
249
250 spin_lock(&cdev->lock);
251
252 if (WARN_ON(cdev->deactivations == 0))
253 status = -EINVAL;
254 else {
255 cdev->deactivations--;
256 if (cdev->deactivations == 0)
257 status = usb_gadget_connect(cdev->gadget);
258 }
259
260 spin_unlock(&cdev->lock);
261 return status;
262}
263
264/**
David Brownell40982be2008-06-19 17:52:58 -0700265 * usb_interface_id() - allocate an unused interface ID
266 * @config: configuration associated with the interface
267 * @function: function handling the interface
268 * Context: single threaded during gadget setup
269 *
270 * usb_interface_id() is called from usb_function.bind() callbacks to
271 * allocate new interface IDs. The function driver will then store that
272 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300273 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700274 * particularly changing its altsetting via set_alt(). There may
275 * also be class-specific or vendor-specific requests to handle.
276 *
277 * All interface identifier should be allocated using this routine, to
278 * ensure that for example different functions don't wrongly assign
279 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300280 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700281 * one configuration (or more than once in a given configuration) need
282 * multiple versions of the relevant descriptors.
283 *
284 * Returns the interface ID which was allocated; or -ENODEV if no
285 * more interface IDs can be allocated.
286 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200287int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700288 struct usb_function *function)
289{
290 unsigned id = config->next_interface_id;
291
292 if (id < MAX_CONFIG_INTERFACES) {
293 config->interface[id] = function;
294 config->next_interface_id = id + 1;
295 return id;
296 }
297 return -ENODEV;
298}
299
300static int config_buf(struct usb_configuration *config,
301 enum usb_device_speed speed, void *buf, u8 type)
302{
303 struct usb_config_descriptor *c = buf;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500304 struct usb_interface_descriptor *intf;
John Michelauf71a3942010-12-10 12:09:53 -0600305 struct usb_interface_assoc_descriptor *iad = NULL;
David Brownell40982be2008-06-19 17:52:58 -0700306 void *next = buf + USB_DT_CONFIG_SIZE;
307 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
308 struct usb_function *f;
309 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500310 int interfaceCount = 0;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500311 u8 *dest;
David Brownell40982be2008-06-19 17:52:58 -0700312
313 /* write the config descriptor */
314 c = buf;
315 c->bLength = USB_DT_CONFIG_SIZE;
316 c->bDescriptorType = type;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500317 /* wTotalLength and bNumInterfaces are written later */
David Brownell40982be2008-06-19 17:52:58 -0700318 c->bConfigurationValue = config->bConfigurationValue;
319 c->iConfiguration = config->iConfiguration;
320 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700321 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700322
323 /* There may be e.g. OTG descriptors */
324 if (config->descriptors) {
325 status = usb_descriptor_fillbuf(next, len,
326 config->descriptors);
327 if (status < 0)
328 return status;
329 len -= status;
330 next += status;
331 }
332
333 /* add each function's descriptors */
334 list_for_each_entry(f, &config->functions, list) {
335 struct usb_descriptor_header **descriptors;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500336 struct usb_descriptor_header *descriptor;
David Brownell40982be2008-06-19 17:52:58 -0700337
338 if (speed == USB_SPEED_HIGH)
339 descriptors = f->hs_descriptors;
340 else
341 descriptors = f->descriptors;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400342 if (f->disabled || !descriptors || descriptors[0] == NULL)
David Brownell40982be2008-06-19 17:52:58 -0700343 continue;
344 status = usb_descriptor_fillbuf(next, len,
345 (const struct usb_descriptor_header **) descriptors);
346 if (status < 0)
347 return status;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500348
349 /* set interface numbers dynamically */
350 dest = next;
351 while ((descriptor = *descriptors++) != NULL) {
352 intf = (struct usb_interface_descriptor *)dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500353 if (intf->bDescriptorType == USB_DT_INTERFACE) {
354 /* don't increment bInterfaceNumber for alternate settings */
355 if (intf->bAlternateSetting == 0)
356 intf->bInterfaceNumber = interfaceCount++;
357 else
358 intf->bInterfaceNumber = interfaceCount - 1;
John Michelauf71a3942010-12-10 12:09:53 -0600359 if (iad) {
360 iad->bFirstInterface =
361 intf->bInterfaceNumber;
362 iad = NULL;
363 }
364 } else if (intf->bDescriptorType ==
365 USB_DT_INTERFACE_ASSOCIATION) {
366 /* This will be first if it exists. Save
367 * a pointer to it so we can properly set
368 * bFirstInterface when we process the first
369 * interface.
370 */
371 iad = (struct usb_interface_assoc_descriptor *)
372 dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500373 }
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500374 dest += intf->bLength;
375 }
376
David Brownell40982be2008-06-19 17:52:58 -0700377 len -= status;
378 next += status;
379 }
380
381 len = next - buf;
382 c->wTotalLength = cpu_to_le16(len);
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500383 c->bNumInterfaces = interfaceCount;
David Brownell40982be2008-06-19 17:52:58 -0700384 return len;
385}
386
387static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
388{
389 struct usb_gadget *gadget = cdev->gadget;
390 struct usb_configuration *c;
391 u8 type = w_value >> 8;
392 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
393
394 if (gadget_is_dualspeed(gadget)) {
395 int hs = 0;
396
397 if (gadget->speed == USB_SPEED_HIGH)
398 hs = 1;
399 if (type == USB_DT_OTHER_SPEED_CONFIG)
400 hs = !hs;
401 if (hs)
402 speed = USB_SPEED_HIGH;
403
404 }
405
406 /* This is a lookup by config *INDEX* */
407 w_value &= 0xff;
408 list_for_each_entry(c, &cdev->configs, list) {
409 /* ignore configs that won't work at this speed */
410 if (speed == USB_SPEED_HIGH) {
411 if (!c->highspeed)
412 continue;
413 } else {
414 if (!c->fullspeed)
415 continue;
416 }
417 if (w_value == 0)
418 return config_buf(c, speed, cdev->req->buf, type);
419 w_value--;
420 }
421 return -EINVAL;
422}
423
424static int count_configs(struct usb_composite_dev *cdev, unsigned type)
425{
426 struct usb_gadget *gadget = cdev->gadget;
427 struct usb_configuration *c;
428 unsigned count = 0;
429 int hs = 0;
430
431 if (gadget_is_dualspeed(gadget)) {
432 if (gadget->speed == USB_SPEED_HIGH)
433 hs = 1;
434 if (type == USB_DT_DEVICE_QUALIFIER)
435 hs = !hs;
436 }
437 list_for_each_entry(c, &cdev->configs, list) {
438 /* ignore configs that won't work at this speed */
439 if (hs) {
440 if (!c->highspeed)
441 continue;
442 } else {
443 if (!c->fullspeed)
444 continue;
445 }
446 count++;
447 }
448 return count;
449}
450
451static void device_qual(struct usb_composite_dev *cdev)
452{
453 struct usb_qualifier_descriptor *qual = cdev->req->buf;
454
455 qual->bLength = sizeof(*qual);
456 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
457 /* POLICY: same bcdUSB and device type info at both speeds */
458 qual->bcdUSB = cdev->desc.bcdUSB;
459 qual->bDeviceClass = cdev->desc.bDeviceClass;
460 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
461 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
462 /* ASSUME same EP0 fifo size at both speeds */
463 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
464 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700465 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700466}
467
468/*-------------------------------------------------------------------------*/
469
470static void reset_config(struct usb_composite_dev *cdev)
471{
472 struct usb_function *f;
473
474 DBG(cdev, "reset config\n");
475
476 list_for_each_entry(f, &cdev->config->functions, list) {
477 if (f->disable)
478 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200479
480 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700481 }
482 cdev->config = NULL;
483}
484
485static int set_config(struct usb_composite_dev *cdev,
486 const struct usb_ctrlrequest *ctrl, unsigned number)
487{
488 struct usb_gadget *gadget = cdev->gadget;
489 struct usb_configuration *c = NULL;
490 int result = -EINVAL;
491 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
492 int tmp;
493
494 if (cdev->config)
495 reset_config(cdev);
496
497 if (number) {
498 list_for_each_entry(c, &cdev->configs, list) {
499 if (c->bConfigurationValue == number) {
500 result = 0;
501 break;
502 }
503 }
504 if (result < 0)
505 goto done;
506 } else
507 result = 0;
508
509 INFO(cdev, "%s speed config #%d: %s\n",
510 ({ char *speed;
511 switch (gadget->speed) {
512 case USB_SPEED_LOW: speed = "low"; break;
513 case USB_SPEED_FULL: speed = "full"; break;
514 case USB_SPEED_HIGH: speed = "high"; break;
515 default: speed = "?"; break;
516 } ; speed; }), number, c ? c->label : "unconfigured");
517
518 if (!c)
519 goto done;
520
521 cdev->config = c;
522
523 /* Initialize all interfaces by setting them to altsetting zero. */
524 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
525 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200526 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700527
528 if (!f)
529 break;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400530 if (f->disabled)
Mike Lockwood8c6a6b22010-02-26 09:30:01 -0500531 continue;
David Brownell40982be2008-06-19 17:52:58 -0700532
Laurent Pinchart52426582009-10-21 00:03:38 +0200533 /*
534 * Record which endpoints are used by the function. This is used
535 * to dispatch control requests targeted at that endpoint to the
536 * function's setup callback instead of the current
537 * configuration's setup callback.
538 */
539 if (gadget->speed == USB_SPEED_HIGH)
540 descriptors = f->hs_descriptors;
541 else
542 descriptors = f->descriptors;
543
544 for (; *descriptors; ++descriptors) {
545 struct usb_endpoint_descriptor *ep;
546 int addr;
547
548 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
549 continue;
550
551 ep = (struct usb_endpoint_descriptor *)*descriptors;
552 addr = ((ep->bEndpointAddress & 0x80) >> 3)
553 | (ep->bEndpointAddress & 0x0f);
554 set_bit(addr, f->endpoints);
555 }
556
David Brownell40982be2008-06-19 17:52:58 -0700557 result = f->set_alt(f, tmp, 0);
558 if (result < 0) {
559 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
560 tmp, f->name, f, result);
561
562 reset_config(cdev);
563 goto done;
564 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300565
566 if (result == USB_GADGET_DELAYED_STATUS) {
567 DBG(cdev,
568 "%s: interface %d (%s) requested delayed status\n",
569 __func__, tmp, f->name);
570 cdev->delayed_status++;
571 DBG(cdev, "delayed_status count %d\n",
572 cdev->delayed_status);
573 }
David Brownell40982be2008-06-19 17:52:58 -0700574 }
575
576 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700577 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700578done:
579 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400580
Mike Lockwood8da4cc82010-06-27 20:05:55 -0400581 schedule_work(&cdev->switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400582
Roger Quadros1b9ba002011-05-09 13:08:06 +0300583 if (result >= 0 && cdev->delayed_status)
584 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700585 return result;
586}
587
588/**
589 * usb_add_config() - add a configuration to a device.
590 * @cdev: wraps the USB gadget
591 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200592 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700593 * Context: single threaded during gadget setup
594 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200595 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700596 * add each of the configurations it supports, using this routine.
597 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200598 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700599 * is zero for success else a negative errno value. Binding configurations
600 * assigns global resources including string IDs, and per-configuration
601 * resources such as interface IDs and endpoints.
602 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200603int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200604 struct usb_configuration *config,
605 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700606{
607 int status = -EINVAL;
608 struct usb_configuration *c;
609
610 DBG(cdev, "adding config #%u '%s'/%p\n",
611 config->bConfigurationValue,
612 config->label, config);
613
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200614 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700615 goto done;
616
617 /* Prevent duplicate configuration identifiers */
618 list_for_each_entry(c, &cdev->configs, list) {
619 if (c->bConfigurationValue == config->bConfigurationValue) {
620 status = -EBUSY;
621 goto done;
622 }
623 }
624
625 config->cdev = cdev;
626 list_add_tail(&config->list, &cdev->configs);
627
628 INIT_LIST_HEAD(&config->functions);
629 config->next_interface_id = 0;
630
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200631 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700632 if (status < 0) {
633 list_del(&config->list);
634 config->cdev = NULL;
635 } else {
636 unsigned i;
637
638 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
639 config->bConfigurationValue, config,
640 config->highspeed ? " high" : "",
641 config->fullspeed
642 ? (gadget_is_dualspeed(cdev->gadget)
643 ? " full"
644 : " full/low")
645 : "");
646
647 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
648 struct usb_function *f = config->interface[i];
649
650 if (!f)
651 continue;
652 DBG(cdev, " interface %d = %s/%p\n",
653 i, f->name, f);
654 }
655 }
656
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200657 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700658 * ep->driver_data as needed.
659 */
660 usb_ep_autoconfig_reset(cdev->gadget);
661
662done:
663 if (status)
664 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
665 config->bConfigurationValue, status);
666 return status;
667}
668
669/*-------------------------------------------------------------------------*/
670
671/* We support strings in multiple languages ... string descriptor zero
672 * says which languages are supported. The typical case will be that
673 * only one language (probably English) is used, with I18N handled on
674 * the host side.
675 */
676
677static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
678{
679 const struct usb_gadget_strings *s;
680 u16 language;
681 __le16 *tmp;
682
683 while (*sp) {
684 s = *sp;
685 language = cpu_to_le16(s->language);
686 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
687 if (*tmp == language)
688 goto repeat;
689 }
690 *tmp++ = language;
691repeat:
692 sp++;
693 }
694}
695
696static int lookup_string(
697 struct usb_gadget_strings **sp,
698 void *buf,
699 u16 language,
700 int id
701)
702{
703 struct usb_gadget_strings *s;
704 int value;
705
706 while (*sp) {
707 s = *sp++;
708 if (s->language != language)
709 continue;
710 value = usb_gadget_get_string(s, id, buf);
711 if (value > 0)
712 return value;
713 }
714 return -EINVAL;
715}
716
717static int get_string(struct usb_composite_dev *cdev,
718 void *buf, u16 language, int id)
719{
720 struct usb_configuration *c;
721 struct usb_function *f;
722 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200723 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700724
725 /* Yes, not only is USB's I18N support probably more than most
726 * folk will ever care about ... also, it's all supported here.
727 * (Except for UTF8 support for Unicode's "Astral Planes".)
728 */
729
730 /* 0 == report all available language codes */
731 if (id == 0) {
732 struct usb_string_descriptor *s = buf;
733 struct usb_gadget_strings **sp;
734
735 memset(s, 0, 256);
736 s->bDescriptorType = USB_DT_STRING;
737
738 sp = composite->strings;
739 if (sp)
740 collect_langs(sp, s->wData);
741
742 list_for_each_entry(c, &cdev->configs, list) {
743 sp = c->strings;
744 if (sp)
745 collect_langs(sp, s->wData);
746
747 list_for_each_entry(f, &c->functions, list) {
748 sp = f->strings;
749 if (sp)
750 collect_langs(sp, s->wData);
751 }
752 }
753
Roel Kluin417b57b2009-08-06 16:09:51 -0700754 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700755 continue;
756 if (!len)
757 return -EINVAL;
758
759 s->bLength = 2 * (len + 1);
760 return s->bLength;
761 }
762
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200763 /* Otherwise, look up and return a specified string. First
764 * check if the string has not been overridden.
765 */
766 if (cdev->manufacturer_override == id)
767 str = iManufacturer ?: composite->iManufacturer ?:
768 composite_manufacturer;
769 else if (cdev->product_override == id)
770 str = iProduct ?: composite->iProduct;
771 else if (cdev->serial_override == id)
772 str = iSerialNumber;
773 else
774 str = NULL;
775 if (str) {
776 struct usb_gadget_strings strings = {
777 .language = language,
778 .strings = &(struct usb_string) { 0xff, str }
779 };
780 return usb_gadget_get_string(&strings, 0xff, buf);
781 }
782
783 /* String IDs are device-scoped, so we look up each string
784 * table we're told about. These lookups are infrequent;
785 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700786 */
787 if (composite->strings) {
788 len = lookup_string(composite->strings, buf, language, id);
789 if (len > 0)
790 return len;
791 }
792 list_for_each_entry(c, &cdev->configs, list) {
793 if (c->strings) {
794 len = lookup_string(c->strings, buf, language, id);
795 if (len > 0)
796 return len;
797 }
798 list_for_each_entry(f, &c->functions, list) {
799 if (!f->strings)
800 continue;
801 len = lookup_string(f->strings, buf, language, id);
802 if (len > 0)
803 return len;
804 }
805 }
806 return -EINVAL;
807}
808
809/**
810 * usb_string_id() - allocate an unused string ID
811 * @cdev: the device whose string descriptor IDs are being allocated
812 * Context: single threaded during gadget setup
813 *
814 * @usb_string_id() is called from bind() callbacks to allocate
815 * string IDs. Drivers for functions, configurations, or gadgets will
816 * then store that ID in the appropriate descriptors and string table.
817 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200818 * All string identifier should be allocated using this,
819 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
820 * that for example different functions don't wrongly assign different
821 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700822 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200823int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700824{
825 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200826 /* string id 0 is reserved by USB spec for list of
827 * supported languages */
828 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700829 cdev->next_string_id++;
830 return cdev->next_string_id;
831 }
832 return -ENODEV;
833}
834
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200835/**
836 * usb_string_ids() - allocate unused string IDs in batch
837 * @cdev: the device whose string descriptor IDs are being allocated
838 * @str: an array of usb_string objects to assign numbers to
839 * Context: single threaded during gadget setup
840 *
841 * @usb_string_ids() is called from bind() callbacks to allocate
842 * string IDs. Drivers for functions, configurations, or gadgets will
843 * then copy IDs from the string table to the appropriate descriptors
844 * and string table for other languages.
845 *
846 * All string identifier should be allocated using this,
847 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
848 * example different functions don't wrongly assign different meanings
849 * to the same identifier.
850 */
851int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
852{
853 int next = cdev->next_string_id;
854
855 for (; str->s; ++str) {
856 if (unlikely(next >= 254))
857 return -ENODEV;
858 str->id = ++next;
859 }
860
861 cdev->next_string_id = next;
862
863 return 0;
864}
865
866/**
867 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700868 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200869 * @n: number of string IDs to allocate
870 * Context: single threaded during gadget setup
871 *
872 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700873 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200874 * is, returns last requested ID which is now very useful information.
875 *
876 * @usb_string_ids_n() is called from bind() callbacks to allocate
877 * string IDs. Drivers for functions, configurations, or gadgets will
878 * then store that ID in the appropriate descriptors and string table.
879 *
880 * All string identifier should be allocated using this,
881 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
882 * example different functions don't wrongly assign different meanings
883 * to the same identifier.
884 */
885int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
886{
887 unsigned next = c->next_string_id;
888 if (unlikely(n > 254 || (unsigned)next + n > 254))
889 return -ENODEV;
890 c->next_string_id += n;
891 return next + 1;
892}
893
894
David Brownell40982be2008-06-19 17:52:58 -0700895/*-------------------------------------------------------------------------*/
896
897static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
898{
899 if (req->status || req->actual != req->length)
900 DBG((struct usb_composite_dev *) ep->driver_data,
901 "setup complete --> %d, %d/%d\n",
902 req->status, req->actual, req->length);
903}
904
905/*
906 * The setup() callback implements all the ep0 functionality that's
907 * not handled lower down, in hardware or the hardware driver(like
908 * device and endpoint feature flags, and their status). It's all
909 * housekeeping for the gadget function we're implementing. Most of
910 * the work is in config and function specific setup.
911 */
912static int
913composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
914{
915 struct usb_composite_dev *cdev = get_gadget_data(gadget);
916 struct usb_request *req = cdev->req;
917 int value = -EOPNOTSUPP;
918 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800919 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700920 u16 w_value = le16_to_cpu(ctrl->wValue);
921 u16 w_length = le16_to_cpu(ctrl->wLength);
922 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200923 u8 endp;
Mike Lockwoode6be8942010-12-10 16:30:15 -0800924 unsigned long flags;
925
926 spin_lock_irqsave(&cdev->lock, flags);
927 if (!cdev->connected) {
928 cdev->connected = 1;
929 schedule_work(&cdev->switch_work);
930 }
931 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -0700932
933 /* partial re-init of the response message; the function or the
934 * gadget might need to intercept e.g. a control-OUT completion
935 * when we delegate to it.
936 */
937 req->zero = 0;
938 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530939 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700940 gadget->ep0->driver_data = cdev;
941
942 switch (ctrl->bRequest) {
943
944 /* we handle all standard USB descriptors */
945 case USB_REQ_GET_DESCRIPTOR:
946 if (ctrl->bRequestType != USB_DIR_IN)
947 goto unknown;
948 switch (w_value >> 8) {
949
950 case USB_DT_DEVICE:
951 cdev->desc.bNumConfigurations =
952 count_configs(cdev, USB_DT_DEVICE);
953 value = min(w_length, (u16) sizeof cdev->desc);
954 memcpy(req->buf, &cdev->desc, value);
955 break;
956 case USB_DT_DEVICE_QUALIFIER:
957 if (!gadget_is_dualspeed(gadget))
958 break;
959 device_qual(cdev);
960 value = min_t(int, w_length,
961 sizeof(struct usb_qualifier_descriptor));
962 break;
963 case USB_DT_OTHER_SPEED_CONFIG:
964 if (!gadget_is_dualspeed(gadget))
965 break;
966 /* FALLTHROUGH */
967 case USB_DT_CONFIG:
968 value = config_desc(cdev, w_value);
969 if (value >= 0)
970 value = min(w_length, (u16) value);
971 break;
972 case USB_DT_STRING:
973 value = get_string(cdev, req->buf,
974 w_index, w_value & 0xff);
Mike Lockwoodfb52b002010-04-16 15:32:15 -0400975
976 /* Allow functions to handle USB_DT_STRING.
977 * This is required for MTP.
978 */
979 if (value < 0) {
980 struct usb_configuration *cfg;
981 list_for_each_entry(cfg, &cdev->configs, list) {
982 if (cfg && cfg->setup) {
983 value = cfg->setup(cfg, ctrl);
984 if (value >= 0)
985 break;
986 }
987 }
988 }
989
David Brownell40982be2008-06-19 17:52:58 -0700990 if (value >= 0)
991 value = min(w_length, (u16) value);
992 break;
993 }
994 break;
995
996 /* any number of configs can work */
997 case USB_REQ_SET_CONFIGURATION:
998 if (ctrl->bRequestType != 0)
999 goto unknown;
1000 if (gadget_is_otg(gadget)) {
1001 if (gadget->a_hnp_support)
1002 DBG(cdev, "HNP available\n");
1003 else if (gadget->a_alt_hnp_support)
1004 DBG(cdev, "HNP on another port\n");
1005 else
1006 VDBG(cdev, "HNP inactive\n");
1007 }
1008 spin_lock(&cdev->lock);
1009 value = set_config(cdev, ctrl, w_value);
1010 spin_unlock(&cdev->lock);
1011 break;
1012 case USB_REQ_GET_CONFIGURATION:
1013 if (ctrl->bRequestType != USB_DIR_IN)
1014 goto unknown;
Jared Suttles258d1032009-08-07 18:57:49 -05001015 if (cdev->config) {
David Brownell40982be2008-06-19 17:52:58 -07001016 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Jared Suttles258d1032009-08-07 18:57:49 -05001017 value = min(w_length, (u16) 1);
1018 } else {
David Brownell40982be2008-06-19 17:52:58 -07001019 *(u8 *)req->buf = 0;
Jared Suttles258d1032009-08-07 18:57:49 -05001020 }
David Brownell40982be2008-06-19 17:52:58 -07001021 break;
1022
1023 /* function drivers must handle get/set altsetting; if there's
1024 * no get() method, we know only altsetting zero works.
1025 */
1026 case USB_REQ_SET_INTERFACE:
1027 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1028 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001029 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001030 break;
Bryan Wu08889512009-01-08 00:21:19 +08001031 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001032 if (!f)
1033 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001034 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001035 break;
1036 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001037 if (value == USB_GADGET_DELAYED_STATUS) {
1038 DBG(cdev,
1039 "%s: interface %d (%s) requested delayed status\n",
1040 __func__, intf, f->name);
1041 cdev->delayed_status++;
1042 DBG(cdev, "delayed_status count %d\n",
1043 cdev->delayed_status);
1044 }
David Brownell40982be2008-06-19 17:52:58 -07001045 break;
1046 case USB_REQ_GET_INTERFACE:
1047 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1048 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001049 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001050 break;
Bryan Wu08889512009-01-08 00:21:19 +08001051 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001052 if (!f)
1053 break;
1054 /* lots of interfaces only need altsetting zero... */
1055 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1056 if (value < 0)
1057 break;
1058 *((u8 *)req->buf) = value;
1059 value = min(w_length, (u16) 1);
1060 break;
1061 default:
1062unknown:
1063 VDBG(cdev,
1064 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1065 ctrl->bRequestType, ctrl->bRequest,
1066 w_value, w_index, w_length);
1067
Laurent Pinchart52426582009-10-21 00:03:38 +02001068 /* functions always handle their interfaces and endpoints...
1069 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001070 * configuration code.
1071 *
1072 * REVISIT it could make sense to let the composite device
1073 * take such requests too, if that's ever needed: to work
1074 * in config 0, etc.
1075 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001076 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1077 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001078 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301079 break;
1080 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001081 break;
1082
1083 case USB_RECIP_ENDPOINT:
1084 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1085 list_for_each_entry(f, &cdev->config->functions, list) {
1086 if (test_bit(endp, f->endpoints))
1087 break;
1088 }
1089 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001090 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001091 break;
David Brownell40982be2008-06-19 17:52:58 -07001092 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001093
1094 if (f && f->setup)
1095 value = f->setup(f, ctrl);
1096 else {
David Brownell40982be2008-06-19 17:52:58 -07001097 struct usb_configuration *c;
1098
1099 c = cdev->config;
1100 if (c && c->setup)
1101 value = c->setup(c, ctrl);
1102 }
1103
Joe Swantek292b1bc2009-12-15 07:17:40 -05001104 /* If the vendor request is not processed (value < 0),
1105 * call all device registered configure setup callbacks
1106 * to process it.
1107 * This is used to handle the following cases:
1108 * - vendor request is for the device and arrives before
1109 * setconfiguration.
1110 * - Some devices are required to handle vendor request before
1111 * setconfiguration such as MTP, USBNET.
1112 */
1113
1114 if (value < 0) {
1115 struct usb_configuration *cfg;
1116
1117 list_for_each_entry(cfg, &cdev->configs, list) {
1118 if (cfg && cfg->setup)
1119 value = cfg->setup(cfg, ctrl);
1120 }
1121 }
1122
David Brownell40982be2008-06-19 17:52:58 -07001123 goto done;
1124 }
1125
1126 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001127 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001128 req->length = value;
1129 req->zero = value < w_length;
1130 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1131 if (value < 0) {
1132 DBG(cdev, "ep_queue --> %d\n", value);
1133 req->status = 0;
1134 composite_setup_complete(gadget->ep0, req);
1135 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001136 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1137 WARN(cdev,
1138 "%s: Delayed status not supported for w_length != 0",
1139 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001140 }
1141
1142done:
1143 /* device either stalls (value < 0) or reports success */
1144 return value;
1145}
1146
1147static void composite_disconnect(struct usb_gadget *gadget)
1148{
1149 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1150 unsigned long flags;
1151
1152 /* REVISIT: should we have config and device level
1153 * disconnect callbacks?
1154 */
1155 spin_lock_irqsave(&cdev->lock, flags);
1156 if (cdev->config)
1157 reset_config(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001158
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001159 if (composite->disconnect)
1160 composite->disconnect(cdev);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001161
Mike Lockwoode6be8942010-12-10 16:30:15 -08001162 cdev->connected = 0;
1163 schedule_work(&cdev->switch_work);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001164 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001165}
1166
1167/*-------------------------------------------------------------------------*/
1168
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001169static ssize_t composite_show_suspended(struct device *dev,
1170 struct device_attribute *attr,
1171 char *buf)
1172{
1173 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1174 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1175
1176 return sprintf(buf, "%d\n", cdev->suspended);
1177}
1178
1179static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1180
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001181static void
David Brownell40982be2008-06-19 17:52:58 -07001182composite_unbind(struct usb_gadget *gadget)
1183{
1184 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1185
1186 /* composite_disconnect() must already have been called
1187 * by the underlying peripheral controller driver!
1188 * so there's no i/o concurrency that could affect the
1189 * state protected by cdev->lock.
1190 */
1191 WARN_ON(cdev->config);
1192
1193 while (!list_empty(&cdev->configs)) {
1194 struct usb_configuration *c;
1195
1196 c = list_first_entry(&cdev->configs,
1197 struct usb_configuration, list);
1198 while (!list_empty(&c->functions)) {
1199 struct usb_function *f;
1200
1201 f = list_first_entry(&c->functions,
1202 struct usb_function, list);
1203 list_del(&f->list);
1204 if (f->unbind) {
1205 DBG(cdev, "unbind function '%s'/%p\n",
1206 f->name, f);
1207 f->unbind(c, f);
1208 /* may free memory for "f" */
1209 }
1210 }
1211 list_del(&c->list);
1212 if (c->unbind) {
1213 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1214 c->unbind(c);
1215 /* may free memory for "c" */
1216 }
1217 }
1218 if (composite->unbind)
1219 composite->unbind(cdev);
1220
1221 if (cdev->req) {
1222 kfree(cdev->req->buf);
1223 usb_ep_free_request(gadget->ep0, cdev->req);
1224 }
Mike Lockwoode6be8942010-12-10 16:30:15 -08001225 switch_dev_unregister(&cdev->sw_connected);
1226 switch_dev_unregister(&cdev->sw_config);
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301227 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001228 kfree(cdev);
1229 set_gadget_data(gadget, NULL);
1230 composite = NULL;
1231}
1232
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001233static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001234{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001235 if (!*desc) {
1236 int ret = usb_string_id(cdev);
1237 if (unlikely(ret < 0))
1238 WARNING(cdev, "failed to override string ID\n");
1239 else
1240 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001241 }
David Brownell40982be2008-06-19 17:52:58 -07001242
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001243 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001244}
1245
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001246static void
1247composite_switch_work(struct work_struct *data)
1248{
1249 struct usb_composite_dev *cdev =
1250 container_of(data, struct usb_composite_dev, switch_work);
1251 struct usb_configuration *config = cdev->config;
Mike Lockwoode6be8942010-12-10 16:30:15 -08001252 int connected;
1253 unsigned long flags;
1254
1255 spin_lock_irqsave(&cdev->lock, flags);
1256 if (cdev->connected != cdev->sw_connected.state) {
1257 connected = cdev->connected;
1258 spin_unlock_irqrestore(&cdev->lock, flags);
1259 switch_set_state(&cdev->sw_connected, connected);
1260 } else {
1261 spin_unlock_irqrestore(&cdev->lock, flags);
1262 }
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001263
1264 if (config)
Mike Lockwoode6be8942010-12-10 16:30:15 -08001265 switch_set_state(&cdev->sw_config, config->bConfigurationValue);
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001266 else
Mike Lockwoode6be8942010-12-10 16:30:15 -08001267 switch_set_state(&cdev->sw_config, 0);
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001268}
1269
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001270static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001271{
1272 struct usb_composite_dev *cdev;
1273 int status = -ENOMEM;
1274
1275 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1276 if (!cdev)
1277 return status;
1278
1279 spin_lock_init(&cdev->lock);
1280 cdev->gadget = gadget;
1281 set_gadget_data(gadget, cdev);
1282 INIT_LIST_HEAD(&cdev->configs);
1283
1284 /* preallocate control response and buffer */
1285 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1286 if (!cdev->req)
1287 goto fail;
1288 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1289 if (!cdev->req->buf)
1290 goto fail;
1291 cdev->req->complete = composite_setup_complete;
1292 gadget->ep0->driver_data = cdev;
1293
1294 cdev->bufsiz = USB_BUFSIZ;
1295 cdev->driver = composite;
1296
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301297 /*
1298 * As per USB compliance update, a device that is actively drawing
1299 * more than 100mA from USB must report itself as bus-powered in
1300 * the GetStatus(DEVICE) call.
1301 */
1302 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1303 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001304
1305 /* interface and string IDs start at zero via kzalloc.
1306 * we force endpoints to start unassigned; few controller
1307 * drivers will zero ep->driver_data.
1308 */
1309 usb_ep_autoconfig_reset(cdev->gadget);
1310
1311 /* composite gadget needs to assign strings for whole device (like
1312 * serial number), register function drivers, potentially update
1313 * power state and consumption, etc
1314 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001315 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001316 if (status < 0)
1317 goto fail;
1318
Mike Lockwoode6be8942010-12-10 16:30:15 -08001319 cdev->sw_connected.name = "usb_connected";
1320 status = switch_dev_register(&cdev->sw_connected);
1321 if (status < 0)
1322 goto fail;
1323 cdev->sw_config.name = "usb_configuration";
1324 status = switch_dev_register(&cdev->sw_config);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001325 if (status < 0)
1326 goto fail;
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001327 INIT_WORK(&cdev->switch_work, composite_switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001328
David Brownell40982be2008-06-19 17:52:58 -07001329 cdev->desc = *composite->dev;
1330 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1331
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001332 /* standardized runtime overrides for device ID data */
1333 if (idVendor)
1334 cdev->desc.idVendor = cpu_to_le16(idVendor);
1335 if (idProduct)
1336 cdev->desc.idProduct = cpu_to_le16(idProduct);
1337 if (bcdDevice)
1338 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1339
Marek Belisko78bff3c2010-10-27 10:19:01 +02001340 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001341 if (iManufacturer || !cdev->desc.iManufacturer) {
1342 if (!iManufacturer && !composite->iManufacturer &&
1343 !*composite_manufacturer)
1344 snprintf(composite_manufacturer,
1345 sizeof composite_manufacturer,
1346 "%s %s with %s",
1347 init_utsname()->sysname,
1348 init_utsname()->release,
1349 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001350
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001351 cdev->manufacturer_override =
1352 override_id(cdev, &cdev->desc.iManufacturer);
1353 }
1354
1355 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1356 cdev->product_override =
1357 override_id(cdev, &cdev->desc.iProduct);
1358
1359 if (iSerialNumber)
1360 cdev->serial_override =
1361 override_id(cdev, &cdev->desc.iSerialNumber);
1362
1363 /* has userspace failed to provide a serial number? */
1364 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1365 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1366
1367 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001368 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1369 if (status)
1370 goto fail;
1371
David Brownell40982be2008-06-19 17:52:58 -07001372 INFO(cdev, "%s ready\n", composite->name);
1373 return 0;
1374
1375fail:
1376 composite_unbind(gadget);
1377 return status;
1378}
1379
1380/*-------------------------------------------------------------------------*/
1381
1382static void
1383composite_suspend(struct usb_gadget *gadget)
1384{
1385 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1386 struct usb_function *f;
1387
David Brownell89429392009-03-19 14:14:17 -07001388 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001389 * suspend/resume callbacks?
1390 */
1391 DBG(cdev, "suspend\n");
1392 if (cdev->config) {
1393 list_for_each_entry(f, &cdev->config->functions, list) {
1394 if (f->suspend)
1395 f->suspend(f);
1396 }
1397 }
David Brownell89429392009-03-19 14:14:17 -07001398 if (composite->suspend)
1399 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001400
1401 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001402
1403 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001404}
1405
1406static void
1407composite_resume(struct usb_gadget *gadget)
1408{
1409 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1410 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001411 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001412
David Brownell89429392009-03-19 14:14:17 -07001413 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001414 * suspend/resume callbacks?
1415 */
1416 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001417 if (composite->resume)
1418 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001419 if (cdev->config) {
1420 list_for_each_entry(f, &cdev->config->functions, list) {
1421 if (f->resume)
1422 f->resume(f);
1423 }
Hao Wub23f2f92010-11-29 15:17:03 +08001424
1425 maxpower = cdev->config->bMaxPower;
1426
1427 usb_gadget_vbus_draw(gadget, maxpower ?
1428 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001429 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001430
1431 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001432}
1433
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001434static int
1435composite_uevent(struct device *dev, struct kobj_uevent_env *env)
1436{
1437 struct usb_function *f = dev_get_drvdata(dev);
1438
1439 if (!f) {
1440 /* this happens when the device is first created */
1441 return 0;
1442 }
1443
1444 if (add_uevent_var(env, "FUNCTION=%s", f->name))
1445 return -ENOMEM;
1446 if (add_uevent_var(env, "ENABLED=%d", !f->disabled))
1447 return -ENOMEM;
1448 return 0;
1449}
1450
David Brownell40982be2008-06-19 17:52:58 -07001451/*-------------------------------------------------------------------------*/
1452
1453static struct usb_gadget_driver composite_driver = {
1454 .speed = USB_SPEED_HIGH,
1455
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001456 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001457
1458 .setup = composite_setup,
1459 .disconnect = composite_disconnect,
1460
1461 .suspend = composite_suspend,
1462 .resume = composite_resume,
1463
1464 .driver = {
1465 .owner = THIS_MODULE,
1466 },
1467};
1468
1469/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001470 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001471 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001472 * @bind: the callback used to allocate resources that are shared across the
1473 * whole device, such as string IDs, and add its configurations using
1474 * @usb_add_config(). This may fail by returning a negative errno
1475 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001476 * Context: single threaded during gadget setup
1477 *
1478 * This function is used to register drivers using the composite driver
1479 * framework. The return value is zero, or a negative errno value.
1480 * Those values normally come from the driver's @bind method, which does
1481 * all the work of setting up the driver to match the hardware.
1482 *
1483 * On successful return, the gadget is ready to respond to requests from
1484 * the host, unless one of its components invokes usb_gadget_disconnect()
1485 * while it was binding. That would usually be done in order to wait for
1486 * some userspace participation.
1487 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001488int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001489 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001490{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001491 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001492 return -EINVAL;
1493
1494 if (!driver->name)
1495 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001496 if (!driver->iProduct)
1497 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001498 composite_driver.function = (char *) driver->name;
1499 composite_driver.driver.name = driver->name;
1500 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001501 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001502
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001503 driver->class = class_create(THIS_MODULE, "usb_composite");
1504 if (IS_ERR(driver->class))
1505 return PTR_ERR(driver->class);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001506 driver->class->dev_uevent = composite_uevent;
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001507
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001508 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001509}
1510
1511/**
1512 * usb_composite_unregister() - unregister a composite driver
1513 * @driver: the driver to unregister
1514 *
1515 * This function is used to unregister drivers using the composite
1516 * driver framework.
1517 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001518void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001519{
1520 if (composite != driver)
1521 return;
1522 usb_gadget_unregister_driver(&composite_driver);
1523}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001524
1525/**
1526 * usb_composite_setup_continue() - Continue with the control transfer
1527 * @cdev: the composite device who's control transfer was kept waiting
1528 *
1529 * This function must be called by the USB function driver to continue
1530 * with the control transfer's data/status stage in case it had requested to
1531 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1532 * can request the composite framework to delay the setup request's data/status
1533 * stages by returning USB_GADGET_DELAYED_STATUS.
1534 */
1535void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1536{
1537 int value;
1538 struct usb_request *req = cdev->req;
1539 unsigned long flags;
1540
1541 DBG(cdev, "%s\n", __func__);
1542 spin_lock_irqsave(&cdev->lock, flags);
1543
1544 if (cdev->delayed_status == 0) {
1545 WARN(cdev, "%s: Unexpected call\n", __func__);
1546
1547 } else if (--cdev->delayed_status == 0) {
1548 DBG(cdev, "%s: Completing delayed status\n", __func__);
1549 req->length = 0;
1550 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1551 if (value < 0) {
1552 DBG(cdev, "ep_queue --> %d\n", value);
1553 req->status = 0;
1554 composite_setup_complete(cdev->gadget->ep0, req);
1555 }
1556 }
1557
1558 spin_unlock_irqrestore(&cdev->lock, flags);
1559}
1560