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