Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Bluetooth support for Realtek devices
   4 *
   5 *  Copyright (C) 2015 Endless Mobile, Inc.
   6 */
   7
   8#include <linux/module.h>
   9#include <linux/firmware.h>
  10#include <linux/unaligned.h>
  11#include <linux/usb.h>
  12
  13#include <net/bluetooth/bluetooth.h>
  14#include <net/bluetooth/hci_core.h>
  15
  16#include "btrtl.h"
  17
  18#define VERSION "0.1"
  19
  20#define RTL_CHIP_8723CS_CG	3
  21#define RTL_CHIP_8723CS_VF	4
  22#define RTL_CHIP_8723CS_XX	5
  23#define RTL_EPATCH_SIGNATURE	"Realtech"
  24#define RTL_EPATCH_SIGNATURE_V2	"RTBTCore"
  25#define RTL_ROM_LMP_8703B	0x8703
  26#define RTL_ROM_LMP_8723A	0x1200
  27#define RTL_ROM_LMP_8723B	0x8723
  28#define RTL_ROM_LMP_8821A	0x8821
  29#define RTL_ROM_LMP_8761A	0x8761
  30#define RTL_ROM_LMP_8822B	0x8822
  31#define RTL_ROM_LMP_8852A	0x8852
  32#define RTL_ROM_LMP_8851B	0x8851
  33#define RTL_ROM_LMP_8922A	0x8922
  34#define RTL_CONFIG_MAGIC	0x8723ab55
  35
  36#define RTL_VSC_OP_COREDUMP	0xfcff
  37
  38#define IC_MATCH_FL_LMPSUBV	(1 << 0)
  39#define IC_MATCH_FL_HCIREV	(1 << 1)
  40#define IC_MATCH_FL_HCIVER	(1 << 2)
  41#define IC_MATCH_FL_HCIBUS	(1 << 3)
  42#define IC_MATCH_FL_CHIP_TYPE	(1 << 4)
  43#define IC_INFO(lmps, hcir, hciv, bus) \
  44	.match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | \
  45		       IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS, \
  46	.lmp_subver = (lmps), \
  47	.hci_rev = (hcir), \
  48	.hci_ver = (hciv), \
  49	.hci_bus = (bus)
  50
  51#define	RTL_CHIP_SUBVER (&(struct rtl_vendor_cmd) {{0x10, 0x38, 0x04, 0x28, 0x80}})
  52#define	RTL_CHIP_REV    (&(struct rtl_vendor_cmd) {{0x10, 0x3A, 0x04, 0x28, 0x80}})
  53#define	RTL_SEC_PROJ    (&(struct rtl_vendor_cmd) {{0x10, 0xA4, 0x0D, 0x00, 0xb0}})
  54
  55#define RTL_PATCH_SNIPPETS		0x01
  56#define RTL_PATCH_DUMMY_HEADER		0x02
  57#define RTL_PATCH_SECURITY_HEADER	0x03
  58
  59enum btrtl_chip_id {
  60	CHIP_ID_8723A,
  61	CHIP_ID_8723B,
  62	CHIP_ID_8821A,
  63	CHIP_ID_8761A,
  64	CHIP_ID_8822B = 8,
  65	CHIP_ID_8723D,
  66	CHIP_ID_8821C,
  67	CHIP_ID_8822C = 13,
  68	CHIP_ID_8761B,
  69	CHIP_ID_8852A = 18,
  70	CHIP_ID_8852B = 20,
  71	CHIP_ID_8852C = 25,
  72	CHIP_ID_8851B = 36,
  73	CHIP_ID_8922A = 44,
  74	CHIP_ID_8852BT = 47,
  75};
  76
  77struct id_table {
  78	__u16 match_flags;
  79	__u16 lmp_subver;
  80	__u16 hci_rev;
  81	__u8 hci_ver;
  82	__u8 hci_bus;
  83	__u8 chip_type;
  84	bool config_needed;
  85	bool has_rom_version;
  86	bool has_msft_ext;
  87	char *fw_name;
  88	char *cfg_name;
  89	char *hw_info;
  90};
  91
  92struct btrtl_device_info {
  93	const struct id_table *ic_info;
  94	u8 rom_version;
  95	u8 *fw_data;
  96	int fw_len;
  97	u8 *cfg_data;
  98	int cfg_len;
  99	bool drop_fw;
 100	int project_id;
 101	u8 key_id;
 102	struct list_head patch_subsecs;
 103};
 104
 105static const struct id_table ic_id_table[] = {
 106	/* 8723A */
 107	{ IC_INFO(RTL_ROM_LMP_8723A, 0xb, 0x6, HCI_USB),
 108	  .config_needed = false,
 109	  .has_rom_version = false,
 110	  .fw_name = "rtl_bt/rtl8723a_fw",
 111	  .cfg_name = NULL,
 112	  .hw_info = "rtl8723au" },
 113
 114	/* 8723BS */
 115	{ IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_UART),
 116	  .config_needed = true,
 117	  .has_rom_version = true,
 118	  .fw_name  = "rtl_bt/rtl8723bs_fw",
 119	  .cfg_name = "rtl_bt/rtl8723bs_config",
 120	  .hw_info  = "rtl8723bs" },
 121
 122	/* 8723B */
 123	{ IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_USB),
 124	  .config_needed = false,
 125	  .has_rom_version = true,
 126	  .fw_name  = "rtl_bt/rtl8723b_fw",
 127	  .cfg_name = "rtl_bt/rtl8723b_config",
 128	  .hw_info  = "rtl8723bu" },
 129
 130	/* 8723CS-CG */
 131	{ .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
 132			 IC_MATCH_FL_HCIBUS,
 133	  .lmp_subver = RTL_ROM_LMP_8703B,
 134	  .chip_type = RTL_CHIP_8723CS_CG,
 135	  .hci_bus = HCI_UART,
 136	  .config_needed = true,
 137	  .has_rom_version = true,
 138	  .fw_name  = "rtl_bt/rtl8723cs_cg_fw",
 139	  .cfg_name = "rtl_bt/rtl8723cs_cg_config",
 140	  .hw_info  = "rtl8723cs-cg" },
 141
 142	/* 8723CS-VF */
 143	{ .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
 144			 IC_MATCH_FL_HCIBUS,
 145	  .lmp_subver = RTL_ROM_LMP_8703B,
 146	  .chip_type = RTL_CHIP_8723CS_VF,
 147	  .hci_bus = HCI_UART,
 148	  .config_needed = true,
 149	  .has_rom_version = true,
 150	  .fw_name  = "rtl_bt/rtl8723cs_vf_fw",
 151	  .cfg_name = "rtl_bt/rtl8723cs_vf_config",
 152	  .hw_info  = "rtl8723cs-vf" },
 153
 154	/* 8723CS-XX */
 155	{ .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
 156			 IC_MATCH_FL_HCIBUS,
 157	  .lmp_subver = RTL_ROM_LMP_8703B,
 158	  .chip_type = RTL_CHIP_8723CS_XX,
 159	  .hci_bus = HCI_UART,
 160	  .config_needed = true,
 161	  .has_rom_version = true,
 162	  .fw_name  = "rtl_bt/rtl8723cs_xx_fw",
 163	  .cfg_name = "rtl_bt/rtl8723cs_xx_config",
 164	  .hw_info  = "rtl8723cs" },
 165
 166	/* 8723D */
 167	{ IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_USB),
 168	  .config_needed = true,
 169	  .has_rom_version = true,
 170	  .fw_name  = "rtl_bt/rtl8723d_fw",
 171	  .cfg_name = "rtl_bt/rtl8723d_config",
 172	  .hw_info  = "rtl8723du" },
 173
 174	/* 8723DS */
 175	{ IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_UART),
 176	  .config_needed = true,
 177	  .has_rom_version = true,
 178	  .fw_name  = "rtl_bt/rtl8723ds_fw",
 179	  .cfg_name = "rtl_bt/rtl8723ds_config",
 180	  .hw_info  = "rtl8723ds" },
 181
 182	/* 8821A */
 183	{ IC_INFO(RTL_ROM_LMP_8821A, 0xa, 0x6, HCI_USB),
 184	  .config_needed = false,
 185	  .has_rom_version = true,
 186	  .fw_name  = "rtl_bt/rtl8821a_fw",
 187	  .cfg_name = "rtl_bt/rtl8821a_config",
 188	  .hw_info  = "rtl8821au" },
 189
 190	/* 8821C */
 191	{ IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_USB),
 192	  .config_needed = false,
 193	  .has_rom_version = true,
 194	  .has_msft_ext = true,
 195	  .fw_name  = "rtl_bt/rtl8821c_fw",
 196	  .cfg_name = "rtl_bt/rtl8821c_config",
 197	  .hw_info  = "rtl8821cu" },
 198
 199	/* 8821CS */
 200	{ IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_UART),
 201	  .config_needed = true,
 202	  .has_rom_version = true,
 203	  .has_msft_ext = true,
 204	  .fw_name  = "rtl_bt/rtl8821cs_fw",
 205	  .cfg_name = "rtl_bt/rtl8821cs_config",
 206	  .hw_info  = "rtl8821cs" },
 207
 208	/* 8761A */
 209	{ IC_INFO(RTL_ROM_LMP_8761A, 0xa, 0x6, HCI_USB),
 210	  .config_needed = false,
 211	  .has_rom_version = true,
 212	  .fw_name  = "rtl_bt/rtl8761a_fw",
 213	  .cfg_name = "rtl_bt/rtl8761a_config",
 214	  .hw_info  = "rtl8761au" },
 215
 216	/* 8761B */
 217	{ IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_UART),
 218	  .config_needed = false,
 219	  .has_rom_version = true,
 220	  .has_msft_ext = true,
 221	  .fw_name  = "rtl_bt/rtl8761b_fw",
 222	  .cfg_name = "rtl_bt/rtl8761b_config",
 223	  .hw_info  = "rtl8761btv" },
 224
 225	/* 8761BU */
 226	{ IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_USB),
 227	  .config_needed = false,
 228	  .has_rom_version = true,
 229	  .fw_name  = "rtl_bt/rtl8761bu_fw",
 230	  .cfg_name = "rtl_bt/rtl8761bu_config",
 231	  .hw_info  = "rtl8761bu" },
 232
 233	/* 8822C with UART interface */
 234	{ IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0x8, HCI_UART),
 235	  .config_needed = true,
 236	  .has_rom_version = true,
 237	  .has_msft_ext = true,
 238	  .fw_name  = "rtl_bt/rtl8822cs_fw",
 239	  .cfg_name = "rtl_bt/rtl8822cs_config",
 240	  .hw_info  = "rtl8822cs" },
 241
 242	/* 8822C with UART interface */
 243	{ IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_UART),
 244	  .config_needed = true,
 245	  .has_rom_version = true,
 246	  .has_msft_ext = true,
 247	  .fw_name  = "rtl_bt/rtl8822cs_fw",
 248	  .cfg_name = "rtl_bt/rtl8822cs_config",
 249	  .hw_info  = "rtl8822cs" },
 250
 251	/* 8822C with USB interface */
 252	{ IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_USB),
 253	  .config_needed = false,
 254	  .has_rom_version = true,
 255	  .has_msft_ext = true,
 256	  .fw_name  = "rtl_bt/rtl8822cu_fw",
 257	  .cfg_name = "rtl_bt/rtl8822cu_config",
 258	  .hw_info  = "rtl8822cu" },
 259
 260	/* 8822B */
 261	{ IC_INFO(RTL_ROM_LMP_8822B, 0xb, 0x7, HCI_USB),
 262	  .config_needed = true,
 263	  .has_rom_version = true,
 264	  .has_msft_ext = true,
 265	  .fw_name  = "rtl_bt/rtl8822b_fw",
 266	  .cfg_name = "rtl_bt/rtl8822b_config",
 267	  .hw_info  = "rtl8822bu" },
 268
 269	/* 8852A */
 270	{ IC_INFO(RTL_ROM_LMP_8852A, 0xa, 0xb, HCI_USB),
 271	  .config_needed = false,
 272	  .has_rom_version = true,
 273	  .has_msft_ext = true,
 274	  .fw_name  = "rtl_bt/rtl8852au_fw",
 275	  .cfg_name = "rtl_bt/rtl8852au_config",
 276	  .hw_info  = "rtl8852au" },
 277
 278	/* 8852B with UART interface */
 279	{ IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_UART),
 280	  .config_needed = true,
 281	  .has_rom_version = true,
 282	  .has_msft_ext = true,
 283	  .fw_name  = "rtl_bt/rtl8852bs_fw",
 284	  .cfg_name = "rtl_bt/rtl8852bs_config",
 285	  .hw_info  = "rtl8852bs" },
 286
 287	/* 8852B */
 288	{ IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_USB),
 289	  .config_needed = false,
 290	  .has_rom_version = true,
 291	  .has_msft_ext = true,
 292	  .fw_name  = "rtl_bt/rtl8852bu_fw",
 293	  .cfg_name = "rtl_bt/rtl8852bu_config",
 294	  .hw_info  = "rtl8852bu" },
 295
 296	/* 8852C */
 297	{ IC_INFO(RTL_ROM_LMP_8852A, 0xc, 0xc, HCI_USB),
 298	  .config_needed = false,
 299	  .has_rom_version = true,
 300	  .has_msft_ext = true,
 301	  .fw_name  = "rtl_bt/rtl8852cu_fw",
 302	  .cfg_name = "rtl_bt/rtl8852cu_config",
 303	  .hw_info  = "rtl8852cu" },
 304
 305	/* 8851B */
 306	{ IC_INFO(RTL_ROM_LMP_8851B, 0xb, 0xc, HCI_USB),
 307	  .config_needed = false,
 308	  .has_rom_version = true,
 309	  .has_msft_ext = false,
 310	  .fw_name  = "rtl_bt/rtl8851bu_fw",
 311	  .cfg_name = "rtl_bt/rtl8851bu_config",
 312	  .hw_info  = "rtl8851bu" },
 313
 314	/* 8922A */
 315	{ IC_INFO(RTL_ROM_LMP_8922A, 0xa, 0xc, HCI_USB),
 316	  .config_needed = false,
 317	  .has_rom_version = true,
 318	  .has_msft_ext = true,
 319	  .fw_name  = "rtl_bt/rtl8922au_fw",
 320	  .cfg_name = "rtl_bt/rtl8922au_config",
 321	  .hw_info  = "rtl8922au" },
 322
 323	/* 8852BT/8852BE-VT */
 324	{ IC_INFO(RTL_ROM_LMP_8852A, 0x87, 0xc, HCI_USB),
 325	  .config_needed = false,
 326	  .has_rom_version = true,
 327	  .has_msft_ext = true,
 328	  .fw_name  = "rtl_bt/rtl8852btu_fw",
 329	  .cfg_name = "rtl_bt/rtl8852btu_config",
 330	  .hw_info  = "rtl8852btu" },
 331	};
 332
 333static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
 334					     u8 hci_ver, u8 hci_bus,
 335					     u8 chip_type)
 336{
 337	int i;
 338
 339	for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
 340		if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
 341		    (ic_id_table[i].lmp_subver != lmp_subver))
 342			continue;
 343		if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
 344		    (ic_id_table[i].hci_rev != hci_rev))
 345			continue;
 346		if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
 347		    (ic_id_table[i].hci_ver != hci_ver))
 348			continue;
 349		if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
 350		    (ic_id_table[i].hci_bus != hci_bus))
 351			continue;
 352		if ((ic_id_table[i].match_flags & IC_MATCH_FL_CHIP_TYPE) &&
 353		    (ic_id_table[i].chip_type != chip_type))
 354			continue;
 355
 356		break;
 357	}
 358	if (i >= ARRAY_SIZE(ic_id_table))
 359		return NULL;
 360
 361	return &ic_id_table[i];
 362}
 363
 364static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
 365{
 366	struct sk_buff *skb;
 367
 368	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
 369			     HCI_INIT_TIMEOUT);
 370	if (IS_ERR(skb)) {
 371		rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)",
 372			    PTR_ERR(skb));
 373		return skb;
 374	}
 375
 376	if (skb->len != sizeof(struct hci_rp_read_local_version)) {
 377		rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch");
 378		kfree_skb(skb);
 379		return ERR_PTR(-EIO);
 380	}
 381
 382	return skb;
 383}
 384
 385static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
 386{
 387	struct rtl_rom_version_evt *rom_version;
 388	struct sk_buff *skb;
 389
 390	/* Read RTL ROM version command */
 391	skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
 392	if (IS_ERR(skb)) {
 393		rtl_dev_err(hdev, "Read ROM version failed (%ld)",
 394			    PTR_ERR(skb));
 395		return PTR_ERR(skb);
 396	}
 397
 398	if (skb->len != sizeof(*rom_version)) {
 399		rtl_dev_err(hdev, "version event length mismatch");
 400		kfree_skb(skb);
 401		return -EIO;
 402	}
 403
 404	rom_version = (struct rtl_rom_version_evt *)skb->data;
 405	rtl_dev_info(hdev, "rom_version status=%x version=%x",
 406		     rom_version->status, rom_version->version);
 407
 408	*version = rom_version->version;
 409
 410	kfree_skb(skb);
 411	return 0;
 412}
 413
 414static int btrtl_vendor_read_reg16(struct hci_dev *hdev,
 415				   struct rtl_vendor_cmd *cmd, u8 *rp)
 416{
 417	struct sk_buff *skb;
 418	int err = 0;
 419
 420	skb = __hci_cmd_sync(hdev, 0xfc61, sizeof(*cmd), cmd,
 421			     HCI_INIT_TIMEOUT);
 422	if (IS_ERR(skb)) {
 423		err = PTR_ERR(skb);
 424		rtl_dev_err(hdev, "RTL: Read reg16 failed (%d)", err);
 425		return err;
 426	}
 427
 428	if (skb->len != 3 || skb->data[0]) {
 429		bt_dev_err(hdev, "RTL: Read reg16 length mismatch");
 430		kfree_skb(skb);
 431		return -EIO;
 432	}
 433
 434	if (rp)
 435		memcpy(rp, skb->data + 1, 2);
 436
 437	kfree_skb(skb);
 438
 439	return 0;
 440}
 441
 442static void *rtl_iov_pull_data(struct rtl_iovec *iov, u32 len)
 443{
 444	void *data = iov->data;
 445
 446	if (iov->len < len)
 447		return NULL;
 448
 449	iov->data += len;
 450	iov->len  -= len;
 451
 452	return data;
 453}
 454
 455static void btrtl_insert_ordered_subsec(struct rtl_subsection *node,
 456					struct btrtl_device_info *btrtl_dev)
 457{
 458	struct list_head *pos;
 459	struct list_head *next;
 460	struct rtl_subsection *subsec;
 461
 462	list_for_each_safe(pos, next, &btrtl_dev->patch_subsecs) {
 463		subsec = list_entry(pos, struct rtl_subsection, list);
 464		if (subsec->prio >= node->prio)
 465			break;
 466	}
 467	__list_add(&node->list, pos->prev, pos);
 468}
 469
 470static int btrtl_parse_section(struct hci_dev *hdev,
 471			       struct btrtl_device_info *btrtl_dev, u32 opcode,
 472			       u8 *data, u32 len)
 473{
 474	struct rtl_section_hdr *hdr;
 475	struct rtl_subsection *subsec;
 476	struct rtl_common_subsec *common_subsec;
 477	struct rtl_sec_hdr *sec_hdr;
 478	int i;
 479	u8 *ptr;
 480	u16 num_subsecs;
 481	u32 subsec_len;
 482	int rc = 0;
 483	struct rtl_iovec iov = {
 484		.data = data,
 485		.len  = len,
 486	};
 487
 488	hdr = rtl_iov_pull_data(&iov, sizeof(*hdr));
 489	if (!hdr)
 490		return -EINVAL;
 491	num_subsecs = le16_to_cpu(hdr->num);
 492
 493	for (i = 0; i < num_subsecs; i++) {
 494		common_subsec = rtl_iov_pull_data(&iov, sizeof(*common_subsec));
 495		if (!common_subsec)
 496			break;
 497		subsec_len = le32_to_cpu(common_subsec->len);
 498
 499		rtl_dev_dbg(hdev, "subsec, eco 0x%02x, len %08x",
 500			    common_subsec->eco, subsec_len);
 501
 502		ptr = rtl_iov_pull_data(&iov, subsec_len);
 503		if (!ptr)
 504			break;
 505
 506		if (common_subsec->eco != btrtl_dev->rom_version + 1)
 507			continue;
 508
 509		switch (opcode) {
 510		case RTL_PATCH_SECURITY_HEADER:
 511			sec_hdr = (void *)common_subsec;
 512			if (sec_hdr->key_id != btrtl_dev->key_id)
 513				continue;
 514			break;
 515		}
 516
 517		subsec = kzalloc(sizeof(*subsec), GFP_KERNEL);
 518		if (!subsec)
 519			return -ENOMEM;
 520		subsec->opcode = opcode;
 521		subsec->prio = common_subsec->prio;
 522		subsec->len  = subsec_len;
 523		subsec->data = ptr;
 524		btrtl_insert_ordered_subsec(subsec, btrtl_dev);
 525		rc  += subsec_len;
 526	}
 527
 528	return rc;
 529}
 530
 531static int rtlbt_parse_firmware_v2(struct hci_dev *hdev,
 532				   struct btrtl_device_info *btrtl_dev,
 533				   unsigned char **_buf)
 534{
 535	struct rtl_epatch_header_v2 *hdr;
 536	int rc;
 537	u8 reg_val[2];
 538	u8 key_id;
 539	u32 num_sections;
 540	struct rtl_section *section;
 541	struct rtl_subsection *entry, *tmp;
 542	u32 section_len;
 543	u32 opcode;
 544	int len = 0;
 545	int i;
 546	u8 *ptr;
 547	struct rtl_iovec iov = {
 548		.data = btrtl_dev->fw_data,
 549		.len  = btrtl_dev->fw_len - 7, /* Cut the tail */
 550	};
 551
 552	rc = btrtl_vendor_read_reg16(hdev, RTL_SEC_PROJ, reg_val);
 553	if (rc < 0)
 554		return -EIO;
 555	key_id = reg_val[0];
 556
 557	rtl_dev_dbg(hdev, "%s: key id %u", __func__, key_id);
 558
 559	btrtl_dev->key_id = key_id;
 560
 561	hdr = rtl_iov_pull_data(&iov, sizeof(*hdr));
 562	if (!hdr)
 563		return -EINVAL;
 564	num_sections = le32_to_cpu(hdr->num_sections);
 565
 566	rtl_dev_dbg(hdev, "FW version %08x-%08x", *((u32 *)hdr->fw_version),
 567		    *((u32 *)(hdr->fw_version + 4)));
 568
 569	for (i = 0; i < num_sections; i++) {
 570		section = rtl_iov_pull_data(&iov, sizeof(*section));
 571		if (!section)
 572			break;
 573		section_len = le32_to_cpu(section->len);
 574		opcode      = le32_to_cpu(section->opcode);
 575
 576		rtl_dev_dbg(hdev, "opcode 0x%04x", section->opcode);
 577
 578		ptr = rtl_iov_pull_data(&iov, section_len);
 579		if (!ptr)
 580			break;
 581
 582		switch (opcode) {
 583		case RTL_PATCH_SNIPPETS:
 584			rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
 585						 ptr, section_len);
 586			break;
 587		case RTL_PATCH_SECURITY_HEADER:
 588			/* If key_id from chip is zero, ignore all security
 589			 * headers.
 590			 */
 591			if (!key_id)
 592				break;
 593			rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
 594						 ptr, section_len);
 595			break;
 596		case RTL_PATCH_DUMMY_HEADER:
 597			rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
 598						 ptr, section_len);
 599			break;
 600		default:
 601			rc = 0;
 602			break;
 603		}
 604		if (rc < 0) {
 605			rtl_dev_err(hdev, "RTL: Parse section (%u) err %d",
 606				    opcode, rc);
 607			return rc;
 608		}
 609		len += rc;
 610	}
 611
 612	if (!len)
 613		return -ENODATA;
 614
 615	/* Allocate mem and copy all found subsecs. */
 616	ptr = kvmalloc(len, GFP_KERNEL);
 617	if (!ptr)
 618		return -ENOMEM;
 619
 620	len = 0;
 621	list_for_each_entry_safe(entry, tmp, &btrtl_dev->patch_subsecs, list) {
 622		rtl_dev_dbg(hdev, "RTL: opcode %08x, addr %p, len 0x%x",
 623			    entry->opcode, entry->data, entry->len);
 624		memcpy(ptr + len, entry->data, entry->len);
 625		len += entry->len;
 626	}
 627
 628	if (!len)
 629		return -EPERM;
 630
 631	*_buf = ptr;
 632	return len;
 633}
 634
 635static int rtlbt_parse_firmware(struct hci_dev *hdev,
 636				struct btrtl_device_info *btrtl_dev,
 637				unsigned char **_buf)
 638{
 639	static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
 640	struct btrealtek_data *coredump_info = hci_get_priv(hdev);
 641	struct rtl_epatch_header *epatch_info;
 642	unsigned char *buf;
 643	int i, len;
 644	size_t min_size;
 645	u8 opcode, length, data;
 646	int project_id = -1;
 647	const unsigned char *fwptr, *chip_id_base;
 648	const unsigned char *patch_length_base, *patch_offset_base;
 649	u32 patch_offset = 0;
 650	u16 patch_length, num_patches;
 651	static const struct {
 652		__u16 lmp_subver;
 653		__u8 id;
 654	} project_id_to_lmp_subver[] = {
 655		{ RTL_ROM_LMP_8723A, 0 },
 656		{ RTL_ROM_LMP_8723B, 1 },
 657		{ RTL_ROM_LMP_8821A, 2 },
 658		{ RTL_ROM_LMP_8761A, 3 },
 659		{ RTL_ROM_LMP_8703B, 7 },
 660		{ RTL_ROM_LMP_8822B, 8 },
 661		{ RTL_ROM_LMP_8723B, 9 },	/* 8723D */
 662		{ RTL_ROM_LMP_8821A, 10 },	/* 8821C */
 663		{ RTL_ROM_LMP_8822B, 13 },	/* 8822C */
 664		{ RTL_ROM_LMP_8761A, 14 },	/* 8761B */
 665		{ RTL_ROM_LMP_8852A, 18 },	/* 8852A */
 666		{ RTL_ROM_LMP_8852A, 20 },	/* 8852B */
 667		{ RTL_ROM_LMP_8852A, 25 },	/* 8852C */
 668		{ RTL_ROM_LMP_8851B, 36 },	/* 8851B */
 669		{ RTL_ROM_LMP_8922A, 44 },	/* 8922A */
 670		{ RTL_ROM_LMP_8852A, 47 },	/* 8852BT */
 671	};
 672
 673	if (btrtl_dev->fw_len <= 8)
 674		return -EINVAL;
 675
 676	if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8))
 677		min_size = sizeof(struct rtl_epatch_header) +
 678				sizeof(extension_sig) + 3;
 679	else if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE_V2, 8))
 680		min_size = sizeof(struct rtl_epatch_header_v2) +
 681				sizeof(extension_sig) + 3;
 682	else
 683		return -EINVAL;
 684
 685	if (btrtl_dev->fw_len < min_size)
 686		return -EINVAL;
 687
 688	fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
 689	if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
 690		rtl_dev_err(hdev, "extension section signature mismatch");
 691		return -EINVAL;
 692	}
 693
 694	/* Loop from the end of the firmware parsing instructions, until
 695	 * we find an instruction that identifies the "project ID" for the
 696	 * hardware supported by this firwmare file.
 697	 * Once we have that, we double-check that project_id is suitable
 698	 * for the hardware we are working with.
 699	 */
 700	while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
 701		opcode = *--fwptr;
 702		length = *--fwptr;
 703		data = *--fwptr;
 704
 705		BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
 706
 707		if (opcode == 0xff) /* EOF */
 708			break;
 709
 710		if (length == 0) {
 711			rtl_dev_err(hdev, "found instruction with length 0");
 712			return -EINVAL;
 713		}
 714
 715		if (opcode == 0 && length == 1) {
 716			project_id = data;
 717			break;
 718		}
 719
 720		fwptr -= length;
 721	}
 722
 723	if (project_id < 0) {
 724		rtl_dev_err(hdev, "failed to find version instruction");
 725		return -EINVAL;
 726	}
 727
 728	/* Find project_id in table */
 729	for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
 730		if (project_id == project_id_to_lmp_subver[i].id) {
 731			btrtl_dev->project_id = project_id;
 732			break;
 733		}
 734	}
 735
 736	if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
 737		rtl_dev_err(hdev, "unknown project id %d", project_id);
 738		return -EINVAL;
 739	}
 740
 741	if (btrtl_dev->ic_info->lmp_subver !=
 742				project_id_to_lmp_subver[i].lmp_subver) {
 743		rtl_dev_err(hdev, "firmware is for %x but this is a %x",
 744			    project_id_to_lmp_subver[i].lmp_subver,
 745			    btrtl_dev->ic_info->lmp_subver);
 746		return -EINVAL;
 747	}
 748
 749	if (memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8) != 0) {
 750		if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE_V2, 8))
 751			return rtlbt_parse_firmware_v2(hdev, btrtl_dev, _buf);
 752		rtl_dev_err(hdev, "bad EPATCH signature");
 753		return -EINVAL;
 754	}
 755
 756	epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
 757	num_patches = le16_to_cpu(epatch_info->num_patches);
 758
 759	BT_DBG("fw_version=%x, num_patches=%d",
 760	       le32_to_cpu(epatch_info->fw_version), num_patches);
 761	coredump_info->rtl_dump.fw_version = le32_to_cpu(epatch_info->fw_version);
 762
 763	/* After the rtl_epatch_header there is a funky patch metadata section.
 764	 * Assuming 2 patches, the layout is:
 765	 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
 766	 *
 767	 * Find the right patch for this chip.
 768	 */
 769	min_size += 8 * num_patches;
 770	if (btrtl_dev->fw_len < min_size)
 771		return -EINVAL;
 772
 773	chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
 774	patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
 775	patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
 776	for (i = 0; i < num_patches; i++) {
 777		u16 chip_id = get_unaligned_le16(chip_id_base +
 778						 (i * sizeof(u16)));
 779		if (chip_id == btrtl_dev->rom_version + 1) {
 780			patch_length = get_unaligned_le16(patch_length_base +
 781							  (i * sizeof(u16)));
 782			patch_offset = get_unaligned_le32(patch_offset_base +
 783							  (i * sizeof(u32)));
 784			break;
 785		}
 786	}
 787
 788	if (!patch_offset) {
 789		rtl_dev_err(hdev, "didn't find patch for chip id %d",
 790			    btrtl_dev->rom_version);
 791		return -EINVAL;
 792	}
 793
 794	BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
 795	min_size = patch_offset + patch_length;
 796	if (btrtl_dev->fw_len < min_size)
 797		return -EINVAL;
 798
 799	/* Copy the firmware into a new buffer and write the version at
 800	 * the end.
 801	 */
 802	len = patch_length;
 803	buf = kvmalloc(patch_length, GFP_KERNEL);
 804	if (!buf)
 805		return -ENOMEM;
 806
 807	memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
 808	memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
 809
 810	*_buf = buf;
 811	return len;
 812}
 813
 814static int rtl_download_firmware(struct hci_dev *hdev,
 815				 const unsigned char *data, int fw_len)
 816{
 817	struct rtl_download_cmd *dl_cmd;
 818	int frag_num = fw_len / RTL_FRAG_LEN + 1;
 819	int frag_len = RTL_FRAG_LEN;
 820	int ret = 0;
 821	int i;
 822	int j = 0;
 823	struct sk_buff *skb;
 824	struct hci_rp_read_local_version *rp;
 825
 826	dl_cmd = kmalloc(sizeof(*dl_cmd), GFP_KERNEL);
 827	if (!dl_cmd)
 828		return -ENOMEM;
 829
 830	for (i = 0; i < frag_num; i++) {
 831		struct sk_buff *skb;
 832
 833		dl_cmd->index = j++;
 834		if (dl_cmd->index == 0x7f)
 835			j = 1;
 836
 837		if (i == (frag_num - 1)) {
 838			dl_cmd->index |= 0x80; /* data end */
 839			frag_len = fw_len % RTL_FRAG_LEN;
 840		}
 841		rtl_dev_dbg(hdev, "download fw (%d/%d). index = %d", i,
 842				frag_num, dl_cmd->index);
 843		memcpy(dl_cmd->data, data, frag_len);
 844
 845		/* Send download command */
 846		skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
 847				     HCI_INIT_TIMEOUT);
 848		if (IS_ERR(skb)) {
 849			rtl_dev_err(hdev, "download fw command failed (%ld)",
 850				    PTR_ERR(skb));
 851			ret = PTR_ERR(skb);
 852			goto out;
 853		}
 854
 855		if (skb->len != sizeof(struct rtl_download_response)) {
 856			rtl_dev_err(hdev, "download fw event length mismatch");
 857			kfree_skb(skb);
 858			ret = -EIO;
 859			goto out;
 860		}
 861
 862		kfree_skb(skb);
 863		data += RTL_FRAG_LEN;
 864	}
 865
 866	skb = btrtl_read_local_version(hdev);
 867	if (IS_ERR(skb)) {
 868		ret = PTR_ERR(skb);
 869		rtl_dev_err(hdev, "read local version failed");
 870		goto out;
 871	}
 872
 873	rp = (struct hci_rp_read_local_version *)skb->data;
 874	rtl_dev_info(hdev, "fw version 0x%04x%04x",
 875		     __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver));
 876	kfree_skb(skb);
 877
 878out:
 879	kfree(dl_cmd);
 880	return ret;
 881}
 882
 883static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
 884{
 885	const struct firmware *fw;
 886	int ret;
 887
 888	rtl_dev_info(hdev, "loading %s", name);
 889	ret = request_firmware(&fw, name, &hdev->dev);
 890	if (ret < 0)
 891		return ret;
 892	ret = fw->size;
 893	*buff = kvmemdup(fw->data, fw->size, GFP_KERNEL);
 894	if (!*buff)
 
 
 895		ret = -ENOMEM;
 896
 897	release_firmware(fw);
 898
 899	return ret;
 900}
 901
 902static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
 903				struct btrtl_device_info *btrtl_dev)
 904{
 905	if (btrtl_dev->fw_len < 8)
 906		return -EINVAL;
 907
 908	/* Check that the firmware doesn't have the epatch signature
 909	 * (which is only for RTL8723B and newer).
 910	 */
 911	if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
 912		rtl_dev_err(hdev, "unexpected EPATCH signature!");
 913		return -EINVAL;
 914	}
 915
 916	return rtl_download_firmware(hdev, btrtl_dev->fw_data,
 917				     btrtl_dev->fw_len);
 918}
 919
 920static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
 921				struct btrtl_device_info *btrtl_dev)
 922{
 923	unsigned char *fw_data = NULL;
 924	int ret;
 925	u8 *tbuff;
 926
 927	ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
 928	if (ret < 0)
 929		goto out;
 930
 931	if (btrtl_dev->cfg_len > 0) {
 932		tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
 933		if (!tbuff) {
 934			ret = -ENOMEM;
 935			goto out;
 936		}
 937
 938		memcpy(tbuff, fw_data, ret);
 939		kvfree(fw_data);
 940
 941		memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
 942		ret += btrtl_dev->cfg_len;
 943
 944		fw_data = tbuff;
 945	}
 946
 947	rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret);
 948
 949	ret = rtl_download_firmware(hdev, fw_data, ret);
 950
 951out:
 952	kvfree(fw_data);
 953	return ret;
 954}
 955
 956static void btrtl_coredump(struct hci_dev *hdev)
 957{
 958	static const u8 param[] = { 0x00, 0x00 };
 959
 960	__hci_cmd_send(hdev, RTL_VSC_OP_COREDUMP, sizeof(param), param);
 961}
 962
 963static void btrtl_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
 964{
 965	struct btrealtek_data *coredump_info = hci_get_priv(hdev);
 966	char buf[80];
 967
 968	if (coredump_info->rtl_dump.controller)
 969		snprintf(buf, sizeof(buf), "Controller Name: %s\n",
 970			 coredump_info->rtl_dump.controller);
 971	else
 972		snprintf(buf, sizeof(buf), "Controller Name: Unknown\n");
 973	skb_put_data(skb, buf, strlen(buf));
 974
 975	snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
 976		 coredump_info->rtl_dump.fw_version);
 977	skb_put_data(skb, buf, strlen(buf));
 978
 979	snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info->rtl_dump.driver_name);
 980	skb_put_data(skb, buf, strlen(buf));
 981
 982	snprintf(buf, sizeof(buf), "Vendor: Realtek\n");
 983	skb_put_data(skb, buf, strlen(buf));
 984}
 985
 986static void btrtl_register_devcoredump_support(struct hci_dev *hdev)
 987{
 988	hci_devcd_register(hdev, btrtl_coredump, btrtl_dmp_hdr, NULL);
 989
 990}
 991
 992void btrtl_set_driver_name(struct hci_dev *hdev, const char *driver_name)
 993{
 994	struct btrealtek_data *coredump_info = hci_get_priv(hdev);
 995
 996	coredump_info->rtl_dump.driver_name = driver_name;
 997}
 998EXPORT_SYMBOL_GPL(btrtl_set_driver_name);
 999
1000static bool rtl_has_chip_type(u16 lmp_subver)
1001{
1002	switch (lmp_subver) {
1003	case RTL_ROM_LMP_8703B:
1004		return true;
1005	default:
1006		break;
1007	}
1008
1009	return  false;
1010}
1011
1012static int rtl_read_chip_type(struct hci_dev *hdev, u8 *type)
1013{
1014	struct rtl_chip_type_evt *chip_type;
1015	struct sk_buff *skb;
1016	const unsigned char cmd_buf[] = {0x00, 0x94, 0xa0, 0x00, 0xb0};
1017
1018	/* Read RTL chip type command */
1019	skb = __hci_cmd_sync(hdev, 0xfc61, 5, cmd_buf, HCI_INIT_TIMEOUT);
1020	if (IS_ERR(skb)) {
1021		rtl_dev_err(hdev, "Read chip type failed (%ld)",
1022			    PTR_ERR(skb));
1023		return PTR_ERR(skb);
1024	}
1025
1026	chip_type = skb_pull_data(skb, sizeof(*chip_type));
1027	if (!chip_type) {
1028		rtl_dev_err(hdev, "RTL chip type event length mismatch");
1029		kfree_skb(skb);
1030		return -EIO;
1031	}
1032
1033	rtl_dev_info(hdev, "chip_type status=%x type=%x",
1034		     chip_type->status, chip_type->type);
1035
1036	*type = chip_type->type & 0x0f;
1037
1038	kfree_skb(skb);
1039	return 0;
1040}
1041
1042void btrtl_free(struct btrtl_device_info *btrtl_dev)
1043{
1044	struct rtl_subsection *entry, *tmp;
1045
1046	kvfree(btrtl_dev->fw_data);
1047	kvfree(btrtl_dev->cfg_data);
1048
1049	list_for_each_entry_safe(entry, tmp, &btrtl_dev->patch_subsecs, list) {
1050		list_del(&entry->list);
1051		kfree(entry);
1052	}
1053
1054	kfree(btrtl_dev);
1055}
1056EXPORT_SYMBOL_GPL(btrtl_free);
1057
1058struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
1059					   const char *postfix)
1060{
1061	struct btrealtek_data *coredump_info = hci_get_priv(hdev);
1062	struct btrtl_device_info *btrtl_dev;
1063	struct sk_buff *skb;
1064	struct hci_rp_read_local_version *resp;
1065	struct hci_command_hdr *cmd;
1066	char fw_name[40];
1067	char cfg_name[40];
1068	u16 hci_rev, lmp_subver;
1069	u8 hci_ver, lmp_ver, chip_type = 0;
1070	int ret;
1071	u8 reg_val[2];
1072
1073	btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
1074	if (!btrtl_dev) {
1075		ret = -ENOMEM;
1076		goto err_alloc;
1077	}
1078
1079	INIT_LIST_HEAD(&btrtl_dev->patch_subsecs);
1080
1081check_version:
1082	ret = btrtl_vendor_read_reg16(hdev, RTL_CHIP_SUBVER, reg_val);
1083	if (ret < 0)
1084		goto err_free;
1085	lmp_subver = get_unaligned_le16(reg_val);
1086
1087	if (lmp_subver == RTL_ROM_LMP_8822B) {
1088		ret = btrtl_vendor_read_reg16(hdev, RTL_CHIP_REV, reg_val);
1089		if (ret < 0)
1090			goto err_free;
1091		hci_rev = get_unaligned_le16(reg_val);
1092
1093		/* 8822E */
1094		if (hci_rev == 0x000e) {
1095			hci_ver = 0x0c;
1096			lmp_ver = 0x0c;
1097			btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev,
1098							    hci_ver, hdev->bus,
1099							    chip_type);
1100			goto next;
1101		}
1102	}
1103
1104	skb = btrtl_read_local_version(hdev);
1105	if (IS_ERR(skb)) {
1106		ret = PTR_ERR(skb);
1107		goto err_free;
1108	}
1109
1110	resp = (struct hci_rp_read_local_version *)skb->data;
1111
1112	hci_ver    = resp->hci_ver;
1113	hci_rev    = le16_to_cpu(resp->hci_rev);
1114	lmp_ver    = resp->lmp_ver;
1115	lmp_subver = le16_to_cpu(resp->lmp_subver);
1116
1117	kfree_skb(skb);
1118
1119	if (rtl_has_chip_type(lmp_subver)) {
1120		ret = rtl_read_chip_type(hdev, &chip_type);
1121		if (ret)
1122			goto err_free;
1123	}
1124
1125	btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
1126					    hdev->bus, chip_type);
1127
1128next:
1129	rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
1130		     hci_ver, hci_rev,
1131		     lmp_ver, lmp_subver);
1132
1133	if (!btrtl_dev->ic_info && !btrtl_dev->drop_fw)
1134		btrtl_dev->drop_fw = true;
1135	else
1136		btrtl_dev->drop_fw = false;
1137
1138	if (btrtl_dev->drop_fw) {
1139		skb = bt_skb_alloc(sizeof(*cmd), GFP_KERNEL);
1140		if (!skb)
1141			goto err_free;
1142
1143		cmd = skb_put(skb, HCI_COMMAND_HDR_SIZE);
1144		cmd->opcode = cpu_to_le16(0xfc66);
1145		cmd->plen = 0;
1146
1147		hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1148
1149		ret = hdev->send(hdev, skb);
1150		if (ret < 0) {
1151			bt_dev_err(hdev, "sending frame failed (%d)", ret);
1152			kfree_skb(skb);
1153			goto err_free;
1154		}
1155
1156		/* Ensure the above vendor command is sent to controller and
1157		 * process has done.
1158		 */
1159		msleep(200);
1160
1161		goto check_version;
1162	}
1163
1164	if (!btrtl_dev->ic_info) {
1165		rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
1166			    lmp_subver, hci_rev, hci_ver);
1167		return btrtl_dev;
1168	}
1169
1170	if (btrtl_dev->ic_info->has_rom_version) {
1171		ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
1172		if (ret)
1173			goto err_free;
1174	}
1175
1176	if (!btrtl_dev->ic_info->fw_name) {
1177		ret = -ENOMEM;
1178		goto err_free;
1179	}
1180
1181	btrtl_dev->fw_len = -EIO;
1182	if (lmp_subver == RTL_ROM_LMP_8852A && hci_rev == 0x000c) {
1183		snprintf(fw_name, sizeof(fw_name), "%s_v2.bin",
1184				btrtl_dev->ic_info->fw_name);
1185		btrtl_dev->fw_len = rtl_load_file(hdev, fw_name,
1186				&btrtl_dev->fw_data);
1187	}
1188
1189	if (btrtl_dev->fw_len < 0) {
1190		snprintf(fw_name, sizeof(fw_name), "%s.bin",
1191				btrtl_dev->ic_info->fw_name);
1192		btrtl_dev->fw_len = rtl_load_file(hdev, fw_name,
1193				&btrtl_dev->fw_data);
1194	}
1195
1196	if (btrtl_dev->fw_len < 0) {
1197		rtl_dev_err(hdev, "firmware file %s not found",
1198			    btrtl_dev->ic_info->fw_name);
1199		ret = btrtl_dev->fw_len;
1200		goto err_free;
1201	}
1202
1203	if (btrtl_dev->ic_info->cfg_name) {
1204		if (postfix) {
1205			snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
1206				 btrtl_dev->ic_info->cfg_name, postfix);
1207		} else {
1208			snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
1209				 btrtl_dev->ic_info->cfg_name);
1210		}
1211		btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
1212						   &btrtl_dev->cfg_data);
1213		if (btrtl_dev->ic_info->config_needed &&
1214		    btrtl_dev->cfg_len <= 0) {
1215			rtl_dev_err(hdev, "mandatory config file %s not found",
1216				    btrtl_dev->ic_info->cfg_name);
1217			ret = btrtl_dev->cfg_len;
1218			goto err_free;
1219		}
1220	}
1221
1222	/* The following chips supports the Microsoft vendor extension,
1223	 * therefore set the corresponding VsMsftOpCode.
1224	 */
1225	if (btrtl_dev->ic_info->has_msft_ext)
1226		hci_set_msft_opcode(hdev, 0xFCF0);
1227
1228	if (btrtl_dev->ic_info)
1229		coredump_info->rtl_dump.controller = btrtl_dev->ic_info->hw_info;
1230
1231	return btrtl_dev;
1232
1233err_free:
1234	btrtl_free(btrtl_dev);
1235err_alloc:
1236	return ERR_PTR(ret);
1237}
1238EXPORT_SYMBOL_GPL(btrtl_initialize);
1239
1240int btrtl_download_firmware(struct hci_dev *hdev,
1241			    struct btrtl_device_info *btrtl_dev)
1242{
1243	int err = 0;
1244
1245	/* Match a set of subver values that correspond to stock firmware,
1246	 * which is not compatible with standard btusb.
1247	 * If matched, upload an alternative firmware that does conform to
1248	 * standard btusb. Once that firmware is uploaded, the subver changes
1249	 * to a different value.
1250	 */
1251	if (!btrtl_dev->ic_info) {
1252		rtl_dev_info(hdev, "assuming no firmware upload needed");
1253		err = 0;
1254		goto done;
1255	}
1256
1257	switch (btrtl_dev->ic_info->lmp_subver) {
1258	case RTL_ROM_LMP_8723A:
1259		err = btrtl_setup_rtl8723a(hdev, btrtl_dev);
1260		break;
1261	case RTL_ROM_LMP_8723B:
1262	case RTL_ROM_LMP_8821A:
1263	case RTL_ROM_LMP_8761A:
1264	case RTL_ROM_LMP_8822B:
1265	case RTL_ROM_LMP_8852A:
1266	case RTL_ROM_LMP_8703B:
1267	case RTL_ROM_LMP_8851B:
1268	case RTL_ROM_LMP_8922A:
1269		err = btrtl_setup_rtl8723b(hdev, btrtl_dev);
1270		break;
1271	default:
1272		rtl_dev_info(hdev, "assuming no firmware upload needed");
1273		break;
1274	}
1275
1276done:
1277	btrtl_register_devcoredump_support(hdev);
1278
1279	return err;
1280}
1281EXPORT_SYMBOL_GPL(btrtl_download_firmware);
1282
1283void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev)
1284{
1285	/* Enable controller to do both LE scan and BR/EDR inquiry
1286	 * simultaneously.
1287	 */
1288	set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1289
1290	/* Enable central-peripheral role (able to create new connections with
1291	 * an existing connection in slave role).
1292	 */
1293	/* Enable WBS supported for the specific Realtek devices. */
1294	switch (btrtl_dev->project_id) {
1295	case CHIP_ID_8822C:
1296	case CHIP_ID_8852A:
1297	case CHIP_ID_8852B:
1298	case CHIP_ID_8852C:
1299	case CHIP_ID_8851B:
1300	case CHIP_ID_8922A:
1301	case CHIP_ID_8852BT:
 
1302		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
1303
1304		/* RTL8852C needs to transmit mSBC data continuously without
1305		 * the zero length of USB packets for the ALT 6 supported chips
1306		 */
1307		if (btrtl_dev->project_id == CHIP_ID_8852C)
1308			btrealtek_set_flag(hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP);
1309
1310		if (btrtl_dev->project_id == CHIP_ID_8852A ||
1311		    btrtl_dev->project_id == CHIP_ID_8852B ||
1312		    btrtl_dev->project_id == CHIP_ID_8852C)
1313			set_bit(HCI_QUIRK_USE_MSFT_EXT_ADDRESS_FILTER, &hdev->quirks);
1314
1315		hci_set_aosp_capable(hdev);
1316		break;
1317	default:
1318		rtl_dev_dbg(hdev, "Central-peripheral role not enabled.");
1319		rtl_dev_dbg(hdev, "WBS supported not enabled.");
1320		break;
1321	}
1322
1323	if (!btrtl_dev->ic_info)
1324		return;
1325
1326	switch (btrtl_dev->ic_info->lmp_subver) {
1327	case RTL_ROM_LMP_8703B:
1328		/* 8723CS reports two pages for local ext features,
1329		 * but it doesn't support any features from page 2 -
1330		 * it either responds with garbage or with error status
1331		 */
1332		set_bit(HCI_QUIRK_BROKEN_LOCAL_EXT_FEATURES_PAGE_2,
1333			&hdev->quirks);
1334		break;
1335	default:
1336		break;
1337	}
1338}
1339EXPORT_SYMBOL_GPL(btrtl_set_quirks);
1340
1341int btrtl_setup_realtek(struct hci_dev *hdev)
1342{
1343	struct btrtl_device_info *btrtl_dev;
1344	int ret;
1345
1346	btrtl_dev = btrtl_initialize(hdev, NULL);
1347	if (IS_ERR(btrtl_dev))
1348		return PTR_ERR(btrtl_dev);
1349
1350	ret = btrtl_download_firmware(hdev, btrtl_dev);
1351
1352	btrtl_set_quirks(hdev, btrtl_dev);
1353
1354	if (btrtl_dev->ic_info) {
1355		hci_set_hw_info(hdev,
1356			"RTL lmp_subver=%u hci_rev=%u hci_ver=%u hci_bus=%u",
1357			btrtl_dev->ic_info->lmp_subver,
1358			btrtl_dev->ic_info->hci_rev,
1359			btrtl_dev->ic_info->hci_ver,
1360			btrtl_dev->ic_info->hci_bus);
1361	}
1362
1363	btrtl_free(btrtl_dev);
1364	return ret;
1365}
1366EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
1367
1368int btrtl_shutdown_realtek(struct hci_dev *hdev)
1369{
1370	struct sk_buff *skb;
1371	int ret;
1372
1373	/* According to the vendor driver, BT must be reset on close to avoid
1374	 * firmware crash.
1375	 */
1376	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
1377	if (IS_ERR(skb)) {
1378		ret = PTR_ERR(skb);
1379		bt_dev_err(hdev, "HCI reset during shutdown failed");
1380		return ret;
1381	}
1382	kfree_skb(skb);
1383
1384	return 0;
1385}
1386EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
1387
1388static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
1389{
1390	switch (device_baudrate) {
1391	case 0x0252a00a:
1392		return 230400;
1393
1394	case 0x05f75004:
1395		return 921600;
1396
1397	case 0x00005004:
1398		return 1000000;
1399
1400	case 0x04928002:
1401	case 0x01128002:
1402		return 1500000;
1403
1404	case 0x00005002:
1405		return 2000000;
1406
1407	case 0x0000b001:
1408		return 2500000;
1409
1410	case 0x04928001:
1411		return 3000000;
1412
1413	case 0x052a6001:
1414		return 3500000;
1415
1416	case 0x00005001:
1417		return 4000000;
1418
1419	case 0x0252c014:
1420	default:
1421		return 115200;
1422	}
1423}
1424
1425int btrtl_get_uart_settings(struct hci_dev *hdev,
1426			    struct btrtl_device_info *btrtl_dev,
1427			    unsigned int *controller_baudrate,
1428			    u32 *device_baudrate, bool *flow_control)
1429{
1430	struct rtl_vendor_config *config;
1431	struct rtl_vendor_config_entry *entry;
1432	int i, total_data_len;
1433	bool found = false;
1434
1435	total_data_len = btrtl_dev->cfg_len - sizeof(*config);
1436	if (total_data_len <= 0) {
1437		rtl_dev_warn(hdev, "no config loaded");
1438		return -EINVAL;
1439	}
1440
1441	config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
1442	if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
1443		rtl_dev_err(hdev, "invalid config magic");
1444		return -EINVAL;
1445	}
1446
1447	if (total_data_len < le16_to_cpu(config->total_len)) {
1448		rtl_dev_err(hdev, "config is too short");
1449		return -EINVAL;
1450	}
1451
1452	for (i = 0; i < total_data_len; ) {
1453		entry = ((void *)config->entry) + i;
1454
1455		switch (le16_to_cpu(entry->offset)) {
1456		case 0xc:
1457			if (entry->len < sizeof(*device_baudrate)) {
1458				rtl_dev_err(hdev, "invalid UART config entry");
1459				return -EINVAL;
1460			}
1461
1462			*device_baudrate = get_unaligned_le32(entry->data);
1463			*controller_baudrate = btrtl_convert_baudrate(
1464							*device_baudrate);
1465
1466			if (entry->len >= 13)
1467				*flow_control = !!(entry->data[12] & BIT(2));
1468			else
1469				*flow_control = false;
1470
1471			found = true;
1472			break;
1473
1474		default:
1475			rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)",
1476				   le16_to_cpu(entry->offset), entry->len);
1477			break;
1478		}
1479
1480		i += sizeof(*entry) + entry->len;
1481	}
1482
1483	if (!found) {
1484		rtl_dev_err(hdev, "no UART config entry found");
1485		return -ENOENT;
1486	}
1487
1488	rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate);
1489	rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate);
1490	rtl_dev_dbg(hdev, "flow control %d", *flow_control);
1491
1492	return 0;
1493}
1494EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
1495
1496MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
1497MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
1498MODULE_VERSION(VERSION);
1499MODULE_LICENSE("GPL");
1500MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
1501MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
1502MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
1503MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
1504MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
1505MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_fw.bin");
1506MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_config.bin");
1507MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_fw.bin");
1508MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_config.bin");
1509MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_fw.bin");
1510MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_config.bin");
1511MODULE_FIRMWARE("rtl_bt/rtl8723d_fw.bin");
1512MODULE_FIRMWARE("rtl_bt/rtl8723d_config.bin");
1513MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
1514MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
1515MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
1516MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
1517MODULE_FIRMWARE("rtl_bt/rtl8761b_fw.bin");
1518MODULE_FIRMWARE("rtl_bt/rtl8761b_config.bin");
1519MODULE_FIRMWARE("rtl_bt/rtl8761bu_fw.bin");
1520MODULE_FIRMWARE("rtl_bt/rtl8761bu_config.bin");
1521MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
1522MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
1523MODULE_FIRMWARE("rtl_bt/rtl8821c_fw.bin");
1524MODULE_FIRMWARE("rtl_bt/rtl8821c_config.bin");
1525MODULE_FIRMWARE("rtl_bt/rtl8821cs_fw.bin");
1526MODULE_FIRMWARE("rtl_bt/rtl8821cs_config.bin");
1527MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
1528MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
1529MODULE_FIRMWARE("rtl_bt/rtl8822cs_fw.bin");
1530MODULE_FIRMWARE("rtl_bt/rtl8822cs_config.bin");
1531MODULE_FIRMWARE("rtl_bt/rtl8822cu_fw.bin");
1532MODULE_FIRMWARE("rtl_bt/rtl8822cu_config.bin");
1533MODULE_FIRMWARE("rtl_bt/rtl8851bu_fw.bin");
1534MODULE_FIRMWARE("rtl_bt/rtl8851bu_config.bin");
1535MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin");
1536MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin");
1537MODULE_FIRMWARE("rtl_bt/rtl8852bs_fw.bin");
1538MODULE_FIRMWARE("rtl_bt/rtl8852bs_config.bin");
1539MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin");
1540MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin");
1541MODULE_FIRMWARE("rtl_bt/rtl8852btu_fw.bin");
1542MODULE_FIRMWARE("rtl_bt/rtl8852btu_config.bin");
1543MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin");
1544MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw_v2.bin");
1545MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin");
1546MODULE_FIRMWARE("rtl_bt/rtl8922au_fw.bin");
1547MODULE_FIRMWARE("rtl_bt/rtl8922au_config.bin");
v6.9.4
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Bluetooth support for Realtek devices
   4 *
   5 *  Copyright (C) 2015 Endless Mobile, Inc.
   6 */
   7
   8#include <linux/module.h>
   9#include <linux/firmware.h>
  10#include <asm/unaligned.h>
  11#include <linux/usb.h>
  12
  13#include <net/bluetooth/bluetooth.h>
  14#include <net/bluetooth/hci_core.h>
  15
  16#include "btrtl.h"
  17
  18#define VERSION "0.1"
  19
  20#define RTL_CHIP_8723CS_CG	3
  21#define RTL_CHIP_8723CS_VF	4
  22#define RTL_CHIP_8723CS_XX	5
  23#define RTL_EPATCH_SIGNATURE	"Realtech"
  24#define RTL_EPATCH_SIGNATURE_V2	"RTBTCore"
  25#define RTL_ROM_LMP_8703B	0x8703
  26#define RTL_ROM_LMP_8723A	0x1200
  27#define RTL_ROM_LMP_8723B	0x8723
  28#define RTL_ROM_LMP_8821A	0x8821
  29#define RTL_ROM_LMP_8761A	0x8761
  30#define RTL_ROM_LMP_8822B	0x8822
  31#define RTL_ROM_LMP_8852A	0x8852
  32#define RTL_ROM_LMP_8851B	0x8851
 
  33#define RTL_CONFIG_MAGIC	0x8723ab55
  34
  35#define RTL_VSC_OP_COREDUMP	0xfcff
  36
  37#define IC_MATCH_FL_LMPSUBV	(1 << 0)
  38#define IC_MATCH_FL_HCIREV	(1 << 1)
  39#define IC_MATCH_FL_HCIVER	(1 << 2)
  40#define IC_MATCH_FL_HCIBUS	(1 << 3)
  41#define IC_MATCH_FL_CHIP_TYPE	(1 << 4)
  42#define IC_INFO(lmps, hcir, hciv, bus) \
  43	.match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | \
  44		       IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS, \
  45	.lmp_subver = (lmps), \
  46	.hci_rev = (hcir), \
  47	.hci_ver = (hciv), \
  48	.hci_bus = (bus)
  49
  50#define	RTL_CHIP_SUBVER (&(struct rtl_vendor_cmd) {{0x10, 0x38, 0x04, 0x28, 0x80}})
  51#define	RTL_CHIP_REV    (&(struct rtl_vendor_cmd) {{0x10, 0x3A, 0x04, 0x28, 0x80}})
  52#define	RTL_SEC_PROJ    (&(struct rtl_vendor_cmd) {{0x10, 0xA4, 0x0D, 0x00, 0xb0}})
  53
  54#define RTL_PATCH_SNIPPETS		0x01
  55#define RTL_PATCH_DUMMY_HEADER		0x02
  56#define RTL_PATCH_SECURITY_HEADER	0x03
  57
  58enum btrtl_chip_id {
  59	CHIP_ID_8723A,
  60	CHIP_ID_8723B,
  61	CHIP_ID_8821A,
  62	CHIP_ID_8761A,
  63	CHIP_ID_8822B = 8,
  64	CHIP_ID_8723D,
  65	CHIP_ID_8821C,
  66	CHIP_ID_8822C = 13,
  67	CHIP_ID_8761B,
  68	CHIP_ID_8852A = 18,
  69	CHIP_ID_8852B = 20,
  70	CHIP_ID_8852C = 25,
  71	CHIP_ID_8851B = 36,
 
  72	CHIP_ID_8852BT = 47,
  73};
  74
  75struct id_table {
  76	__u16 match_flags;
  77	__u16 lmp_subver;
  78	__u16 hci_rev;
  79	__u8 hci_ver;
  80	__u8 hci_bus;
  81	__u8 chip_type;
  82	bool config_needed;
  83	bool has_rom_version;
  84	bool has_msft_ext;
  85	char *fw_name;
  86	char *cfg_name;
  87	char *hw_info;
  88};
  89
  90struct btrtl_device_info {
  91	const struct id_table *ic_info;
  92	u8 rom_version;
  93	u8 *fw_data;
  94	int fw_len;
  95	u8 *cfg_data;
  96	int cfg_len;
  97	bool drop_fw;
  98	int project_id;
  99	u8 key_id;
 100	struct list_head patch_subsecs;
 101};
 102
 103static const struct id_table ic_id_table[] = {
 104	/* 8723A */
 105	{ IC_INFO(RTL_ROM_LMP_8723A, 0xb, 0x6, HCI_USB),
 106	  .config_needed = false,
 107	  .has_rom_version = false,
 108	  .fw_name = "rtl_bt/rtl8723a_fw",
 109	  .cfg_name = NULL,
 110	  .hw_info = "rtl8723au" },
 111
 112	/* 8723BS */
 113	{ IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_UART),
 114	  .config_needed = true,
 115	  .has_rom_version = true,
 116	  .fw_name  = "rtl_bt/rtl8723bs_fw",
 117	  .cfg_name = "rtl_bt/rtl8723bs_config",
 118	  .hw_info  = "rtl8723bs" },
 119
 120	/* 8723B */
 121	{ IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_USB),
 122	  .config_needed = false,
 123	  .has_rom_version = true,
 124	  .fw_name  = "rtl_bt/rtl8723b_fw",
 125	  .cfg_name = "rtl_bt/rtl8723b_config",
 126	  .hw_info  = "rtl8723bu" },
 127
 128	/* 8723CS-CG */
 129	{ .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
 130			 IC_MATCH_FL_HCIBUS,
 131	  .lmp_subver = RTL_ROM_LMP_8703B,
 132	  .chip_type = RTL_CHIP_8723CS_CG,
 133	  .hci_bus = HCI_UART,
 134	  .config_needed = true,
 135	  .has_rom_version = true,
 136	  .fw_name  = "rtl_bt/rtl8723cs_cg_fw",
 137	  .cfg_name = "rtl_bt/rtl8723cs_cg_config",
 138	  .hw_info  = "rtl8723cs-cg" },
 139
 140	/* 8723CS-VF */
 141	{ .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
 142			 IC_MATCH_FL_HCIBUS,
 143	  .lmp_subver = RTL_ROM_LMP_8703B,
 144	  .chip_type = RTL_CHIP_8723CS_VF,
 145	  .hci_bus = HCI_UART,
 146	  .config_needed = true,
 147	  .has_rom_version = true,
 148	  .fw_name  = "rtl_bt/rtl8723cs_vf_fw",
 149	  .cfg_name = "rtl_bt/rtl8723cs_vf_config",
 150	  .hw_info  = "rtl8723cs-vf" },
 151
 152	/* 8723CS-XX */
 153	{ .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
 154			 IC_MATCH_FL_HCIBUS,
 155	  .lmp_subver = RTL_ROM_LMP_8703B,
 156	  .chip_type = RTL_CHIP_8723CS_XX,
 157	  .hci_bus = HCI_UART,
 158	  .config_needed = true,
 159	  .has_rom_version = true,
 160	  .fw_name  = "rtl_bt/rtl8723cs_xx_fw",
 161	  .cfg_name = "rtl_bt/rtl8723cs_xx_config",
 162	  .hw_info  = "rtl8723cs" },
 163
 164	/* 8723D */
 165	{ IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_USB),
 166	  .config_needed = true,
 167	  .has_rom_version = true,
 168	  .fw_name  = "rtl_bt/rtl8723d_fw",
 169	  .cfg_name = "rtl_bt/rtl8723d_config",
 170	  .hw_info  = "rtl8723du" },
 171
 172	/* 8723DS */
 173	{ IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_UART),
 174	  .config_needed = true,
 175	  .has_rom_version = true,
 176	  .fw_name  = "rtl_bt/rtl8723ds_fw",
 177	  .cfg_name = "rtl_bt/rtl8723ds_config",
 178	  .hw_info  = "rtl8723ds" },
 179
 180	/* 8821A */
 181	{ IC_INFO(RTL_ROM_LMP_8821A, 0xa, 0x6, HCI_USB),
 182	  .config_needed = false,
 183	  .has_rom_version = true,
 184	  .fw_name  = "rtl_bt/rtl8821a_fw",
 185	  .cfg_name = "rtl_bt/rtl8821a_config",
 186	  .hw_info  = "rtl8821au" },
 187
 188	/* 8821C */
 189	{ IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_USB),
 190	  .config_needed = false,
 191	  .has_rom_version = true,
 192	  .has_msft_ext = true,
 193	  .fw_name  = "rtl_bt/rtl8821c_fw",
 194	  .cfg_name = "rtl_bt/rtl8821c_config",
 195	  .hw_info  = "rtl8821cu" },
 196
 197	/* 8821CS */
 198	{ IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_UART),
 199	  .config_needed = true,
 200	  .has_rom_version = true,
 201	  .has_msft_ext = true,
 202	  .fw_name  = "rtl_bt/rtl8821cs_fw",
 203	  .cfg_name = "rtl_bt/rtl8821cs_config",
 204	  .hw_info  = "rtl8821cs" },
 205
 206	/* 8761A */
 207	{ IC_INFO(RTL_ROM_LMP_8761A, 0xa, 0x6, HCI_USB),
 208	  .config_needed = false,
 209	  .has_rom_version = true,
 210	  .fw_name  = "rtl_bt/rtl8761a_fw",
 211	  .cfg_name = "rtl_bt/rtl8761a_config",
 212	  .hw_info  = "rtl8761au" },
 213
 214	/* 8761B */
 215	{ IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_UART),
 216	  .config_needed = false,
 217	  .has_rom_version = true,
 218	  .has_msft_ext = true,
 219	  .fw_name  = "rtl_bt/rtl8761b_fw",
 220	  .cfg_name = "rtl_bt/rtl8761b_config",
 221	  .hw_info  = "rtl8761btv" },
 222
 223	/* 8761BU */
 224	{ IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_USB),
 225	  .config_needed = false,
 226	  .has_rom_version = true,
 227	  .fw_name  = "rtl_bt/rtl8761bu_fw",
 228	  .cfg_name = "rtl_bt/rtl8761bu_config",
 229	  .hw_info  = "rtl8761bu" },
 230
 231	/* 8822C with UART interface */
 232	{ IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0x8, HCI_UART),
 233	  .config_needed = true,
 234	  .has_rom_version = true,
 235	  .has_msft_ext = true,
 236	  .fw_name  = "rtl_bt/rtl8822cs_fw",
 237	  .cfg_name = "rtl_bt/rtl8822cs_config",
 238	  .hw_info  = "rtl8822cs" },
 239
 240	/* 8822C with UART interface */
 241	{ IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_UART),
 242	  .config_needed = true,
 243	  .has_rom_version = true,
 244	  .has_msft_ext = true,
 245	  .fw_name  = "rtl_bt/rtl8822cs_fw",
 246	  .cfg_name = "rtl_bt/rtl8822cs_config",
 247	  .hw_info  = "rtl8822cs" },
 248
 249	/* 8822C with USB interface */
 250	{ IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_USB),
 251	  .config_needed = false,
 252	  .has_rom_version = true,
 253	  .has_msft_ext = true,
 254	  .fw_name  = "rtl_bt/rtl8822cu_fw",
 255	  .cfg_name = "rtl_bt/rtl8822cu_config",
 256	  .hw_info  = "rtl8822cu" },
 257
 258	/* 8822B */
 259	{ IC_INFO(RTL_ROM_LMP_8822B, 0xb, 0x7, HCI_USB),
 260	  .config_needed = true,
 261	  .has_rom_version = true,
 262	  .has_msft_ext = true,
 263	  .fw_name  = "rtl_bt/rtl8822b_fw",
 264	  .cfg_name = "rtl_bt/rtl8822b_config",
 265	  .hw_info  = "rtl8822bu" },
 266
 267	/* 8852A */
 268	{ IC_INFO(RTL_ROM_LMP_8852A, 0xa, 0xb, HCI_USB),
 269	  .config_needed = false,
 270	  .has_rom_version = true,
 271	  .has_msft_ext = true,
 272	  .fw_name  = "rtl_bt/rtl8852au_fw",
 273	  .cfg_name = "rtl_bt/rtl8852au_config",
 274	  .hw_info  = "rtl8852au" },
 275
 276	/* 8852B with UART interface */
 277	{ IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_UART),
 278	  .config_needed = true,
 279	  .has_rom_version = true,
 280	  .has_msft_ext = true,
 281	  .fw_name  = "rtl_bt/rtl8852bs_fw",
 282	  .cfg_name = "rtl_bt/rtl8852bs_config",
 283	  .hw_info  = "rtl8852bs" },
 284
 285	/* 8852B */
 286	{ IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_USB),
 287	  .config_needed = false,
 288	  .has_rom_version = true,
 289	  .has_msft_ext = true,
 290	  .fw_name  = "rtl_bt/rtl8852bu_fw",
 291	  .cfg_name = "rtl_bt/rtl8852bu_config",
 292	  .hw_info  = "rtl8852bu" },
 293
 294	/* 8852C */
 295	{ IC_INFO(RTL_ROM_LMP_8852A, 0xc, 0xc, HCI_USB),
 296	  .config_needed = false,
 297	  .has_rom_version = true,
 298	  .has_msft_ext = true,
 299	  .fw_name  = "rtl_bt/rtl8852cu_fw",
 300	  .cfg_name = "rtl_bt/rtl8852cu_config",
 301	  .hw_info  = "rtl8852cu" },
 302
 303	/* 8851B */
 304	{ IC_INFO(RTL_ROM_LMP_8851B, 0xb, 0xc, HCI_USB),
 305	  .config_needed = false,
 306	  .has_rom_version = true,
 307	  .has_msft_ext = false,
 308	  .fw_name  = "rtl_bt/rtl8851bu_fw",
 309	  .cfg_name = "rtl_bt/rtl8851bu_config",
 310	  .hw_info  = "rtl8851bu" },
 311
 
 
 
 
 
 
 
 
 
 312	/* 8852BT/8852BE-VT */
 313	{ IC_INFO(RTL_ROM_LMP_8852A, 0x87, 0xc, HCI_USB),
 314	  .config_needed = false,
 315	  .has_rom_version = true,
 316	  .has_msft_ext = true,
 317	  .fw_name  = "rtl_bt/rtl8852btu_fw",
 318	  .cfg_name = "rtl_bt/rtl8852btu_config",
 319	  .hw_info  = "rtl8852btu" },
 320	};
 321
 322static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
 323					     u8 hci_ver, u8 hci_bus,
 324					     u8 chip_type)
 325{
 326	int i;
 327
 328	for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
 329		if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
 330		    (ic_id_table[i].lmp_subver != lmp_subver))
 331			continue;
 332		if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
 333		    (ic_id_table[i].hci_rev != hci_rev))
 334			continue;
 335		if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
 336		    (ic_id_table[i].hci_ver != hci_ver))
 337			continue;
 338		if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
 339		    (ic_id_table[i].hci_bus != hci_bus))
 340			continue;
 341		if ((ic_id_table[i].match_flags & IC_MATCH_FL_CHIP_TYPE) &&
 342		    (ic_id_table[i].chip_type != chip_type))
 343			continue;
 344
 345		break;
 346	}
 347	if (i >= ARRAY_SIZE(ic_id_table))
 348		return NULL;
 349
 350	return &ic_id_table[i];
 351}
 352
 353static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
 354{
 355	struct sk_buff *skb;
 356
 357	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
 358			     HCI_INIT_TIMEOUT);
 359	if (IS_ERR(skb)) {
 360		rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)",
 361			    PTR_ERR(skb));
 362		return skb;
 363	}
 364
 365	if (skb->len != sizeof(struct hci_rp_read_local_version)) {
 366		rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch");
 367		kfree_skb(skb);
 368		return ERR_PTR(-EIO);
 369	}
 370
 371	return skb;
 372}
 373
 374static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
 375{
 376	struct rtl_rom_version_evt *rom_version;
 377	struct sk_buff *skb;
 378
 379	/* Read RTL ROM version command */
 380	skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
 381	if (IS_ERR(skb)) {
 382		rtl_dev_err(hdev, "Read ROM version failed (%ld)",
 383			    PTR_ERR(skb));
 384		return PTR_ERR(skb);
 385	}
 386
 387	if (skb->len != sizeof(*rom_version)) {
 388		rtl_dev_err(hdev, "version event length mismatch");
 389		kfree_skb(skb);
 390		return -EIO;
 391	}
 392
 393	rom_version = (struct rtl_rom_version_evt *)skb->data;
 394	rtl_dev_info(hdev, "rom_version status=%x version=%x",
 395		     rom_version->status, rom_version->version);
 396
 397	*version = rom_version->version;
 398
 399	kfree_skb(skb);
 400	return 0;
 401}
 402
 403static int btrtl_vendor_read_reg16(struct hci_dev *hdev,
 404				   struct rtl_vendor_cmd *cmd, u8 *rp)
 405{
 406	struct sk_buff *skb;
 407	int err = 0;
 408
 409	skb = __hci_cmd_sync(hdev, 0xfc61, sizeof(*cmd), cmd,
 410			     HCI_INIT_TIMEOUT);
 411	if (IS_ERR(skb)) {
 412		err = PTR_ERR(skb);
 413		rtl_dev_err(hdev, "RTL: Read reg16 failed (%d)", err);
 414		return err;
 415	}
 416
 417	if (skb->len != 3 || skb->data[0]) {
 418		bt_dev_err(hdev, "RTL: Read reg16 length mismatch");
 419		kfree_skb(skb);
 420		return -EIO;
 421	}
 422
 423	if (rp)
 424		memcpy(rp, skb->data + 1, 2);
 425
 426	kfree_skb(skb);
 427
 428	return 0;
 429}
 430
 431static void *rtl_iov_pull_data(struct rtl_iovec *iov, u32 len)
 432{
 433	void *data = iov->data;
 434
 435	if (iov->len < len)
 436		return NULL;
 437
 438	iov->data += len;
 439	iov->len  -= len;
 440
 441	return data;
 442}
 443
 444static void btrtl_insert_ordered_subsec(struct rtl_subsection *node,
 445					struct btrtl_device_info *btrtl_dev)
 446{
 447	struct list_head *pos;
 448	struct list_head *next;
 449	struct rtl_subsection *subsec;
 450
 451	list_for_each_safe(pos, next, &btrtl_dev->patch_subsecs) {
 452		subsec = list_entry(pos, struct rtl_subsection, list);
 453		if (subsec->prio >= node->prio)
 454			break;
 455	}
 456	__list_add(&node->list, pos->prev, pos);
 457}
 458
 459static int btrtl_parse_section(struct hci_dev *hdev,
 460			       struct btrtl_device_info *btrtl_dev, u32 opcode,
 461			       u8 *data, u32 len)
 462{
 463	struct rtl_section_hdr *hdr;
 464	struct rtl_subsection *subsec;
 465	struct rtl_common_subsec *common_subsec;
 466	struct rtl_sec_hdr *sec_hdr;
 467	int i;
 468	u8 *ptr;
 469	u16 num_subsecs;
 470	u32 subsec_len;
 471	int rc = 0;
 472	struct rtl_iovec iov = {
 473		.data = data,
 474		.len  = len,
 475	};
 476
 477	hdr = rtl_iov_pull_data(&iov, sizeof(*hdr));
 478	if (!hdr)
 479		return -EINVAL;
 480	num_subsecs = le16_to_cpu(hdr->num);
 481
 482	for (i = 0; i < num_subsecs; i++) {
 483		common_subsec = rtl_iov_pull_data(&iov, sizeof(*common_subsec));
 484		if (!common_subsec)
 485			break;
 486		subsec_len = le32_to_cpu(common_subsec->len);
 487
 488		rtl_dev_dbg(hdev, "subsec, eco 0x%02x, len %08x",
 489			    common_subsec->eco, subsec_len);
 490
 491		ptr = rtl_iov_pull_data(&iov, subsec_len);
 492		if (!ptr)
 493			break;
 494
 495		if (common_subsec->eco != btrtl_dev->rom_version + 1)
 496			continue;
 497
 498		switch (opcode) {
 499		case RTL_PATCH_SECURITY_HEADER:
 500			sec_hdr = (void *)common_subsec;
 501			if (sec_hdr->key_id != btrtl_dev->key_id)
 502				continue;
 503			break;
 504		}
 505
 506		subsec = kzalloc(sizeof(*subsec), GFP_KERNEL);
 507		if (!subsec)
 508			return -ENOMEM;
 509		subsec->opcode = opcode;
 510		subsec->prio = common_subsec->prio;
 511		subsec->len  = subsec_len;
 512		subsec->data = ptr;
 513		btrtl_insert_ordered_subsec(subsec, btrtl_dev);
 514		rc  += subsec_len;
 515	}
 516
 517	return rc;
 518}
 519
 520static int rtlbt_parse_firmware_v2(struct hci_dev *hdev,
 521				   struct btrtl_device_info *btrtl_dev,
 522				   unsigned char **_buf)
 523{
 524	struct rtl_epatch_header_v2 *hdr;
 525	int rc;
 526	u8 reg_val[2];
 527	u8 key_id;
 528	u32 num_sections;
 529	struct rtl_section *section;
 530	struct rtl_subsection *entry, *tmp;
 531	u32 section_len;
 532	u32 opcode;
 533	int len = 0;
 534	int i;
 535	u8 *ptr;
 536	struct rtl_iovec iov = {
 537		.data = btrtl_dev->fw_data,
 538		.len  = btrtl_dev->fw_len - 7, /* Cut the tail */
 539	};
 540
 541	rc = btrtl_vendor_read_reg16(hdev, RTL_SEC_PROJ, reg_val);
 542	if (rc < 0)
 543		return -EIO;
 544	key_id = reg_val[0];
 545
 546	rtl_dev_dbg(hdev, "%s: key id %u", __func__, key_id);
 547
 548	btrtl_dev->key_id = key_id;
 549
 550	hdr = rtl_iov_pull_data(&iov, sizeof(*hdr));
 551	if (!hdr)
 552		return -EINVAL;
 553	num_sections = le32_to_cpu(hdr->num_sections);
 554
 555	rtl_dev_dbg(hdev, "FW version %08x-%08x", *((u32 *)hdr->fw_version),
 556		    *((u32 *)(hdr->fw_version + 4)));
 557
 558	for (i = 0; i < num_sections; i++) {
 559		section = rtl_iov_pull_data(&iov, sizeof(*section));
 560		if (!section)
 561			break;
 562		section_len = le32_to_cpu(section->len);
 563		opcode      = le32_to_cpu(section->opcode);
 564
 565		rtl_dev_dbg(hdev, "opcode 0x%04x", section->opcode);
 566
 567		ptr = rtl_iov_pull_data(&iov, section_len);
 568		if (!ptr)
 569			break;
 570
 571		switch (opcode) {
 572		case RTL_PATCH_SNIPPETS:
 573			rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
 574						 ptr, section_len);
 575			break;
 576		case RTL_PATCH_SECURITY_HEADER:
 577			/* If key_id from chip is zero, ignore all security
 578			 * headers.
 579			 */
 580			if (!key_id)
 581				break;
 582			rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
 583						 ptr, section_len);
 584			break;
 585		case RTL_PATCH_DUMMY_HEADER:
 586			rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
 587						 ptr, section_len);
 588			break;
 589		default:
 590			rc = 0;
 591			break;
 592		}
 593		if (rc < 0) {
 594			rtl_dev_err(hdev, "RTL: Parse section (%u) err %d",
 595				    opcode, rc);
 596			return rc;
 597		}
 598		len += rc;
 599	}
 600
 601	if (!len)
 602		return -ENODATA;
 603
 604	/* Allocate mem and copy all found subsecs. */
 605	ptr = kvmalloc(len, GFP_KERNEL);
 606	if (!ptr)
 607		return -ENOMEM;
 608
 609	len = 0;
 610	list_for_each_entry_safe(entry, tmp, &btrtl_dev->patch_subsecs, list) {
 611		rtl_dev_dbg(hdev, "RTL: opcode %08x, addr %p, len 0x%x",
 612			    entry->opcode, entry->data, entry->len);
 613		memcpy(ptr + len, entry->data, entry->len);
 614		len += entry->len;
 615	}
 616
 617	if (!len)
 618		return -EPERM;
 619
 620	*_buf = ptr;
 621	return len;
 622}
 623
 624static int rtlbt_parse_firmware(struct hci_dev *hdev,
 625				struct btrtl_device_info *btrtl_dev,
 626				unsigned char **_buf)
 627{
 628	static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
 629	struct btrealtek_data *coredump_info = hci_get_priv(hdev);
 630	struct rtl_epatch_header *epatch_info;
 631	unsigned char *buf;
 632	int i, len;
 633	size_t min_size;
 634	u8 opcode, length, data;
 635	int project_id = -1;
 636	const unsigned char *fwptr, *chip_id_base;
 637	const unsigned char *patch_length_base, *patch_offset_base;
 638	u32 patch_offset = 0;
 639	u16 patch_length, num_patches;
 640	static const struct {
 641		__u16 lmp_subver;
 642		__u8 id;
 643	} project_id_to_lmp_subver[] = {
 644		{ RTL_ROM_LMP_8723A, 0 },
 645		{ RTL_ROM_LMP_8723B, 1 },
 646		{ RTL_ROM_LMP_8821A, 2 },
 647		{ RTL_ROM_LMP_8761A, 3 },
 648		{ RTL_ROM_LMP_8703B, 7 },
 649		{ RTL_ROM_LMP_8822B, 8 },
 650		{ RTL_ROM_LMP_8723B, 9 },	/* 8723D */
 651		{ RTL_ROM_LMP_8821A, 10 },	/* 8821C */
 652		{ RTL_ROM_LMP_8822B, 13 },	/* 8822C */
 653		{ RTL_ROM_LMP_8761A, 14 },	/* 8761B */
 654		{ RTL_ROM_LMP_8852A, 18 },	/* 8852A */
 655		{ RTL_ROM_LMP_8852A, 20 },	/* 8852B */
 656		{ RTL_ROM_LMP_8852A, 25 },	/* 8852C */
 657		{ RTL_ROM_LMP_8851B, 36 },	/* 8851B */
 
 658		{ RTL_ROM_LMP_8852A, 47 },	/* 8852BT */
 659	};
 660
 661	if (btrtl_dev->fw_len <= 8)
 662		return -EINVAL;
 663
 664	if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8))
 665		min_size = sizeof(struct rtl_epatch_header) +
 666				sizeof(extension_sig) + 3;
 667	else if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE_V2, 8))
 668		min_size = sizeof(struct rtl_epatch_header_v2) +
 669				sizeof(extension_sig) + 3;
 670	else
 671		return -EINVAL;
 672
 673	if (btrtl_dev->fw_len < min_size)
 674		return -EINVAL;
 675
 676	fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
 677	if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
 678		rtl_dev_err(hdev, "extension section signature mismatch");
 679		return -EINVAL;
 680	}
 681
 682	/* Loop from the end of the firmware parsing instructions, until
 683	 * we find an instruction that identifies the "project ID" for the
 684	 * hardware supported by this firwmare file.
 685	 * Once we have that, we double-check that project_id is suitable
 686	 * for the hardware we are working with.
 687	 */
 688	while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
 689		opcode = *--fwptr;
 690		length = *--fwptr;
 691		data = *--fwptr;
 692
 693		BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
 694
 695		if (opcode == 0xff) /* EOF */
 696			break;
 697
 698		if (length == 0) {
 699			rtl_dev_err(hdev, "found instruction with length 0");
 700			return -EINVAL;
 701		}
 702
 703		if (opcode == 0 && length == 1) {
 704			project_id = data;
 705			break;
 706		}
 707
 708		fwptr -= length;
 709	}
 710
 711	if (project_id < 0) {
 712		rtl_dev_err(hdev, "failed to find version instruction");
 713		return -EINVAL;
 714	}
 715
 716	/* Find project_id in table */
 717	for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
 718		if (project_id == project_id_to_lmp_subver[i].id) {
 719			btrtl_dev->project_id = project_id;
 720			break;
 721		}
 722	}
 723
 724	if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
 725		rtl_dev_err(hdev, "unknown project id %d", project_id);
 726		return -EINVAL;
 727	}
 728
 729	if (btrtl_dev->ic_info->lmp_subver !=
 730				project_id_to_lmp_subver[i].lmp_subver) {
 731		rtl_dev_err(hdev, "firmware is for %x but this is a %x",
 732			    project_id_to_lmp_subver[i].lmp_subver,
 733			    btrtl_dev->ic_info->lmp_subver);
 734		return -EINVAL;
 735	}
 736
 737	if (memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8) != 0) {
 738		if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE_V2, 8))
 739			return rtlbt_parse_firmware_v2(hdev, btrtl_dev, _buf);
 740		rtl_dev_err(hdev, "bad EPATCH signature");
 741		return -EINVAL;
 742	}
 743
 744	epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
 745	num_patches = le16_to_cpu(epatch_info->num_patches);
 746
 747	BT_DBG("fw_version=%x, num_patches=%d",
 748	       le32_to_cpu(epatch_info->fw_version), num_patches);
 749	coredump_info->rtl_dump.fw_version = le32_to_cpu(epatch_info->fw_version);
 750
 751	/* After the rtl_epatch_header there is a funky patch metadata section.
 752	 * Assuming 2 patches, the layout is:
 753	 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
 754	 *
 755	 * Find the right patch for this chip.
 756	 */
 757	min_size += 8 * num_patches;
 758	if (btrtl_dev->fw_len < min_size)
 759		return -EINVAL;
 760
 761	chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
 762	patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
 763	patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
 764	for (i = 0; i < num_patches; i++) {
 765		u16 chip_id = get_unaligned_le16(chip_id_base +
 766						 (i * sizeof(u16)));
 767		if (chip_id == btrtl_dev->rom_version + 1) {
 768			patch_length = get_unaligned_le16(patch_length_base +
 769							  (i * sizeof(u16)));
 770			patch_offset = get_unaligned_le32(patch_offset_base +
 771							  (i * sizeof(u32)));
 772			break;
 773		}
 774	}
 775
 776	if (!patch_offset) {
 777		rtl_dev_err(hdev, "didn't find patch for chip id %d",
 778			    btrtl_dev->rom_version);
 779		return -EINVAL;
 780	}
 781
 782	BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
 783	min_size = patch_offset + patch_length;
 784	if (btrtl_dev->fw_len < min_size)
 785		return -EINVAL;
 786
 787	/* Copy the firmware into a new buffer and write the version at
 788	 * the end.
 789	 */
 790	len = patch_length;
 791	buf = kvmalloc(patch_length, GFP_KERNEL);
 792	if (!buf)
 793		return -ENOMEM;
 794
 795	memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
 796	memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
 797
 798	*_buf = buf;
 799	return len;
 800}
 801
 802static int rtl_download_firmware(struct hci_dev *hdev,
 803				 const unsigned char *data, int fw_len)
 804{
 805	struct rtl_download_cmd *dl_cmd;
 806	int frag_num = fw_len / RTL_FRAG_LEN + 1;
 807	int frag_len = RTL_FRAG_LEN;
 808	int ret = 0;
 809	int i;
 810	int j = 0;
 811	struct sk_buff *skb;
 812	struct hci_rp_read_local_version *rp;
 813
 814	dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
 815	if (!dl_cmd)
 816		return -ENOMEM;
 817
 818	for (i = 0; i < frag_num; i++) {
 819		struct sk_buff *skb;
 820
 821		dl_cmd->index = j++;
 822		if (dl_cmd->index == 0x7f)
 823			j = 1;
 824
 825		if (i == (frag_num - 1)) {
 826			dl_cmd->index |= 0x80; /* data end */
 827			frag_len = fw_len % RTL_FRAG_LEN;
 828		}
 829		rtl_dev_dbg(hdev, "download fw (%d/%d). index = %d", i,
 830				frag_num, dl_cmd->index);
 831		memcpy(dl_cmd->data, data, frag_len);
 832
 833		/* Send download command */
 834		skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
 835				     HCI_INIT_TIMEOUT);
 836		if (IS_ERR(skb)) {
 837			rtl_dev_err(hdev, "download fw command failed (%ld)",
 838				    PTR_ERR(skb));
 839			ret = PTR_ERR(skb);
 840			goto out;
 841		}
 842
 843		if (skb->len != sizeof(struct rtl_download_response)) {
 844			rtl_dev_err(hdev, "download fw event length mismatch");
 845			kfree_skb(skb);
 846			ret = -EIO;
 847			goto out;
 848		}
 849
 850		kfree_skb(skb);
 851		data += RTL_FRAG_LEN;
 852	}
 853
 854	skb = btrtl_read_local_version(hdev);
 855	if (IS_ERR(skb)) {
 856		ret = PTR_ERR(skb);
 857		rtl_dev_err(hdev, "read local version failed");
 858		goto out;
 859	}
 860
 861	rp = (struct hci_rp_read_local_version *)skb->data;
 862	rtl_dev_info(hdev, "fw version 0x%04x%04x",
 863		     __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver));
 864	kfree_skb(skb);
 865
 866out:
 867	kfree(dl_cmd);
 868	return ret;
 869}
 870
 871static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
 872{
 873	const struct firmware *fw;
 874	int ret;
 875
 876	rtl_dev_info(hdev, "loading %s", name);
 877	ret = request_firmware(&fw, name, &hdev->dev);
 878	if (ret < 0)
 879		return ret;
 880	ret = fw->size;
 881	*buff = kvmalloc(fw->size, GFP_KERNEL);
 882	if (*buff)
 883		memcpy(*buff, fw->data, ret);
 884	else
 885		ret = -ENOMEM;
 886
 887	release_firmware(fw);
 888
 889	return ret;
 890}
 891
 892static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
 893				struct btrtl_device_info *btrtl_dev)
 894{
 895	if (btrtl_dev->fw_len < 8)
 896		return -EINVAL;
 897
 898	/* Check that the firmware doesn't have the epatch signature
 899	 * (which is only for RTL8723B and newer).
 900	 */
 901	if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
 902		rtl_dev_err(hdev, "unexpected EPATCH signature!");
 903		return -EINVAL;
 904	}
 905
 906	return rtl_download_firmware(hdev, btrtl_dev->fw_data,
 907				     btrtl_dev->fw_len);
 908}
 909
 910static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
 911				struct btrtl_device_info *btrtl_dev)
 912{
 913	unsigned char *fw_data = NULL;
 914	int ret;
 915	u8 *tbuff;
 916
 917	ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
 918	if (ret < 0)
 919		goto out;
 920
 921	if (btrtl_dev->cfg_len > 0) {
 922		tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
 923		if (!tbuff) {
 924			ret = -ENOMEM;
 925			goto out;
 926		}
 927
 928		memcpy(tbuff, fw_data, ret);
 929		kvfree(fw_data);
 930
 931		memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
 932		ret += btrtl_dev->cfg_len;
 933
 934		fw_data = tbuff;
 935	}
 936
 937	rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret);
 938
 939	ret = rtl_download_firmware(hdev, fw_data, ret);
 940
 941out:
 942	kvfree(fw_data);
 943	return ret;
 944}
 945
 946static void btrtl_coredump(struct hci_dev *hdev)
 947{
 948	static const u8 param[] = { 0x00, 0x00 };
 949
 950	__hci_cmd_send(hdev, RTL_VSC_OP_COREDUMP, sizeof(param), param);
 951}
 952
 953static void btrtl_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
 954{
 955	struct btrealtek_data *coredump_info = hci_get_priv(hdev);
 956	char buf[80];
 957
 958	if (coredump_info->rtl_dump.controller)
 959		snprintf(buf, sizeof(buf), "Controller Name: %s\n",
 960			 coredump_info->rtl_dump.controller);
 961	else
 962		snprintf(buf, sizeof(buf), "Controller Name: Unknown\n");
 963	skb_put_data(skb, buf, strlen(buf));
 964
 965	snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
 966		 coredump_info->rtl_dump.fw_version);
 967	skb_put_data(skb, buf, strlen(buf));
 968
 969	snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info->rtl_dump.driver_name);
 970	skb_put_data(skb, buf, strlen(buf));
 971
 972	snprintf(buf, sizeof(buf), "Vendor: Realtek\n");
 973	skb_put_data(skb, buf, strlen(buf));
 974}
 975
 976static void btrtl_register_devcoredump_support(struct hci_dev *hdev)
 977{
 978	hci_devcd_register(hdev, btrtl_coredump, btrtl_dmp_hdr, NULL);
 979
 980}
 981
 982void btrtl_set_driver_name(struct hci_dev *hdev, const char *driver_name)
 983{
 984	struct btrealtek_data *coredump_info = hci_get_priv(hdev);
 985
 986	coredump_info->rtl_dump.driver_name = driver_name;
 987}
 988EXPORT_SYMBOL_GPL(btrtl_set_driver_name);
 989
 990static bool rtl_has_chip_type(u16 lmp_subver)
 991{
 992	switch (lmp_subver) {
 993	case RTL_ROM_LMP_8703B:
 994		return true;
 995	default:
 996		break;
 997	}
 998
 999	return  false;
1000}
1001
1002static int rtl_read_chip_type(struct hci_dev *hdev, u8 *type)
1003{
1004	struct rtl_chip_type_evt *chip_type;
1005	struct sk_buff *skb;
1006	const unsigned char cmd_buf[] = {0x00, 0x94, 0xa0, 0x00, 0xb0};
1007
1008	/* Read RTL chip type command */
1009	skb = __hci_cmd_sync(hdev, 0xfc61, 5, cmd_buf, HCI_INIT_TIMEOUT);
1010	if (IS_ERR(skb)) {
1011		rtl_dev_err(hdev, "Read chip type failed (%ld)",
1012			    PTR_ERR(skb));
1013		return PTR_ERR(skb);
1014	}
1015
1016	chip_type = skb_pull_data(skb, sizeof(*chip_type));
1017	if (!chip_type) {
1018		rtl_dev_err(hdev, "RTL chip type event length mismatch");
1019		kfree_skb(skb);
1020		return -EIO;
1021	}
1022
1023	rtl_dev_info(hdev, "chip_type status=%x type=%x",
1024		     chip_type->status, chip_type->type);
1025
1026	*type = chip_type->type & 0x0f;
1027
1028	kfree_skb(skb);
1029	return 0;
1030}
1031
1032void btrtl_free(struct btrtl_device_info *btrtl_dev)
1033{
1034	struct rtl_subsection *entry, *tmp;
1035
1036	kvfree(btrtl_dev->fw_data);
1037	kvfree(btrtl_dev->cfg_data);
1038
1039	list_for_each_entry_safe(entry, tmp, &btrtl_dev->patch_subsecs, list) {
1040		list_del(&entry->list);
1041		kfree(entry);
1042	}
1043
1044	kfree(btrtl_dev);
1045}
1046EXPORT_SYMBOL_GPL(btrtl_free);
1047
1048struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
1049					   const char *postfix)
1050{
1051	struct btrealtek_data *coredump_info = hci_get_priv(hdev);
1052	struct btrtl_device_info *btrtl_dev;
1053	struct sk_buff *skb;
1054	struct hci_rp_read_local_version *resp;
1055	struct hci_command_hdr *cmd;
1056	char fw_name[40];
1057	char cfg_name[40];
1058	u16 hci_rev, lmp_subver;
1059	u8 hci_ver, lmp_ver, chip_type = 0;
1060	int ret;
1061	u8 reg_val[2];
1062
1063	btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
1064	if (!btrtl_dev) {
1065		ret = -ENOMEM;
1066		goto err_alloc;
1067	}
1068
1069	INIT_LIST_HEAD(&btrtl_dev->patch_subsecs);
1070
1071check_version:
1072	ret = btrtl_vendor_read_reg16(hdev, RTL_CHIP_SUBVER, reg_val);
1073	if (ret < 0)
1074		goto err_free;
1075	lmp_subver = get_unaligned_le16(reg_val);
1076
1077	if (lmp_subver == RTL_ROM_LMP_8822B) {
1078		ret = btrtl_vendor_read_reg16(hdev, RTL_CHIP_REV, reg_val);
1079		if (ret < 0)
1080			goto err_free;
1081		hci_rev = get_unaligned_le16(reg_val);
1082
1083		/* 8822E */
1084		if (hci_rev == 0x000e) {
1085			hci_ver = 0x0c;
1086			lmp_ver = 0x0c;
1087			btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev,
1088							    hci_ver, hdev->bus,
1089							    chip_type);
1090			goto next;
1091		}
1092	}
1093
1094	skb = btrtl_read_local_version(hdev);
1095	if (IS_ERR(skb)) {
1096		ret = PTR_ERR(skb);
1097		goto err_free;
1098	}
1099
1100	resp = (struct hci_rp_read_local_version *)skb->data;
1101
1102	hci_ver    = resp->hci_ver;
1103	hci_rev    = le16_to_cpu(resp->hci_rev);
1104	lmp_ver    = resp->lmp_ver;
1105	lmp_subver = le16_to_cpu(resp->lmp_subver);
1106
1107	kfree_skb(skb);
1108
1109	if (rtl_has_chip_type(lmp_subver)) {
1110		ret = rtl_read_chip_type(hdev, &chip_type);
1111		if (ret)
1112			goto err_free;
1113	}
1114
1115	btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
1116					    hdev->bus, chip_type);
1117
1118next:
1119	rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
1120		     hci_ver, hci_rev,
1121		     lmp_ver, lmp_subver);
1122
1123	if (!btrtl_dev->ic_info && !btrtl_dev->drop_fw)
1124		btrtl_dev->drop_fw = true;
1125	else
1126		btrtl_dev->drop_fw = false;
1127
1128	if (btrtl_dev->drop_fw) {
1129		skb = bt_skb_alloc(sizeof(*cmd), GFP_KERNEL);
1130		if (!skb)
1131			goto err_free;
1132
1133		cmd = skb_put(skb, HCI_COMMAND_HDR_SIZE);
1134		cmd->opcode = cpu_to_le16(0xfc66);
1135		cmd->plen = 0;
1136
1137		hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
1138
1139		ret = hdev->send(hdev, skb);
1140		if (ret < 0) {
1141			bt_dev_err(hdev, "sending frame failed (%d)", ret);
1142			kfree_skb(skb);
1143			goto err_free;
1144		}
1145
1146		/* Ensure the above vendor command is sent to controller and
1147		 * process has done.
1148		 */
1149		msleep(200);
1150
1151		goto check_version;
1152	}
1153
1154	if (!btrtl_dev->ic_info) {
1155		rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
1156			    lmp_subver, hci_rev, hci_ver);
1157		return btrtl_dev;
1158	}
1159
1160	if (btrtl_dev->ic_info->has_rom_version) {
1161		ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
1162		if (ret)
1163			goto err_free;
1164	}
1165
1166	if (!btrtl_dev->ic_info->fw_name) {
1167		ret = -ENOMEM;
1168		goto err_free;
1169	}
1170
1171	btrtl_dev->fw_len = -EIO;
1172	if (lmp_subver == RTL_ROM_LMP_8852A && hci_rev == 0x000c) {
1173		snprintf(fw_name, sizeof(fw_name), "%s_v2.bin",
1174				btrtl_dev->ic_info->fw_name);
1175		btrtl_dev->fw_len = rtl_load_file(hdev, fw_name,
1176				&btrtl_dev->fw_data);
1177	}
1178
1179	if (btrtl_dev->fw_len < 0) {
1180		snprintf(fw_name, sizeof(fw_name), "%s.bin",
1181				btrtl_dev->ic_info->fw_name);
1182		btrtl_dev->fw_len = rtl_load_file(hdev, fw_name,
1183				&btrtl_dev->fw_data);
1184	}
1185
1186	if (btrtl_dev->fw_len < 0) {
1187		rtl_dev_err(hdev, "firmware file %s not found",
1188			    btrtl_dev->ic_info->fw_name);
1189		ret = btrtl_dev->fw_len;
1190		goto err_free;
1191	}
1192
1193	if (btrtl_dev->ic_info->cfg_name) {
1194		if (postfix) {
1195			snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
1196				 btrtl_dev->ic_info->cfg_name, postfix);
1197		} else {
1198			snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
1199				 btrtl_dev->ic_info->cfg_name);
1200		}
1201		btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
1202						   &btrtl_dev->cfg_data);
1203		if (btrtl_dev->ic_info->config_needed &&
1204		    btrtl_dev->cfg_len <= 0) {
1205			rtl_dev_err(hdev, "mandatory config file %s not found",
1206				    btrtl_dev->ic_info->cfg_name);
1207			ret = btrtl_dev->cfg_len;
1208			goto err_free;
1209		}
1210	}
1211
1212	/* The following chips supports the Microsoft vendor extension,
1213	 * therefore set the corresponding VsMsftOpCode.
1214	 */
1215	if (btrtl_dev->ic_info->has_msft_ext)
1216		hci_set_msft_opcode(hdev, 0xFCF0);
1217
1218	if (btrtl_dev->ic_info)
1219		coredump_info->rtl_dump.controller = btrtl_dev->ic_info->hw_info;
1220
1221	return btrtl_dev;
1222
1223err_free:
1224	btrtl_free(btrtl_dev);
1225err_alloc:
1226	return ERR_PTR(ret);
1227}
1228EXPORT_SYMBOL_GPL(btrtl_initialize);
1229
1230int btrtl_download_firmware(struct hci_dev *hdev,
1231			    struct btrtl_device_info *btrtl_dev)
1232{
1233	int err = 0;
1234
1235	/* Match a set of subver values that correspond to stock firmware,
1236	 * which is not compatible with standard btusb.
1237	 * If matched, upload an alternative firmware that does conform to
1238	 * standard btusb. Once that firmware is uploaded, the subver changes
1239	 * to a different value.
1240	 */
1241	if (!btrtl_dev->ic_info) {
1242		rtl_dev_info(hdev, "assuming no firmware upload needed");
1243		err = 0;
1244		goto done;
1245	}
1246
1247	switch (btrtl_dev->ic_info->lmp_subver) {
1248	case RTL_ROM_LMP_8723A:
1249		err = btrtl_setup_rtl8723a(hdev, btrtl_dev);
1250		break;
1251	case RTL_ROM_LMP_8723B:
1252	case RTL_ROM_LMP_8821A:
1253	case RTL_ROM_LMP_8761A:
1254	case RTL_ROM_LMP_8822B:
1255	case RTL_ROM_LMP_8852A:
1256	case RTL_ROM_LMP_8703B:
1257	case RTL_ROM_LMP_8851B:
 
1258		err = btrtl_setup_rtl8723b(hdev, btrtl_dev);
1259		break;
1260	default:
1261		rtl_dev_info(hdev, "assuming no firmware upload needed");
1262		break;
1263	}
1264
1265done:
1266	btrtl_register_devcoredump_support(hdev);
1267
1268	return err;
1269}
1270EXPORT_SYMBOL_GPL(btrtl_download_firmware);
1271
1272void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev)
1273{
1274	/* Enable controller to do both LE scan and BR/EDR inquiry
1275	 * simultaneously.
1276	 */
1277	set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
1278
1279	/* Enable central-peripheral role (able to create new connections with
1280	 * an existing connection in slave role).
1281	 */
1282	/* Enable WBS supported for the specific Realtek devices. */
1283	switch (btrtl_dev->project_id) {
1284	case CHIP_ID_8822C:
1285	case CHIP_ID_8852A:
1286	case CHIP_ID_8852B:
1287	case CHIP_ID_8852C:
1288	case CHIP_ID_8851B:
 
1289	case CHIP_ID_8852BT:
1290		set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
1291		set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
1292
1293		/* RTL8852C needs to transmit mSBC data continuously without
1294		 * the zero length of USB packets for the ALT 6 supported chips
1295		 */
1296		if (btrtl_dev->project_id == CHIP_ID_8852C)
1297			btrealtek_set_flag(hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP);
1298
1299		if (btrtl_dev->project_id == CHIP_ID_8852A ||
 
1300		    btrtl_dev->project_id == CHIP_ID_8852C)
1301			set_bit(HCI_QUIRK_USE_MSFT_EXT_ADDRESS_FILTER, &hdev->quirks);
1302
1303		hci_set_aosp_capable(hdev);
1304		break;
1305	default:
1306		rtl_dev_dbg(hdev, "Central-peripheral role not enabled.");
1307		rtl_dev_dbg(hdev, "WBS supported not enabled.");
1308		break;
1309	}
1310
1311	if (!btrtl_dev->ic_info)
1312		return;
1313
1314	switch (btrtl_dev->ic_info->lmp_subver) {
1315	case RTL_ROM_LMP_8703B:
1316		/* 8723CS reports two pages for local ext features,
1317		 * but it doesn't support any features from page 2 -
1318		 * it either responds with garbage or with error status
1319		 */
1320		set_bit(HCI_QUIRK_BROKEN_LOCAL_EXT_FEATURES_PAGE_2,
1321			&hdev->quirks);
1322		break;
1323	default:
1324		break;
1325	}
1326}
1327EXPORT_SYMBOL_GPL(btrtl_set_quirks);
1328
1329int btrtl_setup_realtek(struct hci_dev *hdev)
1330{
1331	struct btrtl_device_info *btrtl_dev;
1332	int ret;
1333
1334	btrtl_dev = btrtl_initialize(hdev, NULL);
1335	if (IS_ERR(btrtl_dev))
1336		return PTR_ERR(btrtl_dev);
1337
1338	ret = btrtl_download_firmware(hdev, btrtl_dev);
1339
1340	btrtl_set_quirks(hdev, btrtl_dev);
1341
 
 
 
 
 
 
 
 
 
1342	btrtl_free(btrtl_dev);
1343	return ret;
1344}
1345EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
1346
1347int btrtl_shutdown_realtek(struct hci_dev *hdev)
1348{
1349	struct sk_buff *skb;
1350	int ret;
1351
1352	/* According to the vendor driver, BT must be reset on close to avoid
1353	 * firmware crash.
1354	 */
1355	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1356	if (IS_ERR(skb)) {
1357		ret = PTR_ERR(skb);
1358		bt_dev_err(hdev, "HCI reset during shutdown failed");
1359		return ret;
1360	}
1361	kfree_skb(skb);
1362
1363	return 0;
1364}
1365EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
1366
1367static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
1368{
1369	switch (device_baudrate) {
1370	case 0x0252a00a:
1371		return 230400;
1372
1373	case 0x05f75004:
1374		return 921600;
1375
1376	case 0x00005004:
1377		return 1000000;
1378
1379	case 0x04928002:
1380	case 0x01128002:
1381		return 1500000;
1382
1383	case 0x00005002:
1384		return 2000000;
1385
1386	case 0x0000b001:
1387		return 2500000;
1388
1389	case 0x04928001:
1390		return 3000000;
1391
1392	case 0x052a6001:
1393		return 3500000;
1394
1395	case 0x00005001:
1396		return 4000000;
1397
1398	case 0x0252c014:
1399	default:
1400		return 115200;
1401	}
1402}
1403
1404int btrtl_get_uart_settings(struct hci_dev *hdev,
1405			    struct btrtl_device_info *btrtl_dev,
1406			    unsigned int *controller_baudrate,
1407			    u32 *device_baudrate, bool *flow_control)
1408{
1409	struct rtl_vendor_config *config;
1410	struct rtl_vendor_config_entry *entry;
1411	int i, total_data_len;
1412	bool found = false;
1413
1414	total_data_len = btrtl_dev->cfg_len - sizeof(*config);
1415	if (total_data_len <= 0) {
1416		rtl_dev_warn(hdev, "no config loaded");
1417		return -EINVAL;
1418	}
1419
1420	config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
1421	if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
1422		rtl_dev_err(hdev, "invalid config magic");
1423		return -EINVAL;
1424	}
1425
1426	if (total_data_len < le16_to_cpu(config->total_len)) {
1427		rtl_dev_err(hdev, "config is too short");
1428		return -EINVAL;
1429	}
1430
1431	for (i = 0; i < total_data_len; ) {
1432		entry = ((void *)config->entry) + i;
1433
1434		switch (le16_to_cpu(entry->offset)) {
1435		case 0xc:
1436			if (entry->len < sizeof(*device_baudrate)) {
1437				rtl_dev_err(hdev, "invalid UART config entry");
1438				return -EINVAL;
1439			}
1440
1441			*device_baudrate = get_unaligned_le32(entry->data);
1442			*controller_baudrate = btrtl_convert_baudrate(
1443							*device_baudrate);
1444
1445			if (entry->len >= 13)
1446				*flow_control = !!(entry->data[12] & BIT(2));
1447			else
1448				*flow_control = false;
1449
1450			found = true;
1451			break;
1452
1453		default:
1454			rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)",
1455				   le16_to_cpu(entry->offset), entry->len);
1456			break;
1457		}
1458
1459		i += sizeof(*entry) + entry->len;
1460	}
1461
1462	if (!found) {
1463		rtl_dev_err(hdev, "no UART config entry found");
1464		return -ENOENT;
1465	}
1466
1467	rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate);
1468	rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate);
1469	rtl_dev_dbg(hdev, "flow control %d", *flow_control);
1470
1471	return 0;
1472}
1473EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
1474
1475MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
1476MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
1477MODULE_VERSION(VERSION);
1478MODULE_LICENSE("GPL");
1479MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
1480MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
1481MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
1482MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
1483MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
1484MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_fw.bin");
1485MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_config.bin");
1486MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_fw.bin");
1487MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_config.bin");
1488MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_fw.bin");
1489MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_config.bin");
1490MODULE_FIRMWARE("rtl_bt/rtl8723d_fw.bin");
1491MODULE_FIRMWARE("rtl_bt/rtl8723d_config.bin");
1492MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
1493MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
1494MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
1495MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
1496MODULE_FIRMWARE("rtl_bt/rtl8761b_fw.bin");
1497MODULE_FIRMWARE("rtl_bt/rtl8761b_config.bin");
1498MODULE_FIRMWARE("rtl_bt/rtl8761bu_fw.bin");
1499MODULE_FIRMWARE("rtl_bt/rtl8761bu_config.bin");
1500MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
1501MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
1502MODULE_FIRMWARE("rtl_bt/rtl8821c_fw.bin");
1503MODULE_FIRMWARE("rtl_bt/rtl8821c_config.bin");
1504MODULE_FIRMWARE("rtl_bt/rtl8821cs_fw.bin");
1505MODULE_FIRMWARE("rtl_bt/rtl8821cs_config.bin");
1506MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
1507MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
1508MODULE_FIRMWARE("rtl_bt/rtl8822cs_fw.bin");
1509MODULE_FIRMWARE("rtl_bt/rtl8822cs_config.bin");
1510MODULE_FIRMWARE("rtl_bt/rtl8822cu_fw.bin");
1511MODULE_FIRMWARE("rtl_bt/rtl8822cu_config.bin");
1512MODULE_FIRMWARE("rtl_bt/rtl8851bu_fw.bin");
1513MODULE_FIRMWARE("rtl_bt/rtl8851bu_config.bin");
1514MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin");
1515MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin");
1516MODULE_FIRMWARE("rtl_bt/rtl8852bs_fw.bin");
1517MODULE_FIRMWARE("rtl_bt/rtl8852bs_config.bin");
1518MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin");
1519MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin");
1520MODULE_FIRMWARE("rtl_bt/rtl8852btu_fw.bin");
1521MODULE_FIRMWARE("rtl_bt/rtl8852btu_config.bin");
1522MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin");
1523MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw_v2.bin");
1524MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin");