ffc69891318a68693273af79db205fd3478c04a1
[pandora-kernel.git] / drivers / usb / core / config.c
1 #include <linux/usb.h>
2 #include <linux/usb/ch9.h>
3 #include <linux/usb/hcd.h>
4 #include <linux/usb/quirks.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/device.h>
9 #include <asm/byteorder.h>
10 #include "usb.h"
11
12
13 #define USB_MAXALTSETTING               128     /* Hard limit */
14 #define USB_MAXENDPOINTS                30      /* Hard limit */
15
16 #define USB_MAXCONFIG                   8       /* Arbitrary limit */
17
18
19 static inline const char *plural(int n)
20 {
21         return (n == 1 ? "" : "s");
22 }
23
24 static int find_next_descriptor(unsigned char *buffer, int size,
25     int dt1, int dt2, int *num_skipped)
26 {
27         struct usb_descriptor_header *h;
28         int n = 0;
29         unsigned char *buffer0 = buffer;
30
31         /* Find the next descriptor of type dt1 or dt2 */
32         while (size > 0) {
33                 h = (struct usb_descriptor_header *) buffer;
34                 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
35                         break;
36                 buffer += h->bLength;
37                 size -= h->bLength;
38                 ++n;
39         }
40
41         /* Store the number of descriptors skipped and return the
42          * number of bytes skipped */
43         if (num_skipped)
44                 *num_skipped = n;
45         return buffer - buffer0;
46 }
47
48 static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
49                 int inum, int asnum, struct usb_host_endpoint *ep,
50                 unsigned char *buffer, int size)
51 {
52         struct usb_ss_ep_comp_descriptor *desc;
53         int max_tx;
54
55         /* The SuperSpeed endpoint companion descriptor is supposed to
56          * be the first thing immediately following the endpoint descriptor.
57          */
58         desc = (struct usb_ss_ep_comp_descriptor *) buffer;
59         if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
60                         size < USB_DT_SS_EP_COMP_SIZE) {
61                 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
62                                 " interface %d altsetting %d ep %d: "
63                                 "using minimum values\n",
64                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
65
66                 /* Fill in some default values.
67                  * Leave bmAttributes as zero, which will mean no streams for
68                  * bulk, and isoc won't support multiple bursts of packets.
69                  * With bursts of only one packet, and a Mult of 1, the max
70                  * amount of data moved per endpoint service interval is one
71                  * packet.
72                  */
73                 ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
74                 ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
75                 if (usb_endpoint_xfer_isoc(&ep->desc) ||
76                                 usb_endpoint_xfer_int(&ep->desc))
77                         ep->ss_ep_comp.wBytesPerInterval =
78                                         ep->desc.wMaxPacketSize;
79                 return;
80         }
81
82         memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
83
84         /* Check the various values */
85         if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
86                 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
87                                 "config %d interface %d altsetting %d ep %d: "
88                                 "setting to zero\n", desc->bMaxBurst,
89                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
90                 ep->ss_ep_comp.bMaxBurst = 0;
91         } else if (desc->bMaxBurst > 15) {
92                 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
93                                 "config %d interface %d altsetting %d ep %d: "
94                                 "setting to 15\n", desc->bMaxBurst,
95                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
96                 ep->ss_ep_comp.bMaxBurst = 15;
97         }
98
99         if ((usb_endpoint_xfer_control(&ep->desc) ||
100                         usb_endpoint_xfer_int(&ep->desc)) &&
101                                 desc->bmAttributes != 0) {
102                 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
103                                 "config %d interface %d altsetting %d ep %d: "
104                                 "setting to zero\n",
105                                 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
106                                 desc->bmAttributes,
107                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
108                 ep->ss_ep_comp.bmAttributes = 0;
109         } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
110                         desc->bmAttributes > 16) {
111                 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
112                                 "config %d interface %d altsetting %d ep %d: "
113                                 "setting to max\n",
114                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
115                 ep->ss_ep_comp.bmAttributes = 16;
116         } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
117                    USB_SS_MULT(desc->bmAttributes) > 3) {
118                 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
119                                 "config %d interface %d altsetting %d ep %d: "
120                                 "setting to 3\n",
121                                 USB_SS_MULT(desc->bmAttributes),
122                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
123                 ep->ss_ep_comp.bmAttributes = 2;
124         }
125
126         if (usb_endpoint_xfer_isoc(&ep->desc))
127                 max_tx = (desc->bMaxBurst + 1) *
128                         (USB_SS_MULT(desc->bmAttributes)) *
129                         usb_endpoint_maxp(&ep->desc);
130         else if (usb_endpoint_xfer_int(&ep->desc))
131                 max_tx = usb_endpoint_maxp(&ep->desc) *
132                         (desc->bMaxBurst + 1);
133         else
134                 max_tx = 999999;
135         if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
136                 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
137                                 "config %d interface %d altsetting %d ep %d: "
138                                 "setting to %d\n",
139                                 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
140                                 le16_to_cpu(desc->wBytesPerInterval),
141                                 cfgno, inum, asnum, ep->desc.bEndpointAddress,
142                                 max_tx);
143                 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
144         }
145 }
146
147 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
148     int asnum, struct usb_host_interface *ifp, int num_ep,
149     unsigned char *buffer, int size)
150 {
151         unsigned char *buffer0 = buffer;
152         struct usb_endpoint_descriptor *d;
153         struct usb_host_endpoint *endpoint;
154         int n, i, j, retval;
155
156         d = (struct usb_endpoint_descriptor *) buffer;
157         buffer += d->bLength;
158         size -= d->bLength;
159
160         if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
161                 n = USB_DT_ENDPOINT_AUDIO_SIZE;
162         else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
163                 n = USB_DT_ENDPOINT_SIZE;
164         else {
165                 dev_warn(ddev, "config %d interface %d altsetting %d has an "
166                     "invalid endpoint descriptor of length %d, skipping\n",
167                     cfgno, inum, asnum, d->bLength);
168                 goto skip_to_next_endpoint_or_interface_descriptor;
169         }
170
171         i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
172         if (i >= 16 || i == 0) {
173                 dev_warn(ddev, "config %d interface %d altsetting %d has an "
174                     "invalid endpoint with address 0x%X, skipping\n",
175                     cfgno, inum, asnum, d->bEndpointAddress);
176                 goto skip_to_next_endpoint_or_interface_descriptor;
177         }
178
179         /* Only store as many endpoints as we have room for */
180         if (ifp->desc.bNumEndpoints >= num_ep)
181                 goto skip_to_next_endpoint_or_interface_descriptor;
182
183         endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
184         ++ifp->desc.bNumEndpoints;
185
186         memcpy(&endpoint->desc, d, n);
187         INIT_LIST_HEAD(&endpoint->urb_list);
188
189         /* Fix up bInterval values outside the legal range. Use 32 ms if no
190          * proper value can be guessed. */
191         i = 0;          /* i = min, j = max, n = default */
192         j = 255;
193         if (usb_endpoint_xfer_int(d)) {
194                 i = 1;
195                 switch (to_usb_device(ddev)->speed) {
196                 case USB_SPEED_SUPER:
197                 case USB_SPEED_HIGH:
198                         /* Many device manufacturers are using full-speed
199                          * bInterval values in high-speed interrupt endpoint
200                          * descriptors. Try to fix those and fall back to a
201                          * 32 ms default value otherwise. */
202                         n = fls(d->bInterval*8);
203                         if (n == 0)
204                                 n = 9;  /* 32 ms = 2^(9-1) uframes */
205                         j = 16;
206
207                         /*
208                          * Adjust bInterval for quirked devices.
209                          * This quirk fixes bIntervals reported in
210                          * linear microframes.
211                          */
212                         if (to_usb_device(ddev)->quirks &
213                                 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
214                                 n = clamp(fls(d->bInterval), i, j);
215                                 i = j = n;
216                         }
217                         break;
218                 default:                /* USB_SPEED_FULL or _LOW */
219                         /* For low-speed, 10 ms is the official minimum.
220                          * But some "overclocked" devices might want faster
221                          * polling so we'll allow it. */
222                         n = 32;
223                         break;
224                 }
225         } else if (usb_endpoint_xfer_isoc(d)) {
226                 i = 1;
227                 j = 16;
228                 switch (to_usb_device(ddev)->speed) {
229                 case USB_SPEED_HIGH:
230                         n = 9;          /* 32 ms = 2^(9-1) uframes */
231                         break;
232                 default:                /* USB_SPEED_FULL */
233                         n = 6;          /* 32 ms = 2^(6-1) frames */
234                         break;
235                 }
236         }
237         if (d->bInterval < i || d->bInterval > j) {
238                 dev_warn(ddev, "config %d interface %d altsetting %d "
239                     "endpoint 0x%X has an invalid bInterval %d, "
240                     "changing to %d\n",
241                     cfgno, inum, asnum,
242                     d->bEndpointAddress, d->bInterval, n);
243                 endpoint->desc.bInterval = n;
244         }
245
246         /* Some buggy low-speed devices have Bulk endpoints, which is
247          * explicitly forbidden by the USB spec.  In an attempt to make
248          * them usable, we will try treating them as Interrupt endpoints.
249          */
250         if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
251                         usb_endpoint_xfer_bulk(d)) {
252                 dev_warn(ddev, "config %d interface %d altsetting %d "
253                     "endpoint 0x%X is Bulk; changing to Interrupt\n",
254                     cfgno, inum, asnum, d->bEndpointAddress);
255                 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
256                 endpoint->desc.bInterval = 1;
257                 if (usb_endpoint_maxp(&endpoint->desc) > 8)
258                         endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
259         }
260
261         /*
262          * Some buggy high speed devices have bulk endpoints using
263          * maxpacket sizes other than 512.  High speed HCDs may not
264          * be able to handle that particular bug, so let's warn...
265          */
266         if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
267                         && usb_endpoint_xfer_bulk(d)) {
268                 unsigned maxp;
269
270                 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
271                 if (maxp != 512)
272                         dev_warn(ddev, "config %d interface %d altsetting %d "
273                                 "bulk endpoint 0x%X has invalid maxpacket %d\n",
274                                 cfgno, inum, asnum, d->bEndpointAddress,
275                                 maxp);
276         }
277
278         /* Parse a possible SuperSpeed endpoint companion descriptor */
279         if (to_usb_device(ddev)->speed == USB_SPEED_SUPER)
280                 usb_parse_ss_endpoint_companion(ddev, cfgno,
281                                 inum, asnum, endpoint, buffer, size);
282
283         /* Skip over any Class Specific or Vendor Specific descriptors;
284          * find the next endpoint or interface descriptor */
285         endpoint->extra = buffer;
286         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
287                         USB_DT_INTERFACE, &n);
288         endpoint->extralen = i;
289         retval = buffer - buffer0 + i;
290         if (n > 0)
291                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
292                     n, plural(n), "endpoint");
293         return retval;
294
295 skip_to_next_endpoint_or_interface_descriptor:
296         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
297             USB_DT_INTERFACE, NULL);
298         return buffer - buffer0 + i;
299 }
300
301 void usb_release_interface_cache(struct kref *ref)
302 {
303         struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
304         int j;
305
306         for (j = 0; j < intfc->num_altsetting; j++) {
307                 struct usb_host_interface *alt = &intfc->altsetting[j];
308
309                 kfree(alt->endpoint);
310                 kfree(alt->string);
311         }
312         kfree(intfc);
313 }
314
315 static int usb_parse_interface(struct device *ddev, int cfgno,
316     struct usb_host_config *config, unsigned char *buffer, int size,
317     u8 inums[], u8 nalts[])
318 {
319         unsigned char *buffer0 = buffer;
320         struct usb_interface_descriptor *d;
321         int inum, asnum;
322         struct usb_interface_cache *intfc;
323         struct usb_host_interface *alt;
324         int i, n;
325         int len, retval;
326         int num_ep, num_ep_orig;
327
328         d = (struct usb_interface_descriptor *) buffer;
329         buffer += d->bLength;
330         size -= d->bLength;
331
332         if (d->bLength < USB_DT_INTERFACE_SIZE)
333                 goto skip_to_next_interface_descriptor;
334
335         /* Which interface entry is this? */
336         intfc = NULL;
337         inum = d->bInterfaceNumber;
338         for (i = 0; i < config->desc.bNumInterfaces; ++i) {
339                 if (inums[i] == inum) {
340                         intfc = config->intf_cache[i];
341                         break;
342                 }
343         }
344         if (!intfc || intfc->num_altsetting >= nalts[i])
345                 goto skip_to_next_interface_descriptor;
346
347         /* Check for duplicate altsetting entries */
348         asnum = d->bAlternateSetting;
349         for ((i = 0, alt = &intfc->altsetting[0]);
350               i < intfc->num_altsetting;
351              (++i, ++alt)) {
352                 if (alt->desc.bAlternateSetting == asnum) {
353                         dev_warn(ddev, "Duplicate descriptor for config %d "
354                             "interface %d altsetting %d, skipping\n",
355                             cfgno, inum, asnum);
356                         goto skip_to_next_interface_descriptor;
357                 }
358         }
359
360         ++intfc->num_altsetting;
361         memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
362
363         /* Skip over any Class Specific or Vendor Specific descriptors;
364          * find the first endpoint or interface descriptor */
365         alt->extra = buffer;
366         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
367             USB_DT_INTERFACE, &n);
368         alt->extralen = i;
369         if (n > 0)
370                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
371                     n, plural(n), "interface");
372         buffer += i;
373         size -= i;
374
375         /* Allocate space for the right(?) number of endpoints */
376         num_ep = num_ep_orig = alt->desc.bNumEndpoints;
377         alt->desc.bNumEndpoints = 0;            /* Use as a counter */
378         if (num_ep > USB_MAXENDPOINTS) {
379                 dev_warn(ddev, "too many endpoints for config %d interface %d "
380                     "altsetting %d: %d, using maximum allowed: %d\n",
381                     cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
382                 num_ep = USB_MAXENDPOINTS;
383         }
384
385         if (num_ep > 0) {
386                 /* Can't allocate 0 bytes */
387                 len = sizeof(struct usb_host_endpoint) * num_ep;
388                 alt->endpoint = kzalloc(len, GFP_KERNEL);
389                 if (!alt->endpoint)
390                         return -ENOMEM;
391         }
392
393         /* Parse all the endpoint descriptors */
394         n = 0;
395         while (size > 0) {
396                 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
397                      == USB_DT_INTERFACE)
398                         break;
399                 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
400                     num_ep, buffer, size);
401                 if (retval < 0)
402                         return retval;
403                 ++n;
404
405                 buffer += retval;
406                 size -= retval;
407         }
408
409         if (n != num_ep_orig)
410                 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
411                     "endpoint descriptor%s, different from the interface "
412                     "descriptor's value: %d\n",
413                     cfgno, inum, asnum, n, plural(n), num_ep_orig);
414         return buffer - buffer0;
415
416 skip_to_next_interface_descriptor:
417         i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
418             USB_DT_INTERFACE, NULL);
419         return buffer - buffer0 + i;
420 }
421
422 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
423     struct usb_host_config *config, unsigned char *buffer, int size)
424 {
425         struct device *ddev = &dev->dev;
426         unsigned char *buffer0 = buffer;
427         int cfgno;
428         int nintf, nintf_orig;
429         int i, j, n;
430         struct usb_interface_cache *intfc;
431         unsigned char *buffer2;
432         int size2;
433         struct usb_descriptor_header *header;
434         int len, retval;
435         u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
436         unsigned iad_num = 0;
437
438         memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
439         if (config->desc.bDescriptorType != USB_DT_CONFIG ||
440             config->desc.bLength < USB_DT_CONFIG_SIZE ||
441             config->desc.bLength > size) {
442                 dev_err(ddev, "invalid descriptor for config index %d: "
443                     "type = 0x%X, length = %d\n", cfgidx,
444                     config->desc.bDescriptorType, config->desc.bLength);
445                 return -EINVAL;
446         }
447         cfgno = config->desc.bConfigurationValue;
448
449         buffer += config->desc.bLength;
450         size -= config->desc.bLength;
451
452         nintf = nintf_orig = config->desc.bNumInterfaces;
453         if (nintf > USB_MAXINTERFACES) {
454                 dev_warn(ddev, "config %d has too many interfaces: %d, "
455                     "using maximum allowed: %d\n",
456                     cfgno, nintf, USB_MAXINTERFACES);
457                 nintf = USB_MAXINTERFACES;
458         }
459
460         /* Go through the descriptors, checking their length and counting the
461          * number of altsettings for each interface */
462         n = 0;
463         for ((buffer2 = buffer, size2 = size);
464               size2 > 0;
465              (buffer2 += header->bLength, size2 -= header->bLength)) {
466
467                 if (size2 < sizeof(struct usb_descriptor_header)) {
468                         dev_warn(ddev, "config %d descriptor has %d excess "
469                             "byte%s, ignoring\n",
470                             cfgno, size2, plural(size2));
471                         break;
472                 }
473
474                 header = (struct usb_descriptor_header *) buffer2;
475                 if ((header->bLength > size2) || (header->bLength < 2)) {
476                         dev_warn(ddev, "config %d has an invalid descriptor "
477                             "of length %d, skipping remainder of the config\n",
478                             cfgno, header->bLength);
479                         break;
480                 }
481
482                 if (header->bDescriptorType == USB_DT_INTERFACE) {
483                         struct usb_interface_descriptor *d;
484                         int inum;
485
486                         d = (struct usb_interface_descriptor *) header;
487                         if (d->bLength < USB_DT_INTERFACE_SIZE) {
488                                 dev_warn(ddev, "config %d has an invalid "
489                                     "interface descriptor of length %d, "
490                                     "skipping\n", cfgno, d->bLength);
491                                 continue;
492                         }
493
494                         inum = d->bInterfaceNumber;
495
496                         if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
497                             n >= nintf_orig) {
498                                 dev_warn(ddev, "config %d has more interface "
499                                     "descriptors, than it declares in "
500                                     "bNumInterfaces, ignoring interface "
501                                     "number: %d\n", cfgno, inum);
502                                 continue;
503                         }
504
505                         if (inum >= nintf_orig)
506                                 dev_warn(ddev, "config %d has an invalid "
507                                     "interface number: %d but max is %d\n",
508                                     cfgno, inum, nintf_orig - 1);
509
510                         /* Have we already encountered this interface?
511                          * Count its altsettings */
512                         for (i = 0; i < n; ++i) {
513                                 if (inums[i] == inum)
514                                         break;
515                         }
516                         if (i < n) {
517                                 if (nalts[i] < 255)
518                                         ++nalts[i];
519                         } else if (n < USB_MAXINTERFACES) {
520                                 inums[n] = inum;
521                                 nalts[n] = 1;
522                                 ++n;
523                         }
524
525                 } else if (header->bDescriptorType ==
526                                 USB_DT_INTERFACE_ASSOCIATION) {
527                         if (iad_num == USB_MAXIADS) {
528                                 dev_warn(ddev, "found more Interface "
529                                                "Association Descriptors "
530                                                "than allocated for in "
531                                                "configuration %d\n", cfgno);
532                         } else {
533                                 config->intf_assoc[iad_num] =
534                                         (struct usb_interface_assoc_descriptor
535                                         *)header;
536                                 iad_num++;
537                         }
538
539                 } else if (header->bDescriptorType == USB_DT_DEVICE ||
540                             header->bDescriptorType == USB_DT_CONFIG)
541                         dev_warn(ddev, "config %d contains an unexpected "
542                             "descriptor of type 0x%X, skipping\n",
543                             cfgno, header->bDescriptorType);
544
545         }       /* for ((buffer2 = buffer, size2 = size); ...) */
546         size = buffer2 - buffer;
547         config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
548
549         if (n != nintf)
550                 dev_warn(ddev, "config %d has %d interface%s, different from "
551                     "the descriptor's value: %d\n",
552                     cfgno, n, plural(n), nintf_orig);
553         else if (n == 0)
554                 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
555         config->desc.bNumInterfaces = nintf = n;
556
557         /* Check for missing interface numbers */
558         for (i = 0; i < nintf; ++i) {
559                 for (j = 0; j < nintf; ++j) {
560                         if (inums[j] == i)
561                                 break;
562                 }
563                 if (j >= nintf)
564                         dev_warn(ddev, "config %d has no interface number "
565                             "%d\n", cfgno, i);
566         }
567
568         /* Allocate the usb_interface_caches and altsetting arrays */
569         for (i = 0; i < nintf; ++i) {
570                 j = nalts[i];
571                 if (j > USB_MAXALTSETTING) {
572                         dev_warn(ddev, "too many alternate settings for "
573                             "config %d interface %d: %d, "
574                             "using maximum allowed: %d\n",
575                             cfgno, inums[i], j, USB_MAXALTSETTING);
576                         nalts[i] = j = USB_MAXALTSETTING;
577                 }
578
579                 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
580                 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
581                 if (!intfc)
582                         return -ENOMEM;
583                 kref_init(&intfc->ref);
584         }
585
586         /* FIXME: parse the BOS descriptor */
587
588         /* Skip over any Class Specific or Vendor Specific descriptors;
589          * find the first interface descriptor */
590         config->extra = buffer;
591         i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
592             USB_DT_INTERFACE, &n);
593         config->extralen = i;
594         if (n > 0)
595                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
596                     n, plural(n), "configuration");
597         buffer += i;
598         size -= i;
599
600         /* Parse all the interface/altsetting descriptors */
601         while (size > 0) {
602                 retval = usb_parse_interface(ddev, cfgno, config,
603                     buffer, size, inums, nalts);
604                 if (retval < 0)
605                         return retval;
606
607                 buffer += retval;
608                 size -= retval;
609         }
610
611         /* Check for missing altsettings */
612         for (i = 0; i < nintf; ++i) {
613                 intfc = config->intf_cache[i];
614                 for (j = 0; j < intfc->num_altsetting; ++j) {
615                         for (n = 0; n < intfc->num_altsetting; ++n) {
616                                 if (intfc->altsetting[n].desc.
617                                     bAlternateSetting == j)
618                                         break;
619                         }
620                         if (n >= intfc->num_altsetting)
621                                 dev_warn(ddev, "config %d interface %d has no "
622                                     "altsetting %d\n", cfgno, inums[i], j);
623                 }
624         }
625
626         return 0;
627 }
628
629 /* hub-only!! ... and only exported for reset/reinit path.
630  * otherwise used internally on disconnect/destroy path
631  */
632 void usb_destroy_configuration(struct usb_device *dev)
633 {
634         int c, i;
635
636         if (!dev->config)
637                 return;
638
639         if (dev->rawdescriptors) {
640                 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
641                         kfree(dev->rawdescriptors[i]);
642
643                 kfree(dev->rawdescriptors);
644                 dev->rawdescriptors = NULL;
645         }
646
647         for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
648                 struct usb_host_config *cf = &dev->config[c];
649
650                 kfree(cf->string);
651                 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
652                         if (cf->intf_cache[i])
653                                 kref_put(&cf->intf_cache[i]->ref,
654                                           usb_release_interface_cache);
655                 }
656         }
657         kfree(dev->config);
658         dev->config = NULL;
659 }
660
661
662 /*
663  * Get the USB config descriptors, cache and parse'em
664  *
665  * hub-only!! ... and only in reset path, or usb_new_device()
666  * (used by real hubs and virtual root hubs)
667  *
668  * NOTE: if this is a WUSB device and is not authorized, we skip the
669  *       whole thing. A non-authorized USB device has no
670  *       configurations.
671  */
672 int usb_get_configuration(struct usb_device *dev)
673 {
674         struct device *ddev = &dev->dev;
675         int ncfg = dev->descriptor.bNumConfigurations;
676         int result = 0;
677         unsigned int cfgno, length;
678         unsigned char *bigbuffer;
679         struct usb_config_descriptor *desc;
680
681         cfgno = 0;
682         if (dev->authorized == 0)       /* Not really an error */
683                 goto out_not_authorized;
684         result = -ENOMEM;
685         if (ncfg > USB_MAXCONFIG) {
686                 dev_warn(ddev, "too many configurations: %d, "
687                     "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
688                 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
689         }
690
691         if (ncfg < 1) {
692                 dev_err(ddev, "no configurations\n");
693                 return -EINVAL;
694         }
695
696         length = ncfg * sizeof(struct usb_host_config);
697         dev->config = kzalloc(length, GFP_KERNEL);
698         if (!dev->config)
699                 goto err2;
700
701         length = ncfg * sizeof(char *);
702         dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
703         if (!dev->rawdescriptors)
704                 goto err2;
705
706         desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
707         if (!desc)
708                 goto err2;
709
710         result = 0;
711         for (; cfgno < ncfg; cfgno++) {
712                 /* We grab just the first descriptor so we know how long
713                  * the whole configuration is */
714                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
715                     desc, USB_DT_CONFIG_SIZE);
716                 if (result < 0) {
717                         dev_err(ddev, "unable to read config index %d "
718                             "descriptor/%s: %d\n", cfgno, "start", result);
719                         dev_err(ddev, "chopping to %d config(s)\n", cfgno);
720                         dev->descriptor.bNumConfigurations = cfgno;
721                         break;
722                 } else if (result < 4) {
723                         dev_err(ddev, "config index %d descriptor too short "
724                             "(expected %i, got %i)\n", cfgno,
725                             USB_DT_CONFIG_SIZE, result);
726                         result = -EINVAL;
727                         goto err;
728                 }
729                 length = max((int) le16_to_cpu(desc->wTotalLength),
730                     USB_DT_CONFIG_SIZE);
731
732                 /* Now that we know the length, get the whole thing */
733                 bigbuffer = kmalloc(length, GFP_KERNEL);
734                 if (!bigbuffer) {
735                         result = -ENOMEM;
736                         goto err;
737                 }
738
739                 if (dev->quirks & USB_QUIRK_DELAY_INIT)
740                         msleep(100);
741
742                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
743                     bigbuffer, length);
744                 if (result < 0) {
745                         dev_err(ddev, "unable to read config index %d "
746                             "descriptor/%s\n", cfgno, "all");
747                         kfree(bigbuffer);
748                         goto err;
749                 }
750                 if (result < length) {
751                         dev_warn(ddev, "config index %d descriptor too short "
752                             "(expected %i, got %i)\n", cfgno, length, result);
753                         length = result;
754                 }
755
756                 dev->rawdescriptors[cfgno] = bigbuffer;
757
758                 result = usb_parse_configuration(dev, cfgno,
759                     &dev->config[cfgno], bigbuffer, length);
760                 if (result < 0) {
761                         ++cfgno;
762                         goto err;
763                 }
764         }
765         result = 0;
766
767 err:
768         kfree(desc);
769 out_not_authorized:
770         dev->descriptor.bNumConfigurations = cfgno;
771 err2:
772         if (result == -ENOMEM)
773                 dev_err(ddev, "out of memory\n");
774         return result;
775 }
776
777 void usb_release_bos_descriptor(struct usb_device *dev)
778 {
779         if (dev->bos) {
780                 kfree(dev->bos->desc);
781                 kfree(dev->bos);
782                 dev->bos = NULL;
783         }
784 }
785
786 /* Get BOS descriptor set */
787 int usb_get_bos_descriptor(struct usb_device *dev)
788 {
789         struct device *ddev = &dev->dev;
790         struct usb_bos_descriptor *bos;
791         struct usb_dev_cap_header *cap;
792         unsigned char *buffer;
793         int length, total_len, num, i;
794         int ret;
795
796         bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
797         if (!bos)
798                 return -ENOMEM;
799
800         /* Get BOS descriptor */
801         ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
802         if (ret < USB_DT_BOS_SIZE) {
803                 dev_err(ddev, "unable to get BOS descriptor\n");
804                 if (ret >= 0)
805                         ret = -ENOMSG;
806                 kfree(bos);
807                 return ret;
808         }
809
810         length = bos->bLength;
811         total_len = le16_to_cpu(bos->wTotalLength);
812         num = bos->bNumDeviceCaps;
813         kfree(bos);
814         if (total_len < length)
815                 return -EINVAL;
816
817         dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
818         if (!dev->bos)
819                 return -ENOMEM;
820
821         /* Now let's get the whole BOS descriptor set */
822         buffer = kzalloc(total_len, GFP_KERNEL);
823         if (!buffer) {
824                 ret = -ENOMEM;
825                 goto err;
826         }
827         dev->bos->desc = (struct usb_bos_descriptor *)buffer;
828
829         ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
830         if (ret < total_len) {
831                 dev_err(ddev, "unable to get BOS descriptor set\n");
832                 if (ret >= 0)
833                         ret = -ENOMSG;
834                 goto err;
835         }
836         total_len -= length;
837
838         for (i = 0; i < num; i++) {
839                 buffer += length;
840                 cap = (struct usb_dev_cap_header *)buffer;
841                 length = cap->bLength;
842
843                 if (total_len < length)
844                         break;
845                 total_len -= length;
846
847                 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
848                         dev_warn(ddev, "descriptor type invalid, skip\n");
849                         continue;
850                 }
851
852                 switch (cap->bDevCapabilityType) {
853                 case USB_CAP_TYPE_WIRELESS_USB:
854                         /* Wireless USB cap descriptor is handled by wusb */
855                         break;
856                 case USB_CAP_TYPE_EXT:
857                         dev->bos->ext_cap =
858                                 (struct usb_ext_cap_descriptor *)buffer;
859                         break;
860                 case USB_SS_CAP_TYPE:
861                         dev->bos->ss_cap =
862                                 (struct usb_ss_cap_descriptor *)buffer;
863                         break;
864                 case CONTAINER_ID_TYPE:
865                         dev->bos->ss_id =
866                                 (struct usb_ss_container_id_descriptor *)buffer;
867                         break;
868                 default:
869                         break;
870                 }
871         }
872
873         return 0;
874
875 err:
876         usb_release_bos_descriptor(dev);
877         return ret;
878 }