xref: /openbmc/linux/sound/soc/soc-topology.c (revision 7e2e638c)
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 	int ret = 0;
589 
590 	if (tplg->ops && tplg->ops->control_load)
591 		ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr);
592 
593 	if (ret)
594 		dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name);
595 
596 	return ret;
597 }
598 
599 
600 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
601 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
602 {
603 	unsigned int item_len = 2 * sizeof(unsigned int);
604 	unsigned int *p;
605 
606 	p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
607 	if (!p)
608 		return -ENOMEM;
609 
610 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
611 	p[1] = item_len;
612 	p[2] = le32_to_cpu(scale->min);
613 	p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
614 		| (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
615 
616 	kc->tlv.p = (void *)p;
617 	return 0;
618 }
619 
620 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
621 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
622 {
623 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
624 	u32 access = le32_to_cpu(tc->access);
625 
626 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
627 		return 0;
628 
629 	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
630 		tplg_tlv = &tc->tlv;
631 		switch (le32_to_cpu(tplg_tlv->type)) {
632 		case SNDRV_CTL_TLVT_DB_SCALE:
633 			return soc_tplg_create_tlv_db_scale(tplg, kc,
634 					&tplg_tlv->scale);
635 
636 		/* TODO: add support for other TLV types */
637 		default:
638 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
639 					tplg_tlv->type);
640 			return -EINVAL;
641 		}
642 	}
643 
644 	return 0;
645 }
646 
647 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size)
648 {
649 	struct snd_soc_tplg_bytes_control *be;
650 	struct soc_bytes_ext *sbe;
651 	struct snd_kcontrol_new kc;
652 	int ret = 0;
653 
654 	if (soc_tplg_check_elem_count(tplg,
655 				      sizeof(struct snd_soc_tplg_bytes_control),
656 				      1, size, "mixer bytes"))
657 		return -EINVAL;
658 
659 	be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
660 
661 	/* validate kcontrol */
662 	if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
663 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
664 		return -EINVAL;
665 
666 	sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
667 	if (sbe == NULL)
668 		return -ENOMEM;
669 
670 	tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
671 		      le32_to_cpu(be->priv.size));
672 
673 	dev_dbg(tplg->dev,
674 		"ASoC: adding bytes kcontrol %s with access 0x%x\n",
675 		be->hdr.name, be->hdr.access);
676 
677 	memset(&kc, 0, sizeof(kc));
678 	kc.name = be->hdr.name;
679 	kc.private_value = (long)sbe;
680 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
681 	kc.access = le32_to_cpu(be->hdr.access);
682 
683 	sbe->max = le32_to_cpu(be->max);
684 	sbe->dobj.type = SND_SOC_DOBJ_BYTES;
685 	if (tplg->ops)
686 		sbe->dobj.unload = tplg->ops->control_unload;
687 	INIT_LIST_HEAD(&sbe->dobj.list);
688 
689 	/* map io handlers */
690 	ret = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
691 	if (ret) {
692 		soc_control_err(tplg, &be->hdr, be->hdr.name);
693 		goto err;
694 	}
695 
696 	/* pass control to driver for optional further init */
697 	ret = soc_tplg_control_load(tplg, &kc, &be->hdr);
698 	if (ret < 0)
699 		goto err;
700 
701 	/* register control here */
702 	ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol);
703 	if (ret < 0)
704 		goto err;
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 		goto err;
781 
782 	/* register control here */
783 	ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol);
784 	if (ret < 0)
785 		goto err;
786 
787 	list_add(&sm->dobj.list, &tplg->comp->dobj_list);
788 
789 err:
790 	return ret;
791 }
792 
793 static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se,
794 				       struct snd_soc_tplg_enum_control *ec)
795 {
796 	int i, ret;
797 
798 	if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts))
799 		return -EINVAL;
800 
801 	se->dobj.control.dtexts =
802 		devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
803 	if (se->dobj.control.dtexts == NULL)
804 		return -ENOMEM;
805 
806 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
807 
808 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
809 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
810 			ret = -EINVAL;
811 			goto err;
812 		}
813 
814 		se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL);
815 		if (!se->dobj.control.dtexts[i]) {
816 			ret = -ENOMEM;
817 			goto err;
818 		}
819 	}
820 
821 	se->items = le32_to_cpu(ec->items);
822 	se->texts = (const char * const *)se->dobj.control.dtexts;
823 	return 0;
824 
825 err:
826 	return ret;
827 }
828 
829 static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se,
830 					struct snd_soc_tplg_enum_control *ec)
831 {
832 	int i;
833 
834 	/*
835 	 * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS
836 	 * values instead of using ARRAY_SIZE(ec->values) due to the fact that
837 	 * it is oversized for its purpose. Additionally it is done so because
838 	 * it is defined in UAPI header where it can't be easily changed.
839 	 */
840 	if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS)
841 		return -EINVAL;
842 
843 	se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items),
844 					   sizeof(*se->dobj.control.dvalues),
845 					   GFP_KERNEL);
846 	if (!se->dobj.control.dvalues)
847 		return -ENOMEM;
848 
849 	/* convert from little-endian */
850 	for (i = 0; i < le32_to_cpu(ec->items); i++) {
851 		se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
852 	}
853 
854 	se->items = le32_to_cpu(ec->items);
855 	se->values = (const unsigned int *)se->dobj.control.dvalues;
856 	return 0;
857 }
858 
859 static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
860 {
861 	struct snd_soc_tplg_enum_control *ec;
862 	struct soc_enum *se;
863 	struct snd_kcontrol_new kc;
864 	int ret = 0;
865 
866 	if (soc_tplg_check_elem_count(tplg,
867 				      sizeof(struct snd_soc_tplg_enum_control),
868 				      1, size, "enums"))
869 		return -EINVAL;
870 
871 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
872 
873 	/* validate kcontrol */
874 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
875 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
876 		return -EINVAL;
877 
878 	se = devm_kzalloc(tplg->dev, (sizeof(*se)), GFP_KERNEL);
879 	if (se == NULL)
880 		return -ENOMEM;
881 
882 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
883 		      le32_to_cpu(ec->priv.size));
884 
885 	dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
886 		ec->hdr.name, ec->items);
887 
888 	memset(&kc, 0, sizeof(kc));
889 	kc.name = ec->hdr.name;
890 	kc.private_value = (long)se;
891 	kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
892 	kc.access = le32_to_cpu(ec->hdr.access);
893 
894 	se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
895 	se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
896 		SNDRV_CHMAP_FL);
897 	se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
898 		SNDRV_CHMAP_FL);
899 
900 	se->mask = le32_to_cpu(ec->mask);
901 	se->dobj.index = tplg->index;
902 	se->dobj.type = SND_SOC_DOBJ_ENUM;
903 	if (tplg->ops)
904 		se->dobj.unload = tplg->ops->control_unload;
905 	INIT_LIST_HEAD(&se->dobj.list);
906 
907 	switch (le32_to_cpu(ec->hdr.ops.info)) {
908 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
909 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
910 		ret = soc_tplg_denum_create_values(tplg, se, ec);
911 		if (ret < 0) {
912 			dev_err(tplg->dev,
913 				"ASoC: could not create values for %s\n",
914 				ec->hdr.name);
915 			goto err;
916 		}
917 		fallthrough;
918 	case SND_SOC_TPLG_CTL_ENUM:
919 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
920 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
921 		ret = soc_tplg_denum_create_texts(tplg, se, ec);
922 		if (ret < 0) {
923 			dev_err(tplg->dev,
924 				"ASoC: could not create texts for %s\n",
925 				ec->hdr.name);
926 			goto err;
927 		}
928 		break;
929 	default:
930 		ret = -EINVAL;
931 		dev_err(tplg->dev,
932 			"ASoC: invalid enum control type %d for %s\n",
933 			ec->hdr.ops.info, ec->hdr.name);
934 		goto err;
935 	}
936 
937 	/* map io handlers */
938 	ret = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
939 	if (ret) {
940 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
941 		goto err;
942 	}
943 
944 	/* pass control to driver for optional further init */
945 	ret = soc_tplg_control_load(tplg, &kc, &ec->hdr);
946 	if (ret < 0)
947 		goto err;
948 
949 	/* register control here */
950 	ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
951 	if (ret < 0)
952 		goto err;
953 
954 	list_add(&se->dobj.list, &tplg->comp->dobj_list);
955 
956 err:
957 	return ret;
958 }
959 
960 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
961 	struct snd_soc_tplg_hdr *hdr)
962 {
963 	int ret;
964 	int i;
965 
966 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
967 		soc_tplg_get_offset(tplg));
968 
969 	for (i = 0; i < le32_to_cpu(hdr->count); i++) {
970 		struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
971 
972 		if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
973 			dev_err(tplg->dev, "ASoC: invalid control size\n");
974 			return -EINVAL;
975 		}
976 
977 		switch (le32_to_cpu(control_hdr->ops.info)) {
978 		case SND_SOC_TPLG_CTL_VOLSW:
979 		case SND_SOC_TPLG_CTL_STROBE:
980 		case SND_SOC_TPLG_CTL_VOLSW_SX:
981 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
982 		case SND_SOC_TPLG_CTL_RANGE:
983 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
984 		case SND_SOC_TPLG_DAPM_CTL_PIN:
985 			ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
986 			break;
987 		case SND_SOC_TPLG_CTL_ENUM:
988 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
989 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
990 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
991 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
992 			ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
993 			break;
994 		case SND_SOC_TPLG_CTL_BYTES:
995 			ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
996 			break;
997 		default:
998 			soc_bind_err(tplg, control_hdr, i);
999 			return -EINVAL;
1000 		}
1001 		if (ret < 0) {
1002 			dev_err(tplg->dev, "ASoC: invalid control\n");
1003 			return ret;
1004 		}
1005 
1006 	}
1007 
1008 	return 0;
1009 }
1010 
1011 /* optionally pass new dynamic kcontrol to component driver. */
1012 static int soc_tplg_add_route(struct soc_tplg *tplg,
1013 	struct snd_soc_dapm_route *route)
1014 {
1015 	if (tplg->ops && tplg->ops->dapm_route_load)
1016 		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1017 			route);
1018 
1019 	return 0;
1020 }
1021 
1022 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1023 	struct snd_soc_tplg_hdr *hdr)
1024 {
1025 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1026 	const size_t maxlen = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
1027 	struct snd_soc_tplg_dapm_graph_elem *elem;
1028 	struct snd_soc_dapm_route *route;
1029 	int count, i;
1030 	int ret = 0;
1031 
1032 	count = le32_to_cpu(hdr->count);
1033 
1034 	if (soc_tplg_check_elem_count(tplg,
1035 				      sizeof(struct snd_soc_tplg_dapm_graph_elem),
1036 				      count, le32_to_cpu(hdr->payload_size), "graph"))
1037 		return -EINVAL;
1038 
1039 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1040 		hdr->index);
1041 
1042 	for (i = 0; i < count; i++) {
1043 		route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL);
1044 		if (!route)
1045 			return -ENOMEM;
1046 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1047 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1048 
1049 		/* validate routes */
1050 		if ((strnlen(elem->source, maxlen) == maxlen) ||
1051 		    (strnlen(elem->sink, maxlen) == maxlen) ||
1052 		    (strnlen(elem->control, maxlen) == maxlen)) {
1053 			ret = -EINVAL;
1054 			break;
1055 		}
1056 
1057 		route->source = devm_kstrdup(tplg->dev, elem->source, GFP_KERNEL);
1058 		route->sink = devm_kstrdup(tplg->dev, elem->sink, GFP_KERNEL);
1059 		if (!route->source || !route->sink) {
1060 			ret = -ENOMEM;
1061 			break;
1062 		}
1063 
1064 		if (strnlen(elem->control, maxlen) != 0) {
1065 			route->control = devm_kstrdup(tplg->dev, elem->control, GFP_KERNEL);
1066 			if (!route->control) {
1067 				ret = -ENOMEM;
1068 				break;
1069 			}
1070 		}
1071 
1072 		/* add route dobj to dobj_list */
1073 		route->dobj.type = SND_SOC_DOBJ_GRAPH;
1074 		if (tplg->ops)
1075 			route->dobj.unload = tplg->ops->dapm_route_unload;
1076 		route->dobj.index = tplg->index;
1077 		list_add(&route->dobj.list, &tplg->comp->dobj_list);
1078 
1079 		ret = soc_tplg_add_route(tplg, route);
1080 		if (ret < 0) {
1081 			dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
1082 			break;
1083 		}
1084 
1085 		/* add route, but keep going if some fail */
1086 		snd_soc_dapm_add_routes(dapm, route, 1);
1087 	}
1088 
1089 	return ret;
1090 }
1091 
1092 static int soc_tplg_dapm_widget_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1093 {
1094 	struct soc_mixer_control *sm;
1095 	struct snd_soc_tplg_mixer_control *mc;
1096 	int err;
1097 
1098 	mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1099 
1100 	/* validate kcontrol */
1101 	if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1102 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1103 		return -EINVAL;
1104 
1105 	sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
1106 	if (!sm)
1107 		return -ENOMEM;
1108 
1109 	tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) +
1110 		le32_to_cpu(mc->priv.size);
1111 
1112 	dev_dbg(tplg->dev, " adding DAPM widget mixer control %s\n",
1113 		mc->hdr.name);
1114 
1115 	kc->private_value = (long)sm;
1116 	kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL);
1117 	if (!kc->name)
1118 		return -ENOMEM;
1119 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1120 	kc->access = le32_to_cpu(mc->hdr.access);
1121 
1122 	/* we only support FL/FR channel mapping atm */
1123 	sm->reg = tplg_chan_get_reg(tplg, mc->channel,
1124 				    SNDRV_CHMAP_FL);
1125 	sm->rreg = tplg_chan_get_reg(tplg, mc->channel,
1126 				     SNDRV_CHMAP_FR);
1127 	sm->shift = tplg_chan_get_shift(tplg, mc->channel,
1128 					SNDRV_CHMAP_FL);
1129 	sm->rshift = tplg_chan_get_shift(tplg, mc->channel,
1130 					 SNDRV_CHMAP_FR);
1131 
1132 	sm->max = le32_to_cpu(mc->max);
1133 	sm->min = le32_to_cpu(mc->min);
1134 	sm->invert = le32_to_cpu(mc->invert);
1135 	sm->platform_max = le32_to_cpu(mc->platform_max);
1136 	sm->dobj.index = tplg->index;
1137 	INIT_LIST_HEAD(&sm->dobj.list);
1138 
1139 	/* map io handlers */
1140 	err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg);
1141 	if (err) {
1142 		soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1143 		return err;
1144 	}
1145 
1146 	/* create any TLV data */
1147 	err = soc_tplg_create_tlv(tplg, kc, &mc->hdr);
1148 	if (err < 0) {
1149 		dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1150 			mc->hdr.name);
1151 		return err;
1152 	}
1153 
1154 	/* pass control to driver for optional further init */
1155 	err = soc_tplg_control_load(tplg, kc, &mc->hdr);
1156 	if (err < 0)
1157 		return err;
1158 
1159 	return 0;
1160 }
1161 
1162 static int soc_tplg_dapm_widget_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1163 {
1164 	struct snd_soc_tplg_enum_control *ec;
1165 	struct soc_enum *se;
1166 	int err;
1167 
1168 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1169 	/* validate kcontrol */
1170 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1171 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1172 		return -EINVAL;
1173 
1174 	se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL);
1175 	if (!se)
1176 		return -ENOMEM;
1177 
1178 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1179 		      le32_to_cpu(ec->priv.size));
1180 
1181 	dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1182 		ec->hdr.name);
1183 
1184 	kc->private_value = (long)se;
1185 	kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL);
1186 	if (!kc->name)
1187 		return -ENOMEM;
1188 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1189 	kc->access = le32_to_cpu(ec->hdr.access);
1190 
1191 	/* we only support FL/FR channel mapping atm */
1192 	se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1193 	se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
1194 					  SNDRV_CHMAP_FL);
1195 	se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
1196 					  SNDRV_CHMAP_FR);
1197 
1198 	se->items = le32_to_cpu(ec->items);
1199 	se->mask = le32_to_cpu(ec->mask);
1200 	se->dobj.index = tplg->index;
1201 
1202 	switch (le32_to_cpu(ec->hdr.ops.info)) {
1203 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
1204 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1205 		err = soc_tplg_denum_create_values(tplg, se, ec);
1206 		if (err < 0) {
1207 			dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1208 				ec->hdr.name);
1209 			return err;
1210 		}
1211 		fallthrough;
1212 	case SND_SOC_TPLG_CTL_ENUM:
1213 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1214 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1215 		err = soc_tplg_denum_create_texts(tplg, se, ec);
1216 		if (err < 0) {
1217 			dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1218 				ec->hdr.name);
1219 			return err;
1220 		}
1221 		break;
1222 	default:
1223 		dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1224 			ec->hdr.ops.info, ec->hdr.name);
1225 		return -EINVAL;
1226 	}
1227 
1228 	/* map io handlers */
1229 	err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
1230 	if (err) {
1231 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1232 		return err;
1233 	}
1234 
1235 	/* pass control to driver for optional further init */
1236 	err = soc_tplg_control_load(tplg, kc, &ec->hdr);
1237 	if (err < 0)
1238 		return err;
1239 
1240 	return 0;
1241 }
1242 
1243 static int soc_tplg_dapm_widget_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
1244 {
1245 	struct snd_soc_tplg_bytes_control *be;
1246 	struct soc_bytes_ext *sbe;
1247 	int err;
1248 
1249 	be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1250 
1251 	/* validate kcontrol */
1252 	if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1253 	    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1254 		return -EINVAL;
1255 
1256 	sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
1257 	if (!sbe)
1258 		return -ENOMEM;
1259 
1260 	tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1261 		      le32_to_cpu(be->priv.size));
1262 
1263 	dev_dbg(tplg->dev,
1264 		"ASoC: adding bytes kcontrol %s with access 0x%x\n",
1265 		be->hdr.name, be->hdr.access);
1266 
1267 	kc->private_value = (long)sbe;
1268 	kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL);
1269 	if (!kc->name)
1270 		return -ENOMEM;
1271 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1272 	kc->access = le32_to_cpu(be->hdr.access);
1273 
1274 	sbe->max = le32_to_cpu(be->max);
1275 	INIT_LIST_HEAD(&sbe->dobj.list);
1276 
1277 	/* map standard io handlers and check for external handlers */
1278 	err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg);
1279 	if (err) {
1280 		soc_control_err(tplg, &be->hdr, be->hdr.name);
1281 		return err;
1282 	}
1283 
1284 	/* pass control to driver for optional further init */
1285 	err = soc_tplg_control_load(tplg, kc, &be->hdr);
1286 	if (err < 0)
1287 		return err;
1288 
1289 	return 0;
1290 }
1291 
1292 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1293 	struct snd_soc_tplg_dapm_widget *w)
1294 {
1295 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1296 	struct snd_soc_dapm_widget template, *widget;
1297 	struct snd_soc_tplg_ctl_hdr *control_hdr;
1298 	struct snd_soc_card *card = tplg->comp->card;
1299 	unsigned int *kcontrol_type = NULL;
1300 	struct snd_kcontrol_new *kc;
1301 	int mixer_count = 0;
1302 	int bytes_count = 0;
1303 	int enum_count = 0;
1304 	int ret = 0;
1305 	int i;
1306 
1307 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1308 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1309 		return -EINVAL;
1310 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1311 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1312 		return -EINVAL;
1313 
1314 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1315 		w->name, w->id);
1316 
1317 	memset(&template, 0, sizeof(template));
1318 
1319 	/* map user to kernel widget ID */
1320 	template.id = get_widget_id(le32_to_cpu(w->id));
1321 	if ((int)template.id < 0)
1322 		return template.id;
1323 
1324 	/* strings are allocated here, but used and freed by the widget */
1325 	template.name = kstrdup(w->name, GFP_KERNEL);
1326 	if (!template.name)
1327 		return -ENOMEM;
1328 	template.sname = kstrdup(w->sname, GFP_KERNEL);
1329 	if (!template.sname) {
1330 		ret = -ENOMEM;
1331 		goto err;
1332 	}
1333 	template.reg = le32_to_cpu(w->reg);
1334 	template.shift = le32_to_cpu(w->shift);
1335 	template.mask = le32_to_cpu(w->mask);
1336 	template.subseq = le32_to_cpu(w->subseq);
1337 	template.on_val = w->invert ? 0 : 1;
1338 	template.off_val = w->invert ? 1 : 0;
1339 	template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1340 	template.event_flags = le16_to_cpu(w->event_flags);
1341 	template.dobj.index = tplg->index;
1342 
1343 	tplg->pos +=
1344 		(sizeof(struct snd_soc_tplg_dapm_widget) +
1345 		 le32_to_cpu(w->priv.size));
1346 
1347 	if (w->num_kcontrols == 0) {
1348 		template.num_kcontrols = 0;
1349 		goto widget;
1350 	}
1351 
1352 	template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1353 	kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL);
1354 	if (!kc) {
1355 		ret = -ENOMEM;
1356 		goto hdr_err;
1357 	}
1358 
1359 	kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int),
1360 				     GFP_KERNEL);
1361 	if (!kcontrol_type) {
1362 		ret = -ENOMEM;
1363 		goto hdr_err;
1364 	}
1365 
1366 	for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) {
1367 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1368 		switch (le32_to_cpu(control_hdr->ops.info)) {
1369 		case SND_SOC_TPLG_CTL_VOLSW:
1370 		case SND_SOC_TPLG_CTL_STROBE:
1371 		case SND_SOC_TPLG_CTL_VOLSW_SX:
1372 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1373 		case SND_SOC_TPLG_CTL_RANGE:
1374 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1375 			/* volume mixer */
1376 			kc[i].index = mixer_count;
1377 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER;
1378 			mixer_count++;
1379 			ret = soc_tplg_dapm_widget_dmixer_create(tplg, &kc[i]);
1380 			if (ret < 0)
1381 				goto hdr_err;
1382 			break;
1383 		case SND_SOC_TPLG_CTL_ENUM:
1384 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
1385 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1386 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1387 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1388 			/* enumerated mixer */
1389 			kc[i].index = enum_count;
1390 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM;
1391 			enum_count++;
1392 			ret = soc_tplg_dapm_widget_denum_create(tplg, &kc[i]);
1393 			if (ret < 0)
1394 				goto hdr_err;
1395 			break;
1396 		case SND_SOC_TPLG_CTL_BYTES:
1397 			/* bytes control */
1398 			kc[i].index = bytes_count;
1399 			kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES;
1400 			bytes_count++;
1401 			ret = soc_tplg_dapm_widget_dbytes_create(tplg, &kc[i]);
1402 			if (ret < 0)
1403 				goto hdr_err;
1404 			break;
1405 		default:
1406 			dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1407 				control_hdr->ops.get, control_hdr->ops.put,
1408 				le32_to_cpu(control_hdr->ops.info));
1409 			ret = -EINVAL;
1410 			goto hdr_err;
1411 		}
1412 	}
1413 
1414 	template.kcontrol_news = kc;
1415 	dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n",
1416 		w->name, mixer_count, enum_count, bytes_count);
1417 
1418 widget:
1419 	ret = soc_tplg_widget_load(tplg, &template, w);
1420 	if (ret < 0)
1421 		goto hdr_err;
1422 
1423 	/* card dapm mutex is held by the core if we are loading topology
1424 	 * data during sound card init. */
1425 	if (snd_soc_card_is_instantiated(card))
1426 		widget = snd_soc_dapm_new_control(dapm, &template);
1427 	else
1428 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1429 	if (IS_ERR(widget)) {
1430 		ret = PTR_ERR(widget);
1431 		goto hdr_err;
1432 	}
1433 
1434 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1435 	widget->dobj.widget.kcontrol_type = kcontrol_type;
1436 	if (tplg->ops)
1437 		widget->dobj.unload = tplg->ops->widget_unload;
1438 	widget->dobj.index = tplg->index;
1439 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1440 
1441 	ret = soc_tplg_widget_ready(tplg, widget, w);
1442 	if (ret < 0)
1443 		goto ready_err;
1444 
1445 	kfree(template.sname);
1446 	kfree(template.name);
1447 
1448 	return 0;
1449 
1450 ready_err:
1451 	soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET);
1452 	snd_soc_dapm_free_widget(widget);
1453 hdr_err:
1454 	kfree(template.sname);
1455 err:
1456 	kfree(template.name);
1457 	return ret;
1458 }
1459 
1460 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1461 	struct snd_soc_tplg_hdr *hdr)
1462 {
1463 	int count, i;
1464 
1465 	count = le32_to_cpu(hdr->count);
1466 
1467 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1468 
1469 	for (i = 0; i < count; i++) {
1470 		struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1471 		int ret;
1472 
1473 		/*
1474 		 * check if widget itself fits within topology file
1475 		 * use sizeof instead of widget->size, as we can't be sure
1476 		 * it is set properly yet (file may end before it is present)
1477 		 */
1478 		if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) {
1479 			dev_err(tplg->dev, "ASoC: invalid widget data size\n");
1480 			return -EINVAL;
1481 		}
1482 
1483 		/* check if widget has proper size */
1484 		if (le32_to_cpu(widget->size) != sizeof(*widget)) {
1485 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
1486 			return -EINVAL;
1487 		}
1488 
1489 		/* check if widget private data fits within topology file */
1490 		if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) {
1491 			dev_err(tplg->dev, "ASoC: invalid widget private data size\n");
1492 			return -EINVAL;
1493 		}
1494 
1495 		ret = soc_tplg_dapm_widget_create(tplg, widget);
1496 		if (ret < 0) {
1497 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1498 				widget->name);
1499 			return ret;
1500 		}
1501 	}
1502 
1503 	return 0;
1504 }
1505 
1506 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1507 {
1508 	struct snd_soc_card *card = tplg->comp->card;
1509 	int ret;
1510 
1511 	/* Card might not have been registered at this point.
1512 	 * If so, just return success.
1513 	*/
1514 	if (!snd_soc_card_is_instantiated(card)) {
1515 		dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n");
1516 		return 0;
1517 	}
1518 
1519 	ret = snd_soc_dapm_new_widgets(card);
1520 	if (ret < 0)
1521 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret);
1522 
1523 	return ret;
1524 }
1525 
1526 static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream,
1527 			   struct snd_soc_tplg_stream_caps *caps)
1528 {
1529 	stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL);
1530 	if (!stream->stream_name)
1531 		return -ENOMEM;
1532 
1533 	stream->channels_min = le32_to_cpu(caps->channels_min);
1534 	stream->channels_max = le32_to_cpu(caps->channels_max);
1535 	stream->rates = le32_to_cpu(caps->rates);
1536 	stream->rate_min = le32_to_cpu(caps->rate_min);
1537 	stream->rate_max = le32_to_cpu(caps->rate_max);
1538 	stream->formats = le64_to_cpu(caps->formats);
1539 	stream->sig_bits = le32_to_cpu(caps->sig_bits);
1540 
1541 	return 0;
1542 }
1543 
1544 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1545 			  unsigned int flag_mask, unsigned int flags)
1546 {
1547 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1548 		dai_drv->symmetric_rate =
1549 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1550 
1551 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1552 		dai_drv->symmetric_channels =
1553 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ?
1554 			1 : 0;
1555 
1556 	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1557 		dai_drv->symmetric_sample_bits =
1558 			(flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1559 			1 : 0;
1560 }
1561 
1562 static const struct snd_soc_dai_ops tplg_dai_ops = {
1563 	.compress_new	= snd_soc_new_compress,
1564 };
1565 
1566 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1567 	struct snd_soc_tplg_pcm *pcm)
1568 {
1569 	struct snd_soc_dai_driver *dai_drv;
1570 	struct snd_soc_pcm_stream *stream;
1571 	struct snd_soc_tplg_stream_caps *caps;
1572 	struct snd_soc_dai *dai;
1573 	struct snd_soc_dapm_context *dapm =
1574 		snd_soc_component_get_dapm(tplg->comp);
1575 	int ret;
1576 
1577 	dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1578 	if (dai_drv == NULL)
1579 		return -ENOMEM;
1580 
1581 	if (strlen(pcm->dai_name)) {
1582 		dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1583 		if (!dai_drv->name) {
1584 			ret = -ENOMEM;
1585 			goto err;
1586 		}
1587 	}
1588 	dai_drv->id = le32_to_cpu(pcm->dai_id);
1589 
1590 	if (pcm->playback) {
1591 		stream = &dai_drv->playback;
1592 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1593 		ret = set_stream_info(tplg, stream, caps);
1594 		if (ret < 0)
1595 			goto err;
1596 	}
1597 
1598 	if (pcm->capture) {
1599 		stream = &dai_drv->capture;
1600 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1601 		ret = set_stream_info(tplg, stream, caps);
1602 		if (ret < 0)
1603 			goto err;
1604 	}
1605 
1606 	if (pcm->compress)
1607 		dai_drv->ops = &tplg_dai_ops;
1608 
1609 	/* pass control to component driver for optional further init */
1610 	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1611 	if (ret < 0) {
1612 		dev_err(tplg->dev, "ASoC: DAI loading failed\n");
1613 		goto err;
1614 	}
1615 
1616 	dai_drv->dobj.index = tplg->index;
1617 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1618 	if (tplg->ops)
1619 		dai_drv->dobj.unload = tplg->ops->dai_unload;
1620 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1621 
1622 	/* register the DAI to the component */
1623 	dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1624 	if (!dai)
1625 		return -ENOMEM;
1626 
1627 	/* Create the DAI widgets here */
1628 	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1629 	if (ret != 0) {
1630 		dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1631 		snd_soc_unregister_dai(dai);
1632 		return ret;
1633 	}
1634 
1635 	return 0;
1636 
1637 err:
1638 	return ret;
1639 }
1640 
1641 static void set_link_flags(struct snd_soc_dai_link *link,
1642 		unsigned int flag_mask, unsigned int flags)
1643 {
1644 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1645 		link->symmetric_rate =
1646 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1647 
1648 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1649 		link->symmetric_channels =
1650 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ?
1651 			1 : 0;
1652 
1653 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1654 		link->symmetric_sample_bits =
1655 			(flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1656 			1 : 0;
1657 
1658 	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1659 		link->ignore_suspend =
1660 			(flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ?
1661 			1 : 0;
1662 }
1663 
1664 /* create the FE DAI link */
1665 static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1666 	struct snd_soc_tplg_pcm *pcm)
1667 {
1668 	struct snd_soc_dai_link *link;
1669 	struct snd_soc_dai_link_component *dlc;
1670 	int ret;
1671 
1672 	/* link + cpu + codec + platform */
1673 	link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1674 	if (link == NULL)
1675 		return -ENOMEM;
1676 
1677 	dlc = (struct snd_soc_dai_link_component *)(link + 1);
1678 
1679 	link->cpus	= &dlc[0];
1680 	link->num_cpus	 = 1;
1681 
1682 	link->dobj.index = tplg->index;
1683 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1684 	if (tplg->ops)
1685 		link->dobj.unload = tplg->ops->link_unload;
1686 
1687 	if (strlen(pcm->pcm_name)) {
1688 		link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1689 		link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1690 		if (!link->name || !link->stream_name) {
1691 			ret = -ENOMEM;
1692 			goto err;
1693 		}
1694 	}
1695 	link->id = le32_to_cpu(pcm->pcm_id);
1696 
1697 	if (strlen(pcm->dai_name)) {
1698 		link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1699 		if (!link->cpus->dai_name) {
1700 			ret = -ENOMEM;
1701 			goto err;
1702 		}
1703 	}
1704 
1705 	/*
1706 	 * Many topology are assuming link has Codec / Platform, and
1707 	 * these might be overwritten at soc_tplg_dai_link_load().
1708 	 * Don't use &asoc_dummy_dlc here.
1709 	 */
1710 	link->codecs		= &dlc[1];	/* Don't use &asoc_dummy_dlc here */
1711 	link->codecs->name	= "snd-soc-dummy";
1712 	link->codecs->dai_name	= "snd-soc-dummy-dai";
1713 	link->num_codecs	= 1;
1714 
1715 	link->platforms		= &dlc[2];	/* Don't use &asoc_dummy_dlc here */
1716 	link->platforms->name	= "snd-soc-dummy";
1717 	link->num_platforms	= 1;
1718 
1719 	/* enable DPCM */
1720 	link->dynamic = 1;
1721 	link->ignore_pmdown_time = 1;
1722 	link->dpcm_playback = le32_to_cpu(pcm->playback);
1723 	link->dpcm_capture = le32_to_cpu(pcm->capture);
1724 	if (pcm->flag_mask)
1725 		set_link_flags(link,
1726 			       le32_to_cpu(pcm->flag_mask),
1727 			       le32_to_cpu(pcm->flags));
1728 
1729 	/* pass control to component driver for optional further init */
1730 	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1731 	if (ret < 0) {
1732 		dev_err(tplg->dev, "ASoC: FE link loading failed\n");
1733 		goto err;
1734 	}
1735 
1736 	ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1);
1737 	if (ret < 0) {
1738 		if (ret != -EPROBE_DEFER)
1739 			dev_err(tplg->dev, "ASoC: adding FE link failed\n");
1740 		goto err;
1741 	}
1742 
1743 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1744 
1745 	return 0;
1746 err:
1747 	return ret;
1748 }
1749 
1750 /* create a FE DAI and DAI link from the PCM object */
1751 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1752 	struct snd_soc_tplg_pcm *pcm)
1753 {
1754 	int ret;
1755 
1756 	ret = soc_tplg_dai_create(tplg, pcm);
1757 	if (ret < 0)
1758 		return ret;
1759 
1760 	return  soc_tplg_fe_link_create(tplg, pcm);
1761 }
1762 
1763 /* copy stream caps from the old version 4 of source */
1764 static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1765 				struct snd_soc_tplg_stream_caps_v4 *src)
1766 {
1767 	dest->size = cpu_to_le32(sizeof(*dest));
1768 	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1769 	dest->formats = src->formats;
1770 	dest->rates = src->rates;
1771 	dest->rate_min = src->rate_min;
1772 	dest->rate_max = src->rate_max;
1773 	dest->channels_min = src->channels_min;
1774 	dest->channels_max = src->channels_max;
1775 	dest->periods_min = src->periods_min;
1776 	dest->periods_max = src->periods_max;
1777 	dest->period_size_min = src->period_size_min;
1778 	dest->period_size_max = src->period_size_max;
1779 	dest->buffer_size_min = src->buffer_size_min;
1780 	dest->buffer_size_max = src->buffer_size_max;
1781 }
1782 
1783 /**
1784  * pcm_new_ver - Create the new version of PCM from the old version.
1785  * @tplg: topology context
1786  * @src: older version of pcm as a source
1787  * @pcm: latest version of pcm created from the source
1788  *
1789  * Support from version 4. User should free the returned pcm manually.
1790  */
1791 static int pcm_new_ver(struct soc_tplg *tplg,
1792 		       struct snd_soc_tplg_pcm *src,
1793 		       struct snd_soc_tplg_pcm **pcm)
1794 {
1795 	struct snd_soc_tplg_pcm *dest;
1796 	struct snd_soc_tplg_pcm_v4 *src_v4;
1797 	int i;
1798 
1799 	*pcm = NULL;
1800 
1801 	if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
1802 		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1803 		return -EINVAL;
1804 	}
1805 
1806 	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1807 	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1808 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1809 	if (!dest)
1810 		return -ENOMEM;
1811 
1812 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
1813 	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1814 	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1815 	dest->pcm_id = src_v4->pcm_id;
1816 	dest->dai_id = src_v4->dai_id;
1817 	dest->playback = src_v4->playback;
1818 	dest->capture = src_v4->capture;
1819 	dest->compress = src_v4->compress;
1820 	dest->num_streams = src_v4->num_streams;
1821 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
1822 		memcpy(&dest->stream[i], &src_v4->stream[i],
1823 		       sizeof(struct snd_soc_tplg_stream));
1824 
1825 	for (i = 0; i < 2; i++)
1826 		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1827 
1828 	*pcm = dest;
1829 	return 0;
1830 }
1831 
1832 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1833 	struct snd_soc_tplg_hdr *hdr)
1834 {
1835 	struct snd_soc_tplg_pcm *pcm, *_pcm;
1836 	int count;
1837 	int size;
1838 	int i;
1839 	bool abi_match;
1840 	int ret;
1841 
1842 	count = le32_to_cpu(hdr->count);
1843 
1844 	/* check the element size and count */
1845 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1846 	size = le32_to_cpu(pcm->size);
1847 	if (size > sizeof(struct snd_soc_tplg_pcm)
1848 		|| size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1849 		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1850 			size);
1851 		return -EINVAL;
1852 	}
1853 
1854 	if (soc_tplg_check_elem_count(tplg,
1855 				      size, count,
1856 				      le32_to_cpu(hdr->payload_size),
1857 				      "PCM DAI"))
1858 		return -EINVAL;
1859 
1860 	for (i = 0; i < count; i++) {
1861 		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1862 		size = le32_to_cpu(pcm->size);
1863 
1864 		/* check ABI version by size, create a new version of pcm
1865 		 * if abi not match.
1866 		 */
1867 		if (size == sizeof(*pcm)) {
1868 			abi_match = true;
1869 			_pcm = pcm;
1870 		} else {
1871 			abi_match = false;
1872 			ret = pcm_new_ver(tplg, pcm, &_pcm);
1873 			if (ret < 0)
1874 				return ret;
1875 		}
1876 
1877 		/* create the FE DAIs and DAI links */
1878 		ret = soc_tplg_pcm_create(tplg, _pcm);
1879 		if (ret < 0) {
1880 			if (!abi_match)
1881 				kfree(_pcm);
1882 			return ret;
1883 		}
1884 
1885 		/* offset by version-specific struct size and
1886 		 * real priv data size
1887 		 */
1888 		tplg->pos += size + le32_to_cpu(_pcm->priv.size);
1889 
1890 		if (!abi_match)
1891 			kfree(_pcm); /* free the duplicated one */
1892 	}
1893 
1894 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1895 
1896 	return 0;
1897 }
1898 
1899 /**
1900  * set_link_hw_format - Set the HW audio format of the physical DAI link.
1901  * @link: &snd_soc_dai_link which should be updated
1902  * @cfg: physical link configs.
1903  *
1904  * Topology context contains a list of supported HW formats (configs) and
1905  * a default format ID for the physical link. This function will use this
1906  * default ID to choose the HW format to set the link's DAI format for init.
1907  */
1908 static void set_link_hw_format(struct snd_soc_dai_link *link,
1909 			struct snd_soc_tplg_link_config *cfg)
1910 {
1911 	struct snd_soc_tplg_hw_config *hw_config;
1912 	unsigned char bclk_provider, fsync_provider;
1913 	unsigned char invert_bclk, invert_fsync;
1914 	int i;
1915 
1916 	for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
1917 		hw_config = &cfg->hw_config[i];
1918 		if (hw_config->id != cfg->default_hw_config_id)
1919 			continue;
1920 
1921 		link->dai_fmt = le32_to_cpu(hw_config->fmt) &
1922 			SND_SOC_DAIFMT_FORMAT_MASK;
1923 
1924 		/* clock gating */
1925 		switch (hw_config->clock_gated) {
1926 		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
1927 			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
1928 			break;
1929 
1930 		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
1931 			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
1932 			break;
1933 
1934 		default:
1935 			/* ignore the value */
1936 			break;
1937 		}
1938 
1939 		/* clock signal polarity */
1940 		invert_bclk = hw_config->invert_bclk;
1941 		invert_fsync = hw_config->invert_fsync;
1942 		if (!invert_bclk && !invert_fsync)
1943 			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1944 		else if (!invert_bclk && invert_fsync)
1945 			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1946 		else if (invert_bclk && !invert_fsync)
1947 			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1948 		else
1949 			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1950 
1951 		/* clock masters */
1952 		bclk_provider = (hw_config->bclk_provider ==
1953 			       SND_SOC_TPLG_BCLK_CP);
1954 		fsync_provider = (hw_config->fsync_provider ==
1955 				SND_SOC_TPLG_FSYNC_CP);
1956 		if (bclk_provider && fsync_provider)
1957 			link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
1958 		else if (!bclk_provider && fsync_provider)
1959 			link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
1960 		else if (bclk_provider && !fsync_provider)
1961 			link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
1962 		else
1963 			link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
1964 	}
1965 }
1966 
1967 /**
1968  * link_new_ver - Create a new physical link config from the old
1969  * version of source.
1970  * @tplg: topology context
1971  * @src: old version of phyical link config as a source
1972  * @link: latest version of physical link config created from the source
1973  *
1974  * Support from version 4. User need free the returned link config manually.
1975  */
1976 static int link_new_ver(struct soc_tplg *tplg,
1977 			struct snd_soc_tplg_link_config *src,
1978 			struct snd_soc_tplg_link_config **link)
1979 {
1980 	struct snd_soc_tplg_link_config *dest;
1981 	struct snd_soc_tplg_link_config_v4 *src_v4;
1982 	int i;
1983 
1984 	*link = NULL;
1985 
1986 	if (le32_to_cpu(src->size) !=
1987 	    sizeof(struct snd_soc_tplg_link_config_v4)) {
1988 		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
1989 		return -EINVAL;
1990 	}
1991 
1992 	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
1993 
1994 	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
1995 	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1996 	if (!dest)
1997 		return -ENOMEM;
1998 
1999 	dest->size = cpu_to_le32(sizeof(*dest));
2000 	dest->id = src_v4->id;
2001 	dest->num_streams = src_v4->num_streams;
2002 	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2003 		memcpy(&dest->stream[i], &src_v4->stream[i],
2004 		       sizeof(struct snd_soc_tplg_stream));
2005 
2006 	*link = dest;
2007 	return 0;
2008 }
2009 
2010 /**
2011  * snd_soc_find_dai_link - Find a DAI link
2012  *
2013  * @card: soc card
2014  * @id: DAI link ID to match
2015  * @name: DAI link name to match, optional
2016  * @stream_name: DAI link stream name to match, optional
2017  *
2018  * This function will search all existing DAI links of the soc card to
2019  * find the link of the same ID. Since DAI links may not have their
2020  * unique ID, so name and stream name should also match if being
2021  * specified.
2022  *
2023  * Return: pointer of DAI link, or NULL if not found.
2024  */
2025 static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2026 						      int id, const char *name,
2027 						      const char *stream_name)
2028 {
2029 	struct snd_soc_pcm_runtime *rtd;
2030 
2031 	for_each_card_rtds(card, rtd) {
2032 		struct snd_soc_dai_link *link = rtd->dai_link;
2033 
2034 		if (link->id != id)
2035 			continue;
2036 
2037 		if (name && (!link->name || !strstr(link->name, name)))
2038 			continue;
2039 
2040 		if (stream_name && (!link->stream_name ||
2041 				    !strstr(link->stream_name, stream_name)))
2042 			continue;
2043 
2044 		return link;
2045 	}
2046 
2047 	return NULL;
2048 }
2049 
2050 /* Find and configure an existing physical DAI link */
2051 static int soc_tplg_link_config(struct soc_tplg *tplg,
2052 	struct snd_soc_tplg_link_config *cfg)
2053 {
2054 	struct snd_soc_dai_link *link;
2055 	const char *name, *stream_name;
2056 	size_t len;
2057 	int ret;
2058 
2059 	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2060 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2061 		return -EINVAL;
2062 	else if (len)
2063 		name = cfg->name;
2064 	else
2065 		name = NULL;
2066 
2067 	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2068 	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2069 		return -EINVAL;
2070 	else if (len)
2071 		stream_name = cfg->stream_name;
2072 	else
2073 		stream_name = NULL;
2074 
2075 	link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2076 				     name, stream_name);
2077 	if (!link) {
2078 		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2079 			name, cfg->id);
2080 		return -EINVAL;
2081 	}
2082 
2083 	/* hw format */
2084 	if (cfg->num_hw_configs)
2085 		set_link_hw_format(link, cfg);
2086 
2087 	/* flags */
2088 	if (cfg->flag_mask)
2089 		set_link_flags(link,
2090 			       le32_to_cpu(cfg->flag_mask),
2091 			       le32_to_cpu(cfg->flags));
2092 
2093 	/* pass control to component driver for optional further init */
2094 	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2095 	if (ret < 0) {
2096 		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2097 		return ret;
2098 	}
2099 
2100 	/* for unloading it in snd_soc_tplg_component_remove */
2101 	link->dobj.index = tplg->index;
2102 	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2103 	if (tplg->ops)
2104 		link->dobj.unload = tplg->ops->link_unload;
2105 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
2106 
2107 	return 0;
2108 }
2109 
2110 
2111 /* Load physical link config elements from the topology context */
2112 static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2113 	struct snd_soc_tplg_hdr *hdr)
2114 {
2115 	struct snd_soc_tplg_link_config *link, *_link;
2116 	int count;
2117 	int size;
2118 	int i, ret;
2119 	bool abi_match;
2120 
2121 	count = le32_to_cpu(hdr->count);
2122 
2123 	/* check the element size and count */
2124 	link = (struct snd_soc_tplg_link_config *)tplg->pos;
2125 	size = le32_to_cpu(link->size);
2126 	if (size > sizeof(struct snd_soc_tplg_link_config)
2127 		|| size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2128 		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2129 			size);
2130 		return -EINVAL;
2131 	}
2132 
2133 	if (soc_tplg_check_elem_count(tplg, size, count,
2134 				      le32_to_cpu(hdr->payload_size),
2135 				      "physical link config"))
2136 		return -EINVAL;
2137 
2138 	/* config physical DAI links */
2139 	for (i = 0; i < count; i++) {
2140 		link = (struct snd_soc_tplg_link_config *)tplg->pos;
2141 		size = le32_to_cpu(link->size);
2142 		if (size == sizeof(*link)) {
2143 			abi_match = true;
2144 			_link = link;
2145 		} else {
2146 			abi_match = false;
2147 			ret = link_new_ver(tplg, link, &_link);
2148 			if (ret < 0)
2149 				return ret;
2150 		}
2151 
2152 		ret = soc_tplg_link_config(tplg, _link);
2153 		if (ret < 0) {
2154 			if (!abi_match)
2155 				kfree(_link);
2156 			return ret;
2157 		}
2158 
2159 		/* offset by version-specific struct size and
2160 		 * real priv data size
2161 		 */
2162 		tplg->pos += size + le32_to_cpu(_link->priv.size);
2163 
2164 		if (!abi_match)
2165 			kfree(_link); /* free the duplicated one */
2166 	}
2167 
2168 	return 0;
2169 }
2170 
2171 /**
2172  * soc_tplg_dai_config - Find and configure an existing physical DAI.
2173  * @tplg: topology context
2174  * @d: physical DAI configs.
2175  *
2176  * The physical dai should already be registered by the platform driver.
2177  * The platform driver should specify the DAI name and ID for matching.
2178  */
2179 static int soc_tplg_dai_config(struct soc_tplg *tplg,
2180 			       struct snd_soc_tplg_dai *d)
2181 {
2182 	struct snd_soc_dai_link_component dai_component;
2183 	struct snd_soc_dai *dai;
2184 	struct snd_soc_dai_driver *dai_drv;
2185 	struct snd_soc_pcm_stream *stream;
2186 	struct snd_soc_tplg_stream_caps *caps;
2187 	int ret;
2188 
2189 	memset(&dai_component, 0, sizeof(dai_component));
2190 
2191 	dai_component.dai_name = d->dai_name;
2192 	dai = snd_soc_find_dai(&dai_component);
2193 	if (!dai) {
2194 		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2195 			d->dai_name);
2196 		return -EINVAL;
2197 	}
2198 
2199 	if (le32_to_cpu(d->dai_id) != dai->id) {
2200 		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2201 			d->dai_name);
2202 		return -EINVAL;
2203 	}
2204 
2205 	dai_drv = dai->driver;
2206 	if (!dai_drv)
2207 		return -EINVAL;
2208 
2209 	if (d->playback) {
2210 		stream = &dai_drv->playback;
2211 		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2212 		ret = set_stream_info(tplg, stream, caps);
2213 		if (ret < 0)
2214 			goto err;
2215 	}
2216 
2217 	if (d->capture) {
2218 		stream = &dai_drv->capture;
2219 		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2220 		ret = set_stream_info(tplg, stream, caps);
2221 		if (ret < 0)
2222 			goto err;
2223 	}
2224 
2225 	if (d->flag_mask)
2226 		set_dai_flags(dai_drv,
2227 			      le32_to_cpu(d->flag_mask),
2228 			      le32_to_cpu(d->flags));
2229 
2230 	/* pass control to component driver for optional further init */
2231 	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2232 	if (ret < 0) {
2233 		dev_err(tplg->dev, "ASoC: DAI loading failed\n");
2234 		goto err;
2235 	}
2236 
2237 	return 0;
2238 
2239 err:
2240 	return ret;
2241 }
2242 
2243 /* load physical DAI elements */
2244 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2245 				   struct snd_soc_tplg_hdr *hdr)
2246 {
2247 	int count;
2248 	int i;
2249 
2250 	count = le32_to_cpu(hdr->count);
2251 
2252 	/* config the existing BE DAIs */
2253 	for (i = 0; i < count; i++) {
2254 		struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos;
2255 		int ret;
2256 
2257 		if (le32_to_cpu(dai->size) != sizeof(*dai)) {
2258 			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2259 			return -EINVAL;
2260 		}
2261 
2262 		ret = soc_tplg_dai_config(tplg, dai);
2263 		if (ret < 0) {
2264 			dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2265 			return ret;
2266 		}
2267 
2268 		tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
2269 	}
2270 
2271 	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2272 	return 0;
2273 }
2274 
2275 /**
2276  * manifest_new_ver - Create a new version of manifest from the old version
2277  * of source.
2278  * @tplg: topology context
2279  * @src: old version of manifest as a source
2280  * @manifest: latest version of manifest created from the source
2281  *
2282  * Support from version 4. Users need free the returned manifest manually.
2283  */
2284 static int manifest_new_ver(struct soc_tplg *tplg,
2285 			    struct snd_soc_tplg_manifest *src,
2286 			    struct snd_soc_tplg_manifest **manifest)
2287 {
2288 	struct snd_soc_tplg_manifest *dest;
2289 	struct snd_soc_tplg_manifest_v4 *src_v4;
2290 	int size;
2291 
2292 	*manifest = NULL;
2293 
2294 	size = le32_to_cpu(src->size);
2295 	if (size != sizeof(*src_v4)) {
2296 		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2297 			 size);
2298 		if (size)
2299 			return -EINVAL;
2300 		src->size = cpu_to_le32(sizeof(*src_v4));
2301 	}
2302 
2303 	dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2304 
2305 	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2306 	dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2307 		       GFP_KERNEL);
2308 	if (!dest)
2309 		return -ENOMEM;
2310 
2311 	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2312 	dest->control_elems = src_v4->control_elems;
2313 	dest->widget_elems = src_v4->widget_elems;
2314 	dest->graph_elems = src_v4->graph_elems;
2315 	dest->pcm_elems = src_v4->pcm_elems;
2316 	dest->dai_link_elems = src_v4->dai_link_elems;
2317 	dest->priv.size = src_v4->priv.size;
2318 	if (dest->priv.size)
2319 		memcpy(dest->priv.data, src_v4->priv.data,
2320 		       le32_to_cpu(src_v4->priv.size));
2321 
2322 	*manifest = dest;
2323 	return 0;
2324 }
2325 
2326 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2327 				  struct snd_soc_tplg_hdr *hdr)
2328 {
2329 	struct snd_soc_tplg_manifest *manifest, *_manifest;
2330 	bool abi_match;
2331 	int ret = 0;
2332 
2333 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2334 
2335 	/* check ABI version by size, create a new manifest if abi not match */
2336 	if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2337 		abi_match = true;
2338 		_manifest = manifest;
2339 	} else {
2340 		abi_match = false;
2341 
2342 		ret = manifest_new_ver(tplg, manifest, &_manifest);
2343 		if (ret < 0)
2344 			return ret;
2345 	}
2346 
2347 	/* pass control to component driver for optional further init */
2348 	if (tplg->ops && tplg->ops->manifest)
2349 		ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
2350 
2351 	if (!abi_match)	/* free the duplicated one */
2352 		kfree(_manifest);
2353 
2354 	return ret;
2355 }
2356 
2357 /* validate header magic, size and type */
2358 static int soc_tplg_valid_header(struct soc_tplg *tplg,
2359 	struct snd_soc_tplg_hdr *hdr)
2360 {
2361 	if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
2362 		dev_err(tplg->dev,
2363 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2364 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2365 			tplg->fw->size);
2366 		return -EINVAL;
2367 	}
2368 
2369 	if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) {
2370 		dev_err(tplg->dev,
2371 			"ASoC: invalid header of type %d at offset %ld payload_size %d\n",
2372 			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2373 			hdr->payload_size);
2374 		return -EINVAL;
2375 	}
2376 
2377 	/* big endian firmware objects not supported atm */
2378 	if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
2379 		dev_err(tplg->dev,
2380 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2381 			tplg->pass, hdr->magic,
2382 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2383 		return -EINVAL;
2384 	}
2385 
2386 	if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
2387 		dev_err(tplg->dev,
2388 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2389 			tplg->pass, hdr->magic,
2390 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2391 		return -EINVAL;
2392 	}
2393 
2394 	/* Support ABI from version 4 */
2395 	if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2396 	    le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
2397 		dev_err(tplg->dev,
2398 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2399 			tplg->pass, hdr->abi,
2400 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2401 			tplg->fw->size);
2402 		return -EINVAL;
2403 	}
2404 
2405 	if (hdr->payload_size == 0) {
2406 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2407 			soc_tplg_get_hdr_offset(tplg));
2408 		return -EINVAL;
2409 	}
2410 
2411 	return 0;
2412 }
2413 
2414 /* check header type and call appropriate handler */
2415 static int soc_tplg_load_header(struct soc_tplg *tplg,
2416 	struct snd_soc_tplg_hdr *hdr)
2417 {
2418 	int (*elem_load)(struct soc_tplg *tplg,
2419 			 struct snd_soc_tplg_hdr *hdr);
2420 	unsigned int hdr_pass;
2421 
2422 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2423 
2424 	tplg->index = le32_to_cpu(hdr->index);
2425 
2426 	switch (le32_to_cpu(hdr->type)) {
2427 	case SND_SOC_TPLG_TYPE_MIXER:
2428 	case SND_SOC_TPLG_TYPE_ENUM:
2429 	case SND_SOC_TPLG_TYPE_BYTES:
2430 		hdr_pass = SOC_TPLG_PASS_CONTROL;
2431 		elem_load = soc_tplg_kcontrol_elems_load;
2432 		break;
2433 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2434 		hdr_pass = SOC_TPLG_PASS_GRAPH;
2435 		elem_load = soc_tplg_dapm_graph_elems_load;
2436 		break;
2437 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2438 		hdr_pass = SOC_TPLG_PASS_WIDGET;
2439 		elem_load = soc_tplg_dapm_widget_elems_load;
2440 		break;
2441 	case SND_SOC_TPLG_TYPE_PCM:
2442 		hdr_pass = SOC_TPLG_PASS_PCM_DAI;
2443 		elem_load = soc_tplg_pcm_elems_load;
2444 		break;
2445 	case SND_SOC_TPLG_TYPE_DAI:
2446 		hdr_pass = SOC_TPLG_PASS_BE_DAI;
2447 		elem_load = soc_tplg_dai_elems_load;
2448 		break;
2449 	case SND_SOC_TPLG_TYPE_DAI_LINK:
2450 	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2451 		/* physical link configurations */
2452 		hdr_pass = SOC_TPLG_PASS_LINK;
2453 		elem_load = soc_tplg_link_elems_load;
2454 		break;
2455 	case SND_SOC_TPLG_TYPE_MANIFEST:
2456 		hdr_pass = SOC_TPLG_PASS_MANIFEST;
2457 		elem_load = soc_tplg_manifest_load;
2458 		break;
2459 	default:
2460 		/* bespoke vendor data object */
2461 		hdr_pass = SOC_TPLG_PASS_VENDOR;
2462 		elem_load = soc_tplg_vendor_load;
2463 		break;
2464 	}
2465 
2466 	if (tplg->pass == hdr_pass) {
2467 		dev_dbg(tplg->dev,
2468 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2469 			hdr->payload_size, hdr->type, hdr->version,
2470 			hdr->vendor_type, tplg->pass);
2471 		return elem_load(tplg, hdr);
2472 	}
2473 
2474 	return 0;
2475 }
2476 
2477 /* process the topology file headers */
2478 static int soc_tplg_process_headers(struct soc_tplg *tplg)
2479 {
2480 	int ret;
2481 
2482 	/* process the header types from start to end */
2483 	for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) {
2484 		struct snd_soc_tplg_hdr *hdr;
2485 
2486 		tplg->hdr_pos = tplg->fw->data;
2487 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2488 
2489 		while (!soc_tplg_is_eof(tplg)) {
2490 
2491 			/* make sure header is valid before loading */
2492 			ret = soc_tplg_valid_header(tplg, hdr);
2493 			if (ret < 0)
2494 				return ret;
2495 
2496 			/* load the header object */
2497 			ret = soc_tplg_load_header(tplg, hdr);
2498 			if (ret < 0) {
2499 				if (ret != -EPROBE_DEFER) {
2500 					dev_err(tplg->dev,
2501 						"ASoC: topology: could not load header: %d\n",
2502 						ret);
2503 				}
2504 				return ret;
2505 			}
2506 
2507 			/* goto next header */
2508 			tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
2509 				sizeof(struct snd_soc_tplg_hdr);
2510 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2511 		}
2512 
2513 	}
2514 
2515 	/* signal DAPM we are complete */
2516 	ret = soc_tplg_dapm_complete(tplg);
2517 
2518 	return ret;
2519 }
2520 
2521 static int soc_tplg_load(struct soc_tplg *tplg)
2522 {
2523 	int ret;
2524 
2525 	ret = soc_tplg_process_headers(tplg);
2526 	if (ret == 0)
2527 		return soc_tplg_complete(tplg);
2528 
2529 	return ret;
2530 }
2531 
2532 /* load audio component topology from "firmware" file */
2533 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2534 	struct snd_soc_tplg_ops *ops, const struct firmware *fw)
2535 {
2536 	struct soc_tplg tplg;
2537 	int ret;
2538 
2539 	/*
2540 	 * check if we have sane parameters:
2541 	 * comp - needs to exist to keep and reference data while parsing
2542 	 * comp->card - used for setting card related parameters
2543 	 * comp->card->dev - used for resource management and prints
2544 	 * fw - we need it, as it is the very thing we parse
2545 	 */
2546 	if (!comp || !comp->card || !comp->card->dev || !fw)
2547 		return -EINVAL;
2548 
2549 	/* setup parsing context */
2550 	memset(&tplg, 0, sizeof(tplg));
2551 	tplg.fw = fw;
2552 	tplg.dev = comp->card->dev;
2553 	tplg.comp = comp;
2554 	if (ops) {
2555 		tplg.ops = ops;
2556 		tplg.io_ops = ops->io_ops;
2557 		tplg.io_ops_count = ops->io_ops_count;
2558 		tplg.bytes_ext_ops = ops->bytes_ext_ops;
2559 		tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2560 	}
2561 
2562 	ret = soc_tplg_load(&tplg);
2563 	/* free the created components if fail to load topology */
2564 	if (ret)
2565 		snd_soc_tplg_component_remove(comp);
2566 
2567 	return ret;
2568 }
2569 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2570 
2571 /* remove dynamic controls from the component driver */
2572 int snd_soc_tplg_component_remove(struct snd_soc_component *comp)
2573 {
2574 	struct snd_soc_dobj *dobj, *next_dobj;
2575 	int pass;
2576 
2577 	/* process the header types from end to start */
2578 	for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) {
2579 
2580 		/* remove mixer controls */
2581 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2582 			list) {
2583 
2584 			switch (dobj->type) {
2585 			case SND_SOC_DOBJ_BYTES:
2586 			case SND_SOC_DOBJ_ENUM:
2587 			case SND_SOC_DOBJ_MIXER:
2588 				soc_tplg_remove_kcontrol(comp, dobj, pass);
2589 				break;
2590 			case SND_SOC_DOBJ_GRAPH:
2591 				soc_tplg_remove_route(comp, dobj, pass);
2592 				break;
2593 			case SND_SOC_DOBJ_WIDGET:
2594 				soc_tplg_remove_widget(comp, dobj, pass);
2595 				break;
2596 			case SND_SOC_DOBJ_PCM:
2597 				soc_tplg_remove_dai(comp, dobj, pass);
2598 				break;
2599 			case SND_SOC_DOBJ_DAI_LINK:
2600 				soc_tplg_remove_link(comp, dobj, pass);
2601 				break;
2602 			case SND_SOC_DOBJ_BACKEND_LINK:
2603 				/*
2604 				 * call link_unload ops if extra
2605 				 * deinitialization is needed.
2606 				 */
2607 				remove_backend_link(comp, dobj, pass);
2608 				break;
2609 			default:
2610 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2611 					dobj->type);
2612 				break;
2613 			}
2614 		}
2615 	}
2616 
2617 	/* let caller know if FW can be freed when no objects are left */
2618 	return !list_empty(&comp->dobj_list);
2619 }
2620 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2621