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