hci_vhci.c 6.6 KB

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