OSDN Git Service

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