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