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