dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / usb / core / config.c
blob6a287c81a7be7050248317974b7fe5f4295ce070
1 #include <linux/usb.h>
2 #include <linux/usb/ch9.h>
3 #include <linux/usb/hcd.h>
4 #include <linux/usb/quirks.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
7 #include <linux/device.h>
8 #include <asm/byteorder.h>
9 #include "usb.h"
12 #define USB_MAXALTSETTING 128 /* 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;
26 int n = 0;
27 unsigned char *buffer0 = buffer;
29 /* Find the next descriptor of type dt1 or dt2 */
30 while (size > 0) {
31 h = (struct usb_descriptor_header *) buffer;
32 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
33 break;
34 buffer += h->bLength;
35 size -= h->bLength;
36 ++n;
39 /* Store the number of descriptors skipped and return the
40 * number of bytes skipped */
41 if (num_skipped)
42 *num_skipped = n;
43 return buffer - buffer0;
46 static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
47 int inum, int asnum, struct usb_host_endpoint *ep,
48 unsigned char *buffer, int size)
50 struct usb_ss_ep_comp_descriptor *desc;
51 int max_tx;
53 /* The SuperSpeed endpoint companion descriptor is supposed to
54 * be the first thing immediately following the endpoint descriptor.
56 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
57 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
58 size < USB_DT_SS_EP_COMP_SIZE) {
59 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
60 " interface %d altsetting %d ep %d: "
61 "using minimum values\n",
62 cfgno, inum, asnum, ep->desc.bEndpointAddress);
64 /* Fill in some default values.
65 * Leave bmAttributes as zero, which will mean no streams for
66 * bulk, and isoc won't support multiple bursts of packets.
67 * With bursts of only one packet, and a Mult of 1, the max
68 * amount of data moved per endpoint service interval is one
69 * packet.
71 ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
72 ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
73 if (usb_endpoint_xfer_isoc(&ep->desc) ||
74 usb_endpoint_xfer_int(&ep->desc))
75 ep->ss_ep_comp.wBytesPerInterval =
76 ep->desc.wMaxPacketSize;
77 return;
80 memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
82 /* Check the various values */
83 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
84 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
85 "config %d interface %d altsetting %d ep %d: "
86 "setting to zero\n", desc->bMaxBurst,
87 cfgno, inum, asnum, ep->desc.bEndpointAddress);
88 ep->ss_ep_comp.bMaxBurst = 0;
89 } else if (desc->bMaxBurst > 15) {
90 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
91 "config %d interface %d altsetting %d ep %d: "
92 "setting to 15\n", desc->bMaxBurst,
93 cfgno, inum, asnum, ep->desc.bEndpointAddress);
94 ep->ss_ep_comp.bMaxBurst = 15;
97 if ((usb_endpoint_xfer_control(&ep->desc) ||
98 usb_endpoint_xfer_int(&ep->desc)) &&
99 desc->bmAttributes != 0) {
100 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
101 "config %d interface %d altsetting %d ep %d: "
102 "setting to zero\n",
103 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
104 desc->bmAttributes,
105 cfgno, inum, asnum, ep->desc.bEndpointAddress);
106 ep->ss_ep_comp.bmAttributes = 0;
107 } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
108 desc->bmAttributes > 16) {
109 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
110 "config %d interface %d altsetting %d ep %d: "
111 "setting to max\n",
112 cfgno, inum, asnum, ep->desc.bEndpointAddress);
113 ep->ss_ep_comp.bmAttributes = 16;
114 } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
115 USB_SS_MULT(desc->bmAttributes) > 3) {
116 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
117 "config %d interface %d altsetting %d ep %d: "
118 "setting to 3\n",
119 USB_SS_MULT(desc->bmAttributes),
120 cfgno, inum, asnum, ep->desc.bEndpointAddress);
121 ep->ss_ep_comp.bmAttributes = 2;
124 if (usb_endpoint_xfer_isoc(&ep->desc))
125 max_tx = (desc->bMaxBurst + 1) *
126 (USB_SS_MULT(desc->bmAttributes)) *
127 usb_endpoint_maxp(&ep->desc);
128 else if (usb_endpoint_xfer_int(&ep->desc))
129 max_tx = usb_endpoint_maxp(&ep->desc) *
130 (desc->bMaxBurst + 1);
131 else
132 max_tx = 999999;
133 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
134 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
135 "config %d interface %d altsetting %d ep %d: "
136 "setting to %d\n",
137 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
138 le16_to_cpu(desc->wBytesPerInterval),
139 cfgno, inum, asnum, ep->desc.bEndpointAddress,
140 max_tx);
141 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
145 static const unsigned short low_speed_maxpacket_maxes[4] = {
146 [USB_ENDPOINT_XFER_CONTROL] = 8,
147 [USB_ENDPOINT_XFER_ISOC] = 0,
148 [USB_ENDPOINT_XFER_BULK] = 0,
149 [USB_ENDPOINT_XFER_INT] = 8,
151 static const unsigned short full_speed_maxpacket_maxes[4] = {
152 [USB_ENDPOINT_XFER_CONTROL] = 64,
153 [USB_ENDPOINT_XFER_ISOC] = 1023,
154 [USB_ENDPOINT_XFER_BULK] = 64,
155 [USB_ENDPOINT_XFER_INT] = 64,
157 static const unsigned short high_speed_maxpacket_maxes[4] = {
158 [USB_ENDPOINT_XFER_CONTROL] = 64,
159 [USB_ENDPOINT_XFER_ISOC] = 1024,
161 /* Bulk should be 512, but some devices use 1024: we will warn below */
162 [USB_ENDPOINT_XFER_BULK] = 1024,
163 [USB_ENDPOINT_XFER_INT] = 1024,
165 static const unsigned short super_speed_maxpacket_maxes[4] = {
166 [USB_ENDPOINT_XFER_CONTROL] = 512,
167 [USB_ENDPOINT_XFER_ISOC] = 1024,
168 [USB_ENDPOINT_XFER_BULK] = 1024,
169 [USB_ENDPOINT_XFER_INT] = 1024,
172 static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
173 int asnum, struct usb_host_interface *ifp, int num_ep,
174 unsigned char *buffer, int size)
176 unsigned char *buffer0 = buffer;
177 struct usb_endpoint_descriptor *d;
178 struct usb_host_endpoint *endpoint;
179 int n, i, j, retval;
180 unsigned int maxp;
181 const unsigned short *maxpacket_maxes;
183 d = (struct usb_endpoint_descriptor *) buffer;
184 buffer += d->bLength;
185 size -= d->bLength;
187 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
188 n = USB_DT_ENDPOINT_AUDIO_SIZE;
189 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
190 n = USB_DT_ENDPOINT_SIZE;
191 else {
192 dev_warn(ddev, "config %d interface %d altsetting %d has an "
193 "invalid endpoint descriptor of length %d, skipping\n",
194 cfgno, inum, asnum, d->bLength);
195 goto skip_to_next_endpoint_or_interface_descriptor;
198 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
199 if (i >= 16 || i == 0) {
200 dev_warn(ddev, "config %d interface %d altsetting %d has an "
201 "invalid endpoint with address 0x%X, skipping\n",
202 cfgno, inum, asnum, d->bEndpointAddress);
203 goto skip_to_next_endpoint_or_interface_descriptor;
206 /* Only store as many endpoints as we have room for */
207 if (ifp->desc.bNumEndpoints >= num_ep)
208 goto skip_to_next_endpoint_or_interface_descriptor;
210 /* Check for duplicate endpoint addresses */
211 for (i = 0; i < ifp->desc.bNumEndpoints; ++i) {
212 if (ifp->endpoint[i].desc.bEndpointAddress ==
213 d->bEndpointAddress) {
214 dev_warn(ddev, "config %d interface %d altsetting %d has a duplicate endpoint with address 0x%X, skipping\n",
215 cfgno, inum, asnum, d->bEndpointAddress);
216 goto skip_to_next_endpoint_or_interface_descriptor;
220 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
221 ++ifp->desc.bNumEndpoints;
223 memcpy(&endpoint->desc, d, n);
224 INIT_LIST_HEAD(&endpoint->urb_list);
227 * Fix up bInterval values outside the legal range.
228 * Use 10 or 8 ms if no proper value can be guessed.
230 i = 0; /* i = min, j = max, n = default */
231 j = 255;
232 if (usb_endpoint_xfer_int(d)) {
233 i = 1;
234 switch (to_usb_device(ddev)->speed) {
235 case USB_SPEED_SUPER_PLUS:
236 case USB_SPEED_SUPER:
237 case USB_SPEED_HIGH:
239 * Many device manufacturers are using full-speed
240 * bInterval values in high-speed interrupt endpoint
241 * descriptors. Try to fix those and fall back to an
242 * 8-ms default value otherwise.
244 n = fls(d->bInterval*8);
245 if (n == 0)
246 n = 7; /* 8 ms = 2^(7-1) uframes */
247 j = 16;
250 * Adjust bInterval for quirked devices.
253 * This quirk fixes bIntervals reported in ms.
255 if (to_usb_device(ddev)->quirks &
256 USB_QUIRK_LINEAR_FRAME_INTR_BINTERVAL) {
257 n = clamp(fls(d->bInterval) + 3, i, j);
258 i = j = n;
261 * This quirk fixes bIntervals reported in
262 * linear microframes.
264 if (to_usb_device(ddev)->quirks &
265 USB_QUIRK_LINEAR_UFRAME_INTR_BINTERVAL) {
266 n = clamp(fls(d->bInterval), i, j);
267 i = j = n;
269 break;
270 default: /* USB_SPEED_FULL or _LOW */
272 * For low-speed, 10 ms is the official minimum.
273 * But some "overclocked" devices might want faster
274 * polling so we'll allow it.
276 n = 10;
277 break;
279 } else if (usb_endpoint_xfer_isoc(d)) {
280 i = 1;
281 j = 16;
282 switch (to_usb_device(ddev)->speed) {
283 case USB_SPEED_HIGH:
284 n = 7; /* 8 ms = 2^(7-1) uframes */
285 break;
286 default: /* USB_SPEED_FULL */
287 n = 4; /* 8 ms = 2^(4-1) frames */
288 break;
291 if (d->bInterval < i || d->bInterval > j) {
292 dev_warn(ddev, "config %d interface %d altsetting %d "
293 "endpoint 0x%X has an invalid bInterval %d, "
294 "changing to %d\n",
295 cfgno, inum, asnum,
296 d->bEndpointAddress, d->bInterval, n);
297 endpoint->desc.bInterval = n;
300 /* Some buggy low-speed devices have Bulk endpoints, which is
301 * explicitly forbidden by the USB spec. In an attempt to make
302 * them usable, we will try treating them as Interrupt endpoints.
304 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
305 usb_endpoint_xfer_bulk(d)) {
306 dev_warn(ddev, "config %d interface %d altsetting %d "
307 "endpoint 0x%X is Bulk; changing to Interrupt\n",
308 cfgno, inum, asnum, d->bEndpointAddress);
309 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
310 endpoint->desc.bInterval = 1;
311 if (usb_endpoint_maxp(&endpoint->desc) > 8)
312 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
315 /* Validate the wMaxPacketSize field */
316 maxp = usb_endpoint_maxp(&endpoint->desc);
318 /* Find the highest legal maxpacket size for this endpoint */
319 i = 0; /* additional transactions per microframe */
320 switch (to_usb_device(ddev)->speed) {
321 case USB_SPEED_LOW:
322 maxpacket_maxes = low_speed_maxpacket_maxes;
323 break;
324 case USB_SPEED_FULL:
325 maxpacket_maxes = full_speed_maxpacket_maxes;
326 break;
327 case USB_SPEED_HIGH:
328 /* Bits 12..11 are allowed only for HS periodic endpoints */
329 if (usb_endpoint_xfer_int(d) || usb_endpoint_xfer_isoc(d)) {
330 i = maxp & (BIT(12) | BIT(11));
331 maxp &= ~i;
333 /* fallthrough */
334 default:
335 maxpacket_maxes = high_speed_maxpacket_maxes;
336 break;
337 case USB_SPEED_SUPER:
338 case USB_SPEED_SUPER_PLUS:
339 maxpacket_maxes = super_speed_maxpacket_maxes;
340 break;
342 j = maxpacket_maxes[usb_endpoint_type(&endpoint->desc)];
344 if (maxp > j) {
345 dev_warn(ddev, "config %d interface %d altsetting %d endpoint 0x%X has invalid maxpacket %d, setting to %d\n",
346 cfgno, inum, asnum, d->bEndpointAddress, maxp, j);
347 maxp = j;
348 endpoint->desc.wMaxPacketSize = cpu_to_le16(i | maxp);
352 * Some buggy high speed devices have bulk endpoints using
353 * maxpacket sizes other than 512. High speed HCDs may not
354 * be able to handle that particular bug, so let's warn...
356 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
357 && usb_endpoint_xfer_bulk(d)) {
358 if (maxp != 512)
359 dev_warn(ddev, "config %d interface %d altsetting %d "
360 "bulk endpoint 0x%X has invalid maxpacket %d\n",
361 cfgno, inum, asnum, d->bEndpointAddress,
362 maxp);
365 /* Parse a possible SuperSpeed endpoint companion descriptor */
366 if (to_usb_device(ddev)->speed >= USB_SPEED_SUPER)
367 usb_parse_ss_endpoint_companion(ddev, cfgno,
368 inum, asnum, endpoint, buffer, size);
370 /* Skip over any Class Specific or Vendor Specific descriptors;
371 * find the next endpoint or interface descriptor */
372 endpoint->extra = buffer;
373 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
374 USB_DT_INTERFACE, &n);
375 endpoint->extralen = i;
376 retval = buffer - buffer0 + i;
377 if (n > 0)
378 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
379 n, plural(n), "endpoint");
380 return retval;
382 skip_to_next_endpoint_or_interface_descriptor:
383 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
384 USB_DT_INTERFACE, NULL);
385 return buffer - buffer0 + i;
388 void usb_release_interface_cache(struct kref *ref)
390 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
391 int j;
393 for (j = 0; j < intfc->num_altsetting; j++) {
394 struct usb_host_interface *alt = &intfc->altsetting[j];
396 kfree(alt->endpoint);
397 kfree(alt->string);
399 kfree(intfc);
402 static int usb_parse_interface(struct device *ddev, int cfgno,
403 struct usb_host_config *config, unsigned char *buffer, int size,
404 u8 inums[], u8 nalts[])
406 unsigned char *buffer0 = buffer;
407 struct usb_interface_descriptor *d;
408 int inum, asnum;
409 struct usb_interface_cache *intfc;
410 struct usb_host_interface *alt;
411 int i, n;
412 int len, retval;
413 int num_ep, num_ep_orig;
415 d = (struct usb_interface_descriptor *) buffer;
416 buffer += d->bLength;
417 size -= d->bLength;
419 if (d->bLength < USB_DT_INTERFACE_SIZE)
420 goto skip_to_next_interface_descriptor;
422 /* Which interface entry is this? */
423 intfc = NULL;
424 inum = d->bInterfaceNumber;
425 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
426 if (inums[i] == inum) {
427 intfc = config->intf_cache[i];
428 break;
431 if (!intfc || intfc->num_altsetting >= nalts[i])
432 goto skip_to_next_interface_descriptor;
434 /* Check for duplicate altsetting entries */
435 asnum = d->bAlternateSetting;
436 for ((i = 0, alt = &intfc->altsetting[0]);
437 i < intfc->num_altsetting;
438 (++i, ++alt)) {
439 if (alt->desc.bAlternateSetting == asnum) {
440 dev_warn(ddev, "Duplicate descriptor for config %d "
441 "interface %d altsetting %d, skipping\n",
442 cfgno, inum, asnum);
443 goto skip_to_next_interface_descriptor;
447 ++intfc->num_altsetting;
448 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
450 /* Skip over any Class Specific or Vendor Specific descriptors;
451 * find the first endpoint or interface descriptor */
452 alt->extra = buffer;
453 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
454 USB_DT_INTERFACE, &n);
455 alt->extralen = i;
456 if (n > 0)
457 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
458 n, plural(n), "interface");
459 buffer += i;
460 size -= i;
462 /* Allocate space for the right(?) number of endpoints */
463 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
464 alt->desc.bNumEndpoints = 0; /* Use as a counter */
465 if (num_ep > USB_MAXENDPOINTS) {
466 dev_warn(ddev, "too many endpoints for config %d interface %d "
467 "altsetting %d: %d, using maximum allowed: %d\n",
468 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
469 num_ep = USB_MAXENDPOINTS;
472 if (num_ep > 0) {
473 /* Can't allocate 0 bytes */
474 len = sizeof(struct usb_host_endpoint) * num_ep;
475 alt->endpoint = kzalloc(len, GFP_KERNEL);
476 if (!alt->endpoint)
477 return -ENOMEM;
480 /* Parse all the endpoint descriptors */
481 n = 0;
482 while (size > 0) {
483 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
484 == USB_DT_INTERFACE)
485 break;
486 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
487 num_ep, buffer, size);
488 if (retval < 0)
489 return retval;
490 ++n;
492 buffer += retval;
493 size -= retval;
496 if (n != num_ep_orig)
497 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
498 "endpoint descriptor%s, different from the interface "
499 "descriptor's value: %d\n",
500 cfgno, inum, asnum, n, plural(n), num_ep_orig);
501 return buffer - buffer0;
503 skip_to_next_interface_descriptor:
504 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
505 USB_DT_INTERFACE, NULL);
506 return buffer - buffer0 + i;
509 static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
510 struct usb_host_config *config, unsigned char *buffer, int size)
512 struct device *ddev = &dev->dev;
513 unsigned char *buffer0 = buffer;
514 int cfgno;
515 int nintf, nintf_orig;
516 int i, j, n;
517 struct usb_interface_cache *intfc;
518 unsigned char *buffer2;
519 int size2;
520 struct usb_descriptor_header *header;
521 int len, retval;
522 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
523 unsigned iad_num = 0;
525 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
526 nintf = nintf_orig = config->desc.bNumInterfaces;
527 config->desc.bNumInterfaces = 0; // Adjusted later
529 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
530 config->desc.bLength < USB_DT_CONFIG_SIZE ||
531 config->desc.bLength > size) {
532 dev_err(ddev, "invalid descriptor for config index %d: "
533 "type = 0x%X, length = %d\n", cfgidx,
534 config->desc.bDescriptorType, config->desc.bLength);
535 return -EINVAL;
537 cfgno = config->desc.bConfigurationValue;
539 buffer += config->desc.bLength;
540 size -= config->desc.bLength;
542 if (nintf > USB_MAXINTERFACES) {
543 dev_warn(ddev, "config %d has too many interfaces: %d, "
544 "using maximum allowed: %d\n",
545 cfgno, nintf, USB_MAXINTERFACES);
546 nintf = USB_MAXINTERFACES;
549 /* Go through the descriptors, checking their length and counting the
550 * number of altsettings for each interface */
551 n = 0;
552 for ((buffer2 = buffer, size2 = size);
553 size2 > 0;
554 (buffer2 += header->bLength, size2 -= header->bLength)) {
556 if (size2 < sizeof(struct usb_descriptor_header)) {
557 dev_warn(ddev, "config %d descriptor has %d excess "
558 "byte%s, ignoring\n",
559 cfgno, size2, plural(size2));
560 break;
563 header = (struct usb_descriptor_header *) buffer2;
564 if ((header->bLength > size2) || (header->bLength < 2)) {
565 dev_warn(ddev, "config %d has an invalid descriptor "
566 "of length %d, skipping remainder of the config\n",
567 cfgno, header->bLength);
568 break;
571 if (header->bDescriptorType == USB_DT_INTERFACE) {
572 struct usb_interface_descriptor *d;
573 int inum;
575 d = (struct usb_interface_descriptor *) header;
576 if (d->bLength < USB_DT_INTERFACE_SIZE) {
577 dev_warn(ddev, "config %d has an invalid "
578 "interface descriptor of length %d, "
579 "skipping\n", cfgno, d->bLength);
580 continue;
583 inum = d->bInterfaceNumber;
585 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
586 n >= nintf_orig) {
587 dev_warn(ddev, "config %d has more interface "
588 "descriptors, than it declares in "
589 "bNumInterfaces, ignoring interface "
590 "number: %d\n", cfgno, inum);
591 continue;
594 if (inum >= nintf_orig)
595 dev_warn(ddev, "config %d has an invalid "
596 "interface number: %d but max is %d\n",
597 cfgno, inum, nintf_orig - 1);
599 /* Have we already encountered this interface?
600 * Count its altsettings */
601 for (i = 0; i < n; ++i) {
602 if (inums[i] == inum)
603 break;
605 if (i < n) {
606 if (nalts[i] < 255)
607 ++nalts[i];
608 } else if (n < USB_MAXINTERFACES) {
609 inums[n] = inum;
610 nalts[n] = 1;
611 ++n;
614 } else if (header->bDescriptorType ==
615 USB_DT_INTERFACE_ASSOCIATION) {
616 struct usb_interface_assoc_descriptor *d;
618 d = (struct usb_interface_assoc_descriptor *)header;
619 if (d->bLength < USB_DT_INTERFACE_ASSOCIATION_SIZE) {
620 dev_warn(ddev,
621 "config %d has an invalid interface association descriptor of length %d, skipping\n",
622 cfgno, d->bLength);
623 continue;
626 if (iad_num == USB_MAXIADS) {
627 dev_warn(ddev, "found more Interface "
628 "Association Descriptors "
629 "than allocated for in "
630 "configuration %d\n", cfgno);
631 } else {
632 config->intf_assoc[iad_num] = d;
633 iad_num++;
636 } else if (header->bDescriptorType == USB_DT_DEVICE ||
637 header->bDescriptorType == USB_DT_CONFIG)
638 dev_warn(ddev, "config %d contains an unexpected "
639 "descriptor of type 0x%X, skipping\n",
640 cfgno, header->bDescriptorType);
642 } /* for ((buffer2 = buffer, size2 = size); ...) */
643 size = buffer2 - buffer;
644 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
646 if (n != nintf)
647 dev_warn(ddev, "config %d has %d interface%s, different from "
648 "the descriptor's value: %d\n",
649 cfgno, n, plural(n), nintf_orig);
650 else if (n == 0)
651 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
652 config->desc.bNumInterfaces = nintf = n;
654 /* Check for missing interface numbers */
655 for (i = 0; i < nintf; ++i) {
656 for (j = 0; j < nintf; ++j) {
657 if (inums[j] == i)
658 break;
660 if (j >= nintf)
661 dev_warn(ddev, "config %d has no interface number "
662 "%d\n", cfgno, i);
665 /* Allocate the usb_interface_caches and altsetting arrays */
666 for (i = 0; i < nintf; ++i) {
667 j = nalts[i];
668 if (j > USB_MAXALTSETTING) {
669 dev_warn(ddev, "too many alternate settings for "
670 "config %d interface %d: %d, "
671 "using maximum allowed: %d\n",
672 cfgno, inums[i], j, USB_MAXALTSETTING);
673 nalts[i] = j = USB_MAXALTSETTING;
676 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
677 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
678 if (!intfc)
679 return -ENOMEM;
680 kref_init(&intfc->ref);
683 /* FIXME: parse the BOS descriptor */
685 /* Skip over any Class Specific or Vendor Specific descriptors;
686 * find the first interface descriptor */
687 config->extra = buffer;
688 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
689 USB_DT_INTERFACE, &n);
690 config->extralen = i;
691 if (n > 0)
692 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
693 n, plural(n), "configuration");
694 buffer += i;
695 size -= i;
697 /* Parse all the interface/altsetting descriptors */
698 while (size > 0) {
699 retval = usb_parse_interface(ddev, cfgno, config,
700 buffer, size, inums, nalts);
701 if (retval < 0)
702 return retval;
704 buffer += retval;
705 size -= retval;
708 /* Check for missing altsettings */
709 for (i = 0; i < nintf; ++i) {
710 intfc = config->intf_cache[i];
711 for (j = 0; j < intfc->num_altsetting; ++j) {
712 for (n = 0; n < intfc->num_altsetting; ++n) {
713 if (intfc->altsetting[n].desc.
714 bAlternateSetting == j)
715 break;
717 if (n >= intfc->num_altsetting)
718 dev_warn(ddev, "config %d interface %d has no "
719 "altsetting %d\n", cfgno, inums[i], j);
723 return 0;
726 /* hub-only!! ... and only exported for reset/reinit path.
727 * otherwise used internally on disconnect/destroy path
729 void usb_destroy_configuration(struct usb_device *dev)
731 int c, i;
733 if (!dev->config)
734 return;
736 if (dev->rawdescriptors) {
737 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
738 kfree(dev->rawdescriptors[i]);
740 kfree(dev->rawdescriptors);
741 dev->rawdescriptors = NULL;
744 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
745 struct usb_host_config *cf = &dev->config[c];
747 kfree(cf->string);
748 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
749 if (cf->intf_cache[i])
750 kref_put(&cf->intf_cache[i]->ref,
751 usb_release_interface_cache);
754 kfree(dev->config);
755 dev->config = NULL;
760 * Get the USB config descriptors, cache and parse'em
762 * hub-only!! ... and only in reset path, or usb_new_device()
763 * (used by real hubs and virtual root hubs)
765 int usb_get_configuration(struct usb_device *dev)
767 struct device *ddev = &dev->dev;
768 int ncfg = dev->descriptor.bNumConfigurations;
769 int result = 0;
770 unsigned int cfgno, length;
771 unsigned char *bigbuffer;
772 struct usb_config_descriptor *desc;
774 cfgno = 0;
775 result = -ENOMEM;
776 if (ncfg > USB_MAXCONFIG) {
777 dev_warn(ddev, "too many configurations: %d, "
778 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
779 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
782 if (ncfg < 1) {
783 dev_err(ddev, "no configurations\n");
784 return -EINVAL;
787 length = ncfg * sizeof(struct usb_host_config);
788 dev->config = kzalloc(length, GFP_KERNEL);
789 if (!dev->config)
790 goto err2;
792 length = ncfg * sizeof(char *);
793 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
794 if (!dev->rawdescriptors)
795 goto err2;
797 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
798 if (!desc)
799 goto err2;
801 result = 0;
802 for (; cfgno < ncfg; cfgno++) {
803 /* We grab just the first descriptor so we know how long
804 * the whole configuration is */
805 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
806 desc, USB_DT_CONFIG_SIZE);
807 if (result < 0) {
808 dev_err(ddev, "unable to read config index %d "
809 "descriptor/%s: %d\n", cfgno, "start", result);
810 if (result != -EPIPE)
811 goto err;
812 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
813 dev->descriptor.bNumConfigurations = cfgno;
814 break;
815 } else if (result < 4) {
816 dev_err(ddev, "config index %d descriptor too short "
817 "(expected %i, got %i)\n", cfgno,
818 USB_DT_CONFIG_SIZE, result);
819 result = -EINVAL;
820 goto err;
822 length = max((int) le16_to_cpu(desc->wTotalLength),
823 USB_DT_CONFIG_SIZE);
825 /* Now that we know the length, get the whole thing */
826 bigbuffer = kmalloc(length, GFP_KERNEL);
827 if (!bigbuffer) {
828 result = -ENOMEM;
829 goto err;
832 if (dev->quirks & USB_QUIRK_DELAY_INIT)
833 msleep(200);
835 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
836 bigbuffer, length);
837 if (result < 0) {
838 dev_err(ddev, "unable to read config index %d "
839 "descriptor/%s\n", cfgno, "all");
840 kfree(bigbuffer);
841 goto err;
843 if (result < length) {
844 dev_warn(ddev, "config index %d descriptor too short "
845 "(expected %i, got %i)\n", cfgno, length, result);
846 length = result;
849 dev->rawdescriptors[cfgno] = bigbuffer;
851 result = usb_parse_configuration(dev, cfgno,
852 &dev->config[cfgno], bigbuffer, length);
853 if (result < 0) {
854 ++cfgno;
855 goto err;
858 result = 0;
860 err:
861 kfree(desc);
862 dev->descriptor.bNumConfigurations = cfgno;
863 err2:
864 if (result == -ENOMEM)
865 dev_err(ddev, "out of memory\n");
866 return result;
869 void usb_release_bos_descriptor(struct usb_device *dev)
871 if (dev->bos) {
872 kfree(dev->bos->desc);
873 kfree(dev->bos);
874 dev->bos = NULL;
878 static const __u8 bos_desc_len[256] = {
879 [USB_CAP_TYPE_WIRELESS_USB] = USB_DT_USB_WIRELESS_CAP_SIZE,
880 [USB_CAP_TYPE_EXT] = USB_DT_USB_EXT_CAP_SIZE,
881 [USB_SS_CAP_TYPE] = USB_DT_USB_SS_CAP_SIZE,
882 [USB_SSP_CAP_TYPE] = USB_DT_USB_SSP_CAP_SIZE(1),
883 [CONTAINER_ID_TYPE] = USB_DT_USB_SS_CONTN_ID_SIZE,
884 [USB_PTM_CAP_TYPE] = USB_DT_USB_PTM_ID_SIZE,
887 /* Get BOS descriptor set */
888 int usb_get_bos_descriptor(struct usb_device *dev)
890 struct device *ddev = &dev->dev;
891 struct usb_bos_descriptor *bos;
892 struct usb_dev_cap_header *cap;
893 struct usb_ssp_cap_descriptor *ssp_cap;
894 unsigned char *buffer;
895 int length, total_len, num, i, ssac;
896 __u8 cap_type;
897 int ret;
899 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
900 if (!bos)
901 return -ENOMEM;
903 /* Get BOS descriptor */
904 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
905 if (ret < USB_DT_BOS_SIZE) {
906 dev_err(ddev, "unable to get BOS descriptor\n");
907 if (ret >= 0)
908 ret = -ENOMSG;
909 kfree(bos);
910 return ret;
913 length = bos->bLength;
914 total_len = le16_to_cpu(bos->wTotalLength);
915 num = bos->bNumDeviceCaps;
916 kfree(bos);
917 if (total_len < length)
918 return -EINVAL;
920 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
921 if (!dev->bos)
922 return -ENOMEM;
924 /* Now let's get the whole BOS descriptor set */
925 buffer = kzalloc(total_len, GFP_KERNEL);
926 if (!buffer) {
927 ret = -ENOMEM;
928 goto err;
930 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
932 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
933 if (ret < total_len) {
934 dev_err(ddev, "unable to get BOS descriptor set\n");
935 if (ret >= 0)
936 ret = -ENOMSG;
937 goto err;
939 total_len -= length;
941 for (i = 0; i < num; i++) {
942 buffer += length;
943 cap = (struct usb_dev_cap_header *)buffer;
945 if (total_len < sizeof(*cap) || total_len < cap->bLength) {
946 dev->bos->desc->bNumDeviceCaps = i;
947 break;
949 cap_type = cap->bDevCapabilityType;
950 length = cap->bLength;
951 if (bos_desc_len[cap_type] && length < bos_desc_len[cap_type]) {
952 dev->bos->desc->bNumDeviceCaps = i;
953 break;
956 total_len -= length;
958 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
959 dev_warn(ddev, "descriptor type invalid, skip\n");
960 continue;
963 switch (cap_type) {
964 case USB_CAP_TYPE_WIRELESS_USB:
965 /* Wireless USB cap descriptor is handled by wusb */
966 break;
967 case USB_CAP_TYPE_EXT:
968 dev->bos->ext_cap =
969 (struct usb_ext_cap_descriptor *)buffer;
970 break;
971 case USB_SS_CAP_TYPE:
972 dev->bos->ss_cap =
973 (struct usb_ss_cap_descriptor *)buffer;
974 break;
975 case USB_SSP_CAP_TYPE:
976 ssp_cap = (struct usb_ssp_cap_descriptor *)buffer;
977 ssac = (le32_to_cpu(ssp_cap->bmAttributes) &
978 USB_SSP_SUBLINK_SPEED_ATTRIBS);
979 if (length >= USB_DT_USB_SSP_CAP_SIZE(ssac))
980 dev->bos->ssp_cap = ssp_cap;
981 break;
982 case CONTAINER_ID_TYPE:
983 dev->bos->ss_id =
984 (struct usb_ss_container_id_descriptor *)buffer;
985 break;
986 case USB_PTM_CAP_TYPE:
987 dev->bos->ptm_cap =
988 (struct usb_ptm_cap_descriptor *)buffer;
989 default:
990 break;
994 return 0;
996 err:
997 usb_release_bos_descriptor(dev);
998 return ret;