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>
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
;
27 unsigned char *buffer0
= buffer
;
29 /* Find the next descriptor of type dt1 or dt2 */
31 h
= (struct usb_descriptor_header
*) buffer
;
32 if (h
->bDescriptorType
== dt1
|| h
->bDescriptorType
== dt2
)
39 /* Store the number of descriptors skipped and return the
40 * number of bytes skipped */
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
;
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
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
;
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: "
103 usb_endpoint_xfer_control(&ep
->desc
) ? "Control" : "Bulk",
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: "
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: "
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);
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: "
137 usb_endpoint_xfer_isoc(&ep
->desc
) ? "Isoc" : "Int",
138 le16_to_cpu(desc
->wBytesPerInterval
),
139 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
,
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
;
181 const unsigned short *maxpacket_maxes
;
183 d
= (struct usb_endpoint_descriptor
*) buffer
;
184 buffer
+= 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
;
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 */
232 if (usb_endpoint_xfer_int(d
)) {
234 switch (to_usb_device(ddev
)->speed
) {
235 case USB_SPEED_SUPER_PLUS
:
236 case USB_SPEED_SUPER
:
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);
246 n
= 7; /* 8 ms = 2^(7-1) uframes */
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
);
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
);
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.
279 } else if (usb_endpoint_xfer_isoc(d
)) {
282 switch (to_usb_device(ddev
)->speed
) {
284 n
= 7; /* 8 ms = 2^(7-1) uframes */
286 default: /* USB_SPEED_FULL */
287 n
= 4; /* 8 ms = 2^(4-1) frames */
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, "
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
) {
322 maxpacket_maxes
= low_speed_maxpacket_maxes
;
325 maxpacket_maxes
= full_speed_maxpacket_maxes
;
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));
335 maxpacket_maxes
= high_speed_maxpacket_maxes
;
337 case USB_SPEED_SUPER
:
338 case USB_SPEED_SUPER_PLUS
:
339 maxpacket_maxes
= super_speed_maxpacket_maxes
;
342 j
= maxpacket_maxes
[usb_endpoint_type(&endpoint
->desc
)];
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
);
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
)) {
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
,
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
;
378 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
379 n
, plural(n
), "endpoint");
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
);
393 for (j
= 0; j
< intfc
->num_altsetting
; j
++) {
394 struct usb_host_interface
*alt
= &intfc
->altsetting
[j
];
396 kfree(alt
->endpoint
);
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
;
409 struct usb_interface_cache
*intfc
;
410 struct usb_host_interface
*alt
;
413 int num_ep
, num_ep_orig
;
415 d
= (struct usb_interface_descriptor
*) buffer
;
416 buffer
+= d
->bLength
;
419 if (d
->bLength
< USB_DT_INTERFACE_SIZE
)
420 goto skip_to_next_interface_descriptor
;
422 /* Which interface entry is this? */
424 inum
= d
->bInterfaceNumber
;
425 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
426 if (inums
[i
] == inum
) {
427 intfc
= config
->intf_cache
[i
];
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
;
439 if (alt
->desc
.bAlternateSetting
== asnum
) {
440 dev_warn(ddev
, "Duplicate descriptor for config %d "
441 "interface %d altsetting %d, skipping\n",
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 */
453 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
454 USB_DT_INTERFACE
, &n
);
457 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
458 n
, plural(n
), "interface");
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
;
473 /* Can't allocate 0 bytes */
474 len
= sizeof(struct usb_host_endpoint
) * num_ep
;
475 alt
->endpoint
= kzalloc(len
, GFP_KERNEL
);
480 /* Parse all the endpoint descriptors */
483 if (((struct usb_descriptor_header
*) buffer
)->bDescriptorType
486 retval
= usb_parse_endpoint(ddev
, cfgno
, inum
, asnum
, alt
,
487 num_ep
, buffer
, size
);
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
;
515 int nintf
, nintf_orig
;
517 struct usb_interface_cache
*intfc
;
518 unsigned char *buffer2
;
520 struct usb_descriptor_header
*header
;
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
);
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 */
552 for ((buffer2
= buffer
, size2
= size
);
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
));
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
);
571 if (header
->bDescriptorType
== USB_DT_INTERFACE
) {
572 struct usb_interface_descriptor
*d
;
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
);
583 inum
= d
->bInterfaceNumber
;
585 if ((dev
->quirks
& USB_QUIRK_HONOR_BNUMINTERFACES
) &&
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
);
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
)
608 } else if (n
< USB_MAXINTERFACES
) {
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
) {
621 "config %d has an invalid interface association descriptor of length %d, skipping\n",
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
);
632 config
->intf_assoc
[iad_num
] = d
;
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
);
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
);
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
) {
661 dev_warn(ddev
, "config %d has no interface number "
665 /* Allocate the usb_interface_caches and altsetting arrays */
666 for (i
= 0; i
< nintf
; ++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
);
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
;
692 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
693 n
, plural(n
), "configuration");
697 /* Parse all the interface/altsetting descriptors */
699 retval
= usb_parse_interface(ddev
, cfgno
, config
,
700 buffer
, size
, inums
, nalts
);
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
)
717 if (n
>= intfc
->num_altsetting
)
718 dev_warn(ddev
, "config %d interface %d has no "
719 "altsetting %d\n", cfgno
, inums
[i
], j
);
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
)
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
];
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
);
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
;
770 unsigned int cfgno
, length
;
771 unsigned char *bigbuffer
;
772 struct usb_config_descriptor
*desc
;
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
;
783 dev_err(ddev
, "no configurations\n");
787 length
= ncfg
* sizeof(struct usb_host_config
);
788 dev
->config
= kzalloc(length
, GFP_KERNEL
);
792 length
= ncfg
* sizeof(char *);
793 dev
->rawdescriptors
= kzalloc(length
, GFP_KERNEL
);
794 if (!dev
->rawdescriptors
)
797 desc
= kmalloc(USB_DT_CONFIG_SIZE
, GFP_KERNEL
);
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
);
808 dev_err(ddev
, "unable to read config index %d "
809 "descriptor/%s: %d\n", cfgno
, "start", result
);
810 if (result
!= -EPIPE
)
812 dev_err(ddev
, "chopping to %d config(s)\n", cfgno
);
813 dev
->descriptor
.bNumConfigurations
= cfgno
;
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
);
822 length
= max((int) le16_to_cpu(desc
->wTotalLength
),
825 /* Now that we know the length, get the whole thing */
826 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
832 if (dev
->quirks
& USB_QUIRK_DELAY_INIT
)
835 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
838 dev_err(ddev
, "unable to read config index %d "
839 "descriptor/%s\n", cfgno
, "all");
843 if (result
< length
) {
844 dev_warn(ddev
, "config index %d descriptor too short "
845 "(expected %i, got %i)\n", cfgno
, length
, result
);
849 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
851 result
= usb_parse_configuration(dev
, cfgno
,
852 &dev
->config
[cfgno
], bigbuffer
, length
);
862 dev
->descriptor
.bNumConfigurations
= cfgno
;
864 if (result
== -ENOMEM
)
865 dev_err(ddev
, "out of memory\n");
869 void usb_release_bos_descriptor(struct usb_device
*dev
)
872 kfree(dev
->bos
->desc
);
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
;
899 bos
= kzalloc(sizeof(struct usb_bos_descriptor
), GFP_KERNEL
);
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");
913 length
= bos
->bLength
;
914 total_len
= le16_to_cpu(bos
->wTotalLength
);
915 num
= bos
->bNumDeviceCaps
;
917 if (total_len
< length
)
920 dev
->bos
= kzalloc(sizeof(struct usb_host_bos
), GFP_KERNEL
);
924 /* Now let's get the whole BOS descriptor set */
925 buffer
= kzalloc(total_len
, GFP_KERNEL
);
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");
941 for (i
= 0; i
< num
; i
++) {
943 cap
= (struct usb_dev_cap_header
*)buffer
;
945 if (total_len
< sizeof(*cap
) || total_len
< cap
->bLength
) {
946 dev
->bos
->desc
->bNumDeviceCaps
= i
;
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
;
958 if (cap
->bDescriptorType
!= USB_DT_DEVICE_CAPABILITY
) {
959 dev_warn(ddev
, "descriptor type invalid, skip\n");
964 case USB_CAP_TYPE_WIRELESS_USB
:
965 /* Wireless USB cap descriptor is handled by wusb */
967 case USB_CAP_TYPE_EXT
:
969 (struct usb_ext_cap_descriptor
*)buffer
;
971 case USB_SS_CAP_TYPE
:
973 (struct usb_ss_cap_descriptor
*)buffer
;
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
;
982 case CONTAINER_ID_TYPE
:
984 (struct usb_ss_container_id_descriptor
*)buffer
;
986 case USB_PTM_CAP_TYPE
:
988 (struct usb_ptm_cap_descriptor
*)buffer
;
997 usb_release_bos_descriptor(dev
);