blob: c91bba00df4e494c4c963c9f4297c1f745211e50 [file] [log] [blame]
Carlo Caionedb33c772015-05-14 10:49:09 +02001/*
2 * Bluetooth support for Realtek devices
3 *
4 * Copyright (C) 2015 Endless Mobile, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/module.h>
19#include <linux/firmware.h>
20#include <asm/unaligned.h>
21#include <linux/usb.h>
22
23#include <net/bluetooth/bluetooth.h>
24#include <net/bluetooth/hci_core.h>
25
26#include "btrtl.h"
27
28#define VERSION "0.1"
29
30#define RTL_EPATCH_SIGNATURE "Realtech"
31#define RTL_ROM_LMP_3499 0x3499
32#define RTL_ROM_LMP_8723A 0x1200
33#define RTL_ROM_LMP_8723B 0x8723
34#define RTL_ROM_LMP_8821A 0x8821
35#define RTL_ROM_LMP_8761A 0x8761
Larry Finger1110a2d2016-09-09 10:02:05 -050036#define RTL_ROM_LMP_8822B 0x8822
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +020037#define RTL_CONFIG_MAGIC 0x8723ab55
Carlo Caionedb33c772015-05-14 10:49:09 +020038
Alex Lu907f8492018-02-11 12:24:33 -060039#define IC_MATCH_FL_LMPSUBV (1 << 0)
40#define IC_MATCH_FL_HCIREV (1 << 1)
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020041#define IC_MATCH_FL_HCIVER (1 << 2)
42#define IC_MATCH_FL_HCIBUS (1 << 3)
Alex Lu907f8492018-02-11 12:24:33 -060043#define IC_INFO(lmps, hcir) \
44 .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
45 .lmp_subver = (lmps), \
46 .hci_rev = (hcir)
47
48struct id_table {
49 __u16 match_flags;
50 __u16 lmp_subver;
51 __u16 hci_rev;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020052 __u8 hci_ver;
53 __u8 hci_bus;
Alex Lu907f8492018-02-11 12:24:33 -060054 bool config_needed;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020055 bool has_rom_version;
Alex Lu907f8492018-02-11 12:24:33 -060056 char *fw_name;
57 char *cfg_name;
58};
59
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020060struct btrtl_device_info {
61 const struct id_table *ic_info;
62 u8 rom_version;
63 u8 *fw_data;
64 int fw_len;
65 u8 *cfg_data;
66 int cfg_len;
67};
68
Alex Lu907f8492018-02-11 12:24:33 -060069static const struct id_table ic_id_table[] = {
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020070 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0,
71 .config_needed = false,
72 .has_rom_version = false,
73 .fw_name = "rtl_bt/rtl8723a_fw.bin",
74 .cfg_name = NULL },
75
76 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0,
77 .config_needed = false,
78 .has_rom_version = false,
79 .fw_name = "rtl_bt/rtl8723a_fw.bin",
80 .cfg_name = NULL },
81
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020082 /* 8723BS */
83 { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
84 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS,
85 .lmp_subver = RTL_ROM_LMP_8723B,
86 .hci_rev = 0xb,
87 .hci_ver = 6,
88 .hci_bus = HCI_UART,
89 .config_needed = true,
90 .has_rom_version = true,
91 .fw_name = "rtl_bt/rtl8723bs_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020092 .cfg_name = "rtl_bt/rtl8723bs_config" },
Martin Blumenstinglc50903e2018-08-02 16:57:16 +020093
Alex Lu907f8492018-02-11 12:24:33 -060094 /* 8723B */
95 { IC_INFO(RTL_ROM_LMP_8723B, 0xb),
96 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020097 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060098 .fw_name = "rtl_bt/rtl8723b_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +020099 .cfg_name = "rtl_bt/rtl8723b_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600100
101 /* 8723D */
102 { IC_INFO(RTL_ROM_LMP_8723B, 0xd),
103 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200104 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600105 .fw_name = "rtl_bt/rtl8723d_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200106 .cfg_name = "rtl_bt/rtl8723d_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600107
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200108 /* 8723DS */
109 { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
110 IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS,
111 .lmp_subver = RTL_ROM_LMP_8723B,
112 .hci_rev = 0xd,
113 .hci_ver = 8,
114 .hci_bus = HCI_UART,
115 .config_needed = true,
116 .has_rom_version = true,
117 .fw_name = "rtl_bt/rtl8723ds_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200118 .cfg_name = "rtl_bt/rtl8723ds_config" },
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200119
Alex Lu907f8492018-02-11 12:24:33 -0600120 /* 8821A */
121 { IC_INFO(RTL_ROM_LMP_8821A, 0xa),
122 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200123 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600124 .fw_name = "rtl_bt/rtl8821a_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200125 .cfg_name = "rtl_bt/rtl8821a_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600126
127 /* 8821C */
128 { IC_INFO(RTL_ROM_LMP_8821A, 0xc),
129 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200130 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600131 .fw_name = "rtl_bt/rtl8821c_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200132 .cfg_name = "rtl_bt/rtl8821c_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600133
134 /* 8761A */
135 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0,
136 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200137 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600138 .fw_name = "rtl_bt/rtl8761a_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200139 .cfg_name = "rtl_bt/rtl8761a_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600140
Alex Lu8ecfdc92018-09-08 12:05:59 -0500141 /* 8822C with USB interface */
142 { IC_INFO(RTL_ROM_LMP_8822B, 0xc),
143 .config_needed = false,
144 .has_rom_version = true,
145 .fw_name = "rtl_bt/rtl8822cu_fw.bin",
146 .cfg_name = "rtl_bt/rtl8822cu_config" },
147
Alex Lu907f8492018-02-11 12:24:33 -0600148 /* 8822B */
149 { IC_INFO(RTL_ROM_LMP_8822B, 0xb),
150 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200151 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600152 .fw_name = "rtl_bt/rtl8822b_fw.bin",
Hans de Goede1cc194c2018-08-02 16:57:17 +0200153 .cfg_name = "rtl_bt/rtl8822b_config" },
Alex Lu907f8492018-02-11 12:24:33 -0600154 };
155
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200156static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
157 u8 hci_ver, u8 hci_bus)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200158{
159 int i;
160
161 for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
162 if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
163 (ic_id_table[i].lmp_subver != lmp_subver))
164 continue;
165 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
166 (ic_id_table[i].hci_rev != hci_rev))
167 continue;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200168 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
169 (ic_id_table[i].hci_ver != hci_ver))
170 continue;
171 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
172 (ic_id_table[i].hci_bus != hci_bus))
173 continue;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200174
175 break;
176 }
177 if (i >= ARRAY_SIZE(ic_id_table))
178 return NULL;
179
180 return &ic_id_table[i];
181}
182
Carlo Caionedb33c772015-05-14 10:49:09 +0200183static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
184{
185 struct rtl_rom_version_evt *rom_version;
186 struct sk_buff *skb;
187
188 /* Read RTL ROM version command */
189 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
190 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200191 rtl_dev_err(hdev, "Read ROM version failed (%ld)\n",
192 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200193 return PTR_ERR(skb);
194 }
195
196 if (skb->len != sizeof(*rom_version)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200197 rtl_dev_err(hdev, "RTL version event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200198 kfree_skb(skb);
199 return -EIO;
200 }
201
202 rom_version = (struct rtl_rom_version_evt *)skb->data;
Hans de Goedea5c76e62018-08-02 16:57:14 +0200203 rtl_dev_info(hdev, "rom_version status=%x version=%x\n",
204 rom_version->status, rom_version->version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200205
206 *version = rom_version->version;
207
208 kfree_skb(skb);
209 return 0;
210}
211
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200212static int rtlbt_parse_firmware(struct hci_dev *hdev,
213 struct btrtl_device_info *btrtl_dev,
Alex Lu907f8492018-02-11 12:24:33 -0600214 unsigned char **_buf)
Carlo Caionedb33c772015-05-14 10:49:09 +0200215{
Colin Ian Kinge5070e072018-09-26 09:32:08 +0100216 static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
Carlo Caionedb33c772015-05-14 10:49:09 +0200217 struct rtl_epatch_header *epatch_info;
218 unsigned char *buf;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200219 int i, len;
Carlo Caionedb33c772015-05-14 10:49:09 +0200220 size_t min_size;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200221 u8 opcode, length, data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200222 int project_id = -1;
223 const unsigned char *fwptr, *chip_id_base;
224 const unsigned char *patch_length_base, *patch_offset_base;
225 u32 patch_offset = 0;
226 u16 patch_length, num_patches;
Larry Finger1110a2d2016-09-09 10:02:05 -0500227 static const struct {
228 __u16 lmp_subver;
229 __u8 id;
230 } project_id_to_lmp_subver[] = {
231 { RTL_ROM_LMP_8723A, 0 },
232 { RTL_ROM_LMP_8723B, 1 },
233 { RTL_ROM_LMP_8821A, 2 },
234 { RTL_ROM_LMP_8761A, 3 },
235 { RTL_ROM_LMP_8822B, 8 },
Alex Lu907f8492018-02-11 12:24:33 -0600236 { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
237 { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
Alex Lu8ecfdc92018-09-08 12:05:59 -0500238 { RTL_ROM_LMP_8822B, 13 }, /* 8822C */
Carlo Caionedb33c772015-05-14 10:49:09 +0200239 };
240
Carlo Caionedb33c772015-05-14 10:49:09 +0200241 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200242 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200243 return -EINVAL;
244
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200245 fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
Carlo Caionedb33c772015-05-14 10:49:09 +0200246 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200247 rtl_dev_err(hdev, "extension section signature mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200248 return -EINVAL;
249 }
250
251 /* Loop from the end of the firmware parsing instructions, until
252 * we find an instruction that identifies the "project ID" for the
253 * hardware supported by this firwmare file.
254 * Once we have that, we double-check that that project_id is suitable
255 * for the hardware we are working with.
256 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200257 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200258 opcode = *--fwptr;
259 length = *--fwptr;
260 data = *--fwptr;
261
262 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
263
264 if (opcode == 0xff) /* EOF */
265 break;
266
267 if (length == 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200268 rtl_dev_err(hdev, "found instruction with length 0\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200269 return -EINVAL;
270 }
271
272 if (opcode == 0 && length == 1) {
273 project_id = data;
274 break;
275 }
276
277 fwptr -= length;
278 }
279
280 if (project_id < 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200281 rtl_dev_err(hdev, "failed to find version instruction\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200282 return -EINVAL;
283 }
284
Larry Finger1110a2d2016-09-09 10:02:05 -0500285 /* Find project_id in table */
286 for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
287 if (project_id == project_id_to_lmp_subver[i].id)
288 break;
289 }
290
291 if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200292 rtl_dev_err(hdev, "unknown project id %d\n", project_id);
Carlo Caionedb33c772015-05-14 10:49:09 +0200293 return -EINVAL;
294 }
295
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200296 if (btrtl_dev->ic_info->lmp_subver !=
297 project_id_to_lmp_subver[i].lmp_subver) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200298 rtl_dev_err(hdev, "firmware is for %x but this is a %x\n",
299 project_id_to_lmp_subver[i].lmp_subver,
300 btrtl_dev->ic_info->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200301 return -EINVAL;
302 }
303
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200304 epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200305 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200306 rtl_dev_err(hdev, "bad EPATCH signature\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200307 return -EINVAL;
308 }
309
310 num_patches = le16_to_cpu(epatch_info->num_patches);
311 BT_DBG("fw_version=%x, num_patches=%d",
312 le32_to_cpu(epatch_info->fw_version), num_patches);
313
314 /* After the rtl_epatch_header there is a funky patch metadata section.
315 * Assuming 2 patches, the layout is:
316 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
317 *
318 * Find the right patch for this chip.
319 */
320 min_size += 8 * num_patches;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200321 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200322 return -EINVAL;
323
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200324 chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
Carlo Caionedb33c772015-05-14 10:49:09 +0200325 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
326 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
327 for (i = 0; i < num_patches; i++) {
328 u16 chip_id = get_unaligned_le16(chip_id_base +
329 (i * sizeof(u16)));
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200330 if (chip_id == btrtl_dev->rom_version + 1) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200331 patch_length = get_unaligned_le16(patch_length_base +
332 (i * sizeof(u16)));
333 patch_offset = get_unaligned_le32(patch_offset_base +
334 (i * sizeof(u32)));
335 break;
336 }
337 }
338
339 if (!patch_offset) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200340 rtl_dev_err(hdev, "didn't find patch for chip id %d",
341 btrtl_dev->rom_version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200342 return -EINVAL;
343 }
344
345 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
346 min_size = patch_offset + patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200347 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200348 return -EINVAL;
349
350 /* Copy the firmware into a new buffer and write the version at
351 * the end.
352 */
353 len = patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200354 buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length,
355 GFP_KERNEL);
Carlo Caionedb33c772015-05-14 10:49:09 +0200356 if (!buf)
357 return -ENOMEM;
358
359 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
360
361 *_buf = buf;
362 return len;
363}
364
365static int rtl_download_firmware(struct hci_dev *hdev,
366 const unsigned char *data, int fw_len)
367{
368 struct rtl_download_cmd *dl_cmd;
369 int frag_num = fw_len / RTL_FRAG_LEN + 1;
370 int frag_len = RTL_FRAG_LEN;
371 int ret = 0;
372 int i;
373
374 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
375 if (!dl_cmd)
376 return -ENOMEM;
377
378 for (i = 0; i < frag_num; i++) {
379 struct sk_buff *skb;
380
381 BT_DBG("download fw (%d/%d)", i, frag_num);
382
383 dl_cmd->index = i;
384 if (i == (frag_num - 1)) {
385 dl_cmd->index |= 0x80; /* data end */
386 frag_len = fw_len % RTL_FRAG_LEN;
387 }
388 memcpy(dl_cmd->data, data, frag_len);
389
390 /* Send download command */
391 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
392 HCI_INIT_TIMEOUT);
393 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200394 rtl_dev_err(hdev, "download fw command failed (%ld)\n",
395 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200396 ret = -PTR_ERR(skb);
397 goto out;
398 }
399
400 if (skb->len != sizeof(struct rtl_download_response)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200401 rtl_dev_err(hdev, "download fw event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200402 kfree_skb(skb);
403 ret = -EIO;
404 goto out;
405 }
406
407 kfree_skb(skb);
408 data += RTL_FRAG_LEN;
409 }
410
411out:
412 kfree(dl_cmd);
413 return ret;
414}
415
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200416static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
Larry Finger1110a2d2016-09-09 10:02:05 -0500417{
418 const struct firmware *fw;
419 int ret;
420
Hans de Goedea5c76e62018-08-02 16:57:14 +0200421 rtl_dev_info(hdev, "rtl: loading %s\n", name);
Larry Finger1110a2d2016-09-09 10:02:05 -0500422 ret = request_firmware(&fw, name, &hdev->dev);
Larry Fingerabed84a2017-02-19 20:04:57 -0600423 if (ret < 0)
Larry Finger1110a2d2016-09-09 10:02:05 -0500424 return ret;
Larry Finger1110a2d2016-09-09 10:02:05 -0500425 ret = fw->size;
426 *buff = kmemdup(fw->data, ret, GFP_KERNEL);
Dan Carpenterc3327bd2017-07-28 17:41:11 +0300427 if (!*buff)
428 ret = -ENOMEM;
Larry Finger1110a2d2016-09-09 10:02:05 -0500429
430 release_firmware(fw);
431
432 return ret;
433}
434
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200435static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
436 struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200437{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200438 if (btrtl_dev->fw_len < 8)
439 return -EINVAL;
Carlo Caionedb33c772015-05-14 10:49:09 +0200440
441 /* Check that the firmware doesn't have the epatch signature
442 * (which is only for RTL8723B and newer).
443 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200444 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200445 rtl_dev_err(hdev, "unexpected EPATCH signature!\n");
Alex Lu907f8492018-02-11 12:24:33 -0600446 return -EINVAL;
447 }
448
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200449 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
450 btrtl_dev->fw_len);
451}
Alex Lu907f8492018-02-11 12:24:33 -0600452
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200453static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
454 struct btrtl_device_info *btrtl_dev)
455{
456 unsigned char *fw_data = NULL;
457 int ret;
458 u8 *tbuff;
Carlo Caionedb33c772015-05-14 10:49:09 +0200459
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200460 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200461 if (ret < 0)
462 goto out;
463
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200464 if (btrtl_dev->cfg_len > 0) {
465 tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
Larry Finger1110a2d2016-09-09 10:02:05 -0500466 if (!tbuff) {
467 ret = -ENOMEM;
468 goto out;
469 }
470
471 memcpy(tbuff, fw_data, ret);
472 kfree(fw_data);
473
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200474 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
475 ret += btrtl_dev->cfg_len;
Larry Finger1110a2d2016-09-09 10:02:05 -0500476
477 fw_data = tbuff;
478 }
479
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200480 rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
Larry Finger1110a2d2016-09-09 10:02:05 -0500481
Carlo Caionedb33c772015-05-14 10:49:09 +0200482 ret = rtl_download_firmware(hdev, fw_data, ret);
Carlo Caionedb33c772015-05-14 10:49:09 +0200483
484out:
Larry Finger1110a2d2016-09-09 10:02:05 -0500485 kfree(fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200486 return ret;
487}
488
489static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
490{
491 struct sk_buff *skb;
492
493 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
494 HCI_INIT_TIMEOUT);
495 if (IS_ERR(skb)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200496 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
497 PTR_ERR(skb));
Carlo Caionedb33c772015-05-14 10:49:09 +0200498 return skb;
499 }
500
501 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Hans de Goedea5c76e62018-08-02 16:57:14 +0200502 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200503 kfree_skb(skb);
504 return ERR_PTR(-EIO);
505 }
506
507 return skb;
508}
509
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200510void btrtl_free(struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200511{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200512 kfree(btrtl_dev->fw_data);
513 kfree(btrtl_dev->cfg_data);
514 kfree(btrtl_dev);
515}
516EXPORT_SYMBOL_GPL(btrtl_free);
517
Hans de Goede1cc194c2018-08-02 16:57:17 +0200518struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
519 const char *postfix)
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200520{
521 struct btrtl_device_info *btrtl_dev;
Carlo Caionedb33c772015-05-14 10:49:09 +0200522 struct sk_buff *skb;
523 struct hci_rp_read_local_version *resp;
Hans de Goede1cc194c2018-08-02 16:57:17 +0200524 char cfg_name[40];
Alex Lu907f8492018-02-11 12:24:33 -0600525 u16 hci_rev, lmp_subver;
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200526 u8 hci_ver;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200527 int ret;
528
529 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
530 if (!btrtl_dev) {
531 ret = -ENOMEM;
532 goto err_alloc;
533 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200534
535 skb = btrtl_read_local_version(hdev);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200536 if (IS_ERR(skb)) {
537 ret = PTR_ERR(skb);
538 goto err_free;
539 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200540
541 resp = (struct hci_rp_read_local_version *)skb->data;
Hans de Goedea5c76e62018-08-02 16:57:14 +0200542 rtl_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
543 resp->hci_ver, resp->hci_rev,
544 resp->lmp_ver, resp->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200545
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200546 hci_ver = resp->hci_ver;
Alex Lu907f8492018-02-11 12:24:33 -0600547 hci_rev = le16_to_cpu(resp->hci_rev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200548 lmp_subver = le16_to_cpu(resp->lmp_subver);
549 kfree_skb(skb);
550
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200551 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
552 hdev->bus);
553
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200554 if (!btrtl_dev->ic_info) {
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800555 rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200556 lmp_subver, hci_rev, hci_ver);
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800557 return btrtl_dev;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200558 }
559
560 if (btrtl_dev->ic_info->has_rom_version) {
561 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
562 if (ret)
563 goto err_free;
564 }
565
566 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
567 &btrtl_dev->fw_data);
568 if (btrtl_dev->fw_len < 0) {
569 rtl_dev_err(hdev, "firmware file %s not found\n",
570 btrtl_dev->ic_info->fw_name);
571 ret = btrtl_dev->fw_len;
572 goto err_free;
573 }
574
575 if (btrtl_dev->ic_info->cfg_name) {
Hans de Goede1cc194c2018-08-02 16:57:17 +0200576 if (postfix) {
577 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
578 btrtl_dev->ic_info->cfg_name, postfix);
579 } else {
580 snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
581 btrtl_dev->ic_info->cfg_name);
582 }
583 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200584 &btrtl_dev->cfg_data);
585 if (btrtl_dev->ic_info->config_needed &&
586 btrtl_dev->cfg_len <= 0) {
587 rtl_dev_err(hdev, "mandatory config file %s not found\n",
588 btrtl_dev->ic_info->cfg_name);
589 ret = btrtl_dev->cfg_len;
590 goto err_free;
591 }
592 }
593
594 return btrtl_dev;
595
596err_free:
597 btrtl_free(btrtl_dev);
598err_alloc:
599 return ERR_PTR(ret);
600}
601EXPORT_SYMBOL_GPL(btrtl_initialize);
602
603int btrtl_download_firmware(struct hci_dev *hdev,
604 struct btrtl_device_info *btrtl_dev)
605{
Carlo Caionedb33c772015-05-14 10:49:09 +0200606 /* Match a set of subver values that correspond to stock firmware,
607 * which is not compatible with standard btusb.
608 * If matched, upload an alternative firmware that does conform to
609 * standard btusb. Once that firmware is uploaded, the subver changes
610 * to a different value.
611 */
Kai-Heng Feng00df2142019-01-27 16:33:59 +0800612 if (!btrtl_dev->ic_info) {
613 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
614 return 0;
615 }
616
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200617 switch (btrtl_dev->ic_info->lmp_subver) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200618 case RTL_ROM_LMP_8723A:
619 case RTL_ROM_LMP_3499:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200620 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200621 case RTL_ROM_LMP_8723B:
Carlo Caionedb33c772015-05-14 10:49:09 +0200622 case RTL_ROM_LMP_8821A:
Carlo Caionedb33c772015-05-14 10:49:09 +0200623 case RTL_ROM_LMP_8761A:
Larry Finger1110a2d2016-09-09 10:02:05 -0500624 case RTL_ROM_LMP_8822B:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200625 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200626 default:
Hans de Goedea5c76e62018-08-02 16:57:14 +0200627 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
Carlo Caionedb33c772015-05-14 10:49:09 +0200628 return 0;
629 }
630}
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200631EXPORT_SYMBOL_GPL(btrtl_download_firmware);
632
633int btrtl_setup_realtek(struct hci_dev *hdev)
634{
635 struct btrtl_device_info *btrtl_dev;
636 int ret;
637
Hans de Goede1cc194c2018-08-02 16:57:17 +0200638 btrtl_dev = btrtl_initialize(hdev, NULL);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200639 if (IS_ERR(btrtl_dev))
640 return PTR_ERR(btrtl_dev);
641
642 ret = btrtl_download_firmware(hdev, btrtl_dev);
643
644 btrtl_free(btrtl_dev);
645
646 return ret;
647}
Carlo Caionedb33c772015-05-14 10:49:09 +0200648EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
649
Martin Blumenstinglb85b0ee2018-08-02 16:57:15 +0200650static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
651{
652 switch (device_baudrate) {
653 case 0x0252a00a:
654 return 230400;
655
656 case 0x05f75004:
657 return 921600;
658
659 case 0x00005004:
660 return 1000000;
661
662 case 0x04928002:
663 case 0x01128002:
664 return 1500000;
665
666 case 0x00005002:
667 return 2000000;
668
669 case 0x0000b001:
670 return 2500000;
671
672 case 0x04928001:
673 return 3000000;
674
675 case 0x052a6001:
676 return 3500000;
677
678 case 0x00005001:
679 return 4000000;
680
681 case 0x0252c014:
682 default:
683 return 115200;
684 }
685}
686
687int btrtl_get_uart_settings(struct hci_dev *hdev,
688 struct btrtl_device_info *btrtl_dev,
689 unsigned int *controller_baudrate,
690 u32 *device_baudrate, bool *flow_control)
691{
692 struct rtl_vendor_config *config;
693 struct rtl_vendor_config_entry *entry;
694 int i, total_data_len;
695 bool found = false;
696
697 total_data_len = btrtl_dev->cfg_len - sizeof(*config);
698 if (total_data_len <= 0) {
699 rtl_dev_warn(hdev, "no config loaded\n");
700 return -EINVAL;
701 }
702
703 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
704 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
705 rtl_dev_err(hdev, "invalid config magic\n");
706 return -EINVAL;
707 }
708
709 if (total_data_len < le16_to_cpu(config->total_len)) {
710 rtl_dev_err(hdev, "config is too short\n");
711 return -EINVAL;
712 }
713
714 for (i = 0; i < total_data_len; ) {
715 entry = ((void *)config->entry) + i;
716
717 switch (le16_to_cpu(entry->offset)) {
718 case 0xc:
719 if (entry->len < sizeof(*device_baudrate)) {
720 rtl_dev_err(hdev, "invalid UART config entry\n");
721 return -EINVAL;
722 }
723
724 *device_baudrate = get_unaligned_le32(entry->data);
725 *controller_baudrate = btrtl_convert_baudrate(
726 *device_baudrate);
727
728 if (entry->len >= 13)
729 *flow_control = !!(entry->data[12] & BIT(2));
730 else
731 *flow_control = false;
732
733 found = true;
734 break;
735
736 default:
737 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n",
738 le16_to_cpu(entry->offset), entry->len);
739 break;
740 };
741
742 i += sizeof(*entry) + entry->len;
743 }
744
745 if (!found) {
746 rtl_dev_err(hdev, "no UART config entry found\n");
747 return -ENOENT;
748 }
749
750 rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate);
751 rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate);
752 rtl_dev_dbg(hdev, "flow control %d\n", *flow_control);
753
754 return 0;
755}
756EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
757
Carlo Caionedb33c772015-05-14 10:49:09 +0200758MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
759MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
760MODULE_VERSION(VERSION);
761MODULE_LICENSE("GPL");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200762MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
763MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
764MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
Martin Blumenstinglc50903e2018-08-02 16:57:16 +0200765MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
766MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
767MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
768MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200769MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
770MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
771MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
772MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
773MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
774MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");