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 | |
Max Chou | 848fc61 | 2020-02-17 16:14:55 +0800 | [diff] [blame^] | 139 | /* 8822C with UART interface */ |
| 140 | { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | |
| 141 | IC_MATCH_FL_HCIBUS, |
| 142 | .lmp_subver = RTL_ROM_LMP_8822B, |
| 143 | .hci_rev = 0x000c, |
| 144 | .hci_ver = 0x0a, |
| 145 | .hci_bus = HCI_UART, |
| 146 | .config_needed = true, |
| 147 | .has_rom_version = true, |
| 148 | .fw_name = "rtl_bt/rtl8822cs_fw.bin", |
| 149 | .cfg_name = "rtl_bt/rtl8822cs_config" }, |
| 150 | |
Alex Lu | 8ecfdc9 | 2018-09-08 12:05:59 -0500 | [diff] [blame] | 151 | /* 8822C with USB interface */ |
| 152 | { IC_INFO(RTL_ROM_LMP_8822B, 0xc), |
| 153 | .config_needed = false, |
| 154 | .has_rom_version = true, |
| 155 | .fw_name = "rtl_bt/rtl8822cu_fw.bin", |
| 156 | .cfg_name = "rtl_bt/rtl8822cu_config" }, |
| 157 | |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 158 | /* 8822B */ |
| 159 | { IC_INFO(RTL_ROM_LMP_8822B, 0xb), |
| 160 | .config_needed = true, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 161 | .has_rom_version = true, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 162 | .fw_name = "rtl_bt/rtl8822b_fw.bin", |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 163 | .cfg_name = "rtl_bt/rtl8822b_config" }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 164 | }; |
| 165 | |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 166 | static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev, |
| 167 | u8 hci_ver, u8 hci_bus) |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 168 | { |
| 169 | int i; |
| 170 | |
| 171 | for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) { |
| 172 | if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) && |
| 173 | (ic_id_table[i].lmp_subver != lmp_subver)) |
| 174 | continue; |
| 175 | if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) && |
| 176 | (ic_id_table[i].hci_rev != hci_rev)) |
| 177 | continue; |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 178 | if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) && |
| 179 | (ic_id_table[i].hci_ver != hci_ver)) |
| 180 | continue; |
| 181 | if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) && |
| 182 | (ic_id_table[i].hci_bus != hci_bus)) |
| 183 | continue; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 184 | |
| 185 | break; |
| 186 | } |
| 187 | if (i >= ARRAY_SIZE(ic_id_table)) |
| 188 | return NULL; |
| 189 | |
| 190 | return &ic_id_table[i]; |
| 191 | } |
| 192 | |
Alex Lu | 240b64a | 2019-08-31 16:36:02 +0800 | [diff] [blame] | 193 | static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev) |
| 194 | { |
| 195 | struct sk_buff *skb; |
| 196 | |
| 197 | skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, |
| 198 | HCI_INIT_TIMEOUT); |
| 199 | if (IS_ERR(skb)) { |
| 200 | rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)", |
| 201 | PTR_ERR(skb)); |
| 202 | return skb; |
| 203 | } |
| 204 | |
| 205 | if (skb->len != sizeof(struct hci_rp_read_local_version)) { |
| 206 | rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch"); |
| 207 | kfree_skb(skb); |
| 208 | return ERR_PTR(-EIO); |
| 209 | } |
| 210 | |
| 211 | return skb; |
| 212 | } |
| 213 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 214 | static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version) |
| 215 | { |
| 216 | struct rtl_rom_version_evt *rom_version; |
| 217 | struct sk_buff *skb; |
| 218 | |
| 219 | /* Read RTL ROM version command */ |
| 220 | skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT); |
| 221 | if (IS_ERR(skb)) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 222 | rtl_dev_err(hdev, "Read ROM version failed (%ld)", |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 223 | PTR_ERR(skb)); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 224 | return PTR_ERR(skb); |
| 225 | } |
| 226 | |
| 227 | if (skb->len != sizeof(*rom_version)) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 228 | rtl_dev_err(hdev, "version event length mismatch"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 229 | kfree_skb(skb); |
| 230 | return -EIO; |
| 231 | } |
| 232 | |
| 233 | rom_version = (struct rtl_rom_version_evt *)skb->data; |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 234 | rtl_dev_info(hdev, "rom_version status=%x version=%x", |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 235 | rom_version->status, rom_version->version); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 236 | |
| 237 | *version = rom_version->version; |
| 238 | |
| 239 | kfree_skb(skb); |
| 240 | return 0; |
| 241 | } |
| 242 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 243 | static int rtlbt_parse_firmware(struct hci_dev *hdev, |
| 244 | struct btrtl_device_info *btrtl_dev, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 245 | unsigned char **_buf) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 246 | { |
Colin Ian King | e5070e07 | 2018-09-26 09:32:08 +0100 | [diff] [blame] | 247 | static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 }; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 248 | struct rtl_epatch_header *epatch_info; |
| 249 | unsigned char *buf; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 250 | int i, len; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 251 | size_t min_size; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 252 | u8 opcode, length, data; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 253 | int project_id = -1; |
| 254 | const unsigned char *fwptr, *chip_id_base; |
| 255 | const unsigned char *patch_length_base, *patch_offset_base; |
| 256 | u32 patch_offset = 0; |
| 257 | u16 patch_length, num_patches; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 258 | static const struct { |
| 259 | __u16 lmp_subver; |
| 260 | __u8 id; |
| 261 | } project_id_to_lmp_subver[] = { |
| 262 | { RTL_ROM_LMP_8723A, 0 }, |
| 263 | { RTL_ROM_LMP_8723B, 1 }, |
| 264 | { RTL_ROM_LMP_8821A, 2 }, |
| 265 | { RTL_ROM_LMP_8761A, 3 }, |
| 266 | { RTL_ROM_LMP_8822B, 8 }, |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 267 | { RTL_ROM_LMP_8723B, 9 }, /* 8723D */ |
| 268 | { RTL_ROM_LMP_8821A, 10 }, /* 8821C */ |
Alex Lu | 8ecfdc9 | 2018-09-08 12:05:59 -0500 | [diff] [blame] | 269 | { RTL_ROM_LMP_8822B, 13 }, /* 8822C */ |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 270 | }; |
| 271 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 272 | min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 273 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 274 | return -EINVAL; |
| 275 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 276 | fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 277 | if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 278 | rtl_dev_err(hdev, "extension section signature mismatch"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | /* Loop from the end of the firmware parsing instructions, until |
| 283 | * we find an instruction that identifies the "project ID" for the |
| 284 | * hardware supported by this firwmare file. |
| 285 | * Once we have that, we double-check that that project_id is suitable |
| 286 | * for the hardware we are working with. |
| 287 | */ |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 288 | while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 289 | opcode = *--fwptr; |
| 290 | length = *--fwptr; |
| 291 | data = *--fwptr; |
| 292 | |
| 293 | BT_DBG("check op=%x len=%x data=%x", opcode, length, data); |
| 294 | |
| 295 | if (opcode == 0xff) /* EOF */ |
| 296 | break; |
| 297 | |
| 298 | if (length == 0) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 299 | rtl_dev_err(hdev, "found instruction with length 0"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 300 | return -EINVAL; |
| 301 | } |
| 302 | |
| 303 | if (opcode == 0 && length == 1) { |
| 304 | project_id = data; |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | fwptr -= length; |
| 309 | } |
| 310 | |
| 311 | if (project_id < 0) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 312 | rtl_dev_err(hdev, "failed to find version instruction"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 313 | return -EINVAL; |
| 314 | } |
| 315 | |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 316 | /* Find project_id in table */ |
| 317 | for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) { |
| 318 | if (project_id == project_id_to_lmp_subver[i].id) |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 323 | rtl_dev_err(hdev, "unknown project id %d", project_id); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 324 | return -EINVAL; |
| 325 | } |
| 326 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 327 | if (btrtl_dev->ic_info->lmp_subver != |
| 328 | project_id_to_lmp_subver[i].lmp_subver) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 329 | rtl_dev_err(hdev, "firmware is for %x but this is a %x", |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 330 | project_id_to_lmp_subver[i].lmp_subver, |
| 331 | btrtl_dev->ic_info->lmp_subver); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 332 | return -EINVAL; |
| 333 | } |
| 334 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 335 | epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 336 | if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 337 | rtl_dev_err(hdev, "bad EPATCH signature"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | num_patches = le16_to_cpu(epatch_info->num_patches); |
| 342 | BT_DBG("fw_version=%x, num_patches=%d", |
| 343 | le32_to_cpu(epatch_info->fw_version), num_patches); |
| 344 | |
| 345 | /* After the rtl_epatch_header there is a funky patch metadata section. |
| 346 | * Assuming 2 patches, the layout is: |
| 347 | * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2 |
| 348 | * |
| 349 | * Find the right patch for this chip. |
| 350 | */ |
| 351 | min_size += 8 * num_patches; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 352 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 353 | return -EINVAL; |
| 354 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 355 | chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 356 | patch_length_base = chip_id_base + (sizeof(u16) * num_patches); |
| 357 | patch_offset_base = patch_length_base + (sizeof(u16) * num_patches); |
| 358 | for (i = 0; i < num_patches; i++) { |
| 359 | u16 chip_id = get_unaligned_le16(chip_id_base + |
| 360 | (i * sizeof(u16))); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 361 | if (chip_id == btrtl_dev->rom_version + 1) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 362 | patch_length = get_unaligned_le16(patch_length_base + |
| 363 | (i * sizeof(u16))); |
| 364 | patch_offset = get_unaligned_le32(patch_offset_base + |
| 365 | (i * sizeof(u32))); |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (!patch_offset) { |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 371 | rtl_dev_err(hdev, "didn't find patch for chip id %d", |
| 372 | btrtl_dev->rom_version); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 373 | return -EINVAL; |
| 374 | } |
| 375 | |
| 376 | BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i); |
| 377 | min_size = patch_offset + patch_length; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 378 | if (btrtl_dev->fw_len < min_size) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 379 | return -EINVAL; |
| 380 | |
| 381 | /* Copy the firmware into a new buffer and write the version at |
| 382 | * the end. |
| 383 | */ |
| 384 | len = patch_length; |
Maxim Mikityanskiy | 268d363 | 2020-01-24 19:15:35 +0200 | [diff] [blame] | 385 | buf = kvmalloc(patch_length, GFP_KERNEL); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 386 | if (!buf) |
| 387 | return -ENOMEM; |
| 388 | |
Maxim Mikityanskiy | 268d363 | 2020-01-24 19:15:35 +0200 | [diff] [blame] | 389 | memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 390 | memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4); |
| 391 | |
| 392 | *_buf = buf; |
| 393 | return len; |
| 394 | } |
| 395 | |
| 396 | static int rtl_download_firmware(struct hci_dev *hdev, |
| 397 | const unsigned char *data, int fw_len) |
| 398 | { |
| 399 | struct rtl_download_cmd *dl_cmd; |
| 400 | int frag_num = fw_len / RTL_FRAG_LEN + 1; |
| 401 | int frag_len = RTL_FRAG_LEN; |
| 402 | int ret = 0; |
| 403 | int i; |
Alex Lu | 240b64a | 2019-08-31 16:36:02 +0800 | [diff] [blame] | 404 | struct sk_buff *skb; |
| 405 | struct hci_rp_read_local_version *rp; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 406 | |
| 407 | dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL); |
| 408 | if (!dl_cmd) |
| 409 | return -ENOMEM; |
| 410 | |
| 411 | for (i = 0; i < frag_num; i++) { |
| 412 | struct sk_buff *skb; |
| 413 | |
| 414 | BT_DBG("download fw (%d/%d)", i, frag_num); |
| 415 | |
Max Chou | cf0d9a7 | 2019-09-05 13:21:57 +0800 | [diff] [blame] | 416 | if (i > 0x7f) |
| 417 | dl_cmd->index = (i & 0x7f) + 1; |
| 418 | else |
| 419 | dl_cmd->index = i; |
| 420 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 421 | if (i == (frag_num - 1)) { |
| 422 | dl_cmd->index |= 0x80; /* data end */ |
| 423 | frag_len = fw_len % RTL_FRAG_LEN; |
| 424 | } |
| 425 | memcpy(dl_cmd->data, data, frag_len); |
| 426 | |
| 427 | /* Send download command */ |
| 428 | skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd, |
| 429 | HCI_INIT_TIMEOUT); |
| 430 | if (IS_ERR(skb)) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 431 | rtl_dev_err(hdev, "download fw command failed (%ld)", |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 432 | PTR_ERR(skb)); |
Max Chou | d171dfb | 2019-09-18 16:56:41 +0800 | [diff] [blame] | 433 | ret = PTR_ERR(skb); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 434 | goto out; |
| 435 | } |
| 436 | |
| 437 | if (skb->len != sizeof(struct rtl_download_response)) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 438 | rtl_dev_err(hdev, "download fw event length mismatch"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 439 | kfree_skb(skb); |
| 440 | ret = -EIO; |
| 441 | goto out; |
| 442 | } |
| 443 | |
| 444 | kfree_skb(skb); |
| 445 | data += RTL_FRAG_LEN; |
| 446 | } |
| 447 | |
Alex Lu | 240b64a | 2019-08-31 16:36:02 +0800 | [diff] [blame] | 448 | skb = btrtl_read_local_version(hdev); |
| 449 | if (IS_ERR(skb)) { |
| 450 | ret = PTR_ERR(skb); |
| 451 | rtl_dev_err(hdev, "read local version failed"); |
| 452 | goto out; |
| 453 | } |
| 454 | |
| 455 | rp = (struct hci_rp_read_local_version *)skb->data; |
| 456 | rtl_dev_info(hdev, "fw version 0x%04x%04x", |
| 457 | __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver)); |
| 458 | kfree_skb(skb); |
| 459 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 460 | out: |
| 461 | kfree(dl_cmd); |
| 462 | return ret; |
| 463 | } |
| 464 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 465 | 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] | 466 | { |
| 467 | const struct firmware *fw; |
| 468 | int ret; |
| 469 | |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 470 | rtl_dev_info(hdev, "loading %s", name); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 471 | ret = request_firmware(&fw, name, &hdev->dev); |
Larry Finger | abed84a | 2017-02-19 20:04:57 -0600 | [diff] [blame] | 472 | if (ret < 0) |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 473 | return ret; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 474 | ret = fw->size; |
Maxim Mikityanskiy | 268d363 | 2020-01-24 19:15:35 +0200 | [diff] [blame] | 475 | *buff = kvmalloc(fw->size, GFP_KERNEL); |
| 476 | if (*buff) |
| 477 | memcpy(*buff, fw->data, ret); |
| 478 | else |
Dan Carpenter | c3327bd | 2017-07-28 17:41:11 +0300 | [diff] [blame] | 479 | ret = -ENOMEM; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 480 | |
| 481 | release_firmware(fw); |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 486 | static int btrtl_setup_rtl8723a(struct hci_dev *hdev, |
| 487 | struct btrtl_device_info *btrtl_dev) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 488 | { |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 489 | if (btrtl_dev->fw_len < 8) |
| 490 | return -EINVAL; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 491 | |
| 492 | /* Check that the firmware doesn't have the epatch signature |
| 493 | * (which is only for RTL8723B and newer). |
| 494 | */ |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 495 | if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 496 | rtl_dev_err(hdev, "unexpected EPATCH signature!"); |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 497 | return -EINVAL; |
| 498 | } |
| 499 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 500 | return rtl_download_firmware(hdev, btrtl_dev->fw_data, |
| 501 | btrtl_dev->fw_len); |
| 502 | } |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 503 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 504 | static int btrtl_setup_rtl8723b(struct hci_dev *hdev, |
| 505 | struct btrtl_device_info *btrtl_dev) |
| 506 | { |
| 507 | unsigned char *fw_data = NULL; |
| 508 | int ret; |
| 509 | u8 *tbuff; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 510 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 511 | ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 512 | if (ret < 0) |
| 513 | goto out; |
| 514 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 515 | if (btrtl_dev->cfg_len > 0) { |
Maxim Mikityanskiy | 268d363 | 2020-01-24 19:15:35 +0200 | [diff] [blame] | 516 | tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 517 | if (!tbuff) { |
| 518 | ret = -ENOMEM; |
| 519 | goto out; |
| 520 | } |
| 521 | |
| 522 | memcpy(tbuff, fw_data, ret); |
Maxim Mikityanskiy | 268d363 | 2020-01-24 19:15:35 +0200 | [diff] [blame] | 523 | kvfree(fw_data); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 524 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 525 | memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len); |
| 526 | ret += btrtl_dev->cfg_len; |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 527 | |
| 528 | fw_data = tbuff; |
| 529 | } |
| 530 | |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 531 | rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret); |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 532 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 533 | ret = rtl_download_firmware(hdev, fw_data, ret); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 534 | |
| 535 | out: |
Maxim Mikityanskiy | 268d363 | 2020-01-24 19:15:35 +0200 | [diff] [blame] | 536 | kvfree(fw_data); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 540 | void btrtl_free(struct btrtl_device_info *btrtl_dev) |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 541 | { |
Maxim Mikityanskiy | 268d363 | 2020-01-24 19:15:35 +0200 | [diff] [blame] | 542 | kvfree(btrtl_dev->fw_data); |
| 543 | kvfree(btrtl_dev->cfg_data); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 544 | kfree(btrtl_dev); |
| 545 | } |
| 546 | EXPORT_SYMBOL_GPL(btrtl_free); |
| 547 | |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 548 | struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev, |
| 549 | const char *postfix) |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 550 | { |
| 551 | struct btrtl_device_info *btrtl_dev; |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 552 | struct sk_buff *skb; |
| 553 | struct hci_rp_read_local_version *resp; |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 554 | char cfg_name[40]; |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 555 | u16 hci_rev, lmp_subver; |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 556 | u8 hci_ver; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 557 | int ret; |
| 558 | |
| 559 | btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL); |
| 560 | if (!btrtl_dev) { |
| 561 | ret = -ENOMEM; |
| 562 | goto err_alloc; |
| 563 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 564 | |
| 565 | skb = btrtl_read_local_version(hdev); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 566 | if (IS_ERR(skb)) { |
| 567 | ret = PTR_ERR(skb); |
| 568 | goto err_free; |
| 569 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 570 | |
| 571 | resp = (struct hci_rp_read_local_version *)skb->data; |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 572 | rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x", |
Hans de Goede | a5c76e6 | 2018-08-02 16:57:14 +0200 | [diff] [blame] | 573 | resp->hci_ver, resp->hci_rev, |
| 574 | resp->lmp_ver, resp->lmp_subver); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 575 | |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 576 | hci_ver = resp->hci_ver; |
Alex Lu | 907f849 | 2018-02-11 12:24:33 -0600 | [diff] [blame] | 577 | hci_rev = le16_to_cpu(resp->hci_rev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 578 | lmp_subver = le16_to_cpu(resp->lmp_subver); |
| 579 | kfree_skb(skb); |
| 580 | |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 581 | btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, |
| 582 | hdev->bus); |
| 583 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 584 | if (!btrtl_dev->ic_info) { |
Alex Lu | d182215 | 2019-08-31 16:41:13 +0800 | [diff] [blame] | 585 | rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x", |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 586 | lmp_subver, hci_rev, hci_ver); |
Kai-Heng Feng | 00df214 | 2019-01-27 16:33:59 +0800 | [diff] [blame] | 587 | return btrtl_dev; |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | if (btrtl_dev->ic_info->has_rom_version) { |
| 591 | ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version); |
| 592 | if (ret) |
| 593 | goto err_free; |
| 594 | } |
| 595 | |
| 596 | btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name, |
| 597 | &btrtl_dev->fw_data); |
| 598 | if (btrtl_dev->fw_len < 0) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 599 | rtl_dev_err(hdev, "firmware file %s not found", |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 600 | btrtl_dev->ic_info->fw_name); |
| 601 | ret = btrtl_dev->fw_len; |
| 602 | goto err_free; |
| 603 | } |
| 604 | |
| 605 | if (btrtl_dev->ic_info->cfg_name) { |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 606 | if (postfix) { |
| 607 | snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin", |
| 608 | btrtl_dev->ic_info->cfg_name, postfix); |
| 609 | } else { |
| 610 | snprintf(cfg_name, sizeof(cfg_name), "%s.bin", |
| 611 | btrtl_dev->ic_info->cfg_name); |
| 612 | } |
| 613 | btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name, |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 614 | &btrtl_dev->cfg_data); |
| 615 | if (btrtl_dev->ic_info->config_needed && |
| 616 | btrtl_dev->cfg_len <= 0) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 617 | rtl_dev_err(hdev, "mandatory config file %s not found", |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 618 | btrtl_dev->ic_info->cfg_name); |
| 619 | ret = btrtl_dev->cfg_len; |
| 620 | goto err_free; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | return btrtl_dev; |
| 625 | |
| 626 | err_free: |
| 627 | btrtl_free(btrtl_dev); |
| 628 | err_alloc: |
| 629 | return ERR_PTR(ret); |
| 630 | } |
| 631 | EXPORT_SYMBOL_GPL(btrtl_initialize); |
| 632 | |
| 633 | int btrtl_download_firmware(struct hci_dev *hdev, |
| 634 | struct btrtl_device_info *btrtl_dev) |
| 635 | { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 636 | /* Match a set of subver values that correspond to stock firmware, |
| 637 | * which is not compatible with standard btusb. |
| 638 | * If matched, upload an alternative firmware that does conform to |
| 639 | * standard btusb. Once that firmware is uploaded, the subver changes |
| 640 | * to a different value. |
| 641 | */ |
Kai-Heng Feng | 00df214 | 2019-01-27 16:33:59 +0800 | [diff] [blame] | 642 | if (!btrtl_dev->ic_info) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 643 | rtl_dev_info(hdev, "assuming no firmware upload needed"); |
Kai-Heng Feng | 00df214 | 2019-01-27 16:33:59 +0800 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 647 | switch (btrtl_dev->ic_info->lmp_subver) { |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 648 | case RTL_ROM_LMP_8723A: |
| 649 | case RTL_ROM_LMP_3499: |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 650 | return btrtl_setup_rtl8723a(hdev, btrtl_dev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 651 | case RTL_ROM_LMP_8723B: |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 652 | case RTL_ROM_LMP_8821A: |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 653 | case RTL_ROM_LMP_8761A: |
Larry Finger | 1110a2d | 2016-09-09 10:02:05 -0500 | [diff] [blame] | 654 | case RTL_ROM_LMP_8822B: |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 655 | return btrtl_setup_rtl8723b(hdev, btrtl_dev); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 656 | default: |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 657 | rtl_dev_info(hdev, "assuming no firmware upload needed"); |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | } |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 661 | EXPORT_SYMBOL_GPL(btrtl_download_firmware); |
| 662 | |
| 663 | int btrtl_setup_realtek(struct hci_dev *hdev) |
| 664 | { |
| 665 | struct btrtl_device_info *btrtl_dev; |
| 666 | int ret; |
| 667 | |
Hans de Goede | 1cc194c | 2018-08-02 16:57:17 +0200 | [diff] [blame] | 668 | btrtl_dev = btrtl_initialize(hdev, NULL); |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 669 | if (IS_ERR(btrtl_dev)) |
| 670 | return PTR_ERR(btrtl_dev); |
| 671 | |
| 672 | ret = btrtl_download_firmware(hdev, btrtl_dev); |
| 673 | |
| 674 | btrtl_free(btrtl_dev); |
| 675 | |
Alex Lu | 65251e2 | 2019-08-30 20:02:14 +0800 | [diff] [blame] | 676 | /* Enable controller to do both LE scan and BR/EDR inquiry |
| 677 | * simultaneously. |
| 678 | */ |
| 679 | set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); |
| 680 | |
Martin Blumenstingl | 26503ad2 | 2018-08-02 16:57:13 +0200 | [diff] [blame] | 681 | return ret; |
| 682 | } |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 683 | EXPORT_SYMBOL_GPL(btrtl_setup_realtek); |
| 684 | |
Jian-Hong Pan | 7af3f558 | 2019-06-25 16:30:51 +0800 | [diff] [blame] | 685 | int btrtl_shutdown_realtek(struct hci_dev *hdev) |
| 686 | { |
| 687 | struct sk_buff *skb; |
| 688 | int ret; |
| 689 | |
| 690 | /* According to the vendor driver, BT must be reset on close to avoid |
| 691 | * firmware crash. |
| 692 | */ |
| 693 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); |
| 694 | if (IS_ERR(skb)) { |
| 695 | ret = PTR_ERR(skb); |
| 696 | bt_dev_err(hdev, "HCI reset during shutdown failed"); |
| 697 | return ret; |
| 698 | } |
| 699 | kfree_skb(skb); |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek); |
| 704 | |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 705 | static unsigned int btrtl_convert_baudrate(u32 device_baudrate) |
| 706 | { |
| 707 | switch (device_baudrate) { |
| 708 | case 0x0252a00a: |
| 709 | return 230400; |
| 710 | |
| 711 | case 0x05f75004: |
| 712 | return 921600; |
| 713 | |
| 714 | case 0x00005004: |
| 715 | return 1000000; |
| 716 | |
| 717 | case 0x04928002: |
| 718 | case 0x01128002: |
| 719 | return 1500000; |
| 720 | |
| 721 | case 0x00005002: |
| 722 | return 2000000; |
| 723 | |
| 724 | case 0x0000b001: |
| 725 | return 2500000; |
| 726 | |
| 727 | case 0x04928001: |
| 728 | return 3000000; |
| 729 | |
| 730 | case 0x052a6001: |
| 731 | return 3500000; |
| 732 | |
| 733 | case 0x00005001: |
| 734 | return 4000000; |
| 735 | |
| 736 | case 0x0252c014: |
| 737 | default: |
| 738 | return 115200; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | int btrtl_get_uart_settings(struct hci_dev *hdev, |
| 743 | struct btrtl_device_info *btrtl_dev, |
| 744 | unsigned int *controller_baudrate, |
| 745 | u32 *device_baudrate, bool *flow_control) |
| 746 | { |
| 747 | struct rtl_vendor_config *config; |
| 748 | struct rtl_vendor_config_entry *entry; |
| 749 | int i, total_data_len; |
| 750 | bool found = false; |
| 751 | |
| 752 | total_data_len = btrtl_dev->cfg_len - sizeof(*config); |
| 753 | if (total_data_len <= 0) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 754 | rtl_dev_warn(hdev, "no config loaded"); |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 755 | return -EINVAL; |
| 756 | } |
| 757 | |
| 758 | config = (struct rtl_vendor_config *)btrtl_dev->cfg_data; |
| 759 | if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 760 | rtl_dev_err(hdev, "invalid config magic"); |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 761 | return -EINVAL; |
| 762 | } |
| 763 | |
| 764 | if (total_data_len < le16_to_cpu(config->total_len)) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 765 | rtl_dev_err(hdev, "config is too short"); |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 766 | return -EINVAL; |
| 767 | } |
| 768 | |
| 769 | for (i = 0; i < total_data_len; ) { |
| 770 | entry = ((void *)config->entry) + i; |
| 771 | |
| 772 | switch (le16_to_cpu(entry->offset)) { |
| 773 | case 0xc: |
| 774 | if (entry->len < sizeof(*device_baudrate)) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 775 | rtl_dev_err(hdev, "invalid UART config entry"); |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 776 | return -EINVAL; |
| 777 | } |
| 778 | |
| 779 | *device_baudrate = get_unaligned_le32(entry->data); |
| 780 | *controller_baudrate = btrtl_convert_baudrate( |
| 781 | *device_baudrate); |
| 782 | |
| 783 | if (entry->len >= 13) |
| 784 | *flow_control = !!(entry->data[12] & BIT(2)); |
| 785 | else |
| 786 | *flow_control = false; |
| 787 | |
| 788 | found = true; |
| 789 | break; |
| 790 | |
| 791 | default: |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 792 | rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)", |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 793 | le16_to_cpu(entry->offset), entry->len); |
| 794 | break; |
YueHaibing | 515d679 | 2019-10-25 17:26:53 +0800 | [diff] [blame] | 795 | } |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 796 | |
| 797 | i += sizeof(*entry) + entry->len; |
| 798 | } |
| 799 | |
| 800 | if (!found) { |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 801 | rtl_dev_err(hdev, "no UART config entry found"); |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 802 | return -ENOENT; |
| 803 | } |
| 804 | |
Alex Lu | f1300c0 | 2019-08-31 16:46:11 +0800 | [diff] [blame] | 805 | rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate); |
| 806 | rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate); |
| 807 | rtl_dev_dbg(hdev, "flow control %d", *flow_control); |
Martin Blumenstingl | b85b0ee | 2018-08-02 16:57:15 +0200 | [diff] [blame] | 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | EXPORT_SYMBOL_GPL(btrtl_get_uart_settings); |
| 812 | |
Carlo Caione | db33c77 | 2015-05-14 10:49:09 +0200 | [diff] [blame] | 813 | MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>"); |
| 814 | MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION); |
| 815 | MODULE_VERSION(VERSION); |
| 816 | MODULE_LICENSE("GPL"); |
Martin Blumenstingl | f96dbd3 | 2018-08-02 16:57:12 +0200 | [diff] [blame] | 817 | MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin"); |
| 818 | MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin"); |
| 819 | MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin"); |
Martin Blumenstingl | c50903e | 2018-08-02 16:57:16 +0200 | [diff] [blame] | 820 | MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin"); |
| 821 | MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin"); |
| 822 | MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin"); |
| 823 | MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin"); |
Martin Blumenstingl | f96dbd3 | 2018-08-02 16:57:12 +0200 | [diff] [blame] | 824 | MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin"); |
| 825 | MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin"); |
| 826 | MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin"); |
| 827 | MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin"); |
| 828 | MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin"); |
| 829 | MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin"); |