xref: /linux/drivers/usb/core/config.c (revision 4eee1520ea845a6d6d82e85498d9412419560871)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Released under the GPLv2 only.
4  */
5 
6 #include <linux/usb.h>
7 #include <linux/usb/ch9.h>
8 #include <linux/usb/hcd.h>
9 #include <linux/usb/quirks.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/string_choices.h>
13 #include <linux/device.h>
14 #include <asm/byteorder.h>
15 #include "usb.h"
16 
17 
18 #define USB_MAXALTSETTING		128	/* Hard limit */
19 
20 #define USB_MAXCONFIG			8	/* Arbitrary limit */
21 
find_next_descriptor(unsigned char * buffer,int size,int dt1,int dt2,int * num_skipped)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 
usb_parse_ssp_isoc_endpoint_companion(struct device * ddev,int cfgno,int inum,int asnum,struct usb_host_endpoint * ep,unsigned char * buffer,int size)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_notice(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 
usb_parse_eusb2_isoc_endpoint_companion(struct device * ddev,int cfgno,int inum,int asnum,struct usb_host_endpoint * ep,unsigned char * buffer,int size)67 static void usb_parse_eusb2_isoc_endpoint_companion(struct device *ddev,
68 		int cfgno, int inum, int asnum, struct usb_host_endpoint *ep,
69 		unsigned char *buffer, int size)
70 {
71 	struct usb_eusb2_isoc_ep_comp_descriptor *desc;
72 	struct usb_descriptor_header *h;
73 
74 	/*
75 	 * eUSB2 isochronous endpoint companion descriptor for this endpoint
76 	 * shall be declared before the next endpoint or interface descriptor
77 	 */
78 	while (size >= USB_DT_EUSB2_ISOC_EP_COMP_SIZE) {
79 		h = (struct usb_descriptor_header *)buffer;
80 
81 		if (h->bDescriptorType == USB_DT_EUSB2_ISOC_ENDPOINT_COMP) {
82 			desc = (struct usb_eusb2_isoc_ep_comp_descriptor *)buffer;
83 			ep->eusb2_isoc_ep_comp = *desc;
84 			return;
85 		}
86 		if (h->bDescriptorType == USB_DT_ENDPOINT ||
87 		    h->bDescriptorType == USB_DT_INTERFACE)
88 			break;
89 
90 		buffer += h->bLength;
91 		size -= h->bLength;
92 	}
93 
94 	dev_notice(ddev, "No eUSB2 isoc ep %d companion for config %d interface %d altsetting %d\n",
95 		   ep->desc.bEndpointAddress, cfgno, inum, asnum);
96 }
97 
usb_parse_ss_endpoint_companion(struct device * ddev,int cfgno,int inum,int asnum,struct usb_host_endpoint * ep,unsigned char * buffer,int size)98 static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
99 		int inum, int asnum, struct usb_host_endpoint *ep,
100 		unsigned char *buffer, int size)
101 {
102 	struct usb_ss_ep_comp_descriptor *desc;
103 	int max_tx;
104 
105 	/* The SuperSpeed endpoint companion descriptor is supposed to
106 	 * be the first thing immediately following the endpoint descriptor.
107 	 */
108 	desc = (struct usb_ss_ep_comp_descriptor *) buffer;
109 
110 	if (size < USB_DT_SS_EP_COMP_SIZE) {
111 		dev_notice(ddev,
112 			   "invalid SuperSpeed endpoint companion descriptor "
113 			   "of length %d, skipping\n", size);
114 		return;
115 	}
116 
117 	if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) {
118 		dev_notice(ddev, "No SuperSpeed endpoint companion for config %d "
119 				" interface %d altsetting %d ep %d: "
120 				"using minimum values\n",
121 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
122 
123 		/* Fill in some default values.
124 		 * Leave bmAttributes as zero, which will mean no streams for
125 		 * bulk, and isoc won't support multiple bursts of packets.
126 		 * With bursts of only one packet, and a Mult of 1, the max
127 		 * amount of data moved per endpoint service interval is one
128 		 * packet.
129 		 */
130 		ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
131 		ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
132 		if (usb_endpoint_xfer_isoc(&ep->desc) ||
133 				usb_endpoint_xfer_int(&ep->desc))
134 			ep->ss_ep_comp.wBytesPerInterval =
135 					ep->desc.wMaxPacketSize;
136 		return;
137 	}
138 	buffer += desc->bLength;
139 	size -= desc->bLength;
140 	memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
141 
142 	/* Check the various values */
143 	if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
144 		dev_notice(ddev, "Control endpoint with bMaxBurst = %d in "
145 				"config %d interface %d altsetting %d ep %d: "
146 				"setting to zero\n", desc->bMaxBurst,
147 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
148 		ep->ss_ep_comp.bMaxBurst = 0;
149 	} else if (desc->bMaxBurst > 15) {
150 		dev_notice(ddev, "Endpoint with bMaxBurst = %d in "
151 				"config %d interface %d altsetting %d ep %d: "
152 				"setting to 15\n", desc->bMaxBurst,
153 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
154 		ep->ss_ep_comp.bMaxBurst = 15;
155 	}
156 
157 	if ((usb_endpoint_xfer_control(&ep->desc) ||
158 			usb_endpoint_xfer_int(&ep->desc)) &&
159 				desc->bmAttributes != 0) {
160 		dev_notice(ddev, "%s endpoint with bmAttributes = %d in "
161 				"config %d interface %d altsetting %d ep %d: "
162 				"setting to zero\n",
163 				usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
164 				desc->bmAttributes,
165 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
166 		ep->ss_ep_comp.bmAttributes = 0;
167 	} else if (usb_endpoint_xfer_bulk(&ep->desc) &&
168 			desc->bmAttributes > 16) {
169 		dev_notice(ddev, "Bulk endpoint with more than 65536 streams in "
170 				"config %d interface %d altsetting %d ep %d: "
171 				"setting to max\n",
172 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
173 		ep->ss_ep_comp.bmAttributes = 16;
174 	} else if (usb_endpoint_xfer_isoc(&ep->desc) &&
175 		   !USB_SS_SSP_ISOC_COMP(desc->bmAttributes) &&
176 		   USB_SS_MULT(desc->bmAttributes) > 3) {
177 		dev_notice(ddev, "Isoc endpoint has Mult of %d in "
178 				"config %d interface %d altsetting %d ep %d: "
179 				"setting to 3\n",
180 				USB_SS_MULT(desc->bmAttributes),
181 				cfgno, inum, asnum, ep->desc.bEndpointAddress);
182 		ep->ss_ep_comp.bmAttributes = 2;
183 	}
184 
185 	if (usb_endpoint_xfer_isoc(&ep->desc))
186 		max_tx = (desc->bMaxBurst + 1) *
187 			(USB_SS_MULT(desc->bmAttributes)) *
188 			usb_endpoint_maxp(&ep->desc);
189 	else if (usb_endpoint_xfer_int(&ep->desc))
190 		max_tx = usb_endpoint_maxp(&ep->desc) *
191 			(desc->bMaxBurst + 1);
192 	else
193 		max_tx = 999999;
194 	if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
195 		dev_notice(ddev, "%s endpoint with wBytesPerInterval of %d in "
196 				"config %d interface %d altsetting %d ep %d: "
197 				"setting to %d\n",
198 				usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
199 				le16_to_cpu(desc->wBytesPerInterval),
200 				cfgno, inum, asnum, ep->desc.bEndpointAddress,
201 				max_tx);
202 		ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
203 	}
204 	/* Parse a possible SuperSpeedPlus isoc ep companion descriptor */
205 	if (usb_endpoint_xfer_isoc(&ep->desc) &&
206 	    USB_SS_SSP_ISOC_COMP(desc->bmAttributes))
207 		usb_parse_ssp_isoc_endpoint_companion(ddev, cfgno, inum, asnum,
208 							ep, buffer, size);
209 }
210 
211 static const unsigned short low_speed_maxpacket_maxes[4] = {
212 	[USB_ENDPOINT_XFER_CONTROL] = 8,
213 	[USB_ENDPOINT_XFER_ISOC] = 0,
214 	[USB_ENDPOINT_XFER_BULK] = 0,
215 	[USB_ENDPOINT_XFER_INT] = 8,
216 };
217 static const unsigned short full_speed_maxpacket_maxes[4] = {
218 	[USB_ENDPOINT_XFER_CONTROL] = 64,
219 	[USB_ENDPOINT_XFER_ISOC] = 1023,
220 	[USB_ENDPOINT_XFER_BULK] = 64,
221 	[USB_ENDPOINT_XFER_INT] = 64,
222 };
223 static const unsigned short high_speed_maxpacket_maxes[4] = {
224 	[USB_ENDPOINT_XFER_CONTROL] = 64,
225 	[USB_ENDPOINT_XFER_ISOC] = 1024,
226 
227 	/* Bulk should be 512, but some devices use 1024: we will warn below */
228 	[USB_ENDPOINT_XFER_BULK] = 1024,
229 	[USB_ENDPOINT_XFER_INT] = 1024,
230 };
231 static const unsigned short super_speed_maxpacket_maxes[4] = {
232 	[USB_ENDPOINT_XFER_CONTROL] = 512,
233 	[USB_ENDPOINT_XFER_ISOC] = 1024,
234 	[USB_ENDPOINT_XFER_BULK] = 1024,
235 	[USB_ENDPOINT_XFER_INT] = 1024,
236 };
237 
endpoint_is_duplicate(struct usb_endpoint_descriptor * e1,struct usb_endpoint_descriptor * e2)238 static bool endpoint_is_duplicate(struct usb_endpoint_descriptor *e1,
239 		struct usb_endpoint_descriptor *e2)
240 {
241 	if (e1->bEndpointAddress == e2->bEndpointAddress)
242 		return true;
243 
244 	if (usb_endpoint_xfer_control(e1) || usb_endpoint_xfer_control(e2)) {
245 		if (usb_endpoint_num(e1) == usb_endpoint_num(e2))
246 			return true;
247 	}
248 
249 	return false;
250 }
251 
252 /*
253  * Check for duplicate endpoint addresses in other interfaces and in the
254  * altsetting currently being parsed.
255  */
config_endpoint_is_duplicate(struct usb_host_config * config,int inum,int asnum,struct usb_endpoint_descriptor * d)256 static bool config_endpoint_is_duplicate(struct usb_host_config *config,
257 		int inum, int asnum, struct usb_endpoint_descriptor *d)
258 {
259 	struct usb_endpoint_descriptor *epd;
260 	struct usb_interface_cache *intfc;
261 	struct usb_host_interface *alt;
262 	int i, j, k;
263 
264 	for (i = 0; i < config->desc.bNumInterfaces; ++i) {
265 		intfc = config->intf_cache[i];
266 
267 		for (j = 0; j < intfc->num_altsetting; ++j) {
268 			alt = &intfc->altsetting[j];
269 
270 			if (alt->desc.bInterfaceNumber == inum &&
271 					alt->desc.bAlternateSetting != asnum)
272 				continue;
273 
274 			for (k = 0; k < alt->desc.bNumEndpoints; ++k) {
275 				epd = &alt->endpoint[k].desc;
276 
277 				if (endpoint_is_duplicate(epd, d))
278 					return true;
279 			}
280 		}
281 	}
282 
283 	return false;
284 }
285 
usb_parse_endpoint(struct device * ddev,int cfgno,struct usb_host_config * config,int inum,int asnum,struct usb_host_interface * ifp,int num_ep,unsigned char * buffer,int size)286 static int usb_parse_endpoint(struct device *ddev, int cfgno,
287 		struct usb_host_config *config, int inum, int asnum,
288 		struct usb_host_interface *ifp, int num_ep,
289 		unsigned char *buffer, int size)
290 {
291 	struct usb_device *udev = to_usb_device(ddev);
292 	unsigned char *buffer0 = buffer;
293 	struct usb_endpoint_descriptor *d;
294 	struct usb_host_endpoint *endpoint;
295 	int n, i, j, retval;
296 	unsigned int maxp;
297 	const unsigned short *maxpacket_maxes;
298 	u16 bcdUSB;
299 
300 	d = (struct usb_endpoint_descriptor *) buffer;
301 	bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
302 	buffer += d->bLength;
303 	size -= d->bLength;
304 
305 	if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
306 		n = USB_DT_ENDPOINT_AUDIO_SIZE;
307 	else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
308 		n = USB_DT_ENDPOINT_SIZE;
309 	else {
310 		dev_notice(ddev, "config %d interface %d altsetting %d has an "
311 		    "invalid endpoint descriptor of length %d, skipping\n",
312 		    cfgno, inum, asnum, d->bLength);
313 		goto skip_to_next_endpoint_or_interface_descriptor;
314 	}
315 
316 	i = usb_endpoint_num(d);
317 	if (i == 0) {
318 		dev_notice(ddev, "config %d interface %d altsetting %d has an "
319 		    "invalid descriptor for endpoint zero, skipping\n",
320 		    cfgno, inum, asnum);
321 		goto skip_to_next_endpoint_or_interface_descriptor;
322 	}
323 
324 	/* Only store as many endpoints as we have room for */
325 	if (ifp->desc.bNumEndpoints >= num_ep)
326 		goto skip_to_next_endpoint_or_interface_descriptor;
327 
328 	/* Save a copy of the descriptor and use it instead of the original */
329 	endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
330 	memcpy(&endpoint->desc, d, n);
331 	d = &endpoint->desc;
332 
333 	/* Clear the reserved bits in bEndpointAddress */
334 	i = d->bEndpointAddress &
335 			(USB_ENDPOINT_DIR_MASK | USB_ENDPOINT_NUMBER_MASK);
336 	if (i != d->bEndpointAddress) {
337 		dev_notice(ddev, "config %d interface %d altsetting %d has an endpoint descriptor with address 0x%X, changing to 0x%X\n",
338 		    cfgno, inum, asnum, d->bEndpointAddress, i);
339 		endpoint->desc.bEndpointAddress = i;
340 	}
341 
342 	/* Check for duplicate endpoint addresses */
343 	if (config_endpoint_is_duplicate(config, inum, asnum, d)) {
344 		dev_notice(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
345 				cfgno, inum, asnum, d->bEndpointAddress);
346 		goto skip_to_next_endpoint_or_interface_descriptor;
347 	}
348 
349 	/* Ignore some endpoints */
350 	if (udev->quirks & USB_QUIRK_ENDPOINT_IGNORE) {
351 		if (usb_endpoint_is_ignored(udev, ifp, d)) {
352 			dev_notice(ddev, "config %d interface %d altsetting %d has an ignored endpoint with address 0x%X, skipping\n",
353 					cfgno, inum, asnum,
354 					d->bEndpointAddress);
355 			goto skip_to_next_endpoint_or_interface_descriptor;
356 		}
357 	}
358 
359 	/* Accept this endpoint */
360 	++ifp->desc.bNumEndpoints;
361 	INIT_LIST_HEAD(&endpoint->urb_list);
362 
363 	/*
364 	 * Fix up bInterval values outside the legal range.
365 	 * Use 10 or 8 ms if no proper value can be guessed.
366 	 */
367 	i = 0;		/* i = min, j = max, n = default */
368 	j = 255;
369 	if (usb_endpoint_xfer_int(d)) {
370 		i = 1;
371 		switch (udev->speed) {
372 		case USB_SPEED_SUPER_PLUS:
373 		case USB_SPEED_SUPER:
374 		case USB_SPEED_HIGH:
375 			/*
376 			 * Many device manufacturers are using full-speed
377 			 * bInterval values in high-speed interrupt endpoint
378 			 * descriptors. Try to fix those and fall back to an
379 			 * 8-ms default value otherwise.
380 			 */
381 			n = fls(d->bInterval*8);
382 			if (n == 0)
383 				n = 7;	/* 8 ms = 2^(7-1) uframes */
384 			j = 16;
385 
386 			/*
387 			 * Adjust bInterval for quirked devices.
388 			 */
389 			/*
390 			 * This quirk fixes bIntervals reported in ms.
391 			 */
392 			if (udev->quirks & USB_QUIRK_LINEAR_FRAME_INTR_BINTERVAL) {
393 				n = clamp(fls(d->bInterval) + 3, i, j);
394 				i = j = n;
395 			}
396 			/*
397 			 * This quirk fixes bIntervals reported in
398 			 * linear microframes.
399 			 */
400 			if (udev->quirks & USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
401 				n = clamp(fls(d->bInterval), i, j);
402 				i = j = n;
403 			}
404 			break;
405 		default:		/* USB_SPEED_FULL or _LOW */
406 			/*
407 			 * For low-speed, 10 ms is the official minimum.
408 			 * But some "overclocked" devices might want faster
409 			 * polling so we'll allow it.
410 			 */
411 			n = 10;
412 			break;
413 		}
414 	} else if (usb_endpoint_xfer_isoc(d)) {
415 		i = 1;
416 		j = 16;
417 		switch (udev->speed) {
418 		case USB_SPEED_HIGH:
419 			n = 7;		/* 8 ms = 2^(7-1) uframes */
420 			break;
421 		default:		/* USB_SPEED_FULL */
422 			n = 4;		/* 8 ms = 2^(4-1) frames */
423 			break;
424 		}
425 	}
426 	if (d->bInterval < i || d->bInterval > j) {
427 		dev_notice(ddev, "config %d interface %d altsetting %d "
428 		    "endpoint 0x%X has an invalid bInterval %d, "
429 		    "changing to %d\n",
430 		    cfgno, inum, asnum,
431 		    d->bEndpointAddress, d->bInterval, n);
432 		endpoint->desc.bInterval = n;
433 	}
434 
435 	/* Some buggy low-speed devices have Bulk endpoints, which is
436 	 * explicitly forbidden by the USB spec.  In an attempt to make
437 	 * them usable, we will try treating them as Interrupt endpoints.
438 	 */
439 	if (udev->speed == USB_SPEED_LOW && usb_endpoint_xfer_bulk(d)) {
440 		dev_notice(ddev, "config %d interface %d altsetting %d "
441 		    "endpoint 0x%X is Bulk; changing to Interrupt\n",
442 		    cfgno, inum, asnum, d->bEndpointAddress);
443 		endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
444 		endpoint->desc.bInterval = 1;
445 		if (usb_endpoint_maxp(&endpoint->desc) > 8)
446 			endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
447 	}
448 
449 	/*
450 	 * Validate the wMaxPacketSize field.
451 	 * eUSB2 devices (see USB 2.0 Double Isochronous IN ECN 9.6.6 Endpoint)
452 	 * and devices with isochronous endpoints in altsetting 0 (see USB 2.0
453 	 * end of section 5.6.3) have wMaxPacketSize = 0.
454 	 * So don't warn about those.
455 	 */
456 	maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize);
457 
458 	if (maxp == 0 && bcdUSB != 0x0220 &&
459 	    !(usb_endpoint_xfer_isoc(d) && asnum == 0))
460 		dev_notice(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid wMaxPacketSize 0\n",
461 		    cfgno, inum, asnum, d->bEndpointAddress);
462 
463 	/* Find the highest legal maxpacket size for this endpoint */
464 	i = 0;		/* additional transactions per microframe */
465 	switch (udev->speed) {
466 	case USB_SPEED_LOW:
467 		maxpacket_maxes = low_speed_maxpacket_maxes;
468 		break;
469 	case USB_SPEED_FULL:
470 		maxpacket_maxes = full_speed_maxpacket_maxes;
471 		break;
472 	case USB_SPEED_HIGH:
473 		/* Multiple-transactions bits are allowed only for HS periodic endpoints */
474 		if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
475 			i = maxp & USB_EP_MAXP_MULT_MASK;
476 			maxp &= ~i;
477 		}
478 		fallthrough;
479 	default:
480 		maxpacket_maxes = high_speed_maxpacket_maxes;
481 		break;
482 	case USB_SPEED_SUPER:
483 	case USB_SPEED_SUPER_PLUS:
484 		maxpacket_maxes = super_speed_maxpacket_maxes;
485 		break;
486 	}
487 	j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
488 
489 	if (maxp > j) {
490 		dev_notice(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
491 		    cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
492 		maxp = j;
493 		endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
494 	}
495 
496 	/*
497 	 * Some buggy high speed devices have bulk endpoints using
498 	 * maxpacket sizes other than 512.  High speed HCDs may not
499 	 * be able to handle that particular bug, so let's warn...
500 	 */
501 	if (udev->speed == USB_SPEED_HIGH && usb_endpoint_xfer_bulk(d)) {
502 		if (maxp != 512)
503 			dev_notice(ddev, "config %d interface %d altsetting %d "
504 				"bulk endpoint 0x%X has invalid maxpacket %d\n",
505 				cfgno, inum, asnum, d->bEndpointAddress,
506 				maxp);
507 	}
508 
509 	/* Parse a possible eUSB2 periodic endpoint companion descriptor */
510 	if (bcdUSB == 0x0220 && d->wMaxPacketSize == 0 &&
511 	    (usb_endpoint_xfer_isoc(d) || usb_endpoint_xfer_int(d)))
512 		usb_parse_eusb2_isoc_endpoint_companion(ddev, cfgno, inum, asnum,
513 							endpoint, buffer, size);
514 
515 	/* Parse a possible SuperSpeed endpoint companion descriptor */
516 	if (udev->speed >= USB_SPEED_SUPER)
517 		usb_parse_ss_endpoint_companion(ddev, cfgno,
518 				inum, asnum, endpoint, buffer, size);
519 
520 	/* Skip over any Class Specific or Vendor Specific descriptors;
521 	 * find the next endpoint or interface descriptor */
522 	endpoint->extra = buffer;
523 	i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
524 			USB_DT_INTERFACE, &n);
525 	endpoint->extralen = i;
526 	retval = buffer - buffer0 + i;
527 	if (n > 0)
528 		dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
529 		    n, str_plural(n), "endpoint");
530 	return retval;
531 
532 skip_to_next_endpoint_or_interface_descriptor:
533 	i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
534 	    USB_DT_INTERFACE, NULL);
535 	return buffer - buffer0 + i;
536 }
537 
usb_release_interface_cache(struct kref * ref)538 void usb_release_interface_cache(struct kref *ref)
539 {
540 	struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
541 	int j;
542 
543 	for (j = 0; j < intfc->num_altsetting; j++) {
544 		struct usb_host_interface *alt = &intfc->altsetting[j];
545 
546 		kfree(alt->endpoint);
547 		kfree(alt->string);
548 	}
549 	kfree(intfc);
550 }
551 
usb_parse_interface(struct device * ddev,int cfgno,struct usb_host_config * config,unsigned char * buffer,int size,u8 inums[],u8 nalts[])552 static int usb_parse_interface(struct device *ddev, int cfgno,
553     struct usb_host_config *config, unsigned char *buffer, int size,
554     u8 inums[], u8 nalts[])
555 {
556 	unsigned char *buffer0 = buffer;
557 	struct usb_interface_descriptor	*d;
558 	int inum, asnum;
559 	struct usb_interface_cache *intfc;
560 	struct usb_host_interface *alt;
561 	int i, n;
562 	int len, retval;
563 	int num_ep, num_ep_orig;
564 
565 	d = (struct usb_interface_descriptor *) buffer;
566 	buffer += d->bLength;
567 	size -= d->bLength;
568 
569 	if (d->bLength < USB_DT_INTERFACE_SIZE)
570 		goto skip_to_next_interface_descriptor;
571 
572 	/* Which interface entry is this? */
573 	intfc = NULL;
574 	inum = d->bInterfaceNumber;
575 	for (i = 0; i < config->desc.bNumInterfaces; ++i) {
576 		if (inums[i] == inum) {
577 			intfc = config->intf_cache[i];
578 			break;
579 		}
580 	}
581 	if (!intfc || intfc->num_altsetting >= nalts[i])
582 		goto skip_to_next_interface_descriptor;
583 
584 	/* Check for duplicate altsetting entries */
585 	asnum = d->bAlternateSetting;
586 	for ((i = 0, alt = &intfc->altsetting[0]);
587 	      i < intfc->num_altsetting;
588 	     (++i, ++alt)) {
589 		if (alt->desc.bAlternateSetting == asnum) {
590 			dev_notice(ddev, "Duplicate descriptor for config %d "
591 			    "interface %d altsetting %d, skipping\n",
592 			    cfgno, inum, asnum);
593 			goto skip_to_next_interface_descriptor;
594 		}
595 	}
596 
597 	++intfc->num_altsetting;
598 	memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
599 
600 	/* Skip over any Class Specific or Vendor Specific descriptors;
601 	 * find the first endpoint or interface descriptor */
602 	alt->extra = buffer;
603 	i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
604 	    USB_DT_INTERFACE, &n);
605 	alt->extralen = i;
606 	if (n > 0)
607 		dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
608 		    n, str_plural(n), "interface");
609 	buffer += i;
610 	size -= i;
611 
612 	/* Allocate space for the right(?) number of endpoints */
613 	num_ep = num_ep_orig = alt->desc.bNumEndpoints;
614 	alt->desc.bNumEndpoints = 0;		/* Use as a counter */
615 	if (num_ep > USB_MAXENDPOINTS) {
616 		dev_notice(ddev, "too many endpoints for config %d interface %d "
617 		    "altsetting %d: %d, using maximum allowed: %d\n",
618 		    cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
619 		num_ep = USB_MAXENDPOINTS;
620 	}
621 
622 	if (num_ep > 0) {
623 		/* Can't allocate 0 bytes */
624 		len = sizeof(struct usb_host_endpoint) * num_ep;
625 		alt->endpoint = kzalloc(len, GFP_KERNEL);
626 		if (!alt->endpoint)
627 			return -ENOMEM;
628 	}
629 
630 	/* Parse all the endpoint descriptors */
631 	n = 0;
632 	while (size > 0) {
633 		if (((struct usb_descriptor_header *) buffer)->bDescriptorType
634 		     == USB_DT_INTERFACE)
635 			break;
636 		retval = usb_parse_endpoint(ddev, cfgno, config, inum, asnum,
637 				alt, num_ep, buffer, size);
638 		if (retval < 0)
639 			return retval;
640 		++n;
641 
642 		buffer += retval;
643 		size -= retval;
644 	}
645 
646 	if (n != num_ep_orig)
647 		dev_notice(ddev, "config %d interface %d altsetting %d has %d "
648 		    "endpoint descriptor%s, different from the interface "
649 		    "descriptor's value: %d\n",
650 		    cfgno, inum, asnum, n, str_plural(n), num_ep_orig);
651 	return buffer - buffer0;
652 
653 skip_to_next_interface_descriptor:
654 	i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
655 	    USB_DT_INTERFACE, NULL);
656 	return buffer - buffer0 + i;
657 }
658 
usb_parse_configuration(struct usb_device * dev,int cfgidx,struct usb_host_config * config,unsigned char * buffer,int size)659 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
660     struct usb_host_config *config, unsigned char *buffer, int size)
661 {
662 	struct device *ddev = &dev->dev;
663 	unsigned char *buffer0 = buffer;
664 	int cfgno;
665 	int nintf, nintf_orig;
666 	int i, j, n;
667 	struct usb_interface_cache *intfc;
668 	unsigned char *buffer2;
669 	int size2;
670 	struct usb_descriptor_header *header;
671 	int retval;
672 	u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
673 	unsigned iad_num = 0;
674 
675 	memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
676 	nintf = nintf_orig = config->desc.bNumInterfaces;
677 	config->desc.bNumInterfaces = 0;	// Adjusted later
678 
679 	if (config->desc.bDescriptorType != USB_DT_CONFIG ||
680 	    config->desc.bLength < USB_DT_CONFIG_SIZE ||
681 	    config->desc.bLength > size) {
682 		dev_notice(ddev, "invalid descriptor for config index %d: "
683 		    "type = 0x%X, length = %d\n", cfgidx,
684 		    config->desc.bDescriptorType, config->desc.bLength);
685 		return -EINVAL;
686 	}
687 	cfgno = config->desc.bConfigurationValue;
688 
689 	buffer += config->desc.bLength;
690 	size -= config->desc.bLength;
691 
692 	if (nintf > USB_MAXINTERFACES) {
693 		dev_notice(ddev, "config %d has too many interfaces: %d, "
694 		    "using maximum allowed: %d\n",
695 		    cfgno, nintf, USB_MAXINTERFACES);
696 		nintf = USB_MAXINTERFACES;
697 	}
698 
699 	/* Go through the descriptors, checking their length and counting the
700 	 * number of altsettings for each interface */
701 	n = 0;
702 	for ((buffer2 = buffer, size2 = size);
703 	      size2 > 0;
704 	     (buffer2 += header->bLength, size2 -= header->bLength)) {
705 
706 		if (size2 < sizeof(struct usb_descriptor_header)) {
707 			dev_notice(ddev, "config %d descriptor has %d excess "
708 			    "byte%s, ignoring\n",
709 			    cfgno, size2, str_plural(size2));
710 			break;
711 		}
712 
713 		header = (struct usb_descriptor_header *) buffer2;
714 		if ((header->bLength > size2) || (header->bLength < 2)) {
715 			dev_notice(ddev, "config %d has an invalid descriptor "
716 			    "of length %d, skipping remainder of the config\n",
717 			    cfgno, header->bLength);
718 			break;
719 		}
720 
721 		if (header->bDescriptorType == USB_DT_INTERFACE) {
722 			struct usb_interface_descriptor *d;
723 			int inum;
724 
725 			d = (struct usb_interface_descriptor *) header;
726 			if (d->bLength < USB_DT_INTERFACE_SIZE) {
727 				dev_notice(ddev, "config %d has an invalid "
728 				    "interface descriptor of length %d, "
729 				    "skipping\n", cfgno, d->bLength);
730 				continue;
731 			}
732 
733 			inum = d->bInterfaceNumber;
734 
735 			if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
736 			    n >= nintf_orig) {
737 				dev_notice(ddev, "config %d has more interface "
738 				    "descriptors, than it declares in "
739 				    "bNumInterfaces, ignoring interface "
740 				    "number: %d\n", cfgno, inum);
741 				continue;
742 			}
743 
744 			if (inum >= nintf_orig)
745 				dev_notice(ddev, "config %d has an invalid "
746 				    "interface number: %d but max is %d\n",
747 				    cfgno, inum, nintf_orig - 1);
748 
749 			/* Have we already encountered this interface?
750 			 * Count its altsettings */
751 			for (i = 0; i < n; ++i) {
752 				if (inums[i] == inum)
753 					break;
754 			}
755 			if (i < n) {
756 				if (nalts[i] < 255)
757 					++nalts[i];
758 			} else if (n < USB_MAXINTERFACES) {
759 				inums[n] = inum;
760 				nalts[n] = 1;
761 				++n;
762 			}
763 
764 		} else if (header->bDescriptorType ==
765 				USB_DT_INTERFACE_ASSOCIATION) {
766 			struct usb_interface_assoc_descriptor *d;
767 
768 			d = (struct usb_interface_assoc_descriptor *)header;
769 			if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) {
770 				dev_notice(ddev,
771 					 "config %d has an invalid interface association descriptor of length %d, skipping\n",
772 					 cfgno, d->bLength);
773 				continue;
774 			}
775 
776 			if (iad_num == USB_MAXIADS) {
777 				dev_notice(ddev, "found more Interface "
778 					       "Association Descriptors "
779 					       "than allocated for in "
780 					       "configuration %d\n", cfgno);
781 			} else {
782 				config->intf_assoc[iad_num] = d;
783 				iad_num++;
784 			}
785 
786 		} else if (header->bDescriptorType == USB_DT_DEVICE ||
787 			    header->bDescriptorType == USB_DT_CONFIG)
788 			dev_notice(ddev, "config %d contains an unexpected "
789 			    "descriptor of type 0x%X, skipping\n",
790 			    cfgno, header->bDescriptorType);
791 
792 	}	/* for ((buffer2 = buffer, size2 = size); ...) */
793 	size = buffer2 - buffer;
794 	config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
795 
796 	if (n != nintf)
797 		dev_notice(ddev, "config %d has %d interface%s, different from "
798 		    "the descriptor's value: %d\n",
799 		    cfgno, n, str_plural(n), nintf_orig);
800 	else if (n == 0)
801 		dev_notice(ddev, "config %d has no interfaces?\n", cfgno);
802 	config->desc.bNumInterfaces = nintf = n;
803 
804 	/* Check for missing interface numbers */
805 	for (i = 0; i < nintf; ++i) {
806 		for (j = 0; j < nintf; ++j) {
807 			if (inums[j] == i)
808 				break;
809 		}
810 		if (j >= nintf)
811 			dev_notice(ddev, "config %d has no interface number "
812 			    "%d\n", cfgno, i);
813 	}
814 
815 	/* Allocate the usb_interface_caches and altsetting arrays */
816 	for (i = 0; i < nintf; ++i) {
817 		j = nalts[i];
818 		if (j > USB_MAXALTSETTING) {
819 			dev_notice(ddev, "too many alternate settings for "
820 			    "config %d interface %d: %d, "
821 			    "using maximum allowed: %d\n",
822 			    cfgno, inums[i], j, USB_MAXALTSETTING);
823 			nalts[i] = j = USB_MAXALTSETTING;
824 		}
825 
826 		intfc = kzalloc(struct_size(intfc, altsetting, j), GFP_KERNEL);
827 		config->intf_cache[i] = intfc;
828 		if (!intfc)
829 			return -ENOMEM;
830 		kref_init(&intfc->ref);
831 	}
832 
833 	/* FIXME: parse the BOS descriptor */
834 
835 	/* Skip over any Class Specific or Vendor Specific descriptors;
836 	 * find the first interface descriptor */
837 	config->extra = buffer;
838 	i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
839 	    USB_DT_INTERFACE, &n);
840 	config->extralen = i;
841 	if (n > 0)
842 		dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
843 		    n, str_plural(n), "configuration");
844 	buffer += i;
845 	size -= i;
846 
847 	/* Parse all the interface/altsetting descriptors */
848 	while (size > 0) {
849 		retval = usb_parse_interface(ddev, cfgno, config,
850 		    buffer, size, inums, nalts);
851 		if (retval < 0)
852 			return retval;
853 
854 		buffer += retval;
855 		size -= retval;
856 	}
857 
858 	/* Check for missing altsettings */
859 	for (i = 0; i < nintf; ++i) {
860 		intfc = config->intf_cache[i];
861 		for (j = 0; j < intfc->num_altsetting; ++j) {
862 			for (n = 0; n < intfc->num_altsetting; ++n) {
863 				if (intfc->altsetting[n].desc.
864 				    bAlternateSetting == j)
865 					break;
866 			}
867 			if (n >= intfc->num_altsetting)
868 				dev_notice(ddev, "config %d interface %d has no "
869 				    "altsetting %d\n", cfgno, inums[i], j);
870 		}
871 	}
872 
873 	return 0;
874 }
875 
876 /* hub-only!! ... and only exported for reset/reinit path.
877  * otherwise used internally on disconnect/destroy path
878  */
usb_destroy_configuration(struct usb_device * dev)879 void usb_destroy_configuration(struct usb_device *dev)
880 {
881 	int c, i;
882 
883 	if (!dev->config)
884 		return;
885 
886 	if (dev->rawdescriptors) {
887 		for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
888 			kfree(dev->rawdescriptors[i]);
889 
890 		kfree(dev->rawdescriptors);
891 		dev->rawdescriptors = NULL;
892 	}
893 
894 	for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
895 		struct usb_host_config *cf = &dev->config[c];
896 
897 		kfree(cf->string);
898 		for (i = 0; i < cf->desc.bNumInterfaces; i++) {
899 			if (cf->intf_cache[i])
900 				kref_put(&cf->intf_cache[i]->ref,
901 					  usb_release_interface_cache);
902 		}
903 	}
904 	kfree(dev->config);
905 	dev->config = NULL;
906 }
907 
908 
909 /*
910  * Get the USB config descriptors, cache and parse'em
911  *
912  * hub-only!! ... and only in reset path, or usb_new_device()
913  * (used by real hubs and virtual root hubs)
914  */
usb_get_configuration(struct usb_device * dev)915 int usb_get_configuration(struct usb_device *dev)
916 {
917 	struct device *ddev = &dev->dev;
918 	int ncfg = dev->descriptor.bNumConfigurations;
919 	unsigned int cfgno, length;
920 	unsigned char *bigbuffer;
921 	struct usb_config_descriptor *desc;
922 	int result;
923 
924 	if (ncfg > USB_MAXCONFIG) {
925 		dev_notice(ddev, "too many configurations: %d, "
926 		    "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
927 		dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
928 	}
929 
930 	if (ncfg < 1) {
931 		dev_err(ddev, "no configurations\n");
932 		return -EINVAL;
933 	}
934 
935 	length = ncfg * sizeof(struct usb_host_config);
936 	dev->config = kzalloc(length, GFP_KERNEL);
937 	if (!dev->config)
938 		return -ENOMEM;
939 
940 	length = ncfg * sizeof(char *);
941 	dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
942 	if (!dev->rawdescriptors)
943 		return -ENOMEM;
944 
945 	desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
946 	if (!desc)
947 		return -ENOMEM;
948 
949 	for (cfgno = 0; cfgno < ncfg; cfgno++) {
950 		/* We grab just the first descriptor so we know how long
951 		 * the whole configuration is */
952 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
953 		    desc, USB_DT_CONFIG_SIZE);
954 		if (result < 0) {
955 			dev_err(ddev, "unable to read config index %d "
956 			    "descriptor/%s: %d\n", cfgno, "start", result);
957 			if (result != -EPIPE)
958 				goto err;
959 			dev_notice(ddev, "chopping to %d config(s)\n", cfgno);
960 			dev->descriptor.bNumConfigurations = cfgno;
961 			break;
962 		} else if (result < 4) {
963 			dev_err(ddev, "config index %d descriptor too short "
964 			    "(expected %i, got %i)\n", cfgno,
965 			    USB_DT_CONFIG_SIZE, result);
966 			result = -EINVAL;
967 			goto err;
968 		}
969 		length = max_t(int, le16_to_cpu(desc->wTotalLength),
970 		    USB_DT_CONFIG_SIZE);
971 
972 		/* Now that we know the length, get the whole thing */
973 		bigbuffer = kmalloc(length, GFP_KERNEL);
974 		if (!bigbuffer) {
975 			result = -ENOMEM;
976 			goto err;
977 		}
978 
979 		if (dev->quirks & USB_QUIRK_DELAY_INIT)
980 			msleep(200);
981 
982 		result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
983 		    bigbuffer, length);
984 		if (result < 0) {
985 			dev_err(ddev, "unable to read config index %d "
986 			    "descriptor/%s\n", cfgno, "all");
987 			kfree(bigbuffer);
988 			goto err;
989 		}
990 		if (result < length) {
991 			dev_notice(ddev, "config index %d descriptor too short "
992 			    "(expected %i, got %i)\n", cfgno, length, result);
993 			length = result;
994 		}
995 
996 		dev->rawdescriptors[cfgno] = bigbuffer;
997 
998 		result = usb_parse_configuration(dev, cfgno,
999 		    &dev->config[cfgno], bigbuffer, length);
1000 		if (result < 0) {
1001 			++cfgno;
1002 			goto err;
1003 		}
1004 	}
1005 
1006 err:
1007 	kfree(desc);
1008 	dev->descriptor.bNumConfigurations = cfgno;
1009 
1010 	return result;
1011 }
1012 
usb_release_bos_descriptor(struct usb_device * dev)1013 void usb_release_bos_descriptor(struct usb_device *dev)
1014 {
1015 	if (dev->bos) {
1016 		kfree(dev->bos->desc);
1017 		kfree(dev->bos);
1018 		dev->bos = NULL;
1019 	}
1020 }
1021 
1022 static const __u8 bos_desc_len[256] = {
1023 	[USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE,
1024 	[USB_CAP_TYPE_EXT]          = USB_DT_USB_EXT_CAP_SIZE,
1025 	[USB_SS_CAP_TYPE]           = USB_DT_USB_SS_CAP_SIZE,
1026 	[USB_SSP_CAP_TYPE]          = USB_DT_USB_SSP_CAP_SIZE(1),
1027 	[CONTAINER_ID_TYPE]         = USB_DT_USB_SS_CONTN_ID_SIZE,
1028 	[USB_PTM_CAP_TYPE]          = USB_DT_USB_PTM_ID_SIZE,
1029 };
1030 
1031 /* Get BOS descriptor set */
usb_get_bos_descriptor(struct usb_device * dev)1032 int usb_get_bos_descriptor(struct usb_device *dev)
1033 {
1034 	struct device *ddev = &dev->dev;
1035 	struct usb_bos_descriptor *bos;
1036 	struct usb_dev_cap_header *cap;
1037 	struct usb_ssp_cap_descriptor *ssp_cap;
1038 	unsigned char *buffer, *buffer0;
1039 	int length, total_len, num, i, ssac;
1040 	__u8 cap_type;
1041 	int ret;
1042 
1043 	bos = kzalloc(sizeof(*bos), GFP_KERNEL);
1044 	if (!bos)
1045 		return -ENOMEM;
1046 
1047 	/* Get BOS descriptor */
1048 	ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
1049 	if (ret < USB_DT_BOS_SIZE || bos->bLength < USB_DT_BOS_SIZE) {
1050 		dev_notice(ddev, "unable to get BOS descriptor or descriptor too short\n");
1051 		if (ret >= 0)
1052 			ret = -ENOMSG;
1053 		kfree(bos);
1054 		return ret;
1055 	}
1056 
1057 	length = bos->bLength;
1058 	total_len = le16_to_cpu(bos->wTotalLength);
1059 	num = bos->bNumDeviceCaps;
1060 	kfree(bos);
1061 	if (total_len < length)
1062 		return -EINVAL;
1063 
1064 	dev->bos = kzalloc(sizeof(*dev->bos), GFP_KERNEL);
1065 	if (!dev->bos)
1066 		return -ENOMEM;
1067 
1068 	/* Now let's get the whole BOS descriptor set */
1069 	buffer = kzalloc(total_len, GFP_KERNEL);
1070 	if (!buffer) {
1071 		ret = -ENOMEM;
1072 		goto err;
1073 	}
1074 	dev->bos->desc = (struct usb_bos_descriptor *)buffer;
1075 
1076 	ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
1077 	if (ret < total_len) {
1078 		dev_notice(ddev, "unable to get BOS descriptor set\n");
1079 		if (ret >= 0)
1080 			ret = -ENOMSG;
1081 		goto err;
1082 	}
1083 
1084 	buffer0 = buffer;
1085 	total_len -= length;
1086 	buffer += length;
1087 
1088 	for (i = 0; i < num; i++) {
1089 		cap = (struct usb_dev_cap_header *)buffer;
1090 
1091 		if (total_len < sizeof(*cap) || total_len < cap->bLength) {
1092 			dev->bos->desc->bNumDeviceCaps = i;
1093 			break;
1094 		}
1095 		cap_type = cap->bDevCapabilityType;
1096 		length = cap->bLength;
1097 		if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) {
1098 			dev->bos->desc->bNumDeviceCaps = i;
1099 			break;
1100 		}
1101 
1102 		if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
1103 			dev_notice(ddev, "descriptor type invalid, skip\n");
1104 			goto skip_to_next_descriptor;
1105 		}
1106 
1107 		switch (cap_type) {
1108 		case USB_CAP_TYPE_EXT:
1109 			dev->bos->ext_cap =
1110 				(struct usb_ext_cap_descriptor *)buffer;
1111 			break;
1112 		case USB_SS_CAP_TYPE:
1113 			dev->bos->ss_cap =
1114 				(struct usb_ss_cap_descriptor *)buffer;
1115 			break;
1116 		case USB_SSP_CAP_TYPE:
1117 			ssp_cap = (struct usb_ssp_cap_descriptor *)buffer;
1118 			ssac = (le32_to_cpu(ssp_cap->bmAttributes) &
1119 				USB_SSP_SUBLINK_SPEED_ATTRIBS);
1120 			if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac))
1121 				dev->bos->ssp_cap = ssp_cap;
1122 			break;
1123 		case CONTAINER_ID_TYPE:
1124 			dev->bos->ss_id =
1125 				(struct usb_ss_container_id_descriptor *)buffer;
1126 			break;
1127 		case USB_PTM_CAP_TYPE:
1128 			dev->bos->ptm_cap =
1129 				(struct usb_ptm_cap_descriptor *)buffer;
1130 			break;
1131 		default:
1132 			break;
1133 		}
1134 
1135 skip_to_next_descriptor:
1136 		total_len -= length;
1137 		buffer += length;
1138 	}
1139 	dev->bos->desc->wTotalLength = cpu_to_le16(buffer - buffer0);
1140 
1141 	return 0;
1142 
1143 err:
1144 	usb_release_bos_descriptor(dev);
1145 	return ret;
1146 }
1147