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_EPATCH_SIGNATURE "Realtech" 21 #define RTL_ROM_LMP_8723A 0x1200 22 #define RTL_ROM_LMP_8723B 0x8723 23 #define RTL_ROM_LMP_8821A 0x8821 24 #define RTL_ROM_LMP_8761A 0x8761 25 #define RTL_ROM_LMP_8822B 0x8822 26 #define RTL_ROM_LMP_8852A 0x8852 27 #define RTL_CONFIG_MAGIC 0x8723ab55 28 29 #define IC_MATCH_FL_LMPSUBV (1 << 0) 30 #define IC_MATCH_FL_HCIREV (1 << 1) 31 #define IC_MATCH_FL_HCIVER (1 << 2) 32 #define IC_MATCH_FL_HCIBUS (1 << 3) 33 #define IC_INFO(lmps, hcir, hciv, bus) \ 34 .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | \ 35 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS, \ 36 .lmp_subver = (lmps), \ 37 .hci_rev = (hcir), \ 38 .hci_ver = (hciv), \ 39 .hci_bus = (bus) 40 41 enum btrtl_chip_id { 42 CHIP_ID_8723A, 43 CHIP_ID_8723B, 44 CHIP_ID_8821A, 45 CHIP_ID_8761A, 46 CHIP_ID_8822B = 8, 47 CHIP_ID_8723D, 48 CHIP_ID_8821C, 49 CHIP_ID_8822C = 13, 50 CHIP_ID_8761B, 51 CHIP_ID_8852A = 18, 52 CHIP_ID_8852B = 20, 53 CHIP_ID_8852C = 25, 54 }; 55 56 struct id_table { 57 __u16 match_flags; 58 __u16 lmp_subver; 59 __u16 hci_rev; 60 __u8 hci_ver; 61 __u8 hci_bus; 62 bool config_needed; 63 bool has_rom_version; 64 bool has_msft_ext; 65 char *fw_name; 66 char *cfg_name; 67 }; 68 69 struct btrtl_device_info { 70 const struct id_table *ic_info; 71 u8 rom_version; 72 u8 *fw_data; 73 int fw_len; 74 u8 *cfg_data; 75 int cfg_len; 76 bool drop_fw; 77 int project_id; 78 }; 79 80 static const struct id_table ic_id_table[] = { 81 /* 8723A */ 82 { IC_INFO(RTL_ROM_LMP_8723A, 0xb, 0x6, HCI_USB), 83 .config_needed = false, 84 .has_rom_version = false, 85 .fw_name = "rtl_bt/rtl8723a_fw.bin", 86 .cfg_name = NULL }, 87 88 /* 8723BS */ 89 { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_UART), 90 .config_needed = true, 91 .has_rom_version = true, 92 .fw_name = "rtl_bt/rtl8723bs_fw.bin", 93 .cfg_name = "rtl_bt/rtl8723bs_config" }, 94 95 /* 8723B */ 96 { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_USB), 97 .config_needed = false, 98 .has_rom_version = true, 99 .fw_name = "rtl_bt/rtl8723b_fw.bin", 100 .cfg_name = "rtl_bt/rtl8723b_config" }, 101 102 /* 8723D */ 103 { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_USB), 104 .config_needed = true, 105 .has_rom_version = true, 106 .fw_name = "rtl_bt/rtl8723d_fw.bin", 107 .cfg_name = "rtl_bt/rtl8723d_config" }, 108 109 /* 8723DS */ 110 { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_UART), 111 .config_needed = true, 112 .has_rom_version = true, 113 .fw_name = "rtl_bt/rtl8723ds_fw.bin", 114 .cfg_name = "rtl_bt/rtl8723ds_config" }, 115 116 /* 8821A */ 117 { IC_INFO(RTL_ROM_LMP_8821A, 0xa, 0x6, HCI_USB), 118 .config_needed = false, 119 .has_rom_version = true, 120 .fw_name = "rtl_bt/rtl8821a_fw.bin", 121 .cfg_name = "rtl_bt/rtl8821a_config" }, 122 123 /* 8821C */ 124 { IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_USB), 125 .config_needed = false, 126 .has_rom_version = true, 127 .has_msft_ext = true, 128 .fw_name = "rtl_bt/rtl8821c_fw.bin", 129 .cfg_name = "rtl_bt/rtl8821c_config" }, 130 131 /* 8761A */ 132 { IC_INFO(RTL_ROM_LMP_8761A, 0xa, 0x6, HCI_USB), 133 .config_needed = false, 134 .has_rom_version = true, 135 .fw_name = "rtl_bt/rtl8761a_fw.bin", 136 .cfg_name = "rtl_bt/rtl8761a_config" }, 137 138 /* 8761B */ 139 { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_UART), 140 .config_needed = false, 141 .has_rom_version = true, 142 .has_msft_ext = true, 143 .fw_name = "rtl_bt/rtl8761b_fw.bin", 144 .cfg_name = "rtl_bt/rtl8761b_config" }, 145 146 /* 8761BU */ 147 { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_USB), 148 .config_needed = false, 149 .has_rom_version = true, 150 .fw_name = "rtl_bt/rtl8761bu_fw.bin", 151 .cfg_name = "rtl_bt/rtl8761bu_config" }, 152 153 /* 8822C with UART interface */ 154 { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0x8, HCI_UART), 155 .config_needed = true, 156 .has_rom_version = true, 157 .has_msft_ext = true, 158 .fw_name = "rtl_bt/rtl8822cs_fw.bin", 159 .cfg_name = "rtl_bt/rtl8822cs_config" }, 160 161 /* 8822C with UART interface */ 162 { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_UART), 163 .config_needed = true, 164 .has_rom_version = true, 165 .has_msft_ext = true, 166 .fw_name = "rtl_bt/rtl8822cs_fw.bin", 167 .cfg_name = "rtl_bt/rtl8822cs_config" }, 168 169 /* 8822C with USB interface */ 170 { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_USB), 171 .config_needed = false, 172 .has_rom_version = true, 173 .has_msft_ext = true, 174 .fw_name = "rtl_bt/rtl8822cu_fw.bin", 175 .cfg_name = "rtl_bt/rtl8822cu_config" }, 176 177 /* 8822B */ 178 { IC_INFO(RTL_ROM_LMP_8822B, 0xb, 0x7, HCI_USB), 179 .config_needed = true, 180 .has_rom_version = true, 181 .has_msft_ext = true, 182 .fw_name = "rtl_bt/rtl8822b_fw.bin", 183 .cfg_name = "rtl_bt/rtl8822b_config" }, 184 185 /* 8852A */ 186 { IC_INFO(RTL_ROM_LMP_8852A, 0xa, 0xb, HCI_USB), 187 .config_needed = false, 188 .has_rom_version = true, 189 .has_msft_ext = true, 190 .fw_name = "rtl_bt/rtl8852au_fw.bin", 191 .cfg_name = "rtl_bt/rtl8852au_config" }, 192 193 /* 8852B with UART interface */ 194 { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_UART), 195 .config_needed = true, 196 .has_rom_version = true, 197 .has_msft_ext = true, 198 .fw_name = "rtl_bt/rtl8852bs_fw.bin", 199 .cfg_name = "rtl_bt/rtl8852bs_config" }, 200 201 /* 8852B */ 202 { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_USB), 203 .config_needed = false, 204 .has_rom_version = true, 205 .has_msft_ext = true, 206 .fw_name = "rtl_bt/rtl8852bu_fw.bin", 207 .cfg_name = "rtl_bt/rtl8852bu_config" }, 208 209 /* 8852C */ 210 { IC_INFO(RTL_ROM_LMP_8852A, 0xc, 0xc, HCI_USB), 211 .config_needed = false, 212 .has_rom_version = true, 213 .has_msft_ext = true, 214 .fw_name = "rtl_bt/rtl8852cu_fw.bin", 215 .cfg_name = "rtl_bt/rtl8852cu_config" }, 216 }; 217 218 static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev, 219 u8 hci_ver, u8 hci_bus) 220 { 221 int i; 222 223 for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) { 224 if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) && 225 (ic_id_table[i].lmp_subver != lmp_subver)) 226 continue; 227 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) && 228 (ic_id_table[i].hci_rev != hci_rev)) 229 continue; 230 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) && 231 (ic_id_table[i].hci_ver != hci_ver)) 232 continue; 233 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) && 234 (ic_id_table[i].hci_bus != hci_bus)) 235 continue; 236 237 break; 238 } 239 if (i >= ARRAY_SIZE(ic_id_table)) 240 return NULL; 241 242 return &ic_id_table[i]; 243 } 244 245 static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev) 246 { 247 struct sk_buff *skb; 248 249 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, 250 HCI_INIT_TIMEOUT); 251 if (IS_ERR(skb)) { 252 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)", 253 PTR_ERR(skb)); 254 return skb; 255 } 256 257 if (skb->len != sizeof(struct hci_rp_read_local_version)) { 258 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch"); 259 kfree_skb(skb); 260 return ERR_PTR(-EIO); 261 } 262 263 return skb; 264 } 265 266 static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version) 267 { 268 struct rtl_rom_version_evt *rom_version; 269 struct sk_buff *skb; 270 271 /* Read RTL ROM version command */ 272 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT); 273 if (IS_ERR(skb)) { 274 rtl_dev_err(hdev, "Read ROM version failed (%ld)", 275 PTR_ERR(skb)); 276 return PTR_ERR(skb); 277 } 278 279 if (skb->len != sizeof(*rom_version)) { 280 rtl_dev_err(hdev, "version event length mismatch"); 281 kfree_skb(skb); 282 return -EIO; 283 } 284 285 rom_version = (struct rtl_rom_version_evt *)skb->data; 286 rtl_dev_info(hdev, "rom_version status=%x version=%x", 287 rom_version->status, rom_version->version); 288 289 *version = rom_version->version; 290 291 kfree_skb(skb); 292 return 0; 293 } 294 295 static int rtlbt_parse_firmware(struct hci_dev *hdev, 296 struct btrtl_device_info *btrtl_dev, 297 unsigned char **_buf) 298 { 299 static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 }; 300 struct rtl_epatch_header *epatch_info; 301 unsigned char *buf; 302 int i, len; 303 size_t min_size; 304 u8 opcode, length, data; 305 int project_id = -1; 306 const unsigned char *fwptr, *chip_id_base; 307 const unsigned char *patch_length_base, *patch_offset_base; 308 u32 patch_offset = 0; 309 u16 patch_length, num_patches; 310 static const struct { 311 __u16 lmp_subver; 312 __u8 id; 313 } project_id_to_lmp_subver[] = { 314 { RTL_ROM_LMP_8723A, 0 }, 315 { RTL_ROM_LMP_8723B, 1 }, 316 { RTL_ROM_LMP_8821A, 2 }, 317 { RTL_ROM_LMP_8761A, 3 }, 318 { RTL_ROM_LMP_8822B, 8 }, 319 { RTL_ROM_LMP_8723B, 9 }, /* 8723D */ 320 { RTL_ROM_LMP_8821A, 10 }, /* 8821C */ 321 { RTL_ROM_LMP_8822B, 13 }, /* 8822C */ 322 { RTL_ROM_LMP_8761A, 14 }, /* 8761B */ 323 { RTL_ROM_LMP_8852A, 18 }, /* 8852A */ 324 { RTL_ROM_LMP_8852A, 20 }, /* 8852B */ 325 { RTL_ROM_LMP_8852A, 25 }, /* 8852C */ 326 }; 327 328 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3; 329 if (btrtl_dev->fw_len < min_size) 330 return -EINVAL; 331 332 fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig); 333 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) { 334 rtl_dev_err(hdev, "extension section signature mismatch"); 335 return -EINVAL; 336 } 337 338 /* Loop from the end of the firmware parsing instructions, until 339 * we find an instruction that identifies the "project ID" for the 340 * hardware supported by this firwmare file. 341 * Once we have that, we double-check that project_id is suitable 342 * for the hardware we are working with. 343 */ 344 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) { 345 opcode = *--fwptr; 346 length = *--fwptr; 347 data = *--fwptr; 348 349 BT_DBG("check op=%x len=%x data=%x", opcode, length, data); 350 351 if (opcode == 0xff) /* EOF */ 352 break; 353 354 if (length == 0) { 355 rtl_dev_err(hdev, "found instruction with length 0"); 356 return -EINVAL; 357 } 358 359 if (opcode == 0 && length == 1) { 360 project_id = data; 361 break; 362 } 363 364 fwptr -= length; 365 } 366 367 if (project_id < 0) { 368 rtl_dev_err(hdev, "failed to find version instruction"); 369 return -EINVAL; 370 } 371 372 /* Find project_id in table */ 373 for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) { 374 if (project_id == project_id_to_lmp_subver[i].id) { 375 btrtl_dev->project_id = project_id; 376 break; 377 } 378 } 379 380 if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) { 381 rtl_dev_err(hdev, "unknown project id %d", project_id); 382 return -EINVAL; 383 } 384 385 if (btrtl_dev->ic_info->lmp_subver != 386 project_id_to_lmp_subver[i].lmp_subver) { 387 rtl_dev_err(hdev, "firmware is for %x but this is a %x", 388 project_id_to_lmp_subver[i].lmp_subver, 389 btrtl_dev->ic_info->lmp_subver); 390 return -EINVAL; 391 } 392 393 epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data; 394 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) { 395 rtl_dev_err(hdev, "bad EPATCH signature"); 396 return -EINVAL; 397 } 398 399 num_patches = le16_to_cpu(epatch_info->num_patches); 400 BT_DBG("fw_version=%x, num_patches=%d", 401 le32_to_cpu(epatch_info->fw_version), num_patches); 402 403 /* After the rtl_epatch_header there is a funky patch metadata section. 404 * Assuming 2 patches, the layout is: 405 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2 406 * 407 * Find the right patch for this chip. 408 */ 409 min_size += 8 * num_patches; 410 if (btrtl_dev->fw_len < min_size) 411 return -EINVAL; 412 413 chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header); 414 patch_length_base = chip_id_base + (sizeof(u16) * num_patches); 415 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches); 416 for (i = 0; i < num_patches; i++) { 417 u16 chip_id = get_unaligned_le16(chip_id_base + 418 (i * sizeof(u16))); 419 if (chip_id == btrtl_dev->rom_version + 1) { 420 patch_length = get_unaligned_le16(patch_length_base + 421 (i * sizeof(u16))); 422 patch_offset = get_unaligned_le32(patch_offset_base + 423 (i * sizeof(u32))); 424 break; 425 } 426 } 427 428 if (!patch_offset) { 429 rtl_dev_err(hdev, "didn't find patch for chip id %d", 430 btrtl_dev->rom_version); 431 return -EINVAL; 432 } 433 434 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i); 435 min_size = patch_offset + patch_length; 436 if (btrtl_dev->fw_len < min_size) 437 return -EINVAL; 438 439 /* Copy the firmware into a new buffer and write the version at 440 * the end. 441 */ 442 len = patch_length; 443 buf = kvmalloc(patch_length, GFP_KERNEL); 444 if (!buf) 445 return -ENOMEM; 446 447 memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4); 448 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4); 449 450 *_buf = buf; 451 return len; 452 } 453 454 static int rtl_download_firmware(struct hci_dev *hdev, 455 const unsigned char *data, int fw_len) 456 { 457 struct rtl_download_cmd *dl_cmd; 458 int frag_num = fw_len / RTL_FRAG_LEN + 1; 459 int frag_len = RTL_FRAG_LEN; 460 int ret = 0; 461 int i; 462 struct sk_buff *skb; 463 struct hci_rp_read_local_version *rp; 464 465 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL); 466 if (!dl_cmd) 467 return -ENOMEM; 468 469 for (i = 0; i < frag_num; i++) { 470 struct sk_buff *skb; 471 472 BT_DBG("download fw (%d/%d)", i, frag_num); 473 474 if (i > 0x7f) 475 dl_cmd->index = (i & 0x7f) + 1; 476 else 477 dl_cmd->index = i; 478 479 if (i == (frag_num - 1)) { 480 dl_cmd->index |= 0x80; /* data end */ 481 frag_len = fw_len % RTL_FRAG_LEN; 482 } 483 memcpy(dl_cmd->data, data, frag_len); 484 485 /* Send download command */ 486 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd, 487 HCI_INIT_TIMEOUT); 488 if (IS_ERR(skb)) { 489 rtl_dev_err(hdev, "download fw command failed (%ld)", 490 PTR_ERR(skb)); 491 ret = PTR_ERR(skb); 492 goto out; 493 } 494 495 if (skb->len != sizeof(struct rtl_download_response)) { 496 rtl_dev_err(hdev, "download fw event length mismatch"); 497 kfree_skb(skb); 498 ret = -EIO; 499 goto out; 500 } 501 502 kfree_skb(skb); 503 data += RTL_FRAG_LEN; 504 } 505 506 skb = btrtl_read_local_version(hdev); 507 if (IS_ERR(skb)) { 508 ret = PTR_ERR(skb); 509 rtl_dev_err(hdev, "read local version failed"); 510 goto out; 511 } 512 513 rp = (struct hci_rp_read_local_version *)skb->data; 514 rtl_dev_info(hdev, "fw version 0x%04x%04x", 515 __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver)); 516 kfree_skb(skb); 517 518 out: 519 kfree(dl_cmd); 520 return ret; 521 } 522 523 static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff) 524 { 525 const struct firmware *fw; 526 int ret; 527 528 rtl_dev_info(hdev, "loading %s", name); 529 ret = request_firmware(&fw, name, &hdev->dev); 530 if (ret < 0) 531 return ret; 532 ret = fw->size; 533 *buff = kvmalloc(fw->size, GFP_KERNEL); 534 if (*buff) 535 memcpy(*buff, fw->data, ret); 536 else 537 ret = -ENOMEM; 538 539 release_firmware(fw); 540 541 return ret; 542 } 543 544 static int btrtl_setup_rtl8723a(struct hci_dev *hdev, 545 struct btrtl_device_info *btrtl_dev) 546 { 547 if (btrtl_dev->fw_len < 8) 548 return -EINVAL; 549 550 /* Check that the firmware doesn't have the epatch signature 551 * (which is only for RTL8723B and newer). 552 */ 553 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) { 554 rtl_dev_err(hdev, "unexpected EPATCH signature!"); 555 return -EINVAL; 556 } 557 558 return rtl_download_firmware(hdev, btrtl_dev->fw_data, 559 btrtl_dev->fw_len); 560 } 561 562 static int btrtl_setup_rtl8723b(struct hci_dev *hdev, 563 struct btrtl_device_info *btrtl_dev) 564 { 565 unsigned char *fw_data = NULL; 566 int ret; 567 u8 *tbuff; 568 569 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data); 570 if (ret < 0) 571 goto out; 572 573 if (btrtl_dev->cfg_len > 0) { 574 tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL); 575 if (!tbuff) { 576 ret = -ENOMEM; 577 goto out; 578 } 579 580 memcpy(tbuff, fw_data, ret); 581 kvfree(fw_data); 582 583 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len); 584 ret += btrtl_dev->cfg_len; 585 586 fw_data = tbuff; 587 } 588 589 rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret); 590 591 ret = rtl_download_firmware(hdev, fw_data, ret); 592 593 out: 594 kvfree(fw_data); 595 return ret; 596 } 597 598 void btrtl_free(struct btrtl_device_info *btrtl_dev) 599 { 600 kvfree(btrtl_dev->fw_data); 601 kvfree(btrtl_dev->cfg_data); 602 kfree(btrtl_dev); 603 } 604 EXPORT_SYMBOL_GPL(btrtl_free); 605 606 struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev, 607 const char *postfix) 608 { 609 struct btrtl_device_info *btrtl_dev; 610 struct sk_buff *skb; 611 struct hci_rp_read_local_version *resp; 612 char cfg_name[40]; 613 u16 hci_rev, lmp_subver; 614 u8 hci_ver; 615 int ret; 616 u16 opcode; 617 u8 cmd[2]; 618 619 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL); 620 if (!btrtl_dev) { 621 ret = -ENOMEM; 622 goto err_alloc; 623 } 624 625 skb = btrtl_read_local_version(hdev); 626 if (IS_ERR(skb)) { 627 ret = PTR_ERR(skb); 628 goto err_free; 629 } 630 631 resp = (struct hci_rp_read_local_version *)skb->data; 632 rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x", 633 resp->hci_ver, resp->hci_rev, 634 resp->lmp_ver, resp->lmp_subver); 635 636 hci_ver = resp->hci_ver; 637 hci_rev = le16_to_cpu(resp->hci_rev); 638 lmp_subver = le16_to_cpu(resp->lmp_subver); 639 640 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, 641 hdev->bus); 642 643 if (!btrtl_dev->ic_info) 644 btrtl_dev->drop_fw = true; 645 646 if (btrtl_dev->drop_fw) { 647 opcode = hci_opcode_pack(0x3f, 0x66); 648 cmd[0] = opcode & 0xff; 649 cmd[1] = opcode >> 8; 650 651 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); 652 if (!skb) 653 goto out_free; 654 655 skb_put_data(skb, cmd, sizeof(cmd)); 656 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; 657 658 hdev->send(hdev, skb); 659 660 /* Ensure the above vendor command is sent to controller and 661 * process has done. 662 */ 663 msleep(200); 664 665 /* Read the local version again. Expect to have the vanilla 666 * version as cold boot. 667 */ 668 skb = btrtl_read_local_version(hdev); 669 if (IS_ERR(skb)) { 670 ret = PTR_ERR(skb); 671 goto err_free; 672 } 673 674 resp = (struct hci_rp_read_local_version *)skb->data; 675 rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x", 676 resp->hci_ver, resp->hci_rev, 677 resp->lmp_ver, resp->lmp_subver); 678 679 hci_ver = resp->hci_ver; 680 hci_rev = le16_to_cpu(resp->hci_rev); 681 lmp_subver = le16_to_cpu(resp->lmp_subver); 682 683 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, 684 hdev->bus); 685 } 686 out_free: 687 kfree_skb(skb); 688 689 if (!btrtl_dev->ic_info) { 690 rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x", 691 lmp_subver, hci_rev, hci_ver); 692 return btrtl_dev; 693 } 694 695 if (btrtl_dev->ic_info->has_rom_version) { 696 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version); 697 if (ret) 698 goto err_free; 699 } 700 701 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name, 702 &btrtl_dev->fw_data); 703 if (btrtl_dev->fw_len < 0) { 704 rtl_dev_err(hdev, "firmware file %s not found", 705 btrtl_dev->ic_info->fw_name); 706 ret = btrtl_dev->fw_len; 707 goto err_free; 708 } 709 710 if (btrtl_dev->ic_info->cfg_name) { 711 if (postfix) { 712 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin", 713 btrtl_dev->ic_info->cfg_name, postfix); 714 } else { 715 snprintf(cfg_name, sizeof(cfg_name), "%s.bin", 716 btrtl_dev->ic_info->cfg_name); 717 } 718 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name, 719 &btrtl_dev->cfg_data); 720 if (btrtl_dev->ic_info->config_needed && 721 btrtl_dev->cfg_len <= 0) { 722 rtl_dev_err(hdev, "mandatory config file %s not found", 723 btrtl_dev->ic_info->cfg_name); 724 ret = btrtl_dev->cfg_len; 725 goto err_free; 726 } 727 } 728 729 /* The following chips supports the Microsoft vendor extension, 730 * therefore set the corresponding VsMsftOpCode. 731 */ 732 if (btrtl_dev->ic_info->has_msft_ext) 733 hci_set_msft_opcode(hdev, 0xFCF0); 734 735 return btrtl_dev; 736 737 err_free: 738 btrtl_free(btrtl_dev); 739 err_alloc: 740 return ERR_PTR(ret); 741 } 742 EXPORT_SYMBOL_GPL(btrtl_initialize); 743 744 int btrtl_download_firmware(struct hci_dev *hdev, 745 struct btrtl_device_info *btrtl_dev) 746 { 747 /* Match a set of subver values that correspond to stock firmware, 748 * which is not compatible with standard btusb. 749 * If matched, upload an alternative firmware that does conform to 750 * standard btusb. Once that firmware is uploaded, the subver changes 751 * to a different value. 752 */ 753 if (!btrtl_dev->ic_info) { 754 rtl_dev_info(hdev, "assuming no firmware upload needed"); 755 return 0; 756 } 757 758 switch (btrtl_dev->ic_info->lmp_subver) { 759 case RTL_ROM_LMP_8723A: 760 return btrtl_setup_rtl8723a(hdev, btrtl_dev); 761 case RTL_ROM_LMP_8723B: 762 case RTL_ROM_LMP_8821A: 763 case RTL_ROM_LMP_8761A: 764 case RTL_ROM_LMP_8822B: 765 case RTL_ROM_LMP_8852A: 766 return btrtl_setup_rtl8723b(hdev, btrtl_dev); 767 default: 768 rtl_dev_info(hdev, "assuming no firmware upload needed"); 769 return 0; 770 } 771 } 772 EXPORT_SYMBOL_GPL(btrtl_download_firmware); 773 774 void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev) 775 { 776 /* Enable controller to do both LE scan and BR/EDR inquiry 777 * simultaneously. 778 */ 779 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); 780 781 /* Enable central-peripheral role (able to create new connections with 782 * an existing connection in slave role). 783 */ 784 /* Enable WBS supported for the specific Realtek devices. */ 785 switch (btrtl_dev->project_id) { 786 case CHIP_ID_8822C: 787 case CHIP_ID_8852A: 788 case CHIP_ID_8852B: 789 case CHIP_ID_8852C: 790 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks); 791 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks); 792 793 /* RTL8852C needs to transmit mSBC data continuously without 794 * the zero length of USB packets for the ALT 6 supported chips 795 */ 796 if (btrtl_dev->project_id == CHIP_ID_8852C) 797 btrealtek_set_flag(hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP); 798 799 hci_set_aosp_capable(hdev); 800 break; 801 default: 802 rtl_dev_dbg(hdev, "Central-peripheral role not enabled."); 803 rtl_dev_dbg(hdev, "WBS supported not enabled."); 804 break; 805 } 806 } 807 EXPORT_SYMBOL_GPL(btrtl_set_quirks); 808 809 int btrtl_setup_realtek(struct hci_dev *hdev) 810 { 811 struct btrtl_device_info *btrtl_dev; 812 int ret; 813 814 btrtl_dev = btrtl_initialize(hdev, NULL); 815 if (IS_ERR(btrtl_dev)) 816 return PTR_ERR(btrtl_dev); 817 818 ret = btrtl_download_firmware(hdev, btrtl_dev); 819 820 btrtl_set_quirks(hdev, btrtl_dev); 821 822 btrtl_free(btrtl_dev); 823 return ret; 824 } 825 EXPORT_SYMBOL_GPL(btrtl_setup_realtek); 826 827 int btrtl_shutdown_realtek(struct hci_dev *hdev) 828 { 829 struct sk_buff *skb; 830 int ret; 831 832 /* According to the vendor driver, BT must be reset on close to avoid 833 * firmware crash. 834 */ 835 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); 836 if (IS_ERR(skb)) { 837 ret = PTR_ERR(skb); 838 bt_dev_err(hdev, "HCI reset during shutdown failed"); 839 return ret; 840 } 841 kfree_skb(skb); 842 843 return 0; 844 } 845 EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek); 846 847 static unsigned int btrtl_convert_baudrate(u32 device_baudrate) 848 { 849 switch (device_baudrate) { 850 case 0x0252a00a: 851 return 230400; 852 853 case 0x05f75004: 854 return 921600; 855 856 case 0x00005004: 857 return 1000000; 858 859 case 0x04928002: 860 case 0x01128002: 861 return 1500000; 862 863 case 0x00005002: 864 return 2000000; 865 866 case 0x0000b001: 867 return 2500000; 868 869 case 0x04928001: 870 return 3000000; 871 872 case 0x052a6001: 873 return 3500000; 874 875 case 0x00005001: 876 return 4000000; 877 878 case 0x0252c014: 879 default: 880 return 115200; 881 } 882 } 883 884 int btrtl_get_uart_settings(struct hci_dev *hdev, 885 struct btrtl_device_info *btrtl_dev, 886 unsigned int *controller_baudrate, 887 u32 *device_baudrate, bool *flow_control) 888 { 889 struct rtl_vendor_config *config; 890 struct rtl_vendor_config_entry *entry; 891 int i, total_data_len; 892 bool found = false; 893 894 total_data_len = btrtl_dev->cfg_len - sizeof(*config); 895 if (total_data_len <= 0) { 896 rtl_dev_warn(hdev, "no config loaded"); 897 return -EINVAL; 898 } 899 900 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data; 901 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) { 902 rtl_dev_err(hdev, "invalid config magic"); 903 return -EINVAL; 904 } 905 906 if (total_data_len < le16_to_cpu(config->total_len)) { 907 rtl_dev_err(hdev, "config is too short"); 908 return -EINVAL; 909 } 910 911 for (i = 0; i < total_data_len; ) { 912 entry = ((void *)config->entry) + i; 913 914 switch (le16_to_cpu(entry->offset)) { 915 case 0xc: 916 if (entry->len < sizeof(*device_baudrate)) { 917 rtl_dev_err(hdev, "invalid UART config entry"); 918 return -EINVAL; 919 } 920 921 *device_baudrate = get_unaligned_le32(entry->data); 922 *controller_baudrate = btrtl_convert_baudrate( 923 *device_baudrate); 924 925 if (entry->len >= 13) 926 *flow_control = !!(entry->data[12] & BIT(2)); 927 else 928 *flow_control = false; 929 930 found = true; 931 break; 932 933 default: 934 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)", 935 le16_to_cpu(entry->offset), entry->len); 936 break; 937 } 938 939 i += sizeof(*entry) + entry->len; 940 } 941 942 if (!found) { 943 rtl_dev_err(hdev, "no UART config entry found"); 944 return -ENOENT; 945 } 946 947 rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate); 948 rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate); 949 rtl_dev_dbg(hdev, "flow control %d", *flow_control); 950 951 return 0; 952 } 953 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings); 954 955 MODULE_AUTHOR("Daniel Drake <[email protected]>"); 956 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION); 957 MODULE_VERSION(VERSION); 958 MODULE_LICENSE("GPL"); 959 MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin"); 960 MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin"); 961 MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin"); 962 MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin"); 963 MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin"); 964 MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin"); 965 MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin"); 966 MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin"); 967 MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin"); 968 MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin"); 969 MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin"); 970 MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin"); 971 MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin"); 972 MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin"); 973 MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin"); 974 MODULE_FIRMWARE("rtl_bt/rtl8852bs_fw.bin"); 975 MODULE_FIRMWARE("rtl_bt/rtl8852bs_config.bin"); 976 MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin"); 977 MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin"); 978 MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin"); 979 MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin"); 980