blob: 6eaa00c210117265e778fa6ccb528d530e7f556f [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
595 ext_ops = tplg->bytes_ext_ops;
596 num_ops = tplg->bytes_ext_ops_count;
597 for (i = 0; i < num_ops; i++) {
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600598 if (!sbe->put &&
599 ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800600 sbe->put = ext_ops[i].put;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600601 if (!sbe->get &&
602 ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800603 sbe->get = ext_ops[i].get;
604 }
605
606 if (sbe->put && sbe->get)
607 return 0;
608 else
609 return -EINVAL;
610 }
611
Mengdong Lin88a17d82015-08-18 18:11:51 +0800612 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800613 ops = tplg->io_ops;
614 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100615 for (i = 0; i < num_ops; i++) {
616
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600617 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
Liam Girdwood8a978232015-05-29 19:06:14 +0100618 k->put = ops[i].put;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600619 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
Liam Girdwood8a978232015-05-29 19:06:14 +0100620 k->get = ops[i].get;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600621 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800622 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100623 }
624
Mengdong Lin88a17d82015-08-18 18:11:51 +0800625 /* vendor specific handlers found ? */
626 if (k->put && k->get && k->info)
627 return 0;
628
629 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800630 ops = io_ops;
631 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800632 for (i = 0; i < num_ops; i++) {
633
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600634 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
Mengdong Lin88a17d82015-08-18 18:11:51 +0800635 k->put = ops[i].put;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600636 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
Mengdong Lin88a17d82015-08-18 18:11:51 +0800637 k->get = ops[i].get;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600638 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
Liam Girdwood8a978232015-05-29 19:06:14 +0100639 k->info = ops[i].info;
640 }
641
642 /* standard handlers found ? */
643 if (k->put && k->get && k->info)
644 return 0;
645
Liam Girdwood8a978232015-05-29 19:06:14 +0100646 /* nothing to bind */
647 return -EINVAL;
648}
649
650/* bind a widgets to it's evnt handlers */
651int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
652 const struct snd_soc_tplg_widget_events *events,
653 int num_events, u16 event_type)
654{
655 int i;
656
657 w->event = NULL;
658
659 for (i = 0; i < num_events; i++) {
660 if (event_type == events[i].type) {
661
662 /* found - so assign event */
663 w->event = events[i].event_handler;
664 return 0;
665 }
666 }
667
668 /* not found */
669 return -EINVAL;
670}
671EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
672
673/* optionally pass new dynamic kcontrol to component driver. */
674static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
675 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
676{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -0400677 if (tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100678 return tplg->ops->control_load(tplg->comp, tplg->index, k,
679 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100680
681 return 0;
682}
683
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100684
685static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
686 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100687{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100688 unsigned int item_len = 2 * sizeof(unsigned int);
689 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100690
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100691 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
692 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100693 return -ENOMEM;
694
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100695 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
696 p[1] = item_len;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500697 p[2] = le32_to_cpu(scale->min);
698 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
699 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100700
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100701 kc->tlv.p = (void *)p;
702 return 0;
703}
704
705static int soc_tplg_create_tlv(struct soc_tplg *tplg,
706 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
707{
708 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500709 u32 access = le32_to_cpu(tc->access);
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100710
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500711 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100712 return 0;
713
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500714 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100715 tplg_tlv = &tc->tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500716 switch (le32_to_cpu(tplg_tlv->type)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100717 case SNDRV_CTL_TLVT_DB_SCALE:
718 return soc_tplg_create_tlv_db_scale(tplg, kc,
719 &tplg_tlv->scale);
720
721 /* TODO: add support for other TLV types */
722 default:
723 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
724 tplg_tlv->type);
725 return -EINVAL;
726 }
727 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100728
729 return 0;
730}
731
732static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
733 struct snd_kcontrol_new *kc)
734{
735 kfree(kc->tlv.p);
736}
737
738static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
739 size_t size)
740{
741 struct snd_soc_tplg_bytes_control *be;
742 struct soc_bytes_ext *sbe;
743 struct snd_kcontrol_new kc;
744 int i, err;
745
746 if (soc_tplg_check_elem_count(tplg,
747 sizeof(struct snd_soc_tplg_bytes_control), count,
748 size, "mixer bytes")) {
749 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
750 count);
751 return -EINVAL;
752 }
753
754 for (i = 0; i < count; i++) {
755 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
756
757 /* validate kcontrol */
758 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
759 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
760 return -EINVAL;
761
762 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
763 if (sbe == NULL)
764 return -ENOMEM;
765
766 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500767 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100768
769 dev_dbg(tplg->dev,
770 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
771 be->hdr.name, be->hdr.access);
772
773 memset(&kc, 0, sizeof(kc));
774 kc.name = be->hdr.name;
775 kc.private_value = (long)sbe;
776 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500777 kc.access = le32_to_cpu(be->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100778
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500779 sbe->max = le32_to_cpu(be->max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100780 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
781 sbe->dobj.ops = tplg->ops;
782 INIT_LIST_HEAD(&sbe->dobj.list);
783
784 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800785 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100786 if (err) {
787 soc_control_err(tplg, &be->hdr, be->hdr.name);
788 kfree(sbe);
789 continue;
790 }
791
792 /* pass control to driver for optional further init */
793 err = soc_tplg_init_kcontrol(tplg, &kc,
794 (struct snd_soc_tplg_ctl_hdr *)be);
795 if (err < 0) {
796 dev_err(tplg->dev, "ASoC: failed to init %s\n",
797 be->hdr.name);
798 kfree(sbe);
799 continue;
800 }
801
802 /* register control here */
803 err = soc_tplg_add_kcontrol(tplg, &kc,
804 &sbe->dobj.control.kcontrol);
805 if (err < 0) {
806 dev_err(tplg->dev, "ASoC: failed to add %s\n",
807 be->hdr.name);
808 kfree(sbe);
809 continue;
810 }
811
812 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
813 }
814 return 0;
815
816}
817
818static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
819 size_t size)
820{
821 struct snd_soc_tplg_mixer_control *mc;
822 struct soc_mixer_control *sm;
823 struct snd_kcontrol_new kc;
824 int i, err;
825
826 if (soc_tplg_check_elem_count(tplg,
827 sizeof(struct snd_soc_tplg_mixer_control),
828 count, size, "mixers")) {
829
830 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
831 count);
832 return -EINVAL;
833 }
834
835 for (i = 0; i < count; i++) {
836 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
837
838 /* validate kcontrol */
839 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
840 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
841 return -EINVAL;
842
843 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
844 if (sm == NULL)
845 return -ENOMEM;
846 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500847 le32_to_cpu(mc->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100848
849 dev_dbg(tplg->dev,
850 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
851 mc->hdr.name, mc->hdr.access);
852
853 memset(&kc, 0, sizeof(kc));
854 kc.name = mc->hdr.name;
855 kc.private_value = (long)sm;
856 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500857 kc.access = le32_to_cpu(mc->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100858
859 /* we only support FL/FR channel mapping atm */
860 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
861 SNDRV_CHMAP_FL);
862 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
863 SNDRV_CHMAP_FR);
864 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
865 SNDRV_CHMAP_FL);
866 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
867 SNDRV_CHMAP_FR);
868
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500869 sm->max = le32_to_cpu(mc->max);
870 sm->min = le32_to_cpu(mc->min);
871 sm->invert = le32_to_cpu(mc->invert);
872 sm->platform_max = le32_to_cpu(mc->platform_max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100873 sm->dobj.index = tplg->index;
874 sm->dobj.ops = tplg->ops;
875 sm->dobj.type = SND_SOC_DOBJ_MIXER;
876 INIT_LIST_HEAD(&sm->dobj.list);
877
878 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800879 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100880 if (err) {
881 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
882 kfree(sm);
883 continue;
884 }
885
Bard liao3789deb2019-03-13 21:49:43 +0800886 /* create any TLV data */
Amadeusz Sławiński482db552020-03-27 16:47:25 -0400887 err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
888 if (err < 0) {
889 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
890 mc->hdr.name);
891 kfree(sm);
892 continue;
893 }
Bard liao3789deb2019-03-13 21:49:43 +0800894
Liam Girdwood8a978232015-05-29 19:06:14 +0100895 /* pass control to driver for optional further init */
896 err = soc_tplg_init_kcontrol(tplg, &kc,
897 (struct snd_soc_tplg_ctl_hdr *) mc);
898 if (err < 0) {
899 dev_err(tplg->dev, "ASoC: failed to init %s\n",
900 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +0800901 soc_tplg_free_tlv(tplg, &kc);
Liam Girdwood8a978232015-05-29 19:06:14 +0100902 kfree(sm);
903 continue;
904 }
905
Liam Girdwood8a978232015-05-29 19:06:14 +0100906 /* register control here */
907 err = soc_tplg_add_kcontrol(tplg, &kc,
908 &sm->dobj.control.kcontrol);
909 if (err < 0) {
910 dev_err(tplg->dev, "ASoC: failed to add %s\n",
911 mc->hdr.name);
912 soc_tplg_free_tlv(tplg, &kc);
913 kfree(sm);
914 continue;
915 }
916
917 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
918 }
919
920 return 0;
921}
922
923static int soc_tplg_denum_create_texts(struct soc_enum *se,
924 struct snd_soc_tplg_enum_control *ec)
925{
926 int i, ret;
927
928 se->dobj.control.dtexts =
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500929 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100930 if (se->dobj.control.dtexts == NULL)
931 return -ENOMEM;
932
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -0600933 for (i = 0; i < le32_to_cpu(ec->items); i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100934
935 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
936 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
937 ret = -EINVAL;
938 goto err;
939 }
940
941 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
942 if (!se->dobj.control.dtexts[i]) {
943 ret = -ENOMEM;
944 goto err;
945 }
946 }
947
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200948 se->items = le32_to_cpu(ec->items);
Mousumi Janab6e38b22017-04-11 13:06:22 +0530949 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100950 return 0;
951
952err:
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200953 se->items = i;
954 soc_tplg_denum_remove_texts(se);
955 return ret;
956}
957
958static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
959{
960 int i = se->items;
961
Liam Girdwood8a978232015-05-29 19:06:14 +0100962 for (--i; i >= 0; i--)
963 kfree(se->dobj.control.dtexts[i]);
964 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +0100965}
966
967static int soc_tplg_denum_create_values(struct soc_enum *se,
968 struct snd_soc_tplg_enum_control *ec)
969{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500970 int i;
971
972 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
Liam Girdwood8a978232015-05-29 19:06:14 +0100973 return -EINVAL;
974
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500975 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
976 sizeof(u32),
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200977 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100978 if (!se->dobj.control.dvalues)
979 return -ENOMEM;
980
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500981 /* convert from little-endian */
982 for (i = 0; i < le32_to_cpu(ec->items); i++) {
983 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
984 }
985
Liam Girdwood8a978232015-05-29 19:06:14 +0100986 return 0;
987}
988
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +0200989static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
990{
991 kfree(se->dobj.control.dvalues);
992}
993
Liam Girdwood8a978232015-05-29 19:06:14 +0100994static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
995 size_t size)
996{
997 struct snd_soc_tplg_enum_control *ec;
998 struct soc_enum *se;
999 struct snd_kcontrol_new kc;
1000 int i, ret, err;
1001
1002 if (soc_tplg_check_elem_count(tplg,
1003 sizeof(struct snd_soc_tplg_enum_control),
1004 count, size, "enums")) {
1005
1006 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1007 count);
1008 return -EINVAL;
1009 }
1010
1011 for (i = 0; i < count; i++) {
1012 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001013
1014 /* validate kcontrol */
1015 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1016 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1017 return -EINVAL;
1018
1019 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1020 if (se == NULL)
1021 return -ENOMEM;
1022
Liam Girdwood02b64242019-03-01 19:08:52 -06001023 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001024 le32_to_cpu(ec->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001025
Liam Girdwood8a978232015-05-29 19:06:14 +01001026 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1027 ec->hdr.name, ec->items);
1028
1029 memset(&kc, 0, sizeof(kc));
1030 kc.name = ec->hdr.name;
1031 kc.private_value = (long)se;
1032 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001033 kc.access = le32_to_cpu(ec->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001034
1035 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1036 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1037 SNDRV_CHMAP_FL);
1038 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1039 SNDRV_CHMAP_FL);
1040
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001041 se->mask = le32_to_cpu(ec->mask);
Liam Girdwood8a978232015-05-29 19:06:14 +01001042 se->dobj.index = tplg->index;
1043 se->dobj.type = SND_SOC_DOBJ_ENUM;
1044 se->dobj.ops = tplg->ops;
1045 INIT_LIST_HEAD(&se->dobj.list);
1046
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001047 switch (le32_to_cpu(ec->hdr.ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001048 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1049 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1050 err = soc_tplg_denum_create_values(se, ec);
1051 if (err < 0) {
1052 dev_err(tplg->dev,
1053 "ASoC: could not create values for %s\n",
1054 ec->hdr.name);
1055 kfree(se);
1056 continue;
1057 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001058 /* fall through */
Liam Girdwood8a978232015-05-29 19:06:14 +01001059 case SND_SOC_TPLG_CTL_ENUM:
1060 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1061 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1062 err = soc_tplg_denum_create_texts(se, ec);
1063 if (err < 0) {
1064 dev_err(tplg->dev,
1065 "ASoC: could not create texts for %s\n",
1066 ec->hdr.name);
1067 kfree(se);
1068 continue;
1069 }
1070 break;
1071 default:
1072 dev_err(tplg->dev,
1073 "ASoC: invalid enum control type %d for %s\n",
1074 ec->hdr.ops.info, ec->hdr.name);
1075 kfree(se);
1076 continue;
1077 }
1078
1079 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001080 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001081 if (err) {
1082 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1083 kfree(se);
1084 continue;
1085 }
1086
1087 /* pass control to driver for optional further init */
1088 err = soc_tplg_init_kcontrol(tplg, &kc,
1089 (struct snd_soc_tplg_ctl_hdr *) ec);
1090 if (err < 0) {
1091 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1092 ec->hdr.name);
1093 kfree(se);
1094 continue;
1095 }
1096
1097 /* register control here */
1098 ret = soc_tplg_add_kcontrol(tplg,
1099 &kc, &se->dobj.control.kcontrol);
1100 if (ret < 0) {
1101 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1102 ec->hdr.name);
1103 kfree(se);
1104 continue;
1105 }
1106
1107 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1108 }
1109
1110 return 0;
1111}
1112
1113static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1114 struct snd_soc_tplg_hdr *hdr)
1115{
1116 struct snd_soc_tplg_ctl_hdr *control_hdr;
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001117 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001118 int i;
1119
Liam Girdwood8a978232015-05-29 19:06:14 +01001120 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1121 soc_tplg_get_offset(tplg));
1122
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001123 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001124
1125 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1126
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001127 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001128 dev_err(tplg->dev, "ASoC: invalid control size\n");
1129 return -EINVAL;
1130 }
1131
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001132 switch (le32_to_cpu(control_hdr->ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001133 case SND_SOC_TPLG_CTL_VOLSW:
1134 case SND_SOC_TPLG_CTL_STROBE:
1135 case SND_SOC_TPLG_CTL_VOLSW_SX:
1136 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1137 case SND_SOC_TPLG_CTL_RANGE:
1138 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1139 case SND_SOC_TPLG_DAPM_CTL_PIN:
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001140 ret = soc_tplg_dmixer_create(tplg, 1,
1141 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001142 break;
1143 case SND_SOC_TPLG_CTL_ENUM:
1144 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1145 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1146 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1147 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001148 ret = soc_tplg_denum_create(tplg, 1,
1149 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001150 break;
1151 case SND_SOC_TPLG_CTL_BYTES:
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001152 ret = soc_tplg_dbytes_create(tplg, 1,
1153 le32_to_cpu(hdr->payload_size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001154 break;
1155 default:
1156 soc_bind_err(tplg, control_hdr, i);
1157 return -EINVAL;
1158 }
Amadeusz Sławiński2ae548f2020-03-27 16:47:26 -04001159 if (ret < 0) {
1160 dev_err(tplg->dev, "ASoC: invalid control\n");
1161 return ret;
1162 }
1163
Liam Girdwood8a978232015-05-29 19:06:14 +01001164 }
1165
1166 return 0;
1167}
1168
Liam Girdwood503e79b2018-06-14 20:53:59 +01001169/* optionally pass new dynamic kcontrol to component driver. */
1170static int soc_tplg_add_route(struct soc_tplg *tplg,
1171 struct snd_soc_dapm_route *route)
1172{
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -04001173 if (tplg->ops && tplg->ops->dapm_route_load)
Liam Girdwood503e79b2018-06-14 20:53:59 +01001174 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1175 route);
1176
1177 return 0;
1178}
1179
Liam Girdwood8a978232015-05-29 19:06:14 +01001180static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1181 struct snd_soc_tplg_hdr *hdr)
1182{
1183 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001184 struct snd_soc_tplg_dapm_graph_elem *elem;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001185 struct snd_soc_dapm_route **routes;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001186 int count, i, j;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001187 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001188
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001189 count = le32_to_cpu(hdr->count);
1190
Liam Girdwood8a978232015-05-29 19:06:14 +01001191 if (soc_tplg_check_elem_count(tplg,
1192 sizeof(struct snd_soc_tplg_dapm_graph_elem),
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001193 count, le32_to_cpu(hdr->payload_size), "graph")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001194
1195 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1196 count);
1197 return -EINVAL;
1198 }
1199
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001200 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1201 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001202
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001203 /* allocate memory for pointer to array of dapm routes */
1204 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1205 GFP_KERNEL);
1206 if (!routes)
1207 return -ENOMEM;
1208
1209 /*
1210 * allocate memory for each dapm route in the array.
1211 * This needs to be done individually so that
1212 * each route can be freed when it is removed in remove_route().
1213 */
1214 for (i = 0; i < count; i++) {
1215 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1216 if (!routes[i]) {
1217 /* free previously allocated memory */
1218 for (j = 0; j < i; j++)
1219 kfree(routes[j]);
1220
1221 kfree(routes);
1222 return -ENOMEM;
1223 }
1224 }
1225
Liam Girdwood8a978232015-05-29 19:06:14 +01001226 for (i = 0; i < count; i++) {
1227 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1228 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1229
1230 /* validate routes */
1231 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001232 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1233 ret = -EINVAL;
1234 break;
1235 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001236 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001237 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1238 ret = -EINVAL;
1239 break;
1240 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001241 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001242 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1243 ret = -EINVAL;
1244 break;
1245 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001246
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001247 routes[i]->source = elem->source;
1248 routes[i]->sink = elem->sink;
1249
1250 /* set to NULL atm for tplg users */
1251 routes[i]->connected = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001252 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001253 routes[i]->control = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001254 else
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001255 routes[i]->control = elem->control;
Liam Girdwood8a978232015-05-29 19:06:14 +01001256
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001257 /* add route dobj to dobj_list */
1258 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1259 routes[i]->dobj.ops = tplg->ops;
1260 routes[i]->dobj.index = tplg->index;
1261 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1262
Amadeusz Sławiński6856e882020-03-27 16:47:27 -04001263 ret = soc_tplg_add_route(tplg, routes[i]);
Pierre-Louis Bossart6f0307d2020-07-07 15:37:45 -05001264 if (ret < 0) {
1265 /*
1266 * this route was added to the list, it will
1267 * be freed in remove_route() so increment the
1268 * counter to skip it in the error handling
1269 * below.
1270 */
1271 i++;
Amadeusz Sławiński6856e882020-03-27 16:47:27 -04001272 break;
Pierre-Louis Bossart6f0307d2020-07-07 15:37:45 -05001273 }
Liam Girdwood503e79b2018-06-14 20:53:59 +01001274
Liam Girdwood8a978232015-05-29 19:06:14 +01001275 /* add route, but keep going if some fail */
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001276 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Liam Girdwood8a978232015-05-29 19:06:14 +01001277 }
1278
Pierre-Louis Bossart6f0307d2020-07-07 15:37:45 -05001279 /*
1280 * free memory allocated for all dapm routes not added to the
1281 * list in case of error
1282 */
1283 if (ret < 0) {
1284 while (i < count)
1285 kfree(routes[i++]);
1286 }
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001287
1288 /*
1289 * free pointer to array of dapm routes as this is no longer needed.
1290 * The memory allocated for each dapm route will be freed
1291 * when it is removed in remove_route().
1292 */
1293 kfree(routes);
1294
1295 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001296}
1297
1298static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1299 struct soc_tplg *tplg, int num_kcontrols)
1300{
1301 struct snd_kcontrol_new *kc;
1302 struct soc_mixer_control *sm;
1303 struct snd_soc_tplg_mixer_control *mc;
1304 int i, err;
1305
Axel Lin4ca7deb2015-08-05 22:34:22 +08001306 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001307 if (kc == NULL)
1308 return NULL;
1309
1310 for (i = 0; i < num_kcontrols; i++) {
1311 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001312
Liam Girdwood8a978232015-05-29 19:06:14 +01001313 /* validate kcontrol */
1314 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1315 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001316 goto err_sm;
1317
1318 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1319 if (sm == NULL)
1320 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001321
Liam Girdwood02b64242019-03-01 19:08:52 -06001322 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001323 le32_to_cpu(mc->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001324
Liam Girdwood8a978232015-05-29 19:06:14 +01001325 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1326 mc->hdr.name, i);
1327
Colin Ian King1ad741d2019-06-27 14:32:08 +01001328 kc[i].private_value = (long)sm;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001329 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1330 if (kc[i].name == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001331 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001332 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001333 kc[i].access = le32_to_cpu(mc->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001334
1335 /* we only support FL/FR channel mapping atm */
1336 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1337 SNDRV_CHMAP_FL);
1338 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1339 SNDRV_CHMAP_FR);
1340 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1341 SNDRV_CHMAP_FL);
1342 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1343 SNDRV_CHMAP_FR);
1344
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001345 sm->max = le32_to_cpu(mc->max);
1346 sm->min = le32_to_cpu(mc->min);
1347 sm->invert = le32_to_cpu(mc->invert);
1348 sm->platform_max = le32_to_cpu(mc->platform_max);
Liam Girdwood8a978232015-05-29 19:06:14 +01001349 sm->dobj.index = tplg->index;
1350 INIT_LIST_HEAD(&sm->dobj.list);
1351
1352 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001353 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001354 if (err) {
1355 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001356 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001357 }
1358
Bard liao3789deb2019-03-13 21:49:43 +08001359 /* create any TLV data */
Amadeusz Sławiński482db552020-03-27 16:47:25 -04001360 err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1361 if (err < 0) {
1362 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1363 mc->hdr.name);
1364 kfree(sm);
1365 continue;
1366 }
Bard liao3789deb2019-03-13 21:49:43 +08001367
Liam Girdwood8a978232015-05-29 19:06:14 +01001368 /* pass control to driver for optional further init */
1369 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1370 (struct snd_soc_tplg_ctl_hdr *)mc);
1371 if (err < 0) {
1372 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1373 mc->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001374 goto err_sm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001375 }
1376 }
1377 return kc;
1378
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001379err_sm:
1380 for (; i >= 0; i--) {
Pierre-Louis Bossart8edac482020-07-07 15:37:46 -05001381 soc_tplg_free_tlv(tplg, &kc[i]);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001382 sm = (struct soc_mixer_control *)kc[i].private_value;
1383 kfree(sm);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001384 kfree(kc[i].name);
1385 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001386 kfree(kc);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001387
Liam Girdwood8a978232015-05-29 19:06:14 +01001388 return NULL;
1389}
1390
1391static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001392 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001393{
1394 struct snd_kcontrol_new *kc;
1395 struct snd_soc_tplg_enum_control *ec;
1396 struct soc_enum *se;
Amadeusz Sławiński3cde8182019-06-17 13:36:43 +02001397 int i, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001398
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001399 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001400 if (kc == NULL)
1401 return NULL;
1402
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001403 for (i = 0; i < num_kcontrols; i++) {
1404 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1405 /* validate kcontrol */
1406 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1407 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001408 goto err_se;
Liam Girdwood8a978232015-05-29 19:06:14 +01001409
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001410 se = kzalloc(sizeof(*se), GFP_KERNEL);
1411 if (se == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001412 goto err_se;
Liam Girdwood8a978232015-05-29 19:06:14 +01001413
Liam Girdwood02b64242019-03-01 19:08:52 -06001414 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001415 le32_to_cpu(ec->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001416
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001417 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001418 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001419
Colin Ian King1ad741d2019-06-27 14:32:08 +01001420 kc[i].private_value = (long)se;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001421 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001422 if (kc[i].name == NULL)
Liam Girdwood267e2c62018-03-27 12:04:04 +01001423 goto err_se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001424 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001425 kc[i].access = le32_to_cpu(ec->hdr.access);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001426
1427 /* we only support FL/FR channel mapping atm */
1428 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1429 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1430 SNDRV_CHMAP_FL);
1431 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1432 SNDRV_CHMAP_FR);
1433
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001434 se->items = le32_to_cpu(ec->items);
1435 se->mask = le32_to_cpu(ec->mask);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001436 se->dobj.index = tplg->index;
1437
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001438 switch (le32_to_cpu(ec->hdr.ops.info)) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001439 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1440 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1441 err = soc_tplg_denum_create_values(se, ec);
1442 if (err < 0) {
1443 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1444 ec->hdr.name);
1445 goto err_se;
1446 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001447 /* fall through */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001448 case SND_SOC_TPLG_CTL_ENUM:
1449 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1450 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1451 err = soc_tplg_denum_create_texts(se, ec);
1452 if (err < 0) {
1453 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1454 ec->hdr.name);
1455 goto err_se;
1456 }
1457 break;
1458 default:
1459 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1460 ec->hdr.ops.info, ec->hdr.name);
1461 goto err_se;
1462 }
1463
1464 /* map io handlers */
1465 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1466 if (err) {
1467 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1468 goto err_se;
1469 }
1470
1471 /* pass control to driver for optional further init */
1472 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1473 (struct snd_soc_tplg_ctl_hdr *)ec);
1474 if (err < 0) {
1475 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1476 ec->hdr.name);
1477 goto err_se;
1478 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001479 }
1480
1481 return kc;
1482
1483err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001484 for (; i >= 0; i--) {
1485 /* free values and texts */
1486 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001487
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001488 if (se) {
1489 soc_tplg_denum_remove_values(se);
1490 soc_tplg_denum_remove_texts(se);
1491 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001492
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001493 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001494 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001495 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001496 kfree(kc);
1497
1498 return NULL;
1499}
1500
1501static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001502 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001503{
1504 struct snd_soc_tplg_bytes_control *be;
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001505 struct soc_bytes_ext *sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001506 struct snd_kcontrol_new *kc;
1507 int i, err;
1508
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001509 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001510 if (!kc)
1511 return NULL;
1512
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001513 for (i = 0; i < num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001514 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1515
1516 /* validate kcontrol */
1517 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1518 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001519 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001520
1521 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1522 if (sbe == NULL)
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001523 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001524
1525 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001526 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001527
1528 dev_dbg(tplg->dev,
1529 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1530 be->hdr.name, be->hdr.access);
1531
Colin Ian King1ad741d2019-06-27 14:32:08 +01001532 kc[i].private_value = (long)sbe;
Liam Girdwood267e2c62018-03-27 12:04:04 +01001533 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001534 if (kc[i].name == NULL)
1535 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001536 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001537 kc[i].access = le32_to_cpu(be->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001538
Pierre-Louis Bossart72bbeda2020-01-02 13:59:52 -06001539 sbe->max = le32_to_cpu(be->max);
Liam Girdwood8a978232015-05-29 19:06:14 +01001540 INIT_LIST_HEAD(&sbe->dobj.list);
1541
1542 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001543 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001544 if (err) {
1545 soc_control_err(tplg, &be->hdr, be->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001546 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001547 }
1548
1549 /* pass control to driver for optional further init */
1550 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1551 (struct snd_soc_tplg_ctl_hdr *)be);
1552 if (err < 0) {
1553 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1554 be->hdr.name);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001555 goto err_sbe;
Liam Girdwood8a978232015-05-29 19:06:14 +01001556 }
1557 }
1558
1559 return kc;
1560
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001561err_sbe:
1562 for (; i >= 0; i--) {
1563 sbe = (struct soc_bytes_ext *)kc[i].private_value;
1564 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001565 kfree(kc[i].name);
1566 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001567 kfree(kc);
Amadeusz Sławiński9f90af32019-06-17 13:36:44 +02001568
Liam Girdwood8a978232015-05-29 19:06:14 +01001569 return NULL;
1570}
1571
1572static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1573 struct snd_soc_tplg_dapm_widget *w)
1574{
1575 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1576 struct snd_soc_dapm_widget template, *widget;
1577 struct snd_soc_tplg_ctl_hdr *control_hdr;
1578 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001579 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001580 int ret = 0;
1581
1582 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1583 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1584 return -EINVAL;
1585 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1586 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1587 return -EINVAL;
1588
1589 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1590 w->name, w->id);
1591
1592 memset(&template, 0, sizeof(template));
1593
1594 /* map user to kernel widget ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001595 template.id = get_widget_id(le32_to_cpu(w->id));
Dan Carpenter752c9382019-09-25 14:06:24 +03001596 if ((int)template.id < 0)
Liam Girdwood8a978232015-05-29 19:06:14 +01001597 return template.id;
1598
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001599 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001600 template.name = kstrdup(w->name, GFP_KERNEL);
1601 if (!template.name)
1602 return -ENOMEM;
1603 template.sname = kstrdup(w->sname, GFP_KERNEL);
1604 if (!template.sname) {
1605 ret = -ENOMEM;
1606 goto err;
1607 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001608 template.reg = le32_to_cpu(w->reg);
1609 template.shift = le32_to_cpu(w->shift);
1610 template.mask = le32_to_cpu(w->mask);
1611 template.subseq = le32_to_cpu(w->subseq);
Liam Girdwood8a978232015-05-29 19:06:14 +01001612 template.on_val = w->invert ? 0 : 1;
1613 template.off_val = w->invert ? 1 : 0;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001614 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1615 template.event_flags = le16_to_cpu(w->event_flags);
Liam Girdwood8a978232015-05-29 19:06:14 +01001616 template.dobj.index = tplg->index;
1617
1618 tplg->pos +=
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001619 (sizeof(struct snd_soc_tplg_dapm_widget) +
1620 le32_to_cpu(w->priv.size));
1621
Liam Girdwood8a978232015-05-29 19:06:14 +01001622 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001623 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001624 template.num_kcontrols = 0;
1625 goto widget;
1626 }
1627
1628 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1629 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1630 w->name, w->num_kcontrols, control_hdr->type);
1631
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001632 switch (le32_to_cpu(control_hdr->ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001633 case SND_SOC_TPLG_CTL_VOLSW:
1634 case SND_SOC_TPLG_CTL_STROBE:
1635 case SND_SOC_TPLG_CTL_VOLSW_SX:
1636 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1637 case SND_SOC_TPLG_CTL_RANGE:
1638 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001639 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001640 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001641 template.kcontrol_news =
1642 soc_tplg_dapm_widget_dmixer_create(tplg,
1643 template.num_kcontrols);
1644 if (!template.kcontrol_news) {
1645 ret = -ENOMEM;
1646 goto hdr_err;
1647 }
1648 break;
1649 case SND_SOC_TPLG_CTL_ENUM:
1650 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1651 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1652 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1653 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001654 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001655 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001656 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001657 soc_tplg_dapm_widget_denum_create(tplg,
1658 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001659 if (!template.kcontrol_news) {
1660 ret = -ENOMEM;
1661 goto hdr_err;
1662 }
1663 break;
1664 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001665 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001666 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001667 template.kcontrol_news =
1668 soc_tplg_dapm_widget_dbytes_create(tplg,
1669 template.num_kcontrols);
1670 if (!template.kcontrol_news) {
1671 ret = -ENOMEM;
1672 goto hdr_err;
1673 }
1674 break;
1675 default:
1676 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1677 control_hdr->ops.get, control_hdr->ops.put,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001678 le32_to_cpu(control_hdr->ops.info));
Liam Girdwood8a978232015-05-29 19:06:14 +01001679 ret = -EINVAL;
1680 goto hdr_err;
1681 }
1682
1683widget:
1684 ret = soc_tplg_widget_load(tplg, &template, w);
1685 if (ret < 0)
1686 goto hdr_err;
1687
1688 /* card dapm mutex is held by the core if we are loading topology
1689 * data during sound card init. */
1690 if (card->instantiated)
1691 widget = snd_soc_dapm_new_control(dapm, &template);
1692 else
1693 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001694 if (IS_ERR(widget)) {
1695 ret = PTR_ERR(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001696 goto hdr_err;
1697 }
1698
1699 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001700 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001701 widget->dobj.ops = tplg->ops;
1702 widget->dobj.index = tplg->index;
1703 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001704
1705 ret = soc_tplg_widget_ready(tplg, widget, w);
1706 if (ret < 0)
1707 goto ready_err;
1708
Bard liao7620fe92019-01-25 14:06:45 -06001709 kfree(template.sname);
1710 kfree(template.name);
1711
Liam Girdwood8a978232015-05-29 19:06:14 +01001712 return 0;
1713
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001714ready_err:
1715 snd_soc_tplg_widget_remove(widget);
1716 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001717hdr_err:
1718 kfree(template.sname);
1719err:
1720 kfree(template.name);
1721 return ret;
1722}
1723
1724static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1725 struct snd_soc_tplg_hdr *hdr)
1726{
1727 struct snd_soc_tplg_dapm_widget *widget;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001728 int ret, count, i;
1729
1730 count = le32_to_cpu(hdr->count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001731
Liam Girdwood8a978232015-05-29 19:06:14 +01001732 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1733
1734 for (i = 0; i < count; i++) {
1735 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001736 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001737 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1738 return -EINVAL;
1739 }
1740
Liam Girdwood8a978232015-05-29 19:06:14 +01001741 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001742 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001743 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1744 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001745 return ret;
1746 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001747 }
1748
1749 return 0;
1750}
1751
1752static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1753{
1754 struct snd_soc_card *card = tplg->comp->card;
1755 int ret;
1756
1757 /* Card might not have been registered at this point.
1758 * If so, just return success.
1759 */
1760 if (!card || !card->instantiated) {
1761 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001762 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001763 return 0;
1764 }
1765
1766 ret = snd_soc_dapm_new_widgets(card);
1767 if (ret < 0)
1768 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1769 ret);
1770
1771 return 0;
1772}
1773
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001774static int set_stream_info(struct snd_soc_pcm_stream *stream,
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001775 struct snd_soc_tplg_stream_caps *caps)
1776{
1777 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001778 if (!stream->stream_name)
1779 return -ENOMEM;
1780
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001781 stream->channels_min = le32_to_cpu(caps->channels_min);
1782 stream->channels_max = le32_to_cpu(caps->channels_max);
1783 stream->rates = le32_to_cpu(caps->rates);
1784 stream->rate_min = le32_to_cpu(caps->rate_min);
1785 stream->rate_max = le32_to_cpu(caps->rate_max);
1786 stream->formats = le64_to_cpu(caps->formats);
1787 stream->sig_bits = le32_to_cpu(caps->sig_bits);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001788
1789 return 0;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001790}
1791
Mengdong Lin0038be92016-07-26 14:32:37 +08001792static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1793 unsigned int flag_mask, unsigned int flags)
1794{
1795 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1796 dai_drv->symmetric_rates =
1797 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1798
1799 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1800 dai_drv->symmetric_channels =
1801 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1802 1 : 0;
1803
1804 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1805 dai_drv->symmetric_samplebits =
1806 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1807 1 : 0;
1808}
1809
Mengdong Lin64527e82016-01-15 16:13:28 +08001810static int soc_tplg_dai_create(struct soc_tplg *tplg,
1811 struct snd_soc_tplg_pcm *pcm)
1812{
1813 struct snd_soc_dai_driver *dai_drv;
1814 struct snd_soc_pcm_stream *stream;
1815 struct snd_soc_tplg_stream_caps *caps;
Kuninori Morimotoe443c202019-11-05 15:47:14 +09001816 struct snd_soc_dai *dai;
1817 struct snd_soc_dapm_context *dapm =
1818 snd_soc_component_get_dapm(tplg->comp);
Mengdong Lin64527e82016-01-15 16:13:28 +08001819 int ret;
1820
1821 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1822 if (dai_drv == NULL)
1823 return -ENOMEM;
1824
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001825 if (strlen(pcm->dai_name)) {
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001826 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001827 if (!dai_drv->name) {
1828 ret = -ENOMEM;
1829 goto err;
1830 }
1831 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001832 dai_drv->id = le32_to_cpu(pcm->dai_id);
Mengdong Lin64527e82016-01-15 16:13:28 +08001833
1834 if (pcm->playback) {
1835 stream = &dai_drv->playback;
1836 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001837 ret = set_stream_info(stream, caps);
1838 if (ret < 0)
1839 goto err;
Mengdong Lin64527e82016-01-15 16:13:28 +08001840 }
1841
1842 if (pcm->capture) {
1843 stream = &dai_drv->capture;
1844 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001845 ret = set_stream_info(stream, caps);
1846 if (ret < 0)
1847 goto err;
Mengdong Lin64527e82016-01-15 16:13:28 +08001848 }
1849
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001850 if (pcm->compress)
1851 dai_drv->compress_new = snd_soc_new_compress;
1852
Mengdong Lin64527e82016-01-15 16:13:28 +08001853 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001854 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001855 if (ret < 0) {
1856 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001857 goto err;
Mengdong Lin64527e82016-01-15 16:13:28 +08001858 }
1859
1860 dai_drv->dobj.index = tplg->index;
1861 dai_drv->dobj.ops = tplg->ops;
1862 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1863 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1864
1865 /* register the DAI to the component */
Pierre-Louis Bossart6ae49022020-06-12 15:59:38 -05001866 dai = devm_snd_soc_register_dai(tplg->comp->dev, tplg->comp, dai_drv, false);
Kuninori Morimotoe443c202019-11-05 15:47:14 +09001867 if (!dai)
1868 return -ENOMEM;
1869
1870 /* Create the DAI widgets here */
1871 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1872 if (ret != 0) {
1873 dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
Kuninori Morimotoe443c202019-11-05 15:47:14 +09001874 return ret;
1875 }
1876
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001877 return 0;
1878
1879err:
1880 kfree(dai_drv->playback.stream_name);
1881 kfree(dai_drv->capture.stream_name);
1882 kfree(dai_drv->name);
1883 kfree(dai_drv);
1884
Kuninori Morimotoe443c202019-11-05 15:47:14 +09001885 return ret;
Mengdong Lin64527e82016-01-15 16:13:28 +08001886}
1887
Mengdong Lin717a8e72016-11-03 01:03:34 +08001888static void set_link_flags(struct snd_soc_dai_link *link,
1889 unsigned int flag_mask, unsigned int flags)
1890{
1891 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1892 link->symmetric_rates =
1893 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1894
1895 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1896 link->symmetric_channels =
1897 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1898 1 : 0;
1899
1900 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1901 link->symmetric_samplebits =
1902 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1903 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001904
1905 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1906 link->ignore_suspend =
1907 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1908 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001909}
1910
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001911/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001912static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001913 struct snd_soc_tplg_pcm *pcm)
1914{
1915 struct snd_soc_dai_link *link;
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001916 struct snd_soc_dai_link_component *dlc;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001917 int ret;
1918
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001919 /* link + cpu + codec + platform */
1920 link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001921 if (link == NULL)
1922 return -ENOMEM;
1923
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001924 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1925
1926 link->cpus = &dlc[0];
1927 link->codecs = &dlc[1];
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001928 link->platforms = &dlc[2];
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001929
1930 link->num_cpus = 1;
1931 link->num_codecs = 1;
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001932 link->num_platforms = 1;
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001933
Jaroslav Kysela8ce1cbd62020-01-22 20:07:52 +01001934 link->dobj.index = tplg->index;
1935 link->dobj.ops = tplg->ops;
1936 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1937
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001938 if (strlen(pcm->pcm_name)) {
1939 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1940 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001941 if (!link->name || !link->stream_name) {
1942 ret = -ENOMEM;
1943 goto err;
1944 }
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001945 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001946 link->id = le32_to_cpu(pcm->pcm_id);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001947
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001948 if (strlen(pcm->dai_name)) {
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001949 link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04001950 if (!link->cpus->dai_name) {
1951 ret = -ENOMEM;
1952 goto err;
1953 }
1954 }
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001955
Kuninori Morimoto23b946c2019-06-06 13:19:14 +09001956 link->codecs->name = "snd-soc-dummy";
1957 link->codecs->dai_name = "snd-soc-dummy-dai";
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001958
Pierre-Louis Bossart3e6de892019-06-12 11:38:45 -05001959 link->platforms->name = "snd-soc-dummy";
1960
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001961 /* enable DPCM */
1962 link->dynamic = 1;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001963 link->dpcm_playback = le32_to_cpu(pcm->playback);
1964 link->dpcm_capture = le32_to_cpu(pcm->capture);
Mengdong Lin717a8e72016-11-03 01:03:34 +08001965 if (pcm->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001966 set_link_flags(link,
1967 le32_to_cpu(pcm->flag_mask),
1968 le32_to_cpu(pcm->flags));
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001969
Mengdong Linacfc7d42016-01-15 16:13:37 +08001970 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001971 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001972 if (ret < 0) {
1973 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
Dragos Tarcatu76d27032019-12-09 18:39:38 -06001974 goto err;
1975 }
1976
Mark Brown2acf6ce2019-12-10 13:27:14 +00001977 ret = snd_soc_add_pcm_runtime(tplg->comp->card, link);
Dragos Tarcatu76d27032019-12-09 18:39:38 -06001978 if (ret < 0) {
1979 dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n");
1980 goto err;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001981 }
1982
Mengdong Linacfc7d42016-01-15 16:13:37 +08001983 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1984
Mengdong Linacfc7d42016-01-15 16:13:37 +08001985 return 0;
Dragos Tarcatu76d27032019-12-09 18:39:38 -06001986err:
1987 kfree(link->name);
1988 kfree(link->stream_name);
1989 kfree(link->cpus->dai_name);
1990 kfree(link);
1991 return ret;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001992}
1993
1994/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001995static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1996 struct snd_soc_tplg_pcm *pcm)
1997{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001998 int ret;
1999
2000 ret = soc_tplg_dai_create(tplg, pcm);
2001 if (ret < 0)
2002 return ret;
2003
Mengdong Linab4bc5e2016-11-03 01:04:42 +08002004 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08002005}
2006
Mengdong Lin55726dc2016-11-03 01:00:16 +08002007/* copy stream caps from the old version 4 of source */
2008static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
2009 struct snd_soc_tplg_stream_caps_v4 *src)
2010{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002011 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin55726dc2016-11-03 01:00:16 +08002012 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2013 dest->formats = src->formats;
2014 dest->rates = src->rates;
2015 dest->rate_min = src->rate_min;
2016 dest->rate_max = src->rate_max;
2017 dest->channels_min = src->channels_min;
2018 dest->channels_max = src->channels_max;
2019 dest->periods_min = src->periods_min;
2020 dest->periods_max = src->periods_max;
2021 dest->period_size_min = src->period_size_min;
2022 dest->period_size_max = src->period_size_max;
2023 dest->buffer_size_min = src->buffer_size_min;
2024 dest->buffer_size_max = src->buffer_size_max;
2025}
2026
2027/**
2028 * pcm_new_ver - Create the new version of PCM from the old version.
2029 * @tplg: topology context
2030 * @src: older version of pcm as a source
2031 * @pcm: latest version of pcm created from the source
2032 *
2033 * Support from vesion 4. User should free the returned pcm manually.
2034 */
2035static int pcm_new_ver(struct soc_tplg *tplg,
2036 struct snd_soc_tplg_pcm *src,
2037 struct snd_soc_tplg_pcm **pcm)
2038{
2039 struct snd_soc_tplg_pcm *dest;
2040 struct snd_soc_tplg_pcm_v4 *src_v4;
2041 int i;
2042
2043 *pcm = NULL;
2044
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002045 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002046 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
2047 return -EINVAL;
2048 }
2049
2050 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
2051 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
2052 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2053 if (!dest)
2054 return -ENOMEM;
2055
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002056 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin55726dc2016-11-03 01:00:16 +08002057 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2058 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2059 dest->pcm_id = src_v4->pcm_id;
2060 dest->dai_id = src_v4->dai_id;
2061 dest->playback = src_v4->playback;
2062 dest->capture = src_v4->capture;
2063 dest->compress = src_v4->compress;
2064 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002065 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin55726dc2016-11-03 01:00:16 +08002066 memcpy(&dest->stream[i], &src_v4->stream[i],
2067 sizeof(struct snd_soc_tplg_stream));
2068
2069 for (i = 0; i < 2; i++)
2070 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2071
2072 *pcm = dest;
2073 return 0;
2074}
2075
Mengdong Lin64527e82016-01-15 16:13:28 +08002076static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01002077 struct snd_soc_tplg_hdr *hdr)
2078{
Mengdong Lin55726dc2016-11-03 01:00:16 +08002079 struct snd_soc_tplg_pcm *pcm, *_pcm;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002080 int count;
2081 int size;
Vinod Koulfd340452016-12-08 23:01:27 +05302082 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08002083 bool abi_match;
Dragos Tarcatua3039ae2019-12-09 18:39:39 -06002084 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002085
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002086 count = le32_to_cpu(hdr->count);
2087
Mengdong Lin55726dc2016-11-03 01:00:16 +08002088 /* check the element size and count */
2089 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002090 size = le32_to_cpu(pcm->size);
2091 if (size > sizeof(struct snd_soc_tplg_pcm)
2092 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002093 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002094 size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002095 return -EINVAL;
2096 }
2097
Liam Girdwood8a978232015-05-29 19:06:14 +01002098 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002099 size, count,
2100 le32_to_cpu(hdr->payload_size),
2101 "PCM DAI")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002102 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2103 count);
2104 return -EINVAL;
2105 }
2106
Mengdong Lin64527e82016-01-15 16:13:28 +08002107 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002108 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002109 size = le32_to_cpu(pcm->size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002110
2111 /* check ABI version by size, create a new version of pcm
2112 * if abi not match.
2113 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002114 if (size == sizeof(*pcm)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002115 abi_match = true;
2116 _pcm = pcm;
2117 } else {
2118 abi_match = false;
Amadeusz Sławińskib3677fc32020-03-27 16:47:28 -04002119 ret = pcm_new_ver(tplg, pcm, &_pcm);
2120 if (ret < 0)
2121 return ret;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002122 }
2123
Mengdong Lin55726dc2016-11-03 01:00:16 +08002124 /* create the FE DAIs and DAI links */
Dragos Tarcatua3039ae2019-12-09 18:39:39 -06002125 ret = soc_tplg_pcm_create(tplg, _pcm);
2126 if (ret < 0) {
2127 if (!abi_match)
2128 kfree(_pcm);
2129 return ret;
2130 }
Mengdong Lin55726dc2016-11-03 01:00:16 +08002131
Mengdong Lin717a8e72016-11-03 01:03:34 +08002132 /* offset by version-specific struct size and
2133 * real priv data size
2134 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002135 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
Mengdong Lin717a8e72016-11-03 01:03:34 +08002136
Mengdong Lin55726dc2016-11-03 01:00:16 +08002137 if (!abi_match)
2138 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08002139 }
2140
Liam Girdwood8a978232015-05-29 19:06:14 +01002141 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002142
Liam Girdwood8a978232015-05-29 19:06:14 +01002143 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002144}
2145
Mengdong Lin593d9e52016-11-03 01:04:27 +08002146/**
2147 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002148 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002149 * @cfg: physical link configs.
2150 *
2151 * Topology context contains a list of supported HW formats (configs) and
2152 * a default format ID for the physical link. This function will use this
2153 * default ID to choose the HW format to set the link's DAI format for init.
2154 */
2155static void set_link_hw_format(struct snd_soc_dai_link *link,
2156 struct snd_soc_tplg_link_config *cfg)
2157{
2158 struct snd_soc_tplg_hw_config *hw_config;
2159 unsigned char bclk_master, fsync_master;
2160 unsigned char invert_bclk, invert_fsync;
2161 int i;
2162
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002163 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002164 hw_config = &cfg->hw_config[i];
2165 if (hw_config->id != cfg->default_hw_config_id)
2166 continue;
2167
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002168 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2169 SND_SOC_DAIFMT_FORMAT_MASK;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002170
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002171 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002172 switch (hw_config->clock_gated) {
2173 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002174 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002175 break;
2176
2177 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002178 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002179 break;
2180
2181 default:
2182 /* ignore the value */
2183 break;
2184 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002185
Mengdong Lin593d9e52016-11-03 01:04:27 +08002186 /* clock signal polarity */
2187 invert_bclk = hw_config->invert_bclk;
2188 invert_fsync = hw_config->invert_fsync;
2189 if (!invert_bclk && !invert_fsync)
2190 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2191 else if (!invert_bclk && invert_fsync)
2192 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2193 else if (invert_bclk && !invert_fsync)
2194 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2195 else
2196 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2197
2198 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002199 bclk_master = (hw_config->bclk_master ==
2200 SND_SOC_TPLG_BCLK_CM);
2201 fsync_master = (hw_config->fsync_master ==
2202 SND_SOC_TPLG_FSYNC_CM);
2203 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002204 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002205 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002206 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2207 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002208 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2209 else
2210 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2211 }
2212}
2213
2214/**
2215 * link_new_ver - Create a new physical link config from the old
2216 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002217 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002218 * @src: old version of phyical link config as a source
2219 * @link: latest version of physical link config created from the source
2220 *
2221 * Support from vesion 4. User need free the returned link config manually.
2222 */
2223static int link_new_ver(struct soc_tplg *tplg,
2224 struct snd_soc_tplg_link_config *src,
2225 struct snd_soc_tplg_link_config **link)
2226{
2227 struct snd_soc_tplg_link_config *dest;
2228 struct snd_soc_tplg_link_config_v4 *src_v4;
2229 int i;
2230
2231 *link = NULL;
2232
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002233 if (le32_to_cpu(src->size) !=
2234 sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002235 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2236 return -EINVAL;
2237 }
2238
2239 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2240
2241 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2242 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2243 if (!dest)
2244 return -ENOMEM;
2245
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002246 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002247 dest->id = src_v4->id;
2248 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002249 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002250 memcpy(&dest->stream[i], &src_v4->stream[i],
2251 sizeof(struct snd_soc_tplg_stream));
2252
2253 *link = dest;
2254 return 0;
2255}
2256
Kuninori Morimotod6f31e02019-12-10 09:34:14 +09002257/**
2258 * snd_soc_find_dai_link - Find a DAI link
2259 *
2260 * @card: soc card
2261 * @id: DAI link ID to match
2262 * @name: DAI link name to match, optional
2263 * @stream_name: DAI link stream name to match, optional
2264 *
2265 * This function will search all existing DAI links of the soc card to
2266 * find the link of the same ID. Since DAI links may not have their
2267 * unique ID, so name and stream name should also match if being
2268 * specified.
2269 *
2270 * Return: pointer of DAI link, or NULL if not found.
2271 */
2272static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2273 int id, const char *name,
2274 const char *stream_name)
2275{
2276 struct snd_soc_pcm_runtime *rtd;
2277 struct snd_soc_dai_link *link;
2278
2279 for_each_card_rtds(card, rtd) {
2280 link = rtd->dai_link;
2281
2282 if (link->id != id)
2283 continue;
2284
2285 if (name && (!link->name || strcmp(name, link->name)))
2286 continue;
2287
2288 if (stream_name && (!link->stream_name
2289 || strcmp(stream_name, link->stream_name)))
2290 continue;
2291
2292 return link;
2293 }
2294
2295 return NULL;
2296}
2297
Mengdong Lin593d9e52016-11-03 01:04:27 +08002298/* Find and configure an existing physical DAI link */
2299static int soc_tplg_link_config(struct soc_tplg *tplg,
2300 struct snd_soc_tplg_link_config *cfg)
2301{
2302 struct snd_soc_dai_link *link;
2303 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002304 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002305 int ret;
2306
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002307 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2308 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2309 return -EINVAL;
2310 else if (len)
2311 name = cfg->name;
2312 else
2313 name = NULL;
2314
2315 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2316 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2317 return -EINVAL;
2318 else if (len)
2319 stream_name = cfg->stream_name;
2320 else
2321 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002322
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002323 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
Mengdong Lin593d9e52016-11-03 01:04:27 +08002324 name, stream_name);
2325 if (!link) {
2326 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2327 name, cfg->id);
2328 return -EINVAL;
2329 }
2330
2331 /* hw format */
2332 if (cfg->num_hw_configs)
2333 set_link_hw_format(link, cfg);
2334
2335 /* flags */
2336 if (cfg->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002337 set_link_flags(link,
2338 le32_to_cpu(cfg->flag_mask),
2339 le32_to_cpu(cfg->flags));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002340
2341 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002342 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002343 if (ret < 0) {
2344 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2345 return ret;
2346 }
2347
Bard liaoadfebb52019-02-01 11:07:40 -06002348 /* for unloading it in snd_soc_tplg_component_remove */
2349 link->dobj.index = tplg->index;
2350 link->dobj.ops = tplg->ops;
2351 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2352 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2353
Mengdong Lin593d9e52016-11-03 01:04:27 +08002354 return 0;
2355}
2356
2357
2358/* Load physical link config elements from the topology context */
2359static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2360 struct snd_soc_tplg_hdr *hdr)
2361{
2362 struct snd_soc_tplg_link_config *link, *_link;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002363 int count;
2364 int size;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002365 int i, ret;
2366 bool abi_match;
2367
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002368 count = le32_to_cpu(hdr->count);
2369
Mengdong Lin593d9e52016-11-03 01:04:27 +08002370 /* check the element size and count */
2371 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002372 size = le32_to_cpu(link->size);
2373 if (size > sizeof(struct snd_soc_tplg_link_config)
2374 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002375 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002376 size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002377 return -EINVAL;
2378 }
2379
2380 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002381 size, count,
2382 le32_to_cpu(hdr->payload_size),
2383 "physical link config")) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002384 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2385 count);
2386 return -EINVAL;
2387 }
2388
2389 /* config physical DAI links */
2390 for (i = 0; i < count; i++) {
2391 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002392 size = le32_to_cpu(link->size);
2393 if (size == sizeof(*link)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002394 abi_match = true;
2395 _link = link;
2396 } else {
2397 abi_match = false;
2398 ret = link_new_ver(tplg, link, &_link);
2399 if (ret < 0)
2400 return ret;
2401 }
2402
2403 ret = soc_tplg_link_config(tplg, _link);
Dragos Tarcatu2b2d5c42020-02-07 20:53:24 +02002404 if (ret < 0) {
2405 if (!abi_match)
2406 kfree(_link);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002407 return ret;
Dragos Tarcatu2b2d5c42020-02-07 20:53:24 +02002408 }
Mengdong Lin593d9e52016-11-03 01:04:27 +08002409
2410 /* offset by version-specific struct size and
2411 * real priv data size
2412 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002413 tplg->pos += size + le32_to_cpu(_link->priv.size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002414
2415 if (!abi_match)
2416 kfree(_link); /* free the duplicated one */
2417 }
2418
2419 return 0;
2420}
2421
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002422/**
2423 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002424 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002425 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002426 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002427 * The physical dai should already be registered by the platform driver.
2428 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002429 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002430static int soc_tplg_dai_config(struct soc_tplg *tplg,
2431 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002432{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002433 struct snd_soc_dai_link_component dai_component;
Mengdong Lin0038be92016-07-26 14:32:37 +08002434 struct snd_soc_dai *dai;
2435 struct snd_soc_dai_driver *dai_drv;
2436 struct snd_soc_pcm_stream *stream;
2437 struct snd_soc_tplg_stream_caps *caps;
2438 int ret;
2439
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002440 memset(&dai_component, 0, sizeof(dai_component));
2441
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002442 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002443 dai = snd_soc_find_dai(&dai_component);
2444 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002445 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2446 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002447 return -EINVAL;
2448 }
2449
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002450 if (le32_to_cpu(d->dai_id) != dai->id) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002451 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2452 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002453 return -EINVAL;
2454 }
2455
2456 dai_drv = dai->driver;
2457 if (!dai_drv)
2458 return -EINVAL;
2459
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002460 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002461 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002462 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04002463 ret = set_stream_info(stream, caps);
2464 if (ret < 0)
2465 goto err;
Mengdong Lin0038be92016-07-26 14:32:37 +08002466 }
2467
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002468 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002469 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002470 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04002471 ret = set_stream_info(stream, caps);
2472 if (ret < 0)
2473 goto err;
Mengdong Lin0038be92016-07-26 14:32:37 +08002474 }
2475
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002476 if (d->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002477 set_dai_flags(dai_drv,
2478 le32_to_cpu(d->flag_mask),
2479 le32_to_cpu(d->flags));
Mengdong Lin0038be92016-07-26 14:32:37 +08002480
2481 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002482 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002483 if (ret < 0) {
2484 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04002485 goto err;
Mengdong Lin0038be92016-07-26 14:32:37 +08002486 }
2487
2488 return 0;
Amadeusz Sławińskiabc3caa2020-03-27 16:47:24 -04002489
2490err:
2491 kfree(dai_drv->playback.stream_name);
2492 kfree(dai_drv->capture.stream_name);
2493 return ret;
Mengdong Lin0038be92016-07-26 14:32:37 +08002494}
2495
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002496/* load physical DAI elements */
2497static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2498 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002499{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002500 struct snd_soc_tplg_dai *dai;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002501 int count;
Amadeusz Sławińskidd8e8712020-03-27 16:47:29 -04002502 int i, ret;
Mengdong Lin0038be92016-07-26 14:32:37 +08002503
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002504 count = le32_to_cpu(hdr->count);
2505
Mengdong Lin0038be92016-07-26 14:32:37 +08002506 /* config the existing BE DAIs */
2507 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002508 dai = (struct snd_soc_tplg_dai *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002509 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002510 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002511 return -EINVAL;
2512 }
2513
Amadeusz Sławińskidd8e8712020-03-27 16:47:29 -04002514 ret = soc_tplg_dai_config(tplg, dai);
2515 if (ret < 0) {
2516 dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2517 return ret;
2518 }
2519
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002520 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
Mengdong Lin0038be92016-07-26 14:32:37 +08002521 }
2522
2523 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2524 return 0;
2525}
2526
Mengdong Lin583958f2016-10-11 14:36:42 +08002527/**
2528 * manifest_new_ver - Create a new version of manifest from the old version
2529 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002530 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002531 * @src: old version of manifest as a source
2532 * @manifest: latest version of manifest created from the source
2533 *
2534 * Support from vesion 4. Users need free the returned manifest manually.
2535 */
2536static int manifest_new_ver(struct soc_tplg *tplg,
2537 struct snd_soc_tplg_manifest *src,
2538 struct snd_soc_tplg_manifest **manifest)
2539{
2540 struct snd_soc_tplg_manifest *dest;
2541 struct snd_soc_tplg_manifest_v4 *src_v4;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002542 int size;
Mengdong Lin583958f2016-10-11 14:36:42 +08002543
2544 *manifest = NULL;
2545
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002546 size = le32_to_cpu(src->size);
2547 if (size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002548 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002549 size);
2550 if (size)
Guenter Roeckac9391d2018-05-24 12:49:21 -07002551 return -EINVAL;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002552 src->size = cpu_to_le32(sizeof(*src_v4));
Mengdong Lin583958f2016-10-11 14:36:42 +08002553 }
2554
2555 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2556
2557 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002558 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2559 GFP_KERNEL);
Mengdong Lin583958f2016-10-11 14:36:42 +08002560 if (!dest)
2561 return -ENOMEM;
2562
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002563 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin583958f2016-10-11 14:36:42 +08002564 dest->control_elems = src_v4->control_elems;
2565 dest->widget_elems = src_v4->widget_elems;
2566 dest->graph_elems = src_v4->graph_elems;
2567 dest->pcm_elems = src_v4->pcm_elems;
2568 dest->dai_link_elems = src_v4->dai_link_elems;
2569 dest->priv.size = src_v4->priv.size;
2570 if (dest->priv.size)
2571 memcpy(dest->priv.data, src_v4->priv.data,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002572 le32_to_cpu(src_v4->priv.size));
Mengdong Lin583958f2016-10-11 14:36:42 +08002573
2574 *manifest = dest;
2575 return 0;
2576}
Mengdong Lin0038be92016-07-26 14:32:37 +08002577
Liam Girdwood8a978232015-05-29 19:06:14 +01002578static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002579 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002580{
Mengdong Lin583958f2016-10-11 14:36:42 +08002581 struct snd_soc_tplg_manifest *manifest, *_manifest;
2582 bool abi_match;
Dragos Tarcatu242c46c2020-02-07 20:53:25 +02002583 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002584
Liam Girdwood8a978232015-05-29 19:06:14 +01002585 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002586
2587 /* check ABI version by size, create a new manifest if abi not match */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002588 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
Mengdong Lin583958f2016-10-11 14:36:42 +08002589 abi_match = true;
2590 _manifest = manifest;
2591 } else {
2592 abi_match = false;
Dragos Tarcatu242c46c2020-02-07 20:53:25 +02002593 ret = manifest_new_ver(tplg, manifest, &_manifest);
2594 if (ret < 0)
2595 return ret;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002596 }
2597
Mengdong Lin583958f2016-10-11 14:36:42 +08002598 /* pass control to component driver for optional further init */
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -04002599 if (tplg->ops && tplg->ops->manifest)
Dragos Tarcatu242c46c2020-02-07 20:53:25 +02002600 ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002601
Mengdong Lin583958f2016-10-11 14:36:42 +08002602 if (!abi_match) /* free the duplicated one */
2603 kfree(_manifest);
2604
Dragos Tarcatu242c46c2020-02-07 20:53:25 +02002605 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002606}
2607
2608/* validate header magic, size and type */
2609static int soc_valid_header(struct soc_tplg *tplg,
2610 struct snd_soc_tplg_hdr *hdr)
2611{
2612 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2613 return 0;
2614
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002615 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002616 dev_err(tplg->dev,
2617 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002618 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002619 tplg->fw->size);
2620 return -EINVAL;
2621 }
2622
Liam Girdwood8a978232015-05-29 19:06:14 +01002623 /* big endian firmware objects not supported atm */
Amadeusz Sławiński26d87882020-04-15 12:24:35 -04002624 if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002625 dev_err(tplg->dev,
2626 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2627 tplg->pass, hdr->magic,
2628 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2629 return -EINVAL;
2630 }
2631
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002632 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002633 dev_err(tplg->dev,
2634 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2635 tplg->pass, hdr->magic,
2636 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2637 return -EINVAL;
2638 }
2639
Mengdong Lin288b8da2016-11-03 01:03:17 +08002640 /* Support ABI from version 4 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002641 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2642 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002643 dev_err(tplg->dev,
2644 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2645 tplg->pass, hdr->abi,
2646 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2647 tplg->fw->size);
2648 return -EINVAL;
2649 }
2650
2651 if (hdr->payload_size == 0) {
2652 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2653 soc_tplg_get_hdr_offset(tplg));
2654 return -EINVAL;
2655 }
2656
Liam Girdwood8a978232015-05-29 19:06:14 +01002657 return 1;
2658}
2659
2660/* check header type and call appropriate handler */
2661static int soc_tplg_load_header(struct soc_tplg *tplg,
2662 struct snd_soc_tplg_hdr *hdr)
2663{
Keyon Jie82ed7412020-05-27 10:28:00 +08002664 int (*elem_load)(struct soc_tplg *tplg,
2665 struct snd_soc_tplg_hdr *hdr);
2666 unsigned int hdr_pass;
2667
Liam Girdwood8a978232015-05-29 19:06:14 +01002668 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2669
2670 /* check for matching ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002671 if (le32_to_cpu(hdr->index) != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002672 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002673 return 0;
2674
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002675 tplg->index = le32_to_cpu(hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01002676
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002677 switch (le32_to_cpu(hdr->type)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002678 case SND_SOC_TPLG_TYPE_MIXER:
2679 case SND_SOC_TPLG_TYPE_ENUM:
2680 case SND_SOC_TPLG_TYPE_BYTES:
Keyon Jie82ed7412020-05-27 10:28:00 +08002681 hdr_pass = SOC_TPLG_PASS_MIXER;
2682 elem_load = soc_tplg_kcontrol_elems_load;
2683 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002684 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
Keyon Jie82ed7412020-05-27 10:28:00 +08002685 hdr_pass = SOC_TPLG_PASS_GRAPH;
2686 elem_load = soc_tplg_dapm_graph_elems_load;
2687 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002688 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
Keyon Jie82ed7412020-05-27 10:28:00 +08002689 hdr_pass = SOC_TPLG_PASS_WIDGET;
2690 elem_load = soc_tplg_dapm_widget_elems_load;
2691 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002692 case SND_SOC_TPLG_TYPE_PCM:
Keyon Jie82ed7412020-05-27 10:28:00 +08002693 hdr_pass = SOC_TPLG_PASS_PCM_DAI;
2694 elem_load = soc_tplg_pcm_elems_load;
2695 break;
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002696 case SND_SOC_TPLG_TYPE_DAI:
Keyon Jie82ed7412020-05-27 10:28:00 +08002697 hdr_pass = SOC_TPLG_PASS_BE_DAI;
2698 elem_load = soc_tplg_dai_elems_load;
2699 break;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002700 case SND_SOC_TPLG_TYPE_DAI_LINK:
2701 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2702 /* physical link configurations */
Keyon Jie82ed7412020-05-27 10:28:00 +08002703 hdr_pass = SOC_TPLG_PASS_LINK;
2704 elem_load = soc_tplg_link_elems_load;
2705 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002706 case SND_SOC_TPLG_TYPE_MANIFEST:
Keyon Jie82ed7412020-05-27 10:28:00 +08002707 hdr_pass = SOC_TPLG_PASS_MANIFEST;
2708 elem_load = soc_tplg_manifest_load;
2709 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002710 default:
2711 /* bespoke vendor data object */
Keyon Jie82ed7412020-05-27 10:28:00 +08002712 hdr_pass = SOC_TPLG_PASS_VENDOR;
2713 elem_load = soc_tplg_vendor_load;
2714 break;
2715 }
2716
2717 if (tplg->pass == hdr_pass) {
2718 dev_dbg(tplg->dev,
2719 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2720 hdr->payload_size, hdr->type, hdr->version,
2721 hdr->vendor_type, tplg->pass);
2722 return elem_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002723 }
2724
2725 return 0;
2726}
2727
2728/* process the topology file headers */
2729static int soc_tplg_process_headers(struct soc_tplg *tplg)
2730{
2731 struct snd_soc_tplg_hdr *hdr;
2732 int ret;
2733
2734 tplg->pass = SOC_TPLG_PASS_START;
2735
2736 /* process the header types from start to end */
2737 while (tplg->pass <= SOC_TPLG_PASS_END) {
2738
2739 tplg->hdr_pos = tplg->fw->data;
2740 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2741
2742 while (!soc_tplg_is_eof(tplg)) {
2743
2744 /* make sure header is valid before loading */
2745 ret = soc_valid_header(tplg, hdr);
2746 if (ret < 0)
2747 return ret;
2748 else if (ret == 0)
2749 break;
2750
2751 /* load the header object */
2752 ret = soc_tplg_load_header(tplg, hdr);
2753 if (ret < 0)
2754 return ret;
2755
2756 /* goto next header */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002757 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
Liam Girdwood8a978232015-05-29 19:06:14 +01002758 sizeof(struct snd_soc_tplg_hdr);
2759 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2760 }
2761
2762 /* next data type pass */
2763 tplg->pass++;
2764 }
2765
2766 /* signal DAPM we are complete */
2767 ret = soc_tplg_dapm_complete(tplg);
2768 if (ret < 0)
2769 dev_err(tplg->dev,
2770 "ASoC: failed to initialise DAPM from Firmware\n");
2771
2772 return ret;
2773}
2774
2775static int soc_tplg_load(struct soc_tplg *tplg)
2776{
2777 int ret;
2778
2779 ret = soc_tplg_process_headers(tplg);
2780 if (ret == 0)
2781 soc_tplg_complete(tplg);
2782
2783 return ret;
2784}
2785
2786/* load audio component topology from "firmware" file */
2787int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2788 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2789{
2790 struct soc_tplg tplg;
Bard liao304017d2019-02-17 21:23:47 +08002791 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002792
Amadeusz Sławińskic42464a2020-03-12 08:22:39 -04002793 /* component needs to exist to keep and reference data while parsing */
2794 if (!comp)
2795 return -EINVAL;
2796
Liam Girdwood8a978232015-05-29 19:06:14 +01002797 /* setup parsing context */
2798 memset(&tplg, 0, sizeof(tplg));
2799 tplg.fw = fw;
2800 tplg.dev = comp->dev;
2801 tplg.comp = comp;
2802 tplg.ops = ops;
2803 tplg.req_index = id;
2804 tplg.io_ops = ops->io_ops;
2805 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002806 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2807 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002808
Bard liao304017d2019-02-17 21:23:47 +08002809 ret = soc_tplg_load(&tplg);
2810 /* free the created components if fail to load topology */
2811 if (ret)
2812 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2813
2814 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002815}
2816EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2817
2818/* remove this dynamic widget */
2819void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2820{
2821 /* make sure we are a widget */
2822 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2823 return;
2824
2825 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2826}
2827EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2828
2829/* remove all dynamic widgets from this DAPM context */
2830void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2831 u32 index)
2832{
2833 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002834
Kuninori Morimoto14596692020-03-09 13:08:21 +09002835 for_each_card_widgets_safe(dapm->card, w, next_w) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002836
2837 /* make sure we are a widget with correct context */
2838 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2839 continue;
2840
2841 /* match ID */
2842 if (w->dobj.index != index &&
2843 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2844 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002845 /* check and free and dynamic widget kcontrols */
2846 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002847 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002848 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002849 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002850}
2851EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2852
2853/* remove dynamic controls from the component driver */
2854int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2855{
2856 struct snd_soc_dobj *dobj, *next_dobj;
2857 int pass = SOC_TPLG_PASS_END;
2858
2859 /* process the header types from end to start */
2860 while (pass >= SOC_TPLG_PASS_START) {
2861
2862 /* remove mixer controls */
2863 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2864 list) {
2865
2866 /* match index */
2867 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002868 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002869 continue;
2870
2871 switch (dobj->type) {
2872 case SND_SOC_DOBJ_MIXER:
2873 remove_mixer(comp, dobj, pass);
2874 break;
2875 case SND_SOC_DOBJ_ENUM:
2876 remove_enum(comp, dobj, pass);
2877 break;
2878 case SND_SOC_DOBJ_BYTES:
2879 remove_bytes(comp, dobj, pass);
2880 break;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06002881 case SND_SOC_DOBJ_GRAPH:
2882 remove_route(comp, dobj, pass);
2883 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002884 case SND_SOC_DOBJ_WIDGET:
2885 remove_widget(comp, dobj, pass);
2886 break;
2887 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002888 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002889 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002890 case SND_SOC_DOBJ_DAI_LINK:
2891 remove_link(comp, dobj, pass);
2892 break;
Bard liaoadfebb52019-02-01 11:07:40 -06002893 case SND_SOC_DOBJ_BACKEND_LINK:
2894 /*
2895 * call link_unload ops if extra
2896 * deinitialization is needed.
2897 */
2898 remove_backend_link(comp, dobj, pass);
2899 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002900 default:
2901 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2902 dobj->type);
2903 break;
2904 }
2905 }
2906 pass--;
2907 }
2908
2909 /* let caller know if FW can be freed when no objects are left */
2910 return !list_empty(&comp->dobj_list);
2911}
2912EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);