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