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; 224c3078f53SLiam Girdwood 225c3078f53SLiam Girdwood if (be->max > sizeof(ucontrol->value.bytes.data)) { 226ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, 227c3078f53SLiam Girdwood "error: data max %d exceeds ucontrol data array size\n", 228c3078f53SLiam Girdwood be->max); 229c3078f53SLiam Girdwood return -EINVAL; 230c3078f53SLiam Girdwood } 231c3078f53SLiam Girdwood 232c3078f53SLiam Girdwood size = data->size + sizeof(*data); 233c3078f53SLiam Girdwood if (size > be->max) { 234ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, 235c3078f53SLiam Girdwood "error: DSP sent %zu bytes max is %d\n", 236c3078f53SLiam Girdwood size, be->max); 237*b9f8e138SGuennadi Liakhovetski return -EINVAL; 238c3078f53SLiam Girdwood } 239c3078f53SLiam Girdwood 240c3078f53SLiam Girdwood /* copy back to kcontrol */ 241c3078f53SLiam Girdwood memcpy(ucontrol->value.bytes.data, data, size); 242c3078f53SLiam Girdwood 243*b9f8e138SGuennadi Liakhovetski return 0; 244c3078f53SLiam Girdwood } 245c3078f53SLiam Girdwood 246c3078f53SLiam Girdwood int snd_sof_bytes_put(struct snd_kcontrol *kcontrol, 247c3078f53SLiam Girdwood struct snd_ctl_elem_value *ucontrol) 248c3078f53SLiam Girdwood { 249c3078f53SLiam Girdwood struct soc_bytes_ext *be = 250c3078f53SLiam Girdwood (struct soc_bytes_ext *)kcontrol->private_value; 251c3078f53SLiam Girdwood struct snd_sof_control *scontrol = be->dobj.private; 252ee1e79b7SRanjani Sridharan struct snd_soc_component *scomp = scontrol->scomp; 253c3078f53SLiam Girdwood struct sof_ipc_ctrl_data *cdata = scontrol->control_data; 254c3078f53SLiam Girdwood struct sof_abi_hdr *data = cdata->data; 2555661ad94SKeyon Jie size_t size = data->size + sizeof(*data); 256c3078f53SLiam Girdwood 257c3078f53SLiam Girdwood if (be->max > sizeof(ucontrol->value.bytes.data)) { 258ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, 259c3078f53SLiam Girdwood "error: data max %d exceeds ucontrol data array size\n", 260c3078f53SLiam Girdwood be->max); 261c3078f53SLiam Girdwood return -EINVAL; 262c3078f53SLiam Girdwood } 263c3078f53SLiam Girdwood 2645661ad94SKeyon Jie if (size > be->max) { 265ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, 2665661ad94SKeyon Jie "error: size too big %zu bytes max is %d\n", 2675661ad94SKeyon Jie size, be->max); 268c3078f53SLiam Girdwood return -EINVAL; 269c3078f53SLiam Girdwood } 270c3078f53SLiam Girdwood 271c3078f53SLiam Girdwood /* copy from kcontrol */ 2725661ad94SKeyon Jie memcpy(data, ucontrol->value.bytes.data, size); 273c3078f53SLiam Girdwood 274c3078f53SLiam Girdwood /* notify DSP of byte control updates */ 275ee1e79b7SRanjani Sridharan if (pm_runtime_active(scomp->dev)) 276ee1e79b7SRanjani Sridharan snd_sof_ipc_set_get_comp_data(scontrol, 277c3078f53SLiam Girdwood SOF_IPC_COMP_SET_DATA, 278c3078f53SLiam Girdwood SOF_CTRL_TYPE_DATA_SET, 279c3078f53SLiam Girdwood scontrol->cmd, 280c3078f53SLiam Girdwood true); 281c3078f53SLiam Girdwood 2820c888babSBard Liao return 0; 283c3078f53SLiam Girdwood } 284c3078f53SLiam Girdwood 285c3078f53SLiam Girdwood int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol, 286c3078f53SLiam Girdwood const unsigned int __user *binary_data, 287c3078f53SLiam Girdwood unsigned int size) 288c3078f53SLiam Girdwood { 289c3078f53SLiam Girdwood struct soc_bytes_ext *be = 290c3078f53SLiam Girdwood (struct soc_bytes_ext *)kcontrol->private_value; 291c3078f53SLiam Girdwood struct snd_sof_control *scontrol = be->dobj.private; 292ee1e79b7SRanjani Sridharan struct snd_soc_component *scomp = scontrol->scomp; 293c3078f53SLiam Girdwood struct sof_ipc_ctrl_data *cdata = scontrol->control_data; 294c3078f53SLiam Girdwood struct snd_ctl_tlv header; 295c3078f53SLiam Girdwood const struct snd_ctl_tlv __user *tlvd = 296c3078f53SLiam Girdwood (const struct snd_ctl_tlv __user *)binary_data; 297c3078f53SLiam Girdwood 298c3078f53SLiam Girdwood /* 299c3078f53SLiam Girdwood * The beginning of bytes data contains a header from where 300c3078f53SLiam Girdwood * the length (as bytes) is needed to know the correct copy 301c3078f53SLiam Girdwood * length of data from tlvd->tlv. 302c3078f53SLiam Girdwood */ 303c3078f53SLiam Girdwood if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv))) 304c3078f53SLiam Girdwood return -EFAULT; 305c3078f53SLiam Girdwood 306c3078f53SLiam Girdwood /* be->max is coming from topology */ 307c3078f53SLiam Girdwood if (header.length > be->max) { 308ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, "error: Bytes data size %d exceeds max %d.\n", 309c3078f53SLiam Girdwood header.length, be->max); 310c3078f53SLiam Girdwood return -EINVAL; 311c3078f53SLiam Girdwood } 312c3078f53SLiam Girdwood 313c3078f53SLiam Girdwood /* Check that header id matches the command */ 314c3078f53SLiam Girdwood if (header.numid != scontrol->cmd) { 315ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, 316c3078f53SLiam Girdwood "error: incorrect numid %d\n", 317c3078f53SLiam Girdwood header.numid); 318c3078f53SLiam Girdwood return -EINVAL; 319c3078f53SLiam Girdwood } 320c3078f53SLiam Girdwood 321c3078f53SLiam Girdwood if (copy_from_user(cdata->data, tlvd->tlv, header.length)) 322c3078f53SLiam Girdwood return -EFAULT; 323c3078f53SLiam Girdwood 324c3078f53SLiam Girdwood if (cdata->data->magic != SOF_ABI_MAGIC) { 325ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, 326c3078f53SLiam Girdwood "error: Wrong ABI magic 0x%08x.\n", 327c3078f53SLiam Girdwood cdata->data->magic); 328c3078f53SLiam Girdwood return -EINVAL; 329c3078f53SLiam Girdwood } 330c3078f53SLiam Girdwood 331c3078f53SLiam Girdwood if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) { 332ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, "error: Incompatible ABI version 0x%08x.\n", 333c3078f53SLiam Girdwood cdata->data->abi); 334c3078f53SLiam Girdwood return -EINVAL; 335c3078f53SLiam Girdwood } 336c3078f53SLiam Girdwood 337c3078f53SLiam Girdwood if (cdata->data->size + sizeof(const struct sof_abi_hdr) > be->max) { 338ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, "error: Mismatch in ABI data size (truncated?).\n"); 339c3078f53SLiam Girdwood return -EINVAL; 340c3078f53SLiam Girdwood } 341c3078f53SLiam Girdwood 342c3078f53SLiam Girdwood /* notify DSP of byte control updates */ 343ee1e79b7SRanjani Sridharan if (pm_runtime_active(scomp->dev)) 344ee1e79b7SRanjani Sridharan snd_sof_ipc_set_get_comp_data(scontrol, 345c3078f53SLiam Girdwood SOF_IPC_COMP_SET_DATA, 346c3078f53SLiam Girdwood SOF_CTRL_TYPE_DATA_SET, 347c3078f53SLiam Girdwood scontrol->cmd, 348c3078f53SLiam Girdwood true); 349c3078f53SLiam Girdwood 3500c888babSBard Liao return 0; 351c3078f53SLiam Girdwood } 352c3078f53SLiam Girdwood 353783560d0SDharageswari R int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data, 354783560d0SDharageswari R unsigned int size) 355783560d0SDharageswari R { 356783560d0SDharageswari R struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value; 357783560d0SDharageswari R struct snd_sof_control *scontrol = be->dobj.private; 358783560d0SDharageswari R struct snd_soc_component *scomp = scontrol->scomp; 359783560d0SDharageswari R struct sof_ipc_ctrl_data *cdata = scontrol->control_data; 360783560d0SDharageswari R struct snd_ctl_tlv header; 361783560d0SDharageswari R struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data; 362783560d0SDharageswari R size_t data_size; 363783560d0SDharageswari R int ret; 364783560d0SDharageswari R int err; 365783560d0SDharageswari R 366783560d0SDharageswari R ret = pm_runtime_get_sync(scomp->dev); 36799ceec5cSPierre-Louis Bossart if (ret < 0 && ret != -EACCES) { 368783560d0SDharageswari R dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to resume %d\n", ret); 369783560d0SDharageswari R pm_runtime_put_noidle(scomp->dev); 370783560d0SDharageswari R return ret; 371783560d0SDharageswari R } 372783560d0SDharageswari R 373783560d0SDharageswari R /* set the ABI header values */ 374783560d0SDharageswari R cdata->data->magic = SOF_ABI_MAGIC; 375783560d0SDharageswari R cdata->data->abi = SOF_ABI_VERSION; 376783560d0SDharageswari R /* get all the component data from DSP */ 377783560d0SDharageswari R ret = snd_sof_ipc_set_get_comp_data(scontrol, SOF_IPC_COMP_GET_DATA, SOF_CTRL_TYPE_DATA_GET, 378783560d0SDharageswari R scontrol->cmd, false); 379783560d0SDharageswari R if (ret < 0) 380783560d0SDharageswari R goto out; 381783560d0SDharageswari R 382783560d0SDharageswari R /* check data size doesn't exceed max coming from topology */ 383783560d0SDharageswari R if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) { 384783560d0SDharageswari R dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %zu.\n", 385783560d0SDharageswari R cdata->data->size, 386783560d0SDharageswari R be->max - sizeof(const struct sof_abi_hdr)); 387783560d0SDharageswari R ret = -EINVAL; 388783560d0SDharageswari R goto out; 389783560d0SDharageswari R } 390783560d0SDharageswari R 391783560d0SDharageswari R data_size = cdata->data->size + sizeof(const struct sof_abi_hdr); 392783560d0SDharageswari R 393783560d0SDharageswari R header.numid = scontrol->cmd; 394783560d0SDharageswari R header.length = data_size; 395783560d0SDharageswari R if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) { 396783560d0SDharageswari R ret = -EFAULT; 397783560d0SDharageswari R goto out; 398783560d0SDharageswari R } 399783560d0SDharageswari R 400783560d0SDharageswari R if (copy_to_user(tlvd->tlv, cdata->data, data_size)) 401783560d0SDharageswari R ret = -EFAULT; 402783560d0SDharageswari R out: 403783560d0SDharageswari R pm_runtime_mark_last_busy(scomp->dev); 404783560d0SDharageswari R err = pm_runtime_put_autosuspend(scomp->dev); 405783560d0SDharageswari R if (err < 0) 406783560d0SDharageswari R dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to idle %d\n", err); 407783560d0SDharageswari R 408783560d0SDharageswari R return ret; 409783560d0SDharageswari R } 410783560d0SDharageswari R 411c3078f53SLiam Girdwood int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol, 412c3078f53SLiam Girdwood unsigned int __user *binary_data, 413c3078f53SLiam Girdwood unsigned int size) 414c3078f53SLiam Girdwood { 415c3078f53SLiam Girdwood struct soc_bytes_ext *be = 416c3078f53SLiam Girdwood (struct soc_bytes_ext *)kcontrol->private_value; 417c3078f53SLiam Girdwood struct snd_sof_control *scontrol = be->dobj.private; 418ee1e79b7SRanjani Sridharan struct snd_soc_component *scomp = scontrol->scomp; 419c3078f53SLiam Girdwood struct sof_ipc_ctrl_data *cdata = scontrol->control_data; 420c3078f53SLiam Girdwood struct snd_ctl_tlv header; 421c3078f53SLiam Girdwood struct snd_ctl_tlv __user *tlvd = 422c3078f53SLiam Girdwood (struct snd_ctl_tlv __user *)binary_data; 423c3078f53SLiam Girdwood int data_size; 424c3078f53SLiam Girdwood 425c3078f53SLiam Girdwood /* 426c3078f53SLiam Girdwood * Decrement the limit by ext bytes header size to 427c3078f53SLiam Girdwood * ensure the user space buffer is not exceeded. 428c3078f53SLiam Girdwood */ 429c3078f53SLiam Girdwood size -= sizeof(const struct snd_ctl_tlv); 430c3078f53SLiam Girdwood 431c3078f53SLiam Girdwood /* set the ABI header values */ 432c3078f53SLiam Girdwood cdata->data->magic = SOF_ABI_MAGIC; 433c3078f53SLiam Girdwood cdata->data->abi = SOF_ABI_VERSION; 434c3078f53SLiam Girdwood 435c3078f53SLiam Girdwood /* Prevent read of other kernel data or possibly corrupt response */ 436c3078f53SLiam Girdwood data_size = cdata->data->size + sizeof(const struct sof_abi_hdr); 437c3078f53SLiam Girdwood 438c3078f53SLiam Girdwood /* check data size doesn't exceed max coming from topology */ 439c3078f53SLiam Girdwood if (data_size > be->max) { 440ee1e79b7SRanjani Sridharan dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %d.\n", 441c3078f53SLiam Girdwood data_size, be->max); 442*b9f8e138SGuennadi Liakhovetski return -EINVAL; 443c3078f53SLiam Girdwood } 444c3078f53SLiam Girdwood 445c3078f53SLiam Girdwood header.numid = scontrol->cmd; 446c3078f53SLiam Girdwood header.length = data_size; 447*b9f8e138SGuennadi Liakhovetski if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) 448*b9f8e138SGuennadi Liakhovetski return -EFAULT; 449c3078f53SLiam Girdwood 450c3078f53SLiam Girdwood if (copy_to_user(tlvd->tlv, cdata->data, data_size)) 451*b9f8e138SGuennadi Liakhovetski return -EFAULT; 452c3078f53SLiam Girdwood 453*b9f8e138SGuennadi Liakhovetski return 0; 454c3078f53SLiam Girdwood } 455