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_ss_endpoint_companion(struct device
*ddev
, int cfgno
,
47 int inum
, int asnum
, struct usb_host_endpoint
*ep
,
48 unsigned char *buffer
, int size
)
50 struct usb_ss_ep_comp_descriptor
*desc
;
53 /* The SuperSpeed endpoint companion descriptor is supposed to
54 * be the first thing immediately following the endpoint descriptor.
56 desc
= (struct usb_ss_ep_comp_descriptor
*) buffer
;
57 if (desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
||
58 size
< USB_DT_SS_EP_COMP_SIZE
) {
59 dev_warn(ddev
, "No SuperSpeed endpoint companion for config %d "
60 " interface %d altsetting %d ep %d: "
61 "using minimum values\n",
62 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
64 /* Fill in some default values.
65 * Leave bmAttributes as zero, which will mean no streams for
66 * bulk, and isoc won't support multiple bursts of packets.
67 * With bursts of only one packet, and a Mult of 1, the max
68 * amount of data moved per endpoint service interval is one
71 ep
->ss_ep_comp
.bLength
= USB_DT_SS_EP_COMP_SIZE
;
72 ep
->ss_ep_comp
.bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
;
73 if (usb_endpoint_xfer_isoc(&ep
->desc
) ||
74 usb_endpoint_xfer_int(&ep
->desc
))
75 ep
->ss_ep_comp
.wBytesPerInterval
=
76 ep
->desc
.wMaxPacketSize
;
80 memcpy(&ep
->ss_ep_comp
, desc
, USB_DT_SS_EP_COMP_SIZE
);
82 /* Check the various values */
83 if (usb_endpoint_xfer_control(&ep
->desc
) && desc
->bMaxBurst
!= 0) {
84 dev_warn(ddev
, "Control endpoint with bMaxBurst = %d in "
85 "config %d interface %d altsetting %d ep %d: "
86 "setting to zero\n", desc
->bMaxBurst
,
87 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
88 ep
->ss_ep_comp
.bMaxBurst
= 0;
89 } else if (desc
->bMaxBurst
> 15) {
90 dev_warn(ddev
, "Endpoint with bMaxBurst = %d in "
91 "config %d interface %d altsetting %d ep %d: "
92 "setting to 15\n", desc
->bMaxBurst
,
93 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
94 ep
->ss_ep_comp
.bMaxBurst
= 15;
97 if ((usb_endpoint_xfer_control(&ep
->desc
) ||
98 usb_endpoint_xfer_int(&ep
->desc
)) &&
99 desc
->bmAttributes
!= 0) {
100 dev_warn(ddev
, "%s endpoint with bmAttributes = %d in "
101 "config %d interface %d altsetting %d ep %d: "
103 usb_endpoint_xfer_control(&ep
->desc
) ? "Control" : "Bulk",
105 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
106 ep
->ss_ep_comp
.bmAttributes
= 0;
107 } else if (usb_endpoint_xfer_bulk(&ep
->desc
) &&
108 desc
->bmAttributes
> 16) {
109 dev_warn(ddev
, "Bulk endpoint with more than 65536 streams in "
110 "config %d interface %d altsetting %d ep %d: "
112 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
113 ep
->ss_ep_comp
.bmAttributes
= 16;
114 } else if (usb_endpoint_xfer_isoc(&ep
->desc
) &&
115 USB_SS_MULT(desc
->bmAttributes
) > 3) {
116 dev_warn(ddev
, "Isoc endpoint has Mult of %d in "
117 "config %d interface %d altsetting %d ep %d: "
118 "setting to 3\n", desc
->bmAttributes
+ 1,
119 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
120 ep
->ss_ep_comp
.bmAttributes
= 2;
123 if (usb_endpoint_xfer_isoc(&ep
->desc
))
124 max_tx
= (desc
->bMaxBurst
+ 1) *
125 (USB_SS_MULT(desc
->bmAttributes
)) *
126 usb_endpoint_maxp(&ep
->desc
);
127 else if (usb_endpoint_xfer_int(&ep
->desc
))
128 max_tx
= usb_endpoint_maxp(&ep
->desc
) *
129 (desc
->bMaxBurst
+ 1);
132 if (le16_to_cpu(desc
->wBytesPerInterval
) > max_tx
) {
133 dev_warn(ddev
, "%s endpoint with wBytesPerInterval of %d in "
134 "config %d interface %d altsetting %d ep %d: "
136 usb_endpoint_xfer_isoc(&ep
->desc
) ? "Isoc" : "Int",
137 le16_to_cpu(desc
->wBytesPerInterval
),
138 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
,
140 ep
->ss_ep_comp
.wBytesPerInterval
= cpu_to_le16(max_tx
);
144 static int usb_parse_endpoint(struct device
*ddev
, int cfgno
, int inum
,
145 int asnum
, struct usb_host_interface
*ifp
, int num_ep
,
146 unsigned char *buffer
, int size
)
148 unsigned char *buffer0
= buffer
;
149 struct usb_endpoint_descriptor
*d
;
150 struct usb_host_endpoint
*endpoint
;
153 d
= (struct usb_endpoint_descriptor
*) buffer
;
154 buffer
+= d
->bLength
;
157 if (d
->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
)
158 n
= USB_DT_ENDPOINT_AUDIO_SIZE
;
159 else if (d
->bLength
>= USB_DT_ENDPOINT_SIZE
)
160 n
= USB_DT_ENDPOINT_SIZE
;
162 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
163 "invalid endpoint descriptor of length %d, skipping\n",
164 cfgno
, inum
, asnum
, d
->bLength
);
165 goto skip_to_next_endpoint_or_interface_descriptor
;
168 i
= d
->bEndpointAddress
& ~USB_ENDPOINT_DIR_MASK
;
169 if (i
>= 16 || i
== 0) {
170 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
171 "invalid endpoint with address 0x%X, skipping\n",
172 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
173 goto skip_to_next_endpoint_or_interface_descriptor
;
176 /* Only store as many endpoints as we have room for */
177 if (ifp
->desc
.bNumEndpoints
>= num_ep
)
178 goto skip_to_next_endpoint_or_interface_descriptor
;
180 endpoint
= &ifp
->endpoint
[ifp
->desc
.bNumEndpoints
];
181 ++ifp
->desc
.bNumEndpoints
;
183 memcpy(&endpoint
->desc
, d
, n
);
184 INIT_LIST_HEAD(&endpoint
->urb_list
);
186 /* Fix up bInterval values outside the legal range. Use 32 ms if no
187 * proper value can be guessed. */
188 i
= 0; /* i = min, j = max, n = default */
190 if (usb_endpoint_xfer_int(d
)) {
192 switch (to_usb_device(ddev
)->speed
) {
193 case USB_SPEED_SUPER
:
195 /* Many device manufacturers are using full-speed
196 * bInterval values in high-speed interrupt endpoint
197 * descriptors. Try to fix those and fall back to a
198 * 32 ms default value otherwise. */
199 n
= fls(d
->bInterval
*8);
201 n
= 9; /* 32 ms = 2^(9-1) uframes */
205 * Adjust bInterval for quirked devices.
206 * This quirk fixes bIntervals reported in
207 * linear microframes.
209 if (to_usb_device(ddev
)->quirks
&
210 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL
) {
211 n
= clamp(fls(d
->bInterval
), i
, j
);
215 default: /* USB_SPEED_FULL or _LOW */
216 /* For low-speed, 10 ms is the official minimum.
217 * But some "overclocked" devices might want faster
218 * polling so we'll allow it. */
222 } else if (usb_endpoint_xfer_isoc(d
)) {
225 switch (to_usb_device(ddev
)->speed
) {
227 n
= 9; /* 32 ms = 2^(9-1) uframes */
229 default: /* USB_SPEED_FULL */
230 n
= 6; /* 32 ms = 2^(6-1) frames */
234 if (d
->bInterval
< i
|| d
->bInterval
> j
) {
235 dev_warn(ddev
, "config %d interface %d altsetting %d "
236 "endpoint 0x%X has an invalid bInterval %d, "
239 d
->bEndpointAddress
, d
->bInterval
, n
);
240 endpoint
->desc
.bInterval
= n
;
243 /* Some buggy low-speed devices have Bulk endpoints, which is
244 * explicitly forbidden by the USB spec. In an attempt to make
245 * them usable, we will try treating them as Interrupt endpoints.
247 if (to_usb_device(ddev
)->speed
== USB_SPEED_LOW
&&
248 usb_endpoint_xfer_bulk(d
)) {
249 dev_warn(ddev
, "config %d interface %d altsetting %d "
250 "endpoint 0x%X is Bulk; changing to Interrupt\n",
251 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
252 endpoint
->desc
.bmAttributes
= USB_ENDPOINT_XFER_INT
;
253 endpoint
->desc
.bInterval
= 1;
254 if (usb_endpoint_maxp(&endpoint
->desc
) > 8)
255 endpoint
->desc
.wMaxPacketSize
= cpu_to_le16(8);
259 * Some buggy high speed devices have bulk endpoints using
260 * maxpacket sizes other than 512. High speed HCDs may not
261 * be able to handle that particular bug, so let's warn...
263 if (to_usb_device(ddev
)->speed
== USB_SPEED_HIGH
264 && usb_endpoint_xfer_bulk(d
)) {
267 maxp
= usb_endpoint_maxp(&endpoint
->desc
) & 0x07ff;
269 dev_warn(ddev
, "config %d interface %d altsetting %d "
270 "bulk endpoint 0x%X has invalid maxpacket %d\n",
271 cfgno
, inum
, asnum
, d
->bEndpointAddress
,
275 /* Parse a possible SuperSpeed endpoint companion descriptor */
276 if (to_usb_device(ddev
)->speed
== USB_SPEED_SUPER
)
277 usb_parse_ss_endpoint_companion(ddev
, cfgno
,
278 inum
, asnum
, endpoint
, buffer
, size
);
280 /* Skip over any Class Specific or Vendor Specific descriptors;
281 * find the next endpoint or interface descriptor */
282 endpoint
->extra
= buffer
;
283 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
284 USB_DT_INTERFACE
, &n
);
285 endpoint
->extralen
= i
;
286 retval
= buffer
- buffer0
+ i
;
288 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
289 n
, plural(n
), "endpoint");
292 skip_to_next_endpoint_or_interface_descriptor
:
293 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
294 USB_DT_INTERFACE
, NULL
);
295 return buffer
- buffer0
+ i
;
298 void usb_release_interface_cache(struct kref
*ref
)
300 struct usb_interface_cache
*intfc
= ref_to_usb_interface_cache(ref
);
303 for (j
= 0; j
< intfc
->num_altsetting
; j
++) {
304 struct usb_host_interface
*alt
= &intfc
->altsetting
[j
];
306 kfree(alt
->endpoint
);
312 static int usb_parse_interface(struct device
*ddev
, int cfgno
,
313 struct usb_host_config
*config
, unsigned char *buffer
, int size
,
314 u8 inums
[], u8 nalts
[])
316 unsigned char *buffer0
= buffer
;
317 struct usb_interface_descriptor
*d
;
319 struct usb_interface_cache
*intfc
;
320 struct usb_host_interface
*alt
;
323 int num_ep
, num_ep_orig
;
325 d
= (struct usb_interface_descriptor
*) buffer
;
326 buffer
+= d
->bLength
;
329 if (d
->bLength
< USB_DT_INTERFACE_SIZE
)
330 goto skip_to_next_interface_descriptor
;
332 /* Which interface entry is this? */
334 inum
= d
->bInterfaceNumber
;
335 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
336 if (inums
[i
] == inum
) {
337 intfc
= config
->intf_cache
[i
];
341 if (!intfc
|| intfc
->num_altsetting
>= nalts
[i
])
342 goto skip_to_next_interface_descriptor
;
344 /* Check for duplicate altsetting entries */
345 asnum
= d
->bAlternateSetting
;
346 for ((i
= 0, alt
= &intfc
->altsetting
[0]);
347 i
< intfc
->num_altsetting
;
349 if (alt
->desc
.bAlternateSetting
== asnum
) {
350 dev_warn(ddev
, "Duplicate descriptor for config %d "
351 "interface %d altsetting %d, skipping\n",
353 goto skip_to_next_interface_descriptor
;
357 ++intfc
->num_altsetting
;
358 memcpy(&alt
->desc
, d
, USB_DT_INTERFACE_SIZE
);
360 /* Skip over any Class Specific or Vendor Specific descriptors;
361 * find the first endpoint or interface descriptor */
363 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
364 USB_DT_INTERFACE
, &n
);
367 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
368 n
, plural(n
), "interface");
372 /* Allocate space for the right(?) number of endpoints */
373 num_ep
= num_ep_orig
= alt
->desc
.bNumEndpoints
;
374 alt
->desc
.bNumEndpoints
= 0; /* Use as a counter */
375 if (num_ep
> USB_MAXENDPOINTS
) {
376 dev_warn(ddev
, "too many endpoints for config %d interface %d "
377 "altsetting %d: %d, using maximum allowed: %d\n",
378 cfgno
, inum
, asnum
, num_ep
, USB_MAXENDPOINTS
);
379 num_ep
= USB_MAXENDPOINTS
;
383 /* Can't allocate 0 bytes */
384 len
= sizeof(struct usb_host_endpoint
) * num_ep
;
385 alt
->endpoint
= kzalloc(len
, GFP_KERNEL
);
390 /* Parse all the endpoint descriptors */
393 if (((struct usb_descriptor_header
*) buffer
)->bDescriptorType
396 retval
= usb_parse_endpoint(ddev
, cfgno
, inum
, asnum
, alt
,
397 num_ep
, buffer
, size
);
406 if (n
!= num_ep_orig
)
407 dev_warn(ddev
, "config %d interface %d altsetting %d has %d "
408 "endpoint descriptor%s, different from the interface "
409 "descriptor's value: %d\n",
410 cfgno
, inum
, asnum
, n
, plural(n
), num_ep_orig
);
411 return buffer
- buffer0
;
413 skip_to_next_interface_descriptor
:
414 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
415 USB_DT_INTERFACE
, NULL
);
416 return buffer
- buffer0
+ i
;
419 static int usb_parse_configuration(struct usb_device
*dev
, int cfgidx
,
420 struct usb_host_config
*config
, unsigned char *buffer
, int size
)
422 struct device
*ddev
= &dev
->dev
;
423 unsigned char *buffer0
= buffer
;
425 int nintf
, nintf_orig
;
427 struct usb_interface_cache
*intfc
;
428 unsigned char *buffer2
;
430 struct usb_descriptor_header
*header
;
432 u8 inums
[USB_MAXINTERFACES
], nalts
[USB_MAXINTERFACES
];
433 unsigned iad_num
= 0;
435 memcpy(&config
->desc
, buffer
, USB_DT_CONFIG_SIZE
);
436 if (config
->desc
.bDescriptorType
!= USB_DT_CONFIG
||
437 config
->desc
.bLength
< USB_DT_CONFIG_SIZE
||
438 config
->desc
.bLength
> size
) {
439 dev_err(ddev
, "invalid descriptor for config index %d: "
440 "type = 0x%X, length = %d\n", cfgidx
,
441 config
->desc
.bDescriptorType
, config
->desc
.bLength
);
444 cfgno
= config
->desc
.bConfigurationValue
;
446 buffer
+= config
->desc
.bLength
;
447 size
-= config
->desc
.bLength
;
449 nintf
= nintf_orig
= config
->desc
.bNumInterfaces
;
450 if (nintf
> USB_MAXINTERFACES
) {
451 dev_warn(ddev
, "config %d has too many interfaces: %d, "
452 "using maximum allowed: %d\n",
453 cfgno
, nintf
, USB_MAXINTERFACES
);
454 nintf
= USB_MAXINTERFACES
;
457 /* Go through the descriptors, checking their length and counting the
458 * number of altsettings for each interface */
460 for ((buffer2
= buffer
, size2
= size
);
462 (buffer2
+= header
->bLength
, size2
-= header
->bLength
)) {
464 if (size2
< sizeof(struct usb_descriptor_header
)) {
465 dev_warn(ddev
, "config %d descriptor has %d excess "
466 "byte%s, ignoring\n",
467 cfgno
, size2
, plural(size2
));
471 header
= (struct usb_descriptor_header
*) buffer2
;
472 if ((header
->bLength
> size2
) || (header
->bLength
< 2)) {
473 dev_warn(ddev
, "config %d has an invalid descriptor "
474 "of length %d, skipping remainder of the config\n",
475 cfgno
, header
->bLength
);
479 if (header
->bDescriptorType
== USB_DT_INTERFACE
) {
480 struct usb_interface_descriptor
*d
;
483 d
= (struct usb_interface_descriptor
*) header
;
484 if (d
->bLength
< USB_DT_INTERFACE_SIZE
) {
485 dev_warn(ddev
, "config %d has an invalid "
486 "interface descriptor of length %d, "
487 "skipping\n", cfgno
, d
->bLength
);
491 inum
= d
->bInterfaceNumber
;
493 if ((dev
->quirks
& USB_QUIRK_HONOR_BNUMINTERFACES
) &&
495 dev_warn(ddev
, "config %d has more interface "
496 "descriptors, than it declares in "
497 "bNumInterfaces, ignoring interface "
498 "number: %d\n", cfgno
, inum
);
502 if (inum
>= nintf_orig
)
503 dev_warn(ddev
, "config %d has an invalid "
504 "interface number: %d but max is %d\n",
505 cfgno
, inum
, nintf_orig
- 1);
507 /* Have we already encountered this interface?
508 * Count its altsettings */
509 for (i
= 0; i
< n
; ++i
) {
510 if (inums
[i
] == inum
)
516 } else if (n
< USB_MAXINTERFACES
) {
522 } else if (header
->bDescriptorType
==
523 USB_DT_INTERFACE_ASSOCIATION
) {
524 if (iad_num
== USB_MAXIADS
) {
525 dev_warn(ddev
, "found more Interface "
526 "Association Descriptors "
527 "than allocated for in "
528 "configuration %d\n", cfgno
);
530 config
->intf_assoc
[iad_num
] =
531 (struct usb_interface_assoc_descriptor
536 } else if (header
->bDescriptorType
== USB_DT_DEVICE
||
537 header
->bDescriptorType
== USB_DT_CONFIG
)
538 dev_warn(ddev
, "config %d contains an unexpected "
539 "descriptor of type 0x%X, skipping\n",
540 cfgno
, header
->bDescriptorType
);
542 } /* for ((buffer2 = buffer, size2 = size); ...) */
543 size
= buffer2
- buffer
;
544 config
->desc
.wTotalLength
= cpu_to_le16(buffer2
- buffer0
);
547 dev_warn(ddev
, "config %d has %d interface%s, different from "
548 "the descriptor's value: %d\n",
549 cfgno
, n
, plural(n
), nintf_orig
);
551 dev_warn(ddev
, "config %d has no interfaces?\n", cfgno
);
552 config
->desc
.bNumInterfaces
= nintf
= n
;
554 /* Check for missing interface numbers */
555 for (i
= 0; i
< nintf
; ++i
) {
556 for (j
= 0; j
< nintf
; ++j
) {
561 dev_warn(ddev
, "config %d has no interface number "
565 /* Allocate the usb_interface_caches and altsetting arrays */
566 for (i
= 0; i
< nintf
; ++i
) {
568 if (j
> USB_MAXALTSETTING
) {
569 dev_warn(ddev
, "too many alternate settings for "
570 "config %d interface %d: %d, "
571 "using maximum allowed: %d\n",
572 cfgno
, inums
[i
], j
, USB_MAXALTSETTING
);
573 nalts
[i
] = j
= USB_MAXALTSETTING
;
576 len
= sizeof(*intfc
) + sizeof(struct usb_host_interface
) * j
;
577 config
->intf_cache
[i
] = intfc
= kzalloc(len
, GFP_KERNEL
);
580 kref_init(&intfc
->ref
);
583 /* FIXME: parse the BOS descriptor */
585 /* Skip over any Class Specific or Vendor Specific descriptors;
586 * find the first interface descriptor */
587 config
->extra
= buffer
;
588 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
589 USB_DT_INTERFACE
, &n
);
590 config
->extralen
= i
;
592 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
593 n
, plural(n
), "configuration");
597 /* Parse all the interface/altsetting descriptors */
599 retval
= usb_parse_interface(ddev
, cfgno
, config
,
600 buffer
, size
, inums
, nalts
);
608 /* Check for missing altsettings */
609 for (i
= 0; i
< nintf
; ++i
) {
610 intfc
= config
->intf_cache
[i
];
611 for (j
= 0; j
< intfc
->num_altsetting
; ++j
) {
612 for (n
= 0; n
< intfc
->num_altsetting
; ++n
) {
613 if (intfc
->altsetting
[n
].desc
.
614 bAlternateSetting
== j
)
617 if (n
>= intfc
->num_altsetting
)
618 dev_warn(ddev
, "config %d interface %d has no "
619 "altsetting %d\n", cfgno
, inums
[i
], j
);
626 /* hub-only!! ... and only exported for reset/reinit path.
627 * otherwise used internally on disconnect/destroy path
629 void usb_destroy_configuration(struct usb_device
*dev
)
636 if (dev
->rawdescriptors
) {
637 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++)
638 kfree(dev
->rawdescriptors
[i
]);
640 kfree(dev
->rawdescriptors
);
641 dev
->rawdescriptors
= NULL
;
644 for (c
= 0; c
< dev
->descriptor
.bNumConfigurations
; c
++) {
645 struct usb_host_config
*cf
= &dev
->config
[c
];
648 for (i
= 0; i
< cf
->desc
.bNumInterfaces
; i
++) {
649 if (cf
->intf_cache
[i
])
650 kref_put(&cf
->intf_cache
[i
]->ref
,
651 usb_release_interface_cache
);
660 * Get the USB config descriptors, cache and parse'em
662 * hub-only!! ... and only in reset path, or usb_new_device()
663 * (used by real hubs and virtual root hubs)
665 int usb_get_configuration(struct usb_device
*dev
)
667 struct device
*ddev
= &dev
->dev
;
668 int ncfg
= dev
->descriptor
.bNumConfigurations
;
670 unsigned int cfgno
, length
;
671 unsigned char *bigbuffer
;
672 struct usb_config_descriptor
*desc
;
676 if (ncfg
> USB_MAXCONFIG
) {
677 dev_warn(ddev
, "too many configurations: %d, "
678 "using maximum allowed: %d\n", ncfg
, USB_MAXCONFIG
);
679 dev
->descriptor
.bNumConfigurations
= ncfg
= USB_MAXCONFIG
;
683 dev_err(ddev
, "no configurations\n");
687 length
= ncfg
* sizeof(struct usb_host_config
);
688 dev
->config
= kzalloc(length
, GFP_KERNEL
);
692 length
= ncfg
* sizeof(char *);
693 dev
->rawdescriptors
= kzalloc(length
, GFP_KERNEL
);
694 if (!dev
->rawdescriptors
)
697 desc
= kmalloc(USB_DT_CONFIG_SIZE
, GFP_KERNEL
);
702 for (; cfgno
< ncfg
; cfgno
++) {
703 /* We grab just the first descriptor so we know how long
704 * the whole configuration is */
705 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
706 desc
, USB_DT_CONFIG_SIZE
);
708 dev_err(ddev
, "unable to read config index %d "
709 "descriptor/%s: %d\n", cfgno
, "start", result
);
710 if (result
!= -EPIPE
)
712 dev_err(ddev
, "chopping to %d config(s)\n", cfgno
);
713 dev
->descriptor
.bNumConfigurations
= cfgno
;
715 } else if (result
< 4) {
716 dev_err(ddev
, "config index %d descriptor too short "
717 "(expected %i, got %i)\n", cfgno
,
718 USB_DT_CONFIG_SIZE
, result
);
722 length
= max((int) le16_to_cpu(desc
->wTotalLength
),
725 /* Now that we know the length, get the whole thing */
726 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
732 if (dev
->quirks
& USB_QUIRK_DELAY_INIT
)
735 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
738 dev_err(ddev
, "unable to read config index %d "
739 "descriptor/%s\n", cfgno
, "all");
743 if (result
< length
) {
744 dev_warn(ddev
, "config index %d descriptor too short "
745 "(expected %i, got %i)\n", cfgno
, length
, result
);
749 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
751 result
= usb_parse_configuration(dev
, cfgno
,
752 &dev
->config
[cfgno
], bigbuffer
, length
);
762 dev
->descriptor
.bNumConfigurations
= cfgno
;
764 if (result
== -ENOMEM
)
765 dev_err(ddev
, "out of memory\n");
769 void usb_release_bos_descriptor(struct usb_device
*dev
)
772 kfree(dev
->bos
->desc
);
778 /* Get BOS descriptor set */
779 int usb_get_bos_descriptor(struct usb_device
*dev
)
781 struct device
*ddev
= &dev
->dev
;
782 struct usb_bos_descriptor
*bos
;
783 struct usb_dev_cap_header
*cap
;
784 unsigned char *buffer
;
785 int length
, total_len
, num
, i
;
788 bos
= kzalloc(sizeof(struct usb_bos_descriptor
), GFP_KERNEL
);
792 /* Get BOS descriptor */
793 ret
= usb_get_descriptor(dev
, USB_DT_BOS
, 0, bos
, USB_DT_BOS_SIZE
);
794 if (ret
< USB_DT_BOS_SIZE
) {
795 dev_err(ddev
, "unable to get BOS descriptor\n");
802 length
= bos
->bLength
;
803 total_len
= le16_to_cpu(bos
->wTotalLength
);
804 num
= bos
->bNumDeviceCaps
;
806 if (total_len
< length
)
809 dev
->bos
= kzalloc(sizeof(struct usb_host_bos
), GFP_KERNEL
);
813 /* Now let's get the whole BOS descriptor set */
814 buffer
= kzalloc(total_len
, GFP_KERNEL
);
819 dev
->bos
->desc
= (struct usb_bos_descriptor
*)buffer
;
821 ret
= usb_get_descriptor(dev
, USB_DT_BOS
, 0, buffer
, total_len
);
822 if (ret
< total_len
) {
823 dev_err(ddev
, "unable to get BOS descriptor set\n");
830 for (i
= 0; i
< num
; i
++) {
832 cap
= (struct usb_dev_cap_header
*)buffer
;
833 length
= cap
->bLength
;
835 if (total_len
< length
)
839 if (cap
->bDescriptorType
!= USB_DT_DEVICE_CAPABILITY
) {
840 dev_warn(ddev
, "descriptor type invalid, skip\n");
844 switch (cap
->bDevCapabilityType
) {
845 case USB_CAP_TYPE_WIRELESS_USB
:
846 /* Wireless USB cap descriptor is handled by wusb */
848 case USB_CAP_TYPE_EXT
:
850 (struct usb_ext_cap_descriptor
*)buffer
;
852 case USB_SS_CAP_TYPE
:
854 (struct usb_ss_cap_descriptor
*)buffer
;
856 case USB_SSP_CAP_TYPE
:
858 (struct usb_ssp_cap_descriptor
*)buffer
;
860 case CONTAINER_ID_TYPE
:
862 (struct usb_ss_container_id_descriptor
*)buffer
;
872 usb_release_bos_descriptor(dev
);