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