blob: 4f2946cc637e2e49e5e2894e73dbc37e3ed3b876 [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>
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +030030#include <asm/unaligned.h>
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
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300131 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
132 int want_comp_desc = 0;
133
Tatyana Brokhmancf64ce42011-06-28 16:33:49 +0300134 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
135
136 if (!g || !f || !_ep)
137 return -EIO;
138
139 /* select desired speed */
140 switch (g->speed) {
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300141 case USB_SPEED_SUPER:
142 if (gadget_is_superspeed(g)) {
143 speed_desc = f->ss_descriptors;
144 want_comp_desc = 1;
145 break;
146 }
147 /* else: Fall trough */
Tatyana Brokhmancf64ce42011-06-28 16:33:49 +0300148 case USB_SPEED_HIGH:
149 if (gadget_is_dualspeed(g)) {
150 speed_desc = f->hs_descriptors;
151 break;
152 }
153 /* else: fall through */
154 default:
155 speed_desc = f->descriptors;
156 }
157 /* find descriptors */
158 for_each_ep_desc(speed_desc, d_spd) {
159 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
160 if (chosen_desc->bEndpointAddress == _ep->address)
161 goto ep_found;
162 }
163 return -EIO;
164
165ep_found:
166 /* commit results */
167 _ep->maxpacket = le16_to_cpu(chosen_desc->wMaxPacketSize);
168 _ep->desc = chosen_desc;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300169 _ep->comp_desc = NULL;
170 _ep->maxburst = 0;
171 _ep->mult = 0;
172 if (!want_comp_desc)
173 return 0;
Tatyana Brokhmancf64ce42011-06-28 16:33:49 +0300174
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300175 /*
176 * Companion descriptor should follow EP descriptor
177 * USB 3.0 spec, #9.6.7
178 */
179 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
180 if (!comp_desc ||
181 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
182 return -EIO;
183 _ep->comp_desc = comp_desc;
184 if (g->speed == USB_SPEED_SUPER) {
185 switch (usb_endpoint_type(_ep->desc)) {
186 case USB_ENDPOINT_XFER_BULK:
187 case USB_ENDPOINT_XFER_INT:
188 _ep->maxburst = comp_desc->bMaxBurst;
189 break;
190 case USB_ENDPOINT_XFER_ISOC:
191 /* mult: bits 1:0 of bmAttributes */
192 _ep->mult = comp_desc->bmAttributes & 0x3;
193 break;
194 default:
195 /* Do nothing for control endpoints */
196 break;
197 }
198 }
Tatyana Brokhmancf64ce42011-06-28 16:33:49 +0300199 return 0;
200}
David Brownell40982be2008-06-19 17:52:58 -0700201
202/**
203 * usb_add_function() - add a function to a configuration
204 * @config: the configuration
205 * @function: the function being added
206 * Context: single threaded during gadget setup
207 *
208 * After initialization, each configuration must have one or more
209 * functions added to it. Adding a function involves calling its @bind()
210 * method to allocate resources such as interface and string identifiers
211 * and endpoints.
212 *
213 * This function returns the value of the function's bind(), which is
214 * zero for success else a negative errno value.
215 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200216int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700217 struct usb_function *function)
218{
219 int value = -EINVAL;
220
Benoit Gobyaab96812011-04-19 20:37:33 -0700221 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
David Brownell40982be2008-06-19 17:52:58 -0700222 function->name, function,
223 config->label, config);
224
225 if (!function->set_alt || !function->disable)
226 goto done;
227
228 function->config = config;
229 list_add_tail(&function->list, &config->functions);
230
231 /* REVISIT *require* function->bind? */
232 if (function->bind) {
233 value = function->bind(config, function);
234 if (value < 0) {
235 list_del(&function->list);
236 function->config = NULL;
237 }
238 } else
239 value = 0;
240
241 /* We allow configurations that don't work at both speeds.
242 * If we run into a lowspeed Linux system, treat it the same
243 * as full speed ... it's the function drivers that will need
244 * to avoid bulk and ISO transfers.
245 */
246 if (!config->fullspeed && function->descriptors)
247 config->fullspeed = true;
248 if (!config->highspeed && function->hs_descriptors)
249 config->highspeed = true;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300250 if (!config->superspeed && function->ss_descriptors)
251 config->superspeed = true;
David Brownell40982be2008-06-19 17:52:58 -0700252
253done:
254 if (value)
Benoit Gobyaab96812011-04-19 20:37:33 -0700255 DBG(config->cdev, "adding '%s'/%p --> %d\n",
David Brownell40982be2008-06-19 17:52:58 -0700256 function->name, function, value);
257 return value;
258}
259
260/**
David Brownell60beed92008-08-18 17:38:22 -0700261 * usb_function_deactivate - prevent function and gadget enumeration
262 * @function: the function that isn't yet ready to respond
263 *
264 * Blocks response of the gadget driver to host enumeration by
265 * preventing the data line pullup from being activated. This is
266 * normally called during @bind() processing to change from the
267 * initial "ready to respond" state, or when a required resource
268 * becomes available.
269 *
270 * For example, drivers that serve as a passthrough to a userspace
271 * daemon can block enumeration unless that daemon (such as an OBEX,
272 * MTP, or print server) is ready to handle host requests.
273 *
274 * Not all systems support software control of their USB peripheral
275 * data pullups.
276 *
277 * Returns zero on success, else negative errno.
278 */
279int usb_function_deactivate(struct usb_function *function)
280{
281 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200282 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700283 int status = 0;
284
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200285 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700286
287 if (cdev->deactivations == 0)
288 status = usb_gadget_disconnect(cdev->gadget);
289 if (status == 0)
290 cdev->deactivations++;
291
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200292 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700293 return status;
294}
295
296/**
297 * usb_function_activate - allow function and gadget enumeration
298 * @function: function on which usb_function_activate() was called
299 *
300 * Reverses effect of usb_function_deactivate(). If no more functions
301 * are delaying their activation, the gadget driver will respond to
302 * host enumeration procedures.
303 *
304 * Returns zero on success, else negative errno.
305 */
306int usb_function_activate(struct usb_function *function)
307{
308 struct usb_composite_dev *cdev = function->config->cdev;
309 int status = 0;
310
311 spin_lock(&cdev->lock);
312
313 if (WARN_ON(cdev->deactivations == 0))
314 status = -EINVAL;
315 else {
316 cdev->deactivations--;
317 if (cdev->deactivations == 0)
318 status = usb_gadget_connect(cdev->gadget);
319 }
320
321 spin_unlock(&cdev->lock);
322 return status;
323}
324
325/**
David Brownell40982be2008-06-19 17:52:58 -0700326 * usb_interface_id() - allocate an unused interface ID
327 * @config: configuration associated with the interface
328 * @function: function handling the interface
329 * Context: single threaded during gadget setup
330 *
331 * usb_interface_id() is called from usb_function.bind() callbacks to
332 * allocate new interface IDs. The function driver will then store that
333 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300334 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700335 * particularly changing its altsetting via set_alt(). There may
336 * also be class-specific or vendor-specific requests to handle.
337 *
338 * All interface identifier should be allocated using this routine, to
339 * ensure that for example different functions don't wrongly assign
340 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300341 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700342 * one configuration (or more than once in a given configuration) need
343 * multiple versions of the relevant descriptors.
344 *
345 * Returns the interface ID which was allocated; or -ENODEV if no
346 * more interface IDs can be allocated.
347 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200348int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700349 struct usb_function *function)
350{
351 unsigned id = config->next_interface_id;
352
353 if (id < MAX_CONFIG_INTERFACES) {
354 config->interface[id] = function;
355 config->next_interface_id = id + 1;
356 return id;
357 }
358 return -ENODEV;
359}
360
361static int config_buf(struct usb_configuration *config,
362 enum usb_device_speed speed, void *buf, u8 type)
363{
364 struct usb_config_descriptor *c = buf;
365 void *next = buf + USB_DT_CONFIG_SIZE;
366 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
367 struct usb_function *f;
368 int status;
369
370 /* write the config descriptor */
371 c = buf;
372 c->bLength = USB_DT_CONFIG_SIZE;
373 c->bDescriptorType = type;
Benoit Gobyaab96812011-04-19 20:37:33 -0700374 /* wTotalLength is written later */
375 c->bNumInterfaces = config->next_interface_id;
David Brownell40982be2008-06-19 17:52:58 -0700376 c->bConfigurationValue = config->bConfigurationValue;
377 c->iConfiguration = config->iConfiguration;
378 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700379 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700380
381 /* There may be e.g. OTG descriptors */
382 if (config->descriptors) {
383 status = usb_descriptor_fillbuf(next, len,
384 config->descriptors);
385 if (status < 0)
386 return status;
387 len -= status;
388 next += status;
389 }
390
391 /* add each function's descriptors */
392 list_for_each_entry(f, &config->functions, list) {
393 struct usb_descriptor_header **descriptors;
394
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300395 switch (speed) {
396 case USB_SPEED_SUPER:
397 descriptors = f->ss_descriptors;
398 break;
399 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700400 descriptors = f->hs_descriptors;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300401 break;
402 default:
David Brownell40982be2008-06-19 17:52:58 -0700403 descriptors = f->descriptors;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300404 }
405
Benoit Gobyaab96812011-04-19 20:37:33 -0700406 if (!descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700407 continue;
408 status = usb_descriptor_fillbuf(next, len,
409 (const struct usb_descriptor_header **) descriptors);
410 if (status < 0)
411 return status;
412 len -= status;
413 next += status;
414 }
415
416 len = next - buf;
417 c->wTotalLength = cpu_to_le16(len);
418 return len;
419}
420
421static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
422{
423 struct usb_gadget *gadget = cdev->gadget;
424 struct usb_configuration *c;
425 u8 type = w_value >> 8;
426 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
427
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300428 if (gadget->speed == USB_SPEED_SUPER)
429 speed = gadget->speed;
430 else if (gadget_is_dualspeed(gadget)) {
431 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700432 if (gadget->speed == USB_SPEED_HIGH)
433 hs = 1;
434 if (type == USB_DT_OTHER_SPEED_CONFIG)
435 hs = !hs;
436 if (hs)
437 speed = USB_SPEED_HIGH;
438
439 }
440
441 /* This is a lookup by config *INDEX* */
442 w_value &= 0xff;
443 list_for_each_entry(c, &cdev->configs, list) {
444 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300445 switch (speed) {
446 case USB_SPEED_SUPER:
447 if (!c->superspeed)
448 continue;
449 break;
450 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700451 if (!c->highspeed)
452 continue;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300453 break;
454 default:
David Brownell40982be2008-06-19 17:52:58 -0700455 if (!c->fullspeed)
456 continue;
457 }
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300458
David Brownell40982be2008-06-19 17:52:58 -0700459 if (w_value == 0)
460 return config_buf(c, speed, cdev->req->buf, type);
461 w_value--;
462 }
463 return -EINVAL;
464}
465
466static int count_configs(struct usb_composite_dev *cdev, unsigned type)
467{
468 struct usb_gadget *gadget = cdev->gadget;
469 struct usb_configuration *c;
470 unsigned count = 0;
471 int hs = 0;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300472 int ss = 0;
David Brownell40982be2008-06-19 17:52:58 -0700473
474 if (gadget_is_dualspeed(gadget)) {
475 if (gadget->speed == USB_SPEED_HIGH)
476 hs = 1;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300477 if (gadget->speed == USB_SPEED_SUPER)
478 ss = 1;
David Brownell40982be2008-06-19 17:52:58 -0700479 if (type == USB_DT_DEVICE_QUALIFIER)
480 hs = !hs;
481 }
482 list_for_each_entry(c, &cdev->configs, list) {
483 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300484 if (ss) {
485 if (!c->superspeed)
486 continue;
487 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700488 if (!c->highspeed)
489 continue;
490 } else {
491 if (!c->fullspeed)
492 continue;
493 }
494 count++;
495 }
496 return count;
497}
498
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300499/**
500 * bos_desc() - prepares the BOS descriptor.
501 * @cdev: pointer to usb_composite device to generate the bos
502 * descriptor for
503 *
504 * This function generates the BOS (Binary Device Object)
505 * descriptor and its device capabilities descriptors. The BOS
506 * descriptor should be supported by a SuperSpeed device.
507 */
508static int bos_desc(struct usb_composite_dev *cdev)
509{
510 struct usb_ext_cap_descriptor *usb_ext;
511 struct usb_ss_cap_descriptor *ss_cap;
512 struct usb_dcd_config_params dcd_config_params;
513 struct usb_bos_descriptor *bos = cdev->req->buf;
514
515 bos->bLength = USB_DT_BOS_SIZE;
516 bos->bDescriptorType = USB_DT_BOS;
517
518 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
519 bos->bNumDeviceCaps = 0;
520
521 /*
522 * A SuperSpeed device shall include the USB2.0 extension descriptor
523 * and shall support LPM when operating in USB2.0 HS mode.
524 */
525 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
526 bos->bNumDeviceCaps++;
527 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
528 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
529 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
530 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
531 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
532
533 /*
534 * The Superspeed USB Capability descriptor shall be implemented by all
535 * SuperSpeed devices.
536 */
537 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
538 bos->bNumDeviceCaps++;
539 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
540 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
541 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
542 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
543 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
544 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
545 USB_FULL_SPEED_OPERATION |
546 USB_HIGH_SPEED_OPERATION |
547 USB_5GBPS_OPERATION);
548 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
549
550 /* Get Controller configuration */
551 if (cdev->gadget->ops->get_config_params)
552 cdev->gadget->ops->get_config_params(&dcd_config_params);
553 else {
Felipe Balbib0c3e7f2011-10-10 09:43:44 +0300554 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300555 dcd_config_params.bU2DevExitLat =
Felipe Balbib0c3e7f2011-10-10 09:43:44 +0300556 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300557 }
558 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
559 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
560
561 return le16_to_cpu(bos->wTotalLength);
562}
563
David Brownell40982be2008-06-19 17:52:58 -0700564static void device_qual(struct usb_composite_dev *cdev)
565{
566 struct usb_qualifier_descriptor *qual = cdev->req->buf;
567
568 qual->bLength = sizeof(*qual);
569 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
570 /* POLICY: same bcdUSB and device type info at both speeds */
571 qual->bcdUSB = cdev->desc.bcdUSB;
572 qual->bDeviceClass = cdev->desc.bDeviceClass;
573 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
574 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
575 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior0d0c2402011-06-23 14:26:11 +0200576 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700577 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700578 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700579}
580
581/*-------------------------------------------------------------------------*/
582
583static void reset_config(struct usb_composite_dev *cdev)
584{
585 struct usb_function *f;
586
587 DBG(cdev, "reset config\n");
588
589 list_for_each_entry(f, &cdev->config->functions, list) {
590 if (f->disable)
591 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200592
593 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700594 }
595 cdev->config = NULL;
596}
597
598static int set_config(struct usb_composite_dev *cdev,
599 const struct usb_ctrlrequest *ctrl, unsigned number)
600{
601 struct usb_gadget *gadget = cdev->gadget;
602 struct usb_configuration *c = NULL;
603 int result = -EINVAL;
604 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
605 int tmp;
606
David Brownell40982be2008-06-19 17:52:58 -0700607 if (number) {
608 list_for_each_entry(c, &cdev->configs, list) {
609 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300610 /*
611 * We disable the FDs of the previous
612 * configuration only if the new configuration
613 * is a valid one
614 */
615 if (cdev->config)
616 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700617 result = 0;
618 break;
619 }
620 }
621 if (result < 0)
622 goto done;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300623 } else { /* Zero configuration value - need to reset the config */
624 if (cdev->config)
625 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700626 result = 0;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300627 }
David Brownell40982be2008-06-19 17:52:58 -0700628
629 INFO(cdev, "%s speed config #%d: %s\n",
630 ({ char *speed;
631 switch (gadget->speed) {
Tatyana Brokhman5ef53d92011-06-28 16:33:52 +0300632 case USB_SPEED_LOW:
633 speed = "low";
634 break;
635 case USB_SPEED_FULL:
636 speed = "full";
637 break;
638 case USB_SPEED_HIGH:
639 speed = "high";
640 break;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300641 case USB_SPEED_SUPER:
642 speed = "super";
643 break;
Tatyana Brokhman5ef53d92011-06-28 16:33:52 +0300644 default:
645 speed = "?";
646 break;
David Brownell40982be2008-06-19 17:52:58 -0700647 } ; speed; }), number, c ? c->label : "unconfigured");
648
649 if (!c)
650 goto done;
651
652 cdev->config = c;
653
654 /* Initialize all interfaces by setting them to altsetting zero. */
655 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
656 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200657 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700658
659 if (!f)
660 break;
661
Laurent Pinchart52426582009-10-21 00:03:38 +0200662 /*
663 * Record which endpoints are used by the function. This is used
664 * to dispatch control requests targeted at that endpoint to the
665 * function's setup callback instead of the current
666 * configuration's setup callback.
667 */
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300668 switch (gadget->speed) {
669 case USB_SPEED_SUPER:
670 descriptors = f->ss_descriptors;
671 break;
672 case USB_SPEED_HIGH:
Laurent Pinchart52426582009-10-21 00:03:38 +0200673 descriptors = f->hs_descriptors;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300674 break;
675 default:
Laurent Pinchart52426582009-10-21 00:03:38 +0200676 descriptors = f->descriptors;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300677 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200678
679 for (; *descriptors; ++descriptors) {
680 struct usb_endpoint_descriptor *ep;
681 int addr;
682
683 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
684 continue;
685
686 ep = (struct usb_endpoint_descriptor *)*descriptors;
687 addr = ((ep->bEndpointAddress & 0x80) >> 3)
688 | (ep->bEndpointAddress & 0x0f);
689 set_bit(addr, f->endpoints);
690 }
691
David Brownell40982be2008-06-19 17:52:58 -0700692 result = f->set_alt(f, tmp, 0);
693 if (result < 0) {
694 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
695 tmp, f->name, f, result);
696
697 reset_config(cdev);
698 goto done;
699 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300700
701 if (result == USB_GADGET_DELAYED_STATUS) {
702 DBG(cdev,
703 "%s: interface %d (%s) requested delayed status\n",
704 __func__, tmp, f->name);
705 cdev->delayed_status++;
706 DBG(cdev, "delayed_status count %d\n",
707 cdev->delayed_status);
708 }
David Brownell40982be2008-06-19 17:52:58 -0700709 }
710
711 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700712 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700713done:
714 usb_gadget_vbus_draw(gadget, power);
Mike Lockwoode2dc5032010-06-23 08:20:59 -0400715
Roger Quadros1b9ba002011-05-09 13:08:06 +0300716 if (result >= 0 && cdev->delayed_status)
717 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700718 return result;
719}
720
721/**
722 * usb_add_config() - add a configuration to a device.
723 * @cdev: wraps the USB gadget
724 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200725 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700726 * Context: single threaded during gadget setup
727 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200728 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700729 * add each of the configurations it supports, using this routine.
730 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200731 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700732 * is zero for success else a negative errno value. Binding configurations
733 * assigns global resources including string IDs, and per-configuration
734 * resources such as interface IDs and endpoints.
735 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200736int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200737 struct usb_configuration *config,
738 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700739{
740 int status = -EINVAL;
741 struct usb_configuration *c;
742
743 DBG(cdev, "adding config #%u '%s'/%p\n",
744 config->bConfigurationValue,
745 config->label, config);
746
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200747 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700748 goto done;
749
750 /* Prevent duplicate configuration identifiers */
751 list_for_each_entry(c, &cdev->configs, list) {
752 if (c->bConfigurationValue == config->bConfigurationValue) {
753 status = -EBUSY;
754 goto done;
755 }
756 }
757
758 config->cdev = cdev;
759 list_add_tail(&config->list, &cdev->configs);
760
761 INIT_LIST_HEAD(&config->functions);
762 config->next_interface_id = 0;
Benoit Gobyaab96812011-04-19 20:37:33 -0700763 memset(config->interface, '\0', sizeof(config->interface));
David Brownell40982be2008-06-19 17:52:58 -0700764
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200765 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700766 if (status < 0) {
767 list_del(&config->list);
768 config->cdev = NULL;
769 } else {
770 unsigned i;
771
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300772 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700773 config->bConfigurationValue, config,
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +0300774 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700775 config->highspeed ? " high" : "",
776 config->fullspeed
777 ? (gadget_is_dualspeed(cdev->gadget)
778 ? " full"
779 : " full/low")
780 : "");
781
782 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
783 struct usb_function *f = config->interface[i];
784
785 if (!f)
786 continue;
787 DBG(cdev, " interface %d = %s/%p\n",
788 i, f->name, f);
789 }
790 }
791
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200792 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700793 * ep->driver_data as needed.
794 */
795 usb_ep_autoconfig_reset(cdev->gadget);
796
797done:
798 if (status)
799 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
800 config->bConfigurationValue, status);
801 return status;
802}
803
Benoit Goby94df1bd2011-05-25 23:59:43 -0700804static int remove_config(struct usb_composite_dev *cdev,
805 struct usb_configuration *config)
806{
807 while (!list_empty(&config->functions)) {
808 struct usb_function *f;
809
810 f = list_first_entry(&config->functions,
811 struct usb_function, list);
812 list_del(&f->list);
813 if (f->unbind) {
814 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
815 f->unbind(config, f);
816 /* may free memory for "f" */
817 }
818 }
819 list_del(&config->list);
820 if (config->unbind) {
821 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
822 config->unbind(config);
823 /* may free memory for "c" */
824 }
825 return 0;
826}
827
828int usb_remove_config(struct usb_composite_dev *cdev,
829 struct usb_configuration *config)
830{
831 unsigned long flags;
832
833 spin_lock_irqsave(&cdev->lock, flags);
834
835 if (cdev->config == config)
836 reset_config(cdev);
837
838 spin_unlock_irqrestore(&cdev->lock, flags);
839
840 return remove_config(cdev, config);
841}
842
David Brownell40982be2008-06-19 17:52:58 -0700843/*-------------------------------------------------------------------------*/
844
845/* We support strings in multiple languages ... string descriptor zero
846 * says which languages are supported. The typical case will be that
847 * only one language (probably English) is used, with I18N handled on
848 * the host side.
849 */
850
851static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
852{
853 const struct usb_gadget_strings *s;
854 u16 language;
855 __le16 *tmp;
856
857 while (*sp) {
858 s = *sp;
859 language = cpu_to_le16(s->language);
860 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
861 if (*tmp == language)
862 goto repeat;
863 }
864 *tmp++ = language;
865repeat:
866 sp++;
867 }
868}
869
870static int lookup_string(
871 struct usb_gadget_strings **sp,
872 void *buf,
873 u16 language,
874 int id
875)
876{
877 struct usb_gadget_strings *s;
878 int value;
879
880 while (*sp) {
881 s = *sp++;
882 if (s->language != language)
883 continue;
884 value = usb_gadget_get_string(s, id, buf);
885 if (value > 0)
886 return value;
887 }
888 return -EINVAL;
889}
890
891static int get_string(struct usb_composite_dev *cdev,
892 void *buf, u16 language, int id)
893{
894 struct usb_configuration *c;
895 struct usb_function *f;
896 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200897 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700898
899 /* Yes, not only is USB's I18N support probably more than most
900 * folk will ever care about ... also, it's all supported here.
901 * (Except for UTF8 support for Unicode's "Astral Planes".)
902 */
903
904 /* 0 == report all available language codes */
905 if (id == 0) {
906 struct usb_string_descriptor *s = buf;
907 struct usb_gadget_strings **sp;
908
909 memset(s, 0, 256);
910 s->bDescriptorType = USB_DT_STRING;
911
912 sp = composite->strings;
913 if (sp)
914 collect_langs(sp, s->wData);
915
916 list_for_each_entry(c, &cdev->configs, list) {
917 sp = c->strings;
918 if (sp)
919 collect_langs(sp, s->wData);
920
921 list_for_each_entry(f, &c->functions, list) {
922 sp = f->strings;
923 if (sp)
924 collect_langs(sp, s->wData);
925 }
926 }
927
Roel Kluin417b57b2009-08-06 16:09:51 -0700928 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700929 continue;
930 if (!len)
931 return -EINVAL;
932
933 s->bLength = 2 * (len + 1);
934 return s->bLength;
935 }
936
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200937 /* Otherwise, look up and return a specified string. First
938 * check if the string has not been overridden.
939 */
940 if (cdev->manufacturer_override == id)
941 str = iManufacturer ?: composite->iManufacturer ?:
942 composite_manufacturer;
943 else if (cdev->product_override == id)
944 str = iProduct ?: composite->iProduct;
945 else if (cdev->serial_override == id)
946 str = iSerialNumber;
947 else
948 str = NULL;
949 if (str) {
950 struct usb_gadget_strings strings = {
951 .language = language,
952 .strings = &(struct usb_string) { 0xff, str }
953 };
954 return usb_gadget_get_string(&strings, 0xff, buf);
955 }
956
957 /* String IDs are device-scoped, so we look up each string
958 * table we're told about. These lookups are infrequent;
959 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700960 */
961 if (composite->strings) {
962 len = lookup_string(composite->strings, buf, language, id);
963 if (len > 0)
964 return len;
965 }
966 list_for_each_entry(c, &cdev->configs, list) {
967 if (c->strings) {
968 len = lookup_string(c->strings, buf, language, id);
969 if (len > 0)
970 return len;
971 }
972 list_for_each_entry(f, &c->functions, list) {
973 if (!f->strings)
974 continue;
975 len = lookup_string(f->strings, buf, language, id);
976 if (len > 0)
977 return len;
978 }
979 }
980 return -EINVAL;
981}
982
983/**
984 * usb_string_id() - allocate an unused string ID
985 * @cdev: the device whose string descriptor IDs are being allocated
986 * Context: single threaded during gadget setup
987 *
988 * @usb_string_id() is called from bind() callbacks to allocate
989 * string IDs. Drivers for functions, configurations, or gadgets will
990 * then store that ID in the appropriate descriptors and string table.
991 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200992 * All string identifier should be allocated using this,
993 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
994 * that for example different functions don't wrongly assign different
995 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700996 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200997int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700998{
999 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001000 /* string id 0 is reserved by USB spec for list of
1001 * supported languages */
1002 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001003 cdev->next_string_id++;
1004 return cdev->next_string_id;
1005 }
1006 return -ENODEV;
1007}
1008
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001009/**
1010 * usb_string_ids() - allocate unused string IDs in batch
1011 * @cdev: the device whose string descriptor IDs are being allocated
1012 * @str: an array of usb_string objects to assign numbers to
1013 * Context: single threaded during gadget setup
1014 *
1015 * @usb_string_ids() is called from bind() callbacks to allocate
1016 * string IDs. Drivers for functions, configurations, or gadgets will
1017 * then copy IDs from the string table to the appropriate descriptors
1018 * and string table for other languages.
1019 *
1020 * All string identifier should be allocated using this,
1021 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1022 * example different functions don't wrongly assign different meanings
1023 * to the same identifier.
1024 */
1025int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1026{
1027 int next = cdev->next_string_id;
1028
1029 for (; str->s; ++str) {
1030 if (unlikely(next >= 254))
1031 return -ENODEV;
1032 str->id = ++next;
1033 }
1034
1035 cdev->next_string_id = next;
1036
1037 return 0;
1038}
1039
1040/**
1041 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001042 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001043 * @n: number of string IDs to allocate
1044 * Context: single threaded during gadget setup
1045 *
1046 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001047 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001048 * is, returns last requested ID which is now very useful information.
1049 *
1050 * @usb_string_ids_n() is called from bind() callbacks to allocate
1051 * string IDs. Drivers for functions, configurations, or gadgets will
1052 * then store that ID in the appropriate descriptors and string table.
1053 *
1054 * All string identifier should be allocated using this,
1055 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1056 * example different functions don't wrongly assign different meanings
1057 * to the same identifier.
1058 */
1059int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1060{
1061 unsigned next = c->next_string_id;
1062 if (unlikely(n > 254 || (unsigned)next + n > 254))
1063 return -ENODEV;
1064 c->next_string_id += n;
1065 return next + 1;
1066}
1067
1068
David Brownell40982be2008-06-19 17:52:58 -07001069/*-------------------------------------------------------------------------*/
1070
1071static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1072{
1073 if (req->status || req->actual != req->length)
1074 DBG((struct usb_composite_dev *) ep->driver_data,
1075 "setup complete --> %d, %d/%d\n",
1076 req->status, req->actual, req->length);
1077}
1078
1079/*
1080 * The setup() callback implements all the ep0 functionality that's
1081 * not handled lower down, in hardware or the hardware driver(like
1082 * device and endpoint feature flags, and their status). It's all
1083 * housekeeping for the gadget function we're implementing. Most of
1084 * the work is in config and function specific setup.
1085 */
1086static int
1087composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1088{
1089 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1090 struct usb_request *req = cdev->req;
1091 int value = -EOPNOTSUPP;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +03001092 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001093 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001094 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001095 u16 w_value = le16_to_cpu(ctrl->wValue);
1096 u16 w_length = le16_to_cpu(ctrl->wLength);
1097 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001098 u8 endp;
Vijayavardhan Vennapusa95b650a2012-01-18 12:54:01 +05301099 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001100
Manu Gautam8d887a12011-10-12 17:13:52 +05301101
1102 if (w_length > USB_BUFSIZ)
1103 return value;
1104
David Brownell40982be2008-06-19 17:52:58 -07001105 /* partial re-init of the response message; the function or the
1106 * gadget might need to intercept e.g. a control-OUT completion
1107 * when we delegate to it.
1108 */
1109 req->zero = 0;
1110 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301111 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001112 gadget->ep0->driver_data = cdev;
1113
1114 switch (ctrl->bRequest) {
1115
1116 /* we handle all standard USB descriptors */
1117 case USB_REQ_GET_DESCRIPTOR:
1118 if (ctrl->bRequestType != USB_DIR_IN)
1119 goto unknown;
1120 switch (w_value >> 8) {
1121
1122 case USB_DT_DEVICE:
1123 cdev->desc.bNumConfigurations =
1124 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +03001125 cdev->desc.bMaxPacketSize0 =
1126 cdev->gadget->ep0->maxpacket;
1127 if (gadget_is_superspeed(gadget)) {
1128 if (gadget->speed >= USB_SPEED_SUPER)
1129 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
1130 else
1131 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1132 }
1133
David Brownell40982be2008-06-19 17:52:58 -07001134 value = min(w_length, (u16) sizeof cdev->desc);
1135 memcpy(req->buf, &cdev->desc, value);
1136 break;
1137 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +03001138 if (!gadget_is_dualspeed(gadget) ||
1139 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001140 break;
1141 device_qual(cdev);
1142 value = min_t(int, w_length,
1143 sizeof(struct usb_qualifier_descriptor));
1144 break;
1145 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +03001146 if (!gadget_is_dualspeed(gadget) ||
1147 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001148 break;
1149 /* FALLTHROUGH */
1150 case USB_DT_CONFIG:
1151 value = config_desc(cdev, w_value);
1152 if (value >= 0)
1153 value = min(w_length, (u16) value);
1154 break;
Vijayavardhan Vennapusa95b650a2012-01-18 12:54:01 +05301155 case USB_DT_OTG:
1156 if (!gadget_is_otg(gadget))
1157 break;
1158 c = list_first_entry(&cdev->configs,
1159 struct usb_configuration, list);
1160 if (c && c->descriptors)
1161 value = usb_find_descriptor_fillbuf(req->buf,
1162 USB_BUFSIZ, c->descriptors,
1163 USB_DT_OTG);
1164 break;
David Brownell40982be2008-06-19 17:52:58 -07001165 case USB_DT_STRING:
1166 value = get_string(cdev, req->buf,
1167 w_index, w_value & 0xff);
1168 if (value >= 0)
1169 value = min(w_length, (u16) value);
1170 break;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +03001171 case USB_DT_BOS:
1172 if (gadget_is_superspeed(gadget)) {
1173 value = bos_desc(cdev);
1174 value = min(w_length, (u16) value);
1175 }
1176 break;
David Brownell40982be2008-06-19 17:52:58 -07001177 }
1178 break;
1179
1180 /* any number of configs can work */
1181 case USB_REQ_SET_CONFIGURATION:
1182 if (ctrl->bRequestType != 0)
1183 goto unknown;
1184 if (gadget_is_otg(gadget)) {
1185 if (gadget->a_hnp_support)
1186 DBG(cdev, "HNP available\n");
1187 else if (gadget->a_alt_hnp_support)
1188 DBG(cdev, "HNP on another port\n");
1189 else
1190 VDBG(cdev, "HNP inactive\n");
1191 }
1192 spin_lock(&cdev->lock);
1193 value = set_config(cdev, ctrl, w_value);
1194 spin_unlock(&cdev->lock);
1195 break;
1196 case USB_REQ_GET_CONFIGURATION:
1197 if (ctrl->bRequestType != USB_DIR_IN)
1198 goto unknown;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001199 if (cdev->config)
David Brownell40982be2008-06-19 17:52:58 -07001200 *(u8 *)req->buf = cdev->config->bConfigurationValue;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001201 else
David Brownell40982be2008-06-19 17:52:58 -07001202 *(u8 *)req->buf = 0;
Oleg Matcovschie67dd162011-03-11 10:24:30 -08001203 value = min(w_length, (u16) 1);
David Brownell40982be2008-06-19 17:52:58 -07001204 break;
1205
1206 /* function drivers must handle get/set altsetting; if there's
1207 * no get() method, we know only altsetting zero works.
1208 */
1209 case USB_REQ_SET_INTERFACE:
1210 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1211 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001212 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001213 break;
Bryan Wu08889512009-01-08 00:21:19 +08001214 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001215 if (!f)
1216 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001217 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001218 break;
1219 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001220 if (value == USB_GADGET_DELAYED_STATUS) {
1221 DBG(cdev,
1222 "%s: interface %d (%s) requested delayed status\n",
1223 __func__, intf, f->name);
1224 cdev->delayed_status++;
1225 DBG(cdev, "delayed_status count %d\n",
1226 cdev->delayed_status);
1227 }
David Brownell40982be2008-06-19 17:52:58 -07001228 break;
1229 case USB_REQ_GET_INTERFACE:
1230 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1231 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001232 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001233 break;
Bryan Wu08889512009-01-08 00:21:19 +08001234 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001235 if (!f)
1236 break;
1237 /* lots of interfaces only need altsetting zero... */
1238 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1239 if (value < 0)
1240 break;
1241 *((u8 *)req->buf) = value;
1242 value = min(w_length, (u16) 1);
1243 break;
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +03001244
1245 /*
1246 * USB 3.0 additions:
1247 * Function driver should handle get_status request. If such cb
1248 * wasn't supplied we respond with default value = 0
1249 * Note: function driver should supply such cb only for the first
1250 * interface of the function
1251 */
1252 case USB_REQ_GET_STATUS:
1253 if (!gadget_is_superspeed(gadget))
1254 goto unknown;
1255 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1256 goto unknown;
1257 value = 2; /* This is the length of the get_status reply */
1258 put_unaligned_le16(0, req->buf);
1259 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1260 break;
1261 f = cdev->config->interface[intf];
1262 if (!f)
1263 break;
1264 status = f->get_status ? f->get_status(f) : 0;
1265 if (status < 0)
1266 break;
1267 put_unaligned_le16(status & 0x0000ffff, req->buf);
1268 break;
1269 /*
1270 * Function drivers should handle SetFeature/ClearFeature
1271 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1272 * only for the first interface of the function
1273 */
1274 case USB_REQ_CLEAR_FEATURE:
1275 case USB_REQ_SET_FEATURE:
1276 if (!gadget_is_superspeed(gadget))
1277 goto unknown;
1278 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1279 goto unknown;
1280 switch (w_value) {
1281 case USB_INTRF_FUNC_SUSPEND:
1282 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1283 break;
1284 f = cdev->config->interface[intf];
1285 if (!f)
1286 break;
1287 value = 0;
1288 if (f->func_suspend)
1289 value = f->func_suspend(f, w_index >> 8);
1290 if (value < 0) {
1291 ERROR(cdev,
1292 "func_suspend() returned error %d\n",
1293 value);
1294 value = 0;
1295 }
1296 break;
1297 }
1298 break;
David Brownell40982be2008-06-19 17:52:58 -07001299 default:
1300unknown:
1301 VDBG(cdev,
1302 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1303 ctrl->bRequestType, ctrl->bRequest,
1304 w_value, w_index, w_length);
1305
Laurent Pinchart52426582009-10-21 00:03:38 +02001306 /* functions always handle their interfaces and endpoints...
1307 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001308 * configuration code.
1309 *
1310 * REVISIT it could make sense to let the composite device
1311 * take such requests too, if that's ever needed: to work
1312 * in config 0, etc.
1313 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001314 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1315 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001316 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301317 break;
1318 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001319 break;
1320
1321 case USB_RECIP_ENDPOINT:
1322 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1323 list_for_each_entry(f, &cdev->config->functions, list) {
1324 if (test_bit(endp, f->endpoints))
1325 break;
1326 }
1327 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001328 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001329 break;
David Brownell40982be2008-06-19 17:52:58 -07001330 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001331
1332 if (f && f->setup)
1333 value = f->setup(f, ctrl);
1334 else {
David Brownell40982be2008-06-19 17:52:58 -07001335 struct usb_configuration *c;
1336
1337 c = cdev->config;
1338 if (c && c->setup)
1339 value = c->setup(c, ctrl);
1340 }
1341
1342 goto done;
1343 }
1344
1345 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001346 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001347 req->length = value;
1348 req->zero = value < w_length;
1349 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1350 if (value < 0) {
1351 DBG(cdev, "ep_queue --> %d\n", value);
1352 req->status = 0;
1353 composite_setup_complete(gadget->ep0, req);
1354 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001355 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1356 WARN(cdev,
1357 "%s: Delayed status not supported for w_length != 0",
1358 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001359 }
1360
1361done:
1362 /* device either stalls (value < 0) or reports success */
1363 return value;
1364}
1365
1366static void composite_disconnect(struct usb_gadget *gadget)
1367{
1368 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1369 unsigned long flags;
1370
1371 /* REVISIT: should we have config and device level
1372 * disconnect callbacks?
1373 */
1374 spin_lock_irqsave(&cdev->lock, flags);
1375 if (cdev->config)
1376 reset_config(cdev);
Michal Nazarewicz3f3e12d2010-06-21 13:57:08 +02001377 if (composite->disconnect)
1378 composite->disconnect(cdev);
Mike Lockwood28acc1a2010-06-28 16:19:32 -04001379 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell40982be2008-06-19 17:52:58 -07001380}
1381
1382/*-------------------------------------------------------------------------*/
1383
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001384static ssize_t composite_show_suspended(struct device *dev,
1385 struct device_attribute *attr,
1386 char *buf)
1387{
1388 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1389 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1390
1391 return sprintf(buf, "%d\n", cdev->suspended);
1392}
1393
1394static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1395
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001396static void
David Brownell40982be2008-06-19 17:52:58 -07001397composite_unbind(struct usb_gadget *gadget)
1398{
1399 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1400
1401 /* composite_disconnect() must already have been called
1402 * by the underlying peripheral controller driver!
1403 * so there's no i/o concurrency that could affect the
1404 * state protected by cdev->lock.
1405 */
1406 WARN_ON(cdev->config);
1407
1408 while (!list_empty(&cdev->configs)) {
1409 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001410 c = list_first_entry(&cdev->configs,
1411 struct usb_configuration, list);
Benoit Goby94df1bd2011-05-25 23:59:43 -07001412 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001413 }
1414 if (composite->unbind)
1415 composite->unbind(cdev);
1416
1417 if (cdev->req) {
1418 kfree(cdev->req->buf);
1419 usb_ep_free_request(gadget->ep0, cdev->req);
1420 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301421 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001422 kfree(cdev);
1423 set_gadget_data(gadget, NULL);
1424 composite = NULL;
1425}
1426
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001427static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001428{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001429 if (!*desc) {
1430 int ret = usb_string_id(cdev);
1431 if (unlikely(ret < 0))
1432 WARNING(cdev, "failed to override string ID\n");
1433 else
1434 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001435 }
David Brownell40982be2008-06-19 17:52:58 -07001436
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001437 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001438}
1439
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001440static int composite_bind(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001441{
1442 struct usb_composite_dev *cdev;
1443 int status = -ENOMEM;
1444
1445 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1446 if (!cdev)
1447 return status;
1448
1449 spin_lock_init(&cdev->lock);
1450 cdev->gadget = gadget;
1451 set_gadget_data(gadget, cdev);
1452 INIT_LIST_HEAD(&cdev->configs);
1453
1454 /* preallocate control response and buffer */
1455 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1456 if (!cdev->req)
1457 goto fail;
1458 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1459 if (!cdev->req->buf)
1460 goto fail;
1461 cdev->req->complete = composite_setup_complete;
1462 gadget->ep0->driver_data = cdev;
1463
1464 cdev->bufsiz = USB_BUFSIZ;
1465 cdev->driver = composite;
1466
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301467 /*
1468 * As per USB compliance update, a device that is actively drawing
1469 * more than 100mA from USB must report itself as bus-powered in
1470 * the GetStatus(DEVICE) call.
1471 */
1472 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1473 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001474
1475 /* interface and string IDs start at zero via kzalloc.
1476 * we force endpoints to start unassigned; few controller
1477 * drivers will zero ep->driver_data.
1478 */
1479 usb_ep_autoconfig_reset(cdev->gadget);
1480
1481 /* composite gadget needs to assign strings for whole device (like
1482 * serial number), register function drivers, potentially update
1483 * power state and consumption, etc
1484 */
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001485 status = composite_gadget_bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001486 if (status < 0)
1487 goto fail;
1488
1489 cdev->desc = *composite->dev;
David Brownell40982be2008-06-19 17:52:58 -07001490
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001491 /* standardized runtime overrides for device ID data */
1492 if (idVendor)
1493 cdev->desc.idVendor = cpu_to_le16(idVendor);
1494 if (idProduct)
1495 cdev->desc.idProduct = cpu_to_le16(idProduct);
1496 if (bcdDevice)
1497 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1498
Marek Belisko78bff3c2010-10-27 10:19:01 +02001499 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001500 if (iManufacturer || !cdev->desc.iManufacturer) {
1501 if (!iManufacturer && !composite->iManufacturer &&
1502 !*composite_manufacturer)
1503 snprintf(composite_manufacturer,
1504 sizeof composite_manufacturer,
1505 "%s %s with %s",
1506 init_utsname()->sysname,
1507 init_utsname()->release,
1508 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001509
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001510 cdev->manufacturer_override =
1511 override_id(cdev, &cdev->desc.iManufacturer);
1512 }
1513
1514 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1515 cdev->product_override =
1516 override_id(cdev, &cdev->desc.iProduct);
1517
1518 if (iSerialNumber)
1519 cdev->serial_override =
1520 override_id(cdev, &cdev->desc.iSerialNumber);
1521
1522 /* has userspace failed to provide a serial number? */
1523 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1524 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1525
1526 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001527 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1528 if (status)
1529 goto fail;
1530
David Brownell40982be2008-06-19 17:52:58 -07001531 INFO(cdev, "%s ready\n", composite->name);
1532 return 0;
1533
1534fail:
1535 composite_unbind(gadget);
1536 return status;
1537}
1538
1539/*-------------------------------------------------------------------------*/
1540
1541static void
1542composite_suspend(struct usb_gadget *gadget)
1543{
1544 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1545 struct usb_function *f;
1546
David Brownell89429392009-03-19 14:14:17 -07001547 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001548 * suspend/resume callbacks?
1549 */
1550 DBG(cdev, "suspend\n");
1551 if (cdev->config) {
1552 list_for_each_entry(f, &cdev->config->functions, list) {
1553 if (f->suspend)
1554 f->suspend(f);
1555 }
1556 }
David Brownell89429392009-03-19 14:14:17 -07001557 if (composite->suspend)
1558 composite->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001559
1560 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001561
1562 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001563}
1564
1565static void
1566composite_resume(struct usb_gadget *gadget)
1567{
1568 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1569 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001570 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001571
David Brownell89429392009-03-19 14:14:17 -07001572 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001573 * suspend/resume callbacks?
1574 */
1575 DBG(cdev, "resume\n");
David Brownell89429392009-03-19 14:14:17 -07001576 if (composite->resume)
1577 composite->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001578 if (cdev->config) {
1579 list_for_each_entry(f, &cdev->config->functions, list) {
1580 if (f->resume)
1581 f->resume(f);
1582 }
Hao Wub23f2f92010-11-29 15:17:03 +08001583
1584 maxpower = cdev->config->bMaxPower;
1585
1586 usb_gadget_vbus_draw(gadget, maxpower ?
1587 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001588 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001589
1590 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001591}
1592
1593/*-------------------------------------------------------------------------*/
1594
1595static struct usb_gadget_driver composite_driver = {
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +03001596#ifdef CONFIG_USB_GADGET_SUPERSPEED
1597 .speed = USB_SPEED_SUPER,
1598#else
David Brownell40982be2008-06-19 17:52:58 -07001599 .speed = USB_SPEED_HIGH,
Tatyana Brokhmanbc2aa112011-06-29 16:41:50 +03001600#endif
David Brownell40982be2008-06-19 17:52:58 -07001601
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001602 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001603
1604 .setup = composite_setup,
1605 .disconnect = composite_disconnect,
1606
1607 .suspend = composite_suspend,
1608 .resume = composite_resume,
1609
1610 .driver = {
1611 .owner = THIS_MODULE,
1612 },
1613};
1614
1615/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001616 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001617 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001618 * @bind: the callback used to allocate resources that are shared across the
1619 * whole device, such as string IDs, and add its configurations using
1620 * @usb_add_config(). This may fail by returning a negative errno
1621 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001622 * Context: single threaded during gadget setup
1623 *
1624 * This function is used to register drivers using the composite driver
1625 * framework. The return value is zero, or a negative errno value.
1626 * Those values normally come from the driver's @bind method, which does
1627 * all the work of setting up the driver to match the hardware.
1628 *
1629 * On successful return, the gadget is ready to respond to requests from
1630 * the host, unless one of its components invokes usb_gadget_disconnect()
1631 * while it was binding. That would usually be done in order to wait for
1632 * some userspace participation.
1633 */
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001634int usb_composite_probe(struct usb_composite_driver *driver,
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001635 int (*bind)(struct usb_composite_dev *cdev))
David Brownell40982be2008-06-19 17:52:58 -07001636{
Lena Salmand092f2d2012-03-12 17:27:24 +02001637 int retval;
1638
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001639 if (!driver || !driver->dev || !bind || composite)
David Brownell40982be2008-06-19 17:52:58 -07001640 return -EINVAL;
1641
1642 if (!driver->name)
1643 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001644 if (!driver->iProduct)
1645 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001646 composite_driver.function = (char *) driver->name;
1647 composite_driver.driver.name = driver->name;
Tatyana Brokhman3ba28902011-06-29 16:41:49 +03001648 composite_driver.speed = min((u8)composite_driver.speed,
1649 (u8)driver->max_speed);
David Brownell40982be2008-06-19 17:52:58 -07001650 composite = driver;
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001651 composite_gadget_bind = bind;
David Brownell40982be2008-06-19 17:52:58 -07001652
Lena Salmand092f2d2012-03-12 17:27:24 +02001653 retval = usb_gadget_probe_driver(&composite_driver, composite_bind);
1654 if (retval)
1655 composite = NULL;
1656 return retval;
David Brownell40982be2008-06-19 17:52:58 -07001657}
1658
1659/**
1660 * usb_composite_unregister() - unregister a composite driver
1661 * @driver: the driver to unregister
1662 *
1663 * This function is used to unregister drivers using the composite
1664 * driver framework.
1665 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001666void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001667{
1668 if (composite != driver)
1669 return;
1670 usb_gadget_unregister_driver(&composite_driver);
1671}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001672
1673/**
1674 * usb_composite_setup_continue() - Continue with the control transfer
1675 * @cdev: the composite device who's control transfer was kept waiting
1676 *
1677 * This function must be called by the USB function driver to continue
1678 * with the control transfer's data/status stage in case it had requested to
1679 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1680 * can request the composite framework to delay the setup request's data/status
1681 * stages by returning USB_GADGET_DELAYED_STATUS.
1682 */
1683void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1684{
1685 int value;
1686 struct usb_request *req = cdev->req;
1687 unsigned long flags;
1688
1689 DBG(cdev, "%s\n", __func__);
1690 spin_lock_irqsave(&cdev->lock, flags);
1691
1692 if (cdev->delayed_status == 0) {
1693 WARN(cdev, "%s: Unexpected call\n", __func__);
1694
1695 } else if (--cdev->delayed_status == 0) {
1696 DBG(cdev, "%s: Completing delayed status\n", __func__);
1697 req->length = 0;
1698 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1699 if (value < 0) {
1700 DBG(cdev, "ep_queue --> %d\n", value);
1701 req->status = 0;
1702 composite_setup_complete(cdev->gadget->ep0, req);
1703 }
1704 }
1705
1706 spin_unlock_irqrestore(&cdev->lock, flags);
1707}
1708