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