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 /* If the bInterval value is outside the legal range,
89 * set it to a default value: 32 ms */
90 i
= 0; /* i = min, j = max, n = default */
92 if (usb_endpoint_xfer_int(d
)) {
94 switch (to_usb_device(ddev
)->speed
) {
96 n
= 9; /* 32 ms = 2^(9-1) uframes */
99 default: /* USB_SPEED_FULL or _LOW */
100 /* For low-speed, 10 ms is the official minimum.
101 * But some "overclocked" devices might want faster
102 * polling so we'll allow it. */
106 } else if (usb_endpoint_xfer_isoc(d
)) {
109 switch (to_usb_device(ddev
)->speed
) {
111 n
= 9; /* 32 ms = 2^(9-1) uframes */
113 default: /* USB_SPEED_FULL */
114 n
= 6; /* 32 ms = 2^(6-1) frames */
118 if (d
->bInterval
< i
|| d
->bInterval
> j
) {
119 dev_warn(ddev
, "config %d interface %d altsetting %d "
120 "endpoint 0x%X has an invalid bInterval %d, "
123 d
->bEndpointAddress
, d
->bInterval
, n
);
124 endpoint
->desc
.bInterval
= n
;
127 /* Skip over any Class Specific or Vendor Specific descriptors;
128 * find the next endpoint or interface descriptor */
129 endpoint
->extra
= buffer
;
130 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
131 USB_DT_INTERFACE
, &n
);
132 endpoint
->extralen
= i
;
134 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
135 n
, plural(n
), "endpoint");
136 return buffer
- buffer0
+ i
;
138 skip_to_next_endpoint_or_interface_descriptor
:
139 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
140 USB_DT_INTERFACE
, NULL
);
141 return buffer
- buffer0
+ i
;
144 void usb_release_interface_cache(struct kref
*ref
)
146 struct usb_interface_cache
*intfc
= ref_to_usb_interface_cache(ref
);
149 for (j
= 0; j
< intfc
->num_altsetting
; j
++) {
150 struct usb_host_interface
*alt
= &intfc
->altsetting
[j
];
152 kfree(alt
->endpoint
);
158 static int usb_parse_interface(struct device
*ddev
, int cfgno
,
159 struct usb_host_config
*config
, unsigned char *buffer
, int size
,
160 u8 inums
[], u8 nalts
[])
162 unsigned char *buffer0
= buffer
;
163 struct usb_interface_descriptor
*d
;
165 struct usb_interface_cache
*intfc
;
166 struct usb_host_interface
*alt
;
169 int num_ep
, num_ep_orig
;
171 d
= (struct usb_interface_descriptor
*) buffer
;
172 buffer
+= d
->bLength
;
175 if (d
->bLength
< USB_DT_INTERFACE_SIZE
)
176 goto skip_to_next_interface_descriptor
;
178 /* Which interface entry is this? */
180 inum
= d
->bInterfaceNumber
;
181 for (i
= 0; i
< config
->desc
.bNumInterfaces
; ++i
) {
182 if (inums
[i
] == inum
) {
183 intfc
= config
->intf_cache
[i
];
187 if (!intfc
|| intfc
->num_altsetting
>= nalts
[i
])
188 goto skip_to_next_interface_descriptor
;
190 /* Check for duplicate altsetting entries */
191 asnum
= d
->bAlternateSetting
;
192 for ((i
= 0, alt
= &intfc
->altsetting
[0]);
193 i
< intfc
->num_altsetting
;
195 if (alt
->desc
.bAlternateSetting
== asnum
) {
196 dev_warn(ddev
, "Duplicate descriptor for config %d "
197 "interface %d altsetting %d, skipping\n",
199 goto skip_to_next_interface_descriptor
;
203 ++intfc
->num_altsetting
;
204 memcpy(&alt
->desc
, d
, USB_DT_INTERFACE_SIZE
);
206 /* Skip over any Class Specific or Vendor Specific descriptors;
207 * find the first endpoint or interface descriptor */
209 i
= find_next_descriptor(buffer
, size
, USB_DT_ENDPOINT
,
210 USB_DT_INTERFACE
, &n
);
213 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
214 n
, plural(n
), "interface");
218 /* Allocate space for the right(?) number of endpoints */
219 num_ep
= num_ep_orig
= alt
->desc
.bNumEndpoints
;
220 alt
->desc
.bNumEndpoints
= 0; // Use as a counter
221 if (num_ep
> USB_MAXENDPOINTS
) {
222 dev_warn(ddev
, "too many endpoints for config %d interface %d "
223 "altsetting %d: %d, using maximum allowed: %d\n",
224 cfgno
, inum
, asnum
, num_ep
, USB_MAXENDPOINTS
);
225 num_ep
= USB_MAXENDPOINTS
;
228 if (num_ep
> 0) { /* Can't allocate 0 bytes */
229 len
= sizeof(struct usb_host_endpoint
) * num_ep
;
230 alt
->endpoint
= kzalloc(len
, GFP_KERNEL
);
235 /* Parse all the endpoint descriptors */
238 if (((struct usb_descriptor_header
*) buffer
)->bDescriptorType
241 retval
= usb_parse_endpoint(ddev
, cfgno
, inum
, asnum
, alt
,
242 num_ep
, buffer
, size
);
251 if (n
!= num_ep_orig
)
252 dev_warn(ddev
, "config %d interface %d altsetting %d has %d "
253 "endpoint descriptor%s, different from the interface "
254 "descriptor's value: %d\n",
255 cfgno
, inum
, asnum
, n
, plural(n
), num_ep_orig
);
256 return buffer
- buffer0
;
258 skip_to_next_interface_descriptor
:
259 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
260 USB_DT_INTERFACE
, NULL
);
261 return buffer
- buffer0
+ i
;
264 static int usb_parse_configuration(struct device
*ddev
, int cfgidx
,
265 struct usb_host_config
*config
, unsigned char *buffer
, int size
)
267 unsigned char *buffer0
= buffer
;
269 int nintf
, nintf_orig
;
271 struct usb_interface_cache
*intfc
;
272 unsigned char *buffer2
;
274 struct usb_descriptor_header
*header
;
276 u8 inums
[USB_MAXINTERFACES
], nalts
[USB_MAXINTERFACES
];
278 memcpy(&config
->desc
, buffer
, USB_DT_CONFIG_SIZE
);
279 if (config
->desc
.bDescriptorType
!= USB_DT_CONFIG
||
280 config
->desc
.bLength
< USB_DT_CONFIG_SIZE
) {
281 dev_err(ddev
, "invalid descriptor for config index %d: "
282 "type = 0x%X, length = %d\n", cfgidx
,
283 config
->desc
.bDescriptorType
, config
->desc
.bLength
);
286 cfgno
= config
->desc
.bConfigurationValue
;
288 buffer
+= config
->desc
.bLength
;
289 size
-= config
->desc
.bLength
;
291 nintf
= nintf_orig
= config
->desc
.bNumInterfaces
;
292 if (nintf
> USB_MAXINTERFACES
) {
293 dev_warn(ddev
, "config %d has too many interfaces: %d, "
294 "using maximum allowed: %d\n",
295 cfgno
, nintf
, USB_MAXINTERFACES
);
296 nintf
= USB_MAXINTERFACES
;
299 /* Go through the descriptors, checking their length and counting the
300 * number of altsettings for each interface */
302 for ((buffer2
= buffer
, size2
= size
);
304 (buffer2
+= header
->bLength
, size2
-= header
->bLength
)) {
306 if (size2
< sizeof(struct usb_descriptor_header
)) {
307 dev_warn(ddev
, "config %d descriptor has %d excess "
308 "byte%s, ignoring\n",
309 cfgno
, size2
, plural(size2
));
313 header
= (struct usb_descriptor_header
*) buffer2
;
314 if ((header
->bLength
> size2
) || (header
->bLength
< 2)) {
315 dev_warn(ddev
, "config %d has an invalid descriptor "
316 "of length %d, skipping remainder of the config\n",
317 cfgno
, header
->bLength
);
321 if (header
->bDescriptorType
== USB_DT_INTERFACE
) {
322 struct usb_interface_descriptor
*d
;
325 d
= (struct usb_interface_descriptor
*) header
;
326 if (d
->bLength
< USB_DT_INTERFACE_SIZE
) {
327 dev_warn(ddev
, "config %d has an invalid "
328 "interface descriptor of length %d, "
329 "skipping\n", cfgno
, d
->bLength
);
333 inum
= d
->bInterfaceNumber
;
334 if (inum
>= nintf_orig
)
335 dev_warn(ddev
, "config %d has an invalid "
336 "interface number: %d but max is %d\n",
337 cfgno
, inum
, nintf_orig
- 1);
339 /* Have we already encountered this interface?
340 * Count its altsettings */
341 for (i
= 0; i
< n
; ++i
) {
342 if (inums
[i
] == inum
)
348 } else if (n
< USB_MAXINTERFACES
) {
354 } else if (header
->bDescriptorType
== USB_DT_DEVICE
||
355 header
->bDescriptorType
== USB_DT_CONFIG
)
356 dev_warn(ddev
, "config %d contains an unexpected "
357 "descriptor of type 0x%X, skipping\n",
358 cfgno
, header
->bDescriptorType
);
360 } /* for ((buffer2 = buffer, size2 = size); ...) */
361 size
= buffer2
- buffer
;
362 config
->desc
.wTotalLength
= cpu_to_le16(buffer2
- buffer0
);
365 dev_warn(ddev
, "config %d has %d interface%s, different from "
366 "the descriptor's value: %d\n",
367 cfgno
, n
, plural(n
), nintf_orig
);
369 dev_warn(ddev
, "config %d has no interfaces?\n", cfgno
);
370 config
->desc
.bNumInterfaces
= nintf
= n
;
372 /* Check for missing interface numbers */
373 for (i
= 0; i
< nintf
; ++i
) {
374 for (j
= 0; j
< nintf
; ++j
) {
379 dev_warn(ddev
, "config %d has no interface number "
383 /* Allocate the usb_interface_caches and altsetting arrays */
384 for (i
= 0; i
< nintf
; ++i
) {
386 if (j
> USB_MAXALTSETTING
) {
387 dev_warn(ddev
, "too many alternate settings for "
388 "config %d interface %d: %d, "
389 "using maximum allowed: %d\n",
390 cfgno
, inums
[i
], j
, USB_MAXALTSETTING
);
391 nalts
[i
] = j
= USB_MAXALTSETTING
;
394 len
= sizeof(*intfc
) + sizeof(struct usb_host_interface
) * j
;
395 config
->intf_cache
[i
] = intfc
= kzalloc(len
, GFP_KERNEL
);
398 kref_init(&intfc
->ref
);
401 /* Skip over any Class Specific or Vendor Specific descriptors;
402 * find the first interface descriptor */
403 config
->extra
= buffer
;
404 i
= find_next_descriptor(buffer
, size
, USB_DT_INTERFACE
,
405 USB_DT_INTERFACE
, &n
);
406 config
->extralen
= i
;
408 dev_dbg(ddev
, "skipped %d descriptor%s after %s\n",
409 n
, plural(n
), "configuration");
413 /* Parse all the interface/altsetting descriptors */
415 retval
= usb_parse_interface(ddev
, cfgno
, config
,
416 buffer
, size
, inums
, nalts
);
424 /* Check for missing altsettings */
425 for (i
= 0; i
< nintf
; ++i
) {
426 intfc
= config
->intf_cache
[i
];
427 for (j
= 0; j
< intfc
->num_altsetting
; ++j
) {
428 for (n
= 0; n
< intfc
->num_altsetting
; ++n
) {
429 if (intfc
->altsetting
[n
].desc
.
430 bAlternateSetting
== j
)
433 if (n
>= intfc
->num_altsetting
)
434 dev_warn(ddev
, "config %d interface %d has no "
435 "altsetting %d\n", cfgno
, inums
[i
], j
);
442 // hub-only!! ... and only exported for reset/reinit path.
443 // otherwise used internally on disconnect/destroy path
444 void usb_destroy_configuration(struct usb_device
*dev
)
451 if (dev
->rawdescriptors
) {
452 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++)
453 kfree(dev
->rawdescriptors
[i
]);
455 kfree(dev
->rawdescriptors
);
456 dev
->rawdescriptors
= NULL
;
459 for (c
= 0; c
< dev
->descriptor
.bNumConfigurations
; c
++) {
460 struct usb_host_config
*cf
= &dev
->config
[c
];
463 for (i
= 0; i
< cf
->desc
.bNumInterfaces
; i
++) {
464 if (cf
->intf_cache
[i
])
465 kref_put(&cf
->intf_cache
[i
]->ref
,
466 usb_release_interface_cache
);
474 // hub-only!! ... and only in reset path, or usb_new_device()
475 // (used by real hubs and virtual root hubs)
476 int usb_get_configuration(struct usb_device
*dev
)
478 struct device
*ddev
= &dev
->dev
;
479 int ncfg
= dev
->descriptor
.bNumConfigurations
;
480 int result
= -ENOMEM
;
481 unsigned int cfgno
, length
;
482 unsigned char *buffer
;
483 unsigned char *bigbuffer
;
484 struct usb_config_descriptor
*desc
;
486 if (ncfg
> USB_MAXCONFIG
) {
487 dev_warn(ddev
, "too many configurations: %d, "
488 "using maximum allowed: %d\n", ncfg
, USB_MAXCONFIG
);
489 dev
->descriptor
.bNumConfigurations
= ncfg
= USB_MAXCONFIG
;
493 dev_err(ddev
, "no configurations\n");
497 length
= ncfg
* sizeof(struct usb_host_config
);
498 dev
->config
= kzalloc(length
, GFP_KERNEL
);
502 length
= ncfg
* sizeof(char *);
503 dev
->rawdescriptors
= kzalloc(length
, GFP_KERNEL
);
504 if (!dev
->rawdescriptors
)
507 buffer
= kmalloc(USB_DT_CONFIG_SIZE
, GFP_KERNEL
);
510 desc
= (struct usb_config_descriptor
*)buffer
;
512 for (cfgno
= 0; cfgno
< ncfg
; cfgno
++) {
513 /* We grab just the first descriptor so we know how long
514 * the whole configuration is */
515 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
516 buffer
, USB_DT_CONFIG_SIZE
);
518 dev_err(ddev
, "unable to read config index %d "
519 "descriptor/%s\n", cfgno
, "start");
520 dev_err(ddev
, "chopping to %d config(s)\n", cfgno
);
521 dev
->descriptor
.bNumConfigurations
= cfgno
;
523 } else if (result
< 4) {
524 dev_err(ddev
, "config index %d descriptor too short "
525 "(expected %i, got %i)\n", cfgno
,
526 USB_DT_CONFIG_SIZE
, result
);
530 length
= max((int) le16_to_cpu(desc
->wTotalLength
),
533 /* Now that we know the length, get the whole thing */
534 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
539 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
,
542 dev_err(ddev
, "unable to read config index %d "
543 "descriptor/%s\n", cfgno
, "all");
547 if (result
< length
) {
548 dev_warn(ddev
, "config index %d descriptor too short "
549 "(expected %i, got %i)\n", cfgno
, length
, result
);
553 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
555 result
= usb_parse_configuration(&dev
->dev
, cfgno
,
556 &dev
->config
[cfgno
], bigbuffer
, length
);
566 dev
->descriptor
.bNumConfigurations
= cfgno
;
568 if (result
== -ENOMEM
)
569 dev_err(ddev
, "out of memory\n");