Merge tag 'v3.10.95' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / core / config.c
CommitLineData
1da177e4 1#include <linux/usb.h>
615ae11b 2#include <linux/usb/ch9.h>
27729aad 3#include <linux/usb/hcd.h>
317149c6 4#include <linux/usb/quirks.h>
1da177e4
LT
5#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/slab.h>
8#include <linux/device.h>
9#include <asm/byteorder.h>
6d5e8254 10#include "usb.h"
27729aad 11
1da177e4
LT
12
13#define USB_MAXALTSETTING 128 /* Hard limit */
14#define USB_MAXENDPOINTS 30 /* Hard limit */
15
16#define USB_MAXCONFIG 8 /* Arbitrary limit */
17
18
19static inline const char *plural(int n)
20{
21 return (n == 1 ? "" : "s");
22}
23
24static int find_next_descriptor(unsigned char *buffer, int size,
25 int dt1, int dt2, int *num_skipped)
26{
27 struct usb_descriptor_header *h;
28 int n = 0;
29 unsigned char *buffer0 = buffer;
30
31 /* Find the next descriptor of type dt1 or dt2 */
32 while (size > 0) {
33 h = (struct usb_descriptor_header *) buffer;
34 if (h->bDescriptorType == dt1 || h->bDescriptorType == dt2)
35 break;
36 buffer += h->bLength;
37 size -= h->bLength;
38 ++n;
39 }
40
41 /* Store the number of descriptors skipped and return the
42 * number of bytes skipped */
43 if (num_skipped)
44 *num_skipped = n;
45 return buffer - buffer0;
46}
47
842f1690 48static void usb_parse_ss_endpoint_companion(struct device *ddev, int cfgno,
663c30d0 49 int inum, int asnum, struct usb_host_endpoint *ep,
842f1690 50 unsigned char *buffer, int size)
663c30d0 51{
842f1690 52 struct usb_ss_ep_comp_descriptor *desc;
663c30d0 53 int max_tx;
663c30d0 54
842f1690
AS
55 /* The SuperSpeed endpoint companion descriptor is supposed to
56 * be the first thing immediately following the endpoint descriptor.
57 */
f0058c62 58 desc = (struct usb_ss_ep_comp_descriptor *) buffer;
842f1690
AS
59 if (desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP ||
60 size < USB_DT_SS_EP_COMP_SIZE) {
663c30d0
SS
61 dev_warn(ddev, "No SuperSpeed endpoint companion for config %d "
62 " interface %d altsetting %d ep %d: "
63 "using minimum values\n",
64 cfgno, inum, asnum, ep->desc.bEndpointAddress);
842f1690
AS
65
66 /* Fill in some default values.
67 * Leave bmAttributes as zero, which will mean no streams for
68 * bulk, and isoc won't support multiple bursts of packets.
69 * With bursts of only one packet, and a Mult of 1, the max
70 * amount of data moved per endpoint service interval is one
71 * packet.
663c30d0 72 */
842f1690
AS
73 ep->ss_ep_comp.bLength = USB_DT_SS_EP_COMP_SIZE;
74 ep->ss_ep_comp.bDescriptorType = USB_DT_SS_ENDPOINT_COMP;
75 if (usb_endpoint_xfer_isoc(&ep->desc) ||
76 usb_endpoint_xfer_int(&ep->desc))
77 ep->ss_ep_comp.wBytesPerInterval =
78 ep->desc.wMaxPacketSize;
79 return;
663c30d0 80 }
663c30d0 81
842f1690 82 memcpy(&ep->ss_ep_comp, desc, USB_DT_SS_EP_COMP_SIZE);
663c30d0
SS
83
84 /* Check the various values */
85 if (usb_endpoint_xfer_control(&ep->desc) && desc->bMaxBurst != 0) {
86 dev_warn(ddev, "Control endpoint with bMaxBurst = %d in "
87 "config %d interface %d altsetting %d ep %d: "
88 "setting to zero\n", desc->bMaxBurst,
89 cfgno, inum, asnum, ep->desc.bEndpointAddress);
842f1690
AS
90 ep->ss_ep_comp.bMaxBurst = 0;
91 } else if (desc->bMaxBurst > 15) {
663c30d0
SS
92 dev_warn(ddev, "Endpoint with bMaxBurst = %d in "
93 "config %d interface %d altsetting %d ep %d: "
94 "setting to 15\n", desc->bMaxBurst,
95 cfgno, inum, asnum, ep->desc.bEndpointAddress);
842f1690 96 ep->ss_ep_comp.bMaxBurst = 15;
663c30d0 97 }
842f1690
AS
98
99 if ((usb_endpoint_xfer_control(&ep->desc) ||
100 usb_endpoint_xfer_int(&ep->desc)) &&
101 desc->bmAttributes != 0) {
663c30d0
SS
102 dev_warn(ddev, "%s endpoint with bmAttributes = %d in "
103 "config %d interface %d altsetting %d ep %d: "
104 "setting to zero\n",
105 usb_endpoint_xfer_control(&ep->desc) ? "Control" : "Bulk",
106 desc->bmAttributes,
107 cfgno, inum, asnum, ep->desc.bEndpointAddress);
842f1690
AS
108 ep->ss_ep_comp.bmAttributes = 0;
109 } else if (usb_endpoint_xfer_bulk(&ep->desc) &&
110 desc->bmAttributes > 16) {
663c30d0
SS
111 dev_warn(ddev, "Bulk endpoint with more than 65536 streams in "
112 "config %d interface %d altsetting %d ep %d: "
113 "setting to max\n",
114 cfgno, inum, asnum, ep->desc.bEndpointAddress);
842f1690
AS
115 ep->ss_ep_comp.bmAttributes = 16;
116 } else if (usb_endpoint_xfer_isoc(&ep->desc) &&
560fac95 117 USB_SS_MULT(desc->bmAttributes) > 3) {
663c30d0
SS
118 dev_warn(ddev, "Isoc endpoint has Mult of %d in "
119 "config %d interface %d altsetting %d ep %d: "
7541d74e
BH
120 "setting to 3\n",
121 USB_SS_MULT(desc->bmAttributes),
663c30d0 122 cfgno, inum, asnum, ep->desc.bEndpointAddress);
842f1690 123 ep->ss_ep_comp.bmAttributes = 2;
663c30d0 124 }
842f1690
AS
125
126 if (usb_endpoint_xfer_isoc(&ep->desc))
560fac95
MN
127 max_tx = (desc->bMaxBurst + 1) *
128 (USB_SS_MULT(desc->bmAttributes)) *
29cc8897 129 usb_endpoint_maxp(&ep->desc);
842f1690 130 else if (usb_endpoint_xfer_int(&ep->desc))
29cc8897 131 max_tx = usb_endpoint_maxp(&ep->desc) *
7de7c7d2 132 (desc->bMaxBurst + 1);
842f1690
AS
133 else
134 max_tx = 999999;
64b3c304 135 if (le16_to_cpu(desc->wBytesPerInterval) > max_tx) {
663c30d0
SS
136 dev_warn(ddev, "%s endpoint with wBytesPerInterval of %d in "
137 "config %d interface %d altsetting %d ep %d: "
138 "setting to %d\n",
139 usb_endpoint_xfer_isoc(&ep->desc) ? "Isoc" : "Int",
7de7c7d2 140 le16_to_cpu(desc->wBytesPerInterval),
663c30d0
SS
141 cfgno, inum, asnum, ep->desc.bEndpointAddress,
142 max_tx);
7de7c7d2 143 ep->ss_ep_comp.wBytesPerInterval = cpu_to_le16(max_tx);
663c30d0 144 }
663c30d0
SS
145}
146
1da177e4
LT
147static int usb_parse_endpoint(struct device *ddev, int cfgno, int inum,
148 int asnum, struct usb_host_interface *ifp, int num_ep,
149 unsigned char *buffer, int size)
150{
151 unsigned char *buffer0 = buffer;
152 struct usb_endpoint_descriptor *d;
153 struct usb_host_endpoint *endpoint;
663c30d0 154 int n, i, j, retval;
1da177e4
LT
155
156 d = (struct usb_endpoint_descriptor *) buffer;
157 buffer += d->bLength;
158 size -= d->bLength;
159
160 if (d->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE)
161 n = USB_DT_ENDPOINT_AUDIO_SIZE;
162 else if (d->bLength >= USB_DT_ENDPOINT_SIZE)
163 n = USB_DT_ENDPOINT_SIZE;
164 else {
165 dev_warn(ddev, "config %d interface %d altsetting %d has an "
166 "invalid endpoint descriptor of length %d, skipping\n",
167 cfgno, inum, asnum, d->bLength);
168 goto skip_to_next_endpoint_or_interface_descriptor;
169 }
170
171 i = d->bEndpointAddress & ~USB_ENDPOINT_DIR_MASK;
172 if (i >= 16 || i == 0) {
173 dev_warn(ddev, "config %d interface %d altsetting %d has an "
174 "invalid endpoint with address 0x%X, skipping\n",
175 cfgno, inum, asnum, d->bEndpointAddress);
176 goto skip_to_next_endpoint_or_interface_descriptor;
177 }
178
179 /* Only store as many endpoints as we have room for */
180 if (ifp->desc.bNumEndpoints >= num_ep)
181 goto skip_to_next_endpoint_or_interface_descriptor;
182
183 endpoint = &ifp->endpoint[ifp->desc.bNumEndpoints];
184 ++ifp->desc.bNumEndpoints;
185
186 memcpy(&endpoint->desc, d, n);
187 INIT_LIST_HEAD(&endpoint->urb_list);
188
300871cd
LP
189 /* Fix up bInterval values outside the legal range. Use 32 ms if no
190 * proper value can be guessed. */
615ae11b
AS
191 i = 0; /* i = min, j = max, n = default */
192 j = 255;
193 if (usb_endpoint_xfer_int(d)) {
194 i = 1;
195 switch (to_usb_device(ddev)->speed) {
6b403b02 196 case USB_SPEED_SUPER:
615ae11b 197 case USB_SPEED_HIGH:
300871cd
LP
198 /* Many device manufacturers are using full-speed
199 * bInterval values in high-speed interrupt endpoint
200 * descriptors. Try to fix those and fall back to a
201 * 32 ms default value otherwise. */
202 n = fls(d->bInterval*8);
203 if (n == 0)
204 n = 9; /* 32 ms = 2^(9-1) uframes */
615ae11b
AS
205 j = 16;
206 break;
207 default: /* USB_SPEED_FULL or _LOW */
208 /* For low-speed, 10 ms is the official minimum.
209 * But some "overclocked" devices might want faster
210 * polling so we'll allow it. */
211 n = 32;
212 break;
213 }
214 } else if (usb_endpoint_xfer_isoc(d)) {
215 i = 1;
216 j = 16;
217 switch (to_usb_device(ddev)->speed) {
218 case USB_SPEED_HIGH:
219 n = 9; /* 32 ms = 2^(9-1) uframes */
220 break;
221 default: /* USB_SPEED_FULL */
222 n = 6; /* 32 ms = 2^(6-1) frames */
223 break;
224 }
225 }
226 if (d->bInterval < i || d->bInterval > j) {
227 dev_warn(ddev, "config %d interface %d altsetting %d "
228 "endpoint 0x%X has an invalid bInterval %d, "
229 "changing to %d\n",
230 cfgno, inum, asnum,
231 d->bEndpointAddress, d->bInterval, n);
232 endpoint->desc.bInterval = n;
233 }
234
60aac1ec
AS
235 /* Some buggy low-speed devices have Bulk endpoints, which is
236 * explicitly forbidden by the USB spec. In an attempt to make
237 * them usable, we will try treating them as Interrupt endpoints.
238 */
239 if (to_usb_device(ddev)->speed == USB_SPEED_LOW &&
240 usb_endpoint_xfer_bulk(d)) {
241 dev_warn(ddev, "config %d interface %d altsetting %d "
242 "endpoint 0x%X is Bulk; changing to Interrupt\n",
243 cfgno, inum, asnum, d->bEndpointAddress);
244 endpoint->desc.bmAttributes = USB_ENDPOINT_XFER_INT;
245 endpoint->desc.bInterval = 1;
29cc8897 246 if (usb_endpoint_maxp(&endpoint->desc) > 8)
60aac1ec
AS
247 endpoint->desc.wMaxPacketSize = cpu_to_le16(8);
248 }
249
caa9ef67
DB
250 /*
251 * Some buggy high speed devices have bulk endpoints using
252 * maxpacket sizes other than 512. High speed HCDs may not
253 * be able to handle that particular bug, so let's warn...
254 */
255 if (to_usb_device(ddev)->speed == USB_SPEED_HIGH
256 && usb_endpoint_xfer_bulk(d)) {
257 unsigned maxp;
258
29cc8897 259 maxp = usb_endpoint_maxp(&endpoint->desc) & 0x07ff;
caa9ef67
DB
260 if (maxp != 512)
261 dev_warn(ddev, "config %d interface %d altsetting %d "
262 "bulk endpoint 0x%X has invalid maxpacket %d\n",
263 cfgno, inum, asnum, d->bEndpointAddress,
264 maxp);
265 }
663c30d0 266
842f1690
AS
267 /* Parse a possible SuperSpeed endpoint companion descriptor */
268 if (to_usb_device(ddev)->speed == USB_SPEED_SUPER)
269 usb_parse_ss_endpoint_companion(ddev, cfgno,
270 inum, asnum, endpoint, buffer, size);
9f8e4438 271
842f1690
AS
272 /* Skip over any Class Specific or Vendor Specific descriptors;
273 * find the next endpoint or interface descriptor */
274 endpoint->extra = buffer;
275 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
276 USB_DT_INTERFACE, &n);
277 endpoint->extralen = i;
278 retval = buffer - buffer0 + i;
1da177e4
LT
279 if (n > 0)
280 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
281 n, plural(n), "endpoint");
663c30d0 282 return retval;
1da177e4
LT
283
284skip_to_next_endpoint_or_interface_descriptor:
285 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
286 USB_DT_INTERFACE, NULL);
287 return buffer - buffer0 + i;
288}
289
290void usb_release_interface_cache(struct kref *ref)
291{
292 struct usb_interface_cache *intfc = ref_to_usb_interface_cache(ref);
293 int j;
294
4f62efe6
AS
295 for (j = 0; j < intfc->num_altsetting; j++) {
296 struct usb_host_interface *alt = &intfc->altsetting[j];
297
298 kfree(alt->endpoint);
299 kfree(alt->string);
300 }
1da177e4
LT
301 kfree(intfc);
302}
303
304static int usb_parse_interface(struct device *ddev, int cfgno,
305 struct usb_host_config *config, unsigned char *buffer, int size,
306 u8 inums[], u8 nalts[])
307{
308 unsigned char *buffer0 = buffer;
309 struct usb_interface_descriptor *d;
310 int inum, asnum;
311 struct usb_interface_cache *intfc;
312 struct usb_host_interface *alt;
313 int i, n;
314 int len, retval;
315 int num_ep, num_ep_orig;
316
317 d = (struct usb_interface_descriptor *) buffer;
318 buffer += d->bLength;
319 size -= d->bLength;
320
321 if (d->bLength < USB_DT_INTERFACE_SIZE)
322 goto skip_to_next_interface_descriptor;
323
324 /* Which interface entry is this? */
325 intfc = NULL;
326 inum = d->bInterfaceNumber;
327 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
328 if (inums[i] == inum) {
329 intfc = config->intf_cache[i];
330 break;
331 }
332 }
333 if (!intfc || intfc->num_altsetting >= nalts[i])
334 goto skip_to_next_interface_descriptor;
335
336 /* Check for duplicate altsetting entries */
337 asnum = d->bAlternateSetting;
338 for ((i = 0, alt = &intfc->altsetting[0]);
339 i < intfc->num_altsetting;
340 (++i, ++alt)) {
341 if (alt->desc.bAlternateSetting == asnum) {
342 dev_warn(ddev, "Duplicate descriptor for config %d "
343 "interface %d altsetting %d, skipping\n",
344 cfgno, inum, asnum);
345 goto skip_to_next_interface_descriptor;
346 }
347 }
348
349 ++intfc->num_altsetting;
350 memcpy(&alt->desc, d, USB_DT_INTERFACE_SIZE);
351
352 /* Skip over any Class Specific or Vendor Specific descriptors;
353 * find the first endpoint or interface descriptor */
354 alt->extra = buffer;
355 i = find_next_descriptor(buffer, size, USB_DT_ENDPOINT,
356 USB_DT_INTERFACE, &n);
357 alt->extralen = i;
358 if (n > 0)
359 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
360 n, plural(n), "interface");
361 buffer += i;
362 size -= i;
363
364 /* Allocate space for the right(?) number of endpoints */
365 num_ep = num_ep_orig = alt->desc.bNumEndpoints;
2c044a48 366 alt->desc.bNumEndpoints = 0; /* Use as a counter */
1da177e4
LT
367 if (num_ep > USB_MAXENDPOINTS) {
368 dev_warn(ddev, "too many endpoints for config %d interface %d "
369 "altsetting %d: %d, using maximum allowed: %d\n",
370 cfgno, inum, asnum, num_ep, USB_MAXENDPOINTS);
371 num_ep = USB_MAXENDPOINTS;
372 }
373
2c044a48
GKH
374 if (num_ep > 0) {
375 /* Can't allocate 0 bytes */
57a21c1b
AS
376 len = sizeof(struct usb_host_endpoint) * num_ep;
377 alt->endpoint = kzalloc(len, GFP_KERNEL);
378 if (!alt->endpoint)
379 return -ENOMEM;
380 }
1da177e4
LT
381
382 /* Parse all the endpoint descriptors */
383 n = 0;
384 while (size > 0) {
385 if (((struct usb_descriptor_header *) buffer)->bDescriptorType
386 == USB_DT_INTERFACE)
387 break;
388 retval = usb_parse_endpoint(ddev, cfgno, inum, asnum, alt,
389 num_ep, buffer, size);
390 if (retval < 0)
391 return retval;
392 ++n;
393
394 buffer += retval;
395 size -= retval;
396 }
397
398 if (n != num_ep_orig)
399 dev_warn(ddev, "config %d interface %d altsetting %d has %d "
400 "endpoint descriptor%s, different from the interface "
401 "descriptor's value: %d\n",
402 cfgno, inum, asnum, n, plural(n), num_ep_orig);
403 return buffer - buffer0;
404
405skip_to_next_interface_descriptor:
406 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
407 USB_DT_INTERFACE, NULL);
408 return buffer - buffer0 + i;
409}
410
317149c6 411static int usb_parse_configuration(struct usb_device *dev, int cfgidx,
1da177e4
LT
412 struct usb_host_config *config, unsigned char *buffer, int size)
413{
317149c6 414 struct device *ddev = &dev->dev;
1da177e4
LT
415 unsigned char *buffer0 = buffer;
416 int cfgno;
417 int nintf, nintf_orig;
418 int i, j, n;
419 struct usb_interface_cache *intfc;
420 unsigned char *buffer2;
421 int size2;
422 struct usb_descriptor_header *header;
423 int len, retval;
424 u8 inums[USB_MAXINTERFACES], nalts[USB_MAXINTERFACES];
165fe97e 425 unsigned iad_num = 0;
1da177e4
LT
426
427 memcpy(&config->desc, buffer, USB_DT_CONFIG_SIZE);
428 if (config->desc.bDescriptorType != USB_DT_CONFIG ||
ef7198be
HG
429 config->desc.bLength < USB_DT_CONFIG_SIZE ||
430 config->desc.bLength > size) {
1da177e4
LT
431 dev_err(ddev, "invalid descriptor for config index %d: "
432 "type = 0x%X, length = %d\n", cfgidx,
433 config->desc.bDescriptorType, config->desc.bLength);
434 return -EINVAL;
435 }
436 cfgno = config->desc.bConfigurationValue;
437
438 buffer += config->desc.bLength;
439 size -= config->desc.bLength;
440
441 nintf = nintf_orig = config->desc.bNumInterfaces;
442 if (nintf > USB_MAXINTERFACES) {
443 dev_warn(ddev, "config %d has too many interfaces: %d, "
444 "using maximum allowed: %d\n",
445 cfgno, nintf, USB_MAXINTERFACES);
446 nintf = USB_MAXINTERFACES;
447 }
448
449 /* Go through the descriptors, checking their length and counting the
450 * number of altsettings for each interface */
451 n = 0;
452 for ((buffer2 = buffer, size2 = size);
453 size2 > 0;
454 (buffer2 += header->bLength, size2 -= header->bLength)) {
455
456 if (size2 < sizeof(struct usb_descriptor_header)) {
457 dev_warn(ddev, "config %d descriptor has %d excess "
458 "byte%s, ignoring\n",
459 cfgno, size2, plural(size2));
460 break;
461 }
462
463 header = (struct usb_descriptor_header *) buffer2;
464 if ((header->bLength > size2) || (header->bLength < 2)) {
465 dev_warn(ddev, "config %d has an invalid descriptor "
466 "of length %d, skipping remainder of the config\n",
467 cfgno, header->bLength);
468 break;
469 }
470
471 if (header->bDescriptorType == USB_DT_INTERFACE) {
472 struct usb_interface_descriptor *d;
473 int inum;
474
475 d = (struct usb_interface_descriptor *) header;
476 if (d->bLength < USB_DT_INTERFACE_SIZE) {
477 dev_warn(ddev, "config %d has an invalid "
478 "interface descriptor of length %d, "
479 "skipping\n", cfgno, d->bLength);
480 continue;
481 }
482
483 inum = d->bInterfaceNumber;
317149c6
HG
484
485 if ((dev->quirks & USB_QUIRK_HONOR_BNUMINTERFACES) &&
486 n >= nintf_orig) {
487 dev_warn(ddev, "config %d has more interface "
488 "descriptors, than it declares in "
489 "bNumInterfaces, ignoring interface "
490 "number: %d\n", cfgno, inum);
491 continue;
492 }
493
1da177e4
LT
494 if (inum >= nintf_orig)
495 dev_warn(ddev, "config %d has an invalid "
496 "interface number: %d but max is %d\n",
497 cfgno, inum, nintf_orig - 1);
498
499 /* Have we already encountered this interface?
500 * Count its altsettings */
501 for (i = 0; i < n; ++i) {
502 if (inums[i] == inum)
503 break;
504 }
505 if (i < n) {
506 if (nalts[i] < 255)
507 ++nalts[i];
508 } else if (n < USB_MAXINTERFACES) {
509 inums[n] = inum;
510 nalts[n] = 1;
511 ++n;
512 }
513
165fe97e
CN
514 } else if (header->bDescriptorType ==
515 USB_DT_INTERFACE_ASSOCIATION) {
516 if (iad_num == USB_MAXIADS) {
517 dev_warn(ddev, "found more Interface "
518 "Association Descriptors "
519 "than allocated for in "
520 "configuration %d\n", cfgno);
521 } else {
522 config->intf_assoc[iad_num] =
523 (struct usb_interface_assoc_descriptor
524 *)header;
525 iad_num++;
526 }
527
1da177e4
LT
528 } else if (header->bDescriptorType == USB_DT_DEVICE ||
529 header->bDescriptorType == USB_DT_CONFIG)
530 dev_warn(ddev, "config %d contains an unexpected "
531 "descriptor of type 0x%X, skipping\n",
532 cfgno, header->bDescriptorType);
533
534 } /* for ((buffer2 = buffer, size2 = size); ...) */
535 size = buffer2 - buffer;
536 config->desc.wTotalLength = cpu_to_le16(buffer2 - buffer0);
537
538 if (n != nintf)
539 dev_warn(ddev, "config %d has %d interface%s, different from "
540 "the descriptor's value: %d\n",
541 cfgno, n, plural(n), nintf_orig);
542 else if (n == 0)
543 dev_warn(ddev, "config %d has no interfaces?\n", cfgno);
544 config->desc.bNumInterfaces = nintf = n;
545
546 /* Check for missing interface numbers */
547 for (i = 0; i < nintf; ++i) {
548 for (j = 0; j < nintf; ++j) {
549 if (inums[j] == i)
550 break;
551 }
552 if (j >= nintf)
553 dev_warn(ddev, "config %d has no interface number "
554 "%d\n", cfgno, i);
555 }
556
557 /* Allocate the usb_interface_caches and altsetting arrays */
558 for (i = 0; i < nintf; ++i) {
559 j = nalts[i];
560 if (j > USB_MAXALTSETTING) {
561 dev_warn(ddev, "too many alternate settings for "
562 "config %d interface %d: %d, "
563 "using maximum allowed: %d\n",
564 cfgno, inums[i], j, USB_MAXALTSETTING);
565 nalts[i] = j = USB_MAXALTSETTING;
566 }
567
568 len = sizeof(*intfc) + sizeof(struct usb_host_interface) * j;
0a1ef3b5 569 config->intf_cache[i] = intfc = kzalloc(len, GFP_KERNEL);
1da177e4
LT
570 if (!intfc)
571 return -ENOMEM;
1da177e4
LT
572 kref_init(&intfc->ref);
573 }
574
663c30d0
SS
575 /* FIXME: parse the BOS descriptor */
576
1da177e4
LT
577 /* Skip over any Class Specific or Vendor Specific descriptors;
578 * find the first interface descriptor */
579 config->extra = buffer;
580 i = find_next_descriptor(buffer, size, USB_DT_INTERFACE,
581 USB_DT_INTERFACE, &n);
582 config->extralen = i;
583 if (n > 0)
584 dev_dbg(ddev, "skipped %d descriptor%s after %s\n",
585 n, plural(n), "configuration");
586 buffer += i;
587 size -= i;
588
589 /* Parse all the interface/altsetting descriptors */
590 while (size > 0) {
591 retval = usb_parse_interface(ddev, cfgno, config,
592 buffer, size, inums, nalts);
593 if (retval < 0)
594 return retval;
595
596 buffer += retval;
597 size -= retval;
598 }
599
600 /* Check for missing altsettings */
601 for (i = 0; i < nintf; ++i) {
602 intfc = config->intf_cache[i];
603 for (j = 0; j < intfc->num_altsetting; ++j) {
604 for (n = 0; n < intfc->num_altsetting; ++n) {
605 if (intfc->altsetting[n].desc.
606 bAlternateSetting == j)
607 break;
608 }
609 if (n >= intfc->num_altsetting)
610 dev_warn(ddev, "config %d interface %d has no "
611 "altsetting %d\n", cfgno, inums[i], j);
612 }
613 }
614
615 return 0;
616}
617
2c044a48
GKH
618/* hub-only!! ... and only exported for reset/reinit path.
619 * otherwise used internally on disconnect/destroy path
620 */
1da177e4
LT
621void usb_destroy_configuration(struct usb_device *dev)
622{
623 int c, i;
624
625 if (!dev->config)
626 return;
627
628 if (dev->rawdescriptors) {
629 for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
630 kfree(dev->rawdescriptors[i]);
631
632 kfree(dev->rawdescriptors);
633 dev->rawdescriptors = NULL;
634 }
635
636 for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
637 struct usb_host_config *cf = &dev->config[c];
638
639 kfree(cf->string);
1da177e4
LT
640 for (i = 0; i < cf->desc.bNumInterfaces; i++) {
641 if (cf->intf_cache[i])
2c044a48 642 kref_put(&cf->intf_cache[i]->ref,
1da177e4
LT
643 usb_release_interface_cache);
644 }
645 }
646 kfree(dev->config);
647 dev->config = NULL;
648}
649
650
1145065c
IPG
651/*
652 * Get the USB config descriptors, cache and parse'em
653 *
654 * hub-only!! ... and only in reset path, or usb_new_device()
655 * (used by real hubs and virtual root hubs)
1145065c 656 */
1da177e4
LT
657int usb_get_configuration(struct usb_device *dev)
658{
659 struct device *ddev = &dev->dev;
660 int ncfg = dev->descriptor.bNumConfigurations;
1145065c 661 int result = 0;
1da177e4 662 unsigned int cfgno, length;
1da177e4 663 unsigned char *bigbuffer;
2c044a48 664 struct usb_config_descriptor *desc;
1da177e4 665
1145065c 666 cfgno = 0;
1145065c 667 result = -ENOMEM;
1da177e4
LT
668 if (ncfg > USB_MAXCONFIG) {
669 dev_warn(ddev, "too many configurations: %d, "
670 "using maximum allowed: %d\n", ncfg, USB_MAXCONFIG);
671 dev->descriptor.bNumConfigurations = ncfg = USB_MAXCONFIG;
672 }
673
674 if (ncfg < 1) {
675 dev_err(ddev, "no configurations\n");
676 return -EINVAL;
677 }
678
679 length = ncfg * sizeof(struct usb_host_config);
0a1ef3b5 680 dev->config = kzalloc(length, GFP_KERNEL);
1da177e4
LT
681 if (!dev->config)
682 goto err2;
1da177e4
LT
683
684 length = ncfg * sizeof(char *);
0a1ef3b5 685 dev->rawdescriptors = kzalloc(length, GFP_KERNEL);
1da177e4
LT
686 if (!dev->rawdescriptors)
687 goto err2;
1da177e4 688
e8f4af30
MN
689 desc = kmalloc(USB_DT_CONFIG_SIZE, GFP_KERNEL);
690 if (!desc)
1da177e4 691 goto err2;
1da177e4 692
1145065c
IPG
693 result = 0;
694 for (; cfgno < ncfg; cfgno++) {
1da177e4
LT
695 /* We grab just the first descriptor so we know how long
696 * the whole configuration is */
697 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
e8f4af30 698 desc, USB_DT_CONFIG_SIZE);
1da177e4
LT
699 if (result < 0) {
700 dev_err(ddev, "unable to read config index %d "
1145065c 701 "descriptor/%s: %d\n", cfgno, "start", result);
3a22b872
LT
702 if (result != -EPIPE)
703 goto err;
cb4c8fe5
IPG
704 dev_err(ddev, "chopping to %d config(s)\n", cfgno);
705 dev->descriptor.bNumConfigurations = cfgno;
706 break;
1da177e4
LT
707 } else if (result < 4) {
708 dev_err(ddev, "config index %d descriptor too short "
709 "(expected %i, got %i)\n", cfgno,
710 USB_DT_CONFIG_SIZE, result);
711 result = -EINVAL;
712 goto err;
713 }
714 length = max((int) le16_to_cpu(desc->wTotalLength),
715 USB_DT_CONFIG_SIZE);
716
717 /* Now that we know the length, get the whole thing */
718 bigbuffer = kmalloc(length, GFP_KERNEL);
719 if (!bigbuffer) {
720 result = -ENOMEM;
721 goto err;
722 }
3ec34db2
JW
723
724 if (dev->quirks & USB_QUIRK_DELAY_INIT)
725 msleep(100);
726
1da177e4
LT
727 result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno,
728 bigbuffer, length);
729 if (result < 0) {
730 dev_err(ddev, "unable to read config index %d "
731 "descriptor/%s\n", cfgno, "all");
732 kfree(bigbuffer);
733 goto err;
734 }
735 if (result < length) {
736 dev_warn(ddev, "config index %d descriptor too short "
737 "(expected %i, got %i)\n", cfgno, length, result);
738 length = result;
739 }
740
741 dev->rawdescriptors[cfgno] = bigbuffer;
742
317149c6 743 result = usb_parse_configuration(dev, cfgno,
1da177e4
LT
744 &dev->config[cfgno], bigbuffer, length);
745 if (result < 0) {
746 ++cfgno;
747 goto err;
748 }
749 }
750 result = 0;
751
752err:
e8f4af30 753 kfree(desc);
1da177e4
LT
754 dev->descriptor.bNumConfigurations = cfgno;
755err2:
756 if (result == -ENOMEM)
757 dev_err(ddev, "out of memory\n");
758 return result;
759}
3148bf04
AX
760
761void usb_release_bos_descriptor(struct usb_device *dev)
762{
763 if (dev->bos) {
764 kfree(dev->bos->desc);
765 kfree(dev->bos);
766 dev->bos = NULL;
767 }
768}
769
770/* Get BOS descriptor set */
771int usb_get_bos_descriptor(struct usb_device *dev)
772{
773 struct device *ddev = &dev->dev;
774 struct usb_bos_descriptor *bos;
775 struct usb_dev_cap_header *cap;
776 unsigned char *buffer;
777 int length, total_len, num, i;
778 int ret;
779
780 bos = kzalloc(sizeof(struct usb_bos_descriptor), GFP_KERNEL);
781 if (!bos)
782 return -ENOMEM;
783
784 /* Get BOS descriptor */
785 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, bos, USB_DT_BOS_SIZE);
786 if (ret < USB_DT_BOS_SIZE) {
787 dev_err(ddev, "unable to get BOS descriptor\n");
788 if (ret >= 0)
789 ret = -ENOMSG;
790 kfree(bos);
791 return ret;
792 }
793
794 length = bos->bLength;
795 total_len = le16_to_cpu(bos->wTotalLength);
796 num = bos->bNumDeviceCaps;
797 kfree(bos);
798 if (total_len < length)
799 return -EINVAL;
800
801 dev->bos = kzalloc(sizeof(struct usb_host_bos), GFP_KERNEL);
802 if (!dev->bos)
803 return -ENOMEM;
804
805 /* Now let's get the whole BOS descriptor set */
806 buffer = kzalloc(total_len, GFP_KERNEL);
807 if (!buffer) {
808 ret = -ENOMEM;
809 goto err;
810 }
811 dev->bos->desc = (struct usb_bos_descriptor *)buffer;
812
813 ret = usb_get_descriptor(dev, USB_DT_BOS, 0, buffer, total_len);
814 if (ret < total_len) {
815 dev_err(ddev, "unable to get BOS descriptor set\n");
816 if (ret >= 0)
817 ret = -ENOMSG;
818 goto err;
819 }
820 total_len -= length;
821
822 for (i = 0; i < num; i++) {
823 buffer += length;
824 cap = (struct usb_dev_cap_header *)buffer;
825 length = cap->bLength;
826
827 if (total_len < length)
828 break;
829 total_len -= length;
830
831 if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {
832 dev_warn(ddev, "descriptor type invalid, skip\n");
833 continue;
834 }
835
836 switch (cap->bDevCapabilityType) {
837 case USB_CAP_TYPE_WIRELESS_USB:
838 /* Wireless USB cap descriptor is handled by wusb */
839 break;
840 case USB_CAP_TYPE_EXT:
841 dev->bos->ext_cap =
842 (struct usb_ext_cap_descriptor *)buffer;
843 break;
844 case USB_SS_CAP_TYPE:
845 dev->bos->ss_cap =
846 (struct usb_ss_cap_descriptor *)buffer;
847 break;
848 case CONTAINER_ID_TYPE:
849 dev->bos->ss_id =
850 (struct usb_ss_container_id_descriptor *)buffer;
851 break;
852 default:
853 break;
854 }
855 }
856
857 return 0;
858
859err:
860 usb_release_bos_descriptor(dev);
861 return ret;
862}