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,
160 [USB_ENDPOINT_XFER_BULK] = 512,
161 [USB_ENDPOINT_XFER_INT] = 1024,
163 static const unsigned short super_speed_maxpacket_maxes[4] = {
164 [USB_ENDPOINT_XFER_CONTROL] = 512,
165 [USB_ENDPOINT_XFER_ISOC] = 1024,
166 [USB_ENDPOINT_XFER_BULK] = 1024,
167 [USB_ENDPOINT_XFER_INT] = 1024,
170 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
171 int asnum, struct usb_host_interface *ifp, int num_ep,
172 unsigned char *buffer, int size)
174 unsigned char *buffer0 = buffer;
175 struct usb_endpoint_descriptor *d;
176 struct usb_host_endpoint *endpoint;
179 const unsigned short *maxpacket_maxes;
181 d = (struct usb_endpoint_descriptor *) buffer;
182 buffer += d->bLength;
185 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
186 n = USB_DT_ENDPOINT_AUDIO_SIZE;
187 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
188 n = USB_DT_ENDPOINT_SIZE;
190 dev_warn(ddev, "config %d interface %d altsetting %d has an "
191 "invalid endpoint descriptor of length %d, skipping\n",
192 cfgno, inum, asnum, d->bLength);
193 goto skip_to_next_endpoint_or_interface_descriptor;
196 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
197 if (i >= 16 || i == 0) {
198 dev_warn(ddev, "config %d interface %d altsetting %d has an "
199 "invalid endpoint with address 0x%X, skipping\n",
200 cfgno, inum, asnum, d->bEndpointAddress);
201 goto skip_to_next_endpoint_or_interface_descriptor;
204 /* Only store as many endpoints as we have room for */
205 if (ifp->desc.bNumEndpoints >= num_ep)
206 goto skip_to_next_endpoint_or_interface_descriptor;
208 /* Check for duplicate endpoint addresses */
209 for (i = 0; i < ifp->desc.bNumEndpoints; ++i) {
210 if (ifp->endpoint[i].desc.bEndpointAddress ==
211 d->bEndpointAddress) {
212 dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
213 cfgno, inum, asnum, d->bEndpointAddress);
214 goto skip_to_next_endpoint_or_interface_descriptor;
218 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
219 ++ifp->desc.bNumEndpoints;
221 memcpy(&endpoint->desc, d, n);
222 INIT_LIST_HEAD(&endpoint->urb_list);
225 * Fix up bInterval values outside the legal range.
226 * Use 10 or 8 ms if no proper value can be guessed.
228 i = 0; /* i = min, j = max, n = default */
230 if (usb_endpoint_xfer_int(d)) {
232 switch (to_usb_device(ddev)->speed) {
233 case USB_SPEED_SUPER_PLUS:
234 case USB_SPEED_SUPER:
237 * Many device manufacturers are using full-speed
238 * bInterval values in high-speed interrupt endpoint
239 * descriptors. Try to fix those and fall back to an
240 * 8-ms default value otherwise.
242 n = fls(d->bInterval*8);
244 n = 7; /* 8 ms = 2^(7-1) uframes */
248 * Adjust bInterval for quirked devices.
249 * This quirk fixes bIntervals reported in
250 * linear microframes.
252 if (to_usb_device(ddev)->quirks &
253 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
254 n = clamp(fls(d->bInterval), i, j);
258 default: /* USB_SPEED_FULL or _LOW */
260 * For low-speed, 10 ms is the official minimum.
261 * But some "overclocked" devices might want faster
262 * polling so we'll allow it.
267 } else if (usb_endpoint_xfer_isoc(d)) {
270 switch (to_usb_device(ddev)->speed) {
272 n = 7; /* 8 ms = 2^(7-1) uframes */
274 default: /* USB_SPEED_FULL */
275 n = 4; /* 8 ms = 2^(4-1) frames */
279 if (d->bInterval < i || d->bInterval > j) {
280 dev_warn(ddev, "config %d interface %d altsetting %d "
281 "endpoint 0x%X has an invalid bInterval %d, "
284 d->bEndpointAddress, d->bInterval, n);
285 endpoint->desc.bInterval = n;
288 /* Some buggy low-speed devices have Bulk endpoints, which is
289 * explicitly forbidden by the USB spec. In an attempt to make
290 * them usable, we will try treating them as Interrupt endpoints.
292 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
293 usb_endpoint_xfer_bulk(d)) {
294 dev_warn(ddev, "config %d interface %d altsetting %d "
295 "endpoint 0x%X is Bulk; changing to Interrupt\n",
296 cfgno, inum, asnum, d->bEndpointAddress);
297 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
298 endpoint->desc.bInterval = 1;
299 if (usb_endpoint_maxp(&endpoint->desc) > 8)
300 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
303 /* Validate the wMaxPacketSize field */
304 maxp = usb_endpoint_maxp(&endpoint->desc);
306 /* Find the highest legal maxpacket size for this endpoint */
307 i = 0; /* additional transactions per microframe */
308 switch (to_usb_device(ddev)->speed) {
310 maxpacket_maxes = low_speed_maxpacket_maxes;
313 maxpacket_maxes = full_speed_maxpacket_maxes;
316 /* Bits 12..11 are allowed only for HS periodic endpoints */
317 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
318 i = maxp & (BIT(12) | BIT(11));
323 maxpacket_maxes = high_speed_maxpacket_maxes;
325 case USB_SPEED_SUPER:
326 case USB_SPEED_SUPER_PLUS:
327 maxpacket_maxes = super_speed_maxpacket_maxes;
330 j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
333 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
334 cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
336 endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
340 * Some buggy high speed devices have bulk endpoints using
341 * maxpacket sizes other than 512. High speed HCDs may not
342 * be able to handle that particular bug, so let's warn...
344 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
345 && usb_endpoint_xfer_bulk(d)) {
347 dev_warn(ddev, "config %d interface %d altsetting %d "
348 "bulk endpoint 0x%X has invalid maxpacket %d\n",
349 cfgno, inum, asnum, d->bEndpointAddress,
353 /* Parse a possible SuperSpeed endpoint companion descriptor */
354 if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
355 usb_parse_ss_endpoint_companion(ddev, cfgno,
356 inum, asnum, endpoint, buffer, size);
358 /* Skip over any Class Specific or Vendor Specific descriptors;
359 * find the next endpoint or interface descriptor */
360 endpoint->extra = buffer;
361 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
362 USB_DT_INTERFACE, &n);
363 endpoint->extralen = i;
364 retval = buffer - buffer0 + i;
366 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
367 n, plural(n), "endpoint");
370 skip_to_next_endpoint_or_interface_descriptor:
371 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
372 USB_DT_INTERFACE, NULL);
373 return buffer - buffer0 + i;
376 void usb_release_interface_cache(struct kref *ref)
378 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
381 for (j = 0; j < intfc->num_altsetting; j++) {
382 struct usb_host_interface *alt = &intfc->altsetting[j];
384 kfree(alt->endpoint);
390 static int usb_parse_interface(struct device *ddev, int cfgno,
391 struct usb_host_config *config, unsigned char *buffer, int size,
392 u8 inums[], u8 nalts[])
394 unsigned char *buffer0 = buffer;
395 struct usb_interface_descriptor *d;
397 struct usb_interface_cache *intfc;
398 struct usb_host_interface *alt;
401 int num_ep, num_ep_orig;
403 d = (struct usb_interface_descriptor *) buffer;
404 buffer += d->bLength;
407 if (d->bLength < USB_DT_INTERFACE_SIZE)
408 goto skip_to_next_interface_descriptor;
410 /* Which interface entry is this? */
412 inum = d->bInterfaceNumber;
413 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
414 if (inums[i] == inum) {
415 intfc = config->intf_cache[i];
419 if (!intfc || intfc->num_altsetting >= nalts[i])
420 goto skip_to_next_interface_descriptor;
422 /* Check for duplicate altsetting entries */
423 asnum = d->bAlternateSetting;
424 for ((i = 0, alt = &intfc->altsetting[0]);
425 i < intfc->num_altsetting;
427 if (alt->desc.bAlternateSetting == asnum) {
428 dev_warn(ddev, "Duplicate descriptor for config %d "
429 "interface %d altsetting %d, skipping\n",
431 goto skip_to_next_interface_descriptor;
435 ++intfc->num_altsetting;
436 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
438 /* Skip over any Class Specific or Vendor Specific descriptors;
439 * find the first endpoint or interface descriptor */
441 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
442 USB_DT_INTERFACE, &n);
445 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
446 n, plural(n), "interface");
450 /* Allocate space for the right(?) number of endpoints */
451 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
452 alt->desc.bNumEndpoints = 0; /* Use as a counter */
453 if (num_ep > USB_MAXENDPOINTS) {
454 dev_warn(ddev, "too many endpoints for config %d interface %d "
455 "altsetting %d: %d, using maximum allowed: %d\n",
456 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
457 num_ep = USB_MAXENDPOINTS;
461 /* Can't allocate 0 bytes */
462 len = sizeof(struct usb_host_endpoint) * num_ep;
463 alt->endpoint = kzalloc(len, GFP_KERNEL);
468 /* Parse all the endpoint descriptors */
471 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
474 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
475 num_ep, buffer, size);
484 if (n != num_ep_orig)
485 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
486 "endpoint descriptor%s, different from the interface "
487 "descriptor's value: %d\n",
488 cfgno, inum, asnum, n, plural(n), num_ep_orig);
489 return buffer - buffer0;
491 skip_to_next_interface_descriptor:
492 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
493 USB_DT_INTERFACE, NULL);
494 return buffer - buffer0 + i;
497 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
498 struct usb_host_config *config, unsigned char *buffer, int size)
500 struct device *ddev = &dev->dev;
501 unsigned char *buffer0 = buffer;
503 int nintf, nintf_orig;
505 struct usb_interface_cache *intfc;
506 unsigned char *buffer2;
508 struct usb_descriptor_header *header;
510 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
511 unsigned iad_num = 0;
513 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
514 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
515 config->desc.bLength < USB_DT_CONFIG_SIZE ||
516 config->desc.bLength > size) {
517 dev_err(ddev, "invalid descriptor for config index %d: "
518 "type = 0x%X, length = %d\n", cfgidx,
519 config->desc.bDescriptorType, config->desc.bLength);
522 cfgno = config->desc.bConfigurationValue;
524 buffer += config->desc.bLength;
525 size -= config->desc.bLength;
527 nintf = nintf_orig = config->desc.bNumInterfaces;
528 if (nintf > USB_MAXINTERFACES) {
529 dev_warn(ddev, "config %d has too many interfaces: %d, "
530 "using maximum allowed: %d\n",
531 cfgno, nintf, USB_MAXINTERFACES);
532 nintf = USB_MAXINTERFACES;
535 /* Go through the descriptors, checking their length and counting the
536 * number of altsettings for each interface */
538 for ((buffer2 = buffer, size2 = size);
540 (buffer2 += header->bLength, size2 -= header->bLength)) {
542 if (size2 < sizeof(struct usb_descriptor_header)) {
543 dev_warn(ddev, "config %d descriptor has %d excess "
544 "byte%s, ignoring\n",
545 cfgno, size2, plural(size2));
549 header = (struct usb_descriptor_header *) buffer2;
550 if ((header->bLength > size2) || (header->bLength < 2)) {
551 dev_warn(ddev, "config %d has an invalid descriptor "
552 "of length %d, skipping remainder of the config\n",
553 cfgno, header->bLength);
557 if (header->bDescriptorType == USB_DT_INTERFACE) {
558 struct usb_interface_descriptor *d;
561 d = (struct usb_interface_descriptor *) header;
562 if (d->bLength < USB_DT_INTERFACE_SIZE) {
563 dev_warn(ddev, "config %d has an invalid "
564 "interface descriptor of length %d, "
565 "skipping\n", cfgno, d->bLength);
569 inum = d->bInterfaceNumber;
571 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
573 dev_warn(ddev, "config %d has more interface "
574 "descriptors, than it declares in "
575 "bNumInterfaces, ignoring interface "
576 "number: %d\n", cfgno, inum);
580 if (inum >= nintf_orig)
581 dev_warn(ddev, "config %d has an invalid "
582 "interface number: %d but max is %d\n",
583 cfgno, inum, nintf_orig - 1);
585 /* Have we already encountered this interface?
586 * Count its altsettings */
587 for (i = 0; i < n; ++i) {
588 if (inums[i] == inum)
594 } else if (n < USB_MAXINTERFACES) {
600 } else if (header->bDescriptorType ==
601 USB_DT_INTERFACE_ASSOCIATION) {
602 if (iad_num == USB_MAXIADS) {
603 dev_warn(ddev, "found more Interface "
604 "Association Descriptors "
605 "than allocated for in "
606 "configuration %d\n", cfgno);
608 config->intf_assoc[iad_num] =
609 (struct usb_interface_assoc_descriptor
614 } else if (header->bDescriptorType == USB_DT_DEVICE ||
615 header->bDescriptorType == USB_DT_CONFIG)
616 dev_warn(ddev, "config %d contains an unexpected "
617 "descriptor of type 0x%X, skipping\n",
618 cfgno, header->bDescriptorType);
620 } /* for ((buffer2 = buffer, size2 = size); ...) */
621 size = buffer2 - buffer;
622 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
625 dev_warn(ddev, "config %d has %d interface%s, different from "
626 "the descriptor's value: %d\n",
627 cfgno, n, plural(n), nintf_orig);
629 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
630 config->desc.bNumInterfaces = nintf = n;
632 /* Check for missing interface numbers */
633 for (i = 0; i < nintf; ++i) {
634 for (j = 0; j < nintf; ++j) {
639 dev_warn(ddev, "config %d has no interface number "
643 /* Allocate the usb_interface_caches and altsetting arrays */
644 for (i = 0; i < nintf; ++i) {
646 if (j > USB_MAXALTSETTING) {
647 dev_warn(ddev, "too many alternate settings for "
648 "config %d interface %d: %d, "
649 "using maximum allowed: %d\n",
650 cfgno, inums[i], j, USB_MAXALTSETTING);
651 nalts[i] = j = USB_MAXALTSETTING;
654 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
655 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
658 kref_init(&intfc->ref);
661 /* FIXME: parse the BOS descriptor */
663 /* Skip over any Class Specific or Vendor Specific descriptors;
664 * find the first interface descriptor */
665 config->extra = buffer;
666 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
667 USB_DT_INTERFACE, &n);
668 config->extralen = i;
670 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
671 n, plural(n), "configuration");
675 /* Parse all the interface/altsetting descriptors */
677 retval = usb_parse_interface(ddev, cfgno, config,
678 buffer, size, inums, nalts);
686 /* Check for missing altsettings */
687 for (i = 0; i < nintf; ++i) {
688 intfc = config->intf_cache[i];
689 for (j = 0; j < intfc->num_altsetting; ++j) {
690 for (n = 0; n < intfc->num_altsetting; ++n) {
691 if (intfc->altsetting[n].desc.
692 bAlternateSetting == j)
695 if (n >= intfc->num_altsetting)
696 dev_warn(ddev, "config %d interface %d has no "
697 "altsetting %d\n", cfgno, inums[i], j);
704 /* hub-only!! ... and only exported for reset/reinit path.
705 * otherwise used internally on disconnect/destroy path
707 void usb_destroy_configuration(struct usb_device *dev)
714 if (dev->rawdescriptors) {
715 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
716 kfree(dev->rawdescriptors[i]);
718 kfree(dev->rawdescriptors);
719 dev->rawdescriptors = NULL;
722 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
723 struct usb_host_config *cf = &dev->config[c];
726 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
727 if (cf->intf_cache[i])
728 kref_put(&cf->intf_cache[i]->ref,
729 usb_release_interface_cache);
738 * Get the USB config descriptors, cache and parse'em
740 * hub-only!! ... and only in reset path, or usb_new_device()
741 * (used by real hubs and virtual root hubs)
743 int usb_get_configuration(struct usb_device *dev)
745 struct device *ddev = &dev->dev;
746 int ncfg = dev->descriptor.bNumConfigurations;
748 unsigned int cfgno, length;
749 unsigned char *bigbuffer;
750 struct usb_config_descriptor *desc;
754 if (ncfg > USB_MAXCONFIG) {
755 dev_warn(ddev, "too many configurations: %d, "
756 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
757 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
761 dev_err(ddev, "no configurations\n");
765 length = ncfg * sizeof(struct usb_host_config);
766 dev->config = kzalloc(length, GFP_KERNEL);
770 length = ncfg * sizeof(char *);
771 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
772 if (!dev->rawdescriptors)
775 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
780 for (; cfgno < ncfg; cfgno++) {
781 /* We grab just the first descriptor so we know how long
782 * the whole configuration is */
783 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
784 desc, USB_DT_CONFIG_SIZE);
786 dev_err(ddev, "unable to read config index %d "
787 "descriptor/%s: %d\n", cfgno, "start", result);
788 if (result != -EPIPE)
790 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
791 dev->descriptor.bNumConfigurations = cfgno;
793 } else if (result < 4) {
794 dev_err(ddev, "config index %d descriptor too short "
795 "(expected %i, got %i)\n", cfgno,
796 USB_DT_CONFIG_SIZE, result);
800 length = max((int) le16_to_cpu(desc->wTotalLength),
803 /* Now that we know the length, get the whole thing */
804 bigbuffer = kmalloc(length, GFP_KERNEL);
810 if (dev->quirks & USB_QUIRK_DELAY_INIT)
813 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
816 dev_err(ddev, "unable to read config index %d "
817 "descriptor/%s\n", cfgno, "all");
821 if (result < length) {
822 dev_warn(ddev, "config index %d descriptor too short "
823 "(expected %i, got %i)\n", cfgno, length, result);
827 dev->rawdescriptors[cfgno] = bigbuffer;
829 result = usb_parse_configuration(dev, cfgno,
830 &dev->config[cfgno], bigbuffer, length);
840 dev->descriptor.bNumConfigurations = cfgno;
842 if (result == -ENOMEM)
843 dev_err(ddev, "out of memory\n");
847 void usb_release_bos_descriptor(struct usb_device *dev)
850 kfree(dev->bos->desc);
856 /* Get BOS descriptor set */
857 int usb_get_bos_descriptor(struct usb_device *dev)
859 struct device *ddev = &dev->dev;
860 struct usb_bos_descriptor *bos;
861 struct usb_dev_cap_header *cap;
862 unsigned char *buffer;
863 int length, total_len, num, i;
866 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
870 /* Get BOS descriptor */
871 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
872 if (ret < USB_DT_BOS_SIZE) {
873 dev_err(ddev, "unable to get BOS descriptor\n");
880 length = bos->bLength;
881 total_len = le16_to_cpu(bos->wTotalLength);
882 num = bos->bNumDeviceCaps;
884 if (total_len < length)
887 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
891 /* Now let's get the whole BOS descriptor set */
892 buffer = kzalloc(total_len, GFP_KERNEL);
897 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
899 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
900 if (ret < total_len) {
901 dev_err(ddev, "unable to get BOS descriptor set\n");
908 for (i = 0; i < num; i++) {
910 cap = (struct usb_dev_cap_header *)buffer;
911 length = cap->bLength;
913 if (total_len < length)
917 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
918 dev_warn(ddev, "descriptor type invalid, skip\n");
922 switch (cap->bDevCapabilityType) {
923 case USB_CAP_TYPE_WIRELESS_USB:
924 /* Wireless USB cap descriptor is handled by wusb */
926 case USB_CAP_TYPE_EXT:
928 (struct usb_ext_cap_descriptor *)buffer;
930 case USB_SS_CAP_TYPE:
932 (struct usb_ss_cap_descriptor *)buffer;
934 case USB_SSP_CAP_TYPE:
936 (struct usb_ssp_cap_descriptor *)buffer;
938 case CONTAINER_ID_TYPE:
940 (struct usb_ss_container_id_descriptor *)buffer;
950 usb_release_bos_descriptor(dev);