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 <linux/usb/otg.h>
23 #include <asm/unaligned.h>
25 #include "u_os_desc.h"
28 * struct usb_os_string - represents OS String to be reported by a gadget
29 * @bLength: total length of the entire descritor, always 0x12
30 * @bDescriptorType: USB_DT_STRING
31 * @qwSignature: the OS String proper
32 * @bMS_VendorCode: code used by the host for subsequent requests
33 * @bPad: not used, must be zero
35 struct usb_os_string
{
38 __u8 qwSignature
[OS_STRING_QW_SIGN_LEN
];
44 * The code in this file is utility code, used to build a gadget driver
45 * from one or more "function" drivers, one or more "configuration"
46 * objects, and a "usb_composite_driver" by gluing them together along
47 * with the relevant device-wide data.
50 static struct usb_gadget_strings
**get_containers_gs(
51 struct usb_gadget_string_container
*uc
)
53 return (struct usb_gadget_strings
**)uc
->stash
;
57 * function_descriptors() - get function descriptors for speed
61 * Returns the descriptors or NULL if not set.
63 static struct usb_descriptor_header
**
64 function_descriptors(struct usb_function
*f
,
65 enum usb_device_speed speed
)
67 struct usb_descriptor_header
**descriptors
;
70 case USB_SPEED_SUPER_PLUS
:
71 descriptors
= f
->ssp_descriptors
;
74 descriptors
= f
->ss_descriptors
;
77 descriptors
= f
->hs_descriptors
;
80 descriptors
= f
->fs_descriptors
;
87 * next_ep_desc() - advance to the next EP descriptor
88 * @t: currect pointer within descriptor array
90 * Return: next EP descriptor or NULL
92 * Iterate over @t until either EP descriptor found or
93 * NULL (that indicates end of list) encountered
95 static struct usb_descriptor_header
**
96 next_ep_desc(struct usb_descriptor_header
**t
)
99 if ((*t
)->bDescriptorType
== USB_DT_ENDPOINT
)
106 * for_each_ep_desc()- iterate over endpoint descriptors in the
108 * @start: pointer within descriptor array.
109 * @ep_desc: endpoint descriptor to use as the loop cursor
111 #define for_each_ep_desc(start, ep_desc) \
112 for (ep_desc = next_ep_desc(start); \
113 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
116 * config_ep_by_speed() - configures the given endpoint
117 * according to gadget speed.
118 * @g: pointer to the gadget
120 * @_ep: the endpoint to configure
122 * Return: error code, 0 on success
124 * This function chooses the right descriptors for a given
125 * endpoint according to gadget speed and saves it in the
126 * endpoint desc field. If the endpoint already has a descriptor
127 * assigned to it - overwrites it with currently corresponding
128 * descriptor. The endpoint maxpacket field is updated according
129 * to the chosen descriptor.
130 * Note: the supplied function should hold all the descriptors
131 * for supported speeds
133 int config_ep_by_speed(struct usb_gadget
*g
,
134 struct usb_function
*f
,
137 struct usb_composite_dev
*cdev
= get_gadget_data(g
);
138 struct usb_endpoint_descriptor
*chosen_desc
= NULL
;
139 struct usb_descriptor_header
**speed_desc
= NULL
;
141 struct usb_ss_ep_comp_descriptor
*comp_desc
= NULL
;
142 int want_comp_desc
= 0;
144 struct usb_descriptor_header
**d_spd
; /* cursor for speed desc */
146 if (!g
|| !f
|| !_ep
)
149 /* select desired speed */
151 case USB_SPEED_SUPER_PLUS
:
152 if (gadget_is_superspeed_plus(g
)) {
153 speed_desc
= f
->ssp_descriptors
;
157 /* else: Fall trough */
158 case USB_SPEED_SUPER
:
159 if (gadget_is_superspeed(g
)) {
160 speed_desc
= f
->ss_descriptors
;
164 /* else: Fall trough */
166 if (gadget_is_dualspeed(g
)) {
167 speed_desc
= f
->hs_descriptors
;
170 /* else: fall through */
172 speed_desc
= f
->fs_descriptors
;
174 /* find descriptors */
175 for_each_ep_desc(speed_desc
, d_spd
) {
176 chosen_desc
= (struct usb_endpoint_descriptor
*)*d_spd
;
177 if (chosen_desc
->bEndpointAddress
== _ep
->address
)
184 _ep
->maxpacket
= usb_endpoint_maxp(chosen_desc
);
185 _ep
->desc
= chosen_desc
;
186 _ep
->comp_desc
= NULL
;
193 * Companion descriptor should follow EP descriptor
194 * USB 3.0 spec, #9.6.7
196 comp_desc
= (struct usb_ss_ep_comp_descriptor
*)*(++d_spd
);
198 (comp_desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
))
200 _ep
->comp_desc
= comp_desc
;
201 if (g
->speed
>= USB_SPEED_SUPER
) {
202 switch (usb_endpoint_type(_ep
->desc
)) {
203 case USB_ENDPOINT_XFER_ISOC
:
204 /* mult: bits 1:0 of bmAttributes */
205 _ep
->mult
= comp_desc
->bmAttributes
& 0x3;
206 case USB_ENDPOINT_XFER_BULK
:
207 case USB_ENDPOINT_XFER_INT
:
208 _ep
->maxburst
= comp_desc
->bMaxBurst
+ 1;
211 if (comp_desc
->bMaxBurst
!= 0)
212 ERROR(cdev
, "ep0 bMaxBurst must be 0\n");
219 EXPORT_SYMBOL_GPL(config_ep_by_speed
);
222 * usb_add_function() - add a function to a configuration
223 * @config: the configuration
224 * @function: the function being added
225 * Context: single threaded during gadget setup
227 * After initialization, each configuration must have one or more
228 * functions added to it. Adding a function involves calling its @bind()
229 * method to allocate resources such as interface and string identifiers
232 * This function returns the value of the function's bind(), which is
233 * zero for success else a negative errno value.
235 int usb_add_function(struct usb_configuration
*config
,
236 struct usb_function
*function
)
240 DBG(config
->cdev
, "adding '%s'/%p to config '%s'/%p\n",
241 function
->name
, function
,
242 config
->label
, config
);
244 if (!function
->set_alt
|| !function
->disable
)
247 function
->config
= config
;
248 list_add_tail(&function
->list
, &config
->functions
);
250 if (function
->bind_deactivated
) {
251 value
= usb_function_deactivate(function
);
256 /* REVISIT *require* function->bind? */
257 if (function
->bind
) {
258 value
= function
->bind(config
, function
);
260 list_del(&function
->list
);
261 function
->config
= NULL
;
266 /* We allow configurations that don't work at both speeds.
267 * If we run into a lowspeed Linux system, treat it the same
268 * as full speed ... it's the function drivers that will need
269 * to avoid bulk and ISO transfers.
271 if (!config
->fullspeed
&& function
->fs_descriptors
)
272 config
->fullspeed
= true;
273 if (!config
->highspeed
&& function
->hs_descriptors
)
274 config
->highspeed
= true;
275 if (!config
->superspeed
&& function
->ss_descriptors
)
276 config
->superspeed
= true;
277 if (!config
->superspeed_plus
&& function
->ssp_descriptors
)
278 config
->superspeed_plus
= true;
282 DBG(config
->cdev
, "adding '%s'/%p --> %d\n",
283 function
->name
, function
, value
);
286 EXPORT_SYMBOL_GPL(usb_add_function
);
288 void usb_remove_function(struct usb_configuration
*c
, struct usb_function
*f
)
293 bitmap_zero(f
->endpoints
, 32);
298 EXPORT_SYMBOL_GPL(usb_remove_function
);
301 * usb_function_deactivate - prevent function and gadget enumeration
302 * @function: the function that isn't yet ready to respond
304 * Blocks response of the gadget driver to host enumeration by
305 * preventing the data line pullup from being activated. This is
306 * normally called during @bind() processing to change from the
307 * initial "ready to respond" state, or when a required resource
310 * For example, drivers that serve as a passthrough to a userspace
311 * daemon can block enumeration unless that daemon (such as an OBEX,
312 * MTP, or print server) is ready to handle host requests.
314 * Not all systems support software control of their USB peripheral
317 * Returns zero on success, else negative errno.
319 int usb_function_deactivate(struct usb_function
*function
)
321 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
325 spin_lock_irqsave(&cdev
->lock
, flags
);
327 if (cdev
->deactivations
== 0)
328 status
= usb_gadget_deactivate(cdev
->gadget
);
330 cdev
->deactivations
++;
332 spin_unlock_irqrestore(&cdev
->lock
, flags
);
335 EXPORT_SYMBOL_GPL(usb_function_deactivate
);
338 * usb_function_activate - allow function and gadget enumeration
339 * @function: function on which usb_function_activate() was called
341 * Reverses effect of usb_function_deactivate(). If no more functions
342 * are delaying their activation, the gadget driver will respond to
343 * host enumeration procedures.
345 * Returns zero on success, else negative errno.
347 int usb_function_activate(struct usb_function
*function
)
349 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
353 spin_lock_irqsave(&cdev
->lock
, flags
);
355 if (WARN_ON(cdev
->deactivations
== 0))
358 cdev
->deactivations
--;
359 if (cdev
->deactivations
== 0)
360 status
= usb_gadget_activate(cdev
->gadget
);
363 spin_unlock_irqrestore(&cdev
->lock
, flags
);
366 EXPORT_SYMBOL_GPL(usb_function_activate
);
369 * usb_interface_id() - allocate an unused interface ID
370 * @config: configuration associated with the interface
371 * @function: function handling the interface
372 * Context: single threaded during gadget setup
374 * usb_interface_id() is called from usb_function.bind() callbacks to
375 * allocate new interface IDs. The function driver will then store that
376 * ID in interface, association, CDC union, and other descriptors. It
377 * will also handle any control requests targeted at that interface,
378 * particularly changing its altsetting via set_alt(). There may
379 * also be class-specific or vendor-specific requests to handle.
381 * All interface identifier should be allocated using this routine, to
382 * ensure that for example different functions don't wrongly assign
383 * different meanings to the same identifier. Note that since interface
384 * identifiers are configuration-specific, functions used in more than
385 * one configuration (or more than once in a given configuration) need
386 * multiple versions of the relevant descriptors.
388 * Returns the interface ID which was allocated; or -ENODEV if no
389 * more interface IDs can be allocated.
391 int usb_interface_id(struct usb_configuration
*config
,
392 struct usb_function
*function
)
394 unsigned id
= config
->next_interface_id
;
396 if (id
< MAX_CONFIG_INTERFACES
) {
397 config
->interface
[id
] = function
;
398 config
->next_interface_id
= id
+ 1;
403 EXPORT_SYMBOL_GPL(usb_interface_id
);
405 static u8
encode_bMaxPower(enum usb_device_speed speed
,
406 struct usb_configuration
*c
)
413 val
= CONFIG_USB_GADGET_VBUS_DRAW
;
417 case USB_SPEED_SUPER
:
418 return DIV_ROUND_UP(val
, 8);
420 return DIV_ROUND_UP(val
, 2);
424 static int config_buf(struct usb_configuration
*config
,
425 enum usb_device_speed speed
, void *buf
, u8 type
)
427 struct usb_config_descriptor
*c
= buf
;
428 void *next
= buf
+ USB_DT_CONFIG_SIZE
;
430 struct usb_function
*f
;
433 len
= USB_COMP_EP0_BUFSIZ
- USB_DT_CONFIG_SIZE
;
434 /* write the config descriptor */
436 c
->bLength
= USB_DT_CONFIG_SIZE
;
437 c
->bDescriptorType
= type
;
438 /* wTotalLength is written later */
439 c
->bNumInterfaces
= config
->next_interface_id
;
440 c
->bConfigurationValue
= config
->bConfigurationValue
;
441 c
->iConfiguration
= config
->iConfiguration
;
442 c
->bmAttributes
= USB_CONFIG_ATT_ONE
| config
->bmAttributes
;
443 c
->bMaxPower
= encode_bMaxPower(speed
, config
);
445 /* There may be e.g. OTG descriptors */
446 if (config
->descriptors
) {
447 status
= usb_descriptor_fillbuf(next
, len
,
448 config
->descriptors
);
455 /* add each function's descriptors */
456 list_for_each_entry(f
, &config
->functions
, list
) {
457 struct usb_descriptor_header
**descriptors
;
459 descriptors
= function_descriptors(f
, speed
);
462 status
= usb_descriptor_fillbuf(next
, len
,
463 (const struct usb_descriptor_header
**) descriptors
);
471 c
->wTotalLength
= cpu_to_le16(len
);
475 static int config_desc(struct usb_composite_dev
*cdev
, unsigned w_value
)
477 struct usb_gadget
*gadget
= cdev
->gadget
;
478 struct usb_configuration
*c
;
479 struct list_head
*pos
;
480 u8 type
= w_value
>> 8;
481 enum usb_device_speed speed
= USB_SPEED_UNKNOWN
;
483 if (gadget
->speed
>= USB_SPEED_SUPER
)
484 speed
= gadget
->speed
;
485 else if (gadget_is_dualspeed(gadget
)) {
487 if (gadget
->speed
== USB_SPEED_HIGH
)
489 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
492 speed
= USB_SPEED_HIGH
;
496 /* This is a lookup by config *INDEX* */
499 pos
= &cdev
->configs
;
500 c
= cdev
->os_desc_config
;
504 while ((pos
= pos
->next
) != &cdev
->configs
) {
505 c
= list_entry(pos
, typeof(*c
), list
);
507 /* skip OS Descriptors config which is handled separately */
508 if (c
== cdev
->os_desc_config
)
512 /* ignore configs that won't work at this speed */
514 case USB_SPEED_SUPER_PLUS
:
515 if (!c
->superspeed_plus
)
518 case USB_SPEED_SUPER
:
532 return config_buf(c
, speed
, cdev
->req
->buf
, type
);
538 static int count_configs(struct usb_composite_dev
*cdev
, unsigned type
)
540 struct usb_gadget
*gadget
= cdev
->gadget
;
541 struct usb_configuration
*c
;
547 if (gadget_is_dualspeed(gadget
)) {
548 if (gadget
->speed
== USB_SPEED_HIGH
)
550 if (gadget
->speed
== USB_SPEED_SUPER
)
552 if (gadget
->speed
== USB_SPEED_SUPER_PLUS
)
554 if (type
== USB_DT_DEVICE_QUALIFIER
)
557 list_for_each_entry(c
, &cdev
->configs
, list
) {
558 /* ignore configs that won't work at this speed */
560 if (!c
->superspeed_plus
)
578 * bos_desc() - prepares the BOS descriptor.
579 * @cdev: pointer to usb_composite device to generate the bos
582 * This function generates the BOS (Binary Device Object)
583 * descriptor and its device capabilities descriptors. The BOS
584 * descriptor should be supported by a SuperSpeed device.
586 static int bos_desc(struct usb_composite_dev
*cdev
)
588 struct usb_ext_cap_descriptor
*usb_ext
;
589 struct usb_ss_cap_descriptor
*ss_cap
;
590 struct usb_dcd_config_params dcd_config_params
;
591 struct usb_bos_descriptor
*bos
= cdev
->req
->buf
;
593 bos
->bLength
= USB_DT_BOS_SIZE
;
594 bos
->bDescriptorType
= USB_DT_BOS
;
596 bos
->wTotalLength
= cpu_to_le16(USB_DT_BOS_SIZE
);
597 bos
->bNumDeviceCaps
= 0;
600 * A SuperSpeed device shall include the USB2.0 extension descriptor
601 * and shall support LPM when operating in USB2.0 HS mode.
603 usb_ext
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
604 bos
->bNumDeviceCaps
++;
605 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_EXT_CAP_SIZE
);
606 usb_ext
->bLength
= USB_DT_USB_EXT_CAP_SIZE
;
607 usb_ext
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
608 usb_ext
->bDevCapabilityType
= USB_CAP_TYPE_EXT
;
609 usb_ext
->bmAttributes
= cpu_to_le32(USB_LPM_SUPPORT
| USB_BESL_SUPPORT
);
612 * The Superspeed USB Capability descriptor shall be implemented by all
613 * SuperSpeed devices.
615 ss_cap
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
616 bos
->bNumDeviceCaps
++;
617 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_SS_CAP_SIZE
);
618 ss_cap
->bLength
= USB_DT_USB_SS_CAP_SIZE
;
619 ss_cap
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
620 ss_cap
->bDevCapabilityType
= USB_SS_CAP_TYPE
;
621 ss_cap
->bmAttributes
= 0; /* LTM is not supported yet */
622 ss_cap
->wSpeedSupported
= cpu_to_le16(USB_LOW_SPEED_OPERATION
|
623 USB_FULL_SPEED_OPERATION
|
624 USB_HIGH_SPEED_OPERATION
|
625 USB_5GBPS_OPERATION
);
626 ss_cap
->bFunctionalitySupport
= USB_LOW_SPEED_OPERATION
;
628 /* Get Controller configuration */
629 if (cdev
->gadget
->ops
->get_config_params
)
630 cdev
->gadget
->ops
->get_config_params(&dcd_config_params
);
632 dcd_config_params
.bU1devExitLat
= USB_DEFAULT_U1_DEV_EXIT_LAT
;
633 dcd_config_params
.bU2DevExitLat
=
634 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT
);
636 ss_cap
->bU1devExitLat
= dcd_config_params
.bU1devExitLat
;
637 ss_cap
->bU2DevExitLat
= dcd_config_params
.bU2DevExitLat
;
639 /* The SuperSpeedPlus USB Device Capability descriptor */
640 if (gadget_is_superspeed_plus(cdev
->gadget
)) {
641 struct usb_ssp_cap_descriptor
*ssp_cap
;
643 ssp_cap
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
644 bos
->bNumDeviceCaps
++;
647 * Report typical values.
650 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_SSP_CAP_SIZE(1));
651 ssp_cap
->bLength
= USB_DT_USB_SSP_CAP_SIZE(1);
652 ssp_cap
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
653 ssp_cap
->bDevCapabilityType
= USB_SSP_CAP_TYPE
;
655 /* SSAC = 1 (2 attributes) */
656 ssp_cap
->bmAttributes
= cpu_to_le32(1);
658 /* Min RX/TX Lane Count = 1 */
659 ssp_cap
->wFunctionalitySupport
=
660 cpu_to_le16((1 << 8) | (1 << 12));
663 * bmSublinkSpeedAttr[0]:
666 * LP = 1 (SuperSpeedPlus)
669 ssp_cap
->bmSublinkSpeedAttr
[0] =
670 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
672 * bmSublinkSpeedAttr[1] =
675 * LP = 1 (SuperSpeedPlus)
678 ssp_cap
->bmSublinkSpeedAttr
[1] =
679 cpu_to_le32((3 << 4) | (1 << 14) |
680 (0xa << 16) | (1 << 7));
683 return le16_to_cpu(bos
->wTotalLength
);
686 static void device_qual(struct usb_composite_dev
*cdev
)
688 struct usb_qualifier_descriptor
*qual
= cdev
->req
->buf
;
690 qual
->bLength
= sizeof(*qual
);
691 qual
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
692 /* POLICY: same bcdUSB and device type info at both speeds */
693 qual
->bcdUSB
= cdev
->desc
.bcdUSB
;
694 qual
->bDeviceClass
= cdev
->desc
.bDeviceClass
;
695 qual
->bDeviceSubClass
= cdev
->desc
.bDeviceSubClass
;
696 qual
->bDeviceProtocol
= cdev
->desc
.bDeviceProtocol
;
697 /* ASSUME same EP0 fifo size at both speeds */
698 qual
->bMaxPacketSize0
= cdev
->gadget
->ep0
->maxpacket
;
699 qual
->bNumConfigurations
= count_configs(cdev
, USB_DT_DEVICE_QUALIFIER
);
703 /*-------------------------------------------------------------------------*/
705 static void reset_config(struct usb_composite_dev
*cdev
)
707 struct usb_function
*f
;
709 DBG(cdev
, "reset config\n");
711 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
715 bitmap_zero(f
->endpoints
, 32);
718 cdev
->delayed_status
= 0;
721 static int set_config(struct usb_composite_dev
*cdev
,
722 const struct usb_ctrlrequest
*ctrl
, unsigned number
)
724 struct usb_gadget
*gadget
= cdev
->gadget
;
725 struct usb_configuration
*c
= NULL
;
726 int result
= -EINVAL
;
727 unsigned power
= gadget_is_otg(gadget
) ? 8 : 100;
731 list_for_each_entry(c
, &cdev
->configs
, list
) {
732 if (c
->bConfigurationValue
== number
) {
734 * We disable the FDs of the previous
735 * configuration only if the new configuration
746 } else { /* Zero configuration value - need to reset the config */
752 INFO(cdev
, "%s config #%d: %s\n",
753 usb_speed_string(gadget
->speed
),
754 number
, c
? c
->label
: "unconfigured");
759 usb_gadget_set_state(gadget
, USB_STATE_CONFIGURED
);
762 /* Initialize all interfaces by setting them to altsetting zero. */
763 for (tmp
= 0; tmp
< MAX_CONFIG_INTERFACES
; tmp
++) {
764 struct usb_function
*f
= c
->interface
[tmp
];
765 struct usb_descriptor_header
**descriptors
;
771 * Record which endpoints are used by the function. This is used
772 * to dispatch control requests targeted at that endpoint to the
773 * function's setup callback instead of the current
774 * configuration's setup callback.
776 descriptors
= function_descriptors(f
, gadget
->speed
);
778 for (; *descriptors
; ++descriptors
) {
779 struct usb_endpoint_descriptor
*ep
;
782 if ((*descriptors
)->bDescriptorType
!= USB_DT_ENDPOINT
)
785 ep
= (struct usb_endpoint_descriptor
*)*descriptors
;
786 addr
= ((ep
->bEndpointAddress
& 0x80) >> 3)
787 | (ep
->bEndpointAddress
& 0x0f);
788 set_bit(addr
, f
->endpoints
);
791 result
= f
->set_alt(f
, tmp
, 0);
793 DBG(cdev
, "interface %d (%s/%p) alt 0 --> %d\n",
794 tmp
, f
->name
, f
, result
);
800 if (result
== USB_GADGET_DELAYED_STATUS
) {
802 "%s: interface %d (%s) requested delayed status\n",
803 __func__
, tmp
, f
->name
);
804 cdev
->delayed_status
++;
805 DBG(cdev
, "delayed_status count %d\n",
806 cdev
->delayed_status
);
810 /* when we return, be sure our power usage is valid */
811 power
= c
->MaxPower
? c
->MaxPower
: CONFIG_USB_GADGET_VBUS_DRAW
;
813 usb_gadget_vbus_draw(gadget
, power
);
814 if (result
>= 0 && cdev
->delayed_status
)
815 result
= USB_GADGET_DELAYED_STATUS
;
819 int usb_add_config_only(struct usb_composite_dev
*cdev
,
820 struct usb_configuration
*config
)
822 struct usb_configuration
*c
;
824 if (!config
->bConfigurationValue
)
827 /* Prevent duplicate configuration identifiers */
828 list_for_each_entry(c
, &cdev
->configs
, list
) {
829 if (c
->bConfigurationValue
== config
->bConfigurationValue
)
834 list_add_tail(&config
->list
, &cdev
->configs
);
836 INIT_LIST_HEAD(&config
->functions
);
837 config
->next_interface_id
= 0;
838 memset(config
->interface
, 0, sizeof(config
->interface
));
842 EXPORT_SYMBOL_GPL(usb_add_config_only
);
845 * usb_add_config() - add a configuration to a device.
846 * @cdev: wraps the USB gadget
847 * @config: the configuration, with bConfigurationValue assigned
848 * @bind: the configuration's bind function
849 * Context: single threaded during gadget setup
851 * One of the main tasks of a composite @bind() routine is to
852 * add each of the configurations it supports, using this routine.
854 * This function returns the value of the configuration's @bind(), which
855 * is zero for success else a negative errno value. Binding configurations
856 * assigns global resources including string IDs, and per-configuration
857 * resources such as interface IDs and endpoints.
859 int usb_add_config(struct usb_composite_dev
*cdev
,
860 struct usb_configuration
*config
,
861 int (*bind
)(struct usb_configuration
*))
863 int status
= -EINVAL
;
868 DBG(cdev
, "adding config #%u '%s'/%p\n",
869 config
->bConfigurationValue
,
870 config
->label
, config
);
872 status
= usb_add_config_only(cdev
, config
);
876 status
= bind(config
);
878 while (!list_empty(&config
->functions
)) {
879 struct usb_function
*f
;
881 f
= list_first_entry(&config
->functions
,
882 struct usb_function
, list
);
885 DBG(cdev
, "unbind function '%s'/%p\n",
887 f
->unbind(config
, f
);
888 /* may free memory for "f" */
891 list_del(&config
->list
);
896 DBG(cdev
, "cfg %d/%p speeds:%s%s%s%s\n",
897 config
->bConfigurationValue
, config
,
898 config
->superspeed_plus
? " superplus" : "",
899 config
->superspeed
? " super" : "",
900 config
->highspeed
? " high" : "",
902 ? (gadget_is_dualspeed(cdev
->gadget
)
907 for (i
= 0; i
< MAX_CONFIG_INTERFACES
; i
++) {
908 struct usb_function
*f
= config
->interface
[i
];
912 DBG(cdev
, " interface %d = %s/%p\n",
917 /* set_alt(), or next bind(), sets up ep->claimed as needed */
918 usb_ep_autoconfig_reset(cdev
->gadget
);
922 DBG(cdev
, "added config '%s'/%u --> %d\n", config
->label
,
923 config
->bConfigurationValue
, status
);
926 EXPORT_SYMBOL_GPL(usb_add_config
);
928 static void remove_config(struct usb_composite_dev
*cdev
,
929 struct usb_configuration
*config
)
931 while (!list_empty(&config
->functions
)) {
932 struct usb_function
*f
;
934 f
= list_first_entry(&config
->functions
,
935 struct usb_function
, list
);
938 DBG(cdev
, "unbind function '%s'/%p\n", f
->name
, f
);
939 f
->unbind(config
, f
);
940 /* may free memory for "f" */
943 list_del(&config
->list
);
944 if (config
->unbind
) {
945 DBG(cdev
, "unbind config '%s'/%p\n", config
->label
, config
);
946 config
->unbind(config
);
947 /* may free memory for "c" */
952 * usb_remove_config() - remove a configuration from a device.
953 * @cdev: wraps the USB gadget
954 * @config: the configuration
956 * Drivers must call usb_gadget_disconnect before calling this function
957 * to disconnect the device from the host and make sure the host will not
958 * try to enumerate the device while we are changing the config list.
960 void usb_remove_config(struct usb_composite_dev
*cdev
,
961 struct usb_configuration
*config
)
965 spin_lock_irqsave(&cdev
->lock
, flags
);
967 if (cdev
->config
== config
)
970 spin_unlock_irqrestore(&cdev
->lock
, flags
);
972 remove_config(cdev
, config
);
975 /*-------------------------------------------------------------------------*/
977 /* We support strings in multiple languages ... string descriptor zero
978 * says which languages are supported. The typical case will be that
979 * only one language (probably English) is used, with i18n handled on
983 static void collect_langs(struct usb_gadget_strings
**sp
, __le16
*buf
)
985 const struct usb_gadget_strings
*s
;
991 language
= cpu_to_le16(s
->language
);
992 for (tmp
= buf
; *tmp
&& tmp
< &buf
[126]; tmp
++) {
993 if (*tmp
== language
)
1002 static int lookup_string(
1003 struct usb_gadget_strings
**sp
,
1009 struct usb_gadget_strings
*s
;
1014 if (s
->language
!= language
)
1016 value
= usb_gadget_get_string(s
, id
, buf
);
1023 static int get_string(struct usb_composite_dev
*cdev
,
1024 void *buf
, u16 language
, int id
)
1026 struct usb_composite_driver
*composite
= cdev
->driver
;
1027 struct usb_gadget_string_container
*uc
;
1028 struct usb_configuration
*c
;
1029 struct usb_function
*f
;
1032 /* Yes, not only is USB's i18n support probably more than most
1033 * folk will ever care about ... also, it's all supported here.
1034 * (Except for UTF8 support for Unicode's "Astral Planes".)
1037 /* 0 == report all available language codes */
1039 struct usb_string_descriptor
*s
= buf
;
1040 struct usb_gadget_strings
**sp
;
1043 s
->bDescriptorType
= USB_DT_STRING
;
1045 sp
= composite
->strings
;
1047 collect_langs(sp
, s
->wData
);
1049 list_for_each_entry(c
, &cdev
->configs
, list
) {
1052 collect_langs(sp
, s
->wData
);
1054 list_for_each_entry(f
, &c
->functions
, list
) {
1057 collect_langs(sp
, s
->wData
);
1060 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
1061 struct usb_gadget_strings
**sp
;
1063 sp
= get_containers_gs(uc
);
1064 collect_langs(sp
, s
->wData
);
1067 for (len
= 0; len
<= 126 && s
->wData
[len
]; len
++)
1072 s
->bLength
= 2 * (len
+ 1);
1076 if (cdev
->use_os_string
&& language
== 0 && id
== OS_STRING_IDX
) {
1077 struct usb_os_string
*b
= buf
;
1078 b
->bLength
= sizeof(*b
);
1079 b
->bDescriptorType
= USB_DT_STRING
;
1081 sizeof(b
->qwSignature
) == sizeof(cdev
->qw_sign
),
1082 "qwSignature size must be equal to qw_sign");
1083 memcpy(&b
->qwSignature
, cdev
->qw_sign
, sizeof(b
->qwSignature
));
1084 b
->bMS_VendorCode
= cdev
->b_vendor_code
;
1089 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
1090 struct usb_gadget_strings
**sp
;
1092 sp
= get_containers_gs(uc
);
1093 len
= lookup_string(sp
, buf
, language
, id
);
1098 /* String IDs are device-scoped, so we look up each string
1099 * table we're told about. These lookups are infrequent;
1100 * simpler-is-better here.
1102 if (composite
->strings
) {
1103 len
= lookup_string(composite
->strings
, buf
, language
, id
);
1107 list_for_each_entry(c
, &cdev
->configs
, list
) {
1109 len
= lookup_string(c
->strings
, buf
, language
, id
);
1113 list_for_each_entry(f
, &c
->functions
, list
) {
1116 len
= lookup_string(f
->strings
, buf
, language
, id
);
1125 * usb_string_id() - allocate an unused string ID
1126 * @cdev: the device whose string descriptor IDs are being allocated
1127 * Context: single threaded during gadget setup
1129 * @usb_string_id() is called from bind() callbacks to allocate
1130 * string IDs. Drivers for functions, configurations, or gadgets will
1131 * then store that ID in the appropriate descriptors and string table.
1133 * All string identifier should be allocated using this,
1134 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1135 * that for example different functions don't wrongly assign different
1136 * meanings to the same identifier.
1138 int usb_string_id(struct usb_composite_dev
*cdev
)
1140 if (cdev
->next_string_id
< 254) {
1141 /* string id 0 is reserved by USB spec for list of
1142 * supported languages */
1143 /* 255 reserved as well? -- mina86 */
1144 cdev
->next_string_id
++;
1145 return cdev
->next_string_id
;
1149 EXPORT_SYMBOL_GPL(usb_string_id
);
1152 * usb_string_ids() - allocate unused string IDs in batch
1153 * @cdev: the device whose string descriptor IDs are being allocated
1154 * @str: an array of usb_string objects to assign numbers to
1155 * Context: single threaded during gadget setup
1157 * @usb_string_ids() is called from bind() callbacks to allocate
1158 * string IDs. Drivers for functions, configurations, or gadgets will
1159 * then copy IDs from the string table to the appropriate descriptors
1160 * and string table for other languages.
1162 * All string identifier should be allocated using this,
1163 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1164 * example different functions don't wrongly assign different meanings
1165 * to the same identifier.
1167 int usb_string_ids_tab(struct usb_composite_dev
*cdev
, struct usb_string
*str
)
1169 int next
= cdev
->next_string_id
;
1171 for (; str
->s
; ++str
) {
1172 if (unlikely(next
>= 254))
1177 cdev
->next_string_id
= next
;
1181 EXPORT_SYMBOL_GPL(usb_string_ids_tab
);
1183 static struct usb_gadget_string_container
*copy_gadget_strings(
1184 struct usb_gadget_strings
**sp
, unsigned n_gstrings
,
1187 struct usb_gadget_string_container
*uc
;
1188 struct usb_gadget_strings
**gs_array
;
1189 struct usb_gadget_strings
*gs
;
1190 struct usb_string
*s
;
1197 mem
+= sizeof(void *) * (n_gstrings
+ 1);
1198 mem
+= sizeof(struct usb_gadget_strings
) * n_gstrings
;
1199 mem
+= sizeof(struct usb_string
) * (n_strings
+ 1) * (n_gstrings
);
1200 uc
= kmalloc(mem
, GFP_KERNEL
);
1202 return ERR_PTR(-ENOMEM
);
1203 gs_array
= get_containers_gs(uc
);
1205 stash
+= sizeof(void *) * (n_gstrings
+ 1);
1206 for (n_gs
= 0; n_gs
< n_gstrings
; n_gs
++) {
1207 struct usb_string
*org_s
;
1209 gs_array
[n_gs
] = stash
;
1210 gs
= gs_array
[n_gs
];
1211 stash
+= sizeof(struct usb_gadget_strings
);
1212 gs
->language
= sp
[n_gs
]->language
;
1213 gs
->strings
= stash
;
1214 org_s
= sp
[n_gs
]->strings
;
1216 for (n_s
= 0; n_s
< n_strings
; n_s
++) {
1218 stash
+= sizeof(struct usb_string
);
1227 stash
+= sizeof(struct usb_string
);
1230 gs_array
[n_gs
] = NULL
;
1235 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1236 * @cdev: the device whose string descriptor IDs are being allocated
1238 * @sp: an array of usb_gadget_strings to attach.
1239 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1241 * This function will create a deep copy of usb_gadget_strings and usb_string
1242 * and attach it to the cdev. The actual string (usb_string.s) will not be
1243 * copied but only a referenced will be made. The struct usb_gadget_strings
1244 * array may contain multiple languages and should be NULL terminated.
1245 * The ->language pointer of each struct usb_gadget_strings has to contain the
1246 * same amount of entries.
1247 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1248 * usb_string entry of es-ES contains the translation of the first usb_string
1249 * entry of en-US. Therefore both entries become the same id assign.
1251 struct usb_string
*usb_gstrings_attach(struct usb_composite_dev
*cdev
,
1252 struct usb_gadget_strings
**sp
, unsigned n_strings
)
1254 struct usb_gadget_string_container
*uc
;
1255 struct usb_gadget_strings
**n_gs
;
1256 unsigned n_gstrings
= 0;
1260 for (i
= 0; sp
[i
]; i
++)
1264 return ERR_PTR(-EINVAL
);
1266 uc
= copy_gadget_strings(sp
, n_gstrings
, n_strings
);
1268 return ERR_CAST(uc
);
1270 n_gs
= get_containers_gs(uc
);
1271 ret
= usb_string_ids_tab(cdev
, n_gs
[0]->strings
);
1275 for (i
= 1; i
< n_gstrings
; i
++) {
1276 struct usb_string
*m_s
;
1277 struct usb_string
*s
;
1280 m_s
= n_gs
[0]->strings
;
1281 s
= n_gs
[i
]->strings
;
1282 for (n
= 0; n
< n_strings
; n
++) {
1288 list_add_tail(&uc
->list
, &cdev
->gstrings
);
1289 return n_gs
[0]->strings
;
1292 return ERR_PTR(ret
);
1294 EXPORT_SYMBOL_GPL(usb_gstrings_attach
);
1297 * usb_string_ids_n() - allocate unused string IDs in batch
1298 * @c: the device whose string descriptor IDs are being allocated
1299 * @n: number of string IDs to allocate
1300 * Context: single threaded during gadget setup
1302 * Returns the first requested ID. This ID and next @n-1 IDs are now
1303 * valid IDs. At least provided that @n is non-zero because if it
1304 * is, returns last requested ID which is now very useful information.
1306 * @usb_string_ids_n() is called from bind() callbacks to allocate
1307 * string IDs. Drivers for functions, configurations, or gadgets will
1308 * then store that ID in the appropriate descriptors and string table.
1310 * All string identifier should be allocated using this,
1311 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1312 * example different functions don't wrongly assign different meanings
1313 * to the same identifier.
1315 int usb_string_ids_n(struct usb_composite_dev
*c
, unsigned n
)
1317 unsigned next
= c
->next_string_id
;
1318 if (unlikely(n
> 254 || (unsigned)next
+ n
> 254))
1320 c
->next_string_id
+= n
;
1323 EXPORT_SYMBOL_GPL(usb_string_ids_n
);
1325 /*-------------------------------------------------------------------------*/
1327 static void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1329 struct usb_composite_dev
*cdev
;
1331 if (req
->status
|| req
->actual
!= req
->length
)
1332 DBG((struct usb_composite_dev
*) ep
->driver_data
,
1333 "setup complete --> %d, %d/%d\n",
1334 req
->status
, req
->actual
, req
->length
);
1337 * REVIST The same ep0 requests are shared with function drivers
1338 * so they don't have to maintain the same ->complete() stubs.
1340 * Because of that, we need to check for the validity of ->context
1341 * here, even though we know we've set it to something useful.
1346 cdev
= req
->context
;
1348 if (cdev
->req
== req
)
1349 cdev
->setup_pending
= false;
1350 else if (cdev
->os_desc_req
== req
)
1351 cdev
->os_desc_pending
= false;
1353 WARN(1, "unknown request %p\n", req
);
1356 static int composite_ep0_queue(struct usb_composite_dev
*cdev
,
1357 struct usb_request
*req
, gfp_t gfp_flags
)
1361 ret
= usb_ep_queue(cdev
->gadget
->ep0
, req
, gfp_flags
);
1363 if (cdev
->req
== req
)
1364 cdev
->setup_pending
= true;
1365 else if (cdev
->os_desc_req
== req
)
1366 cdev
->os_desc_pending
= true;
1368 WARN(1, "unknown request %p\n", req
);
1374 static int count_ext_compat(struct usb_configuration
*c
)
1379 for (i
= 0; i
< c
->next_interface_id
; ++i
) {
1380 struct usb_function
*f
;
1383 f
= c
->interface
[i
];
1384 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1385 struct usb_os_desc
*d
;
1387 if (i
!= f
->os_desc_table
[j
].if_id
)
1389 d
= f
->os_desc_table
[j
].os_desc
;
1390 if (d
&& d
->ext_compat_id
)
1398 static void fill_ext_compat(struct usb_configuration
*c
, u8
*buf
)
1403 for (i
= 0; i
< c
->next_interface_id
; ++i
) {
1404 struct usb_function
*f
;
1407 f
= c
->interface
[i
];
1408 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1409 struct usb_os_desc
*d
;
1411 if (i
!= f
->os_desc_table
[j
].if_id
)
1413 d
= f
->os_desc_table
[j
].os_desc
;
1414 if (d
&& d
->ext_compat_id
) {
1417 memcpy(buf
, d
->ext_compat_id
, 16);
1431 static int count_ext_prop(struct usb_configuration
*c
, int interface
)
1433 struct usb_function
*f
;
1436 f
= c
->interface
[interface
];
1437 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1438 struct usb_os_desc
*d
;
1440 if (interface
!= f
->os_desc_table
[j
].if_id
)
1442 d
= f
->os_desc_table
[j
].os_desc
;
1443 if (d
&& d
->ext_compat_id
)
1444 return d
->ext_prop_count
;
1449 static int len_ext_prop(struct usb_configuration
*c
, int interface
)
1451 struct usb_function
*f
;
1452 struct usb_os_desc
*d
;
1455 res
= 10; /* header length */
1456 f
= c
->interface
[interface
];
1457 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1458 if (interface
!= f
->os_desc_table
[j
].if_id
)
1460 d
= f
->os_desc_table
[j
].os_desc
;
1462 return min(res
+ d
->ext_prop_len
, 4096);
1467 static int fill_ext_prop(struct usb_configuration
*c
, int interface
, u8
*buf
)
1469 struct usb_function
*f
;
1470 struct usb_os_desc
*d
;
1471 struct usb_os_desc_ext_prop
*ext_prop
;
1472 int j
, count
, n
, ret
;
1475 f
= c
->interface
[interface
];
1476 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1477 if (interface
!= f
->os_desc_table
[j
].if_id
)
1479 d
= f
->os_desc_table
[j
].os_desc
;
1481 list_for_each_entry(ext_prop
, &d
->ext_prop
, entry
) {
1482 /* 4kB minus header length */
1487 count
= ext_prop
->data_len
+
1488 ext_prop
->name_len
+ 14;
1489 if (count
> 4086 - n
)
1491 usb_ext_prop_put_size(buf
, count
);
1492 usb_ext_prop_put_type(buf
, ext_prop
->type
);
1493 ret
= usb_ext_prop_put_name(buf
, ext_prop
->name
,
1494 ext_prop
->name_len
);
1497 switch (ext_prop
->type
) {
1498 case USB_EXT_PROP_UNICODE
:
1499 case USB_EXT_PROP_UNICODE_ENV
:
1500 case USB_EXT_PROP_UNICODE_LINK
:
1501 usb_ext_prop_put_unicode(buf
, ret
,
1503 ext_prop
->data_len
);
1505 case USB_EXT_PROP_BINARY
:
1506 usb_ext_prop_put_binary(buf
, ret
,
1508 ext_prop
->data_len
);
1510 case USB_EXT_PROP_LE32
:
1511 /* not implemented */
1512 case USB_EXT_PROP_BE32
:
1513 /* not implemented */
1525 * The setup() callback implements all the ep0 functionality that's
1526 * not handled lower down, in hardware or the hardware driver(like
1527 * device and endpoint feature flags, and their status). It's all
1528 * housekeeping for the gadget function we're implementing. Most of
1529 * the work is in config and function specific setup.
1532 composite_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1534 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1535 struct usb_request
*req
= cdev
->req
;
1536 int value
= -EOPNOTSUPP
;
1538 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1539 u8 intf
= w_index
& 0xFF;
1540 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1541 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1542 struct usb_function
*f
= NULL
;
1545 /* partial re-init of the response message; the function or the
1546 * gadget might need to intercept e.g. a control-OUT completion
1547 * when we delegate to it.
1550 req
->context
= cdev
;
1551 req
->complete
= composite_setup_complete
;
1553 gadget
->ep0
->driver_data
= cdev
;
1556 * Don't let non-standard requests match any of the cases below
1559 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_STANDARD
)
1562 switch (ctrl
->bRequest
) {
1564 /* we handle all standard USB descriptors */
1565 case USB_REQ_GET_DESCRIPTOR
:
1566 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1568 switch (w_value
>> 8) {
1571 cdev
->desc
.bNumConfigurations
=
1572 count_configs(cdev
, USB_DT_DEVICE
);
1573 cdev
->desc
.bMaxPacketSize0
=
1574 cdev
->gadget
->ep0
->maxpacket
;
1575 if (gadget_is_superspeed(gadget
)) {
1576 if (gadget
->speed
>= USB_SPEED_SUPER
) {
1577 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0310);
1578 cdev
->desc
.bMaxPacketSize0
= 9;
1580 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0210);
1583 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0200);
1586 value
= min(w_length
, (u16
) sizeof cdev
->desc
);
1587 memcpy(req
->buf
, &cdev
->desc
, value
);
1589 case USB_DT_DEVICE_QUALIFIER
:
1590 if (!gadget_is_dualspeed(gadget
) ||
1591 gadget
->speed
>= USB_SPEED_SUPER
)
1594 value
= min_t(int, w_length
,
1595 sizeof(struct usb_qualifier_descriptor
));
1597 case USB_DT_OTHER_SPEED_CONFIG
:
1598 if (!gadget_is_dualspeed(gadget
) ||
1599 gadget
->speed
>= USB_SPEED_SUPER
)
1603 value
= config_desc(cdev
, w_value
);
1605 value
= min(w_length
, (u16
) value
);
1608 value
= get_string(cdev
, req
->buf
,
1609 w_index
, w_value
& 0xff);
1611 value
= min(w_length
, (u16
) value
);
1614 if (gadget_is_superspeed(gadget
)) {
1615 value
= bos_desc(cdev
);
1616 value
= min(w_length
, (u16
) value
);
1620 if (gadget_is_otg(gadget
)) {
1621 struct usb_configuration
*config
;
1622 int otg_desc_len
= 0;
1625 config
= cdev
->config
;
1627 config
= list_first_entry(
1629 struct usb_configuration
, list
);
1633 if (gadget
->otg_caps
&&
1634 (gadget
->otg_caps
->otg_rev
>= 0x0200))
1635 otg_desc_len
+= sizeof(
1636 struct usb_otg20_descriptor
);
1638 otg_desc_len
+= sizeof(
1639 struct usb_otg_descriptor
);
1641 value
= min_t(int, w_length
, otg_desc_len
);
1642 memcpy(req
->buf
, config
->descriptors
[0], value
);
1648 /* any number of configs can work */
1649 case USB_REQ_SET_CONFIGURATION
:
1650 if (ctrl
->bRequestType
!= 0)
1652 if (gadget_is_otg(gadget
)) {
1653 if (gadget
->a_hnp_support
)
1654 DBG(cdev
, "HNP available\n");
1655 else if (gadget
->a_alt_hnp_support
)
1656 DBG(cdev
, "HNP on another port\n");
1658 VDBG(cdev
, "HNP inactive\n");
1660 spin_lock(&cdev
->lock
);
1661 value
= set_config(cdev
, ctrl
, w_value
);
1662 spin_unlock(&cdev
->lock
);
1664 case USB_REQ_GET_CONFIGURATION
:
1665 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1668 *(u8
*)req
->buf
= cdev
->config
->bConfigurationValue
;
1670 *(u8
*)req
->buf
= 0;
1671 value
= min(w_length
, (u16
) 1);
1674 /* function drivers must handle get/set altsetting; if there's
1675 * no get() method, we know only altsetting zero works.
1677 case USB_REQ_SET_INTERFACE
:
1678 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
1680 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1682 f
= cdev
->config
->interface
[intf
];
1685 if (w_value
&& !f
->set_alt
)
1687 value
= f
->set_alt(f
, w_index
, w_value
);
1688 if (value
== USB_GADGET_DELAYED_STATUS
) {
1690 "%s: interface %d (%s) requested delayed status\n",
1691 __func__
, intf
, f
->name
);
1692 cdev
->delayed_status
++;
1693 DBG(cdev
, "delayed_status count %d\n",
1694 cdev
->delayed_status
);
1697 case USB_REQ_GET_INTERFACE
:
1698 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1700 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1702 f
= cdev
->config
->interface
[intf
];
1705 /* lots of interfaces only need altsetting zero... */
1706 value
= f
->get_alt
? f
->get_alt(f
, w_index
) : 0;
1709 *((u8
*)req
->buf
) = value
;
1710 value
= min(w_length
, (u16
) 1);
1712 case USB_REQ_GET_STATUS
:
1713 if (gadget_is_otg(gadget
) && gadget
->hnp_polling_support
&&
1714 (w_index
== OTG_STS_SELECTOR
)) {
1715 if (ctrl
->bRequestType
!= (USB_DIR_IN
|
1718 *((u8
*)req
->buf
) = gadget
->host_request_flag
;
1724 * USB 3.0 additions:
1725 * Function driver should handle get_status request. If such cb
1726 * wasn't supplied we respond with default value = 0
1727 * Note: function driver should supply such cb only for the
1728 * first interface of the function
1730 if (!gadget_is_superspeed(gadget
))
1732 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_RECIP_INTERFACE
))
1734 value
= 2; /* This is the length of the get_status reply */
1735 put_unaligned_le16(0, req
->buf
);
1736 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1738 f
= cdev
->config
->interface
[intf
];
1741 status
= f
->get_status
? f
->get_status(f
) : 0;
1744 put_unaligned_le16(status
& 0x0000ffff, req
->buf
);
1747 * Function drivers should handle SetFeature/ClearFeature
1748 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1749 * only for the first interface of the function
1751 case USB_REQ_CLEAR_FEATURE
:
1752 case USB_REQ_SET_FEATURE
:
1753 if (!gadget_is_superspeed(gadget
))
1755 if (ctrl
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_INTERFACE
))
1758 case USB_INTRF_FUNC_SUSPEND
:
1759 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1761 f
= cdev
->config
->interface
[intf
];
1765 if (f
->func_suspend
)
1766 value
= f
->func_suspend(f
, w_index
>> 8);
1769 "func_suspend() returned error %d\n",
1779 * OS descriptors handling
1781 if (cdev
->use_os_string
&& cdev
->os_desc_config
&&
1782 (ctrl
->bRequestType
& USB_TYPE_VENDOR
) &&
1783 ctrl
->bRequest
== cdev
->b_vendor_code
) {
1784 struct usb_request
*req
;
1785 struct usb_configuration
*os_desc_cfg
;
1790 req
= cdev
->os_desc_req
;
1791 req
->context
= cdev
;
1792 req
->complete
= composite_setup_complete
;
1794 os_desc_cfg
= cdev
->os_desc_config
;
1795 memset(buf
, 0, w_length
);
1797 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1798 case USB_RECIP_DEVICE
:
1799 if (w_index
!= 0x4 || (w_value
>> 8))
1802 if (w_length
== 0x10) {
1803 /* Number of ext compat interfaces */
1804 count
= count_ext_compat(os_desc_cfg
);
1806 count
*= 24; /* 24 B/ext compat desc */
1807 count
+= 16; /* header */
1808 put_unaligned_le32(count
, buf
);
1811 /* "extended compatibility ID"s */
1812 count
= count_ext_compat(os_desc_cfg
);
1814 count
*= 24; /* 24 B/ext compat desc */
1815 count
+= 16; /* header */
1816 put_unaligned_le32(count
, buf
);
1818 fill_ext_compat(os_desc_cfg
, buf
);
1822 case USB_RECIP_INTERFACE
:
1823 if (w_index
!= 0x5 || (w_value
>> 8))
1825 interface
= w_value
& 0xFF;
1827 if (w_length
== 0x0A) {
1828 count
= count_ext_prop(os_desc_cfg
,
1830 put_unaligned_le16(count
, buf
+ 8);
1831 count
= len_ext_prop(os_desc_cfg
,
1833 put_unaligned_le32(count
, buf
);
1837 count
= count_ext_prop(os_desc_cfg
,
1839 put_unaligned_le16(count
, buf
+ 8);
1840 count
= len_ext_prop(os_desc_cfg
,
1842 put_unaligned_le32(count
, buf
);
1844 value
= fill_ext_prop(os_desc_cfg
,
1853 req
->length
= value
;
1854 req
->context
= cdev
;
1855 req
->zero
= value
< w_length
;
1856 value
= composite_ep0_queue(cdev
, req
, GFP_ATOMIC
);
1858 DBG(cdev
, "ep_queue --> %d\n", value
);
1860 composite_setup_complete(gadget
->ep0
, req
);
1866 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1867 ctrl
->bRequestType
, ctrl
->bRequest
,
1868 w_value
, w_index
, w_length
);
1870 /* functions always handle their interfaces and endpoints...
1871 * punt other recipients (other, WUSB, ...) to the current
1872 * configuration code.
1874 * REVISIT it could make sense to let the composite device
1875 * take such requests too, if that's ever needed: to work
1879 list_for_each_entry(f
, &cdev
->config
->functions
, list
)
1880 if (f
->req_match
&& f
->req_match(f
, ctrl
))
1885 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1886 case USB_RECIP_INTERFACE
:
1887 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1889 f
= cdev
->config
->interface
[intf
];
1892 case USB_RECIP_ENDPOINT
:
1893 endp
= ((w_index
& 0x80) >> 3) | (w_index
& 0x0f);
1894 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1895 if (test_bit(endp
, f
->endpoints
))
1898 if (&f
->list
== &cdev
->config
->functions
)
1904 value
= f
->setup(f
, ctrl
);
1906 struct usb_configuration
*c
;
1912 /* try current config's setup */
1914 value
= c
->setup(c
, ctrl
);
1918 /* try the only function in the current config */
1919 if (!list_is_singular(&c
->functions
))
1921 f
= list_first_entry(&c
->functions
, struct usb_function
,
1924 value
= f
->setup(f
, ctrl
);
1930 /* respond with data transfer before status phase? */
1931 if (value
>= 0 && value
!= USB_GADGET_DELAYED_STATUS
) {
1932 req
->length
= value
;
1933 req
->context
= cdev
;
1934 req
->zero
= value
< w_length
;
1935 value
= composite_ep0_queue(cdev
, req
, GFP_ATOMIC
);
1937 DBG(cdev
, "ep_queue --> %d\n", value
);
1939 composite_setup_complete(gadget
->ep0
, req
);
1941 } else if (value
== USB_GADGET_DELAYED_STATUS
&& w_length
!= 0) {
1943 "%s: Delayed status not supported for w_length != 0",
1948 /* device either stalls (value < 0) or reports success */
1952 void composite_disconnect(struct usb_gadget
*gadget
)
1954 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1955 unsigned long flags
;
1957 /* REVISIT: should we have config and device level
1958 * disconnect callbacks?
1960 spin_lock_irqsave(&cdev
->lock
, flags
);
1963 if (cdev
->driver
->disconnect
)
1964 cdev
->driver
->disconnect(cdev
);
1965 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1968 /*-------------------------------------------------------------------------*/
1970 static ssize_t
suspended_show(struct device
*dev
, struct device_attribute
*attr
,
1973 struct usb_gadget
*gadget
= dev_to_usb_gadget(dev
);
1974 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1976 return sprintf(buf
, "%d\n", cdev
->suspended
);
1978 static DEVICE_ATTR_RO(suspended
);
1980 static void __composite_unbind(struct usb_gadget
*gadget
, bool unbind_driver
)
1982 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1984 /* composite_disconnect() must already have been called
1985 * by the underlying peripheral controller driver!
1986 * so there's no i/o concurrency that could affect the
1987 * state protected by cdev->lock.
1989 WARN_ON(cdev
->config
);
1991 while (!list_empty(&cdev
->configs
)) {
1992 struct usb_configuration
*c
;
1993 c
= list_first_entry(&cdev
->configs
,
1994 struct usb_configuration
, list
);
1995 remove_config(cdev
, c
);
1997 if (cdev
->driver
->unbind
&& unbind_driver
)
1998 cdev
->driver
->unbind(cdev
);
2000 composite_dev_cleanup(cdev
);
2002 kfree(cdev
->def_manufacturer
);
2004 set_gadget_data(gadget
, NULL
);
2007 static void composite_unbind(struct usb_gadget
*gadget
)
2009 __composite_unbind(gadget
, true);
2012 static void update_unchanged_dev_desc(struct usb_device_descriptor
*new,
2013 const struct usb_device_descriptor
*old
)
2023 * these variables may have been set in
2024 * usb_composite_overwrite_options()
2026 idVendor
= new->idVendor
;
2027 idProduct
= new->idProduct
;
2028 bcdDevice
= new->bcdDevice
;
2029 iSerialNumber
= new->iSerialNumber
;
2030 iManufacturer
= new->iManufacturer
;
2031 iProduct
= new->iProduct
;
2035 new->idVendor
= idVendor
;
2037 new->idProduct
= idProduct
;
2039 new->bcdDevice
= bcdDevice
;
2041 new->bcdDevice
= cpu_to_le16(get_default_bcdDevice());
2043 new->iSerialNumber
= iSerialNumber
;
2045 new->iManufacturer
= iManufacturer
;
2047 new->iProduct
= iProduct
;
2050 int composite_dev_prepare(struct usb_composite_driver
*composite
,
2051 struct usb_composite_dev
*cdev
)
2053 struct usb_gadget
*gadget
= cdev
->gadget
;
2056 /* preallocate control response and buffer */
2057 cdev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
2061 cdev
->req
->buf
= kmalloc(USB_COMP_EP0_BUFSIZ
, GFP_KERNEL
);
2062 if (!cdev
->req
->buf
)
2065 ret
= device_create_file(&gadget
->dev
, &dev_attr_suspended
);
2069 cdev
->req
->complete
= composite_setup_complete
;
2070 cdev
->req
->context
= cdev
;
2071 gadget
->ep0
->driver_data
= cdev
;
2073 cdev
->driver
= composite
;
2076 * As per USB compliance update, a device that is actively drawing
2077 * more than 100mA from USB must report itself as bus-powered in
2078 * the GetStatus(DEVICE) call.
2080 if (CONFIG_USB_GADGET_VBUS_DRAW
<= USB_SELF_POWER_VBUS_MAX_DRAW
)
2081 usb_gadget_set_selfpowered(gadget
);
2083 /* interface and string IDs start at zero via kzalloc.
2084 * we force endpoints to start unassigned; few controller
2085 * drivers will zero ep->driver_data.
2087 usb_ep_autoconfig_reset(gadget
);
2090 kfree(cdev
->req
->buf
);
2092 usb_ep_free_request(gadget
->ep0
, cdev
->req
);
2097 int composite_os_desc_req_prepare(struct usb_composite_dev
*cdev
,
2102 cdev
->os_desc_req
= usb_ep_alloc_request(ep0
, GFP_KERNEL
);
2103 if (!cdev
->os_desc_req
) {
2104 ret
= PTR_ERR(cdev
->os_desc_req
);
2108 /* OS feature descriptor length <= 4kB */
2109 cdev
->os_desc_req
->buf
= kmalloc(4096, GFP_KERNEL
);
2110 if (!cdev
->os_desc_req
->buf
) {
2111 ret
= PTR_ERR(cdev
->os_desc_req
->buf
);
2112 kfree(cdev
->os_desc_req
);
2115 cdev
->os_desc_req
->context
= cdev
;
2116 cdev
->os_desc_req
->complete
= composite_setup_complete
;
2121 void composite_dev_cleanup(struct usb_composite_dev
*cdev
)
2123 struct usb_gadget_string_container
*uc
, *tmp
;
2125 list_for_each_entry_safe(uc
, tmp
, &cdev
->gstrings
, list
) {
2126 list_del(&uc
->list
);
2129 if (cdev
->os_desc_req
) {
2130 if (cdev
->os_desc_pending
)
2131 usb_ep_dequeue(cdev
->gadget
->ep0
, cdev
->os_desc_req
);
2133 kfree(cdev
->os_desc_req
->buf
);
2134 usb_ep_free_request(cdev
->gadget
->ep0
, cdev
->os_desc_req
);
2137 if (cdev
->setup_pending
)
2138 usb_ep_dequeue(cdev
->gadget
->ep0
, cdev
->req
);
2140 kfree(cdev
->req
->buf
);
2141 usb_ep_free_request(cdev
->gadget
->ep0
, cdev
->req
);
2143 cdev
->next_string_id
= 0;
2144 device_remove_file(&cdev
->gadget
->dev
, &dev_attr_suspended
);
2147 static int composite_bind(struct usb_gadget
*gadget
,
2148 struct usb_gadget_driver
*gdriver
)
2150 struct usb_composite_dev
*cdev
;
2151 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
2152 int status
= -ENOMEM
;
2154 cdev
= kzalloc(sizeof *cdev
, GFP_KERNEL
);
2158 spin_lock_init(&cdev
->lock
);
2159 cdev
->gadget
= gadget
;
2160 set_gadget_data(gadget
, cdev
);
2161 INIT_LIST_HEAD(&cdev
->configs
);
2162 INIT_LIST_HEAD(&cdev
->gstrings
);
2164 status
= composite_dev_prepare(composite
, cdev
);
2168 /* composite gadget needs to assign strings for whole device (like
2169 * serial number), register function drivers, potentially update
2170 * power state and consumption, etc
2172 status
= composite
->bind(cdev
);
2176 if (cdev
->use_os_string
) {
2177 status
= composite_os_desc_req_prepare(cdev
, gadget
->ep0
);
2182 update_unchanged_dev_desc(&cdev
->desc
, composite
->dev
);
2184 /* has userspace failed to provide a serial number? */
2185 if (composite
->needs_serial
&& !cdev
->desc
.iSerialNumber
)
2186 WARNING(cdev
, "userspace failed to provide iSerialNumber\n");
2188 INFO(cdev
, "%s ready\n", composite
->name
);
2192 __composite_unbind(gadget
, false);
2196 /*-------------------------------------------------------------------------*/
2198 void composite_suspend(struct usb_gadget
*gadget
)
2200 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2201 struct usb_function
*f
;
2203 /* REVISIT: should we have config level
2204 * suspend/resume callbacks?
2206 DBG(cdev
, "suspend\n");
2208 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
2213 if (cdev
->driver
->suspend
)
2214 cdev
->driver
->suspend(cdev
);
2216 cdev
->suspended
= 1;
2218 usb_gadget_vbus_draw(gadget
, 2);
2221 void composite_resume(struct usb_gadget
*gadget
)
2223 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2224 struct usb_function
*f
;
2227 /* REVISIT: should we have config level
2228 * suspend/resume callbacks?
2230 DBG(cdev
, "resume\n");
2231 if (cdev
->driver
->resume
)
2232 cdev
->driver
->resume(cdev
);
2234 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
2239 maxpower
= cdev
->config
->MaxPower
;
2241 usb_gadget_vbus_draw(gadget
, maxpower
?
2242 maxpower
: CONFIG_USB_GADGET_VBUS_DRAW
);
2245 cdev
->suspended
= 0;
2248 /*-------------------------------------------------------------------------*/
2250 static const struct usb_gadget_driver composite_driver_template
= {
2251 .bind
= composite_bind
,
2252 .unbind
= composite_unbind
,
2254 .setup
= composite_setup
,
2255 .reset
= composite_disconnect
,
2256 .disconnect
= composite_disconnect
,
2258 .suspend
= composite_suspend
,
2259 .resume
= composite_resume
,
2262 .owner
= THIS_MODULE
,
2267 * usb_composite_probe() - register a composite driver
2268 * @driver: the driver to register
2270 * Context: single threaded during gadget setup
2272 * This function is used to register drivers using the composite driver
2273 * framework. The return value is zero, or a negative errno value.
2274 * Those values normally come from the driver's @bind method, which does
2275 * all the work of setting up the driver to match the hardware.
2277 * On successful return, the gadget is ready to respond to requests from
2278 * the host, unless one of its components invokes usb_gadget_disconnect()
2279 * while it was binding. That would usually be done in order to wait for
2280 * some userspace participation.
2282 int usb_composite_probe(struct usb_composite_driver
*driver
)
2284 struct usb_gadget_driver
*gadget_driver
;
2286 if (!driver
|| !driver
->dev
|| !driver
->bind
)
2290 driver
->name
= "composite";
2292 driver
->gadget_driver
= composite_driver_template
;
2293 gadget_driver
= &driver
->gadget_driver
;
2295 gadget_driver
->function
= (char *) driver
->name
;
2296 gadget_driver
->driver
.name
= driver
->name
;
2297 gadget_driver
->max_speed
= driver
->max_speed
;
2299 return usb_gadget_probe_driver(gadget_driver
);
2301 EXPORT_SYMBOL_GPL(usb_composite_probe
);
2304 * usb_composite_unregister() - unregister a composite driver
2305 * @driver: the driver to unregister
2307 * This function is used to unregister drivers using the composite
2310 void usb_composite_unregister(struct usb_composite_driver
*driver
)
2312 usb_gadget_unregister_driver(&driver
->gadget_driver
);
2314 EXPORT_SYMBOL_GPL(usb_composite_unregister
);
2317 * usb_composite_setup_continue() - Continue with the control transfer
2318 * @cdev: the composite device who's control transfer was kept waiting
2320 * This function must be called by the USB function driver to continue
2321 * with the control transfer's data/status stage in case it had requested to
2322 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2323 * can request the composite framework to delay the setup request's data/status
2324 * stages by returning USB_GADGET_DELAYED_STATUS.
2326 void usb_composite_setup_continue(struct usb_composite_dev
*cdev
)
2329 struct usb_request
*req
= cdev
->req
;
2330 unsigned long flags
;
2332 DBG(cdev
, "%s\n", __func__
);
2333 spin_lock_irqsave(&cdev
->lock
, flags
);
2335 if (cdev
->delayed_status
== 0) {
2336 WARN(cdev
, "%s: Unexpected call\n", __func__
);
2338 } else if (--cdev
->delayed_status
== 0) {
2339 DBG(cdev
, "%s: Completing delayed status\n", __func__
);
2341 req
->context
= cdev
;
2342 value
= composite_ep0_queue(cdev
, req
, GFP_ATOMIC
);
2344 DBG(cdev
, "ep_queue --> %d\n", value
);
2346 composite_setup_complete(cdev
->gadget
->ep0
, req
);
2350 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2352 EXPORT_SYMBOL_GPL(usb_composite_setup_continue
);
2354 static char *composite_default_mfr(struct usb_gadget
*gadget
)
2359 len
= snprintf(NULL
, 0, "%s %s with %s", init_utsname()->sysname
,
2360 init_utsname()->release
, gadget
->name
);
2362 mfr
= kmalloc(len
, GFP_KERNEL
);
2365 snprintf(mfr
, len
, "%s %s with %s", init_utsname()->sysname
,
2366 init_utsname()->release
, gadget
->name
);
2370 void usb_composite_overwrite_options(struct usb_composite_dev
*cdev
,
2371 struct usb_composite_overwrite
*covr
)
2373 struct usb_device_descriptor
*desc
= &cdev
->desc
;
2374 struct usb_gadget_strings
*gstr
= cdev
->driver
->strings
[0];
2375 struct usb_string
*dev_str
= gstr
->strings
;
2378 desc
->idVendor
= cpu_to_le16(covr
->idVendor
);
2380 if (covr
->idProduct
)
2381 desc
->idProduct
= cpu_to_le16(covr
->idProduct
);
2383 if (covr
->bcdDevice
)
2384 desc
->bcdDevice
= cpu_to_le16(covr
->bcdDevice
);
2386 if (covr
->serial_number
) {
2387 desc
->iSerialNumber
= dev_str
[USB_GADGET_SERIAL_IDX
].id
;
2388 dev_str
[USB_GADGET_SERIAL_IDX
].s
= covr
->serial_number
;
2390 if (covr
->manufacturer
) {
2391 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
2392 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= covr
->manufacturer
;
2394 } else if (!strlen(dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
)) {
2395 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
2396 cdev
->def_manufacturer
= composite_default_mfr(cdev
->gadget
);
2397 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= cdev
->def_manufacturer
;
2400 if (covr
->product
) {
2401 desc
->iProduct
= dev_str
[USB_GADGET_PRODUCT_IDX
].id
;
2402 dev_str
[USB_GADGET_PRODUCT_IDX
].s
= covr
->product
;
2405 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options
);
2407 MODULE_LICENSE("GPL");
2408 MODULE_AUTHOR("David Brownell");