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
) & 0x7ff;
138 _ep
->desc
= chosen_desc
;
139 _ep
->comp_desc
= NULL
;
143 if (g
->speed
== USB_SPEED_HIGH
&& (usb_endpoint_xfer_isoc(_ep
->desc
) ||
144 usb_endpoint_xfer_int(_ep
->desc
)))
145 _ep
->mult
= ((usb_endpoint_maxp(_ep
->desc
) & 0x1800) >> 11) + 1;
151 * Companion descriptor should follow EP descriptor
152 * USB 3.0 spec, #9.6.7
154 comp_desc
= (struct usb_ss_ep_comp_descriptor
*)*(++d_spd
);
156 (comp_desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
))
158 _ep
->comp_desc
= comp_desc
;
159 if (g
->speed
== USB_SPEED_SUPER
) {
160 switch (usb_endpoint_type(_ep
->desc
)) {
161 case USB_ENDPOINT_XFER_ISOC
:
162 /* mult: bits 1:0 of bmAttributes */
163 _ep
->mult
= (comp_desc
->bmAttributes
& 0x3) + 1;
164 case USB_ENDPOINT_XFER_BULK
:
165 case USB_ENDPOINT_XFER_INT
:
166 _ep
->maxburst
= comp_desc
->bMaxBurst
+ 1;
169 if (comp_desc
->bMaxBurst
!= 0)
170 ERROR(cdev
, "ep0 bMaxBurst must be 0\n");
177 EXPORT_SYMBOL_GPL(config_ep_by_speed
);
180 * usb_add_function() - add a function to a configuration
181 * @config: the configuration
182 * @function: the function being added
183 * Context: single threaded during gadget setup
185 * After initialization, each configuration must have one or more
186 * functions added to it. Adding a function involves calling its @bind()
187 * method to allocate resources such as interface and string identifiers
190 * This function returns the value of the function's bind(), which is
191 * zero for success else a negative errno value.
193 int usb_add_function(struct usb_configuration
*config
,
194 struct usb_function
*function
)
198 pr_debug("[XLOG_DEBUG][USB][COM]%s: \n", __func__
);
200 INFO(config
->cdev
, "adding '%s'/%p to config '%s'/%p\n",
201 function
->name
, function
,
202 config
->label
, config
);
204 if (!function
->set_alt
|| !function
->disable
)
207 function
->config
= config
;
208 list_add_tail(&function
->list
, &config
->functions
);
210 /* REVISIT *require* function->bind? */
211 if (function
->bind
) {
212 value
= function
->bind(config
, function
);
214 list_del(&function
->list
);
215 function
->config
= NULL
;
220 /* We allow configurations that don't work at both speeds.
221 * If we run into a lowspeed Linux system, treat it the same
222 * as full speed ... it's the function drivers that will need
223 * to avoid bulk and ISO transfers.
225 if (!config
->fullspeed
&& function
->fs_descriptors
)
226 config
->fullspeed
= true;
227 if (!config
->highspeed
&& function
->hs_descriptors
)
228 config
->highspeed
= true;
229 if (!config
->superspeed
&& function
->ss_descriptors
)
230 config
->superspeed
= true;
234 INFO(config
->cdev
, "adding '%s'/%p --> %d\n",
235 function
->name
, function
, value
);
238 EXPORT_SYMBOL_GPL(usb_add_function
);
240 void usb_remove_function(struct usb_configuration
*c
, struct usb_function
*f
)
243 INFO(c
->cdev
, "disable function '%s'/%p\n", f
->name
, f
);
247 bitmap_zero(f
->endpoints
, 32);
250 INFO(c
->cdev
, "unbind function '%s'/%p\n", f
->name
, f
);
254 EXPORT_SYMBOL_GPL(usb_remove_function
);
257 * usb_function_deactivate - prevent function and gadget enumeration
258 * @function: the function that isn't yet ready to respond
260 * Blocks response of the gadget driver to host enumeration by
261 * preventing the data line pullup from being activated. This is
262 * normally called during @bind() processing to change from the
263 * initial "ready to respond" state, or when a required resource
266 * For example, drivers that serve as a passthrough to a userspace
267 * daemon can block enumeration unless that daemon (such as an OBEX,
268 * MTP, or print server) is ready to handle host requests.
270 * Not all systems support software control of their USB peripheral
273 * Returns zero on success, else negative errno.
275 int usb_function_deactivate(struct usb_function
*function
)
277 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
281 spin_lock_irqsave(&cdev
->lock
, flags
);
283 if (cdev
->deactivations
== 0)
284 status
= usb_gadget_disconnect(cdev
->gadget
);
286 cdev
->deactivations
++;
288 spin_unlock_irqrestore(&cdev
->lock
, flags
);
291 EXPORT_SYMBOL_GPL(usb_function_deactivate
);
294 * usb_function_activate - allow function and gadget enumeration
295 * @function: function on which usb_function_activate() was called
297 * Reverses effect of usb_function_deactivate(). If no more functions
298 * are delaying their activation, the gadget driver will respond to
299 * host enumeration procedures.
301 * Returns zero on success, else negative errno.
303 int usb_function_activate(struct usb_function
*function
)
305 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
309 spin_lock_irqsave(&cdev
->lock
, flags
);
311 if (WARN_ON(cdev
->deactivations
== 0))
314 cdev
->deactivations
--;
315 if (cdev
->deactivations
== 0)
316 status
= usb_gadget_connect(cdev
->gadget
);
319 spin_unlock_irqrestore(&cdev
->lock
, flags
);
322 EXPORT_SYMBOL_GPL(usb_function_activate
);
325 * usb_interface_id() - allocate an unused interface ID
326 * @config: configuration associated with the interface
327 * @function: function handling the interface
328 * Context: single threaded during gadget setup
330 * usb_interface_id() is called from usb_function.bind() callbacks to
331 * allocate new interface IDs. The function driver will then store that
332 * ID in interface, association, CDC union, and other descriptors. It
333 * will also handle any control requests targeted at that interface,
334 * particularly changing its altsetting via set_alt(). There may
335 * also be class-specific or vendor-specific requests to handle.
337 * All interface identifier should be allocated using this routine, to
338 * ensure that for example different functions don't wrongly assign
339 * different meanings to the same identifier. Note that since interface
340 * identifiers are configuration-specific, functions used in more than
341 * one configuration (or more than once in a given configuration) need
342 * multiple versions of the relevant descriptors.
344 * Returns the interface ID which was allocated; or -ENODEV if no
345 * more interface IDs can be allocated.
347 int usb_interface_id(struct usb_configuration
*config
,
348 struct usb_function
*function
)
350 unsigned id
= config
->next_interface_id
;
352 if (id
< MAX_CONFIG_INTERFACES
) {
353 config
->interface
[id
] = function
;
354 config
->next_interface_id
= id
+ 1;
359 EXPORT_SYMBOL_GPL(usb_interface_id
);
361 static u8
encode_bMaxPower(enum usb_device_speed speed
,
362 struct usb_configuration
*c
)
369 val
= CONFIG_USB_GADGET_VBUS_DRAW
;
373 case USB_SPEED_SUPER
:
374 return DIV_ROUND_UP(val
, 8);
376 return DIV_ROUND_UP(val
, 2);
380 static int config_buf(struct usb_configuration
*config
,
381 enum usb_device_speed speed
, void *buf
, u8 type
)
383 struct usb_config_descriptor
*c
= buf
;
384 void *next
= buf
+ USB_DT_CONFIG_SIZE
;
386 struct usb_function
*f
;
389 len
= USB_COMP_EP0_BUFSIZ
- USB_DT_CONFIG_SIZE
;
390 /* write the config descriptor */
392 c
->bLength
= USB_DT_CONFIG_SIZE
;
393 c
->bDescriptorType
= type
;
394 /* wTotalLength is written later */
395 c
->bNumInterfaces
= config
->next_interface_id
;
396 c
->bConfigurationValue
= config
->bConfigurationValue
;
397 c
->iConfiguration
= config
->iConfiguration
;
398 c
->bmAttributes
= USB_CONFIG_ATT_ONE
| config
->bmAttributes
;
399 c
->bMaxPower
= encode_bMaxPower(speed
, config
);
401 /* There may be e.g. OTG descriptors */
402 if (config
->descriptors
) {
403 status
= usb_descriptor_fillbuf(next
, len
,
404 config
->descriptors
);
411 /* add each function's descriptors */
412 list_for_each_entry(f
, &config
->functions
, list
) {
413 struct usb_descriptor_header
**descriptors
;
416 case USB_SPEED_SUPER
:
417 descriptors
= f
->ss_descriptors
;
420 descriptors
= f
->hs_descriptors
;
423 descriptors
= f
->fs_descriptors
;
428 status
= usb_descriptor_fillbuf(next
, len
,
429 (const struct usb_descriptor_header
**) descriptors
);
437 c
->wTotalLength
= cpu_to_le16(len
);
441 static int config_desc(struct usb_composite_dev
*cdev
, unsigned w_value
)
443 struct usb_gadget
*gadget
= cdev
->gadget
;
444 struct usb_configuration
*c
;
445 u8 type
= w_value
>> 8;
446 enum usb_device_speed speed
= USB_SPEED_UNKNOWN
;
448 if (gadget
->speed
== USB_SPEED_SUPER
)
449 speed
= gadget
->speed
;
450 else if (gadget_is_dualspeed(gadget
)) {
452 if (gadget
->speed
== USB_SPEED_HIGH
)
454 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
457 speed
= USB_SPEED_HIGH
;
461 /* This is a lookup by config *INDEX* */
463 list_for_each_entry(c
, &cdev
->configs
, list
) {
464 /* ignore configs that won't work at this speed */
466 case USB_SPEED_SUPER
:
480 return config_buf(c
, speed
, cdev
->req
->buf
, type
);
486 static int count_configs(struct usb_composite_dev
*cdev
, unsigned type
)
488 struct usb_gadget
*gadget
= cdev
->gadget
;
489 struct usb_configuration
*c
;
494 if (gadget_is_dualspeed(gadget
)) {
495 if (gadget
->speed
== USB_SPEED_HIGH
)
497 if (gadget
->speed
== USB_SPEED_SUPER
)
499 if (type
== USB_DT_DEVICE_QUALIFIER
)
502 list_for_each_entry(c
, &cdev
->configs
, list
) {
503 /* ignore configs that won't work at this speed */
520 * bos_desc() - prepares the BOS descriptor.
521 * @cdev: pointer to usb_composite device to generate the bos
524 * This function generates the BOS (Binary Device Object)
525 * descriptor and its device capabilities descriptors. The BOS
526 * descriptor should be supported by a SuperSpeed device.
528 static int bos_desc(struct usb_composite_dev
*cdev
)
530 struct usb_ext_cap_descriptor
*usb_ext
;
531 struct usb_ss_cap_descriptor
*ss_cap
;
532 struct usb_dcd_config_params dcd_config_params
;
533 struct usb_bos_descriptor
*bos
= cdev
->req
->buf
;
535 bos
->bLength
= USB_DT_BOS_SIZE
;
536 bos
->bDescriptorType
= USB_DT_BOS
;
538 bos
->wTotalLength
= cpu_to_le16(USB_DT_BOS_SIZE
);
539 bos
->bNumDeviceCaps
= 0;
542 * A SuperSpeed device shall include the USB2.0 extension descriptor
543 * and shall support LPM when operating in USB2.0 HS mode.
545 usb_ext
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
546 bos
->bNumDeviceCaps
++;
547 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_EXT_CAP_SIZE
);
548 usb_ext
->bLength
= USB_DT_USB_EXT_CAP_SIZE
;
549 usb_ext
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
550 usb_ext
->bDevCapabilityType
= USB_CAP_TYPE_EXT
;
551 #ifdef CONFIG_USBIF_COMPLIANCE
552 usb_ext
->bmAttributes
= cpu_to_le32(USB_LPM_SUPPORT
) | cpu_to_le32(USB_BESL_SUPPORT
) ;
554 usb_ext
->bmAttributes
= cpu_to_le32(USB_LPM_SUPPORT
);
558 * The Superspeed USB Capability descriptor shall be implemented by all
559 * SuperSpeed devices.
561 ss_cap
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
562 bos
->bNumDeviceCaps
++;
563 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_SS_CAP_SIZE
);
564 ss_cap
->bLength
= USB_DT_USB_SS_CAP_SIZE
;
565 ss_cap
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
566 ss_cap
->bDevCapabilityType
= USB_SS_CAP_TYPE
;
567 ss_cap
->bmAttributes
= 0; /* LTM is not supported yet */
568 ss_cap
->wSpeedSupported
= cpu_to_le16(USB_LOW_SPEED_OPERATION
|
569 USB_FULL_SPEED_OPERATION
|
570 USB_HIGH_SPEED_OPERATION
|
571 USB_5GBPS_OPERATION
);
572 ss_cap
->bFunctionalitySupport
= USB_LOW_SPEED_OPERATION
;
574 /* Get Controller configuration */
575 if (cdev
->gadget
->ops
->get_config_params
)
576 cdev
->gadget
->ops
->get_config_params(&dcd_config_params
);
578 dcd_config_params
.bU1devExitLat
= USB_DEFAULT_U1_DEV_EXIT_LAT
;
579 dcd_config_params
.bU2DevExitLat
=
580 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT
);
582 ss_cap
->bU1devExitLat
= dcd_config_params
.bU1devExitLat
;
583 ss_cap
->bU2DevExitLat
= dcd_config_params
.bU2DevExitLat
;
585 return le16_to_cpu(bos
->wTotalLength
);
588 static void device_qual(struct usb_composite_dev
*cdev
)
590 struct usb_qualifier_descriptor
*qual
= cdev
->req
->buf
;
592 qual
->bLength
= sizeof(*qual
);
593 qual
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
594 /* POLICY: same bcdUSB and device type info at both speeds */
595 qual
->bcdUSB
= cdev
->desc
.bcdUSB
;
596 qual
->bDeviceClass
= cdev
->desc
.bDeviceClass
;
597 qual
->bDeviceSubClass
= cdev
->desc
.bDeviceSubClass
;
598 qual
->bDeviceProtocol
= cdev
->desc
.bDeviceProtocol
;
599 /* ASSUME same EP0 fifo size at both speeds */
600 qual
->bMaxPacketSize0
= cdev
->gadget
->ep0
->maxpacket
;
601 qual
->bNumConfigurations
= count_configs(cdev
, USB_DT_DEVICE_QUALIFIER
);
605 /*-------------------------------------------------------------------------*/
607 static void reset_config(struct usb_composite_dev
*cdev
)
609 struct usb_function
*f
;
611 DBG(cdev
, "reset config\n");
613 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
614 INFO(cdev
, "disable function '%s'/%p\n", f
->name
, f
);
618 bitmap_zero(f
->endpoints
, 32);
621 cdev
->delayed_status
= 0;
624 static int set_config(struct usb_composite_dev
*cdev
,
625 const struct usb_ctrlrequest
*ctrl
, unsigned number
)
627 struct usb_gadget
*gadget
= cdev
->gadget
;
628 struct usb_configuration
*c
= NULL
;
629 int result
= -EINVAL
;
630 unsigned power
= gadget_is_otg(gadget
) ? 8 : 100;
634 list_for_each_entry(c
, &cdev
->configs
, list
) {
635 if (c
->bConfigurationValue
== number
) {
637 * We disable the FDs of the previous
638 * configuration only if the new configuration
649 } else { /* Zero configuration value - need to reset the config */
655 INFO(cdev
, "%s config #%d: %s\n",
656 usb_speed_string(gadget
->speed
),
657 number
, c
? c
->label
: "unconfigured");
664 /* Initialize all interfaces by setting them to altsetting zero. */
665 for (tmp
= 0; tmp
< MAX_CONFIG_INTERFACES
; tmp
++) {
666 struct usb_function
*f
= c
->interface
[tmp
];
667 struct usb_descriptor_header
**descriptors
;
673 * Record which endpoints are used by the function. This is used
674 * to dispatch control requests targeted at that endpoint to the
675 * function's setup callback instead of the current
676 * configuration's setup callback.
678 switch (gadget
->speed
) {
679 case USB_SPEED_SUPER
:
680 descriptors
= f
->ss_descriptors
;
683 descriptors
= f
->hs_descriptors
;
686 descriptors
= f
->fs_descriptors
;
689 for (; *descriptors
; ++descriptors
) {
690 struct usb_endpoint_descriptor
*ep
;
693 if ((*descriptors
)->bDescriptorType
!= USB_DT_ENDPOINT
)
696 ep
= (struct usb_endpoint_descriptor
*)*descriptors
;
697 addr
= ((ep
->bEndpointAddress
& 0x80) >> 3)
698 | (ep
->bEndpointAddress
& 0x0f);
699 set_bit(addr
, f
->endpoints
);
702 result
= f
->set_alt(f
, tmp
, 0);
704 DBG(cdev
, "interface %d (%s/%p) alt 0 --> %d\n",
705 tmp
, f
->name
, f
, result
);
711 if (result
== USB_GADGET_DELAYED_STATUS
) {
713 "%s: interface %d (%s) requested delayed status\n",
714 __func__
, tmp
, f
->name
);
715 cdev
->delayed_status
++;
716 DBG(cdev
, "delayed_status count %d\n",
717 cdev
->delayed_status
);
721 /* when we return, be sure our power usage is valid */
722 power
= c
->MaxPower
? c
->MaxPower
: CONFIG_USB_GADGET_VBUS_DRAW
;
724 usb_gadget_vbus_draw(gadget
, power
);
725 if (result
>= 0 && cdev
->delayed_status
)
726 result
= USB_GADGET_DELAYED_STATUS
;
730 int usb_add_config_only(struct usb_composite_dev
*cdev
,
731 struct usb_configuration
*config
)
733 struct usb_configuration
*c
;
735 if (!config
->bConfigurationValue
)
738 /* Prevent duplicate configuration identifiers */
739 list_for_each_entry(c
, &cdev
->configs
, list
) {
740 if (c
->bConfigurationValue
== config
->bConfigurationValue
)
745 list_add_tail(&config
->list
, &cdev
->configs
);
747 INIT_LIST_HEAD(&config
->functions
);
748 config
->next_interface_id
= 0;
749 memset(config
->interface
, 0, sizeof(config
->interface
));
753 EXPORT_SYMBOL_GPL(usb_add_config_only
);
756 * usb_add_config() - add a configuration to a device.
757 * @cdev: wraps the USB gadget
758 * @config: the configuration, with bConfigurationValue assigned
759 * @bind: the configuration's bind function
760 * Context: single threaded during gadget setup
762 * One of the main tasks of a composite @bind() routine is to
763 * add each of the configurations it supports, using this routine.
765 * This function returns the value of the configuration's @bind(), which
766 * is zero for success else a negative errno value. Binding configurations
767 * assigns global resources including string IDs, and per-configuration
768 * resources such as interface IDs and endpoints.
770 int usb_add_config(struct usb_composite_dev
*cdev
,
771 struct usb_configuration
*config
,
772 int (*bind
)(struct usb_configuration
*))
774 int status
= -EINVAL
;
779 DBG(cdev
, "adding config #%u '%s'/%p\n",
780 config
->bConfigurationValue
,
781 config
->label
, config
);
783 status
= usb_add_config_only(cdev
, config
);
787 status
= bind(config
);
789 while (!list_empty(&config
->functions
)) {
790 struct usb_function
*f
;
792 f
= list_first_entry(&config
->functions
,
793 struct usb_function
, list
);
796 INFO(cdev
, "unbind function '%s'/%p\n",
798 f
->unbind(config
, f
);
799 /* may free memory for "f" */
802 list_del(&config
->list
);
803 pr_debug("[XLOG_DEBUG][USB][COM]bind fialed and the list should be init because there is one entry only");
808 INFO(cdev
, "cfg %d/%p speeds:%s%s%s\n",
809 config
->bConfigurationValue
, config
,
810 config
->superspeed
? " super" : "",
811 config
->highspeed
? " high" : "",
813 ? (gadget_is_dualspeed(cdev
->gadget
)
818 for (i
= 0; i
< MAX_CONFIG_INTERFACES
; i
++) {
819 struct usb_function
*f
= config
->interface
[i
];
823 DBG(cdev
, " interface %d = %s/%p\n",
828 /* set_alt(), or next bind(), sets up
829 * ep->driver_data as needed.
831 usb_ep_autoconfig_reset(cdev
->gadget
);
835 DBG(cdev
, "added config '%s'/%u --> %d\n", config
->label
,
836 config
->bConfigurationValue
, status
);
839 EXPORT_SYMBOL_GPL(usb_add_config
);
841 static void unbind_config(struct usb_composite_dev
*cdev
,
842 struct usb_configuration
*config
)
844 while (!list_empty(&config
->functions
)) {
845 struct usb_function
*f
;
847 f
= list_first_entry(&config
->functions
,
848 struct usb_function
, list
);
851 INFO(cdev
, "unbind function '%s'/%p\n", f
->name
, f
);
852 f
->unbind(config
, f
);
853 /* may free memory for "f" */
856 if (config
->unbind
) {
857 INFO(cdev
, "unbind config '%s'/%p\n", config
->label
, config
);
858 config
->unbind(config
);
859 /* may free memory for "c" */
862 /* reset all driver data to prevent leakage of ep allocation */
863 usb_ep_autoconfig_reset(cdev
->gadget
);
867 * usb_remove_config() - remove a configuration from a device.
868 * @cdev: wraps the USB gadget
869 * @config: the configuration
871 * Drivers must call usb_gadget_disconnect before calling this function
872 * to disconnect the device from the host and make sure the host will not
873 * try to enumerate the device while we are changing the config list.
875 void usb_remove_config(struct usb_composite_dev
*cdev
,
876 struct usb_configuration
*config
)
880 spin_lock_irqsave(&cdev
->lock
, flags
);
882 if (cdev
->config
== config
)
886 if(config
->cdev
!= NULL
)
888 list_del(&config
->list
);
891 DBG(cdev
, "%s: config->list has been delete!! \n", __func__
);
894 spin_unlock_irqrestore(&cdev
->lock
, flags
);
896 unbind_config(cdev
, config
);
899 /*-------------------------------------------------------------------------*/
901 /* We support strings in multiple languages ... string descriptor zero
902 * says which languages are supported. The typical case will be that
903 * only one language (probably English) is used, with I18N handled on
907 static void collect_langs(struct usb_gadget_strings
**sp
, __le16
*buf
)
909 const struct usb_gadget_strings
*s
;
915 language
= cpu_to_le16(s
->language
);
916 for (tmp
= buf
; *tmp
&& tmp
< &buf
[126]; tmp
++) {
917 if (*tmp
== language
)
926 static int lookup_string(
927 struct usb_gadget_strings
**sp
,
933 struct usb_gadget_strings
*s
;
938 if (s
->language
!= language
)
940 value
= usb_gadget_get_string(s
, id
, buf
);
947 static int get_string(struct usb_composite_dev
*cdev
,
948 void *buf
, u16 language
, int id
)
950 struct usb_composite_driver
*composite
= cdev
->driver
;
951 struct usb_gadget_string_container
*uc
;
952 struct usb_configuration
*c
;
953 struct usb_function
*f
;
956 /* Yes, not only is USB's I18N support probably more than most
957 * folk will ever care about ... also, it's all supported here.
958 * (Except for UTF8 support for Unicode's "Astral Planes".)
961 /* 0 == report all available language codes */
963 struct usb_string_descriptor
*s
= buf
;
964 struct usb_gadget_strings
**sp
;
967 s
->bDescriptorType
= USB_DT_STRING
;
969 sp
= composite
->strings
;
971 collect_langs(sp
, s
->wData
);
973 list_for_each_entry(c
, &cdev
->configs
, list
) {
976 collect_langs(sp
, s
->wData
);
978 list_for_each_entry(f
, &c
->functions
, list
) {
981 collect_langs(sp
, s
->wData
);
984 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
985 struct usb_gadget_strings
**sp
;
987 sp
= get_containers_gs(uc
);
988 collect_langs(sp
, s
->wData
);
991 for (len
= 0; len
<= 126 && s
->wData
[len
]; len
++)
996 s
->bLength
= 2 * (len
+ 1);
1000 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
1001 struct usb_gadget_strings
**sp
;
1003 sp
= get_containers_gs(uc
);
1004 len
= lookup_string(sp
, buf
, language
, id
);
1009 /* String IDs are device-scoped, so we look up each string
1010 * table we're told about. These lookups are infrequent;
1011 * simpler-is-better here.
1013 if (composite
->strings
) {
1014 len
= lookup_string(composite
->strings
, buf
, language
, id
);
1018 list_for_each_entry(c
, &cdev
->configs
, list
) {
1020 len
= lookup_string(c
->strings
, buf
, language
, id
);
1024 list_for_each_entry(f
, &c
->functions
, list
) {
1027 len
= lookup_string(f
->strings
, buf
, language
, id
);
1036 * usb_string_id() - allocate an unused string ID
1037 * @cdev: the device whose string descriptor IDs are being allocated
1038 * Context: single threaded during gadget setup
1040 * @usb_string_id() is called from bind() callbacks to allocate
1041 * string IDs. Drivers for functions, configurations, or gadgets will
1042 * then store that ID in the appropriate descriptors and string table.
1044 * All string identifier should be allocated using this,
1045 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1046 * that for example different functions don't wrongly assign different
1047 * meanings to the same identifier.
1049 int usb_string_id(struct usb_composite_dev
*cdev
)
1051 if (cdev
->next_string_id
< 254) {
1052 /* string id 0 is reserved by USB spec for list of
1053 * supported languages */
1054 /* 255 reserved as well? -- mina86 */
1055 cdev
->next_string_id
++;
1056 return cdev
->next_string_id
;
1060 EXPORT_SYMBOL_GPL(usb_string_id
);
1063 * usb_string_ids() - allocate unused string IDs in batch
1064 * @cdev: the device whose string descriptor IDs are being allocated
1065 * @str: an array of usb_string objects to assign numbers to
1066 * Context: single threaded during gadget setup
1068 * @usb_string_ids() is called from bind() callbacks to allocate
1069 * string IDs. Drivers for functions, configurations, or gadgets will
1070 * then copy IDs from the string table to the appropriate descriptors
1071 * and string table for other languages.
1073 * All string identifier should be allocated using this,
1074 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1075 * example different functions don't wrongly assign different meanings
1076 * to the same identifier.
1078 int usb_string_ids_tab(struct usb_composite_dev
*cdev
, struct usb_string
*str
)
1080 int next
= cdev
->next_string_id
;
1082 for (; str
->s
; ++str
) {
1083 if (unlikely(next
>= 254))
1088 cdev
->next_string_id
= next
;
1092 EXPORT_SYMBOL_GPL(usb_string_ids_tab
);
1094 static struct usb_gadget_string_container
*copy_gadget_strings(
1095 struct usb_gadget_strings
**sp
, unsigned n_gstrings
,
1098 struct usb_gadget_string_container
*uc
;
1099 struct usb_gadget_strings
**gs_array
;
1100 struct usb_gadget_strings
*gs
;
1101 struct usb_string
*s
;
1108 mem
+= sizeof(void *) * (n_gstrings
+ 1);
1109 mem
+= sizeof(struct usb_gadget_strings
) * n_gstrings
;
1110 mem
+= sizeof(struct usb_string
) * (n_strings
+ 1) * (n_gstrings
);
1111 uc
= kmalloc(mem
, GFP_KERNEL
);
1113 return ERR_PTR(-ENOMEM
);
1114 gs_array
= get_containers_gs(uc
);
1116 stash
+= sizeof(void *) * (n_gstrings
+ 1);
1117 for (n_gs
= 0; n_gs
< n_gstrings
; n_gs
++) {
1118 struct usb_string
*org_s
;
1120 gs_array
[n_gs
] = stash
;
1121 gs
= gs_array
[n_gs
];
1122 stash
+= sizeof(struct usb_gadget_strings
);
1123 gs
->language
= sp
[n_gs
]->language
;
1124 gs
->strings
= stash
;
1125 org_s
= sp
[n_gs
]->strings
;
1127 for (n_s
= 0; n_s
< n_strings
; n_s
++) {
1129 stash
+= sizeof(struct usb_string
);
1138 stash
+= sizeof(struct usb_string
);
1141 gs_array
[n_gs
] = NULL
;
1146 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1147 * @cdev: the device whose string descriptor IDs are being allocated
1149 * @sp: an array of usb_gadget_strings to attach.
1150 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1152 * This function will create a deep copy of usb_gadget_strings and usb_string
1153 * and attach it to the cdev. The actual string (usb_string.s) will not be
1154 * copied but only a referenced will be made. The struct usb_gadget_strings
1155 * array may contain multiple languges and should be NULL terminated.
1156 * The ->language pointer of each struct usb_gadget_strings has to contain the
1157 * same amount of entries.
1158 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1159 * usb_string entry of es-ES containts the translation of the first usb_string
1160 * entry of en-US. Therefore both entries become the same id assign.
1162 struct usb_string
*usb_gstrings_attach(struct usb_composite_dev
*cdev
,
1163 struct usb_gadget_strings
**sp
, unsigned n_strings
)
1165 struct usb_gadget_string_container
*uc
;
1166 struct usb_gadget_strings
**n_gs
;
1167 unsigned n_gstrings
= 0;
1171 for (i
= 0; sp
[i
]; i
++)
1175 return ERR_PTR(-EINVAL
);
1177 uc
= copy_gadget_strings(sp
, n_gstrings
, n_strings
);
1179 return ERR_PTR(PTR_ERR(uc
));
1181 n_gs
= get_containers_gs(uc
);
1182 ret
= usb_string_ids_tab(cdev
, n_gs
[0]->strings
);
1186 for (i
= 1; i
< n_gstrings
; i
++) {
1187 struct usb_string
*m_s
;
1188 struct usb_string
*s
;
1191 m_s
= n_gs
[0]->strings
;
1192 s
= n_gs
[i
]->strings
;
1193 for (n
= 0; n
< n_strings
; n
++) {
1199 list_add_tail(&uc
->list
, &cdev
->gstrings
);
1200 return n_gs
[0]->strings
;
1203 return ERR_PTR(ret
);
1205 EXPORT_SYMBOL_GPL(usb_gstrings_attach
);
1208 * usb_string_ids_n() - allocate unused string IDs in batch
1209 * @c: the device whose string descriptor IDs are being allocated
1210 * @n: number of string IDs to allocate
1211 * Context: single threaded during gadget setup
1213 * Returns the first requested ID. This ID and next @n-1 IDs are now
1214 * valid IDs. At least provided that @n is non-zero because if it
1215 * is, returns last requested ID which is now very useful information.
1217 * @usb_string_ids_n() is called from bind() callbacks to allocate
1218 * string IDs. Drivers for functions, configurations, or gadgets will
1219 * then store that ID in the appropriate descriptors and string table.
1221 * All string identifier should be allocated using this,
1222 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1223 * example different functions don't wrongly assign different meanings
1224 * to the same identifier.
1226 int usb_string_ids_n(struct usb_composite_dev
*c
, unsigned n
)
1228 unsigned next
= c
->next_string_id
;
1229 if (unlikely(n
> 254 || (unsigned)next
+ n
> 254))
1231 c
->next_string_id
+= n
;
1234 EXPORT_SYMBOL_GPL(usb_string_ids_n
);
1236 /*-------------------------------------------------------------------------*/
1238 void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1240 if (req
->status
|| req
->actual
!= req
->length
)
1241 DBG((struct usb_composite_dev
*) ep
->driver_data
,
1242 "setup complete --> %d, %d/%d\n",
1243 req
->status
, req
->actual
, req
->length
);
1246 EXPORT_SYMBOL_GPL(composite_setup_complete
);
1249 * The setup() callback implements all the ep0 functionality that's
1250 * not handled lower down, in hardware or the hardware driver(like
1251 * device and endpoint feature flags, and their status). It's all
1252 * housekeeping for the gadget function we're implementing. Most of
1253 * the work is in config and function specific setup.
1256 composite_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1258 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1259 struct usb_request
*req
= cdev
->req
;
1260 int value
= -EOPNOTSUPP
;
1262 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1263 u8 intf
= w_index
& 0xFF;
1264 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1265 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1266 struct usb_function
*f
= NULL
;
1269 pr_debug("[XLOG_DEBUG][USB][COM]%s bRequest=0x%X\n",
1270 __func__
, ctrl
->bRequest
);
1272 /* partial re-init of the response message; the function or the
1273 * gadget might need to intercept e.g. a control-OUT completion
1274 * when we delegate to it.
1277 req
->complete
= composite_setup_complete
;
1279 gadget
->ep0
->driver_data
= cdev
;
1281 switch (ctrl
->bRequest
) {
1283 /* we handle all standard USB descriptors */
1284 case USB_REQ_GET_DESCRIPTOR
:
1285 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1287 switch (w_value
>> 8) {
1288 #ifdef CONFIG_USBIF_COMPLIANCE
1291 struct usb_otg_descriptor
*otg_desc
= req
->buf
;
1292 otg_desc
->bLength
= sizeof(*otg_desc
);
1293 otg_desc
->bDescriptorType
= USB_DT_OTG
;
1294 otg_desc
->bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
;
1295 otg_desc
->bcdOTG
= cpu_to_le16(0x0200);
1296 value
= min_t(int, w_length
,sizeof(struct usb_otg_descriptor
));
1301 cdev
->desc
.bNumConfigurations
=
1302 count_configs(cdev
, USB_DT_DEVICE
);
1303 cdev
->desc
.bMaxPacketSize0
=
1304 cdev
->gadget
->ep0
->maxpacket
;
1305 if (gadget_is_superspeed(gadget
)) {
1306 if (gadget
->speed
>= USB_SPEED_SUPER
) {
1307 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0300);
1308 cdev
->desc
.bMaxPacketSize0
= 9;
1310 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0210);
1314 value
= min(w_length
, (u16
) sizeof cdev
->desc
);
1315 memcpy(req
->buf
, &cdev
->desc
, value
);
1316 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1317 "USB_DT_DEVICE, value=%d\n",value
);
1319 case USB_DT_DEVICE_QUALIFIER
:
1320 if (!gadget_is_dualspeed(gadget
) ||
1321 gadget
->speed
>= USB_SPEED_SUPER
)
1324 value
= min_t(int, w_length
,
1325 sizeof(struct usb_qualifier_descriptor
));
1326 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1327 "USB_DT_DEVICE_QUALIFIER, value=%d\n",value
);
1329 case USB_DT_OTHER_SPEED_CONFIG
:
1330 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1331 "USB_DT_OTHER_SPEED_CONFIG\n");
1332 if (!gadget_is_dualspeed(gadget
) ||
1333 gadget
->speed
>= USB_SPEED_SUPER
)
1337 value
= config_desc(cdev
, w_value
);
1339 value
= min(w_length
, (u16
) value
);
1340 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1341 "USB_DT_CONFIG, value=%d\n",value
);
1344 value
= get_string(cdev
, req
->buf
,
1345 w_index
, w_value
& 0xff);
1347 value
= min(w_length
, (u16
) value
);
1348 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1349 "USB_DT_STRING, value=%d\n" ,value
);
1353 if (gadget_is_superspeed(gadget
)) {
1354 value
= bos_desc(cdev
);
1355 value
= min(w_length
, (u16
) value
);
1357 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR: "
1358 "USB_DT_BOS, value=%d\n",value
);
1361 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_DESCRIPTOR w_value=0x%X\n", w_value
);
1366 /* any number of configs can work */
1367 case USB_REQ_SET_CONFIGURATION
:
1368 if (ctrl
->bRequestType
!= 0)
1370 if (gadget_is_otg(gadget
)) {
1371 if (gadget
->a_hnp_support
)
1372 DBG(cdev
, "HNP available\n");
1373 else if (gadget
->a_alt_hnp_support
)
1374 DBG(cdev
, "HNP on another port\n");
1376 VDBG(cdev
, "HNP inactive\n");
1378 spin_lock(&cdev
->lock
);
1379 value
= set_config(cdev
, ctrl
, w_value
);
1380 spin_unlock(&cdev
->lock
);
1381 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_SET_CONFIGURATION: "
1382 "value=%d\n",value
);
1384 case USB_REQ_GET_CONFIGURATION
:
1385 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1388 *(u8
*)req
->buf
= cdev
->config
->bConfigurationValue
;
1390 *(u8
*)req
->buf
= 0;
1391 value
= min(w_length
, (u16
) 1);
1394 /* function drivers must handle get/set altsetting */
1395 case USB_REQ_SET_INTERFACE
:
1396 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
1398 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1402 f
= cdev
->config
->interface
[intf
];
1404 pr_debug("%s: cdev->config = NULL \n", __func__
);
1410 * If there's no get_alt() method, we know only altsetting zero
1411 * works. There is no need to check if set_alt() is not NULL
1412 * as we check this in usb_add_function().
1414 if (w_value
&& !f
->get_alt
)
1416 value
= f
->set_alt(f
, w_index
, w_value
);
1417 if (value
== USB_GADGET_DELAYED_STATUS
) {
1419 "%s: interface %d (%s) requested delayed status\n",
1420 __func__
, intf
, f
->name
);
1421 cdev
->delayed_status
++;
1422 DBG(cdev
, "delayed_status count %d\n",
1423 cdev
->delayed_status
);
1426 case USB_REQ_GET_INTERFACE
:
1427 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1429 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1431 f
= cdev
->config
->interface
[intf
];
1434 /* lots of interfaces only need altsetting zero... */
1435 value
= f
->get_alt
? f
->get_alt(f
, w_index
) : 0;
1438 *((u8
*)req
->buf
) = value
;
1439 value
= min(w_length
, (u16
) 1);
1443 * USB 3.0 additions:
1444 * Function driver should handle get_status request. If such cb
1445 * wasn't supplied we respond with default value = 0
1446 * Note: function driver should supply such cb only for the first
1447 * interface of the function
1449 case USB_REQ_GET_STATUS
:
1450 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_GET_STATUS\n");
1451 if (!gadget_is_superspeed(gadget
))
1453 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_RECIP_INTERFACE
))
1455 value
= 2; /* This is the length of the get_status reply */
1456 put_unaligned_le16(0, req
->buf
);
1457 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1459 f
= cdev
->config
->interface
[intf
];
1462 status
= f
->get_status
? f
->get_status(f
) : 0;
1465 put_unaligned_le16(status
& 0x0000ffff, req
->buf
);
1468 * Function drivers should handle SetFeature/ClearFeature
1469 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1470 * only for the first interface of the function
1472 case USB_REQ_CLEAR_FEATURE
:
1473 case USB_REQ_SET_FEATURE
:
1474 pr_debug("[XLOG_DEBUG][USB][COM]%s w_value=%d\n",
1475 ((ctrl
->bRequest
==USB_REQ_SET_FEATURE
)? "USB_REQ_SET_FEATURE" : "USB_REQ_CLEAR_FEATURE"), w_value
);
1476 if (!gadget_is_superspeed(gadget
))
1478 if (ctrl
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_INTERFACE
))
1481 case USB_INTRF_FUNC_SUSPEND
:
1482 pr_debug("[COM]USB_INTRF_FUNC_SUSPEND\n");
1483 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1485 f
= cdev
->config
->interface
[intf
];
1489 if (f
->func_suspend
)
1490 value
= f
->func_suspend(f
, w_index
>> 8);
1493 "func_suspend() returned error %d\n",
1500 case USB_REQ_SET_SEL
:
1501 pr_debug("[XLOG_DEBUG][USB][COM]USB_REQ_SET_SEL Pretend success\n");
1507 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1508 ctrl
->bRequestType
, ctrl
->bRequest
,
1509 w_value
, w_index
, w_length
);
1511 /* functions always handle their interfaces and endpoints...
1512 * punt other recipients (other, WUSB, ...) to the current
1513 * configuration code.
1515 * REVISIT it could make sense to let the composite device
1516 * take such requests too, if that's ever needed: to work
1519 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1520 case USB_RECIP_INTERFACE
:
1521 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1523 f
= cdev
->config
->interface
[intf
];
1526 case USB_RECIP_ENDPOINT
:
1527 endp
= ((w_index
& 0x80) >> 3) | (w_index
& 0x0f);
1528 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1529 if (test_bit(endp
, f
->endpoints
))
1532 if (&f
->list
== &cdev
->config
->functions
)
1538 value
= f
->setup(f
, ctrl
);
1540 struct usb_configuration
*c
;
1544 value
= c
->setup(c
, ctrl
);
1550 /* respond with data transfer before status phase? */
1551 if (value
>= 0 && value
!= USB_GADGET_DELAYED_STATUS
) {
1552 req
->length
= value
;
1553 req
->zero
= value
< w_length
;
1554 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
1556 DBG(cdev
, "ep_queue --> %d\n", value
);
1558 composite_setup_complete(gadget
->ep0
, req
);
1560 } else if (value
== USB_GADGET_DELAYED_STATUS
&& w_length
!= 0) {
1562 "%s: Delayed status not supported for w_length != 0",
1568 pr_debug("[XLOG_DEBUG][USB][COM]composite_setup: value=%d,"
1569 "bRequestType=0x%x, bRequest=0x%x, w_value=0x%x, w_length=0x%x \n", value
,
1570 ctrl
->bRequestType
, ctrl
->bRequest
, w_value
, w_length
);
1572 /* device either stalls (value < 0) or reports success */
1576 void composite_disconnect(struct usb_gadget
*gadget
)
1578 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1579 unsigned long flags
;
1581 /* REVISIT: should we have config and device level
1582 * disconnect callbacks?
1584 spin_lock_irqsave(&cdev
->lock
, flags
);
1587 if (cdev
->driver
->disconnect
)
1588 cdev
->driver
->disconnect(cdev
);
1590 /* ALPS00235316 and ALPS00234976 */
1591 /* reset the complet function */
1592 if(cdev
->req
->complete
) {
1593 pr_debug("[XLOG_DEBUG][USB][COM]%s: reassign the complete function!!\n", __func__
);
1594 cdev
->req
->complete
= composite_setup_complete
;
1597 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1600 /*-------------------------------------------------------------------------*/
1602 static ssize_t
composite_show_suspended(struct device
*dev
,
1603 struct device_attribute
*attr
,
1606 struct usb_gadget
*gadget
= dev_to_usb_gadget(dev
);
1607 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1609 return sprintf(buf
, "%d\n", cdev
->suspended
);
1612 static DEVICE_ATTR(suspended
, 0444, composite_show_suspended
, NULL
);
1614 static void __composite_unbind(struct usb_gadget
*gadget
, bool unbind_driver
)
1616 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1618 /* composite_disconnect() must already have been called
1619 * by the underlying peripheral controller driver!
1620 * so there's no i/o concurrency that could affect the
1621 * state protected by cdev->lock.
1623 WARN_ON(cdev
->config
);
1625 while (!list_empty(&cdev
->configs
)) {
1626 struct usb_configuration
*c
;
1627 c
= list_first_entry(&cdev
->configs
,
1628 struct usb_configuration
, list
);
1630 unbind_config(cdev
, c
);
1632 if (cdev
->driver
->unbind
&& unbind_driver
)
1633 cdev
->driver
->unbind(cdev
);
1635 composite_dev_cleanup(cdev
);
1637 kfree(cdev
->def_manufacturer
);
1639 set_gadget_data(gadget
, NULL
);
1642 static void composite_unbind(struct usb_gadget
*gadget
)
1644 __composite_unbind(gadget
, true);
1647 static void update_unchanged_dev_desc(struct usb_device_descriptor
*new,
1648 const struct usb_device_descriptor
*old
)
1658 * these variables may have been set in
1659 * usb_composite_overwrite_options()
1661 idVendor
= new->idVendor
;
1662 idProduct
= new->idProduct
;
1663 bcdDevice
= new->bcdDevice
;
1664 iSerialNumber
= new->iSerialNumber
;
1665 iManufacturer
= new->iManufacturer
;
1666 iProduct
= new->iProduct
;
1670 new->idVendor
= idVendor
;
1672 new->idProduct
= idProduct
;
1674 new->bcdDevice
= bcdDevice
;
1676 new->bcdDevice
= cpu_to_le16(get_default_bcdDevice());
1678 new->iSerialNumber
= iSerialNumber
;
1680 new->iManufacturer
= iManufacturer
;
1682 new->iProduct
= iProduct
;
1685 int composite_dev_prepare(struct usb_composite_driver
*composite
,
1686 struct usb_composite_dev
*cdev
)
1688 struct usb_gadget
*gadget
= cdev
->gadget
;
1691 /* preallocate control response and buffer */
1692 cdev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
1696 cdev
->req
->buf
= kmalloc(USB_COMP_EP0_BUFSIZ
, GFP_KERNEL
);
1697 if (!cdev
->req
->buf
)
1700 ret
= device_create_file(&gadget
->dev
, &dev_attr_suspended
);
1704 cdev
->req
->complete
= composite_setup_complete
;
1705 gadget
->ep0
->driver_data
= cdev
;
1707 cdev
->driver
= composite
;
1710 * As per USB compliance update, a device that is actively drawing
1711 * more than 100mA from USB must report itself as bus-powered in
1712 * the GetStatus(DEVICE) call.
1714 if (CONFIG_USB_GADGET_VBUS_DRAW
<= USB_SELF_POWER_VBUS_MAX_DRAW
)
1715 usb_gadget_set_selfpowered(gadget
);
1717 /* interface and string IDs start at zero via kzalloc.
1718 * we force endpoints to start unassigned; few controller
1719 * drivers will zero ep->driver_data.
1721 usb_ep_autoconfig_reset(gadget
);
1724 kfree(cdev
->req
->buf
);
1726 usb_ep_free_request(gadget
->ep0
, cdev
->req
);
1731 void composite_dev_cleanup(struct usb_composite_dev
*cdev
)
1733 struct usb_gadget_string_container
*uc
, *tmp
;
1735 list_for_each_entry_safe(uc
, tmp
, &cdev
->gstrings
, list
) {
1736 list_del(&uc
->list
);
1740 kfree(cdev
->req
->buf
);
1741 usb_ep_free_request(cdev
->gadget
->ep0
, cdev
->req
);
1743 cdev
->next_string_id
= 0;
1744 device_remove_file(&cdev
->gadget
->dev
, &dev_attr_suspended
);
1747 static int composite_bind(struct usb_gadget
*gadget
,
1748 struct usb_gadget_driver
*gdriver
)
1750 struct usb_composite_dev
*cdev
;
1751 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
1752 int status
= -ENOMEM
;
1754 cdev
= kzalloc(sizeof *cdev
, GFP_KERNEL
);
1758 spin_lock_init(&cdev
->lock
);
1759 cdev
->gadget
= gadget
;
1760 set_gadget_data(gadget
, cdev
);
1761 INIT_LIST_HEAD(&cdev
->configs
);
1762 INIT_LIST_HEAD(&cdev
->gstrings
);
1764 status
= composite_dev_prepare(composite
, cdev
);
1768 /* composite gadget needs to assign strings for whole device (like
1769 * serial number), register function drivers, potentially update
1770 * power state and consumption, etc
1772 status
= composite
->bind(cdev
);
1776 update_unchanged_dev_desc(&cdev
->desc
, composite
->dev
);
1778 /* has userspace failed to provide a serial number? */
1779 if (composite
->needs_serial
&& !cdev
->desc
.iSerialNumber
)
1780 WARNING(cdev
, "userspace failed to provide iSerialNumber\n");
1782 INFO(cdev
, "%s ready\n", composite
->name
);
1786 __composite_unbind(gadget
, false);
1790 /*-------------------------------------------------------------------------*/
1793 composite_suspend(struct usb_gadget
*gadget
)
1795 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1796 struct usb_function
*f
;
1798 /* REVISIT: should we have config level
1799 * suspend/resume callbacks?
1801 DBG(cdev
, "suspend\n");
1803 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1808 if (cdev
->driver
->suspend
)
1809 cdev
->driver
->suspend(cdev
);
1811 cdev
->suspended
= 1;
1813 usb_gadget_vbus_draw(gadget
, 2);
1817 composite_resume(struct usb_gadget
*gadget
)
1819 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1820 struct usb_function
*f
;
1823 /* REVISIT: should we have config level
1824 * suspend/resume callbacks?
1826 DBG(cdev
, "resume\n");
1827 if (cdev
->driver
->resume
)
1828 cdev
->driver
->resume(cdev
);
1830 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1835 maxpower
= cdev
->config
->MaxPower
;
1837 usb_gadget_vbus_draw(gadget
, maxpower
?
1838 maxpower
: CONFIG_USB_GADGET_VBUS_DRAW
);
1841 cdev
->suspended
= 0;
1844 /*-------------------------------------------------------------------------*/
1846 static const struct usb_gadget_driver composite_driver_template
= {
1847 .bind
= composite_bind
,
1848 .unbind
= composite_unbind
,
1850 .setup
= composite_setup
,
1851 .disconnect
= composite_disconnect
,
1853 .suspend
= composite_suspend
,
1854 .resume
= composite_resume
,
1857 .owner
= THIS_MODULE
,
1862 * usb_composite_probe() - register a composite driver
1863 * @driver: the driver to register
1865 * Context: single threaded during gadget setup
1867 * This function is used to register drivers using the composite driver
1868 * framework. The return value is zero, or a negative errno value.
1869 * Those values normally come from the driver's @bind method, which does
1870 * all the work of setting up the driver to match the hardware.
1872 * On successful return, the gadget is ready to respond to requests from
1873 * the host, unless one of its components invokes usb_gadget_disconnect()
1874 * while it was binding. That would usually be done in order to wait for
1875 * some userspace participation.
1877 int usb_composite_probe(struct usb_composite_driver
*driver
)
1879 struct usb_gadget_driver
*gadget_driver
;
1881 if (!driver
|| !driver
->dev
|| !driver
->bind
)
1884 pr_debug("[XLOG_DEBUG][USB][COM]%s: driver->name = %s", __func__
, driver
->name
);
1887 driver
->name
= "composite";
1889 driver
->gadget_driver
= composite_driver_template
;
1890 gadget_driver
= &driver
->gadget_driver
;
1892 gadget_driver
->function
= (char *) driver
->name
;
1893 gadget_driver
->driver
.name
= driver
->name
;
1894 gadget_driver
->max_speed
= driver
->max_speed
;
1896 return usb_gadget_probe_driver(gadget_driver
);
1898 EXPORT_SYMBOL_GPL(usb_composite_probe
);
1901 * usb_composite_unregister() - unregister a composite driver
1902 * @driver: the driver to unregister
1904 * This function is used to unregister drivers using the composite
1907 void usb_composite_unregister(struct usb_composite_driver
*driver
)
1909 usb_gadget_unregister_driver(&driver
->gadget_driver
);
1911 EXPORT_SYMBOL_GPL(usb_composite_unregister
);
1914 * usb_composite_setup_continue() - Continue with the control transfer
1915 * @cdev: the composite device who's control transfer was kept waiting
1917 * This function must be called by the USB function driver to continue
1918 * with the control transfer's data/status stage in case it had requested to
1919 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1920 * can request the composite framework to delay the setup request's data/status
1921 * stages by returning USB_GADGET_DELAYED_STATUS.
1923 void usb_composite_setup_continue(struct usb_composite_dev
*cdev
)
1926 struct usb_request
*req
= cdev
->req
;
1927 unsigned long flags
;
1929 DBG(cdev
, "%s\n", __func__
);
1930 spin_lock_irqsave(&cdev
->lock
, flags
);
1932 if (cdev
->delayed_status
== 0) {
1933 WARN(cdev
, "%s: Unexpected call\n", __func__
);
1935 } else if (--cdev
->delayed_status
== 0) {
1936 DBG(cdev
, "%s: Completing delayed status\n", __func__
);
1938 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
1940 DBG(cdev
, "ep_queue --> %d\n", value
);
1942 composite_setup_complete(cdev
->gadget
->ep0
, req
);
1946 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1948 EXPORT_SYMBOL_GPL(usb_composite_setup_continue
);
1950 static char *composite_default_mfr(struct usb_gadget
*gadget
)
1955 len
= snprintf(NULL
, 0, "%s %s with %s", init_utsname()->sysname
,
1956 init_utsname()->release
, gadget
->name
);
1958 mfr
= kmalloc(len
, GFP_KERNEL
);
1961 snprintf(mfr
, len
, "%s %s with %s", init_utsname()->sysname
,
1962 init_utsname()->release
, gadget
->name
);
1966 void usb_composite_overwrite_options(struct usb_composite_dev
*cdev
,
1967 struct usb_composite_overwrite
*covr
)
1969 struct usb_device_descriptor
*desc
= &cdev
->desc
;
1970 struct usb_gadget_strings
*gstr
= cdev
->driver
->strings
[0];
1971 struct usb_string
*dev_str
= gstr
->strings
;
1974 desc
->idVendor
= cpu_to_le16(covr
->idVendor
);
1976 if (covr
->idProduct
)
1977 desc
->idProduct
= cpu_to_le16(covr
->idProduct
);
1979 if (covr
->bcdDevice
)
1980 desc
->bcdDevice
= cpu_to_le16(covr
->bcdDevice
);
1982 if (covr
->serial_number
) {
1983 desc
->iSerialNumber
= dev_str
[USB_GADGET_SERIAL_IDX
].id
;
1984 dev_str
[USB_GADGET_SERIAL_IDX
].s
= covr
->serial_number
;
1986 if (covr
->manufacturer
) {
1987 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
1988 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= covr
->manufacturer
;
1990 } else if (!strlen(dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
)) {
1991 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
1992 cdev
->def_manufacturer
= composite_default_mfr(cdev
->gadget
);
1993 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= cdev
->def_manufacturer
;
1996 if (covr
->product
) {
1997 desc
->iProduct
= dev_str
[USB_GADGET_PRODUCT_IDX
].id
;
1998 dev_str
[USB_GADGET_PRODUCT_IDX
].s
= covr
->product
;
2001 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options
);
2003 MODULE_LICENSE("GPL");
2004 MODULE_AUTHOR("David Brownell");