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