xref: /openbmc/linux/sound/soc/soc-topology.c (revision 293d5b43)
1 /*
2  * soc-topology.c  --  ALSA SoC Topology
3  *
4  * Copyright (C) 2012 Texas Instruments Inc.
5  * Copyright (C) 2015 Intel Corporation.
6  *
7  * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
8  *		K, Mythri P <mythri.p.k@intel.com>
9  *		Prusty, Subhransu S <subhransu.s.prusty@intel.com>
10  *		B, Jayachandran <jayachandran.b@intel.com>
11  *		Abdullah, Omair M <omair.m.abdullah@intel.com>
12  *		Jin, Yao <yao.jin@intel.com>
13  *		Lin, Mengdong <mengdong.lin@intel.com>
14  *
15  *  This program is free software; you can redistribute  it and/or modify it
16  *  under  the terms of  the GNU General  Public License as published by the
17  *  Free Software Foundation;  either version 2 of the  License, or (at your
18  *  option) any later version.
19  *
20  *  Add support to read audio firmware topology alongside firmware text. The
21  *  topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
22  *  equalizers, firmware, coefficients etc.
23  *
24  *  This file only manages the core ALSA and ASoC components, all other bespoke
25  *  firmware topology data is passed to component drivers for bespoke handling.
26  */
27 
28 #include <linux/kernel.h>
29 #include <linux/export.h>
30 #include <linux/list.h>
31 #include <linux/firmware.h>
32 #include <linux/slab.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/soc-topology.h>
36 #include <sound/tlv.h>
37 
38 /*
39  * We make several passes over the data (since it wont necessarily be ordered)
40  * and process objects in the following order. This guarantees the component
41  * drivers will be ready with any vendor data before the mixers and DAPM objects
42  * are loaded (that may make use of the vendor data).
43  */
44 #define SOC_TPLG_PASS_MANIFEST		0
45 #define SOC_TPLG_PASS_VENDOR		1
46 #define SOC_TPLG_PASS_MIXER		2
47 #define SOC_TPLG_PASS_WIDGET		3
48 #define SOC_TPLG_PASS_PCM_DAI		4
49 #define SOC_TPLG_PASS_GRAPH		5
50 #define SOC_TPLG_PASS_PINS		6
51 
52 #define SOC_TPLG_PASS_START	SOC_TPLG_PASS_MANIFEST
53 #define SOC_TPLG_PASS_END	SOC_TPLG_PASS_PINS
54 
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 };
187 
188 static int tplc_chan_get_reg(struct soc_tplg *tplg,
189 	struct snd_soc_tplg_channel *chan, int map)
190 {
191 	int i;
192 
193 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
194 		if (chan[i].id == map)
195 			return chan[i].reg;
196 	}
197 
198 	return -EINVAL;
199 }
200 
201 static int tplc_chan_get_shift(struct soc_tplg *tplg,
202 	struct snd_soc_tplg_channel *chan, int map)
203 {
204 	int i;
205 
206 	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
207 		if (chan[i].id == map)
208 			return chan[i].shift;
209 	}
210 
211 	return -EINVAL;
212 }
213 
214 static int get_widget_id(int tplg_type)
215 {
216 	int i;
217 
218 	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
219 		if (tplg_type == dapm_map[i].uid)
220 			return dapm_map[i].kid;
221 	}
222 
223 	return -EINVAL;
224 }
225 
226 static inline void soc_bind_err(struct soc_tplg *tplg,
227 	struct snd_soc_tplg_ctl_hdr *hdr, int index)
228 {
229 	dev_err(tplg->dev,
230 		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
231 		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
232 		soc_tplg_get_offset(tplg));
233 }
234 
235 static inline void soc_control_err(struct soc_tplg *tplg,
236 	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
237 {
238 	dev_err(tplg->dev,
239 		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
240 		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
241 		soc_tplg_get_offset(tplg));
242 }
243 
244 /* pass vendor data to component driver for processing */
245 static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
246 	struct snd_soc_tplg_hdr *hdr)
247 {
248 	int ret = 0;
249 
250 	if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
251 		ret = tplg->ops->vendor_load(tplg->comp, hdr);
252 	else {
253 		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
254 			hdr->vendor_type);
255 		return -EINVAL;
256 	}
257 
258 	if (ret < 0)
259 		dev_err(tplg->dev,
260 			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
261 			soc_tplg_get_hdr_offset(tplg),
262 			soc_tplg_get_hdr_offset(tplg),
263 			hdr->type, hdr->vendor_type);
264 	return ret;
265 }
266 
267 /* pass vendor data to component driver for processing */
268 static int soc_tplg_vendor_load(struct soc_tplg *tplg,
269 	struct snd_soc_tplg_hdr *hdr)
270 {
271 	if (tplg->pass != SOC_TPLG_PASS_VENDOR)
272 		return 0;
273 
274 	return soc_tplg_vendor_load_(tplg, hdr);
275 }
276 
277 /* optionally pass new dynamic widget to component driver. This is mainly for
278  * external widgets where we can assign private data/ops */
279 static int soc_tplg_widget_load(struct soc_tplg *tplg,
280 	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
281 {
282 	if (tplg->comp && tplg->ops && tplg->ops->widget_load)
283 		return tplg->ops->widget_load(tplg->comp, w, tplg_w);
284 
285 	return 0;
286 }
287 
288 /* pass DAI configurations to component driver for extra intialization */
289 static int soc_tplg_dai_load(struct soc_tplg *tplg,
290 	struct snd_soc_dai_driver *dai_drv)
291 {
292 	if (tplg->comp && tplg->ops && tplg->ops->dai_load)
293 		return tplg->ops->dai_load(tplg->comp, dai_drv);
294 
295 	return 0;
296 }
297 
298 /* pass link configurations to component driver for extra intialization */
299 static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
300 	struct snd_soc_dai_link *link)
301 {
302 	if (tplg->comp && tplg->ops && tplg->ops->link_load)
303 		return tplg->ops->link_load(tplg->comp, link);
304 
305 	return 0;
306 }
307 
308 /* tell the component driver that all firmware has been loaded in this request */
309 static void soc_tplg_complete(struct soc_tplg *tplg)
310 {
311 	if (tplg->comp && tplg->ops && tplg->ops->complete)
312 		tplg->ops->complete(tplg->comp);
313 }
314 
315 /* add a dynamic kcontrol */
316 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
317 	const struct snd_kcontrol_new *control_new, const char *prefix,
318 	void *data, struct snd_kcontrol **kcontrol)
319 {
320 	int err;
321 
322 	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
323 	if (*kcontrol == NULL) {
324 		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
325 		control_new->name);
326 		return -ENOMEM;
327 	}
328 
329 	err = snd_ctl_add(card, *kcontrol);
330 	if (err < 0) {
331 		dev_err(dev, "ASoC: Failed to add %s: %d\n",
332 			control_new->name, err);
333 		return err;
334 	}
335 
336 	return 0;
337 }
338 
339 /* add a dynamic kcontrol for component driver */
340 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
341 	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
342 {
343 	struct snd_soc_component *comp = tplg->comp;
344 
345 	return soc_tplg_add_dcontrol(comp->card->snd_card,
346 				comp->dev, k, NULL, comp, kcontrol);
347 }
348 
349 /* remove a mixer kcontrol */
350 static void remove_mixer(struct snd_soc_component *comp,
351 	struct snd_soc_dobj *dobj, int pass)
352 {
353 	struct snd_card *card = comp->card->snd_card;
354 	struct soc_mixer_control *sm =
355 		container_of(dobj, struct soc_mixer_control, dobj);
356 	const unsigned int *p = NULL;
357 
358 	if (pass != SOC_TPLG_PASS_MIXER)
359 		return;
360 
361 	if (dobj->ops && dobj->ops->control_unload)
362 		dobj->ops->control_unload(comp, dobj);
363 
364 	if (sm->dobj.control.kcontrol->tlv.p)
365 		p = sm->dobj.control.kcontrol->tlv.p;
366 	snd_ctl_remove(card, sm->dobj.control.kcontrol);
367 	list_del(&sm->dobj.list);
368 	kfree(sm);
369 	kfree(p);
370 }
371 
372 /* remove an enum kcontrol */
373 static void remove_enum(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_enum *se = container_of(dobj, struct soc_enum, dobj);
378 	int i;
379 
380 	if (pass != SOC_TPLG_PASS_MIXER)
381 		return;
382 
383 	if (dobj->ops && dobj->ops->control_unload)
384 		dobj->ops->control_unload(comp, dobj);
385 
386 	snd_ctl_remove(card, se->dobj.control.kcontrol);
387 	list_del(&se->dobj.list);
388 
389 	kfree(se->dobj.control.dvalues);
390 	for (i = 0; i < se->items; i++)
391 		kfree(se->dobj.control.dtexts[i]);
392 	kfree(se);
393 }
394 
395 /* remove a byte kcontrol */
396 static void remove_bytes(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_bytes_ext *sb =
401 		container_of(dobj, struct soc_bytes_ext, dobj);
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, sb->dobj.control.kcontrol);
410 	list_del(&sb->dobj.list);
411 	kfree(sb);
412 }
413 
414 /* remove a widget and it's kcontrols - routes must be removed first */
415 static void remove_widget(struct snd_soc_component *comp,
416 	struct snd_soc_dobj *dobj, int pass)
417 {
418 	struct snd_card *card = comp->card->snd_card;
419 	struct snd_soc_dapm_widget *w =
420 		container_of(dobj, struct snd_soc_dapm_widget, dobj);
421 	int i;
422 
423 	if (pass != SOC_TPLG_PASS_WIDGET)
424 		return;
425 
426 	if (dobj->ops && dobj->ops->widget_unload)
427 		dobj->ops->widget_unload(comp, dobj);
428 
429 	/*
430 	 * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers.
431 	 * The enum may either have an array of values or strings.
432 	 */
433 	if (dobj->widget.kcontrol_enum) {
434 		/* enumerated widget mixer */
435 		struct soc_enum *se =
436 			(struct soc_enum *)w->kcontrols[0]->private_value;
437 
438 		snd_ctl_remove(card, w->kcontrols[0]);
439 
440 		kfree(se->dobj.control.dvalues);
441 		for (i = 0; i < se->items; i++)
442 			kfree(se->dobj.control.dtexts[i]);
443 
444 		kfree(se);
445 		kfree(w->kcontrol_news);
446 	} else {
447 		/* non enumerated widget mixer */
448 		for (i = 0; i < w->num_kcontrols; i++) {
449 			struct snd_kcontrol *kcontrol = w->kcontrols[i];
450 			struct soc_mixer_control *sm =
451 			(struct soc_mixer_control *) kcontrol->private_value;
452 
453 			kfree(w->kcontrols[i]->tlv.p);
454 
455 			snd_ctl_remove(card, w->kcontrols[i]);
456 			kfree(sm);
457 		}
458 		kfree(w->kcontrol_news);
459 	}
460 	/* widget w is freed by soc-dapm.c */
461 }
462 
463 /* remove DAI configurations */
464 static void remove_dai(struct snd_soc_component *comp,
465 	struct snd_soc_dobj *dobj, int pass)
466 {
467 	struct snd_soc_dai_driver *dai_drv =
468 		container_of(dobj, struct snd_soc_dai_driver, dobj);
469 
470 	if (pass != SOC_TPLG_PASS_PCM_DAI)
471 		return;
472 
473 	if (dobj->ops && dobj->ops->dai_unload)
474 		dobj->ops->dai_unload(comp, dobj);
475 
476 	list_del(&dobj->list);
477 	kfree(dai_drv);
478 }
479 
480 /* remove link configurations */
481 static void remove_link(struct snd_soc_component *comp,
482 	struct snd_soc_dobj *dobj, int pass)
483 {
484 	struct snd_soc_dai_link *link =
485 		container_of(dobj, struct snd_soc_dai_link, dobj);
486 
487 	if (pass != SOC_TPLG_PASS_PCM_DAI)
488 		return;
489 
490 	if (dobj->ops && dobj->ops->link_unload)
491 		dobj->ops->link_unload(comp, dobj);
492 
493 	list_del(&dobj->list);
494 	snd_soc_remove_dai_link(comp->card, link);
495 	kfree(link);
496 }
497 
498 /* bind a kcontrol to it's IO handlers */
499 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
500 	struct snd_kcontrol_new *k,
501 	const struct soc_tplg *tplg)
502 {
503 	const struct snd_soc_tplg_kcontrol_ops *ops;
504 	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
505 	int num_ops, i;
506 
507 	if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
508 		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
509 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
510 		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
511 		struct soc_bytes_ext *sbe;
512 		struct snd_soc_tplg_bytes_control *be;
513 
514 		sbe = (struct soc_bytes_ext *)k->private_value;
515 		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
516 
517 		/* TLV bytes controls need standard kcontrol info handler,
518 		 * TLV callback and extended put/get handlers.
519 		 */
520 		k->info = snd_soc_bytes_info_ext;
521 		k->tlv.c = snd_soc_bytes_tlv_callback;
522 
523 		ext_ops = tplg->bytes_ext_ops;
524 		num_ops = tplg->bytes_ext_ops_count;
525 		for (i = 0; i < num_ops; i++) {
526 			if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
527 				sbe->put = ext_ops[i].put;
528 			if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
529 				sbe->get = ext_ops[i].get;
530 		}
531 
532 		if (sbe->put && sbe->get)
533 			return 0;
534 		else
535 			return -EINVAL;
536 	}
537 
538 	/* try and map vendor specific kcontrol handlers first */
539 	ops = tplg->io_ops;
540 	num_ops = tplg->io_ops_count;
541 	for (i = 0; i < num_ops; i++) {
542 
543 		if (k->put == NULL && ops[i].id == hdr->ops.put)
544 			k->put = ops[i].put;
545 		if (k->get == NULL && ops[i].id == hdr->ops.get)
546 			k->get = ops[i].get;
547 		if (k->info == NULL && ops[i].id == hdr->ops.info)
548 			k->info = ops[i].info;
549 	}
550 
551 	/* vendor specific handlers found ? */
552 	if (k->put && k->get && k->info)
553 		return 0;
554 
555 	/* none found so try standard kcontrol handlers */
556 	ops = io_ops;
557 	num_ops = ARRAY_SIZE(io_ops);
558 	for (i = 0; i < num_ops; i++) {
559 
560 		if (k->put == NULL && ops[i].id == hdr->ops.put)
561 			k->put = ops[i].put;
562 		if (k->get == NULL && ops[i].id == hdr->ops.get)
563 			k->get = ops[i].get;
564 		if (k->info == NULL && ops[i].id == hdr->ops.info)
565 			k->info = ops[i].info;
566 	}
567 
568 	/* standard handlers found ? */
569 	if (k->put && k->get && k->info)
570 		return 0;
571 
572 	/* nothing to bind */
573 	return -EINVAL;
574 }
575 
576 /* bind a widgets to it's evnt handlers */
577 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
578 		const struct snd_soc_tplg_widget_events *events,
579 		int num_events, u16 event_type)
580 {
581 	int i;
582 
583 	w->event = NULL;
584 
585 	for (i = 0; i < num_events; i++) {
586 		if (event_type == events[i].type) {
587 
588 			/* found - so assign event */
589 			w->event = events[i].event_handler;
590 			return 0;
591 		}
592 	}
593 
594 	/* not found */
595 	return -EINVAL;
596 }
597 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
598 
599 /* optionally pass new dynamic kcontrol to component driver. */
600 static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
601 	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
602 {
603 	if (tplg->comp && tplg->ops && tplg->ops->control_load)
604 		return tplg->ops->control_load(tplg->comp, k, hdr);
605 
606 	return 0;
607 }
608 
609 
610 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
611 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
612 {
613 	unsigned int item_len = 2 * sizeof(unsigned int);
614 	unsigned int *p;
615 
616 	p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
617 	if (!p)
618 		return -ENOMEM;
619 
620 	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
621 	p[1] = item_len;
622 	p[2] = scale->min;
623 	p[3] = (scale->step & TLV_DB_SCALE_MASK)
624 			| (scale->mute ? TLV_DB_SCALE_MUTE : 0);
625 
626 	kc->tlv.p = (void *)p;
627 	return 0;
628 }
629 
630 static int soc_tplg_create_tlv(struct soc_tplg *tplg,
631 	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
632 {
633 	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
634 
635 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
636 		return 0;
637 
638 	if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
639 		tplg_tlv = &tc->tlv;
640 		switch (tplg_tlv->type) {
641 		case SNDRV_CTL_TLVT_DB_SCALE:
642 			return soc_tplg_create_tlv_db_scale(tplg, kc,
643 					&tplg_tlv->scale);
644 
645 		/* TODO: add support for other TLV types */
646 		default:
647 			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
648 					tplg_tlv->type);
649 			return -EINVAL;
650 		}
651 	}
652 
653 	return 0;
654 }
655 
656 static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
657 	struct snd_kcontrol_new *kc)
658 {
659 	kfree(kc->tlv.p);
660 }
661 
662 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
663 	size_t size)
664 {
665 	struct snd_soc_tplg_bytes_control *be;
666 	struct soc_bytes_ext *sbe;
667 	struct snd_kcontrol_new kc;
668 	int i, err;
669 
670 	if (soc_tplg_check_elem_count(tplg,
671 		sizeof(struct snd_soc_tplg_bytes_control), count,
672 			size, "mixer bytes")) {
673 		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
674 			count);
675 		return -EINVAL;
676 	}
677 
678 	for (i = 0; i < count; i++) {
679 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
680 
681 		/* validate kcontrol */
682 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
683 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
684 			return -EINVAL;
685 
686 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
687 		if (sbe == NULL)
688 			return -ENOMEM;
689 
690 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
691 			be->priv.size);
692 
693 		dev_dbg(tplg->dev,
694 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
695 			be->hdr.name, be->hdr.access);
696 
697 		memset(&kc, 0, sizeof(kc));
698 		kc.name = be->hdr.name;
699 		kc.private_value = (long)sbe;
700 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
701 		kc.access = be->hdr.access;
702 
703 		sbe->max = be->max;
704 		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
705 		sbe->dobj.ops = tplg->ops;
706 		INIT_LIST_HEAD(&sbe->dobj.list);
707 
708 		/* map io handlers */
709 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
710 		if (err) {
711 			soc_control_err(tplg, &be->hdr, be->hdr.name);
712 			kfree(sbe);
713 			continue;
714 		}
715 
716 		/* pass control to driver for optional further init */
717 		err = soc_tplg_init_kcontrol(tplg, &kc,
718 			(struct snd_soc_tplg_ctl_hdr *)be);
719 		if (err < 0) {
720 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
721 				be->hdr.name);
722 			kfree(sbe);
723 			continue;
724 		}
725 
726 		/* register control here */
727 		err = soc_tplg_add_kcontrol(tplg, &kc,
728 			&sbe->dobj.control.kcontrol);
729 		if (err < 0) {
730 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
731 				be->hdr.name);
732 			kfree(sbe);
733 			continue;
734 		}
735 
736 		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
737 	}
738 	return 0;
739 
740 }
741 
742 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
743 	size_t size)
744 {
745 	struct snd_soc_tplg_mixer_control *mc;
746 	struct soc_mixer_control *sm;
747 	struct snd_kcontrol_new kc;
748 	int i, err;
749 
750 	if (soc_tplg_check_elem_count(tplg,
751 		sizeof(struct snd_soc_tplg_mixer_control),
752 		count, size, "mixers")) {
753 
754 		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
755 			count);
756 		return -EINVAL;
757 	}
758 
759 	for (i = 0; i < count; i++) {
760 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
761 
762 		/* validate kcontrol */
763 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
764 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
765 			return -EINVAL;
766 
767 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
768 		if (sm == NULL)
769 			return -ENOMEM;
770 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
771 			mc->priv.size);
772 
773 		dev_dbg(tplg->dev,
774 			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
775 			mc->hdr.name, mc->hdr.access);
776 
777 		memset(&kc, 0, sizeof(kc));
778 		kc.name = mc->hdr.name;
779 		kc.private_value = (long)sm;
780 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
781 		kc.access = mc->hdr.access;
782 
783 		/* we only support FL/FR channel mapping atm */
784 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
785 			SNDRV_CHMAP_FL);
786 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
787 			SNDRV_CHMAP_FR);
788 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
789 			SNDRV_CHMAP_FL);
790 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
791 			SNDRV_CHMAP_FR);
792 
793 		sm->max = mc->max;
794 		sm->min = mc->min;
795 		sm->invert = mc->invert;
796 		sm->platform_max = mc->platform_max;
797 		sm->dobj.index = tplg->index;
798 		sm->dobj.ops = tplg->ops;
799 		sm->dobj.type = SND_SOC_DOBJ_MIXER;
800 		INIT_LIST_HEAD(&sm->dobj.list);
801 
802 		/* map io handlers */
803 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
804 		if (err) {
805 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
806 			kfree(sm);
807 			continue;
808 		}
809 
810 		/* pass control to driver for optional further init */
811 		err = soc_tplg_init_kcontrol(tplg, &kc,
812 			(struct snd_soc_tplg_ctl_hdr *) mc);
813 		if (err < 0) {
814 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
815 				mc->hdr.name);
816 			kfree(sm);
817 			continue;
818 		}
819 
820 		/* create any TLV data */
821 		soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
822 
823 		/* register control here */
824 		err = soc_tplg_add_kcontrol(tplg, &kc,
825 			&sm->dobj.control.kcontrol);
826 		if (err < 0) {
827 			dev_err(tplg->dev, "ASoC: failed to add %s\n",
828 				mc->hdr.name);
829 			soc_tplg_free_tlv(tplg, &kc);
830 			kfree(sm);
831 			continue;
832 		}
833 
834 		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
835 	}
836 
837 	return 0;
838 }
839 
840 static int soc_tplg_denum_create_texts(struct soc_enum *se,
841 	struct snd_soc_tplg_enum_control *ec)
842 {
843 	int i, ret;
844 
845 	se->dobj.control.dtexts =
846 		kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
847 	if (se->dobj.control.dtexts == NULL)
848 		return -ENOMEM;
849 
850 	for (i = 0; i < ec->items; i++) {
851 
852 		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
853 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
854 			ret = -EINVAL;
855 			goto err;
856 		}
857 
858 		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
859 		if (!se->dobj.control.dtexts[i]) {
860 			ret = -ENOMEM;
861 			goto err;
862 		}
863 	}
864 
865 	return 0;
866 
867 err:
868 	for (--i; i >= 0; i--)
869 		kfree(se->dobj.control.dtexts[i]);
870 	kfree(se->dobj.control.dtexts);
871 	return ret;
872 }
873 
874 static int soc_tplg_denum_create_values(struct soc_enum *se,
875 	struct snd_soc_tplg_enum_control *ec)
876 {
877 	if (ec->items > sizeof(*ec->values))
878 		return -EINVAL;
879 
880 	se->dobj.control.dvalues = kmemdup(ec->values,
881 					   ec->items * sizeof(u32),
882 					   GFP_KERNEL);
883 	if (!se->dobj.control.dvalues)
884 		return -ENOMEM;
885 
886 	return 0;
887 }
888 
889 static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
890 	size_t size)
891 {
892 	struct snd_soc_tplg_enum_control *ec;
893 	struct soc_enum *se;
894 	struct snd_kcontrol_new kc;
895 	int i, ret, err;
896 
897 	if (soc_tplg_check_elem_count(tplg,
898 		sizeof(struct snd_soc_tplg_enum_control),
899 		count, size, "enums")) {
900 
901 		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
902 			count);
903 		return -EINVAL;
904 	}
905 
906 	for (i = 0; i < count; i++) {
907 		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
908 		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
909 			ec->priv.size);
910 
911 		/* validate kcontrol */
912 		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
913 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
914 			return -EINVAL;
915 
916 		se = kzalloc((sizeof(*se)), GFP_KERNEL);
917 		if (se == NULL)
918 			return -ENOMEM;
919 
920 		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
921 			ec->hdr.name, ec->items);
922 
923 		memset(&kc, 0, sizeof(kc));
924 		kc.name = ec->hdr.name;
925 		kc.private_value = (long)se;
926 		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
927 		kc.access = ec->hdr.access;
928 
929 		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
930 		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
931 			SNDRV_CHMAP_FL);
932 		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
933 			SNDRV_CHMAP_FL);
934 
935 		se->items = ec->items;
936 		se->mask = ec->mask;
937 		se->dobj.index = tplg->index;
938 		se->dobj.type = SND_SOC_DOBJ_ENUM;
939 		se->dobj.ops = tplg->ops;
940 		INIT_LIST_HEAD(&se->dobj.list);
941 
942 		switch (ec->hdr.ops.info) {
943 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
944 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
945 			err = soc_tplg_denum_create_values(se, ec);
946 			if (err < 0) {
947 				dev_err(tplg->dev,
948 					"ASoC: could not create values for %s\n",
949 					ec->hdr.name);
950 				kfree(se);
951 				continue;
952 			}
953 			/* fall through and create texts */
954 		case SND_SOC_TPLG_CTL_ENUM:
955 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
956 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
957 			err = soc_tplg_denum_create_texts(se, ec);
958 			if (err < 0) {
959 				dev_err(tplg->dev,
960 					"ASoC: could not create texts for %s\n",
961 					ec->hdr.name);
962 				kfree(se);
963 				continue;
964 			}
965 			break;
966 		default:
967 			dev_err(tplg->dev,
968 				"ASoC: invalid enum control type %d for %s\n",
969 				ec->hdr.ops.info, ec->hdr.name);
970 			kfree(se);
971 			continue;
972 		}
973 
974 		/* map io handlers */
975 		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
976 		if (err) {
977 			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
978 			kfree(se);
979 			continue;
980 		}
981 
982 		/* pass control to driver for optional further init */
983 		err = soc_tplg_init_kcontrol(tplg, &kc,
984 			(struct snd_soc_tplg_ctl_hdr *) ec);
985 		if (err < 0) {
986 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
987 				ec->hdr.name);
988 			kfree(se);
989 			continue;
990 		}
991 
992 		/* register control here */
993 		ret = soc_tplg_add_kcontrol(tplg,
994 			&kc, &se->dobj.control.kcontrol);
995 		if (ret < 0) {
996 			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
997 				ec->hdr.name);
998 			kfree(se);
999 			continue;
1000 		}
1001 
1002 		list_add(&se->dobj.list, &tplg->comp->dobj_list);
1003 	}
1004 
1005 	return 0;
1006 }
1007 
1008 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1009 	struct snd_soc_tplg_hdr *hdr)
1010 {
1011 	struct snd_soc_tplg_ctl_hdr *control_hdr;
1012 	int i;
1013 
1014 	if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1015 		tplg->pos += hdr->size + hdr->payload_size;
1016 		return 0;
1017 	}
1018 
1019 	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1020 		soc_tplg_get_offset(tplg));
1021 
1022 	for (i = 0; i < hdr->count; i++) {
1023 
1024 		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1025 
1026 		if (control_hdr->size != sizeof(*control_hdr)) {
1027 			dev_err(tplg->dev, "ASoC: invalid control size\n");
1028 			return -EINVAL;
1029 		}
1030 
1031 		switch (control_hdr->ops.info) {
1032 		case SND_SOC_TPLG_CTL_VOLSW:
1033 		case SND_SOC_TPLG_CTL_STROBE:
1034 		case SND_SOC_TPLG_CTL_VOLSW_SX:
1035 		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1036 		case SND_SOC_TPLG_CTL_RANGE:
1037 		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1038 		case SND_SOC_TPLG_DAPM_CTL_PIN:
1039 			soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1040 			break;
1041 		case SND_SOC_TPLG_CTL_ENUM:
1042 		case SND_SOC_TPLG_CTL_ENUM_VALUE:
1043 		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1044 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1045 		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1046 			soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1047 			break;
1048 		case SND_SOC_TPLG_CTL_BYTES:
1049 			soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1050 			break;
1051 		default:
1052 			soc_bind_err(tplg, control_hdr, i);
1053 			return -EINVAL;
1054 		}
1055 	}
1056 
1057 	return 0;
1058 }
1059 
1060 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1061 	struct snd_soc_tplg_hdr *hdr)
1062 {
1063 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1064 	struct snd_soc_dapm_route route;
1065 	struct snd_soc_tplg_dapm_graph_elem *elem;
1066 	int count = hdr->count, i;
1067 
1068 	if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1069 		tplg->pos += hdr->size + hdr->payload_size;
1070 		return 0;
1071 	}
1072 
1073 	if (soc_tplg_check_elem_count(tplg,
1074 		sizeof(struct snd_soc_tplg_dapm_graph_elem),
1075 		count, hdr->payload_size, "graph")) {
1076 
1077 		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1078 			count);
1079 		return -EINVAL;
1080 	}
1081 
1082 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1083 
1084 	for (i = 0; i < count; i++) {
1085 		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1086 		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1087 
1088 		/* validate routes */
1089 		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1090 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1091 			return -EINVAL;
1092 		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1093 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1094 			return -EINVAL;
1095 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1096 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1097 			return -EINVAL;
1098 
1099 		route.source = elem->source;
1100 		route.sink = elem->sink;
1101 		route.connected = NULL; /* set to NULL atm for tplg users */
1102 		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1103 			route.control = NULL;
1104 		else
1105 			route.control = elem->control;
1106 
1107 		/* add route, but keep going if some fail */
1108 		snd_soc_dapm_add_routes(dapm, &route, 1);
1109 	}
1110 
1111 	return 0;
1112 }
1113 
1114 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1115 	struct soc_tplg *tplg, int num_kcontrols)
1116 {
1117 	struct snd_kcontrol_new *kc;
1118 	struct soc_mixer_control *sm;
1119 	struct snd_soc_tplg_mixer_control *mc;
1120 	int i, err;
1121 
1122 	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1123 	if (kc == NULL)
1124 		return NULL;
1125 
1126 	for (i = 0; i < num_kcontrols; i++) {
1127 		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1128 		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1129 		if (sm == NULL)
1130 			goto err;
1131 
1132 		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1133 			mc->priv.size);
1134 
1135 		/* validate kcontrol */
1136 		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1137 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1138 			goto err_str;
1139 
1140 		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1141 			mc->hdr.name, i);
1142 
1143 		kc[i].name = mc->hdr.name;
1144 		kc[i].private_value = (long)sm;
1145 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1146 		kc[i].access = mc->hdr.access;
1147 
1148 		/* we only support FL/FR channel mapping atm */
1149 		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1150 			SNDRV_CHMAP_FL);
1151 		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1152 			SNDRV_CHMAP_FR);
1153 		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1154 			SNDRV_CHMAP_FL);
1155 		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1156 			SNDRV_CHMAP_FR);
1157 
1158 		sm->max = mc->max;
1159 		sm->min = mc->min;
1160 		sm->invert = mc->invert;
1161 		sm->platform_max = mc->platform_max;
1162 		sm->dobj.index = tplg->index;
1163 		INIT_LIST_HEAD(&sm->dobj.list);
1164 
1165 		/* map io handlers */
1166 		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1167 		if (err) {
1168 			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1169 			kfree(sm);
1170 			continue;
1171 		}
1172 
1173 		/* pass control to driver for optional further init */
1174 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
1175 			(struct snd_soc_tplg_ctl_hdr *)mc);
1176 		if (err < 0) {
1177 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
1178 				mc->hdr.name);
1179 			kfree(sm);
1180 			continue;
1181 		}
1182 	}
1183 	return kc;
1184 
1185 err_str:
1186 	kfree(sm);
1187 err:
1188 	for (--i; i >= 0; i--)
1189 		kfree((void *)kc[i].private_value);
1190 	kfree(kc);
1191 	return NULL;
1192 }
1193 
1194 static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1195 	struct soc_tplg *tplg)
1196 {
1197 	struct snd_kcontrol_new *kc;
1198 	struct snd_soc_tplg_enum_control *ec;
1199 	struct soc_enum *se;
1200 	int i, err;
1201 
1202 	ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1203 	tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1204 		ec->priv.size);
1205 
1206 	/* validate kcontrol */
1207 	if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1208 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1209 		return NULL;
1210 
1211 	kc = kzalloc(sizeof(*kc), GFP_KERNEL);
1212 	if (kc == NULL)
1213 		return NULL;
1214 
1215 	se = kzalloc(sizeof(*se), GFP_KERNEL);
1216 	if (se == NULL)
1217 		goto err;
1218 
1219 	dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1220 		ec->hdr.name);
1221 
1222 	kc->name = ec->hdr.name;
1223 	kc->private_value = (long)se;
1224 	kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1225 	kc->access = ec->hdr.access;
1226 
1227 	/* we only support FL/FR channel mapping atm */
1228 	se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1229 	se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
1230 	se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
1231 
1232 	se->items = ec->items;
1233 	se->mask = ec->mask;
1234 	se->dobj.index = tplg->index;
1235 
1236 	switch (ec->hdr.ops.info) {
1237 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
1238 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1239 		err = soc_tplg_denum_create_values(se, ec);
1240 		if (err < 0) {
1241 			dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1242 				ec->hdr.name);
1243 			goto err_se;
1244 		}
1245 		/* fall through to create texts */
1246 	case SND_SOC_TPLG_CTL_ENUM:
1247 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1248 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1249 		err = soc_tplg_denum_create_texts(se, ec);
1250 		if (err < 0) {
1251 			dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1252 				ec->hdr.name);
1253 			goto err_se;
1254 		}
1255 		break;
1256 	default:
1257 		dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1258 			ec->hdr.ops.info, ec->hdr.name);
1259 		goto err_se;
1260 	}
1261 
1262 	/* map io handlers */
1263 	err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
1264 	if (err) {
1265 		soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1266 		goto err_se;
1267 	}
1268 
1269 	/* pass control to driver for optional further init */
1270 	err = soc_tplg_init_kcontrol(tplg, kc,
1271 		(struct snd_soc_tplg_ctl_hdr *)ec);
1272 	if (err < 0) {
1273 		dev_err(tplg->dev, "ASoC: failed to init %s\n",
1274 			ec->hdr.name);
1275 		goto err_se;
1276 	}
1277 
1278 	return kc;
1279 
1280 err_se:
1281 	/* free values and texts */
1282 	kfree(se->dobj.control.dvalues);
1283 	for (i = 0; i < ec->items; i++)
1284 		kfree(se->dobj.control.dtexts[i]);
1285 
1286 	kfree(se);
1287 err:
1288 	kfree(kc);
1289 
1290 	return NULL;
1291 }
1292 
1293 static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1294 	struct soc_tplg *tplg, int count)
1295 {
1296 	struct snd_soc_tplg_bytes_control *be;
1297 	struct soc_bytes_ext  *sbe;
1298 	struct snd_kcontrol_new *kc;
1299 	int i, err;
1300 
1301 	kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
1302 	if (!kc)
1303 		return NULL;
1304 
1305 	for (i = 0; i < count; i++) {
1306 		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1307 
1308 		/* validate kcontrol */
1309 		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1310 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1311 			goto err;
1312 
1313 		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1314 		if (sbe == NULL)
1315 			goto err;
1316 
1317 		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1318 			be->priv.size);
1319 
1320 		dev_dbg(tplg->dev,
1321 			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
1322 			be->hdr.name, be->hdr.access);
1323 
1324 		kc[i].name = be->hdr.name;
1325 		kc[i].private_value = (long)sbe;
1326 		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1327 		kc[i].access = be->hdr.access;
1328 
1329 		sbe->max = be->max;
1330 		INIT_LIST_HEAD(&sbe->dobj.list);
1331 
1332 		/* map standard io handlers and check for external handlers */
1333 		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1334 		if (err) {
1335 			soc_control_err(tplg, &be->hdr, be->hdr.name);
1336 			kfree(sbe);
1337 			continue;
1338 		}
1339 
1340 		/* pass control to driver for optional further init */
1341 		err = soc_tplg_init_kcontrol(tplg, &kc[i],
1342 			(struct snd_soc_tplg_ctl_hdr *)be);
1343 		if (err < 0) {
1344 			dev_err(tplg->dev, "ASoC: failed to init %s\n",
1345 				be->hdr.name);
1346 			kfree(sbe);
1347 			continue;
1348 		}
1349 	}
1350 
1351 	return kc;
1352 
1353 err:
1354 	for (--i; i >= 0; i--)
1355 		kfree((void *)kc[i].private_value);
1356 
1357 	kfree(kc);
1358 	return NULL;
1359 }
1360 
1361 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1362 	struct snd_soc_tplg_dapm_widget *w)
1363 {
1364 	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1365 	struct snd_soc_dapm_widget template, *widget;
1366 	struct snd_soc_tplg_ctl_hdr *control_hdr;
1367 	struct snd_soc_card *card = tplg->comp->card;
1368 	int ret = 0;
1369 
1370 	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1371 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1372 		return -EINVAL;
1373 	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1374 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1375 		return -EINVAL;
1376 
1377 	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1378 		w->name, w->id);
1379 
1380 	memset(&template, 0, sizeof(template));
1381 
1382 	/* map user to kernel widget ID */
1383 	template.id = get_widget_id(w->id);
1384 	if (template.id < 0)
1385 		return template.id;
1386 
1387 	template.name = kstrdup(w->name, GFP_KERNEL);
1388 	if (!template.name)
1389 		return -ENOMEM;
1390 	template.sname = kstrdup(w->sname, GFP_KERNEL);
1391 	if (!template.sname) {
1392 		ret = -ENOMEM;
1393 		goto err;
1394 	}
1395 	template.reg = w->reg;
1396 	template.shift = w->shift;
1397 	template.mask = w->mask;
1398 	template.subseq = w->subseq;
1399 	template.on_val = w->invert ? 0 : 1;
1400 	template.off_val = w->invert ? 1 : 0;
1401 	template.ignore_suspend = w->ignore_suspend;
1402 	template.event_flags = w->event_flags;
1403 	template.dobj.index = tplg->index;
1404 
1405 	tplg->pos +=
1406 		(sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1407 	if (w->num_kcontrols == 0) {
1408 		template.num_kcontrols = 0;
1409 		goto widget;
1410 	}
1411 
1412 	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1413 	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1414 		w->name, w->num_kcontrols, control_hdr->type);
1415 
1416 	switch (control_hdr->ops.info) {
1417 	case SND_SOC_TPLG_CTL_VOLSW:
1418 	case SND_SOC_TPLG_CTL_STROBE:
1419 	case SND_SOC_TPLG_CTL_VOLSW_SX:
1420 	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1421 	case SND_SOC_TPLG_CTL_RANGE:
1422 	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1423 		template.num_kcontrols = w->num_kcontrols;
1424 		template.kcontrol_news =
1425 			soc_tplg_dapm_widget_dmixer_create(tplg,
1426 			template.num_kcontrols);
1427 		if (!template.kcontrol_news) {
1428 			ret = -ENOMEM;
1429 			goto hdr_err;
1430 		}
1431 		break;
1432 	case SND_SOC_TPLG_CTL_ENUM:
1433 	case SND_SOC_TPLG_CTL_ENUM_VALUE:
1434 	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1435 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1436 	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1437 		template.dobj.widget.kcontrol_enum = 1;
1438 		template.num_kcontrols = 1;
1439 		template.kcontrol_news =
1440 			soc_tplg_dapm_widget_denum_create(tplg);
1441 		if (!template.kcontrol_news) {
1442 			ret = -ENOMEM;
1443 			goto hdr_err;
1444 		}
1445 		break;
1446 	case SND_SOC_TPLG_CTL_BYTES:
1447 		template.num_kcontrols = w->num_kcontrols;
1448 		template.kcontrol_news =
1449 			soc_tplg_dapm_widget_dbytes_create(tplg,
1450 				template.num_kcontrols);
1451 		if (!template.kcontrol_news) {
1452 			ret = -ENOMEM;
1453 			goto hdr_err;
1454 		}
1455 		break;
1456 	default:
1457 		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1458 			control_hdr->ops.get, control_hdr->ops.put,
1459 			control_hdr->ops.info);
1460 		ret = -EINVAL;
1461 		goto hdr_err;
1462 	}
1463 
1464 widget:
1465 	ret = soc_tplg_widget_load(tplg, &template, w);
1466 	if (ret < 0)
1467 		goto hdr_err;
1468 
1469 	/* card dapm mutex is held by the core if we are loading topology
1470 	 * data during sound card init. */
1471 	if (card->instantiated)
1472 		widget = snd_soc_dapm_new_control(dapm, &template);
1473 	else
1474 		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1475 	if (widget == NULL) {
1476 		dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1477 			w->name);
1478 		goto hdr_err;
1479 	}
1480 
1481 	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1482 	widget->dobj.ops = tplg->ops;
1483 	widget->dobj.index = tplg->index;
1484 	kfree(template.sname);
1485 	kfree(template.name);
1486 	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1487 	return 0;
1488 
1489 hdr_err:
1490 	kfree(template.sname);
1491 err:
1492 	kfree(template.name);
1493 	return ret;
1494 }
1495 
1496 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1497 	struct snd_soc_tplg_hdr *hdr)
1498 {
1499 	struct snd_soc_tplg_dapm_widget *widget;
1500 	int ret, count = hdr->count, i;
1501 
1502 	if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1503 		return 0;
1504 
1505 	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1506 
1507 	for (i = 0; i < count; i++) {
1508 		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1509 		if (widget->size != sizeof(*widget)) {
1510 			dev_err(tplg->dev, "ASoC: invalid widget size\n");
1511 			return -EINVAL;
1512 		}
1513 
1514 		ret = soc_tplg_dapm_widget_create(tplg, widget);
1515 		if (ret < 0) {
1516 			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1517 				widget->name);
1518 			return ret;
1519 		}
1520 	}
1521 
1522 	return 0;
1523 }
1524 
1525 static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1526 {
1527 	struct snd_soc_card *card = tplg->comp->card;
1528 	int ret;
1529 
1530 	/* Card might not have been registered at this point.
1531 	 * If so, just return success.
1532 	*/
1533 	if (!card || !card->instantiated) {
1534 		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1535 				"Do not add new widgets now\n");
1536 		return 0;
1537 	}
1538 
1539 	ret = snd_soc_dapm_new_widgets(card);
1540 	if (ret < 0)
1541 		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1542 			ret);
1543 
1544 	return 0;
1545 }
1546 
1547 static void set_stream_info(struct snd_soc_pcm_stream *stream,
1548 	struct snd_soc_tplg_stream_caps *caps)
1549 {
1550 	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1551 	stream->channels_min = caps->channels_min;
1552 	stream->channels_max = caps->channels_max;
1553 	stream->rates = caps->rates;
1554 	stream->rate_min = caps->rate_min;
1555 	stream->rate_max = caps->rate_max;
1556 	stream->formats = caps->formats;
1557 }
1558 
1559 static int soc_tplg_dai_create(struct soc_tplg *tplg,
1560 	struct snd_soc_tplg_pcm *pcm)
1561 {
1562 	struct snd_soc_dai_driver *dai_drv;
1563 	struct snd_soc_pcm_stream *stream;
1564 	struct snd_soc_tplg_stream_caps *caps;
1565 	int ret;
1566 
1567 	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1568 	if (dai_drv == NULL)
1569 		return -ENOMEM;
1570 
1571 	dai_drv->name = pcm->dai_name;
1572 	dai_drv->id = pcm->dai_id;
1573 
1574 	if (pcm->playback) {
1575 		stream = &dai_drv->playback;
1576 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1577 		set_stream_info(stream, caps);
1578 	}
1579 
1580 	if (pcm->capture) {
1581 		stream = &dai_drv->capture;
1582 		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1583 		set_stream_info(stream, caps);
1584 	}
1585 
1586 	/* pass control to component driver for optional further init */
1587 	ret = soc_tplg_dai_load(tplg, dai_drv);
1588 	if (ret < 0) {
1589 		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1590 		kfree(dai_drv);
1591 		return ret;
1592 	}
1593 
1594 	dai_drv->dobj.index = tplg->index;
1595 	dai_drv->dobj.ops = tplg->ops;
1596 	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1597 	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1598 
1599 	/* register the DAI to the component */
1600 	return snd_soc_register_dai(tplg->comp, dai_drv);
1601 }
1602 
1603 /* create the FE DAI link */
1604 static int soc_tplg_link_create(struct soc_tplg *tplg,
1605 	struct snd_soc_tplg_pcm *pcm)
1606 {
1607 	struct snd_soc_dai_link *link;
1608 	int ret;
1609 
1610 	link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1611 	if (link == NULL)
1612 		return -ENOMEM;
1613 
1614 	link->name = pcm->pcm_name;
1615 	link->stream_name = pcm->pcm_name;
1616 	link->id = pcm->pcm_id;
1617 
1618 	link->cpu_dai_name = pcm->dai_name;
1619 	link->codec_name = "snd-soc-dummy";
1620 	link->codec_dai_name = "snd-soc-dummy-dai";
1621 
1622 	/* enable DPCM */
1623 	link->dynamic = 1;
1624 	link->dpcm_playback = pcm->playback;
1625 	link->dpcm_capture = pcm->capture;
1626 
1627 	/* pass control to component driver for optional further init */
1628 	ret = soc_tplg_dai_link_load(tplg, link);
1629 	if (ret < 0) {
1630 		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1631 		kfree(link);
1632 		return ret;
1633 	}
1634 
1635 	link->dobj.index = tplg->index;
1636 	link->dobj.ops = tplg->ops;
1637 	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1638 	list_add(&link->dobj.list, &tplg->comp->dobj_list);
1639 
1640 	snd_soc_add_dai_link(tplg->comp->card, link);
1641 	return 0;
1642 }
1643 
1644 /* create a FE DAI and DAI link from the PCM object */
1645 static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1646 	struct snd_soc_tplg_pcm *pcm)
1647 {
1648 	int ret;
1649 
1650 	ret = soc_tplg_dai_create(tplg, pcm);
1651 	if (ret < 0)
1652 		return ret;
1653 
1654 	return  soc_tplg_link_create(tplg, pcm);
1655 }
1656 
1657 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1658 	struct snd_soc_tplg_hdr *hdr)
1659 {
1660 	struct snd_soc_tplg_pcm *pcm;
1661 	int count = hdr->count;
1662 	int i;
1663 
1664 	if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1665 		return 0;
1666 
1667 	if (soc_tplg_check_elem_count(tplg,
1668 		sizeof(struct snd_soc_tplg_pcm), count,
1669 		hdr->payload_size, "PCM DAI")) {
1670 		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1671 			count);
1672 		return -EINVAL;
1673 	}
1674 
1675 	/* create the FE DAIs and DAI links */
1676 	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1677 	for (i = 0; i < count; i++) {
1678 		if (pcm->size != sizeof(*pcm)) {
1679 			dev_err(tplg->dev, "ASoC: invalid pcm size\n");
1680 			return -EINVAL;
1681 		}
1682 
1683 		soc_tplg_pcm_create(tplg, pcm);
1684 		pcm++;
1685 	}
1686 
1687 	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1688 	tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count;
1689 
1690 	return 0;
1691 }
1692 
1693 static int soc_tplg_manifest_load(struct soc_tplg *tplg,
1694 	struct snd_soc_tplg_hdr *hdr)
1695 {
1696 	struct snd_soc_tplg_manifest *manifest;
1697 
1698 	if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
1699 		return 0;
1700 
1701 	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
1702 	if (manifest->size != sizeof(*manifest)) {
1703 		dev_err(tplg->dev, "ASoC: invalid manifest size\n");
1704 		return -EINVAL;
1705 	}
1706 
1707 	tplg->pos += sizeof(struct snd_soc_tplg_manifest);
1708 
1709 	if (tplg->comp && tplg->ops && tplg->ops->manifest)
1710 		return tplg->ops->manifest(tplg->comp, manifest);
1711 
1712 	dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
1713 	return 0;
1714 }
1715 
1716 /* validate header magic, size and type */
1717 static int soc_valid_header(struct soc_tplg *tplg,
1718 	struct snd_soc_tplg_hdr *hdr)
1719 {
1720 	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
1721 		return 0;
1722 
1723 	if (hdr->size != sizeof(*hdr)) {
1724 		dev_err(tplg->dev,
1725 			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
1726 			hdr->type, soc_tplg_get_hdr_offset(tplg),
1727 			tplg->fw->size);
1728 		return -EINVAL;
1729 	}
1730 
1731 	/* big endian firmware objects not supported atm */
1732 	if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
1733 		dev_err(tplg->dev,
1734 			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1735 			tplg->pass, hdr->magic,
1736 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1737 		return -EINVAL;
1738 	}
1739 
1740 	if (hdr->magic != SND_SOC_TPLG_MAGIC) {
1741 		dev_err(tplg->dev,
1742 			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1743 			tplg->pass, hdr->magic,
1744 			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1745 		return -EINVAL;
1746 	}
1747 
1748 	if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
1749 		dev_err(tplg->dev,
1750 			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1751 			tplg->pass, hdr->abi,
1752 			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
1753 			tplg->fw->size);
1754 		return -EINVAL;
1755 	}
1756 
1757 	if (hdr->payload_size == 0) {
1758 		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
1759 			soc_tplg_get_hdr_offset(tplg));
1760 		return -EINVAL;
1761 	}
1762 
1763 	if (tplg->pass == hdr->type)
1764 		dev_dbg(tplg->dev,
1765 			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
1766 			hdr->payload_size, hdr->type, hdr->version,
1767 			hdr->vendor_type, tplg->pass);
1768 
1769 	return 1;
1770 }
1771 
1772 /* check header type and call appropriate handler */
1773 static int soc_tplg_load_header(struct soc_tplg *tplg,
1774 	struct snd_soc_tplg_hdr *hdr)
1775 {
1776 	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
1777 
1778 	/* check for matching ID */
1779 	if (hdr->index != tplg->req_index &&
1780 		hdr->index != SND_SOC_TPLG_INDEX_ALL)
1781 		return 0;
1782 
1783 	tplg->index = hdr->index;
1784 
1785 	switch (hdr->type) {
1786 	case SND_SOC_TPLG_TYPE_MIXER:
1787 	case SND_SOC_TPLG_TYPE_ENUM:
1788 	case SND_SOC_TPLG_TYPE_BYTES:
1789 		return soc_tplg_kcontrol_elems_load(tplg, hdr);
1790 	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
1791 		return soc_tplg_dapm_graph_elems_load(tplg, hdr);
1792 	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
1793 		return soc_tplg_dapm_widget_elems_load(tplg, hdr);
1794 	case SND_SOC_TPLG_TYPE_PCM:
1795 		return soc_tplg_pcm_elems_load(tplg, hdr);
1796 	case SND_SOC_TPLG_TYPE_MANIFEST:
1797 		return soc_tplg_manifest_load(tplg, hdr);
1798 	default:
1799 		/* bespoke vendor data object */
1800 		return soc_tplg_vendor_load(tplg, hdr);
1801 	}
1802 
1803 	return 0;
1804 }
1805 
1806 /* process the topology file headers */
1807 static int soc_tplg_process_headers(struct soc_tplg *tplg)
1808 {
1809 	struct snd_soc_tplg_hdr *hdr;
1810 	int ret;
1811 
1812 	tplg->pass = SOC_TPLG_PASS_START;
1813 
1814 	/* process the header types from start to end */
1815 	while (tplg->pass <= SOC_TPLG_PASS_END) {
1816 
1817 		tplg->hdr_pos = tplg->fw->data;
1818 		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1819 
1820 		while (!soc_tplg_is_eof(tplg)) {
1821 
1822 			/* make sure header is valid before loading */
1823 			ret = soc_valid_header(tplg, hdr);
1824 			if (ret < 0)
1825 				return ret;
1826 			else if (ret == 0)
1827 				break;
1828 
1829 			/* load the header object */
1830 			ret = soc_tplg_load_header(tplg, hdr);
1831 			if (ret < 0)
1832 				return ret;
1833 
1834 			/* goto next header */
1835 			tplg->hdr_pos += hdr->payload_size +
1836 				sizeof(struct snd_soc_tplg_hdr);
1837 			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
1838 		}
1839 
1840 		/* next data type pass */
1841 		tplg->pass++;
1842 	}
1843 
1844 	/* signal DAPM we are complete */
1845 	ret = soc_tplg_dapm_complete(tplg);
1846 	if (ret < 0)
1847 		dev_err(tplg->dev,
1848 			"ASoC: failed to initialise DAPM from Firmware\n");
1849 
1850 	return ret;
1851 }
1852 
1853 static int soc_tplg_load(struct soc_tplg *tplg)
1854 {
1855 	int ret;
1856 
1857 	ret = soc_tplg_process_headers(tplg);
1858 	if (ret == 0)
1859 		soc_tplg_complete(tplg);
1860 
1861 	return ret;
1862 }
1863 
1864 /* load audio component topology from "firmware" file */
1865 int snd_soc_tplg_component_load(struct snd_soc_component *comp,
1866 	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
1867 {
1868 	struct soc_tplg tplg;
1869 
1870 	/* setup parsing context */
1871 	memset(&tplg, 0, sizeof(tplg));
1872 	tplg.fw = fw;
1873 	tplg.dev = comp->dev;
1874 	tplg.comp = comp;
1875 	tplg.ops = ops;
1876 	tplg.req_index = id;
1877 	tplg.io_ops = ops->io_ops;
1878 	tplg.io_ops_count = ops->io_ops_count;
1879 	tplg.bytes_ext_ops = ops->bytes_ext_ops;
1880 	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
1881 
1882 	return soc_tplg_load(&tplg);
1883 }
1884 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
1885 
1886 /* remove this dynamic widget */
1887 void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
1888 {
1889 	/* make sure we are a widget */
1890 	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
1891 		return;
1892 
1893 	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
1894 }
1895 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
1896 
1897 /* remove all dynamic widgets from this DAPM context */
1898 void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
1899 	u32 index)
1900 {
1901 	struct snd_soc_dapm_widget *w, *next_w;
1902 
1903 	list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
1904 
1905 		/* make sure we are a widget with correct context */
1906 		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
1907 			continue;
1908 
1909 		/* match ID */
1910 		if (w->dobj.index != index &&
1911 			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
1912 			continue;
1913 		/* check and free and dynamic widget kcontrols */
1914 		snd_soc_tplg_widget_remove(w);
1915 		snd_soc_dapm_free_widget(w);
1916 	}
1917 	snd_soc_dapm_reset_cache(dapm);
1918 }
1919 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
1920 
1921 /* remove dynamic controls from the component driver */
1922 int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
1923 {
1924 	struct snd_soc_dobj *dobj, *next_dobj;
1925 	int pass = SOC_TPLG_PASS_END;
1926 
1927 	/* process the header types from end to start */
1928 	while (pass >= SOC_TPLG_PASS_START) {
1929 
1930 		/* remove mixer controls */
1931 		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
1932 			list) {
1933 
1934 			/* match index */
1935 			if (dobj->index != index &&
1936 				dobj->index != SND_SOC_TPLG_INDEX_ALL)
1937 				continue;
1938 
1939 			switch (dobj->type) {
1940 			case SND_SOC_DOBJ_MIXER:
1941 				remove_mixer(comp, dobj, pass);
1942 				break;
1943 			case SND_SOC_DOBJ_ENUM:
1944 				remove_enum(comp, dobj, pass);
1945 				break;
1946 			case SND_SOC_DOBJ_BYTES:
1947 				remove_bytes(comp, dobj, pass);
1948 				break;
1949 			case SND_SOC_DOBJ_WIDGET:
1950 				remove_widget(comp, dobj, pass);
1951 				break;
1952 			case SND_SOC_DOBJ_PCM:
1953 				remove_dai(comp, dobj, pass);
1954 				break;
1955 			case SND_SOC_DOBJ_DAI_LINK:
1956 				remove_link(comp, dobj, pass);
1957 				break;
1958 			default:
1959 				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
1960 					dobj->type);
1961 				break;
1962 			}
1963 		}
1964 		pass--;
1965 	}
1966 
1967 	/* let caller know if FW can be freed when no objects are left */
1968 	return !list_empty(&comp->dobj_list);
1969 }
1970 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
1971