1 // SPDX-License-Identifier: GPL-2.0+
3 * composite.c - infrastructure for Composite USB Gadgets
5 * Copyright (C) 2006-2008 David Brownell
6 * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com>
11 #include <dm/devres.h>
12 #include <linux/bitops.h>
13 #include <linux/usb/composite.h>
15 #define USB_BUFSIZ 4096
17 /* Helper type for accessing packed u16 pointers */
18 typedef struct { __le16 val; } __packed __le16_packed;
20 static struct usb_composite_driver *composite;
22 static inline void le16_add_cpu_packed(__le16_packed *var, u16 val)
24 var->val = cpu_to_le16(le16_to_cpu(var->val) + val);
28 * usb_add_function() - add a function to a configuration
29 * @config: the configuration
30 * @function: the function being added
31 * Context: single threaded during gadget setup
33 * After initialization, each configuration must have one or more
34 * functions added to it. Adding a function involves calling its @bind()
35 * method to allocate resources such as interface and string identifiers
38 * This function returns the value of the function's bind(), which is
39 * zero for success else a negative errno value.
41 int usb_add_function(struct usb_configuration *config,
42 struct usb_function *function)
46 debug("adding '%s'/%p to config '%s'/%p\n",
47 function->name, function,
48 config->label, config);
50 if (!function->set_alt || !function->disable)
53 function->config = config;
54 list_add_tail(&function->list, &config->functions);
57 value = function->bind(config, function);
59 list_del(&function->list);
60 function->config = NULL;
65 if (!config->fullspeed && function->descriptors)
66 config->fullspeed = 1;
67 if (!config->highspeed && function->hs_descriptors)
68 config->highspeed = 1;
72 debug("adding '%s'/%p --> %d\n",
73 function->name, function, value);
78 * usb_function_deactivate - prevent function and gadget enumeration
79 * @function: the function that isn't yet ready to respond
81 * Blocks response of the gadget driver to host enumeration by
82 * preventing the data line pullup from being activated. This is
83 * normally called during @bind() processing to change from the
84 * initial "ready to respond" state, or when a required resource
87 * For example, drivers that serve as a passthrough to a userspace
88 * daemon can block enumeration unless that daemon (such as an OBEX,
89 * MTP, or print server) is ready to handle host requests.
91 * Not all systems support software control of their USB peripheral
94 * Returns zero on success, else negative errno.
96 int usb_function_deactivate(struct usb_function *function)
98 struct usb_composite_dev *cdev = function->config->cdev;
101 if (cdev->deactivations == 0)
102 status = usb_gadget_disconnect(cdev->gadget);
104 cdev->deactivations++;
110 * usb_function_activate - allow function and gadget enumeration
111 * @function: function on which usb_function_activate() was called
113 * Reverses effect of usb_function_deactivate(). If no more functions
114 * are delaying their activation, the gadget driver will respond to
115 * host enumeration procedures.
117 * Returns zero on success, else negative errno.
119 int usb_function_activate(struct usb_function *function)
121 struct usb_composite_dev *cdev = function->config->cdev;
124 if (cdev->deactivations == 0)
127 cdev->deactivations--;
128 if (cdev->deactivations == 0)
129 status = usb_gadget_connect(cdev->gadget);
136 * usb_interface_id() - allocate an unused interface ID
137 * @config: configuration associated with the interface
138 * @function: function handling the interface
139 * Context: single threaded during gadget setup
141 * usb_interface_id() is called from usb_function.bind() callbacks to
142 * allocate new interface IDs. The function driver will then store that
143 * ID in interface, association, CDC union, and other descriptors. It
144 * will also handle any control requests targetted at that interface,
145 * particularly changing its altsetting via set_alt(). There may
146 * also be class-specific or vendor-specific requests to handle.
148 * All interface identifier should be allocated using this routine, to
149 * ensure that for example different functions don't wrongly assign
150 * different meanings to the same identifier. Note that since interface
151 * identifers are configuration-specific, functions used in more than
152 * one configuration (or more than once in a given configuration) need
153 * multiple versions of the relevant descriptors.
155 * Returns the interface ID which was allocated; or -ENODEV if no
156 * more interface IDs can be allocated.
158 int usb_interface_id(struct usb_configuration *config,
159 struct usb_function *function)
161 unsigned char id = config->next_interface_id;
163 if (id < MAX_CONFIG_INTERFACES) {
164 config->interface[id] = function;
165 config->next_interface_id = id + 1;
171 static int config_buf(struct usb_configuration *config,
172 enum usb_device_speed speed, void *buf, u8 type)
174 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
175 void *next = buf + USB_DT_CONFIG_SIZE;
176 struct usb_descriptor_header **descriptors;
177 struct usb_config_descriptor *c;
179 struct usb_function *f;
181 /* write the config descriptor */
183 c->bLength = USB_DT_CONFIG_SIZE;
184 c->bDescriptorType = type;
186 c->bNumInterfaces = config->next_interface_id;
187 c->bConfigurationValue = config->bConfigurationValue;
188 c->iConfiguration = config->iConfiguration;
189 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
190 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
192 /* There may be e.g. OTG descriptors */
193 if (config->descriptors) {
194 status = usb_descriptor_fillbuf(next, len,
195 config->descriptors);
202 /* add each function's descriptors */
203 list_for_each_entry(f, &config->functions, list) {
204 if (speed == USB_SPEED_HIGH)
205 descriptors = f->hs_descriptors;
207 descriptors = f->descriptors;
210 status = usb_descriptor_fillbuf(next, len,
211 (const struct usb_descriptor_header **) descriptors);
219 c->wTotalLength = cpu_to_le16(len);
223 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
225 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
226 struct usb_gadget *gadget = cdev->gadget;
227 u8 type = w_value >> 8;
229 struct usb_configuration *c;
231 if (gadget_is_dualspeed(gadget)) {
232 if (gadget->speed == USB_SPEED_HIGH)
234 if (type == USB_DT_OTHER_SPEED_CONFIG)
237 speed = USB_SPEED_HIGH;
241 list_for_each_entry(c, &cdev->configs, list) {
242 if (speed == USB_SPEED_HIGH) {
250 return config_buf(c, speed, cdev->req->buf, type);
256 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
258 struct usb_gadget *gadget = cdev->gadget;
261 struct usb_configuration *c;
263 if (gadget_is_dualspeed(gadget)) {
264 if (gadget->speed == USB_SPEED_HIGH)
266 if (type == USB_DT_DEVICE_QUALIFIER)
269 list_for_each_entry(c, &cdev->configs, list) {
270 /* ignore configs that won't work at this speed */
283 static void device_qual(struct usb_composite_dev *cdev)
285 struct usb_qualifier_descriptor *qual = cdev->req->buf;
287 qual->bLength = sizeof(*qual);
288 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
289 /* POLICY: same bcdUSB and device type info at both speeds */
290 qual->bcdUSB = cdev->desc.bcdUSB;
291 qual->bDeviceClass = cdev->desc.bDeviceClass;
292 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
293 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
294 /* ASSUME same EP0 fifo size at both speeds */
295 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
296 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
300 static void reset_config(struct usb_composite_dev *cdev)
302 struct usb_function *f;
304 debug("%s:\n", __func__);
306 list_for_each_entry(f, &cdev->config->functions, list) {
310 bitmap_zero(f->endpoints, 32);
315 static int set_config(struct usb_composite_dev *cdev,
316 const struct usb_ctrlrequest *ctrl, unsigned number)
318 struct usb_gadget *gadget = cdev->gadget;
319 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
320 struct usb_descriptor_header **descriptors;
321 int result = -EINVAL;
322 struct usb_endpoint_descriptor *ep;
323 struct usb_configuration *c = NULL;
326 struct usb_function *f;
332 list_for_each_entry(c, &cdev->configs, list) {
333 if (c->bConfigurationValue == number) {
343 debug("%s: %s speed config #%d: %s\n", __func__,
345 switch (gadget->speed) {
360 }), number, c ? c->label : "unconfigured");
367 /* Initialize all interfaces by setting them to altsetting zero. */
368 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
369 f = c->interface[tmp];
374 * Record which endpoints are used by the function. This is used
375 * to dispatch control requests targeted at that endpoint to the
376 * function's setup callback instead of the current
377 * configuration's setup callback.
379 if (gadget->speed == USB_SPEED_HIGH)
380 descriptors = f->hs_descriptors;
382 descriptors = f->descriptors;
384 for (; *descriptors; ++descriptors) {
385 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
388 ep = (struct usb_endpoint_descriptor *)*descriptors;
389 addr = ((ep->bEndpointAddress & 0x80) >> 3)
390 | (ep->bEndpointAddress & 0x0f);
391 generic_set_bit(addr, f->endpoints);
394 result = f->set_alt(f, tmp, 0);
396 debug("interface %d (%s/%p) alt 0 --> %d\n",
397 tmp, f->name, f, result);
404 /* when we return, be sure our power usage is valid */
405 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
407 usb_gadget_vbus_draw(gadget, power);
412 * usb_add_config() - add a configuration to a device.
413 * @cdev: wraps the USB gadget
414 * @config: the configuration, with bConfigurationValue assigned
415 * Context: single threaded during gadget setup
417 * One of the main tasks of a composite driver's bind() routine is to
418 * add each of the configurations it supports, using this routine.
420 * This function returns the value of the configuration's bind(), which
421 * is zero for success else a negative errno value. Binding configurations
422 * assigns global resources including string IDs, and per-configuration
423 * resources such as interface IDs and endpoints.
425 int usb_add_config(struct usb_composite_dev *cdev,
426 struct usb_configuration *config)
428 int status = -EINVAL;
429 struct usb_configuration *c;
430 struct usb_function *f;
433 debug("%s: adding config #%u '%s'/%p\n", __func__,
434 config->bConfigurationValue,
435 config->label, config);
437 if (!config->bConfigurationValue || !config->bind)
440 /* Prevent duplicate configuration identifiers */
441 list_for_each_entry(c, &cdev->configs, list) {
442 if (c->bConfigurationValue == config->bConfigurationValue) {
449 list_add_tail(&config->list, &cdev->configs);
451 INIT_LIST_HEAD(&config->functions);
452 config->next_interface_id = 0;
454 status = config->bind(config);
456 list_del(&config->list);
459 debug("cfg %d/%p speeds:%s%s\n",
460 config->bConfigurationValue, config,
461 config->highspeed ? " high" : "",
463 ? (gadget_is_dualspeed(cdev->gadget)
468 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
469 f = config->interface[i];
472 debug("%s: interface %d = %s/%p\n",
473 __func__, i, f->name, f);
477 usb_ep_autoconfig_reset(cdev->gadget);
481 debug("added config '%s'/%u --> %d\n", config->label,
482 config->bConfigurationValue, status);
487 * We support strings in multiple languages ... string descriptor zero
488 * says which languages are supported. The typical case will be that
489 * only one language (probably English) is used, with I18N handled on
493 static void collect_langs(struct usb_gadget_strings **sp, void *buf)
495 const struct usb_gadget_strings *s;
498 __le16_packed *end = (buf + 252);
502 language = cpu_to_le16(s->language);
503 for (tmp = buf; tmp->val && tmp < end; tmp++) {
504 if (tmp->val == language)
513 static int lookup_string(
514 struct usb_gadget_strings **sp,
521 struct usb_gadget_strings *s;
525 if (s->language != language)
527 value = usb_gadget_get_string(s, id, buf);
534 static int get_string(struct usb_composite_dev *cdev,
535 void *buf, u16 language, int id)
537 struct usb_string_descriptor *s = buf;
538 struct usb_gadget_strings **sp;
540 struct usb_configuration *c;
541 struct usb_function *f;
544 * Yes, not only is USB's I18N support probably more than most
545 * folk will ever care about ... also, it's all supported here.
546 * (Except for UTF8 support for Unicode's "Astral Planes".)
549 /* 0 == report all available language codes */
552 s->bDescriptorType = USB_DT_STRING;
554 sp = composite->strings;
556 collect_langs(sp, s->wData);
558 list_for_each_entry(c, &cdev->configs, list) {
561 collect_langs(sp, s->wData);
563 list_for_each_entry(f, &c->functions, list) {
566 collect_langs(sp, s->wData);
570 for (len = 0; len <= 126 && s->wData[len]; len++)
575 s->bLength = 2 * (len + 1);
580 * Otherwise, look up and return a specified string. String IDs
581 * are device-scoped, so we look up each string table we're told
582 * about. These lookups are infrequent; simpler-is-better here.
584 if (composite->strings) {
585 len = lookup_string(composite->strings, buf, language, id);
589 list_for_each_entry(c, &cdev->configs, list) {
591 len = lookup_string(c->strings, buf, language, id);
595 list_for_each_entry(f, &c->functions, list) {
598 len = lookup_string(f->strings, buf, language, id);
607 * usb_string_id() - allocate an unused string ID
608 * @cdev: the device whose string descriptor IDs are being allocated
609 * Context: single threaded during gadget setup
611 * @usb_string_id() is called from bind() callbacks to allocate
612 * string IDs. Drivers for functions, configurations, or gadgets will
613 * then store that ID in the appropriate descriptors and string table.
615 * All string identifier should be allocated using this,
616 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
617 * that for example different functions don't wrongly assign different
618 * meanings to the same identifier.
620 int usb_string_id(struct usb_composite_dev *cdev)
622 if (cdev->next_string_id < 254) {
624 * string id 0 is reserved by USB spec for list of
625 * supported languages
626 * 255 reserved as well? -- mina86
628 cdev->next_string_id++;
629 return cdev->next_string_id;
635 * usb_string_ids() - allocate unused string IDs in batch
636 * @cdev: the device whose string descriptor IDs are being allocated
637 * @str: an array of usb_string objects to assign numbers to
638 * Context: single threaded during gadget setup
640 * @usb_string_ids() is called from bind() callbacks to allocate
641 * string IDs. Drivers for functions, configurations, or gadgets will
642 * then copy IDs from the string table to the appropriate descriptors
643 * and string table for other languages.
645 * All string identifier should be allocated using this,
646 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
647 * example different functions don't wrongly assign different meanings
648 * to the same identifier.
650 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
652 u8 next = cdev->next_string_id;
654 for (; str->s; ++str) {
660 cdev->next_string_id = next;
666 * usb_string_ids_n() - allocate unused string IDs in batch
667 * @c: the device whose string descriptor IDs are being allocated
668 * @n: number of string IDs to allocate
669 * Context: single threaded during gadget setup
671 * Returns the first requested ID. This ID and next @n-1 IDs are now
672 * valid IDs. At least provided that @n is non-zero because if it
673 * is, returns last requested ID which is now very useful information.
675 * @usb_string_ids_n() is called from bind() callbacks to allocate
676 * string IDs. Drivers for functions, configurations, or gadgets will
677 * then store that ID in the appropriate descriptors and string table.
679 * All string identifier should be allocated using this,
680 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
681 * example different functions don't wrongly assign different meanings
682 * to the same identifier.
684 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
686 u8 next = c->next_string_id;
688 if (n > 254 || next + n > 254)
691 c->next_string_id += n;
695 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
697 if (req->status || req->actual != req->length)
698 debug("%s: setup complete --> %d, %d/%d\n", __func__,
699 req->status, req->actual, req->length);
702 static int bos_desc(struct usb_composite_dev *cdev)
704 struct usb_ext_cap_descriptor *usb_ext;
705 struct usb_bos_descriptor *bos = cdev->req->buf;
707 bos->bLength = USB_DT_BOS_SIZE;
708 bos->bDescriptorType = USB_DT_BOS;
710 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
711 bos->bNumDeviceCaps = 0;
714 * A SuperSpeed device shall include the USB2.0 extension descriptor
715 * and shall support LPM when operating in USB2.0 HS mode.
717 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
718 bos->bNumDeviceCaps++;
719 le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
720 USB_DT_USB_EXT_CAP_SIZE);
721 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
722 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
723 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
724 usb_ext->bmAttributes =
725 cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
728 * The Superspeed USB Capability descriptor shall be implemented
729 * by all SuperSpeed devices.
731 if (gadget_is_superspeed(cdev->gadget)) {
732 struct usb_ss_cap_descriptor *ss_cap;
734 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
735 bos->bNumDeviceCaps++;
736 le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
737 USB_DT_USB_SS_CAP_SIZE);
738 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
739 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
740 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
741 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
742 ss_cap->wSpeedSupported =
743 cpu_to_le16(USB_LOW_SPEED_OPERATION |
744 USB_FULL_SPEED_OPERATION |
745 USB_HIGH_SPEED_OPERATION |
746 USB_5GBPS_OPERATION);
747 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
748 ss_cap->bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
749 ss_cap->bU2DevExitLat =
750 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
752 return le16_to_cpu(bos->wTotalLength);
756 * The setup() callback implements all the ep0 functionality that's
757 * not handled lower down, in hardware or the hardware driver(like
758 * device and endpoint feature flags, and their status). It's all
759 * housekeeping for the gadget function we're implementing. Most of
760 * the work is in config and function specific setup.
763 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
765 u16 w_length = le16_to_cpu(ctrl->wLength);
766 u16 w_index = le16_to_cpu(ctrl->wIndex);
767 u16 w_value = le16_to_cpu(ctrl->wValue);
768 struct usb_composite_dev *cdev = get_gadget_data(gadget);
769 u8 intf = w_index & 0xFF;
770 int value = -EOPNOTSUPP;
771 struct usb_request *req = cdev->req;
772 struct usb_function *f = NULL;
775 struct usb_configuration *c;
778 * partial re-init of the response message; the function or the
779 * gadget might need to intercept e.g. a control-OUT completion
780 * when we delegate to it.
783 req->complete = composite_setup_complete;
784 req->length = USB_BUFSIZ;
785 gadget->ep0->driver_data = cdev;
786 standard = (ctrl->bRequestType & USB_TYPE_MASK)
787 == USB_TYPE_STANDARD;
791 switch (ctrl->bRequest) {
793 /* we handle all standard USB descriptors */
794 case USB_REQ_GET_DESCRIPTOR:
795 if (ctrl->bRequestType != USB_DIR_IN)
797 switch (w_value >> 8) {
800 cdev->desc.bNumConfigurations =
801 count_configs(cdev, USB_DT_DEVICE);
804 * If the speed is Super speed, then the supported
805 * max packet size is 512 and it should be sent as
806 * exponent of 2. So, 9(2^9=512) should be filled in
807 * bMaxPacketSize0. Also fill USB version as 3.0
808 * if speed is Super speed.
810 if (cdev->gadget->speed == USB_SPEED_SUPER) {
811 cdev->desc.bMaxPacketSize0 = 9;
812 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
814 cdev->desc.bMaxPacketSize0 =
815 cdev->gadget->ep0->maxpacket;
817 value = min(w_length, (u16) sizeof cdev->desc);
818 memcpy(req->buf, &cdev->desc, value);
820 case USB_DT_DEVICE_QUALIFIER:
821 if (!gadget_is_dualspeed(gadget))
824 value = min_t(int, w_length,
825 sizeof(struct usb_qualifier_descriptor));
827 case USB_DT_OTHER_SPEED_CONFIG:
828 if (!gadget_is_dualspeed(gadget))
832 value = config_desc(cdev, w_value);
834 value = min(w_length, (u16) value);
837 value = get_string(cdev, req->buf,
838 w_index, w_value & 0xff);
840 value = min(w_length, (u16) value);
843 if (gadget_is_superspeed(cdev->gadget))
844 value = bos_desc(cdev);
846 value = min(w_length, (u16)value);
853 /* any number of configs can work */
854 case USB_REQ_SET_CONFIGURATION:
855 if (ctrl->bRequestType != 0)
857 if (gadget_is_otg(gadget)) {
858 if (gadget->a_hnp_support)
859 debug("HNP available\n");
860 else if (gadget->a_alt_hnp_support)
861 debug("HNP on another port\n");
863 debug("HNP inactive\n");
866 value = set_config(cdev, ctrl, w_value);
868 case USB_REQ_GET_CONFIGURATION:
869 if (ctrl->bRequestType != USB_DIR_IN)
872 *(u8 *)req->buf = cdev->config->bConfigurationValue;
875 value = min(w_length, (u16) 1);
879 * function drivers must handle get/set altsetting; if there's
880 * no get() method, we know only altsetting zero works.
882 case USB_REQ_SET_INTERFACE:
883 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
885 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
887 f = cdev->config->interface[intf];
890 if (w_value && !f->set_alt)
892 value = f->set_alt(f, w_index, w_value);
894 case USB_REQ_GET_INTERFACE:
895 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
897 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
899 f = cdev->config->interface[intf];
902 /* lots of interfaces only need altsetting zero... */
903 value = f->get_alt ? f->get_alt(f, w_index) : 0;
906 *((u8 *)req->buf) = value;
907 value = min(w_length, (u16) 1);
911 debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
912 ctrl->bRequestType, ctrl->bRequest,
913 w_value, w_index, w_length);
919 * functions always handle their interfaces and endpoints...
920 * punt other recipients (other, WUSB, ...) to the current
921 * configuration code.
923 switch (ctrl->bRequestType & USB_RECIP_MASK) {
924 case USB_RECIP_INTERFACE:
925 f = cdev->config->interface[intf];
928 case USB_RECIP_ENDPOINT:
929 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
930 list_for_each_entry(f, &cdev->config->functions, list) {
931 if (test_bit(endp, f->endpoints))
934 if (&f->list == &cdev->config->functions)
938 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
939 * for non-standard request (w_value = 0x21,
940 * bRequest = GET_DESCRIPTOR in this case).
941 * When only one interface is registered (as it is done now),
942 * then this request shall be handled as it was requested for
945 * In the below code it is checked if only one interface is
946 * present and proper function for it is extracted. Due to that
947 * function's setup (f->setup) is called to handle this
948 * special non-standard request.
950 case USB_RECIP_DEVICE:
951 debug("cdev->config->next_interface_id: %d intf: %d\n",
952 cdev->config->next_interface_id, intf);
953 if (cdev->config->next_interface_id == 1)
954 f = cdev->config->interface[intf];
959 value = f->setup(f, ctrl);
963 value = c->setup(c, ctrl);
969 /* respond with data transfer before status phase? */
972 req->zero = value < w_length;
973 value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
975 debug("ep_queue --> %d\n", value);
977 composite_setup_complete(gadget->ep0, req);
982 /* device either stalls (value < 0) or reports success */
986 static void composite_disconnect(struct usb_gadget *gadget)
988 struct usb_composite_dev *cdev = get_gadget_data(gadget);
992 if (composite->disconnect)
993 composite->disconnect(cdev);
996 static void composite_unbind(struct usb_gadget *gadget)
998 struct usb_composite_dev *cdev = get_gadget_data(gadget);
999 struct usb_configuration *c;
1000 struct usb_function *f;
1003 * composite_disconnect() must already have been called
1004 * by the underlying peripheral controller driver!
1005 * so there's no i/o concurrency that could affect the
1006 * state protected by cdev->lock.
1009 assert_noisy(!cdev->config);
1011 BUG_ON(cdev->config);
1014 while (!list_empty(&cdev->configs)) {
1015 c = list_first_entry(&cdev->configs,
1016 struct usb_configuration, list);
1017 while (!list_empty(&c->functions)) {
1018 f = list_first_entry(&c->functions,
1019 struct usb_function, list);
1022 debug("unbind function '%s'/%p\n",
1029 debug("unbind config '%s'/%p\n", c->label, c);
1034 if (composite->unbind)
1035 composite->unbind(cdev);
1038 kfree(cdev->req->buf);
1039 usb_ep_free_request(gadget->ep0, cdev->req);
1042 set_gadget_data(gadget, NULL);
1047 static int composite_bind(struct usb_gadget *gadget)
1049 int status = -ENOMEM;
1050 struct usb_composite_dev *cdev;
1052 cdev = calloc(sizeof *cdev, 1);
1056 cdev->gadget = gadget;
1057 set_gadget_data(gadget, cdev);
1058 INIT_LIST_HEAD(&cdev->configs);
1060 /* preallocate control response and buffer */
1061 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1064 cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
1065 if (!cdev->req->buf)
1067 cdev->req->complete = composite_setup_complete;
1068 gadget->ep0->driver_data = cdev;
1070 cdev->bufsiz = USB_BUFSIZ;
1071 cdev->driver = composite;
1073 usb_gadget_set_selfpowered(gadget);
1074 usb_ep_autoconfig_reset(cdev->gadget);
1076 status = composite->bind(cdev);
1080 memcpy(&cdev->desc, composite->dev,
1081 sizeof(struct usb_device_descriptor));
1082 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1084 debug("%s: ready\n", composite->name);
1088 composite_unbind(gadget);
1093 composite_suspend(struct usb_gadget *gadget)
1095 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1096 struct usb_function *f;
1098 debug("%s: suspend\n", __func__);
1100 list_for_each_entry(f, &cdev->config->functions, list) {
1105 if (composite->suspend)
1106 composite->suspend(cdev);
1108 cdev->suspended = 1;
1112 composite_resume(struct usb_gadget *gadget)
1114 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1115 struct usb_function *f;
1117 debug("%s: resume\n", __func__);
1118 if (composite->resume)
1119 composite->resume(cdev);
1121 list_for_each_entry(f, &cdev->config->functions, list) {
1127 cdev->suspended = 0;
1130 static struct usb_gadget_driver composite_driver = {
1131 .speed = USB_SPEED_HIGH,
1133 .bind = composite_bind,
1134 .unbind = composite_unbind,
1136 .setup = composite_setup,
1137 .reset = composite_disconnect,
1138 .disconnect = composite_disconnect,
1140 .suspend = composite_suspend,
1141 .resume = composite_resume,
1145 * usb_composite_register() - register a composite driver
1146 * @driver: the driver to register
1147 * Context: single threaded during gadget setup
1149 * This function is used to register drivers using the composite driver
1150 * framework. The return value is zero, or a negative errno value.
1151 * Those values normally come from the driver's @bind method, which does
1152 * all the work of setting up the driver to match the hardware.
1154 * On successful return, the gadget is ready to respond to requests from
1155 * the host, unless one of its components invokes usb_gadget_disconnect()
1156 * while it was binding. That would usually be done in order to wait for
1157 * some userspace participation.
1159 int usb_composite_register(struct usb_composite_driver *driver)
1163 if (!driver || !driver->dev || !driver->bind || composite)
1167 driver->name = "composite";
1170 res = usb_gadget_register_driver(&composite_driver);
1178 * usb_composite_unregister() - unregister a composite driver
1179 * @driver: the driver to unregister
1181 * This function is used to unregister drivers using the composite
1184 void usb_composite_unregister(struct usb_composite_driver *driver)
1186 if (composite != driver)
1188 usb_gadget_unregister_driver(&composite_driver);