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