blob: 9554d71b0887bb98e710335583d4083274b52846 [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) {
David Brownac5d1542012-02-06 10:37:22 -0800498 case USB_SPEED_LOW: speed = "low"; break;
499 case USB_SPEED_FULL: speed = "full"; break;
500 case USB_SPEED_HIGH: speed = "high"; break;
501 default: speed = "?"; break;
David Brownell40982be2008-06-19 17:52:58 -0700502 } ; speed; }), number, c ? c->label : "unconfigured");
503
504 if (!c)
505 goto done;
506
507 cdev->config = c;
508
509 /* Initialize all interfaces by setting them to altsetting zero. */
510 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
511 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200512 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700513
514 if (!f)
515 break;
516
Laurent Pinchart52426582009-10-21 00:03:38 +0200517 /*
518 * Record which endpoints are used by the function. This is used
519 * to dispatch control requests targeted at that endpoint to the
520 * function's setup callback instead of the current
521 * configuration's setup callback.
522 */
David Brownac5d1542012-02-06 10:37:22 -0800523 if (gadget->speed == USB_SPEED_HIGH)
Laurent Pinchart52426582009-10-21 00:03:38 +0200524 descriptors = f->hs_descriptors;
David Brownac5d1542012-02-06 10:37:22 -0800525 else
Laurent Pinchart52426582009-10-21 00:03:38 +0200526 descriptors = f->descriptors;
527
528 for (; *descriptors; ++descriptors) {
529 struct usb_endpoint_descriptor *ep;
530 int addr;
531
532 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
533 continue;
534
535 ep = (struct usb_endpoint_descriptor *)*descriptors;
536 addr = ((ep->bEndpointAddress & 0x80) >> 3)
537 | (ep->bEndpointAddress & 0x0f);
538 set_bit(addr, f->endpoints);
539 }
540
David Brownell40982be2008-06-19 17:52:58 -0700541 result = f->set_alt(f, tmp, 0);
542 if (result < 0) {
543 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
544 tmp, f->name, f, result);
545
546 reset_config(cdev);
547 goto done;
548 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300549
550 if (result == USB_GADGET_DELAYED_STATUS) {
551 DBG(cdev,
552 "%s: interface %d (%s) requested delayed status\n",
553 __func__, tmp, f->name);
554 cdev->delayed_status++;
555 DBG(cdev, "delayed_status count %d\n",
556 cdev->delayed_status);
557 }
David Brownell40982be2008-06-19 17:52:58 -0700558 }
559
560 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700561 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700562done:
563 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400564
Roger Quadros1b9ba002011-05-09 13:08:06 +0300565 if (result >= 0 && cdev->delayed_status)
566 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700567 return result;
568}
569
570/**
571 * usb_add_config() - add a configuration to a device.
572 * @cdev: wraps the USB gadget
573 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200574 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700575 * Context: single threaded during gadget setup
576 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200577 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700578 * add each of the configurations it supports, using this routine.
579 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200580 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700581 * is zero for success else a negative errno value. Binding configurations
582 * assigns global resources including string IDs, and per-configuration
583 * resources such as interface IDs and endpoints.
584 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200585int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200586 struct usb_configuration *config,
587 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700588{
589 int status = -EINVAL;
590 struct usb_configuration *c;
591
592 DBG(cdev, "adding config #%u '%s'/%p\n",
593 config->bConfigurationValue,
594 config->label, config);
595
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200596 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700597 goto done;
598
599 /* Prevent duplicate configuration identifiers */
600 list_for_each_entry(c, &cdev->configs, list) {
601 if (c->bConfigurationValue == config->bConfigurationValue) {
602 status = -EBUSY;
603 goto done;
604 }
605 }
606
607 config->cdev = cdev;
608 list_add_tail(&config->list, &cdev->configs);
609
610 INIT_LIST_HEAD(&config->functions);
611 config->next_interface_id = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700612 memset(config->interface, '\0', sizeof(config->interface));
David Brownell40982be2008-06-19 17:52:58 -0700613
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200614 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700615 if (status < 0) {
616 list_del(&config->list);
617 config->cdev = NULL;
618 } else {
619 unsigned i;
620
David Brownac5d1542012-02-06 10:37:22 -0800621 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700622 config->bConfigurationValue, config,
623 config->highspeed ? " high" : "",
624 config->fullspeed
625 ? (gadget_is_dualspeed(cdev->gadget)
626 ? " full"
627 : " full/low")
628 : "");
629
630 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
631 struct usb_function *f = config->interface[i];
632
633 if (!f)
634 continue;
635 DBG(cdev, " interface %d = %s/%p\n",
636 i, f->name, f);
637 }
638 }
639
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200640 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700641 * ep->driver_data as needed.
642 */
643 usb_ep_autoconfig_reset(cdev->gadget);
644
645done:
646 if (status)
647 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
648 config->bConfigurationValue, status);
649 return status;
650}
651
Benoit Goby94df1bd2011-05-25 23:59:43 -0700652static int remove_config(struct usb_composite_dev *cdev,
653 struct usb_configuration *config)
654{
655 while (!list_empty(&config->functions)) {
656 struct usb_function *f;
657
658 f = list_first_entry(&config->functions,
659 struct usb_function, list);
660 list_del(&f->list);
661 if (f->unbind) {
662 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
663 f->unbind(config, f);
664 /* may free memory for "f" */
665 }
666 }
667 list_del(&config->list);
668 if (config->unbind) {
669 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
670 config->unbind(config);
671 /* may free memory for "c" */
672 }
673 return 0;
674}
675
676int usb_remove_config(struct usb_composite_dev *cdev,
677 struct usb_configuration *config)
678{
679 unsigned long flags;
680
681 spin_lock_irqsave(&cdev->lock, flags);
682
683 if (cdev->config == config)
684 reset_config(cdev);
685
686 spin_unlock_irqrestore(&cdev->lock, flags);
687
688 return remove_config(cdev, config);
689}
690
David Brownell40982be2008-06-19 17:52:58 -0700691/*-------------------------------------------------------------------------*/
692
693/* We support strings in multiple languages ... string descriptor zero
694 * says which languages are supported. The typical case will be that
695 * only one language (probably English) is used, with I18N handled on
696 * the host side.
697 */
698
699static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
700{
701 const struct usb_gadget_strings *s;
702 u16 language;
703 __le16 *tmp;
704
705 while (*sp) {
706 s = *sp;
707 language = cpu_to_le16(s->language);
708 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
709 if (*tmp == language)
710 goto repeat;
711 }
712 *tmp++ = language;
713repeat:
714 sp++;
715 }
716}
717
718static int lookup_string(
719 struct usb_gadget_strings **sp,
720 void *buf,
721 u16 language,
722 int id
723)
724{
725 struct usb_gadget_strings *s;
726 int value;
727
728 while (*sp) {
729 s = *sp++;
730 if (s->language != language)
731 continue;
732 value = usb_gadget_get_string(s, id, buf);
733 if (value > 0)
734 return value;
735 }
736 return -EINVAL;
737}
738
739static int get_string(struct usb_composite_dev *cdev,
740 void *buf, u16 language, int id)
741{
742 struct usb_configuration *c;
743 struct usb_function *f;
744 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200745 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700746
747 /* Yes, not only is USB's I18N support probably more than most
748 * folk will ever care about ... also, it's all supported here.
749 * (Except for UTF8 support for Unicode's "Astral Planes".)
750 */
751
752 /* 0 == report all available language codes */
753 if (id == 0) {
754 struct usb_string_descriptor *s = buf;
755 struct usb_gadget_strings **sp;
756
757 memset(s, 0, 256);
758 s->bDescriptorType = USB_DT_STRING;
759
760 sp = composite->strings;
761 if (sp)
762 collect_langs(sp, s->wData);
763
764 list_for_each_entry(c, &cdev->configs, list) {
765 sp = c->strings;
766 if (sp)
767 collect_langs(sp, s->wData);
768
769 list_for_each_entry(f, &c->functions, list) {
770 sp = f->strings;
771 if (sp)
772 collect_langs(sp, s->wData);
773 }
774 }
775
Roel Kluin417b57b2009-08-06 16:09:51 -0700776 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700777 continue;
778 if (!len)
779 return -EINVAL;
780
781 s->bLength = 2 * (len + 1);
782 return s->bLength;
783 }
784
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200785 /* Otherwise, look up and return a specified string. First
786 * check if the string has not been overridden.
787 */
788 if (cdev->manufacturer_override == id)
789 str = iManufacturer ?: composite->iManufacturer ?:
790 composite_manufacturer;
791 else if (cdev->product_override == id)
792 str = iProduct ?: composite->iProduct;
793 else if (cdev->serial_override == id)
794 str = iSerialNumber;
795 else
796 str = NULL;
797 if (str) {
798 struct usb_gadget_strings strings = {
799 .language = language,
800 .strings = &(struct usb_string) { 0xff, str }
801 };
802 return usb_gadget_get_string(&strings, 0xff, buf);
803 }
804
805 /* String IDs are device-scoped, so we look up each string
806 * table we're told about. These lookups are infrequent;
807 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700808 */
809 if (composite->strings) {
810 len = lookup_string(composite->strings, buf, language, id);
811 if (len > 0)
812 return len;
813 }
814 list_for_each_entry(c, &cdev->configs, list) {
815 if (c->strings) {
816 len = lookup_string(c->strings, buf, language, id);
817 if (len > 0)
818 return len;
819 }
820 list_for_each_entry(f, &c->functions, list) {
821 if (!f->strings)
822 continue;
823 len = lookup_string(f->strings, buf, language, id);
824 if (len > 0)
825 return len;
826 }
827 }
828 return -EINVAL;
829}
830
831/**
832 * usb_string_id() - allocate an unused string ID
833 * @cdev: the device whose string descriptor IDs are being allocated
834 * Context: single threaded during gadget setup
835 *
836 * @usb_string_id() is called from bind() callbacks to allocate
837 * string IDs. Drivers for functions, configurations, or gadgets will
838 * then store that ID in the appropriate descriptors and string table.
839 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200840 * All string identifier should be allocated using this,
841 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
842 * that for example different functions don't wrongly assign different
843 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700844 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200845int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700846{
847 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200848 /* string id 0 is reserved by USB spec for list of
849 * supported languages */
850 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700851 cdev->next_string_id++;
852 return cdev->next_string_id;
853 }
854 return -ENODEV;
855}
856
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200857/**
858 * usb_string_ids() - allocate unused string IDs in batch
859 * @cdev: the device whose string descriptor IDs are being allocated
860 * @str: an array of usb_string objects to assign numbers to
861 * Context: single threaded during gadget setup
862 *
863 * @usb_string_ids() is called from bind() callbacks to allocate
864 * string IDs. Drivers for functions, configurations, or gadgets will
865 * then copy IDs from the string table to the appropriate descriptors
866 * and string table for other languages.
867 *
868 * All string identifier should be allocated using this,
869 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
870 * example different functions don't wrongly assign different meanings
871 * to the same identifier.
872 */
873int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
874{
875 int next = cdev->next_string_id;
876
877 for (; str->s; ++str) {
878 if (unlikely(next >= 254))
879 return -ENODEV;
880 str->id = ++next;
881 }
882
883 cdev->next_string_id = next;
884
885 return 0;
886}
887
888/**
889 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -0700890 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200891 * @n: number of string IDs to allocate
892 * Context: single threaded during gadget setup
893 *
894 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -0700895 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200896 * is, returns last requested ID which is now very useful information.
897 *
898 * @usb_string_ids_n() is called from bind() callbacks to allocate
899 * string IDs. Drivers for functions, configurations, or gadgets will
900 * then store that ID in the appropriate descriptors and string table.
901 *
902 * All string identifier should be allocated using this,
903 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
904 * example different functions don't wrongly assign different meanings
905 * to the same identifier.
906 */
907int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
908{
909 unsigned next = c->next_string_id;
910 if (unlikely(n > 254 || (unsigned)next + n > 254))
911 return -ENODEV;
912 c->next_string_id += n;
913 return next + 1;
914}
915
916
David Brownell40982be2008-06-19 17:52:58 -0700917/*-------------------------------------------------------------------------*/
918
919static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
920{
921 if (req->status || req->actual != req->length)
922 DBG((struct usb_composite_dev *) ep->driver_data,
923 "setup complete --> %d, %d/%d\n",
924 req->status, req->actual, req->length);
925}
926
927/*
928 * The setup() callback implements all the ep0 functionality that's
929 * not handled lower down, in hardware or the hardware driver(like
930 * device and endpoint feature flags, and their status). It's all
931 * housekeeping for the gadget function we're implementing. Most of
932 * the work is in config and function specific setup.
933 */
934static int
935composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
936{
937 struct usb_composite_dev *cdev = get_gadget_data(gadget);
938 struct usb_request *req = cdev->req;
939 int value = -EOPNOTSUPP;
940 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +0800941 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -0700942 u16 w_value = le16_to_cpu(ctrl->wValue);
943 u16 w_length = le16_to_cpu(ctrl->wLength);
944 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +0200945 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -0700946
Manu Gautam8d887a12011-10-12 17:13:52 +0530947
948 if (w_length > USB_BUFSIZ)
949 return value;
950
David Brownell40982be2008-06-19 17:52:58 -0700951 /* partial re-init of the response message; the function or the
952 * gadget might need to intercept e.g. a control-OUT completion
953 * when we delegate to it.
954 */
955 req->zero = 0;
956 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +0530957 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -0700958 gadget->ep0->driver_data = cdev;
959
960 switch (ctrl->bRequest) {
961
962 /* we handle all standard USB descriptors */
963 case USB_REQ_GET_DESCRIPTOR:
964 if (ctrl->bRequestType != USB_DIR_IN)
965 goto unknown;
966 switch (w_value >> 8) {
967
968 case USB_DT_DEVICE:
969 cdev->desc.bNumConfigurations =
970 count_configs(cdev, USB_DT_DEVICE);
971 value = min(w_length, (u16) sizeof cdev->desc);
972 memcpy(req->buf, &cdev->desc, value);
973 break;
974 case USB_DT_DEVICE_QUALIFIER:
David Brownac5d1542012-02-06 10:37:22 -0800975 if (!gadget_is_dualspeed(gadget))
David Brownell40982be2008-06-19 17:52:58 -0700976 break;
977 device_qual(cdev);
978 value = min_t(int, w_length,
979 sizeof(struct usb_qualifier_descriptor));
980 break;
981 case USB_DT_OTHER_SPEED_CONFIG:
David Brownac5d1542012-02-06 10:37:22 -0800982 if (!gadget_is_dualspeed(gadget))
David Brownell40982be2008-06-19 17:52:58 -0700983 break;
984 /* FALLTHROUGH */
985 case USB_DT_CONFIG:
986 value = config_desc(cdev, w_value);
987 if (value >= 0)
988 value = min(w_length, (u16) value);
989 break;
990 case USB_DT_STRING:
991 value = get_string(cdev, req->buf,
992 w_index, w_value & 0xff);
993 if (value >= 0)
994 value = min(w_length, (u16) value);
995 break;
996 }
997 break;
998
999 /* any number of configs can work */
1000 case USB_REQ_SET_CONFIGURATION:
1001 if (ctrl->bRequestType != 0)
1002 goto unknown;
1003 if (gadget_is_otg(gadget)) {
1004 if (gadget->a_hnp_support)
1005 DBG(cdev, "HNP available\n");
1006 else if (gadget->a_alt_hnp_support)
1007 DBG(cdev, "HNP on another port\n");
1008 else
1009 VDBG(cdev, "HNP inactive\n");
1010 }
1011 spin_lock(&cdev->lock);
1012 value = set_config(cdev, ctrl, w_value);
1013 spin_unlock(&cdev->lock);
1014 break;
1015 case USB_REQ_GET_CONFIGURATION:
1016 if (ctrl->bRequestType != USB_DIR_IN)
1017 goto unknown;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001018 if (cdev->config)
David Brownell40982be2008-06-19 17:52:58 -07001019 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001020 else
David Brownell40982be2008-06-19 17:52:58 -07001021 *(u8 *)req->buf = 0;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001022 value = min(w_length, (u16) 1);
David Brownell40982be2008-06-19 17:52:58 -07001023 break;
1024
1025 /* function drivers must handle get/set altsetting; if there's
1026 * no get() method, we know only altsetting zero works.
1027 */
1028 case USB_REQ_SET_INTERFACE:
1029 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1030 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001031 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001032 break;
Bryan Wu08889512009-01-08 00:21:19 +08001033 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001034 if (!f)
1035 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001036 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001037 break;
1038 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001039 if (value == USB_GADGET_DELAYED_STATUS) {
1040 DBG(cdev,
1041 "%s: interface %d (%s) requested delayed status\n",
1042 __func__, intf, f->name);
1043 cdev->delayed_status++;
1044 DBG(cdev, "delayed_status count %d\n",
1045 cdev->delayed_status);
1046 }
David Brownell40982be2008-06-19 17:52:58 -07001047 break;
1048 case USB_REQ_GET_INTERFACE:
1049 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1050 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001051 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001052 break;
Bryan Wu08889512009-01-08 00:21:19 +08001053 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001054 if (!f)
1055 break;
1056 /* lots of interfaces only need altsetting zero... */
1057 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1058 if (value < 0)
1059 break;
1060 *((u8 *)req->buf) = value;
1061 value = min(w_length, (u16) 1);
1062 break;
1063 default:
1064unknown:
1065 VDBG(cdev,
1066 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1067 ctrl->bRequestType, ctrl->bRequest,
1068 w_value, w_index, w_length);
1069
Laurent Pinchart52426582009-10-21 00:03:38 +02001070 /* functions always handle their interfaces and endpoints...
1071 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001072 * configuration code.
1073 *
1074 * REVISIT it could make sense to let the composite device
1075 * take such requests too, if that's ever needed: to work
1076 * in config 0, etc.
1077 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001078 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1079 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001080 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301081 break;
1082 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001083 break;
1084
1085 case USB_RECIP_ENDPOINT:
1086 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1087 list_for_each_entry(f, &cdev->config->functions, list) {
1088 if (test_bit(endp, f->endpoints))
1089 break;
1090 }
1091 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001092 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001093 break;
David Brownell40982be2008-06-19 17:52:58 -07001094 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001095
1096 if (f && f->setup)
1097 value = f->setup(f, ctrl);
1098 else {
David Brownell40982be2008-06-19 17:52:58 -07001099 struct usb_configuration *c;
1100
1101 c = cdev->config;
1102 if (c && c->setup)
1103 value = c->setup(c, ctrl);
1104 }
1105
1106 goto done;
1107 }
1108
1109 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001110 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001111 req->length = value;
1112 req->zero = value < w_length;
1113 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1114 if (value < 0) {
1115 DBG(cdev, "ep_queue --> %d\n", value);
1116 req->status = 0;
1117 composite_setup_complete(gadget->ep0, req);
1118 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001119 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1120 WARN(cdev,
1121 "%s: Delayed status not supported for w_length != 0",
1122 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001123 }
1124
1125done:
1126 /* device either stalls (value < 0) or reports success */
1127 return value;
1128}
1129
1130static void composite_disconnect(struct usb_gadget *gadget)
1131{
1132 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1133 unsigned long flags;
1134
1135 /* REVISIT: should we have config and device level
1136 * disconnect callbacks?
1137 */
1138 spin_lock_irqsave(&cdev->lock, flags);
1139 if (cdev->config)
1140 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001141 if (composite->disconnect)
1142 composite->disconnect(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001143 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001144}
1145
1146/*-------------------------------------------------------------------------*/
1147
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001148static ssize_t composite_show_suspended(struct device *dev,
1149 struct device_attribute *attr,
1150 char *buf)
1151{
1152 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1153 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1154
1155 return sprintf(buf, "%d\n", cdev->suspended);
1156}
1157
1158static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1159
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001160static void
David Brownell40982be2008-06-19 17:52:58 -07001161composite_unbind(struct usb_gadget *gadget)
1162{
1163 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1164
1165 /* composite_disconnect() must already have been called
1166 * by the underlying peripheral controller driver!
1167 * so there's no i/o concurrency that could affect the
1168 * state protected by cdev->lock.
1169 */
1170 WARN_ON(cdev->config);
1171
1172 while (!list_empty(&cdev->configs)) {
1173 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001174 c = list_first_entry(&cdev->configs,
1175 struct usb_configuration, list);
Benoit Goby94df1bd2011-05-25 23:59:43 -07001176 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001177 }
1178 if (composite->unbind)
1179 composite->unbind(cdev);
1180
1181 if (cdev->req) {
1182 kfree(cdev->req->buf);
1183 usb_ep_free_request(gadget->ep0, cdev->req);
1184 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301185 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001186 kfree(cdev);
1187 set_gadget_data(gadget, NULL);
1188 composite = NULL;
1189}
1190
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001191static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001192{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001193 if (!*desc) {
1194 int ret = usb_string_id(cdev);
1195 if (unlikely(ret < 0))
1196 WARNING(cdev, "failed to override string ID\n");
1197 else
1198 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001199 }
David Brownell40982be2008-06-19 17:52:58 -07001200
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001201 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001202}
1203
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001204static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001205{
1206 struct usb_composite_dev *cdev;
1207 int status = -ENOMEM;
1208
1209 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1210 if (!cdev)
1211 return status;
1212
1213 spin_lock_init(&cdev->lock);
1214 cdev->gadget = gadget;
1215 set_gadget_data(gadget, cdev);
1216 INIT_LIST_HEAD(&cdev->configs);
1217
1218 /* preallocate control response and buffer */
1219 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1220 if (!cdev->req)
1221 goto fail;
1222 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1223 if (!cdev->req->buf)
1224 goto fail;
1225 cdev->req->complete = composite_setup_complete;
1226 gadget->ep0->driver_data = cdev;
1227
1228 cdev->bufsiz = USB_BUFSIZ;
1229 cdev->driver = composite;
1230
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301231 /*
1232 * As per USB compliance update, a device that is actively drawing
1233 * more than 100mA from USB must report itself as bus-powered in
1234 * the GetStatus(DEVICE) call.
1235 */
1236 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1237 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001238
1239 /* interface and string IDs start at zero via kzalloc.
1240 * we force endpoints to start unassigned; few controller
1241 * drivers will zero ep->driver_data.
1242 */
1243 usb_ep_autoconfig_reset(cdev->gadget);
1244
1245 /* composite gadget needs to assign strings for whole device (like
1246 * serial number), register function drivers, potentially update
1247 * power state and consumption, etc
1248 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001249 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001250 if (status < 0)
1251 goto fail;
1252
1253 cdev->desc = *composite->dev;
David Brownac5d1542012-02-06 10:37:22 -08001254 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -07001255
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001256 /* standardized runtime overrides for device ID data */
1257 if (idVendor)
1258 cdev->desc.idVendor = cpu_to_le16(idVendor);
1259 if (idProduct)
1260 cdev->desc.idProduct = cpu_to_le16(idProduct);
1261 if (bcdDevice)
1262 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1263
Marek Belisko78bff3c2010-10-27 10:19:01 +02001264 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001265 if (iManufacturer || !cdev->desc.iManufacturer) {
1266 if (!iManufacturer && !composite->iManufacturer &&
1267 !*composite_manufacturer)
1268 snprintf(composite_manufacturer,
1269 sizeof composite_manufacturer,
1270 "%s %s with %s",
1271 init_utsname()->sysname,
1272 init_utsname()->release,
1273 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001274
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001275 cdev->manufacturer_override =
1276 override_id(cdev, &cdev->desc.iManufacturer);
1277 }
1278
1279 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1280 cdev->product_override =
1281 override_id(cdev, &cdev->desc.iProduct);
1282
1283 if (iSerialNumber)
1284 cdev->serial_override =
1285 override_id(cdev, &cdev->desc.iSerialNumber);
1286
1287 /* has userspace failed to provide a serial number? */
1288 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1289 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1290
1291 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001292 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1293 if (status)
1294 goto fail;
1295
David Brownell40982be2008-06-19 17:52:58 -07001296 INFO(cdev, "%s ready\n", composite->name);
1297 return 0;
1298
1299fail:
1300 composite_unbind(gadget);
1301 return status;
1302}
1303
1304/*-------------------------------------------------------------------------*/
1305
1306static void
1307composite_suspend(struct usb_gadget *gadget)
1308{
1309 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1310 struct usb_function *f;
1311
David Brownell89429392009-03-19 14:14:17 -07001312 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001313 * suspend/resume callbacks?
1314 */
1315 DBG(cdev, "suspend\n");
1316 if (cdev->config) {
1317 list_for_each_entry(f, &cdev->config->functions, list) {
1318 if (f->suspend)
1319 f->suspend(f);
1320 }
1321 }
David Brownell89429392009-03-19 14:14:17 -07001322 if (composite->suspend)
1323 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001324
1325 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001326
1327 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001328}
1329
1330static void
1331composite_resume(struct usb_gadget *gadget)
1332{
1333 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1334 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001335 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001336
David Brownell89429392009-03-19 14:14:17 -07001337 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001338 * suspend/resume callbacks?
1339 */
1340 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001341 if (composite->resume)
1342 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001343 if (cdev->config) {
1344 list_for_each_entry(f, &cdev->config->functions, list) {
1345 if (f->resume)
1346 f->resume(f);
1347 }
Hao Wub23f2f92010-11-29 15:17:03 +08001348
1349 maxpower = cdev->config->bMaxPower;
1350
1351 usb_gadget_vbus_draw(gadget, maxpower ?
1352 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001353 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001354
1355 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001356}
1357
1358/*-------------------------------------------------------------------------*/
1359
1360static struct usb_gadget_driver composite_driver = {
1361 .speed = USB_SPEED_HIGH,
1362
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001363 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001364
1365 .setup = composite_setup,
1366 .disconnect = composite_disconnect,
1367
1368 .suspend = composite_suspend,
1369 .resume = composite_resume,
1370
1371 .driver = {
1372 .owner = THIS_MODULE,
1373 },
1374};
1375
1376/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001377 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001378 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001379 * @bind: the callback used to allocate resources that are shared across the
1380 * whole device, such as string IDs, and add its configurations using
1381 * @usb_add_config(). This may fail by returning a negative errno
1382 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001383 * Context: single threaded during gadget setup
1384 *
1385 * This function is used to register drivers using the composite driver
1386 * framework. The return value is zero, or a negative errno value.
1387 * Those values normally come from the driver's @bind method, which does
1388 * all the work of setting up the driver to match the hardware.
1389 *
1390 * On successful return, the gadget is ready to respond to requests from
1391 * the host, unless one of its components invokes usb_gadget_disconnect()
1392 * while it was binding. That would usually be done in order to wait for
1393 * some userspace participation.
1394 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001395int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001396 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001397{
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001398 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001399 return -EINVAL;
1400
1401 if (!driver->name)
1402 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001403 if (!driver->iProduct)
1404 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001405 composite_driver.function = (char *) driver->name;
1406 composite_driver.driver.name = driver->name;
1407 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001408 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001409
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001410 return usb_gadget_probe_driver(&composite_driver, composite_bind);
David Brownell40982be2008-06-19 17:52:58 -07001411}
1412
1413/**
1414 * usb_composite_unregister() - unregister a composite driver
1415 * @driver: the driver to unregister
1416 *
1417 * This function is used to unregister drivers using the composite
1418 * driver framework.
1419 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001420void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001421{
1422 if (composite != driver)
1423 return;
1424 usb_gadget_unregister_driver(&composite_driver);
1425}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001426
1427/**
1428 * usb_composite_setup_continue() - Continue with the control transfer
1429 * @cdev: the composite device who's control transfer was kept waiting
1430 *
1431 * This function must be called by the USB function driver to continue
1432 * with the control transfer's data/status stage in case it had requested to
1433 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1434 * can request the composite framework to delay the setup request's data/status
1435 * stages by returning USB_GADGET_DELAYED_STATUS.
1436 */
1437void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1438{
1439 int value;
1440 struct usb_request *req = cdev->req;
1441 unsigned long flags;
1442
1443 DBG(cdev, "%s\n", __func__);
1444 spin_lock_irqsave(&cdev->lock, flags);
1445
1446 if (cdev->delayed_status == 0) {
1447 WARN(cdev, "%s: Unexpected call\n", __func__);
1448
1449 } else if (--cdev->delayed_status == 0) {
1450 DBG(cdev, "%s: Completing delayed status\n", __func__);
1451 req->length = 0;
1452 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1453 if (value < 0) {
1454 DBG(cdev, "ep_queue --> %d\n", value);
1455 req->status = 0;
1456 composite_setup_complete(cdev->gadget->ep0, req);
1457 }
1458 }
1459
1460 spin_unlock_irqrestore(&cdev->lock, flags);
1461}
1462