2 #include <linux/usb/ch9.h>
3 #include <linux/usb/quirks.h>
4 #include <linux/module.h>
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/device.h>
8 #include <asm/byteorder.h>
12 #define USB_MAXALTSETTING 128 /* Hard limit */
13 #define USB_MAXENDPOINTS 30 /* Hard limit */
15 #define USB_MAXCONFIG 8 /* Arbitrary limit */
18 static inline const char *plural(int n
)
20 return (n
== 1 ? "" : "s");
23 static int find_next_descriptor(unsigned char *buffer
, int size
,
24 int dt1
, int dt2
, int *num_skipped
)
26 struct usb_descriptor_header
*h
;
28 unsigned char *buffer0
= buffer
;
30 /* Find the next descriptor of type dt1 or dt2 */
32 h
= (struct usb_descriptor_header
*) buffer
;
33 if (h
->bDescriptorType
== dt1
|| h
->bDescriptorType
== dt2
)
40 /* Store the number of descriptors skipped and return the
41 * number of bytes skipped */
44 return buffer
- buffer0
;
47 static int usb_parse_endpoint(struct device
*ddev
, int cfgno
, int inum
,
48 int asnum
, struct usb_host_interface
*ifp
, int num_ep
,
49 unsigned char *buffer
, int size
)
51 unsigned char *buffer0
= buffer
;
52 struct usb_endpoint_descriptor
*d
;
53 struct usb_host_endpoint
*endpoint
;
56 d
= (struct usb_endpoint_descriptor
*) buffer
;
60 if (d
->bLength
>= USB_DT_ENDPOINT_AUDIO_SIZE
)
61 n
= USB_DT_ENDPOINT_AUDIO_SIZE
;
62 else if (d
->bLength
>= USB_DT_ENDPOINT_SIZE
)
63 n
= USB_DT_ENDPOINT_SIZE
;
65 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
66 "invalid endpoint descriptor of length %d, skipping\n",
67 cfgno
, inum
, asnum
, d
->bLength
);
68 goto skip_to_next_endpoint_or_interface_descriptor
;
71 i
= d
->bEndpointAddress
& ~USB_ENDPOINT_DIR_MASK
;
72 if (i
>= 16 || i
== 0) {
73 dev_warn(ddev
, "config %d interface %d altsetting %d has an "
74 "invalid endpoint with address 0x%X, skipping\n",
75 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
76 goto skip_to_next_endpoint_or_interface_descriptor
;
79 /* Only store as many endpoints as we have room for */
80 if (ifp
->desc
.bNumEndpoints
>= num_ep
)
81 goto skip_to_next_endpoint_or_interface_descriptor
;
83 endpoint
= &ifp
->endpoint
[ifp
->desc
.bNumEndpoints
];
84 ++ifp
->desc
.bNumEndpoints
;
86 memcpy(&endpoint
->desc
, d
, n
);
87 INIT_LIST_HEAD(&endpoint
->urb_list
);
89 /* Fix up bInterval values outside the legal range. Use 32 ms if no
90 * proper value can be guessed. */
91 i
= 0; /* i = min, j = max, n = default */
93 if (usb_endpoint_xfer_int(d
)) {
95 switch (to_usb_device(ddev
)->speed
) {
97 /* Many device manufacturers are using full-speed
98 * bInterval values in high-speed interrupt endpoint
99 * descriptors. Try to fix those and fall back to a
100 * 32 ms default value otherwise. */
101 n
= fls(d
->bInterval
*8);
103 n
= 9; /* 32 ms = 2^(9-1) uframes */
106 default: /* USB_SPEED_FULL or _LOW */
107 /* For low-speed, 10 ms is the official minimum.
108 * But some "overclocked" devices might want faster
109 * polling so we'll allow it. */
113 } else if (usb_endpoint_xfer_isoc(d
)) {
116 switch (to_usb_device(ddev
)->speed
) {
118 n
= 9; /* 32 ms = 2^(9-1) uframes */
120 default: /* USB_SPEED_FULL */
121 n
= 6; /* 32 ms = 2^(6-1) frames */
125 if (d
->bInterval
< i
|| d
->bInterval
> j
) {
126 dev_warn(ddev
, "config %d interface %d altsetting %d "
127 "endpoint 0x%X has an invalid bInterval %d, "
130 d
->bEndpointAddress
, d
->bInterval
, n
);
131 endpoint
->desc
.bInterval
= n
;
134 /* Some buggy low-speed devices have Bulk endpoints, which is
135 * explicitly forbidden by the USB spec. In an attempt to make
136 * them usable, we will try treating them as Interrupt endpoints.
138 if (to_usb_device(ddev
)->speed
== USB_SPEED_LOW
&&
139 usb_endpoint_xfer_bulk(d
)) {
140 dev_warn(ddev
, "config %d interface %d altsetting %d "
141 "endpoint 0x%X is Bulk; changing to Interrupt\n",
142 cfgno
, inum
, asnum
, d
->bEndpointAddress
);
143 endpoint
->desc
.bmAttributes
= USB_ENDPOINT_XFER_INT
;
144 endpoint
->desc
.bInterval
= 1;
145 if (le16_to_cpu(endpoint
->desc
.wMaxPacketSize
) > 8)
146 endpoint
->desc
.wMaxPacketSize
= cpu_to_le16(8);
150 * Some buggy high speed devices have bulk endpoints using
151 * maxpacket sizes other than 512. High speed HCDs may not
152 * be able to handle that particular bug, so let's warn...
154 if (to_usb_device(ddev
)->speed
== USB_SPEED_HIGH
155 && usb_endpoint_xfer_bulk(d
)) {
158 maxp
= le16_to_cpu(endpoint
->desc
.wMaxPacketSize
) & 0x07ff;
160 dev_warn(ddev
, "config %d interface %d altsetting %d "
161 "bulk endpoint 0x%X has invalid maxpacket %d\n",
162 cfgno
, inum
, asnum
, d
->bEndpointAddress
,
166 /* Skip over any Class Specific or Vendor Specific descriptors;
167 * find the next endpoint or interface descriptor */
168 endpoint
->extra
= buffer
;
169 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
170 USB_DT_INTERFACE
, &n
);
171 endpoint
->extralen
= i
;
173 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
174 n
, plural(n
), "endpoint");
175 return buffer
- buffer0
+ i
;
177 skip_to_next_endpoint_or_interface_descriptor
:
178 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
179 USB_DT_INTERFACE
, NULL
);
180 return buffer
- buffer0
+ i
;
183 void usb_release_interface_cache(struct kref
*ref
)
185 struct usb_interface_cache
*intfc
= ref_to_usb_interface_cache(ref
);
188 for (j
= 0; j
< intfc
->num_altsetting
; j
++) {
189 struct usb_host_interface
*alt
= &intfc
->altsetting
[j
];
191 kfree(alt
->endpoint
);
197 static int usb_parse_interface(struct device
*ddev
, int cfgno
,
198 struct usb_host_config
*config
, unsigned char *buffer
, int size
,
199 u8 inums
[], u8 nalts
[])
201 unsigned char *buffer0
= buffer
;
202 struct usb_interface_descriptor
*d
;
204 struct usb_interface_cache
*intfc
;
205 struct usb_host_interface
*alt
;
208 int num_ep
, num_ep_orig
;
210 d
= (struct usb_interface_descriptor
*) buffer
;
211 buffer
+= d
->bLength
;
214 if (d
->bLength
< USB_DT_INTERFACE_SIZE
)
215 goto skip_to_next_interface_descriptor
;
217 /* Which interface entry is this? */
219 inum
= d
->bInterfaceNumber
;
220 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
221 if (inums
[i
] == inum
) {
222 intfc
= config
->intf_cache
[i
];
226 if (!intfc
|| intfc
->num_altsetting
>= nalts
[i
])
227 goto skip_to_next_interface_descriptor
;
229 /* Check for duplicate altsetting entries */
230 asnum
= d
->bAlternateSetting
;
231 for ((i
= 0, alt
= &intfc
->altsetting
[0]);
232 i
< intfc
->num_altsetting
;
234 if (alt
->desc
.bAlternateSetting
== asnum
) {
235 dev_warn(ddev
, "Duplicate descriptor for config %d "
236 "interface %d altsetting %d, skipping\n",
238 goto skip_to_next_interface_descriptor
;
242 ++intfc
->num_altsetting
;
243 memcpy(&alt
->desc
, d
, USB_DT_INTERFACE_SIZE
);
245 /* Skip over any Class Specific or Vendor Specific descriptors;
246 * find the first endpoint or interface descriptor */
248 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
249 USB_DT_INTERFACE
, &n
);
252 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
253 n
, plural(n
), "interface");
257 /* Allocate space for the right(?) number of endpoints */
258 num_ep
= num_ep_orig
= alt
->desc
.bNumEndpoints
;
259 alt
->desc
.bNumEndpoints
= 0; /* Use as a counter */
260 if (num_ep
> USB_MAXENDPOINTS
) {
261 dev_warn(ddev
, "too many endpoints for config %d interface %d "
262 "altsetting %d: %d, using maximum allowed: %d\n",
263 cfgno
, inum
, asnum
, num_ep
, USB_MAXENDPOINTS
);
264 num_ep
= USB_MAXENDPOINTS
;
268 /* Can't allocate 0 bytes */
269 len
= sizeof(struct usb_host_endpoint
) * num_ep
;
270 alt
->endpoint
= kzalloc(len
, GFP_KERNEL
);
275 /* Parse all the endpoint descriptors */
278 if (((struct usb_descriptor_header
*) buffer
)->bDescriptorType
281 retval
= usb_parse_endpoint(ddev
, cfgno
, inum
, asnum
, alt
,
282 num_ep
, buffer
, size
);
291 if (n
!= num_ep_orig
)
292 dev_warn(ddev
, "config %d interface %d altsetting %d has %d "
293 "endpoint descriptor%s, different from the interface "
294 "descriptor's value: %d\n",
295 cfgno
, inum
, asnum
, n
, plural(n
), num_ep_orig
);
296 return buffer
- buffer0
;
298 skip_to_next_interface_descriptor
:
299 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
300 USB_DT_INTERFACE
, NULL
);
301 return buffer
- buffer0
+ i
;
304 static int usb_parse_configuration(struct usb_device
*dev
, int cfgidx
,
305 struct usb_host_config
*config
, unsigned char *buffer
, int size
)
307 struct device
*ddev
= &dev
->dev
;
308 unsigned char *buffer0
= buffer
;
310 int nintf
, nintf_orig
;
312 struct usb_interface_cache
*intfc
;
313 unsigned char *buffer2
;
315 struct usb_descriptor_header
*header
;
317 u8 inums
[USB_MAXINTERFACES
], nalts
[USB_MAXINTERFACES
];
318 unsigned iad_num
= 0;
320 memcpy(&config
->desc
, buffer
, USB_DT_CONFIG_SIZE
);
321 if (config
->desc
.bDescriptorType
!= USB_DT_CONFIG
||
322 config
->desc
.bLength
< USB_DT_CONFIG_SIZE
) {
323 dev_err(ddev
, "invalid descriptor for config index %d: "
324 "type = 0x%X, length = %d\n", cfgidx
,
325 config
->desc
.bDescriptorType
, config
->desc
.bLength
);
328 cfgno
= config
->desc
.bConfigurationValue
;
330 buffer
+= config
->desc
.bLength
;
331 size
-= config
->desc
.bLength
;
333 nintf
= nintf_orig
= config
->desc
.bNumInterfaces
;
334 if (nintf
> USB_MAXINTERFACES
) {
335 dev_warn(ddev
, "config %d has too many interfaces: %d, "
336 "using maximum allowed: %d\n",
337 cfgno
, nintf
, USB_MAXINTERFACES
);
338 nintf
= USB_MAXINTERFACES
;
341 /* Go through the descriptors, checking their length and counting the
342 * number of altsettings for each interface */
344 for ((buffer2
= buffer
, size2
= size
);
346 (buffer2
+= header
->bLength
, size2
-= header
->bLength
)) {
348 if (size2
< sizeof(struct usb_descriptor_header
)) {
349 dev_warn(ddev
, "config %d descriptor has %d excess "
350 "byte%s, ignoring\n",
351 cfgno
, size2
, plural(size2
));
355 header
= (struct usb_descriptor_header
*) buffer2
;
356 if ((header
->bLength
> size2
) || (header
->bLength
< 2)) {
357 dev_warn(ddev
, "config %d has an invalid descriptor "
358 "of length %d, skipping remainder of the config\n",
359 cfgno
, header
->bLength
);
363 if (header
->bDescriptorType
== USB_DT_INTERFACE
) {
364 struct usb_interface_descriptor
*d
;
367 d
= (struct usb_interface_descriptor
*) header
;
368 if (d
->bLength
< USB_DT_INTERFACE_SIZE
) {
369 dev_warn(ddev
, "config %d has an invalid "
370 "interface descriptor of length %d, "
371 "skipping\n", cfgno
, d
->bLength
);
375 inum
= d
->bInterfaceNumber
;
377 if ((dev
->quirks
& USB_QUIRK_HONOR_BNUMINTERFACES
) &&
379 dev_warn(ddev
, "config %d has more interface "
380 "descriptors than it declares in "
381 "bNumInterfaces, ignoring interface "
382 "number: %d\n", cfgno
, inum
);
386 if (inum
>= nintf_orig
)
387 dev_warn(ddev
, "config %d has an invalid "
388 "interface number: %d but max is %d\n",
389 cfgno
, inum
, nintf_orig
- 1);
391 /* Have we already encountered this interface?
392 * Count its altsettings */
393 for (i
= 0; i
< n
; ++i
) {
394 if (inums
[i
] == inum
)
400 } else if (n
< USB_MAXINTERFACES
) {
406 } else if (header
->bDescriptorType
==
407 USB_DT_INTERFACE_ASSOCIATION
) {
408 if (iad_num
== USB_MAXIADS
) {
409 dev_warn(ddev
, "found more Interface "
410 "Association Descriptors "
411 "than allocated for in "
412 "configuration %d\n", cfgno
);
414 config
->intf_assoc
[iad_num
] =
415 (struct usb_interface_assoc_descriptor
420 } else if (header
->bDescriptorType
== USB_DT_DEVICE
||
421 header
->bDescriptorType
== USB_DT_CONFIG
)
422 dev_warn(ddev
, "config %d contains an unexpected "
423 "descriptor of type 0x%X, skipping\n",
424 cfgno
, header
->bDescriptorType
);
426 } /* for ((buffer2 = buffer, size2 = size); ...) */
427 size
= buffer2
- buffer
;
428 config
->desc
.wTotalLength
= cpu_to_le16(buffer2
- buffer0
);
431 dev_warn(ddev
, "config %d has %d interface%s, different from "
432 "the descriptor's value: %d\n",
433 cfgno
, n
, plural(n
), nintf_orig
);
435 dev_warn(ddev
, "config %d has no interfaces?\n", cfgno
);
436 config
->desc
.bNumInterfaces
= nintf
= n
;
438 /* Check for missing interface numbers */
439 for (i
= 0; i
< nintf
; ++i
) {
440 for (j
= 0; j
< nintf
; ++j
) {
445 dev_warn(ddev
, "config %d has no interface number "
449 /* Allocate the usb_interface_caches and altsetting arrays */
450 for (i
= 0; i
< nintf
; ++i
) {
452 if (j
> USB_MAXALTSETTING
) {
453 dev_warn(ddev
, "too many alternate settings for "
454 "config %d interface %d: %d, "
455 "using maximum allowed: %d\n",
456 cfgno
, inums
[i
], j
, USB_MAXALTSETTING
);
457 nalts
[i
] = j
= USB_MAXALTSETTING
;
460 len
= sizeof(*intfc
) + sizeof(struct usb_host_interface
) * j
;
461 config
->intf_cache
[i
] = intfc
= kzalloc(len
, GFP_KERNEL
);
464 kref_init(&intfc
->ref
);
467 /* Skip over any Class Specific or Vendor Specific descriptors;
468 * find the first interface descriptor */
469 config
->extra
= buffer
;
470 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
471 USB_DT_INTERFACE
, &n
);
472 config
->extralen
= i
;
474 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
475 n
, plural(n
), "configuration");
479 /* Parse all the interface/altsetting descriptors */
481 retval
= usb_parse_interface(ddev
, cfgno
, config
,
482 buffer
, size
, inums
, nalts
);
490 /* Check for missing altsettings */
491 for (i
= 0; i
< nintf
; ++i
) {
492 intfc
= config
->intf_cache
[i
];
493 for (j
= 0; j
< intfc
->num_altsetting
; ++j
) {
494 for (n
= 0; n
< intfc
->num_altsetting
; ++n
) {
495 if (intfc
->altsetting
[n
].desc
.
496 bAlternateSetting
== j
)
499 if (n
>= intfc
->num_altsetting
)
500 dev_warn(ddev
, "config %d interface %d has no "
501 "altsetting %d\n", cfgno
, inums
[i
], j
);
508 /* hub-only!! ... and only exported for reset/reinit path.
509 * otherwise used internally on disconnect/destroy path
511 void usb_destroy_configuration(struct usb_device
*dev
)
518 if (dev
->rawdescriptors
) {
519 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++)
520 kfree(dev
->rawdescriptors
[i
]);
522 kfree(dev
->rawdescriptors
);
523 dev
->rawdescriptors
= NULL
;
526 for (c
= 0; c
< dev
->descriptor
.bNumConfigurations
; c
++) {
527 struct usb_host_config
*cf
= &dev
->config
[c
];
530 for (i
= 0; i
< cf
->desc
.bNumInterfaces
; i
++) {
531 if (cf
->intf_cache
[i
])
532 kref_put(&cf
->intf_cache
[i
]->ref
,
533 usb_release_interface_cache
);
541 // hub-only!! ... and only in reset path, or usb_new_device()
542 // (used by real hubs and virtual root hubs)
543 int usb_get_configuration(struct usb_device
*dev
)
545 struct device
*ddev
= &dev
->dev
;
546 int ncfg
= dev
->descriptor
.bNumConfigurations
;
547 int result
= -ENOMEM
;
548 unsigned int cfgno
, length
;
549 unsigned char *bigbuffer
;
550 struct usb_config_descriptor
*desc
;
552 if (ncfg
> USB_MAXCONFIG
) {
553 dev_warn(ddev
, "too many configurations: %d, "
554 "using maximum allowed: %d\n", ncfg
, USB_MAXCONFIG
);
555 dev
->descriptor
.bNumConfigurations
= ncfg
= USB_MAXCONFIG
;
559 dev_err(ddev
, "no configurations\n");
563 length
= ncfg
* sizeof(struct usb_host_config
);
564 dev
->config
= kzalloc(length
, GFP_KERNEL
);
568 length
= ncfg
* sizeof(char *);
569 dev
->rawdescriptors
= kzalloc(length
, GFP_KERNEL
);
570 if (!dev
->rawdescriptors
)
573 desc
= kmalloc(USB_DT_CONFIG_SIZE
, GFP_KERNEL
);
577 for (cfgno
= 0; cfgno
< ncfg
; cfgno
++) {
578 /* We grab just the first descriptor so we know how long
579 * the whole configuration is */
580 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
581 desc
, USB_DT_CONFIG_SIZE
);
583 dev_err(ddev
, "unable to read config index %d "
584 "descriptor/%s\n", cfgno
, "start");
585 dev_err(ddev
, "chopping to %d config(s)\n", cfgno
);
586 dev
->descriptor
.bNumConfigurations
= cfgno
;
588 } else if (result
< 4) {
589 dev_err(ddev
, "config index %d descriptor too short "
590 "(expected %i, got %i)\n", cfgno
,
591 USB_DT_CONFIG_SIZE
, result
);
595 length
= max((int) le16_to_cpu(desc
->wTotalLength
),
598 /* Now that we know the length, get the whole thing */
599 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
604 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
607 dev_err(ddev
, "unable to read config index %d "
608 "descriptor/%s\n", cfgno
, "all");
612 if (result
< length
) {
613 dev_warn(ddev
, "config index %d descriptor too short "
614 "(expected %i, got %i)\n", cfgno
, length
, result
);
618 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
620 result
= usb_parse_configuration(dev
, cfgno
,
621 &dev
->config
[cfgno
], bigbuffer
, length
);
631 dev
->descriptor
.bNumConfigurations
= cfgno
;
633 if (result
== -ENOMEM
)
634 dev_err(ddev
, "out of memory\n");