aboutsummaryrefslogtreecommitdiff
path: root/sound/virtio/virtio_chmap.c
blob: 5bc924933a59fb2cea6985b334fa177a583601a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-License-Identifier: GPL-2.0+
/*
 * virtio-snd: Virtio sound device
 * Copyright (C) 2021 OpenSynergy GmbH
 */
#include <linux/virtio_config.h>

#include "virtio_card.h"

/* VirtIO->ALSA channel position map */
static const u8 g_v2a_position_map[] = {
	[VIRTIO_SND_CHMAP_NONE] = SNDRV_CHMAP_UNKNOWN,
	[VIRTIO_SND_CHMAP_NA] = SNDRV_CHMAP_NA,
	[VIRTIO_SND_CHMAP_MONO] = SNDRV_CHMAP_MONO,
	[VIRTIO_SND_CHMAP_FL] = SNDRV_CHMAP_FL,
	[VIRTIO_SND_CHMAP_FR] = SNDRV_CHMAP_FR,
	[VIRTIO_SND_CHMAP_RL] = SNDRV_CHMAP_RL,
	[VIRTIO_SND_CHMAP_RR] = SNDRV_CHMAP_RR,
	[VIRTIO_SND_CHMAP_FC] = SNDRV_CHMAP_FC,
	[VIRTIO_SND_CHMAP_LFE] = SNDRV_CHMAP_LFE,
	[VIRTIO_SND_CHMAP_SL] = SND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_CHMAP_BLC] = SNDRV_CHMAP_BLC,
	[VIRTIO_SND_CHMAP_BRC] = SNDRV_CHMAP_BRC
};

/**
 * 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;
	u32 i;
	int rc;

	virtio_cread_le(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];
		u32 nid = le32_to_cpu(info->hdr.hda_fn_nid);
		struct virtio_pcm *vpcm;
		struct virtio_pcm_stream *vs;

		vpcm = virtsnd_pcm_find_or_create(snd, nid);
		if (IS_ERR(vpcm))
			return PTR_ERR(vpcm);

		switch (info->direction) {
		case VIRTIO_SND_D_OUTPUT:
			vs = &vpcm->streams[SNDRV_PCM_STREAM_PLAYBACK];
			break;
		case VIRTIO_SND_D_INPUT:
			vs = &vpcm->streams[SNDRV_PCM_STREAM_CAPTURE];
			break;
		default:
			dev_err(&vdev->dev,
				"chmap #%u: unknown direction (%u)\n", i,
				info->direction);
			return -EINVAL;
		}

		vs->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).
 * @vs: 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 *vs)
{
	u32 i;
	int max_channels = 0;

	for (i = 0; i < vs->nchmaps; i++)
		if (max_channels < vs->chmaps[i].channels)
			max_channels = vs->chmaps[i].channels;

	return snd_pcm_add_chmap_ctls(pcm, direction, vs->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 *vpcm;
	struct virtio_pcm_stream *vs;
	u32 i;
	int rc;

	/* Allocate channel map elements per each PCM device/stream. */
	list_for_each_entry(vpcm, &snd->pcm_list, list) {
		for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
			vs = &vpcm->streams[i];

			if (!vs->nchmaps)
				continue;

			vs->chmaps = devm_kcalloc(&vdev->dev, vs->nchmaps + 1,
						  sizeof(*vs->chmaps),
						  GFP_KERNEL);
			if (!vs->chmaps)
				return -ENOMEM;

			vs->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 channels = info->channels;
		unsigned int ch;
		struct snd_pcm_chmap_elem *chmap;

		vpcm = virtsnd_pcm_find(snd, le32_to_cpu(info->hdr.hda_fn_nid));
		if (IS_ERR(vpcm))
			return PTR_ERR(vpcm);

		if (info->direction == VIRTIO_SND_D_OUTPUT)
			vs = &vpcm->streams[SNDRV_PCM_STREAM_PLAYBACK];
		else
			vs = &vpcm->streams[SNDRV_PCM_STREAM_CAPTURE];

		chmap = &vs->chmaps[vs->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];
		}
	}

	/* Create an ALSA control per each PCM device/stream. */
	list_for_each_entry(vpcm, &snd->pcm_list, list) {
		if (!vpcm->pcm)
			continue;

		for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
			vs = &vpcm->streams[i];

			if (!vs->nchmaps)
				continue;

			rc = virtsnd_chmap_add_ctls(vpcm->pcm, i, vs);
			if (rc)
				return rc;
		}
	}

	return 0;
}