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 static 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));
281 int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
285 /* The hardware platform number has a fixed value of 0x37 and
286 * for now only accept this single value.
288 if (ver->hw_platform != 0x37) {
289 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
294 /* Check for supported iBT hardware variants of this firmware
297 * This check has been put in place to ensure correct forward
298 * compatibility options when newer hardware variants come along.
300 switch (ver->hw_variant) {
301 case 0x07: /* WP - Legacy ROM */
302 case 0x08: /* StP - Legacy ROM */
311 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
316 switch (ver->fw_variant) {
318 variant = "Legacy ROM 2.5";
321 variant = "Bootloader";
324 variant = "Legacy ROM 2.x";
327 variant = "Firmware";
330 bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
334 coredump_info.hw_variant = ver->hw_variant;
335 coredump_info.fw_build_num = ver->fw_build_num;
337 bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
338 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
339 ver->fw_build_num, ver->fw_build_ww,
340 2000 + ver->fw_build_yy);
344 EXPORT_SYMBOL_GPL(btintel_version_info);
346 static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
351 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
353 cmd_param[0] = fragment_type;
354 memcpy(cmd_param + 1, param, fragment_len);
356 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
357 cmd_param, HCI_INIT_TIMEOUT);
363 plen -= fragment_len;
364 param += fragment_len;
370 int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
372 const struct firmware *fw;
377 err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
379 bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
384 bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
388 /* DDC file contains one or more DDC structure which has
389 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
391 while (fw->size > fw_ptr - fw->data) {
392 u8 cmd_plen = fw_ptr[0] + sizeof(u8);
394 skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
397 bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
399 release_firmware(fw);
407 release_firmware(fw);
409 bt_dev_info(hdev, "Applying Intel DDC parameters completed");
413 EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
415 int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
419 err = btintel_enter_mfg(hdev);
423 ret = btintel_set_event_mask(hdev, debug);
425 err = btintel_exit_mfg(hdev, false, false);
431 EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
433 int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
437 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
439 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
444 if (!skb || skb->len != sizeof(*ver)) {
445 bt_dev_err(hdev, "Intel version event size mismatch");
450 memcpy(ver, skb->data, sizeof(*ver));
456 EXPORT_SYMBOL_GPL(btintel_read_version);
458 static int btintel_version_info_tlv(struct hci_dev *hdev,
459 struct intel_version_tlv *version)
463 /* The hardware platform number has a fixed value of 0x37 and
464 * for now only accept this single value.
466 if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
467 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
468 INTEL_HW_PLATFORM(version->cnvi_bt));
472 /* Check for supported iBT hardware variants of this firmware
475 * This check has been put in place to ensure correct forward
476 * compatibility options when newer hardware variants come along.
478 switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
481 case 0x19: /* Slr-F */
483 case 0x1c: /* Gale Peak (GaP) */
486 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
487 INTEL_HW_VARIANT(version->cnvi_bt));
491 switch (version->img_type) {
493 variant = "Bootloader";
494 /* It is required that every single firmware fragment is acknowledged
495 * with a command complete event. If the boot parameters indicate
496 * that this bootloader does not send them, then abort the setup.
498 if (version->limited_cce != 0x00) {
499 bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
500 version->limited_cce);
504 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
505 if (version->sbe_type > 0x01) {
506 bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
511 bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
512 bt_dev_info(hdev, "Secure boot is %s",
513 version->secure_boot ? "enabled" : "disabled");
514 bt_dev_info(hdev, "OTP lock is %s",
515 version->otp_lock ? "enabled" : "disabled");
516 bt_dev_info(hdev, "API lock is %s",
517 version->api_lock ? "enabled" : "disabled");
518 bt_dev_info(hdev, "Debug lock is %s",
519 version->debug_lock ? "enabled" : "disabled");
520 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
521 version->min_fw_build_nn, version->min_fw_build_cw,
522 2000 + version->min_fw_build_yy);
525 variant = "Firmware";
528 bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
532 coredump_info.hw_variant = INTEL_HW_VARIANT(version->cnvi_bt);
533 coredump_info.fw_build_num = version->build_num;
535 bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
536 2000 + (version->timestamp >> 8), version->timestamp & 0xff,
537 version->build_type, version->build_num);
538 if (version->img_type == 0x03)
539 bt_dev_info(hdev, "Firmware SHA1: 0x%8.8x", version->git_sha1);
544 static int btintel_parse_version_tlv(struct hci_dev *hdev,
545 struct intel_version_tlv *version,
548 /* Consume Command Complete Status field */
551 /* Event parameters contatin multiple TLVs. Read each of them
552 * and only keep the required data. Also, it use existing legacy
553 * version field like hw_platform, hw_variant, and fw_variant
554 * to keep the existing setup flow
557 struct intel_tlv *tlv;
559 /* Make sure skb has a minimum length of the header */
560 if (skb->len < sizeof(*tlv))
563 tlv = (struct intel_tlv *)skb->data;
565 /* Make sure skb has a enough data */
566 if (skb->len < tlv->len + sizeof(*tlv))
570 case INTEL_TLV_CNVI_TOP:
571 version->cnvi_top = get_unaligned_le32(tlv->val);
573 case INTEL_TLV_CNVR_TOP:
574 version->cnvr_top = get_unaligned_le32(tlv->val);
576 case INTEL_TLV_CNVI_BT:
577 version->cnvi_bt = get_unaligned_le32(tlv->val);
579 case INTEL_TLV_CNVR_BT:
580 version->cnvr_bt = get_unaligned_le32(tlv->val);
582 case INTEL_TLV_DEV_REV_ID:
583 version->dev_rev_id = get_unaligned_le16(tlv->val);
585 case INTEL_TLV_IMAGE_TYPE:
586 version->img_type = tlv->val[0];
588 case INTEL_TLV_TIME_STAMP:
589 /* If image type is Operational firmware (0x03), then
590 * running FW Calendar Week and Year information can
591 * be extracted from Timestamp information
593 version->min_fw_build_cw = tlv->val[0];
594 version->min_fw_build_yy = tlv->val[1];
595 version->timestamp = get_unaligned_le16(tlv->val);
597 case INTEL_TLV_BUILD_TYPE:
598 version->build_type = tlv->val[0];
600 case INTEL_TLV_BUILD_NUM:
601 /* If image type is Operational firmware (0x03), then
602 * running FW build number can be extracted from the
605 version->min_fw_build_nn = tlv->val[0];
606 version->build_num = get_unaligned_le32(tlv->val);
608 case INTEL_TLV_SECURE_BOOT:
609 version->secure_boot = tlv->val[0];
611 case INTEL_TLV_OTP_LOCK:
612 version->otp_lock = tlv->val[0];
614 case INTEL_TLV_API_LOCK:
615 version->api_lock = tlv->val[0];
617 case INTEL_TLV_DEBUG_LOCK:
618 version->debug_lock = tlv->val[0];
620 case INTEL_TLV_MIN_FW:
621 version->min_fw_build_nn = tlv->val[0];
622 version->min_fw_build_cw = tlv->val[1];
623 version->min_fw_build_yy = tlv->val[2];
625 case INTEL_TLV_LIMITED_CCE:
626 version->limited_cce = tlv->val[0];
628 case INTEL_TLV_SBE_TYPE:
629 version->sbe_type = tlv->val[0];
631 case INTEL_TLV_OTP_BDADDR:
632 memcpy(&version->otp_bd_addr, tlv->val,
635 case INTEL_TLV_GIT_SHA1:
636 version->git_sha1 = get_unaligned_le32(tlv->val);
639 /* Ignore rest of information */
642 /* consume the current tlv and move to next*/
643 skb_pull(skb, tlv->len + sizeof(*tlv));
649 static int btintel_read_version_tlv(struct hci_dev *hdev,
650 struct intel_version_tlv *version)
653 const u8 param[1] = { 0xFF };
658 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
660 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
666 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
672 btintel_parse_version_tlv(hdev, version, skb);
678 /* ------- REGMAP IBT SUPPORT ------- */
680 #define IBT_REG_MODE_8BIT 0x00
681 #define IBT_REG_MODE_16BIT 0x01
682 #define IBT_REG_MODE_32BIT 0x02
684 struct regmap_ibt_context {
685 struct hci_dev *hdev;
690 struct ibt_cp_reg_access {
697 struct ibt_rp_reg_access {
703 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
704 void *val, size_t val_size)
706 struct regmap_ibt_context *ctx = context;
707 struct ibt_cp_reg_access cp;
708 struct ibt_rp_reg_access *rp;
712 if (reg_size != sizeof(__le32))
717 cp.mode = IBT_REG_MODE_8BIT;
720 cp.mode = IBT_REG_MODE_16BIT;
723 cp.mode = IBT_REG_MODE_32BIT;
729 /* regmap provides a little-endian formatted addr */
730 cp.addr = *(__le32 *)addr;
733 bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
735 skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
739 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
740 le32_to_cpu(cp.addr), err);
744 if (skb->len != sizeof(*rp) + val_size) {
745 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
746 le32_to_cpu(cp.addr));
751 rp = (struct ibt_rp_reg_access *)skb->data;
753 if (rp->addr != cp.addr) {
754 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
755 le32_to_cpu(rp->addr));
760 memcpy(val, rp->data, val_size);
767 static int regmap_ibt_gather_write(void *context,
768 const void *addr, size_t reg_size,
769 const void *val, size_t val_size)
771 struct regmap_ibt_context *ctx = context;
772 struct ibt_cp_reg_access *cp;
774 int plen = sizeof(*cp) + val_size;
778 if (reg_size != sizeof(__le32))
783 mode = IBT_REG_MODE_8BIT;
786 mode = IBT_REG_MODE_16BIT;
789 mode = IBT_REG_MODE_32BIT;
795 cp = kmalloc(plen, GFP_KERNEL);
799 /* regmap provides a little-endian formatted addr/value */
800 cp->addr = *(__le32 *)addr;
803 memcpy(&cp->data, val, val_size);
805 bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
807 skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
810 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
811 le32_to_cpu(cp->addr), err);
821 static int regmap_ibt_write(void *context, const void *data, size_t count)
823 /* data contains register+value, since we only support 32bit addr,
824 * minimum data size is 4 bytes.
826 if (WARN_ONCE(count < 4, "Invalid register access"))
829 return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
832 static void regmap_ibt_free_context(void *context)
837 static const struct regmap_bus regmap_ibt = {
838 .read = regmap_ibt_read,
839 .write = regmap_ibt_write,
840 .gather_write = regmap_ibt_gather_write,
841 .free_context = regmap_ibt_free_context,
842 .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
843 .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
846 /* Config is the same for all register regions */
847 static const struct regmap_config regmap_ibt_cfg = {
848 .name = "btintel_regmap",
853 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
856 struct regmap_ibt_context *ctx;
858 bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
861 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
863 return ERR_PTR(-ENOMEM);
865 ctx->op_read = opcode_read;
866 ctx->op_write = opcode_write;
869 return regmap_init(&hdev->dev, ®map_ibt, ctx, ®map_ibt_cfg);
871 EXPORT_SYMBOL_GPL(btintel_regmap_init);
873 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
875 struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
878 params.boot_param = cpu_to_le32(boot_param);
880 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), ¶ms,
883 bt_dev_err(hdev, "Failed to send Intel Reset command");
891 EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
893 int btintel_read_boot_params(struct hci_dev *hdev,
894 struct intel_boot_params *params)
898 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
900 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
905 if (skb->len != sizeof(*params)) {
906 bt_dev_err(hdev, "Intel boot parameters size mismatch");
911 memcpy(params, skb->data, sizeof(*params));
915 if (params->status) {
916 bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
918 return -bt_to_errno(params->status);
921 bt_dev_info(hdev, "Device revision is %u",
922 le16_to_cpu(params->dev_revid));
924 bt_dev_info(hdev, "Secure boot is %s",
925 params->secure_boot ? "enabled" : "disabled");
927 bt_dev_info(hdev, "OTP lock is %s",
928 params->otp_lock ? "enabled" : "disabled");
930 bt_dev_info(hdev, "API lock is %s",
931 params->api_lock ? "enabled" : "disabled");
933 bt_dev_info(hdev, "Debug lock is %s",
934 params->debug_lock ? "enabled" : "disabled");
936 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
937 params->min_fw_build_nn, params->min_fw_build_cw,
938 2000 + params->min_fw_build_yy);
942 EXPORT_SYMBOL_GPL(btintel_read_boot_params);
944 static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
945 const struct firmware *fw)
949 /* Start the firmware download transaction with the Init fragment
950 * represented by the 128 bytes of CSS header.
952 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
954 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
958 /* Send the 256 bytes of public key information from the firmware
959 * as the PKey fragment.
961 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
963 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
967 /* Send the 256 bytes of signature information from the firmware
968 * as the Sign fragment.
970 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
972 bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
980 static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
981 const struct firmware *fw)
985 /* Start the firmware download transaction with the Init fragment
986 * represented by the 128 bytes of CSS header.
988 err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
990 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
994 /* Send the 96 bytes of public key information from the firmware
995 * as the PKey fragment.
997 err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
999 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
1003 /* Send the 96 bytes of signature information from the firmware
1004 * as the Sign fragment
1006 err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
1008 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
1015 static int btintel_download_firmware_payload(struct hci_dev *hdev,
1016 const struct firmware *fw,
1023 fw_ptr = fw->data + offset;
1027 while (fw_ptr - fw->data < fw->size) {
1028 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
1030 frag_len += sizeof(*cmd) + cmd->plen;
1032 /* The parameter length of the secure send command requires
1033 * a 4 byte alignment. It happens so that the firmware file
1034 * contains proper Intel_NOP commands to align the fragments
1037 * Send set of commands with 4 byte alignment from the
1038 * firmware data buffer as a single Data fragement.
1040 if (!(frag_len % 4)) {
1041 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1044 "Failed to send firmware data (%d)",
1058 static bool btintel_firmware_version(struct hci_dev *hdev,
1059 u8 num, u8 ww, u8 yy,
1060 const struct firmware *fw,
1067 while (fw_ptr - fw->data < fw->size) {
1068 struct hci_command_hdr *cmd = (void *)(fw_ptr);
1070 /* Each SKU has a different reset parameter to use in the
1071 * HCI_Intel_Reset command and it is embedded in the firmware
1072 * data. So, instead of using static value per SKU, check
1073 * the firmware data and save it for later use.
1075 if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1076 struct cmd_write_boot_params *params;
1078 params = (void *)(fw_ptr + sizeof(*cmd));
1080 *boot_addr = le32_to_cpu(params->boot_addr);
1082 bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
1084 bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1085 params->fw_build_num, params->fw_build_ww,
1086 params->fw_build_yy);
1088 return (num == params->fw_build_num &&
1089 ww == params->fw_build_ww &&
1090 yy == params->fw_build_yy);
1093 fw_ptr += sizeof(*cmd) + cmd->plen;
1099 int btintel_download_firmware(struct hci_dev *hdev,
1100 struct intel_version *ver,
1101 const struct firmware *fw,
1106 /* SfP and WsP don't seem to update the firmware version on file
1107 * so version checking is currently not possible.
1109 switch (ver->hw_variant) {
1110 case 0x0b: /* SfP */
1111 case 0x0c: /* WsP */
1112 /* Skip version checking */
1116 /* Skip download if firmware has the same version */
1117 if (btintel_firmware_version(hdev, ver->fw_build_num,
1118 ver->fw_build_ww, ver->fw_build_yy,
1120 bt_dev_info(hdev, "Firmware already loaded");
1121 /* Return -EALREADY to indicate that the firmware has
1122 * already been loaded.
1128 /* The firmware variant determines if the device is in bootloader
1129 * mode or is running operational firmware. The value 0x06 identifies
1130 * the bootloader and the value 0x23 identifies the operational
1133 * If the firmware version has changed that means it needs to be reset
1134 * to bootloader when operational so the new firmware can be loaded.
1136 if (ver->fw_variant == 0x23)
1139 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1143 return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1145 EXPORT_SYMBOL_GPL(btintel_download_firmware);
1147 static int btintel_download_fw_tlv(struct hci_dev *hdev,
1148 struct intel_version_tlv *ver,
1149 const struct firmware *fw, u32 *boot_param,
1150 u8 hw_variant, u8 sbe_type)
1155 /* Skip download if firmware has the same version */
1156 if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1157 ver->min_fw_build_cw,
1158 ver->min_fw_build_yy,
1160 bt_dev_info(hdev, "Firmware already loaded");
1161 /* Return -EALREADY to indicate that firmware has
1162 * already been loaded.
1167 /* The firmware variant determines if the device is in bootloader
1168 * mode or is running operational firmware. The value 0x01 identifies
1169 * the bootloader and the value 0x03 identifies the operational
1172 * If the firmware version has changed that means it needs to be reset
1173 * to bootloader when operational so the new firmware can be loaded.
1175 if (ver->img_type == 0x03)
1178 /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1179 * only RSA secure boot engine. Hence, the corresponding sfi file will
1180 * have RSA header of 644 bytes followed by Command Buffer.
1182 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1183 * secure boot engine. As a result, the corresponding sfi file will
1184 * have RSA header of 644, ECDSA header of 320 bytes followed by
1187 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1188 * version: RSA(0x00010000) , ECDSA (0x00020000)
1190 css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1191 if (css_header_ver != 0x00010000) {
1192 bt_dev_err(hdev, "Invalid CSS Header version");
1196 if (hw_variant <= 0x14) {
1197 if (sbe_type != 0x00) {
1198 bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1203 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1207 err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1210 } else if (hw_variant >= 0x17) {
1211 /* Check if CSS header for ECDSA follows the RSA header */
1212 if (fw->data[ECDSA_OFFSET] != 0x06)
1215 /* Check if the CSS Header version is ECDSA(0x00020000) */
1216 css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1217 if (css_header_ver != 0x00020000) {
1218 bt_dev_err(hdev, "Invalid CSS Header version");
1222 if (sbe_type == 0x00) {
1223 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1227 err = btintel_download_firmware_payload(hdev, fw,
1228 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1231 } else if (sbe_type == 0x01) {
1232 err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1236 err = btintel_download_firmware_payload(hdev, fw,
1237 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1245 static void btintel_reset_to_bootloader(struct hci_dev *hdev)
1247 struct intel_reset params;
1248 struct sk_buff *skb;
1250 /* Send Intel Reset command. This will result in
1251 * re-enumeration of BT controller.
1253 * Intel Reset parameter description:
1254 * reset_type : 0x00 (Soft reset),
1256 * patch_enable : 0x00 (Do not enable),
1258 * ddc_reload : 0x00 (Do not reload),
1260 * boot_option: 0x00 (Current image),
1261 * 0x01 (Specified boot address)
1262 * boot_param: Boot address
1265 params.reset_type = 0x01;
1266 params.patch_enable = 0x01;
1267 params.ddc_reload = 0x01;
1268 params.boot_option = 0x00;
1269 params.boot_param = cpu_to_le32(0x00000000);
1271 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1272 ¶ms, HCI_INIT_TIMEOUT);
1274 bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1278 bt_dev_info(hdev, "Intel reset sent to retry FW download");
1281 /* Current Intel BT controllers(ThP/JfP) hold the USB reset
1282 * lines for 2ms when it receives Intel Reset in bootloader mode.
1283 * Whereas, the upcoming Intel BT controllers will hold USB reset
1284 * for 150ms. To keep the delay generic, 150ms is chosen here.
1289 static int btintel_read_debug_features(struct hci_dev *hdev,
1290 struct intel_debug_features *features)
1292 struct sk_buff *skb;
1295 /* Intel controller supports two pages, each page is of 128-bit
1296 * feature bit mask. And each bit defines specific feature support
1298 skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1301 bt_dev_err(hdev, "Reading supported features failed (%ld)",
1303 return PTR_ERR(skb);
1306 if (skb->len != (sizeof(features->page1) + 3)) {
1307 bt_dev_err(hdev, "Supported features event size mismatch");
1312 memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1314 /* Read the supported features page2 if required in future.
1320 static acpi_status btintel_ppag_callback(acpi_handle handle, u32 lvl, void *data,
1325 struct btintel_ppag *ppag = data;
1326 union acpi_object *p, *elements;
1327 struct acpi_buffer string = {ACPI_ALLOCATE_BUFFER, NULL};
1328 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1329 struct hci_dev *hdev = ppag->hdev;
1331 status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1332 if (ACPI_FAILURE(status)) {
1333 bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
1337 len = strlen(string.pointer);
1338 if (len < strlen(BTINTEL_PPAG_NAME)) {
1339 kfree(string.pointer);
1343 if (strncmp((char *)string.pointer + len - 4, BTINTEL_PPAG_NAME, 4)) {
1344 kfree(string.pointer);
1347 kfree(string.pointer);
1349 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
1350 if (ACPI_FAILURE(status)) {
1351 ppag->status = status;
1352 bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
1357 ppag = (struct btintel_ppag *)data;
1359 if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 2) {
1360 kfree(buffer.pointer);
1361 bt_dev_warn(hdev, "PPAG-BT: Invalid object type: %d or package count: %d",
1362 p->type, p->package.count);
1363 ppag->status = AE_ERROR;
1367 elements = p->package.elements;
1369 /* PPAG table is located at element[1] */
1372 ppag->domain = (u32)p->package.elements[0].integer.value;
1373 ppag->mode = (u32)p->package.elements[1].integer.value;
1374 ppag->status = AE_OK;
1375 kfree(buffer.pointer);
1376 return AE_CTRL_TERMINATE;
1379 static int btintel_set_debug_features(struct hci_dev *hdev,
1380 const struct intel_debug_features *features)
1382 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
1384 u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
1385 u8 trace_enable = 0x02;
1386 struct sk_buff *skb;
1389 bt_dev_warn(hdev, "Debug features not read");
1393 if (!(features->page1[0] & 0x3f)) {
1394 bt_dev_info(hdev, "Telemetry exception format not supported");
1398 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1400 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1402 return PTR_ERR(skb);
1406 skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
1408 bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
1410 return PTR_ERR(skb);
1414 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1416 bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
1418 return PTR_ERR(skb);
1422 bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
1423 trace_enable, mask[3]);
1428 static int btintel_reset_debug_features(struct hci_dev *hdev,
1429 const struct intel_debug_features *features)
1431 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
1433 u8 trace_enable = 0x00;
1434 struct sk_buff *skb;
1437 bt_dev_warn(hdev, "Debug features not read");
1441 if (!(features->page1[0] & 0x3f)) {
1442 bt_dev_info(hdev, "Telemetry exception format not supported");
1446 /* Should stop the trace before writing ddc event mask. */
1447 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1449 bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
1451 return PTR_ERR(skb);
1455 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1457 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1459 return PTR_ERR(skb);
1463 bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
1464 trace_enable, mask[3]);
1469 int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
1471 struct intel_debug_features features;
1474 bt_dev_dbg(hdev, "enable %d", enable);
1476 /* Read the Intel supported features and if new exception formats
1477 * supported, need to load the additional DDC config to enable.
1479 err = btintel_read_debug_features(hdev, &features);
1483 /* Set or reset the debug features. */
1485 err = btintel_set_debug_features(hdev, &features);
1487 err = btintel_reset_debug_features(hdev, &features);
1491 EXPORT_SYMBOL_GPL(btintel_set_quality_report);
1493 static void btintel_coredump(struct hci_dev *hdev)
1495 struct sk_buff *skb;
1497 skb = __hci_cmd_sync(hdev, 0xfc4e, 0, NULL, HCI_CMD_TIMEOUT);
1499 bt_dev_err(hdev, "Coredump failed (%ld)", PTR_ERR(skb));
1506 static void btintel_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
1510 snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
1511 coredump_info.hw_variant);
1512 skb_put_data(skb, buf, strlen(buf));
1514 snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
1515 coredump_info.fw_build_num);
1516 skb_put_data(skb, buf, strlen(buf));
1518 snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info.driver_name);
1519 skb_put_data(skb, buf, strlen(buf));
1521 snprintf(buf, sizeof(buf), "Vendor: Intel\n");
1522 skb_put_data(skb, buf, strlen(buf));
1525 static int btintel_register_devcoredump_support(struct hci_dev *hdev)
1527 struct intel_debug_features features;
1530 err = btintel_read_debug_features(hdev, &features);
1532 bt_dev_info(hdev, "Error reading debug features");
1536 if (!(features.page1[0] & 0x3f)) {
1537 bt_dev_dbg(hdev, "Telemetry exception format not supported");
1541 hci_devcd_register(hdev, btintel_coredump, btintel_dmp_hdr, NULL);
1546 static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
1547 struct intel_version *ver)
1549 const struct firmware *fw;
1553 snprintf(fwname, sizeof(fwname),
1554 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1555 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1556 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1557 ver->fw_build_ww, ver->fw_build_yy);
1559 ret = request_firmware(&fw, fwname, &hdev->dev);
1561 if (ret == -EINVAL) {
1562 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1567 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1570 /* If the correct firmware patch file is not found, use the
1571 * default firmware patch file instead
1573 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1574 ver->hw_platform, ver->hw_variant);
1575 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1576 bt_dev_err(hdev, "failed to open default fw file: %s",
1582 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1587 static int btintel_legacy_rom_patching(struct hci_dev *hdev,
1588 const struct firmware *fw,
1589 const u8 **fw_ptr, int *disable_patch)
1591 struct sk_buff *skb;
1592 struct hci_command_hdr *cmd;
1593 const u8 *cmd_param;
1594 struct hci_event_hdr *evt = NULL;
1595 const u8 *evt_param = NULL;
1596 int remain = fw->size - (*fw_ptr - fw->data);
1598 /* The first byte indicates the types of the patch command or event.
1599 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1600 * in the current firmware buffer doesn't start with 0x01 or
1601 * the size of remain buffer is smaller than HCI command header,
1602 * the firmware file is corrupted and it should stop the patching
1605 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1606 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1612 cmd = (struct hci_command_hdr *)(*fw_ptr);
1613 *fw_ptr += sizeof(*cmd);
1614 remain -= sizeof(*cmd);
1616 /* Ensure that the remain firmware data is long enough than the length
1617 * of command parameter. If not, the firmware file is corrupted.
1619 if (remain < cmd->plen) {
1620 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1624 /* If there is a command that loads a patch in the firmware
1625 * file, then enable the patch upon success, otherwise just
1626 * disable the manufacturer mode, for example patch activation
1627 * is not required when the default firmware patch file is used
1628 * because there are no patch data to load.
1630 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1633 cmd_param = *fw_ptr;
1634 *fw_ptr += cmd->plen;
1635 remain -= cmd->plen;
1637 /* This reads the expected events when the above command is sent to the
1638 * device. Some vendor commands expects more than one events, for
1639 * example command status event followed by vendor specific event.
1640 * For this case, it only keeps the last expected event. so the command
1641 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1642 * last expected event.
1644 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1648 evt = (struct hci_event_hdr *)(*fw_ptr);
1649 *fw_ptr += sizeof(*evt);
1650 remain -= sizeof(*evt);
1652 if (remain < evt->plen) {
1653 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1657 evt_param = *fw_ptr;
1658 *fw_ptr += evt->plen;
1659 remain -= evt->plen;
1662 /* Every HCI commands in the firmware file has its correspond event.
1663 * If event is not found or remain is smaller than zero, the firmware
1664 * file is corrupted.
1666 if (!evt || !evt_param || remain < 0) {
1667 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1671 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1672 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1674 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1675 cmd->opcode, PTR_ERR(skb));
1676 return PTR_ERR(skb);
1679 /* It ensures that the returned event matches the event data read from
1680 * the firmware file. At fist, it checks the length and then
1681 * the contents of the event.
1683 if (skb->len != evt->plen) {
1684 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1685 le16_to_cpu(cmd->opcode));
1690 if (memcmp(skb->data, evt_param, evt->plen)) {
1691 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1692 le16_to_cpu(cmd->opcode));
1701 static int btintel_legacy_rom_setup(struct hci_dev *hdev,
1702 struct intel_version *ver)
1704 const struct firmware *fw;
1706 int disable_patch, err;
1707 struct intel_version new_ver;
1709 BT_DBG("%s", hdev->name);
1711 /* fw_patch_num indicates the version of patch the device currently
1712 * have. If there is no patch data in the device, it is always 0x00.
1713 * So, if it is other than 0x00, no need to patch the device again.
1715 if (ver->fw_patch_num) {
1717 "Intel device is already patched. patch num: %02x",
1722 /* Opens the firmware patch file based on the firmware version read
1723 * from the controller. If it fails to open the matching firmware
1724 * patch file, it tries to open the default firmware patch file.
1725 * If no patch file is found, allow the device to operate without
1728 fw = btintel_legacy_rom_get_fw(hdev, ver);
1733 /* Enable the manufacturer mode of the controller.
1734 * Only while this mode is enabled, the driver can download the
1735 * firmware patch data and configuration parameters.
1737 err = btintel_enter_mfg(hdev);
1739 release_firmware(fw);
1745 /* The firmware data file consists of list of Intel specific HCI
1746 * commands and its expected events. The first byte indicates the
1747 * type of the message, either HCI command or HCI event.
1749 * It reads the command and its expected event from the firmware file,
1750 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1751 * the returned event is compared with the event read from the firmware
1752 * file and it will continue until all the messages are downloaded to
1755 * Once the firmware patching is completed successfully,
1756 * the manufacturer mode is disabled with reset and activating the
1759 * If the firmware patching fails, the manufacturer mode is
1760 * disabled with reset and deactivating the patch.
1762 * If the default patch file is used, no reset is done when disabling
1765 while (fw->size > fw_ptr - fw->data) {
1768 ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
1771 goto exit_mfg_deactivate;
1774 release_firmware(fw);
1777 goto exit_mfg_disable;
1779 /* Patching completed successfully and disable the manufacturer mode
1780 * with reset and activate the downloaded firmware patches.
1782 err = btintel_exit_mfg(hdev, true, true);
1786 /* Need build number for downloaded fw patches in
1787 * every power-on boot
1789 err = btintel_read_version(hdev, &new_ver);
1793 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
1794 new_ver.fw_patch_num);
1799 /* Disable the manufacturer mode without reset */
1800 err = btintel_exit_mfg(hdev, false, false);
1804 bt_dev_info(hdev, "Intel firmware patch completed");
1808 exit_mfg_deactivate:
1809 release_firmware(fw);
1811 /* Patching failed. Disable the manufacturer mode with reset and
1812 * deactivate the downloaded firmware patches.
1814 err = btintel_exit_mfg(hdev, true, false);
1818 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1821 /* Set the event mask for Intel specific vendor events. This enables
1822 * a few extra events that are useful during general operation.
1824 btintel_set_event_mask_mfg(hdev, false);
1826 btintel_check_bdaddr(hdev);
1831 static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1833 ktime_t delta, rettime;
1834 unsigned long long duration;
1837 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1839 bt_dev_info(hdev, "Waiting for firmware download to complete");
1841 err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
1843 msecs_to_jiffies(msec));
1844 if (err == -EINTR) {
1845 bt_dev_err(hdev, "Firmware loading interrupted");
1850 bt_dev_err(hdev, "Firmware loading timeout");
1854 if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
1855 bt_dev_err(hdev, "Firmware loading failed");
1859 rettime = ktime_get();
1860 delta = ktime_sub(rettime, calltime);
1861 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1863 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
1868 static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1870 ktime_t delta, rettime;
1871 unsigned long long duration;
1874 bt_dev_info(hdev, "Waiting for device to boot");
1876 err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
1878 msecs_to_jiffies(msec));
1879 if (err == -EINTR) {
1880 bt_dev_err(hdev, "Device boot interrupted");
1885 bt_dev_err(hdev, "Device boot timeout");
1889 rettime = ktime_get();
1890 delta = ktime_sub(rettime, calltime);
1891 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1893 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
1898 static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
1903 calltime = ktime_get();
1905 btintel_set_flag(hdev, INTEL_BOOTING);
1907 err = btintel_send_intel_reset(hdev, boot_addr);
1909 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
1910 btintel_reset_to_bootloader(hdev);
1914 /* The bootloader will not indicate when the device is ready. This
1915 * is done by the operational firmware sending bootup notification.
1917 * Booting into operational firmware should not take longer than
1918 * 1 second. However if that happens, then just fail the setup
1919 * since something went wrong.
1921 err = btintel_boot_wait(hdev, calltime, 1000);
1922 if (err == -ETIMEDOUT)
1923 btintel_reset_to_bootloader(hdev);
1928 static int btintel_get_fw_name(struct intel_version *ver,
1929 struct intel_boot_params *params,
1930 char *fw_name, size_t len,
1933 switch (ver->hw_variant) {
1934 case 0x0b: /* SfP */
1935 case 0x0c: /* WsP */
1936 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
1938 le16_to_cpu(params->dev_revid),
1941 case 0x11: /* JfP */
1942 case 0x12: /* ThP */
1943 case 0x13: /* HrP */
1944 case 0x14: /* CcP */
1945 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
1958 static int btintel_download_fw(struct hci_dev *hdev,
1959 struct intel_version *ver,
1960 struct intel_boot_params *params,
1963 const struct firmware *fw;
1968 if (!ver || !params)
1971 /* The firmware variant determines if the device is in bootloader
1972 * mode or is running operational firmware. The value 0x06 identifies
1973 * the bootloader and the value 0x23 identifies the operational
1976 * When the operational firmware is already present, then only
1977 * the check for valid Bluetooth device address is needed. This
1978 * determines if the device will be added as configured or
1979 * unconfigured controller.
1981 * It is not possible to use the Secure Boot Parameters in this
1982 * case since that command is only available in bootloader mode.
1984 if (ver->fw_variant == 0x23) {
1985 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
1986 btintel_check_bdaddr(hdev);
1988 /* SfP and WsP don't seem to update the firmware version on file
1989 * so version checking is currently possible.
1991 switch (ver->hw_variant) {
1992 case 0x0b: /* SfP */
1993 case 0x0c: /* WsP */
1997 /* Proceed to download to check if the version matches */
2001 /* Read the secure boot parameters to identify the operating
2002 * details of the bootloader.
2004 err = btintel_read_boot_params(hdev, params);
2008 /* It is required that every single firmware fragment is acknowledged
2009 * with a command complete event. If the boot parameters indicate
2010 * that this bootloader does not send them, then abort the setup.
2012 if (params->limited_cce != 0x00) {
2013 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2014 params->limited_cce);
2018 /* If the OTP has no valid Bluetooth device address, then there will
2019 * also be no valid address for the operational firmware.
2021 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2022 bt_dev_info(hdev, "No device address configured");
2023 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2027 /* With this Intel bootloader only the hardware variant and device
2028 * revision information are used to select the right firmware for SfP
2031 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2033 * Currently the supported hardware variants are:
2034 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2035 * 12 (0x0c) for iBT3.5 (WsP)
2037 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2038 * variant, HW revision and FW revision, as these are dependent on CNVi
2039 * and RF Combination.
2041 * 17 (0x11) for iBT3.5 (JfP)
2042 * 18 (0x12) for iBT3.5 (ThP)
2044 * The firmware file name for these will be
2045 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2048 err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
2050 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2051 /* Firmware has already been loaded */
2052 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2056 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2060 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2062 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2063 /* Firmware has already been loaded */
2064 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2068 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2073 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2075 if (fw->size < 644) {
2076 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2082 calltime = ktime_get();
2084 btintel_set_flag(hdev, INTEL_DOWNLOADING);
2086 /* Start firmware downloading and get boot parameter */
2087 err = btintel_download_firmware(hdev, ver, fw, boot_param);
2089 if (err == -EALREADY) {
2090 /* Firmware has already been loaded */
2091 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2096 /* When FW download fails, send Intel Reset to retry
2099 btintel_reset_to_bootloader(hdev);
2103 /* Before switching the device into operational mode and with that
2104 * booting the loaded firmware, wait for the bootloader notification
2105 * that all fragments have been successfully received.
2107 * When the event processing receives the notification, then the
2108 * INTEL_DOWNLOADING flag will be cleared.
2110 * The firmware loading should not take longer than 5 seconds
2111 * and thus just timeout if that happens and fail the setup
2114 err = btintel_download_wait(hdev, calltime, 5000);
2115 if (err == -ETIMEDOUT)
2116 btintel_reset_to_bootloader(hdev);
2119 release_firmware(fw);
2123 static int btintel_bootloader_setup(struct hci_dev *hdev,
2124 struct intel_version *ver)
2126 struct intel_version new_ver;
2127 struct intel_boot_params params;
2132 BT_DBG("%s", hdev->name);
2134 /* Set the default boot parameter to 0x0 and it is updated to
2135 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2136 * command while downloading the firmware.
2138 boot_param = 0x00000000;
2140 btintel_set_flag(hdev, INTEL_BOOTLOADER);
2142 err = btintel_download_fw(hdev, ver, ¶ms, &boot_param);
2146 /* controller is already having an operational firmware */
2147 if (ver->fw_variant == 0x23)
2150 err = btintel_boot(hdev, boot_param);
2154 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2156 err = btintel_get_fw_name(ver, ¶ms, ddcname,
2157 sizeof(ddcname), "ddc");
2160 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2162 /* Once the device is running in operational mode, it needs to
2163 * apply the device configuration (DDC) parameters.
2165 * The device can work without DDC parameters, so even if it
2166 * fails to load the file, no need to fail the setup.
2168 btintel_load_ddc_config(hdev, ddcname);
2171 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2173 /* Read the Intel version information after loading the FW */
2174 err = btintel_read_version(hdev, &new_ver);
2178 btintel_version_info(hdev, &new_ver);
2181 /* Set the event mask for Intel specific vendor events. This enables
2182 * a few extra events that are useful during general operation. It
2183 * does not enable any debugging related events.
2185 * The device will function correctly without these events enabled
2186 * and thus no need to fail the setup.
2188 btintel_set_event_mask(hdev, false);
2193 static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
2194 char *fw_name, size_t len,
2197 /* The firmware file name for new generation controllers will be
2198 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2200 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2201 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2202 INTEL_CNVX_TOP_STEP(ver->cnvi_top)),
2203 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2204 INTEL_CNVX_TOP_STEP(ver->cnvr_top)),
2208 static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
2209 struct intel_version_tlv *ver,
2212 const struct firmware *fw;
2217 if (!ver || !boot_param)
2220 /* The firmware variant determines if the device is in bootloader
2221 * mode or is running operational firmware. The value 0x03 identifies
2222 * the bootloader and the value 0x23 identifies the operational
2225 * When the operational firmware is already present, then only
2226 * the check for valid Bluetooth device address is needed. This
2227 * determines if the device will be added as configured or
2228 * unconfigured controller.
2230 * It is not possible to use the Secure Boot Parameters in this
2231 * case since that command is only available in bootloader mode.
2233 if (ver->img_type == 0x03) {
2234 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2235 btintel_check_bdaddr(hdev);
2238 * Check for valid bd address in boot loader mode. Device
2239 * will be marked as unconfigured if empty bd address is
2242 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2243 bt_dev_info(hdev, "No device address configured");
2244 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2248 btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2249 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2251 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2252 /* Firmware has already been loaded */
2253 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2257 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2263 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2265 if (fw->size < 644) {
2266 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2272 calltime = ktime_get();
2274 btintel_set_flag(hdev, INTEL_DOWNLOADING);
2276 /* Start firmware downloading and get boot parameter */
2277 err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
2278 INTEL_HW_VARIANT(ver->cnvi_bt),
2281 if (err == -EALREADY) {
2282 /* Firmware has already been loaded */
2283 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2288 /* When FW download fails, send Intel Reset to retry
2291 btintel_reset_to_bootloader(hdev);
2295 /* Before switching the device into operational mode and with that
2296 * booting the loaded firmware, wait for the bootloader notification
2297 * that all fragments have been successfully received.
2299 * When the event processing receives the notification, then the
2300 * BTUSB_DOWNLOADING flag will be cleared.
2302 * The firmware loading should not take longer than 5 seconds
2303 * and thus just timeout if that happens and fail the setup
2306 err = btintel_download_wait(hdev, calltime, 5000);
2307 if (err == -ETIMEDOUT)
2308 btintel_reset_to_bootloader(hdev);
2311 release_firmware(fw);
2315 static int btintel_get_codec_config_data(struct hci_dev *hdev,
2316 __u8 link, struct bt_codec *codec,
2317 __u8 *ven_len, __u8 **ven_data)
2321 if (!ven_data || !ven_len)
2327 if (link != ESCO_LINK) {
2328 bt_dev_err(hdev, "Invalid link type(%u)", link);
2332 *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
2338 /* supports only CVSD and mSBC offload codecs */
2339 switch (codec->id) {
2348 bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
2351 /* codec and its capabilities are pre-defined to ids
2352 * preset id = 0x00 represents CVSD codec with sampling rate 8K
2353 * preset id = 0x01 represents mSBC codec with sampling rate 16K
2355 *ven_len = sizeof(__u8);
2364 static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
2366 /* Intel uses 1 as data path id for all the usecases */
2371 static int btintel_configure_offload(struct hci_dev *hdev)
2373 struct sk_buff *skb;
2375 struct intel_offload_use_cases *use_cases;
2377 skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
2379 bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
2381 return PTR_ERR(skb);
2384 if (skb->len < sizeof(*use_cases)) {
2389 use_cases = (void *)skb->data;
2391 if (use_cases->status) {
2392 err = -bt_to_errno(skb->data[0]);
2396 if (use_cases->preset[0] & 0x03) {
2397 hdev->get_data_path_id = btintel_get_data_path_id;
2398 hdev->get_codec_config_data = btintel_get_codec_config_data;
2405 static void btintel_set_ppag(struct hci_dev *hdev, struct intel_version_tlv *ver)
2407 struct btintel_ppag ppag;
2408 struct sk_buff *skb;
2409 struct hci_ppag_enable_cmd ppag_cmd;
2412 /* PPAG is not supported if CRF is HrP2, Jfp2, JfP1 */
2413 switch (ver->cnvr_top & 0xFFF) {
2414 case 0x504: /* Hrp2 */
2415 case 0x202: /* Jfp2 */
2416 case 0x201: /* Jfp1 */
2417 bt_dev_dbg(hdev, "PPAG not supported for Intel CNVr (0x%3x)",
2418 ver->cnvr_top & 0xFFF);
2422 handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2424 bt_dev_info(hdev, "No support for BT device in ACPI firmware");
2428 memset(&ppag, 0, sizeof(ppag));
2431 ppag.status = AE_NOT_FOUND;
2432 acpi_walk_namespace(ACPI_TYPE_PACKAGE, handle, 1, NULL,
2433 btintel_ppag_callback, &ppag, NULL);
2435 if (ACPI_FAILURE(ppag.status)) {
2436 if (ppag.status == AE_NOT_FOUND) {
2437 bt_dev_dbg(hdev, "PPAG-BT: ACPI entry not found");
2443 if (ppag.domain != 0x12) {
2444 bt_dev_dbg(hdev, "PPAG-BT: Bluetooth domain is disabled in ACPI firmware");
2449 * BIT 0 : 0 Disabled in EU
2451 * BIT 1 : 0 Disabled in China
2452 * 1 Enabled in China
2454 if ((ppag.mode & 0x01) != BIT(0) && (ppag.mode & 0x02) != BIT(1)) {
2455 bt_dev_dbg(hdev, "PPAG-BT: EU, China mode are disabled in CB/BIOS");
2459 ppag_cmd.ppag_enable_flags = cpu_to_le32(ppag.mode);
2461 skb = __hci_cmd_sync(hdev, INTEL_OP_PPAG_CMD, sizeof(ppag_cmd), &ppag_cmd, HCI_CMD_TIMEOUT);
2463 bt_dev_warn(hdev, "Failed to send PPAG Enable (%ld)", PTR_ERR(skb));
2466 bt_dev_info(hdev, "PPAG-BT: Enabled (Mode %d)", ppag.mode);
2470 static int btintel_acpi_reset_method(struct hci_dev *hdev)
2474 union acpi_object *p, *ref;
2475 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2477 status = acpi_evaluate_object(ACPI_HANDLE(GET_HCIDEV_DEV(hdev)), "_PRR", NULL, &buffer);
2478 if (ACPI_FAILURE(status)) {
2479 bt_dev_err(hdev, "Failed to run _PRR method");
2485 if (p->package.count != 1 || p->type != ACPI_TYPE_PACKAGE) {
2486 bt_dev_err(hdev, "Invalid arguments");
2491 ref = &p->package.elements[0];
2492 if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) {
2493 bt_dev_err(hdev, "Invalid object type: 0x%x", ref->type);
2498 status = acpi_evaluate_object(ref->reference.handle, "_RST", NULL, NULL);
2499 if (ACPI_FAILURE(status)) {
2500 bt_dev_err(hdev, "Failed to run_RST method");
2506 kfree(buffer.pointer);
2510 static void btintel_set_dsm_reset_method(struct hci_dev *hdev,
2511 struct intel_version_tlv *ver_tlv)
2513 struct btintel_data *data = hci_get_priv(hdev);
2514 acpi_handle handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2515 u8 reset_payload[4] = {0x01, 0x00, 0x01, 0x00};
2516 union acpi_object *obj, argv4;
2518 RESET_TYPE_WDISABLE2,
2522 handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2525 bt_dev_dbg(hdev, "No support for bluetooth device in ACPI firmware");
2529 if (!acpi_has_method(handle, "_PRR")) {
2530 bt_dev_err(hdev, "No support for _PRR ACPI method");
2534 switch (ver_tlv->cnvi_top & 0xfff) {
2535 case 0x910: /* GalePeak2 */
2536 reset_payload[2] = RESET_TYPE_VSEC;
2539 /* WDISABLE2 is the default reset method */
2540 reset_payload[2] = RESET_TYPE_WDISABLE2;
2542 if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2543 BIT(DSM_SET_WDISABLE2_DELAY))) {
2544 bt_dev_err(hdev, "No dsm support to set reset delay");
2547 argv4.integer.type = ACPI_TYPE_INTEGER;
2548 /* delay required to toggle BT power */
2549 argv4.integer.value = 160;
2550 obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2551 DSM_SET_WDISABLE2_DELAY, &argv4);
2553 bt_dev_err(hdev, "Failed to call dsm to set reset delay");
2559 bt_dev_info(hdev, "DSM reset method type: 0x%02x", reset_payload[2]);
2561 if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2562 DSM_SET_RESET_METHOD)) {
2563 bt_dev_warn(hdev, "No support for dsm to set reset method");
2566 argv4.buffer.type = ACPI_TYPE_BUFFER;
2567 argv4.buffer.length = sizeof(reset_payload);
2568 argv4.buffer.pointer = reset_payload;
2570 obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2571 DSM_SET_RESET_METHOD, &argv4);
2573 bt_dev_err(hdev, "Failed to call dsm to set reset method");
2577 data->acpi_reset_method = btintel_acpi_reset_method;
2580 static int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
2581 struct intel_version_tlv *ver)
2586 struct intel_version_tlv new_ver;
2588 bt_dev_dbg(hdev, "");
2590 /* Set the default boot parameter to 0x0 and it is updated to
2591 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2592 * command while downloading the firmware.
2594 boot_param = 0x00000000;
2596 btintel_set_flag(hdev, INTEL_BOOTLOADER);
2598 err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2602 /* check if controller is already having an operational firmware */
2603 if (ver->img_type == 0x03)
2606 err = btintel_boot(hdev, boot_param);
2610 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2612 btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
2613 /* Once the device is running in operational mode, it needs to
2614 * apply the device configuration (DDC) parameters.
2616 * The device can work without DDC parameters, so even if it
2617 * fails to load the file, no need to fail the setup.
2619 btintel_load_ddc_config(hdev, ddcname);
2621 /* Read supported use cases and set callbacks to fetch datapath id */
2622 btintel_configure_offload(hdev);
2624 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2626 /* Set PPAG feature */
2627 btintel_set_ppag(hdev, ver);
2629 /* Read the Intel version information after loading the FW */
2630 err = btintel_read_version_tlv(hdev, &new_ver);
2634 btintel_version_info_tlv(hdev, &new_ver);
2637 /* Set the event mask for Intel specific vendor events. This enables
2638 * a few extra events that are useful during general operation. It
2639 * does not enable any debugging related events.
2641 * The device will function correctly without these events enabled
2642 * and thus no need to fail the setup.
2644 btintel_set_event_mask(hdev, false);
2649 static void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
2651 switch (hw_variant) {
2652 /* Legacy bootloader devices that supports MSFT Extension */
2653 case 0x11: /* JfP */
2654 case 0x12: /* ThP */
2655 case 0x13: /* HrP */
2656 case 0x14: /* CcP */
2657 /* All Intel new genration controllers support the Microsoft vendor
2658 * extension are using 0xFC1E for VsMsftOpCode.
2665 hci_set_msft_opcode(hdev, 0xFC1E);
2673 static void btintel_print_fseq_info(struct hci_dev *hdev)
2675 struct sk_buff *skb;
2680 skb = __hci_cmd_sync(hdev, 0xfcb3, 0, NULL, HCI_CMD_TIMEOUT);
2682 bt_dev_dbg(hdev, "Reading fseq status command failed (%ld)",
2687 if (skb->len < (sizeof(u32) * 16 + 2)) {
2688 bt_dev_dbg(hdev, "Malformed packet of length %u received",
2694 p = skb_pull_data(skb, 1);
2696 bt_dev_dbg(hdev, "Failed to get fseq status (0x%2.2x)", *p);
2701 p = skb_pull_data(skb, 1);
2707 str = "Fatal error";
2710 str = "Semaphore acquire error";
2713 str = "Unknown error";
2718 bt_dev_err(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
2723 bt_dev_info(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
2725 val = get_unaligned_le32(skb_pull_data(skb, 4));
2726 bt_dev_dbg(hdev, "Reason: 0x%8.8x", val);
2728 val = get_unaligned_le32(skb_pull_data(skb, 4));
2729 bt_dev_dbg(hdev, "Global version: 0x%8.8x", val);
2731 val = get_unaligned_le32(skb_pull_data(skb, 4));
2732 bt_dev_dbg(hdev, "Installed version: 0x%8.8x", val);
2735 skb_pull_data(skb, 4);
2736 bt_dev_info(hdev, "Fseq executed: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
2740 skb_pull_data(skb, 4);
2741 bt_dev_info(hdev, "Fseq BT Top: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
2744 val = get_unaligned_le32(skb_pull_data(skb, 4));
2745 bt_dev_dbg(hdev, "Fseq Top init version: 0x%8.8x", val);
2747 val = get_unaligned_le32(skb_pull_data(skb, 4));
2748 bt_dev_dbg(hdev, "Fseq Cnvio init version: 0x%8.8x", val);
2750 val = get_unaligned_le32(skb_pull_data(skb, 4));
2751 bt_dev_dbg(hdev, "Fseq MBX Wifi file version: 0x%8.8x", val);
2753 val = get_unaligned_le32(skb_pull_data(skb, 4));
2754 bt_dev_dbg(hdev, "Fseq BT version: 0x%8.8x", val);
2756 val = get_unaligned_le32(skb_pull_data(skb, 4));
2757 bt_dev_dbg(hdev, "Fseq Top reset address: 0x%8.8x", val);
2759 val = get_unaligned_le32(skb_pull_data(skb, 4));
2760 bt_dev_dbg(hdev, "Fseq MBX timeout: 0x%8.8x", val);
2762 val = get_unaligned_le32(skb_pull_data(skb, 4));
2763 bt_dev_dbg(hdev, "Fseq MBX ack: 0x%8.8x", val);
2765 val = get_unaligned_le32(skb_pull_data(skb, 4));
2766 bt_dev_dbg(hdev, "Fseq CNVi id: 0x%8.8x", val);
2768 val = get_unaligned_le32(skb_pull_data(skb, 4));
2769 bt_dev_dbg(hdev, "Fseq CNVr id: 0x%8.8x", val);
2771 val = get_unaligned_le32(skb_pull_data(skb, 4));
2772 bt_dev_dbg(hdev, "Fseq Error handle: 0x%8.8x", val);
2774 val = get_unaligned_le32(skb_pull_data(skb, 4));
2775 bt_dev_dbg(hdev, "Fseq Magic noalive indication: 0x%8.8x", val);
2777 val = get_unaligned_le32(skb_pull_data(skb, 4));
2778 bt_dev_dbg(hdev, "Fseq OTP version: 0x%8.8x", val);
2780 val = get_unaligned_le32(skb_pull_data(skb, 4));
2781 bt_dev_dbg(hdev, "Fseq MBX otp version: 0x%8.8x", val);
2786 static int btintel_setup_combined(struct hci_dev *hdev)
2788 const u8 param[1] = { 0xFF };
2789 struct intel_version ver;
2790 struct intel_version_tlv ver_tlv;
2791 struct sk_buff *skb;
2794 BT_DBG("%s", hdev->name);
2796 /* The some controllers have a bug with the first HCI command sent to it
2797 * returning number of completed commands as zero. This would stall the
2798 * command processing in the Bluetooth core.
2800 * As a workaround, send HCI Reset command first which will reset the
2801 * number of completed commands and allow normal command processing
2804 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
2805 * in the SW_RFKILL ON state as a workaround of fixing LED issue during
2806 * the shutdown() procedure, and once the device is in SW_RFKILL ON
2807 * state, the only way to exit out of it is sending the HCI_Reset
2810 if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
2811 btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2812 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
2816 "sending initial HCI reset failed (%ld)",
2818 return PTR_ERR(skb);
2823 /* Starting from TyP device, the command parameter and response are
2824 * changed even though the OCF for HCI_Intel_Read_Version command
2825 * remains same. The legacy devices can handle even if the
2826 * command has a parameter and returns a correct version information.
2827 * So, it uses new format to support both legacy and new format.
2829 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
2831 bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
2833 return PTR_ERR(skb);
2836 /* Check the status */
2838 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
2844 /* Apply the common HCI quirks for Intel device */
2845 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2846 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2847 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2849 /* Set up the quality report callback for Intel devices */
2850 hdev->set_quality_report = btintel_set_quality_report;
2852 /* For Legacy device, check the HW platform value and size */
2853 if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
2854 bt_dev_dbg(hdev, "Read the legacy Intel version information");
2856 memcpy(&ver, skb->data, sizeof(ver));
2858 /* Display version information */
2859 btintel_version_info(hdev, &ver);
2861 /* Check for supported iBT hardware variants of this firmware
2864 * This check has been put in place to ensure correct forward
2865 * compatibility options when newer hardware variants come
2868 switch (ver.hw_variant) {
2870 case 0x08: /* StP */
2871 /* Legacy ROM product */
2872 btintel_set_flag(hdev, INTEL_ROM_LEGACY);
2874 /* Apply the device specific HCI quirks
2876 * WBS for SdP - For the Legacy ROM products, only SdP
2877 * supports the WBS. But the version information is not
2878 * enough to use here because the StP2 and SdP have same
2879 * hw_variant and fw_variant. So, this flag is set by
2880 * the transport driver (btusb) based on the HW info
2883 if (!btintel_test_flag(hdev,
2884 INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
2885 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2887 if (ver.hw_variant == 0x08 && ver.fw_variant == 0x22)
2888 set_bit(HCI_QUIRK_VALID_LE_STATES,
2891 err = btintel_legacy_rom_setup(hdev, &ver);
2893 case 0x0b: /* SfP */
2894 case 0x11: /* JfP */
2895 case 0x12: /* ThP */
2896 case 0x13: /* HrP */
2897 case 0x14: /* CcP */
2898 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2900 case 0x0c: /* WsP */
2901 /* Apply the device specific HCI quirks
2903 * All Legacy bootloader devices support WBS
2905 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2908 /* These variants don't seem to support LE Coded PHY */
2909 set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
2911 /* Setup MSFT Extension support */
2912 btintel_set_msft_opcode(hdev, ver.hw_variant);
2914 err = btintel_bootloader_setup(hdev, &ver);
2915 btintel_register_devcoredump_support(hdev);
2918 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2926 /* memset ver_tlv to start with clean state as few fields are exclusive
2927 * to bootloader mode and are not populated in operational mode
2929 memset(&ver_tlv, 0, sizeof(ver_tlv));
2930 /* For TLV type device, parse the tlv data */
2931 err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
2933 bt_dev_err(hdev, "Failed to parse TLV version information");
2937 if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
2938 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2939 INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
2944 /* Check for supported iBT hardware variants of this firmware
2947 * This check has been put in place to ensure correct forward
2948 * compatibility options when newer hardware variants come
2951 switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
2952 case 0x11: /* JfP */
2953 case 0x12: /* ThP */
2954 case 0x13: /* HrP */
2955 case 0x14: /* CcP */
2956 /* Some legacy bootloader devices starting from JfP,
2957 * the operational firmware supports both old and TLV based
2958 * HCI_Intel_Read_Version command based on the command
2961 * For upgrading firmware case, the TLV based version cannot
2962 * be used because the firmware filename for legacy bootloader
2963 * is based on the old format.
2965 * Also, it is not easy to convert TLV based version from the
2966 * legacy version format.
2968 * So, as a workaround for those devices, use the legacy
2969 * HCI_Intel_Read_Version to get the version information and
2970 * run the legacy bootloader setup.
2972 err = btintel_read_version(hdev, &ver);
2976 /* Apply the device specific HCI quirks
2978 * All Legacy bootloader devices support WBS
2980 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2982 /* These variants don't seem to support LE Coded PHY */
2983 set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
2985 /* Set Valid LE States quirk */
2986 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2988 /* Setup MSFT Extension support */
2989 btintel_set_msft_opcode(hdev, ver.hw_variant);
2991 err = btintel_bootloader_setup(hdev, &ver);
2992 btintel_register_devcoredump_support(hdev);
2999 /* Display version information of TLV type */
3000 btintel_version_info_tlv(hdev, &ver_tlv);
3002 /* Apply the device specific HCI quirks for TLV based devices
3004 * All TLV based devices support WBS
3006 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3008 /* Apply LE States quirk from solar onwards */
3009 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3011 /* Setup MSFT Extension support */
3012 btintel_set_msft_opcode(hdev,
3013 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3014 btintel_set_dsm_reset_method(hdev, &ver_tlv);
3016 err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
3017 btintel_register_devcoredump_support(hdev);
3018 btintel_print_fseq_info(hdev);
3021 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
3022 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3033 static int btintel_shutdown_combined(struct hci_dev *hdev)
3035 struct sk_buff *skb;
3038 /* Send HCI Reset to the controller to stop any BT activity which
3039 * were triggered. This will help to save power and maintain the
3040 * sync b/w Host and controller
3042 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3044 bt_dev_err(hdev, "HCI reset during shutdown failed");
3045 return PTR_ERR(skb);
3050 /* Some platforms have an issue with BT LED when the interface is
3051 * down or BT radio is turned off, which takes 5 seconds to BT LED
3052 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
3053 * device in the RFKILL ON state which turns off the BT LED immediately.
3055 if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
3056 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3059 bt_dev_err(hdev, "turning off Intel device LED failed");
3068 int btintel_configure_setup(struct hci_dev *hdev, const char *driver_name)
3070 hdev->manufacturer = 2;
3071 hdev->setup = btintel_setup_combined;
3072 hdev->shutdown = btintel_shutdown_combined;
3073 hdev->hw_error = btintel_hw_error;
3074 hdev->set_diag = btintel_set_diag_combined;
3075 hdev->set_bdaddr = btintel_set_bdaddr;
3077 coredump_info.driver_name = driver_name;
3081 EXPORT_SYMBOL_GPL(btintel_configure_setup);
3083 static int btintel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb)
3085 struct intel_tlv *tlv = (void *)&skb->data[5];
3087 /* The first event is always an event type TLV */
3088 if (tlv->type != INTEL_TLV_TYPE_ID)
3091 switch (tlv->val[0]) {
3092 case INTEL_TLV_SYSTEM_EXCEPTION:
3093 case INTEL_TLV_FATAL_EXCEPTION:
3094 case INTEL_TLV_DEBUG_EXCEPTION:
3095 case INTEL_TLV_TEST_EXCEPTION:
3096 /* Generate devcoredump from exception */
3097 if (!hci_devcd_init(hdev, skb->len)) {
3098 hci_devcd_append(hdev, skb);
3099 hci_devcd_complete(hdev);
3101 bt_dev_err(hdev, "Failed to generate devcoredump");
3106 bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]);
3110 return hci_recv_frame(hdev, skb);
3113 int btintel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
3115 struct hci_event_hdr *hdr = (void *)skb->data;
3116 const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
3118 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
3120 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
3121 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
3123 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
3124 switch (skb->data[2]) {
3126 /* When switching to the operational firmware
3127 * the device sends a vendor specific event
3128 * indicating that the bootup completed.
3130 btintel_bootup(hdev, ptr, len);
3133 /* When the firmware loading completes the
3134 * device sends out a vendor specific event
3135 * indicating the result of the firmware
3138 btintel_secure_send_result(hdev, ptr, len);
3143 /* Handle all diagnostics events separately. May still call
3146 if (len >= sizeof(diagnostics_hdr) &&
3147 memcmp(&skb->data[2], diagnostics_hdr,
3148 sizeof(diagnostics_hdr)) == 0) {
3149 return btintel_diagnostics(hdev, skb);
3153 return hci_recv_frame(hdev, skb);
3155 EXPORT_SYMBOL_GPL(btintel_recv_event);
3157 void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
3159 const struct intel_bootup *evt = ptr;
3161 if (len != sizeof(*evt))
3164 if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
3165 btintel_wake_up_flag(hdev, INTEL_BOOTING);
3167 EXPORT_SYMBOL_GPL(btintel_bootup);
3169 void btintel_secure_send_result(struct hci_dev *hdev,
3170 const void *ptr, unsigned int len)
3172 const struct intel_secure_send_result *evt = ptr;
3174 if (len != sizeof(*evt))
3178 btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
3180 if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
3181 btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
3182 btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
3184 EXPORT_SYMBOL_GPL(btintel_secure_send_result);
3186 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3187 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
3188 MODULE_VERSION(VERSION);
3189 MODULE_LICENSE("GPL");
3190 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
3191 MODULE_FIRMWARE("intel/ibt-11-5.ddc");
3192 MODULE_FIRMWARE("intel/ibt-12-16.sfi");
3193 MODULE_FIRMWARE("intel/ibt-12-16.ddc");