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] = 1023,
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 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
209 ++ifp->desc.bNumEndpoints;
211 memcpy(&endpoint->desc, d, n);
212 INIT_LIST_HEAD(&endpoint->urb_list);
214 /* Fix up bInterval values outside the legal range. Use 32 ms if no
215 * proper value can be guessed. */
216 i = 0; /* i = min, j = max, n = default */
218 if (usb_endpoint_xfer_int(d)) {
220 switch (to_usb_device(ddev)->speed) {
221 case USB_SPEED_SUPER_PLUS:
222 case USB_SPEED_SUPER:
224 /* Many device manufacturers are using full-speed
225 * bInterval values in high-speed interrupt endpoint
226 * descriptors. Try to fix those and fall back to a
227 * 32 ms default value otherwise. */
228 n = fls(d->bInterval*8);
230 n = 9; /* 32 ms = 2^(9-1) uframes */
234 * Adjust bInterval for quirked devices.
235 * This quirk fixes bIntervals reported in
236 * linear microframes.
238 if (to_usb_device(ddev)->quirks &
239 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
240 n = clamp(fls(d->bInterval), i, j);
244 default: /* USB_SPEED_FULL or _LOW */
245 /* For low-speed, 10 ms is the official minimum.
246 * But some "overclocked" devices might want faster
247 * polling so we'll allow it. */
251 } else if (usb_endpoint_xfer_isoc(d)) {
254 switch (to_usb_device(ddev)->speed) {
256 n = 9; /* 32 ms = 2^(9-1) uframes */
258 default: /* USB_SPEED_FULL */
259 n = 6; /* 32 ms = 2^(6-1) frames */
263 if (d->bInterval < i || d->bInterval > j) {
264 dev_warn(ddev, "config %d interface %d altsetting %d "
265 "endpoint 0x%X has an invalid bInterval %d, "
268 d->bEndpointAddress, d->bInterval, n);
269 endpoint->desc.bInterval = n;
272 /* Some buggy low-speed devices have Bulk endpoints, which is
273 * explicitly forbidden by the USB spec. In an attempt to make
274 * them usable, we will try treating them as Interrupt endpoints.
276 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
277 usb_endpoint_xfer_bulk(d)) {
278 dev_warn(ddev, "config %d interface %d altsetting %d "
279 "endpoint 0x%X is Bulk; changing to Interrupt\n",
280 cfgno, inum, asnum, d->bEndpointAddress);
281 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
282 endpoint->desc.bInterval = 1;
283 if (usb_endpoint_maxp(&endpoint->desc) > 8)
284 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
287 /* Validate the wMaxPacketSize field */
288 maxp = usb_endpoint_maxp(&endpoint->desc);
290 /* Find the highest legal maxpacket size for this endpoint */
291 i = 0; /* additional transactions per microframe */
292 switch (to_usb_device(ddev)->speed) {
294 maxpacket_maxes = low_speed_maxpacket_maxes;
297 maxpacket_maxes = full_speed_maxpacket_maxes;
300 /* Bits 12..11 are allowed only for HS periodic endpoints */
301 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
302 i = maxp & (BIT(12) | BIT(11));
307 maxpacket_maxes = high_speed_maxpacket_maxes;
309 case USB_SPEED_SUPER:
310 case USB_SPEED_SUPER_PLUS:
311 maxpacket_maxes = super_speed_maxpacket_maxes;
314 j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
317 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
318 cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
320 endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
324 * Some buggy high speed devices have bulk endpoints using
325 * maxpacket sizes other than 512. High speed HCDs may not
326 * be able to handle that particular bug, so let's warn...
328 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
329 && usb_endpoint_xfer_bulk(d)) {
331 dev_warn(ddev, "config %d interface %d altsetting %d "
332 "bulk endpoint 0x%X has invalid maxpacket %d\n",
333 cfgno, inum, asnum, d->bEndpointAddress,
337 /* Parse a possible SuperSpeed endpoint companion descriptor */
338 if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
339 usb_parse_ss_endpoint_companion(ddev, cfgno,
340 inum, asnum, endpoint, buffer, size);
342 /* Skip over any Class Specific or Vendor Specific descriptors;
343 * find the next endpoint or interface descriptor */
344 endpoint->extra = buffer;
345 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
346 USB_DT_INTERFACE, &n);
347 endpoint->extralen = i;
348 retval = buffer - buffer0 + i;
350 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
351 n, plural(n), "endpoint");
354 skip_to_next_endpoint_or_interface_descriptor:
355 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
356 USB_DT_INTERFACE, NULL);
357 return buffer - buffer0 + i;
360 void usb_release_interface_cache(struct kref *ref)
362 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
365 for (j = 0; j < intfc->num_altsetting; j++) {
366 struct usb_host_interface *alt = &intfc->altsetting[j];
368 kfree(alt->endpoint);
374 static int usb_parse_interface(struct device *ddev, int cfgno,
375 struct usb_host_config *config, unsigned char *buffer, int size,
376 u8 inums[], u8 nalts[])
378 unsigned char *buffer0 = buffer;
379 struct usb_interface_descriptor *d;
381 struct usb_interface_cache *intfc;
382 struct usb_host_interface *alt;
385 int num_ep, num_ep_orig;
387 d = (struct usb_interface_descriptor *) buffer;
388 buffer += d->bLength;
391 if (d->bLength < USB_DT_INTERFACE_SIZE)
392 goto skip_to_next_interface_descriptor;
394 /* Which interface entry is this? */
396 inum = d->bInterfaceNumber;
397 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
398 if (inums[i] == inum) {
399 intfc = config->intf_cache[i];
403 if (!intfc || intfc->num_altsetting >= nalts[i])
404 goto skip_to_next_interface_descriptor;
406 /* Check for duplicate altsetting entries */
407 asnum = d->bAlternateSetting;
408 for ((i = 0, alt = &intfc->altsetting[0]);
409 i < intfc->num_altsetting;
411 if (alt->desc.bAlternateSetting == asnum) {
412 dev_warn(ddev, "Duplicate descriptor for config %d "
413 "interface %d altsetting %d, skipping\n",
415 goto skip_to_next_interface_descriptor;
419 ++intfc->num_altsetting;
420 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
422 /* Skip over any Class Specific or Vendor Specific descriptors;
423 * find the first endpoint or interface descriptor */
425 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
426 USB_DT_INTERFACE, &n);
429 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
430 n, plural(n), "interface");
434 /* Allocate space for the right(?) number of endpoints */
435 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
436 alt->desc.bNumEndpoints = 0; /* Use as a counter */
437 if (num_ep > USB_MAXENDPOINTS) {
438 dev_warn(ddev, "too many endpoints for config %d interface %d "
439 "altsetting %d: %d, using maximum allowed: %d\n",
440 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
441 num_ep = USB_MAXENDPOINTS;
445 /* Can't allocate 0 bytes */
446 len = sizeof(struct usb_host_endpoint) * num_ep;
447 alt->endpoint = kzalloc(len, GFP_KERNEL);
452 /* Parse all the endpoint descriptors */
455 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
458 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
459 num_ep, buffer, size);
468 if (n != num_ep_orig)
469 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
470 "endpoint descriptor%s, different from the interface "
471 "descriptor's value: %d\n",
472 cfgno, inum, asnum, n, plural(n), num_ep_orig);
473 return buffer - buffer0;
475 skip_to_next_interface_descriptor:
476 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
477 USB_DT_INTERFACE, NULL);
478 return buffer - buffer0 + i;
481 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
482 struct usb_host_config *config, unsigned char *buffer, int size)
484 struct device *ddev = &dev->dev;
485 unsigned char *buffer0 = buffer;
487 int nintf, nintf_orig;
489 struct usb_interface_cache *intfc;
490 unsigned char *buffer2;
492 struct usb_descriptor_header *header;
494 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
495 unsigned iad_num = 0;
497 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
498 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
499 config->desc.bLength < USB_DT_CONFIG_SIZE ||
500 config->desc.bLength > size) {
501 dev_err(ddev, "invalid descriptor for config index %d: "
502 "type = 0x%X, length = %d\n", cfgidx,
503 config->desc.bDescriptorType, config->desc.bLength);
506 cfgno = config->desc.bConfigurationValue;
508 buffer += config->desc.bLength;
509 size -= config->desc.bLength;
511 nintf = nintf_orig = config->desc.bNumInterfaces;
512 if (nintf > USB_MAXINTERFACES) {
513 dev_warn(ddev, "config %d has too many interfaces: %d, "
514 "using maximum allowed: %d\n",
515 cfgno, nintf, USB_MAXINTERFACES);
516 nintf = USB_MAXINTERFACES;
519 /* Go through the descriptors, checking their length and counting the
520 * number of altsettings for each interface */
522 for ((buffer2 = buffer, size2 = size);
524 (buffer2 += header->bLength, size2 -= header->bLength)) {
526 if (size2 < sizeof(struct usb_descriptor_header)) {
527 dev_warn(ddev, "config %d descriptor has %d excess "
528 "byte%s, ignoring\n",
529 cfgno, size2, plural(size2));
533 header = (struct usb_descriptor_header *) buffer2;
534 if ((header->bLength > size2) || (header->bLength < 2)) {
535 dev_warn(ddev, "config %d has an invalid descriptor "
536 "of length %d, skipping remainder of the config\n",
537 cfgno, header->bLength);
541 if (header->bDescriptorType == USB_DT_INTERFACE) {
542 struct usb_interface_descriptor *d;
545 d = (struct usb_interface_descriptor *) header;
546 if (d->bLength < USB_DT_INTERFACE_SIZE) {
547 dev_warn(ddev, "config %d has an invalid "
548 "interface descriptor of length %d, "
549 "skipping\n", cfgno, d->bLength);
553 inum = d->bInterfaceNumber;
555 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
557 dev_warn(ddev, "config %d has more interface "
558 "descriptors, than it declares in "
559 "bNumInterfaces, ignoring interface "
560 "number: %d\n", cfgno, inum);
564 if (inum >= nintf_orig)
565 dev_warn(ddev, "config %d has an invalid "
566 "interface number: %d but max is %d\n",
567 cfgno, inum, nintf_orig - 1);
569 /* Have we already encountered this interface?
570 * Count its altsettings */
571 for (i = 0; i < n; ++i) {
572 if (inums[i] == inum)
578 } else if (n < USB_MAXINTERFACES) {
584 } else if (header->bDescriptorType ==
585 USB_DT_INTERFACE_ASSOCIATION) {
586 if (iad_num == USB_MAXIADS) {
587 dev_warn(ddev, "found more Interface "
588 "Association Descriptors "
589 "than allocated for in "
590 "configuration %d\n", cfgno);
592 config->intf_assoc[iad_num] =
593 (struct usb_interface_assoc_descriptor
598 } else if (header->bDescriptorType == USB_DT_DEVICE ||
599 header->bDescriptorType == USB_DT_CONFIG)
600 dev_warn(ddev, "config %d contains an unexpected "
601 "descriptor of type 0x%X, skipping\n",
602 cfgno, header->bDescriptorType);
604 } /* for ((buffer2 = buffer, size2 = size); ...) */
605 size = buffer2 - buffer;
606 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
609 dev_warn(ddev, "config %d has %d interface%s, different from "
610 "the descriptor's value: %d\n",
611 cfgno, n, plural(n), nintf_orig);
613 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
614 config->desc.bNumInterfaces = nintf = n;
616 /* Check for missing interface numbers */
617 for (i = 0; i < nintf; ++i) {
618 for (j = 0; j < nintf; ++j) {
623 dev_warn(ddev, "config %d has no interface number "
627 /* Allocate the usb_interface_caches and altsetting arrays */
628 for (i = 0; i < nintf; ++i) {
630 if (j > USB_MAXALTSETTING) {
631 dev_warn(ddev, "too many alternate settings for "
632 "config %d interface %d: %d, "
633 "using maximum allowed: %d\n",
634 cfgno, inums[i], j, USB_MAXALTSETTING);
635 nalts[i] = j = USB_MAXALTSETTING;
638 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
639 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
642 kref_init(&intfc->ref);
645 /* FIXME: parse the BOS descriptor */
647 /* Skip over any Class Specific or Vendor Specific descriptors;
648 * find the first interface descriptor */
649 config->extra = buffer;
650 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
651 USB_DT_INTERFACE, &n);
652 config->extralen = i;
654 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
655 n, plural(n), "configuration");
659 /* Parse all the interface/altsetting descriptors */
661 retval = usb_parse_interface(ddev, cfgno, config,
662 buffer, size, inums, nalts);
670 /* Check for missing altsettings */
671 for (i = 0; i < nintf; ++i) {
672 intfc = config->intf_cache[i];
673 for (j = 0; j < intfc->num_altsetting; ++j) {
674 for (n = 0; n < intfc->num_altsetting; ++n) {
675 if (intfc->altsetting[n].desc.
676 bAlternateSetting == j)
679 if (n >= intfc->num_altsetting)
680 dev_warn(ddev, "config %d interface %d has no "
681 "altsetting %d\n", cfgno, inums[i], j);
688 /* hub-only!! ... and only exported for reset/reinit path.
689 * otherwise used internally on disconnect/destroy path
691 void usb_destroy_configuration(struct usb_device *dev)
698 if (dev->rawdescriptors) {
699 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
700 kfree(dev->rawdescriptors[i]);
702 kfree(dev->rawdescriptors);
703 dev->rawdescriptors = NULL;
706 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
707 struct usb_host_config *cf = &dev->config[c];
710 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
711 if (cf->intf_cache[i])
712 kref_put(&cf->intf_cache[i]->ref,
713 usb_release_interface_cache);
722 * Get the USB config descriptors, cache and parse'em
724 * hub-only!! ... and only in reset path, or usb_new_device()
725 * (used by real hubs and virtual root hubs)
727 int usb_get_configuration(struct usb_device *dev)
729 struct device *ddev = &dev->dev;
730 int ncfg = dev->descriptor.bNumConfigurations;
732 unsigned int cfgno, length;
733 unsigned char *bigbuffer;
734 struct usb_config_descriptor *desc;
738 if (ncfg > USB_MAXCONFIG) {
739 dev_warn(ddev, "too many configurations: %d, "
740 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
741 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
745 dev_err(ddev, "no configurations\n");
749 length = ncfg * sizeof(struct usb_host_config);
750 dev->config = kzalloc(length, GFP_KERNEL);
754 length = ncfg * sizeof(char *);
755 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
756 if (!dev->rawdescriptors)
759 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
764 for (; cfgno < ncfg; cfgno++) {
765 /* We grab just the first descriptor so we know how long
766 * the whole configuration is */
767 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
768 desc, USB_DT_CONFIG_SIZE);
770 dev_err(ddev, "unable to read config index %d "
771 "descriptor/%s: %d\n", cfgno, "start", result);
772 if (result != -EPIPE)
774 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
775 dev->descriptor.bNumConfigurations = cfgno;
777 } else if (result < 4) {
778 dev_err(ddev, "config index %d descriptor too short "
779 "(expected %i, got %i)\n", cfgno,
780 USB_DT_CONFIG_SIZE, result);
784 length = max((int) le16_to_cpu(desc->wTotalLength),
787 /* Now that we know the length, get the whole thing */
788 bigbuffer = kmalloc(length, GFP_KERNEL);
794 if (dev->quirks & USB_QUIRK_DELAY_INIT)
797 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
800 dev_err(ddev, "unable to read config index %d "
801 "descriptor/%s\n", cfgno, "all");
805 if (result < length) {
806 dev_warn(ddev, "config index %d descriptor too short "
807 "(expected %i, got %i)\n", cfgno, length, result);
811 dev->rawdescriptors[cfgno] = bigbuffer;
813 result = usb_parse_configuration(dev, cfgno,
814 &dev->config[cfgno], bigbuffer, length);
824 dev->descriptor.bNumConfigurations = cfgno;
826 if (result == -ENOMEM)
827 dev_err(ddev, "out of memory\n");
831 void usb_release_bos_descriptor(struct usb_device *dev)
834 kfree(dev->bos->desc);
840 /* Get BOS descriptor set */
841 int usb_get_bos_descriptor(struct usb_device *dev)
843 struct device *ddev = &dev->dev;
844 struct usb_bos_descriptor *bos;
845 struct usb_dev_cap_header *cap;
846 unsigned char *buffer;
847 int length, total_len, num, i;
850 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
854 /* Get BOS descriptor */
855 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
856 if (ret < USB_DT_BOS_SIZE) {
857 dev_err(ddev, "unable to get BOS descriptor\n");
864 length = bos->bLength;
865 total_len = le16_to_cpu(bos->wTotalLength);
866 num = bos->bNumDeviceCaps;
868 if (total_len < length)
871 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
875 /* Now let's get the whole BOS descriptor set */
876 buffer = kzalloc(total_len, GFP_KERNEL);
881 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
883 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
884 if (ret < total_len) {
885 dev_err(ddev, "unable to get BOS descriptor set\n");
892 for (i = 0; i < num; i++) {
894 cap = (struct usb_dev_cap_header *)buffer;
895 length = cap->bLength;
897 if (total_len < length)
901 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
902 dev_warn(ddev, "descriptor type invalid, skip\n");
906 switch (cap->bDevCapabilityType) {
907 case USB_CAP_TYPE_WIRELESS_USB:
908 /* Wireless USB cap descriptor is handled by wusb */
910 case USB_CAP_TYPE_EXT:
912 (struct usb_ext_cap_descriptor *)buffer;
914 case USB_SS_CAP_TYPE:
916 (struct usb_ss_cap_descriptor *)buffer;
918 case USB_SSP_CAP_TYPE:
920 (struct usb_ssp_cap_descriptor *)buffer;
922 case CONTAINER_ID_TYPE:
924 (struct usb_ss_container_id_descriptor *)buffer;
934 usb_release_bos_descriptor(dev);