virtio_card.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. case VIRTIO_SND_EVT_CTL_NOTIFY:
  59. virtsnd_kctl_event(snd, event);
  60. break;
  61. }
  62. }
  63. /**
  64. * virtsnd_event_notify_cb() - Dispatch all reported events from the event queue.
  65. * @vqueue: Underlying event virtqueue.
  66. *
  67. * This callback function is called upon a vring interrupt request from the
  68. * device.
  69. *
  70. * Context: Interrupt context.
  71. */
  72. static void virtsnd_event_notify_cb(struct virtqueue *vqueue)
  73. {
  74. struct virtio_snd *snd = vqueue->vdev->priv;
  75. struct virtio_snd_queue *queue = virtsnd_event_queue(snd);
  76. struct virtio_snd_event *event;
  77. u32 length;
  78. unsigned long flags;
  79. spin_lock_irqsave(&queue->lock, flags);
  80. do {
  81. virtqueue_disable_cb(vqueue);
  82. while ((event = virtqueue_get_buf(vqueue, &length))) {
  83. virtsnd_event_dispatch(snd, event);
  84. virtsnd_event_send(vqueue, event, true, GFP_ATOMIC);
  85. }
  86. } while (!virtqueue_enable_cb(vqueue));
  87. spin_unlock_irqrestore(&queue->lock, flags);
  88. }
  89. /**
  90. * virtsnd_find_vqs() - Enumerate and initialize all virtqueues.
  91. * @snd: VirtIO sound device.
  92. *
  93. * After calling this function, the event queue is disabled.
  94. *
  95. * Context: Any context.
  96. * Return: 0 on success, -errno on failure.
  97. */
  98. static int virtsnd_find_vqs(struct virtio_snd *snd)
  99. {
  100. struct virtio_device *vdev = snd->vdev;
  101. struct virtqueue_info vqs_info[VIRTIO_SND_VQ_MAX] = {
  102. [VIRTIO_SND_VQ_CONTROL] = { "virtsnd-ctl",
  103. virtsnd_ctl_notify_cb },
  104. [VIRTIO_SND_VQ_EVENT] = { "virtsnd-event",
  105. virtsnd_event_notify_cb },
  106. [VIRTIO_SND_VQ_TX] = { "virtsnd-tx",
  107. virtsnd_pcm_tx_notify_cb },
  108. [VIRTIO_SND_VQ_RX] = { "virtsnd-rx",
  109. virtsnd_pcm_rx_notify_cb },
  110. };
  111. struct virtqueue *vqs[VIRTIO_SND_VQ_MAX] = { 0 };
  112. unsigned int i;
  113. unsigned int n;
  114. int rc;
  115. rc = virtio_find_vqs(vdev, VIRTIO_SND_VQ_MAX, vqs, vqs_info, NULL);
  116. if (rc) {
  117. dev_err(&vdev->dev, "failed to initialize virtqueues\n");
  118. return rc;
  119. }
  120. for (i = 0; i < VIRTIO_SND_VQ_MAX; ++i)
  121. snd->queues[i].vqueue = vqs[i];
  122. /* Allocate events and populate the event queue */
  123. virtqueue_disable_cb(vqs[VIRTIO_SND_VQ_EVENT]);
  124. n = virtqueue_get_vring_size(vqs[VIRTIO_SND_VQ_EVENT]);
  125. snd->event_msgs = kmalloc_array(n, sizeof(*snd->event_msgs),
  126. GFP_KERNEL);
  127. if (!snd->event_msgs)
  128. return -ENOMEM;
  129. for (i = 0; i < n; ++i)
  130. virtsnd_event_send(vqs[VIRTIO_SND_VQ_EVENT],
  131. &snd->event_msgs[i], false, GFP_KERNEL);
  132. return 0;
  133. }
  134. /**
  135. * virtsnd_enable_event_vq() - Enable the event virtqueue.
  136. * @snd: VirtIO sound device.
  137. *
  138. * Context: Any context.
  139. */
  140. static void virtsnd_enable_event_vq(struct virtio_snd *snd)
  141. {
  142. struct virtio_snd_queue *queue = virtsnd_event_queue(snd);
  143. if (!virtqueue_enable_cb(queue->vqueue))
  144. virtsnd_event_notify_cb(queue->vqueue);
  145. }
  146. /**
  147. * virtsnd_disable_event_vq() - Disable the event virtqueue.
  148. * @snd: VirtIO sound device.
  149. *
  150. * Context: Any context.
  151. */
  152. static void virtsnd_disable_event_vq(struct virtio_snd *snd)
  153. {
  154. struct virtio_snd_queue *queue = virtsnd_event_queue(snd);
  155. struct virtio_snd_event *event;
  156. u32 length;
  157. unsigned long flags;
  158. if (queue->vqueue) {
  159. spin_lock_irqsave(&queue->lock, flags);
  160. virtqueue_disable_cb(queue->vqueue);
  161. while ((event = virtqueue_get_buf(queue->vqueue, &length)))
  162. virtsnd_event_dispatch(snd, event);
  163. spin_unlock_irqrestore(&queue->lock, flags);
  164. }
  165. }
  166. /**
  167. * virtsnd_build_devs() - Read configuration and build ALSA devices.
  168. * @snd: VirtIO sound device.
  169. *
  170. * Context: Any context that permits to sleep.
  171. * Return: 0 on success, -errno on failure.
  172. */
  173. static int virtsnd_build_devs(struct virtio_snd *snd)
  174. {
  175. struct virtio_device *vdev = snd->vdev;
  176. struct device *dev = &vdev->dev;
  177. int rc;
  178. rc = snd_card_new(dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  179. THIS_MODULE, 0, &snd->card);
  180. if (rc < 0)
  181. return rc;
  182. snd->card->private_data = snd;
  183. strscpy(snd->card->driver, VIRTIO_SND_CARD_DRIVER,
  184. sizeof(snd->card->driver));
  185. strscpy(snd->card->shortname, VIRTIO_SND_CARD_NAME,
  186. sizeof(snd->card->shortname));
  187. if (dev->parent->bus)
  188. snprintf(snd->card->longname, sizeof(snd->card->longname),
  189. VIRTIO_SND_CARD_NAME " at %s/%s/%s",
  190. dev->parent->bus->name, dev_name(dev->parent),
  191. dev_name(dev));
  192. else
  193. snprintf(snd->card->longname, sizeof(snd->card->longname),
  194. VIRTIO_SND_CARD_NAME " at %s/%s",
  195. dev_name(dev->parent), dev_name(dev));
  196. rc = virtsnd_jack_parse_cfg(snd);
  197. if (rc)
  198. return rc;
  199. rc = virtsnd_pcm_parse_cfg(snd);
  200. if (rc)
  201. return rc;
  202. rc = virtsnd_chmap_parse_cfg(snd);
  203. if (rc)
  204. return rc;
  205. if (virtio_has_feature(vdev, VIRTIO_SND_F_CTLS)) {
  206. rc = virtsnd_kctl_parse_cfg(snd);
  207. if (rc)
  208. return rc;
  209. }
  210. if (snd->njacks) {
  211. rc = virtsnd_jack_build_devs(snd);
  212. if (rc)
  213. return rc;
  214. }
  215. if (snd->nsubstreams) {
  216. rc = virtsnd_pcm_build_devs(snd);
  217. if (rc)
  218. return rc;
  219. }
  220. if (snd->nchmaps) {
  221. rc = virtsnd_chmap_build_devs(snd);
  222. if (rc)
  223. return rc;
  224. }
  225. if (snd->nkctls) {
  226. rc = virtsnd_kctl_build_devs(snd);
  227. if (rc)
  228. return rc;
  229. }
  230. return snd_card_register(snd->card);
  231. }
  232. /**
  233. * virtsnd_validate() - Validate if the device can be started.
  234. * @vdev: VirtIO parent device.
  235. *
  236. * Context: Any context.
  237. * Return: 0 on success, -EINVAL on failure.
  238. */
  239. static int virtsnd_validate(struct virtio_device *vdev)
  240. {
  241. if (!vdev->config->get) {
  242. dev_err(&vdev->dev, "configuration access disabled\n");
  243. return -EINVAL;
  244. }
  245. if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
  246. dev_err(&vdev->dev,
  247. "device does not comply with spec version 1.x\n");
  248. return -EINVAL;
  249. }
  250. if (!virtsnd_msg_timeout_ms) {
  251. dev_err(&vdev->dev, "msg_timeout_ms value cannot be zero\n");
  252. return -EINVAL;
  253. }
  254. if (virtsnd_pcm_validate(vdev))
  255. return -EINVAL;
  256. return 0;
  257. }
  258. /**
  259. * virtsnd_probe() - Create and initialize the device.
  260. * @vdev: VirtIO parent device.
  261. *
  262. * Context: Any context that permits to sleep.
  263. * Return: 0 on success, -errno on failure.
  264. */
  265. static int virtsnd_probe(struct virtio_device *vdev)
  266. {
  267. struct virtio_snd *snd;
  268. unsigned int i;
  269. int rc;
  270. snd = devm_kzalloc(&vdev->dev, sizeof(*snd), GFP_KERNEL);
  271. if (!snd)
  272. return -ENOMEM;
  273. snd->vdev = vdev;
  274. INIT_LIST_HEAD(&snd->ctl_msgs);
  275. INIT_LIST_HEAD(&snd->pcm_list);
  276. vdev->priv = snd;
  277. for (i = 0; i < VIRTIO_SND_VQ_MAX; ++i)
  278. spin_lock_init(&snd->queues[i].lock);
  279. rc = virtsnd_find_vqs(snd);
  280. if (rc)
  281. goto on_exit;
  282. virtio_device_ready(vdev);
  283. rc = virtsnd_build_devs(snd);
  284. if (rc)
  285. goto on_exit;
  286. virtsnd_enable_event_vq(snd);
  287. on_exit:
  288. if (rc)
  289. virtsnd_remove(vdev);
  290. return rc;
  291. }
  292. /**
  293. * virtsnd_remove() - Remove VirtIO and ALSA devices.
  294. * @vdev: VirtIO parent device.
  295. *
  296. * Context: Any context that permits to sleep.
  297. */
  298. static void virtsnd_remove(struct virtio_device *vdev)
  299. {
  300. struct virtio_snd *snd = vdev->priv;
  301. unsigned int i;
  302. virtsnd_disable_event_vq(snd);
  303. virtsnd_ctl_msg_cancel_all(snd);
  304. if (snd->card)
  305. snd_card_free(snd->card);
  306. vdev->config->del_vqs(vdev);
  307. virtio_reset_device(vdev);
  308. for (i = 0; snd->substreams && i < snd->nsubstreams; ++i) {
  309. struct virtio_pcm_substream *vss = &snd->substreams[i];
  310. cancel_work_sync(&vss->elapsed_period);
  311. virtsnd_pcm_msg_free(vss);
  312. }
  313. kfree(snd->event_msgs);
  314. }
  315. #ifdef CONFIG_PM_SLEEP
  316. /**
  317. * virtsnd_freeze() - Suspend device.
  318. * @vdev: VirtIO parent device.
  319. *
  320. * Context: Any context.
  321. * Return: 0 on success, -errno on failure.
  322. */
  323. static int virtsnd_freeze(struct virtio_device *vdev)
  324. {
  325. struct virtio_snd *snd = vdev->priv;
  326. unsigned int i;
  327. virtsnd_disable_event_vq(snd);
  328. virtsnd_ctl_msg_cancel_all(snd);
  329. vdev->config->del_vqs(vdev);
  330. virtio_reset_device(vdev);
  331. for (i = 0; i < snd->nsubstreams; ++i)
  332. cancel_work_sync(&snd->substreams[i].elapsed_period);
  333. kfree(snd->event_msgs);
  334. snd->event_msgs = NULL;
  335. return 0;
  336. }
  337. /**
  338. * virtsnd_restore() - Resume device.
  339. * @vdev: VirtIO parent device.
  340. *
  341. * Context: Any context.
  342. * Return: 0 on success, -errno on failure.
  343. */
  344. static int virtsnd_restore(struct virtio_device *vdev)
  345. {
  346. struct virtio_snd *snd = vdev->priv;
  347. int rc;
  348. rc = virtsnd_find_vqs(snd);
  349. if (rc)
  350. return rc;
  351. virtio_device_ready(vdev);
  352. virtsnd_enable_event_vq(snd);
  353. return 0;
  354. }
  355. #endif /* CONFIG_PM_SLEEP */
  356. static const struct virtio_device_id id_table[] = {
  357. { VIRTIO_ID_SOUND, VIRTIO_DEV_ANY_ID },
  358. { 0 },
  359. };
  360. static unsigned int features[] = {
  361. VIRTIO_SND_F_CTLS
  362. };
  363. static struct virtio_driver virtsnd_driver = {
  364. .driver.name = KBUILD_MODNAME,
  365. .id_table = id_table,
  366. .feature_table = features,
  367. .feature_table_size = ARRAY_SIZE(features),
  368. .validate = virtsnd_validate,
  369. .probe = virtsnd_probe,
  370. .remove = virtsnd_remove,
  371. #ifdef CONFIG_PM_SLEEP
  372. .freeze = virtsnd_freeze,
  373. .restore = virtsnd_restore,
  374. #endif
  375. };
  376. module_virtio_driver(virtsnd_driver);
  377. MODULE_DEVICE_TABLE(virtio, id_table);
  378. MODULE_DESCRIPTION("Virtio sound card driver");
  379. MODULE_LICENSE("GPL");