1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
33#define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341
34
35
36
37
38
39
40
41#define SOC_TPLG_PASS_MANIFEST 0
42#define SOC_TPLG_PASS_VENDOR 1
43#define SOC_TPLG_PASS_CONTROL 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_BE_DAI 6
48#define SOC_TPLG_PASS_LINK 7
49
50#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
51#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
52
53
54struct soc_tplg {
55 const struct firmware *fw;
56
57
58 const u8 *pos;
59 const u8 *hdr_pos;
60 unsigned int pass;
61
62
63 struct device *dev;
64 struct snd_soc_component *comp;
65 u32 index;
66
67
68 const struct snd_soc_tplg_kcontrol_ops *io_ops;
69 int io_ops_count;
70
71
72 const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
73 int bytes_ext_ops_count;
74
75
76 const struct snd_soc_tplg_ops *ops;
77};
78
79
80static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
81 unsigned int count, size_t bytes, const char *elem_type)
82{
83 const u8 *end = tplg->pos + elem_size * count;
84
85 if (end > tplg->fw->data + tplg->fw->size) {
86 dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
87 elem_type);
88 return -EINVAL;
89 }
90
91
92
93 if (elem_size * count > bytes) {
94 dev_err(tplg->dev,
95 "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
96 elem_type, count, elem_size, bytes);
97 return -EINVAL;
98 }
99
100 return 0;
101}
102
103static inline bool soc_tplg_is_eof(struct soc_tplg *tplg)
104{
105 const u8 *end = tplg->hdr_pos;
106
107 if (end >= tplg->fw->data + tplg->fw->size)
108 return true;
109 return false;
110}
111
112static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
113{
114 return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
115}
116
117static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
118{
119 return (unsigned long)(tplg->pos - tplg->fw->data);
120}
121
122
123static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
124 {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
125 snd_soc_put_volsw, snd_soc_info_volsw},
126 {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
127 snd_soc_put_volsw_sx, NULL},
128 {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
129 snd_soc_put_enum_double, snd_soc_info_enum_double},
130 {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
131 snd_soc_put_enum_double, NULL},
132 {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
133 snd_soc_bytes_put, snd_soc_bytes_info},
134 {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw,
135 snd_soc_put_volsw, snd_soc_info_volsw},
136 {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
137 snd_soc_put_xr_sx, snd_soc_info_xr_sx},
138 {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
139 snd_soc_put_strobe, NULL},
140 {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
141 snd_soc_dapm_put_volsw, snd_soc_info_volsw},
142 {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
143 snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
144 {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
145 snd_soc_dapm_put_enum_double, NULL},
146 {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
147 snd_soc_dapm_put_enum_double, NULL},
148 {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
149 snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
150};
151
152struct soc_tplg_map {
153 int uid;
154 int kid;
155};
156
157
158static const struct soc_tplg_map dapm_map[] = {
159 {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
160 {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
161 {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
162 {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
163 {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
164 {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
165 {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
166 {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
167 {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
168 {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
169 {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
170 {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
171 {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
172 {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
173 {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
174 {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
175 {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
176 {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
177 {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
178 {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
179 {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
180 {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
181 {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
182 {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
183};
184
185static int tplg_chan_get_reg(struct soc_tplg *tplg,
186 struct snd_soc_tplg_channel *chan, int map)
187{
188 int i;
189
190 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
191 if (le32_to_cpu(chan[i].id) == map)
192 return le32_to_cpu(chan[i].reg);
193 }
194
195 return -EINVAL;
196}
197
198static int tplg_chan_get_shift(struct soc_tplg *tplg,
199 struct snd_soc_tplg_channel *chan, int map)
200{
201 int i;
202
203 for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
204 if (le32_to_cpu(chan[i].id) == map)
205 return le32_to_cpu(chan[i].shift);
206 }
207
208 return -EINVAL;
209}
210
211static int get_widget_id(int tplg_type)
212{
213 int i;
214
215 for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
216 if (tplg_type == dapm_map[i].uid)
217 return dapm_map[i].kid;
218 }
219
220 return -EINVAL;
221}
222
223static inline void soc_control_err(struct soc_tplg *tplg,
224 struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
225{
226 dev_err(tplg->dev,
227 "ASoC: no complete control IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
228 name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
229 soc_tplg_get_offset(tplg));
230}
231
232
233static int soc_tplg_vendor_load(struct soc_tplg *tplg,
234 struct snd_soc_tplg_hdr *hdr)
235{
236 int ret = 0;
237
238 if (tplg->ops && tplg->ops->vendor_load)
239 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
240 else {
241 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
242 hdr->vendor_type);
243 return -EINVAL;
244 }
245
246 if (ret < 0)
247 dev_err(tplg->dev,
248 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
249 soc_tplg_get_hdr_offset(tplg),
250 soc_tplg_get_hdr_offset(tplg),
251 hdr->type, hdr->vendor_type);
252 return ret;
253}
254
255
256
257static int soc_tplg_widget_load(struct soc_tplg *tplg,
258 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
259{
260 if (tplg->ops && tplg->ops->widget_load)
261 return tplg->ops->widget_load(tplg->comp, tplg->index, w,
262 tplg_w);
263
264 return 0;
265}
266
267
268
269static int soc_tplg_widget_ready(struct soc_tplg *tplg,
270 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
271{
272 if (tplg->ops && tplg->ops->widget_ready)
273 return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
274 tplg_w);
275
276 return 0;
277}
278
279
280static int soc_tplg_dai_load(struct soc_tplg *tplg,
281 struct snd_soc_dai_driver *dai_drv,
282 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
283{
284 if (tplg->ops && tplg->ops->dai_load)
285 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
286 pcm, dai);
287
288 return 0;
289}
290
291
292static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
293 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
294{
295 if (tplg->ops && tplg->ops->link_load)
296 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
297
298 return 0;
299}
300
301
302static int soc_tplg_complete(struct soc_tplg *tplg)
303{
304 if (tplg->ops && tplg->ops->complete)
305 return tplg->ops->complete(tplg->comp);
306
307 return 0;
308}
309
310
311static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
312 const struct snd_kcontrol_new *control_new, const char *prefix,
313 void *data, struct snd_kcontrol **kcontrol)
314{
315 int err;
316
317 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
318 if (*kcontrol == NULL) {
319 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
320 control_new->name);
321 return -ENOMEM;
322 }
323
324 err = snd_ctl_add(card, *kcontrol);
325 if (err < 0) {
326 dev_err(dev, "ASoC: Failed to add %s: %d\n",
327 control_new->name, err);
328 return err;
329 }
330
331 return 0;
332}
333
334
335static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
336 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
337{
338 struct snd_soc_component *comp = tplg->comp;
339
340 return soc_tplg_add_dcontrol(comp->card->snd_card,
341 tplg->dev, k, comp->name_prefix, comp, kcontrol);
342}
343
344
345static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj,
346 int pass)
347{
348 struct snd_card *card = comp->card->snd_card;
349
350 if (pass != SOC_TPLG_PASS_CONTROL)
351 return;
352
353 if (dobj->unload)
354 dobj->unload(comp, dobj);
355
356 snd_ctl_remove(card, dobj->control.kcontrol);
357 list_del(&dobj->list);
358}
359
360
361static void soc_tplg_remove_route(struct snd_soc_component *comp,
362 struct snd_soc_dobj *dobj, int pass)
363{
364 if (pass != SOC_TPLG_PASS_GRAPH)
365 return;
366
367 if (dobj->unload)
368 dobj->unload(comp, dobj);
369
370 list_del(&dobj->list);
371}
372
373
374static void soc_tplg_remove_widget(struct snd_soc_component *comp,
375 struct snd_soc_dobj *dobj, int pass)
376{
377 struct snd_card *card = comp->card->snd_card;
378 struct snd_soc_dapm_widget *w =
379 container_of(dobj, struct snd_soc_dapm_widget, dobj);
380 int i;
381
382 if (pass != SOC_TPLG_PASS_WIDGET)
383 return;
384
385 if (dobj->unload)
386 dobj->unload(comp, dobj);
387
388 if (w->kcontrols)
389 for (i = 0; i < w->num_kcontrols; i++)
390 snd_ctl_remove(card, w->kcontrols[i]);
391
392 list_del(&dobj->list);
393
394
395}
396
397
398static void soc_tplg_remove_dai(struct snd_soc_component *comp,
399 struct snd_soc_dobj *dobj, int pass)
400{
401 struct snd_soc_dai_driver *dai_drv =
402 container_of(dobj, struct snd_soc_dai_driver, dobj);
403 struct snd_soc_dai *dai, *_dai;
404
405 if (pass != SOC_TPLG_PASS_PCM_DAI)
406 return;
407
408 if (dobj->unload)
409 dobj->unload(comp, dobj);
410
411 for_each_component_dais_safe(comp, dai, _dai)
412 if (dai->driver == dai_drv)
413 snd_soc_unregister_dai(dai);
414
415 list_del(&dobj->list);
416}
417
418
419static void soc_tplg_remove_link(struct snd_soc_component *comp,
420 struct snd_soc_dobj *dobj, int pass)
421{
422 struct snd_soc_dai_link *link =
423 container_of(dobj, struct snd_soc_dai_link, dobj);
424
425 if (pass != SOC_TPLG_PASS_PCM_DAI)
426 return;
427
428 if (dobj->unload)
429 dobj->unload(comp, dobj);
430
431 list_del(&dobj->list);
432
433
434 if (!link->ignore)
435 snd_soc_remove_pcm_runtime(comp->card,
436 snd_soc_get_pcm_runtime(comp->card, link));
437}
438
439
440static void remove_backend_link(struct snd_soc_component *comp,
441 struct snd_soc_dobj *dobj, int pass)
442{
443 if (pass != SOC_TPLG_PASS_LINK)
444 return;
445
446 if (dobj->unload)
447 dobj->unload(comp, dobj);
448
449
450
451
452
453
454 dobj->type = SND_SOC_DOBJ_NONE;
455 list_del(&dobj->list);
456}
457
458
459static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
460 struct snd_kcontrol_new *k,
461 const struct soc_tplg *tplg)
462{
463 const struct snd_soc_tplg_kcontrol_ops *ops;
464 const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
465 int num_ops, i;
466
467 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
468 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
469 && (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ
470 || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
471 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
472 struct soc_bytes_ext *sbe;
473 struct snd_soc_tplg_bytes_control *be;
474
475 sbe = (struct soc_bytes_ext *)k->private_value;
476 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
477
478
479
480
481 k->info = snd_soc_bytes_info_ext;
482 k->tlv.c = snd_soc_bytes_tlv_callback;
483
484
485
486
487
488
489
490
491
492 if (sbe->max > 512)
493 k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
494
495 ext_ops = tplg->bytes_ext_ops;
496 num_ops = tplg->bytes_ext_ops_count;
497 for (i = 0; i < num_ops; i++) {
498 if (!sbe->put &&
499 ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
500 sbe->put = ext_ops[i].put;
501 if (!sbe->get &&
502 ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
503 sbe->get = ext_ops[i].get;
504 }
505
506 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
507 return -EINVAL;
508 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
509 return -EINVAL;
510 return 0;
511 }
512
513
514 ops = tplg->io_ops;
515 num_ops = tplg->io_ops_count;
516 for (i = 0; i < num_ops; i++) {
517
518 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
519 k->put = ops[i].put;
520 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
521 k->get = ops[i].get;
522 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
523 k->info = ops[i].info;
524 }
525
526
527 if (k->put && k->get && k->info)
528 return 0;
529
530
531 ops = io_ops;
532 num_ops = ARRAY_SIZE(io_ops);
533 for (i = 0; i < num_ops; i++) {
534
535 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
536 k->put = ops[i].put;
537 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
538 k->get = ops[i].get;
539 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
540 k->info = ops[i].info;
541 }
542
543
544 if (k->put && k->get && k->info)
545 return 0;
546
547
548 return -EINVAL;
549}
550
551
552int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
553 const struct snd_soc_tplg_widget_events *events,
554 int num_events, u16 event_type)
555{
556 int i;
557
558 w->event = NULL;
559
560 for (i = 0; i < num_events; i++) {
561 if (event_type == events[i].type) {
562
563
564 w->event = events[i].event_handler;
565 return 0;
566 }
567 }
568
569
570 return -EINVAL;
571}
572EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
573
574
575static int soc_tplg_control_load(struct soc_tplg *tplg,
576 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
577{
578 int ret = 0;
579
580 if (tplg->ops && tplg->ops->control_load)
581 ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr);
582
583 if (ret)
584 dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name);
585
586 return ret;
587}
588
589
590static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
591 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
592{
593 unsigned int item_len = 2 * sizeof(unsigned int);
594 unsigned int *p;
595
596 p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
597 if (!p)
598 return -ENOMEM;
599
600 p[0] = SNDRV_CTL_TLVT_DB_SCALE;
601 p[1] = item_len;
602 p[2] = le32_to_cpu(scale->min);
603 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
604 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
605
606 kc->tlv.p = (void *)p;
607 return 0;
608}
609
610static int soc_tplg_create_tlv(struct soc_tplg *tplg,
611 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
612{
613 struct snd_soc_tplg_ctl_tlv *tplg_tlv;
614 u32 access = le32_to_cpu(tc->access);
615
616 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
617 return 0;
618
619 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
620 tplg_tlv = &tc->tlv;
621 switch (le32_to_cpu(tplg_tlv->type)) {
622 case SNDRV_CTL_TLVT_DB_SCALE:
623 return soc_tplg_create_tlv_db_scale(tplg, kc,
624 &tplg_tlv->scale);
625
626
627 default:
628 dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
629 tplg_tlv->type);
630 return -EINVAL;
631 }
632 }
633
634 return 0;
635}
636
637static int soc_tplg_control_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
638{
639 struct snd_soc_tplg_mixer_control *mc;
640 struct soc_mixer_control *sm;
641 int err;
642
643 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
644
645
646 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
647 return -EINVAL;
648
649 sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
650 if (!sm)
651 return -ENOMEM;
652
653 tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) + le32_to_cpu(mc->priv.size);
654
655 dev_dbg(tplg->dev, "ASoC: adding mixer kcontrol %s with access 0x%x\n",
656 mc->hdr.name, mc->hdr.access);
657
658 kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL);
659 if (!kc->name)
660 return -ENOMEM;
661 kc->private_value = (long)sm;
662 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
663 kc->access = le32_to_cpu(mc->hdr.access);
664
665
666 sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL);
667 sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR);
668 sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL);
669 sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR);
670
671 sm->max = le32_to_cpu(mc->max);
672 sm->min = le32_to_cpu(mc->min);
673 sm->invert = le32_to_cpu(mc->invert);
674 sm->platform_max = le32_to_cpu(mc->platform_max);
675 sm->num_channels = le32_to_cpu(mc->num_channels);
676
677
678 err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg);
679 if (err) {
680 soc_control_err(tplg, &mc->hdr, mc->hdr.name);
681 return err;
682 }
683
684
685 err = soc_tplg_create_tlv(tplg, kc, &mc->hdr);
686 if (err < 0) {
687 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name);
688 return err;
689 }
690
691
692 return soc_tplg_control_load(tplg, kc, &mc->hdr);
693}
694
695static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se,
696 struct snd_soc_tplg_enum_control *ec)
697{
698 int i, ret;
699
700 if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts))
701 return -EINVAL;
702
703 se->dobj.control.dtexts =
704 devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
705 if (se->dobj.control.dtexts == NULL)
706 return -ENOMEM;
707
708 for (i = 0; i < le32_to_cpu(ec->items); i++) {
709
710 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
711 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
712 ret = -EINVAL;
713 goto err;
714 }
715
716 se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL);
717 if (!se->dobj.control.dtexts[i]) {
718 ret = -ENOMEM;
719 goto err;
720 }
721 }
722
723 se->items = le32_to_cpu(ec->items);
724 se->texts = (const char * const *)se->dobj.control.dtexts;
725 return 0;
726
727err:
728 return ret;
729}
730
731static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se,
732 struct snd_soc_tplg_enum_control *ec)
733{
734 int i;
735
736
737
738
739
740
741
742 if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS)
743 return -EINVAL;
744
745 se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items),
746 sizeof(*se->dobj.control.dvalues),
747 GFP_KERNEL);
748 if (!se->dobj.control.dvalues)
749 return -ENOMEM;
750
751
752 for (i = 0; i < le32_to_cpu(ec->items); i++) {
753 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
754 }
755
756 se->items = le32_to_cpu(ec->items);
757 se->values = (const unsigned int *)se->dobj.control.dvalues;
758 return 0;
759}
760
761static int soc_tplg_control_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
762{
763 struct snd_soc_tplg_enum_control *ec;
764 struct soc_enum *se;
765 int err;
766
767 ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
768
769
770 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
771 return -EINVAL;
772
773 se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL);
774 if (!se)
775 return -ENOMEM;
776
777 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + le32_to_cpu(ec->priv.size));
778
779 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", ec->hdr.name, ec->items);
780
781 kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL);
782 if (!kc->name)
783 return -ENOMEM;
784 kc->private_value = (long)se;
785 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
786 kc->access = le32_to_cpu(ec->hdr.access);
787
788
789 se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
790 se->shift_l = tplg_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
791 se->shift_r = tplg_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
792
793 se->mask = le32_to_cpu(ec->mask);
794
795 switch (le32_to_cpu(ec->hdr.ops.info)) {
796 case SND_SOC_TPLG_CTL_ENUM_VALUE:
797 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
798 err = soc_tplg_denum_create_values(tplg, se, ec);
799 if (err < 0) {
800 dev_err(tplg->dev, "ASoC: could not create values for %s\n", ec->hdr.name);
801 return err;
802 }
803 fallthrough;
804 case SND_SOC_TPLG_CTL_ENUM:
805 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
806 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
807 err = soc_tplg_denum_create_texts(tplg, se, ec);
808 if (err < 0) {
809 dev_err(tplg->dev, "ASoC: could not create texts for %s\n", ec->hdr.name);
810 return err;
811 }
812 break;
813 default:
814 dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
815 ec->hdr.ops.info, ec->hdr.name);
816 return -EINVAL;
817 }
818
819
820 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
821 if (err) {
822 soc_control_err(tplg, &ec->hdr, ec->hdr.name);
823 return err;
824 }
825
826
827 return soc_tplg_control_load(tplg, kc, &ec->hdr);
828}
829
830static int soc_tplg_control_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc)
831{
832 struct snd_soc_tplg_bytes_control *be;
833 struct soc_bytes_ext *sbe;
834 int err;
835
836 be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
837
838
839 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
840 return -EINVAL;
841
842 sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
843 if (!sbe)
844 return -ENOMEM;
845
846 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + le32_to_cpu(be->priv.size));
847
848 dev_dbg(tplg->dev, "ASoC: adding bytes kcontrol %s with access 0x%x\n",
849 be->hdr.name, be->hdr.access);
850
851 kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL);
852 if (!kc->name)
853 return -ENOMEM;
854 kc->private_value = (long)sbe;
855 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
856 kc->access = le32_to_cpu(be->hdr.access);
857
858 sbe->max = le32_to_cpu(be->max);
859
860
861 err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg);
862 if (err) {
863 soc_control_err(tplg, &be->hdr, be->hdr.name);
864 return err;
865 }
866
867
868 return soc_tplg_control_load(tplg, kc, &be->hdr);
869}
870
871static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size)
872{
873 struct snd_kcontrol_new kc = {0};
874 struct soc_bytes_ext *sbe;
875 int ret;
876
877 if (soc_tplg_check_elem_count(tplg,
878 sizeof(struct snd_soc_tplg_bytes_control),
879 1, size, "mixer bytes"))
880 return -EINVAL;
881
882 ret = soc_tplg_control_dbytes_create(tplg, &kc);
883 if (ret)
884 return ret;
885
886
887 sbe = (struct soc_bytes_ext *)kc.private_value;
888
889 INIT_LIST_HEAD(&sbe->dobj.list);
890 sbe->dobj.type = SND_SOC_DOBJ_BYTES;
891 sbe->dobj.index = tplg->index;
892 if (tplg->ops)
893 sbe->dobj.unload = tplg->ops->control_unload;
894
895
896 ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol);
897 if (ret < 0)
898 return ret;
899
900 list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
901
902 return ret;
903}
904
905static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size)
906{
907 struct snd_kcontrol_new kc = {0};
908 struct soc_mixer_control *sm;
909 int ret;
910
911 if (soc_tplg_check_elem_count(tplg,
912 sizeof(struct snd_soc_tplg_mixer_control),
913 1, size, "mixers"))
914 return -EINVAL;
915
916 ret = soc_tplg_control_dmixer_create(tplg, &kc);
917 if (ret)
918 return ret;
919
920
921 sm = (struct soc_mixer_control *)kc.private_value;
922
923 INIT_LIST_HEAD(&sm->dobj.list);
924 sm->dobj.type = SND_SOC_DOBJ_MIXER;
925 sm->dobj.index = tplg->index;
926 if (tplg->ops)
927 sm->dobj.unload = tplg->ops->control_unload;
928
929
930 ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol);
931 if (ret < 0)
932 return ret;
933
934 list_add(&sm->dobj.list, &tplg->comp->dobj_list);
935
936 return ret;
937}
938
939static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
940{
941 struct snd_kcontrol_new kc = {0};
942 struct soc_enum *se;
943 int ret;
944
945 if (soc_tplg_check_elem_count(tplg,
946 sizeof(struct snd_soc_tplg_enum_control),
947 1, size, "enums"))
948 return -EINVAL;
949
950 ret = soc_tplg_control_denum_create(tplg, &kc);
951 if (ret)
952 return ret;
953
954
955 se = (struct soc_enum *)kc.private_value;
956
957 INIT_LIST_HEAD(&se->dobj.list);
958 se->dobj.type = SND_SOC_DOBJ_ENUM;
959 se->dobj.index = tplg->index;
960 if (tplg->ops)
961 se->dobj.unload = tplg->ops->control_unload;
962
963
964 ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
965 if (ret < 0)
966 return ret;
967
968 list_add(&se->dobj.list, &tplg->comp->dobj_list);
969
970 return ret;
971}
972
973static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
974 struct snd_soc_tplg_hdr *hdr)
975{
976 int ret;
977 int i;
978
979 dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
980 soc_tplg_get_offset(tplg));
981
982 for (i = 0; i < le32_to_cpu(hdr->count); i++) {
983 struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
984
985 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
986 dev_err(tplg->dev, "ASoC: invalid control size\n");
987 return -EINVAL;
988 }
989
990 switch (le32_to_cpu(control_hdr->type)) {
991 case SND_SOC_TPLG_TYPE_MIXER:
992 ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
993 break;
994 case SND_SOC_TPLG_TYPE_ENUM:
995 ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
996 break;
997 case SND_SOC_TPLG_TYPE_BYTES:
998 ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
999 break;
1000 default:
1001 ret = -EINVAL;
1002 break;
1003 }
1004
1005 if (ret < 0) {
1006 dev_err(tplg->dev, "ASoC: invalid control type: %d, index: %d at 0x%lx\n",
1007 control_hdr->type, i, soc_tplg_get_offset(tplg));
1008 return ret;
1009 }
1010 }
1011
1012 return 0;
1013}
1014
1015
1016static int soc_tplg_add_route(struct soc_tplg *tplg,
1017 struct snd_soc_dapm_route *route)
1018{
1019 if (tplg->ops && tplg->ops->dapm_route_load)
1020 return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
1021 route);
1022
1023 return 0;
1024}
1025
1026static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
1027 struct snd_soc_tplg_hdr *hdr)
1028{
1029 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1030 const size_t maxlen = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
1031 struct snd_soc_tplg_dapm_graph_elem *elem;
1032 struct snd_soc_dapm_route *route;
1033 int count, i;
1034 int ret = 0;
1035
1036 count = le32_to_cpu(hdr->count);
1037
1038 if (soc_tplg_check_elem_count(tplg,
1039 sizeof(struct snd_soc_tplg_dapm_graph_elem),
1040 count, le32_to_cpu(hdr->payload_size), "graph"))
1041 return -EINVAL;
1042
1043 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
1044 hdr->index);
1045
1046 for (i = 0; i < count; i++) {
1047 route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL);
1048 if (!route)
1049 return -ENOMEM;
1050 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
1051 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
1052
1053
1054 if ((strnlen(elem->source, maxlen) == maxlen) ||
1055 (strnlen(elem->sink, maxlen) == maxlen) ||
1056 (strnlen(elem->control, maxlen) == maxlen)) {
1057 ret = -EINVAL;
1058 break;
1059 }
1060
1061 route->source = devm_kstrdup(tplg->dev, elem->source, GFP_KERNEL);
1062 route->sink = devm_kstrdup(tplg->dev, elem->sink, GFP_KERNEL);
1063 if (!route->source || !route->sink) {
1064 ret = -ENOMEM;
1065 break;
1066 }
1067
1068 if (strnlen(elem->control, maxlen) != 0) {
1069 route->control = devm_kstrdup(tplg->dev, elem->control, GFP_KERNEL);
1070 if (!route->control) {
1071 ret = -ENOMEM;
1072 break;
1073 }
1074 }
1075
1076
1077 route->dobj.type = SND_SOC_DOBJ_GRAPH;
1078 if (tplg->ops)
1079 route->dobj.unload = tplg->ops->dapm_route_unload;
1080 route->dobj.index = tplg->index;
1081 list_add(&route->dobj.list, &tplg->comp->dobj_list);
1082
1083 ret = soc_tplg_add_route(tplg, route);
1084 if (ret < 0) {
1085 dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
1086 break;
1087 }
1088
1089 ret = snd_soc_dapm_add_routes(dapm, route, 1);
1090 if (ret)
1091 break;
1092 }
1093
1094 return ret;
1095}
1096
1097static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
1098 struct snd_soc_tplg_dapm_widget *w)
1099{
1100 struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
1101 struct snd_soc_dapm_widget template, *widget;
1102 struct snd_soc_tplg_ctl_hdr *control_hdr;
1103 struct snd_soc_card *card = tplg->comp->card;
1104 unsigned int *kcontrol_type = NULL;
1105 struct snd_kcontrol_new *kc;
1106 int mixer_count = 0;
1107 int bytes_count = 0;
1108 int enum_count = 0;
1109 int ret = 0;
1110 int i;
1111
1112 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1113 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1114 return -EINVAL;
1115 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1116 SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1117 return -EINVAL;
1118
1119 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
1120 w->name, w->id);
1121
1122 memset(&template, 0, sizeof(template));
1123
1124
1125 template.id = get_widget_id(le32_to_cpu(w->id));
1126 if ((int)template.id < 0)
1127 return template.id;
1128
1129
1130 template.name = kstrdup(w->name, GFP_KERNEL);
1131 if (!template.name)
1132 return -ENOMEM;
1133 template.sname = kstrdup(w->sname, GFP_KERNEL);
1134 if (!template.sname) {
1135 ret = -ENOMEM;
1136 goto err;
1137 }
1138 template.reg = le32_to_cpu(w->reg);
1139 template.shift = le32_to_cpu(w->shift);
1140 template.mask = le32_to_cpu(w->mask);
1141 template.subseq = le32_to_cpu(w->subseq);
1142 template.on_val = w->invert ? 0 : 1;
1143 template.off_val = w->invert ? 1 : 0;
1144 template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
1145 template.event_flags = le16_to_cpu(w->event_flags);
1146 template.dobj.index = tplg->index;
1147
1148 tplg->pos +=
1149 (sizeof(struct snd_soc_tplg_dapm_widget) +
1150 le32_to_cpu(w->priv.size));
1151
1152 if (w->num_kcontrols == 0) {
1153 template.num_kcontrols = 0;
1154 goto widget;
1155 }
1156
1157 template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1158 kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL);
1159 if (!kc) {
1160 ret = -ENOMEM;
1161 goto hdr_err;
1162 }
1163
1164 kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int),
1165 GFP_KERNEL);
1166 if (!kcontrol_type) {
1167 ret = -ENOMEM;
1168 goto hdr_err;
1169 }
1170
1171 for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) {
1172 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
1173
1174 switch (le32_to_cpu(control_hdr->type)) {
1175 case SND_SOC_TPLG_TYPE_MIXER:
1176
1177 kc[i].index = mixer_count;
1178 kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER;
1179 mixer_count++;
1180 ret = soc_tplg_control_dmixer_create(tplg, &kc[i]);
1181 if (ret < 0)
1182 goto hdr_err;
1183 break;
1184 case SND_SOC_TPLG_TYPE_ENUM:
1185
1186 kc[i].index = enum_count;
1187 kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM;
1188 enum_count++;
1189 ret = soc_tplg_control_denum_create(tplg, &kc[i]);
1190 if (ret < 0)
1191 goto hdr_err;
1192 break;
1193 case SND_SOC_TPLG_TYPE_BYTES:
1194
1195 kc[i].index = bytes_count;
1196 kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES;
1197 bytes_count++;
1198 ret = soc_tplg_control_dbytes_create(tplg, &kc[i]);
1199 if (ret < 0)
1200 goto hdr_err;
1201 break;
1202 default:
1203 dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
1204 control_hdr->ops.get, control_hdr->ops.put,
1205 le32_to_cpu(control_hdr->ops.info));
1206 ret = -EINVAL;
1207 goto hdr_err;
1208 }
1209 }
1210
1211 template.kcontrol_news = kc;
1212 dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n",
1213 w->name, mixer_count, enum_count, bytes_count);
1214
1215widget:
1216 ret = soc_tplg_widget_load(tplg, &template, w);
1217 if (ret < 0)
1218 goto hdr_err;
1219
1220
1221
1222 if (snd_soc_card_is_instantiated(card))
1223 widget = snd_soc_dapm_new_control(dapm, &template);
1224 else
1225 widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1226 if (IS_ERR(widget)) {
1227 ret = PTR_ERR(widget);
1228 goto hdr_err;
1229 }
1230
1231 widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1232 widget->dobj.widget.kcontrol_type = kcontrol_type;
1233 if (tplg->ops)
1234 widget->dobj.unload = tplg->ops->widget_unload;
1235 widget->dobj.index = tplg->index;
1236 list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1237
1238 ret = soc_tplg_widget_ready(tplg, widget, w);
1239 if (ret < 0)
1240 goto ready_err;
1241
1242 kfree(template.sname);
1243 kfree(template.name);
1244
1245 return 0;
1246
1247ready_err:
1248 soc_tplg_remove_widget(widget->dapm->component, &widget->dobj, SOC_TPLG_PASS_WIDGET);
1249 snd_soc_dapm_free_widget(widget);
1250hdr_err:
1251 kfree(template.sname);
1252err:
1253 kfree(template.name);
1254 return ret;
1255}
1256
1257static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
1258 struct snd_soc_tplg_hdr *hdr)
1259{
1260 int count, i;
1261
1262 count = le32_to_cpu(hdr->count);
1263
1264 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
1265
1266 for (i = 0; i < count; i++) {
1267 struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1268 int ret;
1269
1270
1271
1272
1273
1274
1275 if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) {
1276 dev_err(tplg->dev, "ASoC: invalid widget data size\n");
1277 return -EINVAL;
1278 }
1279
1280
1281 if (le32_to_cpu(widget->size) != sizeof(*widget)) {
1282 dev_err(tplg->dev, "ASoC: invalid widget size\n");
1283 return -EINVAL;
1284 }
1285
1286
1287 if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) {
1288 dev_err(tplg->dev, "ASoC: invalid widget private data size\n");
1289 return -EINVAL;
1290 }
1291
1292 ret = soc_tplg_dapm_widget_create(tplg, widget);
1293 if (ret < 0) {
1294 dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
1295 widget->name);
1296 return ret;
1297 }
1298 }
1299
1300 return 0;
1301}
1302
1303static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
1304{
1305 struct snd_soc_card *card = tplg->comp->card;
1306 int ret;
1307
1308
1309
1310
1311 if (!snd_soc_card_is_instantiated(card)) {
1312 dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n");
1313 return 0;
1314 }
1315
1316 ret = snd_soc_dapm_new_widgets(card);
1317 if (ret < 0)
1318 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret);
1319
1320 return ret;
1321}
1322
1323static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream,
1324 struct snd_soc_tplg_stream_caps *caps)
1325{
1326 stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL);
1327 if (!stream->stream_name)
1328 return -ENOMEM;
1329
1330 stream->channels_min = le32_to_cpu(caps->channels_min);
1331 stream->channels_max = le32_to_cpu(caps->channels_max);
1332 stream->rates = le32_to_cpu(caps->rates);
1333 stream->rate_min = le32_to_cpu(caps->rate_min);
1334 stream->rate_max = le32_to_cpu(caps->rate_max);
1335 stream->formats = le64_to_cpu(caps->formats);
1336 stream->sig_bits = le32_to_cpu(caps->sig_bits);
1337
1338 return 0;
1339}
1340
1341static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
1342 unsigned int flag_mask, unsigned int flags)
1343{
1344 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
1345 dai_drv->symmetric_rate =
1346 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1347
1348 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
1349 dai_drv->symmetric_channels =
1350 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ?
1351 1 : 0;
1352
1353 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
1354 dai_drv->symmetric_sample_bits =
1355 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1356 1 : 0;
1357}
1358
1359static const struct snd_soc_dai_ops tplg_dai_ops = {
1360 .compress_new = snd_soc_new_compress,
1361};
1362
1363static int soc_tplg_dai_create(struct soc_tplg *tplg,
1364 struct snd_soc_tplg_pcm *pcm)
1365{
1366 struct snd_soc_dai_driver *dai_drv;
1367 struct snd_soc_pcm_stream *stream;
1368 struct snd_soc_tplg_stream_caps *caps;
1369 struct snd_soc_dai *dai;
1370 struct snd_soc_dapm_context *dapm =
1371 snd_soc_component_get_dapm(tplg->comp);
1372 int ret;
1373
1374 dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
1375 if (dai_drv == NULL)
1376 return -ENOMEM;
1377
1378 if (strlen(pcm->dai_name)) {
1379 dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1380 if (!dai_drv->name) {
1381 ret = -ENOMEM;
1382 goto err;
1383 }
1384 }
1385 dai_drv->id = le32_to_cpu(pcm->dai_id);
1386
1387 if (pcm->playback) {
1388 stream = &dai_drv->playback;
1389 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1390 ret = set_stream_info(tplg, stream, caps);
1391 if (ret < 0)
1392 goto err;
1393 }
1394
1395 if (pcm->capture) {
1396 stream = &dai_drv->capture;
1397 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1398 ret = set_stream_info(tplg, stream, caps);
1399 if (ret < 0)
1400 goto err;
1401 }
1402
1403 if (pcm->compress)
1404 dai_drv->ops = &tplg_dai_ops;
1405
1406
1407 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1408 if (ret < 0) {
1409 dev_err(tplg->dev, "ASoC: DAI loading failed\n");
1410 goto err;
1411 }
1412
1413 dai_drv->dobj.index = tplg->index;
1414 dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
1415 if (tplg->ops)
1416 dai_drv->dobj.unload = tplg->ops->dai_unload;
1417 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
1418
1419
1420 dai = snd_soc_register_dai(tplg->comp, dai_drv, false);
1421 if (!dai)
1422 return -ENOMEM;
1423
1424
1425 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1426 if (ret != 0) {
1427 dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
1428 snd_soc_unregister_dai(dai);
1429 return ret;
1430 }
1431
1432 return 0;
1433
1434err:
1435 return ret;
1436}
1437
1438static void set_link_flags(struct snd_soc_dai_link *link,
1439 unsigned int flag_mask, unsigned int flags)
1440{
1441 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
1442 link->symmetric_rate =
1443 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0;
1444
1445 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
1446 link->symmetric_channels =
1447 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ?
1448 1 : 0;
1449
1450 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
1451 link->symmetric_sample_bits =
1452 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ?
1453 1 : 0;
1454
1455 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
1456 link->ignore_suspend =
1457 (flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ?
1458 1 : 0;
1459}
1460
1461
1462static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1463 struct snd_soc_tplg_pcm *pcm)
1464{
1465 struct snd_soc_dai_link *link;
1466 struct snd_soc_dai_link_component *dlc;
1467 int ret;
1468
1469
1470 link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1471 if (link == NULL)
1472 return -ENOMEM;
1473
1474 dlc = (struct snd_soc_dai_link_component *)(link + 1);
1475
1476 link->cpus = &dlc[0];
1477 link->num_cpus = 1;
1478
1479 link->dobj.index = tplg->index;
1480 link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
1481 if (tplg->ops)
1482 link->dobj.unload = tplg->ops->link_unload;
1483
1484 if (strlen(pcm->pcm_name)) {
1485 link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1486 link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL);
1487 if (!link->name || !link->stream_name) {
1488 ret = -ENOMEM;
1489 goto err;
1490 }
1491 }
1492 link->id = le32_to_cpu(pcm->pcm_id);
1493
1494 if (strlen(pcm->dai_name)) {
1495 link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL);
1496 if (!link->cpus->dai_name) {
1497 ret = -ENOMEM;
1498 goto err;
1499 }
1500 }
1501
1502
1503
1504
1505
1506
1507 link->codecs = &dlc[1];
1508 link->codecs->name = "snd-soc-dummy";
1509 link->codecs->dai_name = "snd-soc-dummy-dai";
1510 link->num_codecs = 1;
1511
1512 link->platforms = &dlc[2];
1513 link->platforms->name = "snd-soc-dummy";
1514 link->num_platforms = 1;
1515
1516
1517 link->dynamic = 1;
1518 link->ignore_pmdown_time = 1;
1519 link->playback_only = le32_to_cpu(pcm->playback) && !le32_to_cpu(pcm->capture);
1520 link->capture_only = !le32_to_cpu(pcm->playback) && le32_to_cpu(pcm->capture);
1521 if (pcm->flag_mask)
1522 set_link_flags(link,
1523 le32_to_cpu(pcm->flag_mask),
1524 le32_to_cpu(pcm->flags));
1525
1526
1527 ret = soc_tplg_dai_link_load(tplg, link, NULL);
1528 if (ret < 0) {
1529 dev_err(tplg->dev, "ASoC: FE link loading failed\n");
1530 goto err;
1531 }
1532
1533 ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1);
1534 if (ret < 0) {
1535 if (ret != -EPROBE_DEFER)
1536 dev_err(tplg->dev, "ASoC: adding FE link failed\n");
1537 goto err;
1538 }
1539
1540 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1541
1542 return 0;
1543err:
1544 return ret;
1545}
1546
1547
1548static int soc_tplg_pcm_create(struct soc_tplg *tplg,
1549 struct snd_soc_tplg_pcm *pcm)
1550{
1551 int ret;
1552
1553 ret = soc_tplg_dai_create(tplg, pcm);
1554 if (ret < 0)
1555 return ret;
1556
1557 return soc_tplg_fe_link_create(tplg, pcm);
1558}
1559
1560static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
1561 struct snd_soc_tplg_hdr *hdr)
1562{
1563 struct snd_soc_tplg_pcm *pcm;
1564 int count;
1565 int size;
1566 int i;
1567 int ret;
1568
1569 count = le32_to_cpu(hdr->count);
1570
1571
1572 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1573 size = le32_to_cpu(pcm->size);
1574 if (size > sizeof(struct snd_soc_tplg_pcm)) {
1575 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
1576 size);
1577 return -EINVAL;
1578 }
1579
1580 if (soc_tplg_check_elem_count(tplg,
1581 size, count,
1582 le32_to_cpu(hdr->payload_size),
1583 "PCM DAI"))
1584 return -EINVAL;
1585
1586 for (i = 0; i < count; i++) {
1587 pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
1588 size = le32_to_cpu(pcm->size);
1589
1590
1591
1592
1593 if (size != sizeof(*pcm))
1594 return -EINVAL;
1595
1596
1597 ret = soc_tplg_pcm_create(tplg, pcm);
1598 if (ret < 0)
1599 return ret;
1600
1601
1602
1603
1604 tplg->pos += size + le32_to_cpu(pcm->priv.size);
1605 }
1606
1607 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
1608
1609 return 0;
1610}
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621static void set_link_hw_format(struct snd_soc_dai_link *link,
1622 struct snd_soc_tplg_link_config *cfg)
1623{
1624 struct snd_soc_tplg_hw_config *hw_config;
1625 unsigned char bclk_provider, fsync_provider;
1626 unsigned char invert_bclk, invert_fsync;
1627 int i;
1628
1629 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
1630 hw_config = &cfg->hw_config[i];
1631 if (hw_config->id != cfg->default_hw_config_id)
1632 continue;
1633
1634 link->dai_fmt = le32_to_cpu(hw_config->fmt) &
1635 SND_SOC_DAIFMT_FORMAT_MASK;
1636
1637
1638 switch (hw_config->clock_gated) {
1639 case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
1640 link->dai_fmt |= SND_SOC_DAIFMT_GATED;
1641 break;
1642
1643 case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
1644 link->dai_fmt |= SND_SOC_DAIFMT_CONT;
1645 break;
1646
1647 default:
1648
1649 break;
1650 }
1651
1652
1653 invert_bclk = hw_config->invert_bclk;
1654 invert_fsync = hw_config->invert_fsync;
1655 if (!invert_bclk && !invert_fsync)
1656 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
1657 else if (!invert_bclk && invert_fsync)
1658 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
1659 else if (invert_bclk && !invert_fsync)
1660 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
1661 else
1662 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
1663
1664
1665 bclk_provider = (hw_config->bclk_provider ==
1666 SND_SOC_TPLG_BCLK_CP);
1667 fsync_provider = (hw_config->fsync_provider ==
1668 SND_SOC_TPLG_FSYNC_CP);
1669 if (bclk_provider && fsync_provider)
1670 link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
1671 else if (!bclk_provider && fsync_provider)
1672 link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP;
1673 else if (bclk_provider && !fsync_provider)
1674 link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC;
1675 else
1676 link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
1677 }
1678}
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
1696 int id, const char *name,
1697 const char *stream_name)
1698{
1699 struct snd_soc_pcm_runtime *rtd;
1700
1701 for_each_card_rtds(card, rtd) {
1702 struct snd_soc_dai_link *link = rtd->dai_link;
1703
1704 if (link->id != id)
1705 continue;
1706
1707 if (name && (!link->name || !strstr(link->name, name)))
1708 continue;
1709
1710 if (stream_name && (!link->stream_name ||
1711 !strstr(link->stream_name, stream_name)))
1712 continue;
1713
1714 return link;
1715 }
1716
1717 return NULL;
1718}
1719
1720
1721static int soc_tplg_link_config(struct soc_tplg *tplg,
1722 struct snd_soc_tplg_link_config *cfg)
1723{
1724 struct snd_soc_dai_link *link;
1725 const char *name, *stream_name;
1726 size_t len;
1727 int ret;
1728
1729 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1730 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1731 return -EINVAL;
1732 else if (len)
1733 name = cfg->name;
1734 else
1735 name = NULL;
1736
1737 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
1738 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1739 return -EINVAL;
1740 else if (len)
1741 stream_name = cfg->stream_name;
1742 else
1743 stream_name = NULL;
1744
1745 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
1746 name, stream_name);
1747 if (!link) {
1748 dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
1749 name, cfg->id);
1750 return -EINVAL;
1751 }
1752
1753
1754 if (cfg->num_hw_configs)
1755 set_link_hw_format(link, cfg);
1756
1757
1758 if (cfg->flag_mask)
1759 set_link_flags(link,
1760 le32_to_cpu(cfg->flag_mask),
1761 le32_to_cpu(cfg->flags));
1762
1763
1764 ret = soc_tplg_dai_link_load(tplg, link, cfg);
1765 if (ret < 0) {
1766 dev_err(tplg->dev, "ASoC: physical link loading failed\n");
1767 return ret;
1768 }
1769
1770
1771 link->dobj.index = tplg->index;
1772 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
1773 if (tplg->ops)
1774 link->dobj.unload = tplg->ops->link_unload;
1775 list_add(&link->dobj.list, &tplg->comp->dobj_list);
1776
1777 return 0;
1778}
1779
1780
1781
1782static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
1783 struct snd_soc_tplg_hdr *hdr)
1784{
1785 struct snd_soc_tplg_link_config *link;
1786 int count;
1787 int size;
1788 int i, ret;
1789
1790 count = le32_to_cpu(hdr->count);
1791
1792
1793 link = (struct snd_soc_tplg_link_config *)tplg->pos;
1794 size = le32_to_cpu(link->size);
1795 if (size > sizeof(struct snd_soc_tplg_link_config)) {
1796 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
1797 size);
1798 return -EINVAL;
1799 }
1800
1801 if (soc_tplg_check_elem_count(tplg, size, count,
1802 le32_to_cpu(hdr->payload_size),
1803 "physical link config"))
1804 return -EINVAL;
1805
1806
1807 for (i = 0; i < count; i++) {
1808 link = (struct snd_soc_tplg_link_config *)tplg->pos;
1809 size = le32_to_cpu(link->size);
1810 if (size != sizeof(*link))
1811 return -EINVAL;
1812
1813 ret = soc_tplg_link_config(tplg, link);
1814 if (ret < 0)
1815 return ret;
1816
1817
1818
1819
1820 tplg->pos += size + le32_to_cpu(link->priv.size);
1821 }
1822
1823 return 0;
1824}
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834static int soc_tplg_dai_config(struct soc_tplg *tplg,
1835 struct snd_soc_tplg_dai *d)
1836{
1837 struct snd_soc_dai_link_component dai_component;
1838 struct snd_soc_dai *dai;
1839 struct snd_soc_dai_driver *dai_drv;
1840 struct snd_soc_pcm_stream *stream;
1841 struct snd_soc_tplg_stream_caps *caps;
1842 int ret;
1843
1844 memset(&dai_component, 0, sizeof(dai_component));
1845
1846 dai_component.dai_name = d->dai_name;
1847 dai = snd_soc_find_dai(&dai_component);
1848 if (!dai) {
1849 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
1850 d->dai_name);
1851 return -EINVAL;
1852 }
1853
1854 if (le32_to_cpu(d->dai_id) != dai->id) {
1855 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
1856 d->dai_name);
1857 return -EINVAL;
1858 }
1859
1860 dai_drv = dai->driver;
1861 if (!dai_drv)
1862 return -EINVAL;
1863
1864 if (d->playback) {
1865 stream = &dai_drv->playback;
1866 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1867 ret = set_stream_info(tplg, stream, caps);
1868 if (ret < 0)
1869 return ret;
1870 }
1871
1872 if (d->capture) {
1873 stream = &dai_drv->capture;
1874 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1875 ret = set_stream_info(tplg, stream, caps);
1876 if (ret < 0)
1877 return ret;
1878 }
1879
1880 if (d->flag_mask)
1881 set_dai_flags(dai_drv,
1882 le32_to_cpu(d->flag_mask),
1883 le32_to_cpu(d->flags));
1884
1885
1886 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
1887 if (ret < 0) {
1888 dev_err(tplg->dev, "ASoC: DAI loading failed\n");
1889 return ret;
1890 }
1891
1892 return 0;
1893}
1894
1895
1896static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
1897 struct snd_soc_tplg_hdr *hdr)
1898{
1899 int count;
1900 int i;
1901
1902 count = le32_to_cpu(hdr->count);
1903
1904
1905 for (i = 0; i < count; i++) {
1906 struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos;
1907 int ret;
1908
1909 if (le32_to_cpu(dai->size) != sizeof(*dai)) {
1910 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
1911 return -EINVAL;
1912 }
1913
1914 ret = soc_tplg_dai_config(tplg, dai);
1915 if (ret < 0) {
1916 dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
1917 return ret;
1918 }
1919
1920 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
1921 }
1922
1923 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
1924 return 0;
1925}
1926
1927static int soc_tplg_manifest_load(struct soc_tplg *tplg,
1928 struct snd_soc_tplg_hdr *hdr)
1929{
1930 struct snd_soc_tplg_manifest *manifest;
1931 int ret = 0;
1932
1933 manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
1934
1935
1936 if (le32_to_cpu(manifest->size) != sizeof(*manifest))
1937 return -EINVAL;
1938
1939
1940 if (tplg->ops && tplg->ops->manifest)
1941 ret = tplg->ops->manifest(tplg->comp, tplg->index, manifest);
1942
1943 return ret;
1944}
1945
1946
1947static int soc_tplg_valid_header(struct soc_tplg *tplg,
1948 struct snd_soc_tplg_hdr *hdr)
1949{
1950 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
1951 dev_err(tplg->dev,
1952 "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
1953 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
1954 tplg->fw->size);
1955 return -EINVAL;
1956 }
1957
1958 if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) {
1959 dev_err(tplg->dev,
1960 "ASoC: invalid header of type %d at offset %ld payload_size %d\n",
1961 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
1962 hdr->payload_size);
1963 return -EINVAL;
1964 }
1965
1966
1967 if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
1968 dev_err(tplg->dev,
1969 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
1970 tplg->pass, hdr->magic,
1971 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1972 return -EINVAL;
1973 }
1974
1975 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
1976 dev_err(tplg->dev,
1977 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
1978 tplg->pass, hdr->magic,
1979 soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
1980 return -EINVAL;
1981 }
1982
1983
1984 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
1985 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
1986 dev_err(tplg->dev,
1987 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
1988 tplg->pass, hdr->abi,
1989 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
1990 tplg->fw->size);
1991 return -EINVAL;
1992 }
1993
1994 if (hdr->payload_size == 0) {
1995 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
1996 soc_tplg_get_hdr_offset(tplg));
1997 return -EINVAL;
1998 }
1999
2000 return 0;
2001}
2002
2003
2004static int soc_tplg_load_header(struct soc_tplg *tplg,
2005 struct snd_soc_tplg_hdr *hdr)
2006{
2007 int (*elem_load)(struct soc_tplg *tplg,
2008 struct snd_soc_tplg_hdr *hdr);
2009 unsigned int hdr_pass;
2010
2011 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
2012
2013 tplg->index = le32_to_cpu(hdr->index);
2014
2015 switch (le32_to_cpu(hdr->type)) {
2016 case SND_SOC_TPLG_TYPE_MIXER:
2017 case SND_SOC_TPLG_TYPE_ENUM:
2018 case SND_SOC_TPLG_TYPE_BYTES:
2019 hdr_pass = SOC_TPLG_PASS_CONTROL;
2020 elem_load = soc_tplg_kcontrol_elems_load;
2021 break;
2022 case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2023 hdr_pass = SOC_TPLG_PASS_GRAPH;
2024 elem_load = soc_tplg_dapm_graph_elems_load;
2025 break;
2026 case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2027 hdr_pass = SOC_TPLG_PASS_WIDGET;
2028 elem_load = soc_tplg_dapm_widget_elems_load;
2029 break;
2030 case SND_SOC_TPLG_TYPE_PCM:
2031 hdr_pass = SOC_TPLG_PASS_PCM_DAI;
2032 elem_load = soc_tplg_pcm_elems_load;
2033 break;
2034 case SND_SOC_TPLG_TYPE_DAI:
2035 hdr_pass = SOC_TPLG_PASS_BE_DAI;
2036 elem_load = soc_tplg_dai_elems_load;
2037 break;
2038 case SND_SOC_TPLG_TYPE_DAI_LINK:
2039 case SND_SOC_TPLG_TYPE_BACKEND_LINK:
2040
2041 hdr_pass = SOC_TPLG_PASS_LINK;
2042 elem_load = soc_tplg_link_elems_load;
2043 break;
2044 case SND_SOC_TPLG_TYPE_MANIFEST:
2045 hdr_pass = SOC_TPLG_PASS_MANIFEST;
2046 elem_load = soc_tplg_manifest_load;
2047 break;
2048 default:
2049
2050 hdr_pass = SOC_TPLG_PASS_VENDOR;
2051 elem_load = soc_tplg_vendor_load;
2052 break;
2053 }
2054
2055 if (tplg->pass == hdr_pass) {
2056 dev_dbg(tplg->dev,
2057 "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
2058 hdr->payload_size, hdr->type, hdr->version,
2059 hdr->vendor_type, tplg->pass);
2060 return elem_load(tplg, hdr);
2061 }
2062
2063 return 0;
2064}
2065
2066
2067static int soc_tplg_process_headers(struct soc_tplg *tplg)
2068{
2069 int ret;
2070
2071
2072 for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) {
2073 struct snd_soc_tplg_hdr *hdr;
2074
2075 tplg->hdr_pos = tplg->fw->data;
2076 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2077
2078 while (!soc_tplg_is_eof(tplg)) {
2079
2080
2081 ret = soc_tplg_valid_header(tplg, hdr);
2082 if (ret < 0)
2083 return ret;
2084
2085
2086 ret = soc_tplg_load_header(tplg, hdr);
2087 if (ret < 0) {
2088 if (ret != -EPROBE_DEFER) {
2089 dev_err(tplg->dev,
2090 "ASoC: topology: could not load header: %d\n",
2091 ret);
2092 }
2093 return ret;
2094 }
2095
2096
2097 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
2098 sizeof(struct snd_soc_tplg_hdr);
2099 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
2100 }
2101
2102 }
2103
2104
2105 ret = soc_tplg_dapm_complete(tplg);
2106
2107 return ret;
2108}
2109
2110static int soc_tplg_load(struct soc_tplg *tplg)
2111{
2112 int ret;
2113
2114 ret = soc_tplg_process_headers(tplg);
2115 if (ret == 0)
2116 return soc_tplg_complete(tplg);
2117
2118 return ret;
2119}
2120
2121
2122int snd_soc_tplg_component_load(struct snd_soc_component *comp,
2123 const struct snd_soc_tplg_ops *ops, const struct firmware *fw)
2124{
2125 struct soc_tplg tplg;
2126 int ret;
2127
2128
2129
2130
2131
2132
2133
2134
2135 if (!comp || !comp->card || !comp->card->dev || !fw)
2136 return -EINVAL;
2137
2138
2139 memset(&tplg, 0, sizeof(tplg));
2140 tplg.fw = fw;
2141 tplg.dev = comp->card->dev;
2142 tplg.comp = comp;
2143 if (ops) {
2144 tplg.ops = ops;
2145 tplg.io_ops = ops->io_ops;
2146 tplg.io_ops_count = ops->io_ops_count;
2147 tplg.bytes_ext_ops = ops->bytes_ext_ops;
2148 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2149 }
2150
2151 ret = soc_tplg_load(&tplg);
2152
2153 if (ret)
2154 snd_soc_tplg_component_remove(comp);
2155
2156 return ret;
2157}
2158EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
2159
2160
2161int snd_soc_tplg_component_remove(struct snd_soc_component *comp)
2162{
2163 struct snd_soc_dobj *dobj, *next_dobj;
2164 int pass;
2165
2166
2167 for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) {
2168
2169
2170 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
2171 list) {
2172
2173 switch (dobj->type) {
2174 case SND_SOC_DOBJ_BYTES:
2175 case SND_SOC_DOBJ_ENUM:
2176 case SND_SOC_DOBJ_MIXER:
2177 soc_tplg_remove_kcontrol(comp, dobj, pass);
2178 break;
2179 case SND_SOC_DOBJ_GRAPH:
2180 soc_tplg_remove_route(comp, dobj, pass);
2181 break;
2182 case SND_SOC_DOBJ_WIDGET:
2183 soc_tplg_remove_widget(comp, dobj, pass);
2184 break;
2185 case SND_SOC_DOBJ_PCM:
2186 soc_tplg_remove_dai(comp, dobj, pass);
2187 break;
2188 case SND_SOC_DOBJ_DAI_LINK:
2189 soc_tplg_remove_link(comp, dobj, pass);
2190 break;
2191 case SND_SOC_DOBJ_BACKEND_LINK:
2192
2193
2194
2195
2196 remove_backend_link(comp, dobj, pass);
2197 break;
2198 default:
2199 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
2200 dobj->type);
2201 break;
2202 }
2203 }
2204 }
2205
2206
2207 return !list_empty(&comp->dobj_list);
2208}
2209EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);
2210