xref: /openbmc/linux/sound/soc/sof/control.c (revision ee1e79b72e3cf5eac42ba9de827536f91d4c04e2)
1c3078f53SLiam Girdwood // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2c3078f53SLiam Girdwood //
3c3078f53SLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
4c3078f53SLiam Girdwood // redistributing this file, you may do so under either license.
5c3078f53SLiam Girdwood //
6c3078f53SLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7c3078f53SLiam Girdwood //
8c3078f53SLiam Girdwood // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9c3078f53SLiam Girdwood //
10c3078f53SLiam Girdwood 
11c3078f53SLiam Girdwood /* Mixer Controls */
12c3078f53SLiam Girdwood 
13c3078f53SLiam Girdwood #include <linux/pm_runtime.h>
145d43001aSJaska Uimonen #include <linux/leds.h>
15c3078f53SLiam Girdwood #include "sof-priv.h"
16*ee1e79b7SRanjani Sridharan #include "sof-audio.h"
17c3078f53SLiam Girdwood 
185d43001aSJaska Uimonen static void update_mute_led(struct snd_sof_control *scontrol,
195d43001aSJaska Uimonen 			    struct snd_kcontrol *kcontrol,
205d43001aSJaska Uimonen 			    struct snd_ctl_elem_value *ucontrol)
215d43001aSJaska Uimonen {
225d43001aSJaska Uimonen 	unsigned int temp = 0;
235d43001aSJaska Uimonen 	unsigned int mask;
245d43001aSJaska Uimonen 	int i;
255d43001aSJaska Uimonen 
265d43001aSJaska Uimonen 	mask = 1U << snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
275d43001aSJaska Uimonen 
285d43001aSJaska Uimonen 	for (i = 0; i < scontrol->num_channels; i++) {
295d43001aSJaska Uimonen 		if (ucontrol->value.integer.value[i]) {
305d43001aSJaska Uimonen 			temp |= mask;
315d43001aSJaska Uimonen 			break;
325d43001aSJaska Uimonen 		}
335d43001aSJaska Uimonen 	}
345d43001aSJaska Uimonen 
355d43001aSJaska Uimonen 	if (temp == scontrol->led_ctl.led_value)
365d43001aSJaska Uimonen 		return;
375d43001aSJaska Uimonen 
385d43001aSJaska Uimonen 	scontrol->led_ctl.led_value = temp;
395d43001aSJaska Uimonen 
409899a7a8SYueHaibing #if IS_REACHABLE(CONFIG_LEDS_TRIGGER_AUDIO)
415d43001aSJaska Uimonen 	if (!scontrol->led_ctl.direction)
425d43001aSJaska Uimonen 		ledtrig_audio_set(LED_AUDIO_MUTE, temp ? LED_OFF : LED_ON);
435d43001aSJaska Uimonen 	else
445d43001aSJaska Uimonen 		ledtrig_audio_set(LED_AUDIO_MICMUTE, temp ? LED_OFF : LED_ON);
459899a7a8SYueHaibing #endif
465d43001aSJaska Uimonen }
475d43001aSJaska Uimonen 
48c3078f53SLiam Girdwood static inline u32 mixer_to_ipc(unsigned int value, u32 *volume_map, int size)
49c3078f53SLiam Girdwood {
50c3078f53SLiam Girdwood 	if (value >= size)
51c3078f53SLiam Girdwood 		return volume_map[size - 1];
52c3078f53SLiam Girdwood 
53c3078f53SLiam Girdwood 	return volume_map[value];
54c3078f53SLiam Girdwood }
55c3078f53SLiam Girdwood 
56c3078f53SLiam Girdwood static inline u32 ipc_to_mixer(u32 value, u32 *volume_map, int size)
57c3078f53SLiam Girdwood {
58c3078f53SLiam Girdwood 	int i;
59c3078f53SLiam Girdwood 
60c3078f53SLiam Girdwood 	for (i = 0; i < size; i++) {
61c3078f53SLiam Girdwood 		if (volume_map[i] >= value)
62c3078f53SLiam Girdwood 			return i;
63c3078f53SLiam Girdwood 	}
64c3078f53SLiam Girdwood 
65c3078f53SLiam Girdwood 	return i - 1;
66c3078f53SLiam Girdwood }
67c3078f53SLiam Girdwood 
68c3078f53SLiam Girdwood int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
69c3078f53SLiam Girdwood 		       struct snd_ctl_elem_value *ucontrol)
70c3078f53SLiam Girdwood {
71c3078f53SLiam Girdwood 	struct soc_mixer_control *sm =
72c3078f53SLiam Girdwood 		(struct soc_mixer_control *)kcontrol->private_value;
73c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = sm->dobj.private;
74c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
75c3078f53SLiam Girdwood 	unsigned int i, channels = scontrol->num_channels;
76c3078f53SLiam Girdwood 
77c3078f53SLiam Girdwood 	/* read back each channel */
78c3078f53SLiam Girdwood 	for (i = 0; i < channels; i++)
79c3078f53SLiam Girdwood 		ucontrol->value.integer.value[i] =
80c3078f53SLiam Girdwood 			ipc_to_mixer(cdata->chanv[i].value,
81c3078f53SLiam Girdwood 				     scontrol->volume_table, sm->max + 1);
82c3078f53SLiam Girdwood 
83c3078f53SLiam Girdwood 	return 0;
84c3078f53SLiam Girdwood }
85c3078f53SLiam Girdwood 
86c3078f53SLiam Girdwood int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
87c3078f53SLiam Girdwood 		       struct snd_ctl_elem_value *ucontrol)
88c3078f53SLiam Girdwood {
89c3078f53SLiam Girdwood 	struct soc_mixer_control *sm =
90c3078f53SLiam Girdwood 		(struct soc_mixer_control *)kcontrol->private_value;
91c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = sm->dobj.private;
92*ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
93c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
94c3078f53SLiam Girdwood 	unsigned int i, channels = scontrol->num_channels;
9595a32c98SDragos Tarcatu 	bool change = false;
9695a32c98SDragos Tarcatu 	u32 value;
97c3078f53SLiam Girdwood 
98c3078f53SLiam Girdwood 	/* update each channel */
99c3078f53SLiam Girdwood 	for (i = 0; i < channels; i++) {
10095a32c98SDragos Tarcatu 		value = mixer_to_ipc(ucontrol->value.integer.value[i],
101c3078f53SLiam Girdwood 				     scontrol->volume_table, sm->max + 1);
10295a32c98SDragos Tarcatu 		change = change || (value != cdata->chanv[i].value);
103c3078f53SLiam Girdwood 		cdata->chanv[i].channel = i;
10495a32c98SDragos Tarcatu 		cdata->chanv[i].value = value;
105c3078f53SLiam Girdwood 	}
106c3078f53SLiam Girdwood 
107c3078f53SLiam Girdwood 	/* notify DSP of mixer updates */
108*ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
109*ee1e79b7SRanjani Sridharan 		snd_sof_ipc_set_get_comp_data(scontrol,
110c3078f53SLiam Girdwood 					      SOF_IPC_COMP_SET_VALUE,
111c3078f53SLiam Girdwood 					      SOF_CTRL_TYPE_VALUE_CHAN_GET,
112c3078f53SLiam Girdwood 					      SOF_CTRL_CMD_VOLUME,
113c3078f53SLiam Girdwood 					      true);
11495a32c98SDragos Tarcatu 	return change;
115c3078f53SLiam Girdwood }
116c3078f53SLiam Girdwood 
117c3078f53SLiam Girdwood int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
118c3078f53SLiam Girdwood 		       struct snd_ctl_elem_value *ucontrol)
119c3078f53SLiam Girdwood {
120c3078f53SLiam Girdwood 	struct soc_mixer_control *sm =
121c3078f53SLiam Girdwood 		(struct soc_mixer_control *)kcontrol->private_value;
122c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = sm->dobj.private;
123c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
124c3078f53SLiam Girdwood 	unsigned int i, channels = scontrol->num_channels;
125c3078f53SLiam Girdwood 
126c3078f53SLiam Girdwood 	/* read back each channel */
127c3078f53SLiam Girdwood 	for (i = 0; i < channels; i++)
128c3078f53SLiam Girdwood 		ucontrol->value.integer.value[i] = cdata->chanv[i].value;
129c3078f53SLiam Girdwood 
130c3078f53SLiam Girdwood 	return 0;
131c3078f53SLiam Girdwood }
132c3078f53SLiam Girdwood 
133c3078f53SLiam Girdwood int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
134c3078f53SLiam Girdwood 		       struct snd_ctl_elem_value *ucontrol)
135c3078f53SLiam Girdwood {
136c3078f53SLiam Girdwood 	struct soc_mixer_control *sm =
137c3078f53SLiam Girdwood 		(struct soc_mixer_control *)kcontrol->private_value;
138c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = sm->dobj.private;
139*ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
140c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
141c3078f53SLiam Girdwood 	unsigned int i, channels = scontrol->num_channels;
14295a32c98SDragos Tarcatu 	bool change = false;
14395a32c98SDragos Tarcatu 	u32 value;
144c3078f53SLiam Girdwood 
145c3078f53SLiam Girdwood 	/* update each channel */
146c3078f53SLiam Girdwood 	for (i = 0; i < channels; i++) {
14795a32c98SDragos Tarcatu 		value = ucontrol->value.integer.value[i];
14895a32c98SDragos Tarcatu 		change = change || (value != cdata->chanv[i].value);
149c3078f53SLiam Girdwood 		cdata->chanv[i].channel = i;
15095a32c98SDragos Tarcatu 		cdata->chanv[i].value = value;
151c3078f53SLiam Girdwood 	}
152c3078f53SLiam Girdwood 
1535d43001aSJaska Uimonen 	if (scontrol->led_ctl.use_led)
1545d43001aSJaska Uimonen 		update_mute_led(scontrol, kcontrol, ucontrol);
1555d43001aSJaska Uimonen 
156c3078f53SLiam Girdwood 	/* notify DSP of mixer updates */
157*ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
158*ee1e79b7SRanjani Sridharan 		snd_sof_ipc_set_get_comp_data(scontrol,
159c3078f53SLiam Girdwood 					      SOF_IPC_COMP_SET_VALUE,
160c3078f53SLiam Girdwood 					      SOF_CTRL_TYPE_VALUE_CHAN_GET,
161c3078f53SLiam Girdwood 					      SOF_CTRL_CMD_SWITCH,
162c3078f53SLiam Girdwood 					      true);
163c3078f53SLiam Girdwood 
16495a32c98SDragos Tarcatu 	return change;
165c3078f53SLiam Girdwood }
166c3078f53SLiam Girdwood 
167c3078f53SLiam Girdwood int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
168c3078f53SLiam Girdwood 		     struct snd_ctl_elem_value *ucontrol)
169c3078f53SLiam Girdwood {
170c3078f53SLiam Girdwood 	struct soc_enum *se =
171c3078f53SLiam Girdwood 		(struct soc_enum *)kcontrol->private_value;
172c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = se->dobj.private;
173c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
174c3078f53SLiam Girdwood 	unsigned int i, channels = scontrol->num_channels;
175c3078f53SLiam Girdwood 
176c3078f53SLiam Girdwood 	/* read back each channel */
177c3078f53SLiam Girdwood 	for (i = 0; i < channels; i++)
178c3078f53SLiam Girdwood 		ucontrol->value.enumerated.item[i] = cdata->chanv[i].value;
179c3078f53SLiam Girdwood 
180c3078f53SLiam Girdwood 	return 0;
181c3078f53SLiam Girdwood }
182c3078f53SLiam Girdwood 
183c3078f53SLiam Girdwood int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
184c3078f53SLiam Girdwood 		     struct snd_ctl_elem_value *ucontrol)
185c3078f53SLiam Girdwood {
186c3078f53SLiam Girdwood 	struct soc_enum *se =
187c3078f53SLiam Girdwood 		(struct soc_enum *)kcontrol->private_value;
188c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = se->dobj.private;
189*ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
190c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
191c3078f53SLiam Girdwood 	unsigned int i, channels = scontrol->num_channels;
19295a32c98SDragos Tarcatu 	bool change = false;
19395a32c98SDragos Tarcatu 	u32 value;
194c3078f53SLiam Girdwood 
195c3078f53SLiam Girdwood 	/* update each channel */
196c3078f53SLiam Girdwood 	for (i = 0; i < channels; i++) {
19795a32c98SDragos Tarcatu 		value = ucontrol->value.enumerated.item[i];
19895a32c98SDragos Tarcatu 		change = change || (value != cdata->chanv[i].value);
199c3078f53SLiam Girdwood 		cdata->chanv[i].channel = i;
20095a32c98SDragos Tarcatu 		cdata->chanv[i].value = value;
201c3078f53SLiam Girdwood 	}
202c3078f53SLiam Girdwood 
203c3078f53SLiam Girdwood 	/* notify DSP of enum updates */
204*ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
205*ee1e79b7SRanjani Sridharan 		snd_sof_ipc_set_get_comp_data(scontrol,
206c3078f53SLiam Girdwood 					      SOF_IPC_COMP_SET_VALUE,
207c3078f53SLiam Girdwood 					      SOF_CTRL_TYPE_VALUE_CHAN_GET,
208c3078f53SLiam Girdwood 					      SOF_CTRL_CMD_ENUM,
209c3078f53SLiam Girdwood 					      true);
210c3078f53SLiam Girdwood 
21195a32c98SDragos Tarcatu 	return change;
212c3078f53SLiam Girdwood }
213c3078f53SLiam Girdwood 
214c3078f53SLiam Girdwood int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
215c3078f53SLiam Girdwood 		      struct snd_ctl_elem_value *ucontrol)
216c3078f53SLiam Girdwood {
217c3078f53SLiam Girdwood 	struct soc_bytes_ext *be =
218c3078f53SLiam Girdwood 		(struct soc_bytes_ext *)kcontrol->private_value;
219c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = be->dobj.private;
220*ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
221c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
222c3078f53SLiam Girdwood 	struct sof_abi_hdr *data = cdata->data;
223c3078f53SLiam Girdwood 	size_t size;
2240c888babSBard Liao 	int ret = 0;
225c3078f53SLiam Girdwood 
226c3078f53SLiam Girdwood 	if (be->max > sizeof(ucontrol->value.bytes.data)) {
227*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev,
228c3078f53SLiam Girdwood 				    "error: data max %d exceeds ucontrol data array size\n",
229c3078f53SLiam Girdwood 				    be->max);
230c3078f53SLiam Girdwood 		return -EINVAL;
231c3078f53SLiam Girdwood 	}
232c3078f53SLiam Girdwood 
233c3078f53SLiam Girdwood 	size = data->size + sizeof(*data);
234c3078f53SLiam Girdwood 	if (size > be->max) {
235*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev,
236c3078f53SLiam Girdwood 				    "error: DSP sent %zu bytes max is %d\n",
237c3078f53SLiam Girdwood 				    size, be->max);
238c3078f53SLiam Girdwood 		ret = -EINVAL;
239c3078f53SLiam Girdwood 		goto out;
240c3078f53SLiam Girdwood 	}
241c3078f53SLiam Girdwood 
242c3078f53SLiam Girdwood 	/* copy back to kcontrol */
243c3078f53SLiam Girdwood 	memcpy(ucontrol->value.bytes.data, data, size);
244c3078f53SLiam Girdwood 
245c3078f53SLiam Girdwood out:
246c3078f53SLiam Girdwood 	return ret;
247c3078f53SLiam Girdwood }
248c3078f53SLiam Girdwood 
249c3078f53SLiam Girdwood int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
250c3078f53SLiam Girdwood 		      struct snd_ctl_elem_value *ucontrol)
251c3078f53SLiam Girdwood {
252c3078f53SLiam Girdwood 	struct soc_bytes_ext *be =
253c3078f53SLiam Girdwood 		(struct soc_bytes_ext *)kcontrol->private_value;
254c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = be->dobj.private;
255*ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
256c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
257c3078f53SLiam Girdwood 	struct sof_abi_hdr *data = cdata->data;
2585661ad94SKeyon Jie 	size_t size = data->size + sizeof(*data);
259c3078f53SLiam Girdwood 
260c3078f53SLiam Girdwood 	if (be->max > sizeof(ucontrol->value.bytes.data)) {
261*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev,
262c3078f53SLiam Girdwood 				    "error: data max %d exceeds ucontrol data array size\n",
263c3078f53SLiam Girdwood 				    be->max);
264c3078f53SLiam Girdwood 		return -EINVAL;
265c3078f53SLiam Girdwood 	}
266c3078f53SLiam Girdwood 
2675661ad94SKeyon Jie 	if (size > be->max) {
268*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev,
2695661ad94SKeyon Jie 				    "error: size too big %zu bytes max is %d\n",
2705661ad94SKeyon Jie 				    size, be->max);
271c3078f53SLiam Girdwood 		return -EINVAL;
272c3078f53SLiam Girdwood 	}
273c3078f53SLiam Girdwood 
274c3078f53SLiam Girdwood 	/* copy from kcontrol */
2755661ad94SKeyon Jie 	memcpy(data, ucontrol->value.bytes.data, size);
276c3078f53SLiam Girdwood 
277c3078f53SLiam Girdwood 	/* notify DSP of byte control updates */
278*ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
279*ee1e79b7SRanjani Sridharan 		snd_sof_ipc_set_get_comp_data(scontrol,
280c3078f53SLiam Girdwood 					      SOF_IPC_COMP_SET_DATA,
281c3078f53SLiam Girdwood 					      SOF_CTRL_TYPE_DATA_SET,
282c3078f53SLiam Girdwood 					      scontrol->cmd,
283c3078f53SLiam Girdwood 					      true);
284c3078f53SLiam Girdwood 
2850c888babSBard Liao 	return 0;
286c3078f53SLiam Girdwood }
287c3078f53SLiam Girdwood 
288c3078f53SLiam Girdwood int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
289c3078f53SLiam Girdwood 			  const unsigned int __user *binary_data,
290c3078f53SLiam Girdwood 			  unsigned int size)
291c3078f53SLiam Girdwood {
292c3078f53SLiam Girdwood 	struct soc_bytes_ext *be =
293c3078f53SLiam Girdwood 		(struct soc_bytes_ext *)kcontrol->private_value;
294c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = be->dobj.private;
295*ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
296c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
297c3078f53SLiam Girdwood 	struct snd_ctl_tlv header;
298c3078f53SLiam Girdwood 	const struct snd_ctl_tlv __user *tlvd =
299c3078f53SLiam Girdwood 		(const struct snd_ctl_tlv __user *)binary_data;
300c3078f53SLiam Girdwood 
301c3078f53SLiam Girdwood 	/*
302c3078f53SLiam Girdwood 	 * The beginning of bytes data contains a header from where
303c3078f53SLiam Girdwood 	 * the length (as bytes) is needed to know the correct copy
304c3078f53SLiam Girdwood 	 * length of data from tlvd->tlv.
305c3078f53SLiam Girdwood 	 */
306c3078f53SLiam Girdwood 	if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv)))
307c3078f53SLiam Girdwood 		return -EFAULT;
308c3078f53SLiam Girdwood 
309c3078f53SLiam Girdwood 	/* be->max is coming from topology */
310c3078f53SLiam Girdwood 	if (header.length > be->max) {
311*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev, "error: Bytes data size %d exceeds max %d.\n",
312c3078f53SLiam Girdwood 				    header.length, be->max);
313c3078f53SLiam Girdwood 		return -EINVAL;
314c3078f53SLiam Girdwood 	}
315c3078f53SLiam Girdwood 
316c3078f53SLiam Girdwood 	/* Check that header id matches the command */
317c3078f53SLiam Girdwood 	if (header.numid != scontrol->cmd) {
318*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev,
319c3078f53SLiam Girdwood 				    "error: incorrect numid %d\n",
320c3078f53SLiam Girdwood 				    header.numid);
321c3078f53SLiam Girdwood 		return -EINVAL;
322c3078f53SLiam Girdwood 	}
323c3078f53SLiam Girdwood 
324c3078f53SLiam Girdwood 	if (copy_from_user(cdata->data, tlvd->tlv, header.length))
325c3078f53SLiam Girdwood 		return -EFAULT;
326c3078f53SLiam Girdwood 
327c3078f53SLiam Girdwood 	if (cdata->data->magic != SOF_ABI_MAGIC) {
328*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev,
329c3078f53SLiam Girdwood 				    "error: Wrong ABI magic 0x%08x.\n",
330c3078f53SLiam Girdwood 				    cdata->data->magic);
331c3078f53SLiam Girdwood 		return -EINVAL;
332c3078f53SLiam Girdwood 	}
333c3078f53SLiam Girdwood 
334c3078f53SLiam Girdwood 	if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
335*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev, "error: Incompatible ABI version 0x%08x.\n",
336c3078f53SLiam Girdwood 				    cdata->data->abi);
337c3078f53SLiam Girdwood 		return -EINVAL;
338c3078f53SLiam Girdwood 	}
339c3078f53SLiam Girdwood 
340c3078f53SLiam Girdwood 	if (cdata->data->size + sizeof(const struct sof_abi_hdr) > be->max) {
341*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev, "error: Mismatch in ABI data size (truncated?).\n");
342c3078f53SLiam Girdwood 		return -EINVAL;
343c3078f53SLiam Girdwood 	}
344c3078f53SLiam Girdwood 
345c3078f53SLiam Girdwood 	/* notify DSP of byte control updates */
346*ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
347*ee1e79b7SRanjani Sridharan 		snd_sof_ipc_set_get_comp_data(scontrol,
348c3078f53SLiam Girdwood 					      SOF_IPC_COMP_SET_DATA,
349c3078f53SLiam Girdwood 					      SOF_CTRL_TYPE_DATA_SET,
350c3078f53SLiam Girdwood 					      scontrol->cmd,
351c3078f53SLiam Girdwood 					      true);
352c3078f53SLiam Girdwood 
3530c888babSBard Liao 	return 0;
354c3078f53SLiam Girdwood }
355c3078f53SLiam Girdwood 
356c3078f53SLiam Girdwood int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
357c3078f53SLiam Girdwood 			  unsigned int __user *binary_data,
358c3078f53SLiam Girdwood 			  unsigned int size)
359c3078f53SLiam Girdwood {
360c3078f53SLiam Girdwood 	struct soc_bytes_ext *be =
361c3078f53SLiam Girdwood 		(struct soc_bytes_ext *)kcontrol->private_value;
362c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = be->dobj.private;
363*ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
364c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
365c3078f53SLiam Girdwood 	struct snd_ctl_tlv header;
366c3078f53SLiam Girdwood 	struct snd_ctl_tlv __user *tlvd =
367c3078f53SLiam Girdwood 		(struct snd_ctl_tlv __user *)binary_data;
368c3078f53SLiam Girdwood 	int data_size;
3690c888babSBard Liao 	int ret = 0;
370c3078f53SLiam Girdwood 
371c3078f53SLiam Girdwood 	/*
372c3078f53SLiam Girdwood 	 * Decrement the limit by ext bytes header size to
373c3078f53SLiam Girdwood 	 * ensure the user space buffer is not exceeded.
374c3078f53SLiam Girdwood 	 */
375c3078f53SLiam Girdwood 	size -= sizeof(const struct snd_ctl_tlv);
376c3078f53SLiam Girdwood 
377c3078f53SLiam Girdwood 	/* set the ABI header values */
378c3078f53SLiam Girdwood 	cdata->data->magic = SOF_ABI_MAGIC;
379c3078f53SLiam Girdwood 	cdata->data->abi = SOF_ABI_VERSION;
380c3078f53SLiam Girdwood 
381c3078f53SLiam Girdwood 	/* Prevent read of other kernel data or possibly corrupt response */
382c3078f53SLiam Girdwood 	data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
383c3078f53SLiam Girdwood 
384c3078f53SLiam Girdwood 	/* check data size doesn't exceed max coming from topology */
385c3078f53SLiam Girdwood 	if (data_size > be->max) {
386*ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %d.\n",
387c3078f53SLiam Girdwood 				    data_size, be->max);
388c3078f53SLiam Girdwood 		ret = -EINVAL;
389c3078f53SLiam Girdwood 		goto out;
390c3078f53SLiam Girdwood 	}
391c3078f53SLiam Girdwood 
392c3078f53SLiam Girdwood 	header.numid = scontrol->cmd;
393c3078f53SLiam Girdwood 	header.length = data_size;
394c3078f53SLiam Girdwood 	if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) {
395c3078f53SLiam Girdwood 		ret = -EFAULT;
396c3078f53SLiam Girdwood 		goto out;
397c3078f53SLiam Girdwood 	}
398c3078f53SLiam Girdwood 
399c3078f53SLiam Girdwood 	if (copy_to_user(tlvd->tlv, cdata->data, data_size))
400c3078f53SLiam Girdwood 		ret = -EFAULT;
401c3078f53SLiam Girdwood 
402c3078f53SLiam Girdwood out:
403c3078f53SLiam Girdwood 	return ret;
404c3078f53SLiam Girdwood }
405