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 bool endpoint_is_duplicate(struct usb_endpoint_descriptor *e1,
173 struct usb_endpoint_descriptor *e2)
175 if (e1->bEndpointAddress == e2->bEndpointAddress)
178 if (usb_endpoint_xfer_control(e1) || usb_endpoint_xfer_control(e2)) {
179 if (usb_endpoint_num(e1) == usb_endpoint_num(e2))
187 * Check for duplicate endpoint addresses in other interfaces and in the
188 * altsetting currently being parsed.
190 static bool config_endpoint_is_duplicate(struct usb_host_config *config,
191 int inum, int asnum, struct usb_endpoint_descriptor *d)
193 struct usb_endpoint_descriptor *epd;
194 struct usb_interface_cache *intfc;
195 struct usb_host_interface *alt;
198 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
199 intfc = config->intf_cache[i];
201 for (j = 0; j < intfc->num_altsetting; ++j) {
202 alt = &intfc->altsetting[j];
204 if (alt->desc.bInterfaceNumber == inum &&
205 alt->desc.bAlternateSetting != asnum)
208 for (k = 0; k < alt->desc.bNumEndpoints; ++k) {
209 epd = &alt->endpoint[k].desc;
211 if (endpoint_is_duplicate(epd, d))
220 static int usb_parse_endpoint(struct device *ddev, int cfgno,
221 struct usb_host_config *config, int inum, int asnum,
222 struct usb_host_interface *ifp, int num_ep,
223 unsigned char *buffer, int size)
225 unsigned char *buffer0 = buffer;
226 struct usb_endpoint_descriptor *d;
227 struct usb_host_endpoint *endpoint;
230 const unsigned short *maxpacket_maxes;
232 d = (struct usb_endpoint_descriptor *) buffer;
233 buffer += d->bLength;
236 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
237 n = USB_DT_ENDPOINT_AUDIO_SIZE;
238 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
239 n = USB_DT_ENDPOINT_SIZE;
241 dev_warn(ddev, "config %d interface %d altsetting %d has an "
242 "invalid endpoint descriptor of length %d, skipping\n",
243 cfgno, inum, asnum, d->bLength);
244 goto skip_to_next_endpoint_or_interface_descriptor;
247 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
248 if (i >= 16 || i == 0) {
249 dev_warn(ddev, "config %d interface %d altsetting %d has an "
250 "invalid endpoint with address 0x%X, skipping\n",
251 cfgno, inum, asnum, d->bEndpointAddress);
252 goto skip_to_next_endpoint_or_interface_descriptor;
255 /* Only store as many endpoints as we have room for */
256 if (ifp->desc.bNumEndpoints >= num_ep)
257 goto skip_to_next_endpoint_or_interface_descriptor;
259 /* Check for duplicate endpoint addresses */
260 if (config_endpoint_is_duplicate(config, inum, asnum, d)) {
261 dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
262 cfgno, inum, asnum, d->bEndpointAddress);
263 goto skip_to_next_endpoint_or_interface_descriptor;
266 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
267 ++ifp->desc.bNumEndpoints;
269 memcpy(&endpoint->desc, d, n);
270 INIT_LIST_HEAD(&endpoint->urb_list);
273 * Fix up bInterval values outside the legal range.
274 * Use 10 or 8 ms if no proper value can be guessed.
276 i = 0; /* i = min, j = max, n = default */
278 if (usb_endpoint_xfer_int(d)) {
280 switch (to_usb_device(ddev)->speed) {
281 case USB_SPEED_SUPER_PLUS:
282 case USB_SPEED_SUPER:
285 * Many device manufacturers are using full-speed
286 * bInterval values in high-speed interrupt endpoint
287 * descriptors. Try to fix those and fall back to an
288 * 8-ms default value otherwise.
290 n = fls(d->bInterval*8);
292 n = 7; /* 8 ms = 2^(7-1) uframes */
296 * Adjust bInterval for quirked devices.
299 * This quirk fixes bIntervals reported in ms.
301 if (to_usb_device(ddev)->quirks &
302 USB_QUIRK_LINEAR_FRAME_INTR_BINTERVAL) {
303 n = clamp(fls(d->bInterval) + 3, i, j);
307 * This quirk fixes bIntervals reported in
308 * linear microframes.
310 if (to_usb_device(ddev)->quirks &
311 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
312 n = clamp(fls(d->bInterval), i, j);
316 default: /* USB_SPEED_FULL or _LOW */
318 * For low-speed, 10 ms is the official minimum.
319 * But some "overclocked" devices might want faster
320 * polling so we'll allow it.
325 } else if (usb_endpoint_xfer_isoc(d)) {
328 switch (to_usb_device(ddev)->speed) {
330 n = 7; /* 8 ms = 2^(7-1) uframes */
332 default: /* USB_SPEED_FULL */
333 n = 4; /* 8 ms = 2^(4-1) frames */
337 if (d->bInterval < i || d->bInterval > j) {
338 dev_warn(ddev, "config %d interface %d altsetting %d "
339 "endpoint 0x%X has an invalid bInterval %d, "
342 d->bEndpointAddress, d->bInterval, n);
343 endpoint->desc.bInterval = n;
346 /* Some buggy low-speed devices have Bulk endpoints, which is
347 * explicitly forbidden by the USB spec. In an attempt to make
348 * them usable, we will try treating them as Interrupt endpoints.
350 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
351 usb_endpoint_xfer_bulk(d)) {
352 dev_warn(ddev, "config %d interface %d altsetting %d "
353 "endpoint 0x%X is Bulk; changing to Interrupt\n",
354 cfgno, inum, asnum, d->bEndpointAddress);
355 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
356 endpoint->desc.bInterval = 1;
357 if (usb_endpoint_maxp(&endpoint->desc) > 8)
358 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
361 /* Validate the wMaxPacketSize field */
362 maxp = usb_endpoint_maxp(&endpoint->desc);
364 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has wMaxPacketSize 0, skipping\n",
365 cfgno, inum, asnum, d->bEndpointAddress);
366 goto skip_to_next_endpoint_or_interface_descriptor;
369 /* Find the highest legal maxpacket size for this endpoint */
370 i = 0; /* additional transactions per microframe */
371 switch (to_usb_device(ddev)->speed) {
373 maxpacket_maxes = low_speed_maxpacket_maxes;
376 maxpacket_maxes = full_speed_maxpacket_maxes;
379 /* Bits 12..11 are allowed only for HS periodic endpoints */
380 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
381 i = maxp & (BIT(12) | BIT(11));
386 maxpacket_maxes = high_speed_maxpacket_maxes;
388 case USB_SPEED_SUPER:
389 case USB_SPEED_SUPER_PLUS:
390 maxpacket_maxes = super_speed_maxpacket_maxes;
393 j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
396 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
397 cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
399 endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
403 * Some buggy high speed devices have bulk endpoints using
404 * maxpacket sizes other than 512. High speed HCDs may not
405 * be able to handle that particular bug, so let's warn...
407 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
408 && usb_endpoint_xfer_bulk(d)) {
410 dev_warn(ddev, "config %d interface %d altsetting %d "
411 "bulk endpoint 0x%X has invalid maxpacket %d\n",
412 cfgno, inum, asnum, d->bEndpointAddress,
416 /* Parse a possible SuperSpeed endpoint companion descriptor */
417 if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
418 usb_parse_ss_endpoint_companion(ddev, cfgno,
419 inum, asnum, endpoint, buffer, size);
421 /* Skip over any Class Specific or Vendor Specific descriptors;
422 * find the next endpoint or interface descriptor */
423 endpoint->extra = buffer;
424 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
425 USB_DT_INTERFACE, &n);
426 endpoint->extralen = i;
427 retval = buffer - buffer0 + i;
429 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
430 n, plural(n), "endpoint");
433 skip_to_next_endpoint_or_interface_descriptor:
434 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
435 USB_DT_INTERFACE, NULL);
436 return buffer - buffer0 + i;
439 void usb_release_interface_cache(struct kref *ref)
441 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
444 for (j = 0; j < intfc->num_altsetting; j++) {
445 struct usb_host_interface *alt = &intfc->altsetting[j];
447 kfree(alt->endpoint);
453 static int usb_parse_interface(struct device *ddev, int cfgno,
454 struct usb_host_config *config, unsigned char *buffer, int size,
455 u8 inums[], u8 nalts[])
457 unsigned char *buffer0 = buffer;
458 struct usb_interface_descriptor *d;
460 struct usb_interface_cache *intfc;
461 struct usb_host_interface *alt;
464 int num_ep, num_ep_orig;
466 d = (struct usb_interface_descriptor *) buffer;
467 buffer += d->bLength;
470 if (d->bLength < USB_DT_INTERFACE_SIZE)
471 goto skip_to_next_interface_descriptor;
473 /* Which interface entry is this? */
475 inum = d->bInterfaceNumber;
476 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
477 if (inums[i] == inum) {
478 intfc = config->intf_cache[i];
482 if (!intfc || intfc->num_altsetting >= nalts[i])
483 goto skip_to_next_interface_descriptor;
485 /* Check for duplicate altsetting entries */
486 asnum = d->bAlternateSetting;
487 for ((i = 0, alt = &intfc->altsetting[0]);
488 i < intfc->num_altsetting;
490 if (alt->desc.bAlternateSetting == asnum) {
491 dev_warn(ddev, "Duplicate descriptor for config %d "
492 "interface %d altsetting %d, skipping\n",
494 goto skip_to_next_interface_descriptor;
498 ++intfc->num_altsetting;
499 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
501 /* Skip over any Class Specific or Vendor Specific descriptors;
502 * find the first endpoint or interface descriptor */
504 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
505 USB_DT_INTERFACE, &n);
508 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
509 n, plural(n), "interface");
513 /* Allocate space for the right(?) number of endpoints */
514 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
515 alt->desc.bNumEndpoints = 0; /* Use as a counter */
516 if (num_ep > USB_MAXENDPOINTS) {
517 dev_warn(ddev, "too many endpoints for config %d interface %d "
518 "altsetting %d: %d, using maximum allowed: %d\n",
519 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
520 num_ep = USB_MAXENDPOINTS;
524 /* Can't allocate 0 bytes */
525 len = sizeof(struct usb_host_endpoint) * num_ep;
526 alt->endpoint = kzalloc(len, GFP_KERNEL);
531 /* Parse all the endpoint descriptors */
534 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
537 retval = usb_parse_endpoint(ddev, cfgno, config, inum, asnum,
538 alt, num_ep, buffer, size);
547 if (n != num_ep_orig)
548 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
549 "endpoint descriptor%s, different from the interface "
550 "descriptor's value: %d\n",
551 cfgno, inum, asnum, n, plural(n), num_ep_orig);
552 return buffer - buffer0;
554 skip_to_next_interface_descriptor:
555 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
556 USB_DT_INTERFACE, NULL);
557 return buffer - buffer0 + i;
560 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
561 struct usb_host_config *config, unsigned char *buffer, int size)
563 struct device *ddev = &dev->dev;
564 unsigned char *buffer0 = buffer;
566 int nintf, nintf_orig;
568 struct usb_interface_cache *intfc;
569 unsigned char *buffer2;
571 struct usb_descriptor_header *header;
573 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
574 unsigned iad_num = 0;
576 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
577 nintf = nintf_orig = config->desc.bNumInterfaces;
578 config->desc.bNumInterfaces = 0; // Adjusted later
580 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
581 config->desc.bLength < USB_DT_CONFIG_SIZE ||
582 config->desc.bLength > size) {
583 dev_err(ddev, "invalid descriptor for config index %d: "
584 "type = 0x%X, length = %d\n", cfgidx,
585 config->desc.bDescriptorType, config->desc.bLength);
588 cfgno = config->desc.bConfigurationValue;
590 buffer += config->desc.bLength;
591 size -= config->desc.bLength;
593 if (nintf > USB_MAXINTERFACES) {
594 dev_warn(ddev, "config %d has too many interfaces: %d, "
595 "using maximum allowed: %d\n",
596 cfgno, nintf, USB_MAXINTERFACES);
597 nintf = USB_MAXINTERFACES;
600 /* Go through the descriptors, checking their length and counting the
601 * number of altsettings for each interface */
603 for ((buffer2 = buffer, size2 = size);
605 (buffer2 += header->bLength, size2 -= header->bLength)) {
607 if (size2 < sizeof(struct usb_descriptor_header)) {
608 dev_warn(ddev, "config %d descriptor has %d excess "
609 "byte%s, ignoring\n",
610 cfgno, size2, plural(size2));
614 header = (struct usb_descriptor_header *) buffer2;
615 if ((header->bLength > size2) || (header->bLength < 2)) {
616 dev_warn(ddev, "config %d has an invalid descriptor "
617 "of length %d, skipping remainder of the config\n",
618 cfgno, header->bLength);
622 if (header->bDescriptorType == USB_DT_INTERFACE) {
623 struct usb_interface_descriptor *d;
626 d = (struct usb_interface_descriptor *) header;
627 if (d->bLength < USB_DT_INTERFACE_SIZE) {
628 dev_warn(ddev, "config %d has an invalid "
629 "interface descriptor of length %d, "
630 "skipping\n", cfgno, d->bLength);
634 inum = d->bInterfaceNumber;
636 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
638 dev_warn(ddev, "config %d has more interface "
639 "descriptors, than it declares in "
640 "bNumInterfaces, ignoring interface "
641 "number: %d\n", cfgno, inum);
645 if (inum >= nintf_orig)
646 dev_warn(ddev, "config %d has an invalid "
647 "interface number: %d but max is %d\n",
648 cfgno, inum, nintf_orig - 1);
650 /* Have we already encountered this interface?
651 * Count its altsettings */
652 for (i = 0; i < n; ++i) {
653 if (inums[i] == inum)
659 } else if (n < USB_MAXINTERFACES) {
665 } else if (header->bDescriptorType ==
666 USB_DT_INTERFACE_ASSOCIATION) {
667 struct usb_interface_assoc_descriptor *d;
669 d = (struct usb_interface_assoc_descriptor *)header;
670 if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) {
672 "config %d has an invalid interface association descriptor of length %d, skipping\n",
677 if (iad_num == USB_MAXIADS) {
678 dev_warn(ddev, "found more Interface "
679 "Association Descriptors "
680 "than allocated for in "
681 "configuration %d\n", cfgno);
683 config->intf_assoc[iad_num] = d;
687 } else if (header->bDescriptorType == USB_DT_DEVICE ||
688 header->bDescriptorType == USB_DT_CONFIG)
689 dev_warn(ddev, "config %d contains an unexpected "
690 "descriptor of type 0x%X, skipping\n",
691 cfgno, header->bDescriptorType);
693 } /* for ((buffer2 = buffer, size2 = size); ...) */
694 size = buffer2 - buffer;
695 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
698 dev_warn(ddev, "config %d has %d interface%s, different from "
699 "the descriptor's value: %d\n",
700 cfgno, n, plural(n), nintf_orig);
702 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
703 config->desc.bNumInterfaces = nintf = n;
705 /* Check for missing interface numbers */
706 for (i = 0; i < nintf; ++i) {
707 for (j = 0; j < nintf; ++j) {
712 dev_warn(ddev, "config %d has no interface number "
716 /* Allocate the usb_interface_caches and altsetting arrays */
717 for (i = 0; i < nintf; ++i) {
719 if (j > USB_MAXALTSETTING) {
720 dev_warn(ddev, "too many alternate settings for "
721 "config %d interface %d: %d, "
722 "using maximum allowed: %d\n",
723 cfgno, inums[i], j, USB_MAXALTSETTING);
724 nalts[i] = j = USB_MAXALTSETTING;
727 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
728 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
731 kref_init(&intfc->ref);
734 /* FIXME: parse the BOS descriptor */
736 /* Skip over any Class Specific or Vendor Specific descriptors;
737 * find the first interface descriptor */
738 config->extra = buffer;
739 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
740 USB_DT_INTERFACE, &n);
741 config->extralen = i;
743 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
744 n, plural(n), "configuration");
748 /* Parse all the interface/altsetting descriptors */
750 retval = usb_parse_interface(ddev, cfgno, config,
751 buffer, size, inums, nalts);
759 /* Check for missing altsettings */
760 for (i = 0; i < nintf; ++i) {
761 intfc = config->intf_cache[i];
762 for (j = 0; j < intfc->num_altsetting; ++j) {
763 for (n = 0; n < intfc->num_altsetting; ++n) {
764 if (intfc->altsetting[n].desc.
765 bAlternateSetting == j)
768 if (n >= intfc->num_altsetting)
769 dev_warn(ddev, "config %d interface %d has no "
770 "altsetting %d\n", cfgno, inums[i], j);
777 /* hub-only!! ... and only exported for reset/reinit path.
778 * otherwise used internally on disconnect/destroy path
780 void usb_destroy_configuration(struct usb_device *dev)
787 if (dev->rawdescriptors) {
788 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
789 kfree(dev->rawdescriptors[i]);
791 kfree(dev->rawdescriptors);
792 dev->rawdescriptors = NULL;
795 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
796 struct usb_host_config *cf = &dev->config[c];
799 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
800 if (cf->intf_cache[i])
801 kref_put(&cf->intf_cache[i]->ref,
802 usb_release_interface_cache);
811 * Get the USB config descriptors, cache and parse'em
813 * hub-only!! ... and only in reset path, or usb_new_device()
814 * (used by real hubs and virtual root hubs)
816 int usb_get_configuration(struct usb_device *dev)
818 struct device *ddev = &dev->dev;
819 int ncfg = dev->descriptor.bNumConfigurations;
821 unsigned int cfgno, length;
822 unsigned char *bigbuffer;
823 struct usb_config_descriptor *desc;
827 if (ncfg > USB_MAXCONFIG) {
828 dev_warn(ddev, "too many configurations: %d, "
829 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
830 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
834 dev_err(ddev, "no configurations\n");
838 length = ncfg * sizeof(struct usb_host_config);
839 dev->config = kzalloc(length, GFP_KERNEL);
843 length = ncfg * sizeof(char *);
844 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
845 if (!dev->rawdescriptors)
848 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
853 for (; cfgno < ncfg; cfgno++) {
854 /* We grab just the first descriptor so we know how long
855 * the whole configuration is */
856 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
857 desc, USB_DT_CONFIG_SIZE);
859 dev_err(ddev, "unable to read config index %d "
860 "descriptor/%s: %d\n", cfgno, "start", result);
861 if (result != -EPIPE)
863 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
864 dev->descriptor.bNumConfigurations = cfgno;
866 } else if (result < 4) {
867 dev_err(ddev, "config index %d descriptor too short "
868 "(expected %i, got %i)\n", cfgno,
869 USB_DT_CONFIG_SIZE, result);
873 length = max((int) le16_to_cpu(desc->wTotalLength),
876 /* Now that we know the length, get the whole thing */
877 bigbuffer = kmalloc(length, GFP_KERNEL);
883 if (dev->quirks & USB_QUIRK_DELAY_INIT)
886 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
889 dev_err(ddev, "unable to read config index %d "
890 "descriptor/%s\n", cfgno, "all");
894 if (result < length) {
895 dev_warn(ddev, "config index %d descriptor too short "
896 "(expected %i, got %i)\n", cfgno, length, result);
900 dev->rawdescriptors[cfgno] = bigbuffer;
902 result = usb_parse_configuration(dev, cfgno,
903 &dev->config[cfgno], bigbuffer, length);
913 dev->descriptor.bNumConfigurations = cfgno;
915 if (result == -ENOMEM)
916 dev_err(ddev, "out of memory\n");
920 void usb_release_bos_descriptor(struct usb_device *dev)
923 kfree(dev->bos->desc);
929 static const __u8 bos_desc_len[256] = {
930 [USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE,
931 [USB_CAP_TYPE_EXT] = USB_DT_USB_EXT_CAP_SIZE,
932 [USB_SS_CAP_TYPE] = USB_DT_USB_SS_CAP_SIZE,
933 [USB_SSP_CAP_TYPE] = USB_DT_USB_SSP_CAP_SIZE(1),
934 [CONTAINER_ID_TYPE] = USB_DT_USB_SS_CONTN_ID_SIZE,
935 [USB_PTM_CAP_TYPE] = USB_DT_USB_PTM_ID_SIZE,
938 /* Get BOS descriptor set */
939 int usb_get_bos_descriptor(struct usb_device *dev)
941 struct device *ddev = &dev->dev;
942 struct usb_bos_descriptor *bos;
943 struct usb_dev_cap_header *cap;
944 struct usb_ssp_cap_descriptor *ssp_cap;
945 unsigned char *buffer, *buffer0;
946 int length, total_len, num, i, ssac;
950 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
954 /* Get BOS descriptor */
955 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
956 if (ret < USB_DT_BOS_SIZE || bos->bLength < USB_DT_BOS_SIZE) {
957 dev_err(ddev, "unable to get BOS descriptor or descriptor too short\n");
964 length = bos->bLength;
965 total_len = le16_to_cpu(bos->wTotalLength);
966 num = bos->bNumDeviceCaps;
968 if (total_len < length)
971 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
975 /* Now let's get the whole BOS descriptor set */
976 buffer = kzalloc(total_len, GFP_KERNEL);
981 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
983 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
984 if (ret < total_len) {
985 dev_err(ddev, "unable to get BOS descriptor set\n");
995 for (i = 0; i < num; i++) {
996 cap = (struct usb_dev_cap_header *)buffer;
998 if (total_len < sizeof(*cap) || total_len < cap->bLength) {
999 dev->bos->desc->bNumDeviceCaps = i;
1002 cap_type = cap->bDevCapabilityType;
1003 length = cap->bLength;
1004 if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) {
1005 dev->bos->desc->bNumDeviceCaps = i;
1009 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
1010 dev_warn(ddev, "descriptor type invalid, skip\n");
1015 case USB_CAP_TYPE_WIRELESS_USB:
1016 /* Wireless USB cap descriptor is handled by wusb */
1018 case USB_CAP_TYPE_EXT:
1020 (struct usb_ext_cap_descriptor *)buffer;
1022 case USB_SS_CAP_TYPE:
1024 (struct usb_ss_cap_descriptor *)buffer;
1026 case USB_SSP_CAP_TYPE:
1027 ssp_cap = (struct usb_ssp_cap_descriptor *)buffer;
1028 ssac = (le32_to_cpu(ssp_cap->bmAttributes) &
1029 USB_SSP_SUBLINK_SPEED_ATTRIBS);
1030 if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac))
1031 dev->bos->ssp_cap = ssp_cap;
1033 case CONTAINER_ID_TYPE:
1035 (struct usb_ss_container_id_descriptor *)buffer;
1037 case USB_PTM_CAP_TYPE:
1039 (struct usb_ptm_cap_descriptor *)buffer;
1044 total_len -= length;
1047 dev->bos->desc->wTotalLength = cpu_to_le16(buffer - buffer0);
1052 usb_release_bos_descriptor(dev);