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