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