hci_vhci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 <linux/unaligned.h>
  12. #include <linux/atomic.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/errno.h>
  18. #include <linux/sched.h>
  19. #include <linux/poll.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/debugfs.h>
  23. #include <net/bluetooth/bluetooth.h>
  24. #include <net/bluetooth/hci_core.h>
  25. #define VERSION "1.5"
  26. static bool amp;
  27. struct vhci_data {
  28. struct hci_dev *hdev;
  29. wait_queue_head_t read_wait;
  30. struct sk_buff_head readq;
  31. struct mutex open_mutex;
  32. struct delayed_work open_timeout;
  33. struct work_struct suspend_work;
  34. bool suspended;
  35. bool wakeup;
  36. __u16 msft_opcode;
  37. bool aosp_capable;
  38. atomic_t initialized;
  39. };
  40. static int vhci_open_dev(struct hci_dev *hdev)
  41. {
  42. return 0;
  43. }
  44. static int vhci_close_dev(struct hci_dev *hdev)
  45. {
  46. struct vhci_data *data = hci_get_drvdata(hdev);
  47. skb_queue_purge(&data->readq);
  48. return 0;
  49. }
  50. static int vhci_flush(struct hci_dev *hdev)
  51. {
  52. struct vhci_data *data = hci_get_drvdata(hdev);
  53. skb_queue_purge(&data->readq);
  54. return 0;
  55. }
  56. static int vhci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  57. {
  58. struct vhci_data *data = hci_get_drvdata(hdev);
  59. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  60. skb_queue_tail(&data->readq, skb);
  61. if (atomic_read(&data->initialized))
  62. wake_up_interruptible(&data->read_wait);
  63. return 0;
  64. }
  65. static int vhci_get_data_path_id(struct hci_dev *hdev, u8 *data_path_id)
  66. {
  67. *data_path_id = 0;
  68. return 0;
  69. }
  70. static int vhci_get_codec_config_data(struct hci_dev *hdev, __u8 type,
  71. struct bt_codec *codec, __u8 *vnd_len,
  72. __u8 **vnd_data)
  73. {
  74. if (type != ESCO_LINK)
  75. return -EINVAL;
  76. *vnd_len = 0;
  77. *vnd_data = NULL;
  78. return 0;
  79. }
  80. static bool vhci_wakeup(struct hci_dev *hdev)
  81. {
  82. struct vhci_data *data = hci_get_drvdata(hdev);
  83. return data->wakeup;
  84. }
  85. static ssize_t force_suspend_read(struct file *file, char __user *user_buf,
  86. size_t count, loff_t *ppos)
  87. {
  88. struct vhci_data *data = file->private_data;
  89. char buf[3];
  90. buf[0] = data->suspended ? 'Y' : 'N';
  91. buf[1] = '\n';
  92. buf[2] = '\0';
  93. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  94. }
  95. static void vhci_suspend_work(struct work_struct *work)
  96. {
  97. struct vhci_data *data = container_of(work, struct vhci_data,
  98. suspend_work);
  99. if (data->suspended)
  100. hci_suspend_dev(data->hdev);
  101. else
  102. hci_resume_dev(data->hdev);
  103. }
  104. static ssize_t force_suspend_write(struct file *file,
  105. const char __user *user_buf,
  106. size_t count, loff_t *ppos)
  107. {
  108. struct vhci_data *data = file->private_data;
  109. bool enable;
  110. int err;
  111. err = kstrtobool_from_user(user_buf, count, &enable);
  112. if (err)
  113. return err;
  114. if (data->suspended == enable)
  115. return -EALREADY;
  116. data->suspended = enable;
  117. schedule_work(&data->suspend_work);
  118. return count;
  119. }
  120. static const struct file_operations force_suspend_fops = {
  121. .open = simple_open,
  122. .read = force_suspend_read,
  123. .write = force_suspend_write,
  124. .llseek = default_llseek,
  125. };
  126. static ssize_t force_wakeup_read(struct file *file, char __user *user_buf,
  127. size_t count, loff_t *ppos)
  128. {
  129. struct vhci_data *data = file->private_data;
  130. char buf[3];
  131. buf[0] = data->wakeup ? 'Y' : 'N';
  132. buf[1] = '\n';
  133. buf[2] = '\0';
  134. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  135. }
  136. static ssize_t force_wakeup_write(struct file *file,
  137. const char __user *user_buf, size_t count,
  138. loff_t *ppos)
  139. {
  140. struct vhci_data *data = file->private_data;
  141. bool enable;
  142. int err;
  143. err = kstrtobool_from_user(user_buf, count, &enable);
  144. if (err)
  145. return err;
  146. if (data->wakeup == enable)
  147. return -EALREADY;
  148. data->wakeup = enable;
  149. return count;
  150. }
  151. static const struct file_operations force_wakeup_fops = {
  152. .open = simple_open,
  153. .read = force_wakeup_read,
  154. .write = force_wakeup_write,
  155. .llseek = default_llseek,
  156. };
  157. static int msft_opcode_set(void *data, u64 val)
  158. {
  159. struct vhci_data *vhci = data;
  160. if (val > 0xffff || hci_opcode_ogf(val) != 0x3f)
  161. return -EINVAL;
  162. if (vhci->msft_opcode)
  163. return -EALREADY;
  164. vhci->msft_opcode = val;
  165. return 0;
  166. }
  167. static int msft_opcode_get(void *data, u64 *val)
  168. {
  169. struct vhci_data *vhci = data;
  170. *val = vhci->msft_opcode;
  171. return 0;
  172. }
  173. DEFINE_DEBUGFS_ATTRIBUTE(msft_opcode_fops, msft_opcode_get, msft_opcode_set,
  174. "%llu\n");
  175. static ssize_t aosp_capable_read(struct file *file, char __user *user_buf,
  176. size_t count, loff_t *ppos)
  177. {
  178. struct vhci_data *vhci = file->private_data;
  179. char buf[3];
  180. buf[0] = vhci->aosp_capable ? 'Y' : 'N';
  181. buf[1] = '\n';
  182. buf[2] = '\0';
  183. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  184. }
  185. static ssize_t aosp_capable_write(struct file *file,
  186. const char __user *user_buf, size_t count,
  187. loff_t *ppos)
  188. {
  189. struct vhci_data *vhci = file->private_data;
  190. bool enable;
  191. int err;
  192. err = kstrtobool_from_user(user_buf, count, &enable);
  193. if (err)
  194. return err;
  195. if (!enable)
  196. return -EINVAL;
  197. if (vhci->aosp_capable)
  198. return -EALREADY;
  199. vhci->aosp_capable = enable;
  200. return count;
  201. }
  202. static const struct file_operations aosp_capable_fops = {
  203. .open = simple_open,
  204. .read = aosp_capable_read,
  205. .write = aosp_capable_write,
  206. .llseek = default_llseek,
  207. };
  208. static int vhci_setup(struct hci_dev *hdev)
  209. {
  210. struct vhci_data *vhci = hci_get_drvdata(hdev);
  211. if (vhci->msft_opcode)
  212. hci_set_msft_opcode(hdev, vhci->msft_opcode);
  213. if (vhci->aosp_capable)
  214. hci_set_aosp_capable(hdev);
  215. return 0;
  216. }
  217. static void vhci_coredump(struct hci_dev *hdev)
  218. {
  219. /* No need to do anything */
  220. }
  221. static void vhci_coredump_hdr(struct hci_dev *hdev, struct sk_buff *skb)
  222. {
  223. const char *buf;
  224. buf = "Controller Name: vhci_ctrl\n";
  225. skb_put_data(skb, buf, strlen(buf));
  226. buf = "Firmware Version: vhci_fw\n";
  227. skb_put_data(skb, buf, strlen(buf));
  228. buf = "Driver: vhci_drv\n";
  229. skb_put_data(skb, buf, strlen(buf));
  230. buf = "Vendor: vhci\n";
  231. skb_put_data(skb, buf, strlen(buf));
  232. }
  233. #define MAX_COREDUMP_LINE_LEN 40
  234. struct devcoredump_test_data {
  235. enum devcoredump_state state;
  236. unsigned int timeout;
  237. char data[MAX_COREDUMP_LINE_LEN];
  238. };
  239. static inline void force_devcd_timeout(struct hci_dev *hdev,
  240. unsigned int timeout)
  241. {
  242. #ifdef CONFIG_DEV_COREDUMP
  243. hdev->dump.timeout = secs_to_jiffies(timeout);
  244. #endif
  245. }
  246. static ssize_t force_devcd_write(struct file *file, const char __user *user_buf,
  247. size_t count, loff_t *ppos)
  248. {
  249. struct vhci_data *data = file->private_data;
  250. struct hci_dev *hdev = data->hdev;
  251. struct sk_buff *skb = NULL;
  252. struct devcoredump_test_data dump_data;
  253. size_t data_size;
  254. int ret;
  255. if (count < offsetof(struct devcoredump_test_data, data) ||
  256. count > sizeof(dump_data))
  257. return -EINVAL;
  258. if (copy_from_user(&dump_data, user_buf, count))
  259. return -EFAULT;
  260. data_size = count - offsetof(struct devcoredump_test_data, data);
  261. skb = alloc_skb(data_size, GFP_ATOMIC);
  262. if (!skb)
  263. return -ENOMEM;
  264. skb_put_data(skb, &dump_data.data, data_size);
  265. hci_devcd_register(hdev, vhci_coredump, vhci_coredump_hdr, NULL);
  266. /* Force the devcoredump timeout */
  267. if (dump_data.timeout)
  268. force_devcd_timeout(hdev, dump_data.timeout);
  269. ret = hci_devcd_init(hdev, skb->len);
  270. if (ret) {
  271. BT_ERR("Failed to generate devcoredump");
  272. kfree_skb(skb);
  273. return ret;
  274. }
  275. hci_devcd_append(hdev, skb);
  276. switch (dump_data.state) {
  277. case HCI_DEVCOREDUMP_DONE:
  278. hci_devcd_complete(hdev);
  279. break;
  280. case HCI_DEVCOREDUMP_ABORT:
  281. hci_devcd_abort(hdev);
  282. break;
  283. case HCI_DEVCOREDUMP_TIMEOUT:
  284. /* Do nothing */
  285. break;
  286. default:
  287. return -EINVAL;
  288. }
  289. return count;
  290. }
  291. static const struct file_operations force_devcoredump_fops = {
  292. .open = simple_open,
  293. .write = force_devcd_write,
  294. };
  295. static void vhci_debugfs_init(struct vhci_data *data)
  296. {
  297. struct hci_dev *hdev = data->hdev;
  298. debugfs_create_file("force_suspend", 0644, hdev->debugfs, data,
  299. &force_suspend_fops);
  300. debugfs_create_file("force_wakeup", 0644, hdev->debugfs, data,
  301. &force_wakeup_fops);
  302. if (IS_ENABLED(CONFIG_BT_MSFTEXT))
  303. debugfs_create_file("msft_opcode", 0644, hdev->debugfs, data,
  304. &msft_opcode_fops);
  305. if (IS_ENABLED(CONFIG_BT_AOSPEXT))
  306. debugfs_create_file("aosp_capable", 0644, hdev->debugfs, data,
  307. &aosp_capable_fops);
  308. debugfs_create_file("force_devcoredump", 0644, hdev->debugfs, data,
  309. &force_devcoredump_fops);
  310. }
  311. static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
  312. {
  313. struct hci_dev *hdev;
  314. struct sk_buff *skb;
  315. if (data->hdev)
  316. return -EBADFD;
  317. /* bits 2-5 are reserved (must be zero) */
  318. if (opcode & 0x3c)
  319. return -EINVAL;
  320. skb = bt_skb_alloc(4, GFP_KERNEL);
  321. if (!skb)
  322. return -ENOMEM;
  323. hdev = hci_alloc_dev();
  324. if (!hdev) {
  325. kfree_skb(skb);
  326. return -ENOMEM;
  327. }
  328. data->hdev = hdev;
  329. hdev->bus = HCI_VIRTUAL;
  330. hci_set_drvdata(hdev, data);
  331. hdev->open = vhci_open_dev;
  332. hdev->close = vhci_close_dev;
  333. hdev->flush = vhci_flush;
  334. hdev->send = vhci_send_frame;
  335. hdev->get_data_path_id = vhci_get_data_path_id;
  336. hdev->get_codec_config_data = vhci_get_codec_config_data;
  337. hdev->wakeup = vhci_wakeup;
  338. hdev->setup = vhci_setup;
  339. hci_set_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_SETUP);
  340. hci_set_quirk(hdev, HCI_QUIRK_SYNC_FLOWCTL_SUPPORTED);
  341. /* bit 6 is for external configuration */
  342. if (opcode & 0x40)
  343. hci_set_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG);
  344. /* bit 7 is for raw device */
  345. if (opcode & 0x80)
  346. hci_set_quirk(hdev, HCI_QUIRK_RAW_DEVICE);
  347. if (hci_register_dev(hdev) < 0) {
  348. BT_ERR("Can't register HCI device");
  349. hci_free_dev(hdev);
  350. data->hdev = NULL;
  351. kfree_skb(skb);
  352. return -EBUSY;
  353. }
  354. if (!IS_ERR_OR_NULL(hdev->debugfs))
  355. vhci_debugfs_init(data);
  356. hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
  357. skb_put_u8(skb, 0xff);
  358. skb_put_u8(skb, opcode);
  359. put_unaligned_le16(hdev->id, skb_put(skb, 2));
  360. skb_queue_head(&data->readq, skb);
  361. atomic_inc(&data->initialized);
  362. wake_up_interruptible(&data->read_wait);
  363. return 0;
  364. }
  365. static int vhci_create_device(struct vhci_data *data, __u8 opcode)
  366. {
  367. int err;
  368. mutex_lock(&data->open_mutex);
  369. err = __vhci_create_device(data, opcode);
  370. mutex_unlock(&data->open_mutex);
  371. return err;
  372. }
  373. static inline ssize_t vhci_get_user(struct vhci_data *data,
  374. struct iov_iter *from)
  375. {
  376. size_t len = iov_iter_count(from);
  377. struct sk_buff *skb;
  378. __u8 pkt_type, opcode;
  379. int ret;
  380. if (len < 2 || len > HCI_MAX_FRAME_SIZE)
  381. return -EINVAL;
  382. skb = bt_skb_alloc(len, GFP_KERNEL);
  383. if (!skb)
  384. return -ENOMEM;
  385. if (!copy_from_iter_full(skb_put(skb, len), len, from)) {
  386. kfree_skb(skb);
  387. return -EFAULT;
  388. }
  389. pkt_type = *((__u8 *) skb->data);
  390. skb_pull(skb, 1);
  391. switch (pkt_type) {
  392. case HCI_EVENT_PKT:
  393. case HCI_ACLDATA_PKT:
  394. case HCI_SCODATA_PKT:
  395. case HCI_ISODATA_PKT:
  396. if (!data->hdev) {
  397. kfree_skb(skb);
  398. return -ENODEV;
  399. }
  400. hci_skb_pkt_type(skb) = pkt_type;
  401. ret = hci_recv_frame(data->hdev, skb);
  402. break;
  403. case HCI_VENDOR_PKT:
  404. cancel_delayed_work_sync(&data->open_timeout);
  405. opcode = *((__u8 *) skb->data);
  406. skb_pull(skb, 1);
  407. if (skb->len > 0) {
  408. kfree_skb(skb);
  409. return -EINVAL;
  410. }
  411. kfree_skb(skb);
  412. ret = vhci_create_device(data, opcode);
  413. break;
  414. default:
  415. kfree_skb(skb);
  416. return -EINVAL;
  417. }
  418. return (ret < 0) ? ret : len;
  419. }
  420. static inline ssize_t vhci_put_user(struct vhci_data *data,
  421. struct sk_buff *skb,
  422. char __user *buf, int count)
  423. {
  424. char __user *ptr = buf;
  425. int len;
  426. len = min_t(unsigned int, skb->len, count);
  427. if (copy_to_user(ptr, skb->data, len))
  428. return -EFAULT;
  429. if (!data->hdev)
  430. return len;
  431. data->hdev->stat.byte_tx += len;
  432. switch (hci_skb_pkt_type(skb)) {
  433. case HCI_COMMAND_PKT:
  434. data->hdev->stat.cmd_tx++;
  435. break;
  436. case HCI_ACLDATA_PKT:
  437. data->hdev->stat.acl_tx++;
  438. break;
  439. case HCI_SCODATA_PKT:
  440. data->hdev->stat.sco_tx++;
  441. break;
  442. }
  443. return len;
  444. }
  445. static ssize_t vhci_read(struct file *file,
  446. char __user *buf, size_t count, loff_t *pos)
  447. {
  448. struct vhci_data *data = file->private_data;
  449. struct sk_buff *skb;
  450. ssize_t ret = 0;
  451. while (count) {
  452. skb = skb_dequeue(&data->readq);
  453. if (skb) {
  454. ret = vhci_put_user(data, skb, buf, count);
  455. if (ret < 0)
  456. skb_queue_head(&data->readq, skb);
  457. else
  458. kfree_skb(skb);
  459. break;
  460. }
  461. if (file->f_flags & O_NONBLOCK) {
  462. ret = -EAGAIN;
  463. break;
  464. }
  465. ret = wait_event_interruptible(data->read_wait,
  466. !skb_queue_empty(&data->readq));
  467. if (ret < 0)
  468. break;
  469. }
  470. return ret;
  471. }
  472. static ssize_t vhci_write(struct kiocb *iocb, struct iov_iter *from)
  473. {
  474. struct file *file = iocb->ki_filp;
  475. struct vhci_data *data = file->private_data;
  476. return vhci_get_user(data, from);
  477. }
  478. static __poll_t vhci_poll(struct file *file, poll_table *wait)
  479. {
  480. struct vhci_data *data = file->private_data;
  481. poll_wait(file, &data->read_wait, wait);
  482. if (!skb_queue_empty(&data->readq))
  483. return EPOLLIN | EPOLLRDNORM;
  484. return EPOLLOUT | EPOLLWRNORM;
  485. }
  486. static void vhci_open_timeout(struct work_struct *work)
  487. {
  488. struct vhci_data *data = container_of(work, struct vhci_data,
  489. open_timeout.work);
  490. vhci_create_device(data, 0x00);
  491. }
  492. static int vhci_open(struct inode *inode, struct file *file)
  493. {
  494. struct vhci_data *data;
  495. data = kzalloc_obj(*data);
  496. if (!data)
  497. return -ENOMEM;
  498. skb_queue_head_init(&data->readq);
  499. init_waitqueue_head(&data->read_wait);
  500. mutex_init(&data->open_mutex);
  501. INIT_DELAYED_WORK(&data->open_timeout, vhci_open_timeout);
  502. INIT_WORK(&data->suspend_work, vhci_suspend_work);
  503. file->private_data = data;
  504. nonseekable_open(inode, file);
  505. schedule_delayed_work(&data->open_timeout, secs_to_jiffies(1));
  506. return 0;
  507. }
  508. static void vhci_debugfs_remove(struct hci_dev *hdev)
  509. {
  510. debugfs_lookup_and_remove("force_suspend", hdev->debugfs);
  511. debugfs_lookup_and_remove("force_wakeup", hdev->debugfs);
  512. if (IS_ENABLED(CONFIG_BT_MSFTEXT))
  513. debugfs_lookup_and_remove("msft_opcode", hdev->debugfs);
  514. if (IS_ENABLED(CONFIG_BT_AOSPEXT))
  515. debugfs_lookup_and_remove("aosp_capable", hdev->debugfs);
  516. debugfs_lookup_and_remove("force_devcoredump", hdev->debugfs);
  517. }
  518. static int vhci_release(struct inode *inode, struct file *file)
  519. {
  520. struct vhci_data *data = file->private_data;
  521. struct hci_dev *hdev;
  522. cancel_delayed_work_sync(&data->open_timeout);
  523. flush_work(&data->suspend_work);
  524. hdev = data->hdev;
  525. if (hdev) {
  526. if (!IS_ERR_OR_NULL(hdev->debugfs))
  527. vhci_debugfs_remove(hdev);
  528. hci_unregister_dev(hdev);
  529. hci_free_dev(hdev);
  530. }
  531. skb_queue_purge(&data->readq);
  532. file->private_data = NULL;
  533. kfree(data);
  534. return 0;
  535. }
  536. static const struct file_operations vhci_fops = {
  537. .owner = THIS_MODULE,
  538. .read = vhci_read,
  539. .write_iter = vhci_write,
  540. .poll = vhci_poll,
  541. .open = vhci_open,
  542. .release = vhci_release,
  543. };
  544. static struct miscdevice vhci_miscdev = {
  545. .name = "vhci",
  546. .fops = &vhci_fops,
  547. .minor = VHCI_MINOR,
  548. };
  549. module_misc_device(vhci_miscdev);
  550. module_param(amp, bool, 0644);
  551. MODULE_PARM_DESC(amp, "Create AMP controller device");
  552. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  553. MODULE_DESCRIPTION("Bluetooth virtual HCI driver ver " VERSION);
  554. MODULE_VERSION(VERSION);
  555. MODULE_LICENSE("GPL");
  556. MODULE_ALIAS("devname:vhci");
  557. MODULE_ALIAS_MISCDEV(VHCI_MINOR);