blob: b34c59ed7769c03433308eb986d63fb51e054876 [file] [log] [blame]
David Brownell40982be2008-06-19 17:52:58 -07001/*
2 * composite.c - infrastructure for Composite USB Gadgets
3 *
4 * Copyright (C) 2006-2008 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21/* #define VERBOSE_DEBUG */
22
23#include <linux/kallsyms.h>
24#include <linux/kernel.h>
25#include <linux/slab.h>
26#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020027#include <linux/utsname.h>
Benoit Gobyaab96812011-04-19 20:37:33 -070028
David Brownell40982be2008-06-19 17:52:58 -070029#include <linux/usb/composite.h>
David Brownac5d1542012-02-06 10:37:22 -080030
David Brownell40982be2008-06-19 17:52:58 -070031
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 */
Manu Gautam8d887a12011-10-12 17:13:52 +053040#define USB_BUFSIZ 4096
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 Brokhmancf64ce42011-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
Benoit Gobyaab96812011-04-19 20:37:33 -0700182 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -0700183 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)
Benoit Gobyaab96812011-04-19 20:37:33 -0700214 DBG(config->cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700215 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;
Benoit Gobyaab96812011-04-19 20:37:33 -0700333 /* wTotalLength is written later */
334 c->bNumInterfaces = config->next_interface_id;
David Brownell40982be2008-06-19 17:52:58 -0700335 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
David Brownac5d1542012-02-06 10:37:22 -0800354 if (speed == USB_SPEED_HIGH)
David Brownell40982be2008-06-19 17:52:58 -0700355 descriptors = f->hs_descriptors;
David Brownac5d1542012-02-06 10:37:22 -0800356 else
David Brownell40982be2008-06-19 17:52:58 -0700357 descriptors = f->descriptors;
Benoit Gobyaab96812011-04-19 20:37:33 -0700358 if (!descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700359 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
David Brownac5d1542012-02-06 10:37:22 -0800380 if (gadget_is_dualspeed(gadget)) {
381 int hs = 0;
382
David Brownell40982be2008-06-19 17:52:58 -0700383 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 */
David Brownac5d1542012-02-06 10:37:22 -0800396 if (speed == USB_SPEED_HIGH) {
David Brownell40982be2008-06-19 17:52:58 -0700397 if (!c->highspeed)
398 continue;
David Brownac5d1542012-02-06 10:37:22 -0800399 } else {
David Brownell40982be2008-06-19 17:52:58 -0700400 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 */
David Brownac5d1542012-02-06 10:37:22 -0800425 if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700426 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 */
David Brownac5d1542012-02-06 10:37:22 -0800449 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
David Brownell40982be2008-06-19 17:52:58 -0700450 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
David Brownac5d1542012-02-06 10:37:22 -0800480 if (cdev->config)
481 reset_config(cdev);
482
David Brownell40982be2008-06-19 17:52:58 -0700483 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;
David Brownac5d1542012-02-06 10:37:22 -0800492 } else
David Brownell40982be2008-06-19 17:52:58 -0700493 result = 0;
494
495 INFO(cdev, "%s speed config #%d: %s\n",
496 ({ char *speed;
497 switch (gadget->speed) {
Tatyana Brokhman5ef53d92011-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 */
David Brownac5d1542012-02-06 10:37:22 -0800531 if (gadget->speed == USB_SPEED_HIGH)
Laurent Pinchart52426582009-10-21 00:03:38 +0200532 descriptors = f->hs_descriptors;
David Brownac5d1542012-02-06 10:37:22 -0800533 else
Laurent Pinchart52426582009-10-21 00:03:38 +0200534 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);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400572
Roger Quadros1b9ba002011-05-09 13:08:06 +0300573 if (result >= 0 && cdev->delayed_status)
574 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700575 return result;
576}
577
578/**
579 * usb_add_config() - add a configuration to a device.
580 * @cdev: wraps the USB gadget
581 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200582 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700583 * Context: single threaded during gadget setup
584 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200585 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700586 * add each of the configurations it supports, using this routine.
587 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200588 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700589 * is zero for success else a negative errno value. Binding configurations
590 * assigns global resources including string IDs, and per-configuration
591 * resources such as interface IDs and endpoints.
592 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200593int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200594 struct usb_configuration *config,
595 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700596{
597 int status = -EINVAL;
598 struct usb_configuration *c;
599
600 DBG(cdev, "adding config #%u '%s'/%p\n",
601 config->bConfigurationValue,
602 config->label, config);
603
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200604 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700605 goto done;
606
607 /* Prevent duplicate configuration identifiers */
608 list_for_each_entry(c, &cdev->configs, list) {
609 if (c->bConfigurationValue == config->bConfigurationValue) {
610 status = -EBUSY;
611 goto done;
612 }
613 }
614
615 config->cdev = cdev;
616 list_add_tail(&config->list, &cdev->configs);
617
618 INIT_LIST_HEAD(&config->functions);
619 config->next_interface_id = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700620 memset(config->interface, '\0', sizeof(config->interface));
David Brownell40982be2008-06-19 17:52:58 -0700621
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200622 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700623 if (status < 0) {
624 list_del(&config->list);
625 config->cdev = NULL;
626 } else {
627 unsigned i;
628
David Brownac5d1542012-02-06 10:37:22 -0800629 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700630 config->bConfigurationValue, config,
631 config->highspeed ? " high" : "",
632 config->fullspeed
633 ? (gadget_is_dualspeed(cdev->gadget)
634 ? " full"
635 : " full/low")
636 : "");
637
638 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
639 struct usb_function *f = config->interface[i];
640
641 if (!f)
642 continue;
643 DBG(cdev, " interface %d = %s/%p\n",
644 i, f->name, f);
645 }
646 }
647
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200648 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700649 * ep->driver_data as needed.
650 */
651 usb_ep_autoconfig_reset(cdev->gadget);
652
653done:
654 if (status)
655 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
656 config->bConfigurationValue, status);
657 return status;
658}
659
Benoit Goby94df1bd2011-05-25 23:59:43 -0700660static int remove_config(struct usb_composite_dev *cdev,
661 struct usb_configuration *config)
662{
663 while (!list_empty(&config->functions)) {
664 struct usb_function *f;
665
666 f = list_first_entry(&config->functions,
667 struct usb_function, list);
668 list_del(&f->list);
669 if (f->unbind) {
670 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
671 f->unbind(config, f);
672 /* may free memory for "f" */
673 }
674 }
675 list_del(&config->list);
676 if (config->unbind) {
677 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
678 config->unbind(config);
679 /* may free memory for "c" */
680 }
681 return 0;
682}
683
684int usb_remove_config(struct usb_composite_dev *cdev,
685 struct usb_configuration *config)
686{
687 unsigned long flags;
688
689 spin_lock_irqsave(&cdev->lock, flags);
690
691 if (cdev->config == config)
692 reset_config(cdev);
693
694 spin_unlock_irqrestore(&cdev->lock, flags);
695
696 return remove_config(cdev, config);
697}
698
David Brownell40982be2008-06-19 17:52:58 -0700699/*-------------------------------------------------------------------------*/
700
701/* We support strings in multiple languages ... string descriptor zero
702 * says which languages are supported. The typical case will be that
703 * only one language (probably English) is used, with I18N handled on
704 * the host side.
705 */
706
707static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
708{
709 const struct usb_gadget_strings *s;
710 u16 language;
711 __le16 *tmp;
712
713 while (*sp) {
714 s = *sp;
715 language = cpu_to_le16(s->language);
716 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
717 if (*tmp == language)
718 goto repeat;
719 }
720 *tmp++ = language;
721repeat:
722 sp++;
723 }
724}
725
726static int lookup_string(
727 struct usb_gadget_strings **sp,
728 void *buf,
729 u16 language,
730 int id
731)
732{
733 struct usb_gadget_strings *s;
734 int value;
735
736 while (*sp) {
737 s = *sp++;
738 if (s->language != language)
739 continue;
740 value = usb_gadget_get_string(s, id, buf);
741 if (value > 0)
742 return value;
743 }
744 return -EINVAL;
745}
746
747static int get_string(struct usb_composite_dev *cdev,
748 void *buf, u16 language, int id)
749{
750 struct usb_configuration *c;
751 struct usb_function *f;
752 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200753 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700754
755 /* Yes, not only is USB's I18N support probably more than most
756 * folk will ever care about ... also, it's all supported here.
757 * (Except for UTF8 support for Unicode's "Astral Planes".)
758 */
759
760 /* 0 == report all available language codes */
761 if (id == 0) {
762 struct usb_string_descriptor *s = buf;
763 struct usb_gadget_strings **sp;
764
765 memset(s, 0, 256);
766 s->bDescriptorType = USB_DT_STRING;
767
768 sp = composite->strings;
769 if (sp)
770 collect_langs(sp, s->wData);
771
772 list_for_each_entry(c, &cdev->configs, list) {
773 sp = c->strings;
774 if (sp)
775 collect_langs(sp, s->wData);
776
777 list_for_each_entry(f, &c->functions, list) {
778 sp = f->strings;
779 if (sp)
780 collect_langs(sp, s->wData);
781 }
782 }
783
Roel Kluin417b57b2009-08-06 16:09:51 -0700784 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700785 continue;
786 if (!len)
787 return -EINVAL;
788
789 s->bLength = 2 * (len + 1);
790 return s->bLength;
791 }
792
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200793 /* Otherwise, look up and return a specified string. First
794 * check if the string has not been overridden.
795 */
796 if (cdev->manufacturer_override == id)
797 str = iManufacturer ?: composite->iManufacturer ?:
798 composite_manufacturer;
799 else if (cdev->product_override == id)
800 str = iProduct ?: composite->iProduct;
801 else if (cdev->serial_override == id)
802 str = iSerialNumber;
803 else
804 str = NULL;
805 if (str) {
806 struct usb_gadget_strings strings = {
807 .language = language,
808 .strings = &(struct usb_string) { 0xff, str }
809 };
810 return usb_gadget_get_string(&strings, 0xff, buf);
811 }
812
813 /* String IDs are device-scoped, so we look up each string
814 * table we're told about. These lookups are infrequent;
815 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700816 */
817 if (composite->strings) {
818 len = lookup_string(composite->strings, buf, language, id);
819 if (len > 0)
820 return len;
821 }
822 list_for_each_entry(c, &cdev->configs, list) {
823 if (c->strings) {
824 len = lookup_string(c->strings, buf, language, id);
825 if (len > 0)
826 return len;
827 }
828 list_for_each_entry(f, &c->functions, list) {
829 if (!f->strings)
830 continue;
831 len = lookup_string(f->strings, buf, language, id);
832 if (len > 0)
833 return len;
834 }
835 }
836 return -EINVAL;
837}
838
839/**
840 * usb_string_id() - allocate an unused string ID
841 * @cdev: the device whose string descriptor IDs are being allocated
842 * Context: single threaded during gadget setup
843 *
844 * @usb_string_id() is called from bind() callbacks to allocate
845 * string IDs. Drivers for functions, configurations, or gadgets will
846 * then store that ID in the appropriate descriptors and string table.
847 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200848 * All string identifier should be allocated using this,
849 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
850 * that for example different functions don't wrongly assign different
851 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700852 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200853int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700854{
855 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200856 /* string id 0 is reserved by USB spec for list of
857 * supported languages */
858 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700859 cdev->next_string_id++;
860 return cdev->next_string_id;
861 }
862 return -ENODEV;
863}
864
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200865/**
866 * usb_string_ids() - allocate unused string IDs in batch
867 * @cdev: the device whose string descriptor IDs are being allocated
868 * @str: an array of usb_string objects to assign numbers to
869 * Context: single threaded during gadget setup
870 *
871 * @usb_string_ids() is called from bind() callbacks to allocate
872 * string IDs. Drivers for functions, configurations, or gadgets will
873 * then copy IDs from the string table to the appropriate descriptors
874 * and string table for other languages.
875 *
876 * All string identifier should be allocated using this,
877 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
878 * example different functions don't wrongly assign different meanings
879 * to the same identifier.
880 */
881int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
882{
883 int next = cdev->next_string_id;
884
885 for (; str->s; ++str) {
886 if (unlikely(next >= 254))
887 return -ENODEV;
888 str->id = ++next;
889 }
890
891 cdev->next_string_id = next;
892
893 return 0;
894}
895
896/**
897 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700898 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200899 * @n: number of string IDs to allocate
900 * Context: single threaded during gadget setup
901 *
902 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700903 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200904 * is, returns last requested ID which is now very useful information.
905 *
906 * @usb_string_ids_n() is called from bind() callbacks to allocate
907 * string IDs. Drivers for functions, configurations, or gadgets will
908 * then store that ID in the appropriate descriptors and string table.
909 *
910 * All string identifier should be allocated using this,
911 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
912 * example different functions don't wrongly assign different meanings
913 * to the same identifier.
914 */
915int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
916{
917 unsigned next = c->next_string_id;
918 if (unlikely(n > 254 || (unsigned)next + n > 254))
919 return -ENODEV;
920 c->next_string_id += n;
921 return next + 1;
922}
923
924
David Brownell40982be2008-06-19 17:52:58 -0700925/*-------------------------------------------------------------------------*/
926
927static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
928{
929 if (req->status || req->actual != req->length)
930 DBG((struct usb_composite_dev *) ep->driver_data,
931 "setup complete --> %d, %d/%d\n",
932 req->status, req->actual, req->length);
933}
934
935/*
936 * The setup() callback implements all the ep0 functionality that's
937 * not handled lower down, in hardware or the hardware driver(like
938 * device and endpoint feature flags, and their status). It's all
939 * housekeeping for the gadget function we're implementing. Most of
940 * the work is in config and function specific setup.
941 */
942static int
943composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
944{
945 struct usb_composite_dev *cdev = get_gadget_data(gadget);
946 struct usb_request *req = cdev->req;
947 int value = -EOPNOTSUPP;
948 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800949 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700950 u16 w_value = le16_to_cpu(ctrl->wValue);
951 u16 w_length = le16_to_cpu(ctrl->wLength);
952 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200953 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700954
Manu Gautam8d887a12011-10-12 17:13:52 +0530955
956 if (w_length > USB_BUFSIZ)
957 return value;
958
David Brownell40982be2008-06-19 17:52:58 -0700959 /* partial re-init of the response message; the function or the
960 * gadget might need to intercept e.g. a control-OUT completion
961 * when we delegate to it.
962 */
963 req->zero = 0;
964 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530965 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700966 gadget->ep0->driver_data = cdev;
967
968 switch (ctrl->bRequest) {
969
970 /* we handle all standard USB descriptors */
971 case USB_REQ_GET_DESCRIPTOR:
972 if (ctrl->bRequestType != USB_DIR_IN)
973 goto unknown;
974 switch (w_value >> 8) {
975
976 case USB_DT_DEVICE:
977 cdev->desc.bNumConfigurations =
978 count_configs(cdev, USB_DT_DEVICE);
979 value = min(w_length, (u16) sizeof cdev->desc);
980 memcpy(req->buf, &cdev->desc, value);
981 break;
982 case USB_DT_DEVICE_QUALIFIER:
David Brownac5d1542012-02-06 10:37:22 -0800983 if (!gadget_is_dualspeed(gadget))
David Brownell40982be2008-06-19 17:52:58 -0700984 break;
985 device_qual(cdev);
986 value = min_t(int, w_length,
987 sizeof(struct usb_qualifier_descriptor));
988 break;
989 case USB_DT_OTHER_SPEED_CONFIG:
David Brownac5d1542012-02-06 10:37:22 -0800990 if (!gadget_is_dualspeed(gadget))
David Brownell40982be2008-06-19 17:52:58 -0700991 break;
992 /* FALLTHROUGH */
993 case USB_DT_CONFIG:
994 value = config_desc(cdev, w_value);
995 if (value >= 0)
996 value = min(w_length, (u16) value);
997 break;
998 case USB_DT_STRING:
999 value = get_string(cdev, req->buf,
1000 w_index, w_value & 0xff);
1001 if (value >= 0)
1002 value = min(w_length, (u16) value);
1003 break;
1004 }
1005 break;
1006
1007 /* any number of configs can work */
1008 case USB_REQ_SET_CONFIGURATION:
1009 if (ctrl->bRequestType != 0)
1010 goto unknown;
1011 if (gadget_is_otg(gadget)) {
1012 if (gadget->a_hnp_support)
1013 DBG(cdev, "HNP available\n");
1014 else if (gadget->a_alt_hnp_support)
1015 DBG(cdev, "HNP on another port\n");
1016 else
1017 VDBG(cdev, "HNP inactive\n");
1018 }
1019 spin_lock(&cdev->lock);
1020 value = set_config(cdev, ctrl, w_value);
1021 spin_unlock(&cdev->lock);
1022 break;
1023 case USB_REQ_GET_CONFIGURATION:
1024 if (ctrl->bRequestType != USB_DIR_IN)
1025 goto unknown;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001026 if (cdev->config)
David Brownell40982be2008-06-19 17:52:58 -07001027 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001028 else
David Brownell40982be2008-06-19 17:52:58 -07001029 *(u8 *)req->buf = 0;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001030 value = min(w_length, (u16) 1);
David Brownell40982be2008-06-19 17:52:58 -07001031 break;
1032
1033 /* function drivers must handle get/set altsetting; if there's
1034 * no get() method, we know only altsetting zero works.
1035 */
1036 case USB_REQ_SET_INTERFACE:
1037 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1038 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001039 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001040 break;
Bryan Wu08889512009-01-08 00:21:19 +08001041 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001042 if (!f)
1043 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001044 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001045 break;
1046 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001047 if (value == USB_GADGET_DELAYED_STATUS) {
1048 DBG(cdev,
1049 "%s: interface %d (%s) requested delayed status\n",
1050 __func__, intf, f->name);
1051 cdev->delayed_status++;
1052 DBG(cdev, "delayed_status count %d\n",
1053 cdev->delayed_status);
1054 }
David Brownell40982be2008-06-19 17:52:58 -07001055 break;
1056 case USB_REQ_GET_INTERFACE:
1057 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1058 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001059 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001060 break;
Bryan Wu08889512009-01-08 00:21:19 +08001061 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001062 if (!f)
1063 break;
1064 /* lots of interfaces only need altsetting zero... */
1065 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1066 if (value < 0)
1067 break;
1068 *((u8 *)req->buf) = value;
1069 value = min(w_length, (u16) 1);
1070 break;
1071 default:
1072unknown:
1073 VDBG(cdev,
1074 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1075 ctrl->bRequestType, ctrl->bRequest,
1076 w_value, w_index, w_length);
1077
Laurent Pinchart52426582009-10-21 00:03:38 +02001078 /* functions always handle their interfaces and endpoints...
1079 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001080 * configuration code.
1081 *
1082 * REVISIT it could make sense to let the composite device
1083 * take such requests too, if that's ever needed: to work
1084 * in config 0, etc.
1085 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001086 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1087 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001088 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301089 break;
1090 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001091 break;
1092
1093 case USB_RECIP_ENDPOINT:
1094 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1095 list_for_each_entry(f, &cdev->config->functions, list) {
1096 if (test_bit(endp, f->endpoints))
1097 break;
1098 }
1099 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001100 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001101 break;
David Brownell40982be2008-06-19 17:52:58 -07001102 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001103
1104 if (f && f->setup)
1105 value = f->setup(f, ctrl);
1106 else {
David Brownell40982be2008-06-19 17:52:58 -07001107 struct usb_configuration *c;
1108
1109 c = cdev->config;
1110 if (c && c->setup)
1111 value = c->setup(c, ctrl);
1112 }
1113
1114 goto done;
1115 }
1116
1117 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001118 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001119 req->length = value;
1120 req->zero = value < w_length;
1121 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1122 if (value < 0) {
1123 DBG(cdev, "ep_queue --> %d\n", value);
1124 req->status = 0;
1125 composite_setup_complete(gadget->ep0, req);
1126 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001127 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1128 WARN(cdev,
1129 "%s: Delayed status not supported for w_length != 0",
1130 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001131 }
1132
1133done:
1134 /* device either stalls (value < 0) or reports success */
1135 return value;
1136}
1137
1138static void composite_disconnect(struct usb_gadget *gadget)
1139{
1140 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1141 unsigned long flags;
1142
1143 /* REVISIT: should we have config and device level
1144 * disconnect callbacks?
1145 */
1146 spin_lock_irqsave(&cdev->lock, flags);
1147 if (cdev->config)
1148 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001149 if (composite->disconnect)
1150 composite->disconnect(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001151 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001152}
1153
1154/*-------------------------------------------------------------------------*/
1155
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001156static ssize_t composite_show_suspended(struct device *dev,
1157 struct device_attribute *attr,
1158 char *buf)
1159{
1160 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1161 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1162
1163 return sprintf(buf, "%d\n", cdev->suspended);
1164}
1165
1166static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1167
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001168static void
David Brownell40982be2008-06-19 17:52:58 -07001169composite_unbind(struct usb_gadget *gadget)
1170{
1171 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1172
1173 /* composite_disconnect() must already have been called
1174 * by the underlying peripheral controller driver!
1175 * so there's no i/o concurrency that could affect the
1176 * state protected by cdev->lock.
1177 */
1178 WARN_ON(cdev->config);
1179
1180 while (!list_empty(&cdev->configs)) {
1181 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001182 c = list_first_entry(&cdev->configs,
1183 struct usb_configuration, list);
Benoit Goby94df1bd2011-05-25 23:59:43 -07001184 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001185 }
1186 if (composite->unbind)
1187 composite->unbind(cdev);
1188
1189 if (cdev->req) {
1190 kfree(cdev->req->buf);
1191 usb_ep_free_request(gadget->ep0, cdev->req);
1192 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301193 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001194 kfree(cdev);
1195 set_gadget_data(gadget, NULL);
1196 composite = NULL;
1197}
1198
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001199static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001200{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001201 if (!*desc) {
1202 int ret = usb_string_id(cdev);
1203 if (unlikely(ret < 0))
1204 WARNING(cdev, "failed to override string ID\n");
1205 else
1206 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001207 }
David Brownell40982be2008-06-19 17:52:58 -07001208
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001209 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001210}
1211
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001212static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001213{
1214 struct usb_composite_dev *cdev;
1215 int status = -ENOMEM;
1216
1217 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1218 if (!cdev)
1219 return status;
1220
1221 spin_lock_init(&cdev->lock);
1222 cdev->gadget = gadget;
1223 set_gadget_data(gadget, cdev);
1224 INIT_LIST_HEAD(&cdev->configs);
1225
1226 /* preallocate control response and buffer */
1227 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1228 if (!cdev->req)
1229 goto fail;
1230 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1231 if (!cdev->req->buf)
1232 goto fail;
1233 cdev->req->complete = composite_setup_complete;
1234 gadget->ep0->driver_data = cdev;
1235
1236 cdev->bufsiz = USB_BUFSIZ;
1237 cdev->driver = composite;
1238
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301239 /*
1240 * As per USB compliance update, a device that is actively drawing
1241 * more than 100mA from USB must report itself as bus-powered in
1242 * the GetStatus(DEVICE) call.
1243 */
1244 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1245 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001246
1247 /* interface and string IDs start at zero via kzalloc.
1248 * we force endpoints to start unassigned; few controller
1249 * drivers will zero ep->driver_data.
1250 */
1251 usb_ep_autoconfig_reset(cdev->gadget);
1252
1253 /* composite gadget needs to assign strings for whole device (like
1254 * serial number), register function drivers, potentially update
1255 * power state and consumption, etc
1256 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001257 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001258 if (status < 0)
1259 goto fail;
1260
1261 cdev->desc = *composite->dev;
David Brownac5d1542012-02-06 10:37:22 -08001262 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -07001263
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001264 /* standardized runtime overrides for device ID data */
1265 if (idVendor)
1266 cdev->desc.idVendor = cpu_to_le16(idVendor);
1267 if (idProduct)
1268 cdev->desc.idProduct = cpu_to_le16(idProduct);
1269 if (bcdDevice)
1270 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1271
Marek Belisko78bff3c2010-10-27 10:19:01 +02001272 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001273 if (iManufacturer || !cdev->desc.iManufacturer) {
1274 if (!iManufacturer && !composite->iManufacturer &&
1275 !*composite_manufacturer)
1276 snprintf(composite_manufacturer,
1277 sizeof composite_manufacturer,
1278 "%s %s with %s",
1279 init_utsname()->sysname,
1280 init_utsname()->release,
1281 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001282
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001283 cdev->manufacturer_override =
1284 override_id(cdev, &cdev->desc.iManufacturer);
1285 }
1286
1287 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1288 cdev->product_override =
1289 override_id(cdev, &cdev->desc.iProduct);
1290
1291 if (iSerialNumber)
1292 cdev->serial_override =
1293 override_id(cdev, &cdev->desc.iSerialNumber);
1294
1295 /* has userspace failed to provide a serial number? */
1296 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1297 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1298
1299 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001300 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1301 if (status)
1302 goto fail;
1303
David Brownell40982be2008-06-19 17:52:58 -07001304 INFO(cdev, "%s ready\n", composite->name);
1305 return 0;
1306
1307fail:
1308 composite_unbind(gadget);
1309 return status;
1310}
1311
1312/*-------------------------------------------------------------------------*/
1313
1314static void
1315composite_suspend(struct usb_gadget *gadget)
1316{
1317 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1318 struct usb_function *f;
1319
David Brownell89429392009-03-19 14:14:17 -07001320 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001321 * suspend/resume callbacks?
1322 */
1323 DBG(cdev, "suspend\n");
1324 if (cdev->config) {
1325 list_for_each_entry(f, &cdev->config->functions, list) {
1326 if (f->suspend)
1327 f->suspend(f);
1328 }
1329 }
David Brownell89429392009-03-19 14:14:17 -07001330 if (composite->suspend)
1331 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001332
1333 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001334
1335 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001336}
1337
1338static void
1339composite_resume(struct usb_gadget *gadget)
1340{
1341 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1342 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001343 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001344
David Brownell89429392009-03-19 14:14:17 -07001345 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001346 * suspend/resume callbacks?
1347 */
1348 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001349 if (composite->resume)
1350 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001351 if (cdev->config) {
1352 list_for_each_entry(f, &cdev->config->functions, list) {
1353 if (f->resume)
1354 f->resume(f);
1355 }
Hao Wub23f2f92010-11-29 15:17:03 +08001356
1357 maxpower = cdev->config->bMaxPower;
1358
1359 usb_gadget_vbus_draw(gadget, maxpower ?
1360 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001361 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001362
1363 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001364}
1365
1366/*-------------------------------------------------------------------------*/
1367
1368static struct usb_gadget_driver composite_driver = {
1369 .speed = USB_SPEED_HIGH,
1370
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001371 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001372
1373 .setup = composite_setup,
1374 .disconnect = composite_disconnect,
1375
1376 .suspend = composite_suspend,
1377 .resume = composite_resume,
1378
1379 .driver = {
1380 .owner = THIS_MODULE,
1381 },
1382};
1383
1384/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001385 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001386 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001387 * @bind: the callback used to allocate resources that are shared across the
1388 * whole device, such as string IDs, and add its configurations using
1389 * @usb_add_config(). This may fail by returning a negative errno
1390 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001391 * Context: single threaded during gadget setup
1392 *
1393 * This function is used to register drivers using the composite driver
1394 * framework. The return value is zero, or a negative errno value.
1395 * Those values normally come from the driver's @bind method, which does
1396 * all the work of setting up the driver to match the hardware.
1397 *
1398 * On successful return, the gadget is ready to respond to requests from
1399 * the host, unless one of its components invokes usb_gadget_disconnect()
1400 * while it was binding. That would usually be done in order to wait for
1401 * some userspace participation.
1402 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001403int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001404 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001405{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001406 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001407 return -EINVAL;
1408
1409 if (!driver->name)
1410 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001411 if (!driver->iProduct)
1412 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001413 composite_driver.function = (char *) driver->name;
1414 composite_driver.driver.name = driver->name;
1415 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001416 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001417
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001418 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001419}
1420
1421/**
1422 * usb_composite_unregister() - unregister a composite driver
1423 * @driver: the driver to unregister
1424 *
1425 * This function is used to unregister drivers using the composite
1426 * driver framework.
1427 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001428void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001429{
1430 if (composite != driver)
1431 return;
1432 usb_gadget_unregister_driver(&composite_driver);
1433}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001434
1435/**
1436 * usb_composite_setup_continue() - Continue with the control transfer
1437 * @cdev: the composite device who's control transfer was kept waiting
1438 *
1439 * This function must be called by the USB function driver to continue
1440 * with the control transfer's data/status stage in case it had requested to
1441 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1442 * can request the composite framework to delay the setup request's data/status
1443 * stages by returning USB_GADGET_DELAYED_STATUS.
1444 */
1445void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1446{
1447 int value;
1448 struct usb_request *req = cdev->req;
1449 unsigned long flags;
1450
1451 DBG(cdev, "%s\n", __func__);
1452 spin_lock_irqsave(&cdev->lock, flags);
1453
1454 if (cdev->delayed_status == 0) {
1455 WARN(cdev, "%s: Unexpected call\n", __func__);
1456
1457 } else if (--cdev->delayed_status == 0) {
1458 DBG(cdev, "%s: Completing delayed status\n", __func__);
1459 req->length = 0;
1460 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1461 if (value < 0) {
1462 DBG(cdev, "ep_queue --> %d\n", value);
1463 req->status = 0;
1464 composite_setup_complete(cdev->gadget->ep0, req);
1465 }
1466 }
1467
1468 spin_unlock_irqrestore(&cdev->lock, flags);
1469}
1470