usb: gadget: composite: Fix possible double free memory bug
[linux/fpc-iii.git] / drivers / usb / gadget / composite.c
blob0f2d1e98481fbd36ed1291d11e32288bf88e1e41
1 /*
2 * composite.c - infrastructure for Composite USB Gadgets
4 * Copyright (C) 2006-2008 David Brownell
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.
12 /* #define VERBOSE_DEBUG */
14 #include <linux/kallsyms.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/utsname.h>
21 #include <linux/usb/composite.h>
22 #include <linux/usb/otg.h>
23 #include <asm/unaligned.h>
25 #include "u_os_desc.h"
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
35 struct 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;
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.
50 static struct usb_gadget_strings **get_containers_gs(
51 struct usb_gadget_string_container *uc)
53 return (struct usb_gadget_strings **)uc->stash;
56 /**
57 * next_ep_desc() - advance to the next EP descriptor
58 * @t: currect pointer within descriptor array
60 * Return: next EP descriptor or NULL
62 * Iterate over @t until either EP descriptor found or
63 * NULL (that indicates end of list) encountered
65 static struct usb_descriptor_header**
66 next_ep_desc(struct usb_descriptor_header **t)
68 for (; *t; t++) {
69 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
70 return t;
72 return NULL;
76 * for_each_ep_desc()- iterate over endpoint descriptors in the
77 * descriptors list
78 * @start: pointer within descriptor array.
79 * @ep_desc: endpoint descriptor to use as the loop cursor
81 #define for_each_ep_desc(start, ep_desc) \
82 for (ep_desc = next_ep_desc(start); \
83 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
85 /**
86 * config_ep_by_speed() - configures the given endpoint
87 * according to gadget speed.
88 * @g: pointer to the gadget
89 * @f: usb function
90 * @_ep: the endpoint to configure
92 * Return: error code, 0 on success
94 * This function chooses the right descriptors for a given
95 * endpoint according to gadget speed and saves it in the
96 * endpoint desc field. If the endpoint already has a descriptor
97 * assigned to it - overwrites it with currently corresponding
98 * descriptor. The endpoint maxpacket field is updated according
99 * to the chosen descriptor.
100 * Note: the supplied function should hold all the descriptors
101 * for supported speeds
103 int config_ep_by_speed(struct usb_gadget *g,
104 struct usb_function *f,
105 struct usb_ep *_ep)
107 struct usb_endpoint_descriptor *chosen_desc = NULL;
108 struct usb_descriptor_header **speed_desc = NULL;
110 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
111 int want_comp_desc = 0;
113 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
115 if (!g || !f || !_ep)
116 return -EIO;
118 /* select desired speed */
119 switch (g->speed) {
120 case USB_SPEED_SUPER:
121 if (gadget_is_superspeed(g)) {
122 speed_desc = f->ss_descriptors;
123 want_comp_desc = 1;
124 break;
126 /* else: Fall trough */
127 case USB_SPEED_HIGH:
128 if (gadget_is_dualspeed(g)) {
129 speed_desc = f->hs_descriptors;
130 break;
132 /* else: fall through */
133 default:
134 speed_desc = f->fs_descriptors;
136 /* find descriptors */
137 for_each_ep_desc(speed_desc, d_spd) {
138 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
139 if (chosen_desc->bEndpointAddress == _ep->address)
140 goto ep_found;
142 return -EIO;
144 ep_found:
145 /* commit results */
146 _ep->maxpacket = usb_endpoint_maxp(chosen_desc) & 0x7ff;
147 _ep->desc = chosen_desc;
148 _ep->comp_desc = NULL;
149 _ep->maxburst = 0;
150 _ep->mult = 1;
152 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
153 usb_endpoint_xfer_int(_ep->desc)))
154 _ep->mult = ((usb_endpoint_maxp(_ep->desc) & 0x1800) >> 11) + 1;
156 if (!want_comp_desc)
157 return 0;
160 * Companion descriptor should follow EP descriptor
161 * USB 3.0 spec, #9.6.7
163 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
164 if (!comp_desc ||
165 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
166 return -EIO;
167 _ep->comp_desc = comp_desc;
168 if (g->speed == USB_SPEED_SUPER) {
169 switch (usb_endpoint_type(_ep->desc)) {
170 case USB_ENDPOINT_XFER_ISOC:
171 /* mult: bits 1:0 of bmAttributes */
172 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
173 case USB_ENDPOINT_XFER_BULK:
174 case USB_ENDPOINT_XFER_INT:
175 _ep->maxburst = comp_desc->bMaxBurst + 1;
176 break;
177 default:
178 if (comp_desc->bMaxBurst != 0) {
179 struct usb_composite_dev *cdev;
181 cdev = get_gadget_data(g);
182 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
184 _ep->maxburst = 1;
185 break;
188 return 0;
190 EXPORT_SYMBOL_GPL(config_ep_by_speed);
193 * usb_add_function() - add a function to a configuration
194 * @config: the configuration
195 * @function: the function being added
196 * Context: single threaded during gadget setup
198 * After initialization, each configuration must have one or more
199 * functions added to it. Adding a function involves calling its @bind()
200 * method to allocate resources such as interface and string identifiers
201 * and endpoints.
203 * This function returns the value of the function's bind(), which is
204 * zero for success else a negative errno value.
206 int usb_add_function(struct usb_configuration *config,
207 struct usb_function *function)
209 int value = -EINVAL;
211 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
212 function->name, function,
213 config->label, config);
215 if (!function->set_alt || !function->disable)
216 goto done;
218 function->config = config;
219 list_add_tail(&function->list, &config->functions);
221 if (function->bind_deactivated) {
222 value = usb_function_deactivate(function);
223 if (value)
224 goto done;
227 /* REVISIT *require* function->bind? */
228 if (function->bind) {
229 value = function->bind(config, function);
230 if (value < 0) {
231 list_del(&function->list);
232 function->config = NULL;
234 } else
235 value = 0;
237 /* We allow configurations that don't work at both speeds.
238 * If we run into a lowspeed Linux system, treat it the same
239 * as full speed ... it's the function drivers that will need
240 * to avoid bulk and ISO transfers.
242 if (!config->fullspeed && function->fs_descriptors)
243 config->fullspeed = true;
244 if (!config->highspeed && function->hs_descriptors)
245 config->highspeed = true;
246 if (!config->superspeed && function->ss_descriptors)
247 config->superspeed = true;
249 done:
250 if (value)
251 DBG(config->cdev, "adding '%s'/%p --> %d\n",
252 function->name, function, value);
253 return value;
255 EXPORT_SYMBOL_GPL(usb_add_function);
257 void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
259 if (f->disable)
260 f->disable(f);
262 bitmap_zero(f->endpoints, 32);
263 list_del(&f->list);
264 if (f->unbind)
265 f->unbind(c, f);
267 EXPORT_SYMBOL_GPL(usb_remove_function);
270 * usb_function_deactivate - prevent function and gadget enumeration
271 * @function: the function that isn't yet ready to respond
273 * Blocks response of the gadget driver to host enumeration by
274 * preventing the data line pullup from being activated. This is
275 * normally called during @bind() processing to change from the
276 * initial "ready to respond" state, or when a required resource
277 * becomes available.
279 * For example, drivers that serve as a passthrough to a userspace
280 * daemon can block enumeration unless that daemon (such as an OBEX,
281 * MTP, or print server) is ready to handle host requests.
283 * Not all systems support software control of their USB peripheral
284 * data pullups.
286 * Returns zero on success, else negative errno.
288 int usb_function_deactivate(struct usb_function *function)
290 struct usb_composite_dev *cdev = function->config->cdev;
291 unsigned long flags;
292 int status = 0;
294 spin_lock_irqsave(&cdev->lock, flags);
296 if (cdev->deactivations == 0)
297 status = usb_gadget_deactivate(cdev->gadget);
298 if (status == 0)
299 cdev->deactivations++;
301 spin_unlock_irqrestore(&cdev->lock, flags);
302 return status;
304 EXPORT_SYMBOL_GPL(usb_function_deactivate);
307 * usb_function_activate - allow function and gadget enumeration
308 * @function: function on which usb_function_activate() was called
310 * Reverses effect of usb_function_deactivate(). If no more functions
311 * are delaying their activation, the gadget driver will respond to
312 * host enumeration procedures.
314 * Returns zero on success, else negative errno.
316 int usb_function_activate(struct usb_function *function)
318 struct usb_composite_dev *cdev = function->config->cdev;
319 unsigned long flags;
320 int status = 0;
322 spin_lock_irqsave(&cdev->lock, flags);
324 if (WARN_ON(cdev->deactivations == 0))
325 status = -EINVAL;
326 else {
327 cdev->deactivations--;
328 if (cdev->deactivations == 0)
329 status = usb_gadget_activate(cdev->gadget);
332 spin_unlock_irqrestore(&cdev->lock, flags);
333 return status;
335 EXPORT_SYMBOL_GPL(usb_function_activate);
338 * usb_interface_id() - allocate an unused interface ID
339 * @config: configuration associated with the interface
340 * @function: function handling the interface
341 * Context: single threaded during gadget setup
343 * usb_interface_id() is called from usb_function.bind() callbacks to
344 * allocate new interface IDs. The function driver will then store that
345 * ID in interface, association, CDC union, and other descriptors. It
346 * will also handle any control requests targeted at that interface,
347 * particularly changing its altsetting via set_alt(). There may
348 * also be class-specific or vendor-specific requests to handle.
350 * All interface identifier should be allocated using this routine, to
351 * ensure that for example different functions don't wrongly assign
352 * different meanings to the same identifier. Note that since interface
353 * identifiers are configuration-specific, functions used in more than
354 * one configuration (or more than once in a given configuration) need
355 * multiple versions of the relevant descriptors.
357 * Returns the interface ID which was allocated; or -ENODEV if no
358 * more interface IDs can be allocated.
360 int usb_interface_id(struct usb_configuration *config,
361 struct usb_function *function)
363 unsigned id = config->next_interface_id;
365 if (id < MAX_CONFIG_INTERFACES) {
366 config->interface[id] = function;
367 config->next_interface_id = id + 1;
368 return id;
370 return -ENODEV;
372 EXPORT_SYMBOL_GPL(usb_interface_id);
374 static u8 encode_bMaxPower(enum usb_device_speed speed,
375 struct usb_configuration *c)
377 unsigned val;
379 if (c->MaxPower)
380 val = c->MaxPower;
381 else
382 val = CONFIG_USB_GADGET_VBUS_DRAW;
383 if (!val)
384 return 0;
385 switch (speed) {
386 case USB_SPEED_SUPER:
387 return DIV_ROUND_UP(val, 8);
388 default:
389 return DIV_ROUND_UP(val, 2);
393 static int config_buf(struct usb_configuration *config,
394 enum usb_device_speed speed, void *buf, u8 type)
396 struct usb_config_descriptor *c = buf;
397 void *next = buf + USB_DT_CONFIG_SIZE;
398 int len;
399 struct usb_function *f;
400 int status;
402 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
403 /* write the config descriptor */
404 c = buf;
405 c->bLength = USB_DT_CONFIG_SIZE;
406 c->bDescriptorType = type;
407 /* wTotalLength is written later */
408 c->bNumInterfaces = config->next_interface_id;
409 c->bConfigurationValue = config->bConfigurationValue;
410 c->iConfiguration = config->iConfiguration;
411 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
412 c->bMaxPower = encode_bMaxPower(speed, config);
414 /* There may be e.g. OTG descriptors */
415 if (config->descriptors) {
416 status = usb_descriptor_fillbuf(next, len,
417 config->descriptors);
418 if (status < 0)
419 return status;
420 len -= status;
421 next += status;
424 /* add each function's descriptors */
425 list_for_each_entry(f, &config->functions, list) {
426 struct usb_descriptor_header **descriptors;
428 switch (speed) {
429 case USB_SPEED_SUPER:
430 descriptors = f->ss_descriptors;
431 break;
432 case USB_SPEED_HIGH:
433 descriptors = f->hs_descriptors;
434 break;
435 default:
436 descriptors = f->fs_descriptors;
439 if (!descriptors)
440 continue;
441 status = usb_descriptor_fillbuf(next, len,
442 (const struct usb_descriptor_header **) descriptors);
443 if (status < 0)
444 return status;
445 len -= status;
446 next += status;
449 len = next - buf;
450 c->wTotalLength = cpu_to_le16(len);
451 return len;
454 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
456 struct usb_gadget *gadget = cdev->gadget;
457 struct usb_configuration *c;
458 struct list_head *pos;
459 u8 type = w_value >> 8;
460 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
462 if (gadget->speed == USB_SPEED_SUPER)
463 speed = gadget->speed;
464 else if (gadget_is_dualspeed(gadget)) {
465 int hs = 0;
466 if (gadget->speed == USB_SPEED_HIGH)
467 hs = 1;
468 if (type == USB_DT_OTHER_SPEED_CONFIG)
469 hs = !hs;
470 if (hs)
471 speed = USB_SPEED_HIGH;
475 /* This is a lookup by config *INDEX* */
476 w_value &= 0xff;
478 pos = &cdev->configs;
479 c = cdev->os_desc_config;
480 if (c)
481 goto check_config;
483 while ((pos = pos->next) != &cdev->configs) {
484 c = list_entry(pos, typeof(*c), list);
486 /* skip OS Descriptors config which is handled separately */
487 if (c == cdev->os_desc_config)
488 continue;
490 check_config:
491 /* ignore configs that won't work at this speed */
492 switch (speed) {
493 case USB_SPEED_SUPER:
494 if (!c->superspeed)
495 continue;
496 break;
497 case USB_SPEED_HIGH:
498 if (!c->highspeed)
499 continue;
500 break;
501 default:
502 if (!c->fullspeed)
503 continue;
506 if (w_value == 0)
507 return config_buf(c, speed, cdev->req->buf, type);
508 w_value--;
510 return -EINVAL;
513 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
515 struct usb_gadget *gadget = cdev->gadget;
516 struct usb_configuration *c;
517 unsigned count = 0;
518 int hs = 0;
519 int ss = 0;
521 if (gadget_is_dualspeed(gadget)) {
522 if (gadget->speed == USB_SPEED_HIGH)
523 hs = 1;
524 if (gadget->speed == USB_SPEED_SUPER)
525 ss = 1;
526 if (type == USB_DT_DEVICE_QUALIFIER)
527 hs = !hs;
529 list_for_each_entry(c, &cdev->configs, list) {
530 /* ignore configs that won't work at this speed */
531 if (ss) {
532 if (!c->superspeed)
533 continue;
534 } else if (hs) {
535 if (!c->highspeed)
536 continue;
537 } else {
538 if (!c->fullspeed)
539 continue;
541 count++;
543 return count;
547 * bos_desc() - prepares the BOS descriptor.
548 * @cdev: pointer to usb_composite device to generate the bos
549 * descriptor for
551 * This function generates the BOS (Binary Device Object)
552 * descriptor and its device capabilities descriptors. The BOS
553 * descriptor should be supported by a SuperSpeed device.
555 static int bos_desc(struct usb_composite_dev *cdev)
557 struct usb_ext_cap_descriptor *usb_ext;
558 struct usb_ss_cap_descriptor *ss_cap;
559 struct usb_dcd_config_params dcd_config_params;
560 struct usb_bos_descriptor *bos = cdev->req->buf;
562 bos->bLength = USB_DT_BOS_SIZE;
563 bos->bDescriptorType = USB_DT_BOS;
565 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
566 bos->bNumDeviceCaps = 0;
569 * A SuperSpeed device shall include the USB2.0 extension descriptor
570 * and shall support LPM when operating in USB2.0 HS mode.
572 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
573 bos->bNumDeviceCaps++;
574 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
575 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
576 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
577 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
578 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
581 * The Superspeed USB Capability descriptor shall be implemented by all
582 * SuperSpeed devices.
584 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
585 bos->bNumDeviceCaps++;
586 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
587 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
588 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
589 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
590 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
591 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
592 USB_FULL_SPEED_OPERATION |
593 USB_HIGH_SPEED_OPERATION |
594 USB_5GBPS_OPERATION);
595 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
597 /* Get Controller configuration */
598 if (cdev->gadget->ops->get_config_params)
599 cdev->gadget->ops->get_config_params(&dcd_config_params);
600 else {
601 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
602 dcd_config_params.bU2DevExitLat =
603 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
605 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
606 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
608 return le16_to_cpu(bos->wTotalLength);
611 static void device_qual(struct usb_composite_dev *cdev)
613 struct usb_qualifier_descriptor *qual = cdev->req->buf;
615 qual->bLength = sizeof(*qual);
616 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
617 /* POLICY: same bcdUSB and device type info at both speeds */
618 qual->bcdUSB = cdev->desc.bcdUSB;
619 qual->bDeviceClass = cdev->desc.bDeviceClass;
620 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
621 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
622 /* ASSUME same EP0 fifo size at both speeds */
623 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
624 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
625 qual->bRESERVED = 0;
628 /*-------------------------------------------------------------------------*/
630 static void reset_config(struct usb_composite_dev *cdev)
632 struct usb_function *f;
634 DBG(cdev, "reset config\n");
636 list_for_each_entry(f, &cdev->config->functions, list) {
637 if (f->disable)
638 f->disable(f);
640 bitmap_zero(f->endpoints, 32);
642 cdev->config = NULL;
643 cdev->delayed_status = 0;
646 static int set_config(struct usb_composite_dev *cdev,
647 const struct usb_ctrlrequest *ctrl, unsigned number)
649 struct usb_gadget *gadget = cdev->gadget;
650 struct usb_configuration *c = NULL;
651 int result = -EINVAL;
652 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
653 int tmp;
655 if (number) {
656 list_for_each_entry(c, &cdev->configs, list) {
657 if (c->bConfigurationValue == number) {
659 * We disable the FDs of the previous
660 * configuration only if the new configuration
661 * is a valid one
663 if (cdev->config)
664 reset_config(cdev);
665 result = 0;
666 break;
669 if (result < 0)
670 goto done;
671 } else { /* Zero configuration value - need to reset the config */
672 if (cdev->config)
673 reset_config(cdev);
674 result = 0;
677 INFO(cdev, "%s config #%d: %s\n",
678 usb_speed_string(gadget->speed),
679 number, c ? c->label : "unconfigured");
681 if (!c)
682 goto done;
684 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
685 cdev->config = c;
687 /* Initialize all interfaces by setting them to altsetting zero. */
688 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
689 struct usb_function *f = c->interface[tmp];
690 struct usb_descriptor_header **descriptors;
692 if (!f)
693 break;
696 * Record which endpoints are used by the function. This is used
697 * to dispatch control requests targeted at that endpoint to the
698 * function's setup callback instead of the current
699 * configuration's setup callback.
701 switch (gadget->speed) {
702 case USB_SPEED_SUPER:
703 descriptors = f->ss_descriptors;
704 break;
705 case USB_SPEED_HIGH:
706 descriptors = f->hs_descriptors;
707 break;
708 default:
709 descriptors = f->fs_descriptors;
712 for (; *descriptors; ++descriptors) {
713 struct usb_endpoint_descriptor *ep;
714 int addr;
716 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
717 continue;
719 ep = (struct usb_endpoint_descriptor *)*descriptors;
720 addr = ((ep->bEndpointAddress & 0x80) >> 3)
721 | (ep->bEndpointAddress & 0x0f);
722 set_bit(addr, f->endpoints);
725 result = f->set_alt(f, tmp, 0);
726 if (result < 0) {
727 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
728 tmp, f->name, f, result);
730 reset_config(cdev);
731 goto done;
734 if (result == USB_GADGET_DELAYED_STATUS) {
735 DBG(cdev,
736 "%s: interface %d (%s) requested delayed status\n",
737 __func__, tmp, f->name);
738 cdev->delayed_status++;
739 DBG(cdev, "delayed_status count %d\n",
740 cdev->delayed_status);
744 /* when we return, be sure our power usage is valid */
745 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
746 done:
747 usb_gadget_vbus_draw(gadget, power);
748 if (result >= 0 && cdev->delayed_status)
749 result = USB_GADGET_DELAYED_STATUS;
750 return result;
753 int usb_add_config_only(struct usb_composite_dev *cdev,
754 struct usb_configuration *config)
756 struct usb_configuration *c;
758 if (!config->bConfigurationValue)
759 return -EINVAL;
761 /* Prevent duplicate configuration identifiers */
762 list_for_each_entry(c, &cdev->configs, list) {
763 if (c->bConfigurationValue == config->bConfigurationValue)
764 return -EBUSY;
767 config->cdev = cdev;
768 list_add_tail(&config->list, &cdev->configs);
770 INIT_LIST_HEAD(&config->functions);
771 config->next_interface_id = 0;
772 memset(config->interface, 0, sizeof(config->interface));
774 return 0;
776 EXPORT_SYMBOL_GPL(usb_add_config_only);
779 * usb_add_config() - add a configuration to a device.
780 * @cdev: wraps the USB gadget
781 * @config: the configuration, with bConfigurationValue assigned
782 * @bind: the configuration's bind function
783 * Context: single threaded during gadget setup
785 * One of the main tasks of a composite @bind() routine is to
786 * add each of the configurations it supports, using this routine.
788 * This function returns the value of the configuration's @bind(), which
789 * is zero for success else a negative errno value. Binding configurations
790 * assigns global resources including string IDs, and per-configuration
791 * resources such as interface IDs and endpoints.
793 int usb_add_config(struct usb_composite_dev *cdev,
794 struct usb_configuration *config,
795 int (*bind)(struct usb_configuration *))
797 int status = -EINVAL;
799 if (!bind)
800 goto done;
802 DBG(cdev, "adding config #%u '%s'/%p\n",
803 config->bConfigurationValue,
804 config->label, config);
806 status = usb_add_config_only(cdev, config);
807 if (status)
808 goto done;
810 status = bind(config);
811 if (status < 0) {
812 while (!list_empty(&config->functions)) {
813 struct usb_function *f;
815 f = list_first_entry(&config->functions,
816 struct usb_function, list);
817 list_del(&f->list);
818 if (f->unbind) {
819 DBG(cdev, "unbind function '%s'/%p\n",
820 f->name, f);
821 f->unbind(config, f);
822 /* may free memory for "f" */
825 list_del(&config->list);
826 config->cdev = NULL;
827 } else {
828 unsigned i;
830 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
831 config->bConfigurationValue, config,
832 config->superspeed ? " super" : "",
833 config->highspeed ? " high" : "",
834 config->fullspeed
835 ? (gadget_is_dualspeed(cdev->gadget)
836 ? " full"
837 : " full/low")
838 : "");
840 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
841 struct usb_function *f = config->interface[i];
843 if (!f)
844 continue;
845 DBG(cdev, " interface %d = %s/%p\n",
846 i, f->name, f);
850 /* set_alt(), or next bind(), sets up ep->claimed as needed */
851 usb_ep_autoconfig_reset(cdev->gadget);
853 done:
854 if (status)
855 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
856 config->bConfigurationValue, status);
857 return status;
859 EXPORT_SYMBOL_GPL(usb_add_config);
861 static void remove_config(struct usb_composite_dev *cdev,
862 struct usb_configuration *config)
864 while (!list_empty(&config->functions)) {
865 struct usb_function *f;
867 f = list_first_entry(&config->functions,
868 struct usb_function, list);
869 list_del(&f->list);
870 if (f->unbind) {
871 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
872 f->unbind(config, f);
873 /* may free memory for "f" */
876 list_del(&config->list);
877 if (config->unbind) {
878 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
879 config->unbind(config);
880 /* may free memory for "c" */
885 * usb_remove_config() - remove a configuration from a device.
886 * @cdev: wraps the USB gadget
887 * @config: the configuration
889 * Drivers must call usb_gadget_disconnect before calling this function
890 * to disconnect the device from the host and make sure the host will not
891 * try to enumerate the device while we are changing the config list.
893 void usb_remove_config(struct usb_composite_dev *cdev,
894 struct usb_configuration *config)
896 unsigned long flags;
898 spin_lock_irqsave(&cdev->lock, flags);
900 if (cdev->config == config)
901 reset_config(cdev);
903 spin_unlock_irqrestore(&cdev->lock, flags);
905 remove_config(cdev, config);
908 /*-------------------------------------------------------------------------*/
910 /* We support strings in multiple languages ... string descriptor zero
911 * says which languages are supported. The typical case will be that
912 * only one language (probably English) is used, with i18n handled on
913 * the host side.
916 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
918 const struct usb_gadget_strings *s;
919 __le16 language;
920 __le16 *tmp;
922 while (*sp) {
923 s = *sp;
924 language = cpu_to_le16(s->language);
925 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
926 if (*tmp == language)
927 goto repeat;
929 *tmp++ = language;
930 repeat:
931 sp++;
935 static int lookup_string(
936 struct usb_gadget_strings **sp,
937 void *buf,
938 u16 language,
939 int id
942 struct usb_gadget_strings *s;
943 int value;
945 while (*sp) {
946 s = *sp++;
947 if (s->language != language)
948 continue;
949 value = usb_gadget_get_string(s, id, buf);
950 if (value > 0)
951 return value;
953 return -EINVAL;
956 static int get_string(struct usb_composite_dev *cdev,
957 void *buf, u16 language, int id)
959 struct usb_composite_driver *composite = cdev->driver;
960 struct usb_gadget_string_container *uc;
961 struct usb_configuration *c;
962 struct usb_function *f;
963 int len;
965 /* Yes, not only is USB's i18n support probably more than most
966 * folk will ever care about ... also, it's all supported here.
967 * (Except for UTF8 support for Unicode's "Astral Planes".)
970 /* 0 == report all available language codes */
971 if (id == 0) {
972 struct usb_string_descriptor *s = buf;
973 struct usb_gadget_strings **sp;
975 memset(s, 0, 256);
976 s->bDescriptorType = USB_DT_STRING;
978 sp = composite->strings;
979 if (sp)
980 collect_langs(sp, s->wData);
982 list_for_each_entry(c, &cdev->configs, list) {
983 sp = c->strings;
984 if (sp)
985 collect_langs(sp, s->wData);
987 list_for_each_entry(f, &c->functions, list) {
988 sp = f->strings;
989 if (sp)
990 collect_langs(sp, s->wData);
993 list_for_each_entry(uc, &cdev->gstrings, list) {
994 struct usb_gadget_strings **sp;
996 sp = get_containers_gs(uc);
997 collect_langs(sp, s->wData);
1000 for (len = 0; len <= 126 && s->wData[len]; len++)
1001 continue;
1002 if (!len)
1003 return -EINVAL;
1005 s->bLength = 2 * (len + 1);
1006 return s->bLength;
1009 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1010 struct usb_os_string *b = buf;
1011 b->bLength = sizeof(*b);
1012 b->bDescriptorType = USB_DT_STRING;
1013 compiletime_assert(
1014 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1015 "qwSignature size must be equal to qw_sign");
1016 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1017 b->bMS_VendorCode = cdev->b_vendor_code;
1018 b->bPad = 0;
1019 return sizeof(*b);
1022 list_for_each_entry(uc, &cdev->gstrings, list) {
1023 struct usb_gadget_strings **sp;
1025 sp = get_containers_gs(uc);
1026 len = lookup_string(sp, buf, language, id);
1027 if (len > 0)
1028 return len;
1031 /* String IDs are device-scoped, so we look up each string
1032 * table we're told about. These lookups are infrequent;
1033 * simpler-is-better here.
1035 if (composite->strings) {
1036 len = lookup_string(composite->strings, buf, language, id);
1037 if (len > 0)
1038 return len;
1040 list_for_each_entry(c, &cdev->configs, list) {
1041 if (c->strings) {
1042 len = lookup_string(c->strings, buf, language, id);
1043 if (len > 0)
1044 return len;
1046 list_for_each_entry(f, &c->functions, list) {
1047 if (!f->strings)
1048 continue;
1049 len = lookup_string(f->strings, buf, language, id);
1050 if (len > 0)
1051 return len;
1054 return -EINVAL;
1058 * usb_string_id() - allocate an unused string ID
1059 * @cdev: the device whose string descriptor IDs are being allocated
1060 * Context: single threaded during gadget setup
1062 * @usb_string_id() is called from bind() callbacks to allocate
1063 * string IDs. Drivers for functions, configurations, or gadgets will
1064 * then store that ID in the appropriate descriptors and string table.
1066 * All string identifier should be allocated using this,
1067 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1068 * that for example different functions don't wrongly assign different
1069 * meanings to the same identifier.
1071 int usb_string_id(struct usb_composite_dev *cdev)
1073 if (cdev->next_string_id < 254) {
1074 /* string id 0 is reserved by USB spec for list of
1075 * supported languages */
1076 /* 255 reserved as well? -- mina86 */
1077 cdev->next_string_id++;
1078 return cdev->next_string_id;
1080 return -ENODEV;
1082 EXPORT_SYMBOL_GPL(usb_string_id);
1085 * usb_string_ids() - allocate unused string IDs in batch
1086 * @cdev: the device whose string descriptor IDs are being allocated
1087 * @str: an array of usb_string objects to assign numbers to
1088 * Context: single threaded during gadget setup
1090 * @usb_string_ids() is called from bind() callbacks to allocate
1091 * string IDs. Drivers for functions, configurations, or gadgets will
1092 * then copy IDs from the string table to the appropriate descriptors
1093 * and string table for other languages.
1095 * All string identifier should be allocated using this,
1096 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1097 * example different functions don't wrongly assign different meanings
1098 * to the same identifier.
1100 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1102 int next = cdev->next_string_id;
1104 for (; str->s; ++str) {
1105 if (unlikely(next >= 254))
1106 return -ENODEV;
1107 str->id = ++next;
1110 cdev->next_string_id = next;
1112 return 0;
1114 EXPORT_SYMBOL_GPL(usb_string_ids_tab);
1116 static struct usb_gadget_string_container *copy_gadget_strings(
1117 struct usb_gadget_strings **sp, unsigned n_gstrings,
1118 unsigned n_strings)
1120 struct usb_gadget_string_container *uc;
1121 struct usb_gadget_strings **gs_array;
1122 struct usb_gadget_strings *gs;
1123 struct usb_string *s;
1124 unsigned mem;
1125 unsigned n_gs;
1126 unsigned n_s;
1127 void *stash;
1129 mem = sizeof(*uc);
1130 mem += sizeof(void *) * (n_gstrings + 1);
1131 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1132 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1133 uc = kmalloc(mem, GFP_KERNEL);
1134 if (!uc)
1135 return ERR_PTR(-ENOMEM);
1136 gs_array = get_containers_gs(uc);
1137 stash = uc->stash;
1138 stash += sizeof(void *) * (n_gstrings + 1);
1139 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1140 struct usb_string *org_s;
1142 gs_array[n_gs] = stash;
1143 gs = gs_array[n_gs];
1144 stash += sizeof(struct usb_gadget_strings);
1145 gs->language = sp[n_gs]->language;
1146 gs->strings = stash;
1147 org_s = sp[n_gs]->strings;
1149 for (n_s = 0; n_s < n_strings; n_s++) {
1150 s = stash;
1151 stash += sizeof(struct usb_string);
1152 if (org_s->s)
1153 s->s = org_s->s;
1154 else
1155 s->s = "";
1156 org_s++;
1158 s = stash;
1159 s->s = NULL;
1160 stash += sizeof(struct usb_string);
1163 gs_array[n_gs] = NULL;
1164 return uc;
1168 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1169 * @cdev: the device whose string descriptor IDs are being allocated
1170 * and attached.
1171 * @sp: an array of usb_gadget_strings to attach.
1172 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1174 * This function will create a deep copy of usb_gadget_strings and usb_string
1175 * and attach it to the cdev. The actual string (usb_string.s) will not be
1176 * copied but only a referenced will be made. The struct usb_gadget_strings
1177 * array may contain multiple languages and should be NULL terminated.
1178 * The ->language pointer of each struct usb_gadget_strings has to contain the
1179 * same amount of entries.
1180 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1181 * usb_string entry of es-ES contains the translation of the first usb_string
1182 * entry of en-US. Therefore both entries become the same id assign.
1184 struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1185 struct usb_gadget_strings **sp, unsigned n_strings)
1187 struct usb_gadget_string_container *uc;
1188 struct usb_gadget_strings **n_gs;
1189 unsigned n_gstrings = 0;
1190 unsigned i;
1191 int ret;
1193 for (i = 0; sp[i]; i++)
1194 n_gstrings++;
1196 if (!n_gstrings)
1197 return ERR_PTR(-EINVAL);
1199 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1200 if (IS_ERR(uc))
1201 return ERR_CAST(uc);
1203 n_gs = get_containers_gs(uc);
1204 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1205 if (ret)
1206 goto err;
1208 for (i = 1; i < n_gstrings; i++) {
1209 struct usb_string *m_s;
1210 struct usb_string *s;
1211 unsigned n;
1213 m_s = n_gs[0]->strings;
1214 s = n_gs[i]->strings;
1215 for (n = 0; n < n_strings; n++) {
1216 s->id = m_s->id;
1217 s++;
1218 m_s++;
1221 list_add_tail(&uc->list, &cdev->gstrings);
1222 return n_gs[0]->strings;
1223 err:
1224 kfree(uc);
1225 return ERR_PTR(ret);
1227 EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1230 * usb_string_ids_n() - allocate unused string IDs in batch
1231 * @c: the device whose string descriptor IDs are being allocated
1232 * @n: number of string IDs to allocate
1233 * Context: single threaded during gadget setup
1235 * Returns the first requested ID. This ID and next @n-1 IDs are now
1236 * valid IDs. At least provided that @n is non-zero because if it
1237 * is, returns last requested ID which is now very useful information.
1239 * @usb_string_ids_n() is called from bind() callbacks to allocate
1240 * string IDs. Drivers for functions, configurations, or gadgets will
1241 * then store that ID in the appropriate descriptors and string table.
1243 * All string identifier should be allocated using this,
1244 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1245 * example different functions don't wrongly assign different meanings
1246 * to the same identifier.
1248 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1250 unsigned next = c->next_string_id;
1251 if (unlikely(n > 254 || (unsigned)next + n > 254))
1252 return -ENODEV;
1253 c->next_string_id += n;
1254 return next + 1;
1256 EXPORT_SYMBOL_GPL(usb_string_ids_n);
1258 /*-------------------------------------------------------------------------*/
1260 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1262 struct usb_composite_dev *cdev;
1264 if (req->status || req->actual != req->length)
1265 DBG((struct usb_composite_dev *) ep->driver_data,
1266 "setup complete --> %d, %d/%d\n",
1267 req->status, req->actual, req->length);
1270 * REVIST The same ep0 requests are shared with function drivers
1271 * so they don't have to maintain the same ->complete() stubs.
1273 * Because of that, we need to check for the validity of ->context
1274 * here, even though we know we've set it to something useful.
1276 if (!req->context)
1277 return;
1279 cdev = req->context;
1281 if (cdev->req == req)
1282 cdev->setup_pending = false;
1283 else if (cdev->os_desc_req == req)
1284 cdev->os_desc_pending = false;
1285 else
1286 WARN(1, "unknown request %p\n", req);
1289 static int composite_ep0_queue(struct usb_composite_dev *cdev,
1290 struct usb_request *req, gfp_t gfp_flags)
1292 int ret;
1294 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1295 if (ret == 0) {
1296 if (cdev->req == req)
1297 cdev->setup_pending = true;
1298 else if (cdev->os_desc_req == req)
1299 cdev->os_desc_pending = true;
1300 else
1301 WARN(1, "unknown request %p\n", req);
1304 return ret;
1307 static int count_ext_compat(struct usb_configuration *c)
1309 int i, res;
1311 res = 0;
1312 for (i = 0; i < c->next_interface_id; ++i) {
1313 struct usb_function *f;
1314 int j;
1316 f = c->interface[i];
1317 for (j = 0; j < f->os_desc_n; ++j) {
1318 struct usb_os_desc *d;
1320 if (i != f->os_desc_table[j].if_id)
1321 continue;
1322 d = f->os_desc_table[j].os_desc;
1323 if (d && d->ext_compat_id)
1324 ++res;
1327 BUG_ON(res > 255);
1328 return res;
1331 static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
1333 int i, count;
1335 count = 16;
1336 for (i = 0; i < c->next_interface_id; ++i) {
1337 struct usb_function *f;
1338 int j;
1340 f = c->interface[i];
1341 for (j = 0; j < f->os_desc_n; ++j) {
1342 struct usb_os_desc *d;
1344 if (i != f->os_desc_table[j].if_id)
1345 continue;
1346 d = f->os_desc_table[j].os_desc;
1347 if (d && d->ext_compat_id) {
1348 *buf++ = i;
1349 *buf++ = 0x01;
1350 memcpy(buf, d->ext_compat_id, 16);
1351 buf += 22;
1352 } else {
1353 ++buf;
1354 *buf = 0x01;
1355 buf += 23;
1357 count += 24;
1358 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1359 return count;
1363 return count;
1366 static int count_ext_prop(struct usb_configuration *c, int interface)
1368 struct usb_function *f;
1369 int j;
1371 f = c->interface[interface];
1372 for (j = 0; j < f->os_desc_n; ++j) {
1373 struct usb_os_desc *d;
1375 if (interface != f->os_desc_table[j].if_id)
1376 continue;
1377 d = f->os_desc_table[j].os_desc;
1378 if (d && d->ext_compat_id)
1379 return d->ext_prop_count;
1381 return 0;
1384 static int len_ext_prop(struct usb_configuration *c, int interface)
1386 struct usb_function *f;
1387 struct usb_os_desc *d;
1388 int j, res;
1390 res = 10; /* header length */
1391 f = c->interface[interface];
1392 for (j = 0; j < f->os_desc_n; ++j) {
1393 if (interface != f->os_desc_table[j].if_id)
1394 continue;
1395 d = f->os_desc_table[j].os_desc;
1396 if (d)
1397 return min(res + d->ext_prop_len, 4096);
1399 return res;
1402 static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1404 struct usb_function *f;
1405 struct usb_os_desc *d;
1406 struct usb_os_desc_ext_prop *ext_prop;
1407 int j, count, n, ret;
1409 f = c->interface[interface];
1410 count = 10; /* header length */
1411 for (j = 0; j < f->os_desc_n; ++j) {
1412 if (interface != f->os_desc_table[j].if_id)
1413 continue;
1414 d = f->os_desc_table[j].os_desc;
1415 if (d)
1416 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1417 n = ext_prop->data_len +
1418 ext_prop->name_len + 14;
1419 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1420 return count;
1421 usb_ext_prop_put_size(buf, n);
1422 usb_ext_prop_put_type(buf, ext_prop->type);
1423 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1424 ext_prop->name_len);
1425 if (ret < 0)
1426 return ret;
1427 switch (ext_prop->type) {
1428 case USB_EXT_PROP_UNICODE:
1429 case USB_EXT_PROP_UNICODE_ENV:
1430 case USB_EXT_PROP_UNICODE_LINK:
1431 usb_ext_prop_put_unicode(buf, ret,
1432 ext_prop->data,
1433 ext_prop->data_len);
1434 break;
1435 case USB_EXT_PROP_BINARY:
1436 usb_ext_prop_put_binary(buf, ret,
1437 ext_prop->data,
1438 ext_prop->data_len);
1439 break;
1440 case USB_EXT_PROP_LE32:
1441 /* not implemented */
1442 case USB_EXT_PROP_BE32:
1443 /* not implemented */
1444 default:
1445 return -EINVAL;
1447 buf += n;
1448 count += n;
1452 return count;
1456 * The setup() callback implements all the ep0 functionality that's
1457 * not handled lower down, in hardware or the hardware driver(like
1458 * device and endpoint feature flags, and their status). It's all
1459 * housekeeping for the gadget function we're implementing. Most of
1460 * the work is in config and function specific setup.
1463 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1465 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1466 struct usb_request *req = cdev->req;
1467 int value = -EOPNOTSUPP;
1468 int status = 0;
1469 u16 w_index = le16_to_cpu(ctrl->wIndex);
1470 u8 intf = w_index & 0xFF;
1471 u16 w_value = le16_to_cpu(ctrl->wValue);
1472 u16 w_length = le16_to_cpu(ctrl->wLength);
1473 struct usb_function *f = NULL;
1474 u8 endp;
1476 /* partial re-init of the response message; the function or the
1477 * gadget might need to intercept e.g. a control-OUT completion
1478 * when we delegate to it.
1480 req->zero = 0;
1481 req->context = cdev;
1482 req->complete = composite_setup_complete;
1483 req->length = 0;
1484 gadget->ep0->driver_data = cdev;
1487 * Don't let non-standard requests match any of the cases below
1488 * by accident.
1490 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1491 goto unknown;
1493 switch (ctrl->bRequest) {
1495 /* we handle all standard USB descriptors */
1496 case USB_REQ_GET_DESCRIPTOR:
1497 if (ctrl->bRequestType != USB_DIR_IN)
1498 goto unknown;
1499 switch (w_value >> 8) {
1501 case USB_DT_DEVICE:
1502 cdev->desc.bNumConfigurations =
1503 count_configs(cdev, USB_DT_DEVICE);
1504 cdev->desc.bMaxPacketSize0 =
1505 cdev->gadget->ep0->maxpacket;
1506 if (gadget_is_superspeed(gadget)) {
1507 if (gadget->speed >= USB_SPEED_SUPER) {
1508 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
1509 cdev->desc.bMaxPacketSize0 = 9;
1510 } else {
1511 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1513 } else {
1514 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
1517 value = min(w_length, (u16) sizeof cdev->desc);
1518 memcpy(req->buf, &cdev->desc, value);
1519 break;
1520 case USB_DT_DEVICE_QUALIFIER:
1521 if (!gadget_is_dualspeed(gadget) ||
1522 gadget->speed >= USB_SPEED_SUPER)
1523 break;
1524 device_qual(cdev);
1525 value = min_t(int, w_length,
1526 sizeof(struct usb_qualifier_descriptor));
1527 break;
1528 case USB_DT_OTHER_SPEED_CONFIG:
1529 if (!gadget_is_dualspeed(gadget) ||
1530 gadget->speed >= USB_SPEED_SUPER)
1531 break;
1532 /* FALLTHROUGH */
1533 case USB_DT_CONFIG:
1534 value = config_desc(cdev, w_value);
1535 if (value >= 0)
1536 value = min(w_length, (u16) value);
1537 break;
1538 case USB_DT_STRING:
1539 value = get_string(cdev, req->buf,
1540 w_index, w_value & 0xff);
1541 if (value >= 0)
1542 value = min(w_length, (u16) value);
1543 break;
1544 case USB_DT_BOS:
1545 if (gadget_is_superspeed(gadget)) {
1546 value = bos_desc(cdev);
1547 value = min(w_length, (u16) value);
1549 break;
1550 case USB_DT_OTG:
1551 if (gadget_is_otg(gadget)) {
1552 struct usb_configuration *config;
1553 int otg_desc_len = 0;
1555 if (cdev->config)
1556 config = cdev->config;
1557 else
1558 config = list_first_entry(
1559 &cdev->configs,
1560 struct usb_configuration, list);
1561 if (!config)
1562 goto done;
1564 if (gadget->otg_caps &&
1565 (gadget->otg_caps->otg_rev >= 0x0200))
1566 otg_desc_len += sizeof(
1567 struct usb_otg20_descriptor);
1568 else
1569 otg_desc_len += sizeof(
1570 struct usb_otg_descriptor);
1572 value = min_t(int, w_length, otg_desc_len);
1573 memcpy(req->buf, config->descriptors[0], value);
1575 break;
1577 break;
1579 /* any number of configs can work */
1580 case USB_REQ_SET_CONFIGURATION:
1581 if (ctrl->bRequestType != 0)
1582 goto unknown;
1583 if (gadget_is_otg(gadget)) {
1584 if (gadget->a_hnp_support)
1585 DBG(cdev, "HNP available\n");
1586 else if (gadget->a_alt_hnp_support)
1587 DBG(cdev, "HNP on another port\n");
1588 else
1589 VDBG(cdev, "HNP inactive\n");
1591 spin_lock(&cdev->lock);
1592 value = set_config(cdev, ctrl, w_value);
1593 spin_unlock(&cdev->lock);
1594 break;
1595 case USB_REQ_GET_CONFIGURATION:
1596 if (ctrl->bRequestType != USB_DIR_IN)
1597 goto unknown;
1598 if (cdev->config)
1599 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1600 else
1601 *(u8 *)req->buf = 0;
1602 value = min(w_length, (u16) 1);
1603 break;
1605 /* function drivers must handle get/set altsetting */
1606 case USB_REQ_SET_INTERFACE:
1607 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1608 goto unknown;
1609 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1610 break;
1611 f = cdev->config->interface[intf];
1612 if (!f)
1613 break;
1616 * If there's no get_alt() method, we know only altsetting zero
1617 * works. There is no need to check if set_alt() is not NULL
1618 * as we check this in usb_add_function().
1620 if (w_value && !f->get_alt)
1621 break;
1623 spin_lock(&cdev->lock);
1624 value = f->set_alt(f, w_index, w_value);
1625 if (value == USB_GADGET_DELAYED_STATUS) {
1626 DBG(cdev,
1627 "%s: interface %d (%s) requested delayed status\n",
1628 __func__, intf, f->name);
1629 cdev->delayed_status++;
1630 DBG(cdev, "delayed_status count %d\n",
1631 cdev->delayed_status);
1633 spin_unlock(&cdev->lock);
1634 break;
1635 case USB_REQ_GET_INTERFACE:
1636 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1637 goto unknown;
1638 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1639 break;
1640 f = cdev->config->interface[intf];
1641 if (!f)
1642 break;
1643 /* lots of interfaces only need altsetting zero... */
1644 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1645 if (value < 0)
1646 break;
1647 *((u8 *)req->buf) = value;
1648 value = min(w_length, (u16) 1);
1649 break;
1652 * USB 3.0 additions:
1653 * Function driver should handle get_status request. If such cb
1654 * wasn't supplied we respond with default value = 0
1655 * Note: function driver should supply such cb only for the first
1656 * interface of the function
1658 case USB_REQ_GET_STATUS:
1659 if (!gadget_is_superspeed(gadget))
1660 goto unknown;
1661 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1662 goto unknown;
1663 value = 2; /* This is the length of the get_status reply */
1664 put_unaligned_le16(0, req->buf);
1665 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1666 break;
1667 f = cdev->config->interface[intf];
1668 if (!f)
1669 break;
1670 status = f->get_status ? f->get_status(f) : 0;
1671 if (status < 0)
1672 break;
1673 put_unaligned_le16(status & 0x0000ffff, req->buf);
1674 break;
1676 * Function drivers should handle SetFeature/ClearFeature
1677 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1678 * only for the first interface of the function
1680 case USB_REQ_CLEAR_FEATURE:
1681 case USB_REQ_SET_FEATURE:
1682 if (!gadget_is_superspeed(gadget))
1683 goto unknown;
1684 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1685 goto unknown;
1686 switch (w_value) {
1687 case USB_INTRF_FUNC_SUSPEND:
1688 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1689 break;
1690 f = cdev->config->interface[intf];
1691 if (!f)
1692 break;
1693 value = 0;
1694 if (f->func_suspend)
1695 value = f->func_suspend(f, w_index >> 8);
1696 if (value < 0) {
1697 ERROR(cdev,
1698 "func_suspend() returned error %d\n",
1699 value);
1700 value = 0;
1702 break;
1704 break;
1705 default:
1706 unknown:
1708 * OS descriptors handling
1710 if (cdev->use_os_string && cdev->os_desc_config &&
1711 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
1712 ctrl->bRequest == cdev->b_vendor_code) {
1713 struct usb_request *req;
1714 struct usb_configuration *os_desc_cfg;
1715 u8 *buf;
1716 int interface;
1717 int count = 0;
1719 req = cdev->os_desc_req;
1720 req->context = cdev;
1721 req->complete = composite_setup_complete;
1722 buf = req->buf;
1723 os_desc_cfg = cdev->os_desc_config;
1724 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
1725 memset(buf, 0, w_length);
1726 buf[5] = 0x01;
1727 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1728 case USB_RECIP_DEVICE:
1729 if (w_index != 0x4 || (w_value >> 8))
1730 break;
1731 buf[6] = w_index;
1732 if (w_length == 0x10) {
1733 /* Number of ext compat interfaces */
1734 count = count_ext_compat(os_desc_cfg);
1735 buf[8] = count;
1736 count *= 24; /* 24 B/ext compat desc */
1737 count += 16; /* header */
1738 put_unaligned_le32(count, buf);
1739 value = w_length;
1740 } else {
1741 /* "extended compatibility ID"s */
1742 count = count_ext_compat(os_desc_cfg);
1743 buf[8] = count;
1744 count *= 24; /* 24 B/ext compat desc */
1745 count += 16; /* header */
1746 put_unaligned_le32(count, buf);
1747 buf += 16;
1748 value = fill_ext_compat(os_desc_cfg, buf);
1749 value = min_t(u16, w_length, value);
1751 break;
1752 case USB_RECIP_INTERFACE:
1753 if (w_index != 0x5 || (w_value >> 8))
1754 break;
1755 interface = w_value & 0xFF;
1756 buf[6] = w_index;
1757 if (w_length == 0x0A) {
1758 count = count_ext_prop(os_desc_cfg,
1759 interface);
1760 put_unaligned_le16(count, buf + 8);
1761 count = len_ext_prop(os_desc_cfg,
1762 interface);
1763 put_unaligned_le32(count, buf);
1765 value = w_length;
1766 } else {
1767 count = count_ext_prop(os_desc_cfg,
1768 interface);
1769 put_unaligned_le16(count, buf + 8);
1770 count = len_ext_prop(os_desc_cfg,
1771 interface);
1772 put_unaligned_le32(count, buf);
1773 buf += 10;
1774 value = fill_ext_prop(os_desc_cfg,
1775 interface, buf);
1776 if (value < 0)
1777 return value;
1778 value = min_t(u16, w_length, value);
1780 break;
1782 req->length = value;
1783 req->context = cdev;
1784 req->zero = value < w_length;
1785 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
1786 if (value < 0) {
1787 DBG(cdev, "ep_queue --> %d\n", value);
1788 req->status = 0;
1789 composite_setup_complete(gadget->ep0, req);
1791 return value;
1794 VDBG(cdev,
1795 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1796 ctrl->bRequestType, ctrl->bRequest,
1797 w_value, w_index, w_length);
1799 /* functions always handle their interfaces and endpoints...
1800 * punt other recipients (other, WUSB, ...) to the current
1801 * configuration code.
1803 * REVISIT it could make sense to let the composite device
1804 * take such requests too, if that's ever needed: to work
1805 * in config 0, etc.
1807 if (cdev->config) {
1808 list_for_each_entry(f, &cdev->config->functions, list)
1809 if (f->req_match && f->req_match(f, ctrl))
1810 goto try_fun_setup;
1811 f = NULL;
1814 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1815 case USB_RECIP_INTERFACE:
1816 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1817 break;
1818 f = cdev->config->interface[intf];
1819 break;
1821 case USB_RECIP_ENDPOINT:
1822 if (!cdev->config)
1823 break;
1824 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1825 list_for_each_entry(f, &cdev->config->functions, list) {
1826 if (test_bit(endp, f->endpoints))
1827 break;
1829 if (&f->list == &cdev->config->functions)
1830 f = NULL;
1831 break;
1833 try_fun_setup:
1834 if (f && f->setup)
1835 value = f->setup(f, ctrl);
1836 else {
1837 struct usb_configuration *c;
1839 c = cdev->config;
1840 if (!c)
1841 goto done;
1843 /* try current config's setup */
1844 if (c->setup) {
1845 value = c->setup(c, ctrl);
1846 goto done;
1849 /* try the only function in the current config */
1850 if (!list_is_singular(&c->functions))
1851 goto done;
1852 f = list_first_entry(&c->functions, struct usb_function,
1853 list);
1854 if (f->setup)
1855 value = f->setup(f, ctrl);
1858 goto done;
1861 /* respond with data transfer before status phase? */
1862 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
1863 req->length = value;
1864 req->context = cdev;
1865 req->zero = value < w_length;
1866 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
1867 if (value < 0) {
1868 DBG(cdev, "ep_queue --> %d\n", value);
1869 req->status = 0;
1870 composite_setup_complete(gadget->ep0, req);
1872 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1873 WARN(cdev,
1874 "%s: Delayed status not supported for w_length != 0",
1875 __func__);
1878 done:
1879 /* device either stalls (value < 0) or reports success */
1880 return value;
1883 void composite_disconnect(struct usb_gadget *gadget)
1885 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1886 unsigned long flags;
1888 /* REVISIT: should we have config and device level
1889 * disconnect callbacks?
1891 spin_lock_irqsave(&cdev->lock, flags);
1892 cdev->suspended = 0;
1893 if (cdev->config)
1894 reset_config(cdev);
1895 if (cdev->driver->disconnect)
1896 cdev->driver->disconnect(cdev);
1897 spin_unlock_irqrestore(&cdev->lock, flags);
1900 /*-------------------------------------------------------------------------*/
1902 static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1903 char *buf)
1905 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1906 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1908 return sprintf(buf, "%d\n", cdev->suspended);
1910 static DEVICE_ATTR_RO(suspended);
1912 static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
1914 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1915 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
1916 struct usb_string *dev_str = gstr->strings;
1918 /* composite_disconnect() must already have been called
1919 * by the underlying peripheral controller driver!
1920 * so there's no i/o concurrency that could affect the
1921 * state protected by cdev->lock.
1923 WARN_ON(cdev->config);
1925 while (!list_empty(&cdev->configs)) {
1926 struct usb_configuration *c;
1927 c = list_first_entry(&cdev->configs,
1928 struct usb_configuration, list);
1929 remove_config(cdev, c);
1931 if (cdev->driver->unbind && unbind_driver)
1932 cdev->driver->unbind(cdev);
1934 composite_dev_cleanup(cdev);
1936 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
1937 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
1939 kfree(cdev->def_manufacturer);
1940 kfree(cdev);
1941 set_gadget_data(gadget, NULL);
1944 static void composite_unbind(struct usb_gadget *gadget)
1946 __composite_unbind(gadget, true);
1949 static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
1950 const struct usb_device_descriptor *old)
1952 __le16 idVendor;
1953 __le16 idProduct;
1954 __le16 bcdDevice;
1955 u8 iSerialNumber;
1956 u8 iManufacturer;
1957 u8 iProduct;
1960 * these variables may have been set in
1961 * usb_composite_overwrite_options()
1963 idVendor = new->idVendor;
1964 idProduct = new->idProduct;
1965 bcdDevice = new->bcdDevice;
1966 iSerialNumber = new->iSerialNumber;
1967 iManufacturer = new->iManufacturer;
1968 iProduct = new->iProduct;
1970 *new = *old;
1971 if (idVendor)
1972 new->idVendor = idVendor;
1973 if (idProduct)
1974 new->idProduct = idProduct;
1975 if (bcdDevice)
1976 new->bcdDevice = bcdDevice;
1977 else
1978 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
1979 if (iSerialNumber)
1980 new->iSerialNumber = iSerialNumber;
1981 if (iManufacturer)
1982 new->iManufacturer = iManufacturer;
1983 if (iProduct)
1984 new->iProduct = iProduct;
1987 int composite_dev_prepare(struct usb_composite_driver *composite,
1988 struct usb_composite_dev *cdev)
1990 struct usb_gadget *gadget = cdev->gadget;
1991 int ret = -ENOMEM;
1993 /* preallocate control response and buffer */
1994 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1995 if (!cdev->req)
1996 return -ENOMEM;
1998 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
1999 if (!cdev->req->buf)
2000 goto fail;
2002 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2003 if (ret)
2004 goto fail_dev;
2006 cdev->req->complete = composite_setup_complete;
2007 cdev->req->context = cdev;
2008 gadget->ep0->driver_data = cdev;
2010 cdev->driver = composite;
2013 * As per USB compliance update, a device that is actively drawing
2014 * more than 100mA from USB must report itself as bus-powered in
2015 * the GetStatus(DEVICE) call.
2017 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2018 usb_gadget_set_selfpowered(gadget);
2020 /* interface and string IDs start at zero via kzalloc.
2021 * we force endpoints to start unassigned; few controller
2022 * drivers will zero ep->driver_data.
2024 usb_ep_autoconfig_reset(gadget);
2025 return 0;
2026 fail_dev:
2027 kfree(cdev->req->buf);
2028 fail:
2029 usb_ep_free_request(gadget->ep0, cdev->req);
2030 cdev->req = NULL;
2031 return ret;
2034 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2035 struct usb_ep *ep0)
2037 int ret = 0;
2039 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2040 if (!cdev->os_desc_req) {
2041 ret = PTR_ERR(cdev->os_desc_req);
2042 goto end;
2045 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
2046 GFP_KERNEL);
2047 if (!cdev->os_desc_req->buf) {
2048 ret = PTR_ERR(cdev->os_desc_req->buf);
2049 kfree(cdev->os_desc_req);
2050 goto end;
2052 cdev->os_desc_req->context = cdev;
2053 cdev->os_desc_req->complete = composite_setup_complete;
2054 end:
2055 return ret;
2058 void composite_dev_cleanup(struct usb_composite_dev *cdev)
2060 struct usb_gadget_string_container *uc, *tmp;
2062 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2063 list_del(&uc->list);
2064 kfree(uc);
2066 if (cdev->os_desc_req) {
2067 if (cdev->os_desc_pending)
2068 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2070 kfree(cdev->os_desc_req->buf);
2071 cdev->os_desc_req->buf = NULL;
2072 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2073 cdev->os_desc_req = NULL;
2075 if (cdev->req) {
2076 if (cdev->setup_pending)
2077 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2079 kfree(cdev->req->buf);
2080 cdev->req->buf = NULL;
2081 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2082 cdev->req = NULL;
2084 cdev->next_string_id = 0;
2085 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
2088 static int composite_bind(struct usb_gadget *gadget,
2089 struct usb_gadget_driver *gdriver)
2091 struct usb_composite_dev *cdev;
2092 struct usb_composite_driver *composite = to_cdriver(gdriver);
2093 int status = -ENOMEM;
2095 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2096 if (!cdev)
2097 return status;
2099 spin_lock_init(&cdev->lock);
2100 cdev->gadget = gadget;
2101 set_gadget_data(gadget, cdev);
2102 INIT_LIST_HEAD(&cdev->configs);
2103 INIT_LIST_HEAD(&cdev->gstrings);
2105 status = composite_dev_prepare(composite, cdev);
2106 if (status)
2107 goto fail;
2109 /* composite gadget needs to assign strings for whole device (like
2110 * serial number), register function drivers, potentially update
2111 * power state and consumption, etc
2113 status = composite->bind(cdev);
2114 if (status < 0)
2115 goto fail;
2117 if (cdev->use_os_string) {
2118 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2119 if (status)
2120 goto fail;
2123 update_unchanged_dev_desc(&cdev->desc, composite->dev);
2125 /* has userspace failed to provide a serial number? */
2126 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2127 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2129 INFO(cdev, "%s ready\n", composite->name);
2130 return 0;
2132 fail:
2133 __composite_unbind(gadget, false);
2134 return status;
2137 /*-------------------------------------------------------------------------*/
2139 void composite_suspend(struct usb_gadget *gadget)
2141 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2142 struct usb_function *f;
2144 /* REVISIT: should we have config level
2145 * suspend/resume callbacks?
2147 DBG(cdev, "suspend\n");
2148 if (cdev->config) {
2149 list_for_each_entry(f, &cdev->config->functions, list) {
2150 if (f->suspend)
2151 f->suspend(f);
2154 if (cdev->driver->suspend)
2155 cdev->driver->suspend(cdev);
2157 cdev->suspended = 1;
2159 usb_gadget_vbus_draw(gadget, 2);
2162 void composite_resume(struct usb_gadget *gadget)
2164 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2165 struct usb_function *f;
2166 u16 maxpower;
2168 /* REVISIT: should we have config level
2169 * suspend/resume callbacks?
2171 DBG(cdev, "resume\n");
2172 if (cdev->driver->resume)
2173 cdev->driver->resume(cdev);
2174 if (cdev->config) {
2175 list_for_each_entry(f, &cdev->config->functions, list) {
2176 if (f->resume)
2177 f->resume(f);
2180 maxpower = cdev->config->MaxPower;
2182 usb_gadget_vbus_draw(gadget, maxpower ?
2183 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
2186 cdev->suspended = 0;
2189 /*-------------------------------------------------------------------------*/
2191 static const struct usb_gadget_driver composite_driver_template = {
2192 .bind = composite_bind,
2193 .unbind = composite_unbind,
2195 .setup = composite_setup,
2196 .reset = composite_disconnect,
2197 .disconnect = composite_disconnect,
2199 .suspend = composite_suspend,
2200 .resume = composite_resume,
2202 .driver = {
2203 .owner = THIS_MODULE,
2208 * usb_composite_probe() - register a composite driver
2209 * @driver: the driver to register
2211 * Context: single threaded during gadget setup
2213 * This function is used to register drivers using the composite driver
2214 * framework. The return value is zero, or a negative errno value.
2215 * Those values normally come from the driver's @bind method, which does
2216 * all the work of setting up the driver to match the hardware.
2218 * On successful return, the gadget is ready to respond to requests from
2219 * the host, unless one of its components invokes usb_gadget_disconnect()
2220 * while it was binding. That would usually be done in order to wait for
2221 * some userspace participation.
2223 int usb_composite_probe(struct usb_composite_driver *driver)
2225 struct usb_gadget_driver *gadget_driver;
2227 if (!driver || !driver->dev || !driver->bind)
2228 return -EINVAL;
2230 if (!driver->name)
2231 driver->name = "composite";
2233 driver->gadget_driver = composite_driver_template;
2234 gadget_driver = &driver->gadget_driver;
2236 gadget_driver->function = (char *) driver->name;
2237 gadget_driver->driver.name = driver->name;
2238 gadget_driver->max_speed = driver->max_speed;
2240 return usb_gadget_probe_driver(gadget_driver);
2242 EXPORT_SYMBOL_GPL(usb_composite_probe);
2245 * usb_composite_unregister() - unregister a composite driver
2246 * @driver: the driver to unregister
2248 * This function is used to unregister drivers using the composite
2249 * driver framework.
2251 void usb_composite_unregister(struct usb_composite_driver *driver)
2253 usb_gadget_unregister_driver(&driver->gadget_driver);
2255 EXPORT_SYMBOL_GPL(usb_composite_unregister);
2258 * usb_composite_setup_continue() - Continue with the control transfer
2259 * @cdev: the composite device who's control transfer was kept waiting
2261 * This function must be called by the USB function driver to continue
2262 * with the control transfer's data/status stage in case it had requested to
2263 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2264 * can request the composite framework to delay the setup request's data/status
2265 * stages by returning USB_GADGET_DELAYED_STATUS.
2267 void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2269 int value;
2270 struct usb_request *req = cdev->req;
2271 unsigned long flags;
2273 DBG(cdev, "%s\n", __func__);
2274 spin_lock_irqsave(&cdev->lock, flags);
2276 if (cdev->delayed_status == 0) {
2277 WARN(cdev, "%s: Unexpected call\n", __func__);
2279 } else if (--cdev->delayed_status == 0) {
2280 DBG(cdev, "%s: Completing delayed status\n", __func__);
2281 req->length = 0;
2282 req->context = cdev;
2283 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
2284 if (value < 0) {
2285 DBG(cdev, "ep_queue --> %d\n", value);
2286 req->status = 0;
2287 composite_setup_complete(cdev->gadget->ep0, req);
2291 spin_unlock_irqrestore(&cdev->lock, flags);
2293 EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
2295 static char *composite_default_mfr(struct usb_gadget *gadget)
2297 char *mfr;
2298 int len;
2300 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2301 init_utsname()->release, gadget->name);
2302 len++;
2303 mfr = kmalloc(len, GFP_KERNEL);
2304 if (!mfr)
2305 return NULL;
2306 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2307 init_utsname()->release, gadget->name);
2308 return mfr;
2311 void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2312 struct usb_composite_overwrite *covr)
2314 struct usb_device_descriptor *desc = &cdev->desc;
2315 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2316 struct usb_string *dev_str = gstr->strings;
2318 if (covr->idVendor)
2319 desc->idVendor = cpu_to_le16(covr->idVendor);
2321 if (covr->idProduct)
2322 desc->idProduct = cpu_to_le16(covr->idProduct);
2324 if (covr->bcdDevice)
2325 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
2327 if (covr->serial_number) {
2328 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2329 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2331 if (covr->manufacturer) {
2332 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2333 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
2335 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2336 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2337 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2338 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
2341 if (covr->product) {
2342 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2343 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2346 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
2348 MODULE_LICENSE("GPL");
2349 MODULE_AUTHOR("David Brownell");