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: "
119 USB_SS_MULT(desc->bmAttributes),
120 cfgno, inum, asnum, ep->desc.bEndpointAddress);
121 ep->ss_ep_comp.bmAttributes = 2;
124 if (usb_endpoint_xfer_isoc(&ep->desc))
125 max_tx = (desc->bMaxBurst + 1) *
126 (USB_SS_MULT(desc->bmAttributes)) *
127 usb_endpoint_maxp(&ep->desc);
128 else if (usb_endpoint_xfer_int(&ep->desc))
129 max_tx = usb_endpoint_maxp(&ep->desc) *
130 (desc->bMaxBurst + 1);
133 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
134 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
135 "config %d interface %d altsetting %d ep %d: "
137 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
138 le16_to_cpu(desc->wBytesPerInterval),
139 cfgno, inum, asnum, ep->desc.bEndpointAddress,
141 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
145 static const unsigned short low_speed_maxpacket_maxes[4] = {
146 [USB_ENDPOINT_XFER_CONTROL] = 8,
147 [USB_ENDPOINT_XFER_ISOC] = 0,
148 [USB_ENDPOINT_XFER_BULK] = 0,
149 [USB_ENDPOINT_XFER_INT] = 8,
151 static const unsigned short full_speed_maxpacket_maxes[4] = {
152 [USB_ENDPOINT_XFER_CONTROL] = 64,
153 [USB_ENDPOINT_XFER_ISOC] = 1023,
154 [USB_ENDPOINT_XFER_BULK] = 64,
155 [USB_ENDPOINT_XFER_INT] = 64,
157 static const unsigned short high_speed_maxpacket_maxes[4] = {
158 [USB_ENDPOINT_XFER_CONTROL] = 64,
159 [USB_ENDPOINT_XFER_ISOC] = 1024,
161 /* Bulk should be 512, but some devices use 1024: we will warn below */
162 [USB_ENDPOINT_XFER_BULK] = 1024,
163 [USB_ENDPOINT_XFER_INT] = 1024,
165 static const unsigned short super_speed_maxpacket_maxes[4] = {
166 [USB_ENDPOINT_XFER_CONTROL] = 512,
167 [USB_ENDPOINT_XFER_ISOC] = 1024,
168 [USB_ENDPOINT_XFER_BULK] = 1024,
169 [USB_ENDPOINT_XFER_INT] = 1024,
172 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
173 int asnum, struct usb_host_interface *ifp, int num_ep,
174 unsigned char *buffer, int size)
176 unsigned char *buffer0 = buffer;
177 struct usb_endpoint_descriptor *d;
178 struct usb_host_endpoint *endpoint;
181 const unsigned short *maxpacket_maxes;
183 d = (struct usb_endpoint_descriptor *) buffer;
184 buffer += d->bLength;
187 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
188 n = USB_DT_ENDPOINT_AUDIO_SIZE;
189 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
190 n = USB_DT_ENDPOINT_SIZE;
192 dev_warn(ddev, "config %d interface %d altsetting %d has an "
193 "invalid endpoint descriptor of length %d, skipping\n",
194 cfgno, inum, asnum, d->bLength);
195 goto skip_to_next_endpoint_or_interface_descriptor;
198 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
199 if (i >= 16 || i == 0) {
200 dev_warn(ddev, "config %d interface %d altsetting %d has an "
201 "invalid endpoint with address 0x%X, skipping\n",
202 cfgno, inum, asnum, d->bEndpointAddress);
203 goto skip_to_next_endpoint_or_interface_descriptor;
206 /* Only store as many endpoints as we have room for */
207 if (ifp->desc.bNumEndpoints >= num_ep)
208 goto skip_to_next_endpoint_or_interface_descriptor;
210 /* Check for duplicate endpoint addresses */
211 for (i = 0; i < ifp->desc.bNumEndpoints; ++i) {
212 if (ifp->endpoint[i].desc.bEndpointAddress ==
213 d->bEndpointAddress) {
214 dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
215 cfgno, inum, asnum, d->bEndpointAddress);
216 goto skip_to_next_endpoint_or_interface_descriptor;
220 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
221 ++ifp->desc.bNumEndpoints;
223 memcpy(&endpoint->desc, d, n);
224 INIT_LIST_HEAD(&endpoint->urb_list);
227 * Fix up bInterval values outside the legal range.
228 * Use 10 or 8 ms if no proper value can be guessed.
230 i = 0; /* i = min, j = max, n = default */
232 if (usb_endpoint_xfer_int(d)) {
234 switch (to_usb_device(ddev)->speed) {
235 case USB_SPEED_SUPER_PLUS:
236 case USB_SPEED_SUPER:
239 * Many device manufacturers are using full-speed
240 * bInterval values in high-speed interrupt endpoint
241 * descriptors. Try to fix those and fall back to an
242 * 8-ms default value otherwise.
244 n = fls(d->bInterval*8);
246 n = 7; /* 8 ms = 2^(7-1) uframes */
250 * Adjust bInterval for quirked devices.
253 * This quirk fixes bIntervals reported in ms.
255 if (to_usb_device(ddev)->quirks &
256 USB_QUIRK_LINEAR_FRAME_INTR_BINTERVAL) {
257 n = clamp(fls(d->bInterval) + 3, i, j);
261 * This quirk fixes bIntervals reported in
262 * linear microframes.
264 if (to_usb_device(ddev)->quirks &
265 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
266 n = clamp(fls(d->bInterval), i, j);
270 default: /* USB_SPEED_FULL or _LOW */
272 * For low-speed, 10 ms is the official minimum.
273 * But some "overclocked" devices might want faster
274 * polling so we'll allow it.
279 } else if (usb_endpoint_xfer_isoc(d)) {
282 switch (to_usb_device(ddev)->speed) {
284 n = 7; /* 8 ms = 2^(7-1) uframes */
286 default: /* USB_SPEED_FULL */
287 n = 4; /* 8 ms = 2^(4-1) frames */
291 if (d->bInterval < i || d->bInterval > j) {
292 dev_warn(ddev, "config %d interface %d altsetting %d "
293 "endpoint 0x%X has an invalid bInterval %d, "
296 d->bEndpointAddress, d->bInterval, n);
297 endpoint->desc.bInterval = n;
300 /* Some buggy low-speed devices have Bulk endpoints, which is
301 * explicitly forbidden by the USB spec. In an attempt to make
302 * them usable, we will try treating them as Interrupt endpoints.
304 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
305 usb_endpoint_xfer_bulk(d)) {
306 dev_warn(ddev, "config %d interface %d altsetting %d "
307 "endpoint 0x%X is Bulk; changing to Interrupt\n",
308 cfgno, inum, asnum, d->bEndpointAddress);
309 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
310 endpoint->desc.bInterval = 1;
311 if (usb_endpoint_maxp(&endpoint->desc) > 8)
312 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
315 /* Validate the wMaxPacketSize field */
316 maxp = usb_endpoint_maxp(&endpoint->desc);
318 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has wMaxPacketSize 0, skipping\n",
319 cfgno, inum, asnum, d->bEndpointAddress);
320 goto skip_to_next_endpoint_or_interface_descriptor;
323 /* Find the highest legal maxpacket size for this endpoint */
324 i = 0; /* additional transactions per microframe */
325 switch (to_usb_device(ddev)->speed) {
327 maxpacket_maxes = low_speed_maxpacket_maxes;
330 maxpacket_maxes = full_speed_maxpacket_maxes;
333 /* Bits 12..11 are allowed only for HS periodic endpoints */
334 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
335 i = maxp & (BIT(12) | BIT(11));
340 maxpacket_maxes = high_speed_maxpacket_maxes;
342 case USB_SPEED_SUPER:
343 case USB_SPEED_SUPER_PLUS:
344 maxpacket_maxes = super_speed_maxpacket_maxes;
347 j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
350 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
351 cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
353 endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
357 * Some buggy high speed devices have bulk endpoints using
358 * maxpacket sizes other than 512. High speed HCDs may not
359 * be able to handle that particular bug, so let's warn...
361 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
362 && usb_endpoint_xfer_bulk(d)) {
364 dev_warn(ddev, "config %d interface %d altsetting %d "
365 "bulk endpoint 0x%X has invalid maxpacket %d\n",
366 cfgno, inum, asnum, d->bEndpointAddress,
370 /* Parse a possible SuperSpeed endpoint companion descriptor */
371 if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
372 usb_parse_ss_endpoint_companion(ddev, cfgno,
373 inum, asnum, endpoint, buffer, size);
375 /* Skip over any Class Specific or Vendor Specific descriptors;
376 * find the next endpoint or interface descriptor */
377 endpoint->extra = buffer;
378 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
379 USB_DT_INTERFACE, &n);
380 endpoint->extralen = i;
381 retval = buffer - buffer0 + i;
383 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
384 n, plural(n), "endpoint");
387 skip_to_next_endpoint_or_interface_descriptor:
388 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
389 USB_DT_INTERFACE, NULL);
390 return buffer - buffer0 + i;
393 void usb_release_interface_cache(struct kref *ref)
395 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
398 for (j = 0; j < intfc->num_altsetting; j++) {
399 struct usb_host_interface *alt = &intfc->altsetting[j];
401 kfree(alt->endpoint);
407 static int usb_parse_interface(struct device *ddev, int cfgno,
408 struct usb_host_config *config, unsigned char *buffer, int size,
409 u8 inums[], u8 nalts[])
411 unsigned char *buffer0 = buffer;
412 struct usb_interface_descriptor *d;
414 struct usb_interface_cache *intfc;
415 struct usb_host_interface *alt;
418 int num_ep, num_ep_orig;
420 d = (struct usb_interface_descriptor *) buffer;
421 buffer += d->bLength;
424 if (d->bLength < USB_DT_INTERFACE_SIZE)
425 goto skip_to_next_interface_descriptor;
427 /* Which interface entry is this? */
429 inum = d->bInterfaceNumber;
430 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
431 if (inums[i] == inum) {
432 intfc = config->intf_cache[i];
436 if (!intfc || intfc->num_altsetting >= nalts[i])
437 goto skip_to_next_interface_descriptor;
439 /* Check for duplicate altsetting entries */
440 asnum = d->bAlternateSetting;
441 for ((i = 0, alt = &intfc->altsetting[0]);
442 i < intfc->num_altsetting;
444 if (alt->desc.bAlternateSetting == asnum) {
445 dev_warn(ddev, "Duplicate descriptor for config %d "
446 "interface %d altsetting %d, skipping\n",
448 goto skip_to_next_interface_descriptor;
452 ++intfc->num_altsetting;
453 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
455 /* Skip over any Class Specific or Vendor Specific descriptors;
456 * find the first endpoint or interface descriptor */
458 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
459 USB_DT_INTERFACE, &n);
462 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
463 n, plural(n), "interface");
467 /* Allocate space for the right(?) number of endpoints */
468 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
469 alt->desc.bNumEndpoints = 0; /* Use as a counter */
470 if (num_ep > USB_MAXENDPOINTS) {
471 dev_warn(ddev, "too many endpoints for config %d interface %d "
472 "altsetting %d: %d, using maximum allowed: %d\n",
473 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
474 num_ep = USB_MAXENDPOINTS;
478 /* Can't allocate 0 bytes */
479 len = sizeof(struct usb_host_endpoint) * num_ep;
480 alt->endpoint = kzalloc(len, GFP_KERNEL);
485 /* Parse all the endpoint descriptors */
488 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
491 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
492 num_ep, buffer, size);
501 if (n != num_ep_orig)
502 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
503 "endpoint descriptor%s, different from the interface "
504 "descriptor's value: %d\n",
505 cfgno, inum, asnum, n, plural(n), num_ep_orig);
506 return buffer - buffer0;
508 skip_to_next_interface_descriptor:
509 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
510 USB_DT_INTERFACE, NULL);
511 return buffer - buffer0 + i;
514 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
515 struct usb_host_config *config, unsigned char *buffer, int size)
517 struct device *ddev = &dev->dev;
518 unsigned char *buffer0 = buffer;
520 int nintf, nintf_orig;
522 struct usb_interface_cache *intfc;
523 unsigned char *buffer2;
525 struct usb_descriptor_header *header;
527 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
528 unsigned iad_num = 0;
530 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
531 nintf = nintf_orig = config->desc.bNumInterfaces;
532 config->desc.bNumInterfaces = 0; // Adjusted later
534 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
535 config->desc.bLength < USB_DT_CONFIG_SIZE ||
536 config->desc.bLength > size) {
537 dev_err(ddev, "invalid descriptor for config index %d: "
538 "type = 0x%X, length = %d\n", cfgidx,
539 config->desc.bDescriptorType, config->desc.bLength);
542 cfgno = config->desc.bConfigurationValue;
544 buffer += config->desc.bLength;
545 size -= config->desc.bLength;
547 if (nintf > USB_MAXINTERFACES) {
548 dev_warn(ddev, "config %d has too many interfaces: %d, "
549 "using maximum allowed: %d\n",
550 cfgno, nintf, USB_MAXINTERFACES);
551 nintf = USB_MAXINTERFACES;
554 /* Go through the descriptors, checking their length and counting the
555 * number of altsettings for each interface */
557 for ((buffer2 = buffer, size2 = size);
559 (buffer2 += header->bLength, size2 -= header->bLength)) {
561 if (size2 < sizeof(struct usb_descriptor_header)) {
562 dev_warn(ddev, "config %d descriptor has %d excess "
563 "byte%s, ignoring\n",
564 cfgno, size2, plural(size2));
568 header = (struct usb_descriptor_header *) buffer2;
569 if ((header->bLength > size2) || (header->bLength < 2)) {
570 dev_warn(ddev, "config %d has an invalid descriptor "
571 "of length %d, skipping remainder of the config\n",
572 cfgno, header->bLength);
576 if (header->bDescriptorType == USB_DT_INTERFACE) {
577 struct usb_interface_descriptor *d;
580 d = (struct usb_interface_descriptor *) header;
581 if (d->bLength < USB_DT_INTERFACE_SIZE) {
582 dev_warn(ddev, "config %d has an invalid "
583 "interface descriptor of length %d, "
584 "skipping\n", cfgno, d->bLength);
588 inum = d->bInterfaceNumber;
590 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
592 dev_warn(ddev, "config %d has more interface "
593 "descriptors, than it declares in "
594 "bNumInterfaces, ignoring interface "
595 "number: %d\n", cfgno, inum);
599 if (inum >= nintf_orig)
600 dev_warn(ddev, "config %d has an invalid "
601 "interface number: %d but max is %d\n",
602 cfgno, inum, nintf_orig - 1);
604 /* Have we already encountered this interface?
605 * Count its altsettings */
606 for (i = 0; i < n; ++i) {
607 if (inums[i] == inum)
613 } else if (n < USB_MAXINTERFACES) {
619 } else if (header->bDescriptorType ==
620 USB_DT_INTERFACE_ASSOCIATION) {
621 struct usb_interface_assoc_descriptor *d;
623 d = (struct usb_interface_assoc_descriptor *)header;
624 if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) {
626 "config %d has an invalid interface association descriptor of length %d, skipping\n",
631 if (iad_num == USB_MAXIADS) {
632 dev_warn(ddev, "found more Interface "
633 "Association Descriptors "
634 "than allocated for in "
635 "configuration %d\n", cfgno);
637 config->intf_assoc[iad_num] = d;
641 } else if (header->bDescriptorType == USB_DT_DEVICE ||
642 header->bDescriptorType == USB_DT_CONFIG)
643 dev_warn(ddev, "config %d contains an unexpected "
644 "descriptor of type 0x%X, skipping\n",
645 cfgno, header->bDescriptorType);
647 } /* for ((buffer2 = buffer, size2 = size); ...) */
648 size = buffer2 - buffer;
649 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
652 dev_warn(ddev, "config %d has %d interface%s, different from "
653 "the descriptor's value: %d\n",
654 cfgno, n, plural(n), nintf_orig);
656 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
657 config->desc.bNumInterfaces = nintf = n;
659 /* Check for missing interface numbers */
660 for (i = 0; i < nintf; ++i) {
661 for (j = 0; j < nintf; ++j) {
666 dev_warn(ddev, "config %d has no interface number "
670 /* Allocate the usb_interface_caches and altsetting arrays */
671 for (i = 0; i < nintf; ++i) {
673 if (j > USB_MAXALTSETTING) {
674 dev_warn(ddev, "too many alternate settings for "
675 "config %d interface %d: %d, "
676 "using maximum allowed: %d\n",
677 cfgno, inums[i], j, USB_MAXALTSETTING);
678 nalts[i] = j = USB_MAXALTSETTING;
681 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
682 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
685 kref_init(&intfc->ref);
688 /* FIXME: parse the BOS descriptor */
690 /* Skip over any Class Specific or Vendor Specific descriptors;
691 * find the first interface descriptor */
692 config->extra = buffer;
693 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
694 USB_DT_INTERFACE, &n);
695 config->extralen = i;
697 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
698 n, plural(n), "configuration");
702 /* Parse all the interface/altsetting descriptors */
704 retval = usb_parse_interface(ddev, cfgno, config,
705 buffer, size, inums, nalts);
713 /* Check for missing altsettings */
714 for (i = 0; i < nintf; ++i) {
715 intfc = config->intf_cache[i];
716 for (j = 0; j < intfc->num_altsetting; ++j) {
717 for (n = 0; n < intfc->num_altsetting; ++n) {
718 if (intfc->altsetting[n].desc.
719 bAlternateSetting == j)
722 if (n >= intfc->num_altsetting)
723 dev_warn(ddev, "config %d interface %d has no "
724 "altsetting %d\n", cfgno, inums[i], j);
731 /* hub-only!! ... and only exported for reset/reinit path.
732 * otherwise used internally on disconnect/destroy path
734 void usb_destroy_configuration(struct usb_device *dev)
741 if (dev->rawdescriptors) {
742 for (i = 0; i < dev->descriptor.bNumConfigurations &&
743 i < USB_MAXCONFIG; i++)
744 kfree(dev->rawdescriptors[i]);
746 kfree(dev->rawdescriptors);
747 dev->rawdescriptors = NULL;
750 for (c = 0; c < dev->descriptor.bNumConfigurations &&
751 c < USB_MAXCONFIG; c++) {
752 struct usb_host_config *cf = &dev->config[c];
755 for (i = 0; i < cf->desc.bNumInterfaces &&
756 i < USB_MAXINTERFACES; i++) {
757 if (cf->intf_cache[i])
758 kref_put(&cf->intf_cache[i]->ref,
759 usb_release_interface_cache);
768 * Get the USB config descriptors, cache and parse'em
770 * hub-only!! ... and only in reset path, or usb_new_device()
771 * (used by real hubs and virtual root hubs)
773 int usb_get_configuration(struct usb_device *dev)
775 struct device *ddev = &dev->dev;
776 int ncfg = dev->descriptor.bNumConfigurations;
778 unsigned int cfgno, length;
779 unsigned char *bigbuffer;
780 struct usb_config_descriptor *desc;
784 if (ncfg > USB_MAXCONFIG) {
785 dev_warn(ddev, "too many configurations: %d, "
786 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
787 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
791 dev_err(ddev, "no configurations\n");
795 length = ncfg * sizeof(struct usb_host_config);
796 dev->config = kzalloc(length, GFP_KERNEL);
800 length = ncfg * sizeof(char *);
801 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
802 if (!dev->rawdescriptors)
805 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
810 for (; cfgno < ncfg; cfgno++) {
811 /* We grab just the first descriptor so we know how long
812 * the whole configuration is */
813 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
814 desc, USB_DT_CONFIG_SIZE);
816 dev_err(ddev, "unable to read config index %d "
817 "descriptor/%s: %d\n", cfgno, "start", result);
818 if (result != -EPIPE)
820 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
821 dev->descriptor.bNumConfigurations = cfgno;
823 } else if (result < 4) {
824 dev_err(ddev, "config index %d descriptor too short "
825 "(expected %i, got %i)\n", cfgno,
826 USB_DT_CONFIG_SIZE, result);
830 length = max((int) le16_to_cpu(desc->wTotalLength),
833 /* Now that we know the length, get the whole thing */
834 bigbuffer = kmalloc(length, GFP_KERNEL);
840 if (dev->quirks & USB_QUIRK_DELAY_INIT)
843 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
846 dev_err(ddev, "unable to read config index %d "
847 "descriptor/%s\n", cfgno, "all");
851 if (result < length) {
852 dev_warn(ddev, "config index %d descriptor too short "
853 "(expected %i, got %i)\n", cfgno, length, result);
857 dev->rawdescriptors[cfgno] = bigbuffer;
859 result = usb_parse_configuration(dev, cfgno,
860 &dev->config[cfgno], bigbuffer, length);
870 dev->descriptor.bNumConfigurations = cfgno;
872 if (result == -ENOMEM)
873 dev_err(ddev, "out of memory\n");
877 void usb_release_bos_descriptor(struct usb_device *dev)
880 kfree(dev->bos->desc);
886 static const __u8 bos_desc_len[256] = {
887 [USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE,
888 [USB_CAP_TYPE_EXT] = USB_DT_USB_EXT_CAP_SIZE,
889 [USB_SS_CAP_TYPE] = USB_DT_USB_SS_CAP_SIZE,
890 [USB_SSP_CAP_TYPE] = USB_DT_USB_SSP_CAP_SIZE(1),
891 [CONTAINER_ID_TYPE] = USB_DT_USB_SS_CONTN_ID_SIZE,
892 [USB_PTM_CAP_TYPE] = USB_DT_USB_PTM_ID_SIZE,
895 /* Get BOS descriptor set */
896 int usb_get_bos_descriptor(struct usb_device *dev)
898 struct device *ddev = &dev->dev;
899 struct usb_bos_descriptor *bos;
900 struct usb_dev_cap_header *cap;
901 struct usb_ssp_cap_descriptor *ssp_cap;
902 unsigned char *buffer, *buffer0;
903 int length, total_len, num, i, ssac;
907 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
911 /* Get BOS descriptor */
912 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
913 if (ret < USB_DT_BOS_SIZE || bos->bLength < USB_DT_BOS_SIZE) {
914 dev_err(ddev, "unable to get BOS descriptor or descriptor too short\n");
921 length = bos->bLength;
922 total_len = le16_to_cpu(bos->wTotalLength);
923 num = bos->bNumDeviceCaps;
925 if (total_len < length)
928 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
932 /* Now let's get the whole BOS descriptor set */
933 buffer = kzalloc(total_len, GFP_KERNEL);
938 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
940 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
941 if (ret < total_len) {
942 dev_err(ddev, "unable to get BOS descriptor set\n");
952 for (i = 0; i < num; i++) {
953 cap = (struct usb_dev_cap_header *)buffer;
955 if (total_len < sizeof(*cap) || total_len < cap->bLength) {
956 dev->bos->desc->bNumDeviceCaps = i;
959 cap_type = cap->bDevCapabilityType;
960 length = cap->bLength;
961 if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) {
962 dev->bos->desc->bNumDeviceCaps = i;
966 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
967 dev_warn(ddev, "descriptor type invalid, skip\n");
972 case USB_CAP_TYPE_WIRELESS_USB:
973 /* Wireless USB cap descriptor is handled by wusb */
975 case USB_CAP_TYPE_EXT:
977 (struct usb_ext_cap_descriptor *)buffer;
979 case USB_SS_CAP_TYPE:
981 (struct usb_ss_cap_descriptor *)buffer;
983 case USB_SSP_CAP_TYPE:
984 ssp_cap = (struct usb_ssp_cap_descriptor *)buffer;
985 ssac = (le32_to_cpu(ssp_cap->bmAttributes) &
986 USB_SSP_SUBLINK_SPEED_ATTRIBS);
987 if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac))
988 dev->bos->ssp_cap = ssp_cap;
990 case CONTAINER_ID_TYPE:
992 (struct usb_ss_container_id_descriptor *)buffer;
994 case USB_PTM_CAP_TYPE:
996 (struct usb_ptm_cap_descriptor *)buffer;
998 case USB_CAP_TYPE_CONFIG_SUMMARY:
999 /* one such desc per configuration */
1000 if (!dev->bos->num_config_summary_desc)
1001 dev->bos->config_summary =
1002 (struct usb_config_summary_descriptor *)buffer;
1004 dev->bos->num_config_summary_desc++;
1010 total_len -= length;
1013 dev->bos->desc->wTotalLength = cpu_to_le16(buffer - buffer0);
1018 usb_release_bos_descriptor(dev);