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