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#include <linux/module.h>
27#include <asm/unaligned.h>
28
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/types.h>
33#include <linux/errno.h>
34#include <linux/sched.h>
35#include <linux/poll.h>
36
37#include <linux/skbuff.h>
38#include <linux/miscdevice.h>
39
40#include <net/bluetooth/bluetooth.h>
41#include <net/bluetooth/hci_core.h>
42
43#define VERSION "1.5"
44
45static bool amp;
46
47struct vhci_data {
48 struct hci_dev *hdev;
49
50 wait_queue_head_t read_wait;
51 struct sk_buff_head readq;
52
53 struct mutex open_mutex;
54 struct delayed_work open_timeout;
55};
56
57static int vhci_open_dev(struct hci_dev *hdev)
58{
59 return 0;
60}
61
62static int vhci_close_dev(struct hci_dev *hdev)
63{
64 struct vhci_data *data = hci_get_drvdata(hdev);
65
66 skb_queue_purge(&data->readq);
67
68 return 0;
69}
70
71static int vhci_flush(struct hci_dev *hdev)
72{
73 struct vhci_data *data = hci_get_drvdata(hdev);
74
75 skb_queue_purge(&data->readq);
76
77 return 0;
78}
79
80static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
81{
82 struct vhci_data *data = hci_get_drvdata(hdev);
83
84 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
85 skb_queue_tail(&data->readq, skb);
86
87 wake_up_interruptible(&data->read_wait);
88 return 0;
89}
90
91static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
92{
93 struct hci_dev *hdev;
94 struct sk_buff *skb;
95 __u8 dev_type;
96
97 if (data->hdev)
98 return -EBADFD;
99
100
101 dev_type = opcode & 0x03;
102
103 if (dev_type != HCI_PRIMARY && dev_type != HCI_AMP)
104 return -EINVAL;
105
106
107 if (opcode & 0x3c)
108 return -EINVAL;
109
110 skb = bt_skb_alloc(4, GFP_KERNEL);
111 if (!skb)
112 return -ENOMEM;
113
114 hdev = hci_alloc_dev();
115 if (!hdev) {
116 kfree_skb(skb);
117 return -ENOMEM;
118 }
119
120 data->hdev = hdev;
121
122 hdev->bus = HCI_VIRTUAL;
123 hdev->dev_type = dev_type;
124 hci_set_drvdata(hdev, data);
125
126 hdev->open = vhci_open_dev;
127 hdev->close = vhci_close_dev;
128 hdev->flush = vhci_flush;
129 hdev->send = vhci_send_frame;
130
131
132 if (opcode & 0x40)
133 set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
134
135
136 if (opcode & 0x80)
137 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
138
139 if (hci_register_dev(hdev) < 0) {
140 BT_ERR("Can't register HCI device");
141 hci_free_dev(hdev);
142 data->hdev = NULL;
143 kfree_skb(skb);
144 return -EBUSY;
145 }
146
147 hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
148
149 skb_put_u8(skb, 0xff);
150 skb_put_u8(skb, opcode);
151 put_unaligned_le16(hdev->id, skb_put(skb, 2));
152 skb_queue_tail(&data->readq, skb);
153
154 wake_up_interruptible(&data->read_wait);
155 return 0;
156}
157
158static int vhci_create_device(struct vhci_data *data, __u8 opcode)
159{
160 int err;
161
162 mutex_lock(&data->open_mutex);
163 err = __vhci_create_device(data, opcode);
164 mutex_unlock(&data->open_mutex);
165
166 return err;
167}
168
169static inline ssize_t vhci_get_user(struct vhci_data *data,
170 struct iov_iter *from)
171{
172 size_t len = iov_iter_count(from);
173 struct sk_buff *skb;
174 __u8 pkt_type, opcode;
175 int ret;
176
177 if (len < 2 || len > HCI_MAX_FRAME_SIZE)
178 return -EINVAL;
179
180 skb = bt_skb_alloc(len, GFP_KERNEL);
181 if (!skb)
182 return -ENOMEM;
183
184 if (!copy_from_iter_full(skb_put(skb, len), len, from)) {
185 kfree_skb(skb);
186 return -EFAULT;
187 }
188
189 pkt_type = *((__u8 *) skb->data);
190 skb_pull(skb, 1);
191
192 switch (pkt_type) {
193 case HCI_EVENT_PKT:
194 case HCI_ACLDATA_PKT:
195 case HCI_SCODATA_PKT:
196 case HCI_ISODATA_PKT:
197 if (!data->hdev) {
198 kfree_skb(skb);
199 return -ENODEV;
200 }
201
202 hci_skb_pkt_type(skb) = pkt_type;
203
204 ret = hci_recv_frame(data->hdev, skb);
205 break;
206
207 case HCI_VENDOR_PKT:
208 cancel_delayed_work_sync(&data->open_timeout);
209
210 opcode = *((__u8 *) skb->data);
211 skb_pull(skb, 1);
212
213 if (skb->len > 0) {
214 kfree_skb(skb);
215 return -EINVAL;
216 }
217
218 kfree_skb(skb);
219
220 ret = vhci_create_device(data, opcode);
221 break;
222
223 default:
224 kfree_skb(skb);
225 return -EINVAL;
226 }
227
228 return (ret < 0) ? ret : len;
229}
230
231static inline ssize_t vhci_put_user(struct vhci_data *data,
232 struct sk_buff *skb,
233 char __user *buf, int count)
234{
235 char __user *ptr = buf;
236 int len;
237
238 len = min_t(unsigned int, skb->len, count);
239
240 if (copy_to_user(ptr, skb->data, len))
241 return -EFAULT;
242
243 if (!data->hdev)
244 return len;
245
246 data->hdev->stat.byte_tx += len;
247
248 switch (hci_skb_pkt_type(skb)) {
249 case HCI_COMMAND_PKT:
250 data->hdev->stat.cmd_tx++;
251 break;
252 case HCI_ACLDATA_PKT:
253 data->hdev->stat.acl_tx++;
254 break;
255 case HCI_SCODATA_PKT:
256 data->hdev->stat.sco_tx++;
257 break;
258 }
259
260 return len;
261}
262
263static ssize_t vhci_read(struct file *file,
264 char __user *buf, size_t count, loff_t *pos)
265{
266 struct vhci_data *data = file->private_data;
267 struct sk_buff *skb;
268 ssize_t ret = 0;
269
270 while (count) {
271 skb = skb_dequeue(&data->readq);
272 if (skb) {
273 ret = vhci_put_user(data, skb, buf, count);
274 if (ret < 0)
275 skb_queue_head(&data->readq, skb);
276 else
277 kfree_skb(skb);
278 break;
279 }
280
281 if (file->f_flags & O_NONBLOCK) {
282 ret = -EAGAIN;
283 break;
284 }
285
286 ret = wait_event_interruptible(data->read_wait,
287 !skb_queue_empty(&data->readq));
288 if (ret < 0)
289 break;
290 }
291
292 return ret;
293}
294
295static ssize_t vhci_write(struct kiocb *iocb, struct iov_iter *from)
296{
297 struct file *file = iocb->ki_filp;
298 struct vhci_data *data = file->private_data;
299
300 return vhci_get_user(data, from);
301}
302
303static __poll_t vhci_poll(struct file *file, poll_table *wait)
304{
305 struct vhci_data *data = file->private_data;
306
307 poll_wait(file, &data->read_wait, wait);
308
309 if (!skb_queue_empty(&data->readq))
310 return EPOLLIN | EPOLLRDNORM;
311
312 return EPOLLOUT | EPOLLWRNORM;
313}
314
315static void vhci_open_timeout(struct work_struct *work)
316{
317 struct vhci_data *data = container_of(work, struct vhci_data,
318 open_timeout.work);
319
320 vhci_create_device(data, amp ? HCI_AMP : HCI_PRIMARY);
321}
322
323static int vhci_open(struct inode *inode, struct file *file)
324{
325 struct vhci_data *data;
326
327 data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
328 if (!data)
329 return -ENOMEM;
330
331 skb_queue_head_init(&data->readq);
332 init_waitqueue_head(&data->read_wait);
333
334 mutex_init(&data->open_mutex);
335 INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
336
337 file->private_data = data;
338 nonseekable_open(inode, file);
339
340 schedule_delayed_work(&data->open_timeout, msecs_to_jiffies(1000));
341
342 return 0;
343}
344
345static int vhci_release(struct inode *inode, struct file *file)
346{
347 struct vhci_data *data = file->private_data;
348 struct hci_dev *hdev;
349
350 cancel_delayed_work_sync(&data->open_timeout);
351
352 hdev = data->hdev;
353
354 if (hdev) {
355 hci_unregister_dev(hdev);
356 hci_free_dev(hdev);
357 }
358
359 skb_queue_purge(&data->readq);
360 file->private_data = NULL;
361 kfree(data);
362
363 return 0;
364}
365
366static const struct file_operations vhci_fops = {
367 .owner = THIS_MODULE,
368 .read = vhci_read,
369 .write_iter = vhci_write,
370 .poll = vhci_poll,
371 .open = vhci_open,
372 .release = vhci_release,
373 .llseek = no_llseek,
374};
375
376static struct miscdevice vhci_miscdev = {
377 .name = "vhci",
378 .fops = &vhci_fops,
379 .minor = VHCI_MINOR,
380};
381module_misc_device(vhci_miscdev);
382
383module_param(amp, bool, 0644);
384MODULE_PARM_DESC(amp, "Create AMP controller device");
385
386MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
387MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
388MODULE_VERSION(VERSION);
389MODULE_LICENSE("GPL");
390MODULE_ALIAS("devname:vhci");
391MODULE_ALIAS_MISCDEV(VHCI_MINOR);
392