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