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: "
120 "setting to 3\n", desc->bmAttributes + 1,
121 cfgno, inum, asnum, ep->desc.bEndpointAddress);
122 ep->ss_ep_comp.bmAttributes = 2;
125 if (usb_endpoint_xfer_isoc(&ep->desc))
126 max_tx = (desc->bMaxBurst + 1) *
127 (USB_SS_MULT(desc->bmAttributes)) *
128 usb_endpoint_maxp(&ep->desc);
129 else if (usb_endpoint_xfer_int(&ep->desc))
130 max_tx = usb_endpoint_maxp(&ep->desc) *
131 (desc->bMaxBurst + 1);
134 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
135 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
136 "config %d interface %d altsetting %d ep %d: "
138 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
139 le16_to_cpu(desc->wBytesPerInterval),
140 cfgno, inum, asnum, ep->desc.bEndpointAddress,
142 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
146 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
147 int asnum, struct usb_host_interface *ifp, int num_ep,
148 unsigned char *buffer, int size)
150 unsigned char *buffer0 = buffer;
151 struct usb_endpoint_descriptor *d;
152 struct usb_host_endpoint *endpoint;
155 d = (struct usb_endpoint_descriptor *) buffer;
156 buffer += d->bLength;
159 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
160 n = USB_DT_ENDPOINT_AUDIO_SIZE;
161 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
162 n = USB_DT_ENDPOINT_SIZE;
164 dev_warn(ddev, "config %d interface %d altsetting %d has an "
165 "invalid endpoint descriptor of length %d, skipping\n",
166 cfgno, inum, asnum, d->bLength);
167 goto skip_to_next_endpoint_or_interface_descriptor;
170 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
171 if (i >= 16 || i == 0) {
172 dev_warn(ddev, "config %d interface %d altsetting %d has an "
173 "invalid endpoint with address 0x%X, skipping\n",
174 cfgno, inum, asnum, d->bEndpointAddress);
175 goto skip_to_next_endpoint_or_interface_descriptor;
178 /* Only store as many endpoints as we have room for */
179 if (ifp->desc.bNumEndpoints >= num_ep)
180 goto skip_to_next_endpoint_or_interface_descriptor;
182 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
183 ++ifp->desc.bNumEndpoints;
185 memcpy(&endpoint->desc, d, n);
186 INIT_LIST_HEAD(&endpoint->urb_list);
188 /* Fix up bInterval values outside the legal range. Use 32 ms if no
189 * proper value can be guessed. */
190 i = 0; /* i = min, j = max, n = default */
192 if (usb_endpoint_xfer_int(d)) {
194 switch (to_usb_device(ddev)->speed) {
195 case USB_SPEED_SUPER:
197 /* Many device manufacturers are using full-speed
198 * bInterval values in high-speed interrupt endpoint
199 * descriptors. Try to fix those and fall back to a
200 * 32 ms default value otherwise. */
201 n = fls(d->bInterval*8);
203 n = 9; /* 32 ms = 2^(9-1) uframes */
207 * Adjust bInterval for quirked devices.
208 * This quirk fixes bIntervals reported in
209 * linear microframes.
211 if (to_usb_device(ddev)->quirks &
212 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
213 n = clamp(fls(d->bInterval), i, j);
217 default: /* USB_SPEED_FULL or _LOW */
218 /* For low-speed, 10 ms is the official minimum.
219 * But some "overclocked" devices might want faster
220 * polling so we'll allow it. */
224 } else if (usb_endpoint_xfer_isoc(d)) {
227 switch (to_usb_device(ddev)->speed) {
229 n = 9; /* 32 ms = 2^(9-1) uframes */
231 default: /* USB_SPEED_FULL */
232 n = 6; /* 32 ms = 2^(6-1) frames */
236 if (d->bInterval < i || d->bInterval > j) {
237 dev_warn(ddev, "config %d interface %d altsetting %d "
238 "endpoint 0x%X has an invalid bInterval %d, "
241 d->bEndpointAddress, d->bInterval, n);
242 endpoint->desc.bInterval = n;
245 /* Some buggy low-speed devices have Bulk endpoints, which is
246 * explicitly forbidden by the USB spec. In an attempt to make
247 * them usable, we will try treating them as Interrupt endpoints.
249 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
250 usb_endpoint_xfer_bulk(d)) {
251 dev_warn(ddev, "config %d interface %d altsetting %d "
252 "endpoint 0x%X is Bulk; changing to Interrupt\n",
253 cfgno, inum, asnum, d->bEndpointAddress);
254 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
255 endpoint->desc.bInterval = 1;
256 if (usb_endpoint_maxp(&endpoint->desc) > 8)
257 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
261 * Some buggy high speed devices have bulk endpoints using
262 * maxpacket sizes other than 512. High speed HCDs may not
263 * be able to handle that particular bug, so let's warn...
265 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
266 && usb_endpoint_xfer_bulk(d)) {
269 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
271 dev_warn(ddev, "config %d interface %d altsetting %d "
272 "bulk endpoint 0x%X has invalid maxpacket %d\n",
273 cfgno, inum, asnum, d->bEndpointAddress,
277 /* Parse a possible SuperSpeed endpoint companion descriptor */
278 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER)
279 usb_parse_ss_endpoint_companion(ddev, cfgno,
280 inum, asnum, endpoint, buffer, size);
282 /* Skip over any Class Specific or Vendor Specific descriptors;
283 * find the next endpoint or interface descriptor */
284 endpoint->extra = buffer;
285 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
286 USB_DT_INTERFACE, &n);
287 endpoint->extralen = i;
288 retval = buffer - buffer0 + i;
290 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
291 n, plural(n), "endpoint");
294 skip_to_next_endpoint_or_interface_descriptor:
295 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
296 USB_DT_INTERFACE, NULL);
297 return buffer - buffer0 + i;
300 void usb_release_interface_cache(struct kref *ref)
302 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
305 for (j = 0; j < intfc->num_altsetting; j++) {
306 struct usb_host_interface *alt = &intfc->altsetting[j];
308 kfree(alt->endpoint);
314 static int usb_parse_interface(struct device *ddev, int cfgno,
315 struct usb_host_config *config, unsigned char *buffer, int size,
316 u8 inums[], u8 nalts[])
318 unsigned char *buffer0 = buffer;
319 struct usb_interface_descriptor *d;
321 struct usb_interface_cache *intfc;
322 struct usb_host_interface *alt;
325 int num_ep, num_ep_orig;
327 d = (struct usb_interface_descriptor *) buffer;
328 buffer += d->bLength;
331 if (d->bLength < USB_DT_INTERFACE_SIZE)
332 goto skip_to_next_interface_descriptor;
334 /* Which interface entry is this? */
336 inum = d->bInterfaceNumber;
337 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
338 if (inums[i] == inum) {
339 intfc = config->intf_cache[i];
343 if (!intfc || intfc->num_altsetting >= nalts[i])
344 goto skip_to_next_interface_descriptor;
346 /* Check for duplicate altsetting entries */
347 asnum = d->bAlternateSetting;
348 for ((i = 0, alt = &intfc->altsetting[0]);
349 i < intfc->num_altsetting;
351 if (alt->desc.bAlternateSetting == asnum) {
352 dev_warn(ddev, "Duplicate descriptor for config %d "
353 "interface %d altsetting %d, skipping\n",
355 goto skip_to_next_interface_descriptor;
359 ++intfc->num_altsetting;
360 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
362 /* Skip over any Class Specific or Vendor Specific descriptors;
363 * find the first endpoint or interface descriptor */
365 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
366 USB_DT_INTERFACE, &n);
369 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
370 n, plural(n), "interface");
374 /* Allocate space for the right(?) number of endpoints */
375 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
376 alt->desc.bNumEndpoints = 0; /* Use as a counter */
377 if (num_ep > USB_MAXENDPOINTS) {
378 dev_warn(ddev, "too many endpoints for config %d interface %d "
379 "altsetting %d: %d, using maximum allowed: %d\n",
380 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
381 num_ep = USB_MAXENDPOINTS;
385 /* Can't allocate 0 bytes */
386 len = sizeof(struct usb_host_endpoint) * num_ep;
387 alt->endpoint = kzalloc(len, GFP_KERNEL);
392 /* Parse all the endpoint descriptors */
395 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
398 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
399 num_ep, buffer, size);
408 if (n != num_ep_orig)
409 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
410 "endpoint descriptor%s, different from the interface "
411 "descriptor's value: %d\n",
412 cfgno, inum, asnum, n, plural(n), num_ep_orig);
413 return buffer - buffer0;
415 skip_to_next_interface_descriptor:
416 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
417 USB_DT_INTERFACE, NULL);
418 return buffer - buffer0 + i;
421 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
422 struct usb_host_config *config, unsigned char *buffer, int size)
424 struct device *ddev = &dev->dev;
425 unsigned char *buffer0 = buffer;
427 int nintf, nintf_orig;
429 struct usb_interface_cache *intfc;
430 unsigned char *buffer2;
432 struct usb_descriptor_header *header;
434 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
435 unsigned iad_num = 0;
437 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
438 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
439 config->desc.bLength < USB_DT_CONFIG_SIZE ||
440 config->desc.bLength > size) {
441 dev_err(ddev, "invalid descriptor for config index %d: "
442 "type = 0x%X, length = %d\n", cfgidx,
443 config->desc.bDescriptorType, config->desc.bLength);
446 cfgno = config->desc.bConfigurationValue;
448 buffer += config->desc.bLength;
449 size -= config->desc.bLength;
451 nintf = nintf_orig = config->desc.bNumInterfaces;
452 if (nintf > USB_MAXINTERFACES) {
453 dev_warn(ddev, "config %d has too many interfaces: %d, "
454 "using maximum allowed: %d\n",
455 cfgno, nintf, USB_MAXINTERFACES);
456 nintf = USB_MAXINTERFACES;
459 /* Go through the descriptors, checking their length and counting the
460 * number of altsettings for each interface */
462 for ((buffer2 = buffer, size2 = size);
464 (buffer2 += header->bLength, size2 -= header->bLength)) {
466 if (size2 < sizeof(struct usb_descriptor_header)) {
467 dev_warn(ddev, "config %d descriptor has %d excess "
468 "byte%s, ignoring\n",
469 cfgno, size2, plural(size2));
473 header = (struct usb_descriptor_header *) buffer2;
474 if ((header->bLength > size2) || (header->bLength < 2)) {
475 dev_warn(ddev, "config %d has an invalid descriptor "
476 "of length %d, skipping remainder of the config\n",
477 cfgno, header->bLength);
481 if (header->bDescriptorType == USB_DT_INTERFACE) {
482 struct usb_interface_descriptor *d;
485 d = (struct usb_interface_descriptor *) header;
486 if (d->bLength < USB_DT_INTERFACE_SIZE) {
487 dev_warn(ddev, "config %d has an invalid "
488 "interface descriptor of length %d, "
489 "skipping\n", cfgno, d->bLength);
493 inum = d->bInterfaceNumber;
495 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
497 dev_warn(ddev, "config %d has more interface "
498 "descriptors, than it declares in "
499 "bNumInterfaces, ignoring interface "
500 "number: %d\n", cfgno, inum);
504 if (inum >= nintf_orig)
505 dev_warn(ddev, "config %d has an invalid "
506 "interface number: %d but max is %d\n",
507 cfgno, inum, nintf_orig - 1);
509 /* Have we already encountered this interface?
510 * Count its altsettings */
511 for (i = 0; i < n; ++i) {
512 if (inums[i] == inum)
518 } else if (n < USB_MAXINTERFACES) {
524 } else if (header->bDescriptorType ==
525 USB_DT_INTERFACE_ASSOCIATION) {
526 if (iad_num == USB_MAXIADS) {
527 dev_warn(ddev, "found more Interface "
528 "Association Descriptors "
529 "than allocated for in "
530 "configuration %d\n", cfgno);
532 config->intf_assoc[iad_num] =
533 (struct usb_interface_assoc_descriptor
538 } else if (header->bDescriptorType == USB_DT_DEVICE ||
539 header->bDescriptorType == USB_DT_CONFIG)
540 dev_warn(ddev, "config %d contains an unexpected "
541 "descriptor of type 0x%X, skipping\n",
542 cfgno, header->bDescriptorType);
544 } /* for ((buffer2 = buffer, size2 = size); ...) */
545 size = buffer2 - buffer;
546 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
549 dev_warn(ddev, "config %d has %d interface%s, different from "
550 "the descriptor's value: %d\n",
551 cfgno, n, plural(n), nintf_orig);
553 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
554 config->desc.bNumInterfaces = nintf = n;
556 /* Check for missing interface numbers */
557 for (i = 0; i < nintf; ++i) {
558 for (j = 0; j < nintf; ++j) {
563 dev_warn(ddev, "config %d has no interface number "
567 /* Allocate the usb_interface_caches and altsetting arrays */
568 for (i = 0; i < nintf; ++i) {
570 if (j > USB_MAXALTSETTING) {
571 dev_warn(ddev, "too many alternate settings for "
572 "config %d interface %d: %d, "
573 "using maximum allowed: %d\n",
574 cfgno, inums[i], j, USB_MAXALTSETTING);
575 nalts[i] = j = USB_MAXALTSETTING;
578 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
579 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
582 kref_init(&intfc->ref);
585 /* FIXME: parse the BOS descriptor */
587 /* Skip over any Class Specific or Vendor Specific descriptors;
588 * find the first interface descriptor */
589 config->extra = buffer;
590 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
591 USB_DT_INTERFACE, &n);
592 config->extralen = i;
594 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
595 n, plural(n), "configuration");
599 /* Parse all the interface/altsetting descriptors */
601 retval = usb_parse_interface(ddev, cfgno, config,
602 buffer, size, inums, nalts);
610 /* Check for missing altsettings */
611 for (i = 0; i < nintf; ++i) {
612 intfc = config->intf_cache[i];
613 for (j = 0; j < intfc->num_altsetting; ++j) {
614 for (n = 0; n < intfc->num_altsetting; ++n) {
615 if (intfc->altsetting[n].desc.
616 bAlternateSetting == j)
619 if (n >= intfc->num_altsetting)
620 dev_warn(ddev, "config %d interface %d has no "
621 "altsetting %d\n", cfgno, inums[i], j);
628 /* hub-only!! ... and only exported for reset/reinit path.
629 * otherwise used internally on disconnect/destroy path
631 void usb_destroy_configuration(struct usb_device *dev)
638 if (dev->rawdescriptors) {
639 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
640 kfree(dev->rawdescriptors[i]);
642 kfree(dev->rawdescriptors);
643 dev->rawdescriptors = NULL;
646 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
647 struct usb_host_config *cf = &dev->config[c];
650 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
651 if (cf->intf_cache[i])
652 kref_put(&cf->intf_cache[i]->ref,
653 usb_release_interface_cache);
662 * Get the USB config descriptors, cache and parse'em
664 * hub-only!! ... and only in reset path, or usb_new_device()
665 * (used by real hubs and virtual root hubs)
667 * NOTE: if this is a WUSB device and is not authorized, we skip the
668 * whole thing. A non-authorized USB device has no
671 int usb_get_configuration(struct usb_device *dev)
673 struct device *ddev = &dev->dev;
674 int ncfg = dev->descriptor.bNumConfigurations;
676 unsigned int cfgno, length;
677 unsigned char *bigbuffer;
678 struct usb_config_descriptor *desc;
681 if (dev->authorized == 0) /* Not really an error */
682 goto out_not_authorized;
684 if (ncfg > USB_MAXCONFIG) {
685 dev_warn(ddev, "too many configurations: %d, "
686 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
687 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
691 dev_err(ddev, "no configurations\n");
695 length = ncfg * sizeof(struct usb_host_config);
696 dev->config = kzalloc(length, GFP_KERNEL);
700 length = ncfg * sizeof(char *);
701 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
702 if (!dev->rawdescriptors)
705 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
710 for (; cfgno < ncfg; cfgno++) {
711 /* We grab just the first descriptor so we know how long
712 * the whole configuration is */
713 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
714 desc, USB_DT_CONFIG_SIZE);
716 dev_err(ddev, "unable to read config index %d "
717 "descriptor/%s: %d\n", cfgno, "start", result);
718 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
719 dev->descriptor.bNumConfigurations = cfgno;
721 } else if (result < 4) {
722 dev_err(ddev, "config index %d descriptor too short "
723 "(expected %i, got %i)\n", cfgno,
724 USB_DT_CONFIG_SIZE, result);
728 length = max((int) le16_to_cpu(desc->wTotalLength),
731 /* Now that we know the length, get the whole thing */
732 bigbuffer = kmalloc(length, GFP_KERNEL);
738 if (dev->quirks & USB_QUIRK_DELAY_INIT)
741 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
744 dev_err(ddev, "unable to read config index %d "
745 "descriptor/%s\n", cfgno, "all");
749 if (result < length) {
750 dev_warn(ddev, "config index %d descriptor too short "
751 "(expected %i, got %i)\n", cfgno, length, result);
755 dev->rawdescriptors[cfgno] = bigbuffer;
757 result = usb_parse_configuration(dev, cfgno,
758 &dev->config[cfgno], bigbuffer, length);
769 dev->descriptor.bNumConfigurations = cfgno;
771 if (result == -ENOMEM)
772 dev_err(ddev, "out of memory\n");
776 void usb_release_bos_descriptor(struct usb_device *dev)
779 kfree(dev->bos->desc);
785 /* Get BOS descriptor set */
786 int usb_get_bos_descriptor(struct usb_device *dev)
788 struct device *ddev = &dev->dev;
789 struct usb_bos_descriptor *bos;
790 struct usb_dev_cap_header *cap;
791 unsigned char *buffer;
792 int length, total_len, num, i;
795 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
799 /* Get BOS descriptor */
800 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
801 if (ret < USB_DT_BOS_SIZE) {
802 dev_err(ddev, "unable to get BOS descriptor\n");
809 length = bos->bLength;
810 total_len = le16_to_cpu(bos->wTotalLength);
811 num = bos->bNumDeviceCaps;
813 if (total_len < length)
816 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
820 /* Now let's get the whole BOS descriptor set */
821 buffer = kzalloc(total_len, GFP_KERNEL);
826 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
828 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
829 if (ret < total_len) {
830 dev_err(ddev, "unable to get BOS descriptor set\n");
837 for (i = 0; i < num; i++) {
839 cap = (struct usb_dev_cap_header *)buffer;
840 length = cap->bLength;
842 if (total_len < length)
846 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
847 dev_warn(ddev, "descriptor type invalid, skip\n");
851 switch (cap->bDevCapabilityType) {
852 case USB_CAP_TYPE_WIRELESS_USB:
853 /* Wireless USB cap descriptor is handled by wusb */
855 case USB_CAP_TYPE_EXT:
857 (struct usb_ext_cap_descriptor *)buffer;
859 case USB_SS_CAP_TYPE:
861 (struct usb_ss_cap_descriptor *)buffer;
863 case CONTAINER_ID_TYPE:
865 (struct usb_ss_container_id_descriptor *)buffer;
875 usb_release_bos_descriptor(dev);