1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Bluetooth support for Intel devices
6 * Copyright (C) 2015 Intel Corporation
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>
16 #include <net/bluetooth/bluetooth.h>
17 #include <net/bluetooth/hci_core.h>
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
29 #define BTINTEL_PPAG_NAME "PPAG"
32 DSM_SET_WDISABLE2_DELAY = 1,
33 DSM_SET_RESET_METHOD = 3,
36 /* structure to store the PPAG data read from ACPI table */
44 #define CMD_WRITE_BOOT_PARAMS 0xfc0e
45 struct cmd_write_boot_params {
53 const char *driver_name;
58 static const guid_t btintel_guid_dsm =
59 GUID_INIT(0xaa10f4e0, 0x81ac, 0x4233,
60 0xab, 0xf6, 0x3b, 0x2a, 0xc5, 0x0e, 0x28, 0xd9);
62 int btintel_check_bdaddr(struct hci_dev *hdev)
64 struct hci_rp_read_bd_addr *bda;
67 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
70 int err = PTR_ERR(skb);
71 bt_dev_err(hdev, "Reading Intel device address failed (%d)",
76 if (skb->len != sizeof(*bda)) {
77 bt_dev_err(hdev, "Intel device address length mismatch");
82 bda = (struct hci_rp_read_bd_addr *)skb->data;
84 /* For some Intel based controllers, the default Bluetooth device
85 * address 00:03:19:9E:8B:00 can be found. These controllers are
86 * fully operational, but have the danger of duplicate addresses
87 * and that in turn can cause problems with Bluetooth operation.
89 if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
90 bt_dev_err(hdev, "Found Intel default device address (%pMR)",
92 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
99 EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
101 int btintel_enter_mfg(struct hci_dev *hdev)
103 static const u8 param[] = { 0x01, 0x00 };
106 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
108 bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
116 EXPORT_SYMBOL_GPL(btintel_enter_mfg);
118 int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
120 u8 param[] = { 0x00, 0x00 };
123 /* The 2nd command parameter specifies the manufacturing exit method:
124 * 0x00: Just disable the manufacturing mode (0x00).
125 * 0x01: Disable manufacturing mode and reset with patches deactivated.
126 * 0x02: Disable manufacturing mode and reset with patches activated.
129 param[1] |= patched ? 0x02 : 0x01;
131 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
133 bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
141 EXPORT_SYMBOL_GPL(btintel_exit_mfg);
143 int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
148 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
151 bt_dev_err(hdev, "Changing Intel device address failed (%d)",
159 EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
161 static int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
163 u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
170 skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
173 bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
181 int btintel_set_diag(struct hci_dev *hdev, bool enable)
197 skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
202 bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
209 btintel_set_event_mask(hdev, enable);
212 EXPORT_SYMBOL_GPL(btintel_set_diag);
214 static int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
218 err = btintel_enter_mfg(hdev);
222 ret = btintel_set_diag(hdev, enable);
224 err = btintel_exit_mfg(hdev, false, false);
231 static int btintel_set_diag_combined(struct hci_dev *hdev, bool enable)
235 /* Legacy ROM device needs to be in the manufacturer mode to apply
238 * This flag is set after reading the Intel version.
240 if (btintel_test_flag(hdev, INTEL_ROM_LEGACY))
241 ret = btintel_set_diag_mfg(hdev, enable);
243 ret = btintel_set_diag(hdev, enable);
248 void btintel_hw_error(struct hci_dev *hdev, u8 code)
253 bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
255 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
257 bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
263 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
265 bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
270 if (skb->len != 13) {
271 bt_dev_err(hdev, "Exception info size mismatch");
276 bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
280 EXPORT_SYMBOL_GPL(btintel_hw_error);
282 int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
286 /* The hardware platform number has a fixed value of 0x37 and
287 * for now only accept this single value.
289 if (ver->hw_platform != 0x37) {
290 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
295 /* Check for supported iBT hardware variants of this firmware
298 * This check has been put in place to ensure correct forward
299 * compatibility options when newer hardware variants come along.
301 switch (ver->hw_variant) {
302 case 0x07: /* WP - Legacy ROM */
303 case 0x08: /* StP - Legacy ROM */
312 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
317 switch (ver->fw_variant) {
319 variant = "Legacy ROM 2.5";
322 variant = "Bootloader";
325 variant = "Legacy ROM 2.x";
328 variant = "Firmware";
331 bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
335 coredump_info.hw_variant = ver->hw_variant;
336 coredump_info.fw_build_num = ver->fw_build_num;
338 bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
339 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
340 ver->fw_build_num, ver->fw_build_ww,
341 2000 + ver->fw_build_yy);
345 EXPORT_SYMBOL_GPL(btintel_version_info);
347 static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
352 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
354 cmd_param[0] = fragment_type;
355 memcpy(cmd_param + 1, param, fragment_len);
357 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
358 cmd_param, HCI_INIT_TIMEOUT);
364 plen -= fragment_len;
365 param += fragment_len;
371 int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
373 const struct firmware *fw;
378 err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
380 bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
385 bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
389 /* DDC file contains one or more DDC structure which has
390 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
392 while (fw->size > fw_ptr - fw->data) {
393 u8 cmd_plen = fw_ptr[0] + sizeof(u8);
395 skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
398 bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
400 release_firmware(fw);
408 release_firmware(fw);
410 bt_dev_info(hdev, "Applying Intel DDC parameters completed");
414 EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
416 int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
420 err = btintel_enter_mfg(hdev);
424 ret = btintel_set_event_mask(hdev, debug);
426 err = btintel_exit_mfg(hdev, false, false);
432 EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
434 int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
438 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
440 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
445 if (!skb || skb->len != sizeof(*ver)) {
446 bt_dev_err(hdev, "Intel version event size mismatch");
451 memcpy(ver, skb->data, sizeof(*ver));
457 EXPORT_SYMBOL_GPL(btintel_read_version);
459 int btintel_version_info_tlv(struct hci_dev *hdev,
460 struct intel_version_tlv *version)
464 /* The hardware platform number has a fixed value of 0x37 and
465 * for now only accept this single value.
467 if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
468 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
469 INTEL_HW_PLATFORM(version->cnvi_bt));
473 /* Check for supported iBT hardware variants of this firmware
476 * This check has been put in place to ensure correct forward
477 * compatibility options when newer hardware variants come along.
479 switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
482 case 0x19: /* Slr-F */
484 case 0x1c: /* Gale Peak (GaP) */
485 case 0x1e: /* BlazarI (Bzr) */
488 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
489 INTEL_HW_VARIANT(version->cnvi_bt));
493 switch (version->img_type) {
494 case BTINTEL_IMG_BOOTLOADER:
495 variant = "Bootloader";
496 /* It is required that every single firmware fragment is acknowledged
497 * with a command complete event. If the boot parameters indicate
498 * that this bootloader does not send them, then abort the setup.
500 if (version->limited_cce != 0x00) {
501 bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
502 version->limited_cce);
506 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
507 if (version->sbe_type > 0x01) {
508 bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
513 bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
514 bt_dev_info(hdev, "Secure boot is %s",
515 version->secure_boot ? "enabled" : "disabled");
516 bt_dev_info(hdev, "OTP lock is %s",
517 version->otp_lock ? "enabled" : "disabled");
518 bt_dev_info(hdev, "API lock is %s",
519 version->api_lock ? "enabled" : "disabled");
520 bt_dev_info(hdev, "Debug lock is %s",
521 version->debug_lock ? "enabled" : "disabled");
522 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
523 version->min_fw_build_nn, version->min_fw_build_cw,
524 2000 + version->min_fw_build_yy);
526 case BTINTEL_IMG_IML:
527 variant = "Intermediate loader";
530 variant = "Firmware";
533 bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
537 coredump_info.hw_variant = INTEL_HW_VARIANT(version->cnvi_bt);
538 coredump_info.fw_build_num = version->build_num;
540 bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
541 2000 + (version->timestamp >> 8), version->timestamp & 0xff,
542 version->build_type, version->build_num);
543 if (version->img_type == BTINTEL_IMG_OP)
544 bt_dev_info(hdev, "Firmware SHA1: 0x%8.8x", version->git_sha1);
548 EXPORT_SYMBOL_GPL(btintel_version_info_tlv);
550 int btintel_parse_version_tlv(struct hci_dev *hdev,
551 struct intel_version_tlv *version,
554 /* Consume Command Complete Status field */
557 /* Event parameters contatin multiple TLVs. Read each of them
558 * and only keep the required data. Also, it use existing legacy
559 * version field like hw_platform, hw_variant, and fw_variant
560 * to keep the existing setup flow
563 struct intel_tlv *tlv;
565 /* Make sure skb has a minimum length of the header */
566 if (skb->len < sizeof(*tlv))
569 tlv = (struct intel_tlv *)skb->data;
571 /* Make sure skb has a enough data */
572 if (skb->len < tlv->len + sizeof(*tlv))
576 case INTEL_TLV_CNVI_TOP:
577 version->cnvi_top = get_unaligned_le32(tlv->val);
579 case INTEL_TLV_CNVR_TOP:
580 version->cnvr_top = get_unaligned_le32(tlv->val);
582 case INTEL_TLV_CNVI_BT:
583 version->cnvi_bt = get_unaligned_le32(tlv->val);
585 case INTEL_TLV_CNVR_BT:
586 version->cnvr_bt = get_unaligned_le32(tlv->val);
588 case INTEL_TLV_DEV_REV_ID:
589 version->dev_rev_id = get_unaligned_le16(tlv->val);
591 case INTEL_TLV_IMAGE_TYPE:
592 version->img_type = tlv->val[0];
594 case INTEL_TLV_TIME_STAMP:
595 /* If image type is Operational firmware (0x03), then
596 * running FW Calendar Week and Year information can
597 * be extracted from Timestamp information
599 version->min_fw_build_cw = tlv->val[0];
600 version->min_fw_build_yy = tlv->val[1];
601 version->timestamp = get_unaligned_le16(tlv->val);
603 case INTEL_TLV_BUILD_TYPE:
604 version->build_type = tlv->val[0];
606 case INTEL_TLV_BUILD_NUM:
607 /* If image type is Operational firmware (0x03), then
608 * running FW build number can be extracted from the
611 version->min_fw_build_nn = tlv->val[0];
612 version->build_num = get_unaligned_le32(tlv->val);
614 case INTEL_TLV_SECURE_BOOT:
615 version->secure_boot = tlv->val[0];
617 case INTEL_TLV_OTP_LOCK:
618 version->otp_lock = tlv->val[0];
620 case INTEL_TLV_API_LOCK:
621 version->api_lock = tlv->val[0];
623 case INTEL_TLV_DEBUG_LOCK:
624 version->debug_lock = tlv->val[0];
626 case INTEL_TLV_MIN_FW:
627 version->min_fw_build_nn = tlv->val[0];
628 version->min_fw_build_cw = tlv->val[1];
629 version->min_fw_build_yy = tlv->val[2];
631 case INTEL_TLV_LIMITED_CCE:
632 version->limited_cce = tlv->val[0];
634 case INTEL_TLV_SBE_TYPE:
635 version->sbe_type = tlv->val[0];
637 case INTEL_TLV_OTP_BDADDR:
638 memcpy(&version->otp_bd_addr, tlv->val,
641 case INTEL_TLV_GIT_SHA1:
642 version->git_sha1 = get_unaligned_le32(tlv->val);
645 /* Ignore rest of information */
648 /* consume the current tlv and move to next*/
649 skb_pull(skb, tlv->len + sizeof(*tlv));
654 EXPORT_SYMBOL_GPL(btintel_parse_version_tlv);
656 static int btintel_read_version_tlv(struct hci_dev *hdev,
657 struct intel_version_tlv *version)
660 const u8 param[1] = { 0xFF };
665 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
667 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
673 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
679 btintel_parse_version_tlv(hdev, version, skb);
685 /* ------- REGMAP IBT SUPPORT ------- */
687 #define IBT_REG_MODE_8BIT 0x00
688 #define IBT_REG_MODE_16BIT 0x01
689 #define IBT_REG_MODE_32BIT 0x02
691 struct regmap_ibt_context {
692 struct hci_dev *hdev;
697 struct ibt_cp_reg_access {
704 struct ibt_rp_reg_access {
710 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
711 void *val, size_t val_size)
713 struct regmap_ibt_context *ctx = context;
714 struct ibt_cp_reg_access cp;
715 struct ibt_rp_reg_access *rp;
719 if (reg_size != sizeof(__le32))
724 cp.mode = IBT_REG_MODE_8BIT;
727 cp.mode = IBT_REG_MODE_16BIT;
730 cp.mode = IBT_REG_MODE_32BIT;
736 /* regmap provides a little-endian formatted addr */
737 cp.addr = *(__le32 *)addr;
740 bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
742 skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
746 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
747 le32_to_cpu(cp.addr), err);
751 if (skb->len != sizeof(*rp) + val_size) {
752 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
753 le32_to_cpu(cp.addr));
758 rp = (struct ibt_rp_reg_access *)skb->data;
760 if (rp->addr != cp.addr) {
761 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
762 le32_to_cpu(rp->addr));
767 memcpy(val, rp->data, val_size);
774 static int regmap_ibt_gather_write(void *context,
775 const void *addr, size_t reg_size,
776 const void *val, size_t val_size)
778 struct regmap_ibt_context *ctx = context;
779 struct ibt_cp_reg_access *cp;
781 int plen = sizeof(*cp) + val_size;
785 if (reg_size != sizeof(__le32))
790 mode = IBT_REG_MODE_8BIT;
793 mode = IBT_REG_MODE_16BIT;
796 mode = IBT_REG_MODE_32BIT;
802 cp = kmalloc(plen, GFP_KERNEL);
806 /* regmap provides a little-endian formatted addr/value */
807 cp->addr = *(__le32 *)addr;
810 memcpy(&cp->data, val, val_size);
812 bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
814 skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
817 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
818 le32_to_cpu(cp->addr), err);
828 static int regmap_ibt_write(void *context, const void *data, size_t count)
830 /* data contains register+value, since we only support 32bit addr,
831 * minimum data size is 4 bytes.
833 if (WARN_ONCE(count < 4, "Invalid register access"))
836 return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
839 static void regmap_ibt_free_context(void *context)
844 static const struct regmap_bus regmap_ibt = {
845 .read = regmap_ibt_read,
846 .write = regmap_ibt_write,
847 .gather_write = regmap_ibt_gather_write,
848 .free_context = regmap_ibt_free_context,
849 .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
850 .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
853 /* Config is the same for all register regions */
854 static const struct regmap_config regmap_ibt_cfg = {
855 .name = "btintel_regmap",
860 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
863 struct regmap_ibt_context *ctx;
865 bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
868 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
870 return ERR_PTR(-ENOMEM);
872 ctx->op_read = opcode_read;
873 ctx->op_write = opcode_write;
876 return regmap_init(&hdev->dev, ®map_ibt, ctx, ®map_ibt_cfg);
878 EXPORT_SYMBOL_GPL(btintel_regmap_init);
880 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
882 struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
885 params.boot_param = cpu_to_le32(boot_param);
887 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), ¶ms,
890 bt_dev_err(hdev, "Failed to send Intel Reset command");
898 EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
900 int btintel_read_boot_params(struct hci_dev *hdev,
901 struct intel_boot_params *params)
905 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
907 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
912 if (skb->len != sizeof(*params)) {
913 bt_dev_err(hdev, "Intel boot parameters size mismatch");
918 memcpy(params, skb->data, sizeof(*params));
922 if (params->status) {
923 bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
925 return -bt_to_errno(params->status);
928 bt_dev_info(hdev, "Device revision is %u",
929 le16_to_cpu(params->dev_revid));
931 bt_dev_info(hdev, "Secure boot is %s",
932 params->secure_boot ? "enabled" : "disabled");
934 bt_dev_info(hdev, "OTP lock is %s",
935 params->otp_lock ? "enabled" : "disabled");
937 bt_dev_info(hdev, "API lock is %s",
938 params->api_lock ? "enabled" : "disabled");
940 bt_dev_info(hdev, "Debug lock is %s",
941 params->debug_lock ? "enabled" : "disabled");
943 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
944 params->min_fw_build_nn, params->min_fw_build_cw,
945 2000 + params->min_fw_build_yy);
949 EXPORT_SYMBOL_GPL(btintel_read_boot_params);
951 static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
952 const struct firmware *fw)
956 /* Start the firmware download transaction with the Init fragment
957 * represented by the 128 bytes of CSS header.
959 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
961 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
965 /* Send the 256 bytes of public key information from the firmware
966 * as the PKey fragment.
968 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
970 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
974 /* Send the 256 bytes of signature information from the firmware
975 * as the Sign fragment.
977 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
979 bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
987 static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
988 const struct firmware *fw)
992 /* Start the firmware download transaction with the Init fragment
993 * represented by the 128 bytes of CSS header.
995 err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
997 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
1001 /* Send the 96 bytes of public key information from the firmware
1002 * as the PKey fragment.
1004 err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
1006 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
1010 /* Send the 96 bytes of signature information from the firmware
1011 * as the Sign fragment
1013 err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
1015 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
1022 static int btintel_download_firmware_payload(struct hci_dev *hdev,
1023 const struct firmware *fw,
1030 fw_ptr = fw->data + offset;
1034 while (fw_ptr - fw->data < fw->size) {
1035 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
1037 frag_len += sizeof(*cmd) + cmd->plen;
1039 /* The parameter length of the secure send command requires
1040 * a 4 byte alignment. It happens so that the firmware file
1041 * contains proper Intel_NOP commands to align the fragments
1044 * Send set of commands with 4 byte alignment from the
1045 * firmware data buffer as a single Data fragement.
1047 if (!(frag_len % 4)) {
1048 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1051 "Failed to send firmware data (%d)",
1065 static bool btintel_firmware_version(struct hci_dev *hdev,
1066 u8 num, u8 ww, u8 yy,
1067 const struct firmware *fw,
1074 while (fw_ptr - fw->data < fw->size) {
1075 struct hci_command_hdr *cmd = (void *)(fw_ptr);
1077 /* Each SKU has a different reset parameter to use in the
1078 * HCI_Intel_Reset command and it is embedded in the firmware
1079 * data. So, instead of using static value per SKU, check
1080 * the firmware data and save it for later use.
1082 if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1083 struct cmd_write_boot_params *params;
1085 params = (void *)(fw_ptr + sizeof(*cmd));
1087 *boot_addr = le32_to_cpu(params->boot_addr);
1089 bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
1091 bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1092 params->fw_build_num, params->fw_build_ww,
1093 params->fw_build_yy);
1095 return (num == params->fw_build_num &&
1096 ww == params->fw_build_ww &&
1097 yy == params->fw_build_yy);
1100 fw_ptr += sizeof(*cmd) + cmd->plen;
1106 int btintel_download_firmware(struct hci_dev *hdev,
1107 struct intel_version *ver,
1108 const struct firmware *fw,
1113 /* SfP and WsP don't seem to update the firmware version on file
1114 * so version checking is currently not possible.
1116 switch (ver->hw_variant) {
1117 case 0x0b: /* SfP */
1118 case 0x0c: /* WsP */
1119 /* Skip version checking */
1123 /* Skip download if firmware has the same version */
1124 if (btintel_firmware_version(hdev, ver->fw_build_num,
1125 ver->fw_build_ww, ver->fw_build_yy,
1127 bt_dev_info(hdev, "Firmware already loaded");
1128 /* Return -EALREADY to indicate that the firmware has
1129 * already been loaded.
1135 /* The firmware variant determines if the device is in bootloader
1136 * mode or is running operational firmware. The value 0x06 identifies
1137 * the bootloader and the value 0x23 identifies the operational
1140 * If the firmware version has changed that means it needs to be reset
1141 * to bootloader when operational so the new firmware can be loaded.
1143 if (ver->fw_variant == 0x23)
1146 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1150 return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1152 EXPORT_SYMBOL_GPL(btintel_download_firmware);
1154 static int btintel_download_fw_tlv(struct hci_dev *hdev,
1155 struct intel_version_tlv *ver,
1156 const struct firmware *fw, u32 *boot_param,
1157 u8 hw_variant, u8 sbe_type)
1162 /* Skip download if firmware has the same version */
1163 if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1164 ver->min_fw_build_cw,
1165 ver->min_fw_build_yy,
1167 bt_dev_info(hdev, "Firmware already loaded");
1168 /* Return -EALREADY to indicate that firmware has
1169 * already been loaded.
1174 /* The firmware variant determines if the device is in bootloader
1175 * mode or is running operational firmware. The value 0x01 identifies
1176 * the bootloader and the value 0x03 identifies the operational
1179 * If the firmware version has changed that means it needs to be reset
1180 * to bootloader when operational so the new firmware can be loaded.
1182 if (ver->img_type == BTINTEL_IMG_OP)
1185 /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1186 * only RSA secure boot engine. Hence, the corresponding sfi file will
1187 * have RSA header of 644 bytes followed by Command Buffer.
1189 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1190 * secure boot engine. As a result, the corresponding sfi file will
1191 * have RSA header of 644, ECDSA header of 320 bytes followed by
1194 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1195 * version: RSA(0x00010000) , ECDSA (0x00020000)
1197 css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1198 if (css_header_ver != 0x00010000) {
1199 bt_dev_err(hdev, "Invalid CSS Header version");
1203 if (hw_variant <= 0x14) {
1204 if (sbe_type != 0x00) {
1205 bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1210 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1214 err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1217 } else if (hw_variant >= 0x17) {
1218 /* Check if CSS header for ECDSA follows the RSA header */
1219 if (fw->data[ECDSA_OFFSET] != 0x06)
1222 /* Check if the CSS Header version is ECDSA(0x00020000) */
1223 css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1224 if (css_header_ver != 0x00020000) {
1225 bt_dev_err(hdev, "Invalid CSS Header version");
1229 if (sbe_type == 0x00) {
1230 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1234 err = btintel_download_firmware_payload(hdev, fw,
1235 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1238 } else if (sbe_type == 0x01) {
1239 err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1243 err = btintel_download_firmware_payload(hdev, fw,
1244 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1252 static void btintel_reset_to_bootloader(struct hci_dev *hdev)
1254 struct intel_reset params;
1255 struct sk_buff *skb;
1257 /* Send Intel Reset command. This will result in
1258 * re-enumeration of BT controller.
1260 * Intel Reset parameter description:
1261 * reset_type : 0x00 (Soft reset),
1263 * patch_enable : 0x00 (Do not enable),
1265 * ddc_reload : 0x00 (Do not reload),
1267 * boot_option: 0x00 (Current image),
1268 * 0x01 (Specified boot address)
1269 * boot_param: Boot address
1272 params.reset_type = 0x01;
1273 params.patch_enable = 0x01;
1274 params.ddc_reload = 0x01;
1275 params.boot_option = 0x00;
1276 params.boot_param = cpu_to_le32(0x00000000);
1278 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1279 ¶ms, HCI_INIT_TIMEOUT);
1281 bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1285 bt_dev_info(hdev, "Intel reset sent to retry FW download");
1288 /* Current Intel BT controllers(ThP/JfP) hold the USB reset
1289 * lines for 2ms when it receives Intel Reset in bootloader mode.
1290 * Whereas, the upcoming Intel BT controllers will hold USB reset
1291 * for 150ms. To keep the delay generic, 150ms is chosen here.
1296 static int btintel_read_debug_features(struct hci_dev *hdev,
1297 struct intel_debug_features *features)
1299 struct sk_buff *skb;
1302 /* Intel controller supports two pages, each page is of 128-bit
1303 * feature bit mask. And each bit defines specific feature support
1305 skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1308 bt_dev_err(hdev, "Reading supported features failed (%ld)",
1310 return PTR_ERR(skb);
1313 if (skb->len != (sizeof(features->page1) + 3)) {
1314 bt_dev_err(hdev, "Supported features event size mismatch");
1319 memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1321 /* Read the supported features page2 if required in future.
1327 static acpi_status btintel_ppag_callback(acpi_handle handle, u32 lvl, void *data,
1332 struct btintel_ppag *ppag = data;
1333 union acpi_object *p, *elements;
1334 struct acpi_buffer string = {ACPI_ALLOCATE_BUFFER, NULL};
1335 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1336 struct hci_dev *hdev = ppag->hdev;
1338 status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1339 if (ACPI_FAILURE(status)) {
1340 bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
1344 len = strlen(string.pointer);
1345 if (len < strlen(BTINTEL_PPAG_NAME)) {
1346 kfree(string.pointer);
1350 if (strncmp((char *)string.pointer + len - 4, BTINTEL_PPAG_NAME, 4)) {
1351 kfree(string.pointer);
1354 kfree(string.pointer);
1356 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
1357 if (ACPI_FAILURE(status)) {
1358 ppag->status = status;
1359 bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
1364 ppag = (struct btintel_ppag *)data;
1366 if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 2) {
1367 kfree(buffer.pointer);
1368 bt_dev_warn(hdev, "PPAG-BT: Invalid object type: %d or package count: %d",
1369 p->type, p->package.count);
1370 ppag->status = AE_ERROR;
1374 elements = p->package.elements;
1376 /* PPAG table is located at element[1] */
1379 ppag->domain = (u32)p->package.elements[0].integer.value;
1380 ppag->mode = (u32)p->package.elements[1].integer.value;
1381 ppag->status = AE_OK;
1382 kfree(buffer.pointer);
1383 return AE_CTRL_TERMINATE;
1386 static int btintel_set_debug_features(struct hci_dev *hdev,
1387 const struct intel_debug_features *features)
1389 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
1391 u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
1392 u8 trace_enable = 0x02;
1393 struct sk_buff *skb;
1396 bt_dev_warn(hdev, "Debug features not read");
1400 if (!(features->page1[0] & 0x3f)) {
1401 bt_dev_info(hdev, "Telemetry exception format not supported");
1405 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1407 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1409 return PTR_ERR(skb);
1413 skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
1415 bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
1417 return PTR_ERR(skb);
1421 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1423 bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
1425 return PTR_ERR(skb);
1429 bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
1430 trace_enable, mask[3]);
1435 static int btintel_reset_debug_features(struct hci_dev *hdev,
1436 const struct intel_debug_features *features)
1438 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
1440 u8 trace_enable = 0x00;
1441 struct sk_buff *skb;
1444 bt_dev_warn(hdev, "Debug features not read");
1448 if (!(features->page1[0] & 0x3f)) {
1449 bt_dev_info(hdev, "Telemetry exception format not supported");
1453 /* Should stop the trace before writing ddc event mask. */
1454 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1456 bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
1458 return PTR_ERR(skb);
1462 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1464 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1466 return PTR_ERR(skb);
1470 bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
1471 trace_enable, mask[3]);
1476 int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
1478 struct intel_debug_features features;
1481 bt_dev_dbg(hdev, "enable %d", enable);
1483 /* Read the Intel supported features and if new exception formats
1484 * supported, need to load the additional DDC config to enable.
1486 err = btintel_read_debug_features(hdev, &features);
1490 /* Set or reset the debug features. */
1492 err = btintel_set_debug_features(hdev, &features);
1494 err = btintel_reset_debug_features(hdev, &features);
1498 EXPORT_SYMBOL_GPL(btintel_set_quality_report);
1500 static void btintel_coredump(struct hci_dev *hdev)
1502 struct sk_buff *skb;
1504 skb = __hci_cmd_sync(hdev, 0xfc4e, 0, NULL, HCI_CMD_TIMEOUT);
1506 bt_dev_err(hdev, "Coredump failed (%ld)", PTR_ERR(skb));
1513 static void btintel_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
1517 snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
1518 coredump_info.hw_variant);
1519 skb_put_data(skb, buf, strlen(buf));
1521 snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
1522 coredump_info.fw_build_num);
1523 skb_put_data(skb, buf, strlen(buf));
1525 snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info.driver_name);
1526 skb_put_data(skb, buf, strlen(buf));
1528 snprintf(buf, sizeof(buf), "Vendor: Intel\n");
1529 skb_put_data(skb, buf, strlen(buf));
1532 static int btintel_register_devcoredump_support(struct hci_dev *hdev)
1534 struct intel_debug_features features;
1537 err = btintel_read_debug_features(hdev, &features);
1539 bt_dev_info(hdev, "Error reading debug features");
1543 if (!(features.page1[0] & 0x3f)) {
1544 bt_dev_dbg(hdev, "Telemetry exception format not supported");
1548 hci_devcd_register(hdev, btintel_coredump, btintel_dmp_hdr, NULL);
1553 static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
1554 struct intel_version *ver)
1556 const struct firmware *fw;
1560 snprintf(fwname, sizeof(fwname),
1561 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1562 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1563 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1564 ver->fw_build_ww, ver->fw_build_yy);
1566 ret = request_firmware(&fw, fwname, &hdev->dev);
1568 if (ret == -EINVAL) {
1569 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1574 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1577 /* If the correct firmware patch file is not found, use the
1578 * default firmware patch file instead
1580 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1581 ver->hw_platform, ver->hw_variant);
1582 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1583 bt_dev_err(hdev, "failed to open default fw file: %s",
1589 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1594 static int btintel_legacy_rom_patching(struct hci_dev *hdev,
1595 const struct firmware *fw,
1596 const u8 **fw_ptr, int *disable_patch)
1598 struct sk_buff *skb;
1599 struct hci_command_hdr *cmd;
1600 const u8 *cmd_param;
1601 struct hci_event_hdr *evt = NULL;
1602 const u8 *evt_param = NULL;
1603 int remain = fw->size - (*fw_ptr - fw->data);
1605 /* The first byte indicates the types of the patch command or event.
1606 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1607 * in the current firmware buffer doesn't start with 0x01 or
1608 * the size of remain buffer is smaller than HCI command header,
1609 * the firmware file is corrupted and it should stop the patching
1612 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1613 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1619 cmd = (struct hci_command_hdr *)(*fw_ptr);
1620 *fw_ptr += sizeof(*cmd);
1621 remain -= sizeof(*cmd);
1623 /* Ensure that the remain firmware data is long enough than the length
1624 * of command parameter. If not, the firmware file is corrupted.
1626 if (remain < cmd->plen) {
1627 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1631 /* If there is a command that loads a patch in the firmware
1632 * file, then enable the patch upon success, otherwise just
1633 * disable the manufacturer mode, for example patch activation
1634 * is not required when the default firmware patch file is used
1635 * because there are no patch data to load.
1637 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1640 cmd_param = *fw_ptr;
1641 *fw_ptr += cmd->plen;
1642 remain -= cmd->plen;
1644 /* This reads the expected events when the above command is sent to the
1645 * device. Some vendor commands expects more than one events, for
1646 * example command status event followed by vendor specific event.
1647 * For this case, it only keeps the last expected event. so the command
1648 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1649 * last expected event.
1651 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1655 evt = (struct hci_event_hdr *)(*fw_ptr);
1656 *fw_ptr += sizeof(*evt);
1657 remain -= sizeof(*evt);
1659 if (remain < evt->plen) {
1660 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1664 evt_param = *fw_ptr;
1665 *fw_ptr += evt->plen;
1666 remain -= evt->plen;
1669 /* Every HCI commands in the firmware file has its correspond event.
1670 * If event is not found or remain is smaller than zero, the firmware
1671 * file is corrupted.
1673 if (!evt || !evt_param || remain < 0) {
1674 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1678 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1679 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1681 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1682 cmd->opcode, PTR_ERR(skb));
1683 return PTR_ERR(skb);
1686 /* It ensures that the returned event matches the event data read from
1687 * the firmware file. At fist, it checks the length and then
1688 * the contents of the event.
1690 if (skb->len != evt->plen) {
1691 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1692 le16_to_cpu(cmd->opcode));
1697 if (memcmp(skb->data, evt_param, evt->plen)) {
1698 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1699 le16_to_cpu(cmd->opcode));
1708 static int btintel_legacy_rom_setup(struct hci_dev *hdev,
1709 struct intel_version *ver)
1711 const struct firmware *fw;
1713 int disable_patch, err;
1714 struct intel_version new_ver;
1716 BT_DBG("%s", hdev->name);
1718 /* fw_patch_num indicates the version of patch the device currently
1719 * have. If there is no patch data in the device, it is always 0x00.
1720 * So, if it is other than 0x00, no need to patch the device again.
1722 if (ver->fw_patch_num) {
1724 "Intel device is already patched. patch num: %02x",
1729 /* Opens the firmware patch file based on the firmware version read
1730 * from the controller. If it fails to open the matching firmware
1731 * patch file, it tries to open the default firmware patch file.
1732 * If no patch file is found, allow the device to operate without
1735 fw = btintel_legacy_rom_get_fw(hdev, ver);
1740 /* Enable the manufacturer mode of the controller.
1741 * Only while this mode is enabled, the driver can download the
1742 * firmware patch data and configuration parameters.
1744 err = btintel_enter_mfg(hdev);
1746 release_firmware(fw);
1752 /* The firmware data file consists of list of Intel specific HCI
1753 * commands and its expected events. The first byte indicates the
1754 * type of the message, either HCI command or HCI event.
1756 * It reads the command and its expected event from the firmware file,
1757 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1758 * the returned event is compared with the event read from the firmware
1759 * file and it will continue until all the messages are downloaded to
1762 * Once the firmware patching is completed successfully,
1763 * the manufacturer mode is disabled with reset and activating the
1766 * If the firmware patching fails, the manufacturer mode is
1767 * disabled with reset and deactivating the patch.
1769 * If the default patch file is used, no reset is done when disabling
1772 while (fw->size > fw_ptr - fw->data) {
1775 ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
1778 goto exit_mfg_deactivate;
1781 release_firmware(fw);
1784 goto exit_mfg_disable;
1786 /* Patching completed successfully and disable the manufacturer mode
1787 * with reset and activate the downloaded firmware patches.
1789 err = btintel_exit_mfg(hdev, true, true);
1793 /* Need build number for downloaded fw patches in
1794 * every power-on boot
1796 err = btintel_read_version(hdev, &new_ver);
1800 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
1801 new_ver.fw_patch_num);
1806 /* Disable the manufacturer mode without reset */
1807 err = btintel_exit_mfg(hdev, false, false);
1811 bt_dev_info(hdev, "Intel firmware patch completed");
1815 exit_mfg_deactivate:
1816 release_firmware(fw);
1818 /* Patching failed. Disable the manufacturer mode with reset and
1819 * deactivate the downloaded firmware patches.
1821 err = btintel_exit_mfg(hdev, true, false);
1825 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1828 /* Set the event mask for Intel specific vendor events. This enables
1829 * a few extra events that are useful during general operation.
1831 btintel_set_event_mask_mfg(hdev, false);
1833 btintel_check_bdaddr(hdev);
1838 static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1840 ktime_t delta, rettime;
1841 unsigned long long duration;
1844 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1846 bt_dev_info(hdev, "Waiting for firmware download to complete");
1848 err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
1850 msecs_to_jiffies(msec));
1851 if (err == -EINTR) {
1852 bt_dev_err(hdev, "Firmware loading interrupted");
1857 bt_dev_err(hdev, "Firmware loading timeout");
1861 if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
1862 bt_dev_err(hdev, "Firmware loading failed");
1866 rettime = ktime_get();
1867 delta = ktime_sub(rettime, calltime);
1868 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1870 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
1875 static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1877 ktime_t delta, rettime;
1878 unsigned long long duration;
1881 bt_dev_info(hdev, "Waiting for device to boot");
1883 err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
1885 msecs_to_jiffies(msec));
1886 if (err == -EINTR) {
1887 bt_dev_err(hdev, "Device boot interrupted");
1892 bt_dev_err(hdev, "Device boot timeout");
1896 rettime = ktime_get();
1897 delta = ktime_sub(rettime, calltime);
1898 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1900 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
1905 static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
1910 calltime = ktime_get();
1912 btintel_set_flag(hdev, INTEL_BOOTING);
1914 err = btintel_send_intel_reset(hdev, boot_addr);
1916 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
1917 btintel_reset_to_bootloader(hdev);
1921 /* The bootloader will not indicate when the device is ready. This
1922 * is done by the operational firmware sending bootup notification.
1924 * Booting into operational firmware should not take longer than
1925 * 1 second. However if that happens, then just fail the setup
1926 * since something went wrong.
1928 err = btintel_boot_wait(hdev, calltime, 1000);
1929 if (err == -ETIMEDOUT)
1930 btintel_reset_to_bootloader(hdev);
1935 static int btintel_get_fw_name(struct intel_version *ver,
1936 struct intel_boot_params *params,
1937 char *fw_name, size_t len,
1940 switch (ver->hw_variant) {
1941 case 0x0b: /* SfP */
1942 case 0x0c: /* WsP */
1943 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
1945 le16_to_cpu(params->dev_revid),
1948 case 0x11: /* JfP */
1949 case 0x12: /* ThP */
1950 case 0x13: /* HrP */
1951 case 0x14: /* CcP */
1952 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
1965 static int btintel_download_fw(struct hci_dev *hdev,
1966 struct intel_version *ver,
1967 struct intel_boot_params *params,
1970 const struct firmware *fw;
1975 if (!ver || !params)
1978 /* The firmware variant determines if the device is in bootloader
1979 * mode or is running operational firmware. The value 0x06 identifies
1980 * the bootloader and the value 0x23 identifies the operational
1983 * When the operational firmware is already present, then only
1984 * the check for valid Bluetooth device address is needed. This
1985 * determines if the device will be added as configured or
1986 * unconfigured controller.
1988 * It is not possible to use the Secure Boot Parameters in this
1989 * case since that command is only available in bootloader mode.
1991 if (ver->fw_variant == 0x23) {
1992 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
1993 btintel_check_bdaddr(hdev);
1995 /* SfP and WsP don't seem to update the firmware version on file
1996 * so version checking is currently possible.
1998 switch (ver->hw_variant) {
1999 case 0x0b: /* SfP */
2000 case 0x0c: /* WsP */
2004 /* Proceed to download to check if the version matches */
2008 /* Read the secure boot parameters to identify the operating
2009 * details of the bootloader.
2011 err = btintel_read_boot_params(hdev, params);
2015 /* It is required that every single firmware fragment is acknowledged
2016 * with a command complete event. If the boot parameters indicate
2017 * that this bootloader does not send them, then abort the setup.
2019 if (params->limited_cce != 0x00) {
2020 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2021 params->limited_cce);
2025 /* If the OTP has no valid Bluetooth device address, then there will
2026 * also be no valid address for the operational firmware.
2028 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2029 bt_dev_info(hdev, "No device address configured");
2030 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2034 /* With this Intel bootloader only the hardware variant and device
2035 * revision information are used to select the right firmware for SfP
2038 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2040 * Currently the supported hardware variants are:
2041 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2042 * 12 (0x0c) for iBT3.5 (WsP)
2044 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2045 * variant, HW revision and FW revision, as these are dependent on CNVi
2046 * and RF Combination.
2048 * 17 (0x11) for iBT3.5 (JfP)
2049 * 18 (0x12) for iBT3.5 (ThP)
2051 * The firmware file name for these will be
2052 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2055 err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
2057 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2058 /* Firmware has already been loaded */
2059 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2063 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2067 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2069 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2070 /* Firmware has already been loaded */
2071 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2075 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2080 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2082 if (fw->size < 644) {
2083 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2089 calltime = ktime_get();
2091 btintel_set_flag(hdev, INTEL_DOWNLOADING);
2093 /* Start firmware downloading and get boot parameter */
2094 err = btintel_download_firmware(hdev, ver, fw, boot_param);
2096 if (err == -EALREADY) {
2097 /* Firmware has already been loaded */
2098 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2103 /* When FW download fails, send Intel Reset to retry
2106 btintel_reset_to_bootloader(hdev);
2110 /* Before switching the device into operational mode and with that
2111 * booting the loaded firmware, wait for the bootloader notification
2112 * that all fragments have been successfully received.
2114 * When the event processing receives the notification, then the
2115 * INTEL_DOWNLOADING flag will be cleared.
2117 * The firmware loading should not take longer than 5 seconds
2118 * and thus just timeout if that happens and fail the setup
2121 err = btintel_download_wait(hdev, calltime, 5000);
2122 if (err == -ETIMEDOUT)
2123 btintel_reset_to_bootloader(hdev);
2126 release_firmware(fw);
2130 static int btintel_bootloader_setup(struct hci_dev *hdev,
2131 struct intel_version *ver)
2133 struct intel_version new_ver;
2134 struct intel_boot_params params;
2139 BT_DBG("%s", hdev->name);
2141 /* Set the default boot parameter to 0x0 and it is updated to
2142 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2143 * command while downloading the firmware.
2145 boot_param = 0x00000000;
2147 btintel_set_flag(hdev, INTEL_BOOTLOADER);
2149 err = btintel_download_fw(hdev, ver, ¶ms, &boot_param);
2153 /* controller is already having an operational firmware */
2154 if (ver->fw_variant == 0x23)
2157 err = btintel_boot(hdev, boot_param);
2161 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2163 err = btintel_get_fw_name(ver, ¶ms, ddcname,
2164 sizeof(ddcname), "ddc");
2167 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2169 /* Once the device is running in operational mode, it needs to
2170 * apply the device configuration (DDC) parameters.
2172 * The device can work without DDC parameters, so even if it
2173 * fails to load the file, no need to fail the setup.
2175 btintel_load_ddc_config(hdev, ddcname);
2178 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2180 /* Read the Intel version information after loading the FW */
2181 err = btintel_read_version(hdev, &new_ver);
2185 btintel_version_info(hdev, &new_ver);
2188 /* Set the event mask for Intel specific vendor events. This enables
2189 * a few extra events that are useful during general operation. It
2190 * does not enable any debugging related events.
2192 * The device will function correctly without these events enabled
2193 * and thus no need to fail the setup.
2195 btintel_set_event_mask(hdev, false);
2200 static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
2201 char *fw_name, size_t len,
2205 /* The firmware file name for new generation controllers will be
2206 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2208 switch (ver->cnvi_top & 0xfff) {
2209 /* Only Blazar product supports downloading of intermediate loader
2212 case BTINTEL_CNVI_BLAZARI:
2213 if (ver->img_type == BTINTEL_IMG_BOOTLOADER)
2214 format = "intel/ibt-%04x-%04x-iml.%s";
2216 format = "intel/ibt-%04x-%04x.%s";
2219 format = "intel/ibt-%04x-%04x.%s";
2223 snprintf(fw_name, len, format,
2224 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2225 INTEL_CNVX_TOP_STEP(ver->cnvi_top)),
2226 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2227 INTEL_CNVX_TOP_STEP(ver->cnvr_top)),
2231 static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
2232 struct intel_version_tlv *ver,
2235 const struct firmware *fw;
2240 if (!ver || !boot_param)
2243 /* The firmware variant determines if the device is in bootloader
2244 * mode or is running operational firmware. The value 0x03 identifies
2245 * the bootloader and the value 0x23 identifies the operational
2248 * When the operational firmware is already present, then only
2249 * the check for valid Bluetooth device address is needed. This
2250 * determines if the device will be added as configured or
2251 * unconfigured controller.
2253 * It is not possible to use the Secure Boot Parameters in this
2254 * case since that command is only available in bootloader mode.
2256 if (ver->img_type == BTINTEL_IMG_OP) {
2257 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2258 btintel_check_bdaddr(hdev);
2261 * Check for valid bd address in boot loader mode. Device
2262 * will be marked as unconfigured if empty bd address is
2265 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2266 bt_dev_info(hdev, "No device address configured");
2267 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2271 btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2272 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2274 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2275 /* Firmware has already been loaded */
2276 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2280 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2286 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2288 if (fw->size < 644) {
2289 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2295 calltime = ktime_get();
2297 btintel_set_flag(hdev, INTEL_DOWNLOADING);
2299 /* Start firmware downloading and get boot parameter */
2300 err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
2301 INTEL_HW_VARIANT(ver->cnvi_bt),
2304 if (err == -EALREADY) {
2305 /* Firmware has already been loaded */
2306 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2311 /* When FW download fails, send Intel Reset to retry
2314 btintel_reset_to_bootloader(hdev);
2318 /* Before switching the device into operational mode and with that
2319 * booting the loaded firmware, wait for the bootloader notification
2320 * that all fragments have been successfully received.
2322 * When the event processing receives the notification, then the
2323 * BTUSB_DOWNLOADING flag will be cleared.
2325 * The firmware loading should not take longer than 5 seconds
2326 * and thus just timeout if that happens and fail the setup
2329 err = btintel_download_wait(hdev, calltime, 5000);
2330 if (err == -ETIMEDOUT)
2331 btintel_reset_to_bootloader(hdev);
2334 release_firmware(fw);
2338 static int btintel_get_codec_config_data(struct hci_dev *hdev,
2339 __u8 link, struct bt_codec *codec,
2340 __u8 *ven_len, __u8 **ven_data)
2344 if (!ven_data || !ven_len)
2350 if (link != ESCO_LINK) {
2351 bt_dev_err(hdev, "Invalid link type(%u)", link);
2355 *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
2361 /* supports only CVSD and mSBC offload codecs */
2362 switch (codec->id) {
2371 bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
2374 /* codec and its capabilities are pre-defined to ids
2375 * preset id = 0x00 represents CVSD codec with sampling rate 8K
2376 * preset id = 0x01 represents mSBC codec with sampling rate 16K
2378 *ven_len = sizeof(__u8);
2387 static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
2389 /* Intel uses 1 as data path id for all the usecases */
2394 static int btintel_configure_offload(struct hci_dev *hdev)
2396 struct sk_buff *skb;
2398 struct intel_offload_use_cases *use_cases;
2400 skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
2402 bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
2404 return PTR_ERR(skb);
2407 if (skb->len < sizeof(*use_cases)) {
2412 use_cases = (void *)skb->data;
2414 if (use_cases->status) {
2415 err = -bt_to_errno(skb->data[0]);
2419 if (use_cases->preset[0] & 0x03) {
2420 hdev->get_data_path_id = btintel_get_data_path_id;
2421 hdev->get_codec_config_data = btintel_get_codec_config_data;
2428 static void btintel_set_ppag(struct hci_dev *hdev, struct intel_version_tlv *ver)
2430 struct btintel_ppag ppag;
2431 struct sk_buff *skb;
2432 struct hci_ppag_enable_cmd ppag_cmd;
2435 /* PPAG is not supported if CRF is HrP2, Jfp2, JfP1 */
2436 switch (ver->cnvr_top & 0xFFF) {
2437 case 0x504: /* Hrp2 */
2438 case 0x202: /* Jfp2 */
2439 case 0x201: /* Jfp1 */
2440 bt_dev_dbg(hdev, "PPAG not supported for Intel CNVr (0x%3x)",
2441 ver->cnvr_top & 0xFFF);
2445 handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2447 bt_dev_info(hdev, "No support for BT device in ACPI firmware");
2451 memset(&ppag, 0, sizeof(ppag));
2454 ppag.status = AE_NOT_FOUND;
2455 acpi_walk_namespace(ACPI_TYPE_PACKAGE, handle, 1, NULL,
2456 btintel_ppag_callback, &ppag, NULL);
2458 if (ACPI_FAILURE(ppag.status)) {
2459 if (ppag.status == AE_NOT_FOUND) {
2460 bt_dev_dbg(hdev, "PPAG-BT: ACPI entry not found");
2466 if (ppag.domain != 0x12) {
2467 bt_dev_dbg(hdev, "PPAG-BT: Bluetooth domain is disabled in ACPI firmware");
2472 * BIT 0 : 0 Disabled in EU
2474 * BIT 1 : 0 Disabled in China
2475 * 1 Enabled in China
2477 if ((ppag.mode & 0x01) != BIT(0) && (ppag.mode & 0x02) != BIT(1)) {
2478 bt_dev_dbg(hdev, "PPAG-BT: EU, China mode are disabled in CB/BIOS");
2482 ppag_cmd.ppag_enable_flags = cpu_to_le32(ppag.mode);
2484 skb = __hci_cmd_sync(hdev, INTEL_OP_PPAG_CMD, sizeof(ppag_cmd), &ppag_cmd, HCI_CMD_TIMEOUT);
2486 bt_dev_warn(hdev, "Failed to send PPAG Enable (%ld)", PTR_ERR(skb));
2489 bt_dev_info(hdev, "PPAG-BT: Enabled (Mode %d)", ppag.mode);
2493 static int btintel_acpi_reset_method(struct hci_dev *hdev)
2497 union acpi_object *p, *ref;
2498 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2500 status = acpi_evaluate_object(ACPI_HANDLE(GET_HCIDEV_DEV(hdev)), "_PRR", NULL, &buffer);
2501 if (ACPI_FAILURE(status)) {
2502 bt_dev_err(hdev, "Failed to run _PRR method");
2508 if (p->package.count != 1 || p->type != ACPI_TYPE_PACKAGE) {
2509 bt_dev_err(hdev, "Invalid arguments");
2514 ref = &p->package.elements[0];
2515 if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) {
2516 bt_dev_err(hdev, "Invalid object type: 0x%x", ref->type);
2521 status = acpi_evaluate_object(ref->reference.handle, "_RST", NULL, NULL);
2522 if (ACPI_FAILURE(status)) {
2523 bt_dev_err(hdev, "Failed to run_RST method");
2529 kfree(buffer.pointer);
2533 static void btintel_set_dsm_reset_method(struct hci_dev *hdev,
2534 struct intel_version_tlv *ver_tlv)
2536 struct btintel_data *data = hci_get_priv(hdev);
2537 acpi_handle handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2538 u8 reset_payload[4] = {0x01, 0x00, 0x01, 0x00};
2539 union acpi_object *obj, argv4;
2541 RESET_TYPE_WDISABLE2,
2545 handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2548 bt_dev_dbg(hdev, "No support for bluetooth device in ACPI firmware");
2552 if (!acpi_has_method(handle, "_PRR")) {
2553 bt_dev_err(hdev, "No support for _PRR ACPI method");
2557 switch (ver_tlv->cnvi_top & 0xfff) {
2558 case 0x910: /* GalePeak2 */
2559 reset_payload[2] = RESET_TYPE_VSEC;
2562 /* WDISABLE2 is the default reset method */
2563 reset_payload[2] = RESET_TYPE_WDISABLE2;
2565 if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2566 BIT(DSM_SET_WDISABLE2_DELAY))) {
2567 bt_dev_err(hdev, "No dsm support to set reset delay");
2570 argv4.integer.type = ACPI_TYPE_INTEGER;
2571 /* delay required to toggle BT power */
2572 argv4.integer.value = 160;
2573 obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2574 DSM_SET_WDISABLE2_DELAY, &argv4);
2576 bt_dev_err(hdev, "Failed to call dsm to set reset delay");
2582 bt_dev_info(hdev, "DSM reset method type: 0x%02x", reset_payload[2]);
2584 if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2585 DSM_SET_RESET_METHOD)) {
2586 bt_dev_warn(hdev, "No support for dsm to set reset method");
2589 argv4.buffer.type = ACPI_TYPE_BUFFER;
2590 argv4.buffer.length = sizeof(reset_payload);
2591 argv4.buffer.pointer = reset_payload;
2593 obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2594 DSM_SET_RESET_METHOD, &argv4);
2596 bt_dev_err(hdev, "Failed to call dsm to set reset method");
2600 data->acpi_reset_method = btintel_acpi_reset_method;
2603 int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
2604 struct intel_version_tlv *ver)
2609 struct intel_version_tlv new_ver;
2611 bt_dev_dbg(hdev, "");
2613 /* Set the default boot parameter to 0x0 and it is updated to
2614 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2615 * command while downloading the firmware.
2617 boot_param = 0x00000000;
2619 btintel_set_flag(hdev, INTEL_BOOTLOADER);
2621 err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2625 /* check if controller is already having an operational firmware */
2626 if (ver->img_type == BTINTEL_IMG_OP)
2629 err = btintel_boot(hdev, boot_param);
2633 err = btintel_read_version_tlv(hdev, ver);
2637 /* If image type returned is BTINTEL_IMG_IML, then controller supports
2638 * intermediae loader image
2640 if (ver->img_type == BTINTEL_IMG_IML) {
2641 err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2645 err = btintel_boot(hdev, boot_param);
2650 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2652 btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
2653 /* Once the device is running in operational mode, it needs to
2654 * apply the device configuration (DDC) parameters.
2656 * The device can work without DDC parameters, so even if it
2657 * fails to load the file, no need to fail the setup.
2659 btintel_load_ddc_config(hdev, ddcname);
2661 /* Read supported use cases and set callbacks to fetch datapath id */
2662 btintel_configure_offload(hdev);
2664 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2666 /* Set PPAG feature */
2667 btintel_set_ppag(hdev, ver);
2669 /* Read the Intel version information after loading the FW */
2670 err = btintel_read_version_tlv(hdev, &new_ver);
2674 btintel_version_info_tlv(hdev, &new_ver);
2677 /* Set the event mask for Intel specific vendor events. This enables
2678 * a few extra events that are useful during general operation. It
2679 * does not enable any debugging related events.
2681 * The device will function correctly without these events enabled
2682 * and thus no need to fail the setup.
2684 btintel_set_event_mask(hdev, false);
2688 EXPORT_SYMBOL_GPL(btintel_bootloader_setup_tlv);
2690 void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
2692 switch (hw_variant) {
2693 /* Legacy bootloader devices that supports MSFT Extension */
2694 case 0x11: /* JfP */
2695 case 0x12: /* ThP */
2696 case 0x13: /* HrP */
2697 case 0x14: /* CcP */
2698 /* All Intel new genration controllers support the Microsoft vendor
2699 * extension are using 0xFC1E for VsMsftOpCode.
2707 hci_set_msft_opcode(hdev, 0xFC1E);
2714 EXPORT_SYMBOL_GPL(btintel_set_msft_opcode);
2716 static void btintel_print_fseq_info(struct hci_dev *hdev)
2718 struct sk_buff *skb;
2723 skb = __hci_cmd_sync(hdev, 0xfcb3, 0, NULL, HCI_CMD_TIMEOUT);
2725 bt_dev_dbg(hdev, "Reading fseq status command failed (%ld)",
2730 if (skb->len < (sizeof(u32) * 16 + 2)) {
2731 bt_dev_dbg(hdev, "Malformed packet of length %u received",
2737 p = skb_pull_data(skb, 1);
2739 bt_dev_dbg(hdev, "Failed to get fseq status (0x%2.2x)", *p);
2744 p = skb_pull_data(skb, 1);
2750 str = "Fatal error";
2753 str = "Semaphore acquire error";
2756 str = "Unknown error";
2761 bt_dev_err(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
2766 bt_dev_info(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
2768 val = get_unaligned_le32(skb_pull_data(skb, 4));
2769 bt_dev_dbg(hdev, "Reason: 0x%8.8x", val);
2771 val = get_unaligned_le32(skb_pull_data(skb, 4));
2772 bt_dev_dbg(hdev, "Global version: 0x%8.8x", val);
2774 val = get_unaligned_le32(skb_pull_data(skb, 4));
2775 bt_dev_dbg(hdev, "Installed version: 0x%8.8x", val);
2778 skb_pull_data(skb, 4);
2779 bt_dev_info(hdev, "Fseq executed: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
2783 skb_pull_data(skb, 4);
2784 bt_dev_info(hdev, "Fseq BT Top: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
2787 val = get_unaligned_le32(skb_pull_data(skb, 4));
2788 bt_dev_dbg(hdev, "Fseq Top init version: 0x%8.8x", val);
2790 val = get_unaligned_le32(skb_pull_data(skb, 4));
2791 bt_dev_dbg(hdev, "Fseq Cnvio init version: 0x%8.8x", val);
2793 val = get_unaligned_le32(skb_pull_data(skb, 4));
2794 bt_dev_dbg(hdev, "Fseq MBX Wifi file version: 0x%8.8x", val);
2796 val = get_unaligned_le32(skb_pull_data(skb, 4));
2797 bt_dev_dbg(hdev, "Fseq BT version: 0x%8.8x", val);
2799 val = get_unaligned_le32(skb_pull_data(skb, 4));
2800 bt_dev_dbg(hdev, "Fseq Top reset address: 0x%8.8x", val);
2802 val = get_unaligned_le32(skb_pull_data(skb, 4));
2803 bt_dev_dbg(hdev, "Fseq MBX timeout: 0x%8.8x", val);
2805 val = get_unaligned_le32(skb_pull_data(skb, 4));
2806 bt_dev_dbg(hdev, "Fseq MBX ack: 0x%8.8x", val);
2808 val = get_unaligned_le32(skb_pull_data(skb, 4));
2809 bt_dev_dbg(hdev, "Fseq CNVi id: 0x%8.8x", val);
2811 val = get_unaligned_le32(skb_pull_data(skb, 4));
2812 bt_dev_dbg(hdev, "Fseq CNVr id: 0x%8.8x", val);
2814 val = get_unaligned_le32(skb_pull_data(skb, 4));
2815 bt_dev_dbg(hdev, "Fseq Error handle: 0x%8.8x", val);
2817 val = get_unaligned_le32(skb_pull_data(skb, 4));
2818 bt_dev_dbg(hdev, "Fseq Magic noalive indication: 0x%8.8x", val);
2820 val = get_unaligned_le32(skb_pull_data(skb, 4));
2821 bt_dev_dbg(hdev, "Fseq OTP version: 0x%8.8x", val);
2823 val = get_unaligned_le32(skb_pull_data(skb, 4));
2824 bt_dev_dbg(hdev, "Fseq MBX otp version: 0x%8.8x", val);
2829 static int btintel_setup_combined(struct hci_dev *hdev)
2831 const u8 param[1] = { 0xFF };
2832 struct intel_version ver;
2833 struct intel_version_tlv ver_tlv;
2834 struct sk_buff *skb;
2837 BT_DBG("%s", hdev->name);
2839 /* The some controllers have a bug with the first HCI command sent to it
2840 * returning number of completed commands as zero. This would stall the
2841 * command processing in the Bluetooth core.
2843 * As a workaround, send HCI Reset command first which will reset the
2844 * number of completed commands and allow normal command processing
2847 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
2848 * in the SW_RFKILL ON state as a workaround of fixing LED issue during
2849 * the shutdown() procedure, and once the device is in SW_RFKILL ON
2850 * state, the only way to exit out of it is sending the HCI_Reset
2853 if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
2854 btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2855 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
2859 "sending initial HCI reset failed (%ld)",
2861 return PTR_ERR(skb);
2866 /* Starting from TyP device, the command parameter and response are
2867 * changed even though the OCF for HCI_Intel_Read_Version command
2868 * remains same. The legacy devices can handle even if the
2869 * command has a parameter and returns a correct version information.
2870 * So, it uses new format to support both legacy and new format.
2872 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
2874 bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
2876 return PTR_ERR(skb);
2879 /* Check the status */
2881 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
2887 /* Apply the common HCI quirks for Intel device */
2888 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2889 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2890 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2892 /* Set up the quality report callback for Intel devices */
2893 hdev->set_quality_report = btintel_set_quality_report;
2895 /* For Legacy device, check the HW platform value and size */
2896 if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
2897 bt_dev_dbg(hdev, "Read the legacy Intel version information");
2899 memcpy(&ver, skb->data, sizeof(ver));
2901 /* Display version information */
2902 btintel_version_info(hdev, &ver);
2904 /* Check for supported iBT hardware variants of this firmware
2907 * This check has been put in place to ensure correct forward
2908 * compatibility options when newer hardware variants come
2911 switch (ver.hw_variant) {
2913 case 0x08: /* StP */
2914 /* Legacy ROM product */
2915 btintel_set_flag(hdev, INTEL_ROM_LEGACY);
2917 /* Apply the device specific HCI quirks
2919 * WBS for SdP - For the Legacy ROM products, only SdP
2920 * supports the WBS. But the version information is not
2921 * enough to use here because the StP2 and SdP have same
2922 * hw_variant and fw_variant. So, this flag is set by
2923 * the transport driver (btusb) based on the HW info
2926 if (!btintel_test_flag(hdev,
2927 INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
2928 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2930 if (ver.hw_variant == 0x08 && ver.fw_variant == 0x22)
2931 set_bit(HCI_QUIRK_VALID_LE_STATES,
2934 err = btintel_legacy_rom_setup(hdev, &ver);
2936 case 0x0b: /* SfP */
2937 case 0x11: /* JfP */
2938 case 0x12: /* ThP */
2939 case 0x13: /* HrP */
2940 case 0x14: /* CcP */
2941 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2943 case 0x0c: /* WsP */
2944 /* Apply the device specific HCI quirks
2946 * All Legacy bootloader devices support WBS
2948 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2951 /* These variants don't seem to support LE Coded PHY */
2952 set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
2954 /* Setup MSFT Extension support */
2955 btintel_set_msft_opcode(hdev, ver.hw_variant);
2957 err = btintel_bootloader_setup(hdev, &ver);
2958 btintel_register_devcoredump_support(hdev);
2961 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2966 hci_set_hw_info(hdev,
2967 "INTEL platform=%u variant=%u revision=%u",
2968 ver.hw_platform, ver.hw_variant,
2974 /* memset ver_tlv to start with clean state as few fields are exclusive
2975 * to bootloader mode and are not populated in operational mode
2977 memset(&ver_tlv, 0, sizeof(ver_tlv));
2978 /* For TLV type device, parse the tlv data */
2979 err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
2981 bt_dev_err(hdev, "Failed to parse TLV version information");
2985 if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
2986 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2987 INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
2992 /* Check for supported iBT hardware variants of this firmware
2995 * This check has been put in place to ensure correct forward
2996 * compatibility options when newer hardware variants come
2999 switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
3000 case 0x11: /* JfP */
3001 case 0x12: /* ThP */
3002 case 0x13: /* HrP */
3003 case 0x14: /* CcP */
3004 /* Some legacy bootloader devices starting from JfP,
3005 * the operational firmware supports both old and TLV based
3006 * HCI_Intel_Read_Version command based on the command
3009 * For upgrading firmware case, the TLV based version cannot
3010 * be used because the firmware filename for legacy bootloader
3011 * is based on the old format.
3013 * Also, it is not easy to convert TLV based version from the
3014 * legacy version format.
3016 * So, as a workaround for those devices, use the legacy
3017 * HCI_Intel_Read_Version to get the version information and
3018 * run the legacy bootloader setup.
3020 err = btintel_read_version(hdev, &ver);
3024 /* Apply the device specific HCI quirks
3026 * All Legacy bootloader devices support WBS
3028 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3030 /* These variants don't seem to support LE Coded PHY */
3031 set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
3033 /* Set Valid LE States quirk */
3034 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3036 /* Setup MSFT Extension support */
3037 btintel_set_msft_opcode(hdev, ver.hw_variant);
3039 err = btintel_bootloader_setup(hdev, &ver);
3040 btintel_register_devcoredump_support(hdev);
3048 /* Display version information of TLV type */
3049 btintel_version_info_tlv(hdev, &ver_tlv);
3051 /* Apply the device specific HCI quirks for TLV based devices
3053 * All TLV based devices support WBS
3055 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3057 /* Apply LE States quirk from solar onwards */
3058 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3060 /* Setup MSFT Extension support */
3061 btintel_set_msft_opcode(hdev,
3062 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3063 btintel_set_dsm_reset_method(hdev, &ver_tlv);
3065 err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
3066 btintel_register_devcoredump_support(hdev);
3067 btintel_print_fseq_info(hdev);
3070 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
3071 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3076 hci_set_hw_info(hdev, "INTEL platform=%u variant=%u",
3077 INTEL_HW_PLATFORM(ver_tlv.cnvi_bt),
3078 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3086 int btintel_shutdown_combined(struct hci_dev *hdev)
3088 struct sk_buff *skb;
3091 /* Send HCI Reset to the controller to stop any BT activity which
3092 * were triggered. This will help to save power and maintain the
3093 * sync b/w Host and controller
3095 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3097 bt_dev_err(hdev, "HCI reset during shutdown failed");
3098 return PTR_ERR(skb);
3103 /* Some platforms have an issue with BT LED when the interface is
3104 * down or BT radio is turned off, which takes 5 seconds to BT LED
3105 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
3106 * device in the RFKILL ON state which turns off the BT LED immediately.
3108 if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
3109 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3112 bt_dev_err(hdev, "turning off Intel device LED failed");
3120 EXPORT_SYMBOL_GPL(btintel_shutdown_combined);
3122 int btintel_configure_setup(struct hci_dev *hdev, const char *driver_name)
3124 hdev->manufacturer = 2;
3125 hdev->setup = btintel_setup_combined;
3126 hdev->shutdown = btintel_shutdown_combined;
3127 hdev->hw_error = btintel_hw_error;
3128 hdev->set_diag = btintel_set_diag_combined;
3129 hdev->set_bdaddr = btintel_set_bdaddr;
3131 coredump_info.driver_name = driver_name;
3135 EXPORT_SYMBOL_GPL(btintel_configure_setup);
3137 static int btintel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb)
3139 struct intel_tlv *tlv = (void *)&skb->data[5];
3141 /* The first event is always an event type TLV */
3142 if (tlv->type != INTEL_TLV_TYPE_ID)
3145 switch (tlv->val[0]) {
3146 case INTEL_TLV_SYSTEM_EXCEPTION:
3147 case INTEL_TLV_FATAL_EXCEPTION:
3148 case INTEL_TLV_DEBUG_EXCEPTION:
3149 case INTEL_TLV_TEST_EXCEPTION:
3150 /* Generate devcoredump from exception */
3151 if (!hci_devcd_init(hdev, skb->len)) {
3152 hci_devcd_append(hdev, skb);
3153 hci_devcd_complete(hdev);
3155 bt_dev_err(hdev, "Failed to generate devcoredump");
3160 bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]);
3164 return hci_recv_frame(hdev, skb);
3167 int btintel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
3169 struct hci_event_hdr *hdr = (void *)skb->data;
3170 const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
3172 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
3174 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
3175 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
3177 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
3178 switch (skb->data[2]) {
3180 /* When switching to the operational firmware
3181 * the device sends a vendor specific event
3182 * indicating that the bootup completed.
3184 btintel_bootup(hdev, ptr, len);
3187 /* When the firmware loading completes the
3188 * device sends out a vendor specific event
3189 * indicating the result of the firmware
3192 btintel_secure_send_result(hdev, ptr, len);
3197 /* Handle all diagnostics events separately. May still call
3200 if (len >= sizeof(diagnostics_hdr) &&
3201 memcmp(&skb->data[2], diagnostics_hdr,
3202 sizeof(diagnostics_hdr)) == 0) {
3203 return btintel_diagnostics(hdev, skb);
3207 return hci_recv_frame(hdev, skb);
3209 EXPORT_SYMBOL_GPL(btintel_recv_event);
3211 void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
3213 const struct intel_bootup *evt = ptr;
3215 if (len != sizeof(*evt))
3218 if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
3219 btintel_wake_up_flag(hdev, INTEL_BOOTING);
3221 EXPORT_SYMBOL_GPL(btintel_bootup);
3223 void btintel_secure_send_result(struct hci_dev *hdev,
3224 const void *ptr, unsigned int len)
3226 const struct intel_secure_send_result *evt = ptr;
3228 if (len != sizeof(*evt))
3232 btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
3234 if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
3235 btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
3236 btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
3238 EXPORT_SYMBOL_GPL(btintel_secure_send_result);
3241 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
3242 MODULE_VERSION(VERSION);
3243 MODULE_LICENSE("GPL");
3244 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
3245 MODULE_FIRMWARE("intel/ibt-11-5.ddc");
3246 MODULE_FIRMWARE("intel/ibt-12-16.sfi");
3247 MODULE_FIRMWARE("intel/ibt-12-16.ddc");