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 /* Allocate space for the SS endpoint companion descriptor */
84 ep
->ss_ep_comp
= kzalloc(sizeof(struct usb_host_ss_ep_comp
),
88 desc
= (struct usb_ss_ep_comp_descriptor
*) buffer
;
89 if (desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
) {
90 dev_warn(ddev
, "No SuperSpeed endpoint companion for config %d "
91 " interface %d altsetting %d ep %d: "
92 "using minimum values\n",
93 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
94 ep
->ss_ep_comp
->desc
.bLength
= USB_DT_SS_EP_COMP_SIZE
;
95 ep
->ss_ep_comp
->desc
.bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
;
96 ep
->ss_ep_comp
->desc
.bMaxBurst
= 0;
98 * Leave bmAttributes as zero, which will mean no streams for
99 * bulk, and isoc won't support multiple bursts of packets.
100 * With bursts of only one packet, and a Mult of 1, the max
101 * amount of data moved per endpoint service interval is one
104 if (usb_endpoint_xfer_isoc(&ep
->desc
) ||
105 usb_endpoint_xfer_int(&ep
->desc
))
106 ep
->ss_ep_comp
->desc
.wBytesPerInterval
=
107 ep
->desc
.wMaxPacketSize
;
109 * The next descriptor is for an Endpoint or Interface,
110 * no extra descriptors to copy into the companion structure,
111 * and we didn't eat up any of the buffer.
116 memcpy(&ep
->ss_ep_comp
->desc
, desc
, USB_DT_SS_EP_COMP_SIZE
);
117 desc
= &ep
->ss_ep_comp
->desc
;
118 buffer
+= desc
->bLength
;
119 size
-= desc
->bLength
;
121 /* Eat up the other descriptors we don't care about */
122 ep
->ss_ep_comp
->extra
= buffer
;
123 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
124 USB_DT_INTERFACE
, &num_skipped
);
125 ep
->ss_ep_comp
->extralen
= i
;
128 retval
= buffer
- buffer_start
+ i
;
130 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
131 num_skipped
, plural(num_skipped
),
132 "SuperSpeed endpoint companion");
134 /* Check the various values */
135 if (usb_endpoint_xfer_control(&ep
->desc
) && desc
->bMaxBurst
!= 0) {
136 dev_warn(ddev
, "Control endpoint with bMaxBurst = %d in "
137 "config %d interface %d altsetting %d ep %d: "
138 "setting to zero\n", desc
->bMaxBurst
,
139 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
142 if (desc
->bMaxBurst
> 15) {
143 dev_warn(ddev
, "Endpoint with bMaxBurst = %d in "
144 "config %d interface %d altsetting %d ep %d: "
145 "setting to 15\n", desc
->bMaxBurst
,
146 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
147 desc
->bMaxBurst
= 15;
149 if ((usb_endpoint_xfer_control(&ep
->desc
) || usb_endpoint_xfer_int(&ep
->desc
))
150 && desc
->bmAttributes
!= 0) {
151 dev_warn(ddev
, "%s endpoint with bmAttributes = %d in "
152 "config %d interface %d altsetting %d ep %d: "
154 usb_endpoint_xfer_control(&ep
->desc
) ? "Control" : "Bulk",
156 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
157 desc
->bmAttributes
= 0;
159 if (usb_endpoint_xfer_bulk(&ep
->desc
) && desc
->bmAttributes
> 16) {
160 dev_warn(ddev
, "Bulk endpoint with more than 65536 streams in "
161 "config %d interface %d altsetting %d ep %d: "
163 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
164 desc
->bmAttributes
= 16;
166 if (usb_endpoint_xfer_isoc(&ep
->desc
) && desc
->bmAttributes
> 2) {
167 dev_warn(ddev
, "Isoc endpoint has Mult of %d in "
168 "config %d interface %d altsetting %d ep %d: "
169 "setting to 3\n", desc
->bmAttributes
+ 1,
170 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
);
171 desc
->bmAttributes
= 2;
173 if (usb_endpoint_xfer_isoc(&ep
->desc
)) {
174 max_tx
= ep
->desc
.wMaxPacketSize
* (desc
->bMaxBurst
+ 1) *
175 (desc
->bmAttributes
+ 1);
176 } else if (usb_endpoint_xfer_int(&ep
->desc
)) {
177 max_tx
= ep
->desc
.wMaxPacketSize
* (desc
->bMaxBurst
+ 1);
181 if (desc
->wBytesPerInterval
> max_tx
) {
182 dev_warn(ddev
, "%s endpoint with wBytesPerInterval of %d in "
183 "config %d interface %d altsetting %d ep %d: "
185 usb_endpoint_xfer_isoc(&ep
->desc
) ? "Isoc" : "Int",
186 desc
->wBytesPerInterval
,
187 cfgno
, inum
, asnum
, ep
->desc
.bEndpointAddress
,
189 desc
->wBytesPerInterval
= max_tx
;
195 static int usb_parse_endpoint(struct device
*ddev
, int cfgno
, int inum
,
196 int asnum
, struct usb_host_interface
*ifp
, int num_ep
,
197 unsigned char *buffer
, int size
)
199 unsigned char *buffer0
= buffer
;
200 struct usb_endpoint_descriptor
*d
;
201 struct usb_host_endpoint
*endpoint
;
204 d
= (struct usb_endpoint_descriptor
*) buffer
;
205 buffer
+= d
->bLength
;
208 if (d
->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
)
209 n
= USB_DT_ENDPOINT_AUDIO_SIZE
;
210 else if (d
->bLength
>= USB_DT_ENDPOINT_SIZE
)
211 n
= USB_DT_ENDPOINT_SIZE
;
213 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
214 "invalid endpoint descriptor of length %d, skipping\n",
215 cfgno
, inum
, asnum
, d
->bLength
);
216 goto skip_to_next_endpoint_or_interface_descriptor
;
219 i
= d
->bEndpointAddress
& ~USB_ENDPOINT_DIR_MASK
;
220 if (i
>= 16 || i
== 0) {
221 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
222 "invalid endpoint with address 0x%X, skipping\n",
223 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
224 goto skip_to_next_endpoint_or_interface_descriptor
;
227 /* Only store as many endpoints as we have room for */
228 if (ifp
->desc
.bNumEndpoints
>= num_ep
)
229 goto skip_to_next_endpoint_or_interface_descriptor
;
231 endpoint
= &ifp
->endpoint
[ifp
->desc
.bNumEndpoints
];
232 ++ifp
->desc
.bNumEndpoints
;
234 memcpy(&endpoint
->desc
, d
, n
);
235 INIT_LIST_HEAD(&endpoint
->urb_list
);
237 /* Fix up bInterval values outside the legal range. Use 32 ms if no
238 * proper value can be guessed. */
239 i
= 0; /* i = min, j = max, n = default */
241 if (usb_endpoint_xfer_int(d
)) {
243 switch (to_usb_device(ddev
)->speed
) {
244 case USB_SPEED_SUPER
:
246 /* Many device manufacturers are using full-speed
247 * bInterval values in high-speed interrupt endpoint
248 * descriptors. Try to fix those and fall back to a
249 * 32 ms default value otherwise. */
250 n
= fls(d
->bInterval
*8);
252 n
= 9; /* 32 ms = 2^(9-1) uframes */
255 default: /* USB_SPEED_FULL or _LOW */
256 /* For low-speed, 10 ms is the official minimum.
257 * But some "overclocked" devices might want faster
258 * polling so we'll allow it. */
262 } else if (usb_endpoint_xfer_isoc(d
)) {
265 switch (to_usb_device(ddev
)->speed
) {
267 n
= 9; /* 32 ms = 2^(9-1) uframes */
269 default: /* USB_SPEED_FULL */
270 n
= 6; /* 32 ms = 2^(6-1) frames */
274 if (d
->bInterval
< i
|| d
->bInterval
> j
) {
275 dev_warn(ddev
, "config %d interface %d altsetting %d "
276 "endpoint 0x%X has an invalid bInterval %d, "
279 d
->bEndpointAddress
, d
->bInterval
, n
);
280 endpoint
->desc
.bInterval
= n
;
283 /* Some buggy low-speed devices have Bulk endpoints, which is
284 * explicitly forbidden by the USB spec. In an attempt to make
285 * them usable, we will try treating them as Interrupt endpoints.
287 if (to_usb_device(ddev
)->speed
== USB_SPEED_LOW
&&
288 usb_endpoint_xfer_bulk(d
)) {
289 dev_warn(ddev
, "config %d interface %d altsetting %d "
290 "endpoint 0x%X is Bulk; changing to Interrupt\n",
291 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
292 endpoint
->desc
.bmAttributes
= USB_ENDPOINT_XFER_INT
;
293 endpoint
->desc
.bInterval
= 1;
294 if (le16_to_cpu(endpoint
->desc
.wMaxPacketSize
) > 8)
295 endpoint
->desc
.wMaxPacketSize
= cpu_to_le16(8);
299 * Some buggy high speed devices have bulk endpoints using
300 * maxpacket sizes other than 512. High speed HCDs may not
301 * be able to handle that particular bug, so let's warn...
303 if (to_usb_device(ddev
)->speed
== USB_SPEED_HIGH
304 && usb_endpoint_xfer_bulk(d
)) {
307 maxp
= le16_to_cpu(endpoint
->desc
.wMaxPacketSize
) & 0x07ff;
309 dev_warn(ddev
, "config %d interface %d altsetting %d "
310 "bulk endpoint 0x%X has invalid maxpacket %d\n",
311 cfgno
, inum
, asnum
, d
->bEndpointAddress
,
314 /* Allocate room for and parse any SS endpoint companion descriptors */
315 if (to_usb_device(ddev
)->speed
== USB_SPEED_SUPER
) {
316 endpoint
->extra
= buffer
;
317 i
= find_next_descriptor_more(buffer
, size
, USB_DT_SS_ENDPOINT_COMP
,
318 USB_DT_ENDPOINT
, USB_DT_INTERFACE
, &n
);
319 endpoint
->extralen
= i
;
324 retval
= usb_parse_ss_endpoint_companion(ddev
, cfgno
,
325 inum
, asnum
, endpoint
, num_ep
, buffer
,
329 retval
= buffer
- buffer0
;
332 retval
= buffer
- buffer0
;
335 /* Skip over any Class Specific or Vendor Specific descriptors;
336 * find the next endpoint or interface descriptor */
337 endpoint
->extra
= buffer
;
338 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
339 USB_DT_INTERFACE
, &n
);
340 endpoint
->extralen
= i
;
341 retval
= buffer
- buffer0
+ i
;
344 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
345 n
, plural(n
), "endpoint");
348 skip_to_next_endpoint_or_interface_descriptor
:
349 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
350 USB_DT_INTERFACE
, NULL
);
351 return buffer
- buffer0
+ i
;
354 void usb_release_interface_cache(struct kref
*ref
)
356 struct usb_interface_cache
*intfc
= ref_to_usb_interface_cache(ref
);
359 for (j
= 0; j
< intfc
->num_altsetting
; j
++) {
360 struct usb_host_interface
*alt
= &intfc
->altsetting
[j
];
362 kfree(alt
->endpoint
);
368 static int usb_parse_interface(struct device
*ddev
, int cfgno
,
369 struct usb_host_config
*config
, unsigned char *buffer
, int size
,
370 u8 inums
[], u8 nalts
[])
372 unsigned char *buffer0
= buffer
;
373 struct usb_interface_descriptor
*d
;
375 struct usb_interface_cache
*intfc
;
376 struct usb_host_interface
*alt
;
379 int num_ep
, num_ep_orig
;
381 d
= (struct usb_interface_descriptor
*) buffer
;
382 buffer
+= d
->bLength
;
385 if (d
->bLength
< USB_DT_INTERFACE_SIZE
)
386 goto skip_to_next_interface_descriptor
;
388 /* Which interface entry is this? */
390 inum
= d
->bInterfaceNumber
;
391 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
392 if (inums
[i
] == inum
) {
393 intfc
= config
->intf_cache
[i
];
397 if (!intfc
|| intfc
->num_altsetting
>= nalts
[i
])
398 goto skip_to_next_interface_descriptor
;
400 /* Check for duplicate altsetting entries */
401 asnum
= d
->bAlternateSetting
;
402 for ((i
= 0, alt
= &intfc
->altsetting
[0]);
403 i
< intfc
->num_altsetting
;
405 if (alt
->desc
.bAlternateSetting
== asnum
) {
406 dev_warn(ddev
, "Duplicate descriptor for config %d "
407 "interface %d altsetting %d, skipping\n",
409 goto skip_to_next_interface_descriptor
;
413 ++intfc
->num_altsetting
;
414 memcpy(&alt
->desc
, d
, USB_DT_INTERFACE_SIZE
);
416 /* Skip over any Class Specific or Vendor Specific descriptors;
417 * find the first endpoint or interface descriptor */
419 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
420 USB_DT_INTERFACE
, &n
);
423 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
424 n
, plural(n
), "interface");
428 /* Allocate space for the right(?) number of endpoints */
429 num_ep
= num_ep_orig
= alt
->desc
.bNumEndpoints
;
430 alt
->desc
.bNumEndpoints
= 0; /* Use as a counter */
431 if (num_ep
> USB_MAXENDPOINTS
) {
432 dev_warn(ddev
, "too many endpoints for config %d interface %d "
433 "altsetting %d: %d, using maximum allowed: %d\n",
434 cfgno
, inum
, asnum
, num_ep
, USB_MAXENDPOINTS
);
435 num_ep
= USB_MAXENDPOINTS
;
439 /* Can't allocate 0 bytes */
440 len
= sizeof(struct usb_host_endpoint
) * num_ep
;
441 alt
->endpoint
= kzalloc(len
, GFP_KERNEL
);
446 /* Parse all the endpoint descriptors */
449 if (((struct usb_descriptor_header
*) buffer
)->bDescriptorType
452 retval
= usb_parse_endpoint(ddev
, cfgno
, inum
, asnum
, alt
,
453 num_ep
, buffer
, size
);
462 if (n
!= num_ep_orig
)
463 dev_warn(ddev
, "config %d interface %d altsetting %d has %d "
464 "endpoint descriptor%s, different from the interface "
465 "descriptor's value: %d\n",
466 cfgno
, inum
, asnum
, n
, plural(n
), num_ep_orig
);
467 return buffer
- buffer0
;
469 skip_to_next_interface_descriptor
:
470 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
471 USB_DT_INTERFACE
, NULL
);
472 return buffer
- buffer0
+ i
;
475 static int usb_parse_configuration(struct device
*ddev
, int cfgidx
,
476 struct usb_host_config
*config
, unsigned char *buffer
, int size
)
478 unsigned char *buffer0
= buffer
;
480 int nintf
, nintf_orig
;
482 struct usb_interface_cache
*intfc
;
483 unsigned char *buffer2
;
485 struct usb_descriptor_header
*header
;
487 u8 inums
[USB_MAXINTERFACES
], nalts
[USB_MAXINTERFACES
];
488 unsigned iad_num
= 0;
490 memcpy(&config
->desc
, buffer
, USB_DT_CONFIG_SIZE
);
491 if (config
->desc
.bDescriptorType
!= USB_DT_CONFIG
||
492 config
->desc
.bLength
< USB_DT_CONFIG_SIZE
) {
493 dev_err(ddev
, "invalid descriptor for config index %d: "
494 "type = 0x%X, length = %d\n", cfgidx
,
495 config
->desc
.bDescriptorType
, config
->desc
.bLength
);
498 cfgno
= config
->desc
.bConfigurationValue
;
500 buffer
+= config
->desc
.bLength
;
501 size
-= config
->desc
.bLength
;
503 nintf
= nintf_orig
= config
->desc
.bNumInterfaces
;
504 if (nintf
> USB_MAXINTERFACES
) {
505 dev_warn(ddev
, "config %d has too many interfaces: %d, "
506 "using maximum allowed: %d\n",
507 cfgno
, nintf
, USB_MAXINTERFACES
);
508 nintf
= USB_MAXINTERFACES
;
511 /* Go through the descriptors, checking their length and counting the
512 * number of altsettings for each interface */
514 for ((buffer2
= buffer
, size2
= size
);
516 (buffer2
+= header
->bLength
, size2
-= header
->bLength
)) {
518 if (size2
< sizeof(struct usb_descriptor_header
)) {
519 dev_warn(ddev
, "config %d descriptor has %d excess "
520 "byte%s, ignoring\n",
521 cfgno
, size2
, plural(size2
));
525 header
= (struct usb_descriptor_header
*) buffer2
;
526 if ((header
->bLength
> size2
) || (header
->bLength
< 2)) {
527 dev_warn(ddev
, "config %d has an invalid descriptor "
528 "of length %d, skipping remainder of the config\n",
529 cfgno
, header
->bLength
);
533 if (header
->bDescriptorType
== USB_DT_INTERFACE
) {
534 struct usb_interface_descriptor
*d
;
537 d
= (struct usb_interface_descriptor
*) header
;
538 if (d
->bLength
< USB_DT_INTERFACE_SIZE
) {
539 dev_warn(ddev
, "config %d has an invalid "
540 "interface descriptor of length %d, "
541 "skipping\n", cfgno
, d
->bLength
);
545 inum
= d
->bInterfaceNumber
;
546 if (inum
>= nintf_orig
)
547 dev_warn(ddev
, "config %d has an invalid "
548 "interface number: %d but max is %d\n",
549 cfgno
, inum
, nintf_orig
- 1);
551 /* Have we already encountered this interface?
552 * Count its altsettings */
553 for (i
= 0; i
< n
; ++i
) {
554 if (inums
[i
] == inum
)
560 } else if (n
< USB_MAXINTERFACES
) {
566 } else if (header
->bDescriptorType
==
567 USB_DT_INTERFACE_ASSOCIATION
) {
568 if (iad_num
== USB_MAXIADS
) {
569 dev_warn(ddev
, "found more Interface "
570 "Association Descriptors "
571 "than allocated for in "
572 "configuration %d\n", cfgno
);
574 config
->intf_assoc
[iad_num
] =
575 (struct usb_interface_assoc_descriptor
580 } else if (header
->bDescriptorType
== USB_DT_DEVICE
||
581 header
->bDescriptorType
== USB_DT_CONFIG
)
582 dev_warn(ddev
, "config %d contains an unexpected "
583 "descriptor of type 0x%X, skipping\n",
584 cfgno
, header
->bDescriptorType
);
586 } /* for ((buffer2 = buffer, size2 = size); ...) */
587 size
= buffer2
- buffer
;
588 config
->desc
.wTotalLength
= cpu_to_le16(buffer2
- buffer0
);
591 dev_warn(ddev
, "config %d has %d interface%s, different from "
592 "the descriptor's value: %d\n",
593 cfgno
, n
, plural(n
), nintf_orig
);
595 dev_warn(ddev
, "config %d has no interfaces?\n", cfgno
);
596 config
->desc
.bNumInterfaces
= nintf
= n
;
598 /* Check for missing interface numbers */
599 for (i
= 0; i
< nintf
; ++i
) {
600 for (j
= 0; j
< nintf
; ++j
) {
605 dev_warn(ddev
, "config %d has no interface number "
609 /* Allocate the usb_interface_caches and altsetting arrays */
610 for (i
= 0; i
< nintf
; ++i
) {
612 if (j
> USB_MAXALTSETTING
) {
613 dev_warn(ddev
, "too many alternate settings for "
614 "config %d interface %d: %d, "
615 "using maximum allowed: %d\n",
616 cfgno
, inums
[i
], j
, USB_MAXALTSETTING
);
617 nalts
[i
] = j
= USB_MAXALTSETTING
;
620 len
= sizeof(*intfc
) + sizeof(struct usb_host_interface
) * j
;
621 config
->intf_cache
[i
] = intfc
= kzalloc(len
, GFP_KERNEL
);
624 kref_init(&intfc
->ref
);
627 /* FIXME: parse the BOS descriptor */
629 /* Skip over any Class Specific or Vendor Specific descriptors;
630 * find the first interface descriptor */
631 config
->extra
= buffer
;
632 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
633 USB_DT_INTERFACE
, &n
);
634 config
->extralen
= i
;
636 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
637 n
, plural(n
), "configuration");
641 /* Parse all the interface/altsetting descriptors */
643 retval
= usb_parse_interface(ddev
, cfgno
, config
,
644 buffer
, size
, inums
, nalts
);
652 /* Check for missing altsettings */
653 for (i
= 0; i
< nintf
; ++i
) {
654 intfc
= config
->intf_cache
[i
];
655 for (j
= 0; j
< intfc
->num_altsetting
; ++j
) {
656 for (n
= 0; n
< intfc
->num_altsetting
; ++n
) {
657 if (intfc
->altsetting
[n
].desc
.
658 bAlternateSetting
== j
)
661 if (n
>= intfc
->num_altsetting
)
662 dev_warn(ddev
, "config %d interface %d has no "
663 "altsetting %d\n", cfgno
, inums
[i
], j
);
670 /* hub-only!! ... and only exported for reset/reinit path.
671 * otherwise used internally on disconnect/destroy path
673 void usb_destroy_configuration(struct usb_device
*dev
)
680 if (dev
->rawdescriptors
) {
681 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++)
682 kfree(dev
->rawdescriptors
[i
]);
684 kfree(dev
->rawdescriptors
);
685 dev
->rawdescriptors
= NULL
;
688 for (c
= 0; c
< dev
->descriptor
.bNumConfigurations
; c
++) {
689 struct usb_host_config
*cf
= &dev
->config
[c
];
692 for (i
= 0; i
< cf
->desc
.bNumInterfaces
; i
++) {
693 if (cf
->intf_cache
[i
])
694 kref_put(&cf
->intf_cache
[i
]->ref
,
695 usb_release_interface_cache
);
704 * Get the USB config descriptors, cache and parse'em
706 * hub-only!! ... and only in reset path, or usb_new_device()
707 * (used by real hubs and virtual root hubs)
709 * NOTE: if this is a WUSB device and is not authorized, we skip the
710 * whole thing. A non-authorized USB device has no
713 int usb_get_configuration(struct usb_device
*dev
)
715 struct device
*ddev
= &dev
->dev
;
716 int ncfg
= dev
->descriptor
.bNumConfigurations
;
718 unsigned int cfgno
, length
;
719 unsigned char *buffer
;
720 unsigned char *bigbuffer
;
721 struct usb_config_descriptor
*desc
;
724 if (dev
->authorized
== 0) /* Not really an error */
725 goto out_not_authorized
;
727 if (ncfg
> USB_MAXCONFIG
) {
728 dev_warn(ddev
, "too many configurations: %d, "
729 "using maximum allowed: %d\n", ncfg
, USB_MAXCONFIG
);
730 dev
->descriptor
.bNumConfigurations
= ncfg
= USB_MAXCONFIG
;
734 dev_err(ddev
, "no configurations\n");
738 length
= ncfg
* sizeof(struct usb_host_config
);
739 dev
->config
= kzalloc(length
, GFP_KERNEL
);
743 length
= ncfg
* sizeof(char *);
744 dev
->rawdescriptors
= kzalloc(length
, GFP_KERNEL
);
745 if (!dev
->rawdescriptors
)
748 buffer
= kmalloc(USB_DT_CONFIG_SIZE
, GFP_KERNEL
);
751 desc
= (struct usb_config_descriptor
*)buffer
;
754 for (; cfgno
< ncfg
; cfgno
++) {
755 /* We grab just the first descriptor so we know how long
756 * the whole configuration is */
757 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
758 buffer
, USB_DT_CONFIG_SIZE
);
760 dev_err(ddev
, "unable to read config index %d "
761 "descriptor/%s: %d\n", cfgno
, "start", result
);
762 dev_err(ddev
, "chopping to %d config(s)\n", cfgno
);
763 dev
->descriptor
.bNumConfigurations
= cfgno
;
765 } else if (result
< 4) {
766 dev_err(ddev
, "config index %d descriptor too short "
767 "(expected %i, got %i)\n", cfgno
,
768 USB_DT_CONFIG_SIZE
, result
);
772 length
= max((int) le16_to_cpu(desc
->wTotalLength
),
775 /* Now that we know the length, get the whole thing */
776 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
781 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
784 dev_err(ddev
, "unable to read config index %d "
785 "descriptor/%s\n", cfgno
, "all");
789 if (result
< length
) {
790 dev_warn(ddev
, "config index %d descriptor too short "
791 "(expected %i, got %i)\n", cfgno
, length
, result
);
795 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
797 result
= usb_parse_configuration(&dev
->dev
, cfgno
,
798 &dev
->config
[cfgno
], bigbuffer
, length
);
809 dev
->descriptor
.bNumConfigurations
= cfgno
;
811 if (result
== -ENOMEM
)
812 dev_err(ddev
, "out of memory\n");