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