xref: /linux/sound/soc/sof/ipc4-control.c (revision 5bdfeccb7fbf6e000fc783cd8412732e67c1ad0c)
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 	const struct sof_abi_hdr *new_hdr =
558 		(const struct sof_abi_hdr *)ucontrol->value.bytes.data;
559 	size_t size;
560 	int ret;
561 
562 	if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
563 		dev_err_ratelimited(scomp->dev,
564 				    "data max %zu exceeds ucontrol data array size\n",
565 				    scontrol->max_size);
566 		return -EINVAL;
567 	}
568 
569 	/* Validate the new data's size, not the old one */
570 	if (new_hdr->size > scontrol->max_size - sizeof(*new_hdr)) {
571 		dev_err_ratelimited(scomp->dev,
572 				    "data size too big %u bytes max is %zu\n",
573 				    new_hdr->size,
574 				    scontrol->max_size - sizeof(*new_hdr));
575 		return -EINVAL;
576 	}
577 
578 	size = new_hdr->size + sizeof(*new_hdr);
579 
580 	/* copy from kcontrol */
581 	memcpy(data, ucontrol->value.bytes.data, size);
582 
583 	ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
584 	if (!ret)
585 		/* Update the cdata size */
586 		scontrol->size = sizeof(*cdata) + size;
587 
588 	return ret;
589 }
590 
591 static int sof_ipc4_bytes_get(struct snd_sof_control *scontrol,
592 			      struct snd_ctl_elem_value *ucontrol)
593 {
594 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
595 	struct snd_soc_component *scomp = scontrol->scomp;
596 	struct sof_abi_hdr *data = cdata->data;
597 	size_t size;
598 
599 	if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
600 		dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
601 				    scontrol->max_size);
602 		return -EINVAL;
603 	}
604 
605 	if (data->size > scontrol->max_size - sizeof(*data)) {
606 		dev_err_ratelimited(scomp->dev,
607 				    "%u bytes of control data is invalid, max is %zu\n",
608 				    data->size, scontrol->max_size - sizeof(*data));
609 		return -EINVAL;
610 	}
611 
612 	sof_ipc4_refresh_bytes_control(scontrol, true);
613 
614 	size = data->size + sizeof(*data);
615 
616 	/* copy back to kcontrol */
617 	memcpy(ucontrol->value.bytes.data, data, size);
618 
619 	return 0;
620 }
621 
622 static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
623 				  const unsigned int __user *binary_data,
624 				  unsigned int size)
625 {
626 	struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
627 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
628 	struct snd_soc_component *scomp = scontrol->scomp;
629 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
630 	struct sof_abi_hdr *data = cdata->data;
631 	struct sof_abi_hdr abi_hdr;
632 	struct snd_ctl_tlv header;
633 
634 	/*
635 	 * The beginning of bytes data contains a header from where
636 	 * the length (as bytes) is needed to know the correct copy
637 	 * length of data from tlvd->tlv.
638 	 */
639 	if (copy_from_user(&header, tlvd, sizeof(struct snd_ctl_tlv)))
640 		return -EFAULT;
641 
642 	/* make sure TLV info is consistent */
643 	if (header.length + sizeof(struct snd_ctl_tlv) > size) {
644 		dev_err_ratelimited(scomp->dev,
645 				    "Inconsistent TLV, data %d + header %zu > %d\n",
646 				    header.length, sizeof(struct snd_ctl_tlv), size);
647 		return -EINVAL;
648 	}
649 
650 	/* be->max is coming from topology */
651 	if (header.length > scontrol->max_size) {
652 		dev_err_ratelimited(scomp->dev,
653 				    "Bytes data size %d exceeds max %zu\n",
654 				    header.length, scontrol->max_size);
655 		return -EINVAL;
656 	}
657 
658 	/* Check header id */
659 	if (header.numid != SOF_CTRL_CMD_BINARY) {
660 		dev_err_ratelimited(scomp->dev,
661 				    "Incorrect numid for bytes put %d\n",
662 				    header.numid);
663 		return -EINVAL;
664 	}
665 
666 	/* Verify the ABI header first */
667 	if (copy_from_user(&abi_hdr, tlvd->tlv, sizeof(abi_hdr)))
668 		return -EFAULT;
669 
670 	if (abi_hdr.magic != SOF_IPC4_ABI_MAGIC) {
671 		dev_err_ratelimited(scomp->dev, "Wrong ABI magic 0x%08x\n",
672 				    abi_hdr.magic);
673 		return -EINVAL;
674 	}
675 
676 	if (abi_hdr.size > scontrol->max_size - sizeof(abi_hdr)) {
677 		dev_err_ratelimited(scomp->dev,
678 				    "%u bytes of control data is invalid, max is %zu\n",
679 				    abi_hdr.size, scontrol->max_size - sizeof(abi_hdr));
680 		return -EINVAL;
681 	}
682 
683 	if (!scontrol->old_ipc_control_data) {
684 		/* Create a backup of the current, valid bytes control */
685 		scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data,
686 							 scontrol->size, GFP_KERNEL);
687 		if (!scontrol->old_ipc_control_data)
688 			return -ENOMEM;
689 	}
690 
691 	/* Copy the whole binary data which includes the ABI header and the payload */
692 	if (copy_from_user(data, tlvd->tlv, header.length)) {
693 		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
694 		       scontrol->size);
695 		kfree(scontrol->old_ipc_control_data);
696 		scontrol->old_ipc_control_data = NULL;
697 		return -EFAULT;
698 	}
699 
700 	/* Update the cdata size */
701 	scontrol->size = sizeof(*cdata) + header.length;
702 
703 	return sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
704 }
705 
706 static int _sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
707 				   const unsigned int __user *binary_data,
708 				   unsigned int size, bool from_dsp)
709 {
710 	struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
711 	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
712 	struct snd_soc_component *scomp = scontrol->scomp;
713 	struct sof_abi_hdr *data = cdata->data;
714 	struct snd_ctl_tlv header;
715 	size_t data_size;
716 
717 	/*
718 	 * Decrement the limit by ext bytes header size to ensure the user space
719 	 * buffer is not exceeded.
720 	 */
721 	if (size < sizeof(struct snd_ctl_tlv))
722 		return -ENOSPC;
723 
724 	size -= sizeof(struct snd_ctl_tlv);
725 
726 	/* get all the component data from DSP */
727 	if (from_dsp) {
728 		struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
729 		int ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, false, true);
730 
731 		if (ret < 0)
732 			return ret;
733 
734 		/* Set the ABI magic (if the control is not initialized) */
735 		data->magic = SOF_IPC4_ABI_MAGIC;
736 	}
737 
738 	if (data->size > scontrol->max_size - sizeof(*data)) {
739 		dev_err_ratelimited(scomp->dev,
740 				    "%u bytes of control data is invalid, max is %zu\n",
741 				    data->size, scontrol->max_size - sizeof(*data));
742 		return -EINVAL;
743 	}
744 
745 	data_size = data->size + sizeof(struct sof_abi_hdr);
746 
747 	/* make sure we don't exceed size provided by user space for data */
748 	if (data_size > size)
749 		return -ENOSPC;
750 
751 	/* Set header id and length */
752 	header.numid = SOF_CTRL_CMD_BINARY;
753 	header.length = data_size;
754 
755 	if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv)))
756 		return -EFAULT;
757 
758 	if (copy_to_user(tlvd->tlv, data, data_size))
759 		return -EFAULT;
760 
761 	return 0;
762 }
763 
764 static int sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
765 				  const unsigned int __user *binary_data,
766 				  unsigned int size)
767 {
768 	sof_ipc4_refresh_bytes_control(scontrol, true);
769 
770 	return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, false);
771 }
772 
773 static int sof_ipc4_bytes_ext_volatile_get(struct snd_sof_control *scontrol,
774 					   const unsigned int __user *binary_data,
775 					   unsigned int size)
776 {
777 	return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, true);
778 }
779 
780 static int
781 sof_ipc4_volsw_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
782 		     struct snd_sof_control *scontrol)
783 {
784 	if (scontrol->max == 1)
785 		return sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, false);
786 
787 	return sof_ipc4_set_volume_data(sdev, swidget, scontrol, false);
788 }
789 
790 #define PARAM_ID_FROM_EXTENSION(_ext)	(((_ext) & SOF_IPC4_MOD_EXT_MSG_PARAM_ID_MASK)	\
791 					 >> SOF_IPC4_MOD_EXT_MSG_PARAM_ID_SHIFT)
792 
793 static void sof_ipc4_control_update(struct snd_sof_dev *sdev, void *ipc_message)
794 {
795 	struct sof_ipc4_msg *ipc4_msg = ipc_message;
796 	struct sof_ipc4_notify_module_data *ndata = ipc4_msg->data_ptr;
797 	struct sof_ipc4_control_msg_payload *msg_data;
798 	struct sof_ipc4_control_data *cdata;
799 	struct snd_soc_dapm_widget *widget;
800 	struct snd_sof_control *scontrol;
801 	struct snd_sof_widget *swidget;
802 	struct snd_kcontrol *kc = NULL;
803 	bool scontrol_found = false;
804 	u32 event_param_id;
805 	int i, type;
806 
807 	if (ndata->event_data_size < sizeof(*msg_data)) {
808 		dev_err(sdev->dev,
809 			"%s: Invalid event data size for module %u.%u: %u\n",
810 			__func__, ndata->module_id, ndata->instance_id,
811 			ndata->event_data_size);
812 		return;
813 	}
814 
815 	event_param_id = ndata->event_id & SOF_IPC4_NOTIFY_MODULE_EVENTID_ALSA_PARAMID_MASK;
816 	switch (event_param_id) {
817 	case SOF_IPC4_SWITCH_CONTROL_PARAM_ID:
818 		type = SND_SOC_TPLG_TYPE_MIXER;
819 		break;
820 	case SOF_IPC4_ENUM_CONTROL_PARAM_ID:
821 		type = SND_SOC_TPLG_TYPE_ENUM;
822 		break;
823 	case SOF_IPC4_BYTES_CONTROL_PARAM_ID:
824 		type = SND_SOC_TPLG_TYPE_BYTES;
825 		break;
826 	default:
827 		dev_err(sdev->dev,
828 			"%s: Invalid control type for module %u.%u: %u\n",
829 			__func__, ndata->module_id, ndata->instance_id,
830 			event_param_id);
831 		return;
832 	}
833 
834 	/* Find the swidget based on ndata->module_id and ndata->instance_id */
835 	swidget = sof_ipc4_find_swidget_by_ids(sdev, ndata->module_id,
836 					       ndata->instance_id);
837 	if (!swidget) {
838 		dev_err(sdev->dev, "%s: Failed to find widget for module %u.%u\n",
839 			__func__, ndata->module_id, ndata->instance_id);
840 		return;
841 	}
842 
843 	/* Find the scontrol which is the source of the notification */
844 	msg_data = (struct sof_ipc4_control_msg_payload *)ndata->event_data;
845 	list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
846 		if (scontrol->comp_id == swidget->comp_id) {
847 			u32 local_param_id;
848 
849 			cdata = scontrol->ipc_control_data;
850 			/*
851 			 * The scontrol's param_id is stored in the IPC message
852 			 * template's extension
853 			 */
854 			local_param_id = PARAM_ID_FROM_EXTENSION(cdata->msg.extension);
855 			if (local_param_id == event_param_id &&
856 			    msg_data->id == cdata->index) {
857 				scontrol_found = true;
858 				break;
859 			}
860 		}
861 	}
862 
863 	if (!scontrol_found) {
864 		dev_err(sdev->dev,
865 			"%s: Failed to find control on widget %s: %u:%u\n",
866 			__func__, swidget->widget->name, ndata->event_id & 0xffff,
867 			msg_data->id);
868 		return;
869 	}
870 
871 	if (msg_data->num_elems) {
872 		/*
873 		 * The message includes the updated value/data, update the
874 		 * control's local cache using the received notification
875 		 */
876 		if (type == SND_SOC_TPLG_TYPE_BYTES) {
877 			struct sof_abi_hdr *data = cdata->data;
878 			size_t source_size = struct_size(msg_data, data, msg_data->num_elems);
879 
880 			if (source_size > ndata->event_data_size) {
881 				dev_warn(sdev->dev,
882 					 "%s: invalid bytes notification size for %s (%zu, %u)\n",
883 					 __func__, scontrol->name, source_size,
884 					 ndata->event_data_size);
885 				scontrol->comp_data_dirty = true;
886 				goto notify;
887 			}
888 
889 			if (msg_data->num_elems > scontrol->max_size - sizeof(*data)) {
890 				dev_warn(sdev->dev,
891 					 "%s: no space for data in %s (%u, %zu)\n",
892 					 __func__, scontrol->name, msg_data->num_elems,
893 					 scontrol->max_size - sizeof(*data));
894 			} else {
895 				memcpy(data->data, msg_data->data, msg_data->num_elems);
896 				data->size = msg_data->num_elems;
897 				scontrol->size = sizeof(*cdata) + sizeof(*data) + data->size;
898 			}
899 		} else {
900 			size_t source_size = struct_size(msg_data, chanv, msg_data->num_elems);
901 
902 			if (source_size > ndata->event_data_size) {
903 				dev_warn(sdev->dev,
904 					 "%s: invalid channel notification size for %s (%zu, %u)\n",
905 					 __func__, scontrol->name, source_size,
906 					 ndata->event_data_size);
907 				scontrol->comp_data_dirty = true;
908 				goto notify;
909 			}
910 
911 			for (i = 0; i < msg_data->num_elems; i++) {
912 				u32 channel = msg_data->chanv[i].channel;
913 
914 				if (channel >= scontrol->num_channels) {
915 					dev_warn(sdev->dev,
916 						 "Invalid channel index for %s: %u\n",
917 						 scontrol->name, i);
918 
919 					/*
920 					 * Mark the scontrol as dirty to force a refresh
921 					 * on next read
922 					 */
923 					scontrol->comp_data_dirty = true;
924 					break;
925 				}
926 
927 				cdata->chanv[channel].value = msg_data->chanv[i].value;
928 			}
929 		}
930 	} else {
931 		/*
932 		 * Mark the scontrol as dirty because the value/data is changed
933 		 * in firmware, forcing a refresh on next read access
934 		 */
935 		scontrol->comp_data_dirty = true;
936 	}
937 
938 notify:
939 
940 	/*
941 	 * Look up the ALSA kcontrol of the scontrol to be able to send a
942 	 * notification to user space
943 	 */
944 	widget = swidget->widget;
945 	for (i = 0; i < widget->num_kcontrols; i++) {
946 		/* skip non matching types or non matching indexes within type */
947 		if (widget->dobj.widget.kcontrol_type[i] == type &&
948 		    widget->kcontrol_news[i].index == cdata->index) {
949 			kc = widget->kcontrols[i];
950 			break;
951 		}
952 	}
953 
954 	if (!kc)
955 		return;
956 
957 	snd_ctl_notify_one(swidget->scomp->card->snd_card,
958 			   SNDRV_CTL_EVENT_MASK_VALUE, kc, 0);
959 }
960 
961 /* set up all controls for the widget */
962 static int sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
963 {
964 	struct snd_sof_control *scontrol;
965 	int ret = 0;
966 
967 	list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
968 		if (scontrol->comp_id == swidget->comp_id) {
969 			switch (scontrol->info_type) {
970 			case SND_SOC_TPLG_CTL_VOLSW:
971 			case SND_SOC_TPLG_CTL_VOLSW_SX:
972 			case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
973 				ret = sof_ipc4_volsw_setup(sdev, swidget, scontrol);
974 				break;
975 			case SND_SOC_TPLG_CTL_BYTES:
976 				ret = sof_ipc4_set_get_bytes_data(sdev, scontrol,
977 								  true, false);
978 				break;
979 			case SND_SOC_TPLG_CTL_ENUM:
980 			case SND_SOC_TPLG_CTL_ENUM_VALUE:
981 				ret = sof_ipc4_set_generic_control_data(sdev, swidget,
982 									scontrol, false);
983 				break;
984 			default:
985 				break;
986 			}
987 
988 			if (ret < 0) {
989 				dev_err(sdev->dev,
990 					"kcontrol %d set up failed for widget %s\n",
991 					scontrol->comp_id, swidget->widget->name);
992 				return ret;
993 			}
994 		}
995 	}
996 
997 	return 0;
998 }
999 
1000 static int
1001 sof_ipc4_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
1002 {
1003 	int i;
1004 
1005 	/* init the volume table */
1006 	scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
1007 	if (!scontrol->volume_table)
1008 		return -ENOMEM;
1009 
1010 	/* populate the volume table */
1011 	for (i = 0; i < size ; i++) {
1012 		u32 val = vol_compute_gain(i, tlv);
1013 		u64 q31val = ((u64)val) << 15; /* Can be over Q1.31, need to saturate */
1014 
1015 		scontrol->volume_table[i] = q31val > SOF_IPC4_VOL_ZERO_DB ?
1016 						SOF_IPC4_VOL_ZERO_DB : q31val;
1017 	}
1018 
1019 	return 0;
1020 }
1021 
1022 const struct sof_ipc_tplg_control_ops tplg_ipc4_control_ops = {
1023 	.volume_put = sof_ipc4_volume_put,
1024 	.volume_get = sof_ipc4_volume_get,
1025 	.switch_put = sof_ipc4_switch_put,
1026 	.switch_get = sof_ipc4_switch_get,
1027 	.enum_put = sof_ipc4_enum_put,
1028 	.enum_get = sof_ipc4_enum_get,
1029 	.bytes_put = sof_ipc4_bytes_put,
1030 	.bytes_get = sof_ipc4_bytes_get,
1031 	.bytes_ext_put = sof_ipc4_bytes_ext_put,
1032 	.bytes_ext_get = sof_ipc4_bytes_ext_get,
1033 	.bytes_ext_volatile_get = sof_ipc4_bytes_ext_volatile_get,
1034 	.update = sof_ipc4_control_update,
1035 	.widget_kcontrol_setup = sof_ipc4_widget_kcontrol_setup,
1036 	.set_up_volume_table = sof_ipc4_set_up_volume_table,
1037 };
1038