blob: 4d24ac255d2532a64def67a2b04a9011a05ef087 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0+
2//
3// soc-topology.c -- ALSA SoC Topology
4//
5// Copyright (C) 2012 Texas Instruments Inc.
6// Copyright (C) 2015 Intel Corporation.
7//
8// Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9// K, Mythri P <mythri.p.k@intel.com>
10// Prusty, Subhransu S <subhransu.s.prusty@intel.com>
11// B, Jayachandran <jayachandran.b@intel.com>
12// Abdullah, Omair M <omair.m.abdullah@intel.com>
13// Jin, Yao <yao.jin@intel.com>
14// Lin, Mengdong <mengdong.lin@intel.com>
15//
16// Add support to read audio firmware topology alongside firmware text. The
17// topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
18// equalizers, firmware, coefficients etc.
19//
20// This file only manages the core ALSA and ASoC components, all other bespoke
21// firmware topology data is passed to component drivers for bespoke handling.
22
23#include <linux/kernel.h>
24#include <linux/export.h>
25#include <linux/list.h>
26#include <linux/firmware.h>
27#include <linux/slab.h>
28#include <sound/soc.h>
29#include <sound/soc-dapm.h>
30#include <sound/soc-topology.h>
31#include <sound/tlv.h>
32
David Brazdil0f672f62019-12-10 10:32:29 +000033#define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */
34
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035/*
36 * We make several passes over the data (since it wont necessarily be ordered)
37 * and process objects in the following order. This guarantees the component
38 * drivers will be ready with any vendor data before the mixers and DAPM objects
39 * are loaded (that may make use of the vendor data).
40 */
41#define SOC_TPLG_PASS_MANIFEST 0
42#define SOC_TPLG_PASS_VENDOR 1
43#define SOC_TPLG_PASS_MIXER 2
44#define SOC_TPLG_PASS_WIDGET 3
45#define SOC_TPLG_PASS_PCM_DAI 4
46#define SOC_TPLG_PASS_GRAPH 5
47#define SOC_TPLG_PASS_PINS 6
48#define SOC_TPLG_PASS_BE_DAI 7
49#define SOC_TPLG_PASS_LINK 8
50
51#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
52#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
53
54/* topology context */
55struct soc_tplg {
56 const struct firmware *fw;
57
58 /* runtime FW parsing */
59 const u8 *pos; /* read postion */
60 const u8 *hdr_pos; /* header position */
61 unsigned int pass; /* pass number */
62
63 /* component caller */
64 struct device *dev;
65 struct snd_soc_component *comp;
66 u32 index; /* current block index */
67 u32 req_index; /* required index, only loaded/free matching blocks */
68
69 /* vendor specific kcontrol operations */
70 const struct snd_soc_tplg_kcontrol_ops *io_ops;
71 int io_ops_count;
72
73 /* vendor specific bytes ext handlers, for TLV bytes controls */
74 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
75 int bytes_ext_ops_count;
76
77 /* optional fw loading callbacks to component drivers */
78 struct snd_soc_tplg_ops *ops;
79};
80
81static int soc_tplg_process_headers(struct soc_tplg *tplg);
82static void soc_tplg_complete(struct soc_tplg *tplg);
David Brazdil0f672f62019-12-10 10:32:29 +000083static void soc_tplg_denum_remove_texts(struct soc_enum *se);
84static void soc_tplg_denum_remove_values(struct soc_enum *se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085
86/* check we dont overflow the data for this control chunk */
87static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
88 unsigned int count, size_t bytes, const char *elem_type)
89{
90 const u8 *end = tplg->pos + elem_size * count;
91
92 if (end > tplg->fw->data + tplg->fw->size) {
93 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
94 elem_type);
95 return -EINVAL;
96 }
97
98 /* check there is enough room in chunk for control.
99 extra bytes at the end of control are for vendor data here */
100 if (elem_size * count > bytes) {
101 dev_err(tplg->dev,
102 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
103 elem_type, count, elem_size, bytes);
104 return -EINVAL;
105 }
106
107 return 0;
108}
109
110static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
111{
112 const u8 *end = tplg->hdr_pos;
113
114 if (end >= tplg->fw->data + tplg->fw->size)
115 return 1;
116 return 0;
117}
118
119static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
120{
121 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
122}
123
124static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
125{
126 return (unsigned long)(tplg->pos - tplg->fw->data);
127}
128
129/* mapping of Kcontrol types and associated operations. */
130static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
131 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
132 snd_soc_put_volsw, snd_soc_info_volsw},
133 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
134 snd_soc_put_volsw_sx, NULL},
135 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
136 snd_soc_put_enum_double, snd_soc_info_enum_double},
137 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
138 snd_soc_put_enum_double, NULL},
139 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
140 snd_soc_bytes_put, snd_soc_bytes_info},
141 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
142 snd_soc_put_volsw_range, snd_soc_info_volsw_range},
143 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
144 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
145 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
146 snd_soc_put_strobe, NULL},
147 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
148 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
149 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
150 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
151 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
152 snd_soc_dapm_put_enum_double, NULL},
153 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
154 snd_soc_dapm_put_enum_double, NULL},
155 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
156 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
157};
158
159struct soc_tplg_map {
160 int uid;
161 int kid;
162};
163
164/* mapping of widget types from UAPI IDs to kernel IDs */
165static const struct soc_tplg_map dapm_map[] = {
166 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
167 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
168 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
169 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
170 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
171 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
172 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
173 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
174 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
175 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
176 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
177 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
178 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
179 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
180 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
181 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
182 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
183 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
184 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
185 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
186 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
187 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
188 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
189 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
190};
191
192static int tplc_chan_get_reg(struct soc_tplg *tplg,
193 struct snd_soc_tplg_channel *chan, int map)
194{
195 int i;
196
197 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +0000198 if (le32_to_cpu(chan[i].id) == map)
199 return le32_to_cpu(chan[i].reg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200 }
201
202 return -EINVAL;
203}
204
205static int tplc_chan_get_shift(struct soc_tplg *tplg,
206 struct snd_soc_tplg_channel *chan, int map)
207{
208 int i;
209
210 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +0000211 if (le32_to_cpu(chan[i].id) == map)
212 return le32_to_cpu(chan[i].shift);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000213 }
214
215 return -EINVAL;
216}
217
218static int get_widget_id(int tplg_type)
219{
220 int i;
221
222 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
223 if (tplg_type == dapm_map[i].uid)
224 return dapm_map[i].kid;
225 }
226
227 return -EINVAL;
228}
229
230static inline void soc_bind_err(struct soc_tplg *tplg,
231 struct snd_soc_tplg_ctl_hdr *hdr, int index)
232{
233 dev_err(tplg->dev,
234 "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
235 hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
236 soc_tplg_get_offset(tplg));
237}
238
239static inline void soc_control_err(struct soc_tplg *tplg,
240 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
241{
242 dev_err(tplg->dev,
243 "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
244 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
245 soc_tplg_get_offset(tplg));
246}
247
248/* pass vendor data to component driver for processing */
Olivier Deprez157378f2022-04-04 15:47:50 +0200249static int soc_tplg_vendor_load(struct soc_tplg *tplg,
250 struct snd_soc_tplg_hdr *hdr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251{
252 int ret = 0;
253
Olivier Deprez157378f2022-04-04 15:47:50 +0200254 if (tplg->ops && tplg->ops->vendor_load)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
256 else {
257 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
258 hdr->vendor_type);
259 return -EINVAL;
260 }
261
262 if (ret < 0)
263 dev_err(tplg->dev,
264 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
265 soc_tplg_get_hdr_offset(tplg),
266 soc_tplg_get_hdr_offset(tplg),
267 hdr->type, hdr->vendor_type);
268 return ret;
269}
270
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271/* optionally pass new dynamic widget to component driver. This is mainly for
272 * external widgets where we can assign private data/ops */
273static int soc_tplg_widget_load(struct soc_tplg *tplg,
274 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
275{
Olivier Deprez157378f2022-04-04 15:47:50 +0200276 if (tplg->ops && tplg->ops->widget_load)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000277 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
278 tplg_w);
279
280 return 0;
281}
282
283/* optionally pass new dynamic widget to component driver. This is mainly for
284 * external widgets where we can assign private data/ops */
285static int soc_tplg_widget_ready(struct soc_tplg *tplg,
286 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
287{
Olivier Deprez157378f2022-04-04 15:47:50 +0200288 if (tplg->ops && tplg->ops->widget_ready)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
290 tplg_w);
291
292 return 0;
293}
294
295/* pass DAI configurations to component driver for extra initialization */
296static int soc_tplg_dai_load(struct soc_tplg *tplg,
297 struct snd_soc_dai_driver *dai_drv,
298 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
299{
Olivier Deprez157378f2022-04-04 15:47:50 +0200300 if (tplg->ops && tplg->ops->dai_load)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000301 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
302 pcm, dai);
303
304 return 0;
305}
306
307/* pass link configurations to component driver for extra initialization */
308static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
309 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
310{
Olivier Deprez157378f2022-04-04 15:47:50 +0200311 if (tplg->ops && tplg->ops->link_load)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000312 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
313
314 return 0;
315}
316
317/* tell the component driver that all firmware has been loaded in this request */
318static void soc_tplg_complete(struct soc_tplg *tplg)
319{
Olivier Deprez157378f2022-04-04 15:47:50 +0200320 if (tplg->ops && tplg->ops->complete)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321 tplg->ops->complete(tplg->comp);
322}
323
324/* add a dynamic kcontrol */
325static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
326 const struct snd_kcontrol_new *control_new, const char *prefix,
327 void *data, struct snd_kcontrol **kcontrol)
328{
329 int err;
330
331 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
332 if (*kcontrol == NULL) {
333 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
334 control_new->name);
335 return -ENOMEM;
336 }
337
338 err = snd_ctl_add(card, *kcontrol);
339 if (err < 0) {
340 dev_err(dev, "ASoC: Failed to add %s: %d\n",
341 control_new->name, err);
342 return err;
343 }
344
345 return 0;
346}
347
348/* add a dynamic kcontrol for component driver */
349static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
350 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
351{
352 struct snd_soc_component *comp = tplg->comp;
353
354 return soc_tplg_add_dcontrol(comp->card->snd_card,
Olivier Deprez0e641232021-09-23 10:07:05 +0200355 comp->dev, k, comp->name_prefix, comp, kcontrol);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356}
357
358/* remove a mixer kcontrol */
359static void remove_mixer(struct snd_soc_component *comp,
360 struct snd_soc_dobj *dobj, int pass)
361{
362 struct snd_card *card = comp->card->snd_card;
363 struct soc_mixer_control *sm =
364 container_of(dobj, struct soc_mixer_control, dobj);
365 const unsigned int *p = NULL;
366
367 if (pass != SOC_TPLG_PASS_MIXER)
368 return;
369
370 if (dobj->ops && dobj->ops->control_unload)
371 dobj->ops->control_unload(comp, dobj);
372
David Brazdil0f672f62019-12-10 10:32:29 +0000373 if (dobj->control.kcontrol->tlv.p)
374 p = dobj->control.kcontrol->tlv.p;
375 snd_ctl_remove(card, dobj->control.kcontrol);
376 list_del(&dobj->list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000377 kfree(sm);
378 kfree(p);
379}
380
381/* remove an enum kcontrol */
382static void remove_enum(struct snd_soc_component *comp,
383 struct snd_soc_dobj *dobj, int pass)
384{
385 struct snd_card *card = comp->card->snd_card;
386 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000387
388 if (pass != SOC_TPLG_PASS_MIXER)
389 return;
390
391 if (dobj->ops && dobj->ops->control_unload)
392 dobj->ops->control_unload(comp, dobj);
393
David Brazdil0f672f62019-12-10 10:32:29 +0000394 snd_ctl_remove(card, dobj->control.kcontrol);
395 list_del(&dobj->list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000396
David Brazdil0f672f62019-12-10 10:32:29 +0000397 soc_tplg_denum_remove_values(se);
398 soc_tplg_denum_remove_texts(se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 kfree(se);
400}
401
402/* remove a byte kcontrol */
403static void remove_bytes(struct snd_soc_component *comp,
404 struct snd_soc_dobj *dobj, int pass)
405{
406 struct snd_card *card = comp->card->snd_card;
407 struct soc_bytes_ext *sb =
408 container_of(dobj, struct soc_bytes_ext, dobj);
409
410 if (pass != SOC_TPLG_PASS_MIXER)
411 return;
412
413 if (dobj->ops && dobj->ops->control_unload)
414 dobj->ops->control_unload(comp, dobj);
415
David Brazdil0f672f62019-12-10 10:32:29 +0000416 snd_ctl_remove(card, dobj->control.kcontrol);
417 list_del(&dobj->list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000418 kfree(sb);
419}
420
David Brazdil0f672f62019-12-10 10:32:29 +0000421/* remove a route */
422static void remove_route(struct snd_soc_component *comp,
423 struct snd_soc_dobj *dobj, int pass)
424{
425 struct snd_soc_dapm_route *route =
426 container_of(dobj, struct snd_soc_dapm_route, dobj);
427
428 if (pass != SOC_TPLG_PASS_GRAPH)
429 return;
430
431 if (dobj->ops && dobj->ops->dapm_route_unload)
432 dobj->ops->dapm_route_unload(comp, dobj);
433
434 list_del(&dobj->list);
435 kfree(route);
436}
437
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000438/* remove a widget and it's kcontrols - routes must be removed first */
439static void remove_widget(struct snd_soc_component *comp,
440 struct snd_soc_dobj *dobj, int pass)
441{
442 struct snd_card *card = comp->card->snd_card;
443 struct snd_soc_dapm_widget *w =
444 container_of(dobj, struct snd_soc_dapm_widget, dobj);
445 int i;
446
447 if (pass != SOC_TPLG_PASS_WIDGET)
448 return;
449
450 if (dobj->ops && dobj->ops->widget_unload)
451 dobj->ops->widget_unload(comp, dobj);
452
453 if (!w->kcontrols)
454 goto free_news;
455
456 /*
457 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
458 * The enum may either have an array of values or strings.
459 */
460 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
461 /* enumerated widget mixer */
462 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
463 struct snd_kcontrol *kcontrol = w->kcontrols[i];
464 struct soc_enum *se =
465 (struct soc_enum *)kcontrol->private_value;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000466
467 snd_ctl_remove(card, kcontrol);
468
David Brazdil0f672f62019-12-10 10:32:29 +0000469 /* free enum kcontrol's dvalues and dtexts */
470 soc_tplg_denum_remove_values(se);
471 soc_tplg_denum_remove_texts(se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000472
473 kfree(se);
474 kfree(w->kcontrol_news[i].name);
475 }
476 } else {
477 /* volume mixer or bytes controls */
478 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
479 struct snd_kcontrol *kcontrol = w->kcontrols[i];
480
481 if (dobj->widget.kcontrol_type
482 == SND_SOC_TPLG_TYPE_MIXER)
483 kfree(kcontrol->tlv.p);
484
485 /* Private value is used as struct soc_mixer_control
486 * for volume mixers or soc_bytes_ext for bytes
487 * controls.
488 */
489 kfree((void *)kcontrol->private_value);
490 snd_ctl_remove(card, kcontrol);
491 kfree(w->kcontrol_news[i].name);
492 }
493 }
494
495free_news:
496 kfree(w->kcontrol_news);
497
David Brazdil0f672f62019-12-10 10:32:29 +0000498 list_del(&dobj->list);
499
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500 /* widget w is freed by soc-dapm.c */
501}
502
503/* remove DAI configurations */
504static void remove_dai(struct snd_soc_component *comp,
505 struct snd_soc_dobj *dobj, int pass)
506{
507 struct snd_soc_dai_driver *dai_drv =
508 container_of(dobj, struct snd_soc_dai_driver, dobj);
Olivier Deprez157378f2022-04-04 15:47:50 +0200509 struct snd_soc_dai *dai, *_dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510
511 if (pass != SOC_TPLG_PASS_PCM_DAI)
512 return;
513
514 if (dobj->ops && dobj->ops->dai_unload)
515 dobj->ops->dai_unload(comp, dobj);
516
Olivier Deprez157378f2022-04-04 15:47:50 +0200517 for_each_component_dais_safe(comp, dai, _dai)
David Brazdil0f672f62019-12-10 10:32:29 +0000518 if (dai->driver == dai_drv)
Olivier Deprez157378f2022-04-04 15:47:50 +0200519 snd_soc_unregister_dai(dai);
David Brazdil0f672f62019-12-10 10:32:29 +0000520
521 kfree(dai_drv->playback.stream_name);
522 kfree(dai_drv->capture.stream_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 kfree(dai_drv->name);
524 list_del(&dobj->list);
525 kfree(dai_drv);
526}
527
528/* remove link configurations */
529static void remove_link(struct snd_soc_component *comp,
530 struct snd_soc_dobj *dobj, int pass)
531{
532 struct snd_soc_dai_link *link =
533 container_of(dobj, struct snd_soc_dai_link, dobj);
534
535 if (pass != SOC_TPLG_PASS_PCM_DAI)
536 return;
537
538 if (dobj->ops && dobj->ops->link_unload)
539 dobj->ops->link_unload(comp, dobj);
540
Olivier Deprez0e641232021-09-23 10:07:05 +0200541 list_del(&dobj->list);
Olivier Deprez157378f2022-04-04 15:47:50 +0200542 snd_soc_remove_pcm_runtime(comp->card,
543 snd_soc_get_pcm_runtime(comp->card, link));
Olivier Deprez0e641232021-09-23 10:07:05 +0200544
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000545 kfree(link->name);
546 kfree(link->stream_name);
David Brazdil0f672f62019-12-10 10:32:29 +0000547 kfree(link->cpus->dai_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000548 kfree(link);
549}
550
David Brazdil0f672f62019-12-10 10:32:29 +0000551/* unload dai link */
552static void remove_backend_link(struct snd_soc_component *comp,
553 struct snd_soc_dobj *dobj, int pass)
554{
555 if (pass != SOC_TPLG_PASS_LINK)
556 return;
557
558 if (dobj->ops && dobj->ops->link_unload)
559 dobj->ops->link_unload(comp, dobj);
560
561 /*
562 * We don't free the link here as what remove_link() do since BE
563 * links are not allocated by topology.
564 * We however need to reset the dobj type to its initial values
565 */
566 dobj->type = SND_SOC_DOBJ_NONE;
567 list_del(&dobj->list);
568}
569
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570/* bind a kcontrol to it's IO handlers */
571static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
572 struct snd_kcontrol_new *k,
573 const struct soc_tplg *tplg)
574{
575 const struct snd_soc_tplg_kcontrol_ops *ops;
576 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
577 int num_ops, i;
578
David Brazdil0f672f62019-12-10 10:32:29 +0000579 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
581 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
582 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
583 struct soc_bytes_ext *sbe;
584 struct snd_soc_tplg_bytes_control *be;
585
586 sbe = (struct soc_bytes_ext *)k->private_value;
587 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
588
589 /* TLV bytes controls need standard kcontrol info handler,
590 * TLV callback and extended put/get handlers.
591 */
592 k->info = snd_soc_bytes_info_ext;
593 k->tlv.c = snd_soc_bytes_tlv_callback;
594
Olivier Deprez157378f2022-04-04 15:47:50 +0200595 /*
596 * When a topology-based implementation abuses the
597 * control interface and uses bytes_ext controls of
598 * more than 512 bytes, we need to disable the size
599 * checks, otherwise accesses to such controls will
600 * return an -EINVAL error and prevent the card from
601 * being configured.
602 */
603 if (IS_ENABLED(CONFIG_SND_CTL_VALIDATION) && sbe->max > 512)
604 k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
605
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000606 ext_ops = tplg->bytes_ext_ops;
607 num_ops = tplg->bytes_ext_ops_count;
608 for (i = 0; i < num_ops; i++) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200609 if (!sbe->put &&
610 ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000611 sbe->put = ext_ops[i].put;
Olivier Deprez0e641232021-09-23 10:07:05 +0200612 if (!sbe->get &&
613 ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000614 sbe->get = ext_ops[i].get;
615 }
616
Olivier Deprez157378f2022-04-04 15:47:50 +0200617 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618 return -EINVAL;
Olivier Deprez157378f2022-04-04 15:47:50 +0200619 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
620 return -EINVAL;
621 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000622 }
623
624 /* try and map vendor specific kcontrol handlers first */
625 ops = tplg->io_ops;
626 num_ops = tplg->io_ops_count;
627 for (i = 0; i < num_ops; i++) {
628
Olivier Deprez0e641232021-09-23 10:07:05 +0200629 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000630 k->put = ops[i].put;
Olivier Deprez0e641232021-09-23 10:07:05 +0200631 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000632 k->get = ops[i].get;
Olivier Deprez0e641232021-09-23 10:07:05 +0200633 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000634 k->info = ops[i].info;
635 }
636
637 /* vendor specific handlers found ? */
638 if (k->put && k->get && k->info)
639 return 0;
640
641 /* none found so try standard kcontrol handlers */
642 ops = io_ops;
643 num_ops = ARRAY_SIZE(io_ops);
644 for (i = 0; i < num_ops; i++) {
645
Olivier Deprez0e641232021-09-23 10:07:05 +0200646 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000647 k->put = ops[i].put;
Olivier Deprez0e641232021-09-23 10:07:05 +0200648 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649 k->get = ops[i].get;
Olivier Deprez0e641232021-09-23 10:07:05 +0200650 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000651 k->info = ops[i].info;
652 }
653
654 /* standard handlers found ? */
655 if (k->put && k->get && k->info)
656 return 0;
657
658 /* nothing to bind */
659 return -EINVAL;
660}
661
662/* bind a widgets to it's evnt handlers */
663int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
664 const struct snd_soc_tplg_widget_events *events,
665 int num_events, u16 event_type)
666{
667 int i;
668
669 w->event = NULL;
670
671 for (i = 0; i < num_events; i++) {
672 if (event_type == events[i].type) {
673
674 /* found - so assign event */
675 w->event = events[i].event_handler;
676 return 0;
677 }
678 }
679
680 /* not found */
681 return -EINVAL;
682}
683EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
684
685/* optionally pass new dynamic kcontrol to component driver. */
686static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
687 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
688{
Olivier Deprez157378f2022-04-04 15:47:50 +0200689 if (tplg->ops && tplg->ops->control_load)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000690 return tplg->ops->control_load(tplg->comp, tplg->index, k,
691 hdr);
692
693 return 0;
694}
695
696
697static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
698 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
699{
700 unsigned int item_len = 2 * sizeof(unsigned int);
701 unsigned int *p;
702
703 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
704 if (!p)
705 return -ENOMEM;
706
707 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
708 p[1] = item_len;
David Brazdil0f672f62019-12-10 10:32:29 +0000709 p[2] = le32_to_cpu(scale->min);
710 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
711 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000712
713 kc->tlv.p = (void *)p;
714 return 0;
715}
716
717static int soc_tplg_create_tlv(struct soc_tplg *tplg,
718 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
719{
720 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
David Brazdil0f672f62019-12-10 10:32:29 +0000721 u32 access = le32_to_cpu(tc->access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000722
David Brazdil0f672f62019-12-10 10:32:29 +0000723 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724 return 0;
725
David Brazdil0f672f62019-12-10 10:32:29 +0000726 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000727 tplg_tlv = &tc->tlv;
David Brazdil0f672f62019-12-10 10:32:29 +0000728 switch (le32_to_cpu(tplg_tlv->type)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000729 case SNDRV_CTL_TLVT_DB_SCALE:
730 return soc_tplg_create_tlv_db_scale(tplg, kc,
731 &tplg_tlv->scale);
732
733 /* TODO: add support for other TLV types */
734 default:
735 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
736 tplg_tlv->type);
737 return -EINVAL;
738 }
739 }
740
741 return 0;
742}
743
744static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
745 struct snd_kcontrol_new *kc)
746{
747 kfree(kc->tlv.p);
748}
749
750static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
751 size_t size)
752{
753 struct snd_soc_tplg_bytes_control *be;
754 struct soc_bytes_ext *sbe;
755 struct snd_kcontrol_new kc;
Olivier Deprez157378f2022-04-04 15:47:50 +0200756 int i;
757 int err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000758
759 if (soc_tplg_check_elem_count(tplg,
760 sizeof(struct snd_soc_tplg_bytes_control), count,
761 size, "mixer bytes")) {
762 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
763 count);
764 return -EINVAL;
765 }
766
767 for (i = 0; i < count; i++) {
768 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
769
770 /* validate kcontrol */
771 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
772 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
773 return -EINVAL;
774
775 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
776 if (sbe == NULL)
777 return -ENOMEM;
778
779 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
David Brazdil0f672f62019-12-10 10:32:29 +0000780 le32_to_cpu(be->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000781
782 dev_dbg(tplg->dev,
783 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
784 be->hdr.name, be->hdr.access);
785
786 memset(&kc, 0, sizeof(kc));
787 kc.name = be->hdr.name;
788 kc.private_value = (long)sbe;
789 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
David Brazdil0f672f62019-12-10 10:32:29 +0000790 kc.access = le32_to_cpu(be->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000791
David Brazdil0f672f62019-12-10 10:32:29 +0000792 sbe->max = le32_to_cpu(be->max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000793 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
794 sbe->dobj.ops = tplg->ops;
795 INIT_LIST_HEAD(&sbe->dobj.list);
796
797 /* map io handlers */
798 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
799 if (err) {
800 soc_control_err(tplg, &be->hdr, be->hdr.name);
801 kfree(sbe);
Olivier Deprez157378f2022-04-04 15:47:50 +0200802 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000803 }
804
805 /* pass control to driver for optional further init */
806 err = soc_tplg_init_kcontrol(tplg, &kc,
807 (struct snd_soc_tplg_ctl_hdr *)be);
808 if (err < 0) {
809 dev_err(tplg->dev, "ASoC: failed to init %s\n",
810 be->hdr.name);
811 kfree(sbe);
Olivier Deprez157378f2022-04-04 15:47:50 +0200812 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000813 }
814
815 /* register control here */
816 err = soc_tplg_add_kcontrol(tplg, &kc,
817 &sbe->dobj.control.kcontrol);
818 if (err < 0) {
819 dev_err(tplg->dev, "ASoC: failed to add %s\n",
820 be->hdr.name);
821 kfree(sbe);
Olivier Deprez157378f2022-04-04 15:47:50 +0200822 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000823 }
824
825 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
826 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200827 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000828
829}
830
831static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
832 size_t size)
833{
834 struct snd_soc_tplg_mixer_control *mc;
835 struct soc_mixer_control *sm;
836 struct snd_kcontrol_new kc;
Olivier Deprez157378f2022-04-04 15:47:50 +0200837 int i;
838 int err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000839
840 if (soc_tplg_check_elem_count(tplg,
841 sizeof(struct snd_soc_tplg_mixer_control),
842 count, size, "mixers")) {
843
844 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
845 count);
846 return -EINVAL;
847 }
848
849 for (i = 0; i < count; i++) {
850 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
851
852 /* validate kcontrol */
853 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
854 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
855 return -EINVAL;
856
857 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
858 if (sm == NULL)
859 return -ENOMEM;
860 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
David Brazdil0f672f62019-12-10 10:32:29 +0000861 le32_to_cpu(mc->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000862
863 dev_dbg(tplg->dev,
864 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
865 mc->hdr.name, mc->hdr.access);
866
867 memset(&kc, 0, sizeof(kc));
868 kc.name = mc->hdr.name;
869 kc.private_value = (long)sm;
870 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
David Brazdil0f672f62019-12-10 10:32:29 +0000871 kc.access = le32_to_cpu(mc->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000872
873 /* we only support FL/FR channel mapping atm */
874 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
875 SNDRV_CHMAP_FL);
876 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
877 SNDRV_CHMAP_FR);
878 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
879 SNDRV_CHMAP_FL);
880 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
881 SNDRV_CHMAP_FR);
882
David Brazdil0f672f62019-12-10 10:32:29 +0000883 sm->max = le32_to_cpu(mc->max);
884 sm->min = le32_to_cpu(mc->min);
885 sm->invert = le32_to_cpu(mc->invert);
886 sm->platform_max = le32_to_cpu(mc->platform_max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000887 sm->dobj.index = tplg->index;
888 sm->dobj.ops = tplg->ops;
889 sm->dobj.type = SND_SOC_DOBJ_MIXER;
890 INIT_LIST_HEAD(&sm->dobj.list);
891
892 /* map io handlers */
893 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
894 if (err) {
895 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
896 kfree(sm);
Olivier Deprez157378f2022-04-04 15:47:50 +0200897 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000898 }
899
David Brazdil0f672f62019-12-10 10:32:29 +0000900 /* create any TLV data */
Olivier Deprez0e641232021-09-23 10:07:05 +0200901 err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
902 if (err < 0) {
903 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
904 mc->hdr.name);
905 kfree(sm);
Olivier Deprez157378f2022-04-04 15:47:50 +0200906 break;
Olivier Deprez0e641232021-09-23 10:07:05 +0200907 }
David Brazdil0f672f62019-12-10 10:32:29 +0000908
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000909 /* pass control to driver for optional further init */
910 err = soc_tplg_init_kcontrol(tplg, &kc,
911 (struct snd_soc_tplg_ctl_hdr *) mc);
912 if (err < 0) {
913 dev_err(tplg->dev, "ASoC: failed to init %s\n",
914 mc->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +0000915 soc_tplg_free_tlv(tplg, &kc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000916 kfree(sm);
Olivier Deprez157378f2022-04-04 15:47:50 +0200917 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000918 }
919
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000920 /* register control here */
921 err = soc_tplg_add_kcontrol(tplg, &kc,
922 &sm->dobj.control.kcontrol);
923 if (err < 0) {
924 dev_err(tplg->dev, "ASoC: failed to add %s\n",
925 mc->hdr.name);
926 soc_tplg_free_tlv(tplg, &kc);
927 kfree(sm);
Olivier Deprez157378f2022-04-04 15:47:50 +0200928 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000929 }
930
931 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
932 }
933
Olivier Deprez157378f2022-04-04 15:47:50 +0200934 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000935}
936
937static int soc_tplg_denum_create_texts(struct soc_enum *se,
938 struct snd_soc_tplg_enum_control *ec)
939{
940 int i, ret;
941
942 se->dobj.control.dtexts =
David Brazdil0f672f62019-12-10 10:32:29 +0000943 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000944 if (se->dobj.control.dtexts == NULL)
945 return -ENOMEM;
946
Olivier Deprez0e641232021-09-23 10:07:05 +0200947 for (i = 0; i < le32_to_cpu(ec->items); i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000948
949 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
950 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
951 ret = -EINVAL;
952 goto err;
953 }
954
955 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
956 if (!se->dobj.control.dtexts[i]) {
957 ret = -ENOMEM;
958 goto err;
959 }
960 }
961
David Brazdil0f672f62019-12-10 10:32:29 +0000962 se->items = le32_to_cpu(ec->items);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000963 se->texts = (const char * const *)se->dobj.control.dtexts;
964 return 0;
965
966err:
David Brazdil0f672f62019-12-10 10:32:29 +0000967 se->items = i;
968 soc_tplg_denum_remove_texts(se);
969 return ret;
970}
971
972static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
973{
974 int i = se->items;
975
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000976 for (--i; i >= 0; i--)
977 kfree(se->dobj.control.dtexts[i]);
978 kfree(se->dobj.control.dtexts);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000979}
980
981static int soc_tplg_denum_create_values(struct soc_enum *se,
982 struct snd_soc_tplg_enum_control *ec)
983{
David Brazdil0f672f62019-12-10 10:32:29 +0000984 int i;
985
986 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000987 return -EINVAL;
988
David Brazdil0f672f62019-12-10 10:32:29 +0000989 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
Olivier Deprez0e641232021-09-23 10:07:05 +0200990 sizeof(*se->dobj.control.dvalues),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000991 GFP_KERNEL);
992 if (!se->dobj.control.dvalues)
993 return -ENOMEM;
994
David Brazdil0f672f62019-12-10 10:32:29 +0000995 /* convert from little-endian */
996 for (i = 0; i < le32_to_cpu(ec->items); i++) {
997 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
998 }
999
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001000 return 0;
1001}
1002
David Brazdil0f672f62019-12-10 10:32:29 +00001003static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
1004{
1005 kfree(se->dobj.control.dvalues);
1006}
1007
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001008static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
1009 size_t size)
1010{
1011 struct snd_soc_tplg_enum_control *ec;
1012 struct soc_enum *se;
1013 struct snd_kcontrol_new kc;
Olivier Deprez157378f2022-04-04 15:47:50 +02001014 int i;
1015 int err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001016
1017 if (soc_tplg_check_elem_count(tplg,
1018 sizeof(struct snd_soc_tplg_enum_control),
1019 count, size, "enums")) {
1020
1021 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1022 count);
1023 return -EINVAL;
1024 }
1025
1026 for (i = 0; i < count; i++) {
1027 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001028
1029 /* validate kcontrol */
1030 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1031 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1032 return -EINVAL;
1033
1034 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1035 if (se == NULL)
1036 return -ENOMEM;
1037
David Brazdil0f672f62019-12-10 10:32:29 +00001038 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1039 le32_to_cpu(ec->priv.size));
1040
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001041 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1042 ec->hdr.name, ec->items);
1043
1044 memset(&kc, 0, sizeof(kc));
1045 kc.name = ec->hdr.name;
1046 kc.private_value = (long)se;
1047 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
David Brazdil0f672f62019-12-10 10:32:29 +00001048 kc.access = le32_to_cpu(ec->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001049
1050 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1051 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1052 SNDRV_CHMAP_FL);
1053 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1054 SNDRV_CHMAP_FL);
1055
David Brazdil0f672f62019-12-10 10:32:29 +00001056 se->mask = le32_to_cpu(ec->mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057 se->dobj.index = tplg->index;
1058 se->dobj.type = SND_SOC_DOBJ_ENUM;
1059 se->dobj.ops = tplg->ops;
1060 INIT_LIST_HEAD(&se->dobj.list);
1061
David Brazdil0f672f62019-12-10 10:32:29 +00001062 switch (le32_to_cpu(ec->hdr.ops.info)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001063 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1064 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1065 err = soc_tplg_denum_create_values(se, ec);
1066 if (err < 0) {
1067 dev_err(tplg->dev,
1068 "ASoC: could not create values for %s\n",
1069 ec->hdr.name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001070 goto err_denum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001071 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001072 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001073 case SND_SOC_TPLG_CTL_ENUM:
1074 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1075 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1076 err = soc_tplg_denum_create_texts(se, ec);
1077 if (err < 0) {
1078 dev_err(tplg->dev,
1079 "ASoC: could not create texts for %s\n",
1080 ec->hdr.name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001081 goto err_denum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001082 }
1083 break;
1084 default:
Olivier Deprez157378f2022-04-04 15:47:50 +02001085 err = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001086 dev_err(tplg->dev,
1087 "ASoC: invalid enum control type %d for %s\n",
1088 ec->hdr.ops.info, ec->hdr.name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001089 goto err_denum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001090 }
1091
1092 /* map io handlers */
1093 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1094 if (err) {
1095 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001096 goto err_denum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001097 }
1098
1099 /* pass control to driver for optional further init */
1100 err = soc_tplg_init_kcontrol(tplg, &kc,
1101 (struct snd_soc_tplg_ctl_hdr *) ec);
1102 if (err < 0) {
1103 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1104 ec->hdr.name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001105 goto err_denum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001106 }
1107
1108 /* register control here */
Olivier Deprez157378f2022-04-04 15:47:50 +02001109 err = soc_tplg_add_kcontrol(tplg,
1110 &kc, &se->dobj.control.kcontrol);
1111 if (err < 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001112 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1113 ec->hdr.name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001114 goto err_denum;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001115 }
1116
1117 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1118 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001119 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001120
1121err_denum:
1122 kfree(se);
1123 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001124}
1125
1126static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1127 struct snd_soc_tplg_hdr *hdr)
1128{
1129 struct snd_soc_tplg_ctl_hdr *control_hdr;
Olivier Deprez0e641232021-09-23 10:07:05 +02001130 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001131 int i;
1132
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001133 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1134 soc_tplg_get_offset(tplg));
1135
David Brazdil0f672f62019-12-10 10:32:29 +00001136 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001137
1138 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1139
David Brazdil0f672f62019-12-10 10:32:29 +00001140 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001141 dev_err(tplg->dev, "ASoC: invalid control size\n");
1142 return -EINVAL;
1143 }
1144
David Brazdil0f672f62019-12-10 10:32:29 +00001145 switch (le32_to_cpu(control_hdr->ops.info)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001146 case SND_SOC_TPLG_CTL_VOLSW:
1147 case SND_SOC_TPLG_CTL_STROBE:
1148 case SND_SOC_TPLG_CTL_VOLSW_SX:
1149 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1150 case SND_SOC_TPLG_CTL_RANGE:
1151 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1152 case SND_SOC_TPLG_DAPM_CTL_PIN:
Olivier Deprez0e641232021-09-23 10:07:05 +02001153 ret = soc_tplg_dmixer_create(tplg, 1,
1154 le32_to_cpu(hdr->payload_size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001155 break;
1156 case SND_SOC_TPLG_CTL_ENUM:
1157 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1158 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1159 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1160 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Olivier Deprez0e641232021-09-23 10:07:05 +02001161 ret = soc_tplg_denum_create(tplg, 1,
1162 le32_to_cpu(hdr->payload_size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001163 break;
1164 case SND_SOC_TPLG_CTL_BYTES:
Olivier Deprez0e641232021-09-23 10:07:05 +02001165 ret = soc_tplg_dbytes_create(tplg, 1,
1166 le32_to_cpu(hdr->payload_size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001167 break;
1168 default:
1169 soc_bind_err(tplg, control_hdr, i);
1170 return -EINVAL;
1171 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001172 if (ret < 0) {
1173 dev_err(tplg->dev, "ASoC: invalid control\n");
1174 return ret;
1175 }
1176
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001177 }
1178
1179 return 0;
1180}
1181
1182/* optionally pass new dynamic kcontrol to component driver. */
1183static int soc_tplg_add_route(struct soc_tplg *tplg,
1184 struct snd_soc_dapm_route *route)
1185{
Olivier Deprez157378f2022-04-04 15:47:50 +02001186 if (tplg->ops && tplg->ops->dapm_route_load)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001187 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1188 route);
1189
1190 return 0;
1191}
1192
1193static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1194 struct snd_soc_tplg_hdr *hdr)
1195{
1196 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001197 struct snd_soc_tplg_dapm_graph_elem *elem;
David Brazdil0f672f62019-12-10 10:32:29 +00001198 struct snd_soc_dapm_route **routes;
1199 int count, i, j;
1200 int ret = 0;
1201
1202 count = le32_to_cpu(hdr->count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001203
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001204 if (soc_tplg_check_elem_count(tplg,
1205 sizeof(struct snd_soc_tplg_dapm_graph_elem),
David Brazdil0f672f62019-12-10 10:32:29 +00001206 count, le32_to_cpu(hdr->payload_size), "graph")) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001207
1208 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1209 count);
1210 return -EINVAL;
1211 }
1212
1213 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1214 hdr->index);
1215
David Brazdil0f672f62019-12-10 10:32:29 +00001216 /* allocate memory for pointer to array of dapm routes */
1217 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1218 GFP_KERNEL);
1219 if (!routes)
1220 return -ENOMEM;
1221
1222 /*
1223 * allocate memory for each dapm route in the array.
1224 * This needs to be done individually so that
1225 * each route can be freed when it is removed in remove_route().
1226 */
1227 for (i = 0; i < count; i++) {
1228 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1229 if (!routes[i]) {
1230 /* free previously allocated memory */
1231 for (j = 0; j < i; j++)
1232 kfree(routes[j]);
1233
1234 kfree(routes);
1235 return -ENOMEM;
1236 }
1237 }
1238
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001239 for (i = 0; i < count; i++) {
1240 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1241 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1242
1243 /* validate routes */
1244 if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
David Brazdil0f672f62019-12-10 10:32:29 +00001245 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1246 ret = -EINVAL;
1247 break;
1248 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001249 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
David Brazdil0f672f62019-12-10 10:32:29 +00001250 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1251 ret = -EINVAL;
1252 break;
1253 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001254 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
David Brazdil0f672f62019-12-10 10:32:29 +00001255 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1256 ret = -EINVAL;
1257 break;
1258 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001259
David Brazdil0f672f62019-12-10 10:32:29 +00001260 routes[i]->source = elem->source;
1261 routes[i]->sink = elem->sink;
1262
1263 /* set to NULL atm for tplg users */
1264 routes[i]->connected = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001265 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
David Brazdil0f672f62019-12-10 10:32:29 +00001266 routes[i]->control = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001267 else
David Brazdil0f672f62019-12-10 10:32:29 +00001268 routes[i]->control = elem->control;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001269
David Brazdil0f672f62019-12-10 10:32:29 +00001270 /* add route dobj to dobj_list */
1271 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1272 routes[i]->dobj.ops = tplg->ops;
1273 routes[i]->dobj.index = tplg->index;
1274 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1275
Olivier Deprez0e641232021-09-23 10:07:05 +02001276 ret = soc_tplg_add_route(tplg, routes[i]);
1277 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001278 dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
Olivier Deprez0e641232021-09-23 10:07:05 +02001279 /*
1280 * this route was added to the list, it will
1281 * be freed in remove_route() so increment the
1282 * counter to skip it in the error handling
1283 * below.
1284 */
1285 i++;
1286 break;
1287 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001288
1289 /* add route, but keep going if some fail */
David Brazdil0f672f62019-12-10 10:32:29 +00001290 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291 }
1292
Olivier Deprez0e641232021-09-23 10:07:05 +02001293 /*
1294 * free memory allocated for all dapm routes not added to the
1295 * list in case of error
1296 */
1297 if (ret < 0) {
1298 while (i < count)
1299 kfree(routes[i++]);
1300 }
David Brazdil0f672f62019-12-10 10:32:29 +00001301
1302 /*
1303 * free pointer to array of dapm routes as this is no longer needed.
1304 * The memory allocated for each dapm route will be freed
1305 * when it is removed in remove_route().
1306 */
1307 kfree(routes);
1308
1309 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001310}
1311
1312static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1313 struct soc_tplg *tplg, int num_kcontrols)
1314{
1315 struct snd_kcontrol_new *kc;
1316 struct soc_mixer_control *sm;
1317 struct snd_soc_tplg_mixer_control *mc;
1318 int i, err;
1319
1320 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1321 if (kc == NULL)
1322 return NULL;
1323
1324 for (i = 0; i < num_kcontrols; i++) {
1325 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001326
1327 /* validate kcontrol */
1328 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1329 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
David Brazdil0f672f62019-12-10 10:32:29 +00001330 goto err_sm;
1331
1332 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1333 if (sm == NULL)
1334 goto err_sm;
1335
1336 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1337 le32_to_cpu(mc->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001338
1339 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1340 mc->hdr.name, i);
1341
David Brazdil0f672f62019-12-10 10:32:29 +00001342 kc[i].private_value = (long)sm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001343 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1344 if (kc[i].name == NULL)
David Brazdil0f672f62019-12-10 10:32:29 +00001345 goto err_sm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001346 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Olivier Deprez0e641232021-09-23 10:07:05 +02001347 kc[i].access = le32_to_cpu(mc->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001348
1349 /* we only support FL/FR channel mapping atm */
1350 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1351 SNDRV_CHMAP_FL);
1352 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1353 SNDRV_CHMAP_FR);
1354 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1355 SNDRV_CHMAP_FL);
1356 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1357 SNDRV_CHMAP_FR);
1358
Olivier Deprez0e641232021-09-23 10:07:05 +02001359 sm->max = le32_to_cpu(mc->max);
1360 sm->min = le32_to_cpu(mc->min);
1361 sm->invert = le32_to_cpu(mc->invert);
1362 sm->platform_max = le32_to_cpu(mc->platform_max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001363 sm->dobj.index = tplg->index;
1364 INIT_LIST_HEAD(&sm->dobj.list);
1365
1366 /* map io handlers */
1367 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1368 if (err) {
1369 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +00001370 goto err_sm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001371 }
1372
David Brazdil0f672f62019-12-10 10:32:29 +00001373 /* create any TLV data */
Olivier Deprez0e641232021-09-23 10:07:05 +02001374 err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1375 if (err < 0) {
1376 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1377 mc->hdr.name);
Olivier Deprez157378f2022-04-04 15:47:50 +02001378 goto err_sm;
Olivier Deprez0e641232021-09-23 10:07:05 +02001379 }
David Brazdil0f672f62019-12-10 10:32:29 +00001380
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001381 /* pass control to driver for optional further init */
1382 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1383 (struct snd_soc_tplg_ctl_hdr *)mc);
1384 if (err < 0) {
1385 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1386 mc->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +00001387 goto err_sm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001388 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001389 }
1390 return kc;
1391
David Brazdil0f672f62019-12-10 10:32:29 +00001392err_sm:
1393 for (; i >= 0; i--) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001394 soc_tplg_free_tlv(tplg, &kc[i]);
David Brazdil0f672f62019-12-10 10:32:29 +00001395 sm = (struct soc_mixer_control *)kc[i].private_value;
1396 kfree(sm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001397 kfree(kc[i].name);
1398 }
1399 kfree(kc);
David Brazdil0f672f62019-12-10 10:32:29 +00001400
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001401 return NULL;
1402}
1403
1404static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1405 struct soc_tplg *tplg, int num_kcontrols)
1406{
1407 struct snd_kcontrol_new *kc;
1408 struct snd_soc_tplg_enum_control *ec;
1409 struct soc_enum *se;
David Brazdil0f672f62019-12-10 10:32:29 +00001410 int i, err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001411
1412 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1413 if (kc == NULL)
1414 return NULL;
1415
1416 for (i = 0; i < num_kcontrols; i++) {
1417 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1418 /* validate kcontrol */
1419 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1420 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
David Brazdil0f672f62019-12-10 10:32:29 +00001421 goto err_se;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001422
1423 se = kzalloc(sizeof(*se), GFP_KERNEL);
1424 if (se == NULL)
David Brazdil0f672f62019-12-10 10:32:29 +00001425 goto err_se;
1426
1427 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Olivier Deprez0e641232021-09-23 10:07:05 +02001428 le32_to_cpu(ec->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001429
1430 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1431 ec->hdr.name);
1432
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001433 kc[i].private_value = (long)se;
David Brazdil0f672f62019-12-10 10:32:29 +00001434 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
1435 if (kc[i].name == NULL)
1436 goto err_se;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001437 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Olivier Deprez0e641232021-09-23 10:07:05 +02001438 kc[i].access = le32_to_cpu(ec->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001439
1440 /* we only support FL/FR channel mapping atm */
1441 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1442 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1443 SNDRV_CHMAP_FL);
1444 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1445 SNDRV_CHMAP_FR);
1446
Olivier Deprez0e641232021-09-23 10:07:05 +02001447 se->items = le32_to_cpu(ec->items);
1448 se->mask = le32_to_cpu(ec->mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449 se->dobj.index = tplg->index;
1450
David Brazdil0f672f62019-12-10 10:32:29 +00001451 switch (le32_to_cpu(ec->hdr.ops.info)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001452 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1453 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1454 err = soc_tplg_denum_create_values(se, ec);
1455 if (err < 0) {
1456 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1457 ec->hdr.name);
1458 goto err_se;
1459 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001460 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001461 case SND_SOC_TPLG_CTL_ENUM:
1462 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1463 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1464 err = soc_tplg_denum_create_texts(se, ec);
1465 if (err < 0) {
1466 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1467 ec->hdr.name);
1468 goto err_se;
1469 }
1470 break;
1471 default:
1472 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1473 ec->hdr.ops.info, ec->hdr.name);
1474 goto err_se;
1475 }
1476
1477 /* map io handlers */
1478 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1479 if (err) {
1480 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1481 goto err_se;
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 *)ec);
1487 if (err < 0) {
1488 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1489 ec->hdr.name);
1490 goto err_se;
1491 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001492 }
1493
1494 return kc;
1495
1496err_se:
1497 for (; i >= 0; i--) {
1498 /* free values and texts */
1499 se = (struct soc_enum *)kc[i].private_value;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001500
David Brazdil0f672f62019-12-10 10:32:29 +00001501 if (se) {
1502 soc_tplg_denum_remove_values(se);
1503 soc_tplg_denum_remove_texts(se);
1504 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001505
1506 kfree(se);
1507 kfree(kc[i].name);
1508 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001509 kfree(kc);
1510
1511 return NULL;
1512}
1513
1514static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
David Brazdil0f672f62019-12-10 10:32:29 +00001515 struct soc_tplg *tplg, int num_kcontrols)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001516{
1517 struct snd_soc_tplg_bytes_control *be;
David Brazdil0f672f62019-12-10 10:32:29 +00001518 struct soc_bytes_ext *sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001519 struct snd_kcontrol_new *kc;
1520 int i, err;
1521
David Brazdil0f672f62019-12-10 10:32:29 +00001522 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001523 if (!kc)
1524 return NULL;
1525
David Brazdil0f672f62019-12-10 10:32:29 +00001526 for (i = 0; i < num_kcontrols; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001527 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1528
1529 /* validate kcontrol */
1530 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1531 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
David Brazdil0f672f62019-12-10 10:32:29 +00001532 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001533
1534 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1535 if (sbe == NULL)
David Brazdil0f672f62019-12-10 10:32:29 +00001536 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001537
1538 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
David Brazdil0f672f62019-12-10 10:32:29 +00001539 le32_to_cpu(be->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001540
1541 dev_dbg(tplg->dev,
1542 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1543 be->hdr.name, be->hdr.access);
1544
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001545 kc[i].private_value = (long)sbe;
David Brazdil0f672f62019-12-10 10:32:29 +00001546 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
1547 if (kc[i].name == NULL)
1548 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001549 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Olivier Deprez0e641232021-09-23 10:07:05 +02001550 kc[i].access = le32_to_cpu(be->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001551
Olivier Deprez0e641232021-09-23 10:07:05 +02001552 sbe->max = le32_to_cpu(be->max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001553 INIT_LIST_HEAD(&sbe->dobj.list);
1554
1555 /* map standard io handlers and check for external handlers */
1556 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1557 if (err) {
1558 soc_control_err(tplg, &be->hdr, be->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +00001559 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001560 }
1561
1562 /* pass control to driver for optional further init */
1563 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1564 (struct snd_soc_tplg_ctl_hdr *)be);
1565 if (err < 0) {
1566 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1567 be->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +00001568 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001569 }
1570 }
1571
1572 return kc;
1573
David Brazdil0f672f62019-12-10 10:32:29 +00001574err_sbe:
1575 for (; i >= 0; i--) {
1576 sbe = (struct soc_bytes_ext *)kc[i].private_value;
1577 kfree(sbe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001578 kfree(kc[i].name);
1579 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001580 kfree(kc);
David Brazdil0f672f62019-12-10 10:32:29 +00001581
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001582 return NULL;
1583}
1584
1585static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1586 struct snd_soc_tplg_dapm_widget *w)
1587{
1588 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1589 struct snd_soc_dapm_widget template, *widget;
1590 struct snd_soc_tplg_ctl_hdr *control_hdr;
1591 struct snd_soc_card *card = tplg->comp->card;
1592 unsigned int kcontrol_type;
1593 int ret = 0;
1594
1595 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1596 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1597 return -EINVAL;
1598 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1599 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1600 return -EINVAL;
1601
1602 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1603 w->name, w->id);
1604
1605 memset(&template, 0, sizeof(template));
1606
1607 /* map user to kernel widget ID */
David Brazdil0f672f62019-12-10 10:32:29 +00001608 template.id = get_widget_id(le32_to_cpu(w->id));
1609 if ((int)template.id < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001610 return template.id;
1611
1612 /* strings are allocated here, but used and freed by the widget */
1613 template.name = kstrdup(w->name, GFP_KERNEL);
1614 if (!template.name)
1615 return -ENOMEM;
1616 template.sname = kstrdup(w->sname, GFP_KERNEL);
1617 if (!template.sname) {
1618 ret = -ENOMEM;
1619 goto err;
1620 }
David Brazdil0f672f62019-12-10 10:32:29 +00001621 template.reg = le32_to_cpu(w->reg);
1622 template.shift = le32_to_cpu(w->shift);
1623 template.mask = le32_to_cpu(w->mask);
1624 template.subseq = le32_to_cpu(w->subseq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001625 template.on_val = w->invert ? 0 : 1;
1626 template.off_val = w->invert ? 1 : 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001627 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1628 template.event_flags = le16_to_cpu(w->event_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001629 template.dobj.index = tplg->index;
1630
1631 tplg->pos +=
David Brazdil0f672f62019-12-10 10:32:29 +00001632 (sizeof(struct snd_soc_tplg_dapm_widget) +
1633 le32_to_cpu(w->priv.size));
1634
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001635 if (w->num_kcontrols == 0) {
1636 kcontrol_type = 0;
1637 template.num_kcontrols = 0;
1638 goto widget;
1639 }
1640
1641 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1642 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1643 w->name, w->num_kcontrols, control_hdr->type);
1644
David Brazdil0f672f62019-12-10 10:32:29 +00001645 switch (le32_to_cpu(control_hdr->ops.info)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001646 case SND_SOC_TPLG_CTL_VOLSW:
1647 case SND_SOC_TPLG_CTL_STROBE:
1648 case SND_SOC_TPLG_CTL_VOLSW_SX:
1649 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1650 case SND_SOC_TPLG_CTL_RANGE:
1651 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1652 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
David Brazdil0f672f62019-12-10 10:32:29 +00001653 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001654 template.kcontrol_news =
1655 soc_tplg_dapm_widget_dmixer_create(tplg,
1656 template.num_kcontrols);
1657 if (!template.kcontrol_news) {
1658 ret = -ENOMEM;
1659 goto hdr_err;
1660 }
1661 break;
1662 case SND_SOC_TPLG_CTL_ENUM:
1663 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1664 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1665 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1666 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1667 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
David Brazdil0f672f62019-12-10 10:32:29 +00001668 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001669 template.kcontrol_news =
1670 soc_tplg_dapm_widget_denum_create(tplg,
1671 template.num_kcontrols);
1672 if (!template.kcontrol_news) {
1673 ret = -ENOMEM;
1674 goto hdr_err;
1675 }
1676 break;
1677 case SND_SOC_TPLG_CTL_BYTES:
1678 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
David Brazdil0f672f62019-12-10 10:32:29 +00001679 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001680 template.kcontrol_news =
1681 soc_tplg_dapm_widget_dbytes_create(tplg,
1682 template.num_kcontrols);
1683 if (!template.kcontrol_news) {
1684 ret = -ENOMEM;
1685 goto hdr_err;
1686 }
1687 break;
1688 default:
1689 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1690 control_hdr->ops.get, control_hdr->ops.put,
David Brazdil0f672f62019-12-10 10:32:29 +00001691 le32_to_cpu(control_hdr->ops.info));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001692 ret = -EINVAL;
1693 goto hdr_err;
1694 }
1695
1696widget:
1697 ret = soc_tplg_widget_load(tplg, &template, w);
1698 if (ret < 0)
1699 goto hdr_err;
1700
1701 /* card dapm mutex is held by the core if we are loading topology
1702 * data during sound card init. */
1703 if (card->instantiated)
1704 widget = snd_soc_dapm_new_control(dapm, &template);
1705 else
1706 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1707 if (IS_ERR(widget)) {
1708 ret = PTR_ERR(widget);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001709 goto hdr_err;
1710 }
1711
1712 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1713 widget->dobj.widget.kcontrol_type = kcontrol_type;
1714 widget->dobj.ops = tplg->ops;
1715 widget->dobj.index = tplg->index;
1716 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1717
1718 ret = soc_tplg_widget_ready(tplg, widget, w);
1719 if (ret < 0)
1720 goto ready_err;
1721
David Brazdil0f672f62019-12-10 10:32:29 +00001722 kfree(template.sname);
1723 kfree(template.name);
1724
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001725 return 0;
1726
1727ready_err:
1728 snd_soc_tplg_widget_remove(widget);
1729 snd_soc_dapm_free_widget(widget);
1730hdr_err:
1731 kfree(template.sname);
1732err:
1733 kfree(template.name);
1734 return ret;
1735}
1736
1737static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1738 struct snd_soc_tplg_hdr *hdr)
1739{
1740 struct snd_soc_tplg_dapm_widget *widget;
David Brazdil0f672f62019-12-10 10:32:29 +00001741 int ret, count, i;
1742
1743 count = le32_to_cpu(hdr->count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001744
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001745 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1746
1747 for (i = 0; i < count; i++) {
1748 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00001749 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001750 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1751 return -EINVAL;
1752 }
1753
1754 ret = soc_tplg_dapm_widget_create(tplg, widget);
1755 if (ret < 0) {
1756 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1757 widget->name);
1758 return ret;
1759 }
1760 }
1761
1762 return 0;
1763}
1764
1765static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1766{
1767 struct snd_soc_card *card = tplg->comp->card;
1768 int ret;
1769
1770 /* Card might not have been registered at this point.
1771 * If so, just return success.
1772 */
1773 if (!card || !card->instantiated) {
1774 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1775 " widget card binding deferred\n");
1776 return 0;
1777 }
1778
1779 ret = snd_soc_dapm_new_widgets(card);
1780 if (ret < 0)
1781 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1782 ret);
1783
1784 return 0;
1785}
1786
Olivier Deprez157378f2022-04-04 15:47:50 +02001787static int set_stream_info(struct snd_soc_pcm_stream *stream,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001788 struct snd_soc_tplg_stream_caps *caps)
1789{
1790 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +02001791 if (!stream->stream_name)
1792 return -ENOMEM;
1793
David Brazdil0f672f62019-12-10 10:32:29 +00001794 stream->channels_min = le32_to_cpu(caps->channels_min);
1795 stream->channels_max = le32_to_cpu(caps->channels_max);
1796 stream->rates = le32_to_cpu(caps->rates);
1797 stream->rate_min = le32_to_cpu(caps->rate_min);
1798 stream->rate_max = le32_to_cpu(caps->rate_max);
1799 stream->formats = le64_to_cpu(caps->formats);
1800 stream->sig_bits = le32_to_cpu(caps->sig_bits);
Olivier Deprez157378f2022-04-04 15:47:50 +02001801
1802 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001803}
1804
1805static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1806 unsigned int flag_mask, unsigned int flags)
1807{
1808 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1809 dai_drv->symmetric_rates =
1810 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1811
1812 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1813 dai_drv->symmetric_channels =
1814 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1815 1 : 0;
1816
1817 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1818 dai_drv->symmetric_samplebits =
1819 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1820 1 : 0;
1821}
1822
1823static int soc_tplg_dai_create(struct soc_tplg *tplg,
1824 struct snd_soc_tplg_pcm *pcm)
1825{
1826 struct snd_soc_dai_driver *dai_drv;
1827 struct snd_soc_pcm_stream *stream;
1828 struct snd_soc_tplg_stream_caps *caps;
Olivier Deprez157378f2022-04-04 15:47:50 +02001829 struct snd_soc_dai *dai;
1830 struct snd_soc_dapm_context *dapm =
1831 snd_soc_component_get_dapm(tplg->comp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001832 int ret;
1833
1834 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1835 if (dai_drv == NULL)
1836 return -ENOMEM;
1837
Olivier Deprez157378f2022-04-04 15:47:50 +02001838 if (strlen(pcm->dai_name)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001839 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +02001840 if (!dai_drv->name) {
1841 ret = -ENOMEM;
1842 goto err;
1843 }
1844 }
David Brazdil0f672f62019-12-10 10:32:29 +00001845 dai_drv->id = le32_to_cpu(pcm->dai_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001846
1847 if (pcm->playback) {
1848 stream = &dai_drv->playback;
1849 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Olivier Deprez157378f2022-04-04 15:47:50 +02001850 ret = set_stream_info(stream, caps);
1851 if (ret < 0)
1852 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001853 }
1854
1855 if (pcm->capture) {
1856 stream = &dai_drv->capture;
1857 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Olivier Deprez157378f2022-04-04 15:47:50 +02001858 ret = set_stream_info(stream, caps);
1859 if (ret < 0)
1860 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001861 }
1862
1863 if (pcm->compress)
1864 dai_drv->compress_new = snd_soc_new_compress;
1865
1866 /* pass control to component driver for optional further init */
1867 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1868 if (ret < 0) {
1869 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Olivier Deprez157378f2022-04-04 15:47:50 +02001870 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001871 }
1872
1873 dai_drv->dobj.index = tplg->index;
1874 dai_drv->dobj.ops = tplg->ops;
1875 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1876 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1877
1878 /* register the DAI to the component */
Olivier Deprez157378f2022-04-04 15:47:50 +02001879 dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1880 if (!dai)
1881 return -ENOMEM;
1882
1883 /* Create the DAI widgets here */
1884 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1885 if (ret != 0) {
1886 dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1887 snd_soc_unregister_dai(dai);
1888 return ret;
1889 }
1890
1891 return 0;
1892
1893err:
1894 kfree(dai_drv->playback.stream_name);
1895 kfree(dai_drv->capture.stream_name);
1896 kfree(dai_drv->name);
1897 kfree(dai_drv);
1898
1899 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001900}
1901
1902static void set_link_flags(struct snd_soc_dai_link *link,
1903 unsigned int flag_mask, unsigned int flags)
1904{
1905 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1906 link->symmetric_rates =
1907 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1908
1909 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1910 link->symmetric_channels =
1911 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1912 1 : 0;
1913
1914 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1915 link->symmetric_samplebits =
1916 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1917 1 : 0;
1918
1919 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1920 link->ignore_suspend =
1921 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1922 1 : 0;
1923}
1924
1925/* create the FE DAI link */
1926static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1927 struct snd_soc_tplg_pcm *pcm)
1928{
1929 struct snd_soc_dai_link *link;
David Brazdil0f672f62019-12-10 10:32:29 +00001930 struct snd_soc_dai_link_component *dlc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001931 int ret;
1932
David Brazdil0f672f62019-12-10 10:32:29 +00001933 /* link + cpu + codec + platform */
1934 link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001935 if (link == NULL)
1936 return -ENOMEM;
1937
David Brazdil0f672f62019-12-10 10:32:29 +00001938 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1939
1940 link->cpus = &dlc[0];
1941 link->codecs = &dlc[1];
1942 link->platforms = &dlc[2];
1943
1944 link->num_cpus = 1;
1945 link->num_codecs = 1;
1946 link->num_platforms = 1;
1947
Olivier Deprez0e641232021-09-23 10:07:05 +02001948 link->dobj.index = tplg->index;
1949 link->dobj.ops = tplg->ops;
1950 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1951
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001952 if (strlen(pcm->pcm_name)) {
1953 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1954 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +02001955 if (!link->name || !link->stream_name) {
1956 ret = -ENOMEM;
1957 goto err;
1958 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001959 }
David Brazdil0f672f62019-12-10 10:32:29 +00001960 link->id = le32_to_cpu(pcm->pcm_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001961
Olivier Deprez157378f2022-04-04 15:47:50 +02001962 if (strlen(pcm->dai_name)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001963 link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
Olivier Deprez157378f2022-04-04 15:47:50 +02001964 if (!link->cpus->dai_name) {
1965 ret = -ENOMEM;
1966 goto err;
1967 }
1968 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001969
David Brazdil0f672f62019-12-10 10:32:29 +00001970 link->codecs->name = "snd-soc-dummy";
1971 link->codecs->dai_name = "snd-soc-dummy-dai";
1972
1973 link->platforms->name = "snd-soc-dummy";
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001974
1975 /* enable DPCM */
1976 link->dynamic = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001977 link->dpcm_playback = le32_to_cpu(pcm->playback);
1978 link->dpcm_capture = le32_to_cpu(pcm->capture);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001979 if (pcm->flag_mask)
David Brazdil0f672f62019-12-10 10:32:29 +00001980 set_link_flags(link,
1981 le32_to_cpu(pcm->flag_mask),
1982 le32_to_cpu(pcm->flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001983
1984 /* pass control to component driver for optional further init */
1985 ret = soc_tplg_dai_link_load(tplg, link, NULL);
1986 if (ret < 0) {
1987 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
Olivier Deprez0e641232021-09-23 10:07:05 +02001988 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001989 }
1990
Olivier Deprez157378f2022-04-04 15:47:50 +02001991 ret = snd_soc_add_pcm_runtime(tplg->comp->card, link);
Olivier Deprez0e641232021-09-23 10:07:05 +02001992 if (ret < 0) {
1993 dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n");
1994 goto err;
1995 }
1996
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001997 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1998
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001999 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02002000err:
2001 kfree(link->name);
2002 kfree(link->stream_name);
2003 kfree(link->cpus->dai_name);
2004 kfree(link);
2005 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002006}
2007
2008/* create a FE DAI and DAI link from the PCM object */
2009static int soc_tplg_pcm_create(struct soc_tplg *tplg,
2010 struct snd_soc_tplg_pcm *pcm)
2011{
2012 int ret;
2013
2014 ret = soc_tplg_dai_create(tplg, pcm);
2015 if (ret < 0)
2016 return ret;
2017
2018 return soc_tplg_fe_link_create(tplg, pcm);
2019}
2020
2021/* copy stream caps from the old version 4 of source */
2022static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
2023 struct snd_soc_tplg_stream_caps_v4 *src)
2024{
David Brazdil0f672f62019-12-10 10:32:29 +00002025 dest->size = cpu_to_le32(sizeof(*dest));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002026 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2027 dest->formats = src->formats;
2028 dest->rates = src->rates;
2029 dest->rate_min = src->rate_min;
2030 dest->rate_max = src->rate_max;
2031 dest->channels_min = src->channels_min;
2032 dest->channels_max = src->channels_max;
2033 dest->periods_min = src->periods_min;
2034 dest->periods_max = src->periods_max;
2035 dest->period_size_min = src->period_size_min;
2036 dest->period_size_max = src->period_size_max;
2037 dest->buffer_size_min = src->buffer_size_min;
2038 dest->buffer_size_max = src->buffer_size_max;
2039}
2040
2041/**
2042 * pcm_new_ver - Create the new version of PCM from the old version.
2043 * @tplg: topology context
2044 * @src: older version of pcm as a source
2045 * @pcm: latest version of pcm created from the source
2046 *
2047 * Support from vesion 4. User should free the returned pcm manually.
2048 */
2049static int pcm_new_ver(struct soc_tplg *tplg,
2050 struct snd_soc_tplg_pcm *src,
2051 struct snd_soc_tplg_pcm **pcm)
2052{
2053 struct snd_soc_tplg_pcm *dest;
2054 struct snd_soc_tplg_pcm_v4 *src_v4;
2055 int i;
2056
2057 *pcm = NULL;
2058
David Brazdil0f672f62019-12-10 10:32:29 +00002059 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002060 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
2061 return -EINVAL;
2062 }
2063
2064 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
2065 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
2066 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2067 if (!dest)
2068 return -ENOMEM;
2069
David Brazdil0f672f62019-12-10 10:32:29 +00002070 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002071 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2072 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2073 dest->pcm_id = src_v4->pcm_id;
2074 dest->dai_id = src_v4->dai_id;
2075 dest->playback = src_v4->playback;
2076 dest->capture = src_v4->capture;
2077 dest->compress = src_v4->compress;
2078 dest->num_streams = src_v4->num_streams;
David Brazdil0f672f62019-12-10 10:32:29 +00002079 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002080 memcpy(&dest->stream[i], &src_v4->stream[i],
2081 sizeof(struct snd_soc_tplg_stream));
2082
2083 for (i = 0; i < 2; i++)
2084 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2085
2086 *pcm = dest;
2087 return 0;
2088}
2089
2090static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
2091 struct snd_soc_tplg_hdr *hdr)
2092{
2093 struct snd_soc_tplg_pcm *pcm, *_pcm;
David Brazdil0f672f62019-12-10 10:32:29 +00002094 int count;
2095 int size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002096 int i;
2097 bool abi_match;
Olivier Deprez0e641232021-09-23 10:07:05 +02002098 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002099
David Brazdil0f672f62019-12-10 10:32:29 +00002100 count = le32_to_cpu(hdr->count);
2101
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002102 /* check the element size and count */
2103 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002104 size = le32_to_cpu(pcm->size);
2105 if (size > sizeof(struct snd_soc_tplg_pcm)
2106 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002107 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002108 size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002109 return -EINVAL;
2110 }
2111
2112 if (soc_tplg_check_elem_count(tplg,
David Brazdil0f672f62019-12-10 10:32:29 +00002113 size, count,
2114 le32_to_cpu(hdr->payload_size),
2115 "PCM DAI")) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002116 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2117 count);
2118 return -EINVAL;
2119 }
2120
2121 for (i = 0; i < count; i++) {
2122 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002123 size = le32_to_cpu(pcm->size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002124
2125 /* check ABI version by size, create a new version of pcm
2126 * if abi not match.
2127 */
David Brazdil0f672f62019-12-10 10:32:29 +00002128 if (size == sizeof(*pcm)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002129 abi_match = true;
2130 _pcm = pcm;
2131 } else {
2132 abi_match = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02002133 ret = pcm_new_ver(tplg, pcm, &_pcm);
2134 if (ret < 0)
2135 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002136 }
2137
2138 /* create the FE DAIs and DAI links */
Olivier Deprez0e641232021-09-23 10:07:05 +02002139 ret = soc_tplg_pcm_create(tplg, _pcm);
2140 if (ret < 0) {
2141 if (!abi_match)
2142 kfree(_pcm);
2143 return ret;
2144 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002145
2146 /* offset by version-specific struct size and
2147 * real priv data size
2148 */
David Brazdil0f672f62019-12-10 10:32:29 +00002149 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002150
2151 if (!abi_match)
2152 kfree(_pcm); /* free the duplicated one */
2153 }
2154
2155 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
2156
2157 return 0;
2158}
2159
2160/**
2161 * set_link_hw_format - Set the HW audio format of the physical DAI link.
2162 * @link: &snd_soc_dai_link which should be updated
2163 * @cfg: physical link configs.
2164 *
2165 * Topology context contains a list of supported HW formats (configs) and
2166 * a default format ID for the physical link. This function will use this
2167 * default ID to choose the HW format to set the link's DAI format for init.
2168 */
2169static void set_link_hw_format(struct snd_soc_dai_link *link,
2170 struct snd_soc_tplg_link_config *cfg)
2171{
2172 struct snd_soc_tplg_hw_config *hw_config;
2173 unsigned char bclk_master, fsync_master;
2174 unsigned char invert_bclk, invert_fsync;
2175 int i;
2176
David Brazdil0f672f62019-12-10 10:32:29 +00002177 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002178 hw_config = &cfg->hw_config[i];
2179 if (hw_config->id != cfg->default_hw_config_id)
2180 continue;
2181
David Brazdil0f672f62019-12-10 10:32:29 +00002182 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2183 SND_SOC_DAIFMT_FORMAT_MASK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002184
2185 /* clock gating */
2186 switch (hw_config->clock_gated) {
2187 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2188 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2189 break;
2190
2191 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2192 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2193 break;
2194
2195 default:
2196 /* ignore the value */
2197 break;
2198 }
2199
2200 /* clock signal polarity */
2201 invert_bclk = hw_config->invert_bclk;
2202 invert_fsync = hw_config->invert_fsync;
2203 if (!invert_bclk && !invert_fsync)
2204 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2205 else if (!invert_bclk && invert_fsync)
2206 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2207 else if (invert_bclk && !invert_fsync)
2208 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2209 else
2210 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2211
2212 /* clock masters */
2213 bclk_master = (hw_config->bclk_master ==
2214 SND_SOC_TPLG_BCLK_CM);
2215 fsync_master = (hw_config->fsync_master ==
2216 SND_SOC_TPLG_FSYNC_CM);
2217 if (bclk_master && fsync_master)
2218 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2219 else if (!bclk_master && fsync_master)
2220 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2221 else if (bclk_master && !fsync_master)
2222 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2223 else
2224 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2225 }
2226}
2227
2228/**
2229 * link_new_ver - Create a new physical link config from the old
2230 * version of source.
2231 * @tplg: topology context
2232 * @src: old version of phyical link config as a source
2233 * @link: latest version of physical link config created from the source
2234 *
2235 * Support from vesion 4. User need free the returned link config manually.
2236 */
2237static int link_new_ver(struct soc_tplg *tplg,
2238 struct snd_soc_tplg_link_config *src,
2239 struct snd_soc_tplg_link_config **link)
2240{
2241 struct snd_soc_tplg_link_config *dest;
2242 struct snd_soc_tplg_link_config_v4 *src_v4;
2243 int i;
2244
2245 *link = NULL;
2246
David Brazdil0f672f62019-12-10 10:32:29 +00002247 if (le32_to_cpu(src->size) !=
2248 sizeof(struct snd_soc_tplg_link_config_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002249 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2250 return -EINVAL;
2251 }
2252
2253 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2254
2255 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2256 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2257 if (!dest)
2258 return -ENOMEM;
2259
David Brazdil0f672f62019-12-10 10:32:29 +00002260 dest->size = cpu_to_le32(sizeof(*dest));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002261 dest->id = src_v4->id;
2262 dest->num_streams = src_v4->num_streams;
David Brazdil0f672f62019-12-10 10:32:29 +00002263 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002264 memcpy(&dest->stream[i], &src_v4->stream[i],
2265 sizeof(struct snd_soc_tplg_stream));
2266
2267 *link = dest;
2268 return 0;
2269}
2270
Olivier Deprez157378f2022-04-04 15:47:50 +02002271/**
2272 * snd_soc_find_dai_link - Find a DAI link
2273 *
2274 * @card: soc card
2275 * @id: DAI link ID to match
2276 * @name: DAI link name to match, optional
2277 * @stream_name: DAI link stream name to match, optional
2278 *
2279 * This function will search all existing DAI links of the soc card to
2280 * find the link of the same ID. Since DAI links may not have their
2281 * unique ID, so name and stream name should also match if being
2282 * specified.
2283 *
2284 * Return: pointer of DAI link, or NULL if not found.
2285 */
2286static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
2287 int id, const char *name,
2288 const char *stream_name)
2289{
2290 struct snd_soc_pcm_runtime *rtd;
2291 struct snd_soc_dai_link *link;
2292
2293 for_each_card_rtds(card, rtd) {
2294 link = rtd->dai_link;
2295
2296 if (link->id != id)
2297 continue;
2298
2299 if (name && (!link->name || strcmp(name, link->name)))
2300 continue;
2301
2302 if (stream_name && (!link->stream_name
2303 || strcmp(stream_name, link->stream_name)))
2304 continue;
2305
2306 return link;
2307 }
2308
2309 return NULL;
2310}
2311
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002312/* Find and configure an existing physical DAI link */
2313static int soc_tplg_link_config(struct soc_tplg *tplg,
2314 struct snd_soc_tplg_link_config *cfg)
2315{
2316 struct snd_soc_dai_link *link;
2317 const char *name, *stream_name;
2318 size_t len;
2319 int ret;
2320
2321 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2322 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2323 return -EINVAL;
2324 else if (len)
2325 name = cfg->name;
2326 else
2327 name = NULL;
2328
2329 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2330 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2331 return -EINVAL;
2332 else if (len)
2333 stream_name = cfg->stream_name;
2334 else
2335 stream_name = NULL;
2336
David Brazdil0f672f62019-12-10 10:32:29 +00002337 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002338 name, stream_name);
2339 if (!link) {
2340 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2341 name, cfg->id);
2342 return -EINVAL;
2343 }
2344
2345 /* hw format */
2346 if (cfg->num_hw_configs)
2347 set_link_hw_format(link, cfg);
2348
2349 /* flags */
2350 if (cfg->flag_mask)
David Brazdil0f672f62019-12-10 10:32:29 +00002351 set_link_flags(link,
2352 le32_to_cpu(cfg->flag_mask),
2353 le32_to_cpu(cfg->flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002354
2355 /* pass control to component driver for optional further init */
2356 ret = soc_tplg_dai_link_load(tplg, link, cfg);
2357 if (ret < 0) {
2358 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2359 return ret;
2360 }
2361
David Brazdil0f672f62019-12-10 10:32:29 +00002362 /* for unloading it in snd_soc_tplg_component_remove */
2363 link->dobj.index = tplg->index;
2364 link->dobj.ops = tplg->ops;
2365 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2366 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2367
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002368 return 0;
2369}
2370
2371
2372/* Load physical link config elements from the topology context */
2373static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2374 struct snd_soc_tplg_hdr *hdr)
2375{
2376 struct snd_soc_tplg_link_config *link, *_link;
David Brazdil0f672f62019-12-10 10:32:29 +00002377 int count;
2378 int size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002379 int i, ret;
2380 bool abi_match;
2381
David Brazdil0f672f62019-12-10 10:32:29 +00002382 count = le32_to_cpu(hdr->count);
2383
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002384 /* check the element size and count */
2385 link = (struct snd_soc_tplg_link_config *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002386 size = le32_to_cpu(link->size);
2387 if (size > sizeof(struct snd_soc_tplg_link_config)
2388 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002389 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002390 size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002391 return -EINVAL;
2392 }
2393
2394 if (soc_tplg_check_elem_count(tplg,
David Brazdil0f672f62019-12-10 10:32:29 +00002395 size, count,
2396 le32_to_cpu(hdr->payload_size),
2397 "physical link config")) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002398 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2399 count);
2400 return -EINVAL;
2401 }
2402
2403 /* config physical DAI links */
2404 for (i = 0; i < count; i++) {
2405 link = (struct snd_soc_tplg_link_config *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002406 size = le32_to_cpu(link->size);
2407 if (size == sizeof(*link)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002408 abi_match = true;
2409 _link = link;
2410 } else {
2411 abi_match = false;
2412 ret = link_new_ver(tplg, link, &_link);
2413 if (ret < 0)
2414 return ret;
2415 }
2416
2417 ret = soc_tplg_link_config(tplg, _link);
Olivier Deprez0e641232021-09-23 10:07:05 +02002418 if (ret < 0) {
2419 if (!abi_match)
2420 kfree(_link);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002421 return ret;
Olivier Deprez0e641232021-09-23 10:07:05 +02002422 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002423
2424 /* offset by version-specific struct size and
2425 * real priv data size
2426 */
David Brazdil0f672f62019-12-10 10:32:29 +00002427 tplg->pos += size + le32_to_cpu(_link->priv.size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002428
2429 if (!abi_match)
2430 kfree(_link); /* free the duplicated one */
2431 }
2432
2433 return 0;
2434}
2435
2436/**
2437 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2438 * @tplg: topology context
2439 * @d: physical DAI configs.
2440 *
2441 * The physical dai should already be registered by the platform driver.
2442 * The platform driver should specify the DAI name and ID for matching.
2443 */
2444static int soc_tplg_dai_config(struct soc_tplg *tplg,
2445 struct snd_soc_tplg_dai *d)
2446{
David Brazdil0f672f62019-12-10 10:32:29 +00002447 struct snd_soc_dai_link_component dai_component;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002448 struct snd_soc_dai *dai;
2449 struct snd_soc_dai_driver *dai_drv;
2450 struct snd_soc_pcm_stream *stream;
2451 struct snd_soc_tplg_stream_caps *caps;
2452 int ret;
2453
David Brazdil0f672f62019-12-10 10:32:29 +00002454 memset(&dai_component, 0, sizeof(dai_component));
2455
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002456 dai_component.dai_name = d->dai_name;
2457 dai = snd_soc_find_dai(&dai_component);
2458 if (!dai) {
2459 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2460 d->dai_name);
2461 return -EINVAL;
2462 }
2463
David Brazdil0f672f62019-12-10 10:32:29 +00002464 if (le32_to_cpu(d->dai_id) != dai->id) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002465 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2466 d->dai_name);
2467 return -EINVAL;
2468 }
2469
2470 dai_drv = dai->driver;
2471 if (!dai_drv)
2472 return -EINVAL;
2473
2474 if (d->playback) {
2475 stream = &dai_drv->playback;
2476 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
Olivier Deprez157378f2022-04-04 15:47:50 +02002477 ret = set_stream_info(stream, caps);
2478 if (ret < 0)
2479 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002480 }
2481
2482 if (d->capture) {
2483 stream = &dai_drv->capture;
2484 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
Olivier Deprez157378f2022-04-04 15:47:50 +02002485 ret = set_stream_info(stream, caps);
2486 if (ret < 0)
2487 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002488 }
2489
2490 if (d->flag_mask)
David Brazdil0f672f62019-12-10 10:32:29 +00002491 set_dai_flags(dai_drv,
2492 le32_to_cpu(d->flag_mask),
2493 le32_to_cpu(d->flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002494
2495 /* pass control to component driver for optional further init */
2496 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2497 if (ret < 0) {
2498 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
Olivier Deprez157378f2022-04-04 15:47:50 +02002499 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002500 }
2501
2502 return 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002503
2504err:
2505 kfree(dai_drv->playback.stream_name);
2506 kfree(dai_drv->capture.stream_name);
2507 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002508}
2509
2510/* load physical DAI elements */
2511static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2512 struct snd_soc_tplg_hdr *hdr)
2513{
2514 struct snd_soc_tplg_dai *dai;
David Brazdil0f672f62019-12-10 10:32:29 +00002515 int count;
Olivier Deprez0e641232021-09-23 10:07:05 +02002516 int i, ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002517
David Brazdil0f672f62019-12-10 10:32:29 +00002518 count = le32_to_cpu(hdr->count);
2519
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002520 /* config the existing BE DAIs */
2521 for (i = 0; i < count; i++) {
2522 dai = (struct snd_soc_tplg_dai *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002523 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002524 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2525 return -EINVAL;
2526 }
2527
Olivier Deprez0e641232021-09-23 10:07:05 +02002528 ret = soc_tplg_dai_config(tplg, dai);
2529 if (ret < 0) {
2530 dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2531 return ret;
2532 }
2533
David Brazdil0f672f62019-12-10 10:32:29 +00002534 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002535 }
2536
2537 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2538 return 0;
2539}
2540
2541/**
2542 * manifest_new_ver - Create a new version of manifest from the old version
2543 * of source.
2544 * @tplg: topology context
2545 * @src: old version of manifest as a source
2546 * @manifest: latest version of manifest created from the source
2547 *
2548 * Support from vesion 4. Users need free the returned manifest manually.
2549 */
2550static int manifest_new_ver(struct soc_tplg *tplg,
2551 struct snd_soc_tplg_manifest *src,
2552 struct snd_soc_tplg_manifest **manifest)
2553{
2554 struct snd_soc_tplg_manifest *dest;
2555 struct snd_soc_tplg_manifest_v4 *src_v4;
David Brazdil0f672f62019-12-10 10:32:29 +00002556 int size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002557
2558 *manifest = NULL;
2559
David Brazdil0f672f62019-12-10 10:32:29 +00002560 size = le32_to_cpu(src->size);
2561 if (size != sizeof(*src_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002562 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002563 size);
2564 if (size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002565 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00002566 src->size = cpu_to_le32(sizeof(*src_v4));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002567 }
2568
2569 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2570
2571 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
David Brazdil0f672f62019-12-10 10:32:29 +00002572 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2573 GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002574 if (!dest)
2575 return -ENOMEM;
2576
David Brazdil0f672f62019-12-10 10:32:29 +00002577 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002578 dest->control_elems = src_v4->control_elems;
2579 dest->widget_elems = src_v4->widget_elems;
2580 dest->graph_elems = src_v4->graph_elems;
2581 dest->pcm_elems = src_v4->pcm_elems;
2582 dest->dai_link_elems = src_v4->dai_link_elems;
2583 dest->priv.size = src_v4->priv.size;
2584 if (dest->priv.size)
2585 memcpy(dest->priv.data, src_v4->priv.data,
David Brazdil0f672f62019-12-10 10:32:29 +00002586 le32_to_cpu(src_v4->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002587
2588 *manifest = dest;
2589 return 0;
2590}
2591
2592static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2593 struct snd_soc_tplg_hdr *hdr)
2594{
2595 struct snd_soc_tplg_manifest *manifest, *_manifest;
2596 bool abi_match;
Olivier Deprez0e641232021-09-23 10:07:05 +02002597 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002598
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002599 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2600
2601 /* check ABI version by size, create a new manifest if abi not match */
David Brazdil0f672f62019-12-10 10:32:29 +00002602 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002603 abi_match = true;
2604 _manifest = manifest;
2605 } else {
2606 abi_match = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02002607 ret = manifest_new_ver(tplg, manifest, &_manifest);
2608 if (ret < 0)
2609 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002610 }
2611
2612 /* pass control to component driver for optional further init */
Olivier Deprez157378f2022-04-04 15:47:50 +02002613 if (tplg->ops && tplg->ops->manifest)
Olivier Deprez0e641232021-09-23 10:07:05 +02002614 ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002615
2616 if (!abi_match) /* free the duplicated one */
2617 kfree(_manifest);
2618
Olivier Deprez0e641232021-09-23 10:07:05 +02002619 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002620}
2621
2622/* validate header magic, size and type */
2623static int soc_valid_header(struct soc_tplg *tplg,
2624 struct snd_soc_tplg_hdr *hdr)
2625{
2626 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2627 return 0;
2628
David Brazdil0f672f62019-12-10 10:32:29 +00002629 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002630 dev_err(tplg->dev,
2631 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002632 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002633 tplg->fw->size);
2634 return -EINVAL;
2635 }
2636
2637 /* big endian firmware objects not supported atm */
Olivier Deprez0e641232021-09-23 10:07:05 +02002638 if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002639 dev_err(tplg->dev,
2640 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2641 tplg->pass, hdr->magic,
2642 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2643 return -EINVAL;
2644 }
2645
David Brazdil0f672f62019-12-10 10:32:29 +00002646 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002647 dev_err(tplg->dev,
2648 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2649 tplg->pass, hdr->magic,
2650 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2651 return -EINVAL;
2652 }
2653
2654 /* Support ABI from version 4 */
David Brazdil0f672f62019-12-10 10:32:29 +00002655 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2656 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002657 dev_err(tplg->dev,
2658 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2659 tplg->pass, hdr->abi,
2660 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2661 tplg->fw->size);
2662 return -EINVAL;
2663 }
2664
2665 if (hdr->payload_size == 0) {
2666 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2667 soc_tplg_get_hdr_offset(tplg));
2668 return -EINVAL;
2669 }
2670
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002671 return 1;
2672}
2673
2674/* check header type and call appropriate handler */
2675static int soc_tplg_load_header(struct soc_tplg *tplg,
2676 struct snd_soc_tplg_hdr *hdr)
2677{
Olivier Deprez157378f2022-04-04 15:47:50 +02002678 int (*elem_load)(struct soc_tplg *tplg,
2679 struct snd_soc_tplg_hdr *hdr);
2680 unsigned int hdr_pass;
2681
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002682 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2683
2684 /* check for matching ID */
David Brazdil0f672f62019-12-10 10:32:29 +00002685 if (le32_to_cpu(hdr->index) != tplg->req_index &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002686 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
2687 return 0;
2688
David Brazdil0f672f62019-12-10 10:32:29 +00002689 tplg->index = le32_to_cpu(hdr->index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002690
David Brazdil0f672f62019-12-10 10:32:29 +00002691 switch (le32_to_cpu(hdr->type)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002692 case SND_SOC_TPLG_TYPE_MIXER:
2693 case SND_SOC_TPLG_TYPE_ENUM:
2694 case SND_SOC_TPLG_TYPE_BYTES:
Olivier Deprez157378f2022-04-04 15:47:50 +02002695 hdr_pass = SOC_TPLG_PASS_MIXER;
2696 elem_load = soc_tplg_kcontrol_elems_load;
2697 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002698 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
Olivier Deprez157378f2022-04-04 15:47:50 +02002699 hdr_pass = SOC_TPLG_PASS_GRAPH;
2700 elem_load = soc_tplg_dapm_graph_elems_load;
2701 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002702 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
Olivier Deprez157378f2022-04-04 15:47:50 +02002703 hdr_pass = SOC_TPLG_PASS_WIDGET;
2704 elem_load = soc_tplg_dapm_widget_elems_load;
2705 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002706 case SND_SOC_TPLG_TYPE_PCM:
Olivier Deprez157378f2022-04-04 15:47:50 +02002707 hdr_pass = SOC_TPLG_PASS_PCM_DAI;
2708 elem_load = soc_tplg_pcm_elems_load;
2709 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002710 case SND_SOC_TPLG_TYPE_DAI:
Olivier Deprez157378f2022-04-04 15:47:50 +02002711 hdr_pass = SOC_TPLG_PASS_BE_DAI;
2712 elem_load = soc_tplg_dai_elems_load;
2713 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002714 case SND_SOC_TPLG_TYPE_DAI_LINK:
2715 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2716 /* physical link configurations */
Olivier Deprez157378f2022-04-04 15:47:50 +02002717 hdr_pass = SOC_TPLG_PASS_LINK;
2718 elem_load = soc_tplg_link_elems_load;
2719 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002720 case SND_SOC_TPLG_TYPE_MANIFEST:
Olivier Deprez157378f2022-04-04 15:47:50 +02002721 hdr_pass = SOC_TPLG_PASS_MANIFEST;
2722 elem_load = soc_tplg_manifest_load;
2723 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002724 default:
2725 /* bespoke vendor data object */
Olivier Deprez157378f2022-04-04 15:47:50 +02002726 hdr_pass = SOC_TPLG_PASS_VENDOR;
2727 elem_load = soc_tplg_vendor_load;
2728 break;
2729 }
2730
2731 if (tplg->pass == hdr_pass) {
2732 dev_dbg(tplg->dev,
2733 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2734 hdr->payload_size, hdr->type, hdr->version,
2735 hdr->vendor_type, tplg->pass);
2736 return elem_load(tplg, hdr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002737 }
2738
2739 return 0;
2740}
2741
2742/* process the topology file headers */
2743static int soc_tplg_process_headers(struct soc_tplg *tplg)
2744{
2745 struct snd_soc_tplg_hdr *hdr;
2746 int ret;
2747
2748 tplg->pass = SOC_TPLG_PASS_START;
2749
2750 /* process the header types from start to end */
2751 while (tplg->pass <= SOC_TPLG_PASS_END) {
2752
2753 tplg->hdr_pos = tplg->fw->data;
2754 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2755
2756 while (!soc_tplg_is_eof(tplg)) {
2757
2758 /* make sure header is valid before loading */
2759 ret = soc_valid_header(tplg, hdr);
Olivier Deprez157378f2022-04-04 15:47:50 +02002760 if (ret < 0) {
2761 dev_err(tplg->dev,
2762 "ASoC: topology: invalid header: %d\n", ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002763 return ret;
Olivier Deprez157378f2022-04-04 15:47:50 +02002764 } else if (ret == 0) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002765 break;
Olivier Deprez157378f2022-04-04 15:47:50 +02002766 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002767
2768 /* load the header object */
2769 ret = soc_tplg_load_header(tplg, hdr);
Olivier Deprez157378f2022-04-04 15:47:50 +02002770 if (ret < 0) {
2771 dev_err(tplg->dev,
2772 "ASoC: topology: could not load header: %d\n", ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002773 return ret;
Olivier Deprez157378f2022-04-04 15:47:50 +02002774 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002775
2776 /* goto next header */
David Brazdil0f672f62019-12-10 10:32:29 +00002777 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002778 sizeof(struct snd_soc_tplg_hdr);
2779 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2780 }
2781
2782 /* next data type pass */
2783 tplg->pass++;
2784 }
2785
2786 /* signal DAPM we are complete */
2787 ret = soc_tplg_dapm_complete(tplg);
2788 if (ret < 0)
2789 dev_err(tplg->dev,
2790 "ASoC: failed to initialise DAPM from Firmware\n");
2791
2792 return ret;
2793}
2794
2795static int soc_tplg_load(struct soc_tplg *tplg)
2796{
2797 int ret;
2798
2799 ret = soc_tplg_process_headers(tplg);
2800 if (ret == 0)
2801 soc_tplg_complete(tplg);
2802
2803 return ret;
2804}
2805
2806/* load audio component topology from "firmware" file */
2807int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2808 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2809{
2810 struct soc_tplg tplg;
David Brazdil0f672f62019-12-10 10:32:29 +00002811 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002812
Olivier Deprez157378f2022-04-04 15:47:50 +02002813 /* component needs to exist to keep and reference data while parsing */
2814 if (!comp)
2815 return -EINVAL;
2816
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002817 /* setup parsing context */
2818 memset(&tplg, 0, sizeof(tplg));
2819 tplg.fw = fw;
2820 tplg.dev = comp->dev;
2821 tplg.comp = comp;
2822 tplg.ops = ops;
2823 tplg.req_index = id;
2824 tplg.io_ops = ops->io_ops;
2825 tplg.io_ops_count = ops->io_ops_count;
2826 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2827 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2828
David Brazdil0f672f62019-12-10 10:32:29 +00002829 ret = soc_tplg_load(&tplg);
2830 /* free the created components if fail to load topology */
2831 if (ret)
2832 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2833
2834 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002835}
2836EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2837
2838/* remove this dynamic widget */
2839void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2840{
2841 /* make sure we are a widget */
2842 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2843 return;
2844
2845 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2846}
2847EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2848
2849/* remove all dynamic widgets from this DAPM context */
2850void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2851 u32 index)
2852{
2853 struct snd_soc_dapm_widget *w, *next_w;
2854
Olivier Deprez157378f2022-04-04 15:47:50 +02002855 for_each_card_widgets_safe(dapm->card, w, next_w) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002856
2857 /* make sure we are a widget with correct context */
2858 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2859 continue;
2860
2861 /* match ID */
2862 if (w->dobj.index != index &&
2863 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2864 continue;
2865 /* check and free and dynamic widget kcontrols */
2866 snd_soc_tplg_widget_remove(w);
2867 snd_soc_dapm_free_widget(w);
2868 }
2869 snd_soc_dapm_reset_cache(dapm);
2870}
2871EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2872
2873/* remove dynamic controls from the component driver */
2874int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2875{
Olivier Deprez157378f2022-04-04 15:47:50 +02002876 struct snd_card *card = comp->card->snd_card;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002877 struct snd_soc_dobj *dobj, *next_dobj;
2878 int pass = SOC_TPLG_PASS_END;
2879
2880 /* process the header types from end to start */
2881 while (pass >= SOC_TPLG_PASS_START) {
2882
2883 /* remove mixer controls */
Olivier Deprez157378f2022-04-04 15:47:50 +02002884 down_write(&card->controls_rwsem);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002885 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2886 list) {
2887
2888 /* match index */
2889 if (dobj->index != index &&
2890 index != SND_SOC_TPLG_INDEX_ALL)
2891 continue;
2892
2893 switch (dobj->type) {
2894 case SND_SOC_DOBJ_MIXER:
2895 remove_mixer(comp, dobj, pass);
2896 break;
2897 case SND_SOC_DOBJ_ENUM:
2898 remove_enum(comp, dobj, pass);
2899 break;
2900 case SND_SOC_DOBJ_BYTES:
2901 remove_bytes(comp, dobj, pass);
2902 break;
David Brazdil0f672f62019-12-10 10:32:29 +00002903 case SND_SOC_DOBJ_GRAPH:
2904 remove_route(comp, dobj, pass);
2905 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002906 case SND_SOC_DOBJ_WIDGET:
2907 remove_widget(comp, dobj, pass);
2908 break;
2909 case SND_SOC_DOBJ_PCM:
2910 remove_dai(comp, dobj, pass);
2911 break;
2912 case SND_SOC_DOBJ_DAI_LINK:
2913 remove_link(comp, dobj, pass);
2914 break;
David Brazdil0f672f62019-12-10 10:32:29 +00002915 case SND_SOC_DOBJ_BACKEND_LINK:
2916 /*
2917 * call link_unload ops if extra
2918 * deinitialization is needed.
2919 */
2920 remove_backend_link(comp, dobj, pass);
2921 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002922 default:
2923 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2924 dobj->type);
2925 break;
2926 }
2927 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002928 up_write(&card->controls_rwsem);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002929 pass--;
2930 }
2931
2932 /* let caller know if FW can be freed when no objects are left */
2933 return !list_empty(&comp->dobj_list);
2934}
2935EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);