virtio_card.c 10 KB

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