xref: /openbmc/linux/drivers/usb/core/config.c (revision e8e0929d)
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"
10 
11 #define USB_MAXALTSETTING		128	/* Hard limit */
12 #define USB_MAXENDPOINTS		30	/* Hard limit */
13 
14 #define USB_MAXCONFIG			8	/* Arbitrary limit */
15 
16 
17 static inline const char *plural(int n)
18 {
19 	return (n == 1 ? "" : "s");
20 }
21 
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)
25 {
26 	struct usb_descriptor_header *h;
27 	int n = 0;
28 	unsigned char *buffer0 = buffer;
29 
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;
39 	}
40 
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;
46 }
47 
48 static int find_next_descriptor(unsigned char *buffer, int size,
49     int dt1, int dt2, int *num_skipped)
50 {
51 	struct usb_descriptor_header *h;
52 	int n = 0;
53 	unsigned char *buffer0 = buffer;
54 
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;
63 	}
64 
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;
70 }
71 
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)
75 {
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;
82 
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);
89 		/*
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.
93 		 */
94 		return 0;
95 	}
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;
100 
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;
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");
113 
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;
121 	}
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;
128 	}
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;
138 	}
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;
145 	}
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;
152 	}
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;
160 	}
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;
170 	}
171 valid:
172 	return retval;
173 }
174 
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)
178 {
179 	unsigned char *buffer0 = buffer;
180 	struct usb_endpoint_descriptor *d;
181 	struct usb_host_endpoint *endpoint;
182 	int n, i, j, retval;
183 
184 	d = (struct usb_endpoint_descriptor *) buffer;
185 	buffer += d->bLength;
186 	size -= d->bLength;
187 
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;
197 	}
198 
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;
205 	}
206 
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;
210 
211 	endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
212 	++ifp->desc.bNumEndpoints;
213 
214 	memcpy(&endpoint->desc, d, n);
215 	INIT_LIST_HEAD(&endpoint->urb_list);
216 
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;
241 		}
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;
252 		}
253 	}
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;
261 	}
262 
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.
266 	 */
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);
276 	}
277 
278 	/*
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...
282 	 */
283 	if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
284 			&& usb_endpoint_xfer_bulk(d)) {
285 		unsigned maxp;
286 
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);
293 	}
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;
302 
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;
308 
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;
313 		/*
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.
319 		 */
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;
324 
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;
332 			}
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;
339 		}
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;
348 	}
349 	if (n > 0)
350 		dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
351 		    n, plural(n), "endpoint");
352 	return retval;
353 
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;
358 }
359 
360 void usb_release_interface_cache(struct kref *ref)
361 {
362 	struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
363 	int j;
364 
365 	for (j = 0; j < intfc->num_altsetting; j++) {
366 		struct usb_host_interface *alt = &intfc->altsetting[j];
367 
368 		kfree(alt->endpoint);
369 		kfree(alt->string);
370 	}
371 	kfree(intfc);
372 }
373 
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[])
377 {
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;
386 
387 	d = (struct usb_interface_descriptor *) buffer;
388 	buffer += d->bLength;
389 	size -= d->bLength;
390 
391 	if (d->bLength < USB_DT_INTERFACE_SIZE)
392 		goto skip_to_next_interface_descriptor;
393 
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;
401 		}
402 	}
403 	if (!intfc || intfc->num_altsetting >= nalts[i])
404 		goto skip_to_next_interface_descriptor;
405 
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;
416 		}
417 	}
418 
419 	++intfc->num_altsetting;
420 	memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
421 
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;
433 
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;
442 	}
443 
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;
450 	}
451 
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;
463 
464 		buffer += retval;
465 		size -= retval;
466 	}
467 
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;
474 
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;
479 }
480 
481 static int usb_parse_configuration(struct device *ddev, int cfgidx,
482     struct usb_host_config *config, unsigned char *buffer, int size)
483 {
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;
495 
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;
503 	}
504 	cfgno = config->desc.bConfigurationValue;
505 
506 	buffer += config->desc.bLength;
507 	size -= config->desc.bLength;
508 
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;
515 	}
516 
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)) {
523 
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;
529 		}
530 
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;
537 		}
538 
539 		if (header->bDescriptorType == USB_DT_INTERFACE) {
540 			struct usb_interface_descriptor *d;
541 			int inum;
542 
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;
549 			}
550 
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);
556 
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;
562 			}
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;
570 			}
571 
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++;
584 			}
585 
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);
591 
592 	}	/* for ((buffer2 = buffer, size2 = size); ...) */
593 	size = buffer2 - buffer;
594 	config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
595 
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;
603 
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;
609 		}
610 		if (j >= nintf)
611 			dev_warn(ddev, "config %d has no interface number "
612 			    "%d\n", cfgno, i);
613 	}
614 
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;
624 		}
625 
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);
631 	}
632 
633 	/* FIXME: parse the BOS descriptor */
634 
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;
646 
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;
653 
654 		buffer += retval;
655 		size -= retval;
656 	}
657 
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;
666 			}
667 			if (n >= intfc->num_altsetting)
668 				dev_warn(ddev, "config %d interface %d has no "
669 				    "altsetting %d\n", cfgno, inums[i], j);
670 		}
671 	}
672 
673 	return 0;
674 }
675 
676 /* hub-only!! ... and only exported for reset/reinit path.
677  * otherwise used internally on disconnect/destroy path
678  */
679 void usb_destroy_configuration(struct usb_device *dev)
680 {
681 	int c, i;
682 
683 	if (!dev->config)
684 		return;
685 
686 	if (dev->rawdescriptors) {
687 		for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
688 			kfree(dev->rawdescriptors[i]);
689 
690 		kfree(dev->rawdescriptors);
691 		dev->rawdescriptors = NULL;
692 	}
693 
694 	for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
695 		struct usb_host_config *cf = &dev->config[c];
696 
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);
702 		}
703 	}
704 	kfree(dev->config);
705 	dev->config = NULL;
706 }
707 
708 
709 /*
710  * Get the USB config descriptors, cache and parse'em
711  *
712  * hub-only!! ... and only in reset path, or usb_new_device()
713  * (used by real hubs and virtual root hubs)
714  *
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.
718  */
719 int usb_get_configuration(struct usb_device *dev)
720 {
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;
728 
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;
737 	}
738 
739 	if (ncfg < 1) {
740 		dev_err(ddev, "no configurations\n");
741 		return -EINVAL;
742 	}
743 
744 	length = ncfg * sizeof(struct usb_host_config);
745 	dev->config = kzalloc(length, GFP_KERNEL);
746 	if (!dev->config)
747 		goto err2;
748 
749 	length = ncfg * sizeof(char *);
750 	dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
751 	if (!dev->rawdescriptors)
752 		goto err2;
753 
754 	buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
755 	if (!buffer)
756 		goto err2;
757 	desc = (struct usb_config_descriptor *)buffer;
758 
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;
777 		}
778 		length = max((int) le16_to_cpu(desc->wTotalLength),
779 		    USB_DT_CONFIG_SIZE);
780 
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;
786 		}
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;
794 		}
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;
799 		}
800 
801 		dev->rawdescriptors[cfgno] = bigbuffer;
802 
803 		result = usb_parse_configuration(&dev->dev, cfgno,
804 		    &dev->config[cfgno], bigbuffer, length);
805 		if (result < 0) {
806 			++cfgno;
807 			goto err;
808 		}
809 	}
810 	result = 0;
811 
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;
820 }
821