Blame view

drivers/bluetooth/hci_vhci.c 6.23 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
   *
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
3
4
   *  Bluetooth virtual HCI driver
   *
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
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>
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
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
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
  #include <linux/kernel.h>
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
28
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  #include <linux/slab.h>
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
30
31
32
  #include <linux/types.h>
  #include <linux/errno.h>
  #include <linux/sched.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
  #include <linux/poll.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
  
  #include <linux/skbuff.h>
  #include <linux/miscdevice.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
  #include <net/bluetooth/bluetooth.h>
  #include <net/bluetooth/hci_core.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39

ac28494c5   Marcel Holtmann   Bluetooth: Use on...
40
  #define VERSION "1.3"
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
41

4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
42
43
44
45
46
47
48
  struct vhci_data {
  	struct hci_dev *hdev;
  
  	unsigned long flags;
  
  	wait_queue_head_t read_wait;
  	struct sk_buff_head readq;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
49
  };
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
50
  static int vhci_open_dev(struct hci_dev *hdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
  {
  	set_bit(HCI_RUNNING, &hdev->flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
  	return 0;
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
56
  static int vhci_close_dev(struct hci_dev *hdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
  {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
58
  	struct vhci_data *data = hdev->driver_data;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
59

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
  	if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  		return 0;
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
62
  	skb_queue_purge(&data->readq);
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
63

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
  	return 0;
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
66
  static int vhci_flush(struct hci_dev *hdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
68
  	struct vhci_data *data = hdev->driver_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69

9c724357f   Marcel Holtmann   [Bluetooth] Code ...
70
  	skb_queue_purge(&data->readq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71

4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
72
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
74
  static int vhci_send_frame(struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
  {
  	struct hci_dev* hdev = (struct hci_dev *) skb->dev;
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
77
  	struct vhci_data *data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
  
  	if (!hdev) {
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
80
  		BT_ERR("Frame for unknown HCI device (hdev=NULL)");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
85
  		return -ENODEV;
  	}
  
  	if (!test_bit(HCI_RUNNING, &hdev->flags))
  		return -EBUSY;
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
86
  	data = hdev->driver_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87

0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
88
  	memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
89
  	skb_queue_tail(&data->readq, skb);
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
90

9c724357f   Marcel Holtmann   [Bluetooth] Code ...
91
  	wake_up_interruptible(&data->read_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
  
  	return 0;
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
95
96
97
  static void vhci_destruct(struct hci_dev *hdev)
  {
  	kfree(hdev->driver_data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
  }
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
99
  static inline ssize_t vhci_get_user(struct vhci_data *data,
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
100
  					const char __user *buf, size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
105
  {
  	struct sk_buff *skb;
  
  	if (count > HCI_MAX_FRAME_SIZE)
  		return -EINVAL;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
106
107
  	skb = bt_skb_alloc(count, GFP_KERNEL);
  	if (!skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  		return -ENOMEM;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
109

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
113
  	if (copy_from_user(skb_put(skb, count), buf, count)) {
  		kfree_skb(skb);
  		return -EFAULT;
  	}
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
114
  	skb->dev = (void *) data->hdev;
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
115
  	bt_cb(skb)->pkt_type = *((__u8 *) skb->data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
118
119
120
  	skb_pull(skb, 1);
  
  	hci_recv_frame(skb);
  
  	return count;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
  }
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
122
  static inline ssize_t vhci_put_user(struct vhci_data *data,
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
123
  			struct sk_buff *skb, char __user *buf, int count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
  	char __user *ptr = buf;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
126
127
128
  	int len, total = 0;
  
  	len = min_t(unsigned int, skb->len, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
  	if (copy_to_user(ptr, skb->data, len))
  		return -EFAULT;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
132

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
  	total += len;
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
134
  	data->hdev->stat.byte_tx += len;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
135

0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
136
137
  	switch (bt_cb(skb)->pkt_type) {
  	case HCI_COMMAND_PKT:
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
138
  		data->hdev->stat.cmd_tx++;
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
139
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140

0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
141
  	case HCI_ACLDATA_PKT:
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
142
  		data->hdev->stat.acl_tx++;
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
143
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144

0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
145
  	case HCI_SCODATA_PKT:
4f7ac1814   Gustavo F. Padovan   Bluetooth: Fix wr...
146
  		data->hdev->stat.sco_tx++;
0d48d9394   Marcel Holtmann   [Bluetooth]: Move...
147
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
  	};
  
  	return total;
  }
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
152
153
  static ssize_t vhci_read(struct file *file,
  				char __user *buf, size_t count, loff_t *pos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
  {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
155
  	struct vhci_data *data = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
  	struct sk_buff *skb;
  	ssize_t ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
  	while (count) {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
159
  		skb = skb_dequeue(&data->readq);
4db7589f3   Marcel Holtmann   Bluetooth: Use wa...
160
161
162
163
164
165
166
  		if (skb) {
  			ret = vhci_put_user(data, skb, buf, count);
  			if (ret < 0)
  				skb_queue_head(&data->readq, skb);
  			else
  				kfree_skb(skb);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
167
  		}
4db7589f3   Marcel Holtmann   Bluetooth: Use wa...
168
169
170
171
  		if (file->f_flags & O_NONBLOCK) {
  			ret = -EAGAIN;
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172

4db7589f3   Marcel Holtmann   Bluetooth: Use wa...
173
174
175
176
  		ret = wait_event_interruptible(data->read_wait,
  					!skb_queue_empty(&data->readq));
  		if (ret < 0)
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
179
180
  
  	return ret;
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
181
182
  static ssize_t vhci_write(struct file *file,
  			const char __user *buf, size_t count, loff_t *pos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
  {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
184
  	struct vhci_data *data = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185

9c724357f   Marcel Holtmann   [Bluetooth] Code ...
186
  	return vhci_get_user(data, buf, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
188
  static unsigned int vhci_poll(struct file *file, poll_table *wait)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
  {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
190
  	struct vhci_data *data = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191

9c724357f   Marcel Holtmann   [Bluetooth] Code ...
192
  	poll_wait(file, &data->read_wait, wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193

9c724357f   Marcel Holtmann   [Bluetooth] Code ...
194
  	if (!skb_queue_empty(&data->readq))
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
195
196
197
198
  		return POLLIN | POLLRDNORM;
  
  	return POLLOUT | POLLWRNORM;
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
199
  static int vhci_open(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
  {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
201
  	struct vhci_data *data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
  	struct hci_dev *hdev;
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
203
204
  	data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
  	if (!data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  		return -ENOMEM;
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
206
207
  	skb_queue_head_init(&data->readq);
  	init_waitqueue_head(&data->read_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
210
  	hdev = hci_alloc_dev();
  	if (!hdev) {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
211
  		kfree(data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
213
  		return -ENOMEM;
  	}
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
214
  	data->hdev = hdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215

c13854cef   Marcel Holtmann   Bluetooth: Conver...
216
  	hdev->bus = HCI_VIRTUAL;
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
217
  	hdev->driver_data = data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218

4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
219
220
221
222
223
  	hdev->open     = vhci_open_dev;
  	hdev->close    = vhci_close_dev;
  	hdev->flush    = vhci_flush;
  	hdev->send     = vhci_send_frame;
  	hdev->destruct = vhci_destruct;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
225
  
  	hdev->owner = THIS_MODULE;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
226

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  	if (hci_register_dev(hdev) < 0) {
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
228
  		BT_ERR("Can't register HCI device");
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
229
  		kfree(data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
232
  		hci_free_dev(hdev);
  		return -EBUSY;
  	}
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
233
  	file->private_data = data;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
234
235
  
  	return nonseekable_open(inode, file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
237
  static int vhci_release(struct inode *inode, struct file *file)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
  {
9c724357f   Marcel Holtmann   [Bluetooth] Code ...
239
240
  	struct vhci_data *data = file->private_data;
  	struct hci_dev *hdev = data->hdev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
244
245
246
247
248
  
  	if (hci_unregister_dev(hdev) < 0) {
  		BT_ERR("Can't unregister HCI device %s", hdev->name);
  	}
  
  	hci_free_dev(hdev);
  
  	file->private_data = NULL;
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
249
250
251
  
  	return 0;
  }
2b8693c06   Arjan van de Ven   [PATCH] mark stru...
252
  static const struct file_operations vhci_fops = {
fed4c2508   Marcel Holtmann   Bluetooth: Fix mi...
253
  	.owner		= THIS_MODULE,
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
254
255
256
  	.read		= vhci_read,
  	.write		= vhci_write,
  	.poll		= vhci_poll,
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
257
258
  	.open		= vhci_open,
  	.release	= vhci_release,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
  };
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
260
  static struct miscdevice vhci_miscdev= {
ac28494c5   Marcel Holtmann   Bluetooth: Use on...
261
262
263
  	.name	= "vhci",
  	.fops	= &vhci_fops,
  	.minor	= MISC_DYNAMIC_MINOR,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  };
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
265
  static int __init vhci_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
  {
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
267
  	BT_INFO("Virtual HCI driver ver %s", VERSION);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268

329ab1b3e   Marcel Holtmann   Bluetooth: Remove...
269
  	return misc_register(&vhci_miscdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
271
  static void __exit vhci_exit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
  {
329ab1b3e   Marcel Holtmann   Bluetooth: Remove...
273
  	misc_deregister(&vhci_miscdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
  }
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
275
276
  module_init(vhci_init);
  module_exit(vhci_exit);
63fbd24e5   Marcel Holtmann   [Bluetooth] Conso...
277
  MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4aa769b99   Marcel Holtmann   [Bluetooth]: Upda...
278
279
280
  MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
  MODULE_VERSION(VERSION);
  MODULE_LICENSE("GPL");