mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
To: Anton Yakovlev <anton.yakovlev@opensynergy.com>
Cc: virtualization@lists.linux-foundation.org,
	alsa-devel@alsa-project.org, virtio-dev@lists.oasis-open.org,
	linux-kernel@vger.kernel.org, Jaroslav Kysela <perex@perex.cz>,
	Takashi Iwai <tiwai@suse.com>,
	"Michael S. Tsirkin" <mst@redhat.com>
Subject: Re: [PATCH v2 8/9] ALSA: virtio: introduce PCM channel map support
Date: Tue, 26 Jan 2021 10:22:49 +0100 (CET)	[thread overview]
Message-ID: <643248d4-d246-686f-34c3-7e592777e3ec@intel.com> (raw)
In-Reply-To: <20210124165408.1122868-9-anton.yakovlev@opensynergy.com>


On Sun, 24 Jan 2021, Anton Yakovlev wrote:

> Enumerate all available PCM channel maps and create ALSA controls.
>
> Signed-off-by: Anton Yakovlev <anton.yakovlev@opensynergy.com>
> ---
> sound/virtio/Makefile       |   1 +
> sound/virtio/virtio_card.c  |  15 +++
> sound/virtio/virtio_card.h  |   8 ++
> sound/virtio/virtio_chmap.c | 237 ++++++++++++++++++++++++++++++++++++
> sound/virtio/virtio_pcm.h   |   4 +
> 5 files changed, 265 insertions(+)
> create mode 100644 sound/virtio/virtio_chmap.c

[snip]

> diff --git a/sound/virtio/virtio_chmap.c b/sound/virtio/virtio_chmap.c
> new file mode 100644
> index 000000000000..8a2ddc4dcffb
> --- /dev/null
> +++ b/sound/virtio/virtio_chmap.c
> @@ -0,0 +1,237 @@

[snip]

> +/**
> + * virtsnd_chmap_parse_cfg() - Parse the channel map configuration.
> + * @snd: VirtIO sound device.
> + *
> + * This function is called during initial device initialization.
> + *
> + * Context: Any context that permits to sleep.
> + * Return: 0 on success, -errno on failure.
> + */
> +int virtsnd_chmap_parse_cfg(struct virtio_snd *snd)
> +{
> +	struct virtio_device *vdev = snd->vdev;
> +	unsigned int i;
> +	int rc;
> +
> +	virtio_cread(vdev, struct virtio_snd_config, chmaps, &snd->nchmaps);
> +	if (!snd->nchmaps)
> +		return 0;
> +
> +	snd->chmaps = devm_kcalloc(&vdev->dev, snd->nchmaps,
> +				   sizeof(*snd->chmaps), GFP_KERNEL);
> +	if (!snd->chmaps)
> +		return -ENOMEM;
> +
> +	rc = virtsnd_ctl_query_info(snd, VIRTIO_SND_R_CHMAP_INFO, 0,
> +				    snd->nchmaps, sizeof(*snd->chmaps),
> +				    snd->chmaps);
> +	if (rc)
> +		return rc;
> +
> +	/* Count the number of channel maps per each PCM device/stream. */
> +	for (i = 0; i < snd->nchmaps; ++i) {
> +		struct virtio_snd_chmap_info *info = &snd->chmaps[i];
> +		unsigned int nid = le32_to_cpu(info->hdr.hda_fn_nid);
> +		struct virtio_pcm *pcm;
> +		struct virtio_pcm_stream *stream;
> +
> +		pcm = virtsnd_pcm_find_or_create(snd, nid);
> +		if (IS_ERR(pcm))
> +			return PTR_ERR(pcm);
> +
> +		switch (info->direction) {
> +		case VIRTIO_SND_D_OUTPUT: {
> +			stream = &pcm->streams[SNDRV_PCM_STREAM_PLAYBACK];
> +			break;
> +		}
> +		case VIRTIO_SND_D_INPUT: {
> +			stream = &pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
> +			break;
> +		}
> +		default: {
> +			dev_err(&vdev->dev,
> +				"chmap #%u: unknown direction (%u)\n", i,
> +				info->direction);
> +			return -EINVAL;
> +		}
> +		}
> +
> +		stream->nchmaps++;
> +	}
> +
> +	return 0;
> +}
> +
> +/**
> + * virtsnd_chmap_add_ctls() - Create an ALSA control for channel maps.
> + * @pcm: ALSA PCM device.
> + * @direction: PCM stream direction (SNDRV_PCM_STREAM_XXX).
> + * @stream: VirtIO PCM stream.
> + *
> + * Context: Any context.
> + * Return: 0 on success, -errno on failure.
> + */
> +static int virtsnd_chmap_add_ctls(struct snd_pcm *pcm, int direction,
> +				  struct virtio_pcm_stream *stream)
> +{
> +	unsigned int i;
> +	int max_channels = 0;
> +
> +	for (i = 0; i < stream->nchmaps; i++)
> +		if (max_channels < stream->chmaps[i].channels)
> +			max_channels = stream->chmaps[i].channels;
> +
> +	return snd_pcm_add_chmap_ctls(pcm, direction, stream->chmaps,
> +				      max_channels, 0, NULL);
> +}
> +
> +/**
> + * virtsnd_chmap_build_devs() - Build ALSA controls for channel maps.
> + * @snd: VirtIO sound device.
> + *
> + * Context: Any context.
> + * Return: 0 on success, -errno on failure.
> + */
> +int virtsnd_chmap_build_devs(struct virtio_snd *snd)
> +{
> +	struct virtio_device *vdev = snd->vdev;
> +	struct virtio_pcm *pcm;
> +	struct virtio_pcm_stream *stream;
> +	unsigned int i;
> +	int rc;
> +
> +	/* Allocate channel map elements per each PCM device/stream. */
> +	list_for_each_entry(pcm, &snd->pcm_list, list) {
> +		for (i = 0; i < ARRAY_SIZE(pcm->streams); ++i) {
> +			stream = &pcm->streams[i];
> +
> +			if (!stream->nchmaps)
> +				continue;
> +
> +			stream->chmaps = devm_kcalloc(&vdev->dev,
> +						      stream->nchmaps + 1,
> +						      sizeof(*stream->chmaps),
> +						      GFP_KERNEL);
> +			if (!stream->chmaps)
> +				return -ENOMEM;
> +
> +			stream->nchmaps = 0;
> +		}
> +	}
> +
> +	/* Initialize channel maps per each PCM device/stream. */
> +	for (i = 0; i < snd->nchmaps; ++i) {
> +		struct virtio_snd_chmap_info *info = &snd->chmaps[i];
> +		unsigned int nid = le32_to_cpu(info->hdr.hda_fn_nid);
> +		unsigned int channels = info->channels;
> +		unsigned int ch;
> +		struct snd_pcm_chmap_elem *chmap;
> +
> +		pcm = virtsnd_pcm_find(snd, nid);
> +		if (IS_ERR(pcm))
> +			return PTR_ERR(pcm);
> +
> +		if (info->direction == VIRTIO_SND_D_OUTPUT)
> +			stream = &pcm->streams[SNDRV_PCM_STREAM_PLAYBACK];
> +		else
> +			stream = &pcm->streams[SNDRV_PCM_STREAM_CAPTURE];
> +
> +		chmap = &stream->chmaps[stream->nchmaps++];
> +
> +		if (channels > ARRAY_SIZE(chmap->map))
> +			channels = ARRAY_SIZE(chmap->map);
> +
> +		chmap->channels = channels;
> +
> +		for (ch = 0; ch < channels; ++ch) {
> +			u8 position = info->positions[ch];
> +
> +			if (position >= ARRAY_SIZE(g_v2a_position_map))
> +				return -EINVAL;
> +
> +			chmap->map[ch] = g_v2a_position_map[position];
> +		}
> +	}

You enter this function after virtsnd_chmap_parse_cfg() has run. And 
virtsnd_chmap_parse_cfg() has already found or created all the PCMs and 
counted channel maps - the same way as you do in the above loop. Wouldn't 
it be enough to reuse the result of that counting and avoid re-counting 
here?

> +
> +	/* Create an ALSA control per each PCM device/stream. */
> +	list_for_each_entry(pcm, &snd->pcm_list, list) {
> +		if (!pcm->pcm)
> +			continue;
> +
> +		for (i = 0; i < ARRAY_SIZE(pcm->streams); ++i) {
> +			stream = &pcm->streams[i];
> +
> +			if (!stream->nchmaps)
> +				continue;
> +
> +			rc = virtsnd_chmap_add_ctls(pcm->pcm, i, stream);
> +			if (rc)
> +				return rc;
> +		}
> +	}
> +
> +	return 0;
> +}

  reply	other threads:[~2021-01-26 18:07 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210124165408.1122868-1-anton.yakovlev@opensynergy.com>
2021-01-24 16:54 ` [PATCH v2 1/9] uapi: virtio_ids: add a sound device type ID from OASIS spec Anton Yakovlev
2021-01-24 16:54 ` [PATCH v2 2/9] ALSA: virtio: add virtio sound driver Anton Yakovlev
2021-01-25 14:54   ` Guennadi Liakhovetski
2021-02-01 23:18     ` [virtio-dev] " Anton Yakovlev
2021-02-03 16:59       ` Takashi Iwai
2021-02-03 17:34         ` Anton Yakovlev
2021-02-03 17:39           ` Takashi Iwai
2021-01-24 16:54 ` [PATCH v2 3/9] ALSA: virtio: handling control messages Anton Yakovlev
2021-01-25 15:22   ` Guennadi Liakhovetski
2021-02-01 23:18     ` [virtio-dev] " Anton Yakovlev
2021-01-24 16:54 ` [PATCH v2 4/9] ALSA: virtio: build PCM devices and substream hardware descriptors Anton Yakovlev
2021-01-25 15:44   ` Guennadi Liakhovetski
2021-02-01 23:19     ` [virtio-dev] " Anton Yakovlev
2021-01-24 16:54 ` [PATCH v2 5/9] ALSA: virtio: handling control and I/O messages for the PCM device Anton Yakovlev
2021-01-25 16:25   ` Guennadi Liakhovetski
2021-02-01 23:20     ` Anton Yakovlev
2021-01-24 16:54 ` [PATCH v2 6/9] ALSA: virtio: PCM substream operators Anton Yakovlev
2021-01-25 16:59   ` Guennadi Liakhovetski
2021-01-26  7:25     ` Guennadi Liakhovetski
2021-02-01 23:21     ` Anton Yakovlev
2021-01-24 16:54 ` [PATCH v2 7/9] ALSA: virtio: introduce jack support Anton Yakovlev
2021-01-26  7:40   ` Guennadi Liakhovetski
2021-01-24 16:54 ` [PATCH v2 8/9] ALSA: virtio: introduce PCM channel map support Anton Yakovlev
2021-01-26  9:22   ` Guennadi Liakhovetski [this message]
2021-02-01 23:21     ` Anton Yakovlev
2021-01-24 16:54 ` [PATCH v2 9/9] ALSA: virtio: introduce device suspend/resume support Anton Yakovlev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=643248d4-d246-686f-34c3-7e592777e3ec@intel.com \
    --to=guennadi.liakhovetski@linux.intel.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=anton.yakovlev@opensynergy.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    --cc=virtio-dev@lists.oasis-open.org \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®