allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / core / config.c
blobcd177e22784e3ea82a80a86eb47885f844a07613
1 #include <linux/usb.h>
2 #include <linux/usb/ch9.h>
3 #include <linux/usb/quirks.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 #include "hcd.h"
12 #define USB_MAXALTSETTING 128 /* Hard limit */
13 #define USB_MAXENDPOINTS 30 /* Hard limit */
15 #define USB_MAXCONFIG 8 /* Arbitrary limit */
18 static inline const char *plural(int n)
20 return (n == 1 ? "" : "s");
23 static int find_next_descriptor(unsigned char *buffer, int size,
24 int dt1, int dt2, int *num_skipped)
26 struct usb_descriptor_header *h;
27 int n = 0;
28 unsigned char *buffer0 = buffer;
30 /* Find the next descriptor of type dt1 or dt2 */
31 while (size > 0) {
32 h = (struct usb_descriptor_header *) buffer;
33 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
34 break;
35 buffer += h->bLength;
36 size -= h->bLength;
37 ++n;
40 /* Store the number of descriptors skipped and return the
41 * number of bytes skipped */
42 if (num_skipped)
43 *num_skipped = n;
44 return buffer - buffer0;
47 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
48 int asnum, struct usb_host_interface *ifp, int num_ep,
49 unsigned char *buffer, int size)
51 unsigned char *buffer0 = buffer;
52 struct usb_endpoint_descriptor *d;
53 struct usb_host_endpoint *endpoint;
54 int n, i, j;
56 d = (struct usb_endpoint_descriptor *) buffer;
57 buffer += d->bLength;
58 size -= d->bLength;
60 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
61 n = USB_DT_ENDPOINT_AUDIO_SIZE;
62 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
63 n = USB_DT_ENDPOINT_SIZE;
64 else {
65 dev_warn(ddev, "config %d interface %d altsetting %d has an "
66 "invalid endpoint descriptor of length %d, skipping\n",
67 cfgno, inum, asnum, d->bLength);
68 goto skip_to_next_endpoint_or_interface_descriptor;
71 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
72 if (i >= 16 || i == 0) {
73 dev_warn(ddev, "config %d interface %d altsetting %d has an "
74 "invalid endpoint with address 0x%X, skipping\n",
75 cfgno, inum, asnum, d->bEndpointAddress);
76 goto skip_to_next_endpoint_or_interface_descriptor;
79 /* Only store as many endpoints as we have room for */
80 if (ifp->desc.bNumEndpoints >= num_ep)
81 goto skip_to_next_endpoint_or_interface_descriptor;
83 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
84 ++ifp->desc.bNumEndpoints;
86 memcpy(&endpoint->desc, d, n);
87 INIT_LIST_HEAD(&endpoint->urb_list);
89 /* Fix up bInterval values outside the legal range. Use 32 ms if no
90 * proper value can be guessed. */
91 i = 0; /* i = min, j = max, n = default */
92 j = 255;
93 if (usb_endpoint_xfer_int(d)) {
94 i = 1;
95 switch (to_usb_device(ddev)->speed) {
96 case USB_SPEED_HIGH:
97 /* Many device manufacturers are using full-speed
98 * bInterval values in high-speed interrupt endpoint
99 * descriptors. Try to fix those and fall back to a
100 * 32 ms default value otherwise. */
101 n = fls(d->bInterval*8);
102 if (n == 0)
103 n = 9; /* 32 ms = 2^(9-1) uframes */
104 j = 16;
105 break;
106 default: /* USB_SPEED_FULL or _LOW */
107 /* For low-speed, 10 ms is the official minimum.
108 * But some "overclocked" devices might want faster
109 * polling so we'll allow it. */
110 n = 32;
111 break;
113 } else if (usb_endpoint_xfer_isoc(d)) {
114 i = 1;
115 j = 16;
116 switch (to_usb_device(ddev)->speed) {
117 case USB_SPEED_HIGH:
118 n = 9; /* 32 ms = 2^(9-1) uframes */
119 break;
120 default: /* USB_SPEED_FULL */
121 n = 6; /* 32 ms = 2^(6-1) frames */
122 break;
125 if (d->bInterval < i || d->bInterval > j) {
126 dev_warn(ddev, "config %d interface %d altsetting %d "
127 "endpoint 0x%X has an invalid bInterval %d, "
128 "changing to %d\n",
129 cfgno, inum, asnum,
130 d->bEndpointAddress, d->bInterval, n);
131 endpoint->desc.bInterval = n;
134 /* Some buggy low-speed devices have Bulk endpoints, which is
135 * explicitly forbidden by the USB spec. In an attempt to make
136 * them usable, we will try treating them as Interrupt endpoints.
138 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
139 usb_endpoint_xfer_bulk(d)) {
140 dev_warn(ddev, "config %d interface %d altsetting %d "
141 "endpoint 0x%X is Bulk; changing to Interrupt\n",
142 cfgno, inum, asnum, d->bEndpointAddress);
143 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
144 endpoint->desc.bInterval = 1;
145 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
146 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
150 * Some buggy high speed devices have bulk endpoints using
151 * maxpacket sizes other than 512. High speed HCDs may not
152 * be able to handle that particular bug, so let's warn...
154 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
155 && usb_endpoint_xfer_bulk(d)) {
156 unsigned maxp;
158 maxp = le16_to_cpu(endpoint->desc.wMaxPacketSize) & 0x07ff;
159 if (maxp != 512)
160 dev_warn(ddev, "config %d interface %d altsetting %d "
161 "bulk endpoint 0x%X has invalid maxpacket %d\n",
162 cfgno, inum, asnum, d->bEndpointAddress,
163 maxp);
166 /* Skip over any Class Specific or Vendor Specific descriptors;
167 * find the next endpoint or interface descriptor */
168 endpoint->extra = buffer;
169 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
170 USB_DT_INTERFACE, &n);
171 endpoint->extralen = i;
172 if (n > 0)
173 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
174 n, plural(n), "endpoint");
175 return buffer - buffer0 + i;
177 skip_to_next_endpoint_or_interface_descriptor:
178 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
179 USB_DT_INTERFACE, NULL);
180 return buffer - buffer0 + i;
183 void usb_release_interface_cache(struct kref *ref)
185 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
186 int j;
188 for (j = 0; j < intfc->num_altsetting; j++) {
189 struct usb_host_interface *alt = &intfc->altsetting[j];
191 kfree(alt->endpoint);
192 kfree(alt->string);
194 kfree(intfc);
197 static int usb_parse_interface(struct device *ddev, int cfgno,
198 struct usb_host_config *config, unsigned char *buffer, int size,
199 u8 inums[], u8 nalts[])
201 unsigned char *buffer0 = buffer;
202 struct usb_interface_descriptor *d;
203 int inum, asnum;
204 struct usb_interface_cache *intfc;
205 struct usb_host_interface *alt;
206 int i, n;
207 int len, retval;
208 int num_ep, num_ep_orig;
210 d = (struct usb_interface_descriptor *) buffer;
211 buffer += d->bLength;
212 size -= d->bLength;
214 if (d->bLength < USB_DT_INTERFACE_SIZE)
215 goto skip_to_next_interface_descriptor;
217 /* Which interface entry is this? */
218 intfc = NULL;
219 inum = d->bInterfaceNumber;
220 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
221 if (inums[i] == inum) {
222 intfc = config->intf_cache[i];
223 break;
226 if (!intfc || intfc->num_altsetting >= nalts[i])
227 goto skip_to_next_interface_descriptor;
229 /* Check for duplicate altsetting entries */
230 asnum = d->bAlternateSetting;
231 for ((i = 0, alt = &intfc->altsetting[0]);
232 i < intfc->num_altsetting;
233 (++i, ++alt)) {
234 if (alt->desc.bAlternateSetting == asnum) {
235 dev_warn(ddev, "Duplicate descriptor for config %d "
236 "interface %d altsetting %d, skipping\n",
237 cfgno, inum, asnum);
238 goto skip_to_next_interface_descriptor;
242 ++intfc->num_altsetting;
243 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
245 /* Skip over any Class Specific or Vendor Specific descriptors;
246 * find the first endpoint or interface descriptor */
247 alt->extra = buffer;
248 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
249 USB_DT_INTERFACE, &n);
250 alt->extralen = i;
251 if (n > 0)
252 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
253 n, plural(n), "interface");
254 buffer += i;
255 size -= i;
257 /* Allocate space for the right(?) number of endpoints */
258 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
259 alt->desc.bNumEndpoints = 0; /* Use as a counter */
260 if (num_ep > USB_MAXENDPOINTS) {
261 dev_warn(ddev, "too many endpoints for config %d interface %d "
262 "altsetting %d: %d, using maximum allowed: %d\n",
263 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
264 num_ep = USB_MAXENDPOINTS;
267 if (num_ep > 0) {
268 /* Can't allocate 0 bytes */
269 len = sizeof(struct usb_host_endpoint) * num_ep;
270 alt->endpoint = kzalloc(len, GFP_KERNEL);
271 if (!alt->endpoint)
272 return -ENOMEM;
275 /* Parse all the endpoint descriptors */
276 n = 0;
277 while (size > 0) {
278 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
279 == USB_DT_INTERFACE)
280 break;
281 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
282 num_ep, buffer, size);
283 if (retval < 0)
284 return retval;
285 ++n;
287 buffer += retval;
288 size -= retval;
291 if (n != num_ep_orig)
292 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
293 "endpoint descriptor%s, different from the interface "
294 "descriptor's value: %d\n",
295 cfgno, inum, asnum, n, plural(n), num_ep_orig);
296 return buffer - buffer0;
298 skip_to_next_interface_descriptor:
299 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
300 USB_DT_INTERFACE, NULL);
301 return buffer - buffer0 + i;
304 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
305 struct usb_host_config *config, unsigned char *buffer, int size)
307 struct device *ddev = &dev->dev;
308 unsigned char *buffer0 = buffer;
309 int cfgno;
310 int nintf, nintf_orig;
311 int i, j, n;
312 struct usb_interface_cache *intfc;
313 unsigned char *buffer2;
314 int size2;
315 struct usb_descriptor_header *header;
316 int len, retval;
317 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
318 unsigned iad_num = 0;
320 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
321 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
322 config->desc.bLength < USB_DT_CONFIG_SIZE) {
323 dev_err(ddev, "invalid descriptor for config index %d: "
324 "type = 0x%X, length = %d\n", cfgidx,
325 config->desc.bDescriptorType, config->desc.bLength);
326 return -EINVAL;
328 cfgno = config->desc.bConfigurationValue;
330 buffer += config->desc.bLength;
331 size -= config->desc.bLength;
333 nintf = nintf_orig = config->desc.bNumInterfaces;
334 if (nintf > USB_MAXINTERFACES) {
335 dev_warn(ddev, "config %d has too many interfaces: %d, "
336 "using maximum allowed: %d\n",
337 cfgno, nintf, USB_MAXINTERFACES);
338 nintf = USB_MAXINTERFACES;
341 /* Go through the descriptors, checking their length and counting the
342 * number of altsettings for each interface */
343 n = 0;
344 for ((buffer2 = buffer, size2 = size);
345 size2 > 0;
346 (buffer2 += header->bLength, size2 -= header->bLength)) {
348 if (size2 < sizeof(struct usb_descriptor_header)) {
349 dev_warn(ddev, "config %d descriptor has %d excess "
350 "byte%s, ignoring\n",
351 cfgno, size2, plural(size2));
352 break;
355 header = (struct usb_descriptor_header *) buffer2;
356 if ((header->bLength > size2) || (header->bLength < 2)) {
357 dev_warn(ddev, "config %d has an invalid descriptor "
358 "of length %d, skipping remainder of the config\n",
359 cfgno, header->bLength);
360 break;
363 if (header->bDescriptorType == USB_DT_INTERFACE) {
364 struct usb_interface_descriptor *d;
365 int inum;
367 d = (struct usb_interface_descriptor *) header;
368 if (d->bLength < USB_DT_INTERFACE_SIZE) {
369 dev_warn(ddev, "config %d has an invalid "
370 "interface descriptor of length %d, "
371 "skipping\n", cfgno, d->bLength);
372 continue;
375 inum = d->bInterfaceNumber;
377 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
378 n >= nintf_orig) {
379 dev_warn(ddev, "config %d has more interface "
380 "descriptors than it declares in "
381 "bNumInterfaces, ignoring interface "
382 "number: %d\n", cfgno, inum);
383 continue;
386 if (inum >= nintf_orig)
387 dev_warn(ddev, "config %d has an invalid "
388 "interface number: %d but max is %d\n",
389 cfgno, inum, nintf_orig - 1);
391 /* Have we already encountered this interface?
392 * Count its altsettings */
393 for (i = 0; i < n; ++i) {
394 if (inums[i] == inum)
395 break;
397 if (i < n) {
398 if (nalts[i] < 255)
399 ++nalts[i];
400 } else if (n < USB_MAXINTERFACES) {
401 inums[n] = inum;
402 nalts[n] = 1;
403 ++n;
406 } else if (header->bDescriptorType ==
407 USB_DT_INTERFACE_ASSOCIATION) {
408 if (iad_num == USB_MAXIADS) {
409 dev_warn(ddev, "found more Interface "
410 "Association Descriptors "
411 "than allocated for in "
412 "configuration %d\n", cfgno);
413 } else {
414 config->intf_assoc[iad_num] =
415 (struct usb_interface_assoc_descriptor
416 *)header;
417 iad_num++;
420 } else if (header->bDescriptorType == USB_DT_DEVICE ||
421 header->bDescriptorType == USB_DT_CONFIG)
422 dev_warn(ddev, "config %d contains an unexpected "
423 "descriptor of type 0x%X, skipping\n",
424 cfgno, header->bDescriptorType);
426 } /* for ((buffer2 = buffer, size2 = size); ...) */
427 size = buffer2 - buffer;
428 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
430 if (n != nintf)
431 dev_warn(ddev, "config %d has %d interface%s, different from "
432 "the descriptor's value: %d\n",
433 cfgno, n, plural(n), nintf_orig);
434 else if (n == 0)
435 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
436 config->desc.bNumInterfaces = nintf = n;
438 /* Check for missing interface numbers */
439 for (i = 0; i < nintf; ++i) {
440 for (j = 0; j < nintf; ++j) {
441 if (inums[j] == i)
442 break;
444 if (j >= nintf)
445 dev_warn(ddev, "config %d has no interface number "
446 "%d\n", cfgno, i);
449 /* Allocate the usb_interface_caches and altsetting arrays */
450 for (i = 0; i < nintf; ++i) {
451 j = nalts[i];
452 if (j > USB_MAXALTSETTING) {
453 dev_warn(ddev, "too many alternate settings for "
454 "config %d interface %d: %d, "
455 "using maximum allowed: %d\n",
456 cfgno, inums[i], j, USB_MAXALTSETTING);
457 nalts[i] = j = USB_MAXALTSETTING;
460 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
461 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
462 if (!intfc)
463 return -ENOMEM;
464 kref_init(&intfc->ref);
467 /* Skip over any Class Specific or Vendor Specific descriptors;
468 * find the first interface descriptor */
469 config->extra = buffer;
470 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
471 USB_DT_INTERFACE, &n);
472 config->extralen = i;
473 if (n > 0)
474 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
475 n, plural(n), "configuration");
476 buffer += i;
477 size -= i;
479 /* Parse all the interface/altsetting descriptors */
480 while (size > 0) {
481 retval = usb_parse_interface(ddev, cfgno, config,
482 buffer, size, inums, nalts);
483 if (retval < 0)
484 return retval;
486 buffer += retval;
487 size -= retval;
490 /* Check for missing altsettings */
491 for (i = 0; i < nintf; ++i) {
492 intfc = config->intf_cache[i];
493 for (j = 0; j < intfc->num_altsetting; ++j) {
494 for (n = 0; n < intfc->num_altsetting; ++n) {
495 if (intfc->altsetting[n].desc.
496 bAlternateSetting == j)
497 break;
499 if (n >= intfc->num_altsetting)
500 dev_warn(ddev, "config %d interface %d has no "
501 "altsetting %d\n", cfgno, inums[i], j);
505 return 0;
508 /* hub-only!! ... and only exported for reset/reinit path.
509 * otherwise used internally on disconnect/destroy path
511 void usb_destroy_configuration(struct usb_device *dev)
513 int c, i;
515 if (!dev->config)
516 return;
518 if (dev->rawdescriptors) {
519 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
520 kfree(dev->rawdescriptors[i]);
522 kfree(dev->rawdescriptors);
523 dev->rawdescriptors = NULL;
526 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
527 struct usb_host_config *cf = &dev->config[c];
529 kfree(cf->string);
530 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
531 if (cf->intf_cache[i])
532 kref_put(&cf->intf_cache[i]->ref,
533 usb_release_interface_cache);
536 kfree(dev->config);
537 dev->config = NULL;
541 // hub-only!! ... and only in reset path, or usb_new_device()
542 // (used by real hubs and virtual root hubs)
543 int usb_get_configuration(struct usb_device *dev)
545 struct device *ddev = &dev->dev;
546 int ncfg = dev->descriptor.bNumConfigurations;
547 int result = -ENOMEM;
548 unsigned int cfgno, length;
549 unsigned char *bigbuffer;
550 struct usb_config_descriptor *desc;
552 if (ncfg > USB_MAXCONFIG) {
553 dev_warn(ddev, "too many configurations: %d, "
554 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
555 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
558 if (ncfg < 1) {
559 dev_err(ddev, "no configurations\n");
560 return -EINVAL;
563 length = ncfg * sizeof(struct usb_host_config);
564 dev->config = kzalloc(length, GFP_KERNEL);
565 if (!dev->config)
566 goto err2;
568 length = ncfg * sizeof(char *);
569 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
570 if (!dev->rawdescriptors)
571 goto err2;
573 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
574 if (!desc)
575 goto err2;
577 for (cfgno = 0; cfgno < ncfg; cfgno++) {
578 /* We grab just the first descriptor so we know how long
579 * the whole configuration is */
580 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
581 desc, USB_DT_CONFIG_SIZE);
582 if (result < 0) {
583 dev_err(ddev, "unable to read config index %d "
584 "descriptor/%s\n", cfgno, "start");
585 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
586 dev->descriptor.bNumConfigurations = cfgno;
587 break;
588 } else if (result < 4) {
589 dev_err(ddev, "config index %d descriptor too short "
590 "(expected %i, got %i)\n", cfgno,
591 USB_DT_CONFIG_SIZE, result);
592 result = -EINVAL;
593 goto err;
595 length = max((int) le16_to_cpu(desc->wTotalLength),
596 USB_DT_CONFIG_SIZE);
598 /* Now that we know the length, get the whole thing */
599 bigbuffer = kmalloc(length, GFP_KERNEL);
600 if (!bigbuffer) {
601 result = -ENOMEM;
602 goto err;
604 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
605 bigbuffer, length);
606 if (result < 0) {
607 dev_err(ddev, "unable to read config index %d "
608 "descriptor/%s\n", cfgno, "all");
609 kfree(bigbuffer);
610 goto err;
612 if (result < length) {
613 dev_warn(ddev, "config index %d descriptor too short "
614 "(expected %i, got %i)\n", cfgno, length, result);
615 length = result;
618 dev->rawdescriptors[cfgno] = bigbuffer;
620 result = usb_parse_configuration(dev, cfgno,
621 &dev->config[cfgno], bigbuffer, length);
622 if (result < 0) {
623 ++cfgno;
624 goto err;
627 result = 0;
629 err:
630 kfree(desc);
631 dev->descriptor.bNumConfigurations = cfgno;
632 err2:
633 if (result == -ENOMEM)
634 dev_err(ddev, "out of memory\n");
635 return result;