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 */
14 #include <linux/kallsyms.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/utsname.h>
21 #include <linux/usb/composite.h>
22 #include <asm/unaligned.h>
24 #include "u_os_desc.h"
27 * struct usb_os_string - represents OS String to be reported by a gadget
28 * @bLength: total length of the entire descritor, always 0x12
29 * @bDescriptorType: USB_DT_STRING
30 * @qwSignature: the OS String proper
31 * @bMS_VendorCode: code used by the host for subsequent requests
32 * @bPad: not used, must be zero
34 struct usb_os_string
{
37 __u8 qwSignature
[OS_STRING_QW_SIGN_LEN
];
43 * The code in this file is utility code, used to build a gadget driver
44 * from one or more "function" drivers, one or more "configuration"
45 * objects, and a "usb_composite_driver" by gluing them together along
46 * with the relevant device-wide data.
49 static struct usb_gadget_strings
**get_containers_gs(
50 struct usb_gadget_string_container
*uc
)
52 return (struct usb_gadget_strings
**)uc
->stash
;
56 * next_ep_desc() - advance to the next EP descriptor
57 * @t: currect pointer within descriptor array
59 * Return: next EP descriptor or NULL
61 * Iterate over @t until either EP descriptor found or
62 * NULL (that indicates end of list) encountered
64 static struct usb_descriptor_header
**
65 next_ep_desc(struct usb_descriptor_header
**t
)
68 if ((*t
)->bDescriptorType
== USB_DT_ENDPOINT
)
75 * for_each_ep_desc()- iterate over endpoint descriptors in the
77 * @start: pointer within descriptor array.
78 * @ep_desc: endpoint descriptor to use as the loop cursor
80 #define for_each_ep_desc(start, ep_desc) \
81 for (ep_desc = next_ep_desc(start); \
82 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
85 * config_ep_by_speed() - configures the given endpoint
86 * according to gadget speed.
87 * @g: pointer to the gadget
89 * @_ep: the endpoint to configure
91 * Return: error code, 0 on success
93 * This function chooses the right descriptors for a given
94 * endpoint according to gadget speed and saves it in the
95 * endpoint desc field. If the endpoint already has a descriptor
96 * assigned to it - overwrites it with currently corresponding
97 * descriptor. The endpoint maxpacket field is updated according
98 * to the chosen descriptor.
99 * Note: the supplied function should hold all the descriptors
100 * for supported speeds
102 int config_ep_by_speed(struct usb_gadget
*g
,
103 struct usb_function
*f
,
106 struct usb_composite_dev
*cdev
= get_gadget_data(g
);
107 struct usb_endpoint_descriptor
*chosen_desc
= NULL
;
108 struct usb_descriptor_header
**speed_desc
= NULL
;
110 struct usb_ss_ep_comp_descriptor
*comp_desc
= NULL
;
111 int want_comp_desc
= 0;
113 struct usb_descriptor_header
**d_spd
; /* cursor for speed desc */
115 if (!g
|| !f
|| !_ep
)
118 /* select desired speed */
120 case USB_SPEED_SUPER
:
121 if (gadget_is_superspeed(g
)) {
122 speed_desc
= f
->ss_descriptors
;
126 /* else: Fall trough */
128 if (gadget_is_dualspeed(g
)) {
129 speed_desc
= f
->hs_descriptors
;
132 /* else: fall through */
134 speed_desc
= f
->fs_descriptors
;
136 /* find descriptors */
137 for_each_ep_desc(speed_desc
, d_spd
) {
138 chosen_desc
= (struct usb_endpoint_descriptor
*)*d_spd
;
139 if (chosen_desc
->bEndpointAddress
== _ep
->address
)
146 _ep
->maxpacket
= usb_endpoint_maxp(chosen_desc
);
147 _ep
->desc
= chosen_desc
;
148 _ep
->comp_desc
= NULL
;
155 * Companion descriptor should follow EP descriptor
156 * USB 3.0 spec, #9.6.7
158 comp_desc
= (struct usb_ss_ep_comp_descriptor
*)*(++d_spd
);
160 (comp_desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
))
162 _ep
->comp_desc
= comp_desc
;
163 if (g
->speed
== USB_SPEED_SUPER
) {
164 switch (usb_endpoint_type(_ep
->desc
)) {
165 case USB_ENDPOINT_XFER_ISOC
:
166 /* mult: bits 1:0 of bmAttributes */
167 _ep
->mult
= comp_desc
->bmAttributes
& 0x3;
168 case USB_ENDPOINT_XFER_BULK
:
169 case USB_ENDPOINT_XFER_INT
:
170 _ep
->maxburst
= comp_desc
->bMaxBurst
+ 1;
173 if (comp_desc
->bMaxBurst
!= 0)
174 ERROR(cdev
, "ep0 bMaxBurst must be 0\n");
181 EXPORT_SYMBOL_GPL(config_ep_by_speed
);
184 * usb_add_function() - add a function to a configuration
185 * @config: the configuration
186 * @function: the function being added
187 * Context: single threaded during gadget setup
189 * After initialization, each configuration must have one or more
190 * functions added to it. Adding a function involves calling its @bind()
191 * method to allocate resources such as interface and string identifiers
194 * This function returns the value of the function's bind(), which is
195 * zero for success else a negative errno value.
197 int usb_add_function(struct usb_configuration
*config
,
198 struct usb_function
*function
)
202 DBG(config
->cdev
, "adding '%s'/%p to config '%s'/%p\n",
203 function
->name
, function
,
204 config
->label
, config
);
206 if (!function
->set_alt
|| !function
->disable
)
209 function
->config
= config
;
210 list_add_tail(&function
->list
, &config
->functions
);
212 /* REVISIT *require* function->bind? */
213 if (function
->bind
) {
214 value
= function
->bind(config
, function
);
216 list_del(&function
->list
);
217 function
->config
= NULL
;
222 /* We allow configurations that don't work at both speeds.
223 * If we run into a lowspeed Linux system, treat it the same
224 * as full speed ... it's the function drivers that will need
225 * to avoid bulk and ISO transfers.
227 if (!config
->fullspeed
&& function
->fs_descriptors
)
228 config
->fullspeed
= true;
229 if (!config
->highspeed
&& function
->hs_descriptors
)
230 config
->highspeed
= true;
231 if (!config
->superspeed
&& function
->ss_descriptors
)
232 config
->superspeed
= true;
236 DBG(config
->cdev
, "adding '%s'/%p --> %d\n",
237 function
->name
, function
, value
);
240 EXPORT_SYMBOL_GPL(usb_add_function
);
242 void usb_remove_function(struct usb_configuration
*c
, struct usb_function
*f
)
247 bitmap_zero(f
->endpoints
, 32);
252 EXPORT_SYMBOL_GPL(usb_remove_function
);
255 * usb_function_deactivate - prevent function and gadget enumeration
256 * @function: the function that isn't yet ready to respond
258 * Blocks response of the gadget driver to host enumeration by
259 * preventing the data line pullup from being activated. This is
260 * normally called during @bind() processing to change from the
261 * initial "ready to respond" state, or when a required resource
264 * For example, drivers that serve as a passthrough to a userspace
265 * daemon can block enumeration unless that daemon (such as an OBEX,
266 * MTP, or print server) is ready to handle host requests.
268 * Not all systems support software control of their USB peripheral
271 * Returns zero on success, else negative errno.
273 int usb_function_deactivate(struct usb_function
*function
)
275 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
279 spin_lock_irqsave(&cdev
->lock
, flags
);
281 if (cdev
->deactivations
== 0)
282 status
= usb_gadget_disconnect(cdev
->gadget
);
284 cdev
->deactivations
++;
286 spin_unlock_irqrestore(&cdev
->lock
, flags
);
289 EXPORT_SYMBOL_GPL(usb_function_deactivate
);
292 * usb_function_activate - allow function and gadget enumeration
293 * @function: function on which usb_function_activate() was called
295 * Reverses effect of usb_function_deactivate(). If no more functions
296 * are delaying their activation, the gadget driver will respond to
297 * host enumeration procedures.
299 * Returns zero on success, else negative errno.
301 int usb_function_activate(struct usb_function
*function
)
303 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
307 spin_lock_irqsave(&cdev
->lock
, flags
);
309 if (WARN_ON(cdev
->deactivations
== 0))
312 cdev
->deactivations
--;
313 if (cdev
->deactivations
== 0)
314 status
= usb_gadget_connect(cdev
->gadget
);
317 spin_unlock_irqrestore(&cdev
->lock
, flags
);
320 EXPORT_SYMBOL_GPL(usb_function_activate
);
323 * usb_interface_id() - allocate an unused interface ID
324 * @config: configuration associated with the interface
325 * @function: function handling the interface
326 * Context: single threaded during gadget setup
328 * usb_interface_id() is called from usb_function.bind() callbacks to
329 * allocate new interface IDs. The function driver will then store that
330 * ID in interface, association, CDC union, and other descriptors. It
331 * will also handle any control requests targeted at that interface,
332 * particularly changing its altsetting via set_alt(). There may
333 * also be class-specific or vendor-specific requests to handle.
335 * All interface identifier should be allocated using this routine, to
336 * ensure that for example different functions don't wrongly assign
337 * different meanings to the same identifier. Note that since interface
338 * identifiers are configuration-specific, functions used in more than
339 * one configuration (or more than once in a given configuration) need
340 * multiple versions of the relevant descriptors.
342 * Returns the interface ID which was allocated; or -ENODEV if no
343 * more interface IDs can be allocated.
345 int usb_interface_id(struct usb_configuration
*config
,
346 struct usb_function
*function
)
348 unsigned id
= config
->next_interface_id
;
350 if (id
< MAX_CONFIG_INTERFACES
) {
351 config
->interface
[id
] = function
;
352 config
->next_interface_id
= id
+ 1;
357 EXPORT_SYMBOL_GPL(usb_interface_id
);
359 static u8
encode_bMaxPower(enum usb_device_speed speed
,
360 struct usb_configuration
*c
)
367 val
= CONFIG_USB_GADGET_VBUS_DRAW
;
371 case USB_SPEED_SUPER
:
372 return DIV_ROUND_UP(val
, 8);
374 return DIV_ROUND_UP(val
, 2);
378 static int config_buf(struct usb_configuration
*config
,
379 enum usb_device_speed speed
, void *buf
, u8 type
)
381 struct usb_config_descriptor
*c
= buf
;
382 void *next
= buf
+ USB_DT_CONFIG_SIZE
;
384 struct usb_function
*f
;
387 len
= USB_COMP_EP0_BUFSIZ
- USB_DT_CONFIG_SIZE
;
388 /* write the config descriptor */
390 c
->bLength
= USB_DT_CONFIG_SIZE
;
391 c
->bDescriptorType
= type
;
392 /* wTotalLength is written later */
393 c
->bNumInterfaces
= config
->next_interface_id
;
394 c
->bConfigurationValue
= config
->bConfigurationValue
;
395 c
->iConfiguration
= config
->iConfiguration
;
396 c
->bmAttributes
= USB_CONFIG_ATT_ONE
| config
->bmAttributes
;
397 c
->bMaxPower
= encode_bMaxPower(speed
, config
);
399 /* There may be e.g. OTG descriptors */
400 if (config
->descriptors
) {
401 status
= usb_descriptor_fillbuf(next
, len
,
402 config
->descriptors
);
409 /* add each function's descriptors */
410 list_for_each_entry(f
, &config
->functions
, list
) {
411 struct usb_descriptor_header
**descriptors
;
414 case USB_SPEED_SUPER
:
415 descriptors
= f
->ss_descriptors
;
418 descriptors
= f
->hs_descriptors
;
421 descriptors
= f
->fs_descriptors
;
426 status
= usb_descriptor_fillbuf(next
, len
,
427 (const struct usb_descriptor_header
**) descriptors
);
435 c
->wTotalLength
= cpu_to_le16(len
);
439 static int config_desc(struct usb_composite_dev
*cdev
, unsigned w_value
)
441 struct usb_gadget
*gadget
= cdev
->gadget
;
442 struct usb_configuration
*c
;
443 struct list_head
*pos
;
444 u8 type
= w_value
>> 8;
445 enum usb_device_speed speed
= USB_SPEED_UNKNOWN
;
447 if (gadget
->speed
== USB_SPEED_SUPER
)
448 speed
= gadget
->speed
;
449 else if (gadget_is_dualspeed(gadget
)) {
451 if (gadget
->speed
== USB_SPEED_HIGH
)
453 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
456 speed
= USB_SPEED_HIGH
;
460 /* This is a lookup by config *INDEX* */
463 pos
= &cdev
->configs
;
464 c
= cdev
->os_desc_config
;
468 while ((pos
= pos
->next
) != &cdev
->configs
) {
469 c
= list_entry(pos
, typeof(*c
), list
);
471 /* skip OS Descriptors config which is handled separately */
472 if (c
== cdev
->os_desc_config
)
476 /* ignore configs that won't work at this speed */
478 case USB_SPEED_SUPER
:
492 return config_buf(c
, speed
, cdev
->req
->buf
, type
);
498 static int count_configs(struct usb_composite_dev
*cdev
, unsigned type
)
500 struct usb_gadget
*gadget
= cdev
->gadget
;
501 struct usb_configuration
*c
;
506 if (gadget_is_dualspeed(gadget
)) {
507 if (gadget
->speed
== USB_SPEED_HIGH
)
509 if (gadget
->speed
== USB_SPEED_SUPER
)
511 if (type
== USB_DT_DEVICE_QUALIFIER
)
514 list_for_each_entry(c
, &cdev
->configs
, list
) {
515 /* ignore configs that won't work at this speed */
532 * bos_desc() - prepares the BOS descriptor.
533 * @cdev: pointer to usb_composite device to generate the bos
536 * This function generates the BOS (Binary Device Object)
537 * descriptor and its device capabilities descriptors. The BOS
538 * descriptor should be supported by a SuperSpeed device.
540 static int bos_desc(struct usb_composite_dev
*cdev
)
542 struct usb_ext_cap_descriptor
*usb_ext
;
543 struct usb_ss_cap_descriptor
*ss_cap
;
544 struct usb_dcd_config_params dcd_config_params
;
545 struct usb_bos_descriptor
*bos
= cdev
->req
->buf
;
547 bos
->bLength
= USB_DT_BOS_SIZE
;
548 bos
->bDescriptorType
= USB_DT_BOS
;
550 bos
->wTotalLength
= cpu_to_le16(USB_DT_BOS_SIZE
);
551 bos
->bNumDeviceCaps
= 0;
554 * A SuperSpeed device shall include the USB2.0 extension descriptor
555 * and shall support LPM when operating in USB2.0 HS mode.
557 usb_ext
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
558 bos
->bNumDeviceCaps
++;
559 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_EXT_CAP_SIZE
);
560 usb_ext
->bLength
= USB_DT_USB_EXT_CAP_SIZE
;
561 usb_ext
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
562 usb_ext
->bDevCapabilityType
= USB_CAP_TYPE_EXT
;
563 usb_ext
->bmAttributes
= cpu_to_le32(USB_LPM_SUPPORT
);
566 * The Superspeed USB Capability descriptor shall be implemented by all
567 * SuperSpeed devices.
569 ss_cap
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
570 bos
->bNumDeviceCaps
++;
571 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_SS_CAP_SIZE
);
572 ss_cap
->bLength
= USB_DT_USB_SS_CAP_SIZE
;
573 ss_cap
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
574 ss_cap
->bDevCapabilityType
= USB_SS_CAP_TYPE
;
575 ss_cap
->bmAttributes
= 0; /* LTM is not supported yet */
576 ss_cap
->wSpeedSupported
= cpu_to_le16(USB_LOW_SPEED_OPERATION
|
577 USB_FULL_SPEED_OPERATION
|
578 USB_HIGH_SPEED_OPERATION
|
579 USB_5GBPS_OPERATION
);
580 ss_cap
->bFunctionalitySupport
= USB_LOW_SPEED_OPERATION
;
582 /* Get Controller configuration */
583 if (cdev
->gadget
->ops
->get_config_params
)
584 cdev
->gadget
->ops
->get_config_params(&dcd_config_params
);
586 dcd_config_params
.bU1devExitLat
= USB_DEFAULT_U1_DEV_EXIT_LAT
;
587 dcd_config_params
.bU2DevExitLat
=
588 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT
);
590 ss_cap
->bU1devExitLat
= dcd_config_params
.bU1devExitLat
;
591 ss_cap
->bU2DevExitLat
= dcd_config_params
.bU2DevExitLat
;
593 return le16_to_cpu(bos
->wTotalLength
);
596 static void device_qual(struct usb_composite_dev
*cdev
)
598 struct usb_qualifier_descriptor
*qual
= cdev
->req
->buf
;
600 qual
->bLength
= sizeof(*qual
);
601 qual
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
602 /* POLICY: same bcdUSB and device type info at both speeds */
603 qual
->bcdUSB
= cdev
->desc
.bcdUSB
;
604 qual
->bDeviceClass
= cdev
->desc
.bDeviceClass
;
605 qual
->bDeviceSubClass
= cdev
->desc
.bDeviceSubClass
;
606 qual
->bDeviceProtocol
= cdev
->desc
.bDeviceProtocol
;
607 /* ASSUME same EP0 fifo size at both speeds */
608 qual
->bMaxPacketSize0
= cdev
->gadget
->ep0
->maxpacket
;
609 qual
->bNumConfigurations
= count_configs(cdev
, USB_DT_DEVICE_QUALIFIER
);
613 /*-------------------------------------------------------------------------*/
615 static void reset_config(struct usb_composite_dev
*cdev
)
617 struct usb_function
*f
;
619 DBG(cdev
, "reset config\n");
621 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
625 bitmap_zero(f
->endpoints
, 32);
628 cdev
->delayed_status
= 0;
631 static int set_config(struct usb_composite_dev
*cdev
,
632 const struct usb_ctrlrequest
*ctrl
, unsigned number
)
634 struct usb_gadget
*gadget
= cdev
->gadget
;
635 struct usb_configuration
*c
= NULL
;
636 int result
= -EINVAL
;
637 unsigned power
= gadget_is_otg(gadget
) ? 8 : 100;
641 list_for_each_entry(c
, &cdev
->configs
, list
) {
642 if (c
->bConfigurationValue
== number
) {
644 * We disable the FDs of the previous
645 * configuration only if the new configuration
656 } else { /* Zero configuration value - need to reset the config */
662 INFO(cdev
, "%s config #%d: %s\n",
663 usb_speed_string(gadget
->speed
),
664 number
, c
? c
->label
: "unconfigured");
669 usb_gadget_set_state(gadget
, USB_STATE_CONFIGURED
);
672 /* Initialize all interfaces by setting them to altsetting zero. */
673 for (tmp
= 0; tmp
< MAX_CONFIG_INTERFACES
; tmp
++) {
674 struct usb_function
*f
= c
->interface
[tmp
];
675 struct usb_descriptor_header
**descriptors
;
681 * Record which endpoints are used by the function. This is used
682 * to dispatch control requests targeted at that endpoint to the
683 * function's setup callback instead of the current
684 * configuration's setup callback.
686 switch (gadget
->speed
) {
687 case USB_SPEED_SUPER
:
688 descriptors
= f
->ss_descriptors
;
691 descriptors
= f
->hs_descriptors
;
694 descriptors
= f
->fs_descriptors
;
697 for (; *descriptors
; ++descriptors
) {
698 struct usb_endpoint_descriptor
*ep
;
701 if ((*descriptors
)->bDescriptorType
!= USB_DT_ENDPOINT
)
704 ep
= (struct usb_endpoint_descriptor
*)*descriptors
;
705 addr
= ((ep
->bEndpointAddress
& 0x80) >> 3)
706 | (ep
->bEndpointAddress
& 0x0f);
707 set_bit(addr
, f
->endpoints
);
710 result
= f
->set_alt(f
, tmp
, 0);
712 DBG(cdev
, "interface %d (%s/%p) alt 0 --> %d\n",
713 tmp
, f
->name
, f
, result
);
719 if (result
== USB_GADGET_DELAYED_STATUS
) {
721 "%s: interface %d (%s) requested delayed status\n",
722 __func__
, tmp
, f
->name
);
723 cdev
->delayed_status
++;
724 DBG(cdev
, "delayed_status count %d\n",
725 cdev
->delayed_status
);
729 /* when we return, be sure our power usage is valid */
730 power
= c
->MaxPower
? c
->MaxPower
: CONFIG_USB_GADGET_VBUS_DRAW
;
732 usb_gadget_vbus_draw(gadget
, power
);
733 if (result
>= 0 && cdev
->delayed_status
)
734 result
= USB_GADGET_DELAYED_STATUS
;
738 int usb_add_config_only(struct usb_composite_dev
*cdev
,
739 struct usb_configuration
*config
)
741 struct usb_configuration
*c
;
743 if (!config
->bConfigurationValue
)
746 /* Prevent duplicate configuration identifiers */
747 list_for_each_entry(c
, &cdev
->configs
, list
) {
748 if (c
->bConfigurationValue
== config
->bConfigurationValue
)
753 list_add_tail(&config
->list
, &cdev
->configs
);
755 INIT_LIST_HEAD(&config
->functions
);
756 config
->next_interface_id
= 0;
757 memset(config
->interface
, 0, sizeof(config
->interface
));
761 EXPORT_SYMBOL_GPL(usb_add_config_only
);
764 * usb_add_config() - add a configuration to a device.
765 * @cdev: wraps the USB gadget
766 * @config: the configuration, with bConfigurationValue assigned
767 * @bind: the configuration's bind function
768 * Context: single threaded during gadget setup
770 * One of the main tasks of a composite @bind() routine is to
771 * add each of the configurations it supports, using this routine.
773 * This function returns the value of the configuration's @bind(), which
774 * is zero for success else a negative errno value. Binding configurations
775 * assigns global resources including string IDs, and per-configuration
776 * resources such as interface IDs and endpoints.
778 int usb_add_config(struct usb_composite_dev
*cdev
,
779 struct usb_configuration
*config
,
780 int (*bind
)(struct usb_configuration
*))
782 int status
= -EINVAL
;
787 DBG(cdev
, "adding config #%u '%s'/%p\n",
788 config
->bConfigurationValue
,
789 config
->label
, config
);
791 status
= usb_add_config_only(cdev
, config
);
795 status
= bind(config
);
797 while (!list_empty(&config
->functions
)) {
798 struct usb_function
*f
;
800 f
= list_first_entry(&config
->functions
,
801 struct usb_function
, list
);
804 DBG(cdev
, "unbind function '%s'/%p\n",
806 f
->unbind(config
, f
);
807 /* may free memory for "f" */
810 list_del(&config
->list
);
815 DBG(cdev
, "cfg %d/%p speeds:%s%s%s\n",
816 config
->bConfigurationValue
, config
,
817 config
->superspeed
? " super" : "",
818 config
->highspeed
? " high" : "",
820 ? (gadget_is_dualspeed(cdev
->gadget
)
825 for (i
= 0; i
< MAX_CONFIG_INTERFACES
; i
++) {
826 struct usb_function
*f
= config
->interface
[i
];
830 DBG(cdev
, " interface %d = %s/%p\n",
835 /* set_alt(), or next bind(), sets up
836 * ep->driver_data as needed.
838 usb_ep_autoconfig_reset(cdev
->gadget
);
842 DBG(cdev
, "added config '%s'/%u --> %d\n", config
->label
,
843 config
->bConfigurationValue
, status
);
846 EXPORT_SYMBOL_GPL(usb_add_config
);
848 static void remove_config(struct usb_composite_dev
*cdev
,
849 struct usb_configuration
*config
)
851 while (!list_empty(&config
->functions
)) {
852 struct usb_function
*f
;
854 f
= list_first_entry(&config
->functions
,
855 struct usb_function
, list
);
858 DBG(cdev
, "unbind function '%s'/%p\n", f
->name
, f
);
859 f
->unbind(config
, f
);
860 /* may free memory for "f" */
863 list_del(&config
->list
);
864 if (config
->unbind
) {
865 DBG(cdev
, "unbind config '%s'/%p\n", config
->label
, config
);
866 config
->unbind(config
);
867 /* may free memory for "c" */
872 * usb_remove_config() - remove a configuration from a device.
873 * @cdev: wraps the USB gadget
874 * @config: the configuration
876 * Drivers must call usb_gadget_disconnect before calling this function
877 * to disconnect the device from the host and make sure the host will not
878 * try to enumerate the device while we are changing the config list.
880 void usb_remove_config(struct usb_composite_dev
*cdev
,
881 struct usb_configuration
*config
)
885 spin_lock_irqsave(&cdev
->lock
, flags
);
887 if (cdev
->config
== config
)
890 spin_unlock_irqrestore(&cdev
->lock
, flags
);
892 remove_config(cdev
, config
);
895 /*-------------------------------------------------------------------------*/
897 /* We support strings in multiple languages ... string descriptor zero
898 * says which languages are supported. The typical case will be that
899 * only one language (probably English) is used, with I18N handled on
903 static void collect_langs(struct usb_gadget_strings
**sp
, __le16
*buf
)
905 const struct usb_gadget_strings
*s
;
911 language
= cpu_to_le16(s
->language
);
912 for (tmp
= buf
; *tmp
&& tmp
< &buf
[126]; tmp
++) {
913 if (*tmp
== language
)
922 static int lookup_string(
923 struct usb_gadget_strings
**sp
,
929 struct usb_gadget_strings
*s
;
934 if (s
->language
!= language
)
936 value
= usb_gadget_get_string(s
, id
, buf
);
943 static int get_string(struct usb_composite_dev
*cdev
,
944 void *buf
, u16 language
, int id
)
946 struct usb_composite_driver
*composite
= cdev
->driver
;
947 struct usb_gadget_string_container
*uc
;
948 struct usb_configuration
*c
;
949 struct usb_function
*f
;
952 /* Yes, not only is USB's I18N support probably more than most
953 * folk will ever care about ... also, it's all supported here.
954 * (Except for UTF8 support for Unicode's "Astral Planes".)
957 /* 0 == report all available language codes */
959 struct usb_string_descriptor
*s
= buf
;
960 struct usb_gadget_strings
**sp
;
963 s
->bDescriptorType
= USB_DT_STRING
;
965 sp
= composite
->strings
;
967 collect_langs(sp
, s
->wData
);
969 list_for_each_entry(c
, &cdev
->configs
, list
) {
972 collect_langs(sp
, s
->wData
);
974 list_for_each_entry(f
, &c
->functions
, list
) {
977 collect_langs(sp
, s
->wData
);
980 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
981 struct usb_gadget_strings
**sp
;
983 sp
= get_containers_gs(uc
);
984 collect_langs(sp
, s
->wData
);
987 for (len
= 0; len
<= 126 && s
->wData
[len
]; len
++)
992 s
->bLength
= 2 * (len
+ 1);
996 if (cdev
->use_os_string
&& language
== 0 && id
== OS_STRING_IDX
) {
997 struct usb_os_string
*b
= buf
;
998 b
->bLength
= sizeof(*b
);
999 b
->bDescriptorType
= USB_DT_STRING
;
1001 sizeof(b
->qwSignature
) == sizeof(cdev
->qw_sign
),
1002 "qwSignature size must be equal to qw_sign");
1003 memcpy(&b
->qwSignature
, cdev
->qw_sign
, sizeof(b
->qwSignature
));
1004 b
->bMS_VendorCode
= cdev
->b_vendor_code
;
1009 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
1010 struct usb_gadget_strings
**sp
;
1012 sp
= get_containers_gs(uc
);
1013 len
= lookup_string(sp
, buf
, language
, id
);
1018 /* String IDs are device-scoped, so we look up each string
1019 * table we're told about. These lookups are infrequent;
1020 * simpler-is-better here.
1022 if (composite
->strings
) {
1023 len
= lookup_string(composite
->strings
, buf
, language
, id
);
1027 list_for_each_entry(c
, &cdev
->configs
, list
) {
1029 len
= lookup_string(c
->strings
, buf
, language
, id
);
1033 list_for_each_entry(f
, &c
->functions
, list
) {
1036 len
= lookup_string(f
->strings
, buf
, language
, id
);
1045 * usb_string_id() - allocate an unused string ID
1046 * @cdev: the device whose string descriptor IDs are being allocated
1047 * Context: single threaded during gadget setup
1049 * @usb_string_id() is called from bind() callbacks to allocate
1050 * string IDs. Drivers for functions, configurations, or gadgets will
1051 * then store that ID in the appropriate descriptors and string table.
1053 * All string identifier should be allocated using this,
1054 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1055 * that for example different functions don't wrongly assign different
1056 * meanings to the same identifier.
1058 int usb_string_id(struct usb_composite_dev
*cdev
)
1060 if (cdev
->next_string_id
< 254) {
1061 /* string id 0 is reserved by USB spec for list of
1062 * supported languages */
1063 /* 255 reserved as well? -- mina86 */
1064 cdev
->next_string_id
++;
1065 return cdev
->next_string_id
;
1069 EXPORT_SYMBOL_GPL(usb_string_id
);
1072 * usb_string_ids() - allocate unused string IDs in batch
1073 * @cdev: the device whose string descriptor IDs are being allocated
1074 * @str: an array of usb_string objects to assign numbers to
1075 * Context: single threaded during gadget setup
1077 * @usb_string_ids() is called from bind() callbacks to allocate
1078 * string IDs. Drivers for functions, configurations, or gadgets will
1079 * then copy IDs from the string table to the appropriate descriptors
1080 * and string table for other languages.
1082 * All string identifier should be allocated using this,
1083 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1084 * example different functions don't wrongly assign different meanings
1085 * to the same identifier.
1087 int usb_string_ids_tab(struct usb_composite_dev
*cdev
, struct usb_string
*str
)
1089 int next
= cdev
->next_string_id
;
1091 for (; str
->s
; ++str
) {
1092 if (unlikely(next
>= 254))
1097 cdev
->next_string_id
= next
;
1101 EXPORT_SYMBOL_GPL(usb_string_ids_tab
);
1103 static struct usb_gadget_string_container
*copy_gadget_strings(
1104 struct usb_gadget_strings
**sp
, unsigned n_gstrings
,
1107 struct usb_gadget_string_container
*uc
;
1108 struct usb_gadget_strings
**gs_array
;
1109 struct usb_gadget_strings
*gs
;
1110 struct usb_string
*s
;
1117 mem
+= sizeof(void *) * (n_gstrings
+ 1);
1118 mem
+= sizeof(struct usb_gadget_strings
) * n_gstrings
;
1119 mem
+= sizeof(struct usb_string
) * (n_strings
+ 1) * (n_gstrings
);
1120 uc
= kmalloc(mem
, GFP_KERNEL
);
1122 return ERR_PTR(-ENOMEM
);
1123 gs_array
= get_containers_gs(uc
);
1125 stash
+= sizeof(void *) * (n_gstrings
+ 1);
1126 for (n_gs
= 0; n_gs
< n_gstrings
; n_gs
++) {
1127 struct usb_string
*org_s
;
1129 gs_array
[n_gs
] = stash
;
1130 gs
= gs_array
[n_gs
];
1131 stash
+= sizeof(struct usb_gadget_strings
);
1132 gs
->language
= sp
[n_gs
]->language
;
1133 gs
->strings
= stash
;
1134 org_s
= sp
[n_gs
]->strings
;
1136 for (n_s
= 0; n_s
< n_strings
; n_s
++) {
1138 stash
+= sizeof(struct usb_string
);
1147 stash
+= sizeof(struct usb_string
);
1150 gs_array
[n_gs
] = NULL
;
1155 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1156 * @cdev: the device whose string descriptor IDs are being allocated
1158 * @sp: an array of usb_gadget_strings to attach.
1159 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1161 * This function will create a deep copy of usb_gadget_strings and usb_string
1162 * and attach it to the cdev. The actual string (usb_string.s) will not be
1163 * copied but only a referenced will be made. The struct usb_gadget_strings
1164 * array may contain multiple languges and should be NULL terminated.
1165 * The ->language pointer of each struct usb_gadget_strings has to contain the
1166 * same amount of entries.
1167 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1168 * usb_string entry of es-ES containts the translation of the first usb_string
1169 * entry of en-US. Therefore both entries become the same id assign.
1171 struct usb_string
*usb_gstrings_attach(struct usb_composite_dev
*cdev
,
1172 struct usb_gadget_strings
**sp
, unsigned n_strings
)
1174 struct usb_gadget_string_container
*uc
;
1175 struct usb_gadget_strings
**n_gs
;
1176 unsigned n_gstrings
= 0;
1180 for (i
= 0; sp
[i
]; i
++)
1184 return ERR_PTR(-EINVAL
);
1186 uc
= copy_gadget_strings(sp
, n_gstrings
, n_strings
);
1188 return ERR_CAST(uc
);
1190 n_gs
= get_containers_gs(uc
);
1191 ret
= usb_string_ids_tab(cdev
, n_gs
[0]->strings
);
1195 for (i
= 1; i
< n_gstrings
; i
++) {
1196 struct usb_string
*m_s
;
1197 struct usb_string
*s
;
1200 m_s
= n_gs
[0]->strings
;
1201 s
= n_gs
[i
]->strings
;
1202 for (n
= 0; n
< n_strings
; n
++) {
1208 list_add_tail(&uc
->list
, &cdev
->gstrings
);
1209 return n_gs
[0]->strings
;
1212 return ERR_PTR(ret
);
1214 EXPORT_SYMBOL_GPL(usb_gstrings_attach
);
1217 * usb_string_ids_n() - allocate unused string IDs in batch
1218 * @c: the device whose string descriptor IDs are being allocated
1219 * @n: number of string IDs to allocate
1220 * Context: single threaded during gadget setup
1222 * Returns the first requested ID. This ID and next @n-1 IDs are now
1223 * valid IDs. At least provided that @n is non-zero because if it
1224 * is, returns last requested ID which is now very useful information.
1226 * @usb_string_ids_n() is called from bind() callbacks to allocate
1227 * string IDs. Drivers for functions, configurations, or gadgets will
1228 * then store that ID in the appropriate descriptors and string table.
1230 * All string identifier should be allocated using this,
1231 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1232 * example different functions don't wrongly assign different meanings
1233 * to the same identifier.
1235 int usb_string_ids_n(struct usb_composite_dev
*c
, unsigned n
)
1237 unsigned next
= c
->next_string_id
;
1238 if (unlikely(n
> 254 || (unsigned)next
+ n
> 254))
1240 c
->next_string_id
+= n
;
1243 EXPORT_SYMBOL_GPL(usb_string_ids_n
);
1245 /*-------------------------------------------------------------------------*/
1247 static void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1249 if (req
->status
|| req
->actual
!= req
->length
)
1250 DBG((struct usb_composite_dev
*) ep
->driver_data
,
1251 "setup complete --> %d, %d/%d\n",
1252 req
->status
, req
->actual
, req
->length
);
1255 static int count_ext_compat(struct usb_configuration
*c
)
1260 for (i
= 0; i
< c
->next_interface_id
; ++i
) {
1261 struct usb_function
*f
;
1264 f
= c
->interface
[i
];
1265 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1266 struct usb_os_desc
*d
;
1268 if (i
!= f
->os_desc_table
[j
].if_id
)
1270 d
= f
->os_desc_table
[j
].os_desc
;
1271 if (d
&& d
->ext_compat_id
)
1279 static void fill_ext_compat(struct usb_configuration
*c
, u8
*buf
)
1284 for (i
= 0; i
< c
->next_interface_id
; ++i
) {
1285 struct usb_function
*f
;
1288 f
= c
->interface
[i
];
1289 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1290 struct usb_os_desc
*d
;
1292 if (i
!= f
->os_desc_table
[j
].if_id
)
1294 d
= f
->os_desc_table
[j
].os_desc
;
1295 if (d
&& d
->ext_compat_id
) {
1298 memcpy(buf
, d
->ext_compat_id
, 16);
1312 static int count_ext_prop(struct usb_configuration
*c
, int interface
)
1314 struct usb_function
*f
;
1317 f
= c
->interface
[interface
];
1318 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1319 struct usb_os_desc
*d
;
1321 if (interface
!= f
->os_desc_table
[j
].if_id
)
1323 d
= f
->os_desc_table
[j
].os_desc
;
1324 if (d
&& d
->ext_compat_id
)
1325 return d
->ext_prop_count
;
1330 static int len_ext_prop(struct usb_configuration
*c
, int interface
)
1332 struct usb_function
*f
;
1333 struct usb_os_desc
*d
;
1336 res
= 10; /* header length */
1337 f
= c
->interface
[interface
];
1338 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1339 if (interface
!= f
->os_desc_table
[j
].if_id
)
1341 d
= f
->os_desc_table
[j
].os_desc
;
1343 return min(res
+ d
->ext_prop_len
, 4096);
1348 static int fill_ext_prop(struct usb_configuration
*c
, int interface
, u8
*buf
)
1350 struct usb_function
*f
;
1351 struct usb_os_desc
*d
;
1352 struct usb_os_desc_ext_prop
*ext_prop
;
1353 int j
, count
, n
, ret
;
1356 f
= c
->interface
[interface
];
1357 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1358 if (interface
!= f
->os_desc_table
[j
].if_id
)
1360 d
= f
->os_desc_table
[j
].os_desc
;
1362 list_for_each_entry(ext_prop
, &d
->ext_prop
, entry
) {
1363 /* 4kB minus header length */
1368 count
= ext_prop
->data_len
+
1369 ext_prop
->name_len
+ 14;
1370 if (count
> 4086 - n
)
1372 usb_ext_prop_put_size(buf
, count
);
1373 usb_ext_prop_put_type(buf
, ext_prop
->type
);
1374 ret
= usb_ext_prop_put_name(buf
, ext_prop
->name
,
1375 ext_prop
->name_len
);
1378 switch (ext_prop
->type
) {
1379 case USB_EXT_PROP_UNICODE
:
1380 case USB_EXT_PROP_UNICODE_ENV
:
1381 case USB_EXT_PROP_UNICODE_LINK
:
1382 usb_ext_prop_put_unicode(buf
, ret
,
1384 ext_prop
->data_len
);
1386 case USB_EXT_PROP_BINARY
:
1387 usb_ext_prop_put_binary(buf
, ret
,
1389 ext_prop
->data_len
);
1391 case USB_EXT_PROP_LE32
:
1392 /* not implemented */
1393 case USB_EXT_PROP_BE32
:
1394 /* not implemented */
1406 * The setup() callback implements all the ep0 functionality that's
1407 * not handled lower down, in hardware or the hardware driver(like
1408 * device and endpoint feature flags, and their status). It's all
1409 * housekeeping for the gadget function we're implementing. Most of
1410 * the work is in config and function specific setup.
1413 composite_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1415 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1416 struct usb_request
*req
= cdev
->req
;
1417 int value
= -EOPNOTSUPP
;
1419 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1420 u8 intf
= w_index
& 0xFF;
1421 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1422 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1423 struct usb_function
*f
= NULL
;
1426 /* partial re-init of the response message; the function or the
1427 * gadget might need to intercept e.g. a control-OUT completion
1428 * when we delegate to it.
1431 req
->complete
= composite_setup_complete
;
1433 gadget
->ep0
->driver_data
= cdev
;
1435 switch (ctrl
->bRequest
) {
1437 /* we handle all standard USB descriptors */
1438 case USB_REQ_GET_DESCRIPTOR
:
1439 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1441 switch (w_value
>> 8) {
1444 cdev
->desc
.bNumConfigurations
=
1445 count_configs(cdev
, USB_DT_DEVICE
);
1446 cdev
->desc
.bMaxPacketSize0
=
1447 cdev
->gadget
->ep0
->maxpacket
;
1448 if (gadget_is_superspeed(gadget
)) {
1449 if (gadget
->speed
>= USB_SPEED_SUPER
) {
1450 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0300);
1451 cdev
->desc
.bMaxPacketSize0
= 9;
1453 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0210);
1457 value
= min(w_length
, (u16
) sizeof cdev
->desc
);
1458 memcpy(req
->buf
, &cdev
->desc
, value
);
1460 case USB_DT_DEVICE_QUALIFIER
:
1461 if (!gadget_is_dualspeed(gadget
) ||
1462 gadget
->speed
>= USB_SPEED_SUPER
)
1465 value
= min_t(int, w_length
,
1466 sizeof(struct usb_qualifier_descriptor
));
1468 case USB_DT_OTHER_SPEED_CONFIG
:
1469 if (!gadget_is_dualspeed(gadget
) ||
1470 gadget
->speed
>= USB_SPEED_SUPER
)
1474 value
= config_desc(cdev
, w_value
);
1476 value
= min(w_length
, (u16
) value
);
1479 value
= get_string(cdev
, req
->buf
,
1480 w_index
, w_value
& 0xff);
1482 value
= min(w_length
, (u16
) value
);
1485 if (gadget_is_superspeed(gadget
)) {
1486 value
= bos_desc(cdev
);
1487 value
= min(w_length
, (u16
) value
);
1493 /* any number of configs can work */
1494 case USB_REQ_SET_CONFIGURATION
:
1495 if (ctrl
->bRequestType
!= 0)
1497 if (gadget_is_otg(gadget
)) {
1498 if (gadget
->a_hnp_support
)
1499 DBG(cdev
, "HNP available\n");
1500 else if (gadget
->a_alt_hnp_support
)
1501 DBG(cdev
, "HNP on another port\n");
1503 VDBG(cdev
, "HNP inactive\n");
1505 spin_lock(&cdev
->lock
);
1506 value
= set_config(cdev
, ctrl
, w_value
);
1507 spin_unlock(&cdev
->lock
);
1509 case USB_REQ_GET_CONFIGURATION
:
1510 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1513 *(u8
*)req
->buf
= cdev
->config
->bConfigurationValue
;
1515 *(u8
*)req
->buf
= 0;
1516 value
= min(w_length
, (u16
) 1);
1519 /* function drivers must handle get/set altsetting; if there's
1520 * no get() method, we know only altsetting zero works.
1522 case USB_REQ_SET_INTERFACE
:
1523 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
1525 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1527 f
= cdev
->config
->interface
[intf
];
1530 if (w_value
&& !f
->set_alt
)
1532 value
= f
->set_alt(f
, w_index
, w_value
);
1533 if (value
== USB_GADGET_DELAYED_STATUS
) {
1535 "%s: interface %d (%s) requested delayed status\n",
1536 __func__
, intf
, f
->name
);
1537 cdev
->delayed_status
++;
1538 DBG(cdev
, "delayed_status count %d\n",
1539 cdev
->delayed_status
);
1542 case USB_REQ_GET_INTERFACE
:
1543 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1545 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1547 f
= cdev
->config
->interface
[intf
];
1550 /* lots of interfaces only need altsetting zero... */
1551 value
= f
->get_alt
? f
->get_alt(f
, w_index
) : 0;
1554 *((u8
*)req
->buf
) = value
;
1555 value
= min(w_length
, (u16
) 1);
1559 * USB 3.0 additions:
1560 * Function driver should handle get_status request. If such cb
1561 * wasn't supplied we respond with default value = 0
1562 * Note: function driver should supply such cb only for the first
1563 * interface of the function
1565 case USB_REQ_GET_STATUS
:
1566 if (!gadget_is_superspeed(gadget
))
1568 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_RECIP_INTERFACE
))
1570 value
= 2; /* This is the length of the get_status reply */
1571 put_unaligned_le16(0, req
->buf
);
1572 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1574 f
= cdev
->config
->interface
[intf
];
1577 status
= f
->get_status
? f
->get_status(f
) : 0;
1580 put_unaligned_le16(status
& 0x0000ffff, req
->buf
);
1583 * Function drivers should handle SetFeature/ClearFeature
1584 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1585 * only for the first interface of the function
1587 case USB_REQ_CLEAR_FEATURE
:
1588 case USB_REQ_SET_FEATURE
:
1589 if (!gadget_is_superspeed(gadget
))
1591 if (ctrl
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_INTERFACE
))
1594 case USB_INTRF_FUNC_SUSPEND
:
1595 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1597 f
= cdev
->config
->interface
[intf
];
1601 if (f
->func_suspend
)
1602 value
= f
->func_suspend(f
, w_index
>> 8);
1605 "func_suspend() returned error %d\n",
1615 * OS descriptors handling
1617 if (cdev
->use_os_string
&& cdev
->os_desc_config
&&
1618 (ctrl
->bRequest
& USB_TYPE_VENDOR
) &&
1619 ctrl
->bRequest
== cdev
->b_vendor_code
) {
1620 struct usb_request
*req
;
1621 struct usb_configuration
*os_desc_cfg
;
1626 req
= cdev
->os_desc_req
;
1627 req
->complete
= composite_setup_complete
;
1629 os_desc_cfg
= cdev
->os_desc_config
;
1630 memset(buf
, 0, w_length
);
1632 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1633 case USB_RECIP_DEVICE
:
1634 if (w_index
!= 0x4 || (w_value
>> 8))
1637 if (w_length
== 0x10) {
1638 /* Number of ext compat interfaces */
1639 count
= count_ext_compat(os_desc_cfg
);
1641 count
*= 24; /* 24 B/ext compat desc */
1642 count
+= 16; /* header */
1643 put_unaligned_le32(count
, buf
);
1646 /* "extended compatibility ID"s */
1647 count
= count_ext_compat(os_desc_cfg
);
1649 count
*= 24; /* 24 B/ext compat desc */
1650 count
+= 16; /* header */
1651 put_unaligned_le32(count
, buf
);
1653 fill_ext_compat(os_desc_cfg
, buf
);
1657 case USB_RECIP_INTERFACE
:
1658 if (w_index
!= 0x5 || (w_value
>> 8))
1660 interface
= w_value
& 0xFF;
1662 if (w_length
== 0x0A) {
1663 count
= count_ext_prop(os_desc_cfg
,
1665 put_unaligned_le16(count
, buf
+ 8);
1666 count
= len_ext_prop(os_desc_cfg
,
1668 put_unaligned_le32(count
, buf
);
1672 count
= count_ext_prop(os_desc_cfg
,
1674 put_unaligned_le16(count
, buf
+ 8);
1675 count
= len_ext_prop(os_desc_cfg
,
1677 put_unaligned_le32(count
, buf
);
1679 value
= fill_ext_prop(os_desc_cfg
,
1688 req
->length
= value
;
1689 req
->zero
= value
< w_length
;
1690 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
1692 DBG(cdev
, "ep_queue --> %d\n", value
);
1694 composite_setup_complete(gadget
->ep0
, req
);
1700 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1701 ctrl
->bRequestType
, ctrl
->bRequest
,
1702 w_value
, w_index
, w_length
);
1704 /* functions always handle their interfaces and endpoints...
1705 * punt other recipients (other, WUSB, ...) to the current
1706 * configuration code.
1708 * REVISIT it could make sense to let the composite device
1709 * take such requests too, if that's ever needed: to work
1712 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1713 case USB_RECIP_INTERFACE
:
1714 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1716 f
= cdev
->config
->interface
[intf
];
1719 case USB_RECIP_ENDPOINT
:
1720 endp
= ((w_index
& 0x80) >> 3) | (w_index
& 0x0f);
1721 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1722 if (test_bit(endp
, f
->endpoints
))
1725 if (&f
->list
== &cdev
->config
->functions
)
1731 value
= f
->setup(f
, ctrl
);
1733 struct usb_configuration
*c
;
1739 /* try current config's setup */
1741 value
= c
->setup(c
, ctrl
);
1745 /* try the only function in the current config */
1746 if (!list_is_singular(&c
->functions
))
1748 f
= list_first_entry(&c
->functions
, struct usb_function
,
1751 value
= f
->setup(f
, ctrl
);
1757 /* respond with data transfer before status phase? */
1758 if (value
>= 0 && value
!= USB_GADGET_DELAYED_STATUS
) {
1759 req
->length
= value
;
1760 req
->zero
= value
< w_length
;
1761 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
1763 DBG(cdev
, "ep_queue --> %d\n", value
);
1765 composite_setup_complete(gadget
->ep0
, req
);
1767 } else if (value
== USB_GADGET_DELAYED_STATUS
&& w_length
!= 0) {
1769 "%s: Delayed status not supported for w_length != 0",
1774 /* device either stalls (value < 0) or reports success */
1778 void composite_disconnect(struct usb_gadget
*gadget
)
1780 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1781 unsigned long flags
;
1783 /* REVISIT: should we have config and device level
1784 * disconnect callbacks?
1786 spin_lock_irqsave(&cdev
->lock
, flags
);
1789 if (cdev
->driver
->disconnect
)
1790 cdev
->driver
->disconnect(cdev
);
1791 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1794 /*-------------------------------------------------------------------------*/
1796 static ssize_t
suspended_show(struct device
*dev
, struct device_attribute
*attr
,
1799 struct usb_gadget
*gadget
= dev_to_usb_gadget(dev
);
1800 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1802 return sprintf(buf
, "%d\n", cdev
->suspended
);
1804 static DEVICE_ATTR_RO(suspended
);
1806 static void __composite_unbind(struct usb_gadget
*gadget
, bool unbind_driver
)
1808 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1810 /* composite_disconnect() must already have been called
1811 * by the underlying peripheral controller driver!
1812 * so there's no i/o concurrency that could affect the
1813 * state protected by cdev->lock.
1815 WARN_ON(cdev
->config
);
1817 while (!list_empty(&cdev
->configs
)) {
1818 struct usb_configuration
*c
;
1819 c
= list_first_entry(&cdev
->configs
,
1820 struct usb_configuration
, list
);
1821 remove_config(cdev
, c
);
1823 if (cdev
->driver
->unbind
&& unbind_driver
)
1824 cdev
->driver
->unbind(cdev
);
1826 composite_dev_cleanup(cdev
);
1828 kfree(cdev
->def_manufacturer
);
1830 set_gadget_data(gadget
, NULL
);
1833 static void composite_unbind(struct usb_gadget
*gadget
)
1835 __composite_unbind(gadget
, true);
1838 static void update_unchanged_dev_desc(struct usb_device_descriptor
*new,
1839 const struct usb_device_descriptor
*old
)
1849 * these variables may have been set in
1850 * usb_composite_overwrite_options()
1852 idVendor
= new->idVendor
;
1853 idProduct
= new->idProduct
;
1854 bcdDevice
= new->bcdDevice
;
1855 iSerialNumber
= new->iSerialNumber
;
1856 iManufacturer
= new->iManufacturer
;
1857 iProduct
= new->iProduct
;
1861 new->idVendor
= idVendor
;
1863 new->idProduct
= idProduct
;
1865 new->bcdDevice
= bcdDevice
;
1867 new->bcdDevice
= cpu_to_le16(get_default_bcdDevice());
1869 new->iSerialNumber
= iSerialNumber
;
1871 new->iManufacturer
= iManufacturer
;
1873 new->iProduct
= iProduct
;
1876 int composite_dev_prepare(struct usb_composite_driver
*composite
,
1877 struct usb_composite_dev
*cdev
)
1879 struct usb_gadget
*gadget
= cdev
->gadget
;
1882 /* preallocate control response and buffer */
1883 cdev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
1887 cdev
->req
->buf
= kmalloc(USB_COMP_EP0_BUFSIZ
, GFP_KERNEL
);
1888 if (!cdev
->req
->buf
)
1891 ret
= device_create_file(&gadget
->dev
, &dev_attr_suspended
);
1895 cdev
->req
->complete
= composite_setup_complete
;
1896 gadget
->ep0
->driver_data
= cdev
;
1898 cdev
->driver
= composite
;
1901 * As per USB compliance update, a device that is actively drawing
1902 * more than 100mA from USB must report itself as bus-powered in
1903 * the GetStatus(DEVICE) call.
1905 if (CONFIG_USB_GADGET_VBUS_DRAW
<= USB_SELF_POWER_VBUS_MAX_DRAW
)
1906 usb_gadget_set_selfpowered(gadget
);
1908 /* interface and string IDs start at zero via kzalloc.
1909 * we force endpoints to start unassigned; few controller
1910 * drivers will zero ep->driver_data.
1912 usb_ep_autoconfig_reset(gadget
);
1915 kfree(cdev
->req
->buf
);
1917 usb_ep_free_request(gadget
->ep0
, cdev
->req
);
1922 int composite_os_desc_req_prepare(struct usb_composite_dev
*cdev
,
1927 cdev
->os_desc_req
= usb_ep_alloc_request(ep0
, GFP_KERNEL
);
1928 if (!cdev
->os_desc_req
) {
1929 ret
= PTR_ERR(cdev
->os_desc_req
);
1933 /* OS feature descriptor length <= 4kB */
1934 cdev
->os_desc_req
->buf
= kmalloc(4096, GFP_KERNEL
);
1935 if (!cdev
->os_desc_req
->buf
) {
1936 ret
= PTR_ERR(cdev
->os_desc_req
->buf
);
1937 kfree(cdev
->os_desc_req
);
1940 cdev
->os_desc_req
->complete
= composite_setup_complete
;
1945 void composite_dev_cleanup(struct usb_composite_dev
*cdev
)
1947 struct usb_gadget_string_container
*uc
, *tmp
;
1949 list_for_each_entry_safe(uc
, tmp
, &cdev
->gstrings
, list
) {
1950 list_del(&uc
->list
);
1953 if (cdev
->os_desc_req
) {
1954 kfree(cdev
->os_desc_req
->buf
);
1955 usb_ep_free_request(cdev
->gadget
->ep0
, cdev
->os_desc_req
);
1958 kfree(cdev
->req
->buf
);
1959 usb_ep_dequeue(cdev
->gadget
->ep0
, cdev
->req
);
1960 usb_ep_free_request(cdev
->gadget
->ep0
, cdev
->req
);
1962 cdev
->next_string_id
= 0;
1963 device_remove_file(&cdev
->gadget
->dev
, &dev_attr_suspended
);
1966 static int composite_bind(struct usb_gadget
*gadget
,
1967 struct usb_gadget_driver
*gdriver
)
1969 struct usb_composite_dev
*cdev
;
1970 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
1971 int status
= -ENOMEM
;
1973 cdev
= kzalloc(sizeof *cdev
, GFP_KERNEL
);
1977 spin_lock_init(&cdev
->lock
);
1978 cdev
->gadget
= gadget
;
1979 set_gadget_data(gadget
, cdev
);
1980 INIT_LIST_HEAD(&cdev
->configs
);
1981 INIT_LIST_HEAD(&cdev
->gstrings
);
1983 status
= composite_dev_prepare(composite
, cdev
);
1987 /* composite gadget needs to assign strings for whole device (like
1988 * serial number), register function drivers, potentially update
1989 * power state and consumption, etc
1991 status
= composite
->bind(cdev
);
1995 if (cdev
->use_os_string
) {
1996 status
= composite_os_desc_req_prepare(cdev
, gadget
->ep0
);
2001 update_unchanged_dev_desc(&cdev
->desc
, composite
->dev
);
2003 /* has userspace failed to provide a serial number? */
2004 if (composite
->needs_serial
&& !cdev
->desc
.iSerialNumber
)
2005 WARNING(cdev
, "userspace failed to provide iSerialNumber\n");
2007 INFO(cdev
, "%s ready\n", composite
->name
);
2011 __composite_unbind(gadget
, false);
2015 /*-------------------------------------------------------------------------*/
2018 composite_suspend(struct usb_gadget
*gadget
)
2020 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2021 struct usb_function
*f
;
2023 /* REVISIT: should we have config level
2024 * suspend/resume callbacks?
2026 DBG(cdev
, "suspend\n");
2028 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
2033 if (cdev
->driver
->suspend
)
2034 cdev
->driver
->suspend(cdev
);
2036 cdev
->suspended
= 1;
2038 usb_gadget_vbus_draw(gadget
, 2);
2042 composite_resume(struct usb_gadget
*gadget
)
2044 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2045 struct usb_function
*f
;
2048 /* REVISIT: should we have config level
2049 * suspend/resume callbacks?
2051 DBG(cdev
, "resume\n");
2052 if (cdev
->driver
->resume
)
2053 cdev
->driver
->resume(cdev
);
2055 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
2060 maxpower
= cdev
->config
->MaxPower
;
2062 usb_gadget_vbus_draw(gadget
, maxpower
?
2063 maxpower
: CONFIG_USB_GADGET_VBUS_DRAW
);
2066 cdev
->suspended
= 0;
2069 /*-------------------------------------------------------------------------*/
2071 static const struct usb_gadget_driver composite_driver_template
= {
2072 .bind
= composite_bind
,
2073 .unbind
= composite_unbind
,
2075 .setup
= composite_setup
,
2076 .disconnect
= composite_disconnect
,
2078 .suspend
= composite_suspend
,
2079 .resume
= composite_resume
,
2082 .owner
= THIS_MODULE
,
2087 * usb_composite_probe() - register a composite driver
2088 * @driver: the driver to register
2090 * Context: single threaded during gadget setup
2092 * This function is used to register drivers using the composite driver
2093 * framework. The return value is zero, or a negative errno value.
2094 * Those values normally come from the driver's @bind method, which does
2095 * all the work of setting up the driver to match the hardware.
2097 * On successful return, the gadget is ready to respond to requests from
2098 * the host, unless one of its components invokes usb_gadget_disconnect()
2099 * while it was binding. That would usually be done in order to wait for
2100 * some userspace participation.
2102 int usb_composite_probe(struct usb_composite_driver
*driver
)
2104 struct usb_gadget_driver
*gadget_driver
;
2106 if (!driver
|| !driver
->dev
|| !driver
->bind
)
2110 driver
->name
= "composite";
2112 driver
->gadget_driver
= composite_driver_template
;
2113 gadget_driver
= &driver
->gadget_driver
;
2115 gadget_driver
->function
= (char *) driver
->name
;
2116 gadget_driver
->driver
.name
= driver
->name
;
2117 gadget_driver
->max_speed
= driver
->max_speed
;
2119 return usb_gadget_probe_driver(gadget_driver
);
2121 EXPORT_SYMBOL_GPL(usb_composite_probe
);
2124 * usb_composite_unregister() - unregister a composite driver
2125 * @driver: the driver to unregister
2127 * This function is used to unregister drivers using the composite
2130 void usb_composite_unregister(struct usb_composite_driver
*driver
)
2132 usb_gadget_unregister_driver(&driver
->gadget_driver
);
2134 EXPORT_SYMBOL_GPL(usb_composite_unregister
);
2137 * usb_composite_setup_continue() - Continue with the control transfer
2138 * @cdev: the composite device who's control transfer was kept waiting
2140 * This function must be called by the USB function driver to continue
2141 * with the control transfer's data/status stage in case it had requested to
2142 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2143 * can request the composite framework to delay the setup request's data/status
2144 * stages by returning USB_GADGET_DELAYED_STATUS.
2146 void usb_composite_setup_continue(struct usb_composite_dev
*cdev
)
2149 struct usb_request
*req
= cdev
->req
;
2150 unsigned long flags
;
2152 DBG(cdev
, "%s\n", __func__
);
2153 spin_lock_irqsave(&cdev
->lock
, flags
);
2155 if (cdev
->delayed_status
== 0) {
2156 WARN(cdev
, "%s: Unexpected call\n", __func__
);
2158 } else if (--cdev
->delayed_status
== 0) {
2159 DBG(cdev
, "%s: Completing delayed status\n", __func__
);
2161 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
2163 DBG(cdev
, "ep_queue --> %d\n", value
);
2165 composite_setup_complete(cdev
->gadget
->ep0
, req
);
2169 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2171 EXPORT_SYMBOL_GPL(usb_composite_setup_continue
);
2173 static char *composite_default_mfr(struct usb_gadget
*gadget
)
2178 len
= snprintf(NULL
, 0, "%s %s with %s", init_utsname()->sysname
,
2179 init_utsname()->release
, gadget
->name
);
2181 mfr
= kmalloc(len
, GFP_KERNEL
);
2184 snprintf(mfr
, len
, "%s %s with %s", init_utsname()->sysname
,
2185 init_utsname()->release
, gadget
->name
);
2189 void usb_composite_overwrite_options(struct usb_composite_dev
*cdev
,
2190 struct usb_composite_overwrite
*covr
)
2192 struct usb_device_descriptor
*desc
= &cdev
->desc
;
2193 struct usb_gadget_strings
*gstr
= cdev
->driver
->strings
[0];
2194 struct usb_string
*dev_str
= gstr
->strings
;
2197 desc
->idVendor
= cpu_to_le16(covr
->idVendor
);
2199 if (covr
->idProduct
)
2200 desc
->idProduct
= cpu_to_le16(covr
->idProduct
);
2202 if (covr
->bcdDevice
)
2203 desc
->bcdDevice
= cpu_to_le16(covr
->bcdDevice
);
2205 if (covr
->serial_number
) {
2206 desc
->iSerialNumber
= dev_str
[USB_GADGET_SERIAL_IDX
].id
;
2207 dev_str
[USB_GADGET_SERIAL_IDX
].s
= covr
->serial_number
;
2209 if (covr
->manufacturer
) {
2210 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
2211 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= covr
->manufacturer
;
2213 } else if (!strlen(dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
)) {
2214 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
2215 cdev
->def_manufacturer
= composite_default_mfr(cdev
->gadget
);
2216 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= cdev
->def_manufacturer
;
2219 if (covr
->product
) {
2220 desc
->iProduct
= dev_str
[USB_GADGET_PRODUCT_IDX
].id
;
2221 dev_str
[USB_GADGET_PRODUCT_IDX
].s
= covr
->product
;
2224 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options
);
2226 MODULE_LICENSE("GPL");
2227 MODULE_AUTHOR("David Brownell");