]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/usb/gadget/composite.c
usb: gadget: make sure each gadget is using same index for Product, Serial,…
[mirror_ubuntu-hirsute-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>
bdb64d72 22#include <asm/unaligned.h>
40982be5
DB
23
24/*
25 * The code in this file is utility code, used to build a gadget driver
26 * from one or more "function" drivers, one or more "configuration"
27 * objects, and a "usb_composite_driver" by gluing them together along
28 * with the relevant device-wide data.
29 */
30
25985edc 31/* Some systems will need runtime overrides for the product identifiers
40982be5
DB
32 * published in the device descriptor, either numbers or strings or both.
33 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
34 */
40982be5 35static char *iManufacturer;
72258493 36module_param(iManufacturer, charp, S_IRUGO);
40982be5
DB
37MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
38
39static char *iProduct;
72258493 40module_param(iProduct, charp, S_IRUGO);
40982be5
DB
41MODULE_PARM_DESC(iProduct, "USB Product string");
42
43static char *iSerialNumber;
72258493 44module_param(iSerialNumber, charp, S_IRUGO);
40982be5
DB
45MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
46
ad1a8102
MN
47static char composite_manufacturer[50];
48
40982be5 49/*-------------------------------------------------------------------------*/
48767a4e
TB
50/**
51 * next_ep_desc() - advance to the next EP descriptor
52 * @t: currect pointer within descriptor array
53 *
54 * Return: next EP descriptor or NULL
55 *
56 * Iterate over @t until either EP descriptor found or
57 * NULL (that indicates end of list) encountered
58 */
59static struct usb_descriptor_header**
60next_ep_desc(struct usb_descriptor_header **t)
61{
62 for (; *t; t++) {
63 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
64 return t;
65 }
66 return NULL;
67}
68
69/*
70 * for_each_ep_desc()- iterate over endpoint descriptors in the
71 * descriptors list
72 * @start: pointer within descriptor array.
73 * @ep_desc: endpoint descriptor to use as the loop cursor
74 */
75#define for_each_ep_desc(start, ep_desc) \
76 for (ep_desc = next_ep_desc(start); \
77 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
78
79/**
80 * config_ep_by_speed() - configures the given endpoint
81 * according to gadget speed.
82 * @g: pointer to the gadget
83 * @f: usb function
84 * @_ep: the endpoint to configure
85 *
86 * Return: error code, 0 on success
87 *
88 * This function chooses the right descriptors for a given
89 * endpoint according to gadget speed and saves it in the
90 * endpoint desc field. If the endpoint already has a descriptor
91 * assigned to it - overwrites it with currently corresponding
92 * descriptor. The endpoint maxpacket field is updated according
93 * to the chosen descriptor.
94 * Note: the supplied function should hold all the descriptors
95 * for supported speeds
96 */
97int config_ep_by_speed(struct usb_gadget *g,
98 struct usb_function *f,
99 struct usb_ep *_ep)
100{
b785ea7c 101 struct usb_composite_dev *cdev = get_gadget_data(g);
48767a4e
TB
102 struct usb_endpoint_descriptor *chosen_desc = NULL;
103 struct usb_descriptor_header **speed_desc = NULL;
104
bdb64d72
TB
105 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
106 int want_comp_desc = 0;
107
48767a4e
TB
108 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
109
110 if (!g || !f || !_ep)
111 return -EIO;
112
113 /* select desired speed */
114 switch (g->speed) {
bdb64d72
TB
115 case USB_SPEED_SUPER:
116 if (gadget_is_superspeed(g)) {
117 speed_desc = f->ss_descriptors;
118 want_comp_desc = 1;
119 break;
120 }
121 /* else: Fall trough */
48767a4e
TB
122 case USB_SPEED_HIGH:
123 if (gadget_is_dualspeed(g)) {
124 speed_desc = f->hs_descriptors;
125 break;
126 }
127 /* else: fall through */
128 default:
129 speed_desc = f->descriptors;
130 }
131 /* find descriptors */
132 for_each_ep_desc(speed_desc, d_spd) {
133 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
134 if (chosen_desc->bEndpointAddress == _ep->address)
135 goto ep_found;
136 }
137 return -EIO;
138
139ep_found:
140 /* commit results */
29cc8897 141 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
48767a4e 142 _ep->desc = chosen_desc;
bdb64d72
TB
143 _ep->comp_desc = NULL;
144 _ep->maxburst = 0;
145 _ep->mult = 0;
146 if (!want_comp_desc)
147 return 0;
48767a4e 148
bdb64d72
TB
149 /*
150 * Companion descriptor should follow EP descriptor
151 * USB 3.0 spec, #9.6.7
152 */
153 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
154 if (!comp_desc ||
155 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
156 return -EIO;
157 _ep->comp_desc = comp_desc;
158 if (g->speed == USB_SPEED_SUPER) {
159 switch (usb_endpoint_type(_ep->desc)) {
bdb64d72
TB
160 case USB_ENDPOINT_XFER_ISOC:
161 /* mult: bits 1:0 of bmAttributes */
162 _ep->mult = comp_desc->bmAttributes & 0x3;
9e878a6b
PZ
163 case USB_ENDPOINT_XFER_BULK:
164 case USB_ENDPOINT_XFER_INT:
b785ea7c 165 _ep->maxburst = comp_desc->bMaxBurst + 1;
bdb64d72
TB
166 break;
167 default:
b785ea7c
FB
168 if (comp_desc->bMaxBurst != 0)
169 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
170 _ep->maxburst = 1;
bdb64d72
TB
171 break;
172 }
173 }
48767a4e
TB
174 return 0;
175}
40982be5
DB
176
177/**
178 * usb_add_function() - add a function to a configuration
179 * @config: the configuration
180 * @function: the function being added
181 * Context: single threaded during gadget setup
182 *
183 * After initialization, each configuration must have one or more
184 * functions added to it. Adding a function involves calling its @bind()
185 * method to allocate resources such as interface and string identifiers
186 * and endpoints.
187 *
188 * This function returns the value of the function's bind(), which is
189 * zero for success else a negative errno value.
190 */
28824b18 191int usb_add_function(struct usb_configuration *config,
40982be5
DB
192 struct usb_function *function)
193{
194 int value = -EINVAL;
195
196 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
197 function->name, function,
198 config->label, config);
199
200 if (!function->set_alt || !function->disable)
201 goto done;
202
203 function->config = config;
204 list_add_tail(&function->list, &config->functions);
205
206 /* REVISIT *require* function->bind? */
207 if (function->bind) {
208 value = function->bind(config, function);
209 if (value < 0) {
210 list_del(&function->list);
211 function->config = NULL;
212 }
213 } else
214 value = 0;
215
216 /* We allow configurations that don't work at both speeds.
217 * If we run into a lowspeed Linux system, treat it the same
218 * as full speed ... it's the function drivers that will need
219 * to avoid bulk and ISO transfers.
220 */
221 if (!config->fullspeed && function->descriptors)
222 config->fullspeed = true;
223 if (!config->highspeed && function->hs_descriptors)
224 config->highspeed = true;
bdb64d72
TB
225 if (!config->superspeed && function->ss_descriptors)
226 config->superspeed = true;
40982be5
DB
227
228done:
229 if (value)
230 DBG(config->cdev, "adding '%s'/%p --> %d\n",
231 function->name, function, value);
232 return value;
233}
234
60beed95
DB
235/**
236 * usb_function_deactivate - prevent function and gadget enumeration
237 * @function: the function that isn't yet ready to respond
238 *
239 * Blocks response of the gadget driver to host enumeration by
240 * preventing the data line pullup from being activated. This is
241 * normally called during @bind() processing to change from the
242 * initial "ready to respond" state, or when a required resource
243 * becomes available.
244 *
245 * For example, drivers that serve as a passthrough to a userspace
246 * daemon can block enumeration unless that daemon (such as an OBEX,
247 * MTP, or print server) is ready to handle host requests.
248 *
249 * Not all systems support software control of their USB peripheral
250 * data pullups.
251 *
252 * Returns zero on success, else negative errno.
253 */
254int usb_function_deactivate(struct usb_function *function)
255{
256 struct usb_composite_dev *cdev = function->config->cdev;
b2bdf3a7 257 unsigned long flags;
60beed95
DB
258 int status = 0;
259
b2bdf3a7 260 spin_lock_irqsave(&cdev->lock, flags);
60beed95
DB
261
262 if (cdev->deactivations == 0)
263 status = usb_gadget_disconnect(cdev->gadget);
264 if (status == 0)
265 cdev->deactivations++;
266
b2bdf3a7 267 spin_unlock_irqrestore(&cdev->lock, flags);
60beed95
DB
268 return status;
269}
270
271/**
272 * usb_function_activate - allow function and gadget enumeration
273 * @function: function on which usb_function_activate() was called
274 *
275 * Reverses effect of usb_function_deactivate(). If no more functions
276 * are delaying their activation, the gadget driver will respond to
277 * host enumeration procedures.
278 *
279 * Returns zero on success, else negative errno.
280 */
281int usb_function_activate(struct usb_function *function)
282{
283 struct usb_composite_dev *cdev = function->config->cdev;
4fefe9f6 284 unsigned long flags;
60beed95
DB
285 int status = 0;
286
4fefe9f6 287 spin_lock_irqsave(&cdev->lock, flags);
60beed95
DB
288
289 if (WARN_ON(cdev->deactivations == 0))
290 status = -EINVAL;
291 else {
292 cdev->deactivations--;
293 if (cdev->deactivations == 0)
294 status = usb_gadget_connect(cdev->gadget);
295 }
296
4fefe9f6 297 spin_unlock_irqrestore(&cdev->lock, flags);
60beed95
DB
298 return status;
299}
300
40982be5
DB
301/**
302 * usb_interface_id() - allocate an unused interface ID
303 * @config: configuration associated with the interface
304 * @function: function handling the interface
305 * Context: single threaded during gadget setup
306 *
307 * usb_interface_id() is called from usb_function.bind() callbacks to
308 * allocate new interface IDs. The function driver will then store that
309 * ID in interface, association, CDC union, and other descriptors. It
25985edc 310 * will also handle any control requests targeted at that interface,
40982be5
DB
311 * particularly changing its altsetting via set_alt(). There may
312 * also be class-specific or vendor-specific requests to handle.
313 *
314 * All interface identifier should be allocated using this routine, to
315 * ensure that for example different functions don't wrongly assign
316 * different meanings to the same identifier. Note that since interface
25985edc 317 * identifiers are configuration-specific, functions used in more than
40982be5
DB
318 * one configuration (or more than once in a given configuration) need
319 * multiple versions of the relevant descriptors.
320 *
321 * Returns the interface ID which was allocated; or -ENODEV if no
322 * more interface IDs can be allocated.
323 */
28824b18 324int usb_interface_id(struct usb_configuration *config,
40982be5
DB
325 struct usb_function *function)
326{
327 unsigned id = config->next_interface_id;
328
329 if (id < MAX_CONFIG_INTERFACES) {
330 config->interface[id] = function;
331 config->next_interface_id = id + 1;
332 return id;
333 }
334 return -ENODEV;
335}
336
337static int config_buf(struct usb_configuration *config,
338 enum usb_device_speed speed, void *buf, u8 type)
339{
340 struct usb_config_descriptor *c = buf;
341 void *next = buf + USB_DT_CONFIG_SIZE;
e13f17ff 342 int len;
40982be5
DB
343 struct usb_function *f;
344 int status;
345
e13f17ff 346 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
40982be5
DB
347 /* write the config descriptor */
348 c = buf;
349 c->bLength = USB_DT_CONFIG_SIZE;
350 c->bDescriptorType = type;
351 /* wTotalLength is written later */
352 c->bNumInterfaces = config->next_interface_id;
353 c->bConfigurationValue = config->bConfigurationValue;
354 c->iConfiguration = config->iConfiguration;
355 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
36e893d2 356 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
40982be5
DB
357
358 /* There may be e.g. OTG descriptors */
359 if (config->descriptors) {
360 status = usb_descriptor_fillbuf(next, len,
361 config->descriptors);
362 if (status < 0)
363 return status;
364 len -= status;
365 next += status;
366 }
367
368 /* add each function's descriptors */
369 list_for_each_entry(f, &config->functions, list) {
370 struct usb_descriptor_header **descriptors;
371
bdb64d72
TB
372 switch (speed) {
373 case USB_SPEED_SUPER:
374 descriptors = f->ss_descriptors;
375 break;
376 case USB_SPEED_HIGH:
40982be5 377 descriptors = f->hs_descriptors;
bdb64d72
TB
378 break;
379 default:
40982be5 380 descriptors = f->descriptors;
bdb64d72
TB
381 }
382
40982be5
DB
383 if (!descriptors)
384 continue;
385 status = usb_descriptor_fillbuf(next, len,
386 (const struct usb_descriptor_header **) descriptors);
387 if (status < 0)
388 return status;
389 len -= status;
390 next += status;
391 }
392
393 len = next - buf;
394 c->wTotalLength = cpu_to_le16(len);
395 return len;
396}
397
398static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
399{
400 struct usb_gadget *gadget = cdev->gadget;
401 struct usb_configuration *c;
402 u8 type = w_value >> 8;
403 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
404
bdb64d72
TB
405 if (gadget->speed == USB_SPEED_SUPER)
406 speed = gadget->speed;
407 else if (gadget_is_dualspeed(gadget)) {
408 int hs = 0;
40982be5
DB
409 if (gadget->speed == USB_SPEED_HIGH)
410 hs = 1;
411 if (type == USB_DT_OTHER_SPEED_CONFIG)
412 hs = !hs;
413 if (hs)
414 speed = USB_SPEED_HIGH;
415
416 }
417
418 /* This is a lookup by config *INDEX* */
419 w_value &= 0xff;
420 list_for_each_entry(c, &cdev->configs, list) {
421 /* ignore configs that won't work at this speed */
bdb64d72
TB
422 switch (speed) {
423 case USB_SPEED_SUPER:
424 if (!c->superspeed)
425 continue;
426 break;
427 case USB_SPEED_HIGH:
40982be5
DB
428 if (!c->highspeed)
429 continue;
bdb64d72
TB
430 break;
431 default:
40982be5
DB
432 if (!c->fullspeed)
433 continue;
434 }
bdb64d72 435
40982be5
DB
436 if (w_value == 0)
437 return config_buf(c, speed, cdev->req->buf, type);
438 w_value--;
439 }
440 return -EINVAL;
441}
442
443static int count_configs(struct usb_composite_dev *cdev, unsigned type)
444{
445 struct usb_gadget *gadget = cdev->gadget;
446 struct usb_configuration *c;
447 unsigned count = 0;
448 int hs = 0;
bdb64d72 449 int ss = 0;
40982be5
DB
450
451 if (gadget_is_dualspeed(gadget)) {
452 if (gadget->speed == USB_SPEED_HIGH)
453 hs = 1;
bdb64d72
TB
454 if (gadget->speed == USB_SPEED_SUPER)
455 ss = 1;
40982be5
DB
456 if (type == USB_DT_DEVICE_QUALIFIER)
457 hs = !hs;
458 }
459 list_for_each_entry(c, &cdev->configs, list) {
460 /* ignore configs that won't work at this speed */
bdb64d72
TB
461 if (ss) {
462 if (!c->superspeed)
463 continue;
464 } else if (hs) {
40982be5
DB
465 if (!c->highspeed)
466 continue;
467 } else {
468 if (!c->fullspeed)
469 continue;
470 }
471 count++;
472 }
473 return count;
474}
475
bdb64d72
TB
476/**
477 * bos_desc() - prepares the BOS descriptor.
478 * @cdev: pointer to usb_composite device to generate the bos
479 * descriptor for
480 *
481 * This function generates the BOS (Binary Device Object)
482 * descriptor and its device capabilities descriptors. The BOS
483 * descriptor should be supported by a SuperSpeed device.
484 */
485static int bos_desc(struct usb_composite_dev *cdev)
486{
487 struct usb_ext_cap_descriptor *usb_ext;
488 struct usb_ss_cap_descriptor *ss_cap;
489 struct usb_dcd_config_params dcd_config_params;
490 struct usb_bos_descriptor *bos = cdev->req->buf;
491
492 bos->bLength = USB_DT_BOS_SIZE;
493 bos->bDescriptorType = USB_DT_BOS;
494
495 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
496 bos->bNumDeviceCaps = 0;
497
498 /*
499 * A SuperSpeed device shall include the USB2.0 extension descriptor
500 * and shall support LPM when operating in USB2.0 HS mode.
501 */
502 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
503 bos->bNumDeviceCaps++;
504 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
505 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
506 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
507 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
508 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
509
510 /*
511 * The Superspeed USB Capability descriptor shall be implemented by all
512 * SuperSpeed devices.
513 */
514 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
515 bos->bNumDeviceCaps++;
516 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
517 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
518 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
519 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
520 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
521 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
522 USB_FULL_SPEED_OPERATION |
523 USB_HIGH_SPEED_OPERATION |
524 USB_5GBPS_OPERATION);
525 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
526
527 /* Get Controller configuration */
528 if (cdev->gadget->ops->get_config_params)
529 cdev->gadget->ops->get_config_params(&dcd_config_params);
530 else {
089b837a 531 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
bdb64d72 532 dcd_config_params.bU2DevExitLat =
089b837a 533 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
bdb64d72
TB
534 }
535 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
536 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
537
538 return le16_to_cpu(bos->wTotalLength);
539}
540
40982be5
DB
541static void device_qual(struct usb_composite_dev *cdev)
542{
543 struct usb_qualifier_descriptor *qual = cdev->req->buf;
544
545 qual->bLength = sizeof(*qual);
546 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
547 /* POLICY: same bcdUSB and device type info at both speeds */
548 qual->bcdUSB = cdev->desc.bcdUSB;
549 qual->bDeviceClass = cdev->desc.bDeviceClass;
550 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
551 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
552 /* ASSUME same EP0 fifo size at both speeds */
765f5b83 553 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
40982be5 554 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
c24f4227 555 qual->bRESERVED = 0;
40982be5
DB
556}
557
558/*-------------------------------------------------------------------------*/
559
560static void reset_config(struct usb_composite_dev *cdev)
561{
562 struct usb_function *f;
563
564 DBG(cdev, "reset config\n");
565
566 list_for_each_entry(f, &cdev->config->functions, list) {
567 if (f->disable)
568 f->disable(f);
5242658d
LP
569
570 bitmap_zero(f->endpoints, 32);
40982be5
DB
571 }
572 cdev->config = NULL;
573}
574
575static int set_config(struct usb_composite_dev *cdev,
576 const struct usb_ctrlrequest *ctrl, unsigned number)
577{
578 struct usb_gadget *gadget = cdev->gadget;
579 struct usb_configuration *c = NULL;
580 int result = -EINVAL;
581 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
582 int tmp;
583
40982be5
DB
584 if (number) {
585 list_for_each_entry(c, &cdev->configs, list) {
586 if (c->bConfigurationValue == number) {
bdb64d72
TB
587 /*
588 * We disable the FDs of the previous
589 * configuration only if the new configuration
590 * is a valid one
591 */
592 if (cdev->config)
593 reset_config(cdev);
40982be5
DB
594 result = 0;
595 break;
596 }
597 }
598 if (result < 0)
599 goto done;
bdb64d72
TB
600 } else { /* Zero configuration value - need to reset the config */
601 if (cdev->config)
602 reset_config(cdev);
40982be5 603 result = 0;
bdb64d72 604 }
40982be5 605
e538dfda
MN
606 INFO(cdev, "%s config #%d: %s\n",
607 usb_speed_string(gadget->speed),
608 number, c ? c->label : "unconfigured");
40982be5
DB
609
610 if (!c)
611 goto done;
612
613 cdev->config = c;
614
615 /* Initialize all interfaces by setting them to altsetting zero. */
616 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
617 struct usb_function *f = c->interface[tmp];
5242658d 618 struct usb_descriptor_header **descriptors;
40982be5
DB
619
620 if (!f)
621 break;
622
5242658d
LP
623 /*
624 * Record which endpoints are used by the function. This is used
625 * to dispatch control requests targeted at that endpoint to the
626 * function's setup callback instead of the current
627 * configuration's setup callback.
628 */
bdb64d72
TB
629 switch (gadget->speed) {
630 case USB_SPEED_SUPER:
631 descriptors = f->ss_descriptors;
632 break;
633 case USB_SPEED_HIGH:
5242658d 634 descriptors = f->hs_descriptors;
bdb64d72
TB
635 break;
636 default:
5242658d 637 descriptors = f->descriptors;
bdb64d72 638 }
5242658d
LP
639
640 for (; *descriptors; ++descriptors) {
641 struct usb_endpoint_descriptor *ep;
642 int addr;
643
644 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
645 continue;
646
647 ep = (struct usb_endpoint_descriptor *)*descriptors;
648 addr = ((ep->bEndpointAddress & 0x80) >> 3)
649 | (ep->bEndpointAddress & 0x0f);
650 set_bit(addr, f->endpoints);
651 }
652
40982be5
DB
653 result = f->set_alt(f, tmp, 0);
654 if (result < 0) {
655 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
656 tmp, f->name, f, result);
657
658 reset_config(cdev);
659 goto done;
660 }
1b9ba000
RQ
661
662 if (result == USB_GADGET_DELAYED_STATUS) {
663 DBG(cdev,
664 "%s: interface %d (%s) requested delayed status\n",
665 __func__, tmp, f->name);
666 cdev->delayed_status++;
667 DBG(cdev, "delayed_status count %d\n",
668 cdev->delayed_status);
669 }
40982be5
DB
670 }
671
672 /* when we return, be sure our power usage is valid */
36e893d2 673 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
40982be5
DB
674done:
675 usb_gadget_vbus_draw(gadget, power);
1b9ba000
RQ
676 if (result >= 0 && cdev->delayed_status)
677 result = USB_GADGET_DELAYED_STATUS;
40982be5
DB
678 return result;
679}
680
681/**
682 * usb_add_config() - add a configuration to a device.
683 * @cdev: wraps the USB gadget
684 * @config: the configuration, with bConfigurationValue assigned
c9bfff9c 685 * @bind: the configuration's bind function
40982be5
DB
686 * Context: single threaded during gadget setup
687 *
c9bfff9c 688 * One of the main tasks of a composite @bind() routine is to
40982be5
DB
689 * add each of the configurations it supports, using this routine.
690 *
c9bfff9c 691 * This function returns the value of the configuration's @bind(), which
40982be5
DB
692 * is zero for success else a negative errno value. Binding configurations
693 * assigns global resources including string IDs, and per-configuration
694 * resources such as interface IDs and endpoints.
695 */
28824b18 696int usb_add_config(struct usb_composite_dev *cdev,
c9bfff9c
UKK
697 struct usb_configuration *config,
698 int (*bind)(struct usb_configuration *))
40982be5
DB
699{
700 int status = -EINVAL;
701 struct usb_configuration *c;
702
703 DBG(cdev, "adding config #%u '%s'/%p\n",
704 config->bConfigurationValue,
705 config->label, config);
706
c9bfff9c 707 if (!config->bConfigurationValue || !bind)
40982be5
DB
708 goto done;
709
710 /* Prevent duplicate configuration identifiers */
711 list_for_each_entry(c, &cdev->configs, list) {
712 if (c->bConfigurationValue == config->bConfigurationValue) {
713 status = -EBUSY;
714 goto done;
715 }
716 }
717
718 config->cdev = cdev;
719 list_add_tail(&config->list, &cdev->configs);
720
721 INIT_LIST_HEAD(&config->functions);
722 config->next_interface_id = 0;
02e8161e 723 memset(config->interface, 0, sizeof(config->interface));
40982be5 724
c9bfff9c 725 status = bind(config);
40982be5 726 if (status < 0) {
124ef389
YO
727 while (!list_empty(&config->functions)) {
728 struct usb_function *f;
729
730 f = list_first_entry(&config->functions,
731 struct usb_function, list);
732 list_del(&f->list);
733 if (f->unbind) {
734 DBG(cdev, "unbind function '%s'/%p\n",
735 f->name, f);
736 f->unbind(config, f);
737 /* may free memory for "f" */
738 }
739 }
40982be5
DB
740 list_del(&config->list);
741 config->cdev = NULL;
742 } else {
743 unsigned i;
744
bdb64d72 745 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
40982be5 746 config->bConfigurationValue, config,
bdb64d72 747 config->superspeed ? " super" : "",
40982be5
DB
748 config->highspeed ? " high" : "",
749 config->fullspeed
750 ? (gadget_is_dualspeed(cdev->gadget)
751 ? " full"
752 : " full/low")
753 : "");
754
755 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
756 struct usb_function *f = config->interface[i];
757
758 if (!f)
759 continue;
760 DBG(cdev, " interface %d = %s/%p\n",
761 i, f->name, f);
762 }
763 }
764
c9bfff9c 765 /* set_alt(), or next bind(), sets up
40982be5
DB
766 * ep->driver_data as needed.
767 */
768 usb_ep_autoconfig_reset(cdev->gadget);
769
770done:
771 if (status)
772 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
773 config->bConfigurationValue, status);
774 return status;
775}
776
51cce6fc
BG
777static void remove_config(struct usb_composite_dev *cdev,
778 struct usb_configuration *config)
779{
780 while (!list_empty(&config->functions)) {
781 struct usb_function *f;
782
783 f = list_first_entry(&config->functions,
784 struct usb_function, list);
785 list_del(&f->list);
786 if (f->unbind) {
787 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
788 f->unbind(config, f);
789 /* may free memory for "f" */
790 }
791 }
792 list_del(&config->list);
793 if (config->unbind) {
794 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
795 config->unbind(config);
796 /* may free memory for "c" */
797 }
798}
799
800/**
801 * usb_remove_config() - remove a configuration from a device.
802 * @cdev: wraps the USB gadget
803 * @config: the configuration
804 *
805 * Drivers must call usb_gadget_disconnect before calling this function
806 * to disconnect the device from the host and make sure the host will not
807 * try to enumerate the device while we are changing the config list.
808 */
809void usb_remove_config(struct usb_composite_dev *cdev,
810 struct usb_configuration *config)
811{
812 unsigned long flags;
813
814 spin_lock_irqsave(&cdev->lock, flags);
815
816 if (cdev->config == config)
817 reset_config(cdev);
818
819 spin_unlock_irqrestore(&cdev->lock, flags);
820
821 remove_config(cdev, config);
822}
823
40982be5
DB
824/*-------------------------------------------------------------------------*/
825
826/* We support strings in multiple languages ... string descriptor zero
827 * says which languages are supported. The typical case will be that
828 * only one language (probably English) is used, with I18N handled on
829 * the host side.
830 */
831
832static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
833{
834 const struct usb_gadget_strings *s;
20c5e74c 835 __le16 language;
40982be5
DB
836 __le16 *tmp;
837
838 while (*sp) {
839 s = *sp;
840 language = cpu_to_le16(s->language);
841 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
842 if (*tmp == language)
843 goto repeat;
844 }
845 *tmp++ = language;
846repeat:
847 sp++;
848 }
849}
850
851static int lookup_string(
852 struct usb_gadget_strings **sp,
853 void *buf,
854 u16 language,
855 int id
856)
857{
858 struct usb_gadget_strings *s;
859 int value;
860
861 while (*sp) {
862 s = *sp++;
863 if (s->language != language)
864 continue;
865 value = usb_gadget_get_string(s, id, buf);
866 if (value > 0)
867 return value;
868 }
869 return -EINVAL;
870}
871
872static int get_string(struct usb_composite_dev *cdev,
873 void *buf, u16 language, int id)
874{
ffe0b335 875 struct usb_composite_driver *composite = cdev->driver;
40982be5
DB
876 struct usb_configuration *c;
877 struct usb_function *f;
878 int len;
ad1a8102 879 const char *str;
40982be5
DB
880
881 /* Yes, not only is USB's I18N support probably more than most
882 * folk will ever care about ... also, it's all supported here.
883 * (Except for UTF8 support for Unicode's "Astral Planes".)
884 */
885
886 /* 0 == report all available language codes */
887 if (id == 0) {
888 struct usb_string_descriptor *s = buf;
889 struct usb_gadget_strings **sp;
890
891 memset(s, 0, 256);
892 s->bDescriptorType = USB_DT_STRING;
893
894 sp = composite->strings;
895 if (sp)
896 collect_langs(sp, s->wData);
897
898 list_for_each_entry(c, &cdev->configs, list) {
899 sp = c->strings;
900 if (sp)
901 collect_langs(sp, s->wData);
902
903 list_for_each_entry(f, &c->functions, list) {
904 sp = f->strings;
905 if (sp)
906 collect_langs(sp, s->wData);
907 }
908 }
909
417b57b3 910 for (len = 0; len <= 126 && s->wData[len]; len++)
40982be5
DB
911 continue;
912 if (!len)
913 return -EINVAL;
914
915 s->bLength = 2 * (len + 1);
916 return s->bLength;
917 }
918
ad1a8102
MN
919 /* Otherwise, look up and return a specified string. First
920 * check if the string has not been overridden.
921 */
922 if (cdev->manufacturer_override == id)
923 str = iManufacturer ?: composite->iManufacturer ?:
924 composite_manufacturer;
925 else if (cdev->product_override == id)
926 str = iProduct ?: composite->iProduct;
927 else if (cdev->serial_override == id)
cad4cd8f 928 str = iSerialNumber ?: composite->iSerialNumber;
ad1a8102
MN
929 else
930 str = NULL;
931 if (str) {
932 struct usb_gadget_strings strings = {
933 .language = language,
934 .strings = &(struct usb_string) { 0xff, str }
935 };
936 return usb_gadget_get_string(&strings, 0xff, buf);
937 }
938
939 /* String IDs are device-scoped, so we look up each string
940 * table we're told about. These lookups are infrequent;
941 * simpler-is-better here.
40982be5
DB
942 */
943 if (composite->strings) {
944 len = lookup_string(composite->strings, buf, language, id);
945 if (len > 0)
946 return len;
947 }
948 list_for_each_entry(c, &cdev->configs, list) {
949 if (c->strings) {
950 len = lookup_string(c->strings, buf, language, id);
951 if (len > 0)
952 return len;
953 }
954 list_for_each_entry(f, &c->functions, list) {
955 if (!f->strings)
956 continue;
957 len = lookup_string(f->strings, buf, language, id);
958 if (len > 0)
959 return len;
960 }
961 }
962 return -EINVAL;
963}
964
965/**
966 * usb_string_id() - allocate an unused string ID
967 * @cdev: the device whose string descriptor IDs are being allocated
968 * Context: single threaded during gadget setup
969 *
970 * @usb_string_id() is called from bind() callbacks to allocate
971 * string IDs. Drivers for functions, configurations, or gadgets will
972 * then store that ID in the appropriate descriptors and string table.
973 *
f2adc4f8
MN
974 * All string identifier should be allocated using this,
975 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
976 * that for example different functions don't wrongly assign different
977 * meanings to the same identifier.
40982be5 978 */
28824b18 979int usb_string_id(struct usb_composite_dev *cdev)
40982be5
DB
980{
981 if (cdev->next_string_id < 254) {
f2adc4f8
MN
982 /* string id 0 is reserved by USB spec for list of
983 * supported languages */
984 /* 255 reserved as well? -- mina86 */
40982be5
DB
985 cdev->next_string_id++;
986 return cdev->next_string_id;
987 }
988 return -ENODEV;
989}
990
f2adc4f8
MN
991/**
992 * usb_string_ids() - allocate unused string IDs in batch
993 * @cdev: the device whose string descriptor IDs are being allocated
994 * @str: an array of usb_string objects to assign numbers to
995 * Context: single threaded during gadget setup
996 *
997 * @usb_string_ids() is called from bind() callbacks to allocate
998 * string IDs. Drivers for functions, configurations, or gadgets will
999 * then copy IDs from the string table to the appropriate descriptors
1000 * and string table for other languages.
1001 *
1002 * All string identifier should be allocated using this,
1003 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1004 * example different functions don't wrongly assign different meanings
1005 * to the same identifier.
1006 */
1007int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1008{
1009 int next = cdev->next_string_id;
1010
1011 for (; str->s; ++str) {
1012 if (unlikely(next >= 254))
1013 return -ENODEV;
1014 str->id = ++next;
1015 }
1016
1017 cdev->next_string_id = next;
1018
1019 return 0;
1020}
1021
1022/**
1023 * usb_string_ids_n() - allocate unused string IDs in batch
d187abb9 1024 * @c: the device whose string descriptor IDs are being allocated
f2adc4f8
MN
1025 * @n: number of string IDs to allocate
1026 * Context: single threaded during gadget setup
1027 *
1028 * Returns the first requested ID. This ID and next @n-1 IDs are now
d187abb9 1029 * valid IDs. At least provided that @n is non-zero because if it
f2adc4f8
MN
1030 * is, returns last requested ID which is now very useful information.
1031 *
1032 * @usb_string_ids_n() is called from bind() callbacks to allocate
1033 * string IDs. Drivers for functions, configurations, or gadgets will
1034 * then store that ID in the appropriate descriptors and string table.
1035 *
1036 * All string identifier should be allocated using this,
1037 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1038 * example different functions don't wrongly assign different meanings
1039 * to the same identifier.
1040 */
1041int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1042{
1043 unsigned next = c->next_string_id;
1044 if (unlikely(n > 254 || (unsigned)next + n > 254))
1045 return -ENODEV;
1046 c->next_string_id += n;
1047 return next + 1;
1048}
1049
1050
40982be5
DB
1051/*-------------------------------------------------------------------------*/
1052
1053static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1054{
1055 if (req->status || req->actual != req->length)
1056 DBG((struct usb_composite_dev *) ep->driver_data,
1057 "setup complete --> %d, %d/%d\n",
1058 req->status, req->actual, req->length);
1059}
1060
1061/*
1062 * The setup() callback implements all the ep0 functionality that's
1063 * not handled lower down, in hardware or the hardware driver(like
1064 * device and endpoint feature flags, and their status). It's all
1065 * housekeeping for the gadget function we're implementing. Most of
1066 * the work is in config and function specific setup.
1067 */
1068static int
1069composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1070{
1071 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1072 struct usb_request *req = cdev->req;
1073 int value = -EOPNOTSUPP;
bdb64d72 1074 int status = 0;
40982be5 1075 u16 w_index = le16_to_cpu(ctrl->wIndex);
08889517 1076 u8 intf = w_index & 0xFF;
40982be5
DB
1077 u16 w_value = le16_to_cpu(ctrl->wValue);
1078 u16 w_length = le16_to_cpu(ctrl->wLength);
1079 struct usb_function *f = NULL;
5242658d 1080 u8 endp;
40982be5
DB
1081
1082 /* partial re-init of the response message; the function or the
1083 * gadget might need to intercept e.g. a control-OUT completion
1084 * when we delegate to it.
1085 */
1086 req->zero = 0;
1087 req->complete = composite_setup_complete;
2edb11cb 1088 req->length = 0;
40982be5
DB
1089 gadget->ep0->driver_data = cdev;
1090
1091 switch (ctrl->bRequest) {
1092
1093 /* we handle all standard USB descriptors */
1094 case USB_REQ_GET_DESCRIPTOR:
1095 if (ctrl->bRequestType != USB_DIR_IN)
1096 goto unknown;
1097 switch (w_value >> 8) {
1098
1099 case USB_DT_DEVICE:
1100 cdev->desc.bNumConfigurations =
1101 count_configs(cdev, USB_DT_DEVICE);
bdb64d72
TB
1102 cdev->desc.bMaxPacketSize0 =
1103 cdev->gadget->ep0->maxpacket;
1104 if (gadget_is_superspeed(gadget)) {
a8f21156 1105 if (gadget->speed >= USB_SPEED_SUPER) {
bdb64d72 1106 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
a8f21156
SAS
1107 cdev->desc.bMaxPacketSize0 = 9;
1108 } else {
bdb64d72 1109 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
a8f21156 1110 }
bdb64d72
TB
1111 }
1112
40982be5
DB
1113 value = min(w_length, (u16) sizeof cdev->desc);
1114 memcpy(req->buf, &cdev->desc, value);
1115 break;
1116 case USB_DT_DEVICE_QUALIFIER:
bdb64d72
TB
1117 if (!gadget_is_dualspeed(gadget) ||
1118 gadget->speed >= USB_SPEED_SUPER)
40982be5
DB
1119 break;
1120 device_qual(cdev);
1121 value = min_t(int, w_length,
1122 sizeof(struct usb_qualifier_descriptor));
1123 break;
1124 case USB_DT_OTHER_SPEED_CONFIG:
bdb64d72
TB
1125 if (!gadget_is_dualspeed(gadget) ||
1126 gadget->speed >= USB_SPEED_SUPER)
40982be5
DB
1127 break;
1128 /* FALLTHROUGH */
1129 case USB_DT_CONFIG:
1130 value = config_desc(cdev, w_value);
1131 if (value >= 0)
1132 value = min(w_length, (u16) value);
1133 break;
1134 case USB_DT_STRING:
1135 value = get_string(cdev, req->buf,
1136 w_index, w_value & 0xff);
1137 if (value >= 0)
1138 value = min(w_length, (u16) value);
1139 break;
bdb64d72
TB
1140 case USB_DT_BOS:
1141 if (gadget_is_superspeed(gadget)) {
1142 value = bos_desc(cdev);
1143 value = min(w_length, (u16) value);
1144 }
1145 break;
40982be5
DB
1146 }
1147 break;
1148
1149 /* any number of configs can work */
1150 case USB_REQ_SET_CONFIGURATION:
1151 if (ctrl->bRequestType != 0)
1152 goto unknown;
1153 if (gadget_is_otg(gadget)) {
1154 if (gadget->a_hnp_support)
1155 DBG(cdev, "HNP available\n");
1156 else if (gadget->a_alt_hnp_support)
1157 DBG(cdev, "HNP on another port\n");
1158 else
1159 VDBG(cdev, "HNP inactive\n");
1160 }
1161 spin_lock(&cdev->lock);
1162 value = set_config(cdev, ctrl, w_value);
1163 spin_unlock(&cdev->lock);
1164 break;
1165 case USB_REQ_GET_CONFIGURATION:
1166 if (ctrl->bRequestType != USB_DIR_IN)
1167 goto unknown;
1168 if (cdev->config)
1169 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1170 else
1171 *(u8 *)req->buf = 0;
1172 value = min(w_length, (u16) 1);
1173 break;
1174
1175 /* function drivers must handle get/set altsetting; if there's
1176 * no get() method, we know only altsetting zero works.
1177 */
1178 case USB_REQ_SET_INTERFACE:
1179 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1180 goto unknown;
ff085de7 1181 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
40982be5 1182 break;
08889517 1183 f = cdev->config->interface[intf];
40982be5
DB
1184 if (!f)
1185 break;
dd4dff8b 1186 if (w_value && !f->set_alt)
40982be5
DB
1187 break;
1188 value = f->set_alt(f, w_index, w_value);
1b9ba000
RQ
1189 if (value == USB_GADGET_DELAYED_STATUS) {
1190 DBG(cdev,
1191 "%s: interface %d (%s) requested delayed status\n",
1192 __func__, intf, f->name);
1193 cdev->delayed_status++;
1194 DBG(cdev, "delayed_status count %d\n",
1195 cdev->delayed_status);
1196 }
40982be5
DB
1197 break;
1198 case USB_REQ_GET_INTERFACE:
1199 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1200 goto unknown;
ff085de7 1201 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
40982be5 1202 break;
08889517 1203 f = cdev->config->interface[intf];
40982be5
DB
1204 if (!f)
1205 break;
1206 /* lots of interfaces only need altsetting zero... */
1207 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1208 if (value < 0)
1209 break;
1210 *((u8 *)req->buf) = value;
1211 value = min(w_length, (u16) 1);
1212 break;
bdb64d72
TB
1213
1214 /*
1215 * USB 3.0 additions:
1216 * Function driver should handle get_status request. If such cb
1217 * wasn't supplied we respond with default value = 0
1218 * Note: function driver should supply such cb only for the first
1219 * interface of the function
1220 */
1221 case USB_REQ_GET_STATUS:
1222 if (!gadget_is_superspeed(gadget))
1223 goto unknown;
1224 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1225 goto unknown;
1226 value = 2; /* This is the length of the get_status reply */
1227 put_unaligned_le16(0, req->buf);
1228 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1229 break;
1230 f = cdev->config->interface[intf];
1231 if (!f)
1232 break;
1233 status = f->get_status ? f->get_status(f) : 0;
1234 if (status < 0)
1235 break;
1236 put_unaligned_le16(status & 0x0000ffff, req->buf);
1237 break;
1238 /*
1239 * Function drivers should handle SetFeature/ClearFeature
1240 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1241 * only for the first interface of the function
1242 */
1243 case USB_REQ_CLEAR_FEATURE:
1244 case USB_REQ_SET_FEATURE:
1245 if (!gadget_is_superspeed(gadget))
1246 goto unknown;
1247 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1248 goto unknown;
1249 switch (w_value) {
1250 case USB_INTRF_FUNC_SUSPEND:
1251 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1252 break;
1253 f = cdev->config->interface[intf];
1254 if (!f)
1255 break;
1256 value = 0;
1257 if (f->func_suspend)
1258 value = f->func_suspend(f, w_index >> 8);
1259 if (value < 0) {
1260 ERROR(cdev,
1261 "func_suspend() returned error %d\n",
1262 value);
1263 value = 0;
1264 }
1265 break;
1266 }
1267 break;
40982be5
DB
1268 default:
1269unknown:
1270 VDBG(cdev,
1271 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1272 ctrl->bRequestType, ctrl->bRequest,
1273 w_value, w_index, w_length);
1274
5242658d
LP
1275 /* functions always handle their interfaces and endpoints...
1276 * punt other recipients (other, WUSB, ...) to the current
40982be5
DB
1277 * configuration code.
1278 *
1279 * REVISIT it could make sense to let the composite device
1280 * take such requests too, if that's ever needed: to work
1281 * in config 0, etc.
1282 */
5242658d
LP
1283 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1284 case USB_RECIP_INTERFACE:
ff085de7 1285 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
3c47eb06
MM
1286 break;
1287 f = cdev->config->interface[intf];
5242658d
LP
1288 break;
1289
1290 case USB_RECIP_ENDPOINT:
1291 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1292 list_for_each_entry(f, &cdev->config->functions, list) {
1293 if (test_bit(endp, f->endpoints))
1294 break;
1295 }
1296 if (&f->list == &cdev->config->functions)
40982be5 1297 f = NULL;
5242658d 1298 break;
40982be5 1299 }
5242658d
LP
1300
1301 if (f && f->setup)
1302 value = f->setup(f, ctrl);
1303 else {
40982be5
DB
1304 struct usb_configuration *c;
1305
1306 c = cdev->config;
1307 if (c && c->setup)
1308 value = c->setup(c, ctrl);
1309 }
1310
1311 goto done;
1312 }
1313
1314 /* respond with data transfer before status phase? */
1b9ba000 1315 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
40982be5
DB
1316 req->length = value;
1317 req->zero = value < w_length;
1318 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1319 if (value < 0) {
1320 DBG(cdev, "ep_queue --> %d\n", value);
1321 req->status = 0;
1322 composite_setup_complete(gadget->ep0, req);
1323 }
1b9ba000
RQ
1324 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1325 WARN(cdev,
1326 "%s: Delayed status not supported for w_length != 0",
1327 __func__);
40982be5
DB
1328 }
1329
1330done:
1331 /* device either stalls (value < 0) or reports success */
1332 return value;
1333}
1334
1335static void composite_disconnect(struct usb_gadget *gadget)
1336{
1337 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1338 unsigned long flags;
1339
1340 /* REVISIT: should we have config and device level
1341 * disconnect callbacks?
1342 */
1343 spin_lock_irqsave(&cdev->lock, flags);
1344 if (cdev->config)
1345 reset_config(cdev);
ffe0b335
SAS
1346 if (cdev->driver->disconnect)
1347 cdev->driver->disconnect(cdev);
40982be5
DB
1348 spin_unlock_irqrestore(&cdev->lock, flags);
1349}
1350
1351/*-------------------------------------------------------------------------*/
1352
f48cf80f
FC
1353static ssize_t composite_show_suspended(struct device *dev,
1354 struct device_attribute *attr,
1355 char *buf)
1356{
1357 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1358 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1359
1360 return sprintf(buf, "%d\n", cdev->suspended);
1361}
1362
1363static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1364
28824b18 1365static void
40982be5
DB
1366composite_unbind(struct usb_gadget *gadget)
1367{
1368 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1369
1370 /* composite_disconnect() must already have been called
1371 * by the underlying peripheral controller driver!
1372 * so there's no i/o concurrency that could affect the
1373 * state protected by cdev->lock.
1374 */
1375 WARN_ON(cdev->config);
1376
1377 while (!list_empty(&cdev->configs)) {
1378 struct usb_configuration *c;
40982be5
DB
1379 c = list_first_entry(&cdev->configs,
1380 struct usb_configuration, list);
51cce6fc 1381 remove_config(cdev, c);
40982be5 1382 }
ffe0b335
SAS
1383 if (cdev->driver->unbind)
1384 cdev->driver->unbind(cdev);
40982be5
DB
1385
1386 if (cdev->req) {
1387 kfree(cdev->req->buf);
1388 usb_ep_free_request(gadget->ep0, cdev->req);
1389 }
daba5803 1390 device_remove_file(&gadget->dev, &dev_attr_suspended);
40982be5
DB
1391 kfree(cdev);
1392 set_gadget_data(gadget, NULL);
40982be5
DB
1393}
1394
ad1a8102 1395static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
40982be5 1396{
ad1a8102
MN
1397 if (!*desc) {
1398 int ret = usb_string_id(cdev);
1399 if (unlikely(ret < 0))
1400 WARNING(cdev, "failed to override string ID\n");
1401 else
1402 *desc = ret;
40982be5 1403 }
40982be5 1404
ad1a8102 1405 return *desc;
40982be5
DB
1406}
1407
7d16e8d3
SAS
1408static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1409 const struct usb_device_descriptor *old)
1410{
1411 __le16 idVendor;
1412 __le16 idProduct;
1413 __le16 bcdDevice;
1414
1415 /*
1416 * these variables may have been set in
1417 * usb_composite_overwrite_options()
1418 */
1419 idVendor = new->idVendor;
1420 idProduct = new->idProduct;
1421 bcdDevice = new->bcdDevice;
1422
1423 *new = *old;
1424 if (idVendor)
1425 new->idVendor = idVendor;
1426 if (idProduct)
1427 new->idProduct = idProduct;
1428 if (bcdDevice)
1429 new->bcdDevice = bcdDevice;
1430}
1431
ffe0b335
SAS
1432static struct usb_composite_driver *to_cdriver(struct usb_gadget_driver *gdrv)
1433{
1434 return container_of(gdrv, struct usb_composite_driver, gadget_driver);
1435}
1436
1437static int composite_bind(struct usb_gadget *gadget,
1438 struct usb_gadget_driver *gdriver)
40982be5
DB
1439{
1440 struct usb_composite_dev *cdev;
ffe0b335 1441 struct usb_composite_driver *composite = to_cdriver(gdriver);
40982be5
DB
1442 int status = -ENOMEM;
1443
1444 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1445 if (!cdev)
1446 return status;
1447
1448 spin_lock_init(&cdev->lock);
1449 cdev->gadget = gadget;
1450 set_gadget_data(gadget, cdev);
1451 INIT_LIST_HEAD(&cdev->configs);
1452
1453 /* preallocate control response and buffer */
1454 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1455 if (!cdev->req)
1456 goto fail;
e13f17ff 1457 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
40982be5
DB
1458 if (!cdev->req->buf)
1459 goto fail;
1460 cdev->req->complete = composite_setup_complete;
1461 gadget->ep0->driver_data = cdev;
1462
40982be5
DB
1463 cdev->driver = composite;
1464
37b5801e
PM
1465 /*
1466 * As per USB compliance update, a device that is actively drawing
1467 * more than 100mA from USB must report itself as bus-powered in
1468 * the GetStatus(DEVICE) call.
1469 */
1470 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1471 usb_gadget_set_selfpowered(gadget);
40982be5
DB
1472
1473 /* interface and string IDs start at zero via kzalloc.
1474 * we force endpoints to start unassigned; few controller
1475 * drivers will zero ep->driver_data.
1476 */
1477 usb_ep_autoconfig_reset(cdev->gadget);
1478
1479 /* composite gadget needs to assign strings for whole device (like
1480 * serial number), register function drivers, potentially update
1481 * power state and consumption, etc
1482 */
fac3a43e 1483 status = composite->bind(cdev);
40982be5
DB
1484 if (status < 0)
1485 goto fail;
1486
7d16e8d3 1487 update_unchanged_dev_desc(&cdev->desc, composite->dev);
dbb442b8 1488
78bff3c6 1489 /* string overrides */
ad1a8102
MN
1490 if (iManufacturer || !cdev->desc.iManufacturer) {
1491 if (!iManufacturer && !composite->iManufacturer &&
1492 !*composite_manufacturer)
1493 snprintf(composite_manufacturer,
1494 sizeof composite_manufacturer,
1495 "%s %s with %s",
1496 init_utsname()->sysname,
1497 init_utsname()->release,
1498 gadget->name);
1499
1500 cdev->manufacturer_override =
1501 override_id(cdev, &cdev->desc.iManufacturer);
1502 }
1503
1504 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1505 cdev->product_override =
1506 override_id(cdev, &cdev->desc.iProduct);
1507
cad4cd8f
AP
1508 if (iSerialNumber ||
1509 (!cdev->desc.iSerialNumber && composite->iSerialNumber))
ad1a8102
MN
1510 cdev->serial_override =
1511 override_id(cdev, &cdev->desc.iSerialNumber);
1512
1513 /* has userspace failed to provide a serial number? */
1514 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1515 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
40982be5 1516
ad1a8102 1517 /* finish up */
f48cf80f
FC
1518 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1519 if (status)
1520 goto fail;
1521
40982be5
DB
1522 INFO(cdev, "%s ready\n", composite->name);
1523 return 0;
1524
1525fail:
1526 composite_unbind(gadget);
1527 return status;
1528}
1529
1530/*-------------------------------------------------------------------------*/
1531
1532static void
1533composite_suspend(struct usb_gadget *gadget)
1534{
1535 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1536 struct usb_function *f;
1537
8942939a 1538 /* REVISIT: should we have config level
40982be5
DB
1539 * suspend/resume callbacks?
1540 */
1541 DBG(cdev, "suspend\n");
1542 if (cdev->config) {
1543 list_for_each_entry(f, &cdev->config->functions, list) {
1544 if (f->suspend)
1545 f->suspend(f);
1546 }
1547 }
ffe0b335
SAS
1548 if (cdev->driver->suspend)
1549 cdev->driver->suspend(cdev);
f48cf80f
FC
1550
1551 cdev->suspended = 1;
b23f2f94
HW
1552
1553 usb_gadget_vbus_draw(gadget, 2);
40982be5
DB
1554}
1555
1556static void
1557composite_resume(struct usb_gadget *gadget)
1558{
1559 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1560 struct usb_function *f;
b23f2f94 1561 u8 maxpower;
40982be5 1562
8942939a 1563 /* REVISIT: should we have config level
40982be5
DB
1564 * suspend/resume callbacks?
1565 */
1566 DBG(cdev, "resume\n");
ffe0b335
SAS
1567 if (cdev->driver->resume)
1568 cdev->driver->resume(cdev);
40982be5
DB
1569 if (cdev->config) {
1570 list_for_each_entry(f, &cdev->config->functions, list) {
1571 if (f->resume)
1572 f->resume(f);
1573 }
b23f2f94
HW
1574
1575 maxpower = cdev->config->bMaxPower;
1576
1577 usb_gadget_vbus_draw(gadget, maxpower ?
1578 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
40982be5 1579 }
f48cf80f
FC
1580
1581 cdev->suspended = 0;
40982be5
DB
1582}
1583
1584/*-------------------------------------------------------------------------*/
1585
ffe0b335 1586static const struct usb_gadget_driver composite_driver_template = {
93952956 1587 .bind = composite_bind,
915c8bef 1588 .unbind = composite_unbind,
40982be5
DB
1589
1590 .setup = composite_setup,
1591 .disconnect = composite_disconnect,
1592
1593 .suspend = composite_suspend,
1594 .resume = composite_resume,
1595
1596 .driver = {
1597 .owner = THIS_MODULE,
1598 },
1599};
1600
1601/**
07a18bd7 1602 * usb_composite_probe() - register a composite driver
40982be5 1603 * @driver: the driver to register
07a18bd7
MN
1604 * @bind: the callback used to allocate resources that are shared across the
1605 * whole device, such as string IDs, and add its configurations using
1606 * @usb_add_config(). This may fail by returning a negative errno
1607 * value; it should return zero on successful initialization.
40982be5
DB
1608 * Context: single threaded during gadget setup
1609 *
1610 * This function is used to register drivers using the composite driver
1611 * framework. The return value is zero, or a negative errno value.
1612 * Those values normally come from the driver's @bind method, which does
1613 * all the work of setting up the driver to match the hardware.
1614 *
1615 * On successful return, the gadget is ready to respond to requests from
1616 * the host, unless one of its components invokes usb_gadget_disconnect()
1617 * while it was binding. That would usually be done in order to wait for
1618 * some userspace participation.
1619 */
03e42bd5 1620int usb_composite_probe(struct usb_composite_driver *driver)
40982be5 1621{
ffe0b335
SAS
1622 struct usb_gadget_driver *gadget_driver;
1623
1624 if (!driver || !driver->dev || !driver->bind)
40982be5
DB
1625 return -EINVAL;
1626
1627 if (!driver->name)
1628 driver->name = "composite";
05c3eebd
JB
1629 if (!driver->iProduct)
1630 driver->iProduct = driver->name;
40982be5 1631
ffe0b335
SAS
1632 driver->gadget_driver = composite_driver_template;
1633 gadget_driver = &driver->gadget_driver;
1634
1635 gadget_driver->function = (char *) driver->name;
1636 gadget_driver->driver.name = driver->name;
1637 gadget_driver->max_speed = driver->max_speed;
1638
1639 return usb_gadget_probe_driver(gadget_driver);
40982be5
DB
1640}
1641
1642/**
1643 * usb_composite_unregister() - unregister a composite driver
1644 * @driver: the driver to unregister
1645 *
1646 * This function is used to unregister drivers using the composite
1647 * driver framework.
1648 */
28824b18 1649void usb_composite_unregister(struct usb_composite_driver *driver)
40982be5 1650{
ffe0b335 1651 usb_gadget_unregister_driver(&driver->gadget_driver);
40982be5 1652}
1b9ba000
RQ
1653
1654/**
1655 * usb_composite_setup_continue() - Continue with the control transfer
1656 * @cdev: the composite device who's control transfer was kept waiting
1657 *
1658 * This function must be called by the USB function driver to continue
1659 * with the control transfer's data/status stage in case it had requested to
1660 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1661 * can request the composite framework to delay the setup request's data/status
1662 * stages by returning USB_GADGET_DELAYED_STATUS.
1663 */
1664void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1665{
1666 int value;
1667 struct usb_request *req = cdev->req;
1668 unsigned long flags;
1669
1670 DBG(cdev, "%s\n", __func__);
1671 spin_lock_irqsave(&cdev->lock, flags);
1672
1673 if (cdev->delayed_status == 0) {
1674 WARN(cdev, "%s: Unexpected call\n", __func__);
1675
1676 } else if (--cdev->delayed_status == 0) {
1677 DBG(cdev, "%s: Completing delayed status\n", __func__);
1678 req->length = 0;
1679 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1680 if (value < 0) {
1681 DBG(cdev, "ep_queue --> %d\n", value);
1682 req->status = 0;
1683 composite_setup_complete(cdev->gadget->ep0, req);
1684 }
1685 }
1686
1687 spin_unlock_irqrestore(&cdev->lock, flags);
1688}
1689
7d16e8d3
SAS
1690void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
1691 struct usb_composite_overwrite *covr)
1692{
1693 struct usb_device_descriptor *desc = &cdev->desc;
1694
1695 if (covr->idVendor)
1696 desc->idVendor = cpu_to_le16(covr->idVendor);
1697
1698 if (covr->idProduct)
1699 desc->idProduct = cpu_to_le16(covr->idProduct);
1700
1701 if (covr->bcdDevice)
1702 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
1703}