virtio_pcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. SNDRV_PCM_INFO_NO_REWINDS |
  102. SNDRV_PCM_INFO_SYNC_APPLPTR;
  103. if (!info->channels_min || info->channels_min > info->channels_max) {
  104. dev_err(&vdev->dev,
  105. "SID %u: invalid channel range [%u %u]\n",
  106. vss->sid, info->channels_min, info->channels_max);
  107. return -EINVAL;
  108. }
  109. vss->hw.channels_min = info->channels_min;
  110. vss->hw.channels_max = info->channels_max;
  111. values = le64_to_cpu(info->formats);
  112. vss->hw.formats = 0;
  113. for (i = 0; i < ARRAY_SIZE(g_v2a_format_map); ++i)
  114. if (values & (1ULL << i)) {
  115. snd_pcm_format_t alsa_fmt = g_v2a_format_map[i];
  116. int bytes = snd_pcm_format_physical_width(alsa_fmt) / 8;
  117. if (!sample_min || sample_min > bytes)
  118. sample_min = bytes;
  119. if (sample_max < bytes)
  120. sample_max = bytes;
  121. vss->hw.formats |= pcm_format_to_bits(alsa_fmt);
  122. }
  123. if (!vss->hw.formats) {
  124. dev_err(&vdev->dev,
  125. "SID %u: no supported PCM sample formats found\n",
  126. vss->sid);
  127. return -EINVAL;
  128. }
  129. values = le64_to_cpu(info->rates);
  130. vss->hw.rates = 0;
  131. for (i = 0; i < ARRAY_SIZE(g_v2a_rate_map); ++i)
  132. if (values & (1ULL << i)) {
  133. if (!vss->hw.rate_min ||
  134. vss->hw.rate_min > g_v2a_rate_map[i].rate)
  135. vss->hw.rate_min = g_v2a_rate_map[i].rate;
  136. if (vss->hw.rate_max < g_v2a_rate_map[i].rate)
  137. vss->hw.rate_max = g_v2a_rate_map[i].rate;
  138. vss->hw.rates |= g_v2a_rate_map[i].alsa_bit;
  139. }
  140. if (!vss->hw.rates) {
  141. dev_err(&vdev->dev,
  142. "SID %u: no supported PCM frame rates found\n",
  143. vss->sid);
  144. return -EINVAL;
  145. }
  146. vss->hw.periods_min = pcm_periods_min;
  147. vss->hw.periods_max = pcm_periods_max;
  148. /*
  149. * We must ensure that there is enough space in the buffer to store
  150. * pcm_buffer_ms ms for the combination (Cmax, Smax, Rmax), where:
  151. * Cmax = maximum supported number of channels,
  152. * Smax = maximum supported sample size in bytes,
  153. * Rmax = maximum supported frame rate.
  154. */
  155. vss->hw.buffer_bytes_max =
  156. PAGE_ALIGN(sample_max * vss->hw.channels_max * pcm_buffer_ms *
  157. (vss->hw.rate_max / MSEC_PER_SEC));
  158. /*
  159. * We must ensure that the minimum period size is enough to store
  160. * pcm_period_ms_min ms for the combination (Cmin, Smin, Rmin), where:
  161. * Cmin = minimum supported number of channels,
  162. * Smin = minimum supported sample size in bytes,
  163. * Rmin = minimum supported frame rate.
  164. */
  165. vss->hw.period_bytes_min =
  166. sample_min * vss->hw.channels_min * pcm_period_ms_min *
  167. (vss->hw.rate_min / MSEC_PER_SEC);
  168. /*
  169. * We must ensure that the maximum period size is enough to store
  170. * pcm_period_ms_max ms for the combination (Cmax, Smax, Rmax).
  171. */
  172. vss->hw.period_bytes_max =
  173. sample_max * vss->hw.channels_max * pcm_period_ms_max *
  174. (vss->hw.rate_max / MSEC_PER_SEC);
  175. return 0;
  176. }
  177. /**
  178. * virtsnd_pcm_find() - Find the PCM device for the specified node ID.
  179. * @snd: VirtIO sound device.
  180. * @nid: Function node ID.
  181. *
  182. * Context: Any context.
  183. * Return: a pointer to the PCM device or ERR_PTR(-ENOENT).
  184. */
  185. struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid)
  186. {
  187. struct virtio_pcm *vpcm;
  188. list_for_each_entry(vpcm, &snd->pcm_list, list)
  189. if (vpcm->nid == nid)
  190. return vpcm;
  191. return ERR_PTR(-ENOENT);
  192. }
  193. /**
  194. * virtsnd_pcm_find_or_create() - Find or create the PCM device for the
  195. * specified node ID.
  196. * @snd: VirtIO sound device.
  197. * @nid: Function node ID.
  198. *
  199. * Context: Any context that permits to sleep.
  200. * Return: a pointer to the PCM device or ERR_PTR(-errno).
  201. */
  202. struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid)
  203. {
  204. struct virtio_device *vdev = snd->vdev;
  205. struct virtio_pcm *vpcm;
  206. vpcm = virtsnd_pcm_find(snd, nid);
  207. if (!IS_ERR(vpcm))
  208. return vpcm;
  209. vpcm = devm_kzalloc(&vdev->dev, sizeof(*vpcm), GFP_KERNEL);
  210. if (!vpcm)
  211. return ERR_PTR(-ENOMEM);
  212. vpcm->nid = nid;
  213. list_add_tail(&vpcm->list, &snd->pcm_list);
  214. return vpcm;
  215. }
  216. /**
  217. * virtsnd_pcm_validate() - Validate if the device can be started.
  218. * @vdev: VirtIO parent device.
  219. *
  220. * Context: Any context.
  221. * Return: 0 on success, -EINVAL on failure.
  222. */
  223. int virtsnd_pcm_validate(struct virtio_device *vdev)
  224. {
  225. if (pcm_periods_min < 2 || pcm_periods_min > pcm_periods_max) {
  226. dev_err(&vdev->dev,
  227. "invalid range [%u %u] of the number of PCM periods\n",
  228. pcm_periods_min, pcm_periods_max);
  229. return -EINVAL;
  230. }
  231. if (!pcm_period_ms_min || pcm_period_ms_min > pcm_period_ms_max) {
  232. dev_err(&vdev->dev,
  233. "invalid range [%u %u] of the size of the PCM period\n",
  234. pcm_period_ms_min, pcm_period_ms_max);
  235. return -EINVAL;
  236. }
  237. if (pcm_buffer_ms < pcm_periods_min * pcm_period_ms_min) {
  238. dev_err(&vdev->dev,
  239. "pcm_buffer_ms(=%u) value cannot be < %u ms\n",
  240. pcm_buffer_ms, pcm_periods_min * pcm_period_ms_min);
  241. return -EINVAL;
  242. }
  243. if (pcm_period_ms_max > pcm_buffer_ms / 2) {
  244. dev_err(&vdev->dev,
  245. "pcm_period_ms_max(=%u) value cannot be > %u ms\n",
  246. pcm_period_ms_max, pcm_buffer_ms / 2);
  247. return -EINVAL;
  248. }
  249. return 0;
  250. }
  251. /**
  252. * virtsnd_pcm_period_elapsed() - Kernel work function to handle the elapsed
  253. * period state.
  254. * @work: Elapsed period work.
  255. *
  256. * The main purpose of this function is to call snd_pcm_period_elapsed() in
  257. * a process context, not in an interrupt context. This is necessary because PCM
  258. * devices operate in non-atomic mode.
  259. *
  260. * Context: Process context.
  261. */
  262. static void virtsnd_pcm_period_elapsed(struct work_struct *work)
  263. {
  264. struct virtio_pcm_substream *vss =
  265. container_of(work, struct virtio_pcm_substream, elapsed_period);
  266. snd_pcm_period_elapsed(vss->substream);
  267. }
  268. /**
  269. * virtsnd_pcm_parse_cfg() - Parse the stream configuration.
  270. * @snd: VirtIO sound device.
  271. *
  272. * This function is called during initial device initialization.
  273. *
  274. * Context: Any context that permits to sleep.
  275. * Return: 0 on success, -errno on failure.
  276. */
  277. int virtsnd_pcm_parse_cfg(struct virtio_snd *snd)
  278. {
  279. struct virtio_device *vdev = snd->vdev;
  280. struct virtio_snd_pcm_info *info;
  281. u32 i;
  282. int rc;
  283. virtio_cread_le(vdev, struct virtio_snd_config, streams,
  284. &snd->nsubstreams);
  285. if (!snd->nsubstreams)
  286. return 0;
  287. snd->substreams = devm_kcalloc(&vdev->dev, snd->nsubstreams,
  288. sizeof(*snd->substreams), GFP_KERNEL);
  289. if (!snd->substreams)
  290. return -ENOMEM;
  291. info = kcalloc(snd->nsubstreams, sizeof(*info), GFP_KERNEL);
  292. if (!info)
  293. return -ENOMEM;
  294. rc = virtsnd_ctl_query_info(snd, VIRTIO_SND_R_PCM_INFO, 0,
  295. snd->nsubstreams, sizeof(*info), info);
  296. if (rc)
  297. goto on_exit;
  298. for (i = 0; i < snd->nsubstreams; ++i) {
  299. struct virtio_pcm_substream *vss = &snd->substreams[i];
  300. struct virtio_pcm *vpcm;
  301. vss->snd = snd;
  302. vss->sid = i;
  303. INIT_WORK(&vss->elapsed_period, virtsnd_pcm_period_elapsed);
  304. init_waitqueue_head(&vss->msg_empty);
  305. spin_lock_init(&vss->lock);
  306. rc = virtsnd_pcm_build_hw(vss, &info[i]);
  307. if (rc)
  308. goto on_exit;
  309. vss->nid = le32_to_cpu(info[i].hdr.hda_fn_nid);
  310. vpcm = virtsnd_pcm_find_or_create(snd, vss->nid);
  311. if (IS_ERR(vpcm)) {
  312. rc = PTR_ERR(vpcm);
  313. goto on_exit;
  314. }
  315. switch (info[i].direction) {
  316. case VIRTIO_SND_D_OUTPUT:
  317. vss->direction = SNDRV_PCM_STREAM_PLAYBACK;
  318. break;
  319. case VIRTIO_SND_D_INPUT:
  320. vss->direction = SNDRV_PCM_STREAM_CAPTURE;
  321. break;
  322. default:
  323. dev_err(&vdev->dev, "SID %u: unknown direction (%u)\n",
  324. vss->sid, info[i].direction);
  325. rc = -EINVAL;
  326. goto on_exit;
  327. }
  328. vpcm->streams[vss->direction].nsubstreams++;
  329. }
  330. on_exit:
  331. kfree(info);
  332. return rc;
  333. }
  334. /**
  335. * virtsnd_pcm_build_devs() - Build ALSA PCM devices.
  336. * @snd: VirtIO sound device.
  337. *
  338. * Context: Any context that permits to sleep.
  339. * Return: 0 on success, -errno on failure.
  340. */
  341. int virtsnd_pcm_build_devs(struct virtio_snd *snd)
  342. {
  343. struct virtio_device *vdev = snd->vdev;
  344. struct virtio_pcm *vpcm;
  345. u32 i;
  346. int rc;
  347. list_for_each_entry(vpcm, &snd->pcm_list, list) {
  348. unsigned int npbs =
  349. vpcm->streams[SNDRV_PCM_STREAM_PLAYBACK].nsubstreams;
  350. unsigned int ncps =
  351. vpcm->streams[SNDRV_PCM_STREAM_CAPTURE].nsubstreams;
  352. if (!npbs && !ncps)
  353. continue;
  354. rc = snd_pcm_new(snd->card, VIRTIO_SND_CARD_DRIVER, vpcm->nid,
  355. npbs, ncps, &vpcm->pcm);
  356. if (rc) {
  357. dev_err(&vdev->dev, "snd_pcm_new[%u] failed: %d\n",
  358. vpcm->nid, rc);
  359. return rc;
  360. }
  361. vpcm->pcm->info_flags = 0;
  362. vpcm->pcm->dev_class = SNDRV_PCM_CLASS_GENERIC;
  363. vpcm->pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  364. snprintf(vpcm->pcm->name, sizeof(vpcm->pcm->name),
  365. VIRTIO_SND_PCM_NAME " %u", vpcm->pcm->device);
  366. vpcm->pcm->private_data = vpcm;
  367. vpcm->pcm->nonatomic = true;
  368. for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
  369. struct virtio_pcm_stream *stream = &vpcm->streams[i];
  370. if (!stream->nsubstreams)
  371. continue;
  372. stream->substreams =
  373. devm_kcalloc(&vdev->dev, stream->nsubstreams,
  374. sizeof(*stream->substreams),
  375. GFP_KERNEL);
  376. if (!stream->substreams)
  377. return -ENOMEM;
  378. stream->nsubstreams = 0;
  379. }
  380. }
  381. for (i = 0; i < snd->nsubstreams; ++i) {
  382. struct virtio_pcm_stream *vs;
  383. struct virtio_pcm_substream *vss = &snd->substreams[i];
  384. vpcm = virtsnd_pcm_find(snd, vss->nid);
  385. if (IS_ERR(vpcm))
  386. return PTR_ERR(vpcm);
  387. vs = &vpcm->streams[vss->direction];
  388. vs->substreams[vs->nsubstreams++] = vss;
  389. }
  390. list_for_each_entry(vpcm, &snd->pcm_list, list) {
  391. for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
  392. struct virtio_pcm_stream *vs = &vpcm->streams[i];
  393. struct snd_pcm_str *ks = &vpcm->pcm->streams[i];
  394. struct snd_pcm_substream *kss;
  395. if (!vs->nsubstreams)
  396. continue;
  397. for (kss = ks->substream; kss; kss = kss->next)
  398. vs->substreams[kss->number]->substream = kss;
  399. snd_pcm_set_ops(vpcm->pcm, i, &virtsnd_pcm_ops[i]);
  400. }
  401. snd_pcm_set_managed_buffer_all(vpcm->pcm,
  402. SNDRV_DMA_TYPE_VMALLOC, NULL,
  403. 0, 0);
  404. }
  405. return 0;
  406. }
  407. /**
  408. * virtsnd_pcm_event() - Handle the PCM device event notification.
  409. * @snd: VirtIO sound device.
  410. * @event: VirtIO sound event.
  411. *
  412. * Context: Interrupt context.
  413. */
  414. void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event)
  415. {
  416. struct virtio_pcm_substream *vss;
  417. u32 sid = le32_to_cpu(event->data);
  418. if (sid >= snd->nsubstreams)
  419. return;
  420. vss = &snd->substreams[sid];
  421. switch (le32_to_cpu(event->hdr.code)) {
  422. case VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED:
  423. /* TODO: deal with shmem elapsed period */
  424. break;
  425. case VIRTIO_SND_EVT_PCM_XRUN:
  426. spin_lock(&vss->lock);
  427. if (vss->xfer_enabled)
  428. vss->xfer_xrun = true;
  429. spin_unlock(&vss->lock);
  430. break;
  431. }
  432. }