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_3499 0x3499 22 #define RTL_ROM_LMP_8723A 0x1200 23 #define RTL_ROM_LMP_8723B 0x8723 24 #define RTL_ROM_LMP_8723D 0x8873 25 #define RTL_ROM_LMP_8821A 0x8821 26 #define RTL_ROM_LMP_8761A 0x8761 27 #define RTL_ROM_LMP_8822B 0x8822 28 #define RTL_CONFIG_MAGIC 0x8723ab55 29 30 #define IC_MATCH_FL_LMPSUBV (1 << 0) 31 #define IC_MATCH_FL_HCIREV (1 << 1) 32 #define IC_MATCH_FL_HCIVER (1 << 2) 33 #define IC_MATCH_FL_HCIBUS (1 << 3) 34 #define IC_INFO(lmps, hcir) \ 35 .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \ 36 .lmp_subver = (lmps), \ 37 .hci_rev = (hcir) 38 39 struct id_table { 40 __u16 match_flags; 41 __u16 lmp_subver; 42 __u16 hci_rev; 43 __u8 hci_ver; 44 __u8 hci_bus; 45 bool config_needed; 46 bool has_rom_version; 47 char *fw_name; 48 char *cfg_name; 49 }; 50 51 struct btrtl_device_info { 52 const struct id_table *ic_info; 53 u8 rom_version; 54 u8 *fw_data; 55 int fw_len; 56 u8 *cfg_data; 57 int cfg_len; 58 }; 59 60 static const struct id_table ic_id_table[] = { 61 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0, 62 .config_needed = false, 63 .has_rom_version = false, 64 .fw_name = "rtl_bt/rtl8723a_fw.bin", 65 .cfg_name = NULL }, 66 67 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0, 68 .config_needed = false, 69 .has_rom_version = false, 70 .fw_name = "rtl_bt/rtl8723a_fw.bin", 71 .cfg_name = NULL }, 72 73 /* 8723BS */ 74 { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | 75 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS, 76 .lmp_subver = RTL_ROM_LMP_8723B, 77 .hci_rev = 0xb, 78 .hci_ver = 6, 79 .hci_bus = HCI_UART, 80 .config_needed = true, 81 .has_rom_version = true, 82 .fw_name = "rtl_bt/rtl8723bs_fw.bin", 83 .cfg_name = "rtl_bt/rtl8723bs_config" }, 84 85 /* 8723B */ 86 { IC_INFO(RTL_ROM_LMP_8723B, 0xb), 87 .config_needed = false, 88 .has_rom_version = true, 89 .fw_name = "rtl_bt/rtl8723b_fw.bin", 90 .cfg_name = "rtl_bt/rtl8723b_config" }, 91 92 /* 8723D */ 93 { IC_INFO(RTL_ROM_LMP_8723B, 0xd), 94 .config_needed = true, 95 .has_rom_version = true, 96 .fw_name = "rtl_bt/rtl8723d_fw.bin", 97 .cfg_name = "rtl_bt/rtl8723d_config" }, 98 99 /* 8723DS */ 100 { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | 101 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS, 102 .lmp_subver = RTL_ROM_LMP_8723B, 103 .hci_rev = 0xd, 104 .hci_ver = 8, 105 .hci_bus = HCI_UART, 106 .config_needed = true, 107 .has_rom_version = true, 108 .fw_name = "rtl_bt/rtl8723ds_fw.bin", 109 .cfg_name = "rtl_bt/rtl8723ds_config" }, 110 111 /* 8723DU */ 112 { IC_INFO(RTL_ROM_LMP_8723D, 0x826C), 113 .config_needed = true, 114 .has_rom_version = true, 115 .fw_name = "rtl_bt/rtl8723d_fw.bin", 116 .cfg_name = "rtl_bt/rtl8723d_config" }, 117 118 /* 8821A */ 119 { IC_INFO(RTL_ROM_LMP_8821A, 0xa), 120 .config_needed = false, 121 .has_rom_version = true, 122 .fw_name = "rtl_bt/rtl8821a_fw.bin", 123 .cfg_name = "rtl_bt/rtl8821a_config" }, 124 125 /* 8821C */ 126 { IC_INFO(RTL_ROM_LMP_8821A, 0xc), 127 .config_needed = false, 128 .has_rom_version = true, 129 .fw_name = "rtl_bt/rtl8821c_fw.bin", 130 .cfg_name = "rtl_bt/rtl8821c_config" }, 131 132 /* 8761A */ 133 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0, 134 .config_needed = false, 135 .has_rom_version = true, 136 .fw_name = "rtl_bt/rtl8761a_fw.bin", 137 .cfg_name = "rtl_bt/rtl8761a_config" }, 138 139 /* 8822C with USB interface */ 140 { IC_INFO(RTL_ROM_LMP_8822B, 0xc), 141 .config_needed = false, 142 .has_rom_version = true, 143 .fw_name = "rtl_bt/rtl8822cu_fw.bin", 144 .cfg_name = "rtl_bt/rtl8822cu_config" }, 145 146 /* 8822B */ 147 { IC_INFO(RTL_ROM_LMP_8822B, 0xb), 148 .config_needed = true, 149 .has_rom_version = true, 150 .fw_name = "rtl_bt/rtl8822b_fw.bin", 151 .cfg_name = "rtl_bt/rtl8822b_config" }, 152 }; 153 154 static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev, 155 u8 hci_ver, u8 hci_bus) 156 { 157 int i; 158 159 for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) { 160 if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) && 161 (ic_id_table[i].lmp_subver != lmp_subver)) 162 continue; 163 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) && 164 (ic_id_table[i].hci_rev != hci_rev)) 165 continue; 166 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) && 167 (ic_id_table[i].hci_ver != hci_ver)) 168 continue; 169 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) && 170 (ic_id_table[i].hci_bus != hci_bus)) 171 continue; 172 173 break; 174 } 175 if (i >= ARRAY_SIZE(ic_id_table)) 176 return NULL; 177 178 return &ic_id_table[i]; 179 } 180 181 static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version) 182 { 183 struct rtl_rom_version_evt *rom_version; 184 struct sk_buff *skb; 185 186 /* Read RTL ROM version command */ 187 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT); 188 if (IS_ERR(skb)) { 189 rtl_dev_err(hdev, "Read ROM version failed (%ld)\n", 190 PTR_ERR(skb)); 191 return PTR_ERR(skb); 192 } 193 194 if (skb->len != sizeof(*rom_version)) { 195 rtl_dev_err(hdev, "RTL version event length mismatch\n"); 196 kfree_skb(skb); 197 return -EIO; 198 } 199 200 rom_version = (struct rtl_rom_version_evt *)skb->data; 201 rtl_dev_info(hdev, "rom_version status=%x version=%x\n", 202 rom_version->status, rom_version->version); 203 204 *version = rom_version->version; 205 206 kfree_skb(skb); 207 return 0; 208 } 209 210 static int rtlbt_parse_firmware(struct hci_dev *hdev, 211 struct btrtl_device_info *btrtl_dev, 212 unsigned char **_buf) 213 { 214 static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 }; 215 struct rtl_epatch_header *epatch_info; 216 unsigned char *buf; 217 int i, len; 218 size_t min_size; 219 u8 opcode, length, data; 220 int project_id = -1; 221 const unsigned char *fwptr, *chip_id_base; 222 const unsigned char *patch_length_base, *patch_offset_base; 223 u32 patch_offset = 0; 224 u16 patch_length, num_patches; 225 static const struct { 226 __u16 lmp_subver; 227 __u8 id; 228 } project_id_to_lmp_subver[] = { 229 { RTL_ROM_LMP_8723A, 0 }, 230 { RTL_ROM_LMP_8723B, 1 }, 231 { RTL_ROM_LMP_8821A, 2 }, 232 { RTL_ROM_LMP_8761A, 3 }, 233 { RTL_ROM_LMP_8822B, 8 }, 234 { RTL_ROM_LMP_8723B, 9 }, /* 8723D */ 235 { RTL_ROM_LMP_8821A, 10 }, /* 8821C */ 236 { RTL_ROM_LMP_8822B, 13 }, /* 8822C */ 237 }; 238 239 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3; 240 if (btrtl_dev->fw_len < min_size) 241 return -EINVAL; 242 243 fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig); 244 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) { 245 rtl_dev_err(hdev, "extension section signature mismatch\n"); 246 return -EINVAL; 247 } 248 249 /* Loop from the end of the firmware parsing instructions, until 250 * we find an instruction that identifies the "project ID" for the 251 * hardware supported by this firwmare file. 252 * Once we have that, we double-check that that project_id is suitable 253 * for the hardware we are working with. 254 */ 255 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) { 256 opcode = *--fwptr; 257 length = *--fwptr; 258 data = *--fwptr; 259 260 BT_DBG("check op=%x len=%x data=%x", opcode, length, data); 261 262 if (opcode == 0xff) /* EOF */ 263 break; 264 265 if (length == 0) { 266 rtl_dev_err(hdev, "found instruction with length 0\n"); 267 return -EINVAL; 268 } 269 270 if (opcode == 0 && length == 1) { 271 project_id = data; 272 break; 273 } 274 275 fwptr -= length; 276 } 277 278 if (project_id < 0) { 279 rtl_dev_err(hdev, "failed to find version instruction\n"); 280 return -EINVAL; 281 } 282 283 /* Find project_id in table */ 284 for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) { 285 if (project_id == project_id_to_lmp_subver[i].id) 286 break; 287 } 288 289 if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) { 290 rtl_dev_err(hdev, "unknown project id %d\n", project_id); 291 return -EINVAL; 292 } 293 294 if (btrtl_dev->ic_info->lmp_subver != 295 project_id_to_lmp_subver[i].lmp_subver) { 296 rtl_dev_err(hdev, "firmware is for %x but this is a %x\n", 297 project_id_to_lmp_subver[i].lmp_subver, 298 btrtl_dev->ic_info->lmp_subver); 299 return -EINVAL; 300 } 301 302 epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data; 303 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) { 304 rtl_dev_err(hdev, "bad EPATCH signature\n"); 305 return -EINVAL; 306 } 307 308 num_patches = le16_to_cpu(epatch_info->num_patches); 309 BT_DBG("fw_version=%x, num_patches=%d", 310 le32_to_cpu(epatch_info->fw_version), num_patches); 311 312 /* After the rtl_epatch_header there is a funky patch metadata section. 313 * Assuming 2 patches, the layout is: 314 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2 315 * 316 * Find the right patch for this chip. 317 */ 318 min_size += 8 * num_patches; 319 if (btrtl_dev->fw_len < min_size) 320 return -EINVAL; 321 322 chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header); 323 patch_length_base = chip_id_base + (sizeof(u16) * num_patches); 324 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches); 325 for (i = 0; i < num_patches; i++) { 326 u16 chip_id = get_unaligned_le16(chip_id_base + 327 (i * sizeof(u16))); 328 if (chip_id == btrtl_dev->rom_version + 1) { 329 patch_length = get_unaligned_le16(patch_length_base + 330 (i * sizeof(u16))); 331 patch_offset = get_unaligned_le32(patch_offset_base + 332 (i * sizeof(u32))); 333 break; 334 } 335 } 336 337 if (!patch_offset) { 338 rtl_dev_err(hdev, "didn't find patch for chip id %d", 339 btrtl_dev->rom_version); 340 return -EINVAL; 341 } 342 343 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i); 344 min_size = patch_offset + patch_length; 345 if (btrtl_dev->fw_len < min_size) 346 return -EINVAL; 347 348 /* Copy the firmware into a new buffer and write the version at 349 * the end. 350 */ 351 len = patch_length; 352 buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length, 353 GFP_KERNEL); 354 if (!buf) 355 return -ENOMEM; 356 357 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4); 358 359 *_buf = buf; 360 return len; 361 } 362 363 static int rtl_download_firmware(struct hci_dev *hdev, 364 const unsigned char *data, int fw_len) 365 { 366 struct rtl_download_cmd *dl_cmd; 367 int frag_num = fw_len / RTL_FRAG_LEN + 1; 368 int frag_len = RTL_FRAG_LEN; 369 int ret = 0; 370 int i; 371 372 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL); 373 if (!dl_cmd) 374 return -ENOMEM; 375 376 for (i = 0; i < frag_num; i++) { 377 struct sk_buff *skb; 378 379 BT_DBG("download fw (%d/%d)", i, frag_num); 380 381 dl_cmd->index = i; 382 if (i == (frag_num - 1)) { 383 dl_cmd->index |= 0x80; /* data end */ 384 frag_len = fw_len % RTL_FRAG_LEN; 385 } 386 memcpy(dl_cmd->data, data, frag_len); 387 388 /* Send download command */ 389 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd, 390 HCI_INIT_TIMEOUT); 391 if (IS_ERR(skb)) { 392 rtl_dev_err(hdev, "download fw command failed (%ld)\n", 393 PTR_ERR(skb)); 394 ret = -PTR_ERR(skb); 395 goto out; 396 } 397 398 if (skb->len != sizeof(struct rtl_download_response)) { 399 rtl_dev_err(hdev, "download fw event length mismatch\n"); 400 kfree_skb(skb); 401 ret = -EIO; 402 goto out; 403 } 404 405 kfree_skb(skb); 406 data += RTL_FRAG_LEN; 407 } 408 409 out: 410 kfree(dl_cmd); 411 return ret; 412 } 413 414 static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff) 415 { 416 const struct firmware *fw; 417 int ret; 418 419 rtl_dev_info(hdev, "rtl: loading %s\n", name); 420 ret = request_firmware(&fw, name, &hdev->dev); 421 if (ret < 0) 422 return ret; 423 ret = fw->size; 424 *buff = kmemdup(fw->data, ret, GFP_KERNEL); 425 if (!*buff) 426 ret = -ENOMEM; 427 428 release_firmware(fw); 429 430 return ret; 431 } 432 433 static int btrtl_setup_rtl8723a(struct hci_dev *hdev, 434 struct btrtl_device_info *btrtl_dev) 435 { 436 if (btrtl_dev->fw_len < 8) 437 return -EINVAL; 438 439 /* Check that the firmware doesn't have the epatch signature 440 * (which is only for RTL8723B and newer). 441 */ 442 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) { 443 rtl_dev_err(hdev, "unexpected EPATCH signature!\n"); 444 return -EINVAL; 445 } 446 447 return rtl_download_firmware(hdev, btrtl_dev->fw_data, 448 btrtl_dev->fw_len); 449 } 450 451 static int btrtl_setup_rtl8723b(struct hci_dev *hdev, 452 struct btrtl_device_info *btrtl_dev) 453 { 454 unsigned char *fw_data = NULL; 455 int ret; 456 u8 *tbuff; 457 458 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data); 459 if (ret < 0) 460 goto out; 461 462 if (btrtl_dev->cfg_len > 0) { 463 tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL); 464 if (!tbuff) { 465 ret = -ENOMEM; 466 goto out; 467 } 468 469 memcpy(tbuff, fw_data, ret); 470 kfree(fw_data); 471 472 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len); 473 ret += btrtl_dev->cfg_len; 474 475 fw_data = tbuff; 476 } 477 478 rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret); 479 480 ret = rtl_download_firmware(hdev, fw_data, ret); 481 482 out: 483 kfree(fw_data); 484 return ret; 485 } 486 487 static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev) 488 { 489 struct sk_buff *skb; 490 491 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, 492 HCI_INIT_TIMEOUT); 493 if (IS_ERR(skb)) { 494 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n", 495 PTR_ERR(skb)); 496 return skb; 497 } 498 499 if (skb->len != sizeof(struct hci_rp_read_local_version)) { 500 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n"); 501 kfree_skb(skb); 502 return ERR_PTR(-EIO); 503 } 504 505 return skb; 506 } 507 508 void btrtl_free(struct btrtl_device_info *btrtl_dev) 509 { 510 kfree(btrtl_dev->fw_data); 511 kfree(btrtl_dev->cfg_data); 512 kfree(btrtl_dev); 513 } 514 EXPORT_SYMBOL_GPL(btrtl_free); 515 516 struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev, 517 const char *postfix) 518 { 519 struct btrtl_device_info *btrtl_dev; 520 struct sk_buff *skb; 521 struct hci_rp_read_local_version *resp; 522 char cfg_name[40]; 523 u16 hci_rev, lmp_subver; 524 u8 hci_ver; 525 int ret; 526 527 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL); 528 if (!btrtl_dev) { 529 ret = -ENOMEM; 530 goto err_alloc; 531 } 532 533 skb = btrtl_read_local_version(hdev); 534 if (IS_ERR(skb)) { 535 ret = PTR_ERR(skb); 536 goto err_free; 537 } 538 539 resp = (struct hci_rp_read_local_version *)skb->data; 540 rtl_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n", 541 resp->hci_ver, resp->hci_rev, 542 resp->lmp_ver, resp->lmp_subver); 543 544 hci_ver = resp->hci_ver; 545 hci_rev = le16_to_cpu(resp->hci_rev); 546 lmp_subver = le16_to_cpu(resp->lmp_subver); 547 kfree_skb(skb); 548 549 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, 550 hdev->bus); 551 552 if (!btrtl_dev->ic_info) { 553 rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x", 554 lmp_subver, hci_rev, hci_ver); 555 return btrtl_dev; 556 } 557 558 if (btrtl_dev->ic_info->has_rom_version) { 559 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version); 560 if (ret) 561 goto err_free; 562 } 563 564 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name, 565 &btrtl_dev->fw_data); 566 if (btrtl_dev->fw_len < 0) { 567 rtl_dev_err(hdev, "firmware file %s not found\n", 568 btrtl_dev->ic_info->fw_name); 569 ret = btrtl_dev->fw_len; 570 goto err_free; 571 } 572 573 if (btrtl_dev->ic_info->cfg_name) { 574 if (postfix) { 575 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin", 576 btrtl_dev->ic_info->cfg_name, postfix); 577 } else { 578 snprintf(cfg_name, sizeof(cfg_name), "%s.bin", 579 btrtl_dev->ic_info->cfg_name); 580 } 581 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name, 582 &btrtl_dev->cfg_data); 583 if (btrtl_dev->ic_info->config_needed && 584 btrtl_dev->cfg_len <= 0) { 585 rtl_dev_err(hdev, "mandatory config file %s not found\n", 586 btrtl_dev->ic_info->cfg_name); 587 ret = btrtl_dev->cfg_len; 588 goto err_free; 589 } 590 } 591 592 return btrtl_dev; 593 594 err_free: 595 btrtl_free(btrtl_dev); 596 err_alloc: 597 return ERR_PTR(ret); 598 } 599 EXPORT_SYMBOL_GPL(btrtl_initialize); 600 601 int btrtl_download_firmware(struct hci_dev *hdev, 602 struct btrtl_device_info *btrtl_dev) 603 { 604 /* Match a set of subver values that correspond to stock firmware, 605 * which is not compatible with standard btusb. 606 * If matched, upload an alternative firmware that does conform to 607 * standard btusb. Once that firmware is uploaded, the subver changes 608 * to a different value. 609 */ 610 if (!btrtl_dev->ic_info) { 611 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n"); 612 return 0; 613 } 614 615 switch (btrtl_dev->ic_info->lmp_subver) { 616 case RTL_ROM_LMP_8723A: 617 case RTL_ROM_LMP_3499: 618 return btrtl_setup_rtl8723a(hdev, btrtl_dev); 619 case RTL_ROM_LMP_8723B: 620 case RTL_ROM_LMP_8821A: 621 case RTL_ROM_LMP_8761A: 622 case RTL_ROM_LMP_8822B: 623 return btrtl_setup_rtl8723b(hdev, btrtl_dev); 624 default: 625 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n"); 626 return 0; 627 } 628 } 629 EXPORT_SYMBOL_GPL(btrtl_download_firmware); 630 631 int btrtl_setup_realtek(struct hci_dev *hdev) 632 { 633 struct btrtl_device_info *btrtl_dev; 634 int ret; 635 636 btrtl_dev = btrtl_initialize(hdev, NULL); 637 if (IS_ERR(btrtl_dev)) 638 return PTR_ERR(btrtl_dev); 639 640 ret = btrtl_download_firmware(hdev, btrtl_dev); 641 642 btrtl_free(btrtl_dev); 643 644 return ret; 645 } 646 EXPORT_SYMBOL_GPL(btrtl_setup_realtek); 647 648 int btrtl_shutdown_realtek(struct hci_dev *hdev) 649 { 650 struct sk_buff *skb; 651 int ret; 652 653 /* According to the vendor driver, BT must be reset on close to avoid 654 * firmware crash. 655 */ 656 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); 657 if (IS_ERR(skb)) { 658 ret = PTR_ERR(skb); 659 bt_dev_err(hdev, "HCI reset during shutdown failed"); 660 return ret; 661 } 662 kfree_skb(skb); 663 664 return 0; 665 } 666 EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek); 667 668 static unsigned int btrtl_convert_baudrate(u32 device_baudrate) 669 { 670 switch (device_baudrate) { 671 case 0x0252a00a: 672 return 230400; 673 674 case 0x05f75004: 675 return 921600; 676 677 case 0x00005004: 678 return 1000000; 679 680 case 0x04928002: 681 case 0x01128002: 682 return 1500000; 683 684 case 0x00005002: 685 return 2000000; 686 687 case 0x0000b001: 688 return 2500000; 689 690 case 0x04928001: 691 return 3000000; 692 693 case 0x052a6001: 694 return 3500000; 695 696 case 0x00005001: 697 return 4000000; 698 699 case 0x0252c014: 700 default: 701 return 115200; 702 } 703 } 704 705 int btrtl_get_uart_settings(struct hci_dev *hdev, 706 struct btrtl_device_info *btrtl_dev, 707 unsigned int *controller_baudrate, 708 u32 *device_baudrate, bool *flow_control) 709 { 710 struct rtl_vendor_config *config; 711 struct rtl_vendor_config_entry *entry; 712 int i, total_data_len; 713 bool found = false; 714 715 total_data_len = btrtl_dev->cfg_len - sizeof(*config); 716 if (total_data_len <= 0) { 717 rtl_dev_warn(hdev, "no config loaded\n"); 718 return -EINVAL; 719 } 720 721 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data; 722 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) { 723 rtl_dev_err(hdev, "invalid config magic\n"); 724 return -EINVAL; 725 } 726 727 if (total_data_len < le16_to_cpu(config->total_len)) { 728 rtl_dev_err(hdev, "config is too short\n"); 729 return -EINVAL; 730 } 731 732 for (i = 0; i < total_data_len; ) { 733 entry = ((void *)config->entry) + i; 734 735 switch (le16_to_cpu(entry->offset)) { 736 case 0xc: 737 if (entry->len < sizeof(*device_baudrate)) { 738 rtl_dev_err(hdev, "invalid UART config entry\n"); 739 return -EINVAL; 740 } 741 742 *device_baudrate = get_unaligned_le32(entry->data); 743 *controller_baudrate = btrtl_convert_baudrate( 744 *device_baudrate); 745 746 if (entry->len >= 13) 747 *flow_control = !!(entry->data[12] & BIT(2)); 748 else 749 *flow_control = false; 750 751 found = true; 752 break; 753 754 default: 755 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n", 756 le16_to_cpu(entry->offset), entry->len); 757 break; 758 }; 759 760 i += sizeof(*entry) + entry->len; 761 } 762 763 if (!found) { 764 rtl_dev_err(hdev, "no UART config entry found\n"); 765 return -ENOENT; 766 } 767 768 rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate); 769 rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate); 770 rtl_dev_dbg(hdev, "flow control %d\n", *flow_control); 771 772 return 0; 773 } 774 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings); 775 776 MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>"); 777 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION); 778 MODULE_VERSION(VERSION); 779 MODULE_LICENSE("GPL"); 780 MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin"); 781 MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin"); 782 MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin"); 783 MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin"); 784 MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin"); 785 MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin"); 786 MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin"); 787 MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin"); 788 MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin"); 789 MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin"); 790 MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin"); 791 MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin"); 792 MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin"); 793