blob: c25939c5611b917ee9462cc50a473a40d8e59291 [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);
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +020089static void soc_tplg_denum_remove_texts(struct soc_enum *se);
90static void soc_tplg_denum_remove_values(struct soc_enum *se);
Liam Girdwood8a978232015-05-29 19:06:14 +010091
92/* check we dont overflow the data for this control chunk */
93static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
94 unsigned int count, size_t bytes, const char *elem_type)
95{
96 const u8 *end = tplg->pos + elem_size * count;
97
98 if (end > tplg->fw->data + tplg->fw->size) {
99 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
100 elem_type);
101 return -EINVAL;
102 }
103
104 /* check there is enough room in chunk for control.
105 extra bytes at the end of control are for vendor data here */
106 if (elem_size * count > bytes) {
107 dev_err(tplg->dev,
108 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
109 elem_type, count, elem_size, bytes);
110 return -EINVAL;
111 }
112
113 return 0;
114}
115
116static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
117{
118 const u8 *end = tplg->hdr_pos;
119
120 if (end >= tplg->fw->data + tplg->fw->size)
121 return 1;
122 return 0;
123}
124
125static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
126{
127 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
128}
129
130static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
131{
132 return (unsigned long)(tplg->pos - tplg->fw->data);
133}
134
135/* mapping of Kcontrol types and associated operations. */
136static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
137 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
138 snd_soc_put_volsw, snd_soc_info_volsw},
139 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
140 snd_soc_put_volsw_sx, NULL},
141 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
142 snd_soc_put_enum_double, snd_soc_info_enum_double},
143 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
144 snd_soc_put_enum_double, NULL},
145 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
146 snd_soc_bytes_put, snd_soc_bytes_info},
147 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
148 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
149 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
150 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
151 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
152 snd_soc_put_strobe, NULL},
153 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530154 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100155 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
156 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
157 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
158 snd_soc_dapm_put_enum_double, NULL},
159 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
160 snd_soc_dapm_put_enum_double, NULL},
161 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
162 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
163};
164
165struct soc_tplg_map {
166 int uid;
167 int kid;
168};
169
170/* mapping of widget types from UAPI IDs to kernel IDs */
171static const struct soc_tplg_map dapm_map[] = {
172 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
173 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
174 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
175 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
176 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
177 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
178 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
179 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
180 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
181 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
182 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
183 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
184 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
185 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
186 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
187 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
Liam Girdwood8a70b452017-06-29 14:22:24 +0100188 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
189 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
190 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
191 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
192 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
193 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
194 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
195 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
Liam Girdwood8a978232015-05-29 19:06:14 +0100196};
197
198static int tplc_chan_get_reg(struct soc_tplg *tplg,
199 struct snd_soc_tplg_channel *chan, int map)
200{
201 int i;
202
203 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500204 if (le32_to_cpu(chan[i].id) == map)
205 return le32_to_cpu(chan[i].reg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100206 }
207
208 return -EINVAL;
209}
210
211static int tplc_chan_get_shift(struct soc_tplg *tplg,
212 struct snd_soc_tplg_channel *chan, int map)
213{
214 int i;
215
216 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500217 if (le32_to_cpu(chan[i].id) == map)
218 return le32_to_cpu(chan[i].shift);
Liam Girdwood8a978232015-05-29 19:06:14 +0100219 }
220
221 return -EINVAL;
222}
223
224static int get_widget_id(int tplg_type)
225{
226 int i;
227
228 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
229 if (tplg_type == dapm_map[i].uid)
230 return dapm_map[i].kid;
231 }
232
233 return -EINVAL;
234}
235
Liam Girdwood8a978232015-05-29 19:06:14 +0100236static inline void soc_bind_err(struct soc_tplg *tplg,
237 struct snd_soc_tplg_ctl_hdr *hdr, int index)
238{
239 dev_err(tplg->dev,
240 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
241 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
242 soc_tplg_get_offset(tplg));
243}
244
245static inline void soc_control_err(struct soc_tplg *tplg,
246 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
247{
248 dev_err(tplg->dev,
249 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
250 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
251 soc_tplg_get_offset(tplg));
252}
253
254/* pass vendor data to component driver for processing */
255static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
256 struct snd_soc_tplg_hdr *hdr)
257{
258 int ret = 0;
259
260 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100261 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100262 else {
263 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
264 hdr->vendor_type);
265 return -EINVAL;
266 }
267
268 if (ret < 0)
269 dev_err(tplg->dev,
270 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
271 soc_tplg_get_hdr_offset(tplg),
272 soc_tplg_get_hdr_offset(tplg),
273 hdr->type, hdr->vendor_type);
274 return ret;
275}
276
277/* pass vendor data to component driver for processing */
278static int soc_tplg_vendor_load(struct soc_tplg *tplg,
279 struct snd_soc_tplg_hdr *hdr)
280{
281 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
282 return 0;
283
284 return soc_tplg_vendor_load_(tplg, hdr);
285}
286
287/* optionally pass new dynamic widget to component driver. This is mainly for
288 * external widgets where we can assign private data/ops */
289static int soc_tplg_widget_load(struct soc_tplg *tplg,
290 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
291{
292 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100293 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
294 tplg_w);
Liam Girdwood8a978232015-05-29 19:06:14 +0100295
296 return 0;
297}
298
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100299/* optionally pass new dynamic widget to component driver. This is mainly for
300 * external widgets where we can assign private data/ops */
301static int soc_tplg_widget_ready(struct soc_tplg *tplg,
302 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
303{
304 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100305 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
306 tplg_w);
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100307
308 return 0;
309}
310
Masahiro Yamada183b8022017-02-27 14:29:20 -0800311/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800312static int soc_tplg_dai_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100313 struct snd_soc_dai_driver *dai_drv,
314 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
Liam Girdwood8a978232015-05-29 19:06:14 +0100315{
Mengdong Lin64527e82016-01-15 16:13:28 +0800316 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100317 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
318 pcm, dai);
Liam Girdwood8a978232015-05-29 19:06:14 +0100319
320 return 0;
321}
322
Masahiro Yamada183b8022017-02-27 14:29:20 -0800323/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800324static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100325 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
Mengdong Linacfc7d42016-01-15 16:13:37 +0800326{
327 if (tplg->comp && tplg->ops && tplg->ops->link_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100328 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800329
330 return 0;
331}
332
Liam Girdwood8a978232015-05-29 19:06:14 +0100333/* tell the component driver that all firmware has been loaded in this request */
334static void soc_tplg_complete(struct soc_tplg *tplg)
335{
336 if (tplg->comp && tplg->ops && tplg->ops->complete)
337 tplg->ops->complete(tplg->comp);
338}
339
340/* add a dynamic kcontrol */
341static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
342 const struct snd_kcontrol_new *control_new, const char *prefix,
343 void *data, struct snd_kcontrol **kcontrol)
344{
345 int err;
346
347 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
348 if (*kcontrol == NULL) {
349 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
350 control_new->name);
351 return -ENOMEM;
352 }
353
354 err = snd_ctl_add(card, *kcontrol);
355 if (err < 0) {
356 dev_err(dev, "ASoC: Failed to add %s: %d\n",
357 control_new->name, err);
358 return err;
359 }
360
361 return 0;
362}
363
364/* add a dynamic kcontrol for component driver */
365static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
366 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
367{
368 struct snd_soc_component *comp = tplg->comp;
369
370 return soc_tplg_add_dcontrol(comp->card->snd_card,
371 comp->dev, k, NULL, comp, kcontrol);
372}
373
374/* remove a mixer kcontrol */
375static void remove_mixer(struct snd_soc_component *comp,
376 struct snd_soc_dobj *dobj, int pass)
377{
378 struct snd_card *card = comp->card->snd_card;
379 struct soc_mixer_control *sm =
380 container_of(dobj, struct soc_mixer_control, dobj);
381 const unsigned int *p = NULL;
382
383 if (pass != SOC_TPLG_PASS_MIXER)
384 return;
385
386 if (dobj->ops && dobj->ops->control_unload)
387 dobj->ops->control_unload(comp, dobj);
388
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600389 if (dobj->control.kcontrol->tlv.p)
390 p = dobj->control.kcontrol->tlv.p;
391 snd_ctl_remove(card, dobj->control.kcontrol);
392 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100393 kfree(sm);
394 kfree(p);
395}
396
397/* remove an enum kcontrol */
398static void remove_enum(struct snd_soc_component *comp,
399 struct snd_soc_dobj *dobj, int pass)
400{
401 struct snd_card *card = comp->card->snd_card;
402 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100403
404 if (pass != SOC_TPLG_PASS_MIXER)
405 return;
406
407 if (dobj->ops && dobj->ops->control_unload)
408 dobj->ops->control_unload(comp, dobj);
409
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600410 snd_ctl_remove(card, dobj->control.kcontrol);
411 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100412
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200413 soc_tplg_denum_remove_values(se);
414 soc_tplg_denum_remove_texts(se);
Liam Girdwood8a978232015-05-29 19:06:14 +0100415 kfree(se);
416}
417
418/* remove a byte kcontrol */
419static void remove_bytes(struct snd_soc_component *comp,
420 struct snd_soc_dobj *dobj, int pass)
421{
422 struct snd_card *card = comp->card->snd_card;
423 struct soc_bytes_ext *sb =
424 container_of(dobj, struct soc_bytes_ext, dobj);
425
426 if (pass != SOC_TPLG_PASS_MIXER)
427 return;
428
429 if (dobj->ops && dobj->ops->control_unload)
430 dobj->ops->control_unload(comp, dobj);
431
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600432 snd_ctl_remove(card, dobj->control.kcontrol);
433 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100434 kfree(sb);
435}
436
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -0600437/* remove a route */
438static void remove_route(struct snd_soc_component *comp,
439 struct snd_soc_dobj *dobj, int pass)
440{
441 struct snd_soc_dapm_route *route =
442 container_of(dobj, struct snd_soc_dapm_route, dobj);
443
444 if (pass != SOC_TPLG_PASS_GRAPH)
445 return;
446
447 if (dobj->ops && dobj->ops->dapm_route_unload)
448 dobj->ops->dapm_route_unload(comp, dobj);
449
450 list_del(&dobj->list);
451 kfree(route);
452}
453
Liam Girdwood8a978232015-05-29 19:06:14 +0100454/* remove a widget and it's kcontrols - routes must be removed first */
455static void remove_widget(struct snd_soc_component *comp,
456 struct snd_soc_dobj *dobj, int pass)
457{
458 struct snd_card *card = comp->card->snd_card;
459 struct snd_soc_dapm_widget *w =
460 container_of(dobj, struct snd_soc_dapm_widget, dobj);
461 int i;
462
463 if (pass != SOC_TPLG_PASS_WIDGET)
464 return;
465
466 if (dobj->ops && dobj->ops->widget_unload)
467 dobj->ops->widget_unload(comp, dobj);
468
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000469 if (!w->kcontrols)
470 goto free_news;
471
Liam Girdwood8a978232015-05-29 19:06:14 +0100472 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800473 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100474 * The enum may either have an array of values or strings.
475 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800476 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100477 /* enumerated widget mixer */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100478 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800479 struct snd_kcontrol *kcontrol = w->kcontrols[i];
480 struct soc_enum *se =
481 (struct soc_enum *)kcontrol->private_value;
Liam Girdwood8a978232015-05-29 19:06:14 +0100482
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800483 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100484
Ranjani Sridharan54f88442019-04-04 19:48:33 -0700485 /* free enum kcontrol's dvalues and dtexts */
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200486 soc_tplg_denum_remove_values(se);
487 soc_tplg_denum_remove_texts(se);
Liam Girdwood8a978232015-05-29 19:06:14 +0100488
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800489 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100490 kfree(w->kcontrol_news[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800491 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100492 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800493 /* volume mixer or bytes controls */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100494 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100495 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100496
Mengdong Lineea3dd42016-11-25 16:09:17 +0800497 if (dobj->widget.kcontrol_type
498 == SND_SOC_TPLG_TYPE_MIXER)
499 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100500
Mengdong Lineea3dd42016-11-25 16:09:17 +0800501 /* Private value is used as struct soc_mixer_control
502 * for volume mixers or soc_bytes_ext for bytes
503 * controls.
504 */
505 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000506 snd_ctl_remove(card, kcontrol);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100507 kfree(w->kcontrol_news[i].name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100508 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100509 }
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000510
511free_news:
512 kfree(w->kcontrol_news);
513
Amadeusz Sławińskia46e8392019-01-25 14:06:43 -0600514 list_del(&dobj->list);
515
Liam Girdwood8a978232015-05-29 19:06:14 +0100516 /* widget w is freed by soc-dapm.c */
517}
518
Mengdong Lin64527e82016-01-15 16:13:28 +0800519/* remove DAI configurations */
520static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100521 struct snd_soc_dobj *dobj, int pass)
522{
Mengdong Lin64527e82016-01-15 16:13:28 +0800523 struct snd_soc_dai_driver *dai_drv =
524 container_of(dobj, struct snd_soc_dai_driver, dobj);
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600525 struct snd_soc_dai *dai;
Mengdong Lin64527e82016-01-15 16:13:28 +0800526
Liam Girdwood8a978232015-05-29 19:06:14 +0100527 if (pass != SOC_TPLG_PASS_PCM_DAI)
528 return;
529
Mengdong Lin64527e82016-01-15 16:13:28 +0800530 if (dobj->ops && dobj->ops->dai_unload)
531 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100532
Kuninori Morimoto43ca5da2019-08-20 14:05:32 +0900533 for_each_component_dais(comp, dai)
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600534 if (dai->driver == dai_drv)
535 dai->driver = NULL;
536
Bard liao7b6f68a2019-03-05 23:57:52 +0800537 kfree(dai_drv->playback.stream_name);
538 kfree(dai_drv->capture.stream_name);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800539 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100540 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800541 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100542}
543
Mengdong Linacfc7d42016-01-15 16:13:37 +0800544/* remove link configurations */
545static void remove_link(struct snd_soc_component *comp,
546 struct snd_soc_dobj *dobj, int pass)
547{
548 struct snd_soc_dai_link *link =
549 container_of(dobj, struct snd_soc_dai_link, dobj);
550
551 if (pass != SOC_TPLG_PASS_PCM_DAI)
552 return;
553
554 if (dobj->ops && dobj->ops->link_unload)
555 dobj->ops->link_unload(comp, dobj);
556
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800557 kfree(link->name);
558 kfree(link->stream_name);
Kuninori Morimoto23b946c2019-06-06 13:19:14 +0900559 kfree(link->cpus->dai_name);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800560
Mengdong Linacfc7d42016-01-15 16:13:37 +0800561 list_del(&dobj->list);
562 snd_soc_remove_dai_link(comp->card, link);
563 kfree(link);
564}
565
Bard liaoadfebb52019-02-01 11:07:40 -0600566/* unload dai link */
567static void remove_backend_link(struct snd_soc_component *comp,
568 struct snd_soc_dobj *dobj, int pass)
569{
570 if (pass != SOC_TPLG_PASS_LINK)
571 return;
572
573 if (dobj->ops && dobj->ops->link_unload)
574 dobj->ops->link_unload(comp, dobj);
575
576 /*
577 * We don't free the link here as what remove_link() do since BE
578 * links are not allocated by topology.
579 * We however need to reset the dobj type to its initial values
580 */
581 dobj->type = SND_SOC_DOBJ_NONE;
582 list_del(&dobj->list);
583}
584
Liam Girdwood8a978232015-05-29 19:06:14 +0100585/* bind a kcontrol to it's IO handlers */
586static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
587 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800588 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100589{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800590 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800591 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800592 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100593
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500594 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800595 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
596 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
597 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
598 struct soc_bytes_ext *sbe;
599 struct snd_soc_tplg_bytes_control *be;
600
601 sbe = (struct soc_bytes_ext *)k->private_value;
602 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
603
604 /* TLV bytes controls need standard kcontrol info handler,
605 * TLV callback and extended put/get handlers.
606 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530607 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800608 k->tlv.c = snd_soc_bytes_tlv_callback;
609
610 ext_ops = tplg->bytes_ext_ops;
611 num_ops = tplg->bytes_ext_ops_count;
612 for (i = 0; i < num_ops; i++) {
613 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
614 sbe->put = ext_ops[i].put;
615 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
616 sbe->get = ext_ops[i].get;
617 }
618
619 if (sbe->put && sbe->get)
620 return 0;
621 else
622 return -EINVAL;
623 }
624
Mengdong Lin88a17d82015-08-18 18:11:51 +0800625 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800626 ops = tplg->io_ops;
627 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100628 for (i = 0; i < num_ops; i++) {
629
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800630 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100631 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800632 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100633 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800634 if (k->info == NULL && ops[i].id == hdr->ops.info)
635 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100636 }
637
Mengdong Lin88a17d82015-08-18 18:11:51 +0800638 /* vendor specific handlers found ? */
639 if (k->put && k->get && k->info)
640 return 0;
641
642 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800643 ops = io_ops;
644 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800645 for (i = 0; i < num_ops; i++) {
646
647 if (k->put == NULL && ops[i].id == hdr->ops.put)
648 k->put = ops[i].put;
649 if (k->get == NULL && ops[i].id == hdr->ops.get)
650 k->get = ops[i].get;
651 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100652 k->info = ops[i].info;
653 }
654
655 /* standard handlers found ? */
656 if (k->put && k->get && k->info)
657 return 0;
658
Liam Girdwood8a978232015-05-29 19:06:14 +0100659 /* nothing to bind */
660 return -EINVAL;
661}
662
663/* bind a widgets to it's evnt handlers */
664int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
665 const struct snd_soc_tplg_widget_events *events,
666 int num_events, u16 event_type)
667{
668 int i;
669
670 w->event = NULL;
671
672 for (i = 0; i < num_events; i++) {
673 if (event_type == events[i].type) {
674
675 /* found - so assign event */
676 w->event = events[i].event_handler;
677 return 0;
678 }
679 }
680
681 /* not found */
682 return -EINVAL;
683}
684EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
685
686/* optionally pass new dynamic kcontrol to component driver. */
687static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
688 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
689{
690 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100691 return tplg->ops->control_load(tplg->comp, tplg->index, k,
692 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100693
694 return 0;
695}
696
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100697
698static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
699 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100700{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100701 unsigned int item_len = 2 * sizeof(unsigned int);
702 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100703
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100704 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
705 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100706 return -ENOMEM;
707
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100708 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
709 p[1] = item_len;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500710 p[2] = le32_to_cpu(scale->min);
711 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
712 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100713
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100714 kc->tlv.p = (void *)p;
715 return 0;
716}
717
718static int soc_tplg_create_tlv(struct soc_tplg *tplg,
719 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
720{
721 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500722 u32 access = le32_to_cpu(tc->access);
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100723
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500724 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100725 return 0;
726
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500727 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100728 tplg_tlv = &tc->tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500729 switch (le32_to_cpu(tplg_tlv->type)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100730 case SNDRV_CTL_TLVT_DB_SCALE:
731 return soc_tplg_create_tlv_db_scale(tplg, kc,
732 &tplg_tlv->scale);
733
734 /* TODO: add support for other TLV types */
735 default:
736 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
737 tplg_tlv->type);
738 return -EINVAL;
739 }
740 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100741
742 return 0;
743}
744
745static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
746 struct snd_kcontrol_new *kc)
747{
748 kfree(kc->tlv.p);
749}
750
751static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
752 size_t size)
753{
754 struct snd_soc_tplg_bytes_control *be;
755 struct soc_bytes_ext *sbe;
756 struct snd_kcontrol_new kc;
757 int i, err;
758
759 if (soc_tplg_check_elem_count(tplg,
760 sizeof(struct snd_soc_tplg_bytes_control), count,
761 size, "mixer bytes")) {
762 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
763 count);
764 return -EINVAL;
765 }
766
767 for (i = 0; i < count; i++) {
768 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
769
770 /* validate kcontrol */
771 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
772 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
773 return -EINVAL;
774
775 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
776 if (sbe == NULL)
777 return -ENOMEM;
778
779 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500780 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100781
782 dev_dbg(tplg->dev,
783 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
784 be->hdr.name, be->hdr.access);
785
786 memset(&kc, 0, sizeof(kc));
787 kc.name = be->hdr.name;
788 kc.private_value = (long)sbe;
789 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500790 kc.access = le32_to_cpu(be->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100791
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500792 sbe->max = le32_to_cpu(be->max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100793 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
794 sbe->dobj.ops = tplg->ops;
795 INIT_LIST_HEAD(&sbe->dobj.list);
796
797 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800798 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100799 if (err) {
800 soc_control_err(tplg, &be->hdr, be->hdr.name);
801 kfree(sbe);
802 continue;
803 }
804
805 /* pass control to driver for optional further init */
806 err = soc_tplg_init_kcontrol(tplg, &kc,
807 (struct snd_soc_tplg_ctl_hdr *)be);
808 if (err < 0) {
809 dev_err(tplg->dev, "ASoC: failed to init %s\n",
810 be->hdr.name);
811 kfree(sbe);
812 continue;
813 }
814
815 /* register control here */
816 err = soc_tplg_add_kcontrol(tplg, &kc,
817 &sbe->dobj.control.kcontrol);
818 if (err < 0) {
819 dev_err(tplg->dev, "ASoC: failed to add %s\n",
820 be->hdr.name);
821 kfree(sbe);
822 continue;
823 }
824
825 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
826 }
827 return 0;
828
829}
830
831static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
832 size_t size)
833{
834 struct snd_soc_tplg_mixer_control *mc;
835 struct soc_mixer_control *sm;
836 struct snd_kcontrol_new kc;
837 int i, err;
838
839 if (soc_tplg_check_elem_count(tplg,
840 sizeof(struct snd_soc_tplg_mixer_control),
841 count, size, "mixers")) {
842
843 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
844 count);
845 return -EINVAL;
846 }
847
848 for (i = 0; i < count; i++) {
849 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
850
851 /* validate kcontrol */
852 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
853 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
854 return -EINVAL;
855
856 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
857 if (sm == NULL)
858 return -ENOMEM;
859 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500860 le32_to_cpu(mc->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100861
862 dev_dbg(tplg->dev,
863 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
864 mc->hdr.name, mc->hdr.access);
865
866 memset(&kc, 0, sizeof(kc));
867 kc.name = mc->hdr.name;
868 kc.private_value = (long)sm;
869 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500870 kc.access = le32_to_cpu(mc->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100871
872 /* we only support FL/FR channel mapping atm */
873 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
874 SNDRV_CHMAP_FL);
875 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
876 SNDRV_CHMAP_FR);
877 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
878 SNDRV_CHMAP_FL);
879 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
880 SNDRV_CHMAP_FR);
881
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500882 sm->max = le32_to_cpu(mc->max);
883 sm->min = le32_to_cpu(mc->min);
884 sm->invert = le32_to_cpu(mc->invert);
885 sm->platform_max = le32_to_cpu(mc->platform_max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100886 sm->dobj.index = tplg->index;
887 sm->dobj.ops = tplg->ops;
888 sm->dobj.type = SND_SOC_DOBJ_MIXER;
889 INIT_LIST_HEAD(&sm->dobj.list);
890
891 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800892 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100893 if (err) {
894 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
895 kfree(sm);
896 continue;
897 }
898
Bard liao3789deb2019-03-13 21:49:43 +0800899 /* create any TLV data */
900 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
901
Liam Girdwood8a978232015-05-29 19:06:14 +0100902 /* pass control to driver for optional further init */
903 err = soc_tplg_init_kcontrol(tplg, &kc,
904 (struct snd_soc_tplg_ctl_hdr *) mc);
905 if (err < 0) {
906 dev_err(tplg->dev, "ASoC: failed to init %s\n",
907 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +0800908 soc_tplg_free_tlv(tplg, &kc);
Liam Girdwood8a978232015-05-29 19:06:14 +0100909 kfree(sm);
910 continue;
911 }
912
Liam Girdwood8a978232015-05-29 19:06:14 +0100913 /* register control here */
914 err = soc_tplg_add_kcontrol(tplg, &kc,
915 &sm->dobj.control.kcontrol);
916 if (err < 0) {
917 dev_err(tplg->dev, "ASoC: failed to add %s\n",
918 mc->hdr.name);
919 soc_tplg_free_tlv(tplg, &kc);
920 kfree(sm);
921 continue;
922 }
923
924 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
925 }
926
927 return 0;
928}
929
930static int soc_tplg_denum_create_texts(struct soc_enum *se,
931 struct snd_soc_tplg_enum_control *ec)
932{
933 int i, ret;
934
935 se->dobj.control.dtexts =
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500936 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100937 if (se->dobj.control.dtexts == NULL)
938 return -ENOMEM;
939
940 for (i = 0; i < ec->items; i++) {
941
942 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
943 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
944 ret = -EINVAL;
945 goto err;
946 }
947
948 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
949 if (!se->dobj.control.dtexts[i]) {
950 ret = -ENOMEM;
951 goto err;
952 }
953 }
954
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200955 se->items = le32_to_cpu(ec->items);
Mousumi Janab6e38b22017-04-11 13:06:22 +0530956 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100957 return 0;
958
959err:
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200960 se->items = i;
961 soc_tplg_denum_remove_texts(se);
962 return ret;
963}
964
965static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
966{
967 int i = se->items;
968
Liam Girdwood8a978232015-05-29 19:06:14 +0100969 for (--i; i >= 0; i--)
970 kfree(se->dobj.control.dtexts[i]);
971 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +0100972}
973
974static int soc_tplg_denum_create_values(struct soc_enum *se,
975 struct snd_soc_tplg_enum_control *ec)
976{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500977 int i;
978
979 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
Liam Girdwood8a978232015-05-29 19:06:14 +0100980 return -EINVAL;
981
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500982 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
983 sizeof(u32),
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200984 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100985 if (!se->dobj.control.dvalues)
986 return -ENOMEM;
987
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500988 /* convert from little-endian */
989 for (i = 0; i < le32_to_cpu(ec->items); i++) {
990 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
991 }
992
Liam Girdwood8a978232015-05-29 19:06:14 +0100993 return 0;
994}
995
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200996static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
997{
998 kfree(se->dobj.control.dvalues);
999}
1000
Liam Girdwood8a978232015-05-29 19:06:14 +01001001static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
1002 size_t size)
1003{
1004 struct snd_soc_tplg_enum_control *ec;
1005 struct soc_enum *se;
1006 struct snd_kcontrol_new kc;
1007 int i, ret, err;
1008
1009 if (soc_tplg_check_elem_count(tplg,
1010 sizeof(struct snd_soc_tplg_enum_control),
1011 count, size, "enums")) {
1012
1013 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1014 count);
1015 return -EINVAL;
1016 }
1017
1018 for (i = 0; i < count; i++) {
1019 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001020
1021 /* validate kcontrol */
1022 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1023 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1024 return -EINVAL;
1025
1026 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1027 if (se == NULL)
1028 return -ENOMEM;
1029
Liam Girdwood02b64242019-03-01 19:08:52 -06001030 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001031 le32_to_cpu(ec->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001032
Liam Girdwood8a978232015-05-29 19:06:14 +01001033 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1034 ec->hdr.name, ec->items);
1035
1036 memset(&kc, 0, sizeof(kc));
1037 kc.name = ec->hdr.name;
1038 kc.private_value = (long)se;
1039 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001040 kc.access = le32_to_cpu(ec->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001041
1042 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1043 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1044 SNDRV_CHMAP_FL);
1045 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1046 SNDRV_CHMAP_FL);
1047
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001048 se->mask = le32_to_cpu(ec->mask);
Liam Girdwood8a978232015-05-29 19:06:14 +01001049 se->dobj.index = tplg->index;
1050 se->dobj.type = SND_SOC_DOBJ_ENUM;
1051 se->dobj.ops = tplg->ops;
1052 INIT_LIST_HEAD(&se->dobj.list);
1053
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001054 switch (le32_to_cpu(ec->hdr.ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001055 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1056 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1057 err = soc_tplg_denum_create_values(se, ec);
1058 if (err < 0) {
1059 dev_err(tplg->dev,
1060 "ASoC: could not create values for %s\n",
1061 ec->hdr.name);
1062 kfree(se);
1063 continue;
1064 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001065 /* fall through */
Liam Girdwood8a978232015-05-29 19:06:14 +01001066 case SND_SOC_TPLG_CTL_ENUM:
1067 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1068 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1069 err = soc_tplg_denum_create_texts(se, ec);
1070 if (err < 0) {
1071 dev_err(tplg->dev,
1072 "ASoC: could not create texts for %s\n",
1073 ec->hdr.name);
1074 kfree(se);
1075 continue;
1076 }
1077 break;
1078 default:
1079 dev_err(tplg->dev,
1080 "ASoC: invalid enum control type %d for %s\n",
1081 ec->hdr.ops.info, ec->hdr.name);
1082 kfree(se);
1083 continue;
1084 }
1085
1086 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001087 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001088 if (err) {
1089 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1090 kfree(se);
1091 continue;
1092 }
1093
1094 /* pass control to driver for optional further init */
1095 err = soc_tplg_init_kcontrol(tplg, &kc,
1096 (struct snd_soc_tplg_ctl_hdr *) ec);
1097 if (err < 0) {
1098 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1099 ec->hdr.name);
1100 kfree(se);
1101 continue;
1102 }
1103
1104 /* register control here */
1105 ret = soc_tplg_add_kcontrol(tplg,
1106 &kc, &se->dobj.control.kcontrol);
1107 if (ret < 0) {
1108 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1109 ec->hdr.name);
1110 kfree(se);
1111 continue;
1112 }
1113
1114 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1115 }
1116
1117 return 0;
1118}
1119
1120static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1121 struct snd_soc_tplg_hdr *hdr)
1122{
1123 struct snd_soc_tplg_ctl_hdr *control_hdr;
1124 int i;
1125
1126 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001127 tplg->pos += le32_to_cpu(hdr->size) +
1128 le32_to_cpu(hdr->payload_size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001129 return 0;
1130 }
1131
1132 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1133 soc_tplg_get_offset(tplg));
1134
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001135 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001136
1137 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1138
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001139 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001140 dev_err(tplg->dev, "ASoC: invalid control size\n");
1141 return -EINVAL;
1142 }
1143
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001144 switch (le32_to_cpu(control_hdr->ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001145 case SND_SOC_TPLG_CTL_VOLSW:
1146 case SND_SOC_TPLG_CTL_STROBE:
1147 case SND_SOC_TPLG_CTL_VOLSW_SX:
1148 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1149 case SND_SOC_TPLG_CTL_RANGE:
1150 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1151 case SND_SOC_TPLG_DAPM_CTL_PIN:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001152 soc_tplg_dmixer_create(tplg, 1,
1153 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001154 break;
1155 case SND_SOC_TPLG_CTL_ENUM:
1156 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1157 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1158 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1159 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001160 soc_tplg_denum_create(tplg, 1,
1161 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001162 break;
1163 case SND_SOC_TPLG_CTL_BYTES:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001164 soc_tplg_dbytes_create(tplg, 1,
1165 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001166 break;
1167 default:
1168 soc_bind_err(tplg, control_hdr, i);
1169 return -EINVAL;
1170 }
1171 }
1172
1173 return 0;
1174}
1175
Liam Girdwood503e79b2018-06-14 20:53:59 +01001176/* optionally pass new dynamic kcontrol to component driver. */
1177static int soc_tplg_add_route(struct soc_tplg *tplg,
1178 struct snd_soc_dapm_route *route)
1179{
1180 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1181 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1182 route);
1183
1184 return 0;
1185}
1186
Liam Girdwood8a978232015-05-29 19:06:14 +01001187static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1188 struct snd_soc_tplg_hdr *hdr)
1189{
1190 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001191 struct snd_soc_tplg_dapm_graph_elem *elem;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001192 struct snd_soc_dapm_route **routes;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001193 int count, i, j;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001194 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001195
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001196 count = le32_to_cpu(hdr->count);
1197
Liam Girdwood8a978232015-05-29 19:06:14 +01001198 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001199 tplg->pos +=
1200 le32_to_cpu(hdr->size) +
1201 le32_to_cpu(hdr->payload_size);
1202
Liam Girdwood8a978232015-05-29 19:06:14 +01001203 return 0;
1204 }
1205
1206 if (soc_tplg_check_elem_count(tplg,
1207 sizeof(struct snd_soc_tplg_dapm_graph_elem),
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001208 count, le32_to_cpu(hdr->payload_size), "graph")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001209
1210 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1211 count);
1212 return -EINVAL;
1213 }
1214
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001215 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1216 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001217
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001218 /* allocate memory for pointer to array of dapm routes */
1219 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1220 GFP_KERNEL);
1221 if (!routes)
1222 return -ENOMEM;
1223
1224 /*
1225 * allocate memory for each dapm route in the array.
1226 * This needs to be done individually so that
1227 * each route can be freed when it is removed in remove_route().
1228 */
1229 for (i = 0; i < count; i++) {
1230 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1231 if (!routes[i]) {
1232 /* free previously allocated memory */
1233 for (j = 0; j < i; j++)
1234 kfree(routes[j]);
1235
1236 kfree(routes);
1237 return -ENOMEM;
1238 }
1239 }
1240
Liam Girdwood8a978232015-05-29 19:06:14 +01001241 for (i = 0; i < count; i++) {
1242 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1243 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1244
1245 /* validate routes */
1246 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001247 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1248 ret = -EINVAL;
1249 break;
1250 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001251 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001252 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1253 ret = -EINVAL;
1254 break;
1255 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001256 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001257 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1258 ret = -EINVAL;
1259 break;
1260 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001261
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001262 routes[i]->source = elem->source;
1263 routes[i]->sink = elem->sink;
1264
1265 /* set to NULL atm for tplg users */
1266 routes[i]->connected = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001267 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001268 routes[i]->control = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001269 else
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001270 routes[i]->control = elem->control;
Liam Girdwood8a978232015-05-29 19:06:14 +01001271
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001272 /* add route dobj to dobj_list */
1273 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1274 routes[i]->dobj.ops = tplg->ops;
1275 routes[i]->dobj.index = tplg->index;
1276 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1277
1278 soc_tplg_add_route(tplg, routes[i]);
Liam Girdwood503e79b2018-06-14 20:53:59 +01001279
Liam Girdwood8a978232015-05-29 19:06:14 +01001280 /* add route, but keep going if some fail */
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001281 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Liam Girdwood8a978232015-05-29 19:06:14 +01001282 }
1283
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001284 /* free memory allocated for all dapm routes in case of error */
1285 if (ret < 0)
1286 for (i = 0; i < count ; i++)
1287 kfree(routes[i]);
1288
1289 /*
1290 * free pointer to array of dapm routes as this is no longer needed.
1291 * The memory allocated for each dapm route will be freed
1292 * when it is removed in remove_route().
1293 */
1294 kfree(routes);
1295
1296 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001297}
1298
1299static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1300 struct soc_tplg *tplg, int num_kcontrols)
1301{
1302 struct snd_kcontrol_new *kc;
1303 struct soc_mixer_control *sm;
1304 struct snd_soc_tplg_mixer_control *mc;
1305 int i, err;
1306
Axel Lin4ca7deb2015-08-05 22:34:22 +08001307 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001308 if (kc == NULL)
1309 return NULL;
1310
1311 for (i = 0; i < num_kcontrols; i++) {
1312 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001313
Liam Girdwood8a978232015-05-29 19:06:14 +01001314 /* validate kcontrol */
1315 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1316 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001317 goto err_sm;
1318
1319 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1320 if (sm == NULL)
1321 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001322
Liam Girdwood02b64242019-03-01 19:08:52 -06001323 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001324 le32_to_cpu(mc->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001325
Liam Girdwood8a978232015-05-29 19:06:14 +01001326 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1327 mc->hdr.name, i);
1328
Colin Ian King1ad741d2019-06-27 14:32:08 +01001329 kc[i].private_value = (long)sm;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001330 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1331 if (kc[i].name == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001332 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001333 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1334 kc[i].access = mc->hdr.access;
1335
1336 /* we only support FL/FR channel mapping atm */
1337 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1338 SNDRV_CHMAP_FL);
1339 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1340 SNDRV_CHMAP_FR);
1341 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1342 SNDRV_CHMAP_FL);
1343 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1344 SNDRV_CHMAP_FR);
1345
1346 sm->max = mc->max;
1347 sm->min = mc->min;
1348 sm->invert = mc->invert;
1349 sm->platform_max = mc->platform_max;
1350 sm->dobj.index = tplg->index;
1351 INIT_LIST_HEAD(&sm->dobj.list);
1352
1353 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001354 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001355 if (err) {
1356 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001357 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001358 }
1359
Bard liao3789deb2019-03-13 21:49:43 +08001360 /* create any TLV data */
1361 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1362
Liam Girdwood8a978232015-05-29 19:06:14 +01001363 /* pass control to driver for optional further init */
1364 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1365 (struct snd_soc_tplg_ctl_hdr *)mc);
1366 if (err < 0) {
1367 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1368 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +08001369 soc_tplg_free_tlv(tplg, &kc[i]);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001370 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001371 }
1372 }
1373 return kc;
1374
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001375err_sm:
1376 for (; i >= 0; i--) {
1377 sm = (struct soc_mixer_control *)kc[i].private_value;
1378 kfree(sm);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001379 kfree(kc[i].name);
1380 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001381 kfree(kc);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001382
Liam Girdwood8a978232015-05-29 19:06:14 +01001383 return NULL;
1384}
1385
1386static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001387 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001388{
1389 struct snd_kcontrol_new *kc;
1390 struct snd_soc_tplg_enum_control *ec;
1391 struct soc_enum *se;
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +02001392 int i, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001393
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001394 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001395 if (kc == NULL)
1396 return NULL;
1397
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001398 for (i = 0; i < num_kcontrols; i++) {
1399 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1400 /* validate kcontrol */
1401 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1402 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001403 goto err_se;
Liam Girdwood8a978232015-05-29 19:06:14 +01001404
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001405 se = kzalloc(sizeof(*se), GFP_KERNEL);
1406 if (se == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001407 goto err_se;
Liam Girdwood8a978232015-05-29 19:06:14 +01001408
Liam Girdwood02b64242019-03-01 19:08:52 -06001409 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1410 ec->priv.size);
1411
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001412 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001413 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001414
Colin Ian King1ad741d2019-06-27 14:32:08 +01001415 kc[i].private_value = (long)se;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001416 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001417 if (kc[i].name == NULL)
Liam Girdwood267e2c62018-03-27 12:04:04 +01001418 goto err_se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001419 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1420 kc[i].access = ec->hdr.access;
1421
1422 /* we only support FL/FR channel mapping atm */
1423 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1424 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1425 SNDRV_CHMAP_FL);
1426 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1427 SNDRV_CHMAP_FR);
1428
1429 se->items = ec->items;
1430 se->mask = ec->mask;
1431 se->dobj.index = tplg->index;
1432
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001433 switch (le32_to_cpu(ec->hdr.ops.info)) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001434 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1435 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1436 err = soc_tplg_denum_create_values(se, ec);
1437 if (err < 0) {
1438 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1439 ec->hdr.name);
1440 goto err_se;
1441 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001442 /* fall through */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001443 case SND_SOC_TPLG_CTL_ENUM:
1444 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1445 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1446 err = soc_tplg_denum_create_texts(se, ec);
1447 if (err < 0) {
1448 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1449 ec->hdr.name);
1450 goto err_se;
1451 }
1452 break;
1453 default:
1454 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1455 ec->hdr.ops.info, ec->hdr.name);
1456 goto err_se;
1457 }
1458
1459 /* map io handlers */
1460 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1461 if (err) {
1462 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1463 goto err_se;
1464 }
1465
1466 /* pass control to driver for optional further init */
1467 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1468 (struct snd_soc_tplg_ctl_hdr *)ec);
1469 if (err < 0) {
1470 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1471 ec->hdr.name);
1472 goto err_se;
1473 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001474 }
1475
1476 return kc;
1477
1478err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001479 for (; i >= 0; i--) {
1480 /* free values and texts */
1481 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001482
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001483 if (se) {
1484 soc_tplg_denum_remove_values(se);
1485 soc_tplg_denum_remove_texts(se);
1486 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001487
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001488 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001489 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001490 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001491 kfree(kc);
1492
1493 return NULL;
1494}
1495
1496static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001497 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001498{
1499 struct snd_soc_tplg_bytes_control *be;
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001500 struct soc_bytes_ext *sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001501 struct snd_kcontrol_new *kc;
1502 int i, err;
1503
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001504 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001505 if (!kc)
1506 return NULL;
1507
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001508 for (i = 0; i < num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001509 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1510
1511 /* validate kcontrol */
1512 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1513 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001514 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001515
1516 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1517 if (sbe == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001518 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001519
1520 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001521 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001522
1523 dev_dbg(tplg->dev,
1524 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1525 be->hdr.name, be->hdr.access);
1526
Colin Ian King1ad741d2019-06-27 14:32:08 +01001527 kc[i].private_value = (long)sbe;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001528 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001529 if (kc[i].name == NULL)
1530 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001531 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1532 kc[i].access = be->hdr.access;
1533
1534 sbe->max = be->max;
1535 INIT_LIST_HEAD(&sbe->dobj.list);
1536
1537 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001538 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001539 if (err) {
1540 soc_control_err(tplg, &be->hdr, be->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001541 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001542 }
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);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001550 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001551 }
1552 }
1553
1554 return kc;
1555
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001556err_sbe:
1557 for (; i >= 0; i--) {
1558 sbe = (struct soc_bytes_ext *)kc[i].private_value;
1559 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001560 kfree(kc[i].name);
1561 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001562 kfree(kc);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001563
Liam Girdwood8a978232015-05-29 19:06:14 +01001564 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));
Dan Carpenter752c9382019-09-25 14:06:24 +03001591 if ((int)template.id < 0)
Liam Girdwood8a978232015-05-29 19:06:14 +01001592 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
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001885 /* link + cpu + codec + platform */
1886 link = kzalloc(sizeof(*link) + (3 * 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];
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001894 link->platforms = &dlc[2];
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001895
1896 link->num_cpus = 1;
1897 link->num_codecs = 1;
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001898 link->num_platforms = 1;
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001899
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001900 if (strlen(pcm->pcm_name)) {
1901 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1902 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1903 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001904 link->id = le32_to_cpu(pcm->pcm_id);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001905
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001906 if (strlen(pcm->dai_name))
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001907 link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001908
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001909 link->codecs->name = "snd-soc-dummy";
1910 link->codecs->dai_name = "snd-soc-dummy-dai";
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001911
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001912 link->platforms->name = "snd-soc-dummy";
1913
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001914 /* enable DPCM */
1915 link->dynamic = 1;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001916 link->dpcm_playback = le32_to_cpu(pcm->playback);
1917 link->dpcm_capture = le32_to_cpu(pcm->capture);
Mengdong Lin717a8e72016-11-03 01:03:34 +08001918 if (pcm->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001919 set_link_flags(link,
1920 le32_to_cpu(pcm->flag_mask),
1921 le32_to_cpu(pcm->flags));
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001922
Mengdong Linacfc7d42016-01-15 16:13:37 +08001923 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001924 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001925 if (ret < 0) {
1926 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
Bard liaob3718b82019-03-05 23:57:53 +08001927 kfree(link->name);
1928 kfree(link->stream_name);
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001929 kfree(link->cpus->dai_name);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001930 kfree(link);
1931 return ret;
1932 }
1933
1934 link->dobj.index = tplg->index;
1935 link->dobj.ops = tplg->ops;
1936 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1937 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1938
1939 snd_soc_add_dai_link(tplg->comp->card, link);
1940 return 0;
1941}
1942
1943/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001944static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1945 struct snd_soc_tplg_pcm *pcm)
1946{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001947 int ret;
1948
1949 ret = soc_tplg_dai_create(tplg, pcm);
1950 if (ret < 0)
1951 return ret;
1952
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001953 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001954}
1955
Mengdong Lin55726dc2016-11-03 01:00:16 +08001956/* copy stream caps from the old version 4 of source */
1957static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1958 struct snd_soc_tplg_stream_caps_v4 *src)
1959{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001960 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin55726dc2016-11-03 01:00:16 +08001961 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1962 dest->formats = src->formats;
1963 dest->rates = src->rates;
1964 dest->rate_min = src->rate_min;
1965 dest->rate_max = src->rate_max;
1966 dest->channels_min = src->channels_min;
1967 dest->channels_max = src->channels_max;
1968 dest->periods_min = src->periods_min;
1969 dest->periods_max = src->periods_max;
1970 dest->period_size_min = src->period_size_min;
1971 dest->period_size_max = src->period_size_max;
1972 dest->buffer_size_min = src->buffer_size_min;
1973 dest->buffer_size_max = src->buffer_size_max;
1974}
1975
1976/**
1977 * pcm_new_ver - Create the new version of PCM from the old version.
1978 * @tplg: topology context
1979 * @src: older version of pcm as a source
1980 * @pcm: latest version of pcm created from the source
1981 *
1982 * Support from vesion 4. User should free the returned pcm manually.
1983 */
1984static int pcm_new_ver(struct soc_tplg *tplg,
1985 struct snd_soc_tplg_pcm *src,
1986 struct snd_soc_tplg_pcm **pcm)
1987{
1988 struct snd_soc_tplg_pcm *dest;
1989 struct snd_soc_tplg_pcm_v4 *src_v4;
1990 int i;
1991
1992 *pcm = NULL;
1993
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001994 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001995 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1996 return -EINVAL;
1997 }
1998
1999 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
2000 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
2001 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2002 if (!dest)
2003 return -ENOMEM;
2004
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002005 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin55726dc2016-11-03 01:00:16 +08002006 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2007 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2008 dest->pcm_id = src_v4->pcm_id;
2009 dest->dai_id = src_v4->dai_id;
2010 dest->playback = src_v4->playback;
2011 dest->capture = src_v4->capture;
2012 dest->compress = src_v4->compress;
2013 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002014 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin55726dc2016-11-03 01:00:16 +08002015 memcpy(&dest->stream[i], &src_v4->stream[i],
2016 sizeof(struct snd_soc_tplg_stream));
2017
2018 for (i = 0; i < 2; i++)
2019 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2020
2021 *pcm = dest;
2022 return 0;
2023}
2024
Mengdong Lin64527e82016-01-15 16:13:28 +08002025static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01002026 struct snd_soc_tplg_hdr *hdr)
2027{
Mengdong Lin55726dc2016-11-03 01:00:16 +08002028 struct snd_soc_tplg_pcm *pcm, *_pcm;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002029 int count;
2030 int size;
Vinod Koulfd340452016-12-08 23:01:27 +05302031 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08002032 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01002033
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002034 count = le32_to_cpu(hdr->count);
2035
Liam Girdwood8a978232015-05-29 19:06:14 +01002036 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
2037 return 0;
2038
Mengdong Lin55726dc2016-11-03 01:00:16 +08002039 /* check the element size and count */
2040 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002041 size = le32_to_cpu(pcm->size);
2042 if (size > sizeof(struct snd_soc_tplg_pcm)
2043 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002044 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002045 size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002046 return -EINVAL;
2047 }
2048
Liam Girdwood8a978232015-05-29 19:06:14 +01002049 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002050 size, count,
2051 le32_to_cpu(hdr->payload_size),
2052 "PCM DAI")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002053 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2054 count);
2055 return -EINVAL;
2056 }
2057
Mengdong Lin64527e82016-01-15 16:13:28 +08002058 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002059 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002060 size = le32_to_cpu(pcm->size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002061
2062 /* check ABI version by size, create a new version of pcm
2063 * if abi not match.
2064 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002065 if (size == sizeof(*pcm)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002066 abi_match = true;
2067 _pcm = pcm;
2068 } else {
2069 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05302070 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002071 }
2072
Mengdong Lin55726dc2016-11-03 01:00:16 +08002073 /* create the FE DAIs and DAI links */
2074 soc_tplg_pcm_create(tplg, _pcm);
2075
Mengdong Lin717a8e72016-11-03 01:03:34 +08002076 /* offset by version-specific struct size and
2077 * real priv data size
2078 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002079 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
Mengdong Lin717a8e72016-11-03 01:03:34 +08002080
Mengdong Lin55726dc2016-11-03 01:00:16 +08002081 if (!abi_match)
2082 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08002083 }
2084
Liam Girdwood8a978232015-05-29 19:06:14 +01002085 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002086
Liam Girdwood8a978232015-05-29 19:06:14 +01002087 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002088}
2089
Mengdong Lin593d9e52016-11-03 01:04:27 +08002090/**
2091 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002092 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002093 * @cfg: physical link configs.
2094 *
2095 * Topology context contains a list of supported HW formats (configs) and
2096 * a default format ID for the physical link. This function will use this
2097 * default ID to choose the HW format to set the link's DAI format for init.
2098 */
2099static void set_link_hw_format(struct snd_soc_dai_link *link,
2100 struct snd_soc_tplg_link_config *cfg)
2101{
2102 struct snd_soc_tplg_hw_config *hw_config;
2103 unsigned char bclk_master, fsync_master;
2104 unsigned char invert_bclk, invert_fsync;
2105 int i;
2106
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002107 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002108 hw_config = &cfg->hw_config[i];
2109 if (hw_config->id != cfg->default_hw_config_id)
2110 continue;
2111
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002112 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2113 SND_SOC_DAIFMT_FORMAT_MASK;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002114
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002115 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002116 switch (hw_config->clock_gated) {
2117 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002118 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002119 break;
2120
2121 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002122 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002123 break;
2124
2125 default:
2126 /* ignore the value */
2127 break;
2128 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002129
Mengdong Lin593d9e52016-11-03 01:04:27 +08002130 /* clock signal polarity */
2131 invert_bclk = hw_config->invert_bclk;
2132 invert_fsync = hw_config->invert_fsync;
2133 if (!invert_bclk && !invert_fsync)
2134 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2135 else if (!invert_bclk && invert_fsync)
2136 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2137 else if (invert_bclk && !invert_fsync)
2138 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2139 else
2140 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2141
2142 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002143 bclk_master = (hw_config->bclk_master ==
2144 SND_SOC_TPLG_BCLK_CM);
2145 fsync_master = (hw_config->fsync_master ==
2146 SND_SOC_TPLG_FSYNC_CM);
2147 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002148 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002149 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002150 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2151 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002152 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2153 else
2154 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2155 }
2156}
2157
2158/**
2159 * link_new_ver - Create a new physical link config from the old
2160 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002161 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002162 * @src: old version of phyical link config as a source
2163 * @link: latest version of physical link config created from the source
2164 *
2165 * Support from vesion 4. User need free the returned link config manually.
2166 */
2167static int link_new_ver(struct soc_tplg *tplg,
2168 struct snd_soc_tplg_link_config *src,
2169 struct snd_soc_tplg_link_config **link)
2170{
2171 struct snd_soc_tplg_link_config *dest;
2172 struct snd_soc_tplg_link_config_v4 *src_v4;
2173 int i;
2174
2175 *link = NULL;
2176
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002177 if (le32_to_cpu(src->size) !=
2178 sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002179 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2180 return -EINVAL;
2181 }
2182
2183 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2184
2185 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2186 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2187 if (!dest)
2188 return -ENOMEM;
2189
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002190 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002191 dest->id = src_v4->id;
2192 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002193 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002194 memcpy(&dest->stream[i], &src_v4->stream[i],
2195 sizeof(struct snd_soc_tplg_stream));
2196
2197 *link = dest;
2198 return 0;
2199}
2200
2201/* Find and configure an existing physical DAI link */
2202static int soc_tplg_link_config(struct soc_tplg *tplg,
2203 struct snd_soc_tplg_link_config *cfg)
2204{
2205 struct snd_soc_dai_link *link;
2206 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002207 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002208 int ret;
2209
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002210 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2211 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2212 return -EINVAL;
2213 else if (len)
2214 name = cfg->name;
2215 else
2216 name = NULL;
2217
2218 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2219 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2220 return -EINVAL;
2221 else if (len)
2222 stream_name = cfg->stream_name;
2223 else
2224 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002225
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002226 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
Mengdong Lin593d9e52016-11-03 01:04:27 +08002227 name, stream_name);
2228 if (!link) {
2229 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2230 name, cfg->id);
2231 return -EINVAL;
2232 }
2233
2234 /* hw format */
2235 if (cfg->num_hw_configs)
2236 set_link_hw_format(link, cfg);
2237
2238 /* flags */
2239 if (cfg->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002240 set_link_flags(link,
2241 le32_to_cpu(cfg->flag_mask),
2242 le32_to_cpu(cfg->flags));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002243
2244 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002245 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002246 if (ret < 0) {
2247 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2248 return ret;
2249 }
2250
Bard liaoadfebb52019-02-01 11:07:40 -06002251 /* for unloading it in snd_soc_tplg_component_remove */
2252 link->dobj.index = tplg->index;
2253 link->dobj.ops = tplg->ops;
2254 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2255 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2256
Mengdong Lin593d9e52016-11-03 01:04:27 +08002257 return 0;
2258}
2259
2260
2261/* Load physical link config elements from the topology context */
2262static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2263 struct snd_soc_tplg_hdr *hdr)
2264{
2265 struct snd_soc_tplg_link_config *link, *_link;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002266 int count;
2267 int size;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002268 int i, ret;
2269 bool abi_match;
2270
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002271 count = le32_to_cpu(hdr->count);
2272
Mengdong Lin593d9e52016-11-03 01:04:27 +08002273 if (tplg->pass != SOC_TPLG_PASS_LINK) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002274 tplg->pos += le32_to_cpu(hdr->size) +
2275 le32_to_cpu(hdr->payload_size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002276 return 0;
2277 };
2278
2279 /* check the element size and count */
2280 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002281 size = le32_to_cpu(link->size);
2282 if (size > sizeof(struct snd_soc_tplg_link_config)
2283 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002284 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002285 size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002286 return -EINVAL;
2287 }
2288
2289 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002290 size, count,
2291 le32_to_cpu(hdr->payload_size),
2292 "physical link config")) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002293 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2294 count);
2295 return -EINVAL;
2296 }
2297
2298 /* config physical DAI links */
2299 for (i = 0; i < count; i++) {
2300 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002301 size = le32_to_cpu(link->size);
2302 if (size == sizeof(*link)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002303 abi_match = true;
2304 _link = link;
2305 } else {
2306 abi_match = false;
2307 ret = link_new_ver(tplg, link, &_link);
2308 if (ret < 0)
2309 return ret;
2310 }
2311
2312 ret = soc_tplg_link_config(tplg, _link);
2313 if (ret < 0)
2314 return ret;
2315
2316 /* offset by version-specific struct size and
2317 * real priv data size
2318 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002319 tplg->pos += size + le32_to_cpu(_link->priv.size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002320
2321 if (!abi_match)
2322 kfree(_link); /* free the duplicated one */
2323 }
2324
2325 return 0;
2326}
2327
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002328/**
2329 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002330 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002331 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002332 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002333 * The physical dai should already be registered by the platform driver.
2334 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002335 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002336static int soc_tplg_dai_config(struct soc_tplg *tplg,
2337 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002338{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002339 struct snd_soc_dai_link_component dai_component;
Mengdong Lin0038be92016-07-26 14:32:37 +08002340 struct snd_soc_dai *dai;
2341 struct snd_soc_dai_driver *dai_drv;
2342 struct snd_soc_pcm_stream *stream;
2343 struct snd_soc_tplg_stream_caps *caps;
2344 int ret;
2345
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002346 memset(&dai_component, 0, sizeof(dai_component));
2347
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002348 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002349 dai = snd_soc_find_dai(&dai_component);
2350 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002351 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2352 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002353 return -EINVAL;
2354 }
2355
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002356 if (le32_to_cpu(d->dai_id) != dai->id) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002357 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2358 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002359 return -EINVAL;
2360 }
2361
2362 dai_drv = dai->driver;
2363 if (!dai_drv)
2364 return -EINVAL;
2365
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002366 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002367 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002368 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002369 set_stream_info(stream, caps);
2370 }
2371
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002372 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002373 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002374 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002375 set_stream_info(stream, caps);
2376 }
2377
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002378 if (d->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002379 set_dai_flags(dai_drv,
2380 le32_to_cpu(d->flag_mask),
2381 le32_to_cpu(d->flags));
Mengdong Lin0038be92016-07-26 14:32:37 +08002382
2383 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002384 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002385 if (ret < 0) {
2386 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2387 return ret;
2388 }
2389
2390 return 0;
2391}
2392
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002393/* load physical DAI elements */
2394static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2395 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002396{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002397 struct snd_soc_tplg_dai *dai;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002398 int count;
Mengdong Lin0038be92016-07-26 14:32:37 +08002399 int i;
2400
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002401 count = le32_to_cpu(hdr->count);
2402
Mengdong Lin0038be92016-07-26 14:32:37 +08002403 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2404 return 0;
2405
2406 /* config the existing BE DAIs */
2407 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002408 dai = (struct snd_soc_tplg_dai *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002409 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002410 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002411 return -EINVAL;
2412 }
2413
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002414 soc_tplg_dai_config(tplg, dai);
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002415 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
Mengdong Lin0038be92016-07-26 14:32:37 +08002416 }
2417
2418 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2419 return 0;
2420}
2421
Mengdong Lin583958f2016-10-11 14:36:42 +08002422/**
2423 * manifest_new_ver - Create a new version of manifest from the old version
2424 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002425 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002426 * @src: old version of manifest as a source
2427 * @manifest: latest version of manifest created from the source
2428 *
2429 * Support from vesion 4. Users need free the returned manifest manually.
2430 */
2431static int manifest_new_ver(struct soc_tplg *tplg,
2432 struct snd_soc_tplg_manifest *src,
2433 struct snd_soc_tplg_manifest **manifest)
2434{
2435 struct snd_soc_tplg_manifest *dest;
2436 struct snd_soc_tplg_manifest_v4 *src_v4;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002437 int size;
Mengdong Lin583958f2016-10-11 14:36:42 +08002438
2439 *manifest = NULL;
2440
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002441 size = le32_to_cpu(src->size);
2442 if (size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002443 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002444 size);
2445 if (size)
Guenter Roeckac9391d2018-05-24 12:49:21 -07002446 return -EINVAL;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002447 src->size = cpu_to_le32(sizeof(*src_v4));
Mengdong Lin583958f2016-10-11 14:36:42 +08002448 }
2449
2450 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2451
2452 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002453 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2454 GFP_KERNEL);
Mengdong Lin583958f2016-10-11 14:36:42 +08002455 if (!dest)
2456 return -ENOMEM;
2457
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002458 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin583958f2016-10-11 14:36:42 +08002459 dest->control_elems = src_v4->control_elems;
2460 dest->widget_elems = src_v4->widget_elems;
2461 dest->graph_elems = src_v4->graph_elems;
2462 dest->pcm_elems = src_v4->pcm_elems;
2463 dest->dai_link_elems = src_v4->dai_link_elems;
2464 dest->priv.size = src_v4->priv.size;
2465 if (dest->priv.size)
2466 memcpy(dest->priv.data, src_v4->priv.data,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002467 le32_to_cpu(src_v4->priv.size));
Mengdong Lin583958f2016-10-11 14:36:42 +08002468
2469 *manifest = dest;
2470 return 0;
2471}
Mengdong Lin0038be92016-07-26 14:32:37 +08002472
Liam Girdwood8a978232015-05-29 19:06:14 +01002473static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002474 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002475{
Mengdong Lin583958f2016-10-11 14:36:42 +08002476 struct snd_soc_tplg_manifest *manifest, *_manifest;
2477 bool abi_match;
2478 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002479
2480 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2481 return 0;
2482
2483 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002484
2485 /* check ABI version by size, create a new manifest if abi not match */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002486 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
Mengdong Lin583958f2016-10-11 14:36:42 +08002487 abi_match = true;
2488 _manifest = manifest;
2489 } else {
2490 abi_match = false;
2491 err = manifest_new_ver(tplg, manifest, &_manifest);
2492 if (err < 0)
2493 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002494 }
2495
Mengdong Lin583958f2016-10-11 14:36:42 +08002496 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002497 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002498 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002499
Mengdong Lin583958f2016-10-11 14:36:42 +08002500 if (!abi_match) /* free the duplicated one */
2501 kfree(_manifest);
2502
Liam Girdwood8a978232015-05-29 19:06:14 +01002503 return 0;
2504}
2505
2506/* validate header magic, size and type */
2507static int soc_valid_header(struct soc_tplg *tplg,
2508 struct snd_soc_tplg_hdr *hdr)
2509{
2510 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2511 return 0;
2512
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002513 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002514 dev_err(tplg->dev,
2515 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002516 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002517 tplg->fw->size);
2518 return -EINVAL;
2519 }
2520
Liam Girdwood8a978232015-05-29 19:06:14 +01002521 /* big endian firmware objects not supported atm */
Pierre-Louis Bossart21141712019-04-04 14:13:58 -05002522 if (hdr->magic == SOC_TPLG_MAGIC_BIG_ENDIAN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002523 dev_err(tplg->dev,
2524 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2525 tplg->pass, hdr->magic,
2526 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2527 return -EINVAL;
2528 }
2529
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002530 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002531 dev_err(tplg->dev,
2532 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2533 tplg->pass, hdr->magic,
2534 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2535 return -EINVAL;
2536 }
2537
Mengdong Lin288b8da2016-11-03 01:03:17 +08002538 /* Support ABI from version 4 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002539 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2540 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002541 dev_err(tplg->dev,
2542 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2543 tplg->pass, hdr->abi,
2544 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2545 tplg->fw->size);
2546 return -EINVAL;
2547 }
2548
2549 if (hdr->payload_size == 0) {
2550 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2551 soc_tplg_get_hdr_offset(tplg));
2552 return -EINVAL;
2553 }
2554
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002555 if (tplg->pass == le32_to_cpu(hdr->type))
Liam Girdwood8a978232015-05-29 19:06:14 +01002556 dev_dbg(tplg->dev,
2557 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2558 hdr->payload_size, hdr->type, hdr->version,
2559 hdr->vendor_type, tplg->pass);
2560
2561 return 1;
2562}
2563
2564/* check header type and call appropriate handler */
2565static int soc_tplg_load_header(struct soc_tplg *tplg,
2566 struct snd_soc_tplg_hdr *hdr)
2567{
2568 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2569
2570 /* check for matching ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002571 if (le32_to_cpu(hdr->index) != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002572 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002573 return 0;
2574
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002575 tplg->index = le32_to_cpu(hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01002576
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002577 switch (le32_to_cpu(hdr->type)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002578 case SND_SOC_TPLG_TYPE_MIXER:
2579 case SND_SOC_TPLG_TYPE_ENUM:
2580 case SND_SOC_TPLG_TYPE_BYTES:
2581 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2582 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2583 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2584 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2585 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2586 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002587 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002588 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002589 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002590 case SND_SOC_TPLG_TYPE_DAI_LINK:
2591 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2592 /* physical link configurations */
2593 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002594 case SND_SOC_TPLG_TYPE_MANIFEST:
2595 return soc_tplg_manifest_load(tplg, hdr);
2596 default:
2597 /* bespoke vendor data object */
2598 return soc_tplg_vendor_load(tplg, hdr);
2599 }
2600
2601 return 0;
2602}
2603
2604/* process the topology file headers */
2605static int soc_tplg_process_headers(struct soc_tplg *tplg)
2606{
2607 struct snd_soc_tplg_hdr *hdr;
2608 int ret;
2609
2610 tplg->pass = SOC_TPLG_PASS_START;
2611
2612 /* process the header types from start to end */
2613 while (tplg->pass <= SOC_TPLG_PASS_END) {
2614
2615 tplg->hdr_pos = tplg->fw->data;
2616 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2617
2618 while (!soc_tplg_is_eof(tplg)) {
2619
2620 /* make sure header is valid before loading */
2621 ret = soc_valid_header(tplg, hdr);
2622 if (ret < 0)
2623 return ret;
2624 else if (ret == 0)
2625 break;
2626
2627 /* load the header object */
2628 ret = soc_tplg_load_header(tplg, hdr);
2629 if (ret < 0)
2630 return ret;
2631
2632 /* goto next header */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002633 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
Liam Girdwood8a978232015-05-29 19:06:14 +01002634 sizeof(struct snd_soc_tplg_hdr);
2635 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2636 }
2637
2638 /* next data type pass */
2639 tplg->pass++;
2640 }
2641
2642 /* signal DAPM we are complete */
2643 ret = soc_tplg_dapm_complete(tplg);
2644 if (ret < 0)
2645 dev_err(tplg->dev,
2646 "ASoC: failed to initialise DAPM from Firmware\n");
2647
2648 return ret;
2649}
2650
2651static int soc_tplg_load(struct soc_tplg *tplg)
2652{
2653 int ret;
2654
2655 ret = soc_tplg_process_headers(tplg);
2656 if (ret == 0)
2657 soc_tplg_complete(tplg);
2658
2659 return ret;
2660}
2661
2662/* load audio component topology from "firmware" file */
2663int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2664 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2665{
2666 struct soc_tplg tplg;
Bard liao304017d2019-02-17 21:23:47 +08002667 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002668
2669 /* setup parsing context */
2670 memset(&tplg, 0, sizeof(tplg));
2671 tplg.fw = fw;
2672 tplg.dev = comp->dev;
2673 tplg.comp = comp;
2674 tplg.ops = ops;
2675 tplg.req_index = id;
2676 tplg.io_ops = ops->io_ops;
2677 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002678 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2679 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002680
Bard liao304017d2019-02-17 21:23:47 +08002681 ret = soc_tplg_load(&tplg);
2682 /* free the created components if fail to load topology */
2683 if (ret)
2684 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2685
2686 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002687}
2688EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2689
2690/* remove this dynamic widget */
2691void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2692{
2693 /* make sure we are a widget */
2694 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2695 return;
2696
2697 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2700
2701/* remove all dynamic widgets from this DAPM context */
2702void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2703 u32 index)
2704{
2705 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002706
2707 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2708
2709 /* make sure we are a widget with correct context */
2710 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2711 continue;
2712
2713 /* match ID */
2714 if (w->dobj.index != index &&
2715 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2716 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002717 /* check and free and dynamic widget kcontrols */
2718 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002719 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002720 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002721 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002722}
2723EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2724
2725/* remove dynamic controls from the component driver */
2726int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2727{
2728 struct snd_soc_dobj *dobj, *next_dobj;
2729 int pass = SOC_TPLG_PASS_END;
2730
2731 /* process the header types from end to start */
2732 while (pass >= SOC_TPLG_PASS_START) {
2733
2734 /* remove mixer controls */
2735 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2736 list) {
2737
2738 /* match index */
2739 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002740 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002741 continue;
2742
2743 switch (dobj->type) {
2744 case SND_SOC_DOBJ_MIXER:
2745 remove_mixer(comp, dobj, pass);
2746 break;
2747 case SND_SOC_DOBJ_ENUM:
2748 remove_enum(comp, dobj, pass);
2749 break;
2750 case SND_SOC_DOBJ_BYTES:
2751 remove_bytes(comp, dobj, pass);
2752 break;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06002753 case SND_SOC_DOBJ_GRAPH:
2754 remove_route(comp, dobj, pass);
2755 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002756 case SND_SOC_DOBJ_WIDGET:
2757 remove_widget(comp, dobj, pass);
2758 break;
2759 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002760 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002761 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002762 case SND_SOC_DOBJ_DAI_LINK:
2763 remove_link(comp, dobj, pass);
2764 break;
Bard liaoadfebb52019-02-01 11:07:40 -06002765 case SND_SOC_DOBJ_BACKEND_LINK:
2766 /*
2767 * call link_unload ops if extra
2768 * deinitialization is needed.
2769 */
2770 remove_backend_link(comp, dobj, pass);
2771 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002772 default:
2773 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2774 dobj->type);
2775 break;
2776 }
2777 }
2778 pass--;
2779 }
2780
2781 /* let caller know if FW can be freed when no objects are left */
2782 return !list_empty(&comp->dobj_list);
2783}
2784EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);