blob: 2f87b1697bf53f790656123c7bec4f2d8c83e0c8 [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
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +020024/**
25 * struct usb_os_string - represents OS String to be reported by a gadget
26 * @bLength: total length of the entire descritor, always 0x12
27 * @bDescriptorType: USB_DT_STRING
28 * @qwSignature: the OS String proper
29 * @bMS_VendorCode: code used by the host for subsequent requests
30 * @bPad: not used, must be zero
31 */
32struct usb_os_string {
33 __u8 bLength;
34 __u8 bDescriptorType;
35 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
36 __u8 bMS_VendorCode;
37 __u8 bPad;
38} __packed;
39
David Brownell40982be2008-06-19 17:52:58 -070040/*
41 * The code in this file is utility code, used to build a gadget driver
42 * from one or more "function" drivers, one or more "configuration"
43 * objects, and a "usb_composite_driver" by gluing them together along
44 * with the relevant device-wide data.
45 */
46
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +010047static struct usb_gadget_strings **get_containers_gs(
48 struct usb_gadget_string_container *uc)
49{
50 return (struct usb_gadget_strings **)uc->stash;
51}
52
Tatyana Brokhman48767a42011-06-28 16:33:49 +030053/**
54 * next_ep_desc() - advance to the next EP descriptor
55 * @t: currect pointer within descriptor array
56 *
57 * Return: next EP descriptor or NULL
58 *
59 * Iterate over @t until either EP descriptor found or
60 * NULL (that indicates end of list) encountered
61 */
62static struct usb_descriptor_header**
63next_ep_desc(struct usb_descriptor_header **t)
64{
65 for (; *t; t++) {
66 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
67 return t;
68 }
69 return NULL;
70}
71
72/*
73 * for_each_ep_desc()- iterate over endpoint descriptors in the
74 * descriptors list
75 * @start: pointer within descriptor array.
76 * @ep_desc: endpoint descriptor to use as the loop cursor
77 */
78#define for_each_ep_desc(start, ep_desc) \
79 for (ep_desc = next_ep_desc(start); \
80 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
81
82/**
83 * config_ep_by_speed() - configures the given endpoint
84 * according to gadget speed.
85 * @g: pointer to the gadget
86 * @f: usb function
87 * @_ep: the endpoint to configure
88 *
89 * Return: error code, 0 on success
90 *
91 * This function chooses the right descriptors for a given
92 * endpoint according to gadget speed and saves it in the
93 * endpoint desc field. If the endpoint already has a descriptor
94 * assigned to it - overwrites it with currently corresponding
95 * descriptor. The endpoint maxpacket field is updated according
96 * to the chosen descriptor.
97 * Note: the supplied function should hold all the descriptors
98 * for supported speeds
99 */
100int config_ep_by_speed(struct usb_gadget *g,
101 struct usb_function *f,
102 struct usb_ep *_ep)
103{
Felipe Balbib785ea72012-06-06 10:20:23 +0300104 struct usb_composite_dev *cdev = get_gadget_data(g);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300105 struct usb_endpoint_descriptor *chosen_desc = NULL;
106 struct usb_descriptor_header **speed_desc = NULL;
107
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300108 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
109 int want_comp_desc = 0;
110
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300111 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
112
113 if (!g || !f || !_ep)
114 return -EIO;
115
116 /* select desired speed */
117 switch (g->speed) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300118 case USB_SPEED_SUPER:
119 if (gadget_is_superspeed(g)) {
120 speed_desc = f->ss_descriptors;
121 want_comp_desc = 1;
122 break;
123 }
124 /* else: Fall trough */
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300125 case USB_SPEED_HIGH:
126 if (gadget_is_dualspeed(g)) {
127 speed_desc = f->hs_descriptors;
128 break;
129 }
130 /* else: fall through */
131 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200132 speed_desc = f->fs_descriptors;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300133 }
134 /* find descriptors */
135 for_each_ep_desc(speed_desc, d_spd) {
136 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
137 if (chosen_desc->bEndpointAddress == _ep->address)
138 goto ep_found;
139 }
140 return -EIO;
141
142ep_found:
143 /* commit results */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700144 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300145 _ep->desc = chosen_desc;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300146 _ep->comp_desc = NULL;
147 _ep->maxburst = 0;
148 _ep->mult = 0;
149 if (!want_comp_desc)
150 return 0;
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300151
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300152 /*
153 * Companion descriptor should follow EP descriptor
154 * USB 3.0 spec, #9.6.7
155 */
156 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
157 if (!comp_desc ||
158 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
159 return -EIO;
160 _ep->comp_desc = comp_desc;
161 if (g->speed == USB_SPEED_SUPER) {
162 switch (usb_endpoint_type(_ep->desc)) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300163 case USB_ENDPOINT_XFER_ISOC:
164 /* mult: bits 1:0 of bmAttributes */
165 _ep->mult = comp_desc->bmAttributes & 0x3;
Paul Zimmerman9e878a62012-01-16 13:24:38 -0800166 case USB_ENDPOINT_XFER_BULK:
167 case USB_ENDPOINT_XFER_INT:
Felipe Balbib785ea72012-06-06 10:20:23 +0300168 _ep->maxburst = comp_desc->bMaxBurst + 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300169 break;
170 default:
Felipe Balbib785ea72012-06-06 10:20:23 +0300171 if (comp_desc->bMaxBurst != 0)
172 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
173 _ep->maxburst = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300174 break;
175 }
176 }
Tatyana Brokhman48767a42011-06-28 16:33:49 +0300177 return 0;
178}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200179EXPORT_SYMBOL_GPL(config_ep_by_speed);
David Brownell40982be2008-06-19 17:52:58 -0700180
181/**
182 * usb_add_function() - add a function to a configuration
183 * @config: the configuration
184 * @function: the function being added
185 * Context: single threaded during gadget setup
186 *
187 * After initialization, each configuration must have one or more
188 * functions added to it. Adding a function involves calling its @bind()
189 * method to allocate resources such as interface and string identifiers
190 * and endpoints.
191 *
192 * This function returns the value of the function's bind(), which is
193 * zero for success else a negative errno value.
194 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200195int usb_add_function(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700196 struct usb_function *function)
197{
198 int value = -EINVAL;
199
200 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
201 function->name, function,
202 config->label, config);
203
204 if (!function->set_alt || !function->disable)
205 goto done;
206
207 function->config = config;
208 list_add_tail(&function->list, &config->functions);
209
210 /* REVISIT *require* function->bind? */
211 if (function->bind) {
212 value = function->bind(config, function);
213 if (value < 0) {
214 list_del(&function->list);
215 function->config = NULL;
216 }
217 } else
218 value = 0;
219
220 /* We allow configurations that don't work at both speeds.
221 * If we run into a lowspeed Linux system, treat it the same
222 * as full speed ... it's the function drivers that will need
223 * to avoid bulk and ISO transfers.
224 */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200225 if (!config->fullspeed && function->fs_descriptors)
David Brownell40982be2008-06-19 17:52:58 -0700226 config->fullspeed = true;
227 if (!config->highspeed && function->hs_descriptors)
228 config->highspeed = true;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300229 if (!config->superspeed && function->ss_descriptors)
230 config->superspeed = true;
David Brownell40982be2008-06-19 17:52:58 -0700231
232done:
233 if (value)
234 DBG(config->cdev, "adding '%s'/%p --> %d\n",
235 function->name, function, value);
236 return value;
237}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200238EXPORT_SYMBOL_GPL(usb_add_function);
David Brownell40982be2008-06-19 17:52:58 -0700239
Sebastian Andrzej Siewiorb47357782012-12-23 21:10:05 +0100240void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
241{
242 if (f->disable)
243 f->disable(f);
244
245 bitmap_zero(f->endpoints, 32);
246 list_del(&f->list);
247 if (f->unbind)
248 f->unbind(c, f);
249}
250EXPORT_SYMBOL_GPL(usb_remove_function);
251
David Brownell40982be2008-06-19 17:52:58 -0700252/**
David Brownell60beed92008-08-18 17:38:22 -0700253 * usb_function_deactivate - prevent function and gadget enumeration
254 * @function: the function that isn't yet ready to respond
255 *
256 * Blocks response of the gadget driver to host enumeration by
257 * preventing the data line pullup from being activated. This is
258 * normally called during @bind() processing to change from the
259 * initial "ready to respond" state, or when a required resource
260 * becomes available.
261 *
262 * For example, drivers that serve as a passthrough to a userspace
263 * daemon can block enumeration unless that daemon (such as an OBEX,
264 * MTP, or print server) is ready to handle host requests.
265 *
266 * Not all systems support software control of their USB peripheral
267 * data pullups.
268 *
269 * Returns zero on success, else negative errno.
270 */
271int usb_function_deactivate(struct usb_function *function)
272{
273 struct usb_composite_dev *cdev = function->config->cdev;
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200274 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700275 int status = 0;
276
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200277 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700278
279 if (cdev->deactivations == 0)
280 status = usb_gadget_disconnect(cdev->gadget);
281 if (status == 0)
282 cdev->deactivations++;
283
Felipe Balbib2bdf3a2009-02-12 15:09:47 +0200284 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700285 return status;
286}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200287EXPORT_SYMBOL_GPL(usb_function_deactivate);
David Brownell60beed92008-08-18 17:38:22 -0700288
289/**
290 * usb_function_activate - allow function and gadget enumeration
291 * @function: function on which usb_function_activate() was called
292 *
293 * Reverses effect of usb_function_deactivate(). If no more functions
294 * are delaying their activation, the gadget driver will respond to
295 * host enumeration procedures.
296 *
297 * Returns zero on success, else negative errno.
298 */
299int usb_function_activate(struct usb_function *function)
300{
301 struct usb_composite_dev *cdev = function->config->cdev;
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200302 unsigned long flags;
David Brownell60beed92008-08-18 17:38:22 -0700303 int status = 0;
304
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200305 spin_lock_irqsave(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700306
307 if (WARN_ON(cdev->deactivations == 0))
308 status = -EINVAL;
309 else {
310 cdev->deactivations--;
311 if (cdev->deactivations == 0)
312 status = usb_gadget_connect(cdev->gadget);
313 }
314
Michael Grzeschik4fefe9f62012-07-19 00:20:11 +0200315 spin_unlock_irqrestore(&cdev->lock, flags);
David Brownell60beed92008-08-18 17:38:22 -0700316 return status;
317}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200318EXPORT_SYMBOL_GPL(usb_function_activate);
David Brownell60beed92008-08-18 17:38:22 -0700319
320/**
David Brownell40982be2008-06-19 17:52:58 -0700321 * usb_interface_id() - allocate an unused interface ID
322 * @config: configuration associated with the interface
323 * @function: function handling the interface
324 * Context: single threaded during gadget setup
325 *
326 * usb_interface_id() is called from usb_function.bind() callbacks to
327 * allocate new interface IDs. The function driver will then store that
328 * ID in interface, association, CDC union, and other descriptors. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300329 * will also handle any control requests targeted at that interface,
David Brownell40982be2008-06-19 17:52:58 -0700330 * particularly changing its altsetting via set_alt(). There may
331 * also be class-specific or vendor-specific requests to handle.
332 *
333 * All interface identifier should be allocated using this routine, to
334 * ensure that for example different functions don't wrongly assign
335 * different meanings to the same identifier. Note that since interface
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300336 * identifiers are configuration-specific, functions used in more than
David Brownell40982be2008-06-19 17:52:58 -0700337 * one configuration (or more than once in a given configuration) need
338 * multiple versions of the relevant descriptors.
339 *
340 * Returns the interface ID which was allocated; or -ENODEV if no
341 * more interface IDs can be allocated.
342 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200343int usb_interface_id(struct usb_configuration *config,
David Brownell40982be2008-06-19 17:52:58 -0700344 struct usb_function *function)
345{
346 unsigned id = config->next_interface_id;
347
348 if (id < MAX_CONFIG_INTERFACES) {
349 config->interface[id] = function;
350 config->next_interface_id = id + 1;
351 return id;
352 }
353 return -ENODEV;
354}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200355EXPORT_SYMBOL_GPL(usb_interface_id);
David Brownell40982be2008-06-19 17:52:58 -0700356
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100357static u8 encode_bMaxPower(enum usb_device_speed speed,
358 struct usb_configuration *c)
359{
360 unsigned val;
361
362 if (c->MaxPower)
363 val = c->MaxPower;
364 else
365 val = CONFIG_USB_GADGET_VBUS_DRAW;
366 if (!val)
367 return 0;
368 switch (speed) {
369 case USB_SPEED_SUPER:
370 return DIV_ROUND_UP(val, 8);
371 default:
372 return DIV_ROUND_UP(val, 2);
Joe Perches2b84f922013-10-08 16:01:37 -0700373 }
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100374}
375
David Brownell40982be2008-06-19 17:52:58 -0700376static int config_buf(struct usb_configuration *config,
377 enum usb_device_speed speed, void *buf, u8 type)
378{
379 struct usb_config_descriptor *c = buf;
380 void *next = buf + USB_DT_CONFIG_SIZE;
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200381 int len;
David Brownell40982be2008-06-19 17:52:58 -0700382 struct usb_function *f;
383 int status;
384
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200385 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
David Brownell40982be2008-06-19 17:52:58 -0700386 /* write the config descriptor */
387 c = buf;
388 c->bLength = USB_DT_CONFIG_SIZE;
389 c->bDescriptorType = type;
390 /* wTotalLength is written later */
391 c->bNumInterfaces = config->next_interface_id;
392 c->bConfigurationValue = config->bConfigurationValue;
393 c->iConfiguration = config->iConfiguration;
394 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100395 c->bMaxPower = encode_bMaxPower(speed, config);
David Brownell40982be2008-06-19 17:52:58 -0700396
397 /* There may be e.g. OTG descriptors */
398 if (config->descriptors) {
399 status = usb_descriptor_fillbuf(next, len,
400 config->descriptors);
401 if (status < 0)
402 return status;
403 len -= status;
404 next += status;
405 }
406
407 /* add each function's descriptors */
408 list_for_each_entry(f, &config->functions, list) {
409 struct usb_descriptor_header **descriptors;
410
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300411 switch (speed) {
412 case USB_SPEED_SUPER:
413 descriptors = f->ss_descriptors;
414 break;
415 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700416 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300417 break;
418 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200419 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300420 }
421
David Brownell40982be2008-06-19 17:52:58 -0700422 if (!descriptors)
423 continue;
424 status = usb_descriptor_fillbuf(next, len,
425 (const struct usb_descriptor_header **) descriptors);
426 if (status < 0)
427 return status;
428 len -= status;
429 next += status;
430 }
431
432 len = next - buf;
433 c->wTotalLength = cpu_to_le16(len);
434 return len;
435}
436
437static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
438{
439 struct usb_gadget *gadget = cdev->gadget;
440 struct usb_configuration *c;
441 u8 type = w_value >> 8;
442 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
443
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300444 if (gadget->speed == USB_SPEED_SUPER)
445 speed = gadget->speed;
446 else if (gadget_is_dualspeed(gadget)) {
447 int hs = 0;
David Brownell40982be2008-06-19 17:52:58 -0700448 if (gadget->speed == USB_SPEED_HIGH)
449 hs = 1;
450 if (type == USB_DT_OTHER_SPEED_CONFIG)
451 hs = !hs;
452 if (hs)
453 speed = USB_SPEED_HIGH;
454
455 }
456
457 /* This is a lookup by config *INDEX* */
458 w_value &= 0xff;
459 list_for_each_entry(c, &cdev->configs, list) {
460 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300461 switch (speed) {
462 case USB_SPEED_SUPER:
463 if (!c->superspeed)
464 continue;
465 break;
466 case USB_SPEED_HIGH:
David Brownell40982be2008-06-19 17:52:58 -0700467 if (!c->highspeed)
468 continue;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300469 break;
470 default:
David Brownell40982be2008-06-19 17:52:58 -0700471 if (!c->fullspeed)
472 continue;
473 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300474
David Brownell40982be2008-06-19 17:52:58 -0700475 if (w_value == 0)
476 return config_buf(c, speed, cdev->req->buf, type);
477 w_value--;
478 }
479 return -EINVAL;
480}
481
482static int count_configs(struct usb_composite_dev *cdev, unsigned type)
483{
484 struct usb_gadget *gadget = cdev->gadget;
485 struct usb_configuration *c;
486 unsigned count = 0;
487 int hs = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300488 int ss = 0;
David Brownell40982be2008-06-19 17:52:58 -0700489
490 if (gadget_is_dualspeed(gadget)) {
491 if (gadget->speed == USB_SPEED_HIGH)
492 hs = 1;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300493 if (gadget->speed == USB_SPEED_SUPER)
494 ss = 1;
David Brownell40982be2008-06-19 17:52:58 -0700495 if (type == USB_DT_DEVICE_QUALIFIER)
496 hs = !hs;
497 }
498 list_for_each_entry(c, &cdev->configs, list) {
499 /* ignore configs that won't work at this speed */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300500 if (ss) {
501 if (!c->superspeed)
502 continue;
503 } else if (hs) {
David Brownell40982be2008-06-19 17:52:58 -0700504 if (!c->highspeed)
505 continue;
506 } else {
507 if (!c->fullspeed)
508 continue;
509 }
510 count++;
511 }
512 return count;
513}
514
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300515/**
516 * bos_desc() - prepares the BOS descriptor.
517 * @cdev: pointer to usb_composite device to generate the bos
518 * descriptor for
519 *
520 * This function generates the BOS (Binary Device Object)
521 * descriptor and its device capabilities descriptors. The BOS
522 * descriptor should be supported by a SuperSpeed device.
523 */
524static int bos_desc(struct usb_composite_dev *cdev)
525{
526 struct usb_ext_cap_descriptor *usb_ext;
527 struct usb_ss_cap_descriptor *ss_cap;
528 struct usb_dcd_config_params dcd_config_params;
529 struct usb_bos_descriptor *bos = cdev->req->buf;
530
531 bos->bLength = USB_DT_BOS_SIZE;
532 bos->bDescriptorType = USB_DT_BOS;
533
534 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
535 bos->bNumDeviceCaps = 0;
536
537 /*
538 * A SuperSpeed device shall include the USB2.0 extension descriptor
539 * and shall support LPM when operating in USB2.0 HS mode.
540 */
541 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
542 bos->bNumDeviceCaps++;
543 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
544 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
545 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
546 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
547 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
548
549 /*
550 * The Superspeed USB Capability descriptor shall be implemented by all
551 * SuperSpeed devices.
552 */
553 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
554 bos->bNumDeviceCaps++;
555 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
556 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
557 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
558 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
559 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
560 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
561 USB_FULL_SPEED_OPERATION |
562 USB_HIGH_SPEED_OPERATION |
563 USB_5GBPS_OPERATION);
564 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
565
566 /* Get Controller configuration */
567 if (cdev->gadget->ops->get_config_params)
568 cdev->gadget->ops->get_config_params(&dcd_config_params);
569 else {
Felipe Balbi089b8372011-10-10 09:43:44 +0300570 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300571 dcd_config_params.bU2DevExitLat =
Felipe Balbi089b8372011-10-10 09:43:44 +0300572 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300573 }
574 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
575 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
576
577 return le16_to_cpu(bos->wTotalLength);
578}
579
David Brownell40982be2008-06-19 17:52:58 -0700580static void device_qual(struct usb_composite_dev *cdev)
581{
582 struct usb_qualifier_descriptor *qual = cdev->req->buf;
583
584 qual->bLength = sizeof(*qual);
585 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
586 /* POLICY: same bcdUSB and device type info at both speeds */
587 qual->bcdUSB = cdev->desc.bcdUSB;
588 qual->bDeviceClass = cdev->desc.bDeviceClass;
589 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
590 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
591 /* ASSUME same EP0 fifo size at both speeds */
Sebastian Andrzej Siewior765f5b82011-06-23 14:26:11 +0200592 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
David Brownell40982be2008-06-19 17:52:58 -0700593 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
David Lopoc24f4222008-07-01 13:14:17 -0700594 qual->bRESERVED = 0;
David Brownell40982be2008-06-19 17:52:58 -0700595}
596
597/*-------------------------------------------------------------------------*/
598
599static void reset_config(struct usb_composite_dev *cdev)
600{
601 struct usb_function *f;
602
603 DBG(cdev, "reset config\n");
604
605 list_for_each_entry(f, &cdev->config->functions, list) {
606 if (f->disable)
607 f->disable(f);
Laurent Pinchart52426582009-10-21 00:03:38 +0200608
609 bitmap_zero(f->endpoints, 32);
David Brownell40982be2008-06-19 17:52:58 -0700610 }
611 cdev->config = NULL;
Michael Grzeschik2bac51a2013-11-11 23:43:32 +0100612 cdev->delayed_status = 0;
David Brownell40982be2008-06-19 17:52:58 -0700613}
614
615static int set_config(struct usb_composite_dev *cdev,
616 const struct usb_ctrlrequest *ctrl, unsigned number)
617{
618 struct usb_gadget *gadget = cdev->gadget;
619 struct usb_configuration *c = NULL;
620 int result = -EINVAL;
621 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
622 int tmp;
623
David Brownell40982be2008-06-19 17:52:58 -0700624 if (number) {
625 list_for_each_entry(c, &cdev->configs, list) {
626 if (c->bConfigurationValue == number) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300627 /*
628 * We disable the FDs of the previous
629 * configuration only if the new configuration
630 * is a valid one
631 */
632 if (cdev->config)
633 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700634 result = 0;
635 break;
636 }
637 }
638 if (result < 0)
639 goto done;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300640 } else { /* Zero configuration value - need to reset the config */
641 if (cdev->config)
642 reset_config(cdev);
David Brownell40982be2008-06-19 17:52:58 -0700643 result = 0;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300644 }
David Brownell40982be2008-06-19 17:52:58 -0700645
Michal Nazarewicze538dfd2011-08-30 17:11:19 +0200646 INFO(cdev, "%s config #%d: %s\n",
647 usb_speed_string(gadget->speed),
648 number, c ? c->label : "unconfigured");
David Brownell40982be2008-06-19 17:52:58 -0700649
650 if (!c)
651 goto done;
652
Peter Chen6027f312014-04-29 13:26:28 +0800653 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
David Brownell40982be2008-06-19 17:52:58 -0700654 cdev->config = c;
655
656 /* Initialize all interfaces by setting them to altsetting zero. */
657 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
658 struct usb_function *f = c->interface[tmp];
Laurent Pinchart52426582009-10-21 00:03:38 +0200659 struct usb_descriptor_header **descriptors;
David Brownell40982be2008-06-19 17:52:58 -0700660
661 if (!f)
662 break;
663
Laurent Pinchart52426582009-10-21 00:03:38 +0200664 /*
665 * Record which endpoints are used by the function. This is used
666 * to dispatch control requests targeted at that endpoint to the
667 * function's setup callback instead of the current
668 * configuration's setup callback.
669 */
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300670 switch (gadget->speed) {
671 case USB_SPEED_SUPER:
672 descriptors = f->ss_descriptors;
673 break;
674 case USB_SPEED_HIGH:
Laurent Pinchart52426582009-10-21 00:03:38 +0200675 descriptors = f->hs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300676 break;
677 default:
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200678 descriptors = f->fs_descriptors;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300679 }
Laurent Pinchart52426582009-10-21 00:03:38 +0200680
681 for (; *descriptors; ++descriptors) {
682 struct usb_endpoint_descriptor *ep;
683 int addr;
684
685 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
686 continue;
687
688 ep = (struct usb_endpoint_descriptor *)*descriptors;
689 addr = ((ep->bEndpointAddress & 0x80) >> 3)
690 | (ep->bEndpointAddress & 0x0f);
691 set_bit(addr, f->endpoints);
692 }
693
David Brownell40982be2008-06-19 17:52:58 -0700694 result = f->set_alt(f, tmp, 0);
695 if (result < 0) {
696 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
697 tmp, f->name, f, result);
698
699 reset_config(cdev);
700 goto done;
701 }
Roger Quadros1b9ba002011-05-09 13:08:06 +0300702
703 if (result == USB_GADGET_DELAYED_STATUS) {
704 DBG(cdev,
705 "%s: interface %d (%s) requested delayed status\n",
706 __func__, tmp, f->name);
707 cdev->delayed_status++;
708 DBG(cdev, "delayed_status count %d\n",
709 cdev->delayed_status);
710 }
David Brownell40982be2008-06-19 17:52:58 -0700711 }
712
713 /* when we return, be sure our power usage is valid */
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +0100714 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
David Brownell40982be2008-06-19 17:52:58 -0700715done:
716 usb_gadget_vbus_draw(gadget, power);
Roger Quadros1b9ba002011-05-09 13:08:06 +0300717 if (result >= 0 && cdev->delayed_status)
718 result = USB_GADGET_DELAYED_STATUS;
David Brownell40982be2008-06-19 17:52:58 -0700719 return result;
720}
721
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100722int usb_add_config_only(struct usb_composite_dev *cdev,
723 struct usb_configuration *config)
724{
725 struct usb_configuration *c;
726
727 if (!config->bConfigurationValue)
728 return -EINVAL;
729
730 /* Prevent duplicate configuration identifiers */
731 list_for_each_entry(c, &cdev->configs, list) {
732 if (c->bConfigurationValue == config->bConfigurationValue)
733 return -EBUSY;
734 }
735
736 config->cdev = cdev;
737 list_add_tail(&config->list, &cdev->configs);
738
739 INIT_LIST_HEAD(&config->functions);
740 config->next_interface_id = 0;
741 memset(config->interface, 0, sizeof(config->interface));
742
743 return 0;
744}
745EXPORT_SYMBOL_GPL(usb_add_config_only);
746
David Brownell40982be2008-06-19 17:52:58 -0700747/**
748 * usb_add_config() - add a configuration to a device.
749 * @cdev: wraps the USB gadget
750 * @config: the configuration, with bConfigurationValue assigned
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200751 * @bind: the configuration's bind function
David Brownell40982be2008-06-19 17:52:58 -0700752 * Context: single threaded during gadget setup
753 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200754 * One of the main tasks of a composite @bind() routine is to
David Brownell40982be2008-06-19 17:52:58 -0700755 * add each of the configurations it supports, using this routine.
756 *
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200757 * This function returns the value of the configuration's @bind(), which
David Brownell40982be2008-06-19 17:52:58 -0700758 * is zero for success else a negative errno value. Binding configurations
759 * assigns global resources including string IDs, and per-configuration
760 * resources such as interface IDs and endpoints.
761 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +0200762int usb_add_config(struct usb_composite_dev *cdev,
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200763 struct usb_configuration *config,
764 int (*bind)(struct usb_configuration *))
David Brownell40982be2008-06-19 17:52:58 -0700765{
766 int status = -EINVAL;
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100767
768 if (!bind)
769 goto done;
David Brownell40982be2008-06-19 17:52:58 -0700770
771 DBG(cdev, "adding config #%u '%s'/%p\n",
772 config->bConfigurationValue,
773 config->label, config);
774
Sebastian Andrzej Siewiorde53c252012-12-23 21:10:00 +0100775 status = usb_add_config_only(cdev, config);
776 if (status)
David Brownell40982be2008-06-19 17:52:58 -0700777 goto done;
778
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200779 status = bind(config);
David Brownell40982be2008-06-19 17:52:58 -0700780 if (status < 0) {
Yongsul Oh124ef382012-03-20 10:38:38 +0900781 while (!list_empty(&config->functions)) {
782 struct usb_function *f;
783
784 f = list_first_entry(&config->functions,
785 struct usb_function, list);
786 list_del(&f->list);
787 if (f->unbind) {
788 DBG(cdev, "unbind function '%s'/%p\n",
789 f->name, f);
790 f->unbind(config, f);
791 /* may free memory for "f" */
792 }
793 }
David Brownell40982be2008-06-19 17:52:58 -0700794 list_del(&config->list);
795 config->cdev = NULL;
796 } else {
797 unsigned i;
798
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300799 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
David Brownell40982be2008-06-19 17:52:58 -0700800 config->bConfigurationValue, config,
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +0300801 config->superspeed ? " super" : "",
David Brownell40982be2008-06-19 17:52:58 -0700802 config->highspeed ? " high" : "",
803 config->fullspeed
804 ? (gadget_is_dualspeed(cdev->gadget)
805 ? " full"
806 : " full/low")
807 : "");
808
809 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
810 struct usb_function *f = config->interface[i];
811
812 if (!f)
813 continue;
814 DBG(cdev, " interface %d = %s/%p\n",
815 i, f->name, f);
816 }
817 }
818
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200819 /* set_alt(), or next bind(), sets up
David Brownell40982be2008-06-19 17:52:58 -0700820 * ep->driver_data as needed.
821 */
822 usb_ep_autoconfig_reset(cdev->gadget);
823
824done:
825 if (status)
826 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
827 config->bConfigurationValue, status);
828 return status;
829}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +0200830EXPORT_SYMBOL_GPL(usb_add_config);
David Brownell40982be2008-06-19 17:52:58 -0700831
Benoit Goby51cce6f2012-05-10 10:07:57 +0200832static void remove_config(struct usb_composite_dev *cdev,
833 struct usb_configuration *config)
834{
835 while (!list_empty(&config->functions)) {
836 struct usb_function *f;
837
838 f = list_first_entry(&config->functions,
839 struct usb_function, list);
840 list_del(&f->list);
841 if (f->unbind) {
842 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
843 f->unbind(config, f);
844 /* may free memory for "f" */
845 }
846 }
847 list_del(&config->list);
848 if (config->unbind) {
849 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
850 config->unbind(config);
851 /* may free memory for "c" */
852 }
853}
854
855/**
856 * usb_remove_config() - remove a configuration from a device.
857 * @cdev: wraps the USB gadget
858 * @config: the configuration
859 *
860 * Drivers must call usb_gadget_disconnect before calling this function
861 * to disconnect the device from the host and make sure the host will not
862 * try to enumerate the device while we are changing the config list.
863 */
864void usb_remove_config(struct usb_composite_dev *cdev,
865 struct usb_configuration *config)
866{
867 unsigned long flags;
868
869 spin_lock_irqsave(&cdev->lock, flags);
870
871 if (cdev->config == config)
872 reset_config(cdev);
873
874 spin_unlock_irqrestore(&cdev->lock, flags);
875
876 remove_config(cdev, config);
877}
878
David Brownell40982be2008-06-19 17:52:58 -0700879/*-------------------------------------------------------------------------*/
880
881/* We support strings in multiple languages ... string descriptor zero
882 * says which languages are supported. The typical case will be that
883 * only one language (probably English) is used, with I18N handled on
884 * the host side.
885 */
886
887static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
888{
889 const struct usb_gadget_strings *s;
Dan Carpenter20c5e742012-04-17 09:30:22 +0300890 __le16 language;
David Brownell40982be2008-06-19 17:52:58 -0700891 __le16 *tmp;
892
893 while (*sp) {
894 s = *sp;
895 language = cpu_to_le16(s->language);
896 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
897 if (*tmp == language)
898 goto repeat;
899 }
900 *tmp++ = language;
901repeat:
902 sp++;
903 }
904}
905
906static int lookup_string(
907 struct usb_gadget_strings **sp,
908 void *buf,
909 u16 language,
910 int id
911)
912{
913 struct usb_gadget_strings *s;
914 int value;
915
916 while (*sp) {
917 s = *sp++;
918 if (s->language != language)
919 continue;
920 value = usb_gadget_get_string(s, id, buf);
921 if (value > 0)
922 return value;
923 }
924 return -EINVAL;
925}
926
927static int get_string(struct usb_composite_dev *cdev,
928 void *buf, u16 language, int id)
929{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +0200930 struct usb_composite_driver *composite = cdev->driver;
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +0100931 struct usb_gadget_string_container *uc;
David Brownell40982be2008-06-19 17:52:58 -0700932 struct usb_configuration *c;
933 struct usb_function *f;
934 int len;
935
936 /* Yes, not only is USB's I18N support probably more than most
937 * folk will ever care about ... also, it's all supported here.
938 * (Except for UTF8 support for Unicode's "Astral Planes".)
939 */
940
941 /* 0 == report all available language codes */
942 if (id == 0) {
943 struct usb_string_descriptor *s = buf;
944 struct usb_gadget_strings **sp;
945
946 memset(s, 0, 256);
947 s->bDescriptorType = USB_DT_STRING;
948
949 sp = composite->strings;
950 if (sp)
951 collect_langs(sp, s->wData);
952
953 list_for_each_entry(c, &cdev->configs, list) {
954 sp = c->strings;
955 if (sp)
956 collect_langs(sp, s->wData);
957
958 list_for_each_entry(f, &c->functions, list) {
959 sp = f->strings;
960 if (sp)
961 collect_langs(sp, s->wData);
962 }
963 }
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +0100964 list_for_each_entry(uc, &cdev->gstrings, list) {
965 struct usb_gadget_strings **sp;
966
967 sp = get_containers_gs(uc);
968 collect_langs(sp, s->wData);
969 }
David Brownell40982be2008-06-19 17:52:58 -0700970
Roel Kluin417b57b2009-08-06 16:09:51 -0700971 for (len = 0; len <= 126 && s->wData[len]; len++)
David Brownell40982be2008-06-19 17:52:58 -0700972 continue;
973 if (!len)
974 return -EINVAL;
975
976 s->bLength = 2 * (len + 1);
977 return s->bLength;
978 }
979
Andrzej Pietrasiewicz19824d52014-05-08 14:06:22 +0200980 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
981 struct usb_os_string *b = buf;
982 b->bLength = sizeof(*b);
983 b->bDescriptorType = USB_DT_STRING;
984 compiletime_assert(
985 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
986 "qwSignature size must be equal to qw_sign");
987 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
988 b->bMS_VendorCode = cdev->b_vendor_code;
989 b->bPad = 0;
990 return sizeof(*b);
991 }
992
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +0100993 list_for_each_entry(uc, &cdev->gstrings, list) {
994 struct usb_gadget_strings **sp;
995
996 sp = get_containers_gs(uc);
997 len = lookup_string(sp, buf, language, id);
998 if (len > 0)
999 return len;
1000 }
1001
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001002 /* String IDs are device-scoped, so we look up each string
1003 * table we're told about. These lookups are infrequent;
1004 * simpler-is-better here.
David Brownell40982be2008-06-19 17:52:58 -07001005 */
1006 if (composite->strings) {
1007 len = lookup_string(composite->strings, buf, language, id);
1008 if (len > 0)
1009 return len;
1010 }
1011 list_for_each_entry(c, &cdev->configs, list) {
1012 if (c->strings) {
1013 len = lookup_string(c->strings, buf, language, id);
1014 if (len > 0)
1015 return len;
1016 }
1017 list_for_each_entry(f, &c->functions, list) {
1018 if (!f->strings)
1019 continue;
1020 len = lookup_string(f->strings, buf, language, id);
1021 if (len > 0)
1022 return len;
1023 }
1024 }
1025 return -EINVAL;
1026}
1027
1028/**
1029 * usb_string_id() - allocate an unused string ID
1030 * @cdev: the device whose string descriptor IDs are being allocated
1031 * Context: single threaded during gadget setup
1032 *
1033 * @usb_string_id() is called from bind() callbacks to allocate
1034 * string IDs. Drivers for functions, configurations, or gadgets will
1035 * then store that ID in the appropriate descriptors and string table.
1036 *
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001037 * All string identifier should be allocated using this,
1038 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1039 * that for example different functions don't wrongly assign different
1040 * meanings to the same identifier.
David Brownell40982be2008-06-19 17:52:58 -07001041 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001042int usb_string_id(struct usb_composite_dev *cdev)
David Brownell40982be2008-06-19 17:52:58 -07001043{
1044 if (cdev->next_string_id < 254) {
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001045 /* string id 0 is reserved by USB spec for list of
1046 * supported languages */
1047 /* 255 reserved as well? -- mina86 */
David Brownell40982be2008-06-19 17:52:58 -07001048 cdev->next_string_id++;
1049 return cdev->next_string_id;
1050 }
1051 return -ENODEV;
1052}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001053EXPORT_SYMBOL_GPL(usb_string_id);
David Brownell40982be2008-06-19 17:52:58 -07001054
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001055/**
1056 * usb_string_ids() - allocate unused string IDs in batch
1057 * @cdev: the device whose string descriptor IDs are being allocated
1058 * @str: an array of usb_string objects to assign numbers to
1059 * Context: single threaded during gadget setup
1060 *
1061 * @usb_string_ids() is called from bind() callbacks to allocate
1062 * string IDs. Drivers for functions, configurations, or gadgets will
1063 * then copy IDs from the string table to the appropriate descriptors
1064 * and string table for other languages.
1065 *
1066 * All string identifier should be allocated using this,
1067 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1068 * example different functions don't wrongly assign different meanings
1069 * to the same identifier.
1070 */
1071int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1072{
1073 int next = cdev->next_string_id;
1074
1075 for (; str->s; ++str) {
1076 if (unlikely(next >= 254))
1077 return -ENODEV;
1078 str->id = ++next;
1079 }
1080
1081 cdev->next_string_id = next;
1082
1083 return 0;
1084}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001085EXPORT_SYMBOL_GPL(usb_string_ids_tab);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001086
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001087static struct usb_gadget_string_container *copy_gadget_strings(
1088 struct usb_gadget_strings **sp, unsigned n_gstrings,
1089 unsigned n_strings)
1090{
1091 struct usb_gadget_string_container *uc;
1092 struct usb_gadget_strings **gs_array;
1093 struct usb_gadget_strings *gs;
1094 struct usb_string *s;
1095 unsigned mem;
1096 unsigned n_gs;
1097 unsigned n_s;
1098 void *stash;
1099
1100 mem = sizeof(*uc);
1101 mem += sizeof(void *) * (n_gstrings + 1);
1102 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1103 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1104 uc = kmalloc(mem, GFP_KERNEL);
1105 if (!uc)
1106 return ERR_PTR(-ENOMEM);
1107 gs_array = get_containers_gs(uc);
1108 stash = uc->stash;
1109 stash += sizeof(void *) * (n_gstrings + 1);
1110 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1111 struct usb_string *org_s;
1112
1113 gs_array[n_gs] = stash;
1114 gs = gs_array[n_gs];
1115 stash += sizeof(struct usb_gadget_strings);
1116 gs->language = sp[n_gs]->language;
1117 gs->strings = stash;
1118 org_s = sp[n_gs]->strings;
1119
1120 for (n_s = 0; n_s < n_strings; n_s++) {
1121 s = stash;
1122 stash += sizeof(struct usb_string);
1123 if (org_s->s)
1124 s->s = org_s->s;
1125 else
1126 s->s = "";
1127 org_s++;
1128 }
1129 s = stash;
1130 s->s = NULL;
1131 stash += sizeof(struct usb_string);
1132
1133 }
1134 gs_array[n_gs] = NULL;
1135 return uc;
1136}
1137
1138/**
1139 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1140 * @cdev: the device whose string descriptor IDs are being allocated
1141 * and attached.
1142 * @sp: an array of usb_gadget_strings to attach.
1143 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1144 *
1145 * This function will create a deep copy of usb_gadget_strings and usb_string
1146 * and attach it to the cdev. The actual string (usb_string.s) will not be
1147 * copied but only a referenced will be made. The struct usb_gadget_strings
1148 * array may contain multiple languges and should be NULL terminated.
1149 * The ->language pointer of each struct usb_gadget_strings has to contain the
1150 * same amount of entries.
1151 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1152 * usb_string entry of es-ES containts the translation of the first usb_string
1153 * entry of en-US. Therefore both entries become the same id assign.
1154 */
1155struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1156 struct usb_gadget_strings **sp, unsigned n_strings)
1157{
1158 struct usb_gadget_string_container *uc;
1159 struct usb_gadget_strings **n_gs;
1160 unsigned n_gstrings = 0;
1161 unsigned i;
1162 int ret;
1163
1164 for (i = 0; sp[i]; i++)
1165 n_gstrings++;
1166
1167 if (!n_gstrings)
1168 return ERR_PTR(-EINVAL);
1169
1170 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1171 if (IS_ERR(uc))
Felipe Balbidad4bab2014-03-10 13:30:56 -05001172 return ERR_CAST(uc);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001173
1174 n_gs = get_containers_gs(uc);
1175 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1176 if (ret)
1177 goto err;
1178
1179 for (i = 1; i < n_gstrings; i++) {
1180 struct usb_string *m_s;
1181 struct usb_string *s;
1182 unsigned n;
1183
1184 m_s = n_gs[0]->strings;
1185 s = n_gs[i]->strings;
1186 for (n = 0; n < n_strings; n++) {
1187 s->id = m_s->id;
1188 s++;
1189 m_s++;
1190 }
1191 }
1192 list_add_tail(&uc->list, &cdev->gstrings);
1193 return n_gs[0]->strings;
1194err:
1195 kfree(uc);
1196 return ERR_PTR(ret);
1197}
1198EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1199
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001200/**
1201 * usb_string_ids_n() - allocate unused string IDs in batch
Randy Dunlapd187abb2010-08-11 12:07:13 -07001202 * @c: the device whose string descriptor IDs are being allocated
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001203 * @n: number of string IDs to allocate
1204 * Context: single threaded during gadget setup
1205 *
1206 * Returns the first requested ID. This ID and next @n-1 IDs are now
Randy Dunlapd187abb2010-08-11 12:07:13 -07001207 * valid IDs. At least provided that @n is non-zero because if it
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001208 * is, returns last requested ID which is now very useful information.
1209 *
1210 * @usb_string_ids_n() is called from bind() callbacks to allocate
1211 * string IDs. Drivers for functions, configurations, or gadgets will
1212 * then store that ID in the appropriate descriptors and string table.
1213 *
1214 * All string identifier should be allocated using this,
1215 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1216 * example different functions don't wrongly assign different meanings
1217 * to the same identifier.
1218 */
1219int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1220{
1221 unsigned next = c->next_string_id;
1222 if (unlikely(n > 254 || (unsigned)next + n > 254))
1223 return -ENODEV;
1224 c->next_string_id += n;
1225 return next + 1;
1226}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001227EXPORT_SYMBOL_GPL(usb_string_ids_n);
Michal Nazarewiczf2adc4f2010-06-16 12:07:59 +02001228
David Brownell40982be2008-06-19 17:52:58 -07001229/*-------------------------------------------------------------------------*/
1230
1231static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1232{
1233 if (req->status || req->actual != req->length)
1234 DBG((struct usb_composite_dev *) ep->driver_data,
1235 "setup complete --> %d, %d/%d\n",
1236 req->status, req->actual, req->length);
1237}
1238
1239/*
1240 * The setup() callback implements all the ep0 functionality that's
1241 * not handled lower down, in hardware or the hardware driver(like
1242 * device and endpoint feature flags, and their status). It's all
1243 * housekeeping for the gadget function we're implementing. Most of
1244 * the work is in config and function specific setup.
1245 */
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001246int
David Brownell40982be2008-06-19 17:52:58 -07001247composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1248{
1249 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1250 struct usb_request *req = cdev->req;
1251 int value = -EOPNOTSUPP;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001252 int status = 0;
David Brownell40982be2008-06-19 17:52:58 -07001253 u16 w_index = le16_to_cpu(ctrl->wIndex);
Bryan Wu08889512009-01-08 00:21:19 +08001254 u8 intf = w_index & 0xFF;
David Brownell40982be2008-06-19 17:52:58 -07001255 u16 w_value = le16_to_cpu(ctrl->wValue);
1256 u16 w_length = le16_to_cpu(ctrl->wLength);
1257 struct usb_function *f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001258 u8 endp;
David Brownell40982be2008-06-19 17:52:58 -07001259
1260 /* partial re-init of the response message; the function or the
1261 * gadget might need to intercept e.g. a control-OUT completion
1262 * when we delegate to it.
1263 */
1264 req->zero = 0;
1265 req->complete = composite_setup_complete;
Maulik Mankad2edb11c2011-02-22 19:08:42 +05301266 req->length = 0;
David Brownell40982be2008-06-19 17:52:58 -07001267 gadget->ep0->driver_data = cdev;
1268
1269 switch (ctrl->bRequest) {
1270
1271 /* we handle all standard USB descriptors */
1272 case USB_REQ_GET_DESCRIPTOR:
1273 if (ctrl->bRequestType != USB_DIR_IN)
1274 goto unknown;
1275 switch (w_value >> 8) {
1276
1277 case USB_DT_DEVICE:
1278 cdev->desc.bNumConfigurations =
1279 count_configs(cdev, USB_DT_DEVICE);
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001280 cdev->desc.bMaxPacketSize0 =
1281 cdev->gadget->ep0->maxpacket;
1282 if (gadget_is_superspeed(gadget)) {
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001283 if (gadget->speed >= USB_SPEED_SUPER) {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001284 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001285 cdev->desc.bMaxPacketSize0 = 9;
1286 } else {
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001287 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
Sebastian Andrzej Siewiora8f21152011-07-19 20:21:52 +02001288 }
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001289 }
1290
David Brownell40982be2008-06-19 17:52:58 -07001291 value = min(w_length, (u16) sizeof cdev->desc);
1292 memcpy(req->buf, &cdev->desc, value);
1293 break;
1294 case USB_DT_DEVICE_QUALIFIER:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001295 if (!gadget_is_dualspeed(gadget) ||
1296 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001297 break;
1298 device_qual(cdev);
1299 value = min_t(int, w_length,
1300 sizeof(struct usb_qualifier_descriptor));
1301 break;
1302 case USB_DT_OTHER_SPEED_CONFIG:
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001303 if (!gadget_is_dualspeed(gadget) ||
1304 gadget->speed >= USB_SPEED_SUPER)
David Brownell40982be2008-06-19 17:52:58 -07001305 break;
1306 /* FALLTHROUGH */
1307 case USB_DT_CONFIG:
1308 value = config_desc(cdev, w_value);
1309 if (value >= 0)
1310 value = min(w_length, (u16) value);
1311 break;
1312 case USB_DT_STRING:
1313 value = get_string(cdev, req->buf,
1314 w_index, w_value & 0xff);
1315 if (value >= 0)
1316 value = min(w_length, (u16) value);
1317 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001318 case USB_DT_BOS:
1319 if (gadget_is_superspeed(gadget)) {
1320 value = bos_desc(cdev);
1321 value = min(w_length, (u16) value);
1322 }
1323 break;
David Brownell40982be2008-06-19 17:52:58 -07001324 }
1325 break;
1326
1327 /* any number of configs can work */
1328 case USB_REQ_SET_CONFIGURATION:
1329 if (ctrl->bRequestType != 0)
1330 goto unknown;
1331 if (gadget_is_otg(gadget)) {
1332 if (gadget->a_hnp_support)
1333 DBG(cdev, "HNP available\n");
1334 else if (gadget->a_alt_hnp_support)
1335 DBG(cdev, "HNP on another port\n");
1336 else
1337 VDBG(cdev, "HNP inactive\n");
1338 }
1339 spin_lock(&cdev->lock);
1340 value = set_config(cdev, ctrl, w_value);
1341 spin_unlock(&cdev->lock);
1342 break;
1343 case USB_REQ_GET_CONFIGURATION:
1344 if (ctrl->bRequestType != USB_DIR_IN)
1345 goto unknown;
1346 if (cdev->config)
1347 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1348 else
1349 *(u8 *)req->buf = 0;
1350 value = min(w_length, (u16) 1);
1351 break;
1352
1353 /* function drivers must handle get/set altsetting; if there's
1354 * no get() method, we know only altsetting zero works.
1355 */
1356 case USB_REQ_SET_INTERFACE:
1357 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1358 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001359 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001360 break;
Bryan Wu08889512009-01-08 00:21:19 +08001361 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001362 if (!f)
1363 break;
Bryan Wudd4dff82009-01-08 00:21:18 +08001364 if (w_value && !f->set_alt)
David Brownell40982be2008-06-19 17:52:58 -07001365 break;
1366 value = f->set_alt(f, w_index, w_value);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001367 if (value == USB_GADGET_DELAYED_STATUS) {
1368 DBG(cdev,
1369 "%s: interface %d (%s) requested delayed status\n",
1370 __func__, intf, f->name);
1371 cdev->delayed_status++;
1372 DBG(cdev, "delayed_status count %d\n",
1373 cdev->delayed_status);
1374 }
David Brownell40982be2008-06-19 17:52:58 -07001375 break;
1376 case USB_REQ_GET_INTERFACE:
1377 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1378 goto unknown;
Jassi Brarff085de2011-02-06 17:39:17 +09001379 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
David Brownell40982be2008-06-19 17:52:58 -07001380 break;
Bryan Wu08889512009-01-08 00:21:19 +08001381 f = cdev->config->interface[intf];
David Brownell40982be2008-06-19 17:52:58 -07001382 if (!f)
1383 break;
1384 /* lots of interfaces only need altsetting zero... */
1385 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1386 if (value < 0)
1387 break;
1388 *((u8 *)req->buf) = value;
1389 value = min(w_length, (u16) 1);
1390 break;
Tatyana Brokhmanbdb64d72011-06-29 16:41:50 +03001391
1392 /*
1393 * USB 3.0 additions:
1394 * Function driver should handle get_status request. If such cb
1395 * wasn't supplied we respond with default value = 0
1396 * Note: function driver should supply such cb only for the first
1397 * interface of the function
1398 */
1399 case USB_REQ_GET_STATUS:
1400 if (!gadget_is_superspeed(gadget))
1401 goto unknown;
1402 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1403 goto unknown;
1404 value = 2; /* This is the length of the get_status reply */
1405 put_unaligned_le16(0, req->buf);
1406 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1407 break;
1408 f = cdev->config->interface[intf];
1409 if (!f)
1410 break;
1411 status = f->get_status ? f->get_status(f) : 0;
1412 if (status < 0)
1413 break;
1414 put_unaligned_le16(status & 0x0000ffff, req->buf);
1415 break;
1416 /*
1417 * Function drivers should handle SetFeature/ClearFeature
1418 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1419 * only for the first interface of the function
1420 */
1421 case USB_REQ_CLEAR_FEATURE:
1422 case USB_REQ_SET_FEATURE:
1423 if (!gadget_is_superspeed(gadget))
1424 goto unknown;
1425 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1426 goto unknown;
1427 switch (w_value) {
1428 case USB_INTRF_FUNC_SUSPEND:
1429 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1430 break;
1431 f = cdev->config->interface[intf];
1432 if (!f)
1433 break;
1434 value = 0;
1435 if (f->func_suspend)
1436 value = f->func_suspend(f, w_index >> 8);
1437 if (value < 0) {
1438 ERROR(cdev,
1439 "func_suspend() returned error %d\n",
1440 value);
1441 value = 0;
1442 }
1443 break;
1444 }
1445 break;
David Brownell40982be2008-06-19 17:52:58 -07001446 default:
1447unknown:
1448 VDBG(cdev,
1449 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1450 ctrl->bRequestType, ctrl->bRequest,
1451 w_value, w_index, w_length);
1452
Laurent Pinchart52426582009-10-21 00:03:38 +02001453 /* functions always handle their interfaces and endpoints...
1454 * punt other recipients (other, WUSB, ...) to the current
David Brownell40982be2008-06-19 17:52:58 -07001455 * configuration code.
1456 *
1457 * REVISIT it could make sense to let the composite device
1458 * take such requests too, if that's ever needed: to work
1459 * in config 0, etc.
1460 */
Laurent Pinchart52426582009-10-21 00:03:38 +02001461 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1462 case USB_RECIP_INTERFACE:
Jassi Brarff085de2011-02-06 17:39:17 +09001463 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
Maulik Mankad3c47eb02011-01-13 18:19:56 +05301464 break;
1465 f = cdev->config->interface[intf];
Laurent Pinchart52426582009-10-21 00:03:38 +02001466 break;
1467
1468 case USB_RECIP_ENDPOINT:
1469 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1470 list_for_each_entry(f, &cdev->config->functions, list) {
1471 if (test_bit(endp, f->endpoints))
1472 break;
1473 }
1474 if (&f->list == &cdev->config->functions)
David Brownell40982be2008-06-19 17:52:58 -07001475 f = NULL;
Laurent Pinchart52426582009-10-21 00:03:38 +02001476 break;
David Brownell40982be2008-06-19 17:52:58 -07001477 }
Laurent Pinchart52426582009-10-21 00:03:38 +02001478
1479 if (f && f->setup)
1480 value = f->setup(f, ctrl);
1481 else {
David Brownell40982be2008-06-19 17:52:58 -07001482 struct usb_configuration *c;
1483
1484 c = cdev->config;
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001485 if (!c)
1486 goto done;
1487
1488 /* try current config's setup */
1489 if (c->setup) {
David Brownell40982be2008-06-19 17:52:58 -07001490 value = c->setup(c, ctrl);
Andrzej Pietrasiewicza01091e2013-11-07 08:41:25 +01001491 goto done;
1492 }
1493
1494 /* try the only function in the current config */
1495 if (!list_is_singular(&c->functions))
1496 goto done;
1497 f = list_first_entry(&c->functions, struct usb_function,
1498 list);
1499 if (f->setup)
1500 value = f->setup(f, ctrl);
David Brownell40982be2008-06-19 17:52:58 -07001501 }
1502
1503 goto done;
1504 }
1505
1506 /* respond with data transfer before status phase? */
Roger Quadros1b9ba002011-05-09 13:08:06 +03001507 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
David Brownell40982be2008-06-19 17:52:58 -07001508 req->length = value;
1509 req->zero = value < w_length;
1510 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1511 if (value < 0) {
1512 DBG(cdev, "ep_queue --> %d\n", value);
1513 req->status = 0;
1514 composite_setup_complete(gadget->ep0, req);
1515 }
Roger Quadros1b9ba002011-05-09 13:08:06 +03001516 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1517 WARN(cdev,
1518 "%s: Delayed status not supported for w_length != 0",
1519 __func__);
David Brownell40982be2008-06-19 17:52:58 -07001520 }
1521
1522done:
1523 /* device either stalls (value < 0) or reports success */
1524 return value;
1525}
1526
Sebastian Andrzej Siewior2d5a8892012-12-23 21:10:21 +01001527void composite_disconnect(struct usb_gadget *gadget)
David Brownell40982be2008-06-19 17:52:58 -07001528{
1529 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1530 unsigned long flags;
1531
1532 /* REVISIT: should we have config and device level
1533 * disconnect callbacks?
1534 */
1535 spin_lock_irqsave(&cdev->lock, flags);
1536 if (cdev->config)
1537 reset_config(cdev);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001538 if (cdev->driver->disconnect)
1539 cdev->driver->disconnect(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001540 spin_unlock_irqrestore(&cdev->lock, flags);
1541}
1542
1543/*-------------------------------------------------------------------------*/
1544
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001545static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1546 char *buf)
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001547{
1548 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1549 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1550
1551 return sprintf(buf, "%d\n", cdev->suspended);
1552}
Greg Kroah-Hartmance26bd22013-08-23 16:34:43 -07001553static DEVICE_ATTR_RO(suspended);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001554
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001555static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
David Brownell40982be2008-06-19 17:52:58 -07001556{
1557 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1558
1559 /* composite_disconnect() must already have been called
1560 * by the underlying peripheral controller driver!
1561 * so there's no i/o concurrency that could affect the
1562 * state protected by cdev->lock.
1563 */
1564 WARN_ON(cdev->config);
1565
1566 while (!list_empty(&cdev->configs)) {
1567 struct usb_configuration *c;
David Brownell40982be2008-06-19 17:52:58 -07001568 c = list_first_entry(&cdev->configs,
1569 struct usb_configuration, list);
Benoit Goby51cce6f2012-05-10 10:07:57 +02001570 remove_config(cdev, c);
David Brownell40982be2008-06-19 17:52:58 -07001571 }
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001572 if (cdev->driver->unbind && unbind_driver)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001573 cdev->driver->unbind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001574
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001575 composite_dev_cleanup(cdev);
1576
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001577 kfree(cdev->def_manufacturer);
David Brownell40982be2008-06-19 17:52:58 -07001578 kfree(cdev);
1579 set_gadget_data(gadget, NULL);
David Brownell40982be2008-06-19 17:52:58 -07001580}
1581
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001582static void composite_unbind(struct usb_gadget *gadget)
1583{
1584 __composite_unbind(gadget, true);
1585}
1586
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001587static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1588 const struct usb_device_descriptor *old)
1589{
1590 __le16 idVendor;
1591 __le16 idProduct;
1592 __le16 bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001593 u8 iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001594 u8 iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001595 u8 iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001596
1597 /*
1598 * these variables may have been set in
1599 * usb_composite_overwrite_options()
1600 */
1601 idVendor = new->idVendor;
1602 idProduct = new->idProduct;
1603 bcdDevice = new->bcdDevice;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001604 iSerialNumber = new->iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001605 iManufacturer = new->iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001606 iProduct = new->iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001607
1608 *new = *old;
1609 if (idVendor)
1610 new->idVendor = idVendor;
1611 if (idProduct)
1612 new->idProduct = idProduct;
1613 if (bcdDevice)
1614 new->bcdDevice = bcdDevice;
Sebastian Andrzej Siewiored9cbda2012-09-10 09:16:07 +02001615 else
1616 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001617 if (iSerialNumber)
1618 new->iSerialNumber = iSerialNumber;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001619 if (iManufacturer)
1620 new->iManufacturer = iManufacturer;
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001621 if (iProduct)
1622 new->iProduct = iProduct;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001623}
1624
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001625int composite_dev_prepare(struct usb_composite_driver *composite,
1626 struct usb_composite_dev *cdev)
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001627{
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001628 struct usb_gadget *gadget = cdev->gadget;
1629 int ret = -ENOMEM;
1630
1631 /* preallocate control response and buffer */
1632 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1633 if (!cdev->req)
1634 return -ENOMEM;
1635
1636 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
1637 if (!cdev->req->buf)
1638 goto fail;
1639
1640 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
1641 if (ret)
1642 goto fail_dev;
1643
1644 cdev->req->complete = composite_setup_complete;
1645 gadget->ep0->driver_data = cdev;
1646
1647 cdev->driver = composite;
1648
1649 /*
1650 * As per USB compliance update, a device that is actively drawing
1651 * more than 100mA from USB must report itself as bus-powered in
1652 * the GetStatus(DEVICE) call.
1653 */
1654 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1655 usb_gadget_set_selfpowered(gadget);
1656
1657 /* interface and string IDs start at zero via kzalloc.
1658 * we force endpoints to start unassigned; few controller
1659 * drivers will zero ep->driver_data.
1660 */
1661 usb_ep_autoconfig_reset(gadget);
1662 return 0;
1663fail_dev:
1664 kfree(cdev->req->buf);
1665fail:
1666 usb_ep_free_request(gadget->ep0, cdev->req);
1667 cdev->req = NULL;
1668 return ret;
1669}
1670
1671void composite_dev_cleanup(struct usb_composite_dev *cdev)
1672{
Sebastian Andrzej Siewior27a466332012-12-23 21:10:23 +01001673 struct usb_gadget_string_container *uc, *tmp;
1674
1675 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
1676 list_del(&uc->list);
1677 kfree(uc);
1678 }
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001679 if (cdev->req) {
1680 kfree(cdev->req->buf);
1681 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
1682 }
Sebastian Andrzej Siewior88af8bb2012-12-23 21:10:24 +01001683 cdev->next_string_id = 0;
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001684 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001685}
1686
1687static int composite_bind(struct usb_gadget *gadget,
1688 struct usb_gadget_driver *gdriver)
David Brownell40982be2008-06-19 17:52:58 -07001689{
1690 struct usb_composite_dev *cdev;
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001691 struct usb_composite_driver *composite = to_cdriver(gdriver);
David Brownell40982be2008-06-19 17:52:58 -07001692 int status = -ENOMEM;
1693
1694 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1695 if (!cdev)
1696 return status;
1697
1698 spin_lock_init(&cdev->lock);
1699 cdev->gadget = gadget;
1700 set_gadget_data(gadget, cdev);
1701 INIT_LIST_HEAD(&cdev->configs);
Sebastian Andrzej Siewior9bb28592012-12-23 21:10:22 +01001702 INIT_LIST_HEAD(&cdev->gstrings);
David Brownell40982be2008-06-19 17:52:58 -07001703
Sebastian Andrzej Siewiora5923342012-12-23 21:10:20 +01001704 status = composite_dev_prepare(composite, cdev);
1705 if (status)
David Brownell40982be2008-06-19 17:52:58 -07001706 goto fail;
David Brownell40982be2008-06-19 17:52:58 -07001707
1708 /* composite gadget needs to assign strings for whole device (like
1709 * serial number), register function drivers, potentially update
1710 * power state and consumption, etc
1711 */
Sebastian Andrzej Siewiorfac3a432012-09-06 20:11:01 +02001712 status = composite->bind(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001713 if (status < 0)
1714 goto fail;
1715
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001716 update_unchanged_dev_desc(&cdev->desc, composite->dev);
Greg Kroah-Hartmandbb442b2010-12-16 15:52:30 -08001717
Michal Nazarewiczad1a8102010-08-12 17:43:46 +02001718 /* has userspace failed to provide a serial number? */
1719 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1720 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1721
David Brownell40982be2008-06-19 17:52:58 -07001722 INFO(cdev, "%s ready\n", composite->name);
1723 return 0;
1724
1725fail:
Sebastian Andrzej Siewior779d5162012-12-23 21:09:55 +01001726 __composite_unbind(gadget, false);
David Brownell40982be2008-06-19 17:52:58 -07001727 return status;
1728}
1729
1730/*-------------------------------------------------------------------------*/
1731
1732static void
1733composite_suspend(struct usb_gadget *gadget)
1734{
1735 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1736 struct usb_function *f;
1737
David Brownell89429392009-03-19 14:14:17 -07001738 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001739 * suspend/resume callbacks?
1740 */
1741 DBG(cdev, "suspend\n");
1742 if (cdev->config) {
1743 list_for_each_entry(f, &cdev->config->functions, list) {
1744 if (f->suspend)
1745 f->suspend(f);
1746 }
1747 }
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001748 if (cdev->driver->suspend)
1749 cdev->driver->suspend(cdev);
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001750
1751 cdev->suspended = 1;
Hao Wub23f2f92010-11-29 15:17:03 +08001752
1753 usb_gadget_vbus_draw(gadget, 2);
David Brownell40982be2008-06-19 17:52:58 -07001754}
1755
1756static void
1757composite_resume(struct usb_gadget *gadget)
1758{
1759 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1760 struct usb_function *f;
Du, ChangbinX56b1b902013-12-17 11:47:42 +00001761 u16 maxpower;
David Brownell40982be2008-06-19 17:52:58 -07001762
David Brownell89429392009-03-19 14:14:17 -07001763 /* REVISIT: should we have config level
David Brownell40982be2008-06-19 17:52:58 -07001764 * suspend/resume callbacks?
1765 */
1766 DBG(cdev, "resume\n");
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001767 if (cdev->driver->resume)
1768 cdev->driver->resume(cdev);
David Brownell40982be2008-06-19 17:52:58 -07001769 if (cdev->config) {
1770 list_for_each_entry(f, &cdev->config->functions, list) {
1771 if (f->resume)
1772 f->resume(f);
1773 }
Hao Wub23f2f92010-11-29 15:17:03 +08001774
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01001775 maxpower = cdev->config->MaxPower;
Hao Wub23f2f92010-11-29 15:17:03 +08001776
1777 usb_gadget_vbus_draw(gadget, maxpower ?
Sebastian Andrzej Siewior8f900a92012-12-03 20:07:05 +01001778 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
David Brownell40982be2008-06-19 17:52:58 -07001779 }
Fabien Chouteauf48cf802010-04-23 14:21:26 +02001780
1781 cdev->suspended = 0;
David Brownell40982be2008-06-19 17:52:58 -07001782}
1783
1784/*-------------------------------------------------------------------------*/
1785
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001786static const struct usb_gadget_driver composite_driver_template = {
Sebastian Andrzej Siewior93952952012-09-06 20:11:05 +02001787 .bind = composite_bind,
Michal Nazarewicz915c8be2009-11-09 14:15:25 +01001788 .unbind = composite_unbind,
David Brownell40982be2008-06-19 17:52:58 -07001789
1790 .setup = composite_setup,
1791 .disconnect = composite_disconnect,
1792
1793 .suspend = composite_suspend,
1794 .resume = composite_resume,
1795
1796 .driver = {
1797 .owner = THIS_MODULE,
1798 },
1799};
1800
1801/**
Michal Nazarewicz07a18bd2010-08-12 17:43:54 +02001802 * usb_composite_probe() - register a composite driver
David Brownell40982be2008-06-19 17:52:58 -07001803 * @driver: the driver to register
Nishanth Menon43febb22013-03-04 16:52:38 -06001804 *
David Brownell40982be2008-06-19 17:52:58 -07001805 * Context: single threaded during gadget setup
1806 *
1807 * This function is used to register drivers using the composite driver
1808 * framework. The return value is zero, or a negative errno value.
1809 * Those values normally come from the driver's @bind method, which does
1810 * all the work of setting up the driver to match the hardware.
1811 *
1812 * On successful return, the gadget is ready to respond to requests from
1813 * the host, unless one of its components invokes usb_gadget_disconnect()
1814 * while it was binding. That would usually be done in order to wait for
1815 * some userspace participation.
1816 */
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +02001817int usb_composite_probe(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001818{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001819 struct usb_gadget_driver *gadget_driver;
1820
1821 if (!driver || !driver->dev || !driver->bind)
David Brownell40982be2008-06-19 17:52:58 -07001822 return -EINVAL;
1823
1824 if (!driver->name)
1825 driver->name = "composite";
David Brownell40982be2008-06-19 17:52:58 -07001826
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001827 driver->gadget_driver = composite_driver_template;
1828 gadget_driver = &driver->gadget_driver;
1829
1830 gadget_driver->function = (char *) driver->name;
1831 gadget_driver->driver.name = driver->name;
1832 gadget_driver->max_speed = driver->max_speed;
1833
1834 return usb_gadget_probe_driver(gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07001835}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001836EXPORT_SYMBOL_GPL(usb_composite_probe);
David Brownell40982be2008-06-19 17:52:58 -07001837
1838/**
1839 * usb_composite_unregister() - unregister a composite driver
1840 * @driver: the driver to unregister
1841 *
1842 * This function is used to unregister drivers using the composite
1843 * driver framework.
1844 */
Michal Nazarewicz28824b12010-05-05 12:53:13 +02001845void usb_composite_unregister(struct usb_composite_driver *driver)
David Brownell40982be2008-06-19 17:52:58 -07001846{
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001847 usb_gadget_unregister_driver(&driver->gadget_driver);
David Brownell40982be2008-06-19 17:52:58 -07001848}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001849EXPORT_SYMBOL_GPL(usb_composite_unregister);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001850
1851/**
1852 * usb_composite_setup_continue() - Continue with the control transfer
1853 * @cdev: the composite device who's control transfer was kept waiting
1854 *
1855 * This function must be called by the USB function driver to continue
1856 * with the control transfer's data/status stage in case it had requested to
1857 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1858 * can request the composite framework to delay the setup request's data/status
1859 * stages by returning USB_GADGET_DELAYED_STATUS.
1860 */
1861void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1862{
1863 int value;
1864 struct usb_request *req = cdev->req;
1865 unsigned long flags;
1866
1867 DBG(cdev, "%s\n", __func__);
1868 spin_lock_irqsave(&cdev->lock, flags);
1869
1870 if (cdev->delayed_status == 0) {
1871 WARN(cdev, "%s: Unexpected call\n", __func__);
1872
1873 } else if (--cdev->delayed_status == 0) {
1874 DBG(cdev, "%s: Completing delayed status\n", __func__);
1875 req->length = 0;
1876 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1877 if (value < 0) {
1878 DBG(cdev, "ep_queue --> %d\n", value);
1879 req->status = 0;
1880 composite_setup_complete(cdev->gadget->ep0, req);
1881 }
1882 }
1883
1884 spin_unlock_irqrestore(&cdev->lock, flags);
1885}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001886EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
Roger Quadros1b9ba002011-05-09 13:08:06 +03001887
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001888static char *composite_default_mfr(struct usb_gadget *gadget)
1889{
1890 char *mfr;
1891 int len;
1892
1893 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
1894 init_utsname()->release, gadget->name);
1895 len++;
1896 mfr = kmalloc(len, GFP_KERNEL);
1897 if (!mfr)
1898 return NULL;
1899 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
1900 init_utsname()->release, gadget->name);
1901 return mfr;
1902}
1903
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001904void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
1905 struct usb_composite_overwrite *covr)
1906{
1907 struct usb_device_descriptor *desc = &cdev->desc;
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001908 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
1909 struct usb_string *dev_str = gstr->strings;
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001910
1911 if (covr->idVendor)
1912 desc->idVendor = cpu_to_le16(covr->idVendor);
1913
1914 if (covr->idProduct)
1915 desc->idProduct = cpu_to_le16(covr->idProduct);
1916
1917 if (covr->bcdDevice)
1918 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
Sebastian Andrzej Siewior1cf0d262012-09-10 15:01:54 +02001919
1920 if (covr->serial_number) {
1921 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
1922 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
1923 }
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001924 if (covr->manufacturer) {
1925 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
1926 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +02001927
1928 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
1929 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
1930 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
1931 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
Sebastian Andrzej Siewior03de9bf2012-09-10 15:01:55 +02001932 }
Sebastian Andrzej Siewior2d35ee42012-09-10 15:01:56 +02001933
1934 if (covr->product) {
1935 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
1936 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
1937 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +02001938}
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +02001939EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
Sebastian Andrzej Siewiord80c3042012-09-06 20:11:28 +02001940
1941MODULE_LICENSE("GPL");
1942MODULE_AUTHOR("David Brownell");