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