blob: c367609433bfc905acb2e155dc6b2c7c801db86d [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 */
249static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
250 struct snd_soc_tplg_hdr *hdr)
251{
252 int ret = 0;
253
254 if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
255 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
271/* pass vendor data to component driver for processing */
272static int soc_tplg_vendor_load(struct soc_tplg *tplg,
273 struct snd_soc_tplg_hdr *hdr)
274{
275 if (tplg->pass != SOC_TPLG_PASS_VENDOR)
276 return 0;
277
278 return soc_tplg_vendor_load_(tplg, hdr);
279}
280
281/* optionally pass new dynamic widget to component driver. This is mainly for
282 * external widgets where we can assign private data/ops */
283static int soc_tplg_widget_load(struct soc_tplg *tplg,
284 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
285{
286 if (tplg->comp && tplg->ops && tplg->ops->widget_load)
287 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
288 tplg_w);
289
290 return 0;
291}
292
293/* optionally pass new dynamic widget to component driver. This is mainly for
294 * external widgets where we can assign private data/ops */
295static int soc_tplg_widget_ready(struct soc_tplg *tplg,
296 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
297{
298 if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
299 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
300 tplg_w);
301
302 return 0;
303}
304
305/* pass DAI configurations to component driver for extra initialization */
306static int soc_tplg_dai_load(struct soc_tplg *tplg,
307 struct snd_soc_dai_driver *dai_drv,
308 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
309{
310 if (tplg->comp && tplg->ops && tplg->ops->dai_load)
311 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
312 pcm, dai);
313
314 return 0;
315}
316
317/* pass link configurations to component driver for extra initialization */
318static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
319 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
320{
321 if (tplg->comp && tplg->ops && tplg->ops->link_load)
322 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
323
324 return 0;
325}
326
327/* tell the component driver that all firmware has been loaded in this request */
328static void soc_tplg_complete(struct soc_tplg *tplg)
329{
330 if (tplg->comp && tplg->ops && tplg->ops->complete)
331 tplg->ops->complete(tplg->comp);
332}
333
334/* add a dynamic kcontrol */
335static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
336 const struct snd_kcontrol_new *control_new, const char *prefix,
337 void *data, struct snd_kcontrol **kcontrol)
338{
339 int err;
340
341 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
342 if (*kcontrol == NULL) {
343 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
344 control_new->name);
345 return -ENOMEM;
346 }
347
348 err = snd_ctl_add(card, *kcontrol);
349 if (err < 0) {
350 dev_err(dev, "ASoC: Failed to add %s: %d\n",
351 control_new->name, err);
352 return err;
353 }
354
355 return 0;
356}
357
358/* add a dynamic kcontrol for component driver */
359static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
360 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
361{
362 struct snd_soc_component *comp = tplg->comp;
363
364 return soc_tplg_add_dcontrol(comp->card->snd_card,
Olivier Deprez0e641232021-09-23 10:07:05 +0200365 comp->dev, k, comp->name_prefix, comp, kcontrol);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000366}
367
368/* remove a mixer kcontrol */
369static void remove_mixer(struct snd_soc_component *comp,
370 struct snd_soc_dobj *dobj, int pass)
371{
372 struct snd_card *card = comp->card->snd_card;
373 struct soc_mixer_control *sm =
374 container_of(dobj, struct soc_mixer_control, dobj);
375 const unsigned int *p = NULL;
376
377 if (pass != SOC_TPLG_PASS_MIXER)
378 return;
379
380 if (dobj->ops && dobj->ops->control_unload)
381 dobj->ops->control_unload(comp, dobj);
382
David Brazdil0f672f62019-12-10 10:32:29 +0000383 if (dobj->control.kcontrol->tlv.p)
384 p = dobj->control.kcontrol->tlv.p;
385 snd_ctl_remove(card, dobj->control.kcontrol);
386 list_del(&dobj->list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000387 kfree(sm);
388 kfree(p);
389}
390
391/* remove an enum kcontrol */
392static void remove_enum(struct snd_soc_component *comp,
393 struct snd_soc_dobj *dobj, int pass)
394{
395 struct snd_card *card = comp->card->snd_card;
396 struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000397
398 if (pass != SOC_TPLG_PASS_MIXER)
399 return;
400
401 if (dobj->ops && dobj->ops->control_unload)
402 dobj->ops->control_unload(comp, dobj);
403
David Brazdil0f672f62019-12-10 10:32:29 +0000404 snd_ctl_remove(card, dobj->control.kcontrol);
405 list_del(&dobj->list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000406
David Brazdil0f672f62019-12-10 10:32:29 +0000407 soc_tplg_denum_remove_values(se);
408 soc_tplg_denum_remove_texts(se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000409 kfree(se);
410}
411
412/* remove a byte kcontrol */
413static void remove_bytes(struct snd_soc_component *comp,
414 struct snd_soc_dobj *dobj, int pass)
415{
416 struct snd_card *card = comp->card->snd_card;
417 struct soc_bytes_ext *sb =
418 container_of(dobj, struct soc_bytes_ext, dobj);
419
420 if (pass != SOC_TPLG_PASS_MIXER)
421 return;
422
423 if (dobj->ops && dobj->ops->control_unload)
424 dobj->ops->control_unload(comp, dobj);
425
David Brazdil0f672f62019-12-10 10:32:29 +0000426 snd_ctl_remove(card, dobj->control.kcontrol);
427 list_del(&dobj->list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000428 kfree(sb);
429}
430
David Brazdil0f672f62019-12-10 10:32:29 +0000431/* remove a route */
432static void remove_route(struct snd_soc_component *comp,
433 struct snd_soc_dobj *dobj, int pass)
434{
435 struct snd_soc_dapm_route *route =
436 container_of(dobj, struct snd_soc_dapm_route, dobj);
437
438 if (pass != SOC_TPLG_PASS_GRAPH)
439 return;
440
441 if (dobj->ops && dobj->ops->dapm_route_unload)
442 dobj->ops->dapm_route_unload(comp, dobj);
443
444 list_del(&dobj->list);
445 kfree(route);
446}
447
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000448/* remove a widget and it's kcontrols - routes must be removed first */
449static void remove_widget(struct snd_soc_component *comp,
450 struct snd_soc_dobj *dobj, int pass)
451{
452 struct snd_card *card = comp->card->snd_card;
453 struct snd_soc_dapm_widget *w =
454 container_of(dobj, struct snd_soc_dapm_widget, dobj);
455 int i;
456
457 if (pass != SOC_TPLG_PASS_WIDGET)
458 return;
459
460 if (dobj->ops && dobj->ops->widget_unload)
461 dobj->ops->widget_unload(comp, dobj);
462
463 if (!w->kcontrols)
464 goto free_news;
465
466 /*
467 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
468 * The enum may either have an array of values or strings.
469 */
470 if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
471 /* enumerated widget mixer */
472 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
473 struct snd_kcontrol *kcontrol = w->kcontrols[i];
474 struct soc_enum *se =
475 (struct soc_enum *)kcontrol->private_value;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000476
477 snd_ctl_remove(card, kcontrol);
478
David Brazdil0f672f62019-12-10 10:32:29 +0000479 /* free enum kcontrol's dvalues and dtexts */
480 soc_tplg_denum_remove_values(se);
481 soc_tplg_denum_remove_texts(se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482
483 kfree(se);
484 kfree(w->kcontrol_news[i].name);
485 }
486 } else {
487 /* volume mixer or bytes controls */
488 for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
489 struct snd_kcontrol *kcontrol = w->kcontrols[i];
490
491 if (dobj->widget.kcontrol_type
492 == SND_SOC_TPLG_TYPE_MIXER)
493 kfree(kcontrol->tlv.p);
494
495 /* Private value is used as struct soc_mixer_control
496 * for volume mixers or soc_bytes_ext for bytes
497 * controls.
498 */
499 kfree((void *)kcontrol->private_value);
500 snd_ctl_remove(card, kcontrol);
501 kfree(w->kcontrol_news[i].name);
502 }
503 }
504
505free_news:
506 kfree(w->kcontrol_news);
507
David Brazdil0f672f62019-12-10 10:32:29 +0000508 list_del(&dobj->list);
509
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510 /* widget w is freed by soc-dapm.c */
511}
512
513/* remove DAI configurations */
514static void remove_dai(struct snd_soc_component *comp,
515 struct snd_soc_dobj *dobj, int pass)
516{
517 struct snd_soc_dai_driver *dai_drv =
518 container_of(dobj, struct snd_soc_dai_driver, dobj);
David Brazdil0f672f62019-12-10 10:32:29 +0000519 struct snd_soc_dai *dai;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520
521 if (pass != SOC_TPLG_PASS_PCM_DAI)
522 return;
523
524 if (dobj->ops && dobj->ops->dai_unload)
525 dobj->ops->dai_unload(comp, dobj);
526
David Brazdil0f672f62019-12-10 10:32:29 +0000527 for_each_component_dais(comp, dai)
528 if (dai->driver == dai_drv)
529 dai->driver = NULL;
530
531 kfree(dai_drv->playback.stream_name);
532 kfree(dai_drv->capture.stream_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533 kfree(dai_drv->name);
534 list_del(&dobj->list);
535 kfree(dai_drv);
536}
537
538/* remove link configurations */
539static void remove_link(struct snd_soc_component *comp,
540 struct snd_soc_dobj *dobj, int pass)
541{
542 struct snd_soc_dai_link *link =
543 container_of(dobj, struct snd_soc_dai_link, dobj);
544
545 if (pass != SOC_TPLG_PASS_PCM_DAI)
546 return;
547
548 if (dobj->ops && dobj->ops->link_unload)
549 dobj->ops->link_unload(comp, dobj);
550
Olivier Deprez0e641232021-09-23 10:07:05 +0200551 list_del(&dobj->list);
552 snd_soc_remove_dai_link(comp->card, link);
553
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000554 kfree(link->name);
555 kfree(link->stream_name);
David Brazdil0f672f62019-12-10 10:32:29 +0000556 kfree(link->cpus->dai_name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000557 kfree(link);
558}
559
David Brazdil0f672f62019-12-10 10:32:29 +0000560/* unload dai link */
561static void remove_backend_link(struct snd_soc_component *comp,
562 struct snd_soc_dobj *dobj, int pass)
563{
564 if (pass != SOC_TPLG_PASS_LINK)
565 return;
566
567 if (dobj->ops && dobj->ops->link_unload)
568 dobj->ops->link_unload(comp, dobj);
569
570 /*
571 * We don't free the link here as what remove_link() do since BE
572 * links are not allocated by topology.
573 * We however need to reset the dobj type to its initial values
574 */
575 dobj->type = SND_SOC_DOBJ_NONE;
576 list_del(&dobj->list);
577}
578
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579/* bind a kcontrol to it's IO handlers */
580static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
581 struct snd_kcontrol_new *k,
582 const struct soc_tplg *tplg)
583{
584 const struct snd_soc_tplg_kcontrol_ops *ops;
585 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
586 int num_ops, i;
587
David Brazdil0f672f62019-12-10 10:32:29 +0000588 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
590 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
591 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
592 struct soc_bytes_ext *sbe;
593 struct snd_soc_tplg_bytes_control *be;
594
595 sbe = (struct soc_bytes_ext *)k->private_value;
596 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
597
598 /* TLV bytes controls need standard kcontrol info handler,
599 * TLV callback and extended put/get handlers.
600 */
601 k->info = snd_soc_bytes_info_ext;
602 k->tlv.c = snd_soc_bytes_tlv_callback;
603
604 ext_ops = tplg->bytes_ext_ops;
605 num_ops = tplg->bytes_ext_ops_count;
606 for (i = 0; i < num_ops; i++) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200607 if (!sbe->put &&
608 ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000609 sbe->put = ext_ops[i].put;
Olivier Deprez0e641232021-09-23 10:07:05 +0200610 if (!sbe->get &&
611 ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000612 sbe->get = ext_ops[i].get;
613 }
614
615 if (sbe->put && sbe->get)
616 return 0;
617 else
618 return -EINVAL;
619 }
620
621 /* try and map vendor specific kcontrol handlers first */
622 ops = tplg->io_ops;
623 num_ops = tplg->io_ops_count;
624 for (i = 0; i < num_ops; i++) {
625
Olivier Deprez0e641232021-09-23 10:07:05 +0200626 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000627 k->put = ops[i].put;
Olivier Deprez0e641232021-09-23 10:07:05 +0200628 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000629 k->get = ops[i].get;
Olivier Deprez0e641232021-09-23 10:07:05 +0200630 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000631 k->info = ops[i].info;
632 }
633
634 /* vendor specific handlers found ? */
635 if (k->put && k->get && k->info)
636 return 0;
637
638 /* none found so try standard kcontrol handlers */
639 ops = io_ops;
640 num_ops = ARRAY_SIZE(io_ops);
641 for (i = 0; i < num_ops; i++) {
642
Olivier Deprez0e641232021-09-23 10:07:05 +0200643 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000644 k->put = ops[i].put;
Olivier Deprez0e641232021-09-23 10:07:05 +0200645 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000646 k->get = ops[i].get;
Olivier Deprez0e641232021-09-23 10:07:05 +0200647 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000648 k->info = ops[i].info;
649 }
650
651 /* standard handlers found ? */
652 if (k->put && k->get && k->info)
653 return 0;
654
655 /* nothing to bind */
656 return -EINVAL;
657}
658
659/* bind a widgets to it's evnt handlers */
660int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
661 const struct snd_soc_tplg_widget_events *events,
662 int num_events, u16 event_type)
663{
664 int i;
665
666 w->event = NULL;
667
668 for (i = 0; i < num_events; i++) {
669 if (event_type == events[i].type) {
670
671 /* found - so assign event */
672 w->event = events[i].event_handler;
673 return 0;
674 }
675 }
676
677 /* not found */
678 return -EINVAL;
679}
680EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
681
682/* optionally pass new dynamic kcontrol to component driver. */
683static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
684 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
685{
686 if (tplg->comp && tplg->ops && tplg->ops->control_load)
687 return tplg->ops->control_load(tplg->comp, tplg->index, k,
688 hdr);
689
690 return 0;
691}
692
693
694static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
695 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
696{
697 unsigned int item_len = 2 * sizeof(unsigned int);
698 unsigned int *p;
699
700 p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
701 if (!p)
702 return -ENOMEM;
703
704 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
705 p[1] = item_len;
David Brazdil0f672f62019-12-10 10:32:29 +0000706 p[2] = le32_to_cpu(scale->min);
707 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
708 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000709
710 kc->tlv.p = (void *)p;
711 return 0;
712}
713
714static int soc_tplg_create_tlv(struct soc_tplg *tplg,
715 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
716{
717 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
David Brazdil0f672f62019-12-10 10:32:29 +0000718 u32 access = le32_to_cpu(tc->access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000719
David Brazdil0f672f62019-12-10 10:32:29 +0000720 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721 return 0;
722
David Brazdil0f672f62019-12-10 10:32:29 +0000723 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724 tplg_tlv = &tc->tlv;
David Brazdil0f672f62019-12-10 10:32:29 +0000725 switch (le32_to_cpu(tplg_tlv->type)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000726 case SNDRV_CTL_TLVT_DB_SCALE:
727 return soc_tplg_create_tlv_db_scale(tplg, kc,
728 &tplg_tlv->scale);
729
730 /* TODO: add support for other TLV types */
731 default:
732 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
733 tplg_tlv->type);
734 return -EINVAL;
735 }
736 }
737
738 return 0;
739}
740
741static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
742 struct snd_kcontrol_new *kc)
743{
744 kfree(kc->tlv.p);
745}
746
747static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
748 size_t size)
749{
750 struct snd_soc_tplg_bytes_control *be;
751 struct soc_bytes_ext *sbe;
752 struct snd_kcontrol_new kc;
753 int i, err;
754
755 if (soc_tplg_check_elem_count(tplg,
756 sizeof(struct snd_soc_tplg_bytes_control), count,
757 size, "mixer bytes")) {
758 dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
759 count);
760 return -EINVAL;
761 }
762
763 for (i = 0; i < count; i++) {
764 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
765
766 /* validate kcontrol */
767 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
768 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
769 return -EINVAL;
770
771 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
772 if (sbe == NULL)
773 return -ENOMEM;
774
775 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
David Brazdil0f672f62019-12-10 10:32:29 +0000776 le32_to_cpu(be->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000777
778 dev_dbg(tplg->dev,
779 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
780 be->hdr.name, be->hdr.access);
781
782 memset(&kc, 0, sizeof(kc));
783 kc.name = be->hdr.name;
784 kc.private_value = (long)sbe;
785 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
David Brazdil0f672f62019-12-10 10:32:29 +0000786 kc.access = le32_to_cpu(be->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000787
David Brazdil0f672f62019-12-10 10:32:29 +0000788 sbe->max = le32_to_cpu(be->max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
790 sbe->dobj.ops = tplg->ops;
791 INIT_LIST_HEAD(&sbe->dobj.list);
792
793 /* map io handlers */
794 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
795 if (err) {
796 soc_control_err(tplg, &be->hdr, be->hdr.name);
797 kfree(sbe);
798 continue;
799 }
800
801 /* pass control to driver for optional further init */
802 err = soc_tplg_init_kcontrol(tplg, &kc,
803 (struct snd_soc_tplg_ctl_hdr *)be);
804 if (err < 0) {
805 dev_err(tplg->dev, "ASoC: failed to init %s\n",
806 be->hdr.name);
807 kfree(sbe);
808 continue;
809 }
810
811 /* register control here */
812 err = soc_tplg_add_kcontrol(tplg, &kc,
813 &sbe->dobj.control.kcontrol);
814 if (err < 0) {
815 dev_err(tplg->dev, "ASoC: failed to add %s\n",
816 be->hdr.name);
817 kfree(sbe);
818 continue;
819 }
820
821 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
822 }
823 return 0;
824
825}
826
827static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
828 size_t size)
829{
830 struct snd_soc_tplg_mixer_control *mc;
831 struct soc_mixer_control *sm;
832 struct snd_kcontrol_new kc;
833 int i, err;
834
835 if (soc_tplg_check_elem_count(tplg,
836 sizeof(struct snd_soc_tplg_mixer_control),
837 count, size, "mixers")) {
838
839 dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
840 count);
841 return -EINVAL;
842 }
843
844 for (i = 0; i < count; i++) {
845 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
846
847 /* validate kcontrol */
848 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
849 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
850 return -EINVAL;
851
852 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
853 if (sm == NULL)
854 return -ENOMEM;
855 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
David Brazdil0f672f62019-12-10 10:32:29 +0000856 le32_to_cpu(mc->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000857
858 dev_dbg(tplg->dev,
859 "ASoC: adding mixer kcontrol %s with access 0x%x\n",
860 mc->hdr.name, mc->hdr.access);
861
862 memset(&kc, 0, sizeof(kc));
863 kc.name = mc->hdr.name;
864 kc.private_value = (long)sm;
865 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
David Brazdil0f672f62019-12-10 10:32:29 +0000866 kc.access = le32_to_cpu(mc->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000867
868 /* we only support FL/FR channel mapping atm */
869 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
870 SNDRV_CHMAP_FL);
871 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
872 SNDRV_CHMAP_FR);
873 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
874 SNDRV_CHMAP_FL);
875 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
876 SNDRV_CHMAP_FR);
877
David Brazdil0f672f62019-12-10 10:32:29 +0000878 sm->max = le32_to_cpu(mc->max);
879 sm->min = le32_to_cpu(mc->min);
880 sm->invert = le32_to_cpu(mc->invert);
881 sm->platform_max = le32_to_cpu(mc->platform_max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000882 sm->dobj.index = tplg->index;
883 sm->dobj.ops = tplg->ops;
884 sm->dobj.type = SND_SOC_DOBJ_MIXER;
885 INIT_LIST_HEAD(&sm->dobj.list);
886
887 /* map io handlers */
888 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
889 if (err) {
890 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
891 kfree(sm);
892 continue;
893 }
894
David Brazdil0f672f62019-12-10 10:32:29 +0000895 /* create any TLV data */
Olivier Deprez0e641232021-09-23 10:07:05 +0200896 err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
897 if (err < 0) {
898 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
899 mc->hdr.name);
900 kfree(sm);
901 continue;
902 }
David Brazdil0f672f62019-12-10 10:32:29 +0000903
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000904 /* pass control to driver for optional further init */
905 err = soc_tplg_init_kcontrol(tplg, &kc,
906 (struct snd_soc_tplg_ctl_hdr *) mc);
907 if (err < 0) {
908 dev_err(tplg->dev, "ASoC: failed to init %s\n",
909 mc->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +0000910 soc_tplg_free_tlv(tplg, &kc);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000911 kfree(sm);
912 continue;
913 }
914
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000915 /* register control here */
916 err = soc_tplg_add_kcontrol(tplg, &kc,
917 &sm->dobj.control.kcontrol);
918 if (err < 0) {
919 dev_err(tplg->dev, "ASoC: failed to add %s\n",
920 mc->hdr.name);
921 soc_tplg_free_tlv(tplg, &kc);
922 kfree(sm);
923 continue;
924 }
925
926 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
927 }
928
929 return 0;
930}
931
932static int soc_tplg_denum_create_texts(struct soc_enum *se,
933 struct snd_soc_tplg_enum_control *ec)
934{
935 int i, ret;
936
937 se->dobj.control.dtexts =
David Brazdil0f672f62019-12-10 10:32:29 +0000938 kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939 if (se->dobj.control.dtexts == NULL)
940 return -ENOMEM;
941
Olivier Deprez0e641232021-09-23 10:07:05 +0200942 for (i = 0; i < le32_to_cpu(ec->items); i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000943
944 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
945 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
946 ret = -EINVAL;
947 goto err;
948 }
949
950 se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
951 if (!se->dobj.control.dtexts[i]) {
952 ret = -ENOMEM;
953 goto err;
954 }
955 }
956
David Brazdil0f672f62019-12-10 10:32:29 +0000957 se->items = le32_to_cpu(ec->items);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000958 se->texts = (const char * const *)se->dobj.control.dtexts;
959 return 0;
960
961err:
David Brazdil0f672f62019-12-10 10:32:29 +0000962 se->items = i;
963 soc_tplg_denum_remove_texts(se);
964 return ret;
965}
966
967static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
968{
969 int i = se->items;
970
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000971 for (--i; i >= 0; i--)
972 kfree(se->dobj.control.dtexts[i]);
973 kfree(se->dobj.control.dtexts);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000974}
975
976static int soc_tplg_denum_create_values(struct soc_enum *se,
977 struct snd_soc_tplg_enum_control *ec)
978{
David Brazdil0f672f62019-12-10 10:32:29 +0000979 int i;
980
981 if (le32_to_cpu(ec->items) > sizeof(*ec->values))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000982 return -EINVAL;
983
David Brazdil0f672f62019-12-10 10:32:29 +0000984 se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
Olivier Deprez0e641232021-09-23 10:07:05 +0200985 sizeof(*se->dobj.control.dvalues),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000986 GFP_KERNEL);
987 if (!se->dobj.control.dvalues)
988 return -ENOMEM;
989
David Brazdil0f672f62019-12-10 10:32:29 +0000990 /* convert from little-endian */
991 for (i = 0; i < le32_to_cpu(ec->items); i++) {
992 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
993 }
994
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000995 return 0;
996}
997
David Brazdil0f672f62019-12-10 10:32:29 +0000998static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
999{
1000 kfree(se->dobj.control.dvalues);
1001}
1002
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001003static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
1004 size_t size)
1005{
1006 struct snd_soc_tplg_enum_control *ec;
1007 struct soc_enum *se;
1008 struct snd_kcontrol_new kc;
1009 int i, ret, err;
1010
1011 if (soc_tplg_check_elem_count(tplg,
1012 sizeof(struct snd_soc_tplg_enum_control),
1013 count, size, "enums")) {
1014
1015 dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
1016 count);
1017 return -EINVAL;
1018 }
1019
1020 for (i = 0; i < count; i++) {
1021 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001022
1023 /* validate kcontrol */
1024 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1025 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1026 return -EINVAL;
1027
1028 se = kzalloc((sizeof(*se)), GFP_KERNEL);
1029 if (se == NULL)
1030 return -ENOMEM;
1031
David Brazdil0f672f62019-12-10 10:32:29 +00001032 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1033 le32_to_cpu(ec->priv.size));
1034
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
1036 ec->hdr.name, ec->items);
1037
1038 memset(&kc, 0, sizeof(kc));
1039 kc.name = ec->hdr.name;
1040 kc.private_value = (long)se;
1041 kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
David Brazdil0f672f62019-12-10 10:32:29 +00001042 kc.access = le32_to_cpu(ec->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001043
1044 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1045 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1046 SNDRV_CHMAP_FL);
1047 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1048 SNDRV_CHMAP_FL);
1049
David Brazdil0f672f62019-12-10 10:32:29 +00001050 se->mask = le32_to_cpu(ec->mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001051 se->dobj.index = tplg->index;
1052 se->dobj.type = SND_SOC_DOBJ_ENUM;
1053 se->dobj.ops = tplg->ops;
1054 INIT_LIST_HEAD(&se->dobj.list);
1055
David Brazdil0f672f62019-12-10 10:32:29 +00001056 switch (le32_to_cpu(ec->hdr.ops.info)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1058 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1059 err = soc_tplg_denum_create_values(se, ec);
1060 if (err < 0) {
1061 dev_err(tplg->dev,
1062 "ASoC: could not create values for %s\n",
1063 ec->hdr.name);
1064 kfree(se);
1065 continue;
1066 }
David Brazdil0f672f62019-12-10 10:32:29 +00001067 /* fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001068 case SND_SOC_TPLG_CTL_ENUM:
1069 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1070 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1071 err = soc_tplg_denum_create_texts(se, ec);
1072 if (err < 0) {
1073 dev_err(tplg->dev,
1074 "ASoC: could not create texts for %s\n",
1075 ec->hdr.name);
1076 kfree(se);
1077 continue;
1078 }
1079 break;
1080 default:
1081 dev_err(tplg->dev,
1082 "ASoC: invalid enum control type %d for %s\n",
1083 ec->hdr.ops.info, ec->hdr.name);
1084 kfree(se);
1085 continue;
1086 }
1087
1088 /* map io handlers */
1089 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1090 if (err) {
1091 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1092 kfree(se);
1093 continue;
1094 }
1095
1096 /* pass control to driver for optional further init */
1097 err = soc_tplg_init_kcontrol(tplg, &kc,
1098 (struct snd_soc_tplg_ctl_hdr *) ec);
1099 if (err < 0) {
1100 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1101 ec->hdr.name);
1102 kfree(se);
1103 continue;
1104 }
1105
1106 /* register control here */
1107 ret = soc_tplg_add_kcontrol(tplg,
1108 &kc, &se->dobj.control.kcontrol);
1109 if (ret < 0) {
1110 dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
1111 ec->hdr.name);
1112 kfree(se);
1113 continue;
1114 }
1115
1116 list_add(&se->dobj.list, &tplg->comp->dobj_list);
1117 }
1118
1119 return 0;
1120}
1121
1122static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
1123 struct snd_soc_tplg_hdr *hdr)
1124{
1125 struct snd_soc_tplg_ctl_hdr *control_hdr;
Olivier Deprez0e641232021-09-23 10:07:05 +02001126 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001127 int i;
1128
1129 if (tplg->pass != SOC_TPLG_PASS_MIXER) {
David Brazdil0f672f62019-12-10 10:32:29 +00001130 tplg->pos += le32_to_cpu(hdr->size) +
1131 le32_to_cpu(hdr->payload_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001132 return 0;
1133 }
1134
1135 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
1136 soc_tplg_get_offset(tplg));
1137
David Brazdil0f672f62019-12-10 10:32:29 +00001138 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001139
1140 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1141
David Brazdil0f672f62019-12-10 10:32:29 +00001142 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143 dev_err(tplg->dev, "ASoC: invalid control size\n");
1144 return -EINVAL;
1145 }
1146
David Brazdil0f672f62019-12-10 10:32:29 +00001147 switch (le32_to_cpu(control_hdr->ops.info)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001148 case SND_SOC_TPLG_CTL_VOLSW:
1149 case SND_SOC_TPLG_CTL_STROBE:
1150 case SND_SOC_TPLG_CTL_VOLSW_SX:
1151 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1152 case SND_SOC_TPLG_CTL_RANGE:
1153 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1154 case SND_SOC_TPLG_DAPM_CTL_PIN:
Olivier Deprez0e641232021-09-23 10:07:05 +02001155 ret = soc_tplg_dmixer_create(tplg, 1,
1156 le32_to_cpu(hdr->payload_size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001157 break;
1158 case SND_SOC_TPLG_CTL_ENUM:
1159 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1160 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1161 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1162 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
Olivier Deprez0e641232021-09-23 10:07:05 +02001163 ret = soc_tplg_denum_create(tplg, 1,
1164 le32_to_cpu(hdr->payload_size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001165 break;
1166 case SND_SOC_TPLG_CTL_BYTES:
Olivier Deprez0e641232021-09-23 10:07:05 +02001167 ret = soc_tplg_dbytes_create(tplg, 1,
1168 le32_to_cpu(hdr->payload_size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001169 break;
1170 default:
1171 soc_bind_err(tplg, control_hdr, i);
1172 return -EINVAL;
1173 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001174 if (ret < 0) {
1175 dev_err(tplg->dev, "ASoC: invalid control\n");
1176 return ret;
1177 }
1178
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001179 }
1180
1181 return 0;
1182}
1183
1184/* optionally pass new dynamic kcontrol to component driver. */
1185static int soc_tplg_add_route(struct soc_tplg *tplg,
1186 struct snd_soc_dapm_route *route)
1187{
1188 if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
1189 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1190 route);
1191
1192 return 0;
1193}
1194
1195static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1196 struct snd_soc_tplg_hdr *hdr)
1197{
1198 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001199 struct snd_soc_tplg_dapm_graph_elem *elem;
David Brazdil0f672f62019-12-10 10:32:29 +00001200 struct snd_soc_dapm_route **routes;
1201 int count, i, j;
1202 int ret = 0;
1203
1204 count = le32_to_cpu(hdr->count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001205
1206 if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
David Brazdil0f672f62019-12-10 10:32:29 +00001207 tplg->pos +=
1208 le32_to_cpu(hdr->size) +
1209 le32_to_cpu(hdr->payload_size);
1210
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001211 return 0;
1212 }
1213
1214 if (soc_tplg_check_elem_count(tplg,
1215 sizeof(struct snd_soc_tplg_dapm_graph_elem),
David Brazdil0f672f62019-12-10 10:32:29 +00001216 count, le32_to_cpu(hdr->payload_size), "graph")) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001217
1218 dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
1219 count);
1220 return -EINVAL;
1221 }
1222
1223 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1224 hdr->index);
1225
David Brazdil0f672f62019-12-10 10:32:29 +00001226 /* allocate memory for pointer to array of dapm routes */
1227 routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
1228 GFP_KERNEL);
1229 if (!routes)
1230 return -ENOMEM;
1231
1232 /*
1233 * allocate memory for each dapm route in the array.
1234 * This needs to be done individually so that
1235 * each route can be freed when it is removed in remove_route().
1236 */
1237 for (i = 0; i < count; i++) {
1238 routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
1239 if (!routes[i]) {
1240 /* free previously allocated memory */
1241 for (j = 0; j < i; j++)
1242 kfree(routes[j]);
1243
1244 kfree(routes);
1245 return -ENOMEM;
1246 }
1247 }
1248
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001249 for (i = 0; i < count; i++) {
1250 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1251 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1252
1253 /* validate routes */
1254 if (strnlen(elem->source, 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 if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
David Brazdil0f672f62019-12-10 10:32:29 +00001260 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1261 ret = -EINVAL;
1262 break;
1263 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001264 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
David Brazdil0f672f62019-12-10 10:32:29 +00001265 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
1266 ret = -EINVAL;
1267 break;
1268 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001269
David Brazdil0f672f62019-12-10 10:32:29 +00001270 routes[i]->source = elem->source;
1271 routes[i]->sink = elem->sink;
1272
1273 /* set to NULL atm for tplg users */
1274 routes[i]->connected = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001275 if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
David Brazdil0f672f62019-12-10 10:32:29 +00001276 routes[i]->control = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001277 else
David Brazdil0f672f62019-12-10 10:32:29 +00001278 routes[i]->control = elem->control;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001279
David Brazdil0f672f62019-12-10 10:32:29 +00001280 /* add route dobj to dobj_list */
1281 routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
1282 routes[i]->dobj.ops = tplg->ops;
1283 routes[i]->dobj.index = tplg->index;
1284 list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1285
Olivier Deprez0e641232021-09-23 10:07:05 +02001286 ret = soc_tplg_add_route(tplg, routes[i]);
1287 if (ret < 0) {
1288 /*
1289 * this route was added to the list, it will
1290 * be freed in remove_route() so increment the
1291 * counter to skip it in the error handling
1292 * below.
1293 */
1294 i++;
1295 break;
1296 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001297
1298 /* add route, but keep going if some fail */
David Brazdil0f672f62019-12-10 10:32:29 +00001299 snd_soc_dapm_add_routes(dapm, routes[i], 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001300 }
1301
Olivier Deprez0e641232021-09-23 10:07:05 +02001302 /*
1303 * free memory allocated for all dapm routes not added to the
1304 * list in case of error
1305 */
1306 if (ret < 0) {
1307 while (i < count)
1308 kfree(routes[i++]);
1309 }
David Brazdil0f672f62019-12-10 10:32:29 +00001310
1311 /*
1312 * free pointer to array of dapm routes as this is no longer needed.
1313 * The memory allocated for each dapm route will be freed
1314 * when it is removed in remove_route().
1315 */
1316 kfree(routes);
1317
1318 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001319}
1320
1321static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
1322 struct soc_tplg *tplg, int num_kcontrols)
1323{
1324 struct snd_kcontrol_new *kc;
1325 struct soc_mixer_control *sm;
1326 struct snd_soc_tplg_mixer_control *mc;
1327 int i, err;
1328
1329 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1330 if (kc == NULL)
1331 return NULL;
1332
1333 for (i = 0; i < num_kcontrols; i++) {
1334 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001335
1336 /* validate kcontrol */
1337 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1338 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
David Brazdil0f672f62019-12-10 10:32:29 +00001339 goto err_sm;
1340
1341 sm = kzalloc(sizeof(*sm), GFP_KERNEL);
1342 if (sm == NULL)
1343 goto err_sm;
1344
1345 tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1346 le32_to_cpu(mc->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001347
1348 dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
1349 mc->hdr.name, i);
1350
David Brazdil0f672f62019-12-10 10:32:29 +00001351 kc[i].private_value = (long)sm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001352 kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
1353 if (kc[i].name == NULL)
David Brazdil0f672f62019-12-10 10:32:29 +00001354 goto err_sm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001355 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Olivier Deprez0e641232021-09-23 10:07:05 +02001356 kc[i].access = le32_to_cpu(mc->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001357
1358 /* we only support FL/FR channel mapping atm */
1359 sm->reg = tplc_chan_get_reg(tplg, mc->channel,
1360 SNDRV_CHMAP_FL);
1361 sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
1362 SNDRV_CHMAP_FR);
1363 sm->shift = tplc_chan_get_shift(tplg, mc->channel,
1364 SNDRV_CHMAP_FL);
1365 sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
1366 SNDRV_CHMAP_FR);
1367
Olivier Deprez0e641232021-09-23 10:07:05 +02001368 sm->max = le32_to_cpu(mc->max);
1369 sm->min = le32_to_cpu(mc->min);
1370 sm->invert = le32_to_cpu(mc->invert);
1371 sm->platform_max = le32_to_cpu(mc->platform_max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001372 sm->dobj.index = tplg->index;
1373 INIT_LIST_HEAD(&sm->dobj.list);
1374
1375 /* map io handlers */
1376 err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1377 if (err) {
1378 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +00001379 goto err_sm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001380 }
1381
David Brazdil0f672f62019-12-10 10:32:29 +00001382 /* create any TLV data */
Olivier Deprez0e641232021-09-23 10:07:05 +02001383 err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
1384 if (err < 0) {
1385 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
1386 mc->hdr.name);
1387 kfree(sm);
1388 continue;
1389 }
David Brazdil0f672f62019-12-10 10:32:29 +00001390
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001391 /* pass control to driver for optional further init */
1392 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1393 (struct snd_soc_tplg_ctl_hdr *)mc);
1394 if (err < 0) {
1395 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1396 mc->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +00001397 goto err_sm;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001398 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001399 }
1400 return kc;
1401
David Brazdil0f672f62019-12-10 10:32:29 +00001402err_sm:
1403 for (; i >= 0; i--) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001404 soc_tplg_free_tlv(tplg, &kc[i]);
David Brazdil0f672f62019-12-10 10:32:29 +00001405 sm = (struct soc_mixer_control *)kc[i].private_value;
1406 kfree(sm);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001407 kfree(kc[i].name);
1408 }
1409 kfree(kc);
David Brazdil0f672f62019-12-10 10:32:29 +00001410
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001411 return NULL;
1412}
1413
1414static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1415 struct soc_tplg *tplg, int num_kcontrols)
1416{
1417 struct snd_kcontrol_new *kc;
1418 struct snd_soc_tplg_enum_control *ec;
1419 struct soc_enum *se;
David Brazdil0f672f62019-12-10 10:32:29 +00001420 int i, err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001421
1422 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1423 if (kc == NULL)
1424 return NULL;
1425
1426 for (i = 0; i < num_kcontrols; i++) {
1427 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
1428 /* validate kcontrol */
1429 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1430 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
David Brazdil0f672f62019-12-10 10:32:29 +00001431 goto err_se;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001432
1433 se = kzalloc(sizeof(*se), GFP_KERNEL);
1434 if (se == NULL)
David Brazdil0f672f62019-12-10 10:32:29 +00001435 goto err_se;
1436
1437 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
Olivier Deprez0e641232021-09-23 10:07:05 +02001438 le32_to_cpu(ec->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001439
1440 dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
1441 ec->hdr.name);
1442
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001443 kc[i].private_value = (long)se;
David Brazdil0f672f62019-12-10 10:32:29 +00001444 kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
1445 if (kc[i].name == NULL)
1446 goto err_se;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001447 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Olivier Deprez0e641232021-09-23 10:07:05 +02001448 kc[i].access = le32_to_cpu(ec->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449
1450 /* we only support FL/FR channel mapping atm */
1451 se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
1452 se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
1453 SNDRV_CHMAP_FL);
1454 se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
1455 SNDRV_CHMAP_FR);
1456
Olivier Deprez0e641232021-09-23 10:07:05 +02001457 se->items = le32_to_cpu(ec->items);
1458 se->mask = le32_to_cpu(ec->mask);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001459 se->dobj.index = tplg->index;
1460
David Brazdil0f672f62019-12-10 10:32:29 +00001461 switch (le32_to_cpu(ec->hdr.ops.info)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001462 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1463 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1464 err = soc_tplg_denum_create_values(se, ec);
1465 if (err < 0) {
1466 dev_err(tplg->dev, "ASoC: could not create values for %s\n",
1467 ec->hdr.name);
1468 goto err_se;
1469 }
David Brazdil0f672f62019-12-10 10:32:29 +00001470 /* fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001471 case SND_SOC_TPLG_CTL_ENUM:
1472 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1473 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1474 err = soc_tplg_denum_create_texts(se, ec);
1475 if (err < 0) {
1476 dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
1477 ec->hdr.name);
1478 goto err_se;
1479 }
1480 break;
1481 default:
1482 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
1483 ec->hdr.ops.info, ec->hdr.name);
1484 goto err_se;
1485 }
1486
1487 /* map io handlers */
1488 err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
1489 if (err) {
1490 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1491 goto err_se;
1492 }
1493
1494 /* pass control to driver for optional further init */
1495 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1496 (struct snd_soc_tplg_ctl_hdr *)ec);
1497 if (err < 0) {
1498 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1499 ec->hdr.name);
1500 goto err_se;
1501 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001502 }
1503
1504 return kc;
1505
1506err_se:
1507 for (; i >= 0; i--) {
1508 /* free values and texts */
1509 se = (struct soc_enum *)kc[i].private_value;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001510
David Brazdil0f672f62019-12-10 10:32:29 +00001511 if (se) {
1512 soc_tplg_denum_remove_values(se);
1513 soc_tplg_denum_remove_texts(se);
1514 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001515
1516 kfree(se);
1517 kfree(kc[i].name);
1518 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001519 kfree(kc);
1520
1521 return NULL;
1522}
1523
1524static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
David Brazdil0f672f62019-12-10 10:32:29 +00001525 struct soc_tplg *tplg, int num_kcontrols)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001526{
1527 struct snd_soc_tplg_bytes_control *be;
David Brazdil0f672f62019-12-10 10:32:29 +00001528 struct soc_bytes_ext *sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001529 struct snd_kcontrol_new *kc;
1530 int i, err;
1531
David Brazdil0f672f62019-12-10 10:32:29 +00001532 kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001533 if (!kc)
1534 return NULL;
1535
David Brazdil0f672f62019-12-10 10:32:29 +00001536 for (i = 0; i < num_kcontrols; i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001537 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
1538
1539 /* validate kcontrol */
1540 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1541 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
David Brazdil0f672f62019-12-10 10:32:29 +00001542 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001543
1544 sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
1545 if (sbe == NULL)
David Brazdil0f672f62019-12-10 10:32:29 +00001546 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001547
1548 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
David Brazdil0f672f62019-12-10 10:32:29 +00001549 le32_to_cpu(be->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001550
1551 dev_dbg(tplg->dev,
1552 "ASoC: adding bytes kcontrol %s with access 0x%x\n",
1553 be->hdr.name, be->hdr.access);
1554
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001555 kc[i].private_value = (long)sbe;
David Brazdil0f672f62019-12-10 10:32:29 +00001556 kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
1557 if (kc[i].name == NULL)
1558 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001559 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
Olivier Deprez0e641232021-09-23 10:07:05 +02001560 kc[i].access = le32_to_cpu(be->hdr.access);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001561
Olivier Deprez0e641232021-09-23 10:07:05 +02001562 sbe->max = le32_to_cpu(be->max);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001563 INIT_LIST_HEAD(&sbe->dobj.list);
1564
1565 /* map standard io handlers and check for external handlers */
1566 err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1567 if (err) {
1568 soc_control_err(tplg, &be->hdr, be->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +00001569 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001570 }
1571
1572 /* pass control to driver for optional further init */
1573 err = soc_tplg_init_kcontrol(tplg, &kc[i],
1574 (struct snd_soc_tplg_ctl_hdr *)be);
1575 if (err < 0) {
1576 dev_err(tplg->dev, "ASoC: failed to init %s\n",
1577 be->hdr.name);
David Brazdil0f672f62019-12-10 10:32:29 +00001578 goto err_sbe;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001579 }
1580 }
1581
1582 return kc;
1583
David Brazdil0f672f62019-12-10 10:32:29 +00001584err_sbe:
1585 for (; i >= 0; i--) {
1586 sbe = (struct soc_bytes_ext *)kc[i].private_value;
1587 kfree(sbe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001588 kfree(kc[i].name);
1589 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001590 kfree(kc);
David Brazdil0f672f62019-12-10 10:32:29 +00001591
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001592 return NULL;
1593}
1594
1595static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1596 struct snd_soc_tplg_dapm_widget *w)
1597{
1598 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1599 struct snd_soc_dapm_widget template, *widget;
1600 struct snd_soc_tplg_ctl_hdr *control_hdr;
1601 struct snd_soc_card *card = tplg->comp->card;
1602 unsigned int kcontrol_type;
1603 int ret = 0;
1604
1605 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1606 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1607 return -EINVAL;
1608 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1609 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1610 return -EINVAL;
1611
1612 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1613 w->name, w->id);
1614
1615 memset(&template, 0, sizeof(template));
1616
1617 /* map user to kernel widget ID */
David Brazdil0f672f62019-12-10 10:32:29 +00001618 template.id = get_widget_id(le32_to_cpu(w->id));
1619 if ((int)template.id < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001620 return template.id;
1621
1622 /* strings are allocated here, but used and freed by the widget */
1623 template.name = kstrdup(w->name, GFP_KERNEL);
1624 if (!template.name)
1625 return -ENOMEM;
1626 template.sname = kstrdup(w->sname, GFP_KERNEL);
1627 if (!template.sname) {
1628 ret = -ENOMEM;
1629 goto err;
1630 }
David Brazdil0f672f62019-12-10 10:32:29 +00001631 template.reg = le32_to_cpu(w->reg);
1632 template.shift = le32_to_cpu(w->shift);
1633 template.mask = le32_to_cpu(w->mask);
1634 template.subseq = le32_to_cpu(w->subseq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001635 template.on_val = w->invert ? 0 : 1;
1636 template.off_val = w->invert ? 1 : 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001637 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1638 template.event_flags = le16_to_cpu(w->event_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001639 template.dobj.index = tplg->index;
1640
1641 tplg->pos +=
David Brazdil0f672f62019-12-10 10:32:29 +00001642 (sizeof(struct snd_soc_tplg_dapm_widget) +
1643 le32_to_cpu(w->priv.size));
1644
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001645 if (w->num_kcontrols == 0) {
1646 kcontrol_type = 0;
1647 template.num_kcontrols = 0;
1648 goto widget;
1649 }
1650
1651 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1652 dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
1653 w->name, w->num_kcontrols, control_hdr->type);
1654
David Brazdil0f672f62019-12-10 10:32:29 +00001655 switch (le32_to_cpu(control_hdr->ops.info)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001656 case SND_SOC_TPLG_CTL_VOLSW:
1657 case SND_SOC_TPLG_CTL_STROBE:
1658 case SND_SOC_TPLG_CTL_VOLSW_SX:
1659 case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
1660 case SND_SOC_TPLG_CTL_RANGE:
1661 case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1662 kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
David Brazdil0f672f62019-12-10 10:32:29 +00001663 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001664 template.kcontrol_news =
1665 soc_tplg_dapm_widget_dmixer_create(tplg,
1666 template.num_kcontrols);
1667 if (!template.kcontrol_news) {
1668 ret = -ENOMEM;
1669 goto hdr_err;
1670 }
1671 break;
1672 case SND_SOC_TPLG_CTL_ENUM:
1673 case SND_SOC_TPLG_CTL_ENUM_VALUE:
1674 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
1675 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
1676 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1677 kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
David Brazdil0f672f62019-12-10 10:32:29 +00001678 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001679 template.kcontrol_news =
1680 soc_tplg_dapm_widget_denum_create(tplg,
1681 template.num_kcontrols);
1682 if (!template.kcontrol_news) {
1683 ret = -ENOMEM;
1684 goto hdr_err;
1685 }
1686 break;
1687 case SND_SOC_TPLG_CTL_BYTES:
1688 kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
David Brazdil0f672f62019-12-10 10:32:29 +00001689 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001690 template.kcontrol_news =
1691 soc_tplg_dapm_widget_dbytes_create(tplg,
1692 template.num_kcontrols);
1693 if (!template.kcontrol_news) {
1694 ret = -ENOMEM;
1695 goto hdr_err;
1696 }
1697 break;
1698 default:
1699 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1700 control_hdr->ops.get, control_hdr->ops.put,
David Brazdil0f672f62019-12-10 10:32:29 +00001701 le32_to_cpu(control_hdr->ops.info));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001702 ret = -EINVAL;
1703 goto hdr_err;
1704 }
1705
1706widget:
1707 ret = soc_tplg_widget_load(tplg, &template, w);
1708 if (ret < 0)
1709 goto hdr_err;
1710
1711 /* card dapm mutex is held by the core if we are loading topology
1712 * data during sound card init. */
1713 if (card->instantiated)
1714 widget = snd_soc_dapm_new_control(dapm, &template);
1715 else
1716 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1717 if (IS_ERR(widget)) {
1718 ret = PTR_ERR(widget);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001719 goto hdr_err;
1720 }
1721
1722 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1723 widget->dobj.widget.kcontrol_type = kcontrol_type;
1724 widget->dobj.ops = tplg->ops;
1725 widget->dobj.index = tplg->index;
1726 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1727
1728 ret = soc_tplg_widget_ready(tplg, widget, w);
1729 if (ret < 0)
1730 goto ready_err;
1731
David Brazdil0f672f62019-12-10 10:32:29 +00001732 kfree(template.sname);
1733 kfree(template.name);
1734
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001735 return 0;
1736
1737ready_err:
1738 snd_soc_tplg_widget_remove(widget);
1739 snd_soc_dapm_free_widget(widget);
1740hdr_err:
1741 kfree(template.sname);
1742err:
1743 kfree(template.name);
1744 return ret;
1745}
1746
1747static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1748 struct snd_soc_tplg_hdr *hdr)
1749{
1750 struct snd_soc_tplg_dapm_widget *widget;
David Brazdil0f672f62019-12-10 10:32:29 +00001751 int ret, count, i;
1752
1753 count = le32_to_cpu(hdr->count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001754
1755 if (tplg->pass != SOC_TPLG_PASS_WIDGET)
1756 return 0;
1757
1758 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1759
1760 for (i = 0; i < count; i++) {
1761 widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00001762 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001763 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1764 return -EINVAL;
1765 }
1766
1767 ret = soc_tplg_dapm_widget_create(tplg, widget);
1768 if (ret < 0) {
1769 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1770 widget->name);
1771 return ret;
1772 }
1773 }
1774
1775 return 0;
1776}
1777
1778static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1779{
1780 struct snd_soc_card *card = tplg->comp->card;
1781 int ret;
1782
1783 /* Card might not have been registered at this point.
1784 * If so, just return success.
1785 */
1786 if (!card || !card->instantiated) {
1787 dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1788 " widget card binding deferred\n");
1789 return 0;
1790 }
1791
1792 ret = snd_soc_dapm_new_widgets(card);
1793 if (ret < 0)
1794 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
1795 ret);
1796
1797 return 0;
1798}
1799
1800static void set_stream_info(struct snd_soc_pcm_stream *stream,
1801 struct snd_soc_tplg_stream_caps *caps)
1802{
1803 stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
David Brazdil0f672f62019-12-10 10:32:29 +00001804 stream->channels_min = le32_to_cpu(caps->channels_min);
1805 stream->channels_max = le32_to_cpu(caps->channels_max);
1806 stream->rates = le32_to_cpu(caps->rates);
1807 stream->rate_min = le32_to_cpu(caps->rate_min);
1808 stream->rate_max = le32_to_cpu(caps->rate_max);
1809 stream->formats = le64_to_cpu(caps->formats);
1810 stream->sig_bits = le32_to_cpu(caps->sig_bits);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001811}
1812
1813static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1814 unsigned int flag_mask, unsigned int flags)
1815{
1816 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1817 dai_drv->symmetric_rates =
1818 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1819
1820 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1821 dai_drv->symmetric_channels =
1822 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
1823 1 : 0;
1824
1825 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1826 dai_drv->symmetric_samplebits =
1827 flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1828 1 : 0;
1829}
1830
1831static int soc_tplg_dai_create(struct soc_tplg *tplg,
1832 struct snd_soc_tplg_pcm *pcm)
1833{
1834 struct snd_soc_dai_driver *dai_drv;
1835 struct snd_soc_pcm_stream *stream;
1836 struct snd_soc_tplg_stream_caps *caps;
1837 int ret;
1838
1839 dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1840 if (dai_drv == NULL)
1841 return -ENOMEM;
1842
1843 if (strlen(pcm->dai_name))
1844 dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
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];
1850 set_stream_info(stream, caps);
1851 }
1852
1853 if (pcm->capture) {
1854 stream = &dai_drv->capture;
1855 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1856 set_stream_info(stream, caps);
1857 }
1858
1859 if (pcm->compress)
1860 dai_drv->compress_new = snd_soc_new_compress;
1861
1862 /* pass control to component driver for optional further init */
1863 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1864 if (ret < 0) {
1865 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
David Brazdil0f672f62019-12-10 10:32:29 +00001866 kfree(dai_drv->playback.stream_name);
1867 kfree(dai_drv->capture.stream_name);
1868 kfree(dai_drv->name);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001869 kfree(dai_drv);
1870 return ret;
1871 }
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 */
1879 return snd_soc_register_dai(tplg->comp, dai_drv);
1880}
1881
1882static void set_link_flags(struct snd_soc_dai_link *link,
1883 unsigned int flag_mask, unsigned int flags)
1884{
1885 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1886 link->symmetric_rates =
1887 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
1888
1889 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1890 link->symmetric_channels =
1891 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
1892 1 : 0;
1893
1894 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1895 link->symmetric_samplebits =
1896 flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
1897 1 : 0;
1898
1899 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1900 link->ignore_suspend =
1901 flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
1902 1 : 0;
1903}
1904
1905/* create the FE DAI link */
1906static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1907 struct snd_soc_tplg_pcm *pcm)
1908{
1909 struct snd_soc_dai_link *link;
David Brazdil0f672f62019-12-10 10:32:29 +00001910 struct snd_soc_dai_link_component *dlc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001911 int ret;
1912
David Brazdil0f672f62019-12-10 10:32:29 +00001913 /* link + cpu + codec + platform */
1914 link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001915 if (link == NULL)
1916 return -ENOMEM;
1917
David Brazdil0f672f62019-12-10 10:32:29 +00001918 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1919
1920 link->cpus = &dlc[0];
1921 link->codecs = &dlc[1];
1922 link->platforms = &dlc[2];
1923
1924 link->num_cpus = 1;
1925 link->num_codecs = 1;
1926 link->num_platforms = 1;
1927
Olivier Deprez0e641232021-09-23 10:07:05 +02001928 link->dobj.index = tplg->index;
1929 link->dobj.ops = tplg->ops;
1930 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1931
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001932 if (strlen(pcm->pcm_name)) {
1933 link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1934 link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1935 }
David Brazdil0f672f62019-12-10 10:32:29 +00001936 link->id = le32_to_cpu(pcm->pcm_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001937
1938 if (strlen(pcm->dai_name))
David Brazdil0f672f62019-12-10 10:32:29 +00001939 link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001940
David Brazdil0f672f62019-12-10 10:32:29 +00001941 link->codecs->name = "snd-soc-dummy";
1942 link->codecs->dai_name = "snd-soc-dummy-dai";
1943
1944 link->platforms->name = "snd-soc-dummy";
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001945
1946 /* enable DPCM */
1947 link->dynamic = 1;
David Brazdil0f672f62019-12-10 10:32:29 +00001948 link->dpcm_playback = le32_to_cpu(pcm->playback);
1949 link->dpcm_capture = le32_to_cpu(pcm->capture);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001950 if (pcm->flag_mask)
David Brazdil0f672f62019-12-10 10:32:29 +00001951 set_link_flags(link,
1952 le32_to_cpu(pcm->flag_mask),
1953 le32_to_cpu(pcm->flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001954
1955 /* pass control to component driver for optional further init */
1956 ret = soc_tplg_dai_link_load(tplg, link, NULL);
1957 if (ret < 0) {
1958 dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
Olivier Deprez0e641232021-09-23 10:07:05 +02001959 goto err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001960 }
1961
Olivier Deprez0e641232021-09-23 10:07:05 +02001962 ret = snd_soc_add_dai_link(tplg->comp->card, link);
1963 if (ret < 0) {
1964 dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n");
1965 goto err;
1966 }
1967
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001968 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1969
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001970 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02001971err:
1972 kfree(link->name);
1973 kfree(link->stream_name);
1974 kfree(link->cpus->dai_name);
1975 kfree(link);
1976 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001977}
1978
1979/* create a FE DAI and DAI link from the PCM object */
1980static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1981 struct snd_soc_tplg_pcm *pcm)
1982{
1983 int ret;
1984
1985 ret = soc_tplg_dai_create(tplg, pcm);
1986 if (ret < 0)
1987 return ret;
1988
1989 return soc_tplg_fe_link_create(tplg, pcm);
1990}
1991
1992/* copy stream caps from the old version 4 of source */
1993static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
1994 struct snd_soc_tplg_stream_caps_v4 *src)
1995{
David Brazdil0f672f62019-12-10 10:32:29 +00001996 dest->size = cpu_to_le32(sizeof(*dest));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001997 memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1998 dest->formats = src->formats;
1999 dest->rates = src->rates;
2000 dest->rate_min = src->rate_min;
2001 dest->rate_max = src->rate_max;
2002 dest->channels_min = src->channels_min;
2003 dest->channels_max = src->channels_max;
2004 dest->periods_min = src->periods_min;
2005 dest->periods_max = src->periods_max;
2006 dest->period_size_min = src->period_size_min;
2007 dest->period_size_max = src->period_size_max;
2008 dest->buffer_size_min = src->buffer_size_min;
2009 dest->buffer_size_max = src->buffer_size_max;
2010}
2011
2012/**
2013 * pcm_new_ver - Create the new version of PCM from the old version.
2014 * @tplg: topology context
2015 * @src: older version of pcm as a source
2016 * @pcm: latest version of pcm created from the source
2017 *
2018 * Support from vesion 4. User should free the returned pcm manually.
2019 */
2020static int pcm_new_ver(struct soc_tplg *tplg,
2021 struct snd_soc_tplg_pcm *src,
2022 struct snd_soc_tplg_pcm **pcm)
2023{
2024 struct snd_soc_tplg_pcm *dest;
2025 struct snd_soc_tplg_pcm_v4 *src_v4;
2026 int i;
2027
2028 *pcm = NULL;
2029
David Brazdil0f672f62019-12-10 10:32:29 +00002030 if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002031 dev_err(tplg->dev, "ASoC: invalid PCM size\n");
2032 return -EINVAL;
2033 }
2034
2035 dev_warn(tplg->dev, "ASoC: old version of PCM\n");
2036 src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
2037 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2038 if (!dest)
2039 return -ENOMEM;
2040
David Brazdil0f672f62019-12-10 10:32:29 +00002041 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002042 memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2043 memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2044 dest->pcm_id = src_v4->pcm_id;
2045 dest->dai_id = src_v4->dai_id;
2046 dest->playback = src_v4->playback;
2047 dest->capture = src_v4->capture;
2048 dest->compress = src_v4->compress;
2049 dest->num_streams = src_v4->num_streams;
David Brazdil0f672f62019-12-10 10:32:29 +00002050 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002051 memcpy(&dest->stream[i], &src_v4->stream[i],
2052 sizeof(struct snd_soc_tplg_stream));
2053
2054 for (i = 0; i < 2; i++)
2055 stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
2056
2057 *pcm = dest;
2058 return 0;
2059}
2060
2061static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
2062 struct snd_soc_tplg_hdr *hdr)
2063{
2064 struct snd_soc_tplg_pcm *pcm, *_pcm;
David Brazdil0f672f62019-12-10 10:32:29 +00002065 int count;
2066 int size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002067 int i;
2068 bool abi_match;
Olivier Deprez0e641232021-09-23 10:07:05 +02002069 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002070
David Brazdil0f672f62019-12-10 10:32:29 +00002071 count = le32_to_cpu(hdr->count);
2072
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002073 if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
2074 return 0;
2075
2076 /* check the element size and count */
2077 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002078 size = le32_to_cpu(pcm->size);
2079 if (size > sizeof(struct snd_soc_tplg_pcm)
2080 || size < sizeof(struct snd_soc_tplg_pcm_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002081 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002082 size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002083 return -EINVAL;
2084 }
2085
2086 if (soc_tplg_check_elem_count(tplg,
David Brazdil0f672f62019-12-10 10:32:29 +00002087 size, count,
2088 le32_to_cpu(hdr->payload_size),
2089 "PCM DAI")) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002090 dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
2091 count);
2092 return -EINVAL;
2093 }
2094
2095 for (i = 0; i < count; i++) {
2096 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002097 size = le32_to_cpu(pcm->size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002098
2099 /* check ABI version by size, create a new version of pcm
2100 * if abi not match.
2101 */
David Brazdil0f672f62019-12-10 10:32:29 +00002102 if (size == sizeof(*pcm)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002103 abi_match = true;
2104 _pcm = pcm;
2105 } else {
2106 abi_match = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02002107 ret = pcm_new_ver(tplg, pcm, &_pcm);
2108 if (ret < 0)
2109 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002110 }
2111
2112 /* create the FE DAIs and DAI links */
Olivier Deprez0e641232021-09-23 10:07:05 +02002113 ret = soc_tplg_pcm_create(tplg, _pcm);
2114 if (ret < 0) {
2115 if (!abi_match)
2116 kfree(_pcm);
2117 return ret;
2118 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002119
2120 /* offset by version-specific struct size and
2121 * real priv data size
2122 */
David Brazdil0f672f62019-12-10 10:32:29 +00002123 tplg->pos += size + le32_to_cpu(_pcm->priv.size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002124
2125 if (!abi_match)
2126 kfree(_pcm); /* free the duplicated one */
2127 }
2128
2129 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
2130
2131 return 0;
2132}
2133
2134/**
2135 * set_link_hw_format - Set the HW audio format of the physical DAI link.
2136 * @link: &snd_soc_dai_link which should be updated
2137 * @cfg: physical link configs.
2138 *
2139 * Topology context contains a list of supported HW formats (configs) and
2140 * a default format ID for the physical link. This function will use this
2141 * default ID to choose the HW format to set the link's DAI format for init.
2142 */
2143static void set_link_hw_format(struct snd_soc_dai_link *link,
2144 struct snd_soc_tplg_link_config *cfg)
2145{
2146 struct snd_soc_tplg_hw_config *hw_config;
2147 unsigned char bclk_master, fsync_master;
2148 unsigned char invert_bclk, invert_fsync;
2149 int i;
2150
David Brazdil0f672f62019-12-10 10:32:29 +00002151 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002152 hw_config = &cfg->hw_config[i];
2153 if (hw_config->id != cfg->default_hw_config_id)
2154 continue;
2155
David Brazdil0f672f62019-12-10 10:32:29 +00002156 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
2157 SND_SOC_DAIFMT_FORMAT_MASK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002158
2159 /* clock gating */
2160 switch (hw_config->clock_gated) {
2161 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2162 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2163 break;
2164
2165 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2166 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2167 break;
2168
2169 default:
2170 /* ignore the value */
2171 break;
2172 }
2173
2174 /* clock signal polarity */
2175 invert_bclk = hw_config->invert_bclk;
2176 invert_fsync = hw_config->invert_fsync;
2177 if (!invert_bclk && !invert_fsync)
2178 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
2179 else if (!invert_bclk && invert_fsync)
2180 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
2181 else if (invert_bclk && !invert_fsync)
2182 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
2183 else
2184 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
2185
2186 /* clock masters */
2187 bclk_master = (hw_config->bclk_master ==
2188 SND_SOC_TPLG_BCLK_CM);
2189 fsync_master = (hw_config->fsync_master ==
2190 SND_SOC_TPLG_FSYNC_CM);
2191 if (bclk_master && fsync_master)
2192 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
2193 else if (!bclk_master && fsync_master)
2194 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
2195 else if (bclk_master && !fsync_master)
2196 link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
2197 else
2198 link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
2199 }
2200}
2201
2202/**
2203 * link_new_ver - Create a new physical link config from the old
2204 * version of source.
2205 * @tplg: topology context
2206 * @src: old version of phyical link config as a source
2207 * @link: latest version of physical link config created from the source
2208 *
2209 * Support from vesion 4. User need free the returned link config manually.
2210 */
2211static int link_new_ver(struct soc_tplg *tplg,
2212 struct snd_soc_tplg_link_config *src,
2213 struct snd_soc_tplg_link_config **link)
2214{
2215 struct snd_soc_tplg_link_config *dest;
2216 struct snd_soc_tplg_link_config_v4 *src_v4;
2217 int i;
2218
2219 *link = NULL;
2220
David Brazdil0f672f62019-12-10 10:32:29 +00002221 if (le32_to_cpu(src->size) !=
2222 sizeof(struct snd_soc_tplg_link_config_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002223 dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
2224 return -EINVAL;
2225 }
2226
2227 dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
2228
2229 src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
2230 dest = kzalloc(sizeof(*dest), GFP_KERNEL);
2231 if (!dest)
2232 return -ENOMEM;
2233
David Brazdil0f672f62019-12-10 10:32:29 +00002234 dest->size = cpu_to_le32(sizeof(*dest));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002235 dest->id = src_v4->id;
2236 dest->num_streams = src_v4->num_streams;
David Brazdil0f672f62019-12-10 10:32:29 +00002237 for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002238 memcpy(&dest->stream[i], &src_v4->stream[i],
2239 sizeof(struct snd_soc_tplg_stream));
2240
2241 *link = dest;
2242 return 0;
2243}
2244
2245/* Find and configure an existing physical DAI link */
2246static int soc_tplg_link_config(struct soc_tplg *tplg,
2247 struct snd_soc_tplg_link_config *cfg)
2248{
2249 struct snd_soc_dai_link *link;
2250 const char *name, *stream_name;
2251 size_t len;
2252 int ret;
2253
2254 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2255 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2256 return -EINVAL;
2257 else if (len)
2258 name = cfg->name;
2259 else
2260 name = NULL;
2261
2262 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
2263 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
2264 return -EINVAL;
2265 else if (len)
2266 stream_name = cfg->stream_name;
2267 else
2268 stream_name = NULL;
2269
David Brazdil0f672f62019-12-10 10:32:29 +00002270 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002271 name, stream_name);
2272 if (!link) {
2273 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
2274 name, cfg->id);
2275 return -EINVAL;
2276 }
2277
2278 /* hw format */
2279 if (cfg->num_hw_configs)
2280 set_link_hw_format(link, cfg);
2281
2282 /* flags */
2283 if (cfg->flag_mask)
David Brazdil0f672f62019-12-10 10:32:29 +00002284 set_link_flags(link,
2285 le32_to_cpu(cfg->flag_mask),
2286 le32_to_cpu(cfg->flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002287
2288 /* pass control to component driver for optional further init */
2289 ret = soc_tplg_dai_link_load(tplg, link, cfg);
2290 if (ret < 0) {
2291 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
2292 return ret;
2293 }
2294
David Brazdil0f672f62019-12-10 10:32:29 +00002295 /* for unloading it in snd_soc_tplg_component_remove */
2296 link->dobj.index = tplg->index;
2297 link->dobj.ops = tplg->ops;
2298 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
2299 list_add(&link->dobj.list, &tplg->comp->dobj_list);
2300
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002301 return 0;
2302}
2303
2304
2305/* Load physical link config elements from the topology context */
2306static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
2307 struct snd_soc_tplg_hdr *hdr)
2308{
2309 struct snd_soc_tplg_link_config *link, *_link;
David Brazdil0f672f62019-12-10 10:32:29 +00002310 int count;
2311 int size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002312 int i, ret;
2313 bool abi_match;
2314
David Brazdil0f672f62019-12-10 10:32:29 +00002315 count = le32_to_cpu(hdr->count);
2316
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002317 if (tplg->pass != SOC_TPLG_PASS_LINK) {
David Brazdil0f672f62019-12-10 10:32:29 +00002318 tplg->pos += le32_to_cpu(hdr->size) +
2319 le32_to_cpu(hdr->payload_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002320 return 0;
2321 };
2322
2323 /* check the element size and count */
2324 link = (struct snd_soc_tplg_link_config *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002325 size = le32_to_cpu(link->size);
2326 if (size > sizeof(struct snd_soc_tplg_link_config)
2327 || size < sizeof(struct snd_soc_tplg_link_config_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002328 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002329 size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002330 return -EINVAL;
2331 }
2332
2333 if (soc_tplg_check_elem_count(tplg,
David Brazdil0f672f62019-12-10 10:32:29 +00002334 size, count,
2335 le32_to_cpu(hdr->payload_size),
2336 "physical link config")) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002337 dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
2338 count);
2339 return -EINVAL;
2340 }
2341
2342 /* config physical DAI links */
2343 for (i = 0; i < count; i++) {
2344 link = (struct snd_soc_tplg_link_config *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002345 size = le32_to_cpu(link->size);
2346 if (size == sizeof(*link)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002347 abi_match = true;
2348 _link = link;
2349 } else {
2350 abi_match = false;
2351 ret = link_new_ver(tplg, link, &_link);
2352 if (ret < 0)
2353 return ret;
2354 }
2355
2356 ret = soc_tplg_link_config(tplg, _link);
Olivier Deprez0e641232021-09-23 10:07:05 +02002357 if (ret < 0) {
2358 if (!abi_match)
2359 kfree(_link);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002360 return ret;
Olivier Deprez0e641232021-09-23 10:07:05 +02002361 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002362
2363 /* offset by version-specific struct size and
2364 * real priv data size
2365 */
David Brazdil0f672f62019-12-10 10:32:29 +00002366 tplg->pos += size + le32_to_cpu(_link->priv.size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002367
2368 if (!abi_match)
2369 kfree(_link); /* free the duplicated one */
2370 }
2371
2372 return 0;
2373}
2374
2375/**
2376 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2377 * @tplg: topology context
2378 * @d: physical DAI configs.
2379 *
2380 * The physical dai should already be registered by the platform driver.
2381 * The platform driver should specify the DAI name and ID for matching.
2382 */
2383static int soc_tplg_dai_config(struct soc_tplg *tplg,
2384 struct snd_soc_tplg_dai *d)
2385{
David Brazdil0f672f62019-12-10 10:32:29 +00002386 struct snd_soc_dai_link_component dai_component;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002387 struct snd_soc_dai *dai;
2388 struct snd_soc_dai_driver *dai_drv;
2389 struct snd_soc_pcm_stream *stream;
2390 struct snd_soc_tplg_stream_caps *caps;
2391 int ret;
2392
David Brazdil0f672f62019-12-10 10:32:29 +00002393 memset(&dai_component, 0, sizeof(dai_component));
2394
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002395 dai_component.dai_name = d->dai_name;
2396 dai = snd_soc_find_dai(&dai_component);
2397 if (!dai) {
2398 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
2399 d->dai_name);
2400 return -EINVAL;
2401 }
2402
David Brazdil0f672f62019-12-10 10:32:29 +00002403 if (le32_to_cpu(d->dai_id) != dai->id) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002404 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
2405 d->dai_name);
2406 return -EINVAL;
2407 }
2408
2409 dai_drv = dai->driver;
2410 if (!dai_drv)
2411 return -EINVAL;
2412
2413 if (d->playback) {
2414 stream = &dai_drv->playback;
2415 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2416 set_stream_info(stream, caps);
2417 }
2418
2419 if (d->capture) {
2420 stream = &dai_drv->capture;
2421 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2422 set_stream_info(stream, caps);
2423 }
2424
2425 if (d->flag_mask)
David Brazdil0f672f62019-12-10 10:32:29 +00002426 set_dai_flags(dai_drv,
2427 le32_to_cpu(d->flag_mask),
2428 le32_to_cpu(d->flags));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002429
2430 /* pass control to component driver for optional further init */
2431 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2432 if (ret < 0) {
2433 dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2434 return ret;
2435 }
2436
2437 return 0;
2438}
2439
2440/* load physical DAI elements */
2441static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
2442 struct snd_soc_tplg_hdr *hdr)
2443{
2444 struct snd_soc_tplg_dai *dai;
David Brazdil0f672f62019-12-10 10:32:29 +00002445 int count;
Olivier Deprez0e641232021-09-23 10:07:05 +02002446 int i, ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002447
David Brazdil0f672f62019-12-10 10:32:29 +00002448 count = le32_to_cpu(hdr->count);
2449
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002450 if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
2451 return 0;
2452
2453 /* config the existing BE DAIs */
2454 for (i = 0; i < count; i++) {
2455 dai = (struct snd_soc_tplg_dai *)tplg->pos;
David Brazdil0f672f62019-12-10 10:32:29 +00002456 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002457 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2458 return -EINVAL;
2459 }
2460
Olivier Deprez0e641232021-09-23 10:07:05 +02002461 ret = soc_tplg_dai_config(tplg, dai);
2462 if (ret < 0) {
2463 dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
2464 return ret;
2465 }
2466
David Brazdil0f672f62019-12-10 10:32:29 +00002467 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002468 }
2469
2470 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
2471 return 0;
2472}
2473
2474/**
2475 * manifest_new_ver - Create a new version of manifest from the old version
2476 * of source.
2477 * @tplg: topology context
2478 * @src: old version of manifest as a source
2479 * @manifest: latest version of manifest created from the source
2480 *
2481 * Support from vesion 4. Users need free the returned manifest manually.
2482 */
2483static int manifest_new_ver(struct soc_tplg *tplg,
2484 struct snd_soc_tplg_manifest *src,
2485 struct snd_soc_tplg_manifest **manifest)
2486{
2487 struct snd_soc_tplg_manifest *dest;
2488 struct snd_soc_tplg_manifest_v4 *src_v4;
David Brazdil0f672f62019-12-10 10:32:29 +00002489 int size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002490
2491 *manifest = NULL;
2492
David Brazdil0f672f62019-12-10 10:32:29 +00002493 size = le32_to_cpu(src->size);
2494 if (size != sizeof(*src_v4)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002495 dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002496 size);
2497 if (size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002498 return -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00002499 src->size = cpu_to_le32(sizeof(*src_v4));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002500 }
2501
2502 dev_warn(tplg->dev, "ASoC: old version of manifest\n");
2503
2504 src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
David Brazdil0f672f62019-12-10 10:32:29 +00002505 dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
2506 GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002507 if (!dest)
2508 return -ENOMEM;
2509
David Brazdil0f672f62019-12-10 10:32:29 +00002510 dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002511 dest->control_elems = src_v4->control_elems;
2512 dest->widget_elems = src_v4->widget_elems;
2513 dest->graph_elems = src_v4->graph_elems;
2514 dest->pcm_elems = src_v4->pcm_elems;
2515 dest->dai_link_elems = src_v4->dai_link_elems;
2516 dest->priv.size = src_v4->priv.size;
2517 if (dest->priv.size)
2518 memcpy(dest->priv.data, src_v4->priv.data,
David Brazdil0f672f62019-12-10 10:32:29 +00002519 le32_to_cpu(src_v4->priv.size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002520
2521 *manifest = dest;
2522 return 0;
2523}
2524
2525static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2526 struct snd_soc_tplg_hdr *hdr)
2527{
2528 struct snd_soc_tplg_manifest *manifest, *_manifest;
2529 bool abi_match;
Olivier Deprez0e641232021-09-23 10:07:05 +02002530 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002531
2532 if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
2533 return 0;
2534
2535 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2536
2537 /* check ABI version by size, create a new manifest if abi not match */
David Brazdil0f672f62019-12-10 10:32:29 +00002538 if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002539 abi_match = true;
2540 _manifest = manifest;
2541 } else {
2542 abi_match = false;
Olivier Deprez0e641232021-09-23 10:07:05 +02002543 ret = manifest_new_ver(tplg, manifest, &_manifest);
2544 if (ret < 0)
2545 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002546 }
2547
2548 /* pass control to component driver for optional further init */
2549 if (tplg->comp && tplg->ops && tplg->ops->manifest)
Olivier Deprez0e641232021-09-23 10:07:05 +02002550 ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002551
2552 if (!abi_match) /* free the duplicated one */
2553 kfree(_manifest);
2554
Olivier Deprez0e641232021-09-23 10:07:05 +02002555 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002556}
2557
2558/* validate header magic, size and type */
2559static int soc_valid_header(struct soc_tplg *tplg,
2560 struct snd_soc_tplg_hdr *hdr)
2561{
2562 if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
2563 return 0;
2564
David Brazdil0f672f62019-12-10 10:32:29 +00002565 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002566 dev_err(tplg->dev,
2567 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
David Brazdil0f672f62019-12-10 10:32:29 +00002568 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002569 tplg->fw->size);
2570 return -EINVAL;
2571 }
2572
2573 /* big endian firmware objects not supported atm */
Olivier Deprez0e641232021-09-23 10:07:05 +02002574 if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002575 dev_err(tplg->dev,
2576 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
2577 tplg->pass, hdr->magic,
2578 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2579 return -EINVAL;
2580 }
2581
David Brazdil0f672f62019-12-10 10:32:29 +00002582 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002583 dev_err(tplg->dev,
2584 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
2585 tplg->pass, hdr->magic,
2586 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
2587 return -EINVAL;
2588 }
2589
2590 /* Support ABI from version 4 */
David Brazdil0f672f62019-12-10 10:32:29 +00002591 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
2592 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002593 dev_err(tplg->dev,
2594 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
2595 tplg->pass, hdr->abi,
2596 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
2597 tplg->fw->size);
2598 return -EINVAL;
2599 }
2600
2601 if (hdr->payload_size == 0) {
2602 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
2603 soc_tplg_get_hdr_offset(tplg));
2604 return -EINVAL;
2605 }
2606
David Brazdil0f672f62019-12-10 10:32:29 +00002607 if (tplg->pass == le32_to_cpu(hdr->type))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002608 dev_dbg(tplg->dev,
2609 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2610 hdr->payload_size, hdr->type, hdr->version,
2611 hdr->vendor_type, tplg->pass);
2612
2613 return 1;
2614}
2615
2616/* check header type and call appropriate handler */
2617static int soc_tplg_load_header(struct soc_tplg *tplg,
2618 struct snd_soc_tplg_hdr *hdr)
2619{
2620 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2621
2622 /* check for matching ID */
David Brazdil0f672f62019-12-10 10:32:29 +00002623 if (le32_to_cpu(hdr->index) != tplg->req_index &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002624 tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
2625 return 0;
2626
David Brazdil0f672f62019-12-10 10:32:29 +00002627 tplg->index = le32_to_cpu(hdr->index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002628
David Brazdil0f672f62019-12-10 10:32:29 +00002629 switch (le32_to_cpu(hdr->type)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002630 case SND_SOC_TPLG_TYPE_MIXER:
2631 case SND_SOC_TPLG_TYPE_ENUM:
2632 case SND_SOC_TPLG_TYPE_BYTES:
2633 return soc_tplg_kcontrol_elems_load(tplg, hdr);
2634 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2635 return soc_tplg_dapm_graph_elems_load(tplg, hdr);
2636 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2637 return soc_tplg_dapm_widget_elems_load(tplg, hdr);
2638 case SND_SOC_TPLG_TYPE_PCM:
2639 return soc_tplg_pcm_elems_load(tplg, hdr);
2640 case SND_SOC_TPLG_TYPE_DAI:
2641 return soc_tplg_dai_elems_load(tplg, hdr);
2642 case SND_SOC_TPLG_TYPE_DAI_LINK:
2643 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2644 /* physical link configurations */
2645 return soc_tplg_link_elems_load(tplg, hdr);
2646 case SND_SOC_TPLG_TYPE_MANIFEST:
2647 return soc_tplg_manifest_load(tplg, hdr);
2648 default:
2649 /* bespoke vendor data object */
2650 return soc_tplg_vendor_load(tplg, hdr);
2651 }
2652
2653 return 0;
2654}
2655
2656/* process the topology file headers */
2657static int soc_tplg_process_headers(struct soc_tplg *tplg)
2658{
2659 struct snd_soc_tplg_hdr *hdr;
2660 int ret;
2661
2662 tplg->pass = SOC_TPLG_PASS_START;
2663
2664 /* process the header types from start to end */
2665 while (tplg->pass <= SOC_TPLG_PASS_END) {
2666
2667 tplg->hdr_pos = tplg->fw->data;
2668 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2669
2670 while (!soc_tplg_is_eof(tplg)) {
2671
2672 /* make sure header is valid before loading */
2673 ret = soc_valid_header(tplg, hdr);
2674 if (ret < 0)
2675 return ret;
2676 else if (ret == 0)
2677 break;
2678
2679 /* load the header object */
2680 ret = soc_tplg_load_header(tplg, hdr);
2681 if (ret < 0)
2682 return ret;
2683
2684 /* goto next header */
David Brazdil0f672f62019-12-10 10:32:29 +00002685 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002686 sizeof(struct snd_soc_tplg_hdr);
2687 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2688 }
2689
2690 /* next data type pass */
2691 tplg->pass++;
2692 }
2693
2694 /* signal DAPM we are complete */
2695 ret = soc_tplg_dapm_complete(tplg);
2696 if (ret < 0)
2697 dev_err(tplg->dev,
2698 "ASoC: failed to initialise DAPM from Firmware\n");
2699
2700 return ret;
2701}
2702
2703static int soc_tplg_load(struct soc_tplg *tplg)
2704{
2705 int ret;
2706
2707 ret = soc_tplg_process_headers(tplg);
2708 if (ret == 0)
2709 soc_tplg_complete(tplg);
2710
2711 return ret;
2712}
2713
2714/* load audio component topology from "firmware" file */
2715int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2716 struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
2717{
2718 struct soc_tplg tplg;
David Brazdil0f672f62019-12-10 10:32:29 +00002719 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002720
2721 /* setup parsing context */
2722 memset(&tplg, 0, sizeof(tplg));
2723 tplg.fw = fw;
2724 tplg.dev = comp->dev;
2725 tplg.comp = comp;
2726 tplg.ops = ops;
2727 tplg.req_index = id;
2728 tplg.io_ops = ops->io_ops;
2729 tplg.io_ops_count = ops->io_ops_count;
2730 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2731 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2732
David Brazdil0f672f62019-12-10 10:32:29 +00002733 ret = soc_tplg_load(&tplg);
2734 /* free the created components if fail to load topology */
2735 if (ret)
2736 snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);
2737
2738 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002739}
2740EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2741
2742/* remove this dynamic widget */
2743void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
2744{
2745 /* make sure we are a widget */
2746 if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
2747 return;
2748
2749 remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
2750}
2751EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
2752
2753/* remove all dynamic widgets from this DAPM context */
2754void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
2755 u32 index)
2756{
2757 struct snd_soc_dapm_widget *w, *next_w;
2758
2759 list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
2760
2761 /* make sure we are a widget with correct context */
2762 if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
2763 continue;
2764
2765 /* match ID */
2766 if (w->dobj.index != index &&
2767 w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
2768 continue;
2769 /* check and free and dynamic widget kcontrols */
2770 snd_soc_tplg_widget_remove(w);
2771 snd_soc_dapm_free_widget(w);
2772 }
2773 snd_soc_dapm_reset_cache(dapm);
2774}
2775EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
2776
2777/* remove dynamic controls from the component driver */
2778int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
2779{
2780 struct snd_soc_dobj *dobj, *next_dobj;
2781 int pass = SOC_TPLG_PASS_END;
2782
2783 /* process the header types from end to start */
2784 while (pass >= SOC_TPLG_PASS_START) {
2785
2786 /* remove mixer controls */
2787 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2788 list) {
2789
2790 /* match index */
2791 if (dobj->index != index &&
2792 index != SND_SOC_TPLG_INDEX_ALL)
2793 continue;
2794
2795 switch (dobj->type) {
2796 case SND_SOC_DOBJ_MIXER:
2797 remove_mixer(comp, dobj, pass);
2798 break;
2799 case SND_SOC_DOBJ_ENUM:
2800 remove_enum(comp, dobj, pass);
2801 break;
2802 case SND_SOC_DOBJ_BYTES:
2803 remove_bytes(comp, dobj, pass);
2804 break;
David Brazdil0f672f62019-12-10 10:32:29 +00002805 case SND_SOC_DOBJ_GRAPH:
2806 remove_route(comp, dobj, pass);
2807 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002808 case SND_SOC_DOBJ_WIDGET:
2809 remove_widget(comp, dobj, pass);
2810 break;
2811 case SND_SOC_DOBJ_PCM:
2812 remove_dai(comp, dobj, pass);
2813 break;
2814 case SND_SOC_DOBJ_DAI_LINK:
2815 remove_link(comp, dobj, pass);
2816 break;
David Brazdil0f672f62019-12-10 10:32:29 +00002817 case SND_SOC_DOBJ_BACKEND_LINK:
2818 /*
2819 * call link_unload ops if extra
2820 * deinitialization is needed.
2821 */
2822 remove_backend_link(comp, dobj, pass);
2823 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002824 default:
2825 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2826 dobj->type);
2827 break;
2828 }
2829 }
2830 pass--;
2831 }
2832
2833 /* let caller know if FW can be freed when no objects are left */
2834 return !list_empty(&comp->dobj_list);
2835}
2836EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);