xref: /openbmc/linux/sound/soc/soc-topology.c (revision d47a97bd)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // soc-topology.c  --  ALSA SoC Topology
4 //
5 // Copyright (C) 2012 Texas Instruments Inc.
6 // Copyright (C) 2015 Intel Corporation.
7 //
8 // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //		K, Mythri P <mythri.p.k@intel.com>
10 //		Prusty, Subhransu S <subhransu.s.prusty@intel.com>
11 //		B, Jayachandran <jayachandran.b@intel.com>
12 //		Abdullah, Omair M <omair.m.abdullah@intel.com>
13 //		Jin, Yao <yao.jin@intel.com>
14 //		Lin, Mengdong <mengdong.lin@intel.com>
15 //
16 //  Add support to read audio firmware topology alongside firmware text. The
17 //  topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
18 //  equalizers, firmware, coefficients etc.
19 //
20 //  This file only manages the core ALSA and ASoC components, all other bespoke
21 //  firmware topology data is passed to component drivers for bespoke handling.
22 
23 #include <linux/kernel.h>
24 #include <linux/export.h>
25 #include <linux/list.h>
26 #include <linux/firmware.h>
27 #include <linux/slab.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/soc-topology.h>
31 #include <sound/tlv.h>
32 
33 #define SOC_TPLG_MAGIC_BIG_ENDIAN            0x436F5341 /* ASoC in reverse */
34 
35 /*
36  * We make several passes over the data (since it wont necessarily be ordered)
37  * and process objects in the following order. This guarantees the component
38  * drivers will be ready with any vendor data before the mixers and DAPM objects
39  * are loaded (that may make use of the vendor data).
40  */
41 #define SOC_TPLG_PASS_MANIFEST		0
42 #define SOC_TPLG_PASS_VENDOR		1
43 #define SOC_TPLG_PASS_CONTROL		2
44 #define SOC_TPLG_PASS_WIDGET		3
45 #define SOC_TPLG_PASS_PCM_DAI		4
46 #define SOC_TPLG_PASS_GRAPH		5
47 #define SOC_TPLG_PASS_BE_DAI		6
48 #define SOC_TPLG_PASS_LINK		7
49 
50 #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
51 #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_LINK
52 
53 /* topology context */
54 struct soc_tplg {
55 	const struct firmware *fw;
56 
57 	/* runtime FW parsing */
58 	const u8 *pos;		/* read position */
59 	const u8 *hdr_pos;	/* header position */
60 	unsigned int pass;	/* pass number */
61 
62 	/* component caller */
63 	struct device *dev;
64 	struct snd_soc_component *comp;
65 	u32 index;	/* current block index */
66 
67 	/* vendor specific kcontrol operations */
68 	const struct snd_soc_tplg_kcontrol_ops *io_ops;
69 	int io_ops_count;
70 
71 	/* vendor specific bytes ext handlers, for TLV bytes controls */
72 	const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
73 	int bytes_ext_ops_count;
74 
75 	/* optional fw loading callbacks to component drivers */
76 	struct snd_soc_tplg_ops *ops;
77 };
78 
79 /* check we dont overflow the data for this control chunk */
80 static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
81 	unsigned int count, size_t bytes, const char *elem_type)
82 {
83 	const u8 *end = tplg->pos + elem_size * count;
84 
85 	if (end > tplg->fw->data + tplg->fw->size) {
86 		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
87 			elem_type);
88 		return -EINVAL;
89 	}
90 
91 	/* check there is enough room in chunk for control.
92 	   extra bytes at the end of control are for vendor data here  */
93 	if (elem_size * count > bytes) {
94 		dev_err(tplg->dev,
95 			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
96 			elem_type, count, elem_size, bytes);
97 		return -EINVAL;
98 	}
99 
100 	return 0;
101 }
102 
103 static inline bool soc_tplg_is_eof(struct soc_tplg *tplg)
104 {
105 	const u8 *end = tplg->hdr_pos;
106 
107 	if (end >= tplg->fw->data + tplg->fw->size)
108 		return true;
109 	return false;
110 }
111 
112 static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
113 {
114 	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
115 }
116 
117 static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
118 {
119 	return (unsigned long)(tplg->pos - tplg->fw->data);
120 }
121 
122 /* mapping of Kcontrol types and associated operations. */
123 static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
124 	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
125 		snd_soc_put_volsw, snd_soc_info_volsw},
126 	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
127 		snd_soc_put_volsw_sx, NULL},
128 	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
129 		snd_soc_put_enum_double, snd_soc_info_enum_double},
130 	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
131 		snd_soc_put_enum_double, NULL},
132 	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
133 		snd_soc_bytes_put, snd_soc_bytes_info},
134 	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
135 		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
136 	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
137 		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
138 	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
139 		snd_soc_put_strobe, NULL},
140 	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
141 		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
142 	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
143 		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
144 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
145 		snd_soc_dapm_put_enum_double, NULL},
146 	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
147 		snd_soc_dapm_put_enum_double, NULL},
148 	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
149 		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
150 };
151 
152 struct soc_tplg_map {
153 	int uid;
154 	int kid;
155 };
156 
157 /* mapping of widget types from UAPI IDs to kernel IDs */
158 static const struct soc_tplg_map dapm_map[] = {
159 	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
160 	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
161 	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
162 	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
163 	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
164 	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
165 	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
166 	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
167 	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
168 	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
169 	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
170 	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
171 	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
172 	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
173 	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
174 	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
175 	{SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
176 	{SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
177 	{SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
178 	{SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
179 	{SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
180 	{SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
181 	{SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
182 	{SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
183 };
184 
185 static int tplg_chan_get_reg(struct soc_tplg *tplg,
186 	struct snd_soc_tplg_channel *chan, int map)
187 {
188 	int i;
189 
190 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
191 		if (le32_to_cpu(chan[i].id) == map)
192 			return le32_to_cpu(chan[i].reg);
193 	}
194 
195 	return -EINVAL;
196 }
197 
198 static int tplg_chan_get_shift(struct soc_tplg *tplg,
199 	struct snd_soc_tplg_channel *chan, int map)
200 {
201 	int i;
202 
203 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
204 		if (le32_to_cpu(chan[i].id) == map)
205 			return le32_to_cpu(chan[i].shift);
206 	}
207 
208 	return -EINVAL;
209 }
210 
211 static int get_widget_id(int tplg_type)
212 {
213 	int i;
214 
215 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
216 		if (tplg_type == dapm_map[i].uid)
217 			return dapm_map[i].kid;
218 	}
219 
220 	return -EINVAL;
221 }
222 
223 static inline void soc_bind_err(struct soc_tplg *tplg,
224 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
225 {
226 	dev_err(tplg->dev,
227 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
228 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
229 		soc_tplg_get_offset(tplg));
230 }
231 
232 static inline void soc_control_err(struct soc_tplg *tplg,
233 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
234 {
235 	dev_err(tplg->dev,
236 		"ASoC: no complete control IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
237 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
238 		soc_tplg_get_offset(tplg));
239 }
240 
241 /* pass vendor data to component driver for processing */
242 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
243 				struct snd_soc_tplg_hdr *hdr)
244 {
245 	int ret = 0;
246 
247 	if (tplg->ops && tplg->ops->vendor_load)
248 		ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
249 	else {
250 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
251 			hdr->vendor_type);
252 		return -EINVAL;
253 	}
254 
255 	if (ret < 0)
256 		dev_err(tplg->dev,
257 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
258 			soc_tplg_get_hdr_offset(tplg),
259 			soc_tplg_get_hdr_offset(tplg),
260 			hdr->type, hdr->vendor_type);
261 	return ret;
262 }
263 
264 /* optionally pass new dynamic widget to component driver. This is mainly for
265  * external widgets where we can assign private data/ops */
266 static int soc_tplg_widget_load(struct soc_tplg *tplg,
267 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
268 {
269 	if (tplg->ops && tplg->ops->widget_load)
270 		return tplg->ops->widget_load(tplg->comp, tplg->index, w,
271 			tplg_w);
272 
273 	return 0;
274 }
275 
276 /* optionally pass new dynamic widget to component driver. This is mainly for
277  * external widgets where we can assign private data/ops */
278 static int soc_tplg_widget_ready(struct soc_tplg *tplg,
279 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
280 {
281 	if (tplg->ops && tplg->ops->widget_ready)
282 		return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
283 			tplg_w);
284 
285 	return 0;
286 }
287 
288 /* pass DAI configurations to component driver for extra initialization */
289 static int soc_tplg_dai_load(struct soc_tplg *tplg,
290 	struct snd_soc_dai_driver *dai_drv,
291 	struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
292 {
293 	if (tplg->ops && tplg->ops->dai_load)
294 		return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
295 			pcm, dai);
296 
297 	return 0;
298 }
299 
300 /* pass link configurations to component driver for extra initialization */
301 static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
302 	struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
303 {
304 	if (tplg->ops && tplg->ops->link_load)
305 		return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
306 
307 	return 0;
308 }
309 
310 /* tell the component driver that all firmware has been loaded in this request */
311 static int soc_tplg_complete(struct soc_tplg *tplg)
312 {
313 	if (tplg->ops && tplg->ops->complete)
314 		return tplg->ops->complete(tplg->comp);
315 
316 	return 0;
317 }
318 
319 /* add a dynamic kcontrol */
320 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
321 	const struct snd_kcontrol_new *control_new, const char *prefix,
322 	void *data, struct snd_kcontrol **kcontrol)
323 {
324 	int err;
325 
326 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
327 	if (*kcontrol == NULL) {
328 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
329 		control_new->name);
330 		return -ENOMEM;
331 	}
332 
333 	err = snd_ctl_add(card, *kcontrol);
334 	if (err < 0) {
335 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
336 			control_new->name, err);
337 		return err;
338 	}
339 
340 	return 0;
341 }
342 
343 /* add a dynamic kcontrol for component driver */
344 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
345 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
346 {
347 	struct snd_soc_component *comp = tplg->comp;
348 
349 	return soc_tplg_add_dcontrol(comp->card->snd_card,
350 				tplg->dev, k, comp->name_prefix, comp, kcontrol);
351 }
352 
353 /* remove kcontrol */
354 static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj,
355 				     int pass)
356 {
357 	struct snd_card *card = comp->card->snd_card;
358 
359 	if (pass != SOC_TPLG_PASS_CONTROL)
360 		return;
361 
362 	if (dobj->unload)
363 		dobj->unload(comp, dobj);
364 
365 	snd_ctl_remove(card, dobj->control.kcontrol);
366 	list_del(&dobj->list);
367 }
368 
369 /* remove a route */
370 static void soc_tplg_remove_route(struct snd_soc_component *comp,
371 			 struct snd_soc_dobj *dobj, int pass)
372 {
373 	if (pass != SOC_TPLG_PASS_GRAPH)
374 		return;
375 
376 	if (dobj->unload)
377 		dobj->unload(comp, dobj);
378 
379 	list_del(&dobj->list);
380 }
381 
382 /* remove a widget and it's kcontrols - routes must be removed first */
383 static void soc_tplg_remove_widget(struct snd_soc_component *comp,
384 	struct snd_soc_dobj *dobj, int pass)
385 {
386 	struct snd_card *card = comp->card->snd_card;
387 	struct snd_soc_dapm_widget *w =
388 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
389 	int i;
390 
391 	if (pass != SOC_TPLG_PASS_WIDGET)
392 		return;
393 
394 	if (dobj->unload)
395 		dobj->unload(comp, dobj);
396 
397 	if (!w->kcontrols)
398 		goto free_news;
399 
400 	for (i = 0; w->kcontrols && i < w->num_kcontrols; i++)
401 		snd_ctl_remove(card, w->kcontrols[i]);
402 
403 free_news:
404 
405 	list_del(&dobj->list);
406 
407 	/* widget w is freed by soc-dapm.c */
408 }
409 
410 /* remove DAI configurations */
411 static void soc_tplg_remove_dai(struct snd_soc_component *comp,
412 	struct snd_soc_dobj *dobj, int pass)
413 {
414 	struct snd_soc_dai_driver *dai_drv =
415 		container_of(dobj, struct snd_soc_dai_driver, dobj);
416 	struct snd_soc_dai *dai, *_dai;
417 
418 	if (pass != SOC_TPLG_PASS_PCM_DAI)
419 		return;
420 
421 	if (dobj->unload)
422 		dobj->unload(comp, dobj);
423 
424 	for_each_component_dais_safe(comp, dai, _dai)
425 		if (dai->driver == dai_drv)
426 			snd_soc_unregister_dai(dai);
427 
428 	list_del(&dobj->list);
429 }
430 
431 /* remove link configurations */
432 static void soc_tplg_remove_link(struct snd_soc_component *comp,
433 	struct snd_soc_dobj *dobj, int pass)
434 {
435 	struct snd_soc_dai_link *link =
436 		container_of(dobj, struct snd_soc_dai_link, dobj);
437 
438 	if (pass != SOC_TPLG_PASS_PCM_DAI)
439 		return;
440 
441 	if (dobj->unload)
442 		dobj->unload(comp, dobj);
443 
444 	list_del(&dobj->list);
445 	snd_soc_remove_pcm_runtime(comp->card,
446 			snd_soc_get_pcm_runtime(comp->card, link));
447 }
448 
449 /* unload dai link */
450 static void remove_backend_link(struct snd_soc_component *comp,
451 	struct snd_soc_dobj *dobj, int pass)
452 {
453 	if (pass != SOC_TPLG_PASS_LINK)
454 		return;
455 
456 	if (dobj->unload)
457 		dobj->unload(comp, dobj);
458 
459 	/*
460 	 * We don't free the link here as what soc_tplg_remove_link() do since BE
461 	 * links are not allocated by topology.
462 	 * We however need to reset the dobj type to its initial values
463 	 */
464 	dobj->type = SND_SOC_DOBJ_NONE;
465 	list_del(&dobj->list);
466 }
467 
468 /* bind a kcontrol to it's IO handlers */
469 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
470 	struct snd_kcontrol_new *k,
471 	const struct soc_tplg *tplg)
472 {
473 	const struct snd_soc_tplg_kcontrol_ops *ops;
474 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
475 	int num_ops, i;
476 
477 	if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
478 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
479 		&& (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ
480 		    || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
481 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
482 		struct soc_bytes_ext *sbe;
483 		struct snd_soc_tplg_bytes_control *be;
484 
485 		sbe = (struct soc_bytes_ext *)k->private_value;
486 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
487 
488 		/* TLV bytes controls need standard kcontrol info handler,
489 		 * TLV callback and extended put/get handlers.
490 		 */
491 		k->info = snd_soc_bytes_info_ext;
492 		k->tlv.c = snd_soc_bytes_tlv_callback;
493 
494 		/*
495 		 * When a topology-based implementation abuses the
496 		 * control interface and uses bytes_ext controls of
497 		 * more than 512 bytes, we need to disable the size
498 		 * checks, otherwise accesses to such controls will
499 		 * return an -EINVAL error and prevent the card from
500 		 * being configured.
501 		 */
502 		if (sbe->max > 512)
503 			k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
504 
505 		ext_ops = tplg->bytes_ext_ops;
506 		num_ops = tplg->bytes_ext_ops_count;
507 		for (i = 0; i < num_ops; i++) {
508 			if (!sbe->put &&
509 			    ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
510 				sbe->put = ext_ops[i].put;
511 			if (!sbe->get &&
512 			    ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
513 				sbe->get = ext_ops[i].get;
514 		}
515 
516 		if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
517 			return -EINVAL;
518 		if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
519 			return -EINVAL;
520 		return 0;
521 	}
522 
523 	/* try and map vendor specific kcontrol handlers first */
524 	ops = tplg->io_ops;
525 	num_ops = tplg->io_ops_count;
526 	for (i = 0; i < num_ops; i++) {
527 
528 		if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
529 			k->put = ops[i].put;
530 		if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
531 			k->get = ops[i].get;
532 		if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
533 			k->info = ops[i].info;
534 	}
535 
536 	/* vendor specific handlers found ? */
537 	if (k->put && k->get && k->info)
538 		return 0;
539 
540 	/* none found so try standard kcontrol handlers */
541 	ops = io_ops;
542 	num_ops = ARRAY_SIZE(io_ops);
543 	for (i = 0; i < num_ops; i++) {
544 
545 		if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
546 			k->put = ops[i].put;
547 		if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
548 			k->get = ops[i].get;
549 		if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
550 			k->info = ops[i].info;
551 	}
552 
553 	/* standard handlers found ? */
554 	if (k->put && k->get && k->info)
555 		return 0;
556 
557 	/* nothing to bind */
558 	return -EINVAL;
559 }
560 
561 /* bind a widgets to it's evnt handlers */
562 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
563 		const struct snd_soc_tplg_widget_events *events,
564 		int num_events, u16 event_type)
565 {
566 	int i;
567 
568 	w->event = NULL;
569 
570 	for (i = 0; i < num_events; i++) {
571 		if (event_type == events[i].type) {
572 
573 			/* found - so assign event */
574 			w->event = events[i].event_handler;
575 			return 0;
576 		}
577 	}
578 
579 	/* not found */
580 	return -EINVAL;
581 }
582 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
583 
584 /* optionally pass new dynamic kcontrol to component driver. */
585 static int soc_tplg_control_load(struct soc_tplg *tplg,
586 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
587 {
588 	if (tplg->ops && tplg->ops->control_load)
589 		return tplg->ops->control_load(tplg->comp, tplg->index, k,
590 			hdr);
591 
592 	return 0;
593 }
594 
595 
596 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
597 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
598 {
599 	unsigned int item_len = 2 * sizeof(unsigned int);
600 	unsigned int *p;
601 
602 	p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
603 	if (!p)
604 		return -ENOMEM;
605 
606 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
607 	p[1] = item_len;
608 	p[2] = le32_to_cpu(scale->min);
609 	p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
610 		| (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
611 
612 	kc->tlv.p = (void *)p;
613 	return 0;
614 }
615 
616 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
617 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
618 {
619 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
620 	u32 access = le32_to_cpu(tc->access);
621 
622 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
623 		return 0;
624 
625 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
626 		tplg_tlv = &tc->tlv;
627 		switch (le32_to_cpu(tplg_tlv->type)) {
628 		case SNDRV_CTL_TLVT_DB_SCALE:
629 			return soc_tplg_create_tlv_db_scale(tplg, kc,
630 					&tplg_tlv->scale);
631 
632 		/* TODO: add support for other TLV types */
633 		default:
634 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
635 					tplg_tlv->type);
636 			return -EINVAL;
637 		}
638 	}
639 
640 	return 0;
641 }
642 
643 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size)
644 {
645 	struct snd_soc_tplg_bytes_control *be;
646 	struct soc_bytes_ext *sbe;
647 	struct snd_kcontrol_new kc;
648 	int ret = 0;
649 
650 	if (soc_tplg_check_elem_count(tplg,
651 				      sizeof(struct snd_soc_tplg_bytes_control),
652 				      1, size, "mixer bytes"))
653 		return -EINVAL;
654 
655 	be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
656 
657 	/* validate kcontrol */
658 	if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
659 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
660 		return -EINVAL;
661 
662 	sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
663 	if (sbe == NULL)
664 		return -ENOMEM;
665 
666 	tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
667 		      le32_to_cpu(be->priv.size));
668 
669 	dev_dbg(tplg->dev,
670 		"ASoC: adding bytes kcontrol %s with access 0x%x\n",
671 		be->hdr.name, be->hdr.access);
672 
673 	memset(&kc, 0, sizeof(kc));
674 	kc.name = be->hdr.name;
675 	kc.private_value = (long)sbe;
676 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
677 	kc.access = le32_to_cpu(be->hdr.access);
678 
679 	sbe->max = le32_to_cpu(be->max);
680 	sbe->dobj.type = SND_SOC_DOBJ_BYTES;
681 	if (tplg->ops)
682 		sbe->dobj.unload = tplg->ops->control_unload;
683 	INIT_LIST_HEAD(&sbe->dobj.list);
684 
685 	/* map io handlers */
686 	ret = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
687 	if (ret) {
688 		soc_control_err(tplg, &be->hdr, be->hdr.name);
689 		goto err;
690 	}
691 
692 	/* pass control to driver for optional further init */
693 	ret = soc_tplg_control_load(tplg, &kc, &be->hdr);
694 	if (ret < 0) {
695 		dev_err(tplg->dev, "ASoC: failed to init %s\n", be->hdr.name);
696 		goto err;
697 	}
698 
699 	/* register control here */
700 	ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol);
701 	if (ret < 0) {
702 		dev_err(tplg->dev, "ASoC: failed to add %s\n", be->hdr.name);
703 		goto err;
704 	}
705 
706 	list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
707 
708 err:
709 	return ret;
710 }
711 
712 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size)
713 {
714 	struct snd_soc_tplg_mixer_control *mc;
715 	struct soc_mixer_control *sm;
716 	struct snd_kcontrol_new kc;
717 	int ret = 0;
718 
719 	if (soc_tplg_check_elem_count(tplg,
720 				      sizeof(struct snd_soc_tplg_mixer_control),
721 				      1, size, "mixers"))
722 		return -EINVAL;
723 
724 	mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
725 
726 	/* validate kcontrol */
727 	if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
728 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
729 		return -EINVAL;
730 
731 	sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
732 	if (sm == NULL)
733 		return -ENOMEM;
734 	tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
735 		      le32_to_cpu(mc->priv.size));
736 
737 	dev_dbg(tplg->dev,
738 		"ASoC: adding mixer kcontrol %s with access 0x%x\n",
739 		mc->hdr.name, mc->hdr.access);
740 
741 	memset(&kc, 0, sizeof(kc));
742 	kc.name = mc->hdr.name;
743 	kc.private_value = (long)sm;
744 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
745 	kc.access = le32_to_cpu(mc->hdr.access);
746 
747 	/* we only support FL/FR channel mapping atm */
748 	sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL);
749 	sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR);
750 	sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL);
751 	sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR);
752 
753 	sm->max = le32_to_cpu(mc->max);
754 	sm->min = le32_to_cpu(mc->min);
755 	sm->invert = le32_to_cpu(mc->invert);
756 	sm->platform_max = le32_to_cpu(mc->platform_max);
757 	sm->dobj.index = tplg->index;
758 	sm->dobj.type = SND_SOC_DOBJ_MIXER;
759 	if (tplg->ops)
760 		sm->dobj.unload = tplg->ops->control_unload;
761 	INIT_LIST_HEAD(&sm->dobj.list);
762 
763 	/* map io handlers */
764 	ret = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
765 	if (ret) {
766 		soc_control_err(tplg, &mc->hdr, mc->hdr.name);
767 		goto err;
768 	}
769 
770 	/* create any TLV data */
771 	ret = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
772 	if (ret < 0) {
773 		dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name);
774 		goto err;
775 	}
776 
777 	/* pass control to driver for optional further init */
778 	ret = soc_tplg_control_load(tplg, &kc, &mc->hdr);
779 	if (ret < 0) {
780 		dev_err(tplg->dev, "ASoC: failed to init %s\n", mc->hdr.name);
781 		goto err;
782 	}
783 
784 	/* register control here */
785 	ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol);
786 	if (ret < 0) {
787 		dev_err(tplg->dev, "ASoC: failed to add %s\n", mc->hdr.name);
788 		goto err;
789 	}
790 
791 	list_add(&sm->dobj.list, &tplg->comp->dobj_list);
792 
793 err:
794 	return ret;
795 }
796 
797 static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se,
798 				       struct snd_soc_tplg_enum_control *ec)
799 {
800 	int i, ret;
801 
802 	if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts))
803 		return -EINVAL;
804 
805 	se->dobj.control.dtexts =
806 		devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
807 	if (se->dobj.control.dtexts == NULL)
808 		return -ENOMEM;
809 
810 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
811 
812 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
813 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
814 			ret = -EINVAL;
815 			goto err;
816 		}
817 
818 		se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL);
819 		if (!se->dobj.control.dtexts[i]) {
820 			ret = -ENOMEM;
821 			goto err;
822 		}
823 	}
824 
825 	se->items = le32_to_cpu(ec->items);
826 	se->texts = (const char * const *)se->dobj.control.dtexts;
827 	return 0;
828 
829 err:
830 	return ret;
831 }
832 
833 static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se,
834 					struct snd_soc_tplg_enum_control *ec)
835 {
836 	int i;
837 
838 	/*
839 	 * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS
840 	 * values instead of using ARRAY_SIZE(ec->values) due to the fact that
841 	 * it is oversized for its purpose. Additionally it is done so because
842 	 * it is defined in UAPI header where it can't be easily changed.
843 	 */
844 	if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS)
845 		return -EINVAL;
846 
847 	se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items),
848 					   sizeof(*se->dobj.control.dvalues),
849 					   GFP_KERNEL);
850 	if (!se->dobj.control.dvalues)
851 		return -ENOMEM;
852 
853 	/* convert from little-endian */
854 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
855 		se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
856 	}
857 
858 	return 0;
859 }
860 
861 static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
862 {
863 	struct snd_soc_tplg_enum_control *ec;
864 	struct soc_enum *se;
865 	struct snd_kcontrol_new kc;
866 	int ret = 0;
867 
868 	if (soc_tplg_check_elem_count(tplg,
869 				      sizeof(struct snd_soc_tplg_enum_control),
870 				      1, size, "enums"))
871 		return -EINVAL;
872 
873 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
874 
875 	/* validate kcontrol */
876 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
877 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
878 		return -EINVAL;
879 
880 	se = devm_kzalloc(tplg->dev, (sizeof(*se)), GFP_KERNEL);
881 	if (se == NULL)
882 		return -ENOMEM;
883 
884 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
885 		      le32_to_cpu(ec->priv.size));
886 
887 	dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
888 		ec->hdr.name, ec->items);
889 
890 	memset(&kc, 0, sizeof(kc));
891 	kc.name = ec->hdr.name;
892 	kc.private_value = (long)se;
893 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
894 	kc.access = le32_to_cpu(ec->hdr.access);
895 
896 	se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
897 	se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
898 		SNDRV_CHMAP_FL);
899 	se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
900 		SNDRV_CHMAP_FL);
901 
902 	se->mask = le32_to_cpu(ec->mask);
903 	se->dobj.index = tplg->index;
904 	se->dobj.type = SND_SOC_DOBJ_ENUM;
905 	if (tplg->ops)
906 		se->dobj.unload = tplg->ops->control_unload;
907 	INIT_LIST_HEAD(&se->dobj.list);
908 
909 	switch (le32_to_cpu(ec->hdr.ops.info)) {
910 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
911 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
912 		ret = soc_tplg_denum_create_values(tplg, se, ec);
913 		if (ret < 0) {
914 			dev_err(tplg->dev,
915 				"ASoC: could not create values for %s\n",
916 				ec->hdr.name);
917 			goto err;
918 		}
919 		fallthrough;
920 	case SND_SOC_TPLG_CTL_ENUM:
921 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
922 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
923 		ret = soc_tplg_denum_create_texts(tplg, se, ec);
924 		if (ret < 0) {
925 			dev_err(tplg->dev,
926 				"ASoC: could not create texts for %s\n",
927 				ec->hdr.name);
928 			goto err;
929 		}
930 		break;
931 	default:
932 		ret = -EINVAL;
933 		dev_err(tplg->dev,
934 			"ASoC: invalid enum control type %d for %s\n",
935 			ec->hdr.ops.info, ec->hdr.name);
936 		goto err;
937 	}
938 
939 	/* map io handlers */
940 	ret = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
941 	if (ret) {
942 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
943 		goto err;
944 	}
945 
946 	/* pass control to driver for optional further init */
947 	ret = soc_tplg_control_load(tplg, &kc, &ec->hdr);
948 	if (ret < 0) {
949 		dev_err(tplg->dev, "ASoC: failed to init %s\n", ec->hdr.name);
950 		goto err;
951 	}
952 
953 	/* register control here */
954 	ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
955 	if (ret < 0) {
956 		dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n", ec->hdr.name);
957 		goto err;
958 	}
959 
960 	list_add(&se->dobj.list, &tplg->comp->dobj_list);
961 
962 err:
963 	return ret;
964 }
965 
966 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
967 	struct snd_soc_tplg_hdr *hdr)
968 {
969 	int ret;
970 	int i;
971 
972 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
973 		soc_tplg_get_offset(tplg));
974 
975 	for (i = 0; i < le32_to_cpu(hdr->count); i++) {
976 		struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
977 
978 		if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
979 			dev_err(tplg->dev, "ASoC: invalid control size\n");
980 			return -EINVAL;
981 		}
982 
983 		switch (le32_to_cpu(control_hdr->ops.info)) {
984 		case SND_SOC_TPLG_CTL_VOLSW:
985 		case SND_SOC_TPLG_CTL_STROBE:
986 		case SND_SOC_TPLG_CTL_VOLSW_SX:
987 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
988 		case SND_SOC_TPLG_CTL_RANGE:
989 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
990 		case SND_SOC_TPLG_DAPM_CTL_PIN:
991 			ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
992 			break;
993 		case SND_SOC_TPLG_CTL_ENUM:
994 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
995 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
996 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
997 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
998 			ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
999 			break;
1000 		case SND_SOC_TPLG_CTL_BYTES:
1001 			ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
1002 			break;
1003 		default:
1004 			soc_bind_err(tplg, control_hdr, i);
1005 			return -EINVAL;
1006 		}
1007 		if (ret < 0) {
1008 			dev_err(tplg->dev, "ASoC: invalid control\n");
1009 			return ret;
1010 		}
1011 
1012 	}
1013 
1014 	return 0;
1015 }
1016 
1017 /* optionally pass new dynamic kcontrol to component driver. */
1018 static int soc_tplg_add_route(struct soc_tplg *tplg,
1019 	struct snd_soc_dapm_route *route)
1020 {
1021 	if (tplg->ops && tplg->ops->dapm_route_load)
1022 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1023 			route);
1024 
1025 	return 0;
1026 }
1027 
1028 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1029 	struct snd_soc_tplg_hdr *hdr)
1030 {
1031 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1032 	struct snd_soc_tplg_dapm_graph_elem *elem;
1033 	struct snd_soc_dapm_route *route;
1034 	int count, i;
1035 	int ret = 0;
1036 
1037 	count = le32_to_cpu(hdr->count);
1038 
1039 	if (soc_tplg_check_elem_count(tplg,
1040 				      sizeof(struct snd_soc_tplg_dapm_graph_elem),
1041 				      count, le32_to_cpu(hdr->payload_size), "graph"))
1042 		return -EINVAL;
1043 
1044 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1045 		hdr->index);
1046 
1047 	for (i = 0; i < count; i++) {
1048 		route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL);
1049 		if (!route)
1050 			return -ENOMEM;
1051 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1052 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1053 
1054 		/* validate routes */
1055 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1056 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1057 			ret = -EINVAL;
1058 			break;
1059 		}
1060 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1061 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1062 			ret = -EINVAL;
1063 			break;
1064 		}
1065 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1066 			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1067 			ret = -EINVAL;
1068 			break;
1069 		}
1070 
1071 		route->source = elem->source;
1072 		route->sink = elem->sink;
1073 
1074 		/* set to NULL atm for tplg users */
1075 		route->connected = NULL;
1076 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1077 			route->control = NULL;
1078 		else
1079 			route->control = elem->control;
1080 
1081 		/* add route dobj to dobj_list */
1082 		route->dobj.type = SND_SOC_DOBJ_GRAPH;
1083 		if (tplg->ops)
1084 			route->dobj.unload = tplg->ops->dapm_route_unload;
1085 		route->dobj.index = tplg->index;
1086 		list_add(&route->dobj.list, &tplg->comp->dobj_list);
1087 
1088 		ret = soc_tplg_add_route(tplg, route);
1089 		if (ret < 0) {
1090 			dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
1091 			break;
1092 		}
1093 
1094 		/* add route, but keep going if some fail */
1095 		snd_soc_dapm_add_routes(dapm, route, 1);
1096 	}
1097 
1098 	return ret;
1099 }
1100 
1101 static int soc_tplg_dapm_widget_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1102 {
1103 	struct soc_mixer_control *sm;
1104 	struct snd_soc_tplg_mixer_control *mc;
1105 	int err;
1106 
1107 	mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1108 
1109 	/* validate kcontrol */
1110 	if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1111 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1112 		return -EINVAL;
1113 
1114 	sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
1115 	if (!sm)
1116 		return -ENOMEM;
1117 
1118 	tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) +
1119 		le32_to_cpu(mc->priv.size);
1120 
1121 	dev_dbg(tplg->dev, " adding DAPM widget mixer control %s\n",
1122 		mc->hdr.name);
1123 
1124 	kc->private_value = (long)sm;
1125 	kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL);
1126 	if (!kc->name)
1127 		return -ENOMEM;
1128 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1129 	kc->access = le32_to_cpu(mc->hdr.access);
1130 
1131 	/* we only support FL/FR channel mapping atm */
1132 	sm->reg = tplg_chan_get_reg(tplg, mc->channel,
1133 				    SNDRV_CHMAP_FL);
1134 	sm->rreg = tplg_chan_get_reg(tplg, mc->channel,
1135 				     SNDRV_CHMAP_FR);
1136 	sm->shift = tplg_chan_get_shift(tplg, mc->channel,
1137 					SNDRV_CHMAP_FL);
1138 	sm->rshift = tplg_chan_get_shift(tplg, mc->channel,
1139 					 SNDRV_CHMAP_FR);
1140 
1141 	sm->max = le32_to_cpu(mc->max);
1142 	sm->min = le32_to_cpu(mc->min);
1143 	sm->invert = le32_to_cpu(mc->invert);
1144 	sm->platform_max = le32_to_cpu(mc->platform_max);
1145 	sm->dobj.index = tplg->index;
1146 	INIT_LIST_HEAD(&sm->dobj.list);
1147 
1148 	/* map io handlers */
1149 	err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg);
1150 	if (err) {
1151 		soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1152 		return err;
1153 	}
1154 
1155 	/* create any TLV data */
1156 	err = soc_tplg_create_tlv(tplg, kc, &mc->hdr);
1157 	if (err < 0) {
1158 		dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1159 			mc->hdr.name);
1160 		return err;
1161 	}
1162 
1163 	/* pass control to driver for optional further init */
1164 	err = soc_tplg_control_load(tplg, kc, &mc->hdr);
1165 	if (err < 0) {
1166 		dev_err(tplg->dev, "ASoC: failed to init %s\n",
1167 			mc->hdr.name);
1168 		return err;
1169 	}
1170 
1171 	return 0;
1172 }
1173 
1174 static int soc_tplg_dapm_widget_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1175 {
1176 	struct snd_soc_tplg_enum_control *ec;
1177 	struct soc_enum *se;
1178 	int err;
1179 
1180 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1181 	/* validate kcontrol */
1182 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1183 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1184 		return -EINVAL;
1185 
1186 	se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL);
1187 	if (!se)
1188 		return -ENOMEM;
1189 
1190 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1191 		      le32_to_cpu(ec->priv.size));
1192 
1193 	dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1194 		ec->hdr.name);
1195 
1196 	kc->private_value = (long)se;
1197 	kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL);
1198 	if (!kc->name)
1199 		return -ENOMEM;
1200 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1201 	kc->access = le32_to_cpu(ec->hdr.access);
1202 
1203 	/* we only support FL/FR channel mapping atm */
1204 	se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1205 	se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
1206 					  SNDRV_CHMAP_FL);
1207 	se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
1208 					  SNDRV_CHMAP_FR);
1209 
1210 	se->items = le32_to_cpu(ec->items);
1211 	se->mask = le32_to_cpu(ec->mask);
1212 	se->dobj.index = tplg->index;
1213 
1214 	switch (le32_to_cpu(ec->hdr.ops.info)) {
1215 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
1216 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1217 		err = soc_tplg_denum_create_values(tplg, se, ec);
1218 		if (err < 0) {
1219 			dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1220 				ec->hdr.name);
1221 			return err;
1222 		}
1223 		fallthrough;
1224 	case SND_SOC_TPLG_CTL_ENUM:
1225 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1226 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1227 		err = soc_tplg_denum_create_texts(tplg, se, ec);
1228 		if (err < 0) {
1229 			dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1230 				ec->hdr.name);
1231 			return err;
1232 		}
1233 		break;
1234 	default:
1235 		dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1236 			ec->hdr.ops.info, ec->hdr.name);
1237 		return -EINVAL;
1238 	}
1239 
1240 	/* map io handlers */
1241 	err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
1242 	if (err) {
1243 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1244 		return err;
1245 	}
1246 
1247 	/* pass control to driver for optional further init */
1248 	err = soc_tplg_control_load(tplg, kc, &ec->hdr);
1249 	if (err < 0) {
1250 		dev_err(tplg->dev, "ASoC: failed to init %s\n",
1251 			ec->hdr.name);
1252 		return err;
1253 	}
1254 
1255 	return 0;
1256 }
1257 
1258 static int soc_tplg_dapm_widget_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1259 {
1260 	struct snd_soc_tplg_bytes_control *be;
1261 	struct soc_bytes_ext *sbe;
1262 	int err;
1263 
1264 	be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1265 
1266 	/* validate kcontrol */
1267 	if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1268 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1269 		return -EINVAL;
1270 
1271 	sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
1272 	if (!sbe)
1273 		return -ENOMEM;
1274 
1275 	tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1276 		      le32_to_cpu(be->priv.size));
1277 
1278 	dev_dbg(tplg->dev,
1279 		"ASoC: adding bytes kcontrol %s with access 0x%x\n",
1280 		be->hdr.name, be->hdr.access);
1281 
1282 	kc->private_value = (long)sbe;
1283 	kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL);
1284 	if (!kc->name)
1285 		return -ENOMEM;
1286 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1287 	kc->access = le32_to_cpu(be->hdr.access);
1288 
1289 	sbe->max = le32_to_cpu(be->max);
1290 	INIT_LIST_HEAD(&sbe->dobj.list);
1291 
1292 	/* map standard io handlers and check for external handlers */
1293 	err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg);
1294 	if (err) {
1295 		soc_control_err(tplg, &be->hdr, be->hdr.name);
1296 		return err;
1297 	}
1298 
1299 	/* pass control to driver for optional further init */
1300 	err = soc_tplg_control_load(tplg, kc, &be->hdr);
1301 	if (err < 0) {
1302 		dev_err(tplg->dev, "ASoC: failed to init %s\n",
1303 			be->hdr.name);
1304 		return err;
1305 	}
1306 
1307 	return 0;
1308 }
1309 
1310 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1311 	struct snd_soc_tplg_dapm_widget *w)
1312 {
1313 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1314 	struct snd_soc_dapm_widget template, *widget;
1315 	struct snd_soc_tplg_ctl_hdr *control_hdr;
1316 	struct snd_soc_card *card = tplg->comp->card;
1317 	unsigned int *kcontrol_type = NULL;
1318 	struct snd_kcontrol_new *kc;
1319 	int mixer_count = 0;
1320 	int bytes_count = 0;
1321 	int enum_count = 0;
1322 	int ret = 0;
1323 	int i;
1324 
1325 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1326 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1327 		return -EINVAL;
1328 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1329 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1330 		return -EINVAL;
1331 
1332 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1333 		w->name, w->id);
1334 
1335 	memset(&template, 0, sizeof(template));
1336 
1337 	/* map user to kernel widget ID */
1338 	template.id = get_widget_id(le32_to_cpu(w->id));
1339 	if ((int)template.id < 0)
1340 		return template.id;
1341 
1342 	/* strings are allocated here, but used and freed by the widget */
1343 	template.name = kstrdup(w->name, GFP_KERNEL);
1344 	if (!template.name)
1345 		return -ENOMEM;
1346 	template.sname = kstrdup(w->sname, GFP_KERNEL);
1347 	if (!template.sname) {
1348 		ret = -ENOMEM;
1349 		goto err;
1350 	}
1351 	template.reg = le32_to_cpu(w->reg);
1352 	template.shift = le32_to_cpu(w->shift);
1353 	template.mask = le32_to_cpu(w->mask);
1354 	template.subseq = le32_to_cpu(w->subseq);
1355 	template.on_val = w->invert ? 0 : 1;
1356 	template.off_val = w->invert ? 1 : 0;
1357 	template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1358 	template.event_flags = le16_to_cpu(w->event_flags);
1359 	template.dobj.index = tplg->index;
1360 
1361 	tplg->pos +=
1362 		(sizeof(struct snd_soc_tplg_dapm_widget) +
1363 		 le32_to_cpu(w->priv.size));
1364 
1365 	if (w->num_kcontrols == 0) {
1366 		template.num_kcontrols = 0;
1367 		goto widget;
1368 	}
1369 
1370 	template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1371 	kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL);
1372 	if (!kc) {
1373 		ret = -ENOMEM;
1374 		goto hdr_err;
1375 	}
1376 
1377 	kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int),
1378 				     GFP_KERNEL);
1379 	if (!kcontrol_type) {
1380 		ret = -ENOMEM;
1381 		goto hdr_err;
1382 	}
1383 
1384 	for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) {
1385 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1386 		switch (le32_to_cpu(control_hdr->ops.info)) {
1387 		case SND_SOC_TPLG_CTL_VOLSW:
1388 		case SND_SOC_TPLG_CTL_STROBE:
1389 		case SND_SOC_TPLG_CTL_VOLSW_SX:
1390 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1391 		case SND_SOC_TPLG_CTL_RANGE:
1392 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1393 			/* volume mixer */
1394 			kc[i].index = mixer_count;
1395 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER;
1396 			mixer_count++;
1397 			ret = soc_tplg_dapm_widget_dmixer_create(tplg, &kc[i]);
1398 			if (ret < 0)
1399 				goto hdr_err;
1400 			break;
1401 		case SND_SOC_TPLG_CTL_ENUM:
1402 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
1403 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1404 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1405 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1406 			/* enumerated mixer */
1407 			kc[i].index = enum_count;
1408 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM;
1409 			enum_count++;
1410 			ret = soc_tplg_dapm_widget_denum_create(tplg, &kc[i]);
1411 			if (ret < 0)
1412 				goto hdr_err;
1413 			break;
1414 		case SND_SOC_TPLG_CTL_BYTES:
1415 			/* bytes control */
1416 			kc[i].index = bytes_count;
1417 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES;
1418 			bytes_count++;
1419 			ret = soc_tplg_dapm_widget_dbytes_create(tplg, &kc[i]);
1420 			if (ret < 0)
1421 				goto hdr_err;
1422 			break;
1423 		default:
1424 			dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1425 				control_hdr->ops.get, control_hdr->ops.put,
1426 				le32_to_cpu(control_hdr->ops.info));
1427 			ret = -EINVAL;
1428 			goto hdr_err;
1429 		}
1430 	}
1431 
1432 	template.kcontrol_news = kc;
1433 	dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n",
1434 		w->name, mixer_count, enum_count, bytes_count);
1435 
1436 widget:
1437 	ret = soc_tplg_widget_load(tplg, &template, w);
1438 	if (ret < 0)
1439 		goto hdr_err;
1440 
1441 	/* card dapm mutex is held by the core if we are loading topology
1442 	 * data during sound card init. */
1443 	if (snd_soc_card_is_instantiated(card))
1444 		widget = snd_soc_dapm_new_control(dapm, &template);
1445 	else
1446 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1447 	if (IS_ERR(widget)) {
1448 		ret = PTR_ERR(widget);
1449 		goto hdr_err;
1450 	}
1451 
1452 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1453 	widget->dobj.widget.kcontrol_type = kcontrol_type;
1454 	if (tplg->ops)
1455 		widget->dobj.unload = tplg->ops->widget_unload;
1456 	widget->dobj.index = tplg->index;
1457 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1458 
1459 	ret = soc_tplg_widget_ready(tplg, widget, w);
1460 	if (ret < 0)
1461 		goto ready_err;
1462 
1463 	kfree(template.sname);
1464 	kfree(template.name);
1465 
1466 	return 0;
1467 
1468 ready_err:
1469 	soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET);
1470 	snd_soc_dapm_free_widget(widget);
1471 hdr_err:
1472 	kfree(template.sname);
1473 err:
1474 	kfree(template.name);
1475 	return ret;
1476 }
1477 
1478 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1479 	struct snd_soc_tplg_hdr *hdr)
1480 {
1481 	int count, i;
1482 
1483 	count = le32_to_cpu(hdr->count);
1484 
1485 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1486 
1487 	for (i = 0; i < count; i++) {
1488 		struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1489 		int ret;
1490 
1491 		/*
1492 		 * check if widget itself fits within topology file
1493 		 * use sizeof instead of widget->size, as we can't be sure
1494 		 * it is set properly yet (file may end before it is present)
1495 		 */
1496 		if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) {
1497 			dev_err(tplg->dev, "ASoC: invalid widget data size\n");
1498 			return -EINVAL;
1499 		}
1500 
1501 		/* check if widget has proper size */
1502 		if (le32_to_cpu(widget->size) != sizeof(*widget)) {
1503 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
1504 			return -EINVAL;
1505 		}
1506 
1507 		/* check if widget private data fits within topology file */
1508 		if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) {
1509 			dev_err(tplg->dev, "ASoC: invalid widget private data size\n");
1510 			return -EINVAL;
1511 		}
1512 
1513 		ret = soc_tplg_dapm_widget_create(tplg, widget);
1514 		if (ret < 0) {
1515 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1516 				widget->name);
1517 			return ret;
1518 		}
1519 	}
1520 
1521 	return 0;
1522 }
1523 
1524 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1525 {
1526 	struct snd_soc_card *card = tplg->comp->card;
1527 	int ret;
1528 
1529 	/* Card might not have been registered at this point.
1530 	 * If so, just return success.
1531 	*/
1532 	if (!snd_soc_card_is_instantiated(card)) {
1533 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1534 			" widget card binding deferred\n");
1535 		return 0;
1536 	}
1537 
1538 	ret = snd_soc_dapm_new_widgets(card);
1539 	if (ret < 0)
1540 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1541 			ret);
1542 
1543 	return ret;
1544 }
1545 
1546 static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream,
1547 			   struct snd_soc_tplg_stream_caps *caps)
1548 {
1549 	stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL);
1550 	if (!stream->stream_name)
1551 		return -ENOMEM;
1552 
1553 	stream->channels_min = le32_to_cpu(caps->channels_min);
1554 	stream->channels_max = le32_to_cpu(caps->channels_max);
1555 	stream->rates = le32_to_cpu(caps->rates);
1556 	stream->rate_min = le32_to_cpu(caps->rate_min);
1557 	stream->rate_max = le32_to_cpu(caps->rate_max);
1558 	stream->formats = le64_to_cpu(caps->formats);
1559 	stream->sig_bits = le32_to_cpu(caps->sig_bits);
1560 
1561 	return 0;
1562 }
1563 
1564 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1565 			  unsigned int flag_mask, unsigned int flags)
1566 {
1567 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1568 		dai_drv->symmetric_rate =
1569 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1570 
1571 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1572 		dai_drv->symmetric_channels =
1573 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ?
1574 			1 : 0;
1575 
1576 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1577 		dai_drv->symmetric_sample_bits =
1578 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1579 			1 : 0;
1580 }
1581 
1582 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1583 	struct snd_soc_tplg_pcm *pcm)
1584 {
1585 	struct snd_soc_dai_driver *dai_drv;
1586 	struct snd_soc_pcm_stream *stream;
1587 	struct snd_soc_tplg_stream_caps *caps;
1588 	struct snd_soc_dai *dai;
1589 	struct snd_soc_dapm_context *dapm =
1590 		snd_soc_component_get_dapm(tplg->comp);
1591 	int ret;
1592 
1593 	dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1594 	if (dai_drv == NULL)
1595 		return -ENOMEM;
1596 
1597 	if (strlen(pcm->dai_name)) {
1598 		dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1599 		if (!dai_drv->name) {
1600 			ret = -ENOMEM;
1601 			goto err;
1602 		}
1603 	}
1604 	dai_drv->id = le32_to_cpu(pcm->dai_id);
1605 
1606 	if (pcm->playback) {
1607 		stream = &dai_drv->playback;
1608 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1609 		ret = set_stream_info(tplg, stream, caps);
1610 		if (ret < 0)
1611 			goto err;
1612 	}
1613 
1614 	if (pcm->capture) {
1615 		stream = &dai_drv->capture;
1616 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1617 		ret = set_stream_info(tplg, stream, caps);
1618 		if (ret < 0)
1619 			goto err;
1620 	}
1621 
1622 	if (pcm->compress)
1623 		dai_drv->compress_new = snd_soc_new_compress;
1624 
1625 	/* pass control to component driver for optional further init */
1626 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1627 	if (ret < 0) {
1628 		dev_err(tplg->dev, "ASoC: DAI loading failed\n");
1629 		goto err;
1630 	}
1631 
1632 	dai_drv->dobj.index = tplg->index;
1633 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1634 	if (tplg->ops)
1635 		dai_drv->dobj.unload = tplg->ops->dai_unload;
1636 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1637 
1638 	/* register the DAI to the component */
1639 	dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1640 	if (!dai)
1641 		return -ENOMEM;
1642 
1643 	/* Create the DAI widgets here */
1644 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1645 	if (ret != 0) {
1646 		dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1647 		snd_soc_unregister_dai(dai);
1648 		return ret;
1649 	}
1650 
1651 	return 0;
1652 
1653 err:
1654 	return ret;
1655 }
1656 
1657 static void set_link_flags(struct snd_soc_dai_link *link,
1658 		unsigned int flag_mask, unsigned int flags)
1659 {
1660 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1661 		link->symmetric_rate =
1662 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1663 
1664 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1665 		link->symmetric_channels =
1666 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ?
1667 			1 : 0;
1668 
1669 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1670 		link->symmetric_sample_bits =
1671 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1672 			1 : 0;
1673 
1674 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1675 		link->ignore_suspend =
1676 			(flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ?
1677 			1 : 0;
1678 }
1679 
1680 /* create the FE DAI link */
1681 static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1682 	struct snd_soc_tplg_pcm *pcm)
1683 {
1684 	struct snd_soc_dai_link *link;
1685 	struct snd_soc_dai_link_component *dlc;
1686 	int ret;
1687 
1688 	/* link + cpu + codec + platform */
1689 	link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1690 	if (link == NULL)
1691 		return -ENOMEM;
1692 
1693 	dlc = (struct snd_soc_dai_link_component *)(link + 1);
1694 
1695 	link->cpus	= &dlc[0];
1696 	link->codecs	= &dlc[1];
1697 	link->platforms	= &dlc[2];
1698 
1699 	link->num_cpus	 = 1;
1700 	link->num_codecs = 1;
1701 	link->num_platforms = 1;
1702 
1703 	link->dobj.index = tplg->index;
1704 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1705 	if (tplg->ops)
1706 		link->dobj.unload = tplg->ops->link_unload;
1707 
1708 	if (strlen(pcm->pcm_name)) {
1709 		link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1710 		link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1711 		if (!link->name || !link->stream_name) {
1712 			ret = -ENOMEM;
1713 			goto err;
1714 		}
1715 	}
1716 	link->id = le32_to_cpu(pcm->pcm_id);
1717 
1718 	if (strlen(pcm->dai_name)) {
1719 		link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1720 		if (!link->cpus->dai_name) {
1721 			ret = -ENOMEM;
1722 			goto err;
1723 		}
1724 	}
1725 
1726 	link->codecs->name = "snd-soc-dummy";
1727 	link->codecs->dai_name = "snd-soc-dummy-dai";
1728 
1729 	link->platforms->name = "snd-soc-dummy";
1730 
1731 	/* enable DPCM */
1732 	link->dynamic = 1;
1733 	link->ignore_pmdown_time = 1;
1734 	link->dpcm_playback = le32_to_cpu(pcm->playback);
1735 	link->dpcm_capture = le32_to_cpu(pcm->capture);
1736 	if (pcm->flag_mask)
1737 		set_link_flags(link,
1738 			       le32_to_cpu(pcm->flag_mask),
1739 			       le32_to_cpu(pcm->flags));
1740 
1741 	/* pass control to component driver for optional further init */
1742 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1743 	if (ret < 0) {
1744 		dev_err(tplg->dev, "ASoC: FE link loading failed\n");
1745 		goto err;
1746 	}
1747 
1748 	ret = snd_soc_add_pcm_runtime(tplg->comp->card, link);
1749 	if (ret < 0) {
1750 		dev_err(tplg->dev, "ASoC: adding FE link failed\n");
1751 		goto err;
1752 	}
1753 
1754 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1755 
1756 	return 0;
1757 err:
1758 	return ret;
1759 }
1760 
1761 /* create a FE DAI and DAI link from the PCM object */
1762 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1763 	struct snd_soc_tplg_pcm *pcm)
1764 {
1765 	int ret;
1766 
1767 	ret = soc_tplg_dai_create(tplg, pcm);
1768 	if (ret < 0)
1769 		return ret;
1770 
1771 	return  soc_tplg_fe_link_create(tplg, pcm);
1772 }
1773 
1774 /* copy stream caps from the old version 4 of source */
1775 static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1776 				struct snd_soc_tplg_stream_caps_v4 *src)
1777 {
1778 	dest->size = cpu_to_le32(sizeof(*dest));
1779 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1780 	dest->formats = src->formats;
1781 	dest->rates = src->rates;
1782 	dest->rate_min = src->rate_min;
1783 	dest->rate_max = src->rate_max;
1784 	dest->channels_min = src->channels_min;
1785 	dest->channels_max = src->channels_max;
1786 	dest->periods_min = src->periods_min;
1787 	dest->periods_max = src->periods_max;
1788 	dest->period_size_min = src->period_size_min;
1789 	dest->period_size_max = src->period_size_max;
1790 	dest->buffer_size_min = src->buffer_size_min;
1791 	dest->buffer_size_max = src->buffer_size_max;
1792 }
1793 
1794 /**
1795  * pcm_new_ver - Create the new version of PCM from the old version.
1796  * @tplg: topology context
1797  * @src: older version of pcm as a source
1798  * @pcm: latest version of pcm created from the source
1799  *
1800  * Support from version 4. User should free the returned pcm manually.
1801  */
1802 static int pcm_new_ver(struct soc_tplg *tplg,
1803 		       struct snd_soc_tplg_pcm *src,
1804 		       struct snd_soc_tplg_pcm **pcm)
1805 {
1806 	struct snd_soc_tplg_pcm *dest;
1807 	struct snd_soc_tplg_pcm_v4 *src_v4;
1808 	int i;
1809 
1810 	*pcm = NULL;
1811 
1812 	if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
1813 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1814 		return -EINVAL;
1815 	}
1816 
1817 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1818 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1819 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1820 	if (!dest)
1821 		return -ENOMEM;
1822 
1823 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
1824 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1825 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1826 	dest->pcm_id = src_v4->pcm_id;
1827 	dest->dai_id = src_v4->dai_id;
1828 	dest->playback = src_v4->playback;
1829 	dest->capture = src_v4->capture;
1830 	dest->compress = src_v4->compress;
1831 	dest->num_streams = src_v4->num_streams;
1832 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
1833 		memcpy(&dest->stream[i], &src_v4->stream[i],
1834 		       sizeof(struct snd_soc_tplg_stream));
1835 
1836 	for (i = 0; i < 2; i++)
1837 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1838 
1839 	*pcm = dest;
1840 	return 0;
1841 }
1842 
1843 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1844 	struct snd_soc_tplg_hdr *hdr)
1845 {
1846 	struct snd_soc_tplg_pcm *pcm, *_pcm;
1847 	int count;
1848 	int size;
1849 	int i;
1850 	bool abi_match;
1851 	int ret;
1852 
1853 	count = le32_to_cpu(hdr->count);
1854 
1855 	/* check the element size and count */
1856 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1857 	size = le32_to_cpu(pcm->size);
1858 	if (size > sizeof(struct snd_soc_tplg_pcm)
1859 		|| size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1860 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1861 			size);
1862 		return -EINVAL;
1863 	}
1864 
1865 	if (soc_tplg_check_elem_count(tplg,
1866 				      size, count,
1867 				      le32_to_cpu(hdr->payload_size),
1868 				      "PCM DAI"))
1869 		return -EINVAL;
1870 
1871 	for (i = 0; i < count; i++) {
1872 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1873 		size = le32_to_cpu(pcm->size);
1874 
1875 		/* check ABI version by size, create a new version of pcm
1876 		 * if abi not match.
1877 		 */
1878 		if (size == sizeof(*pcm)) {
1879 			abi_match = true;
1880 			_pcm = pcm;
1881 		} else {
1882 			abi_match = false;
1883 			ret = pcm_new_ver(tplg, pcm, &_pcm);
1884 			if (ret < 0)
1885 				return ret;
1886 		}
1887 
1888 		/* create the FE DAIs and DAI links */
1889 		ret = soc_tplg_pcm_create(tplg, _pcm);
1890 		if (ret < 0) {
1891 			if (!abi_match)
1892 				kfree(_pcm);
1893 			return ret;
1894 		}
1895 
1896 		/* offset by version-specific struct size and
1897 		 * real priv data size
1898 		 */
1899 		tplg->pos += size + le32_to_cpu(_pcm->priv.size);
1900 
1901 		if (!abi_match)
1902 			kfree(_pcm); /* free the duplicated one */
1903 	}
1904 
1905 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1906 
1907 	return 0;
1908 }
1909 
1910 /**
1911  * set_link_hw_format - Set the HW audio format of the physical DAI link.
1912  * @link: &snd_soc_dai_link which should be updated
1913  * @cfg: physical link configs.
1914  *
1915  * Topology context contains a list of supported HW formats (configs) and
1916  * a default format ID for the physical link. This function will use this
1917  * default ID to choose the HW format to set the link's DAI format for init.
1918  */
1919 static void set_link_hw_format(struct snd_soc_dai_link *link,
1920 			struct snd_soc_tplg_link_config *cfg)
1921 {
1922 	struct snd_soc_tplg_hw_config *hw_config;
1923 	unsigned char bclk_provider, fsync_provider;
1924 	unsigned char invert_bclk, invert_fsync;
1925 	int i;
1926 
1927 	for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
1928 		hw_config = &cfg->hw_config[i];
1929 		if (hw_config->id != cfg->default_hw_config_id)
1930 			continue;
1931 
1932 		link->dai_fmt = le32_to_cpu(hw_config->fmt) &
1933 			SND_SOC_DAIFMT_FORMAT_MASK;
1934 
1935 		/* clock gating */
1936 		switch (hw_config->clock_gated) {
1937 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
1938 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
1939 			break;
1940 
1941 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
1942 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
1943 			break;
1944 
1945 		default:
1946 			/* ignore the value */
1947 			break;
1948 		}
1949 
1950 		/* clock signal polarity */
1951 		invert_bclk = hw_config->invert_bclk;
1952 		invert_fsync = hw_config->invert_fsync;
1953 		if (!invert_bclk && !invert_fsync)
1954 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1955 		else if (!invert_bclk && invert_fsync)
1956 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1957 		else if (invert_bclk && !invert_fsync)
1958 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1959 		else
1960 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1961 
1962 		/* clock masters */
1963 		bclk_provider = (hw_config->bclk_provider ==
1964 			       SND_SOC_TPLG_BCLK_CP);
1965 		fsync_provider = (hw_config->fsync_provider ==
1966 				SND_SOC_TPLG_FSYNC_CP);
1967 		if (bclk_provider && fsync_provider)
1968 			link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
1969 		else if (!bclk_provider && fsync_provider)
1970 			link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
1971 		else if (bclk_provider && !fsync_provider)
1972 			link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
1973 		else
1974 			link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
1975 	}
1976 }
1977 
1978 /**
1979  * link_new_ver - Create a new physical link config from the old
1980  * version of source.
1981  * @tplg: topology context
1982  * @src: old version of phyical link config as a source
1983  * @link: latest version of physical link config created from the source
1984  *
1985  * Support from version 4. User need free the returned link config manually.
1986  */
1987 static int link_new_ver(struct soc_tplg *tplg,
1988 			struct snd_soc_tplg_link_config *src,
1989 			struct snd_soc_tplg_link_config **link)
1990 {
1991 	struct snd_soc_tplg_link_config *dest;
1992 	struct snd_soc_tplg_link_config_v4 *src_v4;
1993 	int i;
1994 
1995 	*link = NULL;
1996 
1997 	if (le32_to_cpu(src->size) !=
1998 	    sizeof(struct snd_soc_tplg_link_config_v4)) {
1999 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2000 		return -EINVAL;
2001 	}
2002 
2003 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2004 
2005 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2006 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2007 	if (!dest)
2008 		return -ENOMEM;
2009 
2010 	dest->size = cpu_to_le32(sizeof(*dest));
2011 	dest->id = src_v4->id;
2012 	dest->num_streams = src_v4->num_streams;
2013 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2014 		memcpy(&dest->stream[i], &src_v4->stream[i],
2015 		       sizeof(struct snd_soc_tplg_stream));
2016 
2017 	*link = dest;
2018 	return 0;
2019 }
2020 
2021 /**
2022  * snd_soc_find_dai_link - Find a DAI link
2023  *
2024  * @card: soc card
2025  * @id: DAI link ID to match
2026  * @name: DAI link name to match, optional
2027  * @stream_name: DAI link stream name to match, optional
2028  *
2029  * This function will search all existing DAI links of the soc card to
2030  * find the link of the same ID. Since DAI links may not have their
2031  * unique ID, so name and stream name should also match if being
2032  * specified.
2033  *
2034  * Return: pointer of DAI link, or NULL if not found.
2035  */
2036 static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2037 						      int id, const char *name,
2038 						      const char *stream_name)
2039 {
2040 	struct snd_soc_pcm_runtime *rtd;
2041 
2042 	for_each_card_rtds(card, rtd) {
2043 		struct snd_soc_dai_link *link = rtd->dai_link;
2044 
2045 		if (link->id != id)
2046 			continue;
2047 
2048 		if (name && (!link->name || strcmp(name, link->name)))
2049 			continue;
2050 
2051 		if (stream_name && (!link->stream_name
2052 				    || strcmp(stream_name, link->stream_name)))
2053 			continue;
2054 
2055 		return link;
2056 	}
2057 
2058 	return NULL;
2059 }
2060 
2061 /* Find and configure an existing physical DAI link */
2062 static int soc_tplg_link_config(struct soc_tplg *tplg,
2063 	struct snd_soc_tplg_link_config *cfg)
2064 {
2065 	struct snd_soc_dai_link *link;
2066 	const char *name, *stream_name;
2067 	size_t len;
2068 	int ret;
2069 
2070 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2071 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2072 		return -EINVAL;
2073 	else if (len)
2074 		name = cfg->name;
2075 	else
2076 		name = NULL;
2077 
2078 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2079 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2080 		return -EINVAL;
2081 	else if (len)
2082 		stream_name = cfg->stream_name;
2083 	else
2084 		stream_name = NULL;
2085 
2086 	link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2087 				     name, stream_name);
2088 	if (!link) {
2089 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2090 			name, cfg->id);
2091 		return -EINVAL;
2092 	}
2093 
2094 	/* hw format */
2095 	if (cfg->num_hw_configs)
2096 		set_link_hw_format(link, cfg);
2097 
2098 	/* flags */
2099 	if (cfg->flag_mask)
2100 		set_link_flags(link,
2101 			       le32_to_cpu(cfg->flag_mask),
2102 			       le32_to_cpu(cfg->flags));
2103 
2104 	/* pass control to component driver for optional further init */
2105 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2106 	if (ret < 0) {
2107 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2108 		return ret;
2109 	}
2110 
2111 	/* for unloading it in snd_soc_tplg_component_remove */
2112 	link->dobj.index = tplg->index;
2113 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2114 	if (tplg->ops)
2115 		link->dobj.unload = tplg->ops->link_unload;
2116 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2117 
2118 	return 0;
2119 }
2120 
2121 
2122 /* Load physical link config elements from the topology context */
2123 static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2124 	struct snd_soc_tplg_hdr *hdr)
2125 {
2126 	struct snd_soc_tplg_link_config *link, *_link;
2127 	int count;
2128 	int size;
2129 	int i, ret;
2130 	bool abi_match;
2131 
2132 	count = le32_to_cpu(hdr->count);
2133 
2134 	/* check the element size and count */
2135 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
2136 	size = le32_to_cpu(link->size);
2137 	if (size > sizeof(struct snd_soc_tplg_link_config)
2138 		|| size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2139 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2140 			size);
2141 		return -EINVAL;
2142 	}
2143 
2144 	if (soc_tplg_check_elem_count(tplg, size, count,
2145 				      le32_to_cpu(hdr->payload_size),
2146 				      "physical link config"))
2147 		return -EINVAL;
2148 
2149 	/* config physical DAI links */
2150 	for (i = 0; i < count; i++) {
2151 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
2152 		size = le32_to_cpu(link->size);
2153 		if (size == sizeof(*link)) {
2154 			abi_match = true;
2155 			_link = link;
2156 		} else {
2157 			abi_match = false;
2158 			ret = link_new_ver(tplg, link, &_link);
2159 			if (ret < 0)
2160 				return ret;
2161 		}
2162 
2163 		ret = soc_tplg_link_config(tplg, _link);
2164 		if (ret < 0) {
2165 			if (!abi_match)
2166 				kfree(_link);
2167 			return ret;
2168 		}
2169 
2170 		/* offset by version-specific struct size and
2171 		 * real priv data size
2172 		 */
2173 		tplg->pos += size + le32_to_cpu(_link->priv.size);
2174 
2175 		if (!abi_match)
2176 			kfree(_link); /* free the duplicated one */
2177 	}
2178 
2179 	return 0;
2180 }
2181 
2182 /**
2183  * soc_tplg_dai_config - Find and configure an existing physical DAI.
2184  * @tplg: topology context
2185  * @d: physical DAI configs.
2186  *
2187  * The physical dai should already be registered by the platform driver.
2188  * The platform driver should specify the DAI name and ID for matching.
2189  */
2190 static int soc_tplg_dai_config(struct soc_tplg *tplg,
2191 			       struct snd_soc_tplg_dai *d)
2192 {
2193 	struct snd_soc_dai_link_component dai_component;
2194 	struct snd_soc_dai *dai;
2195 	struct snd_soc_dai_driver *dai_drv;
2196 	struct snd_soc_pcm_stream *stream;
2197 	struct snd_soc_tplg_stream_caps *caps;
2198 	int ret;
2199 
2200 	memset(&dai_component, 0, sizeof(dai_component));
2201 
2202 	dai_component.dai_name = d->dai_name;
2203 	dai = snd_soc_find_dai(&dai_component);
2204 	if (!dai) {
2205 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2206 			d->dai_name);
2207 		return -EINVAL;
2208 	}
2209 
2210 	if (le32_to_cpu(d->dai_id) != dai->id) {
2211 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2212 			d->dai_name);
2213 		return -EINVAL;
2214 	}
2215 
2216 	dai_drv = dai->driver;
2217 	if (!dai_drv)
2218 		return -EINVAL;
2219 
2220 	if (d->playback) {
2221 		stream = &dai_drv->playback;
2222 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2223 		ret = set_stream_info(tplg, stream, caps);
2224 		if (ret < 0)
2225 			goto err;
2226 	}
2227 
2228 	if (d->capture) {
2229 		stream = &dai_drv->capture;
2230 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2231 		ret = set_stream_info(tplg, stream, caps);
2232 		if (ret < 0)
2233 			goto err;
2234 	}
2235 
2236 	if (d->flag_mask)
2237 		set_dai_flags(dai_drv,
2238 			      le32_to_cpu(d->flag_mask),
2239 			      le32_to_cpu(d->flags));
2240 
2241 	/* pass control to component driver for optional further init */
2242 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2243 	if (ret < 0) {
2244 		dev_err(tplg->dev, "ASoC: DAI loading failed\n");
2245 		goto err;
2246 	}
2247 
2248 	return 0;
2249 
2250 err:
2251 	return ret;
2252 }
2253 
2254 /* load physical DAI elements */
2255 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2256 				   struct snd_soc_tplg_hdr *hdr)
2257 {
2258 	int count;
2259 	int i;
2260 
2261 	count = le32_to_cpu(hdr->count);
2262 
2263 	/* config the existing BE DAIs */
2264 	for (i = 0; i < count; i++) {
2265 		struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos;
2266 		int ret;
2267 
2268 		if (le32_to_cpu(dai->size) != sizeof(*dai)) {
2269 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2270 			return -EINVAL;
2271 		}
2272 
2273 		ret = soc_tplg_dai_config(tplg, dai);
2274 		if (ret < 0) {
2275 			dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2276 			return ret;
2277 		}
2278 
2279 		tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
2280 	}
2281 
2282 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2283 	return 0;
2284 }
2285 
2286 /**
2287  * manifest_new_ver - Create a new version of manifest from the old version
2288  * of source.
2289  * @tplg: topology context
2290  * @src: old version of manifest as a source
2291  * @manifest: latest version of manifest created from the source
2292  *
2293  * Support from version 4. Users need free the returned manifest manually.
2294  */
2295 static int manifest_new_ver(struct soc_tplg *tplg,
2296 			    struct snd_soc_tplg_manifest *src,
2297 			    struct snd_soc_tplg_manifest **manifest)
2298 {
2299 	struct snd_soc_tplg_manifest *dest;
2300 	struct snd_soc_tplg_manifest_v4 *src_v4;
2301 	int size;
2302 
2303 	*manifest = NULL;
2304 
2305 	size = le32_to_cpu(src->size);
2306 	if (size != sizeof(*src_v4)) {
2307 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2308 			 size);
2309 		if (size)
2310 			return -EINVAL;
2311 		src->size = cpu_to_le32(sizeof(*src_v4));
2312 	}
2313 
2314 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2315 
2316 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2317 	dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2318 		       GFP_KERNEL);
2319 	if (!dest)
2320 		return -ENOMEM;
2321 
2322 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2323 	dest->control_elems = src_v4->control_elems;
2324 	dest->widget_elems = src_v4->widget_elems;
2325 	dest->graph_elems = src_v4->graph_elems;
2326 	dest->pcm_elems = src_v4->pcm_elems;
2327 	dest->dai_link_elems = src_v4->dai_link_elems;
2328 	dest->priv.size = src_v4->priv.size;
2329 	if (dest->priv.size)
2330 		memcpy(dest->priv.data, src_v4->priv.data,
2331 		       le32_to_cpu(src_v4->priv.size));
2332 
2333 	*manifest = dest;
2334 	return 0;
2335 }
2336 
2337 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2338 				  struct snd_soc_tplg_hdr *hdr)
2339 {
2340 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2341 	bool abi_match;
2342 	int ret = 0;
2343 
2344 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2345 
2346 	/* check ABI version by size, create a new manifest if abi not match */
2347 	if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2348 		abi_match = true;
2349 		_manifest = manifest;
2350 	} else {
2351 		abi_match = false;
2352 
2353 		ret = manifest_new_ver(tplg, manifest, &_manifest);
2354 		if (ret < 0)
2355 			return ret;
2356 	}
2357 
2358 	/* pass control to component driver for optional further init */
2359 	if (tplg->ops && tplg->ops->manifest)
2360 		ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
2361 
2362 	if (!abi_match)	/* free the duplicated one */
2363 		kfree(_manifest);
2364 
2365 	return ret;
2366 }
2367 
2368 /* validate header magic, size and type */
2369 static int soc_tplg_valid_header(struct soc_tplg *tplg,
2370 	struct snd_soc_tplg_hdr *hdr)
2371 {
2372 	if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
2373 		dev_err(tplg->dev,
2374 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2375 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2376 			tplg->fw->size);
2377 		return -EINVAL;
2378 	}
2379 
2380 	if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) {
2381 		dev_err(tplg->dev,
2382 			"ASoC: invalid header of type %d at offset %ld payload_size %d\n",
2383 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2384 			hdr->payload_size);
2385 		return -EINVAL;
2386 	}
2387 
2388 	/* big endian firmware objects not supported atm */
2389 	if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
2390 		dev_err(tplg->dev,
2391 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2392 			tplg->pass, hdr->magic,
2393 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2394 		return -EINVAL;
2395 	}
2396 
2397 	if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
2398 		dev_err(tplg->dev,
2399 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2400 			tplg->pass, hdr->magic,
2401 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2402 		return -EINVAL;
2403 	}
2404 
2405 	/* Support ABI from version 4 */
2406 	if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2407 	    le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
2408 		dev_err(tplg->dev,
2409 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2410 			tplg->pass, hdr->abi,
2411 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2412 			tplg->fw->size);
2413 		return -EINVAL;
2414 	}
2415 
2416 	if (hdr->payload_size == 0) {
2417 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2418 			soc_tplg_get_hdr_offset(tplg));
2419 		return -EINVAL;
2420 	}
2421 
2422 	return 0;
2423 }
2424 
2425 /* check header type and call appropriate handler */
2426 static int soc_tplg_load_header(struct soc_tplg *tplg,
2427 	struct snd_soc_tplg_hdr *hdr)
2428 {
2429 	int (*elem_load)(struct soc_tplg *tplg,
2430 			 struct snd_soc_tplg_hdr *hdr);
2431 	unsigned int hdr_pass;
2432 
2433 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2434 
2435 	tplg->index = le32_to_cpu(hdr->index);
2436 
2437 	switch (le32_to_cpu(hdr->type)) {
2438 	case SND_SOC_TPLG_TYPE_MIXER:
2439 	case SND_SOC_TPLG_TYPE_ENUM:
2440 	case SND_SOC_TPLG_TYPE_BYTES:
2441 		hdr_pass = SOC_TPLG_PASS_CONTROL;
2442 		elem_load = soc_tplg_kcontrol_elems_load;
2443 		break;
2444 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2445 		hdr_pass = SOC_TPLG_PASS_GRAPH;
2446 		elem_load = soc_tplg_dapm_graph_elems_load;
2447 		break;
2448 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2449 		hdr_pass = SOC_TPLG_PASS_WIDGET;
2450 		elem_load = soc_tplg_dapm_widget_elems_load;
2451 		break;
2452 	case SND_SOC_TPLG_TYPE_PCM:
2453 		hdr_pass = SOC_TPLG_PASS_PCM_DAI;
2454 		elem_load = soc_tplg_pcm_elems_load;
2455 		break;
2456 	case SND_SOC_TPLG_TYPE_DAI:
2457 		hdr_pass = SOC_TPLG_PASS_BE_DAI;
2458 		elem_load = soc_tplg_dai_elems_load;
2459 		break;
2460 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2461 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2462 		/* physical link configurations */
2463 		hdr_pass = SOC_TPLG_PASS_LINK;
2464 		elem_load = soc_tplg_link_elems_load;
2465 		break;
2466 	case SND_SOC_TPLG_TYPE_MANIFEST:
2467 		hdr_pass = SOC_TPLG_PASS_MANIFEST;
2468 		elem_load = soc_tplg_manifest_load;
2469 		break;
2470 	default:
2471 		/* bespoke vendor data object */
2472 		hdr_pass = SOC_TPLG_PASS_VENDOR;
2473 		elem_load = soc_tplg_vendor_load;
2474 		break;
2475 	}
2476 
2477 	if (tplg->pass == hdr_pass) {
2478 		dev_dbg(tplg->dev,
2479 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2480 			hdr->payload_size, hdr->type, hdr->version,
2481 			hdr->vendor_type, tplg->pass);
2482 		return elem_load(tplg, hdr);
2483 	}
2484 
2485 	return 0;
2486 }
2487 
2488 /* process the topology file headers */
2489 static int soc_tplg_process_headers(struct soc_tplg *tplg)
2490 {
2491 	int ret;
2492 
2493 	/* process the header types from start to end */
2494 	for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) {
2495 		struct snd_soc_tplg_hdr *hdr;
2496 
2497 		tplg->hdr_pos = tplg->fw->data;
2498 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2499 
2500 		while (!soc_tplg_is_eof(tplg)) {
2501 
2502 			/* make sure header is valid before loading */
2503 			ret = soc_tplg_valid_header(tplg, hdr);
2504 			if (ret < 0) {
2505 				dev_err(tplg->dev,
2506 					"ASoC: topology: invalid header: %d\n", ret);
2507 				return ret;
2508 			}
2509 
2510 			/* load the header object */
2511 			ret = soc_tplg_load_header(tplg, hdr);
2512 			if (ret < 0) {
2513 				dev_err(tplg->dev,
2514 					"ASoC: topology: could not load header: %d\n", ret);
2515 				return ret;
2516 			}
2517 
2518 			/* goto next header */
2519 			tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
2520 				sizeof(struct snd_soc_tplg_hdr);
2521 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2522 		}
2523 
2524 	}
2525 
2526 	/* signal DAPM we are complete */
2527 	ret = soc_tplg_dapm_complete(tplg);
2528 	if (ret < 0)
2529 		dev_err(tplg->dev,
2530 			"ASoC: failed to initialise DAPM from Firmware\n");
2531 
2532 	return ret;
2533 }
2534 
2535 static int soc_tplg_load(struct soc_tplg *tplg)
2536 {
2537 	int ret;
2538 
2539 	ret = soc_tplg_process_headers(tplg);
2540 	if (ret == 0)
2541 		return soc_tplg_complete(tplg);
2542 
2543 	return ret;
2544 }
2545 
2546 /* load audio component topology from "firmware" file */
2547 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2548 	struct snd_soc_tplg_ops *ops, const struct firmware *fw)
2549 {
2550 	struct soc_tplg tplg;
2551 	int ret;
2552 
2553 	/*
2554 	 * check if we have sane parameters:
2555 	 * comp - needs to exist to keep and reference data while parsing
2556 	 * comp->card - used for setting card related parameters
2557 	 * comp->card->dev - used for resource management and prints
2558 	 * fw - we need it, as it is the very thing we parse
2559 	 */
2560 	if (!comp || !comp->card || !comp->card->dev || !fw)
2561 		return -EINVAL;
2562 
2563 	/* setup parsing context */
2564 	memset(&tplg, 0, sizeof(tplg));
2565 	tplg.fw = fw;
2566 	tplg.dev = comp->card->dev;
2567 	tplg.comp = comp;
2568 	if (ops) {
2569 		tplg.ops = ops;
2570 		tplg.io_ops = ops->io_ops;
2571 		tplg.io_ops_count = ops->io_ops_count;
2572 		tplg.bytes_ext_ops = ops->bytes_ext_ops;
2573 		tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2574 	}
2575 
2576 	ret = soc_tplg_load(&tplg);
2577 	/* free the created components if fail to load topology */
2578 	if (ret)
2579 		snd_soc_tplg_component_remove(comp);
2580 
2581 	return ret;
2582 }
2583 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2584 
2585 /* remove dynamic controls from the component driver */
2586 int snd_soc_tplg_component_remove(struct snd_soc_component *comp)
2587 {
2588 	struct snd_card *card = comp->card->snd_card;
2589 	struct snd_soc_dobj *dobj, *next_dobj;
2590 	int pass;
2591 
2592 	/* process the header types from end to start */
2593 	for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) {
2594 
2595 		/* remove mixer controls */
2596 		down_write(&card->controls_rwsem);
2597 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2598 			list) {
2599 
2600 			switch (dobj->type) {
2601 			case SND_SOC_DOBJ_BYTES:
2602 			case SND_SOC_DOBJ_ENUM:
2603 			case SND_SOC_DOBJ_MIXER:
2604 				soc_tplg_remove_kcontrol(comp, dobj, pass);
2605 				break;
2606 			case SND_SOC_DOBJ_GRAPH:
2607 				soc_tplg_remove_route(comp, dobj, pass);
2608 				break;
2609 			case SND_SOC_DOBJ_WIDGET:
2610 				soc_tplg_remove_widget(comp, dobj, pass);
2611 				break;
2612 			case SND_SOC_DOBJ_PCM:
2613 				soc_tplg_remove_dai(comp, dobj, pass);
2614 				break;
2615 			case SND_SOC_DOBJ_DAI_LINK:
2616 				soc_tplg_remove_link(comp, dobj, pass);
2617 				break;
2618 			case SND_SOC_DOBJ_BACKEND_LINK:
2619 				/*
2620 				 * call link_unload ops if extra
2621 				 * deinitialization is needed.
2622 				 */
2623 				remove_backend_link(comp, dobj, pass);
2624 				break;
2625 			default:
2626 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2627 					dobj->type);
2628 				break;
2629 			}
2630 		}
2631 		up_write(&card->controls_rwsem);
2632 	}
2633 
2634 	/* let caller know if FW can be freed when no objects are left */
2635 	return !list_empty(&comp->dobj_list);
2636 }
2637 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2638