blob: 246d2a2d43c8680915d963479d8e6103bb40be14 [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);
526
Liam Girdwood8a978232015-05-29 19:06:14 +0100527 if (pass != SOC_TPLG_PASS_PCM_DAI)
528 return;
529
Mengdong Lin64527e82016-01-15 16:13:28 +0800530 if (dobj->ops && dobj->ops->dai_unload)
531 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100532
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800533 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100534 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800535 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100536}
537
Mengdong Linacfc7d42016-01-15 16:13:37 +0800538/* remove link configurations */
539static void remove_link(struct snd_soc_component *comp,
540 struct snd_soc_dobj *dobj, int pass)
541{
542 struct snd_soc_dai_link *link =
543 container_of(dobj, struct snd_soc_dai_link, dobj);
544
545 if (pass != SOC_TPLG_PASS_PCM_DAI)
546 return;
547
548 if (dobj->ops && dobj->ops->link_unload)
549 dobj->ops->link_unload(comp, dobj);
550
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800551 kfree(link->name);
552 kfree(link->stream_name);
553 kfree(link->cpu_dai_name);
554
Mengdong Linacfc7d42016-01-15 16:13:37 +0800555 list_del(&dobj->list);
556 snd_soc_remove_dai_link(comp->card, link);
557 kfree(link);
558}
559
Bard liaoadfebb52019-02-01 11:07:40 -0600560/* unload dai link */
561static void remove_backend_link(struct snd_soc_component *comp,
562 struct snd_soc_dobj *dobj, int pass)
563{
564 if (pass != SOC_TPLG_PASS_LINK)
565 return;
566
567 if (dobj->ops && dobj->ops->link_unload)
568 dobj->ops->link_unload(comp, dobj);
569
570 /*
571 * We don't free the link here as what remove_link() do since BE
572 * links are not allocated by topology.
573 * We however need to reset the dobj type to its initial values
574 */
575 dobj->type = SND_SOC_DOBJ_NONE;
576 list_del(&dobj->list);
577}
578
Liam Girdwood8a978232015-05-29 19:06:14 +0100579/* bind a kcontrol to it's IO handlers */
580static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
581 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800582 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100583{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800584 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800585 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800586 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100587
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800588 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
589 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
590 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
591 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
592 struct soc_bytes_ext *sbe;
593 struct snd_soc_tplg_bytes_control *be;
594
595 sbe = (struct soc_bytes_ext *)k->private_value;
596 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
597
598 /* TLV bytes controls need standard kcontrol info handler,
599 * TLV callback and extended put/get handlers.
600 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530601 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800602 k->tlv.c = snd_soc_bytes_tlv_callback;
603
604 ext_ops = tplg->bytes_ext_ops;
605 num_ops = tplg->bytes_ext_ops_count;
606 for (i = 0; i < num_ops; i++) {
607 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
608 sbe->put = ext_ops[i].put;
609 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
610 sbe->get = ext_ops[i].get;
611 }
612
613 if (sbe->put && sbe->get)
614 return 0;
615 else
616 return -EINVAL;
617 }
618
Mengdong Lin88a17d82015-08-18 18:11:51 +0800619 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800620 ops = tplg->io_ops;
621 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100622 for (i = 0; i < num_ops; i++) {
623
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800624 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100625 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800626 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100627 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800628 if (k->info == NULL && ops[i].id == hdr->ops.info)
629 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100630 }
631
Mengdong Lin88a17d82015-08-18 18:11:51 +0800632 /* vendor specific handlers found ? */
633 if (k->put && k->get && k->info)
634 return 0;
635
636 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800637 ops = io_ops;
638 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800639 for (i = 0; i < num_ops; i++) {
640
641 if (k->put == NULL && ops[i].id == hdr->ops.put)
642 k->put = ops[i].put;
643 if (k->get == NULL && ops[i].id == hdr->ops.get)
644 k->get = ops[i].get;
645 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100646 k->info = ops[i].info;
647 }
648
649 /* standard handlers found ? */
650 if (k->put && k->get && k->info)
651 return 0;
652
Liam Girdwood8a978232015-05-29 19:06:14 +0100653 /* nothing to bind */
654 return -EINVAL;
655}
656
657/* bind a widgets to it's evnt handlers */
658int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
659 const struct snd_soc_tplg_widget_events *events,
660 int num_events, u16 event_type)
661{
662 int i;
663
664 w->event = NULL;
665
666 for (i = 0; i < num_events; i++) {
667 if (event_type == events[i].type) {
668
669 /* found - so assign event */
670 w->event = events[i].event_handler;
671 return 0;
672 }
673 }
674
675 /* not found */
676 return -EINVAL;
677}
678EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
679
680/* optionally pass new dynamic kcontrol to component driver. */
681static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
682 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
683{
684 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwoodc60b6132018-06-14 20:50:37 +0100685 return tplg->ops->control_load(tplg->comp, tplg->index, k,
686 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100687
688 return 0;
689}
690
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100691
692static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
693 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100694{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100695 unsigned int item_len = 2 * sizeof(unsigned int);
696 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100697
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100698 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
699 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100700 return -ENOMEM;
701
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100702 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
703 p[1] = item_len;
704 p[2] = scale->min;
705 p[3] = (scale->step & TLV_DB_SCALE_MASK)
706 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100707
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100708 kc->tlv.p = (void *)p;
709 return 0;
710}
711
712static int soc_tplg_create_tlv(struct soc_tplg *tplg,
713 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
714{
715 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
716
717 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
718 return 0;
719
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800720 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100721 tplg_tlv = &tc->tlv;
722 switch (tplg_tlv->type) {
723 case SNDRV_CTL_TLVT_DB_SCALE:
724 return soc_tplg_create_tlv_db_scale(tplg, kc,
725 &tplg_tlv->scale);
726
727 /* TODO: add support for other TLV types */
728 default:
729 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
730 tplg_tlv->type);
731 return -EINVAL;
732 }
733 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100734
735 return 0;
736}
737
738static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
739 struct snd_kcontrol_new *kc)
740{
741 kfree(kc->tlv.p);
742}
743
744static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
745 size_t size)
746{
747 struct snd_soc_tplg_bytes_control *be;
748 struct soc_bytes_ext *sbe;
749 struct snd_kcontrol_new kc;
750 int i, err;
751
752 if (soc_tplg_check_elem_count(tplg,
753 sizeof(struct snd_soc_tplg_bytes_control), count,
754 size, "mixer bytes")) {
755 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
756 count);
757 return -EINVAL;
758 }
759
760 for (i = 0; i < count; i++) {
761 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
762
763 /* validate kcontrol */
764 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
765 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
766 return -EINVAL;
767
768 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
769 if (sbe == NULL)
770 return -ENOMEM;
771
772 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
773 be->priv.size);
774
775 dev_dbg(tplg->dev,
776 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
777 be->hdr.name, be->hdr.access);
778
779 memset(&kc, 0, sizeof(kc));
780 kc.name = be->hdr.name;
781 kc.private_value = (long)sbe;
782 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
783 kc.access = be->hdr.access;
784
785 sbe->max = be->max;
786 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
787 sbe->dobj.ops = tplg->ops;
788 INIT_LIST_HEAD(&sbe->dobj.list);
789
790 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800791 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100792 if (err) {
793 soc_control_err(tplg, &be->hdr, be->hdr.name);
794 kfree(sbe);
795 continue;
796 }
797
798 /* pass control to driver for optional further init */
799 err = soc_tplg_init_kcontrol(tplg, &kc,
800 (struct snd_soc_tplg_ctl_hdr *)be);
801 if (err < 0) {
802 dev_err(tplg->dev, "ASoC: failed to init %s\n",
803 be->hdr.name);
804 kfree(sbe);
805 continue;
806 }
807
808 /* register control here */
809 err = soc_tplg_add_kcontrol(tplg, &kc,
810 &sbe->dobj.control.kcontrol);
811 if (err < 0) {
812 dev_err(tplg->dev, "ASoC: failed to add %s\n",
813 be->hdr.name);
814 kfree(sbe);
815 continue;
816 }
817
818 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
819 }
820 return 0;
821
822}
823
824static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
825 size_t size)
826{
827 struct snd_soc_tplg_mixer_control *mc;
828 struct soc_mixer_control *sm;
829 struct snd_kcontrol_new kc;
830 int i, err;
831
832 if (soc_tplg_check_elem_count(tplg,
833 sizeof(struct snd_soc_tplg_mixer_control),
834 count, size, "mixers")) {
835
836 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
837 count);
838 return -EINVAL;
839 }
840
841 for (i = 0; i < count; i++) {
842 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
843
844 /* validate kcontrol */
845 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
846 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
847 return -EINVAL;
848
849 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
850 if (sm == NULL)
851 return -ENOMEM;
852 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
853 mc->priv.size);
854
855 dev_dbg(tplg->dev,
856 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
857 mc->hdr.name, mc->hdr.access);
858
859 memset(&kc, 0, sizeof(kc));
860 kc.name = mc->hdr.name;
861 kc.private_value = (long)sm;
862 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
863 kc.access = mc->hdr.access;
864
865 /* we only support FL/FR channel mapping atm */
866 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
867 SNDRV_CHMAP_FL);
868 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
869 SNDRV_CHMAP_FR);
870 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
871 SNDRV_CHMAP_FL);
872 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
873 SNDRV_CHMAP_FR);
874
875 sm->max = mc->max;
876 sm->min = mc->min;
877 sm->invert = mc->invert;
878 sm->platform_max = mc->platform_max;
879 sm->dobj.index = tplg->index;
880 sm->dobj.ops = tplg->ops;
881 sm->dobj.type = SND_SOC_DOBJ_MIXER;
882 INIT_LIST_HEAD(&sm->dobj.list);
883
884 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800885 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100886 if (err) {
887 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
888 kfree(sm);
889 continue;
890 }
891
892 /* pass control to driver for optional further init */
893 err = soc_tplg_init_kcontrol(tplg, &kc,
894 (struct snd_soc_tplg_ctl_hdr *) mc);
895 if (err < 0) {
896 dev_err(tplg->dev, "ASoC: failed to init %s\n",
897 mc->hdr.name);
898 kfree(sm);
899 continue;
900 }
901
902 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100903 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100904
905 /* register control here */
906 err = soc_tplg_add_kcontrol(tplg, &kc,
907 &sm->dobj.control.kcontrol);
908 if (err < 0) {
909 dev_err(tplg->dev, "ASoC: failed to add %s\n",
910 mc->hdr.name);
911 soc_tplg_free_tlv(tplg, &kc);
912 kfree(sm);
913 continue;
914 }
915
916 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
917 }
918
919 return 0;
920}
921
922static int soc_tplg_denum_create_texts(struct soc_enum *se,
923 struct snd_soc_tplg_enum_control *ec)
924{
925 int i, ret;
926
927 se->dobj.control.dtexts =
Kees Cook6396bb22018-06-12 14:03:40 -0700928 kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100929 if (se->dobj.control.dtexts == NULL)
930 return -ENOMEM;
931
932 for (i = 0; i < ec->items; i++) {
933
934 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
935 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
936 ret = -EINVAL;
937 goto err;
938 }
939
940 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
941 if (!se->dobj.control.dtexts[i]) {
942 ret = -ENOMEM;
943 goto err;
944 }
945 }
946
Mousumi Janab6e38b22017-04-11 13:06:22 +0530947 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100948 return 0;
949
950err:
951 for (--i; i >= 0; i--)
952 kfree(se->dobj.control.dtexts[i]);
953 kfree(se->dobj.control.dtexts);
954 return ret;
955}
956
957static int soc_tplg_denum_create_values(struct soc_enum *se,
958 struct snd_soc_tplg_enum_control *ec)
959{
960 if (ec->items > sizeof(*ec->values))
961 return -EINVAL;
962
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200963 se->dobj.control.dvalues = kmemdup(ec->values,
964 ec->items * sizeof(u32),
965 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100966 if (!se->dobj.control.dvalues)
967 return -ENOMEM;
968
Liam Girdwood8a978232015-05-29 19:06:14 +0100969 return 0;
970}
971
972static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
973 size_t size)
974{
975 struct snd_soc_tplg_enum_control *ec;
976 struct soc_enum *se;
977 struct snd_kcontrol_new kc;
978 int i, ret, err;
979
980 if (soc_tplg_check_elem_count(tplg,
981 sizeof(struct snd_soc_tplg_enum_control),
982 count, size, "enums")) {
983
984 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
985 count);
986 return -EINVAL;
987 }
988
989 for (i = 0; i < count; i++) {
990 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
991 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
992 ec->priv.size);
993
994 /* validate kcontrol */
995 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
996 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
997 return -EINVAL;
998
999 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1000 if (se == NULL)
1001 return -ENOMEM;
1002
1003 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1004 ec->hdr.name, ec->items);
1005
1006 memset(&kc, 0, sizeof(kc));
1007 kc.name = ec->hdr.name;
1008 kc.private_value = (long)se;
1009 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1010 kc.access = ec->hdr.access;
1011
1012 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1013 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1014 SNDRV_CHMAP_FL);
1015 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1016 SNDRV_CHMAP_FL);
1017
1018 se->items = ec->items;
1019 se->mask = ec->mask;
1020 se->dobj.index = tplg->index;
1021 se->dobj.type = SND_SOC_DOBJ_ENUM;
1022 se->dobj.ops = tplg->ops;
1023 INIT_LIST_HEAD(&se->dobj.list);
1024
1025 switch (ec->hdr.ops.info) {
1026 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1027 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1028 err = soc_tplg_denum_create_values(se, ec);
1029 if (err < 0) {
1030 dev_err(tplg->dev,
1031 "ASoC: could not create values for %s\n",
1032 ec->hdr.name);
1033 kfree(se);
1034 continue;
1035 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001036 /* fall through */
Liam Girdwood8a978232015-05-29 19:06:14 +01001037 case SND_SOC_TPLG_CTL_ENUM:
1038 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1039 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1040 err = soc_tplg_denum_create_texts(se, ec);
1041 if (err < 0) {
1042 dev_err(tplg->dev,
1043 "ASoC: could not create texts for %s\n",
1044 ec->hdr.name);
1045 kfree(se);
1046 continue;
1047 }
1048 break;
1049 default:
1050 dev_err(tplg->dev,
1051 "ASoC: invalid enum control type %d for %s\n",
1052 ec->hdr.ops.info, ec->hdr.name);
1053 kfree(se);
1054 continue;
1055 }
1056
1057 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001058 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001059 if (err) {
1060 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1061 kfree(se);
1062 continue;
1063 }
1064
1065 /* pass control to driver for optional further init */
1066 err = soc_tplg_init_kcontrol(tplg, &kc,
1067 (struct snd_soc_tplg_ctl_hdr *) ec);
1068 if (err < 0) {
1069 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1070 ec->hdr.name);
1071 kfree(se);
1072 continue;
1073 }
1074
1075 /* register control here */
1076 ret = soc_tplg_add_kcontrol(tplg,
1077 &kc, &se->dobj.control.kcontrol);
1078 if (ret < 0) {
1079 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1080 ec->hdr.name);
1081 kfree(se);
1082 continue;
1083 }
1084
1085 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1086 }
1087
1088 return 0;
1089}
1090
1091static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1092 struct snd_soc_tplg_hdr *hdr)
1093{
1094 struct snd_soc_tplg_ctl_hdr *control_hdr;
1095 int i;
1096
1097 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1098 tplg->pos += hdr->size + hdr->payload_size;
1099 return 0;
1100 }
1101
1102 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1103 soc_tplg_get_offset(tplg));
1104
1105 for (i = 0; i < hdr->count; i++) {
1106
1107 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1108
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001109 if (control_hdr->size != sizeof(*control_hdr)) {
1110 dev_err(tplg->dev, "ASoC: invalid control size\n");
1111 return -EINVAL;
1112 }
1113
Liam Girdwood8a978232015-05-29 19:06:14 +01001114 switch (control_hdr->ops.info) {
1115 case SND_SOC_TPLG_CTL_VOLSW:
1116 case SND_SOC_TPLG_CTL_STROBE:
1117 case SND_SOC_TPLG_CTL_VOLSW_SX:
1118 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1119 case SND_SOC_TPLG_CTL_RANGE:
1120 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1121 case SND_SOC_TPLG_DAPM_CTL_PIN:
1122 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1123 break;
1124 case SND_SOC_TPLG_CTL_ENUM:
1125 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1126 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1127 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1128 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1129 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1130 break;
1131 case SND_SOC_TPLG_CTL_BYTES:
1132 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1133 break;
1134 default:
1135 soc_bind_err(tplg, control_hdr, i);
1136 return -EINVAL;
1137 }
1138 }
1139
1140 return 0;
1141}
1142
Liam Girdwood503e79b2018-06-14 20:53:59 +01001143/* optionally pass new dynamic kcontrol to component driver. */
1144static int soc_tplg_add_route(struct soc_tplg *tplg,
1145 struct snd_soc_dapm_route *route)
1146{
1147 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1148 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1149 route);
1150
1151 return 0;
1152}
1153
Liam Girdwood8a978232015-05-29 19:06:14 +01001154static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1155 struct snd_soc_tplg_hdr *hdr)
1156{
1157 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001158 struct snd_soc_tplg_dapm_graph_elem *elem;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001159 struct snd_soc_dapm_route **routes;
1160 int count = hdr->count, i, j;
1161 int ret = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001162
1163 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1164 tplg->pos += hdr->size + hdr->payload_size;
1165 return 0;
1166 }
1167
1168 if (soc_tplg_check_elem_count(tplg,
1169 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1170 count, hdr->payload_size, "graph")) {
1171
1172 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1173 count);
1174 return -EINVAL;
1175 }
1176
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001177 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1178 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001179
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001180 /* allocate memory for pointer to array of dapm routes */
1181 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1182 GFP_KERNEL);
1183 if (!routes)
1184 return -ENOMEM;
1185
1186 /*
1187 * allocate memory for each dapm route in the array.
1188 * This needs to be done individually so that
1189 * each route can be freed when it is removed in remove_route().
1190 */
1191 for (i = 0; i < count; i++) {
1192 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1193 if (!routes[i]) {
1194 /* free previously allocated memory */
1195 for (j = 0; j < i; j++)
1196 kfree(routes[j]);
1197
1198 kfree(routes);
1199 return -ENOMEM;
1200 }
1201 }
1202
Liam Girdwood8a978232015-05-29 19:06:14 +01001203 for (i = 0; i < count; i++) {
1204 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1205 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1206
1207 /* validate routes */
1208 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001209 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1210 ret = -EINVAL;
1211 break;
1212 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001213 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001214 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1215 ret = -EINVAL;
1216 break;
1217 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001218 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001219 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1220 ret = -EINVAL;
1221 break;
1222 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001223
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001224 routes[i]->source = elem->source;
1225 routes[i]->sink = elem->sink;
1226
1227 /* set to NULL atm for tplg users */
1228 routes[i]->connected = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001229 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001230 routes[i]->control = NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001231 else
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001232 routes[i]->control = elem->control;
Liam Girdwood8a978232015-05-29 19:06:14 +01001233
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001234 /* add route dobj to dobj_list */
1235 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1236 routes[i]->dobj.ops = tplg->ops;
1237 routes[i]->dobj.index = tplg->index;
1238 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1239
1240 soc_tplg_add_route(tplg, routes[i]);
Liam Girdwood503e79b2018-06-14 20:53:59 +01001241
Liam Girdwood8a978232015-05-29 19:06:14 +01001242 /* add route, but keep going if some fail */
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001243 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Liam Girdwood8a978232015-05-29 19:06:14 +01001244 }
1245
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06001246 /* free memory allocated for all dapm routes in case of error */
1247 if (ret < 0)
1248 for (i = 0; i < count ; i++)
1249 kfree(routes[i]);
1250
1251 /*
1252 * free pointer to array of dapm routes as this is no longer needed.
1253 * The memory allocated for each dapm route will be freed
1254 * when it is removed in remove_route().
1255 */
1256 kfree(routes);
1257
1258 return ret;
Liam Girdwood8a978232015-05-29 19:06:14 +01001259}
1260
1261static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1262 struct soc_tplg *tplg, int num_kcontrols)
1263{
1264 struct snd_kcontrol_new *kc;
1265 struct soc_mixer_control *sm;
1266 struct snd_soc_tplg_mixer_control *mc;
1267 int i, err;
1268
Axel Lin4ca7deb2015-08-05 22:34:22 +08001269 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001270 if (kc == NULL)
1271 return NULL;
1272
1273 for (i = 0; i < num_kcontrols; i++) {
1274 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1275 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1276 if (sm == NULL)
1277 goto err;
1278
1279 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1280 mc->priv.size);
1281
1282 /* validate kcontrol */
1283 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1284 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1285 goto err_str;
1286
1287 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1288 mc->hdr.name, i);
1289
Liam Girdwood267e2c62018-03-27 12:04:04 +01001290 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1291 if (kc[i].name == NULL)
1292 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001293 kc[i].private_value = (long)sm;
1294 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1295 kc[i].access = mc->hdr.access;
1296
1297 /* we only support FL/FR channel mapping atm */
1298 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1299 SNDRV_CHMAP_FL);
1300 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1301 SNDRV_CHMAP_FR);
1302 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1303 SNDRV_CHMAP_FL);
1304 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1305 SNDRV_CHMAP_FR);
1306
1307 sm->max = mc->max;
1308 sm->min = mc->min;
1309 sm->invert = mc->invert;
1310 sm->platform_max = mc->platform_max;
1311 sm->dobj.index = tplg->index;
1312 INIT_LIST_HEAD(&sm->dobj.list);
1313
1314 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001315 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001316 if (err) {
1317 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1318 kfree(sm);
1319 continue;
1320 }
1321
1322 /* pass control to driver for optional further init */
1323 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1324 (struct snd_soc_tplg_ctl_hdr *)mc);
1325 if (err < 0) {
1326 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1327 mc->hdr.name);
1328 kfree(sm);
1329 continue;
1330 }
Ranjani Sridharanbde8b382018-03-09 11:11:17 -08001331
1332 /* create any TLV data */
1333 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01001334 }
1335 return kc;
1336
1337err_str:
1338 kfree(sm);
1339err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001340 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001341 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001342 kfree(kc[i].name);
1343 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001344 kfree(kc);
1345 return NULL;
1346}
1347
1348static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001349 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001350{
1351 struct snd_kcontrol_new *kc;
1352 struct snd_soc_tplg_enum_control *ec;
1353 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001354 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001355
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001356 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001357 if (kc == NULL)
1358 return NULL;
1359
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001360 for (i = 0; i < num_kcontrols; i++) {
1361 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1362 /* validate kcontrol */
1363 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1364 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001365 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001366
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001367 se = kzalloc(sizeof(*se), GFP_KERNEL);
1368 if (se == NULL)
1369 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001370
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001371 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001372 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001373
Liam Girdwood267e2c62018-03-27 12:04:04 +01001374 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001375 if (kc[i].name == NULL) {
1376 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001377 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001378 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001379 kc[i].private_value = (long)se;
1380 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1381 kc[i].access = ec->hdr.access;
1382
1383 /* we only support FL/FR channel mapping atm */
1384 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1385 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1386 SNDRV_CHMAP_FL);
1387 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1388 SNDRV_CHMAP_FR);
1389
1390 se->items = ec->items;
1391 se->mask = ec->mask;
1392 se->dobj.index = tplg->index;
1393
1394 switch (ec->hdr.ops.info) {
1395 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1396 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1397 err = soc_tplg_denum_create_values(se, ec);
1398 if (err < 0) {
1399 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1400 ec->hdr.name);
1401 goto err_se;
1402 }
Takashi Iwai9c6c4d92018-10-04 20:30:06 +02001403 /* fall through */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001404 case SND_SOC_TPLG_CTL_ENUM:
1405 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1406 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1407 err = soc_tplg_denum_create_texts(se, ec);
1408 if (err < 0) {
1409 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1410 ec->hdr.name);
1411 goto err_se;
1412 }
1413 break;
1414 default:
1415 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1416 ec->hdr.ops.info, ec->hdr.name);
1417 goto err_se;
1418 }
1419
1420 /* map io handlers */
1421 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1422 if (err) {
1423 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1424 goto err_se;
1425 }
1426
1427 /* pass control to driver for optional further init */
1428 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1429 (struct snd_soc_tplg_ctl_hdr *)ec);
1430 if (err < 0) {
1431 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1432 ec->hdr.name);
1433 goto err_se;
1434 }
1435
1436 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1437 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001438 }
1439
1440 return kc;
1441
1442err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001443 for (; i >= 0; i--) {
1444 /* free values and texts */
1445 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001446 if (!se)
1447 continue;
1448
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001449 kfree(se->dobj.control.dvalues);
1450 for (j = 0; j < ec->items; j++)
1451 kfree(se->dobj.control.dtexts[j]);
Amadeusz Sławiński34db6a32019-01-25 14:06:44 -06001452 kfree(se->dobj.control.dtexts);
Liam Girdwood8a978232015-05-29 19:06:14 +01001453
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001454 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001455 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001456 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001457err:
1458 kfree(kc);
1459
1460 return NULL;
1461}
1462
1463static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1464 struct soc_tplg *tplg, int count)
1465{
1466 struct snd_soc_tplg_bytes_control *be;
1467 struct soc_bytes_ext *sbe;
1468 struct snd_kcontrol_new *kc;
1469 int i, err;
1470
Axel Lin4ca7deb2015-08-05 22:34:22 +08001471 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001472 if (!kc)
1473 return NULL;
1474
1475 for (i = 0; i < count; i++) {
1476 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1477
1478 /* validate kcontrol */
1479 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1480 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1481 goto err;
1482
1483 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1484 if (sbe == NULL)
1485 goto err;
1486
1487 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1488 be->priv.size);
1489
1490 dev_dbg(tplg->dev,
1491 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1492 be->hdr.name, be->hdr.access);
1493
Liam Girdwood267e2c62018-03-27 12:04:04 +01001494 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001495 if (kc[i].name == NULL) {
1496 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001497 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001498 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001499 kc[i].private_value = (long)sbe;
1500 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1501 kc[i].access = be->hdr.access;
1502
1503 sbe->max = be->max;
1504 INIT_LIST_HEAD(&sbe->dobj.list);
1505
1506 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001507 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001508 if (err) {
1509 soc_control_err(tplg, &be->hdr, be->hdr.name);
1510 kfree(sbe);
1511 continue;
1512 }
1513
1514 /* pass control to driver for optional further init */
1515 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1516 (struct snd_soc_tplg_ctl_hdr *)be);
1517 if (err < 0) {
1518 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1519 be->hdr.name);
1520 kfree(sbe);
1521 continue;
1522 }
1523 }
1524
1525 return kc;
1526
1527err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001528 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001529 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001530 kfree(kc[i].name);
1531 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001532
1533 kfree(kc);
1534 return NULL;
1535}
1536
1537static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1538 struct snd_soc_tplg_dapm_widget *w)
1539{
1540 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1541 struct snd_soc_dapm_widget template, *widget;
1542 struct snd_soc_tplg_ctl_hdr *control_hdr;
1543 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001544 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001545 int ret = 0;
1546
1547 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1548 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1549 return -EINVAL;
1550 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1551 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1552 return -EINVAL;
1553
1554 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1555 w->name, w->id);
1556
1557 memset(&template, 0, sizeof(template));
1558
1559 /* map user to kernel widget ID */
1560 template.id = get_widget_id(w->id);
1561 if (template.id < 0)
1562 return template.id;
1563
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001564 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001565 template.name = kstrdup(w->name, GFP_KERNEL);
1566 if (!template.name)
1567 return -ENOMEM;
1568 template.sname = kstrdup(w->sname, GFP_KERNEL);
1569 if (!template.sname) {
1570 ret = -ENOMEM;
1571 goto err;
1572 }
1573 template.reg = w->reg;
1574 template.shift = w->shift;
1575 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001576 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001577 template.on_val = w->invert ? 0 : 1;
1578 template.off_val = w->invert ? 1 : 0;
1579 template.ignore_suspend = w->ignore_suspend;
1580 template.event_flags = w->event_flags;
1581 template.dobj.index = tplg->index;
1582
1583 tplg->pos +=
1584 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1585 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001586 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001587 template.num_kcontrols = 0;
1588 goto widget;
1589 }
1590
1591 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1592 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1593 w->name, w->num_kcontrols, control_hdr->type);
1594
1595 switch (control_hdr->ops.info) {
1596 case SND_SOC_TPLG_CTL_VOLSW:
1597 case SND_SOC_TPLG_CTL_STROBE:
1598 case SND_SOC_TPLG_CTL_VOLSW_SX:
1599 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1600 case SND_SOC_TPLG_CTL_RANGE:
1601 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001602 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001603 template.num_kcontrols = w->num_kcontrols;
1604 template.kcontrol_news =
1605 soc_tplg_dapm_widget_dmixer_create(tplg,
1606 template.num_kcontrols);
1607 if (!template.kcontrol_news) {
1608 ret = -ENOMEM;
1609 goto hdr_err;
1610 }
1611 break;
1612 case SND_SOC_TPLG_CTL_ENUM:
1613 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1614 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1615 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1616 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001617 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001618 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001619 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001620 soc_tplg_dapm_widget_denum_create(tplg,
1621 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001622 if (!template.kcontrol_news) {
1623 ret = -ENOMEM;
1624 goto hdr_err;
1625 }
1626 break;
1627 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001628 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001629 template.num_kcontrols = w->num_kcontrols;
1630 template.kcontrol_news =
1631 soc_tplg_dapm_widget_dbytes_create(tplg,
1632 template.num_kcontrols);
1633 if (!template.kcontrol_news) {
1634 ret = -ENOMEM;
1635 goto hdr_err;
1636 }
1637 break;
1638 default:
1639 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1640 control_hdr->ops.get, control_hdr->ops.put,
1641 control_hdr->ops.info);
1642 ret = -EINVAL;
1643 goto hdr_err;
1644 }
1645
1646widget:
1647 ret = soc_tplg_widget_load(tplg, &template, w);
1648 if (ret < 0)
1649 goto hdr_err;
1650
1651 /* card dapm mutex is held by the core if we are loading topology
1652 * data during sound card init. */
1653 if (card->instantiated)
1654 widget = snd_soc_dapm_new_control(dapm, &template);
1655 else
1656 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001657 if (IS_ERR(widget)) {
1658 ret = PTR_ERR(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001659 goto hdr_err;
1660 }
1661
1662 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001663 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001664 widget->dobj.ops = tplg->ops;
1665 widget->dobj.index = tplg->index;
1666 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001667
1668 ret = soc_tplg_widget_ready(tplg, widget, w);
1669 if (ret < 0)
1670 goto ready_err;
1671
Bard liao7620fe92019-01-25 14:06:45 -06001672 kfree(template.sname);
1673 kfree(template.name);
1674
Liam Girdwood8a978232015-05-29 19:06:14 +01001675 return 0;
1676
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001677ready_err:
1678 snd_soc_tplg_widget_remove(widget);
1679 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001680hdr_err:
1681 kfree(template.sname);
1682err:
1683 kfree(template.name);
1684 return ret;
1685}
1686
1687static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1688 struct snd_soc_tplg_hdr *hdr)
1689{
1690 struct snd_soc_tplg_dapm_widget *widget;
1691 int ret, count = hdr->count, i;
1692
1693 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1694 return 0;
1695
1696 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1697
1698 for (i = 0; i < count; i++) {
1699 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001700 if (widget->size != sizeof(*widget)) {
1701 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1702 return -EINVAL;
1703 }
1704
Liam Girdwood8a978232015-05-29 19:06:14 +01001705 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001706 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001707 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1708 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001709 return ret;
1710 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001711 }
1712
1713 return 0;
1714}
1715
1716static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1717{
1718 struct snd_soc_card *card = tplg->comp->card;
1719 int ret;
1720
1721 /* Card might not have been registered at this point.
1722 * If so, just return success.
1723 */
1724 if (!card || !card->instantiated) {
1725 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001726 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001727 return 0;
1728 }
1729
1730 ret = snd_soc_dapm_new_widgets(card);
1731 if (ret < 0)
1732 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1733 ret);
1734
1735 return 0;
1736}
1737
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001738static void set_stream_info(struct snd_soc_pcm_stream *stream,
1739 struct snd_soc_tplg_stream_caps *caps)
1740{
1741 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1742 stream->channels_min = caps->channels_min;
1743 stream->channels_max = caps->channels_max;
1744 stream->rates = caps->rates;
1745 stream->rate_min = caps->rate_min;
1746 stream->rate_max = caps->rate_max;
1747 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001748 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001749}
1750
Mengdong Lin0038be92016-07-26 14:32:37 +08001751static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1752 unsigned int flag_mask, unsigned int flags)
1753{
1754 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1755 dai_drv->symmetric_rates =
1756 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1757
1758 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1759 dai_drv->symmetric_channels =
1760 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1761 1 : 0;
1762
1763 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1764 dai_drv->symmetric_samplebits =
1765 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1766 1 : 0;
1767}
1768
Mengdong Lin64527e82016-01-15 16:13:28 +08001769static int soc_tplg_dai_create(struct soc_tplg *tplg,
1770 struct snd_soc_tplg_pcm *pcm)
1771{
1772 struct snd_soc_dai_driver *dai_drv;
1773 struct snd_soc_pcm_stream *stream;
1774 struct snd_soc_tplg_stream_caps *caps;
1775 int ret;
1776
1777 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1778 if (dai_drv == NULL)
1779 return -ENOMEM;
1780
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001781 if (strlen(pcm->dai_name))
1782 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001783 dai_drv->id = pcm->dai_id;
1784
1785 if (pcm->playback) {
1786 stream = &dai_drv->playback;
1787 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001788 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001789 }
1790
1791 if (pcm->capture) {
1792 stream = &dai_drv->capture;
1793 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001794 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001795 }
1796
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001797 if (pcm->compress)
1798 dai_drv->compress_new = snd_soc_new_compress;
1799
Mengdong Lin64527e82016-01-15 16:13:28 +08001800 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001801 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001802 if (ret < 0) {
1803 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1804 kfree(dai_drv);
1805 return ret;
1806 }
1807
1808 dai_drv->dobj.index = tplg->index;
1809 dai_drv->dobj.ops = tplg->ops;
1810 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1811 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1812
1813 /* register the DAI to the component */
1814 return snd_soc_register_dai(tplg->comp, dai_drv);
1815}
1816
Mengdong Lin717a8e72016-11-03 01:03:34 +08001817static void set_link_flags(struct snd_soc_dai_link *link,
1818 unsigned int flag_mask, unsigned int flags)
1819{
1820 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1821 link->symmetric_rates =
1822 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1823
1824 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1825 link->symmetric_channels =
1826 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1827 1 : 0;
1828
1829 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1830 link->symmetric_samplebits =
1831 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1832 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001833
1834 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1835 link->ignore_suspend =
1836 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1837 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001838}
1839
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001840/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001841static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001842 struct snd_soc_tplg_pcm *pcm)
1843{
1844 struct snd_soc_dai_link *link;
1845 int ret;
1846
1847 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1848 if (link == NULL)
1849 return -ENOMEM;
1850
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001851 if (strlen(pcm->pcm_name)) {
1852 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1853 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1854 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001855 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001856
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001857 if (strlen(pcm->dai_name))
1858 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1859
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001860 link->codec_name = "snd-soc-dummy";
1861 link->codec_dai_name = "snd-soc-dummy-dai";
1862
1863 /* enable DPCM */
1864 link->dynamic = 1;
1865 link->dpcm_playback = pcm->playback;
1866 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001867 if (pcm->flag_mask)
1868 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001869
Mengdong Linacfc7d42016-01-15 16:13:37 +08001870 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01001871 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001872 if (ret < 0) {
1873 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1874 kfree(link);
1875 return ret;
1876 }
1877
1878 link->dobj.index = tplg->index;
1879 link->dobj.ops = tplg->ops;
1880 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1881 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1882
1883 snd_soc_add_dai_link(tplg->comp->card, link);
1884 return 0;
1885}
1886
1887/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001888static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1889 struct snd_soc_tplg_pcm *pcm)
1890{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001891 int ret;
1892
1893 ret = soc_tplg_dai_create(tplg, pcm);
1894 if (ret < 0)
1895 return ret;
1896
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001897 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001898}
1899
Mengdong Lin55726dc2016-11-03 01:00:16 +08001900/* copy stream caps from the old version 4 of source */
1901static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1902 struct snd_soc_tplg_stream_caps_v4 *src)
1903{
1904 dest->size = sizeof(*dest);
1905 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1906 dest->formats = src->formats;
1907 dest->rates = src->rates;
1908 dest->rate_min = src->rate_min;
1909 dest->rate_max = src->rate_max;
1910 dest->channels_min = src->channels_min;
1911 dest->channels_max = src->channels_max;
1912 dest->periods_min = src->periods_min;
1913 dest->periods_max = src->periods_max;
1914 dest->period_size_min = src->period_size_min;
1915 dest->period_size_max = src->period_size_max;
1916 dest->buffer_size_min = src->buffer_size_min;
1917 dest->buffer_size_max = src->buffer_size_max;
1918}
1919
1920/**
1921 * pcm_new_ver - Create the new version of PCM from the old version.
1922 * @tplg: topology context
1923 * @src: older version of pcm as a source
1924 * @pcm: latest version of pcm created from the source
1925 *
1926 * Support from vesion 4. User should free the returned pcm manually.
1927 */
1928static int pcm_new_ver(struct soc_tplg *tplg,
1929 struct snd_soc_tplg_pcm *src,
1930 struct snd_soc_tplg_pcm **pcm)
1931{
1932 struct snd_soc_tplg_pcm *dest;
1933 struct snd_soc_tplg_pcm_v4 *src_v4;
1934 int i;
1935
1936 *pcm = NULL;
1937
1938 if (src->size != sizeof(*src_v4)) {
1939 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1940 return -EINVAL;
1941 }
1942
1943 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1944 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1945 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1946 if (!dest)
1947 return -ENOMEM;
1948
1949 dest->size = sizeof(*dest); /* size of latest abi version */
1950 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1951 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1952 dest->pcm_id = src_v4->pcm_id;
1953 dest->dai_id = src_v4->dai_id;
1954 dest->playback = src_v4->playback;
1955 dest->capture = src_v4->capture;
1956 dest->compress = src_v4->compress;
1957 dest->num_streams = src_v4->num_streams;
1958 for (i = 0; i < dest->num_streams; i++)
1959 memcpy(&dest->stream[i], &src_v4->stream[i],
1960 sizeof(struct snd_soc_tplg_stream));
1961
1962 for (i = 0; i < 2; i++)
1963 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1964
1965 *pcm = dest;
1966 return 0;
1967}
1968
Mengdong Lin64527e82016-01-15 16:13:28 +08001969static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001970 struct snd_soc_tplg_hdr *hdr)
1971{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001972 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001973 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301974 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001975 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001976
1977 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1978 return 0;
1979
Mengdong Lin55726dc2016-11-03 01:00:16 +08001980 /* check the element size and count */
1981 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1982 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1983 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1984 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1985 pcm->size);
1986 return -EINVAL;
1987 }
1988
Liam Girdwood8a978232015-05-29 19:06:14 +01001989 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001990 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001991 hdr->payload_size, "PCM DAI")) {
1992 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1993 count);
1994 return -EINVAL;
1995 }
1996
Mengdong Lin64527e82016-01-15 16:13:28 +08001997 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001998 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1999
2000 /* check ABI version by size, create a new version of pcm
2001 * if abi not match.
2002 */
2003 if (pcm->size == sizeof(*pcm)) {
2004 abi_match = true;
2005 _pcm = pcm;
2006 } else {
2007 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05302008 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002009 }
2010
Mengdong Lin55726dc2016-11-03 01:00:16 +08002011 /* create the FE DAIs and DAI links */
2012 soc_tplg_pcm_create(tplg, _pcm);
2013
Mengdong Lin717a8e72016-11-03 01:03:34 +08002014 /* offset by version-specific struct size and
2015 * real priv data size
2016 */
2017 tplg->pos += pcm->size + _pcm->priv.size;
2018
Mengdong Lin55726dc2016-11-03 01:00:16 +08002019 if (!abi_match)
2020 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08002021 }
2022
Liam Girdwood8a978232015-05-29 19:06:14 +01002023 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002024
Liam Girdwood8a978232015-05-29 19:06:14 +01002025 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002026}
2027
Mengdong Lin593d9e52016-11-03 01:04:27 +08002028/**
2029 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002030 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002031 * @cfg: physical link configs.
2032 *
2033 * Topology context contains a list of supported HW formats (configs) and
2034 * a default format ID for the physical link. This function will use this
2035 * default ID to choose the HW format to set the link's DAI format for init.
2036 */
2037static void set_link_hw_format(struct snd_soc_dai_link *link,
2038 struct snd_soc_tplg_link_config *cfg)
2039{
2040 struct snd_soc_tplg_hw_config *hw_config;
2041 unsigned char bclk_master, fsync_master;
2042 unsigned char invert_bclk, invert_fsync;
2043 int i;
2044
2045 for (i = 0; i < cfg->num_hw_configs; i++) {
2046 hw_config = &cfg->hw_config[i];
2047 if (hw_config->id != cfg->default_hw_config_id)
2048 continue;
2049
2050 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
2051
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002052 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002053 switch (hw_config->clock_gated) {
2054 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002055 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002056 break;
2057
2058 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002059 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002060 break;
2061
2062 default:
2063 /* ignore the value */
2064 break;
2065 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002066
Mengdong Lin593d9e52016-11-03 01:04:27 +08002067 /* clock signal polarity */
2068 invert_bclk = hw_config->invert_bclk;
2069 invert_fsync = hw_config->invert_fsync;
2070 if (!invert_bclk && !invert_fsync)
2071 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2072 else if (!invert_bclk && invert_fsync)
2073 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2074 else if (invert_bclk && !invert_fsync)
2075 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2076 else
2077 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2078
2079 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002080 bclk_master = (hw_config->bclk_master ==
2081 SND_SOC_TPLG_BCLK_CM);
2082 fsync_master = (hw_config->fsync_master ==
2083 SND_SOC_TPLG_FSYNC_CM);
2084 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002085 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002086 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002087 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2088 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002089 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2090 else
2091 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2092 }
2093}
2094
2095/**
2096 * link_new_ver - Create a new physical link config from the old
2097 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002098 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002099 * @src: old version of phyical link config as a source
2100 * @link: latest version of physical link config created from the source
2101 *
2102 * Support from vesion 4. User need free the returned link config manually.
2103 */
2104static int link_new_ver(struct soc_tplg *tplg,
2105 struct snd_soc_tplg_link_config *src,
2106 struct snd_soc_tplg_link_config **link)
2107{
2108 struct snd_soc_tplg_link_config *dest;
2109 struct snd_soc_tplg_link_config_v4 *src_v4;
2110 int i;
2111
2112 *link = NULL;
2113
2114 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2115 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2116 return -EINVAL;
2117 }
2118
2119 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2120
2121 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2122 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2123 if (!dest)
2124 return -ENOMEM;
2125
2126 dest->size = sizeof(*dest);
2127 dest->id = src_v4->id;
2128 dest->num_streams = src_v4->num_streams;
2129 for (i = 0; i < dest->num_streams; i++)
2130 memcpy(&dest->stream[i], &src_v4->stream[i],
2131 sizeof(struct snd_soc_tplg_stream));
2132
2133 *link = dest;
2134 return 0;
2135}
2136
2137/* Find and configure an existing physical DAI link */
2138static int soc_tplg_link_config(struct soc_tplg *tplg,
2139 struct snd_soc_tplg_link_config *cfg)
2140{
2141 struct snd_soc_dai_link *link;
2142 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002143 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002144 int ret;
2145
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002146 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2147 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2148 return -EINVAL;
2149 else if (len)
2150 name = cfg->name;
2151 else
2152 name = NULL;
2153
2154 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2155 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2156 return -EINVAL;
2157 else if (len)
2158 stream_name = cfg->stream_name;
2159 else
2160 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002161
2162 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2163 name, stream_name);
2164 if (!link) {
2165 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2166 name, cfg->id);
2167 return -EINVAL;
2168 }
2169
2170 /* hw format */
2171 if (cfg->num_hw_configs)
2172 set_link_hw_format(link, cfg);
2173
2174 /* flags */
2175 if (cfg->flag_mask)
2176 set_link_flags(link, cfg->flag_mask, cfg->flags);
2177
2178 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002179 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002180 if (ret < 0) {
2181 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2182 return ret;
2183 }
2184
Bard liaoadfebb52019-02-01 11:07:40 -06002185 /* for unloading it in snd_soc_tplg_component_remove */
2186 link->dobj.index = tplg->index;
2187 link->dobj.ops = tplg->ops;
2188 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2189 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2190
Mengdong Lin593d9e52016-11-03 01:04:27 +08002191 return 0;
2192}
2193
2194
2195/* Load physical link config elements from the topology context */
2196static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2197 struct snd_soc_tplg_hdr *hdr)
2198{
2199 struct snd_soc_tplg_link_config *link, *_link;
2200 int count = hdr->count;
2201 int i, ret;
2202 bool abi_match;
2203
2204 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2205 tplg->pos += hdr->size + hdr->payload_size;
2206 return 0;
2207 };
2208
2209 /* check the element size and count */
2210 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2211 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2212 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2213 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2214 link->size);
2215 return -EINVAL;
2216 }
2217
2218 if (soc_tplg_check_elem_count(tplg,
2219 link->size, count,
2220 hdr->payload_size, "physical link config")) {
2221 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2222 count);
2223 return -EINVAL;
2224 }
2225
2226 /* config physical DAI links */
2227 for (i = 0; i < count; i++) {
2228 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2229 if (link->size == sizeof(*link)) {
2230 abi_match = true;
2231 _link = link;
2232 } else {
2233 abi_match = false;
2234 ret = link_new_ver(tplg, link, &_link);
2235 if (ret < 0)
2236 return ret;
2237 }
2238
2239 ret = soc_tplg_link_config(tplg, _link);
2240 if (ret < 0)
2241 return ret;
2242
2243 /* offset by version-specific struct size and
2244 * real priv data size
2245 */
2246 tplg->pos += link->size + _link->priv.size;
2247
2248 if (!abi_match)
2249 kfree(_link); /* free the duplicated one */
2250 }
2251
2252 return 0;
2253}
2254
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002255/**
2256 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002257 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002258 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002259 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002260 * The physical dai should already be registered by the platform driver.
2261 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002262 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002263static int soc_tplg_dai_config(struct soc_tplg *tplg,
2264 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002265{
2266 struct snd_soc_dai_link_component dai_component = {0};
2267 struct snd_soc_dai *dai;
2268 struct snd_soc_dai_driver *dai_drv;
2269 struct snd_soc_pcm_stream *stream;
2270 struct snd_soc_tplg_stream_caps *caps;
2271 int ret;
2272
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002273 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002274 dai = snd_soc_find_dai(&dai_component);
2275 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002276 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2277 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002278 return -EINVAL;
2279 }
2280
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002281 if (d->dai_id != dai->id) {
2282 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2283 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002284 return -EINVAL;
2285 }
2286
2287 dai_drv = dai->driver;
2288 if (!dai_drv)
2289 return -EINVAL;
2290
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002291 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002292 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002293 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002294 set_stream_info(stream, caps);
2295 }
2296
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002297 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002298 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002299 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002300 set_stream_info(stream, caps);
2301 }
2302
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002303 if (d->flag_mask)
2304 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002305
2306 /* pass control to component driver for optional further init */
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002307 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002308 if (ret < 0) {
2309 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2310 return ret;
2311 }
2312
2313 return 0;
2314}
2315
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002316/* load physical DAI elements */
2317static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2318 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002319{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002320 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002321 int count = hdr->count;
2322 int i;
2323
2324 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2325 return 0;
2326
2327 /* config the existing BE DAIs */
2328 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002329 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2330 if (dai->size != sizeof(*dai)) {
2331 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002332 return -EINVAL;
2333 }
2334
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002335 soc_tplg_dai_config(tplg, dai);
2336 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002337 }
2338
2339 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2340 return 0;
2341}
2342
Mengdong Lin583958f2016-10-11 14:36:42 +08002343/**
2344 * manifest_new_ver - Create a new version of manifest from the old version
2345 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002346 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002347 * @src: old version of manifest as a source
2348 * @manifest: latest version of manifest created from the source
2349 *
2350 * Support from vesion 4. Users need free the returned manifest manually.
2351 */
2352static int manifest_new_ver(struct soc_tplg *tplg,
2353 struct snd_soc_tplg_manifest *src,
2354 struct snd_soc_tplg_manifest **manifest)
2355{
2356 struct snd_soc_tplg_manifest *dest;
2357 struct snd_soc_tplg_manifest_v4 *src_v4;
2358
2359 *manifest = NULL;
2360
2361 if (src->size != sizeof(*src_v4)) {
Guenter Roeckac9391d2018-05-24 12:49:21 -07002362 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2363 src->size);
2364 if (src->size)
2365 return -EINVAL;
2366 src->size = sizeof(*src_v4);
Mengdong Lin583958f2016-10-11 14:36:42 +08002367 }
2368
2369 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2370
2371 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2372 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2373 if (!dest)
2374 return -ENOMEM;
2375
2376 dest->size = sizeof(*dest); /* size of latest abi version */
2377 dest->control_elems = src_v4->control_elems;
2378 dest->widget_elems = src_v4->widget_elems;
2379 dest->graph_elems = src_v4->graph_elems;
2380 dest->pcm_elems = src_v4->pcm_elems;
2381 dest->dai_link_elems = src_v4->dai_link_elems;
2382 dest->priv.size = src_v4->priv.size;
2383 if (dest->priv.size)
2384 memcpy(dest->priv.data, src_v4->priv.data,
2385 src_v4->priv.size);
2386
2387 *manifest = dest;
2388 return 0;
2389}
Mengdong Lin0038be92016-07-26 14:32:37 +08002390
Liam Girdwood8a978232015-05-29 19:06:14 +01002391static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002392 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002393{
Mengdong Lin583958f2016-10-11 14:36:42 +08002394 struct snd_soc_tplg_manifest *manifest, *_manifest;
2395 bool abi_match;
2396 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002397
2398 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2399 return 0;
2400
2401 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002402
2403 /* check ABI version by size, create a new manifest if abi not match */
2404 if (manifest->size == sizeof(*manifest)) {
2405 abi_match = true;
2406 _manifest = manifest;
2407 } else {
2408 abi_match = false;
2409 err = manifest_new_ver(tplg, manifest, &_manifest);
2410 if (err < 0)
2411 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002412 }
2413
Mengdong Lin583958f2016-10-11 14:36:42 +08002414 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002415 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwoodc60b6132018-06-14 20:50:37 +01002416 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002417
Mengdong Lin583958f2016-10-11 14:36:42 +08002418 if (!abi_match) /* free the duplicated one */
2419 kfree(_manifest);
2420
Liam Girdwood8a978232015-05-29 19:06:14 +01002421 return 0;
2422}
2423
2424/* validate header magic, size and type */
2425static int soc_valid_header(struct soc_tplg *tplg,
2426 struct snd_soc_tplg_hdr *hdr)
2427{
2428 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2429 return 0;
2430
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002431 if (hdr->size != sizeof(*hdr)) {
2432 dev_err(tplg->dev,
2433 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2434 hdr->type, soc_tplg_get_hdr_offset(tplg),
2435 tplg->fw->size);
2436 return -EINVAL;
2437 }
2438
Liam Girdwood8a978232015-05-29 19:06:14 +01002439 /* big endian firmware objects not supported atm */
2440 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2441 dev_err(tplg->dev,
2442 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2443 tplg->pass, hdr->magic,
2444 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2445 return -EINVAL;
2446 }
2447
2448 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2449 dev_err(tplg->dev,
2450 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2451 tplg->pass, hdr->magic,
2452 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2453 return -EINVAL;
2454 }
2455
Mengdong Lin288b8da2016-11-03 01:03:17 +08002456 /* Support ABI from version 4 */
2457 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2458 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002459 dev_err(tplg->dev,
2460 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2461 tplg->pass, hdr->abi,
2462 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2463 tplg->fw->size);
2464 return -EINVAL;
2465 }
2466
2467 if (hdr->payload_size == 0) {
2468 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2469 soc_tplg_get_hdr_offset(tplg));
2470 return -EINVAL;
2471 }
2472
2473 if (tplg->pass == hdr->type)
2474 dev_dbg(tplg->dev,
2475 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2476 hdr->payload_size, hdr->type, hdr->version,
2477 hdr->vendor_type, tplg->pass);
2478
2479 return 1;
2480}
2481
2482/* check header type and call appropriate handler */
2483static int soc_tplg_load_header(struct soc_tplg *tplg,
2484 struct snd_soc_tplg_hdr *hdr)
2485{
2486 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2487
2488 /* check for matching ID */
2489 if (hdr->index != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002490 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002491 return 0;
2492
2493 tplg->index = hdr->index;
2494
2495 switch (hdr->type) {
2496 case SND_SOC_TPLG_TYPE_MIXER:
2497 case SND_SOC_TPLG_TYPE_ENUM:
2498 case SND_SOC_TPLG_TYPE_BYTES:
2499 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2500 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2501 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2502 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2503 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2504 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002505 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002506 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002507 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002508 case SND_SOC_TPLG_TYPE_DAI_LINK:
2509 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2510 /* physical link configurations */
2511 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002512 case SND_SOC_TPLG_TYPE_MANIFEST:
2513 return soc_tplg_manifest_load(tplg, hdr);
2514 default:
2515 /* bespoke vendor data object */
2516 return soc_tplg_vendor_load(tplg, hdr);
2517 }
2518
2519 return 0;
2520}
2521
2522/* process the topology file headers */
2523static int soc_tplg_process_headers(struct soc_tplg *tplg)
2524{
2525 struct snd_soc_tplg_hdr *hdr;
2526 int ret;
2527
2528 tplg->pass = SOC_TPLG_PASS_START;
2529
2530 /* process the header types from start to end */
2531 while (tplg->pass <= SOC_TPLG_PASS_END) {
2532
2533 tplg->hdr_pos = tplg->fw->data;
2534 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2535
2536 while (!soc_tplg_is_eof(tplg)) {
2537
2538 /* make sure header is valid before loading */
2539 ret = soc_valid_header(tplg, hdr);
2540 if (ret < 0)
2541 return ret;
2542 else if (ret == 0)
2543 break;
2544
2545 /* load the header object */
2546 ret = soc_tplg_load_header(tplg, hdr);
2547 if (ret < 0)
2548 return ret;
2549
2550 /* goto next header */
2551 tplg->hdr_pos += hdr->payload_size +
2552 sizeof(struct snd_soc_tplg_hdr);
2553 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2554 }
2555
2556 /* next data type pass */
2557 tplg->pass++;
2558 }
2559
2560 /* signal DAPM we are complete */
2561 ret = soc_tplg_dapm_complete(tplg);
2562 if (ret < 0)
2563 dev_err(tplg->dev,
2564 "ASoC: failed to initialise DAPM from Firmware\n");
2565
2566 return ret;
2567}
2568
2569static int soc_tplg_load(struct soc_tplg *tplg)
2570{
2571 int ret;
2572
2573 ret = soc_tplg_process_headers(tplg);
2574 if (ret == 0)
2575 soc_tplg_complete(tplg);
2576
2577 return ret;
2578}
2579
2580/* load audio component topology from "firmware" file */
2581int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2582 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2583{
2584 struct soc_tplg tplg;
2585
2586 /* setup parsing context */
2587 memset(&tplg, 0, sizeof(tplg));
2588 tplg.fw = fw;
2589 tplg.dev = comp->dev;
2590 tplg.comp = comp;
2591 tplg.ops = ops;
2592 tplg.req_index = id;
2593 tplg.io_ops = ops->io_ops;
2594 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002595 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2596 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002597
2598 return soc_tplg_load(&tplg);
2599}
2600EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2601
2602/* remove this dynamic widget */
2603void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2604{
2605 /* make sure we are a widget */
2606 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2607 return;
2608
2609 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2610}
2611EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2612
2613/* remove all dynamic widgets from this DAPM context */
2614void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2615 u32 index)
2616{
2617 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002618
2619 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2620
2621 /* make sure we are a widget with correct context */
2622 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2623 continue;
2624
2625 /* match ID */
2626 if (w->dobj.index != index &&
2627 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2628 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002629 /* check and free and dynamic widget kcontrols */
2630 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002631 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002632 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002633 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002634}
2635EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2636
2637/* remove dynamic controls from the component driver */
2638int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2639{
2640 struct snd_soc_dobj *dobj, *next_dobj;
2641 int pass = SOC_TPLG_PASS_END;
2642
2643 /* process the header types from end to start */
2644 while (pass >= SOC_TPLG_PASS_START) {
2645
2646 /* remove mixer controls */
2647 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2648 list) {
2649
2650 /* match index */
2651 if (dobj->index != index &&
Yan Wangfeb12f02018-03-26 16:48:00 +01002652 index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002653 continue;
2654
2655 switch (dobj->type) {
2656 case SND_SOC_DOBJ_MIXER:
2657 remove_mixer(comp, dobj, pass);
2658 break;
2659 case SND_SOC_DOBJ_ENUM:
2660 remove_enum(comp, dobj, pass);
2661 break;
2662 case SND_SOC_DOBJ_BYTES:
2663 remove_bytes(comp, dobj, pass);
2664 break;
Ranjani Sridharan7df04ea2019-01-25 14:06:47 -06002665 case SND_SOC_DOBJ_GRAPH:
2666 remove_route(comp, dobj, pass);
2667 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002668 case SND_SOC_DOBJ_WIDGET:
2669 remove_widget(comp, dobj, pass);
2670 break;
2671 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002672 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002673 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002674 case SND_SOC_DOBJ_DAI_LINK:
2675 remove_link(comp, dobj, pass);
2676 break;
Bard liaoadfebb52019-02-01 11:07:40 -06002677 case SND_SOC_DOBJ_BACKEND_LINK:
2678 /*
2679 * call link_unload ops if extra
2680 * deinitialization is needed.
2681 */
2682 remove_backend_link(comp, dobj, pass);
2683 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002684 default:
2685 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2686 dobj->type);
2687 break;
2688 }
2689 }
2690 pass--;
2691 }
2692
2693 /* let caller know if FW can be freed when no objects are left */
2694 return !list_empty(&comp->dobj_list);
2695}
2696EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);