blob: f24d1f2e82a087fb9d549f79036d042669aa7150 [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},
245};
246
247static int tplc_chan_get_reg(struct soc_tplg *tplg,
248 struct snd_soc_tplg_channel *chan, int map)
249{
250 int i;
251
252 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
253 if (chan[i].id == map)
254 return chan[i].reg;
255 }
256
257 return -EINVAL;
258}
259
260static int tplc_chan_get_shift(struct soc_tplg *tplg,
261 struct snd_soc_tplg_channel *chan, int map)
262{
263 int i;
264
265 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
266 if (chan[i].id == map)
267 return chan[i].shift;
268 }
269
270 return -EINVAL;
271}
272
273static int get_widget_id(int tplg_type)
274{
275 int i;
276
277 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
278 if (tplg_type == dapm_map[i].uid)
279 return dapm_map[i].kid;
280 }
281
282 return -EINVAL;
283}
284
Liam Girdwood8a978232015-05-29 19:06:14 +0100285static inline void soc_bind_err(struct soc_tplg *tplg,
286 struct snd_soc_tplg_ctl_hdr *hdr, int index)
287{
288 dev_err(tplg->dev,
289 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
290 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
291 soc_tplg_get_offset(tplg));
292}
293
294static inline void soc_control_err(struct soc_tplg *tplg,
295 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
296{
297 dev_err(tplg->dev,
298 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
299 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
300 soc_tplg_get_offset(tplg));
301}
302
303/* pass vendor data to component driver for processing */
304static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
305 struct snd_soc_tplg_hdr *hdr)
306{
307 int ret = 0;
308
309 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
310 ret = tplg->ops->vendor_load(tplg->comp, hdr);
311 else {
312 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
313 hdr->vendor_type);
314 return -EINVAL;
315 }
316
317 if (ret < 0)
318 dev_err(tplg->dev,
319 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
320 soc_tplg_get_hdr_offset(tplg),
321 soc_tplg_get_hdr_offset(tplg),
322 hdr->type, hdr->vendor_type);
323 return ret;
324}
325
326/* pass vendor data to component driver for processing */
327static int soc_tplg_vendor_load(struct soc_tplg *tplg,
328 struct snd_soc_tplg_hdr *hdr)
329{
330 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
331 return 0;
332
333 return soc_tplg_vendor_load_(tplg, hdr);
334}
335
336/* optionally pass new dynamic widget to component driver. This is mainly for
337 * external widgets where we can assign private data/ops */
338static int soc_tplg_widget_load(struct soc_tplg *tplg,
339 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
340{
341 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
342 return tplg->ops->widget_load(tplg->comp, w, tplg_w);
343
344 return 0;
345}
346
Liam Girdwoodc8597af2017-06-06 15:45:07 +0100347/* optionally pass new dynamic widget to component driver. This is mainly for
348 * external widgets where we can assign private data/ops */
349static int soc_tplg_widget_ready(struct soc_tplg *tplg,
350 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
351{
352 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
353 return tplg->ops->widget_ready(tplg->comp, w, tplg_w);
354
355 return 0;
356}
357
Masahiro Yamada183b8022017-02-27 14:29:20 -0800358/* pass DAI configurations to component driver for extra initialization */
Mengdong Lin64527e82016-01-15 16:13:28 +0800359static int soc_tplg_dai_load(struct soc_tplg *tplg,
Liam Girdwoodc8597af2017-06-06 15:45:07 +0100360 struct snd_soc_dai_driver *dai_drv,
361 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
Liam Girdwood8a978232015-05-29 19:06:14 +0100362{
Mengdong Lin64527e82016-01-15 16:13:28 +0800363 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
Liam Girdwoodc8597af2017-06-06 15:45:07 +0100364 return tplg->ops->dai_load(tplg->comp, dai_drv, pcm, dai);
Liam Girdwood8a978232015-05-29 19:06:14 +0100365
366 return 0;
367}
368
Masahiro Yamada183b8022017-02-27 14:29:20 -0800369/* pass link configurations to component driver for extra initialization */
Mengdong Linacfc7d42016-01-15 16:13:37 +0800370static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
371 struct snd_soc_dai_link *link)
372{
373 if (tplg->comp && tplg->ops && tplg->ops->link_load)
374 return tplg->ops->link_load(tplg->comp, link);
375
376 return 0;
377}
378
Liam Girdwood8a978232015-05-29 19:06:14 +0100379/* tell the component driver that all firmware has been loaded in this request */
380static void soc_tplg_complete(struct soc_tplg *tplg)
381{
382 if (tplg->comp && tplg->ops && tplg->ops->complete)
383 tplg->ops->complete(tplg->comp);
384}
385
386/* add a dynamic kcontrol */
387static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
388 const struct snd_kcontrol_new *control_new, const char *prefix,
389 void *data, struct snd_kcontrol **kcontrol)
390{
391 int err;
392
393 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
394 if (*kcontrol == NULL) {
395 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
396 control_new->name);
397 return -ENOMEM;
398 }
399
400 err = snd_ctl_add(card, *kcontrol);
401 if (err < 0) {
402 dev_err(dev, "ASoC: Failed to add %s: %d\n",
403 control_new->name, err);
404 return err;
405 }
406
407 return 0;
408}
409
410/* add a dynamic kcontrol for component driver */
411static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
412 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
413{
414 struct snd_soc_component *comp = tplg->comp;
415
416 return soc_tplg_add_dcontrol(comp->card->snd_card,
417 comp->dev, k, NULL, comp, kcontrol);
418}
419
420/* remove a mixer kcontrol */
421static void remove_mixer(struct snd_soc_component *comp,
422 struct snd_soc_dobj *dobj, int pass)
423{
424 struct snd_card *card = comp->card->snd_card;
425 struct soc_mixer_control *sm =
426 container_of(dobj, struct soc_mixer_control, dobj);
427 const unsigned int *p = NULL;
428
429 if (pass != SOC_TPLG_PASS_MIXER)
430 return;
431
432 if (dobj->ops && dobj->ops->control_unload)
433 dobj->ops->control_unload(comp, dobj);
434
435 if (sm->dobj.control.kcontrol->tlv.p)
436 p = sm->dobj.control.kcontrol->tlv.p;
437 snd_ctl_remove(card, sm->dobj.control.kcontrol);
438 list_del(&sm->dobj.list);
439 kfree(sm);
440 kfree(p);
441}
442
443/* remove an enum kcontrol */
444static void remove_enum(struct snd_soc_component *comp,
445 struct snd_soc_dobj *dobj, int pass)
446{
447 struct snd_card *card = comp->card->snd_card;
448 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
449 int i;
450
451 if (pass != SOC_TPLG_PASS_MIXER)
452 return;
453
454 if (dobj->ops && dobj->ops->control_unload)
455 dobj->ops->control_unload(comp, dobj);
456
457 snd_ctl_remove(card, se->dobj.control.kcontrol);
458 list_del(&se->dobj.list);
459
460 kfree(se->dobj.control.dvalues);
461 for (i = 0; i < se->items; i++)
462 kfree(se->dobj.control.dtexts[i]);
463 kfree(se);
464}
465
466/* remove a byte kcontrol */
467static void remove_bytes(struct snd_soc_component *comp,
468 struct snd_soc_dobj *dobj, int pass)
469{
470 struct snd_card *card = comp->card->snd_card;
471 struct soc_bytes_ext *sb =
472 container_of(dobj, struct soc_bytes_ext, dobj);
473
474 if (pass != SOC_TPLG_PASS_MIXER)
475 return;
476
477 if (dobj->ops && dobj->ops->control_unload)
478 dobj->ops->control_unload(comp, dobj);
479
480 snd_ctl_remove(card, sb->dobj.control.kcontrol);
481 list_del(&sb->dobj.list);
482 kfree(sb);
483}
484
485/* remove a widget and it's kcontrols - routes must be removed first */
486static void remove_widget(struct snd_soc_component *comp,
487 struct snd_soc_dobj *dobj, int pass)
488{
489 struct snd_card *card = comp->card->snd_card;
490 struct snd_soc_dapm_widget *w =
491 container_of(dobj, struct snd_soc_dapm_widget, dobj);
492 int i;
493
494 if (pass != SOC_TPLG_PASS_WIDGET)
495 return;
496
497 if (dobj->ops && dobj->ops->widget_unload)
498 dobj->ops->widget_unload(comp, dobj);
499
500 /*
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800501 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
Liam Girdwood8a978232015-05-29 19:06:14 +0100502 * The enum may either have an array of values or strings.
503 */
Mengdong Lineea3dd42016-11-25 16:09:17 +0800504 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
Liam Girdwood8a978232015-05-29 19:06:14 +0100505 /* enumerated widget mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800506 for (i = 0; i < w->num_kcontrols; i++) {
507 struct snd_kcontrol *kcontrol = w->kcontrols[i];
508 struct soc_enum *se =
509 (struct soc_enum *)kcontrol->private_value;
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100510 int j;
Liam Girdwood8a978232015-05-29 19:06:14 +0100511
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800512 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100513
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800514 kfree(se->dobj.control.dvalues);
Colin Ian Kingd7766aa2017-04-15 18:49:54 +0100515 for (j = 0; j < se->items; j++)
516 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +0100517
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +0800518 kfree(se);
519 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100520 kfree(w->kcontrol_news);
521 } else {
Mengdong Lineea3dd42016-11-25 16:09:17 +0800522 /* volume mixer or bytes controls */
Liam Girdwood8a978232015-05-29 19:06:14 +0100523 for (i = 0; i < w->num_kcontrols; i++) {
524 struct snd_kcontrol *kcontrol = w->kcontrols[i];
Liam Girdwood8a978232015-05-29 19:06:14 +0100525
Mengdong Lineea3dd42016-11-25 16:09:17 +0800526 if (dobj->widget.kcontrol_type
527 == SND_SOC_TPLG_TYPE_MIXER)
528 kfree(kcontrol->tlv.p);
Liam Girdwood8a978232015-05-29 19:06:14 +0100529
Mengdong Lineea3dd42016-11-25 16:09:17 +0800530 /* Private value is used as struct soc_mixer_control
531 * for volume mixers or soc_bytes_ext for bytes
532 * controls.
533 */
534 kfree((void *)kcontrol->private_value);
Colin Ian Kingc2b36122016-12-09 14:17:47 +0000535 snd_ctl_remove(card, kcontrol);
Liam Girdwood8a978232015-05-29 19:06:14 +0100536 }
537 kfree(w->kcontrol_news);
538 }
539 /* widget w is freed by soc-dapm.c */
540}
541
Mengdong Lin64527e82016-01-15 16:13:28 +0800542/* remove DAI configurations */
543static void remove_dai(struct snd_soc_component *comp,
Liam Girdwood8a978232015-05-29 19:06:14 +0100544 struct snd_soc_dobj *dobj, int pass)
545{
Mengdong Lin64527e82016-01-15 16:13:28 +0800546 struct snd_soc_dai_driver *dai_drv =
547 container_of(dobj, struct snd_soc_dai_driver, dobj);
548
Liam Girdwood8a978232015-05-29 19:06:14 +0100549 if (pass != SOC_TPLG_PASS_PCM_DAI)
550 return;
551
Mengdong Lin64527e82016-01-15 16:13:28 +0800552 if (dobj->ops && dobj->ops->dai_unload)
553 dobj->ops->dai_unload(comp, dobj);
Liam Girdwood8a978232015-05-29 19:06:14 +0100554
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800555 kfree(dai_drv->name);
Liam Girdwood8a978232015-05-29 19:06:14 +0100556 list_del(&dobj->list);
Mengdong Lin64527e82016-01-15 16:13:28 +0800557 kfree(dai_drv);
Liam Girdwood8a978232015-05-29 19:06:14 +0100558}
559
Mengdong Linacfc7d42016-01-15 16:13:37 +0800560/* remove link configurations */
561static void remove_link(struct snd_soc_component *comp,
562 struct snd_soc_dobj *dobj, int pass)
563{
564 struct snd_soc_dai_link *link =
565 container_of(dobj, struct snd_soc_dai_link, dobj);
566
567 if (pass != SOC_TPLG_PASS_PCM_DAI)
568 return;
569
570 if (dobj->ops && dobj->ops->link_unload)
571 dobj->ops->link_unload(comp, dobj);
572
Mengdong Lin8f27c4a2016-11-03 01:02:59 +0800573 kfree(link->name);
574 kfree(link->stream_name);
575 kfree(link->cpu_dai_name);
576
Mengdong Linacfc7d42016-01-15 16:13:37 +0800577 list_del(&dobj->list);
578 snd_soc_remove_dai_link(comp->card, link);
579 kfree(link);
580}
581
Liam Girdwood8a978232015-05-29 19:06:14 +0100582/* bind a kcontrol to it's IO handlers */
583static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
584 struct snd_kcontrol_new *k,
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800585 const struct soc_tplg *tplg)
Liam Girdwood8a978232015-05-29 19:06:14 +0100586{
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800587 const struct snd_soc_tplg_kcontrol_ops *ops;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800588 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800589 int num_ops, i;
Liam Girdwood8a978232015-05-29 19:06:14 +0100590
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800591 if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
592 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
593 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
594 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
595 struct soc_bytes_ext *sbe;
596 struct snd_soc_tplg_bytes_control *be;
597
598 sbe = (struct soc_bytes_ext *)k->private_value;
599 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
600
601 /* TLV bytes controls need standard kcontrol info handler,
602 * TLV callback and extended put/get handlers.
603 */
Omair M Abdullahf4be9782015-11-09 23:20:01 +0530604 k->info = snd_soc_bytes_info_ext;
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800605 k->tlv.c = snd_soc_bytes_tlv_callback;
606
607 ext_ops = tplg->bytes_ext_ops;
608 num_ops = tplg->bytes_ext_ops_count;
609 for (i = 0; i < num_ops; i++) {
610 if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
611 sbe->put = ext_ops[i].put;
612 if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
613 sbe->get = ext_ops[i].get;
614 }
615
616 if (sbe->put && sbe->get)
617 return 0;
618 else
619 return -EINVAL;
620 }
621
Mengdong Lin88a17d82015-08-18 18:11:51 +0800622 /* try and map vendor specific kcontrol handlers first */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800623 ops = tplg->io_ops;
624 num_ops = tplg->io_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +0100625 for (i = 0; i < num_ops; i++) {
626
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800627 if (k->put == NULL && ops[i].id == hdr->ops.put)
Liam Girdwood8a978232015-05-29 19:06:14 +0100628 k->put = ops[i].put;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800629 if (k->get == NULL && ops[i].id == hdr->ops.get)
Liam Girdwood8a978232015-05-29 19:06:14 +0100630 k->get = ops[i].get;
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800631 if (k->info == NULL && ops[i].id == hdr->ops.info)
632 k->info = ops[i].info;
Liam Girdwood8a978232015-05-29 19:06:14 +0100633 }
634
Mengdong Lin88a17d82015-08-18 18:11:51 +0800635 /* vendor specific handlers found ? */
636 if (k->put && k->get && k->info)
637 return 0;
638
639 /* none found so try standard kcontrol handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800640 ops = io_ops;
641 num_ops = ARRAY_SIZE(io_ops);
Mengdong Lin88a17d82015-08-18 18:11:51 +0800642 for (i = 0; i < num_ops; i++) {
643
644 if (k->put == NULL && ops[i].id == hdr->ops.put)
645 k->put = ops[i].put;
646 if (k->get == NULL && ops[i].id == hdr->ops.get)
647 k->get = ops[i].get;
648 if (k->info == NULL && ops[i].id == hdr->ops.info)
Liam Girdwood8a978232015-05-29 19:06:14 +0100649 k->info = ops[i].info;
650 }
651
652 /* standard handlers found ? */
653 if (k->put && k->get && k->info)
654 return 0;
655
Liam Girdwood8a978232015-05-29 19:06:14 +0100656 /* nothing to bind */
657 return -EINVAL;
658}
659
660/* bind a widgets to it's evnt handlers */
661int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
662 const struct snd_soc_tplg_widget_events *events,
663 int num_events, u16 event_type)
664{
665 int i;
666
667 w->event = NULL;
668
669 for (i = 0; i < num_events; i++) {
670 if (event_type == events[i].type) {
671
672 /* found - so assign event */
673 w->event = events[i].event_handler;
674 return 0;
675 }
676 }
677
678 /* not found */
679 return -EINVAL;
680}
681EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
682
683/* optionally pass new dynamic kcontrol to component driver. */
684static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
685 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
686{
687 if (tplg->comp && tplg->ops && tplg->ops->control_load)
688 return tplg->ops->control_load(tplg->comp, k, hdr);
689
690 return 0;
691}
692
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100693
694static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
695 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
Liam Girdwood8a978232015-05-29 19:06:14 +0100696{
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100697 unsigned int item_len = 2 * sizeof(unsigned int);
698 unsigned int *p;
Liam Girdwood8a978232015-05-29 19:06:14 +0100699
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100700 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
701 if (!p)
Liam Girdwood8a978232015-05-29 19:06:14 +0100702 return -ENOMEM;
703
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100704 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
705 p[1] = item_len;
706 p[2] = scale->min;
707 p[3] = (scale->step & TLV_DB_SCALE_MASK)
708 | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
Liam Girdwood8a978232015-05-29 19:06:14 +0100709
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100710 kc->tlv.p = (void *)p;
711 return 0;
712}
713
714static int soc_tplg_create_tlv(struct soc_tplg *tplg,
715 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
716{
717 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
718
719 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
720 return 0;
721
Mengdong Lin1a3232d2015-08-18 18:12:20 +0800722 if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100723 tplg_tlv = &tc->tlv;
724 switch (tplg_tlv->type) {
725 case SNDRV_CTL_TLVT_DB_SCALE:
726 return soc_tplg_create_tlv_db_scale(tplg, kc,
727 &tplg_tlv->scale);
728
729 /* TODO: add support for other TLV types */
730 default:
731 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
732 tplg_tlv->type);
733 return -EINVAL;
734 }
735 }
Liam Girdwood8a978232015-05-29 19:06:14 +0100736
737 return 0;
738}
739
740static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
741 struct snd_kcontrol_new *kc)
742{
743 kfree(kc->tlv.p);
744}
745
746static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
747 size_t size)
748{
749 struct snd_soc_tplg_bytes_control *be;
750 struct soc_bytes_ext *sbe;
751 struct snd_kcontrol_new kc;
752 int i, err;
753
754 if (soc_tplg_check_elem_count(tplg,
755 sizeof(struct snd_soc_tplg_bytes_control), count,
756 size, "mixer bytes")) {
757 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
758 count);
759 return -EINVAL;
760 }
761
762 for (i = 0; i < count; i++) {
763 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
764
765 /* validate kcontrol */
766 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
767 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
768 return -EINVAL;
769
770 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
771 if (sbe == NULL)
772 return -ENOMEM;
773
774 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
775 be->priv.size);
776
777 dev_dbg(tplg->dev,
778 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
779 be->hdr.name, be->hdr.access);
780
781 memset(&kc, 0, sizeof(kc));
782 kc.name = be->hdr.name;
783 kc.private_value = (long)sbe;
784 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
785 kc.access = be->hdr.access;
786
787 sbe->max = be->max;
788 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
789 sbe->dobj.ops = tplg->ops;
790 INIT_LIST_HEAD(&sbe->dobj.list);
791
792 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800793 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100794 if (err) {
795 soc_control_err(tplg, &be->hdr, be->hdr.name);
796 kfree(sbe);
797 continue;
798 }
799
800 /* pass control to driver for optional further init */
801 err = soc_tplg_init_kcontrol(tplg, &kc,
802 (struct snd_soc_tplg_ctl_hdr *)be);
803 if (err < 0) {
804 dev_err(tplg->dev, "ASoC: failed to init %s\n",
805 be->hdr.name);
806 kfree(sbe);
807 continue;
808 }
809
810 /* register control here */
811 err = soc_tplg_add_kcontrol(tplg, &kc,
812 &sbe->dobj.control.kcontrol);
813 if (err < 0) {
814 dev_err(tplg->dev, "ASoC: failed to add %s\n",
815 be->hdr.name);
816 kfree(sbe);
817 continue;
818 }
819
820 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
821 }
822 return 0;
823
824}
825
826static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
827 size_t size)
828{
829 struct snd_soc_tplg_mixer_control *mc;
830 struct soc_mixer_control *sm;
831 struct snd_kcontrol_new kc;
832 int i, err;
833
834 if (soc_tplg_check_elem_count(tplg,
835 sizeof(struct snd_soc_tplg_mixer_control),
836 count, size, "mixers")) {
837
838 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
839 count);
840 return -EINVAL;
841 }
842
843 for (i = 0; i < count; i++) {
844 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
845
846 /* validate kcontrol */
847 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
848 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
849 return -EINVAL;
850
851 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
852 if (sm == NULL)
853 return -ENOMEM;
854 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
855 mc->priv.size);
856
857 dev_dbg(tplg->dev,
858 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
859 mc->hdr.name, mc->hdr.access);
860
861 memset(&kc, 0, sizeof(kc));
862 kc.name = mc->hdr.name;
863 kc.private_value = (long)sm;
864 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
865 kc.access = mc->hdr.access;
866
867 /* we only support FL/FR channel mapping atm */
868 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
869 SNDRV_CHMAP_FL);
870 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
871 SNDRV_CHMAP_FR);
872 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
873 SNDRV_CHMAP_FL);
874 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
875 SNDRV_CHMAP_FR);
876
877 sm->max = mc->max;
878 sm->min = mc->min;
879 sm->invert = mc->invert;
880 sm->platform_max = mc->platform_max;
881 sm->dobj.index = tplg->index;
882 sm->dobj.ops = tplg->ops;
883 sm->dobj.type = SND_SOC_DOBJ_MIXER;
884 INIT_LIST_HEAD(&sm->dobj.list);
885
886 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +0800887 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +0100888 if (err) {
889 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
890 kfree(sm);
891 continue;
892 }
893
894 /* pass control to driver for optional further init */
895 err = soc_tplg_init_kcontrol(tplg, &kc,
896 (struct snd_soc_tplg_ctl_hdr *) mc);
897 if (err < 0) {
898 dev_err(tplg->dev, "ASoC: failed to init %s\n",
899 mc->hdr.name);
900 kfree(sm);
901 continue;
902 }
903
904 /* create any TLV data */
Mengdong Lin28a87ee2015-08-05 14:41:13 +0100905 soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +0100906
907 /* register control here */
908 err = soc_tplg_add_kcontrol(tplg, &kc,
909 &sm->dobj.control.kcontrol);
910 if (err < 0) {
911 dev_err(tplg->dev, "ASoC: failed to add %s\n",
912 mc->hdr.name);
913 soc_tplg_free_tlv(tplg, &kc);
914 kfree(sm);
915 continue;
916 }
917
918 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
919 }
920
921 return 0;
922}
923
924static int soc_tplg_denum_create_texts(struct soc_enum *se,
925 struct snd_soc_tplg_enum_control *ec)
926{
927 int i, ret;
928
929 se->dobj.control.dtexts =
930 kzalloc(sizeof(char *) * ec->items, GFP_KERNEL);
931 if (se->dobj.control.dtexts == NULL)
932 return -ENOMEM;
933
934 for (i = 0; i < ec->items; i++) {
935
936 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
937 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
938 ret = -EINVAL;
939 goto err;
940 }
941
942 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
943 if (!se->dobj.control.dtexts[i]) {
944 ret = -ENOMEM;
945 goto err;
946 }
947 }
948
Mousumi Janab6e38b22017-04-11 13:06:22 +0530949 se->texts = (const char * const *)se->dobj.control.dtexts;
Liam Girdwood8a978232015-05-29 19:06:14 +0100950 return 0;
951
952err:
953 for (--i; i >= 0; i--)
954 kfree(se->dobj.control.dtexts[i]);
955 kfree(se->dobj.control.dtexts);
956 return ret;
957}
958
959static int soc_tplg_denum_create_values(struct soc_enum *se,
960 struct snd_soc_tplg_enum_control *ec)
961{
962 if (ec->items > sizeof(*ec->values))
963 return -EINVAL;
964
Andrzej Hajda376c0af2015-08-07 09:59:37 +0200965 se->dobj.control.dvalues = kmemdup(ec->values,
966 ec->items * sizeof(u32),
967 GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +0100968 if (!se->dobj.control.dvalues)
969 return -ENOMEM;
970
Liam Girdwood8a978232015-05-29 19:06:14 +0100971 return 0;
972}
973
974static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
975 size_t size)
976{
977 struct snd_soc_tplg_enum_control *ec;
978 struct soc_enum *se;
979 struct snd_kcontrol_new kc;
980 int i, ret, err;
981
982 if (soc_tplg_check_elem_count(tplg,
983 sizeof(struct snd_soc_tplg_enum_control),
984 count, size, "enums")) {
985
986 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
987 count);
988 return -EINVAL;
989 }
990
991 for (i = 0; i < count; i++) {
992 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
993 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
994 ec->priv.size);
995
996 /* validate kcontrol */
997 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
998 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
999 return -EINVAL;
1000
1001 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1002 if (se == NULL)
1003 return -ENOMEM;
1004
1005 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1006 ec->hdr.name, ec->items);
1007
1008 memset(&kc, 0, sizeof(kc));
1009 kc.name = ec->hdr.name;
1010 kc.private_value = (long)se;
1011 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1012 kc.access = ec->hdr.access;
1013
1014 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1015 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1016 SNDRV_CHMAP_FL);
1017 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1018 SNDRV_CHMAP_FL);
1019
1020 se->items = ec->items;
1021 se->mask = ec->mask;
1022 se->dobj.index = tplg->index;
1023 se->dobj.type = SND_SOC_DOBJ_ENUM;
1024 se->dobj.ops = tplg->ops;
1025 INIT_LIST_HEAD(&se->dobj.list);
1026
1027 switch (ec->hdr.ops.info) {
1028 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1029 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1030 err = soc_tplg_denum_create_values(se, ec);
1031 if (err < 0) {
1032 dev_err(tplg->dev,
1033 "ASoC: could not create values for %s\n",
1034 ec->hdr.name);
1035 kfree(se);
1036 continue;
1037 }
1038 /* fall through and create texts */
1039 case SND_SOC_TPLG_CTL_ENUM:
1040 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1041 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1042 err = soc_tplg_denum_create_texts(se, ec);
1043 if (err < 0) {
1044 dev_err(tplg->dev,
1045 "ASoC: could not create texts for %s\n",
1046 ec->hdr.name);
1047 kfree(se);
1048 continue;
1049 }
1050 break;
1051 default:
1052 dev_err(tplg->dev,
1053 "ASoC: invalid enum control type %d for %s\n",
1054 ec->hdr.ops.info, ec->hdr.name);
1055 kfree(se);
1056 continue;
1057 }
1058
1059 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001060 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001061 if (err) {
1062 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1063 kfree(se);
1064 continue;
1065 }
1066
1067 /* pass control to driver for optional further init */
1068 err = soc_tplg_init_kcontrol(tplg, &kc,
1069 (struct snd_soc_tplg_ctl_hdr *) ec);
1070 if (err < 0) {
1071 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1072 ec->hdr.name);
1073 kfree(se);
1074 continue;
1075 }
1076
1077 /* register control here */
1078 ret = soc_tplg_add_kcontrol(tplg,
1079 &kc, &se->dobj.control.kcontrol);
1080 if (ret < 0) {
1081 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1082 ec->hdr.name);
1083 kfree(se);
1084 continue;
1085 }
1086
1087 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1088 }
1089
1090 return 0;
1091}
1092
1093static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1094 struct snd_soc_tplg_hdr *hdr)
1095{
1096 struct snd_soc_tplg_ctl_hdr *control_hdr;
1097 int i;
1098
1099 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
1100 tplg->pos += hdr->size + hdr->payload_size;
1101 return 0;
1102 }
1103
1104 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1105 soc_tplg_get_offset(tplg));
1106
1107 for (i = 0; i < hdr->count; i++) {
1108
1109 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1110
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001111 if (control_hdr->size != sizeof(*control_hdr)) {
1112 dev_err(tplg->dev, "ASoC: invalid control size\n");
1113 return -EINVAL;
1114 }
1115
Liam Girdwood8a978232015-05-29 19:06:14 +01001116 switch (control_hdr->ops.info) {
1117 case SND_SOC_TPLG_CTL_VOLSW:
1118 case SND_SOC_TPLG_CTL_STROBE:
1119 case SND_SOC_TPLG_CTL_VOLSW_SX:
1120 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1121 case SND_SOC_TPLG_CTL_RANGE:
1122 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1123 case SND_SOC_TPLG_DAPM_CTL_PIN:
1124 soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
1125 break;
1126 case SND_SOC_TPLG_CTL_ENUM:
1127 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1128 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1129 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1130 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1131 soc_tplg_denum_create(tplg, 1, hdr->payload_size);
1132 break;
1133 case SND_SOC_TPLG_CTL_BYTES:
1134 soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
1135 break;
1136 default:
1137 soc_bind_err(tplg, control_hdr, i);
1138 return -EINVAL;
1139 }
1140 }
1141
1142 return 0;
1143}
1144
1145static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1146 struct snd_soc_tplg_hdr *hdr)
1147{
1148 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1149 struct snd_soc_dapm_route route;
1150 struct snd_soc_tplg_dapm_graph_elem *elem;
1151 int count = hdr->count, i;
1152
1153 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
1154 tplg->pos += hdr->size + hdr->payload_size;
1155 return 0;
1156 }
1157
1158 if (soc_tplg_check_elem_count(tplg,
1159 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1160 count, hdr->payload_size, "graph")) {
1161
1162 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1163 count);
1164 return -EINVAL;
1165 }
1166
1167 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count);
1168
1169 for (i = 0; i < count; i++) {
1170 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1171 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1172
1173 /* validate routes */
1174 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1175 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1176 return -EINVAL;
1177 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1178 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1179 return -EINVAL;
1180 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1181 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1182 return -EINVAL;
1183
1184 route.source = elem->source;
1185 route.sink = elem->sink;
1186 route.connected = NULL; /* set to NULL atm for tplg users */
1187 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1188 route.control = NULL;
1189 else
1190 route.control = elem->control;
1191
1192 /* add route, but keep going if some fail */
1193 snd_soc_dapm_add_routes(dapm, &route, 1);
1194 }
1195
1196 return 0;
1197}
1198
1199static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1200 struct soc_tplg *tplg, int num_kcontrols)
1201{
1202 struct snd_kcontrol_new *kc;
1203 struct soc_mixer_control *sm;
1204 struct snd_soc_tplg_mixer_control *mc;
1205 int i, err;
1206
Axel Lin4ca7deb2015-08-05 22:34:22 +08001207 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001208 if (kc == NULL)
1209 return NULL;
1210
1211 for (i = 0; i < num_kcontrols; i++) {
1212 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
1213 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1214 if (sm == NULL)
1215 goto err;
1216
1217 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1218 mc->priv.size);
1219
1220 /* validate kcontrol */
1221 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1222 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1223 goto err_str;
1224
1225 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1226 mc->hdr.name, i);
1227
1228 kc[i].name = mc->hdr.name;
1229 kc[i].private_value = (long)sm;
1230 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1231 kc[i].access = mc->hdr.access;
1232
1233 /* we only support FL/FR channel mapping atm */
1234 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1235 SNDRV_CHMAP_FL);
1236 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1237 SNDRV_CHMAP_FR);
1238 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1239 SNDRV_CHMAP_FL);
1240 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1241 SNDRV_CHMAP_FR);
1242
1243 sm->max = mc->max;
1244 sm->min = mc->min;
1245 sm->invert = mc->invert;
1246 sm->platform_max = mc->platform_max;
1247 sm->dobj.index = tplg->index;
1248 INIT_LIST_HEAD(&sm->dobj.list);
1249
1250 /* map io handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001251 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001252 if (err) {
1253 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1254 kfree(sm);
1255 continue;
1256 }
1257
1258 /* pass control to driver for optional further init */
1259 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1260 (struct snd_soc_tplg_ctl_hdr *)mc);
1261 if (err < 0) {
1262 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1263 mc->hdr.name);
1264 kfree(sm);
1265 continue;
1266 }
1267 }
1268 return kc;
1269
1270err_str:
1271 kfree(sm);
1272err:
1273 for (--i; i >= 0; i--)
1274 kfree((void *)kc[i].private_value);
1275 kfree(kc);
1276 return NULL;
1277}
1278
1279static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001280 struct soc_tplg *tplg, int num_kcontrols)
Liam Girdwood8a978232015-05-29 19:06:14 +01001281{
1282 struct snd_kcontrol_new *kc;
1283 struct snd_soc_tplg_enum_control *ec;
1284 struct soc_enum *se;
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001285 int i, j, err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001286
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001287 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001288 if (kc == NULL)
1289 return NULL;
1290
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001291 for (i = 0; i < num_kcontrols; i++) {
1292 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1293 /* validate kcontrol */
1294 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1295 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1296 return NULL;
Liam Girdwood8a978232015-05-29 19:06:14 +01001297
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001298 se = kzalloc(sizeof(*se), GFP_KERNEL);
1299 if (se == NULL)
1300 goto err;
Liam Girdwood8a978232015-05-29 19:06:14 +01001301
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001302 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
Liam Girdwood8a978232015-05-29 19:06:14 +01001303 ec->hdr.name);
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001304
1305 kc[i].name = ec->hdr.name;
1306 kc[i].private_value = (long)se;
1307 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1308 kc[i].access = ec->hdr.access;
1309
1310 /* we only support FL/FR channel mapping atm */
1311 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1312 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1313 SNDRV_CHMAP_FL);
1314 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1315 SNDRV_CHMAP_FR);
1316
1317 se->items = ec->items;
1318 se->mask = ec->mask;
1319 se->dobj.index = tplg->index;
1320
1321 switch (ec->hdr.ops.info) {
1322 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1323 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1324 err = soc_tplg_denum_create_values(se, ec);
1325 if (err < 0) {
1326 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1327 ec->hdr.name);
1328 goto err_se;
1329 }
1330 /* fall through to create texts */
1331 case SND_SOC_TPLG_CTL_ENUM:
1332 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1333 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1334 err = soc_tplg_denum_create_texts(se, ec);
1335 if (err < 0) {
1336 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1337 ec->hdr.name);
1338 goto err_se;
1339 }
1340 break;
1341 default:
1342 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1343 ec->hdr.ops.info, ec->hdr.name);
1344 goto err_se;
1345 }
1346
1347 /* map io handlers */
1348 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1349 if (err) {
1350 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1351 goto err_se;
1352 }
1353
1354 /* pass control to driver for optional further init */
1355 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1356 (struct snd_soc_tplg_ctl_hdr *)ec);
1357 if (err < 0) {
1358 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1359 ec->hdr.name);
1360 goto err_se;
1361 }
1362
1363 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1364 ec->priv.size);
Liam Girdwood8a978232015-05-29 19:06:14 +01001365 }
1366
1367 return kc;
1368
1369err_se:
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001370 for (; i >= 0; i--) {
1371 /* free values and texts */
1372 se = (struct soc_enum *)kc[i].private_value;
1373 kfree(se->dobj.control.dvalues);
1374 for (j = 0; j < ec->items; j++)
1375 kfree(se->dobj.control.dtexts[j]);
Liam Girdwood8a978232015-05-29 19:06:14 +01001376
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001377 kfree(se);
1378 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001379err:
1380 kfree(kc);
1381
1382 return NULL;
1383}
1384
1385static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1386 struct soc_tplg *tplg, int count)
1387{
1388 struct snd_soc_tplg_bytes_control *be;
1389 struct soc_bytes_ext *sbe;
1390 struct snd_kcontrol_new *kc;
1391 int i, err;
1392
Axel Lin4ca7deb2015-08-05 22:34:22 +08001393 kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
Liam Girdwood8a978232015-05-29 19:06:14 +01001394 if (!kc)
1395 return NULL;
1396
1397 for (i = 0; i < count; i++) {
1398 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1399
1400 /* validate kcontrol */
1401 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1402 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1403 goto err;
1404
1405 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1406 if (sbe == NULL)
1407 goto err;
1408
1409 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1410 be->priv.size);
1411
1412 dev_dbg(tplg->dev,
1413 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1414 be->hdr.name, be->hdr.access);
1415
Liam Girdwood8a978232015-05-29 19:06:14 +01001416 kc[i].name = be->hdr.name;
1417 kc[i].private_value = (long)sbe;
1418 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1419 kc[i].access = be->hdr.access;
1420
1421 sbe->max = be->max;
1422 INIT_LIST_HEAD(&sbe->dobj.list);
1423
1424 /* map standard io handlers and check for external handlers */
Mengdong Lin2b5cdb92015-08-18 18:12:01 +08001425 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
Liam Girdwood8a978232015-05-29 19:06:14 +01001426 if (err) {
1427 soc_control_err(tplg, &be->hdr, be->hdr.name);
1428 kfree(sbe);
1429 continue;
1430 }
1431
1432 /* pass control to driver for optional further init */
1433 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1434 (struct snd_soc_tplg_ctl_hdr *)be);
1435 if (err < 0) {
1436 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1437 be->hdr.name);
1438 kfree(sbe);
1439 continue;
1440 }
1441 }
1442
1443 return kc;
1444
1445err:
1446 for (--i; i >= 0; i--)
1447 kfree((void *)kc[i].private_value);
1448
1449 kfree(kc);
1450 return NULL;
1451}
1452
1453static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1454 struct snd_soc_tplg_dapm_widget *w)
1455{
1456 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1457 struct snd_soc_dapm_widget template, *widget;
1458 struct snd_soc_tplg_ctl_hdr *control_hdr;
1459 struct snd_soc_card *card = tplg->comp->card;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001460 unsigned int kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001461 int ret = 0;
1462
1463 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1464 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1465 return -EINVAL;
1466 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1467 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1468 return -EINVAL;
1469
1470 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1471 w->name, w->id);
1472
1473 memset(&template, 0, sizeof(template));
1474
1475 /* map user to kernel widget ID */
1476 template.id = get_widget_id(w->id);
1477 if (template.id < 0)
1478 return template.id;
1479
1480 template.name = kstrdup(w->name, GFP_KERNEL);
1481 if (!template.name)
1482 return -ENOMEM;
1483 template.sname = kstrdup(w->sname, GFP_KERNEL);
1484 if (!template.sname) {
1485 ret = -ENOMEM;
1486 goto err;
1487 }
1488 template.reg = w->reg;
1489 template.shift = w->shift;
1490 template.mask = w->mask;
Subhransu S. Prusty6dc6db72015-06-29 17:36:44 +01001491 template.subseq = w->subseq;
Liam Girdwood8a978232015-05-29 19:06:14 +01001492 template.on_val = w->invert ? 0 : 1;
1493 template.off_val = w->invert ? 1 : 0;
1494 template.ignore_suspend = w->ignore_suspend;
1495 template.event_flags = w->event_flags;
1496 template.dobj.index = tplg->index;
1497
1498 tplg->pos +=
1499 (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
1500 if (w->num_kcontrols == 0) {
Arnd Bergmanndd5abb72016-12-09 12:51:46 +01001501 kcontrol_type = 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001502 template.num_kcontrols = 0;
1503 goto widget;
1504 }
1505
1506 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1507 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1508 w->name, w->num_kcontrols, control_hdr->type);
1509
1510 switch (control_hdr->ops.info) {
1511 case SND_SOC_TPLG_CTL_VOLSW:
1512 case SND_SOC_TPLG_CTL_STROBE:
1513 case SND_SOC_TPLG_CTL_VOLSW_SX:
1514 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1515 case SND_SOC_TPLG_CTL_RANGE:
1516 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001517 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
Liam Girdwood8a978232015-05-29 19:06:14 +01001518 template.num_kcontrols = w->num_kcontrols;
1519 template.kcontrol_news =
1520 soc_tplg_dapm_widget_dmixer_create(tplg,
1521 template.num_kcontrols);
1522 if (!template.kcontrol_news) {
1523 ret = -ENOMEM;
1524 goto hdr_err;
1525 }
1526 break;
1527 case SND_SOC_TPLG_CTL_ENUM:
1528 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1529 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1530 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1531 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001532 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001533 template.num_kcontrols = w->num_kcontrols;
Liam Girdwood8a978232015-05-29 19:06:14 +01001534 template.kcontrol_news =
Mengdong Lin1a7dd6e2016-11-25 16:09:10 +08001535 soc_tplg_dapm_widget_denum_create(tplg,
1536 template.num_kcontrols);
Liam Girdwood8a978232015-05-29 19:06:14 +01001537 if (!template.kcontrol_news) {
1538 ret = -ENOMEM;
1539 goto hdr_err;
1540 }
1541 break;
1542 case SND_SOC_TPLG_CTL_BYTES:
Mengdong Lineea3dd42016-11-25 16:09:17 +08001543 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
Liam Girdwood8a978232015-05-29 19:06:14 +01001544 template.num_kcontrols = w->num_kcontrols;
1545 template.kcontrol_news =
1546 soc_tplg_dapm_widget_dbytes_create(tplg,
1547 template.num_kcontrols);
1548 if (!template.kcontrol_news) {
1549 ret = -ENOMEM;
1550 goto hdr_err;
1551 }
1552 break;
1553 default:
1554 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1555 control_hdr->ops.get, control_hdr->ops.put,
1556 control_hdr->ops.info);
1557 ret = -EINVAL;
1558 goto hdr_err;
1559 }
1560
1561widget:
1562 ret = soc_tplg_widget_load(tplg, &template, w);
1563 if (ret < 0)
1564 goto hdr_err;
1565
1566 /* card dapm mutex is held by the core if we are loading topology
1567 * data during sound card init. */
1568 if (card->instantiated)
1569 widget = snd_soc_dapm_new_control(dapm, &template);
1570 else
1571 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
Linus Walleij37e1df82017-01-13 10:23:52 +01001572 if (IS_ERR(widget)) {
1573 ret = PTR_ERR(widget);
1574 /* Do not nag about probe deferrals */
1575 if (ret != -EPROBE_DEFER)
1576 dev_err(tplg->dev,
1577 "ASoC: failed to create widget %s controls (%d)\n",
1578 w->name, ret);
1579 goto hdr_err;
1580 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001581 if (widget == NULL) {
1582 dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
1583 w->name);
Wei Yongjun8ae3ea42016-08-10 13:43:12 +00001584 ret = -ENOMEM;
Liam Girdwood8a978232015-05-29 19:06:14 +01001585 goto hdr_err;
1586 }
1587
1588 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
Mengdong Lineea3dd42016-11-25 16:09:17 +08001589 widget->dobj.widget.kcontrol_type = kcontrol_type;
Liam Girdwood8a978232015-05-29 19:06:14 +01001590 widget->dobj.ops = tplg->ops;
1591 widget->dobj.index = tplg->index;
Jeeja KP8ea41672016-05-05 11:19:18 +05301592 kfree(template.sname);
1593 kfree(template.name);
Liam Girdwood8a978232015-05-29 19:06:14 +01001594 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
Liam Girdwoodc8597af2017-06-06 15:45:07 +01001595
1596 ret = soc_tplg_widget_ready(tplg, widget, w);
1597 if (ret < 0)
1598 goto ready_err;
1599
Liam Girdwood8a978232015-05-29 19:06:14 +01001600 return 0;
1601
Liam Girdwoodc8597af2017-06-06 15:45:07 +01001602ready_err:
1603 snd_soc_tplg_widget_remove(widget);
1604 snd_soc_dapm_free_widget(widget);
Liam Girdwood8a978232015-05-29 19:06:14 +01001605hdr_err:
1606 kfree(template.sname);
1607err:
1608 kfree(template.name);
1609 return ret;
1610}
1611
1612static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1613 struct snd_soc_tplg_hdr *hdr)
1614{
1615 struct snd_soc_tplg_dapm_widget *widget;
1616 int ret, count = hdr->count, i;
1617
1618 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1619 return 0;
1620
1621 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1622
1623 for (i = 0; i < count; i++) {
1624 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001625 if (widget->size != sizeof(*widget)) {
1626 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1627 return -EINVAL;
1628 }
1629
Liam Girdwood8a978232015-05-29 19:06:14 +01001630 ret = soc_tplg_dapm_widget_create(tplg, widget);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001631 if (ret < 0) {
Liam Girdwood8a978232015-05-29 19:06:14 +01001632 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1633 widget->name);
Mengdong Lin7de76b62016-04-27 14:52:38 +08001634 return ret;
1635 }
Liam Girdwood8a978232015-05-29 19:06:14 +01001636 }
1637
1638 return 0;
1639}
1640
1641static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1642{
1643 struct snd_soc_card *card = tplg->comp->card;
1644 int ret;
1645
1646 /* Card might not have been registered at this point.
1647 * If so, just return success.
1648 */
1649 if (!card || !card->instantiated) {
1650 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
Liam Girdwoodcc9d4712017-06-06 15:45:08 +01001651 " widget card binding deferred\n");
Liam Girdwood8a978232015-05-29 19:06:14 +01001652 return 0;
1653 }
1654
1655 ret = snd_soc_dapm_new_widgets(card);
1656 if (ret < 0)
1657 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1658 ret);
1659
1660 return 0;
1661}
1662
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001663static void set_stream_info(struct snd_soc_pcm_stream *stream,
1664 struct snd_soc_tplg_stream_caps *caps)
1665{
1666 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1667 stream->channels_min = caps->channels_min;
1668 stream->channels_max = caps->channels_max;
1669 stream->rates = caps->rates;
1670 stream->rate_min = caps->rate_min;
1671 stream->rate_max = caps->rate_max;
1672 stream->formats = caps->formats;
Mengdong Linf918e162016-08-19 18:12:46 +08001673 stream->sig_bits = caps->sig_bits;
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001674}
1675
Mengdong Lin0038be92016-07-26 14:32:37 +08001676static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1677 unsigned int flag_mask, unsigned int flags)
1678{
1679 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1680 dai_drv->symmetric_rates =
1681 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1682
1683 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1684 dai_drv->symmetric_channels =
1685 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1686 1 : 0;
1687
1688 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1689 dai_drv->symmetric_samplebits =
1690 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1691 1 : 0;
1692}
1693
Mengdong Lin64527e82016-01-15 16:13:28 +08001694static int soc_tplg_dai_create(struct soc_tplg *tplg,
1695 struct snd_soc_tplg_pcm *pcm)
1696{
1697 struct snd_soc_dai_driver *dai_drv;
1698 struct snd_soc_pcm_stream *stream;
1699 struct snd_soc_tplg_stream_caps *caps;
1700 int ret;
1701
1702 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1703 if (dai_drv == NULL)
1704 return -ENOMEM;
1705
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001706 if (strlen(pcm->dai_name))
1707 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Mengdong Lin64527e82016-01-15 16:13:28 +08001708 dai_drv->id = pcm->dai_id;
1709
1710 if (pcm->playback) {
1711 stream = &dai_drv->playback;
1712 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001713 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001714 }
1715
1716 if (pcm->capture) {
1717 stream = &dai_drv->capture;
1718 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Linb6b6e4d2016-02-22 16:29:19 +08001719 set_stream_info(stream, caps);
Mengdong Lin64527e82016-01-15 16:13:28 +08001720 }
1721
1722 /* pass control to component driver for optional further init */
1723 ret = soc_tplg_dai_load(tplg, dai_drv);
1724 if (ret < 0) {
1725 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1726 kfree(dai_drv);
1727 return ret;
1728 }
1729
1730 dai_drv->dobj.index = tplg->index;
1731 dai_drv->dobj.ops = tplg->ops;
1732 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1733 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1734
1735 /* register the DAI to the component */
1736 return snd_soc_register_dai(tplg->comp, dai_drv);
1737}
1738
Mengdong Lin717a8e72016-11-03 01:03:34 +08001739static void set_link_flags(struct snd_soc_dai_link *link,
1740 unsigned int flag_mask, unsigned int flags)
1741{
1742 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1743 link->symmetric_rates =
1744 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1745
1746 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1747 link->symmetric_channels =
1748 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1749 1 : 0;
1750
1751 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1752 link->symmetric_samplebits =
1753 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1754 1 : 0;
Mengdong Lin6ff67cc2016-11-03 01:05:32 +08001755
1756 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1757 link->ignore_suspend =
1758 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1759 1 : 0;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001760}
1761
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001762/* create the FE DAI link */
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001763static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
Mengdong Linacfc7d42016-01-15 16:13:37 +08001764 struct snd_soc_tplg_pcm *pcm)
1765{
1766 struct snd_soc_dai_link *link;
1767 int ret;
1768
1769 link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
1770 if (link == NULL)
1771 return -ENOMEM;
1772
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001773 if (strlen(pcm->pcm_name)) {
1774 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1775 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1776 }
Mengdong Linb84fff52016-04-19 13:12:43 +08001777 link->id = pcm->pcm_id;
Mengdong Linacfc7d42016-01-15 16:13:37 +08001778
Mengdong Lin8f27c4a2016-11-03 01:02:59 +08001779 if (strlen(pcm->dai_name))
1780 link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1781
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001782 link->codec_name = "snd-soc-dummy";
1783 link->codec_dai_name = "snd-soc-dummy-dai";
1784
1785 /* enable DPCM */
1786 link->dynamic = 1;
1787 link->dpcm_playback = pcm->playback;
1788 link->dpcm_capture = pcm->capture;
Mengdong Lin717a8e72016-11-03 01:03:34 +08001789 if (pcm->flag_mask)
1790 set_link_flags(link, pcm->flag_mask, pcm->flags);
Guneshwor Singh67d1c212016-04-19 13:12:50 +08001791
Mengdong Linacfc7d42016-01-15 16:13:37 +08001792 /* pass control to component driver for optional further init */
1793 ret = soc_tplg_dai_link_load(tplg, link);
1794 if (ret < 0) {
1795 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1796 kfree(link);
1797 return ret;
1798 }
1799
1800 link->dobj.index = tplg->index;
1801 link->dobj.ops = tplg->ops;
1802 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1803 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1804
1805 snd_soc_add_dai_link(tplg->comp->card, link);
1806 return 0;
1807}
1808
1809/* create a FE DAI and DAI link from the PCM object */
Mengdong Lin64527e82016-01-15 16:13:28 +08001810static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1811 struct snd_soc_tplg_pcm *pcm)
1812{
Mengdong Linacfc7d42016-01-15 16:13:37 +08001813 int ret;
1814
1815 ret = soc_tplg_dai_create(tplg, pcm);
1816 if (ret < 0)
1817 return ret;
1818
Mengdong Linab4bc5e2016-11-03 01:04:42 +08001819 return soc_tplg_fe_link_create(tplg, pcm);
Mengdong Lin64527e82016-01-15 16:13:28 +08001820}
1821
Mengdong Lin55726dc2016-11-03 01:00:16 +08001822/* copy stream caps from the old version 4 of source */
1823static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1824 struct snd_soc_tplg_stream_caps_v4 *src)
1825{
1826 dest->size = sizeof(*dest);
1827 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1828 dest->formats = src->formats;
1829 dest->rates = src->rates;
1830 dest->rate_min = src->rate_min;
1831 dest->rate_max = src->rate_max;
1832 dest->channels_min = src->channels_min;
1833 dest->channels_max = src->channels_max;
1834 dest->periods_min = src->periods_min;
1835 dest->periods_max = src->periods_max;
1836 dest->period_size_min = src->period_size_min;
1837 dest->period_size_max = src->period_size_max;
1838 dest->buffer_size_min = src->buffer_size_min;
1839 dest->buffer_size_max = src->buffer_size_max;
1840}
1841
1842/**
1843 * pcm_new_ver - Create the new version of PCM from the old version.
1844 * @tplg: topology context
1845 * @src: older version of pcm as a source
1846 * @pcm: latest version of pcm created from the source
1847 *
1848 * Support from vesion 4. User should free the returned pcm manually.
1849 */
1850static int pcm_new_ver(struct soc_tplg *tplg,
1851 struct snd_soc_tplg_pcm *src,
1852 struct snd_soc_tplg_pcm **pcm)
1853{
1854 struct snd_soc_tplg_pcm *dest;
1855 struct snd_soc_tplg_pcm_v4 *src_v4;
1856 int i;
1857
1858 *pcm = NULL;
1859
1860 if (src->size != sizeof(*src_v4)) {
1861 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
1862 return -EINVAL;
1863 }
1864
1865 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
1866 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
1867 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
1868 if (!dest)
1869 return -ENOMEM;
1870
1871 dest->size = sizeof(*dest); /* size of latest abi version */
1872 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1873 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1874 dest->pcm_id = src_v4->pcm_id;
1875 dest->dai_id = src_v4->dai_id;
1876 dest->playback = src_v4->playback;
1877 dest->capture = src_v4->capture;
1878 dest->compress = src_v4->compress;
1879 dest->num_streams = src_v4->num_streams;
1880 for (i = 0; i < dest->num_streams; i++)
1881 memcpy(&dest->stream[i], &src_v4->stream[i],
1882 sizeof(struct snd_soc_tplg_stream));
1883
1884 for (i = 0; i < 2; i++)
1885 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
1886
1887 *pcm = dest;
1888 return 0;
1889}
1890
Mengdong Lin64527e82016-01-15 16:13:28 +08001891static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
Liam Girdwood8a978232015-05-29 19:06:14 +01001892 struct snd_soc_tplg_hdr *hdr)
1893{
Mengdong Lin55726dc2016-11-03 01:00:16 +08001894 struct snd_soc_tplg_pcm *pcm, *_pcm;
Liam Girdwood8a978232015-05-29 19:06:14 +01001895 int count = hdr->count;
Vinod Koulfd340452016-12-08 23:01:27 +05301896 int i;
Mengdong Lin55726dc2016-11-03 01:00:16 +08001897 bool abi_match;
Liam Girdwood8a978232015-05-29 19:06:14 +01001898
1899 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
1900 return 0;
1901
Mengdong Lin55726dc2016-11-03 01:00:16 +08001902 /* check the element size and count */
1903 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1904 if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
1905 || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
1906 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1907 pcm->size);
1908 return -EINVAL;
1909 }
1910
Liam Girdwood8a978232015-05-29 19:06:14 +01001911 if (soc_tplg_check_elem_count(tplg,
Mengdong Lin55726dc2016-11-03 01:00:16 +08001912 pcm->size, count,
Liam Girdwood8a978232015-05-29 19:06:14 +01001913 hdr->payload_size, "PCM DAI")) {
1914 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
1915 count);
1916 return -EINVAL;
1917 }
1918
Mengdong Lin64527e82016-01-15 16:13:28 +08001919 for (i = 0; i < count; i++) {
Mengdong Lin55726dc2016-11-03 01:00:16 +08001920 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1921
1922 /* check ABI version by size, create a new version of pcm
1923 * if abi not match.
1924 */
1925 if (pcm->size == sizeof(*pcm)) {
1926 abi_match = true;
1927 _pcm = pcm;
1928 } else {
1929 abi_match = false;
Vinod Koulfd340452016-12-08 23:01:27 +05301930 pcm_new_ver(tplg, pcm, &_pcm);
Mengdong Lin06eb49f2016-04-27 14:52:56 +08001931 }
1932
Mengdong Lin55726dc2016-11-03 01:00:16 +08001933 /* create the FE DAIs and DAI links */
1934 soc_tplg_pcm_create(tplg, _pcm);
1935
Mengdong Lin717a8e72016-11-03 01:03:34 +08001936 /* offset by version-specific struct size and
1937 * real priv data size
1938 */
1939 tplg->pos += pcm->size + _pcm->priv.size;
1940
Mengdong Lin55726dc2016-11-03 01:00:16 +08001941 if (!abi_match)
1942 kfree(_pcm); /* free the duplicated one */
Mengdong Lin64527e82016-01-15 16:13:28 +08001943 }
1944
Liam Girdwood8a978232015-05-29 19:06:14 +01001945 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
Liam Girdwood8a978232015-05-29 19:06:14 +01001946
Liam Girdwood8a978232015-05-29 19:06:14 +01001947 return 0;
Liam Girdwood8a978232015-05-29 19:06:14 +01001948}
1949
Mengdong Lin593d9e52016-11-03 01:04:27 +08001950/**
1951 * set_link_hw_format - Set the HW audio format of the physical DAI link.
Charles Keepax8abab352017-01-12 11:38:15 +00001952 * @link: &snd_soc_dai_link which should be updated
Mengdong Lin593d9e52016-11-03 01:04:27 +08001953 * @cfg: physical link configs.
1954 *
1955 * Topology context contains a list of supported HW formats (configs) and
1956 * a default format ID for the physical link. This function will use this
1957 * default ID to choose the HW format to set the link's DAI format for init.
1958 */
1959static void set_link_hw_format(struct snd_soc_dai_link *link,
1960 struct snd_soc_tplg_link_config *cfg)
1961{
1962 struct snd_soc_tplg_hw_config *hw_config;
1963 unsigned char bclk_master, fsync_master;
1964 unsigned char invert_bclk, invert_fsync;
1965 int i;
1966
1967 for (i = 0; i < cfg->num_hw_configs; i++) {
1968 hw_config = &cfg->hw_config[i];
1969 if (hw_config->id != cfg->default_hw_config_id)
1970 continue;
1971
1972 link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
1973
1974 /* clock signal polarity */
1975 invert_bclk = hw_config->invert_bclk;
1976 invert_fsync = hw_config->invert_fsync;
1977 if (!invert_bclk && !invert_fsync)
1978 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1979 else if (!invert_bclk && invert_fsync)
1980 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1981 else if (invert_bclk && !invert_fsync)
1982 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1983 else
1984 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1985
1986 /* clock masters */
1987 bclk_master = hw_config->bclk_master;
1988 fsync_master = hw_config->fsync_master;
1989 if (!bclk_master && !fsync_master)
1990 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1991 else if (bclk_master && !fsync_master)
1992 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1993 else if (!bclk_master && fsync_master)
1994 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1995 else
1996 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1997 }
1998}
1999
2000/**
2001 * link_new_ver - Create a new physical link config from the old
2002 * version of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002003 * @tplg: topology context
Mengdong Lin593d9e52016-11-03 01:04:27 +08002004 * @src: old version of phyical link config as a source
2005 * @link: latest version of physical link config created from the source
2006 *
2007 * Support from vesion 4. User need free the returned link config manually.
2008 */
2009static int link_new_ver(struct soc_tplg *tplg,
2010 struct snd_soc_tplg_link_config *src,
2011 struct snd_soc_tplg_link_config **link)
2012{
2013 struct snd_soc_tplg_link_config *dest;
2014 struct snd_soc_tplg_link_config_v4 *src_v4;
2015 int i;
2016
2017 *link = NULL;
2018
2019 if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
2020 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2021 return -EINVAL;
2022 }
2023
2024 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2025
2026 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2027 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2028 if (!dest)
2029 return -ENOMEM;
2030
2031 dest->size = sizeof(*dest);
2032 dest->id = src_v4->id;
2033 dest->num_streams = src_v4->num_streams;
2034 for (i = 0; i < dest->num_streams; i++)
2035 memcpy(&dest->stream[i], &src_v4->stream[i],
2036 sizeof(struct snd_soc_tplg_stream));
2037
2038 *link = dest;
2039 return 0;
2040}
2041
2042/* Find and configure an existing physical DAI link */
2043static int soc_tplg_link_config(struct soc_tplg *tplg,
2044 struct snd_soc_tplg_link_config *cfg)
2045{
2046 struct snd_soc_dai_link *link;
2047 const char *name, *stream_name;
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002048 size_t len;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002049 int ret;
2050
Mengdong Lindbab1cb2016-11-05 08:42:14 +08002051 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2052 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2053 return -EINVAL;
2054 else if (len)
2055 name = cfg->name;
2056 else
2057 name = NULL;
2058
2059 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2060 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2061 return -EINVAL;
2062 else if (len)
2063 stream_name = cfg->stream_name;
2064 else
2065 stream_name = NULL;
Mengdong Lin593d9e52016-11-03 01:04:27 +08002066
2067 link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
2068 name, stream_name);
2069 if (!link) {
2070 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2071 name, cfg->id);
2072 return -EINVAL;
2073 }
2074
2075 /* hw format */
2076 if (cfg->num_hw_configs)
2077 set_link_hw_format(link, cfg);
2078
2079 /* flags */
2080 if (cfg->flag_mask)
2081 set_link_flags(link, cfg->flag_mask, cfg->flags);
2082
2083 /* pass control to component driver for optional further init */
2084 ret = soc_tplg_dai_link_load(tplg, link);
2085 if (ret < 0) {
2086 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2087 return ret;
2088 }
2089
2090 return 0;
2091}
2092
2093
2094/* Load physical link config elements from the topology context */
2095static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2096 struct snd_soc_tplg_hdr *hdr)
2097{
2098 struct snd_soc_tplg_link_config *link, *_link;
2099 int count = hdr->count;
2100 int i, ret;
2101 bool abi_match;
2102
2103 if (tplg->pass != SOC_TPLG_PASS_LINK) {
2104 tplg->pos += hdr->size + hdr->payload_size;
2105 return 0;
2106 };
2107
2108 /* check the element size and count */
2109 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2110 if (link->size > sizeof(struct snd_soc_tplg_link_config)
2111 || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2112 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2113 link->size);
2114 return -EINVAL;
2115 }
2116
2117 if (soc_tplg_check_elem_count(tplg,
2118 link->size, count,
2119 hdr->payload_size, "physical link config")) {
2120 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2121 count);
2122 return -EINVAL;
2123 }
2124
2125 /* config physical DAI links */
2126 for (i = 0; i < count; i++) {
2127 link = (struct snd_soc_tplg_link_config *)tplg->pos;
2128 if (link->size == sizeof(*link)) {
2129 abi_match = true;
2130 _link = link;
2131 } else {
2132 abi_match = false;
2133 ret = link_new_ver(tplg, link, &_link);
2134 if (ret < 0)
2135 return ret;
2136 }
2137
2138 ret = soc_tplg_link_config(tplg, _link);
2139 if (ret < 0)
2140 return ret;
2141
2142 /* offset by version-specific struct size and
2143 * real priv data size
2144 */
2145 tplg->pos += link->size + _link->priv.size;
2146
2147 if (!abi_match)
2148 kfree(_link); /* free the duplicated one */
2149 }
2150
2151 return 0;
2152}
2153
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002154/**
2155 * soc_tplg_dai_config - Find and configure an existing physical DAI.
Mengdong Lin0038be92016-07-26 14:32:37 +08002156 * @tplg: topology context
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002157 * @d: physical DAI configs.
Mengdong Lin0038be92016-07-26 14:32:37 +08002158 *
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002159 * The physical dai should already be registered by the platform driver.
2160 * The platform driver should specify the DAI name and ID for matching.
Mengdong Lin0038be92016-07-26 14:32:37 +08002161 */
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002162static int soc_tplg_dai_config(struct soc_tplg *tplg,
2163 struct snd_soc_tplg_dai *d)
Mengdong Lin0038be92016-07-26 14:32:37 +08002164{
2165 struct snd_soc_dai_link_component dai_component = {0};
2166 struct snd_soc_dai *dai;
2167 struct snd_soc_dai_driver *dai_drv;
2168 struct snd_soc_pcm_stream *stream;
2169 struct snd_soc_tplg_stream_caps *caps;
2170 int ret;
2171
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002172 dai_component.dai_name = d->dai_name;
Mengdong Lin0038be92016-07-26 14:32:37 +08002173 dai = snd_soc_find_dai(&dai_component);
2174 if (!dai) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002175 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2176 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002177 return -EINVAL;
2178 }
2179
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002180 if (d->dai_id != dai->id) {
2181 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2182 d->dai_name);
Mengdong Lin0038be92016-07-26 14:32:37 +08002183 return -EINVAL;
2184 }
2185
2186 dai_drv = dai->driver;
2187 if (!dai_drv)
2188 return -EINVAL;
2189
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002190 if (d->playback) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002191 stream = &dai_drv->playback;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002192 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Mengdong Lin0038be92016-07-26 14:32:37 +08002193 set_stream_info(stream, caps);
2194 }
2195
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002196 if (d->capture) {
Mengdong Lin0038be92016-07-26 14:32:37 +08002197 stream = &dai_drv->capture;
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002198 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Mengdong Lin0038be92016-07-26 14:32:37 +08002199 set_stream_info(stream, caps);
2200 }
2201
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002202 if (d->flag_mask)
2203 set_dai_flags(dai_drv, d->flag_mask, d->flags);
Mengdong Lin0038be92016-07-26 14:32:37 +08002204
2205 /* pass control to component driver for optional further init */
2206 ret = soc_tplg_dai_load(tplg, dai_drv);
2207 if (ret < 0) {
2208 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2209 return ret;
2210 }
2211
2212 return 0;
2213}
2214
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002215/* load physical DAI elements */
2216static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2217 struct snd_soc_tplg_hdr *hdr)
Mengdong Lin0038be92016-07-26 14:32:37 +08002218{
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002219 struct snd_soc_tplg_dai *dai;
Mengdong Lin0038be92016-07-26 14:32:37 +08002220 int count = hdr->count;
2221 int i;
2222
2223 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2224 return 0;
2225
2226 /* config the existing BE DAIs */
2227 for (i = 0; i < count; i++) {
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002228 dai = (struct snd_soc_tplg_dai *)tplg->pos;
2229 if (dai->size != sizeof(*dai)) {
2230 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
Mengdong Lin0038be92016-07-26 14:32:37 +08002231 return -EINVAL;
2232 }
2233
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002234 soc_tplg_dai_config(tplg, dai);
2235 tplg->pos += (sizeof(*dai) + dai->priv.size);
Mengdong Lin0038be92016-07-26 14:32:37 +08002236 }
2237
2238 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2239 return 0;
2240}
2241
Mengdong Lin583958f2016-10-11 14:36:42 +08002242/**
2243 * manifest_new_ver - Create a new version of manifest from the old version
2244 * of source.
Charles Keepax8abab352017-01-12 11:38:15 +00002245 * @tplg: topology context
Mengdong Lin583958f2016-10-11 14:36:42 +08002246 * @src: old version of manifest as a source
2247 * @manifest: latest version of manifest created from the source
2248 *
2249 * Support from vesion 4. Users need free the returned manifest manually.
2250 */
2251static int manifest_new_ver(struct soc_tplg *tplg,
2252 struct snd_soc_tplg_manifest *src,
2253 struct snd_soc_tplg_manifest **manifest)
2254{
2255 struct snd_soc_tplg_manifest *dest;
2256 struct snd_soc_tplg_manifest_v4 *src_v4;
2257
2258 *manifest = NULL;
2259
2260 if (src->size != sizeof(*src_v4)) {
2261 dev_err(tplg->dev, "ASoC: invalid manifest size\n");
2262 return -EINVAL;
2263 }
2264
2265 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2266
2267 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2268 dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
2269 if (!dest)
2270 return -ENOMEM;
2271
2272 dest->size = sizeof(*dest); /* size of latest abi version */
2273 dest->control_elems = src_v4->control_elems;
2274 dest->widget_elems = src_v4->widget_elems;
2275 dest->graph_elems = src_v4->graph_elems;
2276 dest->pcm_elems = src_v4->pcm_elems;
2277 dest->dai_link_elems = src_v4->dai_link_elems;
2278 dest->priv.size = src_v4->priv.size;
2279 if (dest->priv.size)
2280 memcpy(dest->priv.data, src_v4->priv.data,
2281 src_v4->priv.size);
2282
2283 *manifest = dest;
2284 return 0;
2285}
Mengdong Lin0038be92016-07-26 14:32:37 +08002286
Liam Girdwood8a978232015-05-29 19:06:14 +01002287static int soc_tplg_manifest_load(struct soc_tplg *tplg,
Mengdong Lin0038be92016-07-26 14:32:37 +08002288 struct snd_soc_tplg_hdr *hdr)
Liam Girdwood8a978232015-05-29 19:06:14 +01002289{
Mengdong Lin583958f2016-10-11 14:36:42 +08002290 struct snd_soc_tplg_manifest *manifest, *_manifest;
2291 bool abi_match;
2292 int err;
Liam Girdwood8a978232015-05-29 19:06:14 +01002293
2294 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2295 return 0;
2296
2297 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
Mengdong Lin583958f2016-10-11 14:36:42 +08002298
2299 /* check ABI version by size, create a new manifest if abi not match */
2300 if (manifest->size == sizeof(*manifest)) {
2301 abi_match = true;
2302 _manifest = manifest;
2303 } else {
2304 abi_match = false;
2305 err = manifest_new_ver(tplg, manifest, &_manifest);
2306 if (err < 0)
2307 return err;
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002308 }
2309
Mengdong Lin583958f2016-10-11 14:36:42 +08002310 /* pass control to component driver for optional further init */
Liam Girdwood8a978232015-05-29 19:06:14 +01002311 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Mengdong Lin583958f2016-10-11 14:36:42 +08002312 return tplg->ops->manifest(tplg->comp, _manifest);
Liam Girdwood8a978232015-05-29 19:06:14 +01002313
Mengdong Lin583958f2016-10-11 14:36:42 +08002314 if (!abi_match) /* free the duplicated one */
2315 kfree(_manifest);
2316
Liam Girdwood8a978232015-05-29 19:06:14 +01002317 return 0;
2318}
2319
2320/* validate header magic, size and type */
2321static int soc_valid_header(struct soc_tplg *tplg,
2322 struct snd_soc_tplg_hdr *hdr)
2323{
2324 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2325 return 0;
2326
Mengdong Lin06eb49f2016-04-27 14:52:56 +08002327 if (hdr->size != sizeof(*hdr)) {
2328 dev_err(tplg->dev,
2329 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2330 hdr->type, soc_tplg_get_hdr_offset(tplg),
2331 tplg->fw->size);
2332 return -EINVAL;
2333 }
2334
Liam Girdwood8a978232015-05-29 19:06:14 +01002335 /* big endian firmware objects not supported atm */
2336 if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
2337 dev_err(tplg->dev,
2338 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2339 tplg->pass, hdr->magic,
2340 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2341 return -EINVAL;
2342 }
2343
2344 if (hdr->magic != SND_SOC_TPLG_MAGIC) {
2345 dev_err(tplg->dev,
2346 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2347 tplg->pass, hdr->magic,
2348 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2349 return -EINVAL;
2350 }
2351
Mengdong Lin288b8da2016-11-03 01:03:17 +08002352 /* Support ABI from version 4 */
2353 if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
2354 || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
Liam Girdwood8a978232015-05-29 19:06:14 +01002355 dev_err(tplg->dev,
2356 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2357 tplg->pass, hdr->abi,
2358 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2359 tplg->fw->size);
2360 return -EINVAL;
2361 }
2362
2363 if (hdr->payload_size == 0) {
2364 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2365 soc_tplg_get_hdr_offset(tplg));
2366 return -EINVAL;
2367 }
2368
2369 if (tplg->pass == hdr->type)
2370 dev_dbg(tplg->dev,
2371 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2372 hdr->payload_size, hdr->type, hdr->version,
2373 hdr->vendor_type, tplg->pass);
2374
2375 return 1;
2376}
2377
2378/* check header type and call appropriate handler */
2379static int soc_tplg_load_header(struct soc_tplg *tplg,
2380 struct snd_soc_tplg_hdr *hdr)
2381{
2382 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2383
2384 /* check for matching ID */
2385 if (hdr->index != tplg->req_index &&
2386 hdr->index != SND_SOC_TPLG_INDEX_ALL)
2387 return 0;
2388
2389 tplg->index = hdr->index;
2390
2391 switch (hdr->type) {
2392 case SND_SOC_TPLG_TYPE_MIXER:
2393 case SND_SOC_TPLG_TYPE_ENUM:
2394 case SND_SOC_TPLG_TYPE_BYTES:
2395 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2396 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2397 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2398 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2399 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2400 case SND_SOC_TPLG_TYPE_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002401 return soc_tplg_pcm_elems_load(tplg, hdr);
Mengdong Lin3fbf7932016-11-03 01:05:01 +08002402 case SND_SOC_TPLG_TYPE_DAI:
Mengdong Lin9aa3f032016-11-03 01:05:15 +08002403 return soc_tplg_dai_elems_load(tplg, hdr);
Mengdong Lin593d9e52016-11-03 01:04:27 +08002404 case SND_SOC_TPLG_TYPE_DAI_LINK:
2405 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2406 /* physical link configurations */
2407 return soc_tplg_link_elems_load(tplg, hdr);
Liam Girdwood8a978232015-05-29 19:06:14 +01002408 case SND_SOC_TPLG_TYPE_MANIFEST:
2409 return soc_tplg_manifest_load(tplg, hdr);
2410 default:
2411 /* bespoke vendor data object */
2412 return soc_tplg_vendor_load(tplg, hdr);
2413 }
2414
2415 return 0;
2416}
2417
2418/* process the topology file headers */
2419static int soc_tplg_process_headers(struct soc_tplg *tplg)
2420{
2421 struct snd_soc_tplg_hdr *hdr;
2422 int ret;
2423
2424 tplg->pass = SOC_TPLG_PASS_START;
2425
2426 /* process the header types from start to end */
2427 while (tplg->pass <= SOC_TPLG_PASS_END) {
2428
2429 tplg->hdr_pos = tplg->fw->data;
2430 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2431
2432 while (!soc_tplg_is_eof(tplg)) {
2433
2434 /* make sure header is valid before loading */
2435 ret = soc_valid_header(tplg, hdr);
2436 if (ret < 0)
2437 return ret;
2438 else if (ret == 0)
2439 break;
2440
2441 /* load the header object */
2442 ret = soc_tplg_load_header(tplg, hdr);
2443 if (ret < 0)
2444 return ret;
2445
2446 /* goto next header */
2447 tplg->hdr_pos += hdr->payload_size +
2448 sizeof(struct snd_soc_tplg_hdr);
2449 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2450 }
2451
2452 /* next data type pass */
2453 tplg->pass++;
2454 }
2455
2456 /* signal DAPM we are complete */
2457 ret = soc_tplg_dapm_complete(tplg);
2458 if (ret < 0)
2459 dev_err(tplg->dev,
2460 "ASoC: failed to initialise DAPM from Firmware\n");
2461
2462 return ret;
2463}
2464
2465static int soc_tplg_load(struct soc_tplg *tplg)
2466{
2467 int ret;
2468
2469 ret = soc_tplg_process_headers(tplg);
2470 if (ret == 0)
2471 soc_tplg_complete(tplg);
2472
2473 return ret;
2474}
2475
2476/* load audio component topology from "firmware" file */
2477int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2478 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2479{
2480 struct soc_tplg tplg;
2481
2482 /* setup parsing context */
2483 memset(&tplg, 0, sizeof(tplg));
2484 tplg.fw = fw;
2485 tplg.dev = comp->dev;
2486 tplg.comp = comp;
2487 tplg.ops = ops;
2488 tplg.req_index = id;
2489 tplg.io_ops = ops->io_ops;
2490 tplg.io_ops_count = ops->io_ops_count;
Mengdong Lin1a3232d2015-08-18 18:12:20 +08002491 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2492 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
Liam Girdwood8a978232015-05-29 19:06:14 +01002493
2494 return soc_tplg_load(&tplg);
2495}
2496EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2497
2498/* remove this dynamic widget */
2499void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2500{
2501 /* make sure we are a widget */
2502 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2503 return;
2504
2505 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2506}
2507EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2508
2509/* remove all dynamic widgets from this DAPM context */
2510void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2511 u32 index)
2512{
2513 struct snd_soc_dapm_widget *w, *next_w;
Liam Girdwood8a978232015-05-29 19:06:14 +01002514
2515 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2516
2517 /* make sure we are a widget with correct context */
2518 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2519 continue;
2520
2521 /* match ID */
2522 if (w->dobj.index != index &&
2523 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2524 continue;
Liam Girdwood8a978232015-05-29 19:06:14 +01002525 /* check and free and dynamic widget kcontrols */
2526 snd_soc_tplg_widget_remove(w);
Lars-Peter Clausenb97e2692015-07-21 18:11:07 +02002527 snd_soc_dapm_free_widget(w);
Liam Girdwood8a978232015-05-29 19:06:14 +01002528 }
Jyri Sarhafd589a12015-11-10 18:12:42 +02002529 snd_soc_dapm_reset_cache(dapm);
Liam Girdwood8a978232015-05-29 19:06:14 +01002530}
2531EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2532
2533/* remove dynamic controls from the component driver */
2534int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2535{
2536 struct snd_soc_dobj *dobj, *next_dobj;
2537 int pass = SOC_TPLG_PASS_END;
2538
2539 /* process the header types from end to start */
2540 while (pass >= SOC_TPLG_PASS_START) {
2541
2542 /* remove mixer controls */
2543 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2544 list) {
2545
2546 /* match index */
2547 if (dobj->index != index &&
2548 dobj->index != SND_SOC_TPLG_INDEX_ALL)
2549 continue;
2550
2551 switch (dobj->type) {
2552 case SND_SOC_DOBJ_MIXER:
2553 remove_mixer(comp, dobj, pass);
2554 break;
2555 case SND_SOC_DOBJ_ENUM:
2556 remove_enum(comp, dobj, pass);
2557 break;
2558 case SND_SOC_DOBJ_BYTES:
2559 remove_bytes(comp, dobj, pass);
2560 break;
2561 case SND_SOC_DOBJ_WIDGET:
2562 remove_widget(comp, dobj, pass);
2563 break;
2564 case SND_SOC_DOBJ_PCM:
Mengdong Lin64527e82016-01-15 16:13:28 +08002565 remove_dai(comp, dobj, pass);
Liam Girdwood8a978232015-05-29 19:06:14 +01002566 break;
Mengdong Linacfc7d42016-01-15 16:13:37 +08002567 case SND_SOC_DOBJ_DAI_LINK:
2568 remove_link(comp, dobj, pass);
2569 break;
Liam Girdwood8a978232015-05-29 19:06:14 +01002570 default:
2571 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2572 dobj->type);
2573 break;
2574 }
2575 }
2576 pass--;
2577 }
2578
2579 /* let caller know if FW can be freed when no objects are left */
2580 return !list_empty(&comp->dobj_list);
2581}
2582EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);