OSDN Git Service

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