blob: 208feef63de40f2c629f9ee1543b1e771abf9315 [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
24#define RTL_ROM_LMP_8821A 0x8821
25#define RTL_ROM_LMP_8761A 0x8761
Larry Finger1110a2d2016-09-09 10:02:05 -050026#define RTL_ROM_LMP_8822B 0x8822
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)
Alex Lu907f8492018-02-11 12:24:33 -060033#define IC_INFO(lmps, hcir) \
34 .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
35 .lmp_subver = (lmps), \
36 .hci_rev = (hcir)
37
38struct id_table {
39 __u16 match_flags;
40 __u16 lmp_subver;
41 __u16 hci_rev;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020042 __u8 hci_ver;
43 __u8 hci_bus;
Alex Lu907f8492018-02-11 12:24:33 -060044 bool config_needed;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020045 bool has_rom_version;
Alex Lu907f8492018-02-11 12:24:33 -060046 char *fw_name;
47 char *cfg_name;
48};
49
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020050struct btrtl_device_info {
51 const struct id_table *ic_info;
52 u8 rom_version;
53 u8 *fw_data;
54 int fw_len;
55 u8 *cfg_data;
56 int cfg_len;
57};
58
Alex Lu907f8492018-02-11 12:24:33 -060059static const struct id_table ic_id_table[] = {
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020060 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0,
61 .config_needed = false,
62 .has_rom_version = false,
63 .fw_name = "rtl_bt/rtl8723a_fw.bin",
64 .cfg_name = NULL },
65
66 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0,
67 .config_needed = false,
68 .has_rom_version = false,
69 .fw_name = "rtl_bt/rtl8723a_fw.bin",
70 .cfg_name = NULL },
71
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020072 /* 8723BS */
73 { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
74 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS,
75 .lmp_subver = RTL_ROM_LMP_8723B,
76 .hci_rev = 0xb,
77 .hci_ver = 6,
78 .hci_bus = HCI_UART,
79 .config_needed = true,
80 .has_rom_version = true,
81 .fw_name = "rtl_bt/rtl8723bs_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020082 .cfg_name = "rtl_bt/rtl8723bs_config" },
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020083
Alex Lu907f8492018-02-11 12:24:33 -060084 /* 8723B */
85 { IC_INFO(RTL_ROM_LMP_8723B, 0xb),
86 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020087 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060088 .fw_name = "rtl_bt/rtl8723b_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020089 .cfg_name = "rtl_bt/rtl8723b_config" },
Alex Lu907f8492018-02-11 12:24:33 -060090
91 /* 8723D */
92 { IC_INFO(RTL_ROM_LMP_8723B, 0xd),
93 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020094 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060095 .fw_name = "rtl_bt/rtl8723d_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020096 .cfg_name = "rtl_bt/rtl8723d_config" },
Alex Lu907f8492018-02-11 12:24:33 -060097
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020098 /* 8723DS */
99 { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
100 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS,
101 .lmp_subver = RTL_ROM_LMP_8723B,
102 .hci_rev = 0xd,
103 .hci_ver = 8,
104 .hci_bus = HCI_UART,
105 .config_needed = true,
106 .has_rom_version = true,
107 .fw_name = "rtl_bt/rtl8723ds_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200108 .cfg_name = "rtl_bt/rtl8723ds_config" },
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200109
Alex Lu907f8492018-02-11 12:24:33 -0600110 /* 8821A */
111 { IC_INFO(RTL_ROM_LMP_8821A, 0xa),
112 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200113 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600114 .fw_name = "rtl_bt/rtl8821a_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200115 .cfg_name = "rtl_bt/rtl8821a_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600116
117 /* 8821C */
118 { IC_INFO(RTL_ROM_LMP_8821A, 0xc),
119 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200120 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600121 .fw_name = "rtl_bt/rtl8821c_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200122 .cfg_name = "rtl_bt/rtl8821c_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600123
124 /* 8761A */
125 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0,
126 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200127 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600128 .fw_name = "rtl_bt/rtl8761a_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200129 .cfg_name = "rtl_bt/rtl8761a_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600130
Alex Lu8ecfdc92018-09-08 12:05:59 -0500131 /* 8822C with USB interface */
132 { IC_INFO(RTL_ROM_LMP_8822B, 0xc),
133 .config_needed = false,
134 .has_rom_version = true,
135 .fw_name = "rtl_bt/rtl8822cu_fw.bin",
136 .cfg_name = "rtl_bt/rtl8822cu_config" },
137
Alex Lu907f8492018-02-11 12:24:33 -0600138 /* 8822B */
139 { IC_INFO(RTL_ROM_LMP_8822B, 0xb),
140 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200141 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600142 .fw_name = "rtl_bt/rtl8822b_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200143 .cfg_name = "rtl_bt/rtl8822b_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600144 };
145
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200146static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
147 u8 hci_ver, u8 hci_bus)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200148{
149 int i;
150
151 for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
152 if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
153 (ic_id_table[i].lmp_subver != lmp_subver))
154 continue;
155 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
156 (ic_id_table[i].hci_rev != hci_rev))
157 continue;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200158 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
159 (ic_id_table[i].hci_ver != hci_ver))
160 continue;
161 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
162 (ic_id_table[i].hci_bus != hci_bus))
163 continue;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200164
165 break;
166 }
167 if (i >= ARRAY_SIZE(ic_id_table))
168 return NULL;
169
170 return &ic_id_table[i];
171}
172
Carlo Caionedb33c772015-05-14 10:49:09 +0200173static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
174{
175 struct rtl_rom_version_evt *rom_version;
176 struct sk_buff *skb;
177
178 /* Read RTL ROM version command */
179 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
180 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200181 rtl_dev_err(hdev, "Read ROM version failed (%ld)\n",
182 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200183 return PTR_ERR(skb);
184 }
185
186 if (skb->len != sizeof(*rom_version)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200187 rtl_dev_err(hdev, "RTL version event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200188 kfree_skb(skb);
189 return -EIO;
190 }
191
192 rom_version = (struct rtl_rom_version_evt *)skb->data;
Hans de Goedea5c76e62018-08-02 16:57:14 +0200193 rtl_dev_info(hdev, "rom_version status=%x version=%x\n",
194 rom_version->status, rom_version->version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200195
196 *version = rom_version->version;
197
198 kfree_skb(skb);
199 return 0;
200}
201
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200202static int rtlbt_parse_firmware(struct hci_dev *hdev,
203 struct btrtl_device_info *btrtl_dev,
Alex Lu907f8492018-02-11 12:24:33 -0600204 unsigned char **_buf)
Carlo Caionedb33c772015-05-14 10:49:09 +0200205{
Colin Ian Kinge5070e072018-09-26 09:32:08 +0100206 static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
Carlo Caionedb33c772015-05-14 10:49:09 +0200207 struct rtl_epatch_header *epatch_info;
208 unsigned char *buf;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200209 int i, len;
Carlo Caionedb33c772015-05-14 10:49:09 +0200210 size_t min_size;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200211 u8 opcode, length, data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200212 int project_id = -1;
213 const unsigned char *fwptr, *chip_id_base;
214 const unsigned char *patch_length_base, *patch_offset_base;
215 u32 patch_offset = 0;
216 u16 patch_length, num_patches;
Larry Finger1110a2d2016-09-09 10:02:05 -0500217 static const struct {
218 __u16 lmp_subver;
219 __u8 id;
220 } project_id_to_lmp_subver[] = {
221 { RTL_ROM_LMP_8723A, 0 },
222 { RTL_ROM_LMP_8723B, 1 },
223 { RTL_ROM_LMP_8821A, 2 },
224 { RTL_ROM_LMP_8761A, 3 },
225 { RTL_ROM_LMP_8822B, 8 },
Alex Lu907f8492018-02-11 12:24:33 -0600226 { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
227 { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
Alex Lu8ecfdc92018-09-08 12:05:59 -0500228 { RTL_ROM_LMP_8822B, 13 }, /* 8822C */
Carlo Caionedb33c772015-05-14 10:49:09 +0200229 };
230
Carlo Caionedb33c772015-05-14 10:49:09 +0200231 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200232 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200233 return -EINVAL;
234
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200235 fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
Carlo Caionedb33c772015-05-14 10:49:09 +0200236 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200237 rtl_dev_err(hdev, "extension section signature mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200238 return -EINVAL;
239 }
240
241 /* Loop from the end of the firmware parsing instructions, until
242 * we find an instruction that identifies the "project ID" for the
243 * hardware supported by this firwmare file.
244 * Once we have that, we double-check that that project_id is suitable
245 * for the hardware we are working with.
246 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200247 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200248 opcode = *--fwptr;
249 length = *--fwptr;
250 data = *--fwptr;
251
252 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
253
254 if (opcode == 0xff) /* EOF */
255 break;
256
257 if (length == 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200258 rtl_dev_err(hdev, "found instruction with length 0\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200259 return -EINVAL;
260 }
261
262 if (opcode == 0 && length == 1) {
263 project_id = data;
264 break;
265 }
266
267 fwptr -= length;
268 }
269
270 if (project_id < 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200271 rtl_dev_err(hdev, "failed to find version instruction\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200272 return -EINVAL;
273 }
274
Larry Finger1110a2d2016-09-09 10:02:05 -0500275 /* Find project_id in table */
276 for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
277 if (project_id == project_id_to_lmp_subver[i].id)
278 break;
279 }
280
281 if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200282 rtl_dev_err(hdev, "unknown project id %d\n", project_id);
Carlo Caionedb33c772015-05-14 10:49:09 +0200283 return -EINVAL;
284 }
285
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200286 if (btrtl_dev->ic_info->lmp_subver !=
287 project_id_to_lmp_subver[i].lmp_subver) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200288 rtl_dev_err(hdev, "firmware is for %x but this is a %x\n",
289 project_id_to_lmp_subver[i].lmp_subver,
290 btrtl_dev->ic_info->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200291 return -EINVAL;
292 }
293
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200294 epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200295 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200296 rtl_dev_err(hdev, "bad EPATCH signature\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200297 return -EINVAL;
298 }
299
300 num_patches = le16_to_cpu(epatch_info->num_patches);
301 BT_DBG("fw_version=%x, num_patches=%d",
302 le32_to_cpu(epatch_info->fw_version), num_patches);
303
304 /* After the rtl_epatch_header there is a funky patch metadata section.
305 * Assuming 2 patches, the layout is:
306 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
307 *
308 * Find the right patch for this chip.
309 */
310 min_size += 8 * num_patches;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200311 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200312 return -EINVAL;
313
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200314 chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
Carlo Caionedb33c772015-05-14 10:49:09 +0200315 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
316 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
317 for (i = 0; i < num_patches; i++) {
318 u16 chip_id = get_unaligned_le16(chip_id_base +
319 (i * sizeof(u16)));
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200320 if (chip_id == btrtl_dev->rom_version + 1) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200321 patch_length = get_unaligned_le16(patch_length_base +
322 (i * sizeof(u16)));
323 patch_offset = get_unaligned_le32(patch_offset_base +
324 (i * sizeof(u32)));
325 break;
326 }
327 }
328
329 if (!patch_offset) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200330 rtl_dev_err(hdev, "didn't find patch for chip id %d",
331 btrtl_dev->rom_version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200332 return -EINVAL;
333 }
334
335 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
336 min_size = patch_offset + patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200337 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200338 return -EINVAL;
339
340 /* Copy the firmware into a new buffer and write the version at
341 * the end.
342 */
343 len = patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200344 buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length,
345 GFP_KERNEL);
Carlo Caionedb33c772015-05-14 10:49:09 +0200346 if (!buf)
347 return -ENOMEM;
348
349 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
350
351 *_buf = buf;
352 return len;
353}
354
355static int rtl_download_firmware(struct hci_dev *hdev,
356 const unsigned char *data, int fw_len)
357{
358 struct rtl_download_cmd *dl_cmd;
359 int frag_num = fw_len / RTL_FRAG_LEN + 1;
360 int frag_len = RTL_FRAG_LEN;
361 int ret = 0;
362 int i;
363
364 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
365 if (!dl_cmd)
366 return -ENOMEM;
367
368 for (i = 0; i < frag_num; i++) {
369 struct sk_buff *skb;
370
371 BT_DBG("download fw (%d/%d)", i, frag_num);
372
373 dl_cmd->index = i;
374 if (i == (frag_num - 1)) {
375 dl_cmd->index |= 0x80; /* data end */
376 frag_len = fw_len % RTL_FRAG_LEN;
377 }
378 memcpy(dl_cmd->data, data, frag_len);
379
380 /* Send download command */
381 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
382 HCI_INIT_TIMEOUT);
383 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200384 rtl_dev_err(hdev, "download fw command failed (%ld)\n",
385 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200386 ret = -PTR_ERR(skb);
387 goto out;
388 }
389
390 if (skb->len != sizeof(struct rtl_download_response)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200391 rtl_dev_err(hdev, "download fw event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200392 kfree_skb(skb);
393 ret = -EIO;
394 goto out;
395 }
396
397 kfree_skb(skb);
398 data += RTL_FRAG_LEN;
399 }
400
401out:
402 kfree(dl_cmd);
403 return ret;
404}
405
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200406static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
Larry Finger1110a2d2016-09-09 10:02:05 -0500407{
408 const struct firmware *fw;
409 int ret;
410
Hans de Goedea5c76e62018-08-02 16:57:14 +0200411 rtl_dev_info(hdev, "rtl: loading %s\n", name);
Larry Finger1110a2d2016-09-09 10:02:05 -0500412 ret = request_firmware(&fw, name, &hdev->dev);
Larry Fingerabed84a2017-02-19 20:04:57 -0600413 if (ret < 0)
Larry Finger1110a2d2016-09-09 10:02:05 -0500414 return ret;
Larry Finger1110a2d2016-09-09 10:02:05 -0500415 ret = fw->size;
416 *buff = kmemdup(fw->data, ret, GFP_KERNEL);
Dan Carpenterc3327bd2017-07-28 17:41:11 +0300417 if (!*buff)
418 ret = -ENOMEM;
Larry Finger1110a2d2016-09-09 10:02:05 -0500419
420 release_firmware(fw);
421
422 return ret;
423}
424
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200425static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
426 struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200427{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200428 if (btrtl_dev->fw_len < 8)
429 return -EINVAL;
Carlo Caionedb33c772015-05-14 10:49:09 +0200430
431 /* Check that the firmware doesn't have the epatch signature
432 * (which is only for RTL8723B and newer).
433 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200434 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200435 rtl_dev_err(hdev, "unexpected EPATCH signature!\n");
Alex Lu907f8492018-02-11 12:24:33 -0600436 return -EINVAL;
437 }
438
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200439 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
440 btrtl_dev->fw_len);
441}
Alex Lu907f8492018-02-11 12:24:33 -0600442
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200443static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
444 struct btrtl_device_info *btrtl_dev)
445{
446 unsigned char *fw_data = NULL;
447 int ret;
448 u8 *tbuff;
Carlo Caionedb33c772015-05-14 10:49:09 +0200449
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200450 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200451 if (ret < 0)
452 goto out;
453
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200454 if (btrtl_dev->cfg_len > 0) {
455 tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
Larry Finger1110a2d2016-09-09 10:02:05 -0500456 if (!tbuff) {
457 ret = -ENOMEM;
458 goto out;
459 }
460
461 memcpy(tbuff, fw_data, ret);
462 kfree(fw_data);
463
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200464 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
465 ret += btrtl_dev->cfg_len;
Larry Finger1110a2d2016-09-09 10:02:05 -0500466
467 fw_data = tbuff;
468 }
469
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200470 rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
Larry Finger1110a2d2016-09-09 10:02:05 -0500471
Carlo Caionedb33c772015-05-14 10:49:09 +0200472 ret = rtl_download_firmware(hdev, fw_data, ret);
Carlo Caionedb33c772015-05-14 10:49:09 +0200473
474out:
Larry Finger1110a2d2016-09-09 10:02:05 -0500475 kfree(fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200476 return ret;
477}
478
479static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
480{
481 struct sk_buff *skb;
482
483 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
484 HCI_INIT_TIMEOUT);
485 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200486 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
487 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200488 return skb;
489 }
490
491 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200492 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200493 kfree_skb(skb);
494 return ERR_PTR(-EIO);
495 }
496
497 return skb;
498}
499
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200500void btrtl_free(struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200501{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200502 kfree(btrtl_dev->fw_data);
503 kfree(btrtl_dev->cfg_data);
504 kfree(btrtl_dev);
505}
506EXPORT_SYMBOL_GPL(btrtl_free);
507
Hans de Goede1cc194c2018-08-02 16:57:17 +0200508struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
509 const char *postfix)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200510{
511 struct btrtl_device_info *btrtl_dev;
Carlo Caionedb33c772015-05-14 10:49:09 +0200512 struct sk_buff *skb;
513 struct hci_rp_read_local_version *resp;
Hans de Goede1cc194c2018-08-02 16:57:17 +0200514 char cfg_name[40];
Alex Lu907f8492018-02-11 12:24:33 -0600515 u16 hci_rev, lmp_subver;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200516 u8 hci_ver;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200517 int ret;
518
519 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
520 if (!btrtl_dev) {
521 ret = -ENOMEM;
522 goto err_alloc;
523 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200524
525 skb = btrtl_read_local_version(hdev);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200526 if (IS_ERR(skb)) {
527 ret = PTR_ERR(skb);
528 goto err_free;
529 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200530
531 resp = (struct hci_rp_read_local_version *)skb->data;
Hans de Goedea5c76e62018-08-02 16:57:14 +0200532 rtl_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
533 resp->hci_ver, resp->hci_rev,
534 resp->lmp_ver, resp->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200535
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200536 hci_ver = resp->hci_ver;
Alex Lu907f8492018-02-11 12:24:33 -0600537 hci_rev = le16_to_cpu(resp->hci_rev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200538 lmp_subver = le16_to_cpu(resp->lmp_subver);
539 kfree_skb(skb);
540
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200541 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
542 hdev->bus);
543
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200544 if (!btrtl_dev->ic_info) {
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800545 rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200546 lmp_subver, hci_rev, hci_ver);
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800547 return btrtl_dev;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200548 }
549
550 if (btrtl_dev->ic_info->has_rom_version) {
551 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
552 if (ret)
553 goto err_free;
554 }
555
556 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
557 &btrtl_dev->fw_data);
558 if (btrtl_dev->fw_len < 0) {
559 rtl_dev_err(hdev, "firmware file %s not found\n",
560 btrtl_dev->ic_info->fw_name);
561 ret = btrtl_dev->fw_len;
562 goto err_free;
563 }
564
565 if (btrtl_dev->ic_info->cfg_name) {
Hans de Goede1cc194c2018-08-02 16:57:17 +0200566 if (postfix) {
567 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
568 btrtl_dev->ic_info->cfg_name, postfix);
569 } else {
570 snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
571 btrtl_dev->ic_info->cfg_name);
572 }
573 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200574 &btrtl_dev->cfg_data);
575 if (btrtl_dev->ic_info->config_needed &&
576 btrtl_dev->cfg_len <= 0) {
577 rtl_dev_err(hdev, "mandatory config file %s not found\n",
578 btrtl_dev->ic_info->cfg_name);
579 ret = btrtl_dev->cfg_len;
580 goto err_free;
581 }
582 }
583
584 return btrtl_dev;
585
586err_free:
587 btrtl_free(btrtl_dev);
588err_alloc:
589 return ERR_PTR(ret);
590}
591EXPORT_SYMBOL_GPL(btrtl_initialize);
592
593int btrtl_download_firmware(struct hci_dev *hdev,
594 struct btrtl_device_info *btrtl_dev)
595{
Carlo Caionedb33c772015-05-14 10:49:09 +0200596 /* Match a set of subver values that correspond to stock firmware,
597 * which is not compatible with standard btusb.
598 * If matched, upload an alternative firmware that does conform to
599 * standard btusb. Once that firmware is uploaded, the subver changes
600 * to a different value.
601 */
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800602 if (!btrtl_dev->ic_info) {
603 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
604 return 0;
605 }
606
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200607 switch (btrtl_dev->ic_info->lmp_subver) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200608 case RTL_ROM_LMP_8723A:
609 case RTL_ROM_LMP_3499:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200610 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200611 case RTL_ROM_LMP_8723B:
Carlo Caionedb33c772015-05-14 10:49:09 +0200612 case RTL_ROM_LMP_8821A:
Carlo Caionedb33c772015-05-14 10:49:09 +0200613 case RTL_ROM_LMP_8761A:
Larry Finger1110a2d2016-09-09 10:02:05 -0500614 case RTL_ROM_LMP_8822B:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200615 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200616 default:
Hans de Goedea5c76e62018-08-02 16:57:14 +0200617 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200618 return 0;
619 }
620}
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200621EXPORT_SYMBOL_GPL(btrtl_download_firmware);
622
623int btrtl_setup_realtek(struct hci_dev *hdev)
624{
625 struct btrtl_device_info *btrtl_dev;
626 int ret;
627
Hans de Goede1cc194c2018-08-02 16:57:17 +0200628 btrtl_dev = btrtl_initialize(hdev, NULL);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200629 if (IS_ERR(btrtl_dev))
630 return PTR_ERR(btrtl_dev);
631
632 ret = btrtl_download_firmware(hdev, btrtl_dev);
633
634 btrtl_free(btrtl_dev);
635
636 return ret;
637}
Carlo Caionedb33c772015-05-14 10:49:09 +0200638EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
639
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200640static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
641{
642 switch (device_baudrate) {
643 case 0x0252a00a:
644 return 230400;
645
646 case 0x05f75004:
647 return 921600;
648
649 case 0x00005004:
650 return 1000000;
651
652 case 0x04928002:
653 case 0x01128002:
654 return 1500000;
655
656 case 0x00005002:
657 return 2000000;
658
659 case 0x0000b001:
660 return 2500000;
661
662 case 0x04928001:
663 return 3000000;
664
665 case 0x052a6001:
666 return 3500000;
667
668 case 0x00005001:
669 return 4000000;
670
671 case 0x0252c014:
672 default:
673 return 115200;
674 }
675}
676
677int btrtl_get_uart_settings(struct hci_dev *hdev,
678 struct btrtl_device_info *btrtl_dev,
679 unsigned int *controller_baudrate,
680 u32 *device_baudrate, bool *flow_control)
681{
682 struct rtl_vendor_config *config;
683 struct rtl_vendor_config_entry *entry;
684 int i, total_data_len;
685 bool found = false;
686
687 total_data_len = btrtl_dev->cfg_len - sizeof(*config);
688 if (total_data_len <= 0) {
689 rtl_dev_warn(hdev, "no config loaded\n");
690 return -EINVAL;
691 }
692
693 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
694 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
695 rtl_dev_err(hdev, "invalid config magic\n");
696 return -EINVAL;
697 }
698
699 if (total_data_len < le16_to_cpu(config->total_len)) {
700 rtl_dev_err(hdev, "config is too short\n");
701 return -EINVAL;
702 }
703
704 for (i = 0; i < total_data_len; ) {
705 entry = ((void *)config->entry) + i;
706
707 switch (le16_to_cpu(entry->offset)) {
708 case 0xc:
709 if (entry->len < sizeof(*device_baudrate)) {
710 rtl_dev_err(hdev, "invalid UART config entry\n");
711 return -EINVAL;
712 }
713
714 *device_baudrate = get_unaligned_le32(entry->data);
715 *controller_baudrate = btrtl_convert_baudrate(
716 *device_baudrate);
717
718 if (entry->len >= 13)
719 *flow_control = !!(entry->data[12] & BIT(2));
720 else
721 *flow_control = false;
722
723 found = true;
724 break;
725
726 default:
727 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n",
728 le16_to_cpu(entry->offset), entry->len);
729 break;
730 };
731
732 i += sizeof(*entry) + entry->len;
733 }
734
735 if (!found) {
736 rtl_dev_err(hdev, "no UART config entry found\n");
737 return -ENOENT;
738 }
739
740 rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate);
741 rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate);
742 rtl_dev_dbg(hdev, "flow control %d\n", *flow_control);
743
744 return 0;
745}
746EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
747
Carlo Caionedb33c772015-05-14 10:49:09 +0200748MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
749MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
750MODULE_VERSION(VERSION);
751MODULE_LICENSE("GPL");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200752MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
753MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
754MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200755MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
756MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
757MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
758MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200759MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
760MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
761MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
762MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
763MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
764MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");