virtio_pcm_ops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * virtio-snd: Virtio sound device
  4. * Copyright (C) 2021 OpenSynergy GmbH
  5. */
  6. #include <sound/pcm_params.h>
  7. #include "virtio_card.h"
  8. /*
  9. * I/O messages lifetime
  10. * ---------------------
  11. *
  12. * Allocation:
  13. * Messages are initially allocated in the ops->hw_params() after the size and
  14. * number of periods have been successfully negotiated.
  15. *
  16. * Freeing:
  17. * Messages can be safely freed after the queue has been successfully flushed
  18. * (RELEASE command in the ops->sync_stop()) and the ops->hw_free() has been
  19. * called.
  20. *
  21. * When the substream stops, the ops->sync_stop() waits until the device has
  22. * completed all pending messages. This wait can be interrupted either by a
  23. * signal or due to a timeout. In this case, the device can still access
  24. * messages even after calling ops->hw_free(). It can also issue an interrupt,
  25. * and the interrupt handler will also try to access message structures.
  26. *
  27. * Therefore, freeing of already allocated messages occurs:
  28. *
  29. * - in ops->hw_params(), if this operator was called several times in a row,
  30. * or if ops->hw_free() failed to free messages previously;
  31. *
  32. * - in ops->hw_free(), if the queue has been successfully flushed;
  33. *
  34. * - in dev->release().
  35. */
  36. /* Map for converting ALSA format to VirtIO format. */
  37. struct virtsnd_a2v_format {
  38. snd_pcm_format_t alsa_bit;
  39. unsigned int vio_bit;
  40. };
  41. static const struct virtsnd_a2v_format g_a2v_format_map[] = {
  42. { SNDRV_PCM_FORMAT_IMA_ADPCM, VIRTIO_SND_PCM_FMT_IMA_ADPCM },
  43. { SNDRV_PCM_FORMAT_MU_LAW, VIRTIO_SND_PCM_FMT_MU_LAW },
  44. { SNDRV_PCM_FORMAT_A_LAW, VIRTIO_SND_PCM_FMT_A_LAW },
  45. { SNDRV_PCM_FORMAT_S8, VIRTIO_SND_PCM_FMT_S8 },
  46. { SNDRV_PCM_FORMAT_U8, VIRTIO_SND_PCM_FMT_U8 },
  47. { SNDRV_PCM_FORMAT_S16_LE, VIRTIO_SND_PCM_FMT_S16 },
  48. { SNDRV_PCM_FORMAT_U16_LE, VIRTIO_SND_PCM_FMT_U16 },
  49. { SNDRV_PCM_FORMAT_S18_3LE, VIRTIO_SND_PCM_FMT_S18_3 },
  50. { SNDRV_PCM_FORMAT_U18_3LE, VIRTIO_SND_PCM_FMT_U18_3 },
  51. { SNDRV_PCM_FORMAT_S20_3LE, VIRTIO_SND_PCM_FMT_S20_3 },
  52. { SNDRV_PCM_FORMAT_U20_3LE, VIRTIO_SND_PCM_FMT_U20_3 },
  53. { SNDRV_PCM_FORMAT_S24_3LE, VIRTIO_SND_PCM_FMT_S24_3 },
  54. { SNDRV_PCM_FORMAT_U24_3LE, VIRTIO_SND_PCM_FMT_U24_3 },
  55. { SNDRV_PCM_FORMAT_S20_LE, VIRTIO_SND_PCM_FMT_S20 },
  56. { SNDRV_PCM_FORMAT_U20_LE, VIRTIO_SND_PCM_FMT_U20 },
  57. { SNDRV_PCM_FORMAT_S24_LE, VIRTIO_SND_PCM_FMT_S24 },
  58. { SNDRV_PCM_FORMAT_U24_LE, VIRTIO_SND_PCM_FMT_U24 },
  59. { SNDRV_PCM_FORMAT_S32_LE, VIRTIO_SND_PCM_FMT_S32 },
  60. { SNDRV_PCM_FORMAT_U32_LE, VIRTIO_SND_PCM_FMT_U32 },
  61. { SNDRV_PCM_FORMAT_FLOAT_LE, VIRTIO_SND_PCM_FMT_FLOAT },
  62. { SNDRV_PCM_FORMAT_FLOAT64_LE, VIRTIO_SND_PCM_FMT_FLOAT64 },
  63. { SNDRV_PCM_FORMAT_DSD_U8, VIRTIO_SND_PCM_FMT_DSD_U8 },
  64. { SNDRV_PCM_FORMAT_DSD_U16_LE, VIRTIO_SND_PCM_FMT_DSD_U16 },
  65. { SNDRV_PCM_FORMAT_DSD_U32_LE, VIRTIO_SND_PCM_FMT_DSD_U32 },
  66. { SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE,
  67. VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME }
  68. };
  69. /* Map for converting ALSA frame rate to VirtIO frame rate. */
  70. struct virtsnd_a2v_rate {
  71. unsigned int rate;
  72. unsigned int vio_bit;
  73. };
  74. static const struct virtsnd_a2v_rate g_a2v_rate_map[] = {
  75. { 5512, VIRTIO_SND_PCM_RATE_5512 },
  76. { 8000, VIRTIO_SND_PCM_RATE_8000 },
  77. { 11025, VIRTIO_SND_PCM_RATE_11025 },
  78. { 16000, VIRTIO_SND_PCM_RATE_16000 },
  79. { 22050, VIRTIO_SND_PCM_RATE_22050 },
  80. { 32000, VIRTIO_SND_PCM_RATE_32000 },
  81. { 44100, VIRTIO_SND_PCM_RATE_44100 },
  82. { 48000, VIRTIO_SND_PCM_RATE_48000 },
  83. { 64000, VIRTIO_SND_PCM_RATE_64000 },
  84. { 88200, VIRTIO_SND_PCM_RATE_88200 },
  85. { 96000, VIRTIO_SND_PCM_RATE_96000 },
  86. { 176400, VIRTIO_SND_PCM_RATE_176400 },
  87. { 192000, VIRTIO_SND_PCM_RATE_192000 }
  88. };
  89. static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream);
  90. /**
  91. * virtsnd_pcm_open() - Open the PCM substream.
  92. * @substream: Kernel ALSA substream.
  93. *
  94. * Context: Process context.
  95. * Return: 0 on success, -errno on failure.
  96. */
  97. static int virtsnd_pcm_open(struct snd_pcm_substream *substream)
  98. {
  99. struct virtio_pcm *vpcm = snd_pcm_substream_chip(substream);
  100. struct virtio_pcm_stream *vs = &vpcm->streams[substream->stream];
  101. struct virtio_pcm_substream *vss = vs->substreams[substream->number];
  102. substream->runtime->hw = vss->hw;
  103. substream->private_data = vss;
  104. snd_pcm_hw_constraint_integer(substream->runtime,
  105. SNDRV_PCM_HW_PARAM_PERIODS);
  106. vss->stopped = !!virtsnd_pcm_msg_pending_num(vss);
  107. vss->suspended = false;
  108. /*
  109. * If the substream has already been used, then the I/O queue may be in
  110. * an invalid state. Just in case, we do a check and try to return the
  111. * queue to its original state, if necessary.
  112. */
  113. return virtsnd_pcm_sync_stop(substream);
  114. }
  115. /**
  116. * virtsnd_pcm_close() - Close the PCM substream.
  117. * @substream: Kernel ALSA substream.
  118. *
  119. * Context: Process context.
  120. * Return: 0.
  121. */
  122. static int virtsnd_pcm_close(struct snd_pcm_substream *substream)
  123. {
  124. return 0;
  125. }
  126. /**
  127. * virtsnd_pcm_dev_set_params() - Set the parameters of the PCM substream on
  128. * the device side.
  129. * @vss: VirtIO PCM substream.
  130. * @buffer_bytes: Size of the hardware buffer.
  131. * @period_bytes: Size of the hardware period.
  132. * @channels: Selected number of channels.
  133. * @format: Selected sample format (SNDRV_PCM_FORMAT_XXX).
  134. * @rate: Selected frame rate.
  135. *
  136. * Context: Any context that permits to sleep.
  137. * Return: 0 on success, -errno on failure.
  138. */
  139. static int virtsnd_pcm_dev_set_params(struct virtio_pcm_substream *vss,
  140. unsigned int buffer_bytes,
  141. unsigned int period_bytes,
  142. unsigned int channels,
  143. snd_pcm_format_t format,
  144. unsigned int rate)
  145. {
  146. struct virtio_snd_msg *msg;
  147. struct virtio_snd_pcm_set_params *request;
  148. unsigned int i;
  149. int vformat = -1;
  150. int vrate = -1;
  151. for (i = 0; i < ARRAY_SIZE(g_a2v_format_map); ++i)
  152. if (g_a2v_format_map[i].alsa_bit == format) {
  153. vformat = g_a2v_format_map[i].vio_bit;
  154. break;
  155. }
  156. for (i = 0; i < ARRAY_SIZE(g_a2v_rate_map); ++i)
  157. if (g_a2v_rate_map[i].rate == rate) {
  158. vrate = g_a2v_rate_map[i].vio_bit;
  159. break;
  160. }
  161. if (vformat == -1 || vrate == -1)
  162. return -EINVAL;
  163. msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_SET_PARAMS,
  164. GFP_KERNEL);
  165. if (!msg)
  166. return -ENOMEM;
  167. request = virtsnd_ctl_msg_request(msg);
  168. request->buffer_bytes = cpu_to_le32(buffer_bytes);
  169. request->period_bytes = cpu_to_le32(period_bytes);
  170. request->channels = channels;
  171. request->format = vformat;
  172. request->rate = vrate;
  173. if (vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING))
  174. request->features |=
  175. cpu_to_le32(1U << VIRTIO_SND_PCM_F_MSG_POLLING);
  176. if (vss->features & (1U << VIRTIO_SND_PCM_F_EVT_XRUNS))
  177. request->features |=
  178. cpu_to_le32(1U << VIRTIO_SND_PCM_F_EVT_XRUNS);
  179. return virtsnd_ctl_msg_send_sync(vss->snd, msg);
  180. }
  181. /**
  182. * virtsnd_pcm_hw_params() - Set the parameters of the PCM substream.
  183. * @substream: Kernel ALSA substream.
  184. * @hw_params: Hardware parameters.
  185. *
  186. * Context: Process context.
  187. * Return: 0 on success, -errno on failure.
  188. */
  189. static int virtsnd_pcm_hw_params(struct snd_pcm_substream *substream,
  190. struct snd_pcm_hw_params *hw_params)
  191. {
  192. struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
  193. struct virtio_device *vdev = vss->snd->vdev;
  194. int rc;
  195. if (virtsnd_pcm_msg_pending_num(vss)) {
  196. dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n",
  197. vss->sid);
  198. return -EBADFD;
  199. }
  200. rc = virtsnd_pcm_dev_set_params(vss, params_buffer_bytes(hw_params),
  201. params_period_bytes(hw_params),
  202. params_channels(hw_params),
  203. params_format(hw_params),
  204. params_rate(hw_params));
  205. if (rc)
  206. return rc;
  207. /*
  208. * Free previously allocated messages if ops->hw_params() is called
  209. * several times in a row, or if ops->hw_free() failed to free messages.
  210. */
  211. virtsnd_pcm_msg_free(vss);
  212. return virtsnd_pcm_msg_alloc(vss, params_periods(hw_params),
  213. params_period_bytes(hw_params));
  214. }
  215. /**
  216. * virtsnd_pcm_hw_free() - Reset the parameters of the PCM substream.
  217. * @substream: Kernel ALSA substream.
  218. *
  219. * Context: Process context.
  220. * Return: 0
  221. */
  222. static int virtsnd_pcm_hw_free(struct snd_pcm_substream *substream)
  223. {
  224. struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
  225. /* If the queue is flushed, we can safely free the messages here. */
  226. if (!virtsnd_pcm_msg_pending_num(vss))
  227. virtsnd_pcm_msg_free(vss);
  228. return 0;
  229. }
  230. /**
  231. * virtsnd_pcm_prepare() - Prepare the PCM substream.
  232. * @substream: Kernel ALSA substream.
  233. *
  234. * Context: Process context.
  235. * Return: 0 on success, -errno on failure.
  236. */
  237. static int virtsnd_pcm_prepare(struct snd_pcm_substream *substream)
  238. {
  239. struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
  240. struct virtio_device *vdev = vss->snd->vdev;
  241. struct virtio_snd_msg *msg;
  242. if (!vss->suspended) {
  243. if (virtsnd_pcm_msg_pending_num(vss)) {
  244. dev_err(&vdev->dev, "SID %u: invalid I/O queue state\n",
  245. vss->sid);
  246. return -EBADFD;
  247. }
  248. vss->buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  249. vss->hw_ptr = 0;
  250. vss->msg_last_enqueued = -1;
  251. } else {
  252. struct snd_pcm_runtime *runtime = substream->runtime;
  253. unsigned int buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  254. unsigned int period_bytes = snd_pcm_lib_period_bytes(substream);
  255. int rc;
  256. rc = virtsnd_pcm_dev_set_params(vss, buffer_bytes, period_bytes,
  257. runtime->channels,
  258. runtime->format, runtime->rate);
  259. if (rc)
  260. return rc;
  261. }
  262. vss->xfer_xrun = false;
  263. vss->suspended = false;
  264. vss->msg_count = 0;
  265. msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_PREPARE,
  266. GFP_KERNEL);
  267. if (!msg)
  268. return -ENOMEM;
  269. return virtsnd_ctl_msg_send_sync(vss->snd, msg);
  270. }
  271. /**
  272. * virtsnd_pcm_trigger() - Process command for the PCM substream.
  273. * @substream: Kernel ALSA substream.
  274. * @command: Substream command (SNDRV_PCM_TRIGGER_XXX).
  275. *
  276. * Context: Any context. Takes and releases the VirtIO substream spinlock.
  277. * May take and release the tx/rx queue spinlock.
  278. * Return: 0 on success, -errno on failure.
  279. */
  280. static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
  281. {
  282. struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
  283. struct virtio_snd *snd = vss->snd;
  284. struct virtio_snd_queue *queue;
  285. struct virtio_snd_msg *msg;
  286. unsigned long flags;
  287. int rc;
  288. switch (command) {
  289. case SNDRV_PCM_TRIGGER_START:
  290. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  291. queue = virtsnd_pcm_queue(vss);
  292. spin_lock_irqsave(&queue->lock, flags);
  293. spin_lock(&vss->lock);
  294. rc = virtsnd_pcm_msg_send(vss);
  295. if (!rc)
  296. vss->xfer_enabled = true;
  297. spin_unlock(&vss->lock);
  298. spin_unlock_irqrestore(&queue->lock, flags);
  299. if (rc)
  300. return rc;
  301. msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START,
  302. GFP_KERNEL);
  303. if (!msg) {
  304. spin_lock_irqsave(&vss->lock, flags);
  305. vss->xfer_enabled = false;
  306. spin_unlock_irqrestore(&vss->lock, flags);
  307. return -ENOMEM;
  308. }
  309. return virtsnd_ctl_msg_send_sync(snd, msg);
  310. case SNDRV_PCM_TRIGGER_SUSPEND:
  311. vss->suspended = true;
  312. fallthrough;
  313. case SNDRV_PCM_TRIGGER_STOP:
  314. vss->stopped = true;
  315. fallthrough;
  316. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  317. spin_lock_irqsave(&vss->lock, flags);
  318. vss->xfer_enabled = false;
  319. spin_unlock_irqrestore(&vss->lock, flags);
  320. msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP,
  321. GFP_KERNEL);
  322. if (!msg)
  323. return -ENOMEM;
  324. return virtsnd_ctl_msg_send_sync(snd, msg);
  325. default:
  326. return -EINVAL;
  327. }
  328. }
  329. /**
  330. * virtsnd_pcm_sync_stop() - Synchronous PCM substream stop.
  331. * @substream: Kernel ALSA substream.
  332. *
  333. * The function can be called both from the upper level or from the driver
  334. * itself.
  335. *
  336. * Context: Process context. Takes and releases the VirtIO substream spinlock.
  337. * Return: 0 on success, -errno on failure.
  338. */
  339. static int virtsnd_pcm_sync_stop(struct snd_pcm_substream *substream)
  340. {
  341. struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
  342. struct virtio_snd *snd = vss->snd;
  343. struct virtio_snd_msg *msg;
  344. unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms);
  345. int rc;
  346. cancel_work_sync(&vss->elapsed_period);
  347. if (!vss->stopped)
  348. return 0;
  349. msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_RELEASE,
  350. GFP_KERNEL);
  351. if (!msg)
  352. return -ENOMEM;
  353. rc = virtsnd_ctl_msg_send_sync(snd, msg);
  354. if (rc)
  355. return rc;
  356. /*
  357. * The spec states that upon receipt of the RELEASE command "the device
  358. * MUST complete all pending I/O messages for the specified stream ID".
  359. * Thus, we consider the absence of I/O messages in the queue as an
  360. * indication that the substream has been released.
  361. */
  362. rc = wait_event_interruptible_timeout(vss->msg_empty,
  363. !virtsnd_pcm_msg_pending_num(vss),
  364. js);
  365. if (rc <= 0) {
  366. dev_warn(&snd->vdev->dev, "SID %u: failed to flush I/O queue\n",
  367. vss->sid);
  368. return !rc ? -ETIMEDOUT : rc;
  369. }
  370. vss->stopped = false;
  371. return 0;
  372. }
  373. /**
  374. * virtsnd_pcm_pointer() - Get the current hardware position for the PCM
  375. * substream.
  376. * @substream: Kernel ALSA substream.
  377. *
  378. * Context: Any context. Takes and releases the VirtIO substream spinlock.
  379. * Return: Hardware position in frames inside [0 ... buffer_size) range.
  380. */
  381. static snd_pcm_uframes_t
  382. virtsnd_pcm_pointer(struct snd_pcm_substream *substream)
  383. {
  384. struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
  385. snd_pcm_uframes_t hw_ptr = SNDRV_PCM_POS_XRUN;
  386. unsigned long flags;
  387. spin_lock_irqsave(&vss->lock, flags);
  388. if (!vss->xfer_xrun)
  389. hw_ptr = bytes_to_frames(substream->runtime, vss->hw_ptr);
  390. spin_unlock_irqrestore(&vss->lock, flags);
  391. return hw_ptr;
  392. }
  393. /* PCM substream operators map. */
  394. const struct snd_pcm_ops virtsnd_pcm_ops = {
  395. .open = virtsnd_pcm_open,
  396. .close = virtsnd_pcm_close,
  397. .ioctl = snd_pcm_lib_ioctl,
  398. .hw_params = virtsnd_pcm_hw_params,
  399. .hw_free = virtsnd_pcm_hw_free,
  400. .prepare = virtsnd_pcm_prepare,
  401. .trigger = virtsnd_pcm_trigger,
  402. .sync_stop = virtsnd_pcm_sync_stop,
  403. .pointer = virtsnd_pcm_pointer,
  404. };