blob: 1c6bd666150a30e78e44e793a1845271e8322229 [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/*-------------------------------------------------------------------------*/
Tatyana Brokhman48767a42011-06-28 16:33:49 +030077/**
78 * next_ep_desc() - advance to the next EP descriptor
79 * @t: currect pointer within descriptor array
80 *
81 * Return: next EP descriptor or NULL
82 *
83 * Iterate over @t until either EP descriptor found or
84 * NULL (that indicates end of list) encountered
85 */
86static struct usb_descriptor_header**
87next_ep_desc(struct usb_descriptor_header **t)
88{
89 for (; *t; t++) {
90 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
91 return t;
92 }
93 return NULL;
94}
95
96/*
97 * for_each_ep_desc()- iterate over endpoint descriptors in the
98 * descriptors list
99 * @start: pointer within descriptor array.
100 * @ep_desc: endpoint descriptor to use as the loop cursor
101 */
102#define for_each_ep_desc(start, ep_desc) \
103 for (ep_desc = next_ep_desc(start); \
104 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
105
106/**
107 * config_ep_by_speed() - configures the given endpoint
108 * according to gadget speed.
109 * @g: pointer to the gadget
110 * @f: usb function
111 * @_ep: the endpoint to configure
112 *
113 * Return: error code, 0 on success
114 *
115 * This function chooses the right descriptors for a given
116 * endpoint according to gadget speed and saves it in the
117 * endpoint desc field. If the endpoint already has a descriptor
118 * assigned to it - overwrites it with currently corresponding
119 * descriptor. The endpoint maxpacket field is updated according
120 * to the chosen descriptor.
121 * Note: the supplied function should hold all the descriptors
122 * for supported speeds
123 */
124int config_ep_by_speed(struct usb_gadget *g,
125 struct usb_function *f,
126 struct usb_ep *_ep)
127{
128 struct usb_endpoint_descriptor *chosen_desc = NULL;
129 struct usb_descriptor_header **speed_desc = NULL;
130
131 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
132
133 if (!g || !f || !_ep)
134 return -EIO;
135
136 /* select desired speed */
137 switch (g->speed) {
138 case USB_SPEED_HIGH:
139 if (gadget_is_dualspeed(g)) {
140 speed_desc = f->hs_descriptors;
141 break;
142 }
143 /* else: fall through */
144 default:
145 speed_desc = f->descriptors;
146 }
147 /* find descriptors */
148 for_each_ep_desc(speed_desc, d_spd) {
149 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
150 if (chosen_desc->bEndpointAddress == _ep->address)
151 goto ep_found;
152 }
153 return -EIO;
154
155ep_found:
156 /* commit results */
157 _ep->maxpacket = le16_to_cpu(chosen_desc->wMaxPacketSize);
158 _ep->desc = chosen_desc;
159
160 return 0;
161}
David Brownell40982be2008-06-19 17:52:58 -0700162
163/**
164 * usb_add_function() - add a function to a configuration
165 * @config: the configuration
166 * @function: the function being added
167 * Context: single threaded during gadget setup
168 *
169 * After initialization, each configuration must have one or more
170 * functions added to it. Adding a function involves calling its @bind()
171 * method to allocate resources such as interface and string identifiers
172 * and endpoints.
173 *
174 * This function returns the value of the function's bind(), which is
175 * zero for success else a negative errno value.
176 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200177int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700178 struct usb_function *function)
179{
180 int value = -EINVAL;
181
182 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
183 function->name, function,
184 config->label, config);
185
186 if (!function->set_alt || !function->disable)
187 goto done;
188
189 function->config = config;
190 list_add_tail(&function->list, &config->functions);
191
192 /* REVISIT *require* function->bind? */
193 if (function->bind) {
194 value = function->bind(config, function);
195 if (value < 0) {
196 list_del(&function->list);
197 function->config = NULL;
198 }
199 } else
200 value = 0;
201
202 /* We allow configurations that don't work at both speeds.
203 * If we run into a lowspeed Linux system, treat it the same
204 * as full speed ... it's the function drivers that will need
205 * to avoid bulk and ISO transfers.
206 */
207 if (!config->fullspeed && function->descriptors)
208 config->fullspeed = true;
209 if (!config->highspeed && function->hs_descriptors)
210 config->highspeed = true;
211
212done:
213 if (value)
214 DBG(config->cdev, "adding '%s'/%p --> %d\n",
215 function->name, function, value);
216 return value;
217}
218
219/**
David Brownell60beed92008-08-18 17:38:22 -0700220 * usb_function_deactivate - prevent function and gadget enumeration
221 * @function: the function that isn't yet ready to respond
222 *
223 * Blocks response of the gadget driver to host enumeration by
224 * preventing the data line pullup from being activated. This is
225 * normally called during @bind() processing to change from the
226 * initial "ready to respond" state, or when a required resource
227 * becomes available.
228 *
229 * For example, drivers that serve as a passthrough to a userspace
230 * daemon can block enumeration unless that daemon (such as an OBEX,
231 * MTP, or print server) is ready to handle host requests.
232 *
233 * Not all systems support software control of their USB peripheral
234 * data pullups.
235 *
236 * Returns zero on success, else negative errno.
237 */
238int usb_function_deactivate(struct usb_function *function)
239{
240 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200241 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700242 int status = 0;
243
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200244 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700245
246 if (cdev->deactivations == 0)
247 status = usb_gadget_disconnect(cdev->gadget);
248 if (status == 0)
249 cdev->deactivations++;
250
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200251 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700252 return status;
253}
254
255/**
256 * usb_function_activate - allow function and gadget enumeration
257 * @function: function on which usb_function_activate() was called
258 *
259 * Reverses effect of usb_function_deactivate(). If no more functions
260 * are delaying their activation, the gadget driver will respond to
261 * host enumeration procedures.
262 *
263 * Returns zero on success, else negative errno.
264 */
265int usb_function_activate(struct usb_function *function)
266{
267 struct usb_composite_dev *cdev = function->config->cdev;
268 int status = 0;
269
270 spin_lock(&cdev->lock);
271
272 if (WARN_ON(cdev->deactivations == 0))
273 status = -EINVAL;
274 else {
275 cdev->deactivations--;
276 if (cdev->deactivations == 0)
277 status = usb_gadget_connect(cdev->gadget);
278 }
279
280 spin_unlock(&cdev->lock);
281 return status;
282}
283
284/**
David Brownell40982be2008-06-19 17:52:58 -0700285 * usb_interface_id() - allocate an unused interface ID
286 * @config: configuration associated with the interface
287 * @function: function handling the interface
288 * Context: single threaded during gadget setup
289 *
290 * usb_interface_id() is called from usb_function.bind() callbacks to
291 * allocate new interface IDs. The function driver will then store that
292 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300293 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700294 * particularly changing its altsetting via set_alt(). There may
295 * also be class-specific or vendor-specific requests to handle.
296 *
297 * All interface identifier should be allocated using this routine, to
298 * ensure that for example different functions don't wrongly assign
299 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300300 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700301 * one configuration (or more than once in a given configuration) need
302 * multiple versions of the relevant descriptors.
303 *
304 * Returns the interface ID which was allocated; or -ENODEV if no
305 * more interface IDs can be allocated.
306 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200307int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700308 struct usb_function *function)
309{
310 unsigned id = config->next_interface_id;
311
312 if (id < MAX_CONFIG_INTERFACES) {
313 config->interface[id] = function;
314 config->next_interface_id = id + 1;
315 return id;
316 }
317 return -ENODEV;
318}
319
320static int config_buf(struct usb_configuration *config,
321 enum usb_device_speed speed, void *buf, u8 type)
322{
323 struct usb_config_descriptor *c = buf;
324 void *next = buf + USB_DT_CONFIG_SIZE;
325 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
326 struct usb_function *f;
327 int status;
328
329 /* write the config descriptor */
330 c = buf;
331 c->bLength = USB_DT_CONFIG_SIZE;
332 c->bDescriptorType = type;
333 /* wTotalLength is written later */
334 c->bNumInterfaces = config->next_interface_id;
335 c->bConfigurationValue = config->bConfigurationValue;
336 c->iConfiguration = config->iConfiguration;
337 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700338 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700339
340 /* There may be e.g. OTG descriptors */
341 if (config->descriptors) {
342 status = usb_descriptor_fillbuf(next, len,
343 config->descriptors);
344 if (status < 0)
345 return status;
346 len -= status;
347 next += status;
348 }
349
350 /* add each function's descriptors */
351 list_for_each_entry(f, &config->functions, list) {
352 struct usb_descriptor_header **descriptors;
353
354 if (speed == USB_SPEED_HIGH)
355 descriptors = f->hs_descriptors;
356 else
357 descriptors = f->descriptors;
358 if (!descriptors)
359 continue;
360 status = usb_descriptor_fillbuf(next, len,
361 (const struct usb_descriptor_header **) descriptors);
362 if (status < 0)
363 return status;
364 len -= status;
365 next += status;
366 }
367
368 len = next - buf;
369 c->wTotalLength = cpu_to_le16(len);
370 return len;
371}
372
373static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
374{
375 struct usb_gadget *gadget = cdev->gadget;
376 struct usb_configuration *c;
377 u8 type = w_value >> 8;
378 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
379
380 if (gadget_is_dualspeed(gadget)) {
381 int hs = 0;
382
383 if (gadget->speed == USB_SPEED_HIGH)
384 hs = 1;
385 if (type == USB_DT_OTHER_SPEED_CONFIG)
386 hs = !hs;
387 if (hs)
388 speed = USB_SPEED_HIGH;
389
390 }
391
392 /* This is a lookup by config *INDEX* */
393 w_value &= 0xff;
394 list_for_each_entry(c, &cdev->configs, list) {
395 /* ignore configs that won't work at this speed */
396 if (speed == USB_SPEED_HIGH) {
397 if (!c->highspeed)
398 continue;
399 } else {
400 if (!c->fullspeed)
401 continue;
402 }
403 if (w_value == 0)
404 return config_buf(c, speed, cdev->req->buf, type);
405 w_value--;
406 }
407 return -EINVAL;
408}
409
410static int count_configs(struct usb_composite_dev *cdev, unsigned type)
411{
412 struct usb_gadget *gadget = cdev->gadget;
413 struct usb_configuration *c;
414 unsigned count = 0;
415 int hs = 0;
416
417 if (gadget_is_dualspeed(gadget)) {
418 if (gadget->speed == USB_SPEED_HIGH)
419 hs = 1;
420 if (type == USB_DT_DEVICE_QUALIFIER)
421 hs = !hs;
422 }
423 list_for_each_entry(c, &cdev->configs, list) {
424 /* ignore configs that won't work at this speed */
425 if (hs) {
426 if (!c->highspeed)
427 continue;
428 } else {
429 if (!c->fullspeed)
430 continue;
431 }
432 count++;
433 }
434 return count;
435}
436
437static void device_qual(struct usb_composite_dev *cdev)
438{
439 struct usb_qualifier_descriptor *qual = cdev->req->buf;
440
441 qual->bLength = sizeof(*qual);
442 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
443 /* POLICY: same bcdUSB and device type info at both speeds */
444 qual->bcdUSB = cdev->desc.bcdUSB;
445 qual->bDeviceClass = cdev->desc.bDeviceClass;
446 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
447 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
448 /* ASSUME same EP0 fifo size at both speeds */
449 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
450 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700451 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700452}
453
454/*-------------------------------------------------------------------------*/
455
456static void reset_config(struct usb_composite_dev *cdev)
457{
458 struct usb_function *f;
459
460 DBG(cdev, "reset config\n");
461
462 list_for_each_entry(f, &cdev->config->functions, list) {
463 if (f->disable)
464 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200465
466 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700467 }
468 cdev->config = NULL;
469}
470
471static int set_config(struct usb_composite_dev *cdev,
472 const struct usb_ctrlrequest *ctrl, unsigned number)
473{
474 struct usb_gadget *gadget = cdev->gadget;
475 struct usb_configuration *c = NULL;
476 int result = -EINVAL;
477 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
478 int tmp;
479
480 if (cdev->config)
481 reset_config(cdev);
482
483 if (number) {
484 list_for_each_entry(c, &cdev->configs, list) {
485 if (c->bConfigurationValue == number) {
486 result = 0;
487 break;
488 }
489 }
490 if (result < 0)
491 goto done;
492 } else
493 result = 0;
494
495 INFO(cdev, "%s speed config #%d: %s\n",
496 ({ char *speed;
497 switch (gadget->speed) {
498 case USB_SPEED_LOW: speed = "low"; break;
499 case USB_SPEED_FULL: speed = "full"; break;
500 case USB_SPEED_HIGH: speed = "high"; break;
501 default: speed = "?"; break;
502 } ; speed; }), number, c ? c->label : "unconfigured");
503
504 if (!c)
505 goto done;
506
507 cdev->config = c;
508
509 /* Initialize all interfaces by setting them to altsetting zero. */
510 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
511 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200512 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700513
514 if (!f)
515 break;
516
Laurent Pinchart52426582009-10-21 00:03:38 +0200517 /*
518 * Record which endpoints are used by the function. This is used
519 * to dispatch control requests targeted at that endpoint to the
520 * function's setup callback instead of the current
521 * configuration's setup callback.
522 */
523 if (gadget->speed == USB_SPEED_HIGH)
524 descriptors = f->hs_descriptors;
525 else
526 descriptors = f->descriptors;
527
528 for (; *descriptors; ++descriptors) {
529 struct usb_endpoint_descriptor *ep;
530 int addr;
531
532 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
533 continue;
534
535 ep = (struct usb_endpoint_descriptor *)*descriptors;
536 addr = ((ep->bEndpointAddress & 0x80) >> 3)
537 | (ep->bEndpointAddress & 0x0f);
538 set_bit(addr, f->endpoints);
539 }
540
David Brownell40982be2008-06-19 17:52:58 -0700541 result = f->set_alt(f, tmp, 0);
542 if (result < 0) {
543 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
544 tmp, f->name, f, result);
545
546 reset_config(cdev);
547 goto done;
548 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300549
550 if (result == USB_GADGET_DELAYED_STATUS) {
551 DBG(cdev,
552 "%s: interface %d (%s) requested delayed status\n",
553 __func__, tmp, f->name);
554 cdev->delayed_status++;
555 DBG(cdev, "delayed_status count %d\n",
556 cdev->delayed_status);
557 }
David Brownell40982be2008-06-19 17:52:58 -0700558 }
559
560 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700561 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700562done:
563 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300564 if (result >= 0 && cdev->delayed_status)
565 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700566 return result;
567}
568
569/**
570 * usb_add_config() - add a configuration to a device.
571 * @cdev: wraps the USB gadget
572 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200573 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700574 * Context: single threaded during gadget setup
575 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200576 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700577 * add each of the configurations it supports, using this routine.
578 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200579 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700580 * is zero for success else a negative errno value. Binding configurations
581 * assigns global resources including string IDs, and per-configuration
582 * resources such as interface IDs and endpoints.
583 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200584int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200585 struct usb_configuration *config,
586 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700587{
588 int status = -EINVAL;
589 struct usb_configuration *c;
590
591 DBG(cdev, "adding config #%u '%s'/%p\n",
592 config->bConfigurationValue,
593 config->label, config);
594
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200595 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700596 goto done;
597
598 /* Prevent duplicate configuration identifiers */
599 list_for_each_entry(c, &cdev->configs, list) {
600 if (c->bConfigurationValue == config->bConfigurationValue) {
601 status = -EBUSY;
602 goto done;
603 }
604 }
605
606 config->cdev = cdev;
607 list_add_tail(&config->list, &cdev->configs);
608
609 INIT_LIST_HEAD(&config->functions);
610 config->next_interface_id = 0;
611
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200612 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700613 if (status < 0) {
614 list_del(&config->list);
615 config->cdev = NULL;
616 } else {
617 unsigned i;
618
619 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
620 config->bConfigurationValue, config,
621 config->highspeed ? " high" : "",
622 config->fullspeed
623 ? (gadget_is_dualspeed(cdev->gadget)
624 ? " full"
625 : " full/low")
626 : "");
627
628 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
629 struct usb_function *f = config->interface[i];
630
631 if (!f)
632 continue;
633 DBG(cdev, " interface %d = %s/%p\n",
634 i, f->name, f);
635 }
636 }
637
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200638 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700639 * ep->driver_data as needed.
640 */
641 usb_ep_autoconfig_reset(cdev->gadget);
642
643done:
644 if (status)
645 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
646 config->bConfigurationValue, status);
647 return status;
648}
649
650/*-------------------------------------------------------------------------*/
651
652/* We support strings in multiple languages ... string descriptor zero
653 * says which languages are supported. The typical case will be that
654 * only one language (probably English) is used, with I18N handled on
655 * the host side.
656 */
657
658static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
659{
660 const struct usb_gadget_strings *s;
661 u16 language;
662 __le16 *tmp;
663
664 while (*sp) {
665 s = *sp;
666 language = cpu_to_le16(s->language);
667 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
668 if (*tmp == language)
669 goto repeat;
670 }
671 *tmp++ = language;
672repeat:
673 sp++;
674 }
675}
676
677static int lookup_string(
678 struct usb_gadget_strings **sp,
679 void *buf,
680 u16 language,
681 int id
682)
683{
684 struct usb_gadget_strings *s;
685 int value;
686
687 while (*sp) {
688 s = *sp++;
689 if (s->language != language)
690 continue;
691 value = usb_gadget_get_string(s, id, buf);
692 if (value > 0)
693 return value;
694 }
695 return -EINVAL;
696}
697
698static int get_string(struct usb_composite_dev *cdev,
699 void *buf, u16 language, int id)
700{
701 struct usb_configuration *c;
702 struct usb_function *f;
703 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200704 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700705
706 /* Yes, not only is USB's I18N support probably more than most
707 * folk will ever care about ... also, it's all supported here.
708 * (Except for UTF8 support for Unicode's "Astral Planes".)
709 */
710
711 /* 0 == report all available language codes */
712 if (id == 0) {
713 struct usb_string_descriptor *s = buf;
714 struct usb_gadget_strings **sp;
715
716 memset(s, 0, 256);
717 s->bDescriptorType = USB_DT_STRING;
718
719 sp = composite->strings;
720 if (sp)
721 collect_langs(sp, s->wData);
722
723 list_for_each_entry(c, &cdev->configs, list) {
724 sp = c->strings;
725 if (sp)
726 collect_langs(sp, s->wData);
727
728 list_for_each_entry(f, &c->functions, list) {
729 sp = f->strings;
730 if (sp)
731 collect_langs(sp, s->wData);
732 }
733 }
734
Roel Kluin417b57b2009-08-06 16:09:51 -0700735 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700736 continue;
737 if (!len)
738 return -EINVAL;
739
740 s->bLength = 2 * (len + 1);
741 return s->bLength;
742 }
743
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200744 /* Otherwise, look up and return a specified string. First
745 * check if the string has not been overridden.
746 */
747 if (cdev->manufacturer_override == id)
748 str = iManufacturer ?: composite->iManufacturer ?:
749 composite_manufacturer;
750 else if (cdev->product_override == id)
751 str = iProduct ?: composite->iProduct;
752 else if (cdev->serial_override == id)
753 str = iSerialNumber;
754 else
755 str = NULL;
756 if (str) {
757 struct usb_gadget_strings strings = {
758 .language = language,
759 .strings = &(struct usb_string) { 0xff, str }
760 };
761 return usb_gadget_get_string(&strings, 0xff, buf);
762 }
763
764 /* String IDs are device-scoped, so we look up each string
765 * table we're told about. These lookups are infrequent;
766 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700767 */
768 if (composite->strings) {
769 len = lookup_string(composite->strings, buf, language, id);
770 if (len > 0)
771 return len;
772 }
773 list_for_each_entry(c, &cdev->configs, list) {
774 if (c->strings) {
775 len = lookup_string(c->strings, buf, language, id);
776 if (len > 0)
777 return len;
778 }
779 list_for_each_entry(f, &c->functions, list) {
780 if (!f->strings)
781 continue;
782 len = lookup_string(f->strings, buf, language, id);
783 if (len > 0)
784 return len;
785 }
786 }
787 return -EINVAL;
788}
789
790/**
791 * usb_string_id() - allocate an unused string ID
792 * @cdev: the device whose string descriptor IDs are being allocated
793 * Context: single threaded during gadget setup
794 *
795 * @usb_string_id() is called from bind() callbacks to allocate
796 * string IDs. Drivers for functions, configurations, or gadgets will
797 * then store that ID in the appropriate descriptors and string table.
798 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200799 * All string identifier should be allocated using this,
800 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
801 * that for example different functions don't wrongly assign different
802 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700803 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200804int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700805{
806 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200807 /* string id 0 is reserved by USB spec for list of
808 * supported languages */
809 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700810 cdev->next_string_id++;
811 return cdev->next_string_id;
812 }
813 return -ENODEV;
814}
815
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200816/**
817 * usb_string_ids() - allocate unused string IDs in batch
818 * @cdev: the device whose string descriptor IDs are being allocated
819 * @str: an array of usb_string objects to assign numbers to
820 * Context: single threaded during gadget setup
821 *
822 * @usb_string_ids() is called from bind() callbacks to allocate
823 * string IDs. Drivers for functions, configurations, or gadgets will
824 * then copy IDs from the string table to the appropriate descriptors
825 * and string table for other languages.
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_tab(struct usb_composite_dev *cdev, struct usb_string *str)
833{
834 int next = cdev->next_string_id;
835
836 for (; str->s; ++str) {
837 if (unlikely(next >= 254))
838 return -ENODEV;
839 str->id = ++next;
840 }
841
842 cdev->next_string_id = next;
843
844 return 0;
845}
846
847/**
848 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700849 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200850 * @n: number of string IDs to allocate
851 * Context: single threaded during gadget setup
852 *
853 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700854 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200855 * is, returns last requested ID which is now very useful information.
856 *
857 * @usb_string_ids_n() is called from bind() callbacks to allocate
858 * string IDs. Drivers for functions, configurations, or gadgets will
859 * then store that ID in the appropriate descriptors and string table.
860 *
861 * All string identifier should be allocated using this,
862 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
863 * example different functions don't wrongly assign different meanings
864 * to the same identifier.
865 */
866int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
867{
868 unsigned next = c->next_string_id;
869 if (unlikely(n > 254 || (unsigned)next + n > 254))
870 return -ENODEV;
871 c->next_string_id += n;
872 return next + 1;
873}
874
875
David Brownell40982be2008-06-19 17:52:58 -0700876/*-------------------------------------------------------------------------*/
877
878static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
879{
880 if (req->status || req->actual != req->length)
881 DBG((struct usb_composite_dev *) ep->driver_data,
882 "setup complete --> %d, %d/%d\n",
883 req->status, req->actual, req->length);
884}
885
886/*
887 * The setup() callback implements all the ep0 functionality that's
888 * not handled lower down, in hardware or the hardware driver(like
889 * device and endpoint feature flags, and their status). It's all
890 * housekeeping for the gadget function we're implementing. Most of
891 * the work is in config and function specific setup.
892 */
893static int
894composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
895{
896 struct usb_composite_dev *cdev = get_gadget_data(gadget);
897 struct usb_request *req = cdev->req;
898 int value = -EOPNOTSUPP;
899 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800900 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700901 u16 w_value = le16_to_cpu(ctrl->wValue);
902 u16 w_length = le16_to_cpu(ctrl->wLength);
903 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200904 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700905
906 /* partial re-init of the response message; the function or the
907 * gadget might need to intercept e.g. a control-OUT completion
908 * when we delegate to it.
909 */
910 req->zero = 0;
911 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530912 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700913 gadget->ep0->driver_data = cdev;
914
915 switch (ctrl->bRequest) {
916
917 /* we handle all standard USB descriptors */
918 case USB_REQ_GET_DESCRIPTOR:
919 if (ctrl->bRequestType != USB_DIR_IN)
920 goto unknown;
921 switch (w_value >> 8) {
922
923 case USB_DT_DEVICE:
924 cdev->desc.bNumConfigurations =
925 count_configs(cdev, USB_DT_DEVICE);
926 value = min(w_length, (u16) sizeof cdev->desc);
927 memcpy(req->buf, &cdev->desc, value);
928 break;
929 case USB_DT_DEVICE_QUALIFIER:
930 if (!gadget_is_dualspeed(gadget))
931 break;
932 device_qual(cdev);
933 value = min_t(int, w_length,
934 sizeof(struct usb_qualifier_descriptor));
935 break;
936 case USB_DT_OTHER_SPEED_CONFIG:
937 if (!gadget_is_dualspeed(gadget))
938 break;
939 /* FALLTHROUGH */
940 case USB_DT_CONFIG:
941 value = config_desc(cdev, w_value);
942 if (value >= 0)
943 value = min(w_length, (u16) value);
944 break;
945 case USB_DT_STRING:
946 value = get_string(cdev, req->buf,
947 w_index, w_value & 0xff);
948 if (value >= 0)
949 value = min(w_length, (u16) value);
950 break;
951 }
952 break;
953
954 /* any number of configs can work */
955 case USB_REQ_SET_CONFIGURATION:
956 if (ctrl->bRequestType != 0)
957 goto unknown;
958 if (gadget_is_otg(gadget)) {
959 if (gadget->a_hnp_support)
960 DBG(cdev, "HNP available\n");
961 else if (gadget->a_alt_hnp_support)
962 DBG(cdev, "HNP on another port\n");
963 else
964 VDBG(cdev, "HNP inactive\n");
965 }
966 spin_lock(&cdev->lock);
967 value = set_config(cdev, ctrl, w_value);
968 spin_unlock(&cdev->lock);
969 break;
970 case USB_REQ_GET_CONFIGURATION:
971 if (ctrl->bRequestType != USB_DIR_IN)
972 goto unknown;
973 if (cdev->config)
974 *(u8 *)req->buf = cdev->config->bConfigurationValue;
975 else
976 *(u8 *)req->buf = 0;
977 value = min(w_length, (u16) 1);
978 break;
979
980 /* function drivers must handle get/set altsetting; if there's
981 * no get() method, we know only altsetting zero works.
982 */
983 case USB_REQ_SET_INTERFACE:
984 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
985 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900986 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700987 break;
Bryan Wu08889512009-01-08 00:21:19 +0800988 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700989 if (!f)
990 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800991 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -0700992 break;
993 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300994 if (value == USB_GADGET_DELAYED_STATUS) {
995 DBG(cdev,
996 "%s: interface %d (%s) requested delayed status\n",
997 __func__, intf, f->name);
998 cdev->delayed_status++;
999 DBG(cdev, "delayed_status count %d\n",
1000 cdev->delayed_status);
1001 }
David Brownell40982be2008-06-19 17:52:58 -07001002 break;
1003 case USB_REQ_GET_INTERFACE:
1004 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1005 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001006 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001007 break;
Bryan Wu08889512009-01-08 00:21:19 +08001008 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001009 if (!f)
1010 break;
1011 /* lots of interfaces only need altsetting zero... */
1012 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1013 if (value < 0)
1014 break;
1015 *((u8 *)req->buf) = value;
1016 value = min(w_length, (u16) 1);
1017 break;
1018 default:
1019unknown:
1020 VDBG(cdev,
1021 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1022 ctrl->bRequestType, ctrl->bRequest,
1023 w_value, w_index, w_length);
1024
Laurent Pinchart52426582009-10-21 00:03:38 +02001025 /* functions always handle their interfaces and endpoints...
1026 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001027 * configuration code.
1028 *
1029 * REVISIT it could make sense to let the composite device
1030 * take such requests too, if that's ever needed: to work
1031 * in config 0, etc.
1032 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001033 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1034 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001035 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301036 break;
1037 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001038 break;
1039
1040 case USB_RECIP_ENDPOINT:
1041 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1042 list_for_each_entry(f, &cdev->config->functions, list) {
1043 if (test_bit(endp, f->endpoints))
1044 break;
1045 }
1046 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001047 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001048 break;
David Brownell40982be2008-06-19 17:52:58 -07001049 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001050
1051 if (f && f->setup)
1052 value = f->setup(f, ctrl);
1053 else {
David Brownell40982be2008-06-19 17:52:58 -07001054 struct usb_configuration *c;
1055
1056 c = cdev->config;
1057 if (c && c->setup)
1058 value = c->setup(c, ctrl);
1059 }
1060
1061 goto done;
1062 }
1063
1064 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001065 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001066 req->length = value;
1067 req->zero = value < w_length;
1068 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1069 if (value < 0) {
1070 DBG(cdev, "ep_queue --> %d\n", value);
1071 req->status = 0;
1072 composite_setup_complete(gadget->ep0, req);
1073 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001074 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1075 WARN(cdev,
1076 "%s: Delayed status not supported for w_length != 0",
1077 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001078 }
1079
1080done:
1081 /* device either stalls (value < 0) or reports success */
1082 return value;
1083}
1084
1085static void composite_disconnect(struct usb_gadget *gadget)
1086{
1087 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1088 unsigned long flags;
1089
1090 /* REVISIT: should we have config and device level
1091 * disconnect callbacks?
1092 */
1093 spin_lock_irqsave(&cdev->lock, flags);
1094 if (cdev->config)
1095 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001096 if (composite->disconnect)
1097 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001098 spin_unlock_irqrestore(&cdev->lock, flags);
1099}
1100
1101/*-------------------------------------------------------------------------*/
1102
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001103static ssize_t composite_show_suspended(struct device *dev,
1104 struct device_attribute *attr,
1105 char *buf)
1106{
1107 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1108 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1109
1110 return sprintf(buf, "%d\n", cdev->suspended);
1111}
1112
1113static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1114
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001115static void
David Brownell40982be2008-06-19 17:52:58 -07001116composite_unbind(struct usb_gadget *gadget)
1117{
1118 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1119
1120 /* composite_disconnect() must already have been called
1121 * by the underlying peripheral controller driver!
1122 * so there's no i/o concurrency that could affect the
1123 * state protected by cdev->lock.
1124 */
1125 WARN_ON(cdev->config);
1126
1127 while (!list_empty(&cdev->configs)) {
1128 struct usb_configuration *c;
1129
1130 c = list_first_entry(&cdev->configs,
1131 struct usb_configuration, list);
1132 while (!list_empty(&c->functions)) {
1133 struct usb_function *f;
1134
1135 f = list_first_entry(&c->functions,
1136 struct usb_function, list);
1137 list_del(&f->list);
1138 if (f->unbind) {
1139 DBG(cdev, "unbind function '%s'/%p\n",
1140 f->name, f);
1141 f->unbind(c, f);
1142 /* may free memory for "f" */
1143 }
1144 }
1145 list_del(&c->list);
1146 if (c->unbind) {
1147 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1148 c->unbind(c);
1149 /* may free memory for "c" */
1150 }
1151 }
1152 if (composite->unbind)
1153 composite->unbind(cdev);
1154
1155 if (cdev->req) {
1156 kfree(cdev->req->buf);
1157 usb_ep_free_request(gadget->ep0, cdev->req);
1158 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301159 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001160 kfree(cdev);
1161 set_gadget_data(gadget, NULL);
1162 composite = NULL;
1163}
1164
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001165static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001166{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001167 if (!*desc) {
1168 int ret = usb_string_id(cdev);
1169 if (unlikely(ret < 0))
1170 WARNING(cdev, "failed to override string ID\n");
1171 else
1172 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001173 }
David Brownell40982be2008-06-19 17:52:58 -07001174
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001175 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001176}
1177
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001178static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001179{
1180 struct usb_composite_dev *cdev;
1181 int status = -ENOMEM;
1182
1183 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1184 if (!cdev)
1185 return status;
1186
1187 spin_lock_init(&cdev->lock);
1188 cdev->gadget = gadget;
1189 set_gadget_data(gadget, cdev);
1190 INIT_LIST_HEAD(&cdev->configs);
1191
1192 /* preallocate control response and buffer */
1193 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1194 if (!cdev->req)
1195 goto fail;
1196 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1197 if (!cdev->req->buf)
1198 goto fail;
1199 cdev->req->complete = composite_setup_complete;
1200 gadget->ep0->driver_data = cdev;
1201
1202 cdev->bufsiz = USB_BUFSIZ;
1203 cdev->driver = composite;
1204
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301205 /*
1206 * As per USB compliance update, a device that is actively drawing
1207 * more than 100mA from USB must report itself as bus-powered in
1208 * the GetStatus(DEVICE) call.
1209 */
1210 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1211 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001212
1213 /* interface and string IDs start at zero via kzalloc.
1214 * we force endpoints to start unassigned; few controller
1215 * drivers will zero ep->driver_data.
1216 */
1217 usb_ep_autoconfig_reset(cdev->gadget);
1218
1219 /* composite gadget needs to assign strings for whole device (like
1220 * serial number), register function drivers, potentially update
1221 * power state and consumption, etc
1222 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001223 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001224 if (status < 0)
1225 goto fail;
1226
1227 cdev->desc = *composite->dev;
1228 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1229
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001230 /* standardized runtime overrides for device ID data */
1231 if (idVendor)
1232 cdev->desc.idVendor = cpu_to_le16(idVendor);
1233 if (idProduct)
1234 cdev->desc.idProduct = cpu_to_le16(idProduct);
1235 if (bcdDevice)
1236 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1237
Marek Belisko78bff3c2010-10-27 10:19:01 +02001238 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001239 if (iManufacturer || !cdev->desc.iManufacturer) {
1240 if (!iManufacturer && !composite->iManufacturer &&
1241 !*composite_manufacturer)
1242 snprintf(composite_manufacturer,
1243 sizeof composite_manufacturer,
1244 "%s %s with %s",
1245 init_utsname()->sysname,
1246 init_utsname()->release,
1247 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001248
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001249 cdev->manufacturer_override =
1250 override_id(cdev, &cdev->desc.iManufacturer);
1251 }
1252
1253 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1254 cdev->product_override =
1255 override_id(cdev, &cdev->desc.iProduct);
1256
1257 if (iSerialNumber)
1258 cdev->serial_override =
1259 override_id(cdev, &cdev->desc.iSerialNumber);
1260
1261 /* has userspace failed to provide a serial number? */
1262 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1263 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1264
1265 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001266 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1267 if (status)
1268 goto fail;
1269
David Brownell40982be2008-06-19 17:52:58 -07001270 INFO(cdev, "%s ready\n", composite->name);
1271 return 0;
1272
1273fail:
1274 composite_unbind(gadget);
1275 return status;
1276}
1277
1278/*-------------------------------------------------------------------------*/
1279
1280static void
1281composite_suspend(struct usb_gadget *gadget)
1282{
1283 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1284 struct usb_function *f;
1285
David Brownell89429392009-03-19 14:14:17 -07001286 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001287 * suspend/resume callbacks?
1288 */
1289 DBG(cdev, "suspend\n");
1290 if (cdev->config) {
1291 list_for_each_entry(f, &cdev->config->functions, list) {
1292 if (f->suspend)
1293 f->suspend(f);
1294 }
1295 }
David Brownell89429392009-03-19 14:14:17 -07001296 if (composite->suspend)
1297 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001298
1299 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001300
1301 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001302}
1303
1304static void
1305composite_resume(struct usb_gadget *gadget)
1306{
1307 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1308 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001309 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001310
David Brownell89429392009-03-19 14:14:17 -07001311 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001312 * suspend/resume callbacks?
1313 */
1314 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001315 if (composite->resume)
1316 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001317 if (cdev->config) {
1318 list_for_each_entry(f, &cdev->config->functions, list) {
1319 if (f->resume)
1320 f->resume(f);
1321 }
Hao Wub23f2f92010-11-29 15:17:03 +08001322
1323 maxpower = cdev->config->bMaxPower;
1324
1325 usb_gadget_vbus_draw(gadget, maxpower ?
1326 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001327 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001328
1329 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001330}
1331
1332/*-------------------------------------------------------------------------*/
1333
1334static struct usb_gadget_driver composite_driver = {
1335 .speed = USB_SPEED_HIGH,
1336
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001337 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001338
1339 .setup = composite_setup,
1340 .disconnect = composite_disconnect,
1341
1342 .suspend = composite_suspend,
1343 .resume = composite_resume,
1344
1345 .driver = {
1346 .owner = THIS_MODULE,
1347 },
1348};
1349
1350/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001351 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001352 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001353 * @bind: the callback used to allocate resources that are shared across the
1354 * whole device, such as string IDs, and add its configurations using
1355 * @usb_add_config(). This may fail by returning a negative errno
1356 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001357 * Context: single threaded during gadget setup
1358 *
1359 * This function is used to register drivers using the composite driver
1360 * framework. The return value is zero, or a negative errno value.
1361 * Those values normally come from the driver's @bind method, which does
1362 * all the work of setting up the driver to match the hardware.
1363 *
1364 * On successful return, the gadget is ready to respond to requests from
1365 * the host, unless one of its components invokes usb_gadget_disconnect()
1366 * while it was binding. That would usually be done in order to wait for
1367 * some userspace participation.
1368 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001369int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001370 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001371{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001372 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001373 return -EINVAL;
1374
1375 if (!driver->name)
1376 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001377 if (!driver->iProduct)
1378 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001379 composite_driver.function = (char *) driver->name;
1380 composite_driver.driver.name = driver->name;
1381 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001382 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001383
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001384 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001385}
1386
1387/**
1388 * usb_composite_unregister() - unregister a composite driver
1389 * @driver: the driver to unregister
1390 *
1391 * This function is used to unregister drivers using the composite
1392 * driver framework.
1393 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001394void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001395{
1396 if (composite != driver)
1397 return;
1398 usb_gadget_unregister_driver(&composite_driver);
1399}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001400
1401/**
1402 * usb_composite_setup_continue() - Continue with the control transfer
1403 * @cdev: the composite device who's control transfer was kept waiting
1404 *
1405 * This function must be called by the USB function driver to continue
1406 * with the control transfer's data/status stage in case it had requested to
1407 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1408 * can request the composite framework to delay the setup request's data/status
1409 * stages by returning USB_GADGET_DELAYED_STATUS.
1410 */
1411void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1412{
1413 int value;
1414 struct usb_request *req = cdev->req;
1415 unsigned long flags;
1416
1417 DBG(cdev, "%s\n", __func__);
1418 spin_lock_irqsave(&cdev->lock, flags);
1419
1420 if (cdev->delayed_status == 0) {
1421 WARN(cdev, "%s: Unexpected call\n", __func__);
1422
1423 } else if (--cdev->delayed_status == 0) {
1424 DBG(cdev, "%s: Completing delayed status\n", __func__);
1425 req->length = 0;
1426 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1427 if (value < 0) {
1428 DBG(cdev, "ep_queue --> %d\n", value);
1429 req->status = 0;
1430 composite_setup_complete(cdev->gadget->ep0, req);
1431 }
1432 }
1433
1434 spin_unlock_irqrestore(&cdev->lock, flags);
1435}
1436