^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // This file is provided under a dual BSD/GPLv2 license. When using or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) // redistributing this file, you may do so under either license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // Copyright(c) 2018 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /* Mixer Controls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "sof-priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "sof-audio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static void update_mute_led(struct snd_sof_control *scontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int temp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) mask = 1U << snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) for (i = 0; i < scontrol->num_channels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (ucontrol->value.integer.value[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) temp |= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (temp == scontrol->led_ctl.led_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) scontrol->led_ctl.led_value = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #if IS_REACHABLE(CONFIG_LEDS_TRIGGER_AUDIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (!scontrol->led_ctl.direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ledtrig_audio_set(LED_AUDIO_MUTE, temp ? LED_OFF : LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ledtrig_audio_set(LED_AUDIO_MICMUTE, temp ? LED_OFF : LED_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static inline u32 mixer_to_ipc(unsigned int value, u32 *volume_map, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (value >= size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return volume_map[size - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return volume_map[value];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static inline u32 ipc_to_mixer(u32 value, u32 *volume_map, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) for (i = 0; i < size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (volume_map[i] >= value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return i - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct soc_mixer_control *sm =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) (struct soc_mixer_control *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct snd_sof_control *scontrol = sm->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned int i, channels = scontrol->num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* read back each channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) for (i = 0; i < channels; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ucontrol->value.integer.value[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ipc_to_mixer(cdata->chanv[i].value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) scontrol->volume_table, sm->max + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct soc_mixer_control *sm =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) (struct soc_mixer_control *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct snd_sof_control *scontrol = sm->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct snd_soc_component *scomp = scontrol->scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) unsigned int i, channels = scontrol->num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) bool change = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* update each channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) for (i = 0; i < channels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) value = mixer_to_ipc(ucontrol->value.integer.value[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) scontrol->volume_table, sm->max + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) change = change || (value != cdata->chanv[i].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cdata->chanv[i].channel = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) cdata->chanv[i].value = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* notify DSP of mixer updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (pm_runtime_active(scomp->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) snd_sof_ipc_set_get_comp_data(scontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) SOF_IPC_COMP_SET_VALUE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) SOF_CTRL_TYPE_VALUE_CHAN_GET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) SOF_CTRL_CMD_VOLUME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct soc_mixer_control *sm =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) (struct soc_mixer_control *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct snd_sof_control *scontrol = sm->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned int i, channels = scontrol->num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* read back each channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) for (i = 0; i < channels; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ucontrol->value.integer.value[i] = cdata->chanv[i].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct soc_mixer_control *sm =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) (struct soc_mixer_control *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct snd_sof_control *scontrol = sm->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct snd_soc_component *scomp = scontrol->scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) unsigned int i, channels = scontrol->num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) bool change = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* update each channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) for (i = 0; i < channels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) value = ucontrol->value.integer.value[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) change = change || (value != cdata->chanv[i].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) cdata->chanv[i].channel = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) cdata->chanv[i].value = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (scontrol->led_ctl.use_led)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) update_mute_led(scontrol, kcontrol, ucontrol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* notify DSP of mixer updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (pm_runtime_active(scomp->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) snd_sof_ipc_set_get_comp_data(scontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) SOF_IPC_COMP_SET_VALUE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) SOF_CTRL_TYPE_VALUE_CHAN_GET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) SOF_CTRL_CMD_SWITCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct soc_enum *se =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) (struct soc_enum *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct snd_sof_control *scontrol = se->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) unsigned int i, channels = scontrol->num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* read back each channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) for (i = 0; i < channels; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ucontrol->value.enumerated.item[i] = cdata->chanv[i].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct soc_enum *se =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) (struct soc_enum *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct snd_sof_control *scontrol = se->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct snd_soc_component *scomp = scontrol->scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) unsigned int i, channels = scontrol->num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) bool change = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /* update each channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) for (i = 0; i < channels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) value = ucontrol->value.enumerated.item[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) change = change || (value != cdata->chanv[i].value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) cdata->chanv[i].channel = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) cdata->chanv[i].value = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* notify DSP of enum updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (pm_runtime_active(scomp->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) snd_sof_ipc_set_get_comp_data(scontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) SOF_IPC_COMP_SET_VALUE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) SOF_CTRL_TYPE_VALUE_CHAN_GET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) SOF_CTRL_CMD_ENUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct soc_bytes_ext *be =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) (struct soc_bytes_ext *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct snd_sof_control *scontrol = be->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct snd_soc_component *scomp = scontrol->scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct sof_abi_hdr *data = cdata->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (be->max > sizeof(ucontrol->value.bytes.data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dev_err_ratelimited(scomp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) "error: data max %d exceeds ucontrol data array size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) be->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (data->size > be->max - sizeof(*data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dev_err_ratelimited(scomp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) "error: %u bytes of control data is invalid, max is %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) data->size, be->max - sizeof(*data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) size = data->size + sizeof(*data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* copy back to kcontrol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) memcpy(ucontrol->value.bytes.data, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) struct snd_ctl_elem_value *ucontrol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct soc_bytes_ext *be =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) (struct soc_bytes_ext *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct snd_sof_control *scontrol = be->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct snd_soc_component *scomp = scontrol->scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct sof_abi_hdr *data = cdata->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (be->max > sizeof(ucontrol->value.bytes.data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) dev_err_ratelimited(scomp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) "error: data max %d exceeds ucontrol data array size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) be->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (data->size > be->max - sizeof(*data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) dev_err_ratelimited(scomp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) "error: data size too big %u bytes max is %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) data->size, be->max - sizeof(*data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) size = data->size + sizeof(*data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* copy from kcontrol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) memcpy(data, ucontrol->value.bytes.data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* notify DSP of byte control updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (pm_runtime_active(scomp->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) snd_sof_ipc_set_get_comp_data(scontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) SOF_IPC_COMP_SET_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) SOF_CTRL_TYPE_DATA_SET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) scontrol->cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) const unsigned int __user *binary_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct soc_bytes_ext *be =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) (struct soc_bytes_ext *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct snd_sof_control *scontrol = be->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct snd_soc_component *scomp = scontrol->scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct snd_ctl_tlv header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) const struct snd_ctl_tlv __user *tlvd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) (const struct snd_ctl_tlv __user *)binary_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* make sure we have at least a header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (size < sizeof(struct snd_ctl_tlv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * The beginning of bytes data contains a header from where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * the length (as bytes) is needed to know the correct copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * length of data from tlvd->tlv.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* make sure TLV info is consistent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (header.length + sizeof(struct snd_ctl_tlv) > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) dev_err_ratelimited(scomp->dev, "error: inconsistent TLV, data %d + header %zu > %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) header.length, sizeof(struct snd_ctl_tlv), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) /* be->max is coming from topology */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (header.length > be->max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) dev_err_ratelimited(scomp->dev, "error: Bytes data size %d exceeds max %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) header.length, be->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* Check that header id matches the command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (header.numid != scontrol->cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) dev_err_ratelimited(scomp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) "error: incorrect numid %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) header.numid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (copy_from_user(cdata->data, tlvd->tlv, header.length))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (cdata->data->magic != SOF_ABI_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) dev_err_ratelimited(scomp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) "error: Wrong ABI magic 0x%08x.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) cdata->data->magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) dev_err_ratelimited(scomp->dev, "error: Incompatible ABI version 0x%08x.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) cdata->data->abi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dev_err_ratelimited(scomp->dev, "error: Mismatch in ABI data size (truncated?).\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) /* notify DSP of byte control updates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (pm_runtime_active(scomp->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) snd_sof_ipc_set_get_comp_data(scontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) SOF_IPC_COMP_SET_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) SOF_CTRL_TYPE_DATA_SET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) scontrol->cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct snd_sof_control *scontrol = be->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct snd_soc_component *scomp = scontrol->scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct snd_ctl_tlv header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) size_t data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * Decrement the limit by ext bytes header size to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * ensure the user space buffer is not exceeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (size < sizeof(struct snd_ctl_tlv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) size -= sizeof(struct snd_ctl_tlv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ret = pm_runtime_get_sync(scomp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (ret < 0 && ret != -EACCES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to resume %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) pm_runtime_put_noidle(scomp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /* set the ABI header values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) cdata->data->magic = SOF_ABI_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) cdata->data->abi = SOF_ABI_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /* get all the component data from DSP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) ret = snd_sof_ipc_set_get_comp_data(scontrol, SOF_IPC_COMP_GET_DATA, SOF_CTRL_TYPE_DATA_GET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) scontrol->cmd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /* check data size doesn't exceed max coming from topology */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %zu.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) cdata->data->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) be->max - sizeof(const struct sof_abi_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /* make sure we don't exceed size provided by user space for data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (data_size > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) header.numid = scontrol->cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) header.length = data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (copy_to_user(tlvd->tlv, cdata->data, data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) pm_runtime_mark_last_busy(scomp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) err = pm_runtime_put_autosuspend(scomp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to idle %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) unsigned int __user *binary_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) struct soc_bytes_ext *be =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) (struct soc_bytes_ext *)kcontrol->private_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) struct snd_sof_control *scontrol = be->dobj.private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct snd_soc_component *scomp = scontrol->scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct snd_ctl_tlv header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) struct snd_ctl_tlv __user *tlvd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) (struct snd_ctl_tlv __user *)binary_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) size_t data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * Decrement the limit by ext bytes header size to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * ensure the user space buffer is not exceeded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (size < sizeof(struct snd_ctl_tlv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) size -= sizeof(struct snd_ctl_tlv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) /* set the ABI header values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) cdata->data->magic = SOF_ABI_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) cdata->data->abi = SOF_ABI_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /* check data size doesn't exceed max coming from topology */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %zu.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) cdata->data->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) be->max - sizeof(const struct sof_abi_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /* make sure we don't exceed size provided by user space for data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (data_size > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) header.numid = scontrol->cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) header.length = data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (copy_to_user(tlvd->tlv, cdata->data, data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }