hci_vhci.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. *
  3. * Bluetooth virtual HCI driver
  4. *
  5. * Copyright (C) 2000-2001 Qualcomm Incorporated
  6. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  7. * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/config.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #include <linux/errno.h>
  32. #include <linux/sched.h>
  33. #include <linux/poll.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/miscdevice.h>
  36. #include <net/bluetooth/bluetooth.h>
  37. #include <net/bluetooth/hci_core.h>
  38. #ifndef CONFIG_BT_HCIVHCI_DEBUG
  39. #undef BT_DBG
  40. #define BT_DBG(D...)
  41. #endif
  42. #define VERSION "1.2"
  43. static int minor = MISC_DYNAMIC_MINOR;
  44. struct vhci_data {
  45. struct hci_dev *hdev;
  46. unsigned long flags;
  47. wait_queue_head_t read_wait;
  48. struct sk_buff_head readq;
  49. struct fasync_struct *fasync;
  50. };
  51. #define VHCI_FASYNC 0x0010
  52. static struct miscdevice vhci_miscdev;
  53. static int vhci_open_dev(struct hci_dev *hdev)
  54. {
  55. set_bit(HCI_RUNNING, &hdev->flags);
  56. return 0;
  57. }
  58. static int vhci_close_dev(struct hci_dev *hdev)
  59. {
  60. struct vhci_data *vhci = hdev->driver_data;
  61. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  62. return 0;
  63. skb_queue_purge(&vhci->readq);
  64. return 0;
  65. }
  66. static int vhci_flush(struct hci_dev *hdev)
  67. {
  68. struct vhci_data *vhci = hdev->driver_data;
  69. skb_queue_purge(&vhci->readq);
  70. return 0;
  71. }
  72. static int vhci_send_frame(struct sk_buff *skb)
  73. {
  74. struct hci_dev* hdev = (struct hci_dev *) skb->dev;
  75. struct vhci_data *vhci;
  76. if (!hdev) {
  77. BT_ERR("Frame for unknown HCI device (hdev=NULL)");
  78. return -ENODEV;
  79. }
  80. if (!test_bit(HCI_RUNNING, &hdev->flags))
  81. return -EBUSY;
  82. vhci = hdev->driver_data;
  83. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  84. skb_queue_tail(&vhci->readq, skb);
  85. if (vhci->flags & VHCI_FASYNC)
  86. kill_fasync(&vhci->fasync, SIGIO, POLL_IN);
  87. wake_up_interruptible(&vhci->read_wait);
  88. return 0;
  89. }
  90. static void vhci_destruct(struct hci_dev *hdev)
  91. {
  92. kfree(hdev->driver_data);
  93. }
  94. static inline ssize_t vhci_get_user(struct vhci_data *vhci,
  95. const char __user *buf, size_t count)
  96. {
  97. struct sk_buff *skb;
  98. if (count > HCI_MAX_FRAME_SIZE)
  99. return -EINVAL;
  100. skb = bt_skb_alloc(count, GFP_KERNEL);
  101. if (!skb)
  102. return -ENOMEM;
  103. if (copy_from_user(skb_put(skb, count), buf, count)) {
  104. kfree_skb(skb);
  105. return -EFAULT;
  106. }
  107. skb->dev = (void *) vhci->hdev;
  108. bt_cb(skb)->pkt_type = *((__u8 *) skb->data);
  109. skb_pull(skb, 1);
  110. hci_recv_frame(skb);
  111. return count;
  112. }
  113. static inline ssize_t vhci_put_user(struct vhci_data *vhci,
  114. struct sk_buff *skb, char __user *buf, int count)
  115. {
  116. char __user *ptr = buf;
  117. int len, total = 0;
  118. len = min_t(unsigned int, skb->len, count);
  119. if (copy_to_user(ptr, skb->data, len))
  120. return -EFAULT;
  121. total += len;
  122. vhci->hdev->stat.byte_tx += len;
  123. switch (bt_cb(skb)->pkt_type) {
  124. case HCI_COMMAND_PKT:
  125. vhci->hdev->stat.cmd_tx++;
  126. break;
  127. case HCI_ACLDATA_PKT:
  128. vhci->hdev->stat.acl_tx++;
  129. break;
  130. case HCI_SCODATA_PKT:
  131. vhci->hdev->stat.cmd_tx++;
  132. break;
  133. };
  134. return total;
  135. }
  136. static loff_t vhci_llseek(struct file * file, loff_t offset, int origin)
  137. {
  138. return -ESPIPE;
  139. }
  140. static ssize_t vhci_read(struct file * file, char __user * buf, size_t count, loff_t *pos)
  141. {
  142. DECLARE_WAITQUEUE(wait, current);
  143. struct vhci_data *vhci = file->private_data;
  144. struct sk_buff *skb;
  145. ssize_t ret = 0;
  146. add_wait_queue(&vhci->read_wait, &wait);
  147. while (count) {
  148. set_current_state(TASK_INTERRUPTIBLE);
  149. skb = skb_dequeue(&vhci->readq);
  150. if (!skb) {
  151. if (file->f_flags & O_NONBLOCK) {
  152. ret = -EAGAIN;
  153. break;
  154. }
  155. if (signal_pending(current)) {
  156. ret = -ERESTARTSYS;
  157. break;
  158. }
  159. schedule();
  160. continue;
  161. }
  162. if (access_ok(VERIFY_WRITE, buf, count))
  163. ret = vhci_put_user(vhci, skb, buf, count);
  164. else
  165. ret = -EFAULT;
  166. kfree_skb(skb);
  167. break;
  168. }
  169. set_current_state(TASK_RUNNING);
  170. remove_wait_queue(&vhci->read_wait, &wait);
  171. return ret;
  172. }
  173. static ssize_t vhci_write(struct file *file,
  174. const char __user *buf, size_t count, loff_t *pos)
  175. {
  176. struct vhci_data *vhci = file->private_data;
  177. if (!access_ok(VERIFY_READ, buf, count))
  178. return -EFAULT;
  179. return vhci_get_user(vhci, buf, count);
  180. }
  181. static unsigned int vhci_poll(struct file *file, poll_table *wait)
  182. {
  183. struct vhci_data *vhci = file->private_data;
  184. poll_wait(file, &vhci->read_wait, wait);
  185. if (!skb_queue_empty(&vhci->readq))
  186. return POLLIN | POLLRDNORM;
  187. return POLLOUT | POLLWRNORM;
  188. }
  189. static int vhci_ioctl(struct inode *inode, struct file *file,
  190. unsigned int cmd, unsigned long arg)
  191. {
  192. return -EINVAL;
  193. }
  194. static int vhci_open(struct inode *inode, struct file *file)
  195. {
  196. struct vhci_data *vhci;
  197. struct hci_dev *hdev;
  198. vhci = kmalloc(sizeof(struct vhci_data), GFP_KERNEL);
  199. if (!vhci)
  200. return -ENOMEM;
  201. memset(vhci, 0, sizeof(struct vhci_data));
  202. skb_queue_head_init(&vhci->readq);
  203. init_waitqueue_head(&vhci->read_wait);
  204. hdev = hci_alloc_dev();
  205. if (!hdev) {
  206. kfree(vhci);
  207. return -ENOMEM;
  208. }
  209. vhci->hdev = hdev;
  210. hdev->type = HCI_VHCI;
  211. hdev->driver_data = vhci;
  212. SET_HCIDEV_DEV(hdev, vhci_miscdev.dev);
  213. hdev->open = vhci_open_dev;
  214. hdev->close = vhci_close_dev;
  215. hdev->flush = vhci_flush;
  216. hdev->send = vhci_send_frame;
  217. hdev->destruct = vhci_destruct;
  218. hdev->owner = THIS_MODULE;
  219. if (hci_register_dev(hdev) < 0) {
  220. BT_ERR("Can't register HCI device");
  221. kfree(vhci);
  222. hci_free_dev(hdev);
  223. return -EBUSY;
  224. }
  225. file->private_data = vhci;
  226. return nonseekable_open(inode, file);
  227. }
  228. static int vhci_release(struct inode *inode, struct file *file)
  229. {
  230. struct vhci_data *vhci = file->private_data;
  231. struct hci_dev *hdev = vhci->hdev;
  232. if (hci_unregister_dev(hdev) < 0) {
  233. BT_ERR("Can't unregister HCI device %s", hdev->name);
  234. }
  235. hci_free_dev(hdev);
  236. file->private_data = NULL;
  237. return 0;
  238. }
  239. static int vhci_fasync(int fd, struct file *file, int on)
  240. {
  241. struct vhci_data *vhci = file->private_data;
  242. int err;
  243. err = fasync_helper(fd, file, on, &vhci->fasync);
  244. if (err < 0)
  245. return err;
  246. if (on)
  247. vhci->flags |= VHCI_FASYNC;
  248. else
  249. vhci->flags &= ~VHCI_FASYNC;
  250. return 0;
  251. }
  252. static struct file_operations vhci_fops = {
  253. .owner = THIS_MODULE,
  254. .llseek = vhci_llseek,
  255. .read = vhci_read,
  256. .write = vhci_write,
  257. .poll = vhci_poll,
  258. .ioctl = vhci_ioctl,
  259. .open = vhci_open,
  260. .release = vhci_release,
  261. .fasync = vhci_fasync,
  262. };
  263. static struct miscdevice vhci_miscdev= {
  264. .name = "vhci",
  265. .fops = &vhci_fops,
  266. };
  267. static int __init vhci_init(void)
  268. {
  269. BT_INFO("Virtual HCI driver ver %s", VERSION);
  270. vhci_miscdev.minor = minor;
  271. if (misc_register(&vhci_miscdev) < 0) {
  272. BT_ERR("Can't register misc device with minor %d", minor);
  273. return -EIO;
  274. }
  275. return 0;
  276. }
  277. static void __exit vhci_exit(void)
  278. {
  279. if (misc_deregister(&vhci_miscdev) < 0)
  280. BT_ERR("Can't unregister misc device with minor %d", minor);
  281. }
  282. module_init(vhci_init);
  283. module_exit(vhci_exit);
  284. module_param(minor, int, 0444);
  285. MODULE_PARM_DESC(minor, "Miscellaneous minor device number");
  286. MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
  287. MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
  288. MODULE_VERSION(VERSION);
  289. MODULE_LICENSE("GPL");