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