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