ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / core / config.c
1 #include <linux/config.h>
2
3 #ifdef CONFIG_USB_DEBUG
4 #define DEBUG
5 #endif
6
7 #include <linux/usb.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/device.h>
12 #include <asm/byteorder.h>
13
14
15 #define USB_MAXALTSETTING               128     /* Hard limit */
16 #define USB_MAXENDPOINTS                30      /* Hard limit */
17
18 #define USB_MAXCONFIG                   8       /* Arbitrary limit */
19
20
21 static int find_next_descriptor(unsigned char *buffer, int size,
22     int dt1, int dt2, int *num_skipped)
23 {
24         struct usb_descriptor_header *h;
25         int n = 0;
26         unsigned char *buffer0 = buffer;
27
28         /* Find the next descriptor of type dt1 or dt2 */
29         while (size >= sizeof(struct usb_descriptor_header)) {
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;
36         }
37
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;
43 }
44
45 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
46     int asnum, struct usb_host_endpoint *endpoint,
47     unsigned char *buffer, int size)
48 {
49         unsigned char *buffer0 = buffer;
50         struct usb_descriptor_header *header;
51         int n, i;
52
53         header = (struct usb_descriptor_header *)buffer;
54         if (header->bDescriptorType != USB_DT_ENDPOINT) {
55                 dev_err(ddev, "config %d interface %d altsetting %d has an "
56                     "unexpected descriptor of type 0x%X, "
57                     "expecting endpoint type 0x%X\n",
58                     cfgno, inum, asnum,
59                     header->bDescriptorType, USB_DT_ENDPOINT);
60                 return -EINVAL;
61         }
62
63         if (header->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
64                 memcpy(&endpoint->desc, buffer, USB_DT_ENDPOINT_AUDIO_SIZE);
65         else if (header->bLength >= USB_DT_ENDPOINT_SIZE)
66                 memcpy(&endpoint->desc, buffer, USB_DT_ENDPOINT_SIZE);
67         else {
68                 dev_err(ddev, "config %d interface %d altsetting %d has an "
69                     "invalid endpoint descriptor of length %d\n",
70                     cfgno, inum, asnum, header->bLength);
71                 return -EINVAL;
72         }
73
74         i = endpoint->desc.bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
75         if (i >= 16 || i == 0) {
76                 dev_err(ddev, "config %d interface %d altsetting %d has an "
77                     "invalid endpoint with address 0x%X\n",
78                     cfgno, inum, asnum, endpoint->desc.bEndpointAddress);
79                 return -EINVAL;
80         }
81
82         le16_to_cpus(&endpoint->desc.wMaxPacketSize);
83
84         buffer += header->bLength;
85         size -= header->bLength;
86
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 class/vendor specific endpoint "
95                     "descriptors\n", n);
96         return buffer - buffer0 + i;
97 }
98
99 static void usb_free_intf(struct usb_interface *intf)
100 {
101         int j;
102
103         if (intf->altsetting) {
104                 for (j = 0; j < intf->num_altsetting; j++) {
105                         struct usb_host_interface *alt = &intf->altsetting[j];
106
107                         kfree(alt->endpoint);
108                 }
109                 kfree(intf->altsetting);
110         }
111         kfree(intf);
112 }
113
114 static int usb_parse_interface(struct device *ddev, int cfgno,
115     struct usb_host_config *config, unsigned char *buffer, int size)
116 {
117         unsigned char *buffer0 = buffer;
118         struct usb_interface_descriptor *d;
119         int inum, asnum;
120         struct usb_interface *interface;
121         struct usb_host_interface *alt;
122         int i, n;
123         int len, retval;
124
125         d = (struct usb_interface_descriptor *) buffer;
126         buffer += d->bLength;
127         size -= d->bLength;
128
129         if (d->bDescriptorType != USB_DT_INTERFACE) {
130                 dev_err(ddev, "config %d has an unexpected descriptor of type "
131                     "0x%X, expecting interface type 0x%X\n",
132                     cfgno, d->bDescriptorType, USB_DT_INTERFACE);
133                 return -EINVAL;
134         }
135
136         inum = d->bInterfaceNumber;
137         if (inum >= config->desc.bNumInterfaces)
138                 goto skip_to_next_interface_descriptor;
139
140         interface = config->interface[inum];
141         asnum = d->bAlternateSetting;
142         if (asnum >= interface->num_altsetting) {
143                 dev_err(ddev, "config %d interface %d has an invalid "
144                     "alternate setting number: %d but max is %d\n",
145                     cfgno, inum, asnum, interface->num_altsetting - 1);
146                 return -EINVAL;
147         }
148
149         alt = &interface->altsetting[asnum];
150         if (alt->desc.bLength) {
151                 dev_err(ddev, "Duplicate descriptor for config %d "
152                     "interface %d altsetting %d\n", cfgno, inum, asnum);
153                 return -EINVAL;
154         }
155         memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
156
157         /* Skip over any Class Specific or Vendor Specific descriptors;
158          * find the first endpoint or interface descriptor */
159         alt->extra = buffer;
160         i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
161             USB_DT_INTERFACE, &n);
162         alt->extralen = i;
163         if (n > 0)
164                 dev_dbg(ddev, "skipped %d class/vendor specific "
165                     "interface descriptors\n", n);
166         buffer += i;
167         size -= i;
168
169         if (alt->desc.bNumEndpoints > USB_MAXENDPOINTS) {
170                 dev_err(ddev, "too many endpoints for config %d interface %d "
171                     "altsetting %d: %d, maximum allowed: %d\n",
172                     cfgno, inum, asnum, alt->desc.bNumEndpoints,
173                     USB_MAXENDPOINTS);
174                 return -EINVAL;
175         }
176
177         len = alt->desc.bNumEndpoints * sizeof(struct usb_host_endpoint);
178         alt->endpoint = kmalloc(len, GFP_KERNEL);
179         if (!alt->endpoint)
180                 return -ENOMEM;
181         memset(alt->endpoint, 0, len);
182
183         for (i = 0; i < alt->desc.bNumEndpoints; i++) {
184                 if (size < USB_DT_ENDPOINT_SIZE) {
185                         dev_err(ddev, "too few endpoint descriptors for "
186                             "config %d interface %d altsetting %d\n",
187                             cfgno, inum, asnum);
188                         return -EINVAL;
189                 }
190
191                 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum,
192                     alt->endpoint + i, buffer, size);
193                 if (retval < 0)
194                         return retval;
195
196                 buffer += retval;
197                 size -= retval;
198         }
199         return buffer - buffer0;
200
201 skip_to_next_interface_descriptor:
202         i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
203             USB_DT_INTERFACE, NULL);
204         return buffer - buffer0 + i;
205 }
206
207 int usb_parse_configuration(struct device *ddev, int cfgidx,
208     struct usb_host_config *config, unsigned char *buffer, int size)
209 {
210         int cfgno;
211         int nintf, nintf_orig;
212         int i, j, n;
213         struct usb_interface *interface;
214         unsigned char *buffer2;
215         int size2;
216         struct usb_descriptor_header *header;
217         int len, retval;
218
219         memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
220         if (config->desc.bDescriptorType != USB_DT_CONFIG ||
221             config->desc.bLength < USB_DT_CONFIG_SIZE) {
222                 dev_err(ddev, "invalid descriptor for config index %d: "
223                     "type = 0x%X, length = %d\n", cfgidx,
224                     config->desc.bDescriptorType, config->desc.bLength);
225                 return -EINVAL;
226         }
227         config->desc.wTotalLength = size;
228         cfgno = config->desc.bConfigurationValue;
229
230         buffer += config->desc.bLength;
231         size -= config->desc.bLength;
232
233         nintf = nintf_orig = config->desc.bNumInterfaces;
234         if (nintf > USB_MAXINTERFACES) {
235                 dev_warn(ddev, "config %d has too many interfaces: %d, "
236                     "using maximum allowed: %d\n",
237                     cfgno, nintf, USB_MAXINTERFACES);
238                 config->desc.bNumInterfaces = nintf = USB_MAXINTERFACES;
239         }
240
241         for (i = 0; i < nintf; ++i) {
242                 interface = config->interface[i] =
243                     kmalloc(sizeof(struct usb_interface), GFP_KERNEL);
244                 if (!interface)
245                         return -ENOMEM;
246                 memset(interface, 0, sizeof(struct usb_interface));
247         }
248
249         /* Go through the descriptors, checking their length and counting the
250          * number of altsettings for each interface */
251         for ((buffer2 = buffer, size2 = size);
252               size2 >= sizeof(struct usb_descriptor_header);
253              (buffer2 += header->bLength, size2 -= header->bLength)) {
254
255                 header = (struct usb_descriptor_header *) buffer2;
256                 if ((header->bLength > size2) || (header->bLength < 2)) {
257                         dev_err(ddev, "config %d has an invalid descriptor "
258                             "of length %d\n", cfgno, header->bLength);
259                         return -EINVAL;
260                 }
261
262                 if (header->bDescriptorType == USB_DT_INTERFACE) {
263                         struct usb_interface_descriptor *d;
264
265                         d = (struct usb_interface_descriptor *) header;
266                         if (d->bLength < USB_DT_INTERFACE_SIZE) {
267                                 dev_err(ddev, "config %d has an invalid "
268                                     "interface descriptor of length %d\n",
269                                     cfgno, d->bLength);
270                                 return -EINVAL;
271                         }
272
273                         i = d->bInterfaceNumber;
274                         if (i >= nintf_orig) {
275                                 dev_err(ddev, "config %d has an invalid "
276                                     "interface number: %d but max is %d\n",
277                                     cfgno, i, nintf_orig - 1);
278                                 return -EINVAL;
279                         }
280                         if (i < nintf)
281                                 ++config->interface[i]->num_altsetting;
282
283                 } else if (header->bDescriptorType == USB_DT_DEVICE ||
284                             header->bDescriptorType == USB_DT_CONFIG) {
285                         dev_err(ddev, "config %d contains an unexpected "
286                             "descriptor of type 0x%X\n",
287                             cfgno, header->bDescriptorType);
288                         return -EINVAL;
289                 }
290
291         }       /* for ((buffer2 = buffer, size2 = size); ...) */
292
293         /* Allocate the altsetting arrays */
294         for (i = 0; i < nintf; ++i) {
295                 interface = config->interface[i];
296                 if (interface->num_altsetting > USB_MAXALTSETTING) {
297                         dev_err(ddev, "too many alternate settings for "
298                             "config %d interface %d: %d, "
299                             "maximum allowed: %d\n",
300                             cfgno, i, interface->num_altsetting,
301                             USB_MAXALTSETTING);
302                         return -EINVAL;
303                 }
304                 if (interface->num_altsetting == 0) {
305                         dev_err(ddev, "config %d has no interface number "
306                             "%d\n", cfgno, i);
307                         return -EINVAL;
308                 }
309
310                 len = sizeof(*interface->altsetting) *
311                     interface->num_altsetting;
312                 interface->altsetting = kmalloc(len, GFP_KERNEL);
313                 if (!interface->altsetting)
314                         return -ENOMEM;
315                 memset(interface->altsetting, 0, len);
316         }
317
318         /* Skip over any Class Specific or Vendor Specific descriptors;
319          * find the first interface descriptor */
320         config->extra = buffer;
321         i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
322             USB_DT_INTERFACE, &n);
323         config->extralen = i;
324         if (n > 0)
325                 dev_dbg(ddev, "skipped %d class/vendor specific "
326                     "configuration descriptors\n", n);
327         buffer += i;
328         size -= i;
329
330         /* Parse all the interface/altsetting descriptors */
331         while (size >= sizeof(struct usb_descriptor_header)) {
332                 retval = usb_parse_interface(ddev, cfgno, config,
333                     buffer, size);
334                 if (retval < 0)
335                         return retval;
336
337                 buffer += retval;
338                 size -= retval;
339         }
340
341         /* Check for missing altsettings */
342         for (i = 0; i < nintf; ++i) {
343                 interface = config->interface[i];
344                 for (j = 0; j < interface->num_altsetting; ++j) {
345                         if (!interface->altsetting[j].desc.bLength) {
346                                 dev_err(ddev, "config %d interface %d has no "
347                                     "altsetting %d\n", cfgno, i, j);
348                                 return -EINVAL;
349                         }
350                 }
351         }
352
353         return size;
354 }
355
356 // hub-only!! ... and only exported for reset/reinit path.
357 // otherwise used internally on disconnect/destroy path
358 void usb_destroy_configuration(struct usb_device *dev)
359 {
360         int c, i;
361
362         if (!dev->config)
363                 return;
364
365         if (dev->rawdescriptors) {
366                 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
367                         kfree(dev->rawdescriptors[i]);
368
369                 kfree(dev->rawdescriptors);
370                 dev->rawdescriptors = 0;
371         }
372
373         for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
374                 struct usb_host_config *cf = &dev->config[c];
375
376                 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
377                         struct usb_interface *ifp = cf->interface[i];
378
379                         if (ifp)
380                                 usb_free_intf(ifp);
381                 }
382         }
383         kfree(dev->config);
384         dev->config = 0;
385 }
386
387
388 // hub-only!! ... and only in reset path, or usb_new_device()
389 // (used by real hubs and virtual root hubs)
390 int usb_get_configuration(struct usb_device *dev)
391 {
392         struct device *ddev = &dev->dev;
393         int ncfg = dev->descriptor.bNumConfigurations;
394         int result = -ENOMEM;
395         unsigned int cfgno, length;
396         unsigned char *buffer;
397         unsigned char *bigbuffer;
398         struct usb_config_descriptor *desc;
399
400         if (ncfg > USB_MAXCONFIG) {
401                 dev_warn(ddev, "too many configurations: %d, "
402                     "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
403                 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
404         }
405
406         if (ncfg < 1) {
407                 dev_err(ddev, "no configurations\n");
408                 return -EINVAL;
409         }
410
411         length = ncfg * sizeof(struct usb_host_config);
412         dev->config = kmalloc(length, GFP_KERNEL);
413         if (!dev->config)
414                 goto err2;
415         memset(dev->config, 0, length);
416
417         length = ncfg * sizeof(char *);
418         dev->rawdescriptors = kmalloc(length, GFP_KERNEL);
419         if (!dev->rawdescriptors)
420                 goto err2;
421         memset(dev->rawdescriptors, 0, length);
422
423         buffer = kmalloc(8, GFP_KERNEL);
424         if (!buffer)
425                 goto err2;
426         desc = (struct usb_config_descriptor *)buffer;
427
428         for (cfgno = 0; cfgno < ncfg; cfgno++) {
429                 /* We grab the first 8 bytes so we know how long the whole */
430                 /* configuration is */
431                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
432                     buffer, 8);
433                 if (result < 0) {
434                         dev_err(ddev, "unable to read config index %d "
435                             "descriptor\n", cfgno);
436                         goto err;
437                 } else if (result < 8) {
438                         dev_err(ddev, "config index %d descriptor too short "
439                             "(expected %i, got %i)\n", cfgno, 8, result);
440                         result = -EINVAL;
441                         goto err;
442                 }
443                 length = max((int) le16_to_cpu(desc->wTotalLength),
444                     USB_DT_CONFIG_SIZE);
445
446                 /* Now that we know the length, get the whole thing */
447                 bigbuffer = kmalloc(length, GFP_KERNEL);
448                 if (!bigbuffer) {
449                         result = -ENOMEM;
450                         goto err;
451                 }
452                 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
453                     bigbuffer, length);
454                 if (result < 0) {
455                         dev_err(ddev, "unable to read config index %d "
456                             "descriptor\n", cfgno);
457                         kfree(bigbuffer);
458                         goto err;
459                 }
460                 if (result < length) {
461                         dev_err(ddev, "config index %d descriptor too short "
462                             "(expected %i, got %i)\n", cfgno, length, result);
463                         result = -EINVAL;
464                         kfree(bigbuffer);
465                         goto err;
466                 }
467
468                 dev->rawdescriptors[cfgno] = bigbuffer;
469
470                 result = usb_parse_configuration(&dev->dev, cfgno,
471                     &dev->config[cfgno], bigbuffer, length);
472                 if (result > 0)
473                         dev_dbg(ddev, "config index %d descriptor has %d "
474                             "excess byte(s)\n", cfgno, result);
475                 else if (result < 0) {
476                         ++cfgno;
477                         goto err;
478                 }
479         }
480         result = 0;
481
482 err:
483         kfree(buffer);
484         dev->descriptor.bNumConfigurations = cfgno;
485 err2:
486         if (result == -ENOMEM)
487                 dev_err(ddev, "out of memory\n");
488         return result;
489 }