2 #include <linux/usb/ch9.h>
3 #include <linux/usb/hcd.h>
4 #include <linux/usb/quirks.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
7 #include <linux/device.h>
8 #include <asm/byteorder.h>
12 #define USB_MAXALTSETTING 128 /* Hard limit */
14 #define USB_MAXCONFIG 8 /* Arbitrary limit */
17 static inline const char *plural(int n
)
19 return (n
== 1 ? "" : "s");
22 static int find_next_descriptor(unsigned char *buffer
, int size
,
23 int dt1
, int dt2
, int *num_skipped
)
25 struct usb_descriptor_header
*h
;
27 unsigned char *buffer0
= buffer
;
29 /* Find the next descriptor of type dt1 or dt2 */
31 h
= (struct usb_descriptor_header
*) buffer
;
32 if (h
->bDescriptorType
== dt1
|| h
->bDescriptorType
== dt2
)
39 /* Store the number of descriptors skipped and return the
40 * number of bytes skipped */
43 return buffer
- buffer0
;
46 static void usb_parse_ssp_isoc_endpoint_companion(struct device
*ddev
,
47 int cfgno
, int inum
, int asnum
, struct usb_host_endpoint
*ep
,
48 unsigned char *buffer
, int size
)
50 struct usb_ssp_isoc_ep_comp_descriptor
*desc
;
53 * The SuperSpeedPlus Isoc endpoint companion descriptor immediately
54 * follows the SuperSpeed Endpoint Companion descriptor
56 desc
= (struct usb_ssp_isoc_ep_comp_descriptor
*) buffer
;
57 if (desc
->bDescriptorType
!= USB_DT_SSP_ISOC_ENDPOINT_COMP
||
58 size
< USB_DT_SSP_ISOC_EP_COMP_SIZE
) {
59 dev_warn(ddev
, "Invalid SuperSpeedPlus isoc endpoint companion"
60 "for config %d interface %d altsetting %d ep %d.\n",
61 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
64 memcpy(&ep
->ssp_isoc_ep_comp
, desc
, USB_DT_SSP_ISOC_EP_COMP_SIZE
);
67 static void usb_parse_ss_endpoint_companion(struct device
*ddev
, int cfgno
,
68 int inum
, int asnum
, struct usb_host_endpoint
*ep
,
69 unsigned char *buffer
, int size
)
71 struct usb_ss_ep_comp_descriptor
*desc
;
74 /* The SuperSpeed endpoint companion descriptor is supposed to
75 * be the first thing immediately following the endpoint descriptor.
77 desc
= (struct usb_ss_ep_comp_descriptor
*) buffer
;
78 buffer
+= desc
->bLength
;
79 size
-= desc
->bLength
;
81 if (desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
||
82 size
< USB_DT_SS_EP_COMP_SIZE
) {
83 dev_warn(ddev
, "No SuperSpeed endpoint companion for config %d "
84 " interface %d altsetting %d ep %d: "
85 "using minimum values\n",
86 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
88 /* Fill in some default values.
89 * Leave bmAttributes as zero, which will mean no streams for
90 * bulk, and isoc won't support multiple bursts of packets.
91 * With bursts of only one packet, and a Mult of 1, the max
92 * amount of data moved per endpoint service interval is one
95 ep
->ss_ep_comp
.bLength
= USB_DT_SS_EP_COMP_SIZE
;
96 ep
->ss_ep_comp
.bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
;
97 if (usb_endpoint_xfer_isoc(&ep
->desc
) ||
98 usb_endpoint_xfer_int(&ep
->desc
))
99 ep
->ss_ep_comp
.wBytesPerInterval
=
100 ep
->desc
.wMaxPacketSize
;
104 memcpy(&ep
->ss_ep_comp
, desc
, USB_DT_SS_EP_COMP_SIZE
);
106 /* Check the various values */
107 if (usb_endpoint_xfer_control(&ep
->desc
) && desc
->bMaxBurst
!= 0) {
108 dev_warn(ddev
, "Control endpoint with bMaxBurst = %d in "
109 "config %d interface %d altsetting %d ep %d: "
110 "setting to zero\n", desc
->bMaxBurst
,
111 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
112 ep
->ss_ep_comp
.bMaxBurst
= 0;
113 } else if (desc
->bMaxBurst
> 15) {
114 dev_warn(ddev
, "Endpoint with bMaxBurst = %d in "
115 "config %d interface %d altsetting %d ep %d: "
116 "setting to 15\n", desc
->bMaxBurst
,
117 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
118 ep
->ss_ep_comp
.bMaxBurst
= 15;
121 if ((usb_endpoint_xfer_control(&ep
->desc
) ||
122 usb_endpoint_xfer_int(&ep
->desc
)) &&
123 desc
->bmAttributes
!= 0) {
124 dev_warn(ddev
, "%s endpoint with bmAttributes = %d in "
125 "config %d interface %d altsetting %d ep %d: "
127 usb_endpoint_xfer_control(&ep
->desc
) ? "Control" : "Bulk",
129 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
130 ep
->ss_ep_comp
.bmAttributes
= 0;
131 } else if (usb_endpoint_xfer_bulk(&ep
->desc
) &&
132 desc
->bmAttributes
> 16) {
133 dev_warn(ddev
, "Bulk endpoint with more than 65536 streams in "
134 "config %d interface %d altsetting %d ep %d: "
136 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
137 ep
->ss_ep_comp
.bmAttributes
= 16;
138 } else if (usb_endpoint_xfer_isoc(&ep
->desc
) &&
139 !USB_SS_SSP_ISOC_COMP(desc
->bmAttributes
) &&
140 USB_SS_MULT(desc
->bmAttributes
) > 3) {
141 dev_warn(ddev
, "Isoc endpoint has Mult of %d in "
142 "config %d interface %d altsetting %d ep %d: "
144 USB_SS_MULT(desc
->bmAttributes
),
145 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
146 ep
->ss_ep_comp
.bmAttributes
= 2;
149 /* Parse a possible SuperSpeedPlus isoc ep companion descriptor */
150 if (usb_endpoint_xfer_isoc(&ep
->desc
) &&
151 USB_SS_SSP_ISOC_COMP(desc
->bmAttributes
))
152 usb_parse_ssp_isoc_endpoint_companion(ddev
, cfgno
, inum
, asnum
,
155 if (usb_endpoint_xfer_isoc(&ep
->desc
))
156 max_tx
= (desc
->bMaxBurst
+ 1) *
157 (USB_SS_MULT(desc
->bmAttributes
)) *
158 usb_endpoint_maxp(&ep
->desc
);
159 else if (usb_endpoint_xfer_int(&ep
->desc
))
160 max_tx
= usb_endpoint_maxp(&ep
->desc
) *
161 (desc
->bMaxBurst
+ 1);
164 if (le16_to_cpu(desc
->wBytesPerInterval
) > max_tx
) {
165 dev_warn(ddev
, "%s endpoint with wBytesPerInterval of %d in "
166 "config %d interface %d altsetting %d ep %d: "
168 usb_endpoint_xfer_isoc(&ep
->desc
) ? "Isoc" : "Int",
169 le16_to_cpu(desc
->wBytesPerInterval
),
170 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
,
172 ep
->ss_ep_comp
.wBytesPerInterval
= cpu_to_le16(max_tx
);
176 static int usb_parse_endpoint(struct device
*ddev
, int cfgno
, int inum
,
177 int asnum
, struct usb_host_interface
*ifp
, int num_ep
,
178 unsigned char *buffer
, int size
)
180 unsigned char *buffer0
= buffer
;
181 struct usb_endpoint_descriptor
*d
;
182 struct usb_host_endpoint
*endpoint
;
185 d
= (struct usb_endpoint_descriptor
*) buffer
;
186 buffer
+= d
->bLength
;
189 if (d
->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
)
190 n
= USB_DT_ENDPOINT_AUDIO_SIZE
;
191 else if (d
->bLength
>= USB_DT_ENDPOINT_SIZE
)
192 n
= USB_DT_ENDPOINT_SIZE
;
194 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
195 "invalid endpoint descriptor of length %d, skipping\n",
196 cfgno
, inum
, asnum
, d
->bLength
);
197 goto skip_to_next_endpoint_or_interface_descriptor
;
200 i
= d
->bEndpointAddress
& ~USB_ENDPOINT_DIR_MASK
;
201 if (i
>= 16 || i
== 0) {
202 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
203 "invalid endpoint with address 0x%X, skipping\n",
204 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
205 goto skip_to_next_endpoint_or_interface_descriptor
;
208 /* Only store as many endpoints as we have room for */
209 if (ifp
->desc
.bNumEndpoints
>= num_ep
)
210 goto skip_to_next_endpoint_or_interface_descriptor
;
212 endpoint
= &ifp
->endpoint
[ifp
->desc
.bNumEndpoints
];
213 ++ifp
->desc
.bNumEndpoints
;
215 memcpy(&endpoint
->desc
, d
, n
);
216 INIT_LIST_HEAD(&endpoint
->urb_list
);
218 /* Fix up bInterval values outside the legal range. Use 32 ms if no
219 * proper value can be guessed. */
220 i
= 0; /* i = min, j = max, n = default */
222 if (usb_endpoint_xfer_int(d
)) {
224 switch (to_usb_device(ddev
)->speed
) {
225 case USB_SPEED_SUPER_PLUS
:
226 case USB_SPEED_SUPER
:
228 /* Many device manufacturers are using full-speed
229 * bInterval values in high-speed interrupt endpoint
230 * descriptors. Try to fix those and fall back to a
231 * 32 ms default value otherwise. */
232 n
= fls(d
->bInterval
*8);
234 n
= 9; /* 32 ms = 2^(9-1) uframes */
238 * Adjust bInterval for quirked devices.
239 * This quirk fixes bIntervals reported in
240 * linear microframes.
242 if (to_usb_device(ddev
)->quirks
&
243 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL
) {
244 n
= clamp(fls(d
->bInterval
), i
, j
);
248 default: /* USB_SPEED_FULL or _LOW */
249 /* For low-speed, 10 ms is the official minimum.
250 * But some "overclocked" devices might want faster
251 * polling so we'll allow it. */
255 } else if (usb_endpoint_xfer_isoc(d
)) {
258 switch (to_usb_device(ddev
)->speed
) {
260 n
= 9; /* 32 ms = 2^(9-1) uframes */
262 default: /* USB_SPEED_FULL */
263 n
= 6; /* 32 ms = 2^(6-1) frames */
267 if (d
->bInterval
< i
|| d
->bInterval
> j
) {
268 dev_warn(ddev
, "config %d interface %d altsetting %d "
269 "endpoint 0x%X has an invalid bInterval %d, "
272 d
->bEndpointAddress
, d
->bInterval
, n
);
273 endpoint
->desc
.bInterval
= n
;
276 /* Some buggy low-speed devices have Bulk endpoints, which is
277 * explicitly forbidden by the USB spec. In an attempt to make
278 * them usable, we will try treating them as Interrupt endpoints.
280 if (to_usb_device(ddev
)->speed
== USB_SPEED_LOW
&&
281 usb_endpoint_xfer_bulk(d
)) {
282 dev_warn(ddev
, "config %d interface %d altsetting %d "
283 "endpoint 0x%X is Bulk; changing to Interrupt\n",
284 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
285 endpoint
->desc
.bmAttributes
= USB_ENDPOINT_XFER_INT
;
286 endpoint
->desc
.bInterval
= 1;
287 if (usb_endpoint_maxp(&endpoint
->desc
) > 8)
288 endpoint
->desc
.wMaxPacketSize
= cpu_to_le16(8);
292 * Some buggy high speed devices have bulk endpoints using
293 * maxpacket sizes other than 512. High speed HCDs may not
294 * be able to handle that particular bug, so let's warn...
296 if (to_usb_device(ddev
)->speed
== USB_SPEED_HIGH
297 && usb_endpoint_xfer_bulk(d
)) {
300 maxp
= usb_endpoint_maxp(&endpoint
->desc
) & 0x07ff;
302 dev_warn(ddev
, "config %d interface %d altsetting %d "
303 "bulk endpoint 0x%X has invalid maxpacket %d\n",
304 cfgno
, inum
, asnum
, d
->bEndpointAddress
,
308 /* Parse a possible SuperSpeed endpoint companion descriptor */
309 if (to_usb_device(ddev
)->speed
>= USB_SPEED_SUPER
)
310 usb_parse_ss_endpoint_companion(ddev
, cfgno
,
311 inum
, asnum
, endpoint
, buffer
, size
);
313 /* Skip over any Class Specific or Vendor Specific descriptors;
314 * find the next endpoint or interface descriptor */
315 endpoint
->extra
= buffer
;
316 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
317 USB_DT_INTERFACE
, &n
);
318 endpoint
->extralen
= i
;
319 retval
= buffer
- buffer0
+ i
;
321 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
322 n
, plural(n
), "endpoint");
325 skip_to_next_endpoint_or_interface_descriptor
:
326 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
327 USB_DT_INTERFACE
, NULL
);
328 return buffer
- buffer0
+ i
;
331 void usb_release_interface_cache(struct kref
*ref
)
333 struct usb_interface_cache
*intfc
= ref_to_usb_interface_cache(ref
);
336 for (j
= 0; j
< intfc
->num_altsetting
; j
++) {
337 struct usb_host_interface
*alt
= &intfc
->altsetting
[j
];
339 kfree(alt
->endpoint
);
345 static int usb_parse_interface(struct device
*ddev
, int cfgno
,
346 struct usb_host_config
*config
, unsigned char *buffer
, int size
,
347 u8 inums
[], u8 nalts
[])
349 unsigned char *buffer0
= buffer
;
350 struct usb_interface_descriptor
*d
;
352 struct usb_interface_cache
*intfc
;
353 struct usb_host_interface
*alt
;
356 int num_ep
, num_ep_orig
;
358 d
= (struct usb_interface_descriptor
*) buffer
;
359 buffer
+= d
->bLength
;
362 if (d
->bLength
< USB_DT_INTERFACE_SIZE
)
363 goto skip_to_next_interface_descriptor
;
365 /* Which interface entry is this? */
367 inum
= d
->bInterfaceNumber
;
368 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
369 if (inums
[i
] == inum
) {
370 intfc
= config
->intf_cache
[i
];
374 if (!intfc
|| intfc
->num_altsetting
>= nalts
[i
])
375 goto skip_to_next_interface_descriptor
;
377 /* Check for duplicate altsetting entries */
378 asnum
= d
->bAlternateSetting
;
379 for ((i
= 0, alt
= &intfc
->altsetting
[0]);
380 i
< intfc
->num_altsetting
;
382 if (alt
->desc
.bAlternateSetting
== asnum
) {
383 dev_warn(ddev
, "Duplicate descriptor for config %d "
384 "interface %d altsetting %d, skipping\n",
386 goto skip_to_next_interface_descriptor
;
390 ++intfc
->num_altsetting
;
391 memcpy(&alt
->desc
, d
, USB_DT_INTERFACE_SIZE
);
393 /* Skip over any Class Specific or Vendor Specific descriptors;
394 * find the first endpoint or interface descriptor */
396 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
397 USB_DT_INTERFACE
, &n
);
400 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
401 n
, plural(n
), "interface");
405 /* Allocate space for the right(?) number of endpoints */
406 num_ep
= num_ep_orig
= alt
->desc
.bNumEndpoints
;
407 alt
->desc
.bNumEndpoints
= 0; /* Use as a counter */
408 if (num_ep
> USB_MAXENDPOINTS
) {
409 dev_warn(ddev
, "too many endpoints for config %d interface %d "
410 "altsetting %d: %d, using maximum allowed: %d\n",
411 cfgno
, inum
, asnum
, num_ep
, USB_MAXENDPOINTS
);
412 num_ep
= USB_MAXENDPOINTS
;
416 /* Can't allocate 0 bytes */
417 len
= sizeof(struct usb_host_endpoint
) * num_ep
;
418 alt
->endpoint
= kzalloc(len
, GFP_KERNEL
);
423 /* Parse all the endpoint descriptors */
426 if (((struct usb_descriptor_header
*) buffer
)->bDescriptorType
429 retval
= usb_parse_endpoint(ddev
, cfgno
, inum
, asnum
, alt
,
430 num_ep
, buffer
, size
);
439 if (n
!= num_ep_orig
)
440 dev_warn(ddev
, "config %d interface %d altsetting %d has %d "
441 "endpoint descriptor%s, different from the interface "
442 "descriptor's value: %d\n",
443 cfgno
, inum
, asnum
, n
, plural(n
), num_ep_orig
);
444 return buffer
- buffer0
;
446 skip_to_next_interface_descriptor
:
447 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
448 USB_DT_INTERFACE
, NULL
);
449 return buffer
- buffer0
+ i
;
452 static int usb_parse_configuration(struct usb_device
*dev
, int cfgidx
,
453 struct usb_host_config
*config
, unsigned char *buffer
, int size
)
455 struct device
*ddev
= &dev
->dev
;
456 unsigned char *buffer0
= buffer
;
458 int nintf
, nintf_orig
;
460 struct usb_interface_cache
*intfc
;
461 unsigned char *buffer2
;
463 struct usb_descriptor_header
*header
;
465 u8 inums
[USB_MAXINTERFACES
], nalts
[USB_MAXINTERFACES
];
466 unsigned iad_num
= 0;
468 memcpy(&config
->desc
, buffer
, USB_DT_CONFIG_SIZE
);
469 if (config
->desc
.bDescriptorType
!= USB_DT_CONFIG
||
470 config
->desc
.bLength
< USB_DT_CONFIG_SIZE
||
471 config
->desc
.bLength
> size
) {
472 dev_err(ddev
, "invalid descriptor for config index %d: "
473 "type = 0x%X, length = %d\n", cfgidx
,
474 config
->desc
.bDescriptorType
, config
->desc
.bLength
);
477 cfgno
= config
->desc
.bConfigurationValue
;
479 buffer
+= config
->desc
.bLength
;
480 size
-= config
->desc
.bLength
;
482 nintf
= nintf_orig
= config
->desc
.bNumInterfaces
;
483 if (nintf
> USB_MAXINTERFACES
) {
484 dev_warn(ddev
, "config %d has too many interfaces: %d, "
485 "using maximum allowed: %d\n",
486 cfgno
, nintf
, USB_MAXINTERFACES
);
487 nintf
= USB_MAXINTERFACES
;
490 /* Go through the descriptors, checking their length and counting the
491 * number of altsettings for each interface */
493 for ((buffer2
= buffer
, size2
= size
);
495 (buffer2
+= header
->bLength
, size2
-= header
->bLength
)) {
497 if (size2
< sizeof(struct usb_descriptor_header
)) {
498 dev_warn(ddev
, "config %d descriptor has %d excess "
499 "byte%s, ignoring\n",
500 cfgno
, size2
, plural(size2
));
504 header
= (struct usb_descriptor_header
*) buffer2
;
505 if ((header
->bLength
> size2
) || (header
->bLength
< 2)) {
506 dev_warn(ddev
, "config %d has an invalid descriptor "
507 "of length %d, skipping remainder of the config\n",
508 cfgno
, header
->bLength
);
512 if (header
->bDescriptorType
== USB_DT_INTERFACE
) {
513 struct usb_interface_descriptor
*d
;
516 d
= (struct usb_interface_descriptor
*) header
;
517 if (d
->bLength
< USB_DT_INTERFACE_SIZE
) {
518 dev_warn(ddev
, "config %d has an invalid "
519 "interface descriptor of length %d, "
520 "skipping\n", cfgno
, d
->bLength
);
524 inum
= d
->bInterfaceNumber
;
526 if ((dev
->quirks
& USB_QUIRK_HONOR_BNUMINTERFACES
) &&
528 dev_warn(ddev
, "config %d has more interface "
529 "descriptors, than it declares in "
530 "bNumInterfaces, ignoring interface "
531 "number: %d\n", cfgno
, inum
);
535 if (inum
>= nintf_orig
)
536 dev_warn(ddev
, "config %d has an invalid "
537 "interface number: %d but max is %d\n",
538 cfgno
, inum
, nintf_orig
- 1);
540 /* Have we already encountered this interface?
541 * Count its altsettings */
542 for (i
= 0; i
< n
; ++i
) {
543 if (inums
[i
] == inum
)
549 } else if (n
< USB_MAXINTERFACES
) {
555 } else if (header
->bDescriptorType
==
556 USB_DT_INTERFACE_ASSOCIATION
) {
557 if (iad_num
== USB_MAXIADS
) {
558 dev_warn(ddev
, "found more Interface "
559 "Association Descriptors "
560 "than allocated for in "
561 "configuration %d\n", cfgno
);
563 config
->intf_assoc
[iad_num
] =
564 (struct usb_interface_assoc_descriptor
569 } else if (header
->bDescriptorType
== USB_DT_DEVICE
||
570 header
->bDescriptorType
== USB_DT_CONFIG
)
571 dev_warn(ddev
, "config %d contains an unexpected "
572 "descriptor of type 0x%X, skipping\n",
573 cfgno
, header
->bDescriptorType
);
575 } /* for ((buffer2 = buffer, size2 = size); ...) */
576 size
= buffer2
- buffer
;
577 config
->desc
.wTotalLength
= cpu_to_le16(buffer2
- buffer0
);
580 dev_warn(ddev
, "config %d has %d interface%s, different from "
581 "the descriptor's value: %d\n",
582 cfgno
, n
, plural(n
), nintf_orig
);
584 dev_warn(ddev
, "config %d has no interfaces?\n", cfgno
);
585 config
->desc
.bNumInterfaces
= nintf
= n
;
587 /* Check for missing interface numbers */
588 for (i
= 0; i
< nintf
; ++i
) {
589 for (j
= 0; j
< nintf
; ++j
) {
594 dev_warn(ddev
, "config %d has no interface number "
598 /* Allocate the usb_interface_caches and altsetting arrays */
599 for (i
= 0; i
< nintf
; ++i
) {
601 if (j
> USB_MAXALTSETTING
) {
602 dev_warn(ddev
, "too many alternate settings for "
603 "config %d interface %d: %d, "
604 "using maximum allowed: %d\n",
605 cfgno
, inums
[i
], j
, USB_MAXALTSETTING
);
606 nalts
[i
] = j
= USB_MAXALTSETTING
;
609 len
= sizeof(*intfc
) + sizeof(struct usb_host_interface
) * j
;
610 config
->intf_cache
[i
] = intfc
= kzalloc(len
, GFP_KERNEL
);
613 kref_init(&intfc
->ref
);
616 /* FIXME: parse the BOS descriptor */
618 /* Skip over any Class Specific or Vendor Specific descriptors;
619 * find the first interface descriptor */
620 config
->extra
= buffer
;
621 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
622 USB_DT_INTERFACE
, &n
);
623 config
->extralen
= i
;
625 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
626 n
, plural(n
), "configuration");
630 /* Parse all the interface/altsetting descriptors */
632 retval
= usb_parse_interface(ddev
, cfgno
, config
,
633 buffer
, size
, inums
, nalts
);
641 /* Check for missing altsettings */
642 for (i
= 0; i
< nintf
; ++i
) {
643 intfc
= config
->intf_cache
[i
];
644 for (j
= 0; j
< intfc
->num_altsetting
; ++j
) {
645 for (n
= 0; n
< intfc
->num_altsetting
; ++n
) {
646 if (intfc
->altsetting
[n
].desc
.
647 bAlternateSetting
== j
)
650 if (n
>= intfc
->num_altsetting
)
651 dev_warn(ddev
, "config %d interface %d has no "
652 "altsetting %d\n", cfgno
, inums
[i
], j
);
659 /* hub-only!! ... and only exported for reset/reinit path.
660 * otherwise used internally on disconnect/destroy path
662 void usb_destroy_configuration(struct usb_device
*dev
)
669 if (dev
->rawdescriptors
) {
670 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++)
671 kfree(dev
->rawdescriptors
[i
]);
673 kfree(dev
->rawdescriptors
);
674 dev
->rawdescriptors
= NULL
;
677 for (c
= 0; c
< dev
->descriptor
.bNumConfigurations
; c
++) {
678 struct usb_host_config
*cf
= &dev
->config
[c
];
681 for (i
= 0; i
< cf
->desc
.bNumInterfaces
; i
++) {
682 if (cf
->intf_cache
[i
])
683 kref_put(&cf
->intf_cache
[i
]->ref
,
684 usb_release_interface_cache
);
693 * Get the USB config descriptors, cache and parse'em
695 * hub-only!! ... and only in reset path, or usb_new_device()
696 * (used by real hubs and virtual root hubs)
698 int usb_get_configuration(struct usb_device
*dev
)
700 struct device
*ddev
= &dev
->dev
;
701 int ncfg
= dev
->descriptor
.bNumConfigurations
;
703 unsigned int cfgno
, length
;
704 unsigned char *bigbuffer
;
705 struct usb_config_descriptor
*desc
;
709 if (ncfg
> USB_MAXCONFIG
) {
710 dev_warn(ddev
, "too many configurations: %d, "
711 "using maximum allowed: %d\n", ncfg
, USB_MAXCONFIG
);
712 dev
->descriptor
.bNumConfigurations
= ncfg
= USB_MAXCONFIG
;
716 dev_err(ddev
, "no configurations\n");
720 length
= ncfg
* sizeof(struct usb_host_config
);
721 dev
->config
= kzalloc(length
, GFP_KERNEL
);
725 length
= ncfg
* sizeof(char *);
726 dev
->rawdescriptors
= kzalloc(length
, GFP_KERNEL
);
727 if (!dev
->rawdescriptors
)
730 desc
= kmalloc(USB_DT_CONFIG_SIZE
, GFP_KERNEL
);
735 for (; cfgno
< ncfg
; cfgno
++) {
736 /* We grab just the first descriptor so we know how long
737 * the whole configuration is */
738 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
739 desc
, USB_DT_CONFIG_SIZE
);
741 dev_err(ddev
, "unable to read config index %d "
742 "descriptor/%s: %d\n", cfgno
, "start", result
);
743 if (result
!= -EPIPE
)
745 dev_err(ddev
, "chopping to %d config(s)\n", cfgno
);
746 dev
->descriptor
.bNumConfigurations
= cfgno
;
748 } else if (result
< 4) {
749 dev_err(ddev
, "config index %d descriptor too short "
750 "(expected %i, got %i)\n", cfgno
,
751 USB_DT_CONFIG_SIZE
, result
);
755 length
= max((int) le16_to_cpu(desc
->wTotalLength
),
758 /* Now that we know the length, get the whole thing */
759 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
765 if (dev
->quirks
& USB_QUIRK_DELAY_INIT
)
768 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
771 dev_err(ddev
, "unable to read config index %d "
772 "descriptor/%s\n", cfgno
, "all");
776 if (result
< length
) {
777 dev_warn(ddev
, "config index %d descriptor too short "
778 "(expected %i, got %i)\n", cfgno
, length
, result
);
782 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
784 result
= usb_parse_configuration(dev
, cfgno
,
785 &dev
->config
[cfgno
], bigbuffer
, length
);
795 dev
->descriptor
.bNumConfigurations
= cfgno
;
797 if (result
== -ENOMEM
)
798 dev_err(ddev
, "out of memory\n");
802 void usb_release_bos_descriptor(struct usb_device
*dev
)
805 kfree(dev
->bos
->desc
);
811 /* Get BOS descriptor set */
812 int usb_get_bos_descriptor(struct usb_device
*dev
)
814 struct device
*ddev
= &dev
->dev
;
815 struct usb_bos_descriptor
*bos
;
816 struct usb_dev_cap_header
*cap
;
817 unsigned char *buffer
;
818 int length
, total_len
, num
, i
;
821 bos
= kzalloc(sizeof(struct usb_bos_descriptor
), GFP_KERNEL
);
825 /* Get BOS descriptor */
826 ret
= usb_get_descriptor(dev
, USB_DT_BOS
, 0, bos
, USB_DT_BOS_SIZE
);
827 if (ret
< USB_DT_BOS_SIZE
) {
828 dev_err(ddev
, "unable to get BOS descriptor\n");
835 length
= bos
->bLength
;
836 total_len
= le16_to_cpu(bos
->wTotalLength
);
837 num
= bos
->bNumDeviceCaps
;
839 if (total_len
< length
)
842 dev
->bos
= kzalloc(sizeof(struct usb_host_bos
), GFP_KERNEL
);
846 /* Now let's get the whole BOS descriptor set */
847 buffer
= kzalloc(total_len
, GFP_KERNEL
);
852 dev
->bos
->desc
= (struct usb_bos_descriptor
*)buffer
;
854 ret
= usb_get_descriptor(dev
, USB_DT_BOS
, 0, buffer
, total_len
);
855 if (ret
< total_len
) {
856 dev_err(ddev
, "unable to get BOS descriptor set\n");
863 for (i
= 0; i
< num
; i
++) {
865 cap
= (struct usb_dev_cap_header
*)buffer
;
866 length
= cap
->bLength
;
868 if (total_len
< length
)
872 if (cap
->bDescriptorType
!= USB_DT_DEVICE_CAPABILITY
) {
873 dev_warn(ddev
, "descriptor type invalid, skip\n");
877 switch (cap
->bDevCapabilityType
) {
878 case USB_CAP_TYPE_WIRELESS_USB
:
879 /* Wireless USB cap descriptor is handled by wusb */
881 case USB_CAP_TYPE_EXT
:
883 (struct usb_ext_cap_descriptor
*)buffer
;
885 case USB_SS_CAP_TYPE
:
887 (struct usb_ss_cap_descriptor
*)buffer
;
889 case USB_SSP_CAP_TYPE
:
891 (struct usb_ssp_cap_descriptor
*)buffer
;
893 case CONTAINER_ID_TYPE
:
895 (struct usb_ss_container_id_descriptor
*)buffer
;
897 case USB_PTM_CAP_TYPE
:
899 (struct usb_ptm_cap_descriptor
*)buffer
;
908 usb_release_bos_descriptor(dev
);