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