blob: bbcf4009f99eaa5c0d6119e3c7016ca640cf7da2 [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: "
Ben Hutchings5377adb2015-11-18 02:01:21 +0000118 "setting to 3\n",
119 USB_SS_MULT(desc->bmAttributes),
Sarah Sharp663c30d2009-04-27 19:58:14 -0700120 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Alan Stern842f1692010-04-30 12:44:46 -0400121 ep->ss_ep_comp.bmAttributes = 2;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700122 }
Alan Stern842f1692010-04-30 12:44:46 -0400123
124 if (usb_endpoint_xfer_isoc(&ep->desc))
Mathias Nymanff30cbc2015-09-21 17:46:09 +0300125 max_tx = (desc->bMaxBurst + 1) *
126 (USB_SS_MULT(desc->bmAttributes)) *
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700127 usb_endpoint_maxp(&ep->desc);
Alan Stern842f1692010-04-30 12:44:46 -0400128 else if (usb_endpoint_xfer_int(&ep->desc))
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700129 max_tx = usb_endpoint_maxp(&ep->desc) *
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200130 (desc->bMaxBurst + 1);
Alan Stern842f1692010-04-30 12:44:46 -0400131 else
132 max_tx = 999999;
Sebastian Andrzej Siewior64b3c302011-04-11 20:19:12 +0200133 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
Sarah Sharp663c30d2009-04-27 19:58:14 -0700134 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
135 "config %d interface %d altsetting %d ep %d: "
136 "setting to %d\n",
137 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200138 le16_to_cpu(desc->wBytesPerInterval),
Sarah Sharp663c30d2009-04-27 19:58:14 -0700139 cfgno, inum, asnum, ep->desc.bEndpointAddress,
140 max_tx);
Sebastian Andrzej Siewior7de7c7d2011-07-29 11:05:45 +0200141 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
Sarah Sharp663c30d2009-04-27 19:58:14 -0700142 }
Sarah Sharp663c30d2009-04-27 19:58:14 -0700143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
146 int asnum, struct usb_host_interface *ifp, int num_ep,
147 unsigned char *buffer, int size)
148{
149 unsigned char *buffer0 = buffer;
150 struct usb_endpoint_descriptor *d;
151 struct usb_host_endpoint *endpoint;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700152 int n, i, j, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 d = (struct usb_endpoint_descriptor *) buffer;
155 buffer += d->bLength;
156 size -= d->bLength;
157
158 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
159 n = USB_DT_ENDPOINT_AUDIO_SIZE;
160 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
161 n = USB_DT_ENDPOINT_SIZE;
162 else {
163 dev_warn(ddev, "config %d interface %d altsetting %d has an "
164 "invalid endpoint descriptor of length %d, skipping\n",
165 cfgno, inum, asnum, d->bLength);
166 goto skip_to_next_endpoint_or_interface_descriptor;
167 }
168
169 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
170 if (i >= 16 || i == 0) {
171 dev_warn(ddev, "config %d interface %d altsetting %d has an "
172 "invalid endpoint with address 0x%X, skipping\n",
173 cfgno, inum, asnum, d->bEndpointAddress);
174 goto skip_to_next_endpoint_or_interface_descriptor;
175 }
176
177 /* Only store as many endpoints as we have room for */
178 if (ifp->desc.bNumEndpoints >= num_ep)
179 goto skip_to_next_endpoint_or_interface_descriptor;
180
181 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
182 ++ifp->desc.bNumEndpoints;
183
184 memcpy(&endpoint->desc, d, n);
185 INIT_LIST_HEAD(&endpoint->urb_list);
186
Laurent Pinchart300871c2007-06-12 21:47:17 +0200187 /* Fix up bInterval values outside the legal range. Use 32 ms if no
188 * proper value can be guessed. */
Alan Stern615ae112007-06-08 15:23:27 -0400189 i = 0; /* i = min, j = max, n = default */
190 j = 255;
191 if (usb_endpoint_xfer_int(d)) {
192 i = 1;
193 switch (to_usb_device(ddev)->speed) {
Mathias Nyman8a1b2722015-12-10 09:59:25 +0200194 case USB_SPEED_SUPER_PLUS:
Sarah Sharp6b403b02009-04-27 19:54:10 -0700195 case USB_SPEED_SUPER:
Alan Stern615ae112007-06-08 15:23:27 -0400196 case USB_SPEED_HIGH:
Laurent Pinchart300871c2007-06-12 21:47:17 +0200197 /* Many device manufacturers are using full-speed
198 * bInterval values in high-speed interrupt endpoint
199 * descriptors. Try to fix those and fall back to a
200 * 32 ms default value otherwise. */
201 n = fls(d->bInterval*8);
202 if (n == 0)
203 n = 9; /* 32 ms = 2^(9-1) uframes */
Alan Stern615ae112007-06-08 15:23:27 -0400204 j = 16;
James P Michels IIIcd83ce92014-07-27 13:28:04 -0400205
206 /*
207 * Adjust bInterval for quirked devices.
208 * This quirk fixes bIntervals reported in
209 * linear microframes.
210 */
211 if (to_usb_device(ddev)->quirks &
212 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
213 n = clamp(fls(d->bInterval), i, j);
214 i = j = n;
215 }
Alan Stern615ae112007-06-08 15:23:27 -0400216 break;
217 default: /* USB_SPEED_FULL or _LOW */
218 /* For low-speed, 10 ms is the official minimum.
219 * But some "overclocked" devices might want faster
220 * polling so we'll allow it. */
221 n = 32;
222 break;
223 }
224 } else if (usb_endpoint_xfer_isoc(d)) {
225 i = 1;
226 j = 16;
227 switch (to_usb_device(ddev)->speed) {
228 case USB_SPEED_HIGH:
229 n = 9; /* 32 ms = 2^(9-1) uframes */
230 break;
231 default: /* USB_SPEED_FULL */
232 n = 6; /* 32 ms = 2^(6-1) frames */
233 break;
234 }
235 }
236 if (d->bInterval < i || d->bInterval > j) {
237 dev_warn(ddev, "config %d interface %d altsetting %d "
238 "endpoint 0x%X has an invalid bInterval %d, "
239 "changing to %d\n",
240 cfgno, inum, asnum,
241 d->bEndpointAddress, d->bInterval, n);
242 endpoint->desc.bInterval = n;
243 }
244
Alan Stern60aac1e2007-06-08 15:25:02 -0400245 /* Some buggy low-speed devices have Bulk endpoints, which is
246 * explicitly forbidden by the USB spec. In an attempt to make
247 * them usable, we will try treating them as Interrupt endpoints.
248 */
249 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
250 usb_endpoint_xfer_bulk(d)) {
251 dev_warn(ddev, "config %d interface %d altsetting %d "
252 "endpoint 0x%X is Bulk; changing to Interrupt\n",
253 cfgno, inum, asnum, d->bEndpointAddress);
254 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
255 endpoint->desc.bInterval = 1;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700256 if (usb_endpoint_maxp(&endpoint->desc) > 8)
Alan Stern60aac1e2007-06-08 15:25:02 -0400257 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
258 }
259
David Brownellcaa9ef62008-02-08 15:08:44 -0800260 /*
261 * Some buggy high speed devices have bulk endpoints using
262 * maxpacket sizes other than 512. High speed HCDs may not
263 * be able to handle that particular bug, so let's warn...
264 */
265 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
266 && usb_endpoint_xfer_bulk(d)) {
267 unsigned maxp;
268
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700269 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
David Brownellcaa9ef62008-02-08 15:08:44 -0800270 if (maxp != 512)
271 dev_warn(ddev, "config %d interface %d altsetting %d "
272 "bulk endpoint 0x%X has invalid maxpacket %d\n",
273 cfgno, inum, asnum, d->bEndpointAddress,
274 maxp);
275 }
276
Alan Stern842f1692010-04-30 12:44:46 -0400277 /* Parse a possible SuperSpeed endpoint companion descriptor */
Mathias Nyman8a1b2722015-12-10 09:59:25 +0200278 if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
Alan Stern842f1692010-04-30 12:44:46 -0400279 usb_parse_ss_endpoint_companion(ddev, cfgno,
280 inum, asnum, endpoint, buffer, size);
Sarah Sharp9f8e4432009-07-27 12:04:52 -0700281
Alan Stern842f1692010-04-30 12:44:46 -0400282 /* Skip over any Class Specific or Vendor Specific descriptors;
283 * find the next endpoint or interface descriptor */
284 endpoint->extra = buffer;
285 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
286 USB_DT_INTERFACE, &n);
287 endpoint->extralen = i;
288 retval = buffer - buffer0 + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (n > 0)
290 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
291 n, plural(n), "endpoint");
Sarah Sharp663c30d2009-04-27 19:58:14 -0700292 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294skip_to_next_endpoint_or_interface_descriptor:
295 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
296 USB_DT_INTERFACE, NULL);
297 return buffer - buffer0 + i;
298}
299
300void usb_release_interface_cache(struct kref *ref)
301{
302 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
303 int j;
304
Alan Stern4f62efe2005-10-24 16:24:14 -0400305 for (j = 0; j < intfc->num_altsetting; j++) {
306 struct usb_host_interface *alt = &intfc->altsetting[j];
307
308 kfree(alt->endpoint);
309 kfree(alt->string);
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 kfree(intfc);
312}
313
314static int usb_parse_interface(struct device *ddev, int cfgno,
315 struct usb_host_config *config, unsigned char *buffer, int size,
316 u8 inums[], u8 nalts[])
317{
318 unsigned char *buffer0 = buffer;
319 struct usb_interface_descriptor *d;
320 int inum, asnum;
321 struct usb_interface_cache *intfc;
322 struct usb_host_interface *alt;
323 int i, n;
324 int len, retval;
325 int num_ep, num_ep_orig;
326
327 d = (struct usb_interface_descriptor *) buffer;
328 buffer += d->bLength;
329 size -= d->bLength;
330
331 if (d->bLength < USB_DT_INTERFACE_SIZE)
332 goto skip_to_next_interface_descriptor;
333
334 /* Which interface entry is this? */
335 intfc = NULL;
336 inum = d->bInterfaceNumber;
337 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
338 if (inums[i] == inum) {
339 intfc = config->intf_cache[i];
340 break;
341 }
342 }
343 if (!intfc || intfc->num_altsetting >= nalts[i])
344 goto skip_to_next_interface_descriptor;
345
346 /* Check for duplicate altsetting entries */
347 asnum = d->bAlternateSetting;
348 for ((i = 0, alt = &intfc->altsetting[0]);
349 i < intfc->num_altsetting;
350 (++i, ++alt)) {
351 if (alt->desc.bAlternateSetting == asnum) {
352 dev_warn(ddev, "Duplicate descriptor for config %d "
353 "interface %d altsetting %d, skipping\n",
354 cfgno, inum, asnum);
355 goto skip_to_next_interface_descriptor;
356 }
357 }
358
359 ++intfc->num_altsetting;
360 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
361
362 /* Skip over any Class Specific or Vendor Specific descriptors;
363 * find the first endpoint or interface descriptor */
364 alt->extra = buffer;
365 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
366 USB_DT_INTERFACE, &n);
367 alt->extralen = i;
368 if (n > 0)
369 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
370 n, plural(n), "interface");
371 buffer += i;
372 size -= i;
373
374 /* Allocate space for the right(?) number of endpoints */
375 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800376 alt->desc.bNumEndpoints = 0; /* Use as a counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (num_ep > USB_MAXENDPOINTS) {
378 dev_warn(ddev, "too many endpoints for config %d interface %d "
379 "altsetting %d: %d, using maximum allowed: %d\n",
380 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
381 num_ep = USB_MAXENDPOINTS;
382 }
383
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800384 if (num_ep > 0) {
385 /* Can't allocate 0 bytes */
Alan Stern57a21c12007-05-15 17:40:37 -0400386 len = sizeof(struct usb_host_endpoint) * num_ep;
387 alt->endpoint = kzalloc(len, GFP_KERNEL);
388 if (!alt->endpoint)
389 return -ENOMEM;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* Parse all the endpoint descriptors */
393 n = 0;
394 while (size > 0) {
395 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
396 == USB_DT_INTERFACE)
397 break;
398 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
399 num_ep, buffer, size);
400 if (retval < 0)
401 return retval;
402 ++n;
403
404 buffer += retval;
405 size -= retval;
406 }
407
408 if (n != num_ep_orig)
409 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
410 "endpoint descriptor%s, different from the interface "
411 "descriptor's value: %d\n",
412 cfgno, inum, asnum, n, plural(n), num_ep_orig);
413 return buffer - buffer0;
414
415skip_to_next_interface_descriptor:
416 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
417 USB_DT_INTERFACE, NULL);
418 return buffer - buffer0 + i;
419}
420
Hans de Goede317149c2010-03-29 12:03:17 +0200421static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct usb_host_config *config, unsigned char *buffer, int size)
423{
Hans de Goede317149c2010-03-29 12:03:17 +0200424 struct device *ddev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 unsigned char *buffer0 = buffer;
426 int cfgno;
427 int nintf, nintf_orig;
428 int i, j, n;
429 struct usb_interface_cache *intfc;
430 unsigned char *buffer2;
431 int size2;
432 struct usb_descriptor_header *header;
433 int len, retval;
434 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
Craig W. Nadler165fe972007-06-15 23:14:35 -0400435 unsigned iad_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
438 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
Hans de Goedeb4f17a482013-08-03 16:37:48 +0200439 config->desc.bLength < USB_DT_CONFIG_SIZE ||
440 config->desc.bLength > size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 dev_err(ddev, "invalid descriptor for config index %d: "
442 "type = 0x%X, length = %d\n", cfgidx,
443 config->desc.bDescriptorType, config->desc.bLength);
444 return -EINVAL;
445 }
446 cfgno = config->desc.bConfigurationValue;
447
448 buffer += config->desc.bLength;
449 size -= config->desc.bLength;
450
451 nintf = nintf_orig = config->desc.bNumInterfaces;
452 if (nintf > USB_MAXINTERFACES) {
453 dev_warn(ddev, "config %d has too many interfaces: %d, "
454 "using maximum allowed: %d\n",
455 cfgno, nintf, USB_MAXINTERFACES);
456 nintf = USB_MAXINTERFACES;
457 }
458
459 /* Go through the descriptors, checking their length and counting the
460 * number of altsettings for each interface */
461 n = 0;
462 for ((buffer2 = buffer, size2 = size);
463 size2 > 0;
464 (buffer2 += header->bLength, size2 -= header->bLength)) {
465
466 if (size2 < sizeof(struct usb_descriptor_header)) {
467 dev_warn(ddev, "config %d descriptor has %d excess "
468 "byte%s, ignoring\n",
469 cfgno, size2, plural(size2));
470 break;
471 }
472
473 header = (struct usb_descriptor_header *) buffer2;
474 if ((header->bLength > size2) || (header->bLength < 2)) {
475 dev_warn(ddev, "config %d has an invalid descriptor "
476 "of length %d, skipping remainder of the config\n",
477 cfgno, header->bLength);
478 break;
479 }
480
481 if (header->bDescriptorType == USB_DT_INTERFACE) {
482 struct usb_interface_descriptor *d;
483 int inum;
484
485 d = (struct usb_interface_descriptor *) header;
486 if (d->bLength < USB_DT_INTERFACE_SIZE) {
487 dev_warn(ddev, "config %d has an invalid "
488 "interface descriptor of length %d, "
489 "skipping\n", cfgno, d->bLength);
490 continue;
491 }
492
493 inum = d->bInterfaceNumber;
Hans de Goede317149c2010-03-29 12:03:17 +0200494
495 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
496 n >= nintf_orig) {
497 dev_warn(ddev, "config %d has more interface "
498 "descriptors, than it declares in "
499 "bNumInterfaces, ignoring interface "
500 "number: %d\n", cfgno, inum);
501 continue;
502 }
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (inum >= nintf_orig)
505 dev_warn(ddev, "config %d has an invalid "
506 "interface number: %d but max is %d\n",
507 cfgno, inum, nintf_orig - 1);
508
509 /* Have we already encountered this interface?
510 * Count its altsettings */
511 for (i = 0; i < n; ++i) {
512 if (inums[i] == inum)
513 break;
514 }
515 if (i < n) {
516 if (nalts[i] < 255)
517 ++nalts[i];
518 } else if (n < USB_MAXINTERFACES) {
519 inums[n] = inum;
520 nalts[n] = 1;
521 ++n;
522 }
523
Craig W. Nadler165fe972007-06-15 23:14:35 -0400524 } else if (header->bDescriptorType ==
525 USB_DT_INTERFACE_ASSOCIATION) {
526 if (iad_num == USB_MAXIADS) {
527 dev_warn(ddev, "found more Interface "
528 "Association Descriptors "
529 "than allocated for in "
530 "configuration %d\n", cfgno);
531 } else {
532 config->intf_assoc[iad_num] =
533 (struct usb_interface_assoc_descriptor
534 *)header;
535 iad_num++;
536 }
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 } else if (header->bDescriptorType == USB_DT_DEVICE ||
539 header->bDescriptorType == USB_DT_CONFIG)
540 dev_warn(ddev, "config %d contains an unexpected "
541 "descriptor of type 0x%X, skipping\n",
542 cfgno, header->bDescriptorType);
543
544 } /* for ((buffer2 = buffer, size2 = size); ...) */
545 size = buffer2 - buffer;
546 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
547
548 if (n != nintf)
549 dev_warn(ddev, "config %d has %d interface%s, different from "
550 "the descriptor's value: %d\n",
551 cfgno, n, plural(n), nintf_orig);
552 else if (n == 0)
553 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
554 config->desc.bNumInterfaces = nintf = n;
555
556 /* Check for missing interface numbers */
557 for (i = 0; i < nintf; ++i) {
558 for (j = 0; j < nintf; ++j) {
559 if (inums[j] == i)
560 break;
561 }
562 if (j >= nintf)
563 dev_warn(ddev, "config %d has no interface number "
564 "%d\n", cfgno, i);
565 }
566
567 /* Allocate the usb_interface_caches and altsetting arrays */
568 for (i = 0; i < nintf; ++i) {
569 j = nalts[i];
570 if (j > USB_MAXALTSETTING) {
571 dev_warn(ddev, "too many alternate settings for "
572 "config %d interface %d: %d, "
573 "using maximum allowed: %d\n",
574 cfgno, inums[i], j, USB_MAXALTSETTING);
575 nalts[i] = j = USB_MAXALTSETTING;
576 }
577
578 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400579 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!intfc)
581 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 kref_init(&intfc->ref);
583 }
584
Sarah Sharp663c30d2009-04-27 19:58:14 -0700585 /* FIXME: parse the BOS descriptor */
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Skip over any Class Specific or Vendor Specific descriptors;
588 * find the first interface descriptor */
589 config->extra = buffer;
590 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
591 USB_DT_INTERFACE, &n);
592 config->extralen = i;
593 if (n > 0)
594 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
595 n, plural(n), "configuration");
596 buffer += i;
597 size -= i;
598
599 /* Parse all the interface/altsetting descriptors */
600 while (size > 0) {
601 retval = usb_parse_interface(ddev, cfgno, config,
602 buffer, size, inums, nalts);
603 if (retval < 0)
604 return retval;
605
606 buffer += retval;
607 size -= retval;
608 }
609
610 /* Check for missing altsettings */
611 for (i = 0; i < nintf; ++i) {
612 intfc = config->intf_cache[i];
613 for (j = 0; j < intfc->num_altsetting; ++j) {
614 for (n = 0; n < intfc->num_altsetting; ++n) {
615 if (intfc->altsetting[n].desc.
616 bAlternateSetting == j)
617 break;
618 }
619 if (n >= intfc->num_altsetting)
620 dev_warn(ddev, "config %d interface %d has no "
621 "altsetting %d\n", cfgno, inums[i], j);
622 }
623 }
624
625 return 0;
626}
627
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800628/* hub-only!! ... and only exported for reset/reinit path.
629 * otherwise used internally on disconnect/destroy path
630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631void usb_destroy_configuration(struct usb_device *dev)
632{
633 int c, i;
634
635 if (!dev->config)
636 return;
637
638 if (dev->rawdescriptors) {
639 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
640 kfree(dev->rawdescriptors[i]);
641
642 kfree(dev->rawdescriptors);
643 dev->rawdescriptors = NULL;
644 }
645
646 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
647 struct usb_host_config *cf = &dev->config[c];
648
649 kfree(cf->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
651 if (cf->intf_cache[i])
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800652 kref_put(&cf->intf_cache[i]->ref,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 usb_release_interface_cache);
654 }
655 }
656 kfree(dev->config);
657 dev->config = NULL;
658}
659
660
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700661/*
662 * Get the USB config descriptors, cache and parse'em
663 *
664 * hub-only!! ... and only in reset path, or usb_new_device()
665 * (used by real hubs and virtual root hubs)
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667int usb_get_configuration(struct usb_device *dev)
668{
669 struct device *ddev = &dev->dev;
670 int ncfg = dev->descriptor.bNumConfigurations;
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700671 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 unsigned int cfgno, length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned char *bigbuffer;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800674 struct usb_config_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700676 cfgno = 0;
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700677 result = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (ncfg > USB_MAXCONFIG) {
679 dev_warn(ddev, "too many configurations: %d, "
680 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
681 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
682 }
683
684 if (ncfg < 1) {
685 dev_err(ddev, "no configurations\n");
686 return -EINVAL;
687 }
688
689 length = ncfg * sizeof(struct usb_host_config);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400690 dev->config = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!dev->config)
692 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 length = ncfg * sizeof(char *);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400695 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!dev->rawdescriptors)
697 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200699 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
700 if (!desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700703 result = 0;
704 for (; cfgno < ncfg; cfgno++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* We grab just the first descriptor so we know how long
706 * the whole configuration is */
707 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200708 desc, USB_DT_CONFIG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (result < 0) {
710 dev_err(ddev, "unable to read config index %d "
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700711 "descriptor/%s: %d\n", cfgno, "start", result);
Lan Tianyu3a22b872012-09-05 13:44:37 +0800712 if (result != -EPIPE)
713 goto err;
Inaky Perez-Gonzalezcb4c8fe2006-08-25 19:35:28 -0700714 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
715 dev->descriptor.bNumConfigurations = cfgno;
716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 } else if (result < 4) {
718 dev_err(ddev, "config index %d descriptor too short "
719 "(expected %i, got %i)\n", cfgno,
720 USB_DT_CONFIG_SIZE, result);
721 result = -EINVAL;
722 goto err;
723 }
724 length = max((int) le16_to_cpu(desc->wTotalLength),
725 USB_DT_CONFIG_SIZE);
726
727 /* Now that we know the length, get the whole thing */
728 bigbuffer = kmalloc(length, GFP_KERNEL);
729 if (!bigbuffer) {
730 result = -ENOMEM;
731 goto err;
732 }
Julius Wernerd86db252014-03-04 11:27:38 -0800733
734 if (dev->quirks & USB_QUIRK_DELAY_INIT)
735 msleep(100);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
738 bigbuffer, length);
739 if (result < 0) {
740 dev_err(ddev, "unable to read config index %d "
741 "descriptor/%s\n", cfgno, "all");
742 kfree(bigbuffer);
743 goto err;
744 }
745 if (result < length) {
746 dev_warn(ddev, "config index %d descriptor too short "
747 "(expected %i, got %i)\n", cfgno, length, result);
748 length = result;
749 }
750
751 dev->rawdescriptors[cfgno] = bigbuffer;
752
Hans de Goede317149c2010-03-29 12:03:17 +0200753 result = usb_parse_configuration(dev, cfgno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 &dev->config[cfgno], bigbuffer, length);
755 if (result < 0) {
756 ++cfgno;
757 goto err;
758 }
759 }
760 result = 0;
761
762err:
Michal Nazarewicze8f4af32010-04-17 17:12:58 +0200763 kfree(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 dev->descriptor.bNumConfigurations = cfgno;
765err2:
766 if (result == -ENOMEM)
767 dev_err(ddev, "out of memory\n");
768 return result;
769}
Andiry Xu3148bf02011-09-23 14:19:47 -0700770
771void usb_release_bos_descriptor(struct usb_device *dev)
772{
773 if (dev->bos) {
774 kfree(dev->bos->desc);
775 kfree(dev->bos);
776 dev->bos = NULL;
777 }
778}
779
780/* Get BOS descriptor set */
781int usb_get_bos_descriptor(struct usb_device *dev)
782{
783 struct device *ddev = &dev->dev;
784 struct usb_bos_descriptor *bos;
785 struct usb_dev_cap_header *cap;
786 unsigned char *buffer;
787 int length, total_len, num, i;
788 int ret;
789
790 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
791 if (!bos)
792 return -ENOMEM;
793
794 /* Get BOS descriptor */
795 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
796 if (ret < USB_DT_BOS_SIZE) {
797 dev_err(ddev, "unable to get BOS descriptor\n");
798 if (ret >= 0)
799 ret = -ENOMSG;
800 kfree(bos);
801 return ret;
802 }
803
804 length = bos->bLength;
805 total_len = le16_to_cpu(bos->wTotalLength);
806 num = bos->bNumDeviceCaps;
807 kfree(bos);
808 if (total_len < length)
809 return -EINVAL;
810
811 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
812 if (!dev->bos)
813 return -ENOMEM;
814
815 /* Now let's get the whole BOS descriptor set */
816 buffer = kzalloc(total_len, GFP_KERNEL);
817 if (!buffer) {
818 ret = -ENOMEM;
819 goto err;
820 }
821 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
822
823 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
824 if (ret < total_len) {
825 dev_err(ddev, "unable to get BOS descriptor set\n");
826 if (ret >= 0)
827 ret = -ENOMSG;
828 goto err;
829 }
830 total_len -= length;
831
832 for (i = 0; i < num; i++) {
833 buffer += length;
834 cap = (struct usb_dev_cap_header *)buffer;
835 length = cap->bLength;
836
837 if (total_len < length)
838 break;
839 total_len -= length;
840
841 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
842 dev_warn(ddev, "descriptor type invalid, skip\n");
843 continue;
844 }
845
846 switch (cap->bDevCapabilityType) {
847 case USB_CAP_TYPE_WIRELESS_USB:
848 /* Wireless USB cap descriptor is handled by wusb */
849 break;
850 case USB_CAP_TYPE_EXT:
851 dev->bos->ext_cap =
852 (struct usb_ext_cap_descriptor *)buffer;
853 break;
854 case USB_SS_CAP_TYPE:
855 dev->bos->ss_cap =
856 (struct usb_ss_cap_descriptor *)buffer;
857 break;
Mathias Nyman3220bef2015-10-01 18:40:33 +0300858 case USB_SSP_CAP_TYPE:
859 dev->bos->ssp_cap =
860 (struct usb_ssp_cap_descriptor *)buffer;
861 break;
Andiry Xu3148bf02011-09-23 14:19:47 -0700862 case CONTAINER_ID_TYPE:
863 dev->bos->ss_id =
864 (struct usb_ss_container_id_descriptor *)buffer;
865 break;
866 default:
867 break;
868 }
869 }
870
871 return 0;
872
873err:
874 usb_release_bos_descriptor(dev);
875 return ret;
876}