hci_vhci.c 6.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *
3 4
 *  Bluetooth virtual HCI driver
 *
5 6 7
 *  Copyright (C) 2000-2001  Qualcomm Incorporated
 *  Copyright (C) 2002-2003  Maxim Krasnyansky <maxk@qualcomm.com>
 *  Copyright (C) 2004-2006  Marcel Holtmann <marcel@holtmann.org>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
L
Linus Torvalds 已提交
24 25 26 27 28
 */

#include <linux/module.h>

#include <linux/kernel.h>
29
#include <linux/init.h>
L
Linus Torvalds 已提交
30
#include <linux/slab.h>
31 32 33
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/sched.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41
#include <linux/poll.h>

#include <linux/skbuff.h>
#include <linux/miscdevice.h>

#include <net/bluetooth/bluetooth.h>
#include <net/bluetooth/hci_core.h>

42
#define VERSION "1.3"
43

44 45
static bool amp;

46 47 48 49 50 51 52 53 54 55
struct vhci_data {
	struct hci_dev *hdev;

	unsigned long flags;

	wait_queue_head_t read_wait;
	struct sk_buff_head readq;
};

static int vhci_open_dev(struct hci_dev *hdev)
L
Linus Torvalds 已提交
56 57 58 59 60 61
{
	set_bit(HCI_RUNNING, &hdev->flags);

	return 0;
}

62
static int vhci_close_dev(struct hci_dev *hdev)
L
Linus Torvalds 已提交
63
{
64
	struct vhci_data *data = hci_get_drvdata(hdev);
65

L
Linus Torvalds 已提交
66 67 68
	if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
		return 0;

69
	skb_queue_purge(&data->readq);
70

L
Linus Torvalds 已提交
71 72 73
	return 0;
}

74
static int vhci_flush(struct hci_dev *hdev)
L
Linus Torvalds 已提交
75
{
76
	struct vhci_data *data = hci_get_drvdata(hdev);
L
Linus Torvalds 已提交
77

78
	skb_queue_purge(&data->readq);
L
Linus Torvalds 已提交
79

80
	return 0;
L
Linus Torvalds 已提交
81 82
}

83
static int vhci_send_frame(struct sk_buff *skb)
L
Linus Torvalds 已提交
84 85
{
	struct hci_dev* hdev = (struct hci_dev *) skb->dev;
86
	struct vhci_data *data;
L
Linus Torvalds 已提交
87 88

	if (!hdev) {
89
		BT_ERR("Frame for unknown HCI device (hdev=NULL)");
L
Linus Torvalds 已提交
90 91 92 93 94 95
		return -ENODEV;
	}

	if (!test_bit(HCI_RUNNING, &hdev->flags))
		return -EBUSY;

96
	data = hci_get_drvdata(hdev);
L
Linus Torvalds 已提交
97

98
	memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
99
	skb_queue_tail(&data->readq, skb);
100

101
	wake_up_interruptible(&data->read_wait);
L
Linus Torvalds 已提交
102 103 104 105

	return 0;
}

106
static inline ssize_t vhci_get_user(struct vhci_data *data,
107
					const char __user *buf, size_t count)
L
Linus Torvalds 已提交
108 109 110 111 112 113
{
	struct sk_buff *skb;

	if (count > HCI_MAX_FRAME_SIZE)
		return -EINVAL;

114 115
	skb = bt_skb_alloc(count, GFP_KERNEL);
	if (!skb)
L
Linus Torvalds 已提交
116
		return -ENOMEM;
117

L
Linus Torvalds 已提交
118 119 120 121 122
	if (copy_from_user(skb_put(skb, count), buf, count)) {
		kfree_skb(skb);
		return -EFAULT;
	}

123
	skb->dev = (void *) data->hdev;
124
	bt_cb(skb)->pkt_type = *((__u8 *) skb->data);
L
Linus Torvalds 已提交
125 126 127 128 129 130 131
	skb_pull(skb, 1);

	hci_recv_frame(skb);

	return count;
}

132
static inline ssize_t vhci_put_user(struct vhci_data *data,
133
			struct sk_buff *skb, char __user *buf, int count)
L
Linus Torvalds 已提交
134 135
{
	char __user *ptr = buf;
136 137 138
	int len, total = 0;

	len = min_t(unsigned int, skb->len, count);
L
Linus Torvalds 已提交
139 140 141

	if (copy_to_user(ptr, skb->data, len))
		return -EFAULT;
142

L
Linus Torvalds 已提交
143 144
	total += len;

145
	data->hdev->stat.byte_tx += len;
146

147 148
	switch (bt_cb(skb)->pkt_type) {
	case HCI_COMMAND_PKT:
149
		data->hdev->stat.cmd_tx++;
150
		break;
L
Linus Torvalds 已提交
151

152
	case HCI_ACLDATA_PKT:
153
		data->hdev->stat.acl_tx++;
154
		break;
L
Linus Torvalds 已提交
155

156
	case HCI_SCODATA_PKT:
157
		data->hdev->stat.sco_tx++;
158
		break;
L
Linus Torvalds 已提交
159 160 161 162 163
	};

	return total;
}

164 165
static ssize_t vhci_read(struct file *file,
				char __user *buf, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
166
{
167
	struct vhci_data *data = file->private_data;
L
Linus Torvalds 已提交
168 169 170 171
	struct sk_buff *skb;
	ssize_t ret = 0;

	while (count) {
172
		skb = skb_dequeue(&data->readq);
173 174 175 176 177 178 179
		if (skb) {
			ret = vhci_put_user(data, skb, buf, count);
			if (ret < 0)
				skb_queue_head(&data->readq, skb);
			else
				kfree_skb(skb);
			break;
L
Linus Torvalds 已提交
180 181
		}

182 183 184 185
		if (file->f_flags & O_NONBLOCK) {
			ret = -EAGAIN;
			break;
		}
L
Linus Torvalds 已提交
186

187 188 189 190
		ret = wait_event_interruptible(data->read_wait,
					!skb_queue_empty(&data->readq));
		if (ret < 0)
			break;
L
Linus Torvalds 已提交
191 192 193 194 195
	}

	return ret;
}

196 197
static ssize_t vhci_write(struct file *file,
			const char __user *buf, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
198
{
199
	struct vhci_data *data = file->private_data;
L
Linus Torvalds 已提交
200

201
	return vhci_get_user(data, buf, count);
L
Linus Torvalds 已提交
202 203
}

204
static unsigned int vhci_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
205
{
206
	struct vhci_data *data = file->private_data;
L
Linus Torvalds 已提交
207

208
	poll_wait(file, &data->read_wait, wait);
L
Linus Torvalds 已提交
209

210
	if (!skb_queue_empty(&data->readq))
211 212 213 214 215 216
		return POLLIN | POLLRDNORM;

	return POLLOUT | POLLWRNORM;
}

static int vhci_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
217
{
218
	struct vhci_data *data;
L
Linus Torvalds 已提交
219 220
	struct hci_dev *hdev;

221 222
	data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
	if (!data)
L
Linus Torvalds 已提交
223 224
		return -ENOMEM;

225 226
	skb_queue_head_init(&data->readq);
	init_waitqueue_head(&data->read_wait);
L
Linus Torvalds 已提交
227 228 229

	hdev = hci_alloc_dev();
	if (!hdev) {
230
		kfree(data);
L
Linus Torvalds 已提交
231 232 233
		return -ENOMEM;
	}

234
	data->hdev = hdev;
L
Linus Torvalds 已提交
235

236
	hdev->bus = HCI_VIRTUAL;
237
	hci_set_drvdata(hdev, data);
L
Linus Torvalds 已提交
238

239 240 241
	if (amp)
		hdev->dev_type = HCI_AMP;

242 243 244 245
	hdev->open     = vhci_open_dev;
	hdev->close    = vhci_close_dev;
	hdev->flush    = vhci_flush;
	hdev->send     = vhci_send_frame;
L
Linus Torvalds 已提交
246 247

	if (hci_register_dev(hdev) < 0) {
248
		BT_ERR("Can't register HCI device");
249
		kfree(data);
L
Linus Torvalds 已提交
250 251 252 253
		hci_free_dev(hdev);
		return -EBUSY;
	}

254
	file->private_data = data;
255 256

	return nonseekable_open(inode, file);
L
Linus Torvalds 已提交
257 258
}

259
static int vhci_release(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
260
{
261 262
	struct vhci_data *data = file->private_data;
	struct hci_dev *hdev = data->hdev;
L
Linus Torvalds 已提交
263

264
	hci_unregister_dev(hdev);
L
Linus Torvalds 已提交
265 266 267
	hci_free_dev(hdev);

	file->private_data = NULL;
268
	kfree(data);
269 270 271 272

	return 0;
}

273
static const struct file_operations vhci_fops = {
274
	.owner		= THIS_MODULE,
275 276 277 278 279
	.read		= vhci_read,
	.write		= vhci_write,
	.poll		= vhci_poll,
	.open		= vhci_open,
	.release	= vhci_release,
280
	.llseek		= no_llseek,
L
Linus Torvalds 已提交
281 282
};

283
static struct miscdevice vhci_miscdev= {
284 285 286
	.name	= "vhci",
	.fops	= &vhci_fops,
	.minor	= MISC_DYNAMIC_MINOR,
L
Linus Torvalds 已提交
287 288
};

289
static int __init vhci_init(void)
L
Linus Torvalds 已提交
290
{
291
	BT_INFO("Virtual HCI driver ver %s", VERSION);
L
Linus Torvalds 已提交
292

293
	return misc_register(&vhci_miscdev);
L
Linus Torvalds 已提交
294 295
}

296
static void __exit vhci_exit(void)
L
Linus Torvalds 已提交
297
{
298
	misc_deregister(&vhci_miscdev);
L
Linus Torvalds 已提交
299 300
}

301 302 303
module_init(vhci_init);
module_exit(vhci_exit);

304 305 306
module_param(amp, bool, 0644);
MODULE_PARM_DESC(amp, "Create AMP controller device");

307
MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
308 309 310
MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");