blob: 7caff020106e23f26f9c3bb9c34585885064848d [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
Alan Stern842f1692010-04-30 12:44:46 -040046static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
Sarah Sharp663c30d2009-04-27 19:58:14 -070047 int inum, int asnum, struct usb_host_endpoint *ep,
Alan Stern842f1692010-04-30 12:44:46 -040048 unsigned char *buffer, int size)
Sarah Sharp663c30d2009-04-27 19:58:14 -070049{
Alan Stern842f1692010-04-30 12:44:46 -040050 struct usb_ss_ep_comp_descriptor *desc;
Sarah Sharp663c30d2009-04-27 19:58:14 -070051 int max_tx;
Sarah Sharp663c30d2009-04-27 19:58:14 -070052
Alan Stern842f1692010-04-30 12:44:46 -040053 /* The SuperSpeed endpoint companion descriptor is supposed to
54 * be the first thing immediately following the endpoint descriptor.
55 */
Sarah Sharpf0058c62009-04-29 19:06:20 -070056 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
Alan Stern842f1692010-04-30 12:44:46 -040057 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
58 size < USB_DT_SS_EP_COMP_SIZE) {
Sarah Sharp663c30d2009-04-27 19:58:14 -070059 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
60 " interface %d altsetting %d ep %d: "
61 "using minimum values\n",
62 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Sarah Sharp663c30d2009-04-27 19:58:14 -070063
Alan Stern842f1692010-04-30 12:44:46 -040064 /* Fill in some default values.
65 * Leave bmAttributes as zero, which will mean no streams for
66 * bulk, and isoc won't support multiple bursts of packets.
67 * With bursts of only one packet, and a Mult of 1, the max
68 * amount of data moved per endpoint service interval is one
69 * packet.
70 */
71 ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
72 ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
73 if (usb_endpoint_xfer_isoc(&ep->desc) ||
74 usb_endpoint_xfer_int(&ep->desc))
75 ep->ss_ep_comp.wBytesPerInterval =
76 ep->desc.wMaxPacketSize;
77 return;
78 }
79
80 memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
Sarah Sharp663c30d2009-04-27 19:58:14 -070081
82 /* Check the various values */
83 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
84 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
85 "config %d interface %d altsetting %d ep %d: "
86 "setting to zero\n", desc->bMaxBurst,
87 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Alan Stern842f1692010-04-30 12:44:46 -040088 ep->ss_ep_comp.bMaxBurst = 0;
89 } else if (desc->bMaxBurst > 15) {
Sarah Sharp663c30d2009-04-27 19:58:14 -070090 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
91 "config %d interface %d altsetting %d ep %d: "
92 "setting to 15\n", desc->bMaxBurst,
93 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Alan Stern842f1692010-04-30 12:44:46 -040094 ep->ss_ep_comp.bMaxBurst = 15;
Sarah Sharp663c30d2009-04-27 19:58:14 -070095 }
Alan Stern842f1692010-04-30 12:44:46 -040096
97 if ((usb_endpoint_xfer_control(&ep->desc) ||
98 usb_endpoint_xfer_int(&ep->desc)) &&
99 desc->bmAttributes != 0) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700100 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
101 "config %d interface %d altsetting %d ep %d: "
102 "setting to zero\n",
103 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
104 desc->bmAttributes,
105 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Alan Stern842f1692010-04-30 12:44:46 -0400106 ep->ss_ep_comp.bmAttributes = 0;
107 } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
108 desc->bmAttributes > 16) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700109 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
110 "config %d interface %d altsetting %d ep %d: "
111 "setting to max\n",
112 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Alan Stern842f1692010-04-30 12:44:46 -0400113 ep->ss_ep_comp.bmAttributes = 16;
114 } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
Mathias Nymanff30cbc2015-09-21 17:46:09 +0300115 USB_SS_MULT(desc->bmAttributes) > 3) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700116 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
117 "config %d interface %d altsetting %d ep %d: "
118 "setting to 3\n", desc->bmAttributes + 1,
119 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Alan Stern842f1692010-04-30 12:44:46 -0400120 ep->ss_ep_comp.bmAttributes = 2;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700121 }
Alan Stern842f1692010-04-30 12:44:46 -0400122
123 if (usb_endpoint_xfer_isoc(&ep->desc))
Mathias Nymanff30cbc2015-09-21 17:46:09 +0300124 max_tx = (desc->bMaxBurst + 1) *
125 (USB_SS_MULT(desc->bmAttributes)) *
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700126 usb_endpoint_maxp(&ep->desc);
Alan Stern842f1692010-04-30 12:44:46 -0400127 else if (usb_endpoint_xfer_int(&ep->desc))
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700128 max_tx = usb_endpoint_maxp(&ep->desc) *
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200129 (desc->bMaxBurst + 1);
Alan Stern842f1692010-04-30 12:44:46 -0400130 else
131 max_tx = 999999;
Sebastian Andrzej Siewior64b3c302011-04-11 20:19:12 +0200132 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700133 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
134 "config %d interface %d altsetting %d ep %d: "
135 "setting to %d\n",
136 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200137 le16_to_cpu(desc->wBytesPerInterval),
Sarah Sharp663c30d2009-04-27 19:58:14 -0700138 cfgno, inum, asnum, ep->desc.bEndpointAddress,
139 max_tx);
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200140 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
Sarah Sharp663c30d2009-04-27 19:58:14 -0700141 }
Sarah Sharp663c30d2009-04-27 19:58:14 -0700142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
145 int asnum, struct usb_host_interface *ifp, int num_ep,
146 unsigned char *buffer, int size)
147{
148 unsigned char *buffer0 = buffer;
149 struct usb_endpoint_descriptor *d;
150 struct usb_host_endpoint *endpoint;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700151 int n, i, j, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 d = (struct usb_endpoint_descriptor *) buffer;
154 buffer += d->bLength;
155 size -= d->bLength;
156
157 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
158 n = USB_DT_ENDPOINT_AUDIO_SIZE;
159 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
160 n = USB_DT_ENDPOINT_SIZE;
161 else {
162 dev_warn(ddev, "config %d interface %d altsetting %d has an "
163 "invalid endpoint descriptor of length %d, skipping\n",
164 cfgno, inum, asnum, d->bLength);
165 goto skip_to_next_endpoint_or_interface_descriptor;
166 }
167
168 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
169 if (i >= 16 || i == 0) {
170 dev_warn(ddev, "config %d interface %d altsetting %d has an "
171 "invalid endpoint with address 0x%X, skipping\n",
172 cfgno, inum, asnum, d->bEndpointAddress);
173 goto skip_to_next_endpoint_or_interface_descriptor;
174 }
175
176 /* Only store as many endpoints as we have room for */
177 if (ifp->desc.bNumEndpoints >= num_ep)
178 goto skip_to_next_endpoint_or_interface_descriptor;
179
180 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
181 ++ifp->desc.bNumEndpoints;
182
183 memcpy(&endpoint->desc, d, n);
184 INIT_LIST_HEAD(&endpoint->urb_list);
185
Laurent Pinchart300871c2007-06-12 21:47:17 +0200186 /* Fix up bInterval values outside the legal range. Use 32 ms if no
187 * proper value can be guessed. */
Alan Stern615ae112007-06-08 15:23:27 -0400188 i = 0; /* i = min, j = max, n = default */
189 j = 255;
190 if (usb_endpoint_xfer_int(d)) {
191 i = 1;
192 switch (to_usb_device(ddev)->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -0700193 case USB_SPEED_SUPER:
Alan Stern615ae112007-06-08 15:23:27 -0400194 case USB_SPEED_HIGH:
Laurent Pinchart300871c2007-06-12 21:47:17 +0200195 /* Many device manufacturers are using full-speed
196 * bInterval values in high-speed interrupt endpoint
197 * descriptors. Try to fix those and fall back to a
198 * 32 ms default value otherwise. */
199 n = fls(d->bInterval*8);
200 if (n == 0)
201 n = 9; /* 32 ms = 2^(9-1) uframes */
Alan Stern615ae112007-06-08 15:23:27 -0400202 j = 16;
James P Michels IIIcd83ce92014-07-27 13:28:04 -0400203
204 /*
205 * Adjust bInterval for quirked devices.
206 * This quirk fixes bIntervals reported in
207 * linear microframes.
208 */
209 if (to_usb_device(ddev)->quirks &
210 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
211 n = clamp(fls(d->bInterval), i, j);
212 i = j = n;
213 }
Alan Stern615ae112007-06-08 15:23:27 -0400214 break;
215 default: /* USB_SPEED_FULL or _LOW */
216 /* For low-speed, 10 ms is the official minimum.
217 * But some "overclocked" devices might want faster
218 * polling so we'll allow it. */
219 n = 32;
220 break;
221 }
222 } else if (usb_endpoint_xfer_isoc(d)) {
223 i = 1;
224 j = 16;
225 switch (to_usb_device(ddev)->speed) {
226 case USB_SPEED_HIGH:
227 n = 9; /* 32 ms = 2^(9-1) uframes */
228 break;
229 default: /* USB_SPEED_FULL */
230 n = 6; /* 32 ms = 2^(6-1) frames */
231 break;
232 }
233 }
234 if (d->bInterval < i || d->bInterval > j) {
235 dev_warn(ddev, "config %d interface %d altsetting %d "
236 "endpoint 0x%X has an invalid bInterval %d, "
237 "changing to %d\n",
238 cfgno, inum, asnum,
239 d->bEndpointAddress, d->bInterval, n);
240 endpoint->desc.bInterval = n;
241 }
242
Alan Stern60aac1e2007-06-08 15:25:02 -0400243 /* Some buggy low-speed devices have Bulk endpoints, which is
244 * explicitly forbidden by the USB spec. In an attempt to make
245 * them usable, we will try treating them as Interrupt endpoints.
246 */
247 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
248 usb_endpoint_xfer_bulk(d)) {
249 dev_warn(ddev, "config %d interface %d altsetting %d "
250 "endpoint 0x%X is Bulk; changing to Interrupt\n",
251 cfgno, inum, asnum, d->bEndpointAddress);
252 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
253 endpoint->desc.bInterval = 1;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700254 if (usb_endpoint_maxp(&endpoint->desc) > 8)
Alan Stern60aac1e2007-06-08 15:25:02 -0400255 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
256 }
257
David Brownellcaa9ef62008-02-08 15:08:44 -0800258 /*
259 * Some buggy high speed devices have bulk endpoints using
260 * maxpacket sizes other than 512. High speed HCDs may not
261 * be able to handle that particular bug, so let's warn...
262 */
263 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
264 && usb_endpoint_xfer_bulk(d)) {
265 unsigned maxp;
266
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700267 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
David Brownellcaa9ef62008-02-08 15:08:44 -0800268 if (maxp != 512)
269 dev_warn(ddev, "config %d interface %d altsetting %d "
270 "bulk endpoint 0x%X has invalid maxpacket %d\n",
271 cfgno, inum, asnum, d->bEndpointAddress,
272 maxp);
273 }
274
Alan Stern842f1692010-04-30 12:44:46 -0400275 /* Parse a possible SuperSpeed endpoint companion descriptor */
276 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER)
277 usb_parse_ss_endpoint_companion(ddev, cfgno,
278 inum, asnum, endpoint, buffer, size);
Sarah Sharp9f8e4432009-07-27 12:04:52 -0700279
Alan Stern842f1692010-04-30 12:44:46 -0400280 /* Skip over any Class Specific or Vendor Specific descriptors;
281 * find the next endpoint or interface descriptor */
282 endpoint->extra = buffer;
283 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
284 USB_DT_INTERFACE, &n);
285 endpoint->extralen = i;
286 retval = buffer - buffer0 + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (n > 0)
288 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
289 n, plural(n), "endpoint");
Sarah Sharp663c30d2009-04-27 19:58:14 -0700290 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292skip_to_next_endpoint_or_interface_descriptor:
293 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
294 USB_DT_INTERFACE, NULL);
295 return buffer - buffer0 + i;
296}
297
298void usb_release_interface_cache(struct kref *ref)
299{
300 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
301 int j;
302
Alan Stern4f62efe2005-10-24 16:24:14 -0400303 for (j = 0; j < intfc->num_altsetting; j++) {
304 struct usb_host_interface *alt = &intfc->altsetting[j];
305
306 kfree(alt->endpoint);
307 kfree(alt->string);
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 kfree(intfc);
310}
311
312static int usb_parse_interface(struct device *ddev, int cfgno,
313 struct usb_host_config *config, unsigned char *buffer, int size,
314 u8 inums[], u8 nalts[])
315{
316 unsigned char *buffer0 = buffer;
317 struct usb_interface_descriptor *d;
318 int inum, asnum;
319 struct usb_interface_cache *intfc;
320 struct usb_host_interface *alt;
321 int i, n;
322 int len, retval;
323 int num_ep, num_ep_orig;
324
325 d = (struct usb_interface_descriptor *) buffer;
326 buffer += d->bLength;
327 size -= d->bLength;
328
329 if (d->bLength < USB_DT_INTERFACE_SIZE)
330 goto skip_to_next_interface_descriptor;
331
332 /* Which interface entry is this? */
333 intfc = NULL;
334 inum = d->bInterfaceNumber;
335 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
336 if (inums[i] == inum) {
337 intfc = config->intf_cache[i];
338 break;
339 }
340 }
341 if (!intfc || intfc->num_altsetting >= nalts[i])
342 goto skip_to_next_interface_descriptor;
343
344 /* Check for duplicate altsetting entries */
345 asnum = d->bAlternateSetting;
346 for ((i = 0, alt = &intfc->altsetting[0]);
347 i < intfc->num_altsetting;
348 (++i, ++alt)) {
349 if (alt->desc.bAlternateSetting == asnum) {
350 dev_warn(ddev, "Duplicate descriptor for config %d "
351 "interface %d altsetting %d, skipping\n",
352 cfgno, inum, asnum);
353 goto skip_to_next_interface_descriptor;
354 }
355 }
356
357 ++intfc->num_altsetting;
358 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
359
360 /* Skip over any Class Specific or Vendor Specific descriptors;
361 * find the first endpoint or interface descriptor */
362 alt->extra = buffer;
363 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
364 USB_DT_INTERFACE, &n);
365 alt->extralen = i;
366 if (n > 0)
367 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
368 n, plural(n), "interface");
369 buffer += i;
370 size -= i;
371
372 /* Allocate space for the right(?) number of endpoints */
373 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800374 alt->desc.bNumEndpoints = 0; /* Use as a counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (num_ep > USB_MAXENDPOINTS) {
376 dev_warn(ddev, "too many endpoints for config %d interface %d "
377 "altsetting %d: %d, using maximum allowed: %d\n",
378 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
379 num_ep = USB_MAXENDPOINTS;
380 }
381
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800382 if (num_ep > 0) {
383 /* Can't allocate 0 bytes */
Alan Stern57a21c12007-05-15 17:40:37 -0400384 len = sizeof(struct usb_host_endpoint) * num_ep;
385 alt->endpoint = kzalloc(len, GFP_KERNEL);
386 if (!alt->endpoint)
387 return -ENOMEM;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* Parse all the endpoint descriptors */
391 n = 0;
392 while (size > 0) {
393 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
394 == USB_DT_INTERFACE)
395 break;
396 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
397 num_ep, buffer, size);
398 if (retval < 0)
399 return retval;
400 ++n;
401
402 buffer += retval;
403 size -= retval;
404 }
405
406 if (n != num_ep_orig)
407 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
408 "endpoint descriptor%s, different from the interface "
409 "descriptor's value: %d\n",
410 cfgno, inum, asnum, n, plural(n), num_ep_orig);
411 return buffer - buffer0;
412
413skip_to_next_interface_descriptor:
414 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
415 USB_DT_INTERFACE, NULL);
416 return buffer - buffer0 + i;
417}
418
Hans de Goede317149c2010-03-29 12:03:17 +0200419static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct usb_host_config *config, unsigned char *buffer, int size)
421{
Hans de Goede317149c2010-03-29 12:03:17 +0200422 struct device *ddev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 unsigned char *buffer0 = buffer;
424 int cfgno;
425 int nintf, nintf_orig;
426 int i, j, n;
427 struct usb_interface_cache *intfc;
428 unsigned char *buffer2;
429 int size2;
430 struct usb_descriptor_header *header;
431 int len, retval;
432 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
Craig W. Nadler165fe972007-06-15 23:14:35 -0400433 unsigned iad_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
436 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
Hans de Goedeb4f17a482013-08-03 16:37:48 +0200437 config->desc.bLength < USB_DT_CONFIG_SIZE ||
438 config->desc.bLength > size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 dev_err(ddev, "invalid descriptor for config index %d: "
440 "type = 0x%X, length = %d\n", cfgidx,
441 config->desc.bDescriptorType, config->desc.bLength);
442 return -EINVAL;
443 }
444 cfgno = config->desc.bConfigurationValue;
445
446 buffer += config->desc.bLength;
447 size -= config->desc.bLength;
448
449 nintf = nintf_orig = config->desc.bNumInterfaces;
450 if (nintf > USB_MAXINTERFACES) {
451 dev_warn(ddev, "config %d has too many interfaces: %d, "
452 "using maximum allowed: %d\n",
453 cfgno, nintf, USB_MAXINTERFACES);
454 nintf = USB_MAXINTERFACES;
455 }
456
457 /* Go through the descriptors, checking their length and counting the
458 * number of altsettings for each interface */
459 n = 0;
460 for ((buffer2 = buffer, size2 = size);
461 size2 > 0;
462 (buffer2 += header->bLength, size2 -= header->bLength)) {
463
464 if (size2 < sizeof(struct usb_descriptor_header)) {
465 dev_warn(ddev, "config %d descriptor has %d excess "
466 "byte%s, ignoring\n",
467 cfgno, size2, plural(size2));
468 break;
469 }
470
471 header = (struct usb_descriptor_header *) buffer2;
472 if ((header->bLength > size2) || (header->bLength < 2)) {
473 dev_warn(ddev, "config %d has an invalid descriptor "
474 "of length %d, skipping remainder of the config\n",
475 cfgno, header->bLength);
476 break;
477 }
478
479 if (header->bDescriptorType == USB_DT_INTERFACE) {
480 struct usb_interface_descriptor *d;
481 int inum;
482
483 d = (struct usb_interface_descriptor *) header;
484 if (d->bLength < USB_DT_INTERFACE_SIZE) {
485 dev_warn(ddev, "config %d has an invalid "
486 "interface descriptor of length %d, "
487 "skipping\n", cfgno, d->bLength);
488 continue;
489 }
490
491 inum = d->bInterfaceNumber;
Hans de Goede317149c2010-03-29 12:03:17 +0200492
493 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
494 n >= nintf_orig) {
495 dev_warn(ddev, "config %d has more interface "
496 "descriptors, than it declares in "
497 "bNumInterfaces, ignoring interface "
498 "number: %d\n", cfgno, inum);
499 continue;
500 }
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (inum >= nintf_orig)
503 dev_warn(ddev, "config %d has an invalid "
504 "interface number: %d but max is %d\n",
505 cfgno, inum, nintf_orig - 1);
506
507 /* Have we already encountered this interface?
508 * Count its altsettings */
509 for (i = 0; i < n; ++i) {
510 if (inums[i] == inum)
511 break;
512 }
513 if (i < n) {
514 if (nalts[i] < 255)
515 ++nalts[i];
516 } else if (n < USB_MAXINTERFACES) {
517 inums[n] = inum;
518 nalts[n] = 1;
519 ++n;
520 }
521
Craig W. Nadler165fe972007-06-15 23:14:35 -0400522 } else if (header->bDescriptorType ==
523 USB_DT_INTERFACE_ASSOCIATION) {
524 if (iad_num == USB_MAXIADS) {
525 dev_warn(ddev, "found more Interface "
526 "Association Descriptors "
527 "than allocated for in "
528 "configuration %d\n", cfgno);
529 } else {
530 config->intf_assoc[iad_num] =
531 (struct usb_interface_assoc_descriptor
532 *)header;
533 iad_num++;
534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else if (header->bDescriptorType == USB_DT_DEVICE ||
537 header->bDescriptorType == USB_DT_CONFIG)
538 dev_warn(ddev, "config %d contains an unexpected "
539 "descriptor of type 0x%X, skipping\n",
540 cfgno, header->bDescriptorType);
541
542 } /* for ((buffer2 = buffer, size2 = size); ...) */
543 size = buffer2 - buffer;
544 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
545
546 if (n != nintf)
547 dev_warn(ddev, "config %d has %d interface%s, different from "
548 "the descriptor's value: %d\n",
549 cfgno, n, plural(n), nintf_orig);
550 else if (n == 0)
551 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
552 config->desc.bNumInterfaces = nintf = n;
553
554 /* Check for missing interface numbers */
555 for (i = 0; i < nintf; ++i) {
556 for (j = 0; j < nintf; ++j) {
557 if (inums[j] == i)
558 break;
559 }
560 if (j >= nintf)
561 dev_warn(ddev, "config %d has no interface number "
562 "%d\n", cfgno, i);
563 }
564
565 /* Allocate the usb_interface_caches and altsetting arrays */
566 for (i = 0; i < nintf; ++i) {
567 j = nalts[i];
568 if (j > USB_MAXALTSETTING) {
569 dev_warn(ddev, "too many alternate settings for "
570 "config %d interface %d: %d, "
571 "using maximum allowed: %d\n",
572 cfgno, inums[i], j, USB_MAXALTSETTING);
573 nalts[i] = j = USB_MAXALTSETTING;
574 }
575
576 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400577 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!intfc)
579 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 kref_init(&intfc->ref);
581 }
582
Sarah Sharp663c30d2009-04-27 19:58:14 -0700583 /* FIXME: parse the BOS descriptor */
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Skip over any Class Specific or Vendor Specific descriptors;
586 * find the first interface descriptor */
587 config->extra = buffer;
588 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
589 USB_DT_INTERFACE, &n);
590 config->extralen = i;
591 if (n > 0)
592 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
593 n, plural(n), "configuration");
594 buffer += i;
595 size -= i;
596
597 /* Parse all the interface/altsetting descriptors */
598 while (size > 0) {
599 retval = usb_parse_interface(ddev, cfgno, config,
600 buffer, size, inums, nalts);
601 if (retval < 0)
602 return retval;
603
604 buffer += retval;
605 size -= retval;
606 }
607
608 /* Check for missing altsettings */
609 for (i = 0; i < nintf; ++i) {
610 intfc = config->intf_cache[i];
611 for (j = 0; j < intfc->num_altsetting; ++j) {
612 for (n = 0; n < intfc->num_altsetting; ++n) {
613 if (intfc->altsetting[n].desc.
614 bAlternateSetting == j)
615 break;
616 }
617 if (n >= intfc->num_altsetting)
618 dev_warn(ddev, "config %d interface %d has no "
619 "altsetting %d\n", cfgno, inums[i], j);
620 }
621 }
622
623 return 0;
624}
625
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800626/* hub-only!! ... and only exported for reset/reinit path.
627 * otherwise used internally on disconnect/destroy path
628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629void usb_destroy_configuration(struct usb_device *dev)
630{
631 int c, i;
632
633 if (!dev->config)
634 return;
635
636 if (dev->rawdescriptors) {
637 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
638 kfree(dev->rawdescriptors[i]);
639
640 kfree(dev->rawdescriptors);
641 dev->rawdescriptors = NULL;
642 }
643
644 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
645 struct usb_host_config *cf = &dev->config[c];
646
647 kfree(cf->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
649 if (cf->intf_cache[i])
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800650 kref_put(&cf->intf_cache[i]->ref,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 usb_release_interface_cache);
652 }
653 }
654 kfree(dev->config);
655 dev->config = NULL;
656}
657
658
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700659/*
660 * Get the USB config descriptors, cache and parse'em
661 *
662 * hub-only!! ... and only in reset path, or usb_new_device()
663 * (used by real hubs and virtual root hubs)
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665int usb_get_configuration(struct usb_device *dev)
666{
667 struct device *ddev = &dev->dev;
668 int ncfg = dev->descriptor.bNumConfigurations;
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700669 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned int cfgno, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 unsigned char *bigbuffer;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800672 struct usb_config_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700674 cfgno = 0;
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700675 result = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (ncfg > USB_MAXCONFIG) {
677 dev_warn(ddev, "too many configurations: %d, "
678 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
679 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
680 }
681
682 if (ncfg < 1) {
683 dev_err(ddev, "no configurations\n");
684 return -EINVAL;
685 }
686
687 length = ncfg * sizeof(struct usb_host_config);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400688 dev->config = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (!dev->config)
690 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 length = ncfg * sizeof(char *);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400693 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!dev->rawdescriptors)
695 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200697 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
698 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700701 result = 0;
702 for (; cfgno < ncfg; cfgno++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* We grab just the first descriptor so we know how long
704 * the whole configuration is */
705 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200706 desc, USB_DT_CONFIG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (result < 0) {
708 dev_err(ddev, "unable to read config index %d "
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700709 "descriptor/%s: %d\n", cfgno, "start", result);
Lan Tianyu3a22b872012-09-05 13:44:37 +0800710 if (result != -EPIPE)
711 goto err;
Inaky Perez-Gonzalezcb4c8fe2006-08-25 19:35:28 -0700712 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
713 dev->descriptor.bNumConfigurations = cfgno;
714 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 } else if (result < 4) {
716 dev_err(ddev, "config index %d descriptor too short "
717 "(expected %i, got %i)\n", cfgno,
718 USB_DT_CONFIG_SIZE, result);
719 result = -EINVAL;
720 goto err;
721 }
722 length = max((int) le16_to_cpu(desc->wTotalLength),
723 USB_DT_CONFIG_SIZE);
724
725 /* Now that we know the length, get the whole thing */
726 bigbuffer = kmalloc(length, GFP_KERNEL);
727 if (!bigbuffer) {
728 result = -ENOMEM;
729 goto err;
730 }
Julius Wernerd86db252014-03-04 11:27:38 -0800731
732 if (dev->quirks & USB_QUIRK_DELAY_INIT)
733 msleep(100);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
736 bigbuffer, length);
737 if (result < 0) {
738 dev_err(ddev, "unable to read config index %d "
739 "descriptor/%s\n", cfgno, "all");
740 kfree(bigbuffer);
741 goto err;
742 }
743 if (result < length) {
744 dev_warn(ddev, "config index %d descriptor too short "
745 "(expected %i, got %i)\n", cfgno, length, result);
746 length = result;
747 }
748
749 dev->rawdescriptors[cfgno] = bigbuffer;
750
Hans de Goede317149c2010-03-29 12:03:17 +0200751 result = usb_parse_configuration(dev, cfgno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 &dev->config[cfgno], bigbuffer, length);
753 if (result < 0) {
754 ++cfgno;
755 goto err;
756 }
757 }
758 result = 0;
759
760err:
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200761 kfree(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 dev->descriptor.bNumConfigurations = cfgno;
763err2:
764 if (result == -ENOMEM)
765 dev_err(ddev, "out of memory\n");
766 return result;
767}
Andiry Xu3148bf02011-09-23 14:19:47 -0700768
769void usb_release_bos_descriptor(struct usb_device *dev)
770{
771 if (dev->bos) {
772 kfree(dev->bos->desc);
773 kfree(dev->bos);
774 dev->bos = NULL;
775 }
776}
777
778/* Get BOS descriptor set */
779int usb_get_bos_descriptor(struct usb_device *dev)
780{
781 struct device *ddev = &dev->dev;
782 struct usb_bos_descriptor *bos;
783 struct usb_dev_cap_header *cap;
784 unsigned char *buffer;
785 int length, total_len, num, i;
786 int ret;
787
788 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
789 if (!bos)
790 return -ENOMEM;
791
792 /* Get BOS descriptor */
793 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
794 if (ret < USB_DT_BOS_SIZE) {
795 dev_err(ddev, "unable to get BOS descriptor\n");
796 if (ret >= 0)
797 ret = -ENOMSG;
798 kfree(bos);
799 return ret;
800 }
801
802 length = bos->bLength;
803 total_len = le16_to_cpu(bos->wTotalLength);
804 num = bos->bNumDeviceCaps;
805 kfree(bos);
806 if (total_len < length)
807 return -EINVAL;
808
809 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
810 if (!dev->bos)
811 return -ENOMEM;
812
813 /* Now let's get the whole BOS descriptor set */
814 buffer = kzalloc(total_len, GFP_KERNEL);
815 if (!buffer) {
816 ret = -ENOMEM;
817 goto err;
818 }
819 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
820
821 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
822 if (ret < total_len) {
823 dev_err(ddev, "unable to get BOS descriptor set\n");
824 if (ret >= 0)
825 ret = -ENOMSG;
826 goto err;
827 }
828 total_len -= length;
829
830 for (i = 0; i < num; i++) {
831 buffer += length;
832 cap = (struct usb_dev_cap_header *)buffer;
833 length = cap->bLength;
834
835 if (total_len < length)
836 break;
837 total_len -= length;
838
839 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
840 dev_warn(ddev, "descriptor type invalid, skip\n");
841 continue;
842 }
843
844 switch (cap->bDevCapabilityType) {
845 case USB_CAP_TYPE_WIRELESS_USB:
846 /* Wireless USB cap descriptor is handled by wusb */
847 break;
848 case USB_CAP_TYPE_EXT:
849 dev->bos->ext_cap =
850 (struct usb_ext_cap_descriptor *)buffer;
851 break;
852 case USB_SS_CAP_TYPE:
853 dev->bos->ss_cap =
854 (struct usb_ss_cap_descriptor *)buffer;
855 break;
Mathias Nyman3220bef2015-10-01 18:40:33 +0300856 case USB_SSP_CAP_TYPE:
857 dev->bos->ssp_cap =
858 (struct usb_ssp_cap_descriptor *)buffer;
859 break;
Andiry Xu3148bf02011-09-23 14:19:47 -0700860 case CONTAINER_ID_TYPE:
861 dev->bos->ss_id =
862 (struct usb_ss_container_id_descriptor *)buffer;
863 break;
864 default:
865 break;
866 }
867 }
868
869 return 0;
870
871err:
872 usb_release_bos_descriptor(dev);
873 return ret;
874}