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