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