OSDN Git Service

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