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/init.h>
7 #include <linux/slab.h>
8 #include <linux/device.h>
9 #include <asm/byteorder.h>
13 #define USB_MAXALTSETTING 128 /* Hard limit */
14 #define USB_MAXENDPOINTS 30 /* Hard limit */
16 #define USB_MAXCONFIG 8 /* Arbitrary limit */
19 static inline const char *plural(int n)
21 return (n == 1 ? "" : "s");
24 static int find_next_descriptor(unsigned char *buffer, int size,
25 int dt1, int dt2, int *num_skipped)
27 struct usb_descriptor_header *h;
29 unsigned char *buffer0 = buffer;
31 /* Find the next descriptor of type dt1 or dt2 */
33 h = (struct usb_descriptor_header *) buffer;
34 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
41 /* Store the number of descriptors skipped and return the
42 * number of bytes skipped */
45 return buffer - buffer0;
48 static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
49 int inum, int asnum, struct usb_host_endpoint *ep,
50 unsigned char *buffer, int size)
52 struct usb_ss_ep_comp_descriptor *desc;
55 /* The SuperSpeed endpoint companion descriptor is supposed to
56 * be the first thing immediately following the endpoint descriptor.
58 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
59 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
60 size < USB_DT_SS_EP_COMP_SIZE) {
61 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
62 " interface %d altsetting %d ep %d: "
63 "using minimum values\n",
64 cfgno, inum, asnum, ep->desc.bEndpointAddress);
66 /* Fill in some default values.
67 * Leave bmAttributes as zero, which will mean no streams for
68 * bulk, and isoc won't support multiple bursts of packets.
69 * With bursts of only one packet, and a Mult of 1, the max
70 * amount of data moved per endpoint service interval is one
73 ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
74 ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
75 if (usb_endpoint_xfer_isoc(&ep->desc) ||
76 usb_endpoint_xfer_int(&ep->desc))
77 ep->ss_ep_comp.wBytesPerInterval =
78 ep->desc.wMaxPacketSize;
82 memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
84 /* Check the various values */
85 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
86 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
87 "config %d interface %d altsetting %d ep %d: "
88 "setting to zero\n", desc->bMaxBurst,
89 cfgno, inum, asnum, ep->desc.bEndpointAddress);
90 ep->ss_ep_comp.bMaxBurst = 0;
91 } else if (desc->bMaxBurst > 15) {
92 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
93 "config %d interface %d altsetting %d ep %d: "
94 "setting to 15\n", desc->bMaxBurst,
95 cfgno, inum, asnum, ep->desc.bEndpointAddress);
96 ep->ss_ep_comp.bMaxBurst = 15;
99 if ((usb_endpoint_xfer_control(&ep->desc) ||
100 usb_endpoint_xfer_int(&ep->desc)) &&
101 desc->bmAttributes != 0) {
102 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
103 "config %d interface %d altsetting %d ep %d: "
105 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
107 cfgno, inum, asnum, ep->desc.bEndpointAddress);
108 ep->ss_ep_comp.bmAttributes = 0;
109 } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
110 desc->bmAttributes > 16) {
111 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
112 "config %d interface %d altsetting %d ep %d: "
114 cfgno, inum, asnum, ep->desc.bEndpointAddress);
115 ep->ss_ep_comp.bmAttributes = 16;
116 } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
117 USB_SS_MULT(desc->bmAttributes) > 3) {
118 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
119 "config %d interface %d altsetting %d ep %d: "
121 USB_SS_MULT(desc->bmAttributes),
122 cfgno, inum, asnum, ep->desc.bEndpointAddress);
123 ep->ss_ep_comp.bmAttributes = 2;
126 if (usb_endpoint_xfer_isoc(&ep->desc))
127 max_tx = (desc->bMaxBurst + 1) *
128 (USB_SS_MULT(desc->bmAttributes)) *
129 usb_endpoint_maxp(&ep->desc);
130 else if (usb_endpoint_xfer_int(&ep->desc))
131 max_tx = usb_endpoint_maxp(&ep->desc) *
132 (desc->bMaxBurst + 1);
135 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
136 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
137 "config %d interface %d altsetting %d ep %d: "
139 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
140 le16_to_cpu(desc->wBytesPerInterval),
141 cfgno, inum, asnum, ep->desc.bEndpointAddress,
143 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
147 static const unsigned short low_speed_maxpacket_maxes[4] = {
148 [USB_ENDPOINT_XFER_CONTROL] = 8,
149 [USB_ENDPOINT_XFER_ISOC] = 0,
150 [USB_ENDPOINT_XFER_BULK] = 0,
151 [USB_ENDPOINT_XFER_INT] = 8,
153 static const unsigned short full_speed_maxpacket_maxes[4] = {
154 [USB_ENDPOINT_XFER_CONTROL] = 64,
155 [USB_ENDPOINT_XFER_ISOC] = 1023,
156 [USB_ENDPOINT_XFER_BULK] = 64,
157 [USB_ENDPOINT_XFER_INT] = 64,
159 static const unsigned short high_speed_maxpacket_maxes[4] = {
160 [USB_ENDPOINT_XFER_CONTROL] = 64,
161 [USB_ENDPOINT_XFER_ISOC] = 1024,
162 [USB_ENDPOINT_XFER_BULK] = 512,
163 [USB_ENDPOINT_XFER_INT] = 1023,
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 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
211 ++ifp->desc.bNumEndpoints;
213 memcpy(&endpoint->desc, d, n);
214 INIT_LIST_HEAD(&endpoint->urb_list);
216 /* Fix up bInterval values outside the legal range. Use 32 ms if no
217 * proper value can be guessed. */
218 i = 0; /* i = min, j = max, n = default */
220 if (usb_endpoint_xfer_int(d)) {
222 switch (to_usb_device(ddev)->speed) {
223 case USB_SPEED_SUPER:
225 /* Many device manufacturers are using full-speed
226 * bInterval values in high-speed interrupt endpoint
227 * descriptors. Try to fix those and fall back to a
228 * 32 ms default value otherwise. */
229 n = fls(d->bInterval*8);
231 n = 9; /* 32 ms = 2^(9-1) uframes */
235 * Adjust bInterval for quirked devices.
236 * This quirk fixes bIntervals reported in
237 * linear microframes.
239 if (to_usb_device(ddev)->quirks &
240 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
241 n = clamp(fls(d->bInterval), i, j);
245 default: /* USB_SPEED_FULL or _LOW */
246 /* For low-speed, 10 ms is the official minimum.
247 * But some "overclocked" devices might want faster
248 * polling so we'll allow it. */
252 } else if (usb_endpoint_xfer_isoc(d)) {
255 switch (to_usb_device(ddev)->speed) {
257 n = 9; /* 32 ms = 2^(9-1) uframes */
259 default: /* USB_SPEED_FULL */
260 n = 6; /* 32 ms = 2^(6-1) frames */
264 if (d->bInterval < i || d->bInterval > j) {
265 dev_warn(ddev, "config %d interface %d altsetting %d "
266 "endpoint 0x%X has an invalid bInterval %d, "
269 d->bEndpointAddress, d->bInterval, n);
270 endpoint->desc.bInterval = n;
273 /* Some buggy low-speed devices have Bulk endpoints, which is
274 * explicitly forbidden by the USB spec. In an attempt to make
275 * them usable, we will try treating them as Interrupt endpoints.
277 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
278 usb_endpoint_xfer_bulk(d)) {
279 dev_warn(ddev, "config %d interface %d altsetting %d "
280 "endpoint 0x%X is Bulk; changing to Interrupt\n",
281 cfgno, inum, asnum, d->bEndpointAddress);
282 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
283 endpoint->desc.bInterval = 1;
284 if (usb_endpoint_maxp(&endpoint->desc) > 8)
285 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
288 /* Validate the wMaxPacketSize field */
289 maxp = usb_endpoint_maxp(&endpoint->desc);
291 /* Find the highest legal maxpacket size for this endpoint */
292 i = 0; /* additional transactions per microframe */
293 switch (to_usb_device(ddev)->speed) {
295 maxpacket_maxes = low_speed_maxpacket_maxes;
298 maxpacket_maxes = full_speed_maxpacket_maxes;
301 /* Bits 12..11 are allowed only for HS periodic endpoints */
302 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
303 i = maxp & (BIT(12) | BIT(11));
308 maxpacket_maxes = high_speed_maxpacket_maxes;
310 case USB_SPEED_SUPER:
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 * NOTE: if this is a WUSB device and is not authorized, we skip the
728 * whole thing. A non-authorized USB device has no
731 int usb_get_configuration(struct usb_device *dev)
733 struct device *ddev = &dev->dev;
734 int ncfg = dev->descriptor.bNumConfigurations;
736 unsigned int cfgno, length;
737 unsigned char *bigbuffer;
738 struct usb_config_descriptor *desc;
741 if (dev->authorized == 0) /* Not really an error */
742 goto out_not_authorized;
744 if (ncfg > USB_MAXCONFIG) {
745 dev_warn(ddev, "too many configurations: %d, "
746 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
747 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
751 dev_err(ddev, "no configurations\n");
755 length = ncfg * sizeof(struct usb_host_config);
756 dev->config = kzalloc(length, GFP_KERNEL);
760 length = ncfg * sizeof(char *);
761 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
762 if (!dev->rawdescriptors)
765 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
770 for (; cfgno < ncfg; cfgno++) {
771 /* We grab just the first descriptor so we know how long
772 * the whole configuration is */
773 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
774 desc, USB_DT_CONFIG_SIZE);
776 dev_err(ddev, "unable to read config index %d "
777 "descriptor/%s: %d\n", cfgno, "start", result);
778 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
779 dev->descriptor.bNumConfigurations = cfgno;
781 } else if (result < 4) {
782 dev_err(ddev, "config index %d descriptor too short "
783 "(expected %i, got %i)\n", cfgno,
784 USB_DT_CONFIG_SIZE, result);
788 length = max((int) le16_to_cpu(desc->wTotalLength),
791 /* Now that we know the length, get the whole thing */
792 bigbuffer = kmalloc(length, GFP_KERNEL);
798 if (dev->quirks & USB_QUIRK_DELAY_INIT)
801 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
804 dev_err(ddev, "unable to read config index %d "
805 "descriptor/%s\n", cfgno, "all");
809 if (result < length) {
810 dev_warn(ddev, "config index %d descriptor too short "
811 "(expected %i, got %i)\n", cfgno, length, result);
815 dev->rawdescriptors[cfgno] = bigbuffer;
817 result = usb_parse_configuration(dev, cfgno,
818 &dev->config[cfgno], bigbuffer, length);
829 dev->descriptor.bNumConfigurations = cfgno;
831 if (result == -ENOMEM)
832 dev_err(ddev, "out of memory\n");
836 void usb_release_bos_descriptor(struct usb_device *dev)
839 kfree(dev->bos->desc);
845 /* Get BOS descriptor set */
846 int usb_get_bos_descriptor(struct usb_device *dev)
848 struct device *ddev = &dev->dev;
849 struct usb_bos_descriptor *bos;
850 struct usb_dev_cap_header *cap;
851 unsigned char *buffer;
852 int length, total_len, num, i;
855 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
859 /* Get BOS descriptor */
860 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
861 if (ret < USB_DT_BOS_SIZE) {
862 dev_err(ddev, "unable to get BOS descriptor\n");
869 length = bos->bLength;
870 total_len = le16_to_cpu(bos->wTotalLength);
871 num = bos->bNumDeviceCaps;
873 if (total_len < length)
876 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
880 /* Now let's get the whole BOS descriptor set */
881 buffer = kzalloc(total_len, GFP_KERNEL);
886 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
888 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
889 if (ret < total_len) {
890 dev_err(ddev, "unable to get BOS descriptor set\n");
897 for (i = 0; i < num; i++) {
899 cap = (struct usb_dev_cap_header *)buffer;
900 length = cap->bLength;
902 if (total_len < length)
906 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
907 dev_warn(ddev, "descriptor type invalid, skip\n");
911 switch (cap->bDevCapabilityType) {
912 case USB_CAP_TYPE_WIRELESS_USB:
913 /* Wireless USB cap descriptor is handled by wusb */
915 case USB_CAP_TYPE_EXT:
917 (struct usb_ext_cap_descriptor *)buffer;
919 case USB_SS_CAP_TYPE:
921 (struct usb_ss_cap_descriptor *)buffer;
923 case CONTAINER_ID_TYPE:
925 (struct usb_ss_container_id_descriptor *)buffer;
935 usb_release_bos_descriptor(dev);