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 int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
148 int asnum, struct usb_host_interface *ifp, int num_ep,
149 unsigned char *buffer, int size)
151 unsigned char *buffer0 = buffer;
152 struct usb_endpoint_descriptor *d;
153 struct usb_host_endpoint *endpoint;
156 d = (struct usb_endpoint_descriptor *) buffer;
157 buffer += d->bLength;
160 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
161 n = USB_DT_ENDPOINT_AUDIO_SIZE;
162 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
163 n = USB_DT_ENDPOINT_SIZE;
165 dev_warn(ddev, "config %d interface %d altsetting %d has an "
166 "invalid endpoint descriptor of length %d, skipping\n",
167 cfgno, inum, asnum, d->bLength);
168 goto skip_to_next_endpoint_or_interface_descriptor;
171 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
172 if (i >= 16 || i == 0) {
173 dev_warn(ddev, "config %d interface %d altsetting %d has an "
174 "invalid endpoint with address 0x%X, skipping\n",
175 cfgno, inum, asnum, d->bEndpointAddress);
176 goto skip_to_next_endpoint_or_interface_descriptor;
179 /* Only store as many endpoints as we have room for */
180 if (ifp->desc.bNumEndpoints >= num_ep)
181 goto skip_to_next_endpoint_or_interface_descriptor;
183 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
184 ++ifp->desc.bNumEndpoints;
186 memcpy(&endpoint->desc, d, n);
187 INIT_LIST_HEAD(&endpoint->urb_list);
189 /* Fix up bInterval values outside the legal range. Use 32 ms if no
190 * proper value can be guessed. */
191 i = 0; /* i = min, j = max, n = default */
193 if (usb_endpoint_xfer_int(d)) {
195 switch (to_usb_device(ddev)->speed) {
196 case USB_SPEED_SUPER:
198 /* Many device manufacturers are using full-speed
199 * bInterval values in high-speed interrupt endpoint
200 * descriptors. Try to fix those and fall back to a
201 * 32 ms default value otherwise. */
202 n = fls(d->bInterval*8);
204 n = 9; /* 32 ms = 2^(9-1) uframes */
208 * Adjust bInterval for quirked devices.
209 * This quirk fixes bIntervals reported in
210 * linear microframes.
212 if (to_usb_device(ddev)->quirks &
213 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
214 n = clamp(fls(d->bInterval), i, j);
218 default: /* USB_SPEED_FULL or _LOW */
219 /* For low-speed, 10 ms is the official minimum.
220 * But some "overclocked" devices might want faster
221 * polling so we'll allow it. */
225 } else if (usb_endpoint_xfer_isoc(d)) {
228 switch (to_usb_device(ddev)->speed) {
230 n = 9; /* 32 ms = 2^(9-1) uframes */
232 default: /* USB_SPEED_FULL */
233 n = 6; /* 32 ms = 2^(6-1) frames */
237 if (d->bInterval < i || d->bInterval > j) {
238 dev_warn(ddev, "config %d interface %d altsetting %d "
239 "endpoint 0x%X has an invalid bInterval %d, "
242 d->bEndpointAddress, d->bInterval, n);
243 endpoint->desc.bInterval = n;
246 /* Some buggy low-speed devices have Bulk endpoints, which is
247 * explicitly forbidden by the USB spec. In an attempt to make
248 * them usable, we will try treating them as Interrupt endpoints.
250 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
251 usb_endpoint_xfer_bulk(d)) {
252 dev_warn(ddev, "config %d interface %d altsetting %d "
253 "endpoint 0x%X is Bulk; changing to Interrupt\n",
254 cfgno, inum, asnum, d->bEndpointAddress);
255 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
256 endpoint->desc.bInterval = 1;
257 if (usb_endpoint_maxp(&endpoint->desc) > 8)
258 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
262 * Some buggy high speed devices have bulk endpoints using
263 * maxpacket sizes other than 512. High speed HCDs may not
264 * be able to handle that particular bug, so let's warn...
266 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
267 && usb_endpoint_xfer_bulk(d)) {
270 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
272 dev_warn(ddev, "config %d interface %d altsetting %d "
273 "bulk endpoint 0x%X has invalid maxpacket %d\n",
274 cfgno, inum, asnum, d->bEndpointAddress,
278 /* Parse a possible SuperSpeed endpoint companion descriptor */
279 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER)
280 usb_parse_ss_endpoint_companion(ddev, cfgno,
281 inum, asnum, endpoint, buffer, size);
283 /* Skip over any Class Specific or Vendor Specific descriptors;
284 * find the next endpoint or interface descriptor */
285 endpoint->extra = buffer;
286 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
287 USB_DT_INTERFACE, &n);
288 endpoint->extralen = i;
289 retval = buffer - buffer0 + i;
291 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
292 n, plural(n), "endpoint");
295 skip_to_next_endpoint_or_interface_descriptor:
296 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
297 USB_DT_INTERFACE, NULL);
298 return buffer - buffer0 + i;
301 void usb_release_interface_cache(struct kref *ref)
303 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
306 for (j = 0; j < intfc->num_altsetting; j++) {
307 struct usb_host_interface *alt = &intfc->altsetting[j];
309 kfree(alt->endpoint);
315 static int usb_parse_interface(struct device *ddev, int cfgno,
316 struct usb_host_config *config, unsigned char *buffer, int size,
317 u8 inums[], u8 nalts[])
319 unsigned char *buffer0 = buffer;
320 struct usb_interface_descriptor *d;
322 struct usb_interface_cache *intfc;
323 struct usb_host_interface *alt;
326 int num_ep, num_ep_orig;
328 d = (struct usb_interface_descriptor *) buffer;
329 buffer += d->bLength;
332 if (d->bLength < USB_DT_INTERFACE_SIZE)
333 goto skip_to_next_interface_descriptor;
335 /* Which interface entry is this? */
337 inum = d->bInterfaceNumber;
338 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
339 if (inums[i] == inum) {
340 intfc = config->intf_cache[i];
344 if (!intfc || intfc->num_altsetting >= nalts[i])
345 goto skip_to_next_interface_descriptor;
347 /* Check for duplicate altsetting entries */
348 asnum = d->bAlternateSetting;
349 for ((i = 0, alt = &intfc->altsetting[0]);
350 i < intfc->num_altsetting;
352 if (alt->desc.bAlternateSetting == asnum) {
353 dev_warn(ddev, "Duplicate descriptor for config %d "
354 "interface %d altsetting %d, skipping\n",
356 goto skip_to_next_interface_descriptor;
360 ++intfc->num_altsetting;
361 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
363 /* Skip over any Class Specific or Vendor Specific descriptors;
364 * find the first endpoint or interface descriptor */
366 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
367 USB_DT_INTERFACE, &n);
370 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
371 n, plural(n), "interface");
375 /* Allocate space for the right(?) number of endpoints */
376 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
377 alt->desc.bNumEndpoints = 0; /* Use as a counter */
378 if (num_ep > USB_MAXENDPOINTS) {
379 dev_warn(ddev, "too many endpoints for config %d interface %d "
380 "altsetting %d: %d, using maximum allowed: %d\n",
381 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
382 num_ep = USB_MAXENDPOINTS;
386 /* Can't allocate 0 bytes */
387 len = sizeof(struct usb_host_endpoint) * num_ep;
388 alt->endpoint = kzalloc(len, GFP_KERNEL);
393 /* Parse all the endpoint descriptors */
396 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
399 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
400 num_ep, buffer, size);
409 if (n != num_ep_orig)
410 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
411 "endpoint descriptor%s, different from the interface "
412 "descriptor's value: %d\n",
413 cfgno, inum, asnum, n, plural(n), num_ep_orig);
414 return buffer - buffer0;
416 skip_to_next_interface_descriptor:
417 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
418 USB_DT_INTERFACE, NULL);
419 return buffer - buffer0 + i;
422 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
423 struct usb_host_config *config, unsigned char *buffer, int size)
425 struct device *ddev = &dev->dev;
426 unsigned char *buffer0 = buffer;
428 int nintf, nintf_orig;
430 struct usb_interface_cache *intfc;
431 unsigned char *buffer2;
433 struct usb_descriptor_header *header;
435 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
436 unsigned iad_num = 0;
438 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
439 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
440 config->desc.bLength < USB_DT_CONFIG_SIZE ||
441 config->desc.bLength > size) {
442 dev_err(ddev, "invalid descriptor for config index %d: "
443 "type = 0x%X, length = %d\n", cfgidx,
444 config->desc.bDescriptorType, config->desc.bLength);
447 cfgno = config->desc.bConfigurationValue;
449 buffer += config->desc.bLength;
450 size -= config->desc.bLength;
452 nintf = nintf_orig = config->desc.bNumInterfaces;
453 if (nintf > USB_MAXINTERFACES) {
454 dev_warn(ddev, "config %d has too many interfaces: %d, "
455 "using maximum allowed: %d\n",
456 cfgno, nintf, USB_MAXINTERFACES);
457 nintf = USB_MAXINTERFACES;
460 /* Go through the descriptors, checking their length and counting the
461 * number of altsettings for each interface */
463 for ((buffer2 = buffer, size2 = size);
465 (buffer2 += header->bLength, size2 -= header->bLength)) {
467 if (size2 < sizeof(struct usb_descriptor_header)) {
468 dev_warn(ddev, "config %d descriptor has %d excess "
469 "byte%s, ignoring\n",
470 cfgno, size2, plural(size2));
474 header = (struct usb_descriptor_header *) buffer2;
475 if ((header->bLength > size2) || (header->bLength < 2)) {
476 dev_warn(ddev, "config %d has an invalid descriptor "
477 "of length %d, skipping remainder of the config\n",
478 cfgno, header->bLength);
482 if (header->bDescriptorType == USB_DT_INTERFACE) {
483 struct usb_interface_descriptor *d;
486 d = (struct usb_interface_descriptor *) header;
487 if (d->bLength < USB_DT_INTERFACE_SIZE) {
488 dev_warn(ddev, "config %d has an invalid "
489 "interface descriptor of length %d, "
490 "skipping\n", cfgno, d->bLength);
494 inum = d->bInterfaceNumber;
496 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
498 dev_warn(ddev, "config %d has more interface "
499 "descriptors, than it declares in "
500 "bNumInterfaces, ignoring interface "
501 "number: %d\n", cfgno, inum);
505 if (inum >= nintf_orig)
506 dev_warn(ddev, "config %d has an invalid "
507 "interface number: %d but max is %d\n",
508 cfgno, inum, nintf_orig - 1);
510 /* Have we already encountered this interface?
511 * Count its altsettings */
512 for (i = 0; i < n; ++i) {
513 if (inums[i] == inum)
519 } else if (n < USB_MAXINTERFACES) {
525 } else if (header->bDescriptorType ==
526 USB_DT_INTERFACE_ASSOCIATION) {
527 if (iad_num == USB_MAXIADS) {
528 dev_warn(ddev, "found more Interface "
529 "Association Descriptors "
530 "than allocated for in "
531 "configuration %d\n", cfgno);
533 config->intf_assoc[iad_num] =
534 (struct usb_interface_assoc_descriptor
539 } else if (header->bDescriptorType == USB_DT_DEVICE ||
540 header->bDescriptorType == USB_DT_CONFIG)
541 dev_warn(ddev, "config %d contains an unexpected "
542 "descriptor of type 0x%X, skipping\n",
543 cfgno, header->bDescriptorType);
545 } /* for ((buffer2 = buffer, size2 = size); ...) */
546 size = buffer2 - buffer;
547 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
550 dev_warn(ddev, "config %d has %d interface%s, different from "
551 "the descriptor's value: %d\n",
552 cfgno, n, plural(n), nintf_orig);
554 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
555 config->desc.bNumInterfaces = nintf = n;
557 /* Check for missing interface numbers */
558 for (i = 0; i < nintf; ++i) {
559 for (j = 0; j < nintf; ++j) {
564 dev_warn(ddev, "config %d has no interface number "
568 /* Allocate the usb_interface_caches and altsetting arrays */
569 for (i = 0; i < nintf; ++i) {
571 if (j > USB_MAXALTSETTING) {
572 dev_warn(ddev, "too many alternate settings for "
573 "config %d interface %d: %d, "
574 "using maximum allowed: %d\n",
575 cfgno, inums[i], j, USB_MAXALTSETTING);
576 nalts[i] = j = USB_MAXALTSETTING;
579 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
580 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
583 kref_init(&intfc->ref);
586 /* FIXME: parse the BOS descriptor */
588 /* Skip over any Class Specific or Vendor Specific descriptors;
589 * find the first interface descriptor */
590 config->extra = buffer;
591 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
592 USB_DT_INTERFACE, &n);
593 config->extralen = i;
595 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
596 n, plural(n), "configuration");
600 /* Parse all the interface/altsetting descriptors */
602 retval = usb_parse_interface(ddev, cfgno, config,
603 buffer, size, inums, nalts);
611 /* Check for missing altsettings */
612 for (i = 0; i < nintf; ++i) {
613 intfc = config->intf_cache[i];
614 for (j = 0; j < intfc->num_altsetting; ++j) {
615 for (n = 0; n < intfc->num_altsetting; ++n) {
616 if (intfc->altsetting[n].desc.
617 bAlternateSetting == j)
620 if (n >= intfc->num_altsetting)
621 dev_warn(ddev, "config %d interface %d has no "
622 "altsetting %d\n", cfgno, inums[i], j);
629 /* hub-only!! ... and only exported for reset/reinit path.
630 * otherwise used internally on disconnect/destroy path
632 void usb_destroy_configuration(struct usb_device *dev)
639 if (dev->rawdescriptors) {
640 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
641 kfree(dev->rawdescriptors[i]);
643 kfree(dev->rawdescriptors);
644 dev->rawdescriptors = NULL;
647 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
648 struct usb_host_config *cf = &dev->config[c];
651 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
652 if (cf->intf_cache[i])
653 kref_put(&cf->intf_cache[i]->ref,
654 usb_release_interface_cache);
663 * Get the USB config descriptors, cache and parse'em
665 * hub-only!! ... and only in reset path, or usb_new_device()
666 * (used by real hubs and virtual root hubs)
668 * NOTE: if this is a WUSB device and is not authorized, we skip the
669 * whole thing. A non-authorized USB device has no
672 int usb_get_configuration(struct usb_device *dev)
674 struct device *ddev = &dev->dev;
675 int ncfg = dev->descriptor.bNumConfigurations;
677 unsigned int cfgno, length;
678 unsigned char *bigbuffer;
679 struct usb_config_descriptor *desc;
682 if (dev->authorized == 0) /* Not really an error */
683 goto out_not_authorized;
685 if (ncfg > USB_MAXCONFIG) {
686 dev_warn(ddev, "too many configurations: %d, "
687 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
688 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
692 dev_err(ddev, "no configurations\n");
696 length = ncfg * sizeof(struct usb_host_config);
697 dev->config = kzalloc(length, GFP_KERNEL);
701 length = ncfg * sizeof(char *);
702 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
703 if (!dev->rawdescriptors)
706 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
711 for (; cfgno < ncfg; cfgno++) {
712 /* We grab just the first descriptor so we know how long
713 * the whole configuration is */
714 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
715 desc, USB_DT_CONFIG_SIZE);
717 dev_err(ddev, "unable to read config index %d "
718 "descriptor/%s: %d\n", cfgno, "start", result);
719 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
720 dev->descriptor.bNumConfigurations = cfgno;
722 } else if (result < 4) {
723 dev_err(ddev, "config index %d descriptor too short "
724 "(expected %i, got %i)\n", cfgno,
725 USB_DT_CONFIG_SIZE, result);
729 length = max((int) le16_to_cpu(desc->wTotalLength),
732 /* Now that we know the length, get the whole thing */
733 bigbuffer = kmalloc(length, GFP_KERNEL);
739 if (dev->quirks & USB_QUIRK_DELAY_INIT)
742 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
745 dev_err(ddev, "unable to read config index %d "
746 "descriptor/%s\n", cfgno, "all");
750 if (result < length) {
751 dev_warn(ddev, "config index %d descriptor too short "
752 "(expected %i, got %i)\n", cfgno, length, result);
756 dev->rawdescriptors[cfgno] = bigbuffer;
758 result = usb_parse_configuration(dev, cfgno,
759 &dev->config[cfgno], bigbuffer, length);
770 dev->descriptor.bNumConfigurations = cfgno;
772 if (result == -ENOMEM)
773 dev_err(ddev, "out of memory\n");
777 void usb_release_bos_descriptor(struct usb_device *dev)
780 kfree(dev->bos->desc);
786 /* Get BOS descriptor set */
787 int usb_get_bos_descriptor(struct usb_device *dev)
789 struct device *ddev = &dev->dev;
790 struct usb_bos_descriptor *bos;
791 struct usb_dev_cap_header *cap;
792 unsigned char *buffer;
793 int length, total_len, num, i;
796 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
800 /* Get BOS descriptor */
801 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
802 if (ret < USB_DT_BOS_SIZE) {
803 dev_err(ddev, "unable to get BOS descriptor\n");
810 length = bos->bLength;
811 total_len = le16_to_cpu(bos->wTotalLength);
812 num = bos->bNumDeviceCaps;
814 if (total_len < length)
817 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
821 /* Now let's get the whole BOS descriptor set */
822 buffer = kzalloc(total_len, GFP_KERNEL);
827 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
829 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
830 if (ret < total_len) {
831 dev_err(ddev, "unable to get BOS descriptor set\n");
838 for (i = 0; i < num; i++) {
840 cap = (struct usb_dev_cap_header *)buffer;
841 length = cap->bLength;
843 if (total_len < length)
847 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
848 dev_warn(ddev, "descriptor type invalid, skip\n");
852 switch (cap->bDevCapabilityType) {
853 case USB_CAP_TYPE_WIRELESS_USB:
854 /* Wireless USB cap descriptor is handled by wusb */
856 case USB_CAP_TYPE_EXT:
858 (struct usb_ext_cap_descriptor *)buffer;
860 case USB_SS_CAP_TYPE:
862 (struct usb_ss_cap_descriptor *)buffer;
864 case CONTAINER_ID_TYPE:
866 (struct usb_ss_container_id_descriptor *)buffer;
876 usb_release_bos_descriptor(dev);