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_3499 0x3499
22 #define RTL_ROM_LMP_8723A 0x1200
23 #define RTL_ROM_LMP_8723B 0x8723
24 #define RTL_ROM_LMP_8821A 0x8821
25 #define RTL_ROM_LMP_8761A 0x8761
26 #define RTL_ROM_LMP_8822B 0x8822
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) \
34 .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
35 .lmp_subver = (lmps), \
50 struct btrtl_device_info
{
51 const struct id_table
*ic_info
;
59 static const struct id_table ic_id_table
[] = {
60 { IC_MATCH_FL_LMPSUBV
, RTL_ROM_LMP_8723A
, 0x0,
61 .config_needed
= false,
62 .has_rom_version
= false,
63 .fw_name
= "rtl_bt/rtl8723a_fw.bin",
66 { IC_MATCH_FL_LMPSUBV
, RTL_ROM_LMP_3499
, 0x0,
67 .config_needed
= false,
68 .has_rom_version
= false,
69 .fw_name
= "rtl_bt/rtl8723a_fw.bin",
73 { .match_flags
= IC_MATCH_FL_LMPSUBV
| IC_MATCH_FL_HCIREV
|
74 IC_MATCH_FL_HCIVER
| IC_MATCH_FL_HCIBUS
,
75 .lmp_subver
= RTL_ROM_LMP_8723B
,
79 .config_needed
= true,
80 .has_rom_version
= true,
81 .fw_name
= "rtl_bt/rtl8723bs_fw.bin",
82 .cfg_name
= "rtl_bt/rtl8723bs_config" },
85 { IC_INFO(RTL_ROM_LMP_8723B
, 0xb),
86 .config_needed
= false,
87 .has_rom_version
= true,
88 .fw_name
= "rtl_bt/rtl8723b_fw.bin",
89 .cfg_name
= "rtl_bt/rtl8723b_config" },
92 { IC_INFO(RTL_ROM_LMP_8723B
, 0xd),
93 .config_needed
= true,
94 .has_rom_version
= true,
95 .fw_name
= "rtl_bt/rtl8723d_fw.bin",
96 .cfg_name
= "rtl_bt/rtl8723d_config" },
99 { .match_flags
= IC_MATCH_FL_LMPSUBV
| IC_MATCH_FL_HCIREV
|
100 IC_MATCH_FL_HCIVER
| IC_MATCH_FL_HCIBUS
,
101 .lmp_subver
= RTL_ROM_LMP_8723B
,
105 .config_needed
= true,
106 .has_rom_version
= true,
107 .fw_name
= "rtl_bt/rtl8723ds_fw.bin",
108 .cfg_name
= "rtl_bt/rtl8723ds_config" },
111 { IC_INFO(RTL_ROM_LMP_8821A
, 0xa),
112 .config_needed
= false,
113 .has_rom_version
= true,
114 .fw_name
= "rtl_bt/rtl8821a_fw.bin",
115 .cfg_name
= "rtl_bt/rtl8821a_config" },
118 { IC_INFO(RTL_ROM_LMP_8821A
, 0xc),
119 .config_needed
= false,
120 .has_rom_version
= true,
121 .fw_name
= "rtl_bt/rtl8821c_fw.bin",
122 .cfg_name
= "rtl_bt/rtl8821c_config" },
125 { IC_MATCH_FL_LMPSUBV
, RTL_ROM_LMP_8761A
, 0x0,
126 .config_needed
= false,
127 .has_rom_version
= true,
128 .fw_name
= "rtl_bt/rtl8761a_fw.bin",
129 .cfg_name
= "rtl_bt/rtl8761a_config" },
131 /* 8822C with USB interface */
132 { IC_INFO(RTL_ROM_LMP_8822B
, 0xc),
133 .config_needed
= false,
134 .has_rom_version
= true,
135 .fw_name
= "rtl_bt/rtl8822cu_fw.bin",
136 .cfg_name
= "rtl_bt/rtl8822cu_config" },
139 { IC_INFO(RTL_ROM_LMP_8822B
, 0xb),
140 .config_needed
= true,
141 .has_rom_version
= true,
142 .fw_name
= "rtl_bt/rtl8822b_fw.bin",
143 .cfg_name
= "rtl_bt/rtl8822b_config" },
146 static const struct id_table
*btrtl_match_ic(u16 lmp_subver
, u16 hci_rev
,
147 u8 hci_ver
, u8 hci_bus
)
151 for (i
= 0; i
< ARRAY_SIZE(ic_id_table
); i
++) {
152 if ((ic_id_table
[i
].match_flags
& IC_MATCH_FL_LMPSUBV
) &&
153 (ic_id_table
[i
].lmp_subver
!= lmp_subver
))
155 if ((ic_id_table
[i
].match_flags
& IC_MATCH_FL_HCIREV
) &&
156 (ic_id_table
[i
].hci_rev
!= hci_rev
))
158 if ((ic_id_table
[i
].match_flags
& IC_MATCH_FL_HCIVER
) &&
159 (ic_id_table
[i
].hci_ver
!= hci_ver
))
161 if ((ic_id_table
[i
].match_flags
& IC_MATCH_FL_HCIBUS
) &&
162 (ic_id_table
[i
].hci_bus
!= hci_bus
))
167 if (i
>= ARRAY_SIZE(ic_id_table
))
170 return &ic_id_table
[i
];
173 static int rtl_read_rom_version(struct hci_dev
*hdev
, u8
*version
)
175 struct rtl_rom_version_evt
*rom_version
;
178 /* Read RTL ROM version command */
179 skb
= __hci_cmd_sync(hdev
, 0xfc6d, 0, NULL
, HCI_INIT_TIMEOUT
);
181 rtl_dev_err(hdev
, "Read ROM version failed (%ld)\n",
186 if (skb
->len
!= sizeof(*rom_version
)) {
187 rtl_dev_err(hdev
, "RTL version event length mismatch\n");
192 rom_version
= (struct rtl_rom_version_evt
*)skb
->data
;
193 rtl_dev_info(hdev
, "rom_version status=%x version=%x\n",
194 rom_version
->status
, rom_version
->version
);
196 *version
= rom_version
->version
;
202 static int rtlbt_parse_firmware(struct hci_dev
*hdev
,
203 struct btrtl_device_info
*btrtl_dev
,
204 unsigned char **_buf
)
206 static const u8 extension_sig
[] = { 0x51, 0x04, 0xfd, 0x77 };
207 struct rtl_epatch_header
*epatch_info
;
211 u8 opcode
, length
, data
;
213 const unsigned char *fwptr
, *chip_id_base
;
214 const unsigned char *patch_length_base
, *patch_offset_base
;
215 u32 patch_offset
= 0;
216 u16 patch_length
, num_patches
;
217 static const struct {
220 } project_id_to_lmp_subver
[] = {
221 { RTL_ROM_LMP_8723A
, 0 },
222 { RTL_ROM_LMP_8723B
, 1 },
223 { RTL_ROM_LMP_8821A
, 2 },
224 { RTL_ROM_LMP_8761A
, 3 },
225 { RTL_ROM_LMP_8822B
, 8 },
226 { RTL_ROM_LMP_8723B
, 9 }, /* 8723D */
227 { RTL_ROM_LMP_8821A
, 10 }, /* 8821C */
228 { RTL_ROM_LMP_8822B
, 13 }, /* 8822C */
231 min_size
= sizeof(struct rtl_epatch_header
) + sizeof(extension_sig
) + 3;
232 if (btrtl_dev
->fw_len
< min_size
)
235 fwptr
= btrtl_dev
->fw_data
+ btrtl_dev
->fw_len
- sizeof(extension_sig
);
236 if (memcmp(fwptr
, extension_sig
, sizeof(extension_sig
)) != 0) {
237 rtl_dev_err(hdev
, "extension section signature mismatch\n");
241 /* Loop from the end of the firmware parsing instructions, until
242 * we find an instruction that identifies the "project ID" for the
243 * hardware supported by this firwmare file.
244 * Once we have that, we double-check that that project_id is suitable
245 * for the hardware we are working with.
247 while (fwptr
>= btrtl_dev
->fw_data
+ (sizeof(*epatch_info
) + 3)) {
252 BT_DBG("check op=%x len=%x data=%x", opcode
, length
, data
);
254 if (opcode
== 0xff) /* EOF */
258 rtl_dev_err(hdev
, "found instruction with length 0\n");
262 if (opcode
== 0 && length
== 1) {
270 if (project_id
< 0) {
271 rtl_dev_err(hdev
, "failed to find version instruction\n");
275 /* Find project_id in table */
276 for (i
= 0; i
< ARRAY_SIZE(project_id_to_lmp_subver
); i
++) {
277 if (project_id
== project_id_to_lmp_subver
[i
].id
)
281 if (i
>= ARRAY_SIZE(project_id_to_lmp_subver
)) {
282 rtl_dev_err(hdev
, "unknown project id %d\n", project_id
);
286 if (btrtl_dev
->ic_info
->lmp_subver
!=
287 project_id_to_lmp_subver
[i
].lmp_subver
) {
288 rtl_dev_err(hdev
, "firmware is for %x but this is a %x\n",
289 project_id_to_lmp_subver
[i
].lmp_subver
,
290 btrtl_dev
->ic_info
->lmp_subver
);
294 epatch_info
= (struct rtl_epatch_header
*)btrtl_dev
->fw_data
;
295 if (memcmp(epatch_info
->signature
, RTL_EPATCH_SIGNATURE
, 8) != 0) {
296 rtl_dev_err(hdev
, "bad EPATCH signature\n");
300 num_patches
= le16_to_cpu(epatch_info
->num_patches
);
301 BT_DBG("fw_version=%x, num_patches=%d",
302 le32_to_cpu(epatch_info
->fw_version
), num_patches
);
304 /* After the rtl_epatch_header there is a funky patch metadata section.
305 * Assuming 2 patches, the layout is:
306 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
308 * Find the right patch for this chip.
310 min_size
+= 8 * num_patches
;
311 if (btrtl_dev
->fw_len
< min_size
)
314 chip_id_base
= btrtl_dev
->fw_data
+ sizeof(struct rtl_epatch_header
);
315 patch_length_base
= chip_id_base
+ (sizeof(u16
) * num_patches
);
316 patch_offset_base
= patch_length_base
+ (sizeof(u16
) * num_patches
);
317 for (i
= 0; i
< num_patches
; i
++) {
318 u16 chip_id
= get_unaligned_le16(chip_id_base
+
320 if (chip_id
== btrtl_dev
->rom_version
+ 1) {
321 patch_length
= get_unaligned_le16(patch_length_base
+
323 patch_offset
= get_unaligned_le32(patch_offset_base
+
330 rtl_dev_err(hdev
, "didn't find patch for chip id %d",
331 btrtl_dev
->rom_version
);
335 BT_DBG("length=%x offset=%x index %d", patch_length
, patch_offset
, i
);
336 min_size
= patch_offset
+ patch_length
;
337 if (btrtl_dev
->fw_len
< min_size
)
340 /* Copy the firmware into a new buffer and write the version at
344 buf
= kmemdup(btrtl_dev
->fw_data
+ patch_offset
, patch_length
,
349 memcpy(buf
+ patch_length
- 4, &epatch_info
->fw_version
, 4);
355 static int rtl_download_firmware(struct hci_dev
*hdev
,
356 const unsigned char *data
, int fw_len
)
358 struct rtl_download_cmd
*dl_cmd
;
359 int frag_num
= fw_len
/ RTL_FRAG_LEN
+ 1;
360 int frag_len
= RTL_FRAG_LEN
;
364 dl_cmd
= kmalloc(sizeof(struct rtl_download_cmd
), GFP_KERNEL
);
368 for (i
= 0; i
< frag_num
; i
++) {
371 BT_DBG("download fw (%d/%d)", i
, frag_num
);
374 if (i
== (frag_num
- 1)) {
375 dl_cmd
->index
|= 0x80; /* data end */
376 frag_len
= fw_len
% RTL_FRAG_LEN
;
378 memcpy(dl_cmd
->data
, data
, frag_len
);
380 /* Send download command */
381 skb
= __hci_cmd_sync(hdev
, 0xfc20, frag_len
+ 1, dl_cmd
,
384 rtl_dev_err(hdev
, "download fw command failed (%ld)\n",
390 if (skb
->len
!= sizeof(struct rtl_download_response
)) {
391 rtl_dev_err(hdev
, "download fw event length mismatch\n");
398 data
+= RTL_FRAG_LEN
;
406 static int rtl_load_file(struct hci_dev
*hdev
, const char *name
, u8
**buff
)
408 const struct firmware
*fw
;
411 rtl_dev_info(hdev
, "rtl: loading %s\n", name
);
412 ret
= request_firmware(&fw
, name
, &hdev
->dev
);
416 *buff
= kmemdup(fw
->data
, ret
, GFP_KERNEL
);
420 release_firmware(fw
);
425 static int btrtl_setup_rtl8723a(struct hci_dev
*hdev
,
426 struct btrtl_device_info
*btrtl_dev
)
428 if (btrtl_dev
->fw_len
< 8)
431 /* Check that the firmware doesn't have the epatch signature
432 * (which is only for RTL8723B and newer).
434 if (!memcmp(btrtl_dev
->fw_data
, RTL_EPATCH_SIGNATURE
, 8)) {
435 rtl_dev_err(hdev
, "unexpected EPATCH signature!\n");
439 return rtl_download_firmware(hdev
, btrtl_dev
->fw_data
,
443 static int btrtl_setup_rtl8723b(struct hci_dev
*hdev
,
444 struct btrtl_device_info
*btrtl_dev
)
446 unsigned char *fw_data
= NULL
;
450 ret
= rtlbt_parse_firmware(hdev
, btrtl_dev
, &fw_data
);
454 if (btrtl_dev
->cfg_len
> 0) {
455 tbuff
= kzalloc(ret
+ btrtl_dev
->cfg_len
, GFP_KERNEL
);
461 memcpy(tbuff
, fw_data
, ret
);
464 memcpy(tbuff
+ ret
, btrtl_dev
->cfg_data
, btrtl_dev
->cfg_len
);
465 ret
+= btrtl_dev
->cfg_len
;
470 rtl_dev_info(hdev
, "cfg_sz %d, total sz %d\n", btrtl_dev
->cfg_len
, ret
);
472 ret
= rtl_download_firmware(hdev
, fw_data
, ret
);
479 static struct sk_buff
*btrtl_read_local_version(struct hci_dev
*hdev
)
483 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
486 rtl_dev_err(hdev
, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
491 if (skb
->len
!= sizeof(struct hci_rp_read_local_version
)) {
492 rtl_dev_err(hdev
, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
494 return ERR_PTR(-EIO
);
500 void btrtl_free(struct btrtl_device_info
*btrtl_dev
)
502 kfree(btrtl_dev
->fw_data
);
503 kfree(btrtl_dev
->cfg_data
);
506 EXPORT_SYMBOL_GPL(btrtl_free
);
508 struct btrtl_device_info
*btrtl_initialize(struct hci_dev
*hdev
,
511 struct btrtl_device_info
*btrtl_dev
;
513 struct hci_rp_read_local_version
*resp
;
515 u16 hci_rev
, lmp_subver
;
519 btrtl_dev
= kzalloc(sizeof(*btrtl_dev
), GFP_KERNEL
);
525 skb
= btrtl_read_local_version(hdev
);
531 resp
= (struct hci_rp_read_local_version
*)skb
->data
;
532 rtl_dev_info(hdev
, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
533 resp
->hci_ver
, resp
->hci_rev
,
534 resp
->lmp_ver
, resp
->lmp_subver
);
536 hci_ver
= resp
->hci_ver
;
537 hci_rev
= le16_to_cpu(resp
->hci_rev
);
538 lmp_subver
= le16_to_cpu(resp
->lmp_subver
);
541 btrtl_dev
->ic_info
= btrtl_match_ic(lmp_subver
, hci_rev
, hci_ver
,
544 if (!btrtl_dev
->ic_info
) {
545 rtl_dev_info(hdev
, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
546 lmp_subver
, hci_rev
, hci_ver
);
550 if (btrtl_dev
->ic_info
->has_rom_version
) {
551 ret
= rtl_read_rom_version(hdev
, &btrtl_dev
->rom_version
);
556 btrtl_dev
->fw_len
= rtl_load_file(hdev
, btrtl_dev
->ic_info
->fw_name
,
557 &btrtl_dev
->fw_data
);
558 if (btrtl_dev
->fw_len
< 0) {
559 rtl_dev_err(hdev
, "firmware file %s not found\n",
560 btrtl_dev
->ic_info
->fw_name
);
561 ret
= btrtl_dev
->fw_len
;
565 if (btrtl_dev
->ic_info
->cfg_name
) {
567 snprintf(cfg_name
, sizeof(cfg_name
), "%s-%s.bin",
568 btrtl_dev
->ic_info
->cfg_name
, postfix
);
570 snprintf(cfg_name
, sizeof(cfg_name
), "%s.bin",
571 btrtl_dev
->ic_info
->cfg_name
);
573 btrtl_dev
->cfg_len
= rtl_load_file(hdev
, cfg_name
,
574 &btrtl_dev
->cfg_data
);
575 if (btrtl_dev
->ic_info
->config_needed
&&
576 btrtl_dev
->cfg_len
<= 0) {
577 rtl_dev_err(hdev
, "mandatory config file %s not found\n",
578 btrtl_dev
->ic_info
->cfg_name
);
579 ret
= btrtl_dev
->cfg_len
;
587 btrtl_free(btrtl_dev
);
591 EXPORT_SYMBOL_GPL(btrtl_initialize
);
593 int btrtl_download_firmware(struct hci_dev
*hdev
,
594 struct btrtl_device_info
*btrtl_dev
)
596 /* Match a set of subver values that correspond to stock firmware,
597 * which is not compatible with standard btusb.
598 * If matched, upload an alternative firmware that does conform to
599 * standard btusb. Once that firmware is uploaded, the subver changes
600 * to a different value.
602 if (!btrtl_dev
->ic_info
) {
603 rtl_dev_info(hdev
, "rtl: assuming no firmware upload needed\n");
607 switch (btrtl_dev
->ic_info
->lmp_subver
) {
608 case RTL_ROM_LMP_8723A
:
609 case RTL_ROM_LMP_3499
:
610 return btrtl_setup_rtl8723a(hdev
, btrtl_dev
);
611 case RTL_ROM_LMP_8723B
:
612 case RTL_ROM_LMP_8821A
:
613 case RTL_ROM_LMP_8761A
:
614 case RTL_ROM_LMP_8822B
:
615 return btrtl_setup_rtl8723b(hdev
, btrtl_dev
);
617 rtl_dev_info(hdev
, "rtl: assuming no firmware upload needed\n");
621 EXPORT_SYMBOL_GPL(btrtl_download_firmware
);
623 int btrtl_setup_realtek(struct hci_dev
*hdev
)
625 struct btrtl_device_info
*btrtl_dev
;
628 btrtl_dev
= btrtl_initialize(hdev
, NULL
);
629 if (IS_ERR(btrtl_dev
))
630 return PTR_ERR(btrtl_dev
);
632 ret
= btrtl_download_firmware(hdev
, btrtl_dev
);
634 btrtl_free(btrtl_dev
);
638 EXPORT_SYMBOL_GPL(btrtl_setup_realtek
);
640 static unsigned int btrtl_convert_baudrate(u32 device_baudrate
)
642 switch (device_baudrate
) {
677 int btrtl_get_uart_settings(struct hci_dev
*hdev
,
678 struct btrtl_device_info
*btrtl_dev
,
679 unsigned int *controller_baudrate
,
680 u32
*device_baudrate
, bool *flow_control
)
682 struct rtl_vendor_config
*config
;
683 struct rtl_vendor_config_entry
*entry
;
684 int i
, total_data_len
;
687 total_data_len
= btrtl_dev
->cfg_len
- sizeof(*config
);
688 if (total_data_len
<= 0) {
689 rtl_dev_warn(hdev
, "no config loaded\n");
693 config
= (struct rtl_vendor_config
*)btrtl_dev
->cfg_data
;
694 if (le32_to_cpu(config
->signature
) != RTL_CONFIG_MAGIC
) {
695 rtl_dev_err(hdev
, "invalid config magic\n");
699 if (total_data_len
< le16_to_cpu(config
->total_len
)) {
700 rtl_dev_err(hdev
, "config is too short\n");
704 for (i
= 0; i
< total_data_len
; ) {
705 entry
= ((void *)config
->entry
) + i
;
707 switch (le16_to_cpu(entry
->offset
)) {
709 if (entry
->len
< sizeof(*device_baudrate
)) {
710 rtl_dev_err(hdev
, "invalid UART config entry\n");
714 *device_baudrate
= get_unaligned_le32(entry
->data
);
715 *controller_baudrate
= btrtl_convert_baudrate(
718 if (entry
->len
>= 13)
719 *flow_control
= !!(entry
->data
[12] & BIT(2));
721 *flow_control
= false;
727 rtl_dev_dbg(hdev
, "skipping config entry 0x%x (len %u)\n",
728 le16_to_cpu(entry
->offset
), entry
->len
);
732 i
+= sizeof(*entry
) + entry
->len
;
736 rtl_dev_err(hdev
, "no UART config entry found\n");
740 rtl_dev_dbg(hdev
, "device baudrate = 0x%08x\n", *device_baudrate
);
741 rtl_dev_dbg(hdev
, "controller baudrate = %u\n", *controller_baudrate
);
742 rtl_dev_dbg(hdev
, "flow control %d\n", *flow_control
);
746 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings
);
748 MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
749 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION
);
750 MODULE_VERSION(VERSION
);
751 MODULE_LICENSE("GPL");
752 MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
753 MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
754 MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
755 MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
756 MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
757 MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
758 MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
759 MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
760 MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
761 MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
762 MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
763 MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
764 MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");