1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/module.h>
4 #include <linux/virtio.h>
5 #include <linux/virtio_config.h>
6 #include <linux/skbuff.h>
8 #include <uapi/linux/virtio_ids.h>
9 #include <uapi/linux/virtio_bt.h>
11 #include <net/bluetooth/bluetooth.h>
12 #include <net/bluetooth/hci_core.h>
22 struct virtio_bluetooth {
23 struct virtio_device *vdev;
24 struct virtqueue *vqs[VIRTBT_NUM_VQS];
25 struct work_struct rx;
29 static int virtbt_add_inbuf(struct virtio_bluetooth *vbt)
31 struct virtqueue *vq = vbt->vqs[VIRTBT_VQ_RX];
32 struct scatterlist sg[1];
36 skb = alloc_skb(1000, GFP_KERNEL);
40 sg_init_one(sg, skb->data, 1000);
42 err = virtqueue_add_inbuf(vq, sg, 1, skb, GFP_KERNEL);
51 static int virtbt_open(struct hci_dev *hdev)
56 static int virtbt_open_vdev(struct virtio_bluetooth *vbt)
58 if (virtbt_add_inbuf(vbt) < 0)
61 virtqueue_kick(vbt->vqs[VIRTBT_VQ_RX]);
65 static int virtbt_close(struct hci_dev *hdev)
70 static int virtbt_close_vdev(struct virtio_bluetooth *vbt)
74 cancel_work_sync(&vbt->rx);
76 for (i = 0; i < ARRAY_SIZE(vbt->vqs); i++) {
77 struct virtqueue *vq = vbt->vqs[i];
80 while ((skb = virtqueue_detach_unused_buf(vq)))
87 static int virtbt_flush(struct hci_dev *hdev)
92 static int virtbt_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
94 struct virtio_bluetooth *vbt = hci_get_drvdata(hdev);
95 struct scatterlist sg[1];
98 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
100 sg_init_one(sg, skb->data, skb->len);
101 err = virtqueue_add_outbuf(vbt->vqs[VIRTBT_VQ_TX], sg, 1, skb,
108 virtqueue_kick(vbt->vqs[VIRTBT_VQ_TX]);
112 static int virtbt_setup_zephyr(struct hci_dev *hdev)
116 /* Read Build Information */
117 skb = __hci_cmd_sync(hdev, 0xfc08, 0, NULL, HCI_INIT_TIMEOUT);
121 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
123 hci_set_fw_info(hdev, "%s", skb->data + 1);
129 static int virtbt_set_bdaddr_zephyr(struct hci_dev *hdev,
130 const bdaddr_t *bdaddr)
135 skb = __hci_cmd_sync(hdev, 0xfc06, 6, bdaddr, HCI_INIT_TIMEOUT);
143 static int virtbt_setup_intel(struct hci_dev *hdev)
147 /* Intel Read Version */
148 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
156 static int virtbt_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
160 /* Intel Write BD Address */
161 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
169 static int virtbt_setup_realtek(struct hci_dev *hdev)
173 /* Read ROM Version */
174 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
178 bt_dev_info(hdev, "ROM version %u", *((__u8 *) (skb->data + 1)));
184 static int virtbt_shutdown_generic(struct hci_dev *hdev)
189 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
197 static void virtbt_rx_handle(struct virtio_bluetooth *vbt, struct sk_buff *skb)
201 pkt_type = *((__u8 *) skb->data);
206 case HCI_ACLDATA_PKT:
207 case HCI_SCODATA_PKT:
208 case HCI_ISODATA_PKT:
209 hci_skb_pkt_type(skb) = pkt_type;
210 hci_recv_frame(vbt->hdev, skb);
218 static void virtbt_rx_work(struct work_struct *work)
220 struct virtio_bluetooth *vbt = container_of(work,
221 struct virtio_bluetooth, rx);
225 skb = virtqueue_get_buf(vbt->vqs[VIRTBT_VQ_RX], &len);
230 virtbt_rx_handle(vbt, skb);
232 if (virtbt_add_inbuf(vbt) < 0)
235 virtqueue_kick(vbt->vqs[VIRTBT_VQ_RX]);
238 static void virtbt_tx_done(struct virtqueue *vq)
243 while ((skb = virtqueue_get_buf(vq, &len)))
247 static void virtbt_rx_done(struct virtqueue *vq)
249 struct virtio_bluetooth *vbt = vq->vdev->priv;
251 schedule_work(&vbt->rx);
254 static int virtbt_probe(struct virtio_device *vdev)
256 vq_callback_t *callbacks[VIRTBT_NUM_VQS] = {
257 [VIRTBT_VQ_TX] = virtbt_tx_done,
258 [VIRTBT_VQ_RX] = virtbt_rx_done,
260 const char *names[VIRTBT_NUM_VQS] = {
261 [VIRTBT_VQ_TX] = "tx",
262 [VIRTBT_VQ_RX] = "rx",
264 struct virtio_bluetooth *vbt;
265 struct hci_dev *hdev;
269 if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
272 type = virtio_cread8(vdev, offsetof(struct virtio_bt_config, type));
275 case VIRTIO_BT_CONFIG_TYPE_PRIMARY:
276 case VIRTIO_BT_CONFIG_TYPE_AMP:
282 vbt = kzalloc(sizeof(*vbt), GFP_KERNEL);
289 INIT_WORK(&vbt->rx, virtbt_rx_work);
291 err = virtio_find_vqs(vdev, VIRTBT_NUM_VQS, vbt->vqs, callbacks,
296 hdev = hci_alloc_dev();
304 hdev->bus = HCI_VIRTIO;
305 hdev->dev_type = type;
306 hci_set_drvdata(hdev, vbt);
308 hdev->open = virtbt_open;
309 hdev->close = virtbt_close;
310 hdev->flush = virtbt_flush;
311 hdev->send = virtbt_send_frame;
313 if (virtio_has_feature(vdev, VIRTIO_BT_F_VND_HCI)) {
316 if (virtio_has_feature(vdev, VIRTIO_BT_F_CONFIG_V2))
317 virtio_cread(vdev, struct virtio_bt_config_v2,
320 virtio_cread(vdev, struct virtio_bt_config,
324 case VIRTIO_BT_CONFIG_VENDOR_ZEPHYR:
325 hdev->manufacturer = 1521;
326 hdev->setup = virtbt_setup_zephyr;
327 hdev->shutdown = virtbt_shutdown_generic;
328 hdev->set_bdaddr = virtbt_set_bdaddr_zephyr;
331 case VIRTIO_BT_CONFIG_VENDOR_INTEL:
332 hdev->manufacturer = 2;
333 hdev->setup = virtbt_setup_intel;
334 hdev->shutdown = virtbt_shutdown_generic;
335 hdev->set_bdaddr = virtbt_set_bdaddr_intel;
336 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
337 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
338 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
341 case VIRTIO_BT_CONFIG_VENDOR_REALTEK:
342 hdev->manufacturer = 93;
343 hdev->setup = virtbt_setup_realtek;
344 hdev->shutdown = virtbt_shutdown_generic;
345 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
346 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
351 if (virtio_has_feature(vdev, VIRTIO_BT_F_MSFT_EXT)) {
354 if (virtio_has_feature(vdev, VIRTIO_BT_F_CONFIG_V2))
355 virtio_cread(vdev, struct virtio_bt_config_v2,
356 msft_opcode, &msft_opcode);
358 virtio_cread(vdev, struct virtio_bt_config,
359 msft_opcode, &msft_opcode);
361 hci_set_msft_opcode(hdev, msft_opcode);
364 if (virtio_has_feature(vdev, VIRTIO_BT_F_AOSP_EXT))
365 hci_set_aosp_capable(hdev);
367 if (hci_register_dev(hdev) < 0) {
373 virtio_device_ready(vdev);
374 err = virtbt_open_vdev(vbt);
383 vdev->config->del_vqs(vdev);
387 static void virtbt_remove(struct virtio_device *vdev)
389 struct virtio_bluetooth *vbt = vdev->priv;
390 struct hci_dev *hdev = vbt->hdev;
392 hci_unregister_dev(hdev);
393 virtio_reset_device(vdev);
394 virtbt_close_vdev(vbt);
399 vdev->config->del_vqs(vdev);
403 static struct virtio_device_id virtbt_table[] = {
404 { VIRTIO_ID_BT, VIRTIO_DEV_ANY_ID },
408 MODULE_DEVICE_TABLE(virtio, virtbt_table);
410 static const unsigned int virtbt_features[] = {
412 VIRTIO_BT_F_MSFT_EXT,
413 VIRTIO_BT_F_AOSP_EXT,
414 VIRTIO_BT_F_CONFIG_V2,
417 static struct virtio_driver virtbt_driver = {
418 .driver.name = KBUILD_MODNAME,
419 .driver.owner = THIS_MODULE,
420 .feature_table = virtbt_features,
421 .feature_table_size = ARRAY_SIZE(virtbt_features),
422 .id_table = virtbt_table,
423 .probe = virtbt_probe,
424 .remove = virtbt_remove,
427 module_virtio_driver(virtbt_driver);
429 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
430 MODULE_DESCRIPTION("Generic Bluetooth VIRTIO driver ver " VERSION);
431 MODULE_VERSION(VERSION);
432 MODULE_LICENSE("GPL");