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