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 46 47 48 49 50 51 52 53

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 已提交
54 55 56 57 58 59
{
	set_bit(HCI_RUNNING, &hdev->flags);

	return 0;
}

60
static int vhci_close_dev(struct hci_dev *hdev)
L
Linus Torvalds 已提交
61
{
62
	struct vhci_data *data = hdev->driver_data;
63

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

67
	skb_queue_purge(&data->readq);
68

L
Linus Torvalds 已提交
69 70 71
	return 0;
}

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

76
	skb_queue_purge(&data->readq);
L
Linus Torvalds 已提交
77

78
	return 0;
L
Linus Torvalds 已提交
79 80
}

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

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

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

94
	data = hdev->driver_data;
L
Linus Torvalds 已提交
95

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

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

	return 0;
}

104 105 106
static void vhci_destruct(struct hci_dev *hdev)
{
	kfree(hdev->driver_data);
L
Linus Torvalds 已提交
107 108
}

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

	if (count > HCI_MAX_FRAME_SIZE)
		return -EINVAL;

117 118
	skb = bt_skb_alloc(count, GFP_KERNEL);
	if (!skb)
L
Linus Torvalds 已提交
119
		return -ENOMEM;
120

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

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

	hci_recv_frame(skb);

	return count;
}

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

	len = min_t(unsigned int, skb->len, count);
L
Linus Torvalds 已提交
142 143 144

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

L
Linus Torvalds 已提交
146 147
	total += len;

148
	data->hdev->stat.byte_tx += len;
149

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

155
	case HCI_ACLDATA_PKT:
156
		data->hdev->stat.acl_tx++;
157
		break;
L
Linus Torvalds 已提交
158

159
	case HCI_SCODATA_PKT:
160
		data->hdev->stat.cmd_tx++;
161
		break;
L
Linus Torvalds 已提交
162 163 164 165 166
	};

	return total;
}

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

	while (count) {
175
		skb = skb_dequeue(&data->readq);
176 177 178 179 180 181 182
		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 已提交
183 184
		}

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

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

	return ret;
}

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

204
	return vhci_get_user(data, buf, count);
L
Linus Torvalds 已提交
205 206
}

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

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

213
	if (!skb_queue_empty(&data->readq))
214 215 216 217 218 219
		return POLLIN | POLLRDNORM;

	return POLLOUT | POLLWRNORM;
}

static int vhci_open(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
220
{
221
	struct vhci_data *data;
L
Linus Torvalds 已提交
222 223
	struct hci_dev *hdev;

224 225
	data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
	if (!data)
L
Linus Torvalds 已提交
226 227
		return -ENOMEM;

228 229
	skb_queue_head_init(&data->readq);
	init_waitqueue_head(&data->read_wait);
L
Linus Torvalds 已提交
230 231 232

	hdev = hci_alloc_dev();
	if (!hdev) {
233
		kfree(data);
L
Linus Torvalds 已提交
234 235 236
		return -ENOMEM;
	}

237
	data->hdev = hdev;
L
Linus Torvalds 已提交
238

239
	hdev->type = HCI_VIRTUAL;
240
	hdev->driver_data = data;
L
Linus Torvalds 已提交
241

242 243 244 245 246
	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 已提交
247 248

	hdev->owner = THIS_MODULE;
249

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

257
	file->private_data = data;
258 259

	return nonseekable_open(inode, file);
L
Linus Torvalds 已提交
260 261
}

262
static int vhci_release(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
263
{
264 265
	struct vhci_data *data = file->private_data;
	struct hci_dev *hdev = data->hdev;
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273

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

	hci_free_dev(hdev);

	file->private_data = NULL;
274 275 276 277

	return 0;
}

278
static const struct file_operations vhci_fops = {
279
	.owner		= THIS_MODULE,
280 281 282 283 284
	.read		= vhci_read,
	.write		= vhci_write,
	.poll		= vhci_poll,
	.open		= vhci_open,
	.release	= vhci_release,
L
Linus Torvalds 已提交
285 286
};

287
static struct miscdevice vhci_miscdev= {
288 289 290
	.name	= "vhci",
	.fops	= &vhci_fops,
	.minor	= MISC_DYNAMIC_MINOR,
L
Linus Torvalds 已提交
291 292
};

293
static int __init vhci_init(void)
L
Linus Torvalds 已提交
294
{
295
	BT_INFO("Virtual HCI driver ver %s", VERSION);
L
Linus Torvalds 已提交
296

297
	return misc_register(&vhci_miscdev);
L
Linus Torvalds 已提交
298 299
}

300
static void __exit vhci_exit(void)
L
Linus Torvalds 已提交
301
{
302
	misc_deregister(&vhci_miscdev);
L
Linus Torvalds 已提交
303 304
}

305 306 307
module_init(vhci_init);
module_exit(vhci_exit);

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