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