1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * Bluetooth support for Intel devices 5 * 6 * Copyright (C) 2015 Intel Corporation 7 */ 8 9 #include <linux/module.h> 10 #include <linux/firmware.h> 11 #include <linux/regmap.h> 12 #include <linux/acpi.h> 13 #include <acpi/acpi_bus.h> 14 #include <asm/unaligned.h> 15 16 #include <net/bluetooth/bluetooth.h> 17 #include <net/bluetooth/hci_core.h> 18 19 #include "btintel.h" 20 21 #define VERSION "0.1" 22 23 #define BDADDR_INTEL (&(bdaddr_t){{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}}) 24 #define RSA_HEADER_LEN 644 25 #define CSS_HEADER_OFFSET 8 26 #define ECDSA_OFFSET 644 27 #define ECDSA_HEADER_LEN 320 28 29 enum { 30 DSM_SET_WDISABLE2_DELAY = 1, 31 DSM_SET_RESET_METHOD = 3, 32 }; 33 34 #define CMD_WRITE_BOOT_PARAMS 0xfc0e 35 struct cmd_write_boot_params { 36 __le32 boot_addr; 37 u8 fw_build_num; 38 u8 fw_build_ww; 39 u8 fw_build_yy; 40 } __packed; 41 42 static struct { 43 const char *driver_name; 44 u8 hw_variant; 45 u32 fw_build_num; 46 } coredump_info; 47 48 static const guid_t btintel_guid_dsm = 49 GUID_INIT(0xaa10f4e0, 0x81ac, 0x4233, 50 0xab, 0xf6, 0x3b, 0x2a, 0xc5, 0x0e, 0x28, 0xd9); 51 52 int btintel_check_bdaddr(struct hci_dev *hdev) 53 { 54 struct hci_rp_read_bd_addr *bda; 55 struct sk_buff *skb; 56 57 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL, 58 HCI_INIT_TIMEOUT); 59 if (IS_ERR(skb)) { 60 int err = PTR_ERR(skb); 61 bt_dev_err(hdev, "Reading Intel device address failed (%d)", 62 err); 63 return err; 64 } 65 66 if (skb->len != sizeof(*bda)) { 67 bt_dev_err(hdev, "Intel device address length mismatch"); 68 kfree_skb(skb); 69 return -EIO; 70 } 71 72 bda = (struct hci_rp_read_bd_addr *)skb->data; 73 74 /* For some Intel based controllers, the default Bluetooth device 75 * address 00:03:19:9E:8B:00 can be found. These controllers are 76 * fully operational, but have the danger of duplicate addresses 77 * and that in turn can cause problems with Bluetooth operation. 78 */ 79 if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) { 80 bt_dev_err(hdev, "Found Intel default device address (%pMR)", 81 &bda->bdaddr); 82 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks); 83 } 84 85 kfree_skb(skb); 86 87 return 0; 88 } 89 EXPORT_SYMBOL_GPL(btintel_check_bdaddr); 90 91 int btintel_enter_mfg(struct hci_dev *hdev) 92 { 93 static const u8 param[] = { 0x01, 0x00 }; 94 struct sk_buff *skb; 95 96 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT); 97 if (IS_ERR(skb)) { 98 bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)", 99 PTR_ERR(skb)); 100 return PTR_ERR(skb); 101 } 102 kfree_skb(skb); 103 104 return 0; 105 } 106 EXPORT_SYMBOL_GPL(btintel_enter_mfg); 107 108 int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched) 109 { 110 u8 param[] = { 0x00, 0x00 }; 111 struct sk_buff *skb; 112 113 /* The 2nd command parameter specifies the manufacturing exit method: 114 * 0x00: Just disable the manufacturing mode (0x00). 115 * 0x01: Disable manufacturing mode and reset with patches deactivated. 116 * 0x02: Disable manufacturing mode and reset with patches activated. 117 */ 118 if (reset) 119 param[1] |= patched ? 0x02 : 0x01; 120 121 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT); 122 if (IS_ERR(skb)) { 123 bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)", 124 PTR_ERR(skb)); 125 return PTR_ERR(skb); 126 } 127 kfree_skb(skb); 128 129 return 0; 130 } 131 EXPORT_SYMBOL_GPL(btintel_exit_mfg); 132 133 int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) 134 { 135 struct sk_buff *skb; 136 int err; 137 138 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT); 139 if (IS_ERR(skb)) { 140 err = PTR_ERR(skb); 141 bt_dev_err(hdev, "Changing Intel device address failed (%d)", 142 err); 143 return err; 144 } 145 kfree_skb(skb); 146 147 return 0; 148 } 149 EXPORT_SYMBOL_GPL(btintel_set_bdaddr); 150 151 static int btintel_set_event_mask(struct hci_dev *hdev, bool debug) 152 { 153 u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 154 struct sk_buff *skb; 155 int err; 156 157 if (debug) 158 mask[1] |= 0x62; 159 160 skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT); 161 if (IS_ERR(skb)) { 162 err = PTR_ERR(skb); 163 bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err); 164 return err; 165 } 166 kfree_skb(skb); 167 168 return 0; 169 } 170 171 int btintel_set_diag(struct hci_dev *hdev, bool enable) 172 { 173 struct sk_buff *skb; 174 u8 param[3]; 175 int err; 176 177 if (enable) { 178 param[0] = 0x03; 179 param[1] = 0x03; 180 param[2] = 0x03; 181 } else { 182 param[0] = 0x00; 183 param[1] = 0x00; 184 param[2] = 0x00; 185 } 186 187 skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT); 188 if (IS_ERR(skb)) { 189 err = PTR_ERR(skb); 190 if (err == -ENODATA) 191 goto done; 192 bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)", 193 err); 194 return err; 195 } 196 kfree_skb(skb); 197 198 done: 199 btintel_set_event_mask(hdev, enable); 200 return 0; 201 } 202 EXPORT_SYMBOL_GPL(btintel_set_diag); 203 204 static int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable) 205 { 206 int err, ret; 207 208 err = btintel_enter_mfg(hdev); 209 if (err) 210 return err; 211 212 ret = btintel_set_diag(hdev, enable); 213 214 err = btintel_exit_mfg(hdev, false, false); 215 if (err) 216 return err; 217 218 return ret; 219 } 220 221 static int btintel_set_diag_combined(struct hci_dev *hdev, bool enable) 222 { 223 int ret; 224 225 /* Legacy ROM device needs to be in the manufacturer mode to apply 226 * diagnostic setting 227 * 228 * This flag is set after reading the Intel version. 229 */ 230 if (btintel_test_flag(hdev, INTEL_ROM_LEGACY)) 231 ret = btintel_set_diag_mfg(hdev, enable); 232 else 233 ret = btintel_set_diag(hdev, enable); 234 235 return ret; 236 } 237 238 void btintel_hw_error(struct hci_dev *hdev, u8 code) 239 { 240 struct sk_buff *skb; 241 u8 type = 0x00; 242 243 bt_dev_err(hdev, "Hardware error 0x%2.2x", code); 244 245 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); 246 if (IS_ERR(skb)) { 247 bt_dev_err(hdev, "Reset after hardware error failed (%ld)", 248 PTR_ERR(skb)); 249 return; 250 } 251 kfree_skb(skb); 252 253 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT); 254 if (IS_ERR(skb)) { 255 bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)", 256 PTR_ERR(skb)); 257 return; 258 } 259 260 if (skb->len != 13) { 261 bt_dev_err(hdev, "Exception info size mismatch"); 262 kfree_skb(skb); 263 return; 264 } 265 266 bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1)); 267 268 kfree_skb(skb); 269 } 270 EXPORT_SYMBOL_GPL(btintel_hw_error); 271 272 int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver) 273 { 274 const char *variant; 275 276 /* The hardware platform number has a fixed value of 0x37 and 277 * for now only accept this single value. 278 */ 279 if (ver->hw_platform != 0x37) { 280 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)", 281 ver->hw_platform); 282 return -EINVAL; 283 } 284 285 /* Check for supported iBT hardware variants of this firmware 286 * loading method. 287 * 288 * This check has been put in place to ensure correct forward 289 * compatibility options when newer hardware variants come along. 290 */ 291 switch (ver->hw_variant) { 292 case 0x07: /* WP - Legacy ROM */ 293 case 0x08: /* StP - Legacy ROM */ 294 case 0x0b: /* SfP */ 295 case 0x0c: /* WsP */ 296 case 0x11: /* JfP */ 297 case 0x12: /* ThP */ 298 case 0x13: /* HrP */ 299 case 0x14: /* CcP */ 300 break; 301 default: 302 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)", 303 ver->hw_variant); 304 return -EINVAL; 305 } 306 307 switch (ver->fw_variant) { 308 case 0x01: 309 variant = "Legacy ROM 2.5"; 310 break; 311 case 0x06: 312 variant = "Bootloader"; 313 break; 314 case 0x22: 315 variant = "Legacy ROM 2.x"; 316 break; 317 case 0x23: 318 variant = "Firmware"; 319 break; 320 default: 321 bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant); 322 return -EINVAL; 323 } 324 325 coredump_info.hw_variant = ver->hw_variant; 326 coredump_info.fw_build_num = ver->fw_build_num; 327 328 bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u", 329 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f, 330 ver->fw_build_num, ver->fw_build_ww, 331 2000 + ver->fw_build_yy); 332 333 return 0; 334 } 335 EXPORT_SYMBOL_GPL(btintel_version_info); 336 337 static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen, 338 const void *param) 339 { 340 while (plen > 0) { 341 struct sk_buff *skb; 342 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen; 343 344 cmd_param[0] = fragment_type; 345 memcpy(cmd_param + 1, param, fragment_len); 346 347 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1, 348 cmd_param, HCI_INIT_TIMEOUT); 349 if (IS_ERR(skb)) 350 return PTR_ERR(skb); 351 352 kfree_skb(skb); 353 354 plen -= fragment_len; 355 param += fragment_len; 356 } 357 358 return 0; 359 } 360 361 int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name) 362 { 363 const struct firmware *fw; 364 struct sk_buff *skb; 365 const u8 *fw_ptr; 366 int err; 367 368 err = request_firmware_direct(&fw, ddc_name, &hdev->dev); 369 if (err < 0) { 370 bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)", 371 ddc_name, err); 372 return err; 373 } 374 375 bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name); 376 377 fw_ptr = fw->data; 378 379 /* DDC file contains one or more DDC structure which has 380 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2). 381 */ 382 while (fw->size > fw_ptr - fw->data) { 383 u8 cmd_plen = fw_ptr[0] + sizeof(u8); 384 385 skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr, 386 HCI_INIT_TIMEOUT); 387 if (IS_ERR(skb)) { 388 bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)", 389 PTR_ERR(skb)); 390 release_firmware(fw); 391 return PTR_ERR(skb); 392 } 393 394 fw_ptr += cmd_plen; 395 kfree_skb(skb); 396 } 397 398 release_firmware(fw); 399 400 bt_dev_info(hdev, "Applying Intel DDC parameters completed"); 401 402 return 0; 403 } 404 EXPORT_SYMBOL_GPL(btintel_load_ddc_config); 405 406 int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug) 407 { 408 int err, ret; 409 410 err = btintel_enter_mfg(hdev); 411 if (err) 412 return err; 413 414 ret = btintel_set_event_mask(hdev, debug); 415 416 err = btintel_exit_mfg(hdev, false, false); 417 if (err) 418 return err; 419 420 return ret; 421 } 422 EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg); 423 424 int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver) 425 { 426 struct sk_buff *skb; 427 428 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT); 429 if (IS_ERR(skb)) { 430 bt_dev_err(hdev, "Reading Intel version information failed (%ld)", 431 PTR_ERR(skb)); 432 return PTR_ERR(skb); 433 } 434 435 if (!skb || skb->len != sizeof(*ver)) { 436 bt_dev_err(hdev, "Intel version event size mismatch"); 437 kfree_skb(skb); 438 return -EILSEQ; 439 } 440 441 memcpy(ver, skb->data, sizeof(*ver)); 442 443 kfree_skb(skb); 444 445 return 0; 446 } 447 EXPORT_SYMBOL_GPL(btintel_read_version); 448 449 int btintel_version_info_tlv(struct hci_dev *hdev, 450 struct intel_version_tlv *version) 451 { 452 const char *variant; 453 454 /* The hardware platform number has a fixed value of 0x37 and 455 * for now only accept this single value. 456 */ 457 if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) { 458 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)", 459 INTEL_HW_PLATFORM(version->cnvi_bt)); 460 return -EINVAL; 461 } 462 463 /* Check for supported iBT hardware variants of this firmware 464 * loading method. 465 * 466 * This check has been put in place to ensure correct forward 467 * compatibility options when newer hardware variants come along. 468 */ 469 switch (INTEL_HW_VARIANT(version->cnvi_bt)) { 470 case 0x17: /* TyP */ 471 case 0x18: /* Slr */ 472 case 0x19: /* Slr-F */ 473 case 0x1b: /* Mgr */ 474 case 0x1c: /* Gale Peak (GaP) */ 475 case 0x1e: /* BlazarI (Bzr) */ 476 break; 477 default: 478 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)", 479 INTEL_HW_VARIANT(version->cnvi_bt)); 480 return -EINVAL; 481 } 482 483 switch (version->img_type) { 484 case BTINTEL_IMG_BOOTLOADER: 485 variant = "Bootloader"; 486 /* It is required that every single firmware fragment is acknowledged 487 * with a command complete event. If the boot parameters indicate 488 * that this bootloader does not send them, then abort the setup. 489 */ 490 if (version->limited_cce != 0x00) { 491 bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)", 492 version->limited_cce); 493 return -EINVAL; 494 } 495 496 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */ 497 if (version->sbe_type > 0x01) { 498 bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)", 499 version->sbe_type); 500 return -EINVAL; 501 } 502 503 bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id); 504 bt_dev_info(hdev, "Secure boot is %s", 505 version->secure_boot ? "enabled" : "disabled"); 506 bt_dev_info(hdev, "OTP lock is %s", 507 version->otp_lock ? "enabled" : "disabled"); 508 bt_dev_info(hdev, "API lock is %s", 509 version->api_lock ? "enabled" : "disabled"); 510 bt_dev_info(hdev, "Debug lock is %s", 511 version->debug_lock ? "enabled" : "disabled"); 512 bt_dev_info(hdev, "Minimum firmware build %u week %u %u", 513 version->min_fw_build_nn, version->min_fw_build_cw, 514 2000 + version->min_fw_build_yy); 515 break; 516 case BTINTEL_IMG_IML: 517 variant = "Intermediate loader"; 518 break; 519 case BTINTEL_IMG_OP: 520 variant = "Firmware"; 521 break; 522 default: 523 bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type); 524 return -EINVAL; 525 } 526 527 coredump_info.hw_variant = INTEL_HW_VARIANT(version->cnvi_bt); 528 coredump_info.fw_build_num = version->build_num; 529 530 bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant, 531 2000 + (version->timestamp >> 8), version->timestamp & 0xff, 532 version->build_type, version->build_num); 533 if (version->img_type == BTINTEL_IMG_OP) 534 bt_dev_info(hdev, "Firmware SHA1: 0x%8.8x", version->git_sha1); 535 536 return 0; 537 } 538 EXPORT_SYMBOL_GPL(btintel_version_info_tlv); 539 540 int btintel_parse_version_tlv(struct hci_dev *hdev, 541 struct intel_version_tlv *version, 542 struct sk_buff *skb) 543 { 544 /* Consume Command Complete Status field */ 545 skb_pull(skb, 1); 546 547 /* Event parameters contatin multiple TLVs. Read each of them 548 * and only keep the required data. Also, it use existing legacy 549 * version field like hw_platform, hw_variant, and fw_variant 550 * to keep the existing setup flow 551 */ 552 while (skb->len) { 553 struct intel_tlv *tlv; 554 555 /* Make sure skb has a minimum length of the header */ 556 if (skb->len < sizeof(*tlv)) 557 return -EINVAL; 558 559 tlv = (struct intel_tlv *)skb->data; 560 561 /* Make sure skb has a enough data */ 562 if (skb->len < tlv->len + sizeof(*tlv)) 563 return -EINVAL; 564 565 switch (tlv->type) { 566 case INTEL_TLV_CNVI_TOP: 567 version->cnvi_top = get_unaligned_le32(tlv->val); 568 break; 569 case INTEL_TLV_CNVR_TOP: 570 version->cnvr_top = get_unaligned_le32(tlv->val); 571 break; 572 case INTEL_TLV_CNVI_BT: 573 version->cnvi_bt = get_unaligned_le32(tlv->val); 574 break; 575 case INTEL_TLV_CNVR_BT: 576 version->cnvr_bt = get_unaligned_le32(tlv->val); 577 break; 578 case INTEL_TLV_DEV_REV_ID: 579 version->dev_rev_id = get_unaligned_le16(tlv->val); 580 break; 581 case INTEL_TLV_IMAGE_TYPE: 582 version->img_type = tlv->val[0]; 583 break; 584 case INTEL_TLV_TIME_STAMP: 585 /* If image type is Operational firmware (0x03), then 586 * running FW Calendar Week and Year information can 587 * be extracted from Timestamp information 588 */ 589 version->min_fw_build_cw = tlv->val[0]; 590 version->min_fw_build_yy = tlv->val[1]; 591 version->timestamp = get_unaligned_le16(tlv->val); 592 break; 593 case INTEL_TLV_BUILD_TYPE: 594 version->build_type = tlv->val[0]; 595 break; 596 case INTEL_TLV_BUILD_NUM: 597 /* If image type is Operational firmware (0x03), then 598 * running FW build number can be extracted from the 599 * Build information 600 */ 601 version->min_fw_build_nn = tlv->val[0]; 602 version->build_num = get_unaligned_le32(tlv->val); 603 break; 604 case INTEL_TLV_SECURE_BOOT: 605 version->secure_boot = tlv->val[0]; 606 break; 607 case INTEL_TLV_OTP_LOCK: 608 version->otp_lock = tlv->val[0]; 609 break; 610 case INTEL_TLV_API_LOCK: 611 version->api_lock = tlv->val[0]; 612 break; 613 case INTEL_TLV_DEBUG_LOCK: 614 version->debug_lock = tlv->val[0]; 615 break; 616 case INTEL_TLV_MIN_FW: 617 version->min_fw_build_nn = tlv->val[0]; 618 version->min_fw_build_cw = tlv->val[1]; 619 version->min_fw_build_yy = tlv->val[2]; 620 break; 621 case INTEL_TLV_LIMITED_CCE: 622 version->limited_cce = tlv->val[0]; 623 break; 624 case INTEL_TLV_SBE_TYPE: 625 version->sbe_type = tlv->val[0]; 626 break; 627 case INTEL_TLV_OTP_BDADDR: 628 memcpy(&version->otp_bd_addr, tlv->val, 629 sizeof(bdaddr_t)); 630 break; 631 case INTEL_TLV_GIT_SHA1: 632 version->git_sha1 = get_unaligned_le32(tlv->val); 633 break; 634 case INTEL_TLV_FW_ID: 635 snprintf(version->fw_id, sizeof(version->fw_id), 636 "%s", tlv->val); 637 break; 638 default: 639 /* Ignore rest of information */ 640 break; 641 } 642 /* consume the current tlv and move to next*/ 643 skb_pull(skb, tlv->len + sizeof(*tlv)); 644 } 645 646 return 0; 647 } 648 EXPORT_SYMBOL_GPL(btintel_parse_version_tlv); 649 650 static int btintel_read_version_tlv(struct hci_dev *hdev, 651 struct intel_version_tlv *version) 652 { 653 struct sk_buff *skb; 654 const u8 param[1] = { 0xFF }; 655 656 if (!version) 657 return -EINVAL; 658 659 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT); 660 if (IS_ERR(skb)) { 661 bt_dev_err(hdev, "Reading Intel version information failed (%ld)", 662 PTR_ERR(skb)); 663 return PTR_ERR(skb); 664 } 665 666 if (skb->data[0]) { 667 bt_dev_err(hdev, "Intel Read Version command failed (%02x)", 668 skb->data[0]); 669 kfree_skb(skb); 670 return -EIO; 671 } 672 673 btintel_parse_version_tlv(hdev, version, skb); 674 675 kfree_skb(skb); 676 return 0; 677 } 678 679 /* ------- REGMAP IBT SUPPORT ------- */ 680 681 #define IBT_REG_MODE_8BIT 0x00 682 #define IBT_REG_MODE_16BIT 0x01 683 #define IBT_REG_MODE_32BIT 0x02 684 685 struct regmap_ibt_context { 686 struct hci_dev *hdev; 687 __u16 op_write; 688 __u16 op_read; 689 }; 690 691 struct ibt_cp_reg_access { 692 __le32 addr; 693 __u8 mode; 694 __u8 len; 695 __u8 data[]; 696 } __packed; 697 698 struct ibt_rp_reg_access { 699 __u8 status; 700 __le32 addr; 701 __u8 data[]; 702 } __packed; 703 704 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size, 705 void *val, size_t val_size) 706 { 707 struct regmap_ibt_context *ctx = context; 708 struct ibt_cp_reg_access cp; 709 struct ibt_rp_reg_access *rp; 710 struct sk_buff *skb; 711 int err = 0; 712 713 if (reg_size != sizeof(__le32)) 714 return -EINVAL; 715 716 switch (val_size) { 717 case 1: 718 cp.mode = IBT_REG_MODE_8BIT; 719 break; 720 case 2: 721 cp.mode = IBT_REG_MODE_16BIT; 722 break; 723 case 4: 724 cp.mode = IBT_REG_MODE_32BIT; 725 break; 726 default: 727 return -EINVAL; 728 } 729 730 /* regmap provides a little-endian formatted addr */ 731 cp.addr = *(__le32 *)addr; 732 cp.len = val_size; 733 734 bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr)); 735 736 skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp, 737 HCI_CMD_TIMEOUT); 738 if (IS_ERR(skb)) { 739 err = PTR_ERR(skb); 740 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)", 741 le32_to_cpu(cp.addr), err); 742 return err; 743 } 744 745 if (skb->len != sizeof(*rp) + val_size) { 746 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len", 747 le32_to_cpu(cp.addr)); 748 err = -EINVAL; 749 goto done; 750 } 751 752 rp = (struct ibt_rp_reg_access *)skb->data; 753 754 if (rp->addr != cp.addr) { 755 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr", 756 le32_to_cpu(rp->addr)); 757 err = -EINVAL; 758 goto done; 759 } 760 761 memcpy(val, rp->data, val_size); 762 763 done: 764 kfree_skb(skb); 765 return err; 766 } 767 768 static int regmap_ibt_gather_write(void *context, 769 const void *addr, size_t reg_size, 770 const void *val, size_t val_size) 771 { 772 struct regmap_ibt_context *ctx = context; 773 struct ibt_cp_reg_access *cp; 774 struct sk_buff *skb; 775 int plen = sizeof(*cp) + val_size; 776 u8 mode; 777 int err = 0; 778 779 if (reg_size != sizeof(__le32)) 780 return -EINVAL; 781 782 switch (val_size) { 783 case 1: 784 mode = IBT_REG_MODE_8BIT; 785 break; 786 case 2: 787 mode = IBT_REG_MODE_16BIT; 788 break; 789 case 4: 790 mode = IBT_REG_MODE_32BIT; 791 break; 792 default: 793 return -EINVAL; 794 } 795 796 cp = kmalloc(plen, GFP_KERNEL); 797 if (!cp) 798 return -ENOMEM; 799 800 /* regmap provides a little-endian formatted addr/value */ 801 cp->addr = *(__le32 *)addr; 802 cp->mode = mode; 803 cp->len = val_size; 804 memcpy(&cp->data, val, val_size); 805 806 bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr)); 807 808 skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT); 809 if (IS_ERR(skb)) { 810 err = PTR_ERR(skb); 811 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)", 812 le32_to_cpu(cp->addr), err); 813 goto done; 814 } 815 kfree_skb(skb); 816 817 done: 818 kfree(cp); 819 return err; 820 } 821 822 static int regmap_ibt_write(void *context, const void *data, size_t count) 823 { 824 /* data contains register+value, since we only support 32bit addr, 825 * minimum data size is 4 bytes. 826 */ 827 if (WARN_ONCE(count < 4, "Invalid register access")) 828 return -EINVAL; 829 830 return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4); 831 } 832 833 static void regmap_ibt_free_context(void *context) 834 { 835 kfree(context); 836 } 837 838 static const struct regmap_bus regmap_ibt = { 839 .read = regmap_ibt_read, 840 .write = regmap_ibt_write, 841 .gather_write = regmap_ibt_gather_write, 842 .free_context = regmap_ibt_free_context, 843 .reg_format_endian_default = REGMAP_ENDIAN_LITTLE, 844 .val_format_endian_default = REGMAP_ENDIAN_LITTLE, 845 }; 846 847 /* Config is the same for all register regions */ 848 static const struct regmap_config regmap_ibt_cfg = { 849 .name = "btintel_regmap", 850 .reg_bits = 32, 851 .val_bits = 32, 852 }; 853 854 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read, 855 u16 opcode_write) 856 { 857 struct regmap_ibt_context *ctx; 858 859 bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read, 860 opcode_write); 861 862 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 863 if (!ctx) 864 return ERR_PTR(-ENOMEM); 865 866 ctx->op_read = opcode_read; 867 ctx->op_write = opcode_write; 868 ctx->hdev = hdev; 869 870 return regmap_init(&hdev->dev, ®map_ibt, ctx, ®map_ibt_cfg); 871 } 872 EXPORT_SYMBOL_GPL(btintel_regmap_init); 873 874 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param) 875 { 876 struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 }; 877 struct sk_buff *skb; 878 879 params.boot_param = cpu_to_le32(boot_param); 880 881 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), ¶ms, 882 HCI_INIT_TIMEOUT); 883 if (IS_ERR(skb)) { 884 bt_dev_err(hdev, "Failed to send Intel Reset command"); 885 return PTR_ERR(skb); 886 } 887 888 kfree_skb(skb); 889 890 return 0; 891 } 892 EXPORT_SYMBOL_GPL(btintel_send_intel_reset); 893 894 int btintel_read_boot_params(struct hci_dev *hdev, 895 struct intel_boot_params *params) 896 { 897 struct sk_buff *skb; 898 899 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT); 900 if (IS_ERR(skb)) { 901 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)", 902 PTR_ERR(skb)); 903 return PTR_ERR(skb); 904 } 905 906 if (skb->len != sizeof(*params)) { 907 bt_dev_err(hdev, "Intel boot parameters size mismatch"); 908 kfree_skb(skb); 909 return -EILSEQ; 910 } 911 912 memcpy(params, skb->data, sizeof(*params)); 913 914 kfree_skb(skb); 915 916 if (params->status) { 917 bt_dev_err(hdev, "Intel boot parameters command failed (%02x)", 918 params->status); 919 return -bt_to_errno(params->status); 920 } 921 922 bt_dev_info(hdev, "Device revision is %u", 923 le16_to_cpu(params->dev_revid)); 924 925 bt_dev_info(hdev, "Secure boot is %s", 926 params->secure_boot ? "enabled" : "disabled"); 927 928 bt_dev_info(hdev, "OTP lock is %s", 929 params->otp_lock ? "enabled" : "disabled"); 930 931 bt_dev_info(hdev, "API lock is %s", 932 params->api_lock ? "enabled" : "disabled"); 933 934 bt_dev_info(hdev, "Debug lock is %s", 935 params->debug_lock ? "enabled" : "disabled"); 936 937 bt_dev_info(hdev, "Minimum firmware build %u week %u %u", 938 params->min_fw_build_nn, params->min_fw_build_cw, 939 2000 + params->min_fw_build_yy); 940 941 return 0; 942 } 943 EXPORT_SYMBOL_GPL(btintel_read_boot_params); 944 945 static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev, 946 const struct firmware *fw) 947 { 948 int err; 949 950 /* Start the firmware download transaction with the Init fragment 951 * represented by the 128 bytes of CSS header. 952 */ 953 err = btintel_secure_send(hdev, 0x00, 128, fw->data); 954 if (err < 0) { 955 bt_dev_err(hdev, "Failed to send firmware header (%d)", err); 956 goto done; 957 } 958 959 /* Send the 256 bytes of public key information from the firmware 960 * as the PKey fragment. 961 */ 962 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128); 963 if (err < 0) { 964 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err); 965 goto done; 966 } 967 968 /* Send the 256 bytes of signature information from the firmware 969 * as the Sign fragment. 970 */ 971 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388); 972 if (err < 0) { 973 bt_dev_err(hdev, "Failed to send firmware signature (%d)", err); 974 goto done; 975 } 976 977 done: 978 return err; 979 } 980 981 static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev, 982 const struct firmware *fw) 983 { 984 int err; 985 986 /* Start the firmware download transaction with the Init fragment 987 * represented by the 128 bytes of CSS header. 988 */ 989 err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644); 990 if (err < 0) { 991 bt_dev_err(hdev, "Failed to send firmware header (%d)", err); 992 return err; 993 } 994 995 /* Send the 96 bytes of public key information from the firmware 996 * as the PKey fragment. 997 */ 998 err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128); 999 if (err < 0) { 1000 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err); 1001 return err; 1002 } 1003 1004 /* Send the 96 bytes of signature information from the firmware 1005 * as the Sign fragment 1006 */ 1007 err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224); 1008 if (err < 0) { 1009 bt_dev_err(hdev, "Failed to send firmware signature (%d)", 1010 err); 1011 return err; 1012 } 1013 return 0; 1014 } 1015 1016 static int btintel_download_firmware_payload(struct hci_dev *hdev, 1017 const struct firmware *fw, 1018 size_t offset) 1019 { 1020 int err; 1021 const u8 *fw_ptr; 1022 u32 frag_len; 1023 1024 fw_ptr = fw->data + offset; 1025 frag_len = 0; 1026 err = -EINVAL; 1027 1028 while (fw_ptr - fw->data < fw->size) { 1029 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len); 1030 1031 frag_len += sizeof(*cmd) + cmd->plen; 1032 1033 /* The parameter length of the secure send command requires 1034 * a 4 byte alignment. It happens so that the firmware file 1035 * contains proper Intel_NOP commands to align the fragments 1036 * as needed. 1037 * 1038 * Send set of commands with 4 byte alignment from the 1039 * firmware data buffer as a single Data fragement. 1040 */ 1041 if (!(frag_len % 4)) { 1042 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr); 1043 if (err < 0) { 1044 bt_dev_err(hdev, 1045 "Failed to send firmware data (%d)", 1046 err); 1047 goto done; 1048 } 1049 1050 fw_ptr += frag_len; 1051 frag_len = 0; 1052 } 1053 } 1054 1055 done: 1056 return err; 1057 } 1058 1059 static bool btintel_firmware_version(struct hci_dev *hdev, 1060 u8 num, u8 ww, u8 yy, 1061 const struct firmware *fw, 1062 u32 *boot_addr) 1063 { 1064 const u8 *fw_ptr; 1065 1066 fw_ptr = fw->data; 1067 1068 while (fw_ptr - fw->data < fw->size) { 1069 struct hci_command_hdr *cmd = (void *)(fw_ptr); 1070 1071 /* Each SKU has a different reset parameter to use in the 1072 * HCI_Intel_Reset command and it is embedded in the firmware 1073 * data. So, instead of using static value per SKU, check 1074 * the firmware data and save it for later use. 1075 */ 1076 if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) { 1077 struct cmd_write_boot_params *params; 1078 1079 params = (void *)(fw_ptr + sizeof(*cmd)); 1080 1081 *boot_addr = le32_to_cpu(params->boot_addr); 1082 1083 bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr); 1084 1085 bt_dev_info(hdev, "Firmware Version: %u-%u.%u", 1086 params->fw_build_num, params->fw_build_ww, 1087 params->fw_build_yy); 1088 1089 return (num == params->fw_build_num && 1090 ww == params->fw_build_ww && 1091 yy == params->fw_build_yy); 1092 } 1093 1094 fw_ptr += sizeof(*cmd) + cmd->plen; 1095 } 1096 1097 return false; 1098 } 1099 1100 int btintel_download_firmware(struct hci_dev *hdev, 1101 struct intel_version *ver, 1102 const struct firmware *fw, 1103 u32 *boot_param) 1104 { 1105 int err; 1106 1107 /* SfP and WsP don't seem to update the firmware version on file 1108 * so version checking is currently not possible. 1109 */ 1110 switch (ver->hw_variant) { 1111 case 0x0b: /* SfP */ 1112 case 0x0c: /* WsP */ 1113 /* Skip version checking */ 1114 break; 1115 default: 1116 1117 /* Skip download if firmware has the same version */ 1118 if (btintel_firmware_version(hdev, ver->fw_build_num, 1119 ver->fw_build_ww, ver->fw_build_yy, 1120 fw, boot_param)) { 1121 bt_dev_info(hdev, "Firmware already loaded"); 1122 /* Return -EALREADY to indicate that the firmware has 1123 * already been loaded. 1124 */ 1125 return -EALREADY; 1126 } 1127 } 1128 1129 /* The firmware variant determines if the device is in bootloader 1130 * mode or is running operational firmware. The value 0x06 identifies 1131 * the bootloader and the value 0x23 identifies the operational 1132 * firmware. 1133 * 1134 * If the firmware version has changed that means it needs to be reset 1135 * to bootloader when operational so the new firmware can be loaded. 1136 */ 1137 if (ver->fw_variant == 0x23) 1138 return -EINVAL; 1139 1140 err = btintel_sfi_rsa_header_secure_send(hdev, fw); 1141 if (err) 1142 return err; 1143 1144 return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN); 1145 } 1146 EXPORT_SYMBOL_GPL(btintel_download_firmware); 1147 1148 static int btintel_download_fw_tlv(struct hci_dev *hdev, 1149 struct intel_version_tlv *ver, 1150 const struct firmware *fw, u32 *boot_param, 1151 u8 hw_variant, u8 sbe_type) 1152 { 1153 int err; 1154 u32 css_header_ver; 1155 1156 /* Skip download if firmware has the same version */ 1157 if (btintel_firmware_version(hdev, ver->min_fw_build_nn, 1158 ver->min_fw_build_cw, 1159 ver->min_fw_build_yy, 1160 fw, boot_param)) { 1161 bt_dev_info(hdev, "Firmware already loaded"); 1162 /* Return -EALREADY to indicate that firmware has 1163 * already been loaded. 1164 */ 1165 return -EALREADY; 1166 } 1167 1168 /* The firmware variant determines if the device is in bootloader 1169 * mode or is running operational firmware. The value 0x01 identifies 1170 * the bootloader and the value 0x03 identifies the operational 1171 * firmware. 1172 * 1173 * If the firmware version has changed that means it needs to be reset 1174 * to bootloader when operational so the new firmware can be loaded. 1175 */ 1176 if (ver->img_type == BTINTEL_IMG_OP) 1177 return -EINVAL; 1178 1179 /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support 1180 * only RSA secure boot engine. Hence, the corresponding sfi file will 1181 * have RSA header of 644 bytes followed by Command Buffer. 1182 * 1183 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA 1184 * secure boot engine. As a result, the corresponding sfi file will 1185 * have RSA header of 644, ECDSA header of 320 bytes followed by 1186 * Command Buffer. 1187 * 1188 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header 1189 * version: RSA(0x00010000) , ECDSA (0x00020000) 1190 */ 1191 css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET); 1192 if (css_header_ver != 0x00010000) { 1193 bt_dev_err(hdev, "Invalid CSS Header version"); 1194 return -EINVAL; 1195 } 1196 1197 if (hw_variant <= 0x14) { 1198 if (sbe_type != 0x00) { 1199 bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)", 1200 hw_variant); 1201 return -EINVAL; 1202 } 1203 1204 err = btintel_sfi_rsa_header_secure_send(hdev, fw); 1205 if (err) 1206 return err; 1207 1208 err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN); 1209 if (err) 1210 return err; 1211 } else if (hw_variant >= 0x17) { 1212 /* Check if CSS header for ECDSA follows the RSA header */ 1213 if (fw->data[ECDSA_OFFSET] != 0x06) 1214 return -EINVAL; 1215 1216 /* Check if the CSS Header version is ECDSA(0x00020000) */ 1217 css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET); 1218 if (css_header_ver != 0x00020000) { 1219 bt_dev_err(hdev, "Invalid CSS Header version"); 1220 return -EINVAL; 1221 } 1222 1223 if (sbe_type == 0x00) { 1224 err = btintel_sfi_rsa_header_secure_send(hdev, fw); 1225 if (err) 1226 return err; 1227 1228 err = btintel_download_firmware_payload(hdev, fw, 1229 RSA_HEADER_LEN + ECDSA_HEADER_LEN); 1230 if (err) 1231 return err; 1232 } else if (sbe_type == 0x01) { 1233 err = btintel_sfi_ecdsa_header_secure_send(hdev, fw); 1234 if (err) 1235 return err; 1236 1237 err = btintel_download_firmware_payload(hdev, fw, 1238 RSA_HEADER_LEN + ECDSA_HEADER_LEN); 1239 if (err) 1240 return err; 1241 } 1242 } 1243 return 0; 1244 } 1245 1246 static void btintel_reset_to_bootloader(struct hci_dev *hdev) 1247 { 1248 struct intel_reset params; 1249 struct sk_buff *skb; 1250 1251 /* Send Intel Reset command. This will result in 1252 * re-enumeration of BT controller. 1253 * 1254 * Intel Reset parameter description: 1255 * reset_type : 0x00 (Soft reset), 1256 * 0x01 (Hard reset) 1257 * patch_enable : 0x00 (Do not enable), 1258 * 0x01 (Enable) 1259 * ddc_reload : 0x00 (Do not reload), 1260 * 0x01 (Reload) 1261 * boot_option: 0x00 (Current image), 1262 * 0x01 (Specified boot address) 1263 * boot_param: Boot address 1264 * 1265 */ 1266 params.reset_type = 0x01; 1267 params.patch_enable = 0x01; 1268 params.ddc_reload = 0x01; 1269 params.boot_option = 0x00; 1270 params.boot_param = cpu_to_le32(0x00000000); 1271 1272 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), 1273 ¶ms, HCI_INIT_TIMEOUT); 1274 if (IS_ERR(skb)) { 1275 bt_dev_err(hdev, "FW download error recovery failed (%ld)", 1276 PTR_ERR(skb)); 1277 return; 1278 } 1279 bt_dev_info(hdev, "Intel reset sent to retry FW download"); 1280 kfree_skb(skb); 1281 1282 /* Current Intel BT controllers(ThP/JfP) hold the USB reset 1283 * lines for 2ms when it receives Intel Reset in bootloader mode. 1284 * Whereas, the upcoming Intel BT controllers will hold USB reset 1285 * for 150ms. To keep the delay generic, 150ms is chosen here. 1286 */ 1287 msleep(150); 1288 } 1289 1290 static int btintel_read_debug_features(struct hci_dev *hdev, 1291 struct intel_debug_features *features) 1292 { 1293 struct sk_buff *skb; 1294 u8 page_no = 1; 1295 1296 /* Intel controller supports two pages, each page is of 128-bit 1297 * feature bit mask. And each bit defines specific feature support 1298 */ 1299 skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no, 1300 HCI_INIT_TIMEOUT); 1301 if (IS_ERR(skb)) { 1302 bt_dev_err(hdev, "Reading supported features failed (%ld)", 1303 PTR_ERR(skb)); 1304 return PTR_ERR(skb); 1305 } 1306 1307 if (skb->len != (sizeof(features->page1) + 3)) { 1308 bt_dev_err(hdev, "Supported features event size mismatch"); 1309 kfree_skb(skb); 1310 return -EILSEQ; 1311 } 1312 1313 memcpy(features->page1, skb->data + 3, sizeof(features->page1)); 1314 1315 /* Read the supported features page2 if required in future. 1316 */ 1317 kfree_skb(skb); 1318 return 0; 1319 } 1320 1321 static int btintel_set_debug_features(struct hci_dev *hdev, 1322 const struct intel_debug_features *features) 1323 { 1324 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00, 1325 0x00, 0x00, 0x00 }; 1326 u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 }; 1327 u8 trace_enable = 0x02; 1328 struct sk_buff *skb; 1329 1330 if (!features) { 1331 bt_dev_warn(hdev, "Debug features not read"); 1332 return -EINVAL; 1333 } 1334 1335 if (!(features->page1[0] & 0x3f)) { 1336 bt_dev_info(hdev, "Telemetry exception format not supported"); 1337 return 0; 1338 } 1339 1340 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT); 1341 if (IS_ERR(skb)) { 1342 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)", 1343 PTR_ERR(skb)); 1344 return PTR_ERR(skb); 1345 } 1346 kfree_skb(skb); 1347 1348 skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT); 1349 if (IS_ERR(skb)) { 1350 bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)", 1351 PTR_ERR(skb)); 1352 return PTR_ERR(skb); 1353 } 1354 kfree_skb(skb); 1355 1356 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT); 1357 if (IS_ERR(skb)) { 1358 bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)", 1359 PTR_ERR(skb)); 1360 return PTR_ERR(skb); 1361 } 1362 kfree_skb(skb); 1363 1364 bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x", 1365 trace_enable, mask[3]); 1366 1367 return 0; 1368 } 1369 1370 static int btintel_reset_debug_features(struct hci_dev *hdev, 1371 const struct intel_debug_features *features) 1372 { 1373 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 1374 0x00, 0x00, 0x00 }; 1375 u8 trace_enable = 0x00; 1376 struct sk_buff *skb; 1377 1378 if (!features) { 1379 bt_dev_warn(hdev, "Debug features not read"); 1380 return -EINVAL; 1381 } 1382 1383 if (!(features->page1[0] & 0x3f)) { 1384 bt_dev_info(hdev, "Telemetry exception format not supported"); 1385 return 0; 1386 } 1387 1388 /* Should stop the trace before writing ddc event mask. */ 1389 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT); 1390 if (IS_ERR(skb)) { 1391 bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)", 1392 PTR_ERR(skb)); 1393 return PTR_ERR(skb); 1394 } 1395 kfree_skb(skb); 1396 1397 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT); 1398 if (IS_ERR(skb)) { 1399 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)", 1400 PTR_ERR(skb)); 1401 return PTR_ERR(skb); 1402 } 1403 kfree_skb(skb); 1404 1405 bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x", 1406 trace_enable, mask[3]); 1407 1408 return 0; 1409 } 1410 1411 int btintel_set_quality_report(struct hci_dev *hdev, bool enable) 1412 { 1413 struct intel_debug_features features; 1414 int err; 1415 1416 bt_dev_dbg(hdev, "enable %d", enable); 1417 1418 /* Read the Intel supported features and if new exception formats 1419 * supported, need to load the additional DDC config to enable. 1420 */ 1421 err = btintel_read_debug_features(hdev, &features); 1422 if (err) 1423 return err; 1424 1425 /* Set or reset the debug features. */ 1426 if (enable) 1427 err = btintel_set_debug_features(hdev, &features); 1428 else 1429 err = btintel_reset_debug_features(hdev, &features); 1430 1431 return err; 1432 } 1433 EXPORT_SYMBOL_GPL(btintel_set_quality_report); 1434 1435 static void btintel_coredump(struct hci_dev *hdev) 1436 { 1437 struct sk_buff *skb; 1438 1439 skb = __hci_cmd_sync(hdev, 0xfc4e, 0, NULL, HCI_CMD_TIMEOUT); 1440 if (IS_ERR(skb)) { 1441 bt_dev_err(hdev, "Coredump failed (%ld)", PTR_ERR(skb)); 1442 return; 1443 } 1444 1445 kfree_skb(skb); 1446 } 1447 1448 static void btintel_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb) 1449 { 1450 char buf[80]; 1451 1452 snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n", 1453 coredump_info.hw_variant); 1454 skb_put_data(skb, buf, strlen(buf)); 1455 1456 snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n", 1457 coredump_info.fw_build_num); 1458 skb_put_data(skb, buf, strlen(buf)); 1459 1460 snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info.driver_name); 1461 skb_put_data(skb, buf, strlen(buf)); 1462 1463 snprintf(buf, sizeof(buf), "Vendor: Intel\n"); 1464 skb_put_data(skb, buf, strlen(buf)); 1465 } 1466 1467 static int btintel_register_devcoredump_support(struct hci_dev *hdev) 1468 { 1469 struct intel_debug_features features; 1470 int err; 1471 1472 err = btintel_read_debug_features(hdev, &features); 1473 if (err) { 1474 bt_dev_info(hdev, "Error reading debug features"); 1475 return err; 1476 } 1477 1478 if (!(features.page1[0] & 0x3f)) { 1479 bt_dev_dbg(hdev, "Telemetry exception format not supported"); 1480 return -EOPNOTSUPP; 1481 } 1482 1483 hci_devcd_register(hdev, btintel_coredump, btintel_dmp_hdr, NULL); 1484 1485 return err; 1486 } 1487 1488 static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev, 1489 struct intel_version *ver) 1490 { 1491 const struct firmware *fw; 1492 char fwname[64]; 1493 int ret; 1494 1495 snprintf(fwname, sizeof(fwname), 1496 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq", 1497 ver->hw_platform, ver->hw_variant, ver->hw_revision, 1498 ver->fw_variant, ver->fw_revision, ver->fw_build_num, 1499 ver->fw_build_ww, ver->fw_build_yy); 1500 1501 ret = request_firmware(&fw, fwname, &hdev->dev); 1502 if (ret < 0) { 1503 if (ret == -EINVAL) { 1504 bt_dev_err(hdev, "Intel firmware file request failed (%d)", 1505 ret); 1506 return NULL; 1507 } 1508 1509 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)", 1510 fwname, ret); 1511 1512 /* If the correct firmware patch file is not found, use the 1513 * default firmware patch file instead 1514 */ 1515 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq", 1516 ver->hw_platform, ver->hw_variant); 1517 if (request_firmware(&fw, fwname, &hdev->dev) < 0) { 1518 bt_dev_err(hdev, "failed to open default fw file: %s", 1519 fwname); 1520 return NULL; 1521 } 1522 } 1523 1524 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname); 1525 1526 return fw; 1527 } 1528 1529 static int btintel_legacy_rom_patching(struct hci_dev *hdev, 1530 const struct firmware *fw, 1531 const u8 **fw_ptr, int *disable_patch) 1532 { 1533 struct sk_buff *skb; 1534 struct hci_command_hdr *cmd; 1535 const u8 *cmd_param; 1536 struct hci_event_hdr *evt = NULL; 1537 const u8 *evt_param = NULL; 1538 int remain = fw->size - (*fw_ptr - fw->data); 1539 1540 /* The first byte indicates the types of the patch command or event. 1541 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes 1542 * in the current firmware buffer doesn't start with 0x01 or 1543 * the size of remain buffer is smaller than HCI command header, 1544 * the firmware file is corrupted and it should stop the patching 1545 * process. 1546 */ 1547 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) { 1548 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read"); 1549 return -EINVAL; 1550 } 1551 (*fw_ptr)++; 1552 remain--; 1553 1554 cmd = (struct hci_command_hdr *)(*fw_ptr); 1555 *fw_ptr += sizeof(*cmd); 1556 remain -= sizeof(*cmd); 1557 1558 /* Ensure that the remain firmware data is long enough than the length 1559 * of command parameter. If not, the firmware file is corrupted. 1560 */ 1561 if (remain < cmd->plen) { 1562 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len"); 1563 return -EFAULT; 1564 } 1565 1566 /* If there is a command that loads a patch in the firmware 1567 * file, then enable the patch upon success, otherwise just 1568 * disable the manufacturer mode, for example patch activation 1569 * is not required when the default firmware patch file is used 1570 * because there are no patch data to load. 1571 */ 1572 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e) 1573 *disable_patch = 0; 1574 1575 cmd_param = *fw_ptr; 1576 *fw_ptr += cmd->plen; 1577 remain -= cmd->plen; 1578 1579 /* This reads the expected events when the above command is sent to the 1580 * device. Some vendor commands expects more than one events, for 1581 * example command status event followed by vendor specific event. 1582 * For this case, it only keeps the last expected event. so the command 1583 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of 1584 * last expected event. 1585 */ 1586 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) { 1587 (*fw_ptr)++; 1588 remain--; 1589 1590 evt = (struct hci_event_hdr *)(*fw_ptr); 1591 *fw_ptr += sizeof(*evt); 1592 remain -= sizeof(*evt); 1593 1594 if (remain < evt->plen) { 1595 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len"); 1596 return -EFAULT; 1597 } 1598 1599 evt_param = *fw_ptr; 1600 *fw_ptr += evt->plen; 1601 remain -= evt->plen; 1602 } 1603 1604 /* Every HCI commands in the firmware file has its correspond event. 1605 * If event is not found or remain is smaller than zero, the firmware 1606 * file is corrupted. 1607 */ 1608 if (!evt || !evt_param || remain < 0) { 1609 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read"); 1610 return -EFAULT; 1611 } 1612 1613 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen, 1614 cmd_param, evt->evt, HCI_INIT_TIMEOUT); 1615 if (IS_ERR(skb)) { 1616 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)", 1617 cmd->opcode, PTR_ERR(skb)); 1618 return PTR_ERR(skb); 1619 } 1620 1621 /* It ensures that the returned event matches the event data read from 1622 * the firmware file. At fist, it checks the length and then 1623 * the contents of the event. 1624 */ 1625 if (skb->len != evt->plen) { 1626 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)", 1627 le16_to_cpu(cmd->opcode)); 1628 kfree_skb(skb); 1629 return -EFAULT; 1630 } 1631 1632 if (memcmp(skb->data, evt_param, evt->plen)) { 1633 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)", 1634 le16_to_cpu(cmd->opcode)); 1635 kfree_skb(skb); 1636 return -EFAULT; 1637 } 1638 kfree_skb(skb); 1639 1640 return 0; 1641 } 1642 1643 static int btintel_legacy_rom_setup(struct hci_dev *hdev, 1644 struct intel_version *ver) 1645 { 1646 const struct firmware *fw; 1647 const u8 *fw_ptr; 1648 int disable_patch, err; 1649 struct intel_version new_ver; 1650 1651 BT_DBG("%s", hdev->name); 1652 1653 /* fw_patch_num indicates the version of patch the device currently 1654 * have. If there is no patch data in the device, it is always 0x00. 1655 * So, if it is other than 0x00, no need to patch the device again. 1656 */ 1657 if (ver->fw_patch_num) { 1658 bt_dev_info(hdev, 1659 "Intel device is already patched. patch num: %02x", 1660 ver->fw_patch_num); 1661 goto complete; 1662 } 1663 1664 /* Opens the firmware patch file based on the firmware version read 1665 * from the controller. If it fails to open the matching firmware 1666 * patch file, it tries to open the default firmware patch file. 1667 * If no patch file is found, allow the device to operate without 1668 * a patch. 1669 */ 1670 fw = btintel_legacy_rom_get_fw(hdev, ver); 1671 if (!fw) 1672 goto complete; 1673 fw_ptr = fw->data; 1674 1675 /* Enable the manufacturer mode of the controller. 1676 * Only while this mode is enabled, the driver can download the 1677 * firmware patch data and configuration parameters. 1678 */ 1679 err = btintel_enter_mfg(hdev); 1680 if (err) { 1681 release_firmware(fw); 1682 return err; 1683 } 1684 1685 disable_patch = 1; 1686 1687 /* The firmware data file consists of list of Intel specific HCI 1688 * commands and its expected events. The first byte indicates the 1689 * type of the message, either HCI command or HCI event. 1690 * 1691 * It reads the command and its expected event from the firmware file, 1692 * and send to the controller. Once __hci_cmd_sync_ev() returns, 1693 * the returned event is compared with the event read from the firmware 1694 * file and it will continue until all the messages are downloaded to 1695 * the controller. 1696 * 1697 * Once the firmware patching is completed successfully, 1698 * the manufacturer mode is disabled with reset and activating the 1699 * downloaded patch. 1700 * 1701 * If the firmware patching fails, the manufacturer mode is 1702 * disabled with reset and deactivating the patch. 1703 * 1704 * If the default patch file is used, no reset is done when disabling 1705 * the manufacturer. 1706 */ 1707 while (fw->size > fw_ptr - fw->data) { 1708 int ret; 1709 1710 ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr, 1711 &disable_patch); 1712 if (ret < 0) 1713 goto exit_mfg_deactivate; 1714 } 1715 1716 release_firmware(fw); 1717 1718 if (disable_patch) 1719 goto exit_mfg_disable; 1720 1721 /* Patching completed successfully and disable the manufacturer mode 1722 * with reset and activate the downloaded firmware patches. 1723 */ 1724 err = btintel_exit_mfg(hdev, true, true); 1725 if (err) 1726 return err; 1727 1728 /* Need build number for downloaded fw patches in 1729 * every power-on boot 1730 */ 1731 err = btintel_read_version(hdev, &new_ver); 1732 if (err) 1733 return err; 1734 1735 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated", 1736 new_ver.fw_patch_num); 1737 1738 goto complete; 1739 1740 exit_mfg_disable: 1741 /* Disable the manufacturer mode without reset */ 1742 err = btintel_exit_mfg(hdev, false, false); 1743 if (err) 1744 return err; 1745 1746 bt_dev_info(hdev, "Intel firmware patch completed"); 1747 1748 goto complete; 1749 1750 exit_mfg_deactivate: 1751 release_firmware(fw); 1752 1753 /* Patching failed. Disable the manufacturer mode with reset and 1754 * deactivate the downloaded firmware patches. 1755 */ 1756 err = btintel_exit_mfg(hdev, true, false); 1757 if (err) 1758 return err; 1759 1760 bt_dev_info(hdev, "Intel firmware patch completed and deactivated"); 1761 1762 complete: 1763 /* Set the event mask for Intel specific vendor events. This enables 1764 * a few extra events that are useful during general operation. 1765 */ 1766 btintel_set_event_mask_mfg(hdev, false); 1767 1768 btintel_check_bdaddr(hdev); 1769 1770 return 0; 1771 } 1772 1773 static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec) 1774 { 1775 ktime_t delta, rettime; 1776 unsigned long long duration; 1777 int err; 1778 1779 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED); 1780 1781 bt_dev_info(hdev, "Waiting for firmware download to complete"); 1782 1783 err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING, 1784 TASK_INTERRUPTIBLE, 1785 msecs_to_jiffies(msec)); 1786 if (err == -EINTR) { 1787 bt_dev_err(hdev, "Firmware loading interrupted"); 1788 return err; 1789 } 1790 1791 if (err) { 1792 bt_dev_err(hdev, "Firmware loading timeout"); 1793 return -ETIMEDOUT; 1794 } 1795 1796 if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) { 1797 bt_dev_err(hdev, "Firmware loading failed"); 1798 return -ENOEXEC; 1799 } 1800 1801 rettime = ktime_get(); 1802 delta = ktime_sub(rettime, calltime); 1803 duration = (unsigned long long)ktime_to_ns(delta) >> 10; 1804 1805 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration); 1806 1807 return 0; 1808 } 1809 1810 static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec) 1811 { 1812 ktime_t delta, rettime; 1813 unsigned long long duration; 1814 int err; 1815 1816 bt_dev_info(hdev, "Waiting for device to boot"); 1817 1818 err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING, 1819 TASK_INTERRUPTIBLE, 1820 msecs_to_jiffies(msec)); 1821 if (err == -EINTR) { 1822 bt_dev_err(hdev, "Device boot interrupted"); 1823 return -EINTR; 1824 } 1825 1826 if (err) { 1827 bt_dev_err(hdev, "Device boot timeout"); 1828 return -ETIMEDOUT; 1829 } 1830 1831 rettime = ktime_get(); 1832 delta = ktime_sub(rettime, calltime); 1833 duration = (unsigned long long) ktime_to_ns(delta) >> 10; 1834 1835 bt_dev_info(hdev, "Device booted in %llu usecs", duration); 1836 1837 return 0; 1838 } 1839 1840 static int btintel_boot(struct hci_dev *hdev, u32 boot_addr) 1841 { 1842 ktime_t calltime; 1843 int err; 1844 1845 calltime = ktime_get(); 1846 1847 btintel_set_flag(hdev, INTEL_BOOTING); 1848 1849 err = btintel_send_intel_reset(hdev, boot_addr); 1850 if (err) { 1851 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err); 1852 btintel_reset_to_bootloader(hdev); 1853 return err; 1854 } 1855 1856 /* The bootloader will not indicate when the device is ready. This 1857 * is done by the operational firmware sending bootup notification. 1858 * 1859 * Booting into operational firmware should not take longer than 1860 * 1 second. However if that happens, then just fail the setup 1861 * since something went wrong. 1862 */ 1863 err = btintel_boot_wait(hdev, calltime, 1000); 1864 if (err == -ETIMEDOUT) 1865 btintel_reset_to_bootloader(hdev); 1866 1867 return err; 1868 } 1869 1870 static int btintel_get_fw_name(struct intel_version *ver, 1871 struct intel_boot_params *params, 1872 char *fw_name, size_t len, 1873 const char *suffix) 1874 { 1875 switch (ver->hw_variant) { 1876 case 0x0b: /* SfP */ 1877 case 0x0c: /* WsP */ 1878 snprintf(fw_name, len, "intel/ibt-%u-%u.%s", 1879 ver->hw_variant, 1880 le16_to_cpu(params->dev_revid), 1881 suffix); 1882 break; 1883 case 0x11: /* JfP */ 1884 case 0x12: /* ThP */ 1885 case 0x13: /* HrP */ 1886 case 0x14: /* CcP */ 1887 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s", 1888 ver->hw_variant, 1889 ver->hw_revision, 1890 ver->fw_revision, 1891 suffix); 1892 break; 1893 default: 1894 return -EINVAL; 1895 } 1896 1897 return 0; 1898 } 1899 1900 static int btintel_download_fw(struct hci_dev *hdev, 1901 struct intel_version *ver, 1902 struct intel_boot_params *params, 1903 u32 *boot_param) 1904 { 1905 const struct firmware *fw; 1906 char fwname[64]; 1907 int err; 1908 ktime_t calltime; 1909 1910 if (!ver || !params) 1911 return -EINVAL; 1912 1913 /* The firmware variant determines if the device is in bootloader 1914 * mode or is running operational firmware. The value 0x06 identifies 1915 * the bootloader and the value 0x23 identifies the operational 1916 * firmware. 1917 * 1918 * When the operational firmware is already present, then only 1919 * the check for valid Bluetooth device address is needed. This 1920 * determines if the device will be added as configured or 1921 * unconfigured controller. 1922 * 1923 * It is not possible to use the Secure Boot Parameters in this 1924 * case since that command is only available in bootloader mode. 1925 */ 1926 if (ver->fw_variant == 0x23) { 1927 btintel_clear_flag(hdev, INTEL_BOOTLOADER); 1928 btintel_check_bdaddr(hdev); 1929 1930 /* SfP and WsP don't seem to update the firmware version on file 1931 * so version checking is currently possible. 1932 */ 1933 switch (ver->hw_variant) { 1934 case 0x0b: /* SfP */ 1935 case 0x0c: /* WsP */ 1936 return 0; 1937 } 1938 1939 /* Proceed to download to check if the version matches */ 1940 goto download; 1941 } 1942 1943 /* Read the secure boot parameters to identify the operating 1944 * details of the bootloader. 1945 */ 1946 err = btintel_read_boot_params(hdev, params); 1947 if (err) 1948 return err; 1949 1950 /* It is required that every single firmware fragment is acknowledged 1951 * with a command complete event. If the boot parameters indicate 1952 * that this bootloader does not send them, then abort the setup. 1953 */ 1954 if (params->limited_cce != 0x00) { 1955 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)", 1956 params->limited_cce); 1957 return -EINVAL; 1958 } 1959 1960 /* If the OTP has no valid Bluetooth device address, then there will 1961 * also be no valid address for the operational firmware. 1962 */ 1963 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) { 1964 bt_dev_info(hdev, "No device address configured"); 1965 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks); 1966 } 1967 1968 download: 1969 /* With this Intel bootloader only the hardware variant and device 1970 * revision information are used to select the right firmware for SfP 1971 * and WsP. 1972 * 1973 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi. 1974 * 1975 * Currently the supported hardware variants are: 1976 * 11 (0x0b) for iBT3.0 (LnP/SfP) 1977 * 12 (0x0c) for iBT3.5 (WsP) 1978 * 1979 * For ThP/JfP and for future SKU's, the FW name varies based on HW 1980 * variant, HW revision and FW revision, as these are dependent on CNVi 1981 * and RF Combination. 1982 * 1983 * 17 (0x11) for iBT3.5 (JfP) 1984 * 18 (0x12) for iBT3.5 (ThP) 1985 * 1986 * The firmware file name for these will be 1987 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi. 1988 * 1989 */ 1990 err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi"); 1991 if (err < 0) { 1992 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) { 1993 /* Firmware has already been loaded */ 1994 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED); 1995 return 0; 1996 } 1997 1998 bt_dev_err(hdev, "Unsupported Intel firmware naming"); 1999 return -EINVAL; 2000 } 2001 2002 err = firmware_request_nowarn(&fw, fwname, &hdev->dev); 2003 if (err < 0) { 2004 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) { 2005 /* Firmware has already been loaded */ 2006 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED); 2007 return 0; 2008 } 2009 2010 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)", 2011 fwname, err); 2012 return err; 2013 } 2014 2015 bt_dev_info(hdev, "Found device firmware: %s", fwname); 2016 2017 if (fw->size < 644) { 2018 bt_dev_err(hdev, "Invalid size of firmware file (%zu)", 2019 fw->size); 2020 err = -EBADF; 2021 goto done; 2022 } 2023 2024 calltime = ktime_get(); 2025 2026 btintel_set_flag(hdev, INTEL_DOWNLOADING); 2027 2028 /* Start firmware downloading and get boot parameter */ 2029 err = btintel_download_firmware(hdev, ver, fw, boot_param); 2030 if (err < 0) { 2031 if (err == -EALREADY) { 2032 /* Firmware has already been loaded */ 2033 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED); 2034 err = 0; 2035 goto done; 2036 } 2037 2038 /* When FW download fails, send Intel Reset to retry 2039 * FW download. 2040 */ 2041 btintel_reset_to_bootloader(hdev); 2042 goto done; 2043 } 2044 2045 /* Before switching the device into operational mode and with that 2046 * booting the loaded firmware, wait for the bootloader notification 2047 * that all fragments have been successfully received. 2048 * 2049 * When the event processing receives the notification, then the 2050 * INTEL_DOWNLOADING flag will be cleared. 2051 * 2052 * The firmware loading should not take longer than 5 seconds 2053 * and thus just timeout if that happens and fail the setup 2054 * of this device. 2055 */ 2056 err = btintel_download_wait(hdev, calltime, 5000); 2057 if (err == -ETIMEDOUT) 2058 btintel_reset_to_bootloader(hdev); 2059 2060 done: 2061 release_firmware(fw); 2062 return err; 2063 } 2064 2065 static int btintel_bootloader_setup(struct hci_dev *hdev, 2066 struct intel_version *ver) 2067 { 2068 struct intel_version new_ver; 2069 struct intel_boot_params params; 2070 u32 boot_param; 2071 char ddcname[64]; 2072 int err; 2073 2074 BT_DBG("%s", hdev->name); 2075 2076 /* Set the default boot parameter to 0x0 and it is updated to 2077 * SKU specific boot parameter after reading Intel_Write_Boot_Params 2078 * command while downloading the firmware. 2079 */ 2080 boot_param = 0x00000000; 2081 2082 btintel_set_flag(hdev, INTEL_BOOTLOADER); 2083 2084 err = btintel_download_fw(hdev, ver, ¶ms, &boot_param); 2085 if (err) 2086 return err; 2087 2088 /* controller is already having an operational firmware */ 2089 if (ver->fw_variant == 0x23) 2090 goto finish; 2091 2092 err = btintel_boot(hdev, boot_param); 2093 if (err) 2094 return err; 2095 2096 btintel_clear_flag(hdev, INTEL_BOOTLOADER); 2097 2098 err = btintel_get_fw_name(ver, ¶ms, ddcname, 2099 sizeof(ddcname), "ddc"); 2100 2101 if (err < 0) { 2102 bt_dev_err(hdev, "Unsupported Intel firmware naming"); 2103 } else { 2104 /* Once the device is running in operational mode, it needs to 2105 * apply the device configuration (DDC) parameters. 2106 * 2107 * The device can work without DDC parameters, so even if it 2108 * fails to load the file, no need to fail the setup. 2109 */ 2110 btintel_load_ddc_config(hdev, ddcname); 2111 } 2112 2113 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT); 2114 2115 /* Read the Intel version information after loading the FW */ 2116 err = btintel_read_version(hdev, &new_ver); 2117 if (err) 2118 return err; 2119 2120 btintel_version_info(hdev, &new_ver); 2121 2122 finish: 2123 /* Set the event mask for Intel specific vendor events. This enables 2124 * a few extra events that are useful during general operation. It 2125 * does not enable any debugging related events. 2126 * 2127 * The device will function correctly without these events enabled 2128 * and thus no need to fail the setup. 2129 */ 2130 btintel_set_event_mask(hdev, false); 2131 2132 return 0; 2133 } 2134 2135 static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver, 2136 char *fw_name, size_t len, 2137 const char *suffix) 2138 { 2139 const char *format; 2140 u32 cnvi, cnvr; 2141 2142 cnvi = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top), 2143 INTEL_CNVX_TOP_STEP(ver->cnvi_top)); 2144 2145 cnvr = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top), 2146 INTEL_CNVX_TOP_STEP(ver->cnvr_top)); 2147 2148 /* Only Blazar product supports downloading of intermediate loader 2149 * image 2150 */ 2151 if (INTEL_HW_VARIANT(ver->cnvi_bt) >= 0x1e) { 2152 u8 zero[BTINTEL_FWID_MAXLEN]; 2153 2154 if (ver->img_type == BTINTEL_IMG_BOOTLOADER) { 2155 format = "intel/ibt-%04x-%04x-iml.%s"; 2156 snprintf(fw_name, len, format, cnvi, cnvr, suffix); 2157 return; 2158 } 2159 2160 memset(zero, 0, sizeof(zero)); 2161 2162 /* ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step-fw_id> */ 2163 if (memcmp(ver->fw_id, zero, sizeof(zero))) { 2164 format = "intel/ibt-%04x-%04x-%s.%s"; 2165 snprintf(fw_name, len, format, cnvi, cnvr, 2166 ver->fw_id, suffix); 2167 return; 2168 } 2169 /* If firmware id is not present, fallback to legacy naming 2170 * convention 2171 */ 2172 } 2173 /* Fallback to legacy naming convention for other controllers 2174 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step> 2175 */ 2176 format = "intel/ibt-%04x-%04x.%s"; 2177 snprintf(fw_name, len, format, cnvi, cnvr, suffix); 2178 } 2179 2180 static void btintel_get_iml_tlv(const struct intel_version_tlv *ver, 2181 char *fw_name, size_t len, 2182 const char *suffix) 2183 { 2184 const char *format; 2185 u32 cnvi, cnvr; 2186 2187 cnvi = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top), 2188 INTEL_CNVX_TOP_STEP(ver->cnvi_top)); 2189 2190 cnvr = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top), 2191 INTEL_CNVX_TOP_STEP(ver->cnvr_top)); 2192 2193 format = "intel/ibt-%04x-%04x-iml.%s"; 2194 snprintf(fw_name, len, format, cnvi, cnvr, suffix); 2195 } 2196 2197 static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev, 2198 struct intel_version_tlv *ver, 2199 u32 *boot_param) 2200 { 2201 const struct firmware *fw; 2202 char fwname[128]; 2203 int err; 2204 ktime_t calltime; 2205 2206 if (!ver || !boot_param) 2207 return -EINVAL; 2208 2209 /* The firmware variant determines if the device is in bootloader 2210 * mode or is running operational firmware. The value 0x03 identifies 2211 * the bootloader and the value 0x23 identifies the operational 2212 * firmware. 2213 * 2214 * When the operational firmware is already present, then only 2215 * the check for valid Bluetooth device address is needed. This 2216 * determines if the device will be added as configured or 2217 * unconfigured controller. 2218 * 2219 * It is not possible to use the Secure Boot Parameters in this 2220 * case since that command is only available in bootloader mode. 2221 */ 2222 if (ver->img_type == BTINTEL_IMG_OP) { 2223 btintel_clear_flag(hdev, INTEL_BOOTLOADER); 2224 btintel_check_bdaddr(hdev); 2225 } else { 2226 /* 2227 * Check for valid bd address in boot loader mode. Device 2228 * will be marked as unconfigured if empty bd address is 2229 * found. 2230 */ 2231 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) { 2232 bt_dev_info(hdev, "No device address configured"); 2233 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks); 2234 } 2235 } 2236 2237 if (ver->img_type == BTINTEL_IMG_OP) { 2238 /* Controller running OP image. In case of FW downgrade, 2239 * FWID TLV may not be present and driver may attempt to load 2240 * firmware image which doesn't exist. Lets compare the version 2241 * of IML image 2242 */ 2243 if (INTEL_HW_VARIANT(ver->cnvi_bt) >= 0x1e) 2244 btintel_get_iml_tlv(ver, fwname, sizeof(fwname), "sfi"); 2245 else 2246 btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi"); 2247 } else { 2248 btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi"); 2249 } 2250 2251 err = firmware_request_nowarn(&fw, fwname, &hdev->dev); 2252 if (err < 0) { 2253 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) { 2254 /* Firmware has already been loaded */ 2255 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED); 2256 return 0; 2257 } 2258 2259 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)", 2260 fwname, err); 2261 2262 return err; 2263 } 2264 2265 bt_dev_info(hdev, "Found device firmware: %s", fwname); 2266 2267 if (fw->size < 644) { 2268 bt_dev_err(hdev, "Invalid size of firmware file (%zu)", 2269 fw->size); 2270 err = -EBADF; 2271 goto done; 2272 } 2273 2274 calltime = ktime_get(); 2275 2276 btintel_set_flag(hdev, INTEL_DOWNLOADING); 2277 2278 /* Start firmware downloading and get boot parameter */ 2279 err = btintel_download_fw_tlv(hdev, ver, fw, boot_param, 2280 INTEL_HW_VARIANT(ver->cnvi_bt), 2281 ver->sbe_type); 2282 if (err < 0) { 2283 if (err == -EALREADY) { 2284 /* Firmware has already been loaded */ 2285 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED); 2286 err = 0; 2287 goto done; 2288 } 2289 2290 /* When FW download fails, send Intel Reset to retry 2291 * FW download. 2292 */ 2293 btintel_reset_to_bootloader(hdev); 2294 goto done; 2295 } 2296 2297 /* Before switching the device into operational mode and with that 2298 * booting the loaded firmware, wait for the bootloader notification 2299 * that all fragments have been successfully received. 2300 * 2301 * When the event processing receives the notification, then the 2302 * BTUSB_DOWNLOADING flag will be cleared. 2303 * 2304 * The firmware loading should not take longer than 5 seconds 2305 * and thus just timeout if that happens and fail the setup 2306 * of this device. 2307 */ 2308 err = btintel_download_wait(hdev, calltime, 5000); 2309 if (err == -ETIMEDOUT) 2310 btintel_reset_to_bootloader(hdev); 2311 2312 done: 2313 release_firmware(fw); 2314 return err; 2315 } 2316 2317 static int btintel_get_codec_config_data(struct hci_dev *hdev, 2318 __u8 link, struct bt_codec *codec, 2319 __u8 *ven_len, __u8 **ven_data) 2320 { 2321 int err = 0; 2322 2323 if (!ven_data || !ven_len) 2324 return -EINVAL; 2325 2326 *ven_len = 0; 2327 *ven_data = NULL; 2328 2329 if (link != ESCO_LINK) { 2330 bt_dev_err(hdev, "Invalid link type(%u)", link); 2331 return -EINVAL; 2332 } 2333 2334 *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL); 2335 if (!*ven_data) { 2336 err = -ENOMEM; 2337 goto error; 2338 } 2339 2340 /* supports only CVSD and mSBC offload codecs */ 2341 switch (codec->id) { 2342 case 0x02: 2343 **ven_data = 0x00; 2344 break; 2345 case 0x05: 2346 **ven_data = 0x01; 2347 break; 2348 default: 2349 err = -EINVAL; 2350 bt_dev_err(hdev, "Invalid codec id(%u)", codec->id); 2351 goto error; 2352 } 2353 /* codec and its capabilities are pre-defined to ids 2354 * preset id = 0x00 represents CVSD codec with sampling rate 8K 2355 * preset id = 0x01 represents mSBC codec with sampling rate 16K 2356 */ 2357 *ven_len = sizeof(__u8); 2358 return err; 2359 2360 error: 2361 kfree(*ven_data); 2362 *ven_data = NULL; 2363 return err; 2364 } 2365 2366 static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id) 2367 { 2368 /* Intel uses 1 as data path id for all the usecases */ 2369 *data_path_id = 1; 2370 return 0; 2371 } 2372 2373 static int btintel_configure_offload(struct hci_dev *hdev) 2374 { 2375 struct sk_buff *skb; 2376 int err = 0; 2377 struct intel_offload_use_cases *use_cases; 2378 2379 skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT); 2380 if (IS_ERR(skb)) { 2381 bt_dev_err(hdev, "Reading offload use cases failed (%ld)", 2382 PTR_ERR(skb)); 2383 return PTR_ERR(skb); 2384 } 2385 2386 if (skb->len < sizeof(*use_cases)) { 2387 err = -EIO; 2388 goto error; 2389 } 2390 2391 use_cases = (void *)skb->data; 2392 2393 if (use_cases->status) { 2394 err = -bt_to_errno(skb->data[0]); 2395 goto error; 2396 } 2397 2398 if (use_cases->preset[0] & 0x03) { 2399 hdev->get_data_path_id = btintel_get_data_path_id; 2400 hdev->get_codec_config_data = btintel_get_codec_config_data; 2401 } 2402 error: 2403 kfree_skb(skb); 2404 return err; 2405 } 2406 2407 static void btintel_set_ppag(struct hci_dev *hdev, struct intel_version_tlv *ver) 2408 { 2409 struct sk_buff *skb; 2410 struct hci_ppag_enable_cmd ppag_cmd; 2411 acpi_handle handle; 2412 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 2413 union acpi_object *p, *elements; 2414 u32 domain, mode; 2415 acpi_status status; 2416 2417 /* PPAG is not supported if CRF is HrP2, Jfp2, JfP1 */ 2418 switch (ver->cnvr_top & 0xFFF) { 2419 case 0x504: /* Hrp2 */ 2420 case 0x202: /* Jfp2 */ 2421 case 0x201: /* Jfp1 */ 2422 bt_dev_dbg(hdev, "PPAG not supported for Intel CNVr (0x%3x)", 2423 ver->cnvr_top & 0xFFF); 2424 return; 2425 } 2426 2427 handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev)); 2428 if (!handle) { 2429 bt_dev_info(hdev, "No support for BT device in ACPI firmware"); 2430 return; 2431 } 2432 2433 status = acpi_evaluate_object(handle, "PPAG", NULL, &buffer); 2434 if (ACPI_FAILURE(status)) { 2435 if (status == AE_NOT_FOUND) { 2436 bt_dev_dbg(hdev, "PPAG-BT: ACPI entry not found"); 2437 return; 2438 } 2439 bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status)); 2440 return; 2441 } 2442 2443 p = buffer.pointer; 2444 if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 2) { 2445 bt_dev_warn(hdev, "PPAG-BT: Invalid object type: %d or package count: %d", 2446 p->type, p->package.count); 2447 kfree(buffer.pointer); 2448 return; 2449 } 2450 2451 elements = p->package.elements; 2452 2453 /* PPAG table is located at element[1] */ 2454 p = &elements[1]; 2455 2456 domain = (u32)p->package.elements[0].integer.value; 2457 mode = (u32)p->package.elements[1].integer.value; 2458 kfree(buffer.pointer); 2459 2460 if (domain != 0x12) { 2461 bt_dev_dbg(hdev, "PPAG-BT: Bluetooth domain is disabled in ACPI firmware"); 2462 return; 2463 } 2464 2465 /* PPAG mode 2466 * BIT 0 : 0 Disabled in EU 2467 * 1 Enabled in EU 2468 * BIT 1 : 0 Disabled in China 2469 * 1 Enabled in China 2470 */ 2471 mode &= 0x03; 2472 2473 if (!mode) { 2474 bt_dev_dbg(hdev, "PPAG-BT: EU, China mode are disabled in BIOS"); 2475 return; 2476 } 2477 2478 ppag_cmd.ppag_enable_flags = cpu_to_le32(mode); 2479 2480 skb = __hci_cmd_sync(hdev, INTEL_OP_PPAG_CMD, sizeof(ppag_cmd), 2481 &ppag_cmd, HCI_CMD_TIMEOUT); 2482 if (IS_ERR(skb)) { 2483 bt_dev_warn(hdev, "Failed to send PPAG Enable (%ld)", PTR_ERR(skb)); 2484 return; 2485 } 2486 bt_dev_info(hdev, "PPAG-BT: Enabled (Mode %d)", mode); 2487 kfree_skb(skb); 2488 } 2489 2490 static int btintel_acpi_reset_method(struct hci_dev *hdev) 2491 { 2492 int ret = 0; 2493 acpi_status status; 2494 union acpi_object *p, *ref; 2495 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 2496 2497 status = acpi_evaluate_object(ACPI_HANDLE(GET_HCIDEV_DEV(hdev)), "_PRR", NULL, &buffer); 2498 if (ACPI_FAILURE(status)) { 2499 bt_dev_err(hdev, "Failed to run _PRR method"); 2500 ret = -ENODEV; 2501 return ret; 2502 } 2503 p = buffer.pointer; 2504 2505 if (p->package.count != 1 || p->type != ACPI_TYPE_PACKAGE) { 2506 bt_dev_err(hdev, "Invalid arguments"); 2507 ret = -EINVAL; 2508 goto exit_on_error; 2509 } 2510 2511 ref = &p->package.elements[0]; 2512 if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) { 2513 bt_dev_err(hdev, "Invalid object type: 0x%x", ref->type); 2514 ret = -EINVAL; 2515 goto exit_on_error; 2516 } 2517 2518 status = acpi_evaluate_object(ref->reference.handle, "_RST", NULL, NULL); 2519 if (ACPI_FAILURE(status)) { 2520 bt_dev_err(hdev, "Failed to run_RST method"); 2521 ret = -ENODEV; 2522 goto exit_on_error; 2523 } 2524 2525 exit_on_error: 2526 kfree(buffer.pointer); 2527 return ret; 2528 } 2529 2530 static void btintel_set_dsm_reset_method(struct hci_dev *hdev, 2531 struct intel_version_tlv *ver_tlv) 2532 { 2533 struct btintel_data *data = hci_get_priv(hdev); 2534 acpi_handle handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev)); 2535 u8 reset_payload[4] = {0x01, 0x00, 0x01, 0x00}; 2536 union acpi_object *obj, argv4; 2537 enum { 2538 RESET_TYPE_WDISABLE2, 2539 RESET_TYPE_VSEC 2540 }; 2541 2542 handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev)); 2543 2544 if (!handle) { 2545 bt_dev_dbg(hdev, "No support for bluetooth device in ACPI firmware"); 2546 return; 2547 } 2548 2549 if (!acpi_has_method(handle, "_PRR")) { 2550 bt_dev_err(hdev, "No support for _PRR ACPI method"); 2551 return; 2552 } 2553 2554 switch (ver_tlv->cnvi_top & 0xfff) { 2555 case 0x910: /* GalePeak2 */ 2556 reset_payload[2] = RESET_TYPE_VSEC; 2557 break; 2558 default: 2559 /* WDISABLE2 is the default reset method */ 2560 reset_payload[2] = RESET_TYPE_WDISABLE2; 2561 2562 if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0, 2563 BIT(DSM_SET_WDISABLE2_DELAY))) { 2564 bt_dev_err(hdev, "No dsm support to set reset delay"); 2565 return; 2566 } 2567 argv4.integer.type = ACPI_TYPE_INTEGER; 2568 /* delay required to toggle BT power */ 2569 argv4.integer.value = 160; 2570 obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0, 2571 DSM_SET_WDISABLE2_DELAY, &argv4); 2572 if (!obj) { 2573 bt_dev_err(hdev, "Failed to call dsm to set reset delay"); 2574 return; 2575 } 2576 ACPI_FREE(obj); 2577 } 2578 2579 bt_dev_info(hdev, "DSM reset method type: 0x%02x", reset_payload[2]); 2580 2581 if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0, 2582 DSM_SET_RESET_METHOD)) { 2583 bt_dev_warn(hdev, "No support for dsm to set reset method"); 2584 return; 2585 } 2586 argv4.buffer.type = ACPI_TYPE_BUFFER; 2587 argv4.buffer.length = sizeof(reset_payload); 2588 argv4.buffer.pointer = reset_payload; 2589 2590 obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0, 2591 DSM_SET_RESET_METHOD, &argv4); 2592 if (!obj) { 2593 bt_dev_err(hdev, "Failed to call dsm to set reset method"); 2594 return; 2595 } 2596 ACPI_FREE(obj); 2597 data->acpi_reset_method = btintel_acpi_reset_method; 2598 } 2599 2600 #define BTINTEL_ISODATA_HANDLE_BASE 0x900 2601 2602 static u8 btintel_classify_pkt_type(struct hci_dev *hdev, struct sk_buff *skb) 2603 { 2604 /* 2605 * Distinguish ISO data packets form ACL data packets 2606 * based on their connection handle value range. 2607 */ 2608 if (hci_skb_pkt_type(skb) == HCI_ACLDATA_PKT) { 2609 __u16 handle = __le16_to_cpu(hci_acl_hdr(skb)->handle); 2610 2611 if (hci_handle(handle) >= BTINTEL_ISODATA_HANDLE_BASE) 2612 return HCI_ISODATA_PKT; 2613 } 2614 2615 return hci_skb_pkt_type(skb); 2616 } 2617 2618 int btintel_bootloader_setup_tlv(struct hci_dev *hdev, 2619 struct intel_version_tlv *ver) 2620 { 2621 u32 boot_param; 2622 char ddcname[64]; 2623 int err; 2624 struct intel_version_tlv new_ver; 2625 2626 bt_dev_dbg(hdev, ""); 2627 2628 /* Set the default boot parameter to 0x0 and it is updated to 2629 * SKU specific boot parameter after reading Intel_Write_Boot_Params 2630 * command while downloading the firmware. 2631 */ 2632 boot_param = 0x00000000; 2633 2634 btintel_set_flag(hdev, INTEL_BOOTLOADER); 2635 2636 err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param); 2637 if (err) 2638 return err; 2639 2640 /* check if controller is already having an operational firmware */ 2641 if (ver->img_type == BTINTEL_IMG_OP) 2642 goto finish; 2643 2644 err = btintel_boot(hdev, boot_param); 2645 if (err) 2646 return err; 2647 2648 err = btintel_read_version_tlv(hdev, ver); 2649 if (err) 2650 return err; 2651 2652 /* If image type returned is BTINTEL_IMG_IML, then controller supports 2653 * intermediate loader image 2654 */ 2655 if (ver->img_type == BTINTEL_IMG_IML) { 2656 err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param); 2657 if (err) 2658 return err; 2659 2660 err = btintel_boot(hdev, boot_param); 2661 if (err) 2662 return err; 2663 } 2664 2665 btintel_clear_flag(hdev, INTEL_BOOTLOADER); 2666 2667 btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc"); 2668 /* Once the device is running in operational mode, it needs to 2669 * apply the device configuration (DDC) parameters. 2670 * 2671 * The device can work without DDC parameters, so even if it 2672 * fails to load the file, no need to fail the setup. 2673 */ 2674 btintel_load_ddc_config(hdev, ddcname); 2675 2676 /* Read supported use cases and set callbacks to fetch datapath id */ 2677 btintel_configure_offload(hdev); 2678 2679 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT); 2680 2681 /* Set PPAG feature */ 2682 btintel_set_ppag(hdev, ver); 2683 2684 /* Read the Intel version information after loading the FW */ 2685 err = btintel_read_version_tlv(hdev, &new_ver); 2686 if (err) 2687 return err; 2688 2689 btintel_version_info_tlv(hdev, &new_ver); 2690 2691 finish: 2692 /* Set the event mask for Intel specific vendor events. This enables 2693 * a few extra events that are useful during general operation. It 2694 * does not enable any debugging related events. 2695 * 2696 * The device will function correctly without these events enabled 2697 * and thus no need to fail the setup. 2698 */ 2699 btintel_set_event_mask(hdev, false); 2700 2701 return 0; 2702 } 2703 EXPORT_SYMBOL_GPL(btintel_bootloader_setup_tlv); 2704 2705 void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant) 2706 { 2707 switch (hw_variant) { 2708 /* Legacy bootloader devices that supports MSFT Extension */ 2709 case 0x11: /* JfP */ 2710 case 0x12: /* ThP */ 2711 case 0x13: /* HrP */ 2712 case 0x14: /* CcP */ 2713 /* All Intel new genration controllers support the Microsoft vendor 2714 * extension are using 0xFC1E for VsMsftOpCode. 2715 */ 2716 case 0x17: 2717 case 0x18: 2718 case 0x19: 2719 case 0x1b: 2720 case 0x1c: 2721 case 0x1e: 2722 hci_set_msft_opcode(hdev, 0xFC1E); 2723 break; 2724 default: 2725 /* Not supported */ 2726 break; 2727 } 2728 } 2729 EXPORT_SYMBOL_GPL(btintel_set_msft_opcode); 2730 2731 void btintel_print_fseq_info(struct hci_dev *hdev) 2732 { 2733 struct sk_buff *skb; 2734 u8 *p; 2735 u32 val; 2736 const char *str; 2737 2738 skb = __hci_cmd_sync(hdev, 0xfcb3, 0, NULL, HCI_CMD_TIMEOUT); 2739 if (IS_ERR(skb)) { 2740 bt_dev_dbg(hdev, "Reading fseq status command failed (%ld)", 2741 PTR_ERR(skb)); 2742 return; 2743 } 2744 2745 if (skb->len < (sizeof(u32) * 16 + 2)) { 2746 bt_dev_dbg(hdev, "Malformed packet of length %u received", 2747 skb->len); 2748 kfree_skb(skb); 2749 return; 2750 } 2751 2752 p = skb_pull_data(skb, 1); 2753 if (*p) { 2754 bt_dev_dbg(hdev, "Failed to get fseq status (0x%2.2x)", *p); 2755 kfree_skb(skb); 2756 return; 2757 } 2758 2759 p = skb_pull_data(skb, 1); 2760 switch (*p) { 2761 case 0: 2762 str = "Success"; 2763 break; 2764 case 1: 2765 str = "Fatal error"; 2766 break; 2767 case 2: 2768 str = "Semaphore acquire error"; 2769 break; 2770 default: 2771 str = "Unknown error"; 2772 break; 2773 } 2774 2775 if (*p) { 2776 bt_dev_err(hdev, "Fseq status: %s (0x%2.2x)", str, *p); 2777 kfree_skb(skb); 2778 return; 2779 } 2780 2781 bt_dev_info(hdev, "Fseq status: %s (0x%2.2x)", str, *p); 2782 2783 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2784 bt_dev_dbg(hdev, "Reason: 0x%8.8x", val); 2785 2786 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2787 bt_dev_dbg(hdev, "Global version: 0x%8.8x", val); 2788 2789 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2790 bt_dev_dbg(hdev, "Installed version: 0x%8.8x", val); 2791 2792 p = skb->data; 2793 skb_pull_data(skb, 4); 2794 bt_dev_info(hdev, "Fseq executed: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1], 2795 p[2], p[3]); 2796 2797 p = skb->data; 2798 skb_pull_data(skb, 4); 2799 bt_dev_info(hdev, "Fseq BT Top: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1], 2800 p[2], p[3]); 2801 2802 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2803 bt_dev_dbg(hdev, "Fseq Top init version: 0x%8.8x", val); 2804 2805 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2806 bt_dev_dbg(hdev, "Fseq Cnvio init version: 0x%8.8x", val); 2807 2808 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2809 bt_dev_dbg(hdev, "Fseq MBX Wifi file version: 0x%8.8x", val); 2810 2811 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2812 bt_dev_dbg(hdev, "Fseq BT version: 0x%8.8x", val); 2813 2814 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2815 bt_dev_dbg(hdev, "Fseq Top reset address: 0x%8.8x", val); 2816 2817 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2818 bt_dev_dbg(hdev, "Fseq MBX timeout: 0x%8.8x", val); 2819 2820 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2821 bt_dev_dbg(hdev, "Fseq MBX ack: 0x%8.8x", val); 2822 2823 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2824 bt_dev_dbg(hdev, "Fseq CNVi id: 0x%8.8x", val); 2825 2826 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2827 bt_dev_dbg(hdev, "Fseq CNVr id: 0x%8.8x", val); 2828 2829 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2830 bt_dev_dbg(hdev, "Fseq Error handle: 0x%8.8x", val); 2831 2832 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2833 bt_dev_dbg(hdev, "Fseq Magic noalive indication: 0x%8.8x", val); 2834 2835 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2836 bt_dev_dbg(hdev, "Fseq OTP version: 0x%8.8x", val); 2837 2838 val = get_unaligned_le32(skb_pull_data(skb, 4)); 2839 bt_dev_dbg(hdev, "Fseq MBX otp version: 0x%8.8x", val); 2840 2841 kfree_skb(skb); 2842 } 2843 EXPORT_SYMBOL_GPL(btintel_print_fseq_info); 2844 2845 static int btintel_setup_combined(struct hci_dev *hdev) 2846 { 2847 const u8 param[1] = { 0xFF }; 2848 struct intel_version ver; 2849 struct intel_version_tlv ver_tlv; 2850 struct sk_buff *skb; 2851 int err; 2852 2853 BT_DBG("%s", hdev->name); 2854 2855 /* The some controllers have a bug with the first HCI command sent to it 2856 * returning number of completed commands as zero. This would stall the 2857 * command processing in the Bluetooth core. 2858 * 2859 * As a workaround, send HCI Reset command first which will reset the 2860 * number of completed commands and allow normal command processing 2861 * from now on. 2862 * 2863 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe 2864 * in the SW_RFKILL ON state as a workaround of fixing LED issue during 2865 * the shutdown() procedure, and once the device is in SW_RFKILL ON 2866 * state, the only way to exit out of it is sending the HCI_Reset 2867 * command. 2868 */ 2869 if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) || 2870 btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) { 2871 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, 2872 HCI_INIT_TIMEOUT); 2873 if (IS_ERR(skb)) { 2874 bt_dev_err(hdev, 2875 "sending initial HCI reset failed (%ld)", 2876 PTR_ERR(skb)); 2877 return PTR_ERR(skb); 2878 } 2879 kfree_skb(skb); 2880 } 2881 2882 /* Starting from TyP device, the command parameter and response are 2883 * changed even though the OCF for HCI_Intel_Read_Version command 2884 * remains same. The legacy devices can handle even if the 2885 * command has a parameter and returns a correct version information. 2886 * So, it uses new format to support both legacy and new format. 2887 */ 2888 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT); 2889 if (IS_ERR(skb)) { 2890 bt_dev_err(hdev, "Reading Intel version command failed (%ld)", 2891 PTR_ERR(skb)); 2892 return PTR_ERR(skb); 2893 } 2894 2895 /* Check the status */ 2896 if (skb->data[0]) { 2897 bt_dev_err(hdev, "Intel Read Version command failed (%02x)", 2898 skb->data[0]); 2899 err = -EIO; 2900 goto exit_error; 2901 } 2902 2903 /* Apply the common HCI quirks for Intel device */ 2904 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks); 2905 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); 2906 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks); 2907 2908 /* Set up the quality report callback for Intel devices */ 2909 hdev->set_quality_report = btintel_set_quality_report; 2910 2911 /* For Legacy device, check the HW platform value and size */ 2912 if (skb->len == sizeof(ver) && skb->data[1] == 0x37) { 2913 bt_dev_dbg(hdev, "Read the legacy Intel version information"); 2914 2915 memcpy(&ver, skb->data, sizeof(ver)); 2916 2917 /* Display version information */ 2918 btintel_version_info(hdev, &ver); 2919 2920 /* Check for supported iBT hardware variants of this firmware 2921 * loading method. 2922 * 2923 * This check has been put in place to ensure correct forward 2924 * compatibility options when newer hardware variants come 2925 * along. 2926 */ 2927 switch (ver.hw_variant) { 2928 case 0x07: /* WP */ 2929 case 0x08: /* StP */ 2930 /* Legacy ROM product */ 2931 btintel_set_flag(hdev, INTEL_ROM_LEGACY); 2932 2933 /* Apply the device specific HCI quirks 2934 * 2935 * WBS for SdP - For the Legacy ROM products, only SdP 2936 * supports the WBS. But the version information is not 2937 * enough to use here because the StP2 and SdP have same 2938 * hw_variant and fw_variant. So, this flag is set by 2939 * the transport driver (btusb) based on the HW info 2940 * (idProduct) 2941 */ 2942 if (!btintel_test_flag(hdev, 2943 INTEL_ROM_LEGACY_NO_WBS_SUPPORT)) 2944 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, 2945 &hdev->quirks); 2946 if (ver.hw_variant == 0x08 && ver.fw_variant == 0x22) 2947 set_bit(HCI_QUIRK_VALID_LE_STATES, 2948 &hdev->quirks); 2949 2950 err = btintel_legacy_rom_setup(hdev, &ver); 2951 break; 2952 case 0x0b: /* SfP */ 2953 case 0x11: /* JfP */ 2954 case 0x12: /* ThP */ 2955 case 0x13: /* HrP */ 2956 case 0x14: /* CcP */ 2957 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks); 2958 fallthrough; 2959 case 0x0c: /* WsP */ 2960 /* Apply the device specific HCI quirks 2961 * 2962 * All Legacy bootloader devices support WBS 2963 */ 2964 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, 2965 &hdev->quirks); 2966 2967 /* These variants don't seem to support LE Coded PHY */ 2968 set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks); 2969 2970 /* Setup MSFT Extension support */ 2971 btintel_set_msft_opcode(hdev, ver.hw_variant); 2972 2973 err = btintel_bootloader_setup(hdev, &ver); 2974 btintel_register_devcoredump_support(hdev); 2975 break; 2976 default: 2977 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)", 2978 ver.hw_variant); 2979 err = -EINVAL; 2980 } 2981 2982 hci_set_hw_info(hdev, 2983 "INTEL platform=%u variant=%u revision=%u", 2984 ver.hw_platform, ver.hw_variant, 2985 ver.hw_revision); 2986 2987 goto exit_error; 2988 } 2989 2990 /* memset ver_tlv to start with clean state as few fields are exclusive 2991 * to bootloader mode and are not populated in operational mode 2992 */ 2993 memset(&ver_tlv, 0, sizeof(ver_tlv)); 2994 /* For TLV type device, parse the tlv data */ 2995 err = btintel_parse_version_tlv(hdev, &ver_tlv, skb); 2996 if (err) { 2997 bt_dev_err(hdev, "Failed to parse TLV version information"); 2998 goto exit_error; 2999 } 3000 3001 if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) { 3002 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)", 3003 INTEL_HW_PLATFORM(ver_tlv.cnvi_bt)); 3004 err = -EINVAL; 3005 goto exit_error; 3006 } 3007 3008 /* Check for supported iBT hardware variants of this firmware 3009 * loading method. 3010 * 3011 * This check has been put in place to ensure correct forward 3012 * compatibility options when newer hardware variants come 3013 * along. 3014 */ 3015 switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) { 3016 case 0x11: /* JfP */ 3017 case 0x12: /* ThP */ 3018 case 0x13: /* HrP */ 3019 case 0x14: /* CcP */ 3020 /* Some legacy bootloader devices starting from JfP, 3021 * the operational firmware supports both old and TLV based 3022 * HCI_Intel_Read_Version command based on the command 3023 * parameter. 3024 * 3025 * For upgrading firmware case, the TLV based version cannot 3026 * be used because the firmware filename for legacy bootloader 3027 * is based on the old format. 3028 * 3029 * Also, it is not easy to convert TLV based version from the 3030 * legacy version format. 3031 * 3032 * So, as a workaround for those devices, use the legacy 3033 * HCI_Intel_Read_Version to get the version information and 3034 * run the legacy bootloader setup. 3035 */ 3036 err = btintel_read_version(hdev, &ver); 3037 if (err) 3038 break; 3039 3040 /* Apply the device specific HCI quirks 3041 * 3042 * All Legacy bootloader devices support WBS 3043 */ 3044 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks); 3045 3046 /* These variants don't seem to support LE Coded PHY */ 3047 set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks); 3048 3049 /* Set Valid LE States quirk */ 3050 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks); 3051 3052 /* Setup MSFT Extension support */ 3053 btintel_set_msft_opcode(hdev, ver.hw_variant); 3054 3055 err = btintel_bootloader_setup(hdev, &ver); 3056 btintel_register_devcoredump_support(hdev); 3057 break; 3058 case 0x18: /* GfP2 */ 3059 case 0x1c: /* GaP */ 3060 /* Re-classify packet type for controllers with LE audio */ 3061 hdev->classify_pkt_type = btintel_classify_pkt_type; 3062 fallthrough; 3063 case 0x17: 3064 case 0x19: 3065 case 0x1b: 3066 case 0x1e: 3067 /* Display version information of TLV type */ 3068 btintel_version_info_tlv(hdev, &ver_tlv); 3069 3070 /* Apply the device specific HCI quirks for TLV based devices 3071 * 3072 * All TLV based devices support WBS 3073 */ 3074 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks); 3075 3076 /* Apply LE States quirk from solar onwards */ 3077 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks); 3078 3079 /* Setup MSFT Extension support */ 3080 btintel_set_msft_opcode(hdev, 3081 INTEL_HW_VARIANT(ver_tlv.cnvi_bt)); 3082 btintel_set_dsm_reset_method(hdev, &ver_tlv); 3083 3084 err = btintel_bootloader_setup_tlv(hdev, &ver_tlv); 3085 btintel_register_devcoredump_support(hdev); 3086 btintel_print_fseq_info(hdev); 3087 break; 3088 default: 3089 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)", 3090 INTEL_HW_VARIANT(ver_tlv.cnvi_bt)); 3091 err = -EINVAL; 3092 break; 3093 } 3094 3095 hci_set_hw_info(hdev, "INTEL platform=%u variant=%u", 3096 INTEL_HW_PLATFORM(ver_tlv.cnvi_bt), 3097 INTEL_HW_VARIANT(ver_tlv.cnvi_bt)); 3098 3099 exit_error: 3100 kfree_skb(skb); 3101 3102 return err; 3103 } 3104 3105 int btintel_shutdown_combined(struct hci_dev *hdev) 3106 { 3107 struct sk_buff *skb; 3108 int ret; 3109 3110 /* Send HCI Reset to the controller to stop any BT activity which 3111 * were triggered. This will help to save power and maintain the 3112 * sync b/w Host and controller 3113 */ 3114 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); 3115 if (IS_ERR(skb)) { 3116 bt_dev_err(hdev, "HCI reset during shutdown failed"); 3117 return PTR_ERR(skb); 3118 } 3119 kfree_skb(skb); 3120 3121 3122 /* Some platforms have an issue with BT LED when the interface is 3123 * down or BT radio is turned off, which takes 5 seconds to BT LED 3124 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the 3125 * device in the RFKILL ON state which turns off the BT LED immediately. 3126 */ 3127 if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) { 3128 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT); 3129 if (IS_ERR(skb)) { 3130 ret = PTR_ERR(skb); 3131 bt_dev_err(hdev, "turning off Intel device LED failed"); 3132 return ret; 3133 } 3134 kfree_skb(skb); 3135 } 3136 3137 return 0; 3138 } 3139 EXPORT_SYMBOL_GPL(btintel_shutdown_combined); 3140 3141 int btintel_configure_setup(struct hci_dev *hdev, const char *driver_name) 3142 { 3143 hdev->manufacturer = 2; 3144 hdev->setup = btintel_setup_combined; 3145 hdev->shutdown = btintel_shutdown_combined; 3146 hdev->hw_error = btintel_hw_error; 3147 hdev->set_diag = btintel_set_diag_combined; 3148 hdev->set_bdaddr = btintel_set_bdaddr; 3149 3150 coredump_info.driver_name = driver_name; 3151 3152 return 0; 3153 } 3154 EXPORT_SYMBOL_GPL(btintel_configure_setup); 3155 3156 static int btintel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb) 3157 { 3158 struct intel_tlv *tlv = (void *)&skb->data[5]; 3159 3160 /* The first event is always an event type TLV */ 3161 if (tlv->type != INTEL_TLV_TYPE_ID) 3162 goto recv_frame; 3163 3164 switch (tlv->val[0]) { 3165 case INTEL_TLV_SYSTEM_EXCEPTION: 3166 case INTEL_TLV_FATAL_EXCEPTION: 3167 case INTEL_TLV_DEBUG_EXCEPTION: 3168 case INTEL_TLV_TEST_EXCEPTION: 3169 /* Generate devcoredump from exception */ 3170 if (!hci_devcd_init(hdev, skb->len)) { 3171 hci_devcd_append(hdev, skb); 3172 hci_devcd_complete(hdev); 3173 } else { 3174 bt_dev_err(hdev, "Failed to generate devcoredump"); 3175 kfree_skb(skb); 3176 } 3177 return 0; 3178 default: 3179 bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]); 3180 } 3181 3182 recv_frame: 3183 return hci_recv_frame(hdev, skb); 3184 } 3185 3186 int btintel_recv_event(struct hci_dev *hdev, struct sk_buff *skb) 3187 { 3188 struct hci_event_hdr *hdr = (void *)skb->data; 3189 const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 }; 3190 3191 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff && 3192 hdr->plen > 0) { 3193 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1; 3194 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1; 3195 3196 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) { 3197 switch (skb->data[2]) { 3198 case 0x02: 3199 /* When switching to the operational firmware 3200 * the device sends a vendor specific event 3201 * indicating that the bootup completed. 3202 */ 3203 btintel_bootup(hdev, ptr, len); 3204 break; 3205 case 0x06: 3206 /* When the firmware loading completes the 3207 * device sends out a vendor specific event 3208 * indicating the result of the firmware 3209 * loading. 3210 */ 3211 btintel_secure_send_result(hdev, ptr, len); 3212 break; 3213 } 3214 } 3215 3216 /* Handle all diagnostics events separately. May still call 3217 * hci_recv_frame. 3218 */ 3219 if (len >= sizeof(diagnostics_hdr) && 3220 memcmp(&skb->data[2], diagnostics_hdr, 3221 sizeof(diagnostics_hdr)) == 0) { 3222 return btintel_diagnostics(hdev, skb); 3223 } 3224 } 3225 3226 return hci_recv_frame(hdev, skb); 3227 } 3228 EXPORT_SYMBOL_GPL(btintel_recv_event); 3229 3230 void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len) 3231 { 3232 const struct intel_bootup *evt = ptr; 3233 3234 if (len != sizeof(*evt)) 3235 return; 3236 3237 if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING)) 3238 btintel_wake_up_flag(hdev, INTEL_BOOTING); 3239 } 3240 EXPORT_SYMBOL_GPL(btintel_bootup); 3241 3242 void btintel_secure_send_result(struct hci_dev *hdev, 3243 const void *ptr, unsigned int len) 3244 { 3245 const struct intel_secure_send_result *evt = ptr; 3246 3247 if (len != sizeof(*evt)) 3248 return; 3249 3250 if (evt->result) 3251 btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED); 3252 3253 if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) && 3254 btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED)) 3255 btintel_wake_up_flag(hdev, INTEL_DOWNLOADING); 3256 } 3257 EXPORT_SYMBOL_GPL(btintel_secure_send_result); 3258 3259 MODULE_AUTHOR("Marcel Holtmann <[email protected]>"); 3260 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION); 3261 MODULE_VERSION(VERSION); 3262 MODULE_LICENSE("GPL"); 3263 MODULE_FIRMWARE("intel/ibt-11-5.sfi"); 3264 MODULE_FIRMWARE("intel/ibt-11-5.ddc"); 3265 MODULE_FIRMWARE("intel/ibt-12-16.sfi"); 3266 MODULE_FIRMWARE("intel/ibt-12-16.ddc"); 3267