2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/slab.h>
5 #include <linux/device.h>
6 #include <asm/byteorder.h>
10 #define USB_MAXALTSETTING 128 /* Hard limit */
11 #define USB_MAXENDPOINTS 30 /* Hard limit */
13 #define USB_MAXCONFIG 8 /* Arbitrary limit */
16 static inline const char *plural(int n)
18 return (n == 1 ? "" : "s");
21 static int find_next_descriptor(unsigned char *buffer, int size,
22 int dt1, int dt2, int *num_skipped)
24 struct usb_descriptor_header *h;
26 unsigned char *buffer0 = buffer;
28 /* Find the next descriptor of type dt1 or dt2 */
30 h = (struct usb_descriptor_header *) buffer;
31 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
38 /* Store the number of descriptors skipped and return the
39 * number of bytes skipped */
42 return buffer - buffer0;
45 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
46 int asnum, struct usb_host_interface *ifp, int num_ep,
47 unsigned char *buffer, int size)
49 unsigned char *buffer0 = buffer;
50 struct usb_endpoint_descriptor *d;
51 struct usb_host_endpoint *endpoint;
54 d = (struct usb_endpoint_descriptor *) buffer;
58 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
59 n = USB_DT_ENDPOINT_AUDIO_SIZE;
60 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
61 n = USB_DT_ENDPOINT_SIZE;
63 dev_warn(ddev, "config %d interface %d altsetting %d has an "
64 "invalid endpoint descriptor of length %d, skipping\n",
65 cfgno, inum, asnum, d->bLength);
66 goto skip_to_next_endpoint_or_interface_descriptor;
69 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
70 if (i >= 16 || i == 0) {
71 dev_warn(ddev, "config %d interface %d altsetting %d has an "
72 "invalid endpoint with address 0x%X, skipping\n",
73 cfgno, inum, asnum, d->bEndpointAddress);
74 goto skip_to_next_endpoint_or_interface_descriptor;
77 /* Only store as many endpoints as we have room for */
78 if (ifp->desc.bNumEndpoints >= num_ep)
79 goto skip_to_next_endpoint_or_interface_descriptor;
81 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
82 ++ifp->desc.bNumEndpoints;
84 memcpy(&endpoint->desc, d, n);
85 INIT_LIST_HEAD(&endpoint->urb_list);
87 /* Skip over any Class Specific or Vendor Specific descriptors;
88 * find the next endpoint or interface descriptor */
89 endpoint->extra = buffer;
90 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
91 USB_DT_INTERFACE, &n);
92 endpoint->extralen = i;
94 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
95 n, plural(n), "endpoint");
96 return buffer - buffer0 + i;
98 skip_to_next_endpoint_or_interface_descriptor:
99 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
100 USB_DT_INTERFACE, NULL);
101 return buffer - buffer0 + i;
104 void usb_release_interface_cache(struct kref *ref)
106 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
109 for (j = 0; j < intfc->num_altsetting; j++) {
110 struct usb_host_interface *alt = &intfc->altsetting[j];
112 kfree(alt->endpoint);
118 static int usb_parse_interface(struct device *ddev, int cfgno,
119 struct usb_host_config *config, unsigned char *buffer, int size,
120 u8 inums[], u8 nalts[])
122 unsigned char *buffer0 = buffer;
123 struct usb_interface_descriptor *d;
125 struct usb_interface_cache *intfc;
126 struct usb_host_interface *alt;
129 int num_ep, num_ep_orig;
131 d = (struct usb_interface_descriptor *) buffer;
132 buffer += d->bLength;
135 if (d->bLength < USB_DT_INTERFACE_SIZE)
136 goto skip_to_next_interface_descriptor;
138 /* Which interface entry is this? */
140 inum = d->bInterfaceNumber;
141 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
142 if (inums[i] == inum) {
143 intfc = config->intf_cache[i];
147 if (!intfc || intfc->num_altsetting >= nalts[i])
148 goto skip_to_next_interface_descriptor;
150 /* Check for duplicate altsetting entries */
151 asnum = d->bAlternateSetting;
152 for ((i = 0, alt = &intfc->altsetting[0]);
153 i < intfc->num_altsetting;
155 if (alt->desc.bAlternateSetting == asnum) {
156 dev_warn(ddev, "Duplicate descriptor for config %d "
157 "interface %d altsetting %d, skipping\n",
159 goto skip_to_next_interface_descriptor;
163 ++intfc->num_altsetting;
164 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
166 /* Skip over any Class Specific or Vendor Specific descriptors;
167 * find the first endpoint or interface descriptor */
169 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
170 USB_DT_INTERFACE, &n);
173 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
174 n, plural(n), "interface");
178 /* Allocate space for the right(?) number of endpoints */
179 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
180 alt->desc.bNumEndpoints = 0; // Use as a counter
181 if (num_ep > USB_MAXENDPOINTS) {
182 dev_warn(ddev, "too many endpoints for config %d interface %d "
183 "altsetting %d: %d, using maximum allowed: %d\n",
184 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
185 num_ep = USB_MAXENDPOINTS;
188 len = sizeof(struct usb_host_endpoint) * num_ep;
189 alt->endpoint = kzalloc(len, GFP_KERNEL);
193 /* Parse all the endpoint descriptors */
196 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
199 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
200 num_ep, buffer, size);
209 if (n != num_ep_orig)
210 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
211 "endpoint descriptor%s, different from the interface "
212 "descriptor's value: %d\n",
213 cfgno, inum, asnum, n, plural(n), num_ep_orig);
214 return buffer - buffer0;
216 skip_to_next_interface_descriptor:
217 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
218 USB_DT_INTERFACE, NULL);
219 return buffer - buffer0 + i;
222 static int usb_parse_configuration(struct device *ddev, int cfgidx,
223 struct usb_host_config *config, unsigned char *buffer, int size)
225 unsigned char *buffer0 = buffer;
227 int nintf, nintf_orig;
229 struct usb_interface_cache *intfc;
230 unsigned char *buffer2;
232 struct usb_descriptor_header *header;
234 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
236 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
237 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
238 config->desc.bLength < USB_DT_CONFIG_SIZE) {
239 dev_err(ddev, "invalid descriptor for config index %d: "
240 "type = 0x%X, length = %d\n", cfgidx,
241 config->desc.bDescriptorType, config->desc.bLength);
244 cfgno = config->desc.bConfigurationValue;
246 buffer += config->desc.bLength;
247 size -= config->desc.bLength;
249 nintf = nintf_orig = config->desc.bNumInterfaces;
250 if (nintf > USB_MAXINTERFACES) {
251 dev_warn(ddev, "config %d has too many interfaces: %d, "
252 "using maximum allowed: %d\n",
253 cfgno, nintf, USB_MAXINTERFACES);
254 nintf = USB_MAXINTERFACES;
257 /* Go through the descriptors, checking their length and counting the
258 * number of altsettings for each interface */
260 for ((buffer2 = buffer, size2 = size);
262 (buffer2 += header->bLength, size2 -= header->bLength)) {
264 if (size2 < sizeof(struct usb_descriptor_header)) {
265 dev_warn(ddev, "config %d descriptor has %d excess "
266 "byte%s, ignoring\n",
267 cfgno, size2, plural(size2));
271 header = (struct usb_descriptor_header *) buffer2;
272 if ((header->bLength > size2) || (header->bLength < 2)) {
273 dev_warn(ddev, "config %d has an invalid descriptor "
274 "of length %d, skipping remainder of the config\n",
275 cfgno, header->bLength);
279 if (header->bDescriptorType == USB_DT_INTERFACE) {
280 struct usb_interface_descriptor *d;
283 d = (struct usb_interface_descriptor *) header;
284 if (d->bLength < USB_DT_INTERFACE_SIZE) {
285 dev_warn(ddev, "config %d has an invalid "
286 "interface descriptor of length %d, "
287 "skipping\n", cfgno, d->bLength);
291 inum = d->bInterfaceNumber;
292 if (inum >= nintf_orig)
293 dev_warn(ddev, "config %d has an invalid "
294 "interface number: %d but max is %d\n",
295 cfgno, inum, nintf_orig - 1);
297 /* Have we already encountered this interface?
298 * Count its altsettings */
299 for (i = 0; i < n; ++i) {
300 if (inums[i] == inum)
306 } else if (n < USB_MAXINTERFACES) {
312 } else if (header->bDescriptorType == USB_DT_DEVICE ||
313 header->bDescriptorType == USB_DT_CONFIG)
314 dev_warn(ddev, "config %d contains an unexpected "
315 "descriptor of type 0x%X, skipping\n",
316 cfgno, header->bDescriptorType);
318 } /* for ((buffer2 = buffer, size2 = size); ...) */
319 size = buffer2 - buffer;
320 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
323 dev_warn(ddev, "config %d has %d interface%s, different from "
324 "the descriptor's value: %d\n",
325 cfgno, n, plural(n), nintf_orig);
327 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
328 config->desc.bNumInterfaces = nintf = n;
330 /* Check for missing interface numbers */
331 for (i = 0; i < nintf; ++i) {
332 for (j = 0; j < nintf; ++j) {
337 dev_warn(ddev, "config %d has no interface number "
341 /* Allocate the usb_interface_caches and altsetting arrays */
342 for (i = 0; i < nintf; ++i) {
344 if (j > USB_MAXALTSETTING) {
345 dev_warn(ddev, "too many alternate settings for "
346 "config %d interface %d: %d, "
347 "using maximum allowed: %d\n",
348 cfgno, inums[i], j, USB_MAXALTSETTING);
349 nalts[i] = j = USB_MAXALTSETTING;
352 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
353 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
356 kref_init(&intfc->ref);
359 /* Skip over any Class Specific or Vendor Specific descriptors;
360 * find the first interface descriptor */
361 config->extra = buffer;
362 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
363 USB_DT_INTERFACE, &n);
364 config->extralen = i;
366 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
367 n, plural(n), "configuration");
371 /* Parse all the interface/altsetting descriptors */
373 retval = usb_parse_interface(ddev, cfgno, config,
374 buffer, size, inums, nalts);
382 /* Check for missing altsettings */
383 for (i = 0; i < nintf; ++i) {
384 intfc = config->intf_cache[i];
385 for (j = 0; j < intfc->num_altsetting; ++j) {
386 for (n = 0; n < intfc->num_altsetting; ++n) {
387 if (intfc->altsetting[n].desc.
388 bAlternateSetting == j)
391 if (n >= intfc->num_altsetting)
392 dev_warn(ddev, "config %d interface %d has no "
393 "altsetting %d\n", cfgno, inums[i], j);
400 // hub-only!! ... and only exported for reset/reinit path.
401 // otherwise used internally on disconnect/destroy path
402 void usb_destroy_configuration(struct usb_device *dev)
409 if (dev->rawdescriptors) {
410 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
411 kfree(dev->rawdescriptors[i]);
413 kfree(dev->rawdescriptors);
414 dev->rawdescriptors = NULL;
417 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
418 struct usb_host_config *cf = &dev->config[c];
421 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
422 if (cf->intf_cache[i])
423 kref_put(&cf->intf_cache[i]->ref,
424 usb_release_interface_cache);
432 // hub-only!! ... and only in reset path, or usb_new_device()
433 // (used by real hubs and virtual root hubs)
434 int usb_get_configuration(struct usb_device *dev)
436 struct device *ddev = &dev->dev;
437 int ncfg = dev->descriptor.bNumConfigurations;
438 int result = -ENOMEM;
439 unsigned int cfgno, length;
440 unsigned char *buffer;
441 unsigned char *bigbuffer;
442 struct usb_config_descriptor *desc;
444 if (ncfg > USB_MAXCONFIG) {
445 dev_warn(ddev, "too many configurations: %d, "
446 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
447 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
451 dev_err(ddev, "no configurations\n");
455 length = ncfg * sizeof(struct usb_host_config);
456 dev->config = kzalloc(length, GFP_KERNEL);
460 length = ncfg * sizeof(char *);
461 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
462 if (!dev->rawdescriptors)
465 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
468 desc = (struct usb_config_descriptor *)buffer;
470 for (cfgno = 0; cfgno < ncfg; cfgno++) {
471 /* We grab just the first descriptor so we know how long
472 * the whole configuration is */
473 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
474 buffer, USB_DT_CONFIG_SIZE);
476 dev_err(ddev, "unable to read config index %d "
477 "descriptor/%s\n", cfgno, "start");
478 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
479 dev->descriptor.bNumConfigurations = cfgno;
481 } else if (result < 4) {
482 dev_err(ddev, "config index %d descriptor too short "
483 "(expected %i, got %i)\n", cfgno,
484 USB_DT_CONFIG_SIZE, result);
488 length = max((int) le16_to_cpu(desc->wTotalLength),
491 /* Now that we know the length, get the whole thing */
492 bigbuffer = kmalloc(length, GFP_KERNEL);
497 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
500 dev_err(ddev, "unable to read config index %d "
501 "descriptor/%s\n", cfgno, "all");
505 if (result < length) {
506 dev_warn(ddev, "config index %d descriptor too short "
507 "(expected %i, got %i)\n", cfgno, length, result);
511 dev->rawdescriptors[cfgno] = bigbuffer;
513 result = usb_parse_configuration(&dev->dev, cfgno,
514 &dev->config[cfgno], bigbuffer, length);
524 dev->descriptor.bNumConfigurations = cfgno;
526 if (result == -ENOMEM)
527 dev_err(ddev, "out of memory\n");