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