blob: c2bdd1e6060e83f4ee5542d3c2a7a088833be3ba [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Carlo Caionedb33c772015-05-14 10:49:09 +02002/*
3 * Bluetooth support for Realtek devices
4 *
5 * Copyright (C) 2015 Endless Mobile, Inc.
Carlo Caionedb33c772015-05-14 10:49:09 +02006 */
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 Caionedb33c772015-05-14 10:49:09 +020021#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 Finger1110a2d2016-09-09 10:02:05 -050025#define RTL_ROM_LMP_8822B 0x8822
Max Chou0d484db2020-11-10 11:38:37 +080026#define RTL_ROM_LMP_8852A 0x8852
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +020027#define RTL_CONFIG_MAGIC 0x8723ab55
Carlo Caionedb33c772015-05-14 10:49:09 +020028
Alex Lu907f8492018-02-11 12:24:33 -060029#define IC_MATCH_FL_LMPSUBV (1 << 0)
30#define IC_MATCH_FL_HCIREV (1 << 1)
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020031#define IC_MATCH_FL_HCIVER (1 << 2)
32#define IC_MATCH_FL_HCIBUS (1 << 3)
Max Chou6f9ff242020-11-04 20:04:14 +080033#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 Lu907f8492018-02-11 12:24:33 -060036 .lmp_subver = (lmps), \
Max Chou6f9ff242020-11-04 20:04:14 +080037 .hci_rev = (hcir), \
38 .hci_ver = (hciv), \
39 .hci_bus = (bus)
Alex Lu907f8492018-02-11 12:24:33 -060040
Max Chou9ab92352021-01-27 11:01:52 +080041enum btrtl_chip_id {
42 CHIP_ID_8723A,
43 CHIP_ID_8723B,
44 CHIP_ID_8821A,
45 CHIP_ID_8761A,
46 CHIP_ID_8822B = 8,
47 CHIP_ID_8723D,
48 CHIP_ID_8821C,
49 CHIP_ID_8822C = 13,
50 CHIP_ID_8761B,
51 CHIP_ID_8852A = 18,
52};
53
Alex Lu907f8492018-02-11 12:24:33 -060054struct id_table {
55 __u16 match_flags;
56 __u16 lmp_subver;
57 __u16 hci_rev;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020058 __u8 hci_ver;
59 __u8 hci_bus;
Alex Lu907f8492018-02-11 12:24:33 -060060 bool config_needed;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020061 bool has_rom_version;
Marcel Holtmannf4bcba02021-09-28 20:10:50 +020062 bool has_msft_ext;
Alex Lu907f8492018-02-11 12:24:33 -060063 char *fw_name;
64 char *cfg_name;
65};
66
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020067struct btrtl_device_info {
68 const struct id_table *ic_info;
69 u8 rom_version;
70 u8 *fw_data;
71 int fw_len;
72 u8 *cfg_data;
73 int cfg_len;
Kai-Heng Feng1996d9c2020-10-26 16:28:38 +080074 bool drop_fw;
Max Chou9ab92352021-01-27 11:01:52 +080075 int project_id;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020076};
77
Alex Lu907f8492018-02-11 12:24:33 -060078static const struct id_table ic_id_table[] = {
Max Chou6f9ff242020-11-04 20:04:14 +080079 /* 8723A */
80 { IC_INFO(RTL_ROM_LMP_8723A, 0xb, 0x6, HCI_USB),
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020081 .config_needed = false,
82 .has_rom_version = false,
83 .fw_name = "rtl_bt/rtl8723a_fw.bin",
84 .cfg_name = NULL },
85
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020086 /* 8723BS */
Max Chou6f9ff242020-11-04 20:04:14 +080087 { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_UART),
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020088 .config_needed = true,
89 .has_rom_version = true,
90 .fw_name = "rtl_bt/rtl8723bs_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020091 .cfg_name = "rtl_bt/rtl8723bs_config" },
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020092
Alex Lu907f8492018-02-11 12:24:33 -060093 /* 8723B */
Max Chou6f9ff242020-11-04 20:04:14 +080094 { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_USB),
Alex Lu907f8492018-02-11 12:24:33 -060095 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020096 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060097 .fw_name = "rtl_bt/rtl8723b_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020098 .cfg_name = "rtl_bt/rtl8723b_config" },
Alex Lu907f8492018-02-11 12:24:33 -060099
100 /* 8723D */
Max Chou6f9ff242020-11-04 20:04:14 +0800101 { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_USB),
Alex Lu907f8492018-02-11 12:24:33 -0600102 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200103 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600104 .fw_name = "rtl_bt/rtl8723d_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200105 .cfg_name = "rtl_bt/rtl8723d_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600106
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200107 /* 8723DS */
Max Chou6f9ff242020-11-04 20:04:14 +0800108 { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_UART),
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200109 .config_needed = true,
110 .has_rom_version = true,
111 .fw_name = "rtl_bt/rtl8723ds_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200112 .cfg_name = "rtl_bt/rtl8723ds_config" },
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200113
Alex Lu907f8492018-02-11 12:24:33 -0600114 /* 8821A */
Max Chou6f9ff242020-11-04 20:04:14 +0800115 { IC_INFO(RTL_ROM_LMP_8821A, 0xa, 0x6, HCI_USB),
Alex Lu907f8492018-02-11 12:24:33 -0600116 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200117 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600118 .fw_name = "rtl_bt/rtl8821a_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200119 .cfg_name = "rtl_bt/rtl8821a_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600120
121 /* 8821C */
Max Chou6f9ff242020-11-04 20:04:14 +0800122 { IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_USB),
Alex Lu907f8492018-02-11 12:24:33 -0600123 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200124 .has_rom_version = true,
Marcel Holtmann4d51fb02021-09-29 19:48:19 +0200125 .has_msft_ext = true,
Alex Lu907f8492018-02-11 12:24:33 -0600126 .fw_name = "rtl_bt/rtl8821c_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200127 .cfg_name = "rtl_bt/rtl8821c_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600128
129 /* 8761A */
Max Chou6f9ff242020-11-04 20:04:14 +0800130 { IC_INFO(RTL_ROM_LMP_8761A, 0xa, 0x6, HCI_USB),
Alex Lu907f8492018-02-11 12:24:33 -0600131 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200132 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600133 .fw_name = "rtl_bt/rtl8761a_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200134 .cfg_name = "rtl_bt/rtl8761a_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600135
Ziqian SUN (Zamir)04896832020-04-11 09:34:27 +0800136 /* 8761B */
Joakim Tjernlund9fd2e292021-05-28 17:26:44 +0200137 { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_UART),
Ziqian SUN (Zamir)04896832020-04-11 09:34:27 +0800138 .config_needed = false,
139 .has_rom_version = true,
Marcel Holtmannf4bcba02021-09-28 20:10:50 +0200140 .has_msft_ext = true,
Ziqian SUN (Zamir)04896832020-04-11 09:34:27 +0800141 .fw_name = "rtl_bt/rtl8761b_fw.bin",
142 .cfg_name = "rtl_bt/rtl8761b_config" },
143
Joakim Tjernlund9fd2e292021-05-28 17:26:44 +0200144 /* 8761BU */
145 { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_USB),
146 .config_needed = false,
147 .has_rom_version = true,
148 .fw_name = "rtl_bt/rtl8761bu_fw.bin",
149 .cfg_name = "rtl_bt/rtl8761bu_config" },
150
Max Chou848fc612020-02-17 16:14:55 +0800151 /* 8822C with UART interface */
Max Chou6f9ff242020-11-04 20:04:14 +0800152 { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_UART),
Max Chou848fc612020-02-17 16:14:55 +0800153 .config_needed = true,
154 .has_rom_version = true,
Marcel Holtmannf4bcba02021-09-28 20:10:50 +0200155 .has_msft_ext = true,
Max Chou848fc612020-02-17 16:14:55 +0800156 .fw_name = "rtl_bt/rtl8822cs_fw.bin",
157 .cfg_name = "rtl_bt/rtl8822cs_config" },
158
Alex Lu8ecfdc92018-09-08 12:05:59 -0500159 /* 8822C with USB interface */
Max Chou6f9ff242020-11-04 20:04:14 +0800160 { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_USB),
Alex Lu8ecfdc92018-09-08 12:05:59 -0500161 .config_needed = false,
162 .has_rom_version = true,
Marcel Holtmannf4bcba02021-09-28 20:10:50 +0200163 .has_msft_ext = true,
Alex Lu8ecfdc92018-09-08 12:05:59 -0500164 .fw_name = "rtl_bt/rtl8822cu_fw.bin",
165 .cfg_name = "rtl_bt/rtl8822cu_config" },
166
Alex Lu907f8492018-02-11 12:24:33 -0600167 /* 8822B */
Max Chou6f9ff242020-11-04 20:04:14 +0800168 { IC_INFO(RTL_ROM_LMP_8822B, 0xb, 0x7, HCI_USB),
Alex Lu907f8492018-02-11 12:24:33 -0600169 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200170 .has_rom_version = true,
Marcel Holtmannf4bcba02021-09-28 20:10:50 +0200171 .has_msft_ext = true,
Alex Lu907f8492018-02-11 12:24:33 -0600172 .fw_name = "rtl_bt/rtl8822b_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200173 .cfg_name = "rtl_bt/rtl8822b_config" },
Max Chou0d484db2020-11-10 11:38:37 +0800174
175 /* 8852A */
176 { IC_INFO(RTL_ROM_LMP_8852A, 0xa, 0xb, HCI_USB),
177 .config_needed = false,
178 .has_rom_version = true,
Marcel Holtmannf4bcba02021-09-28 20:10:50 +0200179 .has_msft_ext = true,
Max Chou0d484db2020-11-10 11:38:37 +0800180 .fw_name = "rtl_bt/rtl8852au_fw.bin",
181 .cfg_name = "rtl_bt/rtl8852au_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600182 };
183
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200184static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
185 u8 hci_ver, u8 hci_bus)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200186{
187 int i;
188
189 for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
190 if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
191 (ic_id_table[i].lmp_subver != lmp_subver))
192 continue;
193 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
194 (ic_id_table[i].hci_rev != hci_rev))
195 continue;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200196 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
197 (ic_id_table[i].hci_ver != hci_ver))
198 continue;
199 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
200 (ic_id_table[i].hci_bus != hci_bus))
201 continue;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200202
203 break;
204 }
205 if (i >= ARRAY_SIZE(ic_id_table))
206 return NULL;
207
208 return &ic_id_table[i];
209}
210
Alex Lu240b64a2019-08-31 16:36:02 +0800211static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
212{
213 struct sk_buff *skb;
214
215 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
216 HCI_INIT_TIMEOUT);
217 if (IS_ERR(skb)) {
218 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)",
219 PTR_ERR(skb));
220 return skb;
221 }
222
223 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
224 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch");
225 kfree_skb(skb);
226 return ERR_PTR(-EIO);
227 }
228
229 return skb;
230}
231
Carlo Caionedb33c772015-05-14 10:49:09 +0200232static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
233{
234 struct rtl_rom_version_evt *rom_version;
235 struct sk_buff *skb;
236
237 /* Read RTL ROM version command */
238 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
239 if (IS_ERR(skb)) {
Alex Luf1300c02019-08-31 16:46:11 +0800240 rtl_dev_err(hdev, "Read ROM version failed (%ld)",
Hans de Goedea5c76e62018-08-02 16:57:14 +0200241 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200242 return PTR_ERR(skb);
243 }
244
245 if (skb->len != sizeof(*rom_version)) {
Alex Luf1300c02019-08-31 16:46:11 +0800246 rtl_dev_err(hdev, "version event length mismatch");
Carlo Caionedb33c772015-05-14 10:49:09 +0200247 kfree_skb(skb);
248 return -EIO;
249 }
250
251 rom_version = (struct rtl_rom_version_evt *)skb->data;
Alex Luf1300c02019-08-31 16:46:11 +0800252 rtl_dev_info(hdev, "rom_version status=%x version=%x",
Hans de Goedea5c76e62018-08-02 16:57:14 +0200253 rom_version->status, rom_version->version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200254
255 *version = rom_version->version;
256
257 kfree_skb(skb);
258 return 0;
259}
260
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200261static int rtlbt_parse_firmware(struct hci_dev *hdev,
262 struct btrtl_device_info *btrtl_dev,
Alex Lu907f8492018-02-11 12:24:33 -0600263 unsigned char **_buf)
Carlo Caionedb33c772015-05-14 10:49:09 +0200264{
Colin Ian Kinge5070e072018-09-26 09:32:08 +0100265 static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
Carlo Caionedb33c772015-05-14 10:49:09 +0200266 struct rtl_epatch_header *epatch_info;
267 unsigned char *buf;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200268 int i, len;
Carlo Caionedb33c772015-05-14 10:49:09 +0200269 size_t min_size;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200270 u8 opcode, length, data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200271 int project_id = -1;
272 const unsigned char *fwptr, *chip_id_base;
273 const unsigned char *patch_length_base, *patch_offset_base;
274 u32 patch_offset = 0;
275 u16 patch_length, num_patches;
Larry Finger1110a2d2016-09-09 10:02:05 -0500276 static const struct {
277 __u16 lmp_subver;
278 __u8 id;
279 } project_id_to_lmp_subver[] = {
280 { RTL_ROM_LMP_8723A, 0 },
281 { RTL_ROM_LMP_8723B, 1 },
282 { RTL_ROM_LMP_8821A, 2 },
283 { RTL_ROM_LMP_8761A, 3 },
284 { RTL_ROM_LMP_8822B, 8 },
Alex Lu907f8492018-02-11 12:24:33 -0600285 { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
286 { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
Alex Lu8ecfdc92018-09-08 12:05:59 -0500287 { RTL_ROM_LMP_8822B, 13 }, /* 8822C */
Ziqian SUN (Zamir)04896832020-04-11 09:34:27 +0800288 { RTL_ROM_LMP_8761A, 14 }, /* 8761B */
Max Chou0d484db2020-11-10 11:38:37 +0800289 { RTL_ROM_LMP_8852A, 18 }, /* 8852A */
Carlo Caionedb33c772015-05-14 10:49:09 +0200290 };
291
Carlo Caionedb33c772015-05-14 10:49:09 +0200292 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200293 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200294 return -EINVAL;
295
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200296 fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
Carlo Caionedb33c772015-05-14 10:49:09 +0200297 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
Alex Luf1300c02019-08-31 16:46:11 +0800298 rtl_dev_err(hdev, "extension section signature mismatch");
Carlo Caionedb33c772015-05-14 10:49:09 +0200299 return -EINVAL;
300 }
301
302 /* Loop from the end of the firmware parsing instructions, until
303 * we find an instruction that identifies the "project ID" for the
304 * hardware supported by this firwmare file.
305 * Once we have that, we double-check that that project_id is suitable
306 * for the hardware we are working with.
307 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200308 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200309 opcode = *--fwptr;
310 length = *--fwptr;
311 data = *--fwptr;
312
313 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
314
315 if (opcode == 0xff) /* EOF */
316 break;
317
318 if (length == 0) {
Alex Luf1300c02019-08-31 16:46:11 +0800319 rtl_dev_err(hdev, "found instruction with length 0");
Carlo Caionedb33c772015-05-14 10:49:09 +0200320 return -EINVAL;
321 }
322
323 if (opcode == 0 && length == 1) {
324 project_id = data;
325 break;
326 }
327
328 fwptr -= length;
329 }
330
331 if (project_id < 0) {
Alex Luf1300c02019-08-31 16:46:11 +0800332 rtl_dev_err(hdev, "failed to find version instruction");
Carlo Caionedb33c772015-05-14 10:49:09 +0200333 return -EINVAL;
334 }
335
Larry Finger1110a2d2016-09-09 10:02:05 -0500336 /* Find project_id in table */
337 for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
Max Chou9ab92352021-01-27 11:01:52 +0800338 if (project_id == project_id_to_lmp_subver[i].id) {
339 btrtl_dev->project_id = project_id;
Larry Finger1110a2d2016-09-09 10:02:05 -0500340 break;
Max Chou9ab92352021-01-27 11:01:52 +0800341 }
Larry Finger1110a2d2016-09-09 10:02:05 -0500342 }
343
344 if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Alex Luf1300c02019-08-31 16:46:11 +0800345 rtl_dev_err(hdev, "unknown project id %d", project_id);
Carlo Caionedb33c772015-05-14 10:49:09 +0200346 return -EINVAL;
347 }
348
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200349 if (btrtl_dev->ic_info->lmp_subver !=
350 project_id_to_lmp_subver[i].lmp_subver) {
Alex Luf1300c02019-08-31 16:46:11 +0800351 rtl_dev_err(hdev, "firmware is for %x but this is a %x",
Hans de Goedea5c76e62018-08-02 16:57:14 +0200352 project_id_to_lmp_subver[i].lmp_subver,
353 btrtl_dev->ic_info->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200354 return -EINVAL;
355 }
356
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200357 epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200358 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
Alex Luf1300c02019-08-31 16:46:11 +0800359 rtl_dev_err(hdev, "bad EPATCH signature");
Carlo Caionedb33c772015-05-14 10:49:09 +0200360 return -EINVAL;
361 }
362
363 num_patches = le16_to_cpu(epatch_info->num_patches);
364 BT_DBG("fw_version=%x, num_patches=%d",
365 le32_to_cpu(epatch_info->fw_version), num_patches);
366
367 /* After the rtl_epatch_header there is a funky patch metadata section.
368 * Assuming 2 patches, the layout is:
369 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
370 *
371 * Find the right patch for this chip.
372 */
373 min_size += 8 * num_patches;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200374 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200375 return -EINVAL;
376
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200377 chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
Carlo Caionedb33c772015-05-14 10:49:09 +0200378 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
379 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
380 for (i = 0; i < num_patches; i++) {
381 u16 chip_id = get_unaligned_le16(chip_id_base +
382 (i * sizeof(u16)));
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200383 if (chip_id == btrtl_dev->rom_version + 1) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200384 patch_length = get_unaligned_le16(patch_length_base +
385 (i * sizeof(u16)));
386 patch_offset = get_unaligned_le32(patch_offset_base +
387 (i * sizeof(u32)));
388 break;
389 }
390 }
391
392 if (!patch_offset) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200393 rtl_dev_err(hdev, "didn't find patch for chip id %d",
394 btrtl_dev->rom_version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200395 return -EINVAL;
396 }
397
398 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
399 min_size = patch_offset + patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200400 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200401 return -EINVAL;
402
403 /* Copy the firmware into a new buffer and write the version at
404 * the end.
405 */
406 len = patch_length;
Maxim Mikityanskiy268d3632020-01-24 19:15:35 +0200407 buf = kvmalloc(patch_length, GFP_KERNEL);
Carlo Caionedb33c772015-05-14 10:49:09 +0200408 if (!buf)
409 return -ENOMEM;
410
Maxim Mikityanskiy268d3632020-01-24 19:15:35 +0200411 memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
Carlo Caionedb33c772015-05-14 10:49:09 +0200412 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
413
414 *_buf = buf;
415 return len;
416}
417
418static int rtl_download_firmware(struct hci_dev *hdev,
419 const unsigned char *data, int fw_len)
420{
421 struct rtl_download_cmd *dl_cmd;
422 int frag_num = fw_len / RTL_FRAG_LEN + 1;
423 int frag_len = RTL_FRAG_LEN;
424 int ret = 0;
425 int i;
Alex Lu240b64a2019-08-31 16:36:02 +0800426 struct sk_buff *skb;
427 struct hci_rp_read_local_version *rp;
Carlo Caionedb33c772015-05-14 10:49:09 +0200428
429 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
430 if (!dl_cmd)
431 return -ENOMEM;
432
433 for (i = 0; i < frag_num; i++) {
434 struct sk_buff *skb;
435
436 BT_DBG("download fw (%d/%d)", i, frag_num);
437
Max Choucf0d9a72019-09-05 13:21:57 +0800438 if (i > 0x7f)
439 dl_cmd->index = (i & 0x7f) + 1;
440 else
441 dl_cmd->index = i;
442
Carlo Caionedb33c772015-05-14 10:49:09 +0200443 if (i == (frag_num - 1)) {
444 dl_cmd->index |= 0x80; /* data end */
445 frag_len = fw_len % RTL_FRAG_LEN;
446 }
447 memcpy(dl_cmd->data, data, frag_len);
448
449 /* Send download command */
450 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
451 HCI_INIT_TIMEOUT);
452 if (IS_ERR(skb)) {
Alex Luf1300c02019-08-31 16:46:11 +0800453 rtl_dev_err(hdev, "download fw command failed (%ld)",
Hans de Goedea5c76e62018-08-02 16:57:14 +0200454 PTR_ERR(skb));
Max Choud171dfb2019-09-18 16:56:41 +0800455 ret = PTR_ERR(skb);
Carlo Caionedb33c772015-05-14 10:49:09 +0200456 goto out;
457 }
458
459 if (skb->len != sizeof(struct rtl_download_response)) {
Alex Luf1300c02019-08-31 16:46:11 +0800460 rtl_dev_err(hdev, "download fw event length mismatch");
Carlo Caionedb33c772015-05-14 10:49:09 +0200461 kfree_skb(skb);
462 ret = -EIO;
463 goto out;
464 }
465
466 kfree_skb(skb);
467 data += RTL_FRAG_LEN;
468 }
469
Alex Lu240b64a2019-08-31 16:36:02 +0800470 skb = btrtl_read_local_version(hdev);
471 if (IS_ERR(skb)) {
472 ret = PTR_ERR(skb);
473 rtl_dev_err(hdev, "read local version failed");
474 goto out;
475 }
476
477 rp = (struct hci_rp_read_local_version *)skb->data;
478 rtl_dev_info(hdev, "fw version 0x%04x%04x",
479 __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver));
480 kfree_skb(skb);
481
Carlo Caionedb33c772015-05-14 10:49:09 +0200482out:
483 kfree(dl_cmd);
484 return ret;
485}
486
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200487static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
Larry Finger1110a2d2016-09-09 10:02:05 -0500488{
489 const struct firmware *fw;
490 int ret;
491
Alex Luf1300c02019-08-31 16:46:11 +0800492 rtl_dev_info(hdev, "loading %s", name);
Larry Finger1110a2d2016-09-09 10:02:05 -0500493 ret = request_firmware(&fw, name, &hdev->dev);
Larry Fingerabed84a2017-02-19 20:04:57 -0600494 if (ret < 0)
Larry Finger1110a2d2016-09-09 10:02:05 -0500495 return ret;
Larry Finger1110a2d2016-09-09 10:02:05 -0500496 ret = fw->size;
Maxim Mikityanskiy268d3632020-01-24 19:15:35 +0200497 *buff = kvmalloc(fw->size, GFP_KERNEL);
498 if (*buff)
499 memcpy(*buff, fw->data, ret);
500 else
Dan Carpenterc3327bd2017-07-28 17:41:11 +0300501 ret = -ENOMEM;
Larry Finger1110a2d2016-09-09 10:02:05 -0500502
503 release_firmware(fw);
504
505 return ret;
506}
507
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200508static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
509 struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200510{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200511 if (btrtl_dev->fw_len < 8)
512 return -EINVAL;
Carlo Caionedb33c772015-05-14 10:49:09 +0200513
514 /* Check that the firmware doesn't have the epatch signature
515 * (which is only for RTL8723B and newer).
516 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200517 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
Alex Luf1300c02019-08-31 16:46:11 +0800518 rtl_dev_err(hdev, "unexpected EPATCH signature!");
Alex Lu907f8492018-02-11 12:24:33 -0600519 return -EINVAL;
520 }
521
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200522 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
523 btrtl_dev->fw_len);
524}
Alex Lu907f8492018-02-11 12:24:33 -0600525
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200526static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
527 struct btrtl_device_info *btrtl_dev)
528{
529 unsigned char *fw_data = NULL;
530 int ret;
531 u8 *tbuff;
Carlo Caionedb33c772015-05-14 10:49:09 +0200532
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200533 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200534 if (ret < 0)
535 goto out;
536
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200537 if (btrtl_dev->cfg_len > 0) {
Maxim Mikityanskiy268d3632020-01-24 19:15:35 +0200538 tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
Larry Finger1110a2d2016-09-09 10:02:05 -0500539 if (!tbuff) {
540 ret = -ENOMEM;
541 goto out;
542 }
543
544 memcpy(tbuff, fw_data, ret);
Maxim Mikityanskiy268d3632020-01-24 19:15:35 +0200545 kvfree(fw_data);
Larry Finger1110a2d2016-09-09 10:02:05 -0500546
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200547 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
548 ret += btrtl_dev->cfg_len;
Larry Finger1110a2d2016-09-09 10:02:05 -0500549
550 fw_data = tbuff;
551 }
552
Alex Luf1300c02019-08-31 16:46:11 +0800553 rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret);
Larry Finger1110a2d2016-09-09 10:02:05 -0500554
Carlo Caionedb33c772015-05-14 10:49:09 +0200555 ret = rtl_download_firmware(hdev, fw_data, ret);
Carlo Caionedb33c772015-05-14 10:49:09 +0200556
557out:
Maxim Mikityanskiy268d3632020-01-24 19:15:35 +0200558 kvfree(fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200559 return ret;
560}
561
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200562void btrtl_free(struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200563{
Maxim Mikityanskiy268d3632020-01-24 19:15:35 +0200564 kvfree(btrtl_dev->fw_data);
565 kvfree(btrtl_dev->cfg_data);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200566 kfree(btrtl_dev);
567}
568EXPORT_SYMBOL_GPL(btrtl_free);
569
Hans de Goede1cc194c2018-08-02 16:57:17 +0200570struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
571 const char *postfix)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200572{
573 struct btrtl_device_info *btrtl_dev;
Carlo Caionedb33c772015-05-14 10:49:09 +0200574 struct sk_buff *skb;
575 struct hci_rp_read_local_version *resp;
Hans de Goede1cc194c2018-08-02 16:57:17 +0200576 char cfg_name[40];
Alex Lu907f8492018-02-11 12:24:33 -0600577 u16 hci_rev, lmp_subver;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200578 u8 hci_ver;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200579 int ret;
Kai-Heng Feng1996d9c2020-10-26 16:28:38 +0800580 u16 opcode;
581 u8 cmd[2];
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200582
583 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
584 if (!btrtl_dev) {
585 ret = -ENOMEM;
586 goto err_alloc;
587 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200588
589 skb = btrtl_read_local_version(hdev);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200590 if (IS_ERR(skb)) {
591 ret = PTR_ERR(skb);
592 goto err_free;
593 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200594
595 resp = (struct hci_rp_read_local_version *)skb->data;
Alex Luf1300c02019-08-31 16:46:11 +0800596 rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
Hans de Goedea5c76e62018-08-02 16:57:14 +0200597 resp->hci_ver, resp->hci_rev,
598 resp->lmp_ver, resp->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200599
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200600 hci_ver = resp->hci_ver;
Alex Lu907f8492018-02-11 12:24:33 -0600601 hci_rev = le16_to_cpu(resp->hci_rev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200602 lmp_subver = le16_to_cpu(resp->lmp_subver);
Kai-Heng Feng1996d9c2020-10-26 16:28:38 +0800603
Hilda Wucd367422021-09-30 18:36:34 +0800604 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
605 hdev->bus);
606
607 if (!btrtl_dev->ic_info)
Kai-Heng Feng1996d9c2020-10-26 16:28:38 +0800608 btrtl_dev->drop_fw = true;
609
610 if (btrtl_dev->drop_fw) {
611 opcode = hci_opcode_pack(0x3f, 0x66);
612 cmd[0] = opcode & 0xff;
613 cmd[1] = opcode >> 8;
614
615 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
Colin Ian Kingf5e8e212020-11-10 12:39:15 +0000616 if (!skb)
Kai-Heng Feng1996d9c2020-10-26 16:28:38 +0800617 goto out_free;
618
619 skb_put_data(skb, cmd, sizeof(cmd));
620 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
621
622 hdev->send(hdev, skb);
623
624 /* Ensure the above vendor command is sent to controller and
625 * process has done.
626 */
627 msleep(200);
628
629 /* Read the local version again. Expect to have the vanilla
630 * version as cold boot.
631 */
632 skb = btrtl_read_local_version(hdev);
633 if (IS_ERR(skb)) {
634 ret = PTR_ERR(skb);
635 goto err_free;
636 }
637
638 resp = (struct hci_rp_read_local_version *)skb->data;
639 rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
640 resp->hci_ver, resp->hci_rev,
641 resp->lmp_ver, resp->lmp_subver);
642
643 hci_ver = resp->hci_ver;
644 hci_rev = le16_to_cpu(resp->hci_rev);
645 lmp_subver = le16_to_cpu(resp->lmp_subver);
Hilda Wucd367422021-09-30 18:36:34 +0800646
647 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
648 hdev->bus);
Kai-Heng Feng1996d9c2020-10-26 16:28:38 +0800649 }
650out_free:
Carlo Caionedb33c772015-05-14 10:49:09 +0200651 kfree_skb(skb);
652
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200653 if (!btrtl_dev->ic_info) {
Alex Lud1822152019-08-31 16:41:13 +0800654 rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200655 lmp_subver, hci_rev, hci_ver);
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800656 return btrtl_dev;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200657 }
658
659 if (btrtl_dev->ic_info->has_rom_version) {
660 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
661 if (ret)
662 goto err_free;
663 }
664
665 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
666 &btrtl_dev->fw_data);
667 if (btrtl_dev->fw_len < 0) {
Alex Luf1300c02019-08-31 16:46:11 +0800668 rtl_dev_err(hdev, "firmware file %s not found",
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200669 btrtl_dev->ic_info->fw_name);
670 ret = btrtl_dev->fw_len;
671 goto err_free;
672 }
673
674 if (btrtl_dev->ic_info->cfg_name) {
Hans de Goede1cc194c2018-08-02 16:57:17 +0200675 if (postfix) {
676 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
677 btrtl_dev->ic_info->cfg_name, postfix);
678 } else {
679 snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
680 btrtl_dev->ic_info->cfg_name);
681 }
682 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200683 &btrtl_dev->cfg_data);
684 if (btrtl_dev->ic_info->config_needed &&
685 btrtl_dev->cfg_len <= 0) {
Alex Luf1300c02019-08-31 16:46:11 +0800686 rtl_dev_err(hdev, "mandatory config file %s not found",
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200687 btrtl_dev->ic_info->cfg_name);
688 ret = btrtl_dev->cfg_len;
689 goto err_free;
690 }
691 }
692
Archie Pusaka7f6a7502021-07-13 16:37:03 +0800693 /* The following chips supports the Microsoft vendor extension,
694 * therefore set the corresponding VsMsftOpCode.
Miao-chen Chou673fae12020-12-17 14:53:21 -0800695 */
Marcel Holtmannf4bcba02021-09-28 20:10:50 +0200696 if (btrtl_dev->ic_info->has_msft_ext)
Miao-chen Chou673fae12020-12-17 14:53:21 -0800697 hci_set_msft_opcode(hdev, 0xFCF0);
698
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200699 return btrtl_dev;
700
701err_free:
702 btrtl_free(btrtl_dev);
703err_alloc:
704 return ERR_PTR(ret);
705}
706EXPORT_SYMBOL_GPL(btrtl_initialize);
707
708int btrtl_download_firmware(struct hci_dev *hdev,
709 struct btrtl_device_info *btrtl_dev)
710{
Carlo Caionedb33c772015-05-14 10:49:09 +0200711 /* Match a set of subver values that correspond to stock firmware,
712 * which is not compatible with standard btusb.
713 * If matched, upload an alternative firmware that does conform to
714 * standard btusb. Once that firmware is uploaded, the subver changes
715 * to a different value.
716 */
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800717 if (!btrtl_dev->ic_info) {
Alex Luf1300c02019-08-31 16:46:11 +0800718 rtl_dev_info(hdev, "assuming no firmware upload needed");
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800719 return 0;
720 }
721
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200722 switch (btrtl_dev->ic_info->lmp_subver) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200723 case RTL_ROM_LMP_8723A:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200724 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200725 case RTL_ROM_LMP_8723B:
Carlo Caionedb33c772015-05-14 10:49:09 +0200726 case RTL_ROM_LMP_8821A:
Carlo Caionedb33c772015-05-14 10:49:09 +0200727 case RTL_ROM_LMP_8761A:
Larry Finger1110a2d2016-09-09 10:02:05 -0500728 case RTL_ROM_LMP_8822B:
Max Chou0d484db2020-11-10 11:38:37 +0800729 case RTL_ROM_LMP_8852A:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200730 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200731 default:
Alex Luf1300c02019-08-31 16:46:11 +0800732 rtl_dev_info(hdev, "assuming no firmware upload needed");
Carlo Caionedb33c772015-05-14 10:49:09 +0200733 return 0;
734 }
735}
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200736EXPORT_SYMBOL_GPL(btrtl_download_firmware);
737
Archie Pusaka3011faa2021-05-27 15:19:54 +0800738void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200739{
Alex Lu65251e22019-08-30 20:02:14 +0800740 /* Enable controller to do both LE scan and BR/EDR inquiry
741 * simultaneously.
742 */
743 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
744
Abhishek Pandit-Subedi05672a22020-12-22 10:16:27 -0800745 /* Enable central-peripheral role (able to create new connections with
746 * an existing connection in slave role).
747 */
Max Chou9ab92352021-01-27 11:01:52 +0800748 /* Enable WBS supported for the specific Realtek devices. */
749 switch (btrtl_dev->project_id) {
750 case CHIP_ID_8822C:
751 case CHIP_ID_8852A:
Abhishek Pandit-Subedi05672a22020-12-22 10:16:27 -0800752 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
Max Chou9ab92352021-01-27 11:01:52 +0800753 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
Joseph Hwang099c6d32021-09-26 15:07:48 +0800754 hci_set_aosp_capable(hdev);
Abhishek Pandit-Subedi05672a22020-12-22 10:16:27 -0800755 break;
756 default:
757 rtl_dev_dbg(hdev, "Central-peripheral role not enabled.");
Max Chou9ab92352021-01-27 11:01:52 +0800758 rtl_dev_dbg(hdev, "WBS supported not enabled.");
Abhishek Pandit-Subedi05672a22020-12-22 10:16:27 -0800759 break;
760 }
Archie Pusaka3011faa2021-05-27 15:19:54 +0800761}
762EXPORT_SYMBOL_GPL(btrtl_set_quirks);
763
764int btrtl_setup_realtek(struct hci_dev *hdev)
765{
766 struct btrtl_device_info *btrtl_dev;
767 int ret;
768
769 btrtl_dev = btrtl_initialize(hdev, NULL);
770 if (IS_ERR(btrtl_dev))
771 return PTR_ERR(btrtl_dev);
772
773 ret = btrtl_download_firmware(hdev, btrtl_dev);
774
775 btrtl_set_quirks(hdev, btrtl_dev);
Abhishek Pandit-Subedi05672a22020-12-22 10:16:27 -0800776
777 btrtl_free(btrtl_dev);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200778 return ret;
779}
Carlo Caionedb33c772015-05-14 10:49:09 +0200780EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
781
Jian-Hong Pan7af3f5582019-06-25 16:30:51 +0800782int btrtl_shutdown_realtek(struct hci_dev *hdev)
783{
784 struct sk_buff *skb;
785 int ret;
786
787 /* According to the vendor driver, BT must be reset on close to avoid
788 * firmware crash.
789 */
790 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
791 if (IS_ERR(skb)) {
792 ret = PTR_ERR(skb);
793 bt_dev_err(hdev, "HCI reset during shutdown failed");
794 return ret;
795 }
796 kfree_skb(skb);
797
798 return 0;
799}
800EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
801
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200802static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
803{
804 switch (device_baudrate) {
805 case 0x0252a00a:
806 return 230400;
807
808 case 0x05f75004:
809 return 921600;
810
811 case 0x00005004:
812 return 1000000;
813
814 case 0x04928002:
815 case 0x01128002:
816 return 1500000;
817
818 case 0x00005002:
819 return 2000000;
820
821 case 0x0000b001:
822 return 2500000;
823
824 case 0x04928001:
825 return 3000000;
826
827 case 0x052a6001:
828 return 3500000;
829
830 case 0x00005001:
831 return 4000000;
832
833 case 0x0252c014:
834 default:
835 return 115200;
836 }
837}
838
839int btrtl_get_uart_settings(struct hci_dev *hdev,
840 struct btrtl_device_info *btrtl_dev,
841 unsigned int *controller_baudrate,
842 u32 *device_baudrate, bool *flow_control)
843{
844 struct rtl_vendor_config *config;
845 struct rtl_vendor_config_entry *entry;
846 int i, total_data_len;
847 bool found = false;
848
849 total_data_len = btrtl_dev->cfg_len - sizeof(*config);
850 if (total_data_len <= 0) {
Alex Luf1300c02019-08-31 16:46:11 +0800851 rtl_dev_warn(hdev, "no config loaded");
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200852 return -EINVAL;
853 }
854
855 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
856 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
Alex Luf1300c02019-08-31 16:46:11 +0800857 rtl_dev_err(hdev, "invalid config magic");
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200858 return -EINVAL;
859 }
860
861 if (total_data_len < le16_to_cpu(config->total_len)) {
Alex Luf1300c02019-08-31 16:46:11 +0800862 rtl_dev_err(hdev, "config is too short");
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200863 return -EINVAL;
864 }
865
866 for (i = 0; i < total_data_len; ) {
867 entry = ((void *)config->entry) + i;
868
869 switch (le16_to_cpu(entry->offset)) {
870 case 0xc:
871 if (entry->len < sizeof(*device_baudrate)) {
Alex Luf1300c02019-08-31 16:46:11 +0800872 rtl_dev_err(hdev, "invalid UART config entry");
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200873 return -EINVAL;
874 }
875
876 *device_baudrate = get_unaligned_le32(entry->data);
877 *controller_baudrate = btrtl_convert_baudrate(
878 *device_baudrate);
879
880 if (entry->len >= 13)
881 *flow_control = !!(entry->data[12] & BIT(2));
882 else
883 *flow_control = false;
884
885 found = true;
886 break;
887
888 default:
Alex Luf1300c02019-08-31 16:46:11 +0800889 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)",
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200890 le16_to_cpu(entry->offset), entry->len);
891 break;
YueHaibing515d6792019-10-25 17:26:53 +0800892 }
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200893
894 i += sizeof(*entry) + entry->len;
895 }
896
897 if (!found) {
Alex Luf1300c02019-08-31 16:46:11 +0800898 rtl_dev_err(hdev, "no UART config entry found");
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200899 return -ENOENT;
900 }
901
Alex Luf1300c02019-08-31 16:46:11 +0800902 rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate);
903 rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate);
904 rtl_dev_dbg(hdev, "flow control %d", *flow_control);
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200905
906 return 0;
907}
908EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
909
Carlo Caionedb33c772015-05-14 10:49:09 +0200910MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
911MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
912MODULE_VERSION(VERSION);
913MODULE_LICENSE("GPL");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200914MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
915MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
916MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200917MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
918MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
919MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
920MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200921MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
922MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
923MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
924MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
925MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
926MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
Max Chou0d484db2020-11-10 11:38:37 +0800927MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin");
928MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin");