2015-05-14 11:49:09 +03:00
|
|
|
/*
|
|
|
|
* Bluetooth support for Realtek devices
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Endless Mobile, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/firmware.h>
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
#include <linux/usb.h>
|
|
|
|
|
|
|
|
#include <net/bluetooth/bluetooth.h>
|
|
|
|
#include <net/bluetooth/hci_core.h>
|
|
|
|
|
|
|
|
#include "btrtl.h"
|
|
|
|
|
|
|
|
#define VERSION "0.1"
|
|
|
|
|
|
|
|
#define RTL_EPATCH_SIGNATURE "Realtech"
|
|
|
|
#define RTL_ROM_LMP_3499 0x3499
|
|
|
|
#define RTL_ROM_LMP_8723A 0x1200
|
|
|
|
#define RTL_ROM_LMP_8723B 0x8723
|
|
|
|
#define RTL_ROM_LMP_8821A 0x8821
|
|
|
|
#define RTL_ROM_LMP_8761A 0x8761
|
2016-09-09 18:02:05 +03:00
|
|
|
#define RTL_ROM_LMP_8822B 0x8822
|
2015-05-14 11:49:09 +03:00
|
|
|
|
2018-02-11 21:24:33 +03:00
|
|
|
#define IC_MATCH_FL_LMPSUBV (1 << 0)
|
|
|
|
#define IC_MATCH_FL_HCIREV (1 << 1)
|
|
|
|
#define IC_INFO(lmps, hcir) \
|
|
|
|
.match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
|
|
|
|
.lmp_subver = (lmps), \
|
|
|
|
.hci_rev = (hcir)
|
|
|
|
|
|
|
|
struct id_table {
|
|
|
|
__u16 match_flags;
|
|
|
|
__u16 lmp_subver;
|
|
|
|
__u16 hci_rev;
|
|
|
|
bool config_needed;
|
2018-08-02 17:57:13 +03:00
|
|
|
bool has_rom_version;
|
2018-02-11 21:24:33 +03:00
|
|
|
char *fw_name;
|
|
|
|
char *cfg_name;
|
|
|
|
};
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
struct btrtl_device_info {
|
|
|
|
const struct id_table *ic_info;
|
|
|
|
u8 rom_version;
|
|
|
|
u8 *fw_data;
|
|
|
|
int fw_len;
|
|
|
|
u8 *cfg_data;
|
|
|
|
int cfg_len;
|
|
|
|
};
|
|
|
|
|
2018-02-11 21:24:33 +03:00
|
|
|
static const struct id_table ic_id_table[] = {
|
2018-08-02 17:57:13 +03:00
|
|
|
{ IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0,
|
|
|
|
.config_needed = false,
|
|
|
|
.has_rom_version = false,
|
|
|
|
.fw_name = "rtl_bt/rtl8723a_fw.bin",
|
|
|
|
.cfg_name = NULL },
|
|
|
|
|
|
|
|
{ IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0,
|
|
|
|
.config_needed = false,
|
|
|
|
.has_rom_version = false,
|
|
|
|
.fw_name = "rtl_bt/rtl8723a_fw.bin",
|
|
|
|
.cfg_name = NULL },
|
|
|
|
|
2018-02-11 21:24:33 +03:00
|
|
|
/* 8723B */
|
|
|
|
{ IC_INFO(RTL_ROM_LMP_8723B, 0xb),
|
|
|
|
.config_needed = false,
|
2018-08-02 17:57:13 +03:00
|
|
|
.has_rom_version = true,
|
2018-02-11 21:24:33 +03:00
|
|
|
.fw_name = "rtl_bt/rtl8723b_fw.bin",
|
|
|
|
.cfg_name = "rtl_bt/rtl8723b_config.bin" },
|
|
|
|
|
|
|
|
/* 8723D */
|
|
|
|
{ IC_INFO(RTL_ROM_LMP_8723B, 0xd),
|
|
|
|
.config_needed = true,
|
2018-08-02 17:57:13 +03:00
|
|
|
.has_rom_version = true,
|
2018-02-11 21:24:33 +03:00
|
|
|
.fw_name = "rtl_bt/rtl8723d_fw.bin",
|
|
|
|
.cfg_name = "rtl_bt/rtl8723d_config.bin" },
|
|
|
|
|
|
|
|
/* 8821A */
|
|
|
|
{ IC_INFO(RTL_ROM_LMP_8821A, 0xa),
|
|
|
|
.config_needed = false,
|
2018-08-02 17:57:13 +03:00
|
|
|
.has_rom_version = true,
|
2018-02-11 21:24:33 +03:00
|
|
|
.fw_name = "rtl_bt/rtl8821a_fw.bin",
|
|
|
|
.cfg_name = "rtl_bt/rtl8821a_config.bin" },
|
|
|
|
|
|
|
|
/* 8821C */
|
|
|
|
{ IC_INFO(RTL_ROM_LMP_8821A, 0xc),
|
|
|
|
.config_needed = false,
|
2018-08-02 17:57:13 +03:00
|
|
|
.has_rom_version = true,
|
2018-02-11 21:24:33 +03:00
|
|
|
.fw_name = "rtl_bt/rtl8821c_fw.bin",
|
|
|
|
.cfg_name = "rtl_bt/rtl8821c_config.bin" },
|
|
|
|
|
|
|
|
/* 8761A */
|
|
|
|
{ IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0,
|
|
|
|
.config_needed = false,
|
2018-08-02 17:57:13 +03:00
|
|
|
.has_rom_version = true,
|
2018-02-11 21:24:33 +03:00
|
|
|
.fw_name = "rtl_bt/rtl8761a_fw.bin",
|
|
|
|
.cfg_name = "rtl_bt/rtl8761a_config.bin" },
|
|
|
|
|
|
|
|
/* 8822B */
|
|
|
|
{ IC_INFO(RTL_ROM_LMP_8822B, 0xb),
|
|
|
|
.config_needed = true,
|
2018-08-02 17:57:13 +03:00
|
|
|
.has_rom_version = true,
|
2018-02-11 21:24:33 +03:00
|
|
|
.fw_name = "rtl_bt/rtl8822b_fw.bin",
|
|
|
|
.cfg_name = "rtl_bt/rtl8822b_config.bin" },
|
|
|
|
};
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
|
|
|
|
if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
|
|
|
|
(ic_id_table[i].lmp_subver != lmp_subver))
|
|
|
|
continue;
|
|
|
|
if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
|
|
|
|
(ic_id_table[i].hci_rev != hci_rev))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i >= ARRAY_SIZE(ic_id_table))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &ic_id_table[i];
|
|
|
|
}
|
|
|
|
|
2015-05-14 11:49:09 +03:00
|
|
|
static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
|
|
|
|
{
|
|
|
|
struct rtl_rom_version_evt *rom_version;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
/* Read RTL ROM version command */
|
|
|
|
skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
|
|
|
|
if (IS_ERR(skb)) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "Read ROM version failed (%ld)\n",
|
|
|
|
PTR_ERR(skb));
|
2015-05-14 11:49:09 +03:00
|
|
|
return PTR_ERR(skb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skb->len != sizeof(*rom_version)) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "RTL version event length mismatch\n");
|
2015-05-14 11:49:09 +03:00
|
|
|
kfree_skb(skb);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
rom_version = (struct rtl_rom_version_evt *)skb->data;
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_info(hdev, "rom_version status=%x version=%x\n",
|
|
|
|
rom_version->status, rom_version->version);
|
2015-05-14 11:49:09 +03:00
|
|
|
|
|
|
|
*version = rom_version->version;
|
|
|
|
|
|
|
|
kfree_skb(skb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
static int rtlbt_parse_firmware(struct hci_dev *hdev,
|
|
|
|
struct btrtl_device_info *btrtl_dev,
|
2018-02-11 21:24:33 +03:00
|
|
|
unsigned char **_buf)
|
2015-05-14 11:49:09 +03:00
|
|
|
{
|
|
|
|
const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
|
|
|
|
struct rtl_epatch_header *epatch_info;
|
|
|
|
unsigned char *buf;
|
2018-08-02 17:57:13 +03:00
|
|
|
int i, len;
|
2015-05-14 11:49:09 +03:00
|
|
|
size_t min_size;
|
2018-08-02 17:57:13 +03:00
|
|
|
u8 opcode, length, data;
|
2015-05-14 11:49:09 +03:00
|
|
|
int project_id = -1;
|
|
|
|
const unsigned char *fwptr, *chip_id_base;
|
|
|
|
const unsigned char *patch_length_base, *patch_offset_base;
|
|
|
|
u32 patch_offset = 0;
|
|
|
|
u16 patch_length, num_patches;
|
2016-09-09 18:02:05 +03:00
|
|
|
static const struct {
|
|
|
|
__u16 lmp_subver;
|
|
|
|
__u8 id;
|
|
|
|
} project_id_to_lmp_subver[] = {
|
|
|
|
{ RTL_ROM_LMP_8723A, 0 },
|
|
|
|
{ RTL_ROM_LMP_8723B, 1 },
|
|
|
|
{ RTL_ROM_LMP_8821A, 2 },
|
|
|
|
{ RTL_ROM_LMP_8761A, 3 },
|
|
|
|
{ RTL_ROM_LMP_8822B, 8 },
|
2018-02-11 21:24:33 +03:00
|
|
|
{ RTL_ROM_LMP_8723B, 9 }, /* 8723D */
|
|
|
|
{ RTL_ROM_LMP_8821A, 10 }, /* 8821C */
|
2015-05-14 11:49:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
|
2018-08-02 17:57:13 +03:00
|
|
|
if (btrtl_dev->fw_len < min_size)
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
|
2015-05-14 11:49:09 +03:00
|
|
|
if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "extension section signature mismatch\n");
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop from the end of the firmware parsing instructions, until
|
|
|
|
* we find an instruction that identifies the "project ID" for the
|
|
|
|
* hardware supported by this firwmare file.
|
|
|
|
* Once we have that, we double-check that that project_id is suitable
|
|
|
|
* for the hardware we are working with.
|
|
|
|
*/
|
2018-08-02 17:57:13 +03:00
|
|
|
while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
|
2015-05-14 11:49:09 +03:00
|
|
|
opcode = *--fwptr;
|
|
|
|
length = *--fwptr;
|
|
|
|
data = *--fwptr;
|
|
|
|
|
|
|
|
BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
|
|
|
|
|
|
|
|
if (opcode == 0xff) /* EOF */
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (length == 0) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "found instruction with length 0\n");
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode == 0 && length == 1) {
|
|
|
|
project_id = data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fwptr -= length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (project_id < 0) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "failed to find version instruction\n");
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:02:05 +03:00
|
|
|
/* Find project_id in table */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
|
|
|
|
if (project_id == project_id_to_lmp_subver[i].id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "unknown project id %d\n", project_id);
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
if (btrtl_dev->ic_info->lmp_subver !=
|
|
|
|
project_id_to_lmp_subver[i].lmp_subver) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "firmware is for %x but this is a %x\n",
|
|
|
|
project_id_to_lmp_subver[i].lmp_subver,
|
|
|
|
btrtl_dev->ic_info->lmp_subver);
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
|
2015-05-14 11:49:09 +03:00
|
|
|
if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "bad EPATCH signature\n");
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_patches = le16_to_cpu(epatch_info->num_patches);
|
|
|
|
BT_DBG("fw_version=%x, num_patches=%d",
|
|
|
|
le32_to_cpu(epatch_info->fw_version), num_patches);
|
|
|
|
|
|
|
|
/* After the rtl_epatch_header there is a funky patch metadata section.
|
|
|
|
* Assuming 2 patches, the layout is:
|
|
|
|
* ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
|
|
|
|
*
|
|
|
|
* Find the right patch for this chip.
|
|
|
|
*/
|
|
|
|
min_size += 8 * num_patches;
|
2018-08-02 17:57:13 +03:00
|
|
|
if (btrtl_dev->fw_len < min_size)
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
|
2015-05-14 11:49:09 +03:00
|
|
|
patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
|
|
|
|
patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
|
|
|
|
for (i = 0; i < num_patches; i++) {
|
|
|
|
u16 chip_id = get_unaligned_le16(chip_id_base +
|
|
|
|
(i * sizeof(u16)));
|
2018-08-02 17:57:13 +03:00
|
|
|
if (chip_id == btrtl_dev->rom_version + 1) {
|
2015-05-14 11:49:09 +03:00
|
|
|
patch_length = get_unaligned_le16(patch_length_base +
|
|
|
|
(i * sizeof(u16)));
|
|
|
|
patch_offset = get_unaligned_le32(patch_offset_base +
|
|
|
|
(i * sizeof(u32)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!patch_offset) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "didn't find patch for chip id %d",
|
|
|
|
btrtl_dev->rom_version);
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
|
|
|
|
min_size = patch_offset + patch_length;
|
2018-08-02 17:57:13 +03:00
|
|
|
if (btrtl_dev->fw_len < min_size)
|
2015-05-14 11:49:09 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Copy the firmware into a new buffer and write the version at
|
|
|
|
* the end.
|
|
|
|
*/
|
|
|
|
len = patch_length;
|
2018-08-02 17:57:13 +03:00
|
|
|
buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length,
|
|
|
|
GFP_KERNEL);
|
2015-05-14 11:49:09 +03:00
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
|
|
|
|
|
|
|
|
*_buf = buf;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rtl_download_firmware(struct hci_dev *hdev,
|
|
|
|
const unsigned char *data, int fw_len)
|
|
|
|
{
|
|
|
|
struct rtl_download_cmd *dl_cmd;
|
|
|
|
int frag_num = fw_len / RTL_FRAG_LEN + 1;
|
|
|
|
int frag_len = RTL_FRAG_LEN;
|
|
|
|
int ret = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
|
|
|
|
if (!dl_cmd)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < frag_num; i++) {
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
BT_DBG("download fw (%d/%d)", i, frag_num);
|
|
|
|
|
|
|
|
dl_cmd->index = i;
|
|
|
|
if (i == (frag_num - 1)) {
|
|
|
|
dl_cmd->index |= 0x80; /* data end */
|
|
|
|
frag_len = fw_len % RTL_FRAG_LEN;
|
|
|
|
}
|
|
|
|
memcpy(dl_cmd->data, data, frag_len);
|
|
|
|
|
|
|
|
/* Send download command */
|
|
|
|
skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
|
|
|
|
HCI_INIT_TIMEOUT);
|
|
|
|
if (IS_ERR(skb)) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "download fw command failed (%ld)\n",
|
|
|
|
PTR_ERR(skb));
|
2015-05-14 11:49:09 +03:00
|
|
|
ret = -PTR_ERR(skb);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skb->len != sizeof(struct rtl_download_response)) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "download fw event length mismatch\n");
|
2015-05-14 11:49:09 +03:00
|
|
|
kfree_skb(skb);
|
|
|
|
ret = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree_skb(skb);
|
|
|
|
data += RTL_FRAG_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
kfree(dl_cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
|
2016-09-09 18:02:05 +03:00
|
|
|
{
|
|
|
|
const struct firmware *fw;
|
|
|
|
int ret;
|
|
|
|
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_info(hdev, "rtl: loading %s\n", name);
|
2016-09-09 18:02:05 +03:00
|
|
|
ret = request_firmware(&fw, name, &hdev->dev);
|
2017-02-20 05:04:57 +03:00
|
|
|
if (ret < 0)
|
2016-09-09 18:02:05 +03:00
|
|
|
return ret;
|
|
|
|
ret = fw->size;
|
|
|
|
*buff = kmemdup(fw->data, ret, GFP_KERNEL);
|
2017-07-28 17:41:11 +03:00
|
|
|
if (!*buff)
|
|
|
|
ret = -ENOMEM;
|
2016-09-09 18:02:05 +03:00
|
|
|
|
|
|
|
release_firmware(fw);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
|
|
|
|
struct btrtl_device_info *btrtl_dev)
|
2015-05-14 11:49:09 +03:00
|
|
|
{
|
2018-08-02 17:57:13 +03:00
|
|
|
if (btrtl_dev->fw_len < 8)
|
|
|
|
return -EINVAL;
|
2015-05-14 11:49:09 +03:00
|
|
|
|
|
|
|
/* Check that the firmware doesn't have the epatch signature
|
|
|
|
* (which is only for RTL8723B and newer).
|
|
|
|
*/
|
2018-08-02 17:57:13 +03:00
|
|
|
if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "unexpected EPATCH signature!\n");
|
2018-08-02 17:57:13 +03:00
|
|
|
return -EINVAL;
|
2015-05-14 11:49:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
return rtl_download_firmware(hdev, btrtl_dev->fw_data,
|
|
|
|
btrtl_dev->fw_len);
|
2015-05-14 11:49:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
|
|
|
|
struct btrtl_device_info *btrtl_dev)
|
2015-05-14 11:49:09 +03:00
|
|
|
{
|
|
|
|
unsigned char *fw_data = NULL;
|
|
|
|
int ret;
|
2016-09-09 18:02:05 +03:00
|
|
|
u8 *tbuff;
|
2015-05-14 11:49:09 +03:00
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
|
2015-05-14 11:49:09 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
if (btrtl_dev->cfg_len > 0) {
|
|
|
|
tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
|
2016-09-09 18:02:05 +03:00
|
|
|
if (!tbuff) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tbuff, fw_data, ret);
|
|
|
|
kfree(fw_data);
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
|
|
|
|
ret += btrtl_dev->cfg_len;
|
2016-09-09 18:02:05 +03:00
|
|
|
|
|
|
|
fw_data = tbuff;
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
|
2016-09-09 18:02:05 +03:00
|
|
|
|
2015-05-14 11:49:09 +03:00
|
|
|
ret = rtl_download_firmware(hdev, fw_data, ret);
|
|
|
|
|
|
|
|
out:
|
2016-09-09 18:02:05 +03:00
|
|
|
kfree(fw_data);
|
2015-05-14 11:49:09 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
|
|
|
skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
|
|
|
|
HCI_INIT_TIMEOUT);
|
|
|
|
if (IS_ERR(skb)) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
|
|
|
|
PTR_ERR(skb));
|
2015-05-14 11:49:09 +03:00
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skb->len != sizeof(struct hci_rp_read_local_version)) {
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
|
2015-05-14 11:49:09 +03:00
|
|
|
kfree_skb(skb);
|
|
|
|
return ERR_PTR(-EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
return skb;
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
void btrtl_free(struct btrtl_device_info *btrtl_dev)
|
|
|
|
{
|
|
|
|
kfree(btrtl_dev->fw_data);
|
|
|
|
kfree(btrtl_dev->cfg_data);
|
|
|
|
kfree(btrtl_dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btrtl_free);
|
|
|
|
|
|
|
|
struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev)
|
2015-05-14 11:49:09 +03:00
|
|
|
{
|
2018-08-02 17:57:13 +03:00
|
|
|
struct btrtl_device_info *btrtl_dev;
|
2015-05-14 11:49:09 +03:00
|
|
|
struct sk_buff *skb;
|
|
|
|
struct hci_rp_read_local_version *resp;
|
2018-02-11 21:24:33 +03:00
|
|
|
u16 hci_rev, lmp_subver;
|
2018-08-02 17:57:13 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
|
|
|
|
if (!btrtl_dev) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_alloc;
|
|
|
|
}
|
2015-05-14 11:49:09 +03:00
|
|
|
|
|
|
|
skb = btrtl_read_local_version(hdev);
|
2018-08-02 17:57:13 +03:00
|
|
|
if (IS_ERR(skb)) {
|
|
|
|
ret = PTR_ERR(skb);
|
|
|
|
goto err_free;
|
|
|
|
}
|
2015-05-14 11:49:09 +03:00
|
|
|
|
|
|
|
resp = (struct hci_rp_read_local_version *)skb->data;
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
|
|
|
|
resp->hci_ver, resp->hci_rev,
|
|
|
|
resp->lmp_ver, resp->lmp_subver);
|
2015-05-14 11:49:09 +03:00
|
|
|
|
2018-02-11 21:24:33 +03:00
|
|
|
hci_rev = le16_to_cpu(resp->hci_rev);
|
2015-05-14 11:49:09 +03:00
|
|
|
lmp_subver = le16_to_cpu(resp->lmp_subver);
|
|
|
|
kfree_skb(skb);
|
|
|
|
|
2018-08-02 17:57:13 +03:00
|
|
|
btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev);
|
|
|
|
if (!btrtl_dev->ic_info) {
|
|
|
|
rtl_dev_err(hdev, "unknown IC info, lmp subver %04x, hci rev %04x\n",
|
|
|
|
lmp_subver, hci_rev);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (btrtl_dev->ic_info->has_rom_version) {
|
|
|
|
ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
|
|
|
|
if (ret)
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
|
|
|
|
&btrtl_dev->fw_data);
|
|
|
|
if (btrtl_dev->fw_len < 0) {
|
|
|
|
rtl_dev_err(hdev, "firmware file %s not found\n",
|
|
|
|
btrtl_dev->ic_info->fw_name);
|
|
|
|
ret = btrtl_dev->fw_len;
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (btrtl_dev->ic_info->cfg_name) {
|
|
|
|
btrtl_dev->cfg_len = rtl_load_file(hdev,
|
|
|
|
btrtl_dev->ic_info->cfg_name,
|
|
|
|
&btrtl_dev->cfg_data);
|
|
|
|
if (btrtl_dev->ic_info->config_needed &&
|
|
|
|
btrtl_dev->cfg_len <= 0) {
|
|
|
|
rtl_dev_err(hdev, "mandatory config file %s not found\n",
|
|
|
|
btrtl_dev->ic_info->cfg_name);
|
|
|
|
ret = btrtl_dev->cfg_len;
|
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return btrtl_dev;
|
|
|
|
|
|
|
|
err_free:
|
|
|
|
btrtl_free(btrtl_dev);
|
|
|
|
err_alloc:
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btrtl_initialize);
|
|
|
|
|
|
|
|
int btrtl_download_firmware(struct hci_dev *hdev,
|
|
|
|
struct btrtl_device_info *btrtl_dev)
|
|
|
|
{
|
2015-05-14 11:49:09 +03:00
|
|
|
/* Match a set of subver values that correspond to stock firmware,
|
|
|
|
* which is not compatible with standard btusb.
|
|
|
|
* If matched, upload an alternative firmware that does conform to
|
|
|
|
* standard btusb. Once that firmware is uploaded, the subver changes
|
|
|
|
* to a different value.
|
|
|
|
*/
|
2018-08-02 17:57:13 +03:00
|
|
|
switch (btrtl_dev->ic_info->lmp_subver) {
|
2015-05-14 11:49:09 +03:00
|
|
|
case RTL_ROM_LMP_8723A:
|
|
|
|
case RTL_ROM_LMP_3499:
|
2018-08-02 17:57:13 +03:00
|
|
|
return btrtl_setup_rtl8723a(hdev, btrtl_dev);
|
2015-05-14 11:49:09 +03:00
|
|
|
case RTL_ROM_LMP_8723B:
|
|
|
|
case RTL_ROM_LMP_8821A:
|
|
|
|
case RTL_ROM_LMP_8761A:
|
2016-09-09 18:02:05 +03:00
|
|
|
case RTL_ROM_LMP_8822B:
|
2018-08-02 17:57:13 +03:00
|
|
|
return btrtl_setup_rtl8723b(hdev, btrtl_dev);
|
2015-05-14 11:49:09 +03:00
|
|
|
default:
|
2018-08-02 17:57:14 +03:00
|
|
|
rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
|
2015-05-14 11:49:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 17:57:13 +03:00
|
|
|
EXPORT_SYMBOL_GPL(btrtl_download_firmware);
|
|
|
|
|
|
|
|
int btrtl_setup_realtek(struct hci_dev *hdev)
|
|
|
|
{
|
|
|
|
struct btrtl_device_info *btrtl_dev;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
btrtl_dev = btrtl_initialize(hdev);
|
|
|
|
if (IS_ERR(btrtl_dev))
|
|
|
|
return PTR_ERR(btrtl_dev);
|
|
|
|
|
|
|
|
ret = btrtl_download_firmware(hdev, btrtl_dev);
|
|
|
|
|
|
|
|
btrtl_free(btrtl_dev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-05-14 11:49:09 +03:00
|
|
|
EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
|
|
|
|
MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
|
|
|
|
MODULE_VERSION(VERSION);
|
|
|
|
MODULE_LICENSE("GPL");
|
2018-08-02 17:57:12 +03:00
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
|
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
|
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
|
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
|
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
|
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
|
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
|
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
|
|
|
|
MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
|