forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoc-topology.c
2621 lines (2195 loc) · 70.1 KB
/
soc-topology.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
//
// soc-topology.c -- ALSA SoC Topology
//
// Copyright (C) 2012 Texas Instruments Inc.
// Copyright (C) 2015 Intel Corporation.
//
// Authors: Liam Girdwood <[email protected]>
// K, Mythri P <[email protected]>
// Prusty, Subhransu S <[email protected]>
// B, Jayachandran <[email protected]>
// Abdullah, Omair M <[email protected]>
// Jin, Yao <[email protected]>
// Lin, Mengdong <[email protected]>
//
// Add support to read audio firmware topology alongside firmware text. The
// topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
// equalizers, firmware, coefficients etc.
//
// This file only manages the core ALSA and ASoC components, all other bespoke
// firmware topology data is passed to component drivers for bespoke handling.
#include <linux/kernel.h>
#include <linux/export.h>
#include <linux/list.h>
#include <linux/firmware.h>
#include <linux/slab.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/soc-topology.h>
#include <sound/tlv.h>
#define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */
/*
* We make several passes over the data (since it wont necessarily be ordered)
* and process objects in the following order. This guarantees the component
* drivers will be ready with any vendor data before the mixers and DAPM objects
* are loaded (that may make use of the vendor data).
*/
#define SOC_TPLG_PASS_MANIFEST 0
#define SOC_TPLG_PASS_VENDOR 1
#define SOC_TPLG_PASS_CONTROL 2
#define SOC_TPLG_PASS_WIDGET 3
#define SOC_TPLG_PASS_PCM_DAI 4
#define SOC_TPLG_PASS_GRAPH 5
#define SOC_TPLG_PASS_BE_DAI 6
#define SOC_TPLG_PASS_LINK 7
#define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
#define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
/* topology context */
struct soc_tplg {
const struct firmware *fw;
/* runtime FW parsing */
const u8 *pos; /* read position */
const u8 *hdr_pos; /* header position */
unsigned int pass; /* pass number */
/* component caller */
struct device *dev;
struct snd_soc_component *comp;
u32 index; /* current block index */
/* vendor specific kcontrol operations */
const struct snd_soc_tplg_kcontrol_ops *io_ops;
int io_ops_count;
/* vendor specific bytes ext handlers, for TLV bytes controls */
const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
int bytes_ext_ops_count;
/* optional fw loading callbacks to component drivers */
struct snd_soc_tplg_ops *ops;
};
/* check we dont overflow the data for this control chunk */
static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
unsigned int count, size_t bytes, const char *elem_type)
{
const u8 *end = tplg->pos + elem_size * count;
if (end > tplg->fw->data + tplg->fw->size) {
dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
elem_type);
return -EINVAL;
}
/* check there is enough room in chunk for control.
extra bytes at the end of control are for vendor data here */
if (elem_size * count > bytes) {
dev_err(tplg->dev,
"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
elem_type, count, elem_size, bytes);
return -EINVAL;
}
return 0;
}
static inline bool soc_tplg_is_eof(struct soc_tplg *tplg)
{
const u8 *end = tplg->hdr_pos;
if (end >= tplg->fw->data + tplg->fw->size)
return true;
return false;
}
static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
{
return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
}
static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
{
return (unsigned long)(tplg->pos - tplg->fw->data);
}
/* mapping of Kcontrol types and associated operations. */
static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
snd_soc_put_volsw, snd_soc_info_volsw},
{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
snd_soc_put_volsw_sx, NULL},
{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
snd_soc_put_enum_double, snd_soc_info_enum_double},
{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
snd_soc_put_enum_double, NULL},
{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
snd_soc_bytes_put, snd_soc_bytes_info},
{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
snd_soc_put_volsw_range, snd_soc_info_volsw_range},
{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
snd_soc_put_xr_sx, snd_soc_info_xr_sx},
{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
snd_soc_put_strobe, NULL},
{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
snd_soc_dapm_put_volsw, snd_soc_info_volsw},
{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
snd_soc_dapm_put_enum_double, NULL},
{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
snd_soc_dapm_put_enum_double, NULL},
{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
};
struct soc_tplg_map {
int uid;
int kid;
};
/* mapping of widget types from UAPI IDs to kernel IDs */
static const struct soc_tplg_map dapm_map[] = {
{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
{SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
{SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
{SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
{SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
{SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
{SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
{SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
{SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
};
static int tplg_chan_get_reg(struct soc_tplg *tplg,
struct snd_soc_tplg_channel *chan, int map)
{
int i;
for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
if (le32_to_cpu(chan[i].id) == map)
return le32_to_cpu(chan[i].reg);
}
return -EINVAL;
}
static int tplg_chan_get_shift(struct soc_tplg *tplg,
struct snd_soc_tplg_channel *chan, int map)
{
int i;
for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
if (le32_to_cpu(chan[i].id) == map)
return le32_to_cpu(chan[i].shift);
}
return -EINVAL;
}
static int get_widget_id(int tplg_type)
{
int i;
for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
if (tplg_type == dapm_map[i].uid)
return dapm_map[i].kid;
}
return -EINVAL;
}
static inline void soc_bind_err(struct soc_tplg *tplg,
struct snd_soc_tplg_ctl_hdr *hdr, int index)
{
dev_err(tplg->dev,
"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
soc_tplg_get_offset(tplg));
}
static inline void soc_control_err(struct soc_tplg *tplg,
struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
{
dev_err(tplg->dev,
"ASoC: no complete control IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
soc_tplg_get_offset(tplg));
}
/* pass vendor data to component driver for processing */
static int soc_tplg_vendor_load(struct soc_tplg *tplg,
struct snd_soc_tplg_hdr *hdr)
{
int ret = 0;
if (tplg->ops && tplg->ops->vendor_load)
ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
else {
dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
hdr->vendor_type);
return -EINVAL;
}
if (ret < 0)
dev_err(tplg->dev,
"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
soc_tplg_get_hdr_offset(tplg),
soc_tplg_get_hdr_offset(tplg),
hdr->type, hdr->vendor_type);
return ret;
}
/* optionally pass new dynamic widget to component driver. This is mainly for
* external widgets where we can assign private data/ops */
static int soc_tplg_widget_load(struct soc_tplg *tplg,
struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
{
if (tplg->ops && tplg->ops->widget_load)
return tplg->ops->widget_load(tplg->comp, tplg->index, w,
tplg_w);
return 0;
}
/* optionally pass new dynamic widget to component driver. This is mainly for
* external widgets where we can assign private data/ops */
static int soc_tplg_widget_ready(struct soc_tplg *tplg,
struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
{
if (tplg->ops && tplg->ops->widget_ready)
return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
tplg_w);
return 0;
}
/* pass DAI configurations to component driver for extra initialization */
static int soc_tplg_dai_load(struct soc_tplg *tplg,
struct snd_soc_dai_driver *dai_drv,
struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
{
if (tplg->ops && tplg->ops->dai_load)
return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
pcm, dai);
return 0;
}
/* pass link configurations to component driver for extra initialization */
static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
{
if (tplg->ops && tplg->ops->link_load)
return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
return 0;
}
/* tell the component driver that all firmware has been loaded in this request */
static int soc_tplg_complete(struct soc_tplg *tplg)
{
if (tplg->ops && tplg->ops->complete)
return tplg->ops->complete(tplg->comp);
return 0;
}
/* add a dynamic kcontrol */
static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
const struct snd_kcontrol_new *control_new, const char *prefix,
void *data, struct snd_kcontrol **kcontrol)
{
int err;
*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
if (*kcontrol == NULL) {
dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
control_new->name);
return -ENOMEM;
}
err = snd_ctl_add(card, *kcontrol);
if (err < 0) {
dev_err(dev, "ASoC: Failed to add %s: %d\n",
control_new->name, err);
return err;
}
return 0;
}
/* add a dynamic kcontrol for component driver */
static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
{
struct snd_soc_component *comp = tplg->comp;
return soc_tplg_add_dcontrol(comp->card->snd_card,
tplg->dev, k, comp->name_prefix, comp, kcontrol);
}
/* remove kcontrol */
static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj,
int pass)
{
struct snd_card *card = comp->card->snd_card;
if (pass != SOC_TPLG_PASS_CONTROL)
return;
if (dobj->unload)
dobj->unload(comp, dobj);
snd_ctl_remove(card, dobj->control.kcontrol);
list_del(&dobj->list);
}
/* remove a route */
static void soc_tplg_remove_route(struct snd_soc_component *comp,
struct snd_soc_dobj *dobj, int pass)
{
if (pass != SOC_TPLG_PASS_GRAPH)
return;
if (dobj->unload)
dobj->unload(comp, dobj);
list_del(&dobj->list);
}
/* remove a widget and it's kcontrols - routes must be removed first */
static void soc_tplg_remove_widget(struct snd_soc_component *comp,
struct snd_soc_dobj *dobj, int pass)
{
struct snd_card *card = comp->card->snd_card;
struct snd_soc_dapm_widget *w =
container_of(dobj, struct snd_soc_dapm_widget, dobj);
int i;
if (pass != SOC_TPLG_PASS_WIDGET)
return;
if (dobj->unload)
dobj->unload(comp, dobj);
if (!w->kcontrols)
goto free_news;
for (i = 0; w->kcontrols && i < w->num_kcontrols; i++)
snd_ctl_remove(card, w->kcontrols[i]);
free_news:
list_del(&dobj->list);
/* widget w is freed by soc-dapm.c */
}
/* remove DAI configurations */
static void soc_tplg_remove_dai(struct snd_soc_component *comp,
struct snd_soc_dobj *dobj, int pass)
{
struct snd_soc_dai_driver *dai_drv =
container_of(dobj, struct snd_soc_dai_driver, dobj);
struct snd_soc_dai *dai, *_dai;
if (pass != SOC_TPLG_PASS_PCM_DAI)
return;
if (dobj->unload)
dobj->unload(comp, dobj);
for_each_component_dais_safe(comp, dai, _dai)
if (dai->driver == dai_drv)
snd_soc_unregister_dai(dai);
list_del(&dobj->list);
}
/* remove link configurations */
static void soc_tplg_remove_link(struct snd_soc_component *comp,
struct snd_soc_dobj *dobj, int pass)
{
struct snd_soc_dai_link *link =
container_of(dobj, struct snd_soc_dai_link, dobj);
if (pass != SOC_TPLG_PASS_PCM_DAI)
return;
if (dobj->unload)
dobj->unload(comp, dobj);
list_del(&dobj->list);
snd_soc_remove_pcm_runtime(comp->card,
snd_soc_get_pcm_runtime(comp->card, link));
}
/* unload dai link */
static void remove_backend_link(struct snd_soc_component *comp,
struct snd_soc_dobj *dobj, int pass)
{
if (pass != SOC_TPLG_PASS_LINK)
return;
if (dobj->unload)
dobj->unload(comp, dobj);
/*
* We don't free the link here as what soc_tplg_remove_link() do since BE
* links are not allocated by topology.
* We however need to reset the dobj type to its initial values
*/
dobj->type = SND_SOC_DOBJ_NONE;
list_del(&dobj->list);
}
/* bind a kcontrol to it's IO handlers */
static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
struct snd_kcontrol_new *k,
const struct soc_tplg *tplg)
{
const struct snd_soc_tplg_kcontrol_ops *ops;
const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
int num_ops, i;
if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
&& (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ
|| k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE)
&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
struct soc_bytes_ext *sbe;
struct snd_soc_tplg_bytes_control *be;
sbe = (struct soc_bytes_ext *)k->private_value;
be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
/* TLV bytes controls need standard kcontrol info handler,
* TLV callback and extended put/get handlers.
*/
k->info = snd_soc_bytes_info_ext;
k->tlv.c = snd_soc_bytes_tlv_callback;
/*
* When a topology-based implementation abuses the
* control interface and uses bytes_ext controls of
* more than 512 bytes, we need to disable the size
* checks, otherwise accesses to such controls will
* return an -EINVAL error and prevent the card from
* being configured.
*/
if (sbe->max > 512)
k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;
ext_ops = tplg->bytes_ext_ops;
num_ops = tplg->bytes_ext_ops_count;
for (i = 0; i < num_ops; i++) {
if (!sbe->put &&
ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
sbe->put = ext_ops[i].put;
if (!sbe->get &&
ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
sbe->get = ext_ops[i].get;
}
if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
return -EINVAL;
if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
return -EINVAL;
return 0;
}
/* try and map vendor specific kcontrol handlers first */
ops = tplg->io_ops;
num_ops = tplg->io_ops_count;
for (i = 0; i < num_ops; i++) {
if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
k->put = ops[i].put;
if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
k->get = ops[i].get;
if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
k->info = ops[i].info;
}
/* vendor specific handlers found ? */
if (k->put && k->get && k->info)
return 0;
/* none found so try standard kcontrol handlers */
ops = io_ops;
num_ops = ARRAY_SIZE(io_ops);
for (i = 0; i < num_ops; i++) {
if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
k->put = ops[i].put;
if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
k->get = ops[i].get;
if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
k->info = ops[i].info;
}
/* standard handlers found ? */
if (k->put && k->get && k->info)
return 0;
/* nothing to bind */
return -EINVAL;
}
/* bind a widgets to it's evnt handlers */
int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
const struct snd_soc_tplg_widget_events *events,
int num_events, u16 event_type)
{
int i;
w->event = NULL;
for (i = 0; i < num_events; i++) {
if (event_type == events[i].type) {
/* found - so assign event */
w->event = events[i].event_handler;
return 0;
}
}
/* not found */
return -EINVAL;
}
EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
/* optionally pass new dynamic kcontrol to component driver. */
static int soc_tplg_control_load(struct soc_tplg *tplg,
struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
{
int ret = 0;
if (tplg->ops && tplg->ops->control_load)
ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr);
if (ret)
dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name);
return ret;
}
static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
{
unsigned int item_len = 2 * sizeof(unsigned int);
unsigned int *p;
p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
if (!p)
return -ENOMEM;
p[0] = SNDRV_CTL_TLVT_DB_SCALE;
p[1] = item_len;
p[2] = le32_to_cpu(scale->min);
p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
| (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
kc->tlv.p = (void *)p;
return 0;
}
static int soc_tplg_create_tlv(struct soc_tplg *tplg,
struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
{
struct snd_soc_tplg_ctl_tlv *tplg_tlv;
u32 access = le32_to_cpu(tc->access);
if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
return 0;
if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
tplg_tlv = &tc->tlv;
switch (le32_to_cpu(tplg_tlv->type)) {
case SNDRV_CTL_TLVT_DB_SCALE:
return soc_tplg_create_tlv_db_scale(tplg, kc,
&tplg_tlv->scale);
/* TODO: add support for other TLV types */
default:
dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
tplg_tlv->type);
return -EINVAL;
}
}
return 0;
}
static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size)
{
struct snd_soc_tplg_bytes_control *be;
struct soc_bytes_ext *sbe;
struct snd_kcontrol_new kc;
int ret = 0;
if (soc_tplg_check_elem_count(tplg,
sizeof(struct snd_soc_tplg_bytes_control),
1, size, "mixer bytes"))
return -EINVAL;
be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
/* validate kcontrol */
if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
return -EINVAL;
sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL);
if (sbe == NULL)
return -ENOMEM;
tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
le32_to_cpu(be->priv.size));
dev_dbg(tplg->dev,
"ASoC: adding bytes kcontrol %s with access 0x%x\n",
be->hdr.name, be->hdr.access);
memset(&kc, 0, sizeof(kc));
kc.name = be->hdr.name;
kc.private_value = (long)sbe;
kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
kc.access = le32_to_cpu(be->hdr.access);
sbe->max = le32_to_cpu(be->max);
sbe->dobj.type = SND_SOC_DOBJ_BYTES;
if (tplg->ops)
sbe->dobj.unload = tplg->ops->control_unload;
INIT_LIST_HEAD(&sbe->dobj.list);
/* map io handlers */
ret = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
if (ret) {
soc_control_err(tplg, &be->hdr, be->hdr.name);
goto err;
}
/* pass control to driver for optional further init */
ret = soc_tplg_control_load(tplg, &kc, &be->hdr);
if (ret < 0)
goto err;
/* register control here */
ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol);
if (ret < 0)
goto err;
list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
err:
return ret;
}
static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size)
{
struct snd_soc_tplg_mixer_control *mc;
struct soc_mixer_control *sm;
struct snd_kcontrol_new kc;
int ret = 0;
if (soc_tplg_check_elem_count(tplg,
sizeof(struct snd_soc_tplg_mixer_control),
1, size, "mixers"))
return -EINVAL;
mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
/* validate kcontrol */
if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
return -EINVAL;
sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL);
if (sm == NULL)
return -ENOMEM;
tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
le32_to_cpu(mc->priv.size));
dev_dbg(tplg->dev,
"ASoC: adding mixer kcontrol %s with access 0x%x\n",
mc->hdr.name, mc->hdr.access);
memset(&kc, 0, sizeof(kc));
kc.name = mc->hdr.name;
kc.private_value = (long)sm;
kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
kc.access = le32_to_cpu(mc->hdr.access);
/* we only support FL/FR channel mapping atm */
sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL);
sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR);
sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL);
sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR);
sm->max = le32_to_cpu(mc->max);
sm->min = le32_to_cpu(mc->min);
sm->invert = le32_to_cpu(mc->invert);
sm->platform_max = le32_to_cpu(mc->platform_max);
sm->dobj.index = tplg->index;
sm->dobj.type = SND_SOC_DOBJ_MIXER;
if (tplg->ops)
sm->dobj.unload = tplg->ops->control_unload;
INIT_LIST_HEAD(&sm->dobj.list);
/* map io handlers */
ret = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
if (ret) {
soc_control_err(tplg, &mc->hdr, mc->hdr.name);
goto err;
}
/* create any TLV data */
ret = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
if (ret < 0) {
dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name);
goto err;
}
/* pass control to driver for optional further init */
ret = soc_tplg_control_load(tplg, &kc, &mc->hdr);
if (ret < 0)
goto err;
/* register control here */
ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol);
if (ret < 0)
goto err;
list_add(&sm->dobj.list, &tplg->comp->dobj_list);
err:
return ret;
}
static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se,
struct snd_soc_tplg_enum_control *ec)
{
int i, ret;
if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts))
return -EINVAL;
se->dobj.control.dtexts =
devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
if (se->dobj.control.dtexts == NULL)
return -ENOMEM;
for (i = 0; i < le32_to_cpu(ec->items); i++) {
if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
ret = -EINVAL;
goto err;
}
se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL);
if (!se->dobj.control.dtexts[i]) {
ret = -ENOMEM;
goto err;
}
}
se->items = le32_to_cpu(ec->items);
se->texts = (const char * const *)se->dobj.control.dtexts;
return 0;
err:
return ret;
}
static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se,
struct snd_soc_tplg_enum_control *ec)
{
int i;
/*
* Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS
* values instead of using ARRAY_SIZE(ec->values) due to the fact that
* it is oversized for its purpose. Additionally it is done so because
* it is defined in UAPI header where it can't be easily changed.
*/
if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS)
return -EINVAL;
se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items),
sizeof(*se->dobj.control.dvalues),
GFP_KERNEL);
if (!se->dobj.control.dvalues)
return -ENOMEM;
/* convert from little-endian */
for (i = 0; i < le32_to_cpu(ec->items); i++) {
se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
}
return 0;
}
static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size)
{
struct snd_soc_tplg_enum_control *ec;
struct soc_enum *se;
struct snd_kcontrol_new kc;
int ret = 0;
if (soc_tplg_check_elem_count(tplg,
sizeof(struct snd_soc_tplg_enum_control),
1, size, "enums"))
return -EINVAL;
ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
/* validate kcontrol */
if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
return -EINVAL;
se = devm_kzalloc(tplg->dev, (sizeof(*se)), GFP_KERNEL);
if (se == NULL)
return -ENOMEM;
tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
le32_to_cpu(ec->priv.size));
dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
ec->hdr.name, ec->items);
memset(&kc, 0, sizeof(kc));
kc.name = ec->hdr.name;
kc.private_value = (long)se;
kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
kc.access = le32_to_cpu(ec->hdr.access);
se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
se->shift_l = tplg_chan_get_shift(tplg, ec->channel,
SNDRV_CHMAP_FL);
se->shift_r = tplg_chan_get_shift(tplg, ec->channel,
SNDRV_CHMAP_FL);
se->mask = le32_to_cpu(ec->mask);
se->dobj.index = tplg->index;
se->dobj.type = SND_SOC_DOBJ_ENUM;
if (tplg->ops)
se->dobj.unload = tplg->ops->control_unload;
INIT_LIST_HEAD(&se->dobj.list);
switch (le32_to_cpu(ec->hdr.ops.info)) {
case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
case SND_SOC_TPLG_CTL_ENUM_VALUE:
ret = soc_tplg_denum_create_values(tplg, se, ec);
if (ret < 0) {
dev_err(tplg->dev,
"ASoC: could not create values for %s\n",
ec->hdr.name);
goto err;
}
fallthrough;
case SND_SOC_TPLG_CTL_ENUM:
case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
ret = soc_tplg_denum_create_texts(tplg, se, ec);
if (ret < 0) {
dev_err(tplg->dev,
"ASoC: could not create texts for %s\n",
ec->hdr.name);
goto err;
}
break;
default:
ret = -EINVAL;
dev_err(tplg->dev,
"ASoC: invalid enum control type %d for %s\n",
ec->hdr.ops.info, ec->hdr.name);
goto err;
}
/* map io handlers */
ret = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
if (ret) {
soc_control_err(tplg, &ec->hdr, ec->hdr.name);
goto err;
}
/* pass control to driver for optional further init */
ret = soc_tplg_control_load(tplg, &kc, &ec->hdr);
if (ret < 0)
goto err;
/* register control here */
ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol);
if (ret < 0)
goto err;
list_add(&se->dobj.list, &tplg->comp->dobj_list);
err:
return ret;
}
static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
struct snd_soc_tplg_hdr *hdr)
{
int ret;
int i;
dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
soc_tplg_get_offset(tplg));
for (i = 0; i < le32_to_cpu(hdr->count); i++) {
struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
dev_err(tplg->dev, "ASoC: invalid control size\n");
return -EINVAL;
}
switch (le32_to_cpu(control_hdr->ops.info)) {
case SND_SOC_TPLG_CTL_VOLSW:
case SND_SOC_TPLG_CTL_STROBE:
case SND_SOC_TPLG_CTL_VOLSW_SX:
case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
case SND_SOC_TPLG_CTL_RANGE:
case SND_SOC_TPLG_DAPM_CTL_VOLSW:
case SND_SOC_TPLG_DAPM_CTL_PIN:
ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size));
break;
case SND_SOC_TPLG_CTL_ENUM:
case SND_SOC_TPLG_CTL_ENUM_VALUE:
case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size));
break;
case SND_SOC_TPLG_CTL_BYTES:
ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size));
break;
default:
soc_bind_err(tplg, control_hdr, i);
return -EINVAL;
}
if (ret < 0) {
dev_err(tplg->dev, "ASoC: invalid control\n");