blob: 9152e12dcf7150a64acdbf03036cccb0affeaf10 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/usb.h>
Alan Stern615ae112007-06-08 15:23:27 -04002#include <linux/usb/ch9.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/module.h>
4#include <linux/init.h>
5#include <linux/slab.h>
6#include <linux/device.h>
7#include <asm/byteorder.h>
Greg KH6d5e8252005-04-18 17:39:24 -07008#include "usb.h"
9#include "hcd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#define USB_MAXALTSETTING 128 /* Hard limit */
12#define USB_MAXENDPOINTS 30 /* Hard limit */
13
14#define USB_MAXCONFIG 8 /* Arbitrary limit */
15
16
17static inline const char *plural(int n)
18{
19 return (n == 1 ? "" : "s");
20}
21
22static int find_next_descriptor(unsigned char *buffer, int size,
23 int dt1, int dt2, int *num_skipped)
24{
25 struct usb_descriptor_header *h;
26 int n = 0;
27 unsigned char *buffer0 = buffer;
28
29 /* Find the next descriptor of type dt1 or dt2 */
30 while (size > 0) {
31 h = (struct usb_descriptor_header *) buffer;
32 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
33 break;
34 buffer += h->bLength;
35 size -= h->bLength;
36 ++n;
37 }
38
39 /* Store the number of descriptors skipped and return the
40 * number of bytes skipped */
41 if (num_skipped)
42 *num_skipped = n;
43 return buffer - buffer0;
44}
45
46static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
47 int asnum, struct usb_host_interface *ifp, int num_ep,
48 unsigned char *buffer, int size)
49{
50 unsigned char *buffer0 = buffer;
51 struct usb_endpoint_descriptor *d;
52 struct usb_host_endpoint *endpoint;
Alan Stern615ae112007-06-08 15:23:27 -040053 int n, i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 d = (struct usb_endpoint_descriptor *) buffer;
56 buffer += d->bLength;
57 size -= d->bLength;
58
59 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
60 n = USB_DT_ENDPOINT_AUDIO_SIZE;
61 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
62 n = USB_DT_ENDPOINT_SIZE;
63 else {
64 dev_warn(ddev, "config %d interface %d altsetting %d has an "
65 "invalid endpoint descriptor of length %d, skipping\n",
66 cfgno, inum, asnum, d->bLength);
67 goto skip_to_next_endpoint_or_interface_descriptor;
68 }
69
70 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
71 if (i >= 16 || i == 0) {
72 dev_warn(ddev, "config %d interface %d altsetting %d has an "
73 "invalid endpoint with address 0x%X, skipping\n",
74 cfgno, inum, asnum, d->bEndpointAddress);
75 goto skip_to_next_endpoint_or_interface_descriptor;
76 }
77
78 /* Only store as many endpoints as we have room for */
79 if (ifp->desc.bNumEndpoints >= num_ep)
80 goto skip_to_next_endpoint_or_interface_descriptor;
81
82 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
83 ++ifp->desc.bNumEndpoints;
84
85 memcpy(&endpoint->desc, d, n);
86 INIT_LIST_HEAD(&endpoint->urb_list);
87
Alan Stern615ae112007-06-08 15:23:27 -040088 /* If the bInterval value is outside the legal range,
89 * set it to a default value: 32 ms */
90 i = 0; /* i = min, j = max, n = default */
91 j = 255;
92 if (usb_endpoint_xfer_int(d)) {
93 i = 1;
94 switch (to_usb_device(ddev)->speed) {
95 case USB_SPEED_HIGH:
96 n = 9; /* 32 ms = 2^(9-1) uframes */
97 j = 16;
98 break;
99 default: /* USB_SPEED_FULL or _LOW */
100 /* For low-speed, 10 ms is the official minimum.
101 * But some "overclocked" devices might want faster
102 * polling so we'll allow it. */
103 n = 32;
104 break;
105 }
106 } else if (usb_endpoint_xfer_isoc(d)) {
107 i = 1;
108 j = 16;
109 switch (to_usb_device(ddev)->speed) {
110 case USB_SPEED_HIGH:
111 n = 9; /* 32 ms = 2^(9-1) uframes */
112 break;
113 default: /* USB_SPEED_FULL */
114 n = 6; /* 32 ms = 2^(6-1) frames */
115 break;
116 }
117 }
118 if (d->bInterval < i || d->bInterval > j) {
119 dev_warn(ddev, "config %d interface %d altsetting %d "
120 "endpoint 0x%X has an invalid bInterval %d, "
121 "changing to %d\n",
122 cfgno, inum, asnum,
123 d->bEndpointAddress, d->bInterval, n);
124 endpoint->desc.bInterval = n;
125 }
126
Alan Stern60aac1e2007-06-08 15:25:02 -0400127 /* Some buggy low-speed devices have Bulk endpoints, which is
128 * explicitly forbidden by the USB spec. In an attempt to make
129 * them usable, we will try treating them as Interrupt endpoints.
130 */
131 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
132 usb_endpoint_xfer_bulk(d)) {
133 dev_warn(ddev, "config %d interface %d altsetting %d "
134 "endpoint 0x%X is Bulk; changing to Interrupt\n",
135 cfgno, inum, asnum, d->bEndpointAddress);
136 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
137 endpoint->desc.bInterval = 1;
138 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
139 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
140 }
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Skip over any Class Specific or Vendor Specific descriptors;
143 * find the next endpoint or interface descriptor */
144 endpoint->extra = buffer;
145 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
146 USB_DT_INTERFACE, &n);
147 endpoint->extralen = i;
148 if (n > 0)
149 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
150 n, plural(n), "endpoint");
151 return buffer - buffer0 + i;
152
153skip_to_next_endpoint_or_interface_descriptor:
154 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
155 USB_DT_INTERFACE, NULL);
156 return buffer - buffer0 + i;
157}
158
159void usb_release_interface_cache(struct kref *ref)
160{
161 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
162 int j;
163
Alan Stern4f62efe2005-10-24 16:24:14 -0400164 for (j = 0; j < intfc->num_altsetting; j++) {
165 struct usb_host_interface *alt = &intfc->altsetting[j];
166
167 kfree(alt->endpoint);
168 kfree(alt->string);
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 kfree(intfc);
171}
172
173static int usb_parse_interface(struct device *ddev, int cfgno,
174 struct usb_host_config *config, unsigned char *buffer, int size,
175 u8 inums[], u8 nalts[])
176{
177 unsigned char *buffer0 = buffer;
178 struct usb_interface_descriptor *d;
179 int inum, asnum;
180 struct usb_interface_cache *intfc;
181 struct usb_host_interface *alt;
182 int i, n;
183 int len, retval;
184 int num_ep, num_ep_orig;
185
186 d = (struct usb_interface_descriptor *) buffer;
187 buffer += d->bLength;
188 size -= d->bLength;
189
190 if (d->bLength < USB_DT_INTERFACE_SIZE)
191 goto skip_to_next_interface_descriptor;
192
193 /* Which interface entry is this? */
194 intfc = NULL;
195 inum = d->bInterfaceNumber;
196 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
197 if (inums[i] == inum) {
198 intfc = config->intf_cache[i];
199 break;
200 }
201 }
202 if (!intfc || intfc->num_altsetting >= nalts[i])
203 goto skip_to_next_interface_descriptor;
204
205 /* Check for duplicate altsetting entries */
206 asnum = d->bAlternateSetting;
207 for ((i = 0, alt = &intfc->altsetting[0]);
208 i < intfc->num_altsetting;
209 (++i, ++alt)) {
210 if (alt->desc.bAlternateSetting == asnum) {
211 dev_warn(ddev, "Duplicate descriptor for config %d "
212 "interface %d altsetting %d, skipping\n",
213 cfgno, inum, asnum);
214 goto skip_to_next_interface_descriptor;
215 }
216 }
217
218 ++intfc->num_altsetting;
219 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
220
221 /* Skip over any Class Specific or Vendor Specific descriptors;
222 * find the first endpoint or interface descriptor */
223 alt->extra = buffer;
224 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
225 USB_DT_INTERFACE, &n);
226 alt->extralen = i;
227 if (n > 0)
228 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
229 n, plural(n), "interface");
230 buffer += i;
231 size -= i;
232
233 /* Allocate space for the right(?) number of endpoints */
234 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
235 alt->desc.bNumEndpoints = 0; // Use as a counter
236 if (num_ep > USB_MAXENDPOINTS) {
237 dev_warn(ddev, "too many endpoints for config %d interface %d "
238 "altsetting %d: %d, using maximum allowed: %d\n",
239 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
240 num_ep = USB_MAXENDPOINTS;
241 }
242
Alan Stern57a21c12007-05-15 17:40:37 -0400243 if (num_ep > 0) { /* Can't allocate 0 bytes */
244 len = sizeof(struct usb_host_endpoint) * num_ep;
245 alt->endpoint = kzalloc(len, GFP_KERNEL);
246 if (!alt->endpoint)
247 return -ENOMEM;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* Parse all the endpoint descriptors */
251 n = 0;
252 while (size > 0) {
253 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
254 == USB_DT_INTERFACE)
255 break;
256 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
257 num_ep, buffer, size);
258 if (retval < 0)
259 return retval;
260 ++n;
261
262 buffer += retval;
263 size -= retval;
264 }
265
266 if (n != num_ep_orig)
267 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
268 "endpoint descriptor%s, different from the interface "
269 "descriptor's value: %d\n",
270 cfgno, inum, asnum, n, plural(n), num_ep_orig);
271 return buffer - buffer0;
272
273skip_to_next_interface_descriptor:
274 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
275 USB_DT_INTERFACE, NULL);
276 return buffer - buffer0 + i;
277}
278
279static int usb_parse_configuration(struct device *ddev, int cfgidx,
280 struct usb_host_config *config, unsigned char *buffer, int size)
281{
282 unsigned char *buffer0 = buffer;
283 int cfgno;
284 int nintf, nintf_orig;
285 int i, j, n;
286 struct usb_interface_cache *intfc;
287 unsigned char *buffer2;
288 int size2;
289 struct usb_descriptor_header *header;
290 int len, retval;
291 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
292
293 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
294 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
295 config->desc.bLength < USB_DT_CONFIG_SIZE) {
296 dev_err(ddev, "invalid descriptor for config index %d: "
297 "type = 0x%X, length = %d\n", cfgidx,
298 config->desc.bDescriptorType, config->desc.bLength);
299 return -EINVAL;
300 }
301 cfgno = config->desc.bConfigurationValue;
302
303 buffer += config->desc.bLength;
304 size -= config->desc.bLength;
305
306 nintf = nintf_orig = config->desc.bNumInterfaces;
307 if (nintf > USB_MAXINTERFACES) {
308 dev_warn(ddev, "config %d has too many interfaces: %d, "
309 "using maximum allowed: %d\n",
310 cfgno, nintf, USB_MAXINTERFACES);
311 nintf = USB_MAXINTERFACES;
312 }
313
314 /* Go through the descriptors, checking their length and counting the
315 * number of altsettings for each interface */
316 n = 0;
317 for ((buffer2 = buffer, size2 = size);
318 size2 > 0;
319 (buffer2 += header->bLength, size2 -= header->bLength)) {
320
321 if (size2 < sizeof(struct usb_descriptor_header)) {
322 dev_warn(ddev, "config %d descriptor has %d excess "
323 "byte%s, ignoring\n",
324 cfgno, size2, plural(size2));
325 break;
326 }
327
328 header = (struct usb_descriptor_header *) buffer2;
329 if ((header->bLength > size2) || (header->bLength < 2)) {
330 dev_warn(ddev, "config %d has an invalid descriptor "
331 "of length %d, skipping remainder of the config\n",
332 cfgno, header->bLength);
333 break;
334 }
335
336 if (header->bDescriptorType == USB_DT_INTERFACE) {
337 struct usb_interface_descriptor *d;
338 int inum;
339
340 d = (struct usb_interface_descriptor *) header;
341 if (d->bLength < USB_DT_INTERFACE_SIZE) {
342 dev_warn(ddev, "config %d has an invalid "
343 "interface descriptor of length %d, "
344 "skipping\n", cfgno, d->bLength);
345 continue;
346 }
347
348 inum = d->bInterfaceNumber;
349 if (inum >= nintf_orig)
350 dev_warn(ddev, "config %d has an invalid "
351 "interface number: %d but max is %d\n",
352 cfgno, inum, nintf_orig - 1);
353
354 /* Have we already encountered this interface?
355 * Count its altsettings */
356 for (i = 0; i < n; ++i) {
357 if (inums[i] == inum)
358 break;
359 }
360 if (i < n) {
361 if (nalts[i] < 255)
362 ++nalts[i];
363 } else if (n < USB_MAXINTERFACES) {
364 inums[n] = inum;
365 nalts[n] = 1;
366 ++n;
367 }
368
369 } else if (header->bDescriptorType == USB_DT_DEVICE ||
370 header->bDescriptorType == USB_DT_CONFIG)
371 dev_warn(ddev, "config %d contains an unexpected "
372 "descriptor of type 0x%X, skipping\n",
373 cfgno, header->bDescriptorType);
374
375 } /* for ((buffer2 = buffer, size2 = size); ...) */
376 size = buffer2 - buffer;
377 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
378
379 if (n != nintf)
380 dev_warn(ddev, "config %d has %d interface%s, different from "
381 "the descriptor's value: %d\n",
382 cfgno, n, plural(n), nintf_orig);
383 else if (n == 0)
384 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
385 config->desc.bNumInterfaces = nintf = n;
386
387 /* Check for missing interface numbers */
388 for (i = 0; i < nintf; ++i) {
389 for (j = 0; j < nintf; ++j) {
390 if (inums[j] == i)
391 break;
392 }
393 if (j >= nintf)
394 dev_warn(ddev, "config %d has no interface number "
395 "%d\n", cfgno, i);
396 }
397
398 /* Allocate the usb_interface_caches and altsetting arrays */
399 for (i = 0; i < nintf; ++i) {
400 j = nalts[i];
401 if (j > USB_MAXALTSETTING) {
402 dev_warn(ddev, "too many alternate settings for "
403 "config %d interface %d: %d, "
404 "using maximum allowed: %d\n",
405 cfgno, inums[i], j, USB_MAXALTSETTING);
406 nalts[i] = j = USB_MAXALTSETTING;
407 }
408
409 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400410 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!intfc)
412 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 kref_init(&intfc->ref);
414 }
415
416 /* Skip over any Class Specific or Vendor Specific descriptors;
417 * find the first interface descriptor */
418 config->extra = buffer;
419 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
420 USB_DT_INTERFACE, &n);
421 config->extralen = i;
422 if (n > 0)
423 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
424 n, plural(n), "configuration");
425 buffer += i;
426 size -= i;
427
428 /* Parse all the interface/altsetting descriptors */
429 while (size > 0) {
430 retval = usb_parse_interface(ddev, cfgno, config,
431 buffer, size, inums, nalts);
432 if (retval < 0)
433 return retval;
434
435 buffer += retval;
436 size -= retval;
437 }
438
439 /* Check for missing altsettings */
440 for (i = 0; i < nintf; ++i) {
441 intfc = config->intf_cache[i];
442 for (j = 0; j < intfc->num_altsetting; ++j) {
443 for (n = 0; n < intfc->num_altsetting; ++n) {
444 if (intfc->altsetting[n].desc.
445 bAlternateSetting == j)
446 break;
447 }
448 if (n >= intfc->num_altsetting)
449 dev_warn(ddev, "config %d interface %d has no "
450 "altsetting %d\n", cfgno, inums[i], j);
451 }
452 }
453
454 return 0;
455}
456
457// hub-only!! ... and only exported for reset/reinit path.
458// otherwise used internally on disconnect/destroy path
459void usb_destroy_configuration(struct usb_device *dev)
460{
461 int c, i;
462
463 if (!dev->config)
464 return;
465
466 if (dev->rawdescriptors) {
467 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
468 kfree(dev->rawdescriptors[i]);
469
470 kfree(dev->rawdescriptors);
471 dev->rawdescriptors = NULL;
472 }
473
474 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
475 struct usb_host_config *cf = &dev->config[c];
476
477 kfree(cf->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
479 if (cf->intf_cache[i])
480 kref_put(&cf->intf_cache[i]->ref,
481 usb_release_interface_cache);
482 }
483 }
484 kfree(dev->config);
485 dev->config = NULL;
486}
487
488
489// hub-only!! ... and only in reset path, or usb_new_device()
490// (used by real hubs and virtual root hubs)
491int usb_get_configuration(struct usb_device *dev)
492{
493 struct device *ddev = &dev->dev;
494 int ncfg = dev->descriptor.bNumConfigurations;
495 int result = -ENOMEM;
496 unsigned int cfgno, length;
497 unsigned char *buffer;
498 unsigned char *bigbuffer;
499 struct usb_config_descriptor *desc;
500
501 if (ncfg > USB_MAXCONFIG) {
502 dev_warn(ddev, "too many configurations: %d, "
503 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
504 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
505 }
506
507 if (ncfg < 1) {
508 dev_err(ddev, "no configurations\n");
509 return -EINVAL;
510 }
511
512 length = ncfg * sizeof(struct usb_host_config);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400513 dev->config = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!dev->config)
515 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 length = ncfg * sizeof(char *);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400518 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (!dev->rawdescriptors)
520 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
523 if (!buffer)
524 goto err2;
525 desc = (struct usb_config_descriptor *)buffer;
526
527 for (cfgno = 0; cfgno < ncfg; cfgno++) {
528 /* We grab just the first descriptor so we know how long
529 * the whole configuration is */
530 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
531 buffer, USB_DT_CONFIG_SIZE);
532 if (result < 0) {
533 dev_err(ddev, "unable to read config index %d "
534 "descriptor/%s\n", cfgno, "start");
Inaky Perez-Gonzalezcb4c8fe2006-08-25 19:35:28 -0700535 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
536 dev->descriptor.bNumConfigurations = cfgno;
537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 } else if (result < 4) {
539 dev_err(ddev, "config index %d descriptor too short "
540 "(expected %i, got %i)\n", cfgno,
541 USB_DT_CONFIG_SIZE, result);
542 result = -EINVAL;
543 goto err;
544 }
545 length = max((int) le16_to_cpu(desc->wTotalLength),
546 USB_DT_CONFIG_SIZE);
547
548 /* Now that we know the length, get the whole thing */
549 bigbuffer = kmalloc(length, GFP_KERNEL);
550 if (!bigbuffer) {
551 result = -ENOMEM;
552 goto err;
553 }
554 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
555 bigbuffer, length);
556 if (result < 0) {
557 dev_err(ddev, "unable to read config index %d "
558 "descriptor/%s\n", cfgno, "all");
559 kfree(bigbuffer);
560 goto err;
561 }
562 if (result < length) {
563 dev_warn(ddev, "config index %d descriptor too short "
564 "(expected %i, got %i)\n", cfgno, length, result);
565 length = result;
566 }
567
568 dev->rawdescriptors[cfgno] = bigbuffer;
569
570 result = usb_parse_configuration(&dev->dev, cfgno,
571 &dev->config[cfgno], bigbuffer, length);
572 if (result < 0) {
573 ++cfgno;
574 goto err;
575 }
576 }
577 result = 0;
578
579err:
580 kfree(buffer);
581 dev->descriptor.bNumConfigurations = cfgno;
582err2:
583 if (result == -ENOMEM)
584 dev_err(ddev, "out of memory\n");
585 return result;
586}