blob: 5eb1a87228b47afd397fecfd4180413e7043a304 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/usb.h>
Alan Stern615ae112007-06-08 15:23:27 -04002#include <linux/usb/ch9.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +02003#include <linux/usb/hcd.h>
Hans de Goede317149c2010-03-29 12:03:17 +02004#include <linux/usb/quirks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/slab.h>
7#include <linux/device.h>
8#include <asm/byteorder.h>
Greg KH6d5e8252005-04-18 17:39:24 -07009#include "usb.h"
Eric Lescouet27729aa2010-04-24 23:21:52 +020010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#define USB_MAXALTSETTING 128 /* Hard limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#define USB_MAXCONFIG 8 /* Arbitrary limit */
15
16
17static inline const char *plural(int n)
18{
19 return (n == 1 ? "" : "s");
20}
21
22static 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
Mathias Nymanb37d83a2016-02-12 16:40:13 +020046static 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
Alan Stern842f1692010-04-30 12:44:46 -040067static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
Sarah Sharp663c30d2009-04-27 19:58:14 -070068 int inum, int asnum, struct usb_host_endpoint *ep,
Alan Stern842f1692010-04-30 12:44:46 -040069 unsigned char *buffer, int size)
Sarah Sharp663c30d2009-04-27 19:58:14 -070070{
Alan Stern842f1692010-04-30 12:44:46 -040071 struct usb_ss_ep_comp_descriptor *desc;
Sarah Sharp663c30d2009-04-27 19:58:14 -070072 int max_tx;
Sarah Sharp663c30d2009-04-27 19:58:14 -070073
Alan Stern842f1692010-04-30 12:44:46 -040074 /* The SuperSpeed endpoint companion descriptor is supposed to
75 * be the first thing immediately following the endpoint descriptor.
76 */
Sarah Sharpf0058c62009-04-29 19:06:20 -070077 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
Mathias Nymanb37d83a2016-02-12 16:40:13 +020078 buffer += desc->bLength;
79 size -= desc->bLength;
80
Alan Stern842f1692010-04-30 12:44:46 -040081 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
82 size < USB_DT_SS_EP_COMP_SIZE) {
Sarah Sharp663c30d2009-04-27 19:58:14 -070083 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);
Sarah Sharp663c30d2009-04-27 19:58:14 -070087
Alan Stern842f1692010-04-30 12:44:46 -040088 /* 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);
Sarah Sharp663c30d2009-04-27 19:58:14 -0700105
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);
Alan Stern842f1692010-04-30 12:44:46 -0400112 ep->ss_ep_comp.bMaxBurst = 0;
113 } else if (desc->bMaxBurst > 15) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700114 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);
Alan Stern842f1692010-04-30 12:44:46 -0400118 ep->ss_ep_comp.bMaxBurst = 15;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700119 }
Alan Stern842f1692010-04-30 12:44:46 -0400120
121 if ((usb_endpoint_xfer_control(&ep->desc) ||
122 usb_endpoint_xfer_int(&ep->desc)) &&
123 desc->bmAttributes != 0) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700124 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);
Alan Stern842f1692010-04-30 12:44:46 -0400130 ep->ss_ep_comp.bmAttributes = 0;
131 } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
132 desc->bmAttributes > 16) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700133 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);
Alan Stern842f1692010-04-30 12:44:46 -0400137 ep->ss_ep_comp.bmAttributes = 16;
138 } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
Mathias Nymanb37d83a2016-02-12 16:40:13 +0200139 !USB_SS_SSP_ISOC_COMP(desc->bmAttributes) &&
Mathias Nymanff30cbc2015-09-21 17:46:09 +0300140 USB_SS_MULT(desc->bmAttributes) > 3) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700141 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
142 "config %d interface %d altsetting %d ep %d: "
Ben Hutchings5377adb2015-11-18 02:01:21 +0000143 "setting to 3\n",
144 USB_SS_MULT(desc->bmAttributes),
Sarah Sharp663c30d2009-04-27 19:58:14 -0700145 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Alan Stern842f1692010-04-30 12:44:46 -0400146 ep->ss_ep_comp.bmAttributes = 2;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700147 }
Alan Stern842f1692010-04-30 12:44:46 -0400148
Mathias Nymanb37d83a2016-02-12 16:40:13 +0200149 /* 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
Alan Stern842f1692010-04-30 12:44:46 -0400155 if (usb_endpoint_xfer_isoc(&ep->desc))
Mathias Nymanff30cbc2015-09-21 17:46:09 +0300156 max_tx = (desc->bMaxBurst + 1) *
157 (USB_SS_MULT(desc->bmAttributes)) *
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700158 usb_endpoint_maxp(&ep->desc);
Alan Stern842f1692010-04-30 12:44:46 -0400159 else if (usb_endpoint_xfer_int(&ep->desc))
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700160 max_tx = usb_endpoint_maxp(&ep->desc) *
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200161 (desc->bMaxBurst + 1);
Alan Stern842f1692010-04-30 12:44:46 -0400162 else
163 max_tx = 999999;
Sebastian Andrzej Siewior64b3c302011-04-11 20:19:12 +0200164 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700165 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",
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200169 le16_to_cpu(desc->wBytesPerInterval),
Sarah Sharp663c30d2009-04-27 19:58:14 -0700170 cfgno, inum, asnum, ep->desc.bEndpointAddress,
171 max_tx);
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200172 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
Sarah Sharp663c30d2009-04-27 19:58:14 -0700173 }
Sarah Sharp663c30d2009-04-27 19:58:14 -0700174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static 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;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700183 int n, i, j, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
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
Laurent Pinchart300871c2007-06-12 21:47:17 +0200218 /* Fix up bInterval values outside the legal range. Use 32 ms if no
219 * proper value can be guessed. */
Alan Stern615ae112007-06-08 15:23:27 -0400220 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) {
Mathias Nyman8a1b2722015-12-10 09:59:25 +0200225 case USB_SPEED_SUPER_PLUS:
Sarah Sharp6b403b02009-04-27 19:54:10 -0700226 case USB_SPEED_SUPER:
Alan Stern615ae112007-06-08 15:23:27 -0400227 case USB_SPEED_HIGH:
Laurent Pinchart300871c2007-06-12 21:47:17 +0200228 /* 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 */
Alan Stern615ae112007-06-08 15:23:27 -0400235 j = 16;
James P Michels IIIcd83ce92014-07-27 13:28:04 -0400236
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 }
Alan Stern615ae112007-06-08 15:23:27 -0400247 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
Alan Stern60aac1e2007-06-08 15:25:02 -0400276 /* 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;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700287 if (usb_endpoint_maxp(&endpoint->desc) > 8)
Alan Stern60aac1e2007-06-08 15:25:02 -0400288 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
289 }
290
David Brownellcaa9ef62008-02-08 15:08:44 -0800291 /*
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
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700300 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
David Brownellcaa9ef62008-02-08 15:08:44 -0800301 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
Alan Stern842f1692010-04-30 12:44:46 -0400308 /* Parse a possible SuperSpeed endpoint companion descriptor */
Mathias Nyman8a1b2722015-12-10 09:59:25 +0200309 if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
Alan Stern842f1692010-04-30 12:44:46 -0400310 usb_parse_ss_endpoint_companion(ddev, cfgno,
311 inum, asnum, endpoint, buffer, size);
Sarah Sharp9f8e4432009-07-27 12:04:52 -0700312
Alan Stern842f1692010-04-30 12:44:46 -0400313 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (n > 0)
321 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
322 n, plural(n), "endpoint");
Sarah Sharp663c30d2009-04-27 19:58:14 -0700323 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325skip_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
331void usb_release_interface_cache(struct kref *ref)
332{
333 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
334 int j;
335
Alan Stern4f62efe2005-10-24 16:24:14 -0400336 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 kfree(intfc);
343}
344
345static 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;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800407 alt->desc.bNumEndpoints = 0; /* Use as a counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 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
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800415 if (num_ep > 0) {
416 /* Can't allocate 0 bytes */
Alan Stern57a21c12007-05-15 17:40:37 -0400417 len = sizeof(struct usb_host_endpoint) * num_ep;
418 alt->endpoint = kzalloc(len, GFP_KERNEL);
419 if (!alt->endpoint)
420 return -ENOMEM;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
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
446skip_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
Hans de Goede317149c2010-03-29 12:03:17 +0200452static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct usb_host_config *config, unsigned char *buffer, int size)
454{
Hans de Goede317149c2010-03-29 12:03:17 +0200455 struct device *ddev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 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];
Craig W. Nadler165fe972007-06-15 23:14:35 -0400466 unsigned iad_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
469 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
Hans de Goedeb4f17a482013-08-03 16:37:48 +0200470 config->desc.bLength < USB_DT_CONFIG_SIZE ||
471 config->desc.bLength > size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 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;
Hans de Goede317149c2010-03-29 12:03:17 +0200525
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 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
Craig W. Nadler165fe972007-06-15 23:14:35 -0400555 } 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 } 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;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400610 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!intfc)
612 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 kref_init(&intfc->ref);
614 }
615
Sarah Sharp663c30d2009-04-27 19:58:14 -0700616 /* FIXME: parse the BOS descriptor */
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* 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
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800659/* hub-only!! ... and only exported for reset/reinit path.
660 * otherwise used internally on disconnect/destroy path
661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662void 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
682 if (cf->intf_cache[i])
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800683 kref_put(&cf->intf_cache[i]->ref,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 usb_release_interface_cache);
685 }
686 }
687 kfree(dev->config);
688 dev->config = NULL;
689}
690
691
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700692/*
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)
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698int usb_get_configuration(struct usb_device *dev)
699{
700 struct device *ddev = &dev->dev;
701 int ncfg = dev->descriptor.bNumConfigurations;
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700702 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 unsigned int cfgno, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 unsigned char *bigbuffer;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800705 struct usb_config_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700707 cfgno = 0;
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700708 result = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 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);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400721 dev->config = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!dev->config)
723 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 length = ncfg * sizeof(char *);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400726 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!dev->rawdescriptors)
728 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200730 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
731 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700734 result = 0;
735 for (; cfgno < ncfg; cfgno++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* 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,
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200739 desc, USB_DT_CONFIG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (result < 0) {
741 dev_err(ddev, "unable to read config index %d "
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700742 "descriptor/%s: %d\n", cfgno, "start", result);
Lan Tianyu3a22b872012-09-05 13:44:37 +0800743 if (result != -EPIPE)
744 goto err;
Inaky Perez-Gonzalezcb4c8fe2006-08-25 19:35:28 -0700745 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
746 dev->descriptor.bNumConfigurations = cfgno;
747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } 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 }
Julius Wernerd86db252014-03-04 11:27:38 -0800764
765 if (dev->quirks & USB_QUIRK_DELAY_INIT)
766 msleep(100);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 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
Hans de Goede317149c2010-03-29 12:03:17 +0200784 result = usb_parse_configuration(dev, cfgno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 &dev->config[cfgno], bigbuffer, length);
786 if (result < 0) {
787 ++cfgno;
788 goto err;
789 }
790 }
791 result = 0;
792
793err:
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200794 kfree(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dev->descriptor.bNumConfigurations = cfgno;
796err2:
797 if (result == -ENOMEM)
798 dev_err(ddev, "out of memory\n");
799 return result;
800}
Andiry Xu3148bf02011-09-23 14:19:47 -0700801
802void 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 */
812int 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;
Mathias Nyman3220bef2015-10-01 18:40:33 +0300889 case USB_SSP_CAP_TYPE:
890 dev->bos->ssp_cap =
891 (struct usb_ssp_cap_descriptor *)buffer;
892 break;
Andiry Xu3148bf02011-09-23 14:19:47 -0700893 case CONTAINER_ID_TYPE:
894 dev->bos->ss_id =
895 (struct usb_ss_container_id_descriptor *)buffer;
896 break;
Mathias Nymanfaee8222016-02-12 16:40:14 +0200897 case USB_PTM_CAP_TYPE:
898 dev->bos->ptm_cap =
899 (struct usb_ptm_cap_descriptor *)buffer;
Andiry Xu3148bf02011-09-23 14:19:47 -0700900 default:
901 break;
902 }
903 }
904
905 return 0;
906
907err:
908 usb_release_bos_descriptor(dev);
909 return ret;
910}