blob: 03c4dbdfc58400a8d5933f293ffe4839af3a927f [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
33/*
34 * We make several passes over the data (since it wont necessarily be ordered)
35 * and process objects in the following order. This guarantees the component
36 * drivers will be ready with any vendor data before the mixers and DAPM objects
37 * are loaded (that may make use of the vendor data).
38 */
39#define SOC_TPLG_PASS_MANIFEST 0
40#define SOC_TPLG_PASS_VENDOR 1
41#define SOC_TPLG_PASS_MIXER 2
42#define SOC_TPLG_PASS_WIDGET 3
Mengdong Lin1a8e7fa2015-08-10 22:48:30 +080043#define SOC_TPLG_PASS_PCM_DAI 4
44#define SOC_TPLG_PASS_GRAPH 5
45#define SOC_TPLG_PASS_PINS 6
Mengdong Lin0038be92016-07-26 14:32:37 +080046#define SOC_TPLG_PASS_BE_DAI 7
Mengdong Lin593d9e52016-11-03 01:04:27 +080047#define SOC_TPLG_PASS_LINK 8
Liam Girdwood8a978232015-05-29 19:06:14 +010048
49#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
Mengdong Lin593d9e52016-11-03 01:04:27 +080050#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
Liam Girdwood8a978232015-05-29 19:06:14 +010051
Mengdong Lin583958f2016-10-11 14:36:42 +080052/* topology context */
Liam Girdwood8a978232015-05-29 19:06:14 +010053struct soc_tplg {
54 const struct firmware *fw;
55
56 /* runtime FW parsing */
57 const u8 *pos; /* read postion */
58 const u8 *hdr_pos; /* header position */
59 unsigned int pass; /* pass number */
60
61 /* component caller */
62 struct device *dev;
63 struct snd_soc_component *comp;
64 u32 index; /* current block index */
65 u32 req_index; /* required index, only loaded/free matching blocks */
66
Mengdong Lin88a17d82015-08-18 18:11:51 +080067 /* vendor specific kcontrol operations */
Liam Girdwood8a978232015-05-29 19:06:14 +010068 const struct snd_soc_tplg_kcontrol_ops *io_ops;
69 int io_ops_count;
70
Mengdong Lin1a3232d2015-08-18 18:12:20 +080071 /* vendor specific bytes ext handlers, for TLV bytes controls */
72 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
73 int bytes_ext_ops_count;
74
Liam Girdwood8a978232015-05-29 19:06:14 +010075 /* optional fw loading callbacks to component drivers */
76 struct snd_soc_tplg_ops *ops;
77};
78
79static int soc_tplg_process_headers(struct soc_tplg *tplg);
80static void soc_tplg_complete(struct soc_tplg *tplg);
81struct snd_soc_dapm_widget *
82snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
83 const struct snd_soc_dapm_widget *widget);
84struct snd_soc_dapm_widget *
85snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
86 const struct snd_soc_dapm_widget *widget);
87
88/* check we dont overflow the data for this control chunk */
89static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
90 unsigned int count, size_t bytes, const char *elem_type)
91{
92 const u8 *end = tplg->pos + elem_size * count;
93
94 if (end > tplg->fw->data + tplg->fw->size) {
95 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
96 elem_type);
97 return -EINVAL;
98 }
99
100 /* check there is enough room in chunk for control.
101 extra bytes at the end of control are for vendor data here */
102 if (elem_size * count > bytes) {
103 dev_err(tplg->dev,
104 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
105 elem_type, count, elem_size, bytes);
106 return -EINVAL;
107 }
108
109 return 0;
110}
111
112static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
113{
114 const u8 *end = tplg->hdr_pos;
115
116 if (end >= tplg->fw->data + tplg->fw->size)
117 return 1;
118 return 0;
119}
120
121static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
122{
123 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
124}
125
126static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
127{
128 return (unsigned long)(tplg->pos - tplg->fw->data);
129}
130
131/* mapping of Kcontrol types and associated operations. */
132static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
133 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
134 snd_soc_put_volsw, snd_soc_info_volsw},
135 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
136 snd_soc_put_volsw_sx, NULL},
137 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
138 snd_soc_put_enum_double, snd_soc_info_enum_double},
139 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
140 snd_soc_put_enum_double, NULL},
141 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
142 snd_soc_bytes_put, snd_soc_bytes_info},
143 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
144 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
145 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
146 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
147 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
148 snd_soc_put_strobe, NULL},
149 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530150 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100151 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
152 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
153 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
154 snd_soc_dapm_put_enum_double, NULL},
155 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
156 snd_soc_dapm_put_enum_double, NULL},
157 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
158 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
159};
160
161struct soc_tplg_map {
162 int uid;
163 int kid;
164};
165
166/* mapping of widget types from UAPI IDs to kernel IDs */
167static const struct soc_tplg_map dapm_map[] = {
168 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
169 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
170 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
171 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
172 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
173 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
174 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
175 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
176 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
177 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
178 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
179 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
180 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
181 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
182 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
183 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
Liam Girdwood8a70b452017-06-29 14:22:24 +0100184 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
185 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
186 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
187 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
188 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
189 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
190 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
191 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
Liam Girdwood8a978232015-05-29 19:06:14 +0100192};
193
194static int tplc_chan_get_reg(struct soc_tplg *tplg,
195 struct snd_soc_tplg_channel *chan, int map)
196{
197 int i;
198
199 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500200 if (le32_to_cpu(chan[i].id) == map)
201 return le32_to_cpu(chan[i].reg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100202 }
203
204 return -EINVAL;
205}
206
207static int tplc_chan_get_shift(struct soc_tplg *tplg,
208 struct snd_soc_tplg_channel *chan, int map)
209{
210 int i;
211
212 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500213 if (le32_to_cpu(chan[i].id) == map)
214 return le32_to_cpu(chan[i].shift);
Liam Girdwood8a978232015-05-29 19:06:14 +0100215 }
216
217 return -EINVAL;
218}
219
220static int get_widget_id(int tplg_type)
221{
222 int i;
223
224 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
225 if (tplg_type == dapm_map[i].uid)
226 return dapm_map[i].kid;
227 }
228
229 return -EINVAL;
230}
231
Liam Girdwood8a978232015-05-29 19:06:14 +0100232static inline void soc_bind_err(struct soc_tplg *tplg,
233 struct snd_soc_tplg_ctl_hdr *hdr, int index)
234{
235 dev_err(tplg->dev,
236 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
237 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
238 soc_tplg_get_offset(tplg));
239}
240
241static inline void soc_control_err(struct soc_tplg *tplg,
242 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
243{
244 dev_err(tplg->dev,
245 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
246 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
247 soc_tplg_get_offset(tplg));
248}
249
250/* pass vendor data to component driver for processing */
251static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
252 struct snd_soc_tplg_hdr *hdr)
253{
254 int ret = 0;
255
256 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100257 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100258 else {
259 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
260 hdr->vendor_type);
261 return -EINVAL;
262 }
263
264 if (ret < 0)
265 dev_err(tplg->dev,
266 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
267 soc_tplg_get_hdr_offset(tplg),
268 soc_tplg_get_hdr_offset(tplg),
269 hdr->type, hdr->vendor_type);
270 return ret;
271}
272
273/* pass vendor data to component driver for processing */
274static int soc_tplg_vendor_load(struct soc_tplg *tplg,
275 struct snd_soc_tplg_hdr *hdr)
276{
277 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
278 return 0;
279
280 return soc_tplg_vendor_load_(tplg, hdr);
281}
282
283/* 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_load(struct soc_tplg *tplg,
286 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
287{
288 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100289 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
290 tplg_w);
Liam Girdwood8a978232015-05-29 19:06:14 +0100291
292 return 0;
293}
294
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100295/* optionally pass new dynamic widget to component driver. This is mainly for
296 * external widgets where we can assign private data/ops */
297static int soc_tplg_widget_ready(struct soc_tplg *tplg,
298 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
299{
300 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100301 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
302 tplg_w);
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100303
304 return 0;
305}
306
Masahiro Yamada183b8022017-02-27 14:29:20 -0800307/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800308static int soc_tplg_dai_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100309 struct snd_soc_dai_driver *dai_drv,
310 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
Liam Girdwood8a978232015-05-29 19:06:14 +0100311{
Mengdong Lin64527e82016-01-15 16:13:28 +0800312 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100313 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
314 pcm, dai);
Liam Girdwood8a978232015-05-29 19:06:14 +0100315
316 return 0;
317}
318
Masahiro Yamada183b8022017-02-27 14:29:20 -0800319/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800320static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100321 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
Mengdong Linacfc7d42016-01-15 16:13:37 +0800322{
323 if (tplg->comp && tplg->ops && tplg->ops->link_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100324 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800325
326 return 0;
327}
328
Liam Girdwood8a978232015-05-29 19:06:14 +0100329/* tell the component driver that all firmware has been loaded in this request */
330static void soc_tplg_complete(struct soc_tplg *tplg)
331{
332 if (tplg->comp && tplg->ops && tplg->ops->complete)
333 tplg->ops->complete(tplg->comp);
334}
335
336/* add a dynamic kcontrol */
337static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
338 const struct snd_kcontrol_new *control_new, const char *prefix,
339 void *data, struct snd_kcontrol **kcontrol)
340{
341 int err;
342
343 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
344 if (*kcontrol == NULL) {
345 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
346 control_new->name);
347 return -ENOMEM;
348 }
349
350 err = snd_ctl_add(card, *kcontrol);
351 if (err < 0) {
352 dev_err(dev, "ASoC: Failed to add %s: %d\n",
353 control_new->name, err);
354 return err;
355 }
356
357 return 0;
358}
359
360/* add a dynamic kcontrol for component driver */
361static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
362 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
363{
364 struct snd_soc_component *comp = tplg->comp;
365
366 return soc_tplg_add_dcontrol(comp->card->snd_card,
367 comp->dev, k, NULL, comp, kcontrol);
368}
369
370/* remove a mixer kcontrol */
371static void remove_mixer(struct snd_soc_component *comp,
372 struct snd_soc_dobj *dobj, int pass)
373{
374 struct snd_card *card = comp->card->snd_card;
375 struct soc_mixer_control *sm =
376 container_of(dobj, struct soc_mixer_control, dobj);
377 const unsigned int *p = NULL;
378
379 if (pass != SOC_TPLG_PASS_MIXER)
380 return;
381
382 if (dobj->ops && dobj->ops->control_unload)
383 dobj->ops->control_unload(comp, dobj);
384
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600385 if (dobj->control.kcontrol->tlv.p)
386 p = dobj->control.kcontrol->tlv.p;
387 snd_ctl_remove(card, dobj->control.kcontrol);
388 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100389 kfree(sm);
390 kfree(p);
391}
392
393/* remove an enum kcontrol */
394static void remove_enum(struct snd_soc_component *comp,
395 struct snd_soc_dobj *dobj, int pass)
396{
397 struct snd_card *card = comp->card->snd_card;
398 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
399 int i;
400
401 if (pass != SOC_TPLG_PASS_MIXER)
402 return;
403
404 if (dobj->ops && dobj->ops->control_unload)
405 dobj->ops->control_unload(comp, dobj);
406
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600407 snd_ctl_remove(card, dobj->control.kcontrol);
408 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100409
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600410 kfree(dobj->control.dvalues);
Liam Girdwood8a978232015-05-29 19:06:14 +0100411 for (i = 0; i < se->items; i++)
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600412 kfree(dobj->control.dtexts[i]);
Amadeusz Sławiński34db6a32019-01-25 14:06:44 -0600413 kfree(dobj->control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +0100414 kfree(se);
415}
416
417/* remove a byte kcontrol */
418static void remove_bytes(struct snd_soc_component *comp,
419 struct snd_soc_dobj *dobj, int pass)
420{
421 struct snd_card *card = comp->card->snd_card;
422 struct soc_bytes_ext *sb =
423 container_of(dobj, struct soc_bytes_ext, dobj);
424
425 if (pass != SOC_TPLG_PASS_MIXER)
426 return;
427
428 if (dobj->ops && dobj->ops->control_unload)
429 dobj->ops->control_unload(comp, dobj);
430
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600431 snd_ctl_remove(card, dobj->control.kcontrol);
432 list_del(&dobj->list);
Liam Girdwood8a978232015-05-29 19:06:14 +0100433 kfree(sb);
434}
435
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -0600436/* remove a route */
437static void remove_route(struct snd_soc_component *comp,
438 struct snd_soc_dobj *dobj, int pass)
439{
440 struct snd_soc_dapm_route *route =
441 container_of(dobj, struct snd_soc_dapm_route, dobj);
442
443 if (pass != SOC_TPLG_PASS_GRAPH)
444 return;
445
446 if (dobj->ops && dobj->ops->dapm_route_unload)
447 dobj->ops->dapm_route_unload(comp, dobj);
448
449 list_del(&dobj->list);
450 kfree(route);
451}
452
Liam Girdwood8a978232015-05-29 19:06:14 +0100453/* remove a widget and it's kcontrols - routes must be removed first */
454static void remove_widget(struct snd_soc_component *comp,
455 struct snd_soc_dobj *dobj, int pass)
456{
457 struct snd_card *card = comp->card->snd_card;
458 struct snd_soc_dapm_widget *w =
459 container_of(dobj, struct snd_soc_dapm_widget, dobj);
460 int i;
461
462 if (pass != SOC_TPLG_PASS_WIDGET)
463 return;
464
465 if (dobj->ops && dobj->ops->widget_unload)
466 dobj->ops->widget_unload(comp, dobj);
467
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000468 if (!w->kcontrols)
469 goto free_news;
470
Liam Girdwood8a978232015-05-29 19:06:14 +0100471 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800472 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100473 * The enum may either have an array of values or strings.
474 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800475 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100476 /* enumerated widget mixer */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100477 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800478 struct snd_kcontrol *kcontrol = w->kcontrols[i];
479 struct soc_enum *se =
480 (struct soc_enum *)kcontrol->private_value;
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100481 int j;
Liam Girdwood8a978232015-05-29 19:06:14 +0100482
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800483 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100484
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600485 kfree(dobj->control.dvalues);
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100486 for (j = 0; j < se->items; j++)
Amadeusz Sławiński33ae6ae2019-01-25 14:06:42 -0600487 kfree(dobj->control.dtexts[j]);
Amadeusz Sławiński34db6a32019-01-25 14:06:44 -0600488 kfree(dobj->control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +0100489
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800490 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100491 kfree(w->kcontrol_news[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800492 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100493 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800494 /* volume mixer or bytes controls */
Liam Girdwoodf53c4c22018-03-27 14:30:44 +0100495 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100496 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100497
Mengdong Lineea3dd42016-11-25 16:09:17 +0800498 if (dobj->widget.kcontrol_type
499 == SND_SOC_TPLG_TYPE_MIXER)
500 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100501
Mengdong Lineea3dd42016-11-25 16:09:17 +0800502 /* Private value is used as struct soc_mixer_control
503 * for volume mixers or soc_bytes_ext for bytes
504 * controls.
505 */
506 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000507 snd_ctl_remove(card, kcontrol);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100508 kfree(w->kcontrol_news[i].name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100509 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100510 }
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000511
512free_news:
513 kfree(w->kcontrol_news);
514
Amadeusz Sławińskia46e8392019-01-25 14:06:43 -0600515 list_del(&dobj->list);
516
Liam Girdwood8a978232015-05-29 19:06:14 +0100517 /* widget w is freed by soc-dapm.c */
518}
519
Mengdong Lin64527e82016-01-15 16:13:28 +0800520/* remove DAI configurations */
521static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100522 struct snd_soc_dobj *dobj, int pass)
523{
Mengdong Lin64527e82016-01-15 16:13:28 +0800524 struct snd_soc_dai_driver *dai_drv =
525 container_of(dobj, struct snd_soc_dai_driver, dobj);
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600526 struct snd_soc_dai *dai;
Mengdong Lin64527e82016-01-15 16:13:28 +0800527
Liam Girdwood8a978232015-05-29 19:06:14 +0100528 if (pass != SOC_TPLG_PASS_PCM_DAI)
529 return;
530
Mengdong Lin64527e82016-01-15 16:13:28 +0800531 if (dobj->ops && dobj->ops->dai_unload)
532 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100533
Guennadi Liakhovetski52abe6c2019-02-01 11:05:13 -0600534 list_for_each_entry(dai, &comp->dai_list, list)
535 if (dai->driver == dai_drv)
536 dai->driver = NULL;
537
Bard liao7b6f68a2019-03-05 23:57:52 +0800538 kfree(dai_drv->playback.stream_name);
539 kfree(dai_drv->capture.stream_name);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800540 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100541 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800542 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100543}
544
Mengdong Linacfc7d42016-01-15 16:13:37 +0800545/* remove link configurations */
546static void remove_link(struct snd_soc_component *comp,
547 struct snd_soc_dobj *dobj, int pass)
548{
549 struct snd_soc_dai_link *link =
550 container_of(dobj, struct snd_soc_dai_link, dobj);
551
552 if (pass != SOC_TPLG_PASS_PCM_DAI)
553 return;
554
555 if (dobj->ops && dobj->ops->link_unload)
556 dobj->ops->link_unload(comp, dobj);
557
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800558 kfree(link->name);
559 kfree(link->stream_name);
560 kfree(link->cpu_dai_name);
561
Mengdong Linacfc7d42016-01-15 16:13:37 +0800562 list_del(&dobj->list);
563 snd_soc_remove_dai_link(comp->card, link);
564 kfree(link);
565}
566
Bard liaoadfebb52019-02-01 11:07:40 -0600567/* unload dai link */
568static void remove_backend_link(struct snd_soc_component *comp,
569 struct snd_soc_dobj *dobj, int pass)
570{
571 if (pass != SOC_TPLG_PASS_LINK)
572 return;
573
574 if (dobj->ops && dobj->ops->link_unload)
575 dobj->ops->link_unload(comp, dobj);
576
577 /*
578 * We don't free the link here as what remove_link() do since BE
579 * links are not allocated by topology.
580 * We however need to reset the dobj type to its initial values
581 */
582 dobj->type = SND_SOC_DOBJ_NONE;
583 list_del(&dobj->list);
584}
585
Liam Girdwood8a978232015-05-29 19:06:14 +0100586/* bind a kcontrol to it's IO handlers */
587static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
588 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800589 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100590{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800591 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800592 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800593 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100594
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500595 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800596 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
597 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
598 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
599 struct soc_bytes_ext *sbe;
600 struct snd_soc_tplg_bytes_control *be;
601
602 sbe = (struct soc_bytes_ext *)k->private_value;
603 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
604
605 /* TLV bytes controls need standard kcontrol info handler,
606 * TLV callback and extended put/get handlers.
607 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530608 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800609 k->tlv.c = snd_soc_bytes_tlv_callback;
610
611 ext_ops = tplg->bytes_ext_ops;
612 num_ops = tplg->bytes_ext_ops_count;
613 for (i = 0; i < num_ops; i++) {
614 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
615 sbe->put = ext_ops[i].put;
616 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
617 sbe->get = ext_ops[i].get;
618 }
619
620 if (sbe->put && sbe->get)
621 return 0;
622 else
623 return -EINVAL;
624 }
625
Mengdong Lin88a17d82015-08-18 18:11:51 +0800626 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800627 ops = tplg->io_ops;
628 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100629 for (i = 0; i < num_ops; i++) {
630
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800631 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100632 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800633 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100634 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800635 if (k->info == NULL && ops[i].id == hdr->ops.info)
636 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100637 }
638
Mengdong Lin88a17d82015-08-18 18:11:51 +0800639 /* vendor specific handlers found ? */
640 if (k->put && k->get && k->info)
641 return 0;
642
643 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800644 ops = io_ops;
645 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800646 for (i = 0; i < num_ops; i++) {
647
648 if (k->put == NULL && ops[i].id == hdr->ops.put)
649 k->put = ops[i].put;
650 if (k->get == NULL && ops[i].id == hdr->ops.get)
651 k->get = ops[i].get;
652 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100653 k->info = ops[i].info;
654 }
655
656 /* standard handlers found ? */
657 if (k->put && k->get && k->info)
658 return 0;
659
Liam Girdwood8a978232015-05-29 19:06:14 +0100660 /* nothing to bind */
661 return -EINVAL;
662}
663
664/* bind a widgets to it's evnt handlers */
665int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
666 const struct snd_soc_tplg_widget_events *events,
667 int num_events, u16 event_type)
668{
669 int i;
670
671 w->event = NULL;
672
673 for (i = 0; i < num_events; i++) {
674 if (event_type == events[i].type) {
675
676 /* found - so assign event */
677 w->event = events[i].event_handler;
678 return 0;
679 }
680 }
681
682 /* not found */
683 return -EINVAL;
684}
685EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
686
687/* optionally pass new dynamic kcontrol to component driver. */
688static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
689 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
690{
691 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100692 return tplg->ops->control_load(tplg->comp, tplg->index, k,
693 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100694
695 return 0;
696}
697
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100698
699static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
700 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100701{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100702 unsigned int item_len = 2 * sizeof(unsigned int);
703 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100704
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100705 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
706 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100707 return -ENOMEM;
708
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100709 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
710 p[1] = item_len;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500711 p[2] = le32_to_cpu(scale->min);
712 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
713 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100714
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100715 kc->tlv.p = (void *)p;
716 return 0;
717}
718
719static int soc_tplg_create_tlv(struct soc_tplg *tplg,
720 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
721{
722 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500723 u32 access = le32_to_cpu(tc->access);
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100724
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500725 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100726 return 0;
727
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500728 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100729 tplg_tlv = &tc->tlv;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500730 switch (le32_to_cpu(tplg_tlv->type)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100731 case SNDRV_CTL_TLVT_DB_SCALE:
732 return soc_tplg_create_tlv_db_scale(tplg, kc,
733 &tplg_tlv->scale);
734
735 /* TODO: add support for other TLV types */
736 default:
737 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
738 tplg_tlv->type);
739 return -EINVAL;
740 }
741 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100742
743 return 0;
744}
745
746static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
747 struct snd_kcontrol_new *kc)
748{
749 kfree(kc->tlv.p);
750}
751
752static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
753 size_t size)
754{
755 struct snd_soc_tplg_bytes_control *be;
756 struct soc_bytes_ext *sbe;
757 struct snd_kcontrol_new kc;
758 int i, err;
759
760 if (soc_tplg_check_elem_count(tplg,
761 sizeof(struct snd_soc_tplg_bytes_control), count,
762 size, "mixer bytes")) {
763 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
764 count);
765 return -EINVAL;
766 }
767
768 for (i = 0; i < count; i++) {
769 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
770
771 /* validate kcontrol */
772 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
773 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
774 return -EINVAL;
775
776 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
777 if (sbe == NULL)
778 return -ENOMEM;
779
780 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500781 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100782
783 dev_dbg(tplg->dev,
784 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
785 be->hdr.name, be->hdr.access);
786
787 memset(&kc, 0, sizeof(kc));
788 kc.name = be->hdr.name;
789 kc.private_value = (long)sbe;
790 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500791 kc.access = le32_to_cpu(be->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100792
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500793 sbe->max = le32_to_cpu(be->max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100794 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
795 sbe->dobj.ops = tplg->ops;
796 INIT_LIST_HEAD(&sbe->dobj.list);
797
798 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800799 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100800 if (err) {
801 soc_control_err(tplg, &be->hdr, be->hdr.name);
802 kfree(sbe);
803 continue;
804 }
805
806 /* pass control to driver for optional further init */
807 err = soc_tplg_init_kcontrol(tplg, &kc,
808 (struct snd_soc_tplg_ctl_hdr *)be);
809 if (err < 0) {
810 dev_err(tplg->dev, "ASoC: failed to init %s\n",
811 be->hdr.name);
812 kfree(sbe);
813 continue;
814 }
815
816 /* register control here */
817 err = soc_tplg_add_kcontrol(tplg, &kc,
818 &sbe->dobj.control.kcontrol);
819 if (err < 0) {
820 dev_err(tplg->dev, "ASoC: failed to add %s\n",
821 be->hdr.name);
822 kfree(sbe);
823 continue;
824 }
825
826 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
827 }
828 return 0;
829
830}
831
832static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
833 size_t size)
834{
835 struct snd_soc_tplg_mixer_control *mc;
836 struct soc_mixer_control *sm;
837 struct snd_kcontrol_new kc;
838 int i, err;
839
840 if (soc_tplg_check_elem_count(tplg,
841 sizeof(struct snd_soc_tplg_mixer_control),
842 count, size, "mixers")) {
843
844 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
845 count);
846 return -EINVAL;
847 }
848
849 for (i = 0; i < count; i++) {
850 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
851
852 /* validate kcontrol */
853 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
854 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
855 return -EINVAL;
856
857 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
858 if (sm == NULL)
859 return -ENOMEM;
860 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500861 le32_to_cpu(mc->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +0100862
863 dev_dbg(tplg->dev,
864 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
865 mc->hdr.name, mc->hdr.access);
866
867 memset(&kc, 0, sizeof(kc));
868 kc.name = mc->hdr.name;
869 kc.private_value = (long)sm;
870 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500871 kc.access = le32_to_cpu(mc->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +0100872
873 /* we only support FL/FR channel mapping atm */
874 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
875 SNDRV_CHMAP_FL);
876 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
877 SNDRV_CHMAP_FR);
878 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
879 SNDRV_CHMAP_FL);
880 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
881 SNDRV_CHMAP_FR);
882
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500883 sm->max = le32_to_cpu(mc->max);
884 sm->min = le32_to_cpu(mc->min);
885 sm->invert = le32_to_cpu(mc->invert);
886 sm->platform_max = le32_to_cpu(mc->platform_max);
Liam Girdwood8a978232015-05-29 19:06:14 +0100887 sm->dobj.index = tplg->index;
888 sm->dobj.ops = tplg->ops;
889 sm->dobj.type = SND_SOC_DOBJ_MIXER;
890 INIT_LIST_HEAD(&sm->dobj.list);
891
892 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800893 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100894 if (err) {
895 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
896 kfree(sm);
897 continue;
898 }
899
Bard liao3789deb2019-03-13 21:49:43 +0800900 /* create any TLV data */
901 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
902
Liam Girdwood8a978232015-05-29 19:06:14 +0100903 /* pass control to driver for optional further init */
904 err = soc_tplg_init_kcontrol(tplg, &kc,
905 (struct snd_soc_tplg_ctl_hdr *) mc);
906 if (err < 0) {
907 dev_err(tplg->dev, "ASoC: failed to init %s\n",
908 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +0800909 soc_tplg_free_tlv(tplg, &kc);
Liam Girdwood8a978232015-05-29 19:06:14 +0100910 kfree(sm);
911 continue;
912 }
913
Liam Girdwood8a978232015-05-29 19:06:14 +0100914 /* register control here */
915 err = soc_tplg_add_kcontrol(tplg, &kc,
916 &sm->dobj.control.kcontrol);
917 if (err < 0) {
918 dev_err(tplg->dev, "ASoC: failed to add %s\n",
919 mc->hdr.name);
920 soc_tplg_free_tlv(tplg, &kc);
921 kfree(sm);
922 continue;
923 }
924
925 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
926 }
927
928 return 0;
929}
930
931static int soc_tplg_denum_create_texts(struct soc_enum *se,
932 struct snd_soc_tplg_enum_control *ec)
933{
934 int i, ret;
935
936 se->dobj.control.dtexts =
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500937 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100938 if (se->dobj.control.dtexts == NULL)
939 return -ENOMEM;
940
941 for (i = 0; i < ec->items; i++) {
942
943 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
944 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
945 ret = -EINVAL;
946 goto err;
947 }
948
949 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
950 if (!se->dobj.control.dtexts[i]) {
951 ret = -ENOMEM;
952 goto err;
953 }
954 }
955
Mousumi Janab6e38b22017-04-11 13:06:22 +0530956 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100957 return 0;
958
959err:
960 for (--i; i >= 0; i--)
961 kfree(se->dobj.control.dtexts[i]);
962 kfree(se->dobj.control.dtexts);
963 return ret;
964}
965
966static int soc_tplg_denum_create_values(struct soc_enum *se,
967 struct snd_soc_tplg_enum_control *ec)
968{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500969 int i;
970
971 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
Liam Girdwood8a978232015-05-29 19:06:14 +0100972 return -EINVAL;
973
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500974 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
975 sizeof(u32),
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200976 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100977 if (!se->dobj.control.dvalues)
978 return -ENOMEM;
979
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -0500980 /* convert from little-endian */
981 for (i = 0; i < le32_to_cpu(ec->items); i++) {
982 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
983 }
984
Liam Girdwood8a978232015-05-29 19:06:14 +0100985 return 0;
986}
987
988static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
989 size_t size)
990{
991 struct snd_soc_tplg_enum_control *ec;
992 struct soc_enum *se;
993 struct snd_kcontrol_new kc;
994 int i, ret, err;
995
996 if (soc_tplg_check_elem_count(tplg,
997 sizeof(struct snd_soc_tplg_enum_control),
998 count, size, "enums")) {
999
1000 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1001 count);
1002 return -EINVAL;
1003 }
1004
1005 for (i = 0; i < count; i++) {
1006 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
Liam Girdwood8a978232015-05-29 19:06:14 +01001007
1008 /* validate kcontrol */
1009 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1010 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1011 return -EINVAL;
1012
1013 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1014 if (se == NULL)
1015 return -ENOMEM;
1016
Liam Girdwood02b64242019-03-01 19:08:52 -06001017 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001018 le32_to_cpu(ec->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001019
Liam Girdwood8a978232015-05-29 19:06:14 +01001020 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1021 ec->hdr.name, ec->items);
1022
1023 memset(&kc, 0, sizeof(kc));
1024 kc.name = ec->hdr.name;
1025 kc.private_value = (long)se;
1026 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001027 kc.access = le32_to_cpu(ec->hdr.access);
Liam Girdwood8a978232015-05-29 19:06:14 +01001028
1029 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1030 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1031 SNDRV_CHMAP_FL);
1032 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1033 SNDRV_CHMAP_FL);
1034
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001035 se->items = le32_to_cpu(ec->items);
1036 se->mask = le32_to_cpu(ec->mask);
Liam Girdwood8a978232015-05-29 19:06:14 +01001037 se->dobj.index = tplg->index;
1038 se->dobj.type = SND_SOC_DOBJ_ENUM;
1039 se->dobj.ops = tplg->ops;
1040 INIT_LIST_HEAD(&se->dobj.list);
1041
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001042 switch (le32_to_cpu(ec->hdr.ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001043 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1044 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1045 err = soc_tplg_denum_create_values(se, ec);
1046 if (err < 0) {
1047 dev_err(tplg->dev,
1048 "ASoC: could not create values for %s\n",
1049 ec->hdr.name);
1050 kfree(se);
1051 continue;
1052 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001053 /* fall through */
Liam Girdwood8a978232015-05-29 19:06:14 +01001054 case SND_SOC_TPLG_CTL_ENUM:
1055 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1056 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1057 err = soc_tplg_denum_create_texts(se, ec);
1058 if (err < 0) {
1059 dev_err(tplg->dev,
1060 "ASoC: could not create texts for %s\n",
1061 ec->hdr.name);
1062 kfree(se);
1063 continue;
1064 }
1065 break;
1066 default:
1067 dev_err(tplg->dev,
1068 "ASoC: invalid enum control type %d for %s\n",
1069 ec->hdr.ops.info, ec->hdr.name);
1070 kfree(se);
1071 continue;
1072 }
1073
1074 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001075 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001076 if (err) {
1077 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1078 kfree(se);
1079 continue;
1080 }
1081
1082 /* pass control to driver for optional further init */
1083 err = soc_tplg_init_kcontrol(tplg, &kc,
1084 (struct snd_soc_tplg_ctl_hdr *) ec);
1085 if (err < 0) {
1086 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1087 ec->hdr.name);
1088 kfree(se);
1089 continue;
1090 }
1091
1092 /* register control here */
1093 ret = soc_tplg_add_kcontrol(tplg,
1094 &kc, &se->dobj.control.kcontrol);
1095 if (ret < 0) {
1096 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1097 ec->hdr.name);
1098 kfree(se);
1099 continue;
1100 }
1101
1102 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1103 }
1104
1105 return 0;
1106}
1107
1108static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1109 struct snd_soc_tplg_hdr *hdr)
1110{
1111 struct snd_soc_tplg_ctl_hdr *control_hdr;
1112 int i;
1113
1114 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001115 tplg->pos += le32_to_cpu(hdr->size) +
1116 le32_to_cpu(hdr->payload_size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001117 return 0;
1118 }
1119
1120 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:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001140 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:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001148 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:
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001152 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 }
1159 }
1160
1161 return 0;
1162}
1163
Liam Girdwood503e79b2018-06-14 20:53:59 +01001164/* optionally pass new dynamic kcontrol to component driver. */
1165static int soc_tplg_add_route(struct soc_tplg *tplg,
1166 struct snd_soc_dapm_route *route)
1167{
1168 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1169 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1170 route);
1171
1172 return 0;
1173}
1174
Liam Girdwood8a978232015-05-29 19:06:14 +01001175static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1176 struct snd_soc_tplg_hdr *hdr)
1177{
1178 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001179 struct snd_soc_tplg_dapm_graph_elem *elem;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001180 struct snd_soc_dapm_route **routes;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001181 int count, i, j;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001182 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001183
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001184 count = le32_to_cpu(hdr->count);
1185
Liam Girdwood8a978232015-05-29 19:06:14 +01001186 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001187 tplg->pos +=
1188 le32_to_cpu(hdr->size) +
1189 le32_to_cpu(hdr->payload_size);
1190
Liam Girdwood8a978232015-05-29 19:06:14 +01001191 return 0;
1192 }
1193
1194 if (soc_tplg_check_elem_count(tplg,
1195 sizeof(struct snd_soc_tplg_dapm_graph_elem),
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001196 count, le32_to_cpu(hdr->payload_size), "graph")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001197
1198 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1199 count);
1200 return -EINVAL;
1201 }
1202
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001203 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1204 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001205
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001206 /* allocate memory for pointer to array of dapm routes */
1207 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1208 GFP_KERNEL);
1209 if (!routes)
1210 return -ENOMEM;
1211
1212 /*
1213 * allocate memory for each dapm route in the array.
1214 * This needs to be done individually so that
1215 * each route can be freed when it is removed in remove_route().
1216 */
1217 for (i = 0; i < count; i++) {
1218 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1219 if (!routes[i]) {
1220 /* free previously allocated memory */
1221 for (j = 0; j < i; j++)
1222 kfree(routes[j]);
1223
1224 kfree(routes);
1225 return -ENOMEM;
1226 }
1227 }
1228
Liam Girdwood8a978232015-05-29 19:06:14 +01001229 for (i = 0; i < count; i++) {
1230 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1231 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1232
1233 /* validate routes */
1234 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001235 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1236 ret = -EINVAL;
1237 break;
1238 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001239 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001240 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1241 ret = -EINVAL;
1242 break;
1243 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001244 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001245 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1246 ret = -EINVAL;
1247 break;
1248 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001249
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001250 routes[i]->source = elem->source;
1251 routes[i]->sink = elem->sink;
1252
1253 /* set to NULL atm for tplg users */
1254 routes[i]->connected = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001255 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001256 routes[i]->control = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001257 else
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001258 routes[i]->control = elem->control;
Liam Girdwood8a978232015-05-29 19:06:14 +01001259
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001260 /* add route dobj to dobj_list */
1261 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1262 routes[i]->dobj.ops = tplg->ops;
1263 routes[i]->dobj.index = tplg->index;
1264 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1265
1266 soc_tplg_add_route(tplg, routes[i]);
Liam Girdwood503e79b2018-06-14 20:53:59 +01001267
Liam Girdwood8a978232015-05-29 19:06:14 +01001268 /* add route, but keep going if some fail */
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001269 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Liam Girdwood8a978232015-05-29 19:06:14 +01001270 }
1271
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001272 /* free memory allocated for all dapm routes in case of error */
1273 if (ret < 0)
1274 for (i = 0; i < count ; i++)
1275 kfree(routes[i]);
1276
1277 /*
1278 * free pointer to array of dapm routes as this is no longer needed.
1279 * The memory allocated for each dapm route will be freed
1280 * when it is removed in remove_route().
1281 */
1282 kfree(routes);
1283
1284 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001285}
1286
1287static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1288 struct soc_tplg *tplg, int num_kcontrols)
1289{
1290 struct snd_kcontrol_new *kc;
1291 struct soc_mixer_control *sm;
1292 struct snd_soc_tplg_mixer_control *mc;
1293 int i, err;
1294
Axel Lin4ca7deb2015-08-05 22:34:22 +08001295 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001296 if (kc == NULL)
1297 return NULL;
1298
1299 for (i = 0; i < num_kcontrols; i++) {
1300 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1301 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1302 if (sm == NULL)
1303 goto err;
1304
Liam Girdwood8a978232015-05-29 19:06:14 +01001305 /* validate kcontrol */
1306 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1307 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1308 goto err_str;
1309
Liam Girdwood02b64242019-03-01 19:08:52 -06001310 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001311 le32_to_cpu(mc->priv.size));
Liam Girdwood02b64242019-03-01 19:08:52 -06001312
Liam Girdwood8a978232015-05-29 19:06:14 +01001313 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1314 mc->hdr.name, i);
1315
Liam Girdwood267e2c62018-03-27 12:04:04 +01001316 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1317 if (kc[i].name == NULL)
1318 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001319 kc[i].private_value = (long)sm;
1320 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1321 kc[i].access = mc->hdr.access;
1322
1323 /* we only support FL/FR channel mapping atm */
1324 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1325 SNDRV_CHMAP_FL);
1326 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1327 SNDRV_CHMAP_FR);
1328 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1329 SNDRV_CHMAP_FL);
1330 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1331 SNDRV_CHMAP_FR);
1332
1333 sm->max = mc->max;
1334 sm->min = mc->min;
1335 sm->invert = mc->invert;
1336 sm->platform_max = mc->platform_max;
1337 sm->dobj.index = tplg->index;
1338 INIT_LIST_HEAD(&sm->dobj.list);
1339
1340 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001341 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001342 if (err) {
1343 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1344 kfree(sm);
1345 continue;
1346 }
1347
Bard liao3789deb2019-03-13 21:49:43 +08001348 /* create any TLV data */
1349 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1350
Liam Girdwood8a978232015-05-29 19:06:14 +01001351 /* pass control to driver for optional further init */
1352 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1353 (struct snd_soc_tplg_ctl_hdr *)mc);
1354 if (err < 0) {
1355 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1356 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +08001357 soc_tplg_free_tlv(tplg, &kc[i]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001358 kfree(sm);
1359 continue;
1360 }
1361 }
1362 return kc;
1363
1364err_str:
1365 kfree(sm);
1366err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001367 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001368 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001369 kfree(kc[i].name);
1370 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001371 kfree(kc);
1372 return NULL;
1373}
1374
1375static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001376 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001377{
1378 struct snd_kcontrol_new *kc;
1379 struct snd_soc_tplg_enum_control *ec;
1380 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001381 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001382
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001383 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001384 if (kc == NULL)
1385 return NULL;
1386
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001387 for (i = 0; i < num_kcontrols; i++) {
1388 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1389 /* validate kcontrol */
1390 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1391 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001392 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001393
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001394 se = kzalloc(sizeof(*se), GFP_KERNEL);
1395 if (se == NULL)
1396 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001397
Liam Girdwood02b64242019-03-01 19:08:52 -06001398 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1399 ec->priv.size);
1400
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001401 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001402 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001403
Liam Girdwood267e2c62018-03-27 12:04:04 +01001404 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001405 if (kc[i].name == NULL) {
1406 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001407 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001408 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001409 kc[i].private_value = (long)se;
1410 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1411 kc[i].access = ec->hdr.access;
1412
1413 /* we only support FL/FR channel mapping atm */
1414 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1415 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1416 SNDRV_CHMAP_FL);
1417 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1418 SNDRV_CHMAP_FR);
1419
1420 se->items = ec->items;
1421 se->mask = ec->mask;
1422 se->dobj.index = tplg->index;
1423
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001424 switch (le32_to_cpu(ec->hdr.ops.info)) {
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001425 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1426 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1427 err = soc_tplg_denum_create_values(se, ec);
1428 if (err < 0) {
1429 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1430 ec->hdr.name);
1431 goto err_se;
1432 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001433 /* fall through */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001434 case SND_SOC_TPLG_CTL_ENUM:
1435 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1436 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1437 err = soc_tplg_denum_create_texts(se, ec);
1438 if (err < 0) {
1439 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1440 ec->hdr.name);
1441 goto err_se;
1442 }
1443 break;
1444 default:
1445 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1446 ec->hdr.ops.info, ec->hdr.name);
1447 goto err_se;
1448 }
1449
1450 /* map io handlers */
1451 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1452 if (err) {
1453 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1454 goto err_se;
1455 }
1456
1457 /* pass control to driver for optional further init */
1458 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1459 (struct snd_soc_tplg_ctl_hdr *)ec);
1460 if (err < 0) {
1461 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1462 ec->hdr.name);
1463 goto err_se;
1464 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001465 }
1466
1467 return kc;
1468
1469err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001470 for (; i >= 0; i--) {
1471 /* free values and texts */
1472 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001473 if (!se)
1474 continue;
1475
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001476 kfree(se->dobj.control.dvalues);
1477 for (j = 0; j < ec->items; j++)
1478 kfree(se->dobj.control.dtexts[j]);
Amadeusz Sławiński34db6a32019-01-25 14:06:44 -06001479 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +01001480
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001481 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001482 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001483 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001484err:
1485 kfree(kc);
1486
1487 return NULL;
1488}
1489
1490static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1491 struct soc_tplg *tplg, int count)
1492{
1493 struct snd_soc_tplg_bytes_control *be;
1494 struct soc_bytes_ext *sbe;
1495 struct snd_kcontrol_new *kc;
1496 int i, err;
1497
Axel Lin4ca7deb2015-08-05 22:34:22 +08001498 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001499 if (!kc)
1500 return NULL;
1501
1502 for (i = 0; i < count; i++) {
1503 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1504
1505 /* validate kcontrol */
1506 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1507 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1508 goto err;
1509
1510 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1511 if (sbe == NULL)
1512 goto err;
1513
1514 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001515 le32_to_cpu(be->priv.size));
Liam Girdwood8a978232015-05-29 19:06:14 +01001516
1517 dev_dbg(tplg->dev,
1518 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1519 be->hdr.name, be->hdr.access);
1520
Liam Girdwood267e2c62018-03-27 12:04:04 +01001521 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001522 if (kc[i].name == NULL) {
1523 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001524 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001525 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001526 kc[i].private_value = (long)sbe;
1527 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1528 kc[i].access = be->hdr.access;
1529
1530 sbe->max = be->max;
1531 INIT_LIST_HEAD(&sbe->dobj.list);
1532
1533 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001534 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001535 if (err) {
1536 soc_control_err(tplg, &be->hdr, be->hdr.name);
1537 kfree(sbe);
1538 continue;
1539 }
1540
1541 /* pass control to driver for optional further init */
1542 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1543 (struct snd_soc_tplg_ctl_hdr *)be);
1544 if (err < 0) {
1545 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1546 be->hdr.name);
1547 kfree(sbe);
1548 continue;
1549 }
1550 }
1551
1552 return kc;
1553
1554err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001555 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001556 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001557 kfree(kc[i].name);
1558 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001559
1560 kfree(kc);
1561 return NULL;
1562}
1563
1564static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1565 struct snd_soc_tplg_dapm_widget *w)
1566{
1567 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1568 struct snd_soc_dapm_widget template, *widget;
1569 struct snd_soc_tplg_ctl_hdr *control_hdr;
1570 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001571 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001572 int ret = 0;
1573
1574 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1575 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1576 return -EINVAL;
1577 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1578 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1579 return -EINVAL;
1580
1581 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1582 w->name, w->id);
1583
1584 memset(&template, 0, sizeof(template));
1585
1586 /* map user to kernel widget ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001587 template.id = get_widget_id(le32_to_cpu(w->id));
Liam Girdwood8a978232015-05-29 19:06:14 +01001588 if (template.id < 0)
1589 return template.id;
1590
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001591 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001592 template.name = kstrdup(w->name, GFP_KERNEL);
1593 if (!template.name)
1594 return -ENOMEM;
1595 template.sname = kstrdup(w->sname, GFP_KERNEL);
1596 if (!template.sname) {
1597 ret = -ENOMEM;
1598 goto err;
1599 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001600 template.reg = le32_to_cpu(w->reg);
1601 template.shift = le32_to_cpu(w->shift);
1602 template.mask = le32_to_cpu(w->mask);
1603 template.subseq = le32_to_cpu(w->subseq);
Liam Girdwood8a978232015-05-29 19:06:14 +01001604 template.on_val = w->invert ? 0 : 1;
1605 template.off_val = w->invert ? 1 : 0;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001606 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1607 template.event_flags = le16_to_cpu(w->event_flags);
Liam Girdwood8a978232015-05-29 19:06:14 +01001608 template.dobj.index = tplg->index;
1609
1610 tplg->pos +=
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001611 (sizeof(struct snd_soc_tplg_dapm_widget) +
1612 le32_to_cpu(w->priv.size));
1613
Liam Girdwood8a978232015-05-29 19:06:14 +01001614 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001615 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001616 template.num_kcontrols = 0;
1617 goto widget;
1618 }
1619
1620 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1621 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1622 w->name, w->num_kcontrols, control_hdr->type);
1623
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001624 switch (le32_to_cpu(control_hdr->ops.info)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001625 case SND_SOC_TPLG_CTL_VOLSW:
1626 case SND_SOC_TPLG_CTL_STROBE:
1627 case SND_SOC_TPLG_CTL_VOLSW_SX:
1628 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1629 case SND_SOC_TPLG_CTL_RANGE:
1630 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001631 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001632 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001633 template.kcontrol_news =
1634 soc_tplg_dapm_widget_dmixer_create(tplg,
1635 template.num_kcontrols);
1636 if (!template.kcontrol_news) {
1637 ret = -ENOMEM;
1638 goto hdr_err;
1639 }
1640 break;
1641 case SND_SOC_TPLG_CTL_ENUM:
1642 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1643 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1644 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1645 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001646 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001647 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001648 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001649 soc_tplg_dapm_widget_denum_create(tplg,
1650 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001651 if (!template.kcontrol_news) {
1652 ret = -ENOMEM;
1653 goto hdr_err;
1654 }
1655 break;
1656 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001657 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001658 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001659 template.kcontrol_news =
1660 soc_tplg_dapm_widget_dbytes_create(tplg,
1661 template.num_kcontrols);
1662 if (!template.kcontrol_news) {
1663 ret = -ENOMEM;
1664 goto hdr_err;
1665 }
1666 break;
1667 default:
1668 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1669 control_hdr->ops.get, control_hdr->ops.put,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001670 le32_to_cpu(control_hdr->ops.info));
Liam Girdwood8a978232015-05-29 19:06:14 +01001671 ret = -EINVAL;
1672 goto hdr_err;
1673 }
1674
1675widget:
1676 ret = soc_tplg_widget_load(tplg, &template, w);
1677 if (ret < 0)
1678 goto hdr_err;
1679
1680 /* card dapm mutex is held by the core if we are loading topology
1681 * data during sound card init. */
1682 if (card->instantiated)
1683 widget = snd_soc_dapm_new_control(dapm, &template);
1684 else
1685 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001686 if (IS_ERR(widget)) {
1687 ret = PTR_ERR(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001688 goto hdr_err;
1689 }
1690
1691 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001692 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001693 widget->dobj.ops = tplg->ops;
1694 widget->dobj.index = tplg->index;
1695 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001696
1697 ret = soc_tplg_widget_ready(tplg, widget, w);
1698 if (ret < 0)
1699 goto ready_err;
1700
Bard liao7620fe92019-01-25 14:06:45 -06001701 kfree(template.sname);
1702 kfree(template.name);
1703
Liam Girdwood8a978232015-05-29 19:06:14 +01001704 return 0;
1705
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001706ready_err:
1707 snd_soc_tplg_widget_remove(widget);
1708 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001709hdr_err:
1710 kfree(template.sname);
1711err:
1712 kfree(template.name);
1713 return ret;
1714}
1715
1716static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1717 struct snd_soc_tplg_hdr *hdr)
1718{
1719 struct snd_soc_tplg_dapm_widget *widget;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001720 int ret, count, i;
1721
1722 count = le32_to_cpu(hdr->count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001723
1724 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1725 return 0;
1726
1727 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1728
1729 for (i = 0; i < count; i++) {
1730 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001731 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001732 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1733 return -EINVAL;
1734 }
1735
Liam Girdwood8a978232015-05-29 19:06:14 +01001736 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001737 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001738 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1739 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001740 return ret;
1741 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001742 }
1743
1744 return 0;
1745}
1746
1747static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1748{
1749 struct snd_soc_card *card = tplg->comp->card;
1750 int ret;
1751
1752 /* Card might not have been registered at this point.
1753 * If so, just return success.
1754 */
1755 if (!card || !card->instantiated) {
1756 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001757 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001758 return 0;
1759 }
1760
1761 ret = snd_soc_dapm_new_widgets(card);
1762 if (ret < 0)
1763 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1764 ret);
1765
1766 return 0;
1767}
1768
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001769static void set_stream_info(struct snd_soc_pcm_stream *stream,
1770 struct snd_soc_tplg_stream_caps *caps)
1771{
1772 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001773 stream->channels_min = le32_to_cpu(caps->channels_min);
1774 stream->channels_max = le32_to_cpu(caps->channels_max);
1775 stream->rates = le32_to_cpu(caps->rates);
1776 stream->rate_min = le32_to_cpu(caps->rate_min);
1777 stream->rate_max = le32_to_cpu(caps->rate_max);
1778 stream->formats = le64_to_cpu(caps->formats);
1779 stream->sig_bits = le32_to_cpu(caps->sig_bits);
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001780}
1781
Mengdong Lin0038be92016-07-26 14:32:37 +08001782static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1783 unsigned int flag_mask, unsigned int flags)
1784{
1785 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1786 dai_drv->symmetric_rates =
1787 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1788
1789 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1790 dai_drv->symmetric_channels =
1791 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1792 1 : 0;
1793
1794 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1795 dai_drv->symmetric_samplebits =
1796 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1797 1 : 0;
1798}
1799
Mengdong Lin64527e82016-01-15 16:13:28 +08001800static int soc_tplg_dai_create(struct soc_tplg *tplg,
1801 struct snd_soc_tplg_pcm *pcm)
1802{
1803 struct snd_soc_dai_driver *dai_drv;
1804 struct snd_soc_pcm_stream *stream;
1805 struct snd_soc_tplg_stream_caps *caps;
1806 int ret;
1807
1808 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1809 if (dai_drv == NULL)
1810 return -ENOMEM;
1811
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001812 if (strlen(pcm->dai_name))
1813 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001814 dai_drv->id = le32_to_cpu(pcm->dai_id);
Mengdong Lin64527e82016-01-15 16:13:28 +08001815
1816 if (pcm->playback) {
1817 stream = &dai_drv->playback;
1818 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001819 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001820 }
1821
1822 if (pcm->capture) {
1823 stream = &dai_drv->capture;
1824 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001825 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001826 }
1827
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001828 if (pcm->compress)
1829 dai_drv->compress_new = snd_soc_new_compress;
1830
Mengdong Lin64527e82016-01-15 16:13:28 +08001831 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001832 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001833 if (ret < 0) {
1834 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Bard liao7b6f68a2019-03-05 23:57:52 +08001835 kfree(dai_drv->playback.stream_name);
1836 kfree(dai_drv->capture.stream_name);
1837 kfree(dai_drv->name);
Mengdong Lin64527e82016-01-15 16:13:28 +08001838 kfree(dai_drv);
1839 return ret;
1840 }
1841
1842 dai_drv->dobj.index = tplg->index;
1843 dai_drv->dobj.ops = tplg->ops;
1844 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1845 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1846
1847 /* register the DAI to the component */
1848 return snd_soc_register_dai(tplg->comp, dai_drv);
1849}
1850
Mengdong Lin717a8e72016-11-03 01:03:34 +08001851static void set_link_flags(struct snd_soc_dai_link *link,
1852 unsigned int flag_mask, unsigned int flags)
1853{
1854 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1855 link->symmetric_rates =
1856 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1857
1858 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1859 link->symmetric_channels =
1860 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1861 1 : 0;
1862
1863 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1864 link->symmetric_samplebits =
1865 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1866 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001867
1868 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1869 link->ignore_suspend =
1870 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1871 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001872}
1873
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001874/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001875static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001876 struct snd_soc_tplg_pcm *pcm)
1877{
1878 struct snd_soc_dai_link *link;
1879 int ret;
1880
1881 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1882 if (link == NULL)
1883 return -ENOMEM;
1884
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001885 if (strlen(pcm->pcm_name)) {
1886 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1887 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1888 }
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001889 link->id = le32_to_cpu(pcm->pcm_id);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001890
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001891 if (strlen(pcm->dai_name))
1892 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1893
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001894 link->codec_name = "snd-soc-dummy";
1895 link->codec_dai_name = "snd-soc-dummy-dai";
1896
1897 /* enable DPCM */
1898 link->dynamic = 1;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001899 link->dpcm_playback = le32_to_cpu(pcm->playback);
1900 link->dpcm_capture = le32_to_cpu(pcm->capture);
Mengdong Lin717a8e72016-11-03 01:03:34 +08001901 if (pcm->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001902 set_link_flags(link,
1903 le32_to_cpu(pcm->flag_mask),
1904 le32_to_cpu(pcm->flags));
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001905
Mengdong Linacfc7d42016-01-15 16:13:37 +08001906 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001907 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001908 if (ret < 0) {
1909 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
Bard liaob3718b82019-03-05 23:57:53 +08001910 kfree(link->name);
1911 kfree(link->stream_name);
1912 kfree(link->cpu_dai_name);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001913 kfree(link);
1914 return ret;
1915 }
1916
1917 link->dobj.index = tplg->index;
1918 link->dobj.ops = tplg->ops;
1919 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1920 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1921
1922 snd_soc_add_dai_link(tplg->comp->card, link);
1923 return 0;
1924}
1925
1926/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001927static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1928 struct snd_soc_tplg_pcm *pcm)
1929{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001930 int ret;
1931
1932 ret = soc_tplg_dai_create(tplg, pcm);
1933 if (ret < 0)
1934 return ret;
1935
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001936 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001937}
1938
Mengdong Lin55726dc2016-11-03 01:00:16 +08001939/* copy stream caps from the old version 4 of source */
1940static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1941 struct snd_soc_tplg_stream_caps_v4 *src)
1942{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001943 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin55726dc2016-11-03 01:00:16 +08001944 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1945 dest->formats = src->formats;
1946 dest->rates = src->rates;
1947 dest->rate_min = src->rate_min;
1948 dest->rate_max = src->rate_max;
1949 dest->channels_min = src->channels_min;
1950 dest->channels_max = src->channels_max;
1951 dest->periods_min = src->periods_min;
1952 dest->periods_max = src->periods_max;
1953 dest->period_size_min = src->period_size_min;
1954 dest->period_size_max = src->period_size_max;
1955 dest->buffer_size_min = src->buffer_size_min;
1956 dest->buffer_size_max = src->buffer_size_max;
1957}
1958
1959/**
1960 * pcm_new_ver - Create the new version of PCM from the old version.
1961 * @tplg: topology context
1962 * @src: older version of pcm as a source
1963 * @pcm: latest version of pcm created from the source
1964 *
1965 * Support from vesion 4. User should free the returned pcm manually.
1966 */
1967static int pcm_new_ver(struct soc_tplg *tplg,
1968 struct snd_soc_tplg_pcm *src,
1969 struct snd_soc_tplg_pcm **pcm)
1970{
1971 struct snd_soc_tplg_pcm *dest;
1972 struct snd_soc_tplg_pcm_v4 *src_v4;
1973 int i;
1974
1975 *pcm = NULL;
1976
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001977 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001978 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1979 return -EINVAL;
1980 }
1981
1982 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1983 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1984 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1985 if (!dest)
1986 return -ENOMEM;
1987
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001988 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin55726dc2016-11-03 01:00:16 +08001989 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1990 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1991 dest->pcm_id = src_v4->pcm_id;
1992 dest->dai_id = src_v4->dai_id;
1993 dest->playback = src_v4->playback;
1994 dest->capture = src_v4->capture;
1995 dest->compress = src_v4->compress;
1996 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05001997 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin55726dc2016-11-03 01:00:16 +08001998 memcpy(&dest->stream[i], &src_v4->stream[i],
1999 sizeof(struct snd_soc_tplg_stream));
2000
2001 for (i = 0; i < 2; i++)
2002 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2003
2004 *pcm = dest;
2005 return 0;
2006}
2007
Mengdong Lin64527e82016-01-15 16:13:28 +08002008static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01002009 struct snd_soc_tplg_hdr *hdr)
2010{
Mengdong Lin55726dc2016-11-03 01:00:16 +08002011 struct snd_soc_tplg_pcm *pcm, *_pcm;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002012 int count;
2013 int size;
Vinod Koulfd340452016-12-08 23:01:27 +05302014 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08002015 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01002016
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002017 count = le32_to_cpu(hdr->count);
2018
Liam Girdwood8a978232015-05-29 19:06:14 +01002019 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
2020 return 0;
2021
Mengdong Lin55726dc2016-11-03 01:00:16 +08002022 /* check the element size and count */
2023 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002024 size = le32_to_cpu(pcm->size);
2025 if (size > sizeof(struct snd_soc_tplg_pcm)
2026 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002027 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002028 size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002029 return -EINVAL;
2030 }
2031
Liam Girdwood8a978232015-05-29 19:06:14 +01002032 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002033 size, count,
2034 le32_to_cpu(hdr->payload_size),
2035 "PCM DAI")) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002036 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2037 count);
2038 return -EINVAL;
2039 }
2040
Mengdong Lin64527e82016-01-15 16:13:28 +08002041 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002042 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002043 size = le32_to_cpu(pcm->size);
Mengdong Lin55726dc2016-11-03 01:00:16 +08002044
2045 /* check ABI version by size, create a new version of pcm
2046 * if abi not match.
2047 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002048 if (size == sizeof(*pcm)) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002049 abi_match = true;
2050 _pcm = pcm;
2051 } else {
2052 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05302053 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002054 }
2055
Mengdong Lin55726dc2016-11-03 01:00:16 +08002056 /* create the FE DAIs and DAI links */
2057 soc_tplg_pcm_create(tplg, _pcm);
2058
Mengdong Lin717a8e72016-11-03 01:03:34 +08002059 /* offset by version-specific struct size and
2060 * real priv data size
2061 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002062 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
Mengdong Lin717a8e72016-11-03 01:03:34 +08002063
Mengdong Lin55726dc2016-11-03 01:00:16 +08002064 if (!abi_match)
2065 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08002066 }
2067
Liam Girdwood8a978232015-05-29 19:06:14 +01002068 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002069
Liam Girdwood8a978232015-05-29 19:06:14 +01002070 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002071}
2072
Mengdong Lin593d9e52016-11-03 01:04:27 +08002073/**
2074 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002075 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002076 * @cfg: physical link configs.
2077 *
2078 * Topology context contains a list of supported HW formats (configs) and
2079 * a default format ID for the physical link. This function will use this
2080 * default ID to choose the HW format to set the link's DAI format for init.
2081 */
2082static void set_link_hw_format(struct snd_soc_dai_link *link,
2083 struct snd_soc_tplg_link_config *cfg)
2084{
2085 struct snd_soc_tplg_hw_config *hw_config;
2086 unsigned char bclk_master, fsync_master;
2087 unsigned char invert_bclk, invert_fsync;
2088 int i;
2089
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002090 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002091 hw_config = &cfg->hw_config[i];
2092 if (hw_config->id != cfg->default_hw_config_id)
2093 continue;
2094
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002095 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2096 SND_SOC_DAIFMT_FORMAT_MASK;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002097
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002098 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002099 switch (hw_config->clock_gated) {
2100 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002101 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002102 break;
2103
2104 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002105 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002106 break;
2107
2108 default:
2109 /* ignore the value */
2110 break;
2111 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002112
Mengdong Lin593d9e52016-11-03 01:04:27 +08002113 /* clock signal polarity */
2114 invert_bclk = hw_config->invert_bclk;
2115 invert_fsync = hw_config->invert_fsync;
2116 if (!invert_bclk && !invert_fsync)
2117 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2118 else if (!invert_bclk && invert_fsync)
2119 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2120 else if (invert_bclk && !invert_fsync)
2121 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2122 else
2123 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2124
2125 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002126 bclk_master = (hw_config->bclk_master ==
2127 SND_SOC_TPLG_BCLK_CM);
2128 fsync_master = (hw_config->fsync_master ==
2129 SND_SOC_TPLG_FSYNC_CM);
2130 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002131 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002132 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002133 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2134 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002135 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2136 else
2137 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2138 }
2139}
2140
2141/**
2142 * link_new_ver - Create a new physical link config from the old
2143 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002144 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002145 * @src: old version of phyical link config as a source
2146 * @link: latest version of physical link config created from the source
2147 *
2148 * Support from vesion 4. User need free the returned link config manually.
2149 */
2150static int link_new_ver(struct soc_tplg *tplg,
2151 struct snd_soc_tplg_link_config *src,
2152 struct snd_soc_tplg_link_config **link)
2153{
2154 struct snd_soc_tplg_link_config *dest;
2155 struct snd_soc_tplg_link_config_v4 *src_v4;
2156 int i;
2157
2158 *link = NULL;
2159
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002160 if (le32_to_cpu(src->size) !=
2161 sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002162 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2163 return -EINVAL;
2164 }
2165
2166 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2167
2168 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2169 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2170 if (!dest)
2171 return -ENOMEM;
2172
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002173 dest->size = cpu_to_le32(sizeof(*dest));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002174 dest->id = src_v4->id;
2175 dest->num_streams = src_v4->num_streams;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002176 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002177 memcpy(&dest->stream[i], &src_v4->stream[i],
2178 sizeof(struct snd_soc_tplg_stream));
2179
2180 *link = dest;
2181 return 0;
2182}
2183
2184/* Find and configure an existing physical DAI link */
2185static int soc_tplg_link_config(struct soc_tplg *tplg,
2186 struct snd_soc_tplg_link_config *cfg)
2187{
2188 struct snd_soc_dai_link *link;
2189 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002190 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002191 int ret;
2192
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002193 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2194 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2195 return -EINVAL;
2196 else if (len)
2197 name = cfg->name;
2198 else
2199 name = NULL;
2200
2201 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2202 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2203 return -EINVAL;
2204 else if (len)
2205 stream_name = cfg->stream_name;
2206 else
2207 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002208
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002209 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
Mengdong Lin593d9e52016-11-03 01:04:27 +08002210 name, stream_name);
2211 if (!link) {
2212 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2213 name, cfg->id);
2214 return -EINVAL;
2215 }
2216
2217 /* hw format */
2218 if (cfg->num_hw_configs)
2219 set_link_hw_format(link, cfg);
2220
2221 /* flags */
2222 if (cfg->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002223 set_link_flags(link,
2224 le32_to_cpu(cfg->flag_mask),
2225 le32_to_cpu(cfg->flags));
Mengdong Lin593d9e52016-11-03 01:04:27 +08002226
2227 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002228 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002229 if (ret < 0) {
2230 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2231 return ret;
2232 }
2233
Bard liaoadfebb52019-02-01 11:07:40 -06002234 /* for unloading it in snd_soc_tplg_component_remove */
2235 link->dobj.index = tplg->index;
2236 link->dobj.ops = tplg->ops;
2237 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2238 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2239
Mengdong Lin593d9e52016-11-03 01:04:27 +08002240 return 0;
2241}
2242
2243
2244/* Load physical link config elements from the topology context */
2245static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2246 struct snd_soc_tplg_hdr *hdr)
2247{
2248 struct snd_soc_tplg_link_config *link, *_link;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002249 int count;
2250 int size;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002251 int i, ret;
2252 bool abi_match;
2253
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002254 count = le32_to_cpu(hdr->count);
2255
Mengdong Lin593d9e52016-11-03 01:04:27 +08002256 if (tplg->pass != SOC_TPLG_PASS_LINK) {
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002257 tplg->pos += le32_to_cpu(hdr->size) +
2258 le32_to_cpu(hdr->payload_size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002259 return 0;
2260 };
2261
2262 /* check the element size and count */
2263 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002264 size = le32_to_cpu(link->size);
2265 if (size > sizeof(struct snd_soc_tplg_link_config)
2266 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002267 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002268 size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002269 return -EINVAL;
2270 }
2271
2272 if (soc_tplg_check_elem_count(tplg,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002273 size, count,
2274 le32_to_cpu(hdr->payload_size),
2275 "physical link config")) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002276 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2277 count);
2278 return -EINVAL;
2279 }
2280
2281 /* config physical DAI links */
2282 for (i = 0; i < count; i++) {
2283 link = (struct snd_soc_tplg_link_config *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002284 size = le32_to_cpu(link->size);
2285 if (size == sizeof(*link)) {
Mengdong Lin593d9e52016-11-03 01:04:27 +08002286 abi_match = true;
2287 _link = link;
2288 } else {
2289 abi_match = false;
2290 ret = link_new_ver(tplg, link, &_link);
2291 if (ret < 0)
2292 return ret;
2293 }
2294
2295 ret = soc_tplg_link_config(tplg, _link);
2296 if (ret < 0)
2297 return ret;
2298
2299 /* offset by version-specific struct size and
2300 * real priv data size
2301 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002302 tplg->pos += size + le32_to_cpu(_link->priv.size);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002303
2304 if (!abi_match)
2305 kfree(_link); /* free the duplicated one */
2306 }
2307
2308 return 0;
2309}
2310
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002311/**
2312 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002313 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002314 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002315 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002316 * The physical dai should already be registered by the platform driver.
2317 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002318 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002319static int soc_tplg_dai_config(struct soc_tplg *tplg,
2320 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002321{
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002322 struct snd_soc_dai_link_component dai_component;
Mengdong Lin0038be92016-07-26 14:32:37 +08002323 struct snd_soc_dai *dai;
2324 struct snd_soc_dai_driver *dai_drv;
2325 struct snd_soc_pcm_stream *stream;
2326 struct snd_soc_tplg_stream_caps *caps;
2327 int ret;
2328
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002329 memset(&dai_component, 0, sizeof(dai_component));
2330
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002331 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002332 dai = snd_soc_find_dai(&dai_component);
2333 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002334 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2335 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002336 return -EINVAL;
2337 }
2338
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002339 if (le32_to_cpu(d->dai_id) != dai->id) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002340 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2341 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002342 return -EINVAL;
2343 }
2344
2345 dai_drv = dai->driver;
2346 if (!dai_drv)
2347 return -EINVAL;
2348
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002349 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002350 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002351 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002352 set_stream_info(stream, caps);
2353 }
2354
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002355 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002356 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002357 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002358 set_stream_info(stream, caps);
2359 }
2360
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002361 if (d->flag_mask)
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002362 set_dai_flags(dai_drv,
2363 le32_to_cpu(d->flag_mask),
2364 le32_to_cpu(d->flags));
Mengdong Lin0038be92016-07-26 14:32:37 +08002365
2366 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002367 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002368 if (ret < 0) {
2369 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2370 return ret;
2371 }
2372
2373 return 0;
2374}
2375
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002376/* load physical DAI elements */
2377static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2378 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002379{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002380 struct snd_soc_tplg_dai *dai;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002381 int count;
Mengdong Lin0038be92016-07-26 14:32:37 +08002382 int i;
2383
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002384 count = le32_to_cpu(hdr->count);
2385
Mengdong Lin0038be92016-07-26 14:32:37 +08002386 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2387 return 0;
2388
2389 /* config the existing BE DAIs */
2390 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002391 dai = (struct snd_soc_tplg_dai *)tplg->pos;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002392 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002393 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002394 return -EINVAL;
2395 }
2396
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002397 soc_tplg_dai_config(tplg, dai);
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002398 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
Mengdong Lin0038be92016-07-26 14:32:37 +08002399 }
2400
2401 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2402 return 0;
2403}
2404
Mengdong Lin583958f2016-10-11 14:36:42 +08002405/**
2406 * manifest_new_ver - Create a new version of manifest from the old version
2407 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002408 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002409 * @src: old version of manifest as a source
2410 * @manifest: latest version of manifest created from the source
2411 *
2412 * Support from vesion 4. Users need free the returned manifest manually.
2413 */
2414static int manifest_new_ver(struct soc_tplg *tplg,
2415 struct snd_soc_tplg_manifest *src,
2416 struct snd_soc_tplg_manifest **manifest)
2417{
2418 struct snd_soc_tplg_manifest *dest;
2419 struct snd_soc_tplg_manifest_v4 *src_v4;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002420 int size;
Mengdong Lin583958f2016-10-11 14:36:42 +08002421
2422 *manifest = NULL;
2423
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002424 size = le32_to_cpu(src->size);
2425 if (size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002426 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002427 size);
2428 if (size)
Guenter Roeckac9391d2018-05-24 12:49:21 -07002429 return -EINVAL;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002430 src->size = cpu_to_le32(sizeof(*src_v4));
Mengdong Lin583958f2016-10-11 14:36:42 +08002431 }
2432
2433 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2434
2435 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002436 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2437 GFP_KERNEL);
Mengdong Lin583958f2016-10-11 14:36:42 +08002438 if (!dest)
2439 return -ENOMEM;
2440
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002441 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Mengdong Lin583958f2016-10-11 14:36:42 +08002442 dest->control_elems = src_v4->control_elems;
2443 dest->widget_elems = src_v4->widget_elems;
2444 dest->graph_elems = src_v4->graph_elems;
2445 dest->pcm_elems = src_v4->pcm_elems;
2446 dest->dai_link_elems = src_v4->dai_link_elems;
2447 dest->priv.size = src_v4->priv.size;
2448 if (dest->priv.size)
2449 memcpy(dest->priv.data, src_v4->priv.data,
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002450 le32_to_cpu(src_v4->priv.size));
Mengdong Lin583958f2016-10-11 14:36:42 +08002451
2452 *manifest = dest;
2453 return 0;
2454}
Mengdong Lin0038be92016-07-26 14:32:37 +08002455
Liam Girdwood8a978232015-05-29 19:06:14 +01002456static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002457 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002458{
Mengdong Lin583958f2016-10-11 14:36:42 +08002459 struct snd_soc_tplg_manifest *manifest, *_manifest;
2460 bool abi_match;
2461 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002462
2463 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2464 return 0;
2465
2466 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002467
2468 /* check ABI version by size, create a new manifest if abi not match */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002469 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
Mengdong Lin583958f2016-10-11 14:36:42 +08002470 abi_match = true;
2471 _manifest = manifest;
2472 } else {
2473 abi_match = false;
2474 err = manifest_new_ver(tplg, manifest, &_manifest);
2475 if (err < 0)
2476 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002477 }
2478
Mengdong Lin583958f2016-10-11 14:36:42 +08002479 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002480 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002481 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002482
Mengdong Lin583958f2016-10-11 14:36:42 +08002483 if (!abi_match) /* free the duplicated one */
2484 kfree(_manifest);
2485
Liam Girdwood8a978232015-05-29 19:06:14 +01002486 return 0;
2487}
2488
2489/* validate header magic, size and type */
2490static int soc_valid_header(struct soc_tplg *tplg,
2491 struct snd_soc_tplg_hdr *hdr)
2492{
2493 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2494 return 0;
2495
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002496 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002497 dev_err(tplg->dev,
2498 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002499 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002500 tplg->fw->size);
2501 return -EINVAL;
2502 }
2503
Liam Girdwood8a978232015-05-29 19:06:14 +01002504 /* big endian firmware objects not supported atm */
2505 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2506 dev_err(tplg->dev,
2507 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2508 tplg->pass, hdr->magic,
2509 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2510 return -EINVAL;
2511 }
2512
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002513 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002514 dev_err(tplg->dev,
2515 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2516 tplg->pass, hdr->magic,
2517 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2518 return -EINVAL;
2519 }
2520
Mengdong Lin288b8da2016-11-03 01:03:17 +08002521 /* Support ABI from version 4 */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002522 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2523 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002524 dev_err(tplg->dev,
2525 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2526 tplg->pass, hdr->abi,
2527 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2528 tplg->fw->size);
2529 return -EINVAL;
2530 }
2531
2532 if (hdr->payload_size == 0) {
2533 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2534 soc_tplg_get_hdr_offset(tplg));
2535 return -EINVAL;
2536 }
2537
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002538 if (tplg->pass == le32_to_cpu(hdr->type))
Liam Girdwood8a978232015-05-29 19:06:14 +01002539 dev_dbg(tplg->dev,
2540 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2541 hdr->payload_size, hdr->type, hdr->version,
2542 hdr->vendor_type, tplg->pass);
2543
2544 return 1;
2545}
2546
2547/* check header type and call appropriate handler */
2548static int soc_tplg_load_header(struct soc_tplg *tplg,
2549 struct snd_soc_tplg_hdr *hdr)
2550{
2551 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2552
2553 /* check for matching ID */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002554 if (le32_to_cpu(hdr->index) != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002555 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002556 return 0;
2557
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002558 tplg->index = le32_to_cpu(hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01002559
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002560 switch (le32_to_cpu(hdr->type)) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002561 case SND_SOC_TPLG_TYPE_MIXER:
2562 case SND_SOC_TPLG_TYPE_ENUM:
2563 case SND_SOC_TPLG_TYPE_BYTES:
2564 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2565 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2566 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2567 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2568 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2569 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002570 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002571 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002572 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002573 case SND_SOC_TPLG_TYPE_DAI_LINK:
2574 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2575 /* physical link configurations */
2576 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002577 case SND_SOC_TPLG_TYPE_MANIFEST:
2578 return soc_tplg_manifest_load(tplg, hdr);
2579 default:
2580 /* bespoke vendor data object */
2581 return soc_tplg_vendor_load(tplg, hdr);
2582 }
2583
2584 return 0;
2585}
2586
2587/* process the topology file headers */
2588static int soc_tplg_process_headers(struct soc_tplg *tplg)
2589{
2590 struct snd_soc_tplg_hdr *hdr;
2591 int ret;
2592
2593 tplg->pass = SOC_TPLG_PASS_START;
2594
2595 /* process the header types from start to end */
2596 while (tplg->pass <= SOC_TPLG_PASS_END) {
2597
2598 tplg->hdr_pos = tplg->fw->data;
2599 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2600
2601 while (!soc_tplg_is_eof(tplg)) {
2602
2603 /* make sure header is valid before loading */
2604 ret = soc_valid_header(tplg, hdr);
2605 if (ret < 0)
2606 return ret;
2607 else if (ret == 0)
2608 break;
2609
2610 /* load the header object */
2611 ret = soc_tplg_load_header(tplg, hdr);
2612 if (ret < 0)
2613 return ret;
2614
2615 /* goto next header */
Pierre-Louis Bossart5aebe7c2019-04-04 14:13:57 -05002616 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
Liam Girdwood8a978232015-05-29 19:06:14 +01002617 sizeof(struct snd_soc_tplg_hdr);
2618 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2619 }
2620
2621 /* next data type pass */
2622 tplg->pass++;
2623 }
2624
2625 /* signal DAPM we are complete */
2626 ret = soc_tplg_dapm_complete(tplg);
2627 if (ret < 0)
2628 dev_err(tplg->dev,
2629 "ASoC: failed to initialise DAPM from Firmware\n");
2630
2631 return ret;
2632}
2633
2634static int soc_tplg_load(struct soc_tplg *tplg)
2635{
2636 int ret;
2637
2638 ret = soc_tplg_process_headers(tplg);
2639 if (ret == 0)
2640 soc_tplg_complete(tplg);
2641
2642 return ret;
2643}
2644
2645/* load audio component topology from "firmware" file */
2646int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2647 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2648{
2649 struct soc_tplg tplg;
Bard liao304017d2019-02-17 21:23:47 +08002650 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002651
2652 /* setup parsing context */
2653 memset(&tplg, 0, sizeof(tplg));
2654 tplg.fw = fw;
2655 tplg.dev = comp->dev;
2656 tplg.comp = comp;
2657 tplg.ops = ops;
2658 tplg.req_index = id;
2659 tplg.io_ops = ops->io_ops;
2660 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002661 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2662 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002663
Bard liao304017d2019-02-17 21:23:47 +08002664 ret = soc_tplg_load(&tplg);
2665 /* free the created components if fail to load topology */
2666 if (ret)
2667 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2668
2669 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002670}
2671EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2672
2673/* remove this dynamic widget */
2674void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2675{
2676 /* make sure we are a widget */
2677 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2678 return;
2679
2680 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2681}
2682EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2683
2684/* remove all dynamic widgets from this DAPM context */
2685void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2686 u32 index)
2687{
2688 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002689
2690 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2691
2692 /* make sure we are a widget with correct context */
2693 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2694 continue;
2695
2696 /* match ID */
2697 if (w->dobj.index != index &&
2698 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2699 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002700 /* check and free and dynamic widget kcontrols */
2701 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002702 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002703 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002704 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002705}
2706EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2707
2708/* remove dynamic controls from the component driver */
2709int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2710{
2711 struct snd_soc_dobj *dobj, *next_dobj;
2712 int pass = SOC_TPLG_PASS_END;
2713
2714 /* process the header types from end to start */
2715 while (pass >= SOC_TPLG_PASS_START) {
2716
2717 /* remove mixer controls */
2718 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2719 list) {
2720
2721 /* match index */
2722 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002723 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002724 continue;
2725
2726 switch (dobj->type) {
2727 case SND_SOC_DOBJ_MIXER:
2728 remove_mixer(comp, dobj, pass);
2729 break;
2730 case SND_SOC_DOBJ_ENUM:
2731 remove_enum(comp, dobj, pass);
2732 break;
2733 case SND_SOC_DOBJ_BYTES:
2734 remove_bytes(comp, dobj, pass);
2735 break;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06002736 case SND_SOC_DOBJ_GRAPH:
2737 remove_route(comp, dobj, pass);
2738 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002739 case SND_SOC_DOBJ_WIDGET:
2740 remove_widget(comp, dobj, pass);
2741 break;
2742 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002743 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002744 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002745 case SND_SOC_DOBJ_DAI_LINK:
2746 remove_link(comp, dobj, pass);
2747 break;
Bard liaoadfebb52019-02-01 11:07:40 -06002748 case SND_SOC_DOBJ_BACKEND_LINK:
2749 /*
2750 * call link_unload ops if extra
2751 * deinitialization is needed.
2752 */
2753 remove_backend_link(comp, dobj, pass);
2754 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002755 default:
2756 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2757 dobj->type);
2758 break;
2759 }
2760 }
2761 pass--;
2762 }
2763
2764 /* let caller know if FW can be freed when no objects are left */
2765 return !list_empty(&comp->dobj_list);
2766}
2767EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);