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 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 int usb_parse_endpoint(struct device
*ddev
, int cfgno
, int inum
,
47 int asnum
, struct usb_host_interface
*ifp
, int num_ep
,
48 unsigned char *buffer
, int size
)
50 unsigned char *buffer0
= buffer
;
51 struct usb_endpoint_descriptor
*d
;
52 struct usb_host_endpoint
*endpoint
;
55 d
= (struct usb_endpoint_descriptor
*) buffer
;
59 if (d
->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
)
60 n
= USB_DT_ENDPOINT_AUDIO_SIZE
;
61 else if (d
->bLength
>= USB_DT_ENDPOINT_SIZE
)
62 n
= USB_DT_ENDPOINT_SIZE
;
64 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
65 "invalid endpoint descriptor of length %d, skipping\n",
66 cfgno
, inum
, asnum
, d
->bLength
);
67 goto skip_to_next_endpoint_or_interface_descriptor
;
70 i
= d
->bEndpointAddress
& ~USB_ENDPOINT_DIR_MASK
;
71 if (i
>= 16 || i
== 0) {
72 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
73 "invalid endpoint with address 0x%X, skipping\n",
74 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
75 goto skip_to_next_endpoint_or_interface_descriptor
;
78 /* Only store as many endpoints as we have room for */
79 if (ifp
->desc
.bNumEndpoints
>= num_ep
)
80 goto skip_to_next_endpoint_or_interface_descriptor
;
82 endpoint
= &ifp
->endpoint
[ifp
->desc
.bNumEndpoints
];
83 ++ifp
->desc
.bNumEndpoints
;
85 memcpy(&endpoint
->desc
, d
, n
);
86 INIT_LIST_HEAD(&endpoint
->urb_list
);
88 /* Fix up bInterval values outside the legal range. Use 32 ms if no
89 * proper value can be guessed. */
90 i
= 0; /* i = min, j = max, n = default */
92 if (usb_endpoint_xfer_int(d
)) {
94 switch (to_usb_device(ddev
)->speed
) {
96 /* Many device manufacturers are using full-speed
97 * bInterval values in high-speed interrupt endpoint
98 * descriptors. Try to fix those and fall back to a
99 * 32 ms default value otherwise. */
100 n
= fls(d
->bInterval
*8);
102 n
= 9; /* 32 ms = 2^(9-1) uframes */
105 default: /* USB_SPEED_FULL or _LOW */
106 /* For low-speed, 10 ms is the official minimum.
107 * But some "overclocked" devices might want faster
108 * polling so we'll allow it. */
112 } else if (usb_endpoint_xfer_isoc(d
)) {
115 switch (to_usb_device(ddev
)->speed
) {
117 n
= 9; /* 32 ms = 2^(9-1) uframes */
119 default: /* USB_SPEED_FULL */
120 n
= 6; /* 32 ms = 2^(6-1) frames */
124 if (d
->bInterval
< i
|| d
->bInterval
> j
) {
125 dev_warn(ddev
, "config %d interface %d altsetting %d "
126 "endpoint 0x%X has an invalid bInterval %d, "
129 d
->bEndpointAddress
, d
->bInterval
, n
);
130 endpoint
->desc
.bInterval
= n
;
133 /* Some buggy low-speed devices have Bulk endpoints, which is
134 * explicitly forbidden by the USB spec. In an attempt to make
135 * them usable, we will try treating them as Interrupt endpoints.
137 if (to_usb_device(ddev
)->speed
== USB_SPEED_LOW
&&
138 usb_endpoint_xfer_bulk(d
)) {
139 dev_warn(ddev
, "config %d interface %d altsetting %d "
140 "endpoint 0x%X is Bulk; changing to Interrupt\n",
141 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
142 endpoint
->desc
.bmAttributes
= USB_ENDPOINT_XFER_INT
;
143 endpoint
->desc
.bInterval
= 1;
144 if (le16_to_cpu(endpoint
->desc
.wMaxPacketSize
) > 8)
145 endpoint
->desc
.wMaxPacketSize
= cpu_to_le16(8);
148 /* Skip over any Class Specific or Vendor Specific descriptors;
149 * find the next endpoint or interface descriptor */
150 endpoint
->extra
= buffer
;
151 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
152 USB_DT_INTERFACE
, &n
);
153 endpoint
->extralen
= i
;
155 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
156 n
, plural(n
), "endpoint");
157 return buffer
- buffer0
+ i
;
159 skip_to_next_endpoint_or_interface_descriptor
:
160 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
161 USB_DT_INTERFACE
, NULL
);
162 return buffer
- buffer0
+ i
;
165 void usb_release_interface_cache(struct kref
*ref
)
167 struct usb_interface_cache
*intfc
= ref_to_usb_interface_cache(ref
);
170 for (j
= 0; j
< intfc
->num_altsetting
; j
++) {
171 struct usb_host_interface
*alt
= &intfc
->altsetting
[j
];
173 kfree(alt
->endpoint
);
179 static int usb_parse_interface(struct device
*ddev
, int cfgno
,
180 struct usb_host_config
*config
, unsigned char *buffer
, int size
,
181 u8 inums
[], u8 nalts
[])
183 unsigned char *buffer0
= buffer
;
184 struct usb_interface_descriptor
*d
;
186 struct usb_interface_cache
*intfc
;
187 struct usb_host_interface
*alt
;
190 int num_ep
, num_ep_orig
;
192 d
= (struct usb_interface_descriptor
*) buffer
;
193 buffer
+= d
->bLength
;
196 if (d
->bLength
< USB_DT_INTERFACE_SIZE
)
197 goto skip_to_next_interface_descriptor
;
199 /* Which interface entry is this? */
201 inum
= d
->bInterfaceNumber
;
202 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
203 if (inums
[i
] == inum
) {
204 intfc
= config
->intf_cache
[i
];
208 if (!intfc
|| intfc
->num_altsetting
>= nalts
[i
])
209 goto skip_to_next_interface_descriptor
;
211 /* Check for duplicate altsetting entries */
212 asnum
= d
->bAlternateSetting
;
213 for ((i
= 0, alt
= &intfc
->altsetting
[0]);
214 i
< intfc
->num_altsetting
;
216 if (alt
->desc
.bAlternateSetting
== asnum
) {
217 dev_warn(ddev
, "Duplicate descriptor for config %d "
218 "interface %d altsetting %d, skipping\n",
220 goto skip_to_next_interface_descriptor
;
224 ++intfc
->num_altsetting
;
225 memcpy(&alt
->desc
, d
, USB_DT_INTERFACE_SIZE
);
227 /* Skip over any Class Specific or Vendor Specific descriptors;
228 * find the first endpoint or interface descriptor */
230 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
231 USB_DT_INTERFACE
, &n
);
234 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
235 n
, plural(n
), "interface");
239 /* Allocate space for the right(?) number of endpoints */
240 num_ep
= num_ep_orig
= alt
->desc
.bNumEndpoints
;
241 alt
->desc
.bNumEndpoints
= 0; /* Use as a counter */
242 if (num_ep
> USB_MAXENDPOINTS
) {
243 dev_warn(ddev
, "too many endpoints for config %d interface %d "
244 "altsetting %d: %d, using maximum allowed: %d\n",
245 cfgno
, inum
, asnum
, num_ep
, USB_MAXENDPOINTS
);
246 num_ep
= USB_MAXENDPOINTS
;
250 /* Can't allocate 0 bytes */
251 len
= sizeof(struct usb_host_endpoint
) * num_ep
;
252 alt
->endpoint
= kzalloc(len
, GFP_KERNEL
);
257 /* Parse all the endpoint descriptors */
260 if (((struct usb_descriptor_header
*) buffer
)->bDescriptorType
263 retval
= usb_parse_endpoint(ddev
, cfgno
, inum
, asnum
, alt
,
264 num_ep
, buffer
, size
);
273 if (n
!= num_ep_orig
)
274 dev_warn(ddev
, "config %d interface %d altsetting %d has %d "
275 "endpoint descriptor%s, different from the interface "
276 "descriptor's value: %d\n",
277 cfgno
, inum
, asnum
, n
, plural(n
), num_ep_orig
);
278 return buffer
- buffer0
;
280 skip_to_next_interface_descriptor
:
281 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
282 USB_DT_INTERFACE
, NULL
);
283 return buffer
- buffer0
+ i
;
286 static int usb_parse_configuration(struct device
*ddev
, int cfgidx
,
287 struct usb_host_config
*config
, unsigned char *buffer
, int size
)
289 unsigned char *buffer0
= buffer
;
291 int nintf
, nintf_orig
;
293 struct usb_interface_cache
*intfc
;
294 unsigned char *buffer2
;
296 struct usb_descriptor_header
*header
;
298 u8 inums
[USB_MAXINTERFACES
], nalts
[USB_MAXINTERFACES
];
299 unsigned iad_num
= 0;
301 memcpy(&config
->desc
, buffer
, USB_DT_CONFIG_SIZE
);
302 if (config
->desc
.bDescriptorType
!= USB_DT_CONFIG
||
303 config
->desc
.bLength
< USB_DT_CONFIG_SIZE
) {
304 dev_err(ddev
, "invalid descriptor for config index %d: "
305 "type = 0x%X, length = %d\n", cfgidx
,
306 config
->desc
.bDescriptorType
, config
->desc
.bLength
);
309 cfgno
= config
->desc
.bConfigurationValue
;
311 buffer
+= config
->desc
.bLength
;
312 size
-= config
->desc
.bLength
;
314 nintf
= nintf_orig
= config
->desc
.bNumInterfaces
;
315 if (nintf
> USB_MAXINTERFACES
) {
316 dev_warn(ddev
, "config %d has too many interfaces: %d, "
317 "using maximum allowed: %d\n",
318 cfgno
, nintf
, USB_MAXINTERFACES
);
319 nintf
= USB_MAXINTERFACES
;
322 /* Go through the descriptors, checking their length and counting the
323 * number of altsettings for each interface */
325 for ((buffer2
= buffer
, size2
= size
);
327 (buffer2
+= header
->bLength
, size2
-= header
->bLength
)) {
329 if (size2
< sizeof(struct usb_descriptor_header
)) {
330 dev_warn(ddev
, "config %d descriptor has %d excess "
331 "byte%s, ignoring\n",
332 cfgno
, size2
, plural(size2
));
336 header
= (struct usb_descriptor_header
*) buffer2
;
337 if ((header
->bLength
> size2
) || (header
->bLength
< 2)) {
338 dev_warn(ddev
, "config %d has an invalid descriptor "
339 "of length %d, skipping remainder of the config\n",
340 cfgno
, header
->bLength
);
344 if (header
->bDescriptorType
== USB_DT_INTERFACE
) {
345 struct usb_interface_descriptor
*d
;
348 d
= (struct usb_interface_descriptor
*) header
;
349 if (d
->bLength
< USB_DT_INTERFACE_SIZE
) {
350 dev_warn(ddev
, "config %d has an invalid "
351 "interface descriptor of length %d, "
352 "skipping\n", cfgno
, d
->bLength
);
356 inum
= d
->bInterfaceNumber
;
357 if (inum
>= nintf_orig
)
358 dev_warn(ddev
, "config %d has an invalid "
359 "interface number: %d but max is %d\n",
360 cfgno
, inum
, nintf_orig
- 1);
362 /* Have we already encountered this interface?
363 * Count its altsettings */
364 for (i
= 0; i
< n
; ++i
) {
365 if (inums
[i
] == inum
)
371 } else if (n
< USB_MAXINTERFACES
) {
377 } else if (header
->bDescriptorType
==
378 USB_DT_INTERFACE_ASSOCIATION
) {
379 if (iad_num
== USB_MAXIADS
) {
380 dev_warn(ddev
, "found more Interface "
381 "Association Descriptors "
382 "than allocated for in "
383 "configuration %d\n", cfgno
);
385 config
->intf_assoc
[iad_num
] =
386 (struct usb_interface_assoc_descriptor
391 } else if (header
->bDescriptorType
== USB_DT_DEVICE
||
392 header
->bDescriptorType
== USB_DT_CONFIG
)
393 dev_warn(ddev
, "config %d contains an unexpected "
394 "descriptor of type 0x%X, skipping\n",
395 cfgno
, header
->bDescriptorType
);
397 } /* for ((buffer2 = buffer, size2 = size); ...) */
398 size
= buffer2
- buffer
;
399 config
->desc
.wTotalLength
= cpu_to_le16(buffer2
- buffer0
);
402 dev_warn(ddev
, "config %d has %d interface%s, different from "
403 "the descriptor's value: %d\n",
404 cfgno
, n
, plural(n
), nintf_orig
);
406 dev_warn(ddev
, "config %d has no interfaces?\n", cfgno
);
407 config
->desc
.bNumInterfaces
= nintf
= n
;
409 /* Check for missing interface numbers */
410 for (i
= 0; i
< nintf
; ++i
) {
411 for (j
= 0; j
< nintf
; ++j
) {
416 dev_warn(ddev
, "config %d has no interface number "
420 /* Allocate the usb_interface_caches and altsetting arrays */
421 for (i
= 0; i
< nintf
; ++i
) {
423 if (j
> USB_MAXALTSETTING
) {
424 dev_warn(ddev
, "too many alternate settings for "
425 "config %d interface %d: %d, "
426 "using maximum allowed: %d\n",
427 cfgno
, inums
[i
], j
, USB_MAXALTSETTING
);
428 nalts
[i
] = j
= USB_MAXALTSETTING
;
431 len
= sizeof(*intfc
) + sizeof(struct usb_host_interface
) * j
;
432 config
->intf_cache
[i
] = intfc
= kzalloc(len
, GFP_KERNEL
);
435 kref_init(&intfc
->ref
);
438 /* Skip over any Class Specific or Vendor Specific descriptors;
439 * find the first interface descriptor */
440 config
->extra
= buffer
;
441 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
442 USB_DT_INTERFACE
, &n
);
443 config
->extralen
= i
;
445 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
446 n
, plural(n
), "configuration");
450 /* Parse all the interface/altsetting descriptors */
452 retval
= usb_parse_interface(ddev
, cfgno
, config
,
453 buffer
, size
, inums
, nalts
);
461 /* Check for missing altsettings */
462 for (i
= 0; i
< nintf
; ++i
) {
463 intfc
= config
->intf_cache
[i
];
464 for (j
= 0; j
< intfc
->num_altsetting
; ++j
) {
465 for (n
= 0; n
< intfc
->num_altsetting
; ++n
) {
466 if (intfc
->altsetting
[n
].desc
.
467 bAlternateSetting
== j
)
470 if (n
>= intfc
->num_altsetting
)
471 dev_warn(ddev
, "config %d interface %d has no "
472 "altsetting %d\n", cfgno
, inums
[i
], j
);
479 /* hub-only!! ... and only exported for reset/reinit path.
480 * otherwise used internally on disconnect/destroy path
482 void usb_destroy_configuration(struct usb_device
*dev
)
489 if (dev
->rawdescriptors
) {
490 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++)
491 kfree(dev
->rawdescriptors
[i
]);
493 kfree(dev
->rawdescriptors
);
494 dev
->rawdescriptors
= NULL
;
497 for (c
= 0; c
< dev
->descriptor
.bNumConfigurations
; c
++) {
498 struct usb_host_config
*cf
= &dev
->config
[c
];
501 for (i
= 0; i
< cf
->desc
.bNumInterfaces
; i
++) {
502 if (cf
->intf_cache
[i
])
503 kref_put(&cf
->intf_cache
[i
]->ref
,
504 usb_release_interface_cache
);
513 * Get the USB config descriptors, cache and parse'em
515 * hub-only!! ... and only in reset path, or usb_new_device()
516 * (used by real hubs and virtual root hubs)
518 * NOTE: if this is a WUSB device and is not authorized, we skip the
519 * whole thing. A non-authorized USB device has no
522 int usb_get_configuration(struct usb_device
*dev
)
524 struct device
*ddev
= &dev
->dev
;
525 int ncfg
= dev
->descriptor
.bNumConfigurations
;
527 unsigned int cfgno
, length
;
528 unsigned char *buffer
;
529 unsigned char *bigbuffer
;
530 struct usb_config_descriptor
*desc
;
533 if (dev
->authorized
== 0) /* Not really an error */
534 goto out_not_authorized
;
536 if (ncfg
> USB_MAXCONFIG
) {
537 dev_warn(ddev
, "too many configurations: %d, "
538 "using maximum allowed: %d\n", ncfg
, USB_MAXCONFIG
);
539 dev
->descriptor
.bNumConfigurations
= ncfg
= USB_MAXCONFIG
;
543 dev_err(ddev
, "no configurations\n");
547 length
= ncfg
* sizeof(struct usb_host_config
);
548 dev
->config
= kzalloc(length
, GFP_KERNEL
);
552 length
= ncfg
* sizeof(char *);
553 dev
->rawdescriptors
= kzalloc(length
, GFP_KERNEL
);
554 if (!dev
->rawdescriptors
)
557 buffer
= kmalloc(USB_DT_CONFIG_SIZE
, GFP_KERNEL
);
560 desc
= (struct usb_config_descriptor
*)buffer
;
563 for (; cfgno
< ncfg
; cfgno
++) {
564 /* We grab just the first descriptor so we know how long
565 * the whole configuration is */
566 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
567 buffer
, USB_DT_CONFIG_SIZE
);
569 dev_err(ddev
, "unable to read config index %d "
570 "descriptor/%s: %d\n", cfgno
, "start", result
);
571 dev_err(ddev
, "chopping to %d config(s)\n", cfgno
);
572 dev
->descriptor
.bNumConfigurations
= cfgno
;
574 } else if (result
< 4) {
575 dev_err(ddev
, "config index %d descriptor too short "
576 "(expected %i, got %i)\n", cfgno
,
577 USB_DT_CONFIG_SIZE
, result
);
581 length
= max((int) le16_to_cpu(desc
->wTotalLength
),
584 /* Now that we know the length, get the whole thing */
585 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
590 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
593 dev_err(ddev
, "unable to read config index %d "
594 "descriptor/%s\n", cfgno
, "all");
598 if (result
< length
) {
599 dev_warn(ddev
, "config index %d descriptor too short "
600 "(expected %i, got %i)\n", cfgno
, length
, result
);
604 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
606 result
= usb_parse_configuration(&dev
->dev
, cfgno
,
607 &dev
->config
[cfgno
], bigbuffer
, length
);
618 dev
->descriptor
.bNumConfigurations
= cfgno
;
620 if (result
== -ENOMEM
)
621 dev_err(ddev
, "out of memory\n");