Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *
   4 *  Bluetooth support for Intel devices
   5 *
   6 *  Copyright (C) 2015  Intel Corporation
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/firmware.h>
  11#include <linux/regmap.h>
  12#include <linux/acpi.h>
  13#include <acpi/acpi_bus.h>
  14#include <asm/unaligned.h>
 
  15
  16#include <net/bluetooth/bluetooth.h>
  17#include <net/bluetooth/hci_core.h>
  18
  19#include "btintel.h"
  20
  21#define VERSION "0.1"
  22
  23#define BDADDR_INTEL		(&(bdaddr_t){{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
  24#define RSA_HEADER_LEN		644
  25#define CSS_HEADER_OFFSET	8
  26#define ECDSA_OFFSET		644
  27#define ECDSA_HEADER_LEN	320
  28
  29#define BTINTEL_PPAG_NAME   "PPAG"
  30
  31enum {
  32	DSM_SET_WDISABLE2_DELAY = 1,
  33	DSM_SET_RESET_METHOD = 3,
  34};
  35
  36/* structure to store the PPAG data read from ACPI table */
  37struct btintel_ppag {
  38	u32	domain;
  39	u32     mode;
  40	acpi_status status;
  41	struct hci_dev *hdev;
  42};
  43
  44#define CMD_WRITE_BOOT_PARAMS	0xfc0e
  45struct cmd_write_boot_params {
  46	__le32 boot_addr;
  47	u8  fw_build_num;
  48	u8  fw_build_ww;
  49	u8  fw_build_yy;
  50} __packed;
  51
  52static struct {
  53	const char *driver_name;
  54	u8         hw_variant;
  55	u32        fw_build_num;
  56} coredump_info;
  57
  58static const guid_t btintel_guid_dsm =
  59	GUID_INIT(0xaa10f4e0, 0x81ac, 0x4233,
  60		  0xab, 0xf6, 0x3b, 0x2a, 0xc5, 0x0e, 0x28, 0xd9);
  61
  62int btintel_check_bdaddr(struct hci_dev *hdev)
  63{
  64	struct hci_rp_read_bd_addr *bda;
  65	struct sk_buff *skb;
  66
  67	skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
  68			     HCI_INIT_TIMEOUT);
  69	if (IS_ERR(skb)) {
  70		int err = PTR_ERR(skb);
  71		bt_dev_err(hdev, "Reading Intel device address failed (%d)",
  72			   err);
  73		return err;
  74	}
  75
  76	if (skb->len != sizeof(*bda)) {
  77		bt_dev_err(hdev, "Intel device address length mismatch");
  78		kfree_skb(skb);
  79		return -EIO;
  80	}
  81
  82	bda = (struct hci_rp_read_bd_addr *)skb->data;
  83
  84	/* For some Intel based controllers, the default Bluetooth device
  85	 * address 00:03:19:9E:8B:00 can be found. These controllers are
  86	 * fully operational, but have the danger of duplicate addresses
  87	 * and that in turn can cause problems with Bluetooth operation.
  88	 */
  89	if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
  90		bt_dev_err(hdev, "Found Intel default device address (%pMR)",
  91			   &bda->bdaddr);
  92		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  93	}
  94
  95	kfree_skb(skb);
  96
  97	return 0;
  98}
  99EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
 100
 101int btintel_enter_mfg(struct hci_dev *hdev)
 102{
 103	static const u8 param[] = { 0x01, 0x00 };
 104	struct sk_buff *skb;
 105
 106	skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
 107	if (IS_ERR(skb)) {
 108		bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
 109			   PTR_ERR(skb));
 110		return PTR_ERR(skb);
 111	}
 112	kfree_skb(skb);
 113
 114	return 0;
 115}
 116EXPORT_SYMBOL_GPL(btintel_enter_mfg);
 117
 118int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
 119{
 120	u8 param[] = { 0x00, 0x00 };
 121	struct sk_buff *skb;
 122
 123	/* The 2nd command parameter specifies the manufacturing exit method:
 124	 * 0x00: Just disable the manufacturing mode (0x00).
 125	 * 0x01: Disable manufacturing mode and reset with patches deactivated.
 126	 * 0x02: Disable manufacturing mode and reset with patches activated.
 127	 */
 128	if (reset)
 129		param[1] |= patched ? 0x02 : 0x01;
 130
 131	skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
 132	if (IS_ERR(skb)) {
 133		bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
 134			   PTR_ERR(skb));
 135		return PTR_ERR(skb);
 136	}
 137	kfree_skb(skb);
 138
 139	return 0;
 140}
 141EXPORT_SYMBOL_GPL(btintel_exit_mfg);
 142
 143int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
 144{
 145	struct sk_buff *skb;
 146	int err;
 147
 148	skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
 149	if (IS_ERR(skb)) {
 150		err = PTR_ERR(skb);
 151		bt_dev_err(hdev, "Changing Intel device address failed (%d)",
 152			   err);
 153		return err;
 154	}
 155	kfree_skb(skb);
 156
 157	return 0;
 158}
 159EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
 160
 161static int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
 162{
 163	u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 164	struct sk_buff *skb;
 165	int err;
 166
 167	if (debug)
 168		mask[1] |= 0x62;
 169
 170	skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
 171	if (IS_ERR(skb)) {
 172		err = PTR_ERR(skb);
 173		bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
 174		return err;
 175	}
 176	kfree_skb(skb);
 177
 178	return 0;
 179}
 180
 181int btintel_set_diag(struct hci_dev *hdev, bool enable)
 182{
 183	struct sk_buff *skb;
 184	u8 param[3];
 185	int err;
 186
 187	if (enable) {
 188		param[0] = 0x03;
 189		param[1] = 0x03;
 190		param[2] = 0x03;
 191	} else {
 192		param[0] = 0x00;
 193		param[1] = 0x00;
 194		param[2] = 0x00;
 195	}
 196
 197	skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
 198	if (IS_ERR(skb)) {
 199		err = PTR_ERR(skb);
 200		if (err == -ENODATA)
 201			goto done;
 202		bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
 203			   err);
 204		return err;
 205	}
 206	kfree_skb(skb);
 207
 208done:
 209	btintel_set_event_mask(hdev, enable);
 210	return 0;
 211}
 212EXPORT_SYMBOL_GPL(btintel_set_diag);
 213
 214static int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
 215{
 216	int err, ret;
 217
 218	err = btintel_enter_mfg(hdev);
 219	if (err)
 220		return err;
 221
 222	ret = btintel_set_diag(hdev, enable);
 223
 224	err = btintel_exit_mfg(hdev, false, false);
 225	if (err)
 226		return err;
 227
 228	return ret;
 229}
 230
 231static int btintel_set_diag_combined(struct hci_dev *hdev, bool enable)
 232{
 233	int ret;
 234
 235	/* Legacy ROM device needs to be in the manufacturer mode to apply
 236	 * diagnostic setting
 237	 *
 238	 * This flag is set after reading the Intel version.
 239	 */
 240	if (btintel_test_flag(hdev, INTEL_ROM_LEGACY))
 241		ret = btintel_set_diag_mfg(hdev, enable);
 242	else
 243		ret = btintel_set_diag(hdev, enable);
 244
 245	return ret;
 246}
 247
 248static void btintel_hw_error(struct hci_dev *hdev, u8 code)
 249{
 250	struct sk_buff *skb;
 251	u8 type = 0x00;
 252
 253	bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
 254
 255	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
 256	if (IS_ERR(skb)) {
 257		bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
 258			   PTR_ERR(skb));
 259		return;
 260	}
 261	kfree_skb(skb);
 262
 263	skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
 264	if (IS_ERR(skb)) {
 265		bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
 266			   PTR_ERR(skb));
 267		return;
 268	}
 269
 270	if (skb->len != 13) {
 271		bt_dev_err(hdev, "Exception info size mismatch");
 272		kfree_skb(skb);
 273		return;
 274	}
 275
 276	bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
 277
 278	kfree_skb(skb);
 279}
 
 280
 281int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
 282{
 283	const char *variant;
 284
 285	/* The hardware platform number has a fixed value of 0x37 and
 286	 * for now only accept this single value.
 287	 */
 288	if (ver->hw_platform != 0x37) {
 289		bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
 290			   ver->hw_platform);
 291		return -EINVAL;
 292	}
 293
 294	/* Check for supported iBT hardware variants of this firmware
 295	 * loading method.
 296	 *
 297	 * This check has been put in place to ensure correct forward
 298	 * compatibility options when newer hardware variants come along.
 299	 */
 300	switch (ver->hw_variant) {
 301	case 0x07:	/* WP - Legacy ROM */
 302	case 0x08:	/* StP - Legacy ROM */
 303	case 0x0b:      /* SfP */
 304	case 0x0c:      /* WsP */
 305	case 0x11:      /* JfP */
 306	case 0x12:      /* ThP */
 307	case 0x13:      /* HrP */
 308	case 0x14:      /* CcP */
 309		break;
 310	default:
 311		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
 312			   ver->hw_variant);
 313		return -EINVAL;
 314	}
 315
 316	switch (ver->fw_variant) {
 317	case 0x01:
 318		variant = "Legacy ROM 2.5";
 319		break;
 320	case 0x06:
 321		variant = "Bootloader";
 322		break;
 323	case 0x22:
 324		variant = "Legacy ROM 2.x";
 325		break;
 326	case 0x23:
 327		variant = "Firmware";
 328		break;
 329	default:
 330		bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
 331		return -EINVAL;
 332	}
 333
 334	coredump_info.hw_variant = ver->hw_variant;
 335	coredump_info.fw_build_num = ver->fw_build_num;
 336
 337	bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
 338		    variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
 339		    ver->fw_build_num, ver->fw_build_ww,
 340		    2000 + ver->fw_build_yy);
 341
 342	return 0;
 343}
 344EXPORT_SYMBOL_GPL(btintel_version_info);
 345
 346static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
 347			       const void *param)
 348{
 349	while (plen > 0) {
 350		struct sk_buff *skb;
 351		u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
 352
 353		cmd_param[0] = fragment_type;
 354		memcpy(cmd_param + 1, param, fragment_len);
 355
 356		skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
 357				     cmd_param, HCI_INIT_TIMEOUT);
 358		if (IS_ERR(skb))
 359			return PTR_ERR(skb);
 360
 361		kfree_skb(skb);
 362
 363		plen -= fragment_len;
 364		param += fragment_len;
 365	}
 366
 367	return 0;
 368}
 369
 370int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
 371{
 372	const struct firmware *fw;
 373	struct sk_buff *skb;
 374	const u8 *fw_ptr;
 375	int err;
 376
 377	err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
 378	if (err < 0) {
 379		bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
 380			   ddc_name, err);
 381		return err;
 382	}
 383
 384	bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
 385
 386	fw_ptr = fw->data;
 387
 388	/* DDC file contains one or more DDC structure which has
 389	 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
 390	 */
 391	while (fw->size > fw_ptr - fw->data) {
 392		u8 cmd_plen = fw_ptr[0] + sizeof(u8);
 393
 394		skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
 395				     HCI_INIT_TIMEOUT);
 396		if (IS_ERR(skb)) {
 397			bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
 398				   PTR_ERR(skb));
 399			release_firmware(fw);
 400			return PTR_ERR(skb);
 401		}
 402
 403		fw_ptr += cmd_plen;
 404		kfree_skb(skb);
 405	}
 406
 407	release_firmware(fw);
 408
 409	bt_dev_info(hdev, "Applying Intel DDC parameters completed");
 410
 411	return 0;
 412}
 413EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
 414
 415int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
 416{
 417	int err, ret;
 418
 419	err = btintel_enter_mfg(hdev);
 420	if (err)
 421		return err;
 422
 423	ret = btintel_set_event_mask(hdev, debug);
 424
 425	err = btintel_exit_mfg(hdev, false, false);
 426	if (err)
 427		return err;
 428
 429	return ret;
 430}
 431EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
 432
 433int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
 434{
 435	struct sk_buff *skb;
 436
 437	skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
 438	if (IS_ERR(skb)) {
 439		bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
 440			   PTR_ERR(skb));
 441		return PTR_ERR(skb);
 442	}
 443
 444	if (skb->len != sizeof(*ver)) {
 445		bt_dev_err(hdev, "Intel version event size mismatch");
 446		kfree_skb(skb);
 447		return -EILSEQ;
 448	}
 449
 450	memcpy(ver, skb->data, sizeof(*ver));
 451
 452	kfree_skb(skb);
 453
 454	return 0;
 455}
 456EXPORT_SYMBOL_GPL(btintel_read_version);
 457
 458static int btintel_version_info_tlv(struct hci_dev *hdev,
 459				    struct intel_version_tlv *version)
 460{
 461	const char *variant;
 462
 463	/* The hardware platform number has a fixed value of 0x37 and
 464	 * for now only accept this single value.
 465	 */
 466	if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
 467		bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
 468			   INTEL_HW_PLATFORM(version->cnvi_bt));
 469		return -EINVAL;
 470	}
 471
 472	/* Check for supported iBT hardware variants of this firmware
 473	 * loading method.
 474	 *
 475	 * This check has been put in place to ensure correct forward
 476	 * compatibility options when newer hardware variants come along.
 477	 */
 478	switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
 479	case 0x17:	/* TyP */
 480	case 0x18:	/* Slr */
 481	case 0x19:	/* Slr-F */
 482	case 0x1b:      /* Mgr */
 483	case 0x1c:	/* Gale Peak (GaP) */
 
 
 484		break;
 485	default:
 486		bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
 487			   INTEL_HW_VARIANT(version->cnvi_bt));
 488		return -EINVAL;
 489	}
 490
 491	switch (version->img_type) {
 492	case 0x01:
 493		variant = "Bootloader";
 494		/* It is required that every single firmware fragment is acknowledged
 495		 * with a command complete event. If the boot parameters indicate
 496		 * that this bootloader does not send them, then abort the setup.
 497		 */
 498		if (version->limited_cce != 0x00) {
 499			bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
 500				   version->limited_cce);
 501			return -EINVAL;
 502		}
 503
 504		/* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
 505		if (version->sbe_type > 0x01) {
 506			bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
 507				   version->sbe_type);
 508			return -EINVAL;
 509		}
 510
 511		bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
 512		bt_dev_info(hdev, "Secure boot is %s",
 513			    version->secure_boot ? "enabled" : "disabled");
 514		bt_dev_info(hdev, "OTP lock is %s",
 515			    version->otp_lock ? "enabled" : "disabled");
 516		bt_dev_info(hdev, "API lock is %s",
 517			    version->api_lock ? "enabled" : "disabled");
 518		bt_dev_info(hdev, "Debug lock is %s",
 519			    version->debug_lock ? "enabled" : "disabled");
 520		bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
 521			    version->min_fw_build_nn, version->min_fw_build_cw,
 522			    2000 + version->min_fw_build_yy);
 523		break;
 524	case 0x03:
 
 
 
 525		variant = "Firmware";
 526		break;
 527	default:
 528		bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
 529		return -EINVAL;
 530	}
 531
 532	coredump_info.hw_variant = INTEL_HW_VARIANT(version->cnvi_bt);
 533	coredump_info.fw_build_num = version->build_num;
 534
 535	bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
 536		    2000 + (version->timestamp >> 8), version->timestamp & 0xff,
 537		    version->build_type, version->build_num);
 538	if (version->img_type == 0x03)
 539		bt_dev_info(hdev, "Firmware SHA1: 0x%8.8x", version->git_sha1);
 540
 541	return 0;
 542}
 
 543
 544static int btintel_parse_version_tlv(struct hci_dev *hdev,
 545				     struct intel_version_tlv *version,
 546				     struct sk_buff *skb)
 547{
 548	/* Consume Command Complete Status field */
 549	skb_pull(skb, 1);
 550
 551	/* Event parameters contatin multiple TLVs. Read each of them
 552	 * and only keep the required data. Also, it use existing legacy
 553	 * version field like hw_platform, hw_variant, and fw_variant
 554	 * to keep the existing setup flow
 555	 */
 556	while (skb->len) {
 557		struct intel_tlv *tlv;
 558
 559		/* Make sure skb has a minimum length of the header */
 560		if (skb->len < sizeof(*tlv))
 561			return -EINVAL;
 562
 563		tlv = (struct intel_tlv *)skb->data;
 564
 565		/* Make sure skb has a enough data */
 566		if (skb->len < tlv->len + sizeof(*tlv))
 567			return -EINVAL;
 568
 569		switch (tlv->type) {
 570		case INTEL_TLV_CNVI_TOP:
 571			version->cnvi_top = get_unaligned_le32(tlv->val);
 572			break;
 573		case INTEL_TLV_CNVR_TOP:
 574			version->cnvr_top = get_unaligned_le32(tlv->val);
 575			break;
 576		case INTEL_TLV_CNVI_BT:
 577			version->cnvi_bt = get_unaligned_le32(tlv->val);
 578			break;
 579		case INTEL_TLV_CNVR_BT:
 580			version->cnvr_bt = get_unaligned_le32(tlv->val);
 581			break;
 582		case INTEL_TLV_DEV_REV_ID:
 583			version->dev_rev_id = get_unaligned_le16(tlv->val);
 584			break;
 585		case INTEL_TLV_IMAGE_TYPE:
 586			version->img_type = tlv->val[0];
 587			break;
 588		case INTEL_TLV_TIME_STAMP:
 589			/* If image type is Operational firmware (0x03), then
 590			 * running FW Calendar Week and Year information can
 591			 * be extracted from Timestamp information
 592			 */
 593			version->min_fw_build_cw = tlv->val[0];
 594			version->min_fw_build_yy = tlv->val[1];
 595			version->timestamp = get_unaligned_le16(tlv->val);
 596			break;
 597		case INTEL_TLV_BUILD_TYPE:
 598			version->build_type = tlv->val[0];
 599			break;
 600		case INTEL_TLV_BUILD_NUM:
 601			/* If image type is Operational firmware (0x03), then
 602			 * running FW build number can be extracted from the
 603			 * Build information
 604			 */
 605			version->min_fw_build_nn = tlv->val[0];
 606			version->build_num = get_unaligned_le32(tlv->val);
 607			break;
 608		case INTEL_TLV_SECURE_BOOT:
 609			version->secure_boot = tlv->val[0];
 610			break;
 611		case INTEL_TLV_OTP_LOCK:
 612			version->otp_lock = tlv->val[0];
 613			break;
 614		case INTEL_TLV_API_LOCK:
 615			version->api_lock = tlv->val[0];
 616			break;
 617		case INTEL_TLV_DEBUG_LOCK:
 618			version->debug_lock = tlv->val[0];
 619			break;
 620		case INTEL_TLV_MIN_FW:
 621			version->min_fw_build_nn = tlv->val[0];
 622			version->min_fw_build_cw = tlv->val[1];
 623			version->min_fw_build_yy = tlv->val[2];
 624			break;
 625		case INTEL_TLV_LIMITED_CCE:
 626			version->limited_cce = tlv->val[0];
 627			break;
 628		case INTEL_TLV_SBE_TYPE:
 629			version->sbe_type = tlv->val[0];
 630			break;
 631		case INTEL_TLV_OTP_BDADDR:
 632			memcpy(&version->otp_bd_addr, tlv->val,
 633							sizeof(bdaddr_t));
 634			break;
 635		case INTEL_TLV_GIT_SHA1:
 636			version->git_sha1 = get_unaligned_le32(tlv->val);
 637			break;
 
 
 
 
 638		default:
 639			/* Ignore rest of information */
 640			break;
 641		}
 642		/* consume the current tlv and move to next*/
 643		skb_pull(skb, tlv->len + sizeof(*tlv));
 644	}
 645
 646	return 0;
 647}
 
 648
 649static int btintel_read_version_tlv(struct hci_dev *hdev,
 650				    struct intel_version_tlv *version)
 651{
 652	struct sk_buff *skb;
 653	const u8 param[1] = { 0xFF };
 654
 655	if (!version)
 656		return -EINVAL;
 657
 658	skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
 659	if (IS_ERR(skb)) {
 660		bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
 661			   PTR_ERR(skb));
 662		return PTR_ERR(skb);
 663	}
 664
 665	if (skb->data[0]) {
 666		bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
 667			   skb->data[0]);
 668		kfree_skb(skb);
 669		return -EIO;
 670	}
 671
 672	btintel_parse_version_tlv(hdev, version, skb);
 673
 674	kfree_skb(skb);
 675	return 0;
 676}
 677
 678/* ------- REGMAP IBT SUPPORT ------- */
 679
 680#define IBT_REG_MODE_8BIT  0x00
 681#define IBT_REG_MODE_16BIT 0x01
 682#define IBT_REG_MODE_32BIT 0x02
 683
 684struct regmap_ibt_context {
 685	struct hci_dev *hdev;
 686	__u16 op_write;
 687	__u16 op_read;
 688};
 689
 690struct ibt_cp_reg_access {
 691	__le32  addr;
 692	__u8    mode;
 693	__u8    len;
 694	__u8    data[];
 695} __packed;
 696
 697struct ibt_rp_reg_access {
 698	__u8    status;
 699	__le32  addr;
 700	__u8    data[];
 701} __packed;
 702
 703static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
 704			   void *val, size_t val_size)
 705{
 706	struct regmap_ibt_context *ctx = context;
 707	struct ibt_cp_reg_access cp;
 708	struct ibt_rp_reg_access *rp;
 709	struct sk_buff *skb;
 710	int err = 0;
 711
 712	if (reg_size != sizeof(__le32))
 713		return -EINVAL;
 714
 715	switch (val_size) {
 716	case 1:
 717		cp.mode = IBT_REG_MODE_8BIT;
 718		break;
 719	case 2:
 720		cp.mode = IBT_REG_MODE_16BIT;
 721		break;
 722	case 4:
 723		cp.mode = IBT_REG_MODE_32BIT;
 724		break;
 725	default:
 726		return -EINVAL;
 727	}
 728
 729	/* regmap provides a little-endian formatted addr */
 730	cp.addr = *(__le32 *)addr;
 731	cp.len = val_size;
 732
 733	bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
 734
 735	skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
 736			   HCI_CMD_TIMEOUT);
 737	if (IS_ERR(skb)) {
 738		err = PTR_ERR(skb);
 739		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
 740			   le32_to_cpu(cp.addr), err);
 741		return err;
 742	}
 743
 744	if (skb->len != sizeof(*rp) + val_size) {
 745		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
 746			   le32_to_cpu(cp.addr));
 747		err = -EINVAL;
 748		goto done;
 749	}
 750
 751	rp = (struct ibt_rp_reg_access *)skb->data;
 752
 753	if (rp->addr != cp.addr) {
 754		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
 755			   le32_to_cpu(rp->addr));
 756		err = -EINVAL;
 757		goto done;
 758	}
 759
 760	memcpy(val, rp->data, val_size);
 761
 762done:
 763	kfree_skb(skb);
 764	return err;
 765}
 766
 767static int regmap_ibt_gather_write(void *context,
 768				   const void *addr, size_t reg_size,
 769				   const void *val, size_t val_size)
 770{
 771	struct regmap_ibt_context *ctx = context;
 772	struct ibt_cp_reg_access *cp;
 773	struct sk_buff *skb;
 774	int plen = sizeof(*cp) + val_size;
 775	u8 mode;
 776	int err = 0;
 777
 778	if (reg_size != sizeof(__le32))
 779		return -EINVAL;
 780
 781	switch (val_size) {
 782	case 1:
 783		mode = IBT_REG_MODE_8BIT;
 784		break;
 785	case 2:
 786		mode = IBT_REG_MODE_16BIT;
 787		break;
 788	case 4:
 789		mode = IBT_REG_MODE_32BIT;
 790		break;
 791	default:
 792		return -EINVAL;
 793	}
 794
 795	cp = kmalloc(plen, GFP_KERNEL);
 796	if (!cp)
 797		return -ENOMEM;
 798
 799	/* regmap provides a little-endian formatted addr/value */
 800	cp->addr = *(__le32 *)addr;
 801	cp->mode = mode;
 802	cp->len = val_size;
 803	memcpy(&cp->data, val, val_size);
 804
 805	bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
 806
 807	skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
 808	if (IS_ERR(skb)) {
 809		err = PTR_ERR(skb);
 810		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
 811			   le32_to_cpu(cp->addr), err);
 812		goto done;
 813	}
 814	kfree_skb(skb);
 815
 816done:
 817	kfree(cp);
 818	return err;
 819}
 820
 821static int regmap_ibt_write(void *context, const void *data, size_t count)
 822{
 823	/* data contains register+value, since we only support 32bit addr,
 824	 * minimum data size is 4 bytes.
 825	 */
 826	if (WARN_ONCE(count < 4, "Invalid register access"))
 827		return -EINVAL;
 828
 829	return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
 830}
 831
 832static void regmap_ibt_free_context(void *context)
 833{
 834	kfree(context);
 835}
 836
 837static const struct regmap_bus regmap_ibt = {
 838	.read = regmap_ibt_read,
 839	.write = regmap_ibt_write,
 840	.gather_write = regmap_ibt_gather_write,
 841	.free_context = regmap_ibt_free_context,
 842	.reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
 843	.val_format_endian_default = REGMAP_ENDIAN_LITTLE,
 844};
 845
 846/* Config is the same for all register regions */
 847static const struct regmap_config regmap_ibt_cfg = {
 848	.name      = "btintel_regmap",
 849	.reg_bits  = 32,
 850	.val_bits  = 32,
 851};
 852
 853struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
 854				   u16 opcode_write)
 855{
 856	struct regmap_ibt_context *ctx;
 857
 858	bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
 859		    opcode_write);
 860
 861	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 862	if (!ctx)
 863		return ERR_PTR(-ENOMEM);
 864
 865	ctx->op_read = opcode_read;
 866	ctx->op_write = opcode_write;
 867	ctx->hdev = hdev;
 868
 869	return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_ibt_cfg);
 870}
 871EXPORT_SYMBOL_GPL(btintel_regmap_init);
 872
 873int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
 874{
 875	struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
 876	struct sk_buff *skb;
 877
 878	params.boot_param = cpu_to_le32(boot_param);
 879
 880	skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), &params,
 881			     HCI_INIT_TIMEOUT);
 882	if (IS_ERR(skb)) {
 883		bt_dev_err(hdev, "Failed to send Intel Reset command");
 884		return PTR_ERR(skb);
 885	}
 886
 887	kfree_skb(skb);
 888
 889	return 0;
 890}
 891EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
 892
 893int btintel_read_boot_params(struct hci_dev *hdev,
 894			     struct intel_boot_params *params)
 895{
 896	struct sk_buff *skb;
 897
 898	skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
 899	if (IS_ERR(skb)) {
 900		bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
 901			   PTR_ERR(skb));
 902		return PTR_ERR(skb);
 903	}
 904
 905	if (skb->len != sizeof(*params)) {
 906		bt_dev_err(hdev, "Intel boot parameters size mismatch");
 907		kfree_skb(skb);
 908		return -EILSEQ;
 909	}
 910
 911	memcpy(params, skb->data, sizeof(*params));
 912
 913	kfree_skb(skb);
 914
 915	if (params->status) {
 916		bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
 917			   params->status);
 918		return -bt_to_errno(params->status);
 919	}
 920
 921	bt_dev_info(hdev, "Device revision is %u",
 922		    le16_to_cpu(params->dev_revid));
 923
 924	bt_dev_info(hdev, "Secure boot is %s",
 925		    params->secure_boot ? "enabled" : "disabled");
 926
 927	bt_dev_info(hdev, "OTP lock is %s",
 928		    params->otp_lock ? "enabled" : "disabled");
 929
 930	bt_dev_info(hdev, "API lock is %s",
 931		    params->api_lock ? "enabled" : "disabled");
 932
 933	bt_dev_info(hdev, "Debug lock is %s",
 934		    params->debug_lock ? "enabled" : "disabled");
 935
 936	bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
 937		    params->min_fw_build_nn, params->min_fw_build_cw,
 938		    2000 + params->min_fw_build_yy);
 939
 940	return 0;
 941}
 942EXPORT_SYMBOL_GPL(btintel_read_boot_params);
 943
 944static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
 945					      const struct firmware *fw)
 946{
 947	int err;
 948
 949	/* Start the firmware download transaction with the Init fragment
 950	 * represented by the 128 bytes of CSS header.
 951	 */
 952	err = btintel_secure_send(hdev, 0x00, 128, fw->data);
 953	if (err < 0) {
 954		bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
 955		goto done;
 956	}
 957
 958	/* Send the 256 bytes of public key information from the firmware
 959	 * as the PKey fragment.
 960	 */
 961	err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
 962	if (err < 0) {
 963		bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
 964		goto done;
 965	}
 966
 967	/* Send the 256 bytes of signature information from the firmware
 968	 * as the Sign fragment.
 969	 */
 970	err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
 971	if (err < 0) {
 972		bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
 973		goto done;
 974	}
 975
 976done:
 977	return err;
 978}
 979
 980static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
 981						const struct firmware *fw)
 982{
 983	int err;
 984
 985	/* Start the firmware download transaction with the Init fragment
 986	 * represented by the 128 bytes of CSS header.
 987	 */
 988	err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
 989	if (err < 0) {
 990		bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
 991		return err;
 992	}
 993
 994	/* Send the 96 bytes of public key information from the firmware
 995	 * as the PKey fragment.
 996	 */
 997	err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
 998	if (err < 0) {
 999		bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
1000		return err;
1001	}
1002
1003	/* Send the 96 bytes of signature information from the firmware
1004	 * as the Sign fragment
1005	 */
1006	err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
1007	if (err < 0) {
1008		bt_dev_err(hdev, "Failed to send firmware signature (%d)",
1009			   err);
1010		return err;
1011	}
1012	return 0;
1013}
1014
1015static int btintel_download_firmware_payload(struct hci_dev *hdev,
1016					     const struct firmware *fw,
1017					     size_t offset)
1018{
1019	int err;
1020	const u8 *fw_ptr;
1021	u32 frag_len;
1022
1023	fw_ptr = fw->data + offset;
1024	frag_len = 0;
1025	err = -EINVAL;
1026
1027	while (fw_ptr - fw->data < fw->size) {
1028		struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
1029
1030		frag_len += sizeof(*cmd) + cmd->plen;
1031
1032		/* The parameter length of the secure send command requires
1033		 * a 4 byte alignment. It happens so that the firmware file
1034		 * contains proper Intel_NOP commands to align the fragments
1035		 * as needed.
1036		 *
1037		 * Send set of commands with 4 byte alignment from the
1038		 * firmware data buffer as a single Data fragement.
1039		 */
1040		if (!(frag_len % 4)) {
1041			err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1042			if (err < 0) {
1043				bt_dev_err(hdev,
1044					   "Failed to send firmware data (%d)",
1045					   err);
1046				goto done;
1047			}
1048
1049			fw_ptr += frag_len;
1050			frag_len = 0;
1051		}
1052	}
1053
1054done:
1055	return err;
1056}
1057
1058static bool btintel_firmware_version(struct hci_dev *hdev,
1059				     u8 num, u8 ww, u8 yy,
1060				     const struct firmware *fw,
1061				     u32 *boot_addr)
1062{
1063	const u8 *fw_ptr;
1064
1065	fw_ptr = fw->data;
1066
1067	while (fw_ptr - fw->data < fw->size) {
1068		struct hci_command_hdr *cmd = (void *)(fw_ptr);
1069
1070		/* Each SKU has a different reset parameter to use in the
1071		 * HCI_Intel_Reset command and it is embedded in the firmware
1072		 * data. So, instead of using static value per SKU, check
1073		 * the firmware data and save it for later use.
1074		 */
1075		if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1076			struct cmd_write_boot_params *params;
1077
1078			params = (void *)(fw_ptr + sizeof(*cmd));
1079
1080			*boot_addr = le32_to_cpu(params->boot_addr);
1081
1082			bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
1083
1084			bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1085				    params->fw_build_num, params->fw_build_ww,
1086				    params->fw_build_yy);
1087
1088			return (num == params->fw_build_num &&
1089				ww == params->fw_build_ww &&
1090				yy == params->fw_build_yy);
1091		}
1092
1093		fw_ptr += sizeof(*cmd) + cmd->plen;
1094	}
1095
1096	return false;
1097}
1098
1099int btintel_download_firmware(struct hci_dev *hdev,
1100			      struct intel_version *ver,
1101			      const struct firmware *fw,
1102			      u32 *boot_param)
1103{
1104	int err;
1105
1106	/* SfP and WsP don't seem to update the firmware version on file
1107	 * so version checking is currently not possible.
1108	 */
1109	switch (ver->hw_variant) {
1110	case 0x0b:	/* SfP */
1111	case 0x0c:	/* WsP */
1112		/* Skip version checking */
1113		break;
1114	default:
1115
1116		/* Skip download if firmware has the same version */
1117		if (btintel_firmware_version(hdev, ver->fw_build_num,
1118					     ver->fw_build_ww, ver->fw_build_yy,
1119					     fw, boot_param)) {
1120			bt_dev_info(hdev, "Firmware already loaded");
1121			/* Return -EALREADY to indicate that the firmware has
1122			 * already been loaded.
1123			 */
1124			return -EALREADY;
1125		}
1126	}
1127
1128	/* The firmware variant determines if the device is in bootloader
1129	 * mode or is running operational firmware. The value 0x06 identifies
1130	 * the bootloader and the value 0x23 identifies the operational
1131	 * firmware.
1132	 *
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.
1135	 */
1136	if (ver->fw_variant == 0x23)
1137		return -EINVAL;
1138
1139	err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1140	if (err)
1141		return err;
1142
1143	return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1144}
1145EXPORT_SYMBOL_GPL(btintel_download_firmware);
1146
1147static int btintel_download_fw_tlv(struct hci_dev *hdev,
1148				   struct intel_version_tlv *ver,
1149				   const struct firmware *fw, u32 *boot_param,
1150				   u8 hw_variant, u8 sbe_type)
1151{
1152	int err;
1153	u32 css_header_ver;
1154
1155	/* Skip download if firmware has the same version */
1156	if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1157				     ver->min_fw_build_cw,
1158				     ver->min_fw_build_yy,
1159				     fw, boot_param)) {
1160		bt_dev_info(hdev, "Firmware already loaded");
1161		/* Return -EALREADY to indicate that firmware has
1162		 * already been loaded.
1163		 */
1164		return -EALREADY;
1165	}
1166
1167	/* The firmware variant determines if the device is in bootloader
1168	 * mode or is running operational firmware. The value 0x01 identifies
1169	 * the bootloader and the value 0x03 identifies the operational
1170	 * firmware.
1171	 *
1172	 * If the firmware version has changed that means it needs to be reset
1173	 * to bootloader when operational so the new firmware can be loaded.
1174	 */
1175	if (ver->img_type == 0x03)
1176		return -EINVAL;
1177
1178	/* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1179	 * only RSA secure boot engine. Hence, the corresponding sfi file will
1180	 * have RSA header of 644 bytes followed by Command Buffer.
1181	 *
1182	 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1183	 * secure boot engine. As a result, the corresponding sfi file will
1184	 * have RSA header of 644, ECDSA header of 320 bytes followed by
1185	 * Command Buffer.
1186	 *
1187	 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1188	 * version: RSA(0x00010000) , ECDSA (0x00020000)
1189	 */
1190	css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1191	if (css_header_ver != 0x00010000) {
1192		bt_dev_err(hdev, "Invalid CSS Header version");
1193		return -EINVAL;
1194	}
1195
1196	if (hw_variant <= 0x14) {
1197		if (sbe_type != 0x00) {
1198			bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1199				   hw_variant);
1200			return -EINVAL;
1201		}
1202
1203		err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1204		if (err)
1205			return err;
1206
1207		err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1208		if (err)
1209			return err;
1210	} else if (hw_variant >= 0x17) {
1211		/* Check if CSS header for ECDSA follows the RSA header */
1212		if (fw->data[ECDSA_OFFSET] != 0x06)
1213			return -EINVAL;
1214
1215		/* Check if the CSS Header version is ECDSA(0x00020000) */
1216		css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1217		if (css_header_ver != 0x00020000) {
1218			bt_dev_err(hdev, "Invalid CSS Header version");
1219			return -EINVAL;
1220		}
1221
1222		if (sbe_type == 0x00) {
1223			err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1224			if (err)
1225				return err;
1226
1227			err = btintel_download_firmware_payload(hdev, fw,
1228								RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1229			if (err)
1230				return err;
1231		} else if (sbe_type == 0x01) {
1232			err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1233			if (err)
1234				return err;
1235
1236			err = btintel_download_firmware_payload(hdev, fw,
1237								RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1238			if (err)
1239				return err;
1240		}
1241	}
1242	return 0;
1243}
1244
1245static void btintel_reset_to_bootloader(struct hci_dev *hdev)
1246{
1247	struct intel_reset params;
1248	struct sk_buff *skb;
1249
 
 
 
 
 
 
1250	/* Send Intel Reset command. This will result in
1251	 * re-enumeration of BT controller.
1252	 *
1253	 * Intel Reset parameter description:
1254	 * reset_type :   0x00 (Soft reset),
1255	 *		  0x01 (Hard reset)
1256	 * patch_enable : 0x00 (Do not enable),
1257	 *		  0x01 (Enable)
1258	 * ddc_reload :   0x00 (Do not reload),
1259	 *		  0x01 (Reload)
1260	 * boot_option:   0x00 (Current image),
1261	 *                0x01 (Specified boot address)
1262	 * boot_param:    Boot address
1263	 *
1264	 */
 
1265	params.reset_type = 0x01;
1266	params.patch_enable = 0x01;
1267	params.ddc_reload = 0x01;
1268	params.boot_option = 0x00;
1269	params.boot_param = cpu_to_le32(0x00000000);
1270
1271	skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1272			     &params, HCI_INIT_TIMEOUT);
1273	if (IS_ERR(skb)) {
1274		bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1275			   PTR_ERR(skb));
1276		return;
1277	}
1278	bt_dev_info(hdev, "Intel reset sent to retry FW download");
1279	kfree_skb(skb);
1280
1281	/* Current Intel BT controllers(ThP/JfP) hold the USB reset
1282	 * lines for 2ms when it receives Intel Reset in bootloader mode.
1283	 * Whereas, the upcoming Intel BT controllers will hold USB reset
1284	 * for 150ms. To keep the delay generic, 150ms is chosen here.
1285	 */
1286	msleep(150);
1287}
1288
1289static int btintel_read_debug_features(struct hci_dev *hdev,
1290				       struct intel_debug_features *features)
1291{
1292	struct sk_buff *skb;
1293	u8 page_no = 1;
1294
1295	/* Intel controller supports two pages, each page is of 128-bit
1296	 * feature bit mask. And each bit defines specific feature support
1297	 */
1298	skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1299			     HCI_INIT_TIMEOUT);
1300	if (IS_ERR(skb)) {
1301		bt_dev_err(hdev, "Reading supported features failed (%ld)",
1302			   PTR_ERR(skb));
1303		return PTR_ERR(skb);
1304	}
1305
1306	if (skb->len != (sizeof(features->page1) + 3)) {
1307		bt_dev_err(hdev, "Supported features event size mismatch");
1308		kfree_skb(skb);
1309		return -EILSEQ;
1310	}
1311
1312	memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1313
1314	/* Read the supported features page2 if required in future.
1315	 */
1316	kfree_skb(skb);
1317	return 0;
1318}
1319
1320static acpi_status btintel_ppag_callback(acpi_handle handle, u32 lvl, void *data,
1321					 void **ret)
1322{
1323	acpi_status status;
1324	size_t len;
1325	struct btintel_ppag *ppag = data;
1326	union acpi_object *p, *elements;
1327	struct acpi_buffer string = {ACPI_ALLOCATE_BUFFER, NULL};
1328	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1329	struct hci_dev *hdev = ppag->hdev;
1330
1331	status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &string);
1332	if (ACPI_FAILURE(status)) {
1333		bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
1334		return status;
1335	}
1336
1337	len = strlen(string.pointer);
1338	if (len < strlen(BTINTEL_PPAG_NAME)) {
1339		kfree(string.pointer);
1340		return AE_OK;
1341	}
1342
1343	if (strncmp((char *)string.pointer + len - 4, BTINTEL_PPAG_NAME, 4)) {
1344		kfree(string.pointer);
1345		return AE_OK;
1346	}
1347	kfree(string.pointer);
1348
1349	status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
1350	if (ACPI_FAILURE(status)) {
1351		ppag->status = status;
1352		bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
1353		return status;
1354	}
1355
1356	p = buffer.pointer;
1357	ppag = (struct btintel_ppag *)data;
1358
1359	if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 2) {
1360		kfree(buffer.pointer);
1361		bt_dev_warn(hdev, "PPAG-BT: Invalid object type: %d or package count: %d",
1362			    p->type, p->package.count);
1363		ppag->status = AE_ERROR;
1364		return AE_ERROR;
1365	}
1366
1367	elements = p->package.elements;
1368
1369	/* PPAG table is located at element[1] */
1370	p = &elements[1];
1371
1372	ppag->domain = (u32)p->package.elements[0].integer.value;
1373	ppag->mode = (u32)p->package.elements[1].integer.value;
1374	ppag->status = AE_OK;
1375	kfree(buffer.pointer);
1376	return AE_CTRL_TERMINATE;
1377}
1378
1379static int btintel_set_debug_features(struct hci_dev *hdev,
1380			       const struct intel_debug_features *features)
1381{
1382	u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
1383			0x00, 0x00, 0x00 };
1384	u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
1385	u8 trace_enable = 0x02;
1386	struct sk_buff *skb;
1387
1388	if (!features) {
1389		bt_dev_warn(hdev, "Debug features not read");
1390		return -EINVAL;
1391	}
1392
1393	if (!(features->page1[0] & 0x3f)) {
1394		bt_dev_info(hdev, "Telemetry exception format not supported");
1395		return 0;
1396	}
1397
1398	skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1399	if (IS_ERR(skb)) {
1400		bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1401			   PTR_ERR(skb));
1402		return PTR_ERR(skb);
1403	}
1404	kfree_skb(skb);
1405
1406	skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
1407	if (IS_ERR(skb)) {
1408		bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
1409			   PTR_ERR(skb));
1410		return PTR_ERR(skb);
1411	}
1412	kfree_skb(skb);
1413
1414	skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1415	if (IS_ERR(skb)) {
1416		bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
1417			   PTR_ERR(skb));
1418		return PTR_ERR(skb);
1419	}
1420	kfree_skb(skb);
1421
1422	bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
1423		    trace_enable, mask[3]);
1424
1425	return 0;
1426}
1427
1428static int btintel_reset_debug_features(struct hci_dev *hdev,
1429				 const struct intel_debug_features *features)
1430{
1431	u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
1432			0x00, 0x00, 0x00 };
1433	u8 trace_enable = 0x00;
1434	struct sk_buff *skb;
1435
1436	if (!features) {
1437		bt_dev_warn(hdev, "Debug features not read");
1438		return -EINVAL;
1439	}
1440
1441	if (!(features->page1[0] & 0x3f)) {
1442		bt_dev_info(hdev, "Telemetry exception format not supported");
1443		return 0;
1444	}
1445
1446	/* Should stop the trace before writing ddc event mask. */
1447	skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1448	if (IS_ERR(skb)) {
1449		bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
1450			   PTR_ERR(skb));
1451		return PTR_ERR(skb);
1452	}
1453	kfree_skb(skb);
1454
1455	skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1456	if (IS_ERR(skb)) {
1457		bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1458			   PTR_ERR(skb));
1459		return PTR_ERR(skb);
1460	}
1461	kfree_skb(skb);
1462
1463	bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
1464		    trace_enable, mask[3]);
1465
1466	return 0;
1467}
1468
1469int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
1470{
1471	struct intel_debug_features features;
1472	int err;
1473
1474	bt_dev_dbg(hdev, "enable %d", enable);
1475
1476	/* Read the Intel supported features and if new exception formats
1477	 * supported, need to load the additional DDC config to enable.
1478	 */
1479	err = btintel_read_debug_features(hdev, &features);
1480	if (err)
1481		return err;
1482
1483	/* Set or reset the debug features. */
1484	if (enable)
1485		err = btintel_set_debug_features(hdev, &features);
1486	else
1487		err = btintel_reset_debug_features(hdev, &features);
1488
1489	return err;
1490}
1491EXPORT_SYMBOL_GPL(btintel_set_quality_report);
1492
1493static void btintel_coredump(struct hci_dev *hdev)
1494{
1495	struct sk_buff *skb;
1496
1497	skb = __hci_cmd_sync(hdev, 0xfc4e, 0, NULL, HCI_CMD_TIMEOUT);
1498	if (IS_ERR(skb)) {
1499		bt_dev_err(hdev, "Coredump failed (%ld)", PTR_ERR(skb));
1500		return;
1501	}
1502
1503	kfree_skb(skb);
1504}
1505
1506static void btintel_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
1507{
1508	char buf[80];
1509
1510	snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
1511		 coredump_info.hw_variant);
1512	skb_put_data(skb, buf, strlen(buf));
1513
1514	snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
1515		 coredump_info.fw_build_num);
1516	skb_put_data(skb, buf, strlen(buf));
1517
1518	snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info.driver_name);
1519	skb_put_data(skb, buf, strlen(buf));
1520
1521	snprintf(buf, sizeof(buf), "Vendor: Intel\n");
1522	skb_put_data(skb, buf, strlen(buf));
1523}
1524
1525static int btintel_register_devcoredump_support(struct hci_dev *hdev)
1526{
1527	struct intel_debug_features features;
1528	int err;
1529
1530	err = btintel_read_debug_features(hdev, &features);
1531	if (err) {
1532		bt_dev_info(hdev, "Error reading debug features");
1533		return err;
1534	}
1535
1536	if (!(features.page1[0] & 0x3f)) {
1537		bt_dev_dbg(hdev, "Telemetry exception format not supported");
1538		return -EOPNOTSUPP;
1539	}
1540
1541	hci_devcd_register(hdev, btintel_coredump, btintel_dmp_hdr, NULL);
1542
1543	return err;
1544}
1545
1546static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
1547					       struct intel_version *ver)
1548{
1549	const struct firmware *fw;
1550	char fwname[64];
1551	int ret;
1552
1553	snprintf(fwname, sizeof(fwname),
1554		 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1555		 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1556		 ver->fw_variant,  ver->fw_revision, ver->fw_build_num,
1557		 ver->fw_build_ww, ver->fw_build_yy);
1558
1559	ret = request_firmware(&fw, fwname, &hdev->dev);
1560	if (ret < 0) {
1561		if (ret == -EINVAL) {
1562			bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1563				   ret);
1564			return NULL;
1565		}
1566
1567		bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1568			   fwname, ret);
1569
1570		/* If the correct firmware patch file is not found, use the
1571		 * default firmware patch file instead
1572		 */
1573		snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1574			 ver->hw_platform, ver->hw_variant);
1575		if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1576			bt_dev_err(hdev, "failed to open default fw file: %s",
1577				   fwname);
1578			return NULL;
1579		}
1580	}
1581
1582	bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1583
1584	return fw;
1585}
1586
1587static int btintel_legacy_rom_patching(struct hci_dev *hdev,
1588				      const struct firmware *fw,
1589				      const u8 **fw_ptr, int *disable_patch)
1590{
1591	struct sk_buff *skb;
1592	struct hci_command_hdr *cmd;
1593	const u8 *cmd_param;
1594	struct hci_event_hdr *evt = NULL;
1595	const u8 *evt_param = NULL;
1596	int remain = fw->size - (*fw_ptr - fw->data);
1597
1598	/* The first byte indicates the types of the patch command or event.
1599	 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1600	 * in the current firmware buffer doesn't start with 0x01 or
1601	 * the size of remain buffer is smaller than HCI command header,
1602	 * the firmware file is corrupted and it should stop the patching
1603	 * process.
1604	 */
1605	if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1606		bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1607		return -EINVAL;
1608	}
1609	(*fw_ptr)++;
1610	remain--;
1611
1612	cmd = (struct hci_command_hdr *)(*fw_ptr);
1613	*fw_ptr += sizeof(*cmd);
1614	remain -= sizeof(*cmd);
1615
1616	/* Ensure that the remain firmware data is long enough than the length
1617	 * of command parameter. If not, the firmware file is corrupted.
1618	 */
1619	if (remain < cmd->plen) {
1620		bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1621		return -EFAULT;
1622	}
1623
1624	/* If there is a command that loads a patch in the firmware
1625	 * file, then enable the patch upon success, otherwise just
1626	 * disable the manufacturer mode, for example patch activation
1627	 * is not required when the default firmware patch file is used
1628	 * because there are no patch data to load.
1629	 */
1630	if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1631		*disable_patch = 0;
1632
1633	cmd_param = *fw_ptr;
1634	*fw_ptr += cmd->plen;
1635	remain -= cmd->plen;
1636
1637	/* This reads the expected events when the above command is sent to the
1638	 * device. Some vendor commands expects more than one events, for
1639	 * example command status event followed by vendor specific event.
1640	 * For this case, it only keeps the last expected event. so the command
1641	 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1642	 * last expected event.
1643	 */
1644	while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1645		(*fw_ptr)++;
1646		remain--;
1647
1648		evt = (struct hci_event_hdr *)(*fw_ptr);
1649		*fw_ptr += sizeof(*evt);
1650		remain -= sizeof(*evt);
1651
1652		if (remain < evt->plen) {
1653			bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1654			return -EFAULT;
1655		}
1656
1657		evt_param = *fw_ptr;
1658		*fw_ptr += evt->plen;
1659		remain -= evt->plen;
1660	}
1661
1662	/* Every HCI commands in the firmware file has its correspond event.
1663	 * If event is not found or remain is smaller than zero, the firmware
1664	 * file is corrupted.
1665	 */
1666	if (!evt || !evt_param || remain < 0) {
1667		bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1668		return -EFAULT;
1669	}
1670
1671	skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1672				cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1673	if (IS_ERR(skb)) {
1674		bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1675			   cmd->opcode, PTR_ERR(skb));
1676		return PTR_ERR(skb);
1677	}
1678
1679	/* It ensures that the returned event matches the event data read from
1680	 * the firmware file. At fist, it checks the length and then
1681	 * the contents of the event.
1682	 */
1683	if (skb->len != evt->plen) {
1684		bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1685			   le16_to_cpu(cmd->opcode));
1686		kfree_skb(skb);
1687		return -EFAULT;
1688	}
1689
1690	if (memcmp(skb->data, evt_param, evt->plen)) {
1691		bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1692			   le16_to_cpu(cmd->opcode));
1693		kfree_skb(skb);
1694		return -EFAULT;
1695	}
1696	kfree_skb(skb);
1697
1698	return 0;
1699}
1700
1701static int btintel_legacy_rom_setup(struct hci_dev *hdev,
1702				    struct intel_version *ver)
1703{
1704	const struct firmware *fw;
1705	const u8 *fw_ptr;
1706	int disable_patch, err;
1707	struct intel_version new_ver;
1708
1709	BT_DBG("%s", hdev->name);
1710
1711	/* fw_patch_num indicates the version of patch the device currently
1712	 * have. If there is no patch data in the device, it is always 0x00.
1713	 * So, if it is other than 0x00, no need to patch the device again.
1714	 */
1715	if (ver->fw_patch_num) {
1716		bt_dev_info(hdev,
1717			    "Intel device is already patched. patch num: %02x",
1718			    ver->fw_patch_num);
1719		goto complete;
1720	}
1721
1722	/* Opens the firmware patch file based on the firmware version read
1723	 * from the controller. If it fails to open the matching firmware
1724	 * patch file, it tries to open the default firmware patch file.
1725	 * If no patch file is found, allow the device to operate without
1726	 * a patch.
1727	 */
1728	fw = btintel_legacy_rom_get_fw(hdev, ver);
1729	if (!fw)
1730		goto complete;
1731	fw_ptr = fw->data;
1732
1733	/* Enable the manufacturer mode of the controller.
1734	 * Only while this mode is enabled, the driver can download the
1735	 * firmware patch data and configuration parameters.
1736	 */
1737	err = btintel_enter_mfg(hdev);
1738	if (err) {
1739		release_firmware(fw);
1740		return err;
1741	}
1742
1743	disable_patch = 1;
1744
1745	/* The firmware data file consists of list of Intel specific HCI
1746	 * commands and its expected events. The first byte indicates the
1747	 * type of the message, either HCI command or HCI event.
1748	 *
1749	 * It reads the command and its expected event from the firmware file,
1750	 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1751	 * the returned event is compared with the event read from the firmware
1752	 * file and it will continue until all the messages are downloaded to
1753	 * the controller.
1754	 *
1755	 * Once the firmware patching is completed successfully,
1756	 * the manufacturer mode is disabled with reset and activating the
1757	 * downloaded patch.
1758	 *
1759	 * If the firmware patching fails, the manufacturer mode is
1760	 * disabled with reset and deactivating the patch.
1761	 *
1762	 * If the default patch file is used, no reset is done when disabling
1763	 * the manufacturer.
1764	 */
1765	while (fw->size > fw_ptr - fw->data) {
1766		int ret;
1767
1768		ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
1769						 &disable_patch);
1770		if (ret < 0)
1771			goto exit_mfg_deactivate;
1772	}
1773
1774	release_firmware(fw);
1775
1776	if (disable_patch)
1777		goto exit_mfg_disable;
1778
1779	/* Patching completed successfully and disable the manufacturer mode
1780	 * with reset and activate the downloaded firmware patches.
1781	 */
1782	err = btintel_exit_mfg(hdev, true, true);
1783	if (err)
1784		return err;
1785
1786	/* Need build number for downloaded fw patches in
1787	 * every power-on boot
1788	 */
1789	err = btintel_read_version(hdev, &new_ver);
1790	if (err)
1791		return err;
1792
1793	bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
1794		    new_ver.fw_patch_num);
1795
1796	goto complete;
1797
1798exit_mfg_disable:
1799	/* Disable the manufacturer mode without reset */
1800	err = btintel_exit_mfg(hdev, false, false);
1801	if (err)
1802		return err;
1803
1804	bt_dev_info(hdev, "Intel firmware patch completed");
1805
1806	goto complete;
1807
1808exit_mfg_deactivate:
1809	release_firmware(fw);
1810
1811	/* Patching failed. Disable the manufacturer mode with reset and
1812	 * deactivate the downloaded firmware patches.
1813	 */
1814	err = btintel_exit_mfg(hdev, true, false);
1815	if (err)
1816		return err;
1817
1818	bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1819
1820complete:
1821	/* Set the event mask for Intel specific vendor events. This enables
1822	 * a few extra events that are useful during general operation.
1823	 */
1824	btintel_set_event_mask_mfg(hdev, false);
1825
1826	btintel_check_bdaddr(hdev);
1827
1828	return 0;
1829}
1830
1831static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1832{
1833	ktime_t delta, rettime;
1834	unsigned long long duration;
1835	int err;
1836
1837	btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1838
1839	bt_dev_info(hdev, "Waiting for firmware download to complete");
1840
1841	err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
1842					   TASK_INTERRUPTIBLE,
1843					   msecs_to_jiffies(msec));
1844	if (err == -EINTR) {
1845		bt_dev_err(hdev, "Firmware loading interrupted");
1846		return err;
1847	}
1848
1849	if (err) {
1850		bt_dev_err(hdev, "Firmware loading timeout");
1851		return -ETIMEDOUT;
1852	}
1853
1854	if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
1855		bt_dev_err(hdev, "Firmware loading failed");
1856		return -ENOEXEC;
1857	}
1858
1859	rettime = ktime_get();
1860	delta = ktime_sub(rettime, calltime);
1861	duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1862
1863	bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
1864
1865	return 0;
1866}
1867
1868static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1869{
1870	ktime_t delta, rettime;
1871	unsigned long long duration;
1872	int err;
1873
1874	bt_dev_info(hdev, "Waiting for device to boot");
1875
1876	err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
1877					   TASK_INTERRUPTIBLE,
1878					   msecs_to_jiffies(msec));
1879	if (err == -EINTR) {
1880		bt_dev_err(hdev, "Device boot interrupted");
1881		return -EINTR;
1882	}
1883
1884	if (err) {
1885		bt_dev_err(hdev, "Device boot timeout");
1886		return -ETIMEDOUT;
1887	}
1888
1889	rettime = ktime_get();
1890	delta = ktime_sub(rettime, calltime);
1891	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1892
1893	bt_dev_info(hdev, "Device booted in %llu usecs", duration);
1894
1895	return 0;
1896}
1897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1898static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
1899{
1900	ktime_t calltime;
1901	int err;
1902
1903	calltime = ktime_get();
1904
1905	btintel_set_flag(hdev, INTEL_BOOTING);
 
1906
1907	err = btintel_send_intel_reset(hdev, boot_addr);
1908	if (err) {
1909		bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
1910		btintel_reset_to_bootloader(hdev);
1911		return err;
1912	}
1913
1914	/* The bootloader will not indicate when the device is ready. This
1915	 * is done by the operational firmware sending bootup notification.
1916	 *
1917	 * Booting into operational firmware should not take longer than
1918	 * 1 second. However if that happens, then just fail the setup
1919	 * since something went wrong.
1920	 */
1921	err = btintel_boot_wait(hdev, calltime, 1000);
1922	if (err == -ETIMEDOUT)
1923		btintel_reset_to_bootloader(hdev);
 
 
1924
 
 
 
 
 
 
 
 
 
 
 
 
 
1925	return err;
1926}
1927
1928static int btintel_get_fw_name(struct intel_version *ver,
1929					     struct intel_boot_params *params,
1930					     char *fw_name, size_t len,
1931					     const char *suffix)
1932{
1933	switch (ver->hw_variant) {
1934	case 0x0b:	/* SfP */
1935	case 0x0c:	/* WsP */
1936		snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
1937			 ver->hw_variant,
1938			 le16_to_cpu(params->dev_revid),
1939			 suffix);
1940		break;
1941	case 0x11:	/* JfP */
1942	case 0x12:	/* ThP */
1943	case 0x13:	/* HrP */
1944	case 0x14:	/* CcP */
1945		snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
1946			 ver->hw_variant,
1947			 ver->hw_revision,
1948			 ver->fw_revision,
1949			 suffix);
1950		break;
1951	default:
1952		return -EINVAL;
1953	}
1954
1955	return 0;
1956}
1957
1958static int btintel_download_fw(struct hci_dev *hdev,
1959					 struct intel_version *ver,
1960					 struct intel_boot_params *params,
1961					 u32 *boot_param)
1962{
1963	const struct firmware *fw;
1964	char fwname[64];
1965	int err;
1966	ktime_t calltime;
1967
1968	if (!ver || !params)
1969		return -EINVAL;
1970
1971	/* The firmware variant determines if the device is in bootloader
1972	 * mode or is running operational firmware. The value 0x06 identifies
1973	 * the bootloader and the value 0x23 identifies the operational
1974	 * firmware.
1975	 *
1976	 * When the operational firmware is already present, then only
1977	 * the check for valid Bluetooth device address is needed. This
1978	 * determines if the device will be added as configured or
1979	 * unconfigured controller.
1980	 *
1981	 * It is not possible to use the Secure Boot Parameters in this
1982	 * case since that command is only available in bootloader mode.
1983	 */
1984	if (ver->fw_variant == 0x23) {
1985		btintel_clear_flag(hdev, INTEL_BOOTLOADER);
1986		btintel_check_bdaddr(hdev);
1987
1988		/* SfP and WsP don't seem to update the firmware version on file
1989		 * so version checking is currently possible.
1990		 */
1991		switch (ver->hw_variant) {
1992		case 0x0b:	/* SfP */
1993		case 0x0c:	/* WsP */
1994			return 0;
1995		}
1996
1997		/* Proceed to download to check if the version matches */
1998		goto download;
1999	}
2000
2001	/* Read the secure boot parameters to identify the operating
2002	 * details of the bootloader.
2003	 */
2004	err = btintel_read_boot_params(hdev, params);
2005	if (err)
2006		return err;
2007
2008	/* It is required that every single firmware fragment is acknowledged
2009	 * with a command complete event. If the boot parameters indicate
2010	 * that this bootloader does not send them, then abort the setup.
2011	 */
2012	if (params->limited_cce != 0x00) {
2013		bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2014			   params->limited_cce);
2015		return -EINVAL;
2016	}
2017
2018	/* If the OTP has no valid Bluetooth device address, then there will
2019	 * also be no valid address for the operational firmware.
2020	 */
2021	if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2022		bt_dev_info(hdev, "No device address configured");
2023		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2024	}
2025
2026download:
2027	/* With this Intel bootloader only the hardware variant and device
2028	 * revision information are used to select the right firmware for SfP
2029	 * and WsP.
2030	 *
2031	 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2032	 *
2033	 * Currently the supported hardware variants are:
2034	 *   11 (0x0b) for iBT3.0 (LnP/SfP)
2035	 *   12 (0x0c) for iBT3.5 (WsP)
2036	 *
2037	 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2038	 * variant, HW revision and FW revision, as these are dependent on CNVi
2039	 * and RF Combination.
2040	 *
2041	 *   17 (0x11) for iBT3.5 (JfP)
2042	 *   18 (0x12) for iBT3.5 (ThP)
2043	 *
2044	 * The firmware file name for these will be
2045	 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2046	 *
2047	 */
2048	err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
2049	if (err < 0) {
2050		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2051			/* Firmware has already been loaded */
2052			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2053			return 0;
2054		}
2055
2056		bt_dev_err(hdev, "Unsupported Intel firmware naming");
2057		return -EINVAL;
2058	}
2059
2060	err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2061	if (err < 0) {
2062		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2063			/* Firmware has already been loaded */
2064			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2065			return 0;
2066		}
2067
2068		bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2069			   fwname, err);
2070		return err;
2071	}
2072
2073	bt_dev_info(hdev, "Found device firmware: %s", fwname);
2074
2075	if (fw->size < 644) {
2076		bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2077			   fw->size);
2078		err = -EBADF;
2079		goto done;
2080	}
2081
2082	calltime = ktime_get();
2083
2084	btintel_set_flag(hdev, INTEL_DOWNLOADING);
2085
2086	/* Start firmware downloading and get boot parameter */
2087	err = btintel_download_firmware(hdev, ver, fw, boot_param);
2088	if (err < 0) {
2089		if (err == -EALREADY) {
2090			/* Firmware has already been loaded */
2091			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2092			err = 0;
2093			goto done;
2094		}
2095
2096		/* When FW download fails, send Intel Reset to retry
2097		 * FW download.
2098		 */
2099		btintel_reset_to_bootloader(hdev);
2100		goto done;
2101	}
2102
2103	/* Before switching the device into operational mode and with that
2104	 * booting the loaded firmware, wait for the bootloader notification
2105	 * that all fragments have been successfully received.
2106	 *
2107	 * When the event processing receives the notification, then the
2108	 * INTEL_DOWNLOADING flag will be cleared.
2109	 *
2110	 * The firmware loading should not take longer than 5 seconds
2111	 * and thus just timeout if that happens and fail the setup
2112	 * of this device.
2113	 */
2114	err = btintel_download_wait(hdev, calltime, 5000);
2115	if (err == -ETIMEDOUT)
2116		btintel_reset_to_bootloader(hdev);
2117
2118done:
2119	release_firmware(fw);
2120	return err;
2121}
2122
2123static int btintel_bootloader_setup(struct hci_dev *hdev,
2124				    struct intel_version *ver)
2125{
2126	struct intel_version new_ver;
2127	struct intel_boot_params params;
2128	u32 boot_param;
2129	char ddcname[64];
2130	int err;
2131
2132	BT_DBG("%s", hdev->name);
2133
2134	/* Set the default boot parameter to 0x0 and it is updated to
2135	 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2136	 * command while downloading the firmware.
2137	 */
2138	boot_param = 0x00000000;
2139
2140	btintel_set_flag(hdev, INTEL_BOOTLOADER);
2141
2142	err = btintel_download_fw(hdev, ver, &params, &boot_param);
2143	if (err)
2144		return err;
2145
2146	/* controller is already having an operational firmware */
2147	if (ver->fw_variant == 0x23)
2148		goto finish;
2149
2150	err = btintel_boot(hdev, boot_param);
2151	if (err)
2152		return err;
2153
2154	btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2155
2156	err = btintel_get_fw_name(ver, &params, ddcname,
2157						sizeof(ddcname), "ddc");
2158
2159	if (err < 0) {
2160		bt_dev_err(hdev, "Unsupported Intel firmware naming");
2161	} else {
2162		/* Once the device is running in operational mode, it needs to
2163		 * apply the device configuration (DDC) parameters.
2164		 *
2165		 * The device can work without DDC parameters, so even if it
2166		 * fails to load the file, no need to fail the setup.
2167		 */
2168		btintel_load_ddc_config(hdev, ddcname);
2169	}
2170
2171	hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2172
2173	/* Read the Intel version information after loading the FW  */
2174	err = btintel_read_version(hdev, &new_ver);
2175	if (err)
2176		return err;
2177
2178	btintel_version_info(hdev, &new_ver);
2179
2180finish:
2181	/* Set the event mask for Intel specific vendor events. This enables
2182	 * a few extra events that are useful during general operation. It
2183	 * does not enable any debugging related events.
2184	 *
2185	 * The device will function correctly without these events enabled
2186	 * and thus no need to fail the setup.
2187	 */
2188	btintel_set_event_mask(hdev, false);
2189
2190	return 0;
2191}
2192
2193static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
2194				    char *fw_name, size_t len,
2195				    const char *suffix)
2196{
2197	/* The firmware file name for new generation controllers will be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2198	 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2199	 */
2200	snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2201		 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2202					  INTEL_CNVX_TOP_STEP(ver->cnvi_top)),
2203		 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2204					  INTEL_CNVX_TOP_STEP(ver->cnvr_top)),
2205		 suffix);
 
 
 
 
 
 
 
 
 
 
 
 
 
2206}
2207
2208static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
2209					   struct intel_version_tlv *ver,
2210					   u32 *boot_param)
2211{
2212	const struct firmware *fw;
2213	char fwname[64];
2214	int err;
2215	ktime_t calltime;
2216
2217	if (!ver || !boot_param)
2218		return -EINVAL;
2219
2220	/* The firmware variant determines if the device is in bootloader
2221	 * mode or is running operational firmware. The value 0x03 identifies
2222	 * the bootloader and the value 0x23 identifies the operational
2223	 * firmware.
2224	 *
2225	 * When the operational firmware is already present, then only
2226	 * the check for valid Bluetooth device address is needed. This
2227	 * determines if the device will be added as configured or
2228	 * unconfigured controller.
2229	 *
2230	 * It is not possible to use the Secure Boot Parameters in this
2231	 * case since that command is only available in bootloader mode.
2232	 */
2233	if (ver->img_type == 0x03) {
2234		btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2235		btintel_check_bdaddr(hdev);
2236	} else {
2237		/*
2238		 * Check for valid bd address in boot loader mode. Device
2239		 * will be marked as unconfigured if empty bd address is
2240		 * found.
2241		 */
2242		if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2243			bt_dev_info(hdev, "No device address configured");
2244			set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2245		}
2246	}
2247
2248	btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
 
 
 
 
 
 
 
 
 
 
 
 
 
2249	err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2250	if (err < 0) {
2251		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2252			/* Firmware has already been loaded */
2253			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2254			return 0;
2255		}
2256
2257		bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2258			   fwname, err);
2259
2260		return err;
2261	}
2262
2263	bt_dev_info(hdev, "Found device firmware: %s", fwname);
2264
2265	if (fw->size < 644) {
2266		bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2267			   fw->size);
2268		err = -EBADF;
2269		goto done;
2270	}
2271
2272	calltime = ktime_get();
2273
2274	btintel_set_flag(hdev, INTEL_DOWNLOADING);
2275
2276	/* Start firmware downloading and get boot parameter */
2277	err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
2278					       INTEL_HW_VARIANT(ver->cnvi_bt),
2279					       ver->sbe_type);
2280	if (err < 0) {
2281		if (err == -EALREADY) {
2282			/* Firmware has already been loaded */
2283			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2284			err = 0;
2285			goto done;
2286		}
2287
2288		/* When FW download fails, send Intel Reset to retry
2289		 * FW download.
2290		 */
2291		btintel_reset_to_bootloader(hdev);
2292		goto done;
2293	}
2294
2295	/* Before switching the device into operational mode and with that
2296	 * booting the loaded firmware, wait for the bootloader notification
2297	 * that all fragments have been successfully received.
2298	 *
2299	 * When the event processing receives the notification, then the
2300	 * BTUSB_DOWNLOADING flag will be cleared.
2301	 *
2302	 * The firmware loading should not take longer than 5 seconds
2303	 * and thus just timeout if that happens and fail the setup
2304	 * of this device.
2305	 */
2306	err = btintel_download_wait(hdev, calltime, 5000);
2307	if (err == -ETIMEDOUT)
2308		btintel_reset_to_bootloader(hdev);
2309
2310done:
2311	release_firmware(fw);
2312	return err;
2313}
2314
2315static int btintel_get_codec_config_data(struct hci_dev *hdev,
2316					 __u8 link, struct bt_codec *codec,
2317					 __u8 *ven_len, __u8 **ven_data)
2318{
2319	int err = 0;
2320
2321	if (!ven_data || !ven_len)
2322		return -EINVAL;
2323
2324	*ven_len = 0;
2325	*ven_data = NULL;
2326
2327	if (link != ESCO_LINK) {
2328		bt_dev_err(hdev, "Invalid link type(%u)", link);
2329		return -EINVAL;
2330	}
2331
2332	*ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
2333	if (!*ven_data) {
2334		err = -ENOMEM;
2335		goto error;
2336	}
2337
2338	/* supports only CVSD and mSBC offload codecs */
2339	switch (codec->id) {
2340	case 0x02:
2341		**ven_data = 0x00;
2342		break;
2343	case 0x05:
2344		**ven_data = 0x01;
2345		break;
2346	default:
2347		err = -EINVAL;
2348		bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
2349		goto error;
2350	}
2351	/* codec and its capabilities are pre-defined to ids
2352	 * preset id = 0x00 represents CVSD codec with sampling rate 8K
2353	 * preset id = 0x01 represents mSBC codec with sampling rate 16K
2354	 */
2355	*ven_len = sizeof(__u8);
2356	return err;
2357
2358error:
2359	kfree(*ven_data);
2360	*ven_data = NULL;
2361	return err;
2362}
2363
2364static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
2365{
2366	/* Intel uses 1 as data path id for all the usecases */
2367	*data_path_id = 1;
2368	return 0;
2369}
2370
2371static int btintel_configure_offload(struct hci_dev *hdev)
2372{
2373	struct sk_buff *skb;
2374	int err = 0;
2375	struct intel_offload_use_cases *use_cases;
2376
2377	skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
2378	if (IS_ERR(skb)) {
2379		bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
2380			   PTR_ERR(skb));
2381		return PTR_ERR(skb);
2382	}
2383
2384	if (skb->len < sizeof(*use_cases)) {
2385		err = -EIO;
2386		goto error;
2387	}
2388
2389	use_cases = (void *)skb->data;
2390
2391	if (use_cases->status) {
2392		err = -bt_to_errno(skb->data[0]);
2393		goto error;
2394	}
2395
2396	if (use_cases->preset[0] & 0x03) {
2397		hdev->get_data_path_id = btintel_get_data_path_id;
2398		hdev->get_codec_config_data = btintel_get_codec_config_data;
2399	}
2400error:
2401	kfree_skb(skb);
2402	return err;
2403}
2404
2405static void btintel_set_ppag(struct hci_dev *hdev, struct intel_version_tlv *ver)
2406{
2407	struct btintel_ppag ppag;
2408	struct sk_buff *skb;
2409	struct hci_ppag_enable_cmd ppag_cmd;
2410	acpi_handle handle;
 
 
 
 
2411
2412	/* PPAG is not supported if CRF is HrP2, Jfp2, JfP1 */
2413	switch (ver->cnvr_top & 0xFFF) {
2414	case 0x504:     /* Hrp2 */
2415	case 0x202:     /* Jfp2 */
2416	case 0x201:     /* Jfp1 */
2417		bt_dev_dbg(hdev, "PPAG not supported for Intel CNVr (0x%3x)",
2418			   ver->cnvr_top & 0xFFF);
2419		return;
2420	}
2421
2422	handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2423	if (!handle) {
2424		bt_dev_info(hdev, "No support for BT device in ACPI firmware");
2425		return;
2426	}
2427
2428	memset(&ppag, 0, sizeof(ppag));
2429
2430	ppag.hdev = hdev;
2431	ppag.status = AE_NOT_FOUND;
2432	acpi_walk_namespace(ACPI_TYPE_PACKAGE, handle, 1, NULL,
2433			    btintel_ppag_callback, &ppag, NULL);
2434
2435	if (ACPI_FAILURE(ppag.status)) {
2436		if (ppag.status == AE_NOT_FOUND) {
2437			bt_dev_dbg(hdev, "PPAG-BT: ACPI entry not found");
2438			return;
2439		}
 
2440		return;
2441	}
2442
2443	if (ppag.domain != 0x12) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2444		bt_dev_dbg(hdev, "PPAG-BT: Bluetooth domain is disabled in ACPI firmware");
2445		return;
2446	}
2447
2448	/* PPAG mode
2449	 * BIT 0 : 0 Disabled in EU
2450	 *         1 Enabled in EU
2451	 * BIT 1 : 0 Disabled in China
2452	 *         1 Enabled in China
2453	 */
2454	if ((ppag.mode & 0x01) != BIT(0) && (ppag.mode & 0x02) != BIT(1)) {
2455		bt_dev_dbg(hdev, "PPAG-BT: EU, China mode are disabled in CB/BIOS");
 
 
2456		return;
2457	}
2458
2459	ppag_cmd.ppag_enable_flags = cpu_to_le32(ppag.mode);
2460
2461	skb = __hci_cmd_sync(hdev, INTEL_OP_PPAG_CMD, sizeof(ppag_cmd), &ppag_cmd, HCI_CMD_TIMEOUT);
 
2462	if (IS_ERR(skb)) {
2463		bt_dev_warn(hdev, "Failed to send PPAG Enable (%ld)", PTR_ERR(skb));
2464		return;
2465	}
2466	bt_dev_info(hdev, "PPAG-BT: Enabled (Mode %d)", ppag.mode);
2467	kfree_skb(skb);
2468}
2469
2470static int btintel_acpi_reset_method(struct hci_dev *hdev)
2471{
2472	int ret = 0;
2473	acpi_status status;
2474	union acpi_object *p, *ref;
2475	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2476
2477	status = acpi_evaluate_object(ACPI_HANDLE(GET_HCIDEV_DEV(hdev)), "_PRR", NULL, &buffer);
2478	if (ACPI_FAILURE(status)) {
2479		bt_dev_err(hdev, "Failed to run _PRR method");
2480		ret = -ENODEV;
2481		return ret;
2482	}
2483	p = buffer.pointer;
2484
2485	if (p->package.count != 1 || p->type != ACPI_TYPE_PACKAGE) {
2486		bt_dev_err(hdev, "Invalid arguments");
2487		ret = -EINVAL;
2488		goto exit_on_error;
2489	}
2490
2491	ref = &p->package.elements[0];
2492	if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) {
2493		bt_dev_err(hdev, "Invalid object type: 0x%x", ref->type);
2494		ret = -EINVAL;
2495		goto exit_on_error;
2496	}
2497
2498	status = acpi_evaluate_object(ref->reference.handle, "_RST", NULL, NULL);
2499	if (ACPI_FAILURE(status)) {
2500		bt_dev_err(hdev, "Failed to run_RST method");
2501		ret = -ENODEV;
2502		goto exit_on_error;
2503	}
2504
2505exit_on_error:
2506	kfree(buffer.pointer);
2507	return ret;
2508}
2509
2510static void btintel_set_dsm_reset_method(struct hci_dev *hdev,
2511					 struct intel_version_tlv *ver_tlv)
2512{
2513	struct btintel_data *data = hci_get_priv(hdev);
2514	acpi_handle handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2515	u8 reset_payload[4] = {0x01, 0x00, 0x01, 0x00};
2516	union acpi_object *obj, argv4;
2517	enum {
2518		RESET_TYPE_WDISABLE2,
2519		RESET_TYPE_VSEC
2520	};
2521
2522	handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2523
2524	if (!handle) {
2525		bt_dev_dbg(hdev, "No support for bluetooth device in ACPI firmware");
2526		return;
2527	}
2528
2529	if (!acpi_has_method(handle, "_PRR")) {
2530		bt_dev_err(hdev, "No support for _PRR ACPI method");
2531		return;
2532	}
2533
2534	switch (ver_tlv->cnvi_top & 0xfff) {
2535	case 0x910: /* GalePeak2 */
2536		reset_payload[2] = RESET_TYPE_VSEC;
2537		break;
2538	default:
2539		/* WDISABLE2 is the default reset method */
2540		reset_payload[2] = RESET_TYPE_WDISABLE2;
2541
2542		if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2543				    BIT(DSM_SET_WDISABLE2_DELAY))) {
2544			bt_dev_err(hdev, "No dsm support to set reset delay");
2545			return;
2546		}
2547		argv4.integer.type = ACPI_TYPE_INTEGER;
2548		/* delay required to toggle BT power */
2549		argv4.integer.value = 160;
2550		obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2551					DSM_SET_WDISABLE2_DELAY, &argv4);
2552		if (!obj) {
2553			bt_dev_err(hdev, "Failed to call dsm to set reset delay");
2554			return;
2555		}
2556		ACPI_FREE(obj);
2557	}
2558
2559	bt_dev_info(hdev, "DSM reset method type: 0x%02x", reset_payload[2]);
2560
2561	if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2562			    DSM_SET_RESET_METHOD)) {
2563		bt_dev_warn(hdev, "No support for dsm to set reset method");
2564		return;
2565	}
2566	argv4.buffer.type = ACPI_TYPE_BUFFER;
2567	argv4.buffer.length = sizeof(reset_payload);
2568	argv4.buffer.pointer = reset_payload;
2569
2570	obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2571				DSM_SET_RESET_METHOD, &argv4);
2572	if (!obj) {
2573		bt_dev_err(hdev, "Failed to call dsm to set reset method");
2574		return;
2575	}
2576	ACPI_FREE(obj);
2577	data->acpi_reset_method = btintel_acpi_reset_method;
2578}
2579
2580static int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
2581					struct intel_version_tlv *ver)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2582{
2583	u32 boot_param;
2584	char ddcname[64];
2585	int err;
2586	struct intel_version_tlv new_ver;
2587
2588	bt_dev_dbg(hdev, "");
2589
2590	/* Set the default boot parameter to 0x0 and it is updated to
2591	 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2592	 * command while downloading the firmware.
2593	 */
2594	boot_param = 0x00000000;
2595
 
 
 
 
 
 
 
2596	btintel_set_flag(hdev, INTEL_BOOTLOADER);
2597
2598	err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2599	if (err)
2600		return err;
2601
2602	/* check if controller is already having an operational firmware */
2603	if (ver->img_type == 0x03)
2604		goto finish;
2605
2606	err = btintel_boot(hdev, boot_param);
2607	if (err)
2608		return err;
2609
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2610	btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2611
2612	btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
2613	/* Once the device is running in operational mode, it needs to
2614	 * apply the device configuration (DDC) parameters.
2615	 *
2616	 * The device can work without DDC parameters, so even if it
2617	 * fails to load the file, no need to fail the setup.
2618	 */
2619	btintel_load_ddc_config(hdev, ddcname);
2620
2621	/* Read supported use cases and set callbacks to fetch datapath id */
2622	btintel_configure_offload(hdev);
2623
2624	hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2625
2626	/* Set PPAG feature */
2627	btintel_set_ppag(hdev, ver);
2628
2629	/* Read the Intel version information after loading the FW  */
2630	err = btintel_read_version_tlv(hdev, &new_ver);
2631	if (err)
2632		return err;
2633
2634	btintel_version_info_tlv(hdev, &new_ver);
2635
2636finish:
2637	/* Set the event mask for Intel specific vendor events. This enables
2638	 * a few extra events that are useful during general operation. It
2639	 * does not enable any debugging related events.
2640	 *
2641	 * The device will function correctly without these events enabled
2642	 * and thus no need to fail the setup.
2643	 */
2644	btintel_set_event_mask(hdev, false);
2645
2646	return 0;
2647}
 
2648
2649static void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
2650{
2651	switch (hw_variant) {
2652	/* Legacy bootloader devices that supports MSFT Extension */
2653	case 0x11:	/* JfP */
2654	case 0x12:	/* ThP */
2655	case 0x13:	/* HrP */
2656	case 0x14:	/* CcP */
2657	/* All Intel new genration controllers support the Microsoft vendor
2658	 * extension are using 0xFC1E for VsMsftOpCode.
2659	 */
2660	case 0x17:
2661	case 0x18:
2662	case 0x19:
2663	case 0x1b:
2664	case 0x1c:
 
 
2665		hci_set_msft_opcode(hdev, 0xFC1E);
2666		break;
2667	default:
2668		/* Not supported */
2669		break;
2670	}
2671}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2672
2673static int btintel_setup_combined(struct hci_dev *hdev)
2674{
2675	const u8 param[1] = { 0xFF };
2676	struct intel_version ver;
2677	struct intel_version_tlv ver_tlv;
2678	struct sk_buff *skb;
2679	int err;
2680
2681	BT_DBG("%s", hdev->name);
2682
2683	/* The some controllers have a bug with the first HCI command sent to it
2684	 * returning number of completed commands as zero. This would stall the
2685	 * command processing in the Bluetooth core.
2686	 *
2687	 * As a workaround, send HCI Reset command first which will reset the
2688	 * number of completed commands and allow normal command processing
2689	 * from now on.
2690	 *
2691	 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
2692	 * in the SW_RFKILL ON state as a workaround of fixing LED issue during
2693	 * the shutdown() procedure, and once the device is in SW_RFKILL ON
2694	 * state, the only way to exit out of it is sending the HCI_Reset
2695	 * command.
2696	 */
2697	if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
2698	    btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2699		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
2700				     HCI_INIT_TIMEOUT);
2701		if (IS_ERR(skb)) {
2702			bt_dev_err(hdev,
2703				   "sending initial HCI reset failed (%ld)",
2704				   PTR_ERR(skb));
2705			return PTR_ERR(skb);
2706		}
2707		kfree_skb(skb);
2708	}
2709
2710	/* Starting from TyP device, the command parameter and response are
2711	 * changed even though the OCF for HCI_Intel_Read_Version command
2712	 * remains same. The legacy devices can handle even if the
2713	 * command has a parameter and returns a correct version information.
2714	 * So, it uses new format to support both legacy and new format.
2715	 */
2716	skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
2717	if (IS_ERR(skb)) {
2718		bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
2719			   PTR_ERR(skb));
2720		return PTR_ERR(skb);
2721	}
2722
2723	/* Check the status */
2724	if (skb->data[0]) {
2725		bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
2726			   skb->data[0]);
2727		err = -EIO;
2728		goto exit_error;
2729	}
2730
2731	/* Apply the common HCI quirks for Intel device */
2732	set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2733	set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2734	set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2735
2736	/* Set up the quality report callback for Intel devices */
2737	hdev->set_quality_report = btintel_set_quality_report;
2738
2739	/* For Legacy device, check the HW platform value and size */
2740	if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
2741		bt_dev_dbg(hdev, "Read the legacy Intel version information");
2742
2743		memcpy(&ver, skb->data, sizeof(ver));
2744
2745		/* Display version information */
2746		btintel_version_info(hdev, &ver);
2747
2748		/* Check for supported iBT hardware variants of this firmware
2749		 * loading method.
2750		 *
2751		 * This check has been put in place to ensure correct forward
2752		 * compatibility options when newer hardware variants come
2753		 * along.
2754		 */
2755		switch (ver.hw_variant) {
2756		case 0x07:	/* WP */
2757		case 0x08:	/* StP */
2758			/* Legacy ROM product */
2759			btintel_set_flag(hdev, INTEL_ROM_LEGACY);
2760
2761			/* Apply the device specific HCI quirks
2762			 *
2763			 * WBS for SdP - For the Legacy ROM products, only SdP
2764			 * supports the WBS. But the version information is not
2765			 * enough to use here because the StP2 and SdP have same
2766			 * hw_variant and fw_variant. So, this flag is set by
2767			 * the transport driver (btusb) based on the HW info
2768			 * (idProduct)
2769			 */
2770			if (!btintel_test_flag(hdev,
2771					       INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
2772				set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2773					&hdev->quirks);
2774			if (ver.hw_variant == 0x08 && ver.fw_variant == 0x22)
2775				set_bit(HCI_QUIRK_VALID_LE_STATES,
2776					&hdev->quirks);
2777
2778			err = btintel_legacy_rom_setup(hdev, &ver);
2779			break;
2780		case 0x0b:      /* SfP */
2781		case 0x11:      /* JfP */
2782		case 0x12:      /* ThP */
2783		case 0x13:      /* HrP */
2784		case 0x14:      /* CcP */
2785			set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2786			fallthrough;
2787		case 0x0c:	/* WsP */
2788			/* Apply the device specific HCI quirks
2789			 *
2790			 * All Legacy bootloader devices support WBS
2791			 */
2792			set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
2793				&hdev->quirks);
2794
2795			/* These variants don't seem to support LE Coded PHY */
2796			set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
2797
2798			/* Setup MSFT Extension support */
2799			btintel_set_msft_opcode(hdev, ver.hw_variant);
2800
2801			err = btintel_bootloader_setup(hdev, &ver);
2802			btintel_register_devcoredump_support(hdev);
2803			break;
2804		default:
2805			bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2806				   ver.hw_variant);
2807			err = -EINVAL;
2808		}
2809
 
 
 
 
 
2810		goto exit_error;
2811	}
2812
2813	/* memset ver_tlv to start with clean state as few fields are exclusive
2814	 * to bootloader mode and are not populated in operational mode
2815	 */
2816	memset(&ver_tlv, 0, sizeof(ver_tlv));
2817	/* For TLV type device, parse the tlv data */
2818	err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
2819	if (err) {
2820		bt_dev_err(hdev, "Failed to parse TLV version information");
2821		goto exit_error;
2822	}
2823
2824	if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
2825		bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2826			   INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
2827		err = -EINVAL;
2828		goto exit_error;
2829	}
2830
2831	/* Check for supported iBT hardware variants of this firmware
2832	 * loading method.
2833	 *
2834	 * This check has been put in place to ensure correct forward
2835	 * compatibility options when newer hardware variants come
2836	 * along.
2837	 */
2838	switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
2839	case 0x11:      /* JfP */
2840	case 0x12:      /* ThP */
2841	case 0x13:      /* HrP */
2842	case 0x14:      /* CcP */
2843		/* Some legacy bootloader devices starting from JfP,
2844		 * the operational firmware supports both old and TLV based
2845		 * HCI_Intel_Read_Version command based on the command
2846		 * parameter.
2847		 *
2848		 * For upgrading firmware case, the TLV based version cannot
2849		 * be used because the firmware filename for legacy bootloader
2850		 * is based on the old format.
2851		 *
2852		 * Also, it is not easy to convert TLV based version from the
2853		 * legacy version format.
2854		 *
2855		 * So, as a workaround for those devices, use the legacy
2856		 * HCI_Intel_Read_Version to get the version information and
2857		 * run the legacy bootloader setup.
2858		 */
2859		err = btintel_read_version(hdev, &ver);
2860		if (err)
2861			break;
2862
2863		/* Apply the device specific HCI quirks
2864		 *
2865		 * All Legacy bootloader devices support WBS
2866		 */
2867		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2868
2869		/* These variants don't seem to support LE Coded PHY */
2870		set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
2871
2872		/* Set Valid LE States quirk */
2873		set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2874
2875		/* Setup MSFT Extension support */
2876		btintel_set_msft_opcode(hdev, ver.hw_variant);
2877
2878		err = btintel_bootloader_setup(hdev, &ver);
2879		btintel_register_devcoredump_support(hdev);
2880		break;
 
 
 
 
 
2881	case 0x17:
2882	case 0x18:
2883	case 0x19:
2884	case 0x1b:
2885	case 0x1c:
 
2886		/* Display version information of TLV type */
2887		btintel_version_info_tlv(hdev, &ver_tlv);
2888
2889		/* Apply the device specific HCI quirks for TLV based devices
2890		 *
2891		 * All TLV based devices support WBS
2892		 */
2893		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
2894
2895		/* Apply LE States quirk from solar onwards */
2896		set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
2897
2898		/* Setup MSFT Extension support */
2899		btintel_set_msft_opcode(hdev,
2900					INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2901		btintel_set_dsm_reset_method(hdev, &ver_tlv);
2902
2903		err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
 
 
 
2904		btintel_register_devcoredump_support(hdev);
 
2905		break;
2906	default:
2907		bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
2908			   INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
2909		err = -EINVAL;
2910		break;
2911	}
2912
 
 
 
 
2913exit_error:
2914	kfree_skb(skb);
2915
2916	return err;
2917}
2918
2919static int btintel_shutdown_combined(struct hci_dev *hdev)
2920{
2921	struct sk_buff *skb;
2922	int ret;
2923
2924	/* Send HCI Reset to the controller to stop any BT activity which
2925	 * were triggered. This will help to save power and maintain the
2926	 * sync b/w Host and controller
2927	 */
2928	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2929	if (IS_ERR(skb)) {
2930		bt_dev_err(hdev, "HCI reset during shutdown failed");
2931		return PTR_ERR(skb);
2932	}
2933	kfree_skb(skb);
2934
2935
2936	/* Some platforms have an issue with BT LED when the interface is
2937	 * down or BT radio is turned off, which takes 5 seconds to BT LED
2938	 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
2939	 * device in the RFKILL ON state which turns off the BT LED immediately.
2940	 */
2941	if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
2942		skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2943		if (IS_ERR(skb)) {
2944			ret = PTR_ERR(skb);
2945			bt_dev_err(hdev, "turning off Intel device LED failed");
2946			return ret;
2947		}
2948		kfree_skb(skb);
2949	}
2950
2951	return 0;
2952}
 
2953
2954int btintel_configure_setup(struct hci_dev *hdev, const char *driver_name)
2955{
2956	hdev->manufacturer = 2;
2957	hdev->setup = btintel_setup_combined;
2958	hdev->shutdown = btintel_shutdown_combined;
2959	hdev->hw_error = btintel_hw_error;
2960	hdev->set_diag = btintel_set_diag_combined;
2961	hdev->set_bdaddr = btintel_set_bdaddr;
2962
2963	coredump_info.driver_name = driver_name;
2964
2965	return 0;
2966}
2967EXPORT_SYMBOL_GPL(btintel_configure_setup);
2968
2969static int btintel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb)
2970{
2971	struct intel_tlv *tlv = (void *)&skb->data[5];
2972
2973	/* The first event is always an event type TLV */
2974	if (tlv->type != INTEL_TLV_TYPE_ID)
2975		goto recv_frame;
2976
2977	switch (tlv->val[0]) {
2978	case INTEL_TLV_SYSTEM_EXCEPTION:
2979	case INTEL_TLV_FATAL_EXCEPTION:
2980	case INTEL_TLV_DEBUG_EXCEPTION:
2981	case INTEL_TLV_TEST_EXCEPTION:
2982		/* Generate devcoredump from exception */
2983		if (!hci_devcd_init(hdev, skb->len)) {
2984			hci_devcd_append(hdev, skb);
2985			hci_devcd_complete(hdev);
2986		} else {
2987			bt_dev_err(hdev, "Failed to generate devcoredump");
2988			kfree_skb(skb);
2989		}
2990		return 0;
2991	default:
2992		bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]);
2993	}
2994
2995recv_frame:
2996	return hci_recv_frame(hdev, skb);
2997}
 
2998
2999int btintel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
3000{
3001	struct hci_event_hdr *hdr = (void *)skb->data;
3002	const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
3003
3004	if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
3005	    hdr->plen > 0) {
3006		const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
3007		unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
3008
3009		if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
3010			switch (skb->data[2]) {
3011			case 0x02:
3012				/* When switching to the operational firmware
3013				 * the device sends a vendor specific event
3014				 * indicating that the bootup completed.
3015				 */
3016				btintel_bootup(hdev, ptr, len);
3017				break;
 
3018			case 0x06:
3019				/* When the firmware loading completes the
3020				 * device sends out a vendor specific event
3021				 * indicating the result of the firmware
3022				 * loading.
3023				 */
3024				btintel_secure_send_result(hdev, ptr, len);
3025				break;
 
3026			}
3027		}
3028
3029		/* Handle all diagnostics events separately. May still call
3030		 * hci_recv_frame.
3031		 */
3032		if (len >= sizeof(diagnostics_hdr) &&
3033		    memcmp(&skb->data[2], diagnostics_hdr,
3034			   sizeof(diagnostics_hdr)) == 0) {
3035			return btintel_diagnostics(hdev, skb);
3036		}
3037	}
3038
3039	return hci_recv_frame(hdev, skb);
3040}
3041EXPORT_SYMBOL_GPL(btintel_recv_event);
3042
3043void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
3044{
3045	const struct intel_bootup *evt = ptr;
3046
3047	if (len != sizeof(*evt))
3048		return;
3049
3050	if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
3051		btintel_wake_up_flag(hdev, INTEL_BOOTING);
3052}
3053EXPORT_SYMBOL_GPL(btintel_bootup);
3054
3055void btintel_secure_send_result(struct hci_dev *hdev,
3056				const void *ptr, unsigned int len)
3057{
3058	const struct intel_secure_send_result *evt = ptr;
3059
3060	if (len != sizeof(*evt))
3061		return;
3062
3063	if (evt->result)
3064		btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
3065
3066	if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
3067	    btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
3068		btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
3069}
3070EXPORT_SYMBOL_GPL(btintel_secure_send_result);
3071
3072MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3073MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
3074MODULE_VERSION(VERSION);
3075MODULE_LICENSE("GPL");
3076MODULE_FIRMWARE("intel/ibt-11-5.sfi");
3077MODULE_FIRMWARE("intel/ibt-11-5.ddc");
3078MODULE_FIRMWARE("intel/ibt-12-16.sfi");
3079MODULE_FIRMWARE("intel/ibt-12-16.ddc");
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *
   4 *  Bluetooth support for Intel devices
   5 *
   6 *  Copyright (C) 2015  Intel Corporation
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/firmware.h>
  11#include <linux/regmap.h>
  12#include <linux/acpi.h>
  13#include <acpi/acpi_bus.h>
  14#include <linux/unaligned.h>
  15#include <linux/efi.h>
  16
  17#include <net/bluetooth/bluetooth.h>
  18#include <net/bluetooth/hci_core.h>
  19
  20#include "btintel.h"
  21
  22#define VERSION "0.1"
  23
  24#define BDADDR_INTEL		(&(bdaddr_t){{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
  25#define RSA_HEADER_LEN		644
  26#define CSS_HEADER_OFFSET	8
  27#define ECDSA_OFFSET		644
  28#define ECDSA_HEADER_LEN	320
  29
  30#define BTINTEL_EFI_DSBR	L"UefiCnvCommonDSBR"
  31
  32enum {
  33	DSM_SET_WDISABLE2_DELAY = 1,
  34	DSM_SET_RESET_METHOD = 3,
  35};
  36
 
 
 
 
 
 
 
 
  37#define CMD_WRITE_BOOT_PARAMS	0xfc0e
  38struct cmd_write_boot_params {
  39	__le32 boot_addr;
  40	u8  fw_build_num;
  41	u8  fw_build_ww;
  42	u8  fw_build_yy;
  43} __packed;
  44
  45static struct {
  46	const char *driver_name;
  47	u8         hw_variant;
  48	u32        fw_build_num;
  49} coredump_info;
  50
  51static const guid_t btintel_guid_dsm =
  52	GUID_INIT(0xaa10f4e0, 0x81ac, 0x4233,
  53		  0xab, 0xf6, 0x3b, 0x2a, 0xc5, 0x0e, 0x28, 0xd9);
  54
  55int btintel_check_bdaddr(struct hci_dev *hdev)
  56{
  57	struct hci_rp_read_bd_addr *bda;
  58	struct sk_buff *skb;
  59
  60	skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
  61			     HCI_INIT_TIMEOUT);
  62	if (IS_ERR(skb)) {
  63		int err = PTR_ERR(skb);
  64		bt_dev_err(hdev, "Reading Intel device address failed (%d)",
  65			   err);
  66		return err;
  67	}
  68
  69	if (skb->len != sizeof(*bda)) {
  70		bt_dev_err(hdev, "Intel device address length mismatch");
  71		kfree_skb(skb);
  72		return -EIO;
  73	}
  74
  75	bda = (struct hci_rp_read_bd_addr *)skb->data;
  76
  77	/* For some Intel based controllers, the default Bluetooth device
  78	 * address 00:03:19:9E:8B:00 can be found. These controllers are
  79	 * fully operational, but have the danger of duplicate addresses
  80	 * and that in turn can cause problems with Bluetooth operation.
  81	 */
  82	if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
  83		bt_dev_err(hdev, "Found Intel default device address (%pMR)",
  84			   &bda->bdaddr);
  85		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  86	}
  87
  88	kfree_skb(skb);
  89
  90	return 0;
  91}
  92EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
  93
  94int btintel_enter_mfg(struct hci_dev *hdev)
  95{
  96	static const u8 param[] = { 0x01, 0x00 };
  97	struct sk_buff *skb;
  98
  99	skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
 100	if (IS_ERR(skb)) {
 101		bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
 102			   PTR_ERR(skb));
 103		return PTR_ERR(skb);
 104	}
 105	kfree_skb(skb);
 106
 107	return 0;
 108}
 109EXPORT_SYMBOL_GPL(btintel_enter_mfg);
 110
 111int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
 112{
 113	u8 param[] = { 0x00, 0x00 };
 114	struct sk_buff *skb;
 115
 116	/* The 2nd command parameter specifies the manufacturing exit method:
 117	 * 0x00: Just disable the manufacturing mode (0x00).
 118	 * 0x01: Disable manufacturing mode and reset with patches deactivated.
 119	 * 0x02: Disable manufacturing mode and reset with patches activated.
 120	 */
 121	if (reset)
 122		param[1] |= patched ? 0x02 : 0x01;
 123
 124	skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
 125	if (IS_ERR(skb)) {
 126		bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
 127			   PTR_ERR(skb));
 128		return PTR_ERR(skb);
 129	}
 130	kfree_skb(skb);
 131
 132	return 0;
 133}
 134EXPORT_SYMBOL_GPL(btintel_exit_mfg);
 135
 136int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
 137{
 138	struct sk_buff *skb;
 139	int err;
 140
 141	skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
 142	if (IS_ERR(skb)) {
 143		err = PTR_ERR(skb);
 144		bt_dev_err(hdev, "Changing Intel device address failed (%d)",
 145			   err);
 146		return err;
 147	}
 148	kfree_skb(skb);
 149
 150	return 0;
 151}
 152EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
 153
 154static int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
 155{
 156	u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 157	struct sk_buff *skb;
 158	int err;
 159
 160	if (debug)
 161		mask[1] |= 0x62;
 162
 163	skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
 164	if (IS_ERR(skb)) {
 165		err = PTR_ERR(skb);
 166		bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
 167		return err;
 168	}
 169	kfree_skb(skb);
 170
 171	return 0;
 172}
 173
 174int btintel_set_diag(struct hci_dev *hdev, bool enable)
 175{
 176	struct sk_buff *skb;
 177	u8 param[3];
 178	int err;
 179
 180	if (enable) {
 181		param[0] = 0x03;
 182		param[1] = 0x03;
 183		param[2] = 0x03;
 184	} else {
 185		param[0] = 0x00;
 186		param[1] = 0x00;
 187		param[2] = 0x00;
 188	}
 189
 190	skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
 191	if (IS_ERR(skb)) {
 192		err = PTR_ERR(skb);
 193		if (err == -ENODATA)
 194			goto done;
 195		bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
 196			   err);
 197		return err;
 198	}
 199	kfree_skb(skb);
 200
 201done:
 202	btintel_set_event_mask(hdev, enable);
 203	return 0;
 204}
 205EXPORT_SYMBOL_GPL(btintel_set_diag);
 206
 207static int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
 208{
 209	int err, ret;
 210
 211	err = btintel_enter_mfg(hdev);
 212	if (err)
 213		return err;
 214
 215	ret = btintel_set_diag(hdev, enable);
 216
 217	err = btintel_exit_mfg(hdev, false, false);
 218	if (err)
 219		return err;
 220
 221	return ret;
 222}
 223
 224static int btintel_set_diag_combined(struct hci_dev *hdev, bool enable)
 225{
 226	int ret;
 227
 228	/* Legacy ROM device needs to be in the manufacturer mode to apply
 229	 * diagnostic setting
 230	 *
 231	 * This flag is set after reading the Intel version.
 232	 */
 233	if (btintel_test_flag(hdev, INTEL_ROM_LEGACY))
 234		ret = btintel_set_diag_mfg(hdev, enable);
 235	else
 236		ret = btintel_set_diag(hdev, enable);
 237
 238	return ret;
 239}
 240
 241void btintel_hw_error(struct hci_dev *hdev, u8 code)
 242{
 243	struct sk_buff *skb;
 244	u8 type = 0x00;
 245
 246	bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
 247
 248	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
 249	if (IS_ERR(skb)) {
 250		bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
 251			   PTR_ERR(skb));
 252		return;
 253	}
 254	kfree_skb(skb);
 255
 256	skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
 257	if (IS_ERR(skb)) {
 258		bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
 259			   PTR_ERR(skb));
 260		return;
 261	}
 262
 263	if (skb->len != 13) {
 264		bt_dev_err(hdev, "Exception info size mismatch");
 265		kfree_skb(skb);
 266		return;
 267	}
 268
 269	bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
 270
 271	kfree_skb(skb);
 272}
 273EXPORT_SYMBOL_GPL(btintel_hw_error);
 274
 275int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
 276{
 277	const char *variant;
 278
 279	/* The hardware platform number has a fixed value of 0x37 and
 280	 * for now only accept this single value.
 281	 */
 282	if (ver->hw_platform != 0x37) {
 283		bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
 284			   ver->hw_platform);
 285		return -EINVAL;
 286	}
 287
 288	/* Check for supported iBT hardware variants of this firmware
 289	 * loading method.
 290	 *
 291	 * This check has been put in place to ensure correct forward
 292	 * compatibility options when newer hardware variants come along.
 293	 */
 294	switch (ver->hw_variant) {
 295	case 0x07:	/* WP - Legacy ROM */
 296	case 0x08:	/* StP - Legacy ROM */
 297	case 0x0b:      /* SfP */
 298	case 0x0c:      /* WsP */
 299	case 0x11:      /* JfP */
 300	case 0x12:      /* ThP */
 301	case 0x13:      /* HrP */
 302	case 0x14:      /* CcP */
 303		break;
 304	default:
 305		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
 306			   ver->hw_variant);
 307		return -EINVAL;
 308	}
 309
 310	switch (ver->fw_variant) {
 311	case 0x01:
 312		variant = "Legacy ROM 2.5";
 313		break;
 314	case 0x06:
 315		variant = "Bootloader";
 316		break;
 317	case 0x22:
 318		variant = "Legacy ROM 2.x";
 319		break;
 320	case 0x23:
 321		variant = "Firmware";
 322		break;
 323	default:
 324		bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
 325		return -EINVAL;
 326	}
 327
 328	coredump_info.hw_variant = ver->hw_variant;
 329	coredump_info.fw_build_num = ver->fw_build_num;
 330
 331	bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
 332		    variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
 333		    ver->fw_build_num, ver->fw_build_ww,
 334		    2000 + ver->fw_build_yy);
 335
 336	return 0;
 337}
 338EXPORT_SYMBOL_GPL(btintel_version_info);
 339
 340static int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
 341			       const void *param)
 342{
 343	while (plen > 0) {
 344		struct sk_buff *skb;
 345		u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
 346
 347		cmd_param[0] = fragment_type;
 348		memcpy(cmd_param + 1, param, fragment_len);
 349
 350		skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
 351				     cmd_param, HCI_INIT_TIMEOUT);
 352		if (IS_ERR(skb))
 353			return PTR_ERR(skb);
 354
 355		kfree_skb(skb);
 356
 357		plen -= fragment_len;
 358		param += fragment_len;
 359	}
 360
 361	return 0;
 362}
 363
 364int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
 365{
 366	const struct firmware *fw;
 367	struct sk_buff *skb;
 368	const u8 *fw_ptr;
 369	int err;
 370
 371	err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
 372	if (err < 0) {
 373		bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
 374			   ddc_name, err);
 375		return err;
 376	}
 377
 378	bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
 379
 380	fw_ptr = fw->data;
 381
 382	/* DDC file contains one or more DDC structure which has
 383	 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
 384	 */
 385	while (fw->size > fw_ptr - fw->data) {
 386		u8 cmd_plen = fw_ptr[0] + sizeof(u8);
 387
 388		skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
 389				     HCI_INIT_TIMEOUT);
 390		if (IS_ERR(skb)) {
 391			bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
 392				   PTR_ERR(skb));
 393			release_firmware(fw);
 394			return PTR_ERR(skb);
 395		}
 396
 397		fw_ptr += cmd_plen;
 398		kfree_skb(skb);
 399	}
 400
 401	release_firmware(fw);
 402
 403	bt_dev_info(hdev, "Applying Intel DDC parameters completed");
 404
 405	return 0;
 406}
 407EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
 408
 409int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
 410{
 411	int err, ret;
 412
 413	err = btintel_enter_mfg(hdev);
 414	if (err)
 415		return err;
 416
 417	ret = btintel_set_event_mask(hdev, debug);
 418
 419	err = btintel_exit_mfg(hdev, false, false);
 420	if (err)
 421		return err;
 422
 423	return ret;
 424}
 425EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
 426
 427int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
 428{
 429	struct sk_buff *skb;
 430
 431	skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
 432	if (IS_ERR(skb)) {
 433		bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
 434			   PTR_ERR(skb));
 435		return PTR_ERR(skb);
 436	}
 437
 438	if (!skb || skb->len != sizeof(*ver)) {
 439		bt_dev_err(hdev, "Intel version event size mismatch");
 440		kfree_skb(skb);
 441		return -EILSEQ;
 442	}
 443
 444	memcpy(ver, skb->data, sizeof(*ver));
 445
 446	kfree_skb(skb);
 447
 448	return 0;
 449}
 450EXPORT_SYMBOL_GPL(btintel_read_version);
 451
 452int btintel_version_info_tlv(struct hci_dev *hdev,
 453			     struct intel_version_tlv *version)
 454{
 455	const char *variant;
 456
 457	/* The hardware platform number has a fixed value of 0x37 and
 458	 * for now only accept this single value.
 459	 */
 460	if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
 461		bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
 462			   INTEL_HW_PLATFORM(version->cnvi_bt));
 463		return -EINVAL;
 464	}
 465
 466	/* Check for supported iBT hardware variants of this firmware
 467	 * loading method.
 468	 *
 469	 * This check has been put in place to ensure correct forward
 470	 * compatibility options when newer hardware variants come along.
 471	 */
 472	switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
 473	case 0x17:	/* TyP */
 474	case 0x18:	/* Slr */
 475	case 0x19:	/* Slr-F */
 476	case 0x1b:      /* Mgr */
 477	case 0x1c:	/* Gale Peak (GaP) */
 478	case 0x1d:	/* BlazarU (BzrU) */
 479	case 0x1e:	/* BlazarI (Bzr) */
 480		break;
 481	default:
 482		bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
 483			   INTEL_HW_VARIANT(version->cnvi_bt));
 484		return -EINVAL;
 485	}
 486
 487	switch (version->img_type) {
 488	case BTINTEL_IMG_BOOTLOADER:
 489		variant = "Bootloader";
 490		/* It is required that every single firmware fragment is acknowledged
 491		 * with a command complete event. If the boot parameters indicate
 492		 * that this bootloader does not send them, then abort the setup.
 493		 */
 494		if (version->limited_cce != 0x00) {
 495			bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
 496				   version->limited_cce);
 497			return -EINVAL;
 498		}
 499
 500		/* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
 501		if (version->sbe_type > 0x01) {
 502			bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
 503				   version->sbe_type);
 504			return -EINVAL;
 505		}
 506
 507		bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
 508		bt_dev_info(hdev, "Secure boot is %s",
 509			    version->secure_boot ? "enabled" : "disabled");
 510		bt_dev_info(hdev, "OTP lock is %s",
 511			    version->otp_lock ? "enabled" : "disabled");
 512		bt_dev_info(hdev, "API lock is %s",
 513			    version->api_lock ? "enabled" : "disabled");
 514		bt_dev_info(hdev, "Debug lock is %s",
 515			    version->debug_lock ? "enabled" : "disabled");
 516		bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
 517			    version->min_fw_build_nn, version->min_fw_build_cw,
 518			    2000 + version->min_fw_build_yy);
 519		break;
 520	case BTINTEL_IMG_IML:
 521		variant = "Intermediate loader";
 522		break;
 523	case BTINTEL_IMG_OP:
 524		variant = "Firmware";
 525		break;
 526	default:
 527		bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
 528		return -EINVAL;
 529	}
 530
 531	coredump_info.hw_variant = INTEL_HW_VARIANT(version->cnvi_bt);
 532	coredump_info.fw_build_num = version->build_num;
 533
 534	bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
 535		    2000 + (version->timestamp >> 8), version->timestamp & 0xff,
 536		    version->build_type, version->build_num);
 537	if (version->img_type == BTINTEL_IMG_OP)
 538		bt_dev_info(hdev, "Firmware SHA1: 0x%8.8x", version->git_sha1);
 539
 540	return 0;
 541}
 542EXPORT_SYMBOL_GPL(btintel_version_info_tlv);
 543
 544int btintel_parse_version_tlv(struct hci_dev *hdev,
 545			      struct intel_version_tlv *version,
 546			      struct sk_buff *skb)
 547{
 548	/* Consume Command Complete Status field */
 549	skb_pull(skb, 1);
 550
 551	/* Event parameters contatin multiple TLVs. Read each of them
 552	 * and only keep the required data. Also, it use existing legacy
 553	 * version field like hw_platform, hw_variant, and fw_variant
 554	 * to keep the existing setup flow
 555	 */
 556	while (skb->len) {
 557		struct intel_tlv *tlv;
 558
 559		/* Make sure skb has a minimum length of the header */
 560		if (skb->len < sizeof(*tlv))
 561			return -EINVAL;
 562
 563		tlv = (struct intel_tlv *)skb->data;
 564
 565		/* Make sure skb has a enough data */
 566		if (skb->len < tlv->len + sizeof(*tlv))
 567			return -EINVAL;
 568
 569		switch (tlv->type) {
 570		case INTEL_TLV_CNVI_TOP:
 571			version->cnvi_top = get_unaligned_le32(tlv->val);
 572			break;
 573		case INTEL_TLV_CNVR_TOP:
 574			version->cnvr_top = get_unaligned_le32(tlv->val);
 575			break;
 576		case INTEL_TLV_CNVI_BT:
 577			version->cnvi_bt = get_unaligned_le32(tlv->val);
 578			break;
 579		case INTEL_TLV_CNVR_BT:
 580			version->cnvr_bt = get_unaligned_le32(tlv->val);
 581			break;
 582		case INTEL_TLV_DEV_REV_ID:
 583			version->dev_rev_id = get_unaligned_le16(tlv->val);
 584			break;
 585		case INTEL_TLV_IMAGE_TYPE:
 586			version->img_type = tlv->val[0];
 587			break;
 588		case INTEL_TLV_TIME_STAMP:
 589			/* If image type is Operational firmware (0x03), then
 590			 * running FW Calendar Week and Year information can
 591			 * be extracted from Timestamp information
 592			 */
 593			version->min_fw_build_cw = tlv->val[0];
 594			version->min_fw_build_yy = tlv->val[1];
 595			version->timestamp = get_unaligned_le16(tlv->val);
 596			break;
 597		case INTEL_TLV_BUILD_TYPE:
 598			version->build_type = tlv->val[0];
 599			break;
 600		case INTEL_TLV_BUILD_NUM:
 601			/* If image type is Operational firmware (0x03), then
 602			 * running FW build number can be extracted from the
 603			 * Build information
 604			 */
 605			version->min_fw_build_nn = tlv->val[0];
 606			version->build_num = get_unaligned_le32(tlv->val);
 607			break;
 608		case INTEL_TLV_SECURE_BOOT:
 609			version->secure_boot = tlv->val[0];
 610			break;
 611		case INTEL_TLV_OTP_LOCK:
 612			version->otp_lock = tlv->val[0];
 613			break;
 614		case INTEL_TLV_API_LOCK:
 615			version->api_lock = tlv->val[0];
 616			break;
 617		case INTEL_TLV_DEBUG_LOCK:
 618			version->debug_lock = tlv->val[0];
 619			break;
 620		case INTEL_TLV_MIN_FW:
 621			version->min_fw_build_nn = tlv->val[0];
 622			version->min_fw_build_cw = tlv->val[1];
 623			version->min_fw_build_yy = tlv->val[2];
 624			break;
 625		case INTEL_TLV_LIMITED_CCE:
 626			version->limited_cce = tlv->val[0];
 627			break;
 628		case INTEL_TLV_SBE_TYPE:
 629			version->sbe_type = tlv->val[0];
 630			break;
 631		case INTEL_TLV_OTP_BDADDR:
 632			memcpy(&version->otp_bd_addr, tlv->val,
 633							sizeof(bdaddr_t));
 634			break;
 635		case INTEL_TLV_GIT_SHA1:
 636			version->git_sha1 = get_unaligned_le32(tlv->val);
 637			break;
 638		case INTEL_TLV_FW_ID:
 639			snprintf(version->fw_id, sizeof(version->fw_id),
 640				 "%s", tlv->val);
 641			break;
 642		default:
 643			/* Ignore rest of information */
 644			break;
 645		}
 646		/* consume the current tlv and move to next*/
 647		skb_pull(skb, tlv->len + sizeof(*tlv));
 648	}
 649
 650	return 0;
 651}
 652EXPORT_SYMBOL_GPL(btintel_parse_version_tlv);
 653
 654static int btintel_read_version_tlv(struct hci_dev *hdev,
 655				    struct intel_version_tlv *version)
 656{
 657	struct sk_buff *skb;
 658	const u8 param[1] = { 0xFF };
 659
 660	if (!version)
 661		return -EINVAL;
 662
 663	skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
 664	if (IS_ERR(skb)) {
 665		bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
 666			   PTR_ERR(skb));
 667		return PTR_ERR(skb);
 668	}
 669
 670	if (skb->data[0]) {
 671		bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
 672			   skb->data[0]);
 673		kfree_skb(skb);
 674		return -EIO;
 675	}
 676
 677	btintel_parse_version_tlv(hdev, version, skb);
 678
 679	kfree_skb(skb);
 680	return 0;
 681}
 682
 683/* ------- REGMAP IBT SUPPORT ------- */
 684
 685#define IBT_REG_MODE_8BIT  0x00
 686#define IBT_REG_MODE_16BIT 0x01
 687#define IBT_REG_MODE_32BIT 0x02
 688
 689struct regmap_ibt_context {
 690	struct hci_dev *hdev;
 691	__u16 op_write;
 692	__u16 op_read;
 693};
 694
 695struct ibt_cp_reg_access {
 696	__le32  addr;
 697	__u8    mode;
 698	__u8    len;
 699	__u8    data[];
 700} __packed;
 701
 702struct ibt_rp_reg_access {
 703	__u8    status;
 704	__le32  addr;
 705	__u8    data[];
 706} __packed;
 707
 708static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
 709			   void *val, size_t val_size)
 710{
 711	struct regmap_ibt_context *ctx = context;
 712	struct ibt_cp_reg_access cp;
 713	struct ibt_rp_reg_access *rp;
 714	struct sk_buff *skb;
 715	int err = 0;
 716
 717	if (reg_size != sizeof(__le32))
 718		return -EINVAL;
 719
 720	switch (val_size) {
 721	case 1:
 722		cp.mode = IBT_REG_MODE_8BIT;
 723		break;
 724	case 2:
 725		cp.mode = IBT_REG_MODE_16BIT;
 726		break;
 727	case 4:
 728		cp.mode = IBT_REG_MODE_32BIT;
 729		break;
 730	default:
 731		return -EINVAL;
 732	}
 733
 734	/* regmap provides a little-endian formatted addr */
 735	cp.addr = *(__le32 *)addr;
 736	cp.len = val_size;
 737
 738	bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
 739
 740	skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
 741			   HCI_CMD_TIMEOUT);
 742	if (IS_ERR(skb)) {
 743		err = PTR_ERR(skb);
 744		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
 745			   le32_to_cpu(cp.addr), err);
 746		return err;
 747	}
 748
 749	if (skb->len != sizeof(*rp) + val_size) {
 750		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
 751			   le32_to_cpu(cp.addr));
 752		err = -EINVAL;
 753		goto done;
 754	}
 755
 756	rp = (struct ibt_rp_reg_access *)skb->data;
 757
 758	if (rp->addr != cp.addr) {
 759		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
 760			   le32_to_cpu(rp->addr));
 761		err = -EINVAL;
 762		goto done;
 763	}
 764
 765	memcpy(val, rp->data, val_size);
 766
 767done:
 768	kfree_skb(skb);
 769	return err;
 770}
 771
 772static int regmap_ibt_gather_write(void *context,
 773				   const void *addr, size_t reg_size,
 774				   const void *val, size_t val_size)
 775{
 776	struct regmap_ibt_context *ctx = context;
 777	struct ibt_cp_reg_access *cp;
 778	struct sk_buff *skb;
 779	int plen = sizeof(*cp) + val_size;
 780	u8 mode;
 781	int err = 0;
 782
 783	if (reg_size != sizeof(__le32))
 784		return -EINVAL;
 785
 786	switch (val_size) {
 787	case 1:
 788		mode = IBT_REG_MODE_8BIT;
 789		break;
 790	case 2:
 791		mode = IBT_REG_MODE_16BIT;
 792		break;
 793	case 4:
 794		mode = IBT_REG_MODE_32BIT;
 795		break;
 796	default:
 797		return -EINVAL;
 798	}
 799
 800	cp = kmalloc(plen, GFP_KERNEL);
 801	if (!cp)
 802		return -ENOMEM;
 803
 804	/* regmap provides a little-endian formatted addr/value */
 805	cp->addr = *(__le32 *)addr;
 806	cp->mode = mode;
 807	cp->len = val_size;
 808	memcpy(&cp->data, val, val_size);
 809
 810	bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
 811
 812	skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
 813	if (IS_ERR(skb)) {
 814		err = PTR_ERR(skb);
 815		bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
 816			   le32_to_cpu(cp->addr), err);
 817		goto done;
 818	}
 819	kfree_skb(skb);
 820
 821done:
 822	kfree(cp);
 823	return err;
 824}
 825
 826static int regmap_ibt_write(void *context, const void *data, size_t count)
 827{
 828	/* data contains register+value, since we only support 32bit addr,
 829	 * minimum data size is 4 bytes.
 830	 */
 831	if (WARN_ONCE(count < 4, "Invalid register access"))
 832		return -EINVAL;
 833
 834	return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
 835}
 836
 837static void regmap_ibt_free_context(void *context)
 838{
 839	kfree(context);
 840}
 841
 842static const struct regmap_bus regmap_ibt = {
 843	.read = regmap_ibt_read,
 844	.write = regmap_ibt_write,
 845	.gather_write = regmap_ibt_gather_write,
 846	.free_context = regmap_ibt_free_context,
 847	.reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
 848	.val_format_endian_default = REGMAP_ENDIAN_LITTLE,
 849};
 850
 851/* Config is the same for all register regions */
 852static const struct regmap_config regmap_ibt_cfg = {
 853	.name      = "btintel_regmap",
 854	.reg_bits  = 32,
 855	.val_bits  = 32,
 856};
 857
 858struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
 859				   u16 opcode_write)
 860{
 861	struct regmap_ibt_context *ctx;
 862
 863	bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
 864		    opcode_write);
 865
 866	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 867	if (!ctx)
 868		return ERR_PTR(-ENOMEM);
 869
 870	ctx->op_read = opcode_read;
 871	ctx->op_write = opcode_write;
 872	ctx->hdev = hdev;
 873
 874	return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_ibt_cfg);
 875}
 876EXPORT_SYMBOL_GPL(btintel_regmap_init);
 877
 878int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
 879{
 880	struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
 881	struct sk_buff *skb;
 882
 883	params.boot_param = cpu_to_le32(boot_param);
 884
 885	skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), &params,
 886			     HCI_INIT_TIMEOUT);
 887	if (IS_ERR(skb)) {
 888		bt_dev_err(hdev, "Failed to send Intel Reset command");
 889		return PTR_ERR(skb);
 890	}
 891
 892	kfree_skb(skb);
 893
 894	return 0;
 895}
 896EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
 897
 898int btintel_read_boot_params(struct hci_dev *hdev,
 899			     struct intel_boot_params *params)
 900{
 901	struct sk_buff *skb;
 902
 903	skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
 904	if (IS_ERR(skb)) {
 905		bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
 906			   PTR_ERR(skb));
 907		return PTR_ERR(skb);
 908	}
 909
 910	if (skb->len != sizeof(*params)) {
 911		bt_dev_err(hdev, "Intel boot parameters size mismatch");
 912		kfree_skb(skb);
 913		return -EILSEQ;
 914	}
 915
 916	memcpy(params, skb->data, sizeof(*params));
 917
 918	kfree_skb(skb);
 919
 920	if (params->status) {
 921		bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
 922			   params->status);
 923		return -bt_to_errno(params->status);
 924	}
 925
 926	bt_dev_info(hdev, "Device revision is %u",
 927		    le16_to_cpu(params->dev_revid));
 928
 929	bt_dev_info(hdev, "Secure boot is %s",
 930		    params->secure_boot ? "enabled" : "disabled");
 931
 932	bt_dev_info(hdev, "OTP lock is %s",
 933		    params->otp_lock ? "enabled" : "disabled");
 934
 935	bt_dev_info(hdev, "API lock is %s",
 936		    params->api_lock ? "enabled" : "disabled");
 937
 938	bt_dev_info(hdev, "Debug lock is %s",
 939		    params->debug_lock ? "enabled" : "disabled");
 940
 941	bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
 942		    params->min_fw_build_nn, params->min_fw_build_cw,
 943		    2000 + params->min_fw_build_yy);
 944
 945	return 0;
 946}
 947EXPORT_SYMBOL_GPL(btintel_read_boot_params);
 948
 949static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
 950					      const struct firmware *fw)
 951{
 952	int err;
 953
 954	/* Start the firmware download transaction with the Init fragment
 955	 * represented by the 128 bytes of CSS header.
 956	 */
 957	err = btintel_secure_send(hdev, 0x00, 128, fw->data);
 958	if (err < 0) {
 959		bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
 960		goto done;
 961	}
 962
 963	/* Send the 256 bytes of public key information from the firmware
 964	 * as the PKey fragment.
 965	 */
 966	err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
 967	if (err < 0) {
 968		bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
 969		goto done;
 970	}
 971
 972	/* Send the 256 bytes of signature information from the firmware
 973	 * as the Sign fragment.
 974	 */
 975	err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
 976	if (err < 0) {
 977		bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
 978		goto done;
 979	}
 980
 981done:
 982	return err;
 983}
 984
 985static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
 986						const struct firmware *fw)
 987{
 988	int err;
 989
 990	/* Start the firmware download transaction with the Init fragment
 991	 * represented by the 128 bytes of CSS header.
 992	 */
 993	err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
 994	if (err < 0) {
 995		bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
 996		return err;
 997	}
 998
 999	/* Send the 96 bytes of public key information from the firmware
1000	 * as the PKey fragment.
1001	 */
1002	err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
1003	if (err < 0) {
1004		bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
1005		return err;
1006	}
1007
1008	/* Send the 96 bytes of signature information from the firmware
1009	 * as the Sign fragment
1010	 */
1011	err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
1012	if (err < 0) {
1013		bt_dev_err(hdev, "Failed to send firmware signature (%d)",
1014			   err);
1015		return err;
1016	}
1017	return 0;
1018}
1019
1020static int btintel_download_firmware_payload(struct hci_dev *hdev,
1021					     const struct firmware *fw,
1022					     size_t offset)
1023{
1024	int err;
1025	const u8 *fw_ptr;
1026	u32 frag_len;
1027
1028	fw_ptr = fw->data + offset;
1029	frag_len = 0;
1030	err = -EINVAL;
1031
1032	while (fw_ptr - fw->data < fw->size) {
1033		struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
1034
1035		frag_len += sizeof(*cmd) + cmd->plen;
1036
1037		/* The parameter length of the secure send command requires
1038		 * a 4 byte alignment. It happens so that the firmware file
1039		 * contains proper Intel_NOP commands to align the fragments
1040		 * as needed.
1041		 *
1042		 * Send set of commands with 4 byte alignment from the
1043		 * firmware data buffer as a single Data fragment.
1044		 */
1045		if (!(frag_len % 4)) {
1046			err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1047			if (err < 0) {
1048				bt_dev_err(hdev,
1049					   "Failed to send firmware data (%d)",
1050					   err);
1051				goto done;
1052			}
1053
1054			fw_ptr += frag_len;
1055			frag_len = 0;
1056		}
1057	}
1058
1059done:
1060	return err;
1061}
1062
1063static bool btintel_firmware_version(struct hci_dev *hdev,
1064				     u8 num, u8 ww, u8 yy,
1065				     const struct firmware *fw,
1066				     u32 *boot_addr)
1067{
1068	const u8 *fw_ptr;
1069
1070	fw_ptr = fw->data;
1071
1072	while (fw_ptr - fw->data < fw->size) {
1073		struct hci_command_hdr *cmd = (void *)(fw_ptr);
1074
1075		/* Each SKU has a different reset parameter to use in the
1076		 * HCI_Intel_Reset command and it is embedded in the firmware
1077		 * data. So, instead of using static value per SKU, check
1078		 * the firmware data and save it for later use.
1079		 */
1080		if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1081			struct cmd_write_boot_params *params;
1082
1083			params = (void *)(fw_ptr + sizeof(*cmd));
1084
1085			*boot_addr = le32_to_cpu(params->boot_addr);
1086
1087			bt_dev_info(hdev, "Boot Address: 0x%x", *boot_addr);
1088
1089			bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1090				    params->fw_build_num, params->fw_build_ww,
1091				    params->fw_build_yy);
1092
1093			return (num == params->fw_build_num &&
1094				ww == params->fw_build_ww &&
1095				yy == params->fw_build_yy);
1096		}
1097
1098		fw_ptr += sizeof(*cmd) + cmd->plen;
1099	}
1100
1101	return false;
1102}
1103
1104int btintel_download_firmware(struct hci_dev *hdev,
1105			      struct intel_version *ver,
1106			      const struct firmware *fw,
1107			      u32 *boot_param)
1108{
1109	int err;
1110
1111	/* SfP and WsP don't seem to update the firmware version on file
1112	 * so version checking is currently not possible.
1113	 */
1114	switch (ver->hw_variant) {
1115	case 0x0b:	/* SfP */
1116	case 0x0c:	/* WsP */
1117		/* Skip version checking */
1118		break;
1119	default:
1120
1121		/* Skip download if firmware has the same version */
1122		if (btintel_firmware_version(hdev, ver->fw_build_num,
1123					     ver->fw_build_ww, ver->fw_build_yy,
1124					     fw, boot_param)) {
1125			bt_dev_info(hdev, "Firmware already loaded");
1126			/* Return -EALREADY to indicate that the firmware has
1127			 * already been loaded.
1128			 */
1129			return -EALREADY;
1130		}
1131	}
1132
1133	/* The firmware variant determines if the device is in bootloader
1134	 * mode or is running operational firmware. The value 0x06 identifies
1135	 * the bootloader and the value 0x23 identifies the operational
1136	 * firmware.
1137	 *
1138	 * If the firmware version has changed that means it needs to be reset
1139	 * to bootloader when operational so the new firmware can be loaded.
1140	 */
1141	if (ver->fw_variant == 0x23)
1142		return -EINVAL;
1143
1144	err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1145	if (err)
1146		return err;
1147
1148	return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1149}
1150EXPORT_SYMBOL_GPL(btintel_download_firmware);
1151
1152static int btintel_download_fw_tlv(struct hci_dev *hdev,
1153				   struct intel_version_tlv *ver,
1154				   const struct firmware *fw, u32 *boot_param,
1155				   u8 hw_variant, u8 sbe_type)
1156{
1157	int err;
1158	u32 css_header_ver;
1159
1160	/* Skip download if firmware has the same version */
1161	if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1162				     ver->min_fw_build_cw,
1163				     ver->min_fw_build_yy,
1164				     fw, boot_param)) {
1165		bt_dev_info(hdev, "Firmware already loaded");
1166		/* Return -EALREADY to indicate that firmware has
1167		 * already been loaded.
1168		 */
1169		return -EALREADY;
1170	}
1171
1172	/* The firmware variant determines if the device is in bootloader
1173	 * mode or is running operational firmware. The value 0x01 identifies
1174	 * the bootloader and the value 0x03 identifies the operational
1175	 * firmware.
1176	 *
1177	 * If the firmware version has changed that means it needs to be reset
1178	 * to bootloader when operational so the new firmware can be loaded.
1179	 */
1180	if (ver->img_type == BTINTEL_IMG_OP)
1181		return -EINVAL;
1182
1183	/* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1184	 * only RSA secure boot engine. Hence, the corresponding sfi file will
1185	 * have RSA header of 644 bytes followed by Command Buffer.
1186	 *
1187	 * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1188	 * secure boot engine. As a result, the corresponding sfi file will
1189	 * have RSA header of 644, ECDSA header of 320 bytes followed by
1190	 * Command Buffer.
1191	 *
1192	 * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1193	 * version: RSA(0x00010000) , ECDSA (0x00020000)
1194	 */
1195	css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1196	if (css_header_ver != 0x00010000) {
1197		bt_dev_err(hdev, "Invalid CSS Header version");
1198		return -EINVAL;
1199	}
1200
1201	if (hw_variant <= 0x14) {
1202		if (sbe_type != 0x00) {
1203			bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1204				   hw_variant);
1205			return -EINVAL;
1206		}
1207
1208		err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1209		if (err)
1210			return err;
1211
1212		err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1213		if (err)
1214			return err;
1215	} else if (hw_variant >= 0x17) {
1216		/* Check if CSS header for ECDSA follows the RSA header */
1217		if (fw->data[ECDSA_OFFSET] != 0x06)
1218			return -EINVAL;
1219
1220		/* Check if the CSS Header version is ECDSA(0x00020000) */
1221		css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1222		if (css_header_ver != 0x00020000) {
1223			bt_dev_err(hdev, "Invalid CSS Header version");
1224			return -EINVAL;
1225		}
1226
1227		if (sbe_type == 0x00) {
1228			err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1229			if (err)
1230				return err;
1231
1232			err = btintel_download_firmware_payload(hdev, fw,
1233								RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1234			if (err)
1235				return err;
1236		} else if (sbe_type == 0x01) {
1237			err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1238			if (err)
1239				return err;
1240
1241			err = btintel_download_firmware_payload(hdev, fw,
1242								RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1243			if (err)
1244				return err;
1245		}
1246	}
1247	return 0;
1248}
1249
1250static void btintel_reset_to_bootloader(struct hci_dev *hdev)
1251{
1252	struct intel_reset params;
1253	struct sk_buff *skb;
1254
1255	/* PCIe transport uses shared hardware reset mechanism for recovery
1256	 * which gets triggered in pcie *setup* function on error.
1257	 */
1258	if (hdev->bus == HCI_PCI)
1259		return;
1260
1261	/* Send Intel Reset command. This will result in
1262	 * re-enumeration of BT controller.
1263	 *
1264	 * Intel Reset parameter description:
1265	 * reset_type :   0x00 (Soft reset),
1266	 *		  0x01 (Hard reset)
1267	 * patch_enable : 0x00 (Do not enable),
1268	 *		  0x01 (Enable)
1269	 * ddc_reload :   0x00 (Do not reload),
1270	 *		  0x01 (Reload)
1271	 * boot_option:   0x00 (Current image),
1272	 *                0x01 (Specified boot address)
1273	 * boot_param:    Boot address
1274	 *
1275	 */
1276
1277	params.reset_type = 0x01;
1278	params.patch_enable = 0x01;
1279	params.ddc_reload = 0x01;
1280	params.boot_option = 0x00;
1281	params.boot_param = cpu_to_le32(0x00000000);
1282
1283	skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1284			     &params, HCI_INIT_TIMEOUT);
1285	if (IS_ERR(skb)) {
1286		bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1287			   PTR_ERR(skb));
1288		return;
1289	}
1290	bt_dev_info(hdev, "Intel reset sent to retry FW download");
1291	kfree_skb(skb);
1292
1293	/* Current Intel BT controllers(ThP/JfP) hold the USB reset
1294	 * lines for 2ms when it receives Intel Reset in bootloader mode.
1295	 * Whereas, the upcoming Intel BT controllers will hold USB reset
1296	 * for 150ms. To keep the delay generic, 150ms is chosen here.
1297	 */
1298	msleep(150);
1299}
1300
1301static int btintel_read_debug_features(struct hci_dev *hdev,
1302				       struct intel_debug_features *features)
1303{
1304	struct sk_buff *skb;
1305	u8 page_no = 1;
1306
1307	/* Intel controller supports two pages, each page is of 128-bit
1308	 * feature bit mask. And each bit defines specific feature support
1309	 */
1310	skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1311			     HCI_INIT_TIMEOUT);
1312	if (IS_ERR(skb)) {
1313		bt_dev_err(hdev, "Reading supported features failed (%ld)",
1314			   PTR_ERR(skb));
1315		return PTR_ERR(skb);
1316	}
1317
1318	if (skb->len != (sizeof(features->page1) + 3)) {
1319		bt_dev_err(hdev, "Supported features event size mismatch");
1320		kfree_skb(skb);
1321		return -EILSEQ;
1322	}
1323
1324	memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1325
1326	/* Read the supported features page2 if required in future.
1327	 */
1328	kfree_skb(skb);
1329	return 0;
1330}
1331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1332static int btintel_set_debug_features(struct hci_dev *hdev,
1333			       const struct intel_debug_features *features)
1334{
1335	u8 mask[11] = { 0x0a, 0x92, 0x02, 0x7f, 0x00, 0x00, 0x00, 0x00,
1336			0x00, 0x00, 0x00 };
1337	u8 period[5] = { 0x04, 0x91, 0x02, 0x05, 0x00 };
1338	u8 trace_enable = 0x02;
1339	struct sk_buff *skb;
1340
1341	if (!features) {
1342		bt_dev_warn(hdev, "Debug features not read");
1343		return -EINVAL;
1344	}
1345
1346	if (!(features->page1[0] & 0x3f)) {
1347		bt_dev_info(hdev, "Telemetry exception format not supported");
1348		return 0;
1349	}
1350
1351	skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1352	if (IS_ERR(skb)) {
1353		bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1354			   PTR_ERR(skb));
1355		return PTR_ERR(skb);
1356	}
1357	kfree_skb(skb);
1358
1359	skb = __hci_cmd_sync(hdev, 0xfc8b, 5, period, HCI_INIT_TIMEOUT);
1360	if (IS_ERR(skb)) {
1361		bt_dev_err(hdev, "Setting periodicity for link statistics traces failed (%ld)",
1362			   PTR_ERR(skb));
1363		return PTR_ERR(skb);
1364	}
1365	kfree_skb(skb);
1366
1367	skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1368	if (IS_ERR(skb)) {
1369		bt_dev_err(hdev, "Enable tracing of link statistics events failed (%ld)",
1370			   PTR_ERR(skb));
1371		return PTR_ERR(skb);
1372	}
1373	kfree_skb(skb);
1374
1375	bt_dev_info(hdev, "set debug features: trace_enable 0x%02x mask 0x%02x",
1376		    trace_enable, mask[3]);
1377
1378	return 0;
1379}
1380
1381static int btintel_reset_debug_features(struct hci_dev *hdev,
1382				 const struct intel_debug_features *features)
1383{
1384	u8 mask[11] = { 0x0a, 0x92, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
1385			0x00, 0x00, 0x00 };
1386	u8 trace_enable = 0x00;
1387	struct sk_buff *skb;
1388
1389	if (!features) {
1390		bt_dev_warn(hdev, "Debug features not read");
1391		return -EINVAL;
1392	}
1393
1394	if (!(features->page1[0] & 0x3f)) {
1395		bt_dev_info(hdev, "Telemetry exception format not supported");
1396		return 0;
1397	}
1398
1399	/* Should stop the trace before writing ddc event mask. */
1400	skb = __hci_cmd_sync(hdev, 0xfca1, 1, &trace_enable, HCI_INIT_TIMEOUT);
1401	if (IS_ERR(skb)) {
1402		bt_dev_err(hdev, "Stop tracing of link statistics events failed (%ld)",
1403			   PTR_ERR(skb));
1404		return PTR_ERR(skb);
1405	}
1406	kfree_skb(skb);
1407
1408	skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1409	if (IS_ERR(skb)) {
1410		bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1411			   PTR_ERR(skb));
1412		return PTR_ERR(skb);
1413	}
1414	kfree_skb(skb);
1415
1416	bt_dev_info(hdev, "reset debug features: trace_enable 0x%02x mask 0x%02x",
1417		    trace_enable, mask[3]);
1418
1419	return 0;
1420}
1421
1422int btintel_set_quality_report(struct hci_dev *hdev, bool enable)
1423{
1424	struct intel_debug_features features;
1425	int err;
1426
1427	bt_dev_dbg(hdev, "enable %d", enable);
1428
1429	/* Read the Intel supported features and if new exception formats
1430	 * supported, need to load the additional DDC config to enable.
1431	 */
1432	err = btintel_read_debug_features(hdev, &features);
1433	if (err)
1434		return err;
1435
1436	/* Set or reset the debug features. */
1437	if (enable)
1438		err = btintel_set_debug_features(hdev, &features);
1439	else
1440		err = btintel_reset_debug_features(hdev, &features);
1441
1442	return err;
1443}
1444EXPORT_SYMBOL_GPL(btintel_set_quality_report);
1445
1446static void btintel_coredump(struct hci_dev *hdev)
1447{
1448	struct sk_buff *skb;
1449
1450	skb = __hci_cmd_sync(hdev, 0xfc4e, 0, NULL, HCI_CMD_TIMEOUT);
1451	if (IS_ERR(skb)) {
1452		bt_dev_err(hdev, "Coredump failed (%ld)", PTR_ERR(skb));
1453		return;
1454	}
1455
1456	kfree_skb(skb);
1457}
1458
1459static void btintel_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
1460{
1461	char buf[80];
1462
1463	snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
1464		 coredump_info.hw_variant);
1465	skb_put_data(skb, buf, strlen(buf));
1466
1467	snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
1468		 coredump_info.fw_build_num);
1469	skb_put_data(skb, buf, strlen(buf));
1470
1471	snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info.driver_name);
1472	skb_put_data(skb, buf, strlen(buf));
1473
1474	snprintf(buf, sizeof(buf), "Vendor: Intel\n");
1475	skb_put_data(skb, buf, strlen(buf));
1476}
1477
1478static int btintel_register_devcoredump_support(struct hci_dev *hdev)
1479{
1480	struct intel_debug_features features;
1481	int err;
1482
1483	err = btintel_read_debug_features(hdev, &features);
1484	if (err) {
1485		bt_dev_info(hdev, "Error reading debug features");
1486		return err;
1487	}
1488
1489	if (!(features.page1[0] & 0x3f)) {
1490		bt_dev_dbg(hdev, "Telemetry exception format not supported");
1491		return -EOPNOTSUPP;
1492	}
1493
1494	hci_devcd_register(hdev, btintel_coredump, btintel_dmp_hdr, NULL);
1495
1496	return err;
1497}
1498
1499static const struct firmware *btintel_legacy_rom_get_fw(struct hci_dev *hdev,
1500					       struct intel_version *ver)
1501{
1502	const struct firmware *fw;
1503	char fwname[64];
1504	int ret;
1505
1506	snprintf(fwname, sizeof(fwname),
1507		 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1508		 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1509		 ver->fw_variant,  ver->fw_revision, ver->fw_build_num,
1510		 ver->fw_build_ww, ver->fw_build_yy);
1511
1512	ret = request_firmware(&fw, fwname, &hdev->dev);
1513	if (ret < 0) {
1514		if (ret == -EINVAL) {
1515			bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1516				   ret);
1517			return NULL;
1518		}
1519
1520		bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1521			   fwname, ret);
1522
1523		/* If the correct firmware patch file is not found, use the
1524		 * default firmware patch file instead
1525		 */
1526		snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1527			 ver->hw_platform, ver->hw_variant);
1528		if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1529			bt_dev_err(hdev, "failed to open default fw file: %s",
1530				   fwname);
1531			return NULL;
1532		}
1533	}
1534
1535	bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
1536
1537	return fw;
1538}
1539
1540static int btintel_legacy_rom_patching(struct hci_dev *hdev,
1541				      const struct firmware *fw,
1542				      const u8 **fw_ptr, int *disable_patch)
1543{
1544	struct sk_buff *skb;
1545	struct hci_command_hdr *cmd;
1546	const u8 *cmd_param;
1547	struct hci_event_hdr *evt = NULL;
1548	const u8 *evt_param = NULL;
1549	int remain = fw->size - (*fw_ptr - fw->data);
1550
1551	/* The first byte indicates the types of the patch command or event.
1552	 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1553	 * in the current firmware buffer doesn't start with 0x01 or
1554	 * the size of remain buffer is smaller than HCI command header,
1555	 * the firmware file is corrupted and it should stop the patching
1556	 * process.
1557	 */
1558	if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1559		bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
1560		return -EINVAL;
1561	}
1562	(*fw_ptr)++;
1563	remain--;
1564
1565	cmd = (struct hci_command_hdr *)(*fw_ptr);
1566	*fw_ptr += sizeof(*cmd);
1567	remain -= sizeof(*cmd);
1568
1569	/* Ensure that the remain firmware data is long enough than the length
1570	 * of command parameter. If not, the firmware file is corrupted.
1571	 */
1572	if (remain < cmd->plen) {
1573		bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
1574		return -EFAULT;
1575	}
1576
1577	/* If there is a command that loads a patch in the firmware
1578	 * file, then enable the patch upon success, otherwise just
1579	 * disable the manufacturer mode, for example patch activation
1580	 * is not required when the default firmware patch file is used
1581	 * because there are no patch data to load.
1582	 */
1583	if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1584		*disable_patch = 0;
1585
1586	cmd_param = *fw_ptr;
1587	*fw_ptr += cmd->plen;
1588	remain -= cmd->plen;
1589
1590	/* This reads the expected events when the above command is sent to the
1591	 * device. Some vendor commands expects more than one events, for
1592	 * example command status event followed by vendor specific event.
1593	 * For this case, it only keeps the last expected event. so the command
1594	 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1595	 * last expected event.
1596	 */
1597	while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1598		(*fw_ptr)++;
1599		remain--;
1600
1601		evt = (struct hci_event_hdr *)(*fw_ptr);
1602		*fw_ptr += sizeof(*evt);
1603		remain -= sizeof(*evt);
1604
1605		if (remain < evt->plen) {
1606			bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
1607			return -EFAULT;
1608		}
1609
1610		evt_param = *fw_ptr;
1611		*fw_ptr += evt->plen;
1612		remain -= evt->plen;
1613	}
1614
1615	/* Every HCI commands in the firmware file has its correspond event.
1616	 * If event is not found or remain is smaller than zero, the firmware
1617	 * file is corrupted.
1618	 */
1619	if (!evt || !evt_param || remain < 0) {
1620		bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
1621		return -EFAULT;
1622	}
1623
1624	skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1625				cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1626	if (IS_ERR(skb)) {
1627		bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1628			   cmd->opcode, PTR_ERR(skb));
1629		return PTR_ERR(skb);
1630	}
1631
1632	/* It ensures that the returned event matches the event data read from
1633	 * the firmware file. At fist, it checks the length and then
1634	 * the contents of the event.
1635	 */
1636	if (skb->len != evt->plen) {
1637		bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1638			   le16_to_cpu(cmd->opcode));
1639		kfree_skb(skb);
1640		return -EFAULT;
1641	}
1642
1643	if (memcmp(skb->data, evt_param, evt->plen)) {
1644		bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1645			   le16_to_cpu(cmd->opcode));
1646		kfree_skb(skb);
1647		return -EFAULT;
1648	}
1649	kfree_skb(skb);
1650
1651	return 0;
1652}
1653
1654static int btintel_legacy_rom_setup(struct hci_dev *hdev,
1655				    struct intel_version *ver)
1656{
1657	const struct firmware *fw;
1658	const u8 *fw_ptr;
1659	int disable_patch, err;
1660	struct intel_version new_ver;
1661
1662	BT_DBG("%s", hdev->name);
1663
1664	/* fw_patch_num indicates the version of patch the device currently
1665	 * have. If there is no patch data in the device, it is always 0x00.
1666	 * So, if it is other than 0x00, no need to patch the device again.
1667	 */
1668	if (ver->fw_patch_num) {
1669		bt_dev_info(hdev,
1670			    "Intel device is already patched. patch num: %02x",
1671			    ver->fw_patch_num);
1672		goto complete;
1673	}
1674
1675	/* Opens the firmware patch file based on the firmware version read
1676	 * from the controller. If it fails to open the matching firmware
1677	 * patch file, it tries to open the default firmware patch file.
1678	 * If no patch file is found, allow the device to operate without
1679	 * a patch.
1680	 */
1681	fw = btintel_legacy_rom_get_fw(hdev, ver);
1682	if (!fw)
1683		goto complete;
1684	fw_ptr = fw->data;
1685
1686	/* Enable the manufacturer mode of the controller.
1687	 * Only while this mode is enabled, the driver can download the
1688	 * firmware patch data and configuration parameters.
1689	 */
1690	err = btintel_enter_mfg(hdev);
1691	if (err) {
1692		release_firmware(fw);
1693		return err;
1694	}
1695
1696	disable_patch = 1;
1697
1698	/* The firmware data file consists of list of Intel specific HCI
1699	 * commands and its expected events. The first byte indicates the
1700	 * type of the message, either HCI command or HCI event.
1701	 *
1702	 * It reads the command and its expected event from the firmware file,
1703	 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1704	 * the returned event is compared with the event read from the firmware
1705	 * file and it will continue until all the messages are downloaded to
1706	 * the controller.
1707	 *
1708	 * Once the firmware patching is completed successfully,
1709	 * the manufacturer mode is disabled with reset and activating the
1710	 * downloaded patch.
1711	 *
1712	 * If the firmware patching fails, the manufacturer mode is
1713	 * disabled with reset and deactivating the patch.
1714	 *
1715	 * If the default patch file is used, no reset is done when disabling
1716	 * the manufacturer.
1717	 */
1718	while (fw->size > fw_ptr - fw->data) {
1719		int ret;
1720
1721		ret = btintel_legacy_rom_patching(hdev, fw, &fw_ptr,
1722						 &disable_patch);
1723		if (ret < 0)
1724			goto exit_mfg_deactivate;
1725	}
1726
1727	release_firmware(fw);
1728
1729	if (disable_patch)
1730		goto exit_mfg_disable;
1731
1732	/* Patching completed successfully and disable the manufacturer mode
1733	 * with reset and activate the downloaded firmware patches.
1734	 */
1735	err = btintel_exit_mfg(hdev, true, true);
1736	if (err)
1737		return err;
1738
1739	/* Need build number for downloaded fw patches in
1740	 * every power-on boot
1741	 */
1742	err = btintel_read_version(hdev, &new_ver);
1743	if (err)
1744		return err;
1745
1746	bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
1747		    new_ver.fw_patch_num);
1748
1749	goto complete;
1750
1751exit_mfg_disable:
1752	/* Disable the manufacturer mode without reset */
1753	err = btintel_exit_mfg(hdev, false, false);
1754	if (err)
1755		return err;
1756
1757	bt_dev_info(hdev, "Intel firmware patch completed");
1758
1759	goto complete;
1760
1761exit_mfg_deactivate:
1762	release_firmware(fw);
1763
1764	/* Patching failed. Disable the manufacturer mode with reset and
1765	 * deactivate the downloaded firmware patches.
1766	 */
1767	err = btintel_exit_mfg(hdev, true, false);
1768	if (err)
1769		return err;
1770
1771	bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
1772
1773complete:
1774	/* Set the event mask for Intel specific vendor events. This enables
1775	 * a few extra events that are useful during general operation.
1776	 */
1777	btintel_set_event_mask_mfg(hdev, false);
1778
1779	btintel_check_bdaddr(hdev);
1780
1781	return 0;
1782}
1783
1784static int btintel_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1785{
1786	ktime_t delta, rettime;
1787	unsigned long long duration;
1788	int err;
1789
1790	btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
1791
1792	bt_dev_info(hdev, "Waiting for firmware download to complete");
1793
1794	err = btintel_wait_on_flag_timeout(hdev, INTEL_DOWNLOADING,
1795					   TASK_INTERRUPTIBLE,
1796					   msecs_to_jiffies(msec));
1797	if (err == -EINTR) {
1798		bt_dev_err(hdev, "Firmware loading interrupted");
1799		return err;
1800	}
1801
1802	if (err) {
1803		bt_dev_err(hdev, "Firmware loading timeout");
1804		return -ETIMEDOUT;
1805	}
1806
1807	if (btintel_test_flag(hdev, INTEL_FIRMWARE_FAILED)) {
1808		bt_dev_err(hdev, "Firmware loading failed");
1809		return -ENOEXEC;
1810	}
1811
1812	rettime = ktime_get();
1813	delta = ktime_sub(rettime, calltime);
1814	duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1815
1816	bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
1817
1818	return 0;
1819}
1820
1821static int btintel_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
1822{
1823	ktime_t delta, rettime;
1824	unsigned long long duration;
1825	int err;
1826
1827	bt_dev_info(hdev, "Waiting for device to boot");
1828
1829	err = btintel_wait_on_flag_timeout(hdev, INTEL_BOOTING,
1830					   TASK_INTERRUPTIBLE,
1831					   msecs_to_jiffies(msec));
1832	if (err == -EINTR) {
1833		bt_dev_err(hdev, "Device boot interrupted");
1834		return -EINTR;
1835	}
1836
1837	if (err) {
1838		bt_dev_err(hdev, "Device boot timeout");
1839		return -ETIMEDOUT;
1840	}
1841
1842	rettime = ktime_get();
1843	delta = ktime_sub(rettime, calltime);
1844	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
1845
1846	bt_dev_info(hdev, "Device booted in %llu usecs", duration);
1847
1848	return 0;
1849}
1850
1851static int btintel_boot_wait_d0(struct hci_dev *hdev, ktime_t calltime,
1852				int msec)
1853{
1854	ktime_t delta, rettime;
1855	unsigned long long duration;
1856	int err;
1857
1858	bt_dev_info(hdev, "Waiting for device transition to d0");
1859
1860	err = btintel_wait_on_flag_timeout(hdev, INTEL_WAIT_FOR_D0,
1861					   TASK_INTERRUPTIBLE,
1862					   msecs_to_jiffies(msec));
1863	if (err == -EINTR) {
1864		bt_dev_err(hdev, "Device d0 move interrupted");
1865		return -EINTR;
1866	}
1867
1868	if (err) {
1869		bt_dev_err(hdev, "Device d0 move timeout");
1870		return -ETIMEDOUT;
1871	}
1872
1873	rettime = ktime_get();
1874	delta = ktime_sub(rettime, calltime);
1875	duration = (unsigned long long)ktime_to_ns(delta) >> 10;
1876
1877	bt_dev_info(hdev, "Device moved to D0 in %llu usecs", duration);
1878
1879	return 0;
1880}
1881
1882static int btintel_boot(struct hci_dev *hdev, u32 boot_addr)
1883{
1884	ktime_t calltime;
1885	int err;
1886
1887	calltime = ktime_get();
1888
1889	btintel_set_flag(hdev, INTEL_BOOTING);
1890	btintel_set_flag(hdev, INTEL_WAIT_FOR_D0);
1891
1892	err = btintel_send_intel_reset(hdev, boot_addr);
1893	if (err) {
1894		bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
1895		btintel_reset_to_bootloader(hdev);
1896		return err;
1897	}
1898
1899	/* The bootloader will not indicate when the device is ready. This
1900	 * is done by the operational firmware sending bootup notification.
1901	 *
1902	 * Booting into operational firmware should not take longer than
1903	 * 5 second. However if that happens, then just fail the setup
1904	 * since something went wrong.
1905	 */
1906	err = btintel_boot_wait(hdev, calltime, 5000);
1907	if (err == -ETIMEDOUT) {
1908		btintel_reset_to_bootloader(hdev);
1909		goto exit_error;
1910	}
1911
1912	if (hdev->bus == HCI_PCI) {
1913		/* In case of PCIe, after receiving bootup event, driver performs
1914		 * D0 entry by writing 0 to sleep control register (check
1915		 * btintel_pcie_recv_event())
1916		 * Firmware acks with alive interrupt indicating host is full ready to
1917		 * perform BT operation. Lets wait here till INTEL_WAIT_FOR_D0
1918		 * bit is cleared.
1919		 */
1920		calltime = ktime_get();
1921		err = btintel_boot_wait_d0(hdev, calltime, 2000);
1922	}
1923
1924exit_error:
1925	return err;
1926}
1927
1928static int btintel_get_fw_name(struct intel_version *ver,
1929					     struct intel_boot_params *params,
1930					     char *fw_name, size_t len,
1931					     const char *suffix)
1932{
1933	switch (ver->hw_variant) {
1934	case 0x0b:	/* SfP */
1935	case 0x0c:	/* WsP */
1936		snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
1937			 ver->hw_variant,
1938			 le16_to_cpu(params->dev_revid),
1939			 suffix);
1940		break;
1941	case 0x11:	/* JfP */
1942	case 0x12:	/* ThP */
1943	case 0x13:	/* HrP */
1944	case 0x14:	/* CcP */
1945		snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
1946			 ver->hw_variant,
1947			 ver->hw_revision,
1948			 ver->fw_revision,
1949			 suffix);
1950		break;
1951	default:
1952		return -EINVAL;
1953	}
1954
1955	return 0;
1956}
1957
1958static int btintel_download_fw(struct hci_dev *hdev,
1959					 struct intel_version *ver,
1960					 struct intel_boot_params *params,
1961					 u32 *boot_param)
1962{
1963	const struct firmware *fw;
1964	char fwname[64];
1965	int err;
1966	ktime_t calltime;
1967
1968	if (!ver || !params)
1969		return -EINVAL;
1970
1971	/* The firmware variant determines if the device is in bootloader
1972	 * mode or is running operational firmware. The value 0x06 identifies
1973	 * the bootloader and the value 0x23 identifies the operational
1974	 * firmware.
1975	 *
1976	 * When the operational firmware is already present, then only
1977	 * the check for valid Bluetooth device address is needed. This
1978	 * determines if the device will be added as configured or
1979	 * unconfigured controller.
1980	 *
1981	 * It is not possible to use the Secure Boot Parameters in this
1982	 * case since that command is only available in bootloader mode.
1983	 */
1984	if (ver->fw_variant == 0x23) {
1985		btintel_clear_flag(hdev, INTEL_BOOTLOADER);
1986		btintel_check_bdaddr(hdev);
1987
1988		/* SfP and WsP don't seem to update the firmware version on file
1989		 * so version checking is currently possible.
1990		 */
1991		switch (ver->hw_variant) {
1992		case 0x0b:	/* SfP */
1993		case 0x0c:	/* WsP */
1994			return 0;
1995		}
1996
1997		/* Proceed to download to check if the version matches */
1998		goto download;
1999	}
2000
2001	/* Read the secure boot parameters to identify the operating
2002	 * details of the bootloader.
2003	 */
2004	err = btintel_read_boot_params(hdev, params);
2005	if (err)
2006		return err;
2007
2008	/* It is required that every single firmware fragment is acknowledged
2009	 * with a command complete event. If the boot parameters indicate
2010	 * that this bootloader does not send them, then abort the setup.
2011	 */
2012	if (params->limited_cce != 0x00) {
2013		bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2014			   params->limited_cce);
2015		return -EINVAL;
2016	}
2017
2018	/* If the OTP has no valid Bluetooth device address, then there will
2019	 * also be no valid address for the operational firmware.
2020	 */
2021	if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2022		bt_dev_info(hdev, "No device address configured");
2023		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2024	}
2025
2026download:
2027	/* With this Intel bootloader only the hardware variant and device
2028	 * revision information are used to select the right firmware for SfP
2029	 * and WsP.
2030	 *
2031	 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2032	 *
2033	 * Currently the supported hardware variants are:
2034	 *   11 (0x0b) for iBT3.0 (LnP/SfP)
2035	 *   12 (0x0c) for iBT3.5 (WsP)
2036	 *
2037	 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2038	 * variant, HW revision and FW revision, as these are dependent on CNVi
2039	 * and RF Combination.
2040	 *
2041	 *   17 (0x11) for iBT3.5 (JfP)
2042	 *   18 (0x12) for iBT3.5 (ThP)
2043	 *
2044	 * The firmware file name for these will be
2045	 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2046	 *
2047	 */
2048	err = btintel_get_fw_name(ver, params, fwname, sizeof(fwname), "sfi");
2049	if (err < 0) {
2050		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2051			/* Firmware has already been loaded */
2052			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2053			return 0;
2054		}
2055
2056		bt_dev_err(hdev, "Unsupported Intel firmware naming");
2057		return -EINVAL;
2058	}
2059
2060	err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2061	if (err < 0) {
2062		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2063			/* Firmware has already been loaded */
2064			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2065			return 0;
2066		}
2067
2068		bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2069			   fwname, err);
2070		return err;
2071	}
2072
2073	bt_dev_info(hdev, "Found device firmware: %s", fwname);
2074
2075	if (fw->size < 644) {
2076		bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2077			   fw->size);
2078		err = -EBADF;
2079		goto done;
2080	}
2081
2082	calltime = ktime_get();
2083
2084	btintel_set_flag(hdev, INTEL_DOWNLOADING);
2085
2086	/* Start firmware downloading and get boot parameter */
2087	err = btintel_download_firmware(hdev, ver, fw, boot_param);
2088	if (err < 0) {
2089		if (err == -EALREADY) {
2090			/* Firmware has already been loaded */
2091			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2092			err = 0;
2093			goto done;
2094		}
2095
2096		/* When FW download fails, send Intel Reset to retry
2097		 * FW download.
2098		 */
2099		btintel_reset_to_bootloader(hdev);
2100		goto done;
2101	}
2102
2103	/* Before switching the device into operational mode and with that
2104	 * booting the loaded firmware, wait for the bootloader notification
2105	 * that all fragments have been successfully received.
2106	 *
2107	 * When the event processing receives the notification, then the
2108	 * INTEL_DOWNLOADING flag will be cleared.
2109	 *
2110	 * The firmware loading should not take longer than 5 seconds
2111	 * and thus just timeout if that happens and fail the setup
2112	 * of this device.
2113	 */
2114	err = btintel_download_wait(hdev, calltime, 5000);
2115	if (err == -ETIMEDOUT)
2116		btintel_reset_to_bootloader(hdev);
2117
2118done:
2119	release_firmware(fw);
2120	return err;
2121}
2122
2123static int btintel_bootloader_setup(struct hci_dev *hdev,
2124				    struct intel_version *ver)
2125{
2126	struct intel_version new_ver;
2127	struct intel_boot_params params;
2128	u32 boot_param;
2129	char ddcname[64];
2130	int err;
2131
2132	BT_DBG("%s", hdev->name);
2133
2134	/* Set the default boot parameter to 0x0 and it is updated to
2135	 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2136	 * command while downloading the firmware.
2137	 */
2138	boot_param = 0x00000000;
2139
2140	btintel_set_flag(hdev, INTEL_BOOTLOADER);
2141
2142	err = btintel_download_fw(hdev, ver, &params, &boot_param);
2143	if (err)
2144		return err;
2145
2146	/* controller is already having an operational firmware */
2147	if (ver->fw_variant == 0x23)
2148		goto finish;
2149
2150	err = btintel_boot(hdev, boot_param);
2151	if (err)
2152		return err;
2153
2154	btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2155
2156	err = btintel_get_fw_name(ver, &params, ddcname,
2157						sizeof(ddcname), "ddc");
2158
2159	if (err < 0) {
2160		bt_dev_err(hdev, "Unsupported Intel firmware naming");
2161	} else {
2162		/* Once the device is running in operational mode, it needs to
2163		 * apply the device configuration (DDC) parameters.
2164		 *
2165		 * The device can work without DDC parameters, so even if it
2166		 * fails to load the file, no need to fail the setup.
2167		 */
2168		btintel_load_ddc_config(hdev, ddcname);
2169	}
2170
2171	hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2172
2173	/* Read the Intel version information after loading the FW  */
2174	err = btintel_read_version(hdev, &new_ver);
2175	if (err)
2176		return err;
2177
2178	btintel_version_info(hdev, &new_ver);
2179
2180finish:
2181	/* Set the event mask for Intel specific vendor events. This enables
2182	 * a few extra events that are useful during general operation. It
2183	 * does not enable any debugging related events.
2184	 *
2185	 * The device will function correctly without these events enabled
2186	 * and thus no need to fail the setup.
2187	 */
2188	btintel_set_event_mask(hdev, false);
2189
2190	return 0;
2191}
2192
2193static void btintel_get_fw_name_tlv(const struct intel_version_tlv *ver,
2194				    char *fw_name, size_t len,
2195				    const char *suffix)
2196{
2197	const char *format;
2198	u32 cnvi, cnvr;
2199
2200	cnvi = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2201					INTEL_CNVX_TOP_STEP(ver->cnvi_top));
2202
2203	cnvr = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2204					INTEL_CNVX_TOP_STEP(ver->cnvr_top));
2205
2206	/* Only Blazar  product supports downloading of intermediate loader
2207	 * image
2208	 */
2209	if (INTEL_HW_VARIANT(ver->cnvi_bt) >= 0x1e) {
2210		u8 zero[BTINTEL_FWID_MAXLEN];
2211
2212		if (ver->img_type == BTINTEL_IMG_BOOTLOADER) {
2213			format = "intel/ibt-%04x-%04x-iml.%s";
2214			snprintf(fw_name, len, format, cnvi, cnvr, suffix);
2215			return;
2216		}
2217
2218		memset(zero, 0, sizeof(zero));
2219
2220		/* ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step-fw_id> */
2221		if (memcmp(ver->fw_id, zero, sizeof(zero))) {
2222			format = "intel/ibt-%04x-%04x-%s.%s";
2223			snprintf(fw_name, len, format, cnvi, cnvr,
2224				 ver->fw_id, suffix);
2225			return;
2226		}
2227		/* If firmware id is not present, fallback to legacy naming
2228		 * convention
2229		 */
2230	}
2231	/* Fallback to legacy naming convention for other controllers
2232	 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2233	 */
2234	format = "intel/ibt-%04x-%04x.%s";
2235	snprintf(fw_name, len, format, cnvi, cnvr, suffix);
2236}
2237
2238static void btintel_get_iml_tlv(const struct intel_version_tlv *ver,
2239				char *fw_name, size_t len,
2240				const char *suffix)
2241{
2242	const char *format;
2243	u32 cnvi, cnvr;
2244
2245	cnvi = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvi_top),
2246					INTEL_CNVX_TOP_STEP(ver->cnvi_top));
2247
2248	cnvr = INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver->cnvr_top),
2249					INTEL_CNVX_TOP_STEP(ver->cnvr_top));
2250
2251	format = "intel/ibt-%04x-%04x-iml.%s";
2252	snprintf(fw_name, len, format, cnvi, cnvr, suffix);
2253}
2254
2255static int btintel_prepare_fw_download_tlv(struct hci_dev *hdev,
2256					   struct intel_version_tlv *ver,
2257					   u32 *boot_param)
2258{
2259	const struct firmware *fw;
2260	char fwname[128];
2261	int err;
2262	ktime_t calltime;
2263
2264	if (!ver || !boot_param)
2265		return -EINVAL;
2266
2267	/* The firmware variant determines if the device is in bootloader
2268	 * mode or is running operational firmware. The value 0x03 identifies
2269	 * the bootloader and the value 0x23 identifies the operational
2270	 * firmware.
2271	 *
2272	 * When the operational firmware is already present, then only
2273	 * the check for valid Bluetooth device address is needed. This
2274	 * determines if the device will be added as configured or
2275	 * unconfigured controller.
2276	 *
2277	 * It is not possible to use the Secure Boot Parameters in this
2278	 * case since that command is only available in bootloader mode.
2279	 */
2280	if (ver->img_type == BTINTEL_IMG_OP) {
2281		btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2282		btintel_check_bdaddr(hdev);
2283	} else {
2284		/*
2285		 * Check for valid bd address in boot loader mode. Device
2286		 * will be marked as unconfigured if empty bd address is
2287		 * found.
2288		 */
2289		if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2290			bt_dev_info(hdev, "No device address configured");
2291			set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2292		}
2293	}
2294
2295	if (ver->img_type == BTINTEL_IMG_OP) {
2296		/* Controller running OP image. In case of FW downgrade,
2297		 * FWID TLV may not be present and driver may attempt to load
2298		 * firmware image which doesn't exist. Lets compare the version
2299		 * of IML image
2300		 */
2301		if (INTEL_HW_VARIANT(ver->cnvi_bt) >= 0x1e)
2302			btintel_get_iml_tlv(ver, fwname, sizeof(fwname), "sfi");
2303		else
2304			btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2305	} else {
2306		btintel_get_fw_name_tlv(ver, fwname, sizeof(fwname), "sfi");
2307	}
2308
2309	err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
2310	if (err < 0) {
2311		if (!btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2312			/* Firmware has already been loaded */
2313			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2314			return 0;
2315		}
2316
2317		bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2318			   fwname, err);
2319
2320		return err;
2321	}
2322
2323	bt_dev_info(hdev, "Found device firmware: %s", fwname);
2324
2325	if (fw->size < 644) {
2326		bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2327			   fw->size);
2328		err = -EBADF;
2329		goto done;
2330	}
2331
2332	calltime = ktime_get();
2333
2334	btintel_set_flag(hdev, INTEL_DOWNLOADING);
2335
2336	/* Start firmware downloading and get boot parameter */
2337	err = btintel_download_fw_tlv(hdev, ver, fw, boot_param,
2338					       INTEL_HW_VARIANT(ver->cnvi_bt),
2339					       ver->sbe_type);
2340	if (err < 0) {
2341		if (err == -EALREADY) {
2342			/* Firmware has already been loaded */
2343			btintel_set_flag(hdev, INTEL_FIRMWARE_LOADED);
2344			err = 0;
2345			goto done;
2346		}
2347
2348		/* When FW download fails, send Intel Reset to retry
2349		 * FW download.
2350		 */
2351		btintel_reset_to_bootloader(hdev);
2352		goto done;
2353	}
2354
2355	/* Before switching the device into operational mode and with that
2356	 * booting the loaded firmware, wait for the bootloader notification
2357	 * that all fragments have been successfully received.
2358	 *
2359	 * When the event processing receives the notification, then the
2360	 * BTUSB_DOWNLOADING flag will be cleared.
2361	 *
2362	 * The firmware loading should not take longer than 5 seconds
2363	 * and thus just timeout if that happens and fail the setup
2364	 * of this device.
2365	 */
2366	err = btintel_download_wait(hdev, calltime, 5000);
2367	if (err == -ETIMEDOUT)
2368		btintel_reset_to_bootloader(hdev);
2369
2370done:
2371	release_firmware(fw);
2372	return err;
2373}
2374
2375static int btintel_get_codec_config_data(struct hci_dev *hdev,
2376					 __u8 link, struct bt_codec *codec,
2377					 __u8 *ven_len, __u8 **ven_data)
2378{
2379	int err = 0;
2380
2381	if (!ven_data || !ven_len)
2382		return -EINVAL;
2383
2384	*ven_len = 0;
2385	*ven_data = NULL;
2386
2387	if (link != ESCO_LINK) {
2388		bt_dev_err(hdev, "Invalid link type(%u)", link);
2389		return -EINVAL;
2390	}
2391
2392	*ven_data = kmalloc(sizeof(__u8), GFP_KERNEL);
2393	if (!*ven_data) {
2394		err = -ENOMEM;
2395		goto error;
2396	}
2397
2398	/* supports only CVSD and mSBC offload codecs */
2399	switch (codec->id) {
2400	case 0x02:
2401		**ven_data = 0x00;
2402		break;
2403	case 0x05:
2404		**ven_data = 0x01;
2405		break;
2406	default:
2407		err = -EINVAL;
2408		bt_dev_err(hdev, "Invalid codec id(%u)", codec->id);
2409		goto error;
2410	}
2411	/* codec and its capabilities are pre-defined to ids
2412	 * preset id = 0x00 represents CVSD codec with sampling rate 8K
2413	 * preset id = 0x01 represents mSBC codec with sampling rate 16K
2414	 */
2415	*ven_len = sizeof(__u8);
2416	return err;
2417
2418error:
2419	kfree(*ven_data);
2420	*ven_data = NULL;
2421	return err;
2422}
2423
2424static int btintel_get_data_path_id(struct hci_dev *hdev, __u8 *data_path_id)
2425{
2426	/* Intel uses 1 as data path id for all the usecases */
2427	*data_path_id = 1;
2428	return 0;
2429}
2430
2431static int btintel_configure_offload(struct hci_dev *hdev)
2432{
2433	struct sk_buff *skb;
2434	int err = 0;
2435	struct intel_offload_use_cases *use_cases;
2436
2437	skb = __hci_cmd_sync(hdev, 0xfc86, 0, NULL, HCI_INIT_TIMEOUT);
2438	if (IS_ERR(skb)) {
2439		bt_dev_err(hdev, "Reading offload use cases failed (%ld)",
2440			   PTR_ERR(skb));
2441		return PTR_ERR(skb);
2442	}
2443
2444	if (skb->len < sizeof(*use_cases)) {
2445		err = -EIO;
2446		goto error;
2447	}
2448
2449	use_cases = (void *)skb->data;
2450
2451	if (use_cases->status) {
2452		err = -bt_to_errno(skb->data[0]);
2453		goto error;
2454	}
2455
2456	if (use_cases->preset[0] & 0x03) {
2457		hdev->get_data_path_id = btintel_get_data_path_id;
2458		hdev->get_codec_config_data = btintel_get_codec_config_data;
2459	}
2460error:
2461	kfree_skb(skb);
2462	return err;
2463}
2464
2465static void btintel_set_ppag(struct hci_dev *hdev, struct intel_version_tlv *ver)
2466{
 
2467	struct sk_buff *skb;
2468	struct hci_ppag_enable_cmd ppag_cmd;
2469	acpi_handle handle;
2470	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
2471	union acpi_object *p, *elements;
2472	u32 domain, mode;
2473	acpi_status status;
2474
2475	/* PPAG is not supported if CRF is HrP2, Jfp2, JfP1 */
2476	switch (ver->cnvr_top & 0xFFF) {
2477	case 0x504:     /* Hrp2 */
2478	case 0x202:     /* Jfp2 */
2479	case 0x201:     /* Jfp1 */
2480		bt_dev_dbg(hdev, "PPAG not supported for Intel CNVr (0x%3x)",
2481			   ver->cnvr_top & 0xFFF);
2482		return;
2483	}
2484
2485	handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2486	if (!handle) {
2487		bt_dev_info(hdev, "No support for BT device in ACPI firmware");
2488		return;
2489	}
2490
2491	status = acpi_evaluate_object(handle, "PPAG", NULL, &buffer);
2492	if (ACPI_FAILURE(status)) {
2493		if (status == AE_NOT_FOUND) {
 
 
 
 
 
 
2494			bt_dev_dbg(hdev, "PPAG-BT: ACPI entry not found");
2495			return;
2496		}
2497		bt_dev_warn(hdev, "PPAG-BT: ACPI Failure: %s", acpi_format_exception(status));
2498		return;
2499	}
2500
2501	p = buffer.pointer;
2502	if (p->type != ACPI_TYPE_PACKAGE || p->package.count != 2) {
2503		bt_dev_warn(hdev, "PPAG-BT: Invalid object type: %d or package count: %d",
2504			    p->type, p->package.count);
2505		kfree(buffer.pointer);
2506		return;
2507	}
2508
2509	elements = p->package.elements;
2510
2511	/* PPAG table is located at element[1] */
2512	p = &elements[1];
2513
2514	domain = (u32)p->package.elements[0].integer.value;
2515	mode = (u32)p->package.elements[1].integer.value;
2516	kfree(buffer.pointer);
2517
2518	if (domain != 0x12) {
2519		bt_dev_dbg(hdev, "PPAG-BT: Bluetooth domain is disabled in ACPI firmware");
2520		return;
2521	}
2522
2523	/* PPAG mode
2524	 * BIT 0 : 0 Disabled in EU
2525	 *         1 Enabled in EU
2526	 * BIT 1 : 0 Disabled in China
2527	 *         1 Enabled in China
2528	 */
2529	mode &= 0x03;
2530
2531	if (!mode) {
2532		bt_dev_dbg(hdev, "PPAG-BT: EU, China mode are disabled in BIOS");
2533		return;
2534	}
2535
2536	ppag_cmd.ppag_enable_flags = cpu_to_le32(mode);
2537
2538	skb = __hci_cmd_sync(hdev, INTEL_OP_PPAG_CMD, sizeof(ppag_cmd),
2539			     &ppag_cmd, HCI_CMD_TIMEOUT);
2540	if (IS_ERR(skb)) {
2541		bt_dev_warn(hdev, "Failed to send PPAG Enable (%ld)", PTR_ERR(skb));
2542		return;
2543	}
2544	bt_dev_info(hdev, "PPAG-BT: Enabled (Mode %d)", mode);
2545	kfree_skb(skb);
2546}
2547
2548static int btintel_acpi_reset_method(struct hci_dev *hdev)
2549{
2550	int ret = 0;
2551	acpi_status status;
2552	union acpi_object *p, *ref;
2553	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2554
2555	status = acpi_evaluate_object(ACPI_HANDLE(GET_HCIDEV_DEV(hdev)), "_PRR", NULL, &buffer);
2556	if (ACPI_FAILURE(status)) {
2557		bt_dev_err(hdev, "Failed to run _PRR method");
2558		ret = -ENODEV;
2559		return ret;
2560	}
2561	p = buffer.pointer;
2562
2563	if (p->package.count != 1 || p->type != ACPI_TYPE_PACKAGE) {
2564		bt_dev_err(hdev, "Invalid arguments");
2565		ret = -EINVAL;
2566		goto exit_on_error;
2567	}
2568
2569	ref = &p->package.elements[0];
2570	if (ref->type != ACPI_TYPE_LOCAL_REFERENCE) {
2571		bt_dev_err(hdev, "Invalid object type: 0x%x", ref->type);
2572		ret = -EINVAL;
2573		goto exit_on_error;
2574	}
2575
2576	status = acpi_evaluate_object(ref->reference.handle, "_RST", NULL, NULL);
2577	if (ACPI_FAILURE(status)) {
2578		bt_dev_err(hdev, "Failed to run_RST method");
2579		ret = -ENODEV;
2580		goto exit_on_error;
2581	}
2582
2583exit_on_error:
2584	kfree(buffer.pointer);
2585	return ret;
2586}
2587
2588static void btintel_set_dsm_reset_method(struct hci_dev *hdev,
2589					 struct intel_version_tlv *ver_tlv)
2590{
2591	struct btintel_data *data = hci_get_priv(hdev);
2592	acpi_handle handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2593	u8 reset_payload[4] = {0x01, 0x00, 0x01, 0x00};
2594	union acpi_object *obj, argv4;
2595	enum {
2596		RESET_TYPE_WDISABLE2,
2597		RESET_TYPE_VSEC
2598	};
2599
2600	handle = ACPI_HANDLE(GET_HCIDEV_DEV(hdev));
2601
2602	if (!handle) {
2603		bt_dev_dbg(hdev, "No support for bluetooth device in ACPI firmware");
2604		return;
2605	}
2606
2607	if (!acpi_has_method(handle, "_PRR")) {
2608		bt_dev_err(hdev, "No support for _PRR ACPI method");
2609		return;
2610	}
2611
2612	switch (ver_tlv->cnvi_top & 0xfff) {
2613	case 0x910: /* GalePeak2 */
2614		reset_payload[2] = RESET_TYPE_VSEC;
2615		break;
2616	default:
2617		/* WDISABLE2 is the default reset method */
2618		reset_payload[2] = RESET_TYPE_WDISABLE2;
2619
2620		if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2621				    BIT(DSM_SET_WDISABLE2_DELAY))) {
2622			bt_dev_err(hdev, "No dsm support to set reset delay");
2623			return;
2624		}
2625		argv4.integer.type = ACPI_TYPE_INTEGER;
2626		/* delay required to toggle BT power */
2627		argv4.integer.value = 160;
2628		obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2629					DSM_SET_WDISABLE2_DELAY, &argv4);
2630		if (!obj) {
2631			bt_dev_err(hdev, "Failed to call dsm to set reset delay");
2632			return;
2633		}
2634		ACPI_FREE(obj);
2635	}
2636
2637	bt_dev_info(hdev, "DSM reset method type: 0x%02x", reset_payload[2]);
2638
2639	if (!acpi_check_dsm(handle, &btintel_guid_dsm, 0,
2640			    DSM_SET_RESET_METHOD)) {
2641		bt_dev_warn(hdev, "No support for dsm to set reset method");
2642		return;
2643	}
2644	argv4.buffer.type = ACPI_TYPE_BUFFER;
2645	argv4.buffer.length = sizeof(reset_payload);
2646	argv4.buffer.pointer = reset_payload;
2647
2648	obj = acpi_evaluate_dsm(handle, &btintel_guid_dsm, 0,
2649				DSM_SET_RESET_METHOD, &argv4);
2650	if (!obj) {
2651		bt_dev_err(hdev, "Failed to call dsm to set reset method");
2652		return;
2653	}
2654	ACPI_FREE(obj);
2655	data->acpi_reset_method = btintel_acpi_reset_method;
2656}
2657
2658#define BTINTEL_ISODATA_HANDLE_BASE 0x900
2659
2660static u8 btintel_classify_pkt_type(struct hci_dev *hdev, struct sk_buff *skb)
2661{
2662	/*
2663	 * Distinguish ISO data packets form ACL data packets
2664	 * based on their connection handle value range.
2665	 */
2666	if (hci_skb_pkt_type(skb) == HCI_ACLDATA_PKT) {
2667		__u16 handle = __le16_to_cpu(hci_acl_hdr(skb)->handle);
2668
2669		if (hci_handle(handle) >= BTINTEL_ISODATA_HANDLE_BASE)
2670			return HCI_ISODATA_PKT;
2671	}
2672
2673	return hci_skb_pkt_type(skb);
2674}
2675
2676/*
2677 * UefiCnvCommonDSBR UEFI variable provides information from the OEM platforms
2678 * if they have replaced the BRI (Bluetooth Radio Interface) resistor to
2679 * overcome the potential STEP errors on their designs. Based on the
2680 * configauration, bluetooth firmware shall adjust the BRI response line drive
2681 * strength. The below structure represents DSBR data.
2682 * struct {
2683 *	u8 header;
2684 *	u32 dsbr;
2685 * } __packed;
2686 *
2687 * header - defines revision number of the structure
2688 * dsbr - defines drive strength BRI response
2689 *	bit0
2690 *		0 - instructs bluetooth firmware to use default values
2691 *		1 - instructs bluetooth firmware to override default values
2692 *	bit3:1
2693 *		Reserved
2694 *	bit7:4
2695 *		DSBR override values (only if bit0 is set. Default value is 0xF
2696 *	bit31:7
2697 *		Reserved
2698 * Expected values for dsbr field:
2699 *	1. 0xF1 - indicates that the resistor on board is 33 Ohm
2700 *	2. 0x00 or 0xB1 - indicates that the resistor on board is 10 Ohm
2701 *	3. Non existing UEFI variable or invalid (none of the above) - indicates
2702 *	   that the resistor on board is 10 Ohm
2703 * Even if uefi variable is not present, driver shall send 0xfc0a command to
2704 * firmware to use default values.
2705 *
2706 */
2707static int btintel_uefi_get_dsbr(u32 *dsbr_var)
2708{
2709	struct btintel_dsbr {
2710		u8 header;
2711		u32 dsbr;
2712	} __packed data;
2713
2714	efi_status_t status;
2715	unsigned long data_size = 0;
2716	efi_guid_t guid = EFI_GUID(0xe65d8884, 0xd4af, 0x4b20, 0x8d, 0x03,
2717				   0x77, 0x2e, 0xcc, 0x3d, 0xa5, 0x31);
2718
2719	if (!IS_ENABLED(CONFIG_EFI))
2720		return -EOPNOTSUPP;
2721
2722	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
2723		return -EOPNOTSUPP;
2724
2725	status = efi.get_variable(BTINTEL_EFI_DSBR, &guid, NULL, &data_size,
2726				  NULL);
2727
2728	if (status != EFI_BUFFER_TOO_SMALL || !data_size)
2729		return -EIO;
2730
2731	status = efi.get_variable(BTINTEL_EFI_DSBR, &guid, NULL, &data_size,
2732				  &data);
2733
2734	if (status != EFI_SUCCESS)
2735		return -ENXIO;
2736
2737	*dsbr_var = data.dsbr;
2738	return 0;
2739}
2740
2741static int btintel_set_dsbr(struct hci_dev *hdev, struct intel_version_tlv *ver)
2742{
2743	struct btintel_dsbr_cmd {
2744		u8 enable;
2745		u8 dsbr;
2746	} __packed;
2747
2748	struct btintel_dsbr_cmd cmd;
2749	struct sk_buff *skb;
2750	u32 dsbr, cnvi;
2751	u8 status;
2752	int err;
2753
2754	cnvi = ver->cnvi_top & 0xfff;
2755	/* DSBR command needs to be sent for,
2756	 * 1. BlazarI or BlazarIW + B0 step product in IML image.
2757	 * 2. Gale Peak2 or BlazarU in OP image.
2758	 */
2759
2760	switch (cnvi) {
2761	case BTINTEL_CNVI_BLAZARI:
2762	case BTINTEL_CNVI_BLAZARIW:
2763		if (ver->img_type == BTINTEL_IMG_IML &&
2764		    INTEL_CNVX_TOP_STEP(ver->cnvi_top) == 0x01)
2765			break;
2766		return 0;
2767	case BTINTEL_CNVI_GAP:
2768	case BTINTEL_CNVI_BLAZARU:
2769		if (ver->img_type == BTINTEL_IMG_OP &&
2770		    hdev->bus == HCI_USB)
2771			break;
2772		return 0;
2773	default:
2774		return 0;
2775	}
2776
2777	dsbr = 0;
2778	err = btintel_uefi_get_dsbr(&dsbr);
2779	if (err < 0)
2780		bt_dev_dbg(hdev, "Error reading efi: %ls  (%d)",
2781			   BTINTEL_EFI_DSBR, err);
2782
2783	cmd.enable = dsbr & BIT(0);
2784	cmd.dsbr = dsbr >> 4 & 0xF;
2785
2786	bt_dev_info(hdev, "dsbr: enable: 0x%2.2x value: 0x%2.2x", cmd.enable,
2787		    cmd.dsbr);
2788
2789	skb = __hci_cmd_sync(hdev, 0xfc0a, sizeof(cmd), &cmd,  HCI_CMD_TIMEOUT);
2790	if (IS_ERR(skb))
2791		return -bt_to_errno(PTR_ERR(skb));
2792
2793	status = skb->data[0];
2794	kfree_skb(skb);
2795
2796	if (status)
2797		return -bt_to_errno(status);
2798
2799	return 0;
2800}
2801
2802int btintel_bootloader_setup_tlv(struct hci_dev *hdev,
2803				 struct intel_version_tlv *ver)
2804{
2805	u32 boot_param;
2806	char ddcname[64];
2807	int err;
2808	struct intel_version_tlv new_ver;
2809
2810	bt_dev_dbg(hdev, "");
2811
2812	/* Set the default boot parameter to 0x0 and it is updated to
2813	 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2814	 * command while downloading the firmware.
2815	 */
2816	boot_param = 0x00000000;
2817
2818	/* In case of PCIe, this function might get called multiple times with
2819	 * same hdev instance if there is any error on firmware download.
2820	 * Need to clear stale bits of previous firmware download attempt.
2821	 */
2822	for (int i = 0; i < __INTEL_NUM_FLAGS; i++)
2823		btintel_clear_flag(hdev, i);
2824
2825	btintel_set_flag(hdev, INTEL_BOOTLOADER);
2826
2827	err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2828	if (err)
2829		return err;
2830
2831	/* check if controller is already having an operational firmware */
2832	if (ver->img_type == BTINTEL_IMG_OP)
2833		goto finish;
2834
2835	err = btintel_boot(hdev, boot_param);
2836	if (err)
2837		return err;
2838
2839	err = btintel_read_version_tlv(hdev, ver);
2840	if (err)
2841		return err;
2842
2843	/* set drive strength of BRI response */
2844	err = btintel_set_dsbr(hdev, ver);
2845	if (err) {
2846		bt_dev_err(hdev, "Failed to send dsbr command (%d)", err);
2847		return err;
2848	}
2849
2850	/* If image type returned is BTINTEL_IMG_IML, then controller supports
2851	 * intermediate loader image
2852	 */
2853	if (ver->img_type == BTINTEL_IMG_IML) {
2854		err = btintel_prepare_fw_download_tlv(hdev, ver, &boot_param);
2855		if (err)
2856			return err;
2857
2858		err = btintel_boot(hdev, boot_param);
2859		if (err)
2860			return err;
2861	}
2862
2863	btintel_clear_flag(hdev, INTEL_BOOTLOADER);
2864
2865	btintel_get_fw_name_tlv(ver, ddcname, sizeof(ddcname), "ddc");
2866	/* Once the device is running in operational mode, it needs to
2867	 * apply the device configuration (DDC) parameters.
2868	 *
2869	 * The device can work without DDC parameters, so even if it
2870	 * fails to load the file, no need to fail the setup.
2871	 */
2872	btintel_load_ddc_config(hdev, ddcname);
2873
2874	/* Read supported use cases and set callbacks to fetch datapath id */
2875	btintel_configure_offload(hdev);
2876
2877	hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
2878
2879	/* Set PPAG feature */
2880	btintel_set_ppag(hdev, ver);
2881
2882	/* Read the Intel version information after loading the FW  */
2883	err = btintel_read_version_tlv(hdev, &new_ver);
2884	if (err)
2885		return err;
2886
2887	btintel_version_info_tlv(hdev, &new_ver);
2888
2889finish:
2890	/* Set the event mask for Intel specific vendor events. This enables
2891	 * a few extra events that are useful during general operation. It
2892	 * does not enable any debugging related events.
2893	 *
2894	 * The device will function correctly without these events enabled
2895	 * and thus no need to fail the setup.
2896	 */
2897	btintel_set_event_mask(hdev, false);
2898
2899	return 0;
2900}
2901EXPORT_SYMBOL_GPL(btintel_bootloader_setup_tlv);
2902
2903void btintel_set_msft_opcode(struct hci_dev *hdev, u8 hw_variant)
2904{
2905	switch (hw_variant) {
2906	/* Legacy bootloader devices that supports MSFT Extension */
2907	case 0x11:	/* JfP */
2908	case 0x12:	/* ThP */
2909	case 0x13:	/* HrP */
2910	case 0x14:	/* CcP */
2911	/* All Intel new generation controllers support the Microsoft vendor
2912	 * extension are using 0xFC1E for VsMsftOpCode.
2913	 */
2914	case 0x17:
2915	case 0x18:
2916	case 0x19:
2917	case 0x1b:
2918	case 0x1c:
2919	case 0x1d:
2920	case 0x1e:
2921		hci_set_msft_opcode(hdev, 0xFC1E);
2922		break;
2923	default:
2924		/* Not supported */
2925		break;
2926	}
2927}
2928EXPORT_SYMBOL_GPL(btintel_set_msft_opcode);
2929
2930void btintel_print_fseq_info(struct hci_dev *hdev)
2931{
2932	struct sk_buff *skb;
2933	u8 *p;
2934	u32 val;
2935	const char *str;
2936
2937	skb = __hci_cmd_sync(hdev, 0xfcb3, 0, NULL, HCI_CMD_TIMEOUT);
2938	if (IS_ERR(skb)) {
2939		bt_dev_dbg(hdev, "Reading fseq status command failed (%ld)",
2940			   PTR_ERR(skb));
2941		return;
2942	}
2943
2944	if (skb->len < (sizeof(u32) * 16 + 2)) {
2945		bt_dev_dbg(hdev, "Malformed packet of length %u received",
2946			   skb->len);
2947		kfree_skb(skb);
2948		return;
2949	}
2950
2951	p = skb_pull_data(skb, 1);
2952	if (*p) {
2953		bt_dev_dbg(hdev, "Failed to get fseq status (0x%2.2x)", *p);
2954		kfree_skb(skb);
2955		return;
2956	}
2957
2958	p = skb_pull_data(skb, 1);
2959	switch (*p) {
2960	case 0:
2961		str = "Success";
2962		break;
2963	case 1:
2964		str = "Fatal error";
2965		break;
2966	case 2:
2967		str = "Semaphore acquire error";
2968		break;
2969	default:
2970		str = "Unknown error";
2971		break;
2972	}
2973
2974	if (*p) {
2975		bt_dev_err(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
2976		kfree_skb(skb);
2977		return;
2978	}
2979
2980	bt_dev_info(hdev, "Fseq status: %s (0x%2.2x)", str, *p);
2981
2982	val = get_unaligned_le32(skb_pull_data(skb, 4));
2983	bt_dev_dbg(hdev, "Reason: 0x%8.8x", val);
2984
2985	val = get_unaligned_le32(skb_pull_data(skb, 4));
2986	bt_dev_dbg(hdev, "Global version: 0x%8.8x", val);
2987
2988	val = get_unaligned_le32(skb_pull_data(skb, 4));
2989	bt_dev_dbg(hdev, "Installed version: 0x%8.8x", val);
2990
2991	p = skb->data;
2992	skb_pull_data(skb, 4);
2993	bt_dev_info(hdev, "Fseq executed: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
2994		    p[2], p[3]);
2995
2996	p = skb->data;
2997	skb_pull_data(skb, 4);
2998	bt_dev_info(hdev, "Fseq BT Top: %2.2u.%2.2u.%2.2u.%2.2u", p[0], p[1],
2999		    p[2], p[3]);
3000
3001	val = get_unaligned_le32(skb_pull_data(skb, 4));
3002	bt_dev_dbg(hdev, "Fseq Top init version: 0x%8.8x", val);
3003
3004	val = get_unaligned_le32(skb_pull_data(skb, 4));
3005	bt_dev_dbg(hdev, "Fseq Cnvio init version: 0x%8.8x", val);
3006
3007	val = get_unaligned_le32(skb_pull_data(skb, 4));
3008	bt_dev_dbg(hdev, "Fseq MBX Wifi file version: 0x%8.8x", val);
3009
3010	val = get_unaligned_le32(skb_pull_data(skb, 4));
3011	bt_dev_dbg(hdev, "Fseq BT version: 0x%8.8x", val);
3012
3013	val = get_unaligned_le32(skb_pull_data(skb, 4));
3014	bt_dev_dbg(hdev, "Fseq Top reset address: 0x%8.8x", val);
3015
3016	val = get_unaligned_le32(skb_pull_data(skb, 4));
3017	bt_dev_dbg(hdev, "Fseq MBX timeout: 0x%8.8x", val);
3018
3019	val = get_unaligned_le32(skb_pull_data(skb, 4));
3020	bt_dev_dbg(hdev, "Fseq MBX ack: 0x%8.8x", val);
3021
3022	val = get_unaligned_le32(skb_pull_data(skb, 4));
3023	bt_dev_dbg(hdev, "Fseq CNVi id: 0x%8.8x", val);
3024
3025	val = get_unaligned_le32(skb_pull_data(skb, 4));
3026	bt_dev_dbg(hdev, "Fseq CNVr id: 0x%8.8x", val);
3027
3028	val = get_unaligned_le32(skb_pull_data(skb, 4));
3029	bt_dev_dbg(hdev, "Fseq Error handle: 0x%8.8x", val);
3030
3031	val = get_unaligned_le32(skb_pull_data(skb, 4));
3032	bt_dev_dbg(hdev, "Fseq Magic noalive indication: 0x%8.8x", val);
3033
3034	val = get_unaligned_le32(skb_pull_data(skb, 4));
3035	bt_dev_dbg(hdev, "Fseq OTP version: 0x%8.8x", val);
3036
3037	val = get_unaligned_le32(skb_pull_data(skb, 4));
3038	bt_dev_dbg(hdev, "Fseq MBX otp version: 0x%8.8x", val);
3039
3040	kfree_skb(skb);
3041}
3042EXPORT_SYMBOL_GPL(btintel_print_fseq_info);
3043
3044static int btintel_setup_combined(struct hci_dev *hdev)
3045{
3046	const u8 param[1] = { 0xFF };
3047	struct intel_version ver;
3048	struct intel_version_tlv ver_tlv;
3049	struct sk_buff *skb;
3050	int err;
3051
3052	BT_DBG("%s", hdev->name);
3053
3054	/* The some controllers have a bug with the first HCI command sent to it
3055	 * returning number of completed commands as zero. This would stall the
3056	 * command processing in the Bluetooth core.
3057	 *
3058	 * As a workaround, send HCI Reset command first which will reset the
3059	 * number of completed commands and allow normal command processing
3060	 * from now on.
3061	 *
3062	 * Regarding the INTEL_BROKEN_SHUTDOWN_LED flag, these devices maybe
3063	 * in the SW_RFKILL ON state as a workaround of fixing LED issue during
3064	 * the shutdown() procedure, and once the device is in SW_RFKILL ON
3065	 * state, the only way to exit out of it is sending the HCI_Reset
3066	 * command.
3067	 */
3068	if (btintel_test_flag(hdev, INTEL_BROKEN_INITIAL_NCMD) ||
3069	    btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
3070		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
3071				     HCI_INIT_TIMEOUT);
3072		if (IS_ERR(skb)) {
3073			bt_dev_err(hdev,
3074				   "sending initial HCI reset failed (%ld)",
3075				   PTR_ERR(skb));
3076			return PTR_ERR(skb);
3077		}
3078		kfree_skb(skb);
3079	}
3080
3081	/* Starting from TyP device, the command parameter and response are
3082	 * changed even though the OCF for HCI_Intel_Read_Version command
3083	 * remains same. The legacy devices can handle even if the
3084	 * command has a parameter and returns a correct version information.
3085	 * So, it uses new format to support both legacy and new format.
3086	 */
3087	skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
3088	if (IS_ERR(skb)) {
3089		bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
3090			   PTR_ERR(skb));
3091		return PTR_ERR(skb);
3092	}
3093
3094	/* Check the status */
3095	if (skb->data[0]) {
3096		bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
3097			   skb->data[0]);
3098		err = -EIO;
3099		goto exit_error;
3100	}
3101
3102	/* Apply the common HCI quirks for Intel device */
3103	set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3104	set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3105	set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3106
3107	/* Set up the quality report callback for Intel devices */
3108	hdev->set_quality_report = btintel_set_quality_report;
3109
3110	/* For Legacy device, check the HW platform value and size */
3111	if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
3112		bt_dev_dbg(hdev, "Read the legacy Intel version information");
3113
3114		memcpy(&ver, skb->data, sizeof(ver));
3115
3116		/* Display version information */
3117		btintel_version_info(hdev, &ver);
3118
3119		/* Check for supported iBT hardware variants of this firmware
3120		 * loading method.
3121		 *
3122		 * This check has been put in place to ensure correct forward
3123		 * compatibility options when newer hardware variants come
3124		 * along.
3125		 */
3126		switch (ver.hw_variant) {
3127		case 0x07:	/* WP */
3128		case 0x08:	/* StP */
3129			/* Legacy ROM product */
3130			btintel_set_flag(hdev, INTEL_ROM_LEGACY);
3131
3132			/* Apply the device specific HCI quirks
3133			 *
3134			 * WBS for SdP - For the Legacy ROM products, only SdP
3135			 * supports the WBS. But the version information is not
3136			 * enough to use here because the StP2 and SdP have same
3137			 * hw_variant and fw_variant. So, this flag is set by
3138			 * the transport driver (btusb) based on the HW info
3139			 * (idProduct)
3140			 */
3141			if (!btintel_test_flag(hdev,
3142					       INTEL_ROM_LEGACY_NO_WBS_SUPPORT))
3143				set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
3144					&hdev->quirks);
 
 
 
3145
3146			err = btintel_legacy_rom_setup(hdev, &ver);
3147			break;
3148		case 0x0b:      /* SfP */
3149		case 0x11:      /* JfP */
3150		case 0x12:      /* ThP */
3151		case 0x13:      /* HrP */
3152		case 0x14:      /* CcP */
 
3153			fallthrough;
3154		case 0x0c:	/* WsP */
3155			/* Apply the device specific HCI quirks
3156			 *
3157			 * All Legacy bootloader devices support WBS
3158			 */
3159			set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
3160				&hdev->quirks);
3161
3162			/* These variants don't seem to support LE Coded PHY */
3163			set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
3164
3165			/* Setup MSFT Extension support */
3166			btintel_set_msft_opcode(hdev, ver.hw_variant);
3167
3168			err = btintel_bootloader_setup(hdev, &ver);
3169			btintel_register_devcoredump_support(hdev);
3170			break;
3171		default:
3172			bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
3173				   ver.hw_variant);
3174			err = -EINVAL;
3175		}
3176
3177		hci_set_hw_info(hdev,
3178				"INTEL platform=%u variant=%u revision=%u",
3179				ver.hw_platform, ver.hw_variant,
3180				ver.hw_revision);
3181
3182		goto exit_error;
3183	}
3184
3185	/* memset ver_tlv to start with clean state as few fields are exclusive
3186	 * to bootloader mode and are not populated in operational mode
3187	 */
3188	memset(&ver_tlv, 0, sizeof(ver_tlv));
3189	/* For TLV type device, parse the tlv data */
3190	err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
3191	if (err) {
3192		bt_dev_err(hdev, "Failed to parse TLV version information");
3193		goto exit_error;
3194	}
3195
3196	if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
3197		bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
3198			   INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
3199		err = -EINVAL;
3200		goto exit_error;
3201	}
3202
3203	/* Check for supported iBT hardware variants of this firmware
3204	 * loading method.
3205	 *
3206	 * This check has been put in place to ensure correct forward
3207	 * compatibility options when newer hardware variants come
3208	 * along.
3209	 */
3210	switch (INTEL_HW_VARIANT(ver_tlv.cnvi_bt)) {
3211	case 0x11:      /* JfP */
3212	case 0x12:      /* ThP */
3213	case 0x13:      /* HrP */
3214	case 0x14:      /* CcP */
3215		/* Some legacy bootloader devices starting from JfP,
3216		 * the operational firmware supports both old and TLV based
3217		 * HCI_Intel_Read_Version command based on the command
3218		 * parameter.
3219		 *
3220		 * For upgrading firmware case, the TLV based version cannot
3221		 * be used because the firmware filename for legacy bootloader
3222		 * is based on the old format.
3223		 *
3224		 * Also, it is not easy to convert TLV based version from the
3225		 * legacy version format.
3226		 *
3227		 * So, as a workaround for those devices, use the legacy
3228		 * HCI_Intel_Read_Version to get the version information and
3229		 * run the legacy bootloader setup.
3230		 */
3231		err = btintel_read_version(hdev, &ver);
3232		if (err)
3233			break;
3234
3235		/* Apply the device specific HCI quirks
3236		 *
3237		 * All Legacy bootloader devices support WBS
3238		 */
3239		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3240
3241		/* These variants don't seem to support LE Coded PHY */
3242		set_bit(HCI_QUIRK_BROKEN_LE_CODED, &hdev->quirks);
3243
 
 
 
3244		/* Setup MSFT Extension support */
3245		btintel_set_msft_opcode(hdev, ver.hw_variant);
3246
3247		err = btintel_bootloader_setup(hdev, &ver);
3248		btintel_register_devcoredump_support(hdev);
3249		break;
3250	case 0x18: /* GfP2 */
3251	case 0x1c: /* GaP */
3252		/* Re-classify packet type for controllers with LE audio */
3253		hdev->classify_pkt_type = btintel_classify_pkt_type;
3254		fallthrough;
3255	case 0x17:
 
3256	case 0x19:
3257	case 0x1b:
3258	case 0x1d:
3259	case 0x1e:
3260		/* Display version information of TLV type */
3261		btintel_version_info_tlv(hdev, &ver_tlv);
3262
3263		/* Apply the device specific HCI quirks for TLV based devices
3264		 *
3265		 * All TLV based devices support WBS
3266		 */
3267		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
3268
 
 
 
3269		/* Setup MSFT Extension support */
3270		btintel_set_msft_opcode(hdev,
3271					INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3272		btintel_set_dsm_reset_method(hdev, &ver_tlv);
3273
3274		err = btintel_bootloader_setup_tlv(hdev, &ver_tlv);
3275		if (err)
3276			goto exit_error;
3277
3278		btintel_register_devcoredump_support(hdev);
3279		btintel_print_fseq_info(hdev);
3280		break;
3281	default:
3282		bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
3283			   INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3284		err = -EINVAL;
3285		break;
3286	}
3287
3288	hci_set_hw_info(hdev, "INTEL platform=%u variant=%u",
3289			INTEL_HW_PLATFORM(ver_tlv.cnvi_bt),
3290			INTEL_HW_VARIANT(ver_tlv.cnvi_bt));
3291
3292exit_error:
3293	kfree_skb(skb);
3294
3295	return err;
3296}
3297
3298int btintel_shutdown_combined(struct hci_dev *hdev)
3299{
3300	struct sk_buff *skb;
3301	int ret;
3302
3303	/* Send HCI Reset to the controller to stop any BT activity which
3304	 * were triggered. This will help to save power and maintain the
3305	 * sync b/w Host and controller
3306	 */
3307	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3308	if (IS_ERR(skb)) {
3309		bt_dev_err(hdev, "HCI reset during shutdown failed");
3310		return PTR_ERR(skb);
3311	}
3312	kfree_skb(skb);
3313
3314
3315	/* Some platforms have an issue with BT LED when the interface is
3316	 * down or BT radio is turned off, which takes 5 seconds to BT LED
3317	 * goes off. As a workaround, sends HCI_Intel_SW_RFKILL to put the
3318	 * device in the RFKILL ON state which turns off the BT LED immediately.
3319	 */
3320	if (btintel_test_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED)) {
3321		skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3322		if (IS_ERR(skb)) {
3323			ret = PTR_ERR(skb);
3324			bt_dev_err(hdev, "turning off Intel device LED failed");
3325			return ret;
3326		}
3327		kfree_skb(skb);
3328	}
3329
3330	return 0;
3331}
3332EXPORT_SYMBOL_GPL(btintel_shutdown_combined);
3333
3334int btintel_configure_setup(struct hci_dev *hdev, const char *driver_name)
3335{
3336	hdev->manufacturer = 2;
3337	hdev->setup = btintel_setup_combined;
3338	hdev->shutdown = btintel_shutdown_combined;
3339	hdev->hw_error = btintel_hw_error;
3340	hdev->set_diag = btintel_set_diag_combined;
3341	hdev->set_bdaddr = btintel_set_bdaddr;
3342
3343	coredump_info.driver_name = driver_name;
3344
3345	return 0;
3346}
3347EXPORT_SYMBOL_GPL(btintel_configure_setup);
3348
3349int btintel_diagnostics(struct hci_dev *hdev, struct sk_buff *skb)
3350{
3351	struct intel_tlv *tlv = (void *)&skb->data[5];
3352
3353	/* The first event is always an event type TLV */
3354	if (tlv->type != INTEL_TLV_TYPE_ID)
3355		goto recv_frame;
3356
3357	switch (tlv->val[0]) {
3358	case INTEL_TLV_SYSTEM_EXCEPTION:
3359	case INTEL_TLV_FATAL_EXCEPTION:
3360	case INTEL_TLV_DEBUG_EXCEPTION:
3361	case INTEL_TLV_TEST_EXCEPTION:
3362		/* Generate devcoredump from exception */
3363		if (!hci_devcd_init(hdev, skb->len)) {
3364			hci_devcd_append(hdev, skb_clone(skb, GFP_ATOMIC));
3365			hci_devcd_complete(hdev);
3366		} else {
3367			bt_dev_err(hdev, "Failed to generate devcoredump");
 
3368		}
3369	break;
3370	default:
3371		bt_dev_err(hdev, "Invalid exception type %02X", tlv->val[0]);
3372	}
3373
3374recv_frame:
3375	return hci_recv_frame(hdev, skb);
3376}
3377EXPORT_SYMBOL_GPL(btintel_diagnostics);
3378
3379int btintel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
3380{
3381	struct hci_event_hdr *hdr = (void *)skb->data;
3382	const char diagnostics_hdr[] = { 0x87, 0x80, 0x03 };
3383
3384	if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
3385	    hdr->plen > 0) {
3386		const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
3387		unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
3388
3389		if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
3390			switch (skb->data[2]) {
3391			case 0x02:
3392				/* When switching to the operational firmware
3393				 * the device sends a vendor specific event
3394				 * indicating that the bootup completed.
3395				 */
3396				btintel_bootup(hdev, ptr, len);
3397				kfree_skb(skb);
3398				return 0;
3399			case 0x06:
3400				/* When the firmware loading completes the
3401				 * device sends out a vendor specific event
3402				 * indicating the result of the firmware
3403				 * loading.
3404				 */
3405				btintel_secure_send_result(hdev, ptr, len);
3406				kfree_skb(skb);
3407				return 0;
3408			}
3409		}
3410
3411		/* Handle all diagnostics events separately. May still call
3412		 * hci_recv_frame.
3413		 */
3414		if (len >= sizeof(diagnostics_hdr) &&
3415		    memcmp(&skb->data[2], diagnostics_hdr,
3416			   sizeof(diagnostics_hdr)) == 0) {
3417			return btintel_diagnostics(hdev, skb);
3418		}
3419	}
3420
3421	return hci_recv_frame(hdev, skb);
3422}
3423EXPORT_SYMBOL_GPL(btintel_recv_event);
3424
3425void btintel_bootup(struct hci_dev *hdev, const void *ptr, unsigned int len)
3426{
3427	const struct intel_bootup *evt = ptr;
3428
3429	if (len != sizeof(*evt))
3430		return;
3431
3432	if (btintel_test_and_clear_flag(hdev, INTEL_BOOTING))
3433		btintel_wake_up_flag(hdev, INTEL_BOOTING);
3434}
3435EXPORT_SYMBOL_GPL(btintel_bootup);
3436
3437void btintel_secure_send_result(struct hci_dev *hdev,
3438				const void *ptr, unsigned int len)
3439{
3440	const struct intel_secure_send_result *evt = ptr;
3441
3442	if (len != sizeof(*evt))
3443		return;
3444
3445	if (evt->result)
3446		btintel_set_flag(hdev, INTEL_FIRMWARE_FAILED);
3447
3448	if (btintel_test_and_clear_flag(hdev, INTEL_DOWNLOADING) &&
3449	    btintel_test_flag(hdev, INTEL_FIRMWARE_LOADED))
3450		btintel_wake_up_flag(hdev, INTEL_DOWNLOADING);
3451}
3452EXPORT_SYMBOL_GPL(btintel_secure_send_result);
3453
3454MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3455MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
3456MODULE_VERSION(VERSION);
3457MODULE_LICENSE("GPL");
3458MODULE_FIRMWARE("intel/ibt-11-5.sfi");
3459MODULE_FIRMWARE("intel/ibt-11-5.ddc");
3460MODULE_FIRMWARE("intel/ibt-12-16.sfi");
3461MODULE_FIRMWARE("intel/ibt-12-16.ddc");