blob: 96d89870c2f6c9c3a991ea876f2c28f0e158ecbe [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 */
117 if (cdev && cdev->gadget &&
118 cdev->gadget->speed != USB_SPEED_UNKNOWN) {
119 /* avoid sending a disconnect switch event until after we disconnect */
120 cdev->mute_switch = 1;
121 spin_unlock_irqrestore(&cdev->lock, flags);
122
123 usb_gadget_disconnect(cdev->gadget);
124 msleep(10);
125 usb_gadget_connect(cdev->gadget);
126 } else {
127 spin_unlock_irqrestore(&cdev->lock, flags);
128 }
129}
130
David Brownell40982be2008-06-19 17:52:58 -0700131/**
132 * usb_add_function() - add a function to a configuration
133 * @config: the configuration
134 * @function: the function being added
135 * Context: single threaded during gadget setup
136 *
137 * After initialization, each configuration must have one or more
138 * functions added to it. Adding a function involves calling its @bind()
139 * method to allocate resources such as interface and string identifiers
140 * and endpoints.
141 *
142 * This function returns the value of the function's bind(), which is
143 * zero for success else a negative errno value.
144 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200145int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700146 struct usb_function *function)
147{
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500148 struct usb_composite_dev *cdev = config->cdev;
David Brownell40982be2008-06-19 17:52:58 -0700149 int value = -EINVAL;
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500150 int index;
David Brownell40982be2008-06-19 17:52:58 -0700151
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500152 DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -0700153 function->name, function,
154 config->label, config);
155
156 if (!function->set_alt || !function->disable)
157 goto done;
158
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500159 index = atomic_inc_return(&cdev->driver->function_count);
160 function->dev = device_create(cdev->driver->class, NULL,
161 MKDEV(0, index), NULL, function->name);
162 if (IS_ERR(function->dev))
163 return PTR_ERR(function->dev);
164
165 value = device_create_file(function->dev, &dev_attr_enable);
166 if (value < 0) {
167 device_destroy(cdev->driver->class, MKDEV(0, index));
168 return value;
169 }
170 dev_set_drvdata(function->dev, function);
171
David Brownell40982be2008-06-19 17:52:58 -0700172 function->config = config;
173 list_add_tail(&function->list, &config->functions);
174
175 /* REVISIT *require* function->bind? */
176 if (function->bind) {
177 value = function->bind(config, function);
178 if (value < 0) {
179 list_del(&function->list);
180 function->config = NULL;
181 }
182 } else
183 value = 0;
184
185 /* We allow configurations that don't work at both speeds.
186 * If we run into a lowspeed Linux system, treat it the same
187 * as full speed ... it's the function drivers that will need
188 * to avoid bulk and ISO transfers.
189 */
190 if (!config->fullspeed && function->descriptors)
191 config->fullspeed = true;
192 if (!config->highspeed && function->hs_descriptors)
193 config->highspeed = true;
194
195done:
196 if (value)
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500197 DBG(cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700198 function->name, function, value);
199 return value;
200}
201
202/**
David Brownell60beed92008-08-18 17:38:22 -0700203 * usb_function_deactivate - prevent function and gadget enumeration
204 * @function: the function that isn't yet ready to respond
205 *
206 * Blocks response of the gadget driver to host enumeration by
207 * preventing the data line pullup from being activated. This is
208 * normally called during @bind() processing to change from the
209 * initial "ready to respond" state, or when a required resource
210 * becomes available.
211 *
212 * For example, drivers that serve as a passthrough to a userspace
213 * daemon can block enumeration unless that daemon (such as an OBEX,
214 * MTP, or print server) is ready to handle host requests.
215 *
216 * Not all systems support software control of their USB peripheral
217 * data pullups.
218 *
219 * Returns zero on success, else negative errno.
220 */
221int usb_function_deactivate(struct usb_function *function)
222{
223 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200224 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700225 int status = 0;
226
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200227 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700228
229 if (cdev->deactivations == 0)
230 status = usb_gadget_disconnect(cdev->gadget);
231 if (status == 0)
232 cdev->deactivations++;
233
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200234 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700235 return status;
236}
237
238/**
239 * usb_function_activate - allow function and gadget enumeration
240 * @function: function on which usb_function_activate() was called
241 *
242 * Reverses effect of usb_function_deactivate(). If no more functions
243 * are delaying their activation, the gadget driver will respond to
244 * host enumeration procedures.
245 *
246 * Returns zero on success, else negative errno.
247 */
248int usb_function_activate(struct usb_function *function)
249{
250 struct usb_composite_dev *cdev = function->config->cdev;
251 int status = 0;
252
253 spin_lock(&cdev->lock);
254
255 if (WARN_ON(cdev->deactivations == 0))
256 status = -EINVAL;
257 else {
258 cdev->deactivations--;
259 if (cdev->deactivations == 0)
260 status = usb_gadget_connect(cdev->gadget);
261 }
262
263 spin_unlock(&cdev->lock);
264 return status;
265}
266
267/**
David Brownell40982be2008-06-19 17:52:58 -0700268 * usb_interface_id() - allocate an unused interface ID
269 * @config: configuration associated with the interface
270 * @function: function handling the interface
271 * Context: single threaded during gadget setup
272 *
273 * usb_interface_id() is called from usb_function.bind() callbacks to
274 * allocate new interface IDs. The function driver will then store that
275 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300276 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700277 * particularly changing its altsetting via set_alt(). There may
278 * also be class-specific or vendor-specific requests to handle.
279 *
280 * All interface identifier should be allocated using this routine, to
281 * ensure that for example different functions don't wrongly assign
282 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300283 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700284 * one configuration (or more than once in a given configuration) need
285 * multiple versions of the relevant descriptors.
286 *
287 * Returns the interface ID which was allocated; or -ENODEV if no
288 * more interface IDs can be allocated.
289 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200290int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700291 struct usb_function *function)
292{
293 unsigned id = config->next_interface_id;
294
295 if (id < MAX_CONFIG_INTERFACES) {
296 config->interface[id] = function;
297 config->next_interface_id = id + 1;
298 return id;
299 }
300 return -ENODEV;
301}
302
303static int config_buf(struct usb_configuration *config,
304 enum usb_device_speed speed, void *buf, u8 type)
305{
306 struct usb_config_descriptor *c = buf;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500307 struct usb_interface_descriptor *intf;
John Michelauf71a3942010-12-10 12:09:53 -0600308 struct usb_interface_assoc_descriptor *iad = NULL;
David Brownell40982be2008-06-19 17:52:58 -0700309 void *next = buf + USB_DT_CONFIG_SIZE;
310 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
311 struct usb_function *f;
312 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500313 int interfaceCount = 0;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500314 u8 *dest;
David Brownell40982be2008-06-19 17:52:58 -0700315
316 /* write the config descriptor */
317 c = buf;
318 c->bLength = USB_DT_CONFIG_SIZE;
319 c->bDescriptorType = type;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500320 /* wTotalLength and bNumInterfaces are written later */
David Brownell40982be2008-06-19 17:52:58 -0700321 c->bConfigurationValue = config->bConfigurationValue;
322 c->iConfiguration = config->iConfiguration;
323 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700324 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700325
326 /* There may be e.g. OTG descriptors */
327 if (config->descriptors) {
328 status = usb_descriptor_fillbuf(next, len,
329 config->descriptors);
330 if (status < 0)
331 return status;
332 len -= status;
333 next += status;
334 }
335
336 /* add each function's descriptors */
337 list_for_each_entry(f, &config->functions, list) {
338 struct usb_descriptor_header **descriptors;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500339 struct usb_descriptor_header *descriptor;
David Brownell40982be2008-06-19 17:52:58 -0700340
341 if (speed == USB_SPEED_HIGH)
342 descriptors = f->hs_descriptors;
343 else
344 descriptors = f->descriptors;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400345 if (f->disabled || !descriptors || descriptors[0] == NULL)
David Brownell40982be2008-06-19 17:52:58 -0700346 continue;
347 status = usb_descriptor_fillbuf(next, len,
348 (const struct usb_descriptor_header **) descriptors);
349 if (status < 0)
350 return status;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500351
352 /* set interface numbers dynamically */
353 dest = next;
354 while ((descriptor = *descriptors++) != NULL) {
355 intf = (struct usb_interface_descriptor *)dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500356 if (intf->bDescriptorType == USB_DT_INTERFACE) {
357 /* don't increment bInterfaceNumber for alternate settings */
358 if (intf->bAlternateSetting == 0)
359 intf->bInterfaceNumber = interfaceCount++;
360 else
361 intf->bInterfaceNumber = interfaceCount - 1;
John Michelauf71a3942010-12-10 12:09:53 -0600362 if (iad) {
363 iad->bFirstInterface =
364 intf->bInterfaceNumber;
365 iad = NULL;
366 }
367 } else if (intf->bDescriptorType ==
368 USB_DT_INTERFACE_ASSOCIATION) {
369 /* This will be first if it exists. Save
370 * a pointer to it so we can properly set
371 * bFirstInterface when we process the first
372 * interface.
373 */
374 iad = (struct usb_interface_assoc_descriptor *)
375 dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500376 }
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500377 dest += intf->bLength;
378 }
379
David Brownell40982be2008-06-19 17:52:58 -0700380 len -= status;
381 next += status;
382 }
383
384 len = next - buf;
385 c->wTotalLength = cpu_to_le16(len);
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500386 c->bNumInterfaces = interfaceCount;
David Brownell40982be2008-06-19 17:52:58 -0700387 return len;
388}
389
390static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
391{
392 struct usb_gadget *gadget = cdev->gadget;
393 struct usb_configuration *c;
394 u8 type = w_value >> 8;
395 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
396
397 if (gadget_is_dualspeed(gadget)) {
398 int hs = 0;
399
400 if (gadget->speed == USB_SPEED_HIGH)
401 hs = 1;
402 if (type == USB_DT_OTHER_SPEED_CONFIG)
403 hs = !hs;
404 if (hs)
405 speed = USB_SPEED_HIGH;
406
407 }
408
409 /* This is a lookup by config *INDEX* */
410 w_value &= 0xff;
411 list_for_each_entry(c, &cdev->configs, list) {
412 /* ignore configs that won't work at this speed */
413 if (speed == USB_SPEED_HIGH) {
414 if (!c->highspeed)
415 continue;
416 } else {
417 if (!c->fullspeed)
418 continue;
419 }
420 if (w_value == 0)
421 return config_buf(c, speed, cdev->req->buf, type);
422 w_value--;
423 }
424 return -EINVAL;
425}
426
427static int count_configs(struct usb_composite_dev *cdev, unsigned type)
428{
429 struct usb_gadget *gadget = cdev->gadget;
430 struct usb_configuration *c;
431 unsigned count = 0;
432 int hs = 0;
433
434 if (gadget_is_dualspeed(gadget)) {
435 if (gadget->speed == USB_SPEED_HIGH)
436 hs = 1;
437 if (type == USB_DT_DEVICE_QUALIFIER)
438 hs = !hs;
439 }
440 list_for_each_entry(c, &cdev->configs, list) {
441 /* ignore configs that won't work at this speed */
442 if (hs) {
443 if (!c->highspeed)
444 continue;
445 } else {
446 if (!c->fullspeed)
447 continue;
448 }
449 count++;
450 }
451 return count;
452}
453
454static void device_qual(struct usb_composite_dev *cdev)
455{
456 struct usb_qualifier_descriptor *qual = cdev->req->buf;
457
458 qual->bLength = sizeof(*qual);
459 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
460 /* POLICY: same bcdUSB and device type info at both speeds */
461 qual->bcdUSB = cdev->desc.bcdUSB;
462 qual->bDeviceClass = cdev->desc.bDeviceClass;
463 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
464 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
465 /* ASSUME same EP0 fifo size at both speeds */
466 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
467 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700468 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700469}
470
471/*-------------------------------------------------------------------------*/
472
473static void reset_config(struct usb_composite_dev *cdev)
474{
475 struct usb_function *f;
476
477 DBG(cdev, "reset config\n");
478
479 list_for_each_entry(f, &cdev->config->functions, list) {
480 if (f->disable)
481 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200482
483 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700484 }
485 cdev->config = NULL;
486}
487
488static int set_config(struct usb_composite_dev *cdev,
489 const struct usb_ctrlrequest *ctrl, unsigned number)
490{
491 struct usb_gadget *gadget = cdev->gadget;
492 struct usb_configuration *c = NULL;
493 int result = -EINVAL;
494 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
495 int tmp;
496
497 if (cdev->config)
498 reset_config(cdev);
499
500 if (number) {
501 list_for_each_entry(c, &cdev->configs, list) {
502 if (c->bConfigurationValue == number) {
503 result = 0;
504 break;
505 }
506 }
507 if (result < 0)
508 goto done;
509 } else
510 result = 0;
511
512 INFO(cdev, "%s speed config #%d: %s\n",
513 ({ char *speed;
514 switch (gadget->speed) {
515 case USB_SPEED_LOW: speed = "low"; break;
516 case USB_SPEED_FULL: speed = "full"; break;
517 case USB_SPEED_HIGH: speed = "high"; break;
518 default: speed = "?"; break;
519 } ; speed; }), number, c ? c->label : "unconfigured");
520
521 if (!c)
522 goto done;
523
524 cdev->config = c;
525
526 /* Initialize all interfaces by setting them to altsetting zero. */
527 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
528 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200529 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700530
531 if (!f)
532 break;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400533 if (f->disabled)
Mike Lockwood8c6a6b22010-02-26 09:30:01 -0500534 continue;
David Brownell40982be2008-06-19 17:52:58 -0700535
Laurent Pinchart52426582009-10-21 00:03:38 +0200536 /*
537 * Record which endpoints are used by the function. This is used
538 * to dispatch control requests targeted at that endpoint to the
539 * function's setup callback instead of the current
540 * configuration's setup callback.
541 */
542 if (gadget->speed == USB_SPEED_HIGH)
543 descriptors = f->hs_descriptors;
544 else
545 descriptors = f->descriptors;
546
547 for (; *descriptors; ++descriptors) {
548 struct usb_endpoint_descriptor *ep;
549 int addr;
550
551 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
552 continue;
553
554 ep = (struct usb_endpoint_descriptor *)*descriptors;
555 addr = ((ep->bEndpointAddress & 0x80) >> 3)
556 | (ep->bEndpointAddress & 0x0f);
557 set_bit(addr, f->endpoints);
558 }
559
David Brownell40982be2008-06-19 17:52:58 -0700560 result = f->set_alt(f, tmp, 0);
561 if (result < 0) {
562 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
563 tmp, f->name, f, result);
564
565 reset_config(cdev);
566 goto done;
567 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300568
569 if (result == USB_GADGET_DELAYED_STATUS) {
570 DBG(cdev,
571 "%s: interface %d (%s) requested delayed status\n",
572 __func__, tmp, f->name);
573 cdev->delayed_status++;
574 DBG(cdev, "delayed_status count %d\n",
575 cdev->delayed_status);
576 }
David Brownell40982be2008-06-19 17:52:58 -0700577 }
578
579 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700580 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700581done:
582 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400583
Mike Lockwood8da4cc82010-06-27 20:05:55 -0400584 schedule_work(&cdev->switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400585
Roger Quadros1b9ba002011-05-09 13:08:06 +0300586 if (result >= 0 && cdev->delayed_status)
587 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700588 return result;
589}
590
591/**
592 * usb_add_config() - add a configuration to a device.
593 * @cdev: wraps the USB gadget
594 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200595 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700596 * Context: single threaded during gadget setup
597 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200598 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700599 * add each of the configurations it supports, using this routine.
600 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200601 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700602 * is zero for success else a negative errno value. Binding configurations
603 * assigns global resources including string IDs, and per-configuration
604 * resources such as interface IDs and endpoints.
605 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200606int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200607 struct usb_configuration *config,
608 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700609{
610 int status = -EINVAL;
611 struct usb_configuration *c;
612
613 DBG(cdev, "adding config #%u '%s'/%p\n",
614 config->bConfigurationValue,
615 config->label, config);
616
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200617 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700618 goto done;
619
620 /* Prevent duplicate configuration identifiers */
621 list_for_each_entry(c, &cdev->configs, list) {
622 if (c->bConfigurationValue == config->bConfigurationValue) {
623 status = -EBUSY;
624 goto done;
625 }
626 }
627
628 config->cdev = cdev;
629 list_add_tail(&config->list, &cdev->configs);
630
631 INIT_LIST_HEAD(&config->functions);
632 config->next_interface_id = 0;
633
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200634 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700635 if (status < 0) {
636 list_del(&config->list);
637 config->cdev = NULL;
638 } else {
639 unsigned i;
640
641 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
642 config->bConfigurationValue, config,
643 config->highspeed ? " high" : "",
644 config->fullspeed
645 ? (gadget_is_dualspeed(cdev->gadget)
646 ? " full"
647 : " full/low")
648 : "");
649
650 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
651 struct usb_function *f = config->interface[i];
652
653 if (!f)
654 continue;
655 DBG(cdev, " interface %d = %s/%p\n",
656 i, f->name, f);
657 }
658 }
659
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200660 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700661 * ep->driver_data as needed.
662 */
663 usb_ep_autoconfig_reset(cdev->gadget);
664
665done:
666 if (status)
667 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
668 config->bConfigurationValue, status);
669 return status;
670}
671
672/*-------------------------------------------------------------------------*/
673
674/* We support strings in multiple languages ... string descriptor zero
675 * says which languages are supported. The typical case will be that
676 * only one language (probably English) is used, with I18N handled on
677 * the host side.
678 */
679
680static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
681{
682 const struct usb_gadget_strings *s;
683 u16 language;
684 __le16 *tmp;
685
686 while (*sp) {
687 s = *sp;
688 language = cpu_to_le16(s->language);
689 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
690 if (*tmp == language)
691 goto repeat;
692 }
693 *tmp++ = language;
694repeat:
695 sp++;
696 }
697}
698
699static int lookup_string(
700 struct usb_gadget_strings **sp,
701 void *buf,
702 u16 language,
703 int id
704)
705{
706 struct usb_gadget_strings *s;
707 int value;
708
709 while (*sp) {
710 s = *sp++;
711 if (s->language != language)
712 continue;
713 value = usb_gadget_get_string(s, id, buf);
714 if (value > 0)
715 return value;
716 }
717 return -EINVAL;
718}
719
720static int get_string(struct usb_composite_dev *cdev,
721 void *buf, u16 language, int id)
722{
723 struct usb_configuration *c;
724 struct usb_function *f;
725 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200726 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700727
728 /* Yes, not only is USB's I18N support probably more than most
729 * folk will ever care about ... also, it's all supported here.
730 * (Except for UTF8 support for Unicode's "Astral Planes".)
731 */
732
733 /* 0 == report all available language codes */
734 if (id == 0) {
735 struct usb_string_descriptor *s = buf;
736 struct usb_gadget_strings **sp;
737
738 memset(s, 0, 256);
739 s->bDescriptorType = USB_DT_STRING;
740
741 sp = composite->strings;
742 if (sp)
743 collect_langs(sp, s->wData);
744
745 list_for_each_entry(c, &cdev->configs, list) {
746 sp = c->strings;
747 if (sp)
748 collect_langs(sp, s->wData);
749
750 list_for_each_entry(f, &c->functions, list) {
751 sp = f->strings;
752 if (sp)
753 collect_langs(sp, s->wData);
754 }
755 }
756
Roel Kluin417b57b2009-08-06 16:09:51 -0700757 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700758 continue;
759 if (!len)
760 return -EINVAL;
761
762 s->bLength = 2 * (len + 1);
763 return s->bLength;
764 }
765
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200766 /* Otherwise, look up and return a specified string. First
767 * check if the string has not been overridden.
768 */
769 if (cdev->manufacturer_override == id)
770 str = iManufacturer ?: composite->iManufacturer ?:
771 composite_manufacturer;
772 else if (cdev->product_override == id)
773 str = iProduct ?: composite->iProduct;
774 else if (cdev->serial_override == id)
775 str = iSerialNumber;
776 else
777 str = NULL;
778 if (str) {
779 struct usb_gadget_strings strings = {
780 .language = language,
781 .strings = &(struct usb_string) { 0xff, str }
782 };
783 return usb_gadget_get_string(&strings, 0xff, buf);
784 }
785
786 /* String IDs are device-scoped, so we look up each string
787 * table we're told about. These lookups are infrequent;
788 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700789 */
790 if (composite->strings) {
791 len = lookup_string(composite->strings, buf, language, id);
792 if (len > 0)
793 return len;
794 }
795 list_for_each_entry(c, &cdev->configs, list) {
796 if (c->strings) {
797 len = lookup_string(c->strings, buf, language, id);
798 if (len > 0)
799 return len;
800 }
801 list_for_each_entry(f, &c->functions, list) {
802 if (!f->strings)
803 continue;
804 len = lookup_string(f->strings, buf, language, id);
805 if (len > 0)
806 return len;
807 }
808 }
809 return -EINVAL;
810}
811
812/**
813 * usb_string_id() - allocate an unused string ID
814 * @cdev: the device whose string descriptor IDs are being allocated
815 * Context: single threaded during gadget setup
816 *
817 * @usb_string_id() is called from bind() callbacks to allocate
818 * string IDs. Drivers for functions, configurations, or gadgets will
819 * then store that ID in the appropriate descriptors and string table.
820 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200821 * All string identifier should be allocated using this,
822 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
823 * that for example different functions don't wrongly assign different
824 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700825 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200826int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700827{
828 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200829 /* string id 0 is reserved by USB spec for list of
830 * supported languages */
831 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700832 cdev->next_string_id++;
833 return cdev->next_string_id;
834 }
835 return -ENODEV;
836}
837
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200838/**
839 * usb_string_ids() - allocate unused string IDs in batch
840 * @cdev: the device whose string descriptor IDs are being allocated
841 * @str: an array of usb_string objects to assign numbers to
842 * Context: single threaded during gadget setup
843 *
844 * @usb_string_ids() is called from bind() callbacks to allocate
845 * string IDs. Drivers for functions, configurations, or gadgets will
846 * then copy IDs from the string table to the appropriate descriptors
847 * and string table for other languages.
848 *
849 * All string identifier should be allocated using this,
850 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
851 * example different functions don't wrongly assign different meanings
852 * to the same identifier.
853 */
854int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
855{
856 int next = cdev->next_string_id;
857
858 for (; str->s; ++str) {
859 if (unlikely(next >= 254))
860 return -ENODEV;
861 str->id = ++next;
862 }
863
864 cdev->next_string_id = next;
865
866 return 0;
867}
868
869/**
870 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700871 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200872 * @n: number of string IDs to allocate
873 * Context: single threaded during gadget setup
874 *
875 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700876 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200877 * is, returns last requested ID which is now very useful information.
878 *
879 * @usb_string_ids_n() is called from bind() callbacks to allocate
880 * string IDs. Drivers for functions, configurations, or gadgets will
881 * then store that ID in the appropriate descriptors and string table.
882 *
883 * All string identifier should be allocated using this,
884 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
885 * example different functions don't wrongly assign different meanings
886 * to the same identifier.
887 */
888int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
889{
890 unsigned next = c->next_string_id;
891 if (unlikely(n > 254 || (unsigned)next + n > 254))
892 return -ENODEV;
893 c->next_string_id += n;
894 return next + 1;
895}
896
897
David Brownell40982be2008-06-19 17:52:58 -0700898/*-------------------------------------------------------------------------*/
899
900static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
901{
902 if (req->status || req->actual != req->length)
903 DBG((struct usb_composite_dev *) ep->driver_data,
904 "setup complete --> %d, %d/%d\n",
905 req->status, req->actual, req->length);
906}
907
908/*
909 * The setup() callback implements all the ep0 functionality that's
910 * not handled lower down, in hardware or the hardware driver(like
911 * device and endpoint feature flags, and their status). It's all
912 * housekeeping for the gadget function we're implementing. Most of
913 * the work is in config and function specific setup.
914 */
915static int
916composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
917{
918 struct usb_composite_dev *cdev = get_gadget_data(gadget);
919 struct usb_request *req = cdev->req;
920 int value = -EOPNOTSUPP;
921 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800922 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700923 u16 w_value = le16_to_cpu(ctrl->wValue);
924 u16 w_length = le16_to_cpu(ctrl->wLength);
925 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200926 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700927
928 /* partial re-init of the response message; the function or the
929 * gadget might need to intercept e.g. a control-OUT completion
930 * when we delegate to it.
931 */
932 req->zero = 0;
933 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530934 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700935 gadget->ep0->driver_data = cdev;
936
937 switch (ctrl->bRequest) {
938
939 /* we handle all standard USB descriptors */
940 case USB_REQ_GET_DESCRIPTOR:
941 if (ctrl->bRequestType != USB_DIR_IN)
942 goto unknown;
943 switch (w_value >> 8) {
944
945 case USB_DT_DEVICE:
946 cdev->desc.bNumConfigurations =
947 count_configs(cdev, USB_DT_DEVICE);
948 value = min(w_length, (u16) sizeof cdev->desc);
949 memcpy(req->buf, &cdev->desc, value);
950 break;
951 case USB_DT_DEVICE_QUALIFIER:
952 if (!gadget_is_dualspeed(gadget))
953 break;
954 device_qual(cdev);
955 value = min_t(int, w_length,
956 sizeof(struct usb_qualifier_descriptor));
957 break;
958 case USB_DT_OTHER_SPEED_CONFIG:
959 if (!gadget_is_dualspeed(gadget))
960 break;
961 /* FALLTHROUGH */
962 case USB_DT_CONFIG:
963 value = config_desc(cdev, w_value);
964 if (value >= 0)
965 value = min(w_length, (u16) value);
966 break;
967 case USB_DT_STRING:
968 value = get_string(cdev, req->buf,
969 w_index, w_value & 0xff);
Mike Lockwoodfb52b002010-04-16 15:32:15 -0400970
971 /* Allow functions to handle USB_DT_STRING.
972 * This is required for MTP.
973 */
974 if (value < 0) {
975 struct usb_configuration *cfg;
976 list_for_each_entry(cfg, &cdev->configs, list) {
977 if (cfg && cfg->setup) {
978 value = cfg->setup(cfg, ctrl);
979 if (value >= 0)
980 break;
981 }
982 }
983 }
984
David Brownell40982be2008-06-19 17:52:58 -0700985 if (value >= 0)
986 value = min(w_length, (u16) value);
987 break;
988 }
989 break;
990
991 /* any number of configs can work */
992 case USB_REQ_SET_CONFIGURATION:
993 if (ctrl->bRequestType != 0)
994 goto unknown;
995 if (gadget_is_otg(gadget)) {
996 if (gadget->a_hnp_support)
997 DBG(cdev, "HNP available\n");
998 else if (gadget->a_alt_hnp_support)
999 DBG(cdev, "HNP on another port\n");
1000 else
1001 VDBG(cdev, "HNP inactive\n");
1002 }
1003 spin_lock(&cdev->lock);
1004 value = set_config(cdev, ctrl, w_value);
1005 spin_unlock(&cdev->lock);
1006 break;
1007 case USB_REQ_GET_CONFIGURATION:
1008 if (ctrl->bRequestType != USB_DIR_IN)
1009 goto unknown;
Jared Suttles258d1032009-08-07 18:57:49 -05001010 if (cdev->config) {
David Brownell40982be2008-06-19 17:52:58 -07001011 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Jared Suttles258d1032009-08-07 18:57:49 -05001012 value = min(w_length, (u16) 1);
1013 } else {
David Brownell40982be2008-06-19 17:52:58 -07001014 *(u8 *)req->buf = 0;
Jared Suttles258d1032009-08-07 18:57:49 -05001015 }
David Brownell40982be2008-06-19 17:52:58 -07001016 break;
1017
1018 /* function drivers must handle get/set altsetting; if there's
1019 * no get() method, we know only altsetting zero works.
1020 */
1021 case USB_REQ_SET_INTERFACE:
1022 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1023 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001024 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001025 break;
Bryan Wu08889512009-01-08 00:21:19 +08001026 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001027 if (!f)
1028 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001029 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001030 break;
1031 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001032 if (value == USB_GADGET_DELAYED_STATUS) {
1033 DBG(cdev,
1034 "%s: interface %d (%s) requested delayed status\n",
1035 __func__, intf, f->name);
1036 cdev->delayed_status++;
1037 DBG(cdev, "delayed_status count %d\n",
1038 cdev->delayed_status);
1039 }
David Brownell40982be2008-06-19 17:52:58 -07001040 break;
1041 case USB_REQ_GET_INTERFACE:
1042 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1043 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001044 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001045 break;
Bryan Wu08889512009-01-08 00:21:19 +08001046 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001047 if (!f)
1048 break;
1049 /* lots of interfaces only need altsetting zero... */
1050 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1051 if (value < 0)
1052 break;
1053 *((u8 *)req->buf) = value;
1054 value = min(w_length, (u16) 1);
1055 break;
1056 default:
1057unknown:
1058 VDBG(cdev,
1059 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1060 ctrl->bRequestType, ctrl->bRequest,
1061 w_value, w_index, w_length);
1062
Laurent Pinchart52426582009-10-21 00:03:38 +02001063 /* functions always handle their interfaces and endpoints...
1064 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001065 * configuration code.
1066 *
1067 * REVISIT it could make sense to let the composite device
1068 * take such requests too, if that's ever needed: to work
1069 * in config 0, etc.
1070 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001071 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1072 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001073 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301074 break;
1075 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001076 break;
1077
1078 case USB_RECIP_ENDPOINT:
1079 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1080 list_for_each_entry(f, &cdev->config->functions, list) {
1081 if (test_bit(endp, f->endpoints))
1082 break;
1083 }
1084 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001085 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001086 break;
David Brownell40982be2008-06-19 17:52:58 -07001087 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001088
1089 if (f && f->setup)
1090 value = f->setup(f, ctrl);
1091 else {
David Brownell40982be2008-06-19 17:52:58 -07001092 struct usb_configuration *c;
1093
1094 c = cdev->config;
1095 if (c && c->setup)
1096 value = c->setup(c, ctrl);
1097 }
1098
Joe Swantek292b1bc2009-12-15 07:17:40 -05001099 /* If the vendor request is not processed (value < 0),
1100 * call all device registered configure setup callbacks
1101 * to process it.
1102 * This is used to handle the following cases:
1103 * - vendor request is for the device and arrives before
1104 * setconfiguration.
1105 * - Some devices are required to handle vendor request before
1106 * setconfiguration such as MTP, USBNET.
1107 */
1108
1109 if (value < 0) {
1110 struct usb_configuration *cfg;
1111
1112 list_for_each_entry(cfg, &cdev->configs, list) {
1113 if (cfg && cfg->setup)
1114 value = cfg->setup(cfg, ctrl);
1115 }
1116 }
1117
David Brownell40982be2008-06-19 17:52:58 -07001118 goto done;
1119 }
1120
1121 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001122 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001123 req->length = value;
1124 req->zero = value < w_length;
1125 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1126 if (value < 0) {
1127 DBG(cdev, "ep_queue --> %d\n", value);
1128 req->status = 0;
1129 composite_setup_complete(gadget->ep0, req);
1130 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001131 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1132 WARN(cdev,
1133 "%s: Delayed status not supported for w_length != 0",
1134 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001135 }
1136
1137done:
1138 /* device either stalls (value < 0) or reports success */
1139 return value;
1140}
1141
1142static void composite_disconnect(struct usb_gadget *gadget)
1143{
1144 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1145 unsigned long flags;
1146
1147 /* REVISIT: should we have config and device level
1148 * disconnect callbacks?
1149 */
1150 spin_lock_irqsave(&cdev->lock, flags);
1151 if (cdev->config)
1152 reset_config(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001153
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001154 if (composite->disconnect)
1155 composite->disconnect(cdev);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001156
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001157 if (cdev->mute_switch)
1158 cdev->mute_switch = 0;
1159 else
1160 schedule_work(&cdev->switch_work);
1161 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001162}
1163
1164/*-------------------------------------------------------------------------*/
1165
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001166static ssize_t composite_show_suspended(struct device *dev,
1167 struct device_attribute *attr,
1168 char *buf)
1169{
1170 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1171 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1172
1173 return sprintf(buf, "%d\n", cdev->suspended);
1174}
1175
1176static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1177
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001178static void
David Brownell40982be2008-06-19 17:52:58 -07001179composite_unbind(struct usb_gadget *gadget)
1180{
1181 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1182
1183 /* composite_disconnect() must already have been called
1184 * by the underlying peripheral controller driver!
1185 * so there's no i/o concurrency that could affect the
1186 * state protected by cdev->lock.
1187 */
1188 WARN_ON(cdev->config);
1189
1190 while (!list_empty(&cdev->configs)) {
1191 struct usb_configuration *c;
1192
1193 c = list_first_entry(&cdev->configs,
1194 struct usb_configuration, list);
1195 while (!list_empty(&c->functions)) {
1196 struct usb_function *f;
1197
1198 f = list_first_entry(&c->functions,
1199 struct usb_function, list);
1200 list_del(&f->list);
1201 if (f->unbind) {
1202 DBG(cdev, "unbind function '%s'/%p\n",
1203 f->name, f);
1204 f->unbind(c, f);
1205 /* may free memory for "f" */
1206 }
1207 }
1208 list_del(&c->list);
1209 if (c->unbind) {
1210 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1211 c->unbind(c);
1212 /* may free memory for "c" */
1213 }
1214 }
1215 if (composite->unbind)
1216 composite->unbind(cdev);
1217
1218 if (cdev->req) {
1219 kfree(cdev->req->buf);
1220 usb_ep_free_request(gadget->ep0, cdev->req);
1221 }
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001222 switch_dev_unregister(&cdev->sdev);
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301223 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001224 kfree(cdev);
1225 set_gadget_data(gadget, NULL);
1226 composite = NULL;
1227}
1228
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001229static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001230{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001231 if (!*desc) {
1232 int ret = usb_string_id(cdev);
1233 if (unlikely(ret < 0))
1234 WARNING(cdev, "failed to override string ID\n");
1235 else
1236 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001237 }
David Brownell40982be2008-06-19 17:52:58 -07001238
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001239 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001240}
1241
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001242static void
1243composite_switch_work(struct work_struct *data)
1244{
1245 struct usb_composite_dev *cdev =
1246 container_of(data, struct usb_composite_dev, switch_work);
1247 struct usb_configuration *config = cdev->config;
1248
1249 if (config)
1250 switch_set_state(&cdev->sdev, config->bConfigurationValue);
1251 else
1252 switch_set_state(&cdev->sdev, 0);
1253}
1254
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001255static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001256{
1257 struct usb_composite_dev *cdev;
1258 int status = -ENOMEM;
1259
1260 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1261 if (!cdev)
1262 return status;
1263
1264 spin_lock_init(&cdev->lock);
1265 cdev->gadget = gadget;
1266 set_gadget_data(gadget, cdev);
1267 INIT_LIST_HEAD(&cdev->configs);
1268
1269 /* preallocate control response and buffer */
1270 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1271 if (!cdev->req)
1272 goto fail;
1273 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1274 if (!cdev->req->buf)
1275 goto fail;
1276 cdev->req->complete = composite_setup_complete;
1277 gadget->ep0->driver_data = cdev;
1278
1279 cdev->bufsiz = USB_BUFSIZ;
1280 cdev->driver = composite;
1281
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301282 /*
1283 * As per USB compliance update, a device that is actively drawing
1284 * more than 100mA from USB must report itself as bus-powered in
1285 * the GetStatus(DEVICE) call.
1286 */
1287 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1288 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001289
1290 /* interface and string IDs start at zero via kzalloc.
1291 * we force endpoints to start unassigned; few controller
1292 * drivers will zero ep->driver_data.
1293 */
1294 usb_ep_autoconfig_reset(cdev->gadget);
1295
1296 /* composite gadget needs to assign strings for whole device (like
1297 * serial number), register function drivers, potentially update
1298 * power state and consumption, etc
1299 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001300 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001301 if (status < 0)
1302 goto fail;
1303
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001304 cdev->sdev.name = "usb_configuration";
1305 status = switch_dev_register(&cdev->sdev);
1306 if (status < 0)
1307 goto fail;
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001308 INIT_WORK(&cdev->switch_work, composite_switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001309
David Brownell40982be2008-06-19 17:52:58 -07001310 cdev->desc = *composite->dev;
1311 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1312
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001313 /* standardized runtime overrides for device ID data */
1314 if (idVendor)
1315 cdev->desc.idVendor = cpu_to_le16(idVendor);
1316 if (idProduct)
1317 cdev->desc.idProduct = cpu_to_le16(idProduct);
1318 if (bcdDevice)
1319 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1320
Marek Belisko78bff3c2010-10-27 10:19:01 +02001321 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001322 if (iManufacturer || !cdev->desc.iManufacturer) {
1323 if (!iManufacturer && !composite->iManufacturer &&
1324 !*composite_manufacturer)
1325 snprintf(composite_manufacturer,
1326 sizeof composite_manufacturer,
1327 "%s %s with %s",
1328 init_utsname()->sysname,
1329 init_utsname()->release,
1330 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001331
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001332 cdev->manufacturer_override =
1333 override_id(cdev, &cdev->desc.iManufacturer);
1334 }
1335
1336 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1337 cdev->product_override =
1338 override_id(cdev, &cdev->desc.iProduct);
1339
1340 if (iSerialNumber)
1341 cdev->serial_override =
1342 override_id(cdev, &cdev->desc.iSerialNumber);
1343
1344 /* has userspace failed to provide a serial number? */
1345 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1346 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1347
1348 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001349 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1350 if (status)
1351 goto fail;
1352
David Brownell40982be2008-06-19 17:52:58 -07001353 INFO(cdev, "%s ready\n", composite->name);
1354 return 0;
1355
1356fail:
1357 composite_unbind(gadget);
1358 return status;
1359}
1360
1361/*-------------------------------------------------------------------------*/
1362
1363static void
1364composite_suspend(struct usb_gadget *gadget)
1365{
1366 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1367 struct usb_function *f;
1368
David Brownell89429392009-03-19 14:14:17 -07001369 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001370 * suspend/resume callbacks?
1371 */
1372 DBG(cdev, "suspend\n");
1373 if (cdev->config) {
1374 list_for_each_entry(f, &cdev->config->functions, list) {
1375 if (f->suspend)
1376 f->suspend(f);
1377 }
1378 }
David Brownell89429392009-03-19 14:14:17 -07001379 if (composite->suspend)
1380 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001381
1382 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001383
1384 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001385}
1386
1387static void
1388composite_resume(struct usb_gadget *gadget)
1389{
1390 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1391 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001392 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001393
David Brownell89429392009-03-19 14:14:17 -07001394 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001395 * suspend/resume callbacks?
1396 */
1397 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001398 if (composite->resume)
1399 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001400 if (cdev->config) {
1401 list_for_each_entry(f, &cdev->config->functions, list) {
1402 if (f->resume)
1403 f->resume(f);
1404 }
Hao Wub23f2f92010-11-29 15:17:03 +08001405
1406 maxpower = cdev->config->bMaxPower;
1407
1408 usb_gadget_vbus_draw(gadget, maxpower ?
1409 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001410 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001411
1412 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001413}
1414
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001415static int
1416composite_uevent(struct device *dev, struct kobj_uevent_env *env)
1417{
1418 struct usb_function *f = dev_get_drvdata(dev);
1419
1420 if (!f) {
1421 /* this happens when the device is first created */
1422 return 0;
1423 }
1424
1425 if (add_uevent_var(env, "FUNCTION=%s", f->name))
1426 return -ENOMEM;
1427 if (add_uevent_var(env, "ENABLED=%d", !f->disabled))
1428 return -ENOMEM;
1429 return 0;
1430}
1431
David Brownell40982be2008-06-19 17:52:58 -07001432/*-------------------------------------------------------------------------*/
1433
1434static struct usb_gadget_driver composite_driver = {
1435 .speed = USB_SPEED_HIGH,
1436
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001437 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001438
1439 .setup = composite_setup,
1440 .disconnect = composite_disconnect,
1441
1442 .suspend = composite_suspend,
1443 .resume = composite_resume,
1444
1445 .driver = {
1446 .owner = THIS_MODULE,
1447 },
1448};
1449
1450/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001451 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001452 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001453 * @bind: the callback used to allocate resources that are shared across the
1454 * whole device, such as string IDs, and add its configurations using
1455 * @usb_add_config(). This may fail by returning a negative errno
1456 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001457 * Context: single threaded during gadget setup
1458 *
1459 * This function is used to register drivers using the composite driver
1460 * framework. The return value is zero, or a negative errno value.
1461 * Those values normally come from the driver's @bind method, which does
1462 * all the work of setting up the driver to match the hardware.
1463 *
1464 * On successful return, the gadget is ready to respond to requests from
1465 * the host, unless one of its components invokes usb_gadget_disconnect()
1466 * while it was binding. That would usually be done in order to wait for
1467 * some userspace participation.
1468 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001469int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001470 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001471{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001472 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001473 return -EINVAL;
1474
1475 if (!driver->name)
1476 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001477 if (!driver->iProduct)
1478 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001479 composite_driver.function = (char *) driver->name;
1480 composite_driver.driver.name = driver->name;
1481 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001482 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001483
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001484 driver->class = class_create(THIS_MODULE, "usb_composite");
1485 if (IS_ERR(driver->class))
1486 return PTR_ERR(driver->class);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001487 driver->class->dev_uevent = composite_uevent;
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001488
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001489 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001490}
1491
1492/**
1493 * usb_composite_unregister() - unregister a composite driver
1494 * @driver: the driver to unregister
1495 *
1496 * This function is used to unregister drivers using the composite
1497 * driver framework.
1498 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001499void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001500{
1501 if (composite != driver)
1502 return;
1503 usb_gadget_unregister_driver(&composite_driver);
1504}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001505
1506/**
1507 * usb_composite_setup_continue() - Continue with the control transfer
1508 * @cdev: the composite device who's control transfer was kept waiting
1509 *
1510 * This function must be called by the USB function driver to continue
1511 * with the control transfer's data/status stage in case it had requested to
1512 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1513 * can request the composite framework to delay the setup request's data/status
1514 * stages by returning USB_GADGET_DELAYED_STATUS.
1515 */
1516void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1517{
1518 int value;
1519 struct usb_request *req = cdev->req;
1520 unsigned long flags;
1521
1522 DBG(cdev, "%s\n", __func__);
1523 spin_lock_irqsave(&cdev->lock, flags);
1524
1525 if (cdev->delayed_status == 0) {
1526 WARN(cdev, "%s: Unexpected call\n", __func__);
1527
1528 } else if (--cdev->delayed_status == 0) {
1529 DBG(cdev, "%s: Completing delayed status\n", __func__);
1530 req->length = 0;
1531 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1532 if (value < 0) {
1533 DBG(cdev, "ep_queue --> %d\n", value);
1534 req->status = 0;
1535 composite_setup_complete(cdev->gadget->ep0, req);
1536 }
1537 }
1538
1539 spin_unlock_irqrestore(&cdev->lock, flags);
1540}
1541