blob: 16c1157be3fcf225e879ae35bb52aebb5dd9a4a1 [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>
6#include <linux/init.h>
7#include <linux/slab.h>
8#include <linux/device.h>
9#include <asm/byteorder.h>
Greg KH6d5e8252005-04-18 17:39:24 -070010#include "usb.h"
Eric Lescouet27729aa2010-04-24 23:21:52 +020011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#define USB_MAXALTSETTING 128 /* Hard limit */
14#define USB_MAXENDPOINTS 30 /* Hard limit */
15
16#define USB_MAXCONFIG 8 /* Arbitrary limit */
17
18
19static inline const char *plural(int n)
20{
21 return (n == 1 ? "" : "s");
22}
23
Sarah Sharp663c30d2009-04-27 19:58:14 -070024/* FIXME: this is a kludge */
25static int find_next_descriptor_more(unsigned char *buffer, int size,
26 int dt1, int dt2, int dt3, int *num_skipped)
27{
28 struct usb_descriptor_header *h;
29 int n = 0;
30 unsigned char *buffer0 = buffer;
31
32 /* Find the next descriptor of type dt1 or dt2 or dt3 */
33 while (size > 0) {
34 h = (struct usb_descriptor_header *) buffer;
35 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2 ||
36 h->bDescriptorType == dt3)
37 break;
38 buffer += h->bLength;
39 size -= h->bLength;
40 ++n;
41 }
42
43 /* Store the number of descriptors skipped and return the
44 * number of bytes skipped */
45 if (num_skipped)
46 *num_skipped = n;
47 return buffer - buffer0;
48}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int find_next_descriptor(unsigned char *buffer, int size,
51 int dt1, int dt2, int *num_skipped)
52{
53 struct usb_descriptor_header *h;
54 int n = 0;
55 unsigned char *buffer0 = buffer;
56
57 /* Find the next descriptor of type dt1 or dt2 */
58 while (size > 0) {
59 h = (struct usb_descriptor_header *) buffer;
60 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
61 break;
62 buffer += h->bLength;
63 size -= h->bLength;
64 ++n;
65 }
66
67 /* Store the number of descriptors skipped and return the
68 * number of bytes skipped */
69 if (num_skipped)
70 *num_skipped = n;
71 return buffer - buffer0;
72}
73
Sarah Sharpf0058c62009-04-29 19:06:20 -070074static int usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
Sarah Sharp663c30d2009-04-27 19:58:14 -070075 int inum, int asnum, struct usb_host_endpoint *ep,
76 int num_ep, unsigned char *buffer, int size)
77{
78 unsigned char *buffer_start = buffer;
Sarah Sharpf0058c62009-04-29 19:06:20 -070079 struct usb_ss_ep_comp_descriptor *desc;
Sarah Sharp663c30d2009-04-27 19:58:14 -070080 int retval;
81 int num_skipped;
82 int max_tx;
83 int i;
84
Sarah Sharpf0058c62009-04-29 19:06:20 -070085 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
Sarah Sharp663c30d2009-04-27 19:58:14 -070086 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) {
87 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
88 " interface %d altsetting %d ep %d: "
89 "using minimum values\n",
90 cfgno, inum, asnum, ep->desc.bEndpointAddress);
Sarah Sharp663c30d2009-04-27 19:58:14 -070091 /*
92 * The next descriptor is for an Endpoint or Interface,
93 * no extra descriptors to copy into the companion structure,
94 * and we didn't eat up any of the buffer.
95 */
Sarah Sharp9f8e4432009-07-27 12:04:52 -070096 return 0;
Sarah Sharp663c30d2009-04-27 19:58:14 -070097 }
Sarah Sharpf0058c62009-04-29 19:06:20 -070098 memcpy(&ep->ss_ep_comp->desc, desc, USB_DT_SS_EP_COMP_SIZE);
99 desc = &ep->ss_ep_comp->desc;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700100 buffer += desc->bLength;
101 size -= desc->bLength;
102
103 /* Eat up the other descriptors we don't care about */
Sarah Sharpf0058c62009-04-29 19:06:20 -0700104 ep->ss_ep_comp->extra = buffer;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700105 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
106 USB_DT_INTERFACE, &num_skipped);
Sarah Sharpf0058c62009-04-29 19:06:20 -0700107 ep->ss_ep_comp->extralen = i;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700108 buffer += i;
109 size -= i;
Sarah Sharp6682bb32009-09-08 13:20:16 -0700110 retval = buffer - buffer_start;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700111 if (num_skipped > 0)
112 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
113 num_skipped, plural(num_skipped),
114 "SuperSpeed endpoint companion");
115
116 /* Check the various values */
117 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
118 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
119 "config %d interface %d altsetting %d ep %d: "
120 "setting to zero\n", desc->bMaxBurst,
121 cfgno, inum, asnum, ep->desc.bEndpointAddress);
122 desc->bMaxBurst = 0;
123 }
124 if (desc->bMaxBurst > 15) {
125 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
126 "config %d interface %d altsetting %d ep %d: "
127 "setting to 15\n", desc->bMaxBurst,
128 cfgno, inum, asnum, ep->desc.bEndpointAddress);
129 desc->bMaxBurst = 15;
130 }
131 if ((usb_endpoint_xfer_control(&ep->desc) || usb_endpoint_xfer_int(&ep->desc))
132 && desc->bmAttributes != 0) {
133 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
134 "config %d interface %d altsetting %d ep %d: "
135 "setting to zero\n",
136 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
137 desc->bmAttributes,
138 cfgno, inum, asnum, ep->desc.bEndpointAddress);
139 desc->bmAttributes = 0;
140 }
141 if (usb_endpoint_xfer_bulk(&ep->desc) && desc->bmAttributes > 16) {
142 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
143 "config %d interface %d altsetting %d ep %d: "
144 "setting to max\n",
145 cfgno, inum, asnum, ep->desc.bEndpointAddress);
146 desc->bmAttributes = 16;
147 }
148 if (usb_endpoint_xfer_isoc(&ep->desc) && desc->bmAttributes > 2) {
149 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
150 "config %d interface %d altsetting %d ep %d: "
151 "setting to 3\n", desc->bmAttributes + 1,
152 cfgno, inum, asnum, ep->desc.bEndpointAddress);
153 desc->bmAttributes = 2;
154 }
155 if (usb_endpoint_xfer_isoc(&ep->desc)) {
156 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1) *
157 (desc->bmAttributes + 1);
158 } else if (usb_endpoint_xfer_int(&ep->desc)) {
159 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1);
160 } else {
161 goto valid;
162 }
163 if (desc->wBytesPerInterval > max_tx) {
164 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
165 "config %d interface %d altsetting %d ep %d: "
166 "setting to %d\n",
167 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
168 desc->wBytesPerInterval,
169 cfgno, inum, asnum, ep->desc.bEndpointAddress,
170 max_tx);
171 desc->wBytesPerInterval = max_tx;
172 }
173valid:
174 return retval;
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
178 int asnum, struct usb_host_interface *ifp, int num_ep,
179 unsigned char *buffer, int size)
180{
181 unsigned char *buffer0 = buffer;
182 struct usb_endpoint_descriptor *d;
183 struct usb_host_endpoint *endpoint;
Sarah Sharp663c30d2009-04-27 19:58:14 -0700184 int n, i, j, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 d = (struct usb_endpoint_descriptor *) buffer;
187 buffer += d->bLength;
188 size -= d->bLength;
189
190 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
191 n = USB_DT_ENDPOINT_AUDIO_SIZE;
192 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
193 n = USB_DT_ENDPOINT_SIZE;
194 else {
195 dev_warn(ddev, "config %d interface %d altsetting %d has an "
196 "invalid endpoint descriptor of length %d, skipping\n",
197 cfgno, inum, asnum, d->bLength);
198 goto skip_to_next_endpoint_or_interface_descriptor;
199 }
200
201 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
202 if (i >= 16 || i == 0) {
203 dev_warn(ddev, "config %d interface %d altsetting %d has an "
204 "invalid endpoint with address 0x%X, skipping\n",
205 cfgno, inum, asnum, d->bEndpointAddress);
206 goto skip_to_next_endpoint_or_interface_descriptor;
207 }
208
209 /* Only store as many endpoints as we have room for */
210 if (ifp->desc.bNumEndpoints >= num_ep)
211 goto skip_to_next_endpoint_or_interface_descriptor;
212
213 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
214 ++ifp->desc.bNumEndpoints;
215
216 memcpy(&endpoint->desc, d, n);
217 INIT_LIST_HEAD(&endpoint->urb_list);
218
Laurent Pinchart300871c2007-06-12 21:47:17 +0200219 /* Fix up bInterval values outside the legal range. Use 32 ms if no
220 * proper value can be guessed. */
Alan Stern615ae112007-06-08 15:23:27 -0400221 i = 0; /* i = min, j = max, n = default */
222 j = 255;
223 if (usb_endpoint_xfer_int(d)) {
224 i = 1;
225 switch (to_usb_device(ddev)->speed) {
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;
236 break;
237 default: /* USB_SPEED_FULL or _LOW */
238 /* For low-speed, 10 ms is the official minimum.
239 * But some "overclocked" devices might want faster
240 * polling so we'll allow it. */
241 n = 32;
242 break;
243 }
244 } else if (usb_endpoint_xfer_isoc(d)) {
245 i = 1;
246 j = 16;
247 switch (to_usb_device(ddev)->speed) {
248 case USB_SPEED_HIGH:
249 n = 9; /* 32 ms = 2^(9-1) uframes */
250 break;
251 default: /* USB_SPEED_FULL */
252 n = 6; /* 32 ms = 2^(6-1) frames */
253 break;
254 }
255 }
256 if (d->bInterval < i || d->bInterval > j) {
257 dev_warn(ddev, "config %d interface %d altsetting %d "
258 "endpoint 0x%X has an invalid bInterval %d, "
259 "changing to %d\n",
260 cfgno, inum, asnum,
261 d->bEndpointAddress, d->bInterval, n);
262 endpoint->desc.bInterval = n;
263 }
264
Alan Stern60aac1e2007-06-08 15:25:02 -0400265 /* Some buggy low-speed devices have Bulk endpoints, which is
266 * explicitly forbidden by the USB spec. In an attempt to make
267 * them usable, we will try treating them as Interrupt endpoints.
268 */
269 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
270 usb_endpoint_xfer_bulk(d)) {
271 dev_warn(ddev, "config %d interface %d altsetting %d "
272 "endpoint 0x%X is Bulk; changing to Interrupt\n",
273 cfgno, inum, asnum, d->bEndpointAddress);
274 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
275 endpoint->desc.bInterval = 1;
276 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
277 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
278 }
279
David Brownellcaa9ef62008-02-08 15:08:44 -0800280 /*
281 * Some buggy high speed devices have bulk endpoints using
282 * maxpacket sizes other than 512. High speed HCDs may not
283 * be able to handle that particular bug, so let's warn...
284 */
285 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
286 && usb_endpoint_xfer_bulk(d)) {
287 unsigned maxp;
288
289 maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
290 if (maxp != 512)
291 dev_warn(ddev, "config %d interface %d altsetting %d "
292 "bulk endpoint 0x%X has invalid maxpacket %d\n",
293 cfgno, inum, asnum, d->bEndpointAddress,
294 maxp);
295 }
Sarah Sharpf0058c62009-04-29 19:06:20 -0700296 /* Allocate room for and parse any SS endpoint companion descriptors */
Sarah Sharp663c30d2009-04-27 19:58:14 -0700297 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER) {
298 endpoint->extra = buffer;
299 i = find_next_descriptor_more(buffer, size, USB_DT_SS_ENDPOINT_COMP,
300 USB_DT_ENDPOINT, USB_DT_INTERFACE, &n);
301 endpoint->extralen = i;
302 buffer += i;
303 size -= i;
David Brownellcaa9ef62008-02-08 15:08:44 -0800304
Sarah Sharp9f8e4432009-07-27 12:04:52 -0700305 /* Allocate space for the SS endpoint companion descriptor */
306 endpoint->ss_ep_comp = kzalloc(sizeof(struct usb_host_ss_ep_comp),
307 GFP_KERNEL);
308 if (!endpoint->ss_ep_comp)
309 return -ENOMEM;
310
311 /* Fill in some default values (may be overwritten later) */
312 endpoint->ss_ep_comp->desc.bLength = USB_DT_SS_EP_COMP_SIZE;
313 endpoint->ss_ep_comp->desc.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
314 endpoint->ss_ep_comp->desc.bMaxBurst = 0;
315 /*
316 * Leave bmAttributes as zero, which will mean no streams for
317 * bulk, and isoc won't support multiple bursts of packets.
318 * With bursts of only one packet, and a Mult of 1, the max
319 * amount of data moved per endpoint service interval is one
320 * packet.
321 */
322 if (usb_endpoint_xfer_isoc(&endpoint->desc) ||
323 usb_endpoint_xfer_int(&endpoint->desc))
324 endpoint->ss_ep_comp->desc.wBytesPerInterval =
325 endpoint->desc.wMaxPacketSize;
326
Sarah Sharp663c30d2009-04-27 19:58:14 -0700327 if (size > 0) {
Sarah Sharpf0058c62009-04-29 19:06:20 -0700328 retval = usb_parse_ss_endpoint_companion(ddev, cfgno,
329 inum, asnum, endpoint, num_ep, buffer,
330 size);
Sarah Sharp663c30d2009-04-27 19:58:14 -0700331 if (retval >= 0) {
332 buffer += retval;
333 retval = buffer - buffer0;
334 }
335 } else {
Sarah Sharp9f8e4432009-07-27 12:04:52 -0700336 dev_warn(ddev, "config %d interface %d altsetting %d "
337 "endpoint 0x%X has no "
338 "SuperSpeed companion descriptor\n",
339 cfgno, inum, asnum, d->bEndpointAddress);
Sarah Sharp663c30d2009-04-27 19:58:14 -0700340 retval = buffer - buffer0;
341 }
342 } else {
343 /* Skip over any Class Specific or Vendor Specific descriptors;
344 * find the next endpoint or interface descriptor */
345 endpoint->extra = buffer;
346 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
347 USB_DT_INTERFACE, &n);
348 endpoint->extralen = i;
349 retval = buffer - buffer0 + i;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (n > 0)
352 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
353 n, plural(n), "endpoint");
Sarah Sharp663c30d2009-04-27 19:58:14 -0700354 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356skip_to_next_endpoint_or_interface_descriptor:
357 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
358 USB_DT_INTERFACE, NULL);
359 return buffer - buffer0 + i;
360}
361
362void usb_release_interface_cache(struct kref *ref)
363{
364 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
365 int j;
366
Alan Stern4f62efe2005-10-24 16:24:14 -0400367 for (j = 0; j < intfc->num_altsetting; j++) {
368 struct usb_host_interface *alt = &intfc->altsetting[j];
369
370 kfree(alt->endpoint);
371 kfree(alt->string);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 kfree(intfc);
374}
375
376static int usb_parse_interface(struct device *ddev, int cfgno,
377 struct usb_host_config *config, unsigned char *buffer, int size,
378 u8 inums[], u8 nalts[])
379{
380 unsigned char *buffer0 = buffer;
381 struct usb_interface_descriptor *d;
382 int inum, asnum;
383 struct usb_interface_cache *intfc;
384 struct usb_host_interface *alt;
385 int i, n;
386 int len, retval;
387 int num_ep, num_ep_orig;
388
389 d = (struct usb_interface_descriptor *) buffer;
390 buffer += d->bLength;
391 size -= d->bLength;
392
393 if (d->bLength < USB_DT_INTERFACE_SIZE)
394 goto skip_to_next_interface_descriptor;
395
396 /* Which interface entry is this? */
397 intfc = NULL;
398 inum = d->bInterfaceNumber;
399 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
400 if (inums[i] == inum) {
401 intfc = config->intf_cache[i];
402 break;
403 }
404 }
405 if (!intfc || intfc->num_altsetting >= nalts[i])
406 goto skip_to_next_interface_descriptor;
407
408 /* Check for duplicate altsetting entries */
409 asnum = d->bAlternateSetting;
410 for ((i = 0, alt = &intfc->altsetting[0]);
411 i < intfc->num_altsetting;
412 (++i, ++alt)) {
413 if (alt->desc.bAlternateSetting == asnum) {
414 dev_warn(ddev, "Duplicate descriptor for config %d "
415 "interface %d altsetting %d, skipping\n",
416 cfgno, inum, asnum);
417 goto skip_to_next_interface_descriptor;
418 }
419 }
420
421 ++intfc->num_altsetting;
422 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
423
424 /* Skip over any Class Specific or Vendor Specific descriptors;
425 * find the first endpoint or interface descriptor */
426 alt->extra = buffer;
427 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
428 USB_DT_INTERFACE, &n);
429 alt->extralen = i;
430 if (n > 0)
431 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
432 n, plural(n), "interface");
433 buffer += i;
434 size -= i;
435
436 /* Allocate space for the right(?) number of endpoints */
437 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800438 alt->desc.bNumEndpoints = 0; /* Use as a counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (num_ep > USB_MAXENDPOINTS) {
440 dev_warn(ddev, "too many endpoints for config %d interface %d "
441 "altsetting %d: %d, using maximum allowed: %d\n",
442 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
443 num_ep = USB_MAXENDPOINTS;
444 }
445
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800446 if (num_ep > 0) {
447 /* Can't allocate 0 bytes */
Alan Stern57a21c12007-05-15 17:40:37 -0400448 len = sizeof(struct usb_host_endpoint) * num_ep;
449 alt->endpoint = kzalloc(len, GFP_KERNEL);
450 if (!alt->endpoint)
451 return -ENOMEM;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /* Parse all the endpoint descriptors */
455 n = 0;
456 while (size > 0) {
457 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
458 == USB_DT_INTERFACE)
459 break;
460 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
461 num_ep, buffer, size);
462 if (retval < 0)
463 return retval;
464 ++n;
465
466 buffer += retval;
467 size -= retval;
468 }
469
470 if (n != num_ep_orig)
471 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
472 "endpoint descriptor%s, different from the interface "
473 "descriptor's value: %d\n",
474 cfgno, inum, asnum, n, plural(n), num_ep_orig);
475 return buffer - buffer0;
476
477skip_to_next_interface_descriptor:
478 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
479 USB_DT_INTERFACE, NULL);
480 return buffer - buffer0 + i;
481}
482
Hans de Goede317149c2010-03-29 12:03:17 +0200483static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct usb_host_config *config, unsigned char *buffer, int size)
485{
Hans de Goede317149c2010-03-29 12:03:17 +0200486 struct device *ddev = &dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 unsigned char *buffer0 = buffer;
488 int cfgno;
489 int nintf, nintf_orig;
490 int i, j, n;
491 struct usb_interface_cache *intfc;
492 unsigned char *buffer2;
493 int size2;
494 struct usb_descriptor_header *header;
495 int len, retval;
496 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
Craig W. Nadler165fe972007-06-15 23:14:35 -0400497 unsigned iad_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
500 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
501 config->desc.bLength < USB_DT_CONFIG_SIZE) {
502 dev_err(ddev, "invalid descriptor for config index %d: "
503 "type = 0x%X, length = %d\n", cfgidx,
504 config->desc.bDescriptorType, config->desc.bLength);
505 return -EINVAL;
506 }
507 cfgno = config->desc.bConfigurationValue;
508
509 buffer += config->desc.bLength;
510 size -= config->desc.bLength;
511
512 nintf = nintf_orig = config->desc.bNumInterfaces;
513 if (nintf > USB_MAXINTERFACES) {
514 dev_warn(ddev, "config %d has too many interfaces: %d, "
515 "using maximum allowed: %d\n",
516 cfgno, nintf, USB_MAXINTERFACES);
517 nintf = USB_MAXINTERFACES;
518 }
519
520 /* Go through the descriptors, checking their length and counting the
521 * number of altsettings for each interface */
522 n = 0;
523 for ((buffer2 = buffer, size2 = size);
524 size2 > 0;
525 (buffer2 += header->bLength, size2 -= header->bLength)) {
526
527 if (size2 < sizeof(struct usb_descriptor_header)) {
528 dev_warn(ddev, "config %d descriptor has %d excess "
529 "byte%s, ignoring\n",
530 cfgno, size2, plural(size2));
531 break;
532 }
533
534 header = (struct usb_descriptor_header *) buffer2;
535 if ((header->bLength > size2) || (header->bLength < 2)) {
536 dev_warn(ddev, "config %d has an invalid descriptor "
537 "of length %d, skipping remainder of the config\n",
538 cfgno, header->bLength);
539 break;
540 }
541
542 if (header->bDescriptorType == USB_DT_INTERFACE) {
543 struct usb_interface_descriptor *d;
544 int inum;
545
546 d = (struct usb_interface_descriptor *) header;
547 if (d->bLength < USB_DT_INTERFACE_SIZE) {
548 dev_warn(ddev, "config %d has an invalid "
549 "interface descriptor of length %d, "
550 "skipping\n", cfgno, d->bLength);
551 continue;
552 }
553
554 inum = d->bInterfaceNumber;
Hans de Goede317149c2010-03-29 12:03:17 +0200555
556 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
557 n >= nintf_orig) {
558 dev_warn(ddev, "config %d has more interface "
559 "descriptors, than it declares in "
560 "bNumInterfaces, ignoring interface "
561 "number: %d\n", cfgno, inum);
562 continue;
563 }
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (inum >= nintf_orig)
566 dev_warn(ddev, "config %d has an invalid "
567 "interface number: %d but max is %d\n",
568 cfgno, inum, nintf_orig - 1);
569
570 /* Have we already encountered this interface?
571 * Count its altsettings */
572 for (i = 0; i < n; ++i) {
573 if (inums[i] == inum)
574 break;
575 }
576 if (i < n) {
577 if (nalts[i] < 255)
578 ++nalts[i];
579 } else if (n < USB_MAXINTERFACES) {
580 inums[n] = inum;
581 nalts[n] = 1;
582 ++n;
583 }
584
Craig W. Nadler165fe972007-06-15 23:14:35 -0400585 } else if (header->bDescriptorType ==
586 USB_DT_INTERFACE_ASSOCIATION) {
587 if (iad_num == USB_MAXIADS) {
588 dev_warn(ddev, "found more Interface "
589 "Association Descriptors "
590 "than allocated for in "
591 "configuration %d\n", cfgno);
592 } else {
593 config->intf_assoc[iad_num] =
594 (struct usb_interface_assoc_descriptor
595 *)header;
596 iad_num++;
597 }
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else if (header->bDescriptorType == USB_DT_DEVICE ||
600 header->bDescriptorType == USB_DT_CONFIG)
601 dev_warn(ddev, "config %d contains an unexpected "
602 "descriptor of type 0x%X, skipping\n",
603 cfgno, header->bDescriptorType);
604
605 } /* for ((buffer2 = buffer, size2 = size); ...) */
606 size = buffer2 - buffer;
607 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
608
609 if (n != nintf)
610 dev_warn(ddev, "config %d has %d interface%s, different from "
611 "the descriptor's value: %d\n",
612 cfgno, n, plural(n), nintf_orig);
613 else if (n == 0)
614 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
615 config->desc.bNumInterfaces = nintf = n;
616
617 /* Check for missing interface numbers */
618 for (i = 0; i < nintf; ++i) {
619 for (j = 0; j < nintf; ++j) {
620 if (inums[j] == i)
621 break;
622 }
623 if (j >= nintf)
624 dev_warn(ddev, "config %d has no interface number "
625 "%d\n", cfgno, i);
626 }
627
628 /* Allocate the usb_interface_caches and altsetting arrays */
629 for (i = 0; i < nintf; ++i) {
630 j = nalts[i];
631 if (j > USB_MAXALTSETTING) {
632 dev_warn(ddev, "too many alternate settings for "
633 "config %d interface %d: %d, "
634 "using maximum allowed: %d\n",
635 cfgno, inums[i], j, USB_MAXALTSETTING);
636 nalts[i] = j = USB_MAXALTSETTING;
637 }
638
639 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400640 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!intfc)
642 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 kref_init(&intfc->ref);
644 }
645
Sarah Sharp663c30d2009-04-27 19:58:14 -0700646 /* FIXME: parse the BOS descriptor */
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* Skip over any Class Specific or Vendor Specific descriptors;
649 * find the first interface descriptor */
650 config->extra = buffer;
651 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
652 USB_DT_INTERFACE, &n);
653 config->extralen = i;
654 if (n > 0)
655 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
656 n, plural(n), "configuration");
657 buffer += i;
658 size -= i;
659
660 /* Parse all the interface/altsetting descriptors */
661 while (size > 0) {
662 retval = usb_parse_interface(ddev, cfgno, config,
663 buffer, size, inums, nalts);
664 if (retval < 0)
665 return retval;
666
667 buffer += retval;
668 size -= retval;
669 }
670
671 /* Check for missing altsettings */
672 for (i = 0; i < nintf; ++i) {
673 intfc = config->intf_cache[i];
674 for (j = 0; j < intfc->num_altsetting; ++j) {
675 for (n = 0; n < intfc->num_altsetting; ++n) {
676 if (intfc->altsetting[n].desc.
677 bAlternateSetting == j)
678 break;
679 }
680 if (n >= intfc->num_altsetting)
681 dev_warn(ddev, "config %d interface %d has no "
682 "altsetting %d\n", cfgno, inums[i], j);
683 }
684 }
685
686 return 0;
687}
688
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800689/* hub-only!! ... and only exported for reset/reinit path.
690 * otherwise used internally on disconnect/destroy path
691 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692void usb_destroy_configuration(struct usb_device *dev)
693{
694 int c, i;
695
696 if (!dev->config)
697 return;
698
699 if (dev->rawdescriptors) {
700 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
701 kfree(dev->rawdescriptors[i]);
702
703 kfree(dev->rawdescriptors);
704 dev->rawdescriptors = NULL;
705 }
706
707 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
708 struct usb_host_config *cf = &dev->config[c];
709
710 kfree(cf->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
712 if (cf->intf_cache[i])
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800713 kref_put(&cf->intf_cache[i]->ref,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 usb_release_interface_cache);
715 }
716 }
717 kfree(dev->config);
718 dev->config = NULL;
719}
720
721
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700722/*
723 * Get the USB config descriptors, cache and parse'em
724 *
725 * hub-only!! ... and only in reset path, or usb_new_device()
726 * (used by real hubs and virtual root hubs)
727 *
728 * NOTE: if this is a WUSB device and is not authorized, we skip the
729 * whole thing. A non-authorized USB device has no
730 * configurations.
731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732int usb_get_configuration(struct usb_device *dev)
733{
734 struct device *ddev = &dev->dev;
735 int ncfg = dev->descriptor.bNumConfigurations;
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700736 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 unsigned int cfgno, length;
738 unsigned char *buffer;
739 unsigned char *bigbuffer;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800740 struct usb_config_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700742 cfgno = 0;
743 if (dev->authorized == 0) /* Not really an error */
744 goto out_not_authorized;
745 result = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (ncfg > USB_MAXCONFIG) {
747 dev_warn(ddev, "too many configurations: %d, "
748 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
749 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
750 }
751
752 if (ncfg < 1) {
753 dev_err(ddev, "no configurations\n");
754 return -EINVAL;
755 }
756
757 length = ncfg * sizeof(struct usb_host_config);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400758 dev->config = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!dev->config)
760 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 length = ncfg * sizeof(char *);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400763 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (!dev->rawdescriptors)
765 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
768 if (!buffer)
769 goto err2;
770 desc = (struct usb_config_descriptor *)buffer;
771
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700772 result = 0;
773 for (; cfgno < ncfg; cfgno++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* We grab just the first descriptor so we know how long
775 * the whole configuration is */
776 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
777 buffer, USB_DT_CONFIG_SIZE);
778 if (result < 0) {
779 dev_err(ddev, "unable to read config index %d "
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700780 "descriptor/%s: %d\n", cfgno, "start", result);
Inaky Perez-Gonzalezcb4c8fe2006-08-25 19:35:28 -0700781 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
782 dev->descriptor.bNumConfigurations = cfgno;
783 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 } else if (result < 4) {
785 dev_err(ddev, "config index %d descriptor too short "
786 "(expected %i, got %i)\n", cfgno,
787 USB_DT_CONFIG_SIZE, result);
788 result = -EINVAL;
789 goto err;
790 }
791 length = max((int) le16_to_cpu(desc->wTotalLength),
792 USB_DT_CONFIG_SIZE);
793
794 /* Now that we know the length, get the whole thing */
795 bigbuffer = kmalloc(length, GFP_KERNEL);
796 if (!bigbuffer) {
797 result = -ENOMEM;
798 goto err;
799 }
800 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
801 bigbuffer, length);
802 if (result < 0) {
803 dev_err(ddev, "unable to read config index %d "
804 "descriptor/%s\n", cfgno, "all");
805 kfree(bigbuffer);
806 goto err;
807 }
808 if (result < length) {
809 dev_warn(ddev, "config index %d descriptor too short "
810 "(expected %i, got %i)\n", cfgno, length, result);
811 length = result;
812 }
813
814 dev->rawdescriptors[cfgno] = bigbuffer;
815
Hans de Goede317149c2010-03-29 12:03:17 +0200816 result = usb_parse_configuration(dev, cfgno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 &dev->config[cfgno], bigbuffer, length);
818 if (result < 0) {
819 ++cfgno;
820 goto err;
821 }
822 }
823 result = 0;
824
825err:
826 kfree(buffer);
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700827out_not_authorized:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 dev->descriptor.bNumConfigurations = cfgno;
829err2:
830 if (result == -ENOMEM)
831 dev_err(ddev, "out of memory\n");
832 return result;
833}