hci_vhci.c 8.2 KB

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