1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Bluetooth support for Realtek devices
5 * Copyright (C) 2015 Endless Mobile, Inc.
8 #include <linux/module.h>
9 #include <linux/firmware.h>
10 #include <asm/unaligned.h>
11 #include <linux/usb.h>
13 #include <net/bluetooth/bluetooth.h>
14 #include <net/bluetooth/hci_core.h>
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
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), \
69 struct btrtl_device_info {
70 const struct id_table *ic_info;
80 static const struct id_table ic_id_table[] = {
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",
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
194 { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_USB),
195 .config_needed = false,
196 .has_rom_version = true,
197 .has_msft_ext = true,
198 .fw_name = "rtl_bt/rtl8852bu_fw.bin",
199 .cfg_name = "rtl_bt/rtl8852bu_config" },
202 { IC_INFO(RTL_ROM_LMP_8852A, 0xc, 0xc, HCI_USB),
203 .config_needed = false,
204 .has_rom_version = true,
205 .has_msft_ext = true,
206 .fw_name = "rtl_bt/rtl8852cu_fw.bin",
207 .cfg_name = "rtl_bt/rtl8852cu_config" },
210 static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
211 u8 hci_ver, u8 hci_bus)
215 for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
216 if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
217 (ic_id_table[i].lmp_subver != lmp_subver))
219 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
220 (ic_id_table[i].hci_rev != hci_rev))
222 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
223 (ic_id_table[i].hci_ver != hci_ver))
225 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
226 (ic_id_table[i].hci_bus != hci_bus))
231 if (i >= ARRAY_SIZE(ic_id_table))
234 return &ic_id_table[i];
237 static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
241 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
244 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)",
249 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
250 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch");
252 return ERR_PTR(-EIO);
258 static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
260 struct rtl_rom_version_evt *rom_version;
263 /* Read RTL ROM version command */
264 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
266 rtl_dev_err(hdev, "Read ROM version failed (%ld)",
271 if (skb->len != sizeof(*rom_version)) {
272 rtl_dev_err(hdev, "version event length mismatch");
277 rom_version = (struct rtl_rom_version_evt *)skb->data;
278 rtl_dev_info(hdev, "rom_version status=%x version=%x",
279 rom_version->status, rom_version->version);
281 *version = rom_version->version;
287 static int rtlbt_parse_firmware(struct hci_dev *hdev,
288 struct btrtl_device_info *btrtl_dev,
289 unsigned char **_buf)
291 static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
292 struct rtl_epatch_header *epatch_info;
296 u8 opcode, length, data;
298 const unsigned char *fwptr, *chip_id_base;
299 const unsigned char *patch_length_base, *patch_offset_base;
300 u32 patch_offset = 0;
301 u16 patch_length, num_patches;
302 static const struct {
305 } project_id_to_lmp_subver[] = {
306 { RTL_ROM_LMP_8723A, 0 },
307 { RTL_ROM_LMP_8723B, 1 },
308 { RTL_ROM_LMP_8821A, 2 },
309 { RTL_ROM_LMP_8761A, 3 },
310 { RTL_ROM_LMP_8822B, 8 },
311 { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
312 { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
313 { RTL_ROM_LMP_8822B, 13 }, /* 8822C */
314 { RTL_ROM_LMP_8761A, 14 }, /* 8761B */
315 { RTL_ROM_LMP_8852A, 18 }, /* 8852A */
316 { RTL_ROM_LMP_8852A, 20 }, /* 8852B */
317 { RTL_ROM_LMP_8852A, 25 }, /* 8852C */
320 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
321 if (btrtl_dev->fw_len < min_size)
324 fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
325 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
326 rtl_dev_err(hdev, "extension section signature mismatch");
330 /* Loop from the end of the firmware parsing instructions, until
331 * we find an instruction that identifies the "project ID" for the
332 * hardware supported by this firwmare file.
333 * Once we have that, we double-check that that project_id is suitable
334 * for the hardware we are working with.
336 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
341 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
343 if (opcode == 0xff) /* EOF */
347 rtl_dev_err(hdev, "found instruction with length 0");
351 if (opcode == 0 && length == 1) {
359 if (project_id < 0) {
360 rtl_dev_err(hdev, "failed to find version instruction");
364 /* Find project_id in table */
365 for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
366 if (project_id == project_id_to_lmp_subver[i].id) {
367 btrtl_dev->project_id = project_id;
372 if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
373 rtl_dev_err(hdev, "unknown project id %d", project_id);
377 if (btrtl_dev->ic_info->lmp_subver !=
378 project_id_to_lmp_subver[i].lmp_subver) {
379 rtl_dev_err(hdev, "firmware is for %x but this is a %x",
380 project_id_to_lmp_subver[i].lmp_subver,
381 btrtl_dev->ic_info->lmp_subver);
385 epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
386 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
387 rtl_dev_err(hdev, "bad EPATCH signature");
391 num_patches = le16_to_cpu(epatch_info->num_patches);
392 BT_DBG("fw_version=%x, num_patches=%d",
393 le32_to_cpu(epatch_info->fw_version), num_patches);
395 /* After the rtl_epatch_header there is a funky patch metadata section.
396 * Assuming 2 patches, the layout is:
397 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
399 * Find the right patch for this chip.
401 min_size += 8 * num_patches;
402 if (btrtl_dev->fw_len < min_size)
405 chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
406 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
407 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
408 for (i = 0; i < num_patches; i++) {
409 u16 chip_id = get_unaligned_le16(chip_id_base +
411 if (chip_id == btrtl_dev->rom_version + 1) {
412 patch_length = get_unaligned_le16(patch_length_base +
414 patch_offset = get_unaligned_le32(patch_offset_base +
421 rtl_dev_err(hdev, "didn't find patch for chip id %d",
422 btrtl_dev->rom_version);
426 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
427 min_size = patch_offset + patch_length;
428 if (btrtl_dev->fw_len < min_size)
431 /* Copy the firmware into a new buffer and write the version at
435 buf = kvmalloc(patch_length, GFP_KERNEL);
439 memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
440 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
446 static int rtl_download_firmware(struct hci_dev *hdev,
447 const unsigned char *data, int fw_len)
449 struct rtl_download_cmd *dl_cmd;
450 int frag_num = fw_len / RTL_FRAG_LEN + 1;
451 int frag_len = RTL_FRAG_LEN;
455 struct hci_rp_read_local_version *rp;
457 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
461 for (i = 0; i < frag_num; i++) {
464 BT_DBG("download fw (%d/%d)", i, frag_num);
467 dl_cmd->index = (i & 0x7f) + 1;
471 if (i == (frag_num - 1)) {
472 dl_cmd->index |= 0x80; /* data end */
473 frag_len = fw_len % RTL_FRAG_LEN;
475 memcpy(dl_cmd->data, data, frag_len);
477 /* Send download command */
478 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
481 rtl_dev_err(hdev, "download fw command failed (%ld)",
487 if (skb->len != sizeof(struct rtl_download_response)) {
488 rtl_dev_err(hdev, "download fw event length mismatch");
495 data += RTL_FRAG_LEN;
498 skb = btrtl_read_local_version(hdev);
501 rtl_dev_err(hdev, "read local version failed");
505 rp = (struct hci_rp_read_local_version *)skb->data;
506 rtl_dev_info(hdev, "fw version 0x%04x%04x",
507 __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver));
515 static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
517 const struct firmware *fw;
520 rtl_dev_info(hdev, "loading %s", name);
521 ret = request_firmware(&fw, name, &hdev->dev);
525 *buff = kvmalloc(fw->size, GFP_KERNEL);
527 memcpy(*buff, fw->data, ret);
531 release_firmware(fw);
536 static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
537 struct btrtl_device_info *btrtl_dev)
539 if (btrtl_dev->fw_len < 8)
542 /* Check that the firmware doesn't have the epatch signature
543 * (which is only for RTL8723B and newer).
545 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
546 rtl_dev_err(hdev, "unexpected EPATCH signature!");
550 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
554 static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
555 struct btrtl_device_info *btrtl_dev)
557 unsigned char *fw_data = NULL;
561 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
565 if (btrtl_dev->cfg_len > 0) {
566 tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
572 memcpy(tbuff, fw_data, ret);
575 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
576 ret += btrtl_dev->cfg_len;
581 rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret);
583 ret = rtl_download_firmware(hdev, fw_data, ret);
590 void btrtl_free(struct btrtl_device_info *btrtl_dev)
592 kvfree(btrtl_dev->fw_data);
593 kvfree(btrtl_dev->cfg_data);
596 EXPORT_SYMBOL_GPL(btrtl_free);
598 struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
601 struct btrtl_device_info *btrtl_dev;
603 struct hci_rp_read_local_version *resp;
605 u16 hci_rev, lmp_subver;
611 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
617 skb = btrtl_read_local_version(hdev);
623 resp = (struct hci_rp_read_local_version *)skb->data;
624 rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
625 resp->hci_ver, resp->hci_rev,
626 resp->lmp_ver, resp->lmp_subver);
628 hci_ver = resp->hci_ver;
629 hci_rev = le16_to_cpu(resp->hci_rev);
630 lmp_subver = le16_to_cpu(resp->lmp_subver);
632 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
635 if (!btrtl_dev->ic_info)
636 btrtl_dev->drop_fw = true;
638 if (btrtl_dev->drop_fw) {
639 opcode = hci_opcode_pack(0x3f, 0x66);
640 cmd[0] = opcode & 0xff;
641 cmd[1] = opcode >> 8;
643 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
647 skb_put_data(skb, cmd, sizeof(cmd));
648 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
650 hdev->send(hdev, skb);
652 /* Ensure the above vendor command is sent to controller and
657 /* Read the local version again. Expect to have the vanilla
658 * version as cold boot.
660 skb = btrtl_read_local_version(hdev);
666 resp = (struct hci_rp_read_local_version *)skb->data;
667 rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
668 resp->hci_ver, resp->hci_rev,
669 resp->lmp_ver, resp->lmp_subver);
671 hci_ver = resp->hci_ver;
672 hci_rev = le16_to_cpu(resp->hci_rev);
673 lmp_subver = le16_to_cpu(resp->lmp_subver);
675 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
681 if (!btrtl_dev->ic_info) {
682 rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
683 lmp_subver, hci_rev, hci_ver);
687 if (btrtl_dev->ic_info->has_rom_version) {
688 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
693 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
694 &btrtl_dev->fw_data);
695 if (btrtl_dev->fw_len < 0) {
696 rtl_dev_err(hdev, "firmware file %s not found",
697 btrtl_dev->ic_info->fw_name);
698 ret = btrtl_dev->fw_len;
702 if (btrtl_dev->ic_info->cfg_name) {
704 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
705 btrtl_dev->ic_info->cfg_name, postfix);
707 snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
708 btrtl_dev->ic_info->cfg_name);
710 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
711 &btrtl_dev->cfg_data);
712 if (btrtl_dev->ic_info->config_needed &&
713 btrtl_dev->cfg_len <= 0) {
714 rtl_dev_err(hdev, "mandatory config file %s not found",
715 btrtl_dev->ic_info->cfg_name);
716 ret = btrtl_dev->cfg_len;
721 /* The following chips supports the Microsoft vendor extension,
722 * therefore set the corresponding VsMsftOpCode.
724 if (btrtl_dev->ic_info->has_msft_ext)
725 hci_set_msft_opcode(hdev, 0xFCF0);
730 btrtl_free(btrtl_dev);
734 EXPORT_SYMBOL_GPL(btrtl_initialize);
736 int btrtl_download_firmware(struct hci_dev *hdev,
737 struct btrtl_device_info *btrtl_dev)
739 /* Match a set of subver values that correspond to stock firmware,
740 * which is not compatible with standard btusb.
741 * If matched, upload an alternative firmware that does conform to
742 * standard btusb. Once that firmware is uploaded, the subver changes
743 * to a different value.
745 if (!btrtl_dev->ic_info) {
746 rtl_dev_info(hdev, "assuming no firmware upload needed");
750 switch (btrtl_dev->ic_info->lmp_subver) {
751 case RTL_ROM_LMP_8723A:
752 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
753 case RTL_ROM_LMP_8723B:
754 case RTL_ROM_LMP_8821A:
755 case RTL_ROM_LMP_8761A:
756 case RTL_ROM_LMP_8822B:
757 case RTL_ROM_LMP_8852A:
758 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
760 rtl_dev_info(hdev, "assuming no firmware upload needed");
764 EXPORT_SYMBOL_GPL(btrtl_download_firmware);
766 void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev)
768 /* Enable controller to do both LE scan and BR/EDR inquiry
771 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
773 /* Enable central-peripheral role (able to create new connections with
774 * an existing connection in slave role).
776 /* Enable WBS supported for the specific Realtek devices. */
777 switch (btrtl_dev->project_id) {
782 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
783 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
784 hci_set_aosp_capable(hdev);
787 rtl_dev_dbg(hdev, "Central-peripheral role not enabled.");
788 rtl_dev_dbg(hdev, "WBS supported not enabled.");
792 EXPORT_SYMBOL_GPL(btrtl_set_quirks);
794 int btrtl_setup_realtek(struct hci_dev *hdev)
796 struct btrtl_device_info *btrtl_dev;
799 btrtl_dev = btrtl_initialize(hdev, NULL);
800 if (IS_ERR(btrtl_dev))
801 return PTR_ERR(btrtl_dev);
803 ret = btrtl_download_firmware(hdev, btrtl_dev);
805 btrtl_set_quirks(hdev, btrtl_dev);
807 btrtl_free(btrtl_dev);
810 EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
812 int btrtl_shutdown_realtek(struct hci_dev *hdev)
817 /* According to the vendor driver, BT must be reset on close to avoid
820 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
823 bt_dev_err(hdev, "HCI reset during shutdown failed");
830 EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
832 static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
834 switch (device_baudrate) {
869 int btrtl_get_uart_settings(struct hci_dev *hdev,
870 struct btrtl_device_info *btrtl_dev,
871 unsigned int *controller_baudrate,
872 u32 *device_baudrate, bool *flow_control)
874 struct rtl_vendor_config *config;
875 struct rtl_vendor_config_entry *entry;
876 int i, total_data_len;
879 total_data_len = btrtl_dev->cfg_len - sizeof(*config);
880 if (total_data_len <= 0) {
881 rtl_dev_warn(hdev, "no config loaded");
885 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
886 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
887 rtl_dev_err(hdev, "invalid config magic");
891 if (total_data_len < le16_to_cpu(config->total_len)) {
892 rtl_dev_err(hdev, "config is too short");
896 for (i = 0; i < total_data_len; ) {
897 entry = ((void *)config->entry) + i;
899 switch (le16_to_cpu(entry->offset)) {
901 if (entry->len < sizeof(*device_baudrate)) {
902 rtl_dev_err(hdev, "invalid UART config entry");
906 *device_baudrate = get_unaligned_le32(entry->data);
907 *controller_baudrate = btrtl_convert_baudrate(
910 if (entry->len >= 13)
911 *flow_control = !!(entry->data[12] & BIT(2));
913 *flow_control = false;
919 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)",
920 le16_to_cpu(entry->offset), entry->len);
924 i += sizeof(*entry) + entry->len;
928 rtl_dev_err(hdev, "no UART config entry found");
932 rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate);
933 rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate);
934 rtl_dev_dbg(hdev, "flow control %d", *flow_control);
938 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
940 MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
941 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
942 MODULE_VERSION(VERSION);
943 MODULE_LICENSE("GPL");
944 MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
945 MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
946 MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
947 MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
948 MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
949 MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
950 MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
951 MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
952 MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
953 MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
954 MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
955 MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
956 MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
957 MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin");
958 MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin");
959 MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin");
960 MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin");
961 MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin");
962 MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin");