OSDN Git Service

USB: validate wMaxPacketValue entries in endpoint descriptors
[android-x86/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/slab.h>
7 #include <linux/device.h>
8 #include <asm/byteorder.h>
9 #include "usb.h"
10
11
12 #define USB_MAXALTSETTING               128     /* Hard limit */
13
14 #define USB_MAXCONFIG                   8       /* Arbitrary limit */
15
16
17 static inline const char *plural(int n)
18 {
19         return (n == 1 ? "" : "s");
20 }
21
22 static 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
46 static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
47                 int inum, int asnum, struct usb_host_endpoint *ep,
48                 unsigned char *buffer, int size)
49 {
50         struct usb_ss_ep_comp_descriptor *desc;
51         int max_tx;
52
53         /* The SuperSpeed endpoint companion descriptor is supposed to
54          * be the first thing immediately following the endpoint descriptor.
55          */
56         desc = (struct usb_ss_ep_comp_descriptor *) buffer;
57         if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
58                         size < USB_DT_SS_EP_COMP_SIZE) {
59                 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
60                                 " interface %d altsetting %d ep %d: "
61                                 "using minimum values\n",
62                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
63
64                 /* Fill in some default values.
65                  * Leave bmAttributes as zero, which will mean no streams for
66                  * bulk, and isoc won't support multiple bursts of packets.
67                  * With bursts of only one packet, and a Mult of 1, the max
68                  * amount of data moved per endpoint service interval is one
69                  * packet.
70                  */
71                 ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
72                 ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
73                 if (usb_endpoint_xfer_isoc(&ep->desc) ||
74                                 usb_endpoint_xfer_int(&ep->desc))
75                         ep->ss_ep_comp.wBytesPerInterval =
76                                         ep->desc.wMaxPacketSize;
77                 return;
78         }
79
80         memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
81
82         /* Check the various values */
83         if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
84                 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
85                                 "config %d interface %d altsetting %d ep %d: "
86                                 "setting to zero\n", desc->bMaxBurst,
87                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
88                 ep->ss_ep_comp.bMaxBurst = 0;
89         } else if (desc->bMaxBurst > 15) {
90                 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
91                                 "config %d interface %d altsetting %d ep %d: "
92                                 "setting to 15\n", desc->bMaxBurst,
93                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
94                 ep->ss_ep_comp.bMaxBurst = 15;
95         }
96
97         if ((usb_endpoint_xfer_control(&ep->desc) ||
98                         usb_endpoint_xfer_int(&ep->desc)) &&
99                                 desc->bmAttributes != 0) {
100                 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
101                                 "config %d interface %d altsetting %d ep %d: "
102                                 "setting to zero\n",
103                                 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
104                                 desc->bmAttributes,
105                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
106                 ep->ss_ep_comp.bmAttributes = 0;
107         } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
108                         desc->bmAttributes > 16) {
109                 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
110                                 "config %d interface %d altsetting %d ep %d: "
111                                 "setting to max\n",
112                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
113                 ep->ss_ep_comp.bmAttributes = 16;
114         } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
115                    USB_SS_MULT(desc->bmAttributes) > 3) {
116                 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
117                                 "config %d interface %d altsetting %d ep %d: "
118                                 "setting to 3\n",
119                                 USB_SS_MULT(desc->bmAttributes),
120                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
121                 ep->ss_ep_comp.bmAttributes = 2;
122         }
123
124         if (usb_endpoint_xfer_isoc(&ep->desc))
125                 max_tx = (desc->bMaxBurst + 1) *
126                         (USB_SS_MULT(desc->bmAttributes)) *
127                         usb_endpoint_maxp(&ep->desc);
128         else if (usb_endpoint_xfer_int(&ep->desc))
129                 max_tx = usb_endpoint_maxp(&ep->desc) *
130                         (desc->bMaxBurst + 1);
131         else
132                 max_tx = 999999;
133         if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
134                 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
135                                 "config %d interface %d altsetting %d ep %d: "
136                                 "setting to %d\n",
137                                 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
138                                 le16_to_cpu(desc->wBytesPerInterval),
139                                 cfgno, inum, asnum, ep->desc.bEndpointAddress,
140                                 max_tx);
141                 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
142         }
143 }
144
145 static const unsigned short low_speed_maxpacket_maxes[4] = {
146         [USB_ENDPOINT_XFER_CONTROL] = 8,
147         [USB_ENDPOINT_XFER_ISOC] = 0,
148         [USB_ENDPOINT_XFER_BULK] = 0,
149         [USB_ENDPOINT_XFER_INT] = 8,
150 };
151 static const unsigned short full_speed_maxpacket_maxes[4] = {
152         [USB_ENDPOINT_XFER_CONTROL] = 64,
153         [USB_ENDPOINT_XFER_ISOC] = 1023,
154         [USB_ENDPOINT_XFER_BULK] = 64,
155         [USB_ENDPOINT_XFER_INT] = 64,
156 };
157 static const unsigned short high_speed_maxpacket_maxes[4] = {
158         [USB_ENDPOINT_XFER_CONTROL] = 64,
159         [USB_ENDPOINT_XFER_ISOC] = 1024,
160         [USB_ENDPOINT_XFER_BULK] = 512,
161         [USB_ENDPOINT_XFER_INT] = 1023,
162 };
163 static const unsigned short super_speed_maxpacket_maxes[4] = {
164         [USB_ENDPOINT_XFER_CONTROL] = 512,
165         [USB_ENDPOINT_XFER_ISOC] = 1024,
166         [USB_ENDPOINT_XFER_BULK] = 1024,
167         [USB_ENDPOINT_XFER_INT] = 1024,
168 };
169
170 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
171     int asnum, struct usb_host_interface *ifp, int num_ep,
172     unsigned char *buffer, int size)
173 {
174         unsigned char *buffer0 = buffer;
175         struct usb_endpoint_descriptor *d;
176         struct usb_host_endpoint *endpoint;
177         int n, i, j, retval;
178         unsigned int maxp;
179         const unsigned short *maxpacket_maxes;
180
181         d = (struct usb_endpoint_descriptor *) buffer;
182         buffer += d->bLength;
183         size -= d->bLength;
184
185         if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
186                 n = USB_DT_ENDPOINT_AUDIO_SIZE;
187         else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
188                 n = USB_DT_ENDPOINT_SIZE;
189         else {
190                 dev_warn(ddev, "config %d interface %d altsetting %d has an "
191                     "invalid endpoint descriptor of length %d, skipping\n",
192                     cfgno, inum, asnum, d->bLength);
193                 goto skip_to_next_endpoint_or_interface_descriptor;
194         }
195
196         i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
197         if (i >= 16 || i == 0) {
198                 dev_warn(ddev, "config %d interface %d altsetting %d has an "
199                     "invalid endpoint with address 0x%X, skipping\n",
200                     cfgno, inum, asnum, d->bEndpointAddress);
201                 goto skip_to_next_endpoint_or_interface_descriptor;
202         }
203
204         /* Only store as many endpoints as we have room for */
205         if (ifp->desc.bNumEndpoints >= num_ep)
206                 goto skip_to_next_endpoint_or_interface_descriptor;
207
208         endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
209         ++ifp->desc.bNumEndpoints;
210
211         memcpy(&endpoint->desc, d, n);
212         INIT_LIST_HEAD(&endpoint->urb_list);
213
214         /* Fix up bInterval values outside the legal range. Use 32 ms if no
215          * proper value can be guessed. */
216         i = 0;          /* i = min, j = max, n = default */
217         j = 255;
218         if (usb_endpoint_xfer_int(d)) {
219                 i = 1;
220                 switch (to_usb_device(ddev)->speed) {
221                 case USB_SPEED_SUPER_PLUS:
222                 case USB_SPEED_SUPER:
223                 case USB_SPEED_HIGH:
224                         /* Many device manufacturers are using full-speed
225                          * bInterval values in high-speed interrupt endpoint
226                          * descriptors. Try to fix those and fall back to a
227                          * 32 ms default value otherwise. */
228                         n = fls(d->bInterval*8);
229                         if (n == 0)
230                                 n = 9;  /* 32 ms = 2^(9-1) uframes */
231                         j = 16;
232
233                         /*
234                          * Adjust bInterval for quirked devices.
235                          * This quirk fixes bIntervals reported in
236                          * linear microframes.
237                          */
238                         if (to_usb_device(ddev)->quirks &
239                                 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
240                                 n = clamp(fls(d->bInterval), i, j);
241                                 i = j = n;
242                         }
243                         break;
244                 default:                /* USB_SPEED_FULL or _LOW */
245                         /* For low-speed, 10 ms is the official minimum.
246                          * But some "overclocked" devices might want faster
247                          * polling so we'll allow it. */
248                         n = 32;
249                         break;
250                 }
251         } else if (usb_endpoint_xfer_isoc(d)) {
252                 i = 1;
253                 j = 16;
254                 switch (to_usb_device(ddev)->speed) {
255                 case USB_SPEED_HIGH:
256                         n = 9;          /* 32 ms = 2^(9-1) uframes */
257                         break;
258                 default:                /* USB_SPEED_FULL */
259                         n = 6;          /* 32 ms = 2^(6-1) frames */
260                         break;
261                 }
262         }
263         if (d->bInterval < i || d->bInterval > j) {
264                 dev_warn(ddev, "config %d interface %d altsetting %d "
265                     "endpoint 0x%X has an invalid bInterval %d, "
266                     "changing to %d\n",
267                     cfgno, inum, asnum,
268                     d->bEndpointAddress, d->bInterval, n);
269                 endpoint->desc.bInterval = n;
270         }
271
272         /* Some buggy low-speed devices have Bulk endpoints, which is
273          * explicitly forbidden by the USB spec.  In an attempt to make
274          * them usable, we will try treating them as Interrupt endpoints.
275          */
276         if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
277                         usb_endpoint_xfer_bulk(d)) {
278                 dev_warn(ddev, "config %d interface %d altsetting %d "
279                     "endpoint 0x%X is Bulk; changing to Interrupt\n",
280                     cfgno, inum, asnum, d->bEndpointAddress);
281                 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
282                 endpoint->desc.bInterval = 1;
283                 if (usb_endpoint_maxp(&endpoint->desc) > 8)
284                         endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
285         }
286
287         /* Validate the wMaxPacketSize field */
288         maxp = usb_endpoint_maxp(&endpoint->desc);
289
290         /* Find the highest legal maxpacket size for this endpoint */
291         i = 0;          /* additional transactions per microframe */
292         switch (to_usb_device(ddev)->speed) {
293         case USB_SPEED_LOW:
294                 maxpacket_maxes = low_speed_maxpacket_maxes;
295                 break;
296         case USB_SPEED_FULL:
297                 maxpacket_maxes = full_speed_maxpacket_maxes;
298                 break;
299         case USB_SPEED_HIGH:
300                 /* Bits 12..11 are allowed only for HS periodic endpoints */
301                 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
302                         i = maxp & (BIT(12) | BIT(11));
303                         maxp &= ~i;
304                 }
305                 /* fallthrough */
306         default:
307                 maxpacket_maxes = high_speed_maxpacket_maxes;
308                 break;
309         case USB_SPEED_SUPER:
310         case USB_SPEED_SUPER_PLUS:
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 int usb_get_configuration(struct usb_device *dev)
728 {
729         struct device *ddev = &dev->dev;
730         int ncfg = dev->descriptor.bNumConfigurations;
731         int result = 0;
732         unsigned int cfgno, length;
733         unsigned char *bigbuffer;
734         struct usb_config_descriptor *desc;
735
736         cfgno = 0;
737         result = -ENOMEM;
738         if (ncfg > USB_MAXCONFIG) {
739                 dev_warn(ddev, "too many configurations: %d, "
740                     "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
741                 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
742         }
743
744         if (ncfg < 1) {
745                 dev_err(ddev, "no configurations\n");
746                 return -EINVAL;
747         }
748
749         length = ncfg * sizeof(struct usb_host_config);
750         dev->config = kzalloc(length, GFP_KERNEL);
751         if (!dev->config)
752                 goto err2;
753
754         length = ncfg * sizeof(char *);
755         dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
756         if (!dev->rawdescriptors)
757                 goto err2;
758
759         desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
760         if (!desc)
761                 goto err2;
762
763         result = 0;
764         for (; cfgno < ncfg; cfgno++) {
765                 /* We grab just the first descriptor so we know how long
766                  * the whole configuration is */
767                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
768                     desc, USB_DT_CONFIG_SIZE);
769                 if (result < 0) {
770                         dev_err(ddev, "unable to read config index %d "
771                             "descriptor/%s: %d\n", cfgno, "start", result);
772                         if (result != -EPIPE)
773                                 goto err;
774                         dev_err(ddev, "chopping to %d config(s)\n", cfgno);
775                         dev->descriptor.bNumConfigurations = cfgno;
776                         break;
777                 } else if (result < 4) {
778                         dev_err(ddev, "config index %d descriptor too short "
779                             "(expected %i, got %i)\n", cfgno,
780                             USB_DT_CONFIG_SIZE, result);
781                         result = -EINVAL;
782                         goto err;
783                 }
784                 length = max((int) le16_to_cpu(desc->wTotalLength),
785                     USB_DT_CONFIG_SIZE);
786
787                 /* Now that we know the length, get the whole thing */
788                 bigbuffer = kmalloc(length, GFP_KERNEL);
789                 if (!bigbuffer) {
790                         result = -ENOMEM;
791                         goto err;
792                 }
793
794                 if (dev->quirks & USB_QUIRK_DELAY_INIT)
795                         msleep(100);
796
797                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
798                     bigbuffer, length);
799                 if (result < 0) {
800                         dev_err(ddev, "unable to read config index %d "
801                             "descriptor/%s\n", cfgno, "all");
802                         kfree(bigbuffer);
803                         goto err;
804                 }
805                 if (result < length) {
806                         dev_warn(ddev, "config index %d descriptor too short "
807                             "(expected %i, got %i)\n", cfgno, length, result);
808                         length = result;
809                 }
810
811                 dev->rawdescriptors[cfgno] = bigbuffer;
812
813                 result = usb_parse_configuration(dev, cfgno,
814                     &dev->config[cfgno], bigbuffer, length);
815                 if (result < 0) {
816                         ++cfgno;
817                         goto err;
818                 }
819         }
820         result = 0;
821
822 err:
823         kfree(desc);
824         dev->descriptor.bNumConfigurations = cfgno;
825 err2:
826         if (result == -ENOMEM)
827                 dev_err(ddev, "out of memory\n");
828         return result;
829 }
830
831 void usb_release_bos_descriptor(struct usb_device *dev)
832 {
833         if (dev->bos) {
834                 kfree(dev->bos->desc);
835                 kfree(dev->bos);
836                 dev->bos = NULL;
837         }
838 }
839
840 /* Get BOS descriptor set */
841 int usb_get_bos_descriptor(struct usb_device *dev)
842 {
843         struct device *ddev = &dev->dev;
844         struct usb_bos_descriptor *bos;
845         struct usb_dev_cap_header *cap;
846         unsigned char *buffer;
847         int length, total_len, num, i;
848         int ret;
849
850         bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
851         if (!bos)
852                 return -ENOMEM;
853
854         /* Get BOS descriptor */
855         ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
856         if (ret < USB_DT_BOS_SIZE) {
857                 dev_err(ddev, "unable to get BOS descriptor\n");
858                 if (ret >= 0)
859                         ret = -ENOMSG;
860                 kfree(bos);
861                 return ret;
862         }
863
864         length = bos->bLength;
865         total_len = le16_to_cpu(bos->wTotalLength);
866         num = bos->bNumDeviceCaps;
867         kfree(bos);
868         if (total_len < length)
869                 return -EINVAL;
870
871         dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
872         if (!dev->bos)
873                 return -ENOMEM;
874
875         /* Now let's get the whole BOS descriptor set */
876         buffer = kzalloc(total_len, GFP_KERNEL);
877         if (!buffer) {
878                 ret = -ENOMEM;
879                 goto err;
880         }
881         dev->bos->desc = (struct usb_bos_descriptor *)buffer;
882
883         ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
884         if (ret < total_len) {
885                 dev_err(ddev, "unable to get BOS descriptor set\n");
886                 if (ret >= 0)
887                         ret = -ENOMSG;
888                 goto err;
889         }
890         total_len -= length;
891
892         for (i = 0; i < num; i++) {
893                 buffer += length;
894                 cap = (struct usb_dev_cap_header *)buffer;
895                 length = cap->bLength;
896
897                 if (total_len < length)
898                         break;
899                 total_len -= length;
900
901                 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
902                         dev_warn(ddev, "descriptor type invalid, skip\n");
903                         continue;
904                 }
905
906                 switch (cap->bDevCapabilityType) {
907                 case USB_CAP_TYPE_WIRELESS_USB:
908                         /* Wireless USB cap descriptor is handled by wusb */
909                         break;
910                 case USB_CAP_TYPE_EXT:
911                         dev->bos->ext_cap =
912                                 (struct usb_ext_cap_descriptor *)buffer;
913                         break;
914                 case USB_SS_CAP_TYPE:
915                         dev->bos->ss_cap =
916                                 (struct usb_ss_cap_descriptor *)buffer;
917                         break;
918                 case USB_SSP_CAP_TYPE:
919                         dev->bos->ssp_cap =
920                                 (struct usb_ssp_cap_descriptor *)buffer;
921                         break;
922                 case CONTAINER_ID_TYPE:
923                         dev->bos->ss_id =
924                                 (struct usb_ss_container_id_descriptor *)buffer;
925                         break;
926                 default:
927                         break;
928                 }
929         }
930
931         return 0;
932
933 err:
934         usb_release_bos_descriptor(dev);
935         return ret;
936 }