2 * Bluetooth support for Realtek devices
4 * Copyright (C) 2015 Endless Mobile, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/firmware.h>
20 #include <asm/unaligned.h>
21 #include <linux/usb.h>
23 #include <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
30 #define RTL_EPATCH_SIGNATURE "Realtech"
31 #define RTL_ROM_LMP_3499 0x3499
32 #define RTL_ROM_LMP_8723A 0x1200
33 #define RTL_ROM_LMP_8723B 0x8723
34 #define RTL_ROM_LMP_8821A 0x8821
35 #define RTL_ROM_LMP_8761A 0x8761
36 #define RTL_ROM_LMP_8822B 0x8822
37 #define RTL_CONFIG_MAGIC 0x8723ab55
39 #define IC_MATCH_FL_LMPSUBV (1 << 0)
40 #define IC_MATCH_FL_HCIREV (1 << 1)
41 #define IC_MATCH_FL_HCIVER (1 << 2)
42 #define IC_MATCH_FL_HCIBUS (1 << 3)
43 #define IC_INFO(lmps, hcir) \
44 .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
45 .lmp_subver = (lmps), \
60 struct btrtl_device_info
{
61 const struct id_table
*ic_info
;
69 static const struct id_table ic_id_table
[] = {
70 { IC_MATCH_FL_LMPSUBV
, RTL_ROM_LMP_8723A
, 0x0,
71 .config_needed
= false,
72 .has_rom_version
= false,
73 .fw_name
= "rtl_bt/rtl8723a_fw.bin",
76 { IC_MATCH_FL_LMPSUBV
, RTL_ROM_LMP_3499
, 0x0,
77 .config_needed
= false,
78 .has_rom_version
= false,
79 .fw_name
= "rtl_bt/rtl8723a_fw.bin",
83 { .match_flags
= IC_MATCH_FL_LMPSUBV
| IC_MATCH_FL_HCIREV
|
84 IC_MATCH_FL_HCIVER
| IC_MATCH_FL_HCIBUS
,
85 .lmp_subver
= RTL_ROM_LMP_8723B
,
89 .config_needed
= true,
90 .has_rom_version
= true,
91 .fw_name
= "rtl_bt/rtl8723bs_fw.bin",
92 .cfg_name
= "rtl_bt/rtl8723bs_config" },
95 { IC_INFO(RTL_ROM_LMP_8723B
, 0xb),
96 .config_needed
= false,
97 .has_rom_version
= true,
98 .fw_name
= "rtl_bt/rtl8723b_fw.bin",
99 .cfg_name
= "rtl_bt/rtl8723b_config" },
102 { IC_INFO(RTL_ROM_LMP_8723B
, 0xd),
103 .config_needed
= true,
104 .has_rom_version
= true,
105 .fw_name
= "rtl_bt/rtl8723d_fw.bin",
106 .cfg_name
= "rtl_bt/rtl8723d_config" },
109 { .match_flags
= IC_MATCH_FL_LMPSUBV
| IC_MATCH_FL_HCIREV
|
110 IC_MATCH_FL_HCIVER
| IC_MATCH_FL_HCIBUS
,
111 .lmp_subver
= RTL_ROM_LMP_8723B
,
115 .config_needed
= true,
116 .has_rom_version
= true,
117 .fw_name
= "rtl_bt/rtl8723ds_fw.bin",
118 .cfg_name
= "rtl_bt/rtl8723ds_config" },
121 { IC_INFO(RTL_ROM_LMP_8821A
, 0xa),
122 .config_needed
= false,
123 .has_rom_version
= true,
124 .fw_name
= "rtl_bt/rtl8821a_fw.bin",
125 .cfg_name
= "rtl_bt/rtl8821a_config" },
128 { IC_INFO(RTL_ROM_LMP_8821A
, 0xc),
129 .config_needed
= false,
130 .has_rom_version
= true,
131 .fw_name
= "rtl_bt/rtl8821c_fw.bin",
132 .cfg_name
= "rtl_bt/rtl8821c_config" },
135 { IC_MATCH_FL_LMPSUBV
, RTL_ROM_LMP_8761A
, 0x0,
136 .config_needed
= false,
137 .has_rom_version
= true,
138 .fw_name
= "rtl_bt/rtl8761a_fw.bin",
139 .cfg_name
= "rtl_bt/rtl8761a_config" },
142 { IC_INFO(RTL_ROM_LMP_8822B
, 0xb),
143 .config_needed
= true,
144 .has_rom_version
= true,
145 .fw_name
= "rtl_bt/rtl8822b_fw.bin",
146 .cfg_name
= "rtl_bt/rtl8822b_config" },
149 static const struct id_table
*btrtl_match_ic(u16 lmp_subver
, u16 hci_rev
,
150 u8 hci_ver
, u8 hci_bus
)
154 for (i
= 0; i
< ARRAY_SIZE(ic_id_table
); i
++) {
155 if ((ic_id_table
[i
].match_flags
& IC_MATCH_FL_LMPSUBV
) &&
156 (ic_id_table
[i
].lmp_subver
!= lmp_subver
))
158 if ((ic_id_table
[i
].match_flags
& IC_MATCH_FL_HCIREV
) &&
159 (ic_id_table
[i
].hci_rev
!= hci_rev
))
161 if ((ic_id_table
[i
].match_flags
& IC_MATCH_FL_HCIVER
) &&
162 (ic_id_table
[i
].hci_ver
!= hci_ver
))
164 if ((ic_id_table
[i
].match_flags
& IC_MATCH_FL_HCIBUS
) &&
165 (ic_id_table
[i
].hci_bus
!= hci_bus
))
170 if (i
>= ARRAY_SIZE(ic_id_table
))
173 return &ic_id_table
[i
];
176 static int rtl_read_rom_version(struct hci_dev
*hdev
, u8
*version
)
178 struct rtl_rom_version_evt
*rom_version
;
181 /* Read RTL ROM version command */
182 skb
= __hci_cmd_sync(hdev
, 0xfc6d, 0, NULL
, HCI_INIT_TIMEOUT
);
184 rtl_dev_err(hdev
, "Read ROM version failed (%ld)\n",
189 if (skb
->len
!= sizeof(*rom_version
)) {
190 rtl_dev_err(hdev
, "RTL version event length mismatch\n");
195 rom_version
= (struct rtl_rom_version_evt
*)skb
->data
;
196 rtl_dev_info(hdev
, "rom_version status=%x version=%x\n",
197 rom_version
->status
, rom_version
->version
);
199 *version
= rom_version
->version
;
205 static int rtlbt_parse_firmware(struct hci_dev
*hdev
,
206 struct btrtl_device_info
*btrtl_dev
,
207 unsigned char **_buf
)
209 const u8 extension_sig
[] = { 0x51, 0x04, 0xfd, 0x77 };
210 struct rtl_epatch_header
*epatch_info
;
214 u8 opcode
, length
, data
;
216 const unsigned char *fwptr
, *chip_id_base
;
217 const unsigned char *patch_length_base
, *patch_offset_base
;
218 u32 patch_offset
= 0;
219 u16 patch_length
, num_patches
;
220 static const struct {
223 } project_id_to_lmp_subver
[] = {
224 { RTL_ROM_LMP_8723A
, 0 },
225 { RTL_ROM_LMP_8723B
, 1 },
226 { RTL_ROM_LMP_8821A
, 2 },
227 { RTL_ROM_LMP_8761A
, 3 },
228 { RTL_ROM_LMP_8822B
, 8 },
229 { RTL_ROM_LMP_8723B
, 9 }, /* 8723D */
230 { RTL_ROM_LMP_8821A
, 10 }, /* 8821C */
233 min_size
= sizeof(struct rtl_epatch_header
) + sizeof(extension_sig
) + 3;
234 if (btrtl_dev
->fw_len
< min_size
)
237 fwptr
= btrtl_dev
->fw_data
+ btrtl_dev
->fw_len
- sizeof(extension_sig
);
238 if (memcmp(fwptr
, extension_sig
, sizeof(extension_sig
)) != 0) {
239 rtl_dev_err(hdev
, "extension section signature mismatch\n");
243 /* Loop from the end of the firmware parsing instructions, until
244 * we find an instruction that identifies the "project ID" for the
245 * hardware supported by this firwmare file.
246 * Once we have that, we double-check that that project_id is suitable
247 * for the hardware we are working with.
249 while (fwptr
>= btrtl_dev
->fw_data
+ (sizeof(*epatch_info
) + 3)) {
254 BT_DBG("check op=%x len=%x data=%x", opcode
, length
, data
);
256 if (opcode
== 0xff) /* EOF */
260 rtl_dev_err(hdev
, "found instruction with length 0\n");
264 if (opcode
== 0 && length
== 1) {
272 if (project_id
< 0) {
273 rtl_dev_err(hdev
, "failed to find version instruction\n");
277 /* Find project_id in table */
278 for (i
= 0; i
< ARRAY_SIZE(project_id_to_lmp_subver
); i
++) {
279 if (project_id
== project_id_to_lmp_subver
[i
].id
)
283 if (i
>= ARRAY_SIZE(project_id_to_lmp_subver
)) {
284 rtl_dev_err(hdev
, "unknown project id %d\n", project_id
);
288 if (btrtl_dev
->ic_info
->lmp_subver
!=
289 project_id_to_lmp_subver
[i
].lmp_subver
) {
290 rtl_dev_err(hdev
, "firmware is for %x but this is a %x\n",
291 project_id_to_lmp_subver
[i
].lmp_subver
,
292 btrtl_dev
->ic_info
->lmp_subver
);
296 epatch_info
= (struct rtl_epatch_header
*)btrtl_dev
->fw_data
;
297 if (memcmp(epatch_info
->signature
, RTL_EPATCH_SIGNATURE
, 8) != 0) {
298 rtl_dev_err(hdev
, "bad EPATCH signature\n");
302 num_patches
= le16_to_cpu(epatch_info
->num_patches
);
303 BT_DBG("fw_version=%x, num_patches=%d",
304 le32_to_cpu(epatch_info
->fw_version
), num_patches
);
306 /* After the rtl_epatch_header there is a funky patch metadata section.
307 * Assuming 2 patches, the layout is:
308 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
310 * Find the right patch for this chip.
312 min_size
+= 8 * num_patches
;
313 if (btrtl_dev
->fw_len
< min_size
)
316 chip_id_base
= btrtl_dev
->fw_data
+ sizeof(struct rtl_epatch_header
);
317 patch_length_base
= chip_id_base
+ (sizeof(u16
) * num_patches
);
318 patch_offset_base
= patch_length_base
+ (sizeof(u16
) * num_patches
);
319 for (i
= 0; i
< num_patches
; i
++) {
320 u16 chip_id
= get_unaligned_le16(chip_id_base
+
322 if (chip_id
== btrtl_dev
->rom_version
+ 1) {
323 patch_length
= get_unaligned_le16(patch_length_base
+
325 patch_offset
= get_unaligned_le32(patch_offset_base
+
332 rtl_dev_err(hdev
, "didn't find patch for chip id %d",
333 btrtl_dev
->rom_version
);
337 BT_DBG("length=%x offset=%x index %d", patch_length
, patch_offset
, i
);
338 min_size
= patch_offset
+ patch_length
;
339 if (btrtl_dev
->fw_len
< min_size
)
342 /* Copy the firmware into a new buffer and write the version at
346 buf
= kmemdup(btrtl_dev
->fw_data
+ patch_offset
, patch_length
,
351 memcpy(buf
+ patch_length
- 4, &epatch_info
->fw_version
, 4);
357 static int rtl_download_firmware(struct hci_dev
*hdev
,
358 const unsigned char *data
, int fw_len
)
360 struct rtl_download_cmd
*dl_cmd
;
361 int frag_num
= fw_len
/ RTL_FRAG_LEN
+ 1;
362 int frag_len
= RTL_FRAG_LEN
;
366 dl_cmd
= kmalloc(sizeof(struct rtl_download_cmd
), GFP_KERNEL
);
370 for (i
= 0; i
< frag_num
; i
++) {
373 BT_DBG("download fw (%d/%d)", i
, frag_num
);
376 if (i
== (frag_num
- 1)) {
377 dl_cmd
->index
|= 0x80; /* data end */
378 frag_len
= fw_len
% RTL_FRAG_LEN
;
380 memcpy(dl_cmd
->data
, data
, frag_len
);
382 /* Send download command */
383 skb
= __hci_cmd_sync(hdev
, 0xfc20, frag_len
+ 1, dl_cmd
,
386 rtl_dev_err(hdev
, "download fw command failed (%ld)\n",
392 if (skb
->len
!= sizeof(struct rtl_download_response
)) {
393 rtl_dev_err(hdev
, "download fw event length mismatch\n");
400 data
+= RTL_FRAG_LEN
;
408 static int rtl_load_file(struct hci_dev
*hdev
, const char *name
, u8
**buff
)
410 const struct firmware
*fw
;
413 rtl_dev_info(hdev
, "rtl: loading %s\n", name
);
414 ret
= request_firmware(&fw
, name
, &hdev
->dev
);
418 *buff
= kmemdup(fw
->data
, ret
, GFP_KERNEL
);
422 release_firmware(fw
);
427 static int btrtl_setup_rtl8723a(struct hci_dev
*hdev
,
428 struct btrtl_device_info
*btrtl_dev
)
430 if (btrtl_dev
->fw_len
< 8)
433 /* Check that the firmware doesn't have the epatch signature
434 * (which is only for RTL8723B and newer).
436 if (!memcmp(btrtl_dev
->fw_data
, RTL_EPATCH_SIGNATURE
, 8)) {
437 rtl_dev_err(hdev
, "unexpected EPATCH signature!\n");
441 return rtl_download_firmware(hdev
, btrtl_dev
->fw_data
,
445 static int btrtl_setup_rtl8723b(struct hci_dev
*hdev
,
446 struct btrtl_device_info
*btrtl_dev
)
448 unsigned char *fw_data
= NULL
;
452 ret
= rtlbt_parse_firmware(hdev
, btrtl_dev
, &fw_data
);
456 if (btrtl_dev
->cfg_len
> 0) {
457 tbuff
= kzalloc(ret
+ btrtl_dev
->cfg_len
, GFP_KERNEL
);
463 memcpy(tbuff
, fw_data
, ret
);
466 memcpy(tbuff
+ ret
, btrtl_dev
->cfg_data
, btrtl_dev
->cfg_len
);
467 ret
+= btrtl_dev
->cfg_len
;
472 rtl_dev_info(hdev
, "cfg_sz %d, total sz %d\n", btrtl_dev
->cfg_len
, ret
);
474 ret
= rtl_download_firmware(hdev
, fw_data
, ret
);
481 static struct sk_buff
*btrtl_read_local_version(struct hci_dev
*hdev
)
485 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
488 rtl_dev_err(hdev
, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
493 if (skb
->len
!= sizeof(struct hci_rp_read_local_version
)) {
494 rtl_dev_err(hdev
, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
496 return ERR_PTR(-EIO
);
502 void btrtl_free(struct btrtl_device_info
*btrtl_dev
)
504 kfree(btrtl_dev
->fw_data
);
505 kfree(btrtl_dev
->cfg_data
);
508 EXPORT_SYMBOL_GPL(btrtl_free
);
510 struct btrtl_device_info
*btrtl_initialize(struct hci_dev
*hdev
,
513 struct btrtl_device_info
*btrtl_dev
;
515 struct hci_rp_read_local_version
*resp
;
517 u16 hci_rev
, lmp_subver
;
521 btrtl_dev
= kzalloc(sizeof(*btrtl_dev
), GFP_KERNEL
);
527 skb
= btrtl_read_local_version(hdev
);
533 resp
= (struct hci_rp_read_local_version
*)skb
->data
;
534 rtl_dev_info(hdev
, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
535 resp
->hci_ver
, resp
->hci_rev
,
536 resp
->lmp_ver
, resp
->lmp_subver
);
538 hci_ver
= resp
->hci_ver
;
539 hci_rev
= le16_to_cpu(resp
->hci_rev
);
540 lmp_subver
= le16_to_cpu(resp
->lmp_subver
);
543 btrtl_dev
->ic_info
= btrtl_match_ic(lmp_subver
, hci_rev
, hci_ver
,
546 if (!btrtl_dev
->ic_info
) {
547 rtl_dev_err(hdev
, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
548 lmp_subver
, hci_rev
, hci_ver
);
553 if (btrtl_dev
->ic_info
->has_rom_version
) {
554 ret
= rtl_read_rom_version(hdev
, &btrtl_dev
->rom_version
);
559 btrtl_dev
->fw_len
= rtl_load_file(hdev
, btrtl_dev
->ic_info
->fw_name
,
560 &btrtl_dev
->fw_data
);
561 if (btrtl_dev
->fw_len
< 0) {
562 rtl_dev_err(hdev
, "firmware file %s not found\n",
563 btrtl_dev
->ic_info
->fw_name
);
564 ret
= btrtl_dev
->fw_len
;
568 if (btrtl_dev
->ic_info
->cfg_name
) {
570 snprintf(cfg_name
, sizeof(cfg_name
), "%s-%s.bin",
571 btrtl_dev
->ic_info
->cfg_name
, postfix
);
573 snprintf(cfg_name
, sizeof(cfg_name
), "%s.bin",
574 btrtl_dev
->ic_info
->cfg_name
);
576 btrtl_dev
->cfg_len
= rtl_load_file(hdev
, cfg_name
,
577 &btrtl_dev
->cfg_data
);
578 if (btrtl_dev
->ic_info
->config_needed
&&
579 btrtl_dev
->cfg_len
<= 0) {
580 rtl_dev_err(hdev
, "mandatory config file %s not found\n",
581 btrtl_dev
->ic_info
->cfg_name
);
582 ret
= btrtl_dev
->cfg_len
;
590 btrtl_free(btrtl_dev
);
594 EXPORT_SYMBOL_GPL(btrtl_initialize
);
596 int btrtl_download_firmware(struct hci_dev
*hdev
,
597 struct btrtl_device_info
*btrtl_dev
)
599 /* Match a set of subver values that correspond to stock firmware,
600 * which is not compatible with standard btusb.
601 * If matched, upload an alternative firmware that does conform to
602 * standard btusb. Once that firmware is uploaded, the subver changes
603 * to a different value.
605 switch (btrtl_dev
->ic_info
->lmp_subver
) {
606 case RTL_ROM_LMP_8723A
:
607 case RTL_ROM_LMP_3499
:
608 return btrtl_setup_rtl8723a(hdev
, btrtl_dev
);
609 case RTL_ROM_LMP_8723B
:
610 case RTL_ROM_LMP_8821A
:
611 case RTL_ROM_LMP_8761A
:
612 case RTL_ROM_LMP_8822B
:
613 return btrtl_setup_rtl8723b(hdev
, btrtl_dev
);
615 rtl_dev_info(hdev
, "rtl: assuming no firmware upload needed\n");
619 EXPORT_SYMBOL_GPL(btrtl_download_firmware
);
621 int btrtl_setup_realtek(struct hci_dev
*hdev
)
623 struct btrtl_device_info
*btrtl_dev
;
626 btrtl_dev
= btrtl_initialize(hdev
, NULL
);
627 if (IS_ERR(btrtl_dev
))
628 return PTR_ERR(btrtl_dev
);
630 ret
= btrtl_download_firmware(hdev
, btrtl_dev
);
632 btrtl_free(btrtl_dev
);
636 EXPORT_SYMBOL_GPL(btrtl_setup_realtek
);
638 static unsigned int btrtl_convert_baudrate(u32 device_baudrate
)
640 switch (device_baudrate
) {
675 int btrtl_get_uart_settings(struct hci_dev
*hdev
,
676 struct btrtl_device_info
*btrtl_dev
,
677 unsigned int *controller_baudrate
,
678 u32
*device_baudrate
, bool *flow_control
)
680 struct rtl_vendor_config
*config
;
681 struct rtl_vendor_config_entry
*entry
;
682 int i
, total_data_len
;
685 total_data_len
= btrtl_dev
->cfg_len
- sizeof(*config
);
686 if (total_data_len
<= 0) {
687 rtl_dev_warn(hdev
, "no config loaded\n");
691 config
= (struct rtl_vendor_config
*)btrtl_dev
->cfg_data
;
692 if (le32_to_cpu(config
->signature
) != RTL_CONFIG_MAGIC
) {
693 rtl_dev_err(hdev
, "invalid config magic\n");
697 if (total_data_len
< le16_to_cpu(config
->total_len
)) {
698 rtl_dev_err(hdev
, "config is too short\n");
702 for (i
= 0; i
< total_data_len
; ) {
703 entry
= ((void *)config
->entry
) + i
;
705 switch (le16_to_cpu(entry
->offset
)) {
707 if (entry
->len
< sizeof(*device_baudrate
)) {
708 rtl_dev_err(hdev
, "invalid UART config entry\n");
712 *device_baudrate
= get_unaligned_le32(entry
->data
);
713 *controller_baudrate
= btrtl_convert_baudrate(
716 if (entry
->len
>= 13)
717 *flow_control
= !!(entry
->data
[12] & BIT(2));
719 *flow_control
= false;
725 rtl_dev_dbg(hdev
, "skipping config entry 0x%x (len %u)\n",
726 le16_to_cpu(entry
->offset
), entry
->len
);
730 i
+= sizeof(*entry
) + entry
->len
;
734 rtl_dev_err(hdev
, "no UART config entry found\n");
738 rtl_dev_dbg(hdev
, "device baudrate = 0x%08x\n", *device_baudrate
);
739 rtl_dev_dbg(hdev
, "controller baudrate = %u\n", *controller_baudrate
);
740 rtl_dev_dbg(hdev
, "flow control %d\n", *flow_control
);
744 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings
);
746 MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
747 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION
);
748 MODULE_VERSION(VERSION
);
749 MODULE_LICENSE("GPL");
750 MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
751 MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
752 MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
753 MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
754 MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
755 MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
756 MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
757 MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
758 MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
759 MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
760 MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
761 MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
762 MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");