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>
10 #include <linux/bitops.h>
11 #include <linux/usb/composite.h>
13 #define USB_BUFSIZ 4096
15 static struct usb_composite_driver *composite;
18 * usb_add_function() - add a function to a configuration
19 * @config: the configuration
20 * @function: the function being added
21 * Context: single threaded during gadget setup
23 * After initialization, each configuration must have one or more
24 * functions added to it. Adding a function involves calling its @bind()
25 * method to allocate resources such as interface and string identifiers
28 * This function returns the value of the function's bind(), which is
29 * zero for success else a negative errno value.
31 int usb_add_function(struct usb_configuration *config,
32 struct usb_function *function)
36 debug("adding '%s'/%p to config '%s'/%p\n",
37 function->name, function,
38 config->label, config);
40 if (!function->set_alt || !function->disable)
43 function->config = config;
44 list_add_tail(&function->list, &config->functions);
47 value = function->bind(config, function);
49 list_del(&function->list);
50 function->config = NULL;
55 if (!config->fullspeed && function->descriptors)
56 config->fullspeed = 1;
57 if (!config->highspeed && function->hs_descriptors)
58 config->highspeed = 1;
62 debug("adding '%s'/%p --> %d\n",
63 function->name, function, value);
68 * usb_function_deactivate - prevent function and gadget enumeration
69 * @function: the function that isn't yet ready to respond
71 * Blocks response of the gadget driver to host enumeration by
72 * preventing the data line pullup from being activated. This is
73 * normally called during @bind() processing to change from the
74 * initial "ready to respond" state, or when a required resource
77 * For example, drivers that serve as a passthrough to a userspace
78 * daemon can block enumeration unless that daemon (such as an OBEX,
79 * MTP, or print server) is ready to handle host requests.
81 * Not all systems support software control of their USB peripheral
84 * Returns zero on success, else negative errno.
86 int usb_function_deactivate(struct usb_function *function)
88 struct usb_composite_dev *cdev = function->config->cdev;
91 if (cdev->deactivations == 0)
92 status = usb_gadget_disconnect(cdev->gadget);
94 cdev->deactivations++;
100 * usb_function_activate - allow function and gadget enumeration
101 * @function: function on which usb_function_activate() was called
103 * Reverses effect of usb_function_deactivate(). If no more functions
104 * are delaying their activation, the gadget driver will respond to
105 * host enumeration procedures.
107 * Returns zero on success, else negative errno.
109 int usb_function_activate(struct usb_function *function)
111 struct usb_composite_dev *cdev = function->config->cdev;
114 if (cdev->deactivations == 0)
117 cdev->deactivations--;
118 if (cdev->deactivations == 0)
119 status = usb_gadget_connect(cdev->gadget);
126 * usb_interface_id() - allocate an unused interface ID
127 * @config: configuration associated with the interface
128 * @function: function handling the interface
129 * Context: single threaded during gadget setup
131 * usb_interface_id() is called from usb_function.bind() callbacks to
132 * allocate new interface IDs. The function driver will then store that
133 * ID in interface, association, CDC union, and other descriptors. It
134 * will also handle any control requests targetted at that interface,
135 * particularly changing its altsetting via set_alt(). There may
136 * also be class-specific or vendor-specific requests to handle.
138 * All interface identifier should be allocated using this routine, to
139 * ensure that for example different functions don't wrongly assign
140 * different meanings to the same identifier. Note that since interface
141 * identifers are configuration-specific, functions used in more than
142 * one configuration (or more than once in a given configuration) need
143 * multiple versions of the relevant descriptors.
145 * Returns the interface ID which was allocated; or -ENODEV if no
146 * more interface IDs can be allocated.
148 int usb_interface_id(struct usb_configuration *config,
149 struct usb_function *function)
151 unsigned char id = config->next_interface_id;
153 if (id < MAX_CONFIG_INTERFACES) {
154 config->interface[id] = function;
155 config->next_interface_id = id + 1;
161 static int config_buf(struct usb_configuration *config,
162 enum usb_device_speed speed, void *buf, u8 type)
164 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
165 void *next = buf + USB_DT_CONFIG_SIZE;
166 struct usb_descriptor_header **descriptors;
167 struct usb_config_descriptor *c;
169 struct usb_function *f;
171 /* write the config descriptor */
173 c->bLength = USB_DT_CONFIG_SIZE;
174 c->bDescriptorType = type;
176 c->bNumInterfaces = config->next_interface_id;
177 c->bConfigurationValue = config->bConfigurationValue;
178 c->iConfiguration = config->iConfiguration;
179 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
180 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
182 /* There may be e.g. OTG descriptors */
183 if (config->descriptors) {
184 status = usb_descriptor_fillbuf(next, len,
185 config->descriptors);
192 /* add each function's descriptors */
193 list_for_each_entry(f, &config->functions, list) {
194 if (speed == USB_SPEED_HIGH)
195 descriptors = f->hs_descriptors;
197 descriptors = f->descriptors;
200 status = usb_descriptor_fillbuf(next, len,
201 (const struct usb_descriptor_header **) descriptors);
209 c->wTotalLength = cpu_to_le16(len);
213 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
215 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
216 struct usb_gadget *gadget = cdev->gadget;
217 u8 type = w_value >> 8;
219 struct usb_configuration *c;
221 if (gadget_is_dualspeed(gadget)) {
222 if (gadget->speed == USB_SPEED_HIGH)
224 if (type == USB_DT_OTHER_SPEED_CONFIG)
227 speed = USB_SPEED_HIGH;
231 list_for_each_entry(c, &cdev->configs, list) {
232 if (speed == USB_SPEED_HIGH) {
240 return config_buf(c, speed, cdev->req->buf, type);
246 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
248 struct usb_gadget *gadget = cdev->gadget;
251 struct usb_configuration *c;
253 if (gadget_is_dualspeed(gadget)) {
254 if (gadget->speed == USB_SPEED_HIGH)
256 if (type == USB_DT_DEVICE_QUALIFIER)
259 list_for_each_entry(c, &cdev->configs, list) {
260 /* ignore configs that won't work at this speed */
273 static void device_qual(struct usb_composite_dev *cdev)
275 struct usb_qualifier_descriptor *qual = cdev->req->buf;
277 qual->bLength = sizeof(*qual);
278 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
279 /* POLICY: same bcdUSB and device type info at both speeds */
280 qual->bcdUSB = cdev->desc.bcdUSB;
281 qual->bDeviceClass = cdev->desc.bDeviceClass;
282 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
283 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
284 /* ASSUME same EP0 fifo size at both speeds */
285 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
286 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
290 static void reset_config(struct usb_composite_dev *cdev)
292 struct usb_function *f;
294 debug("%s:\n", __func__);
296 list_for_each_entry(f, &cdev->config->functions, list) {
300 bitmap_zero(f->endpoints, 32);
305 static int set_config(struct usb_composite_dev *cdev,
306 const struct usb_ctrlrequest *ctrl, unsigned number)
308 struct usb_gadget *gadget = cdev->gadget;
309 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
310 struct usb_descriptor_header **descriptors;
311 int result = -EINVAL;
312 struct usb_endpoint_descriptor *ep;
313 struct usb_configuration *c = NULL;
316 struct usb_function *f;
322 list_for_each_entry(c, &cdev->configs, list) {
323 if (c->bConfigurationValue == number) {
333 debug("%s: %s speed config #%d: %s\n", __func__,
335 switch (gadget->speed) {
350 }), number, c ? c->label : "unconfigured");
357 /* Initialize all interfaces by setting them to altsetting zero. */
358 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
359 f = c->interface[tmp];
364 * Record which endpoints are used by the function. This is used
365 * to dispatch control requests targeted at that endpoint to the
366 * function's setup callback instead of the current
367 * configuration's setup callback.
369 if (gadget->speed == USB_SPEED_HIGH)
370 descriptors = f->hs_descriptors;
372 descriptors = f->descriptors;
374 for (; *descriptors; ++descriptors) {
375 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
378 ep = (struct usb_endpoint_descriptor *)*descriptors;
379 addr = ((ep->bEndpointAddress & 0x80) >> 3)
380 | (ep->bEndpointAddress & 0x0f);
381 generic_set_bit(addr, f->endpoints);
384 result = f->set_alt(f, tmp, 0);
386 debug("interface %d (%s/%p) alt 0 --> %d\n",
387 tmp, f->name, f, result);
394 /* when we return, be sure our power usage is valid */
395 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
397 usb_gadget_vbus_draw(gadget, power);
402 * usb_add_config() - add a configuration to a device.
403 * @cdev: wraps the USB gadget
404 * @config: the configuration, with bConfigurationValue assigned
405 * Context: single threaded during gadget setup
407 * One of the main tasks of a composite driver's bind() routine is to
408 * add each of the configurations it supports, using this routine.
410 * This function returns the value of the configuration's bind(), which
411 * is zero for success else a negative errno value. Binding configurations
412 * assigns global resources including string IDs, and per-configuration
413 * resources such as interface IDs and endpoints.
415 int usb_add_config(struct usb_composite_dev *cdev,
416 struct usb_configuration *config)
418 int status = -EINVAL;
419 struct usb_configuration *c;
420 struct usb_function *f;
423 debug("%s: adding config #%u '%s'/%p\n", __func__,
424 config->bConfigurationValue,
425 config->label, config);
427 if (!config->bConfigurationValue || !config->bind)
430 /* Prevent duplicate configuration identifiers */
431 list_for_each_entry(c, &cdev->configs, list) {
432 if (c->bConfigurationValue == config->bConfigurationValue) {
439 list_add_tail(&config->list, &cdev->configs);
441 INIT_LIST_HEAD(&config->functions);
442 config->next_interface_id = 0;
444 status = config->bind(config);
446 list_del(&config->list);
449 debug("cfg %d/%p speeds:%s%s\n",
450 config->bConfigurationValue, config,
451 config->highspeed ? " high" : "",
453 ? (gadget_is_dualspeed(cdev->gadget)
458 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
459 f = config->interface[i];
462 debug("%s: interface %d = %s/%p\n",
463 __func__, i, f->name, f);
467 usb_ep_autoconfig_reset(cdev->gadget);
471 debug("added config '%s'/%u --> %d\n", config->label,
472 config->bConfigurationValue, status);
477 * We support strings in multiple languages ... string descriptor zero
478 * says which languages are supported. The typical case will be that
479 * only one language (probably English) is used, with I18N handled on
483 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
485 const struct usb_gadget_strings *s;
491 language = cpu_to_le16(s->language);
492 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
493 if (*tmp == language)
502 static int lookup_string(
503 struct usb_gadget_strings **sp,
510 struct usb_gadget_strings *s;
514 if (s->language != language)
516 value = usb_gadget_get_string(s, id, buf);
523 static int get_string(struct usb_composite_dev *cdev,
524 void *buf, u16 language, int id)
526 struct usb_string_descriptor *s = buf;
527 struct usb_gadget_strings **sp;
529 struct usb_configuration *c;
530 struct usb_function *f;
533 * Yes, not only is USB's I18N support probably more than most
534 * folk will ever care about ... also, it's all supported here.
535 * (Except for UTF8 support for Unicode's "Astral Planes".)
538 /* 0 == report all available language codes */
541 s->bDescriptorType = USB_DT_STRING;
543 sp = composite->strings;
545 collect_langs(sp, s->wData);
547 list_for_each_entry(c, &cdev->configs, list) {
550 collect_langs(sp, s->wData);
552 list_for_each_entry(f, &c->functions, list) {
555 collect_langs(sp, s->wData);
559 for (len = 0; len <= 126 && s->wData[len]; len++)
564 s->bLength = 2 * (len + 1);
569 * Otherwise, look up and return a specified string. String IDs
570 * are device-scoped, so we look up each string table we're told
571 * about. These lookups are infrequent; simpler-is-better here.
573 if (composite->strings) {
574 len = lookup_string(composite->strings, buf, language, id);
578 list_for_each_entry(c, &cdev->configs, list) {
580 len = lookup_string(c->strings, buf, language, id);
584 list_for_each_entry(f, &c->functions, list) {
587 len = lookup_string(f->strings, buf, language, id);
596 * usb_string_id() - allocate an unused string ID
597 * @cdev: the device whose string descriptor IDs are being allocated
598 * Context: single threaded during gadget setup
600 * @usb_string_id() is called from bind() callbacks to allocate
601 * string IDs. Drivers for functions, configurations, or gadgets will
602 * then store that ID in the appropriate descriptors and string table.
604 * All string identifier should be allocated using this,
605 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
606 * that for example different functions don't wrongly assign different
607 * meanings to the same identifier.
609 int usb_string_id(struct usb_composite_dev *cdev)
611 if (cdev->next_string_id < 254) {
613 * string id 0 is reserved by USB spec for list of
614 * supported languages
615 * 255 reserved as well? -- mina86
617 cdev->next_string_id++;
618 return cdev->next_string_id;
624 * usb_string_ids() - allocate unused string IDs in batch
625 * @cdev: the device whose string descriptor IDs are being allocated
626 * @str: an array of usb_string objects to assign numbers to
627 * Context: single threaded during gadget setup
629 * @usb_string_ids() is called from bind() callbacks to allocate
630 * string IDs. Drivers for functions, configurations, or gadgets will
631 * then copy IDs from the string table to the appropriate descriptors
632 * and string table for other languages.
634 * All string identifier should be allocated using this,
635 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
636 * example different functions don't wrongly assign different meanings
637 * to the same identifier.
639 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
641 u8 next = cdev->next_string_id;
643 for (; str->s; ++str) {
649 cdev->next_string_id = next;
655 * usb_string_ids_n() - allocate unused string IDs in batch
656 * @c: the device whose string descriptor IDs are being allocated
657 * @n: number of string IDs to allocate
658 * Context: single threaded during gadget setup
660 * Returns the first requested ID. This ID and next @n-1 IDs are now
661 * valid IDs. At least provided that @n is non-zero because if it
662 * is, returns last requested ID which is now very useful information.
664 * @usb_string_ids_n() is called from bind() callbacks to allocate
665 * string IDs. Drivers for functions, configurations, or gadgets will
666 * then store that ID in the appropriate descriptors and string table.
668 * All string identifier should be allocated using this,
669 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
670 * example different functions don't wrongly assign different meanings
671 * to the same identifier.
673 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
675 u8 next = c->next_string_id;
677 if (n > 254 || next + n > 254)
680 c->next_string_id += n;
684 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
686 if (req->status || req->actual != req->length)
687 debug("%s: setup complete --> %d, %d/%d\n", __func__,
688 req->status, req->actual, req->length);
692 * The setup() callback implements all the ep0 functionality that's
693 * not handled lower down, in hardware or the hardware driver(like
694 * device and endpoint feature flags, and their status). It's all
695 * housekeeping for the gadget function we're implementing. Most of
696 * the work is in config and function specific setup.
699 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
701 u16 w_length = le16_to_cpu(ctrl->wLength);
702 u16 w_index = le16_to_cpu(ctrl->wIndex);
703 u16 w_value = le16_to_cpu(ctrl->wValue);
704 struct usb_composite_dev *cdev = get_gadget_data(gadget);
705 u8 intf = w_index & 0xFF;
706 int value = -EOPNOTSUPP;
707 struct usb_request *req = cdev->req;
708 struct usb_function *f = NULL;
711 struct usb_configuration *c;
714 * partial re-init of the response message; the function or the
715 * gadget might need to intercept e.g. a control-OUT completion
716 * when we delegate to it.
719 req->complete = composite_setup_complete;
720 req->length = USB_BUFSIZ;
721 gadget->ep0->driver_data = cdev;
722 standard = (ctrl->bRequestType & USB_TYPE_MASK)
723 == USB_TYPE_STANDARD;
727 switch (ctrl->bRequest) {
729 /* we handle all standard USB descriptors */
730 case USB_REQ_GET_DESCRIPTOR:
731 if (ctrl->bRequestType != USB_DIR_IN)
733 switch (w_value >> 8) {
736 cdev->desc.bNumConfigurations =
737 count_configs(cdev, USB_DT_DEVICE);
740 * If the speed is Super speed, then the supported
741 * max packet size is 512 and it should be sent as
742 * exponent of 2. So, 9(2^9=512) should be filled in
743 * bMaxPacketSize0. Also fill USB version as 3.0
744 * if speed is Super speed.
746 if (cdev->gadget->speed == USB_SPEED_SUPER) {
747 cdev->desc.bMaxPacketSize0 = 9;
748 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
750 cdev->desc.bMaxPacketSize0 =
751 cdev->gadget->ep0->maxpacket;
753 value = min(w_length, (u16) sizeof cdev->desc);
754 memcpy(req->buf, &cdev->desc, value);
756 case USB_DT_DEVICE_QUALIFIER:
757 if (!gadget_is_dualspeed(gadget))
760 value = min_t(int, w_length,
761 sizeof(struct usb_qualifier_descriptor));
763 case USB_DT_OTHER_SPEED_CONFIG:
764 if (!gadget_is_dualspeed(gadget))
768 value = config_desc(cdev, w_value);
770 value = min(w_length, (u16) value);
773 value = get_string(cdev, req->buf,
774 w_index, w_value & 0xff);
776 value = min(w_length, (u16) value);
780 * The USB compliance test (USB 2.0 Command Verifier)
781 * issues this request. We should not run into the
782 * default path here. But return for now until
783 * the superspeed support is added.
791 /* any number of configs can work */
792 case USB_REQ_SET_CONFIGURATION:
793 if (ctrl->bRequestType != 0)
795 if (gadget_is_otg(gadget)) {
796 if (gadget->a_hnp_support)
797 debug("HNP available\n");
798 else if (gadget->a_alt_hnp_support)
799 debug("HNP on another port\n");
801 debug("HNP inactive\n");
804 value = set_config(cdev, ctrl, w_value);
806 case USB_REQ_GET_CONFIGURATION:
807 if (ctrl->bRequestType != USB_DIR_IN)
810 *(u8 *)req->buf = cdev->config->bConfigurationValue;
813 value = min(w_length, (u16) 1);
817 * function drivers must handle get/set altsetting; if there's
818 * no get() method, we know only altsetting zero works.
820 case USB_REQ_SET_INTERFACE:
821 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
823 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
825 f = cdev->config->interface[intf];
828 if (w_value && !f->set_alt)
830 value = f->set_alt(f, w_index, w_value);
832 case USB_REQ_GET_INTERFACE:
833 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
835 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
837 f = cdev->config->interface[intf];
840 /* lots of interfaces only need altsetting zero... */
841 value = f->get_alt ? f->get_alt(f, w_index) : 0;
844 *((u8 *)req->buf) = value;
845 value = min(w_length, (u16) 1);
849 debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
850 ctrl->bRequestType, ctrl->bRequest,
851 w_value, w_index, w_length);
857 * functions always handle their interfaces and endpoints...
858 * punt other recipients (other, WUSB, ...) to the current
859 * configuration code.
861 switch (ctrl->bRequestType & USB_RECIP_MASK) {
862 case USB_RECIP_INTERFACE:
863 f = cdev->config->interface[intf];
866 case USB_RECIP_ENDPOINT:
867 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
868 list_for_each_entry(f, &cdev->config->functions, list) {
869 if (test_bit(endp, f->endpoints))
872 if (&f->list == &cdev->config->functions)
876 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
877 * for non-standard request (w_value = 0x21,
878 * bRequest = GET_DESCRIPTOR in this case).
879 * When only one interface is registered (as it is done now),
880 * then this request shall be handled as it was requested for
883 * In the below code it is checked if only one interface is
884 * present and proper function for it is extracted. Due to that
885 * function's setup (f->setup) is called to handle this
886 * special non-standard request.
888 case USB_RECIP_DEVICE:
889 debug("cdev->config->next_interface_id: %d intf: %d\n",
890 cdev->config->next_interface_id, intf);
891 if (cdev->config->next_interface_id == 1)
892 f = cdev->config->interface[intf];
897 value = f->setup(f, ctrl);
901 value = c->setup(c, ctrl);
907 /* respond with data transfer before status phase? */
910 req->zero = value < w_length;
911 value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
913 debug("ep_queue --> %d\n", value);
915 composite_setup_complete(gadget->ep0, req);
920 /* device either stalls (value < 0) or reports success */
924 static void composite_disconnect(struct usb_gadget *gadget)
926 struct usb_composite_dev *cdev = get_gadget_data(gadget);
930 if (composite->disconnect)
931 composite->disconnect(cdev);
934 static void composite_unbind(struct usb_gadget *gadget)
936 struct usb_composite_dev *cdev = get_gadget_data(gadget);
937 struct usb_configuration *c;
938 struct usb_function *f;
941 * composite_disconnect() must already have been called
942 * by the underlying peripheral controller driver!
943 * so there's no i/o concurrency that could affect the
944 * state protected by cdev->lock.
946 BUG_ON(cdev->config);
948 while (!list_empty(&cdev->configs)) {
949 c = list_first_entry(&cdev->configs,
950 struct usb_configuration, list);
951 while (!list_empty(&c->functions)) {
952 f = list_first_entry(&c->functions,
953 struct usb_function, list);
956 debug("unbind function '%s'/%p\n",
963 debug("unbind config '%s'/%p\n", c->label, c);
968 if (composite->unbind)
969 composite->unbind(cdev);
972 kfree(cdev->req->buf);
973 usb_ep_free_request(gadget->ep0, cdev->req);
976 set_gadget_data(gadget, NULL);
981 static int composite_bind(struct usb_gadget *gadget)
983 int status = -ENOMEM;
984 struct usb_composite_dev *cdev;
986 cdev = calloc(sizeof *cdev, 1);
990 cdev->gadget = gadget;
991 set_gadget_data(gadget, cdev);
992 INIT_LIST_HEAD(&cdev->configs);
994 /* preallocate control response and buffer */
995 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
998 cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
1001 cdev->req->complete = composite_setup_complete;
1002 gadget->ep0->driver_data = cdev;
1004 cdev->bufsiz = USB_BUFSIZ;
1005 cdev->driver = composite;
1007 usb_gadget_set_selfpowered(gadget);
1008 usb_ep_autoconfig_reset(cdev->gadget);
1010 status = composite->bind(cdev);
1014 memcpy(&cdev->desc, composite->dev,
1015 sizeof(struct usb_device_descriptor));
1016 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1018 debug("%s: ready\n", composite->name);
1022 composite_unbind(gadget);
1027 composite_suspend(struct usb_gadget *gadget)
1029 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1030 struct usb_function *f;
1032 debug("%s: suspend\n", __func__);
1034 list_for_each_entry(f, &cdev->config->functions, list) {
1039 if (composite->suspend)
1040 composite->suspend(cdev);
1042 cdev->suspended = 1;
1046 composite_resume(struct usb_gadget *gadget)
1048 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1049 struct usb_function *f;
1051 debug("%s: resume\n", __func__);
1052 if (composite->resume)
1053 composite->resume(cdev);
1055 list_for_each_entry(f, &cdev->config->functions, list) {
1061 cdev->suspended = 0;
1064 static struct usb_gadget_driver composite_driver = {
1065 .speed = USB_SPEED_HIGH,
1067 .bind = composite_bind,
1068 .unbind = composite_unbind,
1070 .setup = composite_setup,
1071 .reset = composite_disconnect,
1072 .disconnect = composite_disconnect,
1074 .suspend = composite_suspend,
1075 .resume = composite_resume,
1079 * usb_composite_register() - register a composite driver
1080 * @driver: the driver to register
1081 * Context: single threaded during gadget setup
1083 * This function is used to register drivers using the composite driver
1084 * framework. The return value is zero, or a negative errno value.
1085 * Those values normally come from the driver's @bind method, which does
1086 * all the work of setting up the driver to match the hardware.
1088 * On successful return, the gadget is ready to respond to requests from
1089 * the host, unless one of its components invokes usb_gadget_disconnect()
1090 * while it was binding. That would usually be done in order to wait for
1091 * some userspace participation.
1093 int usb_composite_register(struct usb_composite_driver *driver)
1097 if (!driver || !driver->dev || !driver->bind || composite)
1101 driver->name = "composite";
1104 res = usb_gadget_register_driver(&composite_driver);
1112 * usb_composite_unregister() - unregister a composite driver
1113 * @driver: the driver to unregister
1115 * This function is used to unregister drivers using the composite
1118 void usb_composite_unregister(struct usb_composite_driver *driver)
1120 if (composite != driver)
1122 usb_gadget_unregister_driver(&composite_driver);