2021-10-18 21:30:12 +00:00
|
|
|
// SPDX-License-Identifier: ISC
|
|
|
|
/* Copyright (C) 2021 MediaTek Inc.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/firmware.h>
|
2024-07-04 06:01:13 +00:00
|
|
|
#include <linux/usb.h>
|
2021-10-18 21:30:12 +00:00
|
|
|
|
|
|
|
#include <net/bluetooth/bluetooth.h>
|
|
|
|
#include <net/bluetooth/hci_core.h>
|
|
|
|
|
|
|
|
#include "btmtk.h"
|
|
|
|
|
|
|
|
#define VERSION "0.1"
|
|
|
|
|
|
|
|
/* It is for mt79xx download rom patch*/
|
|
|
|
#define MTK_FW_ROM_PATCH_HEADER_SIZE 32
|
|
|
|
#define MTK_FW_ROM_PATCH_GD_SIZE 64
|
|
|
|
#define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
|
|
|
|
#define MTK_SEC_MAP_COMMON_SIZE 12
|
|
|
|
#define MTK_SEC_MAP_NEED_SEND_SIZE 52
|
|
|
|
|
|
|
|
struct btmtk_patch_header {
|
|
|
|
u8 datetime[16];
|
|
|
|
u8 platform[4];
|
|
|
|
__le16 hwver;
|
|
|
|
__le16 swver;
|
|
|
|
__le32 magicnum;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct btmtk_global_desc {
|
|
|
|
__le32 patch_ver;
|
|
|
|
__le32 sub_sys;
|
|
|
|
__le32 feature_opt;
|
|
|
|
__le32 section_num;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
struct btmtk_section_map {
|
|
|
|
__le32 sectype;
|
|
|
|
__le32 secoffset;
|
|
|
|
__le32 secsize;
|
|
|
|
union {
|
|
|
|
__le32 u4SecSpec[13];
|
|
|
|
struct {
|
|
|
|
__le32 dlAddr;
|
|
|
|
__le32 dlsize;
|
|
|
|
__le32 seckeyidx;
|
|
|
|
__le32 alignlen;
|
|
|
|
__le32 sectype;
|
|
|
|
__le32 dlmodecrctype;
|
|
|
|
__le32 crc;
|
|
|
|
__le32 reserved[6];
|
|
|
|
} bin_info_spec;
|
|
|
|
};
|
|
|
|
} __packed;
|
|
|
|
|
2023-06-28 22:54:57 +00:00
|
|
|
static void btmtk_coredump(struct hci_dev *hdev)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = __hci_cmd_send(hdev, 0xfd5b, 0, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
bt_dev_err(hdev, "Coredump failed (%d)", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btmtk_coredump_hdr(struct hci_dev *hdev, struct sk_buff *skb)
|
|
|
|
{
|
2024-07-04 06:01:11 +00:00
|
|
|
struct btmtk_data *data = hci_get_priv(hdev);
|
2023-06-28 22:54:57 +00:00
|
|
|
char buf[80];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "Controller Name: 0x%X\n",
|
|
|
|
data->dev_id);
|
|
|
|
skb_put_data(skb, buf, strlen(buf));
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
|
|
|
|
data->cd_info.fw_version);
|
|
|
|
skb_put_data(skb, buf, strlen(buf));
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "Driver: %s\n",
|
|
|
|
data->cd_info.driver_name);
|
|
|
|
skb_put_data(skb, buf, strlen(buf));
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "Vendor: MediaTek\n");
|
|
|
|
skb_put_data(skb, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btmtk_coredump_notify(struct hci_dev *hdev, int state)
|
|
|
|
{
|
2024-07-04 06:01:11 +00:00
|
|
|
struct btmtk_data *data = hci_get_priv(hdev);
|
2023-06-28 22:54:57 +00:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case HCI_DEVCOREDUMP_IDLE:
|
|
|
|
data->cd_info.state = HCI_DEVCOREDUMP_IDLE;
|
|
|
|
break;
|
|
|
|
case HCI_DEVCOREDUMP_ACTIVE:
|
|
|
|
data->cd_info.state = HCI_DEVCOREDUMP_ACTIVE;
|
|
|
|
break;
|
|
|
|
case HCI_DEVCOREDUMP_TIMEOUT:
|
|
|
|
case HCI_DEVCOREDUMP_ABORT:
|
|
|
|
case HCI_DEVCOREDUMP_DONE:
|
|
|
|
data->cd_info.state = HCI_DEVCOREDUMP_IDLE;
|
|
|
|
btmtk_reset_sync(hdev);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:17 +00:00
|
|
|
void btmtk_fw_get_filename(char *buf, size_t size, u32 dev_id, u32 fw_ver,
|
|
|
|
u32 fw_flavor)
|
|
|
|
{
|
|
|
|
if (dev_id == 0x7925)
|
|
|
|
snprintf(buf, size,
|
|
|
|
"mediatek/mt%04x/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
|
|
|
|
dev_id & 0xffff, dev_id & 0xffff, (fw_ver & 0xff) + 1);
|
|
|
|
else if (dev_id == 0x7961 && fw_flavor)
|
|
|
|
snprintf(buf, size,
|
|
|
|
"mediatek/BT_RAM_CODE_MT%04x_1a_%x_hdr.bin",
|
|
|
|
dev_id & 0xffff, (fw_ver & 0xff) + 1);
|
|
|
|
else
|
|
|
|
snprintf(buf, size,
|
|
|
|
"mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
|
|
|
|
dev_id & 0xffff, (fw_ver & 0xff) + 1);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btmtk_fw_get_filename);
|
|
|
|
|
2021-10-18 21:30:12 +00:00
|
|
|
int btmtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname,
|
|
|
|
wmt_cmd_sync_func_t wmt_cmd_sync)
|
|
|
|
{
|
|
|
|
struct btmtk_hci_wmt_params wmt_params;
|
2023-06-28 22:20:03 +00:00
|
|
|
struct btmtk_patch_header *hdr;
|
2021-10-18 21:30:12 +00:00
|
|
|
struct btmtk_global_desc *globaldesc = NULL;
|
|
|
|
struct btmtk_section_map *sectionmap;
|
|
|
|
const struct firmware *fw;
|
|
|
|
const u8 *fw_ptr;
|
|
|
|
const u8 *fw_bin_ptr;
|
|
|
|
int err, dlen, i, status;
|
|
|
|
u8 flag, first_block, retry;
|
|
|
|
u32 section_num, dl_size, section_offset;
|
|
|
|
u8 cmd[64];
|
|
|
|
|
|
|
|
err = request_firmware(&fw, fwname, &hdev->dev);
|
|
|
|
if (err < 0) {
|
|
|
|
bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
fw_ptr = fw->data;
|
|
|
|
fw_bin_ptr = fw_ptr;
|
2023-06-28 22:20:03 +00:00
|
|
|
hdr = (struct btmtk_patch_header *)fw_ptr;
|
2021-10-18 21:30:12 +00:00
|
|
|
globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
|
|
|
|
section_num = le32_to_cpu(globaldesc->section_num);
|
|
|
|
|
2023-06-28 22:20:03 +00:00
|
|
|
bt_dev_info(hdev, "HW/SW Version: 0x%04x%04x, Build Time: %s",
|
|
|
|
le16_to_cpu(hdr->hwver), le16_to_cpu(hdr->swver), hdr->datetime);
|
|
|
|
|
2021-10-18 21:30:12 +00:00
|
|
|
for (i = 0; i < section_num; i++) {
|
|
|
|
first_block = 1;
|
|
|
|
fw_ptr = fw_bin_ptr;
|
|
|
|
sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
|
|
|
|
MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
|
|
|
|
|
|
|
|
section_offset = le32_to_cpu(sectionmap->secoffset);
|
|
|
|
dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
|
|
|
|
|
|
|
|
if (dl_size > 0) {
|
|
|
|
retry = 20;
|
|
|
|
while (retry > 0) {
|
|
|
|
cmd[0] = 0; /* 0 means legacy dl mode. */
|
|
|
|
memcpy(cmd + 1,
|
|
|
|
fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
|
|
|
|
MTK_FW_ROM_PATCH_GD_SIZE +
|
|
|
|
MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
|
|
|
|
MTK_SEC_MAP_COMMON_SIZE,
|
|
|
|
MTK_SEC_MAP_NEED_SEND_SIZE + 1);
|
|
|
|
|
|
|
|
wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
|
|
|
|
wmt_params.status = &status;
|
|
|
|
wmt_params.flag = 0;
|
|
|
|
wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
|
|
|
|
wmt_params.data = &cmd;
|
|
|
|
|
|
|
|
err = wmt_cmd_sync(hdev, &wmt_params);
|
|
|
|
if (err < 0) {
|
|
|
|
bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
|
|
|
|
err);
|
|
|
|
goto err_release_fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == BTMTK_WMT_PATCH_UNDONE) {
|
|
|
|
break;
|
|
|
|
} else if (status == BTMTK_WMT_PATCH_PROGRESS) {
|
|
|
|
msleep(100);
|
|
|
|
retry--;
|
|
|
|
} else if (status == BTMTK_WMT_PATCH_DONE) {
|
|
|
|
goto next_section;
|
|
|
|
} else {
|
|
|
|
bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
|
|
|
|
status);
|
2021-12-06 17:33:43 +00:00
|
|
|
err = -EIO;
|
2021-10-18 21:30:12 +00:00
|
|
|
goto err_release_fw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fw_ptr += section_offset;
|
|
|
|
wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
|
|
|
|
wmt_params.status = NULL;
|
|
|
|
|
|
|
|
while (dl_size > 0) {
|
|
|
|
dlen = min_t(int, 250, dl_size);
|
|
|
|
if (first_block == 1) {
|
|
|
|
flag = 1;
|
|
|
|
first_block = 0;
|
|
|
|
} else if (dl_size - dlen <= 0) {
|
|
|
|
flag = 3;
|
|
|
|
} else {
|
|
|
|
flag = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
wmt_params.flag = flag;
|
|
|
|
wmt_params.dlen = dlen;
|
|
|
|
wmt_params.data = fw_ptr;
|
|
|
|
|
|
|
|
err = wmt_cmd_sync(hdev, &wmt_params);
|
|
|
|
if (err < 0) {
|
|
|
|
bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
|
|
|
|
err);
|
|
|
|
goto err_release_fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
dl_size -= dlen;
|
|
|
|
fw_ptr += dlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next_section:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Wait a few moments for firmware activation done */
|
|
|
|
usleep_range(100000, 120000);
|
|
|
|
|
|
|
|
err_release_fw:
|
|
|
|
release_firmware(fw);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btmtk_setup_firmware_79xx);
|
|
|
|
|
|
|
|
int btmtk_setup_firmware(struct hci_dev *hdev, const char *fwname,
|
|
|
|
wmt_cmd_sync_func_t wmt_cmd_sync)
|
|
|
|
{
|
|
|
|
struct btmtk_hci_wmt_params wmt_params;
|
|
|
|
const struct firmware *fw;
|
|
|
|
const u8 *fw_ptr;
|
|
|
|
size_t fw_size;
|
|
|
|
int err, dlen;
|
|
|
|
u8 flag, param;
|
|
|
|
|
|
|
|
err = request_firmware(&fw, fwname, &hdev->dev);
|
|
|
|
if (err < 0) {
|
|
|
|
bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Power on data RAM the firmware relies on. */
|
|
|
|
param = 1;
|
|
|
|
wmt_params.op = BTMTK_WMT_FUNC_CTRL;
|
|
|
|
wmt_params.flag = 3;
|
|
|
|
wmt_params.dlen = sizeof(param);
|
|
|
|
wmt_params.data = ¶m;
|
|
|
|
wmt_params.status = NULL;
|
|
|
|
|
|
|
|
err = wmt_cmd_sync(hdev, &wmt_params);
|
|
|
|
if (err < 0) {
|
|
|
|
bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
|
|
|
|
goto err_release_fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
fw_ptr = fw->data;
|
|
|
|
fw_size = fw->size;
|
|
|
|
|
|
|
|
/* The size of patch header is 30 bytes, should be skip */
|
|
|
|
if (fw_size < 30) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto err_release_fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
fw_size -= 30;
|
|
|
|
fw_ptr += 30;
|
|
|
|
flag = 1;
|
|
|
|
|
|
|
|
wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
|
|
|
|
wmt_params.status = NULL;
|
|
|
|
|
|
|
|
while (fw_size > 0) {
|
|
|
|
dlen = min_t(int, 250, fw_size);
|
|
|
|
|
|
|
|
/* Tell device the position in sequence */
|
|
|
|
if (fw_size - dlen <= 0)
|
|
|
|
flag = 3;
|
|
|
|
else if (fw_size < fw->size - 30)
|
|
|
|
flag = 2;
|
|
|
|
|
|
|
|
wmt_params.flag = flag;
|
|
|
|
wmt_params.dlen = dlen;
|
|
|
|
wmt_params.data = fw_ptr;
|
|
|
|
|
|
|
|
err = wmt_cmd_sync(hdev, &wmt_params);
|
|
|
|
if (err < 0) {
|
|
|
|
bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
|
|
|
|
err);
|
|
|
|
goto err_release_fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
fw_size -= dlen;
|
|
|
|
fw_ptr += dlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
wmt_params.op = BTMTK_WMT_RST;
|
|
|
|
wmt_params.flag = 4;
|
|
|
|
wmt_params.dlen = 0;
|
|
|
|
wmt_params.data = NULL;
|
|
|
|
wmt_params.status = NULL;
|
|
|
|
|
|
|
|
/* Activate funciton the firmware providing to */
|
|
|
|
err = wmt_cmd_sync(hdev, &wmt_params);
|
|
|
|
if (err < 0) {
|
|
|
|
bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
|
|
|
|
goto err_release_fw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a few moments for firmware activation done */
|
|
|
|
usleep_range(10000, 12000);
|
|
|
|
|
|
|
|
err_release_fw:
|
|
|
|
release_firmware(fw);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btmtk_setup_firmware);
|
|
|
|
|
|
|
|
int btmtk_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
|
|
|
|
{
|
|
|
|
struct sk_buff *skb;
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
skb = __hci_cmd_sync(hdev, 0xfc1a, 6, bdaddr, HCI_INIT_TIMEOUT);
|
|
|
|
if (IS_ERR(skb)) {
|
|
|
|
ret = PTR_ERR(skb);
|
|
|
|
bt_dev_err(hdev, "changing Mediatek device address failed (%ld)",
|
|
|
|
ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
kfree_skb(skb);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btmtk_set_bdaddr);
|
|
|
|
|
2023-06-28 22:54:56 +00:00
|
|
|
void btmtk_reset_sync(struct hci_dev *hdev)
|
|
|
|
{
|
2024-07-04 06:01:11 +00:00
|
|
|
struct btmtk_data *reset_work = hci_get_priv(hdev);
|
2023-06-28 22:54:56 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
hci_dev_lock(hdev);
|
|
|
|
|
|
|
|
err = hci_cmd_sync_queue(hdev, reset_work->reset_sync, NULL, NULL);
|
|
|
|
if (err)
|
|
|
|
bt_dev_err(hdev, "failed to reset (%d)", err);
|
|
|
|
|
|
|
|
hci_dev_unlock(hdev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btmtk_reset_sync);
|
|
|
|
|
2023-06-28 22:54:57 +00:00
|
|
|
int btmtk_register_coredump(struct hci_dev *hdev, const char *name,
|
|
|
|
u32 fw_version)
|
|
|
|
{
|
2024-07-04 06:01:11 +00:00
|
|
|
struct btmtk_data *data = hci_get_priv(hdev);
|
2023-06-28 22:54:57 +00:00
|
|
|
|
|
|
|
if (!IS_ENABLED(CONFIG_DEV_COREDUMP))
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
data->cd_info.fw_version = fw_version;
|
|
|
|
data->cd_info.state = HCI_DEVCOREDUMP_IDLE;
|
|
|
|
data->cd_info.driver_name = name;
|
|
|
|
|
|
|
|
return hci_devcd_register(hdev, btmtk_coredump, btmtk_coredump_hdr,
|
|
|
|
btmtk_coredump_notify);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btmtk_register_coredump);
|
|
|
|
|
|
|
|
int btmtk_process_coredump(struct hci_dev *hdev, struct sk_buff *skb)
|
|
|
|
{
|
2024-07-04 06:01:11 +00:00
|
|
|
struct btmtk_data *data = hci_get_priv(hdev);
|
2023-06-28 22:54:57 +00:00
|
|
|
int err;
|
|
|
|
|
2024-02-28 16:17:24 +00:00
|
|
|
if (!IS_ENABLED(CONFIG_DEV_COREDUMP)) {
|
|
|
|
kfree_skb(skb);
|
2023-06-28 22:54:57 +00:00
|
|
|
return 0;
|
2024-02-28 16:17:24 +00:00
|
|
|
}
|
2023-06-28 22:54:57 +00:00
|
|
|
|
|
|
|
switch (data->cd_info.state) {
|
|
|
|
case HCI_DEVCOREDUMP_IDLE:
|
|
|
|
err = hci_devcd_init(hdev, MTK_COREDUMP_SIZE);
|
2024-04-17 23:27:38 +00:00
|
|
|
if (err < 0) {
|
|
|
|
kfree_skb(skb);
|
2023-06-28 22:54:57 +00:00
|
|
|
break;
|
2024-04-17 23:27:38 +00:00
|
|
|
}
|
2023-06-28 22:54:57 +00:00
|
|
|
data->cd_info.cnt = 0;
|
|
|
|
|
|
|
|
/* It is supposed coredump can be done within 5 seconds */
|
|
|
|
schedule_delayed_work(&hdev->dump.dump_timeout,
|
|
|
|
msecs_to_jiffies(5000));
|
|
|
|
fallthrough;
|
|
|
|
case HCI_DEVCOREDUMP_ACTIVE:
|
|
|
|
default:
|
|
|
|
err = hci_devcd_append(hdev, skb);
|
|
|
|
if (err < 0)
|
|
|
|
break;
|
|
|
|
data->cd_info.cnt++;
|
|
|
|
|
|
|
|
/* Mediatek coredump data would be more than MTK_COREDUMP_NUM */
|
|
|
|
if (data->cd_info.cnt > MTK_COREDUMP_NUM &&
|
2023-07-13 07:11:06 +00:00
|
|
|
skb->len > MTK_COREDUMP_END_LEN)
|
|
|
|
if (!memcmp((char *)&skb->data[skb->len - MTK_COREDUMP_END_LEN],
|
|
|
|
MTK_COREDUMP_END, MTK_COREDUMP_END_LEN - 1)) {
|
|
|
|
bt_dev_info(hdev, "Mediatek coredump end");
|
|
|
|
hci_devcd_complete(hdev);
|
|
|
|
}
|
2023-06-28 22:54:57 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btmtk_process_coredump);
|
|
|
|
|
2024-07-04 06:01:13 +00:00
|
|
|
static void btmtk_usb_wmt_recv(struct urb *urb)
|
|
|
|
{
|
|
|
|
struct hci_dev *hdev = urb->context;
|
|
|
|
struct btmtk_data *data = hci_get_priv(hdev);
|
|
|
|
struct sk_buff *skb;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (urb->status == 0 && urb->actual_length > 0) {
|
|
|
|
hdev->stat.byte_rx += urb->actual_length;
|
|
|
|
|
|
|
|
/* WMT event shouldn't be fragmented and the size should be
|
|
|
|
* less than HCI_WMT_MAX_EVENT_SIZE.
|
|
|
|
*/
|
|
|
|
skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
|
|
|
|
if (!skb) {
|
|
|
|
hdev->stat.err_rx++;
|
|
|
|
kfree(urb->setup_packet);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
|
|
|
|
skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
|
|
|
|
|
|
|
|
/* When someone waits for the WMT event, the skb is being cloned
|
|
|
|
* and being processed the events from there then.
|
|
|
|
*/
|
|
|
|
if (test_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags)) {
|
|
|
|
data->evt_skb = skb_clone(skb, GFP_ATOMIC);
|
|
|
|
if (!data->evt_skb) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
kfree(urb->setup_packet);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hci_recv_frame(hdev, skb);
|
|
|
|
if (err < 0) {
|
|
|
|
kfree_skb(data->evt_skb);
|
|
|
|
data->evt_skb = NULL;
|
|
|
|
kfree(urb->setup_packet);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test_and_clear_bit(BTMTK_TX_WAIT_VND_EVT,
|
|
|
|
&data->flags)) {
|
|
|
|
/* Barrier to sync with other CPUs */
|
|
|
|
smp_mb__after_atomic();
|
|
|
|
wake_up_bit(&data->flags,
|
|
|
|
BTMTK_TX_WAIT_VND_EVT);
|
|
|
|
}
|
|
|
|
kfree(urb->setup_packet);
|
|
|
|
return;
|
|
|
|
} else if (urb->status == -ENOENT) {
|
|
|
|
/* Avoid suspend failed when usb_kill_urb */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_mark_last_busy(data->udev);
|
|
|
|
|
|
|
|
/* The URB complete handler is still called with urb->actual_length = 0
|
|
|
|
* when the event is not available, so we should keep re-submitting
|
|
|
|
* URB until WMT event returns, Also, It's necessary to wait some time
|
|
|
|
* between the two consecutive control URBs to relax the target device
|
|
|
|
* to generate the event. Otherwise, the WMT event cannot return from
|
|
|
|
* the device successfully.
|
|
|
|
*/
|
|
|
|
udelay(500);
|
|
|
|
|
|
|
|
usb_anchor_urb(urb, data->ctrl_anchor);
|
|
|
|
err = usb_submit_urb(urb, GFP_ATOMIC);
|
|
|
|
if (err < 0) {
|
|
|
|
kfree(urb->setup_packet);
|
|
|
|
/* -EPERM: urb is being killed;
|
|
|
|
* -ENODEV: device got disconnected
|
|
|
|
*/
|
|
|
|
if (err != -EPERM && err != -ENODEV)
|
|
|
|
bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
|
|
|
|
urb, -err);
|
|
|
|
usb_unanchor_urb(urb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int btmtk_usb_submit_wmt_recv_urb(struct hci_dev *hdev)
|
|
|
|
{
|
|
|
|
struct btmtk_data *data = hci_get_priv(hdev);
|
|
|
|
struct usb_ctrlrequest *dr;
|
|
|
|
unsigned char *buf;
|
|
|
|
int err, size = 64;
|
|
|
|
unsigned int pipe;
|
|
|
|
struct urb *urb;
|
|
|
|
|
|
|
|
urb = usb_alloc_urb(0, GFP_KERNEL);
|
|
|
|
if (!urb)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
dr = kmalloc(sizeof(*dr), GFP_KERNEL);
|
|
|
|
if (!dr) {
|
|
|
|
usb_free_urb(urb);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
|
|
|
|
dr->bRequest = 1;
|
|
|
|
dr->wIndex = cpu_to_le16(0);
|
|
|
|
dr->wValue = cpu_to_le16(48);
|
|
|
|
dr->wLength = cpu_to_le16(size);
|
|
|
|
|
|
|
|
buf = kmalloc(size, GFP_KERNEL);
|
|
|
|
if (!buf) {
|
|
|
|
kfree(dr);
|
|
|
|
usb_free_urb(urb);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe = usb_rcvctrlpipe(data->udev, 0);
|
|
|
|
|
|
|
|
usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
|
|
|
|
buf, size, btmtk_usb_wmt_recv, hdev);
|
|
|
|
|
|
|
|
urb->transfer_flags |= URB_FREE_BUFFER;
|
|
|
|
|
|
|
|
usb_anchor_urb(urb, data->ctrl_anchor);
|
|
|
|
err = usb_submit_urb(urb, GFP_KERNEL);
|
|
|
|
if (err < 0) {
|
|
|
|
if (err != -EPERM && err != -ENODEV)
|
|
|
|
bt_dev_err(hdev, "urb %p submission failed (%d)",
|
|
|
|
urb, -err);
|
|
|
|
usb_unanchor_urb(urb);
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_free_urb(urb);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int btmtk_usb_hci_wmt_sync(struct hci_dev *hdev,
|
|
|
|
struct btmtk_hci_wmt_params *wmt_params)
|
|
|
|
{
|
|
|
|
struct btmtk_data *data = hci_get_priv(hdev);
|
|
|
|
struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
|
|
|
|
u32 hlen, status = BTMTK_WMT_INVALID;
|
|
|
|
struct btmtk_hci_wmt_evt *wmt_evt;
|
|
|
|
struct btmtk_hci_wmt_cmd *wc;
|
|
|
|
struct btmtk_wmt_hdr *hdr;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Send the WMT command and wait until the WMT event returns */
|
|
|
|
hlen = sizeof(*hdr) + wmt_params->dlen;
|
|
|
|
if (hlen > 255)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
wc = kzalloc(hlen, GFP_KERNEL);
|
|
|
|
if (!wc)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
hdr = &wc->hdr;
|
|
|
|
hdr->dir = 1;
|
|
|
|
hdr->op = wmt_params->op;
|
|
|
|
hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
|
|
|
|
hdr->flag = wmt_params->flag;
|
|
|
|
memcpy(wc->data, wmt_params->data, wmt_params->dlen);
|
|
|
|
|
|
|
|
set_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags);
|
|
|
|
|
|
|
|
/* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
|
|
|
|
* it needs constantly polling control pipe until the host received the
|
|
|
|
* WMT event, thus, we should require to specifically acquire PM counter
|
|
|
|
* on the USB to prevent the interface from entering auto suspended
|
|
|
|
* while WMT cmd/event in progress.
|
|
|
|
*/
|
|
|
|
err = usb_autopm_get_interface(data->intf);
|
|
|
|
if (err < 0)
|
|
|
|
goto err_free_wc;
|
|
|
|
|
|
|
|
err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags);
|
|
|
|
usb_autopm_put_interface(data->intf);
|
|
|
|
goto err_free_wc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Submit control IN URB on demand to process the WMT event */
|
|
|
|
err = btmtk_usb_submit_wmt_recv_urb(hdev);
|
|
|
|
|
|
|
|
usb_autopm_put_interface(data->intf);
|
|
|
|
|
|
|
|
if (err < 0)
|
|
|
|
goto err_free_wc;
|
|
|
|
|
|
|
|
/* The vendor specific WMT commands are all answered by a vendor
|
|
|
|
* specific event and will have the Command Status or Command
|
|
|
|
* Complete as with usual HCI command flow control.
|
|
|
|
*
|
|
|
|
* After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
|
|
|
|
* state to be cleared. The driver specific event receive routine
|
|
|
|
* will clear that state and with that indicate completion of the
|
|
|
|
* WMT command.
|
|
|
|
*/
|
|
|
|
err = wait_on_bit_timeout(&data->flags, BTMTK_TX_WAIT_VND_EVT,
|
|
|
|
TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
|
|
|
|
if (err == -EINTR) {
|
|
|
|
bt_dev_err(hdev, "Execution of wmt command interrupted");
|
|
|
|
clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags);
|
|
|
|
goto err_free_wc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
bt_dev_err(hdev, "Execution of wmt command timed out");
|
|
|
|
clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags);
|
|
|
|
err = -ETIMEDOUT;
|
|
|
|
goto err_free_wc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->evt_skb == NULL)
|
|
|
|
goto err_free_wc;
|
|
|
|
|
|
|
|
/* Parse and handle the return WMT event */
|
|
|
|
wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
|
|
|
|
if (wmt_evt->whdr.op != hdr->op) {
|
|
|
|
bt_dev_err(hdev, "Wrong op received %d expected %d",
|
|
|
|
wmt_evt->whdr.op, hdr->op);
|
|
|
|
err = -EIO;
|
|
|
|
goto err_free_skb;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (wmt_evt->whdr.op) {
|
|
|
|
case BTMTK_WMT_SEMAPHORE:
|
|
|
|
if (wmt_evt->whdr.flag == 2)
|
|
|
|
status = BTMTK_WMT_PATCH_UNDONE;
|
|
|
|
else
|
|
|
|
status = BTMTK_WMT_PATCH_DONE;
|
|
|
|
break;
|
|
|
|
case BTMTK_WMT_FUNC_CTRL:
|
|
|
|
wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
|
|
|
|
if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
|
|
|
|
status = BTMTK_WMT_ON_DONE;
|
|
|
|
else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
|
|
|
|
status = BTMTK_WMT_ON_PROGRESS;
|
|
|
|
else
|
|
|
|
status = BTMTK_WMT_ON_UNDONE;
|
|
|
|
break;
|
|
|
|
case BTMTK_WMT_PATCH_DWNLD:
|
|
|
|
if (wmt_evt->whdr.flag == 2)
|
|
|
|
status = BTMTK_WMT_PATCH_DONE;
|
|
|
|
else if (wmt_evt->whdr.flag == 1)
|
|
|
|
status = BTMTK_WMT_PATCH_PROGRESS;
|
|
|
|
else
|
|
|
|
status = BTMTK_WMT_PATCH_UNDONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wmt_params->status)
|
|
|
|
*wmt_params->status = status;
|
|
|
|
|
|
|
|
err_free_skb:
|
|
|
|
kfree_skb(data->evt_skb);
|
|
|
|
data->evt_skb = NULL;
|
|
|
|
err_free_wc:
|
|
|
|
kfree(wc);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(btmtk_usb_hci_wmt_sync);
|
|
|
|
|
2021-10-18 21:30:12 +00:00
|
|
|
MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
|
|
|
|
MODULE_AUTHOR("Mark Chen <mark-yw.chen@mediatek.com>");
|
|
|
|
MODULE_DESCRIPTION("Bluetooth support for MediaTek devices ver " VERSION);
|
|
|
|
MODULE_VERSION(VERSION);
|
|
|
|
MODULE_LICENSE("GPL");
|
2022-03-09 00:03:52 +00:00
|
|
|
MODULE_FIRMWARE(FIRMWARE_MT7622);
|
2021-10-18 21:30:12 +00:00
|
|
|
MODULE_FIRMWARE(FIRMWARE_MT7663);
|
|
|
|
MODULE_FIRMWARE(FIRMWARE_MT7668);
|
2024-02-27 10:29:14 +00:00
|
|
|
MODULE_FIRMWARE(FIRMWARE_MT7922);
|
2021-10-18 21:30:12 +00:00
|
|
|
MODULE_FIRMWARE(FIRMWARE_MT7961);
|
2023-06-20 08:27:50 +00:00
|
|
|
MODULE_FIRMWARE(FIRMWARE_MT7925);
|