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