blob: 5e113db41b591890b740f0bac87a7983a034a1ba [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
Laurent Pinchart300871c2007-06-12 21:47:17 +020088 /* Fix up bInterval values outside the legal range. Use 32 ms if no
89 * proper value can be guessed. */
Alan Stern615ae112007-06-08 15:23:27 -040090 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:
Laurent Pinchart300871c2007-06-12 21:47:17 +020096 /* Many device manufacturers are using full-speed
97 * bInterval values in high-speed interrupt endpoint
98 * descriptors. Try to fix those and fall back to a
99 * 32 ms default value otherwise. */
100 n = fls(d->bInterval*8);
101 if (n == 0)
102 n = 9; /* 32 ms = 2^(9-1) uframes */
Alan Stern615ae112007-06-08 15:23:27 -0400103 j = 16;
104 break;
105 default: /* USB_SPEED_FULL or _LOW */
106 /* For low-speed, 10 ms is the official minimum.
107 * But some "overclocked" devices might want faster
108 * polling so we'll allow it. */
109 n = 32;
110 break;
111 }
112 } else if (usb_endpoint_xfer_isoc(d)) {
113 i = 1;
114 j = 16;
115 switch (to_usb_device(ddev)->speed) {
116 case USB_SPEED_HIGH:
117 n = 9; /* 32 ms = 2^(9-1) uframes */
118 break;
119 default: /* USB_SPEED_FULL */
120 n = 6; /* 32 ms = 2^(6-1) frames */
121 break;
122 }
123 }
124 if (d->bInterval < i || d->bInterval > j) {
125 dev_warn(ddev, "config %d interface %d altsetting %d "
126 "endpoint 0x%X has an invalid bInterval %d, "
127 "changing to %d\n",
128 cfgno, inum, asnum,
129 d->bEndpointAddress, d->bInterval, n);
130 endpoint->desc.bInterval = n;
131 }
132
Alan Stern60aac1e2007-06-08 15:25:02 -0400133 /* Some buggy low-speed devices have Bulk endpoints, which is
134 * explicitly forbidden by the USB spec. In an attempt to make
135 * them usable, we will try treating them as Interrupt endpoints.
136 */
137 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
138 usb_endpoint_xfer_bulk(d)) {
139 dev_warn(ddev, "config %d interface %d altsetting %d "
140 "endpoint 0x%X is Bulk; changing to Interrupt\n",
141 cfgno, inum, asnum, d->bEndpointAddress);
142 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
143 endpoint->desc.bInterval = 1;
144 if (le16_to_cpu(endpoint->desc.wMaxPacketSize) > 8)
145 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Skip over any Class Specific or Vendor Specific descriptors;
149 * find the next endpoint or interface descriptor */
150 endpoint->extra = buffer;
151 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
152 USB_DT_INTERFACE, &n);
153 endpoint->extralen = i;
154 if (n > 0)
155 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
156 n, plural(n), "endpoint");
157 return buffer - buffer0 + i;
158
159skip_to_next_endpoint_or_interface_descriptor:
160 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
161 USB_DT_INTERFACE, NULL);
162 return buffer - buffer0 + i;
163}
164
165void usb_release_interface_cache(struct kref *ref)
166{
167 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
168 int j;
169
Alan Stern4f62efe2005-10-24 16:24:14 -0400170 for (j = 0; j < intfc->num_altsetting; j++) {
171 struct usb_host_interface *alt = &intfc->altsetting[j];
172
173 kfree(alt->endpoint);
174 kfree(alt->string);
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kfree(intfc);
177}
178
179static int usb_parse_interface(struct device *ddev, int cfgno,
180 struct usb_host_config *config, unsigned char *buffer, int size,
181 u8 inums[], u8 nalts[])
182{
183 unsigned char *buffer0 = buffer;
184 struct usb_interface_descriptor *d;
185 int inum, asnum;
186 struct usb_interface_cache *intfc;
187 struct usb_host_interface *alt;
188 int i, n;
189 int len, retval;
190 int num_ep, num_ep_orig;
191
192 d = (struct usb_interface_descriptor *) buffer;
193 buffer += d->bLength;
194 size -= d->bLength;
195
196 if (d->bLength < USB_DT_INTERFACE_SIZE)
197 goto skip_to_next_interface_descriptor;
198
199 /* Which interface entry is this? */
200 intfc = NULL;
201 inum = d->bInterfaceNumber;
202 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
203 if (inums[i] == inum) {
204 intfc = config->intf_cache[i];
205 break;
206 }
207 }
208 if (!intfc || intfc->num_altsetting >= nalts[i])
209 goto skip_to_next_interface_descriptor;
210
211 /* Check for duplicate altsetting entries */
212 asnum = d->bAlternateSetting;
213 for ((i = 0, alt = &intfc->altsetting[0]);
214 i < intfc->num_altsetting;
215 (++i, ++alt)) {
216 if (alt->desc.bAlternateSetting == asnum) {
217 dev_warn(ddev, "Duplicate descriptor for config %d "
218 "interface %d altsetting %d, skipping\n",
219 cfgno, inum, asnum);
220 goto skip_to_next_interface_descriptor;
221 }
222 }
223
224 ++intfc->num_altsetting;
225 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
226
227 /* Skip over any Class Specific or Vendor Specific descriptors;
228 * find the first endpoint or interface descriptor */
229 alt->extra = buffer;
230 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
231 USB_DT_INTERFACE, &n);
232 alt->extralen = i;
233 if (n > 0)
234 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
235 n, plural(n), "interface");
236 buffer += i;
237 size -= i;
238
239 /* Allocate space for the right(?) number of endpoints */
240 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
241 alt->desc.bNumEndpoints = 0; // Use as a counter
242 if (num_ep > USB_MAXENDPOINTS) {
243 dev_warn(ddev, "too many endpoints for config %d interface %d "
244 "altsetting %d: %d, using maximum allowed: %d\n",
245 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
246 num_ep = USB_MAXENDPOINTS;
247 }
248
Alan Stern57a21c12007-05-15 17:40:37 -0400249 if (num_ep > 0) { /* Can't allocate 0 bytes */
250 len = sizeof(struct usb_host_endpoint) * num_ep;
251 alt->endpoint = kzalloc(len, GFP_KERNEL);
252 if (!alt->endpoint)
253 return -ENOMEM;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 /* Parse all the endpoint descriptors */
257 n = 0;
258 while (size > 0) {
259 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
260 == USB_DT_INTERFACE)
261 break;
262 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
263 num_ep, buffer, size);
264 if (retval < 0)
265 return retval;
266 ++n;
267
268 buffer += retval;
269 size -= retval;
270 }
271
272 if (n != num_ep_orig)
273 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
274 "endpoint descriptor%s, different from the interface "
275 "descriptor's value: %d\n",
276 cfgno, inum, asnum, n, plural(n), num_ep_orig);
277 return buffer - buffer0;
278
279skip_to_next_interface_descriptor:
280 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
281 USB_DT_INTERFACE, NULL);
282 return buffer - buffer0 + i;
283}
284
285static int usb_parse_configuration(struct device *ddev, int cfgidx,
286 struct usb_host_config *config, unsigned char *buffer, int size)
287{
288 unsigned char *buffer0 = buffer;
289 int cfgno;
290 int nintf, nintf_orig;
291 int i, j, n;
292 struct usb_interface_cache *intfc;
293 unsigned char *buffer2;
294 int size2;
295 struct usb_descriptor_header *header;
296 int len, retval;
297 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
298
299 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
300 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
301 config->desc.bLength < USB_DT_CONFIG_SIZE) {
302 dev_err(ddev, "invalid descriptor for config index %d: "
303 "type = 0x%X, length = %d\n", cfgidx,
304 config->desc.bDescriptorType, config->desc.bLength);
305 return -EINVAL;
306 }
307 cfgno = config->desc.bConfigurationValue;
308
309 buffer += config->desc.bLength;
310 size -= config->desc.bLength;
311
312 nintf = nintf_orig = config->desc.bNumInterfaces;
313 if (nintf > USB_MAXINTERFACES) {
314 dev_warn(ddev, "config %d has too many interfaces: %d, "
315 "using maximum allowed: %d\n",
316 cfgno, nintf, USB_MAXINTERFACES);
317 nintf = USB_MAXINTERFACES;
318 }
319
320 /* Go through the descriptors, checking their length and counting the
321 * number of altsettings for each interface */
322 n = 0;
323 for ((buffer2 = buffer, size2 = size);
324 size2 > 0;
325 (buffer2 += header->bLength, size2 -= header->bLength)) {
326
327 if (size2 < sizeof(struct usb_descriptor_header)) {
328 dev_warn(ddev, "config %d descriptor has %d excess "
329 "byte%s, ignoring\n",
330 cfgno, size2, plural(size2));
331 break;
332 }
333
334 header = (struct usb_descriptor_header *) buffer2;
335 if ((header->bLength > size2) || (header->bLength < 2)) {
336 dev_warn(ddev, "config %d has an invalid descriptor "
337 "of length %d, skipping remainder of the config\n",
338 cfgno, header->bLength);
339 break;
340 }
341
342 if (header->bDescriptorType == USB_DT_INTERFACE) {
343 struct usb_interface_descriptor *d;
344 int inum;
345
346 d = (struct usb_interface_descriptor *) header;
347 if (d->bLength < USB_DT_INTERFACE_SIZE) {
348 dev_warn(ddev, "config %d has an invalid "
349 "interface descriptor of length %d, "
350 "skipping\n", cfgno, d->bLength);
351 continue;
352 }
353
354 inum = d->bInterfaceNumber;
355 if (inum >= nintf_orig)
356 dev_warn(ddev, "config %d has an invalid "
357 "interface number: %d but max is %d\n",
358 cfgno, inum, nintf_orig - 1);
359
360 /* Have we already encountered this interface?
361 * Count its altsettings */
362 for (i = 0; i < n; ++i) {
363 if (inums[i] == inum)
364 break;
365 }
366 if (i < n) {
367 if (nalts[i] < 255)
368 ++nalts[i];
369 } else if (n < USB_MAXINTERFACES) {
370 inums[n] = inum;
371 nalts[n] = 1;
372 ++n;
373 }
374
375 } else if (header->bDescriptorType == USB_DT_DEVICE ||
376 header->bDescriptorType == USB_DT_CONFIG)
377 dev_warn(ddev, "config %d contains an unexpected "
378 "descriptor of type 0x%X, skipping\n",
379 cfgno, header->bDescriptorType);
380
381 } /* for ((buffer2 = buffer, size2 = size); ...) */
382 size = buffer2 - buffer;
383 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
384
385 if (n != nintf)
386 dev_warn(ddev, "config %d has %d interface%s, different from "
387 "the descriptor's value: %d\n",
388 cfgno, n, plural(n), nintf_orig);
389 else if (n == 0)
390 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
391 config->desc.bNumInterfaces = nintf = n;
392
393 /* Check for missing interface numbers */
394 for (i = 0; i < nintf; ++i) {
395 for (j = 0; j < nintf; ++j) {
396 if (inums[j] == i)
397 break;
398 }
399 if (j >= nintf)
400 dev_warn(ddev, "config %d has no interface number "
401 "%d\n", cfgno, i);
402 }
403
404 /* Allocate the usb_interface_caches and altsetting arrays */
405 for (i = 0; i < nintf; ++i) {
406 j = nalts[i];
407 if (j > USB_MAXALTSETTING) {
408 dev_warn(ddev, "too many alternate settings for "
409 "config %d interface %d: %d, "
410 "using maximum allowed: %d\n",
411 cfgno, inums[i], j, USB_MAXALTSETTING);
412 nalts[i] = j = USB_MAXALTSETTING;
413 }
414
415 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400416 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!intfc)
418 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 kref_init(&intfc->ref);
420 }
421
422 /* Skip over any Class Specific or Vendor Specific descriptors;
423 * find the first interface descriptor */
424 config->extra = buffer;
425 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
426 USB_DT_INTERFACE, &n);
427 config->extralen = i;
428 if (n > 0)
429 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
430 n, plural(n), "configuration");
431 buffer += i;
432 size -= i;
433
434 /* Parse all the interface/altsetting descriptors */
435 while (size > 0) {
436 retval = usb_parse_interface(ddev, cfgno, config,
437 buffer, size, inums, nalts);
438 if (retval < 0)
439 return retval;
440
441 buffer += retval;
442 size -= retval;
443 }
444
445 /* Check for missing altsettings */
446 for (i = 0; i < nintf; ++i) {
447 intfc = config->intf_cache[i];
448 for (j = 0; j < intfc->num_altsetting; ++j) {
449 for (n = 0; n < intfc->num_altsetting; ++n) {
450 if (intfc->altsetting[n].desc.
451 bAlternateSetting == j)
452 break;
453 }
454 if (n >= intfc->num_altsetting)
455 dev_warn(ddev, "config %d interface %d has no "
456 "altsetting %d\n", cfgno, inums[i], j);
457 }
458 }
459
460 return 0;
461}
462
463// hub-only!! ... and only exported for reset/reinit path.
464// otherwise used internally on disconnect/destroy path
465void usb_destroy_configuration(struct usb_device *dev)
466{
467 int c, i;
468
469 if (!dev->config)
470 return;
471
472 if (dev->rawdescriptors) {
473 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
474 kfree(dev->rawdescriptors[i]);
475
476 kfree(dev->rawdescriptors);
477 dev->rawdescriptors = NULL;
478 }
479
480 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
481 struct usb_host_config *cf = &dev->config[c];
482
483 kfree(cf->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
485 if (cf->intf_cache[i])
486 kref_put(&cf->intf_cache[i]->ref,
487 usb_release_interface_cache);
488 }
489 }
490 kfree(dev->config);
491 dev->config = NULL;
492}
493
494
495// hub-only!! ... and only in reset path, or usb_new_device()
496// (used by real hubs and virtual root hubs)
497int usb_get_configuration(struct usb_device *dev)
498{
499 struct device *ddev = &dev->dev;
500 int ncfg = dev->descriptor.bNumConfigurations;
501 int result = -ENOMEM;
502 unsigned int cfgno, length;
503 unsigned char *buffer;
504 unsigned char *bigbuffer;
505 struct usb_config_descriptor *desc;
506
507 if (ncfg > USB_MAXCONFIG) {
508 dev_warn(ddev, "too many configurations: %d, "
509 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
510 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
511 }
512
513 if (ncfg < 1) {
514 dev_err(ddev, "no configurations\n");
515 return -EINVAL;
516 }
517
518 length = ncfg * sizeof(struct usb_host_config);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400519 dev->config = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!dev->config)
521 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 length = ncfg * sizeof(char *);
Alan Stern0a1ef3b2005-10-24 15:38:24 -0400524 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (!dev->rawdescriptors)
526 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
529 if (!buffer)
530 goto err2;
531 desc = (struct usb_config_descriptor *)buffer;
532
533 for (cfgno = 0; cfgno < ncfg; cfgno++) {
534 /* We grab just the first descriptor so we know how long
535 * the whole configuration is */
536 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
537 buffer, USB_DT_CONFIG_SIZE);
538 if (result < 0) {
539 dev_err(ddev, "unable to read config index %d "
540 "descriptor/%s\n", cfgno, "start");
Inaky Perez-Gonzalezcb4c8fe2006-08-25 19:35:28 -0700541 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
542 dev->descriptor.bNumConfigurations = cfgno;
543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else if (result < 4) {
545 dev_err(ddev, "config index %d descriptor too short "
546 "(expected %i, got %i)\n", cfgno,
547 USB_DT_CONFIG_SIZE, result);
548 result = -EINVAL;
549 goto err;
550 }
551 length = max((int) le16_to_cpu(desc->wTotalLength),
552 USB_DT_CONFIG_SIZE);
553
554 /* Now that we know the length, get the whole thing */
555 bigbuffer = kmalloc(length, GFP_KERNEL);
556 if (!bigbuffer) {
557 result = -ENOMEM;
558 goto err;
559 }
560 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
561 bigbuffer, length);
562 if (result < 0) {
563 dev_err(ddev, "unable to read config index %d "
564 "descriptor/%s\n", cfgno, "all");
565 kfree(bigbuffer);
566 goto err;
567 }
568 if (result < length) {
569 dev_warn(ddev, "config index %d descriptor too short "
570 "(expected %i, got %i)\n", cfgno, length, result);
571 length = result;
572 }
573
574 dev->rawdescriptors[cfgno] = bigbuffer;
575
576 result = usb_parse_configuration(&dev->dev, cfgno,
577 &dev->config[cfgno], bigbuffer, length);
578 if (result < 0) {
579 ++cfgno;
580 goto err;
581 }
582 }
583 result = 0;
584
585err:
586 kfree(buffer);
587 dev->descriptor.bNumConfigurations = cfgno;
588err2:
589 if (result == -ENOMEM)
590 dev_err(ddev, "out of memory\n");
591 return result;
592}