blob: b7487ab99eed2246ebfb0962b3ff748941caf06f [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"
21#define RTL_ROM_LMP_3499 0x3499
22#define RTL_ROM_LMP_8723A 0x1200
23#define RTL_ROM_LMP_8723B 0x8723
Larry Finger6c595ea2019-05-29 13:12:34 -050024#define RTL_ROM_LMP_8723D 0x8873
Carlo Caionedb33c772015-05-14 10:49:09 +020025#define RTL_ROM_LMP_8821A 0x8821
26#define RTL_ROM_LMP_8761A 0x8761
Larry Finger1110a2d2016-09-09 10:02:05 -050027#define RTL_ROM_LMP_8822B 0x8822
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +020028#define RTL_CONFIG_MAGIC 0x8723ab55
Carlo Caionedb33c772015-05-14 10:49:09 +020029
Alex Lu907f8492018-02-11 12:24:33 -060030#define IC_MATCH_FL_LMPSUBV (1 << 0)
31#define IC_MATCH_FL_HCIREV (1 << 1)
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020032#define IC_MATCH_FL_HCIVER (1 << 2)
33#define IC_MATCH_FL_HCIBUS (1 << 3)
Alex Lu907f8492018-02-11 12:24:33 -060034#define IC_INFO(lmps, hcir) \
35 .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
36 .lmp_subver = (lmps), \
37 .hci_rev = (hcir)
38
39struct id_table {
40 __u16 match_flags;
41 __u16 lmp_subver;
42 __u16 hci_rev;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020043 __u8 hci_ver;
44 __u8 hci_bus;
Alex Lu907f8492018-02-11 12:24:33 -060045 bool config_needed;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020046 bool has_rom_version;
Alex Lu907f8492018-02-11 12:24:33 -060047 char *fw_name;
48 char *cfg_name;
49};
50
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020051struct btrtl_device_info {
52 const struct id_table *ic_info;
53 u8 rom_version;
54 u8 *fw_data;
55 int fw_len;
56 u8 *cfg_data;
57 int cfg_len;
58};
59
Alex Lu907f8492018-02-11 12:24:33 -060060static const struct id_table ic_id_table[] = {
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020061 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0,
62 .config_needed = false,
63 .has_rom_version = false,
64 .fw_name = "rtl_bt/rtl8723a_fw.bin",
65 .cfg_name = NULL },
66
67 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0,
68 .config_needed = false,
69 .has_rom_version = false,
70 .fw_name = "rtl_bt/rtl8723a_fw.bin",
71 .cfg_name = NULL },
72
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020073 /* 8723BS */
74 { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
75 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS,
76 .lmp_subver = RTL_ROM_LMP_8723B,
77 .hci_rev = 0xb,
78 .hci_ver = 6,
79 .hci_bus = HCI_UART,
80 .config_needed = true,
81 .has_rom_version = true,
82 .fw_name = "rtl_bt/rtl8723bs_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020083 .cfg_name = "rtl_bt/rtl8723bs_config" },
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020084
Alex Lu907f8492018-02-11 12:24:33 -060085 /* 8723B */
86 { IC_INFO(RTL_ROM_LMP_8723B, 0xb),
87 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020088 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060089 .fw_name = "rtl_bt/rtl8723b_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020090 .cfg_name = "rtl_bt/rtl8723b_config" },
Alex Lu907f8492018-02-11 12:24:33 -060091
92 /* 8723D */
93 { IC_INFO(RTL_ROM_LMP_8723B, 0xd),
94 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020095 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060096 .fw_name = "rtl_bt/rtl8723d_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020097 .cfg_name = "rtl_bt/rtl8723d_config" },
Alex Lu907f8492018-02-11 12:24:33 -060098
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020099 /* 8723DS */
100 { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
101 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS,
102 .lmp_subver = RTL_ROM_LMP_8723B,
103 .hci_rev = 0xd,
104 .hci_ver = 8,
105 .hci_bus = HCI_UART,
106 .config_needed = true,
107 .has_rom_version = true,
108 .fw_name = "rtl_bt/rtl8723ds_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200109 .cfg_name = "rtl_bt/rtl8723ds_config" },
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200110
Larry Finger6c595ea2019-05-29 13:12:34 -0500111 /* 8723DU */
112 { IC_INFO(RTL_ROM_LMP_8723D, 0x826C),
113 .config_needed = true,
114 .has_rom_version = true,
115 .fw_name = "rtl_bt/rtl8723d_fw.bin",
116 .cfg_name = "rtl_bt/rtl8723d_config" },
117
Alex Lu907f8492018-02-11 12:24:33 -0600118 /* 8821A */
119 { IC_INFO(RTL_ROM_LMP_8821A, 0xa),
120 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200121 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600122 .fw_name = "rtl_bt/rtl8821a_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200123 .cfg_name = "rtl_bt/rtl8821a_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600124
125 /* 8821C */
126 { IC_INFO(RTL_ROM_LMP_8821A, 0xc),
127 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200128 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600129 .fw_name = "rtl_bt/rtl8821c_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200130 .cfg_name = "rtl_bt/rtl8821c_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600131
132 /* 8761A */
133 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0,
134 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200135 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600136 .fw_name = "rtl_bt/rtl8761a_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200137 .cfg_name = "rtl_bt/rtl8761a_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600138
Alex Lu8ecfdc92018-09-08 12:05:59 -0500139 /* 8822C with USB interface */
140 { IC_INFO(RTL_ROM_LMP_8822B, 0xc),
141 .config_needed = false,
142 .has_rom_version = true,
143 .fw_name = "rtl_bt/rtl8822cu_fw.bin",
144 .cfg_name = "rtl_bt/rtl8822cu_config" },
145
Alex Lu907f8492018-02-11 12:24:33 -0600146 /* 8822B */
147 { IC_INFO(RTL_ROM_LMP_8822B, 0xb),
148 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200149 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600150 .fw_name = "rtl_bt/rtl8822b_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200151 .cfg_name = "rtl_bt/rtl8822b_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600152 };
153
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200154static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
155 u8 hci_ver, u8 hci_bus)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200156{
157 int i;
158
159 for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
160 if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
161 (ic_id_table[i].lmp_subver != lmp_subver))
162 continue;
163 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
164 (ic_id_table[i].hci_rev != hci_rev))
165 continue;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200166 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
167 (ic_id_table[i].hci_ver != hci_ver))
168 continue;
169 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
170 (ic_id_table[i].hci_bus != hci_bus))
171 continue;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200172
173 break;
174 }
175 if (i >= ARRAY_SIZE(ic_id_table))
176 return NULL;
177
178 return &ic_id_table[i];
179}
180
Carlo Caionedb33c772015-05-14 10:49:09 +0200181static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
182{
183 struct rtl_rom_version_evt *rom_version;
184 struct sk_buff *skb;
185
186 /* Read RTL ROM version command */
187 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
188 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200189 rtl_dev_err(hdev, "Read ROM version failed (%ld)\n",
190 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200191 return PTR_ERR(skb);
192 }
193
194 if (skb->len != sizeof(*rom_version)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200195 rtl_dev_err(hdev, "RTL version event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200196 kfree_skb(skb);
197 return -EIO;
198 }
199
200 rom_version = (struct rtl_rom_version_evt *)skb->data;
Hans de Goedea5c76e62018-08-02 16:57:14 +0200201 rtl_dev_info(hdev, "rom_version status=%x version=%x\n",
202 rom_version->status, rom_version->version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200203
204 *version = rom_version->version;
205
206 kfree_skb(skb);
207 return 0;
208}
209
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200210static int rtlbt_parse_firmware(struct hci_dev *hdev,
211 struct btrtl_device_info *btrtl_dev,
Alex Lu907f8492018-02-11 12:24:33 -0600212 unsigned char **_buf)
Carlo Caionedb33c772015-05-14 10:49:09 +0200213{
Colin Ian Kinge5070e072018-09-26 09:32:08 +0100214 static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
Carlo Caionedb33c772015-05-14 10:49:09 +0200215 struct rtl_epatch_header *epatch_info;
216 unsigned char *buf;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200217 int i, len;
Carlo Caionedb33c772015-05-14 10:49:09 +0200218 size_t min_size;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200219 u8 opcode, length, data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200220 int project_id = -1;
221 const unsigned char *fwptr, *chip_id_base;
222 const unsigned char *patch_length_base, *patch_offset_base;
223 u32 patch_offset = 0;
224 u16 patch_length, num_patches;
Larry Finger1110a2d2016-09-09 10:02:05 -0500225 static const struct {
226 __u16 lmp_subver;
227 __u8 id;
228 } project_id_to_lmp_subver[] = {
229 { RTL_ROM_LMP_8723A, 0 },
230 { RTL_ROM_LMP_8723B, 1 },
231 { RTL_ROM_LMP_8821A, 2 },
232 { RTL_ROM_LMP_8761A, 3 },
233 { RTL_ROM_LMP_8822B, 8 },
Alex Lu907f8492018-02-11 12:24:33 -0600234 { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
235 { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
Alex Lu8ecfdc92018-09-08 12:05:59 -0500236 { RTL_ROM_LMP_8822B, 13 }, /* 8822C */
Carlo Caionedb33c772015-05-14 10:49:09 +0200237 };
238
Carlo Caionedb33c772015-05-14 10:49:09 +0200239 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200240 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200241 return -EINVAL;
242
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200243 fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
Carlo Caionedb33c772015-05-14 10:49:09 +0200244 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200245 rtl_dev_err(hdev, "extension section signature mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200246 return -EINVAL;
247 }
248
249 /* Loop from the end of the firmware parsing instructions, until
250 * we find an instruction that identifies the "project ID" for the
251 * hardware supported by this firwmare file.
252 * Once we have that, we double-check that that project_id is suitable
253 * for the hardware we are working with.
254 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200255 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200256 opcode = *--fwptr;
257 length = *--fwptr;
258 data = *--fwptr;
259
260 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
261
262 if (opcode == 0xff) /* EOF */
263 break;
264
265 if (length == 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200266 rtl_dev_err(hdev, "found instruction with length 0\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200267 return -EINVAL;
268 }
269
270 if (opcode == 0 && length == 1) {
271 project_id = data;
272 break;
273 }
274
275 fwptr -= length;
276 }
277
278 if (project_id < 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200279 rtl_dev_err(hdev, "failed to find version instruction\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200280 return -EINVAL;
281 }
282
Larry Finger1110a2d2016-09-09 10:02:05 -0500283 /* Find project_id in table */
284 for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
285 if (project_id == project_id_to_lmp_subver[i].id)
286 break;
287 }
288
289 if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200290 rtl_dev_err(hdev, "unknown project id %d\n", project_id);
Carlo Caionedb33c772015-05-14 10:49:09 +0200291 return -EINVAL;
292 }
293
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200294 if (btrtl_dev->ic_info->lmp_subver !=
295 project_id_to_lmp_subver[i].lmp_subver) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200296 rtl_dev_err(hdev, "firmware is for %x but this is a %x\n",
297 project_id_to_lmp_subver[i].lmp_subver,
298 btrtl_dev->ic_info->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200299 return -EINVAL;
300 }
301
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200302 epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200303 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200304 rtl_dev_err(hdev, "bad EPATCH signature\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200305 return -EINVAL;
306 }
307
308 num_patches = le16_to_cpu(epatch_info->num_patches);
309 BT_DBG("fw_version=%x, num_patches=%d",
310 le32_to_cpu(epatch_info->fw_version), num_patches);
311
312 /* After the rtl_epatch_header there is a funky patch metadata section.
313 * Assuming 2 patches, the layout is:
314 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
315 *
316 * Find the right patch for this chip.
317 */
318 min_size += 8 * num_patches;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200319 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200320 return -EINVAL;
321
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200322 chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
Carlo Caionedb33c772015-05-14 10:49:09 +0200323 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
324 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
325 for (i = 0; i < num_patches; i++) {
326 u16 chip_id = get_unaligned_le16(chip_id_base +
327 (i * sizeof(u16)));
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200328 if (chip_id == btrtl_dev->rom_version + 1) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200329 patch_length = get_unaligned_le16(patch_length_base +
330 (i * sizeof(u16)));
331 patch_offset = get_unaligned_le32(patch_offset_base +
332 (i * sizeof(u32)));
333 break;
334 }
335 }
336
337 if (!patch_offset) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200338 rtl_dev_err(hdev, "didn't find patch for chip id %d",
339 btrtl_dev->rom_version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200340 return -EINVAL;
341 }
342
343 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
344 min_size = patch_offset + patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200345 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200346 return -EINVAL;
347
348 /* Copy the firmware into a new buffer and write the version at
349 * the end.
350 */
351 len = patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200352 buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length,
353 GFP_KERNEL);
Carlo Caionedb33c772015-05-14 10:49:09 +0200354 if (!buf)
355 return -ENOMEM;
356
357 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
358
359 *_buf = buf;
360 return len;
361}
362
363static int rtl_download_firmware(struct hci_dev *hdev,
364 const unsigned char *data, int fw_len)
365{
366 struct rtl_download_cmd *dl_cmd;
367 int frag_num = fw_len / RTL_FRAG_LEN + 1;
368 int frag_len = RTL_FRAG_LEN;
369 int ret = 0;
370 int i;
371
372 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
373 if (!dl_cmd)
374 return -ENOMEM;
375
376 for (i = 0; i < frag_num; i++) {
377 struct sk_buff *skb;
378
379 BT_DBG("download fw (%d/%d)", i, frag_num);
380
381 dl_cmd->index = i;
382 if (i == (frag_num - 1)) {
383 dl_cmd->index |= 0x80; /* data end */
384 frag_len = fw_len % RTL_FRAG_LEN;
385 }
386 memcpy(dl_cmd->data, data, frag_len);
387
388 /* Send download command */
389 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
390 HCI_INIT_TIMEOUT);
391 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200392 rtl_dev_err(hdev, "download fw command failed (%ld)\n",
393 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200394 ret = -PTR_ERR(skb);
395 goto out;
396 }
397
398 if (skb->len != sizeof(struct rtl_download_response)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200399 rtl_dev_err(hdev, "download fw event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200400 kfree_skb(skb);
401 ret = -EIO;
402 goto out;
403 }
404
405 kfree_skb(skb);
406 data += RTL_FRAG_LEN;
407 }
408
409out:
410 kfree(dl_cmd);
411 return ret;
412}
413
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200414static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
Larry Finger1110a2d2016-09-09 10:02:05 -0500415{
416 const struct firmware *fw;
417 int ret;
418
Hans de Goedea5c76e62018-08-02 16:57:14 +0200419 rtl_dev_info(hdev, "rtl: loading %s\n", name);
Larry Finger1110a2d2016-09-09 10:02:05 -0500420 ret = request_firmware(&fw, name, &hdev->dev);
Larry Fingerabed84a2017-02-19 20:04:57 -0600421 if (ret < 0)
Larry Finger1110a2d2016-09-09 10:02:05 -0500422 return ret;
Larry Finger1110a2d2016-09-09 10:02:05 -0500423 ret = fw->size;
424 *buff = kmemdup(fw->data, ret, GFP_KERNEL);
Dan Carpenterc3327bd2017-07-28 17:41:11 +0300425 if (!*buff)
426 ret = -ENOMEM;
Larry Finger1110a2d2016-09-09 10:02:05 -0500427
428 release_firmware(fw);
429
430 return ret;
431}
432
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200433static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
434 struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200435{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200436 if (btrtl_dev->fw_len < 8)
437 return -EINVAL;
Carlo Caionedb33c772015-05-14 10:49:09 +0200438
439 /* Check that the firmware doesn't have the epatch signature
440 * (which is only for RTL8723B and newer).
441 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200442 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200443 rtl_dev_err(hdev, "unexpected EPATCH signature!\n");
Alex Lu907f8492018-02-11 12:24:33 -0600444 return -EINVAL;
445 }
446
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200447 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
448 btrtl_dev->fw_len);
449}
Alex Lu907f8492018-02-11 12:24:33 -0600450
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200451static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
452 struct btrtl_device_info *btrtl_dev)
453{
454 unsigned char *fw_data = NULL;
455 int ret;
456 u8 *tbuff;
Carlo Caionedb33c772015-05-14 10:49:09 +0200457
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200458 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200459 if (ret < 0)
460 goto out;
461
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200462 if (btrtl_dev->cfg_len > 0) {
463 tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
Larry Finger1110a2d2016-09-09 10:02:05 -0500464 if (!tbuff) {
465 ret = -ENOMEM;
466 goto out;
467 }
468
469 memcpy(tbuff, fw_data, ret);
470 kfree(fw_data);
471
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200472 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
473 ret += btrtl_dev->cfg_len;
Larry Finger1110a2d2016-09-09 10:02:05 -0500474
475 fw_data = tbuff;
476 }
477
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200478 rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
Larry Finger1110a2d2016-09-09 10:02:05 -0500479
Carlo Caionedb33c772015-05-14 10:49:09 +0200480 ret = rtl_download_firmware(hdev, fw_data, ret);
Carlo Caionedb33c772015-05-14 10:49:09 +0200481
482out:
Larry Finger1110a2d2016-09-09 10:02:05 -0500483 kfree(fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200484 return ret;
485}
486
487static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
488{
489 struct sk_buff *skb;
490
491 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
492 HCI_INIT_TIMEOUT);
493 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200494 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
495 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200496 return skb;
497 }
498
499 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200500 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200501 kfree_skb(skb);
502 return ERR_PTR(-EIO);
503 }
504
505 return skb;
506}
507
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200508void btrtl_free(struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200509{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200510 kfree(btrtl_dev->fw_data);
511 kfree(btrtl_dev->cfg_data);
512 kfree(btrtl_dev);
513}
514EXPORT_SYMBOL_GPL(btrtl_free);
515
Hans de Goede1cc194c2018-08-02 16:57:17 +0200516struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
517 const char *postfix)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200518{
519 struct btrtl_device_info *btrtl_dev;
Carlo Caionedb33c772015-05-14 10:49:09 +0200520 struct sk_buff *skb;
521 struct hci_rp_read_local_version *resp;
Hans de Goede1cc194c2018-08-02 16:57:17 +0200522 char cfg_name[40];
Alex Lu907f8492018-02-11 12:24:33 -0600523 u16 hci_rev, lmp_subver;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200524 u8 hci_ver;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200525 int ret;
526
527 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
528 if (!btrtl_dev) {
529 ret = -ENOMEM;
530 goto err_alloc;
531 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200532
533 skb = btrtl_read_local_version(hdev);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200534 if (IS_ERR(skb)) {
535 ret = PTR_ERR(skb);
536 goto err_free;
537 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200538
539 resp = (struct hci_rp_read_local_version *)skb->data;
Hans de Goedea5c76e62018-08-02 16:57:14 +0200540 rtl_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
541 resp->hci_ver, resp->hci_rev,
542 resp->lmp_ver, resp->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200543
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200544 hci_ver = resp->hci_ver;
Alex Lu907f8492018-02-11 12:24:33 -0600545 hci_rev = le16_to_cpu(resp->hci_rev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200546 lmp_subver = le16_to_cpu(resp->lmp_subver);
547 kfree_skb(skb);
548
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200549 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
550 hdev->bus);
551
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200552 if (!btrtl_dev->ic_info) {
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800553 rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200554 lmp_subver, hci_rev, hci_ver);
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800555 return btrtl_dev;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200556 }
557
558 if (btrtl_dev->ic_info->has_rom_version) {
559 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
560 if (ret)
561 goto err_free;
562 }
563
564 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
565 &btrtl_dev->fw_data);
566 if (btrtl_dev->fw_len < 0) {
567 rtl_dev_err(hdev, "firmware file %s not found\n",
568 btrtl_dev->ic_info->fw_name);
569 ret = btrtl_dev->fw_len;
570 goto err_free;
571 }
572
573 if (btrtl_dev->ic_info->cfg_name) {
Hans de Goede1cc194c2018-08-02 16:57:17 +0200574 if (postfix) {
575 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
576 btrtl_dev->ic_info->cfg_name, postfix);
577 } else {
578 snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
579 btrtl_dev->ic_info->cfg_name);
580 }
581 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200582 &btrtl_dev->cfg_data);
583 if (btrtl_dev->ic_info->config_needed &&
584 btrtl_dev->cfg_len <= 0) {
585 rtl_dev_err(hdev, "mandatory config file %s not found\n",
586 btrtl_dev->ic_info->cfg_name);
587 ret = btrtl_dev->cfg_len;
588 goto err_free;
589 }
590 }
591
592 return btrtl_dev;
593
594err_free:
595 btrtl_free(btrtl_dev);
596err_alloc:
597 return ERR_PTR(ret);
598}
599EXPORT_SYMBOL_GPL(btrtl_initialize);
600
601int btrtl_download_firmware(struct hci_dev *hdev,
602 struct btrtl_device_info *btrtl_dev)
603{
Carlo Caionedb33c772015-05-14 10:49:09 +0200604 /* Match a set of subver values that correspond to stock firmware,
605 * which is not compatible with standard btusb.
606 * If matched, upload an alternative firmware that does conform to
607 * standard btusb. Once that firmware is uploaded, the subver changes
608 * to a different value.
609 */
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800610 if (!btrtl_dev->ic_info) {
611 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
612 return 0;
613 }
614
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200615 switch (btrtl_dev->ic_info->lmp_subver) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200616 case RTL_ROM_LMP_8723A:
617 case RTL_ROM_LMP_3499:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200618 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200619 case RTL_ROM_LMP_8723B:
Carlo Caionedb33c772015-05-14 10:49:09 +0200620 case RTL_ROM_LMP_8821A:
Carlo Caionedb33c772015-05-14 10:49:09 +0200621 case RTL_ROM_LMP_8761A:
Larry Finger1110a2d2016-09-09 10:02:05 -0500622 case RTL_ROM_LMP_8822B:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200623 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200624 default:
Hans de Goedea5c76e62018-08-02 16:57:14 +0200625 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200626 return 0;
627 }
628}
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200629EXPORT_SYMBOL_GPL(btrtl_download_firmware);
630
631int btrtl_setup_realtek(struct hci_dev *hdev)
632{
633 struct btrtl_device_info *btrtl_dev;
634 int ret;
635
Hans de Goede1cc194c2018-08-02 16:57:17 +0200636 btrtl_dev = btrtl_initialize(hdev, NULL);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200637 if (IS_ERR(btrtl_dev))
638 return PTR_ERR(btrtl_dev);
639
640 ret = btrtl_download_firmware(hdev, btrtl_dev);
641
642 btrtl_free(btrtl_dev);
643
Alex Lu65251e22019-08-30 20:02:14 +0800644 /* Enable controller to do both LE scan and BR/EDR inquiry
645 * simultaneously.
646 */
647 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
648
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200649 return ret;
650}
Carlo Caionedb33c772015-05-14 10:49:09 +0200651EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
652
Jian-Hong Pan7af3f5582019-06-25 16:30:51 +0800653int btrtl_shutdown_realtek(struct hci_dev *hdev)
654{
655 struct sk_buff *skb;
656 int ret;
657
658 /* According to the vendor driver, BT must be reset on close to avoid
659 * firmware crash.
660 */
661 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
662 if (IS_ERR(skb)) {
663 ret = PTR_ERR(skb);
664 bt_dev_err(hdev, "HCI reset during shutdown failed");
665 return ret;
666 }
667 kfree_skb(skb);
668
669 return 0;
670}
671EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
672
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200673static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
674{
675 switch (device_baudrate) {
676 case 0x0252a00a:
677 return 230400;
678
679 case 0x05f75004:
680 return 921600;
681
682 case 0x00005004:
683 return 1000000;
684
685 case 0x04928002:
686 case 0x01128002:
687 return 1500000;
688
689 case 0x00005002:
690 return 2000000;
691
692 case 0x0000b001:
693 return 2500000;
694
695 case 0x04928001:
696 return 3000000;
697
698 case 0x052a6001:
699 return 3500000;
700
701 case 0x00005001:
702 return 4000000;
703
704 case 0x0252c014:
705 default:
706 return 115200;
707 }
708}
709
710int btrtl_get_uart_settings(struct hci_dev *hdev,
711 struct btrtl_device_info *btrtl_dev,
712 unsigned int *controller_baudrate,
713 u32 *device_baudrate, bool *flow_control)
714{
715 struct rtl_vendor_config *config;
716 struct rtl_vendor_config_entry *entry;
717 int i, total_data_len;
718 bool found = false;
719
720 total_data_len = btrtl_dev->cfg_len - sizeof(*config);
721 if (total_data_len <= 0) {
722 rtl_dev_warn(hdev, "no config loaded\n");
723 return -EINVAL;
724 }
725
726 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
727 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
728 rtl_dev_err(hdev, "invalid config magic\n");
729 return -EINVAL;
730 }
731
732 if (total_data_len < le16_to_cpu(config->total_len)) {
733 rtl_dev_err(hdev, "config is too short\n");
734 return -EINVAL;
735 }
736
737 for (i = 0; i < total_data_len; ) {
738 entry = ((void *)config->entry) + i;
739
740 switch (le16_to_cpu(entry->offset)) {
741 case 0xc:
742 if (entry->len < sizeof(*device_baudrate)) {
743 rtl_dev_err(hdev, "invalid UART config entry\n");
744 return -EINVAL;
745 }
746
747 *device_baudrate = get_unaligned_le32(entry->data);
748 *controller_baudrate = btrtl_convert_baudrate(
749 *device_baudrate);
750
751 if (entry->len >= 13)
752 *flow_control = !!(entry->data[12] & BIT(2));
753 else
754 *flow_control = false;
755
756 found = true;
757 break;
758
759 default:
760 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n",
761 le16_to_cpu(entry->offset), entry->len);
762 break;
763 };
764
765 i += sizeof(*entry) + entry->len;
766 }
767
768 if (!found) {
769 rtl_dev_err(hdev, "no UART config entry found\n");
770 return -ENOENT;
771 }
772
773 rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate);
774 rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate);
775 rtl_dev_dbg(hdev, "flow control %d\n", *flow_control);
776
777 return 0;
778}
779EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
780
Carlo Caionedb33c772015-05-14 10:49:09 +0200781MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
782MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
783MODULE_VERSION(VERSION);
784MODULE_LICENSE("GPL");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200785MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
786MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
787MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200788MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
789MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
790MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
791MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200792MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
793MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
794MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
795MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
796MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
797MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");