blob: c9ae8bba6962d1bd5eb6f0edd7733c8aaca27145 [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>
Vikram Pandita8abaec82011-02-07 21:20:50 -080028#include <linux/delay.h>
29#include <linux/kdev_t.h>
David Brownell40982be2008-06-19 17:52:58 -070030#include <linux/usb/composite.h>
31
32
33/*
34 * The code in this file is utility code, used to build a gadget driver
35 * from one or more "function" drivers, one or more "configuration"
36 * objects, and a "usb_composite_driver" by gluing them together along
37 * with the relevant device-wide data.
38 */
39
40/* big enough to hold our biggest descriptor */
Robert Lukassendd0543e2010-03-30 14:14:01 +020041#define USB_BUFSIZ 1024
David Brownell40982be2008-06-19 17:52:58 -070042
43static struct usb_composite_driver *composite;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +020044static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
David Brownell40982be2008-06-19 17:52:58 -070045
Lucas De Marchi25985ed2011-03-30 22:57:33 -030046/* Some systems will need runtime overrides for the product identifiers
David Brownell40982be2008-06-19 17:52:58 -070047 * published in the device descriptor, either numbers or strings or both.
48 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
49 */
50
51static ushort idVendor;
52module_param(idVendor, ushort, 0);
53MODULE_PARM_DESC(idVendor, "USB Vendor ID");
54
55static ushort idProduct;
56module_param(idProduct, ushort, 0);
57MODULE_PARM_DESC(idProduct, "USB Product ID");
58
59static ushort bcdDevice;
60module_param(bcdDevice, ushort, 0);
61MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
62
63static char *iManufacturer;
64module_param(iManufacturer, charp, 0);
65MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
66
67static char *iProduct;
68module_param(iProduct, charp, 0);
69MODULE_PARM_DESC(iProduct, "USB Product string");
70
71static char *iSerialNumber;
72module_param(iSerialNumber, charp, 0);
73MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
74
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020075static char composite_manufacturer[50];
76
David Brownell40982be2008-06-19 17:52:58 -070077/*-------------------------------------------------------------------------*/
78
Mike Lockwoodf041ac62010-02-06 21:53:51 -050079static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
80 char *buf)
81{
82 struct usb_function *f = dev_get_drvdata(dev);
Mike Lockwoode2dc5032010-06-23 08:20:59 -040083 return sprintf(buf, "%d\n", !f->disabled);
Mike Lockwoodf041ac62010-02-06 21:53:51 -050084}
85
86static ssize_t enable_store(
87 struct device *dev, struct device_attribute *attr,
88 const char *buf, size_t size)
89{
90 struct usb_function *f = dev_get_drvdata(dev);
91 struct usb_composite_driver *driver = f->config->cdev->driver;
92 int value;
93
94 sscanf(buf, "%d", &value);
95 if (driver->enable_function)
96 driver->enable_function(f, value);
97 else
Mike Lockwoode2dc5032010-06-23 08:20:59 -040098 usb_function_set_enabled(f, value);
Mike Lockwoodf041ac62010-02-06 21:53:51 -050099
100 return size;
101}
102
103static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
104
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400105void usb_function_set_enabled(struct usb_function *f, int enabled)
106{
107 f->disabled = !enabled;
108 kobject_uevent(&f->dev->kobj, KOBJ_CHANGE);
109}
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500110
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400111
112void usb_composite_force_reset(struct usb_composite_dev *cdev)
113{
114 unsigned long flags;
115
116 spin_lock_irqsave(&cdev->lock, flags);
117 /* force reenumeration */
Mike Lockwoode6be8942010-12-10 16:30:15 -0800118 if (cdev && cdev->gadget && cdev->gadget->speed != USB_SPEED_UNKNOWN) {
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400119 spin_unlock_irqrestore(&cdev->lock, flags);
120
121 usb_gadget_disconnect(cdev->gadget);
122 msleep(10);
123 usb_gadget_connect(cdev->gadget);
124 } else {
125 spin_unlock_irqrestore(&cdev->lock, flags);
126 }
127}
128
David Brownell40982be2008-06-19 17:52:58 -0700129/**
130 * usb_add_function() - add a function to a configuration
131 * @config: the configuration
132 * @function: the function being added
133 * Context: single threaded during gadget setup
134 *
135 * After initialization, each configuration must have one or more
136 * functions added to it. Adding a function involves calling its @bind()
137 * method to allocate resources such as interface and string identifiers
138 * and endpoints.
139 *
140 * This function returns the value of the function's bind(), which is
141 * zero for success else a negative errno value.
142 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200143int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700144 struct usb_function *function)
145{
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500146 struct usb_composite_dev *cdev = config->cdev;
David Brownell40982be2008-06-19 17:52:58 -0700147 int value = -EINVAL;
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500148 int index;
David Brownell40982be2008-06-19 17:52:58 -0700149
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500150 DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -0700151 function->name, function,
152 config->label, config);
153
154 if (!function->set_alt || !function->disable)
155 goto done;
156
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500157 index = atomic_inc_return(&cdev->driver->function_count);
158 function->dev = device_create(cdev->driver->class, NULL,
159 MKDEV(0, index), NULL, function->name);
160 if (IS_ERR(function->dev))
161 return PTR_ERR(function->dev);
162
163 value = device_create_file(function->dev, &dev_attr_enable);
164 if (value < 0) {
165 device_destroy(cdev->driver->class, MKDEV(0, index));
166 return value;
167 }
168 dev_set_drvdata(function->dev, function);
169
David Brownell40982be2008-06-19 17:52:58 -0700170 function->config = config;
171 list_add_tail(&function->list, &config->functions);
172
173 /* REVISIT *require* function->bind? */
174 if (function->bind) {
175 value = function->bind(config, function);
176 if (value < 0) {
177 list_del(&function->list);
178 function->config = NULL;
179 }
180 } else
181 value = 0;
182
183 /* We allow configurations that don't work at both speeds.
184 * If we run into a lowspeed Linux system, treat it the same
185 * as full speed ... it's the function drivers that will need
186 * to avoid bulk and ISO transfers.
187 */
188 if (!config->fullspeed && function->descriptors)
189 config->fullspeed = true;
190 if (!config->highspeed && function->hs_descriptors)
191 config->highspeed = true;
192
193done:
194 if (value)
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500195 DBG(cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700196 function->name, function, value);
197 return value;
198}
199
200/**
David Brownell60beed92008-08-18 17:38:22 -0700201 * usb_function_deactivate - prevent function and gadget enumeration
202 * @function: the function that isn't yet ready to respond
203 *
204 * Blocks response of the gadget driver to host enumeration by
205 * preventing the data line pullup from being activated. This is
206 * normally called during @bind() processing to change from the
207 * initial "ready to respond" state, or when a required resource
208 * becomes available.
209 *
210 * For example, drivers that serve as a passthrough to a userspace
211 * daemon can block enumeration unless that daemon (such as an OBEX,
212 * MTP, or print server) is ready to handle host requests.
213 *
214 * Not all systems support software control of their USB peripheral
215 * data pullups.
216 *
217 * Returns zero on success, else negative errno.
218 */
219int usb_function_deactivate(struct usb_function *function)
220{
221 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200222 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700223 int status = 0;
224
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200225 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700226
227 if (cdev->deactivations == 0)
228 status = usb_gadget_disconnect(cdev->gadget);
229 if (status == 0)
230 cdev->deactivations++;
231
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200232 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700233 return status;
234}
235
236/**
237 * usb_function_activate - allow function and gadget enumeration
238 * @function: function on which usb_function_activate() was called
239 *
240 * Reverses effect of usb_function_deactivate(). If no more functions
241 * are delaying their activation, the gadget driver will respond to
242 * host enumeration procedures.
243 *
244 * Returns zero on success, else negative errno.
245 */
246int usb_function_activate(struct usb_function *function)
247{
248 struct usb_composite_dev *cdev = function->config->cdev;
249 int status = 0;
250
251 spin_lock(&cdev->lock);
252
253 if (WARN_ON(cdev->deactivations == 0))
254 status = -EINVAL;
255 else {
256 cdev->deactivations--;
257 if (cdev->deactivations == 0)
258 status = usb_gadget_connect(cdev->gadget);
259 }
260
261 spin_unlock(&cdev->lock);
262 return status;
263}
264
265/**
David Brownell40982be2008-06-19 17:52:58 -0700266 * usb_interface_id() - allocate an unused interface ID
267 * @config: configuration associated with the interface
268 * @function: function handling the interface
269 * Context: single threaded during gadget setup
270 *
271 * usb_interface_id() is called from usb_function.bind() callbacks to
272 * allocate new interface IDs. The function driver will then store that
273 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300274 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700275 * particularly changing its altsetting via set_alt(). There may
276 * also be class-specific or vendor-specific requests to handle.
277 *
278 * All interface identifier should be allocated using this routine, to
279 * ensure that for example different functions don't wrongly assign
280 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300281 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700282 * one configuration (or more than once in a given configuration) need
283 * multiple versions of the relevant descriptors.
284 *
285 * Returns the interface ID which was allocated; or -ENODEV if no
286 * more interface IDs can be allocated.
287 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200288int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700289 struct usb_function *function)
290{
291 unsigned id = config->next_interface_id;
292
293 if (id < MAX_CONFIG_INTERFACES) {
294 config->interface[id] = function;
295 config->next_interface_id = id + 1;
296 return id;
297 }
298 return -ENODEV;
299}
300
301static int config_buf(struct usb_configuration *config,
302 enum usb_device_speed speed, void *buf, u8 type)
303{
304 struct usb_config_descriptor *c = buf;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500305 struct usb_interface_descriptor *intf;
John Michelauf71a3942010-12-10 12:09:53 -0600306 struct usb_interface_assoc_descriptor *iad = NULL;
David Brownell40982be2008-06-19 17:52:58 -0700307 void *next = buf + USB_DT_CONFIG_SIZE;
308 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
309 struct usb_function *f;
310 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500311 int interfaceCount = 0;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500312 u8 *dest;
David Brownell40982be2008-06-19 17:52:58 -0700313
314 /* write the config descriptor */
315 c = buf;
316 c->bLength = USB_DT_CONFIG_SIZE;
317 c->bDescriptorType = type;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500318 /* wTotalLength and bNumInterfaces are written later */
David Brownell40982be2008-06-19 17:52:58 -0700319 c->bConfigurationValue = config->bConfigurationValue;
320 c->iConfiguration = config->iConfiguration;
321 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700322 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700323
324 /* There may be e.g. OTG descriptors */
325 if (config->descriptors) {
326 status = usb_descriptor_fillbuf(next, len,
327 config->descriptors);
328 if (status < 0)
329 return status;
330 len -= status;
331 next += status;
332 }
333
334 /* add each function's descriptors */
335 list_for_each_entry(f, &config->functions, list) {
336 struct usb_descriptor_header **descriptors;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500337 struct usb_descriptor_header *descriptor;
David Brownell40982be2008-06-19 17:52:58 -0700338
339 if (speed == USB_SPEED_HIGH)
340 descriptors = f->hs_descriptors;
341 else
342 descriptors = f->descriptors;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400343 if (f->disabled || !descriptors || descriptors[0] == NULL)
David Brownell40982be2008-06-19 17:52:58 -0700344 continue;
345 status = usb_descriptor_fillbuf(next, len,
346 (const struct usb_descriptor_header **) descriptors);
347 if (status < 0)
348 return status;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500349
350 /* set interface numbers dynamically */
351 dest = next;
352 while ((descriptor = *descriptors++) != NULL) {
353 intf = (struct usb_interface_descriptor *)dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500354 if (intf->bDescriptorType == USB_DT_INTERFACE) {
355 /* don't increment bInterfaceNumber for alternate settings */
356 if (intf->bAlternateSetting == 0)
357 intf->bInterfaceNumber = interfaceCount++;
358 else
359 intf->bInterfaceNumber = interfaceCount - 1;
John Michelauf71a3942010-12-10 12:09:53 -0600360 if (iad) {
361 iad->bFirstInterface =
362 intf->bInterfaceNumber;
363 iad = NULL;
364 }
365 } else if (intf->bDescriptorType ==
366 USB_DT_INTERFACE_ASSOCIATION) {
367 /* This will be first if it exists. Save
368 * a pointer to it so we can properly set
369 * bFirstInterface when we process the first
370 * interface.
371 */
372 iad = (struct usb_interface_assoc_descriptor *)
373 dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500374 }
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500375 dest += intf->bLength;
376 }
377
David Brownell40982be2008-06-19 17:52:58 -0700378 len -= status;
379 next += status;
380 }
381
382 len = next - buf;
383 c->wTotalLength = cpu_to_le16(len);
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500384 c->bNumInterfaces = interfaceCount;
David Brownell40982be2008-06-19 17:52:58 -0700385 return len;
386}
387
388static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
389{
390 struct usb_gadget *gadget = cdev->gadget;
391 struct usb_configuration *c;
392 u8 type = w_value >> 8;
393 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
394
395 if (gadget_is_dualspeed(gadget)) {
396 int hs = 0;
397
398 if (gadget->speed == USB_SPEED_HIGH)
399 hs = 1;
400 if (type == USB_DT_OTHER_SPEED_CONFIG)
401 hs = !hs;
402 if (hs)
403 speed = USB_SPEED_HIGH;
404
405 }
406
407 /* This is a lookup by config *INDEX* */
408 w_value &= 0xff;
409 list_for_each_entry(c, &cdev->configs, list) {
410 /* ignore configs that won't work at this speed */
411 if (speed == USB_SPEED_HIGH) {
412 if (!c->highspeed)
413 continue;
414 } else {
415 if (!c->fullspeed)
416 continue;
417 }
418 if (w_value == 0)
419 return config_buf(c, speed, cdev->req->buf, type);
420 w_value--;
421 }
422 return -EINVAL;
423}
424
425static int count_configs(struct usb_composite_dev *cdev, unsigned type)
426{
427 struct usb_gadget *gadget = cdev->gadget;
428 struct usb_configuration *c;
429 unsigned count = 0;
430 int hs = 0;
431
432 if (gadget_is_dualspeed(gadget)) {
433 if (gadget->speed == USB_SPEED_HIGH)
434 hs = 1;
435 if (type == USB_DT_DEVICE_QUALIFIER)
436 hs = !hs;
437 }
438 list_for_each_entry(c, &cdev->configs, list) {
439 /* ignore configs that won't work at this speed */
440 if (hs) {
441 if (!c->highspeed)
442 continue;
443 } else {
444 if (!c->fullspeed)
445 continue;
446 }
447 count++;
448 }
449 return count;
450}
451
452static void device_qual(struct usb_composite_dev *cdev)
453{
454 struct usb_qualifier_descriptor *qual = cdev->req->buf;
455
456 qual->bLength = sizeof(*qual);
457 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
458 /* POLICY: same bcdUSB and device type info at both speeds */
459 qual->bcdUSB = cdev->desc.bcdUSB;
460 qual->bDeviceClass = cdev->desc.bDeviceClass;
461 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
462 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
463 /* ASSUME same EP0 fifo size at both speeds */
464 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
465 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700466 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700467}
468
469/*-------------------------------------------------------------------------*/
470
471static void reset_config(struct usb_composite_dev *cdev)
472{
473 struct usb_function *f;
474
475 DBG(cdev, "reset config\n");
476
477 list_for_each_entry(f, &cdev->config->functions, list) {
478 if (f->disable)
479 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200480
481 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700482 }
483 cdev->config = NULL;
484}
485
486static int set_config(struct usb_composite_dev *cdev,
487 const struct usb_ctrlrequest *ctrl, unsigned number)
488{
489 struct usb_gadget *gadget = cdev->gadget;
490 struct usb_configuration *c = NULL;
491 int result = -EINVAL;
492 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
493 int tmp;
494
495 if (cdev->config)
496 reset_config(cdev);
497
498 if (number) {
499 list_for_each_entry(c, &cdev->configs, list) {
500 if (c->bConfigurationValue == number) {
501 result = 0;
502 break;
503 }
504 }
505 if (result < 0)
506 goto done;
507 } else
508 result = 0;
509
510 INFO(cdev, "%s speed config #%d: %s\n",
511 ({ char *speed;
512 switch (gadget->speed) {
513 case USB_SPEED_LOW: speed = "low"; break;
514 case USB_SPEED_FULL: speed = "full"; break;
515 case USB_SPEED_HIGH: speed = "high"; break;
516 default: speed = "?"; break;
517 } ; speed; }), number, c ? c->label : "unconfigured");
518
519 if (!c)
520 goto done;
521
522 cdev->config = c;
523
524 /* Initialize all interfaces by setting them to altsetting zero. */
525 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
526 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200527 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700528
529 if (!f)
530 break;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400531 if (f->disabled)
Mike Lockwood8c6a6b22010-02-26 09:30:01 -0500532 continue;
David Brownell40982be2008-06-19 17:52:58 -0700533
Laurent Pinchart52426582009-10-21 00:03:38 +0200534 /*
535 * Record which endpoints are used by the function. This is used
536 * to dispatch control requests targeted at that endpoint to the
537 * function's setup callback instead of the current
538 * configuration's setup callback.
539 */
540 if (gadget->speed == USB_SPEED_HIGH)
541 descriptors = f->hs_descriptors;
542 else
543 descriptors = f->descriptors;
544
545 for (; *descriptors; ++descriptors) {
546 struct usb_endpoint_descriptor *ep;
547 int addr;
548
549 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
550 continue;
551
552 ep = (struct usb_endpoint_descriptor *)*descriptors;
553 addr = ((ep->bEndpointAddress & 0x80) >> 3)
554 | (ep->bEndpointAddress & 0x0f);
555 set_bit(addr, f->endpoints);
556 }
557
David Brownell40982be2008-06-19 17:52:58 -0700558 result = f->set_alt(f, tmp, 0);
559 if (result < 0) {
560 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
561 tmp, f->name, f, result);
562
563 reset_config(cdev);
564 goto done;
565 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300566
567 if (result == USB_GADGET_DELAYED_STATUS) {
568 DBG(cdev,
569 "%s: interface %d (%s) requested delayed status\n",
570 __func__, tmp, f->name);
571 cdev->delayed_status++;
572 DBG(cdev, "delayed_status count %d\n",
573 cdev->delayed_status);
574 }
David Brownell40982be2008-06-19 17:52:58 -0700575 }
576
577 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700578 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700579done:
580 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400581
Mike Lockwood8da4cc82010-06-27 20:05:55 -0400582 schedule_work(&cdev->switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400583
Roger Quadros1b9ba002011-05-09 13:08:06 +0300584 if (result >= 0 && cdev->delayed_status)
585 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700586 return result;
587}
588
589/**
590 * usb_add_config() - add a configuration to a device.
591 * @cdev: wraps the USB gadget
592 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200593 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700594 * Context: single threaded during gadget setup
595 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200596 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700597 * add each of the configurations it supports, using this routine.
598 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200599 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700600 * is zero for success else a negative errno value. Binding configurations
601 * assigns global resources including string IDs, and per-configuration
602 * resources such as interface IDs and endpoints.
603 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200604int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200605 struct usb_configuration *config,
606 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700607{
608 int status = -EINVAL;
609 struct usb_configuration *c;
610
611 DBG(cdev, "adding config #%u '%s'/%p\n",
612 config->bConfigurationValue,
613 config->label, config);
614
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200615 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700616 goto done;
617
618 /* Prevent duplicate configuration identifiers */
619 list_for_each_entry(c, &cdev->configs, list) {
620 if (c->bConfigurationValue == config->bConfigurationValue) {
621 status = -EBUSY;
622 goto done;
623 }
624 }
625
626 config->cdev = cdev;
627 list_add_tail(&config->list, &cdev->configs);
628
629 INIT_LIST_HEAD(&config->functions);
630 config->next_interface_id = 0;
631
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200632 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700633 if (status < 0) {
634 list_del(&config->list);
635 config->cdev = NULL;
636 } else {
637 unsigned i;
638
639 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
640 config->bConfigurationValue, config,
641 config->highspeed ? " high" : "",
642 config->fullspeed
643 ? (gadget_is_dualspeed(cdev->gadget)
644 ? " full"
645 : " full/low")
646 : "");
647
648 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
649 struct usb_function *f = config->interface[i];
650
651 if (!f)
652 continue;
653 DBG(cdev, " interface %d = %s/%p\n",
654 i, f->name, f);
655 }
656 }
657
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200658 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700659 * ep->driver_data as needed.
660 */
661 usb_ep_autoconfig_reset(cdev->gadget);
662
663done:
664 if (status)
665 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
666 config->bConfigurationValue, status);
667 return status;
668}
669
670/*-------------------------------------------------------------------------*/
671
672/* We support strings in multiple languages ... string descriptor zero
673 * says which languages are supported. The typical case will be that
674 * only one language (probably English) is used, with I18N handled on
675 * the host side.
676 */
677
678static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
679{
680 const struct usb_gadget_strings *s;
681 u16 language;
682 __le16 *tmp;
683
684 while (*sp) {
685 s = *sp;
686 language = cpu_to_le16(s->language);
687 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
688 if (*tmp == language)
689 goto repeat;
690 }
691 *tmp++ = language;
692repeat:
693 sp++;
694 }
695}
696
697static int lookup_string(
698 struct usb_gadget_strings **sp,
699 void *buf,
700 u16 language,
701 int id
702)
703{
704 struct usb_gadget_strings *s;
705 int value;
706
707 while (*sp) {
708 s = *sp++;
709 if (s->language != language)
710 continue;
711 value = usb_gadget_get_string(s, id, buf);
712 if (value > 0)
713 return value;
714 }
715 return -EINVAL;
716}
717
718static int get_string(struct usb_composite_dev *cdev,
719 void *buf, u16 language, int id)
720{
721 struct usb_configuration *c;
722 struct usb_function *f;
723 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200724 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700725
726 /* Yes, not only is USB's I18N support probably more than most
727 * folk will ever care about ... also, it's all supported here.
728 * (Except for UTF8 support for Unicode's "Astral Planes".)
729 */
730
731 /* 0 == report all available language codes */
732 if (id == 0) {
733 struct usb_string_descriptor *s = buf;
734 struct usb_gadget_strings **sp;
735
736 memset(s, 0, 256);
737 s->bDescriptorType = USB_DT_STRING;
738
739 sp = composite->strings;
740 if (sp)
741 collect_langs(sp, s->wData);
742
743 list_for_each_entry(c, &cdev->configs, list) {
744 sp = c->strings;
745 if (sp)
746 collect_langs(sp, s->wData);
747
748 list_for_each_entry(f, &c->functions, list) {
749 sp = f->strings;
750 if (sp)
751 collect_langs(sp, s->wData);
752 }
753 }
754
Roel Kluin417b57b2009-08-06 16:09:51 -0700755 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700756 continue;
757 if (!len)
758 return -EINVAL;
759
760 s->bLength = 2 * (len + 1);
761 return s->bLength;
762 }
763
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200764 /* Otherwise, look up and return a specified string. First
765 * check if the string has not been overridden.
766 */
767 if (cdev->manufacturer_override == id)
768 str = iManufacturer ?: composite->iManufacturer ?:
769 composite_manufacturer;
770 else if (cdev->product_override == id)
771 str = iProduct ?: composite->iProduct;
772 else if (cdev->serial_override == id)
773 str = iSerialNumber;
774 else
775 str = NULL;
776 if (str) {
777 struct usb_gadget_strings strings = {
778 .language = language,
779 .strings = &(struct usb_string) { 0xff, str }
780 };
781 return usb_gadget_get_string(&strings, 0xff, buf);
782 }
783
784 /* String IDs are device-scoped, so we look up each string
785 * table we're told about. These lookups are infrequent;
786 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700787 */
788 if (composite->strings) {
789 len = lookup_string(composite->strings, buf, language, id);
790 if (len > 0)
791 return len;
792 }
793 list_for_each_entry(c, &cdev->configs, list) {
794 if (c->strings) {
795 len = lookup_string(c->strings, buf, language, id);
796 if (len > 0)
797 return len;
798 }
799 list_for_each_entry(f, &c->functions, list) {
800 if (!f->strings)
801 continue;
802 len = lookup_string(f->strings, buf, language, id);
803 if (len > 0)
804 return len;
805 }
806 }
807 return -EINVAL;
808}
809
810/**
811 * usb_string_id() - allocate an unused string ID
812 * @cdev: the device whose string descriptor IDs are being allocated
813 * Context: single threaded during gadget setup
814 *
815 * @usb_string_id() is called from bind() callbacks to allocate
816 * string IDs. Drivers for functions, configurations, or gadgets will
817 * then store that ID in the appropriate descriptors and string table.
818 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200819 * All string identifier should be allocated using this,
820 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
821 * that for example different functions don't wrongly assign different
822 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700823 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200824int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700825{
826 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200827 /* string id 0 is reserved by USB spec for list of
828 * supported languages */
829 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700830 cdev->next_string_id++;
831 return cdev->next_string_id;
832 }
833 return -ENODEV;
834}
835
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200836/**
837 * usb_string_ids() - allocate unused string IDs in batch
838 * @cdev: the device whose string descriptor IDs are being allocated
839 * @str: an array of usb_string objects to assign numbers to
840 * Context: single threaded during gadget setup
841 *
842 * @usb_string_ids() is called from bind() callbacks to allocate
843 * string IDs. Drivers for functions, configurations, or gadgets will
844 * then copy IDs from the string table to the appropriate descriptors
845 * and string table for other languages.
846 *
847 * All string identifier should be allocated using this,
848 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
849 * example different functions don't wrongly assign different meanings
850 * to the same identifier.
851 */
852int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
853{
854 int next = cdev->next_string_id;
855
856 for (; str->s; ++str) {
857 if (unlikely(next >= 254))
858 return -ENODEV;
859 str->id = ++next;
860 }
861
862 cdev->next_string_id = next;
863
864 return 0;
865}
866
867/**
868 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700869 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200870 * @n: number of string IDs to allocate
871 * Context: single threaded during gadget setup
872 *
873 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700874 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200875 * is, returns last requested ID which is now very useful information.
876 *
877 * @usb_string_ids_n() is called from bind() callbacks to allocate
878 * string IDs. Drivers for functions, configurations, or gadgets will
879 * then store that ID in the appropriate descriptors and string table.
880 *
881 * All string identifier should be allocated using this,
882 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
883 * example different functions don't wrongly assign different meanings
884 * to the same identifier.
885 */
886int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
887{
888 unsigned next = c->next_string_id;
889 if (unlikely(n > 254 || (unsigned)next + n > 254))
890 return -ENODEV;
891 c->next_string_id += n;
892 return next + 1;
893}
894
895
David Brownell40982be2008-06-19 17:52:58 -0700896/*-------------------------------------------------------------------------*/
897
898static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
899{
900 if (req->status || req->actual != req->length)
901 DBG((struct usb_composite_dev *) ep->driver_data,
902 "setup complete --> %d, %d/%d\n",
903 req->status, req->actual, req->length);
904}
905
906/*
907 * The setup() callback implements all the ep0 functionality that's
908 * not handled lower down, in hardware or the hardware driver(like
909 * device and endpoint feature flags, and their status). It's all
910 * housekeeping for the gadget function we're implementing. Most of
911 * the work is in config and function specific setup.
912 */
913static int
914composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
915{
916 struct usb_composite_dev *cdev = get_gadget_data(gadget);
917 struct usb_request *req = cdev->req;
918 int value = -EOPNOTSUPP;
919 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800920 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700921 u16 w_value = le16_to_cpu(ctrl->wValue);
922 u16 w_length = le16_to_cpu(ctrl->wLength);
923 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200924 u8 endp;
Mike Lockwoode6be8942010-12-10 16:30:15 -0800925 unsigned long flags;
926
927 spin_lock_irqsave(&cdev->lock, flags);
928 if (!cdev->connected) {
929 cdev->connected = 1;
930 schedule_work(&cdev->switch_work);
931 }
932 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -0700933
934 /* partial re-init of the response message; the function or the
935 * gadget might need to intercept e.g. a control-OUT completion
936 * when we delegate to it.
937 */
938 req->zero = 0;
939 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530940 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700941 gadget->ep0->driver_data = cdev;
942
943 switch (ctrl->bRequest) {
944
945 /* we handle all standard USB descriptors */
946 case USB_REQ_GET_DESCRIPTOR:
947 if (ctrl->bRequestType != USB_DIR_IN)
948 goto unknown;
949 switch (w_value >> 8) {
950
951 case USB_DT_DEVICE:
952 cdev->desc.bNumConfigurations =
953 count_configs(cdev, USB_DT_DEVICE);
954 value = min(w_length, (u16) sizeof cdev->desc);
955 memcpy(req->buf, &cdev->desc, value);
956 break;
957 case USB_DT_DEVICE_QUALIFIER:
958 if (!gadget_is_dualspeed(gadget))
959 break;
960 device_qual(cdev);
961 value = min_t(int, w_length,
962 sizeof(struct usb_qualifier_descriptor));
963 break;
964 case USB_DT_OTHER_SPEED_CONFIG:
965 if (!gadget_is_dualspeed(gadget))
966 break;
967 /* FALLTHROUGH */
968 case USB_DT_CONFIG:
969 value = config_desc(cdev, w_value);
970 if (value >= 0)
971 value = min(w_length, (u16) value);
972 break;
973 case USB_DT_STRING:
974 value = get_string(cdev, req->buf,
975 w_index, w_value & 0xff);
Mike Lockwoodfb52b002010-04-16 15:32:15 -0400976
977 /* Allow functions to handle USB_DT_STRING.
978 * This is required for MTP.
979 */
980 if (value < 0) {
981 struct usb_configuration *cfg;
982 list_for_each_entry(cfg, &cdev->configs, list) {
983 if (cfg && cfg->setup) {
984 value = cfg->setup(cfg, ctrl);
985 if (value >= 0)
986 break;
987 }
988 }
989 }
990
David Brownell40982be2008-06-19 17:52:58 -0700991 if (value >= 0)
992 value = min(w_length, (u16) value);
993 break;
994 }
995 break;
996
997 /* any number of configs can work */
998 case USB_REQ_SET_CONFIGURATION:
999 if (ctrl->bRequestType != 0)
1000 goto unknown;
1001 if (gadget_is_otg(gadget)) {
1002 if (gadget->a_hnp_support)
1003 DBG(cdev, "HNP available\n");
1004 else if (gadget->a_alt_hnp_support)
1005 DBG(cdev, "HNP on another port\n");
1006 else
1007 VDBG(cdev, "HNP inactive\n");
1008 }
1009 spin_lock(&cdev->lock);
1010 value = set_config(cdev, ctrl, w_value);
1011 spin_unlock(&cdev->lock);
1012 break;
1013 case USB_REQ_GET_CONFIGURATION:
1014 if (ctrl->bRequestType != USB_DIR_IN)
1015 goto unknown;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001016 if (cdev->config)
David Brownell40982be2008-06-19 17:52:58 -07001017 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001018 else
David Brownell40982be2008-06-19 17:52:58 -07001019 *(u8 *)req->buf = 0;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001020 value = min(w_length, (u16) 1);
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