virtio_pcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * virtio-snd: Virtio sound device
  4. * Copyright (C) 2021 OpenSynergy GmbH
  5. */
  6. #include <linux/moduleparam.h>
  7. #include <linux/virtio_config.h>
  8. #include "virtio_card.h"
  9. static u32 pcm_buffer_ms = 160;
  10. module_param(pcm_buffer_ms, uint, 0644);
  11. MODULE_PARM_DESC(pcm_buffer_ms, "PCM substream buffer time in milliseconds");
  12. static u32 pcm_periods_min = 2;
  13. module_param(pcm_periods_min, uint, 0644);
  14. MODULE_PARM_DESC(pcm_periods_min, "Minimum number of PCM periods");
  15. static u32 pcm_periods_max = 16;
  16. module_param(pcm_periods_max, uint, 0644);
  17. MODULE_PARM_DESC(pcm_periods_max, "Maximum number of PCM periods");
  18. static u32 pcm_period_ms_min = 10;
  19. module_param(pcm_period_ms_min, uint, 0644);
  20. MODULE_PARM_DESC(pcm_period_ms_min, "Minimum PCM period time in milliseconds");
  21. static u32 pcm_period_ms_max = 80;
  22. module_param(pcm_period_ms_max, uint, 0644);
  23. MODULE_PARM_DESC(pcm_period_ms_max, "Maximum PCM period time in milliseconds");
  24. /* Map for converting VirtIO format to ALSA format. */
  25. static const snd_pcm_format_t g_v2a_format_map[] = {
  26. [VIRTIO_SND_PCM_FMT_IMA_ADPCM] = SNDRV_PCM_FORMAT_IMA_ADPCM,
  27. [VIRTIO_SND_PCM_FMT_MU_LAW] = SNDRV_PCM_FORMAT_MU_LAW,
  28. [VIRTIO_SND_PCM_FMT_A_LAW] = SNDRV_PCM_FORMAT_A_LAW,
  29. [VIRTIO_SND_PCM_FMT_S8] = SNDRV_PCM_FORMAT_S8,
  30. [VIRTIO_SND_PCM_FMT_U8] = SNDRV_PCM_FORMAT_U8,
  31. [VIRTIO_SND_PCM_FMT_S16] = SNDRV_PCM_FORMAT_S16_LE,
  32. [VIRTIO_SND_PCM_FMT_U16] = SNDRV_PCM_FORMAT_U16_LE,
  33. [VIRTIO_SND_PCM_FMT_S18_3] = SNDRV_PCM_FORMAT_S18_3LE,
  34. [VIRTIO_SND_PCM_FMT_U18_3] = SNDRV_PCM_FORMAT_U18_3LE,
  35. [VIRTIO_SND_PCM_FMT_S20_3] = SNDRV_PCM_FORMAT_S20_3LE,
  36. [VIRTIO_SND_PCM_FMT_U20_3] = SNDRV_PCM_FORMAT_U20_3LE,
  37. [VIRTIO_SND_PCM_FMT_S24_3] = SNDRV_PCM_FORMAT_S24_3LE,
  38. [VIRTIO_SND_PCM_FMT_U24_3] = SNDRV_PCM_FORMAT_U24_3LE,
  39. [VIRTIO_SND_PCM_FMT_S20] = SNDRV_PCM_FORMAT_S20_LE,
  40. [VIRTIO_SND_PCM_FMT_U20] = SNDRV_PCM_FORMAT_U20_LE,
  41. [VIRTIO_SND_PCM_FMT_S24] = SNDRV_PCM_FORMAT_S24_LE,
  42. [VIRTIO_SND_PCM_FMT_U24] = SNDRV_PCM_FORMAT_U24_LE,
  43. [VIRTIO_SND_PCM_FMT_S32] = SNDRV_PCM_FORMAT_S32_LE,
  44. [VIRTIO_SND_PCM_FMT_U32] = SNDRV_PCM_FORMAT_U32_LE,
  45. [VIRTIO_SND_PCM_FMT_FLOAT] = SNDRV_PCM_FORMAT_FLOAT_LE,
  46. [VIRTIO_SND_PCM_FMT_FLOAT64] = SNDRV_PCM_FORMAT_FLOAT64_LE,
  47. [VIRTIO_SND_PCM_FMT_DSD_U8] = SNDRV_PCM_FORMAT_DSD_U8,
  48. [VIRTIO_SND_PCM_FMT_DSD_U16] = SNDRV_PCM_FORMAT_DSD_U16_LE,
  49. [VIRTIO_SND_PCM_FMT_DSD_U32] = SNDRV_PCM_FORMAT_DSD_U32_LE,
  50. [VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME] =
  51. SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE
  52. };
  53. /* Map for converting VirtIO frame rate to ALSA frame rate. */
  54. struct virtsnd_v2a_rate {
  55. unsigned int alsa_bit;
  56. unsigned int rate;
  57. };
  58. static const struct virtsnd_v2a_rate g_v2a_rate_map[] = {
  59. [VIRTIO_SND_PCM_RATE_5512] = { SNDRV_PCM_RATE_5512, 5512 },
  60. [VIRTIO_SND_PCM_RATE_8000] = { SNDRV_PCM_RATE_8000, 8000 },
  61. [VIRTIO_SND_PCM_RATE_11025] = { SNDRV_PCM_RATE_11025, 11025 },
  62. [VIRTIO_SND_PCM_RATE_16000] = { SNDRV_PCM_RATE_16000, 16000 },
  63. [VIRTIO_SND_PCM_RATE_22050] = { SNDRV_PCM_RATE_22050, 22050 },
  64. [VIRTIO_SND_PCM_RATE_32000] = { SNDRV_PCM_RATE_32000, 32000 },
  65. [VIRTIO_SND_PCM_RATE_44100] = { SNDRV_PCM_RATE_44100, 44100 },
  66. [VIRTIO_SND_PCM_RATE_48000] = { SNDRV_PCM_RATE_48000, 48000 },
  67. [VIRTIO_SND_PCM_RATE_64000] = { SNDRV_PCM_RATE_64000, 64000 },
  68. [VIRTIO_SND_PCM_RATE_88200] = { SNDRV_PCM_RATE_88200, 88200 },
  69. [VIRTIO_SND_PCM_RATE_96000] = { SNDRV_PCM_RATE_96000, 96000 },
  70. [VIRTIO_SND_PCM_RATE_176400] = { SNDRV_PCM_RATE_176400, 176400 },
  71. [VIRTIO_SND_PCM_RATE_192000] = { SNDRV_PCM_RATE_192000, 192000 }
  72. };
  73. /**
  74. * virtsnd_pcm_build_hw() - Parse substream config and build HW descriptor.
  75. * @vss: VirtIO substream.
  76. * @info: VirtIO substream information entry.
  77. *
  78. * Context: Any context.
  79. * Return: 0 on success, -EINVAL if configuration is invalid.
  80. */
  81. static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
  82. struct virtio_snd_pcm_info *info)
  83. {
  84. struct virtio_device *vdev = vss->snd->vdev;
  85. unsigned int i;
  86. u64 values;
  87. size_t sample_max = 0;
  88. size_t sample_min = 0;
  89. vss->features = le32_to_cpu(info->features);
  90. /*
  91. * TODO: set SNDRV_PCM_INFO_{BATCH,BLOCK_TRANSFER} if device supports
  92. * only message-based transport.
  93. */
  94. vss->hw.info =
  95. SNDRV_PCM_INFO_MMAP |
  96. SNDRV_PCM_INFO_MMAP_VALID |
  97. SNDRV_PCM_INFO_BATCH |
  98. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  99. SNDRV_PCM_INFO_INTERLEAVED |
  100. SNDRV_PCM_INFO_PAUSE;
  101. if (!info->channels_min || info->channels_min > info->channels_max) {
  102. dev_err(&vdev->dev,
  103. "SID %u: invalid channel range [%u %u]\n",
  104. vss->sid, info->channels_min, info->channels_max);
  105. return -EINVAL;
  106. }
  107. vss->hw.channels_min = info->channels_min;
  108. vss->hw.channels_max = info->channels_max;
  109. values = le64_to_cpu(info->formats);
  110. vss->hw.formats = 0;
  111. for (i = 0; i < ARRAY_SIZE(g_v2a_format_map); ++i)
  112. if (values & (1ULL << i)) {
  113. snd_pcm_format_t alsa_fmt = g_v2a_format_map[i];
  114. int bytes = snd_pcm_format_physical_width(alsa_fmt) / 8;
  115. if (!sample_min || sample_min > bytes)
  116. sample_min = bytes;
  117. if (sample_max < bytes)
  118. sample_max = bytes;
  119. vss->hw.formats |= pcm_format_to_bits(alsa_fmt);
  120. }
  121. if (!vss->hw.formats) {
  122. dev_err(&vdev->dev,
  123. "SID %u: no supported PCM sample formats found\n",
  124. vss->sid);
  125. return -EINVAL;
  126. }
  127. values = le64_to_cpu(info->rates);
  128. vss->hw.rates = 0;
  129. for (i = 0; i < ARRAY_SIZE(g_v2a_rate_map); ++i)
  130. if (values & (1ULL << i)) {
  131. if (!vss->hw.rate_min ||
  132. vss->hw.rate_min > g_v2a_rate_map[i].rate)
  133. vss->hw.rate_min = g_v2a_rate_map[i].rate;
  134. if (vss->hw.rate_max < g_v2a_rate_map[i].rate)
  135. vss->hw.rate_max = g_v2a_rate_map[i].rate;
  136. vss->hw.rates |= g_v2a_rate_map[i].alsa_bit;
  137. }
  138. if (!vss->hw.rates) {
  139. dev_err(&vdev->dev,
  140. "SID %u: no supported PCM frame rates found\n",
  141. vss->sid);
  142. return -EINVAL;
  143. }
  144. vss->hw.periods_min = pcm_periods_min;
  145. vss->hw.periods_max = pcm_periods_max;
  146. /*
  147. * We must ensure that there is enough space in the buffer to store
  148. * pcm_buffer_ms ms for the combination (Cmax, Smax, Rmax), where:
  149. * Cmax = maximum supported number of channels,
  150. * Smax = maximum supported sample size in bytes,
  151. * Rmax = maximum supported frame rate.
  152. */
  153. vss->hw.buffer_bytes_max =
  154. PAGE_ALIGN(sample_max * vss->hw.channels_max * pcm_buffer_ms *
  155. (vss->hw.rate_max / MSEC_PER_SEC));
  156. /*
  157. * We must ensure that the minimum period size is enough to store
  158. * pcm_period_ms_min ms for the combination (Cmin, Smin, Rmin), where:
  159. * Cmin = minimum supported number of channels,
  160. * Smin = minimum supported sample size in bytes,
  161. * Rmin = minimum supported frame rate.
  162. */
  163. vss->hw.period_bytes_min =
  164. sample_min * vss->hw.channels_min * pcm_period_ms_min *
  165. (vss->hw.rate_min / MSEC_PER_SEC);
  166. /*
  167. * We must ensure that the maximum period size is enough to store
  168. * pcm_period_ms_max ms for the combination (Cmax, Smax, Rmax).
  169. */
  170. vss->hw.period_bytes_max =
  171. sample_max * vss->hw.channels_max * pcm_period_ms_max *
  172. (vss->hw.rate_max / MSEC_PER_SEC);
  173. return 0;
  174. }
  175. /**
  176. * virtsnd_pcm_find() - Find the PCM device for the specified node ID.
  177. * @snd: VirtIO sound device.
  178. * @nid: Function node ID.
  179. *
  180. * Context: Any context.
  181. * Return: a pointer to the PCM device or ERR_PTR(-ENOENT).
  182. */
  183. struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid)
  184. {
  185. struct virtio_pcm *vpcm;
  186. list_for_each_entry(vpcm, &snd->pcm_list, list)
  187. if (vpcm->nid == nid)
  188. return vpcm;
  189. return ERR_PTR(-ENOENT);
  190. }
  191. /**
  192. * virtsnd_pcm_find_or_create() - Find or create the PCM device for the
  193. * specified node ID.
  194. * @snd: VirtIO sound device.
  195. * @nid: Function node ID.
  196. *
  197. * Context: Any context that permits to sleep.
  198. * Return: a pointer to the PCM device or ERR_PTR(-errno).
  199. */
  200. struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid)
  201. {
  202. struct virtio_device *vdev = snd->vdev;
  203. struct virtio_pcm *vpcm;
  204. vpcm = virtsnd_pcm_find(snd, nid);
  205. if (!IS_ERR(vpcm))
  206. return vpcm;
  207. vpcm = devm_kzalloc(&vdev->dev, sizeof(*vpcm), GFP_KERNEL);
  208. if (!vpcm)
  209. return ERR_PTR(-ENOMEM);
  210. vpcm->nid = nid;
  211. list_add_tail(&vpcm->list, &snd->pcm_list);
  212. return vpcm;
  213. }
  214. /**
  215. * virtsnd_pcm_validate() - Validate if the device can be started.
  216. * @vdev: VirtIO parent device.
  217. *
  218. * Context: Any context.
  219. * Return: 0 on success, -EINVAL on failure.
  220. */
  221. int virtsnd_pcm_validate(struct virtio_device *vdev)
  222. {
  223. if (pcm_periods_min < 2 || pcm_periods_min > pcm_periods_max) {
  224. dev_err(&vdev->dev,
  225. "invalid range [%u %u] of the number of PCM periods\n",
  226. pcm_periods_min, pcm_periods_max);
  227. return -EINVAL;
  228. }
  229. if (!pcm_period_ms_min || pcm_period_ms_min > pcm_period_ms_max) {
  230. dev_err(&vdev->dev,
  231. "invalid range [%u %u] of the size of the PCM period\n",
  232. pcm_period_ms_min, pcm_period_ms_max);
  233. return -EINVAL;
  234. }
  235. if (pcm_buffer_ms < pcm_periods_min * pcm_period_ms_min) {
  236. dev_err(&vdev->dev,
  237. "pcm_buffer_ms(=%u) value cannot be < %u ms\n",
  238. pcm_buffer_ms, pcm_periods_min * pcm_period_ms_min);
  239. return -EINVAL;
  240. }
  241. if (pcm_period_ms_max > pcm_buffer_ms / 2) {
  242. dev_err(&vdev->dev,
  243. "pcm_period_ms_max(=%u) value cannot be > %u ms\n",
  244. pcm_period_ms_max, pcm_buffer_ms / 2);
  245. return -EINVAL;
  246. }
  247. return 0;
  248. }
  249. /**
  250. * virtsnd_pcm_period_elapsed() - Kernel work function to handle the elapsed
  251. * period state.
  252. * @work: Elapsed period work.
  253. *
  254. * The main purpose of this function is to call snd_pcm_period_elapsed() in
  255. * a process context, not in an interrupt context. This is necessary because PCM
  256. * devices operate in non-atomic mode.
  257. *
  258. * Context: Process context.
  259. */
  260. static void virtsnd_pcm_period_elapsed(struct work_struct *work)
  261. {
  262. struct virtio_pcm_substream *vss =
  263. container_of(work, struct virtio_pcm_substream, elapsed_period);
  264. snd_pcm_period_elapsed(vss->substream);
  265. }
  266. /**
  267. * virtsnd_pcm_parse_cfg() - Parse the stream configuration.
  268. * @snd: VirtIO sound device.
  269. *
  270. * This function is called during initial device initialization.
  271. *
  272. * Context: Any context that permits to sleep.
  273. * Return: 0 on success, -errno on failure.
  274. */
  275. int virtsnd_pcm_parse_cfg(struct virtio_snd *snd)
  276. {
  277. struct virtio_device *vdev = snd->vdev;
  278. struct virtio_snd_pcm_info *info;
  279. u32 i;
  280. int rc;
  281. virtio_cread_le(vdev, struct virtio_snd_config, streams,
  282. &snd->nsubstreams);
  283. if (!snd->nsubstreams)
  284. return 0;
  285. snd->substreams = devm_kcalloc(&vdev->dev, snd->nsubstreams,
  286. sizeof(*snd->substreams), GFP_KERNEL);
  287. if (!snd->substreams)
  288. return -ENOMEM;
  289. info = kcalloc(snd->nsubstreams, sizeof(*info), GFP_KERNEL);
  290. if (!info)
  291. return -ENOMEM;
  292. rc = virtsnd_ctl_query_info(snd, VIRTIO_SND_R_PCM_INFO, 0,
  293. snd->nsubstreams, sizeof(*info), info);
  294. if (rc)
  295. goto on_exit;
  296. for (i = 0; i < snd->nsubstreams; ++i) {
  297. struct virtio_pcm_substream *vss = &snd->substreams[i];
  298. struct virtio_pcm *vpcm;
  299. vss->snd = snd;
  300. vss->sid = i;
  301. INIT_WORK(&vss->elapsed_period, virtsnd_pcm_period_elapsed);
  302. init_waitqueue_head(&vss->msg_empty);
  303. spin_lock_init(&vss->lock);
  304. rc = virtsnd_pcm_build_hw(vss, &info[i]);
  305. if (rc)
  306. goto on_exit;
  307. vss->nid = le32_to_cpu(info[i].hdr.hda_fn_nid);
  308. vpcm = virtsnd_pcm_find_or_create(snd, vss->nid);
  309. if (IS_ERR(vpcm)) {
  310. rc = PTR_ERR(vpcm);
  311. goto on_exit;
  312. }
  313. switch (info[i].direction) {
  314. case VIRTIO_SND_D_OUTPUT:
  315. vss->direction = SNDRV_PCM_STREAM_PLAYBACK;
  316. break;
  317. case VIRTIO_SND_D_INPUT:
  318. vss->direction = SNDRV_PCM_STREAM_CAPTURE;
  319. break;
  320. default:
  321. dev_err(&vdev->dev, "SID %u: unknown direction (%u)\n",
  322. vss->sid, info[i].direction);
  323. rc = -EINVAL;
  324. goto on_exit;
  325. }
  326. vpcm->streams[vss->direction].nsubstreams++;
  327. }
  328. on_exit:
  329. kfree(info);
  330. return rc;
  331. }
  332. /**
  333. * virtsnd_pcm_build_devs() - Build ALSA PCM devices.
  334. * @snd: VirtIO sound device.
  335. *
  336. * Context: Any context that permits to sleep.
  337. * Return: 0 on success, -errno on failure.
  338. */
  339. int virtsnd_pcm_build_devs(struct virtio_snd *snd)
  340. {
  341. struct virtio_device *vdev = snd->vdev;
  342. struct virtio_pcm *vpcm;
  343. u32 i;
  344. int rc;
  345. list_for_each_entry(vpcm, &snd->pcm_list, list) {
  346. unsigned int npbs =
  347. vpcm->streams[SNDRV_PCM_STREAM_PLAYBACK].nsubstreams;
  348. unsigned int ncps =
  349. vpcm->streams[SNDRV_PCM_STREAM_CAPTURE].nsubstreams;
  350. if (!npbs && !ncps)
  351. continue;
  352. rc = snd_pcm_new(snd->card, VIRTIO_SND_CARD_DRIVER, vpcm->nid,
  353. npbs, ncps, &vpcm->pcm);
  354. if (rc) {
  355. dev_err(&vdev->dev, "snd_pcm_new[%u] failed: %d\n",
  356. vpcm->nid, rc);
  357. return rc;
  358. }
  359. vpcm->pcm->info_flags = 0;
  360. vpcm->pcm->dev_class = SNDRV_PCM_CLASS_GENERIC;
  361. vpcm->pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  362. snprintf(vpcm->pcm->name, sizeof(vpcm->pcm->name),
  363. VIRTIO_SND_PCM_NAME " %u", vpcm->pcm->device);
  364. vpcm->pcm->private_data = vpcm;
  365. vpcm->pcm->nonatomic = true;
  366. for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
  367. struct virtio_pcm_stream *stream = &vpcm->streams[i];
  368. if (!stream->nsubstreams)
  369. continue;
  370. stream->substreams =
  371. devm_kcalloc(&vdev->dev, stream->nsubstreams,
  372. sizeof(*stream->substreams),
  373. GFP_KERNEL);
  374. if (!stream->substreams)
  375. return -ENOMEM;
  376. stream->nsubstreams = 0;
  377. }
  378. }
  379. for (i = 0; i < snd->nsubstreams; ++i) {
  380. struct virtio_pcm_stream *vs;
  381. struct virtio_pcm_substream *vss = &snd->substreams[i];
  382. vpcm = virtsnd_pcm_find(snd, vss->nid);
  383. if (IS_ERR(vpcm))
  384. return PTR_ERR(vpcm);
  385. vs = &vpcm->streams[vss->direction];
  386. vs->substreams[vs->nsubstreams++] = vss;
  387. }
  388. list_for_each_entry(vpcm, &snd->pcm_list, list) {
  389. for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
  390. struct virtio_pcm_stream *vs = &vpcm->streams[i];
  391. struct snd_pcm_str *ks = &vpcm->pcm->streams[i];
  392. struct snd_pcm_substream *kss;
  393. if (!vs->nsubstreams)
  394. continue;
  395. for (kss = ks->substream; kss; kss = kss->next)
  396. vs->substreams[kss->number]->substream = kss;
  397. snd_pcm_set_ops(vpcm->pcm, i, &virtsnd_pcm_ops);
  398. }
  399. snd_pcm_set_managed_buffer_all(vpcm->pcm,
  400. SNDRV_DMA_TYPE_VMALLOC, NULL,
  401. 0, 0);
  402. }
  403. return 0;
  404. }
  405. /**
  406. * virtsnd_pcm_event() - Handle the PCM device event notification.
  407. * @snd: VirtIO sound device.
  408. * @event: VirtIO sound event.
  409. *
  410. * Context: Interrupt context.
  411. */
  412. void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event)
  413. {
  414. struct virtio_pcm_substream *vss;
  415. u32 sid = le32_to_cpu(event->data);
  416. if (sid >= snd->nsubstreams)
  417. return;
  418. vss = &snd->substreams[sid];
  419. switch (le32_to_cpu(event->hdr.code)) {
  420. case VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED:
  421. /* TODO: deal with shmem elapsed period */
  422. break;
  423. case VIRTIO_SND_EVT_PCM_XRUN:
  424. spin_lock(&vss->lock);
  425. if (vss->xfer_enabled)
  426. vss->xfer_xrun = true;
  427. spin_unlock(&vss->lock);
  428. break;
  429. }
  430. }