blob: f485f7f751a1ddcdd9f1466f41c1cfb37a763ee8 [file] [log] [blame]
Kuninori Morimotof2b6a1b2018-07-02 06:24:45 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-topology.c -- ALSA SoC Topology
4//
5// Copyright (C) 2012 Texas Instruments Inc.
6// Copyright (C) 2015 Intel Corporation.
7//
8// Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9// K, Mythri P <mythri.p.k@intel.com>
10// Prusty, Subhransu S <subhransu.s.prusty@intel.com>
11// B, Jayachandran <jayachandran.b@intel.com>
12// Abdullah, Omair M <omair.m.abdullah@intel.com>
13// Jin, Yao <yao.jin@intel.com>
14// Lin, Mengdong <mengdong.lin@intel.com>
15//
16// Add support to read audio firmware topology alongside firmware text. The
17// topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
18// equalizers, firmware, coefficients etc.
19//
20// This file only manages the core ALSA and ASoC components, all other bespoke
21// firmware topology data is passed to component drivers for bespoke handling.
Liam Girdwood8a978232015-05-29 19:06:14 +010022
23#include <linux/kernel.h>
24#include <linux/export.h>
25#include <linux/list.h>
26#include <linux/firmware.h>
27#include <linux/slab.h>
28#include <sound/soc.h>
29#include <sound/soc-dapm.h>
30#include <sound/soc-topology.h>
Mengdong Lin28a87ee2015-08-05 14:41:13 +010031#include <sound/tlv.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010032
Pierre-Louis Bossart21141712019-04-04 14:13:58 -050033#define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */
34
Liam Girdwood8a978232015-05-29 19:06:14 +010035/*
36 * We make several passes over the data (since it wont necessarily be ordered)
37 * and process objects in the following order. This guarantees the component
38 * drivers will be ready with any vendor data before the mixers and DAPM objects
39 * are loaded (that may make use of the vendor data).
40 */
41#define SOC_TPLG_PASS_MANIFEST 0
42#define SOC_TPLG_PASS_VENDOR 1
43#define SOC_TPLG_PASS_MIXER 2
44#define SOC_TPLG_PASS_WIDGET 3
Mengdong Lin1a8e7fa2015-08-10 22:48:30 +080045#define SOC_TPLG_PASS_PCM_DAI 4
46#define SOC_TPLG_PASS_GRAPH 5
47#define SOC_TPLG_PASS_PINS 6
Mengdong Lin0038be92016-07-26 14:32:37 +080048#define SOC_TPLG_PASS_BE_DAI 7
Mengdong Lin593d9e52016-11-03 01:04:27 +080049#define SOC_TPLG_PASS_LINK 8
Liam Girdwood8a978232015-05-29 19:06:14 +010050
51#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
Mengdong Lin593d9e52016-11-03 01:04:27 +080052#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
Liam Girdwood8a978232015-05-29 19:06:14 +010053
Mengdong Lin583958f2016-10-11 14:36:42 +080054/* topology context */
Liam Girdwood8a978232015-05-29 19:06:14 +010055struct soc_tplg {
56 const struct firmware *fw;
57
58 /* runtime FW parsing */
59 const u8 *pos; /* read postion */
60 const u8 *hdr_pos; /* header position */
61 unsigned int pass; /* pass number */
62
63 /* component caller */
64 struct device *dev;
65 struct snd_soc_component *comp;
66 u32 index; /* current block index */
67 u32 req_index; /* required index, only loaded/free matching blocks */
68
Mengdong Lin88a17d82015-08-18 18:11:51 +080069 /* vendor specific kcontrol operations */
Liam Girdwood8a978232015-05-29 19:06:14 +010070 const struct snd_soc_tplg_kcontrol_ops *io_ops;
71 int io_ops_count;
72
Mengdong Lin1a3232d2015-08-18 18:12:20 +080073 /* vendor specific bytes ext handlers, for TLV bytes controls */
74 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
75 int bytes_ext_ops_count;
76
Liam Girdwood8a978232015-05-29 19:06:14 +010077 /* optional fw loading callbacks to component drivers */
78 struct snd_soc_tplg_ops *ops;
79};
80
81static int soc_tplg_process_headers(struct soc_tplg *tplg);
82static void soc_tplg_complete(struct soc_tplg *tplg);
83struct snd_soc_dapm_widget *
84snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
85 const struct snd_soc_dapm_widget *widget);
86struct snd_soc_dapm_widget *
87snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
88 const struct snd_soc_dapm_widget *widget);
89
90/* check we dont overflow the data for this control chunk */
91static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
92 unsigned int count, size_t bytes, const char *elem_type)
93{
94 const u8 *end = tplg->pos + elem_size * count;
95
96 if (end > tplg->fw->data + tplg->fw->size) {
97 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
98 elem_type);
99 return -EINVAL;
100 }
101
102 /* check there is enough room in chunk for control.
103 extra bytes at the end of control are for vendor data here */
104 if (elem_size * count > bytes) {
105 dev_err(tplg->dev,
106 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
107 elem_type, count, elem_size, bytes);
108 return -EINVAL;
109 }
110
111 return 0;
112}
113
114static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
115{
116 const u8 *end = tplg->hdr_pos;
117
118 if (end >= tplg->fw->data + tplg->fw->size)
119 return 1;
120 return 0;
121}
122
123static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
124{
125 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
126}
127
128static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
129{
130 return (unsigned long)(tplg->pos - tplg->fw->data);
131}
132
133/* mapping of Kcontrol types and associated operations. */
134static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
135 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
136 snd_soc_put_volsw, snd_soc_info_volsw},
137 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
138 snd_soc_put_volsw_sx, NULL},
139 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
140 snd_soc_put_enum_double, snd_soc_info_enum_double},
141 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
142 snd_soc_put_enum_double, NULL},
143 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
144 snd_soc_bytes_put, snd_soc_bytes_info},
145 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
146 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
147 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
148 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
149 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
150 snd_soc_put_strobe, NULL},
151 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530152 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100153 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
154 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
155 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
156 snd_soc_dapm_put_enum_double, NULL},
157 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
158 snd_soc_dapm_put_enum_double, NULL},
159 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
160 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
161};
162
163struct soc_tplg_map {
164 int uid;
165 int kid;
166};
167
168/* mapping of widget types from UAPI IDs to kernel IDs */
169static const struct soc_tplg_map dapm_map[] = {
170 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
171 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
172 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
173 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
174 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
175 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
176 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
177 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
178 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
179 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
180 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
181 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
182 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
183 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
184 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
185 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
Liam Girdwood8a70b452017-06-29 14:22:24 +0100186 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
187 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
188 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
189 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
190 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
191 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
192 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
193 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
Liam Girdwood8a978232015-05-29 19:06:14 +0100194};
195
196static int tplc_chan_get_reg(struct soc_tplg *tplg,
197 struct snd_soc_tplg_channel *chan, int map)
198{
199 int i;
200
201 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500202 if (le32_to_cpu(chan[i].id) == map)
203 return le32_to_cpu(chan[i].reg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100204 }
205
206 return -EINVAL;
207}
208
209static int tplc_chan_get_shift(struct soc_tplg *tplg,
210 struct snd_soc_tplg_channel *chan, int map)
211{
212 int i;
213
214 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500215 if (le32_to_cpu(chan[i].id) == map)
216 return le32_to_cpu(chan[i].shift);
Liam Girdwood8a978232015-05-29 19:06:14 +0100217 }
218
219 return -EINVAL;
220}
221
222static int get_widget_id(int tplg_type)
223{
224 int i;
225
226 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
227 if (tplg_type == dapm_map[i].uid)
228 return dapm_map[i].kid;
229 }
230
231 return -EINVAL;
232}
233
Liam Girdwood8a978232015-05-29 19:06:14 +0100234static inline void soc_bind_err(struct soc_tplg *tplg,
235 struct snd_soc_tplg_ctl_hdr *hdr, int index)
236{
237 dev_err(tplg->dev,
238 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
239 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
240 soc_tplg_get_offset(tplg));
241}
242
243static inline void soc_control_err(struct soc_tplg *tplg,
244 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
245{
246 dev_err(tplg->dev,
247 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
248 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
249 soc_tplg_get_offset(tplg));
250}
251
252/* pass vendor data to component driver for processing */
253static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
254 struct snd_soc_tplg_hdr *hdr)
255{
256 int ret = 0;
257
258 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100259 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100260 else {
261 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
262 hdr->vendor_type);
263 return -EINVAL;
264 }
265
266 if (ret < 0)
267 dev_err(tplg->dev,
268 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
269 soc_tplg_get_hdr_offset(tplg),
270 soc_tplg_get_hdr_offset(tplg),
271 hdr->type, hdr->vendor_type);
272 return ret;
273}
274
275/* pass vendor data to component driver for processing */
276static int soc_tplg_vendor_load(struct soc_tplg *tplg,
277 struct snd_soc_tplg_hdr *hdr)
278{
279 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
280 return 0;
281
282 return soc_tplg_vendor_load_(tplg, hdr);
283}
284
285/* optionally pass new dynamic widget to component driver. This is mainly for
286 * external widgets where we can assign private data/ops */
287static int soc_tplg_widget_load(struct soc_tplg *tplg,
288 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
289{
290 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100291 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
292 tplg_w);
Liam Girdwood8a978232015-05-29 19:06:14 +0100293
294 return 0;
295}
296
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100297/* optionally pass new dynamic widget to component driver. This is mainly for
298 * external widgets where we can assign private data/ops */
299static int soc_tplg_widget_ready(struct soc_tplg *tplg,
300 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
301{
302 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100303 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
304 tplg_w);
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100305
306 return 0;
307}
308
Masahiro Yamada183b8022017-02-27 14:29:20 -0800309/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800310static int soc_tplg_dai_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100311 struct snd_soc_dai_driver *dai_drv,
312 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
Liam Girdwood8a978232015-05-29 19:06:14 +0100313{
Mengdong Lin64527e82016-01-15 16:13:28 +0800314 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100315 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
316 pcm, dai);
Liam Girdwood8a978232015-05-29 19:06:14 +0100317
318 return 0;
319}
320
Masahiro Yamada183b8022017-02-27 14:29:20 -0800321/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800322static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100323 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
Mengdong Linacfc7d42016-01-15 16:13:37 +0800324{
325 if (tplg->comp && tplg->ops && tplg->ops->link_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100326 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800327
328 return 0;
329}
330
Liam Girdwood8a978232015-05-29 19:06:14 +0100331/* tell the component driver that all firmware has been loaded in this request */
332static void soc_tplg_complete(struct soc_tplg *tplg)
333{
334 if (tplg->comp && tplg->ops && tplg->ops->complete)
335 tplg->ops->complete(tplg->comp);
336}
337
338/* add a dynamic kcontrol */
339static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
340 const struct snd_kcontrol_new *control_new, const char *prefix,
341 void *data, struct snd_kcontrol **kcontrol)
342{
343 int err;
344
345 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
346 if (*kcontrol == NULL) {
347 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
348 control_new->name);
349 return -ENOMEM;
350 }
351
352 err = snd_ctl_add(card, *kcontrol);
353 if (err < 0) {
354 dev_err(dev, "ASoC: Failed to add %s: %d\n",
355 control_new->name, err);
356 return err;
357 }
358
359 return 0;
360}
361
362/* add a dynamic kcontrol for component driver */
363static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
364 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
365{
366 struct snd_soc_component *comp = tplg->comp;
367
368 return soc_tplg_add_dcontrol(comp->card->snd_card,
369 comp->dev, k, NULL, comp, kcontrol);
370}
371
372/* remove a mixer kcontrol */
373static void remove_mixer(struct snd_soc_component *comp,
374 struct snd_soc_dobj *dobj, int pass)
375{
376 struct snd_card *card = comp->card->snd_card;
377 struct soc_mixer_control *sm =
378 container_of(dobj, struct soc_mixer_control, dobj);
379 const unsigned int *p = NULL;
380
381 if (pass != SOC_TPLG_PASS_MIXER)
382 return;
383
384 if (dobj->ops && dobj->ops->control_unload)
385 dobj->ops->control_unload(comp, dobj);
386
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600387 if (dobj->control.kcontrol->tlv.p)
388 p = dobj->control.kcontrol->tlv.p;
389 snd_ctl_remove(card, dobj->control.kcontrol);
390 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100391 kfree(sm);
392 kfree(p);
393}
394
395/* remove an enum kcontrol */
396static void remove_enum(struct snd_soc_component *comp,
397 struct snd_soc_dobj *dobj, int pass)
398{
399 struct snd_card *card = comp->card->snd_card;
400 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
401 int i;
402
403 if (pass != SOC_TPLG_PASS_MIXER)
404 return;
405
406 if (dobj->ops && dobj->ops->control_unload)
407 dobj->ops->control_unload(comp, dobj);
408
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600409 snd_ctl_remove(card, dobj->control.kcontrol);
410 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100411
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600412 kfree(dobj->control.dvalues);
Liam Girdwood8a978232015-05-29 19:06:14 +0100413 for (i = 0; i < se->items; i++)
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600414 kfree(dobj->control.dtexts[i]);
Amadeusz Sławiński34db6a32019-01-25 14:06:44 -0600415 kfree(dobj->control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +0100416 kfree(se);
417}
418
419/* remove a byte kcontrol */
420static void remove_bytes(struct snd_soc_component *comp,
421 struct snd_soc_dobj *dobj, int pass)
422{
423 struct snd_card *card = comp->card->snd_card;
424 struct soc_bytes_ext *sb =
425 container_of(dobj, struct soc_bytes_ext, dobj);
426
427 if (pass != SOC_TPLG_PASS_MIXER)
428 return;
429
430 if (dobj->ops && dobj->ops->control_unload)
431 dobj->ops->control_unload(comp, dobj);
432
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600433 snd_ctl_remove(card, dobj->control.kcontrol);
434 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100435 kfree(sb);
436}
437
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -0600438/* remove a route */
439static void remove_route(struct snd_soc_component *comp,
440 struct snd_soc_dobj *dobj, int pass)
441{
442 struct snd_soc_dapm_route *route =
443 container_of(dobj, struct snd_soc_dapm_route, dobj);
444
445 if (pass != SOC_TPLG_PASS_GRAPH)
446 return;
447
448 if (dobj->ops && dobj->ops->dapm_route_unload)
449 dobj->ops->dapm_route_unload(comp, dobj);
450
451 list_del(&dobj->list);
452 kfree(route);
453}
454
Liam Girdwood8a978232015-05-29 19:06:14 +0100455/* remove a widget and it's kcontrols - routes must be removed first */
456static void remove_widget(struct snd_soc_component *comp,
457 struct snd_soc_dobj *dobj, int pass)
458{
459 struct snd_card *card = comp->card->snd_card;
460 struct snd_soc_dapm_widget *w =
461 container_of(dobj, struct snd_soc_dapm_widget, dobj);
462 int i;
463
464 if (pass != SOC_TPLG_PASS_WIDGET)
465 return;
466
467 if (dobj->ops && dobj->ops->widget_unload)
468 dobj->ops->widget_unload(comp, dobj);
469
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000470 if (!w->kcontrols)
471 goto free_news;
472
Liam Girdwood8a978232015-05-29 19:06:14 +0100473 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800474 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100475 * The enum may either have an array of values or strings.
476 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800477 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100478 /* enumerated widget mixer */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100479 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800480 struct snd_kcontrol *kcontrol = w->kcontrols[i];
481 struct soc_enum *se =
482 (struct soc_enum *)kcontrol->private_value;
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100483 int j;
Liam Girdwood8a978232015-05-29 19:06:14 +0100484
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800485 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100486
Ranjani Sridharan54f88442019-04-04 19:48:33 -0700487 /* free enum kcontrol's dvalues and dtexts */
488 kfree(se->dobj.control.dvalues);
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100489 for (j = 0; j < se->items; j++)
Ranjani Sridharan54f88442019-04-04 19:48:33 -0700490 kfree(se->dobj.control.dtexts[j]);
491 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +0100492
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800493 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100494 kfree(w->kcontrol_news[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800495 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100496 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800497 /* volume mixer or bytes controls */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100498 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100499 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100500
Mengdong Lineea3dd42016-11-25 16:09:17 +0800501 if (dobj->widget.kcontrol_type
502 == SND_SOC_TPLG_TYPE_MIXER)
503 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100504
Mengdong Lineea3dd42016-11-25 16:09:17 +0800505 /* Private value is used as struct soc_mixer_control
506 * for volume mixers or soc_bytes_ext for bytes
507 * controls.
508 */
509 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000510 snd_ctl_remove(card, kcontrol);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100511 kfree(w->kcontrol_news[i].name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100512 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100513 }
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000514
515free_news:
516 kfree(w->kcontrol_news);
517
Amadeusz Sławińskia46e8392019-01-25 14:06:43 -0600518 list_del(&dobj->list);
519
Liam Girdwood8a978232015-05-29 19:06:14 +0100520 /* widget w is freed by soc-dapm.c */
521}
522
Mengdong Lin64527e82016-01-15 16:13:28 +0800523/* remove DAI configurations */
524static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100525 struct snd_soc_dobj *dobj, int pass)
526{
Mengdong Lin64527e82016-01-15 16:13:28 +0800527 struct snd_soc_dai_driver *dai_drv =
528 container_of(dobj, struct snd_soc_dai_driver, dobj);
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600529 struct snd_soc_dai *dai;
Mengdong Lin64527e82016-01-15 16:13:28 +0800530
Liam Girdwood8a978232015-05-29 19:06:14 +0100531 if (pass != SOC_TPLG_PASS_PCM_DAI)
532 return;
533
Mengdong Lin64527e82016-01-15 16:13:28 +0800534 if (dobj->ops && dobj->ops->dai_unload)
535 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100536
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600537 list_for_each_entry(dai, &comp->dai_list, list)
538 if (dai->driver == dai_drv)
539 dai->driver = NULL;
540
Bard liao7b6f68a2019-03-05 23:57:52 +0800541 kfree(dai_drv->playback.stream_name);
542 kfree(dai_drv->capture.stream_name);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800543 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100544 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800545 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100546}
547
Mengdong Linacfc7d42016-01-15 16:13:37 +0800548/* remove link configurations */
549static void remove_link(struct snd_soc_component *comp,
550 struct snd_soc_dobj *dobj, int pass)
551{
552 struct snd_soc_dai_link *link =
553 container_of(dobj, struct snd_soc_dai_link, dobj);
554
555 if (pass != SOC_TPLG_PASS_PCM_DAI)
556 return;
557
558 if (dobj->ops && dobj->ops->link_unload)
559 dobj->ops->link_unload(comp, dobj);
560
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800561 kfree(link->name);
562 kfree(link->stream_name);
Kuninori Morimoto23b946c2019-06-06 13:19:14 +0900563 kfree(link->cpus->dai_name);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800564
Mengdong Linacfc7d42016-01-15 16:13:37 +0800565 list_del(&dobj->list);
566 snd_soc_remove_dai_link(comp->card, link);
567 kfree(link);
568}
569
Bard liaoadfebb52019-02-01 11:07:40 -0600570/* unload dai link */
571static void remove_backend_link(struct snd_soc_component *comp,
572 struct snd_soc_dobj *dobj, int pass)
573{
574 if (pass != SOC_TPLG_PASS_LINK)
575 return;
576
577 if (dobj->ops && dobj->ops->link_unload)
578 dobj->ops->link_unload(comp, dobj);
579
580 /*
581 * We don't free the link here as what remove_link() do since BE
582 * links are not allocated by topology.
583 * We however need to reset the dobj type to its initial values
584 */
585 dobj->type = SND_SOC_DOBJ_NONE;
586 list_del(&dobj->list);
587}
588
Liam Girdwood8a978232015-05-29 19:06:14 +0100589/* bind a kcontrol to it's IO handlers */
590static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
591 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800592 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100593{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800594 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800595 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800596 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100597
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500598 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800599 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
600 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
601 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
602 struct soc_bytes_ext *sbe;
603 struct snd_soc_tplg_bytes_control *be;
604
605 sbe = (struct soc_bytes_ext *)k->private_value;
606 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
607
608 /* TLV bytes controls need standard kcontrol info handler,
609 * TLV callback and extended put/get handlers.
610 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530611 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800612 k->tlv.c = snd_soc_bytes_tlv_callback;
613
614 ext_ops = tplg->bytes_ext_ops;
615 num_ops = tplg->bytes_ext_ops_count;
616 for (i = 0; i < num_ops; i++) {
617 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
618 sbe->put = ext_ops[i].put;
619 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
620 sbe->get = ext_ops[i].get;
621 }
622
623 if (sbe->put && sbe->get)
624 return 0;
625 else
626 return -EINVAL;
627 }
628
Mengdong Lin88a17d82015-08-18 18:11:51 +0800629 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800630 ops = tplg->io_ops;
631 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100632 for (i = 0; i < num_ops; i++) {
633
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800634 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100635 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800636 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100637 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800638 if (k->info == NULL && ops[i].id == hdr->ops.info)
639 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100640 }
641
Mengdong Lin88a17d82015-08-18 18:11:51 +0800642 /* vendor specific handlers found ? */
643 if (k->put && k->get && k->info)
644 return 0;
645
646 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800647 ops = io_ops;
648 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800649 for (i = 0; i < num_ops; i++) {
650
651 if (k->put == NULL && ops[i].id == hdr->ops.put)
652 k->put = ops[i].put;
653 if (k->get == NULL && ops[i].id == hdr->ops.get)
654 k->get = ops[i].get;
655 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100656 k->info = ops[i].info;
657 }
658
659 /* standard handlers found ? */
660 if (k->put && k->get && k->info)
661 return 0;
662
Liam Girdwood8a978232015-05-29 19:06:14 +0100663 /* nothing to bind */
664 return -EINVAL;
665}
666
667/* bind a widgets to it's evnt handlers */
668int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
669 const struct snd_soc_tplg_widget_events *events,
670 int num_events, u16 event_type)
671{
672 int i;
673
674 w->event = NULL;
675
676 for (i = 0; i < num_events; i++) {
677 if (event_type == events[i].type) {
678
679 /* found - so assign event */
680 w->event = events[i].event_handler;
681 return 0;
682 }
683 }
684
685 /* not found */
686 return -EINVAL;
687}
688EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
689
690/* optionally pass new dynamic kcontrol to component driver. */
691static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
692 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
693{
694 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100695 return tplg->ops->control_load(tplg->comp, tplg->index, k,
696 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100697
698 return 0;
699}
700
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100701
702static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
703 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100704{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100705 unsigned int item_len = 2 * sizeof(unsigned int);
706 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100707
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100708 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
709 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100710 return -ENOMEM;
711
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100712 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
713 p[1] = item_len;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500714 p[2] = le32_to_cpu(scale->min);
715 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
716 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100717
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100718 kc->tlv.p = (void *)p;
719 return 0;
720}
721
722static int soc_tplg_create_tlv(struct soc_tplg *tplg,
723 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
724{
725 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500726 u32 access = le32_to_cpu(tc->access);
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100727
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500728 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100729 return 0;
730
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500731 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100732 tplg_tlv = &tc->tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500733 switch (le32_to_cpu(tplg_tlv->type)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100734 case SNDRV_CTL_TLVT_DB_SCALE:
735 return soc_tplg_create_tlv_db_scale(tplg, kc,
736 &tplg_tlv->scale);
737
738 /* TODO: add support for other TLV types */
739 default:
740 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
741 tplg_tlv->type);
742 return -EINVAL;
743 }
744 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100745
746 return 0;
747}
748
749static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
750 struct snd_kcontrol_new *kc)
751{
752 kfree(kc->tlv.p);
753}
754
755static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
756 size_t size)
757{
758 struct snd_soc_tplg_bytes_control *be;
759 struct soc_bytes_ext *sbe;
760 struct snd_kcontrol_new kc;
761 int i, err;
762
763 if (soc_tplg_check_elem_count(tplg,
764 sizeof(struct snd_soc_tplg_bytes_control), count,
765 size, "mixer bytes")) {
766 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
767 count);
768 return -EINVAL;
769 }
770
771 for (i = 0; i < count; i++) {
772 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
773
774 /* validate kcontrol */
775 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
776 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
777 return -EINVAL;
778
779 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
780 if (sbe == NULL)
781 return -ENOMEM;
782
783 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500784 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100785
786 dev_dbg(tplg->dev,
787 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
788 be->hdr.name, be->hdr.access);
789
790 memset(&kc, 0, sizeof(kc));
791 kc.name = be->hdr.name;
792 kc.private_value = (long)sbe;
793 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500794 kc.access = le32_to_cpu(be->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100795
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500796 sbe->max = le32_to_cpu(be->max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100797 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
798 sbe->dobj.ops = tplg->ops;
799 INIT_LIST_HEAD(&sbe->dobj.list);
800
801 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800802 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100803 if (err) {
804 soc_control_err(tplg, &be->hdr, be->hdr.name);
805 kfree(sbe);
806 continue;
807 }
808
809 /* pass control to driver for optional further init */
810 err = soc_tplg_init_kcontrol(tplg, &kc,
811 (struct snd_soc_tplg_ctl_hdr *)be);
812 if (err < 0) {
813 dev_err(tplg->dev, "ASoC: failed to init %s\n",
814 be->hdr.name);
815 kfree(sbe);
816 continue;
817 }
818
819 /* register control here */
820 err = soc_tplg_add_kcontrol(tplg, &kc,
821 &sbe->dobj.control.kcontrol);
822 if (err < 0) {
823 dev_err(tplg->dev, "ASoC: failed to add %s\n",
824 be->hdr.name);
825 kfree(sbe);
826 continue;
827 }
828
829 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
830 }
831 return 0;
832
833}
834
835static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
836 size_t size)
837{
838 struct snd_soc_tplg_mixer_control *mc;
839 struct soc_mixer_control *sm;
840 struct snd_kcontrol_new kc;
841 int i, err;
842
843 if (soc_tplg_check_elem_count(tplg,
844 sizeof(struct snd_soc_tplg_mixer_control),
845 count, size, "mixers")) {
846
847 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
848 count);
849 return -EINVAL;
850 }
851
852 for (i = 0; i < count; i++) {
853 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
854
855 /* validate kcontrol */
856 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
857 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
858 return -EINVAL;
859
860 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
861 if (sm == NULL)
862 return -ENOMEM;
863 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500864 le32_to_cpu(mc->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100865
866 dev_dbg(tplg->dev,
867 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
868 mc->hdr.name, mc->hdr.access);
869
870 memset(&kc, 0, sizeof(kc));
871 kc.name = mc->hdr.name;
872 kc.private_value = (long)sm;
873 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500874 kc.access = le32_to_cpu(mc->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100875
876 /* we only support FL/FR channel mapping atm */
877 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
878 SNDRV_CHMAP_FL);
879 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
880 SNDRV_CHMAP_FR);
881 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
882 SNDRV_CHMAP_FL);
883 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
884 SNDRV_CHMAP_FR);
885
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500886 sm->max = le32_to_cpu(mc->max);
887 sm->min = le32_to_cpu(mc->min);
888 sm->invert = le32_to_cpu(mc->invert);
889 sm->platform_max = le32_to_cpu(mc->platform_max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100890 sm->dobj.index = tplg->index;
891 sm->dobj.ops = tplg->ops;
892 sm->dobj.type = SND_SOC_DOBJ_MIXER;
893 INIT_LIST_HEAD(&sm->dobj.list);
894
895 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800896 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100897 if (err) {
898 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
899 kfree(sm);
900 continue;
901 }
902
Bard liao3789deb2019-03-13 21:49:43 +0800903 /* create any TLV data */
904 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
905
Liam Girdwood8a978232015-05-29 19:06:14 +0100906 /* pass control to driver for optional further init */
907 err = soc_tplg_init_kcontrol(tplg, &kc,
908 (struct snd_soc_tplg_ctl_hdr *) mc);
909 if (err < 0) {
910 dev_err(tplg->dev, "ASoC: failed to init %s\n",
911 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +0800912 soc_tplg_free_tlv(tplg, &kc);
Liam Girdwood8a978232015-05-29 19:06:14 +0100913 kfree(sm);
914 continue;
915 }
916
Liam Girdwood8a978232015-05-29 19:06:14 +0100917 /* register control here */
918 err = soc_tplg_add_kcontrol(tplg, &kc,
919 &sm->dobj.control.kcontrol);
920 if (err < 0) {
921 dev_err(tplg->dev, "ASoC: failed to add %s\n",
922 mc->hdr.name);
923 soc_tplg_free_tlv(tplg, &kc);
924 kfree(sm);
925 continue;
926 }
927
928 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
929 }
930
931 return 0;
932}
933
934static int soc_tplg_denum_create_texts(struct soc_enum *se,
935 struct snd_soc_tplg_enum_control *ec)
936{
937 int i, ret;
938
939 se->dobj.control.dtexts =
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500940 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100941 if (se->dobj.control.dtexts == NULL)
942 return -ENOMEM;
943
944 for (i = 0; i < ec->items; i++) {
945
946 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
947 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
948 ret = -EINVAL;
949 goto err;
950 }
951
952 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
953 if (!se->dobj.control.dtexts[i]) {
954 ret = -ENOMEM;
955 goto err;
956 }
957 }
958
Mousumi Janab6e38b22017-04-11 13:06:22 +0530959 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100960 return 0;
961
962err:
963 for (--i; i >= 0; i--)
964 kfree(se->dobj.control.dtexts[i]);
965 kfree(se->dobj.control.dtexts);
966 return ret;
967}
968
969static int soc_tplg_denum_create_values(struct soc_enum *se,
970 struct snd_soc_tplg_enum_control *ec)
971{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500972 int i;
973
974 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
Liam Girdwood8a978232015-05-29 19:06:14 +0100975 return -EINVAL;
976
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500977 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
978 sizeof(u32),
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200979 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100980 if (!se->dobj.control.dvalues)
981 return -ENOMEM;
982
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500983 /* convert from little-endian */
984 for (i = 0; i < le32_to_cpu(ec->items); i++) {
985 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
986 }
987
Liam Girdwood8a978232015-05-29 19:06:14 +0100988 return 0;
989}
990
991static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
992 size_t size)
993{
994 struct snd_soc_tplg_enum_control *ec;
995 struct soc_enum *se;
996 struct snd_kcontrol_new kc;
997 int i, ret, err;
998
999 if (soc_tplg_check_elem_count(tplg,
1000 sizeof(struct snd_soc_tplg_enum_control),
1001 count, size, "enums")) {
1002
1003 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1004 count);
1005 return -EINVAL;
1006 }
1007
1008 for (i = 0; i < count; i++) {
1009 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001010
1011 /* validate kcontrol */
1012 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1013 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1014 return -EINVAL;
1015
1016 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1017 if (se == NULL)
1018 return -ENOMEM;
1019
Liam Girdwood02b64242019-03-01 19:08:52 -06001020 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001021 le32_to_cpu(ec->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001022
Liam Girdwood8a978232015-05-29 19:06:14 +01001023 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1024 ec->hdr.name, ec->items);
1025
1026 memset(&kc, 0, sizeof(kc));
1027 kc.name = ec->hdr.name;
1028 kc.private_value = (long)se;
1029 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001030 kc.access = le32_to_cpu(ec->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001031
1032 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1033 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1034 SNDRV_CHMAP_FL);
1035 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1036 SNDRV_CHMAP_FL);
1037
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001038 se->items = le32_to_cpu(ec->items);
1039 se->mask = le32_to_cpu(ec->mask);
Liam Girdwood8a978232015-05-29 19:06:14 +01001040 se->dobj.index = tplg->index;
1041 se->dobj.type = SND_SOC_DOBJ_ENUM;
1042 se->dobj.ops = tplg->ops;
1043 INIT_LIST_HEAD(&se->dobj.list);
1044
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001045 switch (le32_to_cpu(ec->hdr.ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001046 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1047 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1048 err = soc_tplg_denum_create_values(se, ec);
1049 if (err < 0) {
1050 dev_err(tplg->dev,
1051 "ASoC: could not create values for %s\n",
1052 ec->hdr.name);
1053 kfree(se);
1054 continue;
1055 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001056 /* fall through */
Liam Girdwood8a978232015-05-29 19:06:14 +01001057 case SND_SOC_TPLG_CTL_ENUM:
1058 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1059 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1060 err = soc_tplg_denum_create_texts(se, ec);
1061 if (err < 0) {
1062 dev_err(tplg->dev,
1063 "ASoC: could not create texts for %s\n",
1064 ec->hdr.name);
1065 kfree(se);
1066 continue;
1067 }
1068 break;
1069 default:
1070 dev_err(tplg->dev,
1071 "ASoC: invalid enum control type %d for %s\n",
1072 ec->hdr.ops.info, ec->hdr.name);
1073 kfree(se);
1074 continue;
1075 }
1076
1077 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001078 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001079 if (err) {
1080 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1081 kfree(se);
1082 continue;
1083 }
1084
1085 /* pass control to driver for optional further init */
1086 err = soc_tplg_init_kcontrol(tplg, &kc,
1087 (struct snd_soc_tplg_ctl_hdr *) ec);
1088 if (err < 0) {
1089 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1090 ec->hdr.name);
1091 kfree(se);
1092 continue;
1093 }
1094
1095 /* register control here */
1096 ret = soc_tplg_add_kcontrol(tplg,
1097 &kc, &se->dobj.control.kcontrol);
1098 if (ret < 0) {
1099 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1100 ec->hdr.name);
1101 kfree(se);
1102 continue;
1103 }
1104
1105 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1106 }
1107
1108 return 0;
1109}
1110
1111static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1112 struct snd_soc_tplg_hdr *hdr)
1113{
1114 struct snd_soc_tplg_ctl_hdr *control_hdr;
1115 int i;
1116
1117 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001118 tplg->pos += le32_to_cpu(hdr->size) +
1119 le32_to_cpu(hdr->payload_size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001120 return 0;
1121 }
1122
1123 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1124 soc_tplg_get_offset(tplg));
1125
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001126 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001127
1128 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1129
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001130 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001131 dev_err(tplg->dev, "ASoC: invalid control size\n");
1132 return -EINVAL;
1133 }
1134
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001135 switch (le32_to_cpu(control_hdr->ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001136 case SND_SOC_TPLG_CTL_VOLSW:
1137 case SND_SOC_TPLG_CTL_STROBE:
1138 case SND_SOC_TPLG_CTL_VOLSW_SX:
1139 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1140 case SND_SOC_TPLG_CTL_RANGE:
1141 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1142 case SND_SOC_TPLG_DAPM_CTL_PIN:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001143 soc_tplg_dmixer_create(tplg, 1,
1144 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001145 break;
1146 case SND_SOC_TPLG_CTL_ENUM:
1147 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1148 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1149 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1150 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001151 soc_tplg_denum_create(tplg, 1,
1152 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001153 break;
1154 case SND_SOC_TPLG_CTL_BYTES:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001155 soc_tplg_dbytes_create(tplg, 1,
1156 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001157 break;
1158 default:
1159 soc_bind_err(tplg, control_hdr, i);
1160 return -EINVAL;
1161 }
1162 }
1163
1164 return 0;
1165}
1166
Liam Girdwood503e79b2018-06-14 20:53:59 +01001167/* optionally pass new dynamic kcontrol to component driver. */
1168static int soc_tplg_add_route(struct soc_tplg *tplg,
1169 struct snd_soc_dapm_route *route)
1170{
1171 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1172 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1173 route);
1174
1175 return 0;
1176}
1177
Liam Girdwood8a978232015-05-29 19:06:14 +01001178static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1179 struct snd_soc_tplg_hdr *hdr)
1180{
1181 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001182 struct snd_soc_tplg_dapm_graph_elem *elem;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001183 struct snd_soc_dapm_route **routes;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001184 int count, i, j;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001185 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001186
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001187 count = le32_to_cpu(hdr->count);
1188
Liam Girdwood8a978232015-05-29 19:06:14 +01001189 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001190 tplg->pos +=
1191 le32_to_cpu(hdr->size) +
1192 le32_to_cpu(hdr->payload_size);
1193
Liam Girdwood8a978232015-05-29 19:06:14 +01001194 return 0;
1195 }
1196
1197 if (soc_tplg_check_elem_count(tplg,
1198 sizeof(struct snd_soc_tplg_dapm_graph_elem),
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001199 count, le32_to_cpu(hdr->payload_size), "graph")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001200
1201 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1202 count);
1203 return -EINVAL;
1204 }
1205
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001206 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1207 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001208
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001209 /* allocate memory for pointer to array of dapm routes */
1210 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1211 GFP_KERNEL);
1212 if (!routes)
1213 return -ENOMEM;
1214
1215 /*
1216 * allocate memory for each dapm route in the array.
1217 * This needs to be done individually so that
1218 * each route can be freed when it is removed in remove_route().
1219 */
1220 for (i = 0; i < count; i++) {
1221 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1222 if (!routes[i]) {
1223 /* free previously allocated memory */
1224 for (j = 0; j < i; j++)
1225 kfree(routes[j]);
1226
1227 kfree(routes);
1228 return -ENOMEM;
1229 }
1230 }
1231
Liam Girdwood8a978232015-05-29 19:06:14 +01001232 for (i = 0; i < count; i++) {
1233 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1234 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1235
1236 /* validate routes */
1237 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001238 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1239 ret = -EINVAL;
1240 break;
1241 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001242 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001243 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1244 ret = -EINVAL;
1245 break;
1246 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001247 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001248 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1249 ret = -EINVAL;
1250 break;
1251 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001252
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001253 routes[i]->source = elem->source;
1254 routes[i]->sink = elem->sink;
1255
1256 /* set to NULL atm for tplg users */
1257 routes[i]->connected = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001258 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001259 routes[i]->control = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001260 else
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001261 routes[i]->control = elem->control;
Liam Girdwood8a978232015-05-29 19:06:14 +01001262
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001263 /* add route dobj to dobj_list */
1264 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1265 routes[i]->dobj.ops = tplg->ops;
1266 routes[i]->dobj.index = tplg->index;
1267 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1268
1269 soc_tplg_add_route(tplg, routes[i]);
Liam Girdwood503e79b2018-06-14 20:53:59 +01001270
Liam Girdwood8a978232015-05-29 19:06:14 +01001271 /* add route, but keep going if some fail */
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001272 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Liam Girdwood8a978232015-05-29 19:06:14 +01001273 }
1274
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001275 /* free memory allocated for all dapm routes in case of error */
1276 if (ret < 0)
1277 for (i = 0; i < count ; i++)
1278 kfree(routes[i]);
1279
1280 /*
1281 * free pointer to array of dapm routes as this is no longer needed.
1282 * The memory allocated for each dapm route will be freed
1283 * when it is removed in remove_route().
1284 */
1285 kfree(routes);
1286
1287 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001288}
1289
1290static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1291 struct soc_tplg *tplg, int num_kcontrols)
1292{
1293 struct snd_kcontrol_new *kc;
1294 struct soc_mixer_control *sm;
1295 struct snd_soc_tplg_mixer_control *mc;
1296 int i, err;
1297
Axel Lin4ca7deb2015-08-05 22:34:22 +08001298 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001299 if (kc == NULL)
1300 return NULL;
1301
1302 for (i = 0; i < num_kcontrols; i++) {
1303 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1304 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1305 if (sm == NULL)
1306 goto err;
1307
Liam Girdwood8a978232015-05-29 19:06:14 +01001308 /* validate kcontrol */
1309 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1310 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1311 goto err_str;
1312
Liam Girdwood02b64242019-03-01 19:08:52 -06001313 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001314 le32_to_cpu(mc->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001315
Liam Girdwood8a978232015-05-29 19:06:14 +01001316 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1317 mc->hdr.name, i);
1318
Liam Girdwood267e2c62018-03-27 12:04:04 +01001319 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1320 if (kc[i].name == NULL)
1321 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001322 kc[i].private_value = (long)sm;
1323 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1324 kc[i].access = mc->hdr.access;
1325
1326 /* we only support FL/FR channel mapping atm */
1327 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1328 SNDRV_CHMAP_FL);
1329 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1330 SNDRV_CHMAP_FR);
1331 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1332 SNDRV_CHMAP_FL);
1333 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1334 SNDRV_CHMAP_FR);
1335
1336 sm->max = mc->max;
1337 sm->min = mc->min;
1338 sm->invert = mc->invert;
1339 sm->platform_max = mc->platform_max;
1340 sm->dobj.index = tplg->index;
1341 INIT_LIST_HEAD(&sm->dobj.list);
1342
1343 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001344 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001345 if (err) {
1346 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1347 kfree(sm);
1348 continue;
1349 }
1350
Bard liao3789deb2019-03-13 21:49:43 +08001351 /* create any TLV data */
1352 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1353
Liam Girdwood8a978232015-05-29 19:06:14 +01001354 /* pass control to driver for optional further init */
1355 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1356 (struct snd_soc_tplg_ctl_hdr *)mc);
1357 if (err < 0) {
1358 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1359 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +08001360 soc_tplg_free_tlv(tplg, &kc[i]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001361 kfree(sm);
1362 continue;
1363 }
1364 }
1365 return kc;
1366
1367err_str:
1368 kfree(sm);
1369err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001370 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001371 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001372 kfree(kc[i].name);
1373 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001374 kfree(kc);
1375 return NULL;
1376}
1377
1378static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001379 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001380{
1381 struct snd_kcontrol_new *kc;
1382 struct snd_soc_tplg_enum_control *ec;
1383 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001384 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001385
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001386 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001387 if (kc == NULL)
1388 return NULL;
1389
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001390 for (i = 0; i < num_kcontrols; i++) {
1391 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1392 /* validate kcontrol */
1393 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1394 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001395 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001396
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001397 se = kzalloc(sizeof(*se), GFP_KERNEL);
1398 if (se == NULL)
1399 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001400
Liam Girdwood02b64242019-03-01 19:08:52 -06001401 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1402 ec->priv.size);
1403
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001404 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001405 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001406
Liam Girdwood267e2c62018-03-27 12:04:04 +01001407 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001408 if (kc[i].name == NULL) {
1409 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001410 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001411 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001412 kc[i].private_value = (long)se;
1413 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1414 kc[i].access = ec->hdr.access;
1415
1416 /* we only support FL/FR channel mapping atm */
1417 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1418 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1419 SNDRV_CHMAP_FL);
1420 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1421 SNDRV_CHMAP_FR);
1422
1423 se->items = ec->items;
1424 se->mask = ec->mask;
1425 se->dobj.index = tplg->index;
1426
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001427 switch (le32_to_cpu(ec->hdr.ops.info)) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001428 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1429 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1430 err = soc_tplg_denum_create_values(se, ec);
1431 if (err < 0) {
1432 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1433 ec->hdr.name);
1434 goto err_se;
1435 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001436 /* fall through */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001437 case SND_SOC_TPLG_CTL_ENUM:
1438 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1439 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1440 err = soc_tplg_denum_create_texts(se, ec);
1441 if (err < 0) {
1442 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1443 ec->hdr.name);
1444 goto err_se;
1445 }
1446 break;
1447 default:
1448 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1449 ec->hdr.ops.info, ec->hdr.name);
1450 goto err_se;
1451 }
1452
1453 /* map io handlers */
1454 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1455 if (err) {
1456 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1457 goto err_se;
1458 }
1459
1460 /* pass control to driver for optional further init */
1461 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1462 (struct snd_soc_tplg_ctl_hdr *)ec);
1463 if (err < 0) {
1464 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1465 ec->hdr.name);
1466 goto err_se;
1467 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001468 }
1469
1470 return kc;
1471
1472err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001473 for (; i >= 0; i--) {
1474 /* free values and texts */
1475 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001476 if (!se)
1477 continue;
1478
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001479 kfree(se->dobj.control.dvalues);
1480 for (j = 0; j < ec->items; j++)
1481 kfree(se->dobj.control.dtexts[j]);
Amadeusz Sławiński34db6a32019-01-25 14:06:44 -06001482 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +01001483
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001484 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001485 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001486 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001487err:
1488 kfree(kc);
1489
1490 return NULL;
1491}
1492
1493static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1494 struct soc_tplg *tplg, int count)
1495{
1496 struct snd_soc_tplg_bytes_control *be;
1497 struct soc_bytes_ext *sbe;
1498 struct snd_kcontrol_new *kc;
1499 int i, err;
1500
Axel Lin4ca7deb2015-08-05 22:34:22 +08001501 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001502 if (!kc)
1503 return NULL;
1504
1505 for (i = 0; i < count; i++) {
1506 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1507
1508 /* validate kcontrol */
1509 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1510 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1511 goto err;
1512
1513 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1514 if (sbe == NULL)
1515 goto err;
1516
1517 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001518 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001519
1520 dev_dbg(tplg->dev,
1521 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1522 be->hdr.name, be->hdr.access);
1523
Liam Girdwood267e2c62018-03-27 12:04:04 +01001524 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001525 if (kc[i].name == NULL) {
1526 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001527 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001528 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001529 kc[i].private_value = (long)sbe;
1530 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1531 kc[i].access = be->hdr.access;
1532
1533 sbe->max = be->max;
1534 INIT_LIST_HEAD(&sbe->dobj.list);
1535
1536 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001537 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001538 if (err) {
1539 soc_control_err(tplg, &be->hdr, be->hdr.name);
1540 kfree(sbe);
1541 continue;
1542 }
1543
1544 /* pass control to driver for optional further init */
1545 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1546 (struct snd_soc_tplg_ctl_hdr *)be);
1547 if (err < 0) {
1548 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1549 be->hdr.name);
1550 kfree(sbe);
1551 continue;
1552 }
1553 }
1554
1555 return kc;
1556
1557err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001558 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001559 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001560 kfree(kc[i].name);
1561 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001562
1563 kfree(kc);
1564 return NULL;
1565}
1566
1567static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1568 struct snd_soc_tplg_dapm_widget *w)
1569{
1570 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1571 struct snd_soc_dapm_widget template, *widget;
1572 struct snd_soc_tplg_ctl_hdr *control_hdr;
1573 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001574 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001575 int ret = 0;
1576
1577 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1578 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1579 return -EINVAL;
1580 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1581 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1582 return -EINVAL;
1583
1584 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1585 w->name, w->id);
1586
1587 memset(&template, 0, sizeof(template));
1588
1589 /* map user to kernel widget ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001590 template.id = get_widget_id(le32_to_cpu(w->id));
Liam Girdwood8a978232015-05-29 19:06:14 +01001591 if (template.id < 0)
1592 return template.id;
1593
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001594 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001595 template.name = kstrdup(w->name, GFP_KERNEL);
1596 if (!template.name)
1597 return -ENOMEM;
1598 template.sname = kstrdup(w->sname, GFP_KERNEL);
1599 if (!template.sname) {
1600 ret = -ENOMEM;
1601 goto err;
1602 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001603 template.reg = le32_to_cpu(w->reg);
1604 template.shift = le32_to_cpu(w->shift);
1605 template.mask = le32_to_cpu(w->mask);
1606 template.subseq = le32_to_cpu(w->subseq);
Liam Girdwood8a978232015-05-29 19:06:14 +01001607 template.on_val = w->invert ? 0 : 1;
1608 template.off_val = w->invert ? 1 : 0;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001609 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1610 template.event_flags = le16_to_cpu(w->event_flags);
Liam Girdwood8a978232015-05-29 19:06:14 +01001611 template.dobj.index = tplg->index;
1612
1613 tplg->pos +=
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001614 (sizeof(struct snd_soc_tplg_dapm_widget) +
1615 le32_to_cpu(w->priv.size));
1616
Liam Girdwood8a978232015-05-29 19:06:14 +01001617 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001618 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001619 template.num_kcontrols = 0;
1620 goto widget;
1621 }
1622
1623 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1624 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1625 w->name, w->num_kcontrols, control_hdr->type);
1626
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001627 switch (le32_to_cpu(control_hdr->ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001628 case SND_SOC_TPLG_CTL_VOLSW:
1629 case SND_SOC_TPLG_CTL_STROBE:
1630 case SND_SOC_TPLG_CTL_VOLSW_SX:
1631 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1632 case SND_SOC_TPLG_CTL_RANGE:
1633 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001634 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001635 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001636 template.kcontrol_news =
1637 soc_tplg_dapm_widget_dmixer_create(tplg,
1638 template.num_kcontrols);
1639 if (!template.kcontrol_news) {
1640 ret = -ENOMEM;
1641 goto hdr_err;
1642 }
1643 break;
1644 case SND_SOC_TPLG_CTL_ENUM:
1645 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1646 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1647 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1648 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001649 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001650 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001651 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001652 soc_tplg_dapm_widget_denum_create(tplg,
1653 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001654 if (!template.kcontrol_news) {
1655 ret = -ENOMEM;
1656 goto hdr_err;
1657 }
1658 break;
1659 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001660 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001661 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001662 template.kcontrol_news =
1663 soc_tplg_dapm_widget_dbytes_create(tplg,
1664 template.num_kcontrols);
1665 if (!template.kcontrol_news) {
1666 ret = -ENOMEM;
1667 goto hdr_err;
1668 }
1669 break;
1670 default:
1671 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1672 control_hdr->ops.get, control_hdr->ops.put,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001673 le32_to_cpu(control_hdr->ops.info));
Liam Girdwood8a978232015-05-29 19:06:14 +01001674 ret = -EINVAL;
1675 goto hdr_err;
1676 }
1677
1678widget:
1679 ret = soc_tplg_widget_load(tplg, &template, w);
1680 if (ret < 0)
1681 goto hdr_err;
1682
1683 /* card dapm mutex is held by the core if we are loading topology
1684 * data during sound card init. */
1685 if (card->instantiated)
1686 widget = snd_soc_dapm_new_control(dapm, &template);
1687 else
1688 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001689 if (IS_ERR(widget)) {
1690 ret = PTR_ERR(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001691 goto hdr_err;
1692 }
1693
1694 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001695 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001696 widget->dobj.ops = tplg->ops;
1697 widget->dobj.index = tplg->index;
1698 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001699
1700 ret = soc_tplg_widget_ready(tplg, widget, w);
1701 if (ret < 0)
1702 goto ready_err;
1703
Bard liao7620fe92019-01-25 14:06:45 -06001704 kfree(template.sname);
1705 kfree(template.name);
1706
Liam Girdwood8a978232015-05-29 19:06:14 +01001707 return 0;
1708
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001709ready_err:
1710 snd_soc_tplg_widget_remove(widget);
1711 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001712hdr_err:
1713 kfree(template.sname);
1714err:
1715 kfree(template.name);
1716 return ret;
1717}
1718
1719static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1720 struct snd_soc_tplg_hdr *hdr)
1721{
1722 struct snd_soc_tplg_dapm_widget *widget;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001723 int ret, count, i;
1724
1725 count = le32_to_cpu(hdr->count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001726
1727 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1728 return 0;
1729
1730 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1731
1732 for (i = 0; i < count; i++) {
1733 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001734 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001735 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1736 return -EINVAL;
1737 }
1738
Liam Girdwood8a978232015-05-29 19:06:14 +01001739 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001740 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001741 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1742 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001743 return ret;
1744 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001745 }
1746
1747 return 0;
1748}
1749
1750static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1751{
1752 struct snd_soc_card *card = tplg->comp->card;
1753 int ret;
1754
1755 /* Card might not have been registered at this point.
1756 * If so, just return success.
1757 */
1758 if (!card || !card->instantiated) {
1759 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001760 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001761 return 0;
1762 }
1763
1764 ret = snd_soc_dapm_new_widgets(card);
1765 if (ret < 0)
1766 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1767 ret);
1768
1769 return 0;
1770}
1771
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001772static void set_stream_info(struct snd_soc_pcm_stream *stream,
1773 struct snd_soc_tplg_stream_caps *caps)
1774{
1775 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001776 stream->channels_min = le32_to_cpu(caps->channels_min);
1777 stream->channels_max = le32_to_cpu(caps->channels_max);
1778 stream->rates = le32_to_cpu(caps->rates);
1779 stream->rate_min = le32_to_cpu(caps->rate_min);
1780 stream->rate_max = le32_to_cpu(caps->rate_max);
1781 stream->formats = le64_to_cpu(caps->formats);
1782 stream->sig_bits = le32_to_cpu(caps->sig_bits);
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001783}
1784
Mengdong Lin0038be92016-07-26 14:32:37 +08001785static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1786 unsigned int flag_mask, unsigned int flags)
1787{
1788 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1789 dai_drv->symmetric_rates =
1790 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1791
1792 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1793 dai_drv->symmetric_channels =
1794 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1795 1 : 0;
1796
1797 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1798 dai_drv->symmetric_samplebits =
1799 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1800 1 : 0;
1801}
1802
Mengdong Lin64527e82016-01-15 16:13:28 +08001803static int soc_tplg_dai_create(struct soc_tplg *tplg,
1804 struct snd_soc_tplg_pcm *pcm)
1805{
1806 struct snd_soc_dai_driver *dai_drv;
1807 struct snd_soc_pcm_stream *stream;
1808 struct snd_soc_tplg_stream_caps *caps;
1809 int ret;
1810
1811 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1812 if (dai_drv == NULL)
1813 return -ENOMEM;
1814
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001815 if (strlen(pcm->dai_name))
1816 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001817 dai_drv->id = le32_to_cpu(pcm->dai_id);
Mengdong Lin64527e82016-01-15 16:13:28 +08001818
1819 if (pcm->playback) {
1820 stream = &dai_drv->playback;
1821 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001822 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001823 }
1824
1825 if (pcm->capture) {
1826 stream = &dai_drv->capture;
1827 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001828 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001829 }
1830
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001831 if (pcm->compress)
1832 dai_drv->compress_new = snd_soc_new_compress;
1833
Mengdong Lin64527e82016-01-15 16:13:28 +08001834 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001835 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001836 if (ret < 0) {
1837 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Bard liao7b6f68a2019-03-05 23:57:52 +08001838 kfree(dai_drv->playback.stream_name);
1839 kfree(dai_drv->capture.stream_name);
1840 kfree(dai_drv->name);
Mengdong Lin64527e82016-01-15 16:13:28 +08001841 kfree(dai_drv);
1842 return ret;
1843 }
1844
1845 dai_drv->dobj.index = tplg->index;
1846 dai_drv->dobj.ops = tplg->ops;
1847 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1848 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1849
1850 /* register the DAI to the component */
1851 return snd_soc_register_dai(tplg->comp, dai_drv);
1852}
1853
Mengdong Lin717a8e72016-11-03 01:03:34 +08001854static void set_link_flags(struct snd_soc_dai_link *link,
1855 unsigned int flag_mask, unsigned int flags)
1856{
1857 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1858 link->symmetric_rates =
1859 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1860
1861 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1862 link->symmetric_channels =
1863 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1864 1 : 0;
1865
1866 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1867 link->symmetric_samplebits =
1868 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1869 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001870
1871 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1872 link->ignore_suspend =
1873 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1874 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001875}
1876
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001877/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001878static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001879 struct snd_soc_tplg_pcm *pcm)
1880{
1881 struct snd_soc_dai_link *link;
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001882 struct snd_soc_dai_link_component *dlc;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001883 int ret;
1884
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001885 /* link + cpu + codec */
1886 link = kzalloc(sizeof(*link) + (2 * sizeof(*dlc)), GFP_KERNEL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001887 if (link == NULL)
1888 return -ENOMEM;
1889
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001890 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1891
1892 link->cpus = &dlc[0];
1893 link->codecs = &dlc[1];
1894
1895 link->num_cpus = 1;
1896 link->num_codecs = 1;
1897
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001898 if (strlen(pcm->pcm_name)) {
1899 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1900 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1901 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001902 link->id = le32_to_cpu(pcm->pcm_id);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001903
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001904 if (strlen(pcm->dai_name))
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001905 link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001906
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001907 link->codecs->name = "snd-soc-dummy";
1908 link->codecs->dai_name = "snd-soc-dummy-dai";
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001909
1910 /* enable DPCM */
1911 link->dynamic = 1;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001912 link->dpcm_playback = le32_to_cpu(pcm->playback);
1913 link->dpcm_capture = le32_to_cpu(pcm->capture);
Mengdong Lin717a8e72016-11-03 01:03:34 +08001914 if (pcm->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001915 set_link_flags(link,
1916 le32_to_cpu(pcm->flag_mask),
1917 le32_to_cpu(pcm->flags));
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001918
Mengdong Linacfc7d42016-01-15 16:13:37 +08001919 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001920 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001921 if (ret < 0) {
1922 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
Bard liaob3718b82019-03-05 23:57:53 +08001923 kfree(link->name);
1924 kfree(link->stream_name);
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001925 kfree(link->cpus->dai_name);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001926 kfree(link);
1927 return ret;
1928 }
1929
1930 link->dobj.index = tplg->index;
1931 link->dobj.ops = tplg->ops;
1932 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1933 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1934
1935 snd_soc_add_dai_link(tplg->comp->card, link);
1936 return 0;
1937}
1938
1939/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001940static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1941 struct snd_soc_tplg_pcm *pcm)
1942{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001943 int ret;
1944
1945 ret = soc_tplg_dai_create(tplg, pcm);
1946 if (ret < 0)
1947 return ret;
1948
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001949 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001950}
1951
Mengdong Lin55726dc2016-11-03 01:00:16 +08001952/* copy stream caps from the old version 4 of source */
1953static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1954 struct snd_soc_tplg_stream_caps_v4 *src)
1955{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001956 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin55726dc2016-11-03 01:00:16 +08001957 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1958 dest->formats = src->formats;
1959 dest->rates = src->rates;
1960 dest->rate_min = src->rate_min;
1961 dest->rate_max = src->rate_max;
1962 dest->channels_min = src->channels_min;
1963 dest->channels_max = src->channels_max;
1964 dest->periods_min = src->periods_min;
1965 dest->periods_max = src->periods_max;
1966 dest->period_size_min = src->period_size_min;
1967 dest->period_size_max = src->period_size_max;
1968 dest->buffer_size_min = src->buffer_size_min;
1969 dest->buffer_size_max = src->buffer_size_max;
1970}
1971
1972/**
1973 * pcm_new_ver - Create the new version of PCM from the old version.
1974 * @tplg: topology context
1975 * @src: older version of pcm as a source
1976 * @pcm: latest version of pcm created from the source
1977 *
1978 * Support from vesion 4. User should free the returned pcm manually.
1979 */
1980static int pcm_new_ver(struct soc_tplg *tplg,
1981 struct snd_soc_tplg_pcm *src,
1982 struct snd_soc_tplg_pcm **pcm)
1983{
1984 struct snd_soc_tplg_pcm *dest;
1985 struct snd_soc_tplg_pcm_v4 *src_v4;
1986 int i;
1987
1988 *pcm = NULL;
1989
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001990 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001991 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1992 return -EINVAL;
1993 }
1994
1995 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1996 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1997 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1998 if (!dest)
1999 return -ENOMEM;
2000
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002001 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin55726dc2016-11-03 01:00:16 +08002002 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2003 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2004 dest->pcm_id = src_v4->pcm_id;
2005 dest->dai_id = src_v4->dai_id;
2006 dest->playback = src_v4->playback;
2007 dest->capture = src_v4->capture;
2008 dest->compress = src_v4->compress;
2009 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002010 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin55726dc2016-11-03 01:00:16 +08002011 memcpy(&dest->stream[i], &src_v4->stream[i],
2012 sizeof(struct snd_soc_tplg_stream));
2013
2014 for (i = 0; i < 2; i++)
2015 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2016
2017 *pcm = dest;
2018 return 0;
2019}
2020
Mengdong Lin64527e82016-01-15 16:13:28 +08002021static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01002022 struct snd_soc_tplg_hdr *hdr)
2023{
Mengdong Lin55726dc2016-11-03 01:00:16 +08002024 struct snd_soc_tplg_pcm *pcm, *_pcm;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002025 int count;
2026 int size;
Vinod Koulfd340452016-12-08 23:01:27 +05302027 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08002028 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01002029
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002030 count = le32_to_cpu(hdr->count);
2031
Liam Girdwood8a978232015-05-29 19:06:14 +01002032 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
2033 return 0;
2034
Mengdong Lin55726dc2016-11-03 01:00:16 +08002035 /* check the element size and count */
2036 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002037 size = le32_to_cpu(pcm->size);
2038 if (size > sizeof(struct snd_soc_tplg_pcm)
2039 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002040 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002041 size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002042 return -EINVAL;
2043 }
2044
Liam Girdwood8a978232015-05-29 19:06:14 +01002045 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002046 size, count,
2047 le32_to_cpu(hdr->payload_size),
2048 "PCM DAI")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002049 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2050 count);
2051 return -EINVAL;
2052 }
2053
Mengdong Lin64527e82016-01-15 16:13:28 +08002054 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002055 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002056 size = le32_to_cpu(pcm->size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002057
2058 /* check ABI version by size, create a new version of pcm
2059 * if abi not match.
2060 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002061 if (size == sizeof(*pcm)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002062 abi_match = true;
2063 _pcm = pcm;
2064 } else {
2065 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05302066 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002067 }
2068
Mengdong Lin55726dc2016-11-03 01:00:16 +08002069 /* create the FE DAIs and DAI links */
2070 soc_tplg_pcm_create(tplg, _pcm);
2071
Mengdong Lin717a8e72016-11-03 01:03:34 +08002072 /* offset by version-specific struct size and
2073 * real priv data size
2074 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002075 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
Mengdong Lin717a8e72016-11-03 01:03:34 +08002076
Mengdong Lin55726dc2016-11-03 01:00:16 +08002077 if (!abi_match)
2078 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08002079 }
2080
Liam Girdwood8a978232015-05-29 19:06:14 +01002081 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002082
Liam Girdwood8a978232015-05-29 19:06:14 +01002083 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002084}
2085
Mengdong Lin593d9e52016-11-03 01:04:27 +08002086/**
2087 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002088 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002089 * @cfg: physical link configs.
2090 *
2091 * Topology context contains a list of supported HW formats (configs) and
2092 * a default format ID for the physical link. This function will use this
2093 * default ID to choose the HW format to set the link's DAI format for init.
2094 */
2095static void set_link_hw_format(struct snd_soc_dai_link *link,
2096 struct snd_soc_tplg_link_config *cfg)
2097{
2098 struct snd_soc_tplg_hw_config *hw_config;
2099 unsigned char bclk_master, fsync_master;
2100 unsigned char invert_bclk, invert_fsync;
2101 int i;
2102
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002103 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002104 hw_config = &cfg->hw_config[i];
2105 if (hw_config->id != cfg->default_hw_config_id)
2106 continue;
2107
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002108 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2109 SND_SOC_DAIFMT_FORMAT_MASK;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002110
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002111 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002112 switch (hw_config->clock_gated) {
2113 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002114 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002115 break;
2116
2117 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002118 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002119 break;
2120
2121 default:
2122 /* ignore the value */
2123 break;
2124 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002125
Mengdong Lin593d9e52016-11-03 01:04:27 +08002126 /* clock signal polarity */
2127 invert_bclk = hw_config->invert_bclk;
2128 invert_fsync = hw_config->invert_fsync;
2129 if (!invert_bclk && !invert_fsync)
2130 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2131 else if (!invert_bclk && invert_fsync)
2132 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2133 else if (invert_bclk && !invert_fsync)
2134 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2135 else
2136 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2137
2138 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002139 bclk_master = (hw_config->bclk_master ==
2140 SND_SOC_TPLG_BCLK_CM);
2141 fsync_master = (hw_config->fsync_master ==
2142 SND_SOC_TPLG_FSYNC_CM);
2143 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002144 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002145 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002146 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2147 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002148 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2149 else
2150 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2151 }
2152}
2153
2154/**
2155 * link_new_ver - Create a new physical link config from the old
2156 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002157 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002158 * @src: old version of phyical link config as a source
2159 * @link: latest version of physical link config created from the source
2160 *
2161 * Support from vesion 4. User need free the returned link config manually.
2162 */
2163static int link_new_ver(struct soc_tplg *tplg,
2164 struct snd_soc_tplg_link_config *src,
2165 struct snd_soc_tplg_link_config **link)
2166{
2167 struct snd_soc_tplg_link_config *dest;
2168 struct snd_soc_tplg_link_config_v4 *src_v4;
2169 int i;
2170
2171 *link = NULL;
2172
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002173 if (le32_to_cpu(src->size) !=
2174 sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002175 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2176 return -EINVAL;
2177 }
2178
2179 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2180
2181 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2182 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2183 if (!dest)
2184 return -ENOMEM;
2185
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002186 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002187 dest->id = src_v4->id;
2188 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002189 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002190 memcpy(&dest->stream[i], &src_v4->stream[i],
2191 sizeof(struct snd_soc_tplg_stream));
2192
2193 *link = dest;
2194 return 0;
2195}
2196
2197/* Find and configure an existing physical DAI link */
2198static int soc_tplg_link_config(struct soc_tplg *tplg,
2199 struct snd_soc_tplg_link_config *cfg)
2200{
2201 struct snd_soc_dai_link *link;
2202 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002203 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002204 int ret;
2205
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002206 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2207 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2208 return -EINVAL;
2209 else if (len)
2210 name = cfg->name;
2211 else
2212 name = NULL;
2213
2214 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2215 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2216 return -EINVAL;
2217 else if (len)
2218 stream_name = cfg->stream_name;
2219 else
2220 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002221
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002222 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
Mengdong Lin593d9e52016-11-03 01:04:27 +08002223 name, stream_name);
2224 if (!link) {
2225 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2226 name, cfg->id);
2227 return -EINVAL;
2228 }
2229
2230 /* hw format */
2231 if (cfg->num_hw_configs)
2232 set_link_hw_format(link, cfg);
2233
2234 /* flags */
2235 if (cfg->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002236 set_link_flags(link,
2237 le32_to_cpu(cfg->flag_mask),
2238 le32_to_cpu(cfg->flags));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002239
2240 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002241 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002242 if (ret < 0) {
2243 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2244 return ret;
2245 }
2246
Bard liaoadfebb52019-02-01 11:07:40 -06002247 /* for unloading it in snd_soc_tplg_component_remove */
2248 link->dobj.index = tplg->index;
2249 link->dobj.ops = tplg->ops;
2250 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2251 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2252
Mengdong Lin593d9e52016-11-03 01:04:27 +08002253 return 0;
2254}
2255
2256
2257/* Load physical link config elements from the topology context */
2258static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2259 struct snd_soc_tplg_hdr *hdr)
2260{
2261 struct snd_soc_tplg_link_config *link, *_link;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002262 int count;
2263 int size;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002264 int i, ret;
2265 bool abi_match;
2266
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002267 count = le32_to_cpu(hdr->count);
2268
Mengdong Lin593d9e52016-11-03 01:04:27 +08002269 if (tplg->pass != SOC_TPLG_PASS_LINK) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002270 tplg->pos += le32_to_cpu(hdr->size) +
2271 le32_to_cpu(hdr->payload_size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002272 return 0;
2273 };
2274
2275 /* check the element size and count */
2276 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002277 size = le32_to_cpu(link->size);
2278 if (size > sizeof(struct snd_soc_tplg_link_config)
2279 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002280 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002281 size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002282 return -EINVAL;
2283 }
2284
2285 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002286 size, count,
2287 le32_to_cpu(hdr->payload_size),
2288 "physical link config")) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002289 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2290 count);
2291 return -EINVAL;
2292 }
2293
2294 /* config physical DAI links */
2295 for (i = 0; i < count; i++) {
2296 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002297 size = le32_to_cpu(link->size);
2298 if (size == sizeof(*link)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002299 abi_match = true;
2300 _link = link;
2301 } else {
2302 abi_match = false;
2303 ret = link_new_ver(tplg, link, &_link);
2304 if (ret < 0)
2305 return ret;
2306 }
2307
2308 ret = soc_tplg_link_config(tplg, _link);
2309 if (ret < 0)
2310 return ret;
2311
2312 /* offset by version-specific struct size and
2313 * real priv data size
2314 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002315 tplg->pos += size + le32_to_cpu(_link->priv.size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002316
2317 if (!abi_match)
2318 kfree(_link); /* free the duplicated one */
2319 }
2320
2321 return 0;
2322}
2323
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002324/**
2325 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002326 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002327 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002328 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002329 * The physical dai should already be registered by the platform driver.
2330 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002331 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002332static int soc_tplg_dai_config(struct soc_tplg *tplg,
2333 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002334{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002335 struct snd_soc_dai_link_component dai_component;
Mengdong Lin0038be92016-07-26 14:32:37 +08002336 struct snd_soc_dai *dai;
2337 struct snd_soc_dai_driver *dai_drv;
2338 struct snd_soc_pcm_stream *stream;
2339 struct snd_soc_tplg_stream_caps *caps;
2340 int ret;
2341
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002342 memset(&dai_component, 0, sizeof(dai_component));
2343
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002344 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002345 dai = snd_soc_find_dai(&dai_component);
2346 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002347 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2348 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002349 return -EINVAL;
2350 }
2351
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002352 if (le32_to_cpu(d->dai_id) != dai->id) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002353 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2354 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002355 return -EINVAL;
2356 }
2357
2358 dai_drv = dai->driver;
2359 if (!dai_drv)
2360 return -EINVAL;
2361
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002362 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002363 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002364 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002365 set_stream_info(stream, caps);
2366 }
2367
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002368 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002369 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002370 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002371 set_stream_info(stream, caps);
2372 }
2373
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002374 if (d->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002375 set_dai_flags(dai_drv,
2376 le32_to_cpu(d->flag_mask),
2377 le32_to_cpu(d->flags));
Mengdong Lin0038be92016-07-26 14:32:37 +08002378
2379 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002380 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002381 if (ret < 0) {
2382 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2383 return ret;
2384 }
2385
2386 return 0;
2387}
2388
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002389/* load physical DAI elements */
2390static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2391 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002392{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002393 struct snd_soc_tplg_dai *dai;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002394 int count;
Mengdong Lin0038be92016-07-26 14:32:37 +08002395 int i;
2396
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002397 count = le32_to_cpu(hdr->count);
2398
Mengdong Lin0038be92016-07-26 14:32:37 +08002399 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2400 return 0;
2401
2402 /* config the existing BE DAIs */
2403 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002404 dai = (struct snd_soc_tplg_dai *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002405 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002406 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002407 return -EINVAL;
2408 }
2409
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002410 soc_tplg_dai_config(tplg, dai);
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002411 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
Mengdong Lin0038be92016-07-26 14:32:37 +08002412 }
2413
2414 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2415 return 0;
2416}
2417
Mengdong Lin583958f2016-10-11 14:36:42 +08002418/**
2419 * manifest_new_ver - Create a new version of manifest from the old version
2420 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002421 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002422 * @src: old version of manifest as a source
2423 * @manifest: latest version of manifest created from the source
2424 *
2425 * Support from vesion 4. Users need free the returned manifest manually.
2426 */
2427static int manifest_new_ver(struct soc_tplg *tplg,
2428 struct snd_soc_tplg_manifest *src,
2429 struct snd_soc_tplg_manifest **manifest)
2430{
2431 struct snd_soc_tplg_manifest *dest;
2432 struct snd_soc_tplg_manifest_v4 *src_v4;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002433 int size;
Mengdong Lin583958f2016-10-11 14:36:42 +08002434
2435 *manifest = NULL;
2436
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002437 size = le32_to_cpu(src->size);
2438 if (size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002439 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002440 size);
2441 if (size)
Guenter Roeckac9391d2018-05-24 12:49:21 -07002442 return -EINVAL;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002443 src->size = cpu_to_le32(sizeof(*src_v4));
Mengdong Lin583958f2016-10-11 14:36:42 +08002444 }
2445
2446 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2447
2448 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002449 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2450 GFP_KERNEL);
Mengdong Lin583958f2016-10-11 14:36:42 +08002451 if (!dest)
2452 return -ENOMEM;
2453
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002454 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin583958f2016-10-11 14:36:42 +08002455 dest->control_elems = src_v4->control_elems;
2456 dest->widget_elems = src_v4->widget_elems;
2457 dest->graph_elems = src_v4->graph_elems;
2458 dest->pcm_elems = src_v4->pcm_elems;
2459 dest->dai_link_elems = src_v4->dai_link_elems;
2460 dest->priv.size = src_v4->priv.size;
2461 if (dest->priv.size)
2462 memcpy(dest->priv.data, src_v4->priv.data,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002463 le32_to_cpu(src_v4->priv.size));
Mengdong Lin583958f2016-10-11 14:36:42 +08002464
2465 *manifest = dest;
2466 return 0;
2467}
Mengdong Lin0038be92016-07-26 14:32:37 +08002468
Liam Girdwood8a978232015-05-29 19:06:14 +01002469static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002470 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002471{
Mengdong Lin583958f2016-10-11 14:36:42 +08002472 struct snd_soc_tplg_manifest *manifest, *_manifest;
2473 bool abi_match;
2474 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002475
2476 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2477 return 0;
2478
2479 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002480
2481 /* check ABI version by size, create a new manifest if abi not match */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002482 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
Mengdong Lin583958f2016-10-11 14:36:42 +08002483 abi_match = true;
2484 _manifest = manifest;
2485 } else {
2486 abi_match = false;
2487 err = manifest_new_ver(tplg, manifest, &_manifest);
2488 if (err < 0)
2489 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002490 }
2491
Mengdong Lin583958f2016-10-11 14:36:42 +08002492 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002493 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002494 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002495
Mengdong Lin583958f2016-10-11 14:36:42 +08002496 if (!abi_match) /* free the duplicated one */
2497 kfree(_manifest);
2498
Liam Girdwood8a978232015-05-29 19:06:14 +01002499 return 0;
2500}
2501
2502/* validate header magic, size and type */
2503static int soc_valid_header(struct soc_tplg *tplg,
2504 struct snd_soc_tplg_hdr *hdr)
2505{
2506 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2507 return 0;
2508
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002509 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002510 dev_err(tplg->dev,
2511 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002512 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002513 tplg->fw->size);
2514 return -EINVAL;
2515 }
2516
Liam Girdwood8a978232015-05-29 19:06:14 +01002517 /* big endian firmware objects not supported atm */
Pierre-Louis Bossart21141712019-04-04 14:13:58 -05002518 if (hdr->magic == SOC_TPLG_MAGIC_BIG_ENDIAN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002519 dev_err(tplg->dev,
2520 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2521 tplg->pass, hdr->magic,
2522 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2523 return -EINVAL;
2524 }
2525
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002526 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002527 dev_err(tplg->dev,
2528 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2529 tplg->pass, hdr->magic,
2530 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2531 return -EINVAL;
2532 }
2533
Mengdong Lin288b8da2016-11-03 01:03:17 +08002534 /* Support ABI from version 4 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002535 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2536 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002537 dev_err(tplg->dev,
2538 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2539 tplg->pass, hdr->abi,
2540 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2541 tplg->fw->size);
2542 return -EINVAL;
2543 }
2544
2545 if (hdr->payload_size == 0) {
2546 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2547 soc_tplg_get_hdr_offset(tplg));
2548 return -EINVAL;
2549 }
2550
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002551 if (tplg->pass == le32_to_cpu(hdr->type))
Liam Girdwood8a978232015-05-29 19:06:14 +01002552 dev_dbg(tplg->dev,
2553 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2554 hdr->payload_size, hdr->type, hdr->version,
2555 hdr->vendor_type, tplg->pass);
2556
2557 return 1;
2558}
2559
2560/* check header type and call appropriate handler */
2561static int soc_tplg_load_header(struct soc_tplg *tplg,
2562 struct snd_soc_tplg_hdr *hdr)
2563{
2564 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2565
2566 /* check for matching ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002567 if (le32_to_cpu(hdr->index) != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002568 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002569 return 0;
2570
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002571 tplg->index = le32_to_cpu(hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01002572
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002573 switch (le32_to_cpu(hdr->type)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002574 case SND_SOC_TPLG_TYPE_MIXER:
2575 case SND_SOC_TPLG_TYPE_ENUM:
2576 case SND_SOC_TPLG_TYPE_BYTES:
2577 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2578 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2579 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2580 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2581 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2582 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002583 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002584 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002585 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002586 case SND_SOC_TPLG_TYPE_DAI_LINK:
2587 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2588 /* physical link configurations */
2589 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002590 case SND_SOC_TPLG_TYPE_MANIFEST:
2591 return soc_tplg_manifest_load(tplg, hdr);
2592 default:
2593 /* bespoke vendor data object */
2594 return soc_tplg_vendor_load(tplg, hdr);
2595 }
2596
2597 return 0;
2598}
2599
2600/* process the topology file headers */
2601static int soc_tplg_process_headers(struct soc_tplg *tplg)
2602{
2603 struct snd_soc_tplg_hdr *hdr;
2604 int ret;
2605
2606 tplg->pass = SOC_TPLG_PASS_START;
2607
2608 /* process the header types from start to end */
2609 while (tplg->pass <= SOC_TPLG_PASS_END) {
2610
2611 tplg->hdr_pos = tplg->fw->data;
2612 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2613
2614 while (!soc_tplg_is_eof(tplg)) {
2615
2616 /* make sure header is valid before loading */
2617 ret = soc_valid_header(tplg, hdr);
2618 if (ret < 0)
2619 return ret;
2620 else if (ret == 0)
2621 break;
2622
2623 /* load the header object */
2624 ret = soc_tplg_load_header(tplg, hdr);
2625 if (ret < 0)
2626 return ret;
2627
2628 /* goto next header */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002629 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
Liam Girdwood8a978232015-05-29 19:06:14 +01002630 sizeof(struct snd_soc_tplg_hdr);
2631 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2632 }
2633
2634 /* next data type pass */
2635 tplg->pass++;
2636 }
2637
2638 /* signal DAPM we are complete */
2639 ret = soc_tplg_dapm_complete(tplg);
2640 if (ret < 0)
2641 dev_err(tplg->dev,
2642 "ASoC: failed to initialise DAPM from Firmware\n");
2643
2644 return ret;
2645}
2646
2647static int soc_tplg_load(struct soc_tplg *tplg)
2648{
2649 int ret;
2650
2651 ret = soc_tplg_process_headers(tplg);
2652 if (ret == 0)
2653 soc_tplg_complete(tplg);
2654
2655 return ret;
2656}
2657
2658/* load audio component topology from "firmware" file */
2659int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2660 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2661{
2662 struct soc_tplg tplg;
Bard liao304017d2019-02-17 21:23:47 +08002663 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002664
2665 /* setup parsing context */
2666 memset(&tplg, 0, sizeof(tplg));
2667 tplg.fw = fw;
2668 tplg.dev = comp->dev;
2669 tplg.comp = comp;
2670 tplg.ops = ops;
2671 tplg.req_index = id;
2672 tplg.io_ops = ops->io_ops;
2673 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002674 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2675 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002676
Bard liao304017d2019-02-17 21:23:47 +08002677 ret = soc_tplg_load(&tplg);
2678 /* free the created components if fail to load topology */
2679 if (ret)
2680 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2681
2682 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002683}
2684EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2685
2686/* remove this dynamic widget */
2687void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2688{
2689 /* make sure we are a widget */
2690 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2691 return;
2692
2693 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2694}
2695EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2696
2697/* remove all dynamic widgets from this DAPM context */
2698void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2699 u32 index)
2700{
2701 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002702
2703 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2704
2705 /* make sure we are a widget with correct context */
2706 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2707 continue;
2708
2709 /* match ID */
2710 if (w->dobj.index != index &&
2711 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2712 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002713 /* check and free and dynamic widget kcontrols */
2714 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002715 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002716 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002717 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002718}
2719EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2720
2721/* remove dynamic controls from the component driver */
2722int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2723{
2724 struct snd_soc_dobj *dobj, *next_dobj;
2725 int pass = SOC_TPLG_PASS_END;
2726
2727 /* process the header types from end to start */
2728 while (pass >= SOC_TPLG_PASS_START) {
2729
2730 /* remove mixer controls */
2731 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2732 list) {
2733
2734 /* match index */
2735 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002736 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002737 continue;
2738
2739 switch (dobj->type) {
2740 case SND_SOC_DOBJ_MIXER:
2741 remove_mixer(comp, dobj, pass);
2742 break;
2743 case SND_SOC_DOBJ_ENUM:
2744 remove_enum(comp, dobj, pass);
2745 break;
2746 case SND_SOC_DOBJ_BYTES:
2747 remove_bytes(comp, dobj, pass);
2748 break;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06002749 case SND_SOC_DOBJ_GRAPH:
2750 remove_route(comp, dobj, pass);
2751 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002752 case SND_SOC_DOBJ_WIDGET:
2753 remove_widget(comp, dobj, pass);
2754 break;
2755 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002756 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002757 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002758 case SND_SOC_DOBJ_DAI_LINK:
2759 remove_link(comp, dobj, pass);
2760 break;
Bard liaoadfebb52019-02-01 11:07:40 -06002761 case SND_SOC_DOBJ_BACKEND_LINK:
2762 /*
2763 * call link_unload ops if extra
2764 * deinitialization is needed.
2765 */
2766 remove_backend_link(comp, dobj, pass);
2767 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002768 default:
2769 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2770 dobj->type);
2771 break;
2772 }
2773 }
2774 pass--;
2775 }
2776
2777 /* let caller know if FW can be freed when no objects are left */
2778 return !list_empty(&comp->dobj_list);
2779}
2780EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);