Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Bluetooth support for Realtek devices |
| 3 | * |
| 4 | * Copyright (C) 2015 Endless Mobile, Inc. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/firmware.h> |
| 20 | #include <asm/unaligned.h> |
| 21 | #include <linux/usb.h> |
| 22 | |
| 23 | #include <net/bluetooth/bluetooth.h> |
| 24 | #include <net/bluetooth/hci_core.h> |
| 25 | |
| 26 | #include "btrtl.h" |
| 27 | |
| 28 | #define VERSION "0.1" |
| 29 | |
| 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 |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 36 | #define RTL_ROM_LMP_8822B 0x8822 |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 37 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 38 | #define IC_MATCH_FL_LMPSUBV (1 << 0) |
| 39 | #define IC_MATCH_FL_HCIREV (1 << 1) |
| 40 | #define IC_INFO(lmps, hcir) \ |
| 41 | .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \ |
| 42 | .lmp_subver = (lmps), \ |
| 43 | .hci_rev = (hcir) |
| 44 | |
| 45 | struct id_table { |
| 46 | __u16 match_flags; |
| 47 | __u16 lmp_subver; |
| 48 | __u16 hci_rev; |
| 49 | bool config_needed; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 50 | bool has_rom_version; |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 51 | char *fw_name; |
| 52 | char *cfg_name; |
| 53 | }; |
| 54 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 55 | struct btrtl_device_info { |
| 56 | const struct id_table *ic_info; |
| 57 | u8 rom_version; |
| 58 | u8 *fw_data; |
| 59 | int fw_len; |
| 60 | u8 *cfg_data; |
| 61 | int cfg_len; |
| 62 | }; |
| 63 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 64 | static const struct id_table ic_id_table[] = { |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 65 | { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0, |
| 66 | .config_needed = false, |
| 67 | .has_rom_version = false, |
| 68 | .fw_name = "rtl_bt/rtl8723a_fw.bin", |
| 69 | .cfg_name = NULL }, |
| 70 | |
| 71 | { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0, |
| 72 | .config_needed = false, |
| 73 | .has_rom_version = false, |
| 74 | .fw_name = "rtl_bt/rtl8723a_fw.bin", |
| 75 | .cfg_name = NULL }, |
| 76 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 77 | /* 8723B */ |
| 78 | { IC_INFO(RTL_ROM_LMP_8723B, 0xb), |
| 79 | .config_needed = false, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 80 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 81 | .fw_name = "rtl_bt/rtl8723b_fw.bin", |
| 82 | .cfg_name = "rtl_bt/rtl8723b_config.bin" }, |
| 83 | |
| 84 | /* 8723D */ |
| 85 | { IC_INFO(RTL_ROM_LMP_8723B, 0xd), |
| 86 | .config_needed = true, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 87 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 88 | .fw_name = "rtl_bt/rtl8723d_fw.bin", |
| 89 | .cfg_name = "rtl_bt/rtl8723d_config.bin" }, |
| 90 | |
| 91 | /* 8821A */ |
| 92 | { IC_INFO(RTL_ROM_LMP_8821A, 0xa), |
| 93 | .config_needed = false, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 94 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 95 | .fw_name = "rtl_bt/rtl8821a_fw.bin", |
| 96 | .cfg_name = "rtl_bt/rtl8821a_config.bin" }, |
| 97 | |
| 98 | /* 8821C */ |
| 99 | { IC_INFO(RTL_ROM_LMP_8821A, 0xc), |
| 100 | .config_needed = false, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 101 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 102 | .fw_name = "rtl_bt/rtl8821c_fw.bin", |
| 103 | .cfg_name = "rtl_bt/rtl8821c_config.bin" }, |
| 104 | |
| 105 | /* 8761A */ |
| 106 | { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0, |
| 107 | .config_needed = false, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 108 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 109 | .fw_name = "rtl_bt/rtl8761a_fw.bin", |
| 110 | .cfg_name = "rtl_bt/rtl8761a_config.bin" }, |
| 111 | |
| 112 | /* 8822B */ |
| 113 | { IC_INFO(RTL_ROM_LMP_8822B, 0xb), |
| 114 | .config_needed = true, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 115 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 116 | .fw_name = "rtl_bt/rtl8822b_fw.bin", |
| 117 | .cfg_name = "rtl_bt/rtl8822b_config.bin" }, |
| 118 | }; |
| 119 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 120 | static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev) |
| 121 | { |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) { |
| 125 | if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) && |
| 126 | (ic_id_table[i].lmp_subver != lmp_subver)) |
| 127 | continue; |
| 128 | if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) && |
| 129 | (ic_id_table[i].hci_rev != hci_rev)) |
| 130 | continue; |
| 131 | |
| 132 | break; |
| 133 | } |
| 134 | if (i >= ARRAY_SIZE(ic_id_table)) |
| 135 | return NULL; |
| 136 | |
| 137 | return &ic_id_table[i]; |
| 138 | } |
| 139 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 140 | static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version) |
| 141 | { |
| 142 | struct rtl_rom_version_evt *rom_version; |
| 143 | struct sk_buff *skb; |
| 144 | |
| 145 | /* Read RTL ROM version command */ |
| 146 | skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT); |
| 147 | if (IS_ERR(skb)) { |
| 148 | BT_ERR("%s: Read ROM version failed (%ld)", |
| 149 | hdev->name, PTR_ERR(skb)); |
| 150 | return PTR_ERR(skb); |
| 151 | } |
| 152 | |
| 153 | if (skb->len != sizeof(*rom_version)) { |
| 154 | BT_ERR("%s: RTL version event length mismatch", hdev->name); |
| 155 | kfree_skb(skb); |
| 156 | return -EIO; |
| 157 | } |
| 158 | |
| 159 | rom_version = (struct rtl_rom_version_evt *)skb->data; |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 160 | bt_dev_info(hdev, "rom_version status=%x version=%x", |
| 161 | rom_version->status, rom_version->version); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 162 | |
| 163 | *version = rom_version->version; |
| 164 | |
| 165 | kfree_skb(skb); |
| 166 | return 0; |
| 167 | } |
| 168 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 169 | static int rtlbt_parse_firmware(struct hci_dev *hdev, |
| 170 | struct btrtl_device_info *btrtl_dev, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 171 | unsigned char **_buf) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 172 | { |
| 173 | const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 }; |
| 174 | struct rtl_epatch_header *epatch_info; |
| 175 | unsigned char *buf; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 176 | int i, len; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 177 | size_t min_size; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 178 | u8 opcode, length, data; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 179 | int project_id = -1; |
| 180 | const unsigned char *fwptr, *chip_id_base; |
| 181 | const unsigned char *patch_length_base, *patch_offset_base; |
| 182 | u32 patch_offset = 0; |
| 183 | u16 patch_length, num_patches; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 184 | static const struct { |
| 185 | __u16 lmp_subver; |
| 186 | __u8 id; |
| 187 | } project_id_to_lmp_subver[] = { |
| 188 | { RTL_ROM_LMP_8723A, 0 }, |
| 189 | { RTL_ROM_LMP_8723B, 1 }, |
| 190 | { RTL_ROM_LMP_8821A, 2 }, |
| 191 | { RTL_ROM_LMP_8761A, 3 }, |
| 192 | { RTL_ROM_LMP_8822B, 8 }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 193 | { RTL_ROM_LMP_8723B, 9 }, /* 8723D */ |
| 194 | { RTL_ROM_LMP_8821A, 10 }, /* 8821C */ |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 195 | }; |
| 196 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 197 | min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 198 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 199 | return -EINVAL; |
| 200 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 201 | fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 202 | if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) { |
| 203 | BT_ERR("%s: extension section signature mismatch", hdev->name); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | /* Loop from the end of the firmware parsing instructions, until |
| 208 | * we find an instruction that identifies the "project ID" for the |
| 209 | * hardware supported by this firwmare file. |
| 210 | * Once we have that, we double-check that that project_id is suitable |
| 211 | * for the hardware we are working with. |
| 212 | */ |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 213 | while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 214 | opcode = *--fwptr; |
| 215 | length = *--fwptr; |
| 216 | data = *--fwptr; |
| 217 | |
| 218 | BT_DBG("check op=%x len=%x data=%x", opcode, length, data); |
| 219 | |
| 220 | if (opcode == 0xff) /* EOF */ |
| 221 | break; |
| 222 | |
| 223 | if (length == 0) { |
| 224 | BT_ERR("%s: found instruction with length 0", |
| 225 | hdev->name); |
| 226 | return -EINVAL; |
| 227 | } |
| 228 | |
| 229 | if (opcode == 0 && length == 1) { |
| 230 | project_id = data; |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | fwptr -= length; |
| 235 | } |
| 236 | |
| 237 | if (project_id < 0) { |
| 238 | BT_ERR("%s: failed to find version instruction", hdev->name); |
| 239 | return -EINVAL; |
| 240 | } |
| 241 | |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 242 | /* Find project_id in table */ |
| 243 | for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) { |
| 244 | if (project_id == project_id_to_lmp_subver[i].id) |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 249 | BT_ERR("%s: unknown project id %d", hdev->name, project_id); |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 253 | if (btrtl_dev->ic_info->lmp_subver != |
| 254 | project_id_to_lmp_subver[i].lmp_subver) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 255 | BT_ERR("%s: firmware is for %x but this is a %x", hdev->name, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 256 | project_id_to_lmp_subver[i].lmp_subver, |
| 257 | btrtl_dev->ic_info->lmp_subver); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 258 | return -EINVAL; |
| 259 | } |
| 260 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 261 | epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 262 | if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) { |
| 263 | BT_ERR("%s: bad EPATCH signature", hdev->name); |
| 264 | return -EINVAL; |
| 265 | } |
| 266 | |
| 267 | num_patches = le16_to_cpu(epatch_info->num_patches); |
| 268 | BT_DBG("fw_version=%x, num_patches=%d", |
| 269 | le32_to_cpu(epatch_info->fw_version), num_patches); |
| 270 | |
| 271 | /* After the rtl_epatch_header there is a funky patch metadata section. |
| 272 | * Assuming 2 patches, the layout is: |
| 273 | * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2 |
| 274 | * |
| 275 | * Find the right patch for this chip. |
| 276 | */ |
| 277 | min_size += 8 * num_patches; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 278 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 279 | return -EINVAL; |
| 280 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 281 | chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 282 | patch_length_base = chip_id_base + (sizeof(u16) * num_patches); |
| 283 | patch_offset_base = patch_length_base + (sizeof(u16) * num_patches); |
| 284 | for (i = 0; i < num_patches; i++) { |
| 285 | u16 chip_id = get_unaligned_le16(chip_id_base + |
| 286 | (i * sizeof(u16))); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 287 | if (chip_id == btrtl_dev->rom_version + 1) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 288 | patch_length = get_unaligned_le16(patch_length_base + |
| 289 | (i * sizeof(u16))); |
| 290 | patch_offset = get_unaligned_le32(patch_offset_base + |
| 291 | (i * sizeof(u32))); |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (!patch_offset) { |
| 297 | BT_ERR("%s: didn't find patch for chip id %d", |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 298 | hdev->name, btrtl_dev->rom_version); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 299 | return -EINVAL; |
| 300 | } |
| 301 | |
| 302 | BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i); |
| 303 | min_size = patch_offset + patch_length; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 304 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 305 | return -EINVAL; |
| 306 | |
| 307 | /* Copy the firmware into a new buffer and write the version at |
| 308 | * the end. |
| 309 | */ |
| 310 | len = patch_length; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 311 | buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length, |
| 312 | GFP_KERNEL); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 313 | if (!buf) |
| 314 | return -ENOMEM; |
| 315 | |
| 316 | memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4); |
| 317 | |
| 318 | *_buf = buf; |
| 319 | return len; |
| 320 | } |
| 321 | |
| 322 | static int rtl_download_firmware(struct hci_dev *hdev, |
| 323 | const unsigned char *data, int fw_len) |
| 324 | { |
| 325 | struct rtl_download_cmd *dl_cmd; |
| 326 | int frag_num = fw_len / RTL_FRAG_LEN + 1; |
| 327 | int frag_len = RTL_FRAG_LEN; |
| 328 | int ret = 0; |
| 329 | int i; |
| 330 | |
| 331 | dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL); |
| 332 | if (!dl_cmd) |
| 333 | return -ENOMEM; |
| 334 | |
| 335 | for (i = 0; i < frag_num; i++) { |
| 336 | struct sk_buff *skb; |
| 337 | |
| 338 | BT_DBG("download fw (%d/%d)", i, frag_num); |
| 339 | |
| 340 | dl_cmd->index = i; |
| 341 | if (i == (frag_num - 1)) { |
| 342 | dl_cmd->index |= 0x80; /* data end */ |
| 343 | frag_len = fw_len % RTL_FRAG_LEN; |
| 344 | } |
| 345 | memcpy(dl_cmd->data, data, frag_len); |
| 346 | |
| 347 | /* Send download command */ |
| 348 | skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd, |
| 349 | HCI_INIT_TIMEOUT); |
| 350 | if (IS_ERR(skb)) { |
| 351 | BT_ERR("%s: download fw command failed (%ld)", |
| 352 | hdev->name, PTR_ERR(skb)); |
| 353 | ret = -PTR_ERR(skb); |
| 354 | goto out; |
| 355 | } |
| 356 | |
| 357 | if (skb->len != sizeof(struct rtl_download_response)) { |
| 358 | BT_ERR("%s: download fw event length mismatch", |
| 359 | hdev->name); |
| 360 | kfree_skb(skb); |
| 361 | ret = -EIO; |
| 362 | goto out; |
| 363 | } |
| 364 | |
| 365 | kfree_skb(skb); |
| 366 | data += RTL_FRAG_LEN; |
| 367 | } |
| 368 | |
| 369 | out: |
| 370 | kfree(dl_cmd); |
| 371 | return ret; |
| 372 | } |
| 373 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 374 | 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] | 375 | { |
| 376 | const struct firmware *fw; |
| 377 | int ret; |
| 378 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 379 | bt_dev_info(hdev, "rtl: loading %s", name); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 380 | ret = request_firmware(&fw, name, &hdev->dev); |
Larry Finger | abed84a | 2017-02-19 20:04:57 -0600 | [diff] [blame] | 381 | if (ret < 0) |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 382 | return ret; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 383 | ret = fw->size; |
| 384 | *buff = kmemdup(fw->data, ret, GFP_KERNEL); |
Dan Carpenter | c3327bd | 2017-07-28 17:41:11 +0300 | [diff] [blame] | 385 | if (!*buff) |
| 386 | ret = -ENOMEM; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 387 | |
| 388 | release_firmware(fw); |
| 389 | |
| 390 | return ret; |
| 391 | } |
| 392 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 393 | static int btrtl_setup_rtl8723a(struct hci_dev *hdev, |
| 394 | struct btrtl_device_info *btrtl_dev) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 395 | { |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 396 | if (btrtl_dev->fw_len < 8) |
| 397 | return -EINVAL; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 398 | |
| 399 | /* Check that the firmware doesn't have the epatch signature |
| 400 | * (which is only for RTL8723B and newer). |
| 401 | */ |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 402 | if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 403 | BT_ERR("%s: unexpected EPATCH signature!", hdev->name); |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 404 | return -EINVAL; |
| 405 | } |
| 406 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 407 | return rtl_download_firmware(hdev, btrtl_dev->fw_data, |
| 408 | btrtl_dev->fw_len); |
| 409 | } |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 410 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 411 | static int btrtl_setup_rtl8723b(struct hci_dev *hdev, |
| 412 | struct btrtl_device_info *btrtl_dev) |
| 413 | { |
| 414 | unsigned char *fw_data = NULL; |
| 415 | int ret; |
| 416 | u8 *tbuff; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 417 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 418 | ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 419 | if (ret < 0) |
| 420 | goto out; |
| 421 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 422 | if (btrtl_dev->cfg_len > 0) { |
| 423 | tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 424 | if (!tbuff) { |
| 425 | ret = -ENOMEM; |
| 426 | goto out; |
| 427 | } |
| 428 | |
| 429 | memcpy(tbuff, fw_data, ret); |
| 430 | kfree(fw_data); |
| 431 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 432 | memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len); |
| 433 | ret += btrtl_dev->cfg_len; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 434 | |
| 435 | fw_data = tbuff; |
| 436 | } |
| 437 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 438 | 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] | 439 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 440 | ret = rtl_download_firmware(hdev, fw_data, ret); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 441 | |
| 442 | out: |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 443 | kfree(fw_data); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 444 | return ret; |
| 445 | } |
| 446 | |
| 447 | static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev) |
| 448 | { |
| 449 | struct sk_buff *skb; |
| 450 | |
| 451 | skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, |
| 452 | HCI_INIT_TIMEOUT); |
| 453 | if (IS_ERR(skb)) { |
| 454 | BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)", |
| 455 | hdev->name, PTR_ERR(skb)); |
| 456 | return skb; |
| 457 | } |
| 458 | |
| 459 | if (skb->len != sizeof(struct hci_rp_read_local_version)) { |
| 460 | BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch", |
| 461 | hdev->name); |
| 462 | kfree_skb(skb); |
| 463 | return ERR_PTR(-EIO); |
| 464 | } |
| 465 | |
| 466 | return skb; |
| 467 | } |
| 468 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 469 | void btrtl_free(struct btrtl_device_info *btrtl_dev) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 470 | { |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 471 | kfree(btrtl_dev->fw_data); |
| 472 | kfree(btrtl_dev->cfg_data); |
| 473 | kfree(btrtl_dev); |
| 474 | } |
| 475 | EXPORT_SYMBOL_GPL(btrtl_free); |
| 476 | |
| 477 | struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev) |
| 478 | { |
| 479 | struct btrtl_device_info *btrtl_dev; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 480 | struct sk_buff *skb; |
| 481 | struct hci_rp_read_local_version *resp; |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 482 | u16 hci_rev, lmp_subver; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 483 | int ret; |
| 484 | |
| 485 | btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL); |
| 486 | if (!btrtl_dev) { |
| 487 | ret = -ENOMEM; |
| 488 | goto err_alloc; |
| 489 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 490 | |
| 491 | skb = btrtl_read_local_version(hdev); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 492 | if (IS_ERR(skb)) { |
| 493 | ret = PTR_ERR(skb); |
| 494 | goto err_free; |
| 495 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 496 | |
| 497 | resp = (struct hci_rp_read_local_version *)skb->data; |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 498 | bt_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x " |
| 499 | "lmp_ver=%02x lmp_subver=%04x", |
| 500 | resp->hci_ver, resp->hci_rev, |
| 501 | resp->lmp_ver, resp->lmp_subver); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 502 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 503 | hci_rev = le16_to_cpu(resp->hci_rev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 504 | lmp_subver = le16_to_cpu(resp->lmp_subver); |
| 505 | kfree_skb(skb); |
| 506 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 507 | btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev); |
| 508 | if (!btrtl_dev->ic_info) { |
| 509 | rtl_dev_err(hdev, "unknown IC info, lmp subver %04x, hci rev %04x\n", |
| 510 | lmp_subver, hci_rev); |
| 511 | ret = -EINVAL; |
| 512 | goto err_free; |
| 513 | } |
| 514 | |
| 515 | if (btrtl_dev->ic_info->has_rom_version) { |
| 516 | ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version); |
| 517 | if (ret) |
| 518 | goto err_free; |
| 519 | } |
| 520 | |
| 521 | btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name, |
| 522 | &btrtl_dev->fw_data); |
| 523 | if (btrtl_dev->fw_len < 0) { |
| 524 | rtl_dev_err(hdev, "firmware file %s not found\n", |
| 525 | btrtl_dev->ic_info->fw_name); |
| 526 | ret = btrtl_dev->fw_len; |
| 527 | goto err_free; |
| 528 | } |
| 529 | |
| 530 | if (btrtl_dev->ic_info->cfg_name) { |
| 531 | btrtl_dev->cfg_len = rtl_load_file(hdev, |
| 532 | btrtl_dev->ic_info->cfg_name, |
| 533 | &btrtl_dev->cfg_data); |
| 534 | if (btrtl_dev->ic_info->config_needed && |
| 535 | btrtl_dev->cfg_len <= 0) { |
| 536 | rtl_dev_err(hdev, "mandatory config file %s not found\n", |
| 537 | btrtl_dev->ic_info->cfg_name); |
| 538 | ret = btrtl_dev->cfg_len; |
| 539 | goto err_free; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return btrtl_dev; |
| 544 | |
| 545 | err_free: |
| 546 | btrtl_free(btrtl_dev); |
| 547 | err_alloc: |
| 548 | return ERR_PTR(ret); |
| 549 | } |
| 550 | EXPORT_SYMBOL_GPL(btrtl_initialize); |
| 551 | |
| 552 | int btrtl_download_firmware(struct hci_dev *hdev, |
| 553 | struct btrtl_device_info *btrtl_dev) |
| 554 | { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 555 | /* Match a set of subver values that correspond to stock firmware, |
| 556 | * which is not compatible with standard btusb. |
| 557 | * If matched, upload an alternative firmware that does conform to |
| 558 | * standard btusb. Once that firmware is uploaded, the subver changes |
| 559 | * to a different value. |
| 560 | */ |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 561 | switch (btrtl_dev->ic_info->lmp_subver) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 562 | case RTL_ROM_LMP_8723A: |
| 563 | case RTL_ROM_LMP_3499: |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 564 | return btrtl_setup_rtl8723a(hdev, btrtl_dev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 565 | case RTL_ROM_LMP_8723B: |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 566 | case RTL_ROM_LMP_8821A: |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 567 | case RTL_ROM_LMP_8761A: |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 568 | case RTL_ROM_LMP_8822B: |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 569 | return btrtl_setup_rtl8723b(hdev, btrtl_dev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 570 | default: |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 571 | bt_dev_info(hdev, "rtl: assuming no firmware upload needed"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | } |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame^] | 575 | EXPORT_SYMBOL_GPL(btrtl_download_firmware); |
| 576 | |
| 577 | int btrtl_setup_realtek(struct hci_dev *hdev) |
| 578 | { |
| 579 | struct btrtl_device_info *btrtl_dev; |
| 580 | int ret; |
| 581 | |
| 582 | btrtl_dev = btrtl_initialize(hdev); |
| 583 | if (IS_ERR(btrtl_dev)) |
| 584 | return PTR_ERR(btrtl_dev); |
| 585 | |
| 586 | ret = btrtl_download_firmware(hdev, btrtl_dev); |
| 587 | |
| 588 | btrtl_free(btrtl_dev); |
| 589 | |
| 590 | return ret; |
| 591 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 592 | EXPORT_SYMBOL_GPL(btrtl_setup_realtek); |
| 593 | |
| 594 | MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>"); |
| 595 | MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION); |
| 596 | MODULE_VERSION(VERSION); |
| 597 | MODULE_LICENSE("GPL"); |
Martin Blumenstingl | f96dbd3 | 2018-08-02 16:57:12 +0200 | [diff] [blame] | 598 | MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin"); |
| 599 | MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin"); |
| 600 | MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin"); |
| 601 | MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin"); |
| 602 | MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin"); |
| 603 | MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin"); |
| 604 | MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin"); |
| 605 | MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin"); |
| 606 | MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin"); |