1 // SPDX-License-Identifier: GPL-2.0+
3 * composite.c - infrastructure for Composite USB Gadgets
5 * Copyright (C) 2006-2008 David Brownell
8 /* #define VERBOSE_DEBUG */
10 #include <linux/kallsyms.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/utsname.h>
16 #include <linux/bitfield.h>
17 #include <linux/uuid.h>
19 #include <linux/usb/composite.h>
20 #include <linux/usb/otg.h>
21 #include <linux/usb/webusb.h>
22 #include <asm/unaligned.h>
24 #include "u_os_desc.h"
27 * struct usb_os_string - represents OS String to be reported by a gadget
28 * @bLength: total length of the entire descritor, always 0x12
29 * @bDescriptorType: USB_DT_STRING
30 * @qwSignature: the OS String proper
31 * @bMS_VendorCode: code used by the host for subsequent requests
32 * @bPad: not used, must be zero
34 struct usb_os_string {
37 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
43 * The code in this file is utility code, used to build a gadget driver
44 * from one or more "function" drivers, one or more "configuration"
45 * objects, and a "usb_composite_driver" by gluing them together along
46 * with the relevant device-wide data.
49 static struct usb_gadget_strings **get_containers_gs(
50 struct usb_gadget_string_container *uc)
52 return (struct usb_gadget_strings **)uc->stash;
56 * function_descriptors() - get function descriptors for speed
60 * Returns the descriptors or NULL if not set.
62 static struct usb_descriptor_header **
63 function_descriptors(struct usb_function *f,
64 enum usb_device_speed speed)
66 struct usb_descriptor_header **descriptors;
69 * NOTE: we try to help gadget drivers which might not be setting
70 * max_speed appropriately.
74 case USB_SPEED_SUPER_PLUS:
75 descriptors = f->ssp_descriptors;
80 descriptors = f->ss_descriptors;
85 descriptors = f->hs_descriptors;
90 descriptors = f->fs_descriptors;
94 * if we can't find any descriptors at all, then this gadget deserves to
95 * Oops with a NULL pointer dereference
102 * next_desc() - advance to the next desc_type descriptor
103 * @t: currect pointer within descriptor array
104 * @desc_type: descriptor type
106 * Return: next desc_type descriptor or NULL
108 * Iterate over @t until either desc_type descriptor found or
109 * NULL (that indicates end of list) encountered
111 static struct usb_descriptor_header**
112 next_desc(struct usb_descriptor_header **t, u8 desc_type)
115 if ((*t)->bDescriptorType == desc_type)
122 * for_each_desc() - iterate over desc_type descriptors in the
124 * @start: pointer within descriptor array.
125 * @iter_desc: desc_type descriptor to use as the loop cursor
126 * @desc_type: wanted descriptr type
128 #define for_each_desc(start, iter_desc, desc_type) \
129 for (iter_desc = next_desc(start, desc_type); \
130 iter_desc; iter_desc = next_desc(iter_desc + 1, desc_type))
133 * config_ep_by_speed_and_alt() - configures the given endpoint
134 * according to gadget speed.
135 * @g: pointer to the gadget
137 * @_ep: the endpoint to configure
138 * @alt: alternate setting number
140 * Return: error code, 0 on success
142 * This function chooses the right descriptors for a given
143 * endpoint according to gadget speed and saves it in the
144 * endpoint desc field. If the endpoint already has a descriptor
145 * assigned to it - overwrites it with currently corresponding
146 * descriptor. The endpoint maxpacket field is updated according
147 * to the chosen descriptor.
148 * Note: the supplied function should hold all the descriptors
149 * for supported speeds
151 int config_ep_by_speed_and_alt(struct usb_gadget *g,
152 struct usb_function *f,
156 struct usb_endpoint_descriptor *chosen_desc = NULL;
157 struct usb_interface_descriptor *int_desc = NULL;
158 struct usb_descriptor_header **speed_desc = NULL;
160 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
161 int want_comp_desc = 0;
163 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
164 struct usb_composite_dev *cdev;
165 bool incomplete_desc = false;
167 if (!g || !f || !_ep)
170 /* select desired speed */
172 case USB_SPEED_SUPER_PLUS:
173 if (gadget_is_superspeed_plus(g)) {
174 if (f->ssp_descriptors) {
175 speed_desc = f->ssp_descriptors;
179 incomplete_desc = true;
182 case USB_SPEED_SUPER:
183 if (gadget_is_superspeed(g)) {
184 if (f->ss_descriptors) {
185 speed_desc = f->ss_descriptors;
189 incomplete_desc = true;
193 if (gadget_is_dualspeed(g)) {
194 if (f->hs_descriptors) {
195 speed_desc = f->hs_descriptors;
198 incomplete_desc = true;
202 speed_desc = f->fs_descriptors;
205 cdev = get_gadget_data(g);
208 "%s doesn't hold the descriptors for current speed\n",
211 /* find correct alternate setting descriptor */
212 for_each_desc(speed_desc, d_spd, USB_DT_INTERFACE) {
213 int_desc = (struct usb_interface_descriptor *)*d_spd;
215 if (int_desc->bAlternateSetting == alt) {
223 /* find descriptors */
224 for_each_desc(speed_desc, d_spd, USB_DT_ENDPOINT) {
225 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
226 if (chosen_desc->bEndpointAddress == _ep->address)
233 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
234 _ep->desc = chosen_desc;
235 _ep->comp_desc = NULL;
239 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
240 usb_endpoint_xfer_int(_ep->desc)))
241 _ep->mult = usb_endpoint_maxp_mult(_ep->desc);
247 * Companion descriptor should follow EP descriptor
248 * USB 3.0 spec, #9.6.7
250 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
252 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
254 _ep->comp_desc = comp_desc;
255 if (g->speed >= USB_SPEED_SUPER) {
256 switch (usb_endpoint_type(_ep->desc)) {
257 case USB_ENDPOINT_XFER_ISOC:
258 /* mult: bits 1:0 of bmAttributes */
259 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
261 case USB_ENDPOINT_XFER_BULK:
262 case USB_ENDPOINT_XFER_INT:
263 _ep->maxburst = comp_desc->bMaxBurst + 1;
266 if (comp_desc->bMaxBurst != 0)
267 ERROR(cdev, "ep0 bMaxBurst must be 0\n");
274 EXPORT_SYMBOL_GPL(config_ep_by_speed_and_alt);
277 * config_ep_by_speed() - configures the given endpoint
278 * according to gadget speed.
279 * @g: pointer to the gadget
281 * @_ep: the endpoint to configure
283 * Return: error code, 0 on success
285 * This function chooses the right descriptors for a given
286 * endpoint according to gadget speed and saves it in the
287 * endpoint desc field. If the endpoint already has a descriptor
288 * assigned to it - overwrites it with currently corresponding
289 * descriptor. The endpoint maxpacket field is updated according
290 * to the chosen descriptor.
291 * Note: the supplied function should hold all the descriptors
292 * for supported speeds
294 int config_ep_by_speed(struct usb_gadget *g,
295 struct usb_function *f,
298 return config_ep_by_speed_and_alt(g, f, _ep, 0);
300 EXPORT_SYMBOL_GPL(config_ep_by_speed);
303 * usb_add_function() - add a function to a configuration
304 * @config: the configuration
305 * @function: the function being added
306 * Context: single threaded during gadget setup
308 * After initialization, each configuration must have one or more
309 * functions added to it. Adding a function involves calling its @bind()
310 * method to allocate resources such as interface and string identifiers
313 * This function returns the value of the function's bind(), which is
314 * zero for success else a negative errno value.
316 int usb_add_function(struct usb_configuration *config,
317 struct usb_function *function)
321 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
322 function->name, function,
323 config->label, config);
325 if (!function->set_alt || !function->disable)
328 function->config = config;
329 list_add_tail(&function->list, &config->functions);
331 if (function->bind_deactivated) {
332 value = usb_function_deactivate(function);
337 /* REVISIT *require* function->bind? */
338 if (function->bind) {
339 value = function->bind(config, function);
341 list_del(&function->list);
342 function->config = NULL;
347 /* We allow configurations that don't work at both speeds.
348 * If we run into a lowspeed Linux system, treat it the same
349 * as full speed ... it's the function drivers that will need
350 * to avoid bulk and ISO transfers.
352 if (!config->fullspeed && function->fs_descriptors)
353 config->fullspeed = true;
354 if (!config->highspeed && function->hs_descriptors)
355 config->highspeed = true;
356 if (!config->superspeed && function->ss_descriptors)
357 config->superspeed = true;
358 if (!config->superspeed_plus && function->ssp_descriptors)
359 config->superspeed_plus = true;
363 DBG(config->cdev, "adding '%s'/%p --> %d\n",
364 function->name, function, value);
367 EXPORT_SYMBOL_GPL(usb_add_function);
369 void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
374 bitmap_zero(f->endpoints, 32);
379 if (f->bind_deactivated)
380 usb_function_activate(f);
382 EXPORT_SYMBOL_GPL(usb_remove_function);
385 * usb_function_deactivate - prevent function and gadget enumeration
386 * @function: the function that isn't yet ready to respond
388 * Blocks response of the gadget driver to host enumeration by
389 * preventing the data line pullup from being activated. This is
390 * normally called during @bind() processing to change from the
391 * initial "ready to respond" state, or when a required resource
394 * For example, drivers that serve as a passthrough to a userspace
395 * daemon can block enumeration unless that daemon (such as an OBEX,
396 * MTP, or print server) is ready to handle host requests.
398 * Not all systems support software control of their USB peripheral
401 * Returns zero on success, else negative errno.
403 int usb_function_deactivate(struct usb_function *function)
405 struct usb_composite_dev *cdev = function->config->cdev;
409 spin_lock_irqsave(&cdev->lock, flags);
411 if (cdev->deactivations == 0) {
412 spin_unlock_irqrestore(&cdev->lock, flags);
413 status = usb_gadget_deactivate(cdev->gadget);
414 spin_lock_irqsave(&cdev->lock, flags);
417 cdev->deactivations++;
419 spin_unlock_irqrestore(&cdev->lock, flags);
422 EXPORT_SYMBOL_GPL(usb_function_deactivate);
425 * usb_function_activate - allow function and gadget enumeration
426 * @function: function on which usb_function_activate() was called
428 * Reverses effect of usb_function_deactivate(). If no more functions
429 * are delaying their activation, the gadget driver will respond to
430 * host enumeration procedures.
432 * Returns zero on success, else negative errno.
434 int usb_function_activate(struct usb_function *function)
436 struct usb_composite_dev *cdev = function->config->cdev;
440 spin_lock_irqsave(&cdev->lock, flags);
442 if (WARN_ON(cdev->deactivations == 0))
445 cdev->deactivations--;
446 if (cdev->deactivations == 0) {
447 spin_unlock_irqrestore(&cdev->lock, flags);
448 status = usb_gadget_activate(cdev->gadget);
449 spin_lock_irqsave(&cdev->lock, flags);
453 spin_unlock_irqrestore(&cdev->lock, flags);
456 EXPORT_SYMBOL_GPL(usb_function_activate);
459 * usb_interface_id() - allocate an unused interface ID
460 * @config: configuration associated with the interface
461 * @function: function handling the interface
462 * Context: single threaded during gadget setup
464 * usb_interface_id() is called from usb_function.bind() callbacks to
465 * allocate new interface IDs. The function driver will then store that
466 * ID in interface, association, CDC union, and other descriptors. It
467 * will also handle any control requests targeted at that interface,
468 * particularly changing its altsetting via set_alt(). There may
469 * also be class-specific or vendor-specific requests to handle.
471 * All interface identifier should be allocated using this routine, to
472 * ensure that for example different functions don't wrongly assign
473 * different meanings to the same identifier. Note that since interface
474 * identifiers are configuration-specific, functions used in more than
475 * one configuration (or more than once in a given configuration) need
476 * multiple versions of the relevant descriptors.
478 * Returns the interface ID which was allocated; or -ENODEV if no
479 * more interface IDs can be allocated.
481 int usb_interface_id(struct usb_configuration *config,
482 struct usb_function *function)
484 unsigned id = config->next_interface_id;
486 if (id < MAX_CONFIG_INTERFACES) {
487 config->interface[id] = function;
488 config->next_interface_id = id + 1;
493 EXPORT_SYMBOL_GPL(usb_interface_id);
496 * usb_func_wakeup - sends function wake notification to the host.
497 * @func: function that sends the remote wakeup notification.
499 * Applicable to devices operating at enhanced superspeed when usb
500 * functions are put in function suspend state and armed for function
501 * remote wakeup. On completion, function wake notification is sent. If
502 * the device is in low power state it tries to bring the device to active
503 * state before sending the wake notification. Since it is a synchronous
504 * call, caller must take care of not calling it in interrupt context.
505 * For devices operating at lower speeds returns negative errno.
507 * Returns zero on success, else negative errno.
509 int usb_func_wakeup(struct usb_function *func)
511 struct usb_gadget *gadget = func->config->cdev->gadget;
514 if (!gadget->ops->func_wakeup)
517 if (!func->func_wakeup_armed) {
518 ERROR(func->config->cdev, "not armed for func remote wakeup\n");
522 for (id = 0; id < MAX_CONFIG_INTERFACES; id++)
523 if (func->config->interface[id] == func)
526 if (id == MAX_CONFIG_INTERFACES) {
527 ERROR(func->config->cdev, "Invalid function\n");
531 return gadget->ops->func_wakeup(gadget, id);
533 EXPORT_SYMBOL_GPL(usb_func_wakeup);
535 static u8 encode_bMaxPower(enum usb_device_speed speed,
536 struct usb_configuration *c)
540 if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER))
543 val = CONFIG_USB_GADGET_VBUS_DRAW;
546 if (speed < USB_SPEED_SUPER)
547 return min(val, 500U) / 2;
550 * USB 3.x supports up to 900mA, but since 900 isn't divisible
551 * by 8 the integral division will effectively cap to 896mA.
553 return min(val, 900U) / 8;
556 void check_remote_wakeup_config(struct usb_gadget *g,
557 struct usb_configuration *c)
559 if (USB_CONFIG_ATT_WAKEUP & c->bmAttributes) {
560 /* Reset the rw bit if gadget is not capable of it */
561 if (!g->wakeup_capable && g->ops->set_remote_wakeup) {
562 WARN(c->cdev, "Clearing wakeup bit for config c.%d\n",
563 c->bConfigurationValue);
564 c->bmAttributes &= ~USB_CONFIG_ATT_WAKEUP;
569 static int config_buf(struct usb_configuration *config,
570 enum usb_device_speed speed, void *buf, u8 type)
572 struct usb_config_descriptor *c = buf;
573 void *next = buf + USB_DT_CONFIG_SIZE;
575 struct usb_function *f;
578 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
579 /* write the config descriptor */
581 c->bLength = USB_DT_CONFIG_SIZE;
582 c->bDescriptorType = type;
583 /* wTotalLength is written later */
584 c->bNumInterfaces = config->next_interface_id;
585 c->bConfigurationValue = config->bConfigurationValue;
586 c->iConfiguration = config->iConfiguration;
587 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
588 c->bMaxPower = encode_bMaxPower(speed, config);
590 /* There may be e.g. OTG descriptors */
591 if (config->descriptors) {
592 status = usb_descriptor_fillbuf(next, len,
593 config->descriptors);
600 /* add each function's descriptors */
601 list_for_each_entry(f, &config->functions, list) {
602 struct usb_descriptor_header **descriptors;
604 descriptors = function_descriptors(f, speed);
607 status = usb_descriptor_fillbuf(next, len,
608 (const struct usb_descriptor_header **) descriptors);
616 c->wTotalLength = cpu_to_le16(len);
620 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
622 struct usb_gadget *gadget = cdev->gadget;
623 struct usb_configuration *c;
624 struct list_head *pos;
625 u8 type = w_value >> 8;
626 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
628 if (gadget->speed >= USB_SPEED_SUPER)
629 speed = gadget->speed;
630 else if (gadget_is_dualspeed(gadget)) {
632 if (gadget->speed == USB_SPEED_HIGH)
634 if (type == USB_DT_OTHER_SPEED_CONFIG)
637 speed = USB_SPEED_HIGH;
641 /* This is a lookup by config *INDEX* */
644 pos = &cdev->configs;
645 c = cdev->os_desc_config;
649 while ((pos = pos->next) != &cdev->configs) {
650 c = list_entry(pos, typeof(*c), list);
652 /* skip OS Descriptors config which is handled separately */
653 if (c == cdev->os_desc_config)
657 /* ignore configs that won't work at this speed */
659 case USB_SPEED_SUPER_PLUS:
660 if (!c->superspeed_plus)
663 case USB_SPEED_SUPER:
677 return config_buf(c, speed, cdev->req->buf, type);
683 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
685 struct usb_gadget *gadget = cdev->gadget;
686 struct usb_configuration *c;
692 if (gadget_is_dualspeed(gadget)) {
693 if (gadget->speed == USB_SPEED_HIGH)
695 if (gadget->speed == USB_SPEED_SUPER)
697 if (gadget->speed == USB_SPEED_SUPER_PLUS)
699 if (type == USB_DT_DEVICE_QUALIFIER)
702 list_for_each_entry(c, &cdev->configs, list) {
703 /* ignore configs that won't work at this speed */
705 if (!c->superspeed_plus)
723 * bos_desc() - prepares the BOS descriptor.
724 * @cdev: pointer to usb_composite device to generate the bos
727 * This function generates the BOS (Binary Device Object)
728 * descriptor and its device capabilities descriptors. The BOS
729 * descriptor should be supported by a SuperSpeed device.
731 static int bos_desc(struct usb_composite_dev *cdev)
733 struct usb_ext_cap_descriptor *usb_ext;
734 struct usb_dcd_config_params dcd_config_params;
735 struct usb_bos_descriptor *bos = cdev->req->buf;
736 unsigned int besl = 0;
738 bos->bLength = USB_DT_BOS_SIZE;
739 bos->bDescriptorType = USB_DT_BOS;
741 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
742 bos->bNumDeviceCaps = 0;
744 /* Get Controller configuration */
745 if (cdev->gadget->ops->get_config_params) {
746 cdev->gadget->ops->get_config_params(cdev->gadget,
749 dcd_config_params.besl_baseline =
750 USB_DEFAULT_BESL_UNSPECIFIED;
751 dcd_config_params.besl_deep =
752 USB_DEFAULT_BESL_UNSPECIFIED;
753 dcd_config_params.bU1devExitLat =
754 USB_DEFAULT_U1_DEV_EXIT_LAT;
755 dcd_config_params.bU2DevExitLat =
756 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
759 if (dcd_config_params.besl_baseline != USB_DEFAULT_BESL_UNSPECIFIED)
760 besl = USB_BESL_BASELINE_VALID |
761 USB_SET_BESL_BASELINE(dcd_config_params.besl_baseline);
763 if (dcd_config_params.besl_deep != USB_DEFAULT_BESL_UNSPECIFIED)
764 besl |= USB_BESL_DEEP_VALID |
765 USB_SET_BESL_DEEP(dcd_config_params.besl_deep);
768 * A SuperSpeed device shall include the USB2.0 extension descriptor
769 * and shall support LPM when operating in USB2.0 HS mode.
771 if (cdev->gadget->lpm_capable) {
772 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
773 bos->bNumDeviceCaps++;
774 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
775 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
776 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
777 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
778 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT |
779 USB_BESL_SUPPORT | besl);
783 * The Superspeed USB Capability descriptor shall be implemented by all
784 * SuperSpeed devices.
786 if (gadget_is_superspeed(cdev->gadget)) {
787 struct usb_ss_cap_descriptor *ss_cap;
789 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
790 bos->bNumDeviceCaps++;
791 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
792 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
793 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
794 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
795 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
796 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
797 USB_FULL_SPEED_OPERATION |
798 USB_HIGH_SPEED_OPERATION |
799 USB_5GBPS_OPERATION);
800 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
801 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
802 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
805 /* The SuperSpeedPlus USB Device Capability descriptor */
806 if (gadget_is_superspeed_plus(cdev->gadget)) {
807 struct usb_ssp_cap_descriptor *ssp_cap;
812 if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x2)
816 * Paired RX and TX sublink speed attributes share
819 ssic = (ssac + 1) / 2 - 1;
821 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
822 bos->bNumDeviceCaps++;
824 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(ssac));
825 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(ssac);
826 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
827 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
828 ssp_cap->bReserved = 0;
829 ssp_cap->wReserved = 0;
831 ssp_cap->bmAttributes =
832 cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_ATTRIBS, ssac) |
833 FIELD_PREP(USB_SSP_SUBLINK_SPEED_IDS, ssic));
835 ssp_cap->wFunctionalitySupport =
836 cpu_to_le16(FIELD_PREP(USB_SSP_MIN_SUBLINK_SPEED_ATTRIBUTE_ID, 0) |
837 FIELD_PREP(USB_SSP_MIN_RX_LANE_COUNT, 1) |
838 FIELD_PREP(USB_SSP_MIN_TX_LANE_COUNT, 1));
841 * Use 1 SSID if the gadget supports up to gen2x1 or not
843 * - SSID 0 for symmetric RX/TX sublink speed of 10 Gbps.
845 * Use 1 SSID if the gadget supports up to gen1x2:
846 * - SSID 0 for symmetric RX/TX sublink speed of 5 Gbps.
848 * Use 2 SSIDs if the gadget supports up to gen2x2:
849 * - SSID 0 for symmetric RX/TX sublink speed of 5 Gbps.
850 * - SSID 1 for symmetric RX/TX sublink speed of 10 Gbps.
852 for (i = 0; i < ssac + 1; i++) {
859 if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x1 ||
860 cdev->gadget->max_ssp_rate == USB_SSP_GEN_UNKNOWN)
863 mantissa = 5 << ssid;
866 type = USB_SSP_SUBLINK_SPEED_ST_SYM_TX;
868 type = USB_SSP_SUBLINK_SPEED_ST_SYM_RX;
870 ssp_cap->bmSublinkSpeedAttr[i] =
871 cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_SSID, ssid) |
872 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSE,
873 USB_SSP_SUBLINK_SPEED_LSE_GBPS) |
874 FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST, type) |
875 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LP,
876 USB_SSP_SUBLINK_SPEED_LP_SSP) |
877 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSM, mantissa));
881 /* The WebUSB Platform Capability descriptor */
882 if (cdev->use_webusb) {
883 struct usb_plat_dev_cap_descriptor *webusb_cap;
884 struct usb_webusb_cap_data *webusb_cap_data;
885 guid_t webusb_uuid = WEBUSB_UUID;
887 webusb_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
888 webusb_cap_data = (struct usb_webusb_cap_data *) webusb_cap->CapabilityData;
889 bos->bNumDeviceCaps++;
890 le16_add_cpu(&bos->wTotalLength,
891 USB_DT_USB_PLAT_DEV_CAP_SIZE(USB_WEBUSB_CAP_DATA_SIZE));
893 webusb_cap->bLength = USB_DT_USB_PLAT_DEV_CAP_SIZE(USB_WEBUSB_CAP_DATA_SIZE);
894 webusb_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
895 webusb_cap->bDevCapabilityType = USB_PLAT_DEV_CAP_TYPE;
896 webusb_cap->bReserved = 0;
897 export_guid(webusb_cap->UUID, &webusb_uuid);
899 if (cdev->bcd_webusb_version != 0)
900 webusb_cap_data->bcdVersion = cpu_to_le16(cdev->bcd_webusb_version);
902 webusb_cap_data->bcdVersion = WEBUSB_VERSION_1_00;
904 webusb_cap_data->bVendorCode = cdev->b_webusb_vendor_code;
906 if (strnlen(cdev->landing_page, sizeof(cdev->landing_page)) > 0)
907 webusb_cap_data->iLandingPage = WEBUSB_LANDING_PAGE_PRESENT;
909 webusb_cap_data->iLandingPage = WEBUSB_LANDING_PAGE_NOT_PRESENT;
912 return le16_to_cpu(bos->wTotalLength);
915 static void device_qual(struct usb_composite_dev *cdev)
917 struct usb_qualifier_descriptor *qual = cdev->req->buf;
919 qual->bLength = sizeof(*qual);
920 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
921 /* POLICY: same bcdUSB and device type info at both speeds */
922 qual->bcdUSB = cdev->desc.bcdUSB;
923 qual->bDeviceClass = cdev->desc.bDeviceClass;
924 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
925 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
926 /* ASSUME same EP0 fifo size at both speeds */
927 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
928 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
932 /*-------------------------------------------------------------------------*/
934 static void reset_config(struct usb_composite_dev *cdev)
936 struct usb_function *f;
938 DBG(cdev, "reset config\n");
940 list_for_each_entry(f, &cdev->config->functions, list) {
944 /* Section 9.1.1.6, disable remote wakeup when device is reset */
945 f->func_wakeup_armed = false;
947 bitmap_zero(f->endpoints, 32);
950 cdev->delayed_status = 0;
953 static int set_config(struct usb_composite_dev *cdev,
954 const struct usb_ctrlrequest *ctrl, unsigned number)
956 struct usb_gadget *gadget = cdev->gadget;
957 struct usb_configuration *c = NULL, *iter;
958 int result = -EINVAL;
959 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
963 list_for_each_entry(iter, &cdev->configs, list) {
964 if (iter->bConfigurationValue != number)
967 * We disable the FDs of the previous
968 * configuration only if the new configuration
979 } else { /* Zero configuration value - need to reset the config */
985 DBG(cdev, "%s config #%d: %s\n",
986 usb_speed_string(gadget->speed),
987 number, c ? c->label : "unconfigured");
992 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
995 /* Initialize all interfaces by setting them to altsetting zero. */
996 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
997 struct usb_function *f = c->interface[tmp];
998 struct usb_descriptor_header **descriptors;
1004 * Record which endpoints are used by the function. This is used
1005 * to dispatch control requests targeted at that endpoint to the
1006 * function's setup callback instead of the current
1007 * configuration's setup callback.
1009 descriptors = function_descriptors(f, gadget->speed);
1011 for (; *descriptors; ++descriptors) {
1012 struct usb_endpoint_descriptor *ep;
1015 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
1018 ep = (struct usb_endpoint_descriptor *)*descriptors;
1019 addr = ((ep->bEndpointAddress & 0x80) >> 3)
1020 | (ep->bEndpointAddress & 0x0f);
1021 set_bit(addr, f->endpoints);
1024 result = f->set_alt(f, tmp, 0);
1026 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
1027 tmp, f->name, f, result);
1033 if (result == USB_GADGET_DELAYED_STATUS) {
1035 "%s: interface %d (%s) requested delayed status\n",
1036 __func__, tmp, f->name);
1037 cdev->delayed_status++;
1038 DBG(cdev, "delayed_status count %d\n",
1039 cdev->delayed_status);
1043 /* when we return, be sure our power usage is valid */
1044 if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER))
1045 power = c->MaxPower;
1047 power = CONFIG_USB_GADGET_VBUS_DRAW;
1049 if (gadget->speed < USB_SPEED_SUPER)
1050 power = min(power, 500U);
1052 power = min(power, 900U);
1054 if (USB_CONFIG_ATT_WAKEUP & c->bmAttributes)
1055 usb_gadget_set_remote_wakeup(gadget, 1);
1057 usb_gadget_set_remote_wakeup(gadget, 0);
1059 if (power <= USB_SELF_POWER_VBUS_MAX_DRAW)
1060 usb_gadget_set_selfpowered(gadget);
1062 usb_gadget_clear_selfpowered(gadget);
1064 usb_gadget_vbus_draw(gadget, power);
1065 if (result >= 0 && cdev->delayed_status)
1066 result = USB_GADGET_DELAYED_STATUS;
1070 int usb_add_config_only(struct usb_composite_dev *cdev,
1071 struct usb_configuration *config)
1073 struct usb_configuration *c;
1075 if (!config->bConfigurationValue)
1078 /* Prevent duplicate configuration identifiers */
1079 list_for_each_entry(c, &cdev->configs, list) {
1080 if (c->bConfigurationValue == config->bConfigurationValue)
1084 config->cdev = cdev;
1085 list_add_tail(&config->list, &cdev->configs);
1087 INIT_LIST_HEAD(&config->functions);
1088 config->next_interface_id = 0;
1089 memset(config->interface, 0, sizeof(config->interface));
1093 EXPORT_SYMBOL_GPL(usb_add_config_only);
1096 * usb_add_config() - add a configuration to a device.
1097 * @cdev: wraps the USB gadget
1098 * @config: the configuration, with bConfigurationValue assigned
1099 * @bind: the configuration's bind function
1100 * Context: single threaded during gadget setup
1102 * One of the main tasks of a composite @bind() routine is to
1103 * add each of the configurations it supports, using this routine.
1105 * This function returns the value of the configuration's @bind(), which
1106 * is zero for success else a negative errno value. Binding configurations
1107 * assigns global resources including string IDs, and per-configuration
1108 * resources such as interface IDs and endpoints.
1110 int usb_add_config(struct usb_composite_dev *cdev,
1111 struct usb_configuration *config,
1112 int (*bind)(struct usb_configuration *))
1114 int status = -EINVAL;
1119 DBG(cdev, "adding config #%u '%s'/%p\n",
1120 config->bConfigurationValue,
1121 config->label, config);
1123 status = usb_add_config_only(cdev, config);
1127 status = bind(config);
1129 while (!list_empty(&config->functions)) {
1130 struct usb_function *f;
1132 f = list_first_entry(&config->functions,
1133 struct usb_function, list);
1136 DBG(cdev, "unbind function '%s'/%p\n",
1138 f->unbind(config, f);
1139 /* may free memory for "f" */
1142 list_del(&config->list);
1143 config->cdev = NULL;
1147 DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n",
1148 config->bConfigurationValue, config,
1149 config->superspeed_plus ? " superplus" : "",
1150 config->superspeed ? " super" : "",
1151 config->highspeed ? " high" : "",
1153 ? (gadget_is_dualspeed(cdev->gadget)
1158 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
1159 struct usb_function *f = config->interface[i];
1163 DBG(cdev, " interface %d = %s/%p\n",
1168 /* set_alt(), or next bind(), sets up ep->claimed as needed */
1169 usb_ep_autoconfig_reset(cdev->gadget);
1173 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
1174 config->bConfigurationValue, status);
1177 EXPORT_SYMBOL_GPL(usb_add_config);
1179 static void remove_config(struct usb_composite_dev *cdev,
1180 struct usb_configuration *config)
1182 while (!list_empty(&config->functions)) {
1183 struct usb_function *f;
1185 f = list_first_entry(&config->functions,
1186 struct usb_function, list);
1188 usb_remove_function(config, f);
1190 list_del(&config->list);
1191 if (config->unbind) {
1192 DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
1193 config->unbind(config);
1194 /* may free memory for "c" */
1199 * usb_remove_config() - remove a configuration from a device.
1200 * @cdev: wraps the USB gadget
1201 * @config: the configuration
1203 * Drivers must call usb_gadget_disconnect before calling this function
1204 * to disconnect the device from the host and make sure the host will not
1205 * try to enumerate the device while we are changing the config list.
1207 void usb_remove_config(struct usb_composite_dev *cdev,
1208 struct usb_configuration *config)
1210 unsigned long flags;
1212 spin_lock_irqsave(&cdev->lock, flags);
1214 if (cdev->config == config)
1217 spin_unlock_irqrestore(&cdev->lock, flags);
1219 remove_config(cdev, config);
1222 /*-------------------------------------------------------------------------*/
1224 /* We support strings in multiple languages ... string descriptor zero
1225 * says which languages are supported. The typical case will be that
1226 * only one language (probably English) is used, with i18n handled on
1230 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
1232 const struct usb_gadget_strings *s;
1238 language = cpu_to_le16(s->language);
1239 for (tmp = buf; *tmp && tmp < &buf[USB_MAX_STRING_LEN]; tmp++) {
1240 if (*tmp == language)
1249 static int lookup_string(
1250 struct usb_gadget_strings **sp,
1256 struct usb_gadget_strings *s;
1261 if (s->language != language)
1263 value = usb_gadget_get_string(s, id, buf);
1270 static int get_string(struct usb_composite_dev *cdev,
1271 void *buf, u16 language, int id)
1273 struct usb_composite_driver *composite = cdev->driver;
1274 struct usb_gadget_string_container *uc;
1275 struct usb_configuration *c;
1276 struct usb_function *f;
1279 /* Yes, not only is USB's i18n support probably more than most
1280 * folk will ever care about ... also, it's all supported here.
1281 * (Except for UTF8 support for Unicode's "Astral Planes".)
1284 /* 0 == report all available language codes */
1286 struct usb_string_descriptor *s = buf;
1287 struct usb_gadget_strings **sp;
1290 s->bDescriptorType = USB_DT_STRING;
1292 sp = composite->strings;
1294 collect_langs(sp, s->wData);
1296 list_for_each_entry(c, &cdev->configs, list) {
1299 collect_langs(sp, s->wData);
1301 list_for_each_entry(f, &c->functions, list) {
1304 collect_langs(sp, s->wData);
1307 list_for_each_entry(uc, &cdev->gstrings, list) {
1308 struct usb_gadget_strings **sp;
1310 sp = get_containers_gs(uc);
1311 collect_langs(sp, s->wData);
1314 for (len = 0; len <= USB_MAX_STRING_LEN && s->wData[len]; len++)
1319 s->bLength = 2 * (len + 1);
1323 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
1324 struct usb_os_string *b = buf;
1325 b->bLength = sizeof(*b);
1326 b->bDescriptorType = USB_DT_STRING;
1328 sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
1329 "qwSignature size must be equal to qw_sign");
1330 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
1331 b->bMS_VendorCode = cdev->b_vendor_code;
1336 list_for_each_entry(uc, &cdev->gstrings, list) {
1337 struct usb_gadget_strings **sp;
1339 sp = get_containers_gs(uc);
1340 len = lookup_string(sp, buf, language, id);
1345 /* String IDs are device-scoped, so we look up each string
1346 * table we're told about. These lookups are infrequent;
1347 * simpler-is-better here.
1349 if (composite->strings) {
1350 len = lookup_string(composite->strings, buf, language, id);
1354 list_for_each_entry(c, &cdev->configs, list) {
1356 len = lookup_string(c->strings, buf, language, id);
1360 list_for_each_entry(f, &c->functions, list) {
1363 len = lookup_string(f->strings, buf, language, id);
1372 * usb_string_id() - allocate an unused string ID
1373 * @cdev: the device whose string descriptor IDs are being allocated
1374 * Context: single threaded during gadget setup
1376 * @usb_string_id() is called from bind() callbacks to allocate
1377 * string IDs. Drivers for functions, configurations, or gadgets will
1378 * then store that ID in the appropriate descriptors and string table.
1380 * All string identifier should be allocated using this,
1381 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1382 * that for example different functions don't wrongly assign different
1383 * meanings to the same identifier.
1385 int usb_string_id(struct usb_composite_dev *cdev)
1387 if (cdev->next_string_id < 254) {
1388 /* string id 0 is reserved by USB spec for list of
1389 * supported languages */
1390 /* 255 reserved as well? -- mina86 */
1391 cdev->next_string_id++;
1392 return cdev->next_string_id;
1396 EXPORT_SYMBOL_GPL(usb_string_id);
1399 * usb_string_ids_tab() - allocate unused string IDs in batch
1400 * @cdev: the device whose string descriptor IDs are being allocated
1401 * @str: an array of usb_string objects to assign numbers to
1402 * Context: single threaded during gadget setup
1404 * @usb_string_ids() is called from bind() callbacks to allocate
1405 * string IDs. Drivers for functions, configurations, or gadgets will
1406 * then copy IDs from the string table to the appropriate descriptors
1407 * and string table for other languages.
1409 * All string identifier should be allocated using this,
1410 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1411 * example different functions don't wrongly assign different meanings
1412 * to the same identifier.
1414 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
1416 int next = cdev->next_string_id;
1418 for (; str->s; ++str) {
1419 if (unlikely(next >= 254))
1424 cdev->next_string_id = next;
1428 EXPORT_SYMBOL_GPL(usb_string_ids_tab);
1430 static struct usb_gadget_string_container *copy_gadget_strings(
1431 struct usb_gadget_strings **sp, unsigned n_gstrings,
1434 struct usb_gadget_string_container *uc;
1435 struct usb_gadget_strings **gs_array;
1436 struct usb_gadget_strings *gs;
1437 struct usb_string *s;
1444 mem += sizeof(void *) * (n_gstrings + 1);
1445 mem += sizeof(struct usb_gadget_strings) * n_gstrings;
1446 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
1447 uc = kmalloc(mem, GFP_KERNEL);
1449 return ERR_PTR(-ENOMEM);
1450 gs_array = get_containers_gs(uc);
1452 stash += sizeof(void *) * (n_gstrings + 1);
1453 for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
1454 struct usb_string *org_s;
1456 gs_array[n_gs] = stash;
1457 gs = gs_array[n_gs];
1458 stash += sizeof(struct usb_gadget_strings);
1459 gs->language = sp[n_gs]->language;
1460 gs->strings = stash;
1461 org_s = sp[n_gs]->strings;
1463 for (n_s = 0; n_s < n_strings; n_s++) {
1465 stash += sizeof(struct usb_string);
1474 stash += sizeof(struct usb_string);
1477 gs_array[n_gs] = NULL;
1482 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1483 * @cdev: the device whose string descriptor IDs are being allocated
1485 * @sp: an array of usb_gadget_strings to attach.
1486 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1488 * This function will create a deep copy of usb_gadget_strings and usb_string
1489 * and attach it to the cdev. The actual string (usb_string.s) will not be
1490 * copied but only a referenced will be made. The struct usb_gadget_strings
1491 * array may contain multiple languages and should be NULL terminated.
1492 * The ->language pointer of each struct usb_gadget_strings has to contain the
1493 * same amount of entries.
1494 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1495 * usb_string entry of es-ES contains the translation of the first usb_string
1496 * entry of en-US. Therefore both entries become the same id assign.
1498 struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
1499 struct usb_gadget_strings **sp, unsigned n_strings)
1501 struct usb_gadget_string_container *uc;
1502 struct usb_gadget_strings **n_gs;
1503 unsigned n_gstrings = 0;
1507 for (i = 0; sp[i]; i++)
1511 return ERR_PTR(-EINVAL);
1513 uc = copy_gadget_strings(sp, n_gstrings, n_strings);
1515 return ERR_CAST(uc);
1517 n_gs = get_containers_gs(uc);
1518 ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
1522 for (i = 1; i < n_gstrings; i++) {
1523 struct usb_string *m_s;
1524 struct usb_string *s;
1527 m_s = n_gs[0]->strings;
1528 s = n_gs[i]->strings;
1529 for (n = 0; n < n_strings; n++) {
1535 list_add_tail(&uc->list, &cdev->gstrings);
1536 return n_gs[0]->strings;
1539 return ERR_PTR(ret);
1541 EXPORT_SYMBOL_GPL(usb_gstrings_attach);
1544 * usb_string_ids_n() - allocate unused string IDs in batch
1545 * @c: the device whose string descriptor IDs are being allocated
1546 * @n: number of string IDs to allocate
1547 * Context: single threaded during gadget setup
1549 * Returns the first requested ID. This ID and next @n-1 IDs are now
1550 * valid IDs. At least provided that @n is non-zero because if it
1551 * is, returns last requested ID which is now very useful information.
1553 * @usb_string_ids_n() is called from bind() callbacks to allocate
1554 * string IDs. Drivers for functions, configurations, or gadgets will
1555 * then store that ID in the appropriate descriptors and string table.
1557 * All string identifier should be allocated using this,
1558 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1559 * example different functions don't wrongly assign different meanings
1560 * to the same identifier.
1562 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1564 unsigned next = c->next_string_id;
1565 if (unlikely(n > 254 || (unsigned)next + n > 254))
1567 c->next_string_id += n;
1570 EXPORT_SYMBOL_GPL(usb_string_ids_n);
1572 /*-------------------------------------------------------------------------*/
1574 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1576 struct usb_composite_dev *cdev;
1578 if (req->status || req->actual != req->length)
1579 DBG((struct usb_composite_dev *) ep->driver_data,
1580 "setup complete --> %d, %d/%d\n",
1581 req->status, req->actual, req->length);
1584 * REVIST The same ep0 requests are shared with function drivers
1585 * so they don't have to maintain the same ->complete() stubs.
1587 * Because of that, we need to check for the validity of ->context
1588 * here, even though we know we've set it to something useful.
1593 cdev = req->context;
1595 if (cdev->req == req)
1596 cdev->setup_pending = false;
1597 else if (cdev->os_desc_req == req)
1598 cdev->os_desc_pending = false;
1600 WARN(1, "unknown request %p\n", req);
1603 static int composite_ep0_queue(struct usb_composite_dev *cdev,
1604 struct usb_request *req, gfp_t gfp_flags)
1608 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
1610 if (cdev->req == req)
1611 cdev->setup_pending = true;
1612 else if (cdev->os_desc_req == req)
1613 cdev->os_desc_pending = true;
1615 WARN(1, "unknown request %p\n", req);
1621 static int count_ext_compat(struct usb_configuration *c)
1626 for (i = 0; i < c->next_interface_id; ++i) {
1627 struct usb_function *f;
1630 f = c->interface[i];
1631 for (j = 0; j < f->os_desc_n; ++j) {
1632 struct usb_os_desc *d;
1634 if (i != f->os_desc_table[j].if_id)
1636 d = f->os_desc_table[j].os_desc;
1637 if (d && d->ext_compat_id)
1645 static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
1651 for (i = 0; i < c->next_interface_id; ++i) {
1652 struct usb_function *f;
1655 f = c->interface[i];
1656 for (j = 0; j < f->os_desc_n; ++j) {
1657 struct usb_os_desc *d;
1659 if (i != f->os_desc_table[j].if_id)
1661 d = f->os_desc_table[j].os_desc;
1662 if (d && d->ext_compat_id) {
1665 memcpy(buf, d->ext_compat_id, 16);
1673 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1681 static int count_ext_prop(struct usb_configuration *c, int interface)
1683 struct usb_function *f;
1686 f = c->interface[interface];
1687 for (j = 0; j < f->os_desc_n; ++j) {
1688 struct usb_os_desc *d;
1690 if (interface != f->os_desc_table[j].if_id)
1692 d = f->os_desc_table[j].os_desc;
1693 if (d && d->ext_compat_id)
1694 return d->ext_prop_count;
1699 static int len_ext_prop(struct usb_configuration *c, int interface)
1701 struct usb_function *f;
1702 struct usb_os_desc *d;
1705 res = 10; /* header length */
1706 f = c->interface[interface];
1707 for (j = 0; j < f->os_desc_n; ++j) {
1708 if (interface != f->os_desc_table[j].if_id)
1710 d = f->os_desc_table[j].os_desc;
1712 return min(res + d->ext_prop_len, 4096);
1717 static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
1719 struct usb_function *f;
1720 struct usb_os_desc *d;
1721 struct usb_os_desc_ext_prop *ext_prop;
1722 int j, count, n, ret;
1724 f = c->interface[interface];
1725 count = 10; /* header length */
1727 for (j = 0; j < f->os_desc_n; ++j) {
1728 if (interface != f->os_desc_table[j].if_id)
1730 d = f->os_desc_table[j].os_desc;
1732 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
1733 n = ext_prop->data_len +
1734 ext_prop->name_len + 14;
1735 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
1737 usb_ext_prop_put_size(buf, n);
1738 usb_ext_prop_put_type(buf, ext_prop->type);
1739 ret = usb_ext_prop_put_name(buf, ext_prop->name,
1740 ext_prop->name_len);
1743 switch (ext_prop->type) {
1744 case USB_EXT_PROP_UNICODE:
1745 case USB_EXT_PROP_UNICODE_ENV:
1746 case USB_EXT_PROP_UNICODE_LINK:
1747 usb_ext_prop_put_unicode(buf, ret,
1749 ext_prop->data_len);
1751 case USB_EXT_PROP_BINARY:
1752 usb_ext_prop_put_binary(buf, ret,
1754 ext_prop->data_len);
1756 case USB_EXT_PROP_LE32:
1757 /* not implemented */
1758 case USB_EXT_PROP_BE32:
1759 /* not implemented */
1772 * The setup() callback implements all the ep0 functionality that's
1773 * not handled lower down, in hardware or the hardware driver(like
1774 * device and endpoint feature flags, and their status). It's all
1775 * housekeeping for the gadget function we're implementing. Most of
1776 * the work is in config and function specific setup.
1779 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1781 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1782 struct usb_request *req = cdev->req;
1783 int value = -EOPNOTSUPP;
1785 u16 w_index = le16_to_cpu(ctrl->wIndex);
1786 u8 intf = w_index & 0xFF;
1787 u16 w_value = le16_to_cpu(ctrl->wValue);
1788 u16 w_length = le16_to_cpu(ctrl->wLength);
1789 struct usb_function *f = NULL;
1790 struct usb_function *iter;
1793 if (w_length > USB_COMP_EP0_BUFSIZ) {
1794 if (ctrl->bRequestType & USB_DIR_IN) {
1795 /* Cast away the const, we are going to overwrite on purpose. */
1796 __le16 *temp = (__le16 *)&ctrl->wLength;
1798 *temp = cpu_to_le16(USB_COMP_EP0_BUFSIZ);
1799 w_length = USB_COMP_EP0_BUFSIZ;
1805 /* partial re-init of the response message; the function or the
1806 * gadget might need to intercept e.g. a control-OUT completion
1807 * when we delegate to it.
1810 req->context = cdev;
1811 req->complete = composite_setup_complete;
1813 gadget->ep0->driver_data = cdev;
1816 * Don't let non-standard requests match any of the cases below
1819 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
1822 switch (ctrl->bRequest) {
1824 /* we handle all standard USB descriptors */
1825 case USB_REQ_GET_DESCRIPTOR:
1826 if (ctrl->bRequestType != USB_DIR_IN)
1828 switch (w_value >> 8) {
1831 cdev->desc.bNumConfigurations =
1832 count_configs(cdev, USB_DT_DEVICE);
1833 cdev->desc.bMaxPacketSize0 =
1834 cdev->gadget->ep0->maxpacket;
1835 if (gadget_is_superspeed(gadget)) {
1836 if (gadget->speed >= USB_SPEED_SUPER) {
1837 cdev->desc.bcdUSB = cpu_to_le16(0x0320);
1838 cdev->desc.bMaxPacketSize0 = 9;
1840 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1843 if (gadget->lpm_capable || cdev->use_webusb)
1844 cdev->desc.bcdUSB = cpu_to_le16(0x0201);
1846 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
1849 value = min(w_length, (u16) sizeof cdev->desc);
1850 memcpy(req->buf, &cdev->desc, value);
1852 case USB_DT_DEVICE_QUALIFIER:
1853 if (!gadget_is_dualspeed(gadget) ||
1854 gadget->speed >= USB_SPEED_SUPER)
1857 value = min_t(int, w_length,
1858 sizeof(struct usb_qualifier_descriptor));
1860 case USB_DT_OTHER_SPEED_CONFIG:
1861 if (!gadget_is_dualspeed(gadget) ||
1862 gadget->speed >= USB_SPEED_SUPER)
1866 value = config_desc(cdev, w_value);
1868 value = min(w_length, (u16) value);
1871 value = get_string(cdev, req->buf,
1872 w_index, w_value & 0xff);
1874 value = min(w_length, (u16) value);
1877 if (gadget_is_superspeed(gadget) ||
1878 gadget->lpm_capable || cdev->use_webusb) {
1879 value = bos_desc(cdev);
1880 value = min(w_length, (u16) value);
1884 if (gadget_is_otg(gadget)) {
1885 struct usb_configuration *config;
1886 int otg_desc_len = 0;
1889 config = cdev->config;
1891 config = list_first_entry(
1893 struct usb_configuration, list);
1897 if (gadget->otg_caps &&
1898 (gadget->otg_caps->otg_rev >= 0x0200))
1899 otg_desc_len += sizeof(
1900 struct usb_otg20_descriptor);
1902 otg_desc_len += sizeof(
1903 struct usb_otg_descriptor);
1905 value = min_t(int, w_length, otg_desc_len);
1906 memcpy(req->buf, config->descriptors[0], value);
1912 /* any number of configs can work */
1913 case USB_REQ_SET_CONFIGURATION:
1914 if (ctrl->bRequestType != 0)
1916 if (gadget_is_otg(gadget)) {
1917 if (gadget->a_hnp_support)
1918 DBG(cdev, "HNP available\n");
1919 else if (gadget->a_alt_hnp_support)
1920 DBG(cdev, "HNP on another port\n");
1922 VDBG(cdev, "HNP inactive\n");
1924 spin_lock(&cdev->lock);
1925 value = set_config(cdev, ctrl, w_value);
1926 spin_unlock(&cdev->lock);
1928 case USB_REQ_GET_CONFIGURATION:
1929 if (ctrl->bRequestType != USB_DIR_IN)
1932 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1934 *(u8 *)req->buf = 0;
1935 value = min(w_length, (u16) 1);
1938 /* function drivers must handle get/set altsetting */
1939 case USB_REQ_SET_INTERFACE:
1940 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1942 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1944 f = cdev->config->interface[intf];
1949 * If there's no get_alt() method, we know only altsetting zero
1950 * works. There is no need to check if set_alt() is not NULL
1951 * as we check this in usb_add_function().
1953 if (w_value && !f->get_alt)
1956 spin_lock(&cdev->lock);
1957 value = f->set_alt(f, w_index, w_value);
1958 if (value == USB_GADGET_DELAYED_STATUS) {
1960 "%s: interface %d (%s) requested delayed status\n",
1961 __func__, intf, f->name);
1962 cdev->delayed_status++;
1963 DBG(cdev, "delayed_status count %d\n",
1964 cdev->delayed_status);
1966 spin_unlock(&cdev->lock);
1968 case USB_REQ_GET_INTERFACE:
1969 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1971 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1973 f = cdev->config->interface[intf];
1976 /* lots of interfaces only need altsetting zero... */
1977 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1980 *((u8 *)req->buf) = value;
1981 value = min(w_length, (u16) 1);
1983 case USB_REQ_GET_STATUS:
1984 if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
1985 (w_index == OTG_STS_SELECTOR)) {
1986 if (ctrl->bRequestType != (USB_DIR_IN |
1989 *((u8 *)req->buf) = gadget->host_request_flag;
1995 * USB 3.0 additions:
1996 * Function driver should handle get_status request. If such cb
1997 * wasn't supplied we respond with default value = 0
1998 * Note: function driver should supply such cb only for the
1999 * first interface of the function
2001 if (!gadget_is_superspeed(gadget))
2003 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
2005 value = 2; /* This is the length of the get_status reply */
2006 put_unaligned_le16(0, req->buf);
2007 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
2009 f = cdev->config->interface[intf];
2013 if (f->get_status) {
2014 status = f->get_status(f);
2018 /* Set D0 and D1 bits based on func wakeup capability */
2019 if (f->config->bmAttributes & USB_CONFIG_ATT_WAKEUP) {
2020 status |= USB_INTRF_STAT_FUNC_RW_CAP;
2021 if (f->func_wakeup_armed)
2022 status |= USB_INTRF_STAT_FUNC_RW;
2026 put_unaligned_le16(status & 0x0000ffff, req->buf);
2029 * Function drivers should handle SetFeature/ClearFeature
2030 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
2031 * only for the first interface of the function
2033 case USB_REQ_CLEAR_FEATURE:
2034 case USB_REQ_SET_FEATURE:
2035 if (!gadget_is_superspeed(gadget))
2037 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
2040 case USB_INTRF_FUNC_SUSPEND:
2041 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
2043 f = cdev->config->interface[intf];
2047 if (f->func_suspend) {
2048 value = f->func_suspend(f, w_index >> 8);
2049 /* SetFeature(FUNCTION_SUSPEND) */
2050 } else if (ctrl->bRequest == USB_REQ_SET_FEATURE) {
2051 if (!(f->config->bmAttributes &
2052 USB_CONFIG_ATT_WAKEUP) &&
2053 (w_index & USB_INTRF_FUNC_SUSPEND_RW))
2056 f->func_wakeup_armed = !!(w_index &
2057 USB_INTRF_FUNC_SUSPEND_RW);
2059 if (w_index & USB_INTRF_FUNC_SUSPEND_LP) {
2060 if (f->suspend && !f->func_suspended) {
2062 f->func_suspended = true;
2065 * Handle cases where host sends function resume
2066 * through SetFeature(FUNCTION_SUSPEND) but low power
2070 if (f->resume && f->func_suspended) {
2072 f->func_suspended = false;
2075 /* ClearFeature(FUNCTION_SUSPEND) */
2076 } else if (ctrl->bRequest == USB_REQ_CLEAR_FEATURE) {
2077 f->func_wakeup_armed = false;
2079 if (f->resume && f->func_suspended) {
2081 f->func_suspended = false;
2087 "func_suspend() returned error %d\n",
2097 * OS descriptors handling
2099 if (cdev->use_os_string && cdev->os_desc_config &&
2100 (ctrl->bRequestType & USB_TYPE_VENDOR) &&
2101 ctrl->bRequest == cdev->b_vendor_code) {
2102 struct usb_configuration *os_desc_cfg;
2107 req = cdev->os_desc_req;
2108 req->context = cdev;
2109 req->complete = composite_setup_complete;
2111 os_desc_cfg = cdev->os_desc_config;
2112 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
2113 memset(buf, 0, w_length);
2115 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2116 case USB_RECIP_DEVICE:
2117 if (w_index != 0x4 || (w_value >> 8))
2120 /* Number of ext compat interfaces */
2121 count = count_ext_compat(os_desc_cfg);
2123 count *= 24; /* 24 B/ext compat desc */
2124 count += 16; /* header */
2125 put_unaligned_le32(count, buf);
2127 if (w_length > 0x10) {
2128 value = fill_ext_compat(os_desc_cfg, buf);
2129 value = min_t(u16, w_length, value);
2132 case USB_RECIP_INTERFACE:
2133 if (w_index != 0x5 || (w_value >> 8))
2135 interface = w_value & 0xFF;
2136 if (interface >= MAX_CONFIG_INTERFACES ||
2137 !os_desc_cfg->interface[interface])
2140 count = count_ext_prop(os_desc_cfg,
2142 put_unaligned_le16(count, buf + 8);
2143 count = len_ext_prop(os_desc_cfg,
2145 put_unaligned_le32(count, buf);
2147 if (w_length > 0x0A) {
2148 value = fill_ext_prop(os_desc_cfg,
2151 value = min_t(u16, w_length, value);
2160 * WebUSB URL descriptor handling, following:
2161 * https://wicg.github.io/webusb/#device-requests
2163 if (cdev->use_webusb &&
2164 ctrl->bRequestType == (USB_DIR_IN | USB_TYPE_VENDOR) &&
2165 w_index == WEBUSB_GET_URL &&
2166 w_value == WEBUSB_LANDING_PAGE_PRESENT &&
2167 ctrl->bRequest == cdev->b_webusb_vendor_code) {
2168 unsigned int landing_page_length;
2169 unsigned int landing_page_offset;
2170 struct webusb_url_descriptor *url_descriptor =
2171 (struct webusb_url_descriptor *)cdev->req->buf;
2173 url_descriptor->bDescriptorType = WEBUSB_URL_DESCRIPTOR_TYPE;
2175 if (strncasecmp(cdev->landing_page, "https://", 8) == 0) {
2176 landing_page_offset = 8;
2177 url_descriptor->bScheme = WEBUSB_URL_SCHEME_HTTPS;
2178 } else if (strncasecmp(cdev->landing_page, "http://", 7) == 0) {
2179 landing_page_offset = 7;
2180 url_descriptor->bScheme = WEBUSB_URL_SCHEME_HTTP;
2182 landing_page_offset = 0;
2183 url_descriptor->bScheme = WEBUSB_URL_SCHEME_NONE;
2186 landing_page_length = strnlen(cdev->landing_page,
2187 sizeof(url_descriptor->URL)
2188 - WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH + landing_page_offset);
2190 if (w_length < WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH + landing_page_length)
2191 landing_page_length = w_length
2192 - WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH + landing_page_offset;
2194 memcpy(url_descriptor->URL,
2195 cdev->landing_page + landing_page_offset,
2196 landing_page_length - landing_page_offset);
2197 url_descriptor->bLength = landing_page_length
2198 - landing_page_offset + WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH;
2200 value = url_descriptor->bLength;
2206 "non-core control req%02x.%02x v%04x i%04x l%d\n",
2207 ctrl->bRequestType, ctrl->bRequest,
2208 w_value, w_index, w_length);
2210 /* functions always handle their interfaces and endpoints...
2211 * punt other recipients (other, WUSB, ...) to the current
2212 * configuration code.
2215 list_for_each_entry(f, &cdev->config->functions, list)
2217 f->req_match(f, ctrl, false))
2220 struct usb_configuration *c;
2221 list_for_each_entry(c, &cdev->configs, list)
2222 list_for_each_entry(f, &c->functions, list)
2224 f->req_match(f, ctrl, true))
2229 switch (ctrl->bRequestType & USB_RECIP_MASK) {
2230 case USB_RECIP_INTERFACE:
2231 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
2233 f = cdev->config->interface[intf];
2236 case USB_RECIP_ENDPOINT:
2239 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
2240 list_for_each_entry(iter, &cdev->config->functions, list) {
2241 if (test_bit(endp, iter->endpoints)) {
2250 value = f->setup(f, ctrl);
2252 struct usb_configuration *c;
2258 /* try current config's setup */
2260 value = c->setup(c, ctrl);
2264 /* try the only function in the current config */
2265 if (!list_is_singular(&c->functions))
2267 f = list_first_entry(&c->functions, struct usb_function,
2270 value = f->setup(f, ctrl);
2277 /* respond with data transfer before status phase? */
2278 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
2279 req->length = value;
2280 req->context = cdev;
2281 req->zero = value < w_length;
2282 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
2284 DBG(cdev, "ep_queue --> %d\n", value);
2286 composite_setup_complete(gadget->ep0, req);
2288 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
2290 "%s: Delayed status not supported for w_length != 0",
2295 /* device either stalls (value < 0) or reports success */
2299 static void __composite_disconnect(struct usb_gadget *gadget)
2301 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2302 unsigned long flags;
2304 /* REVISIT: should we have config and device level
2305 * disconnect callbacks?
2307 spin_lock_irqsave(&cdev->lock, flags);
2308 cdev->suspended = 0;
2311 if (cdev->driver->disconnect)
2312 cdev->driver->disconnect(cdev);
2313 spin_unlock_irqrestore(&cdev->lock, flags);
2316 void composite_disconnect(struct usb_gadget *gadget)
2318 usb_gadget_vbus_draw(gadget, 0);
2319 __composite_disconnect(gadget);
2322 void composite_reset(struct usb_gadget *gadget)
2325 * Section 1.4.13 Standard Downstream Port of the USB battery charging
2326 * specification v1.2 states that a device connected on a SDP shall only
2327 * draw at max 100mA while in a connected, but unconfigured state.
2329 usb_gadget_vbus_draw(gadget, 100);
2330 __composite_disconnect(gadget);
2333 /*-------------------------------------------------------------------------*/
2335 static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
2338 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
2339 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2341 return sprintf(buf, "%d\n", cdev->suspended);
2343 static DEVICE_ATTR_RO(suspended);
2345 static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
2347 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2348 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2349 struct usb_string *dev_str = gstr->strings;
2351 /* composite_disconnect() must already have been called
2352 * by the underlying peripheral controller driver!
2353 * so there's no i/o concurrency that could affect the
2354 * state protected by cdev->lock.
2356 WARN_ON(cdev->config);
2358 while (!list_empty(&cdev->configs)) {
2359 struct usb_configuration *c;
2360 c = list_first_entry(&cdev->configs,
2361 struct usb_configuration, list);
2362 remove_config(cdev, c);
2364 if (cdev->driver->unbind && unbind_driver)
2365 cdev->driver->unbind(cdev);
2367 composite_dev_cleanup(cdev);
2369 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
2370 dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
2372 kfree(cdev->def_manufacturer);
2374 set_gadget_data(gadget, NULL);
2377 static void composite_unbind(struct usb_gadget *gadget)
2379 __composite_unbind(gadget, true);
2382 static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
2383 const struct usb_device_descriptor *old)
2393 * these variables may have been set in
2394 * usb_composite_overwrite_options()
2396 idVendor = new->idVendor;
2397 idProduct = new->idProduct;
2398 bcdDevice = new->bcdDevice;
2399 iSerialNumber = new->iSerialNumber;
2400 iManufacturer = new->iManufacturer;
2401 iProduct = new->iProduct;
2405 new->idVendor = idVendor;
2407 new->idProduct = idProduct;
2409 new->bcdDevice = bcdDevice;
2411 new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
2413 new->iSerialNumber = iSerialNumber;
2415 new->iManufacturer = iManufacturer;
2417 new->iProduct = iProduct;
2420 int composite_dev_prepare(struct usb_composite_driver *composite,
2421 struct usb_composite_dev *cdev)
2423 struct usb_gadget *gadget = cdev->gadget;
2426 /* preallocate control response and buffer */
2427 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
2431 cdev->req->buf = kzalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
2432 if (!cdev->req->buf)
2435 ret = device_create_file(&gadget->dev, &dev_attr_suspended);
2439 cdev->req->complete = composite_setup_complete;
2440 cdev->req->context = cdev;
2441 gadget->ep0->driver_data = cdev;
2443 cdev->driver = composite;
2446 * As per USB compliance update, a device that is actively drawing
2447 * more than 100mA from USB must report itself as bus-powered in
2448 * the GetStatus(DEVICE) call.
2450 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
2451 usb_gadget_set_selfpowered(gadget);
2453 /* interface and string IDs start at zero via kzalloc.
2454 * we force endpoints to start unassigned; few controller
2455 * drivers will zero ep->driver_data.
2457 usb_ep_autoconfig_reset(gadget);
2460 kfree(cdev->req->buf);
2462 usb_ep_free_request(gadget->ep0, cdev->req);
2467 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
2472 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
2473 if (!cdev->os_desc_req) {
2478 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
2480 if (!cdev->os_desc_req->buf) {
2482 usb_ep_free_request(ep0, cdev->os_desc_req);
2485 cdev->os_desc_req->context = cdev;
2486 cdev->os_desc_req->complete = composite_setup_complete;
2491 void composite_dev_cleanup(struct usb_composite_dev *cdev)
2493 struct usb_gadget_string_container *uc, *tmp;
2494 struct usb_ep *ep, *tmp_ep;
2496 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
2497 list_del(&uc->list);
2500 if (cdev->os_desc_req) {
2501 if (cdev->os_desc_pending)
2502 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
2504 kfree(cdev->os_desc_req->buf);
2505 cdev->os_desc_req->buf = NULL;
2506 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
2507 cdev->os_desc_req = NULL;
2510 if (cdev->setup_pending)
2511 usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
2513 kfree(cdev->req->buf);
2514 cdev->req->buf = NULL;
2515 usb_ep_free_request(cdev->gadget->ep0, cdev->req);
2518 cdev->next_string_id = 0;
2519 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
2522 * Some UDC backends have a dynamic EP allocation scheme.
2524 * In that case, the dispose() callback is used to notify the
2525 * backend that the EPs are no longer in use.
2527 * Note: The UDC backend can remove the EP from the ep_list as
2528 * a result, so we need to use the _safe list iterator.
2530 list_for_each_entry_safe(ep, tmp_ep,
2531 &cdev->gadget->ep_list, ep_list) {
2532 if (ep->ops->dispose)
2533 ep->ops->dispose(ep);
2537 static int composite_bind(struct usb_gadget *gadget,
2538 struct usb_gadget_driver *gdriver)
2540 struct usb_composite_dev *cdev;
2541 struct usb_composite_driver *composite = to_cdriver(gdriver);
2542 int status = -ENOMEM;
2544 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
2548 spin_lock_init(&cdev->lock);
2549 cdev->gadget = gadget;
2550 set_gadget_data(gadget, cdev);
2551 INIT_LIST_HEAD(&cdev->configs);
2552 INIT_LIST_HEAD(&cdev->gstrings);
2554 status = composite_dev_prepare(composite, cdev);
2558 /* composite gadget needs to assign strings for whole device (like
2559 * serial number), register function drivers, potentially update
2560 * power state and consumption, etc
2562 status = composite->bind(cdev);
2566 if (cdev->use_os_string) {
2567 status = composite_os_desc_req_prepare(cdev, gadget->ep0);
2572 update_unchanged_dev_desc(&cdev->desc, composite->dev);
2574 /* has userspace failed to provide a serial number? */
2575 if (composite->needs_serial && !cdev->desc.iSerialNumber)
2576 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
2578 INFO(cdev, "%s ready\n", composite->name);
2582 __composite_unbind(gadget, false);
2586 /*-------------------------------------------------------------------------*/
2588 void composite_suspend(struct usb_gadget *gadget)
2590 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2591 struct usb_function *f;
2593 /* REVISIT: should we have config level
2594 * suspend/resume callbacks?
2596 DBG(cdev, "suspend\n");
2598 list_for_each_entry(f, &cdev->config->functions, list) {
2603 if (cdev->driver->suspend)
2604 cdev->driver->suspend(cdev);
2606 cdev->suspended = 1;
2608 usb_gadget_set_selfpowered(gadget);
2609 usb_gadget_vbus_draw(gadget, 2);
2612 void composite_resume(struct usb_gadget *gadget)
2614 struct usb_composite_dev *cdev = get_gadget_data(gadget);
2615 struct usb_function *f;
2618 /* REVISIT: should we have config level
2619 * suspend/resume callbacks?
2621 DBG(cdev, "resume\n");
2622 if (cdev->driver->resume)
2623 cdev->driver->resume(cdev);
2625 list_for_each_entry(f, &cdev->config->functions, list) {
2627 * Check for func_suspended flag to see if the function is
2628 * in USB3 FUNCTION_SUSPEND state. In this case resume is
2629 * done via FUNCTION_SUSPEND feature selector.
2631 if (f->resume && !f->func_suspended)
2635 maxpower = cdev->config->MaxPower ?
2636 cdev->config->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
2637 if (gadget->speed < USB_SPEED_SUPER)
2638 maxpower = min(maxpower, 500U);
2640 maxpower = min(maxpower, 900U);
2642 if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW)
2643 usb_gadget_clear_selfpowered(gadget);
2645 usb_gadget_vbus_draw(gadget, maxpower);
2647 maxpower = CONFIG_USB_GADGET_VBUS_DRAW;
2648 maxpower = min(maxpower, 100U);
2649 usb_gadget_vbus_draw(gadget, maxpower);
2652 cdev->suspended = 0;
2655 /*-------------------------------------------------------------------------*/
2657 static const struct usb_gadget_driver composite_driver_template = {
2658 .bind = composite_bind,
2659 .unbind = composite_unbind,
2661 .setup = composite_setup,
2662 .reset = composite_reset,
2663 .disconnect = composite_disconnect,
2665 .suspend = composite_suspend,
2666 .resume = composite_resume,
2669 .owner = THIS_MODULE,
2674 * usb_composite_probe() - register a composite driver
2675 * @driver: the driver to register
2677 * Context: single threaded during gadget setup
2679 * This function is used to register drivers using the composite driver
2680 * framework. The return value is zero, or a negative errno value.
2681 * Those values normally come from the driver's @bind method, which does
2682 * all the work of setting up the driver to match the hardware.
2684 * On successful return, the gadget is ready to respond to requests from
2685 * the host, unless one of its components invokes usb_gadget_disconnect()
2686 * while it was binding. That would usually be done in order to wait for
2687 * some userspace participation.
2689 int usb_composite_probe(struct usb_composite_driver *driver)
2691 struct usb_gadget_driver *gadget_driver;
2693 if (!driver || !driver->dev || !driver->bind)
2697 driver->name = "composite";
2699 driver->gadget_driver = composite_driver_template;
2700 gadget_driver = &driver->gadget_driver;
2702 gadget_driver->function = (char *) driver->name;
2703 gadget_driver->driver.name = driver->name;
2704 gadget_driver->max_speed = driver->max_speed;
2706 return usb_gadget_register_driver(gadget_driver);
2708 EXPORT_SYMBOL_GPL(usb_composite_probe);
2711 * usb_composite_unregister() - unregister a composite driver
2712 * @driver: the driver to unregister
2714 * This function is used to unregister drivers using the composite
2717 void usb_composite_unregister(struct usb_composite_driver *driver)
2719 usb_gadget_unregister_driver(&driver->gadget_driver);
2721 EXPORT_SYMBOL_GPL(usb_composite_unregister);
2724 * usb_composite_setup_continue() - Continue with the control transfer
2725 * @cdev: the composite device who's control transfer was kept waiting
2727 * This function must be called by the USB function driver to continue
2728 * with the control transfer's data/status stage in case it had requested to
2729 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2730 * can request the composite framework to delay the setup request's data/status
2731 * stages by returning USB_GADGET_DELAYED_STATUS.
2733 void usb_composite_setup_continue(struct usb_composite_dev *cdev)
2736 struct usb_request *req = cdev->req;
2737 unsigned long flags;
2739 DBG(cdev, "%s\n", __func__);
2740 spin_lock_irqsave(&cdev->lock, flags);
2742 if (cdev->delayed_status == 0) {
2743 WARN(cdev, "%s: Unexpected call\n", __func__);
2745 } else if (--cdev->delayed_status == 0) {
2746 DBG(cdev, "%s: Completing delayed status\n", __func__);
2748 req->context = cdev;
2749 value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
2751 DBG(cdev, "ep_queue --> %d\n", value);
2753 composite_setup_complete(cdev->gadget->ep0, req);
2757 spin_unlock_irqrestore(&cdev->lock, flags);
2759 EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
2761 static char *composite_default_mfr(struct usb_gadget *gadget)
2763 return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname,
2764 init_utsname()->release, gadget->name);
2767 void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
2768 struct usb_composite_overwrite *covr)
2770 struct usb_device_descriptor *desc = &cdev->desc;
2771 struct usb_gadget_strings *gstr = cdev->driver->strings[0];
2772 struct usb_string *dev_str = gstr->strings;
2775 desc->idVendor = cpu_to_le16(covr->idVendor);
2777 if (covr->idProduct)
2778 desc->idProduct = cpu_to_le16(covr->idProduct);
2780 if (covr->bcdDevice)
2781 desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
2783 if (covr->serial_number) {
2784 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
2785 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
2787 if (covr->manufacturer) {
2788 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2789 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
2791 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
2792 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
2793 cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
2794 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
2797 if (covr->product) {
2798 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
2799 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
2802 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
2804 MODULE_LICENSE("GPL");
2805 MODULE_AUTHOR("David Brownell");