mtd: nand_base: use __func__ instead of typing names
[linux/fpc-iii.git] / drivers / usb / core / config.c
bloba16c538d0132958ab330b6993267663d45d4c4ef
1 #include <linux/usb.h>
2 #include <linux/usb/ch9.h>
3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 #include <linux/device.h>
7 #include <asm/byteorder.h>
8 #include "usb.h"
9 #include "hcd.h"
11 #define USB_MAXALTSETTING 128 /* Hard limit */
12 #define USB_MAXENDPOINTS 30 /* Hard limit */
14 #define USB_MAXCONFIG 8 /* Arbitrary limit */
17 static inline const char *plural(int n)
19 return (n == 1 ? "" : "s");
22 /* FIXME: this is a kludge */
23 static int find_next_descriptor_more(unsigned char *buffer, int size,
24 int dt1, int dt2, int dt3, int *num_skipped)
26 struct usb_descriptor_header *h;
27 int n = 0;
28 unsigned char *buffer0 = buffer;
30 /* Find the next descriptor of type dt1 or dt2 or dt3 */
31 while (size > 0) {
32 h = (struct usb_descriptor_header *) buffer;
33 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2 ||
34 h->bDescriptorType == dt3)
35 break;
36 buffer += h->bLength;
37 size -= h->bLength;
38 ++n;
41 /* Store the number of descriptors skipped and return the
42 * number of bytes skipped */
43 if (num_skipped)
44 *num_skipped = n;
45 return buffer - buffer0;
48 static int find_next_descriptor(unsigned char *buffer, int size,
49 int dt1, int dt2, int *num_skipped)
51 struct usb_descriptor_header *h;
52 int n = 0;
53 unsigned char *buffer0 = buffer;
55 /* Find the next descriptor of type dt1 or dt2 */
56 while (size > 0) {
57 h = (struct usb_descriptor_header *) buffer;
58 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
59 break;
60 buffer += h->bLength;
61 size -= h->bLength;
62 ++n;
65 /* Store the number of descriptors skipped and return the
66 * number of bytes skipped */
67 if (num_skipped)
68 *num_skipped = n;
69 return buffer - buffer0;
72 static int usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
73 int inum, int asnum, struct usb_host_endpoint *ep,
74 int num_ep, unsigned char *buffer, int size)
76 unsigned char *buffer_start = buffer;
77 struct usb_ss_ep_comp_descriptor *desc;
78 int retval;
79 int num_skipped;
80 int max_tx;
81 int i;
83 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
84 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) {
85 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
86 " interface %d altsetting %d ep %d: "
87 "using minimum values\n",
88 cfgno, inum, asnum, ep->desc.bEndpointAddress);
90 * The next descriptor is for an Endpoint or Interface,
91 * no extra descriptors to copy into the companion structure,
92 * and we didn't eat up any of the buffer.
94 return 0;
96 memcpy(&ep->ss_ep_comp->desc, desc, USB_DT_SS_EP_COMP_SIZE);
97 desc = &ep->ss_ep_comp->desc;
98 buffer += desc->bLength;
99 size -= desc->bLength;
101 /* Eat up the other descriptors we don't care about */
102 ep->ss_ep_comp->extra = buffer;
103 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
104 USB_DT_INTERFACE, &num_skipped);
105 ep->ss_ep_comp->extralen = i;
106 buffer += i;
107 size -= i;
108 retval = buffer - buffer_start + i;
109 if (num_skipped > 0)
110 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
111 num_skipped, plural(num_skipped),
112 "SuperSpeed endpoint companion");
114 /* Check the various values */
115 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
116 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
117 "config %d interface %d altsetting %d ep %d: "
118 "setting to zero\n", desc->bMaxBurst,
119 cfgno, inum, asnum, ep->desc.bEndpointAddress);
120 desc->bMaxBurst = 0;
122 if (desc->bMaxBurst > 15) {
123 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
124 "config %d interface %d altsetting %d ep %d: "
125 "setting to 15\n", desc->bMaxBurst,
126 cfgno, inum, asnum, ep->desc.bEndpointAddress);
127 desc->bMaxBurst = 15;
129 if ((usb_endpoint_xfer_control(&ep->desc) || usb_endpoint_xfer_int(&ep->desc))
130 && desc->bmAttributes != 0) {
131 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
132 "config %d interface %d altsetting %d ep %d: "
133 "setting to zero\n",
134 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
135 desc->bmAttributes,
136 cfgno, inum, asnum, ep->desc.bEndpointAddress);
137 desc->bmAttributes = 0;
139 if (usb_endpoint_xfer_bulk(&ep->desc) && desc->bmAttributes > 16) {
140 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
141 "config %d interface %d altsetting %d ep %d: "
142 "setting to max\n",
143 cfgno, inum, asnum, ep->desc.bEndpointAddress);
144 desc->bmAttributes = 16;
146 if (usb_endpoint_xfer_isoc(&ep->desc) && desc->bmAttributes > 2) {
147 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
148 "config %d interface %d altsetting %d ep %d: "
149 "setting to 3\n", desc->bmAttributes + 1,
150 cfgno, inum, asnum, ep->desc.bEndpointAddress);
151 desc->bmAttributes = 2;
153 if (usb_endpoint_xfer_isoc(&ep->desc)) {
154 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1) *
155 (desc->bmAttributes + 1);
156 } else if (usb_endpoint_xfer_int(&ep->desc)) {
157 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1);
158 } else {
159 goto valid;
161 if (desc->wBytesPerInterval > max_tx) {
162 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
163 "config %d interface %d altsetting %d ep %d: "
164 "setting to %d\n",
165 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
166 desc->wBytesPerInterval,
167 cfgno, inum, asnum, ep->desc.bEndpointAddress,
168 max_tx);
169 desc->wBytesPerInterval = max_tx;
171 valid:
172 return retval;
175 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
176 int asnum, struct usb_host_interface *ifp, int num_ep,
177 unsigned char *buffer, int size)
179 unsigned char *buffer0 = buffer;
180 struct usb_endpoint_descriptor *d;
181 struct usb_host_endpoint *endpoint;
182 int n, i, j, retval;
184 d = (struct usb_endpoint_descriptor *) buffer;
185 buffer += d->bLength;
186 size -= d->bLength;
188 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
189 n = USB_DT_ENDPOINT_AUDIO_SIZE;
190 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
191 n = USB_DT_ENDPOINT_SIZE;
192 else {
193 dev_warn(ddev, "config %d interface %d altsetting %d has an "
194 "invalid endpoint descriptor of length %d, skipping\n",
195 cfgno, inum, asnum, d->bLength);
196 goto skip_to_next_endpoint_or_interface_descriptor;
199 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
200 if (i >= 16 || i == 0) {
201 dev_warn(ddev, "config %d interface %d altsetting %d has an "
202 "invalid endpoint with address 0x%X, skipping\n",
203 cfgno, inum, asnum, d->bEndpointAddress);
204 goto skip_to_next_endpoint_or_interface_descriptor;
207 /* Only store as many endpoints as we have room for */
208 if (ifp->desc.bNumEndpoints >= num_ep)
209 goto skip_to_next_endpoint_or_interface_descriptor;
211 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
212 ++ifp->desc.bNumEndpoints;
214 memcpy(&endpoint->desc, d, n);
215 INIT_LIST_HEAD(&endpoint->urb_list);
217 /* Fix up bInterval values outside the legal range. Use 32 ms if no
218 * proper value can be guessed. */
219 i = 0; /* i = min, j = max, n = default */
220 j = 255;
221 if (usb_endpoint_xfer_int(d)) {
222 i = 1;
223 switch (to_usb_device(ddev)->speed) {
224 case USB_SPEED_SUPER:
225 case USB_SPEED_HIGH:
226 /* Many device manufacturers are using full-speed
227 * bInterval values in high-speed interrupt endpoint
228 * descriptors. Try to fix those and fall back to a
229 * 32 ms default value otherwise. */
230 n = fls(d->bInterval*8);
231 if (n == 0)
232 n = 9; /* 32 ms = 2^(9-1) uframes */
233 j = 16;
234 break;
235 default: /* USB_SPEED_FULL or _LOW */
236 /* For low-speed, 10 ms is the official minimum.
237 * But some "overclocked" devices might want faster
238 * polling so we'll allow it. */
239 n = 32;
240 break;
242 } else if (usb_endpoint_xfer_isoc(d)) {
243 i = 1;
244 j = 16;
245 switch (to_usb_device(ddev)->speed) {
246 case USB_SPEED_HIGH:
247 n = 9; /* 32 ms = 2^(9-1) uframes */
248 break;
249 default: /* USB_SPEED_FULL */
250 n = 6; /* 32 ms = 2^(6-1) frames */
251 break;
254 if (d->bInterval < i || d->bInterval > j) {
255 dev_warn(ddev, "config %d interface %d altsetting %d "
256 "endpoint 0x%X has an invalid bInterval %d, "
257 "changing to %d\n",
258 cfgno, inum, asnum,
259 d->bEndpointAddress, d->bInterval, n);
260 endpoint->desc.bInterval = n;
263 /* Some buggy low-speed devices have Bulk endpoints, which is
264 * explicitly forbidden by the USB spec. In an attempt to make
265 * them usable, we will try treating them as Interrupt endpoints.
267 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
268 usb_endpoint_xfer_bulk(d)) {
269 dev_warn(ddev, "config %d interface %d altsetting %d "
270 "endpoint 0x%X is Bulk; changing to Interrupt\n",
271 cfgno, inum, asnum, d->bEndpointAddress);
272 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
273 endpoint->desc.bInterval = 1;
274 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
275 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
279 * Some buggy high speed devices have bulk endpoints using
280 * maxpacket sizes other than 512. High speed HCDs may not
281 * be able to handle that particular bug, so let's warn...
283 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
284 && usb_endpoint_xfer_bulk(d)) {
285 unsigned maxp;
287 maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
288 if (maxp != 512)
289 dev_warn(ddev, "config %d interface %d altsetting %d "
290 "bulk endpoint 0x%X has invalid maxpacket %d\n",
291 cfgno, inum, asnum, d->bEndpointAddress,
292 maxp);
294 /* Allocate room for and parse any SS endpoint companion descriptors */
295 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER) {
296 endpoint->extra = buffer;
297 i = find_next_descriptor_more(buffer, size, USB_DT_SS_ENDPOINT_COMP,
298 USB_DT_ENDPOINT, USB_DT_INTERFACE, &n);
299 endpoint->extralen = i;
300 buffer += i;
301 size -= i;
303 /* Allocate space for the SS endpoint companion descriptor */
304 endpoint->ss_ep_comp = kzalloc(sizeof(struct usb_host_ss_ep_comp),
305 GFP_KERNEL);
306 if (!endpoint->ss_ep_comp)
307 return -ENOMEM;
309 /* Fill in some default values (may be overwritten later) */
310 endpoint->ss_ep_comp->desc.bLength = USB_DT_SS_EP_COMP_SIZE;
311 endpoint->ss_ep_comp->desc.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
312 endpoint->ss_ep_comp->desc.bMaxBurst = 0;
314 * Leave bmAttributes as zero, which will mean no streams for
315 * bulk, and isoc won't support multiple bursts of packets.
316 * With bursts of only one packet, and a Mult of 1, the max
317 * amount of data moved per endpoint service interval is one
318 * packet.
320 if (usb_endpoint_xfer_isoc(&endpoint->desc) ||
321 usb_endpoint_xfer_int(&endpoint->desc))
322 endpoint->ss_ep_comp->desc.wBytesPerInterval =
323 endpoint->desc.wMaxPacketSize;
325 if (size > 0) {
326 retval = usb_parse_ss_endpoint_companion(ddev, cfgno,
327 inum, asnum, endpoint, num_ep, buffer,
328 size);
329 if (retval >= 0) {
330 buffer += retval;
331 retval = buffer - buffer0;
333 } else {
334 dev_warn(ddev, "config %d interface %d altsetting %d "
335 "endpoint 0x%X has no "
336 "SuperSpeed companion descriptor\n",
337 cfgno, inum, asnum, d->bEndpointAddress);
338 retval = buffer - buffer0;
340 } else {
341 /* Skip over any Class Specific or Vendor Specific descriptors;
342 * find the next endpoint or interface descriptor */
343 endpoint->extra = buffer;
344 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
345 USB_DT_INTERFACE, &n);
346 endpoint->extralen = i;
347 retval = buffer - buffer0 + i;
349 if (n > 0)
350 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
351 n, plural(n), "endpoint");
352 return retval;
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);
363 int j;
365 for (j = 0; j < intfc->num_altsetting; j++) {
366 struct usb_host_interface *alt = &intfc->altsetting[j];
368 kfree(alt->endpoint);
369 kfree(alt->string);
371 kfree(intfc);
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;
380 int inum, asnum;
381 struct usb_interface_cache *intfc;
382 struct usb_host_interface *alt;
383 int i, n;
384 int len, retval;
385 int num_ep, num_ep_orig;
387 d = (struct usb_interface_descriptor *) buffer;
388 buffer += d->bLength;
389 size -= d->bLength;
391 if (d->bLength < USB_DT_INTERFACE_SIZE)
392 goto skip_to_next_interface_descriptor;
394 /* Which interface entry is this? */
395 intfc = NULL;
396 inum = d->bInterfaceNumber;
397 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
398 if (inums[i] == inum) {
399 intfc = config->intf_cache[i];
400 break;
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;
410 (++i, ++alt)) {
411 if (alt->desc.bAlternateSetting == asnum) {
412 dev_warn(ddev, "Duplicate descriptor for config %d "
413 "interface %d altsetting %d, skipping\n",
414 cfgno, inum, asnum);
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 */
424 alt->extra = buffer;
425 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
426 USB_DT_INTERFACE, &n);
427 alt->extralen = i;
428 if (n > 0)
429 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
430 n, plural(n), "interface");
431 buffer += i;
432 size -= i;
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;
444 if (num_ep > 0) {
445 /* Can't allocate 0 bytes */
446 len = sizeof(struct usb_host_endpoint) * num_ep;
447 alt->endpoint = kzalloc(len, GFP_KERNEL);
448 if (!alt->endpoint)
449 return -ENOMEM;
452 /* Parse all the endpoint descriptors */
453 n = 0;
454 while (size > 0) {
455 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
456 == USB_DT_INTERFACE)
457 break;
458 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
459 num_ep, buffer, size);
460 if (retval < 0)
461 return retval;
462 ++n;
464 buffer += retval;
465 size -= retval;
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 device *ddev, int cfgidx,
482 struct usb_host_config *config, unsigned char *buffer, int size)
484 unsigned char *buffer0 = buffer;
485 int cfgno;
486 int nintf, nintf_orig;
487 int i, j, n;
488 struct usb_interface_cache *intfc;
489 unsigned char *buffer2;
490 int size2;
491 struct usb_descriptor_header *header;
492 int len, retval;
493 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
494 unsigned iad_num = 0;
496 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
497 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
498 config->desc.bLength < USB_DT_CONFIG_SIZE) {
499 dev_err(ddev, "invalid descriptor for config index %d: "
500 "type = 0x%X, length = %d\n", cfgidx,
501 config->desc.bDescriptorType, config->desc.bLength);
502 return -EINVAL;
504 cfgno = config->desc.bConfigurationValue;
506 buffer += config->desc.bLength;
507 size -= config->desc.bLength;
509 nintf = nintf_orig = config->desc.bNumInterfaces;
510 if (nintf > USB_MAXINTERFACES) {
511 dev_warn(ddev, "config %d has too many interfaces: %d, "
512 "using maximum allowed: %d\n",
513 cfgno, nintf, USB_MAXINTERFACES);
514 nintf = USB_MAXINTERFACES;
517 /* Go through the descriptors, checking their length and counting the
518 * number of altsettings for each interface */
519 n = 0;
520 for ((buffer2 = buffer, size2 = size);
521 size2 > 0;
522 (buffer2 += header->bLength, size2 -= header->bLength)) {
524 if (size2 < sizeof(struct usb_descriptor_header)) {
525 dev_warn(ddev, "config %d descriptor has %d excess "
526 "byte%s, ignoring\n",
527 cfgno, size2, plural(size2));
528 break;
531 header = (struct usb_descriptor_header *) buffer2;
532 if ((header->bLength > size2) || (header->bLength < 2)) {
533 dev_warn(ddev, "config %d has an invalid descriptor "
534 "of length %d, skipping remainder of the config\n",
535 cfgno, header->bLength);
536 break;
539 if (header->bDescriptorType == USB_DT_INTERFACE) {
540 struct usb_interface_descriptor *d;
541 int inum;
543 d = (struct usb_interface_descriptor *) header;
544 if (d->bLength < USB_DT_INTERFACE_SIZE) {
545 dev_warn(ddev, "config %d has an invalid "
546 "interface descriptor of length %d, "
547 "skipping\n", cfgno, d->bLength);
548 continue;
551 inum = d->bInterfaceNumber;
552 if (inum >= nintf_orig)
553 dev_warn(ddev, "config %d has an invalid "
554 "interface number: %d but max is %d\n",
555 cfgno, inum, nintf_orig - 1);
557 /* Have we already encountered this interface?
558 * Count its altsettings */
559 for (i = 0; i < n; ++i) {
560 if (inums[i] == inum)
561 break;
563 if (i < n) {
564 if (nalts[i] < 255)
565 ++nalts[i];
566 } else if (n < USB_MAXINTERFACES) {
567 inums[n] = inum;
568 nalts[n] = 1;
569 ++n;
572 } else if (header->bDescriptorType ==
573 USB_DT_INTERFACE_ASSOCIATION) {
574 if (iad_num == USB_MAXIADS) {
575 dev_warn(ddev, "found more Interface "
576 "Association Descriptors "
577 "than allocated for in "
578 "configuration %d\n", cfgno);
579 } else {
580 config->intf_assoc[iad_num] =
581 (struct usb_interface_assoc_descriptor
582 *)header;
583 iad_num++;
586 } else if (header->bDescriptorType == USB_DT_DEVICE ||
587 header->bDescriptorType == USB_DT_CONFIG)
588 dev_warn(ddev, "config %d contains an unexpected "
589 "descriptor of type 0x%X, skipping\n",
590 cfgno, header->bDescriptorType);
592 } /* for ((buffer2 = buffer, size2 = size); ...) */
593 size = buffer2 - buffer;
594 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
596 if (n != nintf)
597 dev_warn(ddev, "config %d has %d interface%s, different from "
598 "the descriptor's value: %d\n",
599 cfgno, n, plural(n), nintf_orig);
600 else if (n == 0)
601 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
602 config->desc.bNumInterfaces = nintf = n;
604 /* Check for missing interface numbers */
605 for (i = 0; i < nintf; ++i) {
606 for (j = 0; j < nintf; ++j) {
607 if (inums[j] == i)
608 break;
610 if (j >= nintf)
611 dev_warn(ddev, "config %d has no interface number "
612 "%d\n", cfgno, i);
615 /* Allocate the usb_interface_caches and altsetting arrays */
616 for (i = 0; i < nintf; ++i) {
617 j = nalts[i];
618 if (j > USB_MAXALTSETTING) {
619 dev_warn(ddev, "too many alternate settings for "
620 "config %d interface %d: %d, "
621 "using maximum allowed: %d\n",
622 cfgno, inums[i], j, USB_MAXALTSETTING);
623 nalts[i] = j = USB_MAXALTSETTING;
626 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
627 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
628 if (!intfc)
629 return -ENOMEM;
630 kref_init(&intfc->ref);
633 /* FIXME: parse the BOS descriptor */
635 /* Skip over any Class Specific or Vendor Specific descriptors;
636 * find the first interface descriptor */
637 config->extra = buffer;
638 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
639 USB_DT_INTERFACE, &n);
640 config->extralen = i;
641 if (n > 0)
642 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
643 n, plural(n), "configuration");
644 buffer += i;
645 size -= i;
647 /* Parse all the interface/altsetting descriptors */
648 while (size > 0) {
649 retval = usb_parse_interface(ddev, cfgno, config,
650 buffer, size, inums, nalts);
651 if (retval < 0)
652 return retval;
654 buffer += retval;
655 size -= retval;
658 /* Check for missing altsettings */
659 for (i = 0; i < nintf; ++i) {
660 intfc = config->intf_cache[i];
661 for (j = 0; j < intfc->num_altsetting; ++j) {
662 for (n = 0; n < intfc->num_altsetting; ++n) {
663 if (intfc->altsetting[n].desc.
664 bAlternateSetting == j)
665 break;
667 if (n >= intfc->num_altsetting)
668 dev_warn(ddev, "config %d interface %d has no "
669 "altsetting %d\n", cfgno, inums[i], j);
673 return 0;
676 /* hub-only!! ... and only exported for reset/reinit path.
677 * otherwise used internally on disconnect/destroy path
679 void usb_destroy_configuration(struct usb_device *dev)
681 int c, i;
683 if (!dev->config)
684 return;
686 if (dev->rawdescriptors) {
687 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
688 kfree(dev->rawdescriptors[i]);
690 kfree(dev->rawdescriptors);
691 dev->rawdescriptors = NULL;
694 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
695 struct usb_host_config *cf = &dev->config[c];
697 kfree(cf->string);
698 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
699 if (cf->intf_cache[i])
700 kref_put(&cf->intf_cache[i]->ref,
701 usb_release_interface_cache);
704 kfree(dev->config);
705 dev->config = NULL;
710 * Get the USB config descriptors, cache and parse'em
712 * hub-only!! ... and only in reset path, or usb_new_device()
713 * (used by real hubs and virtual root hubs)
715 * NOTE: if this is a WUSB device and is not authorized, we skip the
716 * whole thing. A non-authorized USB device has no
717 * configurations.
719 int usb_get_configuration(struct usb_device *dev)
721 struct device *ddev = &dev->dev;
722 int ncfg = dev->descriptor.bNumConfigurations;
723 int result = 0;
724 unsigned int cfgno, length;
725 unsigned char *buffer;
726 unsigned char *bigbuffer;
727 struct usb_config_descriptor *desc;
729 cfgno = 0;
730 if (dev->authorized == 0) /* Not really an error */
731 goto out_not_authorized;
732 result = -ENOMEM;
733 if (ncfg > USB_MAXCONFIG) {
734 dev_warn(ddev, "too many configurations: %d, "
735 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
736 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
739 if (ncfg < 1) {
740 dev_err(ddev, "no configurations\n");
741 return -EINVAL;
744 length = ncfg * sizeof(struct usb_host_config);
745 dev->config = kzalloc(length, GFP_KERNEL);
746 if (!dev->config)
747 goto err2;
749 length = ncfg * sizeof(char *);
750 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
751 if (!dev->rawdescriptors)
752 goto err2;
754 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
755 if (!buffer)
756 goto err2;
757 desc = (struct usb_config_descriptor *)buffer;
759 result = 0;
760 for (; cfgno < ncfg; cfgno++) {
761 /* We grab just the first descriptor so we know how long
762 * the whole configuration is */
763 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
764 buffer, USB_DT_CONFIG_SIZE);
765 if (result < 0) {
766 dev_err(ddev, "unable to read config index %d "
767 "descriptor/%s: %d\n", cfgno, "start", result);
768 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
769 dev->descriptor.bNumConfigurations = cfgno;
770 break;
771 } else if (result < 4) {
772 dev_err(ddev, "config index %d descriptor too short "
773 "(expected %i, got %i)\n", cfgno,
774 USB_DT_CONFIG_SIZE, result);
775 result = -EINVAL;
776 goto err;
778 length = max((int) le16_to_cpu(desc->wTotalLength),
779 USB_DT_CONFIG_SIZE);
781 /* Now that we know the length, get the whole thing */
782 bigbuffer = kmalloc(length, GFP_KERNEL);
783 if (!bigbuffer) {
784 result = -ENOMEM;
785 goto err;
787 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
788 bigbuffer, length);
789 if (result < 0) {
790 dev_err(ddev, "unable to read config index %d "
791 "descriptor/%s\n", cfgno, "all");
792 kfree(bigbuffer);
793 goto err;
795 if (result < length) {
796 dev_warn(ddev, "config index %d descriptor too short "
797 "(expected %i, got %i)\n", cfgno, length, result);
798 length = result;
801 dev->rawdescriptors[cfgno] = bigbuffer;
803 result = usb_parse_configuration(&dev->dev, cfgno,
804 &dev->config[cfgno], bigbuffer, length);
805 if (result < 0) {
806 ++cfgno;
807 goto err;
810 result = 0;
812 err:
813 kfree(buffer);
814 out_not_authorized:
815 dev->descriptor.bNumConfigurations = cfgno;
816 err2:
817 if (result == -ENOMEM)
818 dev_err(ddev, "out of memory\n");
819 return result;