]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/gadget/composite.c
usb: gadget: Update config for SuperSpeedPlus
[mirror_ubuntu-artful-kernel.git] / drivers / usb / gadget / composite.c
CommitLineData
40982be5
DB
1/*
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.
40982be5
DB
10 */
11
12/* #define VERBOSE_DEBUG */
13
14#include <linux/kallsyms.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
6eb0de82 17#include <linux/module.h>
40982be5 18#include <linux/device.h>
ad1a8102 19#include <linux/utsname.h>
40982be5
DB
20
21#include <linux/usb/composite.h>
53e6242d 22#include <linux/usb/otg.h>
bdb64d72 23#include <asm/unaligned.h>
40982be5 24
37a3a533
AP
25#include "u_os_desc.h"
26
19824d5e
AP
27/**
28 * struct usb_os_string - represents OS String to be reported by a gadget
29 * @bLength: total length of the entire descritor, always 0x12
30 * @bDescriptorType: USB_DT_STRING
31 * @qwSignature: the OS String proper
32 * @bMS_VendorCode: code used by the host for subsequent requests
33 * @bPad: not used, must be zero
34 */
35struct usb_os_string {
36 __u8 bLength;
37 __u8 bDescriptorType;
38 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
39 __u8 bMS_VendorCode;
40 __u8 bPad;
41} __packed;
42
40982be5
DB
43/*
44 * The code in this file is utility code, used to build a gadget driver
45 * from one or more "function" drivers, one or more "configuration"
46 * objects, and a "usb_composite_driver" by gluing them together along
47 * with the relevant device-wide data.
48 */
49
9bb2859f
SAS
50static struct usb_gadget_strings **get_containers_gs(
51 struct usb_gadget_string_container *uc)
52{
53 return (struct usb_gadget_strings **)uc->stash;
54}
55
48767a4e
TB
56/**
57 * next_ep_desc() - advance to the next EP descriptor
58 * @t: currect pointer within descriptor array
59 *
60 * Return: next EP descriptor or NULL
61 *
62 * Iterate over @t until either EP descriptor found or
63 * NULL (that indicates end of list) encountered
64 */
65static struct usb_descriptor_header**
66next_ep_desc(struct usb_descriptor_header **t)
67{
68 for (; *t; t++) {
69 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
70 return t;
71 }
72 return NULL;
73}
74
75/*
76 * for_each_ep_desc()- iterate over endpoint descriptors in the
77 * descriptors list
78 * @start: pointer within descriptor array.
79 * @ep_desc: endpoint descriptor to use as the loop cursor
80 */
81#define for_each_ep_desc(start, ep_desc) \
82 for (ep_desc = next_ep_desc(start); \
83 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
84
85/**
86 * config_ep_by_speed() - configures the given endpoint
87 * according to gadget speed.
88 * @g: pointer to the gadget
89 * @f: usb function
90 * @_ep: the endpoint to configure
91 *
92 * Return: error code, 0 on success
93 *
94 * This function chooses the right descriptors for a given
95 * endpoint according to gadget speed and saves it in the
96 * endpoint desc field. If the endpoint already has a descriptor
97 * assigned to it - overwrites it with currently corresponding
98 * descriptor. The endpoint maxpacket field is updated according
99 * to the chosen descriptor.
100 * Note: the supplied function should hold all the descriptors
101 * for supported speeds
102 */
103int config_ep_by_speed(struct usb_gadget *g,
104 struct usb_function *f,
105 struct usb_ep *_ep)
106{
b785ea7c 107 struct usb_composite_dev *cdev = get_gadget_data(g);
48767a4e
TB
108 struct usb_endpoint_descriptor *chosen_desc = NULL;
109 struct usb_descriptor_header **speed_desc = NULL;
110
bdb64d72
TB
111 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
112 int want_comp_desc = 0;
113
48767a4e
TB
114 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
115
116 if (!g || !f || !_ep)
117 return -EIO;
118
119 /* select desired speed */
120 switch (g->speed) {
bdb64d72
TB
121 case USB_SPEED_SUPER:
122 if (gadget_is_superspeed(g)) {
123 speed_desc = f->ss_descriptors;
124 want_comp_desc = 1;
125 break;
126 }
127 /* else: Fall trough */
48767a4e
TB
128 case USB_SPEED_HIGH:
129 if (gadget_is_dualspeed(g)) {
130 speed_desc = f->hs_descriptors;
131 break;
132 }
133 /* else: fall through */
134 default:
10287bae 135 speed_desc = f->fs_descriptors;
48767a4e
TB
136 }
137 /* find descriptors */
138 for_each_ep_desc(speed_desc, d_spd) {
139 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
140 if (chosen_desc->bEndpointAddress == _ep->address)
141 goto ep_found;
142 }
143 return -EIO;
144
145ep_found:
146 /* commit results */
29cc8897 147 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
48767a4e 148 _ep->desc = chosen_desc;
bdb64d72
TB
149 _ep->comp_desc = NULL;
150 _ep->maxburst = 0;
151 _ep->mult = 0;
152 if (!want_comp_desc)
153 return 0;
48767a4e 154
bdb64d72
TB
155 /*
156 * Companion descriptor should follow EP descriptor
157 * USB 3.0 spec, #9.6.7
158 */
159 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
160 if (!comp_desc ||
161 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
162 return -EIO;
163 _ep->comp_desc = comp_desc;
164 if (g->speed == USB_SPEED_SUPER) {
165 switch (usb_endpoint_type(_ep->desc)) {
bdb64d72
TB
166 case USB_ENDPOINT_XFER_ISOC:
167 /* mult: bits 1:0 of bmAttributes */
168 _ep->mult = comp_desc->bmAttributes & 0x3;
9e878a6b
PZ
169 case USB_ENDPOINT_XFER_BULK:
170 case USB_ENDPOINT_XFER_INT:
b785ea7c 171 _ep->maxburst = comp_desc->bMaxBurst + 1;
bdb64d72
TB
172 break;
173 default:
b785ea7c
FB
174 if (comp_desc->bMaxBurst != 0)
175 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
176 _ep->maxburst = 1;
bdb64d72
TB
177 break;
178 }
179 }
48767a4e
TB
180 return 0;
181}
721e2e91 182EXPORT_SYMBOL_GPL(config_ep_by_speed);
40982be5
DB
183
184/**
185 * usb_add_function() - add a function to a configuration
186 * @config: the configuration
187 * @function: the function being added
188 * Context: single threaded during gadget setup
189 *
190 * After initialization, each configuration must have one or more
191 * functions added to it. Adding a function involves calling its @bind()
192 * method to allocate resources such as interface and string identifiers
193 * and endpoints.
194 *
195 * This function returns the value of the function's bind(), which is
196 * zero for success else a negative errno value.
197 */
28824b18 198int usb_add_function(struct usb_configuration *config,
40982be5
DB
199 struct usb_function *function)
200{
201 int value = -EINVAL;
202
203 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
204 function->name, function,
205 config->label, config);
206
207 if (!function->set_alt || !function->disable)
208 goto done;
209
210 function->config = config;
211 list_add_tail(&function->list, &config->functions);
212
d5bb9b81
RB
213 if (function->bind_deactivated) {
214 value = usb_function_deactivate(function);
215 if (value)
216 goto done;
217 }
218
40982be5
DB
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 */
10287bae 234 if (!config->fullspeed && function->fs_descriptors)
40982be5
DB
235 config->fullspeed = true;
236 if (!config->highspeed && function->hs_descriptors)
237 config->highspeed = true;
bdb64d72
TB
238 if (!config->superspeed && function->ss_descriptors)
239 config->superspeed = true;
554eead5
JY
240 if (!config->superspeed_plus && function->ssp_descriptors)
241 config->superspeed_plus = true;
40982be5
DB
242
243done:
244 if (value)
245 DBG(config->cdev, "adding '%s'/%p --> %d\n",
246 function->name, function, value);
247 return value;
248}
721e2e91 249EXPORT_SYMBOL_GPL(usb_add_function);
40982be5 250
b4735778
SAS
251void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
252{
253 if (f->disable)
254 f->disable(f);
255
256 bitmap_zero(f->endpoints, 32);
257 list_del(&f->list);
258 if (f->unbind)
259 f->unbind(c, f);
260}
261EXPORT_SYMBOL_GPL(usb_remove_function);
262
60beed95
DB
263/**
264 * usb_function_deactivate - prevent function and gadget enumeration
265 * @function: the function that isn't yet ready to respond
266 *
267 * Blocks response of the gadget driver to host enumeration by
268 * preventing the data line pullup from being activated. This is
269 * normally called during @bind() processing to change from the
270 * initial "ready to respond" state, or when a required resource
271 * becomes available.
272 *
273 * For example, drivers that serve as a passthrough to a userspace
274 * daemon can block enumeration unless that daemon (such as an OBEX,
275 * MTP, or print server) is ready to handle host requests.
276 *
277 * Not all systems support software control of their USB peripheral
278 * data pullups.
279 *
280 * Returns zero on success, else negative errno.
281 */
282int usb_function_deactivate(struct usb_function *function)
283{
284 struct usb_composite_dev *cdev = function->config->cdev;
b2bdf3a7 285 unsigned long flags;
60beed95
DB
286 int status = 0;
287
b2bdf3a7 288 spin_lock_irqsave(&cdev->lock, flags);
60beed95
DB
289
290 if (cdev->deactivations == 0)
5601250b 291 status = usb_gadget_deactivate(cdev->gadget);
60beed95
DB
292 if (status == 0)
293 cdev->deactivations++;
294
b2bdf3a7 295 spin_unlock_irqrestore(&cdev->lock, flags);
60beed95
DB
296 return status;
297}
721e2e91 298EXPORT_SYMBOL_GPL(usb_function_deactivate);
60beed95
DB
299
300/**
301 * usb_function_activate - allow function and gadget enumeration
302 * @function: function on which usb_function_activate() was called
303 *
304 * Reverses effect of usb_function_deactivate(). If no more functions
305 * are delaying their activation, the gadget driver will respond to
306 * host enumeration procedures.
307 *
308 * Returns zero on success, else negative errno.
309 */
310int usb_function_activate(struct usb_function *function)
311{
312 struct usb_composite_dev *cdev = function->config->cdev;
4fefe9f6 313 unsigned long flags;
60beed95
DB
314 int status = 0;
315
4fefe9f6 316 spin_lock_irqsave(&cdev->lock, flags);
60beed95
DB
317
318 if (WARN_ON(cdev->deactivations == 0))
319 status = -EINVAL;
320 else {
321 cdev->deactivations--;
322 if (cdev->deactivations == 0)
5601250b 323 status = usb_gadget_activate(cdev->gadget);
60beed95
DB
324 }
325
4fefe9f6 326 spin_unlock_irqrestore(&cdev->lock, flags);
60beed95
DB
327 return status;
328}
721e2e91 329EXPORT_SYMBOL_GPL(usb_function_activate);
60beed95 330
40982be5
DB
331/**
332 * usb_interface_id() - allocate an unused interface ID
333 * @config: configuration associated with the interface
334 * @function: function handling the interface
335 * Context: single threaded during gadget setup
336 *
337 * usb_interface_id() is called from usb_function.bind() callbacks to
338 * allocate new interface IDs. The function driver will then store that
339 * ID in interface, association, CDC union, and other descriptors. It
25985edc 340 * will also handle any control requests targeted at that interface,
40982be5
DB
341 * particularly changing its altsetting via set_alt(). There may
342 * also be class-specific or vendor-specific requests to handle.
343 *
344 * All interface identifier should be allocated using this routine, to
345 * ensure that for example different functions don't wrongly assign
346 * different meanings to the same identifier. Note that since interface
25985edc 347 * identifiers are configuration-specific, functions used in more than
40982be5
DB
348 * one configuration (or more than once in a given configuration) need
349 * multiple versions of the relevant descriptors.
350 *
351 * Returns the interface ID which was allocated; or -ENODEV if no
352 * more interface IDs can be allocated.
353 */
28824b18 354int usb_interface_id(struct usb_configuration *config,
40982be5
DB
355 struct usb_function *function)
356{
357 unsigned id = config->next_interface_id;
358
359 if (id < MAX_CONFIG_INTERFACES) {
360 config->interface[id] = function;
361 config->next_interface_id = id + 1;
362 return id;
363 }
364 return -ENODEV;
365}
721e2e91 366EXPORT_SYMBOL_GPL(usb_interface_id);
40982be5 367
8f900a9a
SAS
368static u8 encode_bMaxPower(enum usb_device_speed speed,
369 struct usb_configuration *c)
370{
371 unsigned val;
372
373 if (c->MaxPower)
374 val = c->MaxPower;
375 else
376 val = CONFIG_USB_GADGET_VBUS_DRAW;
377 if (!val)
378 return 0;
379 switch (speed) {
380 case USB_SPEED_SUPER:
381 return DIV_ROUND_UP(val, 8);
382 default:
383 return DIV_ROUND_UP(val, 2);
2b84f92b 384 }
8f900a9a
SAS
385}
386
40982be5
DB
387static int config_buf(struct usb_configuration *config,
388 enum usb_device_speed speed, void *buf, u8 type)
389{
390 struct usb_config_descriptor *c = buf;
391 void *next = buf + USB_DT_CONFIG_SIZE;
e13f17ff 392 int len;
40982be5
DB
393 struct usb_function *f;
394 int status;
395
e13f17ff 396 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
40982be5
DB
397 /* write the config descriptor */
398 c = buf;
399 c->bLength = USB_DT_CONFIG_SIZE;
400 c->bDescriptorType = type;
401 /* wTotalLength is written later */
402 c->bNumInterfaces = config->next_interface_id;
403 c->bConfigurationValue = config->bConfigurationValue;
404 c->iConfiguration = config->iConfiguration;
405 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
8f900a9a 406 c->bMaxPower = encode_bMaxPower(speed, config);
40982be5
DB
407
408 /* There may be e.g. OTG descriptors */
409 if (config->descriptors) {
410 status = usb_descriptor_fillbuf(next, len,
411 config->descriptors);
412 if (status < 0)
413 return status;
414 len -= status;
415 next += status;
416 }
417
418 /* add each function's descriptors */
419 list_for_each_entry(f, &config->functions, list) {
420 struct usb_descriptor_header **descriptors;
421
bdb64d72
TB
422 switch (speed) {
423 case USB_SPEED_SUPER:
424 descriptors = f->ss_descriptors;
425 break;
426 case USB_SPEED_HIGH:
40982be5 427 descriptors = f->hs_descriptors;
bdb64d72
TB
428 break;
429 default:
10287bae 430 descriptors = f->fs_descriptors;
bdb64d72
TB
431 }
432
40982be5
DB
433 if (!descriptors)
434 continue;
435 status = usb_descriptor_fillbuf(next, len,
436 (const struct usb_descriptor_header **) descriptors);
437 if (status < 0)
438 return status;
439 len -= status;
440 next += status;
441 }
442
443 len = next - buf;
444 c->wTotalLength = cpu_to_le16(len);
445 return len;
446}
447
448static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
449{
450 struct usb_gadget *gadget = cdev->gadget;
451 struct usb_configuration *c;
37a3a533 452 struct list_head *pos;
40982be5
DB
453 u8 type = w_value >> 8;
454 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
455
bdb64d72
TB
456 if (gadget->speed == USB_SPEED_SUPER)
457 speed = gadget->speed;
458 else if (gadget_is_dualspeed(gadget)) {
459 int hs = 0;
40982be5
DB
460 if (gadget->speed == USB_SPEED_HIGH)
461 hs = 1;
462 if (type == USB_DT_OTHER_SPEED_CONFIG)
463 hs = !hs;
464 if (hs)
465 speed = USB_SPEED_HIGH;
466
467 }
468
469 /* This is a lookup by config *INDEX* */
470 w_value &= 0xff;
37a3a533
AP
471
472 pos = &cdev->configs;
473 c = cdev->os_desc_config;
474 if (c)
475 goto check_config;
476
477 while ((pos = pos->next) != &cdev->configs) {
478 c = list_entry(pos, typeof(*c), list);
479
480 /* skip OS Descriptors config which is handled separately */
481 if (c == cdev->os_desc_config)
482 continue;
483
484check_config:
40982be5 485 /* ignore configs that won't work at this speed */
bdb64d72
TB
486 switch (speed) {
487 case USB_SPEED_SUPER:
488 if (!c->superspeed)
489 continue;
490 break;
491 case USB_SPEED_HIGH:
40982be5
DB
492 if (!c->highspeed)
493 continue;
bdb64d72
TB
494 break;
495 default:
40982be5
DB
496 if (!c->fullspeed)
497 continue;
498 }
bdb64d72 499
40982be5
DB
500 if (w_value == 0)
501 return config_buf(c, speed, cdev->req->buf, type);
502 w_value--;
503 }
504 return -EINVAL;
505}
506
507static int count_configs(struct usb_composite_dev *cdev, unsigned type)
508{
509 struct usb_gadget *gadget = cdev->gadget;
510 struct usb_configuration *c;
511 unsigned count = 0;
512 int hs = 0;
bdb64d72 513 int ss = 0;
40982be5
DB
514
515 if (gadget_is_dualspeed(gadget)) {
516 if (gadget->speed == USB_SPEED_HIGH)
517 hs = 1;
bdb64d72
TB
518 if (gadget->speed == USB_SPEED_SUPER)
519 ss = 1;
40982be5
DB
520 if (type == USB_DT_DEVICE_QUALIFIER)
521 hs = !hs;
522 }
523 list_for_each_entry(c, &cdev->configs, list) {
524 /* ignore configs that won't work at this speed */
bdb64d72
TB
525 if (ss) {
526 if (!c->superspeed)
527 continue;
528 } else if (hs) {
40982be5
DB
529 if (!c->highspeed)
530 continue;
531 } else {
532 if (!c->fullspeed)
533 continue;
534 }
535 count++;
536 }
537 return count;
538}
539
bdb64d72
TB
540/**
541 * bos_desc() - prepares the BOS descriptor.
542 * @cdev: pointer to usb_composite device to generate the bos
543 * descriptor for
544 *
545 * This function generates the BOS (Binary Device Object)
546 * descriptor and its device capabilities descriptors. The BOS
547 * descriptor should be supported by a SuperSpeed device.
548 */
549static int bos_desc(struct usb_composite_dev *cdev)
550{
551 struct usb_ext_cap_descriptor *usb_ext;
552 struct usb_ss_cap_descriptor *ss_cap;
553 struct usb_dcd_config_params dcd_config_params;
554 struct usb_bos_descriptor *bos = cdev->req->buf;
555
556 bos->bLength = USB_DT_BOS_SIZE;
557 bos->bDescriptorType = USB_DT_BOS;
558
559 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
560 bos->bNumDeviceCaps = 0;
561
562 /*
563 * A SuperSpeed device shall include the USB2.0 extension descriptor
564 * and shall support LPM when operating in USB2.0 HS mode.
565 */
566 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
567 bos->bNumDeviceCaps++;
568 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
569 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
570 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
571 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
a6615937 572 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
bdb64d72
TB
573
574 /*
575 * The Superspeed USB Capability descriptor shall be implemented by all
576 * SuperSpeed devices.
577 */
578 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
579 bos->bNumDeviceCaps++;
580 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
581 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
582 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
583 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
584 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
585 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
586 USB_FULL_SPEED_OPERATION |
587 USB_HIGH_SPEED_OPERATION |
588 USB_5GBPS_OPERATION);
589 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
590
591 /* Get Controller configuration */
592 if (cdev->gadget->ops->get_config_params)
593 cdev->gadget->ops->get_config_params(&dcd_config_params);
594 else {
089b837a 595 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
bdb64d72 596 dcd_config_params.bU2DevExitLat =
089b837a 597 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
bdb64d72
TB
598 }
599 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
600 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
601
f228a8de
JY
602 /* The SuperSpeedPlus USB Device Capability descriptor */
603 if (gadget_is_superspeed_plus(cdev->gadget)) {
604 struct usb_ssp_cap_descriptor *ssp_cap;
605
606 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
607 bos->bNumDeviceCaps++;
608
609 /*
610 * Report typical values.
611 */
612
613 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
614 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
615 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
616 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
617
618 /* SSAC = 1 (2 attributes) */
619 ssp_cap->bmAttributes = cpu_to_le32(1);
620
621 /* Min RX/TX Lane Count = 1 */
622 ssp_cap->wFunctionalitySupport = (1 << 8) | (1 << 12);
623
624 /*
625 * bmSublinkSpeedAttr[0]:
626 * ST = Symmetric, RX
627 * LSE = 3 (Gbps)
628 * LP = 1 (SuperSpeedPlus)
629 * LSM = 10 (10 Gbps)
630 */
631 ssp_cap->bmSublinkSpeedAttr[0] =
632 (3 << 4) | (1 << 14) | (0xa << 16);
633 /*
634 * bmSublinkSpeedAttr[1] =
635 * ST = Symmetric, TX
636 * LSE = 3 (Gbps)
637 * LP = 1 (SuperSpeedPlus)
638 * LSM = 10 (10 Gbps)
639 */
640 ssp_cap->bmSublinkSpeedAttr[1] =
641 (3 << 4) | (1 << 14) | (0xa << 16) | (1 << 7);
642 }
643
bdb64d72
TB
644 return le16_to_cpu(bos->wTotalLength);
645}
646
40982be5
DB
647static void device_qual(struct usb_composite_dev *cdev)
648{
649 struct usb_qualifier_descriptor *qual = cdev->req->buf;
650
651 qual->bLength = sizeof(*qual);
652 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
653 /* POLICY: same bcdUSB and device type info at both speeds */
654 qual->bcdUSB = cdev->desc.bcdUSB;
655 qual->bDeviceClass = cdev->desc.bDeviceClass;
656 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
657 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
658 /* ASSUME same EP0 fifo size at both speeds */
765f5b83 659 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
40982be5 660 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
c24f4227 661 qual->bRESERVED = 0;
40982be5
DB
662}
663
664/*-------------------------------------------------------------------------*/
665
666static void reset_config(struct usb_composite_dev *cdev)
667{
668 struct usb_function *f;
669
670 DBG(cdev, "reset config\n");
671
672 list_for_each_entry(f, &cdev->config->functions, list) {
673 if (f->disable)
674 f->disable(f);
5242658d
LP
675
676 bitmap_zero(f->endpoints, 32);
40982be5
DB
677 }
678 cdev->config = NULL;
2bac51a1 679 cdev->delayed_status = 0;
40982be5
DB
680}
681
682static int set_config(struct usb_composite_dev *cdev,
683 const struct usb_ctrlrequest *ctrl, unsigned number)
684{
685 struct usb_gadget *gadget = cdev->gadget;
686 struct usb_configuration *c = NULL;
687 int result = -EINVAL;
688 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
689 int tmp;
690
40982be5
DB
691 if (number) {
692 list_for_each_entry(c, &cdev->configs, list) {
693 if (c->bConfigurationValue == number) {
bdb64d72
TB
694 /*
695 * We disable the FDs of the previous
696 * configuration only if the new configuration
697 * is a valid one
698 */
699 if (cdev->config)
700 reset_config(cdev);
40982be5
DB
701 result = 0;
702 break;
703 }
704 }
705 if (result < 0)
706 goto done;
bdb64d72
TB
707 } else { /* Zero configuration value - need to reset the config */
708 if (cdev->config)
709 reset_config(cdev);
40982be5 710 result = 0;
bdb64d72 711 }
40982be5 712
e538dfda
MN
713 INFO(cdev, "%s config #%d: %s\n",
714 usb_speed_string(gadget->speed),
715 number, c ? c->label : "unconfigured");
40982be5
DB
716
717 if (!c)
718 goto done;
719
6027f317 720 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
40982be5
DB
721 cdev->config = c;
722
723 /* Initialize all interfaces by setting them to altsetting zero. */
724 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
725 struct usb_function *f = c->interface[tmp];
5242658d 726 struct usb_descriptor_header **descriptors;
40982be5
DB
727
728 if (!f)
729 break;
730
5242658d
LP
731 /*
732 * Record which endpoints are used by the function. This is used
733 * to dispatch control requests targeted at that endpoint to the
734 * function's setup callback instead of the current
735 * configuration's setup callback.
736 */
bdb64d72
TB
737 switch (gadget->speed) {
738 case USB_SPEED_SUPER:
739 descriptors = f->ss_descriptors;
740 break;
741 case USB_SPEED_HIGH:
5242658d 742 descriptors = f->hs_descriptors;
bdb64d72
TB
743 break;
744 default:
10287bae 745 descriptors = f->fs_descriptors;
bdb64d72 746 }
5242658d
LP
747
748 for (; *descriptors; ++descriptors) {
749 struct usb_endpoint_descriptor *ep;
750 int addr;
751
752 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
753 continue;
754
755 ep = (struct usb_endpoint_descriptor *)*descriptors;
756 addr = ((ep->bEndpointAddress & 0x80) >> 3)
757 | (ep->bEndpointAddress & 0x0f);
758 set_bit(addr, f->endpoints);
759 }
760
40982be5
DB
761 result = f->set_alt(f, tmp, 0);
762 if (result < 0) {
763 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
764 tmp, f->name, f, result);
765
766 reset_config(cdev);
767 goto done;
768 }
1b9ba000
RQ
769
770 if (result == USB_GADGET_DELAYED_STATUS) {
771 DBG(cdev,
772 "%s: interface %d (%s) requested delayed status\n",
773 __func__, tmp, f->name);
774 cdev->delayed_status++;
775 DBG(cdev, "delayed_status count %d\n",
776 cdev->delayed_status);
777 }
40982be5
DB
778 }
779
780 /* when we return, be sure our power usage is valid */
8f900a9a 781 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
40982be5
DB
782done:
783 usb_gadget_vbus_draw(gadget, power);
1b9ba000
RQ
784 if (result >= 0 && cdev->delayed_status)
785 result = USB_GADGET_DELAYED_STATUS;
40982be5
DB
786 return result;
787}
788
de53c254
SAS
789int usb_add_config_only(struct usb_composite_dev *cdev,
790 struct usb_configuration *config)
791{
792 struct usb_configuration *c;
793
794 if (!config->bConfigurationValue)
795 return -EINVAL;
796
797 /* Prevent duplicate configuration identifiers */
798 list_for_each_entry(c, &cdev->configs, list) {
799 if (c->bConfigurationValue == config->bConfigurationValue)
800 return -EBUSY;
801 }
802
803 config->cdev = cdev;
804 list_add_tail(&config->list, &cdev->configs);
805
806 INIT_LIST_HEAD(&config->functions);
807 config->next_interface_id = 0;
808 memset(config->interface, 0, sizeof(config->interface));
809
810 return 0;
811}
812EXPORT_SYMBOL_GPL(usb_add_config_only);
813
40982be5
DB
814/**
815 * usb_add_config() - add a configuration to a device.
816 * @cdev: wraps the USB gadget
817 * @config: the configuration, with bConfigurationValue assigned
c9bfff9c 818 * @bind: the configuration's bind function
40982be5
DB
819 * Context: single threaded during gadget setup
820 *
c9bfff9c 821 * One of the main tasks of a composite @bind() routine is to
40982be5
DB
822 * add each of the configurations it supports, using this routine.
823 *
c9bfff9c 824 * This function returns the value of the configuration's @bind(), which
40982be5
DB
825 * is zero for success else a negative errno value. Binding configurations
826 * assigns global resources including string IDs, and per-configuration
827 * resources such as interface IDs and endpoints.
828 */
28824b18 829int usb_add_config(struct usb_composite_dev *cdev,
c9bfff9c
UKK
830 struct usb_configuration *config,
831 int (*bind)(struct usb_configuration *))
40982be5
DB
832{
833 int status = -EINVAL;
de53c254
SAS
834
835 if (!bind)
836 goto done;
40982be5
DB
837
838 DBG(cdev, "adding config #%u '%s'/%p\n",
839 config->bConfigurationValue,
840 config->label, config);
841
de53c254
SAS
842 status = usb_add_config_only(cdev, config);
843 if (status)
40982be5
DB
844 goto done;
845
c9bfff9c 846 status = bind(config);
40982be5 847 if (status < 0) {
124ef389
YO
848 while (!list_empty(&config->functions)) {
849 struct usb_function *f;
850
851 f = list_first_entry(&config->functions,
852 struct usb_function, list);
853 list_del(&f->list);
854 if (f->unbind) {
855 DBG(cdev, "unbind function '%s'/%p\n",
856 f->name, f);
857 f->unbind(config, f);
858 /* may free memory for "f" */
859 }
860 }
40982be5
DB
861 list_del(&config->list);
862 config->cdev = NULL;
863 } else {
864 unsigned i;
865
bdb64d72 866 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
40982be5 867 config->bConfigurationValue, config,
bdb64d72 868 config->superspeed ? " super" : "",
40982be5
DB
869 config->highspeed ? " high" : "",
870 config->fullspeed
871 ? (gadget_is_dualspeed(cdev->gadget)
872 ? " full"
873 : " full/low")
874 : "");
875
876 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
877 struct usb_function *f = config->interface[i];
878
879 if (!f)
880 continue;
881 DBG(cdev, " interface %d = %s/%p\n",
882 i, f->name, f);
883 }
884 }
885
f871cb9b 886 /* set_alt(), or next bind(), sets up ep->claimed as needed */
40982be5
DB
887 usb_ep_autoconfig_reset(cdev->gadget);
888
889done:
890 if (status)
891 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
892 config->bConfigurationValue, status);
893 return status;
894}
721e2e91 895EXPORT_SYMBOL_GPL(usb_add_config);
40982be5 896
51cce6fc
BG
897static void remove_config(struct usb_composite_dev *cdev,
898 struct usb_configuration *config)
899{
900 while (!list_empty(&config->functions)) {
901 struct usb_function *f;
902
903 f = list_first_entry(&config->functions,
904 struct usb_function, list);
905 list_del(&f->list);
906 if (f->unbind) {
907 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
908 f->unbind(config, f);
909 /* may free memory for "f" */
910 }
911 }
912 list_del(&config->list);
913 if (config->unbind) {
914 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
915 config->unbind(config);
916 /* may free memory for "c" */
917 }
918}
919
920/**
921 * usb_remove_config() - remove a configuration from a device.
922 * @cdev: wraps the USB gadget
923 * @config: the configuration
924 *
925 * Drivers must call usb_gadget_disconnect before calling this function
926 * to disconnect the device from the host and make sure the host will not
927 * try to enumerate the device while we are changing the config list.
928 */
929void usb_remove_config(struct usb_composite_dev *cdev,
930 struct usb_configuration *config)
931{
932 unsigned long flags;
933
934 spin_lock_irqsave(&cdev->lock, flags);
935
936 if (cdev->config == config)
937 reset_config(cdev);
938
939 spin_unlock_irqrestore(&cdev->lock, flags);
940
941 remove_config(cdev, config);
942}
943
40982be5
DB
944/*-------------------------------------------------------------------------*/
945
946/* We support strings in multiple languages ... string descriptor zero
947 * says which languages are supported. The typical case will be that
ad4676ab 948 * only one language (probably English) is used, with i18n handled on
40982be5
DB
949 * the host side.
950 */
951
952static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
953{
954 const struct usb_gadget_strings *s;
20c5e74c 955 __le16 language;
40982be5
DB
956 __le16 *tmp;
957
958 while (*sp) {
959 s = *sp;
960 language = cpu_to_le16(s->language);
961 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
962 if (*tmp == language)
963 goto repeat;
964 }
965 *tmp++ = language;
966repeat:
967 sp++;
968 }
969}
970
971static int lookup_string(
972 struct usb_gadget_strings **sp,
973 void *buf,
974 u16 language,
975 int id
976)
977{
978 struct usb_gadget_strings *s;
979 int value;
980
981 while (*sp) {
982 s = *sp++;
983 if (s->language != language)
984 continue;
985 value = usb_gadget_get_string(s, id, buf);
986 if (value > 0)
987 return value;
988 }
989 return -EINVAL;
990}
991
992static int get_string(struct usb_composite_dev *cdev,
993 void *buf, u16 language, int id)
994{
ffe0b335 995 struct usb_composite_driver *composite = cdev->driver;
9bb2859f 996 struct usb_gadget_string_container *uc;
40982be5
DB
997 struct usb_configuration *c;
998 struct usb_function *f;
999 int len;
1000
ad4676ab 1001 /* Yes, not only is USB's i18n support probably more than most
40982be5
DB
1002 * folk will ever care about ... also, it's all supported here.
1003 * (Except for UTF8 support for Unicode's "Astral Planes".)
1004 */
1005
1006 /* 0 == report all available language codes */
1007 if (id == 0) {
1008 struct usb_string_descriptor *s = buf;
1009 struct usb_gadget_strings **sp;
1010
1011 memset(s, 0, 256);
1012 s->bDescriptorType = USB_DT_STRING;
1013
1014 sp = composite->strings;
1015 if (sp)
1016 collect_langs(sp, s->wData);
1017
1018 list_for_each_entry(c, &cdev->configs, list) {
1019 sp = c->strings;
1020 if (sp)
1021 collect_langs(sp, s->wData);
1022
1023 list_for_each_entry(f, &c->functions, list) {
1024 sp = f->strings;
1025 if (sp)
1026 collect_langs(sp, s->wData);
1027 }
1028 }
27a46633
SAS
1029 list_for_each_entry(uc, &cdev->gstrings, list) {
1030 struct usb_gadget_strings **sp;
1031
1032 sp = get_containers_gs(uc);
1033 collect_langs(sp, s->wData);
1034 }
40982be5 1035
417b57b3 1036 for (len = 0; len <= 126 && s->wData[len]; len++)
40982be5
DB
1037 continue;
1038 if (!len)
1039 return -EINVAL;
1040
1041 s->bLength = 2 * (len + 1);
1042 return s->bLength;
1043 }
1044
19824d5e
AP
1045 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1046 struct usb_os_string *b = buf;
1047 b->bLength = sizeof(*b);
1048 b->bDescriptorType = USB_DT_STRING;
1049 compiletime_assert(
1050 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1051 "qwSignature size must be equal to qw_sign");
1052 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1053 b->bMS_VendorCode = cdev->b_vendor_code;
1054 b->bPad = 0;
1055 return sizeof(*b);
1056 }
1057
9bb2859f
SAS
1058 list_for_each_entry(uc, &cdev->gstrings, list) {
1059 struct usb_gadget_strings **sp;
1060
1061 sp = get_containers_gs(uc);
1062 len = lookup_string(sp, buf, language, id);
1063 if (len > 0)
1064 return len;
1065 }
1066
ad1a8102
MN
1067 /* String IDs are device-scoped, so we look up each string
1068 * table we're told about. These lookups are infrequent;
1069 * simpler-is-better here.
40982be5
DB
1070 */
1071 if (composite->strings) {
1072 len = lookup_string(composite->strings, buf, language, id);
1073 if (len > 0)
1074 return len;
1075 }
1076 list_for_each_entry(c, &cdev->configs, list) {
1077 if (c->strings) {
1078 len = lookup_string(c->strings, buf, language, id);
1079 if (len > 0)
1080 return len;
1081 }
1082 list_for_each_entry(f, &c->functions, list) {
1083 if (!f->strings)
1084 continue;
1085 len = lookup_string(f->strings, buf, language, id);
1086 if (len > 0)
1087 return len;
1088 }
1089 }
1090 return -EINVAL;
1091}
1092
1093/**
1094 * usb_string_id() - allocate an unused string ID
1095 * @cdev: the device whose string descriptor IDs are being allocated
1096 * Context: single threaded during gadget setup
1097 *
1098 * @usb_string_id() is called from bind() callbacks to allocate
1099 * string IDs. Drivers for functions, configurations, or gadgets will
1100 * then store that ID in the appropriate descriptors and string table.
1101 *
f2adc4f8
MN
1102 * All string identifier should be allocated using this,
1103 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1104 * that for example different functions don't wrongly assign different
1105 * meanings to the same identifier.
40982be5 1106 */
28824b18 1107int usb_string_id(struct usb_composite_dev *cdev)
40982be5
DB
1108{
1109 if (cdev->next_string_id < 254) {
f2adc4f8
MN
1110 /* string id 0 is reserved by USB spec for list of
1111 * supported languages */
1112 /* 255 reserved as well? -- mina86 */
40982be5
DB
1113 cdev->next_string_id++;
1114 return cdev->next_string_id;
1115 }
1116 return -ENODEV;
1117}
721e2e91 1118EXPORT_SYMBOL_GPL(usb_string_id);
40982be5 1119
f2adc4f8
MN
1120/**
1121 * usb_string_ids() - allocate unused string IDs in batch
1122 * @cdev: the device whose string descriptor IDs are being allocated
1123 * @str: an array of usb_string objects to assign numbers to
1124 * Context: single threaded during gadget setup
1125 *
1126 * @usb_string_ids() is called from bind() callbacks to allocate
1127 * string IDs. Drivers for functions, configurations, or gadgets will
1128 * then copy IDs from the string table to the appropriate descriptors
1129 * and string table for other languages.
1130 *
1131 * All string identifier should be allocated using this,
1132 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1133 * example different functions don't wrongly assign different meanings
1134 * to the same identifier.
1135 */
1136int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1137{
1138 int next = cdev->next_string_id;
1139
1140 for (; str->s; ++str) {
1141 if (unlikely(next >= 254))
1142 return -ENODEV;
1143 str->id = ++next;
1144 }
1145
1146 cdev->next_string_id = next;
1147
1148 return 0;
1149}
721e2e91 1150EXPORT_SYMBOL_GPL(usb_string_ids_tab);
f2adc4f8 1151
9bb2859f
SAS
1152static struct usb_gadget_string_container *copy_gadget_strings(
1153 struct usb_gadget_strings **sp, unsigned n_gstrings,
1154 unsigned n_strings)
1155{
1156 struct usb_gadget_string_container *uc;
1157 struct usb_gadget_strings **gs_array;
1158 struct usb_gadget_strings *gs;
1159 struct usb_string *s;
1160 unsigned mem;
1161 unsigned n_gs;
1162 unsigned n_s;
1163 void *stash;
1164
1165 mem = sizeof(*uc);
1166 mem += sizeof(void *) * (n_gstrings + 1);
1167 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1168 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1169 uc = kmalloc(mem, GFP_KERNEL);
1170 if (!uc)
1171 return ERR_PTR(-ENOMEM);
1172 gs_array = get_containers_gs(uc);
1173 stash = uc->stash;
1174 stash += sizeof(void *) * (n_gstrings + 1);
1175 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1176 struct usb_string *org_s;
1177
1178 gs_array[n_gs] = stash;
1179 gs = gs_array[n_gs];
1180 stash += sizeof(struct usb_gadget_strings);
1181 gs->language = sp[n_gs]->language;
1182 gs->strings = stash;
1183 org_s = sp[n_gs]->strings;
1184
1185 for (n_s = 0; n_s < n_strings; n_s++) {
1186 s = stash;
1187 stash += sizeof(struct usb_string);
1188 if (org_s->s)
1189 s->s = org_s->s;
1190 else
1191 s->s = "";
1192 org_s++;
1193 }
1194 s = stash;
1195 s->s = NULL;
1196 stash += sizeof(struct usb_string);
1197
1198 }
1199 gs_array[n_gs] = NULL;
1200 return uc;
1201}
1202
1203/**
1204 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1205 * @cdev: the device whose string descriptor IDs are being allocated
1206 * and attached.
1207 * @sp: an array of usb_gadget_strings to attach.
1208 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1209 *
1210 * This function will create a deep copy of usb_gadget_strings and usb_string
1211 * and attach it to the cdev. The actual string (usb_string.s) will not be
1212 * copied but only a referenced will be made. The struct usb_gadget_strings
06ed0de5 1213 * array may contain multiple languages and should be NULL terminated.
9bb2859f
SAS
1214 * The ->language pointer of each struct usb_gadget_strings has to contain the
1215 * same amount of entries.
1216 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
06ed0de5 1217 * usb_string entry of es-ES contains the translation of the first usb_string
9bb2859f
SAS
1218 * entry of en-US. Therefore both entries become the same id assign.
1219 */
1220struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1221 struct usb_gadget_strings **sp, unsigned n_strings)
1222{
1223 struct usb_gadget_string_container *uc;
1224 struct usb_gadget_strings **n_gs;
1225 unsigned n_gstrings = 0;
1226 unsigned i;
1227 int ret;
1228
1229 for (i = 0; sp[i]; i++)
1230 n_gstrings++;
1231
1232 if (!n_gstrings)
1233 return ERR_PTR(-EINVAL);
1234
1235 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1236 if (IS_ERR(uc))
dad4babe 1237 return ERR_CAST(uc);
9bb2859f
SAS
1238
1239 n_gs = get_containers_gs(uc);
1240 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1241 if (ret)
1242 goto err;
1243
1244 for (i = 1; i < n_gstrings; i++) {
1245 struct usb_string *m_s;
1246 struct usb_string *s;
1247 unsigned n;
1248
1249 m_s = n_gs[0]->strings;
1250 s = n_gs[i]->strings;
1251 for (n = 0; n < n_strings; n++) {
1252 s->id = m_s->id;
1253 s++;
1254 m_s++;
1255 }
1256 }
1257 list_add_tail(&uc->list, &cdev->gstrings);
1258 return n_gs[0]->strings;
1259err:
1260 kfree(uc);
1261 return ERR_PTR(ret);
1262}
1263EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1264
f2adc4f8
MN
1265/**
1266 * usb_string_ids_n() - allocate unused string IDs in batch
d187abb9 1267 * @c: the device whose string descriptor IDs are being allocated
f2adc4f8
MN
1268 * @n: number of string IDs to allocate
1269 * Context: single threaded during gadget setup
1270 *
1271 * Returns the first requested ID. This ID and next @n-1 IDs are now
d187abb9 1272 * valid IDs. At least provided that @n is non-zero because if it
f2adc4f8
MN
1273 * is, returns last requested ID which is now very useful information.
1274 *
1275 * @usb_string_ids_n() is called from bind() callbacks to allocate
1276 * string IDs. Drivers for functions, configurations, or gadgets will
1277 * then store that ID in the appropriate descriptors and string table.
1278 *
1279 * All string identifier should be allocated using this,
1280 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1281 * example different functions don't wrongly assign different meanings
1282 * to the same identifier.
1283 */
1284int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1285{
1286 unsigned next = c->next_string_id;
1287 if (unlikely(n > 254 || (unsigned)next + n > 254))
1288 return -ENODEV;
1289 c->next_string_id += n;
1290 return next + 1;
1291}
721e2e91 1292EXPORT_SYMBOL_GPL(usb_string_ids_n);
f2adc4f8 1293
40982be5
DB
1294/*-------------------------------------------------------------------------*/
1295
1296static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1297{
a7c12eaf
FB
1298 struct usb_composite_dev *cdev;
1299
40982be5
DB
1300 if (req->status || req->actual != req->length)
1301 DBG((struct usb_composite_dev *) ep->driver_data,
1302 "setup complete --> %d, %d/%d\n",
1303 req->status, req->actual, req->length);
a7c12eaf
FB
1304
1305 /*
1306 * REVIST The same ep0 requests are shared with function drivers
1307 * so they don't have to maintain the same ->complete() stubs.
1308 *
1309 * Because of that, we need to check for the validity of ->context
1310 * here, even though we know we've set it to something useful.
1311 */
1312 if (!req->context)
1313 return;
1314
1315 cdev = req->context;
1316
1317 if (cdev->req == req)
1318 cdev->setup_pending = false;
1319 else if (cdev->os_desc_req == req)
1320 cdev->os_desc_pending = false;
1321 else
1322 WARN(1, "unknown request %p\n", req);
1323}
1324
1325static int composite_ep0_queue(struct usb_composite_dev *cdev,
1326 struct usb_request *req, gfp_t gfp_flags)
1327{
1328 int ret;
1329
1330 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1331 if (ret == 0) {
1332 if (cdev->req == req)
1333 cdev->setup_pending = true;
1334 else if (cdev->os_desc_req == req)
1335 cdev->os_desc_pending = true;
1336 else
1337 WARN(1, "unknown request %p\n", req);
1338 }
1339
1340 return ret;
40982be5
DB
1341}
1342
37a3a533
AP
1343static int count_ext_compat(struct usb_configuration *c)
1344{
1345 int i, res;
1346
1347 res = 0;
1348 for (i = 0; i < c->next_interface_id; ++i) {
1349 struct usb_function *f;
1350 int j;
1351
1352 f = c->interface[i];
1353 for (j = 0; j < f->os_desc_n; ++j) {
1354 struct usb_os_desc *d;
1355
1356 if (i != f->os_desc_table[j].if_id)
1357 continue;
1358 d = f->os_desc_table[j].os_desc;
1359 if (d && d->ext_compat_id)
1360 ++res;
1361 }
1362 }
1363 BUG_ON(res > 255);
1364 return res;
1365}
1366
1367static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1368{
1369 int i, count;
1370
1371 count = 16;
1372 for (i = 0; i < c->next_interface_id; ++i) {
1373 struct usb_function *f;
1374 int j;
1375
1376 f = c->interface[i];
1377 for (j = 0; j < f->os_desc_n; ++j) {
1378 struct usb_os_desc *d;
1379
1380 if (i != f->os_desc_table[j].if_id)
1381 continue;
1382 d = f->os_desc_table[j].os_desc;
1383 if (d && d->ext_compat_id) {
1384 *buf++ = i;
1385 *buf++ = 0x01;
1386 memcpy(buf, d->ext_compat_id, 16);
1387 buf += 22;
1388 } else {
1389 ++buf;
1390 *buf = 0x01;
1391 buf += 23;
1392 }
1393 count += 24;
1394 if (count >= 4096)
1395 return;
1396 }
1397 }
1398}
1399
1400static int count_ext_prop(struct usb_configuration *c, int interface)
1401{
1402 struct usb_function *f;
849b1333 1403 int j;
37a3a533
AP
1404
1405 f = c->interface[interface];
1406 for (j = 0; j < f->os_desc_n; ++j) {
1407 struct usb_os_desc *d;
1408
1409 if (interface != f->os_desc_table[j].if_id)
1410 continue;
1411 d = f->os_desc_table[j].os_desc;
1412 if (d && d->ext_compat_id)
1413 return d->ext_prop_count;
1414 }
849b1333 1415 return 0;
37a3a533
AP
1416}
1417
1418static int len_ext_prop(struct usb_configuration *c, int interface)
1419{
1420 struct usb_function *f;
1421 struct usb_os_desc *d;
1422 int j, res;
1423
1424 res = 10; /* header length */
1425 f = c->interface[interface];
1426 for (j = 0; j < f->os_desc_n; ++j) {
1427 if (interface != f->os_desc_table[j].if_id)
1428 continue;
1429 d = f->os_desc_table[j].os_desc;
1430 if (d)
1431 return min(res + d->ext_prop_len, 4096);
1432 }
1433 return res;
1434}
1435
1436static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1437{
1438 struct usb_function *f;
1439 struct usb_os_desc *d;
1440 struct usb_os_desc_ext_prop *ext_prop;
1441 int j, count, n, ret;
1442 u8 *start = buf;
1443
1444 f = c->interface[interface];
1445 for (j = 0; j < f->os_desc_n; ++j) {
1446 if (interface != f->os_desc_table[j].if_id)
1447 continue;
1448 d = f->os_desc_table[j].os_desc;
1449 if (d)
1450 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1451 /* 4kB minus header length */
1452 n = buf - start;
1453 if (n >= 4086)
1454 return 0;
1455
1456 count = ext_prop->data_len +
1457 ext_prop->name_len + 14;
1458 if (count > 4086 - n)
1459 return -EINVAL;
1460 usb_ext_prop_put_size(buf, count);
1461 usb_ext_prop_put_type(buf, ext_prop->type);
1462 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1463 ext_prop->name_len);
1464 if (ret < 0)
1465 return ret;
1466 switch (ext_prop->type) {
1467 case USB_EXT_PROP_UNICODE:
1468 case USB_EXT_PROP_UNICODE_ENV:
1469 case USB_EXT_PROP_UNICODE_LINK:
1470 usb_ext_prop_put_unicode(buf, ret,
1471 ext_prop->data,
1472 ext_prop->data_len);
1473 break;
1474 case USB_EXT_PROP_BINARY:
1475 usb_ext_prop_put_binary(buf, ret,
1476 ext_prop->data,
1477 ext_prop->data_len);
1478 break;
1479 case USB_EXT_PROP_LE32:
1480 /* not implemented */
1481 case USB_EXT_PROP_BE32:
1482 /* not implemented */
1483 default:
1484 return -EINVAL;
1485 }
1486 buf += count;
1487 }
1488 }
1489
1490 return 0;
1491}
1492
40982be5
DB
1493/*
1494 * The setup() callback implements all the ep0 functionality that's
1495 * not handled lower down, in hardware or the hardware driver(like
1496 * device and endpoint feature flags, and their status). It's all
1497 * housekeeping for the gadget function we're implementing. Most of
1498 * the work is in config and function specific setup.
1499 */
2d5a8899 1500int
40982be5
DB
1501composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1502{
1503 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1504 struct usb_request *req = cdev->req;
1505 int value = -EOPNOTSUPP;
bdb64d72 1506 int status = 0;
40982be5 1507 u16 w_index = le16_to_cpu(ctrl->wIndex);
08889517 1508 u8 intf = w_index & 0xFF;
40982be5
DB
1509 u16 w_value = le16_to_cpu(ctrl->wValue);
1510 u16 w_length = le16_to_cpu(ctrl->wLength);
1511 struct usb_function *f = NULL;
5242658d 1512 u8 endp;
40982be5
DB
1513
1514 /* partial re-init of the response message; the function or the
1515 * gadget might need to intercept e.g. a control-OUT completion
1516 * when we delegate to it.
1517 */
1518 req->zero = 0;
57943716 1519 req->context = cdev;
40982be5 1520 req->complete = composite_setup_complete;
2edb11cb 1521 req->length = 0;
40982be5
DB
1522 gadget->ep0->driver_data = cdev;
1523
232c0102
AP
1524 /*
1525 * Don't let non-standard requests match any of the cases below
1526 * by accident.
1527 */
1528 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1529 goto unknown;
1530
40982be5
DB
1531 switch (ctrl->bRequest) {
1532
1533 /* we handle all standard USB descriptors */
1534 case USB_REQ_GET_DESCRIPTOR:
1535 if (ctrl->bRequestType != USB_DIR_IN)
1536 goto unknown;
1537 switch (w_value >> 8) {
1538
1539 case USB_DT_DEVICE:
1540 cdev->desc.bNumConfigurations =
1541 count_configs(cdev, USB_DT_DEVICE);
bdb64d72
TB
1542 cdev->desc.bMaxPacketSize0 =
1543 cdev->gadget->ep0->maxpacket;
1544 if (gadget_is_superspeed(gadget)) {
a8f21156 1545 if (gadget->speed >= USB_SPEED_SUPER) {
1a853291 1546 cdev->desc.bcdUSB = cpu_to_le16(0x0310);
a8f21156
SAS
1547 cdev->desc.bMaxPacketSize0 = 9;
1548 } else {
bdb64d72 1549 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
a8f21156 1550 }
5527e733
IK
1551 } else {
1552 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
bdb64d72
TB
1553 }
1554
40982be5
DB
1555 value = min(w_length, (u16) sizeof cdev->desc);
1556 memcpy(req->buf, &cdev->desc, value);
1557 break;
1558 case USB_DT_DEVICE_QUALIFIER:
bdb64d72
TB
1559 if (!gadget_is_dualspeed(gadget) ||
1560 gadget->speed >= USB_SPEED_SUPER)
40982be5
DB
1561 break;
1562 device_qual(cdev);
1563 value = min_t(int, w_length,
1564 sizeof(struct usb_qualifier_descriptor));
1565 break;
1566 case USB_DT_OTHER_SPEED_CONFIG:
bdb64d72
TB
1567 if (!gadget_is_dualspeed(gadget) ||
1568 gadget->speed >= USB_SPEED_SUPER)
40982be5
DB
1569 break;
1570 /* FALLTHROUGH */
1571 case USB_DT_CONFIG:
1572 value = config_desc(cdev, w_value);
1573 if (value >= 0)
1574 value = min(w_length, (u16) value);
1575 break;
1576 case USB_DT_STRING:
1577 value = get_string(cdev, req->buf,
1578 w_index, w_value & 0xff);
1579 if (value >= 0)
1580 value = min(w_length, (u16) value);
1581 break;
bdb64d72
TB
1582 case USB_DT_BOS:
1583 if (gadget_is_superspeed(gadget)) {
1584 value = bos_desc(cdev);
1585 value = min(w_length, (u16) value);
1586 }
1587 break;
53e6242d
ML
1588 case USB_DT_OTG:
1589 if (gadget_is_otg(gadget)) {
1590 struct usb_configuration *config;
1591 int otg_desc_len = 0;
1592
1593 if (cdev->config)
1594 config = cdev->config;
1595 else
1596 config = list_first_entry(
1597 &cdev->configs,
1598 struct usb_configuration, list);
1599 if (!config)
1600 goto done;
1601
1602 if (gadget->otg_caps &&
1603 (gadget->otg_caps->otg_rev >= 0x0200))
1604 otg_desc_len += sizeof(
1605 struct usb_otg20_descriptor);
1606 else
1607 otg_desc_len += sizeof(
1608 struct usb_otg_descriptor);
1609
1610 value = min_t(int, w_length, otg_desc_len);
1611 memcpy(req->buf, config->descriptors[0], value);
1612 }
1613 break;
40982be5
DB
1614 }
1615 break;
1616
1617 /* any number of configs can work */
1618 case USB_REQ_SET_CONFIGURATION:
1619 if (ctrl->bRequestType != 0)
1620 goto unknown;
1621 if (gadget_is_otg(gadget)) {
1622 if (gadget->a_hnp_support)
1623 DBG(cdev, "HNP available\n");
1624 else if (gadget->a_alt_hnp_support)
1625 DBG(cdev, "HNP on another port\n");
1626 else
1627 VDBG(cdev, "HNP inactive\n");
1628 }
1629 spin_lock(&cdev->lock);
1630 value = set_config(cdev, ctrl, w_value);
1631 spin_unlock(&cdev->lock);
1632 break;
1633 case USB_REQ_GET_CONFIGURATION:
1634 if (ctrl->bRequestType != USB_DIR_IN)
1635 goto unknown;
1636 if (cdev->config)
1637 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1638 else
1639 *(u8 *)req->buf = 0;
1640 value = min(w_length, (u16) 1);
1641 break;
1642
1643 /* function drivers must handle get/set altsetting; if there's
1644 * no get() method, we know only altsetting zero works.
1645 */
1646 case USB_REQ_SET_INTERFACE:
1647 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1648 goto unknown;
ff085de7 1649 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
40982be5 1650 break;
08889517 1651 f = cdev->config->interface[intf];
40982be5
DB
1652 if (!f)
1653 break;
dd4dff8b 1654 if (w_value && !f->set_alt)
40982be5
DB
1655 break;
1656 value = f->set_alt(f, w_index, w_value);
1b9ba000
RQ
1657 if (value == USB_GADGET_DELAYED_STATUS) {
1658 DBG(cdev,
1659 "%s: interface %d (%s) requested delayed status\n",
1660 __func__, intf, f->name);
1661 cdev->delayed_status++;
1662 DBG(cdev, "delayed_status count %d\n",
1663 cdev->delayed_status);
1664 }
40982be5
DB
1665 break;
1666 case USB_REQ_GET_INTERFACE:
1667 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1668 goto unknown;
ff085de7 1669 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
40982be5 1670 break;
08889517 1671 f = cdev->config->interface[intf];
40982be5
DB
1672 if (!f)
1673 break;
1674 /* lots of interfaces only need altsetting zero... */
1675 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1676 if (value < 0)
1677 break;
1678 *((u8 *)req->buf) = value;
1679 value = min(w_length, (u16) 1);
1680 break;
bdb64d72
TB
1681
1682 /*
1683 * USB 3.0 additions:
1684 * Function driver should handle get_status request. If such cb
1685 * wasn't supplied we respond with default value = 0
1686 * Note: function driver should supply such cb only for the first
1687 * interface of the function
1688 */
1689 case USB_REQ_GET_STATUS:
1690 if (!gadget_is_superspeed(gadget))
1691 goto unknown;
1692 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1693 goto unknown;
1694 value = 2; /* This is the length of the get_status reply */
1695 put_unaligned_le16(0, req->buf);
1696 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1697 break;
1698 f = cdev->config->interface[intf];
1699 if (!f)
1700 break;
1701 status = f->get_status ? f->get_status(f) : 0;
1702 if (status < 0)
1703 break;
1704 put_unaligned_le16(status & 0x0000ffff, req->buf);
1705 break;
1706 /*
1707 * Function drivers should handle SetFeature/ClearFeature
1708 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1709 * only for the first interface of the function
1710 */
1711 case USB_REQ_CLEAR_FEATURE:
1712 case USB_REQ_SET_FEATURE:
1713 if (!gadget_is_superspeed(gadget))
1714 goto unknown;
1715 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1716 goto unknown;
1717 switch (w_value) {
1718 case USB_INTRF_FUNC_SUSPEND:
1719 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1720 break;
1721 f = cdev->config->interface[intf];
1722 if (!f)
1723 break;
1724 value = 0;
1725 if (f->func_suspend)
1726 value = f->func_suspend(f, w_index >> 8);
1727 if (value < 0) {
1728 ERROR(cdev,
1729 "func_suspend() returned error %d\n",
1730 value);
1731 value = 0;
1732 }
1733 break;
1734 }
1735 break;
40982be5
DB
1736 default:
1737unknown:
37a3a533
AP
1738 /*
1739 * OS descriptors handling
1740 */
1741 if (cdev->use_os_string && cdev->os_desc_config &&
df6738d0 1742 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
37a3a533
AP
1743 ctrl->bRequest == cdev->b_vendor_code) {
1744 struct usb_request *req;
1745 struct usb_configuration *os_desc_cfg;
1746 u8 *buf;
1747 int interface;
1748 int count = 0;
1749
1750 req = cdev->os_desc_req;
57943716 1751 req->context = cdev;
37a3a533
AP
1752 req->complete = composite_setup_complete;
1753 buf = req->buf;
1754 os_desc_cfg = cdev->os_desc_config;
1755 memset(buf, 0, w_length);
1756 buf[5] = 0x01;
1757 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1758 case USB_RECIP_DEVICE:
1759 if (w_index != 0x4 || (w_value >> 8))
1760 break;
1761 buf[6] = w_index;
1762 if (w_length == 0x10) {
1763 /* Number of ext compat interfaces */
1764 count = count_ext_compat(os_desc_cfg);
1765 buf[8] = count;
1766 count *= 24; /* 24 B/ext compat desc */
1767 count += 16; /* header */
1768 put_unaligned_le32(count, buf);
1769 value = w_length;
1770 } else {
1771 /* "extended compatibility ID"s */
1772 count = count_ext_compat(os_desc_cfg);
1773 buf[8] = count;
1774 count *= 24; /* 24 B/ext compat desc */
1775 count += 16; /* header */
1776 put_unaligned_le32(count, buf);
1777 buf += 16;
1778 fill_ext_compat(os_desc_cfg, buf);
1779 value = w_length;
1780 }
1781 break;
1782 case USB_RECIP_INTERFACE:
1783 if (w_index != 0x5 || (w_value >> 8))
1784 break;
1785 interface = w_value & 0xFF;
1786 buf[6] = w_index;
1787 if (w_length == 0x0A) {
1788 count = count_ext_prop(os_desc_cfg,
1789 interface);
1790 put_unaligned_le16(count, buf + 8);
1791 count = len_ext_prop(os_desc_cfg,
1792 interface);
1793 put_unaligned_le32(count, buf);
1794
1795 value = w_length;
1796 } else {
1797 count = count_ext_prop(os_desc_cfg,
1798 interface);
1799 put_unaligned_le16(count, buf + 8);
1800 count = len_ext_prop(os_desc_cfg,
1801 interface);
1802 put_unaligned_le32(count, buf);
1803 buf += 10;
1804 value = fill_ext_prop(os_desc_cfg,
1805 interface, buf);
1806 if (value < 0)
1807 return value;
1808
1809 value = w_length;
1810 }
1811 break;
1812 }
1813 req->length = value;
57943716 1814 req->context = cdev;
37a3a533 1815 req->zero = value < w_length;
a7c12eaf 1816 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
37a3a533
AP
1817 if (value < 0) {
1818 DBG(cdev, "ep_queue --> %d\n", value);
1819 req->status = 0;
1820 composite_setup_complete(gadget->ep0, req);
1821 }
1822 return value;
1823 }
1824
40982be5
DB
1825 VDBG(cdev,
1826 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1827 ctrl->bRequestType, ctrl->bRequest,
1828 w_value, w_index, w_length);
1829
5242658d
LP
1830 /* functions always handle their interfaces and endpoints...
1831 * punt other recipients (other, WUSB, ...) to the current
40982be5
DB
1832 * configuration code.
1833 *
1834 * REVISIT it could make sense to let the composite device
1835 * take such requests too, if that's ever needed: to work
1836 * in config 0, etc.
1837 */
b4c21f0b
KVA
1838 if (cdev->config) {
1839 list_for_each_entry(f, &cdev->config->functions, list)
1840 if (f->req_match && f->req_match(f, ctrl))
1841 goto try_fun_setup;
1842 f = NULL;
1843 }
1844
5242658d
LP
1845 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1846 case USB_RECIP_INTERFACE:
ff085de7 1847 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
3c47eb06
MM
1848 break;
1849 f = cdev->config->interface[intf];
5242658d
LP
1850 break;
1851
1852 case USB_RECIP_ENDPOINT:
1853 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1854 list_for_each_entry(f, &cdev->config->functions, list) {
1855 if (test_bit(endp, f->endpoints))
1856 break;
1857 }
1858 if (&f->list == &cdev->config->functions)
40982be5 1859 f = NULL;
5242658d 1860 break;
40982be5 1861 }
f563d230 1862try_fun_setup:
5242658d
LP
1863 if (f && f->setup)
1864 value = f->setup(f, ctrl);
1865 else {
40982be5
DB
1866 struct usb_configuration *c;
1867
1868 c = cdev->config;
a01091e5
AP
1869 if (!c)
1870 goto done;
1871
1872 /* try current config's setup */
1873 if (c->setup) {
40982be5 1874 value = c->setup(c, ctrl);
a01091e5
AP
1875 goto done;
1876 }
1877
1878 /* try the only function in the current config */
1879 if (!list_is_singular(&c->functions))
1880 goto done;
1881 f = list_first_entry(&c->functions, struct usb_function,
1882 list);
1883 if (f->setup)
1884 value = f->setup(f, ctrl);
40982be5
DB
1885 }
1886
1887 goto done;
1888 }
1889
1890 /* respond with data transfer before status phase? */
1b9ba000 1891 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
40982be5 1892 req->length = value;
57943716 1893 req->context = cdev;
40982be5 1894 req->zero = value < w_length;
a7c12eaf 1895 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
40982be5
DB
1896 if (value < 0) {
1897 DBG(cdev, "ep_queue --> %d\n", value);
1898 req->status = 0;
1899 composite_setup_complete(gadget->ep0, req);
1900 }
1b9ba000
RQ
1901 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1902 WARN(cdev,
1903 "%s: Delayed status not supported for w_length != 0",
1904 __func__);
40982be5
DB
1905 }
1906
1907done:
1908 /* device either stalls (value < 0) or reports success */
1909 return value;
1910}
1911
2d5a8899 1912void composite_disconnect(struct usb_gadget *gadget)
40982be5
DB
1913{
1914 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1915 unsigned long flags;
1916
1917 /* REVISIT: should we have config and device level
1918 * disconnect callbacks?
1919 */
1920 spin_lock_irqsave(&cdev->lock, flags);
1921 if (cdev->config)
1922 reset_config(cdev);
ffe0b335
SAS
1923 if (cdev->driver->disconnect)
1924 cdev->driver->disconnect(cdev);
40982be5
DB
1925 spin_unlock_irqrestore(&cdev->lock, flags);
1926}
1927
1928/*-------------------------------------------------------------------------*/
1929
ce26bd23
GKH
1930static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1931 char *buf)
f48cf80f
FC
1932{
1933 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1934 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1935
1936 return sprintf(buf, "%d\n", cdev->suspended);
1937}
ce26bd23 1938static DEVICE_ATTR_RO(suspended);
f48cf80f 1939
779d516c 1940static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
40982be5
DB
1941{
1942 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1943
1944 /* composite_disconnect() must already have been called
1945 * by the underlying peripheral controller driver!
1946 * so there's no i/o concurrency that could affect the
1947 * state protected by cdev->lock.
1948 */
1949 WARN_ON(cdev->config);
1950
1951 while (!list_empty(&cdev->configs)) {
1952 struct usb_configuration *c;
40982be5
DB
1953 c = list_first_entry(&cdev->configs,
1954 struct usb_configuration, list);
51cce6fc 1955 remove_config(cdev, c);
40982be5 1956 }
779d516c 1957 if (cdev->driver->unbind && unbind_driver)
ffe0b335 1958 cdev->driver->unbind(cdev);
40982be5 1959
a5923340
SAS
1960 composite_dev_cleanup(cdev);
1961
cc2683c3 1962 kfree(cdev->def_manufacturer);
40982be5
DB
1963 kfree(cdev);
1964 set_gadget_data(gadget, NULL);
40982be5
DB
1965}
1966
779d516c
SAS
1967static void composite_unbind(struct usb_gadget *gadget)
1968{
1969 __composite_unbind(gadget, true);
1970}
1971
7d16e8d3
SAS
1972static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1973 const struct usb_device_descriptor *old)
1974{
1975 __le16 idVendor;
1976 __le16 idProduct;
1977 __le16 bcdDevice;
1cf0d264 1978 u8 iSerialNumber;
03de9bf6 1979 u8 iManufacturer;
2d35ee47 1980 u8 iProduct;
7d16e8d3
SAS
1981
1982 /*
1983 * these variables may have been set in
1984 * usb_composite_overwrite_options()
1985 */
1986 idVendor = new->idVendor;
1987 idProduct = new->idProduct;
1988 bcdDevice = new->bcdDevice;
1cf0d264 1989 iSerialNumber = new->iSerialNumber;
03de9bf6 1990 iManufacturer = new->iManufacturer;
2d35ee47 1991 iProduct = new->iProduct;
7d16e8d3
SAS
1992
1993 *new = *old;
1994 if (idVendor)
1995 new->idVendor = idVendor;
1996 if (idProduct)
1997 new->idProduct = idProduct;
1998 if (bcdDevice)
1999 new->bcdDevice = bcdDevice;
ed9cbda6
SAS
2000 else
2001 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
1cf0d264
SAS
2002 if (iSerialNumber)
2003 new->iSerialNumber = iSerialNumber;
03de9bf6
SAS
2004 if (iManufacturer)
2005 new->iManufacturer = iManufacturer;
2d35ee47
SAS
2006 if (iProduct)
2007 new->iProduct = iProduct;
7d16e8d3
SAS
2008}
2009
a5923340
SAS
2010int composite_dev_prepare(struct usb_composite_driver *composite,
2011 struct usb_composite_dev *cdev)
ffe0b335 2012{
a5923340
SAS
2013 struct usb_gadget *gadget = cdev->gadget;
2014 int ret = -ENOMEM;
40982be5
DB
2015
2016 /* preallocate control response and buffer */
2017 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2018 if (!cdev->req)
a5923340
SAS
2019 return -ENOMEM;
2020
e13f17ff 2021 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
40982be5
DB
2022 if (!cdev->req->buf)
2023 goto fail;
a5923340
SAS
2024
2025 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2026 if (ret)
2027 goto fail_dev;
2028
40982be5 2029 cdev->req->complete = composite_setup_complete;
57943716 2030 cdev->req->context = cdev;
40982be5
DB
2031 gadget->ep0->driver_data = cdev;
2032
40982be5
DB
2033 cdev->driver = composite;
2034
37b5801e
PM
2035 /*
2036 * As per USB compliance update, a device that is actively drawing
2037 * more than 100mA from USB must report itself as bus-powered in
2038 * the GetStatus(DEVICE) call.
2039 */
2040 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2041 usb_gadget_set_selfpowered(gadget);
40982be5
DB
2042
2043 /* interface and string IDs start at zero via kzalloc.
2044 * we force endpoints to start unassigned; few controller
2045 * drivers will zero ep->driver_data.
2046 */
a5923340
SAS
2047 usb_ep_autoconfig_reset(gadget);
2048 return 0;
2049fail_dev:
2050 kfree(cdev->req->buf);
2051fail:
2052 usb_ep_free_request(gadget->ep0, cdev->req);
2053 cdev->req = NULL;
2054 return ret;
2055}
2056
37a3a533
AP
2057int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2058 struct usb_ep *ep0)
2059{
2060 int ret = 0;
2061
2062 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2063 if (!cdev->os_desc_req) {
2064 ret = PTR_ERR(cdev->os_desc_req);
2065 goto end;
2066 }
2067
2068 /* OS feature descriptor length <= 4kB */
2069 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
2070 if (!cdev->os_desc_req->buf) {
2071 ret = PTR_ERR(cdev->os_desc_req->buf);
2072 kfree(cdev->os_desc_req);
2073 goto end;
2074 }
57943716 2075 cdev->os_desc_req->context = cdev;
37a3a533
AP
2076 cdev->os_desc_req->complete = composite_setup_complete;
2077end:
2078 return ret;
2079}
2080
a5923340
SAS
2081void composite_dev_cleanup(struct usb_composite_dev *cdev)
2082{
27a46633
SAS
2083 struct usb_gadget_string_container *uc, *tmp;
2084
2085 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2086 list_del(&uc->list);
2087 kfree(uc);
2088 }
37a3a533 2089 if (cdev->os_desc_req) {
a7c12eaf
FB
2090 if (cdev->os_desc_pending)
2091 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2092
37a3a533
AP
2093 kfree(cdev->os_desc_req->buf);
2094 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2095 }
a5923340 2096 if (cdev->req) {
a7c12eaf
FB
2097 if (cdev->setup_pending)
2098 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2099
be0a8887 2100 kfree(cdev->req->buf);
a5923340
SAS
2101 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2102 }
88af8bbe 2103 cdev->next_string_id = 0;
a5923340
SAS
2104 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
2105}
2106
2107static int composite_bind(struct usb_gadget *gadget,
2108 struct usb_gadget_driver *gdriver)
2109{
2110 struct usb_composite_dev *cdev;
2111 struct usb_composite_driver *composite = to_cdriver(gdriver);
2112 int status = -ENOMEM;
2113
2114 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2115 if (!cdev)
2116 return status;
2117
2118 spin_lock_init(&cdev->lock);
2119 cdev->gadget = gadget;
2120 set_gadget_data(gadget, cdev);
2121 INIT_LIST_HEAD(&cdev->configs);
9bb2859f 2122 INIT_LIST_HEAD(&cdev->gstrings);
a5923340
SAS
2123
2124 status = composite_dev_prepare(composite, cdev);
2125 if (status)
2126 goto fail;
40982be5
DB
2127
2128 /* composite gadget needs to assign strings for whole device (like
2129 * serial number), register function drivers, potentially update
2130 * power state and consumption, etc
2131 */
fac3a43e 2132 status = composite->bind(cdev);
40982be5
DB
2133 if (status < 0)
2134 goto fail;
2135
37a3a533
AP
2136 if (cdev->use_os_string) {
2137 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2138 if (status)
2139 goto fail;
2140 }
2141
7d16e8d3 2142 update_unchanged_dev_desc(&cdev->desc, composite->dev);
dbb442b8 2143
ad1a8102
MN
2144 /* has userspace failed to provide a serial number? */
2145 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2146 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
40982be5
DB
2147
2148 INFO(cdev, "%s ready\n", composite->name);
2149 return 0;
2150
2151fail:
779d516c 2152 __composite_unbind(gadget, false);
40982be5
DB
2153 return status;
2154}
2155
2156/*-------------------------------------------------------------------------*/
2157
3a571870 2158void composite_suspend(struct usb_gadget *gadget)
40982be5
DB
2159{
2160 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2161 struct usb_function *f;
2162
8942939a 2163 /* REVISIT: should we have config level
40982be5
DB
2164 * suspend/resume callbacks?
2165 */
2166 DBG(cdev, "suspend\n");
2167 if (cdev->config) {
2168 list_for_each_entry(f, &cdev->config->functions, list) {
2169 if (f->suspend)
2170 f->suspend(f);
2171 }
2172 }
ffe0b335
SAS
2173 if (cdev->driver->suspend)
2174 cdev->driver->suspend(cdev);
f48cf80f
FC
2175
2176 cdev->suspended = 1;
b23f2f94
HW
2177
2178 usb_gadget_vbus_draw(gadget, 2);
40982be5
DB
2179}
2180
3a571870 2181void composite_resume(struct usb_gadget *gadget)
40982be5
DB
2182{
2183 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2184 struct usb_function *f;
56b1b909 2185 u16 maxpower;
40982be5 2186
8942939a 2187 /* REVISIT: should we have config level
40982be5
DB
2188 * suspend/resume callbacks?
2189 */
2190 DBG(cdev, "resume\n");
ffe0b335
SAS
2191 if (cdev->driver->resume)
2192 cdev->driver->resume(cdev);
40982be5
DB
2193 if (cdev->config) {
2194 list_for_each_entry(f, &cdev->config->functions, list) {
2195 if (f->resume)
2196 f->resume(f);
2197 }
b23f2f94 2198
8f900a9a 2199 maxpower = cdev->config->MaxPower;
b23f2f94
HW
2200
2201 usb_gadget_vbus_draw(gadget, maxpower ?
8f900a9a 2202 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
40982be5 2203 }
f48cf80f
FC
2204
2205 cdev->suspended = 0;
40982be5
DB
2206}
2207
2208/*-------------------------------------------------------------------------*/
2209
ffe0b335 2210static const struct usb_gadget_driver composite_driver_template = {
93952956 2211 .bind = composite_bind,
915c8bef 2212 .unbind = composite_unbind,
40982be5
DB
2213
2214 .setup = composite_setup,
d8a816fc 2215 .reset = composite_disconnect,
40982be5
DB
2216 .disconnect = composite_disconnect,
2217
2218 .suspend = composite_suspend,
2219 .resume = composite_resume,
2220
2221 .driver = {
2222 .owner = THIS_MODULE,
2223 },
2224};
2225
2226/**
07a18bd7 2227 * usb_composite_probe() - register a composite driver
40982be5 2228 * @driver: the driver to register
43febb27 2229 *
40982be5
DB
2230 * Context: single threaded during gadget setup
2231 *
2232 * This function is used to register drivers using the composite driver
2233 * framework. The return value is zero, or a negative errno value.
2234 * Those values normally come from the driver's @bind method, which does
2235 * all the work of setting up the driver to match the hardware.
2236 *
2237 * On successful return, the gadget is ready to respond to requests from
2238 * the host, unless one of its components invokes usb_gadget_disconnect()
2239 * while it was binding. That would usually be done in order to wait for
2240 * some userspace participation.
2241 */
03e42bd5 2242int usb_composite_probe(struct usb_composite_driver *driver)
40982be5 2243{
ffe0b335
SAS
2244 struct usb_gadget_driver *gadget_driver;
2245
2246 if (!driver || !driver->dev || !driver->bind)
40982be5
DB
2247 return -EINVAL;
2248
2249 if (!driver->name)
2250 driver->name = "composite";
40982be5 2251
ffe0b335
SAS
2252 driver->gadget_driver = composite_driver_template;
2253 gadget_driver = &driver->gadget_driver;
2254
2255 gadget_driver->function = (char *) driver->name;
2256 gadget_driver->driver.name = driver->name;
2257 gadget_driver->max_speed = driver->max_speed;
2258
2259 return usb_gadget_probe_driver(gadget_driver);
40982be5 2260}
721e2e91 2261EXPORT_SYMBOL_GPL(usb_composite_probe);
40982be5
DB
2262
2263/**
2264 * usb_composite_unregister() - unregister a composite driver
2265 * @driver: the driver to unregister
2266 *
2267 * This function is used to unregister drivers using the composite
2268 * driver framework.
2269 */
28824b18 2270void usb_composite_unregister(struct usb_composite_driver *driver)
40982be5 2271{
ffe0b335 2272 usb_gadget_unregister_driver(&driver->gadget_driver);
40982be5 2273}
721e2e91 2274EXPORT_SYMBOL_GPL(usb_composite_unregister);
1b9ba000
RQ
2275
2276/**
2277 * usb_composite_setup_continue() - Continue with the control transfer
2278 * @cdev: the composite device who's control transfer was kept waiting
2279 *
2280 * This function must be called by the USB function driver to continue
2281 * with the control transfer's data/status stage in case it had requested to
2282 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2283 * can request the composite framework to delay the setup request's data/status
2284 * stages by returning USB_GADGET_DELAYED_STATUS.
2285 */
2286void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2287{
2288 int value;
2289 struct usb_request *req = cdev->req;
2290 unsigned long flags;
2291
2292 DBG(cdev, "%s\n", __func__);
2293 spin_lock_irqsave(&cdev->lock, flags);
2294
2295 if (cdev->delayed_status == 0) {
2296 WARN(cdev, "%s: Unexpected call\n", __func__);
2297
2298 } else if (--cdev->delayed_status == 0) {
2299 DBG(cdev, "%s: Completing delayed status\n", __func__);
2300 req->length = 0;
57943716 2301 req->context = cdev;
a7c12eaf 2302 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
1b9ba000
RQ
2303 if (value < 0) {
2304 DBG(cdev, "ep_queue --> %d\n", value);
2305 req->status = 0;
2306 composite_setup_complete(cdev->gadget->ep0, req);
2307 }
2308 }
2309
2310 spin_unlock_irqrestore(&cdev->lock, flags);
2311}
721e2e91 2312EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
1b9ba000 2313
cc2683c3
SAS
2314static char *composite_default_mfr(struct usb_gadget *gadget)
2315{
2316 char *mfr;
2317 int len;
2318
2319 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2320 init_utsname()->release, gadget->name);
2321 len++;
2322 mfr = kmalloc(len, GFP_KERNEL);
2323 if (!mfr)
2324 return NULL;
2325 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2326 init_utsname()->release, gadget->name);
2327 return mfr;
2328}
2329
7d16e8d3
SAS
2330void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2331 struct usb_composite_overwrite *covr)
2332{
2333 struct usb_device_descriptor *desc = &cdev->desc;
1cf0d264
SAS
2334 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2335 struct usb_string *dev_str = gstr->strings;
7d16e8d3
SAS
2336
2337 if (covr->idVendor)
2338 desc->idVendor = cpu_to_le16(covr->idVendor);
2339
2340 if (covr->idProduct)
2341 desc->idProduct = cpu_to_le16(covr->idProduct);
2342
2343 if (covr->bcdDevice)
2344 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
1cf0d264
SAS
2345
2346 if (covr->serial_number) {
2347 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2348 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2349 }
03de9bf6
SAS
2350 if (covr->manufacturer) {
2351 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2352 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
cc2683c3
SAS
2353
2354 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2355 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2356 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2357 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
03de9bf6 2358 }
2d35ee47
SAS
2359
2360 if (covr->product) {
2361 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2362 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2363 }
7d16e8d3 2364}
721e2e91 2365EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
d80c304b
SAS
2366
2367MODULE_LICENSE("GPL");
2368MODULE_AUTHOR("David Brownell");