hci_vhci.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 <asm/unaligned.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. #define VERSION "1.4"
  39. static bool amp;
  40. struct vhci_data {
  41. struct hci_dev *hdev;
  42. wait_queue_head_t read_wait;
  43. struct sk_buff_head readq;
  44. struct delayed_work open_timeout;
  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 = hci_get_drvdata(hdev);
  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 = hci_get_drvdata(hdev);
  62. skb_queue_purge(&data->readq);
  63. return 0;
  64. }
  65. static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  66. {
  67. struct vhci_data *data = hci_get_drvdata(hdev);
  68. if (!test_bit(HCI_RUNNING, &hdev->flags))
  69. return -EBUSY;
  70. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  71. skb_queue_tail(&data->readq, skb);
  72. wake_up_interruptible(&data->read_wait);
  73. return 0;
  74. }
  75. static int vhci_create_device(struct vhci_data *data, __u8 dev_type)
  76. {
  77. struct hci_dev *hdev;
  78. struct sk_buff *skb;
  79. skb = bt_skb_alloc(4, GFP_KERNEL);
  80. if (!skb)
  81. return -ENOMEM;
  82. hdev = hci_alloc_dev();
  83. if (!hdev) {
  84. kfree_skb(skb);
  85. return -ENOMEM;
  86. }
  87. data->hdev = hdev;
  88. hdev->bus = HCI_VIRTUAL;
  89. hdev->dev_type = dev_type;
  90. hci_set_drvdata(hdev, data);
  91. hdev->open = vhci_open_dev;
  92. hdev->close = vhci_close_dev;
  93. hdev->flush = vhci_flush;
  94. hdev->send = vhci_send_frame;
  95. if (hci_register_dev(hdev) < 0) {
  96. BT_ERR("Can't register HCI device");
  97. hci_free_dev(hdev);
  98. data->hdev = NULL;
  99. kfree_skb(skb);
  100. return -EBUSY;
  101. }
  102. bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
  103. *skb_put(skb, 1) = 0xff;
  104. *skb_put(skb, 1) = dev_type;
  105. put_unaligned_le16(hdev->id, skb_put(skb, 2));
  106. skb_queue_tail(&data->readq, skb);
  107. wake_up_interruptible(&data->read_wait);
  108. return 0;
  109. }
  110. static inline ssize_t vhci_get_user(struct vhci_data *data,
  111. const struct iovec *iov,
  112. unsigned long count)
  113. {
  114. size_t len = iov_length(iov, count);
  115. struct sk_buff *skb;
  116. __u8 pkt_type, dev_type;
  117. unsigned long i;
  118. int ret;
  119. if (len < 2 || len > HCI_MAX_FRAME_SIZE)
  120. return -EINVAL;
  121. skb = bt_skb_alloc(len, GFP_KERNEL);
  122. if (!skb)
  123. return -ENOMEM;
  124. for (i = 0; i < count; i++) {
  125. if (copy_from_user(skb_put(skb, iov[i].iov_len),
  126. iov[i].iov_base, iov[i].iov_len)) {
  127. kfree_skb(skb);
  128. return -EFAULT;
  129. }
  130. }
  131. pkt_type = *((__u8 *) skb->data);
  132. skb_pull(skb, 1);
  133. switch (pkt_type) {
  134. case HCI_EVENT_PKT:
  135. case HCI_ACLDATA_PKT:
  136. case HCI_SCODATA_PKT:
  137. if (!data->hdev) {
  138. kfree_skb(skb);
  139. return -ENODEV;
  140. }
  141. bt_cb(skb)->pkt_type = pkt_type;
  142. ret = hci_recv_frame(data->hdev, skb);
  143. break;
  144. case HCI_VENDOR_PKT:
  145. if (data->hdev) {
  146. kfree_skb(skb);
  147. return -EBADFD;
  148. }
  149. cancel_delayed_work_sync(&data->open_timeout);
  150. dev_type = *((__u8 *) skb->data);
  151. skb_pull(skb, 1);
  152. if (skb->len > 0) {
  153. kfree_skb(skb);
  154. return -EINVAL;
  155. }
  156. kfree_skb(skb);
  157. if (dev_type != HCI_BREDR && dev_type != HCI_AMP)
  158. return -EINVAL;
  159. ret = vhci_create_device(data, dev_type);
  160. break;
  161. default:
  162. kfree_skb(skb);
  163. return -EINVAL;
  164. }
  165. return (ret < 0) ? ret : len;
  166. }
  167. static inline ssize_t vhci_put_user(struct vhci_data *data,
  168. struct sk_buff *skb,
  169. char __user *buf, int count)
  170. {
  171. char __user *ptr = buf;
  172. int len;
  173. len = min_t(unsigned int, skb->len, count);
  174. if (copy_to_user(ptr, skb->data, len))
  175. return -EFAULT;
  176. if (!data->hdev)
  177. return len;
  178. data->hdev->stat.byte_tx += len;
  179. switch (bt_cb(skb)->pkt_type) {
  180. case HCI_COMMAND_PKT:
  181. data->hdev->stat.cmd_tx++;
  182. break;
  183. case HCI_ACLDATA_PKT:
  184. data->hdev->stat.acl_tx++;
  185. break;
  186. case HCI_SCODATA_PKT:
  187. data->hdev->stat.sco_tx++;
  188. break;
  189. }
  190. return len;
  191. }
  192. static ssize_t vhci_read(struct file *file,
  193. char __user *buf, size_t count, loff_t *pos)
  194. {
  195. struct vhci_data *data = file->private_data;
  196. struct sk_buff *skb;
  197. ssize_t ret = 0;
  198. while (count) {
  199. skb = skb_dequeue(&data->readq);
  200. if (skb) {
  201. ret = vhci_put_user(data, skb, buf, count);
  202. if (ret < 0)
  203. skb_queue_head(&data->readq, skb);
  204. else
  205. kfree_skb(skb);
  206. break;
  207. }
  208. if (file->f_flags & O_NONBLOCK) {
  209. ret = -EAGAIN;
  210. break;
  211. }
  212. ret = wait_event_interruptible(data->read_wait,
  213. !skb_queue_empty(&data->readq));
  214. if (ret < 0)
  215. break;
  216. }
  217. return ret;
  218. }
  219. static ssize_t vhci_write(struct kiocb *iocb, const struct iovec *iov,
  220. unsigned long count, loff_t pos)
  221. {
  222. struct file *file = iocb->ki_filp;
  223. struct vhci_data *data = file->private_data;
  224. return vhci_get_user(data, iov, count);
  225. }
  226. static unsigned int vhci_poll(struct file *file, poll_table *wait)
  227. {
  228. struct vhci_data *data = file->private_data;
  229. poll_wait(file, &data->read_wait, wait);
  230. if (!skb_queue_empty(&data->readq))
  231. return POLLIN | POLLRDNORM;
  232. return POLLOUT | POLLWRNORM;
  233. }
  234. static void vhci_open_timeout(struct work_struct *work)
  235. {
  236. struct vhci_data *data = container_of(work, struct vhci_data,
  237. open_timeout.work);
  238. vhci_create_device(data, amp ? HCI_AMP : HCI_BREDR);
  239. }
  240. static int vhci_open(struct inode *inode, struct file *file)
  241. {
  242. struct vhci_data *data;
  243. data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
  244. if (!data)
  245. return -ENOMEM;
  246. skb_queue_head_init(&data->readq);
  247. init_waitqueue_head(&data->read_wait);
  248. INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
  249. file->private_data = data;
  250. nonseekable_open(inode, file);
  251. schedule_delayed_work(&data->open_timeout, msecs_to_jiffies(1000));
  252. return 0;
  253. }
  254. static int vhci_release(struct inode *inode, struct file *file)
  255. {
  256. struct vhci_data *data = file->private_data;
  257. struct hci_dev *hdev = data->hdev;
  258. cancel_delayed_work_sync(&data->open_timeout);
  259. if (hdev) {
  260. hci_unregister_dev(hdev);
  261. hci_free_dev(hdev);
  262. }
  263. file->private_data = NULL;
  264. kfree(data);
  265. return 0;
  266. }
  267. static const struct file_operations vhci_fops = {
  268. .owner = THIS_MODULE,
  269. .read = vhci_read,
  270. .aio_write = vhci_write,
  271. .poll = vhci_poll,
  272. .open = vhci_open,
  273. .release = vhci_release,
  274. .llseek = no_llseek,
  275. };
  276. static struct miscdevice vhci_miscdev= {
  277. .name = "vhci",
  278. .fops = &vhci_fops,
  279. .minor = VHCI_MINOR,
  280. };
  281. static int __init vhci_init(void)
  282. {
  283. BT_INFO("Virtual HCI driver ver %s", VERSION);
  284. return misc_register(&vhci_miscdev);
  285. }
  286. static void __exit vhci_exit(void)
  287. {
  288. misc_deregister(&vhci_miscdev);
  289. }
  290. module_init(vhci_init);
  291. module_exit(vhci_exit);
  292. module_param(amp, bool, 0644);
  293. MODULE_PARM_DESC(amp, "Create AMP controller device");
  294. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  295. MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
  296. MODULE_VERSION(VERSION);
  297. MODULE_LICENSE("GPL");
  298. MODULE_ALIAS("devname:vhci");
  299. MODULE_ALIAS_MISCDEV(VHCI_MINOR);