blob: 11944bda911c779f0afd2dc6a97b9231b2fca99c [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;
281 void *next = buf + USB_DT_CONFIG_SIZE;
282 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
283 struct usb_function *f;
284 int status;
Jared Suttles646bb862009-07-30 16:13:27 -0500285 int interfaceCount = 0;
David Brownell40982be2008-06-19 17:52:58 -0700286
287 /* write the config descriptor */
288 c = buf;
289 c->bLength = USB_DT_CONFIG_SIZE;
290 c->bDescriptorType = type;
291 /* wTotalLength is written later */
292 c->bNumInterfaces = config->next_interface_id;
293 c->bConfigurationValue = config->bConfigurationValue;
294 c->iConfiguration = config->iConfiguration;
295 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700296 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700297
298 /* There may be e.g. OTG descriptors */
299 if (config->descriptors) {
300 status = usb_descriptor_fillbuf(next, len,
301 config->descriptors);
302 if (status < 0)
303 return status;
304 len -= status;
305 next += status;
306 }
307
308 /* add each function's descriptors */
309 list_for_each_entry(f, &config->functions, list) {
310 struct usb_descriptor_header **descriptors;
311
312 if (speed == USB_SPEED_HIGH)
313 descriptors = f->hs_descriptors;
314 else
315 descriptors = f->descriptors;
Mike Lockwoodc7ba16a2009-12-11 11:24:07 -0500316 if (f->hidden || !descriptors || descriptors[0] == NULL) {
Jared Suttles646bb862009-07-30 16:13:27 -0500317 for (; f != config->interface[interfaceCount];) {
318 interfaceCount++;
319 c->bNumInterfaces--;
320 }
David Brownell40982be2008-06-19 17:52:58 -0700321 continue;
Jared Suttles646bb862009-07-30 16:13:27 -0500322 }
323 for (; f != config->interface[interfaceCount];)
324 interfaceCount++;
325
David Brownell40982be2008-06-19 17:52:58 -0700326 status = usb_descriptor_fillbuf(next, len,
327 (const struct usb_descriptor_header **) descriptors);
328 if (status < 0)
329 return status;
330 len -= status;
331 next += status;
332 }
333
334 len = next - buf;
335 c->wTotalLength = cpu_to_le16(len);
336 return len;
337}
338
339static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
340{
341 struct usb_gadget *gadget = cdev->gadget;
342 struct usb_configuration *c;
343 u8 type = w_value >> 8;
344 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
345
346 if (gadget_is_dualspeed(gadget)) {
347 int hs = 0;
348
349 if (gadget->speed == USB_SPEED_HIGH)
350 hs = 1;
351 if (type == USB_DT_OTHER_SPEED_CONFIG)
352 hs = !hs;
353 if (hs)
354 speed = USB_SPEED_HIGH;
355
356 }
357
358 /* This is a lookup by config *INDEX* */
359 w_value &= 0xff;
360 list_for_each_entry(c, &cdev->configs, list) {
361 /* ignore configs that won't work at this speed */
362 if (speed == USB_SPEED_HIGH) {
363 if (!c->highspeed)
364 continue;
365 } else {
366 if (!c->fullspeed)
367 continue;
368 }
369 if (w_value == 0)
370 return config_buf(c, speed, cdev->req->buf, type);
371 w_value--;
372 }
373 return -EINVAL;
374}
375
376static int count_configs(struct usb_composite_dev *cdev, unsigned type)
377{
378 struct usb_gadget *gadget = cdev->gadget;
379 struct usb_configuration *c;
380 unsigned count = 0;
381 int hs = 0;
382
383 if (gadget_is_dualspeed(gadget)) {
384 if (gadget->speed == USB_SPEED_HIGH)
385 hs = 1;
386 if (type == USB_DT_DEVICE_QUALIFIER)
387 hs = !hs;
388 }
389 list_for_each_entry(c, &cdev->configs, list) {
390 /* ignore configs that won't work at this speed */
391 if (hs) {
392 if (!c->highspeed)
393 continue;
394 } else {
395 if (!c->fullspeed)
396 continue;
397 }
398 count++;
399 }
400 return count;
401}
402
403static void device_qual(struct usb_composite_dev *cdev)
404{
405 struct usb_qualifier_descriptor *qual = cdev->req->buf;
406
407 qual->bLength = sizeof(*qual);
408 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
409 /* POLICY: same bcdUSB and device type info at both speeds */
410 qual->bcdUSB = cdev->desc.bcdUSB;
411 qual->bDeviceClass = cdev->desc.bDeviceClass;
412 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
413 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
414 /* ASSUME same EP0 fifo size at both speeds */
415 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
416 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700417 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700418}
419
420/*-------------------------------------------------------------------------*/
421
422static void reset_config(struct usb_composite_dev *cdev)
423{
424 struct usb_function *f;
425
426 DBG(cdev, "reset config\n");
427
428 list_for_each_entry(f, &cdev->config->functions, list) {
429 if (f->disable)
430 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200431
432 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700433 }
434 cdev->config = NULL;
435}
436
437static int set_config(struct usb_composite_dev *cdev,
438 const struct usb_ctrlrequest *ctrl, unsigned number)
439{
440 struct usb_gadget *gadget = cdev->gadget;
441 struct usb_configuration *c = NULL;
442 int result = -EINVAL;
443 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
444 int tmp;
445
446 if (cdev->config)
447 reset_config(cdev);
448
449 if (number) {
450 list_for_each_entry(c, &cdev->configs, list) {
451 if (c->bConfigurationValue == number) {
452 result = 0;
453 break;
454 }
455 }
456 if (result < 0)
457 goto done;
458 } else
459 result = 0;
460
461 INFO(cdev, "%s speed config #%d: %s\n",
462 ({ char *speed;
463 switch (gadget->speed) {
464 case USB_SPEED_LOW: speed = "low"; break;
465 case USB_SPEED_FULL: speed = "full"; break;
466 case USB_SPEED_HIGH: speed = "high"; break;
467 default: speed = "?"; break;
468 } ; speed; }), number, c ? c->label : "unconfigured");
469
470 if (!c)
471 goto done;
472
473 cdev->config = c;
474
475 /* Initialize all interfaces by setting them to altsetting zero. */
476 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
477 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200478 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700479
480 if (!f)
481 break;
482
Laurent Pinchart52426582009-10-21 00:03:38 +0200483 /*
484 * Record which endpoints are used by the function. This is used
485 * to dispatch control requests targeted at that endpoint to the
486 * function's setup callback instead of the current
487 * configuration's setup callback.
488 */
489 if (gadget->speed == USB_SPEED_HIGH)
490 descriptors = f->hs_descriptors;
491 else
492 descriptors = f->descriptors;
493
494 for (; *descriptors; ++descriptors) {
495 struct usb_endpoint_descriptor *ep;
496 int addr;
497
498 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
499 continue;
500
501 ep = (struct usb_endpoint_descriptor *)*descriptors;
502 addr = ((ep->bEndpointAddress & 0x80) >> 3)
503 | (ep->bEndpointAddress & 0x0f);
504 set_bit(addr, f->endpoints);
505 }
506
David Brownell40982be2008-06-19 17:52:58 -0700507 result = f->set_alt(f, tmp, 0);
508 if (result < 0) {
509 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
510 tmp, f->name, f, result);
511
512 reset_config(cdev);
513 goto done;
514 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300515
516 if (result == USB_GADGET_DELAYED_STATUS) {
517 DBG(cdev,
518 "%s: interface %d (%s) requested delayed status\n",
519 __func__, tmp, f->name);
520 cdev->delayed_status++;
521 DBG(cdev, "delayed_status count %d\n",
522 cdev->delayed_status);
523 }
David Brownell40982be2008-06-19 17:52:58 -0700524 }
525
526 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700527 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700528done:
529 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300530 if (result >= 0 && cdev->delayed_status)
531 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700532 return result;
533}
534
535/**
536 * usb_add_config() - add a configuration to a device.
537 * @cdev: wraps the USB gadget
538 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200539 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700540 * Context: single threaded during gadget setup
541 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200542 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700543 * add each of the configurations it supports, using this routine.
544 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200545 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700546 * is zero for success else a negative errno value. Binding configurations
547 * assigns global resources including string IDs, and per-configuration
548 * resources such as interface IDs and endpoints.
549 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200550int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200551 struct usb_configuration *config,
552 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700553{
554 int status = -EINVAL;
555 struct usb_configuration *c;
556
557 DBG(cdev, "adding config #%u '%s'/%p\n",
558 config->bConfigurationValue,
559 config->label, config);
560
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200561 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700562 goto done;
563
564 /* Prevent duplicate configuration identifiers */
565 list_for_each_entry(c, &cdev->configs, list) {
566 if (c->bConfigurationValue == config->bConfigurationValue) {
567 status = -EBUSY;
568 goto done;
569 }
570 }
571
572 config->cdev = cdev;
573 list_add_tail(&config->list, &cdev->configs);
574
575 INIT_LIST_HEAD(&config->functions);
576 config->next_interface_id = 0;
577
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200578 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700579 if (status < 0) {
580 list_del(&config->list);
581 config->cdev = NULL;
582 } else {
583 unsigned i;
584
585 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
586 config->bConfigurationValue, config,
587 config->highspeed ? " high" : "",
588 config->fullspeed
589 ? (gadget_is_dualspeed(cdev->gadget)
590 ? " full"
591 : " full/low")
592 : "");
593
594 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
595 struct usb_function *f = config->interface[i];
596
597 if (!f)
598 continue;
599 DBG(cdev, " interface %d = %s/%p\n",
600 i, f->name, f);
601 }
602 }
603
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200604 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700605 * ep->driver_data as needed.
606 */
607 usb_ep_autoconfig_reset(cdev->gadget);
608
609done:
610 if (status)
611 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
612 config->bConfigurationValue, status);
613 return status;
614}
615
616/*-------------------------------------------------------------------------*/
617
618/* We support strings in multiple languages ... string descriptor zero
619 * says which languages are supported. The typical case will be that
620 * only one language (probably English) is used, with I18N handled on
621 * the host side.
622 */
623
624static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
625{
626 const struct usb_gadget_strings *s;
627 u16 language;
628 __le16 *tmp;
629
630 while (*sp) {
631 s = *sp;
632 language = cpu_to_le16(s->language);
633 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
634 if (*tmp == language)
635 goto repeat;
636 }
637 *tmp++ = language;
638repeat:
639 sp++;
640 }
641}
642
643static int lookup_string(
644 struct usb_gadget_strings **sp,
645 void *buf,
646 u16 language,
647 int id
648)
649{
650 struct usb_gadget_strings *s;
651 int value;
652
653 while (*sp) {
654 s = *sp++;
655 if (s->language != language)
656 continue;
657 value = usb_gadget_get_string(s, id, buf);
658 if (value > 0)
659 return value;
660 }
661 return -EINVAL;
662}
663
664static int get_string(struct usb_composite_dev *cdev,
665 void *buf, u16 language, int id)
666{
667 struct usb_configuration *c;
668 struct usb_function *f;
669 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200670 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700671
672 /* Yes, not only is USB's I18N support probably more than most
673 * folk will ever care about ... also, it's all supported here.
674 * (Except for UTF8 support for Unicode's "Astral Planes".)
675 */
676
677 /* 0 == report all available language codes */
678 if (id == 0) {
679 struct usb_string_descriptor *s = buf;
680 struct usb_gadget_strings **sp;
681
682 memset(s, 0, 256);
683 s->bDescriptorType = USB_DT_STRING;
684
685 sp = composite->strings;
686 if (sp)
687 collect_langs(sp, s->wData);
688
689 list_for_each_entry(c, &cdev->configs, list) {
690 sp = c->strings;
691 if (sp)
692 collect_langs(sp, s->wData);
693
694 list_for_each_entry(f, &c->functions, list) {
695 sp = f->strings;
696 if (sp)
697 collect_langs(sp, s->wData);
698 }
699 }
700
Roel Kluin417b57b2009-08-06 16:09:51 -0700701 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700702 continue;
703 if (!len)
704 return -EINVAL;
705
706 s->bLength = 2 * (len + 1);
707 return s->bLength;
708 }
709
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200710 /* Otherwise, look up and return a specified string. First
711 * check if the string has not been overridden.
712 */
713 if (cdev->manufacturer_override == id)
714 str = iManufacturer ?: composite->iManufacturer ?:
715 composite_manufacturer;
716 else if (cdev->product_override == id)
717 str = iProduct ?: composite->iProduct;
718 else if (cdev->serial_override == id)
719 str = iSerialNumber;
720 else
721 str = NULL;
722 if (str) {
723 struct usb_gadget_strings strings = {
724 .language = language,
725 .strings = &(struct usb_string) { 0xff, str }
726 };
727 return usb_gadget_get_string(&strings, 0xff, buf);
728 }
729
730 /* String IDs are device-scoped, so we look up each string
731 * table we're told about. These lookups are infrequent;
732 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700733 */
734 if (composite->strings) {
735 len = lookup_string(composite->strings, buf, language, id);
736 if (len > 0)
737 return len;
738 }
739 list_for_each_entry(c, &cdev->configs, list) {
740 if (c->strings) {
741 len = lookup_string(c->strings, buf, language, id);
742 if (len > 0)
743 return len;
744 }
745 list_for_each_entry(f, &c->functions, list) {
746 if (!f->strings)
747 continue;
748 len = lookup_string(f->strings, buf, language, id);
749 if (len > 0)
750 return len;
751 }
752 }
753 return -EINVAL;
754}
755
756/**
757 * usb_string_id() - allocate an unused string ID
758 * @cdev: the device whose string descriptor IDs are being allocated
759 * Context: single threaded during gadget setup
760 *
761 * @usb_string_id() is called from bind() callbacks to allocate
762 * string IDs. Drivers for functions, configurations, or gadgets will
763 * then store that ID in the appropriate descriptors and string table.
764 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200765 * All string identifier should be allocated using this,
766 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
767 * that for example different functions don't wrongly assign different
768 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700769 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200770int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700771{
772 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200773 /* string id 0 is reserved by USB spec for list of
774 * supported languages */
775 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700776 cdev->next_string_id++;
777 return cdev->next_string_id;
778 }
779 return -ENODEV;
780}
781
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200782/**
783 * usb_string_ids() - allocate unused string IDs in batch
784 * @cdev: the device whose string descriptor IDs are being allocated
785 * @str: an array of usb_string objects to assign numbers to
786 * Context: single threaded during gadget setup
787 *
788 * @usb_string_ids() is called from bind() callbacks to allocate
789 * string IDs. Drivers for functions, configurations, or gadgets will
790 * then copy IDs from the string table to the appropriate descriptors
791 * and string table for other languages.
792 *
793 * All string identifier should be allocated using this,
794 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
795 * example different functions don't wrongly assign different meanings
796 * to the same identifier.
797 */
798int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
799{
800 int next = cdev->next_string_id;
801
802 for (; str->s; ++str) {
803 if (unlikely(next >= 254))
804 return -ENODEV;
805 str->id = ++next;
806 }
807
808 cdev->next_string_id = next;
809
810 return 0;
811}
812
813/**
814 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700815 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200816 * @n: number of string IDs to allocate
817 * Context: single threaded during gadget setup
818 *
819 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700820 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200821 * is, returns last requested ID which is now very useful information.
822 *
823 * @usb_string_ids_n() is called from bind() callbacks to allocate
824 * string IDs. Drivers for functions, configurations, or gadgets will
825 * then store that ID in the appropriate descriptors and string table.
826 *
827 * All string identifier should be allocated using this,
828 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
829 * example different functions don't wrongly assign different meanings
830 * to the same identifier.
831 */
832int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
833{
834 unsigned next = c->next_string_id;
835 if (unlikely(n > 254 || (unsigned)next + n > 254))
836 return -ENODEV;
837 c->next_string_id += n;
838 return next + 1;
839}
840
841
David Brownell40982be2008-06-19 17:52:58 -0700842/*-------------------------------------------------------------------------*/
843
844static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
845{
846 if (req->status || req->actual != req->length)
847 DBG((struct usb_composite_dev *) ep->driver_data,
848 "setup complete --> %d, %d/%d\n",
849 req->status, req->actual, req->length);
850}
851
852/*
853 * The setup() callback implements all the ep0 functionality that's
854 * not handled lower down, in hardware or the hardware driver(like
855 * device and endpoint feature flags, and their status). It's all
856 * housekeeping for the gadget function we're implementing. Most of
857 * the work is in config and function specific setup.
858 */
859static int
860composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
861{
862 struct usb_composite_dev *cdev = get_gadget_data(gadget);
863 struct usb_request *req = cdev->req;
864 int value = -EOPNOTSUPP;
865 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800866 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700867 u16 w_value = le16_to_cpu(ctrl->wValue);
868 u16 w_length = le16_to_cpu(ctrl->wLength);
869 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200870 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700871
872 /* partial re-init of the response message; the function or the
873 * gadget might need to intercept e.g. a control-OUT completion
874 * when we delegate to it.
875 */
876 req->zero = 0;
877 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530878 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700879 gadget->ep0->driver_data = cdev;
880
881 switch (ctrl->bRequest) {
882
883 /* we handle all standard USB descriptors */
884 case USB_REQ_GET_DESCRIPTOR:
885 if (ctrl->bRequestType != USB_DIR_IN)
886 goto unknown;
887 switch (w_value >> 8) {
888
889 case USB_DT_DEVICE:
890 cdev->desc.bNumConfigurations =
891 count_configs(cdev, USB_DT_DEVICE);
892 value = min(w_length, (u16) sizeof cdev->desc);
893 memcpy(req->buf, &cdev->desc, value);
894 break;
895 case USB_DT_DEVICE_QUALIFIER:
896 if (!gadget_is_dualspeed(gadget))
897 break;
898 device_qual(cdev);
899 value = min_t(int, w_length,
900 sizeof(struct usb_qualifier_descriptor));
901 break;
902 case USB_DT_OTHER_SPEED_CONFIG:
903 if (!gadget_is_dualspeed(gadget))
904 break;
905 /* FALLTHROUGH */
906 case USB_DT_CONFIG:
907 value = config_desc(cdev, w_value);
908 if (value >= 0)
909 value = min(w_length, (u16) value);
910 break;
911 case USB_DT_STRING:
912 value = get_string(cdev, req->buf,
913 w_index, w_value & 0xff);
914 if (value >= 0)
915 value = min(w_length, (u16) value);
916 break;
917 }
918 break;
919
920 /* any number of configs can work */
921 case USB_REQ_SET_CONFIGURATION:
922 if (ctrl->bRequestType != 0)
923 goto unknown;
924 if (gadget_is_otg(gadget)) {
925 if (gadget->a_hnp_support)
926 DBG(cdev, "HNP available\n");
927 else if (gadget->a_alt_hnp_support)
928 DBG(cdev, "HNP on another port\n");
929 else
930 VDBG(cdev, "HNP inactive\n");
931 }
932 spin_lock(&cdev->lock);
933 value = set_config(cdev, ctrl, w_value);
934 spin_unlock(&cdev->lock);
935 break;
936 case USB_REQ_GET_CONFIGURATION:
937 if (ctrl->bRequestType != USB_DIR_IN)
938 goto unknown;
Jared Suttles258d1032009-08-07 18:57:49 -0500939 if (cdev->config) {
David Brownell40982be2008-06-19 17:52:58 -0700940 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Jared Suttles258d1032009-08-07 18:57:49 -0500941 value = min(w_length, (u16) 1);
942 } else {
David Brownell40982be2008-06-19 17:52:58 -0700943 *(u8 *)req->buf = 0;
Jared Suttles258d1032009-08-07 18:57:49 -0500944 }
David Brownell40982be2008-06-19 17:52:58 -0700945 break;
946
947 /* function drivers must handle get/set altsetting; if there's
948 * no get() method, we know only altsetting zero works.
949 */
950 case USB_REQ_SET_INTERFACE:
951 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
952 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900953 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700954 break;
Bryan Wu08889512009-01-08 00:21:19 +0800955 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700956 if (!f)
957 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800958 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700959 break;
960 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300961 if (value == USB_GADGET_DELAYED_STATUS) {
962 DBG(cdev,
963 "%s: interface %d (%s) requested delayed status\n",
964 __func__, intf, f->name);
965 cdev->delayed_status++;
966 DBG(cdev, "delayed_status count %d\n",
967 cdev->delayed_status);
968 }
David Brownell40982be2008-06-19 17:52:58 -0700969 break;
970 case USB_REQ_GET_INTERFACE:
971 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
972 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900973 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700974 break;
Bryan Wu08889512009-01-08 00:21:19 +0800975 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700976 if (!f)
977 break;
978 /* lots of interfaces only need altsetting zero... */
979 value = f->get_alt ? f->get_alt(f, w_index) : 0;
980 if (value < 0)
981 break;
982 *((u8 *)req->buf) = value;
983 value = min(w_length, (u16) 1);
984 break;
985 default:
986unknown:
987 VDBG(cdev,
988 "non-core control req%02x.%02x v%04x i%04x l%d\n",
989 ctrl->bRequestType, ctrl->bRequest,
990 w_value, w_index, w_length);
991
Laurent Pinchart52426582009-10-21 00:03:38 +0200992 /* functions always handle their interfaces and endpoints...
993 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -0700994 * configuration code.
995 *
996 * REVISIT it could make sense to let the composite device
997 * take such requests too, if that's ever needed: to work
998 * in config 0, etc.
999 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001000 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1001 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001002 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301003 break;
1004 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001005 break;
1006
1007 case USB_RECIP_ENDPOINT:
1008 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1009 list_for_each_entry(f, &cdev->config->functions, list) {
1010 if (test_bit(endp, f->endpoints))
1011 break;
1012 }
1013 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001014 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001015 break;
David Brownell40982be2008-06-19 17:52:58 -07001016 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001017
1018 if (f && f->setup)
1019 value = f->setup(f, ctrl);
1020 else {
David Brownell40982be2008-06-19 17:52:58 -07001021 struct usb_configuration *c;
1022
1023 c = cdev->config;
1024 if (c && c->setup)
1025 value = c->setup(c, ctrl);
1026 }
1027
Joe Swantek292b1bc2009-12-15 07:17:40 -05001028 /* If the vendor request is not processed (value < 0),
1029 * call all device registered configure setup callbacks
1030 * to process it.
1031 * This is used to handle the following cases:
1032 * - vendor request is for the device and arrives before
1033 * setconfiguration.
1034 * - Some devices are required to handle vendor request before
1035 * setconfiguration such as MTP, USBNET.
1036 */
1037
1038 if (value < 0) {
1039 struct usb_configuration *cfg;
1040
1041 list_for_each_entry(cfg, &cdev->configs, list) {
1042 if (cfg && cfg->setup)
1043 value = cfg->setup(cfg, ctrl);
1044 }
1045 }
1046
David Brownell40982be2008-06-19 17:52:58 -07001047 goto done;
1048 }
1049
1050 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001051 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001052 req->length = value;
1053 req->zero = value < w_length;
1054 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1055 if (value < 0) {
1056 DBG(cdev, "ep_queue --> %d\n", value);
1057 req->status = 0;
1058 composite_setup_complete(gadget->ep0, req);
1059 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001060 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1061 WARN(cdev,
1062 "%s: Delayed status not supported for w_length != 0",
1063 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001064 }
1065
1066done:
1067 /* device either stalls (value < 0) or reports success */
1068 return value;
1069}
1070
1071static void composite_disconnect(struct usb_gadget *gadget)
1072{
1073 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1074 unsigned long flags;
1075
1076 /* REVISIT: should we have config and device level
1077 * disconnect callbacks?
1078 */
1079 spin_lock_irqsave(&cdev->lock, flags);
1080 if (cdev->config)
1081 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001082 if (composite->disconnect)
1083 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001084 spin_unlock_irqrestore(&cdev->lock, flags);
1085}
1086
1087/*-------------------------------------------------------------------------*/
1088
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001089static ssize_t composite_show_suspended(struct device *dev,
1090 struct device_attribute *attr,
1091 char *buf)
1092{
1093 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1094 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1095
1096 return sprintf(buf, "%d\n", cdev->suspended);
1097}
1098
1099static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1100
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001101static void
David Brownell40982be2008-06-19 17:52:58 -07001102composite_unbind(struct usb_gadget *gadget)
1103{
1104 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1105
1106 /* composite_disconnect() must already have been called
1107 * by the underlying peripheral controller driver!
1108 * so there's no i/o concurrency that could affect the
1109 * state protected by cdev->lock.
1110 */
1111 WARN_ON(cdev->config);
1112
1113 while (!list_empty(&cdev->configs)) {
1114 struct usb_configuration *c;
1115
1116 c = list_first_entry(&cdev->configs,
1117 struct usb_configuration, list);
1118 while (!list_empty(&c->functions)) {
1119 struct usb_function *f;
1120
1121 f = list_first_entry(&c->functions,
1122 struct usb_function, list);
1123 list_del(&f->list);
1124 if (f->unbind) {
1125 DBG(cdev, "unbind function '%s'/%p\n",
1126 f->name, f);
1127 f->unbind(c, f);
1128 /* may free memory for "f" */
1129 }
1130 }
1131 list_del(&c->list);
1132 if (c->unbind) {
1133 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1134 c->unbind(c);
1135 /* may free memory for "c" */
1136 }
1137 }
1138 if (composite->unbind)
1139 composite->unbind(cdev);
1140
1141 if (cdev->req) {
1142 kfree(cdev->req->buf);
1143 usb_ep_free_request(gadget->ep0, cdev->req);
1144 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301145 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001146 kfree(cdev);
1147 set_gadget_data(gadget, NULL);
1148 composite = NULL;
1149}
1150
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001151static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001152{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001153 if (!*desc) {
1154 int ret = usb_string_id(cdev);
1155 if (unlikely(ret < 0))
1156 WARNING(cdev, "failed to override string ID\n");
1157 else
1158 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001159 }
David Brownell40982be2008-06-19 17:52:58 -07001160
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001161 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001162}
1163
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001164static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001165{
1166 struct usb_composite_dev *cdev;
1167 int status = -ENOMEM;
1168
1169 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1170 if (!cdev)
1171 return status;
1172
1173 spin_lock_init(&cdev->lock);
1174 cdev->gadget = gadget;
1175 set_gadget_data(gadget, cdev);
1176 INIT_LIST_HEAD(&cdev->configs);
1177
1178 /* preallocate control response and buffer */
1179 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1180 if (!cdev->req)
1181 goto fail;
1182 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1183 if (!cdev->req->buf)
1184 goto fail;
1185 cdev->req->complete = composite_setup_complete;
1186 gadget->ep0->driver_data = cdev;
1187
1188 cdev->bufsiz = USB_BUFSIZ;
1189 cdev->driver = composite;
1190
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301191 /*
1192 * As per USB compliance update, a device that is actively drawing
1193 * more than 100mA from USB must report itself as bus-powered in
1194 * the GetStatus(DEVICE) call.
1195 */
1196 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1197 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001198
1199 /* interface and string IDs start at zero via kzalloc.
1200 * we force endpoints to start unassigned; few controller
1201 * drivers will zero ep->driver_data.
1202 */
1203 usb_ep_autoconfig_reset(cdev->gadget);
1204
1205 /* composite gadget needs to assign strings for whole device (like
1206 * serial number), register function drivers, potentially update
1207 * power state and consumption, etc
1208 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001209 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001210 if (status < 0)
1211 goto fail;
1212
1213 cdev->desc = *composite->dev;
1214 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1215
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001216 /* standardized runtime overrides for device ID data */
1217 if (idVendor)
1218 cdev->desc.idVendor = cpu_to_le16(idVendor);
1219 if (idProduct)
1220 cdev->desc.idProduct = cpu_to_le16(idProduct);
1221 if (bcdDevice)
1222 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1223
Marek Belisko78bff3c2010-10-27 10:19:01 +02001224 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001225 if (iManufacturer || !cdev->desc.iManufacturer) {
1226 if (!iManufacturer && !composite->iManufacturer &&
1227 !*composite_manufacturer)
1228 snprintf(composite_manufacturer,
1229 sizeof composite_manufacturer,
1230 "%s %s with %s",
1231 init_utsname()->sysname,
1232 init_utsname()->release,
1233 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001234
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001235 cdev->manufacturer_override =
1236 override_id(cdev, &cdev->desc.iManufacturer);
1237 }
1238
1239 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1240 cdev->product_override =
1241 override_id(cdev, &cdev->desc.iProduct);
1242
1243 if (iSerialNumber)
1244 cdev->serial_override =
1245 override_id(cdev, &cdev->desc.iSerialNumber);
1246
1247 /* has userspace failed to provide a serial number? */
1248 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1249 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1250
1251 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001252 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1253 if (status)
1254 goto fail;
1255
David Brownell40982be2008-06-19 17:52:58 -07001256 INFO(cdev, "%s ready\n", composite->name);
1257 return 0;
1258
1259fail:
1260 composite_unbind(gadget);
1261 return status;
1262}
1263
1264/*-------------------------------------------------------------------------*/
1265
1266static void
1267composite_suspend(struct usb_gadget *gadget)
1268{
1269 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1270 struct usb_function *f;
1271
David Brownell89429392009-03-19 14:14:17 -07001272 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001273 * suspend/resume callbacks?
1274 */
1275 DBG(cdev, "suspend\n");
1276 if (cdev->config) {
1277 list_for_each_entry(f, &cdev->config->functions, list) {
1278 if (f->suspend)
1279 f->suspend(f);
1280 }
1281 }
David Brownell89429392009-03-19 14:14:17 -07001282 if (composite->suspend)
1283 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001284
1285 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001286
1287 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001288}
1289
1290static void
1291composite_resume(struct usb_gadget *gadget)
1292{
1293 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1294 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001295 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001296
David Brownell89429392009-03-19 14:14:17 -07001297 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001298 * suspend/resume callbacks?
1299 */
1300 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001301 if (composite->resume)
1302 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001303 if (cdev->config) {
1304 list_for_each_entry(f, &cdev->config->functions, list) {
1305 if (f->resume)
1306 f->resume(f);
1307 }
Hao Wub23f2f92010-11-29 15:17:03 +08001308
1309 maxpower = cdev->config->bMaxPower;
1310
1311 usb_gadget_vbus_draw(gadget, maxpower ?
1312 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001313 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001314
1315 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001316}
1317
1318/*-------------------------------------------------------------------------*/
1319
1320static struct usb_gadget_driver composite_driver = {
1321 .speed = USB_SPEED_HIGH,
1322
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001323 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001324
1325 .setup = composite_setup,
1326 .disconnect = composite_disconnect,
1327
1328 .suspend = composite_suspend,
1329 .resume = composite_resume,
1330
1331 .driver = {
1332 .owner = THIS_MODULE,
1333 },
1334};
1335
1336/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001337 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001338 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001339 * @bind: the callback used to allocate resources that are shared across the
1340 * whole device, such as string IDs, and add its configurations using
1341 * @usb_add_config(). This may fail by returning a negative errno
1342 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001343 * Context: single threaded during gadget setup
1344 *
1345 * This function is used to register drivers using the composite driver
1346 * framework. The return value is zero, or a negative errno value.
1347 * Those values normally come from the driver's @bind method, which does
1348 * all the work of setting up the driver to match the hardware.
1349 *
1350 * On successful return, the gadget is ready to respond to requests from
1351 * the host, unless one of its components invokes usb_gadget_disconnect()
1352 * while it was binding. That would usually be done in order to wait for
1353 * some userspace participation.
1354 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001355int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001356 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001357{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001358 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001359 return -EINVAL;
1360
1361 if (!driver->name)
1362 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001363 if (!driver->iProduct)
1364 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001365 composite_driver.function = (char *) driver->name;
1366 composite_driver.driver.name = driver->name;
1367 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001368 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001369
Mike Lockwoodf041ac62010-02-06 21:53:51 -05001370 driver->class = class_create(THIS_MODULE, "usb_composite");
1371 if (IS_ERR(driver->class))
1372 return PTR_ERR(driver->class);
1373
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001374 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001375}
1376
1377/**
1378 * usb_composite_unregister() - unregister a composite driver
1379 * @driver: the driver to unregister
1380 *
1381 * This function is used to unregister drivers using the composite
1382 * driver framework.
1383 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001384void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001385{
1386 if (composite != driver)
1387 return;
1388 usb_gadget_unregister_driver(&composite_driver);
1389}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001390
1391/**
1392 * usb_composite_setup_continue() - Continue with the control transfer
1393 * @cdev: the composite device who's control transfer was kept waiting
1394 *
1395 * This function must be called by the USB function driver to continue
1396 * with the control transfer's data/status stage in case it had requested to
1397 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1398 * can request the composite framework to delay the setup request's data/status
1399 * stages by returning USB_GADGET_DELAYED_STATUS.
1400 */
1401void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1402{
1403 int value;
1404 struct usb_request *req = cdev->req;
1405 unsigned long flags;
1406
1407 DBG(cdev, "%s\n", __func__);
1408 spin_lock_irqsave(&cdev->lock, flags);
1409
1410 if (cdev->delayed_status == 0) {
1411 WARN(cdev, "%s: Unexpected call\n", __func__);
1412
1413 } else if (--cdev->delayed_status == 0) {
1414 DBG(cdev, "%s: Completing delayed status\n", __func__);
1415 req->length = 0;
1416 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1417 if (value < 0) {
1418 DBG(cdev, "ep_queue --> %d\n", value);
1419 req->status = 0;
1420 composite_setup_complete(cdev->gadget->ep0, req);
1421 }
1422 }
1423
1424 spin_unlock_irqrestore(&cdev->lock, flags);
1425}
1426