virtio_bt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/module.h>
  3. #include <linux/virtio.h>
  4. #include <linux/virtio_config.h>
  5. #include <linux/skbuff.h>
  6. #include <uapi/linux/virtio_ids.h>
  7. #include <uapi/linux/virtio_bt.h>
  8. #include <net/bluetooth/bluetooth.h>
  9. #include <net/bluetooth/hci_core.h>
  10. #define VERSION "0.1"
  11. enum {
  12. VIRTBT_VQ_TX,
  13. VIRTBT_VQ_RX,
  14. VIRTBT_NUM_VQS,
  15. };
  16. struct virtio_bluetooth {
  17. struct virtio_device *vdev;
  18. struct virtqueue *vqs[VIRTBT_NUM_VQS];
  19. struct work_struct rx;
  20. struct hci_dev *hdev;
  21. };
  22. static int virtbt_add_inbuf(struct virtio_bluetooth *vbt)
  23. {
  24. struct virtqueue *vq = vbt->vqs[VIRTBT_VQ_RX];
  25. struct scatterlist sg[1];
  26. struct sk_buff *skb;
  27. int err;
  28. skb = alloc_skb(1000, GFP_KERNEL);
  29. if (!skb)
  30. return -ENOMEM;
  31. sg_init_one(sg, skb->data, 1000);
  32. err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
  33. if (err < 0) {
  34. kfree_skb(skb);
  35. return err;
  36. }
  37. return 0;
  38. }
  39. static int virtbt_open(struct hci_dev *hdev)
  40. {
  41. return 0;
  42. }
  43. static int virtbt_open_vdev(struct virtio_bluetooth *vbt)
  44. {
  45. if (virtbt_add_inbuf(vbt) < 0)
  46. return -EIO;
  47. virtqueue_kick(vbt->vqs[VIRTBT_VQ_RX]);
  48. return 0;
  49. }
  50. static int virtbt_close(struct hci_dev *hdev)
  51. {
  52. return 0;
  53. }
  54. static int virtbt_close_vdev(struct virtio_bluetooth *vbt)
  55. {
  56. int i;
  57. cancel_work_sync(&vbt->rx);
  58. for (i = 0; i < ARRAY_SIZE(vbt->vqs); i++) {
  59. struct virtqueue *vq = vbt->vqs[i];
  60. struct sk_buff *skb;
  61. while ((skb = virtqueue_detach_unused_buf(vq)))
  62. kfree_skb(skb);
  63. cond_resched();
  64. }
  65. return 0;
  66. }
  67. static int virtbt_flush(struct hci_dev *hdev)
  68. {
  69. return 0;
  70. }
  71. static int virtbt_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
  72. {
  73. struct virtio_bluetooth *vbt = hci_get_drvdata(hdev);
  74. struct scatterlist sg[1];
  75. int err;
  76. memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
  77. sg_init_one(sg, skb->data, skb->len);
  78. err = virtqueue_add_outbuf(vbt->vqs[VIRTBT_VQ_TX], sg, 1, skb,
  79. GFP_KERNEL);
  80. if (err) {
  81. kfree_skb(skb);
  82. return err;
  83. }
  84. virtqueue_kick(vbt->vqs[VIRTBT_VQ_TX]);
  85. return 0;
  86. }
  87. static int virtbt_setup_zephyr(struct hci_dev *hdev)
  88. {
  89. struct sk_buff *skb;
  90. /* Read Build Information */
  91. skb = __hci_cmd_sync(hdev, 0xfc08, 0, NULL, HCI_INIT_TIMEOUT);
  92. if (IS_ERR(skb))
  93. return PTR_ERR(skb);
  94. bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
  95. hci_set_fw_info(hdev, "%s", skb->data + 1);
  96. kfree_skb(skb);
  97. return 0;
  98. }
  99. static int virtbt_set_bdaddr_zephyr(struct hci_dev *hdev,
  100. const bdaddr_t *bdaddr)
  101. {
  102. struct sk_buff *skb;
  103. /* Write BD_ADDR */
  104. skb = __hci_cmd_sync(hdev, 0xfc06, 6, bdaddr, HCI_INIT_TIMEOUT);
  105. if (IS_ERR(skb))
  106. return PTR_ERR(skb);
  107. kfree_skb(skb);
  108. return 0;
  109. }
  110. static int virtbt_setup_intel(struct hci_dev *hdev)
  111. {
  112. struct sk_buff *skb;
  113. /* Intel Read Version */
  114. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
  115. if (IS_ERR(skb))
  116. return PTR_ERR(skb);
  117. kfree_skb(skb);
  118. return 0;
  119. }
  120. static int virtbt_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  121. {
  122. struct sk_buff *skb;
  123. /* Intel Write BD Address */
  124. skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
  125. if (IS_ERR(skb))
  126. return PTR_ERR(skb);
  127. kfree_skb(skb);
  128. return 0;
  129. }
  130. static int virtbt_setup_realtek(struct hci_dev *hdev)
  131. {
  132. struct sk_buff *skb;
  133. /* Read ROM Version */
  134. skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
  135. if (IS_ERR(skb))
  136. return PTR_ERR(skb);
  137. bt_dev_info(hdev, "ROM version %u", *((__u8 *) (skb->data + 1)));
  138. kfree_skb(skb);
  139. return 0;
  140. }
  141. static int virtbt_shutdown_generic(struct hci_dev *hdev)
  142. {
  143. struct sk_buff *skb;
  144. /* Reset */
  145. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  146. if (IS_ERR(skb))
  147. return PTR_ERR(skb);
  148. kfree_skb(skb);
  149. return 0;
  150. }
  151. static void virtbt_rx_handle(struct virtio_bluetooth *vbt, struct sk_buff *skb)
  152. {
  153. __u8 pkt_type;
  154. pkt_type = *((__u8 *) skb->data);
  155. skb_pull(skb, 1);
  156. switch (pkt_type) {
  157. case HCI_EVENT_PKT:
  158. case HCI_ACLDATA_PKT:
  159. case HCI_SCODATA_PKT:
  160. case HCI_ISODATA_PKT:
  161. hci_skb_pkt_type(skb) = pkt_type;
  162. hci_recv_frame(vbt->hdev, skb);
  163. break;
  164. default:
  165. kfree_skb(skb);
  166. break;
  167. }
  168. }
  169. static void virtbt_rx_work(struct work_struct *work)
  170. {
  171. struct virtio_bluetooth *vbt = container_of(work,
  172. struct virtio_bluetooth, rx);
  173. struct sk_buff *skb;
  174. unsigned int len;
  175. skb = virtqueue_get_buf(vbt->vqs[VIRTBT_VQ_RX], &len);
  176. if (!skb)
  177. return;
  178. skb_put(skb, len);
  179. virtbt_rx_handle(vbt, skb);
  180. if (virtbt_add_inbuf(vbt) < 0)
  181. return;
  182. virtqueue_kick(vbt->vqs[VIRTBT_VQ_RX]);
  183. }
  184. static void virtbt_tx_done(struct virtqueue *vq)
  185. {
  186. struct sk_buff *skb;
  187. unsigned int len;
  188. while ((skb = virtqueue_get_buf(vq, &len)))
  189. kfree_skb(skb);
  190. }
  191. static void virtbt_rx_done(struct virtqueue *vq)
  192. {
  193. struct virtio_bluetooth *vbt = vq->vdev->priv;
  194. schedule_work(&vbt->rx);
  195. }
  196. static int virtbt_probe(struct virtio_device *vdev)
  197. {
  198. struct virtqueue_info vqs_info[VIRTBT_NUM_VQS] = {
  199. [VIRTBT_VQ_TX] = { "tx", virtbt_tx_done },
  200. [VIRTBT_VQ_RX] = { "rx", virtbt_rx_done },
  201. };
  202. struct virtio_bluetooth *vbt;
  203. struct hci_dev *hdev;
  204. int err;
  205. __u8 type;
  206. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
  207. return -ENODEV;
  208. type = virtio_cread8(vdev, offsetof(struct virtio_bt_config, type));
  209. switch (type) {
  210. case VIRTIO_BT_CONFIG_TYPE_PRIMARY:
  211. break;
  212. default:
  213. return -EINVAL;
  214. }
  215. vbt = kzalloc(sizeof(*vbt), GFP_KERNEL);
  216. if (!vbt)
  217. return -ENOMEM;
  218. vdev->priv = vbt;
  219. vbt->vdev = vdev;
  220. INIT_WORK(&vbt->rx, virtbt_rx_work);
  221. err = virtio_find_vqs(vdev, VIRTBT_NUM_VQS, vbt->vqs, vqs_info, NULL);
  222. if (err)
  223. return err;
  224. hdev = hci_alloc_dev();
  225. if (!hdev) {
  226. err = -ENOMEM;
  227. goto failed;
  228. }
  229. vbt->hdev = hdev;
  230. hdev->bus = HCI_VIRTIO;
  231. hci_set_drvdata(hdev, vbt);
  232. hdev->open = virtbt_open;
  233. hdev->close = virtbt_close;
  234. hdev->flush = virtbt_flush;
  235. hdev->send = virtbt_send_frame;
  236. if (virtio_has_feature(vdev, VIRTIO_BT_F_VND_HCI)) {
  237. __u16 vendor;
  238. if (virtio_has_feature(vdev, VIRTIO_BT_F_CONFIG_V2))
  239. virtio_cread(vdev, struct virtio_bt_config_v2,
  240. vendor, &vendor);
  241. else
  242. virtio_cread(vdev, struct virtio_bt_config,
  243. vendor, &vendor);
  244. switch (vendor) {
  245. case VIRTIO_BT_CONFIG_VENDOR_ZEPHYR:
  246. hdev->manufacturer = 1521;
  247. hdev->setup = virtbt_setup_zephyr;
  248. hdev->shutdown = virtbt_shutdown_generic;
  249. hdev->set_bdaddr = virtbt_set_bdaddr_zephyr;
  250. break;
  251. case VIRTIO_BT_CONFIG_VENDOR_INTEL:
  252. hdev->manufacturer = 2;
  253. hdev->setup = virtbt_setup_intel;
  254. hdev->shutdown = virtbt_shutdown_generic;
  255. hdev->set_bdaddr = virtbt_set_bdaddr_intel;
  256. set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
  257. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  258. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  259. break;
  260. case VIRTIO_BT_CONFIG_VENDOR_REALTEK:
  261. hdev->manufacturer = 93;
  262. hdev->setup = virtbt_setup_realtek;
  263. hdev->shutdown = virtbt_shutdown_generic;
  264. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  265. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  266. break;
  267. }
  268. }
  269. if (virtio_has_feature(vdev, VIRTIO_BT_F_MSFT_EXT)) {
  270. __u16 msft_opcode;
  271. if (virtio_has_feature(vdev, VIRTIO_BT_F_CONFIG_V2))
  272. virtio_cread(vdev, struct virtio_bt_config_v2,
  273. msft_opcode, &msft_opcode);
  274. else
  275. virtio_cread(vdev, struct virtio_bt_config,
  276. msft_opcode, &msft_opcode);
  277. hci_set_msft_opcode(hdev, msft_opcode);
  278. }
  279. if (virtio_has_feature(vdev, VIRTIO_BT_F_AOSP_EXT))
  280. hci_set_aosp_capable(hdev);
  281. if (hci_register_dev(hdev) < 0) {
  282. hci_free_dev(hdev);
  283. err = -EBUSY;
  284. goto failed;
  285. }
  286. virtio_device_ready(vdev);
  287. err = virtbt_open_vdev(vbt);
  288. if (err)
  289. goto open_failed;
  290. return 0;
  291. open_failed:
  292. hci_free_dev(hdev);
  293. failed:
  294. vdev->config->del_vqs(vdev);
  295. return err;
  296. }
  297. static void virtbt_remove(struct virtio_device *vdev)
  298. {
  299. struct virtio_bluetooth *vbt = vdev->priv;
  300. struct hci_dev *hdev = vbt->hdev;
  301. hci_unregister_dev(hdev);
  302. virtio_reset_device(vdev);
  303. virtbt_close_vdev(vbt);
  304. hci_free_dev(hdev);
  305. vbt->hdev = NULL;
  306. vdev->config->del_vqs(vdev);
  307. kfree(vbt);
  308. }
  309. static struct virtio_device_id virtbt_table[] = {
  310. { VIRTIO_ID_BT, VIRTIO_DEV_ANY_ID },
  311. { 0 },
  312. };
  313. MODULE_DEVICE_TABLE(virtio, virtbt_table);
  314. static const unsigned int virtbt_features[] = {
  315. VIRTIO_BT_F_VND_HCI,
  316. VIRTIO_BT_F_MSFT_EXT,
  317. VIRTIO_BT_F_AOSP_EXT,
  318. VIRTIO_BT_F_CONFIG_V2,
  319. };
  320. static struct virtio_driver virtbt_driver = {
  321. .driver.name = KBUILD_MODNAME,
  322. .feature_table = virtbt_features,
  323. .feature_table_size = ARRAY_SIZE(virtbt_features),
  324. .id_table = virtbt_table,
  325. .probe = virtbt_probe,
  326. .remove = virtbt_remove,
  327. };
  328. module_virtio_driver(virtbt_driver);
  329. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  330. MODULE_DESCRIPTION("Generic Bluetooth VIRTIO driver ver " VERSION);
  331. MODULE_VERSION(VERSION);
  332. MODULE_LICENSE("GPL");