blob: 25fca7055464a894e5e260bf872de8c410136f56 [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++) {
200 if (chan[i].id == map)
201 return chan[i].reg;
202 }
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++) {
213 if (chan[i].id == map)
214 return chan[i].shift;
215 }
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
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800538 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100539 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800540 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100541}
542
Mengdong Linacfc7d42016-01-15 16:13:37 +0800543/* remove link configurations */
544static void remove_link(struct snd_soc_component *comp,
545 struct snd_soc_dobj *dobj, int pass)
546{
547 struct snd_soc_dai_link *link =
548 container_of(dobj, struct snd_soc_dai_link, dobj);
549
550 if (pass != SOC_TPLG_PASS_PCM_DAI)
551 return;
552
553 if (dobj->ops && dobj->ops->link_unload)
554 dobj->ops->link_unload(comp, dobj);
555
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800556 kfree(link->name);
557 kfree(link->stream_name);
558 kfree(link->cpu_dai_name);
559
Mengdong Linacfc7d42016-01-15 16:13:37 +0800560 list_del(&dobj->list);
561 snd_soc_remove_dai_link(comp->card, link);
562 kfree(link);
563}
564
Bard liaoadfebb52019-02-01 11:07:40 -0600565/* unload dai link */
566static void remove_backend_link(struct snd_soc_component *comp,
567 struct snd_soc_dobj *dobj, int pass)
568{
569 if (pass != SOC_TPLG_PASS_LINK)
570 return;
571
572 if (dobj->ops && dobj->ops->link_unload)
573 dobj->ops->link_unload(comp, dobj);
574
575 /*
576 * We don't free the link here as what remove_link() do since BE
577 * links are not allocated by topology.
578 * We however need to reset the dobj type to its initial values
579 */
580 dobj->type = SND_SOC_DOBJ_NONE;
581 list_del(&dobj->list);
582}
583
Liam Girdwood8a978232015-05-29 19:06:14 +0100584/* bind a kcontrol to it's IO handlers */
585static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
586 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800587 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100588{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800589 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800590 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800591 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100592
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800593 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
594 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
595 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
596 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
597 struct soc_bytes_ext *sbe;
598 struct snd_soc_tplg_bytes_control *be;
599
600 sbe = (struct soc_bytes_ext *)k->private_value;
601 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
602
603 /* TLV bytes controls need standard kcontrol info handler,
604 * TLV callback and extended put/get handlers.
605 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530606 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800607 k->tlv.c = snd_soc_bytes_tlv_callback;
608
609 ext_ops = tplg->bytes_ext_ops;
610 num_ops = tplg->bytes_ext_ops_count;
611 for (i = 0; i < num_ops; i++) {
612 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
613 sbe->put = ext_ops[i].put;
614 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
615 sbe->get = ext_ops[i].get;
616 }
617
618 if (sbe->put && sbe->get)
619 return 0;
620 else
621 return -EINVAL;
622 }
623
Mengdong Lin88a17d82015-08-18 18:11:51 +0800624 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800625 ops = tplg->io_ops;
626 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100627 for (i = 0; i < num_ops; i++) {
628
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800629 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100630 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800631 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100632 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800633 if (k->info == NULL && ops[i].id == hdr->ops.info)
634 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100635 }
636
Mengdong Lin88a17d82015-08-18 18:11:51 +0800637 /* vendor specific handlers found ? */
638 if (k->put && k->get && k->info)
639 return 0;
640
641 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800642 ops = io_ops;
643 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800644 for (i = 0; i < num_ops; i++) {
645
646 if (k->put == NULL && ops[i].id == hdr->ops.put)
647 k->put = ops[i].put;
648 if (k->get == NULL && ops[i].id == hdr->ops.get)
649 k->get = ops[i].get;
650 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100651 k->info = ops[i].info;
652 }
653
654 /* standard handlers found ? */
655 if (k->put && k->get && k->info)
656 return 0;
657
Liam Girdwood8a978232015-05-29 19:06:14 +0100658 /* nothing to bind */
659 return -EINVAL;
660}
661
662/* bind a widgets to it's evnt handlers */
663int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
664 const struct snd_soc_tplg_widget_events *events,
665 int num_events, u16 event_type)
666{
667 int i;
668
669 w->event = NULL;
670
671 for (i = 0; i < num_events; i++) {
672 if (event_type == events[i].type) {
673
674 /* found - so assign event */
675 w->event = events[i].event_handler;
676 return 0;
677 }
678 }
679
680 /* not found */
681 return -EINVAL;
682}
683EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
684
685/* optionally pass new dynamic kcontrol to component driver. */
686static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
687 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
688{
689 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100690 return tplg->ops->control_load(tplg->comp, tplg->index, k,
691 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100692
693 return 0;
694}
695
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100696
697static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
698 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100699{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100700 unsigned int item_len = 2 * sizeof(unsigned int);
701 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100702
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100703 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
704 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100705 return -ENOMEM;
706
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100707 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
708 p[1] = item_len;
709 p[2] = scale->min;
710 p[3] = (scale->step & TLV_DB_SCALE_MASK)
711 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100712
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100713 kc->tlv.p = (void *)p;
714 return 0;
715}
716
717static int soc_tplg_create_tlv(struct soc_tplg *tplg,
718 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
719{
720 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
721
722 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
723 return 0;
724
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800725 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100726 tplg_tlv = &tc->tlv;
727 switch (tplg_tlv->type) {
728 case SNDRV_CTL_TLVT_DB_SCALE:
729 return soc_tplg_create_tlv_db_scale(tplg, kc,
730 &tplg_tlv->scale);
731
732 /* TODO: add support for other TLV types */
733 default:
734 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
735 tplg_tlv->type);
736 return -EINVAL;
737 }
738 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100739
740 return 0;
741}
742
743static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
744 struct snd_kcontrol_new *kc)
745{
746 kfree(kc->tlv.p);
747}
748
749static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
750 size_t size)
751{
752 struct snd_soc_tplg_bytes_control *be;
753 struct soc_bytes_ext *sbe;
754 struct snd_kcontrol_new kc;
755 int i, err;
756
757 if (soc_tplg_check_elem_count(tplg,
758 sizeof(struct snd_soc_tplg_bytes_control), count,
759 size, "mixer bytes")) {
760 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
761 count);
762 return -EINVAL;
763 }
764
765 for (i = 0; i < count; i++) {
766 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
767
768 /* validate kcontrol */
769 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
770 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
771 return -EINVAL;
772
773 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
774 if (sbe == NULL)
775 return -ENOMEM;
776
777 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
778 be->priv.size);
779
780 dev_dbg(tplg->dev,
781 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
782 be->hdr.name, be->hdr.access);
783
784 memset(&kc, 0, sizeof(kc));
785 kc.name = be->hdr.name;
786 kc.private_value = (long)sbe;
787 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
788 kc.access = be->hdr.access;
789
790 sbe->max = be->max;
791 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
792 sbe->dobj.ops = tplg->ops;
793 INIT_LIST_HEAD(&sbe->dobj.list);
794
795 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800796 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100797 if (err) {
798 soc_control_err(tplg, &be->hdr, be->hdr.name);
799 kfree(sbe);
800 continue;
801 }
802
803 /* pass control to driver for optional further init */
804 err = soc_tplg_init_kcontrol(tplg, &kc,
805 (struct snd_soc_tplg_ctl_hdr *)be);
806 if (err < 0) {
807 dev_err(tplg->dev, "ASoC: failed to init %s\n",
808 be->hdr.name);
809 kfree(sbe);
810 continue;
811 }
812
813 /* register control here */
814 err = soc_tplg_add_kcontrol(tplg, &kc,
815 &sbe->dobj.control.kcontrol);
816 if (err < 0) {
817 dev_err(tplg->dev, "ASoC: failed to add %s\n",
818 be->hdr.name);
819 kfree(sbe);
820 continue;
821 }
822
823 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
824 }
825 return 0;
826
827}
828
829static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
830 size_t size)
831{
832 struct snd_soc_tplg_mixer_control *mc;
833 struct soc_mixer_control *sm;
834 struct snd_kcontrol_new kc;
835 int i, err;
836
837 if (soc_tplg_check_elem_count(tplg,
838 sizeof(struct snd_soc_tplg_mixer_control),
839 count, size, "mixers")) {
840
841 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
842 count);
843 return -EINVAL;
844 }
845
846 for (i = 0; i < count; i++) {
847 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
848
849 /* validate kcontrol */
850 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
851 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
852 return -EINVAL;
853
854 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
855 if (sm == NULL)
856 return -ENOMEM;
857 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
858 mc->priv.size);
859
860 dev_dbg(tplg->dev,
861 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
862 mc->hdr.name, mc->hdr.access);
863
864 memset(&kc, 0, sizeof(kc));
865 kc.name = mc->hdr.name;
866 kc.private_value = (long)sm;
867 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
868 kc.access = mc->hdr.access;
869
870 /* we only support FL/FR channel mapping atm */
871 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
872 SNDRV_CHMAP_FL);
873 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
874 SNDRV_CHMAP_FR);
875 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
876 SNDRV_CHMAP_FL);
877 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
878 SNDRV_CHMAP_FR);
879
880 sm->max = mc->max;
881 sm->min = mc->min;
882 sm->invert = mc->invert;
883 sm->platform_max = mc->platform_max;
884 sm->dobj.index = tplg->index;
885 sm->dobj.ops = tplg->ops;
886 sm->dobj.type = SND_SOC_DOBJ_MIXER;
887 INIT_LIST_HEAD(&sm->dobj.list);
888
889 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800890 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100891 if (err) {
892 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
893 kfree(sm);
894 continue;
895 }
896
897 /* pass control to driver for optional further init */
898 err = soc_tplg_init_kcontrol(tplg, &kc,
899 (struct snd_soc_tplg_ctl_hdr *) mc);
900 if (err < 0) {
901 dev_err(tplg->dev, "ASoC: failed to init %s\n",
902 mc->hdr.name);
903 kfree(sm);
904 continue;
905 }
906
907 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100908 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100909
910 /* register control here */
911 err = soc_tplg_add_kcontrol(tplg, &kc,
912 &sm->dobj.control.kcontrol);
913 if (err < 0) {
914 dev_err(tplg->dev, "ASoC: failed to add %s\n",
915 mc->hdr.name);
916 soc_tplg_free_tlv(tplg, &kc);
917 kfree(sm);
918 continue;
919 }
920
921 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
922 }
923
924 return 0;
925}
926
927static int soc_tplg_denum_create_texts(struct soc_enum *se,
928 struct snd_soc_tplg_enum_control *ec)
929{
930 int i, ret;
931
932 se->dobj.control.dtexts =
Kees Cook6396bb22018-06-12 14:03:40 -0700933 kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100934 if (se->dobj.control.dtexts == NULL)
935 return -ENOMEM;
936
937 for (i = 0; i < ec->items; i++) {
938
939 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
940 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
941 ret = -EINVAL;
942 goto err;
943 }
944
945 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
946 if (!se->dobj.control.dtexts[i]) {
947 ret = -ENOMEM;
948 goto err;
949 }
950 }
951
Mousumi Janab6e38b22017-04-11 13:06:22 +0530952 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100953 return 0;
954
955err:
956 for (--i; i >= 0; i--)
957 kfree(se->dobj.control.dtexts[i]);
958 kfree(se->dobj.control.dtexts);
959 return ret;
960}
961
962static int soc_tplg_denum_create_values(struct soc_enum *se,
963 struct snd_soc_tplg_enum_control *ec)
964{
965 if (ec->items > sizeof(*ec->values))
966 return -EINVAL;
967
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200968 se->dobj.control.dvalues = kmemdup(ec->values,
969 ec->items * sizeof(u32),
970 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100971 if (!se->dobj.control.dvalues)
972 return -ENOMEM;
973
Liam Girdwood8a978232015-05-29 19:06:14 +0100974 return 0;
975}
976
977static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
978 size_t size)
979{
980 struct snd_soc_tplg_enum_control *ec;
981 struct soc_enum *se;
982 struct snd_kcontrol_new kc;
983 int i, ret, err;
984
985 if (soc_tplg_check_elem_count(tplg,
986 sizeof(struct snd_soc_tplg_enum_control),
987 count, size, "enums")) {
988
989 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
990 count);
991 return -EINVAL;
992 }
993
994 for (i = 0; i < count; i++) {
995 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
996 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
997 ec->priv.size);
998
999 /* validate kcontrol */
1000 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1001 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1002 return -EINVAL;
1003
1004 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1005 if (se == NULL)
1006 return -ENOMEM;
1007
1008 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1009 ec->hdr.name, ec->items);
1010
1011 memset(&kc, 0, sizeof(kc));
1012 kc.name = ec->hdr.name;
1013 kc.private_value = (long)se;
1014 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1015 kc.access = ec->hdr.access;
1016
1017 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1018 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1019 SNDRV_CHMAP_FL);
1020 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1021 SNDRV_CHMAP_FL);
1022
1023 se->items = ec->items;
1024 se->mask = ec->mask;
1025 se->dobj.index = tplg->index;
1026 se->dobj.type = SND_SOC_DOBJ_ENUM;
1027 se->dobj.ops = tplg->ops;
1028 INIT_LIST_HEAD(&se->dobj.list);
1029
1030 switch (ec->hdr.ops.info) {
1031 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1032 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1033 err = soc_tplg_denum_create_values(se, ec);
1034 if (err < 0) {
1035 dev_err(tplg->dev,
1036 "ASoC: could not create values for %s\n",
1037 ec->hdr.name);
1038 kfree(se);
1039 continue;
1040 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001041 /* fall through */
Liam Girdwood8a978232015-05-29 19:06:14 +01001042 case SND_SOC_TPLG_CTL_ENUM:
1043 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1044 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1045 err = soc_tplg_denum_create_texts(se, ec);
1046 if (err < 0) {
1047 dev_err(tplg->dev,
1048 "ASoC: could not create texts for %s\n",
1049 ec->hdr.name);
1050 kfree(se);
1051 continue;
1052 }
1053 break;
1054 default:
1055 dev_err(tplg->dev,
1056 "ASoC: invalid enum control type %d for %s\n",
1057 ec->hdr.ops.info, ec->hdr.name);
1058 kfree(se);
1059 continue;
1060 }
1061
1062 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001063 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001064 if (err) {
1065 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1066 kfree(se);
1067 continue;
1068 }
1069
1070 /* pass control to driver for optional further init */
1071 err = soc_tplg_init_kcontrol(tplg, &kc,
1072 (struct snd_soc_tplg_ctl_hdr *) ec);
1073 if (err < 0) {
1074 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1075 ec->hdr.name);
1076 kfree(se);
1077 continue;
1078 }
1079
1080 /* register control here */
1081 ret = soc_tplg_add_kcontrol(tplg,
1082 &kc, &se->dobj.control.kcontrol);
1083 if (ret < 0) {
1084 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1085 ec->hdr.name);
1086 kfree(se);
1087 continue;
1088 }
1089
1090 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1091 }
1092
1093 return 0;
1094}
1095
1096static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1097 struct snd_soc_tplg_hdr *hdr)
1098{
1099 struct snd_soc_tplg_ctl_hdr *control_hdr;
1100 int i;
1101
1102 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1103 tplg->pos += hdr->size + hdr->payload_size;
1104 return 0;
1105 }
1106
1107 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1108 soc_tplg_get_offset(tplg));
1109
1110 for (i = 0; i < hdr->count; i++) {
1111
1112 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1113
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001114 if (control_hdr->size != sizeof(*control_hdr)) {
1115 dev_err(tplg->dev, "ASoC: invalid control size\n");
1116 return -EINVAL;
1117 }
1118
Liam Girdwood8a978232015-05-29 19:06:14 +01001119 switch (control_hdr->ops.info) {
1120 case SND_SOC_TPLG_CTL_VOLSW:
1121 case SND_SOC_TPLG_CTL_STROBE:
1122 case SND_SOC_TPLG_CTL_VOLSW_SX:
1123 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1124 case SND_SOC_TPLG_CTL_RANGE:
1125 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1126 case SND_SOC_TPLG_DAPM_CTL_PIN:
1127 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1128 break;
1129 case SND_SOC_TPLG_CTL_ENUM:
1130 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1131 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1132 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1133 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1134 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1135 break;
1136 case SND_SOC_TPLG_CTL_BYTES:
1137 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1138 break;
1139 default:
1140 soc_bind_err(tplg, control_hdr, i);
1141 return -EINVAL;
1142 }
1143 }
1144
1145 return 0;
1146}
1147
Liam Girdwood503e79b2018-06-14 20:53:59 +01001148/* optionally pass new dynamic kcontrol to component driver. */
1149static int soc_tplg_add_route(struct soc_tplg *tplg,
1150 struct snd_soc_dapm_route *route)
1151{
1152 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1153 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1154 route);
1155
1156 return 0;
1157}
1158
Liam Girdwood8a978232015-05-29 19:06:14 +01001159static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1160 struct snd_soc_tplg_hdr *hdr)
1161{
1162 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001163 struct snd_soc_tplg_dapm_graph_elem *elem;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001164 struct snd_soc_dapm_route **routes;
1165 int count = hdr->count, i, j;
1166 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001167
1168 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1169 tplg->pos += hdr->size + hdr->payload_size;
1170 return 0;
1171 }
1172
1173 if (soc_tplg_check_elem_count(tplg,
1174 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1175 count, hdr->payload_size, "graph")) {
1176
1177 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1178 count);
1179 return -EINVAL;
1180 }
1181
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001182 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1183 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001184
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001185 /* allocate memory for pointer to array of dapm routes */
1186 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1187 GFP_KERNEL);
1188 if (!routes)
1189 return -ENOMEM;
1190
1191 /*
1192 * allocate memory for each dapm route in the array.
1193 * This needs to be done individually so that
1194 * each route can be freed when it is removed in remove_route().
1195 */
1196 for (i = 0; i < count; i++) {
1197 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1198 if (!routes[i]) {
1199 /* free previously allocated memory */
1200 for (j = 0; j < i; j++)
1201 kfree(routes[j]);
1202
1203 kfree(routes);
1204 return -ENOMEM;
1205 }
1206 }
1207
Liam Girdwood8a978232015-05-29 19:06:14 +01001208 for (i = 0; i < count; i++) {
1209 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1210 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1211
1212 /* validate routes */
1213 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001214 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1215 ret = -EINVAL;
1216 break;
1217 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001218 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001219 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1220 ret = -EINVAL;
1221 break;
1222 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001223 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001224 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1225 ret = -EINVAL;
1226 break;
1227 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001228
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001229 routes[i]->source = elem->source;
1230 routes[i]->sink = elem->sink;
1231
1232 /* set to NULL atm for tplg users */
1233 routes[i]->connected = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001234 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001235 routes[i]->control = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001236 else
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001237 routes[i]->control = elem->control;
Liam Girdwood8a978232015-05-29 19:06:14 +01001238
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001239 /* add route dobj to dobj_list */
1240 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1241 routes[i]->dobj.ops = tplg->ops;
1242 routes[i]->dobj.index = tplg->index;
1243 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1244
1245 soc_tplg_add_route(tplg, routes[i]);
Liam Girdwood503e79b2018-06-14 20:53:59 +01001246
Liam Girdwood8a978232015-05-29 19:06:14 +01001247 /* add route, but keep going if some fail */
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001248 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Liam Girdwood8a978232015-05-29 19:06:14 +01001249 }
1250
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001251 /* free memory allocated for all dapm routes in case of error */
1252 if (ret < 0)
1253 for (i = 0; i < count ; i++)
1254 kfree(routes[i]);
1255
1256 /*
1257 * free pointer to array of dapm routes as this is no longer needed.
1258 * The memory allocated for each dapm route will be freed
1259 * when it is removed in remove_route().
1260 */
1261 kfree(routes);
1262
1263 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001264}
1265
1266static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1267 struct soc_tplg *tplg, int num_kcontrols)
1268{
1269 struct snd_kcontrol_new *kc;
1270 struct soc_mixer_control *sm;
1271 struct snd_soc_tplg_mixer_control *mc;
1272 int i, err;
1273
Axel Lin4ca7deb2015-08-05 22:34:22 +08001274 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001275 if (kc == NULL)
1276 return NULL;
1277
1278 for (i = 0; i < num_kcontrols; i++) {
1279 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1280 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1281 if (sm == NULL)
1282 goto err;
1283
1284 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1285 mc->priv.size);
1286
1287 /* validate kcontrol */
1288 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1289 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1290 goto err_str;
1291
1292 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1293 mc->hdr.name, i);
1294
Liam Girdwood267e2c62018-03-27 12:04:04 +01001295 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1296 if (kc[i].name == NULL)
1297 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001298 kc[i].private_value = (long)sm;
1299 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1300 kc[i].access = mc->hdr.access;
1301
1302 /* we only support FL/FR channel mapping atm */
1303 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1304 SNDRV_CHMAP_FL);
1305 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1306 SNDRV_CHMAP_FR);
1307 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1308 SNDRV_CHMAP_FL);
1309 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1310 SNDRV_CHMAP_FR);
1311
1312 sm->max = mc->max;
1313 sm->min = mc->min;
1314 sm->invert = mc->invert;
1315 sm->platform_max = mc->platform_max;
1316 sm->dobj.index = tplg->index;
1317 INIT_LIST_HEAD(&sm->dobj.list);
1318
1319 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001320 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001321 if (err) {
1322 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1323 kfree(sm);
1324 continue;
1325 }
1326
1327 /* pass control to driver for optional further init */
1328 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1329 (struct snd_soc_tplg_ctl_hdr *)mc);
1330 if (err < 0) {
1331 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1332 mc->hdr.name);
1333 kfree(sm);
1334 continue;
1335 }
Ranjani Sridharanbde8b382018-03-09 11:11:17 -08001336
1337 /* create any TLV data */
1338 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01001339 }
1340 return kc;
1341
1342err_str:
1343 kfree(sm);
1344err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001345 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001346 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001347 kfree(kc[i].name);
1348 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001349 kfree(kc);
1350 return NULL;
1351}
1352
1353static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001354 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001355{
1356 struct snd_kcontrol_new *kc;
1357 struct snd_soc_tplg_enum_control *ec;
1358 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001359 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001360
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001361 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001362 if (kc == NULL)
1363 return NULL;
1364
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001365 for (i = 0; i < num_kcontrols; i++) {
1366 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1367 /* validate kcontrol */
1368 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1369 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001370 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001371
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001372 se = kzalloc(sizeof(*se), GFP_KERNEL);
1373 if (se == NULL)
1374 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001375
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001376 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001377 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001378
Liam Girdwood267e2c62018-03-27 12:04:04 +01001379 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001380 if (kc[i].name == NULL) {
1381 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001382 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001383 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001384 kc[i].private_value = (long)se;
1385 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1386 kc[i].access = ec->hdr.access;
1387
1388 /* we only support FL/FR channel mapping atm */
1389 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1390 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1391 SNDRV_CHMAP_FL);
1392 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1393 SNDRV_CHMAP_FR);
1394
1395 se->items = ec->items;
1396 se->mask = ec->mask;
1397 se->dobj.index = tplg->index;
1398
1399 switch (ec->hdr.ops.info) {
1400 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1401 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1402 err = soc_tplg_denum_create_values(se, ec);
1403 if (err < 0) {
1404 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1405 ec->hdr.name);
1406 goto err_se;
1407 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001408 /* fall through */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001409 case SND_SOC_TPLG_CTL_ENUM:
1410 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1411 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1412 err = soc_tplg_denum_create_texts(se, ec);
1413 if (err < 0) {
1414 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1415 ec->hdr.name);
1416 goto err_se;
1417 }
1418 break;
1419 default:
1420 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1421 ec->hdr.ops.info, ec->hdr.name);
1422 goto err_se;
1423 }
1424
1425 /* map io handlers */
1426 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1427 if (err) {
1428 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1429 goto err_se;
1430 }
1431
1432 /* pass control to driver for optional further init */
1433 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1434 (struct snd_soc_tplg_ctl_hdr *)ec);
1435 if (err < 0) {
1436 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1437 ec->hdr.name);
1438 goto err_se;
1439 }
1440
1441 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1442 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001443 }
1444
1445 return kc;
1446
1447err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001448 for (; i >= 0; i--) {
1449 /* free values and texts */
1450 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001451 if (!se)
1452 continue;
1453
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001454 kfree(se->dobj.control.dvalues);
1455 for (j = 0; j < ec->items; j++)
1456 kfree(se->dobj.control.dtexts[j]);
Amadeusz Sławiński34db6a32019-01-25 14:06:44 -06001457 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +01001458
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001459 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001460 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001461 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001462err:
1463 kfree(kc);
1464
1465 return NULL;
1466}
1467
1468static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1469 struct soc_tplg *tplg, int count)
1470{
1471 struct snd_soc_tplg_bytes_control *be;
1472 struct soc_bytes_ext *sbe;
1473 struct snd_kcontrol_new *kc;
1474 int i, err;
1475
Axel Lin4ca7deb2015-08-05 22:34:22 +08001476 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001477 if (!kc)
1478 return NULL;
1479
1480 for (i = 0; i < count; i++) {
1481 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1482
1483 /* validate kcontrol */
1484 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1485 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1486 goto err;
1487
1488 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1489 if (sbe == NULL)
1490 goto err;
1491
1492 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1493 be->priv.size);
1494
1495 dev_dbg(tplg->dev,
1496 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1497 be->hdr.name, be->hdr.access);
1498
Liam Girdwood267e2c62018-03-27 12:04:04 +01001499 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001500 if (kc[i].name == NULL) {
1501 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001502 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001503 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001504 kc[i].private_value = (long)sbe;
1505 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1506 kc[i].access = be->hdr.access;
1507
1508 sbe->max = be->max;
1509 INIT_LIST_HEAD(&sbe->dobj.list);
1510
1511 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001512 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001513 if (err) {
1514 soc_control_err(tplg, &be->hdr, be->hdr.name);
1515 kfree(sbe);
1516 continue;
1517 }
1518
1519 /* pass control to driver for optional further init */
1520 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1521 (struct snd_soc_tplg_ctl_hdr *)be);
1522 if (err < 0) {
1523 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1524 be->hdr.name);
1525 kfree(sbe);
1526 continue;
1527 }
1528 }
1529
1530 return kc;
1531
1532err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001533 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001534 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001535 kfree(kc[i].name);
1536 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001537
1538 kfree(kc);
1539 return NULL;
1540}
1541
1542static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1543 struct snd_soc_tplg_dapm_widget *w)
1544{
1545 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1546 struct snd_soc_dapm_widget template, *widget;
1547 struct snd_soc_tplg_ctl_hdr *control_hdr;
1548 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001549 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001550 int ret = 0;
1551
1552 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1553 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1554 return -EINVAL;
1555 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1556 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1557 return -EINVAL;
1558
1559 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1560 w->name, w->id);
1561
1562 memset(&template, 0, sizeof(template));
1563
1564 /* map user to kernel widget ID */
1565 template.id = get_widget_id(w->id);
1566 if (template.id < 0)
1567 return template.id;
1568
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001569 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001570 template.name = kstrdup(w->name, GFP_KERNEL);
1571 if (!template.name)
1572 return -ENOMEM;
1573 template.sname = kstrdup(w->sname, GFP_KERNEL);
1574 if (!template.sname) {
1575 ret = -ENOMEM;
1576 goto err;
1577 }
1578 template.reg = w->reg;
1579 template.shift = w->shift;
1580 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001581 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001582 template.on_val = w->invert ? 0 : 1;
1583 template.off_val = w->invert ? 1 : 0;
1584 template.ignore_suspend = w->ignore_suspend;
1585 template.event_flags = w->event_flags;
1586 template.dobj.index = tplg->index;
1587
1588 tplg->pos +=
1589 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1590 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001591 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001592 template.num_kcontrols = 0;
1593 goto widget;
1594 }
1595
1596 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1597 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1598 w->name, w->num_kcontrols, control_hdr->type);
1599
1600 switch (control_hdr->ops.info) {
1601 case SND_SOC_TPLG_CTL_VOLSW:
1602 case SND_SOC_TPLG_CTL_STROBE:
1603 case SND_SOC_TPLG_CTL_VOLSW_SX:
1604 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1605 case SND_SOC_TPLG_CTL_RANGE:
1606 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001607 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001608 template.num_kcontrols = w->num_kcontrols;
1609 template.kcontrol_news =
1610 soc_tplg_dapm_widget_dmixer_create(tplg,
1611 template.num_kcontrols);
1612 if (!template.kcontrol_news) {
1613 ret = -ENOMEM;
1614 goto hdr_err;
1615 }
1616 break;
1617 case SND_SOC_TPLG_CTL_ENUM:
1618 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1619 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1620 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1621 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001622 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001623 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001624 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001625 soc_tplg_dapm_widget_denum_create(tplg,
1626 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001627 if (!template.kcontrol_news) {
1628 ret = -ENOMEM;
1629 goto hdr_err;
1630 }
1631 break;
1632 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001633 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001634 template.num_kcontrols = w->num_kcontrols;
1635 template.kcontrol_news =
1636 soc_tplg_dapm_widget_dbytes_create(tplg,
1637 template.num_kcontrols);
1638 if (!template.kcontrol_news) {
1639 ret = -ENOMEM;
1640 goto hdr_err;
1641 }
1642 break;
1643 default:
1644 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1645 control_hdr->ops.get, control_hdr->ops.put,
1646 control_hdr->ops.info);
1647 ret = -EINVAL;
1648 goto hdr_err;
1649 }
1650
1651widget:
1652 ret = soc_tplg_widget_load(tplg, &template, w);
1653 if (ret < 0)
1654 goto hdr_err;
1655
1656 /* card dapm mutex is held by the core if we are loading topology
1657 * data during sound card init. */
1658 if (card->instantiated)
1659 widget = snd_soc_dapm_new_control(dapm, &template);
1660 else
1661 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001662 if (IS_ERR(widget)) {
1663 ret = PTR_ERR(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001664 goto hdr_err;
1665 }
1666
1667 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001668 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001669 widget->dobj.ops = tplg->ops;
1670 widget->dobj.index = tplg->index;
1671 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001672
1673 ret = soc_tplg_widget_ready(tplg, widget, w);
1674 if (ret < 0)
1675 goto ready_err;
1676
Bard liao7620fe92019-01-25 14:06:45 -06001677 kfree(template.sname);
1678 kfree(template.name);
1679
Liam Girdwood8a978232015-05-29 19:06:14 +01001680 return 0;
1681
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001682ready_err:
1683 snd_soc_tplg_widget_remove(widget);
1684 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001685hdr_err:
1686 kfree(template.sname);
1687err:
1688 kfree(template.name);
1689 return ret;
1690}
1691
1692static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1693 struct snd_soc_tplg_hdr *hdr)
1694{
1695 struct snd_soc_tplg_dapm_widget *widget;
1696 int ret, count = hdr->count, i;
1697
1698 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1699 return 0;
1700
1701 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1702
1703 for (i = 0; i < count; i++) {
1704 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001705 if (widget->size != sizeof(*widget)) {
1706 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1707 return -EINVAL;
1708 }
1709
Liam Girdwood8a978232015-05-29 19:06:14 +01001710 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001711 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001712 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1713 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001714 return ret;
1715 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001716 }
1717
1718 return 0;
1719}
1720
1721static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1722{
1723 struct snd_soc_card *card = tplg->comp->card;
1724 int ret;
1725
1726 /* Card might not have been registered at this point.
1727 * If so, just return success.
1728 */
1729 if (!card || !card->instantiated) {
1730 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001731 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001732 return 0;
1733 }
1734
1735 ret = snd_soc_dapm_new_widgets(card);
1736 if (ret < 0)
1737 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1738 ret);
1739
1740 return 0;
1741}
1742
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001743static void set_stream_info(struct snd_soc_pcm_stream *stream,
1744 struct snd_soc_tplg_stream_caps *caps)
1745{
1746 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1747 stream->channels_min = caps->channels_min;
1748 stream->channels_max = caps->channels_max;
1749 stream->rates = caps->rates;
1750 stream->rate_min = caps->rate_min;
1751 stream->rate_max = caps->rate_max;
1752 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001753 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001754}
1755
Mengdong Lin0038be92016-07-26 14:32:37 +08001756static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1757 unsigned int flag_mask, unsigned int flags)
1758{
1759 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1760 dai_drv->symmetric_rates =
1761 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1762
1763 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1764 dai_drv->symmetric_channels =
1765 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1766 1 : 0;
1767
1768 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1769 dai_drv->symmetric_samplebits =
1770 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1771 1 : 0;
1772}
1773
Mengdong Lin64527e82016-01-15 16:13:28 +08001774static int soc_tplg_dai_create(struct soc_tplg *tplg,
1775 struct snd_soc_tplg_pcm *pcm)
1776{
1777 struct snd_soc_dai_driver *dai_drv;
1778 struct snd_soc_pcm_stream *stream;
1779 struct snd_soc_tplg_stream_caps *caps;
1780 int ret;
1781
1782 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1783 if (dai_drv == NULL)
1784 return -ENOMEM;
1785
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001786 if (strlen(pcm->dai_name))
1787 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001788 dai_drv->id = pcm->dai_id;
1789
1790 if (pcm->playback) {
1791 stream = &dai_drv->playback;
1792 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001793 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001794 }
1795
1796 if (pcm->capture) {
1797 stream = &dai_drv->capture;
1798 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001799 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001800 }
1801
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001802 if (pcm->compress)
1803 dai_drv->compress_new = snd_soc_new_compress;
1804
Mengdong Lin64527e82016-01-15 16:13:28 +08001805 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001806 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001807 if (ret < 0) {
1808 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1809 kfree(dai_drv);
1810 return ret;
1811 }
1812
1813 dai_drv->dobj.index = tplg->index;
1814 dai_drv->dobj.ops = tplg->ops;
1815 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1816 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1817
1818 /* register the DAI to the component */
1819 return snd_soc_register_dai(tplg->comp, dai_drv);
1820}
1821
Mengdong Lin717a8e72016-11-03 01:03:34 +08001822static void set_link_flags(struct snd_soc_dai_link *link,
1823 unsigned int flag_mask, unsigned int flags)
1824{
1825 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1826 link->symmetric_rates =
1827 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1828
1829 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1830 link->symmetric_channels =
1831 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1832 1 : 0;
1833
1834 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1835 link->symmetric_samplebits =
1836 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1837 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001838
1839 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1840 link->ignore_suspend =
1841 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1842 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001843}
1844
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001845/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001846static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001847 struct snd_soc_tplg_pcm *pcm)
1848{
1849 struct snd_soc_dai_link *link;
1850 int ret;
1851
1852 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1853 if (link == NULL)
1854 return -ENOMEM;
1855
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001856 if (strlen(pcm->pcm_name)) {
1857 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1858 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1859 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001860 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001861
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001862 if (strlen(pcm->dai_name))
1863 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1864
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001865 link->codec_name = "snd-soc-dummy";
1866 link->codec_dai_name = "snd-soc-dummy-dai";
1867
1868 /* enable DPCM */
1869 link->dynamic = 1;
1870 link->dpcm_playback = pcm->playback;
1871 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001872 if (pcm->flag_mask)
1873 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001874
Mengdong Linacfc7d42016-01-15 16:13:37 +08001875 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001876 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001877 if (ret < 0) {
1878 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1879 kfree(link);
1880 return ret;
1881 }
1882
1883 link->dobj.index = tplg->index;
1884 link->dobj.ops = tplg->ops;
1885 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1886 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1887
1888 snd_soc_add_dai_link(tplg->comp->card, link);
1889 return 0;
1890}
1891
1892/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001893static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1894 struct snd_soc_tplg_pcm *pcm)
1895{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001896 int ret;
1897
1898 ret = soc_tplg_dai_create(tplg, pcm);
1899 if (ret < 0)
1900 return ret;
1901
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001902 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001903}
1904
Mengdong Lin55726dc2016-11-03 01:00:16 +08001905/* copy stream caps from the old version 4 of source */
1906static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1907 struct snd_soc_tplg_stream_caps_v4 *src)
1908{
1909 dest->size = sizeof(*dest);
1910 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1911 dest->formats = src->formats;
1912 dest->rates = src->rates;
1913 dest->rate_min = src->rate_min;
1914 dest->rate_max = src->rate_max;
1915 dest->channels_min = src->channels_min;
1916 dest->channels_max = src->channels_max;
1917 dest->periods_min = src->periods_min;
1918 dest->periods_max = src->periods_max;
1919 dest->period_size_min = src->period_size_min;
1920 dest->period_size_max = src->period_size_max;
1921 dest->buffer_size_min = src->buffer_size_min;
1922 dest->buffer_size_max = src->buffer_size_max;
1923}
1924
1925/**
1926 * pcm_new_ver - Create the new version of PCM from the old version.
1927 * @tplg: topology context
1928 * @src: older version of pcm as a source
1929 * @pcm: latest version of pcm created from the source
1930 *
1931 * Support from vesion 4. User should free the returned pcm manually.
1932 */
1933static int pcm_new_ver(struct soc_tplg *tplg,
1934 struct snd_soc_tplg_pcm *src,
1935 struct snd_soc_tplg_pcm **pcm)
1936{
1937 struct snd_soc_tplg_pcm *dest;
1938 struct snd_soc_tplg_pcm_v4 *src_v4;
1939 int i;
1940
1941 *pcm = NULL;
1942
1943 if (src->size != sizeof(*src_v4)) {
1944 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1945 return -EINVAL;
1946 }
1947
1948 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1949 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1950 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1951 if (!dest)
1952 return -ENOMEM;
1953
1954 dest->size = sizeof(*dest); /* size of latest abi version */
1955 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1956 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1957 dest->pcm_id = src_v4->pcm_id;
1958 dest->dai_id = src_v4->dai_id;
1959 dest->playback = src_v4->playback;
1960 dest->capture = src_v4->capture;
1961 dest->compress = src_v4->compress;
1962 dest->num_streams = src_v4->num_streams;
1963 for (i = 0; i < dest->num_streams; i++)
1964 memcpy(&dest->stream[i], &src_v4->stream[i],
1965 sizeof(struct snd_soc_tplg_stream));
1966
1967 for (i = 0; i < 2; i++)
1968 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1969
1970 *pcm = dest;
1971 return 0;
1972}
1973
Mengdong Lin64527e82016-01-15 16:13:28 +08001974static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001975 struct snd_soc_tplg_hdr *hdr)
1976{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001977 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001978 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301979 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001980 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001981
1982 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1983 return 0;
1984
Mengdong Lin55726dc2016-11-03 01:00:16 +08001985 /* check the element size and count */
1986 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1987 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1988 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1989 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1990 pcm->size);
1991 return -EINVAL;
1992 }
1993
Liam Girdwood8a978232015-05-29 19:06:14 +01001994 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001995 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001996 hdr->payload_size, "PCM DAI")) {
1997 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1998 count);
1999 return -EINVAL;
2000 }
2001
Mengdong Lin64527e82016-01-15 16:13:28 +08002002 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002003 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
2004
2005 /* check ABI version by size, create a new version of pcm
2006 * if abi not match.
2007 */
2008 if (pcm->size == sizeof(*pcm)) {
2009 abi_match = true;
2010 _pcm = pcm;
2011 } else {
2012 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05302013 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002014 }
2015
Mengdong Lin55726dc2016-11-03 01:00:16 +08002016 /* create the FE DAIs and DAI links */
2017 soc_tplg_pcm_create(tplg, _pcm);
2018
Mengdong Lin717a8e72016-11-03 01:03:34 +08002019 /* offset by version-specific struct size and
2020 * real priv data size
2021 */
2022 tplg->pos += pcm->size + _pcm->priv.size;
2023
Mengdong Lin55726dc2016-11-03 01:00:16 +08002024 if (!abi_match)
2025 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08002026 }
2027
Liam Girdwood8a978232015-05-29 19:06:14 +01002028 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002029
Liam Girdwood8a978232015-05-29 19:06:14 +01002030 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002031}
2032
Mengdong Lin593d9e52016-11-03 01:04:27 +08002033/**
2034 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002035 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002036 * @cfg: physical link configs.
2037 *
2038 * Topology context contains a list of supported HW formats (configs) and
2039 * a default format ID for the physical link. This function will use this
2040 * default ID to choose the HW format to set the link's DAI format for init.
2041 */
2042static void set_link_hw_format(struct snd_soc_dai_link *link,
2043 struct snd_soc_tplg_link_config *cfg)
2044{
2045 struct snd_soc_tplg_hw_config *hw_config;
2046 unsigned char bclk_master, fsync_master;
2047 unsigned char invert_bclk, invert_fsync;
2048 int i;
2049
2050 for (i = 0; i < cfg->num_hw_configs; i++) {
2051 hw_config = &cfg->hw_config[i];
2052 if (hw_config->id != cfg->default_hw_config_id)
2053 continue;
2054
2055 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
2056
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002057 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002058 switch (hw_config->clock_gated) {
2059 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002060 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002061 break;
2062
2063 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002064 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002065 break;
2066
2067 default:
2068 /* ignore the value */
2069 break;
2070 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002071
Mengdong Lin593d9e52016-11-03 01:04:27 +08002072 /* clock signal polarity */
2073 invert_bclk = hw_config->invert_bclk;
2074 invert_fsync = hw_config->invert_fsync;
2075 if (!invert_bclk && !invert_fsync)
2076 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2077 else if (!invert_bclk && invert_fsync)
2078 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2079 else if (invert_bclk && !invert_fsync)
2080 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2081 else
2082 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2083
2084 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002085 bclk_master = (hw_config->bclk_master ==
2086 SND_SOC_TPLG_BCLK_CM);
2087 fsync_master = (hw_config->fsync_master ==
2088 SND_SOC_TPLG_FSYNC_CM);
2089 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002090 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002091 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002092 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2093 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002094 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2095 else
2096 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2097 }
2098}
2099
2100/**
2101 * link_new_ver - Create a new physical link config from the old
2102 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002103 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002104 * @src: old version of phyical link config as a source
2105 * @link: latest version of physical link config created from the source
2106 *
2107 * Support from vesion 4. User need free the returned link config manually.
2108 */
2109static int link_new_ver(struct soc_tplg *tplg,
2110 struct snd_soc_tplg_link_config *src,
2111 struct snd_soc_tplg_link_config **link)
2112{
2113 struct snd_soc_tplg_link_config *dest;
2114 struct snd_soc_tplg_link_config_v4 *src_v4;
2115 int i;
2116
2117 *link = NULL;
2118
2119 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2120 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2121 return -EINVAL;
2122 }
2123
2124 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2125
2126 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2127 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2128 if (!dest)
2129 return -ENOMEM;
2130
2131 dest->size = sizeof(*dest);
2132 dest->id = src_v4->id;
2133 dest->num_streams = src_v4->num_streams;
2134 for (i = 0; i < dest->num_streams; i++)
2135 memcpy(&dest->stream[i], &src_v4->stream[i],
2136 sizeof(struct snd_soc_tplg_stream));
2137
2138 *link = dest;
2139 return 0;
2140}
2141
2142/* Find and configure an existing physical DAI link */
2143static int soc_tplg_link_config(struct soc_tplg *tplg,
2144 struct snd_soc_tplg_link_config *cfg)
2145{
2146 struct snd_soc_dai_link *link;
2147 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002148 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002149 int ret;
2150
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002151 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2152 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2153 return -EINVAL;
2154 else if (len)
2155 name = cfg->name;
2156 else
2157 name = NULL;
2158
2159 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2160 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2161 return -EINVAL;
2162 else if (len)
2163 stream_name = cfg->stream_name;
2164 else
2165 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002166
2167 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2168 name, stream_name);
2169 if (!link) {
2170 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2171 name, cfg->id);
2172 return -EINVAL;
2173 }
2174
2175 /* hw format */
2176 if (cfg->num_hw_configs)
2177 set_link_hw_format(link, cfg);
2178
2179 /* flags */
2180 if (cfg->flag_mask)
2181 set_link_flags(link, cfg->flag_mask, cfg->flags);
2182
2183 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002184 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002185 if (ret < 0) {
2186 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2187 return ret;
2188 }
2189
Bard liaoadfebb52019-02-01 11:07:40 -06002190 /* for unloading it in snd_soc_tplg_component_remove */
2191 link->dobj.index = tplg->index;
2192 link->dobj.ops = tplg->ops;
2193 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2194 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2195
Mengdong Lin593d9e52016-11-03 01:04:27 +08002196 return 0;
2197}
2198
2199
2200/* Load physical link config elements from the topology context */
2201static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2202 struct snd_soc_tplg_hdr *hdr)
2203{
2204 struct snd_soc_tplg_link_config *link, *_link;
2205 int count = hdr->count;
2206 int i, ret;
2207 bool abi_match;
2208
2209 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2210 tplg->pos += hdr->size + hdr->payload_size;
2211 return 0;
2212 };
2213
2214 /* check the element size and count */
2215 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2216 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2217 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2218 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2219 link->size);
2220 return -EINVAL;
2221 }
2222
2223 if (soc_tplg_check_elem_count(tplg,
2224 link->size, count,
2225 hdr->payload_size, "physical link config")) {
2226 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2227 count);
2228 return -EINVAL;
2229 }
2230
2231 /* config physical DAI links */
2232 for (i = 0; i < count; i++) {
2233 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2234 if (link->size == sizeof(*link)) {
2235 abi_match = true;
2236 _link = link;
2237 } else {
2238 abi_match = false;
2239 ret = link_new_ver(tplg, link, &_link);
2240 if (ret < 0)
2241 return ret;
2242 }
2243
2244 ret = soc_tplg_link_config(tplg, _link);
2245 if (ret < 0)
2246 return ret;
2247
2248 /* offset by version-specific struct size and
2249 * real priv data size
2250 */
2251 tplg->pos += link->size + _link->priv.size;
2252
2253 if (!abi_match)
2254 kfree(_link); /* free the duplicated one */
2255 }
2256
2257 return 0;
2258}
2259
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002260/**
2261 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002262 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002263 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002264 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002265 * The physical dai should already be registered by the platform driver.
2266 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002267 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002268static int soc_tplg_dai_config(struct soc_tplg *tplg,
2269 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002270{
2271 struct snd_soc_dai_link_component dai_component = {0};
2272 struct snd_soc_dai *dai;
2273 struct snd_soc_dai_driver *dai_drv;
2274 struct snd_soc_pcm_stream *stream;
2275 struct snd_soc_tplg_stream_caps *caps;
2276 int ret;
2277
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002278 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002279 dai = snd_soc_find_dai(&dai_component);
2280 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002281 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2282 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002283 return -EINVAL;
2284 }
2285
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002286 if (d->dai_id != dai->id) {
2287 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2288 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002289 return -EINVAL;
2290 }
2291
2292 dai_drv = dai->driver;
2293 if (!dai_drv)
2294 return -EINVAL;
2295
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002296 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002297 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002298 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002299 set_stream_info(stream, caps);
2300 }
2301
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002302 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002303 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002304 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002305 set_stream_info(stream, caps);
2306 }
2307
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002308 if (d->flag_mask)
2309 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002310
2311 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002312 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002313 if (ret < 0) {
2314 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2315 return ret;
2316 }
2317
2318 return 0;
2319}
2320
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002321/* load physical DAI elements */
2322static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2323 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002324{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002325 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002326 int count = hdr->count;
2327 int i;
2328
2329 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2330 return 0;
2331
2332 /* config the existing BE DAIs */
2333 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002334 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2335 if (dai->size != sizeof(*dai)) {
2336 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002337 return -EINVAL;
2338 }
2339
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002340 soc_tplg_dai_config(tplg, dai);
2341 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002342 }
2343
2344 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2345 return 0;
2346}
2347
Mengdong Lin583958f2016-10-11 14:36:42 +08002348/**
2349 * manifest_new_ver - Create a new version of manifest from the old version
2350 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002351 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002352 * @src: old version of manifest as a source
2353 * @manifest: latest version of manifest created from the source
2354 *
2355 * Support from vesion 4. Users need free the returned manifest manually.
2356 */
2357static int manifest_new_ver(struct soc_tplg *tplg,
2358 struct snd_soc_tplg_manifest *src,
2359 struct snd_soc_tplg_manifest **manifest)
2360{
2361 struct snd_soc_tplg_manifest *dest;
2362 struct snd_soc_tplg_manifest_v4 *src_v4;
2363
2364 *manifest = NULL;
2365
2366 if (src->size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002367 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2368 src->size);
2369 if (src->size)
2370 return -EINVAL;
2371 src->size = sizeof(*src_v4);
Mengdong Lin583958f2016-10-11 14:36:42 +08002372 }
2373
2374 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2375
2376 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2377 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2378 if (!dest)
2379 return -ENOMEM;
2380
2381 dest->size = sizeof(*dest); /* size of latest abi version */
2382 dest->control_elems = src_v4->control_elems;
2383 dest->widget_elems = src_v4->widget_elems;
2384 dest->graph_elems = src_v4->graph_elems;
2385 dest->pcm_elems = src_v4->pcm_elems;
2386 dest->dai_link_elems = src_v4->dai_link_elems;
2387 dest->priv.size = src_v4->priv.size;
2388 if (dest->priv.size)
2389 memcpy(dest->priv.data, src_v4->priv.data,
2390 src_v4->priv.size);
2391
2392 *manifest = dest;
2393 return 0;
2394}
Mengdong Lin0038be92016-07-26 14:32:37 +08002395
Liam Girdwood8a978232015-05-29 19:06:14 +01002396static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002397 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002398{
Mengdong Lin583958f2016-10-11 14:36:42 +08002399 struct snd_soc_tplg_manifest *manifest, *_manifest;
2400 bool abi_match;
2401 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002402
2403 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2404 return 0;
2405
2406 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002407
2408 /* check ABI version by size, create a new manifest if abi not match */
2409 if (manifest->size == sizeof(*manifest)) {
2410 abi_match = true;
2411 _manifest = manifest;
2412 } else {
2413 abi_match = false;
2414 err = manifest_new_ver(tplg, manifest, &_manifest);
2415 if (err < 0)
2416 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002417 }
2418
Mengdong Lin583958f2016-10-11 14:36:42 +08002419 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002420 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002421 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002422
Mengdong Lin583958f2016-10-11 14:36:42 +08002423 if (!abi_match) /* free the duplicated one */
2424 kfree(_manifest);
2425
Liam Girdwood8a978232015-05-29 19:06:14 +01002426 return 0;
2427}
2428
2429/* validate header magic, size and type */
2430static int soc_valid_header(struct soc_tplg *tplg,
2431 struct snd_soc_tplg_hdr *hdr)
2432{
2433 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2434 return 0;
2435
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002436 if (hdr->size != sizeof(*hdr)) {
2437 dev_err(tplg->dev,
2438 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2439 hdr->type, soc_tplg_get_hdr_offset(tplg),
2440 tplg->fw->size);
2441 return -EINVAL;
2442 }
2443
Liam Girdwood8a978232015-05-29 19:06:14 +01002444 /* big endian firmware objects not supported atm */
2445 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2446 dev_err(tplg->dev,
2447 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2448 tplg->pass, hdr->magic,
2449 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2450 return -EINVAL;
2451 }
2452
2453 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2454 dev_err(tplg->dev,
2455 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2456 tplg->pass, hdr->magic,
2457 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2458 return -EINVAL;
2459 }
2460
Mengdong Lin288b8da2016-11-03 01:03:17 +08002461 /* Support ABI from version 4 */
2462 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2463 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002464 dev_err(tplg->dev,
2465 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2466 tplg->pass, hdr->abi,
2467 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2468 tplg->fw->size);
2469 return -EINVAL;
2470 }
2471
2472 if (hdr->payload_size == 0) {
2473 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2474 soc_tplg_get_hdr_offset(tplg));
2475 return -EINVAL;
2476 }
2477
2478 if (tplg->pass == hdr->type)
2479 dev_dbg(tplg->dev,
2480 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2481 hdr->payload_size, hdr->type, hdr->version,
2482 hdr->vendor_type, tplg->pass);
2483
2484 return 1;
2485}
2486
2487/* check header type and call appropriate handler */
2488static int soc_tplg_load_header(struct soc_tplg *tplg,
2489 struct snd_soc_tplg_hdr *hdr)
2490{
2491 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2492
2493 /* check for matching ID */
2494 if (hdr->index != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002495 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002496 return 0;
2497
2498 tplg->index = hdr->index;
2499
2500 switch (hdr->type) {
2501 case SND_SOC_TPLG_TYPE_MIXER:
2502 case SND_SOC_TPLG_TYPE_ENUM:
2503 case SND_SOC_TPLG_TYPE_BYTES:
2504 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2505 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2506 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2507 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2508 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2509 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002510 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002511 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002512 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002513 case SND_SOC_TPLG_TYPE_DAI_LINK:
2514 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2515 /* physical link configurations */
2516 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002517 case SND_SOC_TPLG_TYPE_MANIFEST:
2518 return soc_tplg_manifest_load(tplg, hdr);
2519 default:
2520 /* bespoke vendor data object */
2521 return soc_tplg_vendor_load(tplg, hdr);
2522 }
2523
2524 return 0;
2525}
2526
2527/* process the topology file headers */
2528static int soc_tplg_process_headers(struct soc_tplg *tplg)
2529{
2530 struct snd_soc_tplg_hdr *hdr;
2531 int ret;
2532
2533 tplg->pass = SOC_TPLG_PASS_START;
2534
2535 /* process the header types from start to end */
2536 while (tplg->pass <= SOC_TPLG_PASS_END) {
2537
2538 tplg->hdr_pos = tplg->fw->data;
2539 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2540
2541 while (!soc_tplg_is_eof(tplg)) {
2542
2543 /* make sure header is valid before loading */
2544 ret = soc_valid_header(tplg, hdr);
2545 if (ret < 0)
2546 return ret;
2547 else if (ret == 0)
2548 break;
2549
2550 /* load the header object */
2551 ret = soc_tplg_load_header(tplg, hdr);
2552 if (ret < 0)
2553 return ret;
2554
2555 /* goto next header */
2556 tplg->hdr_pos += hdr->payload_size +
2557 sizeof(struct snd_soc_tplg_hdr);
2558 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2559 }
2560
2561 /* next data type pass */
2562 tplg->pass++;
2563 }
2564
2565 /* signal DAPM we are complete */
2566 ret = soc_tplg_dapm_complete(tplg);
2567 if (ret < 0)
2568 dev_err(tplg->dev,
2569 "ASoC: failed to initialise DAPM from Firmware\n");
2570
2571 return ret;
2572}
2573
2574static int soc_tplg_load(struct soc_tplg *tplg)
2575{
2576 int ret;
2577
2578 ret = soc_tplg_process_headers(tplg);
2579 if (ret == 0)
2580 soc_tplg_complete(tplg);
2581
2582 return ret;
2583}
2584
2585/* load audio component topology from "firmware" file */
2586int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2587 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2588{
2589 struct soc_tplg tplg;
Bard liao304017d2019-02-17 21:23:47 +08002590 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002591
2592 /* setup parsing context */
2593 memset(&tplg, 0, sizeof(tplg));
2594 tplg.fw = fw;
2595 tplg.dev = comp->dev;
2596 tplg.comp = comp;
2597 tplg.ops = ops;
2598 tplg.req_index = id;
2599 tplg.io_ops = ops->io_ops;
2600 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002601 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2602 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002603
Bard liao304017d2019-02-17 21:23:47 +08002604 ret = soc_tplg_load(&tplg);
2605 /* free the created components if fail to load topology */
2606 if (ret)
2607 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2608
2609 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002610}
2611EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2612
2613/* remove this dynamic widget */
2614void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2615{
2616 /* make sure we are a widget */
2617 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2618 return;
2619
2620 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2621}
2622EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2623
2624/* remove all dynamic widgets from this DAPM context */
2625void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2626 u32 index)
2627{
2628 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002629
2630 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2631
2632 /* make sure we are a widget with correct context */
2633 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2634 continue;
2635
2636 /* match ID */
2637 if (w->dobj.index != index &&
2638 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2639 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002640 /* check and free and dynamic widget kcontrols */
2641 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002642 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002643 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002644 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002645}
2646EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2647
2648/* remove dynamic controls from the component driver */
2649int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2650{
2651 struct snd_soc_dobj *dobj, *next_dobj;
2652 int pass = SOC_TPLG_PASS_END;
2653
2654 /* process the header types from end to start */
2655 while (pass >= SOC_TPLG_PASS_START) {
2656
2657 /* remove mixer controls */
2658 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2659 list) {
2660
2661 /* match index */
2662 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002663 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002664 continue;
2665
2666 switch (dobj->type) {
2667 case SND_SOC_DOBJ_MIXER:
2668 remove_mixer(comp, dobj, pass);
2669 break;
2670 case SND_SOC_DOBJ_ENUM:
2671 remove_enum(comp, dobj, pass);
2672 break;
2673 case SND_SOC_DOBJ_BYTES:
2674 remove_bytes(comp, dobj, pass);
2675 break;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06002676 case SND_SOC_DOBJ_GRAPH:
2677 remove_route(comp, dobj, pass);
2678 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002679 case SND_SOC_DOBJ_WIDGET:
2680 remove_widget(comp, dobj, pass);
2681 break;
2682 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002683 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002684 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002685 case SND_SOC_DOBJ_DAI_LINK:
2686 remove_link(comp, dobj, pass);
2687 break;
Bard liaoadfebb52019-02-01 11:07:40 -06002688 case SND_SOC_DOBJ_BACKEND_LINK:
2689 /*
2690 * call link_unload ops if extra
2691 * deinitialization is needed.
2692 */
2693 remove_backend_link(comp, dobj, pass);
2694 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002695 default:
2696 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2697 dobj->type);
2698 break;
2699 }
2700 }
2701 pass--;
2702 }
2703
2704 /* let caller know if FW can be freed when no objects are left */
2705 return !list_empty(&comp->dobj_list);
2706}
2707EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);