blob: c2684b5adc02c074a4554929ea973c29b52b5c88 [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;
David Brownell40982be2008-06-19 17:52:58 -0700308 void *next = buf + USB_DT_CONFIG_SIZE;
309 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
310 struct usb_function *f;
311 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500312 int interfaceCount = 0;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500313 u8 *dest;
David Brownell40982be2008-06-19 17:52:58 -0700314
315 /* write the config descriptor */
316 c = buf;
317 c->bLength = USB_DT_CONFIG_SIZE;
318 c->bDescriptorType = type;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500319 /* wTotalLength and bNumInterfaces are written later */
David Brownell40982be2008-06-19 17:52:58 -0700320 c->bConfigurationValue = config->bConfigurationValue;
321 c->iConfiguration = config->iConfiguration;
322 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700323 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700324
325 /* There may be e.g. OTG descriptors */
326 if (config->descriptors) {
327 status = usb_descriptor_fillbuf(next, len,
328 config->descriptors);
329 if (status < 0)
330 return status;
331 len -= status;
332 next += status;
333 }
334
335 /* add each function's descriptors */
336 list_for_each_entry(f, &config->functions, list) {
337 struct usb_descriptor_header **descriptors;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500338 struct usb_descriptor_header *descriptor;
David Brownell40982be2008-06-19 17:52:58 -0700339
340 if (speed == USB_SPEED_HIGH)
341 descriptors = f->hs_descriptors;
342 else
343 descriptors = f->descriptors;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400344 if (f->disabled || !descriptors || descriptors[0] == NULL)
David Brownell40982be2008-06-19 17:52:58 -0700345 continue;
346 status = usb_descriptor_fillbuf(next, len,
347 (const struct usb_descriptor_header **) descriptors);
348 if (status < 0)
349 return status;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500350
351 /* set interface numbers dynamically */
352 dest = next;
353 while ((descriptor = *descriptors++) != NULL) {
354 intf = (struct usb_interface_descriptor *)dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500355 if (intf->bDescriptorType == USB_DT_INTERFACE) {
356 /* don't increment bInterfaceNumber for alternate settings */
357 if (intf->bAlternateSetting == 0)
358 intf->bInterfaceNumber = interfaceCount++;
359 else
360 intf->bInterfaceNumber = interfaceCount - 1;
361 }
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500362 dest += intf->bLength;
363 }
364
David Brownell40982be2008-06-19 17:52:58 -0700365 len -= status;
366 next += status;
367 }
368
369 len = next - buf;
370 c->wTotalLength = cpu_to_le16(len);
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500371 c->bNumInterfaces = interfaceCount;
David Brownell40982be2008-06-19 17:52:58 -0700372 return len;
373}
374
375static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
376{
377 struct usb_gadget *gadget = cdev->gadget;
378 struct usb_configuration *c;
379 u8 type = w_value >> 8;
380 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
381
382 if (gadget_is_dualspeed(gadget)) {
383 int hs = 0;
384
385 if (gadget->speed == USB_SPEED_HIGH)
386 hs = 1;
387 if (type == USB_DT_OTHER_SPEED_CONFIG)
388 hs = !hs;
389 if (hs)
390 speed = USB_SPEED_HIGH;
391
392 }
393
394 /* This is a lookup by config *INDEX* */
395 w_value &= 0xff;
396 list_for_each_entry(c, &cdev->configs, list) {
397 /* ignore configs that won't work at this speed */
398 if (speed == USB_SPEED_HIGH) {
399 if (!c->highspeed)
400 continue;
401 } else {
402 if (!c->fullspeed)
403 continue;
404 }
405 if (w_value == 0)
406 return config_buf(c, speed, cdev->req->buf, type);
407 w_value--;
408 }
409 return -EINVAL;
410}
411
412static int count_configs(struct usb_composite_dev *cdev, unsigned type)
413{
414 struct usb_gadget *gadget = cdev->gadget;
415 struct usb_configuration *c;
416 unsigned count = 0;
417 int hs = 0;
418
419 if (gadget_is_dualspeed(gadget)) {
420 if (gadget->speed == USB_SPEED_HIGH)
421 hs = 1;
422 if (type == USB_DT_DEVICE_QUALIFIER)
423 hs = !hs;
424 }
425 list_for_each_entry(c, &cdev->configs, list) {
426 /* ignore configs that won't work at this speed */
427 if (hs) {
428 if (!c->highspeed)
429 continue;
430 } else {
431 if (!c->fullspeed)
432 continue;
433 }
434 count++;
435 }
436 return count;
437}
438
439static void device_qual(struct usb_composite_dev *cdev)
440{
441 struct usb_qualifier_descriptor *qual = cdev->req->buf;
442
443 qual->bLength = sizeof(*qual);
444 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
445 /* POLICY: same bcdUSB and device type info at both speeds */
446 qual->bcdUSB = cdev->desc.bcdUSB;
447 qual->bDeviceClass = cdev->desc.bDeviceClass;
448 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
449 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
450 /* ASSUME same EP0 fifo size at both speeds */
451 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
452 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700453 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700454}
455
456/*-------------------------------------------------------------------------*/
457
458static void reset_config(struct usb_composite_dev *cdev)
459{
460 struct usb_function *f;
461
462 DBG(cdev, "reset config\n");
463
464 list_for_each_entry(f, &cdev->config->functions, list) {
465 if (f->disable)
466 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200467
468 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700469 }
470 cdev->config = NULL;
471}
472
473static int set_config(struct usb_composite_dev *cdev,
474 const struct usb_ctrlrequest *ctrl, unsigned number)
475{
476 struct usb_gadget *gadget = cdev->gadget;
477 struct usb_configuration *c = NULL;
478 int result = -EINVAL;
479 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
480 int tmp;
481
482 if (cdev->config)
483 reset_config(cdev);
484
485 if (number) {
486 list_for_each_entry(c, &cdev->configs, list) {
487 if (c->bConfigurationValue == number) {
488 result = 0;
489 break;
490 }
491 }
492 if (result < 0)
493 goto done;
494 } else
495 result = 0;
496
497 INFO(cdev, "%s speed config #%d: %s\n",
498 ({ char *speed;
499 switch (gadget->speed) {
500 case USB_SPEED_LOW: speed = "low"; break;
501 case USB_SPEED_FULL: speed = "full"; break;
502 case USB_SPEED_HIGH: speed = "high"; break;
503 default: speed = "?"; break;
504 } ; speed; }), number, c ? c->label : "unconfigured");
505
506 if (!c)
507 goto done;
508
509 cdev->config = c;
510
511 /* Initialize all interfaces by setting them to altsetting zero. */
512 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
513 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200514 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700515
516 if (!f)
517 break;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400518 if (f->disabled)
Mike Lockwood8c6a6b22010-02-26 09:30:01 -0500519 continue;
David Brownell40982be2008-06-19 17:52:58 -0700520
Laurent Pinchart52426582009-10-21 00:03:38 +0200521 /*
522 * Record which endpoints are used by the function. This is used
523 * to dispatch control requests targeted at that endpoint to the
524 * function's setup callback instead of the current
525 * configuration's setup callback.
526 */
527 if (gadget->speed == USB_SPEED_HIGH)
528 descriptors = f->hs_descriptors;
529 else
530 descriptors = f->descriptors;
531
532 for (; *descriptors; ++descriptors) {
533 struct usb_endpoint_descriptor *ep;
534 int addr;
535
536 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
537 continue;
538
539 ep = (struct usb_endpoint_descriptor *)*descriptors;
540 addr = ((ep->bEndpointAddress & 0x80) >> 3)
541 | (ep->bEndpointAddress & 0x0f);
542 set_bit(addr, f->endpoints);
543 }
544
David Brownell40982be2008-06-19 17:52:58 -0700545 result = f->set_alt(f, tmp, 0);
546 if (result < 0) {
547 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
548 tmp, f->name, f, result);
549
550 reset_config(cdev);
551 goto done;
552 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300553
554 if (result == USB_GADGET_DELAYED_STATUS) {
555 DBG(cdev,
556 "%s: interface %d (%s) requested delayed status\n",
557 __func__, tmp, f->name);
558 cdev->delayed_status++;
559 DBG(cdev, "delayed_status count %d\n",
560 cdev->delayed_status);
561 }
David Brownell40982be2008-06-19 17:52:58 -0700562 }
563
564 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700565 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700566done:
567 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400568
Mike Lockwood8da4cc82010-06-27 20:05:55 -0400569 schedule_work(&cdev->switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400570
Roger Quadros1b9ba002011-05-09 13:08:06 +0300571 if (result >= 0 && cdev->delayed_status)
572 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700573 return result;
574}
575
576/**
577 * usb_add_config() - add a configuration to a device.
578 * @cdev: wraps the USB gadget
579 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200580 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700581 * Context: single threaded during gadget setup
582 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200583 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700584 * add each of the configurations it supports, using this routine.
585 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200586 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700587 * is zero for success else a negative errno value. Binding configurations
588 * assigns global resources including string IDs, and per-configuration
589 * resources such as interface IDs and endpoints.
590 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200591int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200592 struct usb_configuration *config,
593 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700594{
595 int status = -EINVAL;
596 struct usb_configuration *c;
597
598 DBG(cdev, "adding config #%u '%s'/%p\n",
599 config->bConfigurationValue,
600 config->label, config);
601
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200602 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700603 goto done;
604
605 /* Prevent duplicate configuration identifiers */
606 list_for_each_entry(c, &cdev->configs, list) {
607 if (c->bConfigurationValue == config->bConfigurationValue) {
608 status = -EBUSY;
609 goto done;
610 }
611 }
612
613 config->cdev = cdev;
614 list_add_tail(&config->list, &cdev->configs);
615
616 INIT_LIST_HEAD(&config->functions);
617 config->next_interface_id = 0;
618
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200619 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700620 if (status < 0) {
621 list_del(&config->list);
622 config->cdev = NULL;
623 } else {
624 unsigned i;
625
626 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
627 config->bConfigurationValue, config,
628 config->highspeed ? " high" : "",
629 config->fullspeed
630 ? (gadget_is_dualspeed(cdev->gadget)
631 ? " full"
632 : " full/low")
633 : "");
634
635 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
636 struct usb_function *f = config->interface[i];
637
638 if (!f)
639 continue;
640 DBG(cdev, " interface %d = %s/%p\n",
641 i, f->name, f);
642 }
643 }
644
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200645 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700646 * ep->driver_data as needed.
647 */
648 usb_ep_autoconfig_reset(cdev->gadget);
649
650done:
651 if (status)
652 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
653 config->bConfigurationValue, status);
654 return status;
655}
656
657/*-------------------------------------------------------------------------*/
658
659/* We support strings in multiple languages ... string descriptor zero
660 * says which languages are supported. The typical case will be that
661 * only one language (probably English) is used, with I18N handled on
662 * the host side.
663 */
664
665static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
666{
667 const struct usb_gadget_strings *s;
668 u16 language;
669 __le16 *tmp;
670
671 while (*sp) {
672 s = *sp;
673 language = cpu_to_le16(s->language);
674 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
675 if (*tmp == language)
676 goto repeat;
677 }
678 *tmp++ = language;
679repeat:
680 sp++;
681 }
682}
683
684static int lookup_string(
685 struct usb_gadget_strings **sp,
686 void *buf,
687 u16 language,
688 int id
689)
690{
691 struct usb_gadget_strings *s;
692 int value;
693
694 while (*sp) {
695 s = *sp++;
696 if (s->language != language)
697 continue;
698 value = usb_gadget_get_string(s, id, buf);
699 if (value > 0)
700 return value;
701 }
702 return -EINVAL;
703}
704
705static int get_string(struct usb_composite_dev *cdev,
706 void *buf, u16 language, int id)
707{
708 struct usb_configuration *c;
709 struct usb_function *f;
710 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200711 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700712
713 /* Yes, not only is USB's I18N support probably more than most
714 * folk will ever care about ... also, it's all supported here.
715 * (Except for UTF8 support for Unicode's "Astral Planes".)
716 */
717
718 /* 0 == report all available language codes */
719 if (id == 0) {
720 struct usb_string_descriptor *s = buf;
721 struct usb_gadget_strings **sp;
722
723 memset(s, 0, 256);
724 s->bDescriptorType = USB_DT_STRING;
725
726 sp = composite->strings;
727 if (sp)
728 collect_langs(sp, s->wData);
729
730 list_for_each_entry(c, &cdev->configs, list) {
731 sp = c->strings;
732 if (sp)
733 collect_langs(sp, s->wData);
734
735 list_for_each_entry(f, &c->functions, list) {
736 sp = f->strings;
737 if (sp)
738 collect_langs(sp, s->wData);
739 }
740 }
741
Roel Kluin417b57b2009-08-06 16:09:51 -0700742 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700743 continue;
744 if (!len)
745 return -EINVAL;
746
747 s->bLength = 2 * (len + 1);
748 return s->bLength;
749 }
750
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200751 /* Otherwise, look up and return a specified string. First
752 * check if the string has not been overridden.
753 */
754 if (cdev->manufacturer_override == id)
755 str = iManufacturer ?: composite->iManufacturer ?:
756 composite_manufacturer;
757 else if (cdev->product_override == id)
758 str = iProduct ?: composite->iProduct;
759 else if (cdev->serial_override == id)
760 str = iSerialNumber;
761 else
762 str = NULL;
763 if (str) {
764 struct usb_gadget_strings strings = {
765 .language = language,
766 .strings = &(struct usb_string) { 0xff, str }
767 };
768 return usb_gadget_get_string(&strings, 0xff, buf);
769 }
770
771 /* String IDs are device-scoped, so we look up each string
772 * table we're told about. These lookups are infrequent;
773 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700774 */
775 if (composite->strings) {
776 len = lookup_string(composite->strings, buf, language, id);
777 if (len > 0)
778 return len;
779 }
780 list_for_each_entry(c, &cdev->configs, list) {
781 if (c->strings) {
782 len = lookup_string(c->strings, buf, language, id);
783 if (len > 0)
784 return len;
785 }
786 list_for_each_entry(f, &c->functions, list) {
787 if (!f->strings)
788 continue;
789 len = lookup_string(f->strings, buf, language, id);
790 if (len > 0)
791 return len;
792 }
793 }
794 return -EINVAL;
795}
796
797/**
798 * usb_string_id() - allocate an unused string ID
799 * @cdev: the device whose string descriptor IDs are being allocated
800 * Context: single threaded during gadget setup
801 *
802 * @usb_string_id() is called from bind() callbacks to allocate
803 * string IDs. Drivers for functions, configurations, or gadgets will
804 * then store that ID in the appropriate descriptors and string table.
805 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200806 * All string identifier should be allocated using this,
807 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
808 * that for example different functions don't wrongly assign different
809 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700810 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200811int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700812{
813 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200814 /* string id 0 is reserved by USB spec for list of
815 * supported languages */
816 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700817 cdev->next_string_id++;
818 return cdev->next_string_id;
819 }
820 return -ENODEV;
821}
822
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200823/**
824 * usb_string_ids() - allocate unused string IDs in batch
825 * @cdev: the device whose string descriptor IDs are being allocated
826 * @str: an array of usb_string objects to assign numbers to
827 * Context: single threaded during gadget setup
828 *
829 * @usb_string_ids() is called from bind() callbacks to allocate
830 * string IDs. Drivers for functions, configurations, or gadgets will
831 * then copy IDs from the string table to the appropriate descriptors
832 * and string table for other languages.
833 *
834 * All string identifier should be allocated using this,
835 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
836 * example different functions don't wrongly assign different meanings
837 * to the same identifier.
838 */
839int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
840{
841 int next = cdev->next_string_id;
842
843 for (; str->s; ++str) {
844 if (unlikely(next >= 254))
845 return -ENODEV;
846 str->id = ++next;
847 }
848
849 cdev->next_string_id = next;
850
851 return 0;
852}
853
854/**
855 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700856 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200857 * @n: number of string IDs to allocate
858 * Context: single threaded during gadget setup
859 *
860 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700861 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200862 * is, returns last requested ID which is now very useful information.
863 *
864 * @usb_string_ids_n() is called from bind() callbacks to allocate
865 * string IDs. Drivers for functions, configurations, or gadgets will
866 * then store that ID in the appropriate descriptors and string table.
867 *
868 * All string identifier should be allocated using this,
869 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
870 * example different functions don't wrongly assign different meanings
871 * to the same identifier.
872 */
873int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
874{
875 unsigned next = c->next_string_id;
876 if (unlikely(n > 254 || (unsigned)next + n > 254))
877 return -ENODEV;
878 c->next_string_id += n;
879 return next + 1;
880}
881
882
David Brownell40982be2008-06-19 17:52:58 -0700883/*-------------------------------------------------------------------------*/
884
885static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
886{
887 if (req->status || req->actual != req->length)
888 DBG((struct usb_composite_dev *) ep->driver_data,
889 "setup complete --> %d, %d/%d\n",
890 req->status, req->actual, req->length);
891}
892
893/*
894 * The setup() callback implements all the ep0 functionality that's
895 * not handled lower down, in hardware or the hardware driver(like
896 * device and endpoint feature flags, and their status). It's all
897 * housekeeping for the gadget function we're implementing. Most of
898 * the work is in config and function specific setup.
899 */
900static int
901composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
902{
903 struct usb_composite_dev *cdev = get_gadget_data(gadget);
904 struct usb_request *req = cdev->req;
905 int value = -EOPNOTSUPP;
906 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800907 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700908 u16 w_value = le16_to_cpu(ctrl->wValue);
909 u16 w_length = le16_to_cpu(ctrl->wLength);
910 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200911 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700912
913 /* partial re-init of the response message; the function or the
914 * gadget might need to intercept e.g. a control-OUT completion
915 * when we delegate to it.
916 */
917 req->zero = 0;
918 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530919 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700920 gadget->ep0->driver_data = cdev;
921
922 switch (ctrl->bRequest) {
923
924 /* we handle all standard USB descriptors */
925 case USB_REQ_GET_DESCRIPTOR:
926 if (ctrl->bRequestType != USB_DIR_IN)
927 goto unknown;
928 switch (w_value >> 8) {
929
930 case USB_DT_DEVICE:
931 cdev->desc.bNumConfigurations =
932 count_configs(cdev, USB_DT_DEVICE);
933 value = min(w_length, (u16) sizeof cdev->desc);
934 memcpy(req->buf, &cdev->desc, value);
935 break;
936 case USB_DT_DEVICE_QUALIFIER:
937 if (!gadget_is_dualspeed(gadget))
938 break;
939 device_qual(cdev);
940 value = min_t(int, w_length,
941 sizeof(struct usb_qualifier_descriptor));
942 break;
943 case USB_DT_OTHER_SPEED_CONFIG:
944 if (!gadget_is_dualspeed(gadget))
945 break;
946 /* FALLTHROUGH */
947 case USB_DT_CONFIG:
948 value = config_desc(cdev, w_value);
949 if (value >= 0)
950 value = min(w_length, (u16) value);
951 break;
952 case USB_DT_STRING:
953 value = get_string(cdev, req->buf,
954 w_index, w_value & 0xff);
Mike Lockwoodfb52b002010-04-16 15:32:15 -0400955
956 /* Allow functions to handle USB_DT_STRING.
957 * This is required for MTP.
958 */
959 if (value < 0) {
960 struct usb_configuration *cfg;
961 list_for_each_entry(cfg, &cdev->configs, list) {
962 if (cfg && cfg->setup) {
963 value = cfg->setup(cfg, ctrl);
964 if (value >= 0)
965 break;
966 }
967 }
968 }
969
David Brownell40982be2008-06-19 17:52:58 -0700970 if (value >= 0)
971 value = min(w_length, (u16) value);
972 break;
973 }
974 break;
975
976 /* any number of configs can work */
977 case USB_REQ_SET_CONFIGURATION:
978 if (ctrl->bRequestType != 0)
979 goto unknown;
980 if (gadget_is_otg(gadget)) {
981 if (gadget->a_hnp_support)
982 DBG(cdev, "HNP available\n");
983 else if (gadget->a_alt_hnp_support)
984 DBG(cdev, "HNP on another port\n");
985 else
986 VDBG(cdev, "HNP inactive\n");
987 }
988 spin_lock(&cdev->lock);
989 value = set_config(cdev, ctrl, w_value);
990 spin_unlock(&cdev->lock);
991 break;
992 case USB_REQ_GET_CONFIGURATION:
993 if (ctrl->bRequestType != USB_DIR_IN)
994 goto unknown;
Jared Suttles258d1032009-08-07 18:57:49 -0500995 if (cdev->config) {
David Brownell40982be2008-06-19 17:52:58 -0700996 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Jared Suttles258d1032009-08-07 18:57:49 -0500997 value = min(w_length, (u16) 1);
998 } else {
David Brownell40982be2008-06-19 17:52:58 -0700999 *(u8 *)req->buf = 0;
Jared Suttles258d1032009-08-07 18:57:49 -05001000 }
David Brownell40982be2008-06-19 17:52:58 -07001001 break;
1002
1003 /* function drivers must handle get/set altsetting; if there's
1004 * no get() method, we know only altsetting zero works.
1005 */
1006 case USB_REQ_SET_INTERFACE:
1007 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1008 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001009 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001010 break;
Bryan Wu08889512009-01-08 00:21:19 +08001011 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001012 if (!f)
1013 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001014 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001015 break;
1016 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001017 if (value == USB_GADGET_DELAYED_STATUS) {
1018 DBG(cdev,
1019 "%s: interface %d (%s) requested delayed status\n",
1020 __func__, intf, f->name);
1021 cdev->delayed_status++;
1022 DBG(cdev, "delayed_status count %d\n",
1023 cdev->delayed_status);
1024 }
David Brownell40982be2008-06-19 17:52:58 -07001025 break;
1026 case USB_REQ_GET_INTERFACE:
1027 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1028 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001029 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001030 break;
Bryan Wu08889512009-01-08 00:21:19 +08001031 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001032 if (!f)
1033 break;
1034 /* lots of interfaces only need altsetting zero... */
1035 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1036 if (value < 0)
1037 break;
1038 *((u8 *)req->buf) = value;
1039 value = min(w_length, (u16) 1);
1040 break;
1041 default:
1042unknown:
1043 VDBG(cdev,
1044 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1045 ctrl->bRequestType, ctrl->bRequest,
1046 w_value, w_index, w_length);
1047
Laurent Pinchart52426582009-10-21 00:03:38 +02001048 /* functions always handle their interfaces and endpoints...
1049 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001050 * configuration code.
1051 *
1052 * REVISIT it could make sense to let the composite device
1053 * take such requests too, if that's ever needed: to work
1054 * in config 0, etc.
1055 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001056 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1057 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001058 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301059 break;
1060 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001061 break;
1062
1063 case USB_RECIP_ENDPOINT:
1064 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1065 list_for_each_entry(f, &cdev->config->functions, list) {
1066 if (test_bit(endp, f->endpoints))
1067 break;
1068 }
1069 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001070 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001071 break;
David Brownell40982be2008-06-19 17:52:58 -07001072 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001073
1074 if (f && f->setup)
1075 value = f->setup(f, ctrl);
1076 else {
David Brownell40982be2008-06-19 17:52:58 -07001077 struct usb_configuration *c;
1078
1079 c = cdev->config;
1080 if (c && c->setup)
1081 value = c->setup(c, ctrl);
1082 }
1083
Joe Swantek292b1bc2009-12-15 07:17:40 -05001084 /* If the vendor request is not processed (value < 0),
1085 * call all device registered configure setup callbacks
1086 * to process it.
1087 * This is used to handle the following cases:
1088 * - vendor request is for the device and arrives before
1089 * setconfiguration.
1090 * - Some devices are required to handle vendor request before
1091 * setconfiguration such as MTP, USBNET.
1092 */
1093
1094 if (value < 0) {
1095 struct usb_configuration *cfg;
1096
1097 list_for_each_entry(cfg, &cdev->configs, list) {
1098 if (cfg && cfg->setup)
1099 value = cfg->setup(cfg, ctrl);
1100 }
1101 }
1102
David Brownell40982be2008-06-19 17:52:58 -07001103 goto done;
1104 }
1105
1106 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001107 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001108 req->length = value;
1109 req->zero = value < w_length;
1110 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1111 if (value < 0) {
1112 DBG(cdev, "ep_queue --> %d\n", value);
1113 req->status = 0;
1114 composite_setup_complete(gadget->ep0, req);
1115 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001116 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1117 WARN(cdev,
1118 "%s: Delayed status not supported for w_length != 0",
1119 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001120 }
1121
1122done:
1123 /* device either stalls (value < 0) or reports success */
1124 return value;
1125}
1126
1127static void composite_disconnect(struct usb_gadget *gadget)
1128{
1129 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1130 unsigned long flags;
1131
1132 /* REVISIT: should we have config and device level
1133 * disconnect callbacks?
1134 */
1135 spin_lock_irqsave(&cdev->lock, flags);
1136 if (cdev->config)
1137 reset_config(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001138
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001139 if (composite->disconnect)
1140 composite->disconnect(cdev);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001141
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001142 if (cdev->mute_switch)
1143 cdev->mute_switch = 0;
1144 else
1145 schedule_work(&cdev->switch_work);
1146 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001147}
1148
1149/*-------------------------------------------------------------------------*/
1150
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001151static ssize_t composite_show_suspended(struct device *dev,
1152 struct device_attribute *attr,
1153 char *buf)
1154{
1155 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1156 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1157
1158 return sprintf(buf, "%d\n", cdev->suspended);
1159}
1160
1161static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1162
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001163static void
David Brownell40982be2008-06-19 17:52:58 -07001164composite_unbind(struct usb_gadget *gadget)
1165{
1166 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1167
1168 /* composite_disconnect() must already have been called
1169 * by the underlying peripheral controller driver!
1170 * so there's no i/o concurrency that could affect the
1171 * state protected by cdev->lock.
1172 */
1173 WARN_ON(cdev->config);
1174
1175 while (!list_empty(&cdev->configs)) {
1176 struct usb_configuration *c;
1177
1178 c = list_first_entry(&cdev->configs,
1179 struct usb_configuration, list);
1180 while (!list_empty(&c->functions)) {
1181 struct usb_function *f;
1182
1183 f = list_first_entry(&c->functions,
1184 struct usb_function, list);
1185 list_del(&f->list);
1186 if (f->unbind) {
1187 DBG(cdev, "unbind function '%s'/%p\n",
1188 f->name, f);
1189 f->unbind(c, f);
1190 /* may free memory for "f" */
1191 }
1192 }
1193 list_del(&c->list);
1194 if (c->unbind) {
1195 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1196 c->unbind(c);
1197 /* may free memory for "c" */
1198 }
1199 }
1200 if (composite->unbind)
1201 composite->unbind(cdev);
1202
1203 if (cdev->req) {
1204 kfree(cdev->req->buf);
1205 usb_ep_free_request(gadget->ep0, cdev->req);
1206 }
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001207 switch_dev_unregister(&cdev->sdev);
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301208 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001209 kfree(cdev);
1210 set_gadget_data(gadget, NULL);
1211 composite = NULL;
1212}
1213
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001214static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001215{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001216 if (!*desc) {
1217 int ret = usb_string_id(cdev);
1218 if (unlikely(ret < 0))
1219 WARNING(cdev, "failed to override string ID\n");
1220 else
1221 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001222 }
David Brownell40982be2008-06-19 17:52:58 -07001223
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001224 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001225}
1226
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001227static void
1228composite_switch_work(struct work_struct *data)
1229{
1230 struct usb_composite_dev *cdev =
1231 container_of(data, struct usb_composite_dev, switch_work);
1232 struct usb_configuration *config = cdev->config;
1233
1234 if (config)
1235 switch_set_state(&cdev->sdev, config->bConfigurationValue);
1236 else
1237 switch_set_state(&cdev->sdev, 0);
1238}
1239
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001240static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001241{
1242 struct usb_composite_dev *cdev;
1243 int status = -ENOMEM;
1244
1245 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1246 if (!cdev)
1247 return status;
1248
1249 spin_lock_init(&cdev->lock);
1250 cdev->gadget = gadget;
1251 set_gadget_data(gadget, cdev);
1252 INIT_LIST_HEAD(&cdev->configs);
1253
1254 /* preallocate control response and buffer */
1255 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1256 if (!cdev->req)
1257 goto fail;
1258 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1259 if (!cdev->req->buf)
1260 goto fail;
1261 cdev->req->complete = composite_setup_complete;
1262 gadget->ep0->driver_data = cdev;
1263
1264 cdev->bufsiz = USB_BUFSIZ;
1265 cdev->driver = composite;
1266
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301267 /*
1268 * As per USB compliance update, a device that is actively drawing
1269 * more than 100mA from USB must report itself as bus-powered in
1270 * the GetStatus(DEVICE) call.
1271 */
1272 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1273 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001274
1275 /* interface and string IDs start at zero via kzalloc.
1276 * we force endpoints to start unassigned; few controller
1277 * drivers will zero ep->driver_data.
1278 */
1279 usb_ep_autoconfig_reset(cdev->gadget);
1280
1281 /* composite gadget needs to assign strings for whole device (like
1282 * serial number), register function drivers, potentially update
1283 * power state and consumption, etc
1284 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001285 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001286 if (status < 0)
1287 goto fail;
1288
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001289 cdev->sdev.name = "usb_configuration";
1290 status = switch_dev_register(&cdev->sdev);
1291 if (status < 0)
1292 goto fail;
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001293 INIT_WORK(&cdev->switch_work, composite_switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001294
David Brownell40982be2008-06-19 17:52:58 -07001295 cdev->desc = *composite->dev;
1296 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1297
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001298 /* standardized runtime overrides for device ID data */
1299 if (idVendor)
1300 cdev->desc.idVendor = cpu_to_le16(idVendor);
1301 if (idProduct)
1302 cdev->desc.idProduct = cpu_to_le16(idProduct);
1303 if (bcdDevice)
1304 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1305
Marek Belisko78bff3c2010-10-27 10:19:01 +02001306 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001307 if (iManufacturer || !cdev->desc.iManufacturer) {
1308 if (!iManufacturer && !composite->iManufacturer &&
1309 !*composite_manufacturer)
1310 snprintf(composite_manufacturer,
1311 sizeof composite_manufacturer,
1312 "%s %s with %s",
1313 init_utsname()->sysname,
1314 init_utsname()->release,
1315 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001316
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001317 cdev->manufacturer_override =
1318 override_id(cdev, &cdev->desc.iManufacturer);
1319 }
1320
1321 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1322 cdev->product_override =
1323 override_id(cdev, &cdev->desc.iProduct);
1324
1325 if (iSerialNumber)
1326 cdev->serial_override =
1327 override_id(cdev, &cdev->desc.iSerialNumber);
1328
1329 /* has userspace failed to provide a serial number? */
1330 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1331 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1332
1333 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001334 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1335 if (status)
1336 goto fail;
1337
David Brownell40982be2008-06-19 17:52:58 -07001338 INFO(cdev, "%s ready\n", composite->name);
1339 return 0;
1340
1341fail:
1342 composite_unbind(gadget);
1343 return status;
1344}
1345
1346/*-------------------------------------------------------------------------*/
1347
1348static void
1349composite_suspend(struct usb_gadget *gadget)
1350{
1351 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1352 struct usb_function *f;
1353
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, "suspend\n");
1358 if (cdev->config) {
1359 list_for_each_entry(f, &cdev->config->functions, list) {
1360 if (f->suspend)
1361 f->suspend(f);
1362 }
1363 }
David Brownell89429392009-03-19 14:14:17 -07001364 if (composite->suspend)
1365 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001366
1367 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001368
1369 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001370}
1371
1372static void
1373composite_resume(struct usb_gadget *gadget)
1374{
1375 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1376 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001377 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001378
David Brownell89429392009-03-19 14:14:17 -07001379 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001380 * suspend/resume callbacks?
1381 */
1382 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001383 if (composite->resume)
1384 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001385 if (cdev->config) {
1386 list_for_each_entry(f, &cdev->config->functions, list) {
1387 if (f->resume)
1388 f->resume(f);
1389 }
Hao Wub23f2f92010-11-29 15:17:03 +08001390
1391 maxpower = cdev->config->bMaxPower;
1392
1393 usb_gadget_vbus_draw(gadget, maxpower ?
1394 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001395 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001396
1397 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001398}
1399
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001400static int
1401composite_uevent(struct device *dev, struct kobj_uevent_env *env)
1402{
1403 struct usb_function *f = dev_get_drvdata(dev);
1404
1405 if (!f) {
1406 /* this happens when the device is first created */
1407 return 0;
1408 }
1409
1410 if (add_uevent_var(env, "FUNCTION=%s", f->name))
1411 return -ENOMEM;
1412 if (add_uevent_var(env, "ENABLED=%d", !f->disabled))
1413 return -ENOMEM;
1414 return 0;
1415}
1416
David Brownell40982be2008-06-19 17:52:58 -07001417/*-------------------------------------------------------------------------*/
1418
1419static struct usb_gadget_driver composite_driver = {
1420 .speed = USB_SPEED_HIGH,
1421
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001422 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001423
1424 .setup = composite_setup,
1425 .disconnect = composite_disconnect,
1426
1427 .suspend = composite_suspend,
1428 .resume = composite_resume,
1429
1430 .driver = {
1431 .owner = THIS_MODULE,
1432 },
1433};
1434
1435/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001436 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001437 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001438 * @bind: the callback used to allocate resources that are shared across the
1439 * whole device, such as string IDs, and add its configurations using
1440 * @usb_add_config(). This may fail by returning a negative errno
1441 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001442 * Context: single threaded during gadget setup
1443 *
1444 * This function is used to register drivers using the composite driver
1445 * framework. The return value is zero, or a negative errno value.
1446 * Those values normally come from the driver's @bind method, which does
1447 * all the work of setting up the driver to match the hardware.
1448 *
1449 * On successful return, the gadget is ready to respond to requests from
1450 * the host, unless one of its components invokes usb_gadget_disconnect()
1451 * while it was binding. That would usually be done in order to wait for
1452 * some userspace participation.
1453 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001454int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001455 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001456{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001457 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001458 return -EINVAL;
1459
1460 if (!driver->name)
1461 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001462 if (!driver->iProduct)
1463 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001464 composite_driver.function = (char *) driver->name;
1465 composite_driver.driver.name = driver->name;
1466 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001467 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001468
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001469 driver->class = class_create(THIS_MODULE, "usb_composite");
1470 if (IS_ERR(driver->class))
1471 return PTR_ERR(driver->class);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001472 driver->class->dev_uevent = composite_uevent;
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001473
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001474 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001475}
1476
1477/**
1478 * usb_composite_unregister() - unregister a composite driver
1479 * @driver: the driver to unregister
1480 *
1481 * This function is used to unregister drivers using the composite
1482 * driver framework.
1483 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001484void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001485{
1486 if (composite != driver)
1487 return;
1488 usb_gadget_unregister_driver(&composite_driver);
1489}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001490
1491/**
1492 * usb_composite_setup_continue() - Continue with the control transfer
1493 * @cdev: the composite device who's control transfer was kept waiting
1494 *
1495 * This function must be called by the USB function driver to continue
1496 * with the control transfer's data/status stage in case it had requested to
1497 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1498 * can request the composite framework to delay the setup request's data/status
1499 * stages by returning USB_GADGET_DELAYED_STATUS.
1500 */
1501void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1502{
1503 int value;
1504 struct usb_request *req = cdev->req;
1505 unsigned long flags;
1506
1507 DBG(cdev, "%s\n", __func__);
1508 spin_lock_irqsave(&cdev->lock, flags);
1509
1510 if (cdev->delayed_status == 0) {
1511 WARN(cdev, "%s: Unexpected call\n", __func__);
1512
1513 } else if (--cdev->delayed_status == 0) {
1514 DBG(cdev, "%s: Completing delayed status\n", __func__);
1515 req->length = 0;
1516 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1517 if (value < 0) {
1518 DBG(cdev, "ep_queue --> %d\n", value);
1519 req->status = 0;
1520 composite_setup_complete(cdev->gadget->ep0, req);
1521 }
1522 }
1523
1524 spin_unlock_irqrestore(&cdev->lock, flags);
1525}
1526