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 = "/*(DEBLOBBED)*/",
76 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0,
77 .config_needed = false,
78 .has_rom_version = false,
79 .fw_name = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = "/*(DEBLOBBED)*/",
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 = kvmalloc(patch_length, GFP_KERNEL);
350 memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
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 = reject_firmware(&fw, name, &hdev->dev);
418 *buff = kvmalloc(fw->size, GFP_KERNEL);
420 memcpy(*buff, fw->data, ret);
424 release_firmware(fw);
429 static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
430 struct btrtl_device_info *btrtl_dev)
432 if (btrtl_dev->fw_len < 8)
435 /* Check that the firmware doesn't have the epatch signature
436 * (which is only for RTL8723B and newer).
438 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
439 rtl_dev_err(hdev, "unexpected EPATCH signature!\n");
443 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
447 static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
448 struct btrtl_device_info *btrtl_dev)
450 unsigned char *fw_data = NULL;
454 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
458 if (btrtl_dev->cfg_len > 0) {
459 tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
465 memcpy(tbuff, fw_data, ret);
468 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
469 ret += btrtl_dev->cfg_len;
474 rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
476 ret = rtl_download_firmware(hdev, fw_data, ret);
483 static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
487 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
490 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
495 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
496 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
498 return ERR_PTR(-EIO);
504 void btrtl_free(struct btrtl_device_info *btrtl_dev)
506 kvfree(btrtl_dev->fw_data);
507 kvfree(btrtl_dev->cfg_data);
510 EXPORT_SYMBOL_GPL(btrtl_free);
512 struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
515 struct btrtl_device_info *btrtl_dev;
517 struct hci_rp_read_local_version *resp;
519 u16 hci_rev, lmp_subver;
523 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
529 skb = btrtl_read_local_version(hdev);
535 resp = (struct hci_rp_read_local_version *)skb->data;
536 rtl_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
537 resp->hci_ver, resp->hci_rev,
538 resp->lmp_ver, resp->lmp_subver);
540 hci_ver = resp->hci_ver;
541 hci_rev = le16_to_cpu(resp->hci_rev);
542 lmp_subver = le16_to_cpu(resp->lmp_subver);
545 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
548 if (!btrtl_dev->ic_info) {
549 rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
550 lmp_subver, hci_rev, hci_ver);
554 if (btrtl_dev->ic_info->has_rom_version) {
555 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
560 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
561 &btrtl_dev->fw_data);
562 if (btrtl_dev->fw_len < 0) {
563 rtl_dev_err(hdev, "firmware file %s not found\n",
564 btrtl_dev->ic_info->fw_name);
565 ret = btrtl_dev->fw_len;
569 if (btrtl_dev->ic_info->cfg_name) {
571 snprintf(cfg_name, sizeof(cfg_name), "/*(DEBLOBBED)*/",
572 btrtl_dev->ic_info->cfg_name, postfix);
574 snprintf(cfg_name, sizeof(cfg_name), "/*(DEBLOBBED)*/",
575 btrtl_dev->ic_info->cfg_name);
577 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
578 &btrtl_dev->cfg_data);
579 if (btrtl_dev->ic_info->config_needed &&
580 btrtl_dev->cfg_len <= 0) {
581 rtl_dev_err(hdev, "mandatory config file %s not found\n",
582 btrtl_dev->ic_info->cfg_name);
583 ret = btrtl_dev->cfg_len;
591 btrtl_free(btrtl_dev);
595 EXPORT_SYMBOL_GPL(btrtl_initialize);
597 int btrtl_download_firmware(struct hci_dev *hdev,
598 struct btrtl_device_info *btrtl_dev)
600 /* Match a set of subver values that correspond to stock firmware,
601 * which is not compatible with standard btusb.
602 * If matched, upload an alternative firmware that does conform to
603 * standard btusb. Once that firmware is uploaded, the subver changes
604 * to a different value.
606 if (!btrtl_dev->ic_info) {
607 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
611 switch (btrtl_dev->ic_info->lmp_subver) {
612 case RTL_ROM_LMP_8723A:
613 case RTL_ROM_LMP_3499:
614 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
615 case RTL_ROM_LMP_8723B:
616 case RTL_ROM_LMP_8821A:
617 case RTL_ROM_LMP_8761A:
618 case RTL_ROM_LMP_8822B:
619 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
621 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
625 EXPORT_SYMBOL_GPL(btrtl_download_firmware);
627 int btrtl_setup_realtek(struct hci_dev *hdev)
629 struct btrtl_device_info *btrtl_dev;
632 btrtl_dev = btrtl_initialize(hdev, NULL);
633 if (IS_ERR(btrtl_dev))
634 return PTR_ERR(btrtl_dev);
636 ret = btrtl_download_firmware(hdev, btrtl_dev);
638 btrtl_free(btrtl_dev);
642 EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
644 int btrtl_shutdown_realtek(struct hci_dev *hdev)
649 /* According to the vendor driver, BT must be reset on close to avoid
652 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
655 bt_dev_err(hdev, "HCI reset during shutdown failed");
662 EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
664 static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
666 switch (device_baudrate) {
701 int btrtl_get_uart_settings(struct hci_dev *hdev,
702 struct btrtl_device_info *btrtl_dev,
703 unsigned int *controller_baudrate,
704 u32 *device_baudrate, bool *flow_control)
706 struct rtl_vendor_config *config;
707 struct rtl_vendor_config_entry *entry;
708 int i, total_data_len;
711 total_data_len = btrtl_dev->cfg_len - sizeof(*config);
712 if (total_data_len <= 0) {
713 rtl_dev_warn(hdev, "no config loaded\n");
717 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
718 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
719 rtl_dev_err(hdev, "invalid config magic\n");
723 if (total_data_len < le16_to_cpu(config->total_len)) {
724 rtl_dev_err(hdev, "config is too short\n");
728 for (i = 0; i < total_data_len; ) {
729 entry = ((void *)config->entry) + i;
731 switch (le16_to_cpu(entry->offset)) {
733 if (entry->len < sizeof(*device_baudrate)) {
734 rtl_dev_err(hdev, "invalid UART config entry\n");
738 *device_baudrate = get_unaligned_le32(entry->data);
739 *controller_baudrate = btrtl_convert_baudrate(
742 if (entry->len >= 13)
743 *flow_control = !!(entry->data[12] & BIT(2));
745 *flow_control = false;
751 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n",
752 le16_to_cpu(entry->offset), entry->len);
756 i += sizeof(*entry) + entry->len;
760 rtl_dev_err(hdev, "no UART config entry found\n");
764 rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate);
765 rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate);
766 rtl_dev_dbg(hdev, "flow control %d\n", *flow_control);
770 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
772 MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
773 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
774 MODULE_VERSION(VERSION);
775 MODULE_LICENSE("GPL");