mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / usb / gadget / composite.c
blobeb648485a58c571001c4e31b7c79aadaaee04445
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 * function_descriptors() - get function descriptors for speed
58 * @f: the function
59 * @speed: the speed
61 * Returns the descriptors or NULL if not set.
63 static struct usb_descriptor_header **
64 function_descriptors(struct usb_function *f,
65 enum usb_device_speed speed)
67 struct usb_descriptor_header **descriptors;
70 * NOTE: we try to help gadget drivers which might not be setting
71 * max_speed appropriately.
74 switch (speed) {
75 case USB_SPEED_SUPER_PLUS:
76 descriptors = f->ssp_descriptors;
77 if (descriptors)
78 break;
79 /* FALLTHROUGH */
80 case USB_SPEED_SUPER:
81 descriptors = f->ss_descriptors;
82 if (descriptors)
83 break;
84 /* FALLTHROUGH */
85 case USB_SPEED_HIGH:
86 descriptors = f->hs_descriptors;
87 if (descriptors)
88 break;
89 /* FALLTHROUGH */
90 default:
91 descriptors = f->fs_descriptors;
95 * if we can't find any descriptors at all, then this gadget deserves to
96 * Oops with a NULL pointer dereference
99 return descriptors;
103 * next_ep_desc() - advance to the next EP descriptor
104 * @t: currect pointer within descriptor array
106 * Return: next EP descriptor or NULL
108 * Iterate over @t until either EP descriptor found or
109 * NULL (that indicates end of list) encountered
111 static struct usb_descriptor_header**
112 next_ep_desc(struct usb_descriptor_header **t)
114 for (; *t; t++) {
115 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
116 return t;
118 return NULL;
122 * for_each_ep_desc()- iterate over endpoint descriptors in the
123 * descriptors list
124 * @start: pointer within descriptor array.
125 * @ep_desc: endpoint descriptor to use as the loop cursor
127 #define for_each_ep_desc(start, ep_desc) \
128 for (ep_desc = next_ep_desc(start); \
129 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
132 * config_ep_by_speed() - configures the given endpoint
133 * according to gadget speed.
134 * @g: pointer to the gadget
135 * @f: usb function
136 * @_ep: the endpoint to configure
138 * Return: error code, 0 on success
140 * This function chooses the right descriptors for a given
141 * endpoint according to gadget speed and saves it in the
142 * endpoint desc field. If the endpoint already has a descriptor
143 * assigned to it - overwrites it with currently corresponding
144 * descriptor. The endpoint maxpacket field is updated according
145 * to the chosen descriptor.
146 * Note: the supplied function should hold all the descriptors
147 * for supported speeds
149 int config_ep_by_speed(struct usb_gadget *g,
150 struct usb_function *f,
151 struct usb_ep *_ep)
153 struct usb_composite_dev *cdev = get_gadget_data(g);
154 struct usb_endpoint_descriptor *chosen_desc = NULL;
155 struct usb_descriptor_header **speed_desc = NULL;
157 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
158 int want_comp_desc = 0;
160 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
162 if (!g || !f || !_ep)
163 return -EIO;
165 /* select desired speed */
166 switch (g->speed) {
167 case USB_SPEED_SUPER_PLUS:
168 if (gadget_is_superspeed_plus(g)) {
169 speed_desc = f->ssp_descriptors;
170 want_comp_desc = 1;
171 break;
173 /* else: Fall trough */
174 case USB_SPEED_SUPER:
175 if (gadget_is_superspeed(g)) {
176 speed_desc = f->ss_descriptors;
177 want_comp_desc = 1;
178 break;
180 /* else: Fall trough */
181 case USB_SPEED_HIGH:
182 if (gadget_is_dualspeed(g)) {
183 speed_desc = f->hs_descriptors;
184 break;
186 /* else: fall through */
187 default:
188 speed_desc = f->fs_descriptors;
190 /* find descriptors */
191 for_each_ep_desc(speed_desc, d_spd) {
192 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
193 if (chosen_desc->bEndpointAddress == _ep->address)
194 goto ep_found;
196 return -EIO;
198 ep_found:
199 /* commit results */
200 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
201 _ep->desc = chosen_desc;
202 _ep->comp_desc = NULL;
203 _ep->maxburst = 0;
204 _ep->mult = 0;
205 if (!want_comp_desc)
206 return 0;
209 * Companion descriptor should follow EP descriptor
210 * USB 3.0 spec, #9.6.7
212 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
213 if (!comp_desc ||
214 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
215 return -EIO;
216 _ep->comp_desc = comp_desc;
217 if (g->speed >= USB_SPEED_SUPER) {
218 switch (usb_endpoint_type(_ep->desc)) {
219 case USB_ENDPOINT_XFER_ISOC:
220 /* mult: bits 1:0 of bmAttributes */
221 _ep->mult = comp_desc->bmAttributes & 0x3;
222 case USB_ENDPOINT_XFER_BULK:
223 case USB_ENDPOINT_XFER_INT:
224 _ep->maxburst = comp_desc->bMaxBurst + 1;
225 break;
226 default:
227 if (comp_desc->bMaxBurst != 0)
228 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
229 _ep->maxburst = 1;
230 break;
233 return 0;
235 EXPORT_SYMBOL_GPL(config_ep_by_speed);
238 * usb_add_function() - add a function to a configuration
239 * @config: the configuration
240 * @function: the function being added
241 * Context: single threaded during gadget setup
243 * After initialization, each configuration must have one or more
244 * functions added to it. Adding a function involves calling its @bind()
245 * method to allocate resources such as interface and string identifiers
246 * and endpoints.
248 * This function returns the value of the function's bind(), which is
249 * zero for success else a negative errno value.
251 int usb_add_function(struct usb_configuration *config,
252 struct usb_function *function)
254 int value = -EINVAL;
256 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
257 function->name, function,
258 config->label, config);
260 if (!function->set_alt || !function->disable)
261 goto done;
263 function->config = config;
264 list_add_tail(&function->list, &config->functions);
266 if (function->bind_deactivated) {
267 value = usb_function_deactivate(function);
268 if (value)
269 goto done;
272 /* REVISIT *require* function->bind? */
273 if (function->bind) {
274 value = function->bind(config, function);
275 if (value < 0) {
276 list_del(&function->list);
277 function->config = NULL;
279 } else
280 value = 0;
282 /* We allow configurations that don't work at both speeds.
283 * If we run into a lowspeed Linux system, treat it the same
284 * as full speed ... it's the function drivers that will need
285 * to avoid bulk and ISO transfers.
287 if (!config->fullspeed && function->fs_descriptors)
288 config->fullspeed = true;
289 if (!config->highspeed && function->hs_descriptors)
290 config->highspeed = true;
291 if (!config->superspeed && function->ss_descriptors)
292 config->superspeed = true;
293 if (!config->superspeed_plus && function->ssp_descriptors)
294 config->superspeed_plus = true;
296 done:
297 if (value)
298 DBG(config->cdev, "adding '%s'/%p --> %d\n",
299 function->name, function, value);
300 return value;
302 EXPORT_SYMBOL_GPL(usb_add_function);
304 void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
306 if (f->disable)
307 f->disable(f);
309 bitmap_zero(f->endpoints, 32);
310 list_del(&f->list);
311 if (f->unbind)
312 f->unbind(c, f);
314 EXPORT_SYMBOL_GPL(usb_remove_function);
317 * usb_function_deactivate - prevent function and gadget enumeration
318 * @function: the function that isn't yet ready to respond
320 * Blocks response of the gadget driver to host enumeration by
321 * preventing the data line pullup from being activated. This is
322 * normally called during @bind() processing to change from the
323 * initial "ready to respond" state, or when a required resource
324 * becomes available.
326 * For example, drivers that serve as a passthrough to a userspace
327 * daemon can block enumeration unless that daemon (such as an OBEX,
328 * MTP, or print server) is ready to handle host requests.
330 * Not all systems support software control of their USB peripheral
331 * data pullups.
333 * Returns zero on success, else negative errno.
335 int usb_function_deactivate(struct usb_function *function)
337 struct usb_composite_dev *cdev = function->config->cdev;
338 unsigned long flags;
339 int status = 0;
341 spin_lock_irqsave(&cdev->lock, flags);
343 if (cdev->deactivations == 0)
344 status = usb_gadget_deactivate(cdev->gadget);
345 if (status == 0)
346 cdev->deactivations++;
348 spin_unlock_irqrestore(&cdev->lock, flags);
349 return status;
351 EXPORT_SYMBOL_GPL(usb_function_deactivate);
354 * usb_function_activate - allow function and gadget enumeration
355 * @function: function on which usb_function_activate() was called
357 * Reverses effect of usb_function_deactivate(). If no more functions
358 * are delaying their activation, the gadget driver will respond to
359 * host enumeration procedures.
361 * Returns zero on success, else negative errno.
363 int usb_function_activate(struct usb_function *function)
365 struct usb_composite_dev *cdev = function->config->cdev;
366 unsigned long flags;
367 int status = 0;
369 spin_lock_irqsave(&cdev->lock, flags);
371 if (WARN_ON(cdev->deactivations == 0))
372 status = -EINVAL;
373 else {
374 cdev->deactivations--;
375 if (cdev->deactivations == 0)
376 status = usb_gadget_activate(cdev->gadget);
379 spin_unlock_irqrestore(&cdev->lock, flags);
380 return status;
382 EXPORT_SYMBOL_GPL(usb_function_activate);
385 * usb_interface_id() - allocate an unused interface ID
386 * @config: configuration associated with the interface
387 * @function: function handling the interface
388 * Context: single threaded during gadget setup
390 * usb_interface_id() is called from usb_function.bind() callbacks to
391 * allocate new interface IDs. The function driver will then store that
392 * ID in interface, association, CDC union, and other descriptors. It
393 * will also handle any control requests targeted at that interface,
394 * particularly changing its altsetting via set_alt(). There may
395 * also be class-specific or vendor-specific requests to handle.
397 * All interface identifier should be allocated using this routine, to
398 * ensure that for example different functions don't wrongly assign
399 * different meanings to the same identifier. Note that since interface
400 * identifiers are configuration-specific, functions used in more than
401 * one configuration (or more than once in a given configuration) need
402 * multiple versions of the relevant descriptors.
404 * Returns the interface ID which was allocated; or -ENODEV if no
405 * more interface IDs can be allocated.
407 int usb_interface_id(struct usb_configuration *config,
408 struct usb_function *function)
410 unsigned id = config->next_interface_id;
412 if (id < MAX_CONFIG_INTERFACES) {
413 config->interface[id] = function;
414 config->next_interface_id = id + 1;
415 return id;
417 return -ENODEV;
419 EXPORT_SYMBOL_GPL(usb_interface_id);
421 static u8 encode_bMaxPower(enum usb_device_speed speed,
422 struct usb_configuration *c)
424 unsigned val;
426 if (c->MaxPower)
427 val = c->MaxPower;
428 else
429 val = CONFIG_USB_GADGET_VBUS_DRAW;
430 if (!val)
431 return 0;
432 switch (speed) {
433 case USB_SPEED_SUPER:
434 return DIV_ROUND_UP(val, 8);
435 default:
436 return DIV_ROUND_UP(val, 2);
440 static int config_buf(struct usb_configuration *config,
441 enum usb_device_speed speed, void *buf, u8 type)
443 struct usb_config_descriptor *c = buf;
444 void *next = buf + USB_DT_CONFIG_SIZE;
445 int len;
446 struct usb_function *f;
447 int status;
449 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
450 /* write the config descriptor */
451 c = buf;
452 c->bLength = USB_DT_CONFIG_SIZE;
453 c->bDescriptorType = type;
454 /* wTotalLength is written later */
455 c->bNumInterfaces = config->next_interface_id;
456 c->bConfigurationValue = config->bConfigurationValue;
457 c->iConfiguration = config->iConfiguration;
458 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
459 c->bMaxPower = encode_bMaxPower(speed, config);
461 /* There may be e.g. OTG descriptors */
462 if (config->descriptors) {
463 status = usb_descriptor_fillbuf(next, len,
464 config->descriptors);
465 if (status < 0)
466 return status;
467 len -= status;
468 next += status;
471 /* add each function's descriptors */
472 list_for_each_entry(f, &config->functions, list) {
473 struct usb_descriptor_header **descriptors;
475 descriptors = function_descriptors(f, speed);
476 if (!descriptors)
477 continue;
478 status = usb_descriptor_fillbuf(next, len,
479 (const struct usb_descriptor_header **) descriptors);
480 if (status < 0)
481 return status;
482 len -= status;
483 next += status;
486 len = next - buf;
487 c->wTotalLength = cpu_to_le16(len);
488 return len;
491 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
493 struct usb_gadget *gadget = cdev->gadget;
494 struct usb_configuration *c;
495 struct list_head *pos;
496 u8 type = w_value >> 8;
497 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
499 if (gadget->speed >= USB_SPEED_SUPER)
500 speed = gadget->speed;
501 else if (gadget_is_dualspeed(gadget)) {
502 int hs = 0;
503 if (gadget->speed == USB_SPEED_HIGH)
504 hs = 1;
505 if (type == USB_DT_OTHER_SPEED_CONFIG)
506 hs = !hs;
507 if (hs)
508 speed = USB_SPEED_HIGH;
512 /* This is a lookup by config *INDEX* */
513 w_value &= 0xff;
515 pos = &cdev->configs;
516 c = cdev->os_desc_config;
517 if (c)
518 goto check_config;
520 while ((pos = pos->next) != &cdev->configs) {
521 c = list_entry(pos, typeof(*c), list);
523 /* skip OS Descriptors config which is handled separately */
524 if (c == cdev->os_desc_config)
525 continue;
527 check_config:
528 /* ignore configs that won't work at this speed */
529 switch (speed) {
530 case USB_SPEED_SUPER_PLUS:
531 if (!c->superspeed_plus)
532 continue;
533 break;
534 case USB_SPEED_SUPER:
535 if (!c->superspeed)
536 continue;
537 break;
538 case USB_SPEED_HIGH:
539 if (!c->highspeed)
540 continue;
541 break;
542 default:
543 if (!c->fullspeed)
544 continue;
547 if (w_value == 0)
548 return config_buf(c, speed, cdev->req->buf, type);
549 w_value--;
551 return -EINVAL;
554 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
556 struct usb_gadget *gadget = cdev->gadget;
557 struct usb_configuration *c;
558 unsigned count = 0;
559 int hs = 0;
560 int ss = 0;
561 int ssp = 0;
563 if (gadget_is_dualspeed(gadget)) {
564 if (gadget->speed == USB_SPEED_HIGH)
565 hs = 1;
566 if (gadget->speed == USB_SPEED_SUPER)
567 ss = 1;
568 if (gadget->speed == USB_SPEED_SUPER_PLUS)
569 ssp = 1;
570 if (type == USB_DT_DEVICE_QUALIFIER)
571 hs = !hs;
573 list_for_each_entry(c, &cdev->configs, list) {
574 /* ignore configs that won't work at this speed */
575 if (ssp) {
576 if (!c->superspeed_plus)
577 continue;
578 } else if (ss) {
579 if (!c->superspeed)
580 continue;
581 } else if (hs) {
582 if (!c->highspeed)
583 continue;
584 } else {
585 if (!c->fullspeed)
586 continue;
588 count++;
590 return count;
594 * bos_desc() - prepares the BOS descriptor.
595 * @cdev: pointer to usb_composite device to generate the bos
596 * descriptor for
598 * This function generates the BOS (Binary Device Object)
599 * descriptor and its device capabilities descriptors. The BOS
600 * descriptor should be supported by a SuperSpeed device.
602 static int bos_desc(struct usb_composite_dev *cdev)
604 struct usb_ext_cap_descriptor *usb_ext;
605 struct usb_ss_cap_descriptor *ss_cap;
606 struct usb_dcd_config_params dcd_config_params;
607 struct usb_bos_descriptor *bos = cdev->req->buf;
609 bos->bLength = USB_DT_BOS_SIZE;
610 bos->bDescriptorType = USB_DT_BOS;
612 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
613 bos->bNumDeviceCaps = 0;
616 * A SuperSpeed device shall include the USB2.0 extension descriptor
617 * and shall support LPM when operating in USB2.0 HS mode.
619 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
620 bos->bNumDeviceCaps++;
621 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
622 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
623 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
624 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
625 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
628 * The Superspeed USB Capability descriptor shall be implemented by all
629 * SuperSpeed devices.
631 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
632 bos->bNumDeviceCaps++;
633 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
634 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
635 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
636 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
637 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
638 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
639 USB_FULL_SPEED_OPERATION |
640 USB_HIGH_SPEED_OPERATION |
641 USB_5GBPS_OPERATION);
642 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
644 /* Get Controller configuration */
645 if (cdev->gadget->ops->get_config_params)
646 cdev->gadget->ops->get_config_params(&dcd_config_params);
647 else {
648 dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
649 dcd_config_params.bU2DevExitLat =
650 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
652 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
653 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
655 /* The SuperSpeedPlus USB Device Capability descriptor */
656 if (gadget_is_superspeed_plus(cdev->gadget)) {
657 struct usb_ssp_cap_descriptor *ssp_cap;
659 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
660 bos->bNumDeviceCaps++;
663 * Report typical values.
666 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
667 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
668 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
669 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
670 ssp_cap->bReserved = 0;
671 ssp_cap->wReserved = 0;
673 /* SSAC = 1 (2 attributes) */
674 ssp_cap->bmAttributes = cpu_to_le32(1);
676 /* Min RX/TX Lane Count = 1 */
677 ssp_cap->wFunctionalitySupport =
678 cpu_to_le16((1 << 8) | (1 << 12));
681 * bmSublinkSpeedAttr[0]:
682 * ST = Symmetric, RX
683 * LSE = 3 (Gbps)
684 * LP = 1 (SuperSpeedPlus)
685 * LSM = 10 (10 Gbps)
687 ssp_cap->bmSublinkSpeedAttr[0] =
688 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
690 * bmSublinkSpeedAttr[1] =
691 * ST = Symmetric, TX
692 * LSE = 3 (Gbps)
693 * LP = 1 (SuperSpeedPlus)
694 * LSM = 10 (10 Gbps)
696 ssp_cap->bmSublinkSpeedAttr[1] =
697 cpu_to_le32((3 << 4) | (1 << 14) |
698 (0xa << 16) | (1 << 7));
701 return le16_to_cpu(bos->wTotalLength);
704 static void device_qual(struct usb_composite_dev *cdev)
706 struct usb_qualifier_descriptor *qual = cdev->req->buf;
708 qual->bLength = sizeof(*qual);
709 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
710 /* POLICY: same bcdUSB and device type info at both speeds */
711 qual->bcdUSB = cdev->desc.bcdUSB;
712 qual->bDeviceClass = cdev->desc.bDeviceClass;
713 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
714 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
715 /* ASSUME same EP0 fifo size at both speeds */
716 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
717 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
718 qual->bRESERVED = 0;
721 /*-------------------------------------------------------------------------*/
723 static void reset_config(struct usb_composite_dev *cdev)
725 struct usb_function *f;
727 DBG(cdev, "reset config\n");
729 list_for_each_entry(f, &cdev->config->functions, list) {
730 if (f->disable)
731 f->disable(f);
733 bitmap_zero(f->endpoints, 32);
735 cdev->config = NULL;
736 cdev->delayed_status = 0;
739 static int set_config(struct usb_composite_dev *cdev,
740 const struct usb_ctrlrequest *ctrl, unsigned number)
742 struct usb_gadget *gadget = cdev->gadget;
743 struct usb_configuration *c = NULL;
744 int result = -EINVAL;
745 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
746 int tmp;
748 if (number) {
749 list_for_each_entry(c, &cdev->configs, list) {
750 if (c->bConfigurationValue == number) {
752 * We disable the FDs of the previous
753 * configuration only if the new configuration
754 * is a valid one
756 if (cdev->config)
757 reset_config(cdev);
758 result = 0;
759 break;
762 if (result < 0)
763 goto done;
764 } else { /* Zero configuration value - need to reset the config */
765 if (cdev->config)
766 reset_config(cdev);
767 result = 0;
770 INFO(cdev, "%s config #%d: %s\n",
771 usb_speed_string(gadget->speed),
772 number, c ? c->label : "unconfigured");
774 if (!c)
775 goto done;
777 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
778 cdev->config = c;
780 /* Initialize all interfaces by setting them to altsetting zero. */
781 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
782 struct usb_function *f = c->interface[tmp];
783 struct usb_descriptor_header **descriptors;
785 if (!f)
786 break;
789 * Record which endpoints are used by the function. This is used
790 * to dispatch control requests targeted at that endpoint to the
791 * function's setup callback instead of the current
792 * configuration's setup callback.
794 descriptors = function_descriptors(f, gadget->speed);
796 for (; *descriptors; ++descriptors) {
797 struct usb_endpoint_descriptor *ep;
798 int addr;
800 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
801 continue;
803 ep = (struct usb_endpoint_descriptor *)*descriptors;
804 addr = ((ep->bEndpointAddress & 0x80) >> 3)
805 | (ep->bEndpointAddress & 0x0f);
806 set_bit(addr, f->endpoints);
809 result = f->set_alt(f, tmp, 0);
810 if (result < 0) {
811 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
812 tmp, f->name, f, result);
814 reset_config(cdev);
815 goto done;
818 if (result == USB_GADGET_DELAYED_STATUS) {
819 DBG(cdev,
820 "%s: interface %d (%s) requested delayed status\n",
821 __func__, tmp, f->name);
822 cdev->delayed_status++;
823 DBG(cdev, "delayed_status count %d\n",
824 cdev->delayed_status);
828 /* when we return, be sure our power usage is valid */
829 power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
830 done:
831 usb_gadget_vbus_draw(gadget, power);
832 if (result >= 0 && cdev->delayed_status)
833 result = USB_GADGET_DELAYED_STATUS;
834 return result;
837 int usb_add_config_only(struct usb_composite_dev *cdev,
838 struct usb_configuration *config)
840 struct usb_configuration *c;
842 if (!config->bConfigurationValue)
843 return -EINVAL;
845 /* Prevent duplicate configuration identifiers */
846 list_for_each_entry(c, &cdev->configs, list) {
847 if (c->bConfigurationValue == config->bConfigurationValue)
848 return -EBUSY;
851 config->cdev = cdev;
852 list_add_tail(&config->list, &cdev->configs);
854 INIT_LIST_HEAD(&config->functions);
855 config->next_interface_id = 0;
856 memset(config->interface, 0, sizeof(config->interface));
858 return 0;
860 EXPORT_SYMBOL_GPL(usb_add_config_only);
863 * usb_add_config() - add a configuration to a device.
864 * @cdev: wraps the USB gadget
865 * @config: the configuration, with bConfigurationValue assigned
866 * @bind: the configuration's bind function
867 * Context: single threaded during gadget setup
869 * One of the main tasks of a composite @bind() routine is to
870 * add each of the configurations it supports, using this routine.
872 * This function returns the value of the configuration's @bind(), which
873 * is zero for success else a negative errno value. Binding configurations
874 * assigns global resources including string IDs, and per-configuration
875 * resources such as interface IDs and endpoints.
877 int usb_add_config(struct usb_composite_dev *cdev,
878 struct usb_configuration *config,
879 int (*bind)(struct usb_configuration *))
881 int status = -EINVAL;
883 if (!bind)
884 goto done;
886 DBG(cdev, "adding config #%u '%s'/%p\n",
887 config->bConfigurationValue,
888 config->label, config);
890 status = usb_add_config_only(cdev, config);
891 if (status)
892 goto done;
894 status = bind(config);
895 if (status < 0) {
896 while (!list_empty(&config->functions)) {
897 struct usb_function *f;
899 f = list_first_entry(&config->functions,
900 struct usb_function, list);
901 list_del(&f->list);
902 if (f->unbind) {
903 DBG(cdev, "unbind function '%s'/%p\n",
904 f->name, f);
905 f->unbind(config, f);
906 /* may free memory for "f" */
909 list_del(&config->list);
910 config->cdev = NULL;
911 } else {
912 unsigned i;
914 DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n",
915 config->bConfigurationValue, config,
916 config->superspeed_plus ? " superplus" : "",
917 config->superspeed ? " super" : "",
918 config->highspeed ? " high" : "",
919 config->fullspeed
920 ? (gadget_is_dualspeed(cdev->gadget)
921 ? " full"
922 : " full/low")
923 : "");
925 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
926 struct usb_function *f = config->interface[i];
928 if (!f)
929 continue;
930 DBG(cdev, " interface %d = %s/%p\n",
931 i, f->name, f);
935 /* set_alt(), or next bind(), sets up ep->claimed as needed */
936 usb_ep_autoconfig_reset(cdev->gadget);
938 done:
939 if (status)
940 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
941 config->bConfigurationValue, status);
942 return status;
944 EXPORT_SYMBOL_GPL(usb_add_config);
946 static void remove_config(struct usb_composite_dev *cdev,
947 struct usb_configuration *config)
949 while (!list_empty(&config->functions)) {
950 struct usb_function *f;
952 f = list_first_entry(&config->functions,
953 struct usb_function, list);
954 list_del(&f->list);
955 if (f->unbind) {
956 DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
957 f->unbind(config, f);
958 /* may free memory for "f" */
961 list_del(&config->list);
962 if (config->unbind) {
963 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
964 config->unbind(config);
965 /* may free memory for "c" */
970 * usb_remove_config() - remove a configuration from a device.
971 * @cdev: wraps the USB gadget
972 * @config: the configuration
974 * Drivers must call usb_gadget_disconnect before calling this function
975 * to disconnect the device from the host and make sure the host will not
976 * try to enumerate the device while we are changing the config list.
978 void usb_remove_config(struct usb_composite_dev *cdev,
979 struct usb_configuration *config)
981 unsigned long flags;
983 spin_lock_irqsave(&cdev->lock, flags);
985 if (cdev->config == config)
986 reset_config(cdev);
988 spin_unlock_irqrestore(&cdev->lock, flags);
990 remove_config(cdev, config);
993 /*-------------------------------------------------------------------------*/
995 /* We support strings in multiple languages ... string descriptor zero
996 * says which languages are supported. The typical case will be that
997 * only one language (probably English) is used, with i18n handled on
998 * the host side.
1001 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1003 const struct usb_gadget_strings *s;
1004 __le16 language;
1005 __le16 *tmp;
1007 while (*sp) {
1008 s = *sp;
1009 language = cpu_to_le16(s->language);
1010 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
1011 if (*tmp == language)
1012 goto repeat;
1014 *tmp++ = language;
1015 repeat:
1016 sp++;
1020 static int lookup_string(
1021 struct usb_gadget_strings **sp,
1022 void *buf,
1023 u16 language,
1024 int id
1027 struct usb_gadget_strings *s;
1028 int value;
1030 while (*sp) {
1031 s = *sp++;
1032 if (s->language != language)
1033 continue;
1034 value = usb_gadget_get_string(s, id, buf);
1035 if (value > 0)
1036 return value;
1038 return -EINVAL;
1041 static int get_string(struct usb_composite_dev *cdev,
1042 void *buf, u16 language, int id)
1044 struct usb_composite_driver *composite = cdev->driver;
1045 struct usb_gadget_string_container *uc;
1046 struct usb_configuration *c;
1047 struct usb_function *f;
1048 int len;
1050 /* Yes, not only is USB's i18n support probably more than most
1051 * folk will ever care about ... also, it's all supported here.
1052 * (Except for UTF8 support for Unicode's "Astral Planes".)
1055 /* 0 == report all available language codes */
1056 if (id == 0) {
1057 struct usb_string_descriptor *s = buf;
1058 struct usb_gadget_strings **sp;
1060 memset(s, 0, 256);
1061 s->bDescriptorType = USB_DT_STRING;
1063 sp = composite->strings;
1064 if (sp)
1065 collect_langs(sp, s->wData);
1067 list_for_each_entry(c, &cdev->configs, list) {
1068 sp = c->strings;
1069 if (sp)
1070 collect_langs(sp, s->wData);
1072 list_for_each_entry(f, &c->functions, list) {
1073 sp = f->strings;
1074 if (sp)
1075 collect_langs(sp, s->wData);
1078 list_for_each_entry(uc, &cdev->gstrings, list) {
1079 struct usb_gadget_strings **sp;
1081 sp = get_containers_gs(uc);
1082 collect_langs(sp, s->wData);
1085 for (len = 0; len <= 126 && s->wData[len]; len++)
1086 continue;
1087 if (!len)
1088 return -EINVAL;
1090 s->bLength = 2 * (len + 1);
1091 return s->bLength;
1094 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1095 struct usb_os_string *b = buf;
1096 b->bLength = sizeof(*b);
1097 b->bDescriptorType = USB_DT_STRING;
1098 compiletime_assert(
1099 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1100 "qwSignature size must be equal to qw_sign");
1101 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1102 b->bMS_VendorCode = cdev->b_vendor_code;
1103 b->bPad = 0;
1104 return sizeof(*b);
1107 list_for_each_entry(uc, &cdev->gstrings, list) {
1108 struct usb_gadget_strings **sp;
1110 sp = get_containers_gs(uc);
1111 len = lookup_string(sp, buf, language, id);
1112 if (len > 0)
1113 return len;
1116 /* String IDs are device-scoped, so we look up each string
1117 * table we're told about. These lookups are infrequent;
1118 * simpler-is-better here.
1120 if (composite->strings) {
1121 len = lookup_string(composite->strings, buf, language, id);
1122 if (len > 0)
1123 return len;
1125 list_for_each_entry(c, &cdev->configs, list) {
1126 if (c->strings) {
1127 len = lookup_string(c->strings, buf, language, id);
1128 if (len > 0)
1129 return len;
1131 list_for_each_entry(f, &c->functions, list) {
1132 if (!f->strings)
1133 continue;
1134 len = lookup_string(f->strings, buf, language, id);
1135 if (len > 0)
1136 return len;
1139 return -EINVAL;
1143 * usb_string_id() - allocate an unused string ID
1144 * @cdev: the device whose string descriptor IDs are being allocated
1145 * Context: single threaded during gadget setup
1147 * @usb_string_id() is called from bind() callbacks to allocate
1148 * string IDs. Drivers for functions, configurations, or gadgets will
1149 * then store that ID in the appropriate descriptors and string table.
1151 * All string identifier should be allocated using this,
1152 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1153 * that for example different functions don't wrongly assign different
1154 * meanings to the same identifier.
1156 int usb_string_id(struct usb_composite_dev *cdev)
1158 if (cdev->next_string_id < 254) {
1159 /* string id 0 is reserved by USB spec for list of
1160 * supported languages */
1161 /* 255 reserved as well? -- mina86 */
1162 cdev->next_string_id++;
1163 return cdev->next_string_id;
1165 return -ENODEV;
1167 EXPORT_SYMBOL_GPL(usb_string_id);
1170 * usb_string_ids() - allocate unused string IDs in batch
1171 * @cdev: the device whose string descriptor IDs are being allocated
1172 * @str: an array of usb_string objects to assign numbers to
1173 * Context: single threaded during gadget setup
1175 * @usb_string_ids() is called from bind() callbacks to allocate
1176 * string IDs. Drivers for functions, configurations, or gadgets will
1177 * then copy IDs from the string table to the appropriate descriptors
1178 * and string table for other languages.
1180 * All string identifier should be allocated using this,
1181 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1182 * example different functions don't wrongly assign different meanings
1183 * to the same identifier.
1185 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1187 int next = cdev->next_string_id;
1189 for (; str->s; ++str) {
1190 if (unlikely(next >= 254))
1191 return -ENODEV;
1192 str->id = ++next;
1195 cdev->next_string_id = next;
1197 return 0;
1199 EXPORT_SYMBOL_GPL(usb_string_ids_tab);
1201 static struct usb_gadget_string_container *copy_gadget_strings(
1202 struct usb_gadget_strings **sp, unsigned n_gstrings,
1203 unsigned n_strings)
1205 struct usb_gadget_string_container *uc;
1206 struct usb_gadget_strings **gs_array;
1207 struct usb_gadget_strings *gs;
1208 struct usb_string *s;
1209 unsigned mem;
1210 unsigned n_gs;
1211 unsigned n_s;
1212 void *stash;
1214 mem = sizeof(*uc);
1215 mem += sizeof(void *) * (n_gstrings + 1);
1216 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1217 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1218 uc = kmalloc(mem, GFP_KERNEL);
1219 if (!uc)
1220 return ERR_PTR(-ENOMEM);
1221 gs_array = get_containers_gs(uc);
1222 stash = uc->stash;
1223 stash += sizeof(void *) * (n_gstrings + 1);
1224 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1225 struct usb_string *org_s;
1227 gs_array[n_gs] = stash;
1228 gs = gs_array[n_gs];
1229 stash += sizeof(struct usb_gadget_strings);
1230 gs->language = sp[n_gs]->language;
1231 gs->strings = stash;
1232 org_s = sp[n_gs]->strings;
1234 for (n_s = 0; n_s < n_strings; n_s++) {
1235 s = stash;
1236 stash += sizeof(struct usb_string);
1237 if (org_s->s)
1238 s->s = org_s->s;
1239 else
1240 s->s = "";
1241 org_s++;
1243 s = stash;
1244 s->s = NULL;
1245 stash += sizeof(struct usb_string);
1248 gs_array[n_gs] = NULL;
1249 return uc;
1253 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1254 * @cdev: the device whose string descriptor IDs are being allocated
1255 * and attached.
1256 * @sp: an array of usb_gadget_strings to attach.
1257 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1259 * This function will create a deep copy of usb_gadget_strings and usb_string
1260 * and attach it to the cdev. The actual string (usb_string.s) will not be
1261 * copied but only a referenced will be made. The struct usb_gadget_strings
1262 * array may contain multiple languages and should be NULL terminated.
1263 * The ->language pointer of each struct usb_gadget_strings has to contain the
1264 * same amount of entries.
1265 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1266 * usb_string entry of es-ES contains the translation of the first usb_string
1267 * entry of en-US. Therefore both entries become the same id assign.
1269 struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1270 struct usb_gadget_strings **sp, unsigned n_strings)
1272 struct usb_gadget_string_container *uc;
1273 struct usb_gadget_strings **n_gs;
1274 unsigned n_gstrings = 0;
1275 unsigned i;
1276 int ret;
1278 for (i = 0; sp[i]; i++)
1279 n_gstrings++;
1281 if (!n_gstrings)
1282 return ERR_PTR(-EINVAL);
1284 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1285 if (IS_ERR(uc))
1286 return ERR_CAST(uc);
1288 n_gs = get_containers_gs(uc);
1289 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1290 if (ret)
1291 goto err;
1293 for (i = 1; i < n_gstrings; i++) {
1294 struct usb_string *m_s;
1295 struct usb_string *s;
1296 unsigned n;
1298 m_s = n_gs[0]->strings;
1299 s = n_gs[i]->strings;
1300 for (n = 0; n < n_strings; n++) {
1301 s->id = m_s->id;
1302 s++;
1303 m_s++;
1306 list_add_tail(&uc->list, &cdev->gstrings);
1307 return n_gs[0]->strings;
1308 err:
1309 kfree(uc);
1310 return ERR_PTR(ret);
1312 EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1315 * usb_string_ids_n() - allocate unused string IDs in batch
1316 * @c: the device whose string descriptor IDs are being allocated
1317 * @n: number of string IDs to allocate
1318 * Context: single threaded during gadget setup
1320 * Returns the first requested ID. This ID and next @n-1 IDs are now
1321 * valid IDs. At least provided that @n is non-zero because if it
1322 * is, returns last requested ID which is now very useful information.
1324 * @usb_string_ids_n() is called from bind() callbacks to allocate
1325 * string IDs. Drivers for functions, configurations, or gadgets will
1326 * then store that ID in the appropriate descriptors and string table.
1328 * All string identifier should be allocated using this,
1329 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1330 * example different functions don't wrongly assign different meanings
1331 * to the same identifier.
1333 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1335 unsigned next = c->next_string_id;
1336 if (unlikely(n > 254 || (unsigned)next + n > 254))
1337 return -ENODEV;
1338 c->next_string_id += n;
1339 return next + 1;
1341 EXPORT_SYMBOL_GPL(usb_string_ids_n);
1343 /*-------------------------------------------------------------------------*/
1345 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1347 struct usb_composite_dev *cdev;
1349 if (req->status || req->actual != req->length)
1350 DBG((struct usb_composite_dev *) ep->driver_data,
1351 "setup complete --> %d, %d/%d\n",
1352 req->status, req->actual, req->length);
1355 * REVIST The same ep0 requests are shared with function drivers
1356 * so they don't have to maintain the same ->complete() stubs.
1358 * Because of that, we need to check for the validity of ->context
1359 * here, even though we know we've set it to something useful.
1361 if (!req->context)
1362 return;
1364 cdev = req->context;
1366 if (cdev->req == req)
1367 cdev->setup_pending = false;
1368 else if (cdev->os_desc_req == req)
1369 cdev->os_desc_pending = false;
1370 else
1371 WARN(1, "unknown request %p\n", req);
1374 static int composite_ep0_queue(struct usb_composite_dev *cdev,
1375 struct usb_request *req, gfp_t gfp_flags)
1377 int ret;
1379 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1380 if (ret == 0) {
1381 if (cdev->req == req)
1382 cdev->setup_pending = true;
1383 else if (cdev->os_desc_req == req)
1384 cdev->os_desc_pending = true;
1385 else
1386 WARN(1, "unknown request %p\n", req);
1389 return ret;
1392 static int count_ext_compat(struct usb_configuration *c)
1394 int i, res;
1396 res = 0;
1397 for (i = 0; i < c->next_interface_id; ++i) {
1398 struct usb_function *f;
1399 int j;
1401 f = c->interface[i];
1402 for (j = 0; j < f->os_desc_n; ++j) {
1403 struct usb_os_desc *d;
1405 if (i != f->os_desc_table[j].if_id)
1406 continue;
1407 d = f->os_desc_table[j].os_desc;
1408 if (d && d->ext_compat_id)
1409 ++res;
1412 BUG_ON(res > 255);
1413 return res;
1416 static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
1418 int i, count;
1420 count = 16;
1421 for (i = 0; i < c->next_interface_id; ++i) {
1422 struct usb_function *f;
1423 int j;
1425 f = c->interface[i];
1426 for (j = 0; j < f->os_desc_n; ++j) {
1427 struct usb_os_desc *d;
1429 if (i != f->os_desc_table[j].if_id)
1430 continue;
1431 d = f->os_desc_table[j].os_desc;
1432 if (d && d->ext_compat_id) {
1433 *buf++ = i;
1434 *buf++ = 0x01;
1435 memcpy(buf, d->ext_compat_id, 16);
1436 buf += 22;
1437 } else {
1438 ++buf;
1439 *buf = 0x01;
1440 buf += 23;
1442 count += 24;
1443 if (count >= 4096)
1444 return;
1449 static int count_ext_prop(struct usb_configuration *c, int interface)
1451 struct usb_function *f;
1452 int j;
1454 f = c->interface[interface];
1455 for (j = 0; j < f->os_desc_n; ++j) {
1456 struct usb_os_desc *d;
1458 if (interface != f->os_desc_table[j].if_id)
1459 continue;
1460 d = f->os_desc_table[j].os_desc;
1461 if (d && d->ext_compat_id)
1462 return d->ext_prop_count;
1464 return 0;
1467 static int len_ext_prop(struct usb_configuration *c, int interface)
1469 struct usb_function *f;
1470 struct usb_os_desc *d;
1471 int j, res;
1473 res = 10; /* header length */
1474 f = c->interface[interface];
1475 for (j = 0; j < f->os_desc_n; ++j) {
1476 if (interface != f->os_desc_table[j].if_id)
1477 continue;
1478 d = f->os_desc_table[j].os_desc;
1479 if (d)
1480 return min(res + d->ext_prop_len, 4096);
1482 return res;
1485 static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1487 struct usb_function *f;
1488 struct usb_os_desc *d;
1489 struct usb_os_desc_ext_prop *ext_prop;
1490 int j, count, n, ret;
1491 u8 *start = buf;
1493 f = c->interface[interface];
1494 for (j = 0; j < f->os_desc_n; ++j) {
1495 if (interface != f->os_desc_table[j].if_id)
1496 continue;
1497 d = f->os_desc_table[j].os_desc;
1498 if (d)
1499 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1500 /* 4kB minus header length */
1501 n = buf - start;
1502 if (n >= 4086)
1503 return 0;
1505 count = ext_prop->data_len +
1506 ext_prop->name_len + 14;
1507 if (count > 4086 - n)
1508 return -EINVAL;
1509 usb_ext_prop_put_size(buf, count);
1510 usb_ext_prop_put_type(buf, ext_prop->type);
1511 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1512 ext_prop->name_len);
1513 if (ret < 0)
1514 return ret;
1515 switch (ext_prop->type) {
1516 case USB_EXT_PROP_UNICODE:
1517 case USB_EXT_PROP_UNICODE_ENV:
1518 case USB_EXT_PROP_UNICODE_LINK:
1519 usb_ext_prop_put_unicode(buf, ret,
1520 ext_prop->data,
1521 ext_prop->data_len);
1522 break;
1523 case USB_EXT_PROP_BINARY:
1524 usb_ext_prop_put_binary(buf, ret,
1525 ext_prop->data,
1526 ext_prop->data_len);
1527 break;
1528 case USB_EXT_PROP_LE32:
1529 /* not implemented */
1530 case USB_EXT_PROP_BE32:
1531 /* not implemented */
1532 default:
1533 return -EINVAL;
1535 buf += count;
1539 return 0;
1543 * The setup() callback implements all the ep0 functionality that's
1544 * not handled lower down, in hardware or the hardware driver(like
1545 * device and endpoint feature flags, and their status). It's all
1546 * housekeeping for the gadget function we're implementing. Most of
1547 * the work is in config and function specific setup.
1550 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1552 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1553 struct usb_request *req = cdev->req;
1554 int value = -EOPNOTSUPP;
1555 int status = 0;
1556 u16 w_index = le16_to_cpu(ctrl->wIndex);
1557 u8 intf = w_index & 0xFF;
1558 u16 w_value = le16_to_cpu(ctrl->wValue);
1559 u16 w_length = le16_to_cpu(ctrl->wLength);
1560 struct usb_function *f = NULL;
1561 u8 endp;
1563 /* partial re-init of the response message; the function or the
1564 * gadget might need to intercept e.g. a control-OUT completion
1565 * when we delegate to it.
1567 req->zero = 0;
1568 req->context = cdev;
1569 req->complete = composite_setup_complete;
1570 req->length = 0;
1571 gadget->ep0->driver_data = cdev;
1574 * Don't let non-standard requests match any of the cases below
1575 * by accident.
1577 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1578 goto unknown;
1580 switch (ctrl->bRequest) {
1582 /* we handle all standard USB descriptors */
1583 case USB_REQ_GET_DESCRIPTOR:
1584 if (ctrl->bRequestType != USB_DIR_IN)
1585 goto unknown;
1586 switch (w_value >> 8) {
1588 case USB_DT_DEVICE:
1589 cdev->desc.bNumConfigurations =
1590 count_configs(cdev, USB_DT_DEVICE);
1591 cdev->desc.bMaxPacketSize0 =
1592 cdev->gadget->ep0->maxpacket;
1593 if (gadget_is_superspeed(gadget)) {
1594 if (gadget->speed >= USB_SPEED_SUPER) {
1595 cdev->desc.bcdUSB = cpu_to_le16(0x0310);
1596 cdev->desc.bMaxPacketSize0 = 9;
1597 } else {
1598 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1600 } else {
1601 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
1604 value = min(w_length, (u16) sizeof cdev->desc);
1605 memcpy(req->buf, &cdev->desc, value);
1606 break;
1607 case USB_DT_DEVICE_QUALIFIER:
1608 if (!gadget_is_dualspeed(gadget) ||
1609 gadget->speed >= USB_SPEED_SUPER)
1610 break;
1611 device_qual(cdev);
1612 value = min_t(int, w_length,
1613 sizeof(struct usb_qualifier_descriptor));
1614 break;
1615 case USB_DT_OTHER_SPEED_CONFIG:
1616 if (!gadget_is_dualspeed(gadget) ||
1617 gadget->speed >= USB_SPEED_SUPER)
1618 break;
1619 /* FALLTHROUGH */
1620 case USB_DT_CONFIG:
1621 value = config_desc(cdev, w_value);
1622 if (value >= 0)
1623 value = min(w_length, (u16) value);
1624 break;
1625 case USB_DT_STRING:
1626 value = get_string(cdev, req->buf,
1627 w_index, w_value & 0xff);
1628 if (value >= 0)
1629 value = min(w_length, (u16) value);
1630 break;
1631 case USB_DT_BOS:
1632 if (gadget_is_superspeed(gadget)) {
1633 value = bos_desc(cdev);
1634 value = min(w_length, (u16) value);
1636 break;
1637 case USB_DT_OTG:
1638 if (gadget_is_otg(gadget)) {
1639 struct usb_configuration *config;
1640 int otg_desc_len = 0;
1642 if (cdev->config)
1643 config = cdev->config;
1644 else
1645 config = list_first_entry(
1646 &cdev->configs,
1647 struct usb_configuration, list);
1648 if (!config)
1649 goto done;
1651 if (gadget->otg_caps &&
1652 (gadget->otg_caps->otg_rev >= 0x0200))
1653 otg_desc_len += sizeof(
1654 struct usb_otg20_descriptor);
1655 else
1656 otg_desc_len += sizeof(
1657 struct usb_otg_descriptor);
1659 value = min_t(int, w_length, otg_desc_len);
1660 memcpy(req->buf, config->descriptors[0], value);
1662 break;
1664 break;
1666 /* any number of configs can work */
1667 case USB_REQ_SET_CONFIGURATION:
1668 if (ctrl->bRequestType != 0)
1669 goto unknown;
1670 if (gadget_is_otg(gadget)) {
1671 if (gadget->a_hnp_support)
1672 DBG(cdev, "HNP available\n");
1673 else if (gadget->a_alt_hnp_support)
1674 DBG(cdev, "HNP on another port\n");
1675 else
1676 VDBG(cdev, "HNP inactive\n");
1678 spin_lock(&cdev->lock);
1679 value = set_config(cdev, ctrl, w_value);
1680 spin_unlock(&cdev->lock);
1681 break;
1682 case USB_REQ_GET_CONFIGURATION:
1683 if (ctrl->bRequestType != USB_DIR_IN)
1684 goto unknown;
1685 if (cdev->config)
1686 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1687 else
1688 *(u8 *)req->buf = 0;
1689 value = min(w_length, (u16) 1);
1690 break;
1692 /* function drivers must handle get/set altsetting; if there's
1693 * no get() method, we know only altsetting zero works.
1695 case USB_REQ_SET_INTERFACE:
1696 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1697 goto unknown;
1698 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1699 break;
1700 f = cdev->config->interface[intf];
1701 if (!f)
1702 break;
1703 if (w_value && !f->set_alt)
1704 break;
1705 value = f->set_alt(f, w_index, w_value);
1706 if (value == USB_GADGET_DELAYED_STATUS) {
1707 DBG(cdev,
1708 "%s: interface %d (%s) requested delayed status\n",
1709 __func__, intf, f->name);
1710 cdev->delayed_status++;
1711 DBG(cdev, "delayed_status count %d\n",
1712 cdev->delayed_status);
1714 break;
1715 case USB_REQ_GET_INTERFACE:
1716 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1717 goto unknown;
1718 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1719 break;
1720 f = cdev->config->interface[intf];
1721 if (!f)
1722 break;
1723 /* lots of interfaces only need altsetting zero... */
1724 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1725 if (value < 0)
1726 break;
1727 *((u8 *)req->buf) = value;
1728 value = min(w_length, (u16) 1);
1729 break;
1730 case USB_REQ_GET_STATUS:
1731 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1732 (w_index == OTG_STS_SELECTOR)) {
1733 if (ctrl->bRequestType != (USB_DIR_IN |
1734 USB_RECIP_DEVICE))
1735 goto unknown;
1736 *((u8 *)req->buf) = gadget->host_request_flag;
1737 value = 1;
1738 break;
1742 * USB 3.0 additions:
1743 * Function driver should handle get_status request. If such cb
1744 * wasn't supplied we respond with default value = 0
1745 * Note: function driver should supply such cb only for the
1746 * first interface of the function
1748 if (!gadget_is_superspeed(gadget))
1749 goto unknown;
1750 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1751 goto unknown;
1752 value = 2; /* This is the length of the get_status reply */
1753 put_unaligned_le16(0, req->buf);
1754 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1755 break;
1756 f = cdev->config->interface[intf];
1757 if (!f)
1758 break;
1759 status = f->get_status ? f->get_status(f) : 0;
1760 if (status < 0)
1761 break;
1762 put_unaligned_le16(status & 0x0000ffff, req->buf);
1763 break;
1765 * Function drivers should handle SetFeature/ClearFeature
1766 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1767 * only for the first interface of the function
1769 case USB_REQ_CLEAR_FEATURE:
1770 case USB_REQ_SET_FEATURE:
1771 if (!gadget_is_superspeed(gadget))
1772 goto unknown;
1773 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1774 goto unknown;
1775 switch (w_value) {
1776 case USB_INTRF_FUNC_SUSPEND:
1777 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1778 break;
1779 f = cdev->config->interface[intf];
1780 if (!f)
1781 break;
1782 value = 0;
1783 if (f->func_suspend)
1784 value = f->func_suspend(f, w_index >> 8);
1785 if (value < 0) {
1786 ERROR(cdev,
1787 "func_suspend() returned error %d\n",
1788 value);
1789 value = 0;
1791 break;
1793 break;
1794 default:
1795 unknown:
1797 * OS descriptors handling
1799 if (cdev->use_os_string && cdev->os_desc_config &&
1800 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
1801 ctrl->bRequest == cdev->b_vendor_code) {
1802 struct usb_request *req;
1803 struct usb_configuration *os_desc_cfg;
1804 u8 *buf;
1805 int interface;
1806 int count = 0;
1808 req = cdev->os_desc_req;
1809 req->context = cdev;
1810 req->complete = composite_setup_complete;
1811 buf = req->buf;
1812 os_desc_cfg = cdev->os_desc_config;
1813 memset(buf, 0, w_length);
1814 buf[5] = 0x01;
1815 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1816 case USB_RECIP_DEVICE:
1817 if (w_index != 0x4 || (w_value >> 8))
1818 break;
1819 buf[6] = w_index;
1820 if (w_length == 0x10) {
1821 /* Number of ext compat interfaces */
1822 count = count_ext_compat(os_desc_cfg);
1823 buf[8] = count;
1824 count *= 24; /* 24 B/ext compat desc */
1825 count += 16; /* header */
1826 put_unaligned_le32(count, buf);
1827 value = w_length;
1828 } else {
1829 /* "extended compatibility ID"s */
1830 count = count_ext_compat(os_desc_cfg);
1831 buf[8] = count;
1832 count *= 24; /* 24 B/ext compat desc */
1833 count += 16; /* header */
1834 put_unaligned_le32(count, buf);
1835 buf += 16;
1836 fill_ext_compat(os_desc_cfg, buf);
1837 value = w_length;
1839 break;
1840 case USB_RECIP_INTERFACE:
1841 if (w_index != 0x5 || (w_value >> 8))
1842 break;
1843 interface = w_value & 0xFF;
1844 buf[6] = w_index;
1845 if (w_length == 0x0A) {
1846 count = count_ext_prop(os_desc_cfg,
1847 interface);
1848 put_unaligned_le16(count, buf + 8);
1849 count = len_ext_prop(os_desc_cfg,
1850 interface);
1851 put_unaligned_le32(count, buf);
1853 value = w_length;
1854 } else {
1855 count = count_ext_prop(os_desc_cfg,
1856 interface);
1857 put_unaligned_le16(count, buf + 8);
1858 count = len_ext_prop(os_desc_cfg,
1859 interface);
1860 put_unaligned_le32(count, buf);
1861 buf += 10;
1862 value = fill_ext_prop(os_desc_cfg,
1863 interface, buf);
1864 if (value < 0)
1865 return value;
1867 value = w_length;
1869 break;
1872 if (value >= 0) {
1873 req->length = value;
1874 req->context = cdev;
1875 req->zero = value < w_length;
1876 value = composite_ep0_queue(cdev, req,
1877 GFP_ATOMIC);
1878 if (value < 0) {
1879 DBG(cdev, "ep_queue --> %d\n", value);
1880 req->status = 0;
1881 composite_setup_complete(gadget->ep0,
1882 req);
1885 return value;
1888 VDBG(cdev,
1889 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1890 ctrl->bRequestType, ctrl->bRequest,
1891 w_value, w_index, w_length);
1893 /* functions always handle their interfaces and endpoints...
1894 * punt other recipients (other, WUSB, ...) to the current
1895 * configuration code.
1897 * REVISIT it could make sense to let the composite device
1898 * take such requests too, if that's ever needed: to work
1899 * in config 0, etc.
1901 if (cdev->config) {
1902 list_for_each_entry(f, &cdev->config->functions, list)
1903 if (f->req_match && f->req_match(f, ctrl))
1904 goto try_fun_setup;
1905 f = NULL;
1908 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1909 case USB_RECIP_INTERFACE:
1910 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1911 break;
1912 f = cdev->config->interface[intf];
1913 break;
1915 case USB_RECIP_ENDPOINT:
1916 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1917 list_for_each_entry(f, &cdev->config->functions, list) {
1918 if (test_bit(endp, f->endpoints))
1919 break;
1921 if (&f->list == &cdev->config->functions)
1922 f = NULL;
1923 break;
1925 try_fun_setup:
1926 if (f && f->setup)
1927 value = f->setup(f, ctrl);
1928 else {
1929 struct usb_configuration *c;
1931 c = cdev->config;
1932 if (!c)
1933 goto done;
1935 /* try current config's setup */
1936 if (c->setup) {
1937 value = c->setup(c, ctrl);
1938 goto done;
1941 /* try the only function in the current config */
1942 if (!list_is_singular(&c->functions))
1943 goto done;
1944 f = list_first_entry(&c->functions, struct usb_function,
1945 list);
1946 if (f->setup)
1947 value = f->setup(f, ctrl);
1950 goto done;
1953 /* respond with data transfer before status phase? */
1954 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
1955 req->length = value;
1956 req->context = cdev;
1957 req->zero = value < w_length;
1958 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
1959 if (value < 0) {
1960 DBG(cdev, "ep_queue --> %d\n", value);
1961 req->status = 0;
1962 composite_setup_complete(gadget->ep0, req);
1964 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1965 WARN(cdev,
1966 "%s: Delayed status not supported for w_length != 0",
1967 __func__);
1970 done:
1971 /* device either stalls (value < 0) or reports success */
1972 return value;
1975 void composite_disconnect(struct usb_gadget *gadget)
1977 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1978 unsigned long flags;
1980 /* REVISIT: should we have config and device level
1981 * disconnect callbacks?
1983 spin_lock_irqsave(&cdev->lock, flags);
1984 if (cdev->config)
1985 reset_config(cdev);
1986 if (cdev->driver->disconnect)
1987 cdev->driver->disconnect(cdev);
1988 spin_unlock_irqrestore(&cdev->lock, flags);
1991 /*-------------------------------------------------------------------------*/
1993 static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
1994 char *buf)
1996 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1997 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1999 return sprintf(buf, "%d\n", cdev->suspended);
2001 static DEVICE_ATTR_RO(suspended);
2003 static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
2005 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2007 /* composite_disconnect() must already have been called
2008 * by the underlying peripheral controller driver!
2009 * so there's no i/o concurrency that could affect the
2010 * state protected by cdev->lock.
2012 WARN_ON(cdev->config);
2014 while (!list_empty(&cdev->configs)) {
2015 struct usb_configuration *c;
2016 c = list_first_entry(&cdev->configs,
2017 struct usb_configuration, list);
2018 remove_config(cdev, c);
2020 if (cdev->driver->unbind && unbind_driver)
2021 cdev->driver->unbind(cdev);
2023 composite_dev_cleanup(cdev);
2025 kfree(cdev->def_manufacturer);
2026 kfree(cdev);
2027 set_gadget_data(gadget, NULL);
2030 static void composite_unbind(struct usb_gadget *gadget)
2032 __composite_unbind(gadget, true);
2035 static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2036 const struct usb_device_descriptor *old)
2038 __le16 idVendor;
2039 __le16 idProduct;
2040 __le16 bcdDevice;
2041 u8 iSerialNumber;
2042 u8 iManufacturer;
2043 u8 iProduct;
2046 * these variables may have been set in
2047 * usb_composite_overwrite_options()
2049 idVendor = new->idVendor;
2050 idProduct = new->idProduct;
2051 bcdDevice = new->bcdDevice;
2052 iSerialNumber = new->iSerialNumber;
2053 iManufacturer = new->iManufacturer;
2054 iProduct = new->iProduct;
2056 *new = *old;
2057 if (idVendor)
2058 new->idVendor = idVendor;
2059 if (idProduct)
2060 new->idProduct = idProduct;
2061 if (bcdDevice)
2062 new->bcdDevice = bcdDevice;
2063 else
2064 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
2065 if (iSerialNumber)
2066 new->iSerialNumber = iSerialNumber;
2067 if (iManufacturer)
2068 new->iManufacturer = iManufacturer;
2069 if (iProduct)
2070 new->iProduct = iProduct;
2073 int composite_dev_prepare(struct usb_composite_driver *composite,
2074 struct usb_composite_dev *cdev)
2076 struct usb_gadget *gadget = cdev->gadget;
2077 int ret = -ENOMEM;
2079 /* preallocate control response and buffer */
2080 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2081 if (!cdev->req)
2082 return -ENOMEM;
2084 cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2085 if (!cdev->req->buf)
2086 goto fail;
2088 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2089 if (ret)
2090 goto fail_dev;
2092 cdev->req->complete = composite_setup_complete;
2093 cdev->req->context = cdev;
2094 gadget->ep0->driver_data = cdev;
2096 cdev->driver = composite;
2099 * As per USB compliance update, a device that is actively drawing
2100 * more than 100mA from USB must report itself as bus-powered in
2101 * the GetStatus(DEVICE) call.
2103 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2104 usb_gadget_set_selfpowered(gadget);
2106 /* interface and string IDs start at zero via kzalloc.
2107 * we force endpoints to start unassigned; few controller
2108 * drivers will zero ep->driver_data.
2110 usb_ep_autoconfig_reset(gadget);
2111 return 0;
2112 fail_dev:
2113 kfree(cdev->req->buf);
2114 fail:
2115 usb_ep_free_request(gadget->ep0, cdev->req);
2116 cdev->req = NULL;
2117 return ret;
2120 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2121 struct usb_ep *ep0)
2123 int ret = 0;
2125 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2126 if (!cdev->os_desc_req) {
2127 ret = PTR_ERR(cdev->os_desc_req);
2128 goto end;
2131 /* OS feature descriptor length <= 4kB */
2132 cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
2133 if (!cdev->os_desc_req->buf) {
2134 ret = PTR_ERR(cdev->os_desc_req->buf);
2135 kfree(cdev->os_desc_req);
2136 goto end;
2138 cdev->os_desc_req->context = cdev;
2139 cdev->os_desc_req->complete = composite_setup_complete;
2140 end:
2141 return ret;
2144 void composite_dev_cleanup(struct usb_composite_dev *cdev)
2146 struct usb_gadget_string_container *uc, *tmp;
2148 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2149 list_del(&uc->list);
2150 kfree(uc);
2152 if (cdev->os_desc_req) {
2153 if (cdev->os_desc_pending)
2154 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2156 kfree(cdev->os_desc_req->buf);
2157 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2159 if (cdev->req) {
2160 if (cdev->setup_pending)
2161 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2163 kfree(cdev->req->buf);
2164 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2166 cdev->next_string_id = 0;
2167 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
2170 static int composite_bind(struct usb_gadget *gadget,
2171 struct usb_gadget_driver *gdriver)
2173 struct usb_composite_dev *cdev;
2174 struct usb_composite_driver *composite = to_cdriver(gdriver);
2175 int status = -ENOMEM;
2177 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2178 if (!cdev)
2179 return status;
2181 spin_lock_init(&cdev->lock);
2182 cdev->gadget = gadget;
2183 set_gadget_data(gadget, cdev);
2184 INIT_LIST_HEAD(&cdev->configs);
2185 INIT_LIST_HEAD(&cdev->gstrings);
2187 status = composite_dev_prepare(composite, cdev);
2188 if (status)
2189 goto fail;
2191 /* composite gadget needs to assign strings for whole device (like
2192 * serial number), register function drivers, potentially update
2193 * power state and consumption, etc
2195 status = composite->bind(cdev);
2196 if (status < 0)
2197 goto fail;
2199 if (cdev->use_os_string) {
2200 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2201 if (status)
2202 goto fail;
2205 update_unchanged_dev_desc(&cdev->desc, composite->dev);
2207 /* has userspace failed to provide a serial number? */
2208 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2209 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2211 INFO(cdev, "%s ready\n", composite->name);
2212 return 0;
2214 fail:
2215 __composite_unbind(gadget, false);
2216 return status;
2219 /*-------------------------------------------------------------------------*/
2221 void composite_suspend(struct usb_gadget *gadget)
2223 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2224 struct usb_function *f;
2226 /* REVISIT: should we have config level
2227 * suspend/resume callbacks?
2229 DBG(cdev, "suspend\n");
2230 if (cdev->config) {
2231 list_for_each_entry(f, &cdev->config->functions, list) {
2232 if (f->suspend)
2233 f->suspend(f);
2236 if (cdev->driver->suspend)
2237 cdev->driver->suspend(cdev);
2239 cdev->suspended = 1;
2241 usb_gadget_vbus_draw(gadget, 2);
2244 void composite_resume(struct usb_gadget *gadget)
2246 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2247 struct usb_function *f;
2248 u16 maxpower;
2250 /* REVISIT: should we have config level
2251 * suspend/resume callbacks?
2253 DBG(cdev, "resume\n");
2254 if (cdev->driver->resume)
2255 cdev->driver->resume(cdev);
2256 if (cdev->config) {
2257 list_for_each_entry(f, &cdev->config->functions, list) {
2258 if (f->resume)
2259 f->resume(f);
2262 maxpower = cdev->config->MaxPower;
2264 usb_gadget_vbus_draw(gadget, maxpower ?
2265 maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
2268 cdev->suspended = 0;
2271 /*-------------------------------------------------------------------------*/
2273 static const struct usb_gadget_driver composite_driver_template = {
2274 .bind = composite_bind,
2275 .unbind = composite_unbind,
2277 .setup = composite_setup,
2278 .reset = composite_disconnect,
2279 .disconnect = composite_disconnect,
2281 .suspend = composite_suspend,
2282 .resume = composite_resume,
2284 .driver = {
2285 .owner = THIS_MODULE,
2290 * usb_composite_probe() - register a composite driver
2291 * @driver: the driver to register
2293 * Context: single threaded during gadget setup
2295 * This function is used to register drivers using the composite driver
2296 * framework. The return value is zero, or a negative errno value.
2297 * Those values normally come from the driver's @bind method, which does
2298 * all the work of setting up the driver to match the hardware.
2300 * On successful return, the gadget is ready to respond to requests from
2301 * the host, unless one of its components invokes usb_gadget_disconnect()
2302 * while it was binding. That would usually be done in order to wait for
2303 * some userspace participation.
2305 int usb_composite_probe(struct usb_composite_driver *driver)
2307 struct usb_gadget_driver *gadget_driver;
2309 if (!driver || !driver->dev || !driver->bind)
2310 return -EINVAL;
2312 if (!driver->name)
2313 driver->name = "composite";
2315 driver->gadget_driver = composite_driver_template;
2316 gadget_driver = &driver->gadget_driver;
2318 gadget_driver->function = (char *) driver->name;
2319 gadget_driver->driver.name = driver->name;
2320 gadget_driver->max_speed = driver->max_speed;
2322 return usb_gadget_probe_driver(gadget_driver);
2324 EXPORT_SYMBOL_GPL(usb_composite_probe);
2327 * usb_composite_unregister() - unregister a composite driver
2328 * @driver: the driver to unregister
2330 * This function is used to unregister drivers using the composite
2331 * driver framework.
2333 void usb_composite_unregister(struct usb_composite_driver *driver)
2335 usb_gadget_unregister_driver(&driver->gadget_driver);
2337 EXPORT_SYMBOL_GPL(usb_composite_unregister);
2340 * usb_composite_setup_continue() - Continue with the control transfer
2341 * @cdev: the composite device who's control transfer was kept waiting
2343 * This function must be called by the USB function driver to continue
2344 * with the control transfer's data/status stage in case it had requested to
2345 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2346 * can request the composite framework to delay the setup request's data/status
2347 * stages by returning USB_GADGET_DELAYED_STATUS.
2349 void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2351 int value;
2352 struct usb_request *req = cdev->req;
2353 unsigned long flags;
2355 DBG(cdev, "%s\n", __func__);
2356 spin_lock_irqsave(&cdev->lock, flags);
2358 if (cdev->delayed_status == 0) {
2359 WARN(cdev, "%s: Unexpected call\n", __func__);
2361 } else if (--cdev->delayed_status == 0) {
2362 DBG(cdev, "%s: Completing delayed status\n", __func__);
2363 req->length = 0;
2364 req->context = cdev;
2365 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
2366 if (value < 0) {
2367 DBG(cdev, "ep_queue --> %d\n", value);
2368 req->status = 0;
2369 composite_setup_complete(cdev->gadget->ep0, req);
2373 spin_unlock_irqrestore(&cdev->lock, flags);
2375 EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
2377 static char *composite_default_mfr(struct usb_gadget *gadget)
2379 char *mfr;
2380 int len;
2382 len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
2383 init_utsname()->release, gadget->name);
2384 len++;
2385 mfr = kmalloc(len, GFP_KERNEL);
2386 if (!mfr)
2387 return NULL;
2388 snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
2389 init_utsname()->release, gadget->name);
2390 return mfr;
2393 void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2394 struct usb_composite_overwrite *covr)
2396 struct usb_device_descriptor *desc = &cdev->desc;
2397 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2398 struct usb_string *dev_str = gstr->strings;
2400 if (covr->idVendor)
2401 desc->idVendor = cpu_to_le16(covr->idVendor);
2403 if (covr->idProduct)
2404 desc->idProduct = cpu_to_le16(covr->idProduct);
2406 if (covr->bcdDevice)
2407 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
2409 if (covr->serial_number) {
2410 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2411 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2413 if (covr->manufacturer) {
2414 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2415 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
2417 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2418 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2419 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2420 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
2423 if (covr->product) {
2424 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2425 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2428 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
2430 MODULE_LICENSE("GPL");
2431 MODULE_AUTHOR("David Brownell");