hci_vhci.c 7.6 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>
A
Arnd Bergmann 已提交
31
#include <linux/smp_lock.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 44 45 46 47 48 49 50
#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 已提交
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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 已提交
68 69 70 71 72 73
{
	set_bit(HCI_RUNNING, &hdev->flags);

	return 0;
}

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

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

81
	skb_queue_purge(&data->readq);
82

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

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

90
	skb_queue_purge(&data->readq);
L
Linus Torvalds 已提交
91

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

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

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

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

108
	data = hdev->driver_data;
L
Linus Torvalds 已提交
109

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

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

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

	return 0;
}

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

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

	if (count > HCI_MAX_FRAME_SIZE)
		return -EINVAL;

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

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

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

	hci_recv_frame(skb);

	return count;
}

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

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

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

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

165
	data->hdev->stat.byte_tx += len;
166

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

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

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

	return total;
}

184 185
static ssize_t vhci_read(struct file *file,
				char __user *buf, size_t count, loff_t *pos)
L
Linus Torvalds 已提交
186 187
{
	DECLARE_WAITQUEUE(wait, current);
188
	struct vhci_data *data = file->private_data;
L
Linus Torvalds 已提交
189 190 191
	struct sk_buff *skb;
	ssize_t ret = 0;

192
	add_wait_queue(&data->read_wait, &wait);
L
Linus Torvalds 已提交
193 194 195
	while (count) {
		set_current_state(TASK_INTERRUPTIBLE);

196
		skb = skb_dequeue(&data->readq);
197
		if (!skb) {
L
Linus Torvalds 已提交
198 199 200 201
			if (file->f_flags & O_NONBLOCK) {
				ret = -EAGAIN;
				break;
			}
202

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

			schedule();
			continue;
		}

		if (access_ok(VERIFY_WRITE, buf, count))
213
			ret = vhci_put_user(data, skb, buf, count);
L
Linus Torvalds 已提交
214 215 216 217 218 219 220
		else
			ret = -EFAULT;

		kfree_skb(skb);
		break;
	}
	set_current_state(TASK_RUNNING);
221
	remove_wait_queue(&data->read_wait, &wait);
L
Linus Torvalds 已提交
222 223 224 225

	return ret;
}

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

231 232 233
	if (!access_ok(VERIFY_READ, buf, count))
		return -EFAULT;

234
	return vhci_get_user(data, buf, count);
L
Linus Torvalds 已提交
235 236
}

237
static unsigned int vhci_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
238
{
239
	struct vhci_data *data = file->private_data;
L
Linus Torvalds 已提交
240

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

243
	if (!skb_queue_empty(&data->readq))
244 245 246 247 248 249 250 251 252
		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 已提交
253 254
}

255
static int vhci_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
256
{
257
	struct vhci_data *data;
L
Linus Torvalds 已提交
258 259
	struct hci_dev *hdev;

260 261
	data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
	if (!data)
L
Linus Torvalds 已提交
262 263
		return -ENOMEM;

264 265
	skb_queue_head_init(&data->readq);
	init_waitqueue_head(&data->read_wait);
L
Linus Torvalds 已提交
266

A
Arnd Bergmann 已提交
267
	lock_kernel();
L
Linus Torvalds 已提交
268 269
	hdev = hci_alloc_dev();
	if (!hdev) {
270
		kfree(data);
A
Arnd Bergmann 已提交
271
		unlock_kernel();
L
Linus Torvalds 已提交
272 273 274
		return -ENOMEM;
	}

275
	data->hdev = hdev;
L
Linus Torvalds 已提交
276

277
	hdev->type = HCI_VIRTUAL;
278
	hdev->driver_data = data;
L
Linus Torvalds 已提交
279

280 281 282 283 284
	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 已提交
285 286

	hdev->owner = THIS_MODULE;
287

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

296
	file->private_data = data;
A
Arnd Bergmann 已提交
297
	unlock_kernel();
298 299

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

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

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

	hci_free_dev(hdev);

	file->private_data = NULL;
314 315 316 317 318 319

	return 0;
}

static int vhci_fasync(int fd, struct file *file, int on)
{
320
	struct vhci_data *data = file->private_data;
321
	int err = 0;
322

323
	lock_kernel();
324
	err = fasync_helper(fd, file, on, &data->fasync);
325
	if (err < 0)
326
		goto out;
327 328

	if (on)
329
		data->flags |= VHCI_FASYNC;
330
	else
331
		data->flags &= ~VHCI_FASYNC;
332

333 334 335
out:
	unlock_kernel();
	return err;
L
Linus Torvalds 已提交
336 337
}

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

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

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

358 359 360 361
	vhci_miscdev.minor = minor;

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

	return 0;
}

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

374 375 376 377 378
module_init(vhci_init);
module_exit(vhci_exit);

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

380
MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
381 382 383
MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");