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