Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Bluetooth support for Realtek devices |
| 4 | * |
| 5 | * Copyright (C) 2015 Endless Mobile, Inc. |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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 |
Larry Finger | 6c595ea | 2019-05-29 13:12:34 -0500 | [diff] [blame] | 24 | #define RTL_ROM_LMP_8723D 0x8873 |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 25 | #define RTL_ROM_LMP_8821A 0x8821 |
| 26 | #define RTL_ROM_LMP_8761A 0x8761 |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 27 | #define RTL_ROM_LMP_8822B 0x8822 |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 28 | #define RTL_CONFIG_MAGIC 0x8723ab55 |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 29 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 30 | #define IC_MATCH_FL_LMPSUBV (1 << 0) |
| 31 | #define IC_MATCH_FL_HCIREV (1 << 1) |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 32 | #define IC_MATCH_FL_HCIVER (1 << 2) |
| 33 | #define IC_MATCH_FL_HCIBUS (1 << 3) |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 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; |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 43 | __u8 hci_ver; |
| 44 | __u8 hci_bus; |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 45 | bool config_needed; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 46 | bool has_rom_version; |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 47 | char *fw_name; |
| 48 | char *cfg_name; |
| 49 | }; |
| 50 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 60 | static const struct id_table ic_id_table[] = { |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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 | |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 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", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 83 | .cfg_name = "rtl_bt/rtl8723bs_config" }, |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 84 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 85 | /* 8723B */ |
| 86 | { IC_INFO(RTL_ROM_LMP_8723B, 0xb), |
| 87 | .config_needed = false, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 88 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 89 | .fw_name = "rtl_bt/rtl8723b_fw.bin", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 90 | .cfg_name = "rtl_bt/rtl8723b_config" }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 91 | |
| 92 | /* 8723D */ |
| 93 | { IC_INFO(RTL_ROM_LMP_8723B, 0xd), |
| 94 | .config_needed = true, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 95 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 96 | .fw_name = "rtl_bt/rtl8723d_fw.bin", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 97 | .cfg_name = "rtl_bt/rtl8723d_config" }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 98 | |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 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", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 109 | .cfg_name = "rtl_bt/rtl8723ds_config" }, |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 110 | |
Larry Finger | 6c595ea | 2019-05-29 13:12:34 -0500 | [diff] [blame] | 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 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 118 | /* 8821A */ |
| 119 | { IC_INFO(RTL_ROM_LMP_8821A, 0xa), |
| 120 | .config_needed = false, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 121 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 122 | .fw_name = "rtl_bt/rtl8821a_fw.bin", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 123 | .cfg_name = "rtl_bt/rtl8821a_config" }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 124 | |
| 125 | /* 8821C */ |
| 126 | { IC_INFO(RTL_ROM_LMP_8821A, 0xc), |
| 127 | .config_needed = false, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 128 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 129 | .fw_name = "rtl_bt/rtl8821c_fw.bin", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 130 | .cfg_name = "rtl_bt/rtl8821c_config" }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 131 | |
| 132 | /* 8761A */ |
| 133 | { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0, |
| 134 | .config_needed = false, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 135 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 136 | .fw_name = "rtl_bt/rtl8761a_fw.bin", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 137 | .cfg_name = "rtl_bt/rtl8761a_config" }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 138 | |
Alex Lu | 8ecfdc9 | 2018-09-08 12:05:59 -0500 | [diff] [blame] | 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 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 146 | /* 8822B */ |
| 147 | { IC_INFO(RTL_ROM_LMP_8822B, 0xb), |
| 148 | .config_needed = true, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 149 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 150 | .fw_name = "rtl_bt/rtl8822b_fw.bin", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 151 | .cfg_name = "rtl_bt/rtl8822b_config" }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 152 | }; |
| 153 | |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 154 | static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev, |
| 155 | u8 hci_ver, u8 hci_bus) |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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; |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 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; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 172 | |
| 173 | break; |
| 174 | } |
| 175 | if (i >= ARRAY_SIZE(ic_id_table)) |
| 176 | return NULL; |
| 177 | |
| 178 | return &ic_id_table[i]; |
| 179 | } |
| 180 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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)) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 189 | rtl_dev_err(hdev, "Read ROM version failed (%ld)\n", |
| 190 | PTR_ERR(skb)); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 191 | return PTR_ERR(skb); |
| 192 | } |
| 193 | |
| 194 | if (skb->len != sizeof(*rom_version)) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 195 | rtl_dev_err(hdev, "RTL version event length mismatch\n"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 196 | kfree_skb(skb); |
| 197 | return -EIO; |
| 198 | } |
| 199 | |
| 200 | rom_version = (struct rtl_rom_version_evt *)skb->data; |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 201 | rtl_dev_info(hdev, "rom_version status=%x version=%x\n", |
| 202 | rom_version->status, rom_version->version); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 203 | |
| 204 | *version = rom_version->version; |
| 205 | |
| 206 | kfree_skb(skb); |
| 207 | return 0; |
| 208 | } |
| 209 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 210 | static int rtlbt_parse_firmware(struct hci_dev *hdev, |
| 211 | struct btrtl_device_info *btrtl_dev, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 212 | unsigned char **_buf) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 213 | { |
Colin Ian King | e5070e07 | 2018-09-26 09:32:08 +0100 | [diff] [blame] | 214 | static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 }; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 215 | struct rtl_epatch_header *epatch_info; |
| 216 | unsigned char *buf; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 217 | int i, len; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 218 | size_t min_size; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 219 | u8 opcode, length, data; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 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 }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 234 | { RTL_ROM_LMP_8723B, 9 }, /* 8723D */ |
| 235 | { RTL_ROM_LMP_8821A, 10 }, /* 8821C */ |
Alex Lu | 8ecfdc9 | 2018-09-08 12:05:59 -0500 | [diff] [blame] | 236 | { RTL_ROM_LMP_8822B, 13 }, /* 8822C */ |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 237 | }; |
| 238 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 239 | min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 240 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 241 | return -EINVAL; |
| 242 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 243 | fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 244 | if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 245 | rtl_dev_err(hdev, "extension section signature mismatch\n"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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 | */ |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 255 | while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 266 | rtl_dev_err(hdev, "found instruction with length 0\n"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 279 | rtl_dev_err(hdev, "failed to find version instruction\n"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 280 | return -EINVAL; |
| 281 | } |
| 282 | |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 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)) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 290 | rtl_dev_err(hdev, "unknown project id %d\n", project_id); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 291 | return -EINVAL; |
| 292 | } |
| 293 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 294 | if (btrtl_dev->ic_info->lmp_subver != |
| 295 | project_id_to_lmp_subver[i].lmp_subver) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 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); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 299 | return -EINVAL; |
| 300 | } |
| 301 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 302 | epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 303 | if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 304 | rtl_dev_err(hdev, "bad EPATCH signature\n"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 319 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 320 | return -EINVAL; |
| 321 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 322 | chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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))); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 328 | if (chip_id == btrtl_dev->rom_version + 1) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 338 | rtl_dev_err(hdev, "didn't find patch for chip id %d", |
| 339 | btrtl_dev->rom_version); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 345 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 352 | buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length, |
| 353 | GFP_KERNEL); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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)) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 392 | rtl_dev_err(hdev, "download fw command failed (%ld)\n", |
| 393 | PTR_ERR(skb)); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 394 | ret = -PTR_ERR(skb); |
| 395 | goto out; |
| 396 | } |
| 397 | |
| 398 | if (skb->len != sizeof(struct rtl_download_response)) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 399 | rtl_dev_err(hdev, "download fw event length mismatch\n"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 414 | static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff) |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 415 | { |
| 416 | const struct firmware *fw; |
| 417 | int ret; |
| 418 | |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 419 | rtl_dev_info(hdev, "rtl: loading %s\n", name); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 420 | ret = request_firmware(&fw, name, &hdev->dev); |
Larry Finger | abed84a | 2017-02-19 20:04:57 -0600 | [diff] [blame] | 421 | if (ret < 0) |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 422 | return ret; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 423 | ret = fw->size; |
| 424 | *buff = kmemdup(fw->data, ret, GFP_KERNEL); |
Dan Carpenter | c3327bd | 2017-07-28 17:41:11 +0300 | [diff] [blame] | 425 | if (!*buff) |
| 426 | ret = -ENOMEM; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 427 | |
| 428 | release_firmware(fw); |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 433 | static int btrtl_setup_rtl8723a(struct hci_dev *hdev, |
| 434 | struct btrtl_device_info *btrtl_dev) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 435 | { |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 436 | if (btrtl_dev->fw_len < 8) |
| 437 | return -EINVAL; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 438 | |
| 439 | /* Check that the firmware doesn't have the epatch signature |
| 440 | * (which is only for RTL8723B and newer). |
| 441 | */ |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 442 | if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 443 | rtl_dev_err(hdev, "unexpected EPATCH signature!\n"); |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 444 | return -EINVAL; |
| 445 | } |
| 446 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 447 | return rtl_download_firmware(hdev, btrtl_dev->fw_data, |
| 448 | btrtl_dev->fw_len); |
| 449 | } |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 450 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 457 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 458 | ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 459 | if (ret < 0) |
| 460 | goto out; |
| 461 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 462 | if (btrtl_dev->cfg_len > 0) { |
| 463 | tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 464 | if (!tbuff) { |
| 465 | ret = -ENOMEM; |
| 466 | goto out; |
| 467 | } |
| 468 | |
| 469 | memcpy(tbuff, fw_data, ret); |
| 470 | kfree(fw_data); |
| 471 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 472 | memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len); |
| 473 | ret += btrtl_dev->cfg_len; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 474 | |
| 475 | fw_data = tbuff; |
| 476 | } |
| 477 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 478 | rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 479 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 480 | ret = rtl_download_firmware(hdev, fw_data, ret); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 481 | |
| 482 | out: |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 483 | kfree(fw_data); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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)) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 494 | rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n", |
| 495 | PTR_ERR(skb)); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 496 | return skb; |
| 497 | } |
| 498 | |
| 499 | if (skb->len != sizeof(struct hci_rp_read_local_version)) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 500 | rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 501 | kfree_skb(skb); |
| 502 | return ERR_PTR(-EIO); |
| 503 | } |
| 504 | |
| 505 | return skb; |
| 506 | } |
| 507 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 508 | void btrtl_free(struct btrtl_device_info *btrtl_dev) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 509 | { |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 510 | kfree(btrtl_dev->fw_data); |
| 511 | kfree(btrtl_dev->cfg_data); |
| 512 | kfree(btrtl_dev); |
| 513 | } |
| 514 | EXPORT_SYMBOL_GPL(btrtl_free); |
| 515 | |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 516 | struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev, |
| 517 | const char *postfix) |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 518 | { |
| 519 | struct btrtl_device_info *btrtl_dev; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 520 | struct sk_buff *skb; |
| 521 | struct hci_rp_read_local_version *resp; |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 522 | char cfg_name[40]; |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 523 | u16 hci_rev, lmp_subver; |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 524 | u8 hci_ver; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 532 | |
| 533 | skb = btrtl_read_local_version(hdev); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 534 | if (IS_ERR(skb)) { |
| 535 | ret = PTR_ERR(skb); |
| 536 | goto err_free; |
| 537 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 538 | |
| 539 | resp = (struct hci_rp_read_local_version *)skb->data; |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 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); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 543 | |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 544 | hci_ver = resp->hci_ver; |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 545 | hci_rev = le16_to_cpu(resp->hci_rev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 546 | lmp_subver = le16_to_cpu(resp->lmp_subver); |
| 547 | kfree_skb(skb); |
| 548 | |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 549 | btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, |
| 550 | hdev->bus); |
| 551 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 552 | if (!btrtl_dev->ic_info) { |
Kai-Heng Feng | 00df214 | 2019-01-27 16:33:59 +0800 | [diff] [blame] | 553 | rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x", |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 554 | lmp_subver, hci_rev, hci_ver); |
Kai-Heng Feng | 00df214 | 2019-01-27 16:33:59 +0800 | [diff] [blame] | 555 | return btrtl_dev; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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) { |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 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, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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 | { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 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 | */ |
Kai-Heng Feng | 00df214 | 2019-01-27 16:33:59 +0800 | [diff] [blame] | 610 | if (!btrtl_dev->ic_info) { |
| 611 | rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n"); |
| 612 | return 0; |
| 613 | } |
| 614 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 615 | switch (btrtl_dev->ic_info->lmp_subver) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 616 | case RTL_ROM_LMP_8723A: |
| 617 | case RTL_ROM_LMP_3499: |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 618 | return btrtl_setup_rtl8723a(hdev, btrtl_dev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 619 | case RTL_ROM_LMP_8723B: |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 620 | case RTL_ROM_LMP_8821A: |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 621 | case RTL_ROM_LMP_8761A: |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 622 | case RTL_ROM_LMP_8822B: |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 623 | return btrtl_setup_rtl8723b(hdev, btrtl_dev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 624 | default: |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 625 | rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | } |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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 | |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 636 | btrtl_dev = btrtl_initialize(hdev, NULL); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 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 | |
Alex Lu | 65251e2 | 2019-08-30 20:02:14 +0800 | [diff] [blame^] | 644 | /* Enable controller to do both LE scan and BR/EDR inquiry |
| 645 | * simultaneously. |
| 646 | */ |
| 647 | set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); |
| 648 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 649 | return ret; |
| 650 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 651 | EXPORT_SYMBOL_GPL(btrtl_setup_realtek); |
| 652 | |
Jian-Hong Pan | 7af3f558 | 2019-06-25 16:30:51 +0800 | [diff] [blame] | 653 | int btrtl_shutdown_realtek(struct hci_dev *hdev) |
| 654 | { |
| 655 | struct sk_buff *skb; |
| 656 | int ret; |
| 657 | |
| 658 | /* According to the vendor driver, BT must be reset on close to avoid |
| 659 | * firmware crash. |
| 660 | */ |
| 661 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); |
| 662 | if (IS_ERR(skb)) { |
| 663 | ret = PTR_ERR(skb); |
| 664 | bt_dev_err(hdev, "HCI reset during shutdown failed"); |
| 665 | return ret; |
| 666 | } |
| 667 | kfree_skb(skb); |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek); |
| 672 | |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 673 | static unsigned int btrtl_convert_baudrate(u32 device_baudrate) |
| 674 | { |
| 675 | switch (device_baudrate) { |
| 676 | case 0x0252a00a: |
| 677 | return 230400; |
| 678 | |
| 679 | case 0x05f75004: |
| 680 | return 921600; |
| 681 | |
| 682 | case 0x00005004: |
| 683 | return 1000000; |
| 684 | |
| 685 | case 0x04928002: |
| 686 | case 0x01128002: |
| 687 | return 1500000; |
| 688 | |
| 689 | case 0x00005002: |
| 690 | return 2000000; |
| 691 | |
| 692 | case 0x0000b001: |
| 693 | return 2500000; |
| 694 | |
| 695 | case 0x04928001: |
| 696 | return 3000000; |
| 697 | |
| 698 | case 0x052a6001: |
| 699 | return 3500000; |
| 700 | |
| 701 | case 0x00005001: |
| 702 | return 4000000; |
| 703 | |
| 704 | case 0x0252c014: |
| 705 | default: |
| 706 | return 115200; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | int btrtl_get_uart_settings(struct hci_dev *hdev, |
| 711 | struct btrtl_device_info *btrtl_dev, |
| 712 | unsigned int *controller_baudrate, |
| 713 | u32 *device_baudrate, bool *flow_control) |
| 714 | { |
| 715 | struct rtl_vendor_config *config; |
| 716 | struct rtl_vendor_config_entry *entry; |
| 717 | int i, total_data_len; |
| 718 | bool found = false; |
| 719 | |
| 720 | total_data_len = btrtl_dev->cfg_len - sizeof(*config); |
| 721 | if (total_data_len <= 0) { |
| 722 | rtl_dev_warn(hdev, "no config loaded\n"); |
| 723 | return -EINVAL; |
| 724 | } |
| 725 | |
| 726 | config = (struct rtl_vendor_config *)btrtl_dev->cfg_data; |
| 727 | if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) { |
| 728 | rtl_dev_err(hdev, "invalid config magic\n"); |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | |
| 732 | if (total_data_len < le16_to_cpu(config->total_len)) { |
| 733 | rtl_dev_err(hdev, "config is too short\n"); |
| 734 | return -EINVAL; |
| 735 | } |
| 736 | |
| 737 | for (i = 0; i < total_data_len; ) { |
| 738 | entry = ((void *)config->entry) + i; |
| 739 | |
| 740 | switch (le16_to_cpu(entry->offset)) { |
| 741 | case 0xc: |
| 742 | if (entry->len < sizeof(*device_baudrate)) { |
| 743 | rtl_dev_err(hdev, "invalid UART config entry\n"); |
| 744 | return -EINVAL; |
| 745 | } |
| 746 | |
| 747 | *device_baudrate = get_unaligned_le32(entry->data); |
| 748 | *controller_baudrate = btrtl_convert_baudrate( |
| 749 | *device_baudrate); |
| 750 | |
| 751 | if (entry->len >= 13) |
| 752 | *flow_control = !!(entry->data[12] & BIT(2)); |
| 753 | else |
| 754 | *flow_control = false; |
| 755 | |
| 756 | found = true; |
| 757 | break; |
| 758 | |
| 759 | default: |
| 760 | rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n", |
| 761 | le16_to_cpu(entry->offset), entry->len); |
| 762 | break; |
| 763 | }; |
| 764 | |
| 765 | i += sizeof(*entry) + entry->len; |
| 766 | } |
| 767 | |
| 768 | if (!found) { |
| 769 | rtl_dev_err(hdev, "no UART config entry found\n"); |
| 770 | return -ENOENT; |
| 771 | } |
| 772 | |
| 773 | rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate); |
| 774 | rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate); |
| 775 | rtl_dev_dbg(hdev, "flow control %d\n", *flow_control); |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | EXPORT_SYMBOL_GPL(btrtl_get_uart_settings); |
| 780 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 781 | MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>"); |
| 782 | MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION); |
| 783 | MODULE_VERSION(VERSION); |
| 784 | MODULE_LICENSE("GPL"); |
Martin Blumenstingl | f96dbd3 | 2018-08-02 16:57:12 +0200 | [diff] [blame] | 785 | MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin"); |
| 786 | MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin"); |
| 787 | MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin"); |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 788 | MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin"); |
| 789 | MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin"); |
| 790 | MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin"); |
| 791 | MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin"); |
Martin Blumenstingl | f96dbd3 | 2018-08-02 16:57:12 +0200 | [diff] [blame] | 792 | MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin"); |
| 793 | MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin"); |
| 794 | MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin"); |
| 795 | MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin"); |
| 796 | MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin"); |
| 797 | MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin"); |