blob: 897e4f57422f66befc0ccf2df8193eb6061d53d4 [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) {
Tatyana Brokhman7c884fe2011-06-28 16:33:52 +0300498 case USB_SPEED_LOW:
499 speed = "low";
500 break;
501 case USB_SPEED_FULL:
502 speed = "full";
503 break;
504 case USB_SPEED_HIGH:
505 speed = "high";
506 break;
507 default:
508 speed = "?";
509 break;
David Brownell40982be2008-06-19 17:52:58 -0700510 } ; speed; }), number, c ? c->label : "unconfigured");
511
512 if (!c)
513 goto done;
514
515 cdev->config = c;
516
517 /* Initialize all interfaces by setting them to altsetting zero. */
518 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
519 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200520 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700521
522 if (!f)
523 break;
524
Laurent Pinchart52426582009-10-21 00:03:38 +0200525 /*
526 * Record which endpoints are used by the function. This is used
527 * to dispatch control requests targeted at that endpoint to the
528 * function's setup callback instead of the current
529 * configuration's setup callback.
530 */
531 if (gadget->speed == USB_SPEED_HIGH)
532 descriptors = f->hs_descriptors;
533 else
534 descriptors = f->descriptors;
535
536 for (; *descriptors; ++descriptors) {
537 struct usb_endpoint_descriptor *ep;
538 int addr;
539
540 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
541 continue;
542
543 ep = (struct usb_endpoint_descriptor *)*descriptors;
544 addr = ((ep->bEndpointAddress & 0x80) >> 3)
545 | (ep->bEndpointAddress & 0x0f);
546 set_bit(addr, f->endpoints);
547 }
548
David Brownell40982be2008-06-19 17:52:58 -0700549 result = f->set_alt(f, tmp, 0);
550 if (result < 0) {
551 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
552 tmp, f->name, f, result);
553
554 reset_config(cdev);
555 goto done;
556 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300557
558 if (result == USB_GADGET_DELAYED_STATUS) {
559 DBG(cdev,
560 "%s: interface %d (%s) requested delayed status\n",
561 __func__, tmp, f->name);
562 cdev->delayed_status++;
563 DBG(cdev, "delayed_status count %d\n",
564 cdev->delayed_status);
565 }
David Brownell40982be2008-06-19 17:52:58 -0700566 }
567
568 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700569 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700570done:
571 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300572 if (result >= 0 && cdev->delayed_status)
573 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700574 return result;
575}
576
577/**
578 * usb_add_config() - add a configuration to a device.
579 * @cdev: wraps the USB gadget
580 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200581 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700582 * Context: single threaded during gadget setup
583 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200584 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700585 * add each of the configurations it supports, using this routine.
586 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200587 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700588 * is zero for success else a negative errno value. Binding configurations
589 * assigns global resources including string IDs, and per-configuration
590 * resources such as interface IDs and endpoints.
591 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200592int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200593 struct usb_configuration *config,
594 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700595{
596 int status = -EINVAL;
597 struct usb_configuration *c;
598
599 DBG(cdev, "adding config #%u '%s'/%p\n",
600 config->bConfigurationValue,
601 config->label, config);
602
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200603 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700604 goto done;
605
606 /* Prevent duplicate configuration identifiers */
607 list_for_each_entry(c, &cdev->configs, list) {
608 if (c->bConfigurationValue == config->bConfigurationValue) {
609 status = -EBUSY;
610 goto done;
611 }
612 }
613
614 config->cdev = cdev;
615 list_add_tail(&config->list, &cdev->configs);
616
617 INIT_LIST_HEAD(&config->functions);
618 config->next_interface_id = 0;
619
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200620 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700621 if (status < 0) {
622 list_del(&config->list);
623 config->cdev = NULL;
624 } else {
625 unsigned i;
626
627 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
628 config->bConfigurationValue, config,
629 config->highspeed ? " high" : "",
630 config->fullspeed
631 ? (gadget_is_dualspeed(cdev->gadget)
632 ? " full"
633 : " full/low")
634 : "");
635
636 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
637 struct usb_function *f = config->interface[i];
638
639 if (!f)
640 continue;
641 DBG(cdev, " interface %d = %s/%p\n",
642 i, f->name, f);
643 }
644 }
645
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200646 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700647 * ep->driver_data as needed.
648 */
649 usb_ep_autoconfig_reset(cdev->gadget);
650
651done:
652 if (status)
653 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
654 config->bConfigurationValue, status);
655 return status;
656}
657
658/*-------------------------------------------------------------------------*/
659
660/* We support strings in multiple languages ... string descriptor zero
661 * says which languages are supported. The typical case will be that
662 * only one language (probably English) is used, with I18N handled on
663 * the host side.
664 */
665
666static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
667{
668 const struct usb_gadget_strings *s;
669 u16 language;
670 __le16 *tmp;
671
672 while (*sp) {
673 s = *sp;
674 language = cpu_to_le16(s->language);
675 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
676 if (*tmp == language)
677 goto repeat;
678 }
679 *tmp++ = language;
680repeat:
681 sp++;
682 }
683}
684
685static int lookup_string(
686 struct usb_gadget_strings **sp,
687 void *buf,
688 u16 language,
689 int id
690)
691{
692 struct usb_gadget_strings *s;
693 int value;
694
695 while (*sp) {
696 s = *sp++;
697 if (s->language != language)
698 continue;
699 value = usb_gadget_get_string(s, id, buf);
700 if (value > 0)
701 return value;
702 }
703 return -EINVAL;
704}
705
706static int get_string(struct usb_composite_dev *cdev,
707 void *buf, u16 language, int id)
708{
709 struct usb_configuration *c;
710 struct usb_function *f;
711 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200712 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700713
714 /* Yes, not only is USB's I18N support probably more than most
715 * folk will ever care about ... also, it's all supported here.
716 * (Except for UTF8 support for Unicode's "Astral Planes".)
717 */
718
719 /* 0 == report all available language codes */
720 if (id == 0) {
721 struct usb_string_descriptor *s = buf;
722 struct usb_gadget_strings **sp;
723
724 memset(s, 0, 256);
725 s->bDescriptorType = USB_DT_STRING;
726
727 sp = composite->strings;
728 if (sp)
729 collect_langs(sp, s->wData);
730
731 list_for_each_entry(c, &cdev->configs, list) {
732 sp = c->strings;
733 if (sp)
734 collect_langs(sp, s->wData);
735
736 list_for_each_entry(f, &c->functions, list) {
737 sp = f->strings;
738 if (sp)
739 collect_langs(sp, s->wData);
740 }
741 }
742
Roel Kluin417b57b2009-08-06 16:09:51 -0700743 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700744 continue;
745 if (!len)
746 return -EINVAL;
747
748 s->bLength = 2 * (len + 1);
749 return s->bLength;
750 }
751
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200752 /* Otherwise, look up and return a specified string. First
753 * check if the string has not been overridden.
754 */
755 if (cdev->manufacturer_override == id)
756 str = iManufacturer ?: composite->iManufacturer ?:
757 composite_manufacturer;
758 else if (cdev->product_override == id)
759 str = iProduct ?: composite->iProduct;
760 else if (cdev->serial_override == id)
761 str = iSerialNumber;
762 else
763 str = NULL;
764 if (str) {
765 struct usb_gadget_strings strings = {
766 .language = language,
767 .strings = &(struct usb_string) { 0xff, str }
768 };
769 return usb_gadget_get_string(&strings, 0xff, buf);
770 }
771
772 /* String IDs are device-scoped, so we look up each string
773 * table we're told about. These lookups are infrequent;
774 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700775 */
776 if (composite->strings) {
777 len = lookup_string(composite->strings, buf, language, id);
778 if (len > 0)
779 return len;
780 }
781 list_for_each_entry(c, &cdev->configs, list) {
782 if (c->strings) {
783 len = lookup_string(c->strings, buf, language, id);
784 if (len > 0)
785 return len;
786 }
787 list_for_each_entry(f, &c->functions, list) {
788 if (!f->strings)
789 continue;
790 len = lookup_string(f->strings, buf, language, id);
791 if (len > 0)
792 return len;
793 }
794 }
795 return -EINVAL;
796}
797
798/**
799 * usb_string_id() - allocate an unused string ID
800 * @cdev: the device whose string descriptor IDs are being allocated
801 * Context: single threaded during gadget setup
802 *
803 * @usb_string_id() is called from bind() callbacks to allocate
804 * string IDs. Drivers for functions, configurations, or gadgets will
805 * then store that ID in the appropriate descriptors and string table.
806 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200807 * All string identifier should be allocated using this,
808 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
809 * that for example different functions don't wrongly assign different
810 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700811 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200812int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700813{
814 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200815 /* string id 0 is reserved by USB spec for list of
816 * supported languages */
817 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700818 cdev->next_string_id++;
819 return cdev->next_string_id;
820 }
821 return -ENODEV;
822}
823
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200824/**
825 * usb_string_ids() - allocate unused string IDs in batch
826 * @cdev: the device whose string descriptor IDs are being allocated
827 * @str: an array of usb_string objects to assign numbers to
828 * Context: single threaded during gadget setup
829 *
830 * @usb_string_ids() is called from bind() callbacks to allocate
831 * string IDs. Drivers for functions, configurations, or gadgets will
832 * then copy IDs from the string table to the appropriate descriptors
833 * and string table for other languages.
834 *
835 * All string identifier should be allocated using this,
836 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
837 * example different functions don't wrongly assign different meanings
838 * to the same identifier.
839 */
840int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
841{
842 int next = cdev->next_string_id;
843
844 for (; str->s; ++str) {
845 if (unlikely(next >= 254))
846 return -ENODEV;
847 str->id = ++next;
848 }
849
850 cdev->next_string_id = next;
851
852 return 0;
853}
854
855/**
856 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700857 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200858 * @n: number of string IDs to allocate
859 * Context: single threaded during gadget setup
860 *
861 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700862 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200863 * is, returns last requested ID which is now very useful information.
864 *
865 * @usb_string_ids_n() is called from bind() callbacks to allocate
866 * string IDs. Drivers for functions, configurations, or gadgets will
867 * then store that ID in the appropriate descriptors and string table.
868 *
869 * All string identifier should be allocated using this,
870 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
871 * example different functions don't wrongly assign different meanings
872 * to the same identifier.
873 */
874int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
875{
876 unsigned next = c->next_string_id;
877 if (unlikely(n > 254 || (unsigned)next + n > 254))
878 return -ENODEV;
879 c->next_string_id += n;
880 return next + 1;
881}
882
883
David Brownell40982be2008-06-19 17:52:58 -0700884/*-------------------------------------------------------------------------*/
885
886static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
887{
888 if (req->status || req->actual != req->length)
889 DBG((struct usb_composite_dev *) ep->driver_data,
890 "setup complete --> %d, %d/%d\n",
891 req->status, req->actual, req->length);
892}
893
894/*
895 * The setup() callback implements all the ep0 functionality that's
896 * not handled lower down, in hardware or the hardware driver(like
897 * device and endpoint feature flags, and their status). It's all
898 * housekeeping for the gadget function we're implementing. Most of
899 * the work is in config and function specific setup.
900 */
901static int
902composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
903{
904 struct usb_composite_dev *cdev = get_gadget_data(gadget);
905 struct usb_request *req = cdev->req;
906 int value = -EOPNOTSUPP;
907 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800908 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700909 u16 w_value = le16_to_cpu(ctrl->wValue);
910 u16 w_length = le16_to_cpu(ctrl->wLength);
911 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200912 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700913
914 /* partial re-init of the response message; the function or the
915 * gadget might need to intercept e.g. a control-OUT completion
916 * when we delegate to it.
917 */
918 req->zero = 0;
919 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530920 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700921 gadget->ep0->driver_data = cdev;
922
923 switch (ctrl->bRequest) {
924
925 /* we handle all standard USB descriptors */
926 case USB_REQ_GET_DESCRIPTOR:
927 if (ctrl->bRequestType != USB_DIR_IN)
928 goto unknown;
929 switch (w_value >> 8) {
930
931 case USB_DT_DEVICE:
932 cdev->desc.bNumConfigurations =
933 count_configs(cdev, USB_DT_DEVICE);
934 value = min(w_length, (u16) sizeof cdev->desc);
935 memcpy(req->buf, &cdev->desc, value);
936 break;
937 case USB_DT_DEVICE_QUALIFIER:
938 if (!gadget_is_dualspeed(gadget))
939 break;
940 device_qual(cdev);
941 value = min_t(int, w_length,
942 sizeof(struct usb_qualifier_descriptor));
943 break;
944 case USB_DT_OTHER_SPEED_CONFIG:
945 if (!gadget_is_dualspeed(gadget))
946 break;
947 /* FALLTHROUGH */
948 case USB_DT_CONFIG:
949 value = config_desc(cdev, w_value);
950 if (value >= 0)
951 value = min(w_length, (u16) value);
952 break;
953 case USB_DT_STRING:
954 value = get_string(cdev, req->buf,
955 w_index, w_value & 0xff);
956 if (value >= 0)
957 value = min(w_length, (u16) value);
958 break;
959 }
960 break;
961
962 /* any number of configs can work */
963 case USB_REQ_SET_CONFIGURATION:
964 if (ctrl->bRequestType != 0)
965 goto unknown;
966 if (gadget_is_otg(gadget)) {
967 if (gadget->a_hnp_support)
968 DBG(cdev, "HNP available\n");
969 else if (gadget->a_alt_hnp_support)
970 DBG(cdev, "HNP on another port\n");
971 else
972 VDBG(cdev, "HNP inactive\n");
973 }
974 spin_lock(&cdev->lock);
975 value = set_config(cdev, ctrl, w_value);
976 spin_unlock(&cdev->lock);
977 break;
978 case USB_REQ_GET_CONFIGURATION:
979 if (ctrl->bRequestType != USB_DIR_IN)
980 goto unknown;
981 if (cdev->config)
982 *(u8 *)req->buf = cdev->config->bConfigurationValue;
983 else
984 *(u8 *)req->buf = 0;
985 value = min(w_length, (u16) 1);
986 break;
987
988 /* function drivers must handle get/set altsetting; if there's
989 * no get() method, we know only altsetting zero works.
990 */
991 case USB_REQ_SET_INTERFACE:
992 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
993 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +0900994 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -0700995 break;
Bryan Wu08889512009-01-08 00:21:19 +0800996 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -0700997 if (!f)
998 break;
Bryan Wudd4dff82009-01-08 00:21:18 +0800999 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001000 break;
1001 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001002 if (value == USB_GADGET_DELAYED_STATUS) {
1003 DBG(cdev,
1004 "%s: interface %d (%s) requested delayed status\n",
1005 __func__, intf, f->name);
1006 cdev->delayed_status++;
1007 DBG(cdev, "delayed_status count %d\n",
1008 cdev->delayed_status);
1009 }
David Brownell40982be2008-06-19 17:52:58 -07001010 break;
1011 case USB_REQ_GET_INTERFACE:
1012 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1013 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001014 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001015 break;
Bryan Wu08889512009-01-08 00:21:19 +08001016 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001017 if (!f)
1018 break;
1019 /* lots of interfaces only need altsetting zero... */
1020 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1021 if (value < 0)
1022 break;
1023 *((u8 *)req->buf) = value;
1024 value = min(w_length, (u16) 1);
1025 break;
1026 default:
1027unknown:
1028 VDBG(cdev,
1029 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1030 ctrl->bRequestType, ctrl->bRequest,
1031 w_value, w_index, w_length);
1032
Laurent Pinchart52426582009-10-21 00:03:38 +02001033 /* functions always handle their interfaces and endpoints...
1034 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001035 * configuration code.
1036 *
1037 * REVISIT it could make sense to let the composite device
1038 * take such requests too, if that's ever needed: to work
1039 * in config 0, etc.
1040 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001041 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1042 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001043 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301044 break;
1045 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001046 break;
1047
1048 case USB_RECIP_ENDPOINT:
1049 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1050 list_for_each_entry(f, &cdev->config->functions, list) {
1051 if (test_bit(endp, f->endpoints))
1052 break;
1053 }
1054 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001055 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001056 break;
David Brownell40982be2008-06-19 17:52:58 -07001057 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001058
1059 if (f && f->setup)
1060 value = f->setup(f, ctrl);
1061 else {
David Brownell40982be2008-06-19 17:52:58 -07001062 struct usb_configuration *c;
1063
1064 c = cdev->config;
1065 if (c && c->setup)
1066 value = c->setup(c, ctrl);
1067 }
1068
1069 goto done;
1070 }
1071
1072 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001073 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001074 req->length = value;
1075 req->zero = value < w_length;
1076 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1077 if (value < 0) {
1078 DBG(cdev, "ep_queue --> %d\n", value);
1079 req->status = 0;
1080 composite_setup_complete(gadget->ep0, req);
1081 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001082 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1083 WARN(cdev,
1084 "%s: Delayed status not supported for w_length != 0",
1085 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001086 }
1087
1088done:
1089 /* device either stalls (value < 0) or reports success */
1090 return value;
1091}
1092
1093static void composite_disconnect(struct usb_gadget *gadget)
1094{
1095 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1096 unsigned long flags;
1097
1098 /* REVISIT: should we have config and device level
1099 * disconnect callbacks?
1100 */
1101 spin_lock_irqsave(&cdev->lock, flags);
1102 if (cdev->config)
1103 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001104 if (composite->disconnect)
1105 composite->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001106 spin_unlock_irqrestore(&cdev->lock, flags);
1107}
1108
1109/*-------------------------------------------------------------------------*/
1110
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001111static ssize_t composite_show_suspended(struct device *dev,
1112 struct device_attribute *attr,
1113 char *buf)
1114{
1115 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1116 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1117
1118 return sprintf(buf, "%d\n", cdev->suspended);
1119}
1120
1121static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1122
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001123static void
David Brownell40982be2008-06-19 17:52:58 -07001124composite_unbind(struct usb_gadget *gadget)
1125{
1126 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1127
1128 /* composite_disconnect() must already have been called
1129 * by the underlying peripheral controller driver!
1130 * so there's no i/o concurrency that could affect the
1131 * state protected by cdev->lock.
1132 */
1133 WARN_ON(cdev->config);
1134
1135 while (!list_empty(&cdev->configs)) {
1136 struct usb_configuration *c;
1137
1138 c = list_first_entry(&cdev->configs,
1139 struct usb_configuration, list);
1140 while (!list_empty(&c->functions)) {
1141 struct usb_function *f;
1142
1143 f = list_first_entry(&c->functions,
1144 struct usb_function, list);
1145 list_del(&f->list);
1146 if (f->unbind) {
1147 DBG(cdev, "unbind function '%s'/%p\n",
1148 f->name, f);
1149 f->unbind(c, f);
1150 /* may free memory for "f" */
1151 }
1152 }
1153 list_del(&c->list);
1154 if (c->unbind) {
1155 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1156 c->unbind(c);
1157 /* may free memory for "c" */
1158 }
1159 }
1160 if (composite->unbind)
1161 composite->unbind(cdev);
1162
1163 if (cdev->req) {
1164 kfree(cdev->req->buf);
1165 usb_ep_free_request(gadget->ep0, cdev->req);
1166 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301167 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001168 kfree(cdev);
1169 set_gadget_data(gadget, NULL);
1170 composite = NULL;
1171}
1172
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001173static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001174{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001175 if (!*desc) {
1176 int ret = usb_string_id(cdev);
1177 if (unlikely(ret < 0))
1178 WARNING(cdev, "failed to override string ID\n");
1179 else
1180 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001181 }
David Brownell40982be2008-06-19 17:52:58 -07001182
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001183 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001184}
1185
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001186static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001187{
1188 struct usb_composite_dev *cdev;
1189 int status = -ENOMEM;
1190
1191 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1192 if (!cdev)
1193 return status;
1194
1195 spin_lock_init(&cdev->lock);
1196 cdev->gadget = gadget;
1197 set_gadget_data(gadget, cdev);
1198 INIT_LIST_HEAD(&cdev->configs);
1199
1200 /* preallocate control response and buffer */
1201 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1202 if (!cdev->req)
1203 goto fail;
1204 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1205 if (!cdev->req->buf)
1206 goto fail;
1207 cdev->req->complete = composite_setup_complete;
1208 gadget->ep0->driver_data = cdev;
1209
1210 cdev->bufsiz = USB_BUFSIZ;
1211 cdev->driver = composite;
1212
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301213 /*
1214 * As per USB compliance update, a device that is actively drawing
1215 * more than 100mA from USB must report itself as bus-powered in
1216 * the GetStatus(DEVICE) call.
1217 */
1218 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1219 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001220
1221 /* interface and string IDs start at zero via kzalloc.
1222 * we force endpoints to start unassigned; few controller
1223 * drivers will zero ep->driver_data.
1224 */
1225 usb_ep_autoconfig_reset(cdev->gadget);
1226
1227 /* composite gadget needs to assign strings for whole device (like
1228 * serial number), register function drivers, potentially update
1229 * power state and consumption, etc
1230 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001231 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001232 if (status < 0)
1233 goto fail;
1234
1235 cdev->desc = *composite->dev;
1236 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1237
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001238 /* standardized runtime overrides for device ID data */
1239 if (idVendor)
1240 cdev->desc.idVendor = cpu_to_le16(idVendor);
1241 if (idProduct)
1242 cdev->desc.idProduct = cpu_to_le16(idProduct);
1243 if (bcdDevice)
1244 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1245
Marek Belisko78bff3c2010-10-27 10:19:01 +02001246 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001247 if (iManufacturer || !cdev->desc.iManufacturer) {
1248 if (!iManufacturer && !composite->iManufacturer &&
1249 !*composite_manufacturer)
1250 snprintf(composite_manufacturer,
1251 sizeof composite_manufacturer,
1252 "%s %s with %s",
1253 init_utsname()->sysname,
1254 init_utsname()->release,
1255 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001256
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001257 cdev->manufacturer_override =
1258 override_id(cdev, &cdev->desc.iManufacturer);
1259 }
1260
1261 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1262 cdev->product_override =
1263 override_id(cdev, &cdev->desc.iProduct);
1264
1265 if (iSerialNumber)
1266 cdev->serial_override =
1267 override_id(cdev, &cdev->desc.iSerialNumber);
1268
1269 /* has userspace failed to provide a serial number? */
1270 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1271 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1272
1273 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001274 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1275 if (status)
1276 goto fail;
1277
David Brownell40982be2008-06-19 17:52:58 -07001278 INFO(cdev, "%s ready\n", composite->name);
1279 return 0;
1280
1281fail:
1282 composite_unbind(gadget);
1283 return status;
1284}
1285
1286/*-------------------------------------------------------------------------*/
1287
1288static void
1289composite_suspend(struct usb_gadget *gadget)
1290{
1291 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1292 struct usb_function *f;
1293
David Brownell89429392009-03-19 14:14:17 -07001294 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001295 * suspend/resume callbacks?
1296 */
1297 DBG(cdev, "suspend\n");
1298 if (cdev->config) {
1299 list_for_each_entry(f, &cdev->config->functions, list) {
1300 if (f->suspend)
1301 f->suspend(f);
1302 }
1303 }
David Brownell89429392009-03-19 14:14:17 -07001304 if (composite->suspend)
1305 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001306
1307 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001308
1309 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001310}
1311
1312static void
1313composite_resume(struct usb_gadget *gadget)
1314{
1315 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1316 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001317 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001318
David Brownell89429392009-03-19 14:14:17 -07001319 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001320 * suspend/resume callbacks?
1321 */
1322 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001323 if (composite->resume)
1324 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001325 if (cdev->config) {
1326 list_for_each_entry(f, &cdev->config->functions, list) {
1327 if (f->resume)
1328 f->resume(f);
1329 }
Hao Wub23f2f92010-11-29 15:17:03 +08001330
1331 maxpower = cdev->config->bMaxPower;
1332
1333 usb_gadget_vbus_draw(gadget, maxpower ?
1334 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001335 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001336
1337 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001338}
1339
1340/*-------------------------------------------------------------------------*/
1341
1342static struct usb_gadget_driver composite_driver = {
1343 .speed = USB_SPEED_HIGH,
1344
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001345 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001346
1347 .setup = composite_setup,
1348 .disconnect = composite_disconnect,
1349
1350 .suspend = composite_suspend,
1351 .resume = composite_resume,
1352
1353 .driver = {
1354 .owner = THIS_MODULE,
1355 },
1356};
1357
1358/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001359 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001360 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001361 * @bind: the callback used to allocate resources that are shared across the
1362 * whole device, such as string IDs, and add its configurations using
1363 * @usb_add_config(). This may fail by returning a negative errno
1364 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001365 * Context: single threaded during gadget setup
1366 *
1367 * This function is used to register drivers using the composite driver
1368 * framework. The return value is zero, or a negative errno value.
1369 * Those values normally come from the driver's @bind method, which does
1370 * all the work of setting up the driver to match the hardware.
1371 *
1372 * On successful return, the gadget is ready to respond to requests from
1373 * the host, unless one of its components invokes usb_gadget_disconnect()
1374 * while it was binding. That would usually be done in order to wait for
1375 * some userspace participation.
1376 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001377int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001378 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001379{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001380 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001381 return -EINVAL;
1382
1383 if (!driver->name)
1384 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001385 if (!driver->iProduct)
1386 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001387 composite_driver.function = (char *) driver->name;
1388 composite_driver.driver.name = driver->name;
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +03001389 composite_driver.speed = min((u8)composite_driver.speed,
1390 (u8)driver->max_speed);
David Brownell40982be2008-06-19 17:52:58 -07001391 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001392 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001393
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001394 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001395}
1396
1397/**
1398 * usb_composite_unregister() - unregister a composite driver
1399 * @driver: the driver to unregister
1400 *
1401 * This function is used to unregister drivers using the composite
1402 * driver framework.
1403 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001404void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001405{
1406 if (composite != driver)
1407 return;
1408 usb_gadget_unregister_driver(&composite_driver);
1409}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001410
1411/**
1412 * usb_composite_setup_continue() - Continue with the control transfer
1413 * @cdev: the composite device who's control transfer was kept waiting
1414 *
1415 * This function must be called by the USB function driver to continue
1416 * with the control transfer's data/status stage in case it had requested to
1417 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1418 * can request the composite framework to delay the setup request's data/status
1419 * stages by returning USB_GADGET_DELAYED_STATUS.
1420 */
1421void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1422{
1423 int value;
1424 struct usb_request *req = cdev->req;
1425 unsigned long flags;
1426
1427 DBG(cdev, "%s\n", __func__);
1428 spin_lock_irqsave(&cdev->lock, flags);
1429
1430 if (cdev->delayed_status == 0) {
1431 WARN(cdev, "%s: Unexpected call\n", __func__);
1432
1433 } else if (--cdev->delayed_status == 0) {
1434 DBG(cdev, "%s: Completing delayed status\n", __func__);
1435 req->length = 0;
1436 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1437 if (value < 0) {
1438 DBG(cdev, "ep_queue --> %d\n", value);
1439 req->status = 0;
1440 composite_setup_complete(cdev->gadget->ep0, req);
1441 }
1442 }
1443
1444 spin_unlock_irqrestore(&cdev->lock, flags);
1445}
1446