hci_vhci.c 8.1 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
 */

#include <linux/module.h>
27
#include <asm/unaligned.h>
L
Linus Torvalds 已提交
28 29

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

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

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

43
#define VERSION "1.5"
44

45 46
static bool amp;

47 48 49 50 51
struct vhci_data {
	struct hci_dev *hdev;

	wait_queue_head_t read_wait;
	struct sk_buff_head readq;
52 53

	struct delayed_work open_timeout;
54 55 56
};

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

	return 0;
}

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

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

70
	skb_queue_purge(&data->readq);
71

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

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

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

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

84
static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
L
Linus Torvalds 已提交
85
{
86
	struct vhci_data *data = hci_get_drvdata(hdev);
L
Linus Torvalds 已提交
87 88 89 90

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

91
	memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
92
	skb_queue_tail(&data->readq, skb);
93

94
	wake_up_interruptible(&data->read_wait);
95 96 97
	return 0;
}

98
static int vhci_create_device(struct vhci_data *data, __u8 opcode)
99 100 101
{
	struct hci_dev *hdev;
	struct sk_buff *skb;
102 103 104 105 106 107 108 109
	__u8 dev_type;

	/* bits 0-1 are dev_type (BR/EDR or AMP) */
	dev_type = opcode & 0x03;

	if (dev_type != HCI_BREDR && dev_type != HCI_AMP)
		return -EINVAL;

110 111
	/* bits 2-5 are reserved (must be zero) */
	if (opcode & 0x3c)
112
		return -EINVAL;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	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;
	hdev->dev_type = dev_type;
	hci_set_drvdata(hdev, data);

	hdev->open  = vhci_open_dev;
	hdev->close = vhci_close_dev;
	hdev->flush = vhci_flush;
	hdev->send  = vhci_send_frame;
L
Linus Torvalds 已提交
134

135 136 137 138
	/* bit 6 is for external configuration */
	if (opcode & 0x40)
		set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);

139 140 141 142
	/* bit 7 is for raw device */
	if (opcode & 0x80)
		set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);

143 144 145 146 147 148 149 150 151 152 153
	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;
	}

	bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;

	*skb_put(skb, 1) = 0xff;
154
	*skb_put(skb, 1) = opcode;
155 156 157 158
	put_unaligned_le16(hdev->id, skb_put(skb, 2));
	skb_queue_tail(&data->readq, skb);

	wake_up_interruptible(&data->read_wait);
L
Linus Torvalds 已提交
159 160 161
	return 0;
}

162
static inline ssize_t vhci_get_user(struct vhci_data *data,
A
Al Viro 已提交
163
				    struct iov_iter *from)
L
Linus Torvalds 已提交
164
{
A
Al Viro 已提交
165
	size_t len = iov_iter_count(from);
L
Linus Torvalds 已提交
166
	struct sk_buff *skb;
167
	__u8 pkt_type, opcode;
168
	int ret;
L
Linus Torvalds 已提交
169

170
	if (len < 2 || len > HCI_MAX_FRAME_SIZE)
L
Linus Torvalds 已提交
171 172
		return -EINVAL;

173
	skb = bt_skb_alloc(len, GFP_KERNEL);
174
	if (!skb)
L
Linus Torvalds 已提交
175
		return -ENOMEM;
176

A
Al Viro 已提交
177 178 179
	if (copy_from_iter(skb_put(skb, len), len, from) != len) {
		kfree_skb(skb);
		return -EFAULT;
L
Linus Torvalds 已提交
180 181
	}

182
	pkt_type = *((__u8 *) skb->data);
L
Linus Torvalds 已提交
183 184
	skb_pull(skb, 1);

185 186 187 188 189 190 191 192 193 194 195
	switch (pkt_type) {
	case HCI_EVENT_PKT:
	case HCI_ACLDATA_PKT:
	case HCI_SCODATA_PKT:
		if (!data->hdev) {
			kfree_skb(skb);
			return -ENODEV;
		}

		bt_cb(skb)->pkt_type = pkt_type;

196
		ret = hci_recv_frame(data->hdev, skb);
197 198 199 200 201 202 203
		break;

	case HCI_VENDOR_PKT:
		if (data->hdev) {
			kfree_skb(skb);
			return -EBADFD;
		}
L
Linus Torvalds 已提交
204

205 206
		cancel_delayed_work_sync(&data->open_timeout);

207
		opcode = *((__u8 *) skb->data);
208 209 210 211 212 213 214 215 216
		skb_pull(skb, 1);

		if (skb->len > 0) {
			kfree_skb(skb);
			return -EINVAL;
		}

		kfree_skb(skb);

217
		ret = vhci_create_device(data, opcode);
218 219 220 221 222 223 224
		break;

	default:
		kfree_skb(skb);
		return -EINVAL;
	}

225
	return (ret < 0) ? ret : len;
L
Linus Torvalds 已提交
226 227
}

228
static inline ssize_t vhci_put_user(struct vhci_data *data,
229 230
				    struct sk_buff *skb,
				    char __user *buf, int count)
L
Linus Torvalds 已提交
231 232
{
	char __user *ptr = buf;
233
	int len;
234 235

	len = min_t(unsigned int, skb->len, count);
L
Linus Torvalds 已提交
236 237 238

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

240 241
	if (!data->hdev)
		return len;
L
Linus Torvalds 已提交
242

243
	data->hdev->stat.byte_tx += len;
244

245 246
	switch (bt_cb(skb)->pkt_type) {
	case HCI_COMMAND_PKT:
247
		data->hdev->stat.cmd_tx++;
248 249
		break;
	case HCI_ACLDATA_PKT:
250
		data->hdev->stat.acl_tx++;
251 252
		break;
	case HCI_SCODATA_PKT:
253
		data->hdev->stat.sco_tx++;
254
		break;
255
	}
L
Linus Torvalds 已提交
256

257
	return len;
L
Linus Torvalds 已提交
258 259
}

260
static ssize_t vhci_read(struct file *file,
261
			 char __user *buf, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
262
{
263
	struct vhci_data *data = file->private_data;
L
Linus Torvalds 已提交
264 265 266 267
	struct sk_buff *skb;
	ssize_t ret = 0;

	while (count) {
268
		skb = skb_dequeue(&data->readq);
269 270 271 272 273 274 275
		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 已提交
276 277
		}

278 279 280 281
		if (file->f_flags & O_NONBLOCK) {
			ret = -EAGAIN;
			break;
		}
L
Linus Torvalds 已提交
282

283
		ret = wait_event_interruptible(data->read_wait,
284
					       !skb_queue_empty(&data->readq));
285 286
		if (ret < 0)
			break;
L
Linus Torvalds 已提交
287 288 289 290 291
	}

	return ret;
}

A
Al Viro 已提交
292
static ssize_t vhci_write(struct kiocb *iocb, struct iov_iter *from)
L
Linus Torvalds 已提交
293
{
294
	struct file *file = iocb->ki_filp;
295
	struct vhci_data *data = file->private_data;
L
Linus Torvalds 已提交
296

A
Al Viro 已提交
297
	return vhci_get_user(data, from);
L
Linus Torvalds 已提交
298 299
}

300
static unsigned int vhci_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
301
{
302
	struct vhci_data *data = file->private_data;
L
Linus Torvalds 已提交
303

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

306
	if (!skb_queue_empty(&data->readq))
307 308 309 310 311
		return POLLIN | POLLRDNORM;

	return POLLOUT | POLLWRNORM;
}

312 313 314 315 316 317 318 319
static void vhci_open_timeout(struct work_struct *work)
{
	struct vhci_data *data = container_of(work, struct vhci_data,
					      open_timeout.work);

	vhci_create_device(data, amp ? HCI_AMP : HCI_BREDR);
}

320
static int vhci_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
321
{
322
	struct vhci_data *data;
L
Linus Torvalds 已提交
323

324 325
	data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
	if (!data)
L
Linus Torvalds 已提交
326 327
		return -ENOMEM;

328 329
	skb_queue_head_init(&data->readq);
	init_waitqueue_head(&data->read_wait);
L
Linus Torvalds 已提交
330

331
	INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
L
Linus Torvalds 已提交
332

333
	file->private_data = data;
334
	nonseekable_open(inode, file);
335

336 337
	schedule_delayed_work(&data->open_timeout, msecs_to_jiffies(1000));

338
	return 0;
L
Linus Torvalds 已提交
339 340
}

341
static int vhci_release(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
342
{
343 344
	struct vhci_data *data = file->private_data;
	struct hci_dev *hdev = data->hdev;
L
Linus Torvalds 已提交
345

346 347 348 349 350 351
	cancel_delayed_work_sync(&data->open_timeout);

	if (hdev) {
		hci_unregister_dev(hdev);
		hci_free_dev(hdev);
	}
L
Linus Torvalds 已提交
352 353

	file->private_data = NULL;
354
	kfree(data);
355 356 357 358

	return 0;
}

359
static const struct file_operations vhci_fops = {
360
	.owner		= THIS_MODULE,
361
	.read		= vhci_read,
A
Al Viro 已提交
362
	.write_iter	= vhci_write,
363 364 365
	.poll		= vhci_poll,
	.open		= vhci_open,
	.release	= vhci_release,
366
	.llseek		= no_llseek,
L
Linus Torvalds 已提交
367 368
};

369
static struct miscdevice vhci_miscdev= {
370 371
	.name	= "vhci",
	.fops	= &vhci_fops,
372
	.minor	= VHCI_MINOR,
L
Linus Torvalds 已提交
373 374
};

375
static int __init vhci_init(void)
L
Linus Torvalds 已提交
376
{
377
	BT_INFO("Virtual HCI driver ver %s", VERSION);
L
Linus Torvalds 已提交
378

379
	return misc_register(&vhci_miscdev);
L
Linus Torvalds 已提交
380 381
}

382
static void __exit vhci_exit(void)
L
Linus Torvalds 已提交
383
{
384
	misc_deregister(&vhci_miscdev);
L
Linus Torvalds 已提交
385 386
}

387 388 389
module_init(vhci_init);
module_exit(vhci_exit);

390 391 392
module_param(amp, bool, 0644);
MODULE_PARM_DESC(amp, "Create AMP controller device");

393
MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
394 395 396
MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");
397
MODULE_ALIAS("devname:vhci");
398
MODULE_ALIAS_MISCDEV(VHCI_MINOR);