1 // SPDX-License-Identifier: GPL-2.0+
3 * composite.c - infrastructure for Composite USB Gadgets
5 * Copyright (C) 2006-2008 David Brownell
11 #include <dm/devres.h>
12 #include <linux/bitops.h>
13 #include <linux/bug.h>
14 #include <linux/usb/composite.h>
15 #include "u_os_desc.h"
17 #define USB_BUFSIZ 4096
19 /* Helper type for accessing packed u16 pointers */
20 typedef struct { __le16 val; } __packed __le16_packed;
22 static struct usb_composite_driver *composite;
23 static struct usb_configuration *os_desc_config;
25 /* Microsoft OS String Descriptor */
26 static char qw_sign_buf[OS_STRING_QW_SIGN_LEN / 2] = {'M', 'S', 'F', 'T', '1', '0', '0'};
28 static inline void le16_add_cpu_packed(__le16_packed *var, u16 val)
30 var->val = cpu_to_le16(le16_to_cpu(var->val) + val);
34 * struct usb_os_string - represents OS String to be reported by a gadget
35 * @bLength: total length of the entire descritor, always 0x12
36 * @bDescriptorType: USB_DT_STRING
37 * @qwSignature: the OS String proper
38 * @bMS_VendorCode: code used by the host for subsequent requests
39 * @bPad: not used, must be zero
41 struct usb_os_string {
44 __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
50 * usb_add_function() - add a function to a configuration
51 * @config: the configuration
52 * @function: the function being added
53 * Context: single threaded during gadget setup
55 * After initialization, each configuration must have one or more
56 * functions added to it. Adding a function involves calling its @bind()
57 * method to allocate resources such as interface and string identifiers
60 * This function returns the value of the function's bind(), which is
61 * zero for success else a negative errno value.
63 int usb_add_function(struct usb_configuration *config,
64 struct usb_function *function)
68 debug("adding '%s'/%p to config '%s'/%p\n",
69 function->name, function,
70 config->label, config);
72 if (!function->set_alt || !function->disable)
75 function->config = config;
76 list_add_tail(&function->list, &config->functions);
79 value = function->bind(config, function);
81 list_del(&function->list);
82 function->config = NULL;
87 if (!config->fullspeed && function->descriptors)
88 config->fullspeed = 1;
89 if (!config->highspeed && function->hs_descriptors)
90 config->highspeed = 1;
91 if (!config->superspeed && function->ss_descriptors)
92 config->superspeed = 1;
96 debug("adding '%s'/%p --> %d\n",
97 function->name, function, value);
102 * usb_function_deactivate - prevent function and gadget enumeration
103 * @function: the function that isn't yet ready to respond
105 * Blocks response of the gadget driver to host enumeration by
106 * preventing the data line pullup from being activated. This is
107 * normally called during @bind() processing to change from the
108 * initial "ready to respond" state, or when a required resource
111 * For example, drivers that serve as a passthrough to a userspace
112 * daemon can block enumeration unless that daemon (such as an OBEX,
113 * MTP, or print server) is ready to handle host requests.
115 * Not all systems support software control of their USB peripheral
118 * Returns zero on success, else negative errno.
120 int usb_function_deactivate(struct usb_function *function)
122 struct usb_composite_dev *cdev = function->config->cdev;
125 if (cdev->deactivations == 0)
126 status = usb_gadget_disconnect(cdev->gadget);
128 cdev->deactivations++;
134 * usb_function_activate - allow function and gadget enumeration
135 * @function: function on which usb_function_activate() was called
137 * Reverses effect of usb_function_deactivate(). If no more functions
138 * are delaying their activation, the gadget driver will respond to
139 * host enumeration procedures.
141 * Returns zero on success, else negative errno.
143 int usb_function_activate(struct usb_function *function)
145 struct usb_composite_dev *cdev = function->config->cdev;
148 if (cdev->deactivations == 0)
151 cdev->deactivations--;
152 if (cdev->deactivations == 0)
153 status = usb_gadget_connect(cdev->gadget);
160 * usb_interface_id() - allocate an unused interface ID
161 * @config: configuration associated with the interface
162 * @function: function handling the interface
163 * Context: single threaded during gadget setup
165 * usb_interface_id() is called from usb_function.bind() callbacks to
166 * allocate new interface IDs. The function driver will then store that
167 * ID in interface, association, CDC union, and other descriptors. It
168 * will also handle any control requests targetted at that interface,
169 * particularly changing its altsetting via set_alt(). There may
170 * also be class-specific or vendor-specific requests to handle.
172 * All interface identifier should be allocated using this routine, to
173 * ensure that for example different functions don't wrongly assign
174 * different meanings to the same identifier. Note that since interface
175 * identifers are configuration-specific, functions used in more than
176 * one configuration (or more than once in a given configuration) need
177 * multiple versions of the relevant descriptors.
179 * Returns the interface ID which was allocated; or -ENODEV if no
180 * more interface IDs can be allocated.
182 int usb_interface_id(struct usb_configuration *config,
183 struct usb_function *function)
185 unsigned char id = config->next_interface_id;
187 if (id < MAX_CONFIG_INTERFACES) {
188 config->interface[id] = function;
189 config->next_interface_id = id + 1;
195 static int config_buf(struct usb_configuration *config,
196 enum usb_device_speed speed, void *buf, u8 type)
198 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
199 void *next = buf + USB_DT_CONFIG_SIZE;
200 struct usb_descriptor_header **descriptors;
201 struct usb_config_descriptor *c;
203 struct usb_function *f;
205 /* write the config descriptor */
207 c->bLength = USB_DT_CONFIG_SIZE;
208 c->bDescriptorType = type;
210 c->bNumInterfaces = config->next_interface_id;
211 c->bConfigurationValue = config->bConfigurationValue;
212 c->iConfiguration = config->iConfiguration;
213 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
214 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
216 /* There may be e.g. OTG descriptors */
217 if (config->descriptors) {
218 status = usb_descriptor_fillbuf(next, len,
219 config->descriptors);
226 /* add each function's descriptors */
227 list_for_each_entry(f, &config->functions, list) {
228 if (speed == USB_SPEED_SUPER)
229 descriptors = f->ss_descriptors;
230 else if (speed == USB_SPEED_HIGH)
231 descriptors = f->hs_descriptors;
233 descriptors = f->descriptors;
236 status = usb_descriptor_fillbuf(next, len,
237 (const struct usb_descriptor_header **) descriptors);
245 c->wTotalLength = cpu_to_le16(len);
249 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
251 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
252 struct usb_gadget *gadget = cdev->gadget;
253 u8 type = w_value >> 8;
255 struct usb_configuration *c;
256 struct list_head *pos;
258 if (gadget_is_superspeed(gadget)) {
259 speed = gadget->speed;
260 } else if (gadget_is_dualspeed(gadget)) {
261 if (gadget->speed == USB_SPEED_HIGH)
263 if (type == USB_DT_OTHER_SPEED_CONFIG)
266 speed = USB_SPEED_HIGH;
271 pos = &cdev->configs;
272 c = cdev->os_desc_config;
276 while ((pos = pos->next) != &cdev->configs) {
277 c = list_entry(pos, typeof(*c), list);
279 /* skip OS Descriptors config which is handled separately */
280 if (c == cdev->os_desc_config)
284 if (speed == USB_SPEED_SUPER) {
287 } else if (speed == USB_SPEED_HIGH) {
295 return config_buf(c, speed, cdev->req->buf, type);
301 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
303 struct usb_gadget *gadget = cdev->gadget;
307 struct usb_configuration *c;
309 if (gadget->speed == USB_SPEED_SUPER)
312 if (gadget_is_dualspeed(gadget)) {
313 if (gadget->speed == USB_SPEED_HIGH)
315 if (type == USB_DT_DEVICE_QUALIFIER)
318 list_for_each_entry(c, &cdev->configs, list) {
319 /* ignore configs that won't work at this speed */
335 static void device_qual(struct usb_composite_dev *cdev)
337 struct usb_qualifier_descriptor *qual = cdev->req->buf;
339 qual->bLength = sizeof(*qual);
340 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
341 /* POLICY: same bcdUSB and device type info at both speeds */
342 qual->bcdUSB = cdev->desc.bcdUSB;
343 qual->bDeviceClass = cdev->desc.bDeviceClass;
344 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
345 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
346 /* ASSUME same EP0 fifo size at both speeds */
347 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
348 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
352 static void reset_config(struct usb_composite_dev *cdev)
354 struct usb_function *f;
356 debug("%s:\n", __func__);
358 list_for_each_entry(f, &cdev->config->functions, list) {
362 bitmap_zero(f->endpoints, 32);
367 static int set_config(struct usb_composite_dev *cdev,
368 const struct usb_ctrlrequest *ctrl, unsigned number)
370 struct usb_gadget *gadget = cdev->gadget;
371 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
372 struct usb_descriptor_header **descriptors;
373 int result = -EINVAL;
374 struct usb_endpoint_descriptor *ep;
375 struct usb_configuration *c = NULL;
378 struct usb_function *f;
384 list_for_each_entry(c, &cdev->configs, list) {
385 if (c->bConfigurationValue == number) {
395 debug("%s: %s speed config #%d: %s\n", __func__,
397 switch (gadget->speed) {
407 case USB_SPEED_SUPER:
415 }), number, c ? c->label : "unconfigured");
422 /* Initialize all interfaces by setting them to altsetting zero. */
423 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
424 f = c->interface[tmp];
429 * Record which endpoints are used by the function. This is used
430 * to dispatch control requests targeted at that endpoint to the
431 * function's setup callback instead of the current
432 * configuration's setup callback.
434 if (gadget->speed == USB_SPEED_SUPER)
435 descriptors = f->ss_descriptors;
436 else if (gadget->speed == USB_SPEED_HIGH)
437 descriptors = f->hs_descriptors;
439 descriptors = f->descriptors;
441 for (; *descriptors; ++descriptors) {
442 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
445 ep = (struct usb_endpoint_descriptor *)*descriptors;
446 addr = ((ep->bEndpointAddress & 0x80) >> 3)
447 | (ep->bEndpointAddress & 0x0f);
448 generic_set_bit(addr, f->endpoints);
451 result = f->set_alt(f, tmp, 0);
453 debug("interface %d (%s/%p) alt 0 --> %d\n",
454 tmp, f->name, f, result);
461 /* when we return, be sure our power usage is valid */
462 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
464 usb_gadget_vbus_draw(gadget, power);
469 * usb_add_config() - add a configuration to a device.
470 * @cdev: wraps the USB gadget
471 * @config: the configuration, with bConfigurationValue assigned
472 * Context: single threaded during gadget setup
474 * One of the main tasks of a composite driver's bind() routine is to
475 * add each of the configurations it supports, using this routine.
477 * This function returns the value of the configuration's bind(), which
478 * is zero for success else a negative errno value. Binding configurations
479 * assigns global resources including string IDs, and per-configuration
480 * resources such as interface IDs and endpoints.
482 int usb_add_config(struct usb_composite_dev *cdev,
483 struct usb_configuration *config)
485 int status = -EINVAL;
486 struct usb_configuration *c;
487 struct usb_function *f;
490 debug("%s: adding config #%u '%s'/%p\n", __func__,
491 config->bConfigurationValue,
492 config->label, config);
494 if (!config->bConfigurationValue || !config->bind)
497 /* Prevent duplicate configuration identifiers */
498 list_for_each_entry(c, &cdev->configs, list) {
499 if (c->bConfigurationValue == config->bConfigurationValue) {
506 list_add_tail(&config->list, &cdev->configs);
508 INIT_LIST_HEAD(&config->functions);
509 config->next_interface_id = 0;
511 status = config->bind(config);
513 list_del(&config->list);
516 debug("cfg %d/%p speeds:%s%s%s\n",
517 config->bConfigurationValue, config,
518 config->superspeed ? " super" : "",
519 config->highspeed ? " high" : "",
521 ? (gadget_is_dualspeed(cdev->gadget)
526 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
527 f = config->interface[i];
530 debug("%s: interface %d = %s/%p\n",
531 __func__, i, f->name, f);
536 * If one function of config is not super speed capable,
537 * force the gadget to be high speed so controller driver
538 * can init HW to be USB 2.0
540 if (gadget_is_superspeed(cdev->gadget)) {
541 list_for_each_entry(f, &config->functions, list) {
542 if (!f->ss_descriptors)
543 cdev->gadget->max_speed =
548 usb_ep_autoconfig_reset(cdev->gadget);
550 os_desc_config = config;
551 cdev->os_desc_config = os_desc_config;
555 debug("added config '%s'/%u --> %d\n", config->label,
556 config->bConfigurationValue, status);
561 * We support strings in multiple languages ... string descriptor zero
562 * says which languages are supported. The typical case will be that
563 * only one language (probably English) is used, with I18N handled on
567 static void collect_langs(struct usb_gadget_strings **sp, void *buf)
569 const struct usb_gadget_strings *s;
572 __le16_packed *end = (buf + 252);
576 language = cpu_to_le16(s->language);
577 for (tmp = buf; tmp->val && tmp < end; tmp++) {
578 if (tmp->val == language)
587 static int lookup_string(
588 struct usb_gadget_strings **sp,
595 struct usb_gadget_strings *s;
599 if (s->language != language)
601 value = usb_gadget_get_string(s, id, buf);
608 static int get_string(struct usb_composite_dev *cdev,
609 void *buf, u16 language, int id)
611 struct usb_string_descriptor *s = buf;
612 struct usb_gadget_strings **sp;
614 struct usb_configuration *c;
615 struct usb_function *f;
618 * Yes, not only is USB's I18N support probably more than most
619 * folk will ever care about ... also, it's all supported here.
620 * (Except for UTF8 support for Unicode's "Astral Planes".)
623 /* 0 == report all available language codes */
626 s->bDescriptorType = USB_DT_STRING;
628 sp = composite->strings;
630 collect_langs(sp, s->wData);
632 list_for_each_entry(c, &cdev->configs, list) {
635 collect_langs(sp, s->wData);
637 list_for_each_entry(f, &c->functions, list) {
640 collect_langs(sp, s->wData);
644 for (len = 0; len <= 126 && s->wData[len]; len++)
649 s->bLength = 2 * (len + 1);
653 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
654 struct usb_os_string *b = buf;
655 b->bLength = sizeof(*b);
656 b->bDescriptorType = USB_DT_STRING;
657 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
658 b->bMS_VendorCode = cdev->b_vendor_code;
664 * Otherwise, look up and return a specified string. String IDs
665 * are device-scoped, so we look up each string table we're told
666 * about. These lookups are infrequent; simpler-is-better here.
668 if (composite->strings) {
669 len = lookup_string(composite->strings, buf, language, id);
673 list_for_each_entry(c, &cdev->configs, list) {
675 len = lookup_string(c->strings, buf, language, id);
679 list_for_each_entry(f, &c->functions, list) {
682 len = lookup_string(f->strings, buf, language, id);
691 * usb_string_id() - allocate an unused string ID
692 * @cdev: the device whose string descriptor IDs are being allocated
693 * Context: single threaded during gadget setup
695 * @usb_string_id() is called from bind() callbacks to allocate
696 * string IDs. Drivers for functions, configurations, or gadgets will
697 * then store that ID in the appropriate descriptors and string table.
699 * All string identifier should be allocated using this,
700 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
701 * that for example different functions don't wrongly assign different
702 * meanings to the same identifier.
704 int usb_string_id(struct usb_composite_dev *cdev)
706 if (cdev->next_string_id < 254) {
708 * string id 0 is reserved by USB spec for list of
709 * supported languages
710 * 255 reserved as well? -- mina86
712 cdev->next_string_id++;
713 return cdev->next_string_id;
719 * usb_string_ids() - allocate unused string IDs in batch
720 * @cdev: the device whose string descriptor IDs are being allocated
721 * @str: an array of usb_string objects to assign numbers to
722 * Context: single threaded during gadget setup
724 * @usb_string_ids() is called from bind() callbacks to allocate
725 * string IDs. Drivers for functions, configurations, or gadgets will
726 * then copy IDs from the string table to the appropriate descriptors
727 * and string table for other languages.
729 * All string identifier should be allocated using this,
730 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
731 * example different functions don't wrongly assign different meanings
732 * to the same identifier.
734 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
736 u8 next = cdev->next_string_id;
738 for (; str->s; ++str) {
744 cdev->next_string_id = next;
750 * usb_string_ids_n() - allocate unused string IDs in batch
751 * @c: the device whose string descriptor IDs are being allocated
752 * @n: number of string IDs to allocate
753 * Context: single threaded during gadget setup
755 * Returns the first requested ID. This ID and next @n-1 IDs are now
756 * valid IDs. At least provided that @n is non-zero because if it
757 * is, returns last requested ID which is now very useful information.
759 * @usb_string_ids_n() is called from bind() callbacks to allocate
760 * string IDs. Drivers for functions, configurations, or gadgets will
761 * then store that ID in the appropriate descriptors and string table.
763 * All string identifier should be allocated using this,
764 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
765 * example different functions don't wrongly assign different meanings
766 * to the same identifier.
768 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
770 u8 next = c->next_string_id;
772 if (n > 254 || next + n > 254)
775 c->next_string_id += n;
779 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
781 if (req->status || req->actual != req->length)
782 debug("%s: setup complete --> %d, %d/%d\n", __func__,
783 req->status, req->actual, req->length);
786 static int bos_desc(struct usb_composite_dev *cdev)
788 struct usb_ext_cap_descriptor *usb_ext;
789 struct usb_dcd_config_params dcd_config_params;
790 struct usb_bos_descriptor *bos = cdev->req->buf;
792 bos->bLength = USB_DT_BOS_SIZE;
793 bos->bDescriptorType = USB_DT_BOS;
795 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
796 bos->bNumDeviceCaps = 0;
799 * A SuperSpeed device shall include the USB2.0 extension descriptor
800 * and shall support LPM when operating in USB2.0 HS mode.
802 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
803 bos->bNumDeviceCaps++;
804 le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
805 USB_DT_USB_EXT_CAP_SIZE);
806 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
807 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
808 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
809 usb_ext->bmAttributes =
810 cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
813 * The Superspeed USB Capability descriptor shall be implemented
814 * by all SuperSpeed devices.
816 if (gadget_is_superspeed(cdev->gadget)) {
817 struct usb_ss_cap_descriptor *ss_cap;
819 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
820 bos->bNumDeviceCaps++;
821 le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
822 USB_DT_USB_SS_CAP_SIZE);
823 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
824 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
825 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
826 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
827 ss_cap->wSpeedSupported =
828 cpu_to_le16(USB_LOW_SPEED_OPERATION |
829 USB_FULL_SPEED_OPERATION |
830 USB_HIGH_SPEED_OPERATION |
831 USB_5GBPS_OPERATION);
832 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
834 /* Get Controller configuration */
835 if (cdev->gadget->ops->get_config_params) {
836 cdev->gadget->ops->get_config_params(
839 dcd_config_params.bU1devExitLat =
840 USB_DEFAULT_U1_DEV_EXIT_LAT;
841 dcd_config_params.bU2DevExitLat =
842 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
844 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
845 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
847 return le16_to_cpu(bos->wTotalLength);
850 static int count_ext_compat(struct usb_configuration *c)
855 for (i = 0; i < c->next_interface_id; ++i) {
856 struct usb_function *f;
860 for (j = 0; j < f->os_desc_n; ++j) {
861 struct usb_os_desc *d;
863 if (i != f->os_desc_table[j].if_id)
865 d = f->os_desc_table[j].os_desc;
866 if (d && d->ext_compat_id)
874 static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
879 for (i = 0; i < c->next_interface_id; ++i) {
880 struct usb_function *f;
884 for (j = 0; j < f->os_desc_n; ++j) {
885 struct usb_os_desc *d;
887 if (i != f->os_desc_table[j].if_id)
889 d = f->os_desc_table[j].os_desc;
890 if (d && d->ext_compat_id) {
893 memcpy(buf, d->ext_compat_id, 16);
907 static int count_ext_prop(struct usb_configuration *c, int interface)
909 struct usb_function *f;
912 f = c->interface[interface];
913 for (j = 0; j < f->os_desc_n; ++j) {
914 struct usb_os_desc *d;
916 if (interface != f->os_desc_table[j].if_id)
918 d = f->os_desc_table[j].os_desc;
919 if (d && d->ext_compat_id)
920 return d->ext_prop_count;
925 static int len_ext_prop(struct usb_configuration *c, int interface)
927 struct usb_function *f;
928 struct usb_os_desc *d;
931 res = 10; /* header length */
932 f = c->interface[interface];
933 for (j = 0; j < f->os_desc_n; ++j) {
934 if (interface != f->os_desc_table[j].if_id)
936 d = f->os_desc_table[j].os_desc;
938 return min(res + d->ext_prop_len, 4096);
943 static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
945 struct usb_function *f;
946 struct usb_os_desc *d;
947 struct usb_os_desc_ext_prop *ext_prop;
948 int j, count, n, ret;
951 f = c->interface[interface];
952 for (j = 0; j < f->os_desc_n; ++j) {
953 if (interface != f->os_desc_table[j].if_id)
955 d = f->os_desc_table[j].os_desc;
957 list_for_each_entry(ext_prop, &d->ext_prop, entry) {
958 /* 4kB minus header length */
963 count = ext_prop->data_len +
964 ext_prop->name_len + 14;
965 if (count > 4086 - n)
967 usb_ext_prop_put_size(buf, count);
968 usb_ext_prop_put_type(buf, ext_prop->type);
969 ret = usb_ext_prop_put_name(buf, ext_prop->name,
973 switch (ext_prop->type) {
974 case USB_EXT_PROP_UNICODE:
975 case USB_EXT_PROP_UNICODE_ENV:
976 case USB_EXT_PROP_UNICODE_LINK:
977 usb_ext_prop_put_unicode(buf, ret,
981 case USB_EXT_PROP_BINARY:
982 usb_ext_prop_put_binary(buf, ret,
986 case USB_EXT_PROP_LE32:
987 /* not implemented */
988 case USB_EXT_PROP_BE32:
989 /* not implemented */
1001 * The setup() callback implements all the ep0 functionality that's
1002 * not handled lower down, in hardware or the hardware driver(like
1003 * device and endpoint feature flags, and their status). It's all
1004 * housekeeping for the gadget function we're implementing. Most of
1005 * the work is in config and function specific setup.
1008 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1010 u16 w_length = le16_to_cpu(ctrl->wLength);
1011 u16 w_index = le16_to_cpu(ctrl->wIndex);
1012 u16 w_value = le16_to_cpu(ctrl->wValue);
1013 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1014 u8 intf = w_index & 0xFF;
1015 int value = -EOPNOTSUPP;
1016 struct usb_request *req = cdev->req;
1017 struct usb_function *f = NULL;
1020 struct usb_configuration *c;
1023 * partial re-init of the response message; the function or the
1024 * gadget might need to intercept e.g. a control-OUT completion
1025 * when we delegate to it.
1028 req->complete = composite_setup_complete;
1029 req->length = USB_BUFSIZ;
1030 gadget->ep0->driver_data = cdev;
1031 standard = (ctrl->bRequestType & USB_TYPE_MASK)
1032 == USB_TYPE_STANDARD;
1036 switch (ctrl->bRequest) {
1038 /* we handle all standard USB descriptors */
1039 case USB_REQ_GET_DESCRIPTOR:
1040 if (ctrl->bRequestType != USB_DIR_IN)
1042 switch (w_value >> 8) {
1045 cdev->desc.bNumConfigurations =
1046 count_configs(cdev, USB_DT_DEVICE);
1048 cdev->desc.bMaxPacketSize0 =
1049 cdev->gadget->ep0->maxpacket;
1050 if (gadget->speed >= USB_SPEED_SUPER) {
1051 cdev->desc.bcdUSB = cpu_to_le16(0x0310);
1052 cdev->desc.bMaxPacketSize0 = 9;
1054 cdev->desc.bcdUSB = cpu_to_le16(0x0200);
1056 value = min(w_length, (u16) sizeof cdev->desc);
1057 memcpy(req->buf, &cdev->desc, value);
1059 case USB_DT_DEVICE_QUALIFIER:
1060 if (!gadget_is_dualspeed(gadget) ||
1061 gadget->speed >= USB_SPEED_SUPER)
1064 value = min_t(int, w_length,
1065 sizeof(struct usb_qualifier_descriptor));
1067 case USB_DT_OTHER_SPEED_CONFIG:
1068 if (!gadget_is_dualspeed(gadget) ||
1069 gadget->speed >= USB_SPEED_SUPER)
1073 value = config_desc(cdev, w_value);
1075 value = min(w_length, (u16) value);
1078 value = get_string(cdev, req->buf,
1079 w_index, w_value & 0xff);
1081 value = min(w_length, (u16) value);
1085 * Super speed connection should support BOS, and
1086 * USB compliance test (USB 2.0 Command Verifier)
1087 * also issues this request, return for now for
1088 * USB 2.0 connection.
1090 if (gadget->speed >= USB_SPEED_SUPER) {
1091 value = bos_desc(cdev);
1092 value = min(w_length, (u16)value);
1100 /* any number of configs can work */
1101 case USB_REQ_SET_CONFIGURATION:
1102 if (ctrl->bRequestType != 0)
1104 if (gadget_is_otg(gadget)) {
1105 if (gadget->a_hnp_support)
1106 debug("HNP available\n");
1107 else if (gadget->a_alt_hnp_support)
1108 debug("HNP on another port\n");
1110 debug("HNP inactive\n");
1113 value = set_config(cdev, ctrl, w_value);
1115 case USB_REQ_GET_CONFIGURATION:
1116 if (ctrl->bRequestType != USB_DIR_IN)
1119 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1121 *(u8 *)req->buf = 0;
1122 value = min(w_length, (u16) 1);
1126 * function drivers must handle get/set altsetting; if there's
1127 * no get() method, we know only altsetting zero works.
1129 case USB_REQ_SET_INTERFACE:
1130 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1132 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1134 f = cdev->config->interface[intf];
1137 if (w_value && !f->set_alt)
1139 value = f->set_alt(f, w_index, w_value);
1141 case USB_REQ_GET_INTERFACE:
1142 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1144 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1146 f = cdev->config->interface[intf];
1149 /* lots of interfaces only need altsetting zero... */
1150 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1153 *((u8 *)req->buf) = value;
1154 value = min(w_length, (u16) 1);
1159 * OS descriptors handling
1161 if (CONFIG_IS_ENABLED(USB_GADGET_OS_DESCRIPTORS) && cdev->use_os_string &&
1162 cdev->os_desc_config && (ctrl->bRequestType & USB_TYPE_VENDOR) &&
1163 ctrl->bRequest == cdev->b_vendor_code) {
1164 struct usb_configuration *os_desc_cfg;
1170 os_desc_cfg = cdev->os_desc_config;
1171 memset(buf, 0, w_length);
1173 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1174 case USB_RECIP_DEVICE:
1175 if (w_index != 0x4 || (w_value >> 8))
1178 if (w_length == 0x10) {
1179 /* Number of ext compat interfaces */
1180 count = count_ext_compat(os_desc_cfg);
1182 count *= 24; /* 24 B/ext compat desc */
1183 count += 16; /* header */
1184 put_unaligned_le32(count, buf);
1187 /* "extended compatibility ID"s */
1188 count = count_ext_compat(os_desc_cfg);
1190 count *= 24; /* 24 B/ext compat desc */
1191 count += 16; /* header */
1192 put_unaligned_le32(count, buf);
1194 fill_ext_compat(os_desc_cfg, buf);
1198 case USB_RECIP_INTERFACE:
1199 if (w_index != 0x5 || (w_value >> 8))
1201 interface = w_value & 0xFF;
1203 if (w_length == 0x0A) {
1204 count = count_ext_prop(os_desc_cfg,
1206 put_unaligned_le16(count, buf + 8);
1207 count = len_ext_prop(os_desc_cfg,
1209 put_unaligned_le32(count, buf);
1213 count = count_ext_prop(os_desc_cfg,
1215 put_unaligned_le16(count, buf + 8);
1216 count = len_ext_prop(os_desc_cfg,
1218 put_unaligned_le32(count, buf);
1220 value = fill_ext_prop(os_desc_cfg,
1231 req->length = value;
1232 req->zero = value < w_length;
1233 value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
1235 debug("ep_queue --> %d\n", value);
1237 composite_setup_complete(gadget->ep0, req);
1243 debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
1244 ctrl->bRequestType, ctrl->bRequest,
1245 w_value, w_index, w_length);
1251 * functions always handle their interfaces and endpoints...
1252 * punt other recipients (other, WUSB, ...) to the current
1253 * configuration code.
1255 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1256 case USB_RECIP_INTERFACE:
1257 f = cdev->config->interface[intf];
1260 case USB_RECIP_ENDPOINT:
1261 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1262 list_for_each_entry(f, &cdev->config->functions, list) {
1263 if (test_bit(endp, f->endpoints))
1266 if (&f->list == &cdev->config->functions)
1270 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
1271 * for non-standard request (w_value = 0x21,
1272 * bRequest = GET_DESCRIPTOR in this case).
1273 * When only one interface is registered (as it is done now),
1274 * then this request shall be handled as it was requested for
1277 * In the below code it is checked if only one interface is
1278 * present and proper function for it is extracted. Due to that
1279 * function's setup (f->setup) is called to handle this
1280 * special non-standard request.
1282 case USB_RECIP_DEVICE:
1283 debug("cdev->config->next_interface_id: %d intf: %d\n",
1284 cdev->config->next_interface_id, intf);
1285 if (cdev->config->next_interface_id == 1)
1286 f = cdev->config->interface[intf];
1291 value = f->setup(f, ctrl);
1295 value = c->setup(c, ctrl);
1301 /* respond with data transfer before status phase? */
1303 req->length = value;
1304 req->zero = value < w_length;
1305 value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
1307 debug("ep_queue --> %d\n", value);
1309 composite_setup_complete(gadget->ep0, req);
1314 /* device either stalls (value < 0) or reports success */
1318 static void composite_disconnect(struct usb_gadget *gadget)
1320 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1324 if (composite->disconnect)
1325 composite->disconnect(cdev);
1328 static void composite_unbind(struct usb_gadget *gadget)
1330 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1331 struct usb_configuration *c;
1332 struct usb_function *f;
1335 * composite_disconnect() must already have been called
1336 * by the underlying peripheral controller driver!
1337 * so there's no i/o concurrency that could affect the
1338 * state protected by cdev->lock.
1341 assert_noisy(!cdev->config);
1343 BUG_ON(cdev->config);
1346 while (!list_empty(&cdev->configs)) {
1347 c = list_first_entry(&cdev->configs,
1348 struct usb_configuration, list);
1349 while (!list_empty(&c->functions)) {
1350 f = list_first_entry(&c->functions,
1351 struct usb_function, list);
1354 debug("unbind function '%s'/%p\n",
1361 debug("unbind config '%s'/%p\n", c->label, c);
1366 if (composite->unbind)
1367 composite->unbind(cdev);
1370 kfree(cdev->req->buf);
1371 usb_ep_free_request(gadget->ep0, cdev->req);
1374 set_gadget_data(gadget, NULL);
1379 static int composite_bind(struct usb_gadget *gadget)
1381 int status = -ENOMEM;
1382 struct usb_composite_dev *cdev;
1384 cdev = calloc(sizeof *cdev, 1);
1388 cdev->gadget = gadget;
1389 set_gadget_data(gadget, cdev);
1390 INIT_LIST_HEAD(&cdev->configs);
1392 /* preallocate control response and buffer */
1393 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1396 cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
1397 if (!cdev->req->buf)
1399 cdev->req->complete = composite_setup_complete;
1400 gadget->ep0->driver_data = cdev;
1402 cdev->bufsiz = USB_BUFSIZ;
1403 cdev->driver = composite;
1405 usb_gadget_set_selfpowered(gadget);
1406 usb_ep_autoconfig_reset(cdev->gadget);
1408 status = composite->bind(cdev);
1412 memcpy(&cdev->desc, composite->dev,
1413 sizeof(struct usb_device_descriptor));
1414 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1416 if (cdev->use_os_string) {
1417 /* TODO: Do we want to pass this via platform? */
1418 cdev->b_vendor_code = 0x40;
1420 /* Microsoft OS String Descriptor */
1421 utf8_to_utf16le(qw_sign_buf, (__le16 *)cdev->qw_sign,
1422 OS_STRING_QW_SIGN_LEN / 2);
1425 debug("%s: ready\n", composite->name);
1429 composite_unbind(gadget);
1434 composite_suspend(struct usb_gadget *gadget)
1436 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1437 struct usb_function *f;
1439 debug("%s: suspend\n", __func__);
1441 list_for_each_entry(f, &cdev->config->functions, list) {
1446 if (composite->suspend)
1447 composite->suspend(cdev);
1449 cdev->suspended = 1;
1453 composite_resume(struct usb_gadget *gadget)
1455 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1456 struct usb_function *f;
1458 debug("%s: resume\n", __func__);
1459 if (composite->resume)
1460 composite->resume(cdev);
1462 list_for_each_entry(f, &cdev->config->functions, list) {
1468 cdev->suspended = 0;
1471 static struct usb_gadget_driver composite_driver = {
1472 .speed = USB_SPEED_SUPER,
1474 .bind = composite_bind,
1475 .unbind = composite_unbind,
1477 .setup = composite_setup,
1478 .reset = composite_disconnect,
1479 .disconnect = composite_disconnect,
1481 .suspend = composite_suspend,
1482 .resume = composite_resume,
1486 * usb_composite_register() - register a composite driver
1487 * @driver: the driver to register
1488 * Context: single threaded during gadget setup
1490 * This function is used to register drivers using the composite driver
1491 * framework. The return value is zero, or a negative errno value.
1492 * Those values normally come from the driver's @bind method, which does
1493 * all the work of setting up the driver to match the hardware.
1495 * On successful return, the gadget is ready to respond to requests from
1496 * the host, unless one of its components invokes usb_gadget_disconnect()
1497 * while it was binding. That would usually be done in order to wait for
1498 * some userspace participation.
1500 int usb_composite_register(struct usb_composite_driver *driver)
1504 if (!driver || !driver->dev || !driver->bind || composite)
1508 driver->name = "composite";
1511 res = usb_gadget_register_driver(&composite_driver);
1519 * usb_composite_unregister() - unregister a composite driver
1520 * @driver: the driver to unregister
1522 * This function is used to unregister drivers using the composite
1525 void usb_composite_unregister(struct usb_composite_driver *driver)
1527 if (composite != driver)
1529 usb_gadget_unregister_driver(&composite_driver);