hci_vhci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 <[email protected]>
  8. * Copyright (C) 2004-2006 Marcel Holtmann <[email protected]>
  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 <linux/debugfs.h>
  22. #include <net/bluetooth/bluetooth.h>
  23. #include <net/bluetooth/hci_core.h>
  24. #define VERSION "1.5"
  25. static bool amp;
  26. struct vhci_data {
  27. struct hci_dev *hdev;
  28. wait_queue_head_t read_wait;
  29. struct sk_buff_head readq;
  30. struct mutex open_mutex;
  31. struct delayed_work open_timeout;
  32. struct work_struct suspend_work;
  33. bool suspended;
  34. bool wakeup;
  35. __u16 msft_opcode;
  36. bool aosp_capable;
  37. };
  38. static int vhci_open_dev(struct hci_dev *hdev)
  39. {
  40. return 0;
  41. }
  42. static int vhci_close_dev(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_flush(struct hci_dev *hdev)
  49. {
  50. struct vhci_data *data = hci_get_drvdata(hdev);
  51. skb_queue_purge(&data->readq);
  52. return 0;
  53. }
  54. static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  55. {
  56. struct vhci_data *data = hci_get_drvdata(hdev);
  57. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  58. mutex_lock(&data->open_mutex);
  59. skb_queue_tail(&data->readq, skb);
  60. mutex_unlock(&data->open_mutex);
  61. wake_up_interruptible(&data->read_wait);
  62. return 0;
  63. }
  64. static int vhci_get_data_path_id(struct hci_dev *hdev, u8 *data_path_id)
  65. {
  66. *data_path_id = 0;
  67. return 0;
  68. }
  69. static int vhci_get_codec_config_data(struct hci_dev *hdev, __u8 type,
  70. struct bt_codec *codec, __u8 *vnd_len,
  71. __u8 **vnd_data)
  72. {
  73. if (type != ESCO_LINK)
  74. return -EINVAL;
  75. *vnd_len = 0;
  76. *vnd_data = NULL;
  77. return 0;
  78. }
  79. static bool vhci_wakeup(struct hci_dev *hdev)
  80. {
  81. struct vhci_data *data = hci_get_drvdata(hdev);
  82. return data->wakeup;
  83. }
  84. static ssize_t force_suspend_read(struct file *file, char __user *user_buf,
  85. size_t count, loff_t *ppos)
  86. {
  87. struct vhci_data *data = file->private_data;
  88. char buf[3];
  89. buf[0] = data->suspended ? 'Y' : 'N';
  90. buf[1] = '\n';
  91. buf[2] = '\0';
  92. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  93. }
  94. static void vhci_suspend_work(struct work_struct *work)
  95. {
  96. struct vhci_data *data = container_of(work, struct vhci_data,
  97. suspend_work);
  98. if (data->suspended)
  99. hci_suspend_dev(data->hdev);
  100. else
  101. hci_resume_dev(data->hdev);
  102. }
  103. static ssize_t force_suspend_write(struct file *file,
  104. const char __user *user_buf,
  105. size_t count, loff_t *ppos)
  106. {
  107. struct vhci_data *data = file->private_data;
  108. bool enable;
  109. int err;
  110. err = kstrtobool_from_user(user_buf, count, &enable);
  111. if (err)
  112. return err;
  113. if (data->suspended == enable)
  114. return -EALREADY;
  115. data->suspended = enable;
  116. schedule_work(&data->suspend_work);
  117. return count;
  118. }
  119. static const struct file_operations force_suspend_fops = {
  120. .open = simple_open,
  121. .read = force_suspend_read,
  122. .write = force_suspend_write,
  123. .llseek = default_llseek,
  124. };
  125. static ssize_t force_wakeup_read(struct file *file, char __user *user_buf,
  126. size_t count, loff_t *ppos)
  127. {
  128. struct vhci_data *data = file->private_data;
  129. char buf[3];
  130. buf[0] = data->wakeup ? 'Y' : 'N';
  131. buf[1] = '\n';
  132. buf[2] = '\0';
  133. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  134. }
  135. static ssize_t force_wakeup_write(struct file *file,
  136. const char __user *user_buf, size_t count,
  137. loff_t *ppos)
  138. {
  139. struct vhci_data *data = file->private_data;
  140. bool enable;
  141. int err;
  142. err = kstrtobool_from_user(user_buf, count, &enable);
  143. if (err)
  144. return err;
  145. if (data->wakeup == enable)
  146. return -EALREADY;
  147. data->wakeup = enable;
  148. return count;
  149. }
  150. static const struct file_operations force_wakeup_fops = {
  151. .open = simple_open,
  152. .read = force_wakeup_read,
  153. .write = force_wakeup_write,
  154. .llseek = default_llseek,
  155. };
  156. static int msft_opcode_set(void *data, u64 val)
  157. {
  158. struct vhci_data *vhci = data;
  159. if (val > 0xffff || hci_opcode_ogf(val) != 0x3f)
  160. return -EINVAL;
  161. if (vhci->msft_opcode)
  162. return -EALREADY;
  163. vhci->msft_opcode = val;
  164. return 0;
  165. }
  166. static int msft_opcode_get(void *data, u64 *val)
  167. {
  168. struct vhci_data *vhci = data;
  169. *val = vhci->msft_opcode;
  170. return 0;
  171. }
  172. DEFINE_DEBUGFS_ATTRIBUTE(msft_opcode_fops, msft_opcode_get, msft_opcode_set,
  173. "%llu\n");
  174. static ssize_t aosp_capable_read(struct file *file, char __user *user_buf,
  175. size_t count, loff_t *ppos)
  176. {
  177. struct vhci_data *vhci = file->private_data;
  178. char buf[3];
  179. buf[0] = vhci->aosp_capable ? 'Y' : 'N';
  180. buf[1] = '\n';
  181. buf[2] = '\0';
  182. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  183. }
  184. static ssize_t aosp_capable_write(struct file *file,
  185. const char __user *user_buf, size_t count,
  186. loff_t *ppos)
  187. {
  188. struct vhci_data *vhci = file->private_data;
  189. bool enable;
  190. int err;
  191. err = kstrtobool_from_user(user_buf, count, &enable);
  192. if (err)
  193. return err;
  194. if (!enable)
  195. return -EINVAL;
  196. if (vhci->aosp_capable)
  197. return -EALREADY;
  198. vhci->aosp_capable = enable;
  199. return count;
  200. }
  201. static const struct file_operations aosp_capable_fops = {
  202. .open = simple_open,
  203. .read = aosp_capable_read,
  204. .write = aosp_capable_write,
  205. .llseek = default_llseek,
  206. };
  207. static int vhci_setup(struct hci_dev *hdev)
  208. {
  209. struct vhci_data *vhci = hci_get_drvdata(hdev);
  210. if (vhci->msft_opcode)
  211. hci_set_msft_opcode(hdev, vhci->msft_opcode);
  212. if (vhci->aosp_capable)
  213. hci_set_aosp_capable(hdev);
  214. return 0;
  215. }
  216. static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
  217. {
  218. struct hci_dev *hdev;
  219. struct sk_buff *skb;
  220. __u8 dev_type;
  221. if (data->hdev)
  222. return -EBADFD;
  223. /* bits 0-1 are dev_type (Primary or AMP) */
  224. dev_type = opcode & 0x03;
  225. if (dev_type != HCI_PRIMARY && dev_type != HCI_AMP)
  226. return -EINVAL;
  227. /* bits 2-5 are reserved (must be zero) */
  228. if (opcode & 0x3c)
  229. return -EINVAL;
  230. skb = bt_skb_alloc(4, GFP_KERNEL);
  231. if (!skb)
  232. return -ENOMEM;
  233. hdev = hci_alloc_dev();
  234. if (!hdev) {
  235. kfree_skb(skb);
  236. return -ENOMEM;
  237. }
  238. data->hdev = hdev;
  239. hdev->bus = HCI_VIRTUAL;
  240. hdev->dev_type = dev_type;
  241. hci_set_drvdata(hdev, data);
  242. hdev->open = vhci_open_dev;
  243. hdev->close = vhci_close_dev;
  244. hdev->flush = vhci_flush;
  245. hdev->send = vhci_send_frame;
  246. hdev->get_data_path_id = vhci_get_data_path_id;
  247. hdev->get_codec_config_data = vhci_get_codec_config_data;
  248. hdev->wakeup = vhci_wakeup;
  249. hdev->setup = vhci_setup;
  250. set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
  251. /* bit 6 is for external configuration */
  252. if (opcode & 0x40)
  253. set_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks);
  254. /* bit 7 is for raw device */
  255. if (opcode & 0x80)
  256. set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
  257. set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
  258. if (hci_register_dev(hdev) < 0) {
  259. BT_ERR("Can't register HCI device");
  260. hci_free_dev(hdev);
  261. data->hdev = NULL;
  262. kfree_skb(skb);
  263. return -EBUSY;
  264. }
  265. debugfs_create_file("force_suspend", 0644, hdev->debugfs, data,
  266. &force_suspend_fops);
  267. debugfs_create_file("force_wakeup", 0644, hdev->debugfs, data,
  268. &force_wakeup_fops);
  269. if (IS_ENABLED(CONFIG_BT_MSFTEXT))
  270. debugfs_create_file("msft_opcode", 0644, hdev->debugfs, data,
  271. &msft_opcode_fops);
  272. if (IS_ENABLED(CONFIG_BT_AOSPEXT))
  273. debugfs_create_file("aosp_capable", 0644, hdev->debugfs, data,
  274. &aosp_capable_fops);
  275. hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
  276. skb_put_u8(skb, 0xff);
  277. skb_put_u8(skb, opcode);
  278. put_unaligned_le16(hdev->id, skb_put(skb, 2));
  279. skb_queue_tail(&data->readq, skb);
  280. wake_up_interruptible(&data->read_wait);
  281. return 0;
  282. }
  283. static int vhci_create_device(struct vhci_data *data, __u8 opcode)
  284. {
  285. int err;
  286. mutex_lock(&data->open_mutex);
  287. err = __vhci_create_device(data, opcode);
  288. mutex_unlock(&data->open_mutex);
  289. return err;
  290. }
  291. static inline ssize_t vhci_get_user(struct vhci_data *data,
  292. struct iov_iter *from)
  293. {
  294. size_t len = iov_iter_count(from);
  295. struct sk_buff *skb;
  296. __u8 pkt_type, opcode;
  297. int ret;
  298. if (len < 2 || len > HCI_MAX_FRAME_SIZE)
  299. return -EINVAL;
  300. skb = bt_skb_alloc(len, GFP_KERNEL);
  301. if (!skb)
  302. return -ENOMEM;
  303. if (!copy_from_iter_full(skb_put(skb, len), len, from)) {
  304. kfree_skb(skb);
  305. return -EFAULT;
  306. }
  307. pkt_type = *((__u8 *) skb->data);
  308. skb_pull(skb, 1);
  309. switch (pkt_type) {
  310. case HCI_EVENT_PKT:
  311. case HCI_ACLDATA_PKT:
  312. case HCI_SCODATA_PKT:
  313. case HCI_ISODATA_PKT:
  314. if (!data->hdev) {
  315. kfree_skb(skb);
  316. return -ENODEV;
  317. }
  318. hci_skb_pkt_type(skb) = pkt_type;
  319. ret = hci_recv_frame(data->hdev, skb);
  320. break;
  321. case HCI_VENDOR_PKT:
  322. cancel_delayed_work_sync(&data->open_timeout);
  323. opcode = *((__u8 *) skb->data);
  324. skb_pull(skb, 1);
  325. if (skb->len > 0) {
  326. kfree_skb(skb);
  327. return -EINVAL;
  328. }
  329. kfree_skb(skb);
  330. ret = vhci_create_device(data, opcode);
  331. break;
  332. default:
  333. kfree_skb(skb);
  334. return -EINVAL;
  335. }
  336. return (ret < 0) ? ret : len;
  337. }
  338. static inline ssize_t vhci_put_user(struct vhci_data *data,
  339. struct sk_buff *skb,
  340. char __user *buf, int count)
  341. {
  342. char __user *ptr = buf;
  343. int len;
  344. len = min_t(unsigned int, skb->len, count);
  345. if (copy_to_user(ptr, skb->data, len))
  346. return -EFAULT;
  347. if (!data->hdev)
  348. return len;
  349. data->hdev->stat.byte_tx += len;
  350. switch (hci_skb_pkt_type(skb)) {
  351. case HCI_COMMAND_PKT:
  352. data->hdev->stat.cmd_tx++;
  353. break;
  354. case HCI_ACLDATA_PKT:
  355. data->hdev->stat.acl_tx++;
  356. break;
  357. case HCI_SCODATA_PKT:
  358. data->hdev->stat.sco_tx++;
  359. break;
  360. }
  361. return len;
  362. }
  363. static ssize_t vhci_read(struct file *file,
  364. char __user *buf, size_t count, loff_t *pos)
  365. {
  366. struct vhci_data *data = file->private_data;
  367. struct sk_buff *skb;
  368. ssize_t ret = 0;
  369. while (count) {
  370. skb = skb_dequeue(&data->readq);
  371. if (skb) {
  372. ret = vhci_put_user(data, skb, buf, count);
  373. if (ret < 0)
  374. skb_queue_head(&data->readq, skb);
  375. else
  376. kfree_skb(skb);
  377. break;
  378. }
  379. if (file->f_flags & O_NONBLOCK) {
  380. ret = -EAGAIN;
  381. break;
  382. }
  383. ret = wait_event_interruptible(data->read_wait,
  384. !skb_queue_empty(&data->readq));
  385. if (ret < 0)
  386. break;
  387. }
  388. return ret;
  389. }
  390. static ssize_t vhci_write(struct kiocb *iocb, struct iov_iter *from)
  391. {
  392. struct file *file = iocb->ki_filp;
  393. struct vhci_data *data = file->private_data;
  394. return vhci_get_user(data, from);
  395. }
  396. static __poll_t vhci_poll(struct file *file, poll_table *wait)
  397. {
  398. struct vhci_data *data = file->private_data;
  399. poll_wait(file, &data->read_wait, wait);
  400. if (!skb_queue_empty(&data->readq))
  401. return EPOLLIN | EPOLLRDNORM;
  402. return EPOLLOUT | EPOLLWRNORM;
  403. }
  404. static void vhci_open_timeout(struct work_struct *work)
  405. {
  406. struct vhci_data *data = container_of(work, struct vhci_data,
  407. open_timeout.work);
  408. vhci_create_device(data, amp ? HCI_AMP : HCI_PRIMARY);
  409. }
  410. static int vhci_open(struct inode *inode, struct file *file)
  411. {
  412. struct vhci_data *data;
  413. data = kzalloc(sizeof(struct vhci_data), GFP_KERNEL);
  414. if (!data)
  415. return -ENOMEM;
  416. skb_queue_head_init(&data->readq);
  417. init_waitqueue_head(&data->read_wait);
  418. mutex_init(&data->open_mutex);
  419. INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
  420. INIT_WORK(&data->suspend_work, vhci_suspend_work);
  421. file->private_data = data;
  422. nonseekable_open(inode, file);
  423. schedule_delayed_work(&data->open_timeout, msecs_to_jiffies(1000));
  424. return 0;
  425. }
  426. static int vhci_release(struct inode *inode, struct file *file)
  427. {
  428. struct vhci_data *data = file->private_data;
  429. struct hci_dev *hdev;
  430. cancel_delayed_work_sync(&data->open_timeout);
  431. flush_work(&data->suspend_work);
  432. hdev = data->hdev;
  433. if (hdev) {
  434. hci_unregister_dev(hdev);
  435. hci_free_dev(hdev);
  436. }
  437. skb_queue_purge(&data->readq);
  438. file->private_data = NULL;
  439. kfree(data);
  440. return 0;
  441. }
  442. static const struct file_operations vhci_fops = {
  443. .owner = THIS_MODULE,
  444. .read = vhci_read,
  445. .write_iter = vhci_write,
  446. .poll = vhci_poll,
  447. .open = vhci_open,
  448. .release = vhci_release,
  449. .llseek = no_llseek,
  450. };
  451. static struct miscdevice vhci_miscdev = {
  452. .name = "vhci",
  453. .fops = &vhci_fops,
  454. .minor = VHCI_MINOR,
  455. };
  456. module_misc_device(vhci_miscdev);
  457. module_param(amp, bool, 0644);
  458. MODULE_PARM_DESC(amp, "Create AMP controller device");
  459. MODULE_AUTHOR("Marcel Holtmann <[email protected]>");
  460. MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
  461. MODULE_VERSION(VERSION);
  462. MODULE_LICENSE("GPL");
  463. MODULE_ALIAS("devname:vhci");
  464. MODULE_ALIAS_MISCDEV(VHCI_MINOR);