blob: bf555327937f29f7d7eb4814a46291a358318f44 [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
David Brownell40982be2008-06-19 17:52:58 -0700110/**
111 * usb_add_function() - add a function to a configuration
112 * @config: the configuration
113 * @function: the function being added
114 * Context: single threaded during gadget setup
115 *
116 * After initialization, each configuration must have one or more
117 * functions added to it. Adding a function involves calling its @bind()
118 * method to allocate resources such as interface and string identifiers
119 * and endpoints.
120 *
121 * This function returns the value of the function's bind(), which is
122 * zero for success else a negative errno value.
123 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200124int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700125 struct usb_function *function)
126{
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500127 struct usb_composite_dev *cdev = config->cdev;
David Brownell40982be2008-06-19 17:52:58 -0700128 int value = -EINVAL;
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500129 int index;
David Brownell40982be2008-06-19 17:52:58 -0700130
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500131 DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -0700132 function->name, function,
133 config->label, config);
134
135 if (!function->set_alt || !function->disable)
136 goto done;
137
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500138 index = atomic_inc_return(&cdev->driver->function_count);
139 function->dev = device_create(cdev->driver->class, NULL,
140 MKDEV(0, index), NULL, function->name);
141 if (IS_ERR(function->dev))
142 return PTR_ERR(function->dev);
143
144 value = device_create_file(function->dev, &dev_attr_enable);
145 if (value < 0) {
146 device_destroy(cdev->driver->class, MKDEV(0, index));
147 return value;
148 }
149 dev_set_drvdata(function->dev, function);
150
David Brownell40982be2008-06-19 17:52:58 -0700151 function->config = config;
152 list_add_tail(&function->list, &config->functions);
153
154 /* REVISIT *require* function->bind? */
155 if (function->bind) {
156 value = function->bind(config, function);
157 if (value < 0) {
158 list_del(&function->list);
159 function->config = NULL;
160 }
161 } else
162 value = 0;
163
164 /* We allow configurations that don't work at both speeds.
165 * If we run into a lowspeed Linux system, treat it the same
166 * as full speed ... it's the function drivers that will need
167 * to avoid bulk and ISO transfers.
168 */
169 if (!config->fullspeed && function->descriptors)
170 config->fullspeed = true;
171 if (!config->highspeed && function->hs_descriptors)
172 config->highspeed = true;
173
174done:
175 if (value)
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500176 DBG(cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700177 function->name, function, value);
178 return value;
179}
180
181/**
David Brownell60beed92008-08-18 17:38:22 -0700182 * usb_function_deactivate - prevent function and gadget enumeration
183 * @function: the function that isn't yet ready to respond
184 *
185 * Blocks response of the gadget driver to host enumeration by
186 * preventing the data line pullup from being activated. This is
187 * normally called during @bind() processing to change from the
188 * initial "ready to respond" state, or when a required resource
189 * becomes available.
190 *
191 * For example, drivers that serve as a passthrough to a userspace
192 * daemon can block enumeration unless that daemon (such as an OBEX,
193 * MTP, or print server) is ready to handle host requests.
194 *
195 * Not all systems support software control of their USB peripheral
196 * data pullups.
197 *
198 * Returns zero on success, else negative errno.
199 */
200int usb_function_deactivate(struct usb_function *function)
201{
202 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200203 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700204 int status = 0;
205
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200206 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700207
208 if (cdev->deactivations == 0)
209 status = usb_gadget_disconnect(cdev->gadget);
210 if (status == 0)
211 cdev->deactivations++;
212
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200213 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700214 return status;
215}
216
217/**
218 * usb_function_activate - allow function and gadget enumeration
219 * @function: function on which usb_function_activate() was called
220 *
221 * Reverses effect of usb_function_deactivate(). If no more functions
222 * are delaying their activation, the gadget driver will respond to
223 * host enumeration procedures.
224 *
225 * Returns zero on success, else negative errno.
226 */
227int usb_function_activate(struct usb_function *function)
228{
229 struct usb_composite_dev *cdev = function->config->cdev;
230 int status = 0;
231
232 spin_lock(&cdev->lock);
233
234 if (WARN_ON(cdev->deactivations == 0))
235 status = -EINVAL;
236 else {
237 cdev->deactivations--;
238 if (cdev->deactivations == 0)
239 status = usb_gadget_connect(cdev->gadget);
240 }
241
242 spin_unlock(&cdev->lock);
243 return status;
244}
245
246/**
David Brownell40982be2008-06-19 17:52:58 -0700247 * usb_interface_id() - allocate an unused interface ID
248 * @config: configuration associated with the interface
249 * @function: function handling the interface
250 * Context: single threaded during gadget setup
251 *
252 * usb_interface_id() is called from usb_function.bind() callbacks to
253 * allocate new interface IDs. The function driver will then store that
254 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300255 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700256 * particularly changing its altsetting via set_alt(). There may
257 * also be class-specific or vendor-specific requests to handle.
258 *
259 * All interface identifier should be allocated using this routine, to
260 * ensure that for example different functions don't wrongly assign
261 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300262 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700263 * one configuration (or more than once in a given configuration) need
264 * multiple versions of the relevant descriptors.
265 *
266 * Returns the interface ID which was allocated; or -ENODEV if no
267 * more interface IDs can be allocated.
268 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200269int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700270 struct usb_function *function)
271{
272 unsigned id = config->next_interface_id;
273
274 if (id < MAX_CONFIG_INTERFACES) {
275 config->interface[id] = function;
276 config->next_interface_id = id + 1;
277 return id;
278 }
279 return -ENODEV;
280}
281
282static int config_buf(struct usb_configuration *config,
283 enum usb_device_speed speed, void *buf, u8 type)
284{
285 struct usb_config_descriptor *c = buf;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500286 struct usb_interface_descriptor *intf;
David Brownell40982be2008-06-19 17:52:58 -0700287 void *next = buf + USB_DT_CONFIG_SIZE;
288 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
289 struct usb_function *f;
290 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500291 int interfaceCount = 0;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500292 u8 *dest;
David Brownell40982be2008-06-19 17:52:58 -0700293
294 /* write the config descriptor */
295 c = buf;
296 c->bLength = USB_DT_CONFIG_SIZE;
297 c->bDescriptorType = type;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500298 /* wTotalLength and bNumInterfaces are written later */
David Brownell40982be2008-06-19 17:52:58 -0700299 c->bConfigurationValue = config->bConfigurationValue;
300 c->iConfiguration = config->iConfiguration;
301 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700302 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700303
304 /* There may be e.g. OTG descriptors */
305 if (config->descriptors) {
306 status = usb_descriptor_fillbuf(next, len,
307 config->descriptors);
308 if (status < 0)
309 return status;
310 len -= status;
311 next += status;
312 }
313
314 /* add each function's descriptors */
315 list_for_each_entry(f, &config->functions, list) {
316 struct usb_descriptor_header **descriptors;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500317 struct usb_descriptor_header *descriptor;
David Brownell40982be2008-06-19 17:52:58 -0700318
319 if (speed == USB_SPEED_HIGH)
320 descriptors = f->hs_descriptors;
321 else
322 descriptors = f->descriptors;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400323 if (f->disabled || !descriptors || descriptors[0] == NULL)
David Brownell40982be2008-06-19 17:52:58 -0700324 continue;
325 status = usb_descriptor_fillbuf(next, len,
326 (const struct usb_descriptor_header **) descriptors);
327 if (status < 0)
328 return status;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500329
330 /* set interface numbers dynamically */
331 dest = next;
332 while ((descriptor = *descriptors++) != NULL) {
333 intf = (struct usb_interface_descriptor *)dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500334 if (intf->bDescriptorType == USB_DT_INTERFACE) {
335 /* don't increment bInterfaceNumber for alternate settings */
336 if (intf->bAlternateSetting == 0)
337 intf->bInterfaceNumber = interfaceCount++;
338 else
339 intf->bInterfaceNumber = interfaceCount - 1;
340 }
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500341 dest += intf->bLength;
342 }
343
David Brownell40982be2008-06-19 17:52:58 -0700344 len -= status;
345 next += status;
346 }
347
348 len = next - buf;
349 c->wTotalLength = cpu_to_le16(len);
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500350 c->bNumInterfaces = interfaceCount;
David Brownell40982be2008-06-19 17:52:58 -0700351 return len;
352}
353
354static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
355{
356 struct usb_gadget *gadget = cdev->gadget;
357 struct usb_configuration *c;
358 u8 type = w_value >> 8;
359 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
360
361 if (gadget_is_dualspeed(gadget)) {
362 int hs = 0;
363
364 if (gadget->speed == USB_SPEED_HIGH)
365 hs = 1;
366 if (type == USB_DT_OTHER_SPEED_CONFIG)
367 hs = !hs;
368 if (hs)
369 speed = USB_SPEED_HIGH;
370
371 }
372
373 /* This is a lookup by config *INDEX* */
374 w_value &= 0xff;
375 list_for_each_entry(c, &cdev->configs, list) {
376 /* ignore configs that won't work at this speed */
377 if (speed == USB_SPEED_HIGH) {
378 if (!c->highspeed)
379 continue;
380 } else {
381 if (!c->fullspeed)
382 continue;
383 }
384 if (w_value == 0)
385 return config_buf(c, speed, cdev->req->buf, type);
386 w_value--;
387 }
388 return -EINVAL;
389}
390
391static int count_configs(struct usb_composite_dev *cdev, unsigned type)
392{
393 struct usb_gadget *gadget = cdev->gadget;
394 struct usb_configuration *c;
395 unsigned count = 0;
396 int hs = 0;
397
398 if (gadget_is_dualspeed(gadget)) {
399 if (gadget->speed == USB_SPEED_HIGH)
400 hs = 1;
401 if (type == USB_DT_DEVICE_QUALIFIER)
402 hs = !hs;
403 }
404 list_for_each_entry(c, &cdev->configs, list) {
405 /* ignore configs that won't work at this speed */
406 if (hs) {
407 if (!c->highspeed)
408 continue;
409 } else {
410 if (!c->fullspeed)
411 continue;
412 }
413 count++;
414 }
415 return count;
416}
417
418static void device_qual(struct usb_composite_dev *cdev)
419{
420 struct usb_qualifier_descriptor *qual = cdev->req->buf;
421
422 qual->bLength = sizeof(*qual);
423 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
424 /* POLICY: same bcdUSB and device type info at both speeds */
425 qual->bcdUSB = cdev->desc.bcdUSB;
426 qual->bDeviceClass = cdev->desc.bDeviceClass;
427 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
428 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
429 /* ASSUME same EP0 fifo size at both speeds */
430 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
431 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700432 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700433}
434
435/*-------------------------------------------------------------------------*/
436
437static void reset_config(struct usb_composite_dev *cdev)
438{
439 struct usb_function *f;
440
441 DBG(cdev, "reset config\n");
442
443 list_for_each_entry(f, &cdev->config->functions, list) {
444 if (f->disable)
445 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200446
447 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700448 }
449 cdev->config = NULL;
450}
451
452static int set_config(struct usb_composite_dev *cdev,
453 const struct usb_ctrlrequest *ctrl, unsigned number)
454{
455 struct usb_gadget *gadget = cdev->gadget;
456 struct usb_configuration *c = NULL;
457 int result = -EINVAL;
458 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
459 int tmp;
460
461 if (cdev->config)
462 reset_config(cdev);
463
464 if (number) {
465 list_for_each_entry(c, &cdev->configs, list) {
466 if (c->bConfigurationValue == number) {
467 result = 0;
468 break;
469 }
470 }
471 if (result < 0)
472 goto done;
473 } else
474 result = 0;
475
476 INFO(cdev, "%s speed config #%d: %s\n",
477 ({ char *speed;
478 switch (gadget->speed) {
479 case USB_SPEED_LOW: speed = "low"; break;
480 case USB_SPEED_FULL: speed = "full"; break;
481 case USB_SPEED_HIGH: speed = "high"; break;
482 default: speed = "?"; break;
483 } ; speed; }), number, c ? c->label : "unconfigured");
484
485 if (!c)
486 goto done;
487
488 cdev->config = c;
489
490 /* Initialize all interfaces by setting them to altsetting zero. */
491 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
492 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200493 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700494
495 if (!f)
496 break;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400497 if (f->disabled)
Mike Lockwood8c6a6b22010-02-26 09:30:01 -0500498 continue;
David Brownell40982be2008-06-19 17:52:58 -0700499
Laurent Pinchart52426582009-10-21 00:03:38 +0200500 /*
501 * Record which endpoints are used by the function. This is used
502 * to dispatch control requests targeted at that endpoint to the
503 * function's setup callback instead of the current
504 * configuration's setup callback.
505 */
506 if (gadget->speed == USB_SPEED_HIGH)
507 descriptors = f->hs_descriptors;
508 else
509 descriptors = f->descriptors;
510
511 for (; *descriptors; ++descriptors) {
512 struct usb_endpoint_descriptor *ep;
513 int addr;
514
515 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
516 continue;
517
518 ep = (struct usb_endpoint_descriptor *)*descriptors;
519 addr = ((ep->bEndpointAddress & 0x80) >> 3)
520 | (ep->bEndpointAddress & 0x0f);
521 set_bit(addr, f->endpoints);
522 }
523
David Brownell40982be2008-06-19 17:52:58 -0700524 result = f->set_alt(f, tmp, 0);
525 if (result < 0) {
526 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
527 tmp, f->name, f, result);
528
529 reset_config(cdev);
530 goto done;
531 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300532
533 if (result == USB_GADGET_DELAYED_STATUS) {
534 DBG(cdev,
535 "%s: interface %d (%s) requested delayed status\n",
536 __func__, tmp, f->name);
537 cdev->delayed_status++;
538 DBG(cdev, "delayed_status count %d\n",
539 cdev->delayed_status);
540 }
David Brownell40982be2008-06-19 17:52:58 -0700541 }
542
543 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700544 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700545done:
546 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400547
Mike Lockwood8da4cc82010-06-27 20:05:55 -0400548 schedule_work(&cdev->switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400549
Roger Quadros1b9ba002011-05-09 13:08:06 +0300550 if (result >= 0 && cdev->delayed_status)
551 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700552 return result;
553}
554
555/**
556 * usb_add_config() - add a configuration to a device.
557 * @cdev: wraps the USB gadget
558 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200559 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700560 * Context: single threaded during gadget setup
561 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200562 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700563 * add each of the configurations it supports, using this routine.
564 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200565 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700566 * is zero for success else a negative errno value. Binding configurations
567 * assigns global resources including string IDs, and per-configuration
568 * resources such as interface IDs and endpoints.
569 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200570int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200571 struct usb_configuration *config,
572 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700573{
574 int status = -EINVAL;
575 struct usb_configuration *c;
576
577 DBG(cdev, "adding config #%u '%s'/%p\n",
578 config->bConfigurationValue,
579 config->label, config);
580
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200581 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700582 goto done;
583
584 /* Prevent duplicate configuration identifiers */
585 list_for_each_entry(c, &cdev->configs, list) {
586 if (c->bConfigurationValue == config->bConfigurationValue) {
587 status = -EBUSY;
588 goto done;
589 }
590 }
591
592 config->cdev = cdev;
593 list_add_tail(&config->list, &cdev->configs);
594
595 INIT_LIST_HEAD(&config->functions);
596 config->next_interface_id = 0;
597
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200598 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700599 if (status < 0) {
600 list_del(&config->list);
601 config->cdev = NULL;
602 } else {
603 unsigned i;
604
605 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
606 config->bConfigurationValue, config,
607 config->highspeed ? " high" : "",
608 config->fullspeed
609 ? (gadget_is_dualspeed(cdev->gadget)
610 ? " full"
611 : " full/low")
612 : "");
613
614 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
615 struct usb_function *f = config->interface[i];
616
617 if (!f)
618 continue;
619 DBG(cdev, " interface %d = %s/%p\n",
620 i, f->name, f);
621 }
622 }
623
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200624 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700625 * ep->driver_data as needed.
626 */
627 usb_ep_autoconfig_reset(cdev->gadget);
628
629done:
630 if (status)
631 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
632 config->bConfigurationValue, status);
633 return status;
634}
635
636/*-------------------------------------------------------------------------*/
637
638/* We support strings in multiple languages ... string descriptor zero
639 * says which languages are supported. The typical case will be that
640 * only one language (probably English) is used, with I18N handled on
641 * the host side.
642 */
643
644static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
645{
646 const struct usb_gadget_strings *s;
647 u16 language;
648 __le16 *tmp;
649
650 while (*sp) {
651 s = *sp;
652 language = cpu_to_le16(s->language);
653 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
654 if (*tmp == language)
655 goto repeat;
656 }
657 *tmp++ = language;
658repeat:
659 sp++;
660 }
661}
662
663static int lookup_string(
664 struct usb_gadget_strings **sp,
665 void *buf,
666 u16 language,
667 int id
668)
669{
670 struct usb_gadget_strings *s;
671 int value;
672
673 while (*sp) {
674 s = *sp++;
675 if (s->language != language)
676 continue;
677 value = usb_gadget_get_string(s, id, buf);
678 if (value > 0)
679 return value;
680 }
681 return -EINVAL;
682}
683
684static int get_string(struct usb_composite_dev *cdev,
685 void *buf, u16 language, int id)
686{
687 struct usb_configuration *c;
688 struct usb_function *f;
689 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200690 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700691
692 /* Yes, not only is USB's I18N support probably more than most
693 * folk will ever care about ... also, it's all supported here.
694 * (Except for UTF8 support for Unicode's "Astral Planes".)
695 */
696
697 /* 0 == report all available language codes */
698 if (id == 0) {
699 struct usb_string_descriptor *s = buf;
700 struct usb_gadget_strings **sp;
701
702 memset(s, 0, 256);
703 s->bDescriptorType = USB_DT_STRING;
704
705 sp = composite->strings;
706 if (sp)
707 collect_langs(sp, s->wData);
708
709 list_for_each_entry(c, &cdev->configs, list) {
710 sp = c->strings;
711 if (sp)
712 collect_langs(sp, s->wData);
713
714 list_for_each_entry(f, &c->functions, list) {
715 sp = f->strings;
716 if (sp)
717 collect_langs(sp, s->wData);
718 }
719 }
720
Roel Kluin417b57b2009-08-06 16:09:51 -0700721 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700722 continue;
723 if (!len)
724 return -EINVAL;
725
726 s->bLength = 2 * (len + 1);
727 return s->bLength;
728 }
729
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200730 /* Otherwise, look up and return a specified string. First
731 * check if the string has not been overridden.
732 */
733 if (cdev->manufacturer_override == id)
734 str = iManufacturer ?: composite->iManufacturer ?:
735 composite_manufacturer;
736 else if (cdev->product_override == id)
737 str = iProduct ?: composite->iProduct;
738 else if (cdev->serial_override == id)
739 str = iSerialNumber;
740 else
741 str = NULL;
742 if (str) {
743 struct usb_gadget_strings strings = {
744 .language = language,
745 .strings = &(struct usb_string) { 0xff, str }
746 };
747 return usb_gadget_get_string(&strings, 0xff, buf);
748 }
749
750 /* String IDs are device-scoped, so we look up each string
751 * table we're told about. These lookups are infrequent;
752 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700753 */
754 if (composite->strings) {
755 len = lookup_string(composite->strings, buf, language, id);
756 if (len > 0)
757 return len;
758 }
759 list_for_each_entry(c, &cdev->configs, list) {
760 if (c->strings) {
761 len = lookup_string(c->strings, buf, language, id);
762 if (len > 0)
763 return len;
764 }
765 list_for_each_entry(f, &c->functions, list) {
766 if (!f->strings)
767 continue;
768 len = lookup_string(f->strings, buf, language, id);
769 if (len > 0)
770 return len;
771 }
772 }
773 return -EINVAL;
774}
775
776/**
777 * usb_string_id() - allocate an unused string ID
778 * @cdev: the device whose string descriptor IDs are being allocated
779 * Context: single threaded during gadget setup
780 *
781 * @usb_string_id() is called from bind() callbacks to allocate
782 * string IDs. Drivers for functions, configurations, or gadgets will
783 * then store that ID in the appropriate descriptors and string table.
784 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200785 * All string identifier should be allocated using this,
786 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
787 * that for example different functions don't wrongly assign different
788 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700789 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200790int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700791{
792 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200793 /* string id 0 is reserved by USB spec for list of
794 * supported languages */
795 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700796 cdev->next_string_id++;
797 return cdev->next_string_id;
798 }
799 return -ENODEV;
800}
801
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200802/**
803 * usb_string_ids() - allocate unused string IDs in batch
804 * @cdev: the device whose string descriptor IDs are being allocated
805 * @str: an array of usb_string objects to assign numbers to
806 * Context: single threaded during gadget setup
807 *
808 * @usb_string_ids() is called from bind() callbacks to allocate
809 * string IDs. Drivers for functions, configurations, or gadgets will
810 * then copy IDs from the string table to the appropriate descriptors
811 * and string table for other languages.
812 *
813 * All string identifier should be allocated using this,
814 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
815 * example different functions don't wrongly assign different meanings
816 * to the same identifier.
817 */
818int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
819{
820 int next = cdev->next_string_id;
821
822 for (; str->s; ++str) {
823 if (unlikely(next >= 254))
824 return -ENODEV;
825 str->id = ++next;
826 }
827
828 cdev->next_string_id = next;
829
830 return 0;
831}
832
833/**
834 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700835 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200836 * @n: number of string IDs to allocate
837 * Context: single threaded during gadget setup
838 *
839 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700840 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200841 * is, returns last requested ID which is now very useful information.
842 *
843 * @usb_string_ids_n() is called from bind() callbacks to allocate
844 * string IDs. Drivers for functions, configurations, or gadgets will
845 * then store that ID in the appropriate descriptors and string table.
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_n(struct usb_composite_dev *c, unsigned n)
853{
854 unsigned next = c->next_string_id;
855 if (unlikely(n > 254 || (unsigned)next + n > 254))
856 return -ENODEV;
857 c->next_string_id += n;
858 return next + 1;
859}
860
861
David Brownell40982be2008-06-19 17:52:58 -0700862/*-------------------------------------------------------------------------*/
863
864static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
865{
866 if (req->status || req->actual != req->length)
867 DBG((struct usb_composite_dev *) ep->driver_data,
868 "setup complete --> %d, %d/%d\n",
869 req->status, req->actual, req->length);
870}
871
872/*
873 * The setup() callback implements all the ep0 functionality that's
874 * not handled lower down, in hardware or the hardware driver(like
875 * device and endpoint feature flags, and their status). It's all
876 * housekeeping for the gadget function we're implementing. Most of
877 * the work is in config and function specific setup.
878 */
879static int
880composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
881{
882 struct usb_composite_dev *cdev = get_gadget_data(gadget);
883 struct usb_request *req = cdev->req;
884 int value = -EOPNOTSUPP;
885 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800886 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700887 u16 w_value = le16_to_cpu(ctrl->wValue);
888 u16 w_length = le16_to_cpu(ctrl->wLength);
889 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200890 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700891
892 /* partial re-init of the response message; the function or the
893 * gadget might need to intercept e.g. a control-OUT completion
894 * when we delegate to it.
895 */
896 req->zero = 0;
897 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530898 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700899 gadget->ep0->driver_data = cdev;
900
901 switch (ctrl->bRequest) {
902
903 /* we handle all standard USB descriptors */
904 case USB_REQ_GET_DESCRIPTOR:
905 if (ctrl->bRequestType != USB_DIR_IN)
906 goto unknown;
907 switch (w_value >> 8) {
908
909 case USB_DT_DEVICE:
910 cdev->desc.bNumConfigurations =
911 count_configs(cdev, USB_DT_DEVICE);
912 value = min(w_length, (u16) sizeof cdev->desc);
913 memcpy(req->buf, &cdev->desc, value);
914 break;
915 case USB_DT_DEVICE_QUALIFIER:
916 if (!gadget_is_dualspeed(gadget))
917 break;
918 device_qual(cdev);
919 value = min_t(int, w_length,
920 sizeof(struct usb_qualifier_descriptor));
921 break;
922 case USB_DT_OTHER_SPEED_CONFIG:
923 if (!gadget_is_dualspeed(gadget))
924 break;
925 /* FALLTHROUGH */
926 case USB_DT_CONFIG:
927 value = config_desc(cdev, w_value);
928 if (value >= 0)
929 value = min(w_length, (u16) value);
930 break;
931 case USB_DT_STRING:
932 value = get_string(cdev, req->buf,
933 w_index, w_value & 0xff);
Mike Lockwoodfb52b002010-04-16 15:32:15 -0400934
935 /* Allow functions to handle USB_DT_STRING.
936 * This is required for MTP.
937 */
938 if (value < 0) {
939 struct usb_configuration *cfg;
940 list_for_each_entry(cfg, &cdev->configs, list) {
941 if (cfg && cfg->setup) {
942 value = cfg->setup(cfg, ctrl);
943 if (value >= 0)
944 break;
945 }
946 }
947 }
948
David Brownell40982be2008-06-19 17:52:58 -0700949 if (value >= 0)
950 value = min(w_length, (u16) value);
951 break;
952 }
953 break;
954
955 /* any number of configs can work */
956 case USB_REQ_SET_CONFIGURATION:
957 if (ctrl->bRequestType != 0)
958 goto unknown;
959 if (gadget_is_otg(gadget)) {
960 if (gadget->a_hnp_support)
961 DBG(cdev, "HNP available\n");
962 else if (gadget->a_alt_hnp_support)
963 DBG(cdev, "HNP on another port\n");
964 else
965 VDBG(cdev, "HNP inactive\n");
966 }
967 spin_lock(&cdev->lock);
968 value = set_config(cdev, ctrl, w_value);
969 spin_unlock(&cdev->lock);
970 break;
971 case USB_REQ_GET_CONFIGURATION:
972 if (ctrl->bRequestType != USB_DIR_IN)
973 goto unknown;
Jared Suttles258d1032009-08-07 18:57:49 -0500974 if (cdev->config) {
David Brownell40982be2008-06-19 17:52:58 -0700975 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Jared Suttles258d1032009-08-07 18:57:49 -0500976 value = min(w_length, (u16) 1);
977 } else {
David Brownell40982be2008-06-19 17:52:58 -0700978 *(u8 *)req->buf = 0;
Jared Suttles258d1032009-08-07 18:57:49 -0500979 }
David Brownell40982be2008-06-19 17:52:58 -0700980 break;
981
982 /* function drivers must handle get/set altsetting; if there's
983 * no get() method, we know only altsetting zero works.
984 */
985 case USB_REQ_SET_INTERFACE:
986 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
987 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900988 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700989 break;
Bryan Wu08889512009-01-08 00:21:19 +0800990 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700991 if (!f)
992 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800993 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700994 break;
995 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300996 if (value == USB_GADGET_DELAYED_STATUS) {
997 DBG(cdev,
998 "%s: interface %d (%s) requested delayed status\n",
999 __func__, intf, f->name);
1000 cdev->delayed_status++;
1001 DBG(cdev, "delayed_status count %d\n",
1002 cdev->delayed_status);
1003 }
David Brownell40982be2008-06-19 17:52:58 -07001004 break;
1005 case USB_REQ_GET_INTERFACE:
1006 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1007 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001008 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001009 break;
Bryan Wu08889512009-01-08 00:21:19 +08001010 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001011 if (!f)
1012 break;
1013 /* lots of interfaces only need altsetting zero... */
1014 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1015 if (value < 0)
1016 break;
1017 *((u8 *)req->buf) = value;
1018 value = min(w_length, (u16) 1);
1019 break;
1020 default:
1021unknown:
1022 VDBG(cdev,
1023 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1024 ctrl->bRequestType, ctrl->bRequest,
1025 w_value, w_index, w_length);
1026
Laurent Pinchart52426582009-10-21 00:03:38 +02001027 /* functions always handle their interfaces and endpoints...
1028 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001029 * configuration code.
1030 *
1031 * REVISIT it could make sense to let the composite device
1032 * take such requests too, if that's ever needed: to work
1033 * in config 0, etc.
1034 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001035 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1036 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001037 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301038 break;
1039 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001040 break;
1041
1042 case USB_RECIP_ENDPOINT:
1043 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1044 list_for_each_entry(f, &cdev->config->functions, list) {
1045 if (test_bit(endp, f->endpoints))
1046 break;
1047 }
1048 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001049 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001050 break;
David Brownell40982be2008-06-19 17:52:58 -07001051 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001052
1053 if (f && f->setup)
1054 value = f->setup(f, ctrl);
1055 else {
David Brownell40982be2008-06-19 17:52:58 -07001056 struct usb_configuration *c;
1057
1058 c = cdev->config;
1059 if (c && c->setup)
1060 value = c->setup(c, ctrl);
1061 }
1062
Joe Swantek292b1bc2009-12-15 07:17:40 -05001063 /* If the vendor request is not processed (value < 0),
1064 * call all device registered configure setup callbacks
1065 * to process it.
1066 * This is used to handle the following cases:
1067 * - vendor request is for the device and arrives before
1068 * setconfiguration.
1069 * - Some devices are required to handle vendor request before
1070 * setconfiguration such as MTP, USBNET.
1071 */
1072
1073 if (value < 0) {
1074 struct usb_configuration *cfg;
1075
1076 list_for_each_entry(cfg, &cdev->configs, list) {
1077 if (cfg && cfg->setup)
1078 value = cfg->setup(cfg, ctrl);
1079 }
1080 }
1081
David Brownell40982be2008-06-19 17:52:58 -07001082 goto done;
1083 }
1084
1085 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001086 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001087 req->length = value;
1088 req->zero = value < w_length;
1089 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1090 if (value < 0) {
1091 DBG(cdev, "ep_queue --> %d\n", value);
1092 req->status = 0;
1093 composite_setup_complete(gadget->ep0, req);
1094 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001095 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1096 WARN(cdev,
1097 "%s: Delayed status not supported for w_length != 0",
1098 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001099 }
1100
1101done:
1102 /* device either stalls (value < 0) or reports success */
1103 return value;
1104}
1105
1106static void composite_disconnect(struct usb_gadget *gadget)
1107{
1108 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1109 unsigned long flags;
1110
1111 /* REVISIT: should we have config and device level
1112 * disconnect callbacks?
1113 */
1114 spin_lock_irqsave(&cdev->lock, flags);
1115 if (cdev->config)
1116 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001117 if (composite->disconnect)
1118 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001119 spin_unlock_irqrestore(&cdev->lock, flags);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001120
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001121 schedule_work(&cdev->switch_work);
David Brownell40982be2008-06-19 17:52:58 -07001122}
1123
1124/*-------------------------------------------------------------------------*/
1125
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001126static ssize_t composite_show_suspended(struct device *dev,
1127 struct device_attribute *attr,
1128 char *buf)
1129{
1130 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1131 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1132
1133 return sprintf(buf, "%d\n", cdev->suspended);
1134}
1135
1136static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1137
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001138static void
David Brownell40982be2008-06-19 17:52:58 -07001139composite_unbind(struct usb_gadget *gadget)
1140{
1141 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1142
1143 /* composite_disconnect() must already have been called
1144 * by the underlying peripheral controller driver!
1145 * so there's no i/o concurrency that could affect the
1146 * state protected by cdev->lock.
1147 */
1148 WARN_ON(cdev->config);
1149
1150 while (!list_empty(&cdev->configs)) {
1151 struct usb_configuration *c;
1152
1153 c = list_first_entry(&cdev->configs,
1154 struct usb_configuration, list);
1155 while (!list_empty(&c->functions)) {
1156 struct usb_function *f;
1157
1158 f = list_first_entry(&c->functions,
1159 struct usb_function, list);
1160 list_del(&f->list);
1161 if (f->unbind) {
1162 DBG(cdev, "unbind function '%s'/%p\n",
1163 f->name, f);
1164 f->unbind(c, f);
1165 /* may free memory for "f" */
1166 }
1167 }
1168 list_del(&c->list);
1169 if (c->unbind) {
1170 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1171 c->unbind(c);
1172 /* may free memory for "c" */
1173 }
1174 }
1175 if (composite->unbind)
1176 composite->unbind(cdev);
1177
1178 if (cdev->req) {
1179 kfree(cdev->req->buf);
1180 usb_ep_free_request(gadget->ep0, cdev->req);
1181 }
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001182 switch_dev_unregister(&cdev->sdev);
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301183 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001184 kfree(cdev);
1185 set_gadget_data(gadget, NULL);
1186 composite = NULL;
1187}
1188
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001189static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001190{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001191 if (!*desc) {
1192 int ret = usb_string_id(cdev);
1193 if (unlikely(ret < 0))
1194 WARNING(cdev, "failed to override string ID\n");
1195 else
1196 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001197 }
David Brownell40982be2008-06-19 17:52:58 -07001198
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001199 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001200}
1201
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001202static void
1203composite_switch_work(struct work_struct *data)
1204{
1205 struct usb_composite_dev *cdev =
1206 container_of(data, struct usb_composite_dev, switch_work);
1207 struct usb_configuration *config = cdev->config;
1208
1209 if (config)
1210 switch_set_state(&cdev->sdev, config->bConfigurationValue);
1211 else
1212 switch_set_state(&cdev->sdev, 0);
1213}
1214
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001215static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001216{
1217 struct usb_composite_dev *cdev;
1218 int status = -ENOMEM;
1219
1220 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1221 if (!cdev)
1222 return status;
1223
1224 spin_lock_init(&cdev->lock);
1225 cdev->gadget = gadget;
1226 set_gadget_data(gadget, cdev);
1227 INIT_LIST_HEAD(&cdev->configs);
1228
1229 /* preallocate control response and buffer */
1230 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1231 if (!cdev->req)
1232 goto fail;
1233 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1234 if (!cdev->req->buf)
1235 goto fail;
1236 cdev->req->complete = composite_setup_complete;
1237 gadget->ep0->driver_data = cdev;
1238
1239 cdev->bufsiz = USB_BUFSIZ;
1240 cdev->driver = composite;
1241
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301242 /*
1243 * As per USB compliance update, a device that is actively drawing
1244 * more than 100mA from USB must report itself as bus-powered in
1245 * the GetStatus(DEVICE) call.
1246 */
1247 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1248 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001249
1250 /* interface and string IDs start at zero via kzalloc.
1251 * we force endpoints to start unassigned; few controller
1252 * drivers will zero ep->driver_data.
1253 */
1254 usb_ep_autoconfig_reset(cdev->gadget);
1255
1256 /* composite gadget needs to assign strings for whole device (like
1257 * serial number), register function drivers, potentially update
1258 * power state and consumption, etc
1259 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001260 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001261 if (status < 0)
1262 goto fail;
1263
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001264 cdev->sdev.name = "usb_configuration";
1265 status = switch_dev_register(&cdev->sdev);
1266 if (status < 0)
1267 goto fail;
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001268 INIT_WORK(&cdev->switch_work, composite_switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001269
David Brownell40982be2008-06-19 17:52:58 -07001270 cdev->desc = *composite->dev;
1271 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1272
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001273 /* standardized runtime overrides for device ID data */
1274 if (idVendor)
1275 cdev->desc.idVendor = cpu_to_le16(idVendor);
1276 if (idProduct)
1277 cdev->desc.idProduct = cpu_to_le16(idProduct);
1278 if (bcdDevice)
1279 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1280
Marek Belisko78bff3c2010-10-27 10:19:01 +02001281 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001282 if (iManufacturer || !cdev->desc.iManufacturer) {
1283 if (!iManufacturer && !composite->iManufacturer &&
1284 !*composite_manufacturer)
1285 snprintf(composite_manufacturer,
1286 sizeof composite_manufacturer,
1287 "%s %s with %s",
1288 init_utsname()->sysname,
1289 init_utsname()->release,
1290 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001291
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001292 cdev->manufacturer_override =
1293 override_id(cdev, &cdev->desc.iManufacturer);
1294 }
1295
1296 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1297 cdev->product_override =
1298 override_id(cdev, &cdev->desc.iProduct);
1299
1300 if (iSerialNumber)
1301 cdev->serial_override =
1302 override_id(cdev, &cdev->desc.iSerialNumber);
1303
1304 /* has userspace failed to provide a serial number? */
1305 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1306 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1307
1308 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001309 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1310 if (status)
1311 goto fail;
1312
David Brownell40982be2008-06-19 17:52:58 -07001313 INFO(cdev, "%s ready\n", composite->name);
1314 return 0;
1315
1316fail:
1317 composite_unbind(gadget);
1318 return status;
1319}
1320
1321/*-------------------------------------------------------------------------*/
1322
1323static void
1324composite_suspend(struct usb_gadget *gadget)
1325{
1326 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1327 struct usb_function *f;
1328
David Brownell89429392009-03-19 14:14:17 -07001329 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001330 * suspend/resume callbacks?
1331 */
1332 DBG(cdev, "suspend\n");
1333 if (cdev->config) {
1334 list_for_each_entry(f, &cdev->config->functions, list) {
1335 if (f->suspend)
1336 f->suspend(f);
1337 }
1338 }
David Brownell89429392009-03-19 14:14:17 -07001339 if (composite->suspend)
1340 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001341
1342 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001343
1344 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001345}
1346
1347static void
1348composite_resume(struct usb_gadget *gadget)
1349{
1350 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1351 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001352 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001353
David Brownell89429392009-03-19 14:14:17 -07001354 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001355 * suspend/resume callbacks?
1356 */
1357 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001358 if (composite->resume)
1359 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001360 if (cdev->config) {
1361 list_for_each_entry(f, &cdev->config->functions, list) {
1362 if (f->resume)
1363 f->resume(f);
1364 }
Hao Wub23f2f92010-11-29 15:17:03 +08001365
1366 maxpower = cdev->config->bMaxPower;
1367
1368 usb_gadget_vbus_draw(gadget, maxpower ?
1369 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001370 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001371
1372 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001373}
1374
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001375static int
1376composite_uevent(struct device *dev, struct kobj_uevent_env *env)
1377{
1378 struct usb_function *f = dev_get_drvdata(dev);
1379
1380 if (!f) {
1381 /* this happens when the device is first created */
1382 return 0;
1383 }
1384
1385 if (add_uevent_var(env, "FUNCTION=%s", f->name))
1386 return -ENOMEM;
1387 if (add_uevent_var(env, "ENABLED=%d", !f->disabled))
1388 return -ENOMEM;
1389 return 0;
1390}
1391
David Brownell40982be2008-06-19 17:52:58 -07001392/*-------------------------------------------------------------------------*/
1393
1394static struct usb_gadget_driver composite_driver = {
1395 .speed = USB_SPEED_HIGH,
1396
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001397 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001398
1399 .setup = composite_setup,
1400 .disconnect = composite_disconnect,
1401
1402 .suspend = composite_suspend,
1403 .resume = composite_resume,
1404
1405 .driver = {
1406 .owner = THIS_MODULE,
1407 },
1408};
1409
1410/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001411 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001412 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001413 * @bind: the callback used to allocate resources that are shared across the
1414 * whole device, such as string IDs, and add its configurations using
1415 * @usb_add_config(). This may fail by returning a negative errno
1416 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001417 * Context: single threaded during gadget setup
1418 *
1419 * This function is used to register drivers using the composite driver
1420 * framework. The return value is zero, or a negative errno value.
1421 * Those values normally come from the driver's @bind method, which does
1422 * all the work of setting up the driver to match the hardware.
1423 *
1424 * On successful return, the gadget is ready to respond to requests from
1425 * the host, unless one of its components invokes usb_gadget_disconnect()
1426 * while it was binding. That would usually be done in order to wait for
1427 * some userspace participation.
1428 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001429int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001430 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001431{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001432 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001433 return -EINVAL;
1434
1435 if (!driver->name)
1436 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001437 if (!driver->iProduct)
1438 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001439 composite_driver.function = (char *) driver->name;
1440 composite_driver.driver.name = driver->name;
1441 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001442 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001443
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001444 driver->class = class_create(THIS_MODULE, "usb_composite");
1445 if (IS_ERR(driver->class))
1446 return PTR_ERR(driver->class);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001447 driver->class->dev_uevent = composite_uevent;
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001448
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001449 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001450}
1451
1452/**
1453 * usb_composite_unregister() - unregister a composite driver
1454 * @driver: the driver to unregister
1455 *
1456 * This function is used to unregister drivers using the composite
1457 * driver framework.
1458 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001459void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001460{
1461 if (composite != driver)
1462 return;
1463 usb_gadget_unregister_driver(&composite_driver);
1464}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001465
1466/**
1467 * usb_composite_setup_continue() - Continue with the control transfer
1468 * @cdev: the composite device who's control transfer was kept waiting
1469 *
1470 * This function must be called by the USB function driver to continue
1471 * with the control transfer's data/status stage in case it had requested to
1472 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1473 * can request the composite framework to delay the setup request's data/status
1474 * stages by returning USB_GADGET_DELAYED_STATUS.
1475 */
1476void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1477{
1478 int value;
1479 struct usb_request *req = cdev->req;
1480 unsigned long flags;
1481
1482 DBG(cdev, "%s\n", __func__);
1483 spin_lock_irqsave(&cdev->lock, flags);
1484
1485 if (cdev->delayed_status == 0) {
1486 WARN(cdev, "%s: Unexpected call\n", __func__);
1487
1488 } else if (--cdev->delayed_status == 0) {
1489 DBG(cdev, "%s: Completing delayed status\n", __func__);
1490 req->length = 0;
1491 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1492 if (value < 0) {
1493 DBG(cdev, "ep_queue --> %d\n", value);
1494 req->status = 0;
1495 composite_setup_complete(cdev->gadget->ep0, req);
1496 }
1497 }
1498
1499 spin_unlock_irqrestore(&cdev->lock, flags);
1500}
1501