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"
26 #define SSUSB_GADGET_VBUS_DRAW 900 /* in mA */
27 #define SSUSB_GADGET_VBUS_DRAW_UNITS 8
28 #define HSUSB_GADGET_VBUS_DRAW_UNITS 2
31 * Based on enumerated USB speed, draw power with set_config and resume
32 * HSUSB: 500mA, SSUSB: 900mA
34 #define USB_VBUS_DRAW(speed)\
35 (speed == USB_SPEED_SUPER ?\
36 SSUSB_GADGET_VBUS_DRAW : CONFIG_USB_GADGET_VBUS_DRAW)
39 * struct usb_os_string - represents OS String to be reported by a gadget
40 * @bLength: total length of the entire descritor, always 0x12
41 * @bDescriptorType: USB_DT_STRING
42 * @qwSignature: the OS String proper
43 * @bMS_VendorCode: code used by the host for subsequent requests
44 * @bPad: not used, must be zero
46 struct usb_os_string
{
49 __u8 qwSignature
[OS_STRING_QW_SIGN_LEN
];
55 * The code in this file is utility code, used to build a gadget driver
56 * from one or more "function" drivers, one or more "configuration"
57 * objects, and a "usb_composite_driver" by gluing them together along
58 * with the relevant device-wide data.
61 static struct usb_gadget_strings
**get_containers_gs(
62 struct usb_gadget_string_container
*uc
)
64 return (struct usb_gadget_strings
**)uc
->stash
;
68 * function_descriptors() - get function descriptors for speed
72 * Returns the descriptors or NULL if not set.
74 static struct usb_descriptor_header
**
75 function_descriptors(struct usb_function
*f
,
76 enum usb_device_speed speed
)
78 struct usb_descriptor_header
**descriptors
;
81 * NOTE: we try to help gadget drivers which might not be setting
82 * max_speed appropriately.
86 case USB_SPEED_SUPER_PLUS
:
87 descriptors
= f
->ssp_descriptors
;
92 descriptors
= f
->ss_descriptors
;
97 descriptors
= f
->hs_descriptors
;
102 descriptors
= f
->fs_descriptors
;
106 * if we can't find any descriptors at all, then this gadget deserves to
107 * Oops with a NULL pointer dereference
114 * next_ep_desc() - advance to the next EP descriptor
115 * @t: currect pointer within descriptor array
117 * Return: next EP descriptor or NULL
119 * Iterate over @t until either EP descriptor found or
120 * NULL (that indicates end of list) encountered
122 static struct usb_descriptor_header
**
123 next_ep_desc(struct usb_descriptor_header
**t
)
126 if ((*t
)->bDescriptorType
== USB_DT_ENDPOINT
)
133 * for_each_ep_desc()- iterate over endpoint descriptors in the
135 * @start: pointer within descriptor array.
136 * @ep_desc: endpoint descriptor to use as the loop cursor
138 #define for_each_ep_desc(start, ep_desc) \
139 for (ep_desc = next_ep_desc(start); \
140 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
143 * config_ep_by_speed() - configures the given endpoint
144 * according to gadget speed.
145 * @g: pointer to the gadget
147 * @_ep: the endpoint to configure
149 * Return: error code, 0 on success
151 * This function chooses the right descriptors for a given
152 * endpoint according to gadget speed and saves it in the
153 * endpoint desc field. If the endpoint already has a descriptor
154 * assigned to it - overwrites it with currently corresponding
155 * descriptor. The endpoint maxpacket field is updated according
156 * to the chosen descriptor.
157 * Note: the supplied function should hold all the descriptors
158 * for supported speeds
160 int config_ep_by_speed(struct usb_gadget
*g
,
161 struct usb_function
*f
,
164 struct usb_endpoint_descriptor
*chosen_desc
= NULL
;
165 struct usb_descriptor_header
**speed_desc
= NULL
;
167 struct usb_ss_ep_comp_descriptor
*comp_desc
= NULL
;
168 int want_comp_desc
= 0;
170 struct usb_descriptor_header
**d_spd
; /* cursor for speed desc */
172 if (!g
|| !f
|| !_ep
)
175 /* select desired speed */
177 case USB_SPEED_SUPER_PLUS
:
178 if (gadget_is_superspeed_plus(g
)) {
179 speed_desc
= f
->ssp_descriptors
;
183 /* else: Fall trough */
184 case USB_SPEED_SUPER
:
185 if (gadget_is_superspeed(g
)) {
186 speed_desc
= f
->ss_descriptors
;
190 /* else: Fall trough */
192 if (gadget_is_dualspeed(g
)) {
193 speed_desc
= f
->hs_descriptors
;
196 /* else: fall through */
198 speed_desc
= f
->fs_descriptors
;
200 /* find descriptors */
201 for_each_ep_desc(speed_desc
, d_spd
) {
202 chosen_desc
= (struct usb_endpoint_descriptor
*)*d_spd
;
203 if (chosen_desc
->bEndpointAddress
== _ep
->address
)
210 _ep
->maxpacket
= usb_endpoint_maxp(chosen_desc
);
211 _ep
->desc
= chosen_desc
;
212 _ep
->comp_desc
= NULL
;
216 if (g
->speed
== USB_SPEED_HIGH
&& (usb_endpoint_xfer_isoc(_ep
->desc
) ||
217 usb_endpoint_xfer_int(_ep
->desc
)))
218 _ep
->mult
= usb_endpoint_maxp_mult(_ep
->desc
);
224 * Companion descriptor should follow EP descriptor
225 * USB 3.0 spec, #9.6.7
227 comp_desc
= (struct usb_ss_ep_comp_descriptor
*)*(++d_spd
);
229 (comp_desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
))
231 _ep
->comp_desc
= comp_desc
;
232 if (g
->speed
>= USB_SPEED_SUPER
) {
233 switch (usb_endpoint_type(_ep
->desc
)) {
234 case USB_ENDPOINT_XFER_ISOC
:
235 /* mult: bits 1:0 of bmAttributes */
236 _ep
->mult
= (comp_desc
->bmAttributes
& 0x3) + 1;
237 case USB_ENDPOINT_XFER_BULK
:
238 case USB_ENDPOINT_XFER_INT
:
239 _ep
->maxburst
= comp_desc
->bMaxBurst
+ 1;
242 if (comp_desc
->bMaxBurst
!= 0) {
243 struct usb_composite_dev
*cdev
;
245 cdev
= get_gadget_data(g
);
246 ERROR(cdev
, "ep0 bMaxBurst must be 0\n");
254 EXPORT_SYMBOL_GPL(config_ep_by_speed
);
257 * usb_add_function() - add a function to a configuration
258 * @config: the configuration
259 * @function: the function being added
260 * Context: single threaded during gadget setup
262 * After initialization, each configuration must have one or more
263 * functions added to it. Adding a function involves calling its @bind()
264 * method to allocate resources such as interface and string identifiers
267 * This function returns the value of the function's bind(), which is
268 * zero for success else a negative errno value.
270 int usb_add_function(struct usb_configuration
*config
,
271 struct usb_function
*function
)
275 DBG(config
->cdev
, "adding '%s'/%p to config '%s'/%p\n",
276 function
->name
, function
,
277 config
->label
, config
);
279 if (!function
->set_alt
|| !function
->disable
)
282 function
->config
= config
;
283 list_add_tail(&function
->list
, &config
->functions
);
285 if (function
->bind_deactivated
) {
286 value
= usb_function_deactivate(function
);
291 /* REVISIT *require* function->bind? */
292 if (function
->bind
) {
293 value
= function
->bind(config
, function
);
295 list_del(&function
->list
);
296 function
->config
= NULL
;
301 /* We allow configurations that don't work at both speeds.
302 * If we run into a lowspeed Linux system, treat it the same
303 * as full speed ... it's the function drivers that will need
304 * to avoid bulk and ISO transfers.
306 if (!config
->fullspeed
&& function
->fs_descriptors
)
307 config
->fullspeed
= true;
308 if (!config
->highspeed
&& function
->hs_descriptors
)
309 config
->highspeed
= true;
310 if (!config
->superspeed
&& function
->ss_descriptors
)
311 config
->superspeed
= true;
312 if (!config
->superspeed_plus
&& function
->ssp_descriptors
)
313 config
->superspeed_plus
= true;
317 DBG(config
->cdev
, "adding '%s'/%p --> %d\n",
318 function
->name
, function
, value
);
321 EXPORT_SYMBOL_GPL(usb_add_function
);
323 void usb_remove_function(struct usb_configuration
*c
, struct usb_function
*f
)
328 bitmap_zero(f
->endpoints
, 32);
333 if (f
->bind_deactivated
)
334 usb_function_activate(f
);
336 EXPORT_SYMBOL_GPL(usb_remove_function
);
339 * usb_function_deactivate - prevent function and gadget enumeration
340 * @function: the function that isn't yet ready to respond
342 * Blocks response of the gadget driver to host enumeration by
343 * preventing the data line pullup from being activated. This is
344 * normally called during @bind() processing to change from the
345 * initial "ready to respond" state, or when a required resource
348 * For example, drivers that serve as a passthrough to a userspace
349 * daemon can block enumeration unless that daemon (such as an OBEX,
350 * MTP, or print server) is ready to handle host requests.
352 * Not all systems support software control of their USB peripheral
355 * Returns zero on success, else negative errno.
357 int usb_function_deactivate(struct usb_function
*function
)
359 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
363 spin_lock_irqsave(&cdev
->lock
, flags
);
365 if (cdev
->deactivations
== 0)
366 status
= usb_gadget_deactivate(cdev
->gadget
);
368 cdev
->deactivations
++;
370 spin_unlock_irqrestore(&cdev
->lock
, flags
);
373 EXPORT_SYMBOL_GPL(usb_function_deactivate
);
376 * usb_function_activate - allow function and gadget enumeration
377 * @function: function on which usb_function_activate() was called
379 * Reverses effect of usb_function_deactivate(). If no more functions
380 * are delaying their activation, the gadget driver will respond to
381 * host enumeration procedures.
383 * Returns zero on success, else negative errno.
385 int usb_function_activate(struct usb_function
*function
)
387 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
391 spin_lock_irqsave(&cdev
->lock
, flags
);
393 if (WARN_ON(cdev
->deactivations
== 0))
396 cdev
->deactivations
--;
397 if (cdev
->deactivations
== 0)
398 status
= usb_gadget_activate(cdev
->gadget
);
401 spin_unlock_irqrestore(&cdev
->lock
, flags
);
404 EXPORT_SYMBOL_GPL(usb_function_activate
);
407 * usb_interface_id() - allocate an unused interface ID
408 * @config: configuration associated with the interface
409 * @function: function handling the interface
410 * Context: single threaded during gadget setup
412 * usb_interface_id() is called from usb_function.bind() callbacks to
413 * allocate new interface IDs. The function driver will then store that
414 * ID in interface, association, CDC union, and other descriptors. It
415 * will also handle any control requests targeted at that interface,
416 * particularly changing its altsetting via set_alt(). There may
417 * also be class-specific or vendor-specific requests to handle.
419 * All interface identifier should be allocated using this routine, to
420 * ensure that for example different functions don't wrongly assign
421 * different meanings to the same identifier. Note that since interface
422 * identifiers are configuration-specific, functions used in more than
423 * one configuration (or more than once in a given configuration) need
424 * multiple versions of the relevant descriptors.
426 * Returns the interface ID which was allocated; or -ENODEV if no
427 * more interface IDs can be allocated.
429 int usb_interface_id(struct usb_configuration
*config
,
430 struct usb_function
*function
)
432 unsigned id
= config
->next_interface_id
;
434 if (id
< MAX_CONFIG_INTERFACES
) {
435 config
->interface
[id
] = function
;
436 config
->next_interface_id
= id
+ 1;
441 EXPORT_SYMBOL_GPL(usb_interface_id
);
443 static u8
encode_bMaxPower(enum usb_device_speed speed
,
444 struct usb_configuration
*c
)
446 unsigned int val
= CONFIG_USB_GADGET_VBUS_DRAW
;
449 case USB_SPEED_SUPER
:
450 /* with super-speed report 900mA */
451 val
= SSUSB_GADGET_VBUS_DRAW
;
452 return (u8
)(val
/ SSUSB_GADGET_VBUS_DRAW_UNITS
);
454 return DIV_ROUND_UP(val
, HSUSB_GADGET_VBUS_DRAW_UNITS
);
458 static int config_buf(struct usb_configuration
*config
,
459 enum usb_device_speed speed
, void *buf
, u8 type
)
461 struct usb_config_descriptor
*c
= buf
;
462 void *next
= buf
+ USB_DT_CONFIG_SIZE
;
464 struct usb_function
*f
;
467 len
= USB_COMP_EP0_BUFSIZ
- USB_DT_CONFIG_SIZE
;
468 /* write the config descriptor */
470 c
->bLength
= USB_DT_CONFIG_SIZE
;
471 c
->bDescriptorType
= type
;
472 /* wTotalLength is written later */
473 c
->bNumInterfaces
= config
->next_interface_id
;
474 c
->bConfigurationValue
= config
->bConfigurationValue
;
475 c
->iConfiguration
= config
->iConfiguration
;
476 c
->bmAttributes
= USB_CONFIG_ATT_ONE
| config
->bmAttributes
;
477 c
->bMaxPower
= encode_bMaxPower(speed
, config
);
479 /* There may be e.g. OTG descriptors */
480 if (config
->descriptors
) {
481 status
= usb_descriptor_fillbuf(next
, len
,
482 config
->descriptors
);
489 /* add each function's descriptors */
490 list_for_each_entry(f
, &config
->functions
, list
) {
491 struct usb_descriptor_header
**descriptors
;
493 descriptors
= function_descriptors(f
, speed
);
496 status
= usb_descriptor_fillbuf(next
, len
,
497 (const struct usb_descriptor_header
**) descriptors
);
505 c
->wTotalLength
= cpu_to_le16(len
);
509 static int config_desc(struct usb_composite_dev
*cdev
, unsigned w_value
)
511 struct usb_gadget
*gadget
= cdev
->gadget
;
512 struct usb_configuration
*c
;
513 struct list_head
*pos
;
514 u8 type
= w_value
>> 8;
515 enum usb_device_speed speed
= USB_SPEED_UNKNOWN
;
517 if (gadget
->speed
>= USB_SPEED_SUPER
)
518 speed
= gadget
->speed
;
519 else if (gadget_is_dualspeed(gadget
)) {
521 if (gadget
->speed
== USB_SPEED_HIGH
)
523 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
526 speed
= USB_SPEED_HIGH
;
530 /* This is a lookup by config *INDEX* */
533 pos
= &cdev
->configs
;
534 c
= cdev
->os_desc_config
;
538 while ((pos
= pos
->next
) != &cdev
->configs
) {
539 c
= list_entry(pos
, typeof(*c
), list
);
541 /* skip OS Descriptors config which is handled separately */
542 if (c
== cdev
->os_desc_config
)
546 /* ignore configs that won't work at this speed */
548 case USB_SPEED_SUPER_PLUS
:
549 if (!c
->superspeed_plus
)
552 case USB_SPEED_SUPER
:
566 return config_buf(c
, speed
, cdev
->req
->buf
, type
);
572 static int count_configs(struct usb_composite_dev
*cdev
, unsigned type
)
574 struct usb_gadget
*gadget
= cdev
->gadget
;
575 struct usb_configuration
*c
;
581 if (gadget_is_dualspeed(gadget
)) {
582 if (gadget
->speed
== USB_SPEED_HIGH
)
584 if (gadget
->speed
== USB_SPEED_SUPER
)
586 if (gadget
->speed
== USB_SPEED_SUPER_PLUS
)
588 if (type
== USB_DT_DEVICE_QUALIFIER
)
591 list_for_each_entry(c
, &cdev
->configs
, list
) {
592 /* ignore configs that won't work at this speed */
594 if (!c
->superspeed_plus
)
612 * bos_desc() - prepares the BOS descriptor.
613 * @cdev: pointer to usb_composite device to generate the bos
616 * This function generates the BOS (Binary Device Object)
617 * descriptor and its device capabilities descriptors. The BOS
618 * descriptor should be supported by a SuperSpeed device.
620 static int bos_desc(struct usb_composite_dev
*cdev
)
622 struct usb_ext_cap_descriptor
*usb_ext
;
623 struct usb_dcd_config_params dcd_config_params
;
624 struct usb_bos_descriptor
*bos
= cdev
->req
->buf
;
626 bos
->bLength
= USB_DT_BOS_SIZE
;
627 bos
->bDescriptorType
= USB_DT_BOS
;
629 bos
->wTotalLength
= cpu_to_le16(USB_DT_BOS_SIZE
);
630 bos
->bNumDeviceCaps
= 0;
633 * A SuperSpeed device shall include the USB2.0 extension descriptor
634 * and shall support LPM when operating in USB2.0 HS mode.
636 usb_ext
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
637 bos
->bNumDeviceCaps
++;
638 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_EXT_CAP_SIZE
);
639 usb_ext
->bLength
= USB_DT_USB_EXT_CAP_SIZE
;
640 usb_ext
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
641 usb_ext
->bDevCapabilityType
= USB_CAP_TYPE_EXT
;
642 usb_ext
->bmAttributes
= cpu_to_le32(USB_LPM_SUPPORT
| USB_BESL_SUPPORT
);
645 * The Superspeed USB Capability descriptor shall be implemented by all
646 * SuperSpeed devices.
648 if (gadget_is_superspeed(cdev
->gadget
)) {
649 struct usb_ss_cap_descriptor
*ss_cap
;
651 ss_cap
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
652 bos
->bNumDeviceCaps
++;
653 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_SS_CAP_SIZE
);
654 ss_cap
->bLength
= USB_DT_USB_SS_CAP_SIZE
;
655 ss_cap
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
656 ss_cap
->bDevCapabilityType
= USB_SS_CAP_TYPE
;
657 ss_cap
->bmAttributes
= 0; /* LTM is not supported yet */
658 ss_cap
->wSpeedSupported
= cpu_to_le16(USB_LOW_SPEED_OPERATION
|
659 USB_FULL_SPEED_OPERATION
|
660 USB_HIGH_SPEED_OPERATION
|
661 USB_5GBPS_OPERATION
);
662 ss_cap
->bFunctionalitySupport
= USB_LOW_SPEED_OPERATION
;
664 /* Get Controller configuration */
665 if (cdev
->gadget
->ops
->get_config_params
) {
666 cdev
->gadget
->ops
->get_config_params(
669 dcd_config_params
.bU1devExitLat
=
670 USB_DEFAULT_U1_DEV_EXIT_LAT
;
671 dcd_config_params
.bU2DevExitLat
=
672 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT
);
674 ss_cap
->bU1devExitLat
= dcd_config_params
.bU1devExitLat
;
675 ss_cap
->bU2DevExitLat
= dcd_config_params
.bU2DevExitLat
;
678 /* The SuperSpeedPlus USB Device Capability descriptor */
679 if (gadget_is_superspeed_plus(cdev
->gadget
)) {
680 struct usb_ssp_cap_descriptor
*ssp_cap
;
682 ssp_cap
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
683 bos
->bNumDeviceCaps
++;
686 * Report typical values.
689 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_SSP_CAP_SIZE(1));
690 ssp_cap
->bLength
= USB_DT_USB_SSP_CAP_SIZE(1);
691 ssp_cap
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
692 ssp_cap
->bDevCapabilityType
= USB_SSP_CAP_TYPE
;
693 ssp_cap
->bReserved
= 0;
694 ssp_cap
->wReserved
= 0;
696 /* SSAC = 1 (2 attributes) */
697 ssp_cap
->bmAttributes
= cpu_to_le32(1);
699 /* Min RX/TX Lane Count = 1 */
700 ssp_cap
->wFunctionalitySupport
=
701 cpu_to_le16((1 << 8) | (1 << 12));
704 * bmSublinkSpeedAttr[0]:
707 * LP = 1 (SuperSpeedPlus)
710 ssp_cap
->bmSublinkSpeedAttr
[0] =
711 cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
713 * bmSublinkSpeedAttr[1] =
716 * LP = 1 (SuperSpeedPlus)
719 ssp_cap
->bmSublinkSpeedAttr
[1] =
720 cpu_to_le32((3 << 4) | (1 << 14) |
721 (0xa << 16) | (1 << 7));
724 return le16_to_cpu(bos
->wTotalLength
);
727 static void device_qual(struct usb_composite_dev
*cdev
)
729 struct usb_qualifier_descriptor
*qual
= cdev
->req
->buf
;
731 qual
->bLength
= sizeof(*qual
);
732 qual
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
733 /* POLICY: same bcdUSB and device type info at both speeds */
734 qual
->bcdUSB
= cdev
->desc
.bcdUSB
;
735 qual
->bDeviceClass
= cdev
->desc
.bDeviceClass
;
736 qual
->bDeviceSubClass
= cdev
->desc
.bDeviceSubClass
;
737 qual
->bDeviceProtocol
= cdev
->desc
.bDeviceProtocol
;
738 /* ASSUME same EP0 fifo size at both speeds */
739 qual
->bMaxPacketSize0
= cdev
->gadget
->ep0
->maxpacket
;
740 qual
->bNumConfigurations
= count_configs(cdev
, USB_DT_DEVICE_QUALIFIER
);
744 /*-------------------------------------------------------------------------*/
746 static void reset_config(struct usb_composite_dev
*cdev
)
748 struct usb_function
*f
;
750 DBG(cdev
, "reset config\n");
752 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
756 bitmap_zero(f
->endpoints
, 32);
759 cdev
->delayed_status
= 0;
762 static int set_config(struct usb_composite_dev
*cdev
,
763 const struct usb_ctrlrequest
*ctrl
, unsigned number
)
765 struct usb_gadget
*gadget
= cdev
->gadget
;
766 struct usb_configuration
*c
= NULL
;
767 int result
= -EINVAL
;
768 unsigned power
= gadget_is_otg(gadget
) ? 8 : 100;
772 list_for_each_entry(c
, &cdev
->configs
, list
) {
773 if (c
->bConfigurationValue
== number
) {
775 * We disable the FDs of the previous
776 * configuration only if the new configuration
787 } else { /* Zero configuration value - need to reset the config */
793 INFO(cdev
, "%s config #%d: %s\n",
794 usb_speed_string(gadget
->speed
),
795 number
, c
? c
->label
: "unconfigured");
800 usb_gadget_set_state(gadget
, USB_STATE_CONFIGURED
);
803 /* Initialize all interfaces by setting them to altsetting zero. */
804 for (tmp
= 0; tmp
< MAX_CONFIG_INTERFACES
; tmp
++) {
805 struct usb_function
*f
= c
->interface
[tmp
];
806 struct usb_descriptor_header
**descriptors
;
812 * Record which endpoints are used by the function. This is used
813 * to dispatch control requests targeted at that endpoint to the
814 * function's setup callback instead of the current
815 * configuration's setup callback.
817 descriptors
= function_descriptors(f
, gadget
->speed
);
819 for (; *descriptors
; ++descriptors
) {
820 struct usb_endpoint_descriptor
*ep
;
823 if ((*descriptors
)->bDescriptorType
!= USB_DT_ENDPOINT
)
826 ep
= (struct usb_endpoint_descriptor
*)*descriptors
;
827 addr
= ((ep
->bEndpointAddress
& 0x80) >> 3)
828 | (ep
->bEndpointAddress
& 0x0f);
829 set_bit(addr
, f
->endpoints
);
832 result
= f
->set_alt(f
, tmp
, 0);
834 DBG(cdev
, "interface %d (%s/%p) alt 0 --> %d\n",
835 tmp
, f
->name
, f
, result
);
841 if (result
== USB_GADGET_DELAYED_STATUS
) {
843 "%s: interface %d (%s) requested delayed status\n",
844 __func__
, tmp
, f
->name
);
845 cdev
->delayed_status
++;
846 DBG(cdev
, "delayed_status count %d\n",
847 cdev
->delayed_status
);
851 /* when we return, be sure our power usage is valid */
852 power
= c
->MaxPower
? c
->MaxPower
: CONFIG_USB_GADGET_VBUS_DRAW
;
854 usb_gadget_vbus_draw(gadget
, power
);
855 if (result
>= 0 && cdev
->delayed_status
)
856 result
= USB_GADGET_DELAYED_STATUS
;
860 int usb_add_config_only(struct usb_composite_dev
*cdev
,
861 struct usb_configuration
*config
)
863 struct usb_configuration
*c
;
865 if (!config
->bConfigurationValue
)
868 /* Prevent duplicate configuration identifiers */
869 list_for_each_entry(c
, &cdev
->configs
, list
) {
870 if (c
->bConfigurationValue
== config
->bConfigurationValue
)
875 list_add_tail(&config
->list
, &cdev
->configs
);
877 INIT_LIST_HEAD(&config
->functions
);
878 config
->next_interface_id
= 0;
879 memset(config
->interface
, 0, sizeof(config
->interface
));
883 EXPORT_SYMBOL_GPL(usb_add_config_only
);
886 * usb_add_config() - add a configuration to a device.
887 * @cdev: wraps the USB gadget
888 * @config: the configuration, with bConfigurationValue assigned
889 * @bind: the configuration's bind function
890 * Context: single threaded during gadget setup
892 * One of the main tasks of a composite @bind() routine is to
893 * add each of the configurations it supports, using this routine.
895 * This function returns the value of the configuration's @bind(), which
896 * is zero for success else a negative errno value. Binding configurations
897 * assigns global resources including string IDs, and per-configuration
898 * resources such as interface IDs and endpoints.
900 int usb_add_config(struct usb_composite_dev
*cdev
,
901 struct usb_configuration
*config
,
902 int (*bind
)(struct usb_configuration
*))
904 int status
= -EINVAL
;
909 DBG(cdev
, "adding config #%u '%s'/%p\n",
910 config
->bConfigurationValue
,
911 config
->label
, config
);
913 status
= usb_add_config_only(cdev
, config
);
917 status
= bind(config
);
919 while (!list_empty(&config
->functions
)) {
920 struct usb_function
*f
;
922 f
= list_first_entry(&config
->functions
,
923 struct usb_function
, list
);
926 DBG(cdev
, "unbind function '%s'/%p\n",
928 f
->unbind(config
, f
);
929 /* may free memory for "f" */
932 list_del(&config
->list
);
937 DBG(cdev
, "cfg %d/%p speeds:%s%s%s%s\n",
938 config
->bConfigurationValue
, config
,
939 config
->superspeed_plus
? " superplus" : "",
940 config
->superspeed
? " super" : "",
941 config
->highspeed
? " high" : "",
943 ? (gadget_is_dualspeed(cdev
->gadget
)
948 for (i
= 0; i
< MAX_CONFIG_INTERFACES
; i
++) {
949 struct usb_function
*f
= config
->interface
[i
];
953 DBG(cdev
, " interface %d = %s/%p\n",
958 /* set_alt(), or next bind(), sets up ep->claimed as needed */
959 usb_ep_autoconfig_reset(cdev
->gadget
);
963 DBG(cdev
, "added config '%s'/%u --> %d\n", config
->label
,
964 config
->bConfigurationValue
, status
);
967 EXPORT_SYMBOL_GPL(usb_add_config
);
969 static void remove_config(struct usb_composite_dev
*cdev
,
970 struct usb_configuration
*config
)
972 while (!list_empty(&config
->functions
)) {
973 struct usb_function
*f
;
975 f
= list_first_entry(&config
->functions
,
976 struct usb_function
, list
);
978 usb_remove_function(config
, f
);
980 list_del(&config
->list
);
981 if (config
->unbind
) {
982 DBG(cdev
, "unbind config '%s'/%p\n", config
->label
, config
);
983 config
->unbind(config
);
984 /* may free memory for "c" */
989 * usb_remove_config() - remove a configuration from a device.
990 * @cdev: wraps the USB gadget
991 * @config: the configuration
993 * Drivers must call usb_gadget_disconnect before calling this function
994 * to disconnect the device from the host and make sure the host will not
995 * try to enumerate the device while we are changing the config list.
997 void usb_remove_config(struct usb_composite_dev
*cdev
,
998 struct usb_configuration
*config
)
1000 unsigned long flags
;
1002 spin_lock_irqsave(&cdev
->lock
, flags
);
1004 if (cdev
->config
== config
)
1007 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1009 remove_config(cdev
, config
);
1012 /*-------------------------------------------------------------------------*/
1014 /* We support strings in multiple languages ... string descriptor zero
1015 * says which languages are supported. The typical case will be that
1016 * only one language (probably English) is used, with i18n handled on
1020 static void collect_langs(struct usb_gadget_strings
**sp
, __le16
*buf
)
1022 const struct usb_gadget_strings
*s
;
1028 language
= cpu_to_le16(s
->language
);
1029 for (tmp
= buf
; *tmp
&& tmp
< &buf
[126]; tmp
++) {
1030 if (*tmp
== language
)
1039 static int lookup_string(
1040 struct usb_gadget_strings
**sp
,
1046 struct usb_gadget_strings
*s
;
1051 if (s
->language
!= language
)
1053 value
= usb_gadget_get_string(s
, id
, buf
);
1060 static int get_string(struct usb_composite_dev
*cdev
,
1061 void *buf
, u16 language
, int id
)
1063 struct usb_composite_driver
*composite
= cdev
->driver
;
1064 struct usb_gadget_string_container
*uc
;
1065 struct usb_configuration
*c
;
1066 struct usb_function
*f
;
1069 /* Yes, not only is USB's i18n support probably more than most
1070 * folk will ever care about ... also, it's all supported here.
1071 * (Except for UTF8 support for Unicode's "Astral Planes".)
1074 /* 0 == report all available language codes */
1076 struct usb_string_descriptor
*s
= buf
;
1077 struct usb_gadget_strings
**sp
;
1080 s
->bDescriptorType
= USB_DT_STRING
;
1082 sp
= composite
->strings
;
1084 collect_langs(sp
, s
->wData
);
1086 list_for_each_entry(c
, &cdev
->configs
, list
) {
1089 collect_langs(sp
, s
->wData
);
1091 list_for_each_entry(f
, &c
->functions
, list
) {
1094 collect_langs(sp
, s
->wData
);
1097 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
1098 struct usb_gadget_strings
**sp
;
1100 sp
= get_containers_gs(uc
);
1101 collect_langs(sp
, s
->wData
);
1104 for (len
= 0; len
<= 126 && s
->wData
[len
]; len
++)
1109 s
->bLength
= 2 * (len
+ 1);
1113 if (cdev
->use_os_string
&& language
== 0 && id
== OS_STRING_IDX
) {
1114 struct usb_os_string
*b
= buf
;
1115 b
->bLength
= sizeof(*b
);
1116 b
->bDescriptorType
= USB_DT_STRING
;
1118 sizeof(b
->qwSignature
) == sizeof(cdev
->qw_sign
),
1119 "qwSignature size must be equal to qw_sign");
1120 memcpy(&b
->qwSignature
, cdev
->qw_sign
, sizeof(b
->qwSignature
));
1121 b
->bMS_VendorCode
= cdev
->b_vendor_code
;
1126 list_for_each_entry(uc
, &cdev
->gstrings
, list
) {
1127 struct usb_gadget_strings
**sp
;
1129 sp
= get_containers_gs(uc
);
1130 len
= lookup_string(sp
, buf
, language
, id
);
1135 /* String IDs are device-scoped, so we look up each string
1136 * table we're told about. These lookups are infrequent;
1137 * simpler-is-better here.
1139 if (composite
->strings
) {
1140 len
= lookup_string(composite
->strings
, buf
, language
, id
);
1144 list_for_each_entry(c
, &cdev
->configs
, list
) {
1146 len
= lookup_string(c
->strings
, buf
, language
, id
);
1150 list_for_each_entry(f
, &c
->functions
, list
) {
1153 len
= lookup_string(f
->strings
, buf
, language
, id
);
1162 * usb_string_id() - allocate an unused string ID
1163 * @cdev: the device whose string descriptor IDs are being allocated
1164 * Context: single threaded during gadget setup
1166 * @usb_string_id() is called from bind() callbacks to allocate
1167 * string IDs. Drivers for functions, configurations, or gadgets will
1168 * then store that ID in the appropriate descriptors and string table.
1170 * All string identifier should be allocated using this,
1171 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
1172 * that for example different functions don't wrongly assign different
1173 * meanings to the same identifier.
1175 int usb_string_id(struct usb_composite_dev
*cdev
)
1177 if (cdev
->next_string_id
< 254) {
1178 /* string id 0 is reserved by USB spec for list of
1179 * supported languages */
1180 /* 255 reserved as well? -- mina86 */
1181 cdev
->next_string_id
++;
1182 return cdev
->next_string_id
;
1186 EXPORT_SYMBOL_GPL(usb_string_id
);
1189 * usb_string_ids() - allocate unused string IDs in batch
1190 * @cdev: the device whose string descriptor IDs are being allocated
1191 * @str: an array of usb_string objects to assign numbers to
1192 * Context: single threaded during gadget setup
1194 * @usb_string_ids() is called from bind() callbacks to allocate
1195 * string IDs. Drivers for functions, configurations, or gadgets will
1196 * then copy IDs from the string table to the appropriate descriptors
1197 * and string table for other languages.
1199 * All string identifier should be allocated using this,
1200 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1201 * example different functions don't wrongly assign different meanings
1202 * to the same identifier.
1204 int usb_string_ids_tab(struct usb_composite_dev
*cdev
, struct usb_string
*str
)
1206 int next
= cdev
->next_string_id
;
1208 for (; str
->s
; ++str
) {
1209 if (unlikely(next
>= 254))
1214 cdev
->next_string_id
= next
;
1218 EXPORT_SYMBOL_GPL(usb_string_ids_tab
);
1220 static struct usb_gadget_string_container
*copy_gadget_strings(
1221 struct usb_gadget_strings
**sp
, unsigned n_gstrings
,
1224 struct usb_gadget_string_container
*uc
;
1225 struct usb_gadget_strings
**gs_array
;
1226 struct usb_gadget_strings
*gs
;
1227 struct usb_string
*s
;
1234 mem
+= sizeof(void *) * (n_gstrings
+ 1);
1235 mem
+= sizeof(struct usb_gadget_strings
) * n_gstrings
;
1236 mem
+= sizeof(struct usb_string
) * (n_strings
+ 1) * (n_gstrings
);
1237 uc
= kmalloc(mem
, GFP_KERNEL
);
1239 return ERR_PTR(-ENOMEM
);
1240 gs_array
= get_containers_gs(uc
);
1242 stash
+= sizeof(void *) * (n_gstrings
+ 1);
1243 for (n_gs
= 0; n_gs
< n_gstrings
; n_gs
++) {
1244 struct usb_string
*org_s
;
1246 gs_array
[n_gs
] = stash
;
1247 gs
= gs_array
[n_gs
];
1248 stash
+= sizeof(struct usb_gadget_strings
);
1249 gs
->language
= sp
[n_gs
]->language
;
1250 gs
->strings
= stash
;
1251 org_s
= sp
[n_gs
]->strings
;
1253 for (n_s
= 0; n_s
< n_strings
; n_s
++) {
1255 stash
+= sizeof(struct usb_string
);
1264 stash
+= sizeof(struct usb_string
);
1267 gs_array
[n_gs
] = NULL
;
1272 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
1273 * @cdev: the device whose string descriptor IDs are being allocated
1275 * @sp: an array of usb_gadget_strings to attach.
1276 * @n_strings: number of entries in each usb_strings array (sp[]->strings)
1278 * This function will create a deep copy of usb_gadget_strings and usb_string
1279 * and attach it to the cdev. The actual string (usb_string.s) will not be
1280 * copied but only a referenced will be made. The struct usb_gadget_strings
1281 * array may contain multiple languages and should be NULL terminated.
1282 * The ->language pointer of each struct usb_gadget_strings has to contain the
1283 * same amount of entries.
1284 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
1285 * usb_string entry of es-ES contains the translation of the first usb_string
1286 * entry of en-US. Therefore both entries become the same id assign.
1288 struct usb_string
*usb_gstrings_attach(struct usb_composite_dev
*cdev
,
1289 struct usb_gadget_strings
**sp
, unsigned n_strings
)
1291 struct usb_gadget_string_container
*uc
;
1292 struct usb_gadget_strings
**n_gs
;
1293 unsigned n_gstrings
= 0;
1297 for (i
= 0; sp
[i
]; i
++)
1301 return ERR_PTR(-EINVAL
);
1303 uc
= copy_gadget_strings(sp
, n_gstrings
, n_strings
);
1305 return ERR_CAST(uc
);
1307 n_gs
= get_containers_gs(uc
);
1308 ret
= usb_string_ids_tab(cdev
, n_gs
[0]->strings
);
1312 for (i
= 1; i
< n_gstrings
; i
++) {
1313 struct usb_string
*m_s
;
1314 struct usb_string
*s
;
1317 m_s
= n_gs
[0]->strings
;
1318 s
= n_gs
[i
]->strings
;
1319 for (n
= 0; n
< n_strings
; n
++) {
1325 list_add_tail(&uc
->list
, &cdev
->gstrings
);
1326 return n_gs
[0]->strings
;
1329 return ERR_PTR(ret
);
1331 EXPORT_SYMBOL_GPL(usb_gstrings_attach
);
1334 * usb_string_ids_n() - allocate unused string IDs in batch
1335 * @c: the device whose string descriptor IDs are being allocated
1336 * @n: number of string IDs to allocate
1337 * Context: single threaded during gadget setup
1339 * Returns the first requested ID. This ID and next @n-1 IDs are now
1340 * valid IDs. At least provided that @n is non-zero because if it
1341 * is, returns last requested ID which is now very useful information.
1343 * @usb_string_ids_n() is called from bind() callbacks to allocate
1344 * string IDs. Drivers for functions, configurations, or gadgets will
1345 * then store that ID in the appropriate descriptors and string table.
1347 * All string identifier should be allocated using this,
1348 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1349 * example different functions don't wrongly assign different meanings
1350 * to the same identifier.
1352 int usb_string_ids_n(struct usb_composite_dev
*c
, unsigned n
)
1354 unsigned next
= c
->next_string_id
;
1355 if (unlikely(n
> 254 || (unsigned)next
+ n
> 254))
1357 c
->next_string_id
+= n
;
1360 EXPORT_SYMBOL_GPL(usb_string_ids_n
);
1362 /*-------------------------------------------------------------------------*/
1364 static void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1366 struct usb_composite_dev
*cdev
;
1368 if (req
->status
|| req
->actual
!= req
->length
)
1369 DBG((struct usb_composite_dev
*) ep
->driver_data
,
1370 "setup complete --> %d, %d/%d\n",
1371 req
->status
, req
->actual
, req
->length
);
1374 * REVIST The same ep0 requests are shared with function drivers
1375 * so they don't have to maintain the same ->complete() stubs.
1377 * Because of that, we need to check for the validity of ->context
1378 * here, even though we know we've set it to something useful.
1383 cdev
= req
->context
;
1385 if (cdev
->req
== req
)
1386 cdev
->setup_pending
= false;
1387 else if (cdev
->os_desc_req
== req
)
1388 cdev
->os_desc_pending
= false;
1390 WARN(1, "unknown request %p\n", req
);
1393 static int composite_ep0_queue(struct usb_composite_dev
*cdev
,
1394 struct usb_request
*req
, gfp_t gfp_flags
)
1398 ret
= usb_ep_queue(cdev
->gadget
->ep0
, req
, gfp_flags
);
1400 if (cdev
->req
== req
)
1401 cdev
->setup_pending
= true;
1402 else if (cdev
->os_desc_req
== req
)
1403 cdev
->os_desc_pending
= true;
1405 WARN(1, "unknown request %p\n", req
);
1411 static int count_ext_compat(struct usb_configuration
*c
)
1416 for (i
= 0; i
< c
->next_interface_id
; ++i
) {
1417 struct usb_function
*f
;
1420 f
= c
->interface
[i
];
1421 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1422 struct usb_os_desc
*d
;
1424 if (i
!= f
->os_desc_table
[j
].if_id
)
1426 d
= f
->os_desc_table
[j
].os_desc
;
1427 if (d
&& d
->ext_compat_id
)
1435 static int fill_ext_compat(struct usb_configuration
*c
, u8
*buf
)
1440 for (i
= 0; i
< c
->next_interface_id
; ++i
) {
1441 struct usb_function
*f
;
1444 f
= c
->interface
[i
];
1445 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1446 struct usb_os_desc
*d
;
1448 if (i
!= f
->os_desc_table
[j
].if_id
)
1450 d
= f
->os_desc_table
[j
].os_desc
;
1451 if (d
&& d
->ext_compat_id
) {
1454 memcpy(buf
, d
->ext_compat_id
, 16);
1462 if (count
+ 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ
)
1470 static int count_ext_prop(struct usb_configuration
*c
, int interface
)
1472 struct usb_function
*f
;
1475 f
= c
->interface
[interface
];
1476 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1477 struct usb_os_desc
*d
;
1479 if (interface
!= f
->os_desc_table
[j
].if_id
)
1481 d
= f
->os_desc_table
[j
].os_desc
;
1482 if (d
&& d
->ext_compat_id
)
1483 return d
->ext_prop_count
;
1488 static int len_ext_prop(struct usb_configuration
*c
, int interface
)
1490 struct usb_function
*f
;
1491 struct usb_os_desc
*d
;
1494 res
= 10; /* header length */
1495 f
= c
->interface
[interface
];
1496 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1497 if (interface
!= f
->os_desc_table
[j
].if_id
)
1499 d
= f
->os_desc_table
[j
].os_desc
;
1501 return min(res
+ d
->ext_prop_len
, 4096);
1506 static int fill_ext_prop(struct usb_configuration
*c
, int interface
, u8
*buf
)
1508 struct usb_function
*f
;
1509 struct usb_os_desc
*d
;
1510 struct usb_os_desc_ext_prop
*ext_prop
;
1511 int j
, count
, n
, ret
;
1513 f
= c
->interface
[interface
];
1514 count
= 10; /* header length */
1515 for (j
= 0; j
< f
->os_desc_n
; ++j
) {
1516 if (interface
!= f
->os_desc_table
[j
].if_id
)
1518 d
= f
->os_desc_table
[j
].os_desc
;
1520 list_for_each_entry(ext_prop
, &d
->ext_prop
, entry
) {
1521 n
= ext_prop
->data_len
+
1522 ext_prop
->name_len
+ 14;
1523 if (count
+ n
>= USB_COMP_EP0_OS_DESC_BUFSIZ
)
1525 usb_ext_prop_put_size(buf
, n
);
1526 usb_ext_prop_put_type(buf
, ext_prop
->type
);
1527 ret
= usb_ext_prop_put_name(buf
, ext_prop
->name
,
1528 ext_prop
->name_len
);
1531 switch (ext_prop
->type
) {
1532 case USB_EXT_PROP_UNICODE
:
1533 case USB_EXT_PROP_UNICODE_ENV
:
1534 case USB_EXT_PROP_UNICODE_LINK
:
1535 usb_ext_prop_put_unicode(buf
, ret
,
1537 ext_prop
->data_len
);
1539 case USB_EXT_PROP_BINARY
:
1540 usb_ext_prop_put_binary(buf
, ret
,
1542 ext_prop
->data_len
);
1544 case USB_EXT_PROP_LE32
:
1545 /* not implemented */
1546 case USB_EXT_PROP_BE32
:
1547 /* not implemented */
1560 * The setup() callback implements all the ep0 functionality that's
1561 * not handled lower down, in hardware or the hardware driver(like
1562 * device and endpoint feature flags, and their status). It's all
1563 * housekeeping for the gadget function we're implementing. Most of
1564 * the work is in config and function specific setup.
1567 composite_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1569 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1570 struct usb_request
*req
= cdev
->req
;
1571 int value
= -EOPNOTSUPP
;
1573 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1574 u8 intf
= w_index
& 0xFF;
1575 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1576 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1577 struct usb_function
*f
= NULL
;
1580 if (w_length
> USB_COMP_EP0_BUFSIZ
) {
1581 if (ctrl
->bRequestType
& USB_DIR_IN
) {
1582 /* Cast away the const, we are going to overwrite on purpose. */
1583 __le16
*temp
= (__le16
*)&ctrl
->wLength
;
1585 *temp
= cpu_to_le16(USB_COMP_EP0_BUFSIZ
);
1586 w_length
= USB_COMP_EP0_BUFSIZ
;
1592 /* partial re-init of the response message; the function or the
1593 * gadget might need to intercept e.g. a control-OUT completion
1594 * when we delegate to it.
1597 req
->context
= cdev
;
1598 req
->complete
= composite_setup_complete
;
1600 gadget
->ep0
->driver_data
= cdev
;
1603 * Don't let non-standard requests match any of the cases below
1606 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_STANDARD
)
1609 switch (ctrl
->bRequest
) {
1611 /* we handle all standard USB descriptors */
1612 case USB_REQ_GET_DESCRIPTOR
:
1613 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1615 switch (w_value
>> 8) {
1618 cdev
->desc
.bNumConfigurations
=
1619 count_configs(cdev
, USB_DT_DEVICE
);
1620 cdev
->desc
.bMaxPacketSize0
=
1621 cdev
->gadget
->ep0
->maxpacket
;
1622 if (gadget_is_superspeed(gadget
)) {
1623 if (gadget
->speed
>= USB_SPEED_SUPER
) {
1624 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0310);
1625 cdev
->desc
.bMaxPacketSize0
= 9;
1627 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0210);
1630 if (gadget
->lpm_capable
)
1631 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0201);
1633 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0200);
1636 value
= min(w_length
, (u16
) sizeof cdev
->desc
);
1637 memcpy(req
->buf
, &cdev
->desc
, value
);
1639 case USB_DT_DEVICE_QUALIFIER
:
1640 if (!gadget_is_dualspeed(gadget
) ||
1641 gadget
->speed
>= USB_SPEED_SUPER
)
1644 value
= min_t(int, w_length
,
1645 sizeof(struct usb_qualifier_descriptor
));
1647 case USB_DT_OTHER_SPEED_CONFIG
:
1648 if (!gadget_is_dualspeed(gadget
) ||
1649 gadget
->speed
>= USB_SPEED_SUPER
)
1653 value
= config_desc(cdev
, w_value
);
1655 value
= min(w_length
, (u16
) value
);
1658 value
= get_string(cdev
, req
->buf
,
1659 w_index
, w_value
& 0xff);
1661 value
= min(w_length
, (u16
) value
);
1664 if (gadget_is_superspeed(gadget
) ||
1665 gadget
->lpm_capable
) {
1666 value
= bos_desc(cdev
);
1667 value
= min(w_length
, (u16
) value
);
1671 if (gadget_is_otg(gadget
)) {
1672 struct usb_configuration
*config
;
1673 int otg_desc_len
= 0;
1676 config
= cdev
->config
;
1678 config
= list_first_entry(
1680 struct usb_configuration
, list
);
1684 if (gadget
->otg_caps
&&
1685 (gadget
->otg_caps
->otg_rev
>= 0x0200))
1686 otg_desc_len
+= sizeof(
1687 struct usb_otg20_descriptor
);
1689 otg_desc_len
+= sizeof(
1690 struct usb_otg_descriptor
);
1692 value
= min_t(int, w_length
, otg_desc_len
);
1693 memcpy(req
->buf
, config
->descriptors
[0], value
);
1699 /* any number of configs can work */
1700 case USB_REQ_SET_CONFIGURATION
:
1701 if (ctrl
->bRequestType
!= 0)
1703 if (gadget_is_otg(gadget
)) {
1704 if (gadget
->a_hnp_support
)
1705 DBG(cdev
, "HNP available\n");
1706 else if (gadget
->a_alt_hnp_support
)
1707 DBG(cdev
, "HNP on another port\n");
1709 VDBG(cdev
, "HNP inactive\n");
1711 spin_lock(&cdev
->lock
);
1712 value
= set_config(cdev
, ctrl
, w_value
);
1713 spin_unlock(&cdev
->lock
);
1715 case USB_REQ_GET_CONFIGURATION
:
1716 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1719 *(u8
*)req
->buf
= cdev
->config
->bConfigurationValue
;
1721 *(u8
*)req
->buf
= 0;
1722 value
= min(w_length
, (u16
) 1);
1725 /* function drivers must handle get/set altsetting */
1726 case USB_REQ_SET_INTERFACE
:
1727 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
1729 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1731 f
= cdev
->config
->interface
[intf
];
1736 * If there's no get_alt() method, we know only altsetting zero
1737 * works. There is no need to check if set_alt() is not NULL
1738 * as we check this in usb_add_function().
1740 if (w_value
&& !f
->get_alt
)
1743 spin_lock(&cdev
->lock
);
1744 value
= f
->set_alt(f
, w_index
, w_value
);
1745 if (value
== USB_GADGET_DELAYED_STATUS
) {
1747 "%s: interface %d (%s) requested delayed status\n",
1748 __func__
, intf
, f
->name
);
1749 cdev
->delayed_status
++;
1750 DBG(cdev
, "delayed_status count %d\n",
1751 cdev
->delayed_status
);
1753 spin_unlock(&cdev
->lock
);
1755 case USB_REQ_GET_INTERFACE
:
1756 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1758 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1760 f
= cdev
->config
->interface
[intf
];
1763 /* lots of interfaces only need altsetting zero... */
1764 value
= f
->get_alt
? f
->get_alt(f
, w_index
) : 0;
1767 *((u8
*)req
->buf
) = value
;
1768 value
= min(w_length
, (u16
) 1);
1770 case USB_REQ_GET_STATUS
:
1771 if (gadget_is_otg(gadget
) && gadget
->hnp_polling_support
&&
1772 (w_index
== OTG_STS_SELECTOR
)) {
1773 if (ctrl
->bRequestType
!= (USB_DIR_IN
|
1776 *((u8
*)req
->buf
) = gadget
->host_request_flag
;
1782 * USB 3.0 additions:
1783 * Function driver should handle get_status request. If such cb
1784 * wasn't supplied we respond with default value = 0
1785 * Note: function driver should supply such cb only for the
1786 * first interface of the function
1788 if (!gadget_is_superspeed(gadget
))
1790 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_RECIP_INTERFACE
))
1792 value
= 2; /* This is the length of the get_status reply */
1793 put_unaligned_le16(0, req
->buf
);
1794 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1796 f
= cdev
->config
->interface
[intf
];
1799 status
= f
->get_status
? f
->get_status(f
) : 0;
1802 put_unaligned_le16(status
& 0x0000ffff, req
->buf
);
1805 * Function drivers should handle SetFeature/ClearFeature
1806 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1807 * only for the first interface of the function
1809 case USB_REQ_CLEAR_FEATURE
:
1810 case USB_REQ_SET_FEATURE
:
1811 if (!gadget_is_superspeed(gadget
))
1813 if (ctrl
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_INTERFACE
))
1816 case USB_INTRF_FUNC_SUSPEND
:
1817 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1819 f
= cdev
->config
->interface
[intf
];
1823 if (f
->func_suspend
)
1824 value
= f
->func_suspend(f
, w_index
>> 8);
1827 "func_suspend() returned error %d\n",
1837 * OS descriptors handling
1839 if (cdev
->use_os_string
&& cdev
->os_desc_config
&&
1840 (ctrl
->bRequestType
& USB_TYPE_VENDOR
) &&
1841 ctrl
->bRequest
== cdev
->b_vendor_code
) {
1842 struct usb_request
*req
;
1843 struct usb_configuration
*os_desc_cfg
;
1848 req
= cdev
->os_desc_req
;
1849 req
->context
= cdev
;
1850 req
->complete
= composite_setup_complete
;
1852 os_desc_cfg
= cdev
->os_desc_config
;
1853 w_length
= min_t(u16
, w_length
, USB_COMP_EP0_OS_DESC_BUFSIZ
);
1854 memset(buf
, 0, w_length
);
1856 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1857 case USB_RECIP_DEVICE
:
1858 if (w_index
!= 0x4 || (w_value
>> 8))
1861 if (w_length
== 0x10) {
1862 /* Number of ext compat interfaces */
1863 count
= count_ext_compat(os_desc_cfg
);
1865 count
*= 24; /* 24 B/ext compat desc */
1866 count
+= 16; /* header */
1867 put_unaligned_le32(count
, buf
);
1870 /* "extended compatibility ID"s */
1871 count
= count_ext_compat(os_desc_cfg
);
1873 count
*= 24; /* 24 B/ext compat desc */
1874 count
+= 16; /* header */
1875 put_unaligned_le32(count
, buf
);
1877 value
= fill_ext_compat(os_desc_cfg
, buf
);
1878 value
= min_t(u16
, w_length
, value
);
1881 case USB_RECIP_INTERFACE
:
1882 if (w_index
!= 0x5 || (w_value
>> 8))
1884 interface
= w_value
& 0xFF;
1886 if (w_length
== 0x0A) {
1887 count
= count_ext_prop(os_desc_cfg
,
1889 put_unaligned_le16(count
, buf
+ 8);
1890 count
= len_ext_prop(os_desc_cfg
,
1892 put_unaligned_le32(count
, buf
);
1896 count
= count_ext_prop(os_desc_cfg
,
1898 put_unaligned_le16(count
, buf
+ 8);
1899 count
= len_ext_prop(os_desc_cfg
,
1901 put_unaligned_le32(count
, buf
);
1903 value
= fill_ext_prop(os_desc_cfg
,
1907 value
= min_t(u16
, w_length
, value
);
1913 req
->length
= value
;
1914 req
->context
= cdev
;
1915 req
->zero
= value
< w_length
;
1916 value
= composite_ep0_queue(cdev
, req
,
1919 DBG(cdev
, "ep_queue --> %d\n", value
);
1921 composite_setup_complete(gadget
->ep0
,
1929 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1930 ctrl
->bRequestType
, ctrl
->bRequest
,
1931 w_value
, w_index
, w_length
);
1933 /* functions always handle their interfaces and endpoints...
1934 * punt other recipients (other, WUSB, ...) to the current
1935 * configuration code.
1938 list_for_each_entry(f
, &cdev
->config
->functions
, list
)
1940 f
->req_match(f
, ctrl
, false))
1943 struct usb_configuration
*c
;
1944 list_for_each_entry(c
, &cdev
->configs
, list
)
1945 list_for_each_entry(f
, &c
->functions
, list
)
1947 f
->req_match(f
, ctrl
, true))
1952 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1953 case USB_RECIP_INTERFACE
:
1954 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1956 f
= cdev
->config
->interface
[intf
];
1959 case USB_RECIP_ENDPOINT
:
1962 endp
= ((w_index
& 0x80) >> 3) | (w_index
& 0x0f);
1963 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1964 if (test_bit(endp
, f
->endpoints
))
1967 if (&f
->list
== &cdev
->config
->functions
)
1973 value
= f
->setup(f
, ctrl
);
1975 struct usb_configuration
*c
;
1981 /* try current config's setup */
1983 value
= c
->setup(c
, ctrl
);
1987 /* try the only function in the current config */
1988 if (!list_is_singular(&c
->functions
))
1990 f
= list_first_entry(&c
->functions
, struct usb_function
,
1993 value
= f
->setup(f
, ctrl
);
1999 /* respond with data transfer before status phase? */
2000 if (value
>= 0 && value
!= USB_GADGET_DELAYED_STATUS
) {
2001 req
->length
= value
;
2002 req
->context
= cdev
;
2003 req
->zero
= value
< w_length
;
2004 value
= composite_ep0_queue(cdev
, req
, GFP_ATOMIC
);
2006 DBG(cdev
, "ep_queue --> %d\n", value
);
2008 composite_setup_complete(gadget
->ep0
, req
);
2010 } else if (value
== USB_GADGET_DELAYED_STATUS
&& w_length
!= 0) {
2012 "%s: Delayed status not supported for w_length != 0",
2017 /* device either stalls (value < 0) or reports success */
2021 void composite_disconnect(struct usb_gadget
*gadget
)
2023 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2024 unsigned long flags
;
2027 WARN(1, "%s: Calling disconnect on a Gadget that is \
2028 not connected\n", __func__
);
2032 /* REVISIT: should we have config and device level
2033 * disconnect callbacks?
2035 spin_lock_irqsave(&cdev
->lock
, flags
);
2038 if (cdev
->driver
->disconnect
)
2039 cdev
->driver
->disconnect(cdev
);
2040 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2043 /*-------------------------------------------------------------------------*/
2045 static ssize_t
suspended_show(struct device
*dev
, struct device_attribute
*attr
,
2048 struct usb_gadget
*gadget
= dev_to_usb_gadget(dev
);
2049 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2051 return sprintf(buf
, "%d\n", cdev
->suspended
);
2053 static DEVICE_ATTR_RO(suspended
);
2055 static void __composite_unbind(struct usb_gadget
*gadget
, bool unbind_driver
)
2057 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2058 struct usb_gadget_strings
*gstr
= cdev
->driver
->strings
[0];
2059 struct usb_string
*dev_str
= gstr
->strings
;
2061 /* composite_disconnect() must already have been called
2062 * by the underlying peripheral controller driver!
2063 * so there's no i/o concurrency that could affect the
2064 * state protected by cdev->lock.
2066 WARN_ON(cdev
->config
);
2068 while (!list_empty(&cdev
->configs
)) {
2069 struct usb_configuration
*c
;
2070 c
= list_first_entry(&cdev
->configs
,
2071 struct usb_configuration
, list
);
2072 remove_config(cdev
, c
);
2074 if (cdev
->driver
->unbind
&& unbind_driver
)
2075 cdev
->driver
->unbind(cdev
);
2077 composite_dev_cleanup(cdev
);
2079 if (dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
== cdev
->def_manufacturer
)
2080 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= "";
2082 kfree(cdev
->def_manufacturer
);
2084 set_gadget_data(gadget
, NULL
);
2087 static void composite_unbind(struct usb_gadget
*gadget
)
2089 __composite_unbind(gadget
, true);
2092 static void update_unchanged_dev_desc(struct usb_device_descriptor
*new,
2093 const struct usb_device_descriptor
*old
)
2103 * these variables may have been set in
2104 * usb_composite_overwrite_options()
2106 idVendor
= new->idVendor
;
2107 idProduct
= new->idProduct
;
2108 bcdDevice
= new->bcdDevice
;
2109 iSerialNumber
= new->iSerialNumber
;
2110 iManufacturer
= new->iManufacturer
;
2111 iProduct
= new->iProduct
;
2115 new->idVendor
= idVendor
;
2117 new->idProduct
= idProduct
;
2119 new->bcdDevice
= bcdDevice
;
2121 new->bcdDevice
= cpu_to_le16(get_default_bcdDevice());
2123 new->iSerialNumber
= iSerialNumber
;
2125 new->iManufacturer
= iManufacturer
;
2127 new->iProduct
= iProduct
;
2130 int composite_dev_prepare(struct usb_composite_driver
*composite
,
2131 struct usb_composite_dev
*cdev
)
2133 struct usb_gadget
*gadget
= cdev
->gadget
;
2136 /* preallocate control response and buffer */
2137 cdev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
2141 cdev
->req
->buf
= kzalloc(USB_COMP_EP0_BUFSIZ
, GFP_KERNEL
);
2142 if (!cdev
->req
->buf
)
2145 ret
= device_create_file(&gadget
->dev
, &dev_attr_suspended
);
2149 cdev
->req
->complete
= composite_setup_complete
;
2150 cdev
->req
->context
= cdev
;
2151 gadget
->ep0
->driver_data
= cdev
;
2153 cdev
->driver
= composite
;
2156 * As per USB compliance update, a device that is actively drawing
2157 * more than 100mA from USB must report itself as bus-powered in
2158 * the GetStatus(DEVICE) call.
2160 if (CONFIG_USB_GADGET_VBUS_DRAW
<= USB_SELF_POWER_VBUS_MAX_DRAW
)
2161 usb_gadget_set_selfpowered(gadget
);
2163 /* interface and string IDs start at zero via kzalloc.
2164 * we force endpoints to start unassigned; few controller
2165 * drivers will zero ep->driver_data.
2167 usb_ep_autoconfig_reset(gadget
);
2170 kfree(cdev
->req
->buf
);
2172 usb_ep_free_request(gadget
->ep0
, cdev
->req
);
2177 int composite_os_desc_req_prepare(struct usb_composite_dev
*cdev
,
2182 cdev
->os_desc_req
= usb_ep_alloc_request(ep0
, GFP_KERNEL
);
2183 if (!cdev
->os_desc_req
) {
2188 cdev
->os_desc_req
->buf
= kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ
,
2190 if (!cdev
->os_desc_req
->buf
) {
2192 usb_ep_free_request(ep0
, cdev
->os_desc_req
);
2195 cdev
->os_desc_req
->context
= cdev
;
2196 cdev
->os_desc_req
->complete
= composite_setup_complete
;
2201 void composite_dev_cleanup(struct usb_composite_dev
*cdev
)
2203 struct usb_gadget_string_container
*uc
, *tmp
;
2205 list_for_each_entry_safe(uc
, tmp
, &cdev
->gstrings
, list
) {
2206 list_del(&uc
->list
);
2209 if (cdev
->os_desc_req
) {
2210 if (cdev
->os_desc_pending
)
2211 usb_ep_dequeue(cdev
->gadget
->ep0
, cdev
->os_desc_req
);
2213 kfree(cdev
->os_desc_req
->buf
);
2214 cdev
->os_desc_req
->buf
= NULL
;
2215 usb_ep_free_request(cdev
->gadget
->ep0
, cdev
->os_desc_req
);
2216 cdev
->os_desc_req
= NULL
;
2219 if (cdev
->setup_pending
)
2220 usb_ep_dequeue(cdev
->gadget
->ep0
, cdev
->req
);
2222 kfree(cdev
->req
->buf
);
2223 cdev
->req
->buf
= NULL
;
2224 usb_ep_free_request(cdev
->gadget
->ep0
, cdev
->req
);
2227 cdev
->next_string_id
= 0;
2228 device_remove_file(&cdev
->gadget
->dev
, &dev_attr_suspended
);
2231 static int composite_bind(struct usb_gadget
*gadget
,
2232 struct usb_gadget_driver
*gdriver
)
2234 struct usb_composite_dev
*cdev
;
2235 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
2236 int status
= -ENOMEM
;
2238 cdev
= kzalloc(sizeof *cdev
, GFP_KERNEL
);
2242 spin_lock_init(&cdev
->lock
);
2243 cdev
->gadget
= gadget
;
2244 set_gadget_data(gadget
, cdev
);
2245 INIT_LIST_HEAD(&cdev
->configs
);
2246 INIT_LIST_HEAD(&cdev
->gstrings
);
2248 status
= composite_dev_prepare(composite
, cdev
);
2252 /* composite gadget needs to assign strings for whole device (like
2253 * serial number), register function drivers, potentially update
2254 * power state and consumption, etc
2256 status
= composite
->bind(cdev
);
2260 if (cdev
->use_os_string
) {
2261 status
= composite_os_desc_req_prepare(cdev
, gadget
->ep0
);
2266 update_unchanged_dev_desc(&cdev
->desc
, composite
->dev
);
2268 /* has userspace failed to provide a serial number? */
2269 if (composite
->needs_serial
&& !cdev
->desc
.iSerialNumber
)
2270 WARNING(cdev
, "userspace failed to provide iSerialNumber\n");
2272 INFO(cdev
, "%s ready\n", composite
->name
);
2276 __composite_unbind(gadget
, false);
2280 /*-------------------------------------------------------------------------*/
2282 void __nocfi
composite_suspend(struct usb_gadget
*gadget
)
2284 struct usb_composite_dev
*cdev
= NULL
;
2285 struct usb_function
*f
;
2286 unsigned long flags
;
2288 /* REVISIT: should we have config level
2289 * suspend/resume callbacks?
2292 if (gadget
== NULL
) {
2293 pr_info("%s: gadget is NULL\n", __func__
);
2297 cdev
= get_gadget_data(gadget
);
2299 pr_info("%s: cdev is NULL\n", __func__
);
2303 spin_lock_irqsave(&cdev
->lock
, flags
);
2304 DBG(cdev
, "suspend\n");
2306 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
2311 if (cdev
->driver
->suspend
)
2312 cdev
->driver
->suspend(cdev
);
2314 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2316 cdev
->suspended
= 1;
2318 usb_gadget_vbus_draw(gadget
, 2);
2321 void composite_resume(struct usb_gadget
*gadget
)
2323 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
2324 struct usb_function
*f
;
2327 /* REVISIT: should we have config level
2328 * suspend/resume callbacks?
2330 DBG(cdev
, "resume\n");
2331 if (cdev
->driver
->resume
)
2332 cdev
->driver
->resume(cdev
);
2334 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
2339 maxpower
= cdev
->config
->MaxPower
;
2341 usb_gadget_vbus_draw(gadget
, maxpower
?
2342 maxpower
: CONFIG_USB_GADGET_VBUS_DRAW
);
2345 cdev
->suspended
= 0;
2348 /*-------------------------------------------------------------------------*/
2350 static const struct usb_gadget_driver composite_driver_template
= {
2351 .bind
= composite_bind
,
2352 .unbind
= composite_unbind
,
2354 .setup
= composite_setup
,
2355 .reset
= composite_disconnect
,
2356 .disconnect
= composite_disconnect
,
2358 .suspend
= composite_suspend
,
2359 .resume
= composite_resume
,
2362 .owner
= THIS_MODULE
,
2367 * usb_composite_probe() - register a composite driver
2368 * @driver: the driver to register
2370 * Context: single threaded during gadget setup
2372 * This function is used to register drivers using the composite driver
2373 * framework. The return value is zero, or a negative errno value.
2374 * Those values normally come from the driver's @bind method, which does
2375 * all the work of setting up the driver to match the hardware.
2377 * On successful return, the gadget is ready to respond to requests from
2378 * the host, unless one of its components invokes usb_gadget_disconnect()
2379 * while it was binding. That would usually be done in order to wait for
2380 * some userspace participation.
2382 int usb_composite_probe(struct usb_composite_driver
*driver
)
2384 struct usb_gadget_driver
*gadget_driver
;
2386 if (!driver
|| !driver
->dev
|| !driver
->bind
)
2390 driver
->name
= "composite";
2392 driver
->gadget_driver
= composite_driver_template
;
2393 gadget_driver
= &driver
->gadget_driver
;
2395 gadget_driver
->function
= (char *) driver
->name
;
2396 gadget_driver
->driver
.name
= driver
->name
;
2397 gadget_driver
->max_speed
= driver
->max_speed
;
2399 return usb_gadget_probe_driver(gadget_driver
);
2401 EXPORT_SYMBOL_GPL(usb_composite_probe
);
2404 * usb_composite_unregister() - unregister a composite driver
2405 * @driver: the driver to unregister
2407 * This function is used to unregister drivers using the composite
2410 void usb_composite_unregister(struct usb_composite_driver
*driver
)
2412 usb_gadget_unregister_driver(&driver
->gadget_driver
);
2414 EXPORT_SYMBOL_GPL(usb_composite_unregister
);
2417 * usb_composite_setup_continue() - Continue with the control transfer
2418 * @cdev: the composite device who's control transfer was kept waiting
2420 * This function must be called by the USB function driver to continue
2421 * with the control transfer's data/status stage in case it had requested to
2422 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
2423 * can request the composite framework to delay the setup request's data/status
2424 * stages by returning USB_GADGET_DELAYED_STATUS.
2426 void usb_composite_setup_continue(struct usb_composite_dev
*cdev
)
2429 struct usb_request
*req
= cdev
->req
;
2430 unsigned long flags
;
2432 DBG(cdev
, "%s\n", __func__
);
2433 spin_lock_irqsave(&cdev
->lock
, flags
);
2435 if (cdev
->delayed_status
== 0) {
2436 WARN(cdev
, "%s: Unexpected call\n", __func__
);
2438 } else if (--cdev
->delayed_status
== 0) {
2439 DBG(cdev
, "%s: Completing delayed status\n", __func__
);
2441 req
->context
= cdev
;
2442 value
= composite_ep0_queue(cdev
, req
, GFP_ATOMIC
);
2444 DBG(cdev
, "ep_queue --> %d\n", value
);
2446 composite_setup_complete(cdev
->gadget
->ep0
, req
);
2450 spin_unlock_irqrestore(&cdev
->lock
, flags
);
2452 EXPORT_SYMBOL_GPL(usb_composite_setup_continue
);
2454 static char *composite_default_mfr(struct usb_gadget
*gadget
)
2456 return kasprintf(GFP_KERNEL
, "%s %s with %s", init_utsname()->sysname
,
2457 init_utsname()->release
, gadget
->name
);
2460 void usb_composite_overwrite_options(struct usb_composite_dev
*cdev
,
2461 struct usb_composite_overwrite
*covr
)
2463 struct usb_device_descriptor
*desc
= &cdev
->desc
;
2464 struct usb_gadget_strings
*gstr
= cdev
->driver
->strings
[0];
2465 struct usb_string
*dev_str
= gstr
->strings
;
2468 desc
->idVendor
= cpu_to_le16(covr
->idVendor
);
2470 if (covr
->idProduct
)
2471 desc
->idProduct
= cpu_to_le16(covr
->idProduct
);
2473 if (covr
->bcdDevice
)
2474 desc
->bcdDevice
= cpu_to_le16(covr
->bcdDevice
);
2476 if (covr
->serial_number
) {
2477 desc
->iSerialNumber
= dev_str
[USB_GADGET_SERIAL_IDX
].id
;
2478 dev_str
[USB_GADGET_SERIAL_IDX
].s
= covr
->serial_number
;
2480 if (covr
->manufacturer
) {
2481 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
2482 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= covr
->manufacturer
;
2484 } else if (!strlen(dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
)) {
2485 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
2486 cdev
->def_manufacturer
= composite_default_mfr(cdev
->gadget
);
2487 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= cdev
->def_manufacturer
;
2490 if (covr
->product
) {
2491 desc
->iProduct
= dev_str
[USB_GADGET_PRODUCT_IDX
].id
;
2492 dev_str
[USB_GADGET_PRODUCT_IDX
].s
= covr
->product
;
2495 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options
);
2497 MODULE_LICENSE("GPL");
2498 MODULE_AUTHOR("David Brownell");