blob: 42a948e49323e049ec247ea8df05d3bde2abfa22 [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/* #define VERBOSE_DEBUG */
22
23#include <linux/kallsyms.h>
24#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020027#include <linux/utsname.h>
Vikram Pandita8abaec82011-02-07 21:20:50 -080028#include <linux/delay.h>
29#include <linux/kdev_t.h>
David Brownell40982be2008-06-19 17:52:58 -070030#include <linux/usb/composite.h>
31
32
33/*
34 * The code in this file is utility code, used to build a gadget driver
35 * from one or more "function" drivers, one or more "configuration"
36 * objects, and a "usb_composite_driver" by gluing them together along
37 * with the relevant device-wide data.
38 */
39
40/* big enough to hold our biggest descriptor */
Robert Lukassendd0543e2010-03-30 14:14:01 +020041#define USB_BUFSIZ 1024
David Brownell40982be2008-06-19 17:52:58 -070042
43static struct usb_composite_driver *composite;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +020044static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
David Brownell40982be2008-06-19 17:52:58 -070045
Lucas De Marchi25985ed2011-03-30 22:57:33 -030046/* Some systems will need runtime overrides for the product identifiers
David Brownell40982be2008-06-19 17:52:58 -070047 * published in the device descriptor, either numbers or strings or both.
48 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
49 */
50
51static ushort idVendor;
52module_param(idVendor, ushort, 0);
53MODULE_PARM_DESC(idVendor, "USB Vendor ID");
54
55static ushort idProduct;
56module_param(idProduct, ushort, 0);
57MODULE_PARM_DESC(idProduct, "USB Product ID");
58
59static ushort bcdDevice;
60module_param(bcdDevice, ushort, 0);
61MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
62
63static char *iManufacturer;
64module_param(iManufacturer, charp, 0);
65MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
66
67static char *iProduct;
68module_param(iProduct, charp, 0);
69MODULE_PARM_DESC(iProduct, "USB Product string");
70
71static char *iSerialNumber;
72module_param(iSerialNumber, charp, 0);
73MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
74
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020075static char composite_manufacturer[50];
76
David Brownell40982be2008-06-19 17:52:58 -070077/*-------------------------------------------------------------------------*/
78
Mike Lockwoodf041ac62010-02-06 21:53:51 -050079static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
80 char *buf)
81{
82 struct usb_function *f = dev_get_drvdata(dev);
Mike Lockwoode2dc5032010-06-23 08:20:59 -040083 return sprintf(buf, "%d\n", !f->disabled);
Mike Lockwoodf041ac62010-02-06 21:53:51 -050084}
85
86static ssize_t enable_store(
87 struct device *dev, struct device_attribute *attr,
88 const char *buf, size_t size)
89{
90 struct usb_function *f = dev_get_drvdata(dev);
91 struct usb_composite_driver *driver = f->config->cdev->driver;
92 int value;
93
94 sscanf(buf, "%d", &value);
95 if (driver->enable_function)
96 driver->enable_function(f, value);
97 else
Mike Lockwoode2dc5032010-06-23 08:20:59 -040098 usb_function_set_enabled(f, value);
Mike Lockwoodf041ac62010-02-06 21:53:51 -050099
100 return size;
101}
102
103static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
104
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400105void usb_function_set_enabled(struct usb_function *f, int enabled)
106{
107 f->disabled = !enabled;
108 kobject_uevent(&f->dev->kobj, KOBJ_CHANGE);
109}
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500110
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400111
112void usb_composite_force_reset(struct usb_composite_dev *cdev)
113{
114 unsigned long flags;
115
116 spin_lock_irqsave(&cdev->lock, flags);
117 /* force reenumeration */
Mike Lockwoode6be8942010-12-10 16:30:15 -0800118 if (cdev && cdev->gadget && cdev->gadget->speed != USB_SPEED_UNKNOWN) {
Mike Lockwood28acc1a2010-06-28 16:19:32 -0400119 spin_unlock_irqrestore(&cdev->lock, flags);
120
121 usb_gadget_disconnect(cdev->gadget);
122 msleep(10);
123 usb_gadget_connect(cdev->gadget);
124 } else {
125 spin_unlock_irqrestore(&cdev->lock, flags);
126 }
127}
128
David Brownell40982be2008-06-19 17:52:58 -0700129/**
130 * usb_add_function() - add a function to a configuration
131 * @config: the configuration
132 * @function: the function being added
133 * Context: single threaded during gadget setup
134 *
135 * After initialization, each configuration must have one or more
136 * functions added to it. Adding a function involves calling its @bind()
137 * method to allocate resources such as interface and string identifiers
138 * and endpoints.
139 *
140 * This function returns the value of the function's bind(), which is
141 * zero for success else a negative errno value.
142 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200143int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700144 struct usb_function *function)
145{
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500146 struct usb_composite_dev *cdev = config->cdev;
David Brownell40982be2008-06-19 17:52:58 -0700147 int value = -EINVAL;
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500148 int index;
David Brownell40982be2008-06-19 17:52:58 -0700149
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500150 DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -0700151 function->name, function,
152 config->label, config);
153
154 if (!function->set_alt || !function->disable)
155 goto done;
156
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500157 index = atomic_inc_return(&cdev->driver->function_count);
158 function->dev = device_create(cdev->driver->class, NULL,
159 MKDEV(0, index), NULL, function->name);
160 if (IS_ERR(function->dev))
161 return PTR_ERR(function->dev);
162
163 value = device_create_file(function->dev, &dev_attr_enable);
164 if (value < 0) {
165 device_destroy(cdev->driver->class, MKDEV(0, index));
166 return value;
167 }
168 dev_set_drvdata(function->dev, function);
169
David Brownell40982be2008-06-19 17:52:58 -0700170 function->config = config;
171 list_add_tail(&function->list, &config->functions);
172
173 /* REVISIT *require* function->bind? */
174 if (function->bind) {
175 value = function->bind(config, function);
176 if (value < 0) {
177 list_del(&function->list);
178 function->config = NULL;
179 }
180 } else
181 value = 0;
182
183 /* We allow configurations that don't work at both speeds.
184 * If we run into a lowspeed Linux system, treat it the same
185 * as full speed ... it's the function drivers that will need
186 * to avoid bulk and ISO transfers.
187 */
188 if (!config->fullspeed && function->descriptors)
189 config->fullspeed = true;
190 if (!config->highspeed && function->hs_descriptors)
191 config->highspeed = true;
192
193done:
194 if (value)
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500195 DBG(cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700196 function->name, function, value);
197 return value;
198}
199
200/**
David Brownell60beed92008-08-18 17:38:22 -0700201 * usb_function_deactivate - prevent function and gadget enumeration
202 * @function: the function that isn't yet ready to respond
203 *
204 * Blocks response of the gadget driver to host enumeration by
205 * preventing the data line pullup from being activated. This is
206 * normally called during @bind() processing to change from the
207 * initial "ready to respond" state, or when a required resource
208 * becomes available.
209 *
210 * For example, drivers that serve as a passthrough to a userspace
211 * daemon can block enumeration unless that daemon (such as an OBEX,
212 * MTP, or print server) is ready to handle host requests.
213 *
214 * Not all systems support software control of their USB peripheral
215 * data pullups.
216 *
217 * Returns zero on success, else negative errno.
218 */
219int usb_function_deactivate(struct usb_function *function)
220{
221 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200222 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700223 int status = 0;
224
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200225 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700226
227 if (cdev->deactivations == 0)
228 status = usb_gadget_disconnect(cdev->gadget);
229 if (status == 0)
230 cdev->deactivations++;
231
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200232 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700233 return status;
234}
235
236/**
237 * usb_function_activate - allow function and gadget enumeration
238 * @function: function on which usb_function_activate() was called
239 *
240 * Reverses effect of usb_function_deactivate(). If no more functions
241 * are delaying their activation, the gadget driver will respond to
242 * host enumeration procedures.
243 *
244 * Returns zero on success, else negative errno.
245 */
246int usb_function_activate(struct usb_function *function)
247{
248 struct usb_composite_dev *cdev = function->config->cdev;
249 int status = 0;
250
251 spin_lock(&cdev->lock);
252
253 if (WARN_ON(cdev->deactivations == 0))
254 status = -EINVAL;
255 else {
256 cdev->deactivations--;
257 if (cdev->deactivations == 0)
258 status = usb_gadget_connect(cdev->gadget);
259 }
260
261 spin_unlock(&cdev->lock);
262 return status;
263}
264
265/**
David Brownell40982be2008-06-19 17:52:58 -0700266 * usb_interface_id() - allocate an unused interface ID
267 * @config: configuration associated with the interface
268 * @function: function handling the interface
269 * Context: single threaded during gadget setup
270 *
271 * usb_interface_id() is called from usb_function.bind() callbacks to
272 * allocate new interface IDs. The function driver will then store that
273 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300274 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700275 * particularly changing its altsetting via set_alt(). There may
276 * also be class-specific or vendor-specific requests to handle.
277 *
278 * All interface identifier should be allocated using this routine, to
279 * ensure that for example different functions don't wrongly assign
280 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300281 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700282 * one configuration (or more than once in a given configuration) need
283 * multiple versions of the relevant descriptors.
284 *
285 * Returns the interface ID which was allocated; or -ENODEV if no
286 * more interface IDs can be allocated.
287 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200288int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700289 struct usb_function *function)
290{
291 unsigned id = config->next_interface_id;
292
293 if (id < MAX_CONFIG_INTERFACES) {
294 config->interface[id] = function;
295 config->next_interface_id = id + 1;
296 return id;
297 }
298 return -ENODEV;
299}
300
301static int config_buf(struct usb_configuration *config,
302 enum usb_device_speed speed, void *buf, u8 type)
303{
304 struct usb_config_descriptor *c = buf;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500305 struct usb_interface_descriptor *intf;
John Michelauf71a3942010-12-10 12:09:53 -0600306 struct usb_interface_assoc_descriptor *iad = NULL;
David Brownell40982be2008-06-19 17:52:58 -0700307 void *next = buf + USB_DT_CONFIG_SIZE;
308 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
309 struct usb_function *f;
310 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500311 int interfaceCount = 0;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500312 u8 *dest;
David Brownell40982be2008-06-19 17:52:58 -0700313
314 /* write the config descriptor */
315 c = buf;
316 c->bLength = USB_DT_CONFIG_SIZE;
317 c->bDescriptorType = type;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500318 /* wTotalLength and bNumInterfaces are written later */
David Brownell40982be2008-06-19 17:52:58 -0700319 c->bConfigurationValue = config->bConfigurationValue;
320 c->iConfiguration = config->iConfiguration;
321 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700322 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700323
324 /* There may be e.g. OTG descriptors */
325 if (config->descriptors) {
326 status = usb_descriptor_fillbuf(next, len,
327 config->descriptors);
328 if (status < 0)
329 return status;
330 len -= status;
331 next += status;
332 }
333
334 /* add each function's descriptors */
335 list_for_each_entry(f, &config->functions, list) {
336 struct usb_descriptor_header **descriptors;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500337 struct usb_descriptor_header *descriptor;
David Brownell40982be2008-06-19 17:52:58 -0700338
339 if (speed == USB_SPEED_HIGH)
340 descriptors = f->hs_descriptors;
341 else
342 descriptors = f->descriptors;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400343 if (f->disabled || !descriptors || descriptors[0] == NULL)
David Brownell40982be2008-06-19 17:52:58 -0700344 continue;
345 status = usb_descriptor_fillbuf(next, len,
346 (const struct usb_descriptor_header **) descriptors);
347 if (status < 0)
348 return status;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500349
350 /* set interface numbers dynamically */
351 dest = next;
352 while ((descriptor = *descriptors++) != NULL) {
353 intf = (struct usb_interface_descriptor *)dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500354 if (intf->bDescriptorType == USB_DT_INTERFACE) {
355 /* don't increment bInterfaceNumber for alternate settings */
356 if (intf->bAlternateSetting == 0)
357 intf->bInterfaceNumber = interfaceCount++;
358 else
359 intf->bInterfaceNumber = interfaceCount - 1;
John Michelauf71a3942010-12-10 12:09:53 -0600360 if (iad) {
361 iad->bFirstInterface =
362 intf->bInterfaceNumber;
363 iad = NULL;
364 }
365 } else if (intf->bDescriptorType ==
366 USB_DT_INTERFACE_ASSOCIATION) {
367 /* This will be first if it exists. Save
368 * a pointer to it so we can properly set
369 * bFirstInterface when we process the first
370 * interface.
371 */
372 iad = (struct usb_interface_assoc_descriptor *)
373 dest;
Mike Lockwood0f63be22010-02-26 09:34:19 -0500374 }
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500375 dest += intf->bLength;
376 }
377
David Brownell40982be2008-06-19 17:52:58 -0700378 len -= status;
379 next += status;
380 }
381
382 len = next - buf;
383 c->wTotalLength = cpu_to_le16(len);
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500384 c->bNumInterfaces = interfaceCount;
David Brownell40982be2008-06-19 17:52:58 -0700385 return len;
386}
387
388static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
389{
390 struct usb_gadget *gadget = cdev->gadget;
391 struct usb_configuration *c;
392 u8 type = w_value >> 8;
393 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
394
395 if (gadget_is_dualspeed(gadget)) {
396 int hs = 0;
397
398 if (gadget->speed == USB_SPEED_HIGH)
399 hs = 1;
400 if (type == USB_DT_OTHER_SPEED_CONFIG)
401 hs = !hs;
402 if (hs)
403 speed = USB_SPEED_HIGH;
404
405 }
406
407 /* This is a lookup by config *INDEX* */
408 w_value &= 0xff;
409 list_for_each_entry(c, &cdev->configs, list) {
410 /* ignore configs that won't work at this speed */
411 if (speed == USB_SPEED_HIGH) {
412 if (!c->highspeed)
413 continue;
414 } else {
415 if (!c->fullspeed)
416 continue;
417 }
418 if (w_value == 0)
419 return config_buf(c, speed, cdev->req->buf, type);
420 w_value--;
421 }
422 return -EINVAL;
423}
424
425static int count_configs(struct usb_composite_dev *cdev, unsigned type)
426{
427 struct usb_gadget *gadget = cdev->gadget;
428 struct usb_configuration *c;
429 unsigned count = 0;
430 int hs = 0;
431
432 if (gadget_is_dualspeed(gadget)) {
433 if (gadget->speed == USB_SPEED_HIGH)
434 hs = 1;
435 if (type == USB_DT_DEVICE_QUALIFIER)
436 hs = !hs;
437 }
438 list_for_each_entry(c, &cdev->configs, list) {
439 /* ignore configs that won't work at this speed */
440 if (hs) {
441 if (!c->highspeed)
442 continue;
443 } else {
444 if (!c->fullspeed)
445 continue;
446 }
447 count++;
448 }
449 return count;
450}
451
452static void device_qual(struct usb_composite_dev *cdev)
453{
454 struct usb_qualifier_descriptor *qual = cdev->req->buf;
455
456 qual->bLength = sizeof(*qual);
457 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
458 /* POLICY: same bcdUSB and device type info at both speeds */
459 qual->bcdUSB = cdev->desc.bcdUSB;
460 qual->bDeviceClass = cdev->desc.bDeviceClass;
461 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
462 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
463 /* ASSUME same EP0 fifo size at both speeds */
464 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
465 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700466 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700467}
468
469/*-------------------------------------------------------------------------*/
470
471static void reset_config(struct usb_composite_dev *cdev)
472{
473 struct usb_function *f;
474
475 DBG(cdev, "reset config\n");
476
477 list_for_each_entry(f, &cdev->config->functions, list) {
478 if (f->disable)
479 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200480
481 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700482 }
483 cdev->config = NULL;
484}
485
486static int set_config(struct usb_composite_dev *cdev,
487 const struct usb_ctrlrequest *ctrl, unsigned number)
488{
489 struct usb_gadget *gadget = cdev->gadget;
490 struct usb_configuration *c = NULL;
491 int result = -EINVAL;
492 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
493 int tmp;
494
495 if (cdev->config)
496 reset_config(cdev);
497
498 if (number) {
499 list_for_each_entry(c, &cdev->configs, list) {
500 if (c->bConfigurationValue == number) {
501 result = 0;
502 break;
503 }
504 }
505 if (result < 0)
506 goto done;
507 } else
508 result = 0;
509
510 INFO(cdev, "%s speed config #%d: %s\n",
511 ({ char *speed;
512 switch (gadget->speed) {
513 case USB_SPEED_LOW: speed = "low"; break;
514 case USB_SPEED_FULL: speed = "full"; break;
515 case USB_SPEED_HIGH: speed = "high"; break;
516 default: speed = "?"; break;
517 } ; speed; }), number, c ? c->label : "unconfigured");
518
519 if (!c)
520 goto done;
521
522 cdev->config = c;
523
524 /* Initialize all interfaces by setting them to altsetting zero. */
525 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
526 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200527 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700528
529 if (!f)
530 break;
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400531 if (f->disabled)
Mike Lockwood8c6a6b22010-02-26 09:30:01 -0500532 continue;
David Brownell40982be2008-06-19 17:52:58 -0700533
Laurent Pinchart52426582009-10-21 00:03:38 +0200534 /*
535 * Record which endpoints are used by the function. This is used
536 * to dispatch control requests targeted at that endpoint to the
537 * function's setup callback instead of the current
538 * configuration's setup callback.
539 */
540 if (gadget->speed == USB_SPEED_HIGH)
541 descriptors = f->hs_descriptors;
542 else
543 descriptors = f->descriptors;
544
545 for (; *descriptors; ++descriptors) {
546 struct usb_endpoint_descriptor *ep;
547 int addr;
548
549 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
550 continue;
551
552 ep = (struct usb_endpoint_descriptor *)*descriptors;
553 addr = ((ep->bEndpointAddress & 0x80) >> 3)
554 | (ep->bEndpointAddress & 0x0f);
555 set_bit(addr, f->endpoints);
556 }
557
David Brownell40982be2008-06-19 17:52:58 -0700558 result = f->set_alt(f, tmp, 0);
559 if (result < 0) {
560 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
561 tmp, f->name, f, result);
562
563 reset_config(cdev);
564 goto done;
565 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300566
567 if (result == USB_GADGET_DELAYED_STATUS) {
568 DBG(cdev,
569 "%s: interface %d (%s) requested delayed status\n",
570 __func__, tmp, f->name);
571 cdev->delayed_status++;
572 DBG(cdev, "delayed_status count %d\n",
573 cdev->delayed_status);
574 }
David Brownell40982be2008-06-19 17:52:58 -0700575 }
576
577 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700578 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700579done:
580 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400581
Mike Lockwood8da4cc82010-06-27 20:05:55 -0400582 schedule_work(&cdev->switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400583
Roger Quadros1b9ba002011-05-09 13:08:06 +0300584 if (result >= 0 && cdev->delayed_status)
585 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700586 return result;
587}
588
589/**
590 * usb_add_config() - add a configuration to a device.
591 * @cdev: wraps the USB gadget
592 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200593 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700594 * Context: single threaded during gadget setup
595 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200596 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700597 * add each of the configurations it supports, using this routine.
598 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200599 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700600 * is zero for success else a negative errno value. Binding configurations
601 * assigns global resources including string IDs, and per-configuration
602 * resources such as interface IDs and endpoints.
603 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200604int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200605 struct usb_configuration *config,
606 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700607{
608 int status = -EINVAL;
609 struct usb_configuration *c;
610
611 DBG(cdev, "adding config #%u '%s'/%p\n",
612 config->bConfigurationValue,
613 config->label, config);
614
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200615 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700616 goto done;
617
618 /* Prevent duplicate configuration identifiers */
619 list_for_each_entry(c, &cdev->configs, list) {
620 if (c->bConfigurationValue == config->bConfigurationValue) {
621 status = -EBUSY;
622 goto done;
623 }
624 }
625
626 config->cdev = cdev;
627 list_add_tail(&config->list, &cdev->configs);
628
629 INIT_LIST_HEAD(&config->functions);
630 config->next_interface_id = 0;
631
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200632 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700633 if (status < 0) {
634 list_del(&config->list);
635 config->cdev = NULL;
636 } else {
637 unsigned i;
638
639 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
640 config->bConfigurationValue, config,
641 config->highspeed ? " high" : "",
642 config->fullspeed
643 ? (gadget_is_dualspeed(cdev->gadget)
644 ? " full"
645 : " full/low")
646 : "");
647
648 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
649 struct usb_function *f = config->interface[i];
650
651 if (!f)
652 continue;
653 DBG(cdev, " interface %d = %s/%p\n",
654 i, f->name, f);
655 }
656 }
657
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200658 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700659 * ep->driver_data as needed.
660 */
661 usb_ep_autoconfig_reset(cdev->gadget);
662
663done:
664 if (status)
665 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
666 config->bConfigurationValue, status);
667 return status;
668}
669
670/*-------------------------------------------------------------------------*/
671
672/* We support strings in multiple languages ... string descriptor zero
673 * says which languages are supported. The typical case will be that
674 * only one language (probably English) is used, with I18N handled on
675 * the host side.
676 */
677
678static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
679{
680 const struct usb_gadget_strings *s;
681 u16 language;
682 __le16 *tmp;
683
684 while (*sp) {
685 s = *sp;
686 language = cpu_to_le16(s->language);
687 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
688 if (*tmp == language)
689 goto repeat;
690 }
691 *tmp++ = language;
692repeat:
693 sp++;
694 }
695}
696
697static int lookup_string(
698 struct usb_gadget_strings **sp,
699 void *buf,
700 u16 language,
701 int id
702)
703{
704 struct usb_gadget_strings *s;
705 int value;
706
707 while (*sp) {
708 s = *sp++;
709 if (s->language != language)
710 continue;
711 value = usb_gadget_get_string(s, id, buf);
712 if (value > 0)
713 return value;
714 }
715 return -EINVAL;
716}
717
718static int get_string(struct usb_composite_dev *cdev,
719 void *buf, u16 language, int id)
720{
721 struct usb_configuration *c;
722 struct usb_function *f;
723 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200724 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700725
726 /* Yes, not only is USB's I18N support probably more than most
727 * folk will ever care about ... also, it's all supported here.
728 * (Except for UTF8 support for Unicode's "Astral Planes".)
729 */
730
731 /* 0 == report all available language codes */
732 if (id == 0) {
733 struct usb_string_descriptor *s = buf;
734 struct usb_gadget_strings **sp;
735
736 memset(s, 0, 256);
737 s->bDescriptorType = USB_DT_STRING;
738
739 sp = composite->strings;
740 if (sp)
741 collect_langs(sp, s->wData);
742
743 list_for_each_entry(c, &cdev->configs, list) {
744 sp = c->strings;
745 if (sp)
746 collect_langs(sp, s->wData);
747
748 list_for_each_entry(f, &c->functions, list) {
749 sp = f->strings;
750 if (sp)
751 collect_langs(sp, s->wData);
752 }
753 }
754
Roel Kluin417b57b2009-08-06 16:09:51 -0700755 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700756 continue;
757 if (!len)
758 return -EINVAL;
759
760 s->bLength = 2 * (len + 1);
761 return s->bLength;
762 }
763
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200764 /* Otherwise, look up and return a specified string. First
765 * check if the string has not been overridden.
766 */
767 if (cdev->manufacturer_override == id)
768 str = iManufacturer ?: composite->iManufacturer ?:
769 composite_manufacturer;
770 else if (cdev->product_override == id)
771 str = iProduct ?: composite->iProduct;
772 else if (cdev->serial_override == id)
773 str = iSerialNumber;
774 else
775 str = NULL;
776 if (str) {
777 struct usb_gadget_strings strings = {
778 .language = language,
779 .strings = &(struct usb_string) { 0xff, str }
780 };
781 return usb_gadget_get_string(&strings, 0xff, buf);
782 }
783
784 /* String IDs are device-scoped, so we look up each string
785 * table we're told about. These lookups are infrequent;
786 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700787 */
788 if (composite->strings) {
789 len = lookup_string(composite->strings, buf, language, id);
790 if (len > 0)
791 return len;
792 }
793 list_for_each_entry(c, &cdev->configs, list) {
794 if (c->strings) {
795 len = lookup_string(c->strings, buf, language, id);
796 if (len > 0)
797 return len;
798 }
799 list_for_each_entry(f, &c->functions, list) {
800 if (!f->strings)
801 continue;
802 len = lookup_string(f->strings, buf, language, id);
803 if (len > 0)
804 return len;
805 }
806 }
807 return -EINVAL;
808}
809
810/**
811 * usb_string_id() - allocate an unused string ID
812 * @cdev: the device whose string descriptor IDs are being allocated
813 * Context: single threaded during gadget setup
814 *
815 * @usb_string_id() is called from bind() callbacks to allocate
816 * string IDs. Drivers for functions, configurations, or gadgets will
817 * then store that ID in the appropriate descriptors and string table.
818 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200819 * All string identifier should be allocated using this,
820 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
821 * that for example different functions don't wrongly assign different
822 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700823 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200824int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700825{
826 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200827 /* string id 0 is reserved by USB spec for list of
828 * supported languages */
829 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700830 cdev->next_string_id++;
831 return cdev->next_string_id;
832 }
833 return -ENODEV;
834}
835
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200836/**
837 * usb_string_ids() - allocate unused string IDs in batch
838 * @cdev: the device whose string descriptor IDs are being allocated
839 * @str: an array of usb_string objects to assign numbers to
840 * Context: single threaded during gadget setup
841 *
842 * @usb_string_ids() is called from bind() callbacks to allocate
843 * string IDs. Drivers for functions, configurations, or gadgets will
844 * then copy IDs from the string table to the appropriate descriptors
845 * and string table for other languages.
846 *
847 * All string identifier should be allocated using this,
848 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
849 * example different functions don't wrongly assign different meanings
850 * to the same identifier.
851 */
852int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
853{
854 int next = cdev->next_string_id;
855
856 for (; str->s; ++str) {
857 if (unlikely(next >= 254))
858 return -ENODEV;
859 str->id = ++next;
860 }
861
862 cdev->next_string_id = next;
863
864 return 0;
865}
866
867/**
868 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700869 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200870 * @n: number of string IDs to allocate
871 * Context: single threaded during gadget setup
872 *
873 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700874 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200875 * is, returns last requested ID which is now very useful information.
876 *
877 * @usb_string_ids_n() is called from bind() callbacks to allocate
878 * string IDs. Drivers for functions, configurations, or gadgets will
879 * then store that ID in the appropriate descriptors and string table.
880 *
881 * All string identifier should be allocated using this,
882 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
883 * example different functions don't wrongly assign different meanings
884 * to the same identifier.
885 */
886int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
887{
888 unsigned next = c->next_string_id;
889 if (unlikely(n > 254 || (unsigned)next + n > 254))
890 return -ENODEV;
891 c->next_string_id += n;
892 return next + 1;
893}
894
895
David Brownell40982be2008-06-19 17:52:58 -0700896/*-------------------------------------------------------------------------*/
897
898static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
899{
900 if (req->status || req->actual != req->length)
901 DBG((struct usb_composite_dev *) ep->driver_data,
902 "setup complete --> %d, %d/%d\n",
903 req->status, req->actual, req->length);
904}
905
906/*
907 * The setup() callback implements all the ep0 functionality that's
908 * not handled lower down, in hardware or the hardware driver(like
909 * device and endpoint feature flags, and their status). It's all
910 * housekeeping for the gadget function we're implementing. Most of
911 * the work is in config and function specific setup.
912 */
913static int
914composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
915{
916 struct usb_composite_dev *cdev = get_gadget_data(gadget);
917 struct usb_request *req = cdev->req;
918 int value = -EOPNOTSUPP;
919 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800920 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700921 u16 w_value = le16_to_cpu(ctrl->wValue);
922 u16 w_length = le16_to_cpu(ctrl->wLength);
923 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200924 u8 endp;
Mike Lockwoode6be8942010-12-10 16:30:15 -0800925 unsigned long flags;
926
927 spin_lock_irqsave(&cdev->lock, flags);
928 if (!cdev->connected) {
929 cdev->connected = 1;
930 schedule_work(&cdev->switch_work);
931 }
932 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -0700933
934 /* partial re-init of the response message; the function or the
935 * gadget might need to intercept e.g. a control-OUT completion
936 * when we delegate to it.
937 */
938 req->zero = 0;
939 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530940 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700941 gadget->ep0->driver_data = cdev;
942
943 switch (ctrl->bRequest) {
944
945 /* we handle all standard USB descriptors */
946 case USB_REQ_GET_DESCRIPTOR:
947 if (ctrl->bRequestType != USB_DIR_IN)
948 goto unknown;
949 switch (w_value >> 8) {
950
951 case USB_DT_DEVICE:
952 cdev->desc.bNumConfigurations =
953 count_configs(cdev, USB_DT_DEVICE);
954 value = min(w_length, (u16) sizeof cdev->desc);
955 memcpy(req->buf, &cdev->desc, value);
956 break;
957 case USB_DT_DEVICE_QUALIFIER:
958 if (!gadget_is_dualspeed(gadget))
959 break;
960 device_qual(cdev);
961 value = min_t(int, w_length,
962 sizeof(struct usb_qualifier_descriptor));
963 break;
964 case USB_DT_OTHER_SPEED_CONFIG:
965 if (!gadget_is_dualspeed(gadget))
966 break;
967 /* FALLTHROUGH */
968 case USB_DT_CONFIG:
969 value = config_desc(cdev, w_value);
970 if (value >= 0)
971 value = min(w_length, (u16) value);
972 break;
973 case USB_DT_STRING:
974 value = get_string(cdev, req->buf,
975 w_index, w_value & 0xff);
Mike Lockwoodfb52b002010-04-16 15:32:15 -0400976
977 /* Allow functions to handle USB_DT_STRING.
978 * This is required for MTP.
979 */
980 if (value < 0) {
981 struct usb_configuration *cfg;
982 list_for_each_entry(cfg, &cdev->configs, list) {
983 if (cfg && cfg->setup) {
984 value = cfg->setup(cfg, ctrl);
985 if (value >= 0)
986 break;
987 }
988 }
989 }
990
David Brownell40982be2008-06-19 17:52:58 -0700991 if (value >= 0)
992 value = min(w_length, (u16) value);
993 break;
994 }
995 break;
996
997 /* any number of configs can work */
998 case USB_REQ_SET_CONFIGURATION:
999 if (ctrl->bRequestType != 0)
1000 goto unknown;
1001 if (gadget_is_otg(gadget)) {
1002 if (gadget->a_hnp_support)
1003 DBG(cdev, "HNP available\n");
1004 else if (gadget->a_alt_hnp_support)
1005 DBG(cdev, "HNP on another port\n");
1006 else
1007 VDBG(cdev, "HNP inactive\n");
1008 }
1009 spin_lock(&cdev->lock);
1010 value = set_config(cdev, ctrl, w_value);
1011 spin_unlock(&cdev->lock);
1012 break;
1013 case USB_REQ_GET_CONFIGURATION:
1014 if (ctrl->bRequestType != USB_DIR_IN)
1015 goto unknown;
Jared Suttles258d1032009-08-07 18:57:49 -05001016 if (cdev->config) {
David Brownell40982be2008-06-19 17:52:58 -07001017 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Jared Suttles258d1032009-08-07 18:57:49 -05001018 value = min(w_length, (u16) 1);
1019 } else {
David Brownell40982be2008-06-19 17:52:58 -07001020 *(u8 *)req->buf = 0;
Jared Suttles258d1032009-08-07 18:57:49 -05001021 }
David Brownell40982be2008-06-19 17:52:58 -07001022 break;
1023
1024 /* function drivers must handle get/set altsetting; if there's
1025 * no get() method, we know only altsetting zero works.
1026 */
1027 case USB_REQ_SET_INTERFACE:
1028 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1029 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001030 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001031 break;
Bryan Wu08889512009-01-08 00:21:19 +08001032 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001033 if (!f)
1034 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001035 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001036 break;
1037 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001038 if (value == USB_GADGET_DELAYED_STATUS) {
1039 DBG(cdev,
1040 "%s: interface %d (%s) requested delayed status\n",
1041 __func__, intf, f->name);
1042 cdev->delayed_status++;
1043 DBG(cdev, "delayed_status count %d\n",
1044 cdev->delayed_status);
1045 }
David Brownell40982be2008-06-19 17:52:58 -07001046 break;
1047 case USB_REQ_GET_INTERFACE:
1048 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1049 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001050 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001051 break;
Bryan Wu08889512009-01-08 00:21:19 +08001052 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001053 if (!f)
1054 break;
1055 /* lots of interfaces only need altsetting zero... */
1056 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1057 if (value < 0)
1058 break;
1059 *((u8 *)req->buf) = value;
1060 value = min(w_length, (u16) 1);
1061 break;
1062 default:
1063unknown:
1064 VDBG(cdev,
1065 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1066 ctrl->bRequestType, ctrl->bRequest,
1067 w_value, w_index, w_length);
1068
Laurent Pinchart52426582009-10-21 00:03:38 +02001069 /* functions always handle their interfaces and endpoints...
1070 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001071 * configuration code.
1072 *
1073 * REVISIT it could make sense to let the composite device
1074 * take such requests too, if that's ever needed: to work
1075 * in config 0, etc.
1076 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001077 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1078 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001079 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301080 break;
1081 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001082 break;
1083
1084 case USB_RECIP_ENDPOINT:
1085 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1086 list_for_each_entry(f, &cdev->config->functions, list) {
1087 if (test_bit(endp, f->endpoints))
1088 break;
1089 }
1090 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001091 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001092 break;
David Brownell40982be2008-06-19 17:52:58 -07001093 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001094
1095 if (f && f->setup)
1096 value = f->setup(f, ctrl);
1097 else {
David Brownell40982be2008-06-19 17:52:58 -07001098 struct usb_configuration *c;
1099
1100 c = cdev->config;
1101 if (c && c->setup)
1102 value = c->setup(c, ctrl);
1103 }
1104
Joe Swantek292b1bc2009-12-15 07:17:40 -05001105 /* If the vendor request is not processed (value < 0),
1106 * call all device registered configure setup callbacks
1107 * to process it.
1108 * This is used to handle the following cases:
1109 * - vendor request is for the device and arrives before
1110 * setconfiguration.
1111 * - Some devices are required to handle vendor request before
1112 * setconfiguration such as MTP, USBNET.
1113 */
1114
1115 if (value < 0) {
1116 struct usb_configuration *cfg;
1117
1118 list_for_each_entry(cfg, &cdev->configs, list) {
1119 if (cfg && cfg->setup)
1120 value = cfg->setup(cfg, ctrl);
1121 }
1122 }
1123
David Brownell40982be2008-06-19 17:52:58 -07001124 goto done;
1125 }
1126
1127 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001128 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001129 req->length = value;
1130 req->zero = value < w_length;
1131 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1132 if (value < 0) {
1133 DBG(cdev, "ep_queue --> %d\n", value);
1134 req->status = 0;
1135 composite_setup_complete(gadget->ep0, req);
1136 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001137 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1138 WARN(cdev,
1139 "%s: Delayed status not supported for w_length != 0",
1140 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001141 }
1142
1143done:
1144 /* device either stalls (value < 0) or reports success */
1145 return value;
1146}
1147
1148static void composite_disconnect(struct usb_gadget *gadget)
1149{
1150 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1151 unsigned long flags;
1152
1153 /* REVISIT: should we have config and device level
1154 * disconnect callbacks?
1155 */
1156 spin_lock_irqsave(&cdev->lock, flags);
1157 if (cdev->config)
1158 reset_config(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001159
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001160 if (composite->disconnect)
1161 composite->disconnect(cdev);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001162
Mike Lockwoode6be8942010-12-10 16:30:15 -08001163 cdev->connected = 0;
1164 schedule_work(&cdev->switch_work);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001165 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001166}
1167
1168/*-------------------------------------------------------------------------*/
1169
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001170static ssize_t composite_show_suspended(struct device *dev,
1171 struct device_attribute *attr,
1172 char *buf)
1173{
1174 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1175 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1176
1177 return sprintf(buf, "%d\n", cdev->suspended);
1178}
1179
1180static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1181
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001182static void
David Brownell40982be2008-06-19 17:52:58 -07001183composite_unbind(struct usb_gadget *gadget)
1184{
1185 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1186
1187 /* composite_disconnect() must already have been called
1188 * by the underlying peripheral controller driver!
1189 * so there's no i/o concurrency that could affect the
1190 * state protected by cdev->lock.
1191 */
1192 WARN_ON(cdev->config);
1193
1194 while (!list_empty(&cdev->configs)) {
1195 struct usb_configuration *c;
1196
1197 c = list_first_entry(&cdev->configs,
1198 struct usb_configuration, list);
1199 while (!list_empty(&c->functions)) {
1200 struct usb_function *f;
1201
1202 f = list_first_entry(&c->functions,
1203 struct usb_function, list);
1204 list_del(&f->list);
1205 if (f->unbind) {
1206 DBG(cdev, "unbind function '%s'/%p\n",
1207 f->name, f);
1208 f->unbind(c, f);
1209 /* may free memory for "f" */
1210 }
1211 }
1212 list_del(&c->list);
1213 if (c->unbind) {
1214 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1215 c->unbind(c);
1216 /* may free memory for "c" */
1217 }
1218 }
1219 if (composite->unbind)
1220 composite->unbind(cdev);
1221
1222 if (cdev->req) {
1223 kfree(cdev->req->buf);
1224 usb_ep_free_request(gadget->ep0, cdev->req);
1225 }
Mike Lockwoode6be8942010-12-10 16:30:15 -08001226 switch_dev_unregister(&cdev->sw_connected);
1227 switch_dev_unregister(&cdev->sw_config);
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301228 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001229 kfree(cdev);
1230 set_gadget_data(gadget, NULL);
1231 composite = NULL;
1232}
1233
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001234static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001235{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001236 if (!*desc) {
1237 int ret = usb_string_id(cdev);
1238 if (unlikely(ret < 0))
1239 WARNING(cdev, "failed to override string ID\n");
1240 else
1241 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001242 }
David Brownell40982be2008-06-19 17:52:58 -07001243
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001244 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001245}
1246
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001247static void
1248composite_switch_work(struct work_struct *data)
1249{
1250 struct usb_composite_dev *cdev =
1251 container_of(data, struct usb_composite_dev, switch_work);
1252 struct usb_configuration *config = cdev->config;
Mike Lockwoode6be8942010-12-10 16:30:15 -08001253 int connected;
1254 unsigned long flags;
1255
1256 spin_lock_irqsave(&cdev->lock, flags);
1257 if (cdev->connected != cdev->sw_connected.state) {
1258 connected = cdev->connected;
1259 spin_unlock_irqrestore(&cdev->lock, flags);
1260 switch_set_state(&cdev->sw_connected, connected);
1261 } else {
1262 spin_unlock_irqrestore(&cdev->lock, flags);
1263 }
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001264
1265 if (config)
Mike Lockwoode6be8942010-12-10 16:30:15 -08001266 switch_set_state(&cdev->sw_config, config->bConfigurationValue);
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001267 else
Mike Lockwoode6be8942010-12-10 16:30:15 -08001268 switch_set_state(&cdev->sw_config, 0);
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001269}
1270
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001271static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001272{
1273 struct usb_composite_dev *cdev;
1274 int status = -ENOMEM;
1275
1276 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1277 if (!cdev)
1278 return status;
1279
1280 spin_lock_init(&cdev->lock);
1281 cdev->gadget = gadget;
1282 set_gadget_data(gadget, cdev);
1283 INIT_LIST_HEAD(&cdev->configs);
1284
1285 /* preallocate control response and buffer */
1286 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1287 if (!cdev->req)
1288 goto fail;
1289 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1290 if (!cdev->req->buf)
1291 goto fail;
1292 cdev->req->complete = composite_setup_complete;
1293 gadget->ep0->driver_data = cdev;
1294
1295 cdev->bufsiz = USB_BUFSIZ;
1296 cdev->driver = composite;
1297
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301298 /*
1299 * As per USB compliance update, a device that is actively drawing
1300 * more than 100mA from USB must report itself as bus-powered in
1301 * the GetStatus(DEVICE) call.
1302 */
1303 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1304 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001305
1306 /* interface and string IDs start at zero via kzalloc.
1307 * we force endpoints to start unassigned; few controller
1308 * drivers will zero ep->driver_data.
1309 */
1310 usb_ep_autoconfig_reset(cdev->gadget);
1311
1312 /* composite gadget needs to assign strings for whole device (like
1313 * serial number), register function drivers, potentially update
1314 * power state and consumption, etc
1315 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001316 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001317 if (status < 0)
1318 goto fail;
1319
Mike Lockwoode6be8942010-12-10 16:30:15 -08001320 cdev->sw_connected.name = "usb_connected";
1321 status = switch_dev_register(&cdev->sw_connected);
1322 if (status < 0)
1323 goto fail;
1324 cdev->sw_config.name = "usb_configuration";
1325 status = switch_dev_register(&cdev->sw_config);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001326 if (status < 0)
1327 goto fail;
Mike Lockwood8da4cc82010-06-27 20:05:55 -04001328 INIT_WORK(&cdev->switch_work, composite_switch_work);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001329
David Brownell40982be2008-06-19 17:52:58 -07001330 cdev->desc = *composite->dev;
1331 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1332
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001333 /* standardized runtime overrides for device ID data */
1334 if (idVendor)
1335 cdev->desc.idVendor = cpu_to_le16(idVendor);
1336 if (idProduct)
1337 cdev->desc.idProduct = cpu_to_le16(idProduct);
1338 if (bcdDevice)
1339 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1340
Marek Belisko78bff3c2010-10-27 10:19:01 +02001341 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001342 if (iManufacturer || !cdev->desc.iManufacturer) {
1343 if (!iManufacturer && !composite->iManufacturer &&
1344 !*composite_manufacturer)
1345 snprintf(composite_manufacturer,
1346 sizeof composite_manufacturer,
1347 "%s %s with %s",
1348 init_utsname()->sysname,
1349 init_utsname()->release,
1350 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001351
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001352 cdev->manufacturer_override =
1353 override_id(cdev, &cdev->desc.iManufacturer);
1354 }
1355
1356 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1357 cdev->product_override =
1358 override_id(cdev, &cdev->desc.iProduct);
1359
1360 if (iSerialNumber)
1361 cdev->serial_override =
1362 override_id(cdev, &cdev->desc.iSerialNumber);
1363
1364 /* has userspace failed to provide a serial number? */
1365 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1366 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1367
1368 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001369 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1370 if (status)
1371 goto fail;
1372
David Brownell40982be2008-06-19 17:52:58 -07001373 INFO(cdev, "%s ready\n", composite->name);
1374 return 0;
1375
1376fail:
1377 composite_unbind(gadget);
1378 return status;
1379}
1380
1381/*-------------------------------------------------------------------------*/
1382
1383static void
1384composite_suspend(struct usb_gadget *gadget)
1385{
1386 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1387 struct usb_function *f;
1388
David Brownell89429392009-03-19 14:14:17 -07001389 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001390 * suspend/resume callbacks?
1391 */
1392 DBG(cdev, "suspend\n");
1393 if (cdev->config) {
1394 list_for_each_entry(f, &cdev->config->functions, list) {
1395 if (f->suspend)
1396 f->suspend(f);
1397 }
1398 }
David Brownell89429392009-03-19 14:14:17 -07001399 if (composite->suspend)
1400 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001401
1402 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001403
1404 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001405}
1406
1407static void
1408composite_resume(struct usb_gadget *gadget)
1409{
1410 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1411 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001412 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001413
David Brownell89429392009-03-19 14:14:17 -07001414 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001415 * suspend/resume callbacks?
1416 */
1417 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001418 if (composite->resume)
1419 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001420 if (cdev->config) {
1421 list_for_each_entry(f, &cdev->config->functions, list) {
1422 if (f->resume)
1423 f->resume(f);
1424 }
Hao Wub23f2f92010-11-29 15:17:03 +08001425
1426 maxpower = cdev->config->bMaxPower;
1427
1428 usb_gadget_vbus_draw(gadget, maxpower ?
1429 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001430 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001431
1432 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001433}
1434
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001435static int
1436composite_uevent(struct device *dev, struct kobj_uevent_env *env)
1437{
1438 struct usb_function *f = dev_get_drvdata(dev);
1439
1440 if (!f) {
1441 /* this happens when the device is first created */
1442 return 0;
1443 }
1444
1445 if (add_uevent_var(env, "FUNCTION=%s", f->name))
1446 return -ENOMEM;
1447 if (add_uevent_var(env, "ENABLED=%d", !f->disabled))
1448 return -ENOMEM;
1449 return 0;
1450}
1451
David Brownell40982be2008-06-19 17:52:58 -07001452/*-------------------------------------------------------------------------*/
1453
1454static struct usb_gadget_driver composite_driver = {
1455 .speed = USB_SPEED_HIGH,
1456
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001457 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001458
1459 .setup = composite_setup,
1460 .disconnect = composite_disconnect,
1461
1462 .suspend = composite_suspend,
1463 .resume = composite_resume,
1464
1465 .driver = {
1466 .owner = THIS_MODULE,
1467 },
1468};
1469
1470/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001471 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001472 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001473 * @bind: the callback used to allocate resources that are shared across the
1474 * whole device, such as string IDs, and add its configurations using
1475 * @usb_add_config(). This may fail by returning a negative errno
1476 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001477 * Context: single threaded during gadget setup
1478 *
1479 * This function is used to register drivers using the composite driver
1480 * framework. The return value is zero, or a negative errno value.
1481 * Those values normally come from the driver's @bind method, which does
1482 * all the work of setting up the driver to match the hardware.
1483 *
1484 * On successful return, the gadget is ready to respond to requests from
1485 * the host, unless one of its components invokes usb_gadget_disconnect()
1486 * while it was binding. That would usually be done in order to wait for
1487 * some userspace participation.
1488 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001489int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001490 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001491{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001492 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001493 return -EINVAL;
1494
1495 if (!driver->name)
1496 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001497 if (!driver->iProduct)
1498 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001499 composite_driver.function = (char *) driver->name;
1500 composite_driver.driver.name = driver->name;
1501 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001502 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001503
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001504 driver->class = class_create(THIS_MODULE, "usb_composite");
1505 if (IS_ERR(driver->class))
1506 return PTR_ERR(driver->class);
Mike Lockwoode2dc5032010-06-23 08:20:59 -04001507 driver->class->dev_uevent = composite_uevent;
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001508
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001509 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001510}
1511
1512/**
1513 * usb_composite_unregister() - unregister a composite driver
1514 * @driver: the driver to unregister
1515 *
1516 * This function is used to unregister drivers using the composite
1517 * driver framework.
1518 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001519void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001520{
1521 if (composite != driver)
1522 return;
1523 usb_gadget_unregister_driver(&composite_driver);
1524}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001525
1526/**
1527 * usb_composite_setup_continue() - Continue with the control transfer
1528 * @cdev: the composite device who's control transfer was kept waiting
1529 *
1530 * This function must be called by the USB function driver to continue
1531 * with the control transfer's data/status stage in case it had requested to
1532 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1533 * can request the composite framework to delay the setup request's data/status
1534 * stages by returning USB_GADGET_DELAYED_STATUS.
1535 */
1536void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1537{
1538 int value;
1539 struct usb_request *req = cdev->req;
1540 unsigned long flags;
1541
1542 DBG(cdev, "%s\n", __func__);
1543 spin_lock_irqsave(&cdev->lock, flags);
1544
1545 if (cdev->delayed_status == 0) {
1546 WARN(cdev, "%s: Unexpected call\n", __func__);
1547
1548 } else if (--cdev->delayed_status == 0) {
1549 DBG(cdev, "%s: Completing delayed status\n", __func__);
1550 req->length = 0;
1551 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1552 if (value < 0) {
1553 DBG(cdev, "ep_queue --> %d\n", value);
1554 req->status = 0;
1555 composite_setup_complete(cdev->gadget->ep0, req);
1556 }
1557 }
1558
1559 spin_unlock_irqrestore(&cdev->lock, flags);
1560}
1561