1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2007 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * This file contains sub-routines to build up USB descriptors from 29 * USB templates. 30 */ 31 32 #include <dev/usb/usb.h> 33 #include <dev/usb/usb_cdc.h> 34 #include <dev/usb/usb_mfunc.h> 35 #include <dev/usb/usb_defs.h> 36 #include <dev/usb/usb_error.h> 37 38 #define USB_DEBUG_VAR usb2_debug 39 40 #include <dev/usb/usb_core.h> 41 #include <dev/usb/usb_busdma.h> 42 #include <dev/usb/usb_process.h> 43 #include <dev/usb/usb_debug.h> 44 #include <dev/usb/usb_parse.h> 45 #include <dev/usb/usb_device.h> 46 #include <dev/usb/usb_dynamic.h> 47 48 #include <dev/usb/usb_controller.h> 49 #include <dev/usb/usb_bus.h> 50 51 #include <dev/usb/template/usb_template.h> 52 53 MODULE_DEPEND(usb_template, usb, 1, 1, 1); 54 MODULE_VERSION(usb_template, 1); 55 56 /* function prototypes */ 57 58 static void usb2_make_raw_desc(struct usb2_temp_setup *, const uint8_t *); 59 static void usb2_make_endpoint_desc(struct usb2_temp_setup *, 60 const struct usb2_temp_endpoint_desc *); 61 static void usb2_make_interface_desc(struct usb2_temp_setup *, 62 const struct usb2_temp_interface_desc *); 63 static void usb2_make_config_desc(struct usb2_temp_setup *, 64 const struct usb2_temp_config_desc *); 65 static void usb2_make_device_desc(struct usb2_temp_setup *, 66 const struct usb2_temp_device_desc *); 67 static uint8_t usb2_hw_ep_match(const struct usb2_hw_ep_profile *, uint8_t, 68 uint8_t); 69 static uint8_t usb2_hw_ep_find_match(struct usb2_hw_ep_scratch *, 70 struct usb2_hw_ep_scratch_sub *, uint8_t); 71 static uint8_t usb2_hw_ep_get_needs(struct usb2_hw_ep_scratch *, uint8_t, 72 uint8_t); 73 static usb2_error_t usb2_hw_ep_resolve(struct usb2_device *, 74 struct usb2_descriptor *); 75 static const struct usb2_temp_device_desc *usb2_temp_get_tdd(struct usb2_device *); 76 static void *usb2_temp_get_device_desc(struct usb2_device *); 77 static void *usb2_temp_get_qualifier_desc(struct usb2_device *); 78 static void *usb2_temp_get_config_desc(struct usb2_device *, uint16_t *, 79 uint8_t); 80 static const void *usb2_temp_get_string_desc(struct usb2_device *, uint16_t, 81 uint8_t); 82 static const void *usb2_temp_get_vendor_desc(struct usb2_device *, 83 const struct usb2_device_request *); 84 static const void *usb2_temp_get_hub_desc(struct usb2_device *); 85 static void usb2_temp_get_desc(struct usb2_device *, 86 struct usb2_device_request *, const void **, uint16_t *); 87 static usb2_error_t usb2_temp_setup(struct usb2_device *, 88 const struct usb2_temp_device_desc *); 89 static void usb2_temp_unsetup(struct usb2_device *); 90 static usb2_error_t usb2_temp_setup_by_index(struct usb2_device *, 91 uint16_t index); 92 static void usb2_temp_init(void *); 93 94 /*------------------------------------------------------------------------* 95 * usb2_make_raw_desc 96 * 97 * This function will insert a raw USB descriptor into the generated 98 * USB configuration. 99 *------------------------------------------------------------------------*/ 100 static void 101 usb2_make_raw_desc(struct usb2_temp_setup *temp, 102 const uint8_t *raw) 103 { 104 void *dst; 105 uint8_t len; 106 107 /* 108 * The first byte of any USB descriptor gives the length. 109 */ 110 if (raw) { 111 len = raw[0]; 112 if (temp->buf) { 113 dst = USB_ADD_BYTES(temp->buf, temp->size); 114 bcopy(raw, dst, len); 115 116 /* check if we have got a CDC union descriptor */ 117 118 if ((raw[0] >= sizeof(struct usb2_cdc_union_descriptor)) && 119 (raw[1] == UDESC_CS_INTERFACE) && 120 (raw[2] == UDESCSUB_CDC_UNION)) { 121 struct usb2_cdc_union_descriptor *ud = (void *)dst; 122 123 /* update the interface numbers */ 124 125 ud->bMasterInterface += 126 temp->bInterfaceNumber; 127 ud->bSlaveInterface[0] += 128 temp->bInterfaceNumber; 129 } 130 } 131 temp->size += len; 132 } 133 } 134 135 /*------------------------------------------------------------------------* 136 * usb2_make_endpoint_desc 137 * 138 * This function will generate an USB endpoint descriptor from the 139 * given USB template endpoint descriptor, which will be inserted into 140 * the USB configuration. 141 *------------------------------------------------------------------------*/ 142 static void 143 usb2_make_endpoint_desc(struct usb2_temp_setup *temp, 144 const struct usb2_temp_endpoint_desc *ted) 145 { 146 struct usb2_endpoint_descriptor *ed; 147 const void **rd; 148 uint16_t old_size; 149 uint16_t mps; 150 uint8_t ea = 0; /* Endpoint Address */ 151 uint8_t et = 0; /* Endpiont Type */ 152 153 /* Reserve memory */ 154 old_size = temp->size; 155 temp->size += sizeof(*ed); 156 157 /* Scan all Raw Descriptors first */ 158 159 rd = ted->ppRawDesc; 160 if (rd) { 161 while (*rd) { 162 usb2_make_raw_desc(temp, *rd); 163 rd++; 164 } 165 } 166 if (ted->pPacketSize == NULL) { 167 /* not initialized */ 168 temp->err = USB_ERR_INVAL; 169 return; 170 } 171 mps = ted->pPacketSize->mps[temp->usb2_speed]; 172 if (mps == 0) { 173 /* not initialized */ 174 temp->err = USB_ERR_INVAL; 175 return; 176 } else if (mps == UE_ZERO_MPS) { 177 /* escape for Zero Max Packet Size */ 178 mps = 0; 179 } 180 ea = (ted->bEndpointAddress & (UE_ADDR | UE_DIR_IN | UE_DIR_OUT)); 181 et = (ted->bmAttributes & UE_XFERTYPE); 182 183 /* 184 * Fill out the real USB endpoint descriptor 185 * in case there is a buffer present: 186 */ 187 if (temp->buf) { 188 ed = USB_ADD_BYTES(temp->buf, old_size); 189 ed->bLength = sizeof(*ed); 190 ed->bDescriptorType = UDESC_ENDPOINT; 191 ed->bEndpointAddress = ea; 192 ed->bmAttributes = ted->bmAttributes; 193 USETW(ed->wMaxPacketSize, mps); 194 195 /* setup bInterval parameter */ 196 197 if (ted->pIntervals && 198 ted->pIntervals->bInterval[temp->usb2_speed]) { 199 ed->bInterval = 200 ted->pIntervals->bInterval[temp->usb2_speed]; 201 } else { 202 switch (et) { 203 case UE_BULK: 204 case UE_CONTROL: 205 ed->bInterval = 0; /* not used */ 206 break; 207 case UE_INTERRUPT: 208 switch (temp->usb2_speed) { 209 case USB_SPEED_LOW: 210 case USB_SPEED_FULL: 211 ed->bInterval = 1; /* 1 ms */ 212 break; 213 default: 214 ed->bInterval = 8; /* 8*125 us */ 215 break; 216 } 217 break; 218 default: /* UE_ISOCHRONOUS */ 219 switch (temp->usb2_speed) { 220 case USB_SPEED_LOW: 221 case USB_SPEED_FULL: 222 ed->bInterval = 1; /* 1 ms */ 223 break; 224 default: 225 ed->bInterval = 1; /* 125 us */ 226 break; 227 } 228 break; 229 } 230 } 231 } 232 temp->bNumEndpoints++; 233 } 234 235 /*------------------------------------------------------------------------* 236 * usb2_make_interface_desc 237 * 238 * This function will generate an USB interface descriptor from the 239 * given USB template interface descriptor, which will be inserted 240 * into the USB configuration. 241 *------------------------------------------------------------------------*/ 242 static void 243 usb2_make_interface_desc(struct usb2_temp_setup *temp, 244 const struct usb2_temp_interface_desc *tid) 245 { 246 struct usb2_interface_descriptor *id; 247 const struct usb2_temp_endpoint_desc **ted; 248 const void **rd; 249 uint16_t old_size; 250 251 /* Reserve memory */ 252 253 old_size = temp->size; 254 temp->size += sizeof(*id); 255 256 /* Update interface and alternate interface numbers */ 257 258 if (tid->isAltInterface == 0) { 259 temp->bAlternateSetting = 0; 260 temp->bInterfaceNumber++; 261 } else { 262 temp->bAlternateSetting++; 263 } 264 265 /* Scan all Raw Descriptors first */ 266 267 rd = tid->ppRawDesc; 268 269 if (rd) { 270 while (*rd) { 271 usb2_make_raw_desc(temp, *rd); 272 rd++; 273 } 274 } 275 /* Reset some counters */ 276 277 temp->bNumEndpoints = 0; 278 279 /* Scan all Endpoint Descriptors second */ 280 281 ted = tid->ppEndpoints; 282 if (ted) { 283 while (*ted) { 284 usb2_make_endpoint_desc(temp, *ted); 285 ted++; 286 } 287 } 288 /* 289 * Fill out the real USB interface descriptor 290 * in case there is a buffer present: 291 */ 292 if (temp->buf) { 293 id = USB_ADD_BYTES(temp->buf, old_size); 294 id->bLength = sizeof(*id); 295 id->bDescriptorType = UDESC_INTERFACE; 296 id->bInterfaceNumber = temp->bInterfaceNumber; 297 id->bAlternateSetting = temp->bAlternateSetting; 298 id->bNumEndpoints = temp->bNumEndpoints; 299 id->bInterfaceClass = tid->bInterfaceClass; 300 id->bInterfaceSubClass = tid->bInterfaceSubClass; 301 id->bInterfaceProtocol = tid->bInterfaceProtocol; 302 id->iInterface = tid->iInterface; 303 } 304 } 305 306 /*------------------------------------------------------------------------* 307 * usb2_make_config_desc 308 * 309 * This function will generate an USB config descriptor from the given 310 * USB template config descriptor, which will be inserted into the USB 311 * configuration. 312 *------------------------------------------------------------------------*/ 313 static void 314 usb2_make_config_desc(struct usb2_temp_setup *temp, 315 const struct usb2_temp_config_desc *tcd) 316 { 317 struct usb2_config_descriptor *cd; 318 const struct usb2_temp_interface_desc **tid; 319 uint16_t old_size; 320 321 /* Reserve memory */ 322 323 old_size = temp->size; 324 temp->size += sizeof(*cd); 325 326 /* Reset some counters */ 327 328 temp->bInterfaceNumber = 0 - 1; 329 temp->bAlternateSetting = 0; 330 331 /* Scan all the USB interfaces */ 332 333 tid = tcd->ppIfaceDesc; 334 if (tid) { 335 while (*tid) { 336 usb2_make_interface_desc(temp, *tid); 337 tid++; 338 } 339 } 340 /* 341 * Fill out the real USB config descriptor 342 * in case there is a buffer present: 343 */ 344 if (temp->buf) { 345 cd = USB_ADD_BYTES(temp->buf, old_size); 346 347 /* compute total size */ 348 old_size = temp->size - old_size; 349 350 cd->bLength = sizeof(*cd); 351 cd->bDescriptorType = UDESC_CONFIG; 352 USETW(cd->wTotalLength, old_size); 353 cd->bNumInterface = temp->bInterfaceNumber + 1; 354 cd->bConfigurationValue = temp->bConfigurationValue; 355 cd->iConfiguration = tcd->iConfiguration; 356 cd->bmAttributes = tcd->bmAttributes; 357 cd->bMaxPower = tcd->bMaxPower; 358 cd->bmAttributes |= (UC_REMOTE_WAKEUP | UC_BUS_POWERED); 359 360 if (temp->self_powered) { 361 cd->bmAttributes |= UC_SELF_POWERED; 362 } else { 363 cd->bmAttributes &= ~UC_SELF_POWERED; 364 } 365 } 366 } 367 368 /*------------------------------------------------------------------------* 369 * usb2_make_device_desc 370 * 371 * This function will generate an USB device descriptor from the 372 * given USB template device descriptor. 373 *------------------------------------------------------------------------*/ 374 static void 375 usb2_make_device_desc(struct usb2_temp_setup *temp, 376 const struct usb2_temp_device_desc *tdd) 377 { 378 struct usb2_temp_data *utd; 379 const struct usb2_temp_config_desc **tcd; 380 uint16_t old_size; 381 382 /* Reserve memory */ 383 384 old_size = temp->size; 385 temp->size += sizeof(*utd); 386 387 /* Scan all the USB configs */ 388 389 temp->bConfigurationValue = 1; 390 tcd = tdd->ppConfigDesc; 391 if (tcd) { 392 while (*tcd) { 393 usb2_make_config_desc(temp, *tcd); 394 temp->bConfigurationValue++; 395 tcd++; 396 } 397 } 398 /* 399 * Fill out the real USB device descriptor 400 * in case there is a buffer present: 401 */ 402 403 if (temp->buf) { 404 utd = USB_ADD_BYTES(temp->buf, old_size); 405 406 /* Store a pointer to our template device descriptor */ 407 utd->tdd = tdd; 408 409 /* Fill out USB device descriptor */ 410 utd->udd.bLength = sizeof(utd->udd); 411 utd->udd.bDescriptorType = UDESC_DEVICE; 412 utd->udd.bDeviceClass = tdd->bDeviceClass; 413 utd->udd.bDeviceSubClass = tdd->bDeviceSubClass; 414 utd->udd.bDeviceProtocol = tdd->bDeviceProtocol; 415 USETW(utd->udd.idVendor, tdd->idVendor); 416 USETW(utd->udd.idProduct, tdd->idProduct); 417 USETW(utd->udd.bcdDevice, tdd->bcdDevice); 418 utd->udd.iManufacturer = tdd->iManufacturer; 419 utd->udd.iProduct = tdd->iProduct; 420 utd->udd.iSerialNumber = tdd->iSerialNumber; 421 utd->udd.bNumConfigurations = temp->bConfigurationValue - 1; 422 423 /* 424 * Fill out the USB device qualifier. Pretend that we 425 * don't support any other speeds by setting 426 * "bNumConfigurations" equal to zero. That saves us 427 * generating an extra set of configuration 428 * descriptors. 429 */ 430 utd->udq.bLength = sizeof(utd->udq); 431 utd->udq.bDescriptorType = UDESC_DEVICE_QUALIFIER; 432 utd->udq.bDeviceClass = tdd->bDeviceClass; 433 utd->udq.bDeviceSubClass = tdd->bDeviceSubClass; 434 utd->udq.bDeviceProtocol = tdd->bDeviceProtocol; 435 utd->udq.bNumConfigurations = 0; 436 USETW(utd->udq.bcdUSB, 0x0200); 437 utd->udq.bMaxPacketSize0 = 0; 438 439 switch (temp->usb2_speed) { 440 case USB_SPEED_LOW: 441 USETW(utd->udd.bcdUSB, 0x0110); 442 utd->udd.bMaxPacketSize = 8; 443 break; 444 case USB_SPEED_FULL: 445 USETW(utd->udd.bcdUSB, 0x0110); 446 utd->udd.bMaxPacketSize = 32; 447 break; 448 case USB_SPEED_HIGH: 449 USETW(utd->udd.bcdUSB, 0x0200); 450 utd->udd.bMaxPacketSize = 64; 451 break; 452 case USB_SPEED_VARIABLE: 453 USETW(utd->udd.bcdUSB, 0x0250); 454 utd->udd.bMaxPacketSize = 255; /* 512 bytes */ 455 break; 456 default: 457 temp->err = USB_ERR_INVAL; 458 break; 459 } 460 } 461 } 462 463 /*------------------------------------------------------------------------* 464 * usb2_hw_ep_match 465 * 466 * Return values: 467 * 0: The endpoint profile does not match the criterias 468 * Else: The endpoint profile matches the criterias 469 *------------------------------------------------------------------------*/ 470 static uint8_t 471 usb2_hw_ep_match(const struct usb2_hw_ep_profile *pf, 472 uint8_t ep_type, uint8_t ep_dir_in) 473 { 474 if (ep_type == UE_CONTROL) { 475 /* special */ 476 return (pf->support_control); 477 } 478 if ((pf->support_in && ep_dir_in) || 479 (pf->support_out && !ep_dir_in)) { 480 if ((pf->support_interrupt && (ep_type == UE_INTERRUPT)) || 481 (pf->support_isochronous && (ep_type == UE_ISOCHRONOUS)) || 482 (pf->support_bulk && (ep_type == UE_BULK))) { 483 return (1); 484 } 485 } 486 return (0); 487 } 488 489 /*------------------------------------------------------------------------* 490 * usb2_hw_ep_find_match 491 * 492 * This function is used to find the best matching endpoint profile 493 * for and endpoint belonging to an USB descriptor. 494 * 495 * Return values: 496 * 0: Success. Got a match. 497 * Else: Failure. No match. 498 *------------------------------------------------------------------------*/ 499 static uint8_t 500 usb2_hw_ep_find_match(struct usb2_hw_ep_scratch *ues, 501 struct usb2_hw_ep_scratch_sub *ep, uint8_t is_simplex) 502 { 503 const struct usb2_hw_ep_profile *pf; 504 uint16_t distance; 505 uint16_t temp; 506 uint16_t max_frame_size; 507 uint8_t n; 508 uint8_t best_n; 509 uint8_t dir_in; 510 uint8_t dir_out; 511 512 distance = 0xFFFF; 513 best_n = 0; 514 515 if ((!ep->needs_in) && (!ep->needs_out)) { 516 return (0); /* we are done */ 517 } 518 if (ep->needs_ep_type == UE_CONTROL) { 519 dir_in = 1; 520 dir_out = 1; 521 } else { 522 if (ep->needs_in) { 523 dir_in = 1; 524 dir_out = 0; 525 } else { 526 dir_in = 0; 527 dir_out = 1; 528 } 529 } 530 531 for (n = 1; n != (USB_EP_MAX / 2); n++) { 532 533 /* get HW endpoint profile */ 534 (ues->methods->get_hw_ep_profile) (ues->udev, &pf, n); 535 if (pf == NULL) { 536 /* end of profiles */ 537 break; 538 } 539 /* check if IN-endpoint is reserved */ 540 if (dir_in || pf->is_simplex) { 541 if (ues->bmInAlloc[n / 8] & (1 << (n % 8))) { 542 /* mismatch */ 543 continue; 544 } 545 } 546 /* check if OUT-endpoint is reserved */ 547 if (dir_out || pf->is_simplex) { 548 if (ues->bmOutAlloc[n / 8] & (1 << (n % 8))) { 549 /* mismatch */ 550 continue; 551 } 552 } 553 /* check simplex */ 554 if (pf->is_simplex == is_simplex) { 555 /* mismatch */ 556 continue; 557 } 558 /* check if HW endpoint matches */ 559 if (!usb2_hw_ep_match(pf, ep->needs_ep_type, dir_in)) { 560 /* mismatch */ 561 continue; 562 } 563 /* get maximum frame size */ 564 if (dir_in) 565 max_frame_size = pf->max_in_frame_size; 566 else 567 max_frame_size = pf->max_out_frame_size; 568 569 /* check if we have a matching profile */ 570 if (max_frame_size >= ep->max_frame_size) { 571 temp = (max_frame_size - ep->max_frame_size); 572 if (distance > temp) { 573 distance = temp; 574 best_n = n; 575 ep->pf = pf; 576 } 577 } 578 } 579 580 /* see if we got a match */ 581 if (best_n != 0) { 582 /* get the correct profile */ 583 pf = ep->pf; 584 585 /* reserve IN-endpoint */ 586 if (dir_in) { 587 ues->bmInAlloc[best_n / 8] |= 588 (1 << (best_n % 8)); 589 ep->hw_endpoint_in = best_n | UE_DIR_IN; 590 ep->needs_in = 0; 591 } 592 /* reserve OUT-endpoint */ 593 if (dir_out) { 594 ues->bmOutAlloc[best_n / 8] |= 595 (1 << (best_n % 8)); 596 ep->hw_endpoint_out = best_n | UE_DIR_OUT; 597 ep->needs_out = 0; 598 } 599 return (0); /* got a match */ 600 } 601 return (1); /* failure */ 602 } 603 604 /*------------------------------------------------------------------------* 605 * usb2_hw_ep_get_needs 606 * 607 * This function will figure out the type and number of endpoints 608 * which are needed for an USB configuration. 609 * 610 * Return values: 611 * 0: Success. 612 * Else: Failure. 613 *------------------------------------------------------------------------*/ 614 static uint8_t 615 usb2_hw_ep_get_needs(struct usb2_hw_ep_scratch *ues, 616 uint8_t ep_type, uint8_t is_complete) 617 { 618 const struct usb2_hw_ep_profile *pf; 619 struct usb2_hw_ep_scratch_sub *ep_iface; 620 struct usb2_hw_ep_scratch_sub *ep_curr; 621 struct usb2_hw_ep_scratch_sub *ep_max; 622 struct usb2_hw_ep_scratch_sub *ep_end; 623 struct usb2_descriptor *desc; 624 struct usb2_interface_descriptor *id; 625 struct usb2_endpoint_descriptor *ed; 626 uint16_t wMaxPacketSize; 627 uint16_t temp; 628 uint8_t speed; 629 uint8_t ep_no; 630 631 ep_iface = ues->ep_max; 632 ep_curr = ues->ep_max; 633 ep_end = ues->ep + USB_EP_MAX; 634 ep_max = ues->ep_max; 635 desc = NULL; 636 speed = usb2_get_speed(ues->udev); 637 638 repeat: 639 640 while ((desc = usb2_desc_foreach(ues->cd, desc))) { 641 642 if ((desc->bDescriptorType == UDESC_INTERFACE) && 643 (desc->bLength >= sizeof(*id))) { 644 645 id = (void *)desc; 646 647 if (id->bAlternateSetting == 0) { 648 /* going forward */ 649 ep_iface = ep_max; 650 } else { 651 /* reset */ 652 ep_curr = ep_iface; 653 } 654 } 655 if ((desc->bDescriptorType == UDESC_ENDPOINT) && 656 (desc->bLength >= sizeof(*ed))) { 657 658 ed = (void *)desc; 659 660 goto handle_endpoint_desc; 661 } 662 } 663 ues->ep_max = ep_max; 664 return (0); 665 666 handle_endpoint_desc: 667 temp = (ed->bmAttributes & UE_XFERTYPE); 668 669 if (temp == ep_type) { 670 671 if (ep_curr == ep_end) { 672 /* too many endpoints */ 673 return (1); /* failure */ 674 } 675 wMaxPacketSize = UGETW(ed->wMaxPacketSize); 676 if ((wMaxPacketSize & 0xF800) && 677 (speed == USB_SPEED_HIGH)) { 678 /* handle packet multiplier */ 679 temp = (wMaxPacketSize >> 11) & 3; 680 wMaxPacketSize &= 0x7FF; 681 if (temp == 1) { 682 wMaxPacketSize *= 2; 683 } else { 684 wMaxPacketSize *= 3; 685 } 686 } 687 /* 688 * Check if we have a fixed endpoint number, else the 689 * endpoint number is allocated dynamically: 690 */ 691 ep_no = (ed->bEndpointAddress & UE_ADDR); 692 if (ep_no != 0) { 693 694 /* get HW endpoint profile */ 695 (ues->methods->get_hw_ep_profile) 696 (ues->udev, &pf, ep_no); 697 if (pf == NULL) { 698 /* HW profile does not exist - failure */ 699 DPRINTFN(0, "Endpoint profile %u " 700 "does not exist\n", ep_no); 701 return (1); 702 } 703 /* reserve fixed endpoint number */ 704 if (ep_type == UE_CONTROL) { 705 ues->bmInAlloc[ep_no / 8] |= 706 (1 << (ep_no % 8)); 707 ues->bmOutAlloc[ep_no / 8] |= 708 (1 << (ep_no % 8)); 709 if ((pf->max_in_frame_size < wMaxPacketSize) || 710 (pf->max_out_frame_size < wMaxPacketSize)) { 711 DPRINTFN(0, "Endpoint profile %u " 712 "has too small buffer!\n", ep_no); 713 return (1); 714 } 715 } else if (ed->bEndpointAddress & UE_DIR_IN) { 716 ues->bmInAlloc[ep_no / 8] |= 717 (1 << (ep_no % 8)); 718 if (pf->max_in_frame_size < wMaxPacketSize) { 719 DPRINTFN(0, "Endpoint profile %u " 720 "has too small buffer!\n", ep_no); 721 return (1); 722 } 723 } else { 724 ues->bmOutAlloc[ep_no / 8] |= 725 (1 << (ep_no % 8)); 726 if (pf->max_out_frame_size < wMaxPacketSize) { 727 DPRINTFN(0, "Endpoint profile %u " 728 "has too small buffer!\n", ep_no); 729 return (1); 730 } 731 } 732 } else if (is_complete) { 733 734 /* check if we have enough buffer space */ 735 if (wMaxPacketSize > 736 ep_curr->max_frame_size) { 737 return (1); /* failure */ 738 } 739 if (ed->bEndpointAddress & UE_DIR_IN) { 740 ed->bEndpointAddress = 741 ep_curr->hw_endpoint_in; 742 } else { 743 ed->bEndpointAddress = 744 ep_curr->hw_endpoint_out; 745 } 746 747 } else { 748 749 /* compute the maximum frame size */ 750 if (ep_curr->max_frame_size < wMaxPacketSize) { 751 ep_curr->max_frame_size = wMaxPacketSize; 752 } 753 if (temp == UE_CONTROL) { 754 ep_curr->needs_in = 1; 755 ep_curr->needs_out = 1; 756 } else { 757 if (ed->bEndpointAddress & UE_DIR_IN) { 758 ep_curr->needs_in = 1; 759 } else { 760 ep_curr->needs_out = 1; 761 } 762 } 763 ep_curr->needs_ep_type = ep_type; 764 } 765 766 ep_curr++; 767 if (ep_max < ep_curr) { 768 ep_max = ep_curr; 769 } 770 } 771 goto repeat; 772 } 773 774 /*------------------------------------------------------------------------* 775 * usb2_hw_ep_resolve 776 * 777 * This function will try to resolve endpoint requirements by the 778 * given endpoint profiles that the USB hardware reports. 779 * 780 * Return values: 781 * 0: Success 782 * Else: Failure 783 *------------------------------------------------------------------------*/ 784 static usb2_error_t 785 usb2_hw_ep_resolve(struct usb2_device *udev, 786 struct usb2_descriptor *desc) 787 { 788 struct usb2_hw_ep_scratch *ues; 789 struct usb2_hw_ep_scratch_sub *ep; 790 const struct usb2_hw_ep_profile *pf; 791 struct usb2_bus_methods *methods; 792 struct usb2_device_descriptor *dd; 793 uint16_t mps; 794 795 if (desc == NULL) { 796 return (USB_ERR_INVAL); 797 } 798 /* get bus methods */ 799 methods = udev->bus->methods; 800 801 if (methods->get_hw_ep_profile == NULL) { 802 return (USB_ERR_INVAL); 803 } 804 if (desc->bDescriptorType == UDESC_DEVICE) { 805 806 if (desc->bLength < sizeof(*dd)) { 807 return (USB_ERR_INVAL); 808 } 809 dd = (void *)desc; 810 811 /* get HW control endpoint 0 profile */ 812 (methods->get_hw_ep_profile) (udev, &pf, 0); 813 if (pf == NULL) { 814 return (USB_ERR_INVAL); 815 } 816 if (!usb2_hw_ep_match(pf, UE_CONTROL, 0)) { 817 DPRINTFN(0, "Endpoint 0 does not " 818 "support control\n"); 819 return (USB_ERR_INVAL); 820 } 821 mps = dd->bMaxPacketSize; 822 823 if (udev->speed == USB_SPEED_FULL) { 824 /* 825 * We can optionally choose another packet size ! 826 */ 827 while (1) { 828 /* check if "mps" is ok */ 829 if (pf->max_in_frame_size >= mps) { 830 break; 831 } 832 /* reduce maximum packet size */ 833 mps /= 2; 834 835 /* check if "mps" is too small */ 836 if (mps < 8) { 837 return (USB_ERR_INVAL); 838 } 839 } 840 841 dd->bMaxPacketSize = mps; 842 843 } else { 844 /* We only have one choice */ 845 if (mps == 255) { 846 mps = 512; 847 } 848 /* Check if we support the specified wMaxPacketSize */ 849 if (pf->max_in_frame_size < mps) { 850 return (USB_ERR_INVAL); 851 } 852 } 853 return (0); /* success */ 854 } 855 if (desc->bDescriptorType != UDESC_CONFIG) { 856 return (USB_ERR_INVAL); 857 } 858 if (desc->bLength < sizeof(*(ues->cd))) { 859 return (USB_ERR_INVAL); 860 } 861 ues = udev->bus->scratch[0].hw_ep_scratch; 862 863 bzero(ues, sizeof(*ues)); 864 865 ues->ep_max = ues->ep; 866 ues->cd = (void *)desc; 867 ues->methods = methods; 868 ues->udev = udev; 869 870 /* Get all the endpoints we need */ 871 872 if (usb2_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 0) || 873 usb2_hw_ep_get_needs(ues, UE_INTERRUPT, 0) || 874 usb2_hw_ep_get_needs(ues, UE_CONTROL, 0) || 875 usb2_hw_ep_get_needs(ues, UE_BULK, 0)) { 876 DPRINTFN(0, "Could not get needs\n"); 877 return (USB_ERR_INVAL); 878 } 879 for (ep = ues->ep; ep != ues->ep_max; ep++) { 880 881 while (ep->needs_in || ep->needs_out) { 882 883 /* 884 * First try to use a simplex endpoint. 885 * Then try to use a duplex endpoint. 886 */ 887 if (usb2_hw_ep_find_match(ues, ep, 1) && 888 usb2_hw_ep_find_match(ues, ep, 0)) { 889 DPRINTFN(0, "Could not find match\n"); 890 return (USB_ERR_INVAL); 891 } 892 } 893 } 894 895 ues->ep_max = ues->ep; 896 897 /* Update all endpoint addresses */ 898 899 if (usb2_hw_ep_get_needs(ues, UE_ISOCHRONOUS, 1) || 900 usb2_hw_ep_get_needs(ues, UE_INTERRUPT, 1) || 901 usb2_hw_ep_get_needs(ues, UE_CONTROL, 1) || 902 usb2_hw_ep_get_needs(ues, UE_BULK, 1)) { 903 DPRINTFN(0, "Could not update endpoint address\n"); 904 return (USB_ERR_INVAL); 905 } 906 return (0); /* success */ 907 } 908 909 /*------------------------------------------------------------------------* 910 * usb2_temp_get_tdd 911 * 912 * Returns: 913 * NULL: No USB template device descriptor found. 914 * Else: Pointer to the USB template device descriptor. 915 *------------------------------------------------------------------------*/ 916 static const struct usb2_temp_device_desc * 917 usb2_temp_get_tdd(struct usb2_device *udev) 918 { 919 if (udev->usb2_template_ptr == NULL) { 920 return (NULL); 921 } 922 return (udev->usb2_template_ptr->tdd); 923 } 924 925 /*------------------------------------------------------------------------* 926 * usb2_temp_get_device_desc 927 * 928 * Returns: 929 * NULL: No USB device descriptor found. 930 * Else: Pointer to USB device descriptor. 931 *------------------------------------------------------------------------*/ 932 static void * 933 usb2_temp_get_device_desc(struct usb2_device *udev) 934 { 935 struct usb2_device_descriptor *dd; 936 937 if (udev->usb2_template_ptr == NULL) { 938 return (NULL); 939 } 940 dd = &udev->usb2_template_ptr->udd; 941 if (dd->bDescriptorType != UDESC_DEVICE) { 942 /* sanity check failed */ 943 return (NULL); 944 } 945 return (dd); 946 } 947 948 /*------------------------------------------------------------------------* 949 * usb2_temp_get_qualifier_desc 950 * 951 * Returns: 952 * NULL: No USB device_qualifier descriptor found. 953 * Else: Pointer to USB device_qualifier descriptor. 954 *------------------------------------------------------------------------*/ 955 static void * 956 usb2_temp_get_qualifier_desc(struct usb2_device *udev) 957 { 958 struct usb2_device_qualifier *dq; 959 960 if (udev->usb2_template_ptr == NULL) { 961 return (NULL); 962 } 963 dq = &udev->usb2_template_ptr->udq; 964 if (dq->bDescriptorType != UDESC_DEVICE_QUALIFIER) { 965 /* sanity check failed */ 966 return (NULL); 967 } 968 return (dq); 969 } 970 971 /*------------------------------------------------------------------------* 972 * usb2_temp_get_config_desc 973 * 974 * Returns: 975 * NULL: No USB config descriptor found. 976 * Else: Pointer to USB config descriptor having index "index". 977 *------------------------------------------------------------------------*/ 978 static void * 979 usb2_temp_get_config_desc(struct usb2_device *udev, 980 uint16_t *pLength, uint8_t index) 981 { 982 struct usb2_device_descriptor *dd; 983 struct usb2_config_descriptor *cd; 984 uint16_t temp; 985 986 if (udev->usb2_template_ptr == NULL) { 987 return (NULL); 988 } 989 dd = &udev->usb2_template_ptr->udd; 990 cd = (void *)(udev->usb2_template_ptr + 1); 991 992 if (index >= dd->bNumConfigurations) { 993 /* out of range */ 994 return (NULL); 995 } 996 while (index--) { 997 if (cd->bDescriptorType != UDESC_CONFIG) { 998 /* sanity check failed */ 999 return (NULL); 1000 } 1001 temp = UGETW(cd->wTotalLength); 1002 cd = USB_ADD_BYTES(cd, temp); 1003 } 1004 1005 if (pLength) { 1006 *pLength = UGETW(cd->wTotalLength); 1007 } 1008 return (cd); 1009 } 1010 1011 /*------------------------------------------------------------------------* 1012 * usb2_temp_get_vendor_desc 1013 * 1014 * Returns: 1015 * NULL: No vendor descriptor found. 1016 * Else: Pointer to a vendor descriptor. 1017 *------------------------------------------------------------------------*/ 1018 static const void * 1019 usb2_temp_get_vendor_desc(struct usb2_device *udev, 1020 const struct usb2_device_request *req) 1021 { 1022 const struct usb2_temp_device_desc *tdd; 1023 1024 tdd = usb2_temp_get_tdd(udev); 1025 if (tdd == NULL) { 1026 return (NULL); 1027 } 1028 if (tdd->getVendorDesc == NULL) { 1029 return (NULL); 1030 } 1031 return ((tdd->getVendorDesc) (req)); 1032 } 1033 1034 /*------------------------------------------------------------------------* 1035 * usb2_temp_get_string_desc 1036 * 1037 * Returns: 1038 * NULL: No string descriptor found. 1039 * Else: Pointer to a string descriptor. 1040 *------------------------------------------------------------------------*/ 1041 static const void * 1042 usb2_temp_get_string_desc(struct usb2_device *udev, 1043 uint16_t lang_id, uint8_t string_index) 1044 { 1045 const struct usb2_temp_device_desc *tdd; 1046 1047 tdd = usb2_temp_get_tdd(udev); 1048 if (tdd == NULL) { 1049 return (NULL); 1050 } 1051 if (tdd->getStringDesc == NULL) { 1052 return (NULL); 1053 } 1054 return ((tdd->getStringDesc) (lang_id, string_index)); 1055 } 1056 1057 /*------------------------------------------------------------------------* 1058 * usb2_temp_get_hub_desc 1059 * 1060 * Returns: 1061 * NULL: No USB HUB descriptor found. 1062 * Else: Pointer to a USB HUB descriptor. 1063 *------------------------------------------------------------------------*/ 1064 static const void * 1065 usb2_temp_get_hub_desc(struct usb2_device *udev) 1066 { 1067 return (NULL); /* needs to be implemented */ 1068 } 1069 1070 /*------------------------------------------------------------------------* 1071 * usb2_temp_get_desc 1072 * 1073 * This function is a demultiplexer for local USB device side control 1074 * endpoint requests. 1075 *------------------------------------------------------------------------*/ 1076 static void 1077 usb2_temp_get_desc(struct usb2_device *udev, struct usb2_device_request *req, 1078 const void **pPtr, uint16_t *pLength) 1079 { 1080 const uint8_t *buf; 1081 uint16_t len; 1082 1083 buf = NULL; 1084 len = 0; 1085 1086 switch (req->bmRequestType) { 1087 case UT_READ_DEVICE: 1088 switch (req->bRequest) { 1089 case UR_GET_DESCRIPTOR: 1090 goto tr_handle_get_descriptor; 1091 default: 1092 goto tr_stalled; 1093 } 1094 break; 1095 case UT_READ_CLASS_DEVICE: 1096 switch (req->bRequest) { 1097 case UR_GET_DESCRIPTOR: 1098 goto tr_handle_get_class_descriptor; 1099 default: 1100 goto tr_stalled; 1101 } 1102 break; 1103 case UT_READ_VENDOR_DEVICE: 1104 case UT_READ_VENDOR_OTHER: 1105 buf = usb2_temp_get_vendor_desc(udev, req); 1106 goto tr_valid; 1107 default: 1108 goto tr_stalled; 1109 } 1110 1111 tr_handle_get_descriptor: 1112 switch (req->wValue[1]) { 1113 case UDESC_DEVICE: 1114 if (req->wValue[0]) { 1115 goto tr_stalled; 1116 } 1117 buf = usb2_temp_get_device_desc(udev); 1118 goto tr_valid; 1119 case UDESC_DEVICE_QUALIFIER: 1120 if (udev->speed != USB_SPEED_HIGH) { 1121 goto tr_stalled; 1122 } 1123 if (req->wValue[0]) { 1124 goto tr_stalled; 1125 } 1126 buf = usb2_temp_get_qualifier_desc(udev); 1127 goto tr_valid; 1128 case UDESC_OTHER_SPEED_CONFIGURATION: 1129 if (udev->speed != USB_SPEED_HIGH) { 1130 goto tr_stalled; 1131 } 1132 case UDESC_CONFIG: 1133 buf = usb2_temp_get_config_desc(udev, 1134 &len, req->wValue[0]); 1135 goto tr_valid; 1136 case UDESC_STRING: 1137 buf = usb2_temp_get_string_desc(udev, 1138 UGETW(req->wIndex), req->wValue[0]); 1139 goto tr_valid; 1140 default: 1141 goto tr_stalled; 1142 } 1143 goto tr_stalled; 1144 1145 tr_handle_get_class_descriptor: 1146 if (req->wValue[0]) { 1147 goto tr_stalled; 1148 } 1149 buf = usb2_temp_get_hub_desc(udev); 1150 goto tr_valid; 1151 1152 tr_valid: 1153 if (buf == NULL) { 1154 goto tr_stalled; 1155 } 1156 if (len == 0) { 1157 len = buf[0]; 1158 } 1159 *pPtr = buf; 1160 *pLength = len; 1161 return; 1162 1163 tr_stalled: 1164 *pPtr = NULL; 1165 *pLength = 0; 1166 } 1167 1168 /*------------------------------------------------------------------------* 1169 * usb2_temp_setup 1170 * 1171 * This function generates USB descriptors according to the given USB 1172 * template device descriptor. It will also try to figure out the best 1173 * matching endpoint addresses using the hardware endpoint profiles. 1174 * 1175 * Returns: 1176 * 0: Success 1177 * Else: Failure 1178 *------------------------------------------------------------------------*/ 1179 static usb2_error_t 1180 usb2_temp_setup(struct usb2_device *udev, 1181 const struct usb2_temp_device_desc *tdd) 1182 { 1183 struct usb2_temp_setup *uts; 1184 void *buf; 1185 uint8_t n; 1186 1187 if (tdd == NULL) { 1188 /* be NULL safe */ 1189 return (0); 1190 } 1191 uts = udev->bus->scratch[0].temp_setup; 1192 1193 bzero(uts, sizeof(*uts)); 1194 1195 uts->usb2_speed = udev->speed; 1196 uts->self_powered = udev->flags.self_powered; 1197 1198 /* first pass */ 1199 1200 usb2_make_device_desc(uts, tdd); 1201 1202 if (uts->err) { 1203 /* some error happened */ 1204 return (uts->err); 1205 } 1206 /* sanity check */ 1207 if (uts->size == 0) { 1208 return (USB_ERR_INVAL); 1209 } 1210 /* allocate zeroed memory */ 1211 uts->buf = malloc(uts->size, M_USB, M_WAITOK | M_ZERO); 1212 if (uts->buf == NULL) { 1213 /* could not allocate memory */ 1214 return (USB_ERR_NOMEM); 1215 } 1216 /* second pass */ 1217 1218 uts->size = 0; 1219 1220 usb2_make_device_desc(uts, tdd); 1221 1222 /* 1223 * Store a pointer to our descriptors: 1224 */ 1225 udev->usb2_template_ptr = uts->buf; 1226 1227 if (uts->err) { 1228 /* some error happened during second pass */ 1229 goto error; 1230 } 1231 /* 1232 * Resolve all endpoint addresses ! 1233 */ 1234 buf = usb2_temp_get_device_desc(udev); 1235 uts->err = usb2_hw_ep_resolve(udev, buf); 1236 if (uts->err) { 1237 DPRINTFN(0, "Could not resolve endpoints for " 1238 "Device Descriptor, error = %s\n", 1239 usb2_errstr(uts->err)); 1240 goto error; 1241 } 1242 for (n = 0;; n++) { 1243 1244 buf = usb2_temp_get_config_desc(udev, NULL, n); 1245 if (buf == NULL) { 1246 break; 1247 } 1248 uts->err = usb2_hw_ep_resolve(udev, buf); 1249 if (uts->err) { 1250 DPRINTFN(0, "Could not resolve endpoints for " 1251 "Config Descriptor %u, error = %s\n", n, 1252 usb2_errstr(uts->err)); 1253 goto error; 1254 } 1255 } 1256 return (uts->err); 1257 1258 error: 1259 usb2_temp_unsetup(udev); 1260 return (uts->err); 1261 } 1262 1263 /*------------------------------------------------------------------------* 1264 * usb2_temp_unsetup 1265 * 1266 * This function frees any memory associated with the currently 1267 * setup template, if any. 1268 *------------------------------------------------------------------------*/ 1269 static void 1270 usb2_temp_unsetup(struct usb2_device *udev) 1271 { 1272 if (udev->usb2_template_ptr) { 1273 1274 free(udev->usb2_template_ptr, M_USB); 1275 1276 udev->usb2_template_ptr = NULL; 1277 } 1278 } 1279 1280 static usb2_error_t 1281 usb2_temp_setup_by_index(struct usb2_device *udev, uint16_t index) 1282 { 1283 usb2_error_t err; 1284 1285 switch (index) { 1286 case 0: 1287 err = usb2_temp_setup(udev, &usb2_template_msc); 1288 break; 1289 case 1: 1290 err = usb2_temp_setup(udev, &usb2_template_cdce); 1291 break; 1292 case 2: 1293 err = usb2_temp_setup(udev, &usb2_template_mtp); 1294 break; 1295 default: 1296 return (USB_ERR_INVAL); 1297 } 1298 1299 return (err); 1300 } 1301 1302 static void 1303 usb2_temp_init(void *arg) 1304 { 1305 /* register our functions */ 1306 usb2_temp_get_desc_p = &usb2_temp_get_desc; 1307 usb2_temp_setup_by_index_p = &usb2_temp_setup_by_index; 1308 usb2_temp_unsetup_p = &usb2_temp_unsetup; 1309 } 1310 1311 SYSINIT(usb2_temp_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb2_temp_init, NULL); 1312 SYSUNINIT(usb2_temp_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb2_temp_unload, NULL); 1313