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