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