blob: bb6c138b47cccc883b0e0ae21081e4394c8e1427 [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
Carlo Caionedb33c772015-05-14 10:49:09 +020037
Alex Lu907f8492018-02-11 12:24:33 -060038#define IC_MATCH_FL_LMPSUBV (1 << 0)
39#define IC_MATCH_FL_HCIREV (1 << 1)
40#define IC_INFO(lmps, hcir) \
41 .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
42 .lmp_subver = (lmps), \
43 .hci_rev = (hcir)
44
45struct id_table {
46 __u16 match_flags;
47 __u16 lmp_subver;
48 __u16 hci_rev;
49 bool config_needed;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020050 bool has_rom_version;
Alex Lu907f8492018-02-11 12:24:33 -060051 char *fw_name;
52 char *cfg_name;
53};
54
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020055struct btrtl_device_info {
56 const struct id_table *ic_info;
57 u8 rom_version;
58 u8 *fw_data;
59 int fw_len;
60 u8 *cfg_data;
61 int cfg_len;
62};
63
Alex Lu907f8492018-02-11 12:24:33 -060064static const struct id_table ic_id_table[] = {
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020065 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0,
66 .config_needed = false,
67 .has_rom_version = false,
68 .fw_name = "rtl_bt/rtl8723a_fw.bin",
69 .cfg_name = NULL },
70
71 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0,
72 .config_needed = false,
73 .has_rom_version = false,
74 .fw_name = "rtl_bt/rtl8723a_fw.bin",
75 .cfg_name = NULL },
76
Alex Lu907f8492018-02-11 12:24:33 -060077 /* 8723B */
78 { IC_INFO(RTL_ROM_LMP_8723B, 0xb),
79 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020080 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060081 .fw_name = "rtl_bt/rtl8723b_fw.bin",
82 .cfg_name = "rtl_bt/rtl8723b_config.bin" },
83
84 /* 8723D */
85 { IC_INFO(RTL_ROM_LMP_8723B, 0xd),
86 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020087 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060088 .fw_name = "rtl_bt/rtl8723d_fw.bin",
89 .cfg_name = "rtl_bt/rtl8723d_config.bin" },
90
91 /* 8821A */
92 { IC_INFO(RTL_ROM_LMP_8821A, 0xa),
93 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +020094 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -060095 .fw_name = "rtl_bt/rtl8821a_fw.bin",
96 .cfg_name = "rtl_bt/rtl8821a_config.bin" },
97
98 /* 8821C */
99 { IC_INFO(RTL_ROM_LMP_8821A, 0xc),
100 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200101 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600102 .fw_name = "rtl_bt/rtl8821c_fw.bin",
103 .cfg_name = "rtl_bt/rtl8821c_config.bin" },
104
105 /* 8761A */
106 { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0,
107 .config_needed = false,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200108 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600109 .fw_name = "rtl_bt/rtl8761a_fw.bin",
110 .cfg_name = "rtl_bt/rtl8761a_config.bin" },
111
112 /* 8822B */
113 { IC_INFO(RTL_ROM_LMP_8822B, 0xb),
114 .config_needed = true,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200115 .has_rom_version = true,
Alex Lu907f8492018-02-11 12:24:33 -0600116 .fw_name = "rtl_bt/rtl8822b_fw.bin",
117 .cfg_name = "rtl_bt/rtl8822b_config.bin" },
118 };
119
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200120static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev)
121{
122 int i;
123
124 for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
125 if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
126 (ic_id_table[i].lmp_subver != lmp_subver))
127 continue;
128 if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
129 (ic_id_table[i].hci_rev != hci_rev))
130 continue;
131
132 break;
133 }
134 if (i >= ARRAY_SIZE(ic_id_table))
135 return NULL;
136
137 return &ic_id_table[i];
138}
139
Carlo Caionedb33c772015-05-14 10:49:09 +0200140static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
141{
142 struct rtl_rom_version_evt *rom_version;
143 struct sk_buff *skb;
144
145 /* Read RTL ROM version command */
146 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
147 if (IS_ERR(skb)) {
148 BT_ERR("%s: Read ROM version failed (%ld)",
149 hdev->name, PTR_ERR(skb));
150 return PTR_ERR(skb);
151 }
152
153 if (skb->len != sizeof(*rom_version)) {
154 BT_ERR("%s: RTL version event length mismatch", hdev->name);
155 kfree_skb(skb);
156 return -EIO;
157 }
158
159 rom_version = (struct rtl_rom_version_evt *)skb->data;
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100160 bt_dev_info(hdev, "rom_version status=%x version=%x",
161 rom_version->status, rom_version->version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200162
163 *version = rom_version->version;
164
165 kfree_skb(skb);
166 return 0;
167}
168
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200169static int rtlbt_parse_firmware(struct hci_dev *hdev,
170 struct btrtl_device_info *btrtl_dev,
Alex Lu907f8492018-02-11 12:24:33 -0600171 unsigned char **_buf)
Carlo Caionedb33c772015-05-14 10:49:09 +0200172{
173 const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
174 struct rtl_epatch_header *epatch_info;
175 unsigned char *buf;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200176 int i, len;
Carlo Caionedb33c772015-05-14 10:49:09 +0200177 size_t min_size;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200178 u8 opcode, length, data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200179 int project_id = -1;
180 const unsigned char *fwptr, *chip_id_base;
181 const unsigned char *patch_length_base, *patch_offset_base;
182 u32 patch_offset = 0;
183 u16 patch_length, num_patches;
Larry Finger1110a2d2016-09-09 10:02:05 -0500184 static const struct {
185 __u16 lmp_subver;
186 __u8 id;
187 } project_id_to_lmp_subver[] = {
188 { RTL_ROM_LMP_8723A, 0 },
189 { RTL_ROM_LMP_8723B, 1 },
190 { RTL_ROM_LMP_8821A, 2 },
191 { RTL_ROM_LMP_8761A, 3 },
192 { RTL_ROM_LMP_8822B, 8 },
Alex Lu907f8492018-02-11 12:24:33 -0600193 { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
194 { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
Carlo Caionedb33c772015-05-14 10:49:09 +0200195 };
196
Carlo Caionedb33c772015-05-14 10:49:09 +0200197 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200198 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200199 return -EINVAL;
200
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200201 fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
Carlo Caionedb33c772015-05-14 10:49:09 +0200202 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
203 BT_ERR("%s: extension section signature mismatch", hdev->name);
204 return -EINVAL;
205 }
206
207 /* Loop from the end of the firmware parsing instructions, until
208 * we find an instruction that identifies the "project ID" for the
209 * hardware supported by this firwmare file.
210 * Once we have that, we double-check that that project_id is suitable
211 * for the hardware we are working with.
212 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200213 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200214 opcode = *--fwptr;
215 length = *--fwptr;
216 data = *--fwptr;
217
218 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
219
220 if (opcode == 0xff) /* EOF */
221 break;
222
223 if (length == 0) {
224 BT_ERR("%s: found instruction with length 0",
225 hdev->name);
226 return -EINVAL;
227 }
228
229 if (opcode == 0 && length == 1) {
230 project_id = data;
231 break;
232 }
233
234 fwptr -= length;
235 }
236
237 if (project_id < 0) {
238 BT_ERR("%s: failed to find version instruction", hdev->name);
239 return -EINVAL;
240 }
241
Larry Finger1110a2d2016-09-09 10:02:05 -0500242 /* Find project_id in table */
243 for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
244 if (project_id == project_id_to_lmp_subver[i].id)
245 break;
246 }
247
248 if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200249 BT_ERR("%s: unknown project id %d", hdev->name, project_id);
250 return -EINVAL;
251 }
252
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200253 if (btrtl_dev->ic_info->lmp_subver !=
254 project_id_to_lmp_subver[i].lmp_subver) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200255 BT_ERR("%s: firmware is for %x but this is a %x", hdev->name,
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200256 project_id_to_lmp_subver[i].lmp_subver,
257 btrtl_dev->ic_info->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200258 return -EINVAL;
259 }
260
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200261 epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
Carlo Caionedb33c772015-05-14 10:49:09 +0200262 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
263 BT_ERR("%s: bad EPATCH signature", hdev->name);
264 return -EINVAL;
265 }
266
267 num_patches = le16_to_cpu(epatch_info->num_patches);
268 BT_DBG("fw_version=%x, num_patches=%d",
269 le32_to_cpu(epatch_info->fw_version), num_patches);
270
271 /* After the rtl_epatch_header there is a funky patch metadata section.
272 * Assuming 2 patches, the layout is:
273 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
274 *
275 * Find the right patch for this chip.
276 */
277 min_size += 8 * num_patches;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200278 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200279 return -EINVAL;
280
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200281 chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
Carlo Caionedb33c772015-05-14 10:49:09 +0200282 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
283 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
284 for (i = 0; i < num_patches; i++) {
285 u16 chip_id = get_unaligned_le16(chip_id_base +
286 (i * sizeof(u16)));
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200287 if (chip_id == btrtl_dev->rom_version + 1) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200288 patch_length = get_unaligned_le16(patch_length_base +
289 (i * sizeof(u16)));
290 patch_offset = get_unaligned_le32(patch_offset_base +
291 (i * sizeof(u32)));
292 break;
293 }
294 }
295
296 if (!patch_offset) {
297 BT_ERR("%s: didn't find patch for chip id %d",
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200298 hdev->name, btrtl_dev->rom_version);
Carlo Caionedb33c772015-05-14 10:49:09 +0200299 return -EINVAL;
300 }
301
302 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
303 min_size = patch_offset + patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200304 if (btrtl_dev->fw_len < min_size)
Carlo Caionedb33c772015-05-14 10:49:09 +0200305 return -EINVAL;
306
307 /* Copy the firmware into a new buffer and write the version at
308 * the end.
309 */
310 len = patch_length;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200311 buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length,
312 GFP_KERNEL);
Carlo Caionedb33c772015-05-14 10:49:09 +0200313 if (!buf)
314 return -ENOMEM;
315
316 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
317
318 *_buf = buf;
319 return len;
320}
321
322static int rtl_download_firmware(struct hci_dev *hdev,
323 const unsigned char *data, int fw_len)
324{
325 struct rtl_download_cmd *dl_cmd;
326 int frag_num = fw_len / RTL_FRAG_LEN + 1;
327 int frag_len = RTL_FRAG_LEN;
328 int ret = 0;
329 int i;
330
331 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
332 if (!dl_cmd)
333 return -ENOMEM;
334
335 for (i = 0; i < frag_num; i++) {
336 struct sk_buff *skb;
337
338 BT_DBG("download fw (%d/%d)", i, frag_num);
339
340 dl_cmd->index = i;
341 if (i == (frag_num - 1)) {
342 dl_cmd->index |= 0x80; /* data end */
343 frag_len = fw_len % RTL_FRAG_LEN;
344 }
345 memcpy(dl_cmd->data, data, frag_len);
346
347 /* Send download command */
348 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
349 HCI_INIT_TIMEOUT);
350 if (IS_ERR(skb)) {
351 BT_ERR("%s: download fw command failed (%ld)",
352 hdev->name, PTR_ERR(skb));
353 ret = -PTR_ERR(skb);
354 goto out;
355 }
356
357 if (skb->len != sizeof(struct rtl_download_response)) {
358 BT_ERR("%s: download fw event length mismatch",
359 hdev->name);
360 kfree_skb(skb);
361 ret = -EIO;
362 goto out;
363 }
364
365 kfree_skb(skb);
366 data += RTL_FRAG_LEN;
367 }
368
369out:
370 kfree(dl_cmd);
371 return ret;
372}
373
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200374static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
Larry Finger1110a2d2016-09-09 10:02:05 -0500375{
376 const struct firmware *fw;
377 int ret;
378
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100379 bt_dev_info(hdev, "rtl: loading %s", name);
Larry Finger1110a2d2016-09-09 10:02:05 -0500380 ret = request_firmware(&fw, name, &hdev->dev);
Larry Fingerabed84a2017-02-19 20:04:57 -0600381 if (ret < 0)
Larry Finger1110a2d2016-09-09 10:02:05 -0500382 return ret;
Larry Finger1110a2d2016-09-09 10:02:05 -0500383 ret = fw->size;
384 *buff = kmemdup(fw->data, ret, GFP_KERNEL);
Dan Carpenterc3327bd2017-07-28 17:41:11 +0300385 if (!*buff)
386 ret = -ENOMEM;
Larry Finger1110a2d2016-09-09 10:02:05 -0500387
388 release_firmware(fw);
389
390 return ret;
391}
392
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200393static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
394 struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200395{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200396 if (btrtl_dev->fw_len < 8)
397 return -EINVAL;
Carlo Caionedb33c772015-05-14 10:49:09 +0200398
399 /* Check that the firmware doesn't have the epatch signature
400 * (which is only for RTL8723B and newer).
401 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200402 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200403 BT_ERR("%s: unexpected EPATCH signature!", hdev->name);
Alex Lu907f8492018-02-11 12:24:33 -0600404 return -EINVAL;
405 }
406
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200407 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
408 btrtl_dev->fw_len);
409}
Alex Lu907f8492018-02-11 12:24:33 -0600410
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200411static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
412 struct btrtl_device_info *btrtl_dev)
413{
414 unsigned char *fw_data = NULL;
415 int ret;
416 u8 *tbuff;
Carlo Caionedb33c772015-05-14 10:49:09 +0200417
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200418 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200419 if (ret < 0)
420 goto out;
421
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200422 if (btrtl_dev->cfg_len > 0) {
423 tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
Larry Finger1110a2d2016-09-09 10:02:05 -0500424 if (!tbuff) {
425 ret = -ENOMEM;
426 goto out;
427 }
428
429 memcpy(tbuff, fw_data, ret);
430 kfree(fw_data);
431
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200432 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
433 ret += btrtl_dev->cfg_len;
Larry Finger1110a2d2016-09-09 10:02:05 -0500434
435 fw_data = tbuff;
436 }
437
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200438 rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
Larry Finger1110a2d2016-09-09 10:02:05 -0500439
Carlo Caionedb33c772015-05-14 10:49:09 +0200440 ret = rtl_download_firmware(hdev, fw_data, ret);
Carlo Caionedb33c772015-05-14 10:49:09 +0200441
442out:
Larry Finger1110a2d2016-09-09 10:02:05 -0500443 kfree(fw_data);
Carlo Caionedb33c772015-05-14 10:49:09 +0200444 return ret;
445}
446
447static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
448{
449 struct sk_buff *skb;
450
451 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
452 HCI_INIT_TIMEOUT);
453 if (IS_ERR(skb)) {
454 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
455 hdev->name, PTR_ERR(skb));
456 return skb;
457 }
458
459 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
460 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
461 hdev->name);
462 kfree_skb(skb);
463 return ERR_PTR(-EIO);
464 }
465
466 return skb;
467}
468
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200469void btrtl_free(struct btrtl_device_info *btrtl_dev)
Carlo Caionedb33c772015-05-14 10:49:09 +0200470{
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200471 kfree(btrtl_dev->fw_data);
472 kfree(btrtl_dev->cfg_data);
473 kfree(btrtl_dev);
474}
475EXPORT_SYMBOL_GPL(btrtl_free);
476
477struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev)
478{
479 struct btrtl_device_info *btrtl_dev;
Carlo Caionedb33c772015-05-14 10:49:09 +0200480 struct sk_buff *skb;
481 struct hci_rp_read_local_version *resp;
Alex Lu907f8492018-02-11 12:24:33 -0600482 u16 hci_rev, lmp_subver;
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200483 int ret;
484
485 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
486 if (!btrtl_dev) {
487 ret = -ENOMEM;
488 goto err_alloc;
489 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200490
491 skb = btrtl_read_local_version(hdev);
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200492 if (IS_ERR(skb)) {
493 ret = PTR_ERR(skb);
494 goto err_free;
495 }
Carlo Caionedb33c772015-05-14 10:49:09 +0200496
497 resp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100498 bt_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x "
499 "lmp_ver=%02x lmp_subver=%04x",
500 resp->hci_ver, resp->hci_rev,
501 resp->lmp_ver, resp->lmp_subver);
Carlo Caionedb33c772015-05-14 10:49:09 +0200502
Alex Lu907f8492018-02-11 12:24:33 -0600503 hci_rev = le16_to_cpu(resp->hci_rev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200504 lmp_subver = le16_to_cpu(resp->lmp_subver);
505 kfree_skb(skb);
506
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200507 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev);
508 if (!btrtl_dev->ic_info) {
509 rtl_dev_err(hdev, "unknown IC info, lmp subver %04x, hci rev %04x\n",
510 lmp_subver, hci_rev);
511 ret = -EINVAL;
512 goto err_free;
513 }
514
515 if (btrtl_dev->ic_info->has_rom_version) {
516 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
517 if (ret)
518 goto err_free;
519 }
520
521 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
522 &btrtl_dev->fw_data);
523 if (btrtl_dev->fw_len < 0) {
524 rtl_dev_err(hdev, "firmware file %s not found\n",
525 btrtl_dev->ic_info->fw_name);
526 ret = btrtl_dev->fw_len;
527 goto err_free;
528 }
529
530 if (btrtl_dev->ic_info->cfg_name) {
531 btrtl_dev->cfg_len = rtl_load_file(hdev,
532 btrtl_dev->ic_info->cfg_name,
533 &btrtl_dev->cfg_data);
534 if (btrtl_dev->ic_info->config_needed &&
535 btrtl_dev->cfg_len <= 0) {
536 rtl_dev_err(hdev, "mandatory config file %s not found\n",
537 btrtl_dev->ic_info->cfg_name);
538 ret = btrtl_dev->cfg_len;
539 goto err_free;
540 }
541 }
542
543 return btrtl_dev;
544
545err_free:
546 btrtl_free(btrtl_dev);
547err_alloc:
548 return ERR_PTR(ret);
549}
550EXPORT_SYMBOL_GPL(btrtl_initialize);
551
552int btrtl_download_firmware(struct hci_dev *hdev,
553 struct btrtl_device_info *btrtl_dev)
554{
Carlo Caionedb33c772015-05-14 10:49:09 +0200555 /* Match a set of subver values that correspond to stock firmware,
556 * which is not compatible with standard btusb.
557 * If matched, upload an alternative firmware that does conform to
558 * standard btusb. Once that firmware is uploaded, the subver changes
559 * to a different value.
560 */
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200561 switch (btrtl_dev->ic_info->lmp_subver) {
Carlo Caionedb33c772015-05-14 10:49:09 +0200562 case RTL_ROM_LMP_8723A:
563 case RTL_ROM_LMP_3499:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200564 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200565 case RTL_ROM_LMP_8723B:
Carlo Caionedb33c772015-05-14 10:49:09 +0200566 case RTL_ROM_LMP_8821A:
Carlo Caionedb33c772015-05-14 10:49:09 +0200567 case RTL_ROM_LMP_8761A:
Larry Finger1110a2d2016-09-09 10:02:05 -0500568 case RTL_ROM_LMP_8822B:
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200569 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
Carlo Caionedb33c772015-05-14 10:49:09 +0200570 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100571 bt_dev_info(hdev, "rtl: assuming no firmware upload needed");
Carlo Caionedb33c772015-05-14 10:49:09 +0200572 return 0;
573 }
574}
Martin Blumenstingl26503ad22018-08-02 16:57:13 +0200575EXPORT_SYMBOL_GPL(btrtl_download_firmware);
576
577int btrtl_setup_realtek(struct hci_dev *hdev)
578{
579 struct btrtl_device_info *btrtl_dev;
580 int ret;
581
582 btrtl_dev = btrtl_initialize(hdev);
583 if (IS_ERR(btrtl_dev))
584 return PTR_ERR(btrtl_dev);
585
586 ret = btrtl_download_firmware(hdev, btrtl_dev);
587
588 btrtl_free(btrtl_dev);
589
590 return ret;
591}
Carlo Caionedb33c772015-05-14 10:49:09 +0200592EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
593
594MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
595MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
596MODULE_VERSION(VERSION);
597MODULE_LICENSE("GPL");
Martin Blumenstinglf96dbd32018-08-02 16:57:12 +0200598MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
599MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
600MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
601MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
602MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
603MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
604MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
605MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
606MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");