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