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