2 * composite.c - infrastructure for Composite USB Gadgets
4 * Copyright (C) 2006-2008 David Brownell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 /* #define VERBOSE_DEBUG */
17 #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
19 #include <linux/kallsyms.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/utsname.h>
26 #include <linux/usb/composite.h>
27 #include <asm/unaligned.h>
29 #include <linux/printk.h>
34 * The code in this file is utility code, used to build a gadget driver
35 * from one or more "function" drivers, one or more "configuration"
36 * objects, and a "usb_composite_driver" by gluing them together along
37 * with the relevant device-wide data.
40 static struct usb_gadget_strings
**get_containers_gs(
41 struct usb_gadget_string_container
*uc
)
43 return (struct usb_gadget_strings
**)uc
->stash
;
47 * next_ep_desc() - advance to the next EP descriptor
48 * @t: currect pointer within descriptor array
50 * Return: next EP descriptor or NULL
52 * Iterate over @t until either EP descriptor found or
53 * NULL (that indicates end of list) encountered
55 static struct usb_descriptor_header
**
56 next_ep_desc(struct usb_descriptor_header
**t
)
59 if ((*t
)->bDescriptorType
== USB_DT_ENDPOINT
)
66 * for_each_ep_desc()- iterate over endpoint descriptors in the
68 * @start: pointer within descriptor array.
69 * @ep_desc: endpoint descriptor to use as the loop cursor
71 #define for_each_ep_desc(start, ep_desc) \
72 for (ep_desc = next_ep_desc(start); \
73 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
76 * config_ep_by_speed() - configures the given endpoint
77 * according to gadget speed.
78 * @g: pointer to the gadget
80 * @_ep: the endpoint to configure
82 * Return: error code, 0 on success
84 * This function chooses the right descriptors for a given
85 * endpoint according to gadget speed and saves it in the
86 * endpoint desc field. If the endpoint already has a descriptor
87 * assigned to it - overwrites it with currently corresponding
88 * descriptor. The endpoint maxpacket field is updated according
89 * to the chosen descriptor.
90 * Note: the supplied function should hold all the descriptors
91 * for supported speeds
93 int config_ep_by_speed(struct usb_gadget
*g
,
94 struct usb_function
*f
,
97 struct usb_composite_dev
*cdev
= get_gadget_data(g
);
98 struct usb_endpoint_descriptor
*chosen_desc
= NULL
;
99 struct usb_descriptor_header
**speed_desc
= NULL
;
101 struct usb_ss_ep_comp_descriptor
*comp_desc
= NULL
;
102 int want_comp_desc
= 0;
104 struct usb_descriptor_header
**d_spd
; /* cursor for speed desc */
106 if (!g
|| !f
|| !_ep
)
109 /* select desired speed */
111 case USB_SPEED_SUPER
:
112 if (gadget_is_superspeed(g
)) {
113 speed_desc
= f
->ss_descriptors
;
117 /* else: Fall trough */
119 if (gadget_is_dualspeed(g
)) {
120 speed_desc
= f
->hs_descriptors
;
123 /* else: fall through */
125 speed_desc
= f
->fs_descriptors
;
127 /* find descriptors */
128 for_each_ep_desc(speed_desc
, d_spd
) {
129 chosen_desc
= (struct usb_endpoint_descriptor
*)*d_spd
;
130 if (chosen_desc
->bEndpointAddress
== _ep
->address
)
137 _ep
->maxpacket
= usb_endpoint_maxp(chosen_desc
);
138 _ep
->desc
= chosen_desc
;
139 _ep
->comp_desc
= NULL
;
146 * Companion descriptor should follow EP descriptor
147 * USB 3.0 spec, #9.6.7
149 comp_desc
= (struct usb_ss_ep_comp_descriptor
*)*(++d_spd
);
151 (comp_desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
))
153 _ep
->comp_desc
= comp_desc
;
154 if (g
->speed
== USB_SPEED_SUPER
) {
155 switch (usb_endpoint_type(_ep
->desc
)) {
156 case USB_ENDPOINT_XFER_ISOC
:
157 /* mult: bits 1:0 of bmAttributes */
158 _ep
->mult
= comp_desc
->bmAttributes
& 0x3;
159 case USB_ENDPOINT_XFER_BULK
:
160 case USB_ENDPOINT_XFER_INT
:
161 _ep
->maxburst
= comp_desc
->bMaxBurst
+ 1;
164 if (comp_desc
->bMaxBurst
!= 0)
165 ERROR(cdev
, "ep0 bMaxBurst must be 0\n");
172 EXPORT_SYMBOL_GPL(config_ep_by_speed
);
175 * usb_add_function() - add a function to a configuration
176 * @config: the configuration
177 * @function: the function being added
178 * Context: single threaded during gadget setup
180 * After initialization, each configuration must have one or more
181 * functions added to it. Adding a function involves calling its @bind()
182 * method to allocate resources such as interface and string identifiers
185 * This function returns the value of the function's bind(), which is
186 * zero for success else a negative errno value.
188 int usb_add_function(struct usb_configuration
*config
,
189 struct usb_function
*function
)
193 pr_debug("[XLOG_DEBUG][USB][COM]%s: \n", __func__
);
195 INFO(config
->cdev
, "adding '%s'/%p to config '%s'/%p\n",
196 function
->name
, function
,
197 config
->label
, config
);
199 if (!function
->set_alt
|| !function
->disable
)
202 function
->config
= config
;
203 list_add_tail(&function
->list
, &config
->functions
);
205 /* REVISIT *require* function->bind? */
206 if (function
->bind
) {
207 value
= function
->bind(config
, function
);
209 list_del(&function
->list
);
210 function
->config
= NULL
;
215 /* We allow configurations that don't work at both speeds.
216 * If we run into a lowspeed Linux system, treat it the same
217 * as full speed ... it's the function drivers that will need
218 * to avoid bulk and ISO transfers.
220 if (!config
->fullspeed
&& function
->fs_descriptors
)
221 config
->fullspeed
= true;
222 if (!config
->highspeed
&& function
->hs_descriptors
)
223 config
->highspeed
= true;
224 if (!config
->superspeed
&& function
->ss_descriptors
)
225 config
->superspeed
= true;
229 INFO(config
->cdev
, "adding '%s'/%p --> %d\n",
230 function
->name
, function
, value
);
233 EXPORT_SYMBOL_GPL(usb_add_function
);
235 void usb_remove_function(struct usb_configuration
*c
, struct usb_function
*f
)
238 INFO(c
->cdev
, "disable function '%s'/%p\n", f
->name
, f
);
242 bitmap_zero(f
->endpoints
, 32);
245 INFO(c
->cdev
, "unbind function '%s'/%p\n", f
->name
, f
);
249 EXPORT_SYMBOL_GPL(usb_remove_function
);
252 * usb_function_deactivate - prevent function and gadget enumeration
253 * @function: the function that isn't yet ready to respond
255 * Blocks response of the gadget driver to host enumeration by
256 * preventing the data line pullup from being activated. This is
257 * normally called during @bind() processing to change from the
258 * initial "ready to respond" state, or when a required resource
261 * For example, drivers that serve as a passthrough to a userspace
262 * daemon can block enumeration unless that daemon (such as an OBEX,
263 * MTP, or print server) is ready to handle host requests.
265 * Not all systems support software control of their USB peripheral
268 * Returns zero on success, else negative errno.
270 int usb_function_deactivate(struct usb_function
*function
)
272 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
276 spin_lock_irqsave(&cdev
->lock
, flags
);
278 if (cdev
->deactivations
== 0)
279 status
= usb_gadget_disconnect(cdev
->gadget
);
281 cdev
->deactivations
++;
283 spin_unlock_irqrestore(&cdev
->lock
, flags
);
286 EXPORT_SYMBOL_GPL(usb_function_deactivate
);
289 * usb_function_activate - allow function and gadget enumeration
290 * @function: function on which usb_function_activate() was called
292 * Reverses effect of usb_function_deactivate(). If no more functions
293 * are delaying their activation, the gadget driver will respond to
294 * host enumeration procedures.
296 * Returns zero on success, else negative errno.
298 int usb_function_activate(struct usb_function
*function
)
300 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
304 spin_lock_irqsave(&cdev
->lock
, flags
);
306 if (WARN_ON(cdev
->deactivations
== 0))
309 cdev
->deactivations
--;
310 if (cdev
->deactivations
== 0)
311 status
= usb_gadget_connect(cdev
->gadget
);
314 spin_unlock_irqrestore(&cdev
->lock
, flags
);
317 EXPORT_SYMBOL_GPL(usb_function_activate
);
320 * usb_interface_id() - allocate an unused interface ID
321 * @config: configuration associated with the interface
322 * @function: function handling the interface
323 * Context: single threaded during gadget setup
325 * usb_interface_id() is called from usb_function.bind() callbacks to
326 * allocate new interface IDs. The function driver will then store that
327 * ID in interface, association, CDC union, and other descriptors. It
328 * will also handle any control requests targeted at that interface,
329 * particularly changing its altsetting via set_alt(). There may
330 * also be class-specific or vendor-specific requests to handle.
332 * All interface identifier should be allocated using this routine, to
333 * ensure that for example different functions don't wrongly assign
334 * different meanings to the same identifier. Note that since interface
335 * identifiers are configuration-specific, functions used in more than
336 * one configuration (or more than once in a given configuration) need
337 * multiple versions of the relevant descriptors.
339 * Returns the interface ID which was allocated; or -ENODEV if no
340 * more interface IDs can be allocated.
342 int usb_interface_id(struct usb_configuration
*config
,
343 struct usb_function
*function
)
345 unsigned id
= config
->next_interface_id
;
347 if (id
< MAX_CONFIG_INTERFACES
) {
348 config
->interface
[id
] = function
;
349 config
->next_interface_id
= id
+ 1;
354 EXPORT_SYMBOL_GPL(usb_interface_id
);
356 static u8
encode_bMaxPower(enum usb_device_speed speed
,
357 struct usb_configuration
*c
)
364 val
= CONFIG_USB_GADGET_VBUS_DRAW
;
368 case USB_SPEED_SUPER
:
369 return DIV_ROUND_UP(val
, 8);
371 return DIV_ROUND_UP(val
, 2);
375 static int config_buf(struct usb_configuration
*config
,
376 enum usb_device_speed speed
, void *buf
, u8 type
)
378 struct usb_config_descriptor
*c
= buf
;
379 void *next
= buf
+ USB_DT_CONFIG_SIZE
;
381 struct usb_function
*f
;
384 len
= USB_COMP_EP0_BUFSIZ
- USB_DT_CONFIG_SIZE
;
385 /* write the config descriptor */
387 c
->bLength
= USB_DT_CONFIG_SIZE
;
388 c
->bDescriptorType
= type
;
389 /* wTotalLength is written later */
390 c
->bNumInterfaces
= config
->next_interface_id
;
391 c
->bConfigurationValue
= config
->bConfigurationValue
;
392 c
->iConfiguration
= config
->iConfiguration
;
393 c
->bmAttributes
= USB_CONFIG_ATT_ONE
| config
->bmAttributes
;
394 c
->bMaxPower
= encode_bMaxPower(speed
, config
);
396 /* There may be e.g. OTG descriptors */
397 if (config
->descriptors
) {
398 status
= usb_descriptor_fillbuf(next
, len
,
399 config
->descriptors
);
406 /* add each function's descriptors */
407 list_for_each_entry(f
, &config
->functions
, list
) {
408 struct usb_descriptor_header
**descriptors
;
411 case USB_SPEED_SUPER
:
412 descriptors
= f
->ss_descriptors
;
415 descriptors
= f
->hs_descriptors
;
418 descriptors
= f
->fs_descriptors
;
423 status
= usb_descriptor_fillbuf(next
, len
,
424 (const struct usb_descriptor_header
**) descriptors
);
432 c
->wTotalLength
= cpu_to_le16(len
);
436 static int config_desc(struct usb_composite_dev
*cdev
, unsigned w_value
)
438 struct usb_gadget
*gadget
= cdev
->gadget
;
439 struct usb_configuration
*c
;
440 u8 type
= w_value
>> 8;
441 enum usb_device_speed speed
= USB_SPEED_UNKNOWN
;
443 if (gadget
->speed
== USB_SPEED_SUPER
)
444 speed
= gadget
->speed
;
445 else if (gadget_is_dualspeed(gadget
)) {
447 if (gadget
->speed
== USB_SPEED_HIGH
)
449 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
452 speed
= USB_SPEED_HIGH
;
456 /* This is a lookup by config *INDEX* */
458 list_for_each_entry(c
, &cdev
->configs
, list
) {
459 /* ignore configs that won't work at this speed */
461 case USB_SPEED_SUPER
:
475 return config_buf(c
, speed
, cdev
->req
->buf
, type
);
481 static int count_configs(struct usb_composite_dev
*cdev
, unsigned type
)
483 struct usb_gadget
*gadget
= cdev
->gadget
;
484 struct usb_configuration
*c
;
489 if (gadget_is_dualspeed(gadget
)) {
490 if (gadget
->speed
== USB_SPEED_HIGH
)
492 if (gadget
->speed
== USB_SPEED_SUPER
)
494 if (type
== USB_DT_DEVICE_QUALIFIER
)
497 list_for_each_entry(c
, &cdev
->configs
, list
) {
498 /* ignore configs that won't work at this speed */
515 * bos_desc() - prepares the BOS descriptor.
516 * @cdev: pointer to usb_composite device to generate the bos
519 * This function generates the BOS (Binary Device Object)
520 * descriptor and its device capabilities descriptors. The BOS
521 * descriptor should be supported by a SuperSpeed device.
523 static int bos_desc(struct usb_composite_dev
*cdev
)
525 struct usb_ext_cap_descriptor
*usb_ext
;
526 struct usb_ss_cap_descriptor
*ss_cap
;
527 struct usb_dcd_config_params dcd_config_params
;
528 struct usb_bos_descriptor
*bos
= cdev
->req
->buf
;
530 bos
->bLength
= USB_DT_BOS_SIZE
;
531 bos
->bDescriptorType
= USB_DT_BOS
;
533 bos
->wTotalLength
= cpu_to_le16(USB_DT_BOS_SIZE
);
534 bos
->bNumDeviceCaps
= 0;
537 * A SuperSpeed device shall include the USB2.0 extension descriptor
538 * and shall support LPM when operating in USB2.0 HS mode.
540 usb_ext
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
541 bos
->bNumDeviceCaps
++;
542 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_EXT_CAP_SIZE
);
543 usb_ext
->bLength
= USB_DT_USB_EXT_CAP_SIZE
;
544 usb_ext
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
545 usb_ext
->bDevCapabilityType
= USB_CAP_TYPE_EXT
;
546 #ifdef CONFIG_USBIF_COMPLIANCE
547 usb_ext
->bmAttributes
= cpu_to_le32(USB_LPM_SUPPORT
) | cpu_to_le32(USB_BESL_SUPPORT
) ;
549 usb_ext
->bmAttributes
= cpu_to_le32(USB_LPM_SUPPORT
);
553 * The Superspeed USB Capability descriptor shall be implemented by all
554 * SuperSpeed devices.
556 ss_cap
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
557 bos
->bNumDeviceCaps
++;
558 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_SS_CAP_SIZE
);
559 ss_cap
->bLength
= USB_DT_USB_SS_CAP_SIZE
;
560 ss_cap
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
561 ss_cap
->bDevCapabilityType
= USB_SS_CAP_TYPE
;
562 ss_cap
->bmAttributes
= 0; /* LTM is not supported yet */
563 ss_cap
->wSpeedSupported
= cpu_to_le16(USB_LOW_SPEED_OPERATION
|
564 USB_FULL_SPEED_OPERATION
|
565 USB_HIGH_SPEED_OPERATION
|
566 USB_5GBPS_OPERATION
);
567 ss_cap
->bFunctionalitySupport
= USB_LOW_SPEED_OPERATION
;
569 /* Get Controller configuration */
570 if (cdev
->gadget
->ops
->get_config_params
)
571 cdev
->gadget
->ops
->get_config_params(&dcd_config_params
);
573 dcd_config_params
.bU1devExitLat
= USB_DEFAULT_U1_DEV_EXIT_LAT
;
574 dcd_config_params
.bU2DevExitLat
=
575 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT
);
577 ss_cap
->bU1devExitLat
= dcd_config_params
.bU1devExitLat
;
578 ss_cap
->bU2DevExitLat
= dcd_config_params
.bU2DevExitLat
;
580 return le16_to_cpu(bos
->wTotalLength
);
583 static void device_qual(struct usb_composite_dev
*cdev
)
585 struct usb_qualifier_descriptor
*qual
= cdev
->req
->buf
;
587 qual
->bLength
= sizeof(*qual
);
588 qual
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
589 /* POLICY: same bcdUSB and device type info at both speeds */
590 qual
->bcdUSB
= cdev
->desc
.bcdUSB
;
591 qual
->bDeviceClass
= cdev
->desc
.bDeviceClass
;
592 qual
->bDeviceSubClass
= cdev
->desc
.bDeviceSubClass
;
593 qual
->bDeviceProtocol
= cdev
->desc
.bDeviceProtocol
;
594 /* ASSUME same EP0 fifo size at both speeds */
595 qual
->bMaxPacketSize0
= cdev
->gadget
->ep0
->maxpacket
;
596 qual
->bNumConfigurations
= count_configs(cdev
, USB_DT_DEVICE_QUALIFIER
);
600 /*-------------------------------------------------------------------------*/
602 static void reset_config(struct usb_composite_dev
*cdev
)
604 struct usb_function
*f
;
606 DBG(cdev
, "reset config\n");
608 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
609 INFO(cdev
, "disable function '%s'/%p\n", f
->name
, f
);
613 bitmap_zero(f
->endpoints
, 32);
616 cdev
->delayed_status
= 0;
619 static int set_config(struct usb_composite_dev
*cdev
,
620 const struct usb_ctrlrequest
*ctrl
, unsigned number
)
622 struct usb_gadget
*gadget
= cdev
->gadget
;
623 struct usb_configuration
*c
= NULL
;
624 int result
= -EINVAL
;
625 unsigned power
= gadget_is_otg(gadget
) ? 8 : 100;
629 list_for_each_entry(c
, &cdev
->configs
, list
) {
630 if (c
->bConfigurationValue
== number
) {
632 * We disable the FDs of the previous
633 * configuration only if the new configuration
644 } else { /* Zero configuration value - need to reset the config */
650 INFO(cdev
, "%s config #%d: %s\n",
651 usb_speed_string(gadget
->speed
),
652 number
, c
? c
->label
: "unconfigured");
659 /* Initialize all interfaces by setting them to altsetting zero. */
660 for (tmp
= 0; tmp
< MAX_CONFIG_INTERFACES
; tmp
++) {
661 struct usb_function
*f
= c
->interface
[tmp
];
662 struct usb_descriptor_header
**descriptors
;
668 * Record which endpoints are used by the function. This is used
669 * to dispatch control requests targeted at that endpoint to the
670 * function's setup callback instead of the current
671 * configuration's setup callback.
673 switch (gadget
->speed
) {
674 case USB_SPEED_SUPER
:
675 descriptors
= f
->ss_descriptors
;
678 descriptors
= f
->hs_descriptors
;
681 descriptors
= f
->fs_descriptors
;
684 for (; *descriptors
; ++descriptors
) {
685 struct usb_endpoint_descriptor
*ep
;
688 if ((*descriptors
)->bDescriptorType
!= USB_DT_ENDPOINT
)
691 ep
= (struct usb_endpoint_descriptor
*)*descriptors
;
692 addr
= ((ep
->bEndpointAddress
& 0x80) >> 3)
693 | (ep
->bEndpointAddress
& 0x0f);
694 set_bit(addr
, f
->endpoints
);
697 result
= f
->set_alt(f
, tmp
, 0);
699 DBG(cdev
, "interface %d (%s/%p) alt 0 --> %d\n",
700 tmp
, f
->name
, f
, result
);
706 if (result
== USB_GADGET_DELAYED_STATUS
) {
708 "%s: interface %d (%s) requested delayed status\n",
709 __func__
, tmp
, f
->name
);
710 cdev
->delayed_status
++;
711 DBG(cdev
, "delayed_status count %d\n",
712 cdev
->delayed_status
);
716 /* when we return, be sure our power usage is valid */
717 power
= c
->MaxPower
? c
->MaxPower
: CONFIG_USB_GADGET_VBUS_DRAW
;
719 usb_gadget_vbus_draw(gadget
, power
);
720 if (result
>= 0 && cdev
->delayed_status
)
721 result
= USB_GADGET_DELAYED_STATUS
;
725 int usb_add_config_only(struct usb_composite_dev
*cdev
,
726 struct usb_configuration
*config
)
728 struct usb_configuration
*c
;
730 if (!config
->bConfigurationValue
)
733 /* Prevent duplicate configuration identifiers */
734 list_for_each_entry(c
, &cdev
->configs
, list
) {
735 if (c
->bConfigurationValue
== config
->bConfigurationValue
)
740 list_add_tail(&config
->list
, &cdev
->configs
);
742 INIT_LIST_HEAD(&config
->functions
);
743 config
->next_interface_id
= 0;
744 memset(config
->interface
, 0, sizeof(config
->interface
));
748 EXPORT_SYMBOL_GPL(usb_add_config_only
);
751 * usb_add_config() - add a configuration to a device.
752 * @cdev: wraps the USB gadget
753 * @config: the configuration, with bConfigurationValue assigned
754 * @bind: the configuration's bind function
755 * Context: single threaded during gadget setup
757 * One of the main tasks of a composite @bind() routine is to
758 * add each of the configurations it supports, using this routine.
760 * This function returns the value of the configuration's @bind(), which
761 * is zero for success else a negative errno value. Binding configurations
762 * assigns global resources including string IDs, and per-configuration
763 * resources such as interface IDs and endpoints.
765 int usb_add_config(struct usb_composite_dev
*cdev
,
766 struct usb_configuration
*config
,
767 int (*bind
)(struct usb_configuration
*))
769 int status
= -EINVAL
;
774 DBG(cdev
, "adding config #%u '%s'/%p\n",
775 config
->bConfigurationValue
,
776 config
->label
, config
);
778 status
= usb_add_config_only(cdev
, config
);
782 status
= bind(config
);
784 while (!list_empty(&config
->functions
)) {
785 struct usb_function
*f
;
787 f
= list_first_entry(&config
->functions
,
788 struct usb_function
, list
);
791 INFO(cdev
, "unbind function '%s'/%p\n",
793 f
->unbind(config
, f
);
794 /* may free memory for "f" */
797 list_del(&config
->list
);
798 pr_debug("[XLOG_DEBUG][USB][COM]bind fialed and the list should be init because there is one entry only");
803 INFO(cdev
, "cfg %d/%p speeds:%s%s%s\n",
804 config
->bConfigurationValue
, config
,
805 config
->superspeed
? " super" : "",
806 config
->highspeed
? " high" : "",
808 ? (gadget_is_dualspeed(cdev
->gadget
)
813 for (i
= 0; i
< MAX_CONFIG_INTERFACES
; i
++) {
814 struct usb_function
*f
= config
->interface
[i
];
818 DBG(cdev
, " interface %d = %s/%p\n",
823 /* set_alt(), or next bind(), sets up
824 * ep->driver_data as needed.
826 usb_ep_autoconfig_reset(cdev
->gadget
);
830 DBG(cdev
, "added config '%s'/%u --> %d\n", config
->label
,
831 config
->bConfigurationValue
, status
);
834 EXPORT_SYMBOL_GPL(usb_add_config
);
836 static void unbind_config(struct usb_composite_dev
*cdev
,
837 struct usb_configuration
*config
)
839 while (!list_empty(&config
->functions
)) {
840 struct usb_function
*f
;
842 f
= list_first_entry(&config
->functions
,
843 struct usb_function
, list
);
846 INFO(cdev
, "unbind function '%s'/%p\n", f
->name
, f
);
847 f
->unbind(config
, f
);
848 /* may free memory for "f" */
851 if (config
->unbind
) {
852 INFO(cdev
, "unbind config '%s'/%p\n", config
->label
, config
);
853 config
->unbind(config
);
854 /* may free memory for "c" */
857 /* reset all driver data to prevent leakage of ep allocation */
858 usb_ep_autoconfig_reset(cdev
->gadget
);
862 * usb_remove_config() - remove a configuration from a device.
863 * @cdev: wraps the USB gadget
864 * @config: the configuration
866 * Drivers must call usb_gadget_disconnect before calling this function
867 * to disconnect the device from the host and make sure the host will not
868 * try to enumerate the device while we are changing the config list.
870 void usb_remove_config(struct usb_composite_dev
*cdev
,
871 struct usb_configuration
*config
)
875 spin_lock_irqsave(&cdev
->lock
, flags
);
877 if (cdev
->config
== config
)
881 if(config
->cdev
!= NULL
)
883 list_del(&config
->list
);
886 DBG(cdev
, "%s: config->list has been delete!! \n", __func__
);
889 spin_unlock_irqrestore(&cdev
->lock
, flags
);
891 unbind_config(cdev
, config
);
894 /*-------------------------------------------------------------------------*/
896 /* We support strings in multiple languages ... string descriptor zero
897 * says which languages are supported. The typical case will be that
898 * only one language (probably English) is used, with I18N handled on
902 static void collect_langs(struct usb_gadget_strings
**sp
, __le16
*buf
)
904 const struct usb_gadget_strings
*s
;
910 language
= cpu_to_le16(s
->language
);
911 for (tmp
= buf
; *tmp
&& tmp
< &buf
[126]; tmp
++) {
912 if (*tmp
== language
)
921 static int lookup_string(
922 struct usb_gadget_strings
**sp
,
928 struct usb_gadget_strings
*s
;
933 if (s
->language
!= language
)
935 value
= usb_gadget_get_string(s
, id
, buf
);
942 static int get_string(struct usb_composite_dev
*cdev
,
943 void *buf
, u16 language
, int id
)
945 struct usb_composite_driver
*composite
= cdev
->driver
;
946 struct usb_gadget_string_container
*uc
;
947 struct usb_configuration
*c
;
948 struct usb_function
*f
;
951 /* Yes, not only is USB's I18N support probably more than most
952 * folk will ever care about ... also, it's all supported here.
953 * (Except for UTF8 support for Unicode's "Astral Planes".)
956 /* 0 == report all available language codes */
958 struct usb_string_descriptor
*s
= buf
;
959 struct usb_gadget_strings
**sp
;
962 s
->bDescriptorType
= USB_DT_STRING
;
964 sp
= composite
->strings
;
966 collect_langs(sp
, s
->wData
);
968 list_for_each_entry(c
, &cdev
->configs
, list
) {
971 collect_langs(sp
, s
->wData
);
973 list_for_each_entry(f
, &c
->functions
, list
) {
976 collect_langs(sp
, s
->wData
);
979 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
980 struct usb_gadget_strings
**sp
;
982 sp
= get_containers_gs(uc
);
983 collect_langs(sp
, s
->wData
);
986 for (len
= 0; len
<= 126 && s
->wData
[len
]; len
++)
991 s
->bLength
= 2 * (len
+ 1);
995 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
996 struct usb_gadget_strings
**sp
;
998 sp
= get_containers_gs(uc
);
999 len
= lookup_string(sp
, buf
, language
, id
);
1004 /* String IDs are device-scoped, so we look up each string
1005 * table we're told about. These lookups are infrequent;
1006 * simpler-is-better here.
1008 if (composite
->strings
) {
1009 len
= lookup_string(composite
->strings
, buf
, language
, id
);
1013 list_for_each_entry(c
, &cdev
->configs
, list
) {
1015 len
= lookup_string(c
->strings
, buf
, language
, id
);
1019 list_for_each_entry(f
, &c
->functions
, list
) {
1022 len
= lookup_string(f
->strings
, buf
, language
, id
);
1031 * usb_string_id() - allocate an unused string ID
1032 * @cdev: the device whose string descriptor IDs are being allocated
1033 * Context: single threaded during gadget setup
1035 * @usb_string_id() is called from bind() callbacks to allocate
1036 * string IDs. Drivers for functions, configurations, or gadgets will
1037 * then store that ID in the appropriate descriptors and string table.
1039 * All string identifier should be allocated using this,
1040 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1041 * that for example different functions don't wrongly assign different
1042 * meanings to the same identifier.
1044 int usb_string_id(struct usb_composite_dev
*cdev
)
1046 if (cdev
->next_string_id
< 254) {
1047 /* string id 0 is reserved by USB spec for list of
1048 * supported languages */
1049 /* 255 reserved as well? -- mina86 */
1050 cdev
->next_string_id
++;
1051 return cdev
->next_string_id
;
1055 EXPORT_SYMBOL_GPL(usb_string_id
);
1058 * usb_string_ids() - allocate unused string IDs in batch
1059 * @cdev: the device whose string descriptor IDs are being allocated
1060 * @str: an array of usb_string objects to assign numbers to
1061 * Context: single threaded during gadget setup
1063 * @usb_string_ids() is called from bind() callbacks to allocate
1064 * string IDs. Drivers for functions, configurations, or gadgets will
1065 * then copy IDs from the string table to the appropriate descriptors
1066 * and string table for other languages.
1068 * All string identifier should be allocated using this,
1069 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1070 * example different functions don't wrongly assign different meanings
1071 * to the same identifier.
1073 int usb_string_ids_tab(struct usb_composite_dev
*cdev
, struct usb_string
*str
)
1075 int next
= cdev
->next_string_id
;
1077 for (; str
->s
; ++str
) {
1078 if (unlikely(next
>= 254))
1083 cdev
->next_string_id
= next
;
1087 EXPORT_SYMBOL_GPL(usb_string_ids_tab
);
1089 static struct usb_gadget_string_container
*copy_gadget_strings(
1090 struct usb_gadget_strings
**sp
, unsigned n_gstrings
,
1093 struct usb_gadget_string_container
*uc
;
1094 struct usb_gadget_strings
**gs_array
;
1095 struct usb_gadget_strings
*gs
;
1096 struct usb_string
*s
;
1103 mem
+= sizeof(void *) * (n_gstrings
+ 1);
1104 mem
+= sizeof(struct usb_gadget_strings
) * n_gstrings
;
1105 mem
+= sizeof(struct usb_string
) * (n_strings
+ 1) * (n_gstrings
);
1106 uc
= kmalloc(mem
, GFP_KERNEL
);
1108 return ERR_PTR(-ENOMEM
);
1109 gs_array
= get_containers_gs(uc
);
1111 stash
+= sizeof(void *) * (n_gstrings
+ 1);
1112 for (n_gs
= 0; n_gs
< n_gstrings
; n_gs
++) {
1113 struct usb_string
*org_s
;
1115 gs_array
[n_gs
] = stash
;
1116 gs
= gs_array
[n_gs
];
1117 stash
+= sizeof(struct usb_gadget_strings
);
1118 gs
->language
= sp
[n_gs
]->language
;
1119 gs
->strings
= stash
;
1120 org_s
= sp
[n_gs
]->strings
;
1122 for (n_s
= 0; n_s
< n_strings
; n_s
++) {
1124 stash
+= sizeof(struct usb_string
);
1133 stash
+= sizeof(struct usb_string
);
1136 gs_array
[n_gs
] = NULL
;
1141 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1142 * @cdev: the device whose string descriptor IDs are being allocated
1144 * @sp: an array of usb_gadget_strings to attach.
1145 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1147 * This function will create a deep copy of usb_gadget_strings and usb_string
1148 * and attach it to the cdev. The actual string (usb_string.s) will not be
1149 * copied but only a referenced will be made. The struct usb_gadget_strings
1150 * array may contain multiple languges and should be NULL terminated.
1151 * The ->language pointer of each struct usb_gadget_strings has to contain the
1152 * same amount of entries.
1153 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1154 * usb_string entry of es-ES containts the translation of the first usb_string
1155 * entry of en-US. Therefore both entries become the same id assign.
1157 struct usb_string
*usb_gstrings_attach(struct usb_composite_dev
*cdev
,
1158 struct usb_gadget_strings
**sp
, unsigned n_strings
)
1160 struct usb_gadget_string_container
*uc
;
1161 struct usb_gadget_strings
**n_gs
;
1162 unsigned n_gstrings
= 0;
1166 for (i
= 0; sp
[i
]; i
++)
1170 return ERR_PTR(-EINVAL
);
1172 uc
= copy_gadget_strings(sp
, n_gstrings
, n_strings
);
1174 return ERR_PTR(PTR_ERR(uc
));
1176 n_gs
= get_containers_gs(uc
);
1177 ret
= usb_string_ids_tab(cdev
, n_gs
[0]->strings
);
1181 for (i
= 1; i
< n_gstrings
; i
++) {
1182 struct usb_string
*m_s
;
1183 struct usb_string
*s
;
1186 m_s
= n_gs
[0]->strings
;
1187 s
= n_gs
[i
]->strings
;
1188 for (n
= 0; n
< n_strings
; n
++) {
1194 list_add_tail(&uc
->list
, &cdev
->gstrings
);
1195 return n_gs
[0]->strings
;
1198 return ERR_PTR(ret
);
1200 EXPORT_SYMBOL_GPL(usb_gstrings_attach
);
1203 * usb_string_ids_n() - allocate unused string IDs in batch
1204 * @c: the device whose string descriptor IDs are being allocated
1205 * @n: number of string IDs to allocate
1206 * Context: single threaded during gadget setup
1208 * Returns the first requested ID. This ID and next @n-1 IDs are now
1209 * valid IDs. At least provided that @n is non-zero because if it
1210 * is, returns last requested ID which is now very useful information.
1212 * @usb_string_ids_n() is called from bind() callbacks to allocate
1213 * string IDs. Drivers for functions, configurations, or gadgets will
1214 * then store that ID in the appropriate descriptors and string table.
1216 * All string identifier should be allocated using this,
1217 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1218 * example different functions don't wrongly assign different meanings
1219 * to the same identifier.
1221 int usb_string_ids_n(struct usb_composite_dev
*c
, unsigned n
)
1223 unsigned next
= c
->next_string_id
;
1224 if (unlikely(n
> 254 || (unsigned)next
+ n
> 254))
1226 c
->next_string_id
+= n
;
1229 EXPORT_SYMBOL_GPL(usb_string_ids_n
);
1231 /*-------------------------------------------------------------------------*/
1233 void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1235 if (req
->status
|| req
->actual
!= req
->length
)
1236 DBG((struct usb_composite_dev
*) ep
->driver_data
,
1237 "setup complete --> %d, %d/%d\n",
1238 req
->status
, req
->actual
, req
->length
);
1241 EXPORT_SYMBOL_GPL(composite_setup_complete
);
1244 * The setup() callback implements all the ep0 functionality that's
1245 * not handled lower down, in hardware or the hardware driver(like
1246 * device and endpoint feature flags, and their status). It's all
1247 * housekeeping for the gadget function we're implementing. Most of
1248 * the work is in config and function specific setup.
1251 composite_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1253 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1254 struct usb_request
*req
= cdev
->req
;
1255 int value
= -EOPNOTSUPP
;
1257 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1258 u8 intf
= w_index
& 0xFF;
1259 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1260 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1261 struct usb_function
*f
= NULL
;
1264 pr_debug("[XLOG_DEBUG][USB][COM]%s bRequest=0x%X\n",
1265 __func__
, ctrl
->bRequest
);
1267 /* partial re-init of the response message; the function or the
1268 * gadget might need to intercept e.g. a control-OUT completion
1269 * when we delegate to it.
1272 req
->complete
= composite_setup_complete
;
1274 gadget
->ep0
->driver_data
= cdev
;
1276 switch (ctrl
->bRequest
) {
1278 /* we handle all standard USB descriptors */
1279 case USB_REQ_GET_DESCRIPTOR
:
1280 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1282 switch (w_value
>> 8) {
1283 #ifdef CONFIG_USBIF_COMPLIANCE
1286 struct usb_otg_descriptor
*otg_desc
= req
->buf
;
1287 otg_desc
->bLength
= sizeof(*otg_desc
);
1288 otg_desc
->bDescriptorType
= USB_DT_OTG
;
1289 otg_desc
->bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
;
1290 otg_desc
->bcdOTG
= cpu_to_le16(0x0200);
1291 value
= min_t(int, w_length
,sizeof(struct usb_otg_descriptor
));
1296 cdev
->desc
.bNumConfigurations
=
1297 count_configs(cdev
, USB_DT_DEVICE
);
1298 cdev
->desc
.bMaxPacketSize0
=
1299 cdev
->gadget
->ep0
->maxpacket
;
1300 if (gadget_is_superspeed(gadget
)) {
1301 if (gadget
->speed
>= USB_SPEED_SUPER
) {
1302 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0300);
1303 cdev
->desc
.bMaxPacketSize0
= 9;
1305 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0210);
1309 value
= min(w_length
, (u16
) sizeof cdev
->desc
);
1310 memcpy(req
->buf
, &cdev
->desc
, value
);
1311 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1312 "USB_DT_DEVICE, value=%d\n",value
);
1314 case USB_DT_DEVICE_QUALIFIER
:
1315 if (!gadget_is_dualspeed(gadget
) ||
1316 gadget
->speed
>= USB_SPEED_SUPER
)
1319 value
= min_t(int, w_length
,
1320 sizeof(struct usb_qualifier_descriptor
));
1321 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1322 "USB_DT_DEVICE_QUALIFIER, value=%d\n",value
);
1324 case USB_DT_OTHER_SPEED_CONFIG
:
1325 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1326 "USB_DT_OTHER_SPEED_CONFIG\n");
1327 if (!gadget_is_dualspeed(gadget
) ||
1328 gadget
->speed
>= USB_SPEED_SUPER
)
1332 value
= config_desc(cdev
, w_value
);
1334 value
= min(w_length
, (u16
) value
);
1335 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1336 "USB_DT_CONFIG, value=%d\n",value
);
1339 value
= get_string(cdev
, req
->buf
,
1340 w_index
, w_value
& 0xff);
1342 value
= min(w_length
, (u16
) value
);
1343 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1344 "USB_DT_STRING, value=%d\n" ,value
);
1348 if (gadget_is_superspeed(gadget
)) {
1349 value
= bos_desc(cdev
);
1350 value
= min(w_length
, (u16
) value
);
1352 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1353 "USB_DT_BOS, value=%d\n",value
);
1356 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR w_value=0x%X\n", w_value
);
1361 /* any number of configs can work */
1362 case USB_REQ_SET_CONFIGURATION
:
1363 if (ctrl
->bRequestType
!= 0)
1365 if (gadget_is_otg(gadget
)) {
1366 if (gadget
->a_hnp_support
)
1367 DBG(cdev
, "HNP available\n");
1368 else if (gadget
->a_alt_hnp_support
)
1369 DBG(cdev
, "HNP on another port\n");
1371 VDBG(cdev
, "HNP inactive\n");
1373 spin_lock(&cdev
->lock
);
1374 value
= set_config(cdev
, ctrl
, w_value
);
1375 spin_unlock(&cdev
->lock
);
1376 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_SET_CONFIGURATION: "
1377 "value=%d\n",value
);
1379 case USB_REQ_GET_CONFIGURATION
:
1380 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1383 *(u8
*)req
->buf
= cdev
->config
->bConfigurationValue
;
1385 *(u8
*)req
->buf
= 0;
1386 value
= min(w_length
, (u16
) 1);
1389 /* function drivers must handle get/set altsetting; if there's
1390 * no get() method, we know only altsetting zero works.
1392 case USB_REQ_SET_INTERFACE
:
1393 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
1395 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1399 f
= cdev
->config
->interface
[intf
];
1401 pr_debug("%s: cdev->config = NULL \n", __func__
);
1405 if (w_value
&& !f
->set_alt
)
1407 value
= f
->set_alt(f
, w_index
, w_value
);
1408 if (value
== USB_GADGET_DELAYED_STATUS
) {
1410 "%s: interface %d (%s) requested delayed status\n",
1411 __func__
, intf
, f
->name
);
1412 cdev
->delayed_status
++;
1413 DBG(cdev
, "delayed_status count %d\n",
1414 cdev
->delayed_status
);
1417 case USB_REQ_GET_INTERFACE
:
1418 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1420 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1422 f
= cdev
->config
->interface
[intf
];
1425 /* lots of interfaces only need altsetting zero... */
1426 value
= f
->get_alt
? f
->get_alt(f
, w_index
) : 0;
1429 *((u8
*)req
->buf
) = value
;
1430 value
= min(w_length
, (u16
) 1);
1434 * USB 3.0 additions:
1435 * Function driver should handle get_status request. If such cb
1436 * wasn't supplied we respond with default value = 0
1437 * Note: function driver should supply such cb only for the first
1438 * interface of the function
1440 case USB_REQ_GET_STATUS
:
1441 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_STATUS\n");
1442 if (!gadget_is_superspeed(gadget
))
1444 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_RECIP_INTERFACE
))
1446 value
= 2; /* This is the length of the get_status reply */
1447 put_unaligned_le16(0, req
->buf
);
1448 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1450 f
= cdev
->config
->interface
[intf
];
1453 status
= f
->get_status
? f
->get_status(f
) : 0;
1456 put_unaligned_le16(status
& 0x0000ffff, req
->buf
);
1459 * Function drivers should handle SetFeature/ClearFeature
1460 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1461 * only for the first interface of the function
1463 case USB_REQ_CLEAR_FEATURE
:
1464 case USB_REQ_SET_FEATURE
:
1465 pr_debug("[XLOG_DEBUG][USB][COM]%s w_value=%d\n",
1466 ((ctrl
->bRequest
==USB_REQ_SET_FEATURE
)? "USB_REQ_SET_FEATURE" : "USB_REQ_CLEAR_FEATURE"), w_value
);
1467 if (!gadget_is_superspeed(gadget
))
1469 if (ctrl
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_INTERFACE
))
1472 case USB_INTRF_FUNC_SUSPEND
:
1473 pr_debug("[COM]USB_INTRF_FUNC_SUSPEND\n");
1474 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1476 f
= cdev
->config
->interface
[intf
];
1480 if (f
->func_suspend
)
1481 value
= f
->func_suspend(f
, w_index
>> 8);
1484 "func_suspend() returned error %d\n",
1491 case USB_REQ_SET_SEL
:
1492 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_SET_SEL Pretend success\n");
1498 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1499 ctrl
->bRequestType
, ctrl
->bRequest
,
1500 w_value
, w_index
, w_length
);
1502 /* functions always handle their interfaces and endpoints...
1503 * punt other recipients (other, WUSB, ...) to the current
1504 * configuration code.
1506 * REVISIT it could make sense to let the composite device
1507 * take such requests too, if that's ever needed: to work
1510 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1511 case USB_RECIP_INTERFACE
:
1512 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1514 f
= cdev
->config
->interface
[intf
];
1517 case USB_RECIP_ENDPOINT
:
1518 endp
= ((w_index
& 0x80) >> 3) | (w_index
& 0x0f);
1519 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1520 if (test_bit(endp
, f
->endpoints
))
1523 if (&f
->list
== &cdev
->config
->functions
)
1529 value
= f
->setup(f
, ctrl
);
1531 struct usb_configuration
*c
;
1535 value
= c
->setup(c
, ctrl
);
1541 /* respond with data transfer before status phase? */
1542 if (value
>= 0 && value
!= USB_GADGET_DELAYED_STATUS
) {
1543 req
->length
= value
;
1544 req
->zero
= value
< w_length
;
1545 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
1547 DBG(cdev
, "ep_queue --> %d\n", value
);
1549 composite_setup_complete(gadget
->ep0
, req
);
1551 } else if (value
== USB_GADGET_DELAYED_STATUS
&& w_length
!= 0) {
1553 "%s: Delayed status not supported for w_length != 0",
1559 pr_debug("[XLOG_DEBUG][USB][COM]composite_setup: value=%d,"
1560 "bRequestType=0x%x, bRequest=0x%x, w_value=0x%x, w_length=0x%x \n", value
,
1561 ctrl
->bRequestType
, ctrl
->bRequest
, w_value
, w_length
);
1563 /* device either stalls (value < 0) or reports success */
1567 void composite_disconnect(struct usb_gadget
*gadget
)
1569 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1570 unsigned long flags
;
1572 /* REVISIT: should we have config and device level
1573 * disconnect callbacks?
1575 spin_lock_irqsave(&cdev
->lock
, flags
);
1578 if (cdev
->driver
->disconnect
)
1579 cdev
->driver
->disconnect(cdev
);
1581 /* ALPS00235316 and ALPS00234976 */
1582 /* reset the complet function */
1583 if(cdev
->req
->complete
) {
1584 pr_debug("[XLOG_DEBUG][USB][COM]%s: reassign the complete function!!\n", __func__
);
1585 cdev
->req
->complete
= composite_setup_complete
;
1588 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1591 /*-------------------------------------------------------------------------*/
1593 static ssize_t
composite_show_suspended(struct device
*dev
,
1594 struct device_attribute
*attr
,
1597 struct usb_gadget
*gadget
= dev_to_usb_gadget(dev
);
1598 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1600 return sprintf(buf
, "%d\n", cdev
->suspended
);
1603 static DEVICE_ATTR(suspended
, 0444, composite_show_suspended
, NULL
);
1605 static void __composite_unbind(struct usb_gadget
*gadget
, bool unbind_driver
)
1607 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1609 /* composite_disconnect() must already have been called
1610 * by the underlying peripheral controller driver!
1611 * so there's no i/o concurrency that could affect the
1612 * state protected by cdev->lock.
1614 WARN_ON(cdev
->config
);
1616 while (!list_empty(&cdev
->configs
)) {
1617 struct usb_configuration
*c
;
1618 c
= list_first_entry(&cdev
->configs
,
1619 struct usb_configuration
, list
);
1621 unbind_config(cdev
, c
);
1623 if (cdev
->driver
->unbind
&& unbind_driver
)
1624 cdev
->driver
->unbind(cdev
);
1626 composite_dev_cleanup(cdev
);
1628 kfree(cdev
->def_manufacturer
);
1630 set_gadget_data(gadget
, NULL
);
1633 static void composite_unbind(struct usb_gadget
*gadget
)
1635 __composite_unbind(gadget
, true);
1638 static void update_unchanged_dev_desc(struct usb_device_descriptor
*new,
1639 const struct usb_device_descriptor
*old
)
1649 * these variables may have been set in
1650 * usb_composite_overwrite_options()
1652 idVendor
= new->idVendor
;
1653 idProduct
= new->idProduct
;
1654 bcdDevice
= new->bcdDevice
;
1655 iSerialNumber
= new->iSerialNumber
;
1656 iManufacturer
= new->iManufacturer
;
1657 iProduct
= new->iProduct
;
1661 new->idVendor
= idVendor
;
1663 new->idProduct
= idProduct
;
1665 new->bcdDevice
= bcdDevice
;
1667 new->bcdDevice
= cpu_to_le16(get_default_bcdDevice());
1669 new->iSerialNumber
= iSerialNumber
;
1671 new->iManufacturer
= iManufacturer
;
1673 new->iProduct
= iProduct
;
1676 int composite_dev_prepare(struct usb_composite_driver
*composite
,
1677 struct usb_composite_dev
*cdev
)
1679 struct usb_gadget
*gadget
= cdev
->gadget
;
1682 /* preallocate control response and buffer */
1683 cdev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
1687 cdev
->req
->buf
= kmalloc(USB_COMP_EP0_BUFSIZ
, GFP_KERNEL
);
1688 if (!cdev
->req
->buf
)
1691 ret
= device_create_file(&gadget
->dev
, &dev_attr_suspended
);
1695 cdev
->req
->complete
= composite_setup_complete
;
1696 gadget
->ep0
->driver_data
= cdev
;
1698 cdev
->driver
= composite
;
1701 * As per USB compliance update, a device that is actively drawing
1702 * more than 100mA from USB must report itself as bus-powered in
1703 * the GetStatus(DEVICE) call.
1705 if (CONFIG_USB_GADGET_VBUS_DRAW
<= USB_SELF_POWER_VBUS_MAX_DRAW
)
1706 usb_gadget_set_selfpowered(gadget
);
1708 /* interface and string IDs start at zero via kzalloc.
1709 * we force endpoints to start unassigned; few controller
1710 * drivers will zero ep->driver_data.
1712 usb_ep_autoconfig_reset(gadget
);
1715 kfree(cdev
->req
->buf
);
1717 usb_ep_free_request(gadget
->ep0
, cdev
->req
);
1722 void composite_dev_cleanup(struct usb_composite_dev
*cdev
)
1724 struct usb_gadget_string_container
*uc
, *tmp
;
1726 list_for_each_entry_safe(uc
, tmp
, &cdev
->gstrings
, list
) {
1727 list_del(&uc
->list
);
1731 kfree(cdev
->req
->buf
);
1732 usb_ep_free_request(cdev
->gadget
->ep0
, cdev
->req
);
1734 cdev
->next_string_id
= 0;
1735 device_remove_file(&cdev
->gadget
->dev
, &dev_attr_suspended
);
1738 static int composite_bind(struct usb_gadget
*gadget
,
1739 struct usb_gadget_driver
*gdriver
)
1741 struct usb_composite_dev
*cdev
;
1742 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
1743 int status
= -ENOMEM
;
1745 cdev
= kzalloc(sizeof *cdev
, GFP_KERNEL
);
1749 spin_lock_init(&cdev
->lock
);
1750 cdev
->gadget
= gadget
;
1751 set_gadget_data(gadget
, cdev
);
1752 INIT_LIST_HEAD(&cdev
->configs
);
1753 INIT_LIST_HEAD(&cdev
->gstrings
);
1755 status
= composite_dev_prepare(composite
, cdev
);
1759 /* composite gadget needs to assign strings for whole device (like
1760 * serial number), register function drivers, potentially update
1761 * power state and consumption, etc
1763 status
= composite
->bind(cdev
);
1767 update_unchanged_dev_desc(&cdev
->desc
, composite
->dev
);
1769 /* has userspace failed to provide a serial number? */
1770 if (composite
->needs_serial
&& !cdev
->desc
.iSerialNumber
)
1771 WARNING(cdev
, "userspace failed to provide iSerialNumber\n");
1773 INFO(cdev
, "%s ready\n", composite
->name
);
1777 __composite_unbind(gadget
, false);
1781 /*-------------------------------------------------------------------------*/
1784 composite_suspend(struct usb_gadget
*gadget
)
1786 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1787 struct usb_function
*f
;
1789 /* REVISIT: should we have config level
1790 * suspend/resume callbacks?
1792 DBG(cdev
, "suspend\n");
1794 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1799 if (cdev
->driver
->suspend
)
1800 cdev
->driver
->suspend(cdev
);
1802 cdev
->suspended
= 1;
1804 usb_gadget_vbus_draw(gadget
, 2);
1808 composite_resume(struct usb_gadget
*gadget
)
1810 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1811 struct usb_function
*f
;
1814 /* REVISIT: should we have config level
1815 * suspend/resume callbacks?
1817 DBG(cdev
, "resume\n");
1818 if (cdev
->driver
->resume
)
1819 cdev
->driver
->resume(cdev
);
1821 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1826 maxpower
= cdev
->config
->MaxPower
;
1828 usb_gadget_vbus_draw(gadget
, maxpower
?
1829 maxpower
: CONFIG_USB_GADGET_VBUS_DRAW
);
1832 cdev
->suspended
= 0;
1835 /*-------------------------------------------------------------------------*/
1837 static const struct usb_gadget_driver composite_driver_template
= {
1838 .bind
= composite_bind
,
1839 .unbind
= composite_unbind
,
1841 .setup
= composite_setup
,
1842 .disconnect
= composite_disconnect
,
1844 .suspend
= composite_suspend
,
1845 .resume
= composite_resume
,
1848 .owner
= THIS_MODULE
,
1853 * usb_composite_probe() - register a composite driver
1854 * @driver: the driver to register
1856 * Context: single threaded during gadget setup
1858 * This function is used to register drivers using the composite driver
1859 * framework. The return value is zero, or a negative errno value.
1860 * Those values normally come from the driver's @bind method, which does
1861 * all the work of setting up the driver to match the hardware.
1863 * On successful return, the gadget is ready to respond to requests from
1864 * the host, unless one of its components invokes usb_gadget_disconnect()
1865 * while it was binding. That would usually be done in order to wait for
1866 * some userspace participation.
1868 int usb_composite_probe(struct usb_composite_driver
*driver
)
1870 struct usb_gadget_driver
*gadget_driver
;
1872 if (!driver
|| !driver
->dev
|| !driver
->bind
)
1875 pr_debug("[XLOG_DEBUG][USB][COM]%s: driver->name = %s", __func__
, driver
->name
);
1878 driver
->name
= "composite";
1880 driver
->gadget_driver
= composite_driver_template
;
1881 gadget_driver
= &driver
->gadget_driver
;
1883 gadget_driver
->function
= (char *) driver
->name
;
1884 gadget_driver
->driver
.name
= driver
->name
;
1885 gadget_driver
->max_speed
= driver
->max_speed
;
1887 return usb_gadget_probe_driver(gadget_driver
);
1889 EXPORT_SYMBOL_GPL(usb_composite_probe
);
1892 * usb_composite_unregister() - unregister a composite driver
1893 * @driver: the driver to unregister
1895 * This function is used to unregister drivers using the composite
1898 void usb_composite_unregister(struct usb_composite_driver
*driver
)
1900 usb_gadget_unregister_driver(&driver
->gadget_driver
);
1902 EXPORT_SYMBOL_GPL(usb_composite_unregister
);
1905 * usb_composite_setup_continue() - Continue with the control transfer
1906 * @cdev: the composite device who's control transfer was kept waiting
1908 * This function must be called by the USB function driver to continue
1909 * with the control transfer's data/status stage in case it had requested to
1910 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1911 * can request the composite framework to delay the setup request's data/status
1912 * stages by returning USB_GADGET_DELAYED_STATUS.
1914 void usb_composite_setup_continue(struct usb_composite_dev
*cdev
)
1917 struct usb_request
*req
= cdev
->req
;
1918 unsigned long flags
;
1920 DBG(cdev
, "%s\n", __func__
);
1921 spin_lock_irqsave(&cdev
->lock
, flags
);
1923 if (cdev
->delayed_status
== 0) {
1924 WARN(cdev
, "%s: Unexpected call\n", __func__
);
1926 } else if (--cdev
->delayed_status
== 0) {
1927 DBG(cdev
, "%s: Completing delayed status\n", __func__
);
1929 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
1931 DBG(cdev
, "ep_queue --> %d\n", value
);
1933 composite_setup_complete(cdev
->gadget
->ep0
, req
);
1937 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1939 EXPORT_SYMBOL_GPL(usb_composite_setup_continue
);
1941 static char *composite_default_mfr(struct usb_gadget
*gadget
)
1946 len
= snprintf(NULL
, 0, "%s %s with %s", init_utsname()->sysname
,
1947 init_utsname()->release
, gadget
->name
);
1949 mfr
= kmalloc(len
, GFP_KERNEL
);
1952 snprintf(mfr
, len
, "%s %s with %s", init_utsname()->sysname
,
1953 init_utsname()->release
, gadget
->name
);
1957 void usb_composite_overwrite_options(struct usb_composite_dev
*cdev
,
1958 struct usb_composite_overwrite
*covr
)
1960 struct usb_device_descriptor
*desc
= &cdev
->desc
;
1961 struct usb_gadget_strings
*gstr
= cdev
->driver
->strings
[0];
1962 struct usb_string
*dev_str
= gstr
->strings
;
1965 desc
->idVendor
= cpu_to_le16(covr
->idVendor
);
1967 if (covr
->idProduct
)
1968 desc
->idProduct
= cpu_to_le16(covr
->idProduct
);
1970 if (covr
->bcdDevice
)
1971 desc
->bcdDevice
= cpu_to_le16(covr
->bcdDevice
);
1973 if (covr
->serial_number
) {
1974 desc
->iSerialNumber
= dev_str
[USB_GADGET_SERIAL_IDX
].id
;
1975 dev_str
[USB_GADGET_SERIAL_IDX
].s
= covr
->serial_number
;
1977 if (covr
->manufacturer
) {
1978 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
1979 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= covr
->manufacturer
;
1981 } else if (!strlen(dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
)) {
1982 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
1983 cdev
->def_manufacturer
= composite_default_mfr(cdev
->gadget
);
1984 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= cdev
->def_manufacturer
;
1987 if (covr
->product
) {
1988 desc
->iProduct
= dev_str
[USB_GADGET_PRODUCT_IDX
].id
;
1989 dev_str
[USB_GADGET_PRODUCT_IDX
].s
= covr
->product
;
1992 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options
);
1994 MODULE_LICENSE("GPL");
1995 MODULE_AUTHOR("David Brownell");