FILE
ipc4-control.c
File preview with syntax highlighting.
linux-6.19.14/sound/soc/sof/ipc4-control.c
1// SPDX-License-Identifier: (GPL-2.0-only 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) 2022 Intel Corporation 7// 8// 9 10#include "sof-priv.h" 11#include "sof-audio.h" 12#include "ipc4-priv.h" 13#include "ipc4-topology.h" 14 15static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol, 16 bool set, bool lock) 17{ 18 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 19 struct snd_soc_component *scomp = scontrol->scomp; 20 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 21 const struct sof_ipc_ops *iops = sdev->ipc->ops; 22 struct sof_ipc4_msg *msg = &cdata->msg; 23 struct snd_sof_widget *swidget; 24 bool widget_found = false; 25 int ret = 0; 26 27 /* find widget associated with the control */ 28 list_for_each_entry(swidget, &sdev->widget_list, list) { 29 if (swidget->comp_id == scontrol->comp_id) { 30 widget_found = true; 31 break; 32 } 33 } 34 35 if (!widget_found) { 36 dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name); 37 return -ENOENT; 38 } 39 40 if (lock) 41 mutex_lock(&swidget->setup_mutex); 42 else 43 lockdep_assert_held(&swidget->setup_mutex); 44 45 /* 46 * Volatile controls should always be part of static pipelines and the 47 * widget use_count would always be > 0 in this case. For the others, 48 * just return the cached value if the widget is not set up. 49 */ 50 if (!swidget->use_count) 51 goto unlock; 52 53 msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK; 54 msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id); 55 56 ret = iops->set_get_data(sdev, msg, msg->data_size, set); 57 if (!set) 58 goto unlock; 59 60 /* It is a set-data operation, and we have a valid backup that we can restore */ 61 if (ret < 0) { 62 if (!scontrol->old_ipc_control_data) 63 goto unlock; 64 /* 65 * Current ipc_control_data is not valid, we use the last known good 66 * configuration 67 */ 68 memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data, 69 scontrol->size); 70 kfree(scontrol->old_ipc_control_data); 71 scontrol->old_ipc_control_data = NULL; 72 /* Send the last known good configuration to firmware */ 73 ret = iops->set_get_data(sdev, msg, msg->data_size, set); 74 if (ret < 0) 75 goto unlock; 76 } 77 78unlock: 79 if (lock) 80 mutex_unlock(&swidget->setup_mutex); 81 82 return ret; 83} 84 85static int 86sof_ipc4_set_volume_data(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget, 87 struct snd_sof_control *scontrol, bool lock) 88{ 89 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 90 struct sof_ipc4_gain *gain = swidget->private; 91 struct sof_ipc4_msg *msg = &cdata->msg; 92 struct sof_ipc4_gain_params params; 93 bool all_channels_equal = true; 94 u32 value; 95 int ret, i; 96 97 /* check if all channel values are equal */ 98 value = cdata->chanv[0].value; 99 for (i = 1; i < scontrol->num_channels; i++) { 100 if (cdata->chanv[i].value != value) { 101 all_channels_equal = false; 102 break; 103 } 104 } 105 106 /* 107 * notify DSP with a single IPC message if all channel values are equal. Otherwise send 108 * a separate IPC for each channel. 109 */ 110 for (i = 0; i < scontrol->num_channels; i++) { 111 if (all_channels_equal) { 112 params.channels = SOF_IPC4_GAIN_ALL_CHANNELS_MASK; 113 params.init_val = cdata->chanv[0].value; 114 } else { 115 params.channels = cdata->chanv[i].channel; 116 params.init_val = cdata->chanv[i].value; 117 } 118 119 /* set curve type and duration from topology */ 120 params.curve_duration_l = gain->data.params.curve_duration_l; 121 params.curve_duration_h = gain->data.params.curve_duration_h; 122 params.curve_type = gain->data.params.curve_type; 123 124 msg->data_ptr = ¶ms; 125 msg->data_size = sizeof(params); 126 127 ret = sof_ipc4_set_get_kcontrol_data(scontrol, true, lock); 128 msg->data_ptr = NULL; 129 msg->data_size = 0; 130 if (ret < 0) { 131 dev_err(sdev->dev, "Failed to set volume update for %s\n", 132 scontrol->name); 133 return ret; 134 } 135 136 if (all_channels_equal) 137 break; 138 } 139 140 return 0; 141} 142 143static bool sof_ipc4_volume_put(struct snd_sof_control *scontrol, 144 struct snd_ctl_elem_value *ucontrol) 145{ 146 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 147 struct snd_soc_component *scomp = scontrol->scomp; 148 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 149 unsigned int channels = scontrol->num_channels; 150 struct snd_sof_widget *swidget; 151 bool widget_found = false; 152 bool change = false; 153 unsigned int i; 154 int ret; 155 156 /* update each channel */ 157 for (i = 0; i < channels; i++) { 158 u32 value = mixer_to_ipc(ucontrol->value.integer.value[i], 159 scontrol->volume_table, scontrol->max + 1); 160 161 change = change || (value != cdata->chanv[i].value); 162 cdata->chanv[i].channel = i; 163 cdata->chanv[i].value = value; 164 } 165 166 if (!pm_runtime_active(scomp->dev)) 167 return change; 168 169 /* find widget associated with the control */ 170 list_for_each_entry(swidget, &sdev->widget_list, list) { 171 if (swidget->comp_id == scontrol->comp_id) { 172 widget_found = true; 173 break; 174 } 175 } 176 177 if (!widget_found) { 178 dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name); 179 return false; 180 } 181 182 ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol, true); 183 if (ret < 0) 184 return false; 185 186 return change; 187} 188 189static int sof_ipc4_volume_get(struct snd_sof_control *scontrol, 190 struct snd_ctl_elem_value *ucontrol) 191{ 192 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 193 unsigned int channels = scontrol->num_channels; 194 unsigned int i; 195 196 for (i = 0; i < channels; i++) 197 ucontrol->value.integer.value[i] = ipc_to_mixer(cdata->chanv[i].value, 198 scontrol->volume_table, 199 scontrol->max + 1); 200 201 return 0; 202} 203 204static int 205sof_ipc4_set_generic_control_data(struct snd_sof_dev *sdev, 206 struct snd_sof_widget *swidget, 207 struct snd_sof_control *scontrol, bool lock) 208{ 209 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 210 struct sof_ipc4_control_msg_payload *data; 211 struct sof_ipc4_msg *msg = &cdata->msg; 212 size_t data_size; 213 unsigned int i; 214 int ret; 215 216 data_size = struct_size(data, chanv, scontrol->num_channels); 217 data = kzalloc(data_size, GFP_KERNEL); 218 if (!data) 219 return -ENOMEM; 220 221 data->id = cdata->index; 222 data->num_elems = scontrol->num_channels; 223 for (i = 0; i < scontrol->num_channels; i++) { 224 data->chanv[i].channel = cdata->chanv[i].channel; 225 data->chanv[i].value = cdata->chanv[i].value; 226 } 227 228 msg->data_ptr = data; 229 msg->data_size = data_size; 230 231 ret = sof_ipc4_set_get_kcontrol_data(scontrol, true, lock); 232 msg->data_ptr = NULL; 233 msg->data_size = 0; 234 if (ret < 0) 235 dev_err(sdev->dev, "Failed to set control update for %s\n", 236 scontrol->name); 237 238 kfree(data); 239 240 return ret; 241} 242 243static void sof_ipc4_refresh_generic_control(struct snd_sof_control *scontrol) 244{ 245 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 246 struct snd_soc_component *scomp = scontrol->scomp; 247 struct sof_ipc4_control_msg_payload *data; 248 struct sof_ipc4_msg *msg = &cdata->msg; 249 size_t data_size; 250 unsigned int i; 251 int ret; 252 253 if (!scontrol->comp_data_dirty) 254 return; 255 256 if (!pm_runtime_active(scomp->dev)) 257 return; 258 259 data_size = struct_size(data, chanv, scontrol->num_channels); 260 data = kmalloc(data_size, GFP_KERNEL); 261 if (!data) 262 return; 263 264 data->id = cdata->index; 265 data->num_elems = scontrol->num_channels; 266 msg->data_ptr = data; 267 msg->data_size = data_size; 268 269 scontrol->comp_data_dirty = false; 270 ret = sof_ipc4_set_get_kcontrol_data(scontrol, false, true); 271 msg->data_ptr = NULL; 272 msg->data_size = 0; 273 if (!ret) { 274 for (i = 0; i < scontrol->num_channels; i++) { 275 cdata->chanv[i].channel = data->chanv[i].channel; 276 cdata->chanv[i].value = data->chanv[i].value; 277 } 278 } else { 279 dev_err(scomp->dev, "Failed to read control data for %s\n", 280 scontrol->name); 281 scontrol->comp_data_dirty = true; 282 } 283 284 kfree(data); 285} 286 287static bool sof_ipc4_switch_put(struct snd_sof_control *scontrol, 288 struct snd_ctl_elem_value *ucontrol) 289{ 290 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 291 struct snd_soc_component *scomp = scontrol->scomp; 292 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 293 struct snd_sof_widget *swidget; 294 bool widget_found = false; 295 bool change = false; 296 unsigned int i; 297 u32 value; 298 int ret; 299 300 /* update each channel */ 301 for (i = 0; i < scontrol->num_channels; i++) { 302 value = ucontrol->value.integer.value[i]; 303 change = change || (value != cdata->chanv[i].value); 304 cdata->chanv[i].channel = i; 305 cdata->chanv[i].value = value; 306 } 307 308 if (!pm_runtime_active(scomp->dev)) 309 return change; 310 311 /* find widget associated with the control */ 312 list_for_each_entry(swidget, &sdev->widget_list, list) { 313 if (swidget->comp_id == scontrol->comp_id) { 314 widget_found = true; 315 break; 316 } 317 } 318 319 if (!widget_found) { 320 dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name); 321 return false; 322 } 323 324 ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true); 325 if (ret < 0) 326 return false; 327 328 return change; 329} 330 331static int sof_ipc4_switch_get(struct snd_sof_control *scontrol, 332 struct snd_ctl_elem_value *ucontrol) 333{ 334 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 335 unsigned int i; 336 337 sof_ipc4_refresh_generic_control(scontrol); 338 339 /* read back each channel */ 340 for (i = 0; i < scontrol->num_channels; i++) 341 ucontrol->value.integer.value[i] = cdata->chanv[i].value; 342 343 return 0; 344} 345 346static bool sof_ipc4_enum_put(struct snd_sof_control *scontrol, 347 struct snd_ctl_elem_value *ucontrol) 348{ 349 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 350 struct snd_soc_component *scomp = scontrol->scomp; 351 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 352 struct snd_sof_widget *swidget; 353 bool widget_found = false; 354 bool change = false; 355 unsigned int i; 356 u32 value; 357 int ret; 358 359 /* update each channel */ 360 for (i = 0; i < scontrol->num_channels; i++) { 361 value = ucontrol->value.enumerated.item[i]; 362 change = change || (value != cdata->chanv[i].value); 363 cdata->chanv[i].channel = i; 364 cdata->chanv[i].value = value; 365 } 366 367 if (!pm_runtime_active(scomp->dev)) 368 return change; 369 370 /* find widget associated with the control */ 371 list_for_each_entry(swidget, &sdev->widget_list, list) { 372 if (swidget->comp_id == scontrol->comp_id) { 373 widget_found = true; 374 break; 375 } 376 } 377 378 if (!widget_found) { 379 dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name); 380 return false; 381 } 382 383 ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true); 384 if (ret < 0) 385 return false; 386 387 return change; 388} 389 390static int sof_ipc4_enum_get(struct snd_sof_control *scontrol, 391 struct snd_ctl_elem_value *ucontrol) 392{ 393 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 394 unsigned int i; 395 396 sof_ipc4_refresh_generic_control(scontrol); 397 398 /* read back each channel */ 399 for (i = 0; i < scontrol->num_channels; i++) 400 ucontrol->value.enumerated.item[i] = cdata->chanv[i].value; 401 402 return 0; 403} 404 405static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev, 406 struct snd_sof_control *scontrol, 407 bool set, bool lock) 408{ 409 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 410 struct sof_abi_hdr *data = cdata->data; 411 struct sof_ipc4_msg *msg = &cdata->msg; 412 int ret = 0; 413 414 /* Send the new data to the firmware only if it is powered up */ 415 if (set) { 416 if (!pm_runtime_active(sdev->dev)) 417 return 0; 418 419 if (!data->size) { 420 dev_dbg(sdev->dev, "%s: No data to be sent.\n", 421 scontrol->name); 422 return 0; 423 } 424 } 425 426 msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type); 427 428 msg->data_ptr = data->data; 429 if (set) 430 msg->data_size = data->size; 431 else 432 msg->data_size = scontrol->max_size - sizeof(*data); 433 434 ret = sof_ipc4_set_get_kcontrol_data(scontrol, set, lock); 435 if (ret < 0) { 436 dev_err(sdev->dev, "Failed to %s for %s\n", 437 set ? "set bytes update" : "get bytes", 438 scontrol->name); 439 } else if (!set) { 440 /* Update the sizes according to the received payload data */ 441 data->size = msg->data_size; 442 scontrol->size = sizeof(*cdata) + sizeof(*data) + data->size; 443 } 444 445 msg->data_ptr = NULL; 446 msg->data_size = 0; 447 448 return ret; 449} 450 451static int sof_ipc4_bytes_put(struct snd_sof_control *scontrol, 452 struct snd_ctl_elem_value *ucontrol) 453{ 454 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 455 struct snd_soc_component *scomp = scontrol->scomp; 456 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 457 struct sof_abi_hdr *data = cdata->data; 458 size_t size; 459 int ret; 460 461 if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) { 462 dev_err_ratelimited(scomp->dev, 463 "data max %zu exceeds ucontrol data array size\n", 464 scontrol->max_size); 465 return -EINVAL; 466 } 467 468 /* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */ 469 if (data->size > scontrol->max_size - sizeof(*data)) { 470 dev_err_ratelimited(scomp->dev, 471 "data size too big %u bytes max is %zu\n", 472 data->size, scontrol->max_size - sizeof(*data)); 473 return -EINVAL; 474 } 475 476 size = data->size + sizeof(*data); 477 478 /* copy from kcontrol */ 479 memcpy(data, ucontrol->value.bytes.data, size); 480 481 ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true); 482 if (!ret) 483 /* Update the cdata size */ 484 scontrol->size = sizeof(*cdata) + size; 485 486 return ret; 487} 488 489static int sof_ipc4_bytes_get(struct snd_sof_control *scontrol, 490 struct snd_ctl_elem_value *ucontrol) 491{ 492 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 493 struct snd_soc_component *scomp = scontrol->scomp; 494 struct sof_abi_hdr *data = cdata->data; 495 size_t size; 496 497 if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) { 498 dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n", 499 scontrol->max_size); 500 return -EINVAL; 501 } 502 503 if (data->size > scontrol->max_size - sizeof(*data)) { 504 dev_err_ratelimited(scomp->dev, 505 "%u bytes of control data is invalid, max is %zu\n", 506 data->size, scontrol->max_size - sizeof(*data)); 507 return -EINVAL; 508 } 509 510 size = data->size + sizeof(*data); 511 512 /* copy back to kcontrol */ 513 memcpy(ucontrol->value.bytes.data, data, size); 514 515 return 0; 516} 517 518static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol, 519 const unsigned int __user *binary_data, 520 unsigned int size) 521{ 522 struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data; 523 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 524 struct snd_soc_component *scomp = scontrol->scomp; 525 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 526 struct sof_abi_hdr *data = cdata->data; 527 struct sof_abi_hdr abi_hdr; 528 struct snd_ctl_tlv header; 529 530 /* 531 * The beginning of bytes data contains a header from where 532 * the length (as bytes) is needed to know the correct copy 533 * length of data from tlvd->tlv. 534 */ 535 if (copy_from_user(&header, tlvd, sizeof(struct snd_ctl_tlv))) 536 return -EFAULT; 537 538 /* make sure TLV info is consistent */ 539 if (header.length + sizeof(struct snd_ctl_tlv) > size) { 540 dev_err_ratelimited(scomp->dev, 541 "Inconsistent TLV, data %d + header %zu > %d\n", 542 header.length, sizeof(struct snd_ctl_tlv), size); 543 return -EINVAL; 544 } 545 546 /* be->max is coming from topology */ 547 if (header.length > scontrol->max_size) { 548 dev_err_ratelimited(scomp->dev, 549 "Bytes data size %d exceeds max %zu\n", 550 header.length, scontrol->max_size); 551 return -EINVAL; 552 } 553 554 /* Check header id */ 555 if (header.numid != SOF_CTRL_CMD_BINARY) { 556 dev_err_ratelimited(scomp->dev, 557 "Incorrect numid for bytes put %d\n", 558 header.numid); 559 return -EINVAL; 560 } 561 562 /* Verify the ABI header first */ 563 if (copy_from_user(&abi_hdr, tlvd->tlv, sizeof(abi_hdr))) 564 return -EFAULT; 565 566 if (abi_hdr.magic != SOF_IPC4_ABI_MAGIC) { 567 dev_err_ratelimited(scomp->dev, "Wrong ABI magic 0x%08x\n", 568 abi_hdr.magic); 569 return -EINVAL; 570 } 571 572 if (abi_hdr.size > scontrol->max_size - sizeof(abi_hdr)) { 573 dev_err_ratelimited(scomp->dev, 574 "%u bytes of control data is invalid, max is %zu\n", 575 abi_hdr.size, scontrol->max_size - sizeof(abi_hdr)); 576 return -EINVAL; 577 } 578 579 if (!scontrol->old_ipc_control_data) { 580 /* Create a backup of the current, valid bytes control */ 581 scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data, 582 scontrol->size, GFP_KERNEL); 583 if (!scontrol->old_ipc_control_data) 584 return -ENOMEM; 585 } 586 587 /* Copy the whole binary data which includes the ABI header and the payload */ 588 if (copy_from_user(data, tlvd->tlv, header.length)) { 589 memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data, 590 scontrol->size); 591 kfree(scontrol->old_ipc_control_data); 592 scontrol->old_ipc_control_data = NULL; 593 return -EFAULT; 594 } 595 596 /* Update the cdata size */ 597 scontrol->size = sizeof(*cdata) + header.length; 598 599 return sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true); 600} 601 602static int _sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol, 603 const unsigned int __user *binary_data, 604 unsigned int size, bool from_dsp) 605{ 606 struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data; 607 struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data; 608 struct snd_soc_component *scomp = scontrol->scomp; 609 struct sof_abi_hdr *data = cdata->data; 610 struct snd_ctl_tlv header; 611 size_t data_size; 612 613 /* 614 * Decrement the limit by ext bytes header size to ensure the user space 615 * buffer is not exceeded. 616 */ 617 if (size < sizeof(struct snd_ctl_tlv)) 618 return -ENOSPC; 619 620 size -= sizeof(struct snd_ctl_tlv); 621 622 /* get all the component data from DSP */ 623 if (from_dsp) { 624 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp); 625 int ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, false, true); 626 627 if (ret < 0) 628 return ret; 629 630 /* Set the ABI magic (if the control is not initialized) */ 631 data->magic = SOF_IPC4_ABI_MAGIC; 632 } 633 634 if (data->size > scontrol->max_size - sizeof(*data)) { 635 dev_err_ratelimited(scomp->dev, 636 "%u bytes of control data is invalid, max is %zu\n", 637 data->size, scontrol->max_size - sizeof(*data)); 638 return -EINVAL; 639 } 640 641 data_size = data->size + sizeof(struct sof_abi_hdr); 642 643 /* make sure we don't exceed size provided by user space for data */ 644 if (data_size > size) 645 return -ENOSPC; 646 647 /* Set header id and length */ 648 header.numid = SOF_CTRL_CMD_BINARY; 649 header.length = data_size; 650 651 if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv))) 652 return -EFAULT; 653 654 if (copy_to_user(tlvd->tlv, data, data_size)) 655 return -EFAULT; 656 657 return 0; 658} 659 660static int sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol, 661 const unsigned int __user *binary_data, 662 unsigned int size) 663{ 664 return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, false); 665} 666 667static int sof_ipc4_bytes_ext_volatile_get(struct snd_sof_control *scontrol, 668 const unsigned int __user *binary_data, 669 unsigned int size) 670{ 671 return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, true); 672} 673 674static int 675sof_ipc4_volsw_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget, 676 struct snd_sof_control *scontrol) 677{ 678 if (scontrol->max == 1) 679 return sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, false); 680 681 return sof_ipc4_set_volume_data(sdev, swidget, scontrol, false); 682} 683 684#define PARAM_ID_FROM_EXTENSION(_ext) (((_ext) & SOF_IPC4_MOD_EXT_MSG_PARAM_ID_MASK) \ 685 >> SOF_IPC4_MOD_EXT_MSG_PARAM_ID_SHIFT) 686 687static void sof_ipc4_control_update(struct snd_sof_dev *sdev, void *ipc_message) 688{ 689 struct sof_ipc4_msg *ipc4_msg = ipc_message; 690 struct sof_ipc4_notify_module_data *ndata = ipc4_msg->data_ptr; 691 struct sof_ipc4_control_msg_payload *msg_data; 692 struct sof_ipc4_control_data *cdata; 693 struct snd_soc_dapm_widget *widget; 694 struct snd_sof_control *scontrol; 695 struct snd_sof_widget *swidget; 696 struct snd_kcontrol *kc = NULL; 697 bool scontrol_found = false; 698 u32 event_param_id; 699 int i, type; 700 701 if (ndata->event_data_size < sizeof(*msg_data)) { 702 dev_err(sdev->dev, 703 "%s: Invalid event data size for module %u.%u: %u\n", 704 __func__, ndata->module_id, ndata->instance_id, 705 ndata->event_data_size); 706 return; 707 } 708 709 event_param_id = ndata->event_id & SOF_IPC4_NOTIFY_MODULE_EVENTID_ALSA_PARAMID_MASK; 710 switch (event_param_id) { 711 case SOF_IPC4_SWITCH_CONTROL_PARAM_ID: 712 type = SND_SOC_TPLG_TYPE_MIXER; 713 break; 714 case SOF_IPC4_ENUM_CONTROL_PARAM_ID: 715 type = SND_SOC_TPLG_TYPE_ENUM; 716 break; 717 default: 718 dev_err(sdev->dev, 719 "%s: Invalid control type for module %u.%u: %u\n", 720 __func__, ndata->module_id, ndata->instance_id, 721 event_param_id); 722 return; 723 } 724 725 /* Find the swidget based on ndata->module_id and ndata->instance_id */ 726 swidget = sof_ipc4_find_swidget_by_ids(sdev, ndata->module_id, 727 ndata->instance_id); 728 if (!swidget) { 729 dev_err(sdev->dev, "%s: Failed to find widget for module %u.%u\n", 730 __func__, ndata->module_id, ndata->instance_id); 731 return; 732 } 733 734 /* Find the scontrol which is the source of the notification */ 735 msg_data = (struct sof_ipc4_control_msg_payload *)ndata->event_data; 736 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 737 if (scontrol->comp_id == swidget->comp_id) { 738 u32 local_param_id; 739 740 cdata = scontrol->ipc_control_data; 741 /* 742 * The scontrol's param_id is stored in the IPC message 743 * template's extension 744 */ 745 local_param_id = PARAM_ID_FROM_EXTENSION(cdata->msg.extension); 746 if (local_param_id == event_param_id && 747 msg_data->id == cdata->index) { 748 scontrol_found = true; 749 break; 750 } 751 } 752 } 753 754 if (!scontrol_found) { 755 dev_err(sdev->dev, 756 "%s: Failed to find control on widget %s: %u:%u\n", 757 __func__, swidget->widget->name, ndata->event_id & 0xffff, 758 msg_data->id); 759 return; 760 } 761 762 if (msg_data->num_elems) { 763 /* 764 * The message includes the updated value/data, update the 765 * control's local cache using the received notification 766 */ 767 for (i = 0; i < msg_data->num_elems; i++) { 768 u32 channel = msg_data->chanv[i].channel; 769 770 if (channel >= scontrol->num_channels) { 771 dev_warn(sdev->dev, 772 "Invalid channel index for %s: %u\n", 773 scontrol->name, i); 774 775 /* 776 * Mark the scontrol as dirty to force a refresh 777 * on next read 778 */ 779 scontrol->comp_data_dirty = true; 780 break; 781 } 782 783 cdata->chanv[channel].value = msg_data->chanv[i].value; 784 } 785 } else { 786 /* 787 * Mark the scontrol as dirty because the value/data is changed 788 * in firmware, forcing a refresh on next read access 789 */ 790 scontrol->comp_data_dirty = true; 791 } 792 793 /* 794 * Look up the ALSA kcontrol of the scontrol to be able to send a 795 * notification to user space 796 */ 797 widget = swidget->widget; 798 for (i = 0; i < widget->num_kcontrols; i++) { 799 /* skip non matching types or non matching indexes within type */ 800 if (widget->dobj.widget.kcontrol_type[i] == type && 801 widget->kcontrol_news[i].index == cdata->index) { 802 kc = widget->kcontrols[i]; 803 break; 804 } 805 } 806 807 if (!kc) 808 return; 809 810 snd_ctl_notify_one(swidget->scomp->card->snd_card, 811 SNDRV_CTL_EVENT_MASK_VALUE, kc, 0); 812} 813 814/* set up all controls for the widget */ 815static int sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget) 816{ 817 struct snd_sof_control *scontrol; 818 int ret = 0; 819 820 list_for_each_entry(scontrol, &sdev->kcontrol_list, list) { 821 if (scontrol->comp_id == swidget->comp_id) { 822 switch (scontrol->info_type) { 823 case SND_SOC_TPLG_CTL_VOLSW: 824 case SND_SOC_TPLG_CTL_VOLSW_SX: 825 case SND_SOC_TPLG_CTL_VOLSW_XR_SX: 826 ret = sof_ipc4_volsw_setup(sdev, swidget, scontrol); 827 break; 828 case SND_SOC_TPLG_CTL_BYTES: 829 ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, 830 true, false); 831 break; 832 case SND_SOC_TPLG_CTL_ENUM: 833 case SND_SOC_TPLG_CTL_ENUM_VALUE: 834 ret = sof_ipc4_set_generic_control_data(sdev, swidget, 835 scontrol, false); 836 break; 837 default: 838 break; 839 } 840 841 if (ret < 0) { 842 dev_err(sdev->dev, 843 "kcontrol %d set up failed for widget %s\n", 844 scontrol->comp_id, swidget->widget->name); 845 return ret; 846 } 847 } 848 } 849 850 return 0; 851} 852 853static int 854sof_ipc4_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size) 855{ 856 int i; 857 858 /* init the volume table */ 859 scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL); 860 if (!scontrol->volume_table) 861 return -ENOMEM; 862 863 /* populate the volume table */ 864 for (i = 0; i < size ; i++) { 865 u32 val = vol_compute_gain(i, tlv); 866 u64 q31val = ((u64)val) << 15; /* Can be over Q1.31, need to saturate */ 867 868 scontrol->volume_table[i] = q31val > SOF_IPC4_VOL_ZERO_DB ? 869 SOF_IPC4_VOL_ZERO_DB : q31val; 870 } 871 872 return 0; 873} 874 875const struct sof_ipc_tplg_control_ops tplg_ipc4_control_ops = { 876 .volume_put = sof_ipc4_volume_put, 877 .volume_get = sof_ipc4_volume_get, 878 .switch_put = sof_ipc4_switch_put, 879 .switch_get = sof_ipc4_switch_get, 880 .enum_put = sof_ipc4_enum_put, 881 .enum_get = sof_ipc4_enum_get, 882 .bytes_put = sof_ipc4_bytes_put, 883 .bytes_get = sof_ipc4_bytes_get, 884 .bytes_ext_put = sof_ipc4_bytes_ext_put, 885 .bytes_ext_get = sof_ipc4_bytes_ext_get, 886 .bytes_ext_volatile_get = sof_ipc4_bytes_ext_volatile_get, 887 .update = sof_ipc4_control_update, 888 .widget_kcontrol_setup = sof_ipc4_widget_kcontrol_setup, 889 .set_up_volume_table = sof_ipc4_set_up_volume_table, 890};
~/
Read-only file preview.