USB: xhci: Bandwidth allocation support
[linux-2.6] / drivers / usb / core / config.c
1 #include <linux/usb.h>
2 #include <linux/usb/ch9.h>
3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 #include <linux/device.h>
7 #include <asm/byteorder.h>
8 #include "usb.h"
9 #include "hcd.h"
10
11 #define USB_MAXALTSETTING               128     /* Hard limit */
12 #define USB_MAXENDPOINTS                30      /* 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 /* FIXME: this is a kludge */
23 static int find_next_descriptor_more(unsigned char *buffer, int size,
24     int dt1, int dt2, int dt3, int *num_skipped)
25 {
26         struct usb_descriptor_header *h;
27         int n = 0;
28         unsigned char *buffer0 = buffer;
29
30         /* Find the next descriptor of type dt1 or dt2 or dt3 */
31         while (size > 0) {
32                 h = (struct usb_descriptor_header *) buffer;
33                 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2 ||
34                                 h->bDescriptorType == dt3)
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 int find_next_descriptor(unsigned char *buffer, int size,
49     int dt1, int dt2, int *num_skipped)
50 {
51         struct usb_descriptor_header *h;
52         int n = 0;
53         unsigned char *buffer0 = buffer;
54
55         /* Find the next descriptor of type dt1 or dt2 */
56         while (size > 0) {
57                 h = (struct usb_descriptor_header *) buffer;
58                 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
59                         break;
60                 buffer += h->bLength;
61                 size -= h->bLength;
62                 ++n;
63         }
64
65         /* Store the number of descriptors skipped and return the
66          * number of bytes skipped */
67         if (num_skipped)
68                 *num_skipped = n;
69         return buffer - buffer0;
70 }
71
72 static int usb_parse_endpoint_companion(struct device *ddev, int cfgno,
73                 int inum, int asnum, struct usb_host_endpoint *ep,
74                 int num_ep, unsigned char *buffer, int size)
75 {
76         unsigned char *buffer_start = buffer;
77         struct usb_ep_comp_descriptor   *desc;
78         int retval;
79         int num_skipped;
80         int max_tx;
81         int i;
82
83         /* Allocate space for the companion descriptor */
84         ep->ep_comp = kzalloc(sizeof(struct usb_host_ep_comp), GFP_KERNEL);
85         if (!ep->ep_comp)
86                 return -ENOMEM;
87         desc = (struct usb_ep_comp_descriptor *) buffer;
88         if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP) {
89                 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
90                                 " interface %d altsetting %d ep %d: "
91                                 "using minimum values\n",
92                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
93                 ep->ep_comp->desc.bLength = USB_DT_EP_COMP_SIZE;
94                 ep->ep_comp->desc.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
95                 ep->ep_comp->desc.bMaxBurst = 0;
96                 /*
97                  * Leave bmAttributes as zero, which will mean no streams for
98                  * bulk, and isoc won't support multiple bursts of packets.
99                  * With bursts of only one packet, and a Mult of 1, the max
100                  * amount of data moved per endpoint service interval is one
101                  * packet.
102                  */
103                 if (usb_endpoint_xfer_isoc(&ep->desc) ||
104                                 usb_endpoint_xfer_int(&ep->desc))
105                         ep->ep_comp->desc.wBytesPerInterval =
106                                 ep->desc.wMaxPacketSize;
107                 /*
108                  * The next descriptor is for an Endpoint or Interface,
109                  * no extra descriptors to copy into the companion structure,
110                  * and we didn't eat up any of the buffer.
111                  */
112                 retval = 0;
113                 goto valid;
114         }
115         memcpy(&ep->ep_comp->desc, desc, USB_DT_EP_COMP_SIZE);
116         desc = &ep->ep_comp->desc;
117         buffer += desc->bLength;
118         size -= desc->bLength;
119
120         /* Eat up the other descriptors we don't care about */
121         ep->ep_comp->extra = buffer;
122         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
123                         USB_DT_INTERFACE, &num_skipped);
124         ep->ep_comp->extralen = i;
125         buffer += i;
126         size -= i;
127         retval = buffer - buffer_start + i;
128         if (num_skipped > 0)
129                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
130                                 num_skipped, plural(num_skipped),
131                                 "SuperSpeed endpoint companion");
132
133         /* Check the various values */
134         if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
135                 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
136                                 "config %d interface %d altsetting %d ep %d: "
137                                 "setting to zero\n", desc->bMaxBurst,
138                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
139                 desc->bMaxBurst = 0;
140         }
141         if (desc->bMaxBurst > 15) {
142                 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
143                                 "config %d interface %d altsetting %d ep %d: "
144                                 "setting to 15\n", desc->bMaxBurst,
145                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
146                 desc->bMaxBurst = 15;
147         }
148         if ((usb_endpoint_xfer_control(&ep->desc) || usb_endpoint_xfer_int(&ep->desc))
149                         && desc->bmAttributes != 0) {
150                 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
151                                 "config %d interface %d altsetting %d ep %d: "
152                                 "setting to zero\n",
153                                 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
154                                 desc->bmAttributes,
155                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
156                 desc->bmAttributes = 0;
157         }
158         if (usb_endpoint_xfer_bulk(&ep->desc) && desc->bmAttributes > 16) {
159                 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
160                                 "config %d interface %d altsetting %d ep %d: "
161                                 "setting to max\n",
162                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
163                 desc->bmAttributes = 16;
164         }
165         if (usb_endpoint_xfer_isoc(&ep->desc) && desc->bmAttributes > 2) {
166                 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
167                                 "config %d interface %d altsetting %d ep %d: "
168                                 "setting to 3\n", desc->bmAttributes + 1,
169                                 cfgno, inum, asnum, ep->desc.bEndpointAddress);
170                 desc->bmAttributes = 2;
171         }
172         if (usb_endpoint_xfer_isoc(&ep->desc)) {
173                 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1) *
174                         (desc->bmAttributes + 1);
175         } else if (usb_endpoint_xfer_int(&ep->desc)) {
176                 max_tx = ep->desc.wMaxPacketSize * (desc->bMaxBurst + 1);
177         } else {
178                 goto valid;
179         }
180         if (desc->wBytesPerInterval > max_tx) {
181                 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
182                                 "config %d interface %d altsetting %d ep %d: "
183                                 "setting to %d\n",
184                                 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
185                                 desc->wBytesPerInterval,
186                                 cfgno, inum, asnum, ep->desc.bEndpointAddress,
187                                 max_tx);
188                 desc->wBytesPerInterval = max_tx;
189         }
190 valid:
191         return retval;
192 }
193
194 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
195     int asnum, struct usb_host_interface *ifp, int num_ep,
196     unsigned char *buffer, int size)
197 {
198         unsigned char *buffer0 = buffer;
199         struct usb_endpoint_descriptor *d;
200         struct usb_host_endpoint *endpoint;
201         int n, i, j, retval;
202
203         d = (struct usb_endpoint_descriptor *) buffer;
204         buffer += d->bLength;
205         size -= d->bLength;
206
207         if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
208                 n = USB_DT_ENDPOINT_AUDIO_SIZE;
209         else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
210                 n = USB_DT_ENDPOINT_SIZE;
211         else {
212                 dev_warn(ddev, "config %d interface %d altsetting %d has an "
213                     "invalid endpoint descriptor of length %d, skipping\n",
214                     cfgno, inum, asnum, d->bLength);
215                 goto skip_to_next_endpoint_or_interface_descriptor;
216         }
217
218         i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
219         if (i >= 16 || i == 0) {
220                 dev_warn(ddev, "config %d interface %d altsetting %d has an "
221                     "invalid endpoint with address 0x%X, skipping\n",
222                     cfgno, inum, asnum, d->bEndpointAddress);
223                 goto skip_to_next_endpoint_or_interface_descriptor;
224         }
225
226         /* Only store as many endpoints as we have room for */
227         if (ifp->desc.bNumEndpoints >= num_ep)
228                 goto skip_to_next_endpoint_or_interface_descriptor;
229
230         endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
231         ++ifp->desc.bNumEndpoints;
232
233         memcpy(&endpoint->desc, d, n);
234         INIT_LIST_HEAD(&endpoint->urb_list);
235
236         /* Fix up bInterval values outside the legal range. Use 32 ms if no
237          * proper value can be guessed. */
238         i = 0;          /* i = min, j = max, n = default */
239         j = 255;
240         if (usb_endpoint_xfer_int(d)) {
241                 i = 1;
242                 switch (to_usb_device(ddev)->speed) {
243                 case USB_SPEED_SUPER:
244                 case USB_SPEED_HIGH:
245                         /* Many device manufacturers are using full-speed
246                          * bInterval values in high-speed interrupt endpoint
247                          * descriptors. Try to fix those and fall back to a
248                          * 32 ms default value otherwise. */
249                         n = fls(d->bInterval*8);
250                         if (n == 0)
251                                 n = 9;  /* 32 ms = 2^(9-1) uframes */
252                         j = 16;
253                         break;
254                 default:                /* USB_SPEED_FULL or _LOW */
255                         /* For low-speed, 10 ms is the official minimum.
256                          * But some "overclocked" devices might want faster
257                          * polling so we'll allow it. */
258                         n = 32;
259                         break;
260                 }
261         } else if (usb_endpoint_xfer_isoc(d)) {
262                 i = 1;
263                 j = 16;
264                 switch (to_usb_device(ddev)->speed) {
265                 case USB_SPEED_HIGH:
266                         n = 9;          /* 32 ms = 2^(9-1) uframes */
267                         break;
268                 default:                /* USB_SPEED_FULL */
269                         n = 6;          /* 32 ms = 2^(6-1) frames */
270                         break;
271                 }
272         }
273         if (d->bInterval < i || d->bInterval > j) {
274                 dev_warn(ddev, "config %d interface %d altsetting %d "
275                     "endpoint 0x%X has an invalid bInterval %d, "
276                     "changing to %d\n",
277                     cfgno, inum, asnum,
278                     d->bEndpointAddress, d->bInterval, n);
279                 endpoint->desc.bInterval = n;
280         }
281
282         /* Some buggy low-speed devices have Bulk endpoints, which is
283          * explicitly forbidden by the USB spec.  In an attempt to make
284          * them usable, we will try treating them as Interrupt endpoints.
285          */
286         if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
287                         usb_endpoint_xfer_bulk(d)) {
288                 dev_warn(ddev, "config %d interface %d altsetting %d "
289                     "endpoint 0x%X is Bulk; changing to Interrupt\n",
290                     cfgno, inum, asnum, d->bEndpointAddress);
291                 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
292                 endpoint->desc.bInterval = 1;
293                 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
294                         endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
295         }
296
297         /*
298          * Some buggy high speed devices have bulk endpoints using
299          * maxpacket sizes other than 512.  High speed HCDs may not
300          * be able to handle that particular bug, so let's warn...
301          */
302         if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
303                         && usb_endpoint_xfer_bulk(d)) {
304                 unsigned maxp;
305
306                 maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
307                 if (maxp != 512)
308                         dev_warn(ddev, "config %d interface %d altsetting %d "
309                                 "bulk endpoint 0x%X has invalid maxpacket %d\n",
310                                 cfgno, inum, asnum, d->bEndpointAddress,
311                                 maxp);
312         }
313         /* Allocate room for and parse any endpoint companion descriptors */
314         if (to_usb_device(ddev)->speed == USB_SPEED_SUPER) {
315                 endpoint->extra = buffer;
316                 i = find_next_descriptor_more(buffer, size, USB_DT_SS_ENDPOINT_COMP,
317                                 USB_DT_ENDPOINT, USB_DT_INTERFACE, &n);
318                 endpoint->extralen = i;
319                 buffer += i;
320                 size -= i;
321
322                 if (size > 0) {
323                         retval = usb_parse_endpoint_companion(ddev, cfgno, inum, asnum,
324                                         endpoint, num_ep, buffer, size);
325                         if (retval >= 0) {
326                                 buffer += retval;
327                                 retval = buffer - buffer0;
328                         }
329                 } else {
330                         retval = buffer - buffer0;
331                 }
332         } else {
333                 /* Skip over any Class Specific or Vendor Specific descriptors;
334                  * find the next endpoint or interface descriptor */
335                 endpoint->extra = buffer;
336                 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
337                                 USB_DT_INTERFACE, &n);
338                 endpoint->extralen = i;
339                 retval = buffer - buffer0 + i;
340         }
341         if (n > 0)
342                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
343                     n, plural(n), "endpoint");
344         return retval;
345
346 skip_to_next_endpoint_or_interface_descriptor:
347         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
348             USB_DT_INTERFACE, NULL);
349         return buffer - buffer0 + i;
350 }
351
352 void usb_release_interface_cache(struct kref *ref)
353 {
354         struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
355         int j;
356
357         for (j = 0; j < intfc->num_altsetting; j++) {
358                 struct usb_host_interface *alt = &intfc->altsetting[j];
359
360                 kfree(alt->endpoint);
361                 kfree(alt->string);
362         }
363         kfree(intfc);
364 }
365
366 static int usb_parse_interface(struct device *ddev, int cfgno,
367     struct usb_host_config *config, unsigned char *buffer, int size,
368     u8 inums[], u8 nalts[])
369 {
370         unsigned char *buffer0 = buffer;
371         struct usb_interface_descriptor *d;
372         int inum, asnum;
373         struct usb_interface_cache *intfc;
374         struct usb_host_interface *alt;
375         int i, n;
376         int len, retval;
377         int num_ep, num_ep_orig;
378
379         d = (struct usb_interface_descriptor *) buffer;
380         buffer += d->bLength;
381         size -= d->bLength;
382
383         if (d->bLength < USB_DT_INTERFACE_SIZE)
384                 goto skip_to_next_interface_descriptor;
385
386         /* Which interface entry is this? */
387         intfc = NULL;
388         inum = d->bInterfaceNumber;
389         for (i = 0; i < config->desc.bNumInterfaces; ++i) {
390                 if (inums[i] == inum) {
391                         intfc = config->intf_cache[i];
392                         break;
393                 }
394         }
395         if (!intfc || intfc->num_altsetting >= nalts[i])
396                 goto skip_to_next_interface_descriptor;
397
398         /* Check for duplicate altsetting entries */
399         asnum = d->bAlternateSetting;
400         for ((i = 0, alt = &intfc->altsetting[0]);
401               i < intfc->num_altsetting;
402              (++i, ++alt)) {
403                 if (alt->desc.bAlternateSetting == asnum) {
404                         dev_warn(ddev, "Duplicate descriptor for config %d "
405                             "interface %d altsetting %d, skipping\n",
406                             cfgno, inum, asnum);
407                         goto skip_to_next_interface_descriptor;
408                 }
409         }
410
411         ++intfc->num_altsetting;
412         memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
413
414         /* Skip over any Class Specific or Vendor Specific descriptors;
415          * find the first endpoint or interface descriptor */
416         alt->extra = buffer;
417         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
418             USB_DT_INTERFACE, &n);
419         alt->extralen = i;
420         if (n > 0)
421                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
422                     n, plural(n), "interface");
423         buffer += i;
424         size -= i;
425
426         /* Allocate space for the right(?) number of endpoints */
427         num_ep = num_ep_orig = alt->desc.bNumEndpoints;
428         alt->desc.bNumEndpoints = 0;            /* Use as a counter */
429         if (num_ep > USB_MAXENDPOINTS) {
430                 dev_warn(ddev, "too many endpoints for config %d interface %d "
431                     "altsetting %d: %d, using maximum allowed: %d\n",
432                     cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
433                 num_ep = USB_MAXENDPOINTS;
434         }
435
436         if (num_ep > 0) {
437                 /* Can't allocate 0 bytes */
438                 len = sizeof(struct usb_host_endpoint) * num_ep;
439                 alt->endpoint = kzalloc(len, GFP_KERNEL);
440                 if (!alt->endpoint)
441                         return -ENOMEM;
442         }
443
444         /* Parse all the endpoint descriptors */
445         n = 0;
446         while (size > 0) {
447                 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
448                      == USB_DT_INTERFACE)
449                         break;
450                 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
451                     num_ep, buffer, size);
452                 if (retval < 0)
453                         return retval;
454                 ++n;
455
456                 buffer += retval;
457                 size -= retval;
458         }
459
460         if (n != num_ep_orig)
461                 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
462                     "endpoint descriptor%s, different from the interface "
463                     "descriptor's value: %d\n",
464                     cfgno, inum, asnum, n, plural(n), num_ep_orig);
465         return buffer - buffer0;
466
467 skip_to_next_interface_descriptor:
468         i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
469             USB_DT_INTERFACE, NULL);
470         return buffer - buffer0 + i;
471 }
472
473 static int usb_parse_configuration(struct device *ddev, int cfgidx,
474     struct usb_host_config *config, unsigned char *buffer, int size)
475 {
476         unsigned char *buffer0 = buffer;
477         int cfgno;
478         int nintf, nintf_orig;
479         int i, j, n;
480         struct usb_interface_cache *intfc;
481         unsigned char *buffer2;
482         int size2;
483         struct usb_descriptor_header *header;
484         int len, retval;
485         u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
486         unsigned iad_num = 0;
487
488         memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
489         if (config->desc.bDescriptorType != USB_DT_CONFIG ||
490             config->desc.bLength < USB_DT_CONFIG_SIZE) {
491                 dev_err(ddev, "invalid descriptor for config index %d: "
492                     "type = 0x%X, length = %d\n", cfgidx,
493                     config->desc.bDescriptorType, config->desc.bLength);
494                 return -EINVAL;
495         }
496         cfgno = config->desc.bConfigurationValue;
497
498         buffer += config->desc.bLength;
499         size -= config->desc.bLength;
500
501         nintf = nintf_orig = config->desc.bNumInterfaces;
502         if (nintf > USB_MAXINTERFACES) {
503                 dev_warn(ddev, "config %d has too many interfaces: %d, "
504                     "using maximum allowed: %d\n",
505                     cfgno, nintf, USB_MAXINTERFACES);
506                 nintf = USB_MAXINTERFACES;
507         }
508
509         /* Go through the descriptors, checking their length and counting the
510          * number of altsettings for each interface */
511         n = 0;
512         for ((buffer2 = buffer, size2 = size);
513               size2 > 0;
514              (buffer2 += header->bLength, size2 -= header->bLength)) {
515
516                 if (size2 < sizeof(struct usb_descriptor_header)) {
517                         dev_warn(ddev, "config %d descriptor has %d excess "
518                             "byte%s, ignoring\n",
519                             cfgno, size2, plural(size2));
520                         break;
521                 }
522
523                 header = (struct usb_descriptor_header *) buffer2;
524                 if ((header->bLength > size2) || (header->bLength < 2)) {
525                         dev_warn(ddev, "config %d has an invalid descriptor "
526                             "of length %d, skipping remainder of the config\n",
527                             cfgno, header->bLength);
528                         break;
529                 }
530
531                 if (header->bDescriptorType == USB_DT_INTERFACE) {
532                         struct usb_interface_descriptor *d;
533                         int inum;
534
535                         d = (struct usb_interface_descriptor *) header;
536                         if (d->bLength < USB_DT_INTERFACE_SIZE) {
537                                 dev_warn(ddev, "config %d has an invalid "
538                                     "interface descriptor of length %d, "
539                                     "skipping\n", cfgno, d->bLength);
540                                 continue;
541                         }
542
543                         inum = d->bInterfaceNumber;
544                         if (inum >= nintf_orig)
545                                 dev_warn(ddev, "config %d has an invalid "
546                                     "interface number: %d but max is %d\n",
547                                     cfgno, inum, nintf_orig - 1);
548
549                         /* Have we already encountered this interface?
550                          * Count its altsettings */
551                         for (i = 0; i < n; ++i) {
552                                 if (inums[i] == inum)
553                                         break;
554                         }
555                         if (i < n) {
556                                 if (nalts[i] < 255)
557                                         ++nalts[i];
558                         } else if (n < USB_MAXINTERFACES) {
559                                 inums[n] = inum;
560                                 nalts[n] = 1;
561                                 ++n;
562                         }
563
564                 } else if (header->bDescriptorType ==
565                                 USB_DT_INTERFACE_ASSOCIATION) {
566                         if (iad_num == USB_MAXIADS) {
567                                 dev_warn(ddev, "found more Interface "
568                                                "Association Descriptors "
569                                                "than allocated for in "
570                                                "configuration %d\n", cfgno);
571                         } else {
572                                 config->intf_assoc[iad_num] =
573                                         (struct usb_interface_assoc_descriptor
574                                         *)header;
575                                 iad_num++;
576                         }
577
578                 } else if (header->bDescriptorType == USB_DT_DEVICE ||
579                             header->bDescriptorType == USB_DT_CONFIG)
580                         dev_warn(ddev, "config %d contains an unexpected "
581                             "descriptor of type 0x%X, skipping\n",
582                             cfgno, header->bDescriptorType);
583
584         }       /* for ((buffer2 = buffer, size2 = size); ...) */
585         size = buffer2 - buffer;
586         config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
587
588         if (n != nintf)
589                 dev_warn(ddev, "config %d has %d interface%s, different from "
590                     "the descriptor's value: %d\n",
591                     cfgno, n, plural(n), nintf_orig);
592         else if (n == 0)
593                 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
594         config->desc.bNumInterfaces = nintf = n;
595
596         /* Check for missing interface numbers */
597         for (i = 0; i < nintf; ++i) {
598                 for (j = 0; j < nintf; ++j) {
599                         if (inums[j] == i)
600                                 break;
601                 }
602                 if (j >= nintf)
603                         dev_warn(ddev, "config %d has no interface number "
604                             "%d\n", cfgno, i);
605         }
606
607         /* Allocate the usb_interface_caches and altsetting arrays */
608         for (i = 0; i < nintf; ++i) {
609                 j = nalts[i];
610                 if (j > USB_MAXALTSETTING) {
611                         dev_warn(ddev, "too many alternate settings for "
612                             "config %d interface %d: %d, "
613                             "using maximum allowed: %d\n",
614                             cfgno, inums[i], j, USB_MAXALTSETTING);
615                         nalts[i] = j = USB_MAXALTSETTING;
616                 }
617
618                 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
619                 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
620                 if (!intfc)
621                         return -ENOMEM;
622                 kref_init(&intfc->ref);
623         }
624
625         /* FIXME: parse the BOS descriptor */
626
627         /* Skip over any Class Specific or Vendor Specific descriptors;
628          * find the first interface descriptor */
629         config->extra = buffer;
630         i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
631             USB_DT_INTERFACE, &n);
632         config->extralen = i;
633         if (n > 0)
634                 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
635                     n, plural(n), "configuration");
636         buffer += i;
637         size -= i;
638
639         /* Parse all the interface/altsetting descriptors */
640         while (size > 0) {
641                 retval = usb_parse_interface(ddev, cfgno, config,
642                     buffer, size, inums, nalts);
643                 if (retval < 0)
644                         return retval;
645
646                 buffer += retval;
647                 size -= retval;
648         }
649
650         /* Check for missing altsettings */
651         for (i = 0; i < nintf; ++i) {
652                 intfc = config->intf_cache[i];
653                 for (j = 0; j < intfc->num_altsetting; ++j) {
654                         for (n = 0; n < intfc->num_altsetting; ++n) {
655                                 if (intfc->altsetting[n].desc.
656                                     bAlternateSetting == j)
657                                         break;
658                         }
659                         if (n >= intfc->num_altsetting)
660                                 dev_warn(ddev, "config %d interface %d has no "
661                                     "altsetting %d\n", cfgno, inums[i], j);
662                 }
663         }
664
665         return 0;
666 }
667
668 /* hub-only!! ... and only exported for reset/reinit path.
669  * otherwise used internally on disconnect/destroy path
670  */
671 void usb_destroy_configuration(struct usb_device *dev)
672 {
673         int c, i;
674
675         if (!dev->config)
676                 return;
677
678         if (dev->rawdescriptors) {
679                 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
680                         kfree(dev->rawdescriptors[i]);
681
682                 kfree(dev->rawdescriptors);
683                 dev->rawdescriptors = NULL;
684         }
685
686         for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
687                 struct usb_host_config *cf = &dev->config[c];
688
689                 kfree(cf->string);
690                 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
691                         if (cf->intf_cache[i])
692                                 kref_put(&cf->intf_cache[i]->ref,
693                                           usb_release_interface_cache);
694                 }
695         }
696         kfree(dev->config);
697         dev->config = NULL;
698 }
699
700
701 /*
702  * Get the USB config descriptors, cache and parse'em
703  *
704  * hub-only!! ... and only in reset path, or usb_new_device()
705  * (used by real hubs and virtual root hubs)
706  *
707  * NOTE: if this is a WUSB device and is not authorized, we skip the
708  *       whole thing. A non-authorized USB device has no
709  *       configurations.
710  */
711 int usb_get_configuration(struct usb_device *dev)
712 {
713         struct device *ddev = &dev->dev;
714         int ncfg = dev->descriptor.bNumConfigurations;
715         int result = 0;
716         unsigned int cfgno, length;
717         unsigned char *buffer;
718         unsigned char *bigbuffer;
719         struct usb_config_descriptor *desc;
720
721         cfgno = 0;
722         if (dev->authorized == 0)       /* Not really an error */
723                 goto out_not_authorized;
724         result = -ENOMEM;
725         if (ncfg > USB_MAXCONFIG) {
726                 dev_warn(ddev, "too many configurations: %d, "
727                     "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
728                 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
729         }
730
731         if (ncfg < 1) {
732                 dev_err(ddev, "no configurations\n");
733                 return -EINVAL;
734         }
735
736         length = ncfg * sizeof(struct usb_host_config);
737         dev->config = kzalloc(length, GFP_KERNEL);
738         if (!dev->config)
739                 goto err2;
740
741         length = ncfg * sizeof(char *);
742         dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
743         if (!dev->rawdescriptors)
744                 goto err2;
745
746         buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
747         if (!buffer)
748                 goto err2;
749         desc = (struct usb_config_descriptor *)buffer;
750
751         result = 0;
752         for (; cfgno < ncfg; cfgno++) {
753                 /* We grab just the first descriptor so we know how long
754                  * the whole configuration is */
755                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
756                     buffer, USB_DT_CONFIG_SIZE);
757                 if (result < 0) {
758                         dev_err(ddev, "unable to read config index %d "
759                             "descriptor/%s: %d\n", cfgno, "start", result);
760                         dev_err(ddev, "chopping to %d config(s)\n", cfgno);
761                         dev->descriptor.bNumConfigurations = cfgno;
762                         break;
763                 } else if (result < 4) {
764                         dev_err(ddev, "config index %d descriptor too short "
765                             "(expected %i, got %i)\n", cfgno,
766                             USB_DT_CONFIG_SIZE, result);
767                         result = -EINVAL;
768                         goto err;
769                 }
770                 length = max((int) le16_to_cpu(desc->wTotalLength),
771                     USB_DT_CONFIG_SIZE);
772
773                 /* Now that we know the length, get the whole thing */
774                 bigbuffer = kmalloc(length, GFP_KERNEL);
775                 if (!bigbuffer) {
776                         result = -ENOMEM;
777                         goto err;
778                 }
779                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
780                     bigbuffer, length);
781                 if (result < 0) {
782                         dev_err(ddev, "unable to read config index %d "
783                             "descriptor/%s\n", cfgno, "all");
784                         kfree(bigbuffer);
785                         goto err;
786                 }
787                 if (result < length) {
788                         dev_warn(ddev, "config index %d descriptor too short "
789                             "(expected %i, got %i)\n", cfgno, length, result);
790                         length = result;
791                 }
792
793                 dev->rawdescriptors[cfgno] = bigbuffer;
794
795                 result = usb_parse_configuration(&dev->dev, cfgno,
796                     &dev->config[cfgno], bigbuffer, length);
797                 if (result < 0) {
798                         ++cfgno;
799                         goto err;
800                 }
801         }
802         result = 0;
803
804 err:
805         kfree(buffer);
806 out_not_authorized:
807         dev->descriptor.bNumConfigurations = cfgno;
808 err2:
809         if (result == -ENOMEM)
810                 dev_err(ddev, "out of memory\n");
811         return result;
812 }