2 * Released under the GPLv2 only.
3 * SPDX-License-Identifier: GPL-2.0
7 #include <linux/usb/ch9.h>
8 #include <linux/usb/hcd.h>
9 #include <linux/usb/quirks.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <asm/byteorder.h>
17 #define USB_MAXALTSETTING 128 /* Hard limit */
19 #define USB_MAXCONFIG 8 /* Arbitrary limit */
22 static inline const char *plural(int n)
24 return (n == 1 ? "" : "s");
27 static int find_next_descriptor(unsigned char *buffer, int size,
28 int dt1, int dt2, int *num_skipped)
30 struct usb_descriptor_header *h;
32 unsigned char *buffer0 = buffer;
34 /* Find the next descriptor of type dt1 or dt2 */
36 h = (struct usb_descriptor_header *) buffer;
37 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
44 /* Store the number of descriptors skipped and return the
45 * number of bytes skipped */
48 return buffer - buffer0;
51 static void usb_parse_ssp_isoc_endpoint_companion(struct device *ddev,
52 int cfgno, int inum, int asnum, struct usb_host_endpoint *ep,
53 unsigned char *buffer, int size)
55 struct usb_ssp_isoc_ep_comp_descriptor *desc;
58 * The SuperSpeedPlus Isoc endpoint companion descriptor immediately
59 * follows the SuperSpeed Endpoint Companion descriptor
61 desc = (struct usb_ssp_isoc_ep_comp_descriptor *) buffer;
62 if (desc->bDescriptorType != USB_DT_SSP_ISOC_ENDPOINT_COMP ||
63 size < USB_DT_SSP_ISOC_EP_COMP_SIZE) {
64 dev_warn(ddev, "Invalid SuperSpeedPlus isoc endpoint companion"
65 "for config %d interface %d altsetting %d ep %d.\n",
66 cfgno, inum, asnum, ep->desc.bEndpointAddress);
69 memcpy(&ep->ssp_isoc_ep_comp, desc, USB_DT_SSP_ISOC_EP_COMP_SIZE);
72 static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
73 int inum, int asnum, struct usb_host_endpoint *ep,
74 unsigned char *buffer, int size)
76 struct usb_ss_ep_comp_descriptor *desc;
79 /* The SuperSpeed endpoint companion descriptor is supposed to
80 * be the first thing immediately following the endpoint descriptor.
82 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
84 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
85 size < USB_DT_SS_EP_COMP_SIZE) {
86 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
87 " interface %d altsetting %d ep %d: "
88 "using minimum values\n",
89 cfgno, inum, asnum, ep->desc.bEndpointAddress);
91 /* Fill in some default values.
92 * Leave bmAttributes as zero, which will mean no streams for
93 * bulk, and isoc won't support multiple bursts of packets.
94 * With bursts of only one packet, and a Mult of 1, the max
95 * amount of data moved per endpoint service interval is one
98 ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
99 ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
100 if (usb_endpoint_xfer_isoc(&ep->desc) ||
101 usb_endpoint_xfer_int(&ep->desc))
102 ep->ss_ep_comp.wBytesPerInterval =
103 ep->desc.wMaxPacketSize;
106 buffer += desc->bLength;
107 size -= desc->bLength;
108 memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
110 /* Check the various values */
111 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
112 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
113 "config %d interface %d altsetting %d ep %d: "
114 "setting to zero\n", desc->bMaxBurst,
115 cfgno, inum, asnum, ep->desc.bEndpointAddress);
116 ep->ss_ep_comp.bMaxBurst = 0;
117 } else if (desc->bMaxBurst > 15) {
118 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
119 "config %d interface %d altsetting %d ep %d: "
120 "setting to 15\n", desc->bMaxBurst,
121 cfgno, inum, asnum, ep->desc.bEndpointAddress);
122 ep->ss_ep_comp.bMaxBurst = 15;
125 if ((usb_endpoint_xfer_control(&ep->desc) ||
126 usb_endpoint_xfer_int(&ep->desc)) &&
127 desc->bmAttributes != 0) {
128 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
129 "config %d interface %d altsetting %d ep %d: "
131 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
133 cfgno, inum, asnum, ep->desc.bEndpointAddress);
134 ep->ss_ep_comp.bmAttributes = 0;
135 } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
136 desc->bmAttributes > 16) {
137 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
138 "config %d interface %d altsetting %d ep %d: "
140 cfgno, inum, asnum, ep->desc.bEndpointAddress);
141 ep->ss_ep_comp.bmAttributes = 16;
142 } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
143 !USB_SS_SSP_ISOC_COMP(desc->bmAttributes) &&
144 USB_SS_MULT(desc->bmAttributes) > 3) {
145 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
146 "config %d interface %d altsetting %d ep %d: "
148 USB_SS_MULT(desc->bmAttributes),
149 cfgno, inum, asnum, ep->desc.bEndpointAddress);
150 ep->ss_ep_comp.bmAttributes = 2;
153 if (usb_endpoint_xfer_isoc(&ep->desc))
154 max_tx = (desc->bMaxBurst + 1) *
155 (USB_SS_MULT(desc->bmAttributes)) *
156 usb_endpoint_maxp(&ep->desc);
157 else if (usb_endpoint_xfer_int(&ep->desc))
158 max_tx = usb_endpoint_maxp(&ep->desc) *
159 (desc->bMaxBurst + 1);
162 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
163 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
164 "config %d interface %d altsetting %d ep %d: "
166 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
167 le16_to_cpu(desc->wBytesPerInterval),
168 cfgno, inum, asnum, ep->desc.bEndpointAddress,
170 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
172 /* Parse a possible SuperSpeedPlus isoc ep companion descriptor */
173 if (usb_endpoint_xfer_isoc(&ep->desc) &&
174 USB_SS_SSP_ISOC_COMP(desc->bmAttributes))
175 usb_parse_ssp_isoc_endpoint_companion(ddev, cfgno, inum, asnum,
179 static const unsigned short low_speed_maxpacket_maxes[4] = {
180 [USB_ENDPOINT_XFER_CONTROL] = 8,
181 [USB_ENDPOINT_XFER_ISOC] = 0,
182 [USB_ENDPOINT_XFER_BULK] = 0,
183 [USB_ENDPOINT_XFER_INT] = 8,
185 static const unsigned short full_speed_maxpacket_maxes[4] = {
186 [USB_ENDPOINT_XFER_CONTROL] = 64,
187 [USB_ENDPOINT_XFER_ISOC] = 1023,
188 [USB_ENDPOINT_XFER_BULK] = 64,
189 [USB_ENDPOINT_XFER_INT] = 64,
191 static const unsigned short high_speed_maxpacket_maxes[4] = {
192 [USB_ENDPOINT_XFER_CONTROL] = 64,
193 [USB_ENDPOINT_XFER_ISOC] = 1024,
195 /* Bulk should be 512, but some devices use 1024: we will warn below */
196 [USB_ENDPOINT_XFER_BULK] = 1024,
197 [USB_ENDPOINT_XFER_INT] = 1024,
199 static const unsigned short super_speed_maxpacket_maxes[4] = {
200 [USB_ENDPOINT_XFER_CONTROL] = 512,
201 [USB_ENDPOINT_XFER_ISOC] = 1024,
202 [USB_ENDPOINT_XFER_BULK] = 1024,
203 [USB_ENDPOINT_XFER_INT] = 1024,
206 static bool endpoint_is_duplicate(struct usb_endpoint_descriptor *e1,
207 struct usb_endpoint_descriptor *e2)
209 if (e1->bEndpointAddress == e2->bEndpointAddress)
212 if (usb_endpoint_xfer_control(e1) || usb_endpoint_xfer_control(e2)) {
213 if (usb_endpoint_num(e1) == usb_endpoint_num(e2))
221 * Check for duplicate endpoint addresses in other interfaces and in the
222 * altsetting currently being parsed.
224 static bool config_endpoint_is_duplicate(struct usb_host_config *config,
225 int inum, int asnum, struct usb_endpoint_descriptor *d)
227 struct usb_endpoint_descriptor *epd;
228 struct usb_interface_cache *intfc;
229 struct usb_host_interface *alt;
232 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
233 intfc = config->intf_cache[i];
235 for (j = 0; j < intfc->num_altsetting; ++j) {
236 alt = &intfc->altsetting[j];
238 if (alt->desc.bInterfaceNumber == inum &&
239 alt->desc.bAlternateSetting != asnum)
242 for (k = 0; k < alt->desc.bNumEndpoints; ++k) {
243 epd = &alt->endpoint[k].desc;
245 if (endpoint_is_duplicate(epd, d))
254 static int usb_parse_endpoint(struct device *ddev, int cfgno,
255 struct usb_host_config *config, int inum, int asnum,
256 struct usb_host_interface *ifp, int num_ep,
257 unsigned char *buffer, int size)
259 struct usb_device *udev = to_usb_device(ddev);
260 unsigned char *buffer0 = buffer;
261 struct usb_endpoint_descriptor *d;
262 struct usb_host_endpoint *endpoint;
265 const unsigned short *maxpacket_maxes;
267 d = (struct usb_endpoint_descriptor *) buffer;
268 buffer += d->bLength;
271 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
272 n = USB_DT_ENDPOINT_AUDIO_SIZE;
273 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
274 n = USB_DT_ENDPOINT_SIZE;
276 dev_warn(ddev, "config %d interface %d altsetting %d has an "
277 "invalid endpoint descriptor of length %d, skipping\n",
278 cfgno, inum, asnum, d->bLength);
279 goto skip_to_next_endpoint_or_interface_descriptor;
282 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
283 if (i >= 16 || i == 0) {
284 dev_warn(ddev, "config %d interface %d altsetting %d has an "
285 "invalid endpoint with address 0x%X, skipping\n",
286 cfgno, inum, asnum, d->bEndpointAddress);
287 goto skip_to_next_endpoint_or_interface_descriptor;
290 /* Only store as many endpoints as we have room for */
291 if (ifp->desc.bNumEndpoints >= num_ep)
292 goto skip_to_next_endpoint_or_interface_descriptor;
294 /* Check for duplicate endpoint addresses */
295 if (config_endpoint_is_duplicate(config, inum, asnum, d)) {
296 dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
297 cfgno, inum, asnum, d->bEndpointAddress);
298 goto skip_to_next_endpoint_or_interface_descriptor;
301 /* Ignore blacklisted endpoints */
302 if (udev->quirks & USB_QUIRK_ENDPOINT_BLACKLIST) {
303 if (usb_endpoint_is_blacklisted(udev, ifp, d)) {
304 dev_warn(ddev, "config %d interface %d altsetting %d has a blacklisted endpoint with address 0x%X, skipping\n",
306 d->bEndpointAddress);
307 goto skip_to_next_endpoint_or_interface_descriptor;
311 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
312 ++ifp->desc.bNumEndpoints;
314 memcpy(&endpoint->desc, d, n);
315 INIT_LIST_HEAD(&endpoint->urb_list);
318 * Fix up bInterval values outside the legal range.
319 * Use 10 or 8 ms if no proper value can be guessed.
321 i = 0; /* i = min, j = max, n = default */
323 if (usb_endpoint_xfer_int(d)) {
325 switch (to_usb_device(ddev)->speed) {
326 case USB_SPEED_SUPER_PLUS:
327 case USB_SPEED_SUPER:
330 * Many device manufacturers are using full-speed
331 * bInterval values in high-speed interrupt endpoint
332 * descriptors. Try to fix those and fall back to an
333 * 8-ms default value otherwise.
335 n = fls(d->bInterval*8);
337 n = 7; /* 8 ms = 2^(7-1) uframes */
341 * Adjust bInterval for quirked devices.
344 * This quirk fixes bIntervals reported in ms.
346 if (to_usb_device(ddev)->quirks &
347 USB_QUIRK_LINEAR_FRAME_INTR_BINTERVAL) {
348 n = clamp(fls(d->bInterval) + 3, i, j);
352 * This quirk fixes bIntervals reported in
353 * linear microframes.
355 if (to_usb_device(ddev)->quirks &
356 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
357 n = clamp(fls(d->bInterval), i, j);
361 default: /* USB_SPEED_FULL or _LOW */
363 * For low-speed, 10 ms is the official minimum.
364 * But some "overclocked" devices might want faster
365 * polling so we'll allow it.
370 } else if (usb_endpoint_xfer_isoc(d)) {
373 switch (to_usb_device(ddev)->speed) {
375 n = 7; /* 8 ms = 2^(7-1) uframes */
377 default: /* USB_SPEED_FULL */
378 n = 4; /* 8 ms = 2^(4-1) frames */
382 if (d->bInterval < i || d->bInterval > j) {
383 dev_warn(ddev, "config %d interface %d altsetting %d "
384 "endpoint 0x%X has an invalid bInterval %d, "
387 d->bEndpointAddress, d->bInterval, n);
388 endpoint->desc.bInterval = n;
391 /* Some buggy low-speed devices have Bulk endpoints, which is
392 * explicitly forbidden by the USB spec. In an attempt to make
393 * them usable, we will try treating them as Interrupt endpoints.
395 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
396 usb_endpoint_xfer_bulk(d)) {
397 dev_warn(ddev, "config %d interface %d altsetting %d "
398 "endpoint 0x%X is Bulk; changing to Interrupt\n",
399 cfgno, inum, asnum, d->bEndpointAddress);
400 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
401 endpoint->desc.bInterval = 1;
402 if (usb_endpoint_maxp(&endpoint->desc) > 8)
403 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
407 * Validate the wMaxPacketSize field.
408 * Some devices have isochronous endpoints in altsetting 0;
409 * the USB-2 spec requires such endpoints to have wMaxPacketSize = 0
410 * (see the end of section 5.6.3), so don't warn about them.
412 maxp = usb_endpoint_maxp(&endpoint->desc);
413 if (maxp == 0 && !(usb_endpoint_xfer_isoc(d) && asnum == 0)) {
414 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid wMaxPacketSize 0\n",
415 cfgno, inum, asnum, d->bEndpointAddress);
418 /* Find the highest legal maxpacket size for this endpoint */
419 i = 0; /* additional transactions per microframe */
420 switch (to_usb_device(ddev)->speed) {
422 maxpacket_maxes = low_speed_maxpacket_maxes;
425 maxpacket_maxes = full_speed_maxpacket_maxes;
428 /* Bits 12..11 are allowed only for HS periodic endpoints */
429 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
430 i = maxp & (BIT(12) | BIT(11));
435 maxpacket_maxes = high_speed_maxpacket_maxes;
437 case USB_SPEED_SUPER:
438 case USB_SPEED_SUPER_PLUS:
439 maxpacket_maxes = super_speed_maxpacket_maxes;
442 j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
445 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
446 cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
448 endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
452 * Some buggy high speed devices have bulk endpoints using
453 * maxpacket sizes other than 512. High speed HCDs may not
454 * be able to handle that particular bug, so let's warn...
456 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
457 && usb_endpoint_xfer_bulk(d)) {
459 dev_warn(ddev, "config %d interface %d altsetting %d "
460 "bulk endpoint 0x%X has invalid maxpacket %d\n",
461 cfgno, inum, asnum, d->bEndpointAddress,
465 /* Parse a possible SuperSpeed endpoint companion descriptor */
466 if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
467 usb_parse_ss_endpoint_companion(ddev, cfgno,
468 inum, asnum, endpoint, buffer, size);
470 /* Skip over any Class Specific or Vendor Specific descriptors;
471 * find the next endpoint or interface descriptor */
472 endpoint->extra = buffer;
473 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
474 USB_DT_INTERFACE, &n);
475 endpoint->extralen = i;
476 retval = buffer - buffer0 + i;
478 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
479 n, plural(n), "endpoint");
482 skip_to_next_endpoint_or_interface_descriptor:
483 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
484 USB_DT_INTERFACE, NULL);
485 return buffer - buffer0 + i;
488 void usb_release_interface_cache(struct kref *ref)
490 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
493 for (j = 0; j < intfc->num_altsetting; j++) {
494 struct usb_host_interface *alt = &intfc->altsetting[j];
496 kfree(alt->endpoint);
502 static int usb_parse_interface(struct device *ddev, int cfgno,
503 struct usb_host_config *config, unsigned char *buffer, int size,
504 u8 inums[], u8 nalts[])
506 unsigned char *buffer0 = buffer;
507 struct usb_interface_descriptor *d;
509 struct usb_interface_cache *intfc;
510 struct usb_host_interface *alt;
513 int num_ep, num_ep_orig;
515 d = (struct usb_interface_descriptor *) buffer;
516 buffer += d->bLength;
519 if (d->bLength < USB_DT_INTERFACE_SIZE)
520 goto skip_to_next_interface_descriptor;
522 /* Which interface entry is this? */
524 inum = d->bInterfaceNumber;
525 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
526 if (inums[i] == inum) {
527 intfc = config->intf_cache[i];
531 if (!intfc || intfc->num_altsetting >= nalts[i])
532 goto skip_to_next_interface_descriptor;
534 /* Check for duplicate altsetting entries */
535 asnum = d->bAlternateSetting;
536 for ((i = 0, alt = &intfc->altsetting[0]);
537 i < intfc->num_altsetting;
539 if (alt->desc.bAlternateSetting == asnum) {
540 dev_warn(ddev, "Duplicate descriptor for config %d "
541 "interface %d altsetting %d, skipping\n",
543 goto skip_to_next_interface_descriptor;
547 ++intfc->num_altsetting;
548 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
550 /* Skip over any Class Specific or Vendor Specific descriptors;
551 * find the first endpoint or interface descriptor */
553 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
554 USB_DT_INTERFACE, &n);
557 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
558 n, plural(n), "interface");
562 /* Allocate space for the right(?) number of endpoints */
563 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
564 alt->desc.bNumEndpoints = 0; /* Use as a counter */
565 if (num_ep > USB_MAXENDPOINTS) {
566 dev_warn(ddev, "too many endpoints for config %d interface %d "
567 "altsetting %d: %d, using maximum allowed: %d\n",
568 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
569 num_ep = USB_MAXENDPOINTS;
573 /* Can't allocate 0 bytes */
574 len = sizeof(struct usb_host_endpoint) * num_ep;
575 alt->endpoint = kzalloc(len, GFP_KERNEL);
580 /* Parse all the endpoint descriptors */
583 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
586 retval = usb_parse_endpoint(ddev, cfgno, config, inum, asnum,
587 alt, num_ep, buffer, size);
596 if (n != num_ep_orig)
597 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
598 "endpoint descriptor%s, different from the interface "
599 "descriptor's value: %d\n",
600 cfgno, inum, asnum, n, plural(n), num_ep_orig);
601 return buffer - buffer0;
603 skip_to_next_interface_descriptor:
604 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
605 USB_DT_INTERFACE, NULL);
606 return buffer - buffer0 + i;
609 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
610 struct usb_host_config *config, unsigned char *buffer, int size)
612 struct device *ddev = &dev->dev;
613 unsigned char *buffer0 = buffer;
615 int nintf, nintf_orig;
617 struct usb_interface_cache *intfc;
618 unsigned char *buffer2;
620 struct usb_descriptor_header *header;
622 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
623 unsigned iad_num = 0;
625 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
626 nintf = nintf_orig = config->desc.bNumInterfaces;
627 config->desc.bNumInterfaces = 0; // Adjusted later
629 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
630 config->desc.bLength < USB_DT_CONFIG_SIZE ||
631 config->desc.bLength > size) {
632 dev_err(ddev, "invalid descriptor for config index %d: "
633 "type = 0x%X, length = %d\n", cfgidx,
634 config->desc.bDescriptorType, config->desc.bLength);
637 cfgno = config->desc.bConfigurationValue;
639 buffer += config->desc.bLength;
640 size -= config->desc.bLength;
642 if (nintf > USB_MAXINTERFACES) {
643 dev_warn(ddev, "config %d has too many interfaces: %d, "
644 "using maximum allowed: %d\n",
645 cfgno, nintf, USB_MAXINTERFACES);
646 nintf = USB_MAXINTERFACES;
649 /* Go through the descriptors, checking their length and counting the
650 * number of altsettings for each interface */
652 for ((buffer2 = buffer, size2 = size);
654 (buffer2 += header->bLength, size2 -= header->bLength)) {
656 if (size2 < sizeof(struct usb_descriptor_header)) {
657 dev_warn(ddev, "config %d descriptor has %d excess "
658 "byte%s, ignoring\n",
659 cfgno, size2, plural(size2));
663 header = (struct usb_descriptor_header *) buffer2;
664 if ((header->bLength > size2) || (header->bLength < 2)) {
665 dev_warn(ddev, "config %d has an invalid descriptor "
666 "of length %d, skipping remainder of the config\n",
667 cfgno, header->bLength);
671 if (header->bDescriptorType == USB_DT_INTERFACE) {
672 struct usb_interface_descriptor *d;
675 d = (struct usb_interface_descriptor *) header;
676 if (d->bLength < USB_DT_INTERFACE_SIZE) {
677 dev_warn(ddev, "config %d has an invalid "
678 "interface descriptor of length %d, "
679 "skipping\n", cfgno, d->bLength);
683 inum = d->bInterfaceNumber;
685 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
687 dev_warn(ddev, "config %d has more interface "
688 "descriptors, than it declares in "
689 "bNumInterfaces, ignoring interface "
690 "number: %d\n", cfgno, inum);
694 if (inum >= nintf_orig)
695 dev_warn(ddev, "config %d has an invalid "
696 "interface number: %d but max is %d\n",
697 cfgno, inum, nintf_orig - 1);
699 /* Have we already encountered this interface?
700 * Count its altsettings */
701 for (i = 0; i < n; ++i) {
702 if (inums[i] == inum)
708 } else if (n < USB_MAXINTERFACES) {
714 } else if (header->bDescriptorType ==
715 USB_DT_INTERFACE_ASSOCIATION) {
716 struct usb_interface_assoc_descriptor *d;
718 d = (struct usb_interface_assoc_descriptor *)header;
719 if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) {
721 "config %d has an invalid interface association descriptor of length %d, skipping\n",
726 if (iad_num == USB_MAXIADS) {
727 dev_warn(ddev, "found more Interface "
728 "Association Descriptors "
729 "than allocated for in "
730 "configuration %d\n", cfgno);
732 config->intf_assoc[iad_num] = d;
736 } else if (header->bDescriptorType == USB_DT_DEVICE ||
737 header->bDescriptorType == USB_DT_CONFIG)
738 dev_warn(ddev, "config %d contains an unexpected "
739 "descriptor of type 0x%X, skipping\n",
740 cfgno, header->bDescriptorType);
742 } /* for ((buffer2 = buffer, size2 = size); ...) */
743 size = buffer2 - buffer;
744 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
747 dev_warn(ddev, "config %d has %d interface%s, different from "
748 "the descriptor's value: %d\n",
749 cfgno, n, plural(n), nintf_orig);
751 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
752 config->desc.bNumInterfaces = nintf = n;
754 /* Check for missing interface numbers */
755 for (i = 0; i < nintf; ++i) {
756 for (j = 0; j < nintf; ++j) {
761 dev_warn(ddev, "config %d has no interface number "
765 /* Allocate the usb_interface_caches and altsetting arrays */
766 for (i = 0; i < nintf; ++i) {
768 if (j > USB_MAXALTSETTING) {
769 dev_warn(ddev, "too many alternate settings for "
770 "config %d interface %d: %d, "
771 "using maximum allowed: %d\n",
772 cfgno, inums[i], j, USB_MAXALTSETTING);
773 nalts[i] = j = USB_MAXALTSETTING;
776 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
777 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
780 kref_init(&intfc->ref);
783 /* FIXME: parse the BOS descriptor */
785 /* Skip over any Class Specific or Vendor Specific descriptors;
786 * find the first interface descriptor */
787 config->extra = buffer;
788 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
789 USB_DT_INTERFACE, &n);
790 config->extralen = i;
792 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
793 n, plural(n), "configuration");
797 /* Parse all the interface/altsetting descriptors */
799 retval = usb_parse_interface(ddev, cfgno, config,
800 buffer, size, inums, nalts);
808 /* Check for missing altsettings */
809 for (i = 0; i < nintf; ++i) {
810 intfc = config->intf_cache[i];
811 for (j = 0; j < intfc->num_altsetting; ++j) {
812 for (n = 0; n < intfc->num_altsetting; ++n) {
813 if (intfc->altsetting[n].desc.
814 bAlternateSetting == j)
817 if (n >= intfc->num_altsetting)
818 dev_warn(ddev, "config %d interface %d has no "
819 "altsetting %d\n", cfgno, inums[i], j);
826 /* hub-only!! ... and only exported for reset/reinit path.
827 * otherwise used internally on disconnect/destroy path
829 void usb_destroy_configuration(struct usb_device *dev)
836 if (dev->rawdescriptors) {
837 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
838 kfree(dev->rawdescriptors[i]);
840 kfree(dev->rawdescriptors);
841 dev->rawdescriptors = NULL;
844 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
845 struct usb_host_config *cf = &dev->config[c];
848 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
849 if (cf->intf_cache[i])
850 kref_put(&cf->intf_cache[i]->ref,
851 usb_release_interface_cache);
860 * Get the USB config descriptors, cache and parse'em
862 * hub-only!! ... and only in reset path, or usb_new_device()
863 * (used by real hubs and virtual root hubs)
865 int usb_get_configuration(struct usb_device *dev)
867 struct device *ddev = &dev->dev;
868 int ncfg = dev->descriptor.bNumConfigurations;
870 unsigned int cfgno, length;
871 unsigned char *bigbuffer;
872 struct usb_config_descriptor *desc;
876 if (ncfg > USB_MAXCONFIG) {
877 dev_warn(ddev, "too many configurations: %d, "
878 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
879 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
883 dev_err(ddev, "no configurations\n");
887 length = ncfg * sizeof(struct usb_host_config);
888 dev->config = kzalloc(length, GFP_KERNEL);
892 length = ncfg * sizeof(char *);
893 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
894 if (!dev->rawdescriptors)
897 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
902 for (; cfgno < ncfg; cfgno++) {
903 /* We grab just the first descriptor so we know how long
904 * the whole configuration is */
905 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
906 desc, USB_DT_CONFIG_SIZE);
908 dev_err(ddev, "unable to read config index %d "
909 "descriptor/%s: %d\n", cfgno, "start", result);
910 if (result != -EPIPE)
912 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
913 dev->descriptor.bNumConfigurations = cfgno;
915 } else if (result < 4) {
916 dev_err(ddev, "config index %d descriptor too short "
917 "(expected %i, got %i)\n", cfgno,
918 USB_DT_CONFIG_SIZE, result);
922 length = max((int) le16_to_cpu(desc->wTotalLength),
925 /* Now that we know the length, get the whole thing */
926 bigbuffer = kmalloc(length, GFP_KERNEL);
932 if (dev->quirks & USB_QUIRK_DELAY_INIT)
935 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
938 dev_err(ddev, "unable to read config index %d "
939 "descriptor/%s\n", cfgno, "all");
943 if (result < length) {
944 dev_warn(ddev, "config index %d descriptor too short "
945 "(expected %i, got %i)\n", cfgno, length, result);
949 dev->rawdescriptors[cfgno] = bigbuffer;
951 result = usb_parse_configuration(dev, cfgno,
952 &dev->config[cfgno], bigbuffer, length);
962 dev->descriptor.bNumConfigurations = cfgno;
964 if (result == -ENOMEM)
965 dev_err(ddev, "out of memory\n");
969 void usb_release_bos_descriptor(struct usb_device *dev)
972 kfree(dev->bos->desc);
978 static const __u8 bos_desc_len[256] = {
979 [USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE,
980 [USB_CAP_TYPE_EXT] = USB_DT_USB_EXT_CAP_SIZE,
981 [USB_SS_CAP_TYPE] = USB_DT_USB_SS_CAP_SIZE,
982 [USB_SSP_CAP_TYPE] = USB_DT_USB_SSP_CAP_SIZE(1),
983 [CONTAINER_ID_TYPE] = USB_DT_USB_SS_CONTN_ID_SIZE,
984 [USB_PTM_CAP_TYPE] = USB_DT_USB_PTM_ID_SIZE,
987 /* Get BOS descriptor set */
988 int usb_get_bos_descriptor(struct usb_device *dev)
990 struct device *ddev = &dev->dev;
991 struct usb_bos_descriptor *bos;
992 struct usb_dev_cap_header *cap;
993 struct usb_ssp_cap_descriptor *ssp_cap;
994 unsigned char *buffer, *buffer0;
995 int length, total_len, num, i, ssac;
999 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
1003 /* Get BOS descriptor */
1004 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
1005 if (ret < USB_DT_BOS_SIZE || bos->bLength < USB_DT_BOS_SIZE) {
1006 dev_err(ddev, "unable to get BOS descriptor or descriptor too short\n");
1013 length = bos->bLength;
1014 total_len = le16_to_cpu(bos->wTotalLength);
1015 num = bos->bNumDeviceCaps;
1017 if (total_len < length)
1020 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
1024 /* Now let's get the whole BOS descriptor set */
1025 buffer = kzalloc(total_len, GFP_KERNEL);
1030 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
1032 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
1033 if (ret < total_len) {
1034 dev_err(ddev, "unable to get BOS descriptor set\n");
1041 total_len -= length;
1044 for (i = 0; i < num; i++) {
1045 cap = (struct usb_dev_cap_header *)buffer;
1047 if (total_len < sizeof(*cap) || total_len < cap->bLength) {
1048 dev->bos->desc->bNumDeviceCaps = i;
1051 cap_type = cap->bDevCapabilityType;
1052 length = cap->bLength;
1053 if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) {
1054 dev->bos->desc->bNumDeviceCaps = i;
1058 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
1059 dev_warn(ddev, "descriptor type invalid, skip\n");
1064 case USB_CAP_TYPE_WIRELESS_USB:
1065 /* Wireless USB cap descriptor is handled by wusb */
1067 case USB_CAP_TYPE_EXT:
1069 (struct usb_ext_cap_descriptor *)buffer;
1071 case USB_SS_CAP_TYPE:
1073 (struct usb_ss_cap_descriptor *)buffer;
1075 case USB_SSP_CAP_TYPE:
1076 ssp_cap = (struct usb_ssp_cap_descriptor *)buffer;
1077 ssac = (le32_to_cpu(ssp_cap->bmAttributes) &
1078 USB_SSP_SUBLINK_SPEED_ATTRIBS);
1079 if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac))
1080 dev->bos->ssp_cap = ssp_cap;
1082 case CONTAINER_ID_TYPE:
1084 (struct usb_ss_container_id_descriptor *)buffer;
1086 case USB_PTM_CAP_TYPE:
1088 (struct usb_ptm_cap_descriptor *)buffer;
1093 total_len -= length;
1096 dev->bos->desc->wTotalLength = cpu_to_le16(buffer - buffer0);
1101 usb_release_bos_descriptor(dev);