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 /* FIXME: this is a kludge */
25 static int find_next_descriptor_more(unsigned char *buffer, int size,
26 int dt1, int dt2, int dt3, int *num_skipped)
28 struct usb_descriptor_header *h;
30 unsigned char *buffer0 = buffer;
32 /* Find the next descriptor of type dt1 or dt2 or dt3 */
34 h = (struct usb_descriptor_header *) buffer;
35 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2 ||
36 h->bDescriptorType == dt3)
43 /* Store the number of descriptors skipped and return the
44 * number of bytes skipped */
47 return buffer - buffer0;
50 static int find_next_descriptor(unsigned char *buffer, int size,
51 int dt1, int dt2, int *num_skipped)
53 struct usb_descriptor_header *h;
55 unsigned char *buffer0 = buffer;
57 /* Find the next descriptor of type dt1 or dt2 */
59 h = (struct usb_descriptor_header *) buffer;
60 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
67 /* Store the number of descriptors skipped and return the
68 * number of bytes skipped */
71 return buffer - buffer0;
74 static int usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
75 int inum, int asnum, struct usb_host_endpoint *ep,
76 int num_ep, unsigned char *buffer, int size)
78 unsigned char *buffer_start = buffer;
79 struct usb_ss_ep_comp_descriptor *desc;
85 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
86 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) {
87 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
88 " interface %d altsetting %d ep %d: "
89 "using minimum values\n",
90 cfgno, inum, asnum, ep->desc.bEndpointAddress);
92 * The next descriptor is for an Endpoint or Interface,
93 * no extra descriptors to copy into the companion structure,
94 * and we didn't eat up any of the buffer.
98 memcpy(&ep->ss_ep_comp->desc, desc, USB_DT_SS_EP_COMP_SIZE);
99 desc = &ep->ss_ep_comp->desc;
100 buffer += desc->bLength;
101 size -= desc->bLength;
103 /* Eat up the other descriptors we don't care about */
104 ep->ss_ep_comp->extra = buffer;
105 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
106 USB_DT_INTERFACE, &num_skipped);
107 ep->ss_ep_comp->extralen = i;
110 retval = buffer - buffer_start;
112 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
113 num_skipped, plural(num_skipped),
114 "SuperSpeed endpoint companion");
116 /* Check the various values */
117 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
118 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
119 "config %d interface %d altsetting %d ep %d: "
120 "setting to zero\n", desc->bMaxBurst,
121 cfgno, inum, asnum, ep->desc.bEndpointAddress);
124 if (desc->bMaxBurst > 15) {
125 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
126 "config %d interface %d altsetting %d ep %d: "
127 "setting to 15\n", desc->bMaxBurst,
128 cfgno, inum, asnum, ep->desc.bEndpointAddress);
129 desc->bMaxBurst = 15;
131 if ((usb_endpoint_xfer_control(&ep->desc) || usb_endpoint_xfer_int(&ep->desc))
132 && desc->bmAttributes != 0) {
133 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
134 "config %d interface %d altsetting %d ep %d: "
136 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
138 cfgno, inum, asnum, ep->desc.bEndpointAddress);
139 desc->bmAttributes = 0;
141 if (usb_endpoint_xfer_bulk(&ep->desc) && desc->bmAttributes > 16) {
142 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
143 "config %d interface %d altsetting %d ep %d: "
145 cfgno, inum, asnum, ep->desc.bEndpointAddress);
146 desc->bmAttributes = 16;
148 if (usb_endpoint_xfer_isoc(&ep->desc) && desc->bmAttributes > 2) {
149 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
150 "config %d interface %d altsetting %d ep %d: "
151 "setting to 3\n", desc->bmAttributes + 1,
152 cfgno, inum, asnum, ep->desc.bEndpointAddress);
153 desc->bmAttributes = 2;
155 if (usb_endpoint_xfer_isoc(&ep->desc)) {
156 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1) *
157 (desc->bmAttributes + 1);
158 } else if (usb_endpoint_xfer_int(&ep->desc)) {
159 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1);
163 if (desc->wBytesPerInterval > max_tx) {
164 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
165 "config %d interface %d altsetting %d ep %d: "
167 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
168 desc->wBytesPerInterval,
169 cfgno, inum, asnum, ep->desc.bEndpointAddress,
171 desc->wBytesPerInterval = max_tx;
177 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
178 int asnum, struct usb_host_interface *ifp, int num_ep,
179 unsigned char *buffer, int size)
181 unsigned char *buffer0 = buffer;
182 struct usb_endpoint_descriptor *d;
183 struct usb_host_endpoint *endpoint;
186 d = (struct usb_endpoint_descriptor *) buffer;
187 buffer += d->bLength;
190 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
191 n = USB_DT_ENDPOINT_AUDIO_SIZE;
192 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
193 n = USB_DT_ENDPOINT_SIZE;
195 dev_warn(ddev, "config %d interface %d altsetting %d has an "
196 "invalid endpoint descriptor of length %d, skipping\n",
197 cfgno, inum, asnum, d->bLength);
198 goto skip_to_next_endpoint_or_interface_descriptor;
201 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
202 if (i >= 16 || i == 0) {
203 dev_warn(ddev, "config %d interface %d altsetting %d has an "
204 "invalid endpoint with address 0x%X, skipping\n",
205 cfgno, inum, asnum, d->bEndpointAddress);
206 goto skip_to_next_endpoint_or_interface_descriptor;
209 /* Only store as many endpoints as we have room for */
210 if (ifp->desc.bNumEndpoints >= num_ep)
211 goto skip_to_next_endpoint_or_interface_descriptor;
213 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
214 ++ifp->desc.bNumEndpoints;
216 memcpy(&endpoint->desc, d, n);
217 INIT_LIST_HEAD(&endpoint->urb_list);
219 /* Fix up bInterval values outside the legal range. Use 32 ms if no
220 * proper value can be guessed. */
221 i = 0; /* i = min, j = max, n = default */
223 if (usb_endpoint_xfer_int(d)) {
225 switch (to_usb_device(ddev)->speed) {
226 case USB_SPEED_SUPER:
228 /* Many device manufacturers are using full-speed
229 * bInterval values in high-speed interrupt endpoint
230 * descriptors. Try to fix those and fall back to a
231 * 32 ms default value otherwise. */
232 n = fls(d->bInterval*8);
234 n = 9; /* 32 ms = 2^(9-1) uframes */
237 default: /* USB_SPEED_FULL or _LOW */
238 /* For low-speed, 10 ms is the official minimum.
239 * But some "overclocked" devices might want faster
240 * polling so we'll allow it. */
244 } else if (usb_endpoint_xfer_isoc(d)) {
247 switch (to_usb_device(ddev)->speed) {
249 n = 9; /* 32 ms = 2^(9-1) uframes */
251 default: /* USB_SPEED_FULL */
252 n = 6; /* 32 ms = 2^(6-1) frames */
256 if (d->bInterval < i || d->bInterval > j) {
257 dev_warn(ddev, "config %d interface %d altsetting %d "
258 "endpoint 0x%X has an invalid bInterval %d, "
261 d->bEndpointAddress, d->bInterval, n);
262 endpoint->desc.bInterval = n;
265 /* Some buggy low-speed devices have Bulk endpoints, which is
266 * explicitly forbidden by the USB spec. In an attempt to make
267 * them usable, we will try treating them as Interrupt endpoints.
269 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
270 usb_endpoint_xfer_bulk(d)) {
271 dev_warn(ddev, "config %d interface %d altsetting %d "
272 "endpoint 0x%X is Bulk; changing to Interrupt\n",
273 cfgno, inum, asnum, d->bEndpointAddress);
274 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
275 endpoint->desc.bInterval = 1;
276 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
277 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
281 * Some buggy high speed devices have bulk endpoints using
282 * maxpacket sizes other than 512. High speed HCDs may not
283 * be able to handle that particular bug, so let's warn...
285 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
286 && usb_endpoint_xfer_bulk(d)) {
289 maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
291 dev_warn(ddev, "config %d interface %d altsetting %d "
292 "bulk endpoint 0x%X has invalid maxpacket %d\n",
293 cfgno, inum, asnum, d->bEndpointAddress,
296 /* Allocate room for and parse any SS endpoint companion descriptors */
297 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER) {
298 endpoint->extra = buffer;
299 i = find_next_descriptor_more(buffer, size, USB_DT_SS_ENDPOINT_COMP,
300 USB_DT_ENDPOINT, USB_DT_INTERFACE, &n);
301 endpoint->extralen = i;
305 /* Allocate space for the SS endpoint companion descriptor */
306 endpoint->ss_ep_comp = kzalloc(sizeof(struct usb_host_ss_ep_comp),
308 if (!endpoint->ss_ep_comp)
311 /* Fill in some default values (may be overwritten later) */
312 endpoint->ss_ep_comp->desc.bLength = USB_DT_SS_EP_COMP_SIZE;
313 endpoint->ss_ep_comp->desc.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
314 endpoint->ss_ep_comp->desc.bMaxBurst = 0;
316 * Leave bmAttributes as zero, which will mean no streams for
317 * bulk, and isoc won't support multiple bursts of packets.
318 * With bursts of only one packet, and a Mult of 1, the max
319 * amount of data moved per endpoint service interval is one
322 if (usb_endpoint_xfer_isoc(&endpoint->desc) ||
323 usb_endpoint_xfer_int(&endpoint->desc))
324 endpoint->ss_ep_comp->desc.wBytesPerInterval =
325 endpoint->desc.wMaxPacketSize;
328 retval = usb_parse_ss_endpoint_companion(ddev, cfgno,
329 inum, asnum, endpoint, num_ep, buffer,
333 retval = buffer - buffer0;
336 dev_warn(ddev, "config %d interface %d altsetting %d "
337 "endpoint 0x%X has no "
338 "SuperSpeed companion descriptor\n",
339 cfgno, inum, asnum, d->bEndpointAddress);
340 retval = buffer - buffer0;
343 /* Skip over any Class Specific or Vendor Specific descriptors;
344 * find the next endpoint or interface descriptor */
345 endpoint->extra = buffer;
346 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
347 USB_DT_INTERFACE, &n);
348 endpoint->extralen = i;
349 retval = buffer - buffer0 + i;
352 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
353 n, plural(n), "endpoint");
356 skip_to_next_endpoint_or_interface_descriptor:
357 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
358 USB_DT_INTERFACE, NULL);
359 return buffer - buffer0 + i;
362 void usb_release_interface_cache(struct kref *ref)
364 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
367 for (j = 0; j < intfc->num_altsetting; j++) {
368 struct usb_host_interface *alt = &intfc->altsetting[j];
370 kfree(alt->endpoint);
376 static int usb_parse_interface(struct device *ddev, int cfgno,
377 struct usb_host_config *config, unsigned char *buffer, int size,
378 u8 inums[], u8 nalts[])
380 unsigned char *buffer0 = buffer;
381 struct usb_interface_descriptor *d;
383 struct usb_interface_cache *intfc;
384 struct usb_host_interface *alt;
387 int num_ep, num_ep_orig;
389 d = (struct usb_interface_descriptor *) buffer;
390 buffer += d->bLength;
393 if (d->bLength < USB_DT_INTERFACE_SIZE)
394 goto skip_to_next_interface_descriptor;
396 /* Which interface entry is this? */
398 inum = d->bInterfaceNumber;
399 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
400 if (inums[i] == inum) {
401 intfc = config->intf_cache[i];
405 if (!intfc || intfc->num_altsetting >= nalts[i])
406 goto skip_to_next_interface_descriptor;
408 /* Check for duplicate altsetting entries */
409 asnum = d->bAlternateSetting;
410 for ((i = 0, alt = &intfc->altsetting[0]);
411 i < intfc->num_altsetting;
413 if (alt->desc.bAlternateSetting == asnum) {
414 dev_warn(ddev, "Duplicate descriptor for config %d "
415 "interface %d altsetting %d, skipping\n",
417 goto skip_to_next_interface_descriptor;
421 ++intfc->num_altsetting;
422 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
424 /* Skip over any Class Specific or Vendor Specific descriptors;
425 * find the first endpoint or interface descriptor */
427 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
428 USB_DT_INTERFACE, &n);
431 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
432 n, plural(n), "interface");
436 /* Allocate space for the right(?) number of endpoints */
437 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
438 alt->desc.bNumEndpoints = 0; /* Use as a counter */
439 if (num_ep > USB_MAXENDPOINTS) {
440 dev_warn(ddev, "too many endpoints for config %d interface %d "
441 "altsetting %d: %d, using maximum allowed: %d\n",
442 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
443 num_ep = USB_MAXENDPOINTS;
447 /* Can't allocate 0 bytes */
448 len = sizeof(struct usb_host_endpoint) * num_ep;
449 alt->endpoint = kzalloc(len, GFP_KERNEL);
454 /* Parse all the endpoint descriptors */
457 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
460 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
461 num_ep, buffer, size);
470 if (n != num_ep_orig)
471 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
472 "endpoint descriptor%s, different from the interface "
473 "descriptor's value: %d\n",
474 cfgno, inum, asnum, n, plural(n), num_ep_orig);
475 return buffer - buffer0;
477 skip_to_next_interface_descriptor:
478 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
479 USB_DT_INTERFACE, NULL);
480 return buffer - buffer0 + i;
483 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
484 struct usb_host_config *config, unsigned char *buffer, int size)
486 struct device *ddev = &dev->dev;
487 unsigned char *buffer0 = buffer;
489 int nintf, nintf_orig;
491 struct usb_interface_cache *intfc;
492 unsigned char *buffer2;
494 struct usb_descriptor_header *header;
496 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
497 unsigned iad_num = 0;
499 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
500 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
501 config->desc.bLength < USB_DT_CONFIG_SIZE) {
502 dev_err(ddev, "invalid descriptor for config index %d: "
503 "type = 0x%X, length = %d\n", cfgidx,
504 config->desc.bDescriptorType, config->desc.bLength);
507 cfgno = config->desc.bConfigurationValue;
509 buffer += config->desc.bLength;
510 size -= config->desc.bLength;
512 nintf = nintf_orig = config->desc.bNumInterfaces;
513 if (nintf > USB_MAXINTERFACES) {
514 dev_warn(ddev, "config %d has too many interfaces: %d, "
515 "using maximum allowed: %d\n",
516 cfgno, nintf, USB_MAXINTERFACES);
517 nintf = USB_MAXINTERFACES;
520 /* Go through the descriptors, checking their length and counting the
521 * number of altsettings for each interface */
523 for ((buffer2 = buffer, size2 = size);
525 (buffer2 += header->bLength, size2 -= header->bLength)) {
527 if (size2 < sizeof(struct usb_descriptor_header)) {
528 dev_warn(ddev, "config %d descriptor has %d excess "
529 "byte%s, ignoring\n",
530 cfgno, size2, plural(size2));
534 header = (struct usb_descriptor_header *) buffer2;
535 if ((header->bLength > size2) || (header->bLength < 2)) {
536 dev_warn(ddev, "config %d has an invalid descriptor "
537 "of length %d, skipping remainder of the config\n",
538 cfgno, header->bLength);
542 if (header->bDescriptorType == USB_DT_INTERFACE) {
543 struct usb_interface_descriptor *d;
546 d = (struct usb_interface_descriptor *) header;
547 if (d->bLength < USB_DT_INTERFACE_SIZE) {
548 dev_warn(ddev, "config %d has an invalid "
549 "interface descriptor of length %d, "
550 "skipping\n", cfgno, d->bLength);
554 inum = d->bInterfaceNumber;
556 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
558 dev_warn(ddev, "config %d has more interface "
559 "descriptors, than it declares in "
560 "bNumInterfaces, ignoring interface "
561 "number: %d\n", cfgno, inum);
565 if (inum >= nintf_orig)
566 dev_warn(ddev, "config %d has an invalid "
567 "interface number: %d but max is %d\n",
568 cfgno, inum, nintf_orig - 1);
570 /* Have we already encountered this interface?
571 * Count its altsettings */
572 for (i = 0; i < n; ++i) {
573 if (inums[i] == inum)
579 } else if (n < USB_MAXINTERFACES) {
585 } else if (header->bDescriptorType ==
586 USB_DT_INTERFACE_ASSOCIATION) {
587 if (iad_num == USB_MAXIADS) {
588 dev_warn(ddev, "found more Interface "
589 "Association Descriptors "
590 "than allocated for in "
591 "configuration %d\n", cfgno);
593 config->intf_assoc[iad_num] =
594 (struct usb_interface_assoc_descriptor
599 } else if (header->bDescriptorType == USB_DT_DEVICE ||
600 header->bDescriptorType == USB_DT_CONFIG)
601 dev_warn(ddev, "config %d contains an unexpected "
602 "descriptor of type 0x%X, skipping\n",
603 cfgno, header->bDescriptorType);
605 } /* for ((buffer2 = buffer, size2 = size); ...) */
606 size = buffer2 - buffer;
607 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
610 dev_warn(ddev, "config %d has %d interface%s, different from "
611 "the descriptor's value: %d\n",
612 cfgno, n, plural(n), nintf_orig);
614 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
615 config->desc.bNumInterfaces = nintf = n;
617 /* Check for missing interface numbers */
618 for (i = 0; i < nintf; ++i) {
619 for (j = 0; j < nintf; ++j) {
624 dev_warn(ddev, "config %d has no interface number "
628 /* Allocate the usb_interface_caches and altsetting arrays */
629 for (i = 0; i < nintf; ++i) {
631 if (j > USB_MAXALTSETTING) {
632 dev_warn(ddev, "too many alternate settings for "
633 "config %d interface %d: %d, "
634 "using maximum allowed: %d\n",
635 cfgno, inums[i], j, USB_MAXALTSETTING);
636 nalts[i] = j = USB_MAXALTSETTING;
639 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
640 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
643 kref_init(&intfc->ref);
646 /* FIXME: parse the BOS descriptor */
648 /* Skip over any Class Specific or Vendor Specific descriptors;
649 * find the first interface descriptor */
650 config->extra = buffer;
651 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
652 USB_DT_INTERFACE, &n);
653 config->extralen = i;
655 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
656 n, plural(n), "configuration");
660 /* Parse all the interface/altsetting descriptors */
662 retval = usb_parse_interface(ddev, cfgno, config,
663 buffer, size, inums, nalts);
671 /* Check for missing altsettings */
672 for (i = 0; i < nintf; ++i) {
673 intfc = config->intf_cache[i];
674 for (j = 0; j < intfc->num_altsetting; ++j) {
675 for (n = 0; n < intfc->num_altsetting; ++n) {
676 if (intfc->altsetting[n].desc.
677 bAlternateSetting == j)
680 if (n >= intfc->num_altsetting)
681 dev_warn(ddev, "config %d interface %d has no "
682 "altsetting %d\n", cfgno, inums[i], j);
689 /* hub-only!! ... and only exported for reset/reinit path.
690 * otherwise used internally on disconnect/destroy path
692 void usb_destroy_configuration(struct usb_device *dev)
699 if (dev->rawdescriptors) {
700 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
701 kfree(dev->rawdescriptors[i]);
703 kfree(dev->rawdescriptors);
704 dev->rawdescriptors = NULL;
707 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
708 struct usb_host_config *cf = &dev->config[c];
711 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
712 if (cf->intf_cache[i])
713 kref_put(&cf->intf_cache[i]->ref,
714 usb_release_interface_cache);
723 * Get the USB config descriptors, cache and parse'em
725 * hub-only!! ... and only in reset path, or usb_new_device()
726 * (used by real hubs and virtual root hubs)
728 * NOTE: if this is a WUSB device and is not authorized, we skip the
729 * whole thing. A non-authorized USB device has no
732 int usb_get_configuration(struct usb_device *dev)
734 struct device *ddev = &dev->dev;
735 int ncfg = dev->descriptor.bNumConfigurations;
737 unsigned int cfgno, length;
738 unsigned char *buffer;
739 unsigned char *bigbuffer;
740 struct usb_config_descriptor *desc;
743 if (dev->authorized == 0) /* Not really an error */
744 goto out_not_authorized;
746 if (ncfg > USB_MAXCONFIG) {
747 dev_warn(ddev, "too many configurations: %d, "
748 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
749 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
753 dev_err(ddev, "no configurations\n");
757 length = ncfg * sizeof(struct usb_host_config);
758 dev->config = kzalloc(length, GFP_KERNEL);
762 length = ncfg * sizeof(char *);
763 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
764 if (!dev->rawdescriptors)
767 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
770 desc = (struct usb_config_descriptor *)buffer;
773 for (; cfgno < ncfg; cfgno++) {
774 /* We grab just the first descriptor so we know how long
775 * the whole configuration is */
776 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
777 buffer, USB_DT_CONFIG_SIZE);
779 dev_err(ddev, "unable to read config index %d "
780 "descriptor/%s: %d\n", cfgno, "start", result);
781 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
782 dev->descriptor.bNumConfigurations = cfgno;
784 } else if (result < 4) {
785 dev_err(ddev, "config index %d descriptor too short "
786 "(expected %i, got %i)\n", cfgno,
787 USB_DT_CONFIG_SIZE, result);
791 length = max((int) le16_to_cpu(desc->wTotalLength),
794 /* Now that we know the length, get the whole thing */
795 bigbuffer = kmalloc(length, GFP_KERNEL);
800 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
803 dev_err(ddev, "unable to read config index %d "
804 "descriptor/%s\n", cfgno, "all");
808 if (result < length) {
809 dev_warn(ddev, "config index %d descriptor too short "
810 "(expected %i, got %i)\n", cfgno, length, result);
814 dev->rawdescriptors[cfgno] = bigbuffer;
816 result = usb_parse_configuration(dev, cfgno,
817 &dev->config[cfgno], bigbuffer, length);
828 dev->descriptor.bNumConfigurations = cfgno;
830 if (result == -ENOMEM)
831 dev_err(ddev, "out of memory\n");