hci_vhci.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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-2006 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. #define VERSION "1.3"
  38. static int minor = MISC_DYNAMIC_MINOR;
  39. struct vhci_data {
  40. struct hci_dev *hdev;
  41. unsigned long flags;
  42. wait_queue_head_t read_wait;
  43. struct sk_buff_head readq;
  44. };
  45. static int vhci_open_dev(struct hci_dev *hdev)
  46. {
  47. set_bit(HCI_RUNNING, &hdev->flags);
  48. return 0;
  49. }
  50. static int vhci_close_dev(struct hci_dev *hdev)
  51. {
  52. struct vhci_data *data = hdev->driver_data;
  53. if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
  54. return 0;
  55. skb_queue_purge(&data->readq);
  56. return 0;
  57. }
  58. static int vhci_flush(struct hci_dev *hdev)
  59. {
  60. struct vhci_data *data = hdev->driver_data;
  61. skb_queue_purge(&data->readq);
  62. return 0;
  63. }
  64. static int vhci_send_frame(struct sk_buff *skb)
  65. {
  66. struct hci_dev* hdev = (struct hci_dev *) skb->dev;
  67. struct vhci_data *data;
  68. if (!hdev) {
  69. BT_ERR("Frame for unknown HCI device (hdev=NULL)");
  70. return -ENODEV;
  71. }
  72. if (!test_bit(HCI_RUNNING, &hdev->flags))
  73. return -EBUSY;
  74. data = hdev->driver_data;
  75. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  76. skb_queue_tail(&data->readq, skb);
  77. wake_up_interruptible(&data->read_wait);
  78. return 0;
  79. }
  80. static void vhci_destruct(struct hci_dev *hdev)
  81. {
  82. kfree(hdev->driver_data);
  83. }
  84. static inline ssize_t vhci_get_user(struct vhci_data *data,
  85. const char __user *buf, size_t count)
  86. {
  87. struct sk_buff *skb;
  88. if (count > HCI_MAX_FRAME_SIZE)
  89. return -EINVAL;
  90. skb = bt_skb_alloc(count, GFP_KERNEL);
  91. if (!skb)
  92. return -ENOMEM;
  93. if (copy_from_user(skb_put(skb, count), buf, count)) {
  94. kfree_skb(skb);
  95. return -EFAULT;
  96. }
  97. skb->dev = (void *) data->hdev;
  98. bt_cb(skb)->pkt_type = *((__u8 *) skb->data);
  99. skb_pull(skb, 1);
  100. hci_recv_frame(skb);
  101. return count;
  102. }
  103. static inline ssize_t vhci_put_user(struct vhci_data *data,
  104. struct sk_buff *skb, char __user *buf, int count)
  105. {
  106. char __user *ptr = buf;
  107. int len, total = 0;
  108. len = min_t(unsigned int, skb->len, count);
  109. if (copy_to_user(ptr, skb->data, len))
  110. return -EFAULT;
  111. total += len;
  112. data->hdev->stat.byte_tx += len;
  113. switch (bt_cb(skb)->pkt_type) {
  114. case HCI_COMMAND_PKT:
  115. data->hdev->stat.cmd_tx++;
  116. break;
  117. case HCI_ACLDATA_PKT:
  118. data->hdev->stat.acl_tx++;
  119. break;
  120. case HCI_SCODATA_PKT:
  121. data->hdev->stat.cmd_tx++;
  122. break;
  123. };
  124. return total;
  125. }
  126. static ssize_t vhci_read(struct file *file,
  127. char __user *buf, size_t count, loff_t *pos)
  128. {
  129. struct vhci_data *data = file->private_data;
  130. struct sk_buff *skb;
  131. ssize_t ret = 0;
  132. while (count) {
  133. skb = skb_dequeue(&data->readq);
  134. if (skb) {
  135. ret = vhci_put_user(data, skb, buf, count);
  136. if (ret < 0)
  137. skb_queue_head(&data->readq, skb);
  138. else
  139. kfree_skb(skb);
  140. break;
  141. }
  142. if (file->f_flags & O_NONBLOCK) {
  143. ret = -EAGAIN;
  144. break;
  145. }
  146. ret = wait_event_interruptible(data->read_wait,
  147. !skb_queue_empty(&data->readq));
  148. if (ret < 0)
  149. break;
  150. }
  151. return ret;
  152. }
  153. static ssize_t vhci_write(struct file *file,
  154. const char __user *buf, size_t count, loff_t *pos)
  155. {
  156. struct vhci_data *data = file->private_data;
  157. return vhci_get_user(data, buf, count);
  158. }
  159. static unsigned int vhci_poll(struct file *file, poll_table *wait)
  160. {
  161. struct vhci_data *data = file->private_data;
  162. poll_wait(file, &data->read_wait, wait);
  163. if (!skb_queue_empty(&data->readq))
  164. return POLLIN | POLLRDNORM;
  165. return POLLOUT | POLLWRNORM;
  166. }
  167. static int vhci_ioctl(struct inode *inode, struct file *file,
  168. unsigned int cmd, unsigned long arg)
  169. {
  170. return -EINVAL;
  171. }
  172. static int vhci_open(struct inode *inode, struct file *file)
  173. {
  174. struct vhci_data *data;
  175. struct hci_dev *hdev;
  176. data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
  177. if (!data)
  178. return -ENOMEM;
  179. skb_queue_head_init(&data->readq);
  180. init_waitqueue_head(&data->read_wait);
  181. hdev = hci_alloc_dev();
  182. if (!hdev) {
  183. kfree(data);
  184. return -ENOMEM;
  185. }
  186. data->hdev = hdev;
  187. hdev->type = HCI_VIRTUAL;
  188. hdev->driver_data = data;
  189. hdev->open = vhci_open_dev;
  190. hdev->close = vhci_close_dev;
  191. hdev->flush = vhci_flush;
  192. hdev->send = vhci_send_frame;
  193. hdev->destruct = vhci_destruct;
  194. hdev->owner = THIS_MODULE;
  195. if (hci_register_dev(hdev) < 0) {
  196. BT_ERR("Can't register HCI device");
  197. kfree(data);
  198. hci_free_dev(hdev);
  199. return -EBUSY;
  200. }
  201. file->private_data = data;
  202. return nonseekable_open(inode, file);
  203. }
  204. static int vhci_release(struct inode *inode, struct file *file)
  205. {
  206. struct vhci_data *data = file->private_data;
  207. struct hci_dev *hdev = data->hdev;
  208. if (hci_unregister_dev(hdev) < 0) {
  209. BT_ERR("Can't unregister HCI device %s", hdev->name);
  210. }
  211. hci_free_dev(hdev);
  212. file->private_data = NULL;
  213. return 0;
  214. }
  215. static const struct file_operations vhci_fops = {
  216. .read = vhci_read,
  217. .write = vhci_write,
  218. .poll = vhci_poll,
  219. .ioctl = vhci_ioctl,
  220. .open = vhci_open,
  221. .release = vhci_release,
  222. };
  223. static struct miscdevice vhci_miscdev= {
  224. .name = "vhci",
  225. .fops = &vhci_fops,
  226. .minor = MISC_DYNAMIC_MINOR,
  227. };
  228. static int __init vhci_init(void)
  229. {
  230. BT_INFO("Virtual HCI driver ver %s", VERSION);
  231. if (misc_register(&vhci_miscdev) < 0) {
  232. BT_ERR("Can't register misc device with minor %d", minor);
  233. return -EIO;
  234. }
  235. return 0;
  236. }
  237. static void __exit vhci_exit(void)
  238. {
  239. if (misc_deregister(&vhci_miscdev) < 0)
  240. BT_ERR("Can't unregister misc device with minor %d", minor);
  241. }
  242. module_init(vhci_init);
  243. module_exit(vhci_exit);
  244. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  245. MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
  246. MODULE_VERSION(VERSION);
  247. MODULE_LICENSE("GPL");