blob: e9426acf5682d67fbe30a163af46a3dbc6ea17d4 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/module.h>
4#include <linux/init.h>
5#include <linux/slab.h>
6#include <linux/device.h>
7#include <asm/byteorder.h>
Greg KH6d5e8252005-04-18 17:39:24 -07008#include "usb.h"
9#include "hcd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#define USB_MAXALTSETTING 128 /* Hard limit */
12#define USB_MAXENDPOINTS 30 /* Hard limit */
13
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
46static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
47 int asnum, struct usb_host_interface *ifp, int num_ep,
48 unsigned char *buffer, int size)
49{
50 unsigned char *buffer0 = buffer;
51 struct usb_endpoint_descriptor *d;
52 struct usb_host_endpoint *endpoint;
Alan Stern615ae112007-06-08 15:23:27 -040053 int n, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 d = (struct usb_endpoint_descriptor *) buffer;
56 buffer += d->bLength;
57 size -= d->bLength;
58
59 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
60 n = USB_DT_ENDPOINT_AUDIO_SIZE;
61 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
62 n = USB_DT_ENDPOINT_SIZE;
63 else {
64 dev_warn(ddev, "config %d interface %d altsetting %d has an "
65 "invalid endpoint descriptor of length %d, skipping\n",
66 cfgno, inum, asnum, d->bLength);
67 goto skip_to_next_endpoint_or_interface_descriptor;
68 }
69
70 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
71 if (i >= 16 || i == 0) {
72 dev_warn(ddev, "config %d interface %d altsetting %d has an "
73 "invalid endpoint with address 0x%X, skipping\n",
74 cfgno, inum, asnum, d->bEndpointAddress);
75 goto skip_to_next_endpoint_or_interface_descriptor;
76 }
77
78 /* Only store as many endpoints as we have room for */
79 if (ifp->desc.bNumEndpoints >= num_ep)
80 goto skip_to_next_endpoint_or_interface_descriptor;
81
82 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
83 ++ifp->desc.bNumEndpoints;
84
85 memcpy(&endpoint->desc, d, n);
86 INIT_LIST_HEAD(&endpoint->urb_list);
87
Laurent Pinchart300871c2007-06-12 21:47:17 +020088 /* Fix up bInterval values outside the legal range. Use 32 ms if no
89 * proper value can be guessed. */
Alan Stern615ae112007-06-08 15:23:27 -040090 i = 0; /* i = min, j = max, n = default */
91 j = 255;
92 if (usb_endpoint_xfer_int(d)) {
93 i = 1;
94 switch (to_usb_device(ddev)->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -070095 case USB_SPEED_SUPER:
Alan Stern615ae112007-06-08 15:23:27 -040096 case USB_SPEED_HIGH:
Laurent Pinchart300871c2007-06-12 21:47:17 +020097 /* Many device manufacturers are using full-speed
98 * bInterval values in high-speed interrupt endpoint
99 * descriptors. Try to fix those and fall back to a
100 * 32 ms default value otherwise. */
101 n = fls(d->bInterval*8);
102 if (n == 0)
103 n = 9; /* 32 ms = 2^(9-1) uframes */
Alan Stern615ae112007-06-08 15:23:27 -0400104 j = 16;
105 break;
106 default: /* USB_SPEED_FULL or _LOW */
107 /* For low-speed, 10 ms is the official minimum.
108 * But some "overclocked" devices might want faster
109 * polling so we'll allow it. */
110 n = 32;
111 break;
112 }
113 } else if (usb_endpoint_xfer_isoc(d)) {
114 i = 1;
115 j = 16;
116 switch (to_usb_device(ddev)->speed) {
117 case USB_SPEED_HIGH:
118 n = 9; /* 32 ms = 2^(9-1) uframes */
119 break;
120 default: /* USB_SPEED_FULL */
121 n = 6; /* 32 ms = 2^(6-1) frames */
122 break;
123 }
124 }
125 if (d->bInterval < i || d->bInterval > j) {
126 dev_warn(ddev, "config %d interface %d altsetting %d "
127 "endpoint 0x%X has an invalid bInterval %d, "
128 "changing to %d\n",
129 cfgno, inum, asnum,
130 d->bEndpointAddress, d->bInterval, n);
131 endpoint->desc.bInterval = n;
132 }
133
Alan Stern60aac1e2007-06-08 15:25:02 -0400134 /* Some buggy low-speed devices have Bulk endpoints, which is
135 * explicitly forbidden by the USB spec. In an attempt to make
136 * them usable, we will try treating them as Interrupt endpoints.
137 */
138 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
139 usb_endpoint_xfer_bulk(d)) {
140 dev_warn(ddev, "config %d interface %d altsetting %d "
141 "endpoint 0x%X is Bulk; changing to Interrupt\n",
142 cfgno, inum, asnum, d->bEndpointAddress);
143 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
144 endpoint->desc.bInterval = 1;
145 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
146 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
147 }
148
David Brownellcaa9ef62008-02-08 15:08:44 -0800149 /*
150 * Some buggy high speed devices have bulk endpoints using
151 * maxpacket sizes other than 512. High speed HCDs may not
152 * be able to handle that particular bug, so let's warn...
153 */
154 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
155 && usb_endpoint_xfer_bulk(d)) {
156 unsigned maxp;
157
158 maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
159 if (maxp != 512)
160 dev_warn(ddev, "config %d interface %d altsetting %d "
161 "bulk endpoint 0x%X has invalid maxpacket %d\n",
162 cfgno, inum, asnum, d->bEndpointAddress,
163 maxp);
164 }
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* Skip over any Class Specific or Vendor Specific descriptors;
167 * find the next endpoint or interface descriptor */
168 endpoint->extra = buffer;
169 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
170 USB_DT_INTERFACE, &n);
171 endpoint->extralen = i;
172 if (n > 0)
173 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
174 n, plural(n), "endpoint");
175 return buffer - buffer0 + i;
176
177skip_to_next_endpoint_or_interface_descriptor:
178 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
179 USB_DT_INTERFACE, NULL);
180 return buffer - buffer0 + i;
181}
182
183void usb_release_interface_cache(struct kref *ref)
184{
185 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
186 int j;
187
Alan Stern4f62efe2005-10-24 16:24:14 -0400188 for (j = 0; j < intfc->num_altsetting; j++) {
189 struct usb_host_interface *alt = &intfc->altsetting[j];
190
191 kfree(alt->endpoint);
192 kfree(alt->string);
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 kfree(intfc);
195}
196
197static int usb_parse_interface(struct device *ddev, int cfgno,
198 struct usb_host_config *config, unsigned char *buffer, int size,
199 u8 inums[], u8 nalts[])
200{
201 unsigned char *buffer0 = buffer;
202 struct usb_interface_descriptor *d;
203 int inum, asnum;
204 struct usb_interface_cache *intfc;
205 struct usb_host_interface *alt;
206 int i, n;
207 int len, retval;
208 int num_ep, num_ep_orig;
209
210 d = (struct usb_interface_descriptor *) buffer;
211 buffer += d->bLength;
212 size -= d->bLength;
213
214 if (d->bLength < USB_DT_INTERFACE_SIZE)
215 goto skip_to_next_interface_descriptor;
216
217 /* Which interface entry is this? */
218 intfc = NULL;
219 inum = d->bInterfaceNumber;
220 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
221 if (inums[i] == inum) {
222 intfc = config->intf_cache[i];
223 break;
224 }
225 }
226 if (!intfc || intfc->num_altsetting >= nalts[i])
227 goto skip_to_next_interface_descriptor;
228
229 /* Check for duplicate altsetting entries */
230 asnum = d->bAlternateSetting;
231 for ((i = 0, alt = &intfc->altsetting[0]);
232 i < intfc->num_altsetting;
233 (++i, ++alt)) {
234 if (alt->desc.bAlternateSetting == asnum) {
235 dev_warn(ddev, "Duplicate descriptor for config %d "
236 "interface %d altsetting %d, skipping\n",
237 cfgno, inum, asnum);
238 goto skip_to_next_interface_descriptor;
239 }
240 }
241
242 ++intfc->num_altsetting;
243 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
244
245 /* Skip over any Class Specific or Vendor Specific descriptors;
246 * find the first endpoint or interface descriptor */
247 alt->extra = buffer;
248 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
249 USB_DT_INTERFACE, &n);
250 alt->extralen = i;
251 if (n > 0)
252 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
253 n, plural(n), "interface");
254 buffer += i;
255 size -= i;
256
257 /* Allocate space for the right(?) number of endpoints */
258 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800259 alt->desc.bNumEndpoints = 0; /* Use as a counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (num_ep > USB_MAXENDPOINTS) {
261 dev_warn(ddev, "too many endpoints for config %d interface %d "
262 "altsetting %d: %d, using maximum allowed: %d\n",
263 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
264 num_ep = USB_MAXENDPOINTS;
265 }
266
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800267 if (num_ep > 0) {
268 /* Can't allocate 0 bytes */
Alan Stern57a21c12007-05-15 17:40:37 -0400269 len = sizeof(struct usb_host_endpoint) * num_ep;
270 alt->endpoint = kzalloc(len, GFP_KERNEL);
271 if (!alt->endpoint)
272 return -ENOMEM;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* Parse all the endpoint descriptors */
276 n = 0;
277 while (size > 0) {
278 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
279 == USB_DT_INTERFACE)
280 break;
281 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
282 num_ep, buffer, size);
283 if (retval < 0)
284 return retval;
285 ++n;
286
287 buffer += retval;
288 size -= retval;
289 }
290
291 if (n != num_ep_orig)
292 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
293 "endpoint descriptor%s, different from the interface "
294 "descriptor's value: %d\n",
295 cfgno, inum, asnum, n, plural(n), num_ep_orig);
296 return buffer - buffer0;
297
298skip_to_next_interface_descriptor:
299 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
300 USB_DT_INTERFACE, NULL);
301 return buffer - buffer0 + i;
302}
303
304static int usb_parse_configuration(struct device *ddev, int cfgidx,
305 struct usb_host_config *config, unsigned char *buffer, int size)
306{
307 unsigned char *buffer0 = buffer;
308 int cfgno;
309 int nintf, nintf_orig;
310 int i, j, n;
311 struct usb_interface_cache *intfc;
312 unsigned char *buffer2;
313 int size2;
314 struct usb_descriptor_header *header;
315 int len, retval;
316 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
Craig W. Nadler165fe972007-06-15 23:14:35 -0400317 unsigned iad_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
320 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
321 config->desc.bLength < USB_DT_CONFIG_SIZE) {
322 dev_err(ddev, "invalid descriptor for config index %d: "
323 "type = 0x%X, length = %d\n", cfgidx,
324 config->desc.bDescriptorType, config->desc.bLength);
325 return -EINVAL;
326 }
327 cfgno = config->desc.bConfigurationValue;
328
329 buffer += config->desc.bLength;
330 size -= config->desc.bLength;
331
332 nintf = nintf_orig = config->desc.bNumInterfaces;
333 if (nintf > USB_MAXINTERFACES) {
334 dev_warn(ddev, "config %d has too many interfaces: %d, "
335 "using maximum allowed: %d\n",
336 cfgno, nintf, USB_MAXINTERFACES);
337 nintf = USB_MAXINTERFACES;
338 }
339
340 /* Go through the descriptors, checking their length and counting the
341 * number of altsettings for each interface */
342 n = 0;
343 for ((buffer2 = buffer, size2 = size);
344 size2 > 0;
345 (buffer2 += header->bLength, size2 -= header->bLength)) {
346
347 if (size2 < sizeof(struct usb_descriptor_header)) {
348 dev_warn(ddev, "config %d descriptor has %d excess "
349 "byte%s, ignoring\n",
350 cfgno, size2, plural(size2));
351 break;
352 }
353
354 header = (struct usb_descriptor_header *) buffer2;
355 if ((header->bLength > size2) || (header->bLength < 2)) {
356 dev_warn(ddev, "config %d has an invalid descriptor "
357 "of length %d, skipping remainder of the config\n",
358 cfgno, header->bLength);
359 break;
360 }
361
362 if (header->bDescriptorType == USB_DT_INTERFACE) {
363 struct usb_interface_descriptor *d;
364 int inum;
365
366 d = (struct usb_interface_descriptor *) header;
367 if (d->bLength < USB_DT_INTERFACE_SIZE) {
368 dev_warn(ddev, "config %d has an invalid "
369 "interface descriptor of length %d, "
370 "skipping\n", cfgno, d->bLength);
371 continue;
372 }
373
374 inum = d->bInterfaceNumber;
375 if (inum >= nintf_orig)
376 dev_warn(ddev, "config %d has an invalid "
377 "interface number: %d but max is %d\n",
378 cfgno, inum, nintf_orig - 1);
379
380 /* Have we already encountered this interface?
381 * Count its altsettings */
382 for (i = 0; i < n; ++i) {
383 if (inums[i] == inum)
384 break;
385 }
386 if (i < n) {
387 if (nalts[i] < 255)
388 ++nalts[i];
389 } else if (n < USB_MAXINTERFACES) {
390 inums[n] = inum;
391 nalts[n] = 1;
392 ++n;
393 }
394
Craig W. Nadler165fe972007-06-15 23:14:35 -0400395 } else if (header->bDescriptorType ==
396 USB_DT_INTERFACE_ASSOCIATION) {
397 if (iad_num == USB_MAXIADS) {
398 dev_warn(ddev, "found more Interface "
399 "Association Descriptors "
400 "than allocated for in "
401 "configuration %d\n", cfgno);
402 } else {
403 config->intf_assoc[iad_num] =
404 (struct usb_interface_assoc_descriptor
405 *)header;
406 iad_num++;
407 }
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 } else if (header->bDescriptorType == USB_DT_DEVICE ||
410 header->bDescriptorType == USB_DT_CONFIG)
411 dev_warn(ddev, "config %d contains an unexpected "
412 "descriptor of type 0x%X, skipping\n",
413 cfgno, header->bDescriptorType);
414
415 } /* for ((buffer2 = buffer, size2 = size); ...) */
416 size = buffer2 - buffer;
417 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
418
419 if (n != nintf)
420 dev_warn(ddev, "config %d has %d interface%s, different from "
421 "the descriptor's value: %d\n",
422 cfgno, n, plural(n), nintf_orig);
423 else if (n == 0)
424 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
425 config->desc.bNumInterfaces = nintf = n;
426
427 /* Check for missing interface numbers */
428 for (i = 0; i < nintf; ++i) {
429 for (j = 0; j < nintf; ++j) {
430 if (inums[j] == i)
431 break;
432 }
433 if (j >= nintf)
434 dev_warn(ddev, "config %d has no interface number "
435 "%d\n", cfgno, i);
436 }
437
438 /* Allocate the usb_interface_caches and altsetting arrays */
439 for (i = 0; i < nintf; ++i) {
440 j = nalts[i];
441 if (j > USB_MAXALTSETTING) {
442 dev_warn(ddev, "too many alternate settings for "
443 "config %d interface %d: %d, "
444 "using maximum allowed: %d\n",
445 cfgno, inums[i], j, USB_MAXALTSETTING);
446 nalts[i] = j = USB_MAXALTSETTING;
447 }
448
449 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400450 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!intfc)
452 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 kref_init(&intfc->ref);
454 }
455
456 /* Skip over any Class Specific or Vendor Specific descriptors;
457 * find the first interface descriptor */
458 config->extra = buffer;
459 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
460 USB_DT_INTERFACE, &n);
461 config->extralen = i;
462 if (n > 0)
463 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
464 n, plural(n), "configuration");
465 buffer += i;
466 size -= i;
467
468 /* Parse all the interface/altsetting descriptors */
469 while (size > 0) {
470 retval = usb_parse_interface(ddev, cfgno, config,
471 buffer, size, inums, nalts);
472 if (retval < 0)
473 return retval;
474
475 buffer += retval;
476 size -= retval;
477 }
478
479 /* Check for missing altsettings */
480 for (i = 0; i < nintf; ++i) {
481 intfc = config->intf_cache[i];
482 for (j = 0; j < intfc->num_altsetting; ++j) {
483 for (n = 0; n < intfc->num_altsetting; ++n) {
484 if (intfc->altsetting[n].desc.
485 bAlternateSetting == j)
486 break;
487 }
488 if (n >= intfc->num_altsetting)
489 dev_warn(ddev, "config %d interface %d has no "
490 "altsetting %d\n", cfgno, inums[i], j);
491 }
492 }
493
494 return 0;
495}
496
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800497/* hub-only!! ... and only exported for reset/reinit path.
498 * otherwise used internally on disconnect/destroy path
499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500void usb_destroy_configuration(struct usb_device *dev)
501{
502 int c, i;
503
504 if (!dev->config)
505 return;
506
507 if (dev->rawdescriptors) {
508 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
509 kfree(dev->rawdescriptors[i]);
510
511 kfree(dev->rawdescriptors);
512 dev->rawdescriptors = NULL;
513 }
514
515 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
516 struct usb_host_config *cf = &dev->config[c];
517
518 kfree(cf->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
520 if (cf->intf_cache[i])
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800521 kref_put(&cf->intf_cache[i]->ref,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 usb_release_interface_cache);
523 }
524 }
525 kfree(dev->config);
526 dev->config = NULL;
527}
528
529
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700530/*
531 * Get the USB config descriptors, cache and parse'em
532 *
533 * hub-only!! ... and only in reset path, or usb_new_device()
534 * (used by real hubs and virtual root hubs)
535 *
536 * NOTE: if this is a WUSB device and is not authorized, we skip the
537 * whole thing. A non-authorized USB device has no
538 * configurations.
539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540int usb_get_configuration(struct usb_device *dev)
541{
542 struct device *ddev = &dev->dev;
543 int ncfg = dev->descriptor.bNumConfigurations;
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700544 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned int cfgno, length;
546 unsigned char *buffer;
547 unsigned char *bigbuffer;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800548 struct usb_config_descriptor *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700550 cfgno = 0;
551 if (dev->authorized == 0) /* Not really an error */
552 goto out_not_authorized;
553 result = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (ncfg > USB_MAXCONFIG) {
555 dev_warn(ddev, "too many configurations: %d, "
556 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
557 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
558 }
559
560 if (ncfg < 1) {
561 dev_err(ddev, "no configurations\n");
562 return -EINVAL;
563 }
564
565 length = ncfg * sizeof(struct usb_host_config);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400566 dev->config = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!dev->config)
568 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 length = ncfg * sizeof(char *);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400571 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!dev->rawdescriptors)
573 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
576 if (!buffer)
577 goto err2;
578 desc = (struct usb_config_descriptor *)buffer;
579
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700580 result = 0;
581 for (; cfgno < ncfg; cfgno++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* We grab just the first descriptor so we know how long
583 * the whole configuration is */
584 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
585 buffer, USB_DT_CONFIG_SIZE);
586 if (result < 0) {
587 dev_err(ddev, "unable to read config index %d "
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700588 "descriptor/%s: %d\n", cfgno, "start", result);
Inaky Perez-Gonzalezcb4c8fe2006-08-25 19:35:28 -0700589 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
590 dev->descriptor.bNumConfigurations = cfgno;
591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 } else if (result < 4) {
593 dev_err(ddev, "config index %d descriptor too short "
594 "(expected %i, got %i)\n", cfgno,
595 USB_DT_CONFIG_SIZE, result);
596 result = -EINVAL;
597 goto err;
598 }
599 length = max((int) le16_to_cpu(desc->wTotalLength),
600 USB_DT_CONFIG_SIZE);
601
602 /* Now that we know the length, get the whole thing */
603 bigbuffer = kmalloc(length, GFP_KERNEL);
604 if (!bigbuffer) {
605 result = -ENOMEM;
606 goto err;
607 }
608 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
609 bigbuffer, length);
610 if (result < 0) {
611 dev_err(ddev, "unable to read config index %d "
612 "descriptor/%s\n", cfgno, "all");
613 kfree(bigbuffer);
614 goto err;
615 }
616 if (result < length) {
617 dev_warn(ddev, "config index %d descriptor too short "
618 "(expected %i, got %i)\n", cfgno, length, result);
619 length = result;
620 }
621
622 dev->rawdescriptors[cfgno] = bigbuffer;
623
624 result = usb_parse_configuration(&dev->dev, cfgno,
625 &dev->config[cfgno], bigbuffer, length);
626 if (result < 0) {
627 ++cfgno;
628 goto err;
629 }
630 }
631 result = 0;
632
633err:
634 kfree(buffer);
Inaky Perez-Gonzalez11450652007-07-31 20:34:02 -0700635out_not_authorized:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 dev->descriptor.bNumConfigurations = cfgno;
637err2:
638 if (result == -ENOMEM)
639 dev_err(ddev, "out of memory\n");
640 return result;
641}