blob: 1be60cea2bf541842b2ebc95b116ca888f0b2428 [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
Bard liao7b6f68a2019-03-05 23:57:52 +0800538 kfree(dai_drv->playback.stream_name);
539 kfree(dai_drv->capture.stream_name);
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800540 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100541 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800542 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100543}
544
Mengdong Linacfc7d42016-01-15 16:13:37 +0800545/* remove link configurations */
546static void remove_link(struct snd_soc_component *comp,
547 struct snd_soc_dobj *dobj, int pass)
548{
549 struct snd_soc_dai_link *link =
550 container_of(dobj, struct snd_soc_dai_link, dobj);
551
552 if (pass != SOC_TPLG_PASS_PCM_DAI)
553 return;
554
555 if (dobj->ops && dobj->ops->link_unload)
556 dobj->ops->link_unload(comp, dobj);
557
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800558 kfree(link->name);
559 kfree(link->stream_name);
560 kfree(link->cpu_dai_name);
561
Mengdong Linacfc7d42016-01-15 16:13:37 +0800562 list_del(&dobj->list);
563 snd_soc_remove_dai_link(comp->card, link);
564 kfree(link);
565}
566
Bard liaoadfebb52019-02-01 11:07:40 -0600567/* unload dai link */
568static void remove_backend_link(struct snd_soc_component *comp,
569 struct snd_soc_dobj *dobj, int pass)
570{
571 if (pass != SOC_TPLG_PASS_LINK)
572 return;
573
574 if (dobj->ops && dobj->ops->link_unload)
575 dobj->ops->link_unload(comp, dobj);
576
577 /*
578 * We don't free the link here as what remove_link() do since BE
579 * links are not allocated by topology.
580 * We however need to reset the dobj type to its initial values
581 */
582 dobj->type = SND_SOC_DOBJ_NONE;
583 list_del(&dobj->list);
584}
585
Liam Girdwood8a978232015-05-29 19:06:14 +0100586/* bind a kcontrol to it's IO handlers */
587static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
588 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800589 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100590{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800591 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800592 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800593 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100594
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800595 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
596 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
597 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
598 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
599 struct soc_bytes_ext *sbe;
600 struct snd_soc_tplg_bytes_control *be;
601
602 sbe = (struct soc_bytes_ext *)k->private_value;
603 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
604
605 /* TLV bytes controls need standard kcontrol info handler,
606 * TLV callback and extended put/get handlers.
607 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530608 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800609 k->tlv.c = snd_soc_bytes_tlv_callback;
610
611 ext_ops = tplg->bytes_ext_ops;
612 num_ops = tplg->bytes_ext_ops_count;
613 for (i = 0; i < num_ops; i++) {
614 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
615 sbe->put = ext_ops[i].put;
616 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
617 sbe->get = ext_ops[i].get;
618 }
619
620 if (sbe->put && sbe->get)
621 return 0;
622 else
623 return -EINVAL;
624 }
625
Mengdong Lin88a17d82015-08-18 18:11:51 +0800626 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800627 ops = tplg->io_ops;
628 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100629 for (i = 0; i < num_ops; i++) {
630
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800631 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100632 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800633 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100634 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800635 if (k->info == NULL && ops[i].id == hdr->ops.info)
636 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100637 }
638
Mengdong Lin88a17d82015-08-18 18:11:51 +0800639 /* vendor specific handlers found ? */
640 if (k->put && k->get && k->info)
641 return 0;
642
643 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800644 ops = io_ops;
645 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800646 for (i = 0; i < num_ops; i++) {
647
648 if (k->put == NULL && ops[i].id == hdr->ops.put)
649 k->put = ops[i].put;
650 if (k->get == NULL && ops[i].id == hdr->ops.get)
651 k->get = ops[i].get;
652 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100653 k->info = ops[i].info;
654 }
655
656 /* standard handlers found ? */
657 if (k->put && k->get && k->info)
658 return 0;
659
Liam Girdwood8a978232015-05-29 19:06:14 +0100660 /* nothing to bind */
661 return -EINVAL;
662}
663
664/* bind a widgets to it's evnt handlers */
665int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
666 const struct snd_soc_tplg_widget_events *events,
667 int num_events, u16 event_type)
668{
669 int i;
670
671 w->event = NULL;
672
673 for (i = 0; i < num_events; i++) {
674 if (event_type == events[i].type) {
675
676 /* found - so assign event */
677 w->event = events[i].event_handler;
678 return 0;
679 }
680 }
681
682 /* not found */
683 return -EINVAL;
684}
685EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
686
687/* optionally pass new dynamic kcontrol to component driver. */
688static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
689 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
690{
691 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100692 return tplg->ops->control_load(tplg->comp, tplg->index, k,
693 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100694
695 return 0;
696}
697
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100698
699static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
700 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100701{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100702 unsigned int item_len = 2 * sizeof(unsigned int);
703 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100704
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100705 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
706 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100707 return -ENOMEM;
708
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100709 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
710 p[1] = item_len;
711 p[2] = scale->min;
712 p[3] = (scale->step & TLV_DB_SCALE_MASK)
713 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100714
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100715 kc->tlv.p = (void *)p;
716 return 0;
717}
718
719static int soc_tplg_create_tlv(struct soc_tplg *tplg,
720 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
721{
722 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
723
724 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
725 return 0;
726
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800727 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100728 tplg_tlv = &tc->tlv;
729 switch (tplg_tlv->type) {
730 case SNDRV_CTL_TLVT_DB_SCALE:
731 return soc_tplg_create_tlv_db_scale(tplg, kc,
732 &tplg_tlv->scale);
733
734 /* TODO: add support for other TLV types */
735 default:
736 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
737 tplg_tlv->type);
738 return -EINVAL;
739 }
740 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100741
742 return 0;
743}
744
745static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
746 struct snd_kcontrol_new *kc)
747{
748 kfree(kc->tlv.p);
749}
750
751static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
752 size_t size)
753{
754 struct snd_soc_tplg_bytes_control *be;
755 struct soc_bytes_ext *sbe;
756 struct snd_kcontrol_new kc;
757 int i, err;
758
759 if (soc_tplg_check_elem_count(tplg,
760 sizeof(struct snd_soc_tplg_bytes_control), count,
761 size, "mixer bytes")) {
762 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
763 count);
764 return -EINVAL;
765 }
766
767 for (i = 0; i < count; i++) {
768 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
769
770 /* validate kcontrol */
771 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
772 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
773 return -EINVAL;
774
775 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
776 if (sbe == NULL)
777 return -ENOMEM;
778
779 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
780 be->priv.size);
781
782 dev_dbg(tplg->dev,
783 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
784 be->hdr.name, be->hdr.access);
785
786 memset(&kc, 0, sizeof(kc));
787 kc.name = be->hdr.name;
788 kc.private_value = (long)sbe;
789 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
790 kc.access = be->hdr.access;
791
792 sbe->max = be->max;
793 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
794 sbe->dobj.ops = tplg->ops;
795 INIT_LIST_HEAD(&sbe->dobj.list);
796
797 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800798 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100799 if (err) {
800 soc_control_err(tplg, &be->hdr, be->hdr.name);
801 kfree(sbe);
802 continue;
803 }
804
805 /* pass control to driver for optional further init */
806 err = soc_tplg_init_kcontrol(tplg, &kc,
807 (struct snd_soc_tplg_ctl_hdr *)be);
808 if (err < 0) {
809 dev_err(tplg->dev, "ASoC: failed to init %s\n",
810 be->hdr.name);
811 kfree(sbe);
812 continue;
813 }
814
815 /* register control here */
816 err = soc_tplg_add_kcontrol(tplg, &kc,
817 &sbe->dobj.control.kcontrol);
818 if (err < 0) {
819 dev_err(tplg->dev, "ASoC: failed to add %s\n",
820 be->hdr.name);
821 kfree(sbe);
822 continue;
823 }
824
825 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
826 }
827 return 0;
828
829}
830
831static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
832 size_t size)
833{
834 struct snd_soc_tplg_mixer_control *mc;
835 struct soc_mixer_control *sm;
836 struct snd_kcontrol_new kc;
837 int i, err;
838
839 if (soc_tplg_check_elem_count(tplg,
840 sizeof(struct snd_soc_tplg_mixer_control),
841 count, size, "mixers")) {
842
843 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
844 count);
845 return -EINVAL;
846 }
847
848 for (i = 0; i < count; i++) {
849 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
850
851 /* validate kcontrol */
852 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
853 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
854 return -EINVAL;
855
856 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
857 if (sm == NULL)
858 return -ENOMEM;
859 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
860 mc->priv.size);
861
862 dev_dbg(tplg->dev,
863 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
864 mc->hdr.name, mc->hdr.access);
865
866 memset(&kc, 0, sizeof(kc));
867 kc.name = mc->hdr.name;
868 kc.private_value = (long)sm;
869 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
870 kc.access = mc->hdr.access;
871
872 /* we only support FL/FR channel mapping atm */
873 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
874 SNDRV_CHMAP_FL);
875 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
876 SNDRV_CHMAP_FR);
877 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
878 SNDRV_CHMAP_FL);
879 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
880 SNDRV_CHMAP_FR);
881
882 sm->max = mc->max;
883 sm->min = mc->min;
884 sm->invert = mc->invert;
885 sm->platform_max = mc->platform_max;
886 sm->dobj.index = tplg->index;
887 sm->dobj.ops = tplg->ops;
888 sm->dobj.type = SND_SOC_DOBJ_MIXER;
889 INIT_LIST_HEAD(&sm->dobj.list);
890
891 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800892 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100893 if (err) {
894 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
895 kfree(sm);
896 continue;
897 }
898
Bard liao3789deb2019-03-13 21:49:43 +0800899 /* create any TLV data */
900 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
901
Liam Girdwood8a978232015-05-29 19:06:14 +0100902 /* pass control to driver for optional further init */
903 err = soc_tplg_init_kcontrol(tplg, &kc,
904 (struct snd_soc_tplg_ctl_hdr *) mc);
905 if (err < 0) {
906 dev_err(tplg->dev, "ASoC: failed to init %s\n",
907 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +0800908 soc_tplg_free_tlv(tplg, &kc);
Liam Girdwood8a978232015-05-29 19:06:14 +0100909 kfree(sm);
910 continue;
911 }
912
Liam Girdwood8a978232015-05-29 19:06:14 +0100913 /* register control here */
914 err = soc_tplg_add_kcontrol(tplg, &kc,
915 &sm->dobj.control.kcontrol);
916 if (err < 0) {
917 dev_err(tplg->dev, "ASoC: failed to add %s\n",
918 mc->hdr.name);
919 soc_tplg_free_tlv(tplg, &kc);
920 kfree(sm);
921 continue;
922 }
923
924 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
925 }
926
927 return 0;
928}
929
930static int soc_tplg_denum_create_texts(struct soc_enum *se,
931 struct snd_soc_tplg_enum_control *ec)
932{
933 int i, ret;
934
935 se->dobj.control.dtexts =
Kees Cook6396bb22018-06-12 14:03:40 -0700936 kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100937 if (se->dobj.control.dtexts == NULL)
938 return -ENOMEM;
939
940 for (i = 0; i < ec->items; i++) {
941
942 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
943 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
944 ret = -EINVAL;
945 goto err;
946 }
947
948 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
949 if (!se->dobj.control.dtexts[i]) {
950 ret = -ENOMEM;
951 goto err;
952 }
953 }
954
Mousumi Janab6e38b22017-04-11 13:06:22 +0530955 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100956 return 0;
957
958err:
959 for (--i; i >= 0; i--)
960 kfree(se->dobj.control.dtexts[i]);
961 kfree(se->dobj.control.dtexts);
962 return ret;
963}
964
965static int soc_tplg_denum_create_values(struct soc_enum *se,
966 struct snd_soc_tplg_enum_control *ec)
967{
968 if (ec->items > sizeof(*ec->values))
969 return -EINVAL;
970
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200971 se->dobj.control.dvalues = kmemdup(ec->values,
972 ec->items * sizeof(u32),
973 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100974 if (!se->dobj.control.dvalues)
975 return -ENOMEM;
976
Liam Girdwood8a978232015-05-29 19:06:14 +0100977 return 0;
978}
979
980static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
981 size_t size)
982{
983 struct snd_soc_tplg_enum_control *ec;
984 struct soc_enum *se;
985 struct snd_kcontrol_new kc;
986 int i, ret, err;
987
988 if (soc_tplg_check_elem_count(tplg,
989 sizeof(struct snd_soc_tplg_enum_control),
990 count, size, "enums")) {
991
992 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
993 count);
994 return -EINVAL;
995 }
996
997 for (i = 0; i < count; i++) {
998 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
999 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1000 ec->priv.size);
1001
1002 /* validate kcontrol */
1003 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1004 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1005 return -EINVAL;
1006
1007 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1008 if (se == NULL)
1009 return -ENOMEM;
1010
1011 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1012 ec->hdr.name, ec->items);
1013
1014 memset(&kc, 0, sizeof(kc));
1015 kc.name = ec->hdr.name;
1016 kc.private_value = (long)se;
1017 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1018 kc.access = ec->hdr.access;
1019
1020 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1021 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1022 SNDRV_CHMAP_FL);
1023 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1024 SNDRV_CHMAP_FL);
1025
1026 se->items = ec->items;
1027 se->mask = ec->mask;
1028 se->dobj.index = tplg->index;
1029 se->dobj.type = SND_SOC_DOBJ_ENUM;
1030 se->dobj.ops = tplg->ops;
1031 INIT_LIST_HEAD(&se->dobj.list);
1032
1033 switch (ec->hdr.ops.info) {
1034 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1035 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1036 err = soc_tplg_denum_create_values(se, ec);
1037 if (err < 0) {
1038 dev_err(tplg->dev,
1039 "ASoC: could not create values for %s\n",
1040 ec->hdr.name);
1041 kfree(se);
1042 continue;
1043 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001044 /* fall through */
Liam Girdwood8a978232015-05-29 19:06:14 +01001045 case SND_SOC_TPLG_CTL_ENUM:
1046 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1047 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1048 err = soc_tplg_denum_create_texts(se, ec);
1049 if (err < 0) {
1050 dev_err(tplg->dev,
1051 "ASoC: could not create texts for %s\n",
1052 ec->hdr.name);
1053 kfree(se);
1054 continue;
1055 }
1056 break;
1057 default:
1058 dev_err(tplg->dev,
1059 "ASoC: invalid enum control type %d for %s\n",
1060 ec->hdr.ops.info, ec->hdr.name);
1061 kfree(se);
1062 continue;
1063 }
1064
1065 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001066 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001067 if (err) {
1068 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1069 kfree(se);
1070 continue;
1071 }
1072
1073 /* pass control to driver for optional further init */
1074 err = soc_tplg_init_kcontrol(tplg, &kc,
1075 (struct snd_soc_tplg_ctl_hdr *) ec);
1076 if (err < 0) {
1077 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1078 ec->hdr.name);
1079 kfree(se);
1080 continue;
1081 }
1082
1083 /* register control here */
1084 ret = soc_tplg_add_kcontrol(tplg,
1085 &kc, &se->dobj.control.kcontrol);
1086 if (ret < 0) {
1087 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1088 ec->hdr.name);
1089 kfree(se);
1090 continue;
1091 }
1092
1093 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1094 }
1095
1096 return 0;
1097}
1098
1099static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1100 struct snd_soc_tplg_hdr *hdr)
1101{
1102 struct snd_soc_tplg_ctl_hdr *control_hdr;
1103 int i;
1104
1105 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1106 tplg->pos += hdr->size + hdr->payload_size;
1107 return 0;
1108 }
1109
1110 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1111 soc_tplg_get_offset(tplg));
1112
1113 for (i = 0; i < hdr->count; i++) {
1114
1115 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1116
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001117 if (control_hdr->size != sizeof(*control_hdr)) {
1118 dev_err(tplg->dev, "ASoC: invalid control size\n");
1119 return -EINVAL;
1120 }
1121
Liam Girdwood8a978232015-05-29 19:06:14 +01001122 switch (control_hdr->ops.info) {
1123 case SND_SOC_TPLG_CTL_VOLSW:
1124 case SND_SOC_TPLG_CTL_STROBE:
1125 case SND_SOC_TPLG_CTL_VOLSW_SX:
1126 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1127 case SND_SOC_TPLG_CTL_RANGE:
1128 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1129 case SND_SOC_TPLG_DAPM_CTL_PIN:
1130 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1131 break;
1132 case SND_SOC_TPLG_CTL_ENUM:
1133 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1134 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1135 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1136 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1137 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1138 break;
1139 case SND_SOC_TPLG_CTL_BYTES:
1140 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1141 break;
1142 default:
1143 soc_bind_err(tplg, control_hdr, i);
1144 return -EINVAL;
1145 }
1146 }
1147
1148 return 0;
1149}
1150
Liam Girdwood503e79b2018-06-14 20:53:59 +01001151/* optionally pass new dynamic kcontrol to component driver. */
1152static int soc_tplg_add_route(struct soc_tplg *tplg,
1153 struct snd_soc_dapm_route *route)
1154{
1155 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1156 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1157 route);
1158
1159 return 0;
1160}
1161
Liam Girdwood8a978232015-05-29 19:06:14 +01001162static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1163 struct snd_soc_tplg_hdr *hdr)
1164{
1165 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001166 struct snd_soc_tplg_dapm_graph_elem *elem;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001167 struct snd_soc_dapm_route **routes;
1168 int count = hdr->count, i, j;
1169 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001170
1171 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1172 tplg->pos += hdr->size + hdr->payload_size;
1173 return 0;
1174 }
1175
1176 if (soc_tplg_check_elem_count(tplg,
1177 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1178 count, hdr->payload_size, "graph")) {
1179
1180 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1181 count);
1182 return -EINVAL;
1183 }
1184
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001185 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1186 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001187
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001188 /* allocate memory for pointer to array of dapm routes */
1189 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1190 GFP_KERNEL);
1191 if (!routes)
1192 return -ENOMEM;
1193
1194 /*
1195 * allocate memory for each dapm route in the array.
1196 * This needs to be done individually so that
1197 * each route can be freed when it is removed in remove_route().
1198 */
1199 for (i = 0; i < count; i++) {
1200 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1201 if (!routes[i]) {
1202 /* free previously allocated memory */
1203 for (j = 0; j < i; j++)
1204 kfree(routes[j]);
1205
1206 kfree(routes);
1207 return -ENOMEM;
1208 }
1209 }
1210
Liam Girdwood8a978232015-05-29 19:06:14 +01001211 for (i = 0; i < count; i++) {
1212 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1213 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1214
1215 /* validate routes */
1216 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001217 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1218 ret = -EINVAL;
1219 break;
1220 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001221 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001222 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1223 ret = -EINVAL;
1224 break;
1225 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001226 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001227 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1228 ret = -EINVAL;
1229 break;
1230 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001231
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001232 routes[i]->source = elem->source;
1233 routes[i]->sink = elem->sink;
1234
1235 /* set to NULL atm for tplg users */
1236 routes[i]->connected = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001237 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001238 routes[i]->control = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001239 else
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001240 routes[i]->control = elem->control;
Liam Girdwood8a978232015-05-29 19:06:14 +01001241
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001242 /* add route dobj to dobj_list */
1243 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1244 routes[i]->dobj.ops = tplg->ops;
1245 routes[i]->dobj.index = tplg->index;
1246 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1247
1248 soc_tplg_add_route(tplg, routes[i]);
Liam Girdwood503e79b2018-06-14 20:53:59 +01001249
Liam Girdwood8a978232015-05-29 19:06:14 +01001250 /* add route, but keep going if some fail */
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001251 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Liam Girdwood8a978232015-05-29 19:06:14 +01001252 }
1253
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001254 /* free memory allocated for all dapm routes in case of error */
1255 if (ret < 0)
1256 for (i = 0; i < count ; i++)
1257 kfree(routes[i]);
1258
1259 /*
1260 * free pointer to array of dapm routes as this is no longer needed.
1261 * The memory allocated for each dapm route will be freed
1262 * when it is removed in remove_route().
1263 */
1264 kfree(routes);
1265
1266 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001267}
1268
1269static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1270 struct soc_tplg *tplg, int num_kcontrols)
1271{
1272 struct snd_kcontrol_new *kc;
1273 struct soc_mixer_control *sm;
1274 struct snd_soc_tplg_mixer_control *mc;
1275 int i, err;
1276
Axel Lin4ca7deb2015-08-05 22:34:22 +08001277 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001278 if (kc == NULL)
1279 return NULL;
1280
1281 for (i = 0; i < num_kcontrols; i++) {
1282 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1283 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1284 if (sm == NULL)
1285 goto err;
1286
1287 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1288 mc->priv.size);
1289
1290 /* validate kcontrol */
1291 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1292 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1293 goto err_str;
1294
1295 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1296 mc->hdr.name, i);
1297
Liam Girdwood267e2c62018-03-27 12:04:04 +01001298 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1299 if (kc[i].name == NULL)
1300 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001301 kc[i].private_value = (long)sm;
1302 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1303 kc[i].access = mc->hdr.access;
1304
1305 /* we only support FL/FR channel mapping atm */
1306 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1307 SNDRV_CHMAP_FL);
1308 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1309 SNDRV_CHMAP_FR);
1310 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1311 SNDRV_CHMAP_FL);
1312 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1313 SNDRV_CHMAP_FR);
1314
1315 sm->max = mc->max;
1316 sm->min = mc->min;
1317 sm->invert = mc->invert;
1318 sm->platform_max = mc->platform_max;
1319 sm->dobj.index = tplg->index;
1320 INIT_LIST_HEAD(&sm->dobj.list);
1321
1322 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001323 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001324 if (err) {
1325 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1326 kfree(sm);
1327 continue;
1328 }
1329
Bard liao3789deb2019-03-13 21:49:43 +08001330 /* create any TLV data */
1331 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1332
Liam Girdwood8a978232015-05-29 19:06:14 +01001333 /* pass control to driver for optional further init */
1334 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1335 (struct snd_soc_tplg_ctl_hdr *)mc);
1336 if (err < 0) {
1337 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1338 mc->hdr.name);
Bard liao3789deb2019-03-13 21:49:43 +08001339 soc_tplg_free_tlv(tplg, &kc[i]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001340 kfree(sm);
1341 continue;
1342 }
1343 }
1344 return kc;
1345
1346err_str:
1347 kfree(sm);
1348err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001349 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001350 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001351 kfree(kc[i].name);
1352 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001353 kfree(kc);
1354 return NULL;
1355}
1356
1357static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001358 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001359{
1360 struct snd_kcontrol_new *kc;
1361 struct snd_soc_tplg_enum_control *ec;
1362 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001363 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001364
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001365 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001366 if (kc == NULL)
1367 return NULL;
1368
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001369 for (i = 0; i < num_kcontrols; i++) {
1370 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1371 /* validate kcontrol */
1372 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1373 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001374 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001375
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001376 se = kzalloc(sizeof(*se), GFP_KERNEL);
1377 if (se == NULL)
1378 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001379
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001380 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001381 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001382
Liam Girdwood267e2c62018-03-27 12:04:04 +01001383 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001384 if (kc[i].name == NULL) {
1385 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001386 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001387 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001388 kc[i].private_value = (long)se;
1389 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1390 kc[i].access = ec->hdr.access;
1391
1392 /* we only support FL/FR channel mapping atm */
1393 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1394 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1395 SNDRV_CHMAP_FL);
1396 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1397 SNDRV_CHMAP_FR);
1398
1399 se->items = ec->items;
1400 se->mask = ec->mask;
1401 se->dobj.index = tplg->index;
1402
1403 switch (ec->hdr.ops.info) {
1404 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1405 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1406 err = soc_tplg_denum_create_values(se, ec);
1407 if (err < 0) {
1408 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1409 ec->hdr.name);
1410 goto err_se;
1411 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001412 /* fall through */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001413 case SND_SOC_TPLG_CTL_ENUM:
1414 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1415 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1416 err = soc_tplg_denum_create_texts(se, ec);
1417 if (err < 0) {
1418 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1419 ec->hdr.name);
1420 goto err_se;
1421 }
1422 break;
1423 default:
1424 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1425 ec->hdr.ops.info, ec->hdr.name);
1426 goto err_se;
1427 }
1428
1429 /* map io handlers */
1430 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1431 if (err) {
1432 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1433 goto err_se;
1434 }
1435
1436 /* pass control to driver for optional further init */
1437 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1438 (struct snd_soc_tplg_ctl_hdr *)ec);
1439 if (err < 0) {
1440 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1441 ec->hdr.name);
1442 goto err_se;
1443 }
1444
1445 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1446 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001447 }
1448
1449 return kc;
1450
1451err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001452 for (; i >= 0; i--) {
1453 /* free values and texts */
1454 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001455 if (!se)
1456 continue;
1457
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001458 kfree(se->dobj.control.dvalues);
1459 for (j = 0; j < ec->items; j++)
1460 kfree(se->dobj.control.dtexts[j]);
Amadeusz Sławiński34db6a32019-01-25 14:06:44 -06001461 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +01001462
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001463 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001464 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001465 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001466err:
1467 kfree(kc);
1468
1469 return NULL;
1470}
1471
1472static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1473 struct soc_tplg *tplg, int count)
1474{
1475 struct snd_soc_tplg_bytes_control *be;
1476 struct soc_bytes_ext *sbe;
1477 struct snd_kcontrol_new *kc;
1478 int i, err;
1479
Axel Lin4ca7deb2015-08-05 22:34:22 +08001480 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001481 if (!kc)
1482 return NULL;
1483
1484 for (i = 0; i < count; i++) {
1485 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1486
1487 /* validate kcontrol */
1488 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1489 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1490 goto err;
1491
1492 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1493 if (sbe == NULL)
1494 goto err;
1495
1496 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1497 be->priv.size);
1498
1499 dev_dbg(tplg->dev,
1500 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1501 be->hdr.name, be->hdr.access);
1502
Liam Girdwood267e2c62018-03-27 12:04:04 +01001503 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001504 if (kc[i].name == NULL) {
1505 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001506 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001507 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001508 kc[i].private_value = (long)sbe;
1509 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1510 kc[i].access = be->hdr.access;
1511
1512 sbe->max = be->max;
1513 INIT_LIST_HEAD(&sbe->dobj.list);
1514
1515 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001516 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001517 if (err) {
1518 soc_control_err(tplg, &be->hdr, be->hdr.name);
1519 kfree(sbe);
1520 continue;
1521 }
1522
1523 /* pass control to driver for optional further init */
1524 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1525 (struct snd_soc_tplg_ctl_hdr *)be);
1526 if (err < 0) {
1527 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1528 be->hdr.name);
1529 kfree(sbe);
1530 continue;
1531 }
1532 }
1533
1534 return kc;
1535
1536err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001537 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001538 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001539 kfree(kc[i].name);
1540 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001541
1542 kfree(kc);
1543 return NULL;
1544}
1545
1546static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1547 struct snd_soc_tplg_dapm_widget *w)
1548{
1549 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1550 struct snd_soc_dapm_widget template, *widget;
1551 struct snd_soc_tplg_ctl_hdr *control_hdr;
1552 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001553 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001554 int ret = 0;
1555
1556 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1557 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1558 return -EINVAL;
1559 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1560 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1561 return -EINVAL;
1562
1563 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1564 w->name, w->id);
1565
1566 memset(&template, 0, sizeof(template));
1567
1568 /* map user to kernel widget ID */
1569 template.id = get_widget_id(w->id);
1570 if (template.id < 0)
1571 return template.id;
1572
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001573 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001574 template.name = kstrdup(w->name, GFP_KERNEL);
1575 if (!template.name)
1576 return -ENOMEM;
1577 template.sname = kstrdup(w->sname, GFP_KERNEL);
1578 if (!template.sname) {
1579 ret = -ENOMEM;
1580 goto err;
1581 }
1582 template.reg = w->reg;
1583 template.shift = w->shift;
1584 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001585 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001586 template.on_val = w->invert ? 0 : 1;
1587 template.off_val = w->invert ? 1 : 0;
1588 template.ignore_suspend = w->ignore_suspend;
1589 template.event_flags = w->event_flags;
1590 template.dobj.index = tplg->index;
1591
1592 tplg->pos +=
1593 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1594 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001595 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001596 template.num_kcontrols = 0;
1597 goto widget;
1598 }
1599
1600 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1601 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1602 w->name, w->num_kcontrols, control_hdr->type);
1603
1604 switch (control_hdr->ops.info) {
1605 case SND_SOC_TPLG_CTL_VOLSW:
1606 case SND_SOC_TPLG_CTL_STROBE:
1607 case SND_SOC_TPLG_CTL_VOLSW_SX:
1608 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1609 case SND_SOC_TPLG_CTL_RANGE:
1610 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001611 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001612 template.num_kcontrols = w->num_kcontrols;
1613 template.kcontrol_news =
1614 soc_tplg_dapm_widget_dmixer_create(tplg,
1615 template.num_kcontrols);
1616 if (!template.kcontrol_news) {
1617 ret = -ENOMEM;
1618 goto hdr_err;
1619 }
1620 break;
1621 case SND_SOC_TPLG_CTL_ENUM:
1622 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1623 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1624 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1625 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001626 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001627 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001628 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001629 soc_tplg_dapm_widget_denum_create(tplg,
1630 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001631 if (!template.kcontrol_news) {
1632 ret = -ENOMEM;
1633 goto hdr_err;
1634 }
1635 break;
1636 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001637 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001638 template.num_kcontrols = w->num_kcontrols;
1639 template.kcontrol_news =
1640 soc_tplg_dapm_widget_dbytes_create(tplg,
1641 template.num_kcontrols);
1642 if (!template.kcontrol_news) {
1643 ret = -ENOMEM;
1644 goto hdr_err;
1645 }
1646 break;
1647 default:
1648 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1649 control_hdr->ops.get, control_hdr->ops.put,
1650 control_hdr->ops.info);
1651 ret = -EINVAL;
1652 goto hdr_err;
1653 }
1654
1655widget:
1656 ret = soc_tplg_widget_load(tplg, &template, w);
1657 if (ret < 0)
1658 goto hdr_err;
1659
1660 /* card dapm mutex is held by the core if we are loading topology
1661 * data during sound card init. */
1662 if (card->instantiated)
1663 widget = snd_soc_dapm_new_control(dapm, &template);
1664 else
1665 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001666 if (IS_ERR(widget)) {
1667 ret = PTR_ERR(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001668 goto hdr_err;
1669 }
1670
1671 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001672 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001673 widget->dobj.ops = tplg->ops;
1674 widget->dobj.index = tplg->index;
1675 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001676
1677 ret = soc_tplg_widget_ready(tplg, widget, w);
1678 if (ret < 0)
1679 goto ready_err;
1680
Bard liao7620fe92019-01-25 14:06:45 -06001681 kfree(template.sname);
1682 kfree(template.name);
1683
Liam Girdwood8a978232015-05-29 19:06:14 +01001684 return 0;
1685
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001686ready_err:
1687 snd_soc_tplg_widget_remove(widget);
1688 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001689hdr_err:
1690 kfree(template.sname);
1691err:
1692 kfree(template.name);
1693 return ret;
1694}
1695
1696static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1697 struct snd_soc_tplg_hdr *hdr)
1698{
1699 struct snd_soc_tplg_dapm_widget *widget;
1700 int ret, count = hdr->count, i;
1701
1702 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1703 return 0;
1704
1705 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1706
1707 for (i = 0; i < count; i++) {
1708 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001709 if (widget->size != sizeof(*widget)) {
1710 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1711 return -EINVAL;
1712 }
1713
Liam Girdwood8a978232015-05-29 19:06:14 +01001714 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001715 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001716 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1717 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001718 return ret;
1719 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001720 }
1721
1722 return 0;
1723}
1724
1725static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1726{
1727 struct snd_soc_card *card = tplg->comp->card;
1728 int ret;
1729
1730 /* Card might not have been registered at this point.
1731 * If so, just return success.
1732 */
1733 if (!card || !card->instantiated) {
1734 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001735 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001736 return 0;
1737 }
1738
1739 ret = snd_soc_dapm_new_widgets(card);
1740 if (ret < 0)
1741 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1742 ret);
1743
1744 return 0;
1745}
1746
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001747static void set_stream_info(struct snd_soc_pcm_stream *stream,
1748 struct snd_soc_tplg_stream_caps *caps)
1749{
1750 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1751 stream->channels_min = caps->channels_min;
1752 stream->channels_max = caps->channels_max;
1753 stream->rates = caps->rates;
1754 stream->rate_min = caps->rate_min;
1755 stream->rate_max = caps->rate_max;
1756 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001757 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001758}
1759
Mengdong Lin0038be92016-07-26 14:32:37 +08001760static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1761 unsigned int flag_mask, unsigned int flags)
1762{
1763 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1764 dai_drv->symmetric_rates =
1765 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1766
1767 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1768 dai_drv->symmetric_channels =
1769 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1770 1 : 0;
1771
1772 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1773 dai_drv->symmetric_samplebits =
1774 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1775 1 : 0;
1776}
1777
Mengdong Lin64527e82016-01-15 16:13:28 +08001778static int soc_tplg_dai_create(struct soc_tplg *tplg,
1779 struct snd_soc_tplg_pcm *pcm)
1780{
1781 struct snd_soc_dai_driver *dai_drv;
1782 struct snd_soc_pcm_stream *stream;
1783 struct snd_soc_tplg_stream_caps *caps;
1784 int ret;
1785
1786 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1787 if (dai_drv == NULL)
1788 return -ENOMEM;
1789
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001790 if (strlen(pcm->dai_name))
1791 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001792 dai_drv->id = pcm->dai_id;
1793
1794 if (pcm->playback) {
1795 stream = &dai_drv->playback;
1796 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001797 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001798 }
1799
1800 if (pcm->capture) {
1801 stream = &dai_drv->capture;
1802 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001803 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001804 }
1805
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001806 if (pcm->compress)
1807 dai_drv->compress_new = snd_soc_new_compress;
1808
Mengdong Lin64527e82016-01-15 16:13:28 +08001809 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001810 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001811 if (ret < 0) {
1812 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Bard liao7b6f68a2019-03-05 23:57:52 +08001813 kfree(dai_drv->playback.stream_name);
1814 kfree(dai_drv->capture.stream_name);
1815 kfree(dai_drv->name);
Mengdong Lin64527e82016-01-15 16:13:28 +08001816 kfree(dai_drv);
1817 return ret;
1818 }
1819
1820 dai_drv->dobj.index = tplg->index;
1821 dai_drv->dobj.ops = tplg->ops;
1822 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1823 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1824
1825 /* register the DAI to the component */
1826 return snd_soc_register_dai(tplg->comp, dai_drv);
1827}
1828
Mengdong Lin717a8e72016-11-03 01:03:34 +08001829static void set_link_flags(struct snd_soc_dai_link *link,
1830 unsigned int flag_mask, unsigned int flags)
1831{
1832 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1833 link->symmetric_rates =
1834 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1835
1836 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1837 link->symmetric_channels =
1838 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1839 1 : 0;
1840
1841 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1842 link->symmetric_samplebits =
1843 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1844 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001845
1846 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1847 link->ignore_suspend =
1848 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1849 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001850}
1851
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001852/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001853static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001854 struct snd_soc_tplg_pcm *pcm)
1855{
1856 struct snd_soc_dai_link *link;
1857 int ret;
1858
1859 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1860 if (link == NULL)
1861 return -ENOMEM;
1862
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001863 if (strlen(pcm->pcm_name)) {
1864 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1865 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1866 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001867 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001868
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001869 if (strlen(pcm->dai_name))
1870 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1871
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001872 link->codec_name = "snd-soc-dummy";
1873 link->codec_dai_name = "snd-soc-dummy-dai";
1874
1875 /* enable DPCM */
1876 link->dynamic = 1;
1877 link->dpcm_playback = pcm->playback;
1878 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001879 if (pcm->flag_mask)
1880 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001881
Mengdong Linacfc7d42016-01-15 16:13:37 +08001882 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001883 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001884 if (ret < 0) {
1885 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1886 kfree(link);
1887 return ret;
1888 }
1889
1890 link->dobj.index = tplg->index;
1891 link->dobj.ops = tplg->ops;
1892 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1893 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1894
1895 snd_soc_add_dai_link(tplg->comp->card, link);
1896 return 0;
1897}
1898
1899/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001900static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1901 struct snd_soc_tplg_pcm *pcm)
1902{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001903 int ret;
1904
1905 ret = soc_tplg_dai_create(tplg, pcm);
1906 if (ret < 0)
1907 return ret;
1908
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001909 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001910}
1911
Mengdong Lin55726dc2016-11-03 01:00:16 +08001912/* copy stream caps from the old version 4 of source */
1913static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1914 struct snd_soc_tplg_stream_caps_v4 *src)
1915{
1916 dest->size = sizeof(*dest);
1917 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1918 dest->formats = src->formats;
1919 dest->rates = src->rates;
1920 dest->rate_min = src->rate_min;
1921 dest->rate_max = src->rate_max;
1922 dest->channels_min = src->channels_min;
1923 dest->channels_max = src->channels_max;
1924 dest->periods_min = src->periods_min;
1925 dest->periods_max = src->periods_max;
1926 dest->period_size_min = src->period_size_min;
1927 dest->period_size_max = src->period_size_max;
1928 dest->buffer_size_min = src->buffer_size_min;
1929 dest->buffer_size_max = src->buffer_size_max;
1930}
1931
1932/**
1933 * pcm_new_ver - Create the new version of PCM from the old version.
1934 * @tplg: topology context
1935 * @src: older version of pcm as a source
1936 * @pcm: latest version of pcm created from the source
1937 *
1938 * Support from vesion 4. User should free the returned pcm manually.
1939 */
1940static int pcm_new_ver(struct soc_tplg *tplg,
1941 struct snd_soc_tplg_pcm *src,
1942 struct snd_soc_tplg_pcm **pcm)
1943{
1944 struct snd_soc_tplg_pcm *dest;
1945 struct snd_soc_tplg_pcm_v4 *src_v4;
1946 int i;
1947
1948 *pcm = NULL;
1949
1950 if (src->size != sizeof(*src_v4)) {
1951 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1952 return -EINVAL;
1953 }
1954
1955 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1956 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1957 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1958 if (!dest)
1959 return -ENOMEM;
1960
1961 dest->size = sizeof(*dest); /* size of latest abi version */
1962 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1963 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1964 dest->pcm_id = src_v4->pcm_id;
1965 dest->dai_id = src_v4->dai_id;
1966 dest->playback = src_v4->playback;
1967 dest->capture = src_v4->capture;
1968 dest->compress = src_v4->compress;
1969 dest->num_streams = src_v4->num_streams;
1970 for (i = 0; i < dest->num_streams; i++)
1971 memcpy(&dest->stream[i], &src_v4->stream[i],
1972 sizeof(struct snd_soc_tplg_stream));
1973
1974 for (i = 0; i < 2; i++)
1975 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1976
1977 *pcm = dest;
1978 return 0;
1979}
1980
Mengdong Lin64527e82016-01-15 16:13:28 +08001981static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001982 struct snd_soc_tplg_hdr *hdr)
1983{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001984 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001985 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301986 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001987 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001988
1989 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1990 return 0;
1991
Mengdong Lin55726dc2016-11-03 01:00:16 +08001992 /* check the element size and count */
1993 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1994 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1995 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1996 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1997 pcm->size);
1998 return -EINVAL;
1999 }
2000
Liam Girdwood8a978232015-05-29 19:06:14 +01002001 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08002002 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01002003 hdr->payload_size, "PCM DAI")) {
2004 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2005 count);
2006 return -EINVAL;
2007 }
2008
Mengdong Lin64527e82016-01-15 16:13:28 +08002009 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08002010 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
2011
2012 /* check ABI version by size, create a new version of pcm
2013 * if abi not match.
2014 */
2015 if (pcm->size == sizeof(*pcm)) {
2016 abi_match = true;
2017 _pcm = pcm;
2018 } else {
2019 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05302020 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002021 }
2022
Mengdong Lin55726dc2016-11-03 01:00:16 +08002023 /* create the FE DAIs and DAI links */
2024 soc_tplg_pcm_create(tplg, _pcm);
2025
Mengdong Lin717a8e72016-11-03 01:03:34 +08002026 /* offset by version-specific struct size and
2027 * real priv data size
2028 */
2029 tplg->pos += pcm->size + _pcm->priv.size;
2030
Mengdong Lin55726dc2016-11-03 01:00:16 +08002031 if (!abi_match)
2032 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08002033 }
2034
Liam Girdwood8a978232015-05-29 19:06:14 +01002035 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002036
Liam Girdwood8a978232015-05-29 19:06:14 +01002037 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002038}
2039
Mengdong Lin593d9e52016-11-03 01:04:27 +08002040/**
2041 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002042 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002043 * @cfg: physical link configs.
2044 *
2045 * Topology context contains a list of supported HW formats (configs) and
2046 * a default format ID for the physical link. This function will use this
2047 * default ID to choose the HW format to set the link's DAI format for init.
2048 */
2049static void set_link_hw_format(struct snd_soc_dai_link *link,
2050 struct snd_soc_tplg_link_config *cfg)
2051{
2052 struct snd_soc_tplg_hw_config *hw_config;
2053 unsigned char bclk_master, fsync_master;
2054 unsigned char invert_bclk, invert_fsync;
2055 int i;
2056
2057 for (i = 0; i < cfg->num_hw_configs; i++) {
2058 hw_config = &cfg->hw_config[i];
2059 if (hw_config->id != cfg->default_hw_config_id)
2060 continue;
2061
2062 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
2063
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002064 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002065 switch (hw_config->clock_gated) {
2066 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002067 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002068 break;
2069
2070 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002071 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002072 break;
2073
2074 default:
2075 /* ignore the value */
2076 break;
2077 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002078
Mengdong Lin593d9e52016-11-03 01:04:27 +08002079 /* clock signal polarity */
2080 invert_bclk = hw_config->invert_bclk;
2081 invert_fsync = hw_config->invert_fsync;
2082 if (!invert_bclk && !invert_fsync)
2083 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2084 else if (!invert_bclk && invert_fsync)
2085 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2086 else if (invert_bclk && !invert_fsync)
2087 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2088 else
2089 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2090
2091 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002092 bclk_master = (hw_config->bclk_master ==
2093 SND_SOC_TPLG_BCLK_CM);
2094 fsync_master = (hw_config->fsync_master ==
2095 SND_SOC_TPLG_FSYNC_CM);
2096 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002097 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002098 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002099 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2100 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002101 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2102 else
2103 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2104 }
2105}
2106
2107/**
2108 * link_new_ver - Create a new physical link config from the old
2109 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002110 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002111 * @src: old version of phyical link config as a source
2112 * @link: latest version of physical link config created from the source
2113 *
2114 * Support from vesion 4. User need free the returned link config manually.
2115 */
2116static int link_new_ver(struct soc_tplg *tplg,
2117 struct snd_soc_tplg_link_config *src,
2118 struct snd_soc_tplg_link_config **link)
2119{
2120 struct snd_soc_tplg_link_config *dest;
2121 struct snd_soc_tplg_link_config_v4 *src_v4;
2122 int i;
2123
2124 *link = NULL;
2125
2126 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2127 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2128 return -EINVAL;
2129 }
2130
2131 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2132
2133 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2134 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2135 if (!dest)
2136 return -ENOMEM;
2137
2138 dest->size = sizeof(*dest);
2139 dest->id = src_v4->id;
2140 dest->num_streams = src_v4->num_streams;
2141 for (i = 0; i < dest->num_streams; i++)
2142 memcpy(&dest->stream[i], &src_v4->stream[i],
2143 sizeof(struct snd_soc_tplg_stream));
2144
2145 *link = dest;
2146 return 0;
2147}
2148
2149/* Find and configure an existing physical DAI link */
2150static int soc_tplg_link_config(struct soc_tplg *tplg,
2151 struct snd_soc_tplg_link_config *cfg)
2152{
2153 struct snd_soc_dai_link *link;
2154 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002155 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002156 int ret;
2157
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002158 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2159 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2160 return -EINVAL;
2161 else if (len)
2162 name = cfg->name;
2163 else
2164 name = NULL;
2165
2166 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2167 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2168 return -EINVAL;
2169 else if (len)
2170 stream_name = cfg->stream_name;
2171 else
2172 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002173
2174 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2175 name, stream_name);
2176 if (!link) {
2177 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2178 name, cfg->id);
2179 return -EINVAL;
2180 }
2181
2182 /* hw format */
2183 if (cfg->num_hw_configs)
2184 set_link_hw_format(link, cfg);
2185
2186 /* flags */
2187 if (cfg->flag_mask)
2188 set_link_flags(link, cfg->flag_mask, cfg->flags);
2189
2190 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002191 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002192 if (ret < 0) {
2193 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2194 return ret;
2195 }
2196
Bard liaoadfebb52019-02-01 11:07:40 -06002197 /* for unloading it in snd_soc_tplg_component_remove */
2198 link->dobj.index = tplg->index;
2199 link->dobj.ops = tplg->ops;
2200 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2201 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2202
Mengdong Lin593d9e52016-11-03 01:04:27 +08002203 return 0;
2204}
2205
2206
2207/* Load physical link config elements from the topology context */
2208static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2209 struct snd_soc_tplg_hdr *hdr)
2210{
2211 struct snd_soc_tplg_link_config *link, *_link;
2212 int count = hdr->count;
2213 int i, ret;
2214 bool abi_match;
2215
2216 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2217 tplg->pos += hdr->size + hdr->payload_size;
2218 return 0;
2219 };
2220
2221 /* check the element size and count */
2222 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2223 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2224 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2225 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2226 link->size);
2227 return -EINVAL;
2228 }
2229
2230 if (soc_tplg_check_elem_count(tplg,
2231 link->size, count,
2232 hdr->payload_size, "physical link config")) {
2233 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2234 count);
2235 return -EINVAL;
2236 }
2237
2238 /* config physical DAI links */
2239 for (i = 0; i < count; i++) {
2240 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2241 if (link->size == sizeof(*link)) {
2242 abi_match = true;
2243 _link = link;
2244 } else {
2245 abi_match = false;
2246 ret = link_new_ver(tplg, link, &_link);
2247 if (ret < 0)
2248 return ret;
2249 }
2250
2251 ret = soc_tplg_link_config(tplg, _link);
2252 if (ret < 0)
2253 return ret;
2254
2255 /* offset by version-specific struct size and
2256 * real priv data size
2257 */
2258 tplg->pos += link->size + _link->priv.size;
2259
2260 if (!abi_match)
2261 kfree(_link); /* free the duplicated one */
2262 }
2263
2264 return 0;
2265}
2266
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002267/**
2268 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002269 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002270 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002271 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002272 * The physical dai should already be registered by the platform driver.
2273 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002274 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002275static int soc_tplg_dai_config(struct soc_tplg *tplg,
2276 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002277{
2278 struct snd_soc_dai_link_component dai_component = {0};
2279 struct snd_soc_dai *dai;
2280 struct snd_soc_dai_driver *dai_drv;
2281 struct snd_soc_pcm_stream *stream;
2282 struct snd_soc_tplg_stream_caps *caps;
2283 int ret;
2284
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002285 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002286 dai = snd_soc_find_dai(&dai_component);
2287 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002288 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2289 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002290 return -EINVAL;
2291 }
2292
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002293 if (d->dai_id != dai->id) {
2294 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2295 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002296 return -EINVAL;
2297 }
2298
2299 dai_drv = dai->driver;
2300 if (!dai_drv)
2301 return -EINVAL;
2302
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002303 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002304 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002305 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002306 set_stream_info(stream, caps);
2307 }
2308
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002309 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002310 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002311 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002312 set_stream_info(stream, caps);
2313 }
2314
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002315 if (d->flag_mask)
2316 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002317
2318 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002319 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002320 if (ret < 0) {
2321 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2322 return ret;
2323 }
2324
2325 return 0;
2326}
2327
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002328/* load physical DAI elements */
2329static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2330 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002331{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002332 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002333 int count = hdr->count;
2334 int i;
2335
2336 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2337 return 0;
2338
2339 /* config the existing BE DAIs */
2340 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002341 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2342 if (dai->size != sizeof(*dai)) {
2343 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002344 return -EINVAL;
2345 }
2346
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002347 soc_tplg_dai_config(tplg, dai);
2348 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002349 }
2350
2351 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2352 return 0;
2353}
2354
Mengdong Lin583958f2016-10-11 14:36:42 +08002355/**
2356 * manifest_new_ver - Create a new version of manifest from the old version
2357 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002358 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002359 * @src: old version of manifest as a source
2360 * @manifest: latest version of manifest created from the source
2361 *
2362 * Support from vesion 4. Users need free the returned manifest manually.
2363 */
2364static int manifest_new_ver(struct soc_tplg *tplg,
2365 struct snd_soc_tplg_manifest *src,
2366 struct snd_soc_tplg_manifest **manifest)
2367{
2368 struct snd_soc_tplg_manifest *dest;
2369 struct snd_soc_tplg_manifest_v4 *src_v4;
2370
2371 *manifest = NULL;
2372
2373 if (src->size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002374 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2375 src->size);
2376 if (src->size)
2377 return -EINVAL;
2378 src->size = sizeof(*src_v4);
Mengdong Lin583958f2016-10-11 14:36:42 +08002379 }
2380
2381 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2382
2383 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2384 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2385 if (!dest)
2386 return -ENOMEM;
2387
2388 dest->size = sizeof(*dest); /* size of latest abi version */
2389 dest->control_elems = src_v4->control_elems;
2390 dest->widget_elems = src_v4->widget_elems;
2391 dest->graph_elems = src_v4->graph_elems;
2392 dest->pcm_elems = src_v4->pcm_elems;
2393 dest->dai_link_elems = src_v4->dai_link_elems;
2394 dest->priv.size = src_v4->priv.size;
2395 if (dest->priv.size)
2396 memcpy(dest->priv.data, src_v4->priv.data,
2397 src_v4->priv.size);
2398
2399 *manifest = dest;
2400 return 0;
2401}
Mengdong Lin0038be92016-07-26 14:32:37 +08002402
Liam Girdwood8a978232015-05-29 19:06:14 +01002403static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002404 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002405{
Mengdong Lin583958f2016-10-11 14:36:42 +08002406 struct snd_soc_tplg_manifest *manifest, *_manifest;
2407 bool abi_match;
2408 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002409
2410 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2411 return 0;
2412
2413 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002414
2415 /* check ABI version by size, create a new manifest if abi not match */
2416 if (manifest->size == sizeof(*manifest)) {
2417 abi_match = true;
2418 _manifest = manifest;
2419 } else {
2420 abi_match = false;
2421 err = manifest_new_ver(tplg, manifest, &_manifest);
2422 if (err < 0)
2423 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002424 }
2425
Mengdong Lin583958f2016-10-11 14:36:42 +08002426 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002427 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002428 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002429
Mengdong Lin583958f2016-10-11 14:36:42 +08002430 if (!abi_match) /* free the duplicated one */
2431 kfree(_manifest);
2432
Liam Girdwood8a978232015-05-29 19:06:14 +01002433 return 0;
2434}
2435
2436/* validate header magic, size and type */
2437static int soc_valid_header(struct soc_tplg *tplg,
2438 struct snd_soc_tplg_hdr *hdr)
2439{
2440 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2441 return 0;
2442
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002443 if (hdr->size != sizeof(*hdr)) {
2444 dev_err(tplg->dev,
2445 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2446 hdr->type, soc_tplg_get_hdr_offset(tplg),
2447 tplg->fw->size);
2448 return -EINVAL;
2449 }
2450
Liam Girdwood8a978232015-05-29 19:06:14 +01002451 /* big endian firmware objects not supported atm */
2452 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2453 dev_err(tplg->dev,
2454 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2455 tplg->pass, hdr->magic,
2456 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2457 return -EINVAL;
2458 }
2459
2460 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2461 dev_err(tplg->dev,
2462 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2463 tplg->pass, hdr->magic,
2464 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2465 return -EINVAL;
2466 }
2467
Mengdong Lin288b8da2016-11-03 01:03:17 +08002468 /* Support ABI from version 4 */
2469 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2470 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002471 dev_err(tplg->dev,
2472 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2473 tplg->pass, hdr->abi,
2474 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2475 tplg->fw->size);
2476 return -EINVAL;
2477 }
2478
2479 if (hdr->payload_size == 0) {
2480 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2481 soc_tplg_get_hdr_offset(tplg));
2482 return -EINVAL;
2483 }
2484
2485 if (tplg->pass == hdr->type)
2486 dev_dbg(tplg->dev,
2487 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2488 hdr->payload_size, hdr->type, hdr->version,
2489 hdr->vendor_type, tplg->pass);
2490
2491 return 1;
2492}
2493
2494/* check header type and call appropriate handler */
2495static int soc_tplg_load_header(struct soc_tplg *tplg,
2496 struct snd_soc_tplg_hdr *hdr)
2497{
2498 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2499
2500 /* check for matching ID */
2501 if (hdr->index != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002502 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002503 return 0;
2504
2505 tplg->index = hdr->index;
2506
2507 switch (hdr->type) {
2508 case SND_SOC_TPLG_TYPE_MIXER:
2509 case SND_SOC_TPLG_TYPE_ENUM:
2510 case SND_SOC_TPLG_TYPE_BYTES:
2511 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2512 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2513 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2514 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2515 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2516 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002517 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002518 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002519 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002520 case SND_SOC_TPLG_TYPE_DAI_LINK:
2521 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2522 /* physical link configurations */
2523 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002524 case SND_SOC_TPLG_TYPE_MANIFEST:
2525 return soc_tplg_manifest_load(tplg, hdr);
2526 default:
2527 /* bespoke vendor data object */
2528 return soc_tplg_vendor_load(tplg, hdr);
2529 }
2530
2531 return 0;
2532}
2533
2534/* process the topology file headers */
2535static int soc_tplg_process_headers(struct soc_tplg *tplg)
2536{
2537 struct snd_soc_tplg_hdr *hdr;
2538 int ret;
2539
2540 tplg->pass = SOC_TPLG_PASS_START;
2541
2542 /* process the header types from start to end */
2543 while (tplg->pass <= SOC_TPLG_PASS_END) {
2544
2545 tplg->hdr_pos = tplg->fw->data;
2546 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2547
2548 while (!soc_tplg_is_eof(tplg)) {
2549
2550 /* make sure header is valid before loading */
2551 ret = soc_valid_header(tplg, hdr);
2552 if (ret < 0)
2553 return ret;
2554 else if (ret == 0)
2555 break;
2556
2557 /* load the header object */
2558 ret = soc_tplg_load_header(tplg, hdr);
2559 if (ret < 0)
2560 return ret;
2561
2562 /* goto next header */
2563 tplg->hdr_pos += hdr->payload_size +
2564 sizeof(struct snd_soc_tplg_hdr);
2565 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2566 }
2567
2568 /* next data type pass */
2569 tplg->pass++;
2570 }
2571
2572 /* signal DAPM we are complete */
2573 ret = soc_tplg_dapm_complete(tplg);
2574 if (ret < 0)
2575 dev_err(tplg->dev,
2576 "ASoC: failed to initialise DAPM from Firmware\n");
2577
2578 return ret;
2579}
2580
2581static int soc_tplg_load(struct soc_tplg *tplg)
2582{
2583 int ret;
2584
2585 ret = soc_tplg_process_headers(tplg);
2586 if (ret == 0)
2587 soc_tplg_complete(tplg);
2588
2589 return ret;
2590}
2591
2592/* load audio component topology from "firmware" file */
2593int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2594 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2595{
2596 struct soc_tplg tplg;
Bard liao304017d2019-02-17 21:23:47 +08002597 int ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002598
2599 /* setup parsing context */
2600 memset(&tplg, 0, sizeof(tplg));
2601 tplg.fw = fw;
2602 tplg.dev = comp->dev;
2603 tplg.comp = comp;
2604 tplg.ops = ops;
2605 tplg.req_index = id;
2606 tplg.io_ops = ops->io_ops;
2607 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002608 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2609 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002610
Bard liao304017d2019-02-17 21:23:47 +08002611 ret = soc_tplg_load(&tplg);
2612 /* free the created components if fail to load topology */
2613 if (ret)
2614 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2615
2616 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01002617}
2618EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2619
2620/* remove this dynamic widget */
2621void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2622{
2623 /* make sure we are a widget */
2624 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2625 return;
2626
2627 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2628}
2629EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2630
2631/* remove all dynamic widgets from this DAPM context */
2632void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2633 u32 index)
2634{
2635 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002636
2637 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2638
2639 /* make sure we are a widget with correct context */
2640 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2641 continue;
2642
2643 /* match ID */
2644 if (w->dobj.index != index &&
2645 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2646 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002647 /* check and free and dynamic widget kcontrols */
2648 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002649 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002650 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002651 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002652}
2653EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2654
2655/* remove dynamic controls from the component driver */
2656int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2657{
2658 struct snd_soc_dobj *dobj, *next_dobj;
2659 int pass = SOC_TPLG_PASS_END;
2660
2661 /* process the header types from end to start */
2662 while (pass >= SOC_TPLG_PASS_START) {
2663
2664 /* remove mixer controls */
2665 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2666 list) {
2667
2668 /* match index */
2669 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002670 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002671 continue;
2672
2673 switch (dobj->type) {
2674 case SND_SOC_DOBJ_MIXER:
2675 remove_mixer(comp, dobj, pass);
2676 break;
2677 case SND_SOC_DOBJ_ENUM:
2678 remove_enum(comp, dobj, pass);
2679 break;
2680 case SND_SOC_DOBJ_BYTES:
2681 remove_bytes(comp, dobj, pass);
2682 break;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06002683 case SND_SOC_DOBJ_GRAPH:
2684 remove_route(comp, dobj, pass);
2685 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002686 case SND_SOC_DOBJ_WIDGET:
2687 remove_widget(comp, dobj, pass);
2688 break;
2689 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002690 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002691 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002692 case SND_SOC_DOBJ_DAI_LINK:
2693 remove_link(comp, dobj, pass);
2694 break;
Bard liaoadfebb52019-02-01 11:07:40 -06002695 case SND_SOC_DOBJ_BACKEND_LINK:
2696 /*
2697 * call link_unload ops if extra
2698 * deinitialization is needed.
2699 */
2700 remove_backend_link(comp, dobj, pass);
2701 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002702 default:
2703 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2704 dobj->type);
2705 break;
2706 }
2707 }
2708 pass--;
2709 }
2710
2711 /* let caller know if FW can be freed when no objects are left */
2712 return !list_empty(&comp->dobj_list);
2713}
2714EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);