blob: b95a9ab0b5266cf9c8426dcd5f35afd36ad9b466 [file] [log] [blame]
Liam Girdwood8a978232015-05-29 19:06:14 +01001/*
2 * soc-topology.c -- ALSA SoC Topology
3 *
4 * Copyright (C) 2012 Texas Instruments Inc.
5 * Copyright (C) 2015 Intel Corporation.
6 *
7 * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
8 * K, Mythri P <mythri.p.k@intel.com>
9 * Prusty, Subhransu S <subhransu.s.prusty@intel.com>
10 * B, Jayachandran <jayachandran.b@intel.com>
11 * Abdullah, Omair M <omair.m.abdullah@intel.com>
12 * Jin, Yao <yao.jin@intel.com>
13 * Lin, Mengdong <mengdong.lin@intel.com>
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Add support to read audio firmware topology alongside firmware text. The
21 * topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
22 * equalizers, firmware, coefficients etc.
23 *
24 * This file only manages the core ALSA and ASoC components, all other bespoke
25 * firmware topology data is passed to component drivers for bespoke handling.
26 */
27
28#include <linux/kernel.h>
29#include <linux/export.h>
30#include <linux/list.h>
31#include <linux/firmware.h>
32#include <linux/slab.h>
33#include <sound/soc.h>
34#include <sound/soc-dapm.h>
35#include <sound/soc-topology.h>
Mengdong Lin28a87ee2015-08-05 14:41:13 +010036#include <sound/tlv.h>
Liam Girdwood8a978232015-05-29 19:06:14 +010037
38/*
39 * We make several passes over the data (since it wont necessarily be ordered)
40 * and process objects in the following order. This guarantees the component
41 * drivers will be ready with any vendor data before the mixers and DAPM objects
42 * are loaded (that may make use of the vendor data).
43 */
44#define SOC_TPLG_PASS_MANIFEST 0
45#define SOC_TPLG_PASS_VENDOR 1
46#define SOC_TPLG_PASS_MIXER 2
47#define SOC_TPLG_PASS_WIDGET 3
Mengdong Lin1a8e7fa2015-08-10 22:48:30 +080048#define SOC_TPLG_PASS_PCM_DAI 4
49#define SOC_TPLG_PASS_GRAPH 5
50#define SOC_TPLG_PASS_PINS 6
Mengdong Lin0038be92016-07-26 14:32:37 +080051#define SOC_TPLG_PASS_BE_DAI 7
Mengdong Lin593d9e52016-11-03 01:04:27 +080052#define SOC_TPLG_PASS_LINK 8
Liam Girdwood8a978232015-05-29 19:06:14 +010053
54#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
Mengdong Lin593d9e52016-11-03 01:04:27 +080055#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
Liam Girdwood8a978232015-05-29 19:06:14 +010056
Mengdong Lin583958f2016-10-11 14:36:42 +080057/*
58 * Old version of ABI structs, supported for backward compatibility.
59 */
60
61/* Manifest v4 */
62struct snd_soc_tplg_manifest_v4 {
63 __le32 size; /* in bytes of this structure */
64 __le32 control_elems; /* number of control elements */
65 __le32 widget_elems; /* number of widget elements */
66 __le32 graph_elems; /* number of graph elements */
67 __le32 pcm_elems; /* number of PCM elements */
68 __le32 dai_link_elems; /* number of DAI link elements */
69 struct snd_soc_tplg_private priv;
70} __packed;
71
Mengdong Lin55726dc2016-11-03 01:00:16 +080072/* Stream Capabilities v4 */
73struct snd_soc_tplg_stream_caps_v4 {
74 __le32 size; /* in bytes of this structure */
75 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
76 __le64 formats; /* supported formats SNDRV_PCM_FMTBIT_* */
77 __le32 rates; /* supported rates SNDRV_PCM_RATE_* */
78 __le32 rate_min; /* min rate */
79 __le32 rate_max; /* max rate */
80 __le32 channels_min; /* min channels */
81 __le32 channels_max; /* max channels */
82 __le32 periods_min; /* min number of periods */
83 __le32 periods_max; /* max number of periods */
84 __le32 period_size_min; /* min period size bytes */
85 __le32 period_size_max; /* max period size bytes */
86 __le32 buffer_size_min; /* min buffer size bytes */
87 __le32 buffer_size_max; /* max buffer size bytes */
88} __packed;
89
90/* PCM v4 */
91struct snd_soc_tplg_pcm_v4 {
92 __le32 size; /* in bytes of this structure */
93 char pcm_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
94 char dai_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
95 __le32 pcm_id; /* unique ID - used to match with DAI link */
96 __le32 dai_id; /* unique ID - used to match */
97 __le32 playback; /* supports playback mode */
98 __le32 capture; /* supports capture mode */
99 __le32 compress; /* 1 = compressed; 0 = PCM */
100 struct snd_soc_tplg_stream stream[SND_SOC_TPLG_STREAM_CONFIG_MAX]; /* for DAI link */
101 __le32 num_streams; /* number of streams */
102 struct snd_soc_tplg_stream_caps_v4 caps[2]; /* playback and capture for DAI */
103} __packed;
104
Mengdong Lin593d9e52016-11-03 01:04:27 +0800105/* Physical link config v4 */
106struct snd_soc_tplg_link_config_v4 {
107 __le32 size; /* in bytes of this structure */
108 __le32 id; /* unique ID - used to match */
109 struct snd_soc_tplg_stream stream[SND_SOC_TPLG_STREAM_CONFIG_MAX]; /* supported configs playback and captrure */
110 __le32 num_streams; /* number of streams */
111} __packed;
112
Mengdong Lin583958f2016-10-11 14:36:42 +0800113/* topology context */
Liam Girdwood8a978232015-05-29 19:06:14 +0100114struct soc_tplg {
115 const struct firmware *fw;
116
117 /* runtime FW parsing */
118 const u8 *pos; /* read postion */
119 const u8 *hdr_pos; /* header position */
120 unsigned int pass; /* pass number */
121
122 /* component caller */
123 struct device *dev;
124 struct snd_soc_component *comp;
125 u32 index; /* current block index */
126 u32 req_index; /* required index, only loaded/free matching blocks */
127
Mengdong Lin88a17d82015-08-18 18:11:51 +0800128 /* vendor specific kcontrol operations */
Liam Girdwood8a978232015-05-29 19:06:14 +0100129 const struct snd_soc_tplg_kcontrol_ops *io_ops;
130 int io_ops_count;
131
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800132 /* vendor specific bytes ext handlers, for TLV bytes controls */
133 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
134 int bytes_ext_ops_count;
135
Liam Girdwood8a978232015-05-29 19:06:14 +0100136 /* optional fw loading callbacks to component drivers */
137 struct snd_soc_tplg_ops *ops;
138};
139
140static int soc_tplg_process_headers(struct soc_tplg *tplg);
141static void soc_tplg_complete(struct soc_tplg *tplg);
142struct snd_soc_dapm_widget *
143snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
144 const struct snd_soc_dapm_widget *widget);
145struct snd_soc_dapm_widget *
146snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
147 const struct snd_soc_dapm_widget *widget);
148
149/* check we dont overflow the data for this control chunk */
150static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
151 unsigned int count, size_t bytes, const char *elem_type)
152{
153 const u8 *end = tplg->pos + elem_size * count;
154
155 if (end > tplg->fw->data + tplg->fw->size) {
156 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
157 elem_type);
158 return -EINVAL;
159 }
160
161 /* check there is enough room in chunk for control.
162 extra bytes at the end of control are for vendor data here */
163 if (elem_size * count > bytes) {
164 dev_err(tplg->dev,
165 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
166 elem_type, count, elem_size, bytes);
167 return -EINVAL;
168 }
169
170 return 0;
171}
172
173static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
174{
175 const u8 *end = tplg->hdr_pos;
176
177 if (end >= tplg->fw->data + tplg->fw->size)
178 return 1;
179 return 0;
180}
181
182static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
183{
184 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
185}
186
187static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
188{
189 return (unsigned long)(tplg->pos - tplg->fw->data);
190}
191
192/* mapping of Kcontrol types and associated operations. */
193static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
194 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
195 snd_soc_put_volsw, snd_soc_info_volsw},
196 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
197 snd_soc_put_volsw_sx, NULL},
198 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
199 snd_soc_put_enum_double, snd_soc_info_enum_double},
200 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
201 snd_soc_put_enum_double, NULL},
202 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
203 snd_soc_bytes_put, snd_soc_bytes_info},
204 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
205 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
206 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
207 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
208 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
209 snd_soc_put_strobe, NULL},
210 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
Jeeja KP2c57d4782015-07-14 13:10:47 +0530211 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
Liam Girdwood8a978232015-05-29 19:06:14 +0100212 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
213 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
214 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
215 snd_soc_dapm_put_enum_double, NULL},
216 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
217 snd_soc_dapm_put_enum_double, NULL},
218 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
219 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
220};
221
222struct soc_tplg_map {
223 int uid;
224 int kid;
225};
226
227/* mapping of widget types from UAPI IDs to kernel IDs */
228static const struct soc_tplg_map dapm_map[] = {
229 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
230 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
231 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
232 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
233 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
234 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
235 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
236 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
237 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
238 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
239 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
240 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
241 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
242 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
243 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
244 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
Liam Girdwood8a70b452017-06-29 14:22:24 +0100245 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
246 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
247 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
248 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
249 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
250 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
251 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
252 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
Liam Girdwood8a978232015-05-29 19:06:14 +0100253};
254
255static int tplc_chan_get_reg(struct soc_tplg *tplg,
256 struct snd_soc_tplg_channel *chan, int map)
257{
258 int i;
259
260 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
261 if (chan[i].id == map)
262 return chan[i].reg;
263 }
264
265 return -EINVAL;
266}
267
268static int tplc_chan_get_shift(struct soc_tplg *tplg,
269 struct snd_soc_tplg_channel *chan, int map)
270{
271 int i;
272
273 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
274 if (chan[i].id == map)
275 return chan[i].shift;
276 }
277
278 return -EINVAL;
279}
280
281static int get_widget_id(int tplg_type)
282{
283 int i;
284
285 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
286 if (tplg_type == dapm_map[i].uid)
287 return dapm_map[i].kid;
288 }
289
290 return -EINVAL;
291}
292
Liam Girdwood8a978232015-05-29 19:06:14 +0100293static inline void soc_bind_err(struct soc_tplg *tplg,
294 struct snd_soc_tplg_ctl_hdr *hdr, int index)
295{
296 dev_err(tplg->dev,
297 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
298 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
299 soc_tplg_get_offset(tplg));
300}
301
302static inline void soc_control_err(struct soc_tplg *tplg,
303 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
304{
305 dev_err(tplg->dev,
306 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
307 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
308 soc_tplg_get_offset(tplg));
309}
310
311/* pass vendor data to component driver for processing */
312static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
313 struct snd_soc_tplg_hdr *hdr)
314{
315 int ret = 0;
316
317 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
Liam Girdwood81e9b0a2018-03-27 14:30:42 +0100318 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100319 else {
320 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
321 hdr->vendor_type);
322 return -EINVAL;
323 }
324
325 if (ret < 0)
326 dev_err(tplg->dev,
327 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
328 soc_tplg_get_hdr_offset(tplg),
329 soc_tplg_get_hdr_offset(tplg),
330 hdr->type, hdr->vendor_type);
331 return ret;
332}
333
334/* pass vendor data to component driver for processing */
335static int soc_tplg_vendor_load(struct soc_tplg *tplg,
336 struct snd_soc_tplg_hdr *hdr)
337{
338 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
339 return 0;
340
341 return soc_tplg_vendor_load_(tplg, hdr);
342}
343
344/* optionally pass new dynamic widget to component driver. This is mainly for
345 * external widgets where we can assign private data/ops */
346static int soc_tplg_widget_load(struct soc_tplg *tplg,
347 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
348{
349 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
Liam Girdwood81e9b0a2018-03-27 14:30:42 +0100350 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
351 tplg_w);
Liam Girdwood8a978232015-05-29 19:06:14 +0100352
353 return 0;
354}
355
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100356/* optionally pass new dynamic widget to component driver. This is mainly for
357 * external widgets where we can assign private data/ops */
358static int soc_tplg_widget_ready(struct soc_tplg *tplg,
359 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
360{
361 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
Liam Girdwood81e9b0a2018-03-27 14:30:42 +0100362 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
363 tplg_w);
Liam Girdwoodebd259d2017-06-09 15:43:23 +0100364
365 return 0;
366}
367
Masahiro Yamada183b8022017-02-27 14:29:20 -0800368/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800369static int soc_tplg_dai_load(struct soc_tplg *tplg,
Liam Girdwood81e9b0a2018-03-27 14:30:42 +0100370 struct snd_soc_dai_driver *dai_drv,
371 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
Liam Girdwood8a978232015-05-29 19:06:14 +0100372{
Mengdong Lin64527e82016-01-15 16:13:28 +0800373 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
Liam Girdwood81e9b0a2018-03-27 14:30:42 +0100374 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
375 pcm, dai);
Liam Girdwood8a978232015-05-29 19:06:14 +0100376
377 return 0;
378}
379
Masahiro Yamada183b8022017-02-27 14:29:20 -0800380/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800381static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
Liam Girdwood81e9b0a2018-03-27 14:30:42 +0100382 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
Mengdong Linacfc7d42016-01-15 16:13:37 +0800383{
384 if (tplg->comp && tplg->ops && tplg->ops->link_load)
Liam Girdwood81e9b0a2018-03-27 14:30:42 +0100385 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
Mengdong Linacfc7d42016-01-15 16:13:37 +0800386
387 return 0;
388}
389
Liam Girdwood8a978232015-05-29 19:06:14 +0100390/* tell the component driver that all firmware has been loaded in this request */
391static void soc_tplg_complete(struct soc_tplg *tplg)
392{
393 if (tplg->comp && tplg->ops && tplg->ops->complete)
394 tplg->ops->complete(tplg->comp);
395}
396
397/* add a dynamic kcontrol */
398static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
399 const struct snd_kcontrol_new *control_new, const char *prefix,
400 void *data, struct snd_kcontrol **kcontrol)
401{
402 int err;
403
404 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
405 if (*kcontrol == NULL) {
406 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
407 control_new->name);
408 return -ENOMEM;
409 }
410
411 err = snd_ctl_add(card, *kcontrol);
412 if (err < 0) {
413 dev_err(dev, "ASoC: Failed to add %s: %d\n",
414 control_new->name, err);
415 return err;
416 }
417
418 return 0;
419}
420
421/* add a dynamic kcontrol for component driver */
422static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
423 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
424{
425 struct snd_soc_component *comp = tplg->comp;
426
427 return soc_tplg_add_dcontrol(comp->card->snd_card,
428 comp->dev, k, NULL, comp, kcontrol);
429}
430
431/* remove a mixer kcontrol */
432static void remove_mixer(struct snd_soc_component *comp,
433 struct snd_soc_dobj *dobj, int pass)
434{
435 struct snd_card *card = comp->card->snd_card;
436 struct soc_mixer_control *sm =
437 container_of(dobj, struct soc_mixer_control, dobj);
438 const unsigned int *p = NULL;
439
440 if (pass != SOC_TPLG_PASS_MIXER)
441 return;
442
443 if (dobj->ops && dobj->ops->control_unload)
444 dobj->ops->control_unload(comp, dobj);
445
446 if (sm->dobj.control.kcontrol->tlv.p)
447 p = sm->dobj.control.kcontrol->tlv.p;
448 snd_ctl_remove(card, sm->dobj.control.kcontrol);
449 list_del(&sm->dobj.list);
450 kfree(sm);
451 kfree(p);
452}
453
454/* remove an enum kcontrol */
455static void remove_enum(struct snd_soc_component *comp,
456 struct snd_soc_dobj *dobj, int pass)
457{
458 struct snd_card *card = comp->card->snd_card;
459 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
460 int i;
461
462 if (pass != SOC_TPLG_PASS_MIXER)
463 return;
464
465 if (dobj->ops && dobj->ops->control_unload)
466 dobj->ops->control_unload(comp, dobj);
467
468 snd_ctl_remove(card, se->dobj.control.kcontrol);
469 list_del(&se->dobj.list);
470
471 kfree(se->dobj.control.dvalues);
472 for (i = 0; i < se->items; i++)
473 kfree(se->dobj.control.dtexts[i]);
474 kfree(se);
475}
476
477/* remove a byte kcontrol */
478static void remove_bytes(struct snd_soc_component *comp,
479 struct snd_soc_dobj *dobj, int pass)
480{
481 struct snd_card *card = comp->card->snd_card;
482 struct soc_bytes_ext *sb =
483 container_of(dobj, struct soc_bytes_ext, dobj);
484
485 if (pass != SOC_TPLG_PASS_MIXER)
486 return;
487
488 if (dobj->ops && dobj->ops->control_unload)
489 dobj->ops->control_unload(comp, dobj);
490
491 snd_ctl_remove(card, sb->dobj.control.kcontrol);
492 list_del(&sb->dobj.list);
493 kfree(sb);
494}
495
496/* remove a widget and it's kcontrols - routes must be removed first */
497static void remove_widget(struct snd_soc_component *comp,
498 struct snd_soc_dobj *dobj, int pass)
499{
500 struct snd_card *card = comp->card->snd_card;
501 struct snd_soc_dapm_widget *w =
502 container_of(dobj, struct snd_soc_dapm_widget, dobj);
503 int i;
504
505 if (pass != SOC_TPLG_PASS_WIDGET)
506 return;
507
508 if (dobj->ops && dobj->ops->widget_unload)
509 dobj->ops->widget_unload(comp, dobj);
510
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000511 if (!w->kcontrols)
512 goto free_news;
513
Liam Girdwood8a978232015-05-29 19:06:14 +0100514 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800515 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100516 * The enum may either have an array of values or strings.
517 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800518 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100519 /* enumerated widget mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800520 for (i = 0; i < w->num_kcontrols; i++) {
521 struct snd_kcontrol *kcontrol = w->kcontrols[i];
522 struct soc_enum *se =
523 (struct soc_enum *)kcontrol->private_value;
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100524 int j;
Liam Girdwood8a978232015-05-29 19:06:14 +0100525
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800526 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100527
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800528 kfree(se->dobj.control.dvalues);
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100529 for (j = 0; j < se->items; j++)
530 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +0100531
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800532 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100533 kfree(w->kcontrol_news[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800534 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100535 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800536 /* volume mixer or bytes controls */
Liam Girdwood8a978232015-05-29 19:06:14 +0100537 for (i = 0; i < w->num_kcontrols; i++) {
538 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100539
Mengdong Lineea3dd42016-11-25 16:09:17 +0800540 if (dobj->widget.kcontrol_type
541 == SND_SOC_TPLG_TYPE_MIXER)
542 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100543
Mengdong Lineea3dd42016-11-25 16:09:17 +0800544 /* Private value is used as struct soc_mixer_control
545 * for volume mixers or soc_bytes_ext for bytes
546 * controls.
547 */
548 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000549 snd_ctl_remove(card, kcontrol);
Liam Girdwood267e2c62018-03-27 12:04:04 +0100550 kfree(w->kcontrol_news[i].name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100551 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100552 }
Liam Girdwood05bdcf12018-03-14 20:42:40 +0000553
554free_news:
555 kfree(w->kcontrol_news);
556
Liam Girdwood8a978232015-05-29 19:06:14 +0100557 /* widget w is freed by soc-dapm.c */
558}
559
Mengdong Lin64527e82016-01-15 16:13:28 +0800560/* remove DAI configurations */
561static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100562 struct snd_soc_dobj *dobj, int pass)
563{
Mengdong Lin64527e82016-01-15 16:13:28 +0800564 struct snd_soc_dai_driver *dai_drv =
565 container_of(dobj, struct snd_soc_dai_driver, dobj);
566
Liam Girdwood8a978232015-05-29 19:06:14 +0100567 if (pass != SOC_TPLG_PASS_PCM_DAI)
568 return;
569
Mengdong Lin64527e82016-01-15 16:13:28 +0800570 if (dobj->ops && dobj->ops->dai_unload)
571 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100572
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800573 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100574 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800575 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100576}
577
Mengdong Linacfc7d42016-01-15 16:13:37 +0800578/* remove link configurations */
579static void remove_link(struct snd_soc_component *comp,
580 struct snd_soc_dobj *dobj, int pass)
581{
582 struct snd_soc_dai_link *link =
583 container_of(dobj, struct snd_soc_dai_link, dobj);
584
585 if (pass != SOC_TPLG_PASS_PCM_DAI)
586 return;
587
588 if (dobj->ops && dobj->ops->link_unload)
589 dobj->ops->link_unload(comp, dobj);
590
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800591 kfree(link->name);
592 kfree(link->stream_name);
593 kfree(link->cpu_dai_name);
594
Mengdong Linacfc7d42016-01-15 16:13:37 +0800595 list_del(&dobj->list);
596 snd_soc_remove_dai_link(comp->card, link);
597 kfree(link);
598}
599
Liam Girdwood8a978232015-05-29 19:06:14 +0100600/* bind a kcontrol to it's IO handlers */
601static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
602 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800603 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100604{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800605 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800606 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800607 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100608
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800609 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
610 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
611 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
612 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
613 struct soc_bytes_ext *sbe;
614 struct snd_soc_tplg_bytes_control *be;
615
616 sbe = (struct soc_bytes_ext *)k->private_value;
617 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
618
619 /* TLV bytes controls need standard kcontrol info handler,
620 * TLV callback and extended put/get handlers.
621 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530622 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800623 k->tlv.c = snd_soc_bytes_tlv_callback;
624
625 ext_ops = tplg->bytes_ext_ops;
626 num_ops = tplg->bytes_ext_ops_count;
627 for (i = 0; i < num_ops; i++) {
628 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
629 sbe->put = ext_ops[i].put;
630 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
631 sbe->get = ext_ops[i].get;
632 }
633
634 if (sbe->put && sbe->get)
635 return 0;
636 else
637 return -EINVAL;
638 }
639
Mengdong Lin88a17d82015-08-18 18:11:51 +0800640 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800641 ops = tplg->io_ops;
642 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100643 for (i = 0; i < num_ops; i++) {
644
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800645 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100646 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800647 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100648 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800649 if (k->info == NULL && ops[i].id == hdr->ops.info)
650 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100651 }
652
Mengdong Lin88a17d82015-08-18 18:11:51 +0800653 /* vendor specific handlers found ? */
654 if (k->put && k->get && k->info)
655 return 0;
656
657 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800658 ops = io_ops;
659 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800660 for (i = 0; i < num_ops; i++) {
661
662 if (k->put == NULL && ops[i].id == hdr->ops.put)
663 k->put = ops[i].put;
664 if (k->get == NULL && ops[i].id == hdr->ops.get)
665 k->get = ops[i].get;
666 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100667 k->info = ops[i].info;
668 }
669
670 /* standard handlers found ? */
671 if (k->put && k->get && k->info)
672 return 0;
673
Liam Girdwood8a978232015-05-29 19:06:14 +0100674 /* nothing to bind */
675 return -EINVAL;
676}
677
678/* bind a widgets to it's evnt handlers */
679int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
680 const struct snd_soc_tplg_widget_events *events,
681 int num_events, u16 event_type)
682{
683 int i;
684
685 w->event = NULL;
686
687 for (i = 0; i < num_events; i++) {
688 if (event_type == events[i].type) {
689
690 /* found - so assign event */
691 w->event = events[i].event_handler;
692 return 0;
693 }
694 }
695
696 /* not found */
697 return -EINVAL;
698}
699EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
700
701/* optionally pass new dynamic kcontrol to component driver. */
702static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
703 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
704{
705 if (tplg->comp && tplg->ops && tplg->ops->control_load)
Liam Girdwood81e9b0a2018-03-27 14:30:42 +0100706 return tplg->ops->control_load(tplg->comp, tplg->index, k,
707 hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100708
709 return 0;
710}
711
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100712
713static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
714 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100715{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100716 unsigned int item_len = 2 * sizeof(unsigned int);
717 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100718
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100719 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
720 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100721 return -ENOMEM;
722
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100723 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
724 p[1] = item_len;
725 p[2] = scale->min;
726 p[3] = (scale->step & TLV_DB_SCALE_MASK)
727 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100728
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100729 kc->tlv.p = (void *)p;
730 return 0;
731}
732
733static int soc_tplg_create_tlv(struct soc_tplg *tplg,
734 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
735{
736 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
737
738 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
739 return 0;
740
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800741 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100742 tplg_tlv = &tc->tlv;
743 switch (tplg_tlv->type) {
744 case SNDRV_CTL_TLVT_DB_SCALE:
745 return soc_tplg_create_tlv_db_scale(tplg, kc,
746 &tplg_tlv->scale);
747
748 /* TODO: add support for other TLV types */
749 default:
750 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
751 tplg_tlv->type);
752 return -EINVAL;
753 }
754 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100755
756 return 0;
757}
758
759static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
760 struct snd_kcontrol_new *kc)
761{
762 kfree(kc->tlv.p);
763}
764
765static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
766 size_t size)
767{
768 struct snd_soc_tplg_bytes_control *be;
769 struct soc_bytes_ext *sbe;
770 struct snd_kcontrol_new kc;
771 int i, err;
772
773 if (soc_tplg_check_elem_count(tplg,
774 sizeof(struct snd_soc_tplg_bytes_control), count,
775 size, "mixer bytes")) {
776 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
777 count);
778 return -EINVAL;
779 }
780
781 for (i = 0; i < count; i++) {
782 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
783
784 /* validate kcontrol */
785 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
786 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
787 return -EINVAL;
788
789 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
790 if (sbe == NULL)
791 return -ENOMEM;
792
793 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
794 be->priv.size);
795
796 dev_dbg(tplg->dev,
797 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
798 be->hdr.name, be->hdr.access);
799
800 memset(&kc, 0, sizeof(kc));
801 kc.name = be->hdr.name;
802 kc.private_value = (long)sbe;
803 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
804 kc.access = be->hdr.access;
805
806 sbe->max = be->max;
807 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
808 sbe->dobj.ops = tplg->ops;
809 INIT_LIST_HEAD(&sbe->dobj.list);
810
811 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800812 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100813 if (err) {
814 soc_control_err(tplg, &be->hdr, be->hdr.name);
815 kfree(sbe);
816 continue;
817 }
818
819 /* pass control to driver for optional further init */
820 err = soc_tplg_init_kcontrol(tplg, &kc,
821 (struct snd_soc_tplg_ctl_hdr *)be);
822 if (err < 0) {
823 dev_err(tplg->dev, "ASoC: failed to init %s\n",
824 be->hdr.name);
825 kfree(sbe);
826 continue;
827 }
828
829 /* register control here */
830 err = soc_tplg_add_kcontrol(tplg, &kc,
831 &sbe->dobj.control.kcontrol);
832 if (err < 0) {
833 dev_err(tplg->dev, "ASoC: failed to add %s\n",
834 be->hdr.name);
835 kfree(sbe);
836 continue;
837 }
838
839 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
840 }
841 return 0;
842
843}
844
845static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
846 size_t size)
847{
848 struct snd_soc_tplg_mixer_control *mc;
849 struct soc_mixer_control *sm;
850 struct snd_kcontrol_new kc;
851 int i, err;
852
853 if (soc_tplg_check_elem_count(tplg,
854 sizeof(struct snd_soc_tplg_mixer_control),
855 count, size, "mixers")) {
856
857 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
858 count);
859 return -EINVAL;
860 }
861
862 for (i = 0; i < count; i++) {
863 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
864
865 /* validate kcontrol */
866 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
867 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
868 return -EINVAL;
869
870 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
871 if (sm == NULL)
872 return -ENOMEM;
873 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
874 mc->priv.size);
875
876 dev_dbg(tplg->dev,
877 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
878 mc->hdr.name, mc->hdr.access);
879
880 memset(&kc, 0, sizeof(kc));
881 kc.name = mc->hdr.name;
882 kc.private_value = (long)sm;
883 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
884 kc.access = mc->hdr.access;
885
886 /* we only support FL/FR channel mapping atm */
887 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
888 SNDRV_CHMAP_FL);
889 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
890 SNDRV_CHMAP_FR);
891 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
892 SNDRV_CHMAP_FL);
893 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
894 SNDRV_CHMAP_FR);
895
896 sm->max = mc->max;
897 sm->min = mc->min;
898 sm->invert = mc->invert;
899 sm->platform_max = mc->platform_max;
900 sm->dobj.index = tplg->index;
901 sm->dobj.ops = tplg->ops;
902 sm->dobj.type = SND_SOC_DOBJ_MIXER;
903 INIT_LIST_HEAD(&sm->dobj.list);
904
905 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800906 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100907 if (err) {
908 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
909 kfree(sm);
910 continue;
911 }
912
913 /* pass control to driver for optional further init */
914 err = soc_tplg_init_kcontrol(tplg, &kc,
915 (struct snd_soc_tplg_ctl_hdr *) mc);
916 if (err < 0) {
917 dev_err(tplg->dev, "ASoC: failed to init %s\n",
918 mc->hdr.name);
919 kfree(sm);
920 continue;
921 }
922
923 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100924 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100925
926 /* register control here */
927 err = soc_tplg_add_kcontrol(tplg, &kc,
928 &sm->dobj.control.kcontrol);
929 if (err < 0) {
930 dev_err(tplg->dev, "ASoC: failed to add %s\n",
931 mc->hdr.name);
932 soc_tplg_free_tlv(tplg, &kc);
933 kfree(sm);
934 continue;
935 }
936
937 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
938 }
939
940 return 0;
941}
942
943static int soc_tplg_denum_create_texts(struct soc_enum *se,
944 struct snd_soc_tplg_enum_control *ec)
945{
946 int i, ret;
947
948 se->dobj.control.dtexts =
949 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
950 if (se->dobj.control.dtexts == NULL)
951 return -ENOMEM;
952
953 for (i = 0; i < ec->items; i++) {
954
955 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
956 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
957 ret = -EINVAL;
958 goto err;
959 }
960
961 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
962 if (!se->dobj.control.dtexts[i]) {
963 ret = -ENOMEM;
964 goto err;
965 }
966 }
967
Mousumi Janab6e38b22017-04-11 13:06:22 +0530968 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100969 return 0;
970
971err:
972 for (--i; i >= 0; i--)
973 kfree(se->dobj.control.dtexts[i]);
974 kfree(se->dobj.control.dtexts);
975 return ret;
976}
977
978static int soc_tplg_denum_create_values(struct soc_enum *se,
979 struct snd_soc_tplg_enum_control *ec)
980{
981 if (ec->items > sizeof(*ec->values))
982 return -EINVAL;
983
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200984 se->dobj.control.dvalues = kmemdup(ec->values,
985 ec->items * sizeof(u32),
986 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100987 if (!se->dobj.control.dvalues)
988 return -ENOMEM;
989
Liam Girdwood8a978232015-05-29 19:06:14 +0100990 return 0;
991}
992
993static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
994 size_t size)
995{
996 struct snd_soc_tplg_enum_control *ec;
997 struct soc_enum *se;
998 struct snd_kcontrol_new kc;
999 int i, ret, err;
1000
1001 if (soc_tplg_check_elem_count(tplg,
1002 sizeof(struct snd_soc_tplg_enum_control),
1003 count, size, "enums")) {
1004
1005 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1006 count);
1007 return -EINVAL;
1008 }
1009
1010 for (i = 0; i < count; i++) {
1011 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1012 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1013 ec->priv.size);
1014
1015 /* validate kcontrol */
1016 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1017 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1018 return -EINVAL;
1019
1020 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1021 if (se == NULL)
1022 return -ENOMEM;
1023
1024 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1025 ec->hdr.name, ec->items);
1026
1027 memset(&kc, 0, sizeof(kc));
1028 kc.name = ec->hdr.name;
1029 kc.private_value = (long)se;
1030 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1031 kc.access = ec->hdr.access;
1032
1033 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1034 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1035 SNDRV_CHMAP_FL);
1036 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1037 SNDRV_CHMAP_FL);
1038
1039 se->items = ec->items;
1040 se->mask = ec->mask;
1041 se->dobj.index = tplg->index;
1042 se->dobj.type = SND_SOC_DOBJ_ENUM;
1043 se->dobj.ops = tplg->ops;
1044 INIT_LIST_HEAD(&se->dobj.list);
1045
1046 switch (ec->hdr.ops.info) {
1047 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1048 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1049 err = soc_tplg_denum_create_values(se, ec);
1050 if (err < 0) {
1051 dev_err(tplg->dev,
1052 "ASoC: could not create values for %s\n",
1053 ec->hdr.name);
1054 kfree(se);
1055 continue;
1056 }
1057 /* fall through and create texts */
1058 case SND_SOC_TPLG_CTL_ENUM:
1059 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1060 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1061 err = soc_tplg_denum_create_texts(se, ec);
1062 if (err < 0) {
1063 dev_err(tplg->dev,
1064 "ASoC: could not create texts for %s\n",
1065 ec->hdr.name);
1066 kfree(se);
1067 continue;
1068 }
1069 break;
1070 default:
1071 dev_err(tplg->dev,
1072 "ASoC: invalid enum control type %d for %s\n",
1073 ec->hdr.ops.info, ec->hdr.name);
1074 kfree(se);
1075 continue;
1076 }
1077
1078 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001079 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001080 if (err) {
1081 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1082 kfree(se);
1083 continue;
1084 }
1085
1086 /* pass control to driver for optional further init */
1087 err = soc_tplg_init_kcontrol(tplg, &kc,
1088 (struct snd_soc_tplg_ctl_hdr *) ec);
1089 if (err < 0) {
1090 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1091 ec->hdr.name);
1092 kfree(se);
1093 continue;
1094 }
1095
1096 /* register control here */
1097 ret = soc_tplg_add_kcontrol(tplg,
1098 &kc, &se->dobj.control.kcontrol);
1099 if (ret < 0) {
1100 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1101 ec->hdr.name);
1102 kfree(se);
1103 continue;
1104 }
1105
1106 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1107 }
1108
1109 return 0;
1110}
1111
1112static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1113 struct snd_soc_tplg_hdr *hdr)
1114{
1115 struct snd_soc_tplg_ctl_hdr *control_hdr;
1116 int i;
1117
1118 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1119 tplg->pos += hdr->size + hdr->payload_size;
1120 return 0;
1121 }
1122
1123 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1124 soc_tplg_get_offset(tplg));
1125
1126 for (i = 0; i < hdr->count; i++) {
1127
1128 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1129
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001130 if (control_hdr->size != sizeof(*control_hdr)) {
1131 dev_err(tplg->dev, "ASoC: invalid control size\n");
1132 return -EINVAL;
1133 }
1134
Liam Girdwood8a978232015-05-29 19:06:14 +01001135 switch (control_hdr->ops.info) {
1136 case SND_SOC_TPLG_CTL_VOLSW:
1137 case SND_SOC_TPLG_CTL_STROBE:
1138 case SND_SOC_TPLG_CTL_VOLSW_SX:
1139 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1140 case SND_SOC_TPLG_CTL_RANGE:
1141 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1142 case SND_SOC_TPLG_DAPM_CTL_PIN:
1143 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1144 break;
1145 case SND_SOC_TPLG_CTL_ENUM:
1146 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1147 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1148 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1149 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1150 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1151 break;
1152 case SND_SOC_TPLG_CTL_BYTES:
1153 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1154 break;
1155 default:
1156 soc_bind_err(tplg, control_hdr, i);
1157 return -EINVAL;
1158 }
1159 }
1160
1161 return 0;
1162}
1163
Liam Girdwood28aa6f72018-03-27 14:30:43 +01001164/* optionally pass new dynamic kcontrol to component driver. */
1165static int soc_tplg_add_route(struct soc_tplg *tplg,
1166 struct snd_soc_dapm_route *route)
1167{
1168 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1169 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1170 route);
1171
1172 return 0;
1173}
1174
Liam Girdwood8a978232015-05-29 19:06:14 +01001175static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1176 struct snd_soc_tplg_hdr *hdr)
1177{
1178 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1179 struct snd_soc_dapm_route route;
1180 struct snd_soc_tplg_dapm_graph_elem *elem;
1181 int count = hdr->count, i;
1182
1183 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1184 tplg->pos += hdr->size + hdr->payload_size;
1185 return 0;
1186 }
1187
1188 if (soc_tplg_check_elem_count(tplg,
1189 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1190 count, hdr->payload_size, "graph")) {
1191
1192 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1193 count);
1194 return -EINVAL;
1195 }
1196
Liam Girdwoodb75a6512017-06-29 14:22:26 +01001197 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1198 hdr->index);
Liam Girdwood8a978232015-05-29 19:06:14 +01001199
1200 for (i = 0; i < count; i++) {
1201 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1202 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1203
1204 /* validate routes */
1205 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1206 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1207 return -EINVAL;
1208 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1209 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1210 return -EINVAL;
1211 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1212 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1213 return -EINVAL;
1214
1215 route.source = elem->source;
1216 route.sink = elem->sink;
1217 route.connected = NULL; /* set to NULL atm for tplg users */
1218 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1219 route.control = NULL;
1220 else
1221 route.control = elem->control;
1222
Liam Girdwood28aa6f72018-03-27 14:30:43 +01001223 soc_tplg_add_route(tplg, &route);
1224
Liam Girdwood8a978232015-05-29 19:06:14 +01001225 /* add route, but keep going if some fail */
1226 snd_soc_dapm_add_routes(dapm, &route, 1);
1227 }
1228
1229 return 0;
1230}
1231
1232static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1233 struct soc_tplg *tplg, int num_kcontrols)
1234{
1235 struct snd_kcontrol_new *kc;
1236 struct soc_mixer_control *sm;
1237 struct snd_soc_tplg_mixer_control *mc;
1238 int i, err;
1239
Axel Lin4ca7deb2015-08-05 22:34:22 +08001240 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001241 if (kc == NULL)
1242 return NULL;
1243
1244 for (i = 0; i < num_kcontrols; i++) {
1245 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1246 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1247 if (sm == NULL)
1248 goto err;
1249
1250 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1251 mc->priv.size);
1252
1253 /* validate kcontrol */
1254 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1255 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1256 goto err_str;
1257
1258 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1259 mc->hdr.name, i);
1260
Liam Girdwood267e2c62018-03-27 12:04:04 +01001261 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1262 if (kc[i].name == NULL)
1263 goto err_str;
Liam Girdwood8a978232015-05-29 19:06:14 +01001264 kc[i].private_value = (long)sm;
1265 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1266 kc[i].access = mc->hdr.access;
1267
1268 /* we only support FL/FR channel mapping atm */
1269 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1270 SNDRV_CHMAP_FL);
1271 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1272 SNDRV_CHMAP_FR);
1273 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1274 SNDRV_CHMAP_FL);
1275 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1276 SNDRV_CHMAP_FR);
1277
1278 sm->max = mc->max;
1279 sm->min = mc->min;
1280 sm->invert = mc->invert;
1281 sm->platform_max = mc->platform_max;
1282 sm->dobj.index = tplg->index;
1283 INIT_LIST_HEAD(&sm->dobj.list);
1284
1285 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001286 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001287 if (err) {
1288 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1289 kfree(sm);
1290 continue;
1291 }
1292
1293 /* pass control to driver for optional further init */
1294 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1295 (struct snd_soc_tplg_ctl_hdr *)mc);
1296 if (err < 0) {
1297 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1298 mc->hdr.name);
1299 kfree(sm);
1300 continue;
1301 }
Ranjani Sridharanbde8b382018-03-09 11:11:17 -08001302
1303 /* create any TLV data */
1304 soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01001305 }
1306 return kc;
1307
1308err_str:
1309 kfree(sm);
1310err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001311 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001312 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001313 kfree(kc[i].name);
1314 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001315 kfree(kc);
1316 return NULL;
1317}
1318
1319static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001320 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001321{
1322 struct snd_kcontrol_new *kc;
1323 struct snd_soc_tplg_enum_control *ec;
1324 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001325 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001326
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001327 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001328 if (kc == NULL)
1329 return NULL;
1330
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001331 for (i = 0; i < num_kcontrols; i++) {
1332 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1333 /* validate kcontrol */
1334 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1335 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
Christophe JAILLETf3ee9092017-09-14 22:44:24 +02001336 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001337
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001338 se = kzalloc(sizeof(*se), GFP_KERNEL);
1339 if (se == NULL)
1340 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001341
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001342 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001343 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001344
Liam Girdwood267e2c62018-03-27 12:04:04 +01001345 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001346 if (kc[i].name == NULL) {
1347 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001348 goto err_se;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001349 }
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001350 kc[i].private_value = (long)se;
1351 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1352 kc[i].access = ec->hdr.access;
1353
1354 /* we only support FL/FR channel mapping atm */
1355 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1356 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1357 SNDRV_CHMAP_FL);
1358 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1359 SNDRV_CHMAP_FR);
1360
1361 se->items = ec->items;
1362 se->mask = ec->mask;
1363 se->dobj.index = tplg->index;
1364
1365 switch (ec->hdr.ops.info) {
1366 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1367 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1368 err = soc_tplg_denum_create_values(se, ec);
1369 if (err < 0) {
1370 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1371 ec->hdr.name);
1372 goto err_se;
1373 }
1374 /* fall through to create texts */
1375 case SND_SOC_TPLG_CTL_ENUM:
1376 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1377 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1378 err = soc_tplg_denum_create_texts(se, ec);
1379 if (err < 0) {
1380 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1381 ec->hdr.name);
1382 goto err_se;
1383 }
1384 break;
1385 default:
1386 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1387 ec->hdr.ops.info, ec->hdr.name);
1388 goto err_se;
1389 }
1390
1391 /* map io handlers */
1392 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1393 if (err) {
1394 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1395 goto err_se;
1396 }
1397
1398 /* pass control to driver for optional further init */
1399 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1400 (struct snd_soc_tplg_ctl_hdr *)ec);
1401 if (err < 0) {
1402 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1403 ec->hdr.name);
1404 goto err_se;
1405 }
1406
1407 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1408 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001409 }
1410
1411 return kc;
1412
1413err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001414 for (; i >= 0; i--) {
1415 /* free values and texts */
1416 se = (struct soc_enum *)kc[i].private_value;
Christophe JAILLET6d5574e2017-09-14 22:44:12 +02001417 if (!se)
1418 continue;
1419
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001420 kfree(se->dobj.control.dvalues);
1421 for (j = 0; j < ec->items; j++)
1422 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001423
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001424 kfree(se);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001425 kfree(kc[i].name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001426 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001427err:
1428 kfree(kc);
1429
1430 return NULL;
1431}
1432
1433static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1434 struct soc_tplg *tplg, int count)
1435{
1436 struct snd_soc_tplg_bytes_control *be;
1437 struct soc_bytes_ext *sbe;
1438 struct snd_kcontrol_new *kc;
1439 int i, err;
1440
Axel Lin4ca7deb2015-08-05 22:34:22 +08001441 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001442 if (!kc)
1443 return NULL;
1444
1445 for (i = 0; i < count; i++) {
1446 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1447
1448 /* validate kcontrol */
1449 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1450 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1451 goto err;
1452
1453 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1454 if (sbe == NULL)
1455 goto err;
1456
1457 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1458 be->priv.size);
1459
1460 dev_dbg(tplg->dev,
1461 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1462 be->hdr.name, be->hdr.access);
1463
Liam Girdwood267e2c62018-03-27 12:04:04 +01001464 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
Dan Carpenter65030ff2018-04-05 14:25:18 +03001465 if (kc[i].name == NULL) {
1466 kfree(sbe);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001467 goto err;
Dan Carpenter65030ff2018-04-05 14:25:18 +03001468 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001469 kc[i].private_value = (long)sbe;
1470 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1471 kc[i].access = be->hdr.access;
1472
1473 sbe->max = be->max;
1474 INIT_LIST_HEAD(&sbe->dobj.list);
1475
1476 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001477 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001478 if (err) {
1479 soc_control_err(tplg, &be->hdr, be->hdr.name);
1480 kfree(sbe);
1481 continue;
1482 }
1483
1484 /* pass control to driver for optional further init */
1485 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1486 (struct snd_soc_tplg_ctl_hdr *)be);
1487 if (err < 0) {
1488 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1489 be->hdr.name);
1490 kfree(sbe);
1491 continue;
1492 }
1493 }
1494
1495 return kc;
1496
1497err:
Liam Girdwood267e2c62018-03-27 12:04:04 +01001498 for (--i; i >= 0; i--) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001499 kfree((void *)kc[i].private_value);
Liam Girdwood267e2c62018-03-27 12:04:04 +01001500 kfree(kc[i].name);
1501 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001502
1503 kfree(kc);
1504 return NULL;
1505}
1506
1507static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1508 struct snd_soc_tplg_dapm_widget *w)
1509{
1510 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1511 struct snd_soc_dapm_widget template, *widget;
1512 struct snd_soc_tplg_ctl_hdr *control_hdr;
1513 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001514 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001515 int ret = 0;
1516
1517 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1518 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1519 return -EINVAL;
1520 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1521 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1522 return -EINVAL;
1523
1524 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1525 w->name, w->id);
1526
1527 memset(&template, 0, sizeof(template));
1528
1529 /* map user to kernel widget ID */
1530 template.id = get_widget_id(w->id);
1531 if (template.id < 0)
1532 return template.id;
1533
Liam Girdwoodc3421a62017-06-06 15:45:09 +01001534 /* strings are allocated here, but used and freed by the widget */
Liam Girdwood8a978232015-05-29 19:06:14 +01001535 template.name = kstrdup(w->name, GFP_KERNEL);
1536 if (!template.name)
1537 return -ENOMEM;
1538 template.sname = kstrdup(w->sname, GFP_KERNEL);
1539 if (!template.sname) {
1540 ret = -ENOMEM;
1541 goto err;
1542 }
1543 template.reg = w->reg;
1544 template.shift = w->shift;
1545 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001546 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001547 template.on_val = w->invert ? 0 : 1;
1548 template.off_val = w->invert ? 1 : 0;
1549 template.ignore_suspend = w->ignore_suspend;
1550 template.event_flags = w->event_flags;
1551 template.dobj.index = tplg->index;
1552
1553 tplg->pos +=
1554 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1555 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001556 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001557 template.num_kcontrols = 0;
1558 goto widget;
1559 }
1560
1561 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1562 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1563 w->name, w->num_kcontrols, control_hdr->type);
1564
1565 switch (control_hdr->ops.info) {
1566 case SND_SOC_TPLG_CTL_VOLSW:
1567 case SND_SOC_TPLG_CTL_STROBE:
1568 case SND_SOC_TPLG_CTL_VOLSW_SX:
1569 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1570 case SND_SOC_TPLG_CTL_RANGE:
1571 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001572 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001573 template.num_kcontrols = w->num_kcontrols;
1574 template.kcontrol_news =
1575 soc_tplg_dapm_widget_dmixer_create(tplg,
1576 template.num_kcontrols);
1577 if (!template.kcontrol_news) {
1578 ret = -ENOMEM;
1579 goto hdr_err;
1580 }
1581 break;
1582 case SND_SOC_TPLG_CTL_ENUM:
1583 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1584 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1585 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1586 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001587 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001588 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001589 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001590 soc_tplg_dapm_widget_denum_create(tplg,
1591 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001592 if (!template.kcontrol_news) {
1593 ret = -ENOMEM;
1594 goto hdr_err;
1595 }
1596 break;
1597 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001598 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001599 template.num_kcontrols = w->num_kcontrols;
1600 template.kcontrol_news =
1601 soc_tplg_dapm_widget_dbytes_create(tplg,
1602 template.num_kcontrols);
1603 if (!template.kcontrol_news) {
1604 ret = -ENOMEM;
1605 goto hdr_err;
1606 }
1607 break;
1608 default:
1609 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1610 control_hdr->ops.get, control_hdr->ops.put,
1611 control_hdr->ops.info);
1612 ret = -EINVAL;
1613 goto hdr_err;
1614 }
1615
1616widget:
1617 ret = soc_tplg_widget_load(tplg, &template, w);
1618 if (ret < 0)
1619 goto hdr_err;
1620
1621 /* card dapm mutex is held by the core if we are loading topology
1622 * data during sound card init. */
1623 if (card->instantiated)
1624 widget = snd_soc_dapm_new_control(dapm, &template);
1625 else
1626 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001627 if (IS_ERR(widget)) {
1628 ret = PTR_ERR(widget);
1629 /* Do not nag about probe deferrals */
1630 if (ret != -EPROBE_DEFER)
1631 dev_err(tplg->dev,
1632 "ASoC: failed to create widget %s controls (%d)\n",
1633 w->name, ret);
1634 goto hdr_err;
1635 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001636 if (widget == NULL) {
1637 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1638 w->name);
Wei Yongjun8ae3ea42016-08-10 13:43:12 +00001639 ret = -ENOMEM;
Liam Girdwood8a978232015-05-29 19:06:14 +01001640 goto hdr_err;
1641 }
1642
1643 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001644 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001645 widget->dobj.ops = tplg->ops;
1646 widget->dobj.index = tplg->index;
1647 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001648
1649 ret = soc_tplg_widget_ready(tplg, widget, w);
1650 if (ret < 0)
1651 goto ready_err;
1652
Liam Girdwood8a978232015-05-29 19:06:14 +01001653 return 0;
1654
Liam Girdwoodebd259d2017-06-09 15:43:23 +01001655ready_err:
1656 snd_soc_tplg_widget_remove(widget);
1657 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001658hdr_err:
1659 kfree(template.sname);
1660err:
1661 kfree(template.name);
1662 return ret;
1663}
1664
1665static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1666 struct snd_soc_tplg_hdr *hdr)
1667{
1668 struct snd_soc_tplg_dapm_widget *widget;
1669 int ret, count = hdr->count, i;
1670
1671 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1672 return 0;
1673
1674 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1675
1676 for (i = 0; i < count; i++) {
1677 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001678 if (widget->size != sizeof(*widget)) {
1679 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1680 return -EINVAL;
1681 }
1682
Liam Girdwood8a978232015-05-29 19:06:14 +01001683 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001684 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001685 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1686 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001687 return ret;
1688 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001689 }
1690
1691 return 0;
1692}
1693
1694static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1695{
1696 struct snd_soc_card *card = tplg->comp->card;
1697 int ret;
1698
1699 /* Card might not have been registered at this point.
1700 * If so, just return success.
1701 */
1702 if (!card || !card->instantiated) {
1703 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001704 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001705 return 0;
1706 }
1707
1708 ret = snd_soc_dapm_new_widgets(card);
1709 if (ret < 0)
1710 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1711 ret);
1712
1713 return 0;
1714}
1715
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001716static void set_stream_info(struct snd_soc_pcm_stream *stream,
1717 struct snd_soc_tplg_stream_caps *caps)
1718{
1719 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1720 stream->channels_min = caps->channels_min;
1721 stream->channels_max = caps->channels_max;
1722 stream->rates = caps->rates;
1723 stream->rate_min = caps->rate_min;
1724 stream->rate_max = caps->rate_max;
1725 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001726 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001727}
1728
Mengdong Lin0038be92016-07-26 14:32:37 +08001729static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1730 unsigned int flag_mask, unsigned int flags)
1731{
1732 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1733 dai_drv->symmetric_rates =
1734 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1735
1736 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1737 dai_drv->symmetric_channels =
1738 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1739 1 : 0;
1740
1741 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1742 dai_drv->symmetric_samplebits =
1743 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1744 1 : 0;
1745}
1746
Mengdong Lin64527e82016-01-15 16:13:28 +08001747static int soc_tplg_dai_create(struct soc_tplg *tplg,
1748 struct snd_soc_tplg_pcm *pcm)
1749{
1750 struct snd_soc_dai_driver *dai_drv;
1751 struct snd_soc_pcm_stream *stream;
1752 struct snd_soc_tplg_stream_caps *caps;
1753 int ret;
1754
1755 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1756 if (dai_drv == NULL)
1757 return -ENOMEM;
1758
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001759 if (strlen(pcm->dai_name))
1760 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001761 dai_drv->id = pcm->dai_id;
1762
1763 if (pcm->playback) {
1764 stream = &dai_drv->playback;
1765 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001766 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001767 }
1768
1769 if (pcm->capture) {
1770 stream = &dai_drv->capture;
1771 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001772 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001773 }
1774
Liam Girdwood5db6aab2018-03-27 14:30:45 +01001775 if (pcm->compress)
1776 dai_drv->compress_new = snd_soc_new_compress;
1777
Mengdong Lin64527e82016-01-15 16:13:28 +08001778 /* pass control to component driver for optional further init */
Liam Girdwood81e9b0a2018-03-27 14:30:42 +01001779 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001780 if (ret < 0) {
1781 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1782 kfree(dai_drv);
1783 return ret;
1784 }
1785
1786 dai_drv->dobj.index = tplg->index;
1787 dai_drv->dobj.ops = tplg->ops;
1788 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1789 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1790
1791 /* register the DAI to the component */
1792 return snd_soc_register_dai(tplg->comp, dai_drv);
1793}
1794
Mengdong Lin717a8e72016-11-03 01:03:34 +08001795static void set_link_flags(struct snd_soc_dai_link *link,
1796 unsigned int flag_mask, unsigned int flags)
1797{
1798 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1799 link->symmetric_rates =
1800 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1801
1802 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1803 link->symmetric_channels =
1804 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1805 1 : 0;
1806
1807 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1808 link->symmetric_samplebits =
1809 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1810 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001811
1812 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1813 link->ignore_suspend =
1814 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1815 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001816}
1817
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001818/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001819static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001820 struct snd_soc_tplg_pcm *pcm)
1821{
1822 struct snd_soc_dai_link *link;
1823 int ret;
1824
1825 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1826 if (link == NULL)
1827 return -ENOMEM;
1828
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001829 if (strlen(pcm->pcm_name)) {
1830 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1831 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1832 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001833 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001834
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001835 if (strlen(pcm->dai_name))
1836 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1837
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001838 link->codec_name = "snd-soc-dummy";
1839 link->codec_dai_name = "snd-soc-dummy-dai";
1840
1841 /* enable DPCM */
1842 link->dynamic = 1;
1843 link->dpcm_playback = pcm->playback;
1844 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001845 if (pcm->flag_mask)
1846 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001847
Mengdong Linacfc7d42016-01-15 16:13:37 +08001848 /* pass control to component driver for optional further init */
Liam Girdwood81e9b0a2018-03-27 14:30:42 +01001849 ret = soc_tplg_dai_link_load(tplg, link, NULL);
Mengdong Linacfc7d42016-01-15 16:13:37 +08001850 if (ret < 0) {
1851 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1852 kfree(link);
1853 return ret;
1854 }
1855
1856 link->dobj.index = tplg->index;
1857 link->dobj.ops = tplg->ops;
1858 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1859 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1860
1861 snd_soc_add_dai_link(tplg->comp->card, link);
1862 return 0;
1863}
1864
1865/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001866static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1867 struct snd_soc_tplg_pcm *pcm)
1868{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001869 int ret;
1870
1871 ret = soc_tplg_dai_create(tplg, pcm);
1872 if (ret < 0)
1873 return ret;
1874
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001875 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001876}
1877
Mengdong Lin55726dc2016-11-03 01:00:16 +08001878/* copy stream caps from the old version 4 of source */
1879static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1880 struct snd_soc_tplg_stream_caps_v4 *src)
1881{
1882 dest->size = sizeof(*dest);
1883 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1884 dest->formats = src->formats;
1885 dest->rates = src->rates;
1886 dest->rate_min = src->rate_min;
1887 dest->rate_max = src->rate_max;
1888 dest->channels_min = src->channels_min;
1889 dest->channels_max = src->channels_max;
1890 dest->periods_min = src->periods_min;
1891 dest->periods_max = src->periods_max;
1892 dest->period_size_min = src->period_size_min;
1893 dest->period_size_max = src->period_size_max;
1894 dest->buffer_size_min = src->buffer_size_min;
1895 dest->buffer_size_max = src->buffer_size_max;
1896}
1897
1898/**
1899 * pcm_new_ver - Create the new version of PCM from the old version.
1900 * @tplg: topology context
1901 * @src: older version of pcm as a source
1902 * @pcm: latest version of pcm created from the source
1903 *
1904 * Support from vesion 4. User should free the returned pcm manually.
1905 */
1906static int pcm_new_ver(struct soc_tplg *tplg,
1907 struct snd_soc_tplg_pcm *src,
1908 struct snd_soc_tplg_pcm **pcm)
1909{
1910 struct snd_soc_tplg_pcm *dest;
1911 struct snd_soc_tplg_pcm_v4 *src_v4;
1912 int i;
1913
1914 *pcm = NULL;
1915
1916 if (src->size != sizeof(*src_v4)) {
1917 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1918 return -EINVAL;
1919 }
1920
1921 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1922 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1923 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1924 if (!dest)
1925 return -ENOMEM;
1926
1927 dest->size = sizeof(*dest); /* size of latest abi version */
1928 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1929 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1930 dest->pcm_id = src_v4->pcm_id;
1931 dest->dai_id = src_v4->dai_id;
1932 dest->playback = src_v4->playback;
1933 dest->capture = src_v4->capture;
1934 dest->compress = src_v4->compress;
1935 dest->num_streams = src_v4->num_streams;
1936 for (i = 0; i < dest->num_streams; i++)
1937 memcpy(&dest->stream[i], &src_v4->stream[i],
1938 sizeof(struct snd_soc_tplg_stream));
1939
1940 for (i = 0; i < 2; i++)
1941 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1942
1943 *pcm = dest;
1944 return 0;
1945}
1946
Mengdong Lin64527e82016-01-15 16:13:28 +08001947static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001948 struct snd_soc_tplg_hdr *hdr)
1949{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001950 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001951 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301952 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001953 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001954
1955 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1956 return 0;
1957
Mengdong Lin55726dc2016-11-03 01:00:16 +08001958 /* check the element size and count */
1959 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1960 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1961 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1962 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1963 pcm->size);
1964 return -EINVAL;
1965 }
1966
Liam Girdwood8a978232015-05-29 19:06:14 +01001967 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001968 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001969 hdr->payload_size, "PCM DAI")) {
1970 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1971 count);
1972 return -EINVAL;
1973 }
1974
Mengdong Lin64527e82016-01-15 16:13:28 +08001975 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001976 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1977
1978 /* check ABI version by size, create a new version of pcm
1979 * if abi not match.
1980 */
1981 if (pcm->size == sizeof(*pcm)) {
1982 abi_match = true;
1983 _pcm = pcm;
1984 } else {
1985 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05301986 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001987 }
1988
Mengdong Lin55726dc2016-11-03 01:00:16 +08001989 /* create the FE DAIs and DAI links */
1990 soc_tplg_pcm_create(tplg, _pcm);
1991
Mengdong Lin717a8e72016-11-03 01:03:34 +08001992 /* offset by version-specific struct size and
1993 * real priv data size
1994 */
1995 tplg->pos += pcm->size + _pcm->priv.size;
1996
Mengdong Lin55726dc2016-11-03 01:00:16 +08001997 if (!abi_match)
1998 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08001999 }
2000
Liam Girdwood8a978232015-05-29 19:06:14 +01002001 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01002002
Liam Girdwood8a978232015-05-29 19:06:14 +01002003 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01002004}
2005
Mengdong Lin593d9e52016-11-03 01:04:27 +08002006/**
2007 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00002008 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08002009 * @cfg: physical link configs.
2010 *
2011 * Topology context contains a list of supported HW formats (configs) and
2012 * a default format ID for the physical link. This function will use this
2013 * default ID to choose the HW format to set the link's DAI format for init.
2014 */
2015static void set_link_hw_format(struct snd_soc_dai_link *link,
2016 struct snd_soc_tplg_link_config *cfg)
2017{
2018 struct snd_soc_tplg_hw_config *hw_config;
2019 unsigned char bclk_master, fsync_master;
2020 unsigned char invert_bclk, invert_fsync;
2021 int i;
2022
2023 for (i = 0; i < cfg->num_hw_configs; i++) {
2024 hw_config = &cfg->hw_config[i];
2025 if (hw_config->id != cfg->default_hw_config_id)
2026 continue;
2027
2028 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
2029
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002030 /* clock gating */
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002031 switch (hw_config->clock_gated) {
2032 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002033 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002034 break;
2035
2036 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002037 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
Kirill Marinushkinfbeabd02018-04-16 19:56:44 +02002038 break;
2039
2040 default:
2041 /* ignore the value */
2042 break;
2043 }
Kirill Marinushkin933e1c42018-04-04 06:19:38 +02002044
Mengdong Lin593d9e52016-11-03 01:04:27 +08002045 /* clock signal polarity */
2046 invert_bclk = hw_config->invert_bclk;
2047 invert_fsync = hw_config->invert_fsync;
2048 if (!invert_bclk && !invert_fsync)
2049 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2050 else if (!invert_bclk && invert_fsync)
2051 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2052 else if (invert_bclk && !invert_fsync)
2053 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2054 else
2055 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2056
2057 /* clock masters */
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002058 bclk_master = (hw_config->bclk_master ==
2059 SND_SOC_TPLG_BCLK_CM);
2060 fsync_master = (hw_config->fsync_master ==
2061 SND_SOC_TPLG_FSYNC_CM);
2062 if (bclk_master && fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002063 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002064 else if (!bclk_master && fsync_master)
Kirill Marinushkina941e2f2018-04-04 06:19:37 +02002065 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2066 else if (bclk_master && !fsync_master)
Mengdong Lin593d9e52016-11-03 01:04:27 +08002067 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2068 else
2069 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2070 }
2071}
2072
2073/**
2074 * link_new_ver - Create a new physical link config from the old
2075 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002076 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002077 * @src: old version of phyical link config as a source
2078 * @link: latest version of physical link config created from the source
2079 *
2080 * Support from vesion 4. User need free the returned link config manually.
2081 */
2082static int link_new_ver(struct soc_tplg *tplg,
2083 struct snd_soc_tplg_link_config *src,
2084 struct snd_soc_tplg_link_config **link)
2085{
2086 struct snd_soc_tplg_link_config *dest;
2087 struct snd_soc_tplg_link_config_v4 *src_v4;
2088 int i;
2089
2090 *link = NULL;
2091
2092 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2093 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2094 return -EINVAL;
2095 }
2096
2097 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2098
2099 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2100 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2101 if (!dest)
2102 return -ENOMEM;
2103
2104 dest->size = sizeof(*dest);
2105 dest->id = src_v4->id;
2106 dest->num_streams = src_v4->num_streams;
2107 for (i = 0; i < dest->num_streams; i++)
2108 memcpy(&dest->stream[i], &src_v4->stream[i],
2109 sizeof(struct snd_soc_tplg_stream));
2110
2111 *link = dest;
2112 return 0;
2113}
2114
2115/* Find and configure an existing physical DAI link */
2116static int soc_tplg_link_config(struct soc_tplg *tplg,
2117 struct snd_soc_tplg_link_config *cfg)
2118{
2119 struct snd_soc_dai_link *link;
2120 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002121 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002122 int ret;
2123
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002124 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2125 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2126 return -EINVAL;
2127 else if (len)
2128 name = cfg->name;
2129 else
2130 name = NULL;
2131
2132 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2133 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2134 return -EINVAL;
2135 else if (len)
2136 stream_name = cfg->stream_name;
2137 else
2138 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002139
2140 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2141 name, stream_name);
2142 if (!link) {
2143 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2144 name, cfg->id);
2145 return -EINVAL;
2146 }
2147
2148 /* hw format */
2149 if (cfg->num_hw_configs)
2150 set_link_hw_format(link, cfg);
2151
2152 /* flags */
2153 if (cfg->flag_mask)
2154 set_link_flags(link, cfg->flag_mask, cfg->flags);
2155
2156 /* pass control to component driver for optional further init */
Liam Girdwood81e9b0a2018-03-27 14:30:42 +01002157 ret = soc_tplg_dai_link_load(tplg, link, cfg);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002158 if (ret < 0) {
2159 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2160 return ret;
2161 }
2162
2163 return 0;
2164}
2165
2166
2167/* Load physical link config elements from the topology context */
2168static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2169 struct snd_soc_tplg_hdr *hdr)
2170{
2171 struct snd_soc_tplg_link_config *link, *_link;
2172 int count = hdr->count;
2173 int i, ret;
2174 bool abi_match;
2175
2176 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2177 tplg->pos += hdr->size + hdr->payload_size;
2178 return 0;
2179 };
2180
2181 /* check the element size and count */
2182 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2183 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2184 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2185 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2186 link->size);
2187 return -EINVAL;
2188 }
2189
2190 if (soc_tplg_check_elem_count(tplg,
2191 link->size, count,
2192 hdr->payload_size, "physical link config")) {
2193 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2194 count);
2195 return -EINVAL;
2196 }
2197
2198 /* config physical DAI links */
2199 for (i = 0; i < count; i++) {
2200 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2201 if (link->size == sizeof(*link)) {
2202 abi_match = true;
2203 _link = link;
2204 } else {
2205 abi_match = false;
2206 ret = link_new_ver(tplg, link, &_link);
2207 if (ret < 0)
2208 return ret;
2209 }
2210
2211 ret = soc_tplg_link_config(tplg, _link);
2212 if (ret < 0)
2213 return ret;
2214
2215 /* offset by version-specific struct size and
2216 * real priv data size
2217 */
2218 tplg->pos += link->size + _link->priv.size;
2219
2220 if (!abi_match)
2221 kfree(_link); /* free the duplicated one */
2222 }
2223
2224 return 0;
2225}
2226
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002227/**
2228 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002229 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002230 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002231 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002232 * The physical dai should already be registered by the platform driver.
2233 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002234 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002235static int soc_tplg_dai_config(struct soc_tplg *tplg,
2236 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002237{
2238 struct snd_soc_dai_link_component dai_component = {0};
2239 struct snd_soc_dai *dai;
2240 struct snd_soc_dai_driver *dai_drv;
2241 struct snd_soc_pcm_stream *stream;
2242 struct snd_soc_tplg_stream_caps *caps;
2243 int ret;
2244
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002245 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002246 dai = snd_soc_find_dai(&dai_component);
2247 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002248 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2249 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002250 return -EINVAL;
2251 }
2252
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002253 if (d->dai_id != dai->id) {
2254 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2255 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002256 return -EINVAL;
2257 }
2258
2259 dai_drv = dai->driver;
2260 if (!dai_drv)
2261 return -EINVAL;
2262
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002263 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002264 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002265 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002266 set_stream_info(stream, caps);
2267 }
2268
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002269 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002270 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002271 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002272 set_stream_info(stream, caps);
2273 }
2274
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002275 if (d->flag_mask)
2276 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002277
2278 /* pass control to component driver for optional further init */
Liam Girdwood81e9b0a2018-03-27 14:30:42 +01002279 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
Mengdong Lin0038be92016-07-26 14:32:37 +08002280 if (ret < 0) {
2281 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2282 return ret;
2283 }
2284
2285 return 0;
2286}
2287
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002288/* load physical DAI elements */
2289static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2290 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002291{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002292 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002293 int count = hdr->count;
2294 int i;
2295
2296 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2297 return 0;
2298
2299 /* config the existing BE DAIs */
2300 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002301 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2302 if (dai->size != sizeof(*dai)) {
2303 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002304 return -EINVAL;
2305 }
2306
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002307 soc_tplg_dai_config(tplg, dai);
2308 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002309 }
2310
2311 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2312 return 0;
2313}
2314
Mengdong Lin583958f2016-10-11 14:36:42 +08002315/**
2316 * manifest_new_ver - Create a new version of manifest from the old version
2317 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002318 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002319 * @src: old version of manifest as a source
2320 * @manifest: latest version of manifest created from the source
2321 *
2322 * Support from vesion 4. Users need free the returned manifest manually.
2323 */
2324static int manifest_new_ver(struct soc_tplg *tplg,
2325 struct snd_soc_tplg_manifest *src,
2326 struct snd_soc_tplg_manifest **manifest)
2327{
2328 struct snd_soc_tplg_manifest *dest;
2329 struct snd_soc_tplg_manifest_v4 *src_v4;
2330
2331 *manifest = NULL;
2332
2333 if (src->size != sizeof(*src_v4)) {
2334 dev_err(tplg->dev, "ASoC: invalid manifest size\n");
2335 return -EINVAL;
2336 }
2337
2338 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2339
2340 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2341 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2342 if (!dest)
2343 return -ENOMEM;
2344
2345 dest->size = sizeof(*dest); /* size of latest abi version */
2346 dest->control_elems = src_v4->control_elems;
2347 dest->widget_elems = src_v4->widget_elems;
2348 dest->graph_elems = src_v4->graph_elems;
2349 dest->pcm_elems = src_v4->pcm_elems;
2350 dest->dai_link_elems = src_v4->dai_link_elems;
2351 dest->priv.size = src_v4->priv.size;
2352 if (dest->priv.size)
2353 memcpy(dest->priv.data, src_v4->priv.data,
2354 src_v4->priv.size);
2355
2356 *manifest = dest;
2357 return 0;
2358}
Mengdong Lin0038be92016-07-26 14:32:37 +08002359
Liam Girdwood8a978232015-05-29 19:06:14 +01002360static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002361 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002362{
Mengdong Lin583958f2016-10-11 14:36:42 +08002363 struct snd_soc_tplg_manifest *manifest, *_manifest;
2364 bool abi_match;
2365 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002366
2367 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2368 return 0;
2369
2370 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002371
2372 /* check ABI version by size, create a new manifest if abi not match */
2373 if (manifest->size == sizeof(*manifest)) {
2374 abi_match = true;
2375 _manifest = manifest;
2376 } else {
2377 abi_match = false;
2378 err = manifest_new_ver(tplg, manifest, &_manifest);
2379 if (err < 0)
2380 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002381 }
2382
Mengdong Lin583958f2016-10-11 14:36:42 +08002383 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002384 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Liam Girdwood81e9b0a2018-03-27 14:30:42 +01002385 return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002386
Mengdong Lin583958f2016-10-11 14:36:42 +08002387 if (!abi_match) /* free the duplicated one */
2388 kfree(_manifest);
2389
Liam Girdwood8a978232015-05-29 19:06:14 +01002390 return 0;
2391}
2392
2393/* validate header magic, size and type */
2394static int soc_valid_header(struct soc_tplg *tplg,
2395 struct snd_soc_tplg_hdr *hdr)
2396{
2397 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2398 return 0;
2399
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002400 if (hdr->size != sizeof(*hdr)) {
2401 dev_err(tplg->dev,
2402 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2403 hdr->type, soc_tplg_get_hdr_offset(tplg),
2404 tplg->fw->size);
2405 return -EINVAL;
2406 }
2407
Liam Girdwood8a978232015-05-29 19:06:14 +01002408 /* big endian firmware objects not supported atm */
2409 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2410 dev_err(tplg->dev,
2411 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2412 tplg->pass, hdr->magic,
2413 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2414 return -EINVAL;
2415 }
2416
2417 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2418 dev_err(tplg->dev,
2419 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2420 tplg->pass, hdr->magic,
2421 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2422 return -EINVAL;
2423 }
2424
Mengdong Lin288b8da2016-11-03 01:03:17 +08002425 /* Support ABI from version 4 */
2426 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2427 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002428 dev_err(tplg->dev,
2429 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2430 tplg->pass, hdr->abi,
2431 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2432 tplg->fw->size);
2433 return -EINVAL;
2434 }
2435
2436 if (hdr->payload_size == 0) {
2437 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2438 soc_tplg_get_hdr_offset(tplg));
2439 return -EINVAL;
2440 }
2441
2442 if (tplg->pass == hdr->type)
2443 dev_dbg(tplg->dev,
2444 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2445 hdr->payload_size, hdr->type, hdr->version,
2446 hdr->vendor_type, tplg->pass);
2447
2448 return 1;
2449}
2450
2451/* check header type and call appropriate handler */
2452static int soc_tplg_load_header(struct soc_tplg *tplg,
2453 struct snd_soc_tplg_hdr *hdr)
2454{
2455 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2456
2457 /* check for matching ID */
2458 if (hdr->index != tplg->req_index &&
Liam Girdwoodbb971422017-06-29 14:22:25 +01002459 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
Liam Girdwood8a978232015-05-29 19:06:14 +01002460 return 0;
2461
2462 tplg->index = hdr->index;
2463
2464 switch (hdr->type) {
2465 case SND_SOC_TPLG_TYPE_MIXER:
2466 case SND_SOC_TPLG_TYPE_ENUM:
2467 case SND_SOC_TPLG_TYPE_BYTES:
2468 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2469 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2470 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2471 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2472 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2473 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002474 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002475 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002476 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002477 case SND_SOC_TPLG_TYPE_DAI_LINK:
2478 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2479 /* physical link configurations */
2480 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002481 case SND_SOC_TPLG_TYPE_MANIFEST:
2482 return soc_tplg_manifest_load(tplg, hdr);
2483 default:
2484 /* bespoke vendor data object */
2485 return soc_tplg_vendor_load(tplg, hdr);
2486 }
2487
2488 return 0;
2489}
2490
2491/* process the topology file headers */
2492static int soc_tplg_process_headers(struct soc_tplg *tplg)
2493{
2494 struct snd_soc_tplg_hdr *hdr;
2495 int ret;
2496
2497 tplg->pass = SOC_TPLG_PASS_START;
2498
2499 /* process the header types from start to end */
2500 while (tplg->pass <= SOC_TPLG_PASS_END) {
2501
2502 tplg->hdr_pos = tplg->fw->data;
2503 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2504
2505 while (!soc_tplg_is_eof(tplg)) {
2506
2507 /* make sure header is valid before loading */
2508 ret = soc_valid_header(tplg, hdr);
2509 if (ret < 0)
2510 return ret;
2511 else if (ret == 0)
2512 break;
2513
2514 /* load the header object */
2515 ret = soc_tplg_load_header(tplg, hdr);
2516 if (ret < 0)
2517 return ret;
2518
2519 /* goto next header */
2520 tplg->hdr_pos += hdr->payload_size +
2521 sizeof(struct snd_soc_tplg_hdr);
2522 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2523 }
2524
2525 /* next data type pass */
2526 tplg->pass++;
2527 }
2528
2529 /* signal DAPM we are complete */
2530 ret = soc_tplg_dapm_complete(tplg);
2531 if (ret < 0)
2532 dev_err(tplg->dev,
2533 "ASoC: failed to initialise DAPM from Firmware\n");
2534
2535 return ret;
2536}
2537
2538static int soc_tplg_load(struct soc_tplg *tplg)
2539{
2540 int ret;
2541
2542 ret = soc_tplg_process_headers(tplg);
2543 if (ret == 0)
2544 soc_tplg_complete(tplg);
2545
2546 return ret;
2547}
2548
2549/* load audio component topology from "firmware" file */
2550int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2551 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2552{
2553 struct soc_tplg tplg;
2554
2555 /* setup parsing context */
2556 memset(&tplg, 0, sizeof(tplg));
2557 tplg.fw = fw;
2558 tplg.dev = comp->dev;
2559 tplg.comp = comp;
2560 tplg.ops = ops;
2561 tplg.req_index = id;
2562 tplg.io_ops = ops->io_ops;
2563 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002564 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2565 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002566
2567 return soc_tplg_load(&tplg);
2568}
2569EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2570
2571/* remove this dynamic widget */
2572void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2573{
2574 /* make sure we are a widget */
2575 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2576 return;
2577
2578 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2579}
2580EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2581
2582/* remove all dynamic widgets from this DAPM context */
2583void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2584 u32 index)
2585{
2586 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002587
2588 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2589
2590 /* make sure we are a widget with correct context */
2591 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2592 continue;
2593
2594 /* match ID */
2595 if (w->dobj.index != index &&
2596 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2597 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002598 /* check and free and dynamic widget kcontrols */
2599 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002600 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002601 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002602 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002603}
2604EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2605
2606/* remove dynamic controls from the component driver */
2607int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2608{
2609 struct snd_soc_dobj *dobj, *next_dobj;
2610 int pass = SOC_TPLG_PASS_END;
2611
2612 /* process the header types from end to start */
2613 while (pass >= SOC_TPLG_PASS_START) {
2614
2615 /* remove mixer controls */
2616 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2617 list) {
2618
2619 /* match index */
2620 if (dobj->index != index &&
2621 dobj->index != SND_SOC_TPLG_INDEX_ALL)
2622 continue;
2623
2624 switch (dobj->type) {
2625 case SND_SOC_DOBJ_MIXER:
2626 remove_mixer(comp, dobj, pass);
2627 break;
2628 case SND_SOC_DOBJ_ENUM:
2629 remove_enum(comp, dobj, pass);
2630 break;
2631 case SND_SOC_DOBJ_BYTES:
2632 remove_bytes(comp, dobj, pass);
2633 break;
2634 case SND_SOC_DOBJ_WIDGET:
2635 remove_widget(comp, dobj, pass);
2636 break;
2637 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002638 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002639 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002640 case SND_SOC_DOBJ_DAI_LINK:
2641 remove_link(comp, dobj, pass);
2642 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002643 default:
2644 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2645 dobj->type);
2646 break;
2647 }
2648 }
2649 pass--;
2650 }
2651
2652 /* let caller know if FW can be freed when no objects are left */
2653 return !list_empty(&comp->dobj_list);
2654}
2655EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);