Blame view
drivers/bluetooth/hci_vhci.c
6.23 KB
1da177e4c
|
1 |
/* |
1da177e4c
|
2 |
* |
4aa769b99
|
3 4 |
* Bluetooth virtual HCI driver * |
9c724357f
|
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
|
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
|
24 |
*/ |
1da177e4c
|
25 |
|
1da177e4c
|
26 |
#include <linux/module.h> |
1da177e4c
|
27 |
#include <linux/kernel.h> |
4aa769b99
|
28 |
#include <linux/init.h> |
1da177e4c
|
29 |
#include <linux/slab.h> |
4aa769b99
|
30 31 32 |
#include <linux/types.h> #include <linux/errno.h> #include <linux/sched.h> |
1da177e4c
|
33 |
#include <linux/poll.h> |
1da177e4c
|
34 35 36 |
#include <linux/skbuff.h> #include <linux/miscdevice.h> |
1da177e4c
|
37 38 |
#include <net/bluetooth/bluetooth.h> #include <net/bluetooth/hci_core.h> |
1da177e4c
|
39 |
|
ac28494c5
|
40 |
#define VERSION "1.3" |
4aa769b99
|
41 |
|
4aa769b99
|
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
|
49 |
}; |
4aa769b99
|
50 |
static int vhci_open_dev(struct hci_dev *hdev) |
1da177e4c
|
51 52 |
{ set_bit(HCI_RUNNING, &hdev->flags); |
1da177e4c
|
53 |
|
1da177e4c
|
54 55 |
return 0; } |
4aa769b99
|
56 |
static int vhci_close_dev(struct hci_dev *hdev) |
1da177e4c
|
57 |
{ |
9c724357f
|
58 |
struct vhci_data *data = hdev->driver_data; |
4aa769b99
|
59 |
|
1da177e4c
|
60 61 |
if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) return 0; |
9c724357f
|
62 |
skb_queue_purge(&data->readq); |
4aa769b99
|
63 |
|
1da177e4c
|
64 65 |
return 0; } |
4aa769b99
|
66 |
static int vhci_flush(struct hci_dev *hdev) |
1da177e4c
|
67 |
{ |
9c724357f
|
68 |
struct vhci_data *data = hdev->driver_data; |
1da177e4c
|
69 |
|
9c724357f
|
70 |
skb_queue_purge(&data->readq); |
1da177e4c
|
71 |
|
4aa769b99
|
72 |
return 0; |
1da177e4c
|
73 |
} |
4aa769b99
|
74 |
static int vhci_send_frame(struct sk_buff *skb) |
1da177e4c
|
75 76 |
{ struct hci_dev* hdev = (struct hci_dev *) skb->dev; |
9c724357f
|
77 |
struct vhci_data *data; |
1da177e4c
|
78 79 |
if (!hdev) { |
4aa769b99
|
80 |
BT_ERR("Frame for unknown HCI device (hdev=NULL)"); |
1da177e4c
|
81 82 83 84 85 |
return -ENODEV; } if (!test_bit(HCI_RUNNING, &hdev->flags)) return -EBUSY; |
9c724357f
|
86 |
data = hdev->driver_data; |
1da177e4c
|
87 |
|
0d48d9394
|
88 |
memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1); |
9c724357f
|
89 |
skb_queue_tail(&data->readq, skb); |
4aa769b99
|
90 |
|
9c724357f
|
91 |
wake_up_interruptible(&data->read_wait); |
1da177e4c
|
92 93 94 |
return 0; } |
4aa769b99
|
95 96 97 |
static void vhci_destruct(struct hci_dev *hdev) { kfree(hdev->driver_data); |
1da177e4c
|
98 |
} |
9c724357f
|
99 |
static inline ssize_t vhci_get_user(struct vhci_data *data, |
4aa769b99
|
100 |
const char __user *buf, size_t count) |
1da177e4c
|
101 102 103 104 105 |
{ struct sk_buff *skb; if (count > HCI_MAX_FRAME_SIZE) return -EINVAL; |
4aa769b99
|
106 107 |
skb = bt_skb_alloc(count, GFP_KERNEL); if (!skb) |
1da177e4c
|
108 |
return -ENOMEM; |
4aa769b99
|
109 |
|
1da177e4c
|
110 111 112 113 |
if (copy_from_user(skb_put(skb, count), buf, count)) { kfree_skb(skb); return -EFAULT; } |
9c724357f
|
114 |
skb->dev = (void *) data->hdev; |
0d48d9394
|
115 |
bt_cb(skb)->pkt_type = *((__u8 *) skb->data); |
1da177e4c
|
116 117 118 119 120 |
skb_pull(skb, 1); hci_recv_frame(skb); return count; |
1da177e4c
|
121 |
} |
9c724357f
|
122 |
static inline ssize_t vhci_put_user(struct vhci_data *data, |
4aa769b99
|
123 |
struct sk_buff *skb, char __user *buf, int count) |
1da177e4c
|
124 |
{ |
1da177e4c
|
125 |
char __user *ptr = buf; |
4aa769b99
|
126 127 128 |
int len, total = 0; len = min_t(unsigned int, skb->len, count); |
1da177e4c
|
129 |
|
1da177e4c
|
130 131 |
if (copy_to_user(ptr, skb->data, len)) return -EFAULT; |
4aa769b99
|
132 |
|
1da177e4c
|
133 |
total += len; |
9c724357f
|
134 |
data->hdev->stat.byte_tx += len; |
4aa769b99
|
135 |
|
0d48d9394
|
136 137 |
switch (bt_cb(skb)->pkt_type) { case HCI_COMMAND_PKT: |
9c724357f
|
138 |
data->hdev->stat.cmd_tx++; |
0d48d9394
|
139 |
break; |
1da177e4c
|
140 |
|
0d48d9394
|
141 |
case HCI_ACLDATA_PKT: |
9c724357f
|
142 |
data->hdev->stat.acl_tx++; |
0d48d9394
|
143 |
break; |
1da177e4c
|
144 |
|
0d48d9394
|
145 |
case HCI_SCODATA_PKT: |
4f7ac1814
|
146 |
data->hdev->stat.sco_tx++; |
0d48d9394
|
147 |
break; |
1da177e4c
|
148 149 150 151 |
}; return total; } |
9c724357f
|
152 153 |
static ssize_t vhci_read(struct file *file, char __user *buf, size_t count, loff_t *pos) |
1da177e4c
|
154 |
{ |
9c724357f
|
155 |
struct vhci_data *data = file->private_data; |
1da177e4c
|
156 157 |
struct sk_buff *skb; ssize_t ret = 0; |
1da177e4c
|
158 |
while (count) { |
9c724357f
|
159 |
skb = skb_dequeue(&data->readq); |
4db7589f3
|
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
|
167 |
} |
4db7589f3
|
168 169 170 171 |
if (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; break; } |
1da177e4c
|
172 |
|
4db7589f3
|
173 174 175 176 |
ret = wait_event_interruptible(data->read_wait, !skb_queue_empty(&data->readq)); if (ret < 0) break; |
1da177e4c
|
177 |
} |
1da177e4c
|
178 179 180 |
return ret; } |
4aa769b99
|
181 182 |
static ssize_t vhci_write(struct file *file, const char __user *buf, size_t count, loff_t *pos) |
1da177e4c
|
183 |
{ |
9c724357f
|
184 |
struct vhci_data *data = file->private_data; |
1da177e4c
|
185 |
|
9c724357f
|
186 |
return vhci_get_user(data, buf, count); |
1da177e4c
|
187 |
} |
4aa769b99
|
188 |
static unsigned int vhci_poll(struct file *file, poll_table *wait) |
1da177e4c
|
189 |
{ |
9c724357f
|
190 |
struct vhci_data *data = file->private_data; |
1da177e4c
|
191 |
|
9c724357f
|
192 |
poll_wait(file, &data->read_wait, wait); |
1da177e4c
|
193 |
|
9c724357f
|
194 |
if (!skb_queue_empty(&data->readq)) |
4aa769b99
|
195 196 197 198 |
return POLLIN | POLLRDNORM; return POLLOUT | POLLWRNORM; } |
4aa769b99
|
199 |
static int vhci_open(struct inode *inode, struct file *file) |
1da177e4c
|
200 |
{ |
9c724357f
|
201 |
struct vhci_data *data; |
1da177e4c
|
202 |
struct hci_dev *hdev; |
9c724357f
|
203 204 |
data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL); if (!data) |
1da177e4c
|
205 |
return -ENOMEM; |
9c724357f
|
206 207 |
skb_queue_head_init(&data->readq); init_waitqueue_head(&data->read_wait); |
1da177e4c
|
208 |
|
1da177e4c
|
209 210 |
hdev = hci_alloc_dev(); if (!hdev) { |
9c724357f
|
211 |
kfree(data); |
1da177e4c
|
212 213 |
return -ENOMEM; } |
9c724357f
|
214 |
data->hdev = hdev; |
1da177e4c
|
215 |
|
c13854cef
|
216 |
hdev->bus = HCI_VIRTUAL; |
9c724357f
|
217 |
hdev->driver_data = data; |
1da177e4c
|
218 |
|
4aa769b99
|
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
|
224 225 |
hdev->owner = THIS_MODULE; |
4aa769b99
|
226 |
|
1da177e4c
|
227 |
if (hci_register_dev(hdev) < 0) { |
4aa769b99
|
228 |
BT_ERR("Can't register HCI device"); |
9c724357f
|
229 |
kfree(data); |
1da177e4c
|
230 231 232 |
hci_free_dev(hdev); return -EBUSY; } |
9c724357f
|
233 |
file->private_data = data; |
4aa769b99
|
234 235 |
return nonseekable_open(inode, file); |
1da177e4c
|
236 |
} |
4aa769b99
|
237 |
static int vhci_release(struct inode *inode, struct file *file) |
1da177e4c
|
238 |
{ |
9c724357f
|
239 240 |
struct vhci_data *data = file->private_data; struct hci_dev *hdev = data->hdev; |
1da177e4c
|
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
|
249 250 251 |
return 0; } |
2b8693c06
|
252 |
static const struct file_operations vhci_fops = { |
fed4c2508
|
253 |
.owner = THIS_MODULE, |
4aa769b99
|
254 255 256 |
.read = vhci_read, .write = vhci_write, .poll = vhci_poll, |
4aa769b99
|
257 258 |
.open = vhci_open, .release = vhci_release, |
1da177e4c
|
259 |
}; |
4aa769b99
|
260 |
static struct miscdevice vhci_miscdev= { |
ac28494c5
|
261 262 263 |
.name = "vhci", .fops = &vhci_fops, .minor = MISC_DYNAMIC_MINOR, |
1da177e4c
|
264 |
}; |
4aa769b99
|
265 |
static int __init vhci_init(void) |
1da177e4c
|
266 |
{ |
4aa769b99
|
267 |
BT_INFO("Virtual HCI driver ver %s", VERSION); |
1da177e4c
|
268 |
|
329ab1b3e
|
269 |
return misc_register(&vhci_miscdev); |
1da177e4c
|
270 |
} |
4aa769b99
|
271 |
static void __exit vhci_exit(void) |
1da177e4c
|
272 |
{ |
329ab1b3e
|
273 |
misc_deregister(&vhci_miscdev); |
1da177e4c
|
274 |
} |
4aa769b99
|
275 276 |
module_init(vhci_init); module_exit(vhci_exit); |
63fbd24e5
|
277 |
MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
4aa769b99
|
278 279 280 |
MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION); MODULE_VERSION(VERSION); MODULE_LICENSE("GPL"); |