2 #include <linux/usb/ch9.h>
3 #include <linux/module.h>
4 #include <linux/init.h>
5 #include <linux/slab.h>
6 #include <linux/device.h>
7 #include <asm/byteorder.h>
11 #define USB_MAXALTSETTING 128 /* Hard limit */
12 #define USB_MAXENDPOINTS 30 /* Hard limit */
14 #define USB_MAXCONFIG 8 /* Arbitrary limit */
17 static inline const char *plural(int n
)
19 return (n
== 1 ? "" : "s");
22 /* FIXME: this is a kludge */
23 static int find_next_descriptor_more(unsigned char *buffer
, int size
,
24 int dt1
, int dt2
, int dt3
, int *num_skipped
)
26 struct usb_descriptor_header
*h
;
28 unsigned char *buffer0
= buffer
;
30 /* Find the next descriptor of type dt1 or dt2 or dt3 */
32 h
= (struct usb_descriptor_header
*) buffer
;
33 if (h
->bDescriptorType
== dt1
|| h
->bDescriptorType
== dt2
||
34 h
->bDescriptorType
== dt3
)
41 /* Store the number of descriptors skipped and return the
42 * number of bytes skipped */
45 return buffer
- buffer0
;
48 static int find_next_descriptor(unsigned char *buffer
, int size
,
49 int dt1
, int dt2
, int *num_skipped
)
51 struct usb_descriptor_header
*h
;
53 unsigned char *buffer0
= buffer
;
55 /* Find the next descriptor of type dt1 or dt2 */
57 h
= (struct usb_descriptor_header
*) buffer
;
58 if (h
->bDescriptorType
== dt1
|| h
->bDescriptorType
== dt2
)
65 /* Store the number of descriptors skipped and return the
66 * number of bytes skipped */
69 return buffer
- buffer0
;
72 static int usb_parse_ss_endpoint_companion(struct device
*ddev
, int cfgno
,
73 int inum
, int asnum
, struct usb_host_endpoint
*ep
,
74 int num_ep
, unsigned char *buffer
, int size
)
76 unsigned char *buffer_start
= buffer
;
77 struct usb_ss_ep_comp_descriptor
*desc
;
83 desc
= (struct usb_ss_ep_comp_descriptor
*) buffer
;
84 if (desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
) {
85 dev_warn(ddev
, "No SuperSpeed endpoint companion for config %d "
86 " interface %d altsetting %d ep %d: "
87 "using minimum values\n",
88 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
90 * The next descriptor is for an Endpoint or Interface,
91 * no extra descriptors to copy into the companion structure,
92 * and we didn't eat up any of the buffer.
96 memcpy(&ep
->ss_ep_comp
->desc
, desc
, USB_DT_SS_EP_COMP_SIZE
);
97 desc
= &ep
->ss_ep_comp
->desc
;
98 buffer
+= desc
->bLength
;
99 size
-= desc
->bLength
;
101 /* Eat up the other descriptors we don't care about */
102 ep
->ss_ep_comp
->extra
= buffer
;
103 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
104 USB_DT_INTERFACE
, &num_skipped
);
105 ep
->ss_ep_comp
->extralen
= i
;
108 retval
= buffer
- buffer_start
+ i
;
110 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
111 num_skipped
, plural(num_skipped
),
112 "SuperSpeed endpoint companion");
114 /* Check the various values */
115 if (usb_endpoint_xfer_control(&ep
->desc
) && desc
->bMaxBurst
!= 0) {
116 dev_warn(ddev
, "Control endpoint with bMaxBurst = %d in "
117 "config %d interface %d altsetting %d ep %d: "
118 "setting to zero\n", desc
->bMaxBurst
,
119 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
122 if (desc
->bMaxBurst
> 15) {
123 dev_warn(ddev
, "Endpoint with bMaxBurst = %d in "
124 "config %d interface %d altsetting %d ep %d: "
125 "setting to 15\n", desc
->bMaxBurst
,
126 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
127 desc
->bMaxBurst
= 15;
129 if ((usb_endpoint_xfer_control(&ep
->desc
) || usb_endpoint_xfer_int(&ep
->desc
))
130 && desc
->bmAttributes
!= 0) {
131 dev_warn(ddev
, "%s endpoint with bmAttributes = %d in "
132 "config %d interface %d altsetting %d ep %d: "
134 usb_endpoint_xfer_control(&ep
->desc
) ? "Control" : "Bulk",
136 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
137 desc
->bmAttributes
= 0;
139 if (usb_endpoint_xfer_bulk(&ep
->desc
) && desc
->bmAttributes
> 16) {
140 dev_warn(ddev
, "Bulk endpoint with more than 65536 streams in "
141 "config %d interface %d altsetting %d ep %d: "
143 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
144 desc
->bmAttributes
= 16;
146 if (usb_endpoint_xfer_isoc(&ep
->desc
) && desc
->bmAttributes
> 2) {
147 dev_warn(ddev
, "Isoc endpoint has Mult of %d in "
148 "config %d interface %d altsetting %d ep %d: "
149 "setting to 3\n", desc
->bmAttributes
+ 1,
150 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
151 desc
->bmAttributes
= 2;
153 if (usb_endpoint_xfer_isoc(&ep
->desc
)) {
154 max_tx
= ep
->desc
.wMaxPacketSize
* (desc
->bMaxBurst
+ 1) *
155 (desc
->bmAttributes
+ 1);
156 } else if (usb_endpoint_xfer_int(&ep
->desc
)) {
157 max_tx
= ep
->desc
.wMaxPacketSize
* (desc
->bMaxBurst
+ 1);
161 if (desc
->wBytesPerInterval
> max_tx
) {
162 dev_warn(ddev
, "%s endpoint with wBytesPerInterval of %d in "
163 "config %d interface %d altsetting %d ep %d: "
165 usb_endpoint_xfer_isoc(&ep
->desc
) ? "Isoc" : "Int",
166 desc
->wBytesPerInterval
,
167 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
,
169 desc
->wBytesPerInterval
= max_tx
;
175 static int usb_parse_endpoint(struct device
*ddev
, int cfgno
, int inum
,
176 int asnum
, struct usb_host_interface
*ifp
, int num_ep
,
177 unsigned char *buffer
, int size
)
179 unsigned char *buffer0
= buffer
;
180 struct usb_endpoint_descriptor
*d
;
181 struct usb_host_endpoint
*endpoint
;
184 d
= (struct usb_endpoint_descriptor
*) buffer
;
185 buffer
+= d
->bLength
;
188 if (d
->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
)
189 n
= USB_DT_ENDPOINT_AUDIO_SIZE
;
190 else if (d
->bLength
>= USB_DT_ENDPOINT_SIZE
)
191 n
= USB_DT_ENDPOINT_SIZE
;
193 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
194 "invalid endpoint descriptor of length %d, skipping\n",
195 cfgno
, inum
, asnum
, d
->bLength
);
196 goto skip_to_next_endpoint_or_interface_descriptor
;
199 i
= d
->bEndpointAddress
& ~USB_ENDPOINT_DIR_MASK
;
200 if (i
>= 16 || i
== 0) {
201 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
202 "invalid endpoint with address 0x%X, skipping\n",
203 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
204 goto skip_to_next_endpoint_or_interface_descriptor
;
207 /* Only store as many endpoints as we have room for */
208 if (ifp
->desc
.bNumEndpoints
>= num_ep
)
209 goto skip_to_next_endpoint_or_interface_descriptor
;
211 endpoint
= &ifp
->endpoint
[ifp
->desc
.bNumEndpoints
];
212 ++ifp
->desc
.bNumEndpoints
;
214 memcpy(&endpoint
->desc
, d
, n
);
215 INIT_LIST_HEAD(&endpoint
->urb_list
);
217 /* Fix up bInterval values outside the legal range. Use 32 ms if no
218 * proper value can be guessed. */
219 i
= 0; /* i = min, j = max, n = default */
221 if (usb_endpoint_xfer_int(d
)) {
223 switch (to_usb_device(ddev
)->speed
) {
224 case USB_SPEED_SUPER
:
226 /* Many device manufacturers are using full-speed
227 * bInterval values in high-speed interrupt endpoint
228 * descriptors. Try to fix those and fall back to a
229 * 32 ms default value otherwise. */
230 n
= fls(d
->bInterval
*8);
232 n
= 9; /* 32 ms = 2^(9-1) uframes */
235 default: /* USB_SPEED_FULL or _LOW */
236 /* For low-speed, 10 ms is the official minimum.
237 * But some "overclocked" devices might want faster
238 * polling so we'll allow it. */
242 } else if (usb_endpoint_xfer_isoc(d
)) {
245 switch (to_usb_device(ddev
)->speed
) {
247 n
= 9; /* 32 ms = 2^(9-1) uframes */
249 default: /* USB_SPEED_FULL */
250 n
= 6; /* 32 ms = 2^(6-1) frames */
254 if (d
->bInterval
< i
|| d
->bInterval
> j
) {
255 dev_warn(ddev
, "config %d interface %d altsetting %d "
256 "endpoint 0x%X has an invalid bInterval %d, "
259 d
->bEndpointAddress
, d
->bInterval
, n
);
260 endpoint
->desc
.bInterval
= n
;
263 /* Some buggy low-speed devices have Bulk endpoints, which is
264 * explicitly forbidden by the USB spec. In an attempt to make
265 * them usable, we will try treating them as Interrupt endpoints.
267 if (to_usb_device(ddev
)->speed
== USB_SPEED_LOW
&&
268 usb_endpoint_xfer_bulk(d
)) {
269 dev_warn(ddev
, "config %d interface %d altsetting %d "
270 "endpoint 0x%X is Bulk; changing to Interrupt\n",
271 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
272 endpoint
->desc
.bmAttributes
= USB_ENDPOINT_XFER_INT
;
273 endpoint
->desc
.bInterval
= 1;
274 if (le16_to_cpu(endpoint
->desc
.wMaxPacketSize
) > 8)
275 endpoint
->desc
.wMaxPacketSize
= cpu_to_le16(8);
279 * Some buggy high speed devices have bulk endpoints using
280 * maxpacket sizes other than 512. High speed HCDs may not
281 * be able to handle that particular bug, so let's warn...
283 if (to_usb_device(ddev
)->speed
== USB_SPEED_HIGH
284 && usb_endpoint_xfer_bulk(d
)) {
287 maxp
= le16_to_cpu(endpoint
->desc
.wMaxPacketSize
) & 0x07ff;
289 dev_warn(ddev
, "config %d interface %d altsetting %d "
290 "bulk endpoint 0x%X has invalid maxpacket %d\n",
291 cfgno
, inum
, asnum
, d
->bEndpointAddress
,
294 /* Allocate room for and parse any SS endpoint companion descriptors */
295 if (to_usb_device(ddev
)->speed
== USB_SPEED_SUPER
) {
296 endpoint
->extra
= buffer
;
297 i
= find_next_descriptor_more(buffer
, size
, USB_DT_SS_ENDPOINT_COMP
,
298 USB_DT_ENDPOINT
, USB_DT_INTERFACE
, &n
);
299 endpoint
->extralen
= i
;
303 /* Allocate space for the SS endpoint companion descriptor */
304 endpoint
->ss_ep_comp
= kzalloc(sizeof(struct usb_host_ss_ep_comp
),
306 if (!endpoint
->ss_ep_comp
)
309 /* Fill in some default values (may be overwritten later) */
310 endpoint
->ss_ep_comp
->desc
.bLength
= USB_DT_SS_EP_COMP_SIZE
;
311 endpoint
->ss_ep_comp
->desc
.bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
;
312 endpoint
->ss_ep_comp
->desc
.bMaxBurst
= 0;
314 * Leave bmAttributes as zero, which will mean no streams for
315 * bulk, and isoc won't support multiple bursts of packets.
316 * With bursts of only one packet, and a Mult of 1, the max
317 * amount of data moved per endpoint service interval is one
320 if (usb_endpoint_xfer_isoc(&endpoint
->desc
) ||
321 usb_endpoint_xfer_int(&endpoint
->desc
))
322 endpoint
->ss_ep_comp
->desc
.wBytesPerInterval
=
323 endpoint
->desc
.wMaxPacketSize
;
326 retval
= usb_parse_ss_endpoint_companion(ddev
, cfgno
,
327 inum
, asnum
, endpoint
, num_ep
, buffer
,
331 retval
= buffer
- buffer0
;
334 dev_warn(ddev
, "config %d interface %d altsetting %d "
335 "endpoint 0x%X has no "
336 "SuperSpeed companion descriptor\n",
337 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
338 retval
= buffer
- buffer0
;
341 /* Skip over any Class Specific or Vendor Specific descriptors;
342 * find the next endpoint or interface descriptor */
343 endpoint
->extra
= buffer
;
344 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
345 USB_DT_INTERFACE
, &n
);
346 endpoint
->extralen
= i
;
347 retval
= buffer
- buffer0
+ i
;
350 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
351 n
, plural(n
), "endpoint");
354 skip_to_next_endpoint_or_interface_descriptor
:
355 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
356 USB_DT_INTERFACE
, NULL
);
357 return buffer
- buffer0
+ i
;
360 void usb_release_interface_cache(struct kref
*ref
)
362 struct usb_interface_cache
*intfc
= ref_to_usb_interface_cache(ref
);
365 for (j
= 0; j
< intfc
->num_altsetting
; j
++) {
366 struct usb_host_interface
*alt
= &intfc
->altsetting
[j
];
368 kfree(alt
->endpoint
);
374 static int usb_parse_interface(struct device
*ddev
, int cfgno
,
375 struct usb_host_config
*config
, unsigned char *buffer
, int size
,
376 u8 inums
[], u8 nalts
[])
378 unsigned char *buffer0
= buffer
;
379 struct usb_interface_descriptor
*d
;
381 struct usb_interface_cache
*intfc
;
382 struct usb_host_interface
*alt
;
385 int num_ep
, num_ep_orig
;
387 d
= (struct usb_interface_descriptor
*) buffer
;
388 buffer
+= d
->bLength
;
391 if (d
->bLength
< USB_DT_INTERFACE_SIZE
)
392 goto skip_to_next_interface_descriptor
;
394 /* Which interface entry is this? */
396 inum
= d
->bInterfaceNumber
;
397 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
398 if (inums
[i
] == inum
) {
399 intfc
= config
->intf_cache
[i
];
403 if (!intfc
|| intfc
->num_altsetting
>= nalts
[i
])
404 goto skip_to_next_interface_descriptor
;
406 /* Check for duplicate altsetting entries */
407 asnum
= d
->bAlternateSetting
;
408 for ((i
= 0, alt
= &intfc
->altsetting
[0]);
409 i
< intfc
->num_altsetting
;
411 if (alt
->desc
.bAlternateSetting
== asnum
) {
412 dev_warn(ddev
, "Duplicate descriptor for config %d "
413 "interface %d altsetting %d, skipping\n",
415 goto skip_to_next_interface_descriptor
;
419 ++intfc
->num_altsetting
;
420 memcpy(&alt
->desc
, d
, USB_DT_INTERFACE_SIZE
);
422 /* Skip over any Class Specific or Vendor Specific descriptors;
423 * find the first endpoint or interface descriptor */
425 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
426 USB_DT_INTERFACE
, &n
);
429 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
430 n
, plural(n
), "interface");
434 /* Allocate space for the right(?) number of endpoints */
435 num_ep
= num_ep_orig
= alt
->desc
.bNumEndpoints
;
436 alt
->desc
.bNumEndpoints
= 0; /* Use as a counter */
437 if (num_ep
> USB_MAXENDPOINTS
) {
438 dev_warn(ddev
, "too many endpoints for config %d interface %d "
439 "altsetting %d: %d, using maximum allowed: %d\n",
440 cfgno
, inum
, asnum
, num_ep
, USB_MAXENDPOINTS
);
441 num_ep
= USB_MAXENDPOINTS
;
445 /* Can't allocate 0 bytes */
446 len
= sizeof(struct usb_host_endpoint
) * num_ep
;
447 alt
->endpoint
= kzalloc(len
, GFP_KERNEL
);
452 /* Parse all the endpoint descriptors */
455 if (((struct usb_descriptor_header
*) buffer
)->bDescriptorType
458 retval
= usb_parse_endpoint(ddev
, cfgno
, inum
, asnum
, alt
,
459 num_ep
, buffer
, size
);
468 if (n
!= num_ep_orig
)
469 dev_warn(ddev
, "config %d interface %d altsetting %d has %d "
470 "endpoint descriptor%s, different from the interface "
471 "descriptor's value: %d\n",
472 cfgno
, inum
, asnum
, n
, plural(n
), num_ep_orig
);
473 return buffer
- buffer0
;
475 skip_to_next_interface_descriptor
:
476 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
477 USB_DT_INTERFACE
, NULL
);
478 return buffer
- buffer0
+ i
;
481 static int usb_parse_configuration(struct device
*ddev
, int cfgidx
,
482 struct usb_host_config
*config
, unsigned char *buffer
, int size
)
484 unsigned char *buffer0
= buffer
;
486 int nintf
, nintf_orig
;
488 struct usb_interface_cache
*intfc
;
489 unsigned char *buffer2
;
491 struct usb_descriptor_header
*header
;
493 u8 inums
[USB_MAXINTERFACES
], nalts
[USB_MAXINTERFACES
];
494 unsigned iad_num
= 0;
496 memcpy(&config
->desc
, buffer
, USB_DT_CONFIG_SIZE
);
497 if (config
->desc
.bDescriptorType
!= USB_DT_CONFIG
||
498 config
->desc
.bLength
< USB_DT_CONFIG_SIZE
) {
499 dev_err(ddev
, "invalid descriptor for config index %d: "
500 "type = 0x%X, length = %d\n", cfgidx
,
501 config
->desc
.bDescriptorType
, config
->desc
.bLength
);
504 cfgno
= config
->desc
.bConfigurationValue
;
506 buffer
+= config
->desc
.bLength
;
507 size
-= config
->desc
.bLength
;
509 nintf
= nintf_orig
= config
->desc
.bNumInterfaces
;
510 if (nintf
> USB_MAXINTERFACES
) {
511 dev_warn(ddev
, "config %d has too many interfaces: %d, "
512 "using maximum allowed: %d\n",
513 cfgno
, nintf
, USB_MAXINTERFACES
);
514 nintf
= USB_MAXINTERFACES
;
517 /* Go through the descriptors, checking their length and counting the
518 * number of altsettings for each interface */
520 for ((buffer2
= buffer
, size2
= size
);
522 (buffer2
+= header
->bLength
, size2
-= header
->bLength
)) {
524 if (size2
< sizeof(struct usb_descriptor_header
)) {
525 dev_warn(ddev
, "config %d descriptor has %d excess "
526 "byte%s, ignoring\n",
527 cfgno
, size2
, plural(size2
));
531 header
= (struct usb_descriptor_header
*) buffer2
;
532 if ((header
->bLength
> size2
) || (header
->bLength
< 2)) {
533 dev_warn(ddev
, "config %d has an invalid descriptor "
534 "of length %d, skipping remainder of the config\n",
535 cfgno
, header
->bLength
);
539 if (header
->bDescriptorType
== USB_DT_INTERFACE
) {
540 struct usb_interface_descriptor
*d
;
543 d
= (struct usb_interface_descriptor
*) header
;
544 if (d
->bLength
< USB_DT_INTERFACE_SIZE
) {
545 dev_warn(ddev
, "config %d has an invalid "
546 "interface descriptor of length %d, "
547 "skipping\n", cfgno
, d
->bLength
);
551 inum
= d
->bInterfaceNumber
;
552 if (inum
>= nintf_orig
)
553 dev_warn(ddev
, "config %d has an invalid "
554 "interface number: %d but max is %d\n",
555 cfgno
, inum
, nintf_orig
- 1);
557 /* Have we already encountered this interface?
558 * Count its altsettings */
559 for (i
= 0; i
< n
; ++i
) {
560 if (inums
[i
] == inum
)
566 } else if (n
< USB_MAXINTERFACES
) {
572 } else if (header
->bDescriptorType
==
573 USB_DT_INTERFACE_ASSOCIATION
) {
574 if (iad_num
== USB_MAXIADS
) {
575 dev_warn(ddev
, "found more Interface "
576 "Association Descriptors "
577 "than allocated for in "
578 "configuration %d\n", cfgno
);
580 config
->intf_assoc
[iad_num
] =
581 (struct usb_interface_assoc_descriptor
586 } else if (header
->bDescriptorType
== USB_DT_DEVICE
||
587 header
->bDescriptorType
== USB_DT_CONFIG
)
588 dev_warn(ddev
, "config %d contains an unexpected "
589 "descriptor of type 0x%X, skipping\n",
590 cfgno
, header
->bDescriptorType
);
592 } /* for ((buffer2 = buffer, size2 = size); ...) */
593 size
= buffer2
- buffer
;
594 config
->desc
.wTotalLength
= cpu_to_le16(buffer2
- buffer0
);
597 dev_warn(ddev
, "config %d has %d interface%s, different from "
598 "the descriptor's value: %d\n",
599 cfgno
, n
, plural(n
), nintf_orig
);
601 dev_warn(ddev
, "config %d has no interfaces?\n", cfgno
);
602 config
->desc
.bNumInterfaces
= nintf
= n
;
604 /* Check for missing interface numbers */
605 for (i
= 0; i
< nintf
; ++i
) {
606 for (j
= 0; j
< nintf
; ++j
) {
611 dev_warn(ddev
, "config %d has no interface number "
615 /* Allocate the usb_interface_caches and altsetting arrays */
616 for (i
= 0; i
< nintf
; ++i
) {
618 if (j
> USB_MAXALTSETTING
) {
619 dev_warn(ddev
, "too many alternate settings for "
620 "config %d interface %d: %d, "
621 "using maximum allowed: %d\n",
622 cfgno
, inums
[i
], j
, USB_MAXALTSETTING
);
623 nalts
[i
] = j
= USB_MAXALTSETTING
;
626 len
= sizeof(*intfc
) + sizeof(struct usb_host_interface
) * j
;
627 config
->intf_cache
[i
] = intfc
= kzalloc(len
, GFP_KERNEL
);
630 kref_init(&intfc
->ref
);
633 /* FIXME: parse the BOS descriptor */
635 /* Skip over any Class Specific or Vendor Specific descriptors;
636 * find the first interface descriptor */
637 config
->extra
= buffer
;
638 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
639 USB_DT_INTERFACE
, &n
);
640 config
->extralen
= i
;
642 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
643 n
, plural(n
), "configuration");
647 /* Parse all the interface/altsetting descriptors */
649 retval
= usb_parse_interface(ddev
, cfgno
, config
,
650 buffer
, size
, inums
, nalts
);
658 /* Check for missing altsettings */
659 for (i
= 0; i
< nintf
; ++i
) {
660 intfc
= config
->intf_cache
[i
];
661 for (j
= 0; j
< intfc
->num_altsetting
; ++j
) {
662 for (n
= 0; n
< intfc
->num_altsetting
; ++n
) {
663 if (intfc
->altsetting
[n
].desc
.
664 bAlternateSetting
== j
)
667 if (n
>= intfc
->num_altsetting
)
668 dev_warn(ddev
, "config %d interface %d has no "
669 "altsetting %d\n", cfgno
, inums
[i
], j
);
676 /* hub-only!! ... and only exported for reset/reinit path.
677 * otherwise used internally on disconnect/destroy path
679 void usb_destroy_configuration(struct usb_device
*dev
)
686 if (dev
->rawdescriptors
) {
687 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++)
688 kfree(dev
->rawdescriptors
[i
]);
690 kfree(dev
->rawdescriptors
);
691 dev
->rawdescriptors
= NULL
;
694 for (c
= 0; c
< dev
->descriptor
.bNumConfigurations
; c
++) {
695 struct usb_host_config
*cf
= &dev
->config
[c
];
698 for (i
= 0; i
< cf
->desc
.bNumInterfaces
; i
++) {
699 if (cf
->intf_cache
[i
])
700 kref_put(&cf
->intf_cache
[i
]->ref
,
701 usb_release_interface_cache
);
710 * Get the USB config descriptors, cache and parse'em
712 * hub-only!! ... and only in reset path, or usb_new_device()
713 * (used by real hubs and virtual root hubs)
715 * NOTE: if this is a WUSB device and is not authorized, we skip the
716 * whole thing. A non-authorized USB device has no
719 int usb_get_configuration(struct usb_device
*dev
)
721 struct device
*ddev
= &dev
->dev
;
722 int ncfg
= dev
->descriptor
.bNumConfigurations
;
724 unsigned int cfgno
, length
;
725 unsigned char *buffer
;
726 unsigned char *bigbuffer
;
727 struct usb_config_descriptor
*desc
;
730 if (dev
->authorized
== 0) /* Not really an error */
731 goto out_not_authorized
;
733 if (ncfg
> USB_MAXCONFIG
) {
734 dev_warn(ddev
, "too many configurations: %d, "
735 "using maximum allowed: %d\n", ncfg
, USB_MAXCONFIG
);
736 dev
->descriptor
.bNumConfigurations
= ncfg
= USB_MAXCONFIG
;
740 dev_err(ddev
, "no configurations\n");
744 length
= ncfg
* sizeof(struct usb_host_config
);
745 dev
->config
= kzalloc(length
, GFP_KERNEL
);
749 length
= ncfg
* sizeof(char *);
750 dev
->rawdescriptors
= kzalloc(length
, GFP_KERNEL
);
751 if (!dev
->rawdescriptors
)
754 buffer
= kmalloc(USB_DT_CONFIG_SIZE
, GFP_KERNEL
);
757 desc
= (struct usb_config_descriptor
*)buffer
;
760 for (; cfgno
< ncfg
; cfgno
++) {
761 /* We grab just the first descriptor so we know how long
762 * the whole configuration is */
763 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
764 buffer
, USB_DT_CONFIG_SIZE
);
766 dev_err(ddev
, "unable to read config index %d "
767 "descriptor/%s: %d\n", cfgno
, "start", result
);
768 dev_err(ddev
, "chopping to %d config(s)\n", cfgno
);
769 dev
->descriptor
.bNumConfigurations
= cfgno
;
771 } else if (result
< 4) {
772 dev_err(ddev
, "config index %d descriptor too short "
773 "(expected %i, got %i)\n", cfgno
,
774 USB_DT_CONFIG_SIZE
, result
);
778 length
= max((int) le16_to_cpu(desc
->wTotalLength
),
781 /* Now that we know the length, get the whole thing */
782 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
787 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
790 dev_err(ddev
, "unable to read config index %d "
791 "descriptor/%s\n", cfgno
, "all");
795 if (result
< length
) {
796 dev_warn(ddev
, "config index %d descriptor too short "
797 "(expected %i, got %i)\n", cfgno
, length
, result
);
801 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
803 result
= usb_parse_configuration(&dev
->dev
, cfgno
,
804 &dev
->config
[cfgno
], bigbuffer
, length
);
815 dev
->descriptor
.bNumConfigurations
= cfgno
;
817 if (result
== -ENOMEM
)
818 dev_err(ddev
, "out of memory\n");