1de3a9980SAnton Yakovlev // SPDX-License-Identifier: GPL-2.0+ 2de3a9980SAnton Yakovlev /* 3de3a9980SAnton Yakovlev * virtio-snd: Virtio sound device 4de3a9980SAnton Yakovlev * Copyright (C) 2021 OpenSynergy GmbH 5de3a9980SAnton Yakovlev */ 6de3a9980SAnton Yakovlev #include <linux/module.h> 7de3a9980SAnton Yakovlev #include <linux/moduleparam.h> 8de3a9980SAnton Yakovlev #include <linux/virtio_config.h> 9de3a9980SAnton Yakovlev #include <sound/initval.h> 10de3a9980SAnton Yakovlev #include <uapi/linux/virtio_ids.h> 11de3a9980SAnton Yakovlev 12de3a9980SAnton Yakovlev #include "virtio_card.h" 13de3a9980SAnton Yakovlev 149d45e514SAnton Yakovlev u32 virtsnd_msg_timeout_ms = MSEC_PER_SEC; 159d45e514SAnton Yakovlev module_param_named(msg_timeout_ms, virtsnd_msg_timeout_ms, uint, 0644); 169d45e514SAnton Yakovlev MODULE_PARM_DESC(msg_timeout_ms, "Message completion timeout in milliseconds"); 179d45e514SAnton Yakovlev 18de3a9980SAnton Yakovlev static void virtsnd_remove(struct virtio_device *vdev); 19de3a9980SAnton Yakovlev 20de3a9980SAnton Yakovlev /** 21de3a9980SAnton Yakovlev * virtsnd_event_send() - Add an event to the event queue. 22de3a9980SAnton Yakovlev * @vqueue: Underlying event virtqueue. 23de3a9980SAnton Yakovlev * @event: Event. 24de3a9980SAnton Yakovlev * @notify: Indicates whether or not to send a notification to the device. 25de3a9980SAnton Yakovlev * @gfp: Kernel flags for memory allocation. 26de3a9980SAnton Yakovlev * 27de3a9980SAnton Yakovlev * Context: Any context. 28de3a9980SAnton Yakovlev */ 29de3a9980SAnton Yakovlev static void virtsnd_event_send(struct virtqueue *vqueue, 30de3a9980SAnton Yakovlev struct virtio_snd_event *event, bool notify, 31de3a9980SAnton Yakovlev gfp_t gfp) 32de3a9980SAnton Yakovlev { 33de3a9980SAnton Yakovlev struct scatterlist sg; 34de3a9980SAnton Yakovlev struct scatterlist *psgs[1] = { &sg }; 35de3a9980SAnton Yakovlev 36de3a9980SAnton Yakovlev /* reset event content */ 37de3a9980SAnton Yakovlev memset(event, 0, sizeof(*event)); 38de3a9980SAnton Yakovlev 39de3a9980SAnton Yakovlev sg_init_one(&sg, event, sizeof(*event)); 40de3a9980SAnton Yakovlev 41de3a9980SAnton Yakovlev if (virtqueue_add_sgs(vqueue, psgs, 0, 1, event, gfp) || !notify) 42de3a9980SAnton Yakovlev return; 43de3a9980SAnton Yakovlev 44de3a9980SAnton Yakovlev if (virtqueue_kick_prepare(vqueue)) 45de3a9980SAnton Yakovlev virtqueue_notify(vqueue); 46de3a9980SAnton Yakovlev } 47de3a9980SAnton Yakovlev 48de3a9980SAnton Yakovlev /** 49de3a9980SAnton Yakovlev * virtsnd_event_dispatch() - Dispatch an event from the device side. 50de3a9980SAnton Yakovlev * @snd: VirtIO sound device. 51de3a9980SAnton Yakovlev * @event: VirtIO sound event. 52de3a9980SAnton Yakovlev * 53de3a9980SAnton Yakovlev * Context: Any context. 54de3a9980SAnton Yakovlev */ 55de3a9980SAnton Yakovlev static void virtsnd_event_dispatch(struct virtio_snd *snd, 56de3a9980SAnton Yakovlev struct virtio_snd_event *event) 57de3a9980SAnton Yakovlev { 58de3a9980SAnton Yakovlev } 59de3a9980SAnton Yakovlev 60de3a9980SAnton Yakovlev /** 61de3a9980SAnton Yakovlev * virtsnd_event_notify_cb() - Dispatch all reported events from the event queue. 62de3a9980SAnton Yakovlev * @vqueue: Underlying event virtqueue. 63de3a9980SAnton Yakovlev * 64de3a9980SAnton Yakovlev * This callback function is called upon a vring interrupt request from the 65de3a9980SAnton Yakovlev * device. 66de3a9980SAnton Yakovlev * 67de3a9980SAnton Yakovlev * Context: Interrupt context. 68de3a9980SAnton Yakovlev */ 69de3a9980SAnton Yakovlev static void virtsnd_event_notify_cb(struct virtqueue *vqueue) 70de3a9980SAnton Yakovlev { 71de3a9980SAnton Yakovlev struct virtio_snd *snd = vqueue->vdev->priv; 72de3a9980SAnton Yakovlev struct virtio_snd_queue *queue = virtsnd_event_queue(snd); 73de3a9980SAnton Yakovlev struct virtio_snd_event *event; 74de3a9980SAnton Yakovlev u32 length; 75de3a9980SAnton Yakovlev unsigned long flags; 76de3a9980SAnton Yakovlev 77de3a9980SAnton Yakovlev spin_lock_irqsave(&queue->lock, flags); 78de3a9980SAnton Yakovlev do { 79de3a9980SAnton Yakovlev virtqueue_disable_cb(vqueue); 80de3a9980SAnton Yakovlev while ((event = virtqueue_get_buf(vqueue, &length))) { 81de3a9980SAnton Yakovlev virtsnd_event_dispatch(snd, event); 82de3a9980SAnton Yakovlev virtsnd_event_send(vqueue, event, true, GFP_ATOMIC); 83de3a9980SAnton Yakovlev } 84de3a9980SAnton Yakovlev if (unlikely(virtqueue_is_broken(vqueue))) 85de3a9980SAnton Yakovlev break; 86de3a9980SAnton Yakovlev } while (!virtqueue_enable_cb(vqueue)); 87de3a9980SAnton Yakovlev spin_unlock_irqrestore(&queue->lock, flags); 88de3a9980SAnton Yakovlev } 89de3a9980SAnton Yakovlev 90de3a9980SAnton Yakovlev /** 91de3a9980SAnton Yakovlev * virtsnd_find_vqs() - Enumerate and initialize all virtqueues. 92de3a9980SAnton Yakovlev * @snd: VirtIO sound device. 93de3a9980SAnton Yakovlev * 94de3a9980SAnton Yakovlev * After calling this function, the event queue is disabled. 95de3a9980SAnton Yakovlev * 96de3a9980SAnton Yakovlev * Context: Any context. 97de3a9980SAnton Yakovlev * Return: 0 on success, -errno on failure. 98de3a9980SAnton Yakovlev */ 99de3a9980SAnton Yakovlev static int virtsnd_find_vqs(struct virtio_snd *snd) 100de3a9980SAnton Yakovlev { 101de3a9980SAnton Yakovlev struct virtio_device *vdev = snd->vdev; 102de3a9980SAnton Yakovlev static vq_callback_t *callbacks[VIRTIO_SND_VQ_MAX] = { 1039d45e514SAnton Yakovlev [VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb, 104de3a9980SAnton Yakovlev [VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb 105de3a9980SAnton Yakovlev }; 106de3a9980SAnton Yakovlev static const char *names[VIRTIO_SND_VQ_MAX] = { 1079d45e514SAnton Yakovlev [VIRTIO_SND_VQ_CONTROL] = "virtsnd-ctl", 108de3a9980SAnton Yakovlev [VIRTIO_SND_VQ_EVENT] = "virtsnd-event" 109de3a9980SAnton Yakovlev }; 110de3a9980SAnton Yakovlev struct virtqueue *vqs[VIRTIO_SND_VQ_MAX] = { 0 }; 111de3a9980SAnton Yakovlev unsigned int i; 112de3a9980SAnton Yakovlev unsigned int n; 113de3a9980SAnton Yakovlev int rc; 114de3a9980SAnton Yakovlev 115de3a9980SAnton Yakovlev rc = virtio_find_vqs(vdev, VIRTIO_SND_VQ_MAX, vqs, callbacks, names, 116de3a9980SAnton Yakovlev NULL); 117de3a9980SAnton Yakovlev if (rc) { 118de3a9980SAnton Yakovlev dev_err(&vdev->dev, "failed to initialize virtqueues\n"); 119de3a9980SAnton Yakovlev return rc; 120de3a9980SAnton Yakovlev } 121de3a9980SAnton Yakovlev 122de3a9980SAnton Yakovlev for (i = 0; i < VIRTIO_SND_VQ_MAX; ++i) 123de3a9980SAnton Yakovlev snd->queues[i].vqueue = vqs[i]; 124de3a9980SAnton Yakovlev 125de3a9980SAnton Yakovlev /* Allocate events and populate the event queue */ 126de3a9980SAnton Yakovlev virtqueue_disable_cb(vqs[VIRTIO_SND_VQ_EVENT]); 127de3a9980SAnton Yakovlev 128de3a9980SAnton Yakovlev n = virtqueue_get_vring_size(vqs[VIRTIO_SND_VQ_EVENT]); 129de3a9980SAnton Yakovlev 130de3a9980SAnton Yakovlev snd->event_msgs = kmalloc_array(n, sizeof(*snd->event_msgs), 131de3a9980SAnton Yakovlev GFP_KERNEL); 132de3a9980SAnton Yakovlev if (!snd->event_msgs) 133de3a9980SAnton Yakovlev return -ENOMEM; 134de3a9980SAnton Yakovlev 135de3a9980SAnton Yakovlev for (i = 0; i < n; ++i) 136de3a9980SAnton Yakovlev virtsnd_event_send(vqs[VIRTIO_SND_VQ_EVENT], 137de3a9980SAnton Yakovlev &snd->event_msgs[i], false, GFP_KERNEL); 138de3a9980SAnton Yakovlev 139de3a9980SAnton Yakovlev return 0; 140de3a9980SAnton Yakovlev } 141de3a9980SAnton Yakovlev 142de3a9980SAnton Yakovlev /** 143de3a9980SAnton Yakovlev * virtsnd_enable_event_vq() - Enable the event virtqueue. 144de3a9980SAnton Yakovlev * @snd: VirtIO sound device. 145de3a9980SAnton Yakovlev * 146de3a9980SAnton Yakovlev * Context: Any context. 147de3a9980SAnton Yakovlev */ 148de3a9980SAnton Yakovlev static void virtsnd_enable_event_vq(struct virtio_snd *snd) 149de3a9980SAnton Yakovlev { 150de3a9980SAnton Yakovlev struct virtio_snd_queue *queue = virtsnd_event_queue(snd); 151de3a9980SAnton Yakovlev 152de3a9980SAnton Yakovlev if (!virtqueue_enable_cb(queue->vqueue)) 153de3a9980SAnton Yakovlev virtsnd_event_notify_cb(queue->vqueue); 154de3a9980SAnton Yakovlev } 155de3a9980SAnton Yakovlev 156de3a9980SAnton Yakovlev /** 157de3a9980SAnton Yakovlev * virtsnd_disable_event_vq() - Disable the event virtqueue. 158de3a9980SAnton Yakovlev * @snd: VirtIO sound device. 159de3a9980SAnton Yakovlev * 160de3a9980SAnton Yakovlev * Context: Any context. 161de3a9980SAnton Yakovlev */ 162de3a9980SAnton Yakovlev static void virtsnd_disable_event_vq(struct virtio_snd *snd) 163de3a9980SAnton Yakovlev { 164de3a9980SAnton Yakovlev struct virtio_snd_queue *queue = virtsnd_event_queue(snd); 165de3a9980SAnton Yakovlev struct virtio_snd_event *event; 166de3a9980SAnton Yakovlev u32 length; 167de3a9980SAnton Yakovlev unsigned long flags; 168de3a9980SAnton Yakovlev 169de3a9980SAnton Yakovlev if (queue->vqueue) { 170de3a9980SAnton Yakovlev spin_lock_irqsave(&queue->lock, flags); 171de3a9980SAnton Yakovlev virtqueue_disable_cb(queue->vqueue); 172de3a9980SAnton Yakovlev while ((event = virtqueue_get_buf(queue->vqueue, &length))) 173de3a9980SAnton Yakovlev virtsnd_event_dispatch(snd, event); 174de3a9980SAnton Yakovlev spin_unlock_irqrestore(&queue->lock, flags); 175de3a9980SAnton Yakovlev } 176de3a9980SAnton Yakovlev } 177de3a9980SAnton Yakovlev 178de3a9980SAnton Yakovlev /** 179de3a9980SAnton Yakovlev * virtsnd_build_devs() - Read configuration and build ALSA devices. 180de3a9980SAnton Yakovlev * @snd: VirtIO sound device. 181de3a9980SAnton Yakovlev * 182de3a9980SAnton Yakovlev * Context: Any context that permits to sleep. 183de3a9980SAnton Yakovlev * Return: 0 on success, -errno on failure. 184de3a9980SAnton Yakovlev */ 185de3a9980SAnton Yakovlev static int virtsnd_build_devs(struct virtio_snd *snd) 186de3a9980SAnton Yakovlev { 187de3a9980SAnton Yakovlev struct virtio_device *vdev = snd->vdev; 188de3a9980SAnton Yakovlev struct device *dev = &vdev->dev; 189de3a9980SAnton Yakovlev int rc; 190de3a9980SAnton Yakovlev 191de3a9980SAnton Yakovlev rc = snd_card_new(dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, 192de3a9980SAnton Yakovlev THIS_MODULE, 0, &snd->card); 193de3a9980SAnton Yakovlev if (rc < 0) 194de3a9980SAnton Yakovlev return rc; 195de3a9980SAnton Yakovlev 196de3a9980SAnton Yakovlev snd->card->private_data = snd; 197de3a9980SAnton Yakovlev 198de3a9980SAnton Yakovlev strscpy(snd->card->driver, VIRTIO_SND_CARD_DRIVER, 199de3a9980SAnton Yakovlev sizeof(snd->card->driver)); 200de3a9980SAnton Yakovlev strscpy(snd->card->shortname, VIRTIO_SND_CARD_NAME, 201de3a9980SAnton Yakovlev sizeof(snd->card->shortname)); 202de3a9980SAnton Yakovlev if (dev->parent->bus) 203de3a9980SAnton Yakovlev snprintf(snd->card->longname, sizeof(snd->card->longname), 204de3a9980SAnton Yakovlev VIRTIO_SND_CARD_NAME " at %s/%s/%s", 205de3a9980SAnton Yakovlev dev->parent->bus->name, dev_name(dev->parent), 206de3a9980SAnton Yakovlev dev_name(dev)); 207de3a9980SAnton Yakovlev else 208de3a9980SAnton Yakovlev snprintf(snd->card->longname, sizeof(snd->card->longname), 209de3a9980SAnton Yakovlev VIRTIO_SND_CARD_NAME " at %s/%s", 210de3a9980SAnton Yakovlev dev_name(dev->parent), dev_name(dev)); 211de3a9980SAnton Yakovlev 212*29b96bf5SAnton Yakovlev rc = virtsnd_pcm_parse_cfg(snd); 213*29b96bf5SAnton Yakovlev if (rc) 214*29b96bf5SAnton Yakovlev return rc; 215*29b96bf5SAnton Yakovlev 216*29b96bf5SAnton Yakovlev if (snd->nsubstreams) { 217*29b96bf5SAnton Yakovlev rc = virtsnd_pcm_build_devs(snd); 218*29b96bf5SAnton Yakovlev if (rc) 219*29b96bf5SAnton Yakovlev return rc; 220*29b96bf5SAnton Yakovlev } 221*29b96bf5SAnton Yakovlev 222de3a9980SAnton Yakovlev return snd_card_register(snd->card); 223de3a9980SAnton Yakovlev } 224de3a9980SAnton Yakovlev 225de3a9980SAnton Yakovlev /** 226de3a9980SAnton Yakovlev * virtsnd_validate() - Validate if the device can be started. 227de3a9980SAnton Yakovlev * @vdev: VirtIO parent device. 228de3a9980SAnton Yakovlev * 229de3a9980SAnton Yakovlev * Context: Any context. 230de3a9980SAnton Yakovlev * Return: 0 on success, -EINVAL on failure. 231de3a9980SAnton Yakovlev */ 232de3a9980SAnton Yakovlev static int virtsnd_validate(struct virtio_device *vdev) 233de3a9980SAnton Yakovlev { 234de3a9980SAnton Yakovlev if (!vdev->config->get) { 235de3a9980SAnton Yakovlev dev_err(&vdev->dev, "configuration access disabled\n"); 236de3a9980SAnton Yakovlev return -EINVAL; 237de3a9980SAnton Yakovlev } 238de3a9980SAnton Yakovlev 239de3a9980SAnton Yakovlev if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) { 240de3a9980SAnton Yakovlev dev_err(&vdev->dev, 241de3a9980SAnton Yakovlev "device does not comply with spec version 1.x\n"); 242de3a9980SAnton Yakovlev return -EINVAL; 243de3a9980SAnton Yakovlev } 244de3a9980SAnton Yakovlev 2459d45e514SAnton Yakovlev if (!virtsnd_msg_timeout_ms) { 2469d45e514SAnton Yakovlev dev_err(&vdev->dev, "msg_timeout_ms value cannot be zero\n"); 2479d45e514SAnton Yakovlev return -EINVAL; 2489d45e514SAnton Yakovlev } 2499d45e514SAnton Yakovlev 250*29b96bf5SAnton Yakovlev if (virtsnd_pcm_validate(vdev)) 251*29b96bf5SAnton Yakovlev return -EINVAL; 252*29b96bf5SAnton Yakovlev 253de3a9980SAnton Yakovlev return 0; 254de3a9980SAnton Yakovlev } 255de3a9980SAnton Yakovlev 256de3a9980SAnton Yakovlev /** 257de3a9980SAnton Yakovlev * virtsnd_probe() - Create and initialize the device. 258de3a9980SAnton Yakovlev * @vdev: VirtIO parent device. 259de3a9980SAnton Yakovlev * 260de3a9980SAnton Yakovlev * Context: Any context that permits to sleep. 261de3a9980SAnton Yakovlev * Return: 0 on success, -errno on failure. 262de3a9980SAnton Yakovlev */ 263de3a9980SAnton Yakovlev static int virtsnd_probe(struct virtio_device *vdev) 264de3a9980SAnton Yakovlev { 265de3a9980SAnton Yakovlev struct virtio_snd *snd; 266de3a9980SAnton Yakovlev unsigned int i; 267de3a9980SAnton Yakovlev int rc; 268de3a9980SAnton Yakovlev 269de3a9980SAnton Yakovlev snd = devm_kzalloc(&vdev->dev, sizeof(*snd), GFP_KERNEL); 270de3a9980SAnton Yakovlev if (!snd) 271de3a9980SAnton Yakovlev return -ENOMEM; 272de3a9980SAnton Yakovlev 273de3a9980SAnton Yakovlev snd->vdev = vdev; 2749d45e514SAnton Yakovlev INIT_LIST_HEAD(&snd->ctl_msgs); 275*29b96bf5SAnton Yakovlev INIT_LIST_HEAD(&snd->pcm_list); 276de3a9980SAnton Yakovlev 277de3a9980SAnton Yakovlev vdev->priv = snd; 278de3a9980SAnton Yakovlev 279de3a9980SAnton Yakovlev for (i = 0; i < VIRTIO_SND_VQ_MAX; ++i) 280de3a9980SAnton Yakovlev spin_lock_init(&snd->queues[i].lock); 281de3a9980SAnton Yakovlev 282de3a9980SAnton Yakovlev rc = virtsnd_find_vqs(snd); 283de3a9980SAnton Yakovlev if (rc) 284de3a9980SAnton Yakovlev goto on_exit; 285de3a9980SAnton Yakovlev 286de3a9980SAnton Yakovlev virtio_device_ready(vdev); 287de3a9980SAnton Yakovlev 288de3a9980SAnton Yakovlev rc = virtsnd_build_devs(snd); 289de3a9980SAnton Yakovlev if (rc) 290de3a9980SAnton Yakovlev goto on_exit; 291de3a9980SAnton Yakovlev 292de3a9980SAnton Yakovlev virtsnd_enable_event_vq(snd); 293de3a9980SAnton Yakovlev 294de3a9980SAnton Yakovlev on_exit: 295de3a9980SAnton Yakovlev if (rc) 296de3a9980SAnton Yakovlev virtsnd_remove(vdev); 297de3a9980SAnton Yakovlev 298de3a9980SAnton Yakovlev return rc; 299de3a9980SAnton Yakovlev } 300de3a9980SAnton Yakovlev 301de3a9980SAnton Yakovlev /** 302de3a9980SAnton Yakovlev * virtsnd_remove() - Remove VirtIO and ALSA devices. 303de3a9980SAnton Yakovlev * @vdev: VirtIO parent device. 304de3a9980SAnton Yakovlev * 305de3a9980SAnton Yakovlev * Context: Any context that permits to sleep. 306de3a9980SAnton Yakovlev */ 307de3a9980SAnton Yakovlev static void virtsnd_remove(struct virtio_device *vdev) 308de3a9980SAnton Yakovlev { 309de3a9980SAnton Yakovlev struct virtio_snd *snd = vdev->priv; 310*29b96bf5SAnton Yakovlev unsigned int i; 311de3a9980SAnton Yakovlev 312de3a9980SAnton Yakovlev virtsnd_disable_event_vq(snd); 3139d45e514SAnton Yakovlev virtsnd_ctl_msg_cancel_all(snd); 314de3a9980SAnton Yakovlev 315de3a9980SAnton Yakovlev if (snd->card) 316de3a9980SAnton Yakovlev snd_card_free(snd->card); 317de3a9980SAnton Yakovlev 318de3a9980SAnton Yakovlev vdev->config->del_vqs(vdev); 319de3a9980SAnton Yakovlev vdev->config->reset(vdev); 320de3a9980SAnton Yakovlev 321*29b96bf5SAnton Yakovlev for (i = 0; snd->substreams && i < snd->nsubstreams; ++i) 322*29b96bf5SAnton Yakovlev cancel_work_sync(&snd->substreams[i].elapsed_period); 323*29b96bf5SAnton Yakovlev 324de3a9980SAnton Yakovlev kfree(snd->event_msgs); 325de3a9980SAnton Yakovlev } 326de3a9980SAnton Yakovlev 327de3a9980SAnton Yakovlev static const struct virtio_device_id id_table[] = { 328de3a9980SAnton Yakovlev { VIRTIO_ID_SOUND, VIRTIO_DEV_ANY_ID }, 329de3a9980SAnton Yakovlev { 0 }, 330de3a9980SAnton Yakovlev }; 331de3a9980SAnton Yakovlev 332de3a9980SAnton Yakovlev static struct virtio_driver virtsnd_driver = { 333de3a9980SAnton Yakovlev .driver.name = KBUILD_MODNAME, 334de3a9980SAnton Yakovlev .driver.owner = THIS_MODULE, 335de3a9980SAnton Yakovlev .id_table = id_table, 336de3a9980SAnton Yakovlev .validate = virtsnd_validate, 337de3a9980SAnton Yakovlev .probe = virtsnd_probe, 338de3a9980SAnton Yakovlev .remove = virtsnd_remove, 339de3a9980SAnton Yakovlev }; 340de3a9980SAnton Yakovlev 341de3a9980SAnton Yakovlev static int __init init(void) 342de3a9980SAnton Yakovlev { 343de3a9980SAnton Yakovlev return register_virtio_driver(&virtsnd_driver); 344de3a9980SAnton Yakovlev } 345de3a9980SAnton Yakovlev module_init(init); 346de3a9980SAnton Yakovlev 347de3a9980SAnton Yakovlev static void __exit fini(void) 348de3a9980SAnton Yakovlev { 349de3a9980SAnton Yakovlev unregister_virtio_driver(&virtsnd_driver); 350de3a9980SAnton Yakovlev } 351de3a9980SAnton Yakovlev module_exit(fini); 352de3a9980SAnton Yakovlev 353de3a9980SAnton Yakovlev MODULE_DEVICE_TABLE(virtio, id_table); 354de3a9980SAnton Yakovlev MODULE_DESCRIPTION("Virtio sound card driver"); 355de3a9980SAnton Yakovlev MODULE_LICENSE("GPL"); 356