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 <asm/unaligned.h>
14 #include <net/bluetooth/bluetooth.h>
15 #include <net/bluetooth/hci_core.h>
21 #define BDADDR_INTEL (&(bdaddr_t){{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
22 #define RSA_HEADER_LEN 644
23 #define CSS_HEADER_OFFSET 8
24 #define ECDSA_OFFSET 644
25 #define ECDSA_HEADER_LEN 320
27 #define CMD_WRITE_BOOT_PARAMS 0xfc0e
28 struct cmd_write_boot_params {
35 int btintel_check_bdaddr(struct hci_dev *hdev)
37 struct hci_rp_read_bd_addr *bda;
40 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
43 int err = PTR_ERR(skb);
44 bt_dev_err(hdev, "Reading Intel device address failed (%d)",
49 if (skb->len != sizeof(*bda)) {
50 bt_dev_err(hdev, "Intel device address length mismatch");
55 bda = (struct hci_rp_read_bd_addr *)skb->data;
57 /* For some Intel based controllers, the default Bluetooth device
58 * address 00:03:19:9E:8B:00 can be found. These controllers are
59 * fully operational, but have the danger of duplicate addresses
60 * and that in turn can cause problems with Bluetooth operation.
62 if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
63 bt_dev_err(hdev, "Found Intel default device address (%pMR)",
65 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
72 EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
74 int btintel_enter_mfg(struct hci_dev *hdev)
76 static const u8 param[] = { 0x01, 0x00 };
79 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
81 bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
89 EXPORT_SYMBOL_GPL(btintel_enter_mfg);
91 int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
93 u8 param[] = { 0x00, 0x00 };
96 /* The 2nd command parameter specifies the manufacturing exit method:
97 * 0x00: Just disable the manufacturing mode (0x00).
98 * 0x01: Disable manufacturing mode and reset with patches deactivated.
99 * 0x02: Disable manufacturing mode and reset with patches activated.
102 param[1] |= patched ? 0x02 : 0x01;
104 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
106 bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
114 EXPORT_SYMBOL_GPL(btintel_exit_mfg);
116 int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
121 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
124 bt_dev_err(hdev, "Changing Intel device address failed (%d)",
132 EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
134 static int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
136 u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
143 skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
146 bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
154 int btintel_set_diag(struct hci_dev *hdev, bool enable)
170 skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
175 bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
182 btintel_set_event_mask(hdev, enable);
185 EXPORT_SYMBOL_GPL(btintel_set_diag);
187 static int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
191 err = btintel_enter_mfg(hdev);
195 ret = btintel_set_diag(hdev, enable);
197 err = btintel_exit_mfg(hdev, false, false);
204 static int btintel_set_diag_combined(struct hci_dev *hdev, bool enable)
208 /* Legacy ROM device needs to be in the manufacturer mode to apply
211 * This flag is set after reading the Intel version.
213 if (btintel_test_flag(hdev, INTEL_ROM_LEGACY))
214 ret = btintel_set_diag_mfg(hdev, enable);
216 ret = btintel_set_diag(hdev, enable);
221 static void btintel_hw_error(struct hci_dev *hdev, u8 code)
226 bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
228 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
230 bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
236 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
238 bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
243 if (skb->len != 13) {
244 bt_dev_err(hdev, "Exception info size mismatch");
249 bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
254 int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
258 /* The hardware platform number has a fixed value of 0x37 and
259 * for now only accept this single value.
261 if (ver->hw_platform != 0x37) {
262 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
267 /* Check for supported iBT hardware variants of this firmware
270 * This check has been put in place to ensure correct forward
271 * compatibility options when newer hardware variants come along.
273 switch (ver->hw_variant) {
274 case 0x07: /* WP - Legacy ROM */
275 case 0x08: /* StP - Legacy ROM */
284 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
289 switch (ver->fw_variant) {
291 variant = "Legacy ROM 2.5";
294 variant = "Bootloader";
297 variant = "Legacy ROM 2.x";
300 variant = "Firmware";
303 bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
307 bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
308 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
309 ver->fw_build_num, ver->fw_build_ww,
310 2000 + ver->fw_build_yy);
314 EXPORT_SYMBOL_GPL(btintel_version_info);
316 static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
321 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
323 cmd_param[0] = fragment_type;
324 memcpy(cmd_param + 1, param, fragment_len);
326 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
327 cmd_param, HCI_INIT_TIMEOUT);
333 plen -= fragment_len;
334 param += fragment_len;
340 int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
342 const struct firmware *fw;
347 err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
349 bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
354 bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
358 /* DDC file contains one or more DDC structure which has
359 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
361 while (fw->size > fw_ptr - fw->data) {
362 u8 cmd_plen = fw_ptr[0] + sizeof(u8);
364 skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
367 bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
369 release_firmware(fw);
377 release_firmware(fw);
379 bt_dev_info(hdev, "Applying Intel DDC parameters completed");
383 EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
385 int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
389 err = btintel_enter_mfg(hdev);
393 ret = btintel_set_event_mask(hdev, debug);
395 err = btintel_exit_mfg(hdev, false, false);
401 EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
403 int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
407 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
409 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
414 if (skb->len != sizeof(*ver)) {
415 bt_dev_err(hdev, "Intel version event size mismatch");
420 memcpy(ver, skb->data, sizeof(*ver));
426 EXPORT_SYMBOL_GPL(btintel_read_version);
428 static int btintel_version_info_tlv(struct hci_dev *hdev,
429 struct intel_version_tlv *version)
433 /* The hardware platform number has a fixed value of 0x37 and
434 * for now only accept this single value.
436 if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
437 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
438 INTEL_HW_PLATFORM(version->cnvi_bt));
442 /* Check for supported iBT hardware variants of this firmware
445 * This check has been put in place to ensure correct forward
446 * compatibility options when newer hardware variants come along.
448 switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
451 case 0x19: /* Slr-F */
455 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
456 INTEL_HW_VARIANT(version->cnvi_bt));
460 switch (version->img_type) {
462 variant = "Bootloader";
463 /* It is required that every single firmware fragment is acknowledged
464 * with a command complete event. If the boot parameters indicate
465 * that this bootloader does not send them, then abort the setup.
467 if (version->limited_cce != 0x00) {
468 bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
469 version->limited_cce);
473 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
474 if (version->sbe_type > 0x01) {
475 bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
480 bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
481 bt_dev_info(hdev, "Secure boot is %s",
482 version->secure_boot ? "enabled" : "disabled");
483 bt_dev_info(hdev, "OTP lock is %s",
484 version->otp_lock ? "enabled" : "disabled");
485 bt_dev_info(hdev, "API lock is %s",
486 version->api_lock ? "enabled" : "disabled");
487 bt_dev_info(hdev, "Debug lock is %s",
488 version->debug_lock ? "enabled" : "disabled");
489 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
490 version->min_fw_build_nn, version->min_fw_build_cw,
491 2000 + version->min_fw_build_yy);
494 variant = "Firmware";
497 bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
501 bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
502 2000 + (version->timestamp >> 8), version->timestamp & 0xff,
503 version->build_type, version->build_num);
508 static int btintel_parse_version_tlv(struct hci_dev *hdev,
509 struct intel_version_tlv *version,
512 /* Consume Command Complete Status field */
515 /* Event parameters contatin multiple TLVs. Read each of them
516 * and only keep the required data. Also, it use existing legacy
517 * version field like hw_platform, hw_variant, and fw_variant
518 * to keep the existing setup flow
521 struct intel_tlv *tlv;
523 /* Make sure skb has a minimum length of the header */
524 if (skb->len < sizeof(*tlv))
527 tlv = (struct intel_tlv *)skb->data;
529 /* Make sure skb has a enough data */
530 if (skb->len < tlv->len + sizeof(*tlv))
534 case INTEL_TLV_CNVI_TOP:
535 version->cnvi_top = get_unaligned_le32(tlv->val);
537 case INTEL_TLV_CNVR_TOP:
538 version->cnvr_top = get_unaligned_le32(tlv->val);
540 case INTEL_TLV_CNVI_BT:
541 version->cnvi_bt = get_unaligned_le32(tlv->val);
543 case INTEL_TLV_CNVR_BT:
544 version->cnvr_bt = get_unaligned_le32(tlv->val);
546 case INTEL_TLV_DEV_REV_ID:
547 version->dev_rev_id = get_unaligned_le16(tlv->val);
549 case INTEL_TLV_IMAGE_TYPE:
550 version->img_type = tlv->val[0];
552 case INTEL_TLV_TIME_STAMP:
553 /* If image type is Operational firmware (0x03), then
554 * running FW Calendar Week and Year information can
555 * be extracted from Timestamp information
557 version->min_fw_build_cw = tlv->val[0];
558 version->min_fw_build_yy = tlv->val[1];
559 version->timestamp = get_unaligned_le16(tlv->val);
561 case INTEL_TLV_BUILD_TYPE:
562 version->build_type = tlv->val[0];
564 case INTEL_TLV_BUILD_NUM:
565 /* If image type is Operational firmware (0x03), then
566 * running FW build number can be extracted from the
569 version->min_fw_build_nn = tlv->val[0];
570 version->build_num = get_unaligned_le32(tlv->val);
572 case INTEL_TLV_SECURE_BOOT:
573 version->secure_boot = tlv->val[0];
575 case INTEL_TLV_OTP_LOCK:
576 version->otp_lock = tlv->val[0];
578 case INTEL_TLV_API_LOCK:
579 version->api_lock = tlv->val[0];
581 case INTEL_TLV_DEBUG_LOCK:
582 version->debug_lock = tlv->val[0];
584 case INTEL_TLV_MIN_FW:
585 version->min_fw_build_nn = tlv->val[0];
586 version->min_fw_build_cw = tlv->val[1];
587 version->min_fw_build_yy = tlv->val[2];
589 case INTEL_TLV_LIMITED_CCE:
590 version->limited_cce = tlv->val[0];
592 case INTEL_TLV_SBE_TYPE:
593 version->sbe_type = tlv->val[0];
595 case INTEL_TLV_OTP_BDADDR:
596 memcpy(&version->otp_bd_addr, tlv->val,
600 /* Ignore rest of information */
603 /* consume the current tlv and move to next*/
604 skb_pull(skb, tlv->len + sizeof(*tlv));
610 static int btintel_read_version_tlv(struct hci_dev *hdev,
611 struct intel_version_tlv *version)
614 const u8 param[1] = { 0xFF };
619 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
621 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
627 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
633 btintel_parse_version_tlv(hdev, version, skb);
639 /* ------- REGMAP IBT SUPPORT ------- */
641 #define IBT_REG_MODE_8BIT 0x00
642 #define IBT_REG_MODE_16BIT 0x01
643 #define IBT_REG_MODE_32BIT 0x02
645 struct regmap_ibt_context {
646 struct hci_dev *hdev;
651 struct ibt_cp_reg_access {
658 struct ibt_rp_reg_access {
664 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
665 void *val, size_t val_size)
667 struct regmap_ibt_context *ctx = context;
668 struct ibt_cp_reg_access cp;
669 struct ibt_rp_reg_access *rp;
673 if (reg_size != sizeof(__le32))
678 cp.mode = IBT_REG_MODE_8BIT;
681 cp.mode = IBT_REG_MODE_16BIT;
684 cp.mode = IBT_REG_MODE_32BIT;
690 /* regmap provides a little-endian formatted addr */
691 cp.addr = *(__le32 *)addr;
694 bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
696 skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
700 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
701 le32_to_cpu(cp.addr), err);
705 if (skb->len != sizeof(*rp) + val_size) {
706 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
707 le32_to_cpu(cp.addr));
712 rp = (struct ibt_rp_reg_access *)skb->data;
714 if (rp->addr != cp.addr) {
715 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
716 le32_to_cpu(rp->addr));
721 memcpy(val, rp->data, val_size);
728 static int regmap_ibt_gather_write(void *context,
729 const void *addr, size_t reg_size,
730 const void *val, size_t val_size)
732 struct regmap_ibt_context *ctx = context;
733 struct ibt_cp_reg_access *cp;
735 int plen = sizeof(*cp) + val_size;
739 if (reg_size != sizeof(__le32))
744 mode = IBT_REG_MODE_8BIT;
747 mode = IBT_REG_MODE_16BIT;
750 mode = IBT_REG_MODE_32BIT;
756 cp = kmalloc(plen, GFP_KERNEL);
760 /* regmap provides a little-endian formatted addr/value */
761 cp->addr = *(__le32 *)addr;
764 memcpy(&cp->data, val, val_size);
766 bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
768 skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
771 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
772 le32_to_cpu(cp->addr), err);
782 static int regmap_ibt_write(void *context, const void *data, size_t count)
784 /* data contains register+value, since we only support 32bit addr,
785 * minimum data size is 4 bytes.
787 if (WARN_ONCE(count < 4, "Invalid register access"))
790 return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
793 static void regmap_ibt_free_context(void *context)
798 static const struct regmap_bus regmap_ibt = {
799 .read = regmap_ibt_read,
800 .write = regmap_ibt_write,
801 .gather_write = regmap_ibt_gather_write,
802 .free_context = regmap_ibt_free_context,
803 .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
804 .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
807 /* Config is the same for all register regions */
808 static const struct regmap_config regmap_ibt_cfg = {
809 .name = "btintel_regmap",
814 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
817 struct regmap_ibt_context *ctx;
819 bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
822 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
824 return ERR_PTR(-ENOMEM);
826 ctx->op_read = opcode_read;
827 ctx->op_write = opcode_write;
830 return regmap_init(&hdev->dev, ®map_ibt, ctx, ®map_ibt_cfg);
832 EXPORT_SYMBOL_GPL(btintel_regmap_init);
834 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
836 struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
839 params.boot_param = cpu_to_le32(boot_param);
841 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), ¶ms,
844 bt_dev_err(hdev, "Failed to send Intel Reset command");
852 EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
854 int btintel_read_boot_params(struct hci_dev *hdev,
855 struct intel_boot_params *params)
859 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
861 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
866 if (skb->len != sizeof(*params)) {
867 bt_dev_err(hdev, "Intel boot parameters size mismatch");
872 memcpy(params, skb->data, sizeof(*params));
876 if (params->status) {
877 bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
879 return -bt_to_errno(params->status);
882 bt_dev_info(hdev, "Device revision is %u",
883 le16_to_cpu(params->dev_revid));
885 bt_dev_info(hdev, "Secure boot is %s",
886 params->secure_boot ? "enabled" : "disabled");
888 bt_dev_info(hdev, "OTP lock is %s",
889 params->otp_lock ? "enabled" : "disabled");
891 bt_dev_info(hdev, "API lock is %s",
892 params->api_lock ? "enabled" : "disabled");
894 bt_dev_info(hdev, "Debug lock is %s",
895 params->debug_lock ? "enabled" : "disabled");
897 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
898 params->min_fw_build_nn, params->min_fw_build_cw,
899 2000 + params->min_fw_build_yy);
903 EXPORT_SYMBOL_GPL(btintel_read_boot_params);
905 static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
906 const struct firmware *fw)
910 /* Start the firmware download transaction with the Init fragment
911 * represented by the 128 bytes of CSS header.
913 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
915 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
919 /* Send the 256 bytes of public key information from the firmware
920 * as the PKey fragment.
922 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
924 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
928 /* Send the 256 bytes of signature information from the firmware
929 * as the Sign fragment.
931 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
933 bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
941 static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
942 const struct firmware *fw)
946 /* Start the firmware download transaction with the Init fragment
947 * represented by the 128 bytes of CSS header.
949 err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
951 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
955 /* Send the 96 bytes of public key information from the firmware
956 * as the PKey fragment.
958 err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
960 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
964 /* Send the 96 bytes of signature information from the firmware
965 * as the Sign fragment
967 err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
969 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
976 static int btintel_download_firmware_payload(struct hci_dev *hdev,
977 const struct firmware *fw,
984 fw_ptr = fw->data + offset;
988 while (fw_ptr - fw->data < fw->size) {
989 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
991 frag_len += sizeof(*cmd) + cmd->plen;
993 /* The parameter length of the secure send command requires
994 * a 4 byte alignment. It happens so that the firmware file
995 * contains proper Intel_NOP commands to align the fragments
998 * Send set of commands with 4 byte alignment from the
999 * firmware data buffer as a single Data fragement.
1001 if (!(frag_len % 4)) {
1002 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1005 "Failed to send firmware data (%d)",
1019 static bool btintel_firmware_version(struct hci_dev *hdev,
1020 u8 num, u8 ww, u8 yy,
1021 const struct firmware *fw,
1028 while (fw_ptr - fw->data < fw->size) {
1029 struct hci_command_hdr *cmd = (void *)(fw_ptr);
1031 /* Each SKU has a different reset parameter to use in the
1032 * HCI_Intel_Reset command and it is embedded in the firmware
1033 * data. So, instead of using static value per SKU, check
1034 * the firmware data and save it for later use.
1036 if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1037 struct cmd_write_boot_params *params;
1039 params = (void *)(fw_ptr + sizeof(*cmd));
1041 *boot_addr = le32_to_cpu(params->boot_addr);
1043 bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
1045 bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1046 params->fw_build_num, params->fw_build_ww,
1047 params->fw_build_yy);
1049 return (num == params->fw_build_num &&
1050 ww == params->fw_build_ww &&
1051 yy == params->fw_build_yy);
1054 fw_ptr += sizeof(*cmd) + cmd->plen;
1060 int btintel_download_firmware(struct hci_dev *hdev,
1061 struct intel_version *ver,
1062 const struct firmware *fw,
1067 /* SfP and WsP don't seem to update the firmware version on file
1068 * so version checking is currently not possible.
1070 switch (ver->hw_variant) {
1071 case 0x0b: /* SfP */
1072 case 0x0c: /* WsP */
1073 /* Skip version checking */
1077 /* Skip download if firmware has the same version */
1078 if (btintel_firmware_version(hdev, ver->fw_build_num,
1079 ver->fw_build_ww, ver->fw_build_yy,
1081 bt_dev_info(hdev, "Firmware already loaded");
1082 /* Return -EALREADY to indicate that the firmware has
1083 * already been loaded.
1089 /* The firmware variant determines if the device is in bootloader
1090 * mode or is running operational firmware. The value 0x06 identifies
1091 * the bootloader and the value 0x23 identifies the operational
1094 * If the firmware version has changed that means it needs to be reset
1095 * to bootloader when operational so the new firmware can be loaded.
1097 if (ver->fw_variant == 0x23)
1100 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1104 return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1106 EXPORT_SYMBOL_GPL(btintel_download_firmware);
1108 static int btintel_download_fw_tlv(struct hci_dev *hdev,
1109 struct intel_version_tlv *ver,
1110 const struct firmware *fw, u32 *boot_param,
1111 u8 hw_variant, u8 sbe_type)
1116 /* Skip download if firmware has the same version */
1117 if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1118 ver->min_fw_build_cw,
1119 ver->min_fw_build_yy,
1121 bt_dev_info(hdev, "Firmware already loaded");
1122 /* Return -EALREADY to indicate that firmware has
1123 * already been loaded.
1128 /* The firmware variant determines if the device is in bootloader
1129 * mode or is running operational firmware. The value 0x01 identifies
1130 * the bootloader and the value 0x03 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->img_type == 0x03)
1139 /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1140 * only RSA secure boot engine. Hence, the corresponding sfi file will
1141 * have RSA header of 644 bytes followed by Command Buffer.
1143 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1144 * secure boot engine. As a result, the corresponding sfi file will
1145 * have RSA header of 644, ECDSA header of 320 bytes followed by
1148 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1149 * version: RSA(0x00010000) , ECDSA (0x00020000)
1151 css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1152 if (css_header_ver != 0x00010000) {
1153 bt_dev_err(hdev, "Invalid CSS Header version");
1157 if (hw_variant <= 0x14) {
1158 if (sbe_type != 0x00) {
1159 bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1164 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1168 err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1171 } else if (hw_variant >= 0x17) {
1172 /* Check if CSS header for ECDSA follows the RSA header */
1173 if (fw->data[ECDSA_OFFSET] != 0x06)
1176 /* Check if the CSS Header version is ECDSA(0x00020000) */
1177 css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1178 if (css_header_ver != 0x00020000) {
1179 bt_dev_err(hdev, "Invalid CSS Header version");
1183 if (sbe_type == 0x00) {
1184 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1188 err = btintel_download_firmware_payload(hdev, fw,
1189 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1192 } else if (sbe_type == 0x01) {
1193 err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1197 err = btintel_download_firmware_payload(hdev, fw,
1198 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1206 static void btintel_reset_to_bootloader(struct hci_dev *hdev)
1208 struct intel_reset params;
1209 struct sk_buff *skb;
1211 /* Send Intel Reset command. This will result in
1212 * re-enumeration of BT controller.
1214 * Intel Reset parameter description:
1215 * reset_type : 0x00 (Soft reset),
1217 * patch_enable : 0x00 (Do not enable),
1219 * ddc_reload : 0x00 (Do not reload),
1221 * boot_option: 0x00 (Current image),
1222 * 0x01 (Specified boot address)
1223 * boot_param: Boot address
1226 params.reset_type = 0x01;
1227 params.patch_enable = 0x01;
1228 params.ddc_reload = 0x01;
1229 params.boot_option = 0x00;
1230 params.boot_param = cpu_to_le32(0x00000000);
1232 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1233 ¶ms, HCI_INIT_TIMEOUT);
1235 bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1239 bt_dev_info(hdev, "Intel reset sent to retry FW download");
1242 /* Current Intel BT controllers(ThP/JfP) hold the USB reset
1243 * lines for 2ms when it receives Intel Reset in bootloader mode.
1244 * Whereas, the upcoming Intel BT controllers will hold USB reset
1245 * for 150ms. To keep the delay generic, 150ms is chosen here.
1250 static int btintel_read_debug_features(struct hci_dev *hdev,
1251 struct intel_debug_features *features)
1253 struct sk_buff *skb;
1256 /* Intel controller supports two pages, each page is of 128-bit
1257 * feature bit mask. And each bit defines specific feature support
1259 skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1262 bt_dev_err(hdev, "Reading supported features failed (%ld)",
1264 return PTR_ERR(skb);
1267 if (skb->len != (sizeof(features->page1) + 3)) {
1268 bt_dev_err(hdev, "Supported features event size mismatch");
1273 memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1275 /* Read the supported features page2 if required in future.
1281 static int btintel_set_debug_features(struct hci_dev *hdev,
1282 const struct intel_debug_features *features)
1284 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
1286 u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
1287 u8 trace_enable = 0x02;
1288 struct sk_buff *skb;
1291 bt_dev_warn(hdev, "Debug features not read");
1295 if (!(features->page1[0] & 0x3f)) {
1296 bt_dev_info(hdev, "Telemetry exception format not supported");
1300 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1302 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1304 return PTR_ERR(skb);
1308 skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
1310 bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
1312 return PTR_ERR(skb);
1316 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1318 bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
1320 return PTR_ERR(skb);
1324 bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
1325 trace_enable, mask[3]);
1330 static int btintel_reset_debug_features(struct hci_dev *hdev,
1331 const struct intel_debug_features *features)
1333 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
1335 u8 trace_enable = 0x00;
1336 struct sk_buff *skb;
1339 bt_dev_warn(hdev, "Debug features not read");
1343 if (!(features->page1[0] & 0x3f)) {
1344 bt_dev_info(hdev, "Telemetry exception format not supported");
1348 /* Should stop the trace before writing ddc event mask. */
1349 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1351 bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
1353 return PTR_ERR(skb);
1357 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1359 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1361 return PTR_ERR(skb);
1365 bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
1366 trace_enable, mask[3]);
1371 int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
1373 struct intel_debug_features features;
1376 bt_dev_dbg(hdev, "enable %d", enable);
1378 /* Read the Intel supported features and if new exception formats
1379 * supported, need to load the additional DDC config to enable.
1381 err = btintel_read_debug_features(hdev, &features);
1385 /* Set or reset the debug features. */
1387 err = btintel_set_debug_features(hdev, &features);
1389 err = btintel_reset_debug_features(hdev, &features);
1393 EXPORT_SYMBOL_GPL(btintel_set_quality_report);
1395 static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
1396 struct intel_version *ver)
1398 const struct firmware *fw;
1402 snprintf(fwname, sizeof(fwname),
1403 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1404 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1405 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1406 ver->fw_build_ww, ver->fw_build_yy);
1408 ret = request_firmware(&fw, fwname, &hdev->dev);
1410 if (ret == -EINVAL) {
1411 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1416 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1419 /* If the correct firmware patch file is not found, use the
1420 * default firmware patch file instead
1422 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1423 ver->hw_platform, ver->hw_variant);
1424 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1425 bt_dev_err(hdev, "failed to open default fw file: %s",
1431 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1436 static int btintel_legacy_rom_patching(struct hci_dev *hdev,
1437 const struct firmware *fw,
1438 const u8 **fw_ptr, int *disable_patch)
1440 struct sk_buff *skb;
1441 struct hci_command_hdr *cmd;
1442 const u8 *cmd_param;
1443 struct hci_event_hdr *evt = NULL;
1444 const u8 *evt_param = NULL;
1445 int remain = fw->size - (*fw_ptr - fw->data);
1447 /* The first byte indicates the types of the patch command or event.
1448 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1449 * in the current firmware buffer doesn't start with 0x01 or
1450 * the size of remain buffer is smaller than HCI command header,
1451 * the firmware file is corrupted and it should stop the patching
1454 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1455 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1461 cmd = (struct hci_command_hdr *)(*fw_ptr);
1462 *fw_ptr += sizeof(*cmd);
1463 remain -= sizeof(*cmd);
1465 /* Ensure that the remain firmware data is long enough than the length
1466 * of command parameter. If not, the firmware file is corrupted.
1468 if (remain < cmd->plen) {
1469 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1473 /* If there is a command that loads a patch in the firmware
1474 * file, then enable the patch upon success, otherwise just
1475 * disable the manufacturer mode, for example patch activation
1476 * is not required when the default firmware patch file is used
1477 * because there are no patch data to load.
1479 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1482 cmd_param = *fw_ptr;
1483 *fw_ptr += cmd->plen;
1484 remain -= cmd->plen;
1486 /* This reads the expected events when the above command is sent to the
1487 * device. Some vendor commands expects more than one events, for
1488 * example command status event followed by vendor specific event.
1489 * For this case, it only keeps the last expected event. so the command
1490 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1491 * last expected event.
1493 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1497 evt = (struct hci_event_hdr *)(*fw_ptr);
1498 *fw_ptr += sizeof(*evt);
1499 remain -= sizeof(*evt);
1501 if (remain < evt->plen) {
1502 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1506 evt_param = *fw_ptr;
1507 *fw_ptr += evt->plen;
1508 remain -= evt->plen;
1511 /* Every HCI commands in the firmware file has its correspond event.
1512 * If event is not found or remain is smaller than zero, the firmware
1513 * file is corrupted.
1515 if (!evt || !evt_param || remain < 0) {
1516 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1520 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1521 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1523 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1524 cmd->opcode, PTR_ERR(skb));
1525 return PTR_ERR(skb);
1528 /* It ensures that the returned event matches the event data read from
1529 * the firmware file. At fist, it checks the length and then
1530 * the contents of the event.
1532 if (skb->len != evt->plen) {
1533 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1534 le16_to_cpu(cmd->opcode));
1539 if (memcmp(skb->data, evt_param, evt->plen)) {
1540 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1541 le16_to_cpu(cmd->opcode));
1550 static int btintel_legacy_rom_setup(struct hci_dev *hdev,
1551 struct intel_version *ver)
1553 const struct firmware *fw;
1555 int disable_patch, err;
1556 struct intel_version new_ver;
1558 BT_DBG("%s", hdev->name);
1560 /* fw_patch_num indicates the version of patch the device currently
1561 * have. If there is no patch data in the device, it is always 0x00.
1562 * So, if it is other than 0x00, no need to patch the device again.
1564 if (ver->fw_patch_num) {
1566 "Intel device is already patched. patch num: %02x",
1571 /* Opens the firmware patch file based on the firmware version read
1572 * from the controller. If it fails to open the matching firmware
1573 * patch file, it tries to open the default firmware patch file.
1574 * If no patch file is found, allow the device to operate without
1577 fw = btintel_legacy_rom_get_fw(hdev, ver);
1582 /* Enable the manufacturer mode of the controller.
1583 * Only while this mode is enabled, the driver can download the
1584 * firmware patch data and configuration parameters.
1586 err = btintel_enter_mfg(hdev);
1588 release_firmware(fw);
1594 /* The firmware data file consists of list of Intel specific HCI
1595 * commands and its expected events. The first byte indicates the
1596 * type of the message, either HCI command or HCI event.
1598 * It reads the command and its expected event from the firmware file,
1599 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1600 * the returned event is compared with the event read from the firmware
1601 * file and it will continue until all the messages are downloaded to
1604 * Once the firmware patching is completed successfully,
1605 * the manufacturer mode is disabled with reset and activating the
1608 * If the firmware patching fails, the manufacturer mode is
1609 * disabled with reset and deactivating the patch.
1611 * If the default patch file is used, no reset is done when disabling
1614 while (fw->size > fw_ptr - fw->data) {
1617 ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
1620 goto exit_mfg_deactivate;
1623 release_firmware(fw);
1626 goto exit_mfg_disable;
1628 /* Patching completed successfully and disable the manufacturer mode
1629 * with reset and activate the downloaded firmware patches.
1631 err = btintel_exit_mfg(hdev, true, true);
1635 /* Need build number for downloaded fw patches in
1636 * every power-on boot
1638 err = btintel_read_version(hdev, &new_ver);
1642 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
1643 new_ver.fw_patch_num);
1648 /* Disable the manufacturer mode without reset */
1649 err = btintel_exit_mfg(hdev, false, false);
1653 bt_dev_info(hdev, "Intel firmware patch completed");
1657 exit_mfg_deactivate:
1658 release_firmware(fw);
1660 /* Patching failed. Disable the manufacturer mode with reset and
1661 * deactivate the downloaded firmware patches.
1663 err = btintel_exit_mfg(hdev, true, false);
1667 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1670 /* Set the event mask for Intel specific vendor events. This enables
1671 * a few extra events that are useful during general operation.
1673 btintel_set_event_mask_mfg(hdev, false);
1675 btintel_check_bdaddr(hdev);
1680 static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1682 ktime_t delta, rettime;
1683 unsigned long long duration;
1686 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1688 bt_dev_info(hdev, "Waiting for firmware download to complete");
1690 err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
1692 msecs_to_jiffies(msec));
1693 if (err == -EINTR) {
1694 bt_dev_err(hdev, "Firmware loading interrupted");
1699 bt_dev_err(hdev, "Firmware loading timeout");
1703 if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
1704 bt_dev_err(hdev, "Firmware loading failed");
1708 rettime = ktime_get();
1709 delta = ktime_sub(rettime, calltime);
1710 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1712 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
1717 static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1719 ktime_t delta, rettime;
1720 unsigned long long duration;
1723 bt_dev_info(hdev, "Waiting for device to boot");
1725 err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
1727 msecs_to_jiffies(msec));
1728 if (err == -EINTR) {
1729 bt_dev_err(hdev, "Device boot interrupted");
1734 bt_dev_err(hdev, "Device boot timeout");
1738 rettime = ktime_get();
1739 delta = ktime_sub(rettime, calltime);
1740 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1742 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
1747 static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
1752 calltime = ktime_get();
1754 btintel_set_flag(hdev, INTEL_BOOTING);
1756 err = btintel_send_intel_reset(hdev, boot_addr);
1758 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
1759 btintel_reset_to_bootloader(hdev);
1763 /* The bootloader will not indicate when the device is ready. This
1764 * is done by the operational firmware sending bootup notification.
1766 * Booting into operational firmware should not take longer than
1767 * 1 second. However if that happens, then just fail the setup
1768 * since something went wrong.
1770 err = btintel_boot_wait(hdev, calltime, 1000);
1771 if (err == -ETIMEDOUT)
1772 btintel_reset_to_bootloader(hdev);
1777 static int btintel_get_fw_name(struct intel_version *ver,
1778 struct intel_boot_params *params,
1779 char *fw_name, size_t len,
1782 switch (ver->hw_variant) {
1783 case 0x0b: /* SfP */
1784 case 0x0c: /* WsP */
1785 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
1786 le16_to_cpu(ver->hw_variant),
1787 le16_to_cpu(params->dev_revid),
1790 case 0x11: /* JfP */
1791 case 0x12: /* ThP */
1792 case 0x13: /* HrP */
1793 case 0x14: /* CcP */
1794 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
1795 le16_to_cpu(ver->hw_variant),
1796 le16_to_cpu(ver->hw_revision),
1797 le16_to_cpu(ver->fw_revision),
1807 static int btintel_download_fw(struct hci_dev *hdev,
1808 struct intel_version *ver,
1809 struct intel_boot_params *params,
1812 const struct firmware *fw;
1817 if (!ver || !params)
1820 /* The firmware variant determines if the device is in bootloader
1821 * mode or is running operational firmware. The value 0x06 identifies
1822 * the bootloader and the value 0x23 identifies the operational
1825 * When the operational firmware is already present, then only
1826 * the check for valid Bluetooth device address is needed. This
1827 * determines if the device will be added as configured or
1828 * unconfigured controller.
1830 * It is not possible to use the Secure Boot Parameters in this
1831 * case since that command is only available in bootloader mode.
1833 if (ver->fw_variant == 0x23) {
1834 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
1835 btintel_check_bdaddr(hdev);
1837 /* SfP and WsP don't seem to update the firmware version on file
1838 * so version checking is currently possible.
1840 switch (ver->hw_variant) {
1841 case 0x0b: /* SfP */
1842 case 0x0c: /* WsP */
1846 /* Proceed to download to check if the version matches */
1850 /* Read the secure boot parameters to identify the operating
1851 * details of the bootloader.
1853 err = btintel_read_boot_params(hdev, params);
1857 /* It is required that every single firmware fragment is acknowledged
1858 * with a command complete event. If the boot parameters indicate
1859 * that this bootloader does not send them, then abort the setup.
1861 if (params->limited_cce != 0x00) {
1862 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
1863 params->limited_cce);
1867 /* If the OTP has no valid Bluetooth device address, then there will
1868 * also be no valid address for the operational firmware.
1870 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
1871 bt_dev_info(hdev, "No device address configured");
1872 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1876 /* With this Intel bootloader only the hardware variant and device
1877 * revision information are used to select the right firmware for SfP
1880 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
1882 * Currently the supported hardware variants are:
1883 * 11 (0x0b) for iBT3.0 (LnP/SfP)
1884 * 12 (0x0c) for iBT3.5 (WsP)
1886 * For ThP/JfP and for future SKU's, the FW name varies based on HW
1887 * variant, HW revision and FW revision, as these are dependent on CNVi
1888 * and RF Combination.
1890 * 17 (0x11) for iBT3.5 (JfP)
1891 * 18 (0x12) for iBT3.5 (ThP)
1893 * The firmware file name for these will be
1894 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
1897 err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
1899 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
1900 /* Firmware has already been loaded */
1901 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1905 bt_dev_err(hdev, "Unsupported Intel firmware naming");
1909 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
1911 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
1912 /* Firmware has already been loaded */
1913 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1917 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
1922 bt_dev_info(hdev, "Found device firmware: %s", fwname);
1924 if (fw->size < 644) {
1925 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
1931 calltime = ktime_get();
1933 btintel_set_flag(hdev, INTEL_DOWNLOADING);
1935 /* Start firmware downloading and get boot parameter */
1936 err = btintel_download_firmware(hdev, ver, fw, boot_param);
1938 if (err == -EALREADY) {
1939 /* Firmware has already been loaded */
1940 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1945 /* When FW download fails, send Intel Reset to retry
1948 btintel_reset_to_bootloader(hdev);
1952 /* Before switching the device into operational mode and with that
1953 * booting the loaded firmware, wait for the bootloader notification
1954 * that all fragments have been successfully received.
1956 * When the event processing receives the notification, then the
1957 * INTEL_DOWNLOADING flag will be cleared.
1959 * The firmware loading should not take longer than 5 seconds
1960 * and thus just timeout if that happens and fail the setup
1963 err = btintel_download_wait(hdev, calltime, 5000);
1964 if (err == -ETIMEDOUT)
1965 btintel_reset_to_bootloader(hdev);
1968 release_firmware(fw);
1972 static int btintel_bootloader_setup(struct hci_dev *hdev,
1973 struct intel_version *ver)
1975 struct intel_version new_ver;
1976 struct intel_boot_params params;
1981 BT_DBG("%s", hdev->name);
1983 /* Set the default boot parameter to 0x0 and it is updated to
1984 * SKU specific boot parameter after reading Intel_Write_Boot_Params
1985 * command while downloading the firmware.
1987 boot_param = 0x00000000;
1989 btintel_set_flag(hdev, INTEL_BOOTLOADER);
1991 err = btintel_download_fw(hdev, ver, ¶ms, &boot_param);
1995 /* controller is already having an operational firmware */
1996 if (ver->fw_variant == 0x23)
1999 err = btintel_boot(hdev, boot_param);
2003 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2005 err = btintel_get_fw_name(ver, ¶ms, ddcname,
2006 sizeof(ddcname), "ddc");
2009 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2011 /* Once the device is running in operational mode, it needs to
2012 * apply the device configuration (DDC) parameters.
2014 * The device can work without DDC parameters, so even if it
2015 * fails to load the file, no need to fail the setup.
2017 btintel_load_ddc_config(hdev, ddcname);
2020 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2022 /* Read the Intel version information after loading the FW */
2023 err = btintel_read_version(hdev, &new_ver);
2027 btintel_version_info(hdev, &new_ver);
2030 /* Set the event mask for Intel specific vendor events. This enables
2031 * a few extra events that are useful during general operation. It
2032 * does not enable any debugging related events.
2034 * The device will function correctly without these events enabled
2035 * and thus no need to fail the setup.
2037 btintel_set_event_mask(hdev, false);
2042 static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
2043 char *fw_name, size_t len,
2046 /* The firmware file name for new generation controllers will be
2047 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2049 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2050 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2051 INTEL_CNVX_TOP_STEP(ver->cnvi_top)),
2052 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2053 INTEL_CNVX_TOP_STEP(ver->cnvr_top)),
2057 static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
2058 struct intel_version_tlv *ver,
2061 const struct firmware *fw;
2066 if (!ver || !boot_param)
2069 /* The firmware variant determines if the device is in bootloader
2070 * mode or is running operational firmware. The value 0x03 identifies
2071 * the bootloader and the value 0x23 identifies the operational
2074 * When the operational firmware is already present, then only
2075 * the check for valid Bluetooth device address is needed. This
2076 * determines if the device will be added as configured or
2077 * unconfigured controller.
2079 * It is not possible to use the Secure Boot Parameters in this
2080 * case since that command is only available in bootloader mode.
2082 if (ver->img_type == 0x03) {
2083 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2084 btintel_check_bdaddr(hdev);
2087 * Check for valid bd address in boot loader mode. Device
2088 * will be marked as unconfigured if empty bd address is
2091 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2092 bt_dev_info(hdev, "No device address configured");
2093 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2097 btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2098 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2100 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2101 /* Firmware has already been loaded */
2102 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2106 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2112 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2114 if (fw->size < 644) {
2115 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2121 calltime = ktime_get();
2123 btintel_set_flag(hdev, INTEL_DOWNLOADING);
2125 /* Start firmware downloading and get boot parameter */
2126 err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
2127 INTEL_HW_VARIANT(ver->cnvi_bt),
2130 if (err == -EALREADY) {
2131 /* Firmware has already been loaded */
2132 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2137 /* When FW download fails, send Intel Reset to retry
2140 btintel_reset_to_bootloader(hdev);
2144 /* Before switching the device into operational mode and with that
2145 * booting the loaded firmware, wait for the bootloader notification
2146 * that all fragments have been successfully received.
2148 * When the event processing receives the notification, then the
2149 * BTUSB_DOWNLOADING flag will be cleared.
2151 * The firmware loading should not take longer than 5 seconds
2152 * and thus just timeout if that happens and fail the setup
2155 err = btintel_download_wait(hdev, calltime, 5000);
2156 if (err == -ETIMEDOUT)
2157 btintel_reset_to_bootloader(hdev);
2160 release_firmware(fw);
2164 static int btintel_get_codec_config_data(struct hci_dev *hdev,
2165 __u8 link, struct bt_codec *codec,
2166 __u8 *ven_len, __u8 **ven_data)
2170 if (!ven_data || !ven_len)
2176 if (link != ESCO_LINK) {
2177 bt_dev_err(hdev, "Invalid link type(%u)", link);
2181 *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
2187 /* supports only CVSD and mSBC offload codecs */
2188 switch (codec->id) {
2197 bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
2200 /* codec and its capabilities are pre-defined to ids
2201 * preset id = 0x00 represents CVSD codec with sampling rate 8K
2202 * preset id = 0x01 represents mSBC codec with sampling rate 16K
2204 *ven_len = sizeof(__u8);
2213 static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
2215 /* Intel uses 1 as data path id for all the usecases */
2220 static int btintel_configure_offload(struct hci_dev *hdev)
2222 struct sk_buff *skb;
2224 struct intel_offload_use_cases *use_cases;
2226 skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
2228 bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
2230 return PTR_ERR(skb);
2233 if (skb->len < sizeof(*use_cases)) {
2238 use_cases = (void *)skb->data;
2240 if (use_cases->status) {
2241 err = -bt_to_errno(skb->data[0]);
2245 if (use_cases->preset[0] & 0x03) {
2246 hdev->get_data_path_id = btintel_get_data_path_id;
2247 hdev->get_codec_config_data = btintel_get_codec_config_data;
2254 static int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
2255 struct intel_version_tlv *ver)
2260 struct intel_version_tlv new_ver;
2262 bt_dev_dbg(hdev, "");
2264 /* Set the default boot parameter to 0x0 and it is updated to
2265 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2266 * command while downloading the firmware.
2268 boot_param = 0x00000000;
2270 btintel_set_flag(hdev, INTEL_BOOTLOADER);
2272 err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2276 /* check if controller is already having an operational firmware */
2277 if (ver->img_type == 0x03)
2280 err = btintel_boot(hdev, boot_param);
2284 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2286 btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
2287 /* Once the device is running in operational mode, it needs to
2288 * apply the device configuration (DDC) parameters.
2290 * The device can work without DDC parameters, so even if it
2291 * fails to load the file, no need to fail the setup.
2293 btintel_load_ddc_config(hdev, ddcname);
2295 /* Read supported use cases and set callbacks to fetch datapath id */
2296 btintel_configure_offload(hdev);
2298 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2300 /* Read the Intel version information after loading the FW */
2301 err = btintel_read_version_tlv(hdev, &new_ver);
2305 btintel_version_info_tlv(hdev, &new_ver);
2308 /* Set the event mask for Intel specific vendor events. This enables
2309 * a few extra events that are useful during general operation. It
2310 * does not enable any debugging related events.
2312 * The device will function correctly without these events enabled
2313 * and thus no need to fail the setup.
2315 btintel_set_event_mask(hdev, false);
2320 static void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
2322 switch (hw_variant) {
2323 /* Legacy bootloader devices that supports MSFT Extension */
2324 case 0x11: /* JfP */
2325 case 0x12: /* ThP */
2326 case 0x13: /* HrP */
2327 case 0x14: /* CcP */
2328 /* All Intel new genration controllers support the Microsoft vendor
2329 * extension are using 0xFC1E for VsMsftOpCode.
2335 hci_set_msft_opcode(hdev, 0xFC1E);
2343 static int btintel_setup_combined(struct hci_dev *hdev)
2345 const u8 param[1] = { 0xFF };
2346 struct intel_version ver;
2347 struct intel_version_tlv ver_tlv;
2348 struct sk_buff *skb;
2351 BT_DBG("%s", hdev->name);
2353 /* The some controllers have a bug with the first HCI command sent to it
2354 * returning number of completed commands as zero. This would stall the
2355 * command processing in the Bluetooth core.
2357 * As a workaround, send HCI Reset command first which will reset the
2358 * number of completed commands and allow normal command processing
2361 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
2362 * in the SW_RFKILL ON state as a workaround of fixing LED issue during
2363 * the shutdown() procedure, and once the device is in SW_RFKILL ON
2364 * state, the only way to exit out of it is sending the HCI_Reset
2367 if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
2368 btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2369 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
2373 "sending initial HCI reset failed (%ld)",
2375 return PTR_ERR(skb);
2380 /* Starting from TyP device, the command parameter and response are
2381 * changed even though the OCF for HCI_Intel_Read_Version command
2382 * remains same. The legacy devices can handle even if the
2383 * command has a parameter and returns a correct version information.
2384 * So, it uses new format to support both legacy and new format.
2386 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
2388 bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
2390 return PTR_ERR(skb);
2393 /* Check the status */
2395 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
2401 /* Apply the common HCI quirks for Intel device */
2402 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2403 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2404 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2406 /* Set up the quality report callback for Intel devices */
2407 hdev->set_quality_report = btintel_set_quality_report;
2409 /* For Legacy device, check the HW platform value and size */
2410 if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
2411 bt_dev_dbg(hdev, "Read the legacy Intel version information");
2413 memcpy(&ver, skb->data, sizeof(ver));
2415 /* Display version information */
2416 btintel_version_info(hdev, &ver);
2418 /* Check for supported iBT hardware variants of this firmware
2421 * This check has been put in place to ensure correct forward
2422 * compatibility options when newer hardware variants come
2425 switch (ver.hw_variant) {
2427 case 0x08: /* StP */
2428 /* Legacy ROM product */
2429 btintel_set_flag(hdev, INTEL_ROM_LEGACY);
2431 /* Apply the device specific HCI quirks
2433 * WBS for SdP - For the Legacy ROM products, only SdP
2434 * supports the WBS. But the version information is not
2435 * enough to use here because the StP2 and SdP have same
2436 * hw_variant and fw_variant. So, this flag is set by
2437 * the transport driver (btusb) based on the HW info
2440 if (!btintel_test_flag(hdev,
2441 INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
2442 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2444 if (ver.hw_variant == 0x08 && ver.fw_variant == 0x22)
2445 set_bit(HCI_QUIRK_VALID_LE_STATES,
2448 err = btintel_legacy_rom_setup(hdev, &ver);
2450 case 0x0b: /* SfP */
2451 case 0x11: /* JfP */
2452 case 0x12: /* ThP */
2453 case 0x13: /* HrP */
2454 case 0x14: /* CcP */
2455 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2457 case 0x0c: /* WsP */
2458 /* Apply the device specific HCI quirks
2460 * All Legacy bootloader devices support WBS
2462 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2465 /* Setup MSFT Extension support */
2466 btintel_set_msft_opcode(hdev, ver.hw_variant);
2468 err = btintel_bootloader_setup(hdev, &ver);
2471 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2479 /* memset ver_tlv to start with clean state as few fields are exclusive
2480 * to bootloader mode and are not populated in operational mode
2482 memset(&ver_tlv, 0, sizeof(ver_tlv));
2483 /* For TLV type device, parse the tlv data */
2484 err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
2486 bt_dev_err(hdev, "Failed to parse TLV version information");
2490 if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
2491 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2492 INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
2497 /* Check for supported iBT hardware variants of this firmware
2500 * This check has been put in place to ensure correct forward
2501 * compatibility options when newer hardware variants come
2504 switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
2505 case 0x11: /* JfP */
2506 case 0x12: /* ThP */
2507 case 0x13: /* HrP */
2508 case 0x14: /* CcP */
2509 /* Some legacy bootloader devices starting from JfP,
2510 * the operational firmware supports both old and TLV based
2511 * HCI_Intel_Read_Version command based on the command
2514 * For upgrading firmware case, the TLV based version cannot
2515 * be used because the firmware filename for legacy bootloader
2516 * is based on the old format.
2518 * Also, it is not easy to convert TLV based version from the
2519 * legacy version format.
2521 * So, as a workaround for those devices, use the legacy
2522 * HCI_Intel_Read_Version to get the version information and
2523 * run the legacy bootloader setup.
2525 err = btintel_read_version(hdev, &ver);
2529 /* Apply the device specific HCI quirks
2531 * All Legacy bootloader devices support WBS
2533 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2535 /* Set Valid LE States quirk */
2536 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2538 /* Setup MSFT Extension support */
2539 btintel_set_msft_opcode(hdev, ver.hw_variant);
2541 err = btintel_bootloader_setup(hdev, &ver);
2547 /* Display version information of TLV type */
2548 btintel_version_info_tlv(hdev, &ver_tlv);
2550 /* Apply the device specific HCI quirks for TLV based devices
2552 * All TLV based devices support WBS
2554 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2556 /* Valid LE States quirk for GfP */
2557 if (INTEL_HW_VARIANT(ver_tlv.cnvi_bt) == 0x18)
2558 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2560 /* Setup MSFT Extension support */
2561 btintel_set_msft_opcode(hdev,
2562 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2564 err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
2567 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2568 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2578 static int btintel_shutdown_combined(struct hci_dev *hdev)
2580 struct sk_buff *skb;
2583 /* Send HCI Reset to the controller to stop any BT activity which
2584 * were triggered. This will help to save power and maintain the
2585 * sync b/w Host and controller
2587 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2589 bt_dev_err(hdev, "HCI reset during shutdown failed");
2590 return PTR_ERR(skb);
2595 /* Some platforms have an issue with BT LED when the interface is
2596 * down or BT radio is turned off, which takes 5 seconds to BT LED
2597 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
2598 * device in the RFKILL ON state which turns off the BT LED immediately.
2600 if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2601 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2604 bt_dev_err(hdev, "turning off Intel device LED failed");
2613 int btintel_configure_setup(struct hci_dev *hdev)
2615 hdev->manufacturer = 2;
2616 hdev->setup = btintel_setup_combined;
2617 hdev->shutdown = btintel_shutdown_combined;
2618 hdev->hw_error = btintel_hw_error;
2619 hdev->set_diag = btintel_set_diag_combined;
2620 hdev->set_bdaddr = btintel_set_bdaddr;
2624 EXPORT_SYMBOL_GPL(btintel_configure_setup);
2626 void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
2628 const struct intel_bootup *evt = ptr;
2630 if (len != sizeof(*evt))
2633 if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
2634 btintel_wake_up_flag(hdev, INTEL_BOOTING);
2636 EXPORT_SYMBOL_GPL(btintel_bootup);
2638 void btintel_secure_send_result(struct hci_dev *hdev,
2639 const void *ptr, unsigned int len)
2641 const struct intel_secure_send_result *evt = ptr;
2643 if (len != sizeof(*evt))
2647 btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
2649 if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
2650 btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
2651 btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
2653 EXPORT_SYMBOL_GPL(btintel_secure_send_result);
2655 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2656 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
2657 MODULE_VERSION(VERSION);
2658 MODULE_LICENSE("GPL");
2659 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
2660 MODULE_FIRMWARE("intel/ibt-11-5.ddc");
2661 MODULE_FIRMWARE("intel/ibt-12-16.sfi");
2662 MODULE_FIRMWARE("intel/ibt-12-16.ddc");