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