blob: cf7a5cf8503e25ba5ff0054ace23eff2b252d96b [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);
82 return sprintf(buf, "%d\n", !f->hidden);
83}
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
97 f->hidden = !value;
98
99 return size;
100}
101
102static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
103
104
David Brownell40982be2008-06-19 17:52:58 -0700105/**
106 * usb_add_function() - add a function to a configuration
107 * @config: the configuration
108 * @function: the function being added
109 * Context: single threaded during gadget setup
110 *
111 * After initialization, each configuration must have one or more
112 * functions added to it. Adding a function involves calling its @bind()
113 * method to allocate resources such as interface and string identifiers
114 * and endpoints.
115 *
116 * This function returns the value of the function's bind(), which is
117 * zero for success else a negative errno value.
118 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200119int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700120 struct usb_function *function)
121{
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500122 struct usb_composite_dev *cdev = config->cdev;
David Brownell40982be2008-06-19 17:52:58 -0700123 int value = -EINVAL;
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500124 int index;
David Brownell40982be2008-06-19 17:52:58 -0700125
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500126 DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -0700127 function->name, function,
128 config->label, config);
129
130 if (!function->set_alt || !function->disable)
131 goto done;
132
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500133 index = atomic_inc_return(&cdev->driver->function_count);
134 function->dev = device_create(cdev->driver->class, NULL,
135 MKDEV(0, index), NULL, function->name);
136 if (IS_ERR(function->dev))
137 return PTR_ERR(function->dev);
138
139 value = device_create_file(function->dev, &dev_attr_enable);
140 if (value < 0) {
141 device_destroy(cdev->driver->class, MKDEV(0, index));
142 return value;
143 }
144 dev_set_drvdata(function->dev, function);
145
David Brownell40982be2008-06-19 17:52:58 -0700146 function->config = config;
147 list_add_tail(&function->list, &config->functions);
148
149 /* REVISIT *require* function->bind? */
150 if (function->bind) {
151 value = function->bind(config, function);
152 if (value < 0) {
153 list_del(&function->list);
154 function->config = NULL;
155 }
156 } else
157 value = 0;
158
159 /* We allow configurations that don't work at both speeds.
160 * If we run into a lowspeed Linux system, treat it the same
161 * as full speed ... it's the function drivers that will need
162 * to avoid bulk and ISO transfers.
163 */
164 if (!config->fullspeed && function->descriptors)
165 config->fullspeed = true;
166 if (!config->highspeed && function->hs_descriptors)
167 config->highspeed = true;
168
169done:
170 if (value)
Mike Lockwoodf041ac62010-02-06 21:53:51 -0500171 DBG(cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700172 function->name, function, value);
173 return value;
174}
175
176/**
David Brownell60beed92008-08-18 17:38:22 -0700177 * usb_function_deactivate - prevent function and gadget enumeration
178 * @function: the function that isn't yet ready to respond
179 *
180 * Blocks response of the gadget driver to host enumeration by
181 * preventing the data line pullup from being activated. This is
182 * normally called during @bind() processing to change from the
183 * initial "ready to respond" state, or when a required resource
184 * becomes available.
185 *
186 * For example, drivers that serve as a passthrough to a userspace
187 * daemon can block enumeration unless that daemon (such as an OBEX,
188 * MTP, or print server) is ready to handle host requests.
189 *
190 * Not all systems support software control of their USB peripheral
191 * data pullups.
192 *
193 * Returns zero on success, else negative errno.
194 */
195int usb_function_deactivate(struct usb_function *function)
196{
197 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200198 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700199 int status = 0;
200
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200201 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700202
203 if (cdev->deactivations == 0)
204 status = usb_gadget_disconnect(cdev->gadget);
205 if (status == 0)
206 cdev->deactivations++;
207
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200208 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700209 return status;
210}
211
212/**
213 * usb_function_activate - allow function and gadget enumeration
214 * @function: function on which usb_function_activate() was called
215 *
216 * Reverses effect of usb_function_deactivate(). If no more functions
217 * are delaying their activation, the gadget driver will respond to
218 * host enumeration procedures.
219 *
220 * Returns zero on success, else negative errno.
221 */
222int usb_function_activate(struct usb_function *function)
223{
224 struct usb_composite_dev *cdev = function->config->cdev;
225 int status = 0;
226
227 spin_lock(&cdev->lock);
228
229 if (WARN_ON(cdev->deactivations == 0))
230 status = -EINVAL;
231 else {
232 cdev->deactivations--;
233 if (cdev->deactivations == 0)
234 status = usb_gadget_connect(cdev->gadget);
235 }
236
237 spin_unlock(&cdev->lock);
238 return status;
239}
240
241/**
David Brownell40982be2008-06-19 17:52:58 -0700242 * usb_interface_id() - allocate an unused interface ID
243 * @config: configuration associated with the interface
244 * @function: function handling the interface
245 * Context: single threaded during gadget setup
246 *
247 * usb_interface_id() is called from usb_function.bind() callbacks to
248 * allocate new interface IDs. The function driver will then store that
249 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300250 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700251 * particularly changing its altsetting via set_alt(). There may
252 * also be class-specific or vendor-specific requests to handle.
253 *
254 * All interface identifier should be allocated using this routine, to
255 * ensure that for example different functions don't wrongly assign
256 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300257 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700258 * one configuration (or more than once in a given configuration) need
259 * multiple versions of the relevant descriptors.
260 *
261 * Returns the interface ID which was allocated; or -ENODEV if no
262 * more interface IDs can be allocated.
263 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200264int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700265 struct usb_function *function)
266{
267 unsigned id = config->next_interface_id;
268
269 if (id < MAX_CONFIG_INTERFACES) {
270 config->interface[id] = function;
271 config->next_interface_id = id + 1;
272 return id;
273 }
274 return -ENODEV;
275}
276
277static int config_buf(struct usb_configuration *config,
278 enum usb_device_speed speed, void *buf, u8 type)
279{
280 struct usb_config_descriptor *c = buf;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500281 struct usb_interface_descriptor *intf;
David Brownell40982be2008-06-19 17:52:58 -0700282 void *next = buf + USB_DT_CONFIG_SIZE;
283 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
284 struct usb_function *f;
285 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500286 int interfaceCount = 0;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500287 u8 *dest;
David Brownell40982be2008-06-19 17:52:58 -0700288
289 /* write the config descriptor */
290 c = buf;
291 c->bLength = USB_DT_CONFIG_SIZE;
292 c->bDescriptorType = type;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500293 /* wTotalLength and bNumInterfaces are written later */
David Brownell40982be2008-06-19 17:52:58 -0700294 c->bConfigurationValue = config->bConfigurationValue;
295 c->iConfiguration = config->iConfiguration;
296 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700297 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700298
299 /* There may be e.g. OTG descriptors */
300 if (config->descriptors) {
301 status = usb_descriptor_fillbuf(next, len,
302 config->descriptors);
303 if (status < 0)
304 return status;
305 len -= status;
306 next += status;
307 }
308
309 /* add each function's descriptors */
310 list_for_each_entry(f, &config->functions, list) {
311 struct usb_descriptor_header **descriptors;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500312 struct usb_descriptor_header *descriptor;
David Brownell40982be2008-06-19 17:52:58 -0700313
314 if (speed == USB_SPEED_HIGH)
315 descriptors = f->hs_descriptors;
316 else
317 descriptors = f->descriptors;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500318 if (f->hidden || !descriptors || descriptors[0] == NULL)
David Brownell40982be2008-06-19 17:52:58 -0700319 continue;
320 status = usb_descriptor_fillbuf(next, len,
321 (const struct usb_descriptor_header **) descriptors);
322 if (status < 0)
323 return status;
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500324
325 /* set interface numbers dynamically */
326 dest = next;
327 while ((descriptor = *descriptors++) != NULL) {
328 intf = (struct usb_interface_descriptor *)dest;
329 if (intf->bDescriptorType == USB_DT_INTERFACE)
330 intf->bInterfaceNumber = interfaceCount++;
331 dest += intf->bLength;
332 }
333
David Brownell40982be2008-06-19 17:52:58 -0700334 len -= status;
335 next += status;
336 }
337
338 len = next - buf;
339 c->wTotalLength = cpu_to_le16(len);
Mike Lockwoodc832ca82010-02-13 19:16:07 -0500340 c->bNumInterfaces = interfaceCount;
David Brownell40982be2008-06-19 17:52:58 -0700341 return len;
342}
343
344static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
345{
346 struct usb_gadget *gadget = cdev->gadget;
347 struct usb_configuration *c;
348 u8 type = w_value >> 8;
349 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
350
351 if (gadget_is_dualspeed(gadget)) {
352 int hs = 0;
353
354 if (gadget->speed == USB_SPEED_HIGH)
355 hs = 1;
356 if (type == USB_DT_OTHER_SPEED_CONFIG)
357 hs = !hs;
358 if (hs)
359 speed = USB_SPEED_HIGH;
360
361 }
362
363 /* This is a lookup by config *INDEX* */
364 w_value &= 0xff;
365 list_for_each_entry(c, &cdev->configs, list) {
366 /* ignore configs that won't work at this speed */
367 if (speed == USB_SPEED_HIGH) {
368 if (!c->highspeed)
369 continue;
370 } else {
371 if (!c->fullspeed)
372 continue;
373 }
374 if (w_value == 0)
375 return config_buf(c, speed, cdev->req->buf, type);
376 w_value--;
377 }
378 return -EINVAL;
379}
380
381static int count_configs(struct usb_composite_dev *cdev, unsigned type)
382{
383 struct usb_gadget *gadget = cdev->gadget;
384 struct usb_configuration *c;
385 unsigned count = 0;
386 int hs = 0;
387
388 if (gadget_is_dualspeed(gadget)) {
389 if (gadget->speed == USB_SPEED_HIGH)
390 hs = 1;
391 if (type == USB_DT_DEVICE_QUALIFIER)
392 hs = !hs;
393 }
394 list_for_each_entry(c, &cdev->configs, list) {
395 /* ignore configs that won't work at this speed */
396 if (hs) {
397 if (!c->highspeed)
398 continue;
399 } else {
400 if (!c->fullspeed)
401 continue;
402 }
403 count++;
404 }
405 return count;
406}
407
408static void device_qual(struct usb_composite_dev *cdev)
409{
410 struct usb_qualifier_descriptor *qual = cdev->req->buf;
411
412 qual->bLength = sizeof(*qual);
413 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
414 /* POLICY: same bcdUSB and device type info at both speeds */
415 qual->bcdUSB = cdev->desc.bcdUSB;
416 qual->bDeviceClass = cdev->desc.bDeviceClass;
417 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
418 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
419 /* ASSUME same EP0 fifo size at both speeds */
420 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
421 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700422 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700423}
424
425/*-------------------------------------------------------------------------*/
426
427static void reset_config(struct usb_composite_dev *cdev)
428{
429 struct usb_function *f;
430
431 DBG(cdev, "reset config\n");
432
433 list_for_each_entry(f, &cdev->config->functions, list) {
434 if (f->disable)
435 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200436
437 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700438 }
439 cdev->config = NULL;
440}
441
442static int set_config(struct usb_composite_dev *cdev,
443 const struct usb_ctrlrequest *ctrl, unsigned number)
444{
445 struct usb_gadget *gadget = cdev->gadget;
446 struct usb_configuration *c = NULL;
447 int result = -EINVAL;
448 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
449 int tmp;
450
451 if (cdev->config)
452 reset_config(cdev);
453
454 if (number) {
455 list_for_each_entry(c, &cdev->configs, list) {
456 if (c->bConfigurationValue == number) {
457 result = 0;
458 break;
459 }
460 }
461 if (result < 0)
462 goto done;
463 } else
464 result = 0;
465
466 INFO(cdev, "%s speed config #%d: %s\n",
467 ({ char *speed;
468 switch (gadget->speed) {
469 case USB_SPEED_LOW: speed = "low"; break;
470 case USB_SPEED_FULL: speed = "full"; break;
471 case USB_SPEED_HIGH: speed = "high"; break;
472 default: speed = "?"; break;
473 } ; speed; }), number, c ? c->label : "unconfigured");
474
475 if (!c)
476 goto done;
477
478 cdev->config = c;
479
480 /* Initialize all interfaces by setting them to altsetting zero. */
481 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
482 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200483 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700484
485 if (!f)
486 break;
Mike Lockwood8c6a6b22010-02-26 09:30:01 -0500487 if (f->hidden)
488 continue;
David Brownell40982be2008-06-19 17:52:58 -0700489
Laurent Pinchart52426582009-10-21 00:03:38 +0200490 /*
491 * Record which endpoints are used by the function. This is used
492 * to dispatch control requests targeted at that endpoint to the
493 * function's setup callback instead of the current
494 * configuration's setup callback.
495 */
496 if (gadget->speed == USB_SPEED_HIGH)
497 descriptors = f->hs_descriptors;
498 else
499 descriptors = f->descriptors;
500
501 for (; *descriptors; ++descriptors) {
502 struct usb_endpoint_descriptor *ep;
503 int addr;
504
505 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
506 continue;
507
508 ep = (struct usb_endpoint_descriptor *)*descriptors;
509 addr = ((ep->bEndpointAddress & 0x80) >> 3)
510 | (ep->bEndpointAddress & 0x0f);
511 set_bit(addr, f->endpoints);
512 }
513
David Brownell40982be2008-06-19 17:52:58 -0700514 result = f->set_alt(f, tmp, 0);
515 if (result < 0) {
516 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
517 tmp, f->name, f, result);
518
519 reset_config(cdev);
520 goto done;
521 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300522
523 if (result == USB_GADGET_DELAYED_STATUS) {
524 DBG(cdev,
525 "%s: interface %d (%s) requested delayed status\n",
526 __func__, tmp, f->name);
527 cdev->delayed_status++;
528 DBG(cdev, "delayed_status count %d\n",
529 cdev->delayed_status);
530 }
David Brownell40982be2008-06-19 17:52:58 -0700531 }
532
533 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700534 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700535done:
536 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300537 if (result >= 0 && cdev->delayed_status)
538 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700539 return result;
540}
541
542/**
543 * usb_add_config() - add a configuration to a device.
544 * @cdev: wraps the USB gadget
545 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200546 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700547 * Context: single threaded during gadget setup
548 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200549 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700550 * add each of the configurations it supports, using this routine.
551 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200552 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700553 * is zero for success else a negative errno value. Binding configurations
554 * assigns global resources including string IDs, and per-configuration
555 * resources such as interface IDs and endpoints.
556 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200557int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200558 struct usb_configuration *config,
559 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700560{
561 int status = -EINVAL;
562 struct usb_configuration *c;
563
564 DBG(cdev, "adding config #%u '%s'/%p\n",
565 config->bConfigurationValue,
566 config->label, config);
567
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200568 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700569 goto done;
570
571 /* Prevent duplicate configuration identifiers */
572 list_for_each_entry(c, &cdev->configs, list) {
573 if (c->bConfigurationValue == config->bConfigurationValue) {
574 status = -EBUSY;
575 goto done;
576 }
577 }
578
579 config->cdev = cdev;
580 list_add_tail(&config->list, &cdev->configs);
581
582 INIT_LIST_HEAD(&config->functions);
583 config->next_interface_id = 0;
584
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200585 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700586 if (status < 0) {
587 list_del(&config->list);
588 config->cdev = NULL;
589 } else {
590 unsigned i;
591
592 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
593 config->bConfigurationValue, config,
594 config->highspeed ? " high" : "",
595 config->fullspeed
596 ? (gadget_is_dualspeed(cdev->gadget)
597 ? " full"
598 : " full/low")
599 : "");
600
601 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
602 struct usb_function *f = config->interface[i];
603
604 if (!f)
605 continue;
606 DBG(cdev, " interface %d = %s/%p\n",
607 i, f->name, f);
608 }
609 }
610
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200611 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700612 * ep->driver_data as needed.
613 */
614 usb_ep_autoconfig_reset(cdev->gadget);
615
616done:
617 if (status)
618 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
619 config->bConfigurationValue, status);
620 return status;
621}
622
623/*-------------------------------------------------------------------------*/
624
625/* We support strings in multiple languages ... string descriptor zero
626 * says which languages are supported. The typical case will be that
627 * only one language (probably English) is used, with I18N handled on
628 * the host side.
629 */
630
631static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
632{
633 const struct usb_gadget_strings *s;
634 u16 language;
635 __le16 *tmp;
636
637 while (*sp) {
638 s = *sp;
639 language = cpu_to_le16(s->language);
640 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
641 if (*tmp == language)
642 goto repeat;
643 }
644 *tmp++ = language;
645repeat:
646 sp++;
647 }
648}
649
650static int lookup_string(
651 struct usb_gadget_strings **sp,
652 void *buf,
653 u16 language,
654 int id
655)
656{
657 struct usb_gadget_strings *s;
658 int value;
659
660 while (*sp) {
661 s = *sp++;
662 if (s->language != language)
663 continue;
664 value = usb_gadget_get_string(s, id, buf);
665 if (value > 0)
666 return value;
667 }
668 return -EINVAL;
669}
670
671static int get_string(struct usb_composite_dev *cdev,
672 void *buf, u16 language, int id)
673{
674 struct usb_configuration *c;
675 struct usb_function *f;
676 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200677 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700678
679 /* Yes, not only is USB's I18N support probably more than most
680 * folk will ever care about ... also, it's all supported here.
681 * (Except for UTF8 support for Unicode's "Astral Planes".)
682 */
683
684 /* 0 == report all available language codes */
685 if (id == 0) {
686 struct usb_string_descriptor *s = buf;
687 struct usb_gadget_strings **sp;
688
689 memset(s, 0, 256);
690 s->bDescriptorType = USB_DT_STRING;
691
692 sp = composite->strings;
693 if (sp)
694 collect_langs(sp, s->wData);
695
696 list_for_each_entry(c, &cdev->configs, list) {
697 sp = c->strings;
698 if (sp)
699 collect_langs(sp, s->wData);
700
701 list_for_each_entry(f, &c->functions, list) {
702 sp = f->strings;
703 if (sp)
704 collect_langs(sp, s->wData);
705 }
706 }
707
Roel Kluin417b57b2009-08-06 16:09:51 -0700708 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700709 continue;
710 if (!len)
711 return -EINVAL;
712
713 s->bLength = 2 * (len + 1);
714 return s->bLength;
715 }
716
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200717 /* Otherwise, look up and return a specified string. First
718 * check if the string has not been overridden.
719 */
720 if (cdev->manufacturer_override == id)
721 str = iManufacturer ?: composite->iManufacturer ?:
722 composite_manufacturer;
723 else if (cdev->product_override == id)
724 str = iProduct ?: composite->iProduct;
725 else if (cdev->serial_override == id)
726 str = iSerialNumber;
727 else
728 str = NULL;
729 if (str) {
730 struct usb_gadget_strings strings = {
731 .language = language,
732 .strings = &(struct usb_string) { 0xff, str }
733 };
734 return usb_gadget_get_string(&strings, 0xff, buf);
735 }
736
737 /* String IDs are device-scoped, so we look up each string
738 * table we're told about. These lookups are infrequent;
739 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700740 */
741 if (composite->strings) {
742 len = lookup_string(composite->strings, buf, language, id);
743 if (len > 0)
744 return len;
745 }
746 list_for_each_entry(c, &cdev->configs, list) {
747 if (c->strings) {
748 len = lookup_string(c->strings, buf, language, id);
749 if (len > 0)
750 return len;
751 }
752 list_for_each_entry(f, &c->functions, list) {
753 if (!f->strings)
754 continue;
755 len = lookup_string(f->strings, buf, language, id);
756 if (len > 0)
757 return len;
758 }
759 }
760 return -EINVAL;
761}
762
763/**
764 * usb_string_id() - allocate an unused string ID
765 * @cdev: the device whose string descriptor IDs are being allocated
766 * Context: single threaded during gadget setup
767 *
768 * @usb_string_id() is called from bind() callbacks to allocate
769 * string IDs. Drivers for functions, configurations, or gadgets will
770 * then store that ID in the appropriate descriptors and string table.
771 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200772 * All string identifier should be allocated using this,
773 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
774 * that for example different functions don't wrongly assign different
775 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700776 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200777int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700778{
779 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200780 /* string id 0 is reserved by USB spec for list of
781 * supported languages */
782 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700783 cdev->next_string_id++;
784 return cdev->next_string_id;
785 }
786 return -ENODEV;
787}
788
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200789/**
790 * usb_string_ids() - allocate unused string IDs in batch
791 * @cdev: the device whose string descriptor IDs are being allocated
792 * @str: an array of usb_string objects to assign numbers to
793 * Context: single threaded during gadget setup
794 *
795 * @usb_string_ids() is called from bind() callbacks to allocate
796 * string IDs. Drivers for functions, configurations, or gadgets will
797 * then copy IDs from the string table to the appropriate descriptors
798 * and string table for other languages.
799 *
800 * All string identifier should be allocated using this,
801 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
802 * example different functions don't wrongly assign different meanings
803 * to the same identifier.
804 */
805int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
806{
807 int next = cdev->next_string_id;
808
809 for (; str->s; ++str) {
810 if (unlikely(next >= 254))
811 return -ENODEV;
812 str->id = ++next;
813 }
814
815 cdev->next_string_id = next;
816
817 return 0;
818}
819
820/**
821 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700822 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200823 * @n: number of string IDs to allocate
824 * Context: single threaded during gadget setup
825 *
826 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700827 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200828 * is, returns last requested ID which is now very useful information.
829 *
830 * @usb_string_ids_n() is called from bind() callbacks to allocate
831 * string IDs. Drivers for functions, configurations, or gadgets will
832 * then store that ID in the appropriate descriptors and string table.
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_n(struct usb_composite_dev *c, unsigned n)
840{
841 unsigned next = c->next_string_id;
842 if (unlikely(n > 254 || (unsigned)next + n > 254))
843 return -ENODEV;
844 c->next_string_id += n;
845 return next + 1;
846}
847
848
David Brownell40982be2008-06-19 17:52:58 -0700849/*-------------------------------------------------------------------------*/
850
851static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
852{
853 if (req->status || req->actual != req->length)
854 DBG((struct usb_composite_dev *) ep->driver_data,
855 "setup complete --> %d, %d/%d\n",
856 req->status, req->actual, req->length);
857}
858
859/*
860 * The setup() callback implements all the ep0 functionality that's
861 * not handled lower down, in hardware or the hardware driver(like
862 * device and endpoint feature flags, and their status). It's all
863 * housekeeping for the gadget function we're implementing. Most of
864 * the work is in config and function specific setup.
865 */
866static int
867composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
868{
869 struct usb_composite_dev *cdev = get_gadget_data(gadget);
870 struct usb_request *req = cdev->req;
871 int value = -EOPNOTSUPP;
872 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800873 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700874 u16 w_value = le16_to_cpu(ctrl->wValue);
875 u16 w_length = le16_to_cpu(ctrl->wLength);
876 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200877 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700878
879 /* partial re-init of the response message; the function or the
880 * gadget might need to intercept e.g. a control-OUT completion
881 * when we delegate to it.
882 */
883 req->zero = 0;
884 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530885 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700886 gadget->ep0->driver_data = cdev;
887
888 switch (ctrl->bRequest) {
889
890 /* we handle all standard USB descriptors */
891 case USB_REQ_GET_DESCRIPTOR:
892 if (ctrl->bRequestType != USB_DIR_IN)
893 goto unknown;
894 switch (w_value >> 8) {
895
896 case USB_DT_DEVICE:
897 cdev->desc.bNumConfigurations =
898 count_configs(cdev, USB_DT_DEVICE);
899 value = min(w_length, (u16) sizeof cdev->desc);
900 memcpy(req->buf, &cdev->desc, value);
901 break;
902 case USB_DT_DEVICE_QUALIFIER:
903 if (!gadget_is_dualspeed(gadget))
904 break;
905 device_qual(cdev);
906 value = min_t(int, w_length,
907 sizeof(struct usb_qualifier_descriptor));
908 break;
909 case USB_DT_OTHER_SPEED_CONFIG:
910 if (!gadget_is_dualspeed(gadget))
911 break;
912 /* FALLTHROUGH */
913 case USB_DT_CONFIG:
914 value = config_desc(cdev, w_value);
915 if (value >= 0)
916 value = min(w_length, (u16) value);
917 break;
918 case USB_DT_STRING:
919 value = get_string(cdev, req->buf,
920 w_index, w_value & 0xff);
921 if (value >= 0)
922 value = min(w_length, (u16) value);
923 break;
924 }
925 break;
926
927 /* any number of configs can work */
928 case USB_REQ_SET_CONFIGURATION:
929 if (ctrl->bRequestType != 0)
930 goto unknown;
931 if (gadget_is_otg(gadget)) {
932 if (gadget->a_hnp_support)
933 DBG(cdev, "HNP available\n");
934 else if (gadget->a_alt_hnp_support)
935 DBG(cdev, "HNP on another port\n");
936 else
937 VDBG(cdev, "HNP inactive\n");
938 }
939 spin_lock(&cdev->lock);
940 value = set_config(cdev, ctrl, w_value);
941 spin_unlock(&cdev->lock);
942 break;
943 case USB_REQ_GET_CONFIGURATION:
944 if (ctrl->bRequestType != USB_DIR_IN)
945 goto unknown;
Jared Suttles258d1032009-08-07 18:57:49 -0500946 if (cdev->config) {
David Brownell40982be2008-06-19 17:52:58 -0700947 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Jared Suttles258d1032009-08-07 18:57:49 -0500948 value = min(w_length, (u16) 1);
949 } else {
David Brownell40982be2008-06-19 17:52:58 -0700950 *(u8 *)req->buf = 0;
Jared Suttles258d1032009-08-07 18:57:49 -0500951 }
David Brownell40982be2008-06-19 17:52:58 -0700952 break;
953
954 /* function drivers must handle get/set altsetting; if there's
955 * no get() method, we know only altsetting zero works.
956 */
957 case USB_REQ_SET_INTERFACE:
958 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
959 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900960 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700961 break;
Bryan Wu08889512009-01-08 00:21:19 +0800962 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700963 if (!f)
964 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800965 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700966 break;
967 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300968 if (value == USB_GADGET_DELAYED_STATUS) {
969 DBG(cdev,
970 "%s: interface %d (%s) requested delayed status\n",
971 __func__, intf, f->name);
972 cdev->delayed_status++;
973 DBG(cdev, "delayed_status count %d\n",
974 cdev->delayed_status);
975 }
David Brownell40982be2008-06-19 17:52:58 -0700976 break;
977 case USB_REQ_GET_INTERFACE:
978 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
979 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900980 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700981 break;
Bryan Wu08889512009-01-08 00:21:19 +0800982 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700983 if (!f)
984 break;
985 /* lots of interfaces only need altsetting zero... */
986 value = f->get_alt ? f->get_alt(f, w_index) : 0;
987 if (value < 0)
988 break;
989 *((u8 *)req->buf) = value;
990 value = min(w_length, (u16) 1);
991 break;
992 default:
993unknown:
994 VDBG(cdev,
995 "non-core control req%02x.%02x v%04x i%04x l%d\n",
996 ctrl->bRequestType, ctrl->bRequest,
997 w_value, w_index, w_length);
998
Laurent Pinchart52426582009-10-21 00:03:38 +0200999 /* functions always handle their interfaces and endpoints...
1000 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001001 * configuration code.
1002 *
1003 * REVISIT it could make sense to let the composite device
1004 * take such requests too, if that's ever needed: to work
1005 * in config 0, etc.
1006 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001007 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1008 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001009 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301010 break;
1011 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001012 break;
1013
1014 case USB_RECIP_ENDPOINT:
1015 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1016 list_for_each_entry(f, &cdev->config->functions, list) {
1017 if (test_bit(endp, f->endpoints))
1018 break;
1019 }
1020 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001021 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001022 break;
David Brownell40982be2008-06-19 17:52:58 -07001023 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001024
1025 if (f && f->setup)
1026 value = f->setup(f, ctrl);
1027 else {
David Brownell40982be2008-06-19 17:52:58 -07001028 struct usb_configuration *c;
1029
1030 c = cdev->config;
1031 if (c && c->setup)
1032 value = c->setup(c, ctrl);
1033 }
1034
Joe Swantek292b1bc2009-12-15 07:17:40 -05001035 /* If the vendor request is not processed (value < 0),
1036 * call all device registered configure setup callbacks
1037 * to process it.
1038 * This is used to handle the following cases:
1039 * - vendor request is for the device and arrives before
1040 * setconfiguration.
1041 * - Some devices are required to handle vendor request before
1042 * setconfiguration such as MTP, USBNET.
1043 */
1044
1045 if (value < 0) {
1046 struct usb_configuration *cfg;
1047
1048 list_for_each_entry(cfg, &cdev->configs, list) {
1049 if (cfg && cfg->setup)
1050 value = cfg->setup(cfg, ctrl);
1051 }
1052 }
1053
David Brownell40982be2008-06-19 17:52:58 -07001054 goto done;
1055 }
1056
1057 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001058 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001059 req->length = value;
1060 req->zero = value < w_length;
1061 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1062 if (value < 0) {
1063 DBG(cdev, "ep_queue --> %d\n", value);
1064 req->status = 0;
1065 composite_setup_complete(gadget->ep0, req);
1066 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001067 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1068 WARN(cdev,
1069 "%s: Delayed status not supported for w_length != 0",
1070 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001071 }
1072
1073done:
1074 /* device either stalls (value < 0) or reports success */
1075 return value;
1076}
1077
1078static void composite_disconnect(struct usb_gadget *gadget)
1079{
1080 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1081 unsigned long flags;
1082
1083 /* REVISIT: should we have config and device level
1084 * disconnect callbacks?
1085 */
1086 spin_lock_irqsave(&cdev->lock, flags);
1087 if (cdev->config)
1088 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001089 if (composite->disconnect)
1090 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001091 spin_unlock_irqrestore(&cdev->lock, flags);
1092}
1093
1094/*-------------------------------------------------------------------------*/
1095
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001096static ssize_t composite_show_suspended(struct device *dev,
1097 struct device_attribute *attr,
1098 char *buf)
1099{
1100 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1101 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1102
1103 return sprintf(buf, "%d\n", cdev->suspended);
1104}
1105
1106static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1107
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001108static void
David Brownell40982be2008-06-19 17:52:58 -07001109composite_unbind(struct usb_gadget *gadget)
1110{
1111 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1112
1113 /* composite_disconnect() must already have been called
1114 * by the underlying peripheral controller driver!
1115 * so there's no i/o concurrency that could affect the
1116 * state protected by cdev->lock.
1117 */
1118 WARN_ON(cdev->config);
1119
1120 while (!list_empty(&cdev->configs)) {
1121 struct usb_configuration *c;
1122
1123 c = list_first_entry(&cdev->configs,
1124 struct usb_configuration, list);
1125 while (!list_empty(&c->functions)) {
1126 struct usb_function *f;
1127
1128 f = list_first_entry(&c->functions,
1129 struct usb_function, list);
1130 list_del(&f->list);
1131 if (f->unbind) {
1132 DBG(cdev, "unbind function '%s'/%p\n",
1133 f->name, f);
1134 f->unbind(c, f);
1135 /* may free memory for "f" */
1136 }
1137 }
1138 list_del(&c->list);
1139 if (c->unbind) {
1140 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1141 c->unbind(c);
1142 /* may free memory for "c" */
1143 }
1144 }
1145 if (composite->unbind)
1146 composite->unbind(cdev);
1147
1148 if (cdev->req) {
1149 kfree(cdev->req->buf);
1150 usb_ep_free_request(gadget->ep0, cdev->req);
1151 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301152 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001153 kfree(cdev);
1154 set_gadget_data(gadget, NULL);
1155 composite = NULL;
1156}
1157
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001158static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001159{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001160 if (!*desc) {
1161 int ret = usb_string_id(cdev);
1162 if (unlikely(ret < 0))
1163 WARNING(cdev, "failed to override string ID\n");
1164 else
1165 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001166 }
David Brownell40982be2008-06-19 17:52:58 -07001167
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001168 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001169}
1170
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001171static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001172{
1173 struct usb_composite_dev *cdev;
1174 int status = -ENOMEM;
1175
1176 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1177 if (!cdev)
1178 return status;
1179
1180 spin_lock_init(&cdev->lock);
1181 cdev->gadget = gadget;
1182 set_gadget_data(gadget, cdev);
1183 INIT_LIST_HEAD(&cdev->configs);
1184
1185 /* preallocate control response and buffer */
1186 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1187 if (!cdev->req)
1188 goto fail;
1189 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1190 if (!cdev->req->buf)
1191 goto fail;
1192 cdev->req->complete = composite_setup_complete;
1193 gadget->ep0->driver_data = cdev;
1194
1195 cdev->bufsiz = USB_BUFSIZ;
1196 cdev->driver = composite;
1197
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301198 /*
1199 * As per USB compliance update, a device that is actively drawing
1200 * more than 100mA from USB must report itself as bus-powered in
1201 * the GetStatus(DEVICE) call.
1202 */
1203 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1204 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001205
1206 /* interface and string IDs start at zero via kzalloc.
1207 * we force endpoints to start unassigned; few controller
1208 * drivers will zero ep->driver_data.
1209 */
1210 usb_ep_autoconfig_reset(cdev->gadget);
1211
1212 /* composite gadget needs to assign strings for whole device (like
1213 * serial number), register function drivers, potentially update
1214 * power state and consumption, etc
1215 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001216 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001217 if (status < 0)
1218 goto fail;
1219
1220 cdev->desc = *composite->dev;
1221 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1222
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001223 /* standardized runtime overrides for device ID data */
1224 if (idVendor)
1225 cdev->desc.idVendor = cpu_to_le16(idVendor);
1226 if (idProduct)
1227 cdev->desc.idProduct = cpu_to_le16(idProduct);
1228 if (bcdDevice)
1229 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1230
Marek Belisko78bff3c2010-10-27 10:19:01 +02001231 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001232 if (iManufacturer || !cdev->desc.iManufacturer) {
1233 if (!iManufacturer && !composite->iManufacturer &&
1234 !*composite_manufacturer)
1235 snprintf(composite_manufacturer,
1236 sizeof composite_manufacturer,
1237 "%s %s with %s",
1238 init_utsname()->sysname,
1239 init_utsname()->release,
1240 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001241
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001242 cdev->manufacturer_override =
1243 override_id(cdev, &cdev->desc.iManufacturer);
1244 }
1245
1246 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1247 cdev->product_override =
1248 override_id(cdev, &cdev->desc.iProduct);
1249
1250 if (iSerialNumber)
1251 cdev->serial_override =
1252 override_id(cdev, &cdev->desc.iSerialNumber);
1253
1254 /* has userspace failed to provide a serial number? */
1255 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1256 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1257
1258 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001259 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1260 if (status)
1261 goto fail;
1262
David Brownell40982be2008-06-19 17:52:58 -07001263 INFO(cdev, "%s ready\n", composite->name);
1264 return 0;
1265
1266fail:
1267 composite_unbind(gadget);
1268 return status;
1269}
1270
1271/*-------------------------------------------------------------------------*/
1272
1273static void
1274composite_suspend(struct usb_gadget *gadget)
1275{
1276 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1277 struct usb_function *f;
1278
David Brownell89429392009-03-19 14:14:17 -07001279 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001280 * suspend/resume callbacks?
1281 */
1282 DBG(cdev, "suspend\n");
1283 if (cdev->config) {
1284 list_for_each_entry(f, &cdev->config->functions, list) {
1285 if (f->suspend)
1286 f->suspend(f);
1287 }
1288 }
David Brownell89429392009-03-19 14:14:17 -07001289 if (composite->suspend)
1290 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001291
1292 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001293
1294 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001295}
1296
1297static void
1298composite_resume(struct usb_gadget *gadget)
1299{
1300 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1301 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001302 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001303
David Brownell89429392009-03-19 14:14:17 -07001304 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001305 * suspend/resume callbacks?
1306 */
1307 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001308 if (composite->resume)
1309 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001310 if (cdev->config) {
1311 list_for_each_entry(f, &cdev->config->functions, list) {
1312 if (f->resume)
1313 f->resume(f);
1314 }
Hao Wub23f2f92010-11-29 15:17:03 +08001315
1316 maxpower = cdev->config->bMaxPower;
1317
1318 usb_gadget_vbus_draw(gadget, maxpower ?
1319 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001320 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001321
1322 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001323}
1324
1325/*-------------------------------------------------------------------------*/
1326
1327static struct usb_gadget_driver composite_driver = {
1328 .speed = USB_SPEED_HIGH,
1329
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001330 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001331
1332 .setup = composite_setup,
1333 .disconnect = composite_disconnect,
1334
1335 .suspend = composite_suspend,
1336 .resume = composite_resume,
1337
1338 .driver = {
1339 .owner = THIS_MODULE,
1340 },
1341};
1342
1343/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001344 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001345 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001346 * @bind: the callback used to allocate resources that are shared across the
1347 * whole device, such as string IDs, and add its configurations using
1348 * @usb_add_config(). This may fail by returning a negative errno
1349 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001350 * Context: single threaded during gadget setup
1351 *
1352 * This function is used to register drivers using the composite driver
1353 * framework. The return value is zero, or a negative errno value.
1354 * Those values normally come from the driver's @bind method, which does
1355 * all the work of setting up the driver to match the hardware.
1356 *
1357 * On successful return, the gadget is ready to respond to requests from
1358 * the host, unless one of its components invokes usb_gadget_disconnect()
1359 * while it was binding. That would usually be done in order to wait for
1360 * some userspace participation.
1361 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001362int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001363 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001364{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001365 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001366 return -EINVAL;
1367
1368 if (!driver->name)
1369 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001370 if (!driver->iProduct)
1371 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001372 composite_driver.function = (char *) driver->name;
1373 composite_driver.driver.name = driver->name;
1374 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001375 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001376
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001377 driver->class = class_create(THIS_MODULE, "usb_composite");
1378 if (IS_ERR(driver->class))
1379 return PTR_ERR(driver->class);
1380
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001381 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001382}
1383
1384/**
1385 * usb_composite_unregister() - unregister a composite driver
1386 * @driver: the driver to unregister
1387 *
1388 * This function is used to unregister drivers using the composite
1389 * driver framework.
1390 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001391void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001392{
1393 if (composite != driver)
1394 return;
1395 usb_gadget_unregister_driver(&composite_driver);
1396}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001397
1398/**
1399 * usb_composite_setup_continue() - Continue with the control transfer
1400 * @cdev: the composite device who's control transfer was kept waiting
1401 *
1402 * This function must be called by the USB function driver to continue
1403 * with the control transfer's data/status stage in case it had requested to
1404 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1405 * can request the composite framework to delay the setup request's data/status
1406 * stages by returning USB_GADGET_DELAYED_STATUS.
1407 */
1408void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1409{
1410 int value;
1411 struct usb_request *req = cdev->req;
1412 unsigned long flags;
1413
1414 DBG(cdev, "%s\n", __func__);
1415 spin_lock_irqsave(&cdev->lock, flags);
1416
1417 if (cdev->delayed_status == 0) {
1418 WARN(cdev, "%s: Unexpected call\n", __func__);
1419
1420 } else if (--cdev->delayed_status == 0) {
1421 DBG(cdev, "%s: Completing delayed status\n", __func__);
1422 req->length = 0;
1423 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1424 if (value < 0) {
1425 DBG(cdev, "ep_queue --> %d\n", value);
1426 req->status = 0;
1427 composite_setup_complete(cdev->gadget->ep0, req);
1428 }
1429 }
1430
1431 spin_unlock_irqrestore(&cdev->lock, flags);
1432}
1433