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