hci_vhci.c 7.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *  Bluetooth virtual HCI driver
 *
 *  Copyright (C) 2000-2001 Qualcomm Incorporated
 *  Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
 *  Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
 *
 *
 *  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 43 44 45 46 47 48 49
#ifndef CONFIG_BT_HCIVHCI_DEBUG
#undef  BT_DBG
#define BT_DBG(D...)
#endif

#define VERSION "1.2"

static int minor = MISC_DYNAMIC_MINOR;
L
Linus Torvalds 已提交
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
struct vhci_data {
	struct hci_dev *hdev;

	unsigned long flags;

	wait_queue_head_t read_wait;
	struct sk_buff_head readq;

	struct fasync_struct *fasync;
};

#define VHCI_FASYNC	0x0010

static struct miscdevice vhci_miscdev;

static int vhci_open_dev(struct hci_dev *hdev)
L
Linus Torvalds 已提交
67 68 69 70 71 72
{
	set_bit(HCI_RUNNING, &hdev->flags);

	return 0;
}

73
static int vhci_close_dev(struct hci_dev *hdev)
L
Linus Torvalds 已提交
74
{
75 76
	struct vhci_data *vhci = hdev->driver_data;

L
Linus Torvalds 已提交
77 78 79
	if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
		return 0;

80 81
	skb_queue_purge(&vhci->readq);

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

85
static int vhci_flush(struct hci_dev *hdev)
L
Linus Torvalds 已提交
86
{
87
	struct vhci_data *vhci = hdev->driver_data;
L
Linus Torvalds 已提交
88

89
	skb_queue_purge(&vhci->readq);
L
Linus Torvalds 已提交
90

91
	return 0;
L
Linus Torvalds 已提交
92 93
}

94
static int vhci_send_frame(struct sk_buff *skb)
L
Linus Torvalds 已提交
95 96
{
	struct hci_dev* hdev = (struct hci_dev *) skb->dev;
97
	struct vhci_data *vhci;
L
Linus Torvalds 已提交
98 99

	if (!hdev) {
100
		BT_ERR("Frame for unknown HCI device (hdev=NULL)");
L
Linus Torvalds 已提交
101 102 103 104 105 106
		return -ENODEV;
	}

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

107
	vhci = hdev->driver_data;
L
Linus Torvalds 已提交
108

109
	memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
110 111 112 113
	skb_queue_tail(&vhci->readq, skb);

	if (vhci->flags & VHCI_FASYNC)
		kill_fasync(&vhci->fasync, SIGIO, POLL_IN);
L
Linus Torvalds 已提交
114

115
	wake_up_interruptible(&vhci->read_wait);
L
Linus Torvalds 已提交
116 117 118 119

	return 0;
}

120 121 122
static void vhci_destruct(struct hci_dev *hdev)
{
	kfree(hdev->driver_data);
L
Linus Torvalds 已提交
123 124
}

125 126
static inline ssize_t vhci_get_user(struct vhci_data *vhci,
					const char __user *buf, size_t count)
L
Linus Torvalds 已提交
127 128 129 130 131 132
{
	struct sk_buff *skb;

	if (count > HCI_MAX_FRAME_SIZE)
		return -EINVAL;

133 134
	skb = bt_skb_alloc(count, GFP_KERNEL);
	if (!skb)
L
Linus Torvalds 已提交
135
		return -ENOMEM;
136

L
Linus Torvalds 已提交
137 138 139 140 141
	if (copy_from_user(skb_put(skb, count), buf, count)) {
		kfree_skb(skb);
		return -EFAULT;
	}

142
	skb->dev = (void *) vhci->hdev;
143
	bt_cb(skb)->pkt_type = *((__u8 *) skb->data);
L
Linus Torvalds 已提交
144 145 146 147 148 149 150
	skb_pull(skb, 1);

	hci_recv_frame(skb);

	return count;
}

151 152
static inline ssize_t vhci_put_user(struct vhci_data *vhci,
			struct sk_buff *skb, char __user *buf, int count)
L
Linus Torvalds 已提交
153 154
{
	char __user *ptr = buf;
155 156 157
	int len, total = 0;

	len = min_t(unsigned int, skb->len, count);
L
Linus Torvalds 已提交
158 159 160

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

L
Linus Torvalds 已提交
162 163
	total += len;

164 165
	vhci->hdev->stat.byte_tx += len;

166 167 168 169
	switch (bt_cb(skb)->pkt_type) {
	case HCI_COMMAND_PKT:
		vhci->hdev->stat.cmd_tx++;
		break;
L
Linus Torvalds 已提交
170

171 172 173
	case HCI_ACLDATA_PKT:
		vhci->hdev->stat.acl_tx++;
		break;
L
Linus Torvalds 已提交
174

175 176 177
	case HCI_SCODATA_PKT:
		vhci->hdev->stat.cmd_tx++;
		break;
L
Linus Torvalds 已提交
178 179 180 181 182
	};

	return total;
}

183 184 185 186 187 188
static loff_t vhci_llseek(struct file * file, loff_t offset, int origin)
{
	return -ESPIPE;
}

static ssize_t vhci_read(struct file * file, char __user * buf, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
189 190
{
	DECLARE_WAITQUEUE(wait, current);
191
	struct vhci_data *vhci = file->private_data;
L
Linus Torvalds 已提交
192 193 194
	struct sk_buff *skb;
	ssize_t ret = 0;

195
	add_wait_queue(&vhci->read_wait, &wait);
L
Linus Torvalds 已提交
196 197 198
	while (count) {
		set_current_state(TASK_INTERRUPTIBLE);

199 200
		skb = skb_dequeue(&vhci->readq);
		if (!skb) {
L
Linus Torvalds 已提交
201 202 203 204
			if (file->f_flags & O_NONBLOCK) {
				ret = -EAGAIN;
				break;
			}
205

L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215
			if (signal_pending(current)) {
				ret = -ERESTARTSYS;
				break;
			}

			schedule();
			continue;
		}

		if (access_ok(VERIFY_WRITE, buf, count))
216
			ret = vhci_put_user(vhci, skb, buf, count);
L
Linus Torvalds 已提交
217 218 219 220 221 222 223
		else
			ret = -EFAULT;

		kfree_skb(skb);
		break;
	}
	set_current_state(TASK_RUNNING);
224
	remove_wait_queue(&vhci->read_wait, &wait);
L
Linus Torvalds 已提交
225 226 227 228

	return ret;
}

229 230
static ssize_t vhci_write(struct file *file,
			const char __user *buf, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
231
{
232
	struct vhci_data *vhci = file->private_data;
L
Linus Torvalds 已提交
233

234 235 236 237
	if (!access_ok(VERIFY_READ, buf, count))
		return -EFAULT;

	return vhci_get_user(vhci, buf, count);
L
Linus Torvalds 已提交
238 239
}

240
static unsigned int vhci_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
241
{
242
	struct vhci_data *vhci = file->private_data;
L
Linus Torvalds 已提交
243

244
	poll_wait(file, &vhci->read_wait, wait);
L
Linus Torvalds 已提交
245

246 247 248 249 250 251 252 253 254 255
	if (!skb_queue_empty(&vhci->readq))
		return POLLIN | POLLRDNORM;

	return POLLOUT | POLLWRNORM;
}

static int vhci_ioctl(struct inode *inode, struct file *file,
					unsigned int cmd, unsigned long arg)
{
	return -EINVAL;
L
Linus Torvalds 已提交
256 257
}

258
static int vhci_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
259
{
260
	struct vhci_data *vhci;
L
Linus Torvalds 已提交
261 262
	struct hci_dev *hdev;

263
	vhci = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
264
	if (!vhci)
L
Linus Torvalds 已提交
265 266
		return -ENOMEM;

267 268
	skb_queue_head_init(&vhci->readq);
	init_waitqueue_head(&vhci->read_wait);
L
Linus Torvalds 已提交
269 270 271

	hdev = hci_alloc_dev();
	if (!hdev) {
272
		kfree(vhci);
L
Linus Torvalds 已提交
273 274 275
		return -ENOMEM;
	}

276
	vhci->hdev = hdev;
L
Linus Torvalds 已提交
277 278

	hdev->type = HCI_VHCI;
279
	hdev->driver_data = vhci;
L
Linus Torvalds 已提交
280

281 282 283 284 285
	hdev->open     = vhci_open_dev;
	hdev->close    = vhci_close_dev;
	hdev->flush    = vhci_flush;
	hdev->send     = vhci_send_frame;
	hdev->destruct = vhci_destruct;
L
Linus Torvalds 已提交
286 287

	hdev->owner = THIS_MODULE;
288

L
Linus Torvalds 已提交
289
	if (hci_register_dev(hdev) < 0) {
290 291
		BT_ERR("Can't register HCI device");
		kfree(vhci);
L
Linus Torvalds 已提交
292 293 294 295
		hci_free_dev(hdev);
		return -EBUSY;
	}

296 297 298
	file->private_data = vhci;

	return nonseekable_open(inode, file);
L
Linus Torvalds 已提交
299 300
}

301
static int vhci_release(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
302
{
303 304
	struct vhci_data *vhci = file->private_data;
	struct hci_dev *hdev = vhci->hdev;
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312

	if (hci_unregister_dev(hdev) < 0) {
		BT_ERR("Can't unregister HCI device %s", hdev->name);
	}

	hci_free_dev(hdev);

	file->private_data = NULL;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	return 0;
}

static int vhci_fasync(int fd, struct file *file, int on)
{
	struct vhci_data *vhci = file->private_data;
	int err;

	err = fasync_helper(fd, file, on, &vhci->fasync);
	if (err < 0)
		return err;

	if (on)
		vhci->flags |= VHCI_FASYNC;
	else
		vhci->flags &= ~VHCI_FASYNC;

L
Linus Torvalds 已提交
331 332 333
	return 0;
}

334 335 336 337 338 339 340 341 342 343
static struct file_operations vhci_fops = {
	.owner		= THIS_MODULE,
	.llseek		= vhci_llseek,
	.read		= vhci_read,
	.write		= vhci_write,
	.poll		= vhci_poll,
	.ioctl		= vhci_ioctl,
	.open		= vhci_open,
	.release	= vhci_release,
	.fasync		= vhci_fasync,
L
Linus Torvalds 已提交
344 345
};

346 347 348
static struct miscdevice vhci_miscdev= {
	.name		= "vhci",
	.fops		= &vhci_fops,
L
Linus Torvalds 已提交
349 350
};

351
static int __init vhci_init(void)
L
Linus Torvalds 已提交
352
{
353
	BT_INFO("Virtual HCI driver ver %s", VERSION);
L
Linus Torvalds 已提交
354

355 356 357 358
	vhci_miscdev.minor = minor;

	if (misc_register(&vhci_miscdev) < 0) {
		BT_ERR("Can't register misc device with minor %d", minor);
L
Linus Torvalds 已提交
359 360 361 362 363 364
		return -EIO;
	}

	return 0;
}

365
static void __exit vhci_exit(void)
L
Linus Torvalds 已提交
366
{
367 368
	if (misc_deregister(&vhci_miscdev) < 0)
		BT_ERR("Can't unregister misc device with minor %d", minor);
L
Linus Torvalds 已提交
369 370
}

371 372 373 374 375
module_init(vhci_init);
module_exit(vhci_exit);

module_param(minor, int, 0444);
MODULE_PARM_DESC(minor, "Miscellaneous minor device number");
L
Linus Torvalds 已提交
376

377 378 379 380
MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");