2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
*
|
2005-08-10 11:27:37 +08:00
|
|
|
* Bluetooth virtual HCI driver
|
|
|
|
*
|
2006-07-06 21:45:23 +08:00
|
|
|
* Copyright (C) 2000-2001 Qualcomm Incorporated
|
|
|
|
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
|
|
|
|
* Copyright (C) 2004-2006 Marcel Holtmann <marcel@holtmann.org>
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
2024-10-02 03:35:57 +08:00
|
|
|
#include <linux/unaligned.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2023-11-10 09:46:05 +08:00
|
|
|
#include <linux/atomic.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/kernel.h>
|
2005-08-10 11:27:37 +08:00
|
|
|
#include <linux/init.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/slab.h>
|
2005-08-10 11:27:37 +08:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/poll.h>
|
|
|
|
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/miscdevice.h>
|
2021-09-30 04:38:54 +08:00
|
|
|
#include <linux/debugfs.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#include <net/bluetooth/bluetooth.h>
|
|
|
|
#include <net/bluetooth/hci_core.h>
|
|
|
|
|
2014-07-03 07:35:10 +08:00
|
|
|
#define VERSION "1.5"
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2011-11-14 18:42:48 +08:00
|
|
|
static bool amp;
|
|
|
|
|
2005-08-10 11:27:37 +08:00
|
|
|
struct vhci_data {
|
|
|
|
struct hci_dev *hdev;
|
|
|
|
|
|
|
|
wait_queue_head_t read_wait;
|
|
|
|
struct sk_buff_head readq;
|
2013-09-03 01:41:39 +08:00
|
|
|
|
2016-04-14 23:32:19 +08:00
|
|
|
struct mutex open_mutex;
|
2013-09-03 01:41:39 +08:00
|
|
|
struct delayed_work open_timeout;
|
2021-10-06 09:09:33 +08:00
|
|
|
struct work_struct suspend_work;
|
2021-09-30 04:38:54 +08:00
|
|
|
|
|
|
|
bool suspended;
|
2021-10-02 04:22:31 +08:00
|
|
|
bool wakeup;
|
2021-10-14 06:17:01 +08:00
|
|
|
__u16 msft_opcode;
|
|
|
|
bool aosp_capable;
|
2023-11-10 09:46:05 +08:00
|
|
|
atomic_t initialized;
|
2005-08-10 11:27:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int vhci_open_dev(struct hci_dev *hdev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-10 11:27:37 +08:00
|
|
|
static int vhci_close_dev(struct hci_dev *hdev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-02-10 04:58:32 +08:00
|
|
|
struct vhci_data *data = hci_get_drvdata(hdev);
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
skb_queue_purge(&data->readq);
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-10 11:27:37 +08:00
|
|
|
static int vhci_flush(struct hci_dev *hdev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-02-10 04:58:32 +08:00
|
|
|
struct vhci_data *data = hci_get_drvdata(hdev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
skb_queue_purge(&data->readq);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-08-10 11:27:37 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-10-11 21:19:18 +08:00
|
|
|
static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2013-10-11 22:01:04 +08:00
|
|
|
struct vhci_data *data = hci_get_drvdata(hdev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-11-05 14:33:56 +08:00
|
|
|
memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
|
2023-09-20 23:30:07 +08:00
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
skb_queue_tail(&data->readq, skb);
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2023-11-10 09:46:05 +08:00
|
|
|
if (atomic_read(&data->initialized))
|
|
|
|
wake_up_interruptible(&data->read_wait);
|
2013-09-03 01:41:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:12:49 +08:00
|
|
|
static int vhci_get_data_path_id(struct hci_dev *hdev, u8 *data_path_id)
|
|
|
|
{
|
|
|
|
*data_path_id = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vhci_get_codec_config_data(struct hci_dev *hdev, __u8 type,
|
|
|
|
struct bt_codec *codec, __u8 *vnd_len,
|
|
|
|
__u8 **vnd_data)
|
|
|
|
{
|
|
|
|
if (type != ESCO_LINK)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*vnd_len = 0;
|
|
|
|
*vnd_data = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-02 04:22:31 +08:00
|
|
|
static bool vhci_wakeup(struct hci_dev *hdev)
|
2021-09-30 04:38:55 +08:00
|
|
|
{
|
|
|
|
struct vhci_data *data = hci_get_drvdata(hdev);
|
|
|
|
|
2021-10-02 04:22:31 +08:00
|
|
|
return data->wakeup;
|
2021-09-30 04:38:55 +08:00
|
|
|
}
|
|
|
|
|
2021-09-30 04:38:54 +08:00
|
|
|
static ssize_t force_suspend_read(struct file *file, char __user *user_buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct vhci_data *data = file->private_data;
|
|
|
|
char buf[3];
|
|
|
|
|
|
|
|
buf[0] = data->suspended ? 'Y' : 'N';
|
|
|
|
buf[1] = '\n';
|
|
|
|
buf[2] = '\0';
|
|
|
|
return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
|
|
|
|
}
|
|
|
|
|
2021-10-06 09:09:33 +08:00
|
|
|
static void vhci_suspend_work(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct vhci_data *data = container_of(work, struct vhci_data,
|
|
|
|
suspend_work);
|
|
|
|
|
|
|
|
if (data->suspended)
|
|
|
|
hci_suspend_dev(data->hdev);
|
|
|
|
else
|
|
|
|
hci_resume_dev(data->hdev);
|
|
|
|
}
|
|
|
|
|
2021-09-30 04:38:54 +08:00
|
|
|
static ssize_t force_suspend_write(struct file *file,
|
|
|
|
const char __user *user_buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct vhci_data *data = file->private_data;
|
|
|
|
bool enable;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtobool_from_user(user_buf, count, &enable);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (data->suspended == enable)
|
|
|
|
return -EALREADY;
|
|
|
|
|
|
|
|
data->suspended = enable;
|
|
|
|
|
2021-10-06 09:09:33 +08:00
|
|
|
schedule_work(&data->suspend_work);
|
|
|
|
|
2021-09-30 04:38:54 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations force_suspend_fops = {
|
|
|
|
.open = simple_open,
|
|
|
|
.read = force_suspend_read,
|
|
|
|
.write = force_suspend_write,
|
|
|
|
.llseek = default_llseek,
|
|
|
|
};
|
|
|
|
|
2021-10-02 04:22:31 +08:00
|
|
|
static ssize_t force_wakeup_read(struct file *file, char __user *user_buf,
|
|
|
|
size_t count, loff_t *ppos)
|
2021-09-30 04:38:55 +08:00
|
|
|
{
|
|
|
|
struct vhci_data *data = file->private_data;
|
|
|
|
char buf[3];
|
|
|
|
|
2021-10-02 04:22:31 +08:00
|
|
|
buf[0] = data->wakeup ? 'Y' : 'N';
|
2021-09-30 04:38:55 +08:00
|
|
|
buf[1] = '\n';
|
|
|
|
buf[2] = '\0';
|
|
|
|
return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
|
|
|
|
}
|
|
|
|
|
2021-10-02 04:22:31 +08:00
|
|
|
static ssize_t force_wakeup_write(struct file *file,
|
|
|
|
const char __user *user_buf, size_t count,
|
|
|
|
loff_t *ppos)
|
2021-09-30 04:38:55 +08:00
|
|
|
{
|
|
|
|
struct vhci_data *data = file->private_data;
|
|
|
|
bool enable;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtobool_from_user(user_buf, count, &enable);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2021-10-02 04:22:31 +08:00
|
|
|
if (data->wakeup == enable)
|
2021-09-30 04:38:55 +08:00
|
|
|
return -EALREADY;
|
|
|
|
|
2021-10-05 01:01:26 +08:00
|
|
|
data->wakeup = enable;
|
|
|
|
|
2021-09-30 04:38:55 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2021-10-02 04:22:31 +08:00
|
|
|
static const struct file_operations force_wakeup_fops = {
|
2021-09-30 04:38:55 +08:00
|
|
|
.open = simple_open,
|
2021-10-02 04:22:31 +08:00
|
|
|
.read = force_wakeup_read,
|
|
|
|
.write = force_wakeup_write,
|
2021-09-30 04:38:55 +08:00
|
|
|
.llseek = default_llseek,
|
|
|
|
};
|
|
|
|
|
2021-10-14 06:17:01 +08:00
|
|
|
static int msft_opcode_set(void *data, u64 val)
|
|
|
|
{
|
|
|
|
struct vhci_data *vhci = data;
|
|
|
|
|
2021-10-22 02:44:35 +08:00
|
|
|
if (val > 0xffff || hci_opcode_ogf(val) != 0x3f)
|
2021-10-14 06:17:01 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (vhci->msft_opcode)
|
|
|
|
return -EALREADY;
|
|
|
|
|
|
|
|
vhci->msft_opcode = val;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int msft_opcode_get(void *data, u64 *val)
|
|
|
|
{
|
|
|
|
struct vhci_data *vhci = data;
|
|
|
|
|
|
|
|
*val = vhci->msft_opcode;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_DEBUGFS_ATTRIBUTE(msft_opcode_fops, msft_opcode_get, msft_opcode_set,
|
|
|
|
"%llu\n");
|
|
|
|
|
|
|
|
static ssize_t aosp_capable_read(struct file *file, char __user *user_buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct vhci_data *vhci = file->private_data;
|
|
|
|
char buf[3];
|
|
|
|
|
|
|
|
buf[0] = vhci->aosp_capable ? 'Y' : 'N';
|
|
|
|
buf[1] = '\n';
|
|
|
|
buf[2] = '\0';
|
|
|
|
return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t aosp_capable_write(struct file *file,
|
|
|
|
const char __user *user_buf, size_t count,
|
|
|
|
loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct vhci_data *vhci = file->private_data;
|
|
|
|
bool enable;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtobool_from_user(user_buf, count, &enable);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!enable)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (vhci->aosp_capable)
|
|
|
|
return -EALREADY;
|
|
|
|
|
|
|
|
vhci->aosp_capable = enable;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations aosp_capable_fops = {
|
|
|
|
.open = simple_open,
|
|
|
|
.read = aosp_capable_read,
|
|
|
|
.write = aosp_capable_write,
|
|
|
|
.llseek = default_llseek,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int vhci_setup(struct hci_dev *hdev)
|
|
|
|
{
|
|
|
|
struct vhci_data *vhci = hci_get_drvdata(hdev);
|
|
|
|
|
|
|
|
if (vhci->msft_opcode)
|
|
|
|
hci_set_msft_opcode(hdev, vhci->msft_opcode);
|
|
|
|
|
|
|
|
if (vhci->aosp_capable)
|
|
|
|
hci_set_aosp_capable(hdev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-31 00:58:24 +08:00
|
|
|
static void vhci_coredump(struct hci_dev *hdev)
|
|
|
|
{
|
|
|
|
/* No need to do anything */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vhci_coredump_hdr(struct hci_dev *hdev, struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
char buf[80];
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "Controller Name: vhci_ctrl\n");
|
|
|
|
skb_put_data(skb, buf, strlen(buf));
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "Firmware Version: vhci_fw\n");
|
|
|
|
skb_put_data(skb, buf, strlen(buf));
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "Driver: vhci_drv\n");
|
|
|
|
skb_put_data(skb, buf, strlen(buf));
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "Vendor: vhci\n");
|
|
|
|
skb_put_data(skb, buf, strlen(buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_COREDUMP_LINE_LEN 40
|
|
|
|
|
|
|
|
struct devcoredump_test_data {
|
|
|
|
enum devcoredump_state state;
|
|
|
|
unsigned int timeout;
|
|
|
|
char data[MAX_COREDUMP_LINE_LEN];
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void force_devcd_timeout(struct hci_dev *hdev,
|
|
|
|
unsigned int timeout)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEV_COREDUMP
|
|
|
|
hdev->dump.timeout = msecs_to_jiffies(timeout * 1000);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t force_devcd_write(struct file *file, const char __user *user_buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct vhci_data *data = file->private_data;
|
|
|
|
struct hci_dev *hdev = data->hdev;
|
|
|
|
struct sk_buff *skb = NULL;
|
|
|
|
struct devcoredump_test_data dump_data;
|
2023-04-06 16:55:17 +08:00
|
|
|
size_t data_size;
|
2023-03-31 00:58:24 +08:00
|
|
|
int ret;
|
|
|
|
|
2023-04-06 16:55:17 +08:00
|
|
|
if (count < offsetof(struct devcoredump_test_data, data) ||
|
|
|
|
count > sizeof(dump_data))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (copy_from_user(&dump_data, user_buf, count))
|
|
|
|
return -EFAULT;
|
2023-03-31 00:58:24 +08:00
|
|
|
|
2023-04-06 16:55:17 +08:00
|
|
|
data_size = count - offsetof(struct devcoredump_test_data, data);
|
|
|
|
skb = alloc_skb(data_size, GFP_ATOMIC);
|
2023-03-31 00:58:24 +08:00
|
|
|
if (!skb)
|
|
|
|
return -ENOMEM;
|
2023-04-06 16:55:17 +08:00
|
|
|
skb_put_data(skb, &dump_data.data, data_size);
|
2023-03-31 00:58:24 +08:00
|
|
|
|
|
|
|
hci_devcd_register(hdev, vhci_coredump, vhci_coredump_hdr, NULL);
|
|
|
|
|
|
|
|
/* Force the devcoredump timeout */
|
|
|
|
if (dump_data.timeout)
|
|
|
|
force_devcd_timeout(hdev, dump_data.timeout);
|
|
|
|
|
|
|
|
ret = hci_devcd_init(hdev, skb->len);
|
|
|
|
if (ret) {
|
|
|
|
BT_ERR("Failed to generate devcoredump");
|
|
|
|
kfree_skb(skb);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
hci_devcd_append(hdev, skb);
|
|
|
|
|
|
|
|
switch (dump_data.state) {
|
|
|
|
case HCI_DEVCOREDUMP_DONE:
|
|
|
|
hci_devcd_complete(hdev);
|
|
|
|
break;
|
|
|
|
case HCI_DEVCOREDUMP_ABORT:
|
|
|
|
hci_devcd_abort(hdev);
|
|
|
|
break;
|
|
|
|
case HCI_DEVCOREDUMP_TIMEOUT:
|
|
|
|
/* Do nothing */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations force_devcoredump_fops = {
|
|
|
|
.open = simple_open,
|
|
|
|
.write = force_devcd_write,
|
|
|
|
};
|
|
|
|
|
2016-04-14 23:32:19 +08:00
|
|
|
static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
|
2013-09-03 01:41:39 +08:00
|
|
|
{
|
|
|
|
struct hci_dev *hdev;
|
|
|
|
struct sk_buff *skb;
|
2014-07-03 07:35:10 +08:00
|
|
|
|
2016-04-14 23:32:19 +08:00
|
|
|
if (data->hdev)
|
|
|
|
return -EBADFD;
|
|
|
|
|
2014-07-04 23:23:35 +08:00
|
|
|
/* bits 2-5 are reserved (must be zero) */
|
|
|
|
if (opcode & 0x3c)
|
2014-07-03 07:35:10 +08:00
|
|
|
return -EINVAL;
|
2013-09-03 01:41:39 +08:00
|
|
|
|
|
|
|
skb = bt_skb_alloc(4, GFP_KERNEL);
|
|
|
|
if (!skb)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
hdev = hci_alloc_dev();
|
|
|
|
if (!hdev) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->hdev = hdev;
|
|
|
|
|
|
|
|
hdev->bus = HCI_VIRTUAL;
|
|
|
|
hci_set_drvdata(hdev, data);
|
|
|
|
|
|
|
|
hdev->open = vhci_open_dev;
|
|
|
|
hdev->close = vhci_close_dev;
|
|
|
|
hdev->flush = vhci_flush;
|
|
|
|
hdev->send = vhci_send_frame;
|
2021-09-07 18:12:49 +08:00
|
|
|
hdev->get_data_path_id = vhci_get_data_path_id;
|
|
|
|
hdev->get_codec_config_data = vhci_get_codec_config_data;
|
2021-10-02 04:22:31 +08:00
|
|
|
hdev->wakeup = vhci_wakeup;
|
2021-10-14 06:17:01 +08:00
|
|
|
hdev->setup = vhci_setup;
|
|
|
|
set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-07-04 23:23:35 +08:00
|
|
|
/* bit 6 is for external configuration */
|
|
|
|
if (opcode & 0x40)
|
|
|
|
set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
|
|
|
|
|
2014-07-03 07:35:10 +08:00
|
|
|
/* bit 7 is for raw device */
|
|
|
|
if (opcode & 0x80)
|
|
|
|
set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
|
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
if (hci_register_dev(hdev) < 0) {
|
|
|
|
BT_ERR("Can't register HCI device");
|
|
|
|
hci_free_dev(hdev);
|
|
|
|
data->hdev = NULL;
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2021-09-30 04:38:54 +08:00
|
|
|
debugfs_create_file("force_suspend", 0644, hdev->debugfs, data,
|
|
|
|
&force_suspend_fops);
|
|
|
|
|
2021-10-02 04:22:31 +08:00
|
|
|
debugfs_create_file("force_wakeup", 0644, hdev->debugfs, data,
|
|
|
|
&force_wakeup_fops);
|
2021-09-30 04:38:55 +08:00
|
|
|
|
2021-10-14 06:17:01 +08:00
|
|
|
if (IS_ENABLED(CONFIG_BT_MSFTEXT))
|
|
|
|
debugfs_create_file("msft_opcode", 0644, hdev->debugfs, data,
|
|
|
|
&msft_opcode_fops);
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_BT_AOSPEXT))
|
|
|
|
debugfs_create_file("aosp_capable", 0644, hdev->debugfs, data,
|
|
|
|
&aosp_capable_fops);
|
|
|
|
|
2023-03-31 00:58:24 +08:00
|
|
|
debugfs_create_file("force_devcoredump", 0644, hdev->debugfs, data,
|
|
|
|
&force_devcoredump_fops);
|
|
|
|
|
2015-11-05 14:33:56 +08:00
|
|
|
hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
|
2013-09-03 01:41:39 +08:00
|
|
|
|
networking: add and use skb_put_u8()
Joe and Bjørn suggested that it'd be nicer to not have the
cast in the fairly common case of doing
*(u8 *)skb_put(skb, 1) = c;
Add skb_put_u8() for this case, and use it across the code,
using the following spatch:
@@
expression SKB, C, S;
typedef u8;
identifier fn = {skb_put};
fresh identifier fn2 = fn ## "_u8";
@@
- *(u8 *)fn(SKB, S) = C;
+ fn2(SKB, C);
Note that due to the "S", the spatch isn't perfect, it should
have checked that S is 1, but there's also places that use a
sizeof expression like sizeof(var) or sizeof(u8) etc. Turns
out that nobody ever did something like
*(u8 *)skb_put(skb, 2) = c;
which would be wrong anyway since the second byte wouldn't be
initialized.
Suggested-by: Joe Perches <joe@perches.com>
Suggested-by: Bjørn Mork <bjorn@mork.no>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 20:29:24 +08:00
|
|
|
skb_put_u8(skb, 0xff);
|
|
|
|
skb_put_u8(skb, opcode);
|
2013-09-03 01:41:39 +08:00
|
|
|
put_unaligned_le16(hdev->id, skb_put(skb, 2));
|
2023-11-10 09:46:05 +08:00
|
|
|
skb_queue_head(&data->readq, skb);
|
|
|
|
atomic_inc(&data->initialized);
|
2013-09-03 01:41:39 +08:00
|
|
|
|
|
|
|
wake_up_interruptible(&data->read_wait);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-14 23:32:19 +08:00
|
|
|
static int vhci_create_device(struct vhci_data *data, __u8 opcode)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_lock(&data->open_mutex);
|
|
|
|
err = __vhci_create_device(data, opcode);
|
|
|
|
mutex_unlock(&data->open_mutex);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
static inline ssize_t vhci_get_user(struct vhci_data *data,
|
2014-08-23 23:28:14 +08:00
|
|
|
struct iov_iter *from)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2014-08-23 23:28:14 +08:00
|
|
|
size_t len = iov_iter_count(from);
|
2005-04-17 06:20:36 +08:00
|
|
|
struct sk_buff *skb;
|
2014-07-03 07:35:10 +08:00
|
|
|
__u8 pkt_type, opcode;
|
2013-09-03 01:41:39 +08:00
|
|
|
int ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-12-29 13:57:14 +08:00
|
|
|
if (len < 2 || len > HCI_MAX_FRAME_SIZE)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2013-12-29 13:57:14 +08:00
|
|
|
skb = bt_skb_alloc(len, GFP_KERNEL);
|
2005-08-10 11:27:37 +08:00
|
|
|
if (!skb)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2016-11-02 10:09:04 +08:00
|
|
|
if (!copy_from_iter_full(skb_put(skb, len), len, from)) {
|
2014-08-23 23:28:14 +08:00
|
|
|
kfree_skb(skb);
|
|
|
|
return -EFAULT;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
pkt_type = *((__u8 *) skb->data);
|
2005-04-17 06:20:36 +08:00
|
|
|
skb_pull(skb, 1);
|
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
switch (pkt_type) {
|
|
|
|
case HCI_EVENT_PKT:
|
|
|
|
case HCI_ACLDATA_PKT:
|
|
|
|
case HCI_SCODATA_PKT:
|
2020-01-16 05:02:16 +08:00
|
|
|
case HCI_ISODATA_PKT:
|
2013-09-03 01:41:39 +08:00
|
|
|
if (!data->hdev) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2015-11-05 14:33:56 +08:00
|
|
|
hci_skb_pkt_type(skb) = pkt_type;
|
2013-09-03 01:41:39 +08:00
|
|
|
|
2013-10-11 07:52:43 +08:00
|
|
|
ret = hci_recv_frame(data->hdev, skb);
|
2013-09-03 01:41:39 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_VENDOR_PKT:
|
2016-03-19 18:05:18 +08:00
|
|
|
cancel_delayed_work_sync(&data->open_timeout);
|
|
|
|
|
2014-07-03 07:35:10 +08:00
|
|
|
opcode = *((__u8 *) skb->data);
|
2013-09-03 01:41:39 +08:00
|
|
|
skb_pull(skb, 1);
|
|
|
|
|
|
|
|
if (skb->len > 0) {
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree_skb(skb);
|
|
|
|
|
2014-07-03 07:35:10 +08:00
|
|
|
ret = vhci_create_device(data, opcode);
|
2013-09-03 01:41:39 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-12-29 13:57:14 +08:00
|
|
|
return (ret < 0) ? ret : len;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
static inline ssize_t vhci_put_user(struct vhci_data *data,
|
2013-09-03 01:41:39 +08:00
|
|
|
struct sk_buff *skb,
|
|
|
|
char __user *buf, int count)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
char __user *ptr = buf;
|
2013-09-03 01:41:39 +08:00
|
|
|
int len;
|
2005-08-10 11:27:37 +08:00
|
|
|
|
|
|
|
len = min_t(unsigned int, skb->len, count);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (copy_to_user(ptr, skb->data, len))
|
|
|
|
return -EFAULT;
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
if (!data->hdev)
|
|
|
|
return len;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
data->hdev->stat.byte_tx += len;
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2015-11-05 14:33:56 +08:00
|
|
|
switch (hci_skb_pkt_type(skb)) {
|
2005-08-10 11:30:28 +08:00
|
|
|
case HCI_COMMAND_PKT:
|
2006-07-06 21:45:23 +08:00
|
|
|
data->hdev->stat.cmd_tx++;
|
2005-08-10 11:30:28 +08:00
|
|
|
break;
|
|
|
|
case HCI_ACLDATA_PKT:
|
2006-07-06 21:45:23 +08:00
|
|
|
data->hdev->stat.acl_tx++;
|
2005-08-10 11:30:28 +08:00
|
|
|
break;
|
|
|
|
case HCI_SCODATA_PKT:
|
2010-05-02 03:15:34 +08:00
|
|
|
data->hdev->stat.sco_tx++;
|
2005-08-10 11:30:28 +08:00
|
|
|
break;
|
2012-09-07 23:24:47 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
return len;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
static ssize_t vhci_read(struct file *file,
|
2013-09-03 01:41:39 +08:00
|
|
|
char __user *buf, size_t count, loff_t *pos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-07-06 21:45:23 +08:00
|
|
|
struct vhci_data *data = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct sk_buff *skb;
|
|
|
|
ssize_t ret = 0;
|
|
|
|
|
|
|
|
while (count) {
|
2006-07-06 21:45:23 +08:00
|
|
|
skb = skb_dequeue(&data->readq);
|
2009-06-08 20:13:57 +08:00
|
|
|
if (skb) {
|
|
|
|
ret = vhci_put_user(data, skb, buf, count);
|
|
|
|
if (ret < 0)
|
|
|
|
skb_queue_head(&data->readq, skb);
|
|
|
|
else
|
|
|
|
kfree_skb(skb);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-06-08 20:13:57 +08:00
|
|
|
if (file->f_flags & O_NONBLOCK) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-06-08 20:13:57 +08:00
|
|
|
ret = wait_event_interruptible(data->read_wait,
|
2013-09-03 01:41:39 +08:00
|
|
|
!skb_queue_empty(&data->readq));
|
2009-06-08 20:13:57 +08:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-23 23:28:14 +08:00
|
|
|
static ssize_t vhci_write(struct kiocb *iocb, struct iov_iter *from)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2013-12-29 13:57:14 +08:00
|
|
|
struct file *file = iocb->ki_filp;
|
2006-07-06 21:45:23 +08:00
|
|
|
struct vhci_data *data = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-08-23 23:28:14 +08:00
|
|
|
return vhci_get_user(data, from);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2017-07-03 18:39:46 +08:00
|
|
|
static __poll_t vhci_poll(struct file *file, poll_table *wait)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-07-06 21:45:23 +08:00
|
|
|
struct vhci_data *data = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
poll_wait(file, &data->read_wait, wait);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
if (!skb_queue_empty(&data->readq))
|
2018-02-12 06:34:03 +08:00
|
|
|
return EPOLLIN | EPOLLRDNORM;
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2018-02-12 06:34:03 +08:00
|
|
|
return EPOLLOUT | EPOLLWRNORM;
|
2005-08-10 11:27:37 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
static void vhci_open_timeout(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct vhci_data *data = container_of(work, struct vhci_data,
|
|
|
|
open_timeout.work);
|
|
|
|
|
2024-05-07 06:33:52 +08:00
|
|
|
vhci_create_device(data, 0x00);
|
2013-09-03 01:41:39 +08:00
|
|
|
}
|
|
|
|
|
2005-08-10 11:27:37 +08:00
|
|
|
static int vhci_open(struct inode *inode, struct file *file)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-07-06 21:45:23 +08:00
|
|
|
struct vhci_data *data;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2024-05-25 01:11:51 +08:00
|
|
|
data = kzalloc(sizeof(*data), GFP_KERNEL);
|
2006-07-06 21:45:23 +08:00
|
|
|
if (!data)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
skb_queue_head_init(&data->readq);
|
|
|
|
init_waitqueue_head(&data->read_wait);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2016-04-14 23:32:19 +08:00
|
|
|
mutex_init(&data->open_mutex);
|
2013-09-03 01:41:39 +08:00
|
|
|
INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
|
2021-10-06 09:09:33 +08:00
|
|
|
INIT_WORK(&data->suspend_work, vhci_suspend_work);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-07-06 21:45:23 +08:00
|
|
|
file->private_data = data;
|
2012-03-28 17:48:42 +08:00
|
|
|
nonseekable_open(inode, file);
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
schedule_delayed_work(&data->open_timeout, msecs_to_jiffies(1000));
|
|
|
|
|
2012-03-28 17:48:42 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-08-10 11:27:37 +08:00
|
|
|
static int vhci_release(struct inode *inode, struct file *file)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-07-06 21:45:23 +08:00
|
|
|
struct vhci_data *data = file->private_data;
|
2016-03-19 18:05:18 +08:00
|
|
|
struct hci_dev *hdev;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
cancel_delayed_work_sync(&data->open_timeout);
|
2021-10-06 09:09:33 +08:00
|
|
|
flush_work(&data->suspend_work);
|
2013-09-03 01:41:39 +08:00
|
|
|
|
2016-03-19 18:05:18 +08:00
|
|
|
hdev = data->hdev;
|
|
|
|
|
2013-09-03 01:41:39 +08:00
|
|
|
if (hdev) {
|
|
|
|
hci_unregister_dev(hdev);
|
|
|
|
hci_free_dev(hdev);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
Bluetooth: vhci: purge unhandled skbs
The write handler allocates skbs and queues them into data->readq.
Read side should read them, if there is any. If there is none, skbs
should be dropped by hdev->flush. But this happens only if the device
is HCI_UP, i.e. hdev->power_on work was triggered already. When it was
not, skbs stay allocated in the queue when /dev/vhci is closed. So
purge the queue in ->release.
Program to reproduce:
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
int main()
{
char buf[] = { 0xff, 0 };
struct iovec iov = {
.iov_base = buf,
.iov_len = sizeof(buf),
};
int fd;
while (1) {
fd = open("/dev/vhci", O_RDWR);
if (fd < 0)
err(1, "open");
usleep(50);
if (writev(fd, &iov, 1) < 0)
err(1, "writev");
usleep(50);
close(fd);
}
return 0;
}
Result:
kmemleak: 4609 new suspected memory leaks
unreferenced object 0xffff88059f4d5440 (size 232):
comm "vhci", pid 1084, jiffies 4294912542 (age 37569.296s)
hex dump (first 32 bytes):
20 f0 23 87 05 88 ff ff 20 f0 23 87 05 88 ff ff .#..... .#.....
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace:
...
[<ffffffff81ece010>] __alloc_skb+0x0/0x5a0
[<ffffffffa021886c>] vhci_create_device+0x5c/0x580 [hci_vhci]
[<ffffffffa0219436>] vhci_write+0x306/0x4c8 [hci_vhci]
Fixes: 23424c0d31 (Bluetooth: Add support creating virtual AMP controllers)
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Cc: stable 3.13+ <stable@vger.kernel.org>
2016-03-19 18:49:43 +08:00
|
|
|
skb_queue_purge(&data->readq);
|
2005-04-17 06:20:36 +08:00
|
|
|
file->private_data = NULL;
|
2012-01-07 22:47:14 +08:00
|
|
|
kfree(data);
|
2005-08-10 11:27:37 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-12 16:55:32 +08:00
|
|
|
static const struct file_operations vhci_fops = {
|
2009-12-04 01:07:28 +08:00
|
|
|
.owner = THIS_MODULE,
|
2005-08-10 11:27:37 +08:00
|
|
|
.read = vhci_read,
|
2014-08-23 23:28:14 +08:00
|
|
|
.write_iter = vhci_write,
|
2005-08-10 11:27:37 +08:00
|
|
|
.poll = vhci_poll,
|
|
|
|
.open = vhci_open,
|
|
|
|
.release = vhci_release,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2015-06-02 18:50:15 +08:00
|
|
|
static struct miscdevice vhci_miscdev = {
|
2009-06-08 00:09:57 +08:00
|
|
|
.name = "vhci",
|
|
|
|
.fops = &vhci_fops,
|
2014-02-18 13:19:26 +08:00
|
|
|
.minor = VHCI_MINOR,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
2016-08-26 01:00:50 +08:00
|
|
|
module_misc_device(vhci_miscdev);
|
2005-08-10 11:27:37 +08:00
|
|
|
|
2011-11-14 18:42:48 +08:00
|
|
|
module_param(amp, bool, 0644);
|
|
|
|
MODULE_PARM_DESC(amp, "Create AMP controller device");
|
|
|
|
|
2008-08-18 19:23:53 +08:00
|
|
|
MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
|
2005-08-10 11:27:37 +08:00
|
|
|
MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
|
|
|
|
MODULE_VERSION(VERSION);
|
|
|
|
MODULE_LICENSE("GPL");
|
2013-08-27 13:02:38 +08:00
|
|
|
MODULE_ALIAS("devname:vhci");
|
2014-02-18 13:19:26 +08:00
|
|
|
MODULE_ALIAS_MISCDEV(VHCI_MINOR);
|