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