xref: /openbmc/linux/drivers/usb/core/config.c (revision c16352b5b35d8f619028ebf855ce42c1f99649e6)
1 #include <linux/usb.h>
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/slab.h>
7 #include <linux/device.h>
8 #include <asm/byteorder.h>
9 #include "usb.h"
10 
11 
12 #define USB_MAXALTSETTING		128	/* 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 static int find_next_descriptor(unsigned char *buffer, int size,
23     int dt1, int dt2, int *num_skipped)
24 {
25 	struct usb_descriptor_header *h;
26 	int n = 0;
27 	unsigned char *buffer0 = buffer;
28 
29 	/* Find the next descriptor of type dt1 or dt2 */
30 	while (size > 0) {
31 		h = (struct usb_descriptor_header *) buffer;
32 		if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
33 			break;
34 		buffer += h->bLength;
35 		size -= h->bLength;
36 		++n;
37 	}
38 
39 	/* Store the number of descriptors skipped and return the
40 	 * number of bytes skipped */
41 	if (num_skipped)
42 		*num_skipped = n;
43 	return buffer - buffer0;
44 }
45 
46 static void usb_parse_ssp_isoc_endpoint_companion(struct device *ddev,
47 		int cfgno, int inum, int asnum, struct usb_host_endpoint *ep,
48 		unsigned char *buffer, int size)
49 {
50 	struct usb_ssp_isoc_ep_comp_descriptor *desc;
51 
52 	/*
53 	 * The SuperSpeedPlus Isoc endpoint companion descriptor immediately
54 	 * follows the SuperSpeed Endpoint Companion descriptor
55 	 */
56 	desc = (struct usb_ssp_isoc_ep_comp_descriptor *) buffer;
57 	if (desc->bDescriptorType != USB_DT_SSP_ISOC_ENDPOINT_COMP ||
58 	    size < USB_DT_SSP_ISOC_EP_COMP_SIZE) {
59 		dev_warn(ddev, "Invalid SuperSpeedPlus isoc endpoint companion"
60 			 "for config %d interface %d altsetting %d ep %d.\n",
61 			 cfgno, inum, asnum, ep->desc.bEndpointAddress);
62 		return;
63 	}
64 	memcpy(&ep->ssp_isoc_ep_comp, desc, USB_DT_SSP_ISOC_EP_COMP_SIZE);
65 }
66 
67 static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
68 		int inum, int asnum, struct usb_host_endpoint *ep,
69 		unsigned char *buffer, int size)
70 {
71 	struct usb_ss_ep_comp_descriptor *desc;
72 	int max_tx;
73 
74 	/* The SuperSpeed endpoint companion descriptor is supposed to
75 	 * be the first thing immediately following the endpoint descriptor.
76 	 */
77 	desc = (struct usb_ss_ep_comp_descriptor *) buffer;
78 	buffer += desc->bLength;
79 	size -= desc->bLength;
80 
81 	if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
82 			size < USB_DT_SS_EP_COMP_SIZE) {
83 		dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
84 				" interface %d altsetting %d ep %d: "
85 				"using minimum values\n",
86 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
87 
88 		/* Fill in some default values.
89 		 * Leave bmAttributes as zero, which will mean no streams for
90 		 * bulk, and isoc won't support multiple bursts of packets.
91 		 * With bursts of only one packet, and a Mult of 1, the max
92 		 * amount of data moved per endpoint service interval is one
93 		 * packet.
94 		 */
95 		ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
96 		ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
97 		if (usb_endpoint_xfer_isoc(&ep->desc) ||
98 				usb_endpoint_xfer_int(&ep->desc))
99 			ep->ss_ep_comp.wBytesPerInterval =
100 					ep->desc.wMaxPacketSize;
101 		return;
102 	}
103 
104 	memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
105 
106 	/* Check the various values */
107 	if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
108 		dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
109 				"config %d interface %d altsetting %d ep %d: "
110 				"setting to zero\n", desc->bMaxBurst,
111 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
112 		ep->ss_ep_comp.bMaxBurst = 0;
113 	} else if (desc->bMaxBurst > 15) {
114 		dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
115 				"config %d interface %d altsetting %d ep %d: "
116 				"setting to 15\n", desc->bMaxBurst,
117 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
118 		ep->ss_ep_comp.bMaxBurst = 15;
119 	}
120 
121 	if ((usb_endpoint_xfer_control(&ep->desc) ||
122 			usb_endpoint_xfer_int(&ep->desc)) &&
123 				desc->bmAttributes != 0) {
124 		dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
125 				"config %d interface %d altsetting %d ep %d: "
126 				"setting to zero\n",
127 				usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
128 				desc->bmAttributes,
129 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
130 		ep->ss_ep_comp.bmAttributes = 0;
131 	} else if (usb_endpoint_xfer_bulk(&ep->desc) &&
132 			desc->bmAttributes > 16) {
133 		dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
134 				"config %d interface %d altsetting %d ep %d: "
135 				"setting to max\n",
136 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
137 		ep->ss_ep_comp.bmAttributes = 16;
138 	} else if (usb_endpoint_xfer_isoc(&ep->desc) &&
139 		   !USB_SS_SSP_ISOC_COMP(desc->bmAttributes) &&
140 		   USB_SS_MULT(desc->bmAttributes) > 3) {
141 		dev_warn(ddev, "Isoc endpoint has Mult of %d in "
142 				"config %d interface %d altsetting %d ep %d: "
143 				"setting to 3\n",
144 				USB_SS_MULT(desc->bmAttributes),
145 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
146 		ep->ss_ep_comp.bmAttributes = 2;
147 	}
148 
149 	/* Parse a possible SuperSpeedPlus isoc ep companion descriptor */
150 	if (usb_endpoint_xfer_isoc(&ep->desc) &&
151 	    USB_SS_SSP_ISOC_COMP(desc->bmAttributes))
152 		usb_parse_ssp_isoc_endpoint_companion(ddev, cfgno, inum, asnum,
153 							ep, buffer, size);
154 
155 	if (usb_endpoint_xfer_isoc(&ep->desc))
156 		max_tx = (desc->bMaxBurst + 1) *
157 			(USB_SS_MULT(desc->bmAttributes)) *
158 			usb_endpoint_maxp(&ep->desc);
159 	else if (usb_endpoint_xfer_int(&ep->desc))
160 		max_tx = usb_endpoint_maxp(&ep->desc) *
161 			(desc->bMaxBurst + 1);
162 	else
163 		max_tx = 999999;
164 	if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
165 		dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
166 				"config %d interface %d altsetting %d ep %d: "
167 				"setting to %d\n",
168 				usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
169 				le16_to_cpu(desc->wBytesPerInterval),
170 				cfgno, inum, asnum, ep->desc.bEndpointAddress,
171 				max_tx);
172 		ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
173 	}
174 }
175 
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)
179 {
180 	unsigned char *buffer0 = buffer;
181 	struct usb_endpoint_descriptor *d;
182 	struct usb_host_endpoint *endpoint;
183 	int n, i, j, retval;
184 
185 	d = (struct usb_endpoint_descriptor *) buffer;
186 	buffer += d->bLength;
187 	size -= d->bLength;
188 
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;
193 	else {
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;
198 	}
199 
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;
206 	}
207 
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;
211 
212 	endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
213 	++ifp->desc.bNumEndpoints;
214 
215 	memcpy(&endpoint->desc, d, n);
216 	INIT_LIST_HEAD(&endpoint->urb_list);
217 
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 */
221 	j = 255;
222 	if (usb_endpoint_xfer_int(d)) {
223 		i = 1;
224 		switch (to_usb_device(ddev)->speed) {
225 		case USB_SPEED_SUPER_PLUS:
226 		case USB_SPEED_SUPER:
227 		case USB_SPEED_HIGH:
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);
233 			if (n == 0)
234 				n = 9;	/* 32 ms = 2^(9-1) uframes */
235 			j = 16;
236 
237 			/*
238 			 * Adjust bInterval for quirked devices.
239 			 * This quirk fixes bIntervals reported in
240 			 * linear microframes.
241 			 */
242 			if (to_usb_device(ddev)->quirks &
243 				USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
244 				n = clamp(fls(d->bInterval), i, j);
245 				i = j = n;
246 			}
247 			break;
248 		default:		/* USB_SPEED_FULL or _LOW */
249 			/* For low-speed, 10 ms is the official minimum.
250 			 * But some "overclocked" devices might want faster
251 			 * polling so we'll allow it. */
252 			n = 32;
253 			break;
254 		}
255 	} else if (usb_endpoint_xfer_isoc(d)) {
256 		i = 1;
257 		j = 16;
258 		switch (to_usb_device(ddev)->speed) {
259 		case USB_SPEED_HIGH:
260 			n = 9;		/* 32 ms = 2^(9-1) uframes */
261 			break;
262 		default:		/* USB_SPEED_FULL */
263 			n = 6;		/* 32 ms = 2^(6-1) frames */
264 			break;
265 		}
266 	}
267 	if (d->bInterval < i || d->bInterval > j) {
268 		dev_warn(ddev, "config %d interface %d altsetting %d "
269 		    "endpoint 0x%X has an invalid bInterval %d, "
270 		    "changing to %d\n",
271 		    cfgno, inum, asnum,
272 		    d->bEndpointAddress, d->bInterval, n);
273 		endpoint->desc.bInterval = n;
274 	}
275 
276 	/* Some buggy low-speed devices have Bulk endpoints, which is
277 	 * explicitly forbidden by the USB spec.  In an attempt to make
278 	 * them usable, we will try treating them as Interrupt endpoints.
279 	 */
280 	if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
281 			usb_endpoint_xfer_bulk(d)) {
282 		dev_warn(ddev, "config %d interface %d altsetting %d "
283 		    "endpoint 0x%X is Bulk; changing to Interrupt\n",
284 		    cfgno, inum, asnum, d->bEndpointAddress);
285 		endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
286 		endpoint->desc.bInterval = 1;
287 		if (usb_endpoint_maxp(&endpoint->desc) > 8)
288 			endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
289 	}
290 
291 	/*
292 	 * Some buggy high speed devices have bulk endpoints using
293 	 * maxpacket sizes other than 512.  High speed HCDs may not
294 	 * be able to handle that particular bug, so let's warn...
295 	 */
296 	if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
297 			&& usb_endpoint_xfer_bulk(d)) {
298 		unsigned maxp;
299 
300 		maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
301 		if (maxp != 512)
302 			dev_warn(ddev, "config %d interface %d altsetting %d "
303 				"bulk endpoint 0x%X has invalid maxpacket %d\n",
304 				cfgno, inum, asnum, d->bEndpointAddress,
305 				maxp);
306 	}
307 
308 	/* Parse a possible SuperSpeed endpoint companion descriptor */
309 	if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
310 		usb_parse_ss_endpoint_companion(ddev, cfgno,
311 				inum, asnum, endpoint, buffer, size);
312 
313 	/* Skip over any Class Specific or Vendor Specific descriptors;
314 	 * find the next endpoint or interface descriptor */
315 	endpoint->extra = buffer;
316 	i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
317 			USB_DT_INTERFACE, &n);
318 	endpoint->extralen = i;
319 	retval = buffer - buffer0 + i;
320 	if (n > 0)
321 		dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
322 		    n, plural(n), "endpoint");
323 	return retval;
324 
325 skip_to_next_endpoint_or_interface_descriptor:
326 	i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
327 	    USB_DT_INTERFACE, NULL);
328 	return buffer - buffer0 + i;
329 }
330 
331 void usb_release_interface_cache(struct kref *ref)
332 {
333 	struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
334 	int j;
335 
336 	for (j = 0; j < intfc->num_altsetting; j++) {
337 		struct usb_host_interface *alt = &intfc->altsetting[j];
338 
339 		kfree(alt->endpoint);
340 		kfree(alt->string);
341 	}
342 	kfree(intfc);
343 }
344 
345 static int usb_parse_interface(struct device *ddev, int cfgno,
346     struct usb_host_config *config, unsigned char *buffer, int size,
347     u8 inums[], u8 nalts[])
348 {
349 	unsigned char *buffer0 = buffer;
350 	struct usb_interface_descriptor	*d;
351 	int inum, asnum;
352 	struct usb_interface_cache *intfc;
353 	struct usb_host_interface *alt;
354 	int i, n;
355 	int len, retval;
356 	int num_ep, num_ep_orig;
357 
358 	d = (struct usb_interface_descriptor *) buffer;
359 	buffer += d->bLength;
360 	size -= d->bLength;
361 
362 	if (d->bLength < USB_DT_INTERFACE_SIZE)
363 		goto skip_to_next_interface_descriptor;
364 
365 	/* Which interface entry is this? */
366 	intfc = NULL;
367 	inum = d->bInterfaceNumber;
368 	for (i = 0; i < config->desc.bNumInterfaces; ++i) {
369 		if (inums[i] == inum) {
370 			intfc = config->intf_cache[i];
371 			break;
372 		}
373 	}
374 	if (!intfc || intfc->num_altsetting >= nalts[i])
375 		goto skip_to_next_interface_descriptor;
376 
377 	/* Check for duplicate altsetting entries */
378 	asnum = d->bAlternateSetting;
379 	for ((i = 0, alt = &intfc->altsetting[0]);
380 	      i < intfc->num_altsetting;
381 	     (++i, ++alt)) {
382 		if (alt->desc.bAlternateSetting == asnum) {
383 			dev_warn(ddev, "Duplicate descriptor for config %d "
384 			    "interface %d altsetting %d, skipping\n",
385 			    cfgno, inum, asnum);
386 			goto skip_to_next_interface_descriptor;
387 		}
388 	}
389 
390 	++intfc->num_altsetting;
391 	memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
392 
393 	/* Skip over any Class Specific or Vendor Specific descriptors;
394 	 * find the first endpoint or interface descriptor */
395 	alt->extra = buffer;
396 	i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
397 	    USB_DT_INTERFACE, &n);
398 	alt->extralen = i;
399 	if (n > 0)
400 		dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
401 		    n, plural(n), "interface");
402 	buffer += i;
403 	size -= i;
404 
405 	/* Allocate space for the right(?) number of endpoints */
406 	num_ep = num_ep_orig = alt->desc.bNumEndpoints;
407 	alt->desc.bNumEndpoints = 0;		/* Use as a counter */
408 	if (num_ep > USB_MAXENDPOINTS) {
409 		dev_warn(ddev, "too many endpoints for config %d interface %d "
410 		    "altsetting %d: %d, using maximum allowed: %d\n",
411 		    cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
412 		num_ep = USB_MAXENDPOINTS;
413 	}
414 
415 	if (num_ep > 0) {
416 		/* Can't allocate 0 bytes */
417 		len = sizeof(struct usb_host_endpoint) * num_ep;
418 		alt->endpoint = kzalloc(len, GFP_KERNEL);
419 		if (!alt->endpoint)
420 			return -ENOMEM;
421 	}
422 
423 	/* Parse all the endpoint descriptors */
424 	n = 0;
425 	while (size > 0) {
426 		if (((struct usb_descriptor_header *) buffer)->bDescriptorType
427 		     == USB_DT_INTERFACE)
428 			break;
429 		retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
430 		    num_ep, buffer, size);
431 		if (retval < 0)
432 			return retval;
433 		++n;
434 
435 		buffer += retval;
436 		size -= retval;
437 	}
438 
439 	if (n != num_ep_orig)
440 		dev_warn(ddev, "config %d interface %d altsetting %d has %d "
441 		    "endpoint descriptor%s, different from the interface "
442 		    "descriptor's value: %d\n",
443 		    cfgno, inum, asnum, n, plural(n), num_ep_orig);
444 	return buffer - buffer0;
445 
446 skip_to_next_interface_descriptor:
447 	i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
448 	    USB_DT_INTERFACE, NULL);
449 	return buffer - buffer0 + i;
450 }
451 
452 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
453     struct usb_host_config *config, unsigned char *buffer, int size)
454 {
455 	struct device *ddev = &dev->dev;
456 	unsigned char *buffer0 = buffer;
457 	int cfgno;
458 	int nintf, nintf_orig;
459 	int i, j, n;
460 	struct usb_interface_cache *intfc;
461 	unsigned char *buffer2;
462 	int size2;
463 	struct usb_descriptor_header *header;
464 	int len, retval;
465 	u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
466 	unsigned iad_num = 0;
467 
468 	memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
469 	if (config->desc.bDescriptorType != USB_DT_CONFIG ||
470 	    config->desc.bLength < USB_DT_CONFIG_SIZE ||
471 	    config->desc.bLength > size) {
472 		dev_err(ddev, "invalid descriptor for config index %d: "
473 		    "type = 0x%X, length = %d\n", cfgidx,
474 		    config->desc.bDescriptorType, config->desc.bLength);
475 		return -EINVAL;
476 	}
477 	cfgno = config->desc.bConfigurationValue;
478 
479 	buffer += config->desc.bLength;
480 	size -= config->desc.bLength;
481 
482 	nintf = nintf_orig = config->desc.bNumInterfaces;
483 	if (nintf > USB_MAXINTERFACES) {
484 		dev_warn(ddev, "config %d has too many interfaces: %d, "
485 		    "using maximum allowed: %d\n",
486 		    cfgno, nintf, USB_MAXINTERFACES);
487 		nintf = USB_MAXINTERFACES;
488 	}
489 
490 	/* Go through the descriptors, checking their length and counting the
491 	 * number of altsettings for each interface */
492 	n = 0;
493 	for ((buffer2 = buffer, size2 = size);
494 	      size2 > 0;
495 	     (buffer2 += header->bLength, size2 -= header->bLength)) {
496 
497 		if (size2 < sizeof(struct usb_descriptor_header)) {
498 			dev_warn(ddev, "config %d descriptor has %d excess "
499 			    "byte%s, ignoring\n",
500 			    cfgno, size2, plural(size2));
501 			break;
502 		}
503 
504 		header = (struct usb_descriptor_header *) buffer2;
505 		if ((header->bLength > size2) || (header->bLength < 2)) {
506 			dev_warn(ddev, "config %d has an invalid descriptor "
507 			    "of length %d, skipping remainder of the config\n",
508 			    cfgno, header->bLength);
509 			break;
510 		}
511 
512 		if (header->bDescriptorType == USB_DT_INTERFACE) {
513 			struct usb_interface_descriptor *d;
514 			int inum;
515 
516 			d = (struct usb_interface_descriptor *) header;
517 			if (d->bLength < USB_DT_INTERFACE_SIZE) {
518 				dev_warn(ddev, "config %d has an invalid "
519 				    "interface descriptor of length %d, "
520 				    "skipping\n", cfgno, d->bLength);
521 				continue;
522 			}
523 
524 			inum = d->bInterfaceNumber;
525 
526 			if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
527 			    n >= nintf_orig) {
528 				dev_warn(ddev, "config %d has more interface "
529 				    "descriptors, than it declares in "
530 				    "bNumInterfaces, ignoring interface "
531 				    "number: %d\n", cfgno, inum);
532 				continue;
533 			}
534 
535 			if (inum >= nintf_orig)
536 				dev_warn(ddev, "config %d has an invalid "
537 				    "interface number: %d but max is %d\n",
538 				    cfgno, inum, nintf_orig - 1);
539 
540 			/* Have we already encountered this interface?
541 			 * Count its altsettings */
542 			for (i = 0; i < n; ++i) {
543 				if (inums[i] == inum)
544 					break;
545 			}
546 			if (i < n) {
547 				if (nalts[i] < 255)
548 					++nalts[i];
549 			} else if (n < USB_MAXINTERFACES) {
550 				inums[n] = inum;
551 				nalts[n] = 1;
552 				++n;
553 			}
554 
555 		} else if (header->bDescriptorType ==
556 				USB_DT_INTERFACE_ASSOCIATION) {
557 			if (iad_num == USB_MAXIADS) {
558 				dev_warn(ddev, "found more Interface "
559 					       "Association Descriptors "
560 					       "than allocated for in "
561 					       "configuration %d\n", cfgno);
562 			} else {
563 				config->intf_assoc[iad_num] =
564 					(struct usb_interface_assoc_descriptor
565 					*)header;
566 				iad_num++;
567 			}
568 
569 		} else if (header->bDescriptorType == USB_DT_DEVICE ||
570 			    header->bDescriptorType == USB_DT_CONFIG)
571 			dev_warn(ddev, "config %d contains an unexpected "
572 			    "descriptor of type 0x%X, skipping\n",
573 			    cfgno, header->bDescriptorType);
574 
575 	}	/* for ((buffer2 = buffer, size2 = size); ...) */
576 	size = buffer2 - buffer;
577 	config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
578 
579 	if (n != nintf)
580 		dev_warn(ddev, "config %d has %d interface%s, different from "
581 		    "the descriptor's value: %d\n",
582 		    cfgno, n, plural(n), nintf_orig);
583 	else if (n == 0)
584 		dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
585 	config->desc.bNumInterfaces = nintf = n;
586 
587 	/* Check for missing interface numbers */
588 	for (i = 0; i < nintf; ++i) {
589 		for (j = 0; j < nintf; ++j) {
590 			if (inums[j] == i)
591 				break;
592 		}
593 		if (j >= nintf)
594 			dev_warn(ddev, "config %d has no interface number "
595 			    "%d\n", cfgno, i);
596 	}
597 
598 	/* Allocate the usb_interface_caches and altsetting arrays */
599 	for (i = 0; i < nintf; ++i) {
600 		j = nalts[i];
601 		if (j > USB_MAXALTSETTING) {
602 			dev_warn(ddev, "too many alternate settings for "
603 			    "config %d interface %d: %d, "
604 			    "using maximum allowed: %d\n",
605 			    cfgno, inums[i], j, USB_MAXALTSETTING);
606 			nalts[i] = j = USB_MAXALTSETTING;
607 		}
608 
609 		len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
610 		config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
611 		if (!intfc)
612 			return -ENOMEM;
613 		kref_init(&intfc->ref);
614 	}
615 
616 	/* FIXME: parse the BOS descriptor */
617 
618 	/* Skip over any Class Specific or Vendor Specific descriptors;
619 	 * find the first interface descriptor */
620 	config->extra = buffer;
621 	i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
622 	    USB_DT_INTERFACE, &n);
623 	config->extralen = i;
624 	if (n > 0)
625 		dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
626 		    n, plural(n), "configuration");
627 	buffer += i;
628 	size -= i;
629 
630 	/* Parse all the interface/altsetting descriptors */
631 	while (size > 0) {
632 		retval = usb_parse_interface(ddev, cfgno, config,
633 		    buffer, size, inums, nalts);
634 		if (retval < 0)
635 			return retval;
636 
637 		buffer += retval;
638 		size -= retval;
639 	}
640 
641 	/* Check for missing altsettings */
642 	for (i = 0; i < nintf; ++i) {
643 		intfc = config->intf_cache[i];
644 		for (j = 0; j < intfc->num_altsetting; ++j) {
645 			for (n = 0; n < intfc->num_altsetting; ++n) {
646 				if (intfc->altsetting[n].desc.
647 				    bAlternateSetting == j)
648 					break;
649 			}
650 			if (n >= intfc->num_altsetting)
651 				dev_warn(ddev, "config %d interface %d has no "
652 				    "altsetting %d\n", cfgno, inums[i], j);
653 		}
654 	}
655 
656 	return 0;
657 }
658 
659 /* hub-only!! ... and only exported for reset/reinit path.
660  * otherwise used internally on disconnect/destroy path
661  */
662 void usb_destroy_configuration(struct usb_device *dev)
663 {
664 	int c, i;
665 
666 	if (!dev->config)
667 		return;
668 
669 	if (dev->rawdescriptors) {
670 		for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
671 			kfree(dev->rawdescriptors[i]);
672 
673 		kfree(dev->rawdescriptors);
674 		dev->rawdescriptors = NULL;
675 	}
676 
677 	for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
678 		struct usb_host_config *cf = &dev->config[c];
679 
680 		kfree(cf->string);
681 		for (i = 0; i < cf->desc.bNumInterfaces; i++) {
682 			if (cf->intf_cache[i])
683 				kref_put(&cf->intf_cache[i]->ref,
684 					  usb_release_interface_cache);
685 		}
686 	}
687 	kfree(dev->config);
688 	dev->config = NULL;
689 }
690 
691 
692 /*
693  * Get the USB config descriptors, cache and parse'em
694  *
695  * hub-only!! ... and only in reset path, or usb_new_device()
696  * (used by real hubs and virtual root hubs)
697  */
698 int usb_get_configuration(struct usb_device *dev)
699 {
700 	struct device *ddev = &dev->dev;
701 	int ncfg = dev->descriptor.bNumConfigurations;
702 	int result = 0;
703 	unsigned int cfgno, length;
704 	unsigned char *bigbuffer;
705 	struct usb_config_descriptor *desc;
706 
707 	cfgno = 0;
708 	result = -ENOMEM;
709 	if (ncfg > USB_MAXCONFIG) {
710 		dev_warn(ddev, "too many configurations: %d, "
711 		    "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
712 		dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
713 	}
714 
715 	if (ncfg < 1) {
716 		dev_err(ddev, "no configurations\n");
717 		return -EINVAL;
718 	}
719 
720 	length = ncfg * sizeof(struct usb_host_config);
721 	dev->config = kzalloc(length, GFP_KERNEL);
722 	if (!dev->config)
723 		goto err2;
724 
725 	length = ncfg * sizeof(char *);
726 	dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
727 	if (!dev->rawdescriptors)
728 		goto err2;
729 
730 	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
731 	if (!desc)
732 		goto err2;
733 
734 	result = 0;
735 	for (; cfgno < ncfg; cfgno++) {
736 		/* We grab just the first descriptor so we know how long
737 		 * the whole configuration is */
738 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
739 		    desc, USB_DT_CONFIG_SIZE);
740 		if (result < 0) {
741 			dev_err(ddev, "unable to read config index %d "
742 			    "descriptor/%s: %d\n", cfgno, "start", result);
743 			if (result != -EPIPE)
744 				goto err;
745 			dev_err(ddev, "chopping to %d config(s)\n", cfgno);
746 			dev->descriptor.bNumConfigurations = cfgno;
747 			break;
748 		} else if (result < 4) {
749 			dev_err(ddev, "config index %d descriptor too short "
750 			    "(expected %i, got %i)\n", cfgno,
751 			    USB_DT_CONFIG_SIZE, result);
752 			result = -EINVAL;
753 			goto err;
754 		}
755 		length = max((int) le16_to_cpu(desc->wTotalLength),
756 		    USB_DT_CONFIG_SIZE);
757 
758 		/* Now that we know the length, get the whole thing */
759 		bigbuffer = kmalloc(length, GFP_KERNEL);
760 		if (!bigbuffer) {
761 			result = -ENOMEM;
762 			goto err;
763 		}
764 
765 		if (dev->quirks & USB_QUIRK_DELAY_INIT)
766 			msleep(100);
767 
768 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
769 		    bigbuffer, length);
770 		if (result < 0) {
771 			dev_err(ddev, "unable to read config index %d "
772 			    "descriptor/%s\n", cfgno, "all");
773 			kfree(bigbuffer);
774 			goto err;
775 		}
776 		if (result < length) {
777 			dev_warn(ddev, "config index %d descriptor too short "
778 			    "(expected %i, got %i)\n", cfgno, length, result);
779 			length = result;
780 		}
781 
782 		dev->rawdescriptors[cfgno] = bigbuffer;
783 
784 		result = usb_parse_configuration(dev, cfgno,
785 		    &dev->config[cfgno], bigbuffer, length);
786 		if (result < 0) {
787 			++cfgno;
788 			goto err;
789 		}
790 	}
791 	result = 0;
792 
793 err:
794 	kfree(desc);
795 	dev->descriptor.bNumConfigurations = cfgno;
796 err2:
797 	if (result == -ENOMEM)
798 		dev_err(ddev, "out of memory\n");
799 	return result;
800 }
801 
802 void usb_release_bos_descriptor(struct usb_device *dev)
803 {
804 	if (dev->bos) {
805 		kfree(dev->bos->desc);
806 		kfree(dev->bos);
807 		dev->bos = NULL;
808 	}
809 }
810 
811 /* Get BOS descriptor set */
812 int usb_get_bos_descriptor(struct usb_device *dev)
813 {
814 	struct device *ddev = &dev->dev;
815 	struct usb_bos_descriptor *bos;
816 	struct usb_dev_cap_header *cap;
817 	unsigned char *buffer;
818 	int length, total_len, num, i;
819 	int ret;
820 
821 	bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
822 	if (!bos)
823 		return -ENOMEM;
824 
825 	/* Get BOS descriptor */
826 	ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
827 	if (ret < USB_DT_BOS_SIZE) {
828 		dev_err(ddev, "unable to get BOS descriptor\n");
829 		if (ret >= 0)
830 			ret = -ENOMSG;
831 		kfree(bos);
832 		return ret;
833 	}
834 
835 	length = bos->bLength;
836 	total_len = le16_to_cpu(bos->wTotalLength);
837 	num = bos->bNumDeviceCaps;
838 	kfree(bos);
839 	if (total_len < length)
840 		return -EINVAL;
841 
842 	dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
843 	if (!dev->bos)
844 		return -ENOMEM;
845 
846 	/* Now let's get the whole BOS descriptor set */
847 	buffer = kzalloc(total_len, GFP_KERNEL);
848 	if (!buffer) {
849 		ret = -ENOMEM;
850 		goto err;
851 	}
852 	dev->bos->desc = (struct usb_bos_descriptor *)buffer;
853 
854 	ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
855 	if (ret < total_len) {
856 		dev_err(ddev, "unable to get BOS descriptor set\n");
857 		if (ret >= 0)
858 			ret = -ENOMSG;
859 		goto err;
860 	}
861 	total_len -= length;
862 
863 	for (i = 0; i < num; i++) {
864 		buffer += length;
865 		cap = (struct usb_dev_cap_header *)buffer;
866 		length = cap->bLength;
867 
868 		if (total_len < length)
869 			break;
870 		total_len -= length;
871 
872 		if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
873 			dev_warn(ddev, "descriptor type invalid, skip\n");
874 			continue;
875 		}
876 
877 		switch (cap->bDevCapabilityType) {
878 		case USB_CAP_TYPE_WIRELESS_USB:
879 			/* Wireless USB cap descriptor is handled by wusb */
880 			break;
881 		case USB_CAP_TYPE_EXT:
882 			dev->bos->ext_cap =
883 				(struct usb_ext_cap_descriptor *)buffer;
884 			break;
885 		case USB_SS_CAP_TYPE:
886 			dev->bos->ss_cap =
887 				(struct usb_ss_cap_descriptor *)buffer;
888 			break;
889 		case USB_SSP_CAP_TYPE:
890 			dev->bos->ssp_cap =
891 				(struct usb_ssp_cap_descriptor *)buffer;
892 			break;
893 		case CONTAINER_ID_TYPE:
894 			dev->bos->ss_id =
895 				(struct usb_ss_container_id_descriptor *)buffer;
896 			break;
897 		case USB_PTM_CAP_TYPE:
898 			dev->bos->ptm_cap =
899 				(struct usb_ptm_cap_descriptor *)buffer;
900 		default:
901 			break;
902 		}
903 	}
904 
905 	return 0;
906 
907 err:
908 	usb_release_bos_descriptor(dev);
909 	return ret;
910 }
911