blob: dc06da669739548c109c8002666f163c624c6aed [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>
Benoit Gobyaab96812011-04-19 20:37:33 -070028
David Brownell40982be2008-06-19 17:52:58 -070029#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
78/**
79 * usb_add_function() - add a function to a configuration
80 * @config: the configuration
81 * @function: the function being added
82 * Context: single threaded during gadget setup
83 *
84 * After initialization, each configuration must have one or more
85 * functions added to it. Adding a function involves calling its @bind()
86 * method to allocate resources such as interface and string identifiers
87 * and endpoints.
88 *
89 * This function returns the value of the function's bind(), which is
90 * zero for success else a negative errno value.
91 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +020092int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -070093 struct usb_function *function)
94{
95 int value = -EINVAL;
96
Benoit Gobyaab96812011-04-19 20:37:33 -070097 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -070098 function->name, function,
99 config->label, config);
100
101 if (!function->set_alt || !function->disable)
102 goto done;
103
104 function->config = config;
105 list_add_tail(&function->list, &config->functions);
106
107 /* REVISIT *require* function->bind? */
108 if (function->bind) {
109 value = function->bind(config, function);
110 if (value < 0) {
111 list_del(&function->list);
112 function->config = NULL;
113 }
114 } else
115 value = 0;
116
117 /* We allow configurations that don't work at both speeds.
118 * If we run into a lowspeed Linux system, treat it the same
119 * as full speed ... it's the function drivers that will need
120 * to avoid bulk and ISO transfers.
121 */
122 if (!config->fullspeed && function->descriptors)
123 config->fullspeed = true;
124 if (!config->highspeed && function->hs_descriptors)
125 config->highspeed = true;
126
127done:
128 if (value)
Benoit Gobyaab96812011-04-19 20:37:33 -0700129 DBG(config->cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700130 function->name, function, value);
131 return value;
132}
133
134/**
David Brownell60beed92008-08-18 17:38:22 -0700135 * usb_function_deactivate - prevent function and gadget enumeration
136 * @function: the function that isn't yet ready to respond
137 *
138 * Blocks response of the gadget driver to host enumeration by
139 * preventing the data line pullup from being activated. This is
140 * normally called during @bind() processing to change from the
141 * initial "ready to respond" state, or when a required resource
142 * becomes available.
143 *
144 * For example, drivers that serve as a passthrough to a userspace
145 * daemon can block enumeration unless that daemon (such as an OBEX,
146 * MTP, or print server) is ready to handle host requests.
147 *
148 * Not all systems support software control of their USB peripheral
149 * data pullups.
150 *
151 * Returns zero on success, else negative errno.
152 */
153int usb_function_deactivate(struct usb_function *function)
154{
155 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200156 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700157 int status = 0;
158
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200159 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700160
161 if (cdev->deactivations == 0)
162 status = usb_gadget_disconnect(cdev->gadget);
163 if (status == 0)
164 cdev->deactivations++;
165
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200166 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700167 return status;
168}
169
170/**
171 * usb_function_activate - allow function and gadget enumeration
172 * @function: function on which usb_function_activate() was called
173 *
174 * Reverses effect of usb_function_deactivate(). If no more functions
175 * are delaying their activation, the gadget driver will respond to
176 * host enumeration procedures.
177 *
178 * Returns zero on success, else negative errno.
179 */
180int usb_function_activate(struct usb_function *function)
181{
182 struct usb_composite_dev *cdev = function->config->cdev;
183 int status = 0;
184
185 spin_lock(&cdev->lock);
186
187 if (WARN_ON(cdev->deactivations == 0))
188 status = -EINVAL;
189 else {
190 cdev->deactivations--;
191 if (cdev->deactivations == 0)
192 status = usb_gadget_connect(cdev->gadget);
193 }
194
195 spin_unlock(&cdev->lock);
196 return status;
197}
198
199/**
David Brownell40982be2008-06-19 17:52:58 -0700200 * usb_interface_id() - allocate an unused interface ID
201 * @config: configuration associated with the interface
202 * @function: function handling the interface
203 * Context: single threaded during gadget setup
204 *
205 * usb_interface_id() is called from usb_function.bind() callbacks to
206 * allocate new interface IDs. The function driver will then store that
207 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300208 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700209 * particularly changing its altsetting via set_alt(). There may
210 * also be class-specific or vendor-specific requests to handle.
211 *
212 * All interface identifier should be allocated using this routine, to
213 * ensure that for example different functions don't wrongly assign
214 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300215 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700216 * one configuration (or more than once in a given configuration) need
217 * multiple versions of the relevant descriptors.
218 *
219 * Returns the interface ID which was allocated; or -ENODEV if no
220 * more interface IDs can be allocated.
221 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200222int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700223 struct usb_function *function)
224{
225 unsigned id = config->next_interface_id;
226
227 if (id < MAX_CONFIG_INTERFACES) {
228 config->interface[id] = function;
229 config->next_interface_id = id + 1;
230 return id;
231 }
232 return -ENODEV;
233}
234
235static int config_buf(struct usb_configuration *config,
236 enum usb_device_speed speed, void *buf, u8 type)
237{
238 struct usb_config_descriptor *c = buf;
239 void *next = buf + USB_DT_CONFIG_SIZE;
240 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
241 struct usb_function *f;
242 int status;
243
244 /* write the config descriptor */
245 c = buf;
246 c->bLength = USB_DT_CONFIG_SIZE;
247 c->bDescriptorType = type;
Benoit Gobyaab96812011-04-19 20:37:33 -0700248 /* wTotalLength is written later */
249 c->bNumInterfaces = config->next_interface_id;
David Brownell40982be2008-06-19 17:52:58 -0700250 c->bConfigurationValue = config->bConfigurationValue;
251 c->iConfiguration = config->iConfiguration;
252 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700253 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700254
255 /* There may be e.g. OTG descriptors */
256 if (config->descriptors) {
257 status = usb_descriptor_fillbuf(next, len,
258 config->descriptors);
259 if (status < 0)
260 return status;
261 len -= status;
262 next += status;
263 }
264
265 /* add each function's descriptors */
266 list_for_each_entry(f, &config->functions, list) {
267 struct usb_descriptor_header **descriptors;
268
269 if (speed == USB_SPEED_HIGH)
270 descriptors = f->hs_descriptors;
271 else
272 descriptors = f->descriptors;
Benoit Gobyaab96812011-04-19 20:37:33 -0700273 if (!descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700274 continue;
275 status = usb_descriptor_fillbuf(next, len,
276 (const struct usb_descriptor_header **) descriptors);
277 if (status < 0)
278 return status;
279 len -= status;
280 next += status;
281 }
282
283 len = next - buf;
284 c->wTotalLength = cpu_to_le16(len);
285 return len;
286}
287
288static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
289{
290 struct usb_gadget *gadget = cdev->gadget;
291 struct usb_configuration *c;
292 u8 type = w_value >> 8;
293 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
294
295 if (gadget_is_dualspeed(gadget)) {
296 int hs = 0;
297
298 if (gadget->speed == USB_SPEED_HIGH)
299 hs = 1;
300 if (type == USB_DT_OTHER_SPEED_CONFIG)
301 hs = !hs;
302 if (hs)
303 speed = USB_SPEED_HIGH;
304
305 }
306
307 /* This is a lookup by config *INDEX* */
308 w_value &= 0xff;
309 list_for_each_entry(c, &cdev->configs, list) {
310 /* ignore configs that won't work at this speed */
311 if (speed == USB_SPEED_HIGH) {
312 if (!c->highspeed)
313 continue;
314 } else {
315 if (!c->fullspeed)
316 continue;
317 }
318 if (w_value == 0)
319 return config_buf(c, speed, cdev->req->buf, type);
320 w_value--;
321 }
322 return -EINVAL;
323}
324
325static int count_configs(struct usb_composite_dev *cdev, unsigned type)
326{
327 struct usb_gadget *gadget = cdev->gadget;
328 struct usb_configuration *c;
329 unsigned count = 0;
330 int hs = 0;
331
332 if (gadget_is_dualspeed(gadget)) {
333 if (gadget->speed == USB_SPEED_HIGH)
334 hs = 1;
335 if (type == USB_DT_DEVICE_QUALIFIER)
336 hs = !hs;
337 }
338 list_for_each_entry(c, &cdev->configs, list) {
339 /* ignore configs that won't work at this speed */
340 if (hs) {
341 if (!c->highspeed)
342 continue;
343 } else {
344 if (!c->fullspeed)
345 continue;
346 }
347 count++;
348 }
349 return count;
350}
351
352static void device_qual(struct usb_composite_dev *cdev)
353{
354 struct usb_qualifier_descriptor *qual = cdev->req->buf;
355
356 qual->bLength = sizeof(*qual);
357 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
358 /* POLICY: same bcdUSB and device type info at both speeds */
359 qual->bcdUSB = cdev->desc.bcdUSB;
360 qual->bDeviceClass = cdev->desc.bDeviceClass;
361 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
362 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
363 /* ASSUME same EP0 fifo size at both speeds */
364 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
365 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700366 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700367}
368
369/*-------------------------------------------------------------------------*/
370
371static void reset_config(struct usb_composite_dev *cdev)
372{
373 struct usb_function *f;
374
375 DBG(cdev, "reset config\n");
376
377 list_for_each_entry(f, &cdev->config->functions, list) {
378 if (f->disable)
379 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200380
381 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700382 }
383 cdev->config = NULL;
384}
385
386static int set_config(struct usb_composite_dev *cdev,
387 const struct usb_ctrlrequest *ctrl, unsigned number)
388{
389 struct usb_gadget *gadget = cdev->gadget;
390 struct usb_configuration *c = NULL;
391 int result = -EINVAL;
392 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
393 int tmp;
394
395 if (cdev->config)
396 reset_config(cdev);
397
398 if (number) {
399 list_for_each_entry(c, &cdev->configs, list) {
400 if (c->bConfigurationValue == number) {
401 result = 0;
402 break;
403 }
404 }
405 if (result < 0)
406 goto done;
407 } else
408 result = 0;
409
410 INFO(cdev, "%s speed config #%d: %s\n",
411 ({ char *speed;
412 switch (gadget->speed) {
413 case USB_SPEED_LOW: speed = "low"; break;
414 case USB_SPEED_FULL: speed = "full"; break;
415 case USB_SPEED_HIGH: speed = "high"; break;
416 default: speed = "?"; break;
417 } ; speed; }), number, c ? c->label : "unconfigured");
418
419 if (!c)
420 goto done;
421
422 cdev->config = c;
423
424 /* Initialize all interfaces by setting them to altsetting zero. */
425 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
426 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200427 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700428
429 if (!f)
430 break;
431
Laurent Pinchart52426582009-10-21 00:03:38 +0200432 /*
433 * Record which endpoints are used by the function. This is used
434 * to dispatch control requests targeted at that endpoint to the
435 * function's setup callback instead of the current
436 * configuration's setup callback.
437 */
438 if (gadget->speed == USB_SPEED_HIGH)
439 descriptors = f->hs_descriptors;
440 else
441 descriptors = f->descriptors;
442
443 for (; *descriptors; ++descriptors) {
444 struct usb_endpoint_descriptor *ep;
445 int addr;
446
447 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
448 continue;
449
450 ep = (struct usb_endpoint_descriptor *)*descriptors;
451 addr = ((ep->bEndpointAddress & 0x80) >> 3)
452 | (ep->bEndpointAddress & 0x0f);
453 set_bit(addr, f->endpoints);
454 }
455
David Brownell40982be2008-06-19 17:52:58 -0700456 result = f->set_alt(f, tmp, 0);
457 if (result < 0) {
458 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
459 tmp, f->name, f, result);
460
461 reset_config(cdev);
462 goto done;
463 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300464
465 if (result == USB_GADGET_DELAYED_STATUS) {
466 DBG(cdev,
467 "%s: interface %d (%s) requested delayed status\n",
468 __func__, tmp, f->name);
469 cdev->delayed_status++;
470 DBG(cdev, "delayed_status count %d\n",
471 cdev->delayed_status);
472 }
David Brownell40982be2008-06-19 17:52:58 -0700473 }
474
475 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700476 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700477done:
478 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400479
Roger Quadros1b9ba002011-05-09 13:08:06 +0300480 if (result >= 0 && cdev->delayed_status)
481 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700482 return result;
483}
484
485/**
486 * usb_add_config() - add a configuration to a device.
487 * @cdev: wraps the USB gadget
488 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200489 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700490 * Context: single threaded during gadget setup
491 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200492 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700493 * add each of the configurations it supports, using this routine.
494 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200495 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700496 * is zero for success else a negative errno value. Binding configurations
497 * assigns global resources including string IDs, and per-configuration
498 * resources such as interface IDs and endpoints.
499 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200500int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200501 struct usb_configuration *config,
502 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700503{
504 int status = -EINVAL;
505 struct usb_configuration *c;
506
507 DBG(cdev, "adding config #%u '%s'/%p\n",
508 config->bConfigurationValue,
509 config->label, config);
510
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200511 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700512 goto done;
513
514 /* Prevent duplicate configuration identifiers */
515 list_for_each_entry(c, &cdev->configs, list) {
516 if (c->bConfigurationValue == config->bConfigurationValue) {
517 status = -EBUSY;
518 goto done;
519 }
520 }
521
522 config->cdev = cdev;
523 list_add_tail(&config->list, &cdev->configs);
524
525 INIT_LIST_HEAD(&config->functions);
526 config->next_interface_id = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700527 memset(config->interface, '\0', sizeof(config->interface));
David Brownell40982be2008-06-19 17:52:58 -0700528
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200529 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700530 if (status < 0) {
531 list_del(&config->list);
532 config->cdev = NULL;
533 } else {
534 unsigned i;
535
536 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
537 config->bConfigurationValue, config,
538 config->highspeed ? " high" : "",
539 config->fullspeed
540 ? (gadget_is_dualspeed(cdev->gadget)
541 ? " full"
542 : " full/low")
543 : "");
544
545 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
546 struct usb_function *f = config->interface[i];
547
548 if (!f)
549 continue;
550 DBG(cdev, " interface %d = %s/%p\n",
551 i, f->name, f);
552 }
553 }
554
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200555 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700556 * ep->driver_data as needed.
557 */
558 usb_ep_autoconfig_reset(cdev->gadget);
559
560done:
561 if (status)
562 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
563 config->bConfigurationValue, status);
564 return status;
565}
566
Benoit Goby94df1bd2011-05-25 23:59:43 -0700567static int remove_config(struct usb_composite_dev *cdev,
568 struct usb_configuration *config)
569{
570 while (!list_empty(&config->functions)) {
571 struct usb_function *f;
572
573 f = list_first_entry(&config->functions,
574 struct usb_function, list);
575 list_del(&f->list);
576 if (f->unbind) {
577 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
578 f->unbind(config, f);
579 /* may free memory for "f" */
580 }
581 }
582 list_del(&config->list);
583 if (config->unbind) {
584 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
585 config->unbind(config);
586 /* may free memory for "c" */
587 }
588 return 0;
589}
590
591int usb_remove_config(struct usb_composite_dev *cdev,
592 struct usb_configuration *config)
593{
594 unsigned long flags;
595
596 spin_lock_irqsave(&cdev->lock, flags);
597
598 if (cdev->config == config)
599 reset_config(cdev);
600
601 spin_unlock_irqrestore(&cdev->lock, flags);
602
603 return remove_config(cdev, config);
604}
605
David Brownell40982be2008-06-19 17:52:58 -0700606/*-------------------------------------------------------------------------*/
607
608/* We support strings in multiple languages ... string descriptor zero
609 * says which languages are supported. The typical case will be that
610 * only one language (probably English) is used, with I18N handled on
611 * the host side.
612 */
613
614static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
615{
616 const struct usb_gadget_strings *s;
617 u16 language;
618 __le16 *tmp;
619
620 while (*sp) {
621 s = *sp;
622 language = cpu_to_le16(s->language);
623 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
624 if (*tmp == language)
625 goto repeat;
626 }
627 *tmp++ = language;
628repeat:
629 sp++;
630 }
631}
632
633static int lookup_string(
634 struct usb_gadget_strings **sp,
635 void *buf,
636 u16 language,
637 int id
638)
639{
640 struct usb_gadget_strings *s;
641 int value;
642
643 while (*sp) {
644 s = *sp++;
645 if (s->language != language)
646 continue;
647 value = usb_gadget_get_string(s, id, buf);
648 if (value > 0)
649 return value;
650 }
651 return -EINVAL;
652}
653
654static int get_string(struct usb_composite_dev *cdev,
655 void *buf, u16 language, int id)
656{
657 struct usb_configuration *c;
658 struct usb_function *f;
659 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200660 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700661
662 /* Yes, not only is USB's I18N support probably more than most
663 * folk will ever care about ... also, it's all supported here.
664 * (Except for UTF8 support for Unicode's "Astral Planes".)
665 */
666
667 /* 0 == report all available language codes */
668 if (id == 0) {
669 struct usb_string_descriptor *s = buf;
670 struct usb_gadget_strings **sp;
671
672 memset(s, 0, 256);
673 s->bDescriptorType = USB_DT_STRING;
674
675 sp = composite->strings;
676 if (sp)
677 collect_langs(sp, s->wData);
678
679 list_for_each_entry(c, &cdev->configs, list) {
680 sp = c->strings;
681 if (sp)
682 collect_langs(sp, s->wData);
683
684 list_for_each_entry(f, &c->functions, list) {
685 sp = f->strings;
686 if (sp)
687 collect_langs(sp, s->wData);
688 }
689 }
690
Roel Kluin417b57b2009-08-06 16:09:51 -0700691 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700692 continue;
693 if (!len)
694 return -EINVAL;
695
696 s->bLength = 2 * (len + 1);
697 return s->bLength;
698 }
699
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200700 /* Otherwise, look up and return a specified string. First
701 * check if the string has not been overridden.
702 */
703 if (cdev->manufacturer_override == id)
704 str = iManufacturer ?: composite->iManufacturer ?:
705 composite_manufacturer;
706 else if (cdev->product_override == id)
707 str = iProduct ?: composite->iProduct;
708 else if (cdev->serial_override == id)
709 str = iSerialNumber;
710 else
711 str = NULL;
712 if (str) {
713 struct usb_gadget_strings strings = {
714 .language = language,
715 .strings = &(struct usb_string) { 0xff, str }
716 };
717 return usb_gadget_get_string(&strings, 0xff, buf);
718 }
719
720 /* String IDs are device-scoped, so we look up each string
721 * table we're told about. These lookups are infrequent;
722 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700723 */
724 if (composite->strings) {
725 len = lookup_string(composite->strings, buf, language, id);
726 if (len > 0)
727 return len;
728 }
729 list_for_each_entry(c, &cdev->configs, list) {
730 if (c->strings) {
731 len = lookup_string(c->strings, buf, language, id);
732 if (len > 0)
733 return len;
734 }
735 list_for_each_entry(f, &c->functions, list) {
736 if (!f->strings)
737 continue;
738 len = lookup_string(f->strings, buf, language, id);
739 if (len > 0)
740 return len;
741 }
742 }
743 return -EINVAL;
744}
745
746/**
747 * usb_string_id() - allocate an unused string ID
748 * @cdev: the device whose string descriptor IDs are being allocated
749 * Context: single threaded during gadget setup
750 *
751 * @usb_string_id() is called from bind() callbacks to allocate
752 * string IDs. Drivers for functions, configurations, or gadgets will
753 * then store that ID in the appropriate descriptors and string table.
754 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200755 * All string identifier should be allocated using this,
756 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
757 * that for example different functions don't wrongly assign different
758 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700759 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200760int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700761{
762 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200763 /* string id 0 is reserved by USB spec for list of
764 * supported languages */
765 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700766 cdev->next_string_id++;
767 return cdev->next_string_id;
768 }
769 return -ENODEV;
770}
771
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200772/**
773 * usb_string_ids() - allocate unused string IDs in batch
774 * @cdev: the device whose string descriptor IDs are being allocated
775 * @str: an array of usb_string objects to assign numbers to
776 * Context: single threaded during gadget setup
777 *
778 * @usb_string_ids() is called from bind() callbacks to allocate
779 * string IDs. Drivers for functions, configurations, or gadgets will
780 * then copy IDs from the string table to the appropriate descriptors
781 * and string table for other languages.
782 *
783 * All string identifier should be allocated using this,
784 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
785 * example different functions don't wrongly assign different meanings
786 * to the same identifier.
787 */
788int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
789{
790 int next = cdev->next_string_id;
791
792 for (; str->s; ++str) {
793 if (unlikely(next >= 254))
794 return -ENODEV;
795 str->id = ++next;
796 }
797
798 cdev->next_string_id = next;
799
800 return 0;
801}
802
803/**
804 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700805 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200806 * @n: number of string IDs to allocate
807 * Context: single threaded during gadget setup
808 *
809 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700810 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200811 * is, returns last requested ID which is now very useful information.
812 *
813 * @usb_string_ids_n() is called from bind() callbacks to allocate
814 * string IDs. Drivers for functions, configurations, or gadgets will
815 * then store that ID in the appropriate descriptors and string table.
816 *
817 * All string identifier should be allocated using this,
818 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
819 * example different functions don't wrongly assign different meanings
820 * to the same identifier.
821 */
822int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
823{
824 unsigned next = c->next_string_id;
825 if (unlikely(n > 254 || (unsigned)next + n > 254))
826 return -ENODEV;
827 c->next_string_id += n;
828 return next + 1;
829}
830
831
David Brownell40982be2008-06-19 17:52:58 -0700832/*-------------------------------------------------------------------------*/
833
834static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
835{
836 if (req->status || req->actual != req->length)
837 DBG((struct usb_composite_dev *) ep->driver_data,
838 "setup complete --> %d, %d/%d\n",
839 req->status, req->actual, req->length);
840}
841
842/*
843 * The setup() callback implements all the ep0 functionality that's
844 * not handled lower down, in hardware or the hardware driver(like
845 * device and endpoint feature flags, and their status). It's all
846 * housekeeping for the gadget function we're implementing. Most of
847 * the work is in config and function specific setup.
848 */
849static int
850composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
851{
852 struct usb_composite_dev *cdev = get_gadget_data(gadget);
853 struct usb_request *req = cdev->req;
854 int value = -EOPNOTSUPP;
855 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800856 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700857 u16 w_value = le16_to_cpu(ctrl->wValue);
858 u16 w_length = le16_to_cpu(ctrl->wLength);
859 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200860 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700861
862 /* partial re-init of the response message; the function or the
863 * gadget might need to intercept e.g. a control-OUT completion
864 * when we delegate to it.
865 */
866 req->zero = 0;
867 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530868 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700869 gadget->ep0->driver_data = cdev;
870
871 switch (ctrl->bRequest) {
872
873 /* we handle all standard USB descriptors */
874 case USB_REQ_GET_DESCRIPTOR:
875 if (ctrl->bRequestType != USB_DIR_IN)
876 goto unknown;
877 switch (w_value >> 8) {
878
879 case USB_DT_DEVICE:
880 cdev->desc.bNumConfigurations =
881 count_configs(cdev, USB_DT_DEVICE);
882 value = min(w_length, (u16) sizeof cdev->desc);
883 memcpy(req->buf, &cdev->desc, value);
884 break;
885 case USB_DT_DEVICE_QUALIFIER:
886 if (!gadget_is_dualspeed(gadget))
887 break;
888 device_qual(cdev);
889 value = min_t(int, w_length,
890 sizeof(struct usb_qualifier_descriptor));
891 break;
892 case USB_DT_OTHER_SPEED_CONFIG:
893 if (!gadget_is_dualspeed(gadget))
894 break;
895 /* FALLTHROUGH */
896 case USB_DT_CONFIG:
897 value = config_desc(cdev, w_value);
898 if (value >= 0)
899 value = min(w_length, (u16) value);
900 break;
901 case USB_DT_STRING:
902 value = get_string(cdev, req->buf,
903 w_index, w_value & 0xff);
904 if (value >= 0)
905 value = min(w_length, (u16) value);
906 break;
907 }
908 break;
909
910 /* any number of configs can work */
911 case USB_REQ_SET_CONFIGURATION:
912 if (ctrl->bRequestType != 0)
913 goto unknown;
914 if (gadget_is_otg(gadget)) {
915 if (gadget->a_hnp_support)
916 DBG(cdev, "HNP available\n");
917 else if (gadget->a_alt_hnp_support)
918 DBG(cdev, "HNP on another port\n");
919 else
920 VDBG(cdev, "HNP inactive\n");
921 }
922 spin_lock(&cdev->lock);
923 value = set_config(cdev, ctrl, w_value);
924 spin_unlock(&cdev->lock);
925 break;
926 case USB_REQ_GET_CONFIGURATION:
927 if (ctrl->bRequestType != USB_DIR_IN)
928 goto unknown;
Oleg Matcovschie67dd162011-03-11 10:24:30 -0800929 if (cdev->config)
David Brownell40982be2008-06-19 17:52:58 -0700930 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Oleg Matcovschie67dd162011-03-11 10:24:30 -0800931 else
David Brownell40982be2008-06-19 17:52:58 -0700932 *(u8 *)req->buf = 0;
Oleg Matcovschie67dd162011-03-11 10:24:30 -0800933 value = min(w_length, (u16) 1);
David Brownell40982be2008-06-19 17:52:58 -0700934 break;
935
936 /* function drivers must handle get/set altsetting; if there's
937 * no get() method, we know only altsetting zero works.
938 */
939 case USB_REQ_SET_INTERFACE:
940 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
941 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900942 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700943 break;
Bryan Wu08889512009-01-08 00:21:19 +0800944 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700945 if (!f)
946 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800947 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700948 break;
949 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300950 if (value == USB_GADGET_DELAYED_STATUS) {
951 DBG(cdev,
952 "%s: interface %d (%s) requested delayed status\n",
953 __func__, intf, f->name);
954 cdev->delayed_status++;
955 DBG(cdev, "delayed_status count %d\n",
956 cdev->delayed_status);
957 }
David Brownell40982be2008-06-19 17:52:58 -0700958 break;
959 case USB_REQ_GET_INTERFACE:
960 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
961 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900962 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700963 break;
Bryan Wu08889512009-01-08 00:21:19 +0800964 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700965 if (!f)
966 break;
967 /* lots of interfaces only need altsetting zero... */
968 value = f->get_alt ? f->get_alt(f, w_index) : 0;
969 if (value < 0)
970 break;
971 *((u8 *)req->buf) = value;
972 value = min(w_length, (u16) 1);
973 break;
974 default:
975unknown:
976 VDBG(cdev,
977 "non-core control req%02x.%02x v%04x i%04x l%d\n",
978 ctrl->bRequestType, ctrl->bRequest,
979 w_value, w_index, w_length);
980
Laurent Pinchart52426582009-10-21 00:03:38 +0200981 /* functions always handle their interfaces and endpoints...
982 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -0700983 * configuration code.
984 *
985 * REVISIT it could make sense to let the composite device
986 * take such requests too, if that's ever needed: to work
987 * in config 0, etc.
988 */
Laurent Pinchart52426582009-10-21 00:03:38 +0200989 switch (ctrl->bRequestType & USB_RECIP_MASK) {
990 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +0900991 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +0530992 break;
993 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +0200994 break;
995
996 case USB_RECIP_ENDPOINT:
997 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
998 list_for_each_entry(f, &cdev->config->functions, list) {
999 if (test_bit(endp, f->endpoints))
1000 break;
1001 }
1002 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001003 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001004 break;
David Brownell40982be2008-06-19 17:52:58 -07001005 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001006
1007 if (f && f->setup)
1008 value = f->setup(f, ctrl);
1009 else {
David Brownell40982be2008-06-19 17:52:58 -07001010 struct usb_configuration *c;
1011
1012 c = cdev->config;
1013 if (c && c->setup)
1014 value = c->setup(c, ctrl);
1015 }
1016
1017 goto done;
1018 }
1019
1020 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001021 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001022 req->length = value;
1023 req->zero = value < w_length;
1024 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1025 if (value < 0) {
1026 DBG(cdev, "ep_queue --> %d\n", value);
1027 req->status = 0;
1028 composite_setup_complete(gadget->ep0, req);
1029 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001030 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1031 WARN(cdev,
1032 "%s: Delayed status not supported for w_length != 0",
1033 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001034 }
1035
1036done:
1037 /* device either stalls (value < 0) or reports success */
1038 return value;
1039}
1040
1041static void composite_disconnect(struct usb_gadget *gadget)
1042{
1043 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1044 unsigned long flags;
1045
1046 /* REVISIT: should we have config and device level
1047 * disconnect callbacks?
1048 */
1049 spin_lock_irqsave(&cdev->lock, flags);
1050 if (cdev->config)
1051 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001052 if (composite->disconnect)
1053 composite->disconnect(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001054 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001055}
1056
1057/*-------------------------------------------------------------------------*/
1058
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001059static ssize_t composite_show_suspended(struct device *dev,
1060 struct device_attribute *attr,
1061 char *buf)
1062{
1063 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1064 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1065
1066 return sprintf(buf, "%d\n", cdev->suspended);
1067}
1068
1069static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1070
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001071static void
David Brownell40982be2008-06-19 17:52:58 -07001072composite_unbind(struct usb_gadget *gadget)
1073{
1074 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1075
1076 /* composite_disconnect() must already have been called
1077 * by the underlying peripheral controller driver!
1078 * so there's no i/o concurrency that could affect the
1079 * state protected by cdev->lock.
1080 */
1081 WARN_ON(cdev->config);
1082
1083 while (!list_empty(&cdev->configs)) {
1084 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001085 c = list_first_entry(&cdev->configs,
1086 struct usb_configuration, list);
Benoit Goby94df1bd2011-05-25 23:59:43 -07001087 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001088 }
1089 if (composite->unbind)
1090 composite->unbind(cdev);
1091
1092 if (cdev->req) {
1093 kfree(cdev->req->buf);
1094 usb_ep_free_request(gadget->ep0, cdev->req);
1095 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301096 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001097 kfree(cdev);
1098 set_gadget_data(gadget, NULL);
1099 composite = NULL;
1100}
1101
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001102static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001103{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001104 if (!*desc) {
1105 int ret = usb_string_id(cdev);
1106 if (unlikely(ret < 0))
1107 WARNING(cdev, "failed to override string ID\n");
1108 else
1109 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001110 }
David Brownell40982be2008-06-19 17:52:58 -07001111
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001112 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001113}
1114
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001115static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001116{
1117 struct usb_composite_dev *cdev;
1118 int status = -ENOMEM;
1119
1120 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1121 if (!cdev)
1122 return status;
1123
1124 spin_lock_init(&cdev->lock);
1125 cdev->gadget = gadget;
1126 set_gadget_data(gadget, cdev);
1127 INIT_LIST_HEAD(&cdev->configs);
1128
1129 /* preallocate control response and buffer */
1130 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1131 if (!cdev->req)
1132 goto fail;
1133 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1134 if (!cdev->req->buf)
1135 goto fail;
1136 cdev->req->complete = composite_setup_complete;
1137 gadget->ep0->driver_data = cdev;
1138
1139 cdev->bufsiz = USB_BUFSIZ;
1140 cdev->driver = composite;
1141
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301142 /*
1143 * As per USB compliance update, a device that is actively drawing
1144 * more than 100mA from USB must report itself as bus-powered in
1145 * the GetStatus(DEVICE) call.
1146 */
1147 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1148 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001149
1150 /* interface and string IDs start at zero via kzalloc.
1151 * we force endpoints to start unassigned; few controller
1152 * drivers will zero ep->driver_data.
1153 */
1154 usb_ep_autoconfig_reset(cdev->gadget);
1155
1156 /* composite gadget needs to assign strings for whole device (like
1157 * serial number), register function drivers, potentially update
1158 * power state and consumption, etc
1159 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001160 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001161 if (status < 0)
1162 goto fail;
1163
1164 cdev->desc = *composite->dev;
1165 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1166
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001167 /* standardized runtime overrides for device ID data */
1168 if (idVendor)
1169 cdev->desc.idVendor = cpu_to_le16(idVendor);
1170 if (idProduct)
1171 cdev->desc.idProduct = cpu_to_le16(idProduct);
1172 if (bcdDevice)
1173 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1174
Marek Belisko78bff3c2010-10-27 10:19:01 +02001175 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001176 if (iManufacturer || !cdev->desc.iManufacturer) {
1177 if (!iManufacturer && !composite->iManufacturer &&
1178 !*composite_manufacturer)
1179 snprintf(composite_manufacturer,
1180 sizeof composite_manufacturer,
1181 "%s %s with %s",
1182 init_utsname()->sysname,
1183 init_utsname()->release,
1184 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001185
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001186 cdev->manufacturer_override =
1187 override_id(cdev, &cdev->desc.iManufacturer);
1188 }
1189
1190 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1191 cdev->product_override =
1192 override_id(cdev, &cdev->desc.iProduct);
1193
1194 if (iSerialNumber)
1195 cdev->serial_override =
1196 override_id(cdev, &cdev->desc.iSerialNumber);
1197
1198 /* has userspace failed to provide a serial number? */
1199 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1200 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1201
1202 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001203 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1204 if (status)
1205 goto fail;
1206
David Brownell40982be2008-06-19 17:52:58 -07001207 INFO(cdev, "%s ready\n", composite->name);
1208 return 0;
1209
1210fail:
1211 composite_unbind(gadget);
1212 return status;
1213}
1214
1215/*-------------------------------------------------------------------------*/
1216
1217static void
1218composite_suspend(struct usb_gadget *gadget)
1219{
1220 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1221 struct usb_function *f;
1222
David Brownell89429392009-03-19 14:14:17 -07001223 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001224 * suspend/resume callbacks?
1225 */
1226 DBG(cdev, "suspend\n");
1227 if (cdev->config) {
1228 list_for_each_entry(f, &cdev->config->functions, list) {
1229 if (f->suspend)
1230 f->suspend(f);
1231 }
1232 }
David Brownell89429392009-03-19 14:14:17 -07001233 if (composite->suspend)
1234 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001235
1236 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001237
1238 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001239}
1240
1241static void
1242composite_resume(struct usb_gadget *gadget)
1243{
1244 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1245 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001246 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001247
David Brownell89429392009-03-19 14:14:17 -07001248 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001249 * suspend/resume callbacks?
1250 */
1251 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001252 if (composite->resume)
1253 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001254 if (cdev->config) {
1255 list_for_each_entry(f, &cdev->config->functions, list) {
1256 if (f->resume)
1257 f->resume(f);
1258 }
Hao Wub23f2f92010-11-29 15:17:03 +08001259
1260 maxpower = cdev->config->bMaxPower;
1261
1262 usb_gadget_vbus_draw(gadget, maxpower ?
1263 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001264 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001265
1266 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001267}
1268
1269/*-------------------------------------------------------------------------*/
1270
1271static struct usb_gadget_driver composite_driver = {
1272 .speed = USB_SPEED_HIGH,
1273
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001274 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001275
1276 .setup = composite_setup,
1277 .disconnect = composite_disconnect,
1278
1279 .suspend = composite_suspend,
1280 .resume = composite_resume,
1281
1282 .driver = {
1283 .owner = THIS_MODULE,
1284 },
1285};
1286
1287/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001288 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001289 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001290 * @bind: the callback used to allocate resources that are shared across the
1291 * whole device, such as string IDs, and add its configurations using
1292 * @usb_add_config(). This may fail by returning a negative errno
1293 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001294 * Context: single threaded during gadget setup
1295 *
1296 * This function is used to register drivers using the composite driver
1297 * framework. The return value is zero, or a negative errno value.
1298 * Those values normally come from the driver's @bind method, which does
1299 * all the work of setting up the driver to match the hardware.
1300 *
1301 * On successful return, the gadget is ready to respond to requests from
1302 * the host, unless one of its components invokes usb_gadget_disconnect()
1303 * while it was binding. That would usually be done in order to wait for
1304 * some userspace participation.
1305 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001306int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001307 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001308{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001309 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001310 return -EINVAL;
1311
1312 if (!driver->name)
1313 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001314 if (!driver->iProduct)
1315 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001316 composite_driver.function = (char *) driver->name;
1317 composite_driver.driver.name = driver->name;
1318 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001319 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001320
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001321 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001322}
1323
1324/**
1325 * usb_composite_unregister() - unregister a composite driver
1326 * @driver: the driver to unregister
1327 *
1328 * This function is used to unregister drivers using the composite
1329 * driver framework.
1330 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001331void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001332{
1333 if (composite != driver)
1334 return;
1335 usb_gadget_unregister_driver(&composite_driver);
1336}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001337
1338/**
1339 * usb_composite_setup_continue() - Continue with the control transfer
1340 * @cdev: the composite device who's control transfer was kept waiting
1341 *
1342 * This function must be called by the USB function driver to continue
1343 * with the control transfer's data/status stage in case it had requested to
1344 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1345 * can request the composite framework to delay the setup request's data/status
1346 * stages by returning USB_GADGET_DELAYED_STATUS.
1347 */
1348void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1349{
1350 int value;
1351 struct usb_request *req = cdev->req;
1352 unsigned long flags;
1353
1354 DBG(cdev, "%s\n", __func__);
1355 spin_lock_irqsave(&cdev->lock, flags);
1356
1357 if (cdev->delayed_status == 0) {
1358 WARN(cdev, "%s: Unexpected call\n", __func__);
1359
1360 } else if (--cdev->delayed_status == 0) {
1361 DBG(cdev, "%s: Completing delayed status\n", __func__);
1362 req->length = 0;
1363 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1364 if (value < 0) {
1365 DBG(cdev, "ep_queue --> %d\n", value);
1366 req->status = 0;
1367 composite_setup_complete(cdev->gadget->ep0, req);
1368 }
1369 }
1370
1371 spin_unlock_irqrestore(&cdev->lock, flags);
1372}
1373