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->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);
542 static int btintel_parse_version_tlv(struct hci_dev *hdev,
543 struct intel_version_tlv *version,
546 /* Consume Command Complete Status field */
549 /* Event parameters contatin multiple TLVs. Read each of them
550 * and only keep the required data. Also, it use existing legacy
551 * version field like hw_platform, hw_variant, and fw_variant
552 * to keep the existing setup flow
555 struct intel_tlv *tlv;
557 /* Make sure skb has a minimum length of the header */
558 if (skb->len < sizeof(*tlv))
561 tlv = (struct intel_tlv *)skb->data;
563 /* Make sure skb has a enough data */
564 if (skb->len < tlv->len + sizeof(*tlv))
568 case INTEL_TLV_CNVI_TOP:
569 version->cnvi_top = get_unaligned_le32(tlv->val);
571 case INTEL_TLV_CNVR_TOP:
572 version->cnvr_top = get_unaligned_le32(tlv->val);
574 case INTEL_TLV_CNVI_BT:
575 version->cnvi_bt = get_unaligned_le32(tlv->val);
577 case INTEL_TLV_CNVR_BT:
578 version->cnvr_bt = get_unaligned_le32(tlv->val);
580 case INTEL_TLV_DEV_REV_ID:
581 version->dev_rev_id = get_unaligned_le16(tlv->val);
583 case INTEL_TLV_IMAGE_TYPE:
584 version->img_type = tlv->val[0];
586 case INTEL_TLV_TIME_STAMP:
587 /* If image type is Operational firmware (0x03), then
588 * running FW Calendar Week and Year information can
589 * be extracted from Timestamp information
591 version->min_fw_build_cw = tlv->val[0];
592 version->min_fw_build_yy = tlv->val[1];
593 version->timestamp = get_unaligned_le16(tlv->val);
595 case INTEL_TLV_BUILD_TYPE:
596 version->build_type = tlv->val[0];
598 case INTEL_TLV_BUILD_NUM:
599 /* If image type is Operational firmware (0x03), then
600 * running FW build number can be extracted from the
603 version->min_fw_build_nn = tlv->val[0];
604 version->build_num = get_unaligned_le32(tlv->val);
606 case INTEL_TLV_SECURE_BOOT:
607 version->secure_boot = tlv->val[0];
609 case INTEL_TLV_OTP_LOCK:
610 version->otp_lock = tlv->val[0];
612 case INTEL_TLV_API_LOCK:
613 version->api_lock = tlv->val[0];
615 case INTEL_TLV_DEBUG_LOCK:
616 version->debug_lock = tlv->val[0];
618 case INTEL_TLV_MIN_FW:
619 version->min_fw_build_nn = tlv->val[0];
620 version->min_fw_build_cw = tlv->val[1];
621 version->min_fw_build_yy = tlv->val[2];
623 case INTEL_TLV_LIMITED_CCE:
624 version->limited_cce = tlv->val[0];
626 case INTEL_TLV_SBE_TYPE:
627 version->sbe_type = tlv->val[0];
629 case INTEL_TLV_OTP_BDADDR:
630 memcpy(&version->otp_bd_addr, tlv->val,
634 /* Ignore rest of information */
637 /* consume the current tlv and move to next*/
638 skb_pull(skb, tlv->len + sizeof(*tlv));
644 static int btintel_read_version_tlv(struct hci_dev *hdev,
645 struct intel_version_tlv *version)
648 const u8 param[1] = { 0xFF };
653 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
655 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
661 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
667 btintel_parse_version_tlv(hdev, version, skb);
673 /* ------- REGMAP IBT SUPPORT ------- */
675 #define IBT_REG_MODE_8BIT 0x00
676 #define IBT_REG_MODE_16BIT 0x01
677 #define IBT_REG_MODE_32BIT 0x02
679 struct regmap_ibt_context {
680 struct hci_dev *hdev;
685 struct ibt_cp_reg_access {
692 struct ibt_rp_reg_access {
698 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
699 void *val, size_t val_size)
701 struct regmap_ibt_context *ctx = context;
702 struct ibt_cp_reg_access cp;
703 struct ibt_rp_reg_access *rp;
707 if (reg_size != sizeof(__le32))
712 cp.mode = IBT_REG_MODE_8BIT;
715 cp.mode = IBT_REG_MODE_16BIT;
718 cp.mode = IBT_REG_MODE_32BIT;
724 /* regmap provides a little-endian formatted addr */
725 cp.addr = *(__le32 *)addr;
728 bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
730 skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
734 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
735 le32_to_cpu(cp.addr), err);
739 if (skb->len != sizeof(*rp) + val_size) {
740 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
741 le32_to_cpu(cp.addr));
746 rp = (struct ibt_rp_reg_access *)skb->data;
748 if (rp->addr != cp.addr) {
749 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
750 le32_to_cpu(rp->addr));
755 memcpy(val, rp->data, val_size);
762 static int regmap_ibt_gather_write(void *context,
763 const void *addr, size_t reg_size,
764 const void *val, size_t val_size)
766 struct regmap_ibt_context *ctx = context;
767 struct ibt_cp_reg_access *cp;
769 int plen = sizeof(*cp) + val_size;
773 if (reg_size != sizeof(__le32))
778 mode = IBT_REG_MODE_8BIT;
781 mode = IBT_REG_MODE_16BIT;
784 mode = IBT_REG_MODE_32BIT;
790 cp = kmalloc(plen, GFP_KERNEL);
794 /* regmap provides a little-endian formatted addr/value */
795 cp->addr = *(__le32 *)addr;
798 memcpy(&cp->data, val, val_size);
800 bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
802 skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
805 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
806 le32_to_cpu(cp->addr), err);
816 static int regmap_ibt_write(void *context, const void *data, size_t count)
818 /* data contains register+value, since we only support 32bit addr,
819 * minimum data size is 4 bytes.
821 if (WARN_ONCE(count < 4, "Invalid register access"))
824 return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
827 static void regmap_ibt_free_context(void *context)
832 static const struct regmap_bus regmap_ibt = {
833 .read = regmap_ibt_read,
834 .write = regmap_ibt_write,
835 .gather_write = regmap_ibt_gather_write,
836 .free_context = regmap_ibt_free_context,
837 .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
838 .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
841 /* Config is the same for all register regions */
842 static const struct regmap_config regmap_ibt_cfg = {
843 .name = "btintel_regmap",
848 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
851 struct regmap_ibt_context *ctx;
853 bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
856 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
858 return ERR_PTR(-ENOMEM);
860 ctx->op_read = opcode_read;
861 ctx->op_write = opcode_write;
864 return regmap_init(&hdev->dev, ®map_ibt, ctx, ®map_ibt_cfg);
866 EXPORT_SYMBOL_GPL(btintel_regmap_init);
868 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
870 struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
873 params.boot_param = cpu_to_le32(boot_param);
875 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), ¶ms,
878 bt_dev_err(hdev, "Failed to send Intel Reset command");
886 EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
888 int btintel_read_boot_params(struct hci_dev *hdev,
889 struct intel_boot_params *params)
893 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
895 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
900 if (skb->len != sizeof(*params)) {
901 bt_dev_err(hdev, "Intel boot parameters size mismatch");
906 memcpy(params, skb->data, sizeof(*params));
910 if (params->status) {
911 bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
913 return -bt_to_errno(params->status);
916 bt_dev_info(hdev, "Device revision is %u",
917 le16_to_cpu(params->dev_revid));
919 bt_dev_info(hdev, "Secure boot is %s",
920 params->secure_boot ? "enabled" : "disabled");
922 bt_dev_info(hdev, "OTP lock is %s",
923 params->otp_lock ? "enabled" : "disabled");
925 bt_dev_info(hdev, "API lock is %s",
926 params->api_lock ? "enabled" : "disabled");
928 bt_dev_info(hdev, "Debug lock is %s",
929 params->debug_lock ? "enabled" : "disabled");
931 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
932 params->min_fw_build_nn, params->min_fw_build_cw,
933 2000 + params->min_fw_build_yy);
937 EXPORT_SYMBOL_GPL(btintel_read_boot_params);
939 static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
940 const struct firmware *fw)
944 /* Start the firmware download transaction with the Init fragment
945 * represented by the 128 bytes of CSS header.
947 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
949 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
953 /* Send the 256 bytes of public key information from the firmware
954 * as the PKey fragment.
956 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
958 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
962 /* Send the 256 bytes of signature information from the firmware
963 * as the Sign fragment.
965 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
967 bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
975 static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
976 const struct firmware *fw)
980 /* Start the firmware download transaction with the Init fragment
981 * represented by the 128 bytes of CSS header.
983 err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
985 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
989 /* Send the 96 bytes of public key information from the firmware
990 * as the PKey fragment.
992 err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
994 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
998 /* Send the 96 bytes of signature information from the firmware
999 * as the Sign fragment
1001 err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
1003 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
1010 static int btintel_download_firmware_payload(struct hci_dev *hdev,
1011 const struct firmware *fw,
1018 fw_ptr = fw->data + offset;
1022 while (fw_ptr - fw->data < fw->size) {
1023 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
1025 frag_len += sizeof(*cmd) + cmd->plen;
1027 /* The parameter length of the secure send command requires
1028 * a 4 byte alignment. It happens so that the firmware file
1029 * contains proper Intel_NOP commands to align the fragments
1032 * Send set of commands with 4 byte alignment from the
1033 * firmware data buffer as a single Data fragement.
1035 if (!(frag_len % 4)) {
1036 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1039 "Failed to send firmware data (%d)",
1053 static bool btintel_firmware_version(struct hci_dev *hdev,
1054 u8 num, u8 ww, u8 yy,
1055 const struct firmware *fw,
1062 while (fw_ptr - fw->data < fw->size) {
1063 struct hci_command_hdr *cmd = (void *)(fw_ptr);
1065 /* Each SKU has a different reset parameter to use in the
1066 * HCI_Intel_Reset command and it is embedded in the firmware
1067 * data. So, instead of using static value per SKU, check
1068 * the firmware data and save it for later use.
1070 if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1071 struct cmd_write_boot_params *params;
1073 params = (void *)(fw_ptr + sizeof(*cmd));
1075 *boot_addr = le32_to_cpu(params->boot_addr);
1077 bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
1079 bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1080 params->fw_build_num, params->fw_build_ww,
1081 params->fw_build_yy);
1083 return (num == params->fw_build_num &&
1084 ww == params->fw_build_ww &&
1085 yy == params->fw_build_yy);
1088 fw_ptr += sizeof(*cmd) + cmd->plen;
1094 int btintel_download_firmware(struct hci_dev *hdev,
1095 struct intel_version *ver,
1096 const struct firmware *fw,
1101 /* SfP and WsP don't seem to update the firmware version on file
1102 * so version checking is currently not possible.
1104 switch (ver->hw_variant) {
1105 case 0x0b: /* SfP */
1106 case 0x0c: /* WsP */
1107 /* Skip version checking */
1111 /* Skip download if firmware has the same version */
1112 if (btintel_firmware_version(hdev, ver->fw_build_num,
1113 ver->fw_build_ww, ver->fw_build_yy,
1115 bt_dev_info(hdev, "Firmware already loaded");
1116 /* Return -EALREADY to indicate that the firmware has
1117 * already been loaded.
1123 /* The firmware variant determines if the device is in bootloader
1124 * mode or is running operational firmware. The value 0x06 identifies
1125 * the bootloader and the value 0x23 identifies the operational
1128 * If the firmware version has changed that means it needs to be reset
1129 * to bootloader when operational so the new firmware can be loaded.
1131 if (ver->fw_variant == 0x23)
1134 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1138 return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1140 EXPORT_SYMBOL_GPL(btintel_download_firmware);
1142 static int btintel_download_fw_tlv(struct hci_dev *hdev,
1143 struct intel_version_tlv *ver,
1144 const struct firmware *fw, u32 *boot_param,
1145 u8 hw_variant, u8 sbe_type)
1150 /* Skip download if firmware has the same version */
1151 if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1152 ver->min_fw_build_cw,
1153 ver->min_fw_build_yy,
1155 bt_dev_info(hdev, "Firmware already loaded");
1156 /* Return -EALREADY to indicate that firmware has
1157 * already been loaded.
1162 /* The firmware variant determines if the device is in bootloader
1163 * mode or is running operational firmware. The value 0x01 identifies
1164 * the bootloader and the value 0x03 identifies the operational
1167 * If the firmware version has changed that means it needs to be reset
1168 * to bootloader when operational so the new firmware can be loaded.
1170 if (ver->img_type == 0x03)
1173 /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1174 * only RSA secure boot engine. Hence, the corresponding sfi file will
1175 * have RSA header of 644 bytes followed by Command Buffer.
1177 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1178 * secure boot engine. As a result, the corresponding sfi file will
1179 * have RSA header of 644, ECDSA header of 320 bytes followed by
1182 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1183 * version: RSA(0x00010000) , ECDSA (0x00020000)
1185 css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1186 if (css_header_ver != 0x00010000) {
1187 bt_dev_err(hdev, "Invalid CSS Header version");
1191 if (hw_variant <= 0x14) {
1192 if (sbe_type != 0x00) {
1193 bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1198 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1202 err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1205 } else if (hw_variant >= 0x17) {
1206 /* Check if CSS header for ECDSA follows the RSA header */
1207 if (fw->data[ECDSA_OFFSET] != 0x06)
1210 /* Check if the CSS Header version is ECDSA(0x00020000) */
1211 css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1212 if (css_header_ver != 0x00020000) {
1213 bt_dev_err(hdev, "Invalid CSS Header version");
1217 if (sbe_type == 0x00) {
1218 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1222 err = btintel_download_firmware_payload(hdev, fw,
1223 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1226 } else if (sbe_type == 0x01) {
1227 err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1231 err = btintel_download_firmware_payload(hdev, fw,
1232 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1240 static void btintel_reset_to_bootloader(struct hci_dev *hdev)
1242 struct intel_reset params;
1243 struct sk_buff *skb;
1245 /* Send Intel Reset command. This will result in
1246 * re-enumeration of BT controller.
1248 * Intel Reset parameter description:
1249 * reset_type : 0x00 (Soft reset),
1251 * patch_enable : 0x00 (Do not enable),
1253 * ddc_reload : 0x00 (Do not reload),
1255 * boot_option: 0x00 (Current image),
1256 * 0x01 (Specified boot address)
1257 * boot_param: Boot address
1260 params.reset_type = 0x01;
1261 params.patch_enable = 0x01;
1262 params.ddc_reload = 0x01;
1263 params.boot_option = 0x00;
1264 params.boot_param = cpu_to_le32(0x00000000);
1266 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1267 ¶ms, HCI_INIT_TIMEOUT);
1269 bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1273 bt_dev_info(hdev, "Intel reset sent to retry FW download");
1276 /* Current Intel BT controllers(ThP/JfP) hold the USB reset
1277 * lines for 2ms when it receives Intel Reset in bootloader mode.
1278 * Whereas, the upcoming Intel BT controllers will hold USB reset
1279 * for 150ms. To keep the delay generic, 150ms is chosen here.
1284 static int btintel_read_debug_features(struct hci_dev *hdev,
1285 struct intel_debug_features *features)
1287 struct sk_buff *skb;
1290 /* Intel controller supports two pages, each page is of 128-bit
1291 * feature bit mask. And each bit defines specific feature support
1293 skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1296 bt_dev_err(hdev, "Reading supported features failed (%ld)",
1298 return PTR_ERR(skb);
1301 if (skb->len != (sizeof(features->page1) + 3)) {
1302 bt_dev_err(hdev, "Supported features event size mismatch");
1307 memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1309 /* Read the supported features page2 if required in future.
1315 static acpi_status btintel_ppag_callback(acpi_handle handle, u32 lvl, void *data,
1320 struct btintel_ppag *ppag = data;
1321 union acpi_object *p, *elements;
1322 struct acpi_buffer string = {ACPI_ALLOCATE_BUFFER, NULL};
1323 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1324 struct hci_dev *hdev = ppag->hdev;
1326 status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1327 if (ACPI_FAILURE(status)) {
1328 bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
1332 len = strlen(string.pointer);
1333 if (len < strlen(BTINTEL_PPAG_NAME)) {
1334 kfree(string.pointer);
1338 if (strncmp((char *)string.pointer + len - 4, BTINTEL_PPAG_NAME, 4)) {
1339 kfree(string.pointer);
1342 kfree(string.pointer);
1344 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
1345 if (ACPI_FAILURE(status)) {
1346 ppag->status = status;
1347 bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
1352 ppag = (struct btintel_ppag *)data;
1354 if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 2) {
1355 kfree(buffer.pointer);
1356 bt_dev_warn(hdev, "PPAG-BT: Invalid object type: %d or package count: %d",
1357 p->type, p->package.count);
1358 ppag->status = AE_ERROR;
1362 elements = p->package.elements;
1364 /* PPAG table is located at element[1] */
1367 ppag->domain = (u32)p->package.elements[0].integer.value;
1368 ppag->mode = (u32)p->package.elements[1].integer.value;
1369 ppag->status = AE_OK;
1370 kfree(buffer.pointer);
1371 return AE_CTRL_TERMINATE;
1374 static int btintel_set_debug_features(struct hci_dev *hdev,
1375 const struct intel_debug_features *features)
1377 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
1379 u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
1380 u8 trace_enable = 0x02;
1381 struct sk_buff *skb;
1384 bt_dev_warn(hdev, "Debug features not read");
1388 if (!(features->page1[0] & 0x3f)) {
1389 bt_dev_info(hdev, "Telemetry exception format not supported");
1393 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1395 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1397 return PTR_ERR(skb);
1401 skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
1403 bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
1405 return PTR_ERR(skb);
1409 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1411 bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
1413 return PTR_ERR(skb);
1417 bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
1418 trace_enable, mask[3]);
1423 static int btintel_reset_debug_features(struct hci_dev *hdev,
1424 const struct intel_debug_features *features)
1426 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
1428 u8 trace_enable = 0x00;
1429 struct sk_buff *skb;
1432 bt_dev_warn(hdev, "Debug features not read");
1436 if (!(features->page1[0] & 0x3f)) {
1437 bt_dev_info(hdev, "Telemetry exception format not supported");
1441 /* Should stop the trace before writing ddc event mask. */
1442 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1444 bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
1446 return PTR_ERR(skb);
1450 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1452 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1454 return PTR_ERR(skb);
1458 bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
1459 trace_enable, mask[3]);
1464 int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
1466 struct intel_debug_features features;
1469 bt_dev_dbg(hdev, "enable %d", enable);
1471 /* Read the Intel supported features and if new exception formats
1472 * supported, need to load the additional DDC config to enable.
1474 err = btintel_read_debug_features(hdev, &features);
1478 /* Set or reset the debug features. */
1480 err = btintel_set_debug_features(hdev, &features);
1482 err = btintel_reset_debug_features(hdev, &features);
1486 EXPORT_SYMBOL_GPL(btintel_set_quality_report);
1488 static void btintel_coredump(struct hci_dev *hdev)
1490 struct sk_buff *skb;
1492 skb = __hci_cmd_sync(hdev, 0xfc4e, 0, NULL, HCI_CMD_TIMEOUT);
1494 bt_dev_err(hdev, "Coredump failed (%ld)", PTR_ERR(skb));
1501 static void btintel_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
1505 snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
1506 coredump_info.hw_variant);
1507 skb_put_data(skb, buf, strlen(buf));
1509 snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
1510 coredump_info.fw_build_num);
1511 skb_put_data(skb, buf, strlen(buf));
1513 snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info.driver_name);
1514 skb_put_data(skb, buf, strlen(buf));
1516 snprintf(buf, sizeof(buf), "Vendor: Intel\n");
1517 skb_put_data(skb, buf, strlen(buf));
1520 static int btintel_register_devcoredump_support(struct hci_dev *hdev)
1522 struct intel_debug_features features;
1525 err = btintel_read_debug_features(hdev, &features);
1527 bt_dev_info(hdev, "Error reading debug features");
1531 if (!(features.page1[0] & 0x3f)) {
1532 bt_dev_dbg(hdev, "Telemetry exception format not supported");
1536 hci_devcd_register(hdev, btintel_coredump, btintel_dmp_hdr, NULL);
1541 static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
1542 struct intel_version *ver)
1544 const struct firmware *fw;
1548 snprintf(fwname, sizeof(fwname),
1549 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1550 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1551 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1552 ver->fw_build_ww, ver->fw_build_yy);
1554 ret = request_firmware(&fw, fwname, &hdev->dev);
1556 if (ret == -EINVAL) {
1557 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1562 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1565 /* If the correct firmware patch file is not found, use the
1566 * default firmware patch file instead
1568 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1569 ver->hw_platform, ver->hw_variant);
1570 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1571 bt_dev_err(hdev, "failed to open default fw file: %s",
1577 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1582 static int btintel_legacy_rom_patching(struct hci_dev *hdev,
1583 const struct firmware *fw,
1584 const u8 **fw_ptr, int *disable_patch)
1586 struct sk_buff *skb;
1587 struct hci_command_hdr *cmd;
1588 const u8 *cmd_param;
1589 struct hci_event_hdr *evt = NULL;
1590 const u8 *evt_param = NULL;
1591 int remain = fw->size - (*fw_ptr - fw->data);
1593 /* The first byte indicates the types of the patch command or event.
1594 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1595 * in the current firmware buffer doesn't start with 0x01 or
1596 * the size of remain buffer is smaller than HCI command header,
1597 * the firmware file is corrupted and it should stop the patching
1600 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1601 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1607 cmd = (struct hci_command_hdr *)(*fw_ptr);
1608 *fw_ptr += sizeof(*cmd);
1609 remain -= sizeof(*cmd);
1611 /* Ensure that the remain firmware data is long enough than the length
1612 * of command parameter. If not, the firmware file is corrupted.
1614 if (remain < cmd->plen) {
1615 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1619 /* If there is a command that loads a patch in the firmware
1620 * file, then enable the patch upon success, otherwise just
1621 * disable the manufacturer mode, for example patch activation
1622 * is not required when the default firmware patch file is used
1623 * because there are no patch data to load.
1625 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1628 cmd_param = *fw_ptr;
1629 *fw_ptr += cmd->plen;
1630 remain -= cmd->plen;
1632 /* This reads the expected events when the above command is sent to the
1633 * device. Some vendor commands expects more than one events, for
1634 * example command status event followed by vendor specific event.
1635 * For this case, it only keeps the last expected event. so the command
1636 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1637 * last expected event.
1639 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1643 evt = (struct hci_event_hdr *)(*fw_ptr);
1644 *fw_ptr += sizeof(*evt);
1645 remain -= sizeof(*evt);
1647 if (remain < evt->plen) {
1648 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1652 evt_param = *fw_ptr;
1653 *fw_ptr += evt->plen;
1654 remain -= evt->plen;
1657 /* Every HCI commands in the firmware file has its correspond event.
1658 * If event is not found or remain is smaller than zero, the firmware
1659 * file is corrupted.
1661 if (!evt || !evt_param || remain < 0) {
1662 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1666 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1667 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1669 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1670 cmd->opcode, PTR_ERR(skb));
1671 return PTR_ERR(skb);
1674 /* It ensures that the returned event matches the event data read from
1675 * the firmware file. At fist, it checks the length and then
1676 * the contents of the event.
1678 if (skb->len != evt->plen) {
1679 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1680 le16_to_cpu(cmd->opcode));
1685 if (memcmp(skb->data, evt_param, evt->plen)) {
1686 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1687 le16_to_cpu(cmd->opcode));
1696 static int btintel_legacy_rom_setup(struct hci_dev *hdev,
1697 struct intel_version *ver)
1699 const struct firmware *fw;
1701 int disable_patch, err;
1702 struct intel_version new_ver;
1704 BT_DBG("%s", hdev->name);
1706 /* fw_patch_num indicates the version of patch the device currently
1707 * have. If there is no patch data in the device, it is always 0x00.
1708 * So, if it is other than 0x00, no need to patch the device again.
1710 if (ver->fw_patch_num) {
1712 "Intel device is already patched. patch num: %02x",
1717 /* Opens the firmware patch file based on the firmware version read
1718 * from the controller. If it fails to open the matching firmware
1719 * patch file, it tries to open the default firmware patch file.
1720 * If no patch file is found, allow the device to operate without
1723 fw = btintel_legacy_rom_get_fw(hdev, ver);
1728 /* Enable the manufacturer mode of the controller.
1729 * Only while this mode is enabled, the driver can download the
1730 * firmware patch data and configuration parameters.
1732 err = btintel_enter_mfg(hdev);
1734 release_firmware(fw);
1740 /* The firmware data file consists of list of Intel specific HCI
1741 * commands and its expected events. The first byte indicates the
1742 * type of the message, either HCI command or HCI event.
1744 * It reads the command and its expected event from the firmware file,
1745 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1746 * the returned event is compared with the event read from the firmware
1747 * file and it will continue until all the messages are downloaded to
1750 * Once the firmware patching is completed successfully,
1751 * the manufacturer mode is disabled with reset and activating the
1754 * If the firmware patching fails, the manufacturer mode is
1755 * disabled with reset and deactivating the patch.
1757 * If the default patch file is used, no reset is done when disabling
1760 while (fw->size > fw_ptr - fw->data) {
1763 ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
1766 goto exit_mfg_deactivate;
1769 release_firmware(fw);
1772 goto exit_mfg_disable;
1774 /* Patching completed successfully and disable the manufacturer mode
1775 * with reset and activate the downloaded firmware patches.
1777 err = btintel_exit_mfg(hdev, true, true);
1781 /* Need build number for downloaded fw patches in
1782 * every power-on boot
1784 err = btintel_read_version(hdev, &new_ver);
1788 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
1789 new_ver.fw_patch_num);
1794 /* Disable the manufacturer mode without reset */
1795 err = btintel_exit_mfg(hdev, false, false);
1799 bt_dev_info(hdev, "Intel firmware patch completed");
1803 exit_mfg_deactivate:
1804 release_firmware(fw);
1806 /* Patching failed. Disable the manufacturer mode with reset and
1807 * deactivate the downloaded firmware patches.
1809 err = btintel_exit_mfg(hdev, true, false);
1813 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1816 /* Set the event mask for Intel specific vendor events. This enables
1817 * a few extra events that are useful during general operation.
1819 btintel_set_event_mask_mfg(hdev, false);
1821 btintel_check_bdaddr(hdev);
1826 static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1828 ktime_t delta, rettime;
1829 unsigned long long duration;
1832 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1834 bt_dev_info(hdev, "Waiting for firmware download to complete");
1836 err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
1838 msecs_to_jiffies(msec));
1839 if (err == -EINTR) {
1840 bt_dev_err(hdev, "Firmware loading interrupted");
1845 bt_dev_err(hdev, "Firmware loading timeout");
1849 if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
1850 bt_dev_err(hdev, "Firmware loading failed");
1854 rettime = ktime_get();
1855 delta = ktime_sub(rettime, calltime);
1856 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1858 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
1863 static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1865 ktime_t delta, rettime;
1866 unsigned long long duration;
1869 bt_dev_info(hdev, "Waiting for device to boot");
1871 err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
1873 msecs_to_jiffies(msec));
1874 if (err == -EINTR) {
1875 bt_dev_err(hdev, "Device boot interrupted");
1880 bt_dev_err(hdev, "Device boot timeout");
1884 rettime = ktime_get();
1885 delta = ktime_sub(rettime, calltime);
1886 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1888 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
1893 static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
1898 calltime = ktime_get();
1900 btintel_set_flag(hdev, INTEL_BOOTING);
1902 err = btintel_send_intel_reset(hdev, boot_addr);
1904 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
1905 btintel_reset_to_bootloader(hdev);
1909 /* The bootloader will not indicate when the device is ready. This
1910 * is done by the operational firmware sending bootup notification.
1912 * Booting into operational firmware should not take longer than
1913 * 1 second. However if that happens, then just fail the setup
1914 * since something went wrong.
1916 err = btintel_boot_wait(hdev, calltime, 1000);
1917 if (err == -ETIMEDOUT)
1918 btintel_reset_to_bootloader(hdev);
1923 static int btintel_get_fw_name(struct intel_version *ver,
1924 struct intel_boot_params *params,
1925 char *fw_name, size_t len,
1928 switch (ver->hw_variant) {
1929 case 0x0b: /* SfP */
1930 case 0x0c: /* WsP */
1931 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
1933 le16_to_cpu(params->dev_revid),
1936 case 0x11: /* JfP */
1937 case 0x12: /* ThP */
1938 case 0x13: /* HrP */
1939 case 0x14: /* CcP */
1940 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
1953 static int btintel_download_fw(struct hci_dev *hdev,
1954 struct intel_version *ver,
1955 struct intel_boot_params *params,
1958 const struct firmware *fw;
1963 if (!ver || !params)
1966 /* The firmware variant determines if the device is in bootloader
1967 * mode or is running operational firmware. The value 0x06 identifies
1968 * the bootloader and the value 0x23 identifies the operational
1971 * When the operational firmware is already present, then only
1972 * the check for valid Bluetooth device address is needed. This
1973 * determines if the device will be added as configured or
1974 * unconfigured controller.
1976 * It is not possible to use the Secure Boot Parameters in this
1977 * case since that command is only available in bootloader mode.
1979 if (ver->fw_variant == 0x23) {
1980 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
1981 btintel_check_bdaddr(hdev);
1983 /* SfP and WsP don't seem to update the firmware version on file
1984 * so version checking is currently possible.
1986 switch (ver->hw_variant) {
1987 case 0x0b: /* SfP */
1988 case 0x0c: /* WsP */
1992 /* Proceed to download to check if the version matches */
1996 /* Read the secure boot parameters to identify the operating
1997 * details of the bootloader.
1999 err = btintel_read_boot_params(hdev, params);
2003 /* It is required that every single firmware fragment is acknowledged
2004 * with a command complete event. If the boot parameters indicate
2005 * that this bootloader does not send them, then abort the setup.
2007 if (params->limited_cce != 0x00) {
2008 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2009 params->limited_cce);
2013 /* If the OTP has no valid Bluetooth device address, then there will
2014 * also be no valid address for the operational firmware.
2016 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2017 bt_dev_info(hdev, "No device address configured");
2018 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2022 /* With this Intel bootloader only the hardware variant and device
2023 * revision information are used to select the right firmware for SfP
2026 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2028 * Currently the supported hardware variants are:
2029 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2030 * 12 (0x0c) for iBT3.5 (WsP)
2032 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2033 * variant, HW revision and FW revision, as these are dependent on CNVi
2034 * and RF Combination.
2036 * 17 (0x11) for iBT3.5 (JfP)
2037 * 18 (0x12) for iBT3.5 (ThP)
2039 * The firmware file name for these will be
2040 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2043 err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
2045 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2046 /* Firmware has already been loaded */
2047 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2051 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2055 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
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, "Failed to load Intel firmware file %s (%d)",
2068 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2070 if (fw->size < 644) {
2071 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2077 calltime = ktime_get();
2079 btintel_set_flag(hdev, INTEL_DOWNLOADING);
2081 /* Start firmware downloading and get boot parameter */
2082 err = btintel_download_firmware(hdev, ver, fw, boot_param);
2084 if (err == -EALREADY) {
2085 /* Firmware has already been loaded */
2086 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2091 /* When FW download fails, send Intel Reset to retry
2094 btintel_reset_to_bootloader(hdev);
2098 /* Before switching the device into operational mode and with that
2099 * booting the loaded firmware, wait for the bootloader notification
2100 * that all fragments have been successfully received.
2102 * When the event processing receives the notification, then the
2103 * INTEL_DOWNLOADING flag will be cleared.
2105 * The firmware loading should not take longer than 5 seconds
2106 * and thus just timeout if that happens and fail the setup
2109 err = btintel_download_wait(hdev, calltime, 5000);
2110 if (err == -ETIMEDOUT)
2111 btintel_reset_to_bootloader(hdev);
2114 release_firmware(fw);
2118 static int btintel_bootloader_setup(struct hci_dev *hdev,
2119 struct intel_version *ver)
2121 struct intel_version new_ver;
2122 struct intel_boot_params params;
2127 BT_DBG("%s", hdev->name);
2129 /* Set the default boot parameter to 0x0 and it is updated to
2130 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2131 * command while downloading the firmware.
2133 boot_param = 0x00000000;
2135 btintel_set_flag(hdev, INTEL_BOOTLOADER);
2137 err = btintel_download_fw(hdev, ver, ¶ms, &boot_param);
2141 /* controller is already having an operational firmware */
2142 if (ver->fw_variant == 0x23)
2145 err = btintel_boot(hdev, boot_param);
2149 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2151 err = btintel_get_fw_name(ver, ¶ms, ddcname,
2152 sizeof(ddcname), "ddc");
2155 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2157 /* Once the device is running in operational mode, it needs to
2158 * apply the device configuration (DDC) parameters.
2160 * The device can work without DDC parameters, so even if it
2161 * fails to load the file, no need to fail the setup.
2163 btintel_load_ddc_config(hdev, ddcname);
2166 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2168 /* Read the Intel version information after loading the FW */
2169 err = btintel_read_version(hdev, &new_ver);
2173 btintel_version_info(hdev, &new_ver);
2176 /* Set the event mask for Intel specific vendor events. This enables
2177 * a few extra events that are useful during general operation. It
2178 * does not enable any debugging related events.
2180 * The device will function correctly without these events enabled
2181 * and thus no need to fail the setup.
2183 btintel_set_event_mask(hdev, false);
2188 static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
2189 char *fw_name, size_t len,
2192 /* The firmware file name for new generation controllers will be
2193 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2195 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2196 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2197 INTEL_CNVX_TOP_STEP(ver->cnvi_top)),
2198 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2199 INTEL_CNVX_TOP_STEP(ver->cnvr_top)),
2203 static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
2204 struct intel_version_tlv *ver,
2207 const struct firmware *fw;
2212 if (!ver || !boot_param)
2215 /* The firmware variant determines if the device is in bootloader
2216 * mode or is running operational firmware. The value 0x03 identifies
2217 * the bootloader and the value 0x23 identifies the operational
2220 * When the operational firmware is already present, then only
2221 * the check for valid Bluetooth device address is needed. This
2222 * determines if the device will be added as configured or
2223 * unconfigured controller.
2225 * It is not possible to use the Secure Boot Parameters in this
2226 * case since that command is only available in bootloader mode.
2228 if (ver->img_type == 0x03) {
2229 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2230 btintel_check_bdaddr(hdev);
2233 * Check for valid bd address in boot loader mode. Device
2234 * will be marked as unconfigured if empty bd address is
2237 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2238 bt_dev_info(hdev, "No device address configured");
2239 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2243 btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2244 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2246 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2247 /* Firmware has already been loaded */
2248 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2252 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2258 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2260 if (fw->size < 644) {
2261 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2267 calltime = ktime_get();
2269 btintel_set_flag(hdev, INTEL_DOWNLOADING);
2271 /* Start firmware downloading and get boot parameter */
2272 err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
2273 INTEL_HW_VARIANT(ver->cnvi_bt),
2276 if (err == -EALREADY) {
2277 /* Firmware has already been loaded */
2278 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2283 /* When FW download fails, send Intel Reset to retry
2286 btintel_reset_to_bootloader(hdev);
2290 /* Before switching the device into operational mode and with that
2291 * booting the loaded firmware, wait for the bootloader notification
2292 * that all fragments have been successfully received.
2294 * When the event processing receives the notification, then the
2295 * BTUSB_DOWNLOADING flag will be cleared.
2297 * The firmware loading should not take longer than 5 seconds
2298 * and thus just timeout if that happens and fail the setup
2301 err = btintel_download_wait(hdev, calltime, 5000);
2302 if (err == -ETIMEDOUT)
2303 btintel_reset_to_bootloader(hdev);
2306 release_firmware(fw);
2310 static int btintel_get_codec_config_data(struct hci_dev *hdev,
2311 __u8 link, struct bt_codec *codec,
2312 __u8 *ven_len, __u8 **ven_data)
2316 if (!ven_data || !ven_len)
2322 if (link != ESCO_LINK) {
2323 bt_dev_err(hdev, "Invalid link type(%u)", link);
2327 *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
2333 /* supports only CVSD and mSBC offload codecs */
2334 switch (codec->id) {
2343 bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
2346 /* codec and its capabilities are pre-defined to ids
2347 * preset id = 0x00 represents CVSD codec with sampling rate 8K
2348 * preset id = 0x01 represents mSBC codec with sampling rate 16K
2350 *ven_len = sizeof(__u8);
2359 static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
2361 /* Intel uses 1 as data path id for all the usecases */
2366 static int btintel_configure_offload(struct hci_dev *hdev)
2368 struct sk_buff *skb;
2370 struct intel_offload_use_cases *use_cases;
2372 skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
2374 bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
2376 return PTR_ERR(skb);
2379 if (skb->len < sizeof(*use_cases)) {
2384 use_cases = (void *)skb->data;
2386 if (use_cases->status) {
2387 err = -bt_to_errno(skb->data[0]);
2391 if (use_cases->preset[0] & 0x03) {
2392 hdev->get_data_path_id = btintel_get_data_path_id;
2393 hdev->get_codec_config_data = btintel_get_codec_config_data;
2400 static void btintel_set_ppag(struct hci_dev *hdev, struct intel_version_tlv *ver)
2402 struct btintel_ppag ppag;
2403 struct sk_buff *skb;
2404 struct hci_ppag_enable_cmd ppag_cmd;
2407 /* PPAG is not supported if CRF is HrP2, Jfp2, JfP1 */
2408 switch (ver->cnvr_top & 0xFFF) {
2409 case 0x504: /* Hrp2 */
2410 case 0x202: /* Jfp2 */
2411 case 0x201: /* Jfp1 */
2412 bt_dev_dbg(hdev, "PPAG not supported for Intel CNVr (0x%3x)",
2413 ver->cnvr_top & 0xFFF);
2417 handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2419 bt_dev_info(hdev, "No support for BT device in ACPI firmware");
2423 memset(&ppag, 0, sizeof(ppag));
2426 ppag.status = AE_NOT_FOUND;
2427 acpi_walk_namespace(ACPI_TYPE_PACKAGE, handle, 1, NULL,
2428 btintel_ppag_callback, &ppag, NULL);
2430 if (ACPI_FAILURE(ppag.status)) {
2431 if (ppag.status == AE_NOT_FOUND) {
2432 bt_dev_dbg(hdev, "PPAG-BT: ACPI entry not found");
2438 if (ppag.domain != 0x12) {
2439 bt_dev_dbg(hdev, "PPAG-BT: Bluetooth domain is disabled in ACPI firmware");
2444 * BIT 0 : 0 Disabled in EU
2446 * BIT 1 : 0 Disabled in China
2447 * 1 Enabled in China
2449 if ((ppag.mode & 0x01) != BIT(0) && (ppag.mode & 0x02) != BIT(1)) {
2450 bt_dev_dbg(hdev, "PPAG-BT: EU, China mode are disabled in CB/BIOS");
2454 ppag_cmd.ppag_enable_flags = cpu_to_le32(ppag.mode);
2456 skb = __hci_cmd_sync(hdev, INTEL_OP_PPAG_CMD, sizeof(ppag_cmd), &ppag_cmd, HCI_CMD_TIMEOUT);
2458 bt_dev_warn(hdev, "Failed to send PPAG Enable (%ld)", PTR_ERR(skb));
2461 bt_dev_info(hdev, "PPAG-BT: Enabled (Mode %d)", ppag.mode);
2465 static int btintel_acpi_reset_method(struct hci_dev *hdev)
2469 union acpi_object *p, *ref;
2470 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2472 status = acpi_evaluate_object(ACPI_HANDLE(GET_HCIDEV_DEV(hdev)), "_PRR", NULL, &buffer);
2473 if (ACPI_FAILURE(status)) {
2474 bt_dev_err(hdev, "Failed to run _PRR method");
2480 if (p->package.count != 1 || p->type != ACPI_TYPE_PACKAGE) {
2481 bt_dev_err(hdev, "Invalid arguments");
2486 ref = &p->package.elements[0];
2487 if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) {
2488 bt_dev_err(hdev, "Invalid object type: 0x%x", ref->type);
2493 status = acpi_evaluate_object(ref->reference.handle, "_RST", NULL, NULL);
2494 if (ACPI_FAILURE(status)) {
2495 bt_dev_err(hdev, "Failed to run_RST method");
2501 kfree(buffer.pointer);
2505 static void btintel_set_dsm_reset_method(struct hci_dev *hdev,
2506 struct intel_version_tlv *ver_tlv)
2508 struct btintel_data *data = hci_get_priv(hdev);
2509 acpi_handle handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2510 u8 reset_payload[4] = {0x01, 0x00, 0x01, 0x00};
2511 union acpi_object *obj, argv4;
2513 RESET_TYPE_WDISABLE2,
2517 handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2520 bt_dev_dbg(hdev, "No support for bluetooth device in ACPI firmware");
2524 if (!acpi_has_method(handle, "_PRR")) {
2525 bt_dev_err(hdev, "No support for _PRR ACPI method");
2529 switch (ver_tlv->cnvi_top & 0xfff) {
2530 case 0x910: /* GalePeak2 */
2531 reset_payload[2] = RESET_TYPE_VSEC;
2534 /* WDISABLE2 is the default reset method */
2535 reset_payload[2] = RESET_TYPE_WDISABLE2;
2537 if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2538 BIT(DSM_SET_WDISABLE2_DELAY))) {
2539 bt_dev_err(hdev, "No dsm support to set reset delay");
2542 argv4.integer.type = ACPI_TYPE_INTEGER;
2543 /* delay required to toggle BT power */
2544 argv4.integer.value = 160;
2545 obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2546 DSM_SET_WDISABLE2_DELAY, &argv4);
2548 bt_dev_err(hdev, "Failed to call dsm to set reset delay");
2554 bt_dev_info(hdev, "DSM reset method type: 0x%02x", reset_payload[2]);
2556 if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2557 DSM_SET_RESET_METHOD)) {
2558 bt_dev_warn(hdev, "No support for dsm to set reset method");
2561 argv4.buffer.type = ACPI_TYPE_BUFFER;
2562 argv4.buffer.length = sizeof(reset_payload);
2563 argv4.buffer.pointer = reset_payload;
2565 obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2566 DSM_SET_RESET_METHOD, &argv4);
2568 bt_dev_err(hdev, "Failed to call dsm to set reset method");
2572 data->acpi_reset_method = btintel_acpi_reset_method;
2575 static int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
2576 struct intel_version_tlv *ver)
2581 struct intel_version_tlv new_ver;
2583 bt_dev_dbg(hdev, "");
2585 /* Set the default boot parameter to 0x0 and it is updated to
2586 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2587 * command while downloading the firmware.
2589 boot_param = 0x00000000;
2591 btintel_set_flag(hdev, INTEL_BOOTLOADER);
2593 err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2597 /* check if controller is already having an operational firmware */
2598 if (ver->img_type == 0x03)
2601 err = btintel_boot(hdev, boot_param);
2605 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2607 btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
2608 /* Once the device is running in operational mode, it needs to
2609 * apply the device configuration (DDC) parameters.
2611 * The device can work without DDC parameters, so even if it
2612 * fails to load the file, no need to fail the setup.
2614 btintel_load_ddc_config(hdev, ddcname);
2616 /* Read supported use cases and set callbacks to fetch datapath id */
2617 btintel_configure_offload(hdev);
2619 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2621 /* Set PPAG feature */
2622 btintel_set_ppag(hdev, ver);
2624 /* Read the Intel version information after loading the FW */
2625 err = btintel_read_version_tlv(hdev, &new_ver);
2629 btintel_version_info_tlv(hdev, &new_ver);
2632 /* Set the event mask for Intel specific vendor events. This enables
2633 * a few extra events that are useful during general operation. It
2634 * does not enable any debugging related events.
2636 * The device will function correctly without these events enabled
2637 * and thus no need to fail the setup.
2639 btintel_set_event_mask(hdev, false);
2644 static void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
2646 switch (hw_variant) {
2647 /* Legacy bootloader devices that supports MSFT Extension */
2648 case 0x11: /* JfP */
2649 case 0x12: /* ThP */
2650 case 0x13: /* HrP */
2651 case 0x14: /* CcP */
2652 /* All Intel new genration controllers support the Microsoft vendor
2653 * extension are using 0xFC1E for VsMsftOpCode.
2660 hci_set_msft_opcode(hdev, 0xFC1E);
2668 static int btintel_setup_combined(struct hci_dev *hdev)
2670 const u8 param[1] = { 0xFF };
2671 struct intel_version ver;
2672 struct intel_version_tlv ver_tlv;
2673 struct sk_buff *skb;
2676 BT_DBG("%s", hdev->name);
2678 /* The some controllers have a bug with the first HCI command sent to it
2679 * returning number of completed commands as zero. This would stall the
2680 * command processing in the Bluetooth core.
2682 * As a workaround, send HCI Reset command first which will reset the
2683 * number of completed commands and allow normal command processing
2686 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
2687 * in the SW_RFKILL ON state as a workaround of fixing LED issue during
2688 * the shutdown() procedure, and once the device is in SW_RFKILL ON
2689 * state, the only way to exit out of it is sending the HCI_Reset
2692 if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
2693 btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2694 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
2698 "sending initial HCI reset failed (%ld)",
2700 return PTR_ERR(skb);
2705 /* Starting from TyP device, the command parameter and response are
2706 * changed even though the OCF for HCI_Intel_Read_Version command
2707 * remains same. The legacy devices can handle even if the
2708 * command has a parameter and returns a correct version information.
2709 * So, it uses new format to support both legacy and new format.
2711 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
2713 bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
2715 return PTR_ERR(skb);
2718 /* Check the status */
2720 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
2726 /* Apply the common HCI quirks for Intel device */
2727 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2728 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2729 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2731 /* Set up the quality report callback for Intel devices */
2732 hdev->set_quality_report = btintel_set_quality_report;
2734 /* For Legacy device, check the HW platform value and size */
2735 if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
2736 bt_dev_dbg(hdev, "Read the legacy Intel version information");
2738 memcpy(&ver, skb->data, sizeof(ver));
2740 /* Display version information */
2741 btintel_version_info(hdev, &ver);
2743 /* Check for supported iBT hardware variants of this firmware
2746 * This check has been put in place to ensure correct forward
2747 * compatibility options when newer hardware variants come
2750 switch (ver.hw_variant) {
2752 case 0x08: /* StP */
2753 /* Legacy ROM product */
2754 btintel_set_flag(hdev, INTEL_ROM_LEGACY);
2756 /* Apply the device specific HCI quirks
2758 * WBS for SdP - For the Legacy ROM products, only SdP
2759 * supports the WBS. But the version information is not
2760 * enough to use here because the StP2 and SdP have same
2761 * hw_variant and fw_variant. So, this flag is set by
2762 * the transport driver (btusb) based on the HW info
2765 if (!btintel_test_flag(hdev,
2766 INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
2767 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2769 if (ver.hw_variant == 0x08 && ver.fw_variant == 0x22)
2770 set_bit(HCI_QUIRK_VALID_LE_STATES,
2773 err = btintel_legacy_rom_setup(hdev, &ver);
2775 case 0x0b: /* SfP */
2776 case 0x11: /* JfP */
2777 case 0x12: /* ThP */
2778 case 0x13: /* HrP */
2779 case 0x14: /* CcP */
2780 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2782 case 0x0c: /* WsP */
2783 /* Apply the device specific HCI quirks
2785 * All Legacy bootloader devices support WBS
2787 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2790 /* These variants don't seem to support LE Coded PHY */
2791 set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
2793 /* Setup MSFT Extension support */
2794 btintel_set_msft_opcode(hdev, ver.hw_variant);
2796 err = btintel_bootloader_setup(hdev, &ver);
2797 btintel_register_devcoredump_support(hdev);
2800 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2808 /* memset ver_tlv to start with clean state as few fields are exclusive
2809 * to bootloader mode and are not populated in operational mode
2811 memset(&ver_tlv, 0, sizeof(ver_tlv));
2812 /* For TLV type device, parse the tlv data */
2813 err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
2815 bt_dev_err(hdev, "Failed to parse TLV version information");
2819 if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
2820 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2821 INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
2826 /* Check for supported iBT hardware variants of this firmware
2829 * This check has been put in place to ensure correct forward
2830 * compatibility options when newer hardware variants come
2833 switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
2834 case 0x11: /* JfP */
2835 case 0x12: /* ThP */
2836 case 0x13: /* HrP */
2837 case 0x14: /* CcP */
2838 /* Some legacy bootloader devices starting from JfP,
2839 * the operational firmware supports both old and TLV based
2840 * HCI_Intel_Read_Version command based on the command
2843 * For upgrading firmware case, the TLV based version cannot
2844 * be used because the firmware filename for legacy bootloader
2845 * is based on the old format.
2847 * Also, it is not easy to convert TLV based version from the
2848 * legacy version format.
2850 * So, as a workaround for those devices, use the legacy
2851 * HCI_Intel_Read_Version to get the version information and
2852 * run the legacy bootloader setup.
2854 err = btintel_read_version(hdev, &ver);
2858 /* Apply the device specific HCI quirks
2860 * All Legacy bootloader devices support WBS
2862 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2864 /* These variants don't seem to support LE Coded PHY */
2865 set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
2867 /* Set Valid LE States quirk */
2868 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2870 /* Setup MSFT Extension support */
2871 btintel_set_msft_opcode(hdev, ver.hw_variant);
2873 err = btintel_bootloader_setup(hdev, &ver);
2874 btintel_register_devcoredump_support(hdev);
2881 /* Display version information of TLV type */
2882 btintel_version_info_tlv(hdev, &ver_tlv);
2884 /* Apply the device specific HCI quirks for TLV based devices
2886 * All TLV based devices support WBS
2888 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2890 /* Apply LE States quirk from solar onwards */
2891 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2893 /* Setup MSFT Extension support */
2894 btintel_set_msft_opcode(hdev,
2895 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2896 btintel_set_dsm_reset_method(hdev, &ver_tlv);
2898 err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
2899 btintel_register_devcoredump_support(hdev);
2902 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2903 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2914 static int btintel_shutdown_combined(struct hci_dev *hdev)
2916 struct sk_buff *skb;
2919 /* Send HCI Reset to the controller to stop any BT activity which
2920 * were triggered. This will help to save power and maintain the
2921 * sync b/w Host and controller
2923 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2925 bt_dev_err(hdev, "HCI reset during shutdown failed");
2926 return PTR_ERR(skb);
2931 /* Some platforms have an issue with BT LED when the interface is
2932 * down or BT radio is turned off, which takes 5 seconds to BT LED
2933 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
2934 * device in the RFKILL ON state which turns off the BT LED immediately.
2936 if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2937 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2940 bt_dev_err(hdev, "turning off Intel device LED failed");
2949 int btintel_configure_setup(struct hci_dev *hdev, const char *driver_name)
2951 hdev->manufacturer = 2;
2952 hdev->setup = btintel_setup_combined;
2953 hdev->shutdown = btintel_shutdown_combined;
2954 hdev->hw_error = btintel_hw_error;
2955 hdev->set_diag = btintel_set_diag_combined;
2956 hdev->set_bdaddr = btintel_set_bdaddr;
2958 coredump_info.driver_name = driver_name;
2962 EXPORT_SYMBOL_GPL(btintel_configure_setup);
2964 static int btintel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb)
2966 struct intel_tlv *tlv = (void *)&skb->data[5];
2968 /* The first event is always an event type TLV */
2969 if (tlv->type != INTEL_TLV_TYPE_ID)
2972 switch (tlv->val[0]) {
2973 case INTEL_TLV_SYSTEM_EXCEPTION:
2974 case INTEL_TLV_FATAL_EXCEPTION:
2975 case INTEL_TLV_DEBUG_EXCEPTION:
2976 case INTEL_TLV_TEST_EXCEPTION:
2977 /* Generate devcoredump from exception */
2978 if (!hci_devcd_init(hdev, skb->len)) {
2979 hci_devcd_append(hdev, skb);
2980 hci_devcd_complete(hdev);
2982 bt_dev_err(hdev, "Failed to generate devcoredump");
2987 bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]);
2991 return hci_recv_frame(hdev, skb);
2994 int btintel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
2996 struct hci_event_hdr *hdr = (void *)skb->data;
2997 const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
2999 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
3001 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
3002 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
3004 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
3005 switch (skb->data[2]) {
3007 /* When switching to the operational firmware
3008 * the device sends a vendor specific event
3009 * indicating that the bootup completed.
3011 btintel_bootup(hdev, ptr, len);
3014 /* When the firmware loading completes the
3015 * device sends out a vendor specific event
3016 * indicating the result of the firmware
3019 btintel_secure_send_result(hdev, ptr, len);
3024 /* Handle all diagnostics events separately. May still call
3027 if (len >= sizeof(diagnostics_hdr) &&
3028 memcmp(&skb->data[2], diagnostics_hdr,
3029 sizeof(diagnostics_hdr)) == 0) {
3030 return btintel_diagnostics(hdev, skb);
3034 return hci_recv_frame(hdev, skb);
3036 EXPORT_SYMBOL_GPL(btintel_recv_event);
3038 void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
3040 const struct intel_bootup *evt = ptr;
3042 if (len != sizeof(*evt))
3045 if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
3046 btintel_wake_up_flag(hdev, INTEL_BOOTING);
3048 EXPORT_SYMBOL_GPL(btintel_bootup);
3050 void btintel_secure_send_result(struct hci_dev *hdev,
3051 const void *ptr, unsigned int len)
3053 const struct intel_secure_send_result *evt = ptr;
3055 if (len != sizeof(*evt))
3059 btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
3061 if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
3062 btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
3063 btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
3065 EXPORT_SYMBOL_GPL(btintel_secure_send_result);
3068 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
3069 MODULE_VERSION(VERSION);
3070 MODULE_LICENSE("GPL");
3071 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
3072 MODULE_FIRMWARE("intel/ibt-11-5.ddc");
3073 MODULE_FIRMWARE("intel/ibt-12-16.sfi");
3074 MODULE_FIRMWARE("intel/ibt-12-16.ddc");