hci_vhci.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Bluetooth virtual HCI driver
  5. *
  6. * Copyright (C) 2000-2001 Qualcomm Incorporated
  7. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  8. * Copyright (C) 2004-2006 Marcel Holtmann <marcel@holtmann.org>
  9. */
  10. #include <linux/module.h>
  11. #include <asm/unaligned.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/sched.h>
  18. #include <linux/poll.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/miscdevice.h>
  21. #include <net/bluetooth/bluetooth.h>
  22. #include <net/bluetooth/hci_core.h>
  23. #define VERSION "1.5"
  24. static bool amp;
  25. struct vhci_data {
  26. struct hci_dev *hdev;
  27. wait_queue_head_t read_wait;
  28. struct sk_buff_head readq;
  29. struct mutex open_mutex;
  30. struct delayed_work open_timeout;
  31. };
  32. static int vhci_open_dev(struct hci_dev *hdev)
  33. {
  34. return 0;
  35. }
  36. static int vhci_close_dev(struct hci_dev *hdev)
  37. {
  38. struct vhci_data *data = hci_get_drvdata(hdev);
  39. skb_queue_purge(&data->readq);
  40. return 0;
  41. }
  42. static int vhci_flush(struct hci_dev *hdev)
  43. {
  44. struct vhci_data *data = hci_get_drvdata(hdev);
  45. skb_queue_purge(&data->readq);
  46. return 0;
  47. }
  48. static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  49. {
  50. struct vhci_data *data = hci_get_drvdata(hdev);
  51. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  52. skb_queue_tail(&data->readq, skb);
  53. wake_up_interruptible(&data->read_wait);
  54. return 0;
  55. }
  56. static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
  57. {
  58. struct hci_dev *hdev;
  59. struct sk_buff *skb;
  60. __u8 dev_type;
  61. if (data->hdev)
  62. return -EBADFD;
  63. /* bits 0-1 are dev_type (Primary or AMP) */
  64. dev_type = opcode & 0x03;
  65. if (dev_type != HCI_PRIMARY && dev_type != HCI_AMP)
  66. return -EINVAL;
  67. /* bits 2-5 are reserved (must be zero) */
  68. if (opcode & 0x3c)
  69. return -EINVAL;
  70. skb = bt_skb_alloc(4, GFP_KERNEL);
  71. if (!skb)
  72. return -ENOMEM;
  73. hdev = hci_alloc_dev();
  74. if (!hdev) {
  75. kfree_skb(skb);
  76. return -ENOMEM;
  77. }
  78. data->hdev = hdev;
  79. hdev->bus = HCI_VIRTUAL;
  80. hdev->dev_type = dev_type;
  81. hci_set_drvdata(hdev, data);
  82. hdev->open = vhci_open_dev;
  83. hdev->close = vhci_close_dev;
  84. hdev->flush = vhci_flush;
  85. hdev->send = vhci_send_frame;
  86. /* bit 6 is for external configuration */
  87. if (opcode & 0x40)
  88. set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
  89. /* bit 7 is for raw device */
  90. if (opcode & 0x80)
  91. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  92. set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
  93. if (hci_register_dev(hdev) < 0) {
  94. BT_ERR("Can't register HCI device");
  95. hci_free_dev(hdev);
  96. data->hdev = NULL;
  97. kfree_skb(skb);
  98. return -EBUSY;
  99. }
  100. hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
  101. skb_put_u8(skb, 0xff);
  102. skb_put_u8(skb, opcode);
  103. put_unaligned_le16(hdev->id, skb_put(skb, 2));
  104. skb_queue_tail(&data->readq, skb);
  105. wake_up_interruptible(&data->read_wait);
  106. return 0;
  107. }
  108. static int vhci_create_device(struct vhci_data *data, __u8 opcode)
  109. {
  110. int err;
  111. mutex_lock(&data->open_mutex);
  112. err = __vhci_create_device(data, opcode);
  113. mutex_unlock(&data->open_mutex);
  114. return err;
  115. }
  116. static inline ssize_t vhci_get_user(struct vhci_data *data,
  117. struct iov_iter *from)
  118. {
  119. size_t len = iov_iter_count(from);
  120. struct sk_buff *skb;
  121. __u8 pkt_type, opcode;
  122. int ret;
  123. if (len < 2 || len > HCI_MAX_FRAME_SIZE)
  124. return -EINVAL;
  125. skb = bt_skb_alloc(len, GFP_KERNEL);
  126. if (!skb)
  127. return -ENOMEM;
  128. if (!copy_from_iter_full(skb_put(skb, len), len, from)) {
  129. kfree_skb(skb);
  130. return -EFAULT;
  131. }
  132. pkt_type = *((__u8 *) skb->data);
  133. skb_pull(skb, 1);
  134. switch (pkt_type) {
  135. case HCI_EVENT_PKT:
  136. case HCI_ACLDATA_PKT:
  137. case HCI_SCODATA_PKT:
  138. case HCI_ISODATA_PKT:
  139. if (!data->hdev) {
  140. kfree_skb(skb);
  141. return -ENODEV;
  142. }
  143. hci_skb_pkt_type(skb) = pkt_type;
  144. ret = hci_recv_frame(data->hdev, skb);
  145. break;
  146. case HCI_VENDOR_PKT:
  147. cancel_delayed_work_sync(&data->open_timeout);
  148. opcode = *((__u8 *) skb->data);
  149. skb_pull(skb, 1);
  150. if (skb->len > 0) {
  151. kfree_skb(skb);
  152. return -EINVAL;
  153. }
  154. kfree_skb(skb);
  155. ret = vhci_create_device(data, opcode);
  156. break;
  157. default:
  158. kfree_skb(skb);
  159. return -EINVAL;
  160. }
  161. return (ret < 0) ? ret : len;
  162. }
  163. static inline ssize_t vhci_put_user(struct vhci_data *data,
  164. struct sk_buff *skb,
  165. char __user *buf, int count)
  166. {
  167. char __user *ptr = buf;
  168. int len;
  169. len = min_t(unsigned int, skb->len, count);
  170. if (copy_to_user(ptr, skb->data, len))
  171. return -EFAULT;
  172. if (!data->hdev)
  173. return len;
  174. data->hdev->stat.byte_tx += len;
  175. switch (hci_skb_pkt_type(skb)) {
  176. case HCI_COMMAND_PKT:
  177. data->hdev->stat.cmd_tx++;
  178. break;
  179. case HCI_ACLDATA_PKT:
  180. data->hdev->stat.acl_tx++;
  181. break;
  182. case HCI_SCODATA_PKT:
  183. data->hdev->stat.sco_tx++;
  184. break;
  185. }
  186. return len;
  187. }
  188. static ssize_t vhci_read(struct file *file,
  189. char __user *buf, size_t count, loff_t *pos)
  190. {
  191. struct vhci_data *data = file->private_data;
  192. struct sk_buff *skb;
  193. ssize_t ret = 0;
  194. while (count) {
  195. skb = skb_dequeue(&data->readq);
  196. if (skb) {
  197. ret = vhci_put_user(data, skb, buf, count);
  198. if (ret < 0)
  199. skb_queue_head(&data->readq, skb);
  200. else
  201. kfree_skb(skb);
  202. break;
  203. }
  204. if (file->f_flags & O_NONBLOCK) {
  205. ret = -EAGAIN;
  206. break;
  207. }
  208. ret = wait_event_interruptible(data->read_wait,
  209. !skb_queue_empty(&data->readq));
  210. if (ret < 0)
  211. break;
  212. }
  213. return ret;
  214. }
  215. static ssize_t vhci_write(struct kiocb *iocb, struct iov_iter *from)
  216. {
  217. struct file *file = iocb->ki_filp;
  218. struct vhci_data *data = file->private_data;
  219. return vhci_get_user(data, from);
  220. }
  221. static __poll_t vhci_poll(struct file *file, poll_table *wait)
  222. {
  223. struct vhci_data *data = file->private_data;
  224. poll_wait(file, &data->read_wait, wait);
  225. if (!skb_queue_empty(&data->readq))
  226. return EPOLLIN | EPOLLRDNORM;
  227. return EPOLLOUT | EPOLLWRNORM;
  228. }
  229. static void vhci_open_timeout(struct work_struct *work)
  230. {
  231. struct vhci_data *data = container_of(work, struct vhci_data,
  232. open_timeout.work);
  233. vhci_create_device(data, amp ? HCI_AMP : HCI_PRIMARY);
  234. }
  235. static int vhci_open(struct inode *inode, struct file *file)
  236. {
  237. struct vhci_data *data;
  238. data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
  239. if (!data)
  240. return -ENOMEM;
  241. skb_queue_head_init(&data->readq);
  242. init_waitqueue_head(&data->read_wait);
  243. mutex_init(&data->open_mutex);
  244. INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
  245. file->private_data = data;
  246. nonseekable_open(inode, file);
  247. schedule_delayed_work(&data->open_timeout, msecs_to_jiffies(1000));
  248. return 0;
  249. }
  250. static int vhci_release(struct inode *inode, struct file *file)
  251. {
  252. struct vhci_data *data = file->private_data;
  253. struct hci_dev *hdev;
  254. cancel_delayed_work_sync(&data->open_timeout);
  255. hdev = data->hdev;
  256. if (hdev) {
  257. hci_unregister_dev(hdev);
  258. hci_free_dev(hdev);
  259. }
  260. skb_queue_purge(&data->readq);
  261. file->private_data = NULL;
  262. kfree(data);
  263. return 0;
  264. }
  265. static const struct file_operations vhci_fops = {
  266. .owner = THIS_MODULE,
  267. .read = vhci_read,
  268. .write_iter = vhci_write,
  269. .poll = vhci_poll,
  270. .open = vhci_open,
  271. .release = vhci_release,
  272. .llseek = no_llseek,
  273. };
  274. static struct miscdevice vhci_miscdev = {
  275. .name = "vhci",
  276. .fops = &vhci_fops,
  277. .minor = VHCI_MINOR,
  278. };
  279. module_misc_device(vhci_miscdev);
  280. module_param(amp, bool, 0644);
  281. MODULE_PARM_DESC(amp, "Create AMP controller device");
  282. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  283. MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
  284. MODULE_VERSION(VERSION);
  285. MODULE_LICENSE("GPL");
  286. MODULE_ALIAS("devname:vhci");
  287. MODULE_ALIAS_MISCDEV(VHCI_MINOR);