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