2 #include <linux/usb/ch9.h>
3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 #include <linux/device.h>
7 #include <asm/byteorder.h>
11 #define USB_MAXALTSETTING 128 /* Hard limit */
12 #define USB_MAXENDPOINTS 30 /* Hard limit */
14 #define USB_MAXCONFIG 8 /* Arbitrary limit */
17 static inline const char *plural(int n)
19 return (n == 1 ? "" : "s");
22 static int find_next_descriptor(unsigned char *buffer, int size,
23 int dt1, int dt2, int *num_skipped)
25 struct usb_descriptor_header *h;
27 unsigned char *buffer0 = buffer;
29 /* Find the next descriptor of type dt1 or dt2 */
31 h = (struct usb_descriptor_header *) buffer;
32 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
39 /* Store the number of descriptors skipped and return the
40 * number of bytes skipped */
43 return buffer - buffer0;
46 static 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)
50 unsigned char *buffer0 = buffer;
51 struct usb_endpoint_descriptor *d;
52 struct usb_host_endpoint *endpoint;
55 d = (struct usb_endpoint_descriptor *) buffer;
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;
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;
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;
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;
82 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
83 ++ifp->desc.bNumEndpoints;
85 memcpy(&endpoint->desc, d, n);
86 INIT_LIST_HEAD(&endpoint->urb_list);
88 /* 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 */
92 if (usb_endpoint_xfer_int(d)) {
94 switch (to_usb_device(ddev)->speed) {
96 n = 9; /* 32 ms = 2^(9-1) uframes */
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. */
106 } else if (usb_endpoint_xfer_isoc(d)) {
109 switch (to_usb_device(ddev)->speed) {
111 n = 9; /* 32 ms = 2^(9-1) uframes */
113 default: /* USB_SPEED_FULL */
114 n = 6; /* 32 ms = 2^(6-1) frames */
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, "
123 d->bEndpointAddress, d->bInterval, n);
124 endpoint->desc.bInterval = n;
127 /* 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.
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);
142 /* 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;
149 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
150 n, plural(n), "endpoint");
151 return buffer - buffer0 + i;
153 skip_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;
159 void usb_release_interface_cache(struct kref *ref)
161 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
164 for (j = 0; j < intfc->num_altsetting; j++) {
165 struct usb_host_interface *alt = &intfc->altsetting[j];
167 kfree(alt->endpoint);
173 static 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[])
177 unsigned char *buffer0 = buffer;
178 struct usb_interface_descriptor *d;
180 struct usb_interface_cache *intfc;
181 struct usb_host_interface *alt;
184 int num_ep, num_ep_orig;
186 d = (struct usb_interface_descriptor *) buffer;
187 buffer += d->bLength;
190 if (d->bLength < USB_DT_INTERFACE_SIZE)
191 goto skip_to_next_interface_descriptor;
193 /* Which interface entry is this? */
195 inum = d->bInterfaceNumber;
196 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
197 if (inums[i] == inum) {
198 intfc = config->intf_cache[i];
202 if (!intfc || intfc->num_altsetting >= nalts[i])
203 goto skip_to_next_interface_descriptor;
205 /* Check for duplicate altsetting entries */
206 asnum = d->bAlternateSetting;
207 for ((i = 0, alt = &intfc->altsetting[0]);
208 i < intfc->num_altsetting;
210 if (alt->desc.bAlternateSetting == asnum) {
211 dev_warn(ddev, "Duplicate descriptor for config %d "
212 "interface %d altsetting %d, skipping\n",
214 goto skip_to_next_interface_descriptor;
218 ++intfc->num_altsetting;
219 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
221 /* Skip over any Class Specific or Vendor Specific descriptors;
222 * find the first endpoint or interface descriptor */
224 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
225 USB_DT_INTERFACE, &n);
228 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
229 n, plural(n), "interface");
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;
243 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);
250 /* Parse all the endpoint descriptors */
253 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
256 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
257 num_ep, buffer, size);
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;
273 skip_to_next_interface_descriptor:
274 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
275 USB_DT_INTERFACE, NULL);
276 return buffer - buffer0 + i;
279 static int usb_parse_configuration(struct device *ddev, int cfgidx,
280 struct usb_host_config *config, unsigned char *buffer, int size)
282 unsigned char *buffer0 = buffer;
284 int nintf, nintf_orig;
286 struct usb_interface_cache *intfc;
287 unsigned char *buffer2;
289 struct usb_descriptor_header *header;
291 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
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);
301 cfgno = config->desc.bConfigurationValue;
303 buffer += config->desc.bLength;
304 size -= config->desc.bLength;
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;
314 /* Go through the descriptors, checking their length and counting the
315 * number of altsettings for each interface */
317 for ((buffer2 = buffer, size2 = size);
319 (buffer2 += header->bLength, size2 -= header->bLength)) {
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));
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);
336 if (header->bDescriptorType == USB_DT_INTERFACE) {
337 struct usb_interface_descriptor *d;
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);
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);
354 /* Have we already encountered this interface?
355 * Count its altsettings */
356 for (i = 0; i < n; ++i) {
357 if (inums[i] == inum)
363 } else if (n < USB_MAXINTERFACES) {
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);
375 } /* for ((buffer2 = buffer, size2 = size); ...) */
376 size = buffer2 - buffer;
377 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
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);
384 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
385 config->desc.bNumInterfaces = nintf = n;
387 /* Check for missing interface numbers */
388 for (i = 0; i < nintf; ++i) {
389 for (j = 0; j < nintf; ++j) {
394 dev_warn(ddev, "config %d has no interface number "
398 /* Allocate the usb_interface_caches and altsetting arrays */
399 for (i = 0; i < nintf; ++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;
409 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
410 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
413 kref_init(&intfc->ref);
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;
423 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
424 n, plural(n), "configuration");
428 /* Parse all the interface/altsetting descriptors */
430 retval = usb_parse_interface(ddev, cfgno, config,
431 buffer, size, inums, nalts);
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)
448 if (n >= intfc->num_altsetting)
449 dev_warn(ddev, "config %d interface %d has no "
450 "altsetting %d\n", cfgno, inums[i], j);
457 // hub-only!! ... and only exported for reset/reinit path.
458 // otherwise used internally on disconnect/destroy path
459 void usb_destroy_configuration(struct usb_device *dev)
466 if (dev->rawdescriptors) {
467 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
468 kfree(dev->rawdescriptors[i]);
470 kfree(dev->rawdescriptors);
471 dev->rawdescriptors = NULL;
474 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
475 struct usb_host_config *cf = &dev->config[c];
478 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);
489 // hub-only!! ... and only in reset path, or usb_new_device()
490 // (used by real hubs and virtual root hubs)
491 int usb_get_configuration(struct usb_device *dev)
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;
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;
508 dev_err(ddev, "no configurations\n");
512 length = ncfg * sizeof(struct usb_host_config);
513 dev->config = kzalloc(length, GFP_KERNEL);
517 length = ncfg * sizeof(char *);
518 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
519 if (!dev->rawdescriptors)
522 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
525 desc = (struct usb_config_descriptor *)buffer;
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);
533 dev_err(ddev, "unable to read config index %d "
534 "descriptor/%s\n", cfgno, "start");
535 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
536 dev->descriptor.bNumConfigurations = cfgno;
538 } 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);
545 length = max((int) le16_to_cpu(desc->wTotalLength),
548 /* Now that we know the length, get the whole thing */
549 bigbuffer = kmalloc(length, GFP_KERNEL);
554 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
557 dev_err(ddev, "unable to read config index %d "
558 "descriptor/%s\n", cfgno, "all");
562 if (result < length) {
563 dev_warn(ddev, "config index %d descriptor too short "
564 "(expected %i, got %i)\n", cfgno, length, result);
568 dev->rawdescriptors[cfgno] = bigbuffer;
570 result = usb_parse_configuration(&dev->dev, cfgno,
571 &dev->config[cfgno], bigbuffer, length);
581 dev->descriptor.bNumConfigurations = cfgno;
583 if (result == -ENOMEM)
584 dev_err(ddev, "out of memory\n");