xref: /openbmc/linux/sound/soc/sof/control.c (revision 99ceec5ca0cb29e3b1d556d108ddc54654377792)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only 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"
16ee1e79b7SRanjani 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 {
2249c22696SKai-Heng Feng 	int temp = 0;
2349c22696SKai-Heng Feng 	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;
92ee1e79b7SRanjani 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 */
108ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
109ee1e79b7SRanjani 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;
139ee1e79b7SRanjani 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 */
157ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
158ee1e79b7SRanjani 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;
189ee1e79b7SRanjani 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 */
204ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
205ee1e79b7SRanjani 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;
220ee1e79b7SRanjani 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)) {
227ee1e79b7SRanjani 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) {
235ee1e79b7SRanjani 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;
255ee1e79b7SRanjani 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)) {
261ee1e79b7SRanjani 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) {
268ee1e79b7SRanjani 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 */
278ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
279ee1e79b7SRanjani 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;
295ee1e79b7SRanjani 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) {
311ee1e79b7SRanjani 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) {
318ee1e79b7SRanjani 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) {
328ee1e79b7SRanjani 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)) {
335ee1e79b7SRanjani 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) {
341ee1e79b7SRanjani 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 */
346ee1e79b7SRanjani Sridharan 	if (pm_runtime_active(scomp->dev))
347ee1e79b7SRanjani 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 
356783560d0SDharageswari R int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
357783560d0SDharageswari R 				   unsigned int size)
358783560d0SDharageswari R {
359783560d0SDharageswari R 	struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
360783560d0SDharageswari R 	struct snd_sof_control *scontrol = be->dobj.private;
361783560d0SDharageswari R 	struct snd_soc_component *scomp = scontrol->scomp;
362783560d0SDharageswari R 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
363783560d0SDharageswari R 	struct snd_ctl_tlv header;
364783560d0SDharageswari R 	struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
365783560d0SDharageswari R 	size_t data_size;
366783560d0SDharageswari R 	int ret;
367783560d0SDharageswari R 	int err;
368783560d0SDharageswari R 
369783560d0SDharageswari R 	ret = pm_runtime_get_sync(scomp->dev);
370*99ceec5cSPierre-Louis Bossart 	if (ret < 0 && ret != -EACCES) {
371783560d0SDharageswari R 		dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to resume %d\n", ret);
372783560d0SDharageswari R 		pm_runtime_put_noidle(scomp->dev);
373783560d0SDharageswari R 		return ret;
374783560d0SDharageswari R 	}
375783560d0SDharageswari R 
376783560d0SDharageswari R 	/* set the ABI header values */
377783560d0SDharageswari R 	cdata->data->magic = SOF_ABI_MAGIC;
378783560d0SDharageswari R 	cdata->data->abi = SOF_ABI_VERSION;
379783560d0SDharageswari R 	/* get all the component data from DSP */
380783560d0SDharageswari R 	ret = snd_sof_ipc_set_get_comp_data(scontrol, SOF_IPC_COMP_GET_DATA, SOF_CTRL_TYPE_DATA_GET,
381783560d0SDharageswari R 					    scontrol->cmd, false);
382783560d0SDharageswari R 	if (ret < 0)
383783560d0SDharageswari R 		goto out;
384783560d0SDharageswari R 
385783560d0SDharageswari R 	/* check data size doesn't exceed max coming from topology */
386783560d0SDharageswari R 	if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
387783560d0SDharageswari R 		dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %zu.\n",
388783560d0SDharageswari R 				    cdata->data->size,
389783560d0SDharageswari R 				    be->max - sizeof(const struct sof_abi_hdr));
390783560d0SDharageswari R 		ret = -EINVAL;
391783560d0SDharageswari R 		goto out;
392783560d0SDharageswari R 	}
393783560d0SDharageswari R 
394783560d0SDharageswari R 	data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
395783560d0SDharageswari R 
396783560d0SDharageswari R 	header.numid = scontrol->cmd;
397783560d0SDharageswari R 	header.length = data_size;
398783560d0SDharageswari R 	if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) {
399783560d0SDharageswari R 		ret = -EFAULT;
400783560d0SDharageswari R 		goto out;
401783560d0SDharageswari R 	}
402783560d0SDharageswari R 
403783560d0SDharageswari R 	if (copy_to_user(tlvd->tlv, cdata->data, data_size))
404783560d0SDharageswari R 		ret = -EFAULT;
405783560d0SDharageswari R out:
406783560d0SDharageswari R 	pm_runtime_mark_last_busy(scomp->dev);
407783560d0SDharageswari R 	err = pm_runtime_put_autosuspend(scomp->dev);
408783560d0SDharageswari R 	if (err < 0)
409783560d0SDharageswari R 		dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to idle %d\n", err);
410783560d0SDharageswari R 
411783560d0SDharageswari R 	return ret;
412783560d0SDharageswari R }
413783560d0SDharageswari R 
414c3078f53SLiam Girdwood int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
415c3078f53SLiam Girdwood 			  unsigned int __user *binary_data,
416c3078f53SLiam Girdwood 			  unsigned int size)
417c3078f53SLiam Girdwood {
418c3078f53SLiam Girdwood 	struct soc_bytes_ext *be =
419c3078f53SLiam Girdwood 		(struct soc_bytes_ext *)kcontrol->private_value;
420c3078f53SLiam Girdwood 	struct snd_sof_control *scontrol = be->dobj.private;
421ee1e79b7SRanjani Sridharan 	struct snd_soc_component *scomp = scontrol->scomp;
422c3078f53SLiam Girdwood 	struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
423c3078f53SLiam Girdwood 	struct snd_ctl_tlv header;
424c3078f53SLiam Girdwood 	struct snd_ctl_tlv __user *tlvd =
425c3078f53SLiam Girdwood 		(struct snd_ctl_tlv __user *)binary_data;
426c3078f53SLiam Girdwood 	int data_size;
4270c888babSBard Liao 	int ret = 0;
428c3078f53SLiam Girdwood 
429c3078f53SLiam Girdwood 	/*
430c3078f53SLiam Girdwood 	 * Decrement the limit by ext bytes header size to
431c3078f53SLiam Girdwood 	 * ensure the user space buffer is not exceeded.
432c3078f53SLiam Girdwood 	 */
433c3078f53SLiam Girdwood 	size -= sizeof(const struct snd_ctl_tlv);
434c3078f53SLiam Girdwood 
435c3078f53SLiam Girdwood 	/* set the ABI header values */
436c3078f53SLiam Girdwood 	cdata->data->magic = SOF_ABI_MAGIC;
437c3078f53SLiam Girdwood 	cdata->data->abi = SOF_ABI_VERSION;
438c3078f53SLiam Girdwood 
439c3078f53SLiam Girdwood 	/* Prevent read of other kernel data or possibly corrupt response */
440c3078f53SLiam Girdwood 	data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
441c3078f53SLiam Girdwood 
442c3078f53SLiam Girdwood 	/* check data size doesn't exceed max coming from topology */
443c3078f53SLiam Girdwood 	if (data_size > be->max) {
444ee1e79b7SRanjani Sridharan 		dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %d.\n",
445c3078f53SLiam Girdwood 				    data_size, be->max);
446c3078f53SLiam Girdwood 		ret = -EINVAL;
447c3078f53SLiam Girdwood 		goto out;
448c3078f53SLiam Girdwood 	}
449c3078f53SLiam Girdwood 
450c3078f53SLiam Girdwood 	header.numid = scontrol->cmd;
451c3078f53SLiam Girdwood 	header.length = data_size;
452c3078f53SLiam Girdwood 	if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) {
453c3078f53SLiam Girdwood 		ret = -EFAULT;
454c3078f53SLiam Girdwood 		goto out;
455c3078f53SLiam Girdwood 	}
456c3078f53SLiam Girdwood 
457c3078f53SLiam Girdwood 	if (copy_to_user(tlvd->tlv, cdata->data, data_size))
458c3078f53SLiam Girdwood 		ret = -EFAULT;
459c3078f53SLiam Girdwood 
460c3078f53SLiam Girdwood out:
461c3078f53SLiam Girdwood 	return ret;
462c3078f53SLiam Girdwood }
463