blob: c5ef432a023baecae314b54b25fc018e40d01bac [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);
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +020083static void soc_tplg_denum_remove_texts(struct soc_enum *se);
84static void soc_tplg_denum_remove_values(struct soc_enum *se);
Liam Girdwood8a978232015-05-29 19:06:14 +010085
86/* check we dont overflow the data for this control chunk */
87static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
88 unsigned int count, size_t bytes, const char *elem_type)
89{
90 const u8 *end = tplg->pos + elem_size * count;
91
92 if (end > tplg->fw->data + tplg->fw->size) {
93 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
94 elem_type);
95 return -EINVAL;
96 }
97
98 /* check there is enough room in chunk for control.
99 extra bytes at the end of control are for vendor data here */
100 if (elem_size * count > bytes) {
101 dev_err(tplg->dev,
102 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
103 elem_type, count, elem_size, bytes);
104 return -EINVAL;
105 }
106
107 return 0;
108}
109
110static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
111{
112 const u8 *end = tplg->hdr_pos;
113
114 if (end >= tplg->fw->data + tplg->fw->size)
115 return 1;
116 return 0;
117}
118
119static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
120{
121 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
122}
123
124static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
125{
126 return (unsigned long)(tplg->pos - tplg->fw->data);
127}
128
129/* mapping of Kcontrol types and associated operations. */
130static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
131 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
132 snd_soc_put_volsw, snd_soc_info_volsw},
133 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
134 snd_soc_put_volsw_sx, NULL},
135 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
136 snd_soc_put_enum_double, snd_soc_info_enum_double},
137 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
138 snd_soc_put_enum_double, NULL},
139 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
140 snd_soc_bytes_put, snd_soc_bytes_info},
141 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
142 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
143 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
144 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
145 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
146 snd_soc_put_strobe, NULL},
147 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530148 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100149 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
150 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
151 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
152 snd_soc_dapm_put_enum_double, NULL},
153 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
154 snd_soc_dapm_put_enum_double, NULL},
155 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
156 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
157};
158
159struct soc_tplg_map {
160 int uid;
161 int kid;
162};
163
164/* mapping of widget types from UAPI IDs to kernel IDs */
165static const struct soc_tplg_map dapm_map[] = {
166 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
167 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
168 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
169 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
170 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
171 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
172 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
173 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
174 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
175 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
176 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
177 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
178 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
179 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
180 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
181 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
Liam Girdwood8a70b452017-06-29 14:22:24 +0100182 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
183 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
184 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
185 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
186 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
187 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
188 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
189 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
Liam Girdwood8a978232015-05-29 19:06:14 +0100190};
191
192static int tplc_chan_get_reg(struct soc_tplg *tplg,
193 struct snd_soc_tplg_channel *chan, int map)
194{
195 int i;
196
197 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500198 if (le32_to_cpu(chan[i].id) == map)
199 return le32_to_cpu(chan[i].reg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100200 }
201
202 return -EINVAL;
203}
204
205static int tplc_chan_get_shift(struct soc_tplg *tplg,
206 struct snd_soc_tplg_channel *chan, int map)
207{
208 int i;
209
210 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500211 if (le32_to_cpu(chan[i].id) == map)
212 return le32_to_cpu(chan[i].shift);
Liam Girdwood8a978232015-05-29 19:06:14 +0100213 }
214
215 return -EINVAL;
216}
217
218static int get_widget_id(int tplg_type)
219{
220 int i;
221
222 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
223 if (tplg_type == dapm_map[i].uid)
224 return dapm_map[i].kid;
225 }
226
227 return -EINVAL;
228}
229
Liam Girdwood8a978232015-05-29 19:06:14 +0100230static inline void soc_bind_err(struct soc_tplg *tplg,
231 struct snd_soc_tplg_ctl_hdr *hdr, int index)
232{
233 dev_err(tplg->dev,
234 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
235 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
236 soc_tplg_get_offset(tplg));
237}
238
239static inline void soc_control_err(struct soc_tplg *tplg,
240 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
241{
242 dev_err(tplg->dev,
243 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
244 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
245 soc_tplg_get_offset(tplg));
246}
247
248/* pass vendor data to component driver for processing */
Keyon Jiec2cbd0a2020-05-27 10:28:01 +0800249static int soc_tplg_vendor_load(struct soc_tplg *tplg,
250 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +0100251{
252 int ret = 0;
253
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -0400254 if (tplg->ops && tplg->ops->vendor_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100255 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100256 else {
257 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
258 hdr->vendor_type);
259 return -EINVAL;
260 }
261
262 if (ret < 0)
263 dev_err(tplg->dev,
264 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
265 soc_tplg_get_hdr_offset(tplg),
266 soc_tplg_get_hdr_offset(tplg),
267 hdr->type, hdr->vendor_type);
268 return ret;
269}
270
Liam Girdwood8a978232015-05-29 19:06:14 +0100271/* optionally pass new dynamic widget to component driver. This is mainly for
272 * external widgets where we can assign private data/ops */
273static int soc_tplg_widget_load(struct soc_tplg *tplg,
274 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
275{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -0400276 if (tplg->ops && tplg->ops->widget_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100277 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
278 tplg_w);
Liam Girdwood8a978232015-05-29 19:06:14 +0100279
280 return 0;
281}
282
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100283/* optionally pass new dynamic widget to component driver. This is mainly for
284 * external widgets where we can assign private data/ops */
285static int soc_tplg_widget_ready(struct soc_tplg *tplg,
286 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
287{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -0400288 if (tplg->ops && tplg->ops->widget_ready)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100289 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
290 tplg_w);
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100291
292 return 0;
293}
294
Masahiro Yamada183b8022017-02-27 14:29:20 -0800295/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800296static int soc_tplg_dai_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100297 struct snd_soc_dai_driver *dai_drv,
298 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
Liam Girdwood8a978232015-05-29 19:06:14 +0100299{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -0400300 if (tplg->ops && tplg->ops->dai_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100301 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
302 pcm, dai);
Liam Girdwood8a978232015-05-29 19:06:14 +0100303
304 return 0;
305}
306
Masahiro Yamada183b8022017-02-27 14:29:20 -0800307/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800308static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100309 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
Mengdong Linacfc7d42016-01-15 16:13:37 +0800310{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -0400311 if (tplg->ops && tplg->ops->link_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100312 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800313
314 return 0;
315}
316
Liam Girdwood8a978232015-05-29 19:06:14 +0100317/* tell the component driver that all firmware has been loaded in this request */
318static void soc_tplg_complete(struct soc_tplg *tplg)
319{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -0400320 if (tplg->ops && tplg->ops->complete)
Liam Girdwood8a978232015-05-29 19:06:14 +0100321 tplg->ops->complete(tplg->comp);
322}
323
324/* add a dynamic kcontrol */
325static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
326 const struct snd_kcontrol_new *control_new, const char *prefix,
327 void *data, struct snd_kcontrol **kcontrol)
328{
329 int err;
330
331 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
332 if (*kcontrol == NULL) {
333 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
334 control_new->name);
335 return -ENOMEM;
336 }
337
338 err = snd_ctl_add(card, *kcontrol);
339 if (err < 0) {
340 dev_err(dev, "ASoC: Failed to add %s: %d\n",
341 control_new->name, err);
342 return err;
343 }
344
345 return 0;
346}
347
348/* add a dynamic kcontrol for component driver */
349static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
350 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
351{
352 struct snd_soc_component *comp = tplg->comp;
353
354 return soc_tplg_add_dcontrol(comp->card->snd_card,
이경택abca9e4a2020-04-01 18:05:24 +0900355 comp->dev, k, comp->name_prefix, comp, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100356}
357
358/* remove a mixer kcontrol */
359static void remove_mixer(struct snd_soc_component *comp,
360 struct snd_soc_dobj *dobj, int pass)
361{
362 struct snd_card *card = comp->card->snd_card;
363 struct soc_mixer_control *sm =
364 container_of(dobj, struct soc_mixer_control, dobj);
365 const unsigned int *p = NULL;
366
367 if (pass != SOC_TPLG_PASS_MIXER)
368 return;
369
370 if (dobj->ops && dobj->ops->control_unload)
371 dobj->ops->control_unload(comp, dobj);
372
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600373 if (dobj->control.kcontrol->tlv.p)
374 p = dobj->control.kcontrol->tlv.p;
375 snd_ctl_remove(card, dobj->control.kcontrol);
376 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100377 kfree(sm);
378 kfree(p);
379}
380
381/* remove an enum kcontrol */
382static void remove_enum(struct snd_soc_component *comp,
383 struct snd_soc_dobj *dobj, int pass)
384{
385 struct snd_card *card = comp->card->snd_card;
386 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100387
388 if (pass != SOC_TPLG_PASS_MIXER)
389 return;
390
391 if (dobj->ops && dobj->ops->control_unload)
392 dobj->ops->control_unload(comp, dobj);
393
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600394 snd_ctl_remove(card, dobj->control.kcontrol);
395 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100396
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200397 soc_tplg_denum_remove_values(se);
398 soc_tplg_denum_remove_texts(se);
Liam Girdwood8a978232015-05-29 19:06:14 +0100399 kfree(se);
400}
401
402/* remove a byte kcontrol */
403static void remove_bytes(struct snd_soc_component *comp,
404 struct snd_soc_dobj *dobj, int pass)
405{
406 struct snd_card *card = comp->card->snd_card;
407 struct soc_bytes_ext *sb =
408 container_of(dobj, struct soc_bytes_ext, dobj);
409
410 if (pass != SOC_TPLG_PASS_MIXER)
411 return;
412
413 if (dobj->ops && dobj->ops->control_unload)
414 dobj->ops->control_unload(comp, dobj);
415
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600416 snd_ctl_remove(card, dobj->control.kcontrol);
417 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100418 kfree(sb);
419}
420
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -0600421/* remove a route */
422static void remove_route(struct snd_soc_component *comp,
423 struct snd_soc_dobj *dobj, int pass)
424{
425 struct snd_soc_dapm_route *route =
426 container_of(dobj, struct snd_soc_dapm_route, dobj);
427
428 if (pass != SOC_TPLG_PASS_GRAPH)
429 return;
430
431 if (dobj->ops && dobj->ops->dapm_route_unload)
432 dobj->ops->dapm_route_unload(comp, dobj);
433
434 list_del(&dobj->list);
435 kfree(route);
436}
437
Liam Girdwood8a978232015-05-29 19:06:14 +0100438/* remove a widget and it's kcontrols - routes must be removed first */
439static void remove_widget(struct snd_soc_component *comp,
440 struct snd_soc_dobj *dobj, int pass)
441{
442 struct snd_card *card = comp->card->snd_card;
443 struct snd_soc_dapm_widget *w =
444 container_of(dobj, struct snd_soc_dapm_widget, dobj);
445 int i;
446
447 if (pass != SOC_TPLG_PASS_WIDGET)
448 return;
449
450 if (dobj->ops && dobj->ops->widget_unload)
451 dobj->ops->widget_unload(comp, dobj);
452
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000453 if (!w->kcontrols)
454 goto free_news;
455
Liam Girdwood8a978232015-05-29 19:06:14 +0100456 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800457 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100458 * The enum may either have an array of values or strings.
459 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800460 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100461 /* enumerated widget mixer */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100462 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800463 struct snd_kcontrol *kcontrol = w->kcontrols[i];
464 struct soc_enum *se =
465 (struct soc_enum *)kcontrol->private_value;
Liam Girdwood8a978232015-05-29 19:06:14 +0100466
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800467 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100468
Ranjani Sridharan54f88442019-04-04 19:48:33 -0700469 /* free enum kcontrol's dvalues and dtexts */
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200470 soc_tplg_denum_remove_values(se);
471 soc_tplg_denum_remove_texts(se);
Liam Girdwood8a978232015-05-29 19:06:14 +0100472
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800473 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100474 kfree(w->kcontrol_news[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800475 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100476 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800477 /* volume mixer or bytes controls */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100478 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100479 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100480
Mengdong Lineea3dd42016-11-25 16:09:17 +0800481 if (dobj->widget.kcontrol_type
482 == SND_SOC_TPLG_TYPE_MIXER)
483 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100484
Mengdong Lineea3dd42016-11-25 16:09:17 +0800485 /* Private value is used as struct soc_mixer_control
486 * for volume mixers or soc_bytes_ext for bytes
487 * controls.
488 */
489 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000490 snd_ctl_remove(card, kcontrol);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100491 kfree(w->kcontrol_news[i].name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100492 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100493 }
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000494
495free_news:
496 kfree(w->kcontrol_news);
497
Amadeusz Sławińskia46e8392019-01-25 14:06:43 -0600498 list_del(&dobj->list);
499
Liam Girdwood8a978232015-05-29 19:06:14 +0100500 /* widget w is freed by soc-dapm.c */
501}
502
Mengdong Lin64527e82016-01-15 16:13:28 +0800503/* remove DAI configurations */
504static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100505 struct snd_soc_dobj *dobj, int pass)
506{
Mengdong Lin64527e82016-01-15 16:13:28 +0800507 struct snd_soc_dai_driver *dai_drv =
508 container_of(dobj, struct snd_soc_dai_driver, dobj);
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600509 struct snd_soc_dai *dai;
Mengdong Lin64527e82016-01-15 16:13:28 +0800510
Liam Girdwood8a978232015-05-29 19:06:14 +0100511 if (pass != SOC_TPLG_PASS_PCM_DAI)
512 return;
513
Mengdong Lin64527e82016-01-15 16:13:28 +0800514 if (dobj->ops && dobj->ops->dai_unload)
515 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100516
Kuninori Morimoto43ca5da2019-08-20 14:05:32 +0900517 for_each_component_dais(comp, dai)
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600518 if (dai->driver == dai_drv)
519 dai->driver = NULL;
520
Bard liao7b6f68a2019-03-05 23:57:52 +0800521 kfree(dai_drv->playback.stream_name);
522 kfree(dai_drv->capture.stream_name);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800523 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100524 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800525 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100526}
527
Mengdong Linacfc7d42016-01-15 16:13:37 +0800528/* remove link configurations */
529static void remove_link(struct snd_soc_component *comp,
530 struct snd_soc_dobj *dobj, int pass)
531{
532 struct snd_soc_dai_link *link =
533 container_of(dobj, struct snd_soc_dai_link, dobj);
534
535 if (pass != SOC_TPLG_PASS_PCM_DAI)
536 return;
537
538 if (dobj->ops && dobj->ops->link_unload)
539 dobj->ops->link_unload(comp, dobj);
540
Dragos Tarcatudd836dd2019-12-04 15:04:47 -0600541 list_del(&dobj->list);
Kuninori Morimoto50cd9b52019-12-10 09:34:23 +0900542 snd_soc_remove_pcm_runtime(comp->card,
543 snd_soc_get_pcm_runtime(comp->card, link));
Dragos Tarcatudd836dd2019-12-04 15:04:47 -0600544
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800545 kfree(link->name);
546 kfree(link->stream_name);
Kuninori Morimoto23b946c2019-06-06 13:19:14 +0900547 kfree(link->cpus->dai_name);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800548 kfree(link);
549}
550
Bard liaoadfebb52019-02-01 11:07:40 -0600551/* unload dai link */
552static void remove_backend_link(struct snd_soc_component *comp,
553 struct snd_soc_dobj *dobj, int pass)
554{
555 if (pass != SOC_TPLG_PASS_LINK)
556 return;
557
558 if (dobj->ops && dobj->ops->link_unload)
559 dobj->ops->link_unload(comp, dobj);
560
561 /*
562 * We don't free the link here as what remove_link() do since BE
563 * links are not allocated by topology.
564 * We however need to reset the dobj type to its initial values
565 */
566 dobj->type = SND_SOC_DOBJ_NONE;
567 list_del(&dobj->list);
568}
569
Liam Girdwood8a978232015-05-29 19:06:14 +0100570/* bind a kcontrol to it's IO handlers */
571static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
572 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800573 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100574{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800575 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800576 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800577 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100578
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500579 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800580 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
581 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
582 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
583 struct soc_bytes_ext *sbe;
584 struct snd_soc_tplg_bytes_control *be;
585
586 sbe = (struct soc_bytes_ext *)k->private_value;
587 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
588
589 /* TLV bytes controls need standard kcontrol info handler,
590 * TLV callback and extended put/get handlers.
591 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530592 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800593 k->tlv.c = snd_soc_bytes_tlv_callback;
594
Pierre-Louis Bossart6788fc12020-09-17 13:39:12 +0300595 /*
596 * When a topology-based implementation abuses the
597 * control interface and uses bytes_ext controls of
598 * more than 512 bytes, we need to disable the size
599 * checks, otherwise accesses to such controls will
600 * return an -EINVAL error and prevent the card from
601 * being configured.
602 */
603 if (IS_ENABLED(CONFIG_SND_CTL_VALIDATION) && sbe->max > 512)
604 k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
605
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800606 ext_ops = tplg->bytes_ext_ops;
607 num_ops = tplg->bytes_ext_ops_count;
608 for (i = 0; i < num_ops; i++) {
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600609 if (!sbe->put &&
610 ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800611 sbe->put = ext_ops[i].put;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600612 if (!sbe->get &&
613 ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800614 sbe->get = ext_ops[i].get;
615 }
616
Dharageswari R819b9f62020-09-08 12:28:24 +0300617 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800618 return -EINVAL;
Dharageswari R819b9f62020-09-08 12:28:24 +0300619 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
620 return -EINVAL;
621 return 0;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800622 }
623
Mengdong Lin88a17d82015-08-18 18:11:51 +0800624 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800625 ops = tplg->io_ops;
626 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100627 for (i = 0; i < num_ops; i++) {
628
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600629 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
Liam Girdwood8a978232015-05-29 19:06:14 +0100630 k->put = ops[i].put;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600631 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
Liam Girdwood8a978232015-05-29 19:06:14 +0100632 k->get = ops[i].get;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600633 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800634 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100635 }
636
Mengdong Lin88a17d82015-08-18 18:11:51 +0800637 /* vendor specific handlers found ? */
638 if (k->put && k->get && k->info)
639 return 0;
640
641 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800642 ops = io_ops;
643 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800644 for (i = 0; i < num_ops; i++) {
645
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600646 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
Mengdong Lin88a17d82015-08-18 18:11:51 +0800647 k->put = ops[i].put;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600648 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
Mengdong Lin88a17d82015-08-18 18:11:51 +0800649 k->get = ops[i].get;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600650 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
Liam Girdwood8a978232015-05-29 19:06:14 +0100651 k->info = ops[i].info;
652 }
653
654 /* standard handlers found ? */
655 if (k->put && k->get && k->info)
656 return 0;
657
Liam Girdwood8a978232015-05-29 19:06:14 +0100658 /* nothing to bind */
659 return -EINVAL;
660}
661
662/* bind a widgets to it's evnt handlers */
663int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
664 const struct snd_soc_tplg_widget_events *events,
665 int num_events, u16 event_type)
666{
667 int i;
668
669 w->event = NULL;
670
671 for (i = 0; i < num_events; i++) {
672 if (event_type == events[i].type) {
673
674 /* found - so assign event */
675 w->event = events[i].event_handler;
676 return 0;
677 }
678 }
679
680 /* not found */
681 return -EINVAL;
682}
683EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
684
685/* optionally pass new dynamic kcontrol to component driver. */
686static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
687 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
688{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -0400689 if (tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100690 return tplg->ops->control_load(tplg->comp, tplg->index, k,
691 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100692
693 return 0;
694}
695
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100696
697static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
698 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100699{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100700 unsigned int item_len = 2 * sizeof(unsigned int);
701 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100702
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100703 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
704 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100705 return -ENOMEM;
706
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100707 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
708 p[1] = item_len;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500709 p[2] = le32_to_cpu(scale->min);
710 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
711 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100712
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100713 kc->tlv.p = (void *)p;
714 return 0;
715}
716
717static int soc_tplg_create_tlv(struct soc_tplg *tplg,
718 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
719{
720 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500721 u32 access = le32_to_cpu(tc->access);
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100722
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500723 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100724 return 0;
725
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500726 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100727 tplg_tlv = &tc->tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500728 switch (le32_to_cpu(tplg_tlv->type)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100729 case SNDRV_CTL_TLVT_DB_SCALE:
730 return soc_tplg_create_tlv_db_scale(tplg, kc,
731 &tplg_tlv->scale);
732
733 /* TODO: add support for other TLV types */
734 default:
735 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
736 tplg_tlv->type);
737 return -EINVAL;
738 }
739 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100740
741 return 0;
742}
743
744static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
745 struct snd_kcontrol_new *kc)
746{
747 kfree(kc->tlv.p);
748}
749
750static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
751 size_t size)
752{
753 struct snd_soc_tplg_bytes_control *be;
754 struct soc_bytes_ext *sbe;
755 struct snd_kcontrol_new kc;
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500756 int i;
757 int err = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +0100758
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);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500802 break;
Liam Girdwood8a978232015-05-29 19:06:14 +0100803 }
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);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500812 break;
Liam Girdwood8a978232015-05-29 19:06:14 +0100813 }
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);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500822 break;
Liam Girdwood8a978232015-05-29 19:06:14 +0100823 }
824
825 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
826 }
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500827 return err;
Liam Girdwood8a978232015-05-29 19:06:14 +0100828
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;
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500837 int i;
838 int err = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +0100839
840 if (soc_tplg_check_elem_count(tplg,
841 sizeof(struct snd_soc_tplg_mixer_control),
842 count, size, "mixers")) {
843
844 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
845 count);
846 return -EINVAL;
847 }
848
849 for (i = 0; i < count; i++) {
850 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
851
852 /* validate kcontrol */
853 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
854 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
855 return -EINVAL;
856
857 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
858 if (sm == NULL)
859 return -ENOMEM;
860 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500861 le32_to_cpu(mc->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100862
863 dev_dbg(tplg->dev,
864 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
865 mc->hdr.name, mc->hdr.access);
866
867 memset(&kc, 0, sizeof(kc));
868 kc.name = mc->hdr.name;
869 kc.private_value = (long)sm;
870 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500871 kc.access = le32_to_cpu(mc->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100872
873 /* we only support FL/FR channel mapping atm */
874 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
875 SNDRV_CHMAP_FL);
876 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
877 SNDRV_CHMAP_FR);
878 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
879 SNDRV_CHMAP_FL);
880 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
881 SNDRV_CHMAP_FR);
882
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500883 sm->max = le32_to_cpu(mc->max);
884 sm->min = le32_to_cpu(mc->min);
885 sm->invert = le32_to_cpu(mc->invert);
886 sm->platform_max = le32_to_cpu(mc->platform_max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100887 sm->dobj.index = tplg->index;
888 sm->dobj.ops = tplg->ops;
889 sm->dobj.type = SND_SOC_DOBJ_MIXER;
890 INIT_LIST_HEAD(&sm->dobj.list);
891
892 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800893 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100894 if (err) {
895 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
896 kfree(sm);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500897 break;
Liam Girdwood8a978232015-05-29 19:06:14 +0100898 }
899
Bard liao3789deb2019-03-13 21:49:43 +0800900 /* create any TLV data */
Amadeusz Sławiński482db552020-03-27 16:47:25 -0400901 err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
902 if (err < 0) {
903 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
904 mc->hdr.name);
905 kfree(sm);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500906 break;
Amadeusz Sławiński482db552020-03-27 16:47:25 -0400907 }
Bard liao3789deb2019-03-13 21:49:43 +0800908
Liam Girdwood8a978232015-05-29 19:06:14 +0100909 /* pass control to driver for optional further init */
910 err = soc_tplg_init_kcontrol(tplg, &kc,
911 (struct snd_soc_tplg_ctl_hdr *) mc);
912 if (err < 0) {
913 dev_err(tplg->dev, "ASoC: failed to init %s\n",
914 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +0800915 soc_tplg_free_tlv(tplg, &kc);
Liam Girdwood8a978232015-05-29 19:06:14 +0100916 kfree(sm);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500917 break;
Liam Girdwood8a978232015-05-29 19:06:14 +0100918 }
919
Liam Girdwood8a978232015-05-29 19:06:14 +0100920 /* register control here */
921 err = soc_tplg_add_kcontrol(tplg, &kc,
922 &sm->dobj.control.kcontrol);
923 if (err < 0) {
924 dev_err(tplg->dev, "ASoC: failed to add %s\n",
925 mc->hdr.name);
926 soc_tplg_free_tlv(tplg, &kc);
927 kfree(sm);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500928 break;
Liam Girdwood8a978232015-05-29 19:06:14 +0100929 }
930
931 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
932 }
933
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -0500934 return err;
Liam Girdwood8a978232015-05-29 19:06:14 +0100935}
936
937static int soc_tplg_denum_create_texts(struct soc_enum *se,
938 struct snd_soc_tplg_enum_control *ec)
939{
940 int i, ret;
941
942 se->dobj.control.dtexts =
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500943 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100944 if (se->dobj.control.dtexts == NULL)
945 return -ENOMEM;
946
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600947 for (i = 0; i < le32_to_cpu(ec->items); i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100948
949 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
950 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
951 ret = -EINVAL;
952 goto err;
953 }
954
955 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
956 if (!se->dobj.control.dtexts[i]) {
957 ret = -ENOMEM;
958 goto err;
959 }
960 }
961
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200962 se->items = le32_to_cpu(ec->items);
Mousumi Janab6e38b22017-04-11 13:06:22 +0530963 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100964 return 0;
965
966err:
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200967 se->items = i;
968 soc_tplg_denum_remove_texts(se);
969 return ret;
970}
971
972static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
973{
974 int i = se->items;
975
Liam Girdwood8a978232015-05-29 19:06:14 +0100976 for (--i; i >= 0; i--)
977 kfree(se->dobj.control.dtexts[i]);
978 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +0100979}
980
981static int soc_tplg_denum_create_values(struct soc_enum *se,
982 struct snd_soc_tplg_enum_control *ec)
983{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500984 int i;
985
986 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
Liam Girdwood8a978232015-05-29 19:06:14 +0100987 return -EINVAL;
988
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500989 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
990 sizeof(u32),
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200991 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100992 if (!se->dobj.control.dvalues)
993 return -ENOMEM;
994
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500995 /* convert from little-endian */
996 for (i = 0; i < le32_to_cpu(ec->items); i++) {
997 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
998 }
999
Liam Girdwood8a978232015-05-29 19:06:14 +01001000 return 0;
1001}
1002
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +02001003static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
1004{
1005 kfree(se->dobj.control.dvalues);
1006}
1007
Liam Girdwood8a978232015-05-29 19:06:14 +01001008static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
1009 size_t size)
1010{
1011 struct snd_soc_tplg_enum_control *ec;
1012 struct soc_enum *se;
1013 struct snd_kcontrol_new kc;
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001014 int i;
1015 int err = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001016
1017 if (soc_tplg_check_elem_count(tplg,
1018 sizeof(struct snd_soc_tplg_enum_control),
1019 count, size, "enums")) {
1020
1021 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1022 count);
1023 return -EINVAL;
1024 }
1025
1026 for (i = 0; i < count; i++) {
1027 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001028
1029 /* validate kcontrol */
1030 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1031 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1032 return -EINVAL;
1033
1034 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1035 if (se == NULL)
1036 return -ENOMEM;
1037
Liam Girdwood02b64242019-03-01 19:08:52 -06001038 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001039 le32_to_cpu(ec->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001040
Liam Girdwood8a978232015-05-29 19:06:14 +01001041 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1042 ec->hdr.name, ec->items);
1043
1044 memset(&kc, 0, sizeof(kc));
1045 kc.name = ec->hdr.name;
1046 kc.private_value = (long)se;
1047 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001048 kc.access = le32_to_cpu(ec->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001049
1050 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1051 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1052 SNDRV_CHMAP_FL);
1053 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1054 SNDRV_CHMAP_FL);
1055
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001056 se->mask = le32_to_cpu(ec->mask);
Liam Girdwood8a978232015-05-29 19:06:14 +01001057 se->dobj.index = tplg->index;
1058 se->dobj.type = SND_SOC_DOBJ_ENUM;
1059 se->dobj.ops = tplg->ops;
1060 INIT_LIST_HEAD(&se->dobj.list);
1061
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001062 switch (le32_to_cpu(ec->hdr.ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001063 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1064 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1065 err = soc_tplg_denum_create_values(se, ec);
1066 if (err < 0) {
1067 dev_err(tplg->dev,
1068 "ASoC: could not create values for %s\n",
1069 ec->hdr.name);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001070 goto err_denum;
Liam Girdwood8a978232015-05-29 19:06:14 +01001071 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001072 fallthrough;
Liam Girdwood8a978232015-05-29 19:06:14 +01001073 case SND_SOC_TPLG_CTL_ENUM:
1074 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1075 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1076 err = soc_tplg_denum_create_texts(se, ec);
1077 if (err < 0) {
1078 dev_err(tplg->dev,
1079 "ASoC: could not create texts for %s\n",
1080 ec->hdr.name);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001081 goto err_denum;
Liam Girdwood8a978232015-05-29 19:06:14 +01001082 }
1083 break;
1084 default:
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001085 err = -EINVAL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001086 dev_err(tplg->dev,
1087 "ASoC: invalid enum control type %d for %s\n",
1088 ec->hdr.ops.info, ec->hdr.name);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001089 goto err_denum;
Liam Girdwood8a978232015-05-29 19:06:14 +01001090 }
1091
1092 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001093 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001094 if (err) {
1095 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001096 goto err_denum;
Liam Girdwood8a978232015-05-29 19:06:14 +01001097 }
1098
1099 /* pass control to driver for optional further init */
1100 err = soc_tplg_init_kcontrol(tplg, &kc,
1101 (struct snd_soc_tplg_ctl_hdr *) ec);
1102 if (err < 0) {
1103 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1104 ec->hdr.name);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001105 goto err_denum;
Liam Girdwood8a978232015-05-29 19:06:14 +01001106 }
1107
1108 /* register control here */
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001109 err = soc_tplg_add_kcontrol(tplg,
1110 &kc, &se->dobj.control.kcontrol);
1111 if (err < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001112 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1113 ec->hdr.name);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001114 goto err_denum;
Liam Girdwood8a978232015-05-29 19:06:14 +01001115 }
1116
1117 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1118 }
Pierre-Louis Bossart952bd932020-07-07 15:37:48 -05001119 return 0;
1120
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001121err_denum:
Pierre-Louis Bossart952bd932020-07-07 15:37:48 -05001122 kfree(se);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001123 return err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001124}
1125
1126static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1127 struct snd_soc_tplg_hdr *hdr)
1128{
1129 struct snd_soc_tplg_ctl_hdr *control_hdr;
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001130 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001131 int i;
1132
Liam Girdwood8a978232015-05-29 19:06:14 +01001133 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1134 soc_tplg_get_offset(tplg));
1135
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001136 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001137
1138 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1139
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001140 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001141 dev_err(tplg->dev, "ASoC: invalid control size\n");
1142 return -EINVAL;
1143 }
1144
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001145 switch (le32_to_cpu(control_hdr->ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001146 case SND_SOC_TPLG_CTL_VOLSW:
1147 case SND_SOC_TPLG_CTL_STROBE:
1148 case SND_SOC_TPLG_CTL_VOLSW_SX:
1149 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1150 case SND_SOC_TPLG_CTL_RANGE:
1151 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1152 case SND_SOC_TPLG_DAPM_CTL_PIN:
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001153 ret = soc_tplg_dmixer_create(tplg, 1,
1154 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001155 break;
1156 case SND_SOC_TPLG_CTL_ENUM:
1157 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1158 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1159 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1160 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001161 ret = soc_tplg_denum_create(tplg, 1,
1162 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001163 break;
1164 case SND_SOC_TPLG_CTL_BYTES:
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001165 ret = soc_tplg_dbytes_create(tplg, 1,
1166 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001167 break;
1168 default:
1169 soc_bind_err(tplg, control_hdr, i);
1170 return -EINVAL;
1171 }
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001172 if (ret < 0) {
1173 dev_err(tplg->dev, "ASoC: invalid control\n");
1174 return ret;
1175 }
1176
Liam Girdwood8a978232015-05-29 19:06:14 +01001177 }
1178
1179 return 0;
1180}
1181
Liam Girdwood503e79b2018-06-14 20:53:59 +01001182/* optionally pass new dynamic kcontrol to component driver. */
1183static int soc_tplg_add_route(struct soc_tplg *tplg,
1184 struct snd_soc_dapm_route *route)
1185{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -04001186 if (tplg->ops && tplg->ops->dapm_route_load)
Liam Girdwood503e79b2018-06-14 20:53:59 +01001187 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1188 route);
1189
1190 return 0;
1191}
1192
Liam Girdwood8a978232015-05-29 19:06:14 +01001193static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1194 struct snd_soc_tplg_hdr *hdr)
1195{
1196 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001197 struct snd_soc_tplg_dapm_graph_elem *elem;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001198 struct snd_soc_dapm_route **routes;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001199 int count, i, j;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001200 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001201
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001202 count = le32_to_cpu(hdr->count);
1203
Liam Girdwood8a978232015-05-29 19:06:14 +01001204 if (soc_tplg_check_elem_count(tplg,
1205 sizeof(struct snd_soc_tplg_dapm_graph_elem),
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001206 count, le32_to_cpu(hdr->payload_size), "graph")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001207
1208 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1209 count);
1210 return -EINVAL;
1211 }
1212
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001213 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1214 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001215
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001216 /* allocate memory for pointer to array of dapm routes */
1217 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1218 GFP_KERNEL);
1219 if (!routes)
1220 return -ENOMEM;
1221
1222 /*
1223 * allocate memory for each dapm route in the array.
1224 * This needs to be done individually so that
1225 * each route can be freed when it is removed in remove_route().
1226 */
1227 for (i = 0; i < count; i++) {
1228 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1229 if (!routes[i]) {
1230 /* free previously allocated memory */
1231 for (j = 0; j < i; j++)
1232 kfree(routes[j]);
1233
1234 kfree(routes);
1235 return -ENOMEM;
1236 }
1237 }
1238
Liam Girdwood8a978232015-05-29 19:06:14 +01001239 for (i = 0; i < count; i++) {
1240 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1241 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1242
1243 /* validate routes */
1244 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001245 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1246 ret = -EINVAL;
1247 break;
1248 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001249 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001250 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1251 ret = -EINVAL;
1252 break;
1253 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001254 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001255 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1256 ret = -EINVAL;
1257 break;
1258 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001259
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001260 routes[i]->source = elem->source;
1261 routes[i]->sink = elem->sink;
1262
1263 /* set to NULL atm for tplg users */
1264 routes[i]->connected = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001265 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001266 routes[i]->control = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001267 else
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001268 routes[i]->control = elem->control;
Liam Girdwood8a978232015-05-29 19:06:14 +01001269
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001270 /* add route dobj to dobj_list */
1271 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1272 routes[i]->dobj.ops = tplg->ops;
1273 routes[i]->dobj.index = tplg->index;
1274 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1275
Amadeusz Sławiński6856e882020-03-27 16:47:27 -04001276 ret = soc_tplg_add_route(tplg, routes[i]);
Pierre-Louis Bossart6f0307d2020-07-07 15:37:45 -05001277 if (ret < 0) {
Pierre-Louis Bossart8bf94752020-07-07 15:37:49 -05001278 dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
Pierre-Louis Bossart6f0307d2020-07-07 15:37:45 -05001279 /*
1280 * this route was added to the list, it will
1281 * be freed in remove_route() so increment the
1282 * counter to skip it in the error handling
1283 * below.
1284 */
1285 i++;
Amadeusz Sławiński6856e882020-03-27 16:47:27 -04001286 break;
Pierre-Louis Bossart6f0307d2020-07-07 15:37:45 -05001287 }
Liam Girdwood503e79b2018-06-14 20:53:59 +01001288
Liam Girdwood8a978232015-05-29 19:06:14 +01001289 /* add route, but keep going if some fail */
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001290 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Liam Girdwood8a978232015-05-29 19:06:14 +01001291 }
1292
Pierre-Louis Bossart6f0307d2020-07-07 15:37:45 -05001293 /*
1294 * free memory allocated for all dapm routes not added to the
1295 * list in case of error
1296 */
1297 if (ret < 0) {
1298 while (i < count)
1299 kfree(routes[i++]);
1300 }
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001301
1302 /*
1303 * free pointer to array of dapm routes as this is no longer needed.
1304 * The memory allocated for each dapm route will be freed
1305 * when it is removed in remove_route().
1306 */
1307 kfree(routes);
1308
1309 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001310}
1311
1312static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1313 struct soc_tplg *tplg, int num_kcontrols)
1314{
1315 struct snd_kcontrol_new *kc;
1316 struct soc_mixer_control *sm;
1317 struct snd_soc_tplg_mixer_control *mc;
1318 int i, err;
1319
Axel Lin4ca7deb2015-08-05 22:34:22 +08001320 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001321 if (kc == NULL)
1322 return NULL;
1323
1324 for (i = 0; i < num_kcontrols; i++) {
1325 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001326
Liam Girdwood8a978232015-05-29 19:06:14 +01001327 /* validate kcontrol */
1328 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1329 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001330 goto err_sm;
1331
1332 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1333 if (sm == NULL)
1334 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001335
Liam Girdwood02b64242019-03-01 19:08:52 -06001336 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001337 le32_to_cpu(mc->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001338
Liam Girdwood8a978232015-05-29 19:06:14 +01001339 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1340 mc->hdr.name, i);
1341
Colin Ian King1ad741d2019-06-27 14:32:08 +01001342 kc[i].private_value = (long)sm;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001343 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1344 if (kc[i].name == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001345 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001346 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001347 kc[i].access = le32_to_cpu(mc->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001348
1349 /* we only support FL/FR channel mapping atm */
1350 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1351 SNDRV_CHMAP_FL);
1352 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1353 SNDRV_CHMAP_FR);
1354 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1355 SNDRV_CHMAP_FL);
1356 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1357 SNDRV_CHMAP_FR);
1358
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001359 sm->max = le32_to_cpu(mc->max);
1360 sm->min = le32_to_cpu(mc->min);
1361 sm->invert = le32_to_cpu(mc->invert);
1362 sm->platform_max = le32_to_cpu(mc->platform_max);
Liam Girdwood8a978232015-05-29 19:06:14 +01001363 sm->dobj.index = tplg->index;
1364 INIT_LIST_HEAD(&sm->dobj.list);
1365
1366 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001367 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001368 if (err) {
1369 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001370 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001371 }
1372
Bard liao3789deb2019-03-13 21:49:43 +08001373 /* create any TLV data */
Amadeusz Sławiński482db552020-03-27 16:47:25 -04001374 err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1375 if (err < 0) {
1376 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1377 mc->hdr.name);
Pierre-Louis Bossart129fc2b2020-07-07 15:37:47 -05001378 goto err_sm;
Amadeusz Sławiński482db552020-03-27 16:47:25 -04001379 }
Bard liao3789deb2019-03-13 21:49:43 +08001380
Liam Girdwood8a978232015-05-29 19:06:14 +01001381 /* pass control to driver for optional further init */
1382 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1383 (struct snd_soc_tplg_ctl_hdr *)mc);
1384 if (err < 0) {
1385 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1386 mc->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001387 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001388 }
1389 }
1390 return kc;
1391
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001392err_sm:
1393 for (; i >= 0; i--) {
Pierre-Louis Bossart8edac482020-07-07 15:37:46 -05001394 soc_tplg_free_tlv(tplg, &kc[i]);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001395 sm = (struct soc_mixer_control *)kc[i].private_value;
1396 kfree(sm);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001397 kfree(kc[i].name);
1398 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001399 kfree(kc);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001400
Liam Girdwood8a978232015-05-29 19:06:14 +01001401 return NULL;
1402}
1403
1404static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001405 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001406{
1407 struct snd_kcontrol_new *kc;
1408 struct snd_soc_tplg_enum_control *ec;
1409 struct soc_enum *se;
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +02001410 int i, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001411
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001412 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001413 if (kc == NULL)
1414 return NULL;
1415
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001416 for (i = 0; i < num_kcontrols; i++) {
1417 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1418 /* validate kcontrol */
1419 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1420 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001421 goto err_se;
Liam Girdwood8a978232015-05-29 19:06:14 +01001422
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001423 se = kzalloc(sizeof(*se), GFP_KERNEL);
1424 if (se == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001425 goto err_se;
Liam Girdwood8a978232015-05-29 19:06:14 +01001426
Liam Girdwood02b64242019-03-01 19:08:52 -06001427 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001428 le32_to_cpu(ec->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001429
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001430 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001431 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001432
Colin Ian King1ad741d2019-06-27 14:32:08 +01001433 kc[i].private_value = (long)se;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001434 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001435 if (kc[i].name == NULL)
Liam Girdwood267e2c62018-03-27 12:04:04 +01001436 goto err_se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001437 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001438 kc[i].access = le32_to_cpu(ec->hdr.access);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001439
1440 /* we only support FL/FR channel mapping atm */
1441 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1442 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1443 SNDRV_CHMAP_FL);
1444 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1445 SNDRV_CHMAP_FR);
1446
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001447 se->items = le32_to_cpu(ec->items);
1448 se->mask = le32_to_cpu(ec->mask);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001449 se->dobj.index = tplg->index;
1450
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001451 switch (le32_to_cpu(ec->hdr.ops.info)) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001452 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1453 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1454 err = soc_tplg_denum_create_values(se, ec);
1455 if (err < 0) {
1456 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1457 ec->hdr.name);
1458 goto err_se;
1459 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001460 fallthrough;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001461 case SND_SOC_TPLG_CTL_ENUM:
1462 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1463 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1464 err = soc_tplg_denum_create_texts(se, ec);
1465 if (err < 0) {
1466 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1467 ec->hdr.name);
1468 goto err_se;
1469 }
1470 break;
1471 default:
1472 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1473 ec->hdr.ops.info, ec->hdr.name);
1474 goto err_se;
1475 }
1476
1477 /* map io handlers */
1478 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1479 if (err) {
1480 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1481 goto err_se;
1482 }
1483
1484 /* pass control to driver for optional further init */
1485 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1486 (struct snd_soc_tplg_ctl_hdr *)ec);
1487 if (err < 0) {
1488 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1489 ec->hdr.name);
1490 goto err_se;
1491 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001492 }
1493
1494 return kc;
1495
1496err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001497 for (; i >= 0; i--) {
1498 /* free values and texts */
1499 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001500
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001501 if (se) {
1502 soc_tplg_denum_remove_values(se);
1503 soc_tplg_denum_remove_texts(se);
1504 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001505
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001506 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001507 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001508 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001509 kfree(kc);
1510
1511 return NULL;
1512}
1513
1514static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001515 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001516{
1517 struct snd_soc_tplg_bytes_control *be;
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001518 struct soc_bytes_ext *sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001519 struct snd_kcontrol_new *kc;
1520 int i, err;
1521
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001522 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001523 if (!kc)
1524 return NULL;
1525
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001526 for (i = 0; i < num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001527 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1528
1529 /* validate kcontrol */
1530 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1531 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001532 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001533
1534 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1535 if (sbe == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001536 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001537
1538 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001539 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001540
1541 dev_dbg(tplg->dev,
1542 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1543 be->hdr.name, be->hdr.access);
1544
Colin Ian King1ad741d2019-06-27 14:32:08 +01001545 kc[i].private_value = (long)sbe;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001546 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001547 if (kc[i].name == NULL)
1548 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001549 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001550 kc[i].access = le32_to_cpu(be->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001551
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001552 sbe->max = le32_to_cpu(be->max);
Liam Girdwood8a978232015-05-29 19:06:14 +01001553 INIT_LIST_HEAD(&sbe->dobj.list);
1554
1555 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001556 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001557 if (err) {
1558 soc_control_err(tplg, &be->hdr, be->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001559 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001560 }
1561
1562 /* pass control to driver for optional further init */
1563 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1564 (struct snd_soc_tplg_ctl_hdr *)be);
1565 if (err < 0) {
1566 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1567 be->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001568 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001569 }
1570 }
1571
1572 return kc;
1573
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001574err_sbe:
1575 for (; i >= 0; i--) {
1576 sbe = (struct soc_bytes_ext *)kc[i].private_value;
1577 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001578 kfree(kc[i].name);
1579 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001580 kfree(kc);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001581
Liam Girdwood8a978232015-05-29 19:06:14 +01001582 return NULL;
1583}
1584
1585static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1586 struct snd_soc_tplg_dapm_widget *w)
1587{
1588 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1589 struct snd_soc_dapm_widget template, *widget;
1590 struct snd_soc_tplg_ctl_hdr *control_hdr;
1591 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001592 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001593 int ret = 0;
1594
1595 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1596 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1597 return -EINVAL;
1598 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1599 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1600 return -EINVAL;
1601
1602 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1603 w->name, w->id);
1604
1605 memset(&template, 0, sizeof(template));
1606
1607 /* map user to kernel widget ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001608 template.id = get_widget_id(le32_to_cpu(w->id));
Dan Carpenter752c9382019-09-25 14:06:24 +03001609 if ((int)template.id < 0)
Liam Girdwood8a978232015-05-29 19:06:14 +01001610 return template.id;
1611
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001612 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001613 template.name = kstrdup(w->name, GFP_KERNEL);
1614 if (!template.name)
1615 return -ENOMEM;
1616 template.sname = kstrdup(w->sname, GFP_KERNEL);
1617 if (!template.sname) {
1618 ret = -ENOMEM;
1619 goto err;
1620 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001621 template.reg = le32_to_cpu(w->reg);
1622 template.shift = le32_to_cpu(w->shift);
1623 template.mask = le32_to_cpu(w->mask);
1624 template.subseq = le32_to_cpu(w->subseq);
Liam Girdwood8a978232015-05-29 19:06:14 +01001625 template.on_val = w->invert ? 0 : 1;
1626 template.off_val = w->invert ? 1 : 0;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001627 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1628 template.event_flags = le16_to_cpu(w->event_flags);
Liam Girdwood8a978232015-05-29 19:06:14 +01001629 template.dobj.index = tplg->index;
1630
1631 tplg->pos +=
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001632 (sizeof(struct snd_soc_tplg_dapm_widget) +
1633 le32_to_cpu(w->priv.size));
1634
Liam Girdwood8a978232015-05-29 19:06:14 +01001635 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001636 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001637 template.num_kcontrols = 0;
1638 goto widget;
1639 }
1640
1641 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1642 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1643 w->name, w->num_kcontrols, control_hdr->type);
1644
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001645 switch (le32_to_cpu(control_hdr->ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001646 case SND_SOC_TPLG_CTL_VOLSW:
1647 case SND_SOC_TPLG_CTL_STROBE:
1648 case SND_SOC_TPLG_CTL_VOLSW_SX:
1649 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1650 case SND_SOC_TPLG_CTL_RANGE:
1651 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001652 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001653 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001654 template.kcontrol_news =
1655 soc_tplg_dapm_widget_dmixer_create(tplg,
1656 template.num_kcontrols);
1657 if (!template.kcontrol_news) {
1658 ret = -ENOMEM;
1659 goto hdr_err;
1660 }
1661 break;
1662 case SND_SOC_TPLG_CTL_ENUM:
1663 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1664 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1665 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1666 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001667 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001668 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001669 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001670 soc_tplg_dapm_widget_denum_create(tplg,
1671 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001672 if (!template.kcontrol_news) {
1673 ret = -ENOMEM;
1674 goto hdr_err;
1675 }
1676 break;
1677 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001678 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001679 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001680 template.kcontrol_news =
1681 soc_tplg_dapm_widget_dbytes_create(tplg,
1682 template.num_kcontrols);
1683 if (!template.kcontrol_news) {
1684 ret = -ENOMEM;
1685 goto hdr_err;
1686 }
1687 break;
1688 default:
1689 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1690 control_hdr->ops.get, control_hdr->ops.put,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001691 le32_to_cpu(control_hdr->ops.info));
Liam Girdwood8a978232015-05-29 19:06:14 +01001692 ret = -EINVAL;
1693 goto hdr_err;
1694 }
1695
1696widget:
1697 ret = soc_tplg_widget_load(tplg, &template, w);
1698 if (ret < 0)
1699 goto hdr_err;
1700
1701 /* card dapm mutex is held by the core if we are loading topology
1702 * data during sound card init. */
1703 if (card->instantiated)
1704 widget = snd_soc_dapm_new_control(dapm, &template);
1705 else
1706 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001707 if (IS_ERR(widget)) {
1708 ret = PTR_ERR(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001709 goto hdr_err;
1710 }
1711
1712 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001713 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001714 widget->dobj.ops = tplg->ops;
1715 widget->dobj.index = tplg->index;
1716 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001717
1718 ret = soc_tplg_widget_ready(tplg, widget, w);
1719 if (ret < 0)
1720 goto ready_err;
1721
Bard liao7620fe92019-01-25 14:06:45 -06001722 kfree(template.sname);
1723 kfree(template.name);
1724
Liam Girdwood8a978232015-05-29 19:06:14 +01001725 return 0;
1726
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001727ready_err:
1728 snd_soc_tplg_widget_remove(widget);
1729 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001730hdr_err:
1731 kfree(template.sname);
1732err:
1733 kfree(template.name);
1734 return ret;
1735}
1736
1737static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1738 struct snd_soc_tplg_hdr *hdr)
1739{
1740 struct snd_soc_tplg_dapm_widget *widget;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001741 int ret, count, i;
1742
1743 count = le32_to_cpu(hdr->count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001744
Liam Girdwood8a978232015-05-29 19:06:14 +01001745 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1746
1747 for (i = 0; i < count; i++) {
1748 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001749 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001750 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1751 return -EINVAL;
1752 }
1753
Liam Girdwood8a978232015-05-29 19:06:14 +01001754 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001755 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001756 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1757 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001758 return ret;
1759 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001760 }
1761
1762 return 0;
1763}
1764
1765static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1766{
1767 struct snd_soc_card *card = tplg->comp->card;
1768 int ret;
1769
1770 /* Card might not have been registered at this point.
1771 * If so, just return success.
1772 */
1773 if (!card || !card->instantiated) {
1774 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001775 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001776 return 0;
1777 }
1778
1779 ret = snd_soc_dapm_new_widgets(card);
1780 if (ret < 0)
1781 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1782 ret);
1783
1784 return 0;
1785}
1786
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001787static int set_stream_info(struct snd_soc_pcm_stream *stream,
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001788 struct snd_soc_tplg_stream_caps *caps)
1789{
1790 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001791 if (!stream->stream_name)
1792 return -ENOMEM;
1793
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001794 stream->channels_min = le32_to_cpu(caps->channels_min);
1795 stream->channels_max = le32_to_cpu(caps->channels_max);
1796 stream->rates = le32_to_cpu(caps->rates);
1797 stream->rate_min = le32_to_cpu(caps->rate_min);
1798 stream->rate_max = le32_to_cpu(caps->rate_max);
1799 stream->formats = le64_to_cpu(caps->formats);
1800 stream->sig_bits = le32_to_cpu(caps->sig_bits);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001801
1802 return 0;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001803}
1804
Mengdong Lin0038be92016-07-26 14:32:37 +08001805static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1806 unsigned int flag_mask, unsigned int flags)
1807{
1808 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1809 dai_drv->symmetric_rates =
1810 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1811
1812 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1813 dai_drv->symmetric_channels =
1814 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1815 1 : 0;
1816
1817 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1818 dai_drv->symmetric_samplebits =
1819 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1820 1 : 0;
1821}
1822
Mengdong Lin64527e82016-01-15 16:13:28 +08001823static int soc_tplg_dai_create(struct soc_tplg *tplg,
1824 struct snd_soc_tplg_pcm *pcm)
1825{
1826 struct snd_soc_dai_driver *dai_drv;
1827 struct snd_soc_pcm_stream *stream;
1828 struct snd_soc_tplg_stream_caps *caps;
Kuninori Morimotoe443c202019-11-05 15:47:14 +09001829 struct snd_soc_dai *dai;
1830 struct snd_soc_dapm_context *dapm =
1831 snd_soc_component_get_dapm(tplg->comp);
Mengdong Lin64527e82016-01-15 16:13:28 +08001832 int ret;
1833
1834 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1835 if (dai_drv == NULL)
1836 return -ENOMEM;
1837
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001838 if (strlen(pcm->dai_name)) {
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001839 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001840 if (!dai_drv->name) {
1841 ret = -ENOMEM;
1842 goto err;
1843 }
1844 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001845 dai_drv->id = le32_to_cpu(pcm->dai_id);
Mengdong Lin64527e82016-01-15 16:13:28 +08001846
1847 if (pcm->playback) {
1848 stream = &dai_drv->playback;
1849 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001850 ret = set_stream_info(stream, caps);
1851 if (ret < 0)
1852 goto err;
Mengdong Lin64527e82016-01-15 16:13:28 +08001853 }
1854
1855 if (pcm->capture) {
1856 stream = &dai_drv->capture;
1857 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001858 ret = set_stream_info(stream, caps);
1859 if (ret < 0)
1860 goto err;
Mengdong Lin64527e82016-01-15 16:13:28 +08001861 }
1862
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001863 if (pcm->compress)
1864 dai_drv->compress_new = snd_soc_new_compress;
1865
Mengdong Lin64527e82016-01-15 16:13:28 +08001866 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001867 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001868 if (ret < 0) {
1869 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001870 goto err;
Mengdong Lin64527e82016-01-15 16:13:28 +08001871 }
1872
1873 dai_drv->dobj.index = tplg->index;
1874 dai_drv->dobj.ops = tplg->ops;
1875 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1876 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1877
1878 /* register the DAI to the component */
Pierre-Louis Bossart6ae49022020-06-12 15:59:38 -05001879 dai = devm_snd_soc_register_dai(tplg->comp->dev, tplg->comp, dai_drv, false);
Kuninori Morimotoe443c202019-11-05 15:47:14 +09001880 if (!dai)
1881 return -ENOMEM;
1882
1883 /* Create the DAI widgets here */
1884 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1885 if (ret != 0) {
1886 dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
Kuninori Morimotoe443c202019-11-05 15:47:14 +09001887 return ret;
1888 }
1889
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001890 return 0;
1891
1892err:
1893 kfree(dai_drv->playback.stream_name);
1894 kfree(dai_drv->capture.stream_name);
1895 kfree(dai_drv->name);
1896 kfree(dai_drv);
1897
Kuninori Morimotoe443c202019-11-05 15:47:14 +09001898 return ret;
Mengdong Lin64527e82016-01-15 16:13:28 +08001899}
1900
Mengdong Lin717a8e72016-11-03 01:03:34 +08001901static void set_link_flags(struct snd_soc_dai_link *link,
1902 unsigned int flag_mask, unsigned int flags)
1903{
1904 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1905 link->symmetric_rates =
1906 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1907
1908 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1909 link->symmetric_channels =
1910 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1911 1 : 0;
1912
1913 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1914 link->symmetric_samplebits =
1915 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1916 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001917
1918 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1919 link->ignore_suspend =
1920 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1921 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001922}
1923
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001924/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001925static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001926 struct snd_soc_tplg_pcm *pcm)
1927{
1928 struct snd_soc_dai_link *link;
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001929 struct snd_soc_dai_link_component *dlc;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001930 int ret;
1931
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001932 /* link + cpu + codec + platform */
1933 link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001934 if (link == NULL)
1935 return -ENOMEM;
1936
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001937 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1938
1939 link->cpus = &dlc[0];
1940 link->codecs = &dlc[1];
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001941 link->platforms = &dlc[2];
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001942
1943 link->num_cpus = 1;
1944 link->num_codecs = 1;
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001945 link->num_platforms = 1;
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001946
Jaroslav Kysela8ce1cbd62020-01-22 20:07:52 +01001947 link->dobj.index = tplg->index;
1948 link->dobj.ops = tplg->ops;
1949 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1950
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001951 if (strlen(pcm->pcm_name)) {
1952 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1953 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001954 if (!link->name || !link->stream_name) {
1955 ret = -ENOMEM;
1956 goto err;
1957 }
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001958 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001959 link->id = le32_to_cpu(pcm->pcm_id);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001960
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001961 if (strlen(pcm->dai_name)) {
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001962 link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001963 if (!link->cpus->dai_name) {
1964 ret = -ENOMEM;
1965 goto err;
1966 }
1967 }
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001968
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001969 link->codecs->name = "snd-soc-dummy";
1970 link->codecs->dai_name = "snd-soc-dummy-dai";
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001971
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001972 link->platforms->name = "snd-soc-dummy";
1973
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001974 /* enable DPCM */
1975 link->dynamic = 1;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001976 link->dpcm_playback = le32_to_cpu(pcm->playback);
1977 link->dpcm_capture = le32_to_cpu(pcm->capture);
Mengdong Lin717a8e72016-11-03 01:03:34 +08001978 if (pcm->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001979 set_link_flags(link,
1980 le32_to_cpu(pcm->flag_mask),
1981 le32_to_cpu(pcm->flags));
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001982
Mengdong Linacfc7d42016-01-15 16:13:37 +08001983 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001984 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001985 if (ret < 0) {
1986 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
Dragos Tarcatu76d27032019-12-09 18:39:38 -06001987 goto err;
1988 }
1989
Mark Brown2acf6ce2019-12-10 13:27:14 +00001990 ret = snd_soc_add_pcm_runtime(tplg->comp->card, link);
Dragos Tarcatu76d27032019-12-09 18:39:38 -06001991 if (ret < 0) {
1992 dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n");
1993 goto err;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001994 }
1995
Mengdong Linacfc7d42016-01-15 16:13:37 +08001996 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1997
Mengdong Linacfc7d42016-01-15 16:13:37 +08001998 return 0;
Dragos Tarcatu76d27032019-12-09 18:39:38 -06001999err:
2000 kfree(link->name);
2001 kfree(link->stream_name);
2002 kfree(link->cpus->dai_name);
2003 kfree(link);
2004 return ret;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002005}
2006
2007/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08002008static int soc_tplg_pcm_create(struct soc_tplg *tplg,
2009 struct snd_soc_tplg_pcm *pcm)
2010{
Mengdong Linacfc7d42016-01-15 16:13:37 +08002011 int ret;
2012
2013 ret = soc_tplg_dai_create(tplg, pcm);
2014 if (ret < 0)
2015 return ret;
2016
Mengdong Linab4bc5e2016-11-03 01:04:42 +08002017 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08002018}
2019
Mengdong Lin55726dc2016-11-03 01:00:16 +08002020/* copy stream caps from the old version 4 of source */
2021static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
2022 struct snd_soc_tplg_stream_caps_v4 *src)
2023{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002024 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin55726dc2016-11-03 01:00:16 +08002025 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2026 dest->formats = src->formats;
2027 dest->rates = src->rates;
2028 dest->rate_min = src->rate_min;
2029 dest->rate_max = src->rate_max;
2030 dest->channels_min = src->channels_min;
2031 dest->channels_max = src->channels_max;
2032 dest->periods_min = src->periods_min;
2033 dest->periods_max = src->periods_max;
2034 dest->period_size_min = src->period_size_min;
2035 dest->period_size_max = src->period_size_max;
2036 dest->buffer_size_min = src->buffer_size_min;
2037 dest->buffer_size_max = src->buffer_size_max;
2038}
2039
2040/**
2041 * pcm_new_ver - Create the new version of PCM from the old version.
2042 * @tplg: topology context
2043 * @src: older version of pcm as a source
2044 * @pcm: latest version of pcm created from the source
2045 *
2046 * Support from vesion 4. User should free the returned pcm manually.
2047 */
2048static int pcm_new_ver(struct soc_tplg *tplg,
2049 struct snd_soc_tplg_pcm *src,
2050 struct snd_soc_tplg_pcm **pcm)
2051{
2052 struct snd_soc_tplg_pcm *dest;
2053 struct snd_soc_tplg_pcm_v4 *src_v4;
2054 int i;
2055
2056 *pcm = NULL;
2057
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002058 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002059 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
2060 return -EINVAL;
2061 }
2062
2063 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
2064 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
2065 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2066 if (!dest)
2067 return -ENOMEM;
2068
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002069 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin55726dc2016-11-03 01:00:16 +08002070 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2071 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2072 dest->pcm_id = src_v4->pcm_id;
2073 dest->dai_id = src_v4->dai_id;
2074 dest->playback = src_v4->playback;
2075 dest->capture = src_v4->capture;
2076 dest->compress = src_v4->compress;
2077 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002078 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin55726dc2016-11-03 01:00:16 +08002079 memcpy(&dest->stream[i], &src_v4->stream[i],
2080 sizeof(struct snd_soc_tplg_stream));
2081
2082 for (i = 0; i < 2; i++)
2083 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2084
2085 *pcm = dest;
2086 return 0;
2087}
2088
Mengdong Lin64527e82016-01-15 16:13:28 +08002089static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01002090 struct snd_soc_tplg_hdr *hdr)
2091{
Mengdong Lin55726dc2016-11-03 01:00:16 +08002092 struct snd_soc_tplg_pcm *pcm, *_pcm;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002093 int count;
2094 int size;
Vinod Koulfd340452016-12-08 23:01:27 +05302095 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08002096 bool abi_match;
Dragos Tarcatua3039ae2019-12-09 18:39:39 -06002097 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002098
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002099 count = le32_to_cpu(hdr->count);
2100
Mengdong Lin55726dc2016-11-03 01:00:16 +08002101 /* check the element size and count */
2102 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002103 size = le32_to_cpu(pcm->size);
2104 if (size > sizeof(struct snd_soc_tplg_pcm)
2105 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002106 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002107 size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002108 return -EINVAL;
2109 }
2110
Liam Girdwood8a978232015-05-29 19:06:14 +01002111 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002112 size, count,
2113 le32_to_cpu(hdr->payload_size),
2114 "PCM DAI")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002115 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2116 count);
2117 return -EINVAL;
2118 }
2119
Mengdong Lin64527e82016-01-15 16:13:28 +08002120 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002121 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002122 size = le32_to_cpu(pcm->size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002123
2124 /* check ABI version by size, create a new version of pcm
2125 * if abi not match.
2126 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002127 if (size == sizeof(*pcm)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002128 abi_match = true;
2129 _pcm = pcm;
2130 } else {
2131 abi_match = false;
Amadeusz Sławińskib3677fc32020-03-27 16:47:28 -04002132 ret = pcm_new_ver(tplg, pcm, &_pcm);
2133 if (ret < 0)
2134 return ret;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002135 }
2136
Mengdong Lin55726dc2016-11-03 01:00:16 +08002137 /* create the FE DAIs and DAI links */
Dragos Tarcatua3039ae2019-12-09 18:39:39 -06002138 ret = soc_tplg_pcm_create(tplg, _pcm);
2139 if (ret < 0) {
2140 if (!abi_match)
2141 kfree(_pcm);
2142 return ret;
2143 }
Mengdong Lin55726dc2016-11-03 01:00:16 +08002144
Mengdong Lin717a8e72016-11-03 01:03:34 +08002145 /* offset by version-specific struct size and
2146 * real priv data size
2147 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002148 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
Mengdong Lin717a8e72016-11-03 01:03:34 +08002149
Mengdong Lin55726dc2016-11-03 01:00:16 +08002150 if (!abi_match)
2151 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08002152 }
2153
Liam Girdwood8a978232015-05-29 19:06:14 +01002154 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002155
Liam Girdwood8a978232015-05-29 19:06:14 +01002156 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002157}
2158
Mengdong Lin593d9e52016-11-03 01:04:27 +08002159/**
2160 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002161 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002162 * @cfg: physical link configs.
2163 *
2164 * Topology context contains a list of supported HW formats (configs) and
2165 * a default format ID for the physical link. This function will use this
2166 * default ID to choose the HW format to set the link's DAI format for init.
2167 */
2168static void set_link_hw_format(struct snd_soc_dai_link *link,
2169 struct snd_soc_tplg_link_config *cfg)
2170{
2171 struct snd_soc_tplg_hw_config *hw_config;
2172 unsigned char bclk_master, fsync_master;
2173 unsigned char invert_bclk, invert_fsync;
2174 int i;
2175
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002176 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002177 hw_config = &cfg->hw_config[i];
2178 if (hw_config->id != cfg->default_hw_config_id)
2179 continue;
2180
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002181 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2182 SND_SOC_DAIFMT_FORMAT_MASK;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002183
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002184 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002185 switch (hw_config->clock_gated) {
2186 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002187 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002188 break;
2189
2190 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002191 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002192 break;
2193
2194 default:
2195 /* ignore the value */
2196 break;
2197 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002198
Mengdong Lin593d9e52016-11-03 01:04:27 +08002199 /* clock signal polarity */
2200 invert_bclk = hw_config->invert_bclk;
2201 invert_fsync = hw_config->invert_fsync;
2202 if (!invert_bclk && !invert_fsync)
2203 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2204 else if (!invert_bclk && invert_fsync)
2205 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2206 else if (invert_bclk && !invert_fsync)
2207 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2208 else
2209 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2210
2211 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002212 bclk_master = (hw_config->bclk_master ==
2213 SND_SOC_TPLG_BCLK_CM);
2214 fsync_master = (hw_config->fsync_master ==
2215 SND_SOC_TPLG_FSYNC_CM);
2216 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002217 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002218 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002219 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2220 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002221 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2222 else
2223 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2224 }
2225}
2226
2227/**
2228 * link_new_ver - Create a new physical link config from the old
2229 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002230 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002231 * @src: old version of phyical link config as a source
2232 * @link: latest version of physical link config created from the source
2233 *
2234 * Support from vesion 4. User need free the returned link config manually.
2235 */
2236static int link_new_ver(struct soc_tplg *tplg,
2237 struct snd_soc_tplg_link_config *src,
2238 struct snd_soc_tplg_link_config **link)
2239{
2240 struct snd_soc_tplg_link_config *dest;
2241 struct snd_soc_tplg_link_config_v4 *src_v4;
2242 int i;
2243
2244 *link = NULL;
2245
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002246 if (le32_to_cpu(src->size) !=
2247 sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002248 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2249 return -EINVAL;
2250 }
2251
2252 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2253
2254 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2255 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2256 if (!dest)
2257 return -ENOMEM;
2258
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002259 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002260 dest->id = src_v4->id;
2261 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002262 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002263 memcpy(&dest->stream[i], &src_v4->stream[i],
2264 sizeof(struct snd_soc_tplg_stream));
2265
2266 *link = dest;
2267 return 0;
2268}
2269
Kuninori Morimotod6f31e02019-12-10 09:34:14 +09002270/**
2271 * snd_soc_find_dai_link - Find a DAI link
2272 *
2273 * @card: soc card
2274 * @id: DAI link ID to match
2275 * @name: DAI link name to match, optional
2276 * @stream_name: DAI link stream name to match, optional
2277 *
2278 * This function will search all existing DAI links of the soc card to
2279 * find the link of the same ID. Since DAI links may not have their
2280 * unique ID, so name and stream name should also match if being
2281 * specified.
2282 *
2283 * Return: pointer of DAI link, or NULL if not found.
2284 */
2285static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2286 int id, const char *name,
2287 const char *stream_name)
2288{
2289 struct snd_soc_pcm_runtime *rtd;
2290 struct snd_soc_dai_link *link;
2291
2292 for_each_card_rtds(card, rtd) {
2293 link = rtd->dai_link;
2294
2295 if (link->id != id)
2296 continue;
2297
2298 if (name && (!link->name || strcmp(name, link->name)))
2299 continue;
2300
2301 if (stream_name && (!link->stream_name
2302 || strcmp(stream_name, link->stream_name)))
2303 continue;
2304
2305 return link;
2306 }
2307
2308 return NULL;
2309}
2310
Mengdong Lin593d9e52016-11-03 01:04:27 +08002311/* Find and configure an existing physical DAI link */
2312static int soc_tplg_link_config(struct soc_tplg *tplg,
2313 struct snd_soc_tplg_link_config *cfg)
2314{
2315 struct snd_soc_dai_link *link;
2316 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002317 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002318 int ret;
2319
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002320 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2321 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2322 return -EINVAL;
2323 else if (len)
2324 name = cfg->name;
2325 else
2326 name = NULL;
2327
2328 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2329 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2330 return -EINVAL;
2331 else if (len)
2332 stream_name = cfg->stream_name;
2333 else
2334 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002335
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002336 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
Mengdong Lin593d9e52016-11-03 01:04:27 +08002337 name, stream_name);
2338 if (!link) {
2339 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2340 name, cfg->id);
2341 return -EINVAL;
2342 }
2343
2344 /* hw format */
2345 if (cfg->num_hw_configs)
2346 set_link_hw_format(link, cfg);
2347
2348 /* flags */
2349 if (cfg->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002350 set_link_flags(link,
2351 le32_to_cpu(cfg->flag_mask),
2352 le32_to_cpu(cfg->flags));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002353
2354 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002355 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002356 if (ret < 0) {
2357 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2358 return ret;
2359 }
2360
Bard liaoadfebb52019-02-01 11:07:40 -06002361 /* for unloading it in snd_soc_tplg_component_remove */
2362 link->dobj.index = tplg->index;
2363 link->dobj.ops = tplg->ops;
2364 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2365 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2366
Mengdong Lin593d9e52016-11-03 01:04:27 +08002367 return 0;
2368}
2369
2370
2371/* Load physical link config elements from the topology context */
2372static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2373 struct snd_soc_tplg_hdr *hdr)
2374{
2375 struct snd_soc_tplg_link_config *link, *_link;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002376 int count;
2377 int size;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002378 int i, ret;
2379 bool abi_match;
2380
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002381 count = le32_to_cpu(hdr->count);
2382
Mengdong Lin593d9e52016-11-03 01:04:27 +08002383 /* check the element size and count */
2384 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002385 size = le32_to_cpu(link->size);
2386 if (size > sizeof(struct snd_soc_tplg_link_config)
2387 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002388 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002389 size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002390 return -EINVAL;
2391 }
2392
2393 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002394 size, count,
2395 le32_to_cpu(hdr->payload_size),
2396 "physical link config")) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002397 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2398 count);
2399 return -EINVAL;
2400 }
2401
2402 /* config physical DAI links */
2403 for (i = 0; i < count; i++) {
2404 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002405 size = le32_to_cpu(link->size);
2406 if (size == sizeof(*link)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002407 abi_match = true;
2408 _link = link;
2409 } else {
2410 abi_match = false;
2411 ret = link_new_ver(tplg, link, &_link);
2412 if (ret < 0)
2413 return ret;
2414 }
2415
2416 ret = soc_tplg_link_config(tplg, _link);
Dragos Tarcatu2b2d5c42020-02-07 20:53:24 +02002417 if (ret < 0) {
2418 if (!abi_match)
2419 kfree(_link);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002420 return ret;
Dragos Tarcatu2b2d5c42020-02-07 20:53:24 +02002421 }
Mengdong Lin593d9e52016-11-03 01:04:27 +08002422
2423 /* offset by version-specific struct size and
2424 * real priv data size
2425 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002426 tplg->pos += size + le32_to_cpu(_link->priv.size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002427
2428 if (!abi_match)
2429 kfree(_link); /* free the duplicated one */
2430 }
2431
2432 return 0;
2433}
2434
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002435/**
2436 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002437 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002438 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002439 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002440 * The physical dai should already be registered by the platform driver.
2441 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002442 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002443static int soc_tplg_dai_config(struct soc_tplg *tplg,
2444 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002445{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002446 struct snd_soc_dai_link_component dai_component;
Mengdong Lin0038be92016-07-26 14:32:37 +08002447 struct snd_soc_dai *dai;
2448 struct snd_soc_dai_driver *dai_drv;
2449 struct snd_soc_pcm_stream *stream;
2450 struct snd_soc_tplg_stream_caps *caps;
2451 int ret;
2452
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002453 memset(&dai_component, 0, sizeof(dai_component));
2454
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002455 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002456 dai = snd_soc_find_dai(&dai_component);
2457 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002458 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2459 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002460 return -EINVAL;
2461 }
2462
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002463 if (le32_to_cpu(d->dai_id) != dai->id) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002464 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2465 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002466 return -EINVAL;
2467 }
2468
2469 dai_drv = dai->driver;
2470 if (!dai_drv)
2471 return -EINVAL;
2472
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002473 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002474 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002475 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04002476 ret = set_stream_info(stream, caps);
2477 if (ret < 0)
2478 goto err;
Mengdong Lin0038be92016-07-26 14:32:37 +08002479 }
2480
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002481 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002482 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002483 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04002484 ret = set_stream_info(stream, caps);
2485 if (ret < 0)
2486 goto err;
Mengdong Lin0038be92016-07-26 14:32:37 +08002487 }
2488
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002489 if (d->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002490 set_dai_flags(dai_drv,
2491 le32_to_cpu(d->flag_mask),
2492 le32_to_cpu(d->flags));
Mengdong Lin0038be92016-07-26 14:32:37 +08002493
2494 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002495 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002496 if (ret < 0) {
2497 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04002498 goto err;
Mengdong Lin0038be92016-07-26 14:32:37 +08002499 }
2500
2501 return 0;
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04002502
2503err:
2504 kfree(dai_drv->playback.stream_name);
2505 kfree(dai_drv->capture.stream_name);
2506 return ret;
Mengdong Lin0038be92016-07-26 14:32:37 +08002507}
2508
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002509/* load physical DAI elements */
2510static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2511 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002512{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002513 struct snd_soc_tplg_dai *dai;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002514 int count;
Amadeusz Sławińskidd8e8712020-03-27 16:47:29 -04002515 int i, ret;
Mengdong Lin0038be92016-07-26 14:32:37 +08002516
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002517 count = le32_to_cpu(hdr->count);
2518
Mengdong Lin0038be92016-07-26 14:32:37 +08002519 /* config the existing BE DAIs */
2520 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002521 dai = (struct snd_soc_tplg_dai *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002522 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002523 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002524 return -EINVAL;
2525 }
2526
Amadeusz Sławińskidd8e8712020-03-27 16:47:29 -04002527 ret = soc_tplg_dai_config(tplg, dai);
2528 if (ret < 0) {
2529 dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2530 return ret;
2531 }
2532
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002533 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
Mengdong Lin0038be92016-07-26 14:32:37 +08002534 }
2535
2536 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2537 return 0;
2538}
2539
Mengdong Lin583958f2016-10-11 14:36:42 +08002540/**
2541 * manifest_new_ver - Create a new version of manifest from the old version
2542 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002543 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002544 * @src: old version of manifest as a source
2545 * @manifest: latest version of manifest created from the source
2546 *
2547 * Support from vesion 4. Users need free the returned manifest manually.
2548 */
2549static int manifest_new_ver(struct soc_tplg *tplg,
2550 struct snd_soc_tplg_manifest *src,
2551 struct snd_soc_tplg_manifest **manifest)
2552{
2553 struct snd_soc_tplg_manifest *dest;
2554 struct snd_soc_tplg_manifest_v4 *src_v4;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002555 int size;
Mengdong Lin583958f2016-10-11 14:36:42 +08002556
2557 *manifest = NULL;
2558
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002559 size = le32_to_cpu(src->size);
2560 if (size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002561 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002562 size);
2563 if (size)
Guenter Roeckac9391d2018-05-24 12:49:21 -07002564 return -EINVAL;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002565 src->size = cpu_to_le32(sizeof(*src_v4));
Mengdong Lin583958f2016-10-11 14:36:42 +08002566 }
2567
2568 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2569
2570 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002571 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2572 GFP_KERNEL);
Mengdong Lin583958f2016-10-11 14:36:42 +08002573 if (!dest)
2574 return -ENOMEM;
2575
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002576 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin583958f2016-10-11 14:36:42 +08002577 dest->control_elems = src_v4->control_elems;
2578 dest->widget_elems = src_v4->widget_elems;
2579 dest->graph_elems = src_v4->graph_elems;
2580 dest->pcm_elems = src_v4->pcm_elems;
2581 dest->dai_link_elems = src_v4->dai_link_elems;
2582 dest->priv.size = src_v4->priv.size;
2583 if (dest->priv.size)
2584 memcpy(dest->priv.data, src_v4->priv.data,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002585 le32_to_cpu(src_v4->priv.size));
Mengdong Lin583958f2016-10-11 14:36:42 +08002586
2587 *manifest = dest;
2588 return 0;
2589}
Mengdong Lin0038be92016-07-26 14:32:37 +08002590
Liam Girdwood8a978232015-05-29 19:06:14 +01002591static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002592 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002593{
Mengdong Lin583958f2016-10-11 14:36:42 +08002594 struct snd_soc_tplg_manifest *manifest, *_manifest;
2595 bool abi_match;
Dragos Tarcatu242c46c2020-02-07 20:53:25 +02002596 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002597
Liam Girdwood8a978232015-05-29 19:06:14 +01002598 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002599
2600 /* check ABI version by size, create a new manifest if abi not match */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002601 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
Mengdong Lin583958f2016-10-11 14:36:42 +08002602 abi_match = true;
2603 _manifest = manifest;
2604 } else {
2605 abi_match = false;
Dragos Tarcatu242c46c2020-02-07 20:53:25 +02002606 ret = manifest_new_ver(tplg, manifest, &_manifest);
2607 if (ret < 0)
2608 return ret;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002609 }
2610
Mengdong Lin583958f2016-10-11 14:36:42 +08002611 /* pass control to component driver for optional further init */
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -04002612 if (tplg->ops && tplg->ops->manifest)
Dragos Tarcatu242c46c2020-02-07 20:53:25 +02002613 ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002614
Mengdong Lin583958f2016-10-11 14:36:42 +08002615 if (!abi_match) /* free the duplicated one */
2616 kfree(_manifest);
2617
Dragos Tarcatu242c46c2020-02-07 20:53:25 +02002618 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002619}
2620
2621/* validate header magic, size and type */
2622static int soc_valid_header(struct soc_tplg *tplg,
2623 struct snd_soc_tplg_hdr *hdr)
2624{
2625 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2626 return 0;
2627
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002628 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002629 dev_err(tplg->dev,
2630 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002631 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002632 tplg->fw->size);
2633 return -EINVAL;
2634 }
2635
Liam Girdwood8a978232015-05-29 19:06:14 +01002636 /* big endian firmware objects not supported atm */
Amadeusz Sławiński26d87882020-04-15 12:24:35 -04002637 if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002638 dev_err(tplg->dev,
2639 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2640 tplg->pass, hdr->magic,
2641 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2642 return -EINVAL;
2643 }
2644
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002645 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002646 dev_err(tplg->dev,
2647 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2648 tplg->pass, hdr->magic,
2649 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2650 return -EINVAL;
2651 }
2652
Mengdong Lin288b8da2016-11-03 01:03:17 +08002653 /* Support ABI from version 4 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002654 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2655 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002656 dev_err(tplg->dev,
2657 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2658 tplg->pass, hdr->abi,
2659 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2660 tplg->fw->size);
2661 return -EINVAL;
2662 }
2663
2664 if (hdr->payload_size == 0) {
2665 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2666 soc_tplg_get_hdr_offset(tplg));
2667 return -EINVAL;
2668 }
2669
Liam Girdwood8a978232015-05-29 19:06:14 +01002670 return 1;
2671}
2672
2673/* check header type and call appropriate handler */
2674static int soc_tplg_load_header(struct soc_tplg *tplg,
2675 struct snd_soc_tplg_hdr *hdr)
2676{
Keyon Jie82ed7412020-05-27 10:28:00 +08002677 int (*elem_load)(struct soc_tplg *tplg,
2678 struct snd_soc_tplg_hdr *hdr);
2679 unsigned int hdr_pass;
2680
Liam Girdwood8a978232015-05-29 19:06:14 +01002681 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2682
2683 /* check for matching ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002684 if (le32_to_cpu(hdr->index) != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002685 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002686 return 0;
2687
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002688 tplg->index = le32_to_cpu(hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01002689
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002690 switch (le32_to_cpu(hdr->type)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002691 case SND_SOC_TPLG_TYPE_MIXER:
2692 case SND_SOC_TPLG_TYPE_ENUM:
2693 case SND_SOC_TPLG_TYPE_BYTES:
Keyon Jie82ed7412020-05-27 10:28:00 +08002694 hdr_pass = SOC_TPLG_PASS_MIXER;
2695 elem_load = soc_tplg_kcontrol_elems_load;
2696 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002697 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
Keyon Jie82ed7412020-05-27 10:28:00 +08002698 hdr_pass = SOC_TPLG_PASS_GRAPH;
2699 elem_load = soc_tplg_dapm_graph_elems_load;
2700 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002701 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
Keyon Jie82ed7412020-05-27 10:28:00 +08002702 hdr_pass = SOC_TPLG_PASS_WIDGET;
2703 elem_load = soc_tplg_dapm_widget_elems_load;
2704 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002705 case SND_SOC_TPLG_TYPE_PCM:
Keyon Jie82ed7412020-05-27 10:28:00 +08002706 hdr_pass = SOC_TPLG_PASS_PCM_DAI;
2707 elem_load = soc_tplg_pcm_elems_load;
2708 break;
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002709 case SND_SOC_TPLG_TYPE_DAI:
Keyon Jie82ed7412020-05-27 10:28:00 +08002710 hdr_pass = SOC_TPLG_PASS_BE_DAI;
2711 elem_load = soc_tplg_dai_elems_load;
2712 break;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002713 case SND_SOC_TPLG_TYPE_DAI_LINK:
2714 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2715 /* physical link configurations */
Keyon Jie82ed7412020-05-27 10:28:00 +08002716 hdr_pass = SOC_TPLG_PASS_LINK;
2717 elem_load = soc_tplg_link_elems_load;
2718 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002719 case SND_SOC_TPLG_TYPE_MANIFEST:
Keyon Jie82ed7412020-05-27 10:28:00 +08002720 hdr_pass = SOC_TPLG_PASS_MANIFEST;
2721 elem_load = soc_tplg_manifest_load;
2722 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002723 default:
2724 /* bespoke vendor data object */
Keyon Jie82ed7412020-05-27 10:28:00 +08002725 hdr_pass = SOC_TPLG_PASS_VENDOR;
2726 elem_load = soc_tplg_vendor_load;
2727 break;
2728 }
2729
2730 if (tplg->pass == hdr_pass) {
2731 dev_dbg(tplg->dev,
2732 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2733 hdr->payload_size, hdr->type, hdr->version,
2734 hdr->vendor_type, tplg->pass);
2735 return elem_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002736 }
2737
2738 return 0;
2739}
2740
2741/* process the topology file headers */
2742static int soc_tplg_process_headers(struct soc_tplg *tplg)
2743{
2744 struct snd_soc_tplg_hdr *hdr;
2745 int ret;
2746
2747 tplg->pass = SOC_TPLG_PASS_START;
2748
2749 /* process the header types from start to end */
2750 while (tplg->pass <= SOC_TPLG_PASS_END) {
2751
2752 tplg->hdr_pos = tplg->fw->data;
2753 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2754
2755 while (!soc_tplg_is_eof(tplg)) {
2756
2757 /* make sure header is valid before loading */
2758 ret = soc_valid_header(tplg, hdr);
Pierre-Louis Bossart8bf94752020-07-07 15:37:49 -05002759 if (ret < 0) {
2760 dev_err(tplg->dev,
2761 "ASoC: topology: invalid header: %d\n", ret);
Liam Girdwood8a978232015-05-29 19:06:14 +01002762 return ret;
Pierre-Louis Bossart8bf94752020-07-07 15:37:49 -05002763 } else if (ret == 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002764 break;
Pierre-Louis Bossart8bf94752020-07-07 15:37:49 -05002765 }
Liam Girdwood8a978232015-05-29 19:06:14 +01002766
2767 /* load the header object */
2768 ret = soc_tplg_load_header(tplg, hdr);
Pierre-Louis Bossart8bf94752020-07-07 15:37:49 -05002769 if (ret < 0) {
2770 dev_err(tplg->dev,
2771 "ASoC: topology: could not load header: %d\n", ret);
Liam Girdwood8a978232015-05-29 19:06:14 +01002772 return ret;
Pierre-Louis Bossart8bf94752020-07-07 15:37:49 -05002773 }
Liam Girdwood8a978232015-05-29 19:06:14 +01002774
2775 /* goto next header */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002776 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
Liam Girdwood8a978232015-05-29 19:06:14 +01002777 sizeof(struct snd_soc_tplg_hdr);
2778 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2779 }
2780
2781 /* next data type pass */
2782 tplg->pass++;
2783 }
2784
2785 /* signal DAPM we are complete */
2786 ret = soc_tplg_dapm_complete(tplg);
2787 if (ret < 0)
2788 dev_err(tplg->dev,
2789 "ASoC: failed to initialise DAPM from Firmware\n");
2790
2791 return ret;
2792}
2793
2794static int soc_tplg_load(struct soc_tplg *tplg)
2795{
2796 int ret;
2797
2798 ret = soc_tplg_process_headers(tplg);
2799 if (ret == 0)
2800 soc_tplg_complete(tplg);
2801
2802 return ret;
2803}
2804
2805/* load audio component topology from "firmware" file */
2806int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2807 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2808{
2809 struct soc_tplg tplg;
Bard liao304017d2019-02-17 21:23:47 +08002810 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002811
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -04002812 /* component needs to exist to keep and reference data while parsing */
2813 if (!comp)
2814 return -EINVAL;
2815
Liam Girdwood8a978232015-05-29 19:06:14 +01002816 /* setup parsing context */
2817 memset(&tplg, 0, sizeof(tplg));
2818 tplg.fw = fw;
2819 tplg.dev = comp->dev;
2820 tplg.comp = comp;
2821 tplg.ops = ops;
2822 tplg.req_index = id;
2823 tplg.io_ops = ops->io_ops;
2824 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002825 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2826 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002827
Bard liao304017d2019-02-17 21:23:47 +08002828 ret = soc_tplg_load(&tplg);
2829 /* free the created components if fail to load topology */
2830 if (ret)
2831 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2832
2833 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002834}
2835EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2836
2837/* remove this dynamic widget */
2838void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2839{
2840 /* make sure we are a widget */
2841 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2842 return;
2843
2844 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2845}
2846EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2847
2848/* remove all dynamic widgets from this DAPM context */
2849void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2850 u32 index)
2851{
2852 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002853
Kuninori Morimoto14596692020-03-09 13:08:21 +09002854 for_each_card_widgets_safe(dapm->card, w, next_w) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002855
2856 /* make sure we are a widget with correct context */
2857 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2858 continue;
2859
2860 /* match ID */
2861 if (w->dobj.index != index &&
2862 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2863 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002864 /* check and free and dynamic widget kcontrols */
2865 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002866 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002867 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002868 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002869}
2870EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2871
2872/* remove dynamic controls from the component driver */
2873int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2874{
2875 struct snd_soc_dobj *dobj, *next_dobj;
2876 int pass = SOC_TPLG_PASS_END;
2877
2878 /* process the header types from end to start */
2879 while (pass >= SOC_TPLG_PASS_START) {
2880
2881 /* remove mixer controls */
2882 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2883 list) {
2884
2885 /* match index */
2886 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002887 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002888 continue;
2889
2890 switch (dobj->type) {
2891 case SND_SOC_DOBJ_MIXER:
2892 remove_mixer(comp, dobj, pass);
2893 break;
2894 case SND_SOC_DOBJ_ENUM:
2895 remove_enum(comp, dobj, pass);
2896 break;
2897 case SND_SOC_DOBJ_BYTES:
2898 remove_bytes(comp, dobj, pass);
2899 break;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06002900 case SND_SOC_DOBJ_GRAPH:
2901 remove_route(comp, dobj, pass);
2902 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002903 case SND_SOC_DOBJ_WIDGET:
2904 remove_widget(comp, dobj, pass);
2905 break;
2906 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002907 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002908 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002909 case SND_SOC_DOBJ_DAI_LINK:
2910 remove_link(comp, dobj, pass);
2911 break;
Bard liaoadfebb52019-02-01 11:07:40 -06002912 case SND_SOC_DOBJ_BACKEND_LINK:
2913 /*
2914 * call link_unload ops if extra
2915 * deinitialization is needed.
2916 */
2917 remove_backend_link(comp, dobj, pass);
2918 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002919 default:
2920 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2921 dobj->type);
2922 break;
2923 }
2924 }
2925 pass--;
2926 }
2927
2928 /* let caller know if FW can be freed when no objects are left */
2929 return !list_empty(&comp->dobj_list);
2930}
2931EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);