hci_vhci.c 7.6 KB

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