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 */
454 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
455 INTEL_HW_VARIANT(version->cnvi_bt));
459 switch (version->img_type) {
461 variant = "Bootloader";
462 /* It is required that every single firmware fragment is acknowledged
463 * with a command complete event. If the boot parameters indicate
464 * that this bootloader does not send them, then abort the setup.
466 if (version->limited_cce != 0x00) {
467 bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
468 version->limited_cce);
472 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
473 if (version->sbe_type > 0x01) {
474 bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
479 bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
480 bt_dev_info(hdev, "Secure boot is %s",
481 version->secure_boot ? "enabled" : "disabled");
482 bt_dev_info(hdev, "OTP lock is %s",
483 version->otp_lock ? "enabled" : "disabled");
484 bt_dev_info(hdev, "API lock is %s",
485 version->api_lock ? "enabled" : "disabled");
486 bt_dev_info(hdev, "Debug lock is %s",
487 version->debug_lock ? "enabled" : "disabled");
488 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
489 version->min_fw_build_nn, version->min_fw_build_cw,
490 2000 + version->min_fw_build_yy);
493 variant = "Firmware";
496 bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
500 bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
501 2000 + (version->timestamp >> 8), version->timestamp & 0xff,
502 version->build_type, version->build_num);
507 static int btintel_parse_version_tlv(struct hci_dev *hdev,
508 struct intel_version_tlv *version,
511 /* Consume Command Complete Status field */
514 /* Event parameters contatin multiple TLVs. Read each of them
515 * and only keep the required data. Also, it use existing legacy
516 * version field like hw_platform, hw_variant, and fw_variant
517 * to keep the existing setup flow
520 struct intel_tlv *tlv;
522 /* Make sure skb has a minimum length of the header */
523 if (skb->len < sizeof(*tlv))
526 tlv = (struct intel_tlv *)skb->data;
528 /* Make sure skb has a enough data */
529 if (skb->len < tlv->len + sizeof(*tlv))
533 case INTEL_TLV_CNVI_TOP:
534 version->cnvi_top = get_unaligned_le32(tlv->val);
536 case INTEL_TLV_CNVR_TOP:
537 version->cnvr_top = get_unaligned_le32(tlv->val);
539 case INTEL_TLV_CNVI_BT:
540 version->cnvi_bt = get_unaligned_le32(tlv->val);
542 case INTEL_TLV_CNVR_BT:
543 version->cnvr_bt = get_unaligned_le32(tlv->val);
545 case INTEL_TLV_DEV_REV_ID:
546 version->dev_rev_id = get_unaligned_le16(tlv->val);
548 case INTEL_TLV_IMAGE_TYPE:
549 version->img_type = tlv->val[0];
551 case INTEL_TLV_TIME_STAMP:
552 /* If image type is Operational firmware (0x03), then
553 * running FW Calendar Week and Year information can
554 * be extracted from Timestamp information
556 version->min_fw_build_cw = tlv->val[0];
557 version->min_fw_build_yy = tlv->val[1];
558 version->timestamp = get_unaligned_le16(tlv->val);
560 case INTEL_TLV_BUILD_TYPE:
561 version->build_type = tlv->val[0];
563 case INTEL_TLV_BUILD_NUM:
564 /* If image type is Operational firmware (0x03), then
565 * running FW build number can be extracted from the
568 version->min_fw_build_nn = tlv->val[0];
569 version->build_num = get_unaligned_le32(tlv->val);
571 case INTEL_TLV_SECURE_BOOT:
572 version->secure_boot = tlv->val[0];
574 case INTEL_TLV_OTP_LOCK:
575 version->otp_lock = tlv->val[0];
577 case INTEL_TLV_API_LOCK:
578 version->api_lock = tlv->val[0];
580 case INTEL_TLV_DEBUG_LOCK:
581 version->debug_lock = tlv->val[0];
583 case INTEL_TLV_MIN_FW:
584 version->min_fw_build_nn = tlv->val[0];
585 version->min_fw_build_cw = tlv->val[1];
586 version->min_fw_build_yy = tlv->val[2];
588 case INTEL_TLV_LIMITED_CCE:
589 version->limited_cce = tlv->val[0];
591 case INTEL_TLV_SBE_TYPE:
592 version->sbe_type = tlv->val[0];
594 case INTEL_TLV_OTP_BDADDR:
595 memcpy(&version->otp_bd_addr, tlv->val,
599 /* Ignore rest of information */
602 /* consume the current tlv and move to next*/
603 skb_pull(skb, tlv->len + sizeof(*tlv));
609 static int btintel_read_version_tlv(struct hci_dev *hdev,
610 struct intel_version_tlv *version)
613 const u8 param[1] = { 0xFF };
618 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
620 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
626 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
632 btintel_parse_version_tlv(hdev, version, skb);
638 /* ------- REGMAP IBT SUPPORT ------- */
640 #define IBT_REG_MODE_8BIT 0x00
641 #define IBT_REG_MODE_16BIT 0x01
642 #define IBT_REG_MODE_32BIT 0x02
644 struct regmap_ibt_context {
645 struct hci_dev *hdev;
650 struct ibt_cp_reg_access {
657 struct ibt_rp_reg_access {
663 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
664 void *val, size_t val_size)
666 struct regmap_ibt_context *ctx = context;
667 struct ibt_cp_reg_access cp;
668 struct ibt_rp_reg_access *rp;
672 if (reg_size != sizeof(__le32))
677 cp.mode = IBT_REG_MODE_8BIT;
680 cp.mode = IBT_REG_MODE_16BIT;
683 cp.mode = IBT_REG_MODE_32BIT;
689 /* regmap provides a little-endian formatted addr */
690 cp.addr = *(__le32 *)addr;
693 bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
695 skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
699 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
700 le32_to_cpu(cp.addr), err);
704 if (skb->len != sizeof(*rp) + val_size) {
705 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
706 le32_to_cpu(cp.addr));
711 rp = (struct ibt_rp_reg_access *)skb->data;
713 if (rp->addr != cp.addr) {
714 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
715 le32_to_cpu(rp->addr));
720 memcpy(val, rp->data, val_size);
727 static int regmap_ibt_gather_write(void *context,
728 const void *addr, size_t reg_size,
729 const void *val, size_t val_size)
731 struct regmap_ibt_context *ctx = context;
732 struct ibt_cp_reg_access *cp;
734 int plen = sizeof(*cp) + val_size;
738 if (reg_size != sizeof(__le32))
743 mode = IBT_REG_MODE_8BIT;
746 mode = IBT_REG_MODE_16BIT;
749 mode = IBT_REG_MODE_32BIT;
755 cp = kmalloc(plen, GFP_KERNEL);
759 /* regmap provides a little-endian formatted addr/value */
760 cp->addr = *(__le32 *)addr;
763 memcpy(&cp->data, val, val_size);
765 bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
767 skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
770 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
771 le32_to_cpu(cp->addr), err);
781 static int regmap_ibt_write(void *context, const void *data, size_t count)
783 /* data contains register+value, since we only support 32bit addr,
784 * minimum data size is 4 bytes.
786 if (WARN_ONCE(count < 4, "Invalid register access"))
789 return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
792 static void regmap_ibt_free_context(void *context)
797 static const struct regmap_bus regmap_ibt = {
798 .read = regmap_ibt_read,
799 .write = regmap_ibt_write,
800 .gather_write = regmap_ibt_gather_write,
801 .free_context = regmap_ibt_free_context,
802 .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
803 .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
806 /* Config is the same for all register regions */
807 static const struct regmap_config regmap_ibt_cfg = {
808 .name = "btintel_regmap",
813 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
816 struct regmap_ibt_context *ctx;
818 bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
821 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
823 return ERR_PTR(-ENOMEM);
825 ctx->op_read = opcode_read;
826 ctx->op_write = opcode_write;
829 return regmap_init(&hdev->dev, ®map_ibt, ctx, ®map_ibt_cfg);
831 EXPORT_SYMBOL_GPL(btintel_regmap_init);
833 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
835 struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
838 params.boot_param = cpu_to_le32(boot_param);
840 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), ¶ms,
843 bt_dev_err(hdev, "Failed to send Intel Reset command");
851 EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
853 int btintel_read_boot_params(struct hci_dev *hdev,
854 struct intel_boot_params *params)
858 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
860 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
865 if (skb->len != sizeof(*params)) {
866 bt_dev_err(hdev, "Intel boot parameters size mismatch");
871 memcpy(params, skb->data, sizeof(*params));
875 if (params->status) {
876 bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
878 return -bt_to_errno(params->status);
881 bt_dev_info(hdev, "Device revision is %u",
882 le16_to_cpu(params->dev_revid));
884 bt_dev_info(hdev, "Secure boot is %s",
885 params->secure_boot ? "enabled" : "disabled");
887 bt_dev_info(hdev, "OTP lock is %s",
888 params->otp_lock ? "enabled" : "disabled");
890 bt_dev_info(hdev, "API lock is %s",
891 params->api_lock ? "enabled" : "disabled");
893 bt_dev_info(hdev, "Debug lock is %s",
894 params->debug_lock ? "enabled" : "disabled");
896 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
897 params->min_fw_build_nn, params->min_fw_build_cw,
898 2000 + params->min_fw_build_yy);
902 EXPORT_SYMBOL_GPL(btintel_read_boot_params);
904 static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
905 const struct firmware *fw)
909 /* Start the firmware download transaction with the Init fragment
910 * represented by the 128 bytes of CSS header.
912 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
914 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
918 /* Send the 256 bytes of public key information from the firmware
919 * as the PKey fragment.
921 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
923 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
927 /* Send the 256 bytes of signature information from the firmware
928 * as the Sign fragment.
930 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
932 bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
940 static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
941 const struct firmware *fw)
945 /* Start the firmware download transaction with the Init fragment
946 * represented by the 128 bytes of CSS header.
948 err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
950 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
954 /* Send the 96 bytes of public key information from the firmware
955 * as the PKey fragment.
957 err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
959 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
963 /* Send the 96 bytes of signature information from the firmware
964 * as the Sign fragment
966 err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
968 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
975 static int btintel_download_firmware_payload(struct hci_dev *hdev,
976 const struct firmware *fw,
983 fw_ptr = fw->data + offset;
987 while (fw_ptr - fw->data < fw->size) {
988 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
990 frag_len += sizeof(*cmd) + cmd->plen;
992 /* The parameter length of the secure send command requires
993 * a 4 byte alignment. It happens so that the firmware file
994 * contains proper Intel_NOP commands to align the fragments
997 * Send set of commands with 4 byte alignment from the
998 * firmware data buffer as a single Data fragement.
1000 if (!(frag_len % 4)) {
1001 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1004 "Failed to send firmware data (%d)",
1018 static bool btintel_firmware_version(struct hci_dev *hdev,
1019 u8 num, u8 ww, u8 yy,
1020 const struct firmware *fw,
1027 while (fw_ptr - fw->data < fw->size) {
1028 struct hci_command_hdr *cmd = (void *)(fw_ptr);
1030 /* Each SKU has a different reset parameter to use in the
1031 * HCI_Intel_Reset command and it is embedded in the firmware
1032 * data. So, instead of using static value per SKU, check
1033 * the firmware data and save it for later use.
1035 if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1036 struct cmd_write_boot_params *params;
1038 params = (void *)(fw_ptr + sizeof(*cmd));
1040 *boot_addr = le32_to_cpu(params->boot_addr);
1042 bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
1044 bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1045 params->fw_build_num, params->fw_build_ww,
1046 params->fw_build_yy);
1048 return (num == params->fw_build_num &&
1049 ww == params->fw_build_ww &&
1050 yy == params->fw_build_yy);
1053 fw_ptr += sizeof(*cmd) + cmd->plen;
1059 int btintel_download_firmware(struct hci_dev *hdev,
1060 struct intel_version *ver,
1061 const struct firmware *fw,
1066 /* SfP and WsP don't seem to update the firmware version on file
1067 * so version checking is currently not possible.
1069 switch (ver->hw_variant) {
1070 case 0x0b: /* SfP */
1071 case 0x0c: /* WsP */
1072 /* Skip version checking */
1076 /* Skip download if firmware has the same version */
1077 if (btintel_firmware_version(hdev, ver->fw_build_num,
1078 ver->fw_build_ww, ver->fw_build_yy,
1080 bt_dev_info(hdev, "Firmware already loaded");
1081 /* Return -EALREADY to indicate that the firmware has
1082 * already been loaded.
1088 /* The firmware variant determines if the device is in bootloader
1089 * mode or is running operational firmware. The value 0x06 identifies
1090 * the bootloader and the value 0x23 identifies the operational
1093 * If the firmware version has changed that means it needs to be reset
1094 * to bootloader when operational so the new firmware can be loaded.
1096 if (ver->fw_variant == 0x23)
1099 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1103 return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1105 EXPORT_SYMBOL_GPL(btintel_download_firmware);
1107 static int btintel_download_fw_tlv(struct hci_dev *hdev,
1108 struct intel_version_tlv *ver,
1109 const struct firmware *fw, u32 *boot_param,
1110 u8 hw_variant, u8 sbe_type)
1115 /* Skip download if firmware has the same version */
1116 if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1117 ver->min_fw_build_cw,
1118 ver->min_fw_build_yy,
1120 bt_dev_info(hdev, "Firmware already loaded");
1121 /* Return -EALREADY to indicate that firmware has
1122 * already been loaded.
1127 /* The firmware variant determines if the device is in bootloader
1128 * mode or is running operational firmware. The value 0x01 identifies
1129 * the bootloader and the value 0x03 identifies the operational
1132 * If the firmware version has changed that means it needs to be reset
1133 * to bootloader when operational so the new firmware can be loaded.
1135 if (ver->img_type == 0x03)
1138 /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1139 * only RSA secure boot engine. Hence, the corresponding sfi file will
1140 * have RSA header of 644 bytes followed by Command Buffer.
1142 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1143 * secure boot engine. As a result, the corresponding sfi file will
1144 * have RSA header of 644, ECDSA header of 320 bytes followed by
1147 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1148 * version: RSA(0x00010000) , ECDSA (0x00020000)
1150 css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1151 if (css_header_ver != 0x00010000) {
1152 bt_dev_err(hdev, "Invalid CSS Header version");
1156 if (hw_variant <= 0x14) {
1157 if (sbe_type != 0x00) {
1158 bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1163 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1167 err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1170 } else if (hw_variant >= 0x17) {
1171 /* Check if CSS header for ECDSA follows the RSA header */
1172 if (fw->data[ECDSA_OFFSET] != 0x06)
1175 /* Check if the CSS Header version is ECDSA(0x00020000) */
1176 css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1177 if (css_header_ver != 0x00020000) {
1178 bt_dev_err(hdev, "Invalid CSS Header version");
1182 if (sbe_type == 0x00) {
1183 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1187 err = btintel_download_firmware_payload(hdev, fw,
1188 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1191 } else if (sbe_type == 0x01) {
1192 err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1196 err = btintel_download_firmware_payload(hdev, fw,
1197 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1205 static void btintel_reset_to_bootloader(struct hci_dev *hdev)
1207 struct intel_reset params;
1208 struct sk_buff *skb;
1210 /* Send Intel Reset command. This will result in
1211 * re-enumeration of BT controller.
1213 * Intel Reset parameter description:
1214 * reset_type : 0x00 (Soft reset),
1216 * patch_enable : 0x00 (Do not enable),
1218 * ddc_reload : 0x00 (Do not reload),
1220 * boot_option: 0x00 (Current image),
1221 * 0x01 (Specified boot address)
1222 * boot_param: Boot address
1225 params.reset_type = 0x01;
1226 params.patch_enable = 0x01;
1227 params.ddc_reload = 0x01;
1228 params.boot_option = 0x00;
1229 params.boot_param = cpu_to_le32(0x00000000);
1231 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1232 ¶ms, HCI_INIT_TIMEOUT);
1234 bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1238 bt_dev_info(hdev, "Intel reset sent to retry FW download");
1241 /* Current Intel BT controllers(ThP/JfP) hold the USB reset
1242 * lines for 2ms when it receives Intel Reset in bootloader mode.
1243 * Whereas, the upcoming Intel BT controllers will hold USB reset
1244 * for 150ms. To keep the delay generic, 150ms is chosen here.
1249 static int btintel_read_debug_features(struct hci_dev *hdev,
1250 struct intel_debug_features *features)
1252 struct sk_buff *skb;
1255 /* Intel controller supports two pages, each page is of 128-bit
1256 * feature bit mask. And each bit defines specific feature support
1258 skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1261 bt_dev_err(hdev, "Reading supported features failed (%ld)",
1263 return PTR_ERR(skb);
1266 if (skb->len != (sizeof(features->page1) + 3)) {
1267 bt_dev_err(hdev, "Supported features event size mismatch");
1272 memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1274 /* Read the supported features page2 if required in future.
1280 static int btintel_set_debug_features(struct hci_dev *hdev,
1281 const struct intel_debug_features *features)
1283 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
1285 u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
1286 u8 trace_enable = 0x02;
1287 struct sk_buff *skb;
1290 bt_dev_warn(hdev, "Debug features not read");
1294 if (!(features->page1[0] & 0x3f)) {
1295 bt_dev_info(hdev, "Telemetry exception format not supported");
1299 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1301 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1303 return PTR_ERR(skb);
1307 skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
1309 bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
1311 return PTR_ERR(skb);
1315 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1317 bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
1319 return PTR_ERR(skb);
1323 bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
1324 trace_enable, mask[3]);
1329 static int btintel_reset_debug_features(struct hci_dev *hdev,
1330 const struct intel_debug_features *features)
1332 u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
1334 u8 trace_enable = 0x00;
1335 struct sk_buff *skb;
1338 bt_dev_warn(hdev, "Debug features not read");
1342 if (!(features->page1[0] & 0x3f)) {
1343 bt_dev_info(hdev, "Telemetry exception format not supported");
1347 /* Should stop the trace before writing ddc event mask. */
1348 skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1350 bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
1352 return PTR_ERR(skb);
1356 skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1358 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1360 return PTR_ERR(skb);
1364 bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
1365 trace_enable, mask[3]);
1370 int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
1372 struct intel_debug_features features;
1375 bt_dev_dbg(hdev, "enable %d", enable);
1377 /* Read the Intel supported features and if new exception formats
1378 * supported, need to load the additional DDC config to enable.
1380 err = btintel_read_debug_features(hdev, &features);
1384 /* Set or reset the debug features. */
1386 err = btintel_set_debug_features(hdev, &features);
1388 err = btintel_reset_debug_features(hdev, &features);
1392 EXPORT_SYMBOL_GPL(btintel_set_quality_report);
1394 static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
1395 struct intel_version *ver)
1397 const struct firmware *fw;
1401 snprintf(fwname, sizeof(fwname),
1402 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1403 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1404 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1405 ver->fw_build_ww, ver->fw_build_yy);
1407 ret = request_firmware(&fw, fwname, &hdev->dev);
1409 if (ret == -EINVAL) {
1410 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1415 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1418 /* If the correct firmware patch file is not found, use the
1419 * default firmware patch file instead
1421 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1422 ver->hw_platform, ver->hw_variant);
1423 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1424 bt_dev_err(hdev, "failed to open default fw file: %s",
1430 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1435 static int btintel_legacy_rom_patching(struct hci_dev *hdev,
1436 const struct firmware *fw,
1437 const u8 **fw_ptr, int *disable_patch)
1439 struct sk_buff *skb;
1440 struct hci_command_hdr *cmd;
1441 const u8 *cmd_param;
1442 struct hci_event_hdr *evt = NULL;
1443 const u8 *evt_param = NULL;
1444 int remain = fw->size - (*fw_ptr - fw->data);
1446 /* The first byte indicates the types of the patch command or event.
1447 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1448 * in the current firmware buffer doesn't start with 0x01 or
1449 * the size of remain buffer is smaller than HCI command header,
1450 * the firmware file is corrupted and it should stop the patching
1453 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1454 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1460 cmd = (struct hci_command_hdr *)(*fw_ptr);
1461 *fw_ptr += sizeof(*cmd);
1462 remain -= sizeof(*cmd);
1464 /* Ensure that the remain firmware data is long enough than the length
1465 * of command parameter. If not, the firmware file is corrupted.
1467 if (remain < cmd->plen) {
1468 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1472 /* If there is a command that loads a patch in the firmware
1473 * file, then enable the patch upon success, otherwise just
1474 * disable the manufacturer mode, for example patch activation
1475 * is not required when the default firmware patch file is used
1476 * because there are no patch data to load.
1478 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1481 cmd_param = *fw_ptr;
1482 *fw_ptr += cmd->plen;
1483 remain -= cmd->plen;
1485 /* This reads the expected events when the above command is sent to the
1486 * device. Some vendor commands expects more than one events, for
1487 * example command status event followed by vendor specific event.
1488 * For this case, it only keeps the last expected event. so the command
1489 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1490 * last expected event.
1492 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1496 evt = (struct hci_event_hdr *)(*fw_ptr);
1497 *fw_ptr += sizeof(*evt);
1498 remain -= sizeof(*evt);
1500 if (remain < evt->plen) {
1501 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1505 evt_param = *fw_ptr;
1506 *fw_ptr += evt->plen;
1507 remain -= evt->plen;
1510 /* Every HCI commands in the firmware file has its correspond event.
1511 * If event is not found or remain is smaller than zero, the firmware
1512 * file is corrupted.
1514 if (!evt || !evt_param || remain < 0) {
1515 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1519 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1520 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1522 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1523 cmd->opcode, PTR_ERR(skb));
1524 return PTR_ERR(skb);
1527 /* It ensures that the returned event matches the event data read from
1528 * the firmware file. At fist, it checks the length and then
1529 * the contents of the event.
1531 if (skb->len != evt->plen) {
1532 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1533 le16_to_cpu(cmd->opcode));
1538 if (memcmp(skb->data, evt_param, evt->plen)) {
1539 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1540 le16_to_cpu(cmd->opcode));
1549 static int btintel_legacy_rom_setup(struct hci_dev *hdev,
1550 struct intel_version *ver)
1552 const struct firmware *fw;
1554 int disable_patch, err;
1555 struct intel_version new_ver;
1557 BT_DBG("%s", hdev->name);
1559 /* fw_patch_num indicates the version of patch the device currently
1560 * have. If there is no patch data in the device, it is always 0x00.
1561 * So, if it is other than 0x00, no need to patch the device again.
1563 if (ver->fw_patch_num) {
1565 "Intel device is already patched. patch num: %02x",
1570 /* Opens the firmware patch file based on the firmware version read
1571 * from the controller. If it fails to open the matching firmware
1572 * patch file, it tries to open the default firmware patch file.
1573 * If no patch file is found, allow the device to operate without
1576 fw = btintel_legacy_rom_get_fw(hdev, ver);
1581 /* Enable the manufacturer mode of the controller.
1582 * Only while this mode is enabled, the driver can download the
1583 * firmware patch data and configuration parameters.
1585 err = btintel_enter_mfg(hdev);
1587 release_firmware(fw);
1593 /* The firmware data file consists of list of Intel specific HCI
1594 * commands and its expected events. The first byte indicates the
1595 * type of the message, either HCI command or HCI event.
1597 * It reads the command and its expected event from the firmware file,
1598 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1599 * the returned event is compared with the event read from the firmware
1600 * file and it will continue until all the messages are downloaded to
1603 * Once the firmware patching is completed successfully,
1604 * the manufacturer mode is disabled with reset and activating the
1607 * If the firmware patching fails, the manufacturer mode is
1608 * disabled with reset and deactivating the patch.
1610 * If the default patch file is used, no reset is done when disabling
1613 while (fw->size > fw_ptr - fw->data) {
1616 ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
1619 goto exit_mfg_deactivate;
1622 release_firmware(fw);
1625 goto exit_mfg_disable;
1627 /* Patching completed successfully and disable the manufacturer mode
1628 * with reset and activate the downloaded firmware patches.
1630 err = btintel_exit_mfg(hdev, true, true);
1634 /* Need build number for downloaded fw patches in
1635 * every power-on boot
1637 err = btintel_read_version(hdev, &new_ver);
1641 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
1642 new_ver.fw_patch_num);
1647 /* Disable the manufacturer mode without reset */
1648 err = btintel_exit_mfg(hdev, false, false);
1652 bt_dev_info(hdev, "Intel firmware patch completed");
1656 exit_mfg_deactivate:
1657 release_firmware(fw);
1659 /* Patching failed. Disable the manufacturer mode with reset and
1660 * deactivate the downloaded firmware patches.
1662 err = btintel_exit_mfg(hdev, true, false);
1666 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1669 /* Set the event mask for Intel specific vendor events. This enables
1670 * a few extra events that are useful during general operation.
1672 btintel_set_event_mask_mfg(hdev, false);
1674 btintel_check_bdaddr(hdev);
1679 static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1681 ktime_t delta, rettime;
1682 unsigned long long duration;
1685 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1687 bt_dev_info(hdev, "Waiting for firmware download to complete");
1689 err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
1691 msecs_to_jiffies(msec));
1692 if (err == -EINTR) {
1693 bt_dev_err(hdev, "Firmware loading interrupted");
1698 bt_dev_err(hdev, "Firmware loading timeout");
1702 if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
1703 bt_dev_err(hdev, "Firmware loading failed");
1707 rettime = ktime_get();
1708 delta = ktime_sub(rettime, calltime);
1709 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1711 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
1716 static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1718 ktime_t delta, rettime;
1719 unsigned long long duration;
1722 bt_dev_info(hdev, "Waiting for device to boot");
1724 err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
1726 msecs_to_jiffies(msec));
1727 if (err == -EINTR) {
1728 bt_dev_err(hdev, "Device boot interrupted");
1733 bt_dev_err(hdev, "Device boot timeout");
1737 rettime = ktime_get();
1738 delta = ktime_sub(rettime, calltime);
1739 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1741 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
1746 static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
1751 calltime = ktime_get();
1753 btintel_set_flag(hdev, INTEL_BOOTING);
1755 err = btintel_send_intel_reset(hdev, boot_addr);
1757 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
1758 btintel_reset_to_bootloader(hdev);
1762 /* The bootloader will not indicate when the device is ready. This
1763 * is done by the operational firmware sending bootup notification.
1765 * Booting into operational firmware should not take longer than
1766 * 1 second. However if that happens, then just fail the setup
1767 * since something went wrong.
1769 err = btintel_boot_wait(hdev, calltime, 1000);
1770 if (err == -ETIMEDOUT)
1771 btintel_reset_to_bootloader(hdev);
1776 static int btintel_get_fw_name(struct intel_version *ver,
1777 struct intel_boot_params *params,
1778 char *fw_name, size_t len,
1781 switch (ver->hw_variant) {
1782 case 0x0b: /* SfP */
1783 case 0x0c: /* WsP */
1784 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
1785 le16_to_cpu(ver->hw_variant),
1786 le16_to_cpu(params->dev_revid),
1789 case 0x11: /* JfP */
1790 case 0x12: /* ThP */
1791 case 0x13: /* HrP */
1792 case 0x14: /* CcP */
1793 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
1794 le16_to_cpu(ver->hw_variant),
1795 le16_to_cpu(ver->hw_revision),
1796 le16_to_cpu(ver->fw_revision),
1806 static int btintel_download_fw(struct hci_dev *hdev,
1807 struct intel_version *ver,
1808 struct intel_boot_params *params,
1811 const struct firmware *fw;
1816 if (!ver || !params)
1819 /* The firmware variant determines if the device is in bootloader
1820 * mode or is running operational firmware. The value 0x06 identifies
1821 * the bootloader and the value 0x23 identifies the operational
1824 * When the operational firmware is already present, then only
1825 * the check for valid Bluetooth device address is needed. This
1826 * determines if the device will be added as configured or
1827 * unconfigured controller.
1829 * It is not possible to use the Secure Boot Parameters in this
1830 * case since that command is only available in bootloader mode.
1832 if (ver->fw_variant == 0x23) {
1833 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
1834 btintel_check_bdaddr(hdev);
1836 /* SfP and WsP don't seem to update the firmware version on file
1837 * so version checking is currently possible.
1839 switch (ver->hw_variant) {
1840 case 0x0b: /* SfP */
1841 case 0x0c: /* WsP */
1845 /* Proceed to download to check if the version matches */
1849 /* Read the secure boot parameters to identify the operating
1850 * details of the bootloader.
1852 err = btintel_read_boot_params(hdev, params);
1856 /* It is required that every single firmware fragment is acknowledged
1857 * with a command complete event. If the boot parameters indicate
1858 * that this bootloader does not send them, then abort the setup.
1860 if (params->limited_cce != 0x00) {
1861 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
1862 params->limited_cce);
1866 /* If the OTP has no valid Bluetooth device address, then there will
1867 * also be no valid address for the operational firmware.
1869 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
1870 bt_dev_info(hdev, "No device address configured");
1871 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1875 /* With this Intel bootloader only the hardware variant and device
1876 * revision information are used to select the right firmware for SfP
1879 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
1881 * Currently the supported hardware variants are:
1882 * 11 (0x0b) for iBT3.0 (LnP/SfP)
1883 * 12 (0x0c) for iBT3.5 (WsP)
1885 * For ThP/JfP and for future SKU's, the FW name varies based on HW
1886 * variant, HW revision and FW revision, as these are dependent on CNVi
1887 * and RF Combination.
1889 * 17 (0x11) for iBT3.5 (JfP)
1890 * 18 (0x12) for iBT3.5 (ThP)
1892 * The firmware file name for these will be
1893 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
1896 err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
1898 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
1899 /* Firmware has already been loaded */
1900 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1904 bt_dev_err(hdev, "Unsupported Intel firmware naming");
1908 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
1910 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
1911 /* Firmware has already been loaded */
1912 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1916 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
1921 bt_dev_info(hdev, "Found device firmware: %s", fwname);
1923 if (fw->size < 644) {
1924 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
1930 calltime = ktime_get();
1932 btintel_set_flag(hdev, INTEL_DOWNLOADING);
1934 /* Start firmware downloading and get boot parameter */
1935 err = btintel_download_firmware(hdev, ver, fw, boot_param);
1937 if (err == -EALREADY) {
1938 /* Firmware has already been loaded */
1939 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1944 /* When FW download fails, send Intel Reset to retry
1947 btintel_reset_to_bootloader(hdev);
1951 /* Before switching the device into operational mode and with that
1952 * booting the loaded firmware, wait for the bootloader notification
1953 * that all fragments have been successfully received.
1955 * When the event processing receives the notification, then the
1956 * INTEL_DOWNLOADING flag will be cleared.
1958 * The firmware loading should not take longer than 5 seconds
1959 * and thus just timeout if that happens and fail the setup
1962 err = btintel_download_wait(hdev, calltime, 5000);
1963 if (err == -ETIMEDOUT)
1964 btintel_reset_to_bootloader(hdev);
1967 release_firmware(fw);
1971 static int btintel_bootloader_setup(struct hci_dev *hdev,
1972 struct intel_version *ver)
1974 struct intel_version new_ver;
1975 struct intel_boot_params params;
1980 BT_DBG("%s", hdev->name);
1982 /* Set the default boot parameter to 0x0 and it is updated to
1983 * SKU specific boot parameter after reading Intel_Write_Boot_Params
1984 * command while downloading the firmware.
1986 boot_param = 0x00000000;
1988 btintel_set_flag(hdev, INTEL_BOOTLOADER);
1990 err = btintel_download_fw(hdev, ver, ¶ms, &boot_param);
1994 /* controller is already having an operational firmware */
1995 if (ver->fw_variant == 0x23)
1998 err = btintel_boot(hdev, boot_param);
2002 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2004 err = btintel_get_fw_name(ver, ¶ms, ddcname,
2005 sizeof(ddcname), "ddc");
2008 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2010 /* Once the device is running in operational mode, it needs to
2011 * apply the device configuration (DDC) parameters.
2013 * The device can work without DDC parameters, so even if it
2014 * fails to load the file, no need to fail the setup.
2016 btintel_load_ddc_config(hdev, ddcname);
2019 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2021 /* Read the Intel version information after loading the FW */
2022 err = btintel_read_version(hdev, &new_ver);
2026 btintel_version_info(hdev, &new_ver);
2029 /* Set the event mask for Intel specific vendor events. This enables
2030 * a few extra events that are useful during general operation. It
2031 * does not enable any debugging related events.
2033 * The device will function correctly without these events enabled
2034 * and thus no need to fail the setup.
2036 btintel_set_event_mask(hdev, false);
2041 static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
2042 char *fw_name, size_t len,
2045 /* The firmware file name for new generation controllers will be
2046 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2048 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2049 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2050 INTEL_CNVX_TOP_STEP(ver->cnvi_top)),
2051 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2052 INTEL_CNVX_TOP_STEP(ver->cnvr_top)),
2056 static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
2057 struct intel_version_tlv *ver,
2060 const struct firmware *fw;
2065 if (!ver || !boot_param)
2068 /* The firmware variant determines if the device is in bootloader
2069 * mode or is running operational firmware. The value 0x03 identifies
2070 * the bootloader and the value 0x23 identifies the operational
2073 * When the operational firmware is already present, then only
2074 * the check for valid Bluetooth device address is needed. This
2075 * determines if the device will be added as configured or
2076 * unconfigured controller.
2078 * It is not possible to use the Secure Boot Parameters in this
2079 * case since that command is only available in bootloader mode.
2081 if (ver->img_type == 0x03) {
2082 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2083 btintel_check_bdaddr(hdev);
2086 * Check for valid bd address in boot loader mode. Device
2087 * will be marked as unconfigured if empty bd address is
2090 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2091 bt_dev_info(hdev, "No device address configured");
2092 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2096 btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2097 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2099 if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2100 /* Firmware has already been loaded */
2101 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2105 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2111 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2113 if (fw->size < 644) {
2114 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2120 calltime = ktime_get();
2122 btintel_set_flag(hdev, INTEL_DOWNLOADING);
2124 /* Start firmware downloading and get boot parameter */
2125 err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
2126 INTEL_HW_VARIANT(ver->cnvi_bt),
2129 if (err == -EALREADY) {
2130 /* Firmware has already been loaded */
2131 btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2136 /* When FW download fails, send Intel Reset to retry
2139 btintel_reset_to_bootloader(hdev);
2143 /* Before switching the device into operational mode and with that
2144 * booting the loaded firmware, wait for the bootloader notification
2145 * that all fragments have been successfully received.
2147 * When the event processing receives the notification, then the
2148 * BTUSB_DOWNLOADING flag will be cleared.
2150 * The firmware loading should not take longer than 5 seconds
2151 * and thus just timeout if that happens and fail the setup
2154 err = btintel_download_wait(hdev, calltime, 5000);
2155 if (err == -ETIMEDOUT)
2156 btintel_reset_to_bootloader(hdev);
2159 release_firmware(fw);
2163 static int btintel_get_codec_config_data(struct hci_dev *hdev,
2164 __u8 link, struct bt_codec *codec,
2165 __u8 *ven_len, __u8 **ven_data)
2169 if (!ven_data || !ven_len)
2175 if (link != ESCO_LINK) {
2176 bt_dev_err(hdev, "Invalid link type(%u)", link);
2180 *ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
2186 /* supports only CVSD and mSBC offload codecs */
2187 switch (codec->id) {
2196 bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
2199 /* codec and its capabilities are pre-defined to ids
2200 * preset id = 0x00 represents CVSD codec with sampling rate 8K
2201 * preset id = 0x01 represents mSBC codec with sampling rate 16K
2203 *ven_len = sizeof(__u8);
2212 static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
2214 /* Intel uses 1 as data path id for all the usecases */
2219 static int btintel_configure_offload(struct hci_dev *hdev)
2221 struct sk_buff *skb;
2223 struct intel_offload_use_cases *use_cases;
2225 skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
2227 bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
2229 return PTR_ERR(skb);
2232 if (skb->len < sizeof(*use_cases)) {
2237 use_cases = (void *)skb->data;
2239 if (use_cases->status) {
2240 err = -bt_to_errno(skb->data[0]);
2244 if (use_cases->preset[0] & 0x03) {
2245 hdev->get_data_path_id = btintel_get_data_path_id;
2246 hdev->get_codec_config_data = btintel_get_codec_config_data;
2253 static int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
2254 struct intel_version_tlv *ver)
2259 struct intel_version_tlv new_ver;
2261 bt_dev_dbg(hdev, "");
2263 /* Set the default boot parameter to 0x0 and it is updated to
2264 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2265 * command while downloading the firmware.
2267 boot_param = 0x00000000;
2269 btintel_set_flag(hdev, INTEL_BOOTLOADER);
2271 err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2275 /* check if controller is already having an operational firmware */
2276 if (ver->img_type == 0x03)
2279 err = btintel_boot(hdev, boot_param);
2283 btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2285 btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
2286 /* Once the device is running in operational mode, it needs to
2287 * apply the device configuration (DDC) parameters.
2289 * The device can work without DDC parameters, so even if it
2290 * fails to load the file, no need to fail the setup.
2292 btintel_load_ddc_config(hdev, ddcname);
2294 /* Read supported use cases and set callbacks to fetch datapath id */
2295 btintel_configure_offload(hdev);
2297 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2299 /* Read the Intel version information after loading the FW */
2300 err = btintel_read_version_tlv(hdev, &new_ver);
2304 btintel_version_info_tlv(hdev, &new_ver);
2307 /* Set the event mask for Intel specific vendor events. This enables
2308 * a few extra events that are useful during general operation. It
2309 * does not enable any debugging related events.
2311 * The device will function correctly without these events enabled
2312 * and thus no need to fail the setup.
2314 btintel_set_event_mask(hdev, false);
2319 static void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
2321 switch (hw_variant) {
2322 /* Legacy bootloader devices that supports MSFT Extension */
2323 case 0x11: /* JfP */
2324 case 0x12: /* ThP */
2325 case 0x13: /* HrP */
2326 case 0x14: /* CcP */
2327 /* All Intel new genration controllers support the Microsoft vendor
2328 * extension are using 0xFC1E for VsMsftOpCode.
2333 hci_set_msft_opcode(hdev, 0xFC1E);
2341 static int btintel_setup_combined(struct hci_dev *hdev)
2343 const u8 param[1] = { 0xFF };
2344 struct intel_version ver;
2345 struct intel_version_tlv ver_tlv;
2346 struct sk_buff *skb;
2349 BT_DBG("%s", hdev->name);
2351 /* The some controllers have a bug with the first HCI command sent to it
2352 * returning number of completed commands as zero. This would stall the
2353 * command processing in the Bluetooth core.
2355 * As a workaround, send HCI Reset command first which will reset the
2356 * number of completed commands and allow normal command processing
2359 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
2360 * in the SW_RFKILL ON state as a workaround of fixing LED issue during
2361 * the shutdown() procedure, and once the device is in SW_RFKILL ON
2362 * state, the only way to exit out of it is sending the HCI_Reset
2365 if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
2366 btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2367 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
2371 "sending initial HCI reset failed (%ld)",
2373 return PTR_ERR(skb);
2378 /* Starting from TyP device, the command parameter and response are
2379 * changed even though the OCF for HCI_Intel_Read_Version command
2380 * remains same. The legacy devices can handle even if the
2381 * command has a parameter and returns a correct version information.
2382 * So, it uses new format to support both legacy and new format.
2384 skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
2386 bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
2388 return PTR_ERR(skb);
2391 /* Check the status */
2393 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
2399 /* Apply the common HCI quirks for Intel device */
2400 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2401 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2402 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2404 /* Set up the quality report callback for Intel devices */
2405 hdev->set_quality_report = btintel_set_quality_report;
2407 /* For Legacy device, check the HW platform value and size */
2408 if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
2409 bt_dev_dbg(hdev, "Read the legacy Intel version information");
2411 memcpy(&ver, skb->data, sizeof(ver));
2413 /* Display version information */
2414 btintel_version_info(hdev, &ver);
2416 /* Check for supported iBT hardware variants of this firmware
2419 * This check has been put in place to ensure correct forward
2420 * compatibility options when newer hardware variants come
2423 switch (ver.hw_variant) {
2425 case 0x08: /* StP */
2426 /* Legacy ROM product */
2427 btintel_set_flag(hdev, INTEL_ROM_LEGACY);
2429 /* Apply the device specific HCI quirks
2431 * WBS for SdP - For the Legacy ROM products, only SdP
2432 * supports the WBS. But the version information is not
2433 * enough to use here because the StP2 and SdP have same
2434 * hw_variant and fw_variant. So, this flag is set by
2435 * the transport driver (btusb) based on the HW info
2438 if (!btintel_test_flag(hdev,
2439 INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
2440 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2443 err = btintel_legacy_rom_setup(hdev, &ver);
2445 case 0x0b: /* SfP */
2446 case 0x0c: /* WsP */
2447 case 0x11: /* JfP */
2448 case 0x12: /* ThP */
2449 case 0x13: /* HrP */
2450 case 0x14: /* CcP */
2451 /* Apply the device specific HCI quirks
2453 * All Legacy bootloader devices support WBS
2455 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2458 /* Valid LE States quirk for JfP/ThP familiy */
2459 if (ver.hw_variant == 0x11 || ver.hw_variant == 0x12)
2460 set_bit(HCI_QUIRK_VALID_LE_STATES,
2463 /* Setup MSFT Extension support */
2464 btintel_set_msft_opcode(hdev, ver.hw_variant);
2466 err = btintel_bootloader_setup(hdev, &ver);
2469 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2477 /* memset ver_tlv to start with clean state as few fields are exclusive
2478 * to bootloader mode and are not populated in operational mode
2480 memset(&ver_tlv, 0, sizeof(ver_tlv));
2481 /* For TLV type device, parse the tlv data */
2482 err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
2484 bt_dev_err(hdev, "Failed to parse TLV version information");
2488 if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
2489 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2490 INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
2495 /* Check for supported iBT hardware variants of this firmware
2498 * This check has been put in place to ensure correct forward
2499 * compatibility options when newer hardware variants come
2502 switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
2503 case 0x11: /* JfP */
2504 case 0x12: /* ThP */
2505 case 0x13: /* HrP */
2506 case 0x14: /* CcP */
2507 /* Some legacy bootloader devices starting from JfP,
2508 * the operational firmware supports both old and TLV based
2509 * HCI_Intel_Read_Version command based on the command
2512 * For upgrading firmware case, the TLV based version cannot
2513 * be used because the firmware filename for legacy bootloader
2514 * is based on the old format.
2516 * Also, it is not easy to convert TLV based version from the
2517 * legacy version format.
2519 * So, as a workaround for those devices, use the legacy
2520 * HCI_Intel_Read_Version to get the version information and
2521 * run the legacy bootloader setup.
2523 err = btintel_read_version(hdev, &ver);
2527 /* Apply the device specific HCI quirks
2529 * All Legacy bootloader devices support WBS
2531 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2533 /* Valid LE States quirk for JfP/ThP familiy */
2534 if (ver.hw_variant == 0x11 || ver.hw_variant == 0x12)
2535 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2537 /* Setup MSFT Extension support */
2538 btintel_set_msft_opcode(hdev, ver.hw_variant);
2540 err = btintel_bootloader_setup(hdev, &ver);
2545 /* Display version information of TLV type */
2546 btintel_version_info_tlv(hdev, &ver_tlv);
2548 /* Apply the device specific HCI quirks for TLV based devices
2550 * All TLV based devices support WBS
2552 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2554 /* Valid LE States quirk for GfP */
2555 if (INTEL_HW_VARIANT(ver_tlv.cnvi_bt) == 0x18)
2556 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2558 /* Setup MSFT Extension support */
2559 btintel_set_msft_opcode(hdev,
2560 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2562 err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
2565 bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2566 INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2576 static int btintel_shutdown_combined(struct hci_dev *hdev)
2578 struct sk_buff *skb;
2581 /* Send HCI Reset to the controller to stop any BT activity which
2582 * were triggered. This will help to save power and maintain the
2583 * sync b/w Host and controller
2585 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2587 bt_dev_err(hdev, "HCI reset during shutdown failed");
2588 return PTR_ERR(skb);
2593 /* Some platforms have an issue with BT LED when the interface is
2594 * down or BT radio is turned off, which takes 5 seconds to BT LED
2595 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
2596 * device in the RFKILL ON state which turns off the BT LED immediately.
2598 if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2599 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2602 bt_dev_err(hdev, "turning off Intel device LED failed");
2611 int btintel_configure_setup(struct hci_dev *hdev)
2613 hdev->manufacturer = 2;
2614 hdev->setup = btintel_setup_combined;
2615 hdev->shutdown = btintel_shutdown_combined;
2616 hdev->hw_error = btintel_hw_error;
2617 hdev->set_diag = btintel_set_diag_combined;
2618 hdev->set_bdaddr = btintel_set_bdaddr;
2622 EXPORT_SYMBOL_GPL(btintel_configure_setup);
2624 void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
2626 const struct intel_bootup *evt = ptr;
2628 if (len != sizeof(*evt))
2631 if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
2632 btintel_wake_up_flag(hdev, INTEL_BOOTING);
2634 EXPORT_SYMBOL_GPL(btintel_bootup);
2636 void btintel_secure_send_result(struct hci_dev *hdev,
2637 const void *ptr, unsigned int len)
2639 const struct intel_secure_send_result *evt = ptr;
2641 if (len != sizeof(*evt))
2645 btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
2647 if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
2648 btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
2649 btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
2651 EXPORT_SYMBOL_GPL(btintel_secure_send_result);
2654 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
2655 MODULE_VERSION(VERSION);
2656 MODULE_LICENSE("GPL");
2657 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
2658 MODULE_FIRMWARE("intel/ibt-11-5.ddc");
2659 MODULE_FIRMWARE("intel/ibt-12-16.sfi");
2660 MODULE_FIRMWARE("intel/ibt-12-16.ddc");