blob: 52689ee9db1249587c99bfe1c7d43fbc14daedc4 [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.
David Brownell40982be2008-06-19 17:52:58 -070010 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kallsyms.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040017#include <linux/module.h>
David Brownell40982be2008-06-19 17:52:58 -070018#include <linux/device.h>
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020019#include <linux/utsname.h>
David Brownell40982be2008-06-19 17:52:58 -070020
21#include <linux/usb/composite.h>
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +030022#include <asm/unaligned.h>
David Brownell40982be2008-06-19 17:52:58 -070023
24/*
25 * The code in this file is utility code, used to build a gadget driver
26 * from one or more "function" drivers, one or more "configuration"
27 * objects, and a "usb_composite_driver" by gluing them together along
28 * with the relevant device-wide data.
29 */
30
Lucas De Marchi25985ed2011-03-30 22:57:33 -030031/* Some systems will need runtime overrides for the product identifiers
David Brownell40982be2008-06-19 17:52:58 -070032 * published in the device descriptor, either numbers or strings or both.
33 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
34 */
35
36static ushort idVendor;
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +020037module_param(idVendor, ushort, 0644);
David Brownell40982be2008-06-19 17:52:58 -070038MODULE_PARM_DESC(idVendor, "USB Vendor ID");
39
40static ushort idProduct;
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +020041module_param(idProduct, ushort, 0644);
David Brownell40982be2008-06-19 17:52:58 -070042MODULE_PARM_DESC(idProduct, "USB Product ID");
43
44static ushort bcdDevice;
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +020045module_param(bcdDevice, ushort, 0644);
David Brownell40982be2008-06-19 17:52:58 -070046MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
47
48static char *iManufacturer;
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +020049module_param(iManufacturer, charp, 0644);
David Brownell40982be2008-06-19 17:52:58 -070050MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
51
52static char *iProduct;
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +020053module_param(iProduct, charp, 0644);
David Brownell40982be2008-06-19 17:52:58 -070054MODULE_PARM_DESC(iProduct, "USB Product string");
55
56static char *iSerialNumber;
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +020057module_param(iSerialNumber, charp, 0644);
David Brownell40982be2008-06-19 17:52:58 -070058MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
59
Michal Nazarewiczad1a8102010-08-12 17:43:46 +020060static char composite_manufacturer[50];
61
David Brownell40982be2008-06-19 17:52:58 -070062/*-------------------------------------------------------------------------*/
Tatyana Brokhman48767a42011-06-28 16:33:49 +030063/**
64 * next_ep_desc() - advance to the next EP descriptor
65 * @t: currect pointer within descriptor array
66 *
67 * Return: next EP descriptor or NULL
68 *
69 * Iterate over @t until either EP descriptor found or
70 * NULL (that indicates end of list) encountered
71 */
72static struct usb_descriptor_header**
73next_ep_desc(struct usb_descriptor_header **t)
74{
75 for (; *t; t++) {
76 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
77 return t;
78 }
79 return NULL;
80}
81
82/*
83 * for_each_ep_desc()- iterate over endpoint descriptors in the
84 * descriptors list
85 * @start: pointer within descriptor array.
86 * @ep_desc: endpoint descriptor to use as the loop cursor
87 */
88#define for_each_ep_desc(start, ep_desc) \
89 for (ep_desc = next_ep_desc(start); \
90 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
91
92/**
93 * config_ep_by_speed() - configures the given endpoint
94 * according to gadget speed.
95 * @g: pointer to the gadget
96 * @f: usb function
97 * @_ep: the endpoint to configure
98 *
99 * Return: error code, 0 on success
100 *
101 * This function chooses the right descriptors for a given
102 * endpoint according to gadget speed and saves it in the
103 * endpoint desc field. If the endpoint already has a descriptor
104 * assigned to it - overwrites it with currently corresponding
105 * descriptor. The endpoint maxpacket field is updated according
106 * to the chosen descriptor.
107 * Note: the supplied function should hold all the descriptors
108 * for supported speeds
109 */
110int config_ep_by_speed(struct usb_gadget *g,
111 struct usb_function *f,
112 struct usb_ep *_ep)
113{
Felipe Balbib785ea72012-06-06 10:20:23 +0300114 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300115 struct usb_endpoint_descriptor *chosen_desc = NULL;
116 struct usb_descriptor_header **speed_desc = NULL;
117
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300118 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
119 int want_comp_desc = 0;
120
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300121 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
122
123 if (!g || !f || !_ep)
124 return -EIO;
125
126 /* select desired speed */
127 switch (g->speed) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300128 case USB_SPEED_SUPER:
129 if (gadget_is_superspeed(g)) {
130 speed_desc = f->ss_descriptors;
131 want_comp_desc = 1;
132 break;
133 }
134 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300135 case USB_SPEED_HIGH:
136 if (gadget_is_dualspeed(g)) {
137 speed_desc = f->hs_descriptors;
138 break;
139 }
140 /* else: fall through */
141 default:
142 speed_desc = f->descriptors;
143 }
144 /* find descriptors */
145 for_each_ep_desc(speed_desc, d_spd) {
146 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
147 if (chosen_desc->bEndpointAddress == _ep->address)
148 goto ep_found;
149 }
150 return -EIO;
151
152ep_found:
153 /* commit results */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700154 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300155 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300156 _ep->comp_desc = NULL;
157 _ep->maxburst = 0;
158 _ep->mult = 0;
159 if (!want_comp_desc)
160 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300161
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300162 /*
163 * Companion descriptor should follow EP descriptor
164 * USB 3.0 spec, #9.6.7
165 */
166 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
167 if (!comp_desc ||
168 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
169 return -EIO;
170 _ep->comp_desc = comp_desc;
171 if (g->speed == USB_SPEED_SUPER) {
172 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300173 case USB_ENDPOINT_XFER_ISOC:
174 /* mult: bits 1:0 of bmAttributes */
175 _ep->mult = comp_desc->bmAttributes & 0x3;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800176 case USB_ENDPOINT_XFER_BULK:
177 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300178 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300179 break;
180 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300181 if (comp_desc->bMaxBurst != 0)
182 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
183 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300184 break;
185 }
186 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300187 return 0;
188}
David Brownell40982be2008-06-19 17:52:58 -0700189
190/**
191 * usb_add_function() - add a function to a configuration
192 * @config: the configuration
193 * @function: the function being added
194 * Context: single threaded during gadget setup
195 *
196 * After initialization, each configuration must have one or more
197 * functions added to it. Adding a function involves calling its @bind()
198 * method to allocate resources such as interface and string identifiers
199 * and endpoints.
200 *
201 * This function returns the value of the function's bind(), which is
202 * zero for success else a negative errno value.
203 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200204int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700205 struct usb_function *function)
206{
207 int value = -EINVAL;
208
209 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
210 function->name, function,
211 config->label, config);
212
213 if (!function->set_alt || !function->disable)
214 goto done;
215
216 function->config = config;
217 list_add_tail(&function->list, &config->functions);
218
219 /* REVISIT *require* function->bind? */
220 if (function->bind) {
221 value = function->bind(config, function);
222 if (value < 0) {
223 list_del(&function->list);
224 function->config = NULL;
225 }
226 } else
227 value = 0;
228
229 /* We allow configurations that don't work at both speeds.
230 * If we run into a lowspeed Linux system, treat it the same
231 * as full speed ... it's the function drivers that will need
232 * to avoid bulk and ISO transfers.
233 */
234 if (!config->fullspeed && function->descriptors)
235 config->fullspeed = true;
236 if (!config->highspeed && function->hs_descriptors)
237 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300238 if (!config->superspeed && function->ss_descriptors)
239 config->superspeed = true;
David Brownell40982be2008-06-19 17:52:58 -0700240
241done:
242 if (value)
243 DBG(config->cdev, "adding '%s'/%p --> %d\n",
244 function->name, function, value);
245 return value;
246}
247
248/**
David Brownell60beed92008-08-18 17:38:22 -0700249 * usb_function_deactivate - prevent function and gadget enumeration
250 * @function: the function that isn't yet ready to respond
251 *
252 * Blocks response of the gadget driver to host enumeration by
253 * preventing the data line pullup from being activated. This is
254 * normally called during @bind() processing to change from the
255 * initial "ready to respond" state, or when a required resource
256 * becomes available.
257 *
258 * For example, drivers that serve as a passthrough to a userspace
259 * daemon can block enumeration unless that daemon (such as an OBEX,
260 * MTP, or print server) is ready to handle host requests.
261 *
262 * Not all systems support software control of their USB peripheral
263 * data pullups.
264 *
265 * Returns zero on success, else negative errno.
266 */
267int usb_function_deactivate(struct usb_function *function)
268{
269 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200270 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700271 int status = 0;
272
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200273 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700274
275 if (cdev->deactivations == 0)
276 status = usb_gadget_disconnect(cdev->gadget);
277 if (status == 0)
278 cdev->deactivations++;
279
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200280 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700281 return status;
282}
283
284/**
285 * usb_function_activate - allow function and gadget enumeration
286 * @function: function on which usb_function_activate() was called
287 *
288 * Reverses effect of usb_function_deactivate(). If no more functions
289 * are delaying their activation, the gadget driver will respond to
290 * host enumeration procedures.
291 *
292 * Returns zero on success, else negative errno.
293 */
294int usb_function_activate(struct usb_function *function)
295{
296 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200297 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700298 int status = 0;
299
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200300 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700301
302 if (WARN_ON(cdev->deactivations == 0))
303 status = -EINVAL;
304 else {
305 cdev->deactivations--;
306 if (cdev->deactivations == 0)
307 status = usb_gadget_connect(cdev->gadget);
308 }
309
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200310 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700311 return status;
312}
313
314/**
David Brownell40982be2008-06-19 17:52:58 -0700315 * usb_interface_id() - allocate an unused interface ID
316 * @config: configuration associated with the interface
317 * @function: function handling the interface
318 * Context: single threaded during gadget setup
319 *
320 * usb_interface_id() is called from usb_function.bind() callbacks to
321 * allocate new interface IDs. The function driver will then store that
322 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300323 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700324 * particularly changing its altsetting via set_alt(). There may
325 * also be class-specific or vendor-specific requests to handle.
326 *
327 * All interface identifier should be allocated using this routine, to
328 * ensure that for example different functions don't wrongly assign
329 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300330 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700331 * one configuration (or more than once in a given configuration) need
332 * multiple versions of the relevant descriptors.
333 *
334 * Returns the interface ID which was allocated; or -ENODEV if no
335 * more interface IDs can be allocated.
336 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200337int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700338 struct usb_function *function)
339{
340 unsigned id = config->next_interface_id;
341
342 if (id < MAX_CONFIG_INTERFACES) {
343 config->interface[id] = function;
344 config->next_interface_id = id + 1;
345 return id;
346 }
347 return -ENODEV;
348}
349
350static int config_buf(struct usb_configuration *config,
351 enum usb_device_speed speed, void *buf, u8 type)
352{
353 struct usb_config_descriptor *c = buf;
354 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200355 int len;
David Brownell40982be2008-06-19 17:52:58 -0700356 struct usb_function *f;
357 int status;
358
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200359 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700360 /* write the config descriptor */
361 c = buf;
362 c->bLength = USB_DT_CONFIG_SIZE;
363 c->bDescriptorType = type;
364 /* wTotalLength is written later */
365 c->bNumInterfaces = config->next_interface_id;
366 c->bConfigurationValue = config->bConfigurationValue;
367 c->iConfiguration = config->iConfiguration;
368 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
David Brownell36e893d2008-09-12 09:39:06 -0700369 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
David Brownell40982be2008-06-19 17:52:58 -0700370
371 /* There may be e.g. OTG descriptors */
372 if (config->descriptors) {
373 status = usb_descriptor_fillbuf(next, len,
374 config->descriptors);
375 if (status < 0)
376 return status;
377 len -= status;
378 next += status;
379 }
380
381 /* add each function's descriptors */
382 list_for_each_entry(f, &config->functions, list) {
383 struct usb_descriptor_header **descriptors;
384
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300385 switch (speed) {
386 case USB_SPEED_SUPER:
387 descriptors = f->ss_descriptors;
388 break;
389 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700390 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300391 break;
392 default:
David Brownell40982be2008-06-19 17:52:58 -0700393 descriptors = f->descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300394 }
395
David Brownell40982be2008-06-19 17:52:58 -0700396 if (!descriptors)
397 continue;
398 status = usb_descriptor_fillbuf(next, len,
399 (const struct usb_descriptor_header **) descriptors);
400 if (status < 0)
401 return status;
402 len -= status;
403 next += status;
404 }
405
406 len = next - buf;
407 c->wTotalLength = cpu_to_le16(len);
408 return len;
409}
410
411static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
412{
413 struct usb_gadget *gadget = cdev->gadget;
414 struct usb_configuration *c;
415 u8 type = w_value >> 8;
416 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
417
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300418 if (gadget->speed == USB_SPEED_SUPER)
419 speed = gadget->speed;
420 else if (gadget_is_dualspeed(gadget)) {
421 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700422 if (gadget->speed == USB_SPEED_HIGH)
423 hs = 1;
424 if (type == USB_DT_OTHER_SPEED_CONFIG)
425 hs = !hs;
426 if (hs)
427 speed = USB_SPEED_HIGH;
428
429 }
430
431 /* This is a lookup by config *INDEX* */
432 w_value &= 0xff;
433 list_for_each_entry(c, &cdev->configs, list) {
434 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300435 switch (speed) {
436 case USB_SPEED_SUPER:
437 if (!c->superspeed)
438 continue;
439 break;
440 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700441 if (!c->highspeed)
442 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300443 break;
444 default:
David Brownell40982be2008-06-19 17:52:58 -0700445 if (!c->fullspeed)
446 continue;
447 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300448
David Brownell40982be2008-06-19 17:52:58 -0700449 if (w_value == 0)
450 return config_buf(c, speed, cdev->req->buf, type);
451 w_value--;
452 }
453 return -EINVAL;
454}
455
456static int count_configs(struct usb_composite_dev *cdev, unsigned type)
457{
458 struct usb_gadget *gadget = cdev->gadget;
459 struct usb_configuration *c;
460 unsigned count = 0;
461 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300462 int ss = 0;
David Brownell40982be2008-06-19 17:52:58 -0700463
464 if (gadget_is_dualspeed(gadget)) {
465 if (gadget->speed == USB_SPEED_HIGH)
466 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300467 if (gadget->speed == USB_SPEED_SUPER)
468 ss = 1;
David Brownell40982be2008-06-19 17:52:58 -0700469 if (type == USB_DT_DEVICE_QUALIFIER)
470 hs = !hs;
471 }
472 list_for_each_entry(c, &cdev->configs, list) {
473 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300474 if (ss) {
475 if (!c->superspeed)
476 continue;
477 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700478 if (!c->highspeed)
479 continue;
480 } else {
481 if (!c->fullspeed)
482 continue;
483 }
484 count++;
485 }
486 return count;
487}
488
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300489/**
490 * bos_desc() - prepares the BOS descriptor.
491 * @cdev: pointer to usb_composite device to generate the bos
492 * descriptor for
493 *
494 * This function generates the BOS (Binary Device Object)
495 * descriptor and its device capabilities descriptors. The BOS
496 * descriptor should be supported by a SuperSpeed device.
497 */
498static int bos_desc(struct usb_composite_dev *cdev)
499{
500 struct usb_ext_cap_descriptor *usb_ext;
501 struct usb_ss_cap_descriptor *ss_cap;
502 struct usb_dcd_config_params dcd_config_params;
503 struct usb_bos_descriptor *bos = cdev->req->buf;
504
505 bos->bLength = USB_DT_BOS_SIZE;
506 bos->bDescriptorType = USB_DT_BOS;
507
508 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
509 bos->bNumDeviceCaps = 0;
510
511 /*
512 * A SuperSpeed device shall include the USB2.0 extension descriptor
513 * and shall support LPM when operating in USB2.0 HS mode.
514 */
515 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
516 bos->bNumDeviceCaps++;
517 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
518 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
519 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
520 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
521 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
522
523 /*
524 * The Superspeed USB Capability descriptor shall be implemented by all
525 * SuperSpeed devices.
526 */
527 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
528 bos->bNumDeviceCaps++;
529 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
530 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
531 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
532 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
533 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
534 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
535 USB_FULL_SPEED_OPERATION |
536 USB_HIGH_SPEED_OPERATION |
537 USB_5GBPS_OPERATION);
538 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
539
540 /* Get Controller configuration */
541 if (cdev->gadget->ops->get_config_params)
542 cdev->gadget->ops->get_config_params(&dcd_config_params);
543 else {
Felipe Balbi089b8372011-10-10 09:43:44 +0300544 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300545 dcd_config_params.bU2DevExitLat =
Felipe Balbi089b8372011-10-10 09:43:44 +0300546 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300547 }
548 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
549 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
550
551 return le16_to_cpu(bos->wTotalLength);
552}
553
David Brownell40982be2008-06-19 17:52:58 -0700554static void device_qual(struct usb_composite_dev *cdev)
555{
556 struct usb_qualifier_descriptor *qual = cdev->req->buf;
557
558 qual->bLength = sizeof(*qual);
559 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
560 /* POLICY: same bcdUSB and device type info at both speeds */
561 qual->bcdUSB = cdev->desc.bcdUSB;
562 qual->bDeviceClass = cdev->desc.bDeviceClass;
563 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
564 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
565 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200566 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700567 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700568 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700569}
570
571/*-------------------------------------------------------------------------*/
572
573static void reset_config(struct usb_composite_dev *cdev)
574{
575 struct usb_function *f;
576
577 DBG(cdev, "reset config\n");
578
579 list_for_each_entry(f, &cdev->config->functions, list) {
580 if (f->disable)
581 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200582
583 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700584 }
585 cdev->config = NULL;
586}
587
588static int set_config(struct usb_composite_dev *cdev,
589 const struct usb_ctrlrequest *ctrl, unsigned number)
590{
591 struct usb_gadget *gadget = cdev->gadget;
592 struct usb_configuration *c = NULL;
593 int result = -EINVAL;
594 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
595 int tmp;
596
David Brownell40982be2008-06-19 17:52:58 -0700597 if (number) {
598 list_for_each_entry(c, &cdev->configs, list) {
599 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300600 /*
601 * We disable the FDs of the previous
602 * configuration only if the new configuration
603 * is a valid one
604 */
605 if (cdev->config)
606 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700607 result = 0;
608 break;
609 }
610 }
611 if (result < 0)
612 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300613 } else { /* Zero configuration value - need to reset the config */
614 if (cdev->config)
615 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700616 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300617 }
David Brownell40982be2008-06-19 17:52:58 -0700618
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200619 INFO(cdev, "%s config #%d: %s\n",
620 usb_speed_string(gadget->speed),
621 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700622
623 if (!c)
624 goto done;
625
626 cdev->config = c;
627
628 /* Initialize all interfaces by setting them to altsetting zero. */
629 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
630 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200631 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700632
633 if (!f)
634 break;
635
Laurent Pinchart52426582009-10-21 00:03:38 +0200636 /*
637 * Record which endpoints are used by the function. This is used
638 * to dispatch control requests targeted at that endpoint to the
639 * function's setup callback instead of the current
640 * configuration's setup callback.
641 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300642 switch (gadget->speed) {
643 case USB_SPEED_SUPER:
644 descriptors = f->ss_descriptors;
645 break;
646 case USB_SPEED_HIGH:
Laurent Pinchart52426582009-10-21 00:03:38 +0200647 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300648 break;
649 default:
Laurent Pinchart52426582009-10-21 00:03:38 +0200650 descriptors = f->descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300651 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200652
653 for (; *descriptors; ++descriptors) {
654 struct usb_endpoint_descriptor *ep;
655 int addr;
656
657 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
658 continue;
659
660 ep = (struct usb_endpoint_descriptor *)*descriptors;
661 addr = ((ep->bEndpointAddress & 0x80) >> 3)
662 | (ep->bEndpointAddress & 0x0f);
663 set_bit(addr, f->endpoints);
664 }
665
David Brownell40982be2008-06-19 17:52:58 -0700666 result = f->set_alt(f, tmp, 0);
667 if (result < 0) {
668 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
669 tmp, f->name, f, result);
670
671 reset_config(cdev);
672 goto done;
673 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300674
675 if (result == USB_GADGET_DELAYED_STATUS) {
676 DBG(cdev,
677 "%s: interface %d (%s) requested delayed status\n",
678 __func__, tmp, f->name);
679 cdev->delayed_status++;
680 DBG(cdev, "delayed_status count %d\n",
681 cdev->delayed_status);
682 }
David Brownell40982be2008-06-19 17:52:58 -0700683 }
684
685 /* when we return, be sure our power usage is valid */
David Brownell36e893d2008-09-12 09:39:06 -0700686 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700687done:
688 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300689 if (result >= 0 && cdev->delayed_status)
690 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700691 return result;
692}
693
694/**
695 * usb_add_config() - add a configuration to a device.
696 * @cdev: wraps the USB gadget
697 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200698 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700699 * Context: single threaded during gadget setup
700 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200701 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700702 * add each of the configurations it supports, using this routine.
703 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200704 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700705 * is zero for success else a negative errno value. Binding configurations
706 * assigns global resources including string IDs, and per-configuration
707 * resources such as interface IDs and endpoints.
708 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200709int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200710 struct usb_configuration *config,
711 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700712{
713 int status = -EINVAL;
714 struct usb_configuration *c;
715
716 DBG(cdev, "adding config #%u '%s'/%p\n",
717 config->bConfigurationValue,
718 config->label, config);
719
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200720 if (!config->bConfigurationValue || !bind)
David Brownell40982be2008-06-19 17:52:58 -0700721 goto done;
722
723 /* Prevent duplicate configuration identifiers */
724 list_for_each_entry(c, &cdev->configs, list) {
725 if (c->bConfigurationValue == config->bConfigurationValue) {
726 status = -EBUSY;
727 goto done;
728 }
729 }
730
731 config->cdev = cdev;
732 list_add_tail(&config->list, &cdev->configs);
733
734 INIT_LIST_HEAD(&config->functions);
735 config->next_interface_id = 0;
Benoit Goby02e81612012-05-10 10:07:58 +0200736 memset(config->interface, 0, sizeof(config->interface));
David Brownell40982be2008-06-19 17:52:58 -0700737
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200738 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700739 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +0900740 while (!list_empty(&config->functions)) {
741 struct usb_function *f;
742
743 f = list_first_entry(&config->functions,
744 struct usb_function, list);
745 list_del(&f->list);
746 if (f->unbind) {
747 DBG(cdev, "unbind function '%s'/%p\n",
748 f->name, f);
749 f->unbind(config, f);
750 /* may free memory for "f" */
751 }
752 }
David Brownell40982be2008-06-19 17:52:58 -0700753 list_del(&config->list);
754 config->cdev = NULL;
755 } else {
756 unsigned i;
757
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300758 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700759 config->bConfigurationValue, config,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300760 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700761 config->highspeed ? " high" : "",
762 config->fullspeed
763 ? (gadget_is_dualspeed(cdev->gadget)
764 ? " full"
765 : " full/low")
766 : "");
767
768 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
769 struct usb_function *f = config->interface[i];
770
771 if (!f)
772 continue;
773 DBG(cdev, " interface %d = %s/%p\n",
774 i, f->name, f);
775 }
776 }
777
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200778 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700779 * ep->driver_data as needed.
780 */
781 usb_ep_autoconfig_reset(cdev->gadget);
782
783done:
784 if (status)
785 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
786 config->bConfigurationValue, status);
787 return status;
788}
789
Benoit Goby51cce6f2012-05-10 10:07:57 +0200790static void remove_config(struct usb_composite_dev *cdev,
791 struct usb_configuration *config)
792{
793 while (!list_empty(&config->functions)) {
794 struct usb_function *f;
795
796 f = list_first_entry(&config->functions,
797 struct usb_function, list);
798 list_del(&f->list);
799 if (f->unbind) {
800 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
801 f->unbind(config, f);
802 /* may free memory for "f" */
803 }
804 }
805 list_del(&config->list);
806 if (config->unbind) {
807 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
808 config->unbind(config);
809 /* may free memory for "c" */
810 }
811}
812
813/**
814 * usb_remove_config() - remove a configuration from a device.
815 * @cdev: wraps the USB gadget
816 * @config: the configuration
817 *
818 * Drivers must call usb_gadget_disconnect before calling this function
819 * to disconnect the device from the host and make sure the host will not
820 * try to enumerate the device while we are changing the config list.
821 */
822void usb_remove_config(struct usb_composite_dev *cdev,
823 struct usb_configuration *config)
824{
825 unsigned long flags;
826
827 spin_lock_irqsave(&cdev->lock, flags);
828
829 if (cdev->config == config)
830 reset_config(cdev);
831
832 spin_unlock_irqrestore(&cdev->lock, flags);
833
834 remove_config(cdev, config);
835}
836
David Brownell40982be2008-06-19 17:52:58 -0700837/*-------------------------------------------------------------------------*/
838
839/* We support strings in multiple languages ... string descriptor zero
840 * says which languages are supported. The typical case will be that
841 * only one language (probably English) is used, with I18N handled on
842 * the host side.
843 */
844
845static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
846{
847 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +0300848 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -0700849 __le16 *tmp;
850
851 while (*sp) {
852 s = *sp;
853 language = cpu_to_le16(s->language);
854 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
855 if (*tmp == language)
856 goto repeat;
857 }
858 *tmp++ = language;
859repeat:
860 sp++;
861 }
862}
863
864static int lookup_string(
865 struct usb_gadget_strings **sp,
866 void *buf,
867 u16 language,
868 int id
869)
870{
871 struct usb_gadget_strings *s;
872 int value;
873
874 while (*sp) {
875 s = *sp++;
876 if (s->language != language)
877 continue;
878 value = usb_gadget_get_string(s, id, buf);
879 if (value > 0)
880 return value;
881 }
882 return -EINVAL;
883}
884
885static int get_string(struct usb_composite_dev *cdev,
886 void *buf, u16 language, int id)
887{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +0200888 struct usb_composite_driver *composite = cdev->driver;
David Brownell40982be2008-06-19 17:52:58 -0700889 struct usb_configuration *c;
890 struct usb_function *f;
891 int len;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200892 const char *str;
David Brownell40982be2008-06-19 17:52:58 -0700893
894 /* Yes, not only is USB's I18N support probably more than most
895 * folk will ever care about ... also, it's all supported here.
896 * (Except for UTF8 support for Unicode's "Astral Planes".)
897 */
898
899 /* 0 == report all available language codes */
900 if (id == 0) {
901 struct usb_string_descriptor *s = buf;
902 struct usb_gadget_strings **sp;
903
904 memset(s, 0, 256);
905 s->bDescriptorType = USB_DT_STRING;
906
907 sp = composite->strings;
908 if (sp)
909 collect_langs(sp, s->wData);
910
911 list_for_each_entry(c, &cdev->configs, list) {
912 sp = c->strings;
913 if (sp)
914 collect_langs(sp, s->wData);
915
916 list_for_each_entry(f, &c->functions, list) {
917 sp = f->strings;
918 if (sp)
919 collect_langs(sp, s->wData);
920 }
921 }
922
Roel Kluin417b57b2009-08-06 16:09:51 -0700923 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700924 continue;
925 if (!len)
926 return -EINVAL;
927
928 s->bLength = 2 * (len + 1);
929 return s->bLength;
930 }
931
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200932 /* Otherwise, look up and return a specified string. First
933 * check if the string has not been overridden.
934 */
935 if (cdev->manufacturer_override == id)
936 str = iManufacturer ?: composite->iManufacturer ?:
937 composite_manufacturer;
938 else if (cdev->product_override == id)
939 str = iProduct ?: composite->iProduct;
940 else if (cdev->serial_override == id)
Andrzej Pietrasiewiczcad4cd82012-05-10 10:08:00 +0200941 str = iSerialNumber ?: composite->iSerialNumber;
Michal Nazarewiczad1a8102010-08-12 17:43:46 +0200942 else
943 str = NULL;
944 if (str) {
945 struct usb_gadget_strings strings = {
946 .language = language,
947 .strings = &(struct usb_string) { 0xff, str }
948 };
949 return usb_gadget_get_string(&strings, 0xff, buf);
950 }
951
952 /* String IDs are device-scoped, so we look up each string
953 * table we're told about. These lookups are infrequent;
954 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -0700955 */
956 if (composite->strings) {
957 len = lookup_string(composite->strings, buf, language, id);
958 if (len > 0)
959 return len;
960 }
961 list_for_each_entry(c, &cdev->configs, list) {
962 if (c->strings) {
963 len = lookup_string(c->strings, buf, language, id);
964 if (len > 0)
965 return len;
966 }
967 list_for_each_entry(f, &c->functions, list) {
968 if (!f->strings)
969 continue;
970 len = lookup_string(f->strings, buf, language, id);
971 if (len > 0)
972 return len;
973 }
974 }
975 return -EINVAL;
976}
977
978/**
979 * usb_string_id() - allocate an unused string ID
980 * @cdev: the device whose string descriptor IDs are being allocated
981 * Context: single threaded during gadget setup
982 *
983 * @usb_string_id() is called from bind() callbacks to allocate
984 * string IDs. Drivers for functions, configurations, or gadgets will
985 * then store that ID in the appropriate descriptors and string table.
986 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200987 * All string identifier should be allocated using this,
988 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
989 * that for example different functions don't wrongly assign different
990 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -0700991 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200992int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -0700993{
994 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +0200995 /* string id 0 is reserved by USB spec for list of
996 * supported languages */
997 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -0700998 cdev->next_string_id++;
999 return cdev->next_string_id;
1000 }
1001 return -ENODEV;
1002}
1003
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001004/**
1005 * usb_string_ids() - allocate unused string IDs in batch
1006 * @cdev: the device whose string descriptor IDs are being allocated
1007 * @str: an array of usb_string objects to assign numbers to
1008 * Context: single threaded during gadget setup
1009 *
1010 * @usb_string_ids() is called from bind() callbacks to allocate
1011 * string IDs. Drivers for functions, configurations, or gadgets will
1012 * then copy IDs from the string table to the appropriate descriptors
1013 * and string table for other languages.
1014 *
1015 * All string identifier should be allocated using this,
1016 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1017 * example different functions don't wrongly assign different meanings
1018 * to the same identifier.
1019 */
1020int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1021{
1022 int next = cdev->next_string_id;
1023
1024 for (; str->s; ++str) {
1025 if (unlikely(next >= 254))
1026 return -ENODEV;
1027 str->id = ++next;
1028 }
1029
1030 cdev->next_string_id = next;
1031
1032 return 0;
1033}
1034
1035/**
1036 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001037 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001038 * @n: number of string IDs to allocate
1039 * Context: single threaded during gadget setup
1040 *
1041 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001042 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001043 * is, returns last requested ID which is now very useful information.
1044 *
1045 * @usb_string_ids_n() is called from bind() callbacks to allocate
1046 * string IDs. Drivers for functions, configurations, or gadgets will
1047 * then store that ID in the appropriate descriptors and string table.
1048 *
1049 * All string identifier should be allocated using this,
1050 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1051 * example different functions don't wrongly assign different meanings
1052 * to the same identifier.
1053 */
1054int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1055{
1056 unsigned next = c->next_string_id;
1057 if (unlikely(n > 254 || (unsigned)next + n > 254))
1058 return -ENODEV;
1059 c->next_string_id += n;
1060 return next + 1;
1061}
1062
1063
David Brownell40982be2008-06-19 17:52:58 -07001064/*-------------------------------------------------------------------------*/
1065
1066static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1067{
1068 if (req->status || req->actual != req->length)
1069 DBG((struct usb_composite_dev *) ep->driver_data,
1070 "setup complete --> %d, %d/%d\n",
1071 req->status, req->actual, req->length);
1072}
1073
1074/*
1075 * The setup() callback implements all the ep0 functionality that's
1076 * not handled lower down, in hardware or the hardware driver(like
1077 * device and endpoint feature flags, and their status). It's all
1078 * housekeeping for the gadget function we're implementing. Most of
1079 * the work is in config and function specific setup.
1080 */
1081static int
1082composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1083{
1084 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1085 struct usb_request *req = cdev->req;
1086 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001087 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001088 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001089 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001090 u16 w_value = le16_to_cpu(ctrl->wValue);
1091 u16 w_length = le16_to_cpu(ctrl->wLength);
1092 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001093 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001094
1095 /* partial re-init of the response message; the function or the
1096 * gadget might need to intercept e.g. a control-OUT completion
1097 * when we delegate to it.
1098 */
1099 req->zero = 0;
1100 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301101 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001102 gadget->ep0->driver_data = cdev;
1103
1104 switch (ctrl->bRequest) {
1105
1106 /* we handle all standard USB descriptors */
1107 case USB_REQ_GET_DESCRIPTOR:
1108 if (ctrl->bRequestType != USB_DIR_IN)
1109 goto unknown;
1110 switch (w_value >> 8) {
1111
1112 case USB_DT_DEVICE:
1113 cdev->desc.bNumConfigurations =
1114 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001115 cdev->desc.bMaxPacketSize0 =
1116 cdev->gadget->ep0->maxpacket;
1117 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001118 if (gadget->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001119 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001120 cdev->desc.bMaxPacketSize0 = 9;
1121 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001122 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001123 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001124 }
1125
David Brownell40982be2008-06-19 17:52:58 -07001126 value = min(w_length, (u16) sizeof cdev->desc);
1127 memcpy(req->buf, &cdev->desc, value);
1128 break;
1129 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001130 if (!gadget_is_dualspeed(gadget) ||
1131 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001132 break;
1133 device_qual(cdev);
1134 value = min_t(int, w_length,
1135 sizeof(struct usb_qualifier_descriptor));
1136 break;
1137 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-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 /* FALLTHROUGH */
1142 case USB_DT_CONFIG:
1143 value = config_desc(cdev, w_value);
1144 if (value >= 0)
1145 value = min(w_length, (u16) value);
1146 break;
1147 case USB_DT_STRING:
1148 value = get_string(cdev, req->buf,
1149 w_index, w_value & 0xff);
1150 if (value >= 0)
1151 value = min(w_length, (u16) value);
1152 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001153 case USB_DT_BOS:
1154 if (gadget_is_superspeed(gadget)) {
1155 value = bos_desc(cdev);
1156 value = min(w_length, (u16) value);
1157 }
1158 break;
David Brownell40982be2008-06-19 17:52:58 -07001159 }
1160 break;
1161
1162 /* any number of configs can work */
1163 case USB_REQ_SET_CONFIGURATION:
1164 if (ctrl->bRequestType != 0)
1165 goto unknown;
1166 if (gadget_is_otg(gadget)) {
1167 if (gadget->a_hnp_support)
1168 DBG(cdev, "HNP available\n");
1169 else if (gadget->a_alt_hnp_support)
1170 DBG(cdev, "HNP on another port\n");
1171 else
1172 VDBG(cdev, "HNP inactive\n");
1173 }
1174 spin_lock(&cdev->lock);
1175 value = set_config(cdev, ctrl, w_value);
1176 spin_unlock(&cdev->lock);
1177 break;
1178 case USB_REQ_GET_CONFIGURATION:
1179 if (ctrl->bRequestType != USB_DIR_IN)
1180 goto unknown;
1181 if (cdev->config)
1182 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1183 else
1184 *(u8 *)req->buf = 0;
1185 value = min(w_length, (u16) 1);
1186 break;
1187
1188 /* function drivers must handle get/set altsetting; if there's
1189 * no get() method, we know only altsetting zero works.
1190 */
1191 case USB_REQ_SET_INTERFACE:
1192 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1193 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001194 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001195 break;
Bryan Wu08889512009-01-08 00:21:19 +08001196 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001197 if (!f)
1198 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001199 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001200 break;
1201 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001202 if (value == USB_GADGET_DELAYED_STATUS) {
1203 DBG(cdev,
1204 "%s: interface %d (%s) requested delayed status\n",
1205 __func__, intf, f->name);
1206 cdev->delayed_status++;
1207 DBG(cdev, "delayed_status count %d\n",
1208 cdev->delayed_status);
1209 }
David Brownell40982be2008-06-19 17:52:58 -07001210 break;
1211 case USB_REQ_GET_INTERFACE:
1212 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1213 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001214 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001215 break;
Bryan Wu08889512009-01-08 00:21:19 +08001216 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001217 if (!f)
1218 break;
1219 /* lots of interfaces only need altsetting zero... */
1220 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1221 if (value < 0)
1222 break;
1223 *((u8 *)req->buf) = value;
1224 value = min(w_length, (u16) 1);
1225 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001226
1227 /*
1228 * USB 3.0 additions:
1229 * Function driver should handle get_status request. If such cb
1230 * wasn't supplied we respond with default value = 0
1231 * Note: function driver should supply such cb only for the first
1232 * interface of the function
1233 */
1234 case USB_REQ_GET_STATUS:
1235 if (!gadget_is_superspeed(gadget))
1236 goto unknown;
1237 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1238 goto unknown;
1239 value = 2; /* This is the length of the get_status reply */
1240 put_unaligned_le16(0, req->buf);
1241 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1242 break;
1243 f = cdev->config->interface[intf];
1244 if (!f)
1245 break;
1246 status = f->get_status ? f->get_status(f) : 0;
1247 if (status < 0)
1248 break;
1249 put_unaligned_le16(status & 0x0000ffff, req->buf);
1250 break;
1251 /*
1252 * Function drivers should handle SetFeature/ClearFeature
1253 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1254 * only for the first interface of the function
1255 */
1256 case USB_REQ_CLEAR_FEATURE:
1257 case USB_REQ_SET_FEATURE:
1258 if (!gadget_is_superspeed(gadget))
1259 goto unknown;
1260 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1261 goto unknown;
1262 switch (w_value) {
1263 case USB_INTRF_FUNC_SUSPEND:
1264 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1265 break;
1266 f = cdev->config->interface[intf];
1267 if (!f)
1268 break;
1269 value = 0;
1270 if (f->func_suspend)
1271 value = f->func_suspend(f, w_index >> 8);
1272 if (value < 0) {
1273 ERROR(cdev,
1274 "func_suspend() returned error %d\n",
1275 value);
1276 value = 0;
1277 }
1278 break;
1279 }
1280 break;
David Brownell40982be2008-06-19 17:52:58 -07001281 default:
1282unknown:
1283 VDBG(cdev,
1284 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1285 ctrl->bRequestType, ctrl->bRequest,
1286 w_value, w_index, w_length);
1287
Laurent Pinchart52426582009-10-21 00:03:38 +02001288 /* functions always handle their interfaces and endpoints...
1289 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001290 * configuration code.
1291 *
1292 * REVISIT it could make sense to let the composite device
1293 * take such requests too, if that's ever needed: to work
1294 * in config 0, etc.
1295 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001296 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1297 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001298 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301299 break;
1300 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001301 break;
1302
1303 case USB_RECIP_ENDPOINT:
1304 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1305 list_for_each_entry(f, &cdev->config->functions, list) {
1306 if (test_bit(endp, f->endpoints))
1307 break;
1308 }
1309 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001310 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001311 break;
David Brownell40982be2008-06-19 17:52:58 -07001312 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001313
1314 if (f && f->setup)
1315 value = f->setup(f, ctrl);
1316 else {
David Brownell40982be2008-06-19 17:52:58 -07001317 struct usb_configuration *c;
1318
1319 c = cdev->config;
1320 if (c && c->setup)
1321 value = c->setup(c, ctrl);
1322 }
1323
1324 goto done;
1325 }
1326
1327 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001328 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001329 req->length = value;
1330 req->zero = value < w_length;
1331 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1332 if (value < 0) {
1333 DBG(cdev, "ep_queue --> %d\n", value);
1334 req->status = 0;
1335 composite_setup_complete(gadget->ep0, req);
1336 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001337 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1338 WARN(cdev,
1339 "%s: Delayed status not supported for w_length != 0",
1340 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001341 }
1342
1343done:
1344 /* device either stalls (value < 0) or reports success */
1345 return value;
1346}
1347
1348static void composite_disconnect(struct usb_gadget *gadget)
1349{
1350 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1351 unsigned long flags;
1352
1353 /* REVISIT: should we have config and device level
1354 * disconnect callbacks?
1355 */
1356 spin_lock_irqsave(&cdev->lock, flags);
1357 if (cdev->config)
1358 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001359 if (cdev->driver->disconnect)
1360 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001361 spin_unlock_irqrestore(&cdev->lock, flags);
1362}
1363
1364/*-------------------------------------------------------------------------*/
1365
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001366static ssize_t composite_show_suspended(struct device *dev,
1367 struct device_attribute *attr,
1368 char *buf)
1369{
1370 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1371 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1372
1373 return sprintf(buf, "%d\n", cdev->suspended);
1374}
1375
1376static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1377
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001378static void
David Brownell40982be2008-06-19 17:52:58 -07001379composite_unbind(struct usb_gadget *gadget)
1380{
1381 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1382
1383 /* composite_disconnect() must already have been called
1384 * by the underlying peripheral controller driver!
1385 * so there's no i/o concurrency that could affect the
1386 * state protected by cdev->lock.
1387 */
1388 WARN_ON(cdev->config);
1389
1390 while (!list_empty(&cdev->configs)) {
1391 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001392 c = list_first_entry(&cdev->configs,
1393 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001394 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001395 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001396 if (cdev->driver->unbind)
1397 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001398
1399 if (cdev->req) {
1400 kfree(cdev->req->buf);
1401 usb_ep_free_request(gadget->ep0, cdev->req);
1402 }
Pavankumar Kondetidaba5802010-12-16 14:32:25 +05301403 device_remove_file(&gadget->dev, &dev_attr_suspended);
David Brownell40982be2008-06-19 17:52:58 -07001404 kfree(cdev);
1405 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07001406}
1407
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001408static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
David Brownell40982be2008-06-19 17:52:58 -07001409{
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001410 if (!*desc) {
1411 int ret = usb_string_id(cdev);
1412 if (unlikely(ret < 0))
1413 WARNING(cdev, "failed to override string ID\n");
1414 else
1415 *desc = ret;
David Brownell40982be2008-06-19 17:52:58 -07001416 }
David Brownell40982be2008-06-19 17:52:58 -07001417
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001418 return *desc;
David Brownell40982be2008-06-19 17:52:58 -07001419}
1420
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001421static struct usb_composite_driver *to_cdriver(struct usb_gadget_driver *gdrv)
1422{
1423 return container_of(gdrv, struct usb_composite_driver, gadget_driver);
1424}
1425
1426static int composite_bind(struct usb_gadget *gadget,
1427 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07001428{
1429 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001430 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07001431 int status = -ENOMEM;
1432
1433 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1434 if (!cdev)
1435 return status;
1436
1437 spin_lock_init(&cdev->lock);
1438 cdev->gadget = gadget;
1439 set_gadget_data(gadget, cdev);
1440 INIT_LIST_HEAD(&cdev->configs);
1441
1442 /* preallocate control response and buffer */
1443 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1444 if (!cdev->req)
1445 goto fail;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +02001446 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
David Brownell40982be2008-06-19 17:52:58 -07001447 if (!cdev->req->buf)
1448 goto fail;
1449 cdev->req->complete = composite_setup_complete;
1450 gadget->ep0->driver_data = cdev;
1451
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +02001452 cdev->bufsiz = USB_COMP_EP0_BUFSIZ;
David Brownell40982be2008-06-19 17:52:58 -07001453 cdev->driver = composite;
1454
Parirajan Muthalagu37b58012010-08-25 16:33:26 +05301455 /*
1456 * As per USB compliance update, a device that is actively drawing
1457 * more than 100mA from USB must report itself as bus-powered in
1458 * the GetStatus(DEVICE) call.
1459 */
1460 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1461 usb_gadget_set_selfpowered(gadget);
David Brownell40982be2008-06-19 17:52:58 -07001462
1463 /* interface and string IDs start at zero via kzalloc.
1464 * we force endpoints to start unassigned; few controller
1465 * drivers will zero ep->driver_data.
1466 */
1467 usb_ep_autoconfig_reset(cdev->gadget);
1468
1469 /* composite gadget needs to assign strings for whole device (like
1470 * serial number), register function drivers, potentially update
1471 * power state and consumption, etc
1472 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02001473 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001474 if (status < 0)
1475 goto fail;
1476
1477 cdev->desc = *composite->dev;
David Brownell40982be2008-06-19 17:52:58 -07001478
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001479 /* standardized runtime overrides for device ID data */
1480 if (idVendor)
1481 cdev->desc.idVendor = cpu_to_le16(idVendor);
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +02001482 else
1483 idVendor = le16_to_cpu(cdev->desc.idVendor);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001484 if (idProduct)
1485 cdev->desc.idProduct = cpu_to_le16(idProduct);
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +02001486 else
1487 idProduct = le16_to_cpu(cdev->desc.idProduct);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001488 if (bcdDevice)
1489 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
Andrzej Pietrasiewicz4fffd6e52012-05-10 10:07:59 +02001490 else
1491 bcdDevice = le16_to_cpu(cdev->desc.bcdDevice);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001492
Marek Belisko78bff3c2010-10-27 10:19:01 +02001493 /* string overrides */
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001494 if (iManufacturer || !cdev->desc.iManufacturer) {
1495 if (!iManufacturer && !composite->iManufacturer &&
1496 !*composite_manufacturer)
1497 snprintf(composite_manufacturer,
1498 sizeof composite_manufacturer,
1499 "%s %s with %s",
1500 init_utsname()->sysname,
1501 init_utsname()->release,
1502 gadget->name);
David Brownell40982be2008-06-19 17:52:58 -07001503
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001504 cdev->manufacturer_override =
1505 override_id(cdev, &cdev->desc.iManufacturer);
1506 }
1507
1508 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1509 cdev->product_override =
1510 override_id(cdev, &cdev->desc.iProduct);
1511
Andrzej Pietrasiewiczcad4cd82012-05-10 10:08:00 +02001512 if (iSerialNumber ||
1513 (!cdev->desc.iSerialNumber && composite->iSerialNumber))
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001514 cdev->serial_override =
1515 override_id(cdev, &cdev->desc.iSerialNumber);
1516
1517 /* has userspace failed to provide a serial number? */
1518 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1519 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1520
1521 /* finish up */
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001522 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1523 if (status)
1524 goto fail;
1525
David Brownell40982be2008-06-19 17:52:58 -07001526 INFO(cdev, "%s ready\n", composite->name);
1527 return 0;
1528
1529fail:
1530 composite_unbind(gadget);
1531 return status;
1532}
1533
1534/*-------------------------------------------------------------------------*/
1535
1536static void
1537composite_suspend(struct usb_gadget *gadget)
1538{
1539 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1540 struct usb_function *f;
1541
David Brownell89429392009-03-19 14:14:17 -07001542 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001543 * suspend/resume callbacks?
1544 */
1545 DBG(cdev, "suspend\n");
1546 if (cdev->config) {
1547 list_for_each_entry(f, &cdev->config->functions, list) {
1548 if (f->suspend)
1549 f->suspend(f);
1550 }
1551 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001552 if (cdev->driver->suspend)
1553 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001554
1555 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001556
1557 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001558}
1559
1560static void
1561composite_resume(struct usb_gadget *gadget)
1562{
1563 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1564 struct usb_function *f;
Hao Wub23f2f92010-11-29 15:17:03 +08001565 u8 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001566
David Brownell89429392009-03-19 14:14:17 -07001567 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001568 * suspend/resume callbacks?
1569 */
1570 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001571 if (cdev->driver->resume)
1572 cdev->driver->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001573 if (cdev->config) {
1574 list_for_each_entry(f, &cdev->config->functions, list) {
1575 if (f->resume)
1576 f->resume(f);
1577 }
Hao Wub23f2f92010-11-29 15:17:03 +08001578
1579 maxpower = cdev->config->bMaxPower;
1580
1581 usb_gadget_vbus_draw(gadget, maxpower ?
1582 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001583 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001584
1585 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001586}
1587
1588/*-------------------------------------------------------------------------*/
1589
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001590static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001591 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001592 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001593
1594 .setup = composite_setup,
1595 .disconnect = composite_disconnect,
1596
1597 .suspend = composite_suspend,
1598 .resume = composite_resume,
1599
1600 .driver = {
1601 .owner = THIS_MODULE,
1602 },
1603};
1604
1605/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001606 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001607 * @driver: the driver to register
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001608 * @bind: the callback used to allocate resources that are shared across the
1609 * whole device, such as string IDs, and add its configurations using
1610 * @usb_add_config(). This may fail by returning a negative errno
1611 * value; it should return zero on successful initialization.
David Brownell40982be2008-06-19 17:52:58 -07001612 * Context: single threaded during gadget setup
1613 *
1614 * This function is used to register drivers using the composite driver
1615 * framework. The return value is zero, or a negative errno value.
1616 * Those values normally come from the driver's @bind method, which does
1617 * all the work of setting up the driver to match the hardware.
1618 *
1619 * On successful return, the gadget is ready to respond to requests from
1620 * the host, unless one of its components invokes usb_gadget_disconnect()
1621 * while it was binding. That would usually be done in order to wait for
1622 * some userspace participation.
1623 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02001624int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001625{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001626 struct usb_gadget_driver *gadget_driver;
1627
1628 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07001629 return -EINVAL;
1630
1631 if (!driver->name)
1632 driver->name = "composite";
Jassi Brar05c3eeb2011-02-06 18:47:18 +09001633 if (!driver->iProduct)
1634 driver->iProduct = driver->name;
David Brownell40982be2008-06-19 17:52:58 -07001635
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001636 driver->gadget_driver = composite_driver_template;
1637 gadget_driver = &driver->gadget_driver;
1638
1639 gadget_driver->function = (char *) driver->name;
1640 gadget_driver->driver.name = driver->name;
1641 gadget_driver->max_speed = driver->max_speed;
1642
1643 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07001644}
1645
1646/**
1647 * usb_composite_unregister() - unregister a composite driver
1648 * @driver: the driver to unregister
1649 *
1650 * This function is used to unregister drivers using the composite
1651 * driver framework.
1652 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001653void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001654{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001655 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07001656}
Roger Quadros1b9ba002011-05-09 13:08:06 +03001657
1658/**
1659 * usb_composite_setup_continue() - Continue with the control transfer
1660 * @cdev: the composite device who's control transfer was kept waiting
1661 *
1662 * This function must be called by the USB function driver to continue
1663 * with the control transfer's data/status stage in case it had requested to
1664 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1665 * can request the composite framework to delay the setup request's data/status
1666 * stages by returning USB_GADGET_DELAYED_STATUS.
1667 */
1668void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1669{
1670 int value;
1671 struct usb_request *req = cdev->req;
1672 unsigned long flags;
1673
1674 DBG(cdev, "%s\n", __func__);
1675 spin_lock_irqsave(&cdev->lock, flags);
1676
1677 if (cdev->delayed_status == 0) {
1678 WARN(cdev, "%s: Unexpected call\n", __func__);
1679
1680 } else if (--cdev->delayed_status == 0) {
1681 DBG(cdev, "%s: Completing delayed status\n", __func__);
1682 req->length = 0;
1683 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1684 if (value < 0) {
1685 DBG(cdev, "ep_queue --> %d\n", value);
1686 req->status = 0;
1687 composite_setup_complete(cdev->gadget->ep0, req);
1688 }
1689 }
1690
1691 spin_unlock_irqrestore(&cdev->lock, flags);
1692}
1693