2 #include <linux/usb/ch9.h>
3 #include <linux/usb/hcd.h>
4 #include <linux/module.h>
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/device.h>
8 #include <asm/byteorder.h>
12 #define USB_MAXALTSETTING 128 /* Hard limit */
13 #define USB_MAXENDPOINTS 30 /* Hard limit */
15 #define USB_MAXCONFIG 8 /* Arbitrary limit */
18 static inline const char *plural(int n)
20 return (n == 1 ? "" : "s");
23 /* FIXME: this is a kludge */
24 static int find_next_descriptor_more(unsigned char *buffer, int size,
25 int dt1, int dt2, int dt3, int *num_skipped)
27 struct usb_descriptor_header *h;
29 unsigned char *buffer0 = buffer;
31 /* Find the next descriptor of type dt1 or dt2 or dt3 */
33 h = (struct usb_descriptor_header *) buffer;
34 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2 ||
35 h->bDescriptorType == dt3)
42 /* Store the number of descriptors skipped and return the
43 * number of bytes skipped */
46 return buffer - buffer0;
49 static int find_next_descriptor(unsigned char *buffer, int size,
50 int dt1, int dt2, int *num_skipped)
52 struct usb_descriptor_header *h;
54 unsigned char *buffer0 = buffer;
56 /* Find the next descriptor of type dt1 or dt2 */
58 h = (struct usb_descriptor_header *) buffer;
59 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
66 /* Store the number of descriptors skipped and return the
67 * number of bytes skipped */
70 return buffer - buffer0;
73 static int usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
74 int inum, int asnum, struct usb_host_endpoint *ep,
75 int num_ep, unsigned char *buffer, int size)
77 unsigned char *buffer_start = buffer;
78 struct usb_ss_ep_comp_descriptor *desc;
84 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
85 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) {
86 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
87 " interface %d altsetting %d ep %d: "
88 "using minimum values\n",
89 cfgno, inum, asnum, ep->desc.bEndpointAddress);
91 * The next descriptor is for an Endpoint or Interface,
92 * no extra descriptors to copy into the companion structure,
93 * and we didn't eat up any of the buffer.
97 memcpy(&ep->ss_ep_comp->desc, desc, USB_DT_SS_EP_COMP_SIZE);
98 desc = &ep->ss_ep_comp->desc;
99 buffer += desc->bLength;
100 size -= desc->bLength;
102 /* Eat up the other descriptors we don't care about */
103 ep->ss_ep_comp->extra = buffer;
104 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
105 USB_DT_INTERFACE, &num_skipped);
106 ep->ss_ep_comp->extralen = i;
109 retval = buffer - buffer_start;
111 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
112 num_skipped, plural(num_skipped),
113 "SuperSpeed endpoint companion");
115 /* Check the various values */
116 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
117 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
118 "config %d interface %d altsetting %d ep %d: "
119 "setting to zero\n", desc->bMaxBurst,
120 cfgno, inum, asnum, ep->desc.bEndpointAddress);
123 if (desc->bMaxBurst > 15) {
124 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
125 "config %d interface %d altsetting %d ep %d: "
126 "setting to 15\n", desc->bMaxBurst,
127 cfgno, inum, asnum, ep->desc.bEndpointAddress);
128 desc->bMaxBurst = 15;
130 if ((usb_endpoint_xfer_control(&ep->desc) || usb_endpoint_xfer_int(&ep->desc))
131 && desc->bmAttributes != 0) {
132 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
133 "config %d interface %d altsetting %d ep %d: "
135 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
137 cfgno, inum, asnum, ep->desc.bEndpointAddress);
138 desc->bmAttributes = 0;
140 if (usb_endpoint_xfer_bulk(&ep->desc) && desc->bmAttributes > 16) {
141 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
142 "config %d interface %d altsetting %d ep %d: "
144 cfgno, inum, asnum, ep->desc.bEndpointAddress);
145 desc->bmAttributes = 16;
147 if (usb_endpoint_xfer_isoc(&ep->desc) && desc->bmAttributes > 2) {
148 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
149 "config %d interface %d altsetting %d ep %d: "
150 "setting to 3\n", desc->bmAttributes + 1,
151 cfgno, inum, asnum, ep->desc.bEndpointAddress);
152 desc->bmAttributes = 2;
154 if (usb_endpoint_xfer_isoc(&ep->desc)) {
155 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1) *
156 (desc->bmAttributes + 1);
157 } else if (usb_endpoint_xfer_int(&ep->desc)) {
158 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1);
162 if (desc->wBytesPerInterval > max_tx) {
163 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
164 "config %d interface %d altsetting %d ep %d: "
166 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
167 desc->wBytesPerInterval,
168 cfgno, inum, asnum, ep->desc.bEndpointAddress,
170 desc->wBytesPerInterval = max_tx;
176 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
177 int asnum, struct usb_host_interface *ifp, int num_ep,
178 unsigned char *buffer, int size)
180 unsigned char *buffer0 = buffer;
181 struct usb_endpoint_descriptor *d;
182 struct usb_host_endpoint *endpoint;
185 d = (struct usb_endpoint_descriptor *) buffer;
186 buffer += d->bLength;
189 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
190 n = USB_DT_ENDPOINT_AUDIO_SIZE;
191 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
192 n = USB_DT_ENDPOINT_SIZE;
194 dev_warn(ddev, "config %d interface %d altsetting %d has an "
195 "invalid endpoint descriptor of length %d, skipping\n",
196 cfgno, inum, asnum, d->bLength);
197 goto skip_to_next_endpoint_or_interface_descriptor;
200 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
201 if (i >= 16 || i == 0) {
202 dev_warn(ddev, "config %d interface %d altsetting %d has an "
203 "invalid endpoint with address 0x%X, skipping\n",
204 cfgno, inum, asnum, d->bEndpointAddress);
205 goto skip_to_next_endpoint_or_interface_descriptor;
208 /* Only store as many endpoints as we have room for */
209 if (ifp->desc.bNumEndpoints >= num_ep)
210 goto skip_to_next_endpoint_or_interface_descriptor;
212 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
213 ++ifp->desc.bNumEndpoints;
215 memcpy(&endpoint->desc, d, n);
216 INIT_LIST_HEAD(&endpoint->urb_list);
218 /* Fix up bInterval values outside the legal range. Use 32 ms if no
219 * proper value can be guessed. */
220 i = 0; /* i = min, j = max, n = default */
222 if (usb_endpoint_xfer_int(d)) {
224 switch (to_usb_device(ddev)->speed) {
225 case USB_SPEED_SUPER:
227 /* Many device manufacturers are using full-speed
228 * bInterval values in high-speed interrupt endpoint
229 * descriptors. Try to fix those and fall back to a
230 * 32 ms default value otherwise. */
231 n = fls(d->bInterval*8);
233 n = 9; /* 32 ms = 2^(9-1) uframes */
236 default: /* USB_SPEED_FULL or _LOW */
237 /* For low-speed, 10 ms is the official minimum.
238 * But some "overclocked" devices might want faster
239 * polling so we'll allow it. */
243 } else if (usb_endpoint_xfer_isoc(d)) {
246 switch (to_usb_device(ddev)->speed) {
248 n = 9; /* 32 ms = 2^(9-1) uframes */
250 default: /* USB_SPEED_FULL */
251 n = 6; /* 32 ms = 2^(6-1) frames */
255 if (d->bInterval < i || d->bInterval > j) {
256 dev_warn(ddev, "config %d interface %d altsetting %d "
257 "endpoint 0x%X has an invalid bInterval %d, "
260 d->bEndpointAddress, d->bInterval, n);
261 endpoint->desc.bInterval = n;
264 /* Some buggy low-speed devices have Bulk endpoints, which is
265 * explicitly forbidden by the USB spec. In an attempt to make
266 * them usable, we will try treating them as Interrupt endpoints.
268 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
269 usb_endpoint_xfer_bulk(d)) {
270 dev_warn(ddev, "config %d interface %d altsetting %d "
271 "endpoint 0x%X is Bulk; changing to Interrupt\n",
272 cfgno, inum, asnum, d->bEndpointAddress);
273 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
274 endpoint->desc.bInterval = 1;
275 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
276 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
280 * Some buggy high speed devices have bulk endpoints using
281 * maxpacket sizes other than 512. High speed HCDs may not
282 * be able to handle that particular bug, so let's warn...
284 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
285 && usb_endpoint_xfer_bulk(d)) {
288 maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
290 dev_warn(ddev, "config %d interface %d altsetting %d "
291 "bulk endpoint 0x%X has invalid maxpacket %d\n",
292 cfgno, inum, asnum, d->bEndpointAddress,
295 /* Allocate room for and parse any SS endpoint companion descriptors */
296 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER) {
297 endpoint->extra = buffer;
298 i = find_next_descriptor_more(buffer, size, USB_DT_SS_ENDPOINT_COMP,
299 USB_DT_ENDPOINT, USB_DT_INTERFACE, &n);
300 endpoint->extralen = i;
304 /* Allocate space for the SS endpoint companion descriptor */
305 endpoint->ss_ep_comp = kzalloc(sizeof(struct usb_host_ss_ep_comp),
307 if (!endpoint->ss_ep_comp)
310 /* Fill in some default values (may be overwritten later) */
311 endpoint->ss_ep_comp->desc.bLength = USB_DT_SS_EP_COMP_SIZE;
312 endpoint->ss_ep_comp->desc.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
313 endpoint->ss_ep_comp->desc.bMaxBurst = 0;
315 * Leave bmAttributes as zero, which will mean no streams for
316 * bulk, and isoc won't support multiple bursts of packets.
317 * With bursts of only one packet, and a Mult of 1, the max
318 * amount of data moved per endpoint service interval is one
321 if (usb_endpoint_xfer_isoc(&endpoint->desc) ||
322 usb_endpoint_xfer_int(&endpoint->desc))
323 endpoint->ss_ep_comp->desc.wBytesPerInterval =
324 endpoint->desc.wMaxPacketSize;
327 retval = usb_parse_ss_endpoint_companion(ddev, cfgno,
328 inum, asnum, endpoint, num_ep, buffer,
332 retval = buffer - buffer0;
335 dev_warn(ddev, "config %d interface %d altsetting %d "
336 "endpoint 0x%X has no "
337 "SuperSpeed companion descriptor\n",
338 cfgno, inum, asnum, d->bEndpointAddress);
339 retval = buffer - buffer0;
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;
351 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
352 n, plural(n), "endpoint");
355 skip_to_next_endpoint_or_interface_descriptor:
356 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
357 USB_DT_INTERFACE, NULL);
358 return buffer - buffer0 + i;
361 void usb_release_interface_cache(struct kref *ref)
363 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
366 for (j = 0; j < intfc->num_altsetting; j++) {
367 struct usb_host_interface *alt = &intfc->altsetting[j];
369 kfree(alt->endpoint);
375 static int usb_parse_interface(struct device *ddev, int cfgno,
376 struct usb_host_config *config, unsigned char *buffer, int size,
377 u8 inums[], u8 nalts[])
379 unsigned char *buffer0 = buffer;
380 struct usb_interface_descriptor *d;
382 struct usb_interface_cache *intfc;
383 struct usb_host_interface *alt;
386 int num_ep, num_ep_orig;
388 d = (struct usb_interface_descriptor *) buffer;
389 buffer += d->bLength;
392 if (d->bLength < USB_DT_INTERFACE_SIZE)
393 goto skip_to_next_interface_descriptor;
395 /* Which interface entry is this? */
397 inum = d->bInterfaceNumber;
398 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
399 if (inums[i] == inum) {
400 intfc = config->intf_cache[i];
404 if (!intfc || intfc->num_altsetting >= nalts[i])
405 goto skip_to_next_interface_descriptor;
407 /* Check for duplicate altsetting entries */
408 asnum = d->bAlternateSetting;
409 for ((i = 0, alt = &intfc->altsetting[0]);
410 i < intfc->num_altsetting;
412 if (alt->desc.bAlternateSetting == asnum) {
413 dev_warn(ddev, "Duplicate descriptor for config %d "
414 "interface %d altsetting %d, skipping\n",
416 goto skip_to_next_interface_descriptor;
420 ++intfc->num_altsetting;
421 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
423 /* Skip over any Class Specific or Vendor Specific descriptors;
424 * find the first endpoint or interface descriptor */
426 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
427 USB_DT_INTERFACE, &n);
430 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
431 n, plural(n), "interface");
435 /* Allocate space for the right(?) number of endpoints */
436 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
437 alt->desc.bNumEndpoints = 0; /* Use as a counter */
438 if (num_ep > USB_MAXENDPOINTS) {
439 dev_warn(ddev, "too many endpoints for config %d interface %d "
440 "altsetting %d: %d, using maximum allowed: %d\n",
441 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
442 num_ep = USB_MAXENDPOINTS;
446 /* Can't allocate 0 bytes */
447 len = sizeof(struct usb_host_endpoint) * num_ep;
448 alt->endpoint = kzalloc(len, GFP_KERNEL);
453 /* Parse all the endpoint descriptors */
456 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
459 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
460 num_ep, buffer, size);
469 if (n != num_ep_orig)
470 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
471 "endpoint descriptor%s, different from the interface "
472 "descriptor's value: %d\n",
473 cfgno, inum, asnum, n, plural(n), num_ep_orig);
474 return buffer - buffer0;
476 skip_to_next_interface_descriptor:
477 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
478 USB_DT_INTERFACE, NULL);
479 return buffer - buffer0 + i;
482 static int usb_parse_configuration(struct device *ddev, int cfgidx,
483 struct usb_host_config *config, unsigned char *buffer, int size)
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 dev_err(ddev, "invalid descriptor for config index %d: "
501 "type = 0x%X, length = %d\n", cfgidx,
502 config->desc.bDescriptorType, config->desc.bLength);
505 cfgno = config->desc.bConfigurationValue;
507 buffer += config->desc.bLength;
508 size -= config->desc.bLength;
510 nintf = nintf_orig = config->desc.bNumInterfaces;
511 if (nintf > USB_MAXINTERFACES) {
512 dev_warn(ddev, "config %d has too many interfaces: %d, "
513 "using maximum allowed: %d\n",
514 cfgno, nintf, USB_MAXINTERFACES);
515 nintf = USB_MAXINTERFACES;
518 /* Go through the descriptors, checking their length and counting the
519 * number of altsettings for each interface */
521 for ((buffer2 = buffer, size2 = size);
523 (buffer2 += header->bLength, size2 -= header->bLength)) {
525 if (size2 < sizeof(struct usb_descriptor_header)) {
526 dev_warn(ddev, "config %d descriptor has %d excess "
527 "byte%s, ignoring\n",
528 cfgno, size2, plural(size2));
532 header = (struct usb_descriptor_header *) buffer2;
533 if ((header->bLength > size2) || (header->bLength < 2)) {
534 dev_warn(ddev, "config %d has an invalid descriptor "
535 "of length %d, skipping remainder of the config\n",
536 cfgno, header->bLength);
540 if (header->bDescriptorType == USB_DT_INTERFACE) {
541 struct usb_interface_descriptor *d;
544 d = (struct usb_interface_descriptor *) header;
545 if (d->bLength < USB_DT_INTERFACE_SIZE) {
546 dev_warn(ddev, "config %d has an invalid "
547 "interface descriptor of length %d, "
548 "skipping\n", cfgno, d->bLength);
552 inum = d->bInterfaceNumber;
553 if (inum >= nintf_orig)
554 dev_warn(ddev, "config %d has an invalid "
555 "interface number: %d but max is %d\n",
556 cfgno, inum, nintf_orig - 1);
558 /* Have we already encountered this interface?
559 * Count its altsettings */
560 for (i = 0; i < n; ++i) {
561 if (inums[i] == inum)
567 } else if (n < USB_MAXINTERFACES) {
573 } else if (header->bDescriptorType ==
574 USB_DT_INTERFACE_ASSOCIATION) {
575 if (iad_num == USB_MAXIADS) {
576 dev_warn(ddev, "found more Interface "
577 "Association Descriptors "
578 "than allocated for in "
579 "configuration %d\n", cfgno);
581 config->intf_assoc[iad_num] =
582 (struct usb_interface_assoc_descriptor
587 } else if (header->bDescriptorType == USB_DT_DEVICE ||
588 header->bDescriptorType == USB_DT_CONFIG)
589 dev_warn(ddev, "config %d contains an unexpected "
590 "descriptor of type 0x%X, skipping\n",
591 cfgno, header->bDescriptorType);
593 } /* for ((buffer2 = buffer, size2 = size); ...) */
594 size = buffer2 - buffer;
595 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
598 dev_warn(ddev, "config %d has %d interface%s, different from "
599 "the descriptor's value: %d\n",
600 cfgno, n, plural(n), nintf_orig);
602 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
603 config->desc.bNumInterfaces = nintf = n;
605 /* Check for missing interface numbers */
606 for (i = 0; i < nintf; ++i) {
607 for (j = 0; j < nintf; ++j) {
612 dev_warn(ddev, "config %d has no interface number "
616 /* Allocate the usb_interface_caches and altsetting arrays */
617 for (i = 0; i < nintf; ++i) {
619 if (j > USB_MAXALTSETTING) {
620 dev_warn(ddev, "too many alternate settings for "
621 "config %d interface %d: %d, "
622 "using maximum allowed: %d\n",
623 cfgno, inums[i], j, USB_MAXALTSETTING);
624 nalts[i] = j = USB_MAXALTSETTING;
627 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
628 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
631 kref_init(&intfc->ref);
634 /* FIXME: parse the BOS descriptor */
636 /* Skip over any Class Specific or Vendor Specific descriptors;
637 * find the first interface descriptor */
638 config->extra = buffer;
639 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
640 USB_DT_INTERFACE, &n);
641 config->extralen = i;
643 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
644 n, plural(n), "configuration");
648 /* Parse all the interface/altsetting descriptors */
650 retval = usb_parse_interface(ddev, cfgno, config,
651 buffer, size, inums, nalts);
659 /* Check for missing altsettings */
660 for (i = 0; i < nintf; ++i) {
661 intfc = config->intf_cache[i];
662 for (j = 0; j < intfc->num_altsetting; ++j) {
663 for (n = 0; n < intfc->num_altsetting; ++n) {
664 if (intfc->altsetting[n].desc.
665 bAlternateSetting == j)
668 if (n >= intfc->num_altsetting)
669 dev_warn(ddev, "config %d interface %d has no "
670 "altsetting %d\n", cfgno, inums[i], j);
677 /* hub-only!! ... and only exported for reset/reinit path.
678 * otherwise used internally on disconnect/destroy path
680 void usb_destroy_configuration(struct usb_device *dev)
687 if (dev->rawdescriptors) {
688 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
689 kfree(dev->rawdescriptors[i]);
691 kfree(dev->rawdescriptors);
692 dev->rawdescriptors = NULL;
695 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
696 struct usb_host_config *cf = &dev->config[c];
699 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
700 if (cf->intf_cache[i])
701 kref_put(&cf->intf_cache[i]->ref,
702 usb_release_interface_cache);
711 * Get the USB config descriptors, cache and parse'em
713 * hub-only!! ... and only in reset path, or usb_new_device()
714 * (used by real hubs and virtual root hubs)
716 * NOTE: if this is a WUSB device and is not authorized, we skip the
717 * whole thing. A non-authorized USB device has no
720 int usb_get_configuration(struct usb_device *dev)
722 struct device *ddev = &dev->dev;
723 int ncfg = dev->descriptor.bNumConfigurations;
725 unsigned int cfgno, length;
726 unsigned char *buffer;
727 unsigned char *bigbuffer;
728 struct usb_config_descriptor *desc;
731 if (dev->authorized == 0) /* Not really an error */
732 goto out_not_authorized;
734 if (ncfg > USB_MAXCONFIG) {
735 dev_warn(ddev, "too many configurations: %d, "
736 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
737 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
741 dev_err(ddev, "no configurations\n");
745 length = ncfg * sizeof(struct usb_host_config);
746 dev->config = kzalloc(length, GFP_KERNEL);
750 length = ncfg * sizeof(char *);
751 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
752 if (!dev->rawdescriptors)
755 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
758 desc = (struct usb_config_descriptor *)buffer;
761 for (; cfgno < ncfg; cfgno++) {
762 /* We grab just the first descriptor so we know how long
763 * the whole configuration is */
764 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
765 buffer, USB_DT_CONFIG_SIZE);
767 dev_err(ddev, "unable to read config index %d "
768 "descriptor/%s: %d\n", cfgno, "start", result);
769 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
770 dev->descriptor.bNumConfigurations = cfgno;
772 } else if (result < 4) {
773 dev_err(ddev, "config index %d descriptor too short "
774 "(expected %i, got %i)\n", cfgno,
775 USB_DT_CONFIG_SIZE, result);
779 length = max((int) le16_to_cpu(desc->wTotalLength),
782 /* Now that we know the length, get the whole thing */
783 bigbuffer = kmalloc(length, GFP_KERNEL);
788 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
791 dev_err(ddev, "unable to read config index %d "
792 "descriptor/%s\n", cfgno, "all");
796 if (result < length) {
797 dev_warn(ddev, "config index %d descriptor too short "
798 "(expected %i, got %i)\n", cfgno, length, result);
802 dev->rawdescriptors[cfgno] = bigbuffer;
804 result = usb_parse_configuration(&dev->dev, cfgno,
805 &dev->config[cfgno], bigbuffer, length);
816 dev->descriptor.bNumConfigurations = cfgno;
818 if (result == -ENOMEM)
819 dev_err(ddev, "out of memory\n");