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