1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2021 Intel Corporation. All rights reserved.
11 #include "sof-audio.h"
12 #include "ipc3-priv.h"
14 /* IPC set()/get() for kcontrols. */
15 static int sof_ipc3_set_get_kcontrol_data(struct snd_sof_control *scontrol, bool set)
17 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scontrol->scomp);
18 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
19 const struct sof_ipc_ops *iops = sdev->ipc->ops;
20 enum sof_ipc_ctrl_type ctrl_type;
21 struct snd_sof_widget *swidget;
22 bool widget_found = false;
23 u32 ipc_cmd, msg_bytes;
25 list_for_each_entry(swidget, &sdev->widget_list, list) {
26 if (swidget->comp_id == scontrol->comp_id) {
33 dev_err(sdev->dev, "%s: can't find widget with id %d\n", __func__,
39 * Volatile controls should always be part of static pipelines and the widget use_count
40 * would always be > 0 in this case. For the others, just return the cached value if the
41 * widget is not set up.
43 if (!swidget->use_count)
47 * Select the IPC cmd and the ctrl_type based on the ctrl_cmd and the
49 * Note: SOF_CTRL_TYPE_VALUE_COMP_* is not used and supported currently
52 if (cdata->cmd == SOF_CTRL_CMD_BINARY) {
53 ipc_cmd = set ? SOF_IPC_COMP_SET_DATA : SOF_IPC_COMP_GET_DATA;
54 ctrl_type = set ? SOF_CTRL_TYPE_DATA_SET : SOF_CTRL_TYPE_DATA_GET;
56 ipc_cmd = set ? SOF_IPC_COMP_SET_VALUE : SOF_IPC_COMP_GET_VALUE;
57 ctrl_type = set ? SOF_CTRL_TYPE_VALUE_CHAN_SET : SOF_CTRL_TYPE_VALUE_CHAN_GET;
60 cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd;
61 cdata->type = ctrl_type;
62 cdata->comp_id = scontrol->comp_id;
65 /* calculate header and data size */
66 switch (cdata->type) {
67 case SOF_CTRL_TYPE_VALUE_CHAN_GET:
68 case SOF_CTRL_TYPE_VALUE_CHAN_SET:
69 cdata->num_elems = scontrol->num_channels;
71 msg_bytes = scontrol->num_channels *
72 sizeof(struct sof_ipc_ctrl_value_chan);
73 msg_bytes += sizeof(struct sof_ipc_ctrl_data);
75 case SOF_CTRL_TYPE_DATA_GET:
76 case SOF_CTRL_TYPE_DATA_SET:
77 cdata->num_elems = cdata->data->size;
79 msg_bytes = cdata->data->size;
80 msg_bytes += sizeof(struct sof_ipc_ctrl_data) +
81 sizeof(struct sof_abi_hdr);
87 cdata->rhdr.hdr.size = msg_bytes;
88 cdata->elems_remaining = 0;
90 return iops->set_get_data(sdev, cdata, cdata->rhdr.hdr.size, set);
93 static void snd_sof_refresh_control(struct snd_sof_control *scontrol)
95 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
96 struct snd_soc_component *scomp = scontrol->scomp;
99 if (!scontrol->comp_data_dirty)
102 if (!pm_runtime_active(scomp->dev))
105 /* set the ABI header values */
106 cdata->data->magic = SOF_ABI_MAGIC;
107 cdata->data->abi = SOF_ABI_VERSION;
109 /* refresh the component data from DSP */
110 scontrol->comp_data_dirty = false;
111 ret = sof_ipc3_set_get_kcontrol_data(scontrol, false);
113 dev_err(scomp->dev, "Failed to get control data: %d\n", ret);
115 /* Set the flag to re-try next time to get the data */
116 scontrol->comp_data_dirty = true;
120 static int sof_ipc3_volume_get(struct snd_sof_control *scontrol,
121 struct snd_ctl_elem_value *ucontrol)
123 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
124 unsigned int channels = scontrol->num_channels;
127 snd_sof_refresh_control(scontrol);
129 /* read back each channel */
130 for (i = 0; i < channels; i++)
131 ucontrol->value.integer.value[i] = ipc_to_mixer(cdata->chanv[i].value,
132 scontrol->volume_table,
138 static bool sof_ipc3_volume_put(struct snd_sof_control *scontrol,
139 struct snd_ctl_elem_value *ucontrol)
141 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
142 struct snd_soc_component *scomp = scontrol->scomp;
143 unsigned int channels = scontrol->num_channels;
147 /* update each channel */
148 for (i = 0; i < channels; i++) {
149 u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
150 scontrol->volume_table, scontrol->max + 1);
152 change = change || (value != cdata->chanv[i].value);
153 cdata->chanv[i].channel = i;
154 cdata->chanv[i].value = value;
157 /* notify DSP of mixer updates */
158 if (pm_runtime_active(scomp->dev)) {
159 int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true);
162 dev_err(scomp->dev, "Failed to set mixer updates for %s\n",
171 static int sof_ipc3_switch_get(struct snd_sof_control *scontrol,
172 struct snd_ctl_elem_value *ucontrol)
174 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
175 unsigned int channels = scontrol->num_channels;
178 snd_sof_refresh_control(scontrol);
180 /* read back each channel */
181 for (i = 0; i < channels; i++)
182 ucontrol->value.integer.value[i] = cdata->chanv[i].value;
187 static bool sof_ipc3_switch_put(struct snd_sof_control *scontrol,
188 struct snd_ctl_elem_value *ucontrol)
190 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
191 struct snd_soc_component *scomp = scontrol->scomp;
192 unsigned int channels = scontrol->num_channels;
197 /* update each channel */
198 for (i = 0; i < channels; i++) {
199 value = ucontrol->value.integer.value[i];
200 change = change || (value != cdata->chanv[i].value);
201 cdata->chanv[i].channel = i;
202 cdata->chanv[i].value = value;
205 /* notify DSP of mixer updates */
206 if (pm_runtime_active(scomp->dev)) {
207 int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true);
210 dev_err(scomp->dev, "Failed to set mixer updates for %s\n",
219 static int sof_ipc3_enum_get(struct snd_sof_control *scontrol,
220 struct snd_ctl_elem_value *ucontrol)
222 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
223 unsigned int channels = scontrol->num_channels;
226 snd_sof_refresh_control(scontrol);
228 /* read back each channel */
229 for (i = 0; i < channels; i++)
230 ucontrol->value.enumerated.item[i] = cdata->chanv[i].value;
235 static bool sof_ipc3_enum_put(struct snd_sof_control *scontrol,
236 struct snd_ctl_elem_value *ucontrol)
238 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
239 struct snd_soc_component *scomp = scontrol->scomp;
240 unsigned int channels = scontrol->num_channels;
245 /* update each channel */
246 for (i = 0; i < channels; i++) {
247 value = ucontrol->value.enumerated.item[i];
248 change = change || (value != cdata->chanv[i].value);
249 cdata->chanv[i].channel = i;
250 cdata->chanv[i].value = value;
253 /* notify DSP of enum updates */
254 if (pm_runtime_active(scomp->dev)) {
255 int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true);
258 dev_err(scomp->dev, "Failed to set enum updates for %s\n",
267 static int sof_ipc3_bytes_get(struct snd_sof_control *scontrol,
268 struct snd_ctl_elem_value *ucontrol)
270 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
271 struct snd_soc_component *scomp = scontrol->scomp;
272 struct sof_abi_hdr *data = cdata->data;
275 snd_sof_refresh_control(scontrol);
277 if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
278 dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
283 /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
284 if (data->size > scontrol->max_size - sizeof(*data)) {
285 dev_err_ratelimited(scomp->dev,
286 "%u bytes of control data is invalid, max is %zu\n",
287 data->size, scontrol->max_size - sizeof(*data));
291 size = data->size + sizeof(*data);
293 /* copy back to kcontrol */
294 memcpy(ucontrol->value.bytes.data, data, size);
299 static int sof_ipc3_bytes_put(struct snd_sof_control *scontrol,
300 struct snd_ctl_elem_value *ucontrol)
302 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
303 struct snd_soc_component *scomp = scontrol->scomp;
304 struct sof_abi_hdr *data = cdata->data;
307 if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
308 dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
313 /* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */
314 if (data->size > scontrol->max_size - sizeof(*data)) {
315 dev_err_ratelimited(scomp->dev, "data size too big %u bytes max is %zu\n",
316 data->size, scontrol->max_size - sizeof(*data));
320 size = data->size + sizeof(*data);
322 /* copy from kcontrol */
323 memcpy(data, ucontrol->value.bytes.data, size);
325 /* notify DSP of byte control updates */
326 if (pm_runtime_active(scomp->dev))
327 return sof_ipc3_set_get_kcontrol_data(scontrol, true);
332 static int sof_ipc3_bytes_ext_get(struct snd_sof_control *scontrol,
333 const unsigned int __user *binary_data, unsigned int size)
335 struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
336 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
337 struct snd_soc_component *scomp = scontrol->scomp;
338 struct snd_ctl_tlv header;
341 snd_sof_refresh_control(scontrol);
344 * Decrement the limit by ext bytes header size to
345 * ensure the user space buffer is not exceeded.
347 if (size < sizeof(struct snd_ctl_tlv))
350 size -= sizeof(struct snd_ctl_tlv);
352 /* set the ABI header values */
353 cdata->data->magic = SOF_ABI_MAGIC;
354 cdata->data->abi = SOF_ABI_VERSION;
356 /* check data size doesn't exceed max coming from topology */
357 if (cdata->data->size > scontrol->max_size - sizeof(struct sof_abi_hdr)) {
358 dev_err_ratelimited(scomp->dev, "User data size %d exceeds max size %zu\n",
360 scontrol->max_size - sizeof(struct sof_abi_hdr));
364 data_size = cdata->data->size + sizeof(struct sof_abi_hdr);
366 /* make sure we don't exceed size provided by user space for data */
367 if (data_size > size)
370 header.numid = cdata->cmd;
371 header.length = data_size;
372 if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv)))
375 if (copy_to_user(tlvd->tlv, cdata->data, data_size))
381 static int sof_ipc3_bytes_ext_put(struct snd_sof_control *scontrol,
382 const unsigned int __user *binary_data,
385 const struct snd_ctl_tlv __user *tlvd = (const struct snd_ctl_tlv __user *)binary_data;
386 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
387 struct snd_soc_component *scomp = scontrol->scomp;
388 struct snd_ctl_tlv header;
391 * The beginning of bytes data contains a header from where
392 * the length (as bytes) is needed to know the correct copy
393 * length of data from tlvd->tlv.
395 if (copy_from_user(&header, tlvd, sizeof(struct snd_ctl_tlv)))
398 /* make sure TLV info is consistent */
399 if (header.length + sizeof(struct snd_ctl_tlv) > size) {
400 dev_err_ratelimited(scomp->dev, "Inconsistent TLV, data %d + header %zu > %d\n",
401 header.length, sizeof(struct snd_ctl_tlv), size);
405 /* be->max is coming from topology */
406 if (header.length > scontrol->max_size) {
407 dev_err_ratelimited(scomp->dev, "Bytes data size %d exceeds max %zu\n",
408 header.length, scontrol->max_size);
412 /* Check that header id matches the command */
413 if (header.numid != cdata->cmd) {
414 dev_err_ratelimited(scomp->dev, "Incorrect command for bytes put %d\n",
419 if (copy_from_user(cdata->data, tlvd->tlv, header.length))
422 if (cdata->data->magic != SOF_ABI_MAGIC) {
423 dev_err_ratelimited(scomp->dev, "Wrong ABI magic 0x%08x\n", cdata->data->magic);
427 if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
428 dev_err_ratelimited(scomp->dev, "Incompatible ABI version 0x%08x\n",
433 /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
434 if (cdata->data->size > scontrol->max_size - sizeof(struct sof_abi_hdr)) {
435 dev_err_ratelimited(scomp->dev, "Mismatch in ABI data size (truncated?)\n");
439 /* notify DSP of byte control updates */
440 if (pm_runtime_active(scomp->dev))
441 return sof_ipc3_set_get_kcontrol_data(scontrol, true);
446 static int sof_ipc3_bytes_ext_volatile_get(struct snd_sof_control *scontrol,
447 const unsigned int __user *binary_data,
450 struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
451 struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
452 struct snd_soc_component *scomp = scontrol->scomp;
453 struct snd_ctl_tlv header;
458 * Decrement the limit by ext bytes header size to
459 * ensure the user space buffer is not exceeded.
461 if (size < sizeof(struct snd_ctl_tlv))
464 size -= sizeof(struct snd_ctl_tlv);
466 /* set the ABI header values */
467 cdata->data->magic = SOF_ABI_MAGIC;
468 cdata->data->abi = SOF_ABI_VERSION;
470 /* get all the component data from DSP */
471 ret = sof_ipc3_set_get_kcontrol_data(scontrol, false);
475 /* check data size doesn't exceed max coming from topology */
476 if (cdata->data->size > scontrol->max_size - sizeof(struct sof_abi_hdr)) {
477 dev_err_ratelimited(scomp->dev, "User data size %d exceeds max size %zu\n",
479 scontrol->max_size - sizeof(struct sof_abi_hdr));
483 data_size = cdata->data->size + sizeof(struct sof_abi_hdr);
485 /* make sure we don't exceed size provided by user space for data */
486 if (data_size > size)
489 header.numid = cdata->cmd;
490 header.length = data_size;
491 if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv)))
494 if (copy_to_user(tlvd->tlv, cdata->data, data_size))
500 static void snd_sof_update_control(struct snd_sof_control *scontrol,
501 struct sof_ipc_ctrl_data *cdata)
503 struct snd_soc_component *scomp = scontrol->scomp;
504 struct sof_ipc_ctrl_data *local_cdata;
507 local_cdata = scontrol->ipc_control_data;
509 if (cdata->cmd == SOF_CTRL_CMD_BINARY) {
510 if (cdata->num_elems != local_cdata->data->size) {
511 dev_err(scomp->dev, "cdata binary size mismatch %u - %u\n",
512 cdata->num_elems, local_cdata->data->size);
516 /* copy the new binary data */
517 memcpy(local_cdata->data, cdata->data, cdata->num_elems);
518 } else if (cdata->num_elems != scontrol->num_channels) {
519 dev_err(scomp->dev, "cdata channel count mismatch %u - %d\n",
520 cdata->num_elems, scontrol->num_channels);
522 /* copy the new values */
523 for (i = 0; i < cdata->num_elems; i++)
524 local_cdata->chanv[i].value = cdata->chanv[i].value;
528 static void sof_ipc3_control_update(struct snd_sof_dev *sdev, void *ipc_control_message)
530 struct sof_ipc_ctrl_data *cdata = ipc_control_message;
531 struct snd_soc_dapm_widget *widget;
532 struct snd_sof_control *scontrol;
533 struct snd_sof_widget *swidget;
534 struct snd_kcontrol *kc = NULL;
535 struct soc_mixer_control *sm;
536 struct soc_bytes_ext *be;
537 size_t expected_size;
542 if (cdata->type == SOF_CTRL_TYPE_VALUE_COMP_GET ||
543 cdata->type == SOF_CTRL_TYPE_VALUE_COMP_SET) {
544 dev_err(sdev->dev, "Component data is not supported in control notification\n");
548 /* Find the swidget first */
549 list_for_each_entry(swidget, &sdev->widget_list, list) {
550 if (swidget->comp_id == cdata->comp_id) {
559 /* Translate SOF cmd to TPLG type */
560 switch (cdata->cmd) {
561 case SOF_CTRL_CMD_VOLUME:
562 case SOF_CTRL_CMD_SWITCH:
563 type = SND_SOC_TPLG_TYPE_MIXER;
565 case SOF_CTRL_CMD_BINARY:
566 type = SND_SOC_TPLG_TYPE_BYTES;
568 case SOF_CTRL_CMD_ENUM:
569 type = SND_SOC_TPLG_TYPE_ENUM;
572 dev_err(sdev->dev, "Unknown cmd %u in %s\n", cdata->cmd, __func__);
576 widget = swidget->widget;
577 for (i = 0; i < widget->num_kcontrols; i++) {
578 /* skip non matching types or non matching indexes within type */
579 if (widget->dobj.widget.kcontrol_type[i] == type &&
580 widget->kcontrol_news[i].index == cdata->index) {
581 kc = widget->kcontrols[i];
589 switch (cdata->cmd) {
590 case SOF_CTRL_CMD_VOLUME:
591 case SOF_CTRL_CMD_SWITCH:
592 sm = (struct soc_mixer_control *)kc->private_value;
593 scontrol = sm->dobj.private;
595 case SOF_CTRL_CMD_BINARY:
596 be = (struct soc_bytes_ext *)kc->private_value;
597 scontrol = be->dobj.private;
599 case SOF_CTRL_CMD_ENUM:
600 se = (struct soc_enum *)kc->private_value;
601 scontrol = se->dobj.private;
607 expected_size = sizeof(struct sof_ipc_ctrl_data);
608 switch (cdata->type) {
609 case SOF_CTRL_TYPE_VALUE_CHAN_GET:
610 case SOF_CTRL_TYPE_VALUE_CHAN_SET:
611 expected_size += cdata->num_elems *
612 sizeof(struct sof_ipc_ctrl_value_chan);
614 case SOF_CTRL_TYPE_DATA_GET:
615 case SOF_CTRL_TYPE_DATA_SET:
616 expected_size += cdata->num_elems + sizeof(struct sof_abi_hdr);
622 if (cdata->rhdr.hdr.size != expected_size) {
623 dev_err(sdev->dev, "Component notification size mismatch\n");
627 if (cdata->num_elems)
629 * The message includes the updated value/data, update the
630 * control's local cache using the received notification
632 snd_sof_update_control(scontrol, cdata);
634 /* Mark the scontrol that the value/data is changed in SOF */
635 scontrol->comp_data_dirty = true;
637 snd_ctl_notify_one(swidget->scomp->card->snd_card, SNDRV_CTL_EVENT_MASK_VALUE, kc, 0);
640 static int sof_ipc3_widget_kcontrol_setup(struct snd_sof_dev *sdev,
641 struct snd_sof_widget *swidget)
643 struct snd_sof_control *scontrol;
646 /* set up all controls for the widget */
647 list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
648 if (scontrol->comp_id == swidget->comp_id) {
649 /* set kcontrol data in DSP */
650 ret = sof_ipc3_set_get_kcontrol_data(scontrol, true);
653 "kcontrol %d set up failed for widget %s\n",
654 scontrol->comp_id, swidget->widget->name);
659 * Read back the data from the DSP for static widgets.
660 * This is particularly useful for binary kcontrols
661 * associated with static pipeline widgets to initialize
662 * the data size to match that in the DSP.
664 if (swidget->dynamic_pipeline_widget)
667 ret = sof_ipc3_set_get_kcontrol_data(scontrol, false);
670 "kcontrol %d read failed for widget %s\n",
671 scontrol->comp_id, swidget->widget->name);
678 sof_ipc3_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
682 /* init the volume table */
683 scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
684 if (!scontrol->volume_table)
687 /* populate the volume table */
688 for (i = 0; i < size ; i++)
689 scontrol->volume_table[i] = vol_compute_gain(i, tlv);
694 const struct sof_ipc_tplg_control_ops tplg_ipc3_control_ops = {
695 .volume_put = sof_ipc3_volume_put,
696 .volume_get = sof_ipc3_volume_get,
697 .switch_put = sof_ipc3_switch_put,
698 .switch_get = sof_ipc3_switch_get,
699 .enum_put = sof_ipc3_enum_put,
700 .enum_get = sof_ipc3_enum_get,
701 .bytes_put = sof_ipc3_bytes_put,
702 .bytes_get = sof_ipc3_bytes_get,
703 .bytes_ext_put = sof_ipc3_bytes_ext_put,
704 .bytes_ext_get = sof_ipc3_bytes_ext_get,
705 .bytes_ext_volatile_get = sof_ipc3_bytes_ext_volatile_get,
706 .update = sof_ipc3_control_update,
707 .widget_kcontrol_setup = sof_ipc3_widget_kcontrol_setup,
708 .set_up_volume_table = sof_ipc3_set_up_volume_table,