summaryrefslogtreecommitdiffstats
path: root/drivers/bluetooth/hci_vhci.c
blob: f9b956fb2b8b81ac774cb772b21d06aee58a0a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* 
   BlueZ - Bluetooth protocol stack for Linux
   Copyright (C) 2000-2001 Qualcomm Incorporated

   Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 as
   published by the Free Software Foundation;

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
   SOFTWARE IS DISCLAIMED.
*/

/*
 * Bluetooth HCI virtual device driver.
 *
 * $Id: hci_vhci.c,v 1.3 2002/04/17 17:37:20 maxk Exp $ 
 */
#define VERSION "1.1"

#include <linux/config.h>
#include <linux/module.h>

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/random.h>

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

#include <asm/system.h>
#include <asm/uaccess.h>

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

/* HCI device part */

static int hci_vhci_open(struct hci_dev *hdev)
{
	set_bit(HCI_RUNNING, &hdev->flags);
	return 0;
}

static int hci_vhci_flush(struct hci_dev *hdev)
{
	struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) hdev->driver_data;
	skb_queue_purge(&hci_vhci->readq);
	return 0;
}

static int hci_vhci_close(struct hci_dev *hdev)
{
	if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
		return 0;

	hci_vhci_flush(hdev);
	return 0;
}

static void hci_vhci_destruct(struct hci_dev *hdev)
{
	struct hci_vhci_struct *vhci;

	if (!hdev) return;

	vhci = (struct hci_vhci_struct *) hdev->driver_data;
	kfree(vhci);
}

static int hci_vhci_send_frame(struct sk_buff *skb)
{
	struct hci_dev* hdev = (struct hci_dev *) skb->dev;
	struct hci_vhci_struct *hci_vhci;

	if (!hdev) {
		BT_ERR("Frame for uknown device (hdev=NULL)");
		return -ENODEV;
	}

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

	hci_vhci = (struct hci_vhci_struct *) hdev->driver_data;

	memcpy(skb_push(skb, 1), &skb->pkt_type, 1);
	skb_queue_tail(&hci_vhci->readq, skb);

	if (hci_vhci->flags & VHCI_FASYNC)
		kill_fasync(&hci_vhci->fasync, SIGIO, POLL_IN);
	wake_up_interruptible(&hci_vhci->read_wait);

	return 0;
}

/* Character device part */

/* Poll */
static unsigned int hci_vhci_chr_poll(struct file *file, poll_table * wait)
{  
	struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data;

	poll_wait(file, &hci_vhci->read_wait, wait);
 
	if (!skb_queue_empty(&hci_vhci->readq))
		return POLLIN | POLLRDNORM;

	return POLLOUT | POLLWRNORM;
}

/* Get packet from user space buffer(already verified) */
static inline ssize_t hci_vhci_get_user(struct hci_vhci_struct *hci_vhci, const char __user *buf, size_t count)
{
	struct sk_buff *skb;

	if (count > HCI_MAX_FRAME_SIZE)
		return -EINVAL;

	if (!(skb = bt_skb_alloc(count, GFP_KERNEL)))
		return -ENOMEM;
	
	if (copy_from_user(skb_put(skb, count), buf, count)) {
		kfree_skb(skb);
		return -EFAULT;
	}

	skb->dev = (void *) hci_vhci->hdev;
	skb->pkt_type = *((__u8 *) skb->data);
	skb_pull(skb, 1);

	hci_recv_frame(skb);

	return count;
} 

/* Write */
static ssize_t hci_vhci_chr_write(struct file * file, const char __user * buf, 
			     size_t count, loff_t *pos)
{
	struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data;

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

	return hci_vhci_get_user(hci_vhci, buf, count);
}

/* Put packet to user space buffer(already verified) */
static inline ssize_t hci_vhci_put_user(struct hci_vhci_struct *hci_vhci,
				       struct sk_buff *skb, char __user *buf,
				       int count)
{
	int len = count, total = 0;
	char __user *ptr = buf;

	len = min_t(unsigned int, skb->len, len);
	if (copy_to_user(ptr, skb->data, len))
		return -EFAULT;
	total += len;

	hci_vhci->hdev->stat.byte_tx += len;
	switch (skb->pkt_type) {
		case HCI_COMMAND_PKT:
			hci_vhci->hdev->stat.cmd_tx++;
			break;

		case HCI_ACLDATA_PKT:
			hci_vhci->hdev->stat.acl_tx++;
			break;

		case HCI_SCODATA_PKT:
			hci_vhci->hdev->stat.cmd_tx++;
			break;
	};

	return total;
}

/* Read */
static ssize_t hci_vhci_chr_read(struct file * file, char __user * buf, size_t count, loff_t *pos)
{
	struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data;
	DECLARE_WAITQUEUE(wait, current);
	struct sk_buff *skb;
	ssize_t ret = 0;

	add_wait_queue(&hci_vhci->read_wait, &wait);
	while (count) {
		set_current_state(TASK_INTERRUPTIBLE);

		/* Read frames from device queue */
		if (!(skb = skb_dequeue(&hci_vhci->readq))) {
			if (file->f_flags & O_NONBLOCK) {
				ret = -EAGAIN;
				break;
			}
			if (signal_pending(current)) {
				ret = -ERESTARTSYS;
				break;
			}

			/* Nothing to read, let's sleep */
			schedule();
			continue;
		}

		if (access_ok(VERIFY_WRITE, buf, count))
			ret = hci_vhci_put_user(hci_vhci, skb, buf, count);
		else
			ret = -EFAULT;

		kfree_skb(skb);
		break;
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&hci_vhci->read_wait, &wait);

	return ret;
}

static loff_t hci_vhci_chr_lseek(struct file * file, loff_t offset, int origin)
{
	return -ESPIPE;
}

static int hci_vhci_chr_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	return -EINVAL;
}

static int hci_vhci_chr_fasync(int fd, struct file *file, int on)
{
	struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data;
	int ret;

	if ((ret = fasync_helper(fd, file, on, &hci_vhci->fasync)) < 0)
		return ret; 
 
	if (on)
		hci_vhci->flags |= VHCI_FASYNC;
	else 
		hci_vhci->flags &= ~VHCI_FASYNC;

	return 0;
}

static int hci_vhci_chr_open(struct inode *inode, struct file * file)
{
	struct hci_vhci_struct *hci_vhci = NULL; 
	struct hci_dev *hdev;

	if (!(hci_vhci = kmalloc(sizeof(struct hci_vhci_struct), GFP_KERNEL)))
		return -ENOMEM;

	memset(hci_vhci, 0, sizeof(struct hci_vhci_struct));

	skb_queue_head_init(&hci_vhci->readq);
	init_waitqueue_head(&hci_vhci->read_wait);

	/* Initialize and register HCI device */
	hdev = hci_alloc_dev();
	if (!hdev) {
		kfree(hci_vhci);
		return -ENOMEM;
	}

	hci_vhci->hdev = hdev;

	hdev->type = HCI_VHCI;
	hdev->driver_data = hci_vhci;

	hdev->open  = hci_vhci_open;
	hdev->close = hci_vhci_close;
	hdev->flush = hci_vhci_flush;
	hdev->send  = hci_vhci_send_frame;
	hdev->destruct = hci_vhci_destruct;

	hdev->owner = THIS_MODULE;
	
	if (hci_register_dev(hdev) < 0) {
		kfree(hci_vhci);
		hci_free_dev(hdev);
		return -EBUSY;
	}

	file->private_data = hci_vhci;
	return nonseekable_open(inode, file);   
}

static int hci_vhci_chr_close(struct inode *inode, struct file *file)
{
	struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data;
	struct hci_dev *hdev = hci_vhci->hdev;

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

	hci_free_dev(hdev);

	file->private_data = NULL;
	return 0;
}

static struct file_operations hci_vhci_fops = {
	.owner	= THIS_MODULE,	
	.llseek	= hci_vhci_chr_lseek,
	.read	= hci_vhci_chr_read,
	.write	= hci_vhci_chr_write,
	.poll	= hci_vhci_chr_poll,
	.ioctl	= hci_vhci_chr_ioctl,
	.open	= hci_vhci_chr_open,
	.release	= hci_vhci_chr_close,
	.fasync	= hci_vhci_chr_fasync		
};

static struct miscdevice hci_vhci_miscdev=
{
        VHCI_MINOR,
        "hci_vhci",
        &hci_vhci_fops
};

static int __init hci_vhci_init(void)
{
	BT_INFO("VHCI driver ver %s", VERSION);

	if (misc_register(&hci_vhci_miscdev)) {
		BT_ERR("Can't register misc device %d\n", VHCI_MINOR);
		return -EIO;
	}

	return 0;
}

static void hci_vhci_cleanup(void)
{
	misc_deregister(&hci_vhci_miscdev);
}

module_init(hci_vhci_init);
module_exit(hci_vhci_cleanup);

MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
MODULE_DESCRIPTION("Bluetooth VHCI driver ver " VERSION);
MODULE_LICENSE("GPL");