MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / usb / core / config.c
blobbfb3731d42db816531a4cacd69d692ff04727827
1 #include <linux/usb.h>
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>
7 #include "usb.h"
8 #include "hcd.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;
25 int n = 0;
26 unsigned char *buffer0 = buffer;
28 /* Find the next descriptor of type dt1 or dt2 */
29 while (size > 0) {
30 h = (struct usb_descriptor_header *) buffer;
31 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
32 break;
33 buffer += h->bLength;
34 size -= h->bLength;
35 ++n;
38 /* Store the number of descriptors skipped and return the
39 * number of bytes skipped */
40 if (num_skipped)
41 *num_skipped = n;
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;
52 int n, i;
54 d = (struct usb_endpoint_descriptor *) buffer;
55 buffer += d->bLength;
56 size -= d->bLength;
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;
62 else {
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;
93 if (n > 0)
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);
107 int j;
109 for (j = 0; j < intfc->num_altsetting; j++) {
110 struct usb_host_interface *alt = &intfc->altsetting[j];
112 kfree(alt->endpoint);
113 kfree(alt->string);
115 kfree(intfc);
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;
124 int inum, asnum;
125 struct usb_interface_cache *intfc;
126 struct usb_host_interface *alt;
127 int i, n;
128 int len, retval;
129 int num_ep, num_ep_orig;
131 d = (struct usb_interface_descriptor *) buffer;
132 buffer += d->bLength;
133 size -= d->bLength;
135 if (d->bLength < USB_DT_INTERFACE_SIZE)
136 goto skip_to_next_interface_descriptor;
138 /* Which interface entry is this? */
139 intfc = NULL;
140 inum = d->bInterfaceNumber;
141 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
142 if (inums[i] == inum) {
143 intfc = config->intf_cache[i];
144 break;
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;
154 (++i, ++alt)) {
155 if (alt->desc.bAlternateSetting == asnum) {
156 dev_warn(ddev, "Duplicate descriptor for config %d "
157 "interface %d altsetting %d, skipping\n",
158 cfgno, inum, asnum);
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 */
168 alt->extra = buffer;
169 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
170 USB_DT_INTERFACE, &n);
171 alt->extralen = i;
172 if (n > 0)
173 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
174 n, plural(n), "interface");
175 buffer += i;
176 size -= i;
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);
190 if (!alt->endpoint)
191 return -ENOMEM;
193 /* Parse all the endpoint descriptors */
194 n = 0;
195 while (size > 0) {
196 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
197 == USB_DT_INTERFACE)
198 break;
199 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
200 num_ep, buffer, size);
201 if (retval < 0)
202 return retval;
203 ++n;
205 buffer += retval;
206 size -= retval;
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;
226 int cfgno;
227 int nintf, nintf_orig;
228 int i, j, n;
229 struct usb_interface_cache *intfc;
230 unsigned char *buffer2;
231 int size2;
232 struct usb_descriptor_header *header;
233 int len, retval;
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);
242 return -EINVAL;
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 */
259 n = 0;
260 for ((buffer2 = buffer, size2 = size);
261 size2 > 0;
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));
268 break;
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);
276 break;
279 if (header->bDescriptorType == USB_DT_INTERFACE) {
280 struct usb_interface_descriptor *d;
281 int inum;
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);
288 continue;
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)
301 break;
303 if (i < n) {
304 if (nalts[i] < 255)
305 ++nalts[i];
306 } else if (n < USB_MAXINTERFACES) {
307 inums[n] = inum;
308 nalts[n] = 1;
309 ++n;
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);
322 if (n != nintf)
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);
326 else if (n == 0)
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) {
333 if (inums[j] == i)
334 break;
336 if (j >= nintf)
337 dev_warn(ddev, "config %d has no interface number "
338 "%d\n", cfgno, i);
341 /* Allocate the usb_interface_caches and altsetting arrays */
342 for (i = 0; i < nintf; ++i) {
343 j = nalts[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);
354 if (!intfc)
355 return -ENOMEM;
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;
365 if (n > 0)
366 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
367 n, plural(n), "configuration");
368 buffer += i;
369 size -= i;
371 /* Parse all the interface/altsetting descriptors */
372 while (size > 0) {
373 retval = usb_parse_interface(ddev, cfgno, config,
374 buffer, size, inums, nalts);
375 if (retval < 0)
376 return retval;
378 buffer += retval;
379 size -= retval;
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)
389 break;
391 if (n >= intfc->num_altsetting)
392 dev_warn(ddev, "config %d interface %d has no "
393 "altsetting %d\n", cfgno, inums[i], j);
397 return 0;
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)
404 int c, i;
406 if (!dev->config)
407 return;
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];
420 kfree(cf->string);
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);
427 kfree(dev->config);
428 dev->config = NULL;
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;
450 if (ncfg < 1) {
451 dev_err(ddev, "no configurations\n");
452 return -EINVAL;
455 length = ncfg * sizeof(struct usb_host_config);
456 dev->config = kzalloc(length, GFP_KERNEL);
457 if (!dev->config)
458 goto err2;
460 length = ncfg * sizeof(char *);
461 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
462 if (!dev->rawdescriptors)
463 goto err2;
465 buffer = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
466 if (!buffer)
467 goto err2;
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);
475 if (result < 0) {
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;
480 break;
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);
485 result = -EINVAL;
486 goto err;
488 length = max((int) le16_to_cpu(desc->wTotalLength),
489 USB_DT_CONFIG_SIZE);
491 /* Now that we know the length, get the whole thing */
492 bigbuffer = kmalloc(length, GFP_KERNEL);
493 if (!bigbuffer) {
494 result = -ENOMEM;
495 goto err;
497 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
498 bigbuffer, length);
499 if (result < 0) {
500 dev_err(ddev, "unable to read config index %d "
501 "descriptor/%s\n", cfgno, "all");
502 kfree(bigbuffer);
503 goto err;
505 if (result < length) {
506 dev_warn(ddev, "config index %d descriptor too short "
507 "(expected %i, got %i)\n", cfgno, length, result);
508 length = result;
511 dev->rawdescriptors[cfgno] = bigbuffer;
513 result = usb_parse_configuration(&dev->dev, cfgno,
514 &dev->config[cfgno], bigbuffer, length);
515 if (result < 0) {
516 ++cfgno;
517 goto err;
520 result = 0;
522 err:
523 kfree(buffer);
524 dev->descriptor.bNumConfigurations = cfgno;
525 err2:
526 if (result == -ENOMEM)
527 dev_err(ddev, "out of memory\n");
528 return result;