2 * composite.c - infrastructure for Composite USB Gadgets
4 * Copyright (C) 2006-2008 David Brownell
5 * U-boot porting: Lukasz Majewski <l.majewski@samsung.com>
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/bitops.h>
12 #include <linux/usb/composite.h>
14 #define USB_BUFSIZ 4096
16 static struct usb_composite_driver
*composite
;
19 * usb_add_function() - add a function to a configuration
20 * @config: the configuration
21 * @function: the function being added
22 * Context: single threaded during gadget setup
24 * After initialization, each configuration must have one or more
25 * functions added to it. Adding a function involves calling its @bind()
26 * method to allocate resources such as interface and string identifiers
29 * This function returns the value of the function's bind(), which is
30 * zero for success else a negative errno value.
32 int usb_add_function(struct usb_configuration
*config
,
33 struct usb_function
*function
)
37 debug("adding '%s'/%p to config '%s'/%p\n",
38 function
->name
, function
,
39 config
->label
, config
);
41 if (!function
->set_alt
|| !function
->disable
)
44 function
->config
= config
;
45 list_add_tail(&function
->list
, &config
->functions
);
48 value
= function
->bind(config
, function
);
50 list_del(&function
->list
);
51 function
->config
= NULL
;
56 if (!config
->fullspeed
&& function
->descriptors
)
57 config
->fullspeed
= 1;
58 if (!config
->highspeed
&& function
->hs_descriptors
)
59 config
->highspeed
= 1;
63 debug("adding '%s'/%p --> %d\n",
64 function
->name
, function
, value
);
69 * usb_function_deactivate - prevent function and gadget enumeration
70 * @function: the function that isn't yet ready to respond
72 * Blocks response of the gadget driver to host enumeration by
73 * preventing the data line pullup from being activated. This is
74 * normally called during @bind() processing to change from the
75 * initial "ready to respond" state, or when a required resource
78 * For example, drivers that serve as a passthrough to a userspace
79 * daemon can block enumeration unless that daemon (such as an OBEX,
80 * MTP, or print server) is ready to handle host requests.
82 * Not all systems support software control of their USB peripheral
85 * Returns zero on success, else negative errno.
87 int usb_function_deactivate(struct usb_function
*function
)
89 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
92 if (cdev
->deactivations
== 0)
93 status
= usb_gadget_disconnect(cdev
->gadget
);
95 cdev
->deactivations
++;
101 * usb_function_activate - allow function and gadget enumeration
102 * @function: function on which usb_function_activate() was called
104 * Reverses effect of usb_function_deactivate(). If no more functions
105 * are delaying their activation, the gadget driver will respond to
106 * host enumeration procedures.
108 * Returns zero on success, else negative errno.
110 int usb_function_activate(struct usb_function
*function
)
112 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
115 if (cdev
->deactivations
== 0)
118 cdev
->deactivations
--;
119 if (cdev
->deactivations
== 0)
120 status
= usb_gadget_connect(cdev
->gadget
);
127 * usb_interface_id() - allocate an unused interface ID
128 * @config: configuration associated with the interface
129 * @function: function handling the interface
130 * Context: single threaded during gadget setup
132 * usb_interface_id() is called from usb_function.bind() callbacks to
133 * allocate new interface IDs. The function driver will then store that
134 * ID in interface, association, CDC union, and other descriptors. It
135 * will also handle any control requests targetted at that interface,
136 * particularly changing its altsetting via set_alt(). There may
137 * also be class-specific or vendor-specific requests to handle.
139 * All interface identifier should be allocated using this routine, to
140 * ensure that for example different functions don't wrongly assign
141 * different meanings to the same identifier. Note that since interface
142 * identifers are configuration-specific, functions used in more than
143 * one configuration (or more than once in a given configuration) need
144 * multiple versions of the relevant descriptors.
146 * Returns the interface ID which was allocated; or -ENODEV if no
147 * more interface IDs can be allocated.
149 int usb_interface_id(struct usb_configuration
*config
,
150 struct usb_function
*function
)
152 unsigned char id
= config
->next_interface_id
;
154 if (id
< MAX_CONFIG_INTERFACES
) {
155 config
->interface
[id
] = function
;
156 config
->next_interface_id
= id
+ 1;
162 static int config_buf(struct usb_configuration
*config
,
163 enum usb_device_speed speed
, void *buf
, u8 type
)
165 int len
= USB_BUFSIZ
- USB_DT_CONFIG_SIZE
;
166 void *next
= buf
+ USB_DT_CONFIG_SIZE
;
167 struct usb_descriptor_header
**descriptors
;
168 struct usb_config_descriptor
*c
= buf
;
170 struct usb_function
*f
;
172 /* write the config descriptor */
174 c
->bLength
= USB_DT_CONFIG_SIZE
;
175 c
->bDescriptorType
= type
;
177 c
->bNumInterfaces
= config
->next_interface_id
;
178 c
->bConfigurationValue
= config
->bConfigurationValue
;
179 c
->iConfiguration
= config
->iConfiguration
;
180 c
->bmAttributes
= USB_CONFIG_ATT_ONE
| config
->bmAttributes
;
181 c
->bMaxPower
= config
->bMaxPower
? : (CONFIG_USB_GADGET_VBUS_DRAW
/ 2);
183 /* There may be e.g. OTG descriptors */
184 if (config
->descriptors
) {
185 status
= usb_descriptor_fillbuf(next
, len
,
186 config
->descriptors
);
193 /* add each function's descriptors */
194 list_for_each_entry(f
, &config
->functions
, list
) {
195 if (speed
== USB_SPEED_HIGH
)
196 descriptors
= f
->hs_descriptors
;
198 descriptors
= f
->descriptors
;
201 status
= usb_descriptor_fillbuf(next
, len
,
202 (const struct usb_descriptor_header
**) descriptors
);
210 c
->wTotalLength
= cpu_to_le16(len
);
214 static int config_desc(struct usb_composite_dev
*cdev
, unsigned w_value
)
216 enum usb_device_speed speed
= USB_SPEED_UNKNOWN
;
217 struct usb_gadget
*gadget
= cdev
->gadget
;
218 u8 type
= w_value
>> 8;
220 struct usb_configuration
*c
;
222 if (gadget_is_dualspeed(gadget
)) {
223 if (gadget
->speed
== USB_SPEED_HIGH
)
225 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
228 speed
= USB_SPEED_HIGH
;
232 list_for_each_entry(c
, &cdev
->configs
, list
) {
233 if (speed
== USB_SPEED_HIGH
) {
241 return config_buf(c
, speed
, cdev
->req
->buf
, type
);
247 static int count_configs(struct usb_composite_dev
*cdev
, unsigned type
)
249 struct usb_gadget
*gadget
= cdev
->gadget
;
252 struct usb_configuration
*c
;
254 if (gadget_is_dualspeed(gadget
)) {
255 if (gadget
->speed
== USB_SPEED_HIGH
)
257 if (type
== USB_DT_DEVICE_QUALIFIER
)
260 list_for_each_entry(c
, &cdev
->configs
, list
) {
261 /* ignore configs that won't work at this speed */
274 static void device_qual(struct usb_composite_dev
*cdev
)
276 struct usb_qualifier_descriptor
*qual
= cdev
->req
->buf
;
278 qual
->bLength
= sizeof(*qual
);
279 qual
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
280 /* POLICY: same bcdUSB and device type info at both speeds */
281 qual
->bcdUSB
= cdev
->desc
.bcdUSB
;
282 qual
->bDeviceClass
= cdev
->desc
.bDeviceClass
;
283 qual
->bDeviceSubClass
= cdev
->desc
.bDeviceSubClass
;
284 qual
->bDeviceProtocol
= cdev
->desc
.bDeviceProtocol
;
285 /* ASSUME same EP0 fifo size at both speeds */
286 qual
->bMaxPacketSize0
= cdev
->desc
.bMaxPacketSize0
;
287 qual
->bNumConfigurations
= count_configs(cdev
, USB_DT_DEVICE_QUALIFIER
);
291 static void reset_config(struct usb_composite_dev
*cdev
)
293 struct usb_function
*f
;
295 debug("%s:\n", __func__
);
297 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
301 bitmap_zero(f
->endpoints
, 32);
306 static int set_config(struct usb_composite_dev
*cdev
,
307 const struct usb_ctrlrequest
*ctrl
, unsigned number
)
309 struct usb_gadget
*gadget
= cdev
->gadget
;
310 unsigned power
= gadget_is_otg(gadget
) ? 8 : 100;
311 struct usb_descriptor_header
**descriptors
;
312 int result
= -EINVAL
;
313 struct usb_endpoint_descriptor
*ep
;
314 struct usb_configuration
*c
= NULL
;
317 struct usb_function
*f
;
323 list_for_each_entry(c
, &cdev
->configs
, list
) {
324 if (c
->bConfigurationValue
== number
) {
334 debug("%s: %s speed config #%d: %s\n", __func__
,
336 switch (gadget
->speed
) {
351 }), number
, c
? c
->label
: "unconfigured");
358 /* Initialize all interfaces by setting them to altsetting zero. */
359 for (tmp
= 0; tmp
< MAX_CONFIG_INTERFACES
; tmp
++) {
360 f
= c
->interface
[tmp
];
365 * Record which endpoints are used by the function. This is used
366 * to dispatch control requests targeted at that endpoint to the
367 * function's setup callback instead of the current
368 * configuration's setup callback.
370 if (gadget
->speed
== USB_SPEED_HIGH
)
371 descriptors
= f
->hs_descriptors
;
373 descriptors
= f
->descriptors
;
375 for (; *descriptors
; ++descriptors
) {
376 if ((*descriptors
)->bDescriptorType
!= USB_DT_ENDPOINT
)
379 ep
= (struct usb_endpoint_descriptor
*)*descriptors
;
380 addr
= ((ep
->bEndpointAddress
& 0x80) >> 3)
381 | (ep
->bEndpointAddress
& 0x0f);
382 __set_bit(addr
, f
->endpoints
);
385 result
= f
->set_alt(f
, tmp
, 0);
387 debug("interface %d (%s/%p) alt 0 --> %d\n",
388 tmp
, f
->name
, f
, result
);
395 /* when we return, be sure our power usage is valid */
396 power
= c
->bMaxPower
? (2 * c
->bMaxPower
) : CONFIG_USB_GADGET_VBUS_DRAW
;
398 usb_gadget_vbus_draw(gadget
, power
);
403 * usb_add_config() - add a configuration to a device.
404 * @cdev: wraps the USB gadget
405 * @config: the configuration, with bConfigurationValue assigned
406 * Context: single threaded during gadget setup
408 * One of the main tasks of a composite driver's bind() routine is to
409 * add each of the configurations it supports, using this routine.
411 * This function returns the value of the configuration's bind(), which
412 * is zero for success else a negative errno value. Binding configurations
413 * assigns global resources including string IDs, and per-configuration
414 * resources such as interface IDs and endpoints.
416 int usb_add_config(struct usb_composite_dev
*cdev
,
417 struct usb_configuration
*config
)
419 int status
= -EINVAL
;
420 struct usb_configuration
*c
;
421 struct usb_function
*f
;
424 debug("%s: adding config #%u '%s'/%p\n", __func__
,
425 config
->bConfigurationValue
,
426 config
->label
, config
);
428 if (!config
->bConfigurationValue
|| !config
->bind
)
431 /* Prevent duplicate configuration identifiers */
432 list_for_each_entry(c
, &cdev
->configs
, list
) {
433 if (c
->bConfigurationValue
== config
->bConfigurationValue
) {
440 list_add_tail(&config
->list
, &cdev
->configs
);
442 INIT_LIST_HEAD(&config
->functions
);
443 config
->next_interface_id
= 0;
445 status
= config
->bind(config
);
447 list_del(&config
->list
);
450 debug("cfg %d/%p speeds:%s%s\n",
451 config
->bConfigurationValue
, config
,
452 config
->highspeed
? " high" : "",
454 ? (gadget_is_dualspeed(cdev
->gadget
)
459 for (i
= 0; i
< MAX_CONFIG_INTERFACES
; i
++) {
460 f
= config
->interface
[i
];
463 debug("%s: interface %d = %s/%p\n",
464 __func__
, i
, f
->name
, f
);
468 usb_ep_autoconfig_reset(cdev
->gadget
);
472 debug("added config '%s'/%u --> %d\n", config
->label
,
473 config
->bConfigurationValue
, status
);
478 * We support strings in multiple languages ... string descriptor zero
479 * says which languages are supported. The typical case will be that
480 * only one language (probably English) is used, with I18N handled on
484 static void collect_langs(struct usb_gadget_strings
**sp
, __le16
*buf
)
486 const struct usb_gadget_strings
*s
;
492 language
= cpu_to_le16(s
->language
);
493 for (tmp
= buf
; *tmp
&& tmp
< &buf
[126]; tmp
++) {
494 if (*tmp
== language
)
503 static int lookup_string(
504 struct usb_gadget_strings
**sp
,
511 struct usb_gadget_strings
*s
;
515 if (s
->language
!= language
)
517 value
= usb_gadget_get_string(s
, id
, buf
);
524 static int get_string(struct usb_composite_dev
*cdev
,
525 void *buf
, u16 language
, int id
)
527 struct usb_string_descriptor
*s
= buf
;
528 struct usb_gadget_strings
**sp
;
530 struct usb_configuration
*c
;
531 struct usb_function
*f
;
534 * Yes, not only is USB's I18N support probably more than most
535 * folk will ever care about ... also, it's all supported here.
536 * (Except for UTF8 support for Unicode's "Astral Planes".)
539 /* 0 == report all available language codes */
542 s
->bDescriptorType
= USB_DT_STRING
;
544 sp
= composite
->strings
;
546 collect_langs(sp
, s
->wData
);
548 list_for_each_entry(c
, &cdev
->configs
, list
) {
551 collect_langs(sp
, s
->wData
);
553 list_for_each_entry(f
, &c
->functions
, list
) {
556 collect_langs(sp
, s
->wData
);
560 for (len
= 0; len
<= 126 && s
->wData
[len
]; len
++)
565 s
->bLength
= 2 * (len
+ 1);
570 * Otherwise, look up and return a specified string. String IDs
571 * are device-scoped, so we look up each string table we're told
572 * about. These lookups are infrequent; simpler-is-better here.
574 if (composite
->strings
) {
575 len
= lookup_string(composite
->strings
, buf
, language
, id
);
579 list_for_each_entry(c
, &cdev
->configs
, list
) {
581 len
= lookup_string(c
->strings
, buf
, language
, id
);
585 list_for_each_entry(f
, &c
->functions
, list
) {
588 len
= lookup_string(f
->strings
, buf
, language
, id
);
597 * usb_string_id() - allocate an unused string ID
598 * @cdev: the device whose string descriptor IDs are being allocated
599 * Context: single threaded during gadget setup
601 * @usb_string_id() is called from bind() callbacks to allocate
602 * string IDs. Drivers for functions, configurations, or gadgets will
603 * then store that ID in the appropriate descriptors and string table.
605 * All string identifier should be allocated using this,
606 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
607 * that for example different functions don't wrongly assign different
608 * meanings to the same identifier.
610 int usb_string_id(struct usb_composite_dev
*cdev
)
612 if (cdev
->next_string_id
< 254) {
614 * string id 0 is reserved by USB spec for list of
615 * supported languages
616 * 255 reserved as well? -- mina86
618 cdev
->next_string_id
++;
619 return cdev
->next_string_id
;
625 * usb_string_ids() - allocate unused string IDs in batch
626 * @cdev: the device whose string descriptor IDs are being allocated
627 * @str: an array of usb_string objects to assign numbers to
628 * Context: single threaded during gadget setup
630 * @usb_string_ids() is called from bind() callbacks to allocate
631 * string IDs. Drivers for functions, configurations, or gadgets will
632 * then copy IDs from the string table to the appropriate descriptors
633 * and string table for other languages.
635 * All string identifier should be allocated using this,
636 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
637 * example different functions don't wrongly assign different meanings
638 * to the same identifier.
640 int usb_string_ids_tab(struct usb_composite_dev
*cdev
, struct usb_string
*str
)
642 u8 next
= cdev
->next_string_id
;
644 for (; str
->s
; ++str
) {
650 cdev
->next_string_id
= next
;
656 * usb_string_ids_n() - allocate unused string IDs in batch
657 * @c: the device whose string descriptor IDs are being allocated
658 * @n: number of string IDs to allocate
659 * Context: single threaded during gadget setup
661 * Returns the first requested ID. This ID and next @n-1 IDs are now
662 * valid IDs. At least provided that @n is non-zero because if it
663 * is, returns last requested ID which is now very useful information.
665 * @usb_string_ids_n() is called from bind() callbacks to allocate
666 * string IDs. Drivers for functions, configurations, or gadgets will
667 * then store that ID in the appropriate descriptors and string table.
669 * All string identifier should be allocated using this,
670 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
671 * example different functions don't wrongly assign different meanings
672 * to the same identifier.
674 int usb_string_ids_n(struct usb_composite_dev
*c
, unsigned n
)
676 u8 next
= c
->next_string_id
;
678 if (n
> 254 || next
+ n
> 254)
681 c
->next_string_id
+= n
;
685 static void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
687 if (req
->status
|| req
->actual
!= req
->length
)
688 debug("%s: setup complete --> %d, %d/%d\n", __func__
,
689 req
->status
, req
->actual
, req
->length
);
693 * The setup() callback implements all the ep0 functionality that's
694 * not handled lower down, in hardware or the hardware driver(like
695 * device and endpoint feature flags, and their status). It's all
696 * housekeeping for the gadget function we're implementing. Most of
697 * the work is in config and function specific setup.
700 composite_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
702 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
703 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
704 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
705 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
706 u8 intf
= w_index
& 0xFF;
707 int value
= -EOPNOTSUPP
;
708 struct usb_request
*req
= cdev
->req
;
709 struct usb_function
*f
= NULL
;
712 struct usb_configuration
*c
;
715 * partial re-init of the response message; the function or the
716 * gadget might need to intercept e.g. a control-OUT completion
717 * when we delegate to it.
720 req
->complete
= composite_setup_complete
;
721 req
->length
= USB_BUFSIZ
;
722 gadget
->ep0
->driver_data
= cdev
;
723 standard
= (ctrl
->bRequestType
& USB_TYPE_MASK
)
724 == USB_TYPE_STANDARD
;
728 switch (ctrl
->bRequest
) {
730 /* we handle all standard USB descriptors */
731 case USB_REQ_GET_DESCRIPTOR
:
732 if (ctrl
->bRequestType
!= USB_DIR_IN
)
734 switch (w_value
>> 8) {
737 cdev
->desc
.bNumConfigurations
=
738 count_configs(cdev
, USB_DT_DEVICE
);
739 value
= min(w_length
, (u16
) sizeof cdev
->desc
);
740 memcpy(req
->buf
, &cdev
->desc
, value
);
742 case USB_DT_DEVICE_QUALIFIER
:
743 if (!gadget_is_dualspeed(gadget
))
746 value
= min_t(int, w_length
,
747 sizeof(struct usb_qualifier_descriptor
));
749 case USB_DT_OTHER_SPEED_CONFIG
:
750 if (!gadget_is_dualspeed(gadget
))
754 value
= config_desc(cdev
, w_value
);
756 value
= min(w_length
, (u16
) value
);
759 value
= get_string(cdev
, req
->buf
,
760 w_index
, w_value
& 0xff);
762 value
= min(w_length
, (u16
) value
);
766 * The USB compliance test (USB 2.0 Command Verifier)
767 * issues this request. We should not run into the
768 * default path here. But return for now until
769 * the superspeed support is added.
777 /* any number of configs can work */
778 case USB_REQ_SET_CONFIGURATION
:
779 if (ctrl
->bRequestType
!= 0)
781 if (gadget_is_otg(gadget
)) {
782 if (gadget
->a_hnp_support
)
783 debug("HNP available\n");
784 else if (gadget
->a_alt_hnp_support
)
785 debug("HNP on another port\n");
787 debug("HNP inactive\n");
790 value
= set_config(cdev
, ctrl
, w_value
);
792 case USB_REQ_GET_CONFIGURATION
:
793 if (ctrl
->bRequestType
!= USB_DIR_IN
)
796 *(u8
*)req
->buf
= cdev
->config
->bConfigurationValue
;
799 value
= min(w_length
, (u16
) 1);
803 * function drivers must handle get/set altsetting; if there's
804 * no get() method, we know only altsetting zero works.
806 case USB_REQ_SET_INTERFACE
:
807 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
809 if (!cdev
->config
|| w_index
>= MAX_CONFIG_INTERFACES
)
811 f
= cdev
->config
->interface
[intf
];
814 if (w_value
&& !f
->set_alt
)
816 value
= f
->set_alt(f
, w_index
, w_value
);
818 case USB_REQ_GET_INTERFACE
:
819 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
821 if (!cdev
->config
|| w_index
>= MAX_CONFIG_INTERFACES
)
823 f
= cdev
->config
->interface
[intf
];
826 /* lots of interfaces only need altsetting zero... */
827 value
= f
->get_alt
? f
->get_alt(f
, w_index
) : 0;
830 *((u8
*)req
->buf
) = value
;
831 value
= min(w_length
, (u16
) 1);
835 debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
836 ctrl
->bRequestType
, ctrl
->bRequest
,
837 w_value
, w_index
, w_length
);
840 * functions always handle their interfaces and endpoints...
841 * punt other recipients (other, WUSB, ...) to the current
842 * configuration code.
844 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
845 case USB_RECIP_INTERFACE
:
846 f
= cdev
->config
->interface
[intf
];
849 case USB_RECIP_ENDPOINT
:
850 endp
= ((w_index
& 0x80) >> 3) | (w_index
& 0x0f);
851 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
852 if (test_bit(endp
, f
->endpoints
))
855 if (&f
->list
== &cdev
->config
->functions
)
859 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
860 * for non-standard request (w_value = 0x21,
861 * bRequest = GET_DESCRIPTOR in this case).
862 * When only one interface is registered (as it is done now),
863 * then this request shall be handled as it was requested for
866 * In the below code it is checked if only one interface is
867 * present and proper function for it is extracted. Due to that
868 * function's setup (f->setup) is called to handle this
869 * special non-standard request.
871 case USB_RECIP_DEVICE
:
872 debug("cdev->config->next_interface_id: %d intf: %d\n",
873 cdev
->config
->next_interface_id
, intf
);
874 if (cdev
->config
->next_interface_id
== 1)
875 f
= cdev
->config
->interface
[intf
];
880 value
= f
->setup(f
, ctrl
);
884 value
= c
->setup(c
, ctrl
);
890 /* respond with data transfer before status phase? */
893 req
->zero
= value
< w_length
;
894 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_KERNEL
);
896 debug("ep_queue --> %d\n", value
);
898 composite_setup_complete(gadget
->ep0
, req
);
903 /* device either stalls (value < 0) or reports success */
907 static void composite_disconnect(struct usb_gadget
*gadget
)
909 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
913 if (composite
->disconnect
)
914 composite
->disconnect(cdev
);
917 static void composite_unbind(struct usb_gadget
*gadget
)
919 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
920 struct usb_configuration
*c
;
921 struct usb_function
*f
;
924 * composite_disconnect() must already have been called
925 * by the underlying peripheral controller driver!
926 * so there's no i/o concurrency that could affect the
927 * state protected by cdev->lock.
929 BUG_ON(cdev
->config
);
931 while (!list_empty(&cdev
->configs
)) {
932 c
= list_first_entry(&cdev
->configs
,
933 struct usb_configuration
, list
);
934 while (!list_empty(&c
->functions
)) {
935 f
= list_first_entry(&c
->functions
,
936 struct usb_function
, list
);
939 debug("unbind function '%s'/%p\n",
946 debug("unbind config '%s'/%p\n", c
->label
, c
);
950 if (composite
->unbind
)
951 composite
->unbind(cdev
);
954 kfree(cdev
->req
->buf
);
955 usb_ep_free_request(gadget
->ep0
, cdev
->req
);
958 set_gadget_data(gadget
, NULL
);
963 static int composite_bind(struct usb_gadget
*gadget
)
965 int status
= -ENOMEM
;
966 struct usb_composite_dev
*cdev
;
968 cdev
= calloc(sizeof *cdev
, 1);
972 cdev
->gadget
= gadget
;
973 set_gadget_data(gadget
, cdev
);
974 INIT_LIST_HEAD(&cdev
->configs
);
976 /* preallocate control response and buffer */
977 cdev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
980 cdev
->req
->buf
= memalign(CONFIG_SYS_CACHELINE_SIZE
, USB_BUFSIZ
);
983 cdev
->req
->complete
= composite_setup_complete
;
984 gadget
->ep0
->driver_data
= cdev
;
986 cdev
->bufsiz
= USB_BUFSIZ
;
987 cdev
->driver
= composite
;
989 usb_gadget_set_selfpowered(gadget
);
990 usb_ep_autoconfig_reset(cdev
->gadget
);
992 status
= composite
->bind(cdev
);
996 memcpy(&cdev
->desc
, composite
->dev
,
997 sizeof(struct usb_device_descriptor
));
998 cdev
->desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
1000 debug("%s: ready\n", composite
->name
);
1004 composite_unbind(gadget
);
1009 composite_suspend(struct usb_gadget
*gadget
)
1011 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1012 struct usb_function
*f
;
1014 debug("%s: suspend\n", __func__
);
1016 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1021 if (composite
->suspend
)
1022 composite
->suspend(cdev
);
1024 cdev
->suspended
= 1;
1028 composite_resume(struct usb_gadget
*gadget
)
1030 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1031 struct usb_function
*f
;
1033 debug("%s: resume\n", __func__
);
1034 if (composite
->resume
)
1035 composite
->resume(cdev
);
1037 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1043 cdev
->suspended
= 0;
1046 static struct usb_gadget_driver composite_driver
= {
1047 .speed
= USB_SPEED_HIGH
,
1049 .bind
= composite_bind
,
1050 .unbind
= composite_unbind
,
1052 .setup
= composite_setup
,
1053 .disconnect
= composite_disconnect
,
1055 .suspend
= composite_suspend
,
1056 .resume
= composite_resume
,
1060 * usb_composite_register() - register a composite driver
1061 * @driver: the driver to register
1062 * Context: single threaded during gadget setup
1064 * This function is used to register drivers using the composite driver
1065 * framework. The return value is zero, or a negative errno value.
1066 * Those values normally come from the driver's @bind method, which does
1067 * all the work of setting up the driver to match the hardware.
1069 * On successful return, the gadget is ready to respond to requests from
1070 * the host, unless one of its components invokes usb_gadget_disconnect()
1071 * while it was binding. That would usually be done in order to wait for
1072 * some userspace participation.
1074 int usb_composite_register(struct usb_composite_driver
*driver
)
1076 if (!driver
|| !driver
->dev
|| !driver
->bind
|| composite
)
1080 driver
->name
= "composite";
1083 return usb_gadget_register_driver(&composite_driver
);
1087 * usb_composite_unregister() - unregister a composite driver
1088 * @driver: the driver to unregister
1090 * This function is used to unregister drivers using the composite
1093 void usb_composite_unregister(struct usb_composite_driver
*driver
)
1095 if (composite
!= driver
)
1097 usb_gadget_unregister_driver(&composite_driver
);