Commit | Line | Data |
---|---|---|
8a978234 LG |
1 | /* |
2 | * soc-topology.c -- ALSA SoC Topology | |
3 | * | |
4 | * Copyright (C) 2012 Texas Instruments Inc. | |
5 | * Copyright (C) 2015 Intel Corporation. | |
6 | * | |
7 | * Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com> | |
8 | * K, Mythri P <mythri.p.k@intel.com> | |
9 | * Prusty, Subhransu S <subhransu.s.prusty@intel.com> | |
10 | * B, Jayachandran <jayachandran.b@intel.com> | |
11 | * Abdullah, Omair M <omair.m.abdullah@intel.com> | |
12 | * Jin, Yao <yao.jin@intel.com> | |
13 | * Lin, Mengdong <mengdong.lin@intel.com> | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or modify it | |
16 | * under the terms of the GNU General Public License as published by the | |
17 | * Free Software Foundation; either version 2 of the License, or (at your | |
18 | * option) any later version. | |
19 | * | |
20 | * Add support to read audio firmware topology alongside firmware text. The | |
21 | * topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links, | |
22 | * equalizers, firmware, coefficients etc. | |
23 | * | |
24 | * This file only manages the core ALSA and ASoC components, all other bespoke | |
25 | * firmware topology data is passed to component drivers for bespoke handling. | |
26 | */ | |
27 | ||
28 | #include <linux/kernel.h> | |
29 | #include <linux/export.h> | |
30 | #include <linux/list.h> | |
31 | #include <linux/firmware.h> | |
32 | #include <linux/slab.h> | |
33 | #include <sound/soc.h> | |
34 | #include <sound/soc-dapm.h> | |
35 | #include <sound/soc-topology.h> | |
28a87eeb | 36 | #include <sound/tlv.h> |
8a978234 LG |
37 | |
38 | /* | |
39 | * We make several passes over the data (since it wont necessarily be ordered) | |
40 | * and process objects in the following order. This guarantees the component | |
41 | * drivers will be ready with any vendor data before the mixers and DAPM objects | |
42 | * are loaded (that may make use of the vendor data). | |
43 | */ | |
44 | #define SOC_TPLG_PASS_MANIFEST 0 | |
45 | #define SOC_TPLG_PASS_VENDOR 1 | |
46 | #define SOC_TPLG_PASS_MIXER 2 | |
47 | #define SOC_TPLG_PASS_WIDGET 3 | |
1a8e7fab ML |
48 | #define SOC_TPLG_PASS_PCM_DAI 4 |
49 | #define SOC_TPLG_PASS_GRAPH 5 | |
50 | #define SOC_TPLG_PASS_PINS 6 | |
0038be9a | 51 | #define SOC_TPLG_PASS_BE_DAI 7 |
8a978234 LG |
52 | |
53 | #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST | |
0038be9a | 54 | #define SOC_TPLG_PASS_END SOC_TPLG_PASS_BE_DAI |
8a978234 LG |
55 | |
56 | struct soc_tplg { | |
57 | const struct firmware *fw; | |
58 | ||
59 | /* runtime FW parsing */ | |
60 | const u8 *pos; /* read postion */ | |
61 | const u8 *hdr_pos; /* header position */ | |
62 | unsigned int pass; /* pass number */ | |
63 | ||
64 | /* component caller */ | |
65 | struct device *dev; | |
66 | struct snd_soc_component *comp; | |
67 | u32 index; /* current block index */ | |
68 | u32 req_index; /* required index, only loaded/free matching blocks */ | |
69 | ||
88a17d8f | 70 | /* vendor specific kcontrol operations */ |
8a978234 LG |
71 | const struct snd_soc_tplg_kcontrol_ops *io_ops; |
72 | int io_ops_count; | |
73 | ||
1a3232d2 ML |
74 | /* vendor specific bytes ext handlers, for TLV bytes controls */ |
75 | const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops; | |
76 | int bytes_ext_ops_count; | |
77 | ||
8a978234 LG |
78 | /* optional fw loading callbacks to component drivers */ |
79 | struct snd_soc_tplg_ops *ops; | |
80 | }; | |
81 | ||
82 | static int soc_tplg_process_headers(struct soc_tplg *tplg); | |
83 | static void soc_tplg_complete(struct soc_tplg *tplg); | |
84 | struct snd_soc_dapm_widget * | |
85 | snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm, | |
86 | const struct snd_soc_dapm_widget *widget); | |
87 | struct snd_soc_dapm_widget * | |
88 | snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, | |
89 | const struct snd_soc_dapm_widget *widget); | |
90 | ||
91 | /* check we dont overflow the data for this control chunk */ | |
92 | static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size, | |
93 | unsigned int count, size_t bytes, const char *elem_type) | |
94 | { | |
95 | const u8 *end = tplg->pos + elem_size * count; | |
96 | ||
97 | if (end > tplg->fw->data + tplg->fw->size) { | |
98 | dev_err(tplg->dev, "ASoC: %s overflow end of data\n", | |
99 | elem_type); | |
100 | return -EINVAL; | |
101 | } | |
102 | ||
103 | /* check there is enough room in chunk for control. | |
104 | extra bytes at the end of control are for vendor data here */ | |
105 | if (elem_size * count > bytes) { | |
106 | dev_err(tplg->dev, | |
107 | "ASoC: %s count %d of size %zu is bigger than chunk %zu\n", | |
108 | elem_type, count, elem_size, bytes); | |
109 | return -EINVAL; | |
110 | } | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
115 | static inline int soc_tplg_is_eof(struct soc_tplg *tplg) | |
116 | { | |
117 | const u8 *end = tplg->hdr_pos; | |
118 | ||
119 | if (end >= tplg->fw->data + tplg->fw->size) | |
120 | return 1; | |
121 | return 0; | |
122 | } | |
123 | ||
124 | static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg) | |
125 | { | |
126 | return (unsigned long)(tplg->hdr_pos - tplg->fw->data); | |
127 | } | |
128 | ||
129 | static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg) | |
130 | { | |
131 | return (unsigned long)(tplg->pos - tplg->fw->data); | |
132 | } | |
133 | ||
134 | /* mapping of Kcontrol types and associated operations. */ | |
135 | static const struct snd_soc_tplg_kcontrol_ops io_ops[] = { | |
136 | {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw, | |
137 | snd_soc_put_volsw, snd_soc_info_volsw}, | |
138 | {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx, | |
139 | snd_soc_put_volsw_sx, NULL}, | |
140 | {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double, | |
141 | snd_soc_put_enum_double, snd_soc_info_enum_double}, | |
142 | {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double, | |
143 | snd_soc_put_enum_double, NULL}, | |
144 | {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get, | |
145 | snd_soc_bytes_put, snd_soc_bytes_info}, | |
146 | {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range, | |
147 | snd_soc_put_volsw_range, snd_soc_info_volsw_range}, | |
148 | {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx, | |
149 | snd_soc_put_xr_sx, snd_soc_info_xr_sx}, | |
150 | {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe, | |
151 | snd_soc_put_strobe, NULL}, | |
152 | {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw, | |
2c57d478 | 153 | snd_soc_dapm_put_volsw, snd_soc_info_volsw}, |
8a978234 LG |
154 | {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double, |
155 | snd_soc_dapm_put_enum_double, snd_soc_info_enum_double}, | |
156 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double, | |
157 | snd_soc_dapm_put_enum_double, NULL}, | |
158 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double, | |
159 | snd_soc_dapm_put_enum_double, NULL}, | |
160 | {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch, | |
161 | snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch}, | |
162 | }; | |
163 | ||
164 | struct soc_tplg_map { | |
165 | int uid; | |
166 | int kid; | |
167 | }; | |
168 | ||
169 | /* mapping of widget types from UAPI IDs to kernel IDs */ | |
170 | static const struct soc_tplg_map dapm_map[] = { | |
171 | {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input}, | |
172 | {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output}, | |
173 | {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux}, | |
174 | {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer}, | |
175 | {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga}, | |
176 | {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv}, | |
177 | {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc}, | |
178 | {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac}, | |
179 | {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch}, | |
180 | {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre}, | |
181 | {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post}, | |
182 | {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in}, | |
183 | {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out}, | |
184 | {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in}, | |
185 | {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out}, | |
186 | {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link}, | |
187 | }; | |
188 | ||
189 | static int tplc_chan_get_reg(struct soc_tplg *tplg, | |
190 | struct snd_soc_tplg_channel *chan, int map) | |
191 | { | |
192 | int i; | |
193 | ||
194 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { | |
195 | if (chan[i].id == map) | |
196 | return chan[i].reg; | |
197 | } | |
198 | ||
199 | return -EINVAL; | |
200 | } | |
201 | ||
202 | static int tplc_chan_get_shift(struct soc_tplg *tplg, | |
203 | struct snd_soc_tplg_channel *chan, int map) | |
204 | { | |
205 | int i; | |
206 | ||
207 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { | |
208 | if (chan[i].id == map) | |
209 | return chan[i].shift; | |
210 | } | |
211 | ||
212 | return -EINVAL; | |
213 | } | |
214 | ||
215 | static int get_widget_id(int tplg_type) | |
216 | { | |
217 | int i; | |
218 | ||
219 | for (i = 0; i < ARRAY_SIZE(dapm_map); i++) { | |
220 | if (tplg_type == dapm_map[i].uid) | |
221 | return dapm_map[i].kid; | |
222 | } | |
223 | ||
224 | return -EINVAL; | |
225 | } | |
226 | ||
8a978234 LG |
227 | static inline void soc_bind_err(struct soc_tplg *tplg, |
228 | struct snd_soc_tplg_ctl_hdr *hdr, int index) | |
229 | { | |
230 | dev_err(tplg->dev, | |
231 | "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n", | |
232 | hdr->ops.get, hdr->ops.put, hdr->ops.info, index, | |
233 | soc_tplg_get_offset(tplg)); | |
234 | } | |
235 | ||
236 | static inline void soc_control_err(struct soc_tplg *tplg, | |
237 | struct snd_soc_tplg_ctl_hdr *hdr, const char *name) | |
238 | { | |
239 | dev_err(tplg->dev, | |
240 | "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n", | |
241 | name, hdr->ops.get, hdr->ops.put, hdr->ops.info, | |
242 | soc_tplg_get_offset(tplg)); | |
243 | } | |
244 | ||
245 | /* pass vendor data to component driver for processing */ | |
246 | static int soc_tplg_vendor_load_(struct soc_tplg *tplg, | |
247 | struct snd_soc_tplg_hdr *hdr) | |
248 | { | |
249 | int ret = 0; | |
250 | ||
251 | if (tplg->comp && tplg->ops && tplg->ops->vendor_load) | |
252 | ret = tplg->ops->vendor_load(tplg->comp, hdr); | |
253 | else { | |
254 | dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n", | |
255 | hdr->vendor_type); | |
256 | return -EINVAL; | |
257 | } | |
258 | ||
259 | if (ret < 0) | |
260 | dev_err(tplg->dev, | |
261 | "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n", | |
262 | soc_tplg_get_hdr_offset(tplg), | |
263 | soc_tplg_get_hdr_offset(tplg), | |
264 | hdr->type, hdr->vendor_type); | |
265 | return ret; | |
266 | } | |
267 | ||
268 | /* pass vendor data to component driver for processing */ | |
269 | static int soc_tplg_vendor_load(struct soc_tplg *tplg, | |
270 | struct snd_soc_tplg_hdr *hdr) | |
271 | { | |
272 | if (tplg->pass != SOC_TPLG_PASS_VENDOR) | |
273 | return 0; | |
274 | ||
275 | return soc_tplg_vendor_load_(tplg, hdr); | |
276 | } | |
277 | ||
278 | /* optionally pass new dynamic widget to component driver. This is mainly for | |
279 | * external widgets where we can assign private data/ops */ | |
280 | static int soc_tplg_widget_load(struct soc_tplg *tplg, | |
281 | struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) | |
282 | { | |
283 | if (tplg->comp && tplg->ops && tplg->ops->widget_load) | |
284 | return tplg->ops->widget_load(tplg->comp, w, tplg_w); | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
64527e8a ML |
289 | /* pass DAI configurations to component driver for extra intialization */ |
290 | static int soc_tplg_dai_load(struct soc_tplg *tplg, | |
291 | struct snd_soc_dai_driver *dai_drv) | |
8a978234 | 292 | { |
64527e8a ML |
293 | if (tplg->comp && tplg->ops && tplg->ops->dai_load) |
294 | return tplg->ops->dai_load(tplg->comp, dai_drv); | |
8a978234 LG |
295 | |
296 | return 0; | |
297 | } | |
298 | ||
acfc7d46 ML |
299 | /* pass link configurations to component driver for extra intialization */ |
300 | static int soc_tplg_dai_link_load(struct soc_tplg *tplg, | |
301 | struct snd_soc_dai_link *link) | |
302 | { | |
303 | if (tplg->comp && tplg->ops && tplg->ops->link_load) | |
304 | return tplg->ops->link_load(tplg->comp, link); | |
305 | ||
306 | return 0; | |
307 | } | |
308 | ||
8a978234 LG |
309 | /* tell the component driver that all firmware has been loaded in this request */ |
310 | static void soc_tplg_complete(struct soc_tplg *tplg) | |
311 | { | |
312 | if (tplg->comp && tplg->ops && tplg->ops->complete) | |
313 | tplg->ops->complete(tplg->comp); | |
314 | } | |
315 | ||
316 | /* add a dynamic kcontrol */ | |
317 | static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev, | |
318 | const struct snd_kcontrol_new *control_new, const char *prefix, | |
319 | void *data, struct snd_kcontrol **kcontrol) | |
320 | { | |
321 | int err; | |
322 | ||
323 | *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix); | |
324 | if (*kcontrol == NULL) { | |
325 | dev_err(dev, "ASoC: Failed to create new kcontrol %s\n", | |
326 | control_new->name); | |
327 | return -ENOMEM; | |
328 | } | |
329 | ||
330 | err = snd_ctl_add(card, *kcontrol); | |
331 | if (err < 0) { | |
332 | dev_err(dev, "ASoC: Failed to add %s: %d\n", | |
333 | control_new->name, err); | |
334 | return err; | |
335 | } | |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
340 | /* add a dynamic kcontrol for component driver */ | |
341 | static int soc_tplg_add_kcontrol(struct soc_tplg *tplg, | |
342 | struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol) | |
343 | { | |
344 | struct snd_soc_component *comp = tplg->comp; | |
345 | ||
346 | return soc_tplg_add_dcontrol(comp->card->snd_card, | |
347 | comp->dev, k, NULL, comp, kcontrol); | |
348 | } | |
349 | ||
350 | /* remove a mixer kcontrol */ | |
351 | static void remove_mixer(struct snd_soc_component *comp, | |
352 | struct snd_soc_dobj *dobj, int pass) | |
353 | { | |
354 | struct snd_card *card = comp->card->snd_card; | |
355 | struct soc_mixer_control *sm = | |
356 | container_of(dobj, struct soc_mixer_control, dobj); | |
357 | const unsigned int *p = NULL; | |
358 | ||
359 | if (pass != SOC_TPLG_PASS_MIXER) | |
360 | return; | |
361 | ||
362 | if (dobj->ops && dobj->ops->control_unload) | |
363 | dobj->ops->control_unload(comp, dobj); | |
364 | ||
365 | if (sm->dobj.control.kcontrol->tlv.p) | |
366 | p = sm->dobj.control.kcontrol->tlv.p; | |
367 | snd_ctl_remove(card, sm->dobj.control.kcontrol); | |
368 | list_del(&sm->dobj.list); | |
369 | kfree(sm); | |
370 | kfree(p); | |
371 | } | |
372 | ||
373 | /* remove an enum kcontrol */ | |
374 | static void remove_enum(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 soc_enum *se = container_of(dobj, struct soc_enum, dobj); | |
379 | int i; | |
380 | ||
381 | if (pass != SOC_TPLG_PASS_MIXER) | |
382 | return; | |
383 | ||
384 | if (dobj->ops && dobj->ops->control_unload) | |
385 | dobj->ops->control_unload(comp, dobj); | |
386 | ||
387 | snd_ctl_remove(card, se->dobj.control.kcontrol); | |
388 | list_del(&se->dobj.list); | |
389 | ||
390 | kfree(se->dobj.control.dvalues); | |
391 | for (i = 0; i < se->items; i++) | |
392 | kfree(se->dobj.control.dtexts[i]); | |
393 | kfree(se); | |
394 | } | |
395 | ||
396 | /* remove a byte kcontrol */ | |
397 | static void remove_bytes(struct snd_soc_component *comp, | |
398 | struct snd_soc_dobj *dobj, int pass) | |
399 | { | |
400 | struct snd_card *card = comp->card->snd_card; | |
401 | struct soc_bytes_ext *sb = | |
402 | container_of(dobj, struct soc_bytes_ext, dobj); | |
403 | ||
404 | if (pass != SOC_TPLG_PASS_MIXER) | |
405 | return; | |
406 | ||
407 | if (dobj->ops && dobj->ops->control_unload) | |
408 | dobj->ops->control_unload(comp, dobj); | |
409 | ||
410 | snd_ctl_remove(card, sb->dobj.control.kcontrol); | |
411 | list_del(&sb->dobj.list); | |
412 | kfree(sb); | |
413 | } | |
414 | ||
415 | /* remove a widget and it's kcontrols - routes must be removed first */ | |
416 | static void remove_widget(struct snd_soc_component *comp, | |
417 | struct snd_soc_dobj *dobj, int pass) | |
418 | { | |
419 | struct snd_card *card = comp->card->snd_card; | |
420 | struct snd_soc_dapm_widget *w = | |
421 | container_of(dobj, struct snd_soc_dapm_widget, dobj); | |
422 | int i; | |
423 | ||
424 | if (pass != SOC_TPLG_PASS_WIDGET) | |
425 | return; | |
426 | ||
427 | if (dobj->ops && dobj->ops->widget_unload) | |
428 | dobj->ops->widget_unload(comp, dobj); | |
429 | ||
430 | /* | |
431 | * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers. | |
432 | * The enum may either have an array of values or strings. | |
433 | */ | |
434 | if (dobj->widget.kcontrol_enum) { | |
435 | /* enumerated widget mixer */ | |
436 | struct soc_enum *se = | |
437 | (struct soc_enum *)w->kcontrols[0]->private_value; | |
438 | ||
439 | snd_ctl_remove(card, w->kcontrols[0]); | |
440 | ||
441 | kfree(se->dobj.control.dvalues); | |
442 | for (i = 0; i < se->items; i++) | |
443 | kfree(se->dobj.control.dtexts[i]); | |
444 | ||
445 | kfree(se); | |
446 | kfree(w->kcontrol_news); | |
447 | } else { | |
448 | /* non enumerated widget mixer */ | |
449 | for (i = 0; i < w->num_kcontrols; i++) { | |
450 | struct snd_kcontrol *kcontrol = w->kcontrols[i]; | |
451 | struct soc_mixer_control *sm = | |
452 | (struct soc_mixer_control *) kcontrol->private_value; | |
453 | ||
454 | kfree(w->kcontrols[i]->tlv.p); | |
455 | ||
456 | snd_ctl_remove(card, w->kcontrols[i]); | |
457 | kfree(sm); | |
458 | } | |
459 | kfree(w->kcontrol_news); | |
460 | } | |
461 | /* widget w is freed by soc-dapm.c */ | |
462 | } | |
463 | ||
64527e8a ML |
464 | /* remove DAI configurations */ |
465 | static void remove_dai(struct snd_soc_component *comp, | |
8a978234 LG |
466 | struct snd_soc_dobj *dobj, int pass) |
467 | { | |
64527e8a ML |
468 | struct snd_soc_dai_driver *dai_drv = |
469 | container_of(dobj, struct snd_soc_dai_driver, dobj); | |
470 | ||
8a978234 LG |
471 | if (pass != SOC_TPLG_PASS_PCM_DAI) |
472 | return; | |
473 | ||
64527e8a ML |
474 | if (dobj->ops && dobj->ops->dai_unload) |
475 | dobj->ops->dai_unload(comp, dobj); | |
8a978234 LG |
476 | |
477 | list_del(&dobj->list); | |
64527e8a | 478 | kfree(dai_drv); |
8a978234 LG |
479 | } |
480 | ||
acfc7d46 ML |
481 | /* remove link configurations */ |
482 | static void remove_link(struct snd_soc_component *comp, | |
483 | struct snd_soc_dobj *dobj, int pass) | |
484 | { | |
485 | struct snd_soc_dai_link *link = | |
486 | container_of(dobj, struct snd_soc_dai_link, dobj); | |
487 | ||
488 | if (pass != SOC_TPLG_PASS_PCM_DAI) | |
489 | return; | |
490 | ||
491 | if (dobj->ops && dobj->ops->link_unload) | |
492 | dobj->ops->link_unload(comp, dobj); | |
493 | ||
494 | list_del(&dobj->list); | |
495 | snd_soc_remove_dai_link(comp->card, link); | |
496 | kfree(link); | |
497 | } | |
498 | ||
8a978234 LG |
499 | /* bind a kcontrol to it's IO handlers */ |
500 | static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr, | |
501 | struct snd_kcontrol_new *k, | |
2b5cdb91 | 502 | const struct soc_tplg *tplg) |
8a978234 | 503 | { |
2b5cdb91 | 504 | const struct snd_soc_tplg_kcontrol_ops *ops; |
1a3232d2 | 505 | const struct snd_soc_tplg_bytes_ext_ops *ext_ops; |
2b5cdb91 | 506 | int num_ops, i; |
8a978234 | 507 | |
1a3232d2 ML |
508 | if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES |
509 | && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER | |
510 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE | |
511 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { | |
512 | struct soc_bytes_ext *sbe; | |
513 | struct snd_soc_tplg_bytes_control *be; | |
514 | ||
515 | sbe = (struct soc_bytes_ext *)k->private_value; | |
516 | be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); | |
517 | ||
518 | /* TLV bytes controls need standard kcontrol info handler, | |
519 | * TLV callback and extended put/get handlers. | |
520 | */ | |
f4be978b | 521 | k->info = snd_soc_bytes_info_ext; |
1a3232d2 ML |
522 | k->tlv.c = snd_soc_bytes_tlv_callback; |
523 | ||
524 | ext_ops = tplg->bytes_ext_ops; | |
525 | num_ops = tplg->bytes_ext_ops_count; | |
526 | for (i = 0; i < num_ops; i++) { | |
527 | if (!sbe->put && ext_ops[i].id == be->ext_ops.put) | |
528 | sbe->put = ext_ops[i].put; | |
529 | if (!sbe->get && ext_ops[i].id == be->ext_ops.get) | |
530 | sbe->get = ext_ops[i].get; | |
531 | } | |
532 | ||
533 | if (sbe->put && sbe->get) | |
534 | return 0; | |
535 | else | |
536 | return -EINVAL; | |
537 | } | |
8a978234 | 538 | |
88a17d8f | 539 | /* try and map vendor specific kcontrol handlers first */ |
2b5cdb91 ML |
540 | ops = tplg->io_ops; |
541 | num_ops = tplg->io_ops_count; | |
8a978234 LG |
542 | for (i = 0; i < num_ops; i++) { |
543 | ||
2b5cdb91 | 544 | if (k->put == NULL && ops[i].id == hdr->ops.put) |
8a978234 | 545 | k->put = ops[i].put; |
2b5cdb91 | 546 | if (k->get == NULL && ops[i].id == hdr->ops.get) |
8a978234 | 547 | k->get = ops[i].get; |
2b5cdb91 | 548 | if (k->info == NULL && ops[i].id == hdr->ops.info) |
8a978234 LG |
549 | k->info = ops[i].info; |
550 | } | |
551 | ||
88a17d8f | 552 | /* vendor specific handlers found ? */ |
8a978234 LG |
553 | if (k->put && k->get && k->info) |
554 | return 0; | |
555 | ||
88a17d8f | 556 | /* none found so try standard kcontrol handlers */ |
2b5cdb91 ML |
557 | ops = io_ops; |
558 | num_ops = ARRAY_SIZE(io_ops); | |
88a17d8f | 559 | for (i = 0; i < num_ops; i++) { |
8a978234 | 560 | |
88a17d8f ML |
561 | if (k->put == NULL && ops[i].id == hdr->ops.put) |
562 | k->put = ops[i].put; | |
563 | if (k->get == NULL && ops[i].id == hdr->ops.get) | |
564 | k->get = ops[i].get; | |
8a978234 | 565 | if (k->info == NULL && ops[i].id == hdr->ops.info) |
88a17d8f | 566 | k->info = ops[i].info; |
8a978234 LG |
567 | } |
568 | ||
88a17d8f | 569 | /* standard handlers found ? */ |
8a978234 LG |
570 | if (k->put && k->get && k->info) |
571 | return 0; | |
572 | ||
573 | /* nothing to bind */ | |
574 | return -EINVAL; | |
575 | } | |
576 | ||
577 | /* bind a widgets to it's evnt handlers */ | |
578 | int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w, | |
579 | const struct snd_soc_tplg_widget_events *events, | |
580 | int num_events, u16 event_type) | |
581 | { | |
582 | int i; | |
583 | ||
584 | w->event = NULL; | |
585 | ||
586 | for (i = 0; i < num_events; i++) { | |
587 | if (event_type == events[i].type) { | |
588 | ||
589 | /* found - so assign event */ | |
590 | w->event = events[i].event_handler; | |
591 | return 0; | |
592 | } | |
593 | } | |
594 | ||
595 | /* not found */ | |
596 | return -EINVAL; | |
597 | } | |
598 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event); | |
599 | ||
600 | /* optionally pass new dynamic kcontrol to component driver. */ | |
601 | static int soc_tplg_init_kcontrol(struct soc_tplg *tplg, | |
602 | struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr) | |
603 | { | |
604 | if (tplg->comp && tplg->ops && tplg->ops->control_load) | |
605 | return tplg->ops->control_load(tplg->comp, k, hdr); | |
606 | ||
607 | return 0; | |
608 | } | |
609 | ||
8a978234 | 610 | |
28a87eeb ML |
611 | static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg, |
612 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale) | |
613 | { | |
614 | unsigned int item_len = 2 * sizeof(unsigned int); | |
615 | unsigned int *p; | |
8a978234 | 616 | |
28a87eeb ML |
617 | p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL); |
618 | if (!p) | |
8a978234 LG |
619 | return -ENOMEM; |
620 | ||
28a87eeb ML |
621 | p[0] = SNDRV_CTL_TLVT_DB_SCALE; |
622 | p[1] = item_len; | |
623 | p[2] = scale->min; | |
624 | p[3] = (scale->step & TLV_DB_SCALE_MASK) | |
625 | | (scale->mute ? TLV_DB_SCALE_MUTE : 0); | |
626 | ||
627 | kc->tlv.p = (void *)p; | |
628 | return 0; | |
629 | } | |
630 | ||
631 | static int soc_tplg_create_tlv(struct soc_tplg *tplg, | |
632 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc) | |
633 | { | |
634 | struct snd_soc_tplg_ctl_tlv *tplg_tlv; | |
635 | ||
636 | if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) | |
637 | return 0; | |
8a978234 | 638 | |
1a3232d2 | 639 | if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) { |
28a87eeb ML |
640 | tplg_tlv = &tc->tlv; |
641 | switch (tplg_tlv->type) { | |
642 | case SNDRV_CTL_TLVT_DB_SCALE: | |
643 | return soc_tplg_create_tlv_db_scale(tplg, kc, | |
644 | &tplg_tlv->scale); | |
645 | ||
646 | /* TODO: add support for other TLV types */ | |
647 | default: | |
648 | dev_dbg(tplg->dev, "Unsupported TLV type %d\n", | |
649 | tplg_tlv->type); | |
650 | return -EINVAL; | |
651 | } | |
652 | } | |
8a978234 LG |
653 | |
654 | return 0; | |
655 | } | |
656 | ||
657 | static inline void soc_tplg_free_tlv(struct soc_tplg *tplg, | |
658 | struct snd_kcontrol_new *kc) | |
659 | { | |
660 | kfree(kc->tlv.p); | |
661 | } | |
662 | ||
663 | static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count, | |
664 | size_t size) | |
665 | { | |
666 | struct snd_soc_tplg_bytes_control *be; | |
667 | struct soc_bytes_ext *sbe; | |
668 | struct snd_kcontrol_new kc; | |
669 | int i, err; | |
670 | ||
671 | if (soc_tplg_check_elem_count(tplg, | |
672 | sizeof(struct snd_soc_tplg_bytes_control), count, | |
673 | size, "mixer bytes")) { | |
674 | dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n", | |
675 | count); | |
676 | return -EINVAL; | |
677 | } | |
678 | ||
679 | for (i = 0; i < count; i++) { | |
680 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; | |
681 | ||
682 | /* validate kcontrol */ | |
683 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
684 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
685 | return -EINVAL; | |
686 | ||
687 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); | |
688 | if (sbe == NULL) | |
689 | return -ENOMEM; | |
690 | ||
691 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + | |
692 | be->priv.size); | |
693 | ||
694 | dev_dbg(tplg->dev, | |
695 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", | |
696 | be->hdr.name, be->hdr.access); | |
697 | ||
698 | memset(&kc, 0, sizeof(kc)); | |
699 | kc.name = be->hdr.name; | |
700 | kc.private_value = (long)sbe; | |
701 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
702 | kc.access = be->hdr.access; | |
703 | ||
704 | sbe->max = be->max; | |
705 | sbe->dobj.type = SND_SOC_DOBJ_BYTES; | |
706 | sbe->dobj.ops = tplg->ops; | |
707 | INIT_LIST_HEAD(&sbe->dobj.list); | |
708 | ||
709 | /* map io handlers */ | |
2b5cdb91 | 710 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg); |
8a978234 LG |
711 | if (err) { |
712 | soc_control_err(tplg, &be->hdr, be->hdr.name); | |
713 | kfree(sbe); | |
714 | continue; | |
715 | } | |
716 | ||
717 | /* pass control to driver for optional further init */ | |
718 | err = soc_tplg_init_kcontrol(tplg, &kc, | |
719 | (struct snd_soc_tplg_ctl_hdr *)be); | |
720 | if (err < 0) { | |
721 | dev_err(tplg->dev, "ASoC: failed to init %s\n", | |
722 | be->hdr.name); | |
723 | kfree(sbe); | |
724 | continue; | |
725 | } | |
726 | ||
727 | /* register control here */ | |
728 | err = soc_tplg_add_kcontrol(tplg, &kc, | |
729 | &sbe->dobj.control.kcontrol); | |
730 | if (err < 0) { | |
731 | dev_err(tplg->dev, "ASoC: failed to add %s\n", | |
732 | be->hdr.name); | |
733 | kfree(sbe); | |
734 | continue; | |
735 | } | |
736 | ||
737 | list_add(&sbe->dobj.list, &tplg->comp->dobj_list); | |
738 | } | |
739 | return 0; | |
740 | ||
741 | } | |
742 | ||
743 | static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count, | |
744 | size_t size) | |
745 | { | |
746 | struct snd_soc_tplg_mixer_control *mc; | |
747 | struct soc_mixer_control *sm; | |
748 | struct snd_kcontrol_new kc; | |
749 | int i, err; | |
750 | ||
751 | if (soc_tplg_check_elem_count(tplg, | |
752 | sizeof(struct snd_soc_tplg_mixer_control), | |
753 | count, size, "mixers")) { | |
754 | ||
755 | dev_err(tplg->dev, "ASoC: invalid count %d for controls\n", | |
756 | count); | |
757 | return -EINVAL; | |
758 | } | |
759 | ||
760 | for (i = 0; i < count; i++) { | |
761 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; | |
762 | ||
763 | /* validate kcontrol */ | |
764 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
765 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
766 | return -EINVAL; | |
767 | ||
768 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); | |
769 | if (sm == NULL) | |
770 | return -ENOMEM; | |
771 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + | |
772 | mc->priv.size); | |
773 | ||
774 | dev_dbg(tplg->dev, | |
775 | "ASoC: adding mixer kcontrol %s with access 0x%x\n", | |
776 | mc->hdr.name, mc->hdr.access); | |
777 | ||
778 | memset(&kc, 0, sizeof(kc)); | |
779 | kc.name = mc->hdr.name; | |
780 | kc.private_value = (long)sm; | |
781 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
782 | kc.access = mc->hdr.access; | |
783 | ||
784 | /* we only support FL/FR channel mapping atm */ | |
785 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, | |
786 | SNDRV_CHMAP_FL); | |
787 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, | |
788 | SNDRV_CHMAP_FR); | |
789 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, | |
790 | SNDRV_CHMAP_FL); | |
791 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, | |
792 | SNDRV_CHMAP_FR); | |
793 | ||
794 | sm->max = mc->max; | |
795 | sm->min = mc->min; | |
796 | sm->invert = mc->invert; | |
797 | sm->platform_max = mc->platform_max; | |
798 | sm->dobj.index = tplg->index; | |
799 | sm->dobj.ops = tplg->ops; | |
800 | sm->dobj.type = SND_SOC_DOBJ_MIXER; | |
801 | INIT_LIST_HEAD(&sm->dobj.list); | |
802 | ||
803 | /* map io handlers */ | |
2b5cdb91 | 804 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg); |
8a978234 LG |
805 | if (err) { |
806 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); | |
807 | kfree(sm); | |
808 | continue; | |
809 | } | |
810 | ||
811 | /* pass control to driver for optional further init */ | |
812 | err = soc_tplg_init_kcontrol(tplg, &kc, | |
813 | (struct snd_soc_tplg_ctl_hdr *) mc); | |
814 | if (err < 0) { | |
815 | dev_err(tplg->dev, "ASoC: failed to init %s\n", | |
816 | mc->hdr.name); | |
817 | kfree(sm); | |
818 | continue; | |
819 | } | |
820 | ||
821 | /* create any TLV data */ | |
28a87eeb | 822 | soc_tplg_create_tlv(tplg, &kc, &mc->hdr); |
8a978234 LG |
823 | |
824 | /* register control here */ | |
825 | err = soc_tplg_add_kcontrol(tplg, &kc, | |
826 | &sm->dobj.control.kcontrol); | |
827 | if (err < 0) { | |
828 | dev_err(tplg->dev, "ASoC: failed to add %s\n", | |
829 | mc->hdr.name); | |
830 | soc_tplg_free_tlv(tplg, &kc); | |
831 | kfree(sm); | |
832 | continue; | |
833 | } | |
834 | ||
835 | list_add(&sm->dobj.list, &tplg->comp->dobj_list); | |
836 | } | |
837 | ||
838 | return 0; | |
839 | } | |
840 | ||
841 | static int soc_tplg_denum_create_texts(struct soc_enum *se, | |
842 | struct snd_soc_tplg_enum_control *ec) | |
843 | { | |
844 | int i, ret; | |
845 | ||
846 | se->dobj.control.dtexts = | |
847 | kzalloc(sizeof(char *) * ec->items, GFP_KERNEL); | |
848 | if (se->dobj.control.dtexts == NULL) | |
849 | return -ENOMEM; | |
850 | ||
851 | for (i = 0; i < ec->items; i++) { | |
852 | ||
853 | if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
854 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { | |
855 | ret = -EINVAL; | |
856 | goto err; | |
857 | } | |
858 | ||
859 | se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL); | |
860 | if (!se->dobj.control.dtexts[i]) { | |
861 | ret = -ENOMEM; | |
862 | goto err; | |
863 | } | |
864 | } | |
865 | ||
866 | return 0; | |
867 | ||
868 | err: | |
869 | for (--i; i >= 0; i--) | |
870 | kfree(se->dobj.control.dtexts[i]); | |
871 | kfree(se->dobj.control.dtexts); | |
872 | return ret; | |
873 | } | |
874 | ||
875 | static int soc_tplg_denum_create_values(struct soc_enum *se, | |
876 | struct snd_soc_tplg_enum_control *ec) | |
877 | { | |
878 | if (ec->items > sizeof(*ec->values)) | |
879 | return -EINVAL; | |
880 | ||
376c0afe AH |
881 | se->dobj.control.dvalues = kmemdup(ec->values, |
882 | ec->items * sizeof(u32), | |
883 | GFP_KERNEL); | |
8a978234 LG |
884 | if (!se->dobj.control.dvalues) |
885 | return -ENOMEM; | |
886 | ||
8a978234 LG |
887 | return 0; |
888 | } | |
889 | ||
890 | static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count, | |
891 | size_t size) | |
892 | { | |
893 | struct snd_soc_tplg_enum_control *ec; | |
894 | struct soc_enum *se; | |
895 | struct snd_kcontrol_new kc; | |
896 | int i, ret, err; | |
897 | ||
898 | if (soc_tplg_check_elem_count(tplg, | |
899 | sizeof(struct snd_soc_tplg_enum_control), | |
900 | count, size, "enums")) { | |
901 | ||
902 | dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n", | |
903 | count); | |
904 | return -EINVAL; | |
905 | } | |
906 | ||
907 | for (i = 0; i < count; i++) { | |
908 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; | |
909 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + | |
910 | ec->priv.size); | |
911 | ||
912 | /* validate kcontrol */ | |
913 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
914 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
915 | return -EINVAL; | |
916 | ||
917 | se = kzalloc((sizeof(*se)), GFP_KERNEL); | |
918 | if (se == NULL) | |
919 | return -ENOMEM; | |
920 | ||
921 | dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", | |
922 | ec->hdr.name, ec->items); | |
923 | ||
924 | memset(&kc, 0, sizeof(kc)); | |
925 | kc.name = ec->hdr.name; | |
926 | kc.private_value = (long)se; | |
927 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
928 | kc.access = ec->hdr.access; | |
929 | ||
930 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); | |
931 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, | |
932 | SNDRV_CHMAP_FL); | |
933 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, | |
934 | SNDRV_CHMAP_FL); | |
935 | ||
936 | se->items = ec->items; | |
937 | se->mask = ec->mask; | |
938 | se->dobj.index = tplg->index; | |
939 | se->dobj.type = SND_SOC_DOBJ_ENUM; | |
940 | se->dobj.ops = tplg->ops; | |
941 | INIT_LIST_HEAD(&se->dobj.list); | |
942 | ||
943 | switch (ec->hdr.ops.info) { | |
944 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: | |
945 | case SND_SOC_TPLG_CTL_ENUM_VALUE: | |
946 | err = soc_tplg_denum_create_values(se, ec); | |
947 | if (err < 0) { | |
948 | dev_err(tplg->dev, | |
949 | "ASoC: could not create values for %s\n", | |
950 | ec->hdr.name); | |
951 | kfree(se); | |
952 | continue; | |
953 | } | |
954 | /* fall through and create texts */ | |
955 | case SND_SOC_TPLG_CTL_ENUM: | |
956 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: | |
957 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: | |
958 | err = soc_tplg_denum_create_texts(se, ec); | |
959 | if (err < 0) { | |
960 | dev_err(tplg->dev, | |
961 | "ASoC: could not create texts for %s\n", | |
962 | ec->hdr.name); | |
963 | kfree(se); | |
964 | continue; | |
965 | } | |
966 | break; | |
967 | default: | |
968 | dev_err(tplg->dev, | |
969 | "ASoC: invalid enum control type %d for %s\n", | |
970 | ec->hdr.ops.info, ec->hdr.name); | |
971 | kfree(se); | |
972 | continue; | |
973 | } | |
974 | ||
975 | /* map io handlers */ | |
2b5cdb91 | 976 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg); |
8a978234 LG |
977 | if (err) { |
978 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); | |
979 | kfree(se); | |
980 | continue; | |
981 | } | |
982 | ||
983 | /* pass control to driver for optional further init */ | |
984 | err = soc_tplg_init_kcontrol(tplg, &kc, | |
985 | (struct snd_soc_tplg_ctl_hdr *) ec); | |
986 | if (err < 0) { | |
987 | dev_err(tplg->dev, "ASoC: failed to init %s\n", | |
988 | ec->hdr.name); | |
989 | kfree(se); | |
990 | continue; | |
991 | } | |
992 | ||
993 | /* register control here */ | |
994 | ret = soc_tplg_add_kcontrol(tplg, | |
995 | &kc, &se->dobj.control.kcontrol); | |
996 | if (ret < 0) { | |
997 | dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n", | |
998 | ec->hdr.name); | |
999 | kfree(se); | |
1000 | continue; | |
1001 | } | |
1002 | ||
1003 | list_add(&se->dobj.list, &tplg->comp->dobj_list); | |
1004 | } | |
1005 | ||
1006 | return 0; | |
1007 | } | |
1008 | ||
1009 | static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg, | |
1010 | struct snd_soc_tplg_hdr *hdr) | |
1011 | { | |
1012 | struct snd_soc_tplg_ctl_hdr *control_hdr; | |
1013 | int i; | |
1014 | ||
1015 | if (tplg->pass != SOC_TPLG_PASS_MIXER) { | |
1016 | tplg->pos += hdr->size + hdr->payload_size; | |
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count, | |
1021 | soc_tplg_get_offset(tplg)); | |
1022 | ||
1023 | for (i = 0; i < hdr->count; i++) { | |
1024 | ||
1025 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; | |
1026 | ||
06eb49f7 ML |
1027 | if (control_hdr->size != sizeof(*control_hdr)) { |
1028 | dev_err(tplg->dev, "ASoC: invalid control size\n"); | |
1029 | return -EINVAL; | |
1030 | } | |
1031 | ||
8a978234 LG |
1032 | switch (control_hdr->ops.info) { |
1033 | case SND_SOC_TPLG_CTL_VOLSW: | |
1034 | case SND_SOC_TPLG_CTL_STROBE: | |
1035 | case SND_SOC_TPLG_CTL_VOLSW_SX: | |
1036 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: | |
1037 | case SND_SOC_TPLG_CTL_RANGE: | |
1038 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: | |
1039 | case SND_SOC_TPLG_DAPM_CTL_PIN: | |
1040 | soc_tplg_dmixer_create(tplg, 1, hdr->payload_size); | |
1041 | break; | |
1042 | case SND_SOC_TPLG_CTL_ENUM: | |
1043 | case SND_SOC_TPLG_CTL_ENUM_VALUE: | |
1044 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: | |
1045 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: | |
1046 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: | |
1047 | soc_tplg_denum_create(tplg, 1, hdr->payload_size); | |
1048 | break; | |
1049 | case SND_SOC_TPLG_CTL_BYTES: | |
1050 | soc_tplg_dbytes_create(tplg, 1, hdr->payload_size); | |
1051 | break; | |
1052 | default: | |
1053 | soc_bind_err(tplg, control_hdr, i); | |
1054 | return -EINVAL; | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | return 0; | |
1059 | } | |
1060 | ||
1061 | static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, | |
1062 | struct snd_soc_tplg_hdr *hdr) | |
1063 | { | |
1064 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; | |
1065 | struct snd_soc_dapm_route route; | |
1066 | struct snd_soc_tplg_dapm_graph_elem *elem; | |
1067 | int count = hdr->count, i; | |
1068 | ||
1069 | if (tplg->pass != SOC_TPLG_PASS_GRAPH) { | |
1070 | tplg->pos += hdr->size + hdr->payload_size; | |
1071 | return 0; | |
1072 | } | |
1073 | ||
1074 | if (soc_tplg_check_elem_count(tplg, | |
1075 | sizeof(struct snd_soc_tplg_dapm_graph_elem), | |
1076 | count, hdr->payload_size, "graph")) { | |
1077 | ||
1078 | dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n", | |
1079 | count); | |
1080 | return -EINVAL; | |
1081 | } | |
1082 | ||
1083 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count); | |
1084 | ||
1085 | for (i = 0; i < count; i++) { | |
1086 | elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; | |
1087 | tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); | |
1088 | ||
1089 | /* validate routes */ | |
1090 | if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
1091 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
1092 | return -EINVAL; | |
1093 | if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
1094 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
1095 | return -EINVAL; | |
1096 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
1097 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
1098 | return -EINVAL; | |
1099 | ||
1100 | route.source = elem->source; | |
1101 | route.sink = elem->sink; | |
1102 | route.connected = NULL; /* set to NULL atm for tplg users */ | |
1103 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0) | |
1104 | route.control = NULL; | |
1105 | else | |
1106 | route.control = elem->control; | |
1107 | ||
1108 | /* add route, but keep going if some fail */ | |
1109 | snd_soc_dapm_add_routes(dapm, &route, 1); | |
1110 | } | |
1111 | ||
1112 | return 0; | |
1113 | } | |
1114 | ||
1115 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( | |
1116 | struct soc_tplg *tplg, int num_kcontrols) | |
1117 | { | |
1118 | struct snd_kcontrol_new *kc; | |
1119 | struct soc_mixer_control *sm; | |
1120 | struct snd_soc_tplg_mixer_control *mc; | |
1121 | int i, err; | |
1122 | ||
4ca7deb1 | 1123 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
8a978234 LG |
1124 | if (kc == NULL) |
1125 | return NULL; | |
1126 | ||
1127 | for (i = 0; i < num_kcontrols; i++) { | |
1128 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; | |
1129 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); | |
1130 | if (sm == NULL) | |
1131 | goto err; | |
1132 | ||
1133 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + | |
1134 | mc->priv.size); | |
1135 | ||
1136 | /* validate kcontrol */ | |
1137 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
1138 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
1139 | goto err_str; | |
1140 | ||
1141 | dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n", | |
1142 | mc->hdr.name, i); | |
1143 | ||
1144 | kc[i].name = mc->hdr.name; | |
1145 | kc[i].private_value = (long)sm; | |
1146 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
1147 | kc[i].access = mc->hdr.access; | |
1148 | ||
1149 | /* we only support FL/FR channel mapping atm */ | |
1150 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, | |
1151 | SNDRV_CHMAP_FL); | |
1152 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, | |
1153 | SNDRV_CHMAP_FR); | |
1154 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, | |
1155 | SNDRV_CHMAP_FL); | |
1156 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, | |
1157 | SNDRV_CHMAP_FR); | |
1158 | ||
1159 | sm->max = mc->max; | |
1160 | sm->min = mc->min; | |
1161 | sm->invert = mc->invert; | |
1162 | sm->platform_max = mc->platform_max; | |
1163 | sm->dobj.index = tplg->index; | |
1164 | INIT_LIST_HEAD(&sm->dobj.list); | |
1165 | ||
1166 | /* map io handlers */ | |
2b5cdb91 | 1167 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg); |
8a978234 LG |
1168 | if (err) { |
1169 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); | |
1170 | kfree(sm); | |
1171 | continue; | |
1172 | } | |
1173 | ||
1174 | /* pass control to driver for optional further init */ | |
1175 | err = soc_tplg_init_kcontrol(tplg, &kc[i], | |
1176 | (struct snd_soc_tplg_ctl_hdr *)mc); | |
1177 | if (err < 0) { | |
1178 | dev_err(tplg->dev, "ASoC: failed to init %s\n", | |
1179 | mc->hdr.name); | |
1180 | kfree(sm); | |
1181 | continue; | |
1182 | } | |
1183 | } | |
1184 | return kc; | |
1185 | ||
1186 | err_str: | |
1187 | kfree(sm); | |
1188 | err: | |
1189 | for (--i; i >= 0; i--) | |
1190 | kfree((void *)kc[i].private_value); | |
1191 | kfree(kc); | |
1192 | return NULL; | |
1193 | } | |
1194 | ||
1195 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( | |
1196 | struct soc_tplg *tplg) | |
1197 | { | |
1198 | struct snd_kcontrol_new *kc; | |
1199 | struct snd_soc_tplg_enum_control *ec; | |
1200 | struct soc_enum *se; | |
1201 | int i, err; | |
1202 | ||
1203 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; | |
1204 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + | |
1205 | ec->priv.size); | |
1206 | ||
1207 | /* validate kcontrol */ | |
1208 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
1209 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
1210 | return NULL; | |
1211 | ||
1212 | kc = kzalloc(sizeof(*kc), GFP_KERNEL); | |
1213 | if (kc == NULL) | |
1214 | return NULL; | |
1215 | ||
1216 | se = kzalloc(sizeof(*se), GFP_KERNEL); | |
1217 | if (se == NULL) | |
1218 | goto err; | |
1219 | ||
1220 | dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n", | |
1221 | ec->hdr.name); | |
1222 | ||
1223 | kc->name = ec->hdr.name; | |
1224 | kc->private_value = (long)se; | |
1225 | kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
1226 | kc->access = ec->hdr.access; | |
1227 | ||
1228 | /* we only support FL/FR channel mapping atm */ | |
1229 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); | |
1230 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL); | |
1231 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR); | |
1232 | ||
1233 | se->items = ec->items; | |
1234 | se->mask = ec->mask; | |
1235 | se->dobj.index = tplg->index; | |
1236 | ||
1237 | switch (ec->hdr.ops.info) { | |
1238 | case SND_SOC_TPLG_CTL_ENUM_VALUE: | |
1239 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: | |
1240 | err = soc_tplg_denum_create_values(se, ec); | |
1241 | if (err < 0) { | |
1242 | dev_err(tplg->dev, "ASoC: could not create values for %s\n", | |
1243 | ec->hdr.name); | |
1244 | goto err_se; | |
1245 | } | |
1246 | /* fall through to create texts */ | |
1247 | case SND_SOC_TPLG_CTL_ENUM: | |
1248 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: | |
1249 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: | |
1250 | err = soc_tplg_denum_create_texts(se, ec); | |
1251 | if (err < 0) { | |
1252 | dev_err(tplg->dev, "ASoC: could not create texts for %s\n", | |
1253 | ec->hdr.name); | |
1254 | goto err_se; | |
1255 | } | |
1256 | break; | |
1257 | default: | |
1258 | dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n", | |
1259 | ec->hdr.ops.info, ec->hdr.name); | |
1260 | goto err_se; | |
1261 | } | |
1262 | ||
1263 | /* map io handlers */ | |
2b5cdb91 | 1264 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg); |
8a978234 LG |
1265 | if (err) { |
1266 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); | |
1267 | goto err_se; | |
1268 | } | |
1269 | ||
1270 | /* pass control to driver for optional further init */ | |
1271 | err = soc_tplg_init_kcontrol(tplg, kc, | |
1272 | (struct snd_soc_tplg_ctl_hdr *)ec); | |
1273 | if (err < 0) { | |
1274 | dev_err(tplg->dev, "ASoC: failed to init %s\n", | |
1275 | ec->hdr.name); | |
1276 | goto err_se; | |
1277 | } | |
1278 | ||
1279 | return kc; | |
1280 | ||
1281 | err_se: | |
1282 | /* free values and texts */ | |
1283 | kfree(se->dobj.control.dvalues); | |
1284 | for (i = 0; i < ec->items; i++) | |
1285 | kfree(se->dobj.control.dtexts[i]); | |
1286 | ||
1287 | kfree(se); | |
1288 | err: | |
1289 | kfree(kc); | |
1290 | ||
1291 | return NULL; | |
1292 | } | |
1293 | ||
1294 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( | |
1295 | struct soc_tplg *tplg, int count) | |
1296 | { | |
1297 | struct snd_soc_tplg_bytes_control *be; | |
1298 | struct soc_bytes_ext *sbe; | |
1299 | struct snd_kcontrol_new *kc; | |
1300 | int i, err; | |
1301 | ||
4ca7deb1 | 1302 | kc = kcalloc(count, sizeof(*kc), GFP_KERNEL); |
8a978234 LG |
1303 | if (!kc) |
1304 | return NULL; | |
1305 | ||
1306 | for (i = 0; i < count; i++) { | |
1307 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; | |
1308 | ||
1309 | /* validate kcontrol */ | |
1310 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
1311 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
1312 | goto err; | |
1313 | ||
1314 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); | |
1315 | if (sbe == NULL) | |
1316 | goto err; | |
1317 | ||
1318 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + | |
1319 | be->priv.size); | |
1320 | ||
1321 | dev_dbg(tplg->dev, | |
1322 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", | |
1323 | be->hdr.name, be->hdr.access); | |
1324 | ||
8a978234 LG |
1325 | kc[i].name = be->hdr.name; |
1326 | kc[i].private_value = (long)sbe; | |
1327 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
1328 | kc[i].access = be->hdr.access; | |
1329 | ||
1330 | sbe->max = be->max; | |
1331 | INIT_LIST_HEAD(&sbe->dobj.list); | |
1332 | ||
1333 | /* map standard io handlers and check for external handlers */ | |
2b5cdb91 | 1334 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg); |
8a978234 LG |
1335 | if (err) { |
1336 | soc_control_err(tplg, &be->hdr, be->hdr.name); | |
1337 | kfree(sbe); | |
1338 | continue; | |
1339 | } | |
1340 | ||
1341 | /* pass control to driver for optional further init */ | |
1342 | err = soc_tplg_init_kcontrol(tplg, &kc[i], | |
1343 | (struct snd_soc_tplg_ctl_hdr *)be); | |
1344 | if (err < 0) { | |
1345 | dev_err(tplg->dev, "ASoC: failed to init %s\n", | |
1346 | be->hdr.name); | |
1347 | kfree(sbe); | |
1348 | continue; | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | return kc; | |
1353 | ||
1354 | err: | |
1355 | for (--i; i >= 0; i--) | |
1356 | kfree((void *)kc[i].private_value); | |
1357 | ||
1358 | kfree(kc); | |
1359 | return NULL; | |
1360 | } | |
1361 | ||
1362 | static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, | |
1363 | struct snd_soc_tplg_dapm_widget *w) | |
1364 | { | |
1365 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; | |
1366 | struct snd_soc_dapm_widget template, *widget; | |
1367 | struct snd_soc_tplg_ctl_hdr *control_hdr; | |
1368 | struct snd_soc_card *card = tplg->comp->card; | |
1369 | int ret = 0; | |
1370 | ||
1371 | if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
1372 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
1373 | return -EINVAL; | |
1374 | if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == | |
1375 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) | |
1376 | return -EINVAL; | |
1377 | ||
1378 | dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n", | |
1379 | w->name, w->id); | |
1380 | ||
1381 | memset(&template, 0, sizeof(template)); | |
1382 | ||
1383 | /* map user to kernel widget ID */ | |
1384 | template.id = get_widget_id(w->id); | |
1385 | if (template.id < 0) | |
1386 | return template.id; | |
1387 | ||
1388 | template.name = kstrdup(w->name, GFP_KERNEL); | |
1389 | if (!template.name) | |
1390 | return -ENOMEM; | |
1391 | template.sname = kstrdup(w->sname, GFP_KERNEL); | |
1392 | if (!template.sname) { | |
1393 | ret = -ENOMEM; | |
1394 | goto err; | |
1395 | } | |
1396 | template.reg = w->reg; | |
1397 | template.shift = w->shift; | |
1398 | template.mask = w->mask; | |
6dc6db79 | 1399 | template.subseq = w->subseq; |
8a978234 LG |
1400 | template.on_val = w->invert ? 0 : 1; |
1401 | template.off_val = w->invert ? 1 : 0; | |
1402 | template.ignore_suspend = w->ignore_suspend; | |
1403 | template.event_flags = w->event_flags; | |
1404 | template.dobj.index = tplg->index; | |
1405 | ||
1406 | tplg->pos += | |
1407 | (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size); | |
1408 | if (w->num_kcontrols == 0) { | |
1409 | template.num_kcontrols = 0; | |
1410 | goto widget; | |
1411 | } | |
1412 | ||
1413 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; | |
1414 | dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n", | |
1415 | w->name, w->num_kcontrols, control_hdr->type); | |
1416 | ||
1417 | switch (control_hdr->ops.info) { | |
1418 | case SND_SOC_TPLG_CTL_VOLSW: | |
1419 | case SND_SOC_TPLG_CTL_STROBE: | |
1420 | case SND_SOC_TPLG_CTL_VOLSW_SX: | |
1421 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: | |
1422 | case SND_SOC_TPLG_CTL_RANGE: | |
1423 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: | |
1424 | template.num_kcontrols = w->num_kcontrols; | |
1425 | template.kcontrol_news = | |
1426 | soc_tplg_dapm_widget_dmixer_create(tplg, | |
1427 | template.num_kcontrols); | |
1428 | if (!template.kcontrol_news) { | |
1429 | ret = -ENOMEM; | |
1430 | goto hdr_err; | |
1431 | } | |
1432 | break; | |
1433 | case SND_SOC_TPLG_CTL_ENUM: | |
1434 | case SND_SOC_TPLG_CTL_ENUM_VALUE: | |
1435 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: | |
1436 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: | |
1437 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: | |
1438 | template.dobj.widget.kcontrol_enum = 1; | |
1439 | template.num_kcontrols = 1; | |
1440 | template.kcontrol_news = | |
1441 | soc_tplg_dapm_widget_denum_create(tplg); | |
1442 | if (!template.kcontrol_news) { | |
1443 | ret = -ENOMEM; | |
1444 | goto hdr_err; | |
1445 | } | |
1446 | break; | |
1447 | case SND_SOC_TPLG_CTL_BYTES: | |
1448 | template.num_kcontrols = w->num_kcontrols; | |
1449 | template.kcontrol_news = | |
1450 | soc_tplg_dapm_widget_dbytes_create(tplg, | |
1451 | template.num_kcontrols); | |
1452 | if (!template.kcontrol_news) { | |
1453 | ret = -ENOMEM; | |
1454 | goto hdr_err; | |
1455 | } | |
1456 | break; | |
1457 | default: | |
1458 | dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n", | |
1459 | control_hdr->ops.get, control_hdr->ops.put, | |
1460 | control_hdr->ops.info); | |
1461 | ret = -EINVAL; | |
1462 | goto hdr_err; | |
1463 | } | |
1464 | ||
1465 | widget: | |
1466 | ret = soc_tplg_widget_load(tplg, &template, w); | |
1467 | if (ret < 0) | |
1468 | goto hdr_err; | |
1469 | ||
1470 | /* card dapm mutex is held by the core if we are loading topology | |
1471 | * data during sound card init. */ | |
1472 | if (card->instantiated) | |
1473 | widget = snd_soc_dapm_new_control(dapm, &template); | |
1474 | else | |
1475 | widget = snd_soc_dapm_new_control_unlocked(dapm, &template); | |
1476 | if (widget == NULL) { | |
1477 | dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n", | |
1478 | w->name); | |
8ae3ea48 | 1479 | ret = -ENOMEM; |
8a978234 LG |
1480 | goto hdr_err; |
1481 | } | |
1482 | ||
1483 | widget->dobj.type = SND_SOC_DOBJ_WIDGET; | |
1484 | widget->dobj.ops = tplg->ops; | |
1485 | widget->dobj.index = tplg->index; | |
8ea41674 JK |
1486 | kfree(template.sname); |
1487 | kfree(template.name); | |
8a978234 LG |
1488 | list_add(&widget->dobj.list, &tplg->comp->dobj_list); |
1489 | return 0; | |
1490 | ||
1491 | hdr_err: | |
1492 | kfree(template.sname); | |
1493 | err: | |
1494 | kfree(template.name); | |
1495 | return ret; | |
1496 | } | |
1497 | ||
1498 | static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, | |
1499 | struct snd_soc_tplg_hdr *hdr) | |
1500 | { | |
1501 | struct snd_soc_tplg_dapm_widget *widget; | |
1502 | int ret, count = hdr->count, i; | |
1503 | ||
1504 | if (tplg->pass != SOC_TPLG_PASS_WIDGET) | |
1505 | return 0; | |
1506 | ||
1507 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); | |
1508 | ||
1509 | for (i = 0; i < count; i++) { | |
1510 | widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; | |
06eb49f7 ML |
1511 | if (widget->size != sizeof(*widget)) { |
1512 | dev_err(tplg->dev, "ASoC: invalid widget size\n"); | |
1513 | return -EINVAL; | |
1514 | } | |
1515 | ||
8a978234 | 1516 | ret = soc_tplg_dapm_widget_create(tplg, widget); |
7de76b62 | 1517 | if (ret < 0) { |
8a978234 LG |
1518 | dev_err(tplg->dev, "ASoC: failed to load widget %s\n", |
1519 | widget->name); | |
7de76b62 ML |
1520 | return ret; |
1521 | } | |
8a978234 LG |
1522 | } |
1523 | ||
1524 | return 0; | |
1525 | } | |
1526 | ||
1527 | static int soc_tplg_dapm_complete(struct soc_tplg *tplg) | |
1528 | { | |
1529 | struct snd_soc_card *card = tplg->comp->card; | |
1530 | int ret; | |
1531 | ||
1532 | /* Card might not have been registered at this point. | |
1533 | * If so, just return success. | |
1534 | */ | |
1535 | if (!card || !card->instantiated) { | |
1536 | dev_warn(tplg->dev, "ASoC: Parent card not yet available," | |
1537 | "Do not add new widgets now\n"); | |
1538 | return 0; | |
1539 | } | |
1540 | ||
1541 | ret = snd_soc_dapm_new_widgets(card); | |
1542 | if (ret < 0) | |
1543 | dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", | |
1544 | ret); | |
1545 | ||
1546 | return 0; | |
1547 | } | |
1548 | ||
b6b6e4d6 ML |
1549 | static void set_stream_info(struct snd_soc_pcm_stream *stream, |
1550 | struct snd_soc_tplg_stream_caps *caps) | |
1551 | { | |
1552 | stream->stream_name = kstrdup(caps->name, GFP_KERNEL); | |
1553 | stream->channels_min = caps->channels_min; | |
1554 | stream->channels_max = caps->channels_max; | |
1555 | stream->rates = caps->rates; | |
1556 | stream->rate_min = caps->rate_min; | |
1557 | stream->rate_max = caps->rate_max; | |
1558 | stream->formats = caps->formats; | |
1559 | } | |
1560 | ||
0038be9a ML |
1561 | static void set_dai_flags(struct snd_soc_dai_driver *dai_drv, |
1562 | unsigned int flag_mask, unsigned int flags) | |
1563 | { | |
1564 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) | |
1565 | dai_drv->symmetric_rates = | |
1566 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0; | |
1567 | ||
1568 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) | |
1569 | dai_drv->symmetric_channels = | |
1570 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ? | |
1571 | 1 : 0; | |
1572 | ||
1573 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) | |
1574 | dai_drv->symmetric_samplebits = | |
1575 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ? | |
1576 | 1 : 0; | |
1577 | } | |
1578 | ||
64527e8a ML |
1579 | static int soc_tplg_dai_create(struct soc_tplg *tplg, |
1580 | struct snd_soc_tplg_pcm *pcm) | |
1581 | { | |
1582 | struct snd_soc_dai_driver *dai_drv; | |
1583 | struct snd_soc_pcm_stream *stream; | |
1584 | struct snd_soc_tplg_stream_caps *caps; | |
1585 | int ret; | |
1586 | ||
1587 | dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL); | |
1588 | if (dai_drv == NULL) | |
1589 | return -ENOMEM; | |
1590 | ||
1591 | dai_drv->name = pcm->dai_name; | |
1592 | dai_drv->id = pcm->dai_id; | |
1593 | ||
1594 | if (pcm->playback) { | |
1595 | stream = &dai_drv->playback; | |
1596 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; | |
b6b6e4d6 | 1597 | set_stream_info(stream, caps); |
64527e8a ML |
1598 | } |
1599 | ||
1600 | if (pcm->capture) { | |
1601 | stream = &dai_drv->capture; | |
1602 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; | |
b6b6e4d6 | 1603 | set_stream_info(stream, caps); |
64527e8a ML |
1604 | } |
1605 | ||
1606 | /* pass control to component driver for optional further init */ | |
1607 | ret = soc_tplg_dai_load(tplg, dai_drv); | |
1608 | if (ret < 0) { | |
1609 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); | |
1610 | kfree(dai_drv); | |
1611 | return ret; | |
1612 | } | |
1613 | ||
1614 | dai_drv->dobj.index = tplg->index; | |
1615 | dai_drv->dobj.ops = tplg->ops; | |
1616 | dai_drv->dobj.type = SND_SOC_DOBJ_PCM; | |
1617 | list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); | |
1618 | ||
1619 | /* register the DAI to the component */ | |
1620 | return snd_soc_register_dai(tplg->comp, dai_drv); | |
1621 | } | |
1622 | ||
67d1c21e | 1623 | /* create the FE DAI link */ |
acfc7d46 ML |
1624 | static int soc_tplg_link_create(struct soc_tplg *tplg, |
1625 | struct snd_soc_tplg_pcm *pcm) | |
1626 | { | |
1627 | struct snd_soc_dai_link *link; | |
1628 | int ret; | |
1629 | ||
1630 | link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL); | |
1631 | if (link == NULL) | |
1632 | return -ENOMEM; | |
1633 | ||
1634 | link->name = pcm->pcm_name; | |
1635 | link->stream_name = pcm->pcm_name; | |
b84fff5a | 1636 | link->id = pcm->pcm_id; |
acfc7d46 | 1637 | |
67d1c21e GS |
1638 | link->cpu_dai_name = pcm->dai_name; |
1639 | link->codec_name = "snd-soc-dummy"; | |
1640 | link->codec_dai_name = "snd-soc-dummy-dai"; | |
1641 | ||
1642 | /* enable DPCM */ | |
1643 | link->dynamic = 1; | |
1644 | link->dpcm_playback = pcm->playback; | |
1645 | link->dpcm_capture = pcm->capture; | |
1646 | ||
acfc7d46 ML |
1647 | /* pass control to component driver for optional further init */ |
1648 | ret = soc_tplg_dai_link_load(tplg, link); | |
1649 | if (ret < 0) { | |
1650 | dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n"); | |
1651 | kfree(link); | |
1652 | return ret; | |
1653 | } | |
1654 | ||
1655 | link->dobj.index = tplg->index; | |
1656 | link->dobj.ops = tplg->ops; | |
1657 | link->dobj.type = SND_SOC_DOBJ_DAI_LINK; | |
1658 | list_add(&link->dobj.list, &tplg->comp->dobj_list); | |
1659 | ||
1660 | snd_soc_add_dai_link(tplg->comp->card, link); | |
1661 | return 0; | |
1662 | } | |
1663 | ||
1664 | /* create a FE DAI and DAI link from the PCM object */ | |
64527e8a ML |
1665 | static int soc_tplg_pcm_create(struct soc_tplg *tplg, |
1666 | struct snd_soc_tplg_pcm *pcm) | |
1667 | { | |
acfc7d46 ML |
1668 | int ret; |
1669 | ||
1670 | ret = soc_tplg_dai_create(tplg, pcm); | |
1671 | if (ret < 0) | |
1672 | return ret; | |
1673 | ||
1674 | return soc_tplg_link_create(tplg, pcm); | |
64527e8a ML |
1675 | } |
1676 | ||
1677 | static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, | |
8a978234 LG |
1678 | struct snd_soc_tplg_hdr *hdr) |
1679 | { | |
64527e8a | 1680 | struct snd_soc_tplg_pcm *pcm; |
8a978234 | 1681 | int count = hdr->count; |
64527e8a | 1682 | int i; |
8a978234 LG |
1683 | |
1684 | if (tplg->pass != SOC_TPLG_PASS_PCM_DAI) | |
1685 | return 0; | |
1686 | ||
8a978234 | 1687 | if (soc_tplg_check_elem_count(tplg, |
5b2688a5 | 1688 | sizeof(struct snd_soc_tplg_pcm), count, |
8a978234 LG |
1689 | hdr->payload_size, "PCM DAI")) { |
1690 | dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n", | |
1691 | count); | |
1692 | return -EINVAL; | |
1693 | } | |
1694 | ||
64527e8a | 1695 | /* create the FE DAIs and DAI links */ |
06eb49f7 | 1696 | pcm = (struct snd_soc_tplg_pcm *)tplg->pos; |
64527e8a | 1697 | for (i = 0; i < count; i++) { |
06eb49f7 ML |
1698 | if (pcm->size != sizeof(*pcm)) { |
1699 | dev_err(tplg->dev, "ASoC: invalid pcm size\n"); | |
1700 | return -EINVAL; | |
1701 | } | |
1702 | ||
64527e8a ML |
1703 | soc_tplg_pcm_create(tplg, pcm); |
1704 | pcm++; | |
1705 | } | |
1706 | ||
8a978234 | 1707 | dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); |
5b2688a5 | 1708 | tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count; |
8a978234 | 1709 | |
8a978234 | 1710 | return 0; |
8a978234 LG |
1711 | } |
1712 | ||
0038be9a ML |
1713 | /* * |
1714 | * soc_tplg_be_dai_config - Find and configure an existing BE DAI. | |
1715 | * @tplg: topology context | |
1716 | * @be: topology BE DAI configs. | |
1717 | * | |
1718 | * The BE dai should already be registered by the platform driver. The | |
1719 | * platform driver should specify the BE DAI name and ID for matching. | |
1720 | */ | |
1721 | static int soc_tplg_be_dai_config(struct soc_tplg *tplg, | |
1722 | struct snd_soc_tplg_be_dai *be) | |
1723 | { | |
1724 | struct snd_soc_dai_link_component dai_component = {0}; | |
1725 | struct snd_soc_dai *dai; | |
1726 | struct snd_soc_dai_driver *dai_drv; | |
1727 | struct snd_soc_pcm_stream *stream; | |
1728 | struct snd_soc_tplg_stream_caps *caps; | |
1729 | int ret; | |
1730 | ||
1731 | dai_component.dai_name = be->dai_name; | |
1732 | dai = snd_soc_find_dai(&dai_component); | |
1733 | if (!dai) { | |
1734 | dev_err(tplg->dev, "ASoC: BE DAI %s not registered\n", | |
1735 | be->dai_name); | |
1736 | return -EINVAL; | |
1737 | } | |
1738 | ||
1739 | if (be->dai_id != dai->id) { | |
1740 | dev_err(tplg->dev, "ASoC: BE DAI %s id mismatch\n", | |
1741 | be->dai_name); | |
1742 | return -EINVAL; | |
1743 | } | |
1744 | ||
1745 | dai_drv = dai->driver; | |
1746 | if (!dai_drv) | |
1747 | return -EINVAL; | |
1748 | ||
1749 | if (be->playback) { | |
1750 | stream = &dai_drv->playback; | |
1751 | caps = &be->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; | |
1752 | set_stream_info(stream, caps); | |
1753 | } | |
1754 | ||
1755 | if (be->capture) { | |
1756 | stream = &dai_drv->capture; | |
1757 | caps = &be->caps[SND_SOC_TPLG_STREAM_CAPTURE]; | |
1758 | set_stream_info(stream, caps); | |
1759 | } | |
1760 | ||
1761 | if (be->flag_mask) | |
1762 | set_dai_flags(dai_drv, be->flag_mask, be->flags); | |
1763 | ||
1764 | /* pass control to component driver for optional further init */ | |
1765 | ret = soc_tplg_dai_load(tplg, dai_drv); | |
1766 | if (ret < 0) { | |
1767 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); | |
1768 | return ret; | |
1769 | } | |
1770 | ||
1771 | return 0; | |
1772 | } | |
1773 | ||
1774 | static int soc_tplg_be_dai_elems_load(struct soc_tplg *tplg, | |
1775 | struct snd_soc_tplg_hdr *hdr) | |
1776 | { | |
1777 | struct snd_soc_tplg_be_dai *be; | |
1778 | int count = hdr->count; | |
1779 | int i; | |
1780 | ||
1781 | if (tplg->pass != SOC_TPLG_PASS_BE_DAI) | |
1782 | return 0; | |
1783 | ||
1784 | /* config the existing BE DAIs */ | |
1785 | for (i = 0; i < count; i++) { | |
1786 | be = (struct snd_soc_tplg_be_dai *)tplg->pos; | |
1787 | if (be->size != sizeof(*be)) { | |
1788 | dev_err(tplg->dev, "ASoC: invalid BE DAI size\n"); | |
1789 | return -EINVAL; | |
1790 | } | |
1791 | ||
1792 | soc_tplg_be_dai_config(tplg, be); | |
1793 | tplg->pos += (sizeof(*be) + be->priv.size); | |
1794 | } | |
1795 | ||
1796 | dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count); | |
1797 | return 0; | |
1798 | } | |
1799 | ||
1800 | ||
8a978234 | 1801 | static int soc_tplg_manifest_load(struct soc_tplg *tplg, |
0038be9a | 1802 | struct snd_soc_tplg_hdr *hdr) |
8a978234 LG |
1803 | { |
1804 | struct snd_soc_tplg_manifest *manifest; | |
1805 | ||
1806 | if (tplg->pass != SOC_TPLG_PASS_MANIFEST) | |
1807 | return 0; | |
1808 | ||
1809 | manifest = (struct snd_soc_tplg_manifest *)tplg->pos; | |
06eb49f7 ML |
1810 | if (manifest->size != sizeof(*manifest)) { |
1811 | dev_err(tplg->dev, "ASoC: invalid manifest size\n"); | |
1812 | return -EINVAL; | |
1813 | } | |
1814 | ||
8a978234 LG |
1815 | tplg->pos += sizeof(struct snd_soc_tplg_manifest); |
1816 | ||
1817 | if (tplg->comp && tplg->ops && tplg->ops->manifest) | |
1818 | return tplg->ops->manifest(tplg->comp, manifest); | |
1819 | ||
1820 | dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n"); | |
1821 | return 0; | |
1822 | } | |
1823 | ||
1824 | /* validate header magic, size and type */ | |
1825 | static int soc_valid_header(struct soc_tplg *tplg, | |
1826 | struct snd_soc_tplg_hdr *hdr) | |
1827 | { | |
1828 | if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size) | |
1829 | return 0; | |
1830 | ||
06eb49f7 ML |
1831 | if (hdr->size != sizeof(*hdr)) { |
1832 | dev_err(tplg->dev, | |
1833 | "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n", | |
1834 | hdr->type, soc_tplg_get_hdr_offset(tplg), | |
1835 | tplg->fw->size); | |
1836 | return -EINVAL; | |
1837 | } | |
1838 | ||
8a978234 LG |
1839 | /* big endian firmware objects not supported atm */ |
1840 | if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) { | |
1841 | dev_err(tplg->dev, | |
1842 | "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", | |
1843 | tplg->pass, hdr->magic, | |
1844 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); | |
1845 | return -EINVAL; | |
1846 | } | |
1847 | ||
1848 | if (hdr->magic != SND_SOC_TPLG_MAGIC) { | |
1849 | dev_err(tplg->dev, | |
1850 | "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", | |
1851 | tplg->pass, hdr->magic, | |
1852 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); | |
1853 | return -EINVAL; | |
1854 | } | |
1855 | ||
1856 | if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) { | |
1857 | dev_err(tplg->dev, | |
1858 | "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", | |
1859 | tplg->pass, hdr->abi, | |
1860 | SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), | |
1861 | tplg->fw->size); | |
1862 | return -EINVAL; | |
1863 | } | |
1864 | ||
1865 | if (hdr->payload_size == 0) { | |
1866 | dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", | |
1867 | soc_tplg_get_hdr_offset(tplg)); | |
1868 | return -EINVAL; | |
1869 | } | |
1870 | ||
1871 | if (tplg->pass == hdr->type) | |
1872 | dev_dbg(tplg->dev, | |
1873 | "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n", | |
1874 | hdr->payload_size, hdr->type, hdr->version, | |
1875 | hdr->vendor_type, tplg->pass); | |
1876 | ||
1877 | return 1; | |
1878 | } | |
1879 | ||
1880 | /* check header type and call appropriate handler */ | |
1881 | static int soc_tplg_load_header(struct soc_tplg *tplg, | |
1882 | struct snd_soc_tplg_hdr *hdr) | |
1883 | { | |
1884 | tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); | |
1885 | ||
1886 | /* check for matching ID */ | |
1887 | if (hdr->index != tplg->req_index && | |
1888 | hdr->index != SND_SOC_TPLG_INDEX_ALL) | |
1889 | return 0; | |
1890 | ||
1891 | tplg->index = hdr->index; | |
1892 | ||
1893 | switch (hdr->type) { | |
1894 | case SND_SOC_TPLG_TYPE_MIXER: | |
1895 | case SND_SOC_TPLG_TYPE_ENUM: | |
1896 | case SND_SOC_TPLG_TYPE_BYTES: | |
1897 | return soc_tplg_kcontrol_elems_load(tplg, hdr); | |
1898 | case SND_SOC_TPLG_TYPE_DAPM_GRAPH: | |
1899 | return soc_tplg_dapm_graph_elems_load(tplg, hdr); | |
1900 | case SND_SOC_TPLG_TYPE_DAPM_WIDGET: | |
1901 | return soc_tplg_dapm_widget_elems_load(tplg, hdr); | |
1902 | case SND_SOC_TPLG_TYPE_PCM: | |
64527e8a | 1903 | return soc_tplg_pcm_elems_load(tplg, hdr); |
0038be9a ML |
1904 | case SND_SOC_TPLG_TYPE_BE_DAI: |
1905 | return soc_tplg_be_dai_elems_load(tplg, hdr); | |
8a978234 LG |
1906 | case SND_SOC_TPLG_TYPE_MANIFEST: |
1907 | return soc_tplg_manifest_load(tplg, hdr); | |
1908 | default: | |
1909 | /* bespoke vendor data object */ | |
1910 | return soc_tplg_vendor_load(tplg, hdr); | |
1911 | } | |
1912 | ||
1913 | return 0; | |
1914 | } | |
1915 | ||
1916 | /* process the topology file headers */ | |
1917 | static int soc_tplg_process_headers(struct soc_tplg *tplg) | |
1918 | { | |
1919 | struct snd_soc_tplg_hdr *hdr; | |
1920 | int ret; | |
1921 | ||
1922 | tplg->pass = SOC_TPLG_PASS_START; | |
1923 | ||
1924 | /* process the header types from start to end */ | |
1925 | while (tplg->pass <= SOC_TPLG_PASS_END) { | |
1926 | ||
1927 | tplg->hdr_pos = tplg->fw->data; | |
1928 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; | |
1929 | ||
1930 | while (!soc_tplg_is_eof(tplg)) { | |
1931 | ||
1932 | /* make sure header is valid before loading */ | |
1933 | ret = soc_valid_header(tplg, hdr); | |
1934 | if (ret < 0) | |
1935 | return ret; | |
1936 | else if (ret == 0) | |
1937 | break; | |
1938 | ||
1939 | /* load the header object */ | |
1940 | ret = soc_tplg_load_header(tplg, hdr); | |
1941 | if (ret < 0) | |
1942 | return ret; | |
1943 | ||
1944 | /* goto next header */ | |
1945 | tplg->hdr_pos += hdr->payload_size + | |
1946 | sizeof(struct snd_soc_tplg_hdr); | |
1947 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; | |
1948 | } | |
1949 | ||
1950 | /* next data type pass */ | |
1951 | tplg->pass++; | |
1952 | } | |
1953 | ||
1954 | /* signal DAPM we are complete */ | |
1955 | ret = soc_tplg_dapm_complete(tplg); | |
1956 | if (ret < 0) | |
1957 | dev_err(tplg->dev, | |
1958 | "ASoC: failed to initialise DAPM from Firmware\n"); | |
1959 | ||
1960 | return ret; | |
1961 | } | |
1962 | ||
1963 | static int soc_tplg_load(struct soc_tplg *tplg) | |
1964 | { | |
1965 | int ret; | |
1966 | ||
1967 | ret = soc_tplg_process_headers(tplg); | |
1968 | if (ret == 0) | |
1969 | soc_tplg_complete(tplg); | |
1970 | ||
1971 | return ret; | |
1972 | } | |
1973 | ||
1974 | /* load audio component topology from "firmware" file */ | |
1975 | int snd_soc_tplg_component_load(struct snd_soc_component *comp, | |
1976 | struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id) | |
1977 | { | |
1978 | struct soc_tplg tplg; | |
1979 | ||
1980 | /* setup parsing context */ | |
1981 | memset(&tplg, 0, sizeof(tplg)); | |
1982 | tplg.fw = fw; | |
1983 | tplg.dev = comp->dev; | |
1984 | tplg.comp = comp; | |
1985 | tplg.ops = ops; | |
1986 | tplg.req_index = id; | |
1987 | tplg.io_ops = ops->io_ops; | |
1988 | tplg.io_ops_count = ops->io_ops_count; | |
1a3232d2 ML |
1989 | tplg.bytes_ext_ops = ops->bytes_ext_ops; |
1990 | tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; | |
8a978234 LG |
1991 | |
1992 | return soc_tplg_load(&tplg); | |
1993 | } | |
1994 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); | |
1995 | ||
1996 | /* remove this dynamic widget */ | |
1997 | void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w) | |
1998 | { | |
1999 | /* make sure we are a widget */ | |
2000 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET) | |
2001 | return; | |
2002 | ||
2003 | remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET); | |
2004 | } | |
2005 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove); | |
2006 | ||
2007 | /* remove all dynamic widgets from this DAPM context */ | |
2008 | void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm, | |
2009 | u32 index) | |
2010 | { | |
2011 | struct snd_soc_dapm_widget *w, *next_w; | |
8a978234 LG |
2012 | |
2013 | list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) { | |
2014 | ||
2015 | /* make sure we are a widget with correct context */ | |
2016 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm) | |
2017 | continue; | |
2018 | ||
2019 | /* match ID */ | |
2020 | if (w->dobj.index != index && | |
2021 | w->dobj.index != SND_SOC_TPLG_INDEX_ALL) | |
2022 | continue; | |
8a978234 LG |
2023 | /* check and free and dynamic widget kcontrols */ |
2024 | snd_soc_tplg_widget_remove(w); | |
b97e2698 | 2025 | snd_soc_dapm_free_widget(w); |
8a978234 | 2026 | } |
fd589a1b | 2027 | snd_soc_dapm_reset_cache(dapm); |
8a978234 LG |
2028 | } |
2029 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all); | |
2030 | ||
2031 | /* remove dynamic controls from the component driver */ | |
2032 | int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index) | |
2033 | { | |
2034 | struct snd_soc_dobj *dobj, *next_dobj; | |
2035 | int pass = SOC_TPLG_PASS_END; | |
2036 | ||
2037 | /* process the header types from end to start */ | |
2038 | while (pass >= SOC_TPLG_PASS_START) { | |
2039 | ||
2040 | /* remove mixer controls */ | |
2041 | list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, | |
2042 | list) { | |
2043 | ||
2044 | /* match index */ | |
2045 | if (dobj->index != index && | |
2046 | dobj->index != SND_SOC_TPLG_INDEX_ALL) | |
2047 | continue; | |
2048 | ||
2049 | switch (dobj->type) { | |
2050 | case SND_SOC_DOBJ_MIXER: | |
2051 | remove_mixer(comp, dobj, pass); | |
2052 | break; | |
2053 | case SND_SOC_DOBJ_ENUM: | |
2054 | remove_enum(comp, dobj, pass); | |
2055 | break; | |
2056 | case SND_SOC_DOBJ_BYTES: | |
2057 | remove_bytes(comp, dobj, pass); | |
2058 | break; | |
2059 | case SND_SOC_DOBJ_WIDGET: | |
2060 | remove_widget(comp, dobj, pass); | |
2061 | break; | |
2062 | case SND_SOC_DOBJ_PCM: | |
64527e8a | 2063 | remove_dai(comp, dobj, pass); |
8a978234 | 2064 | break; |
acfc7d46 ML |
2065 | case SND_SOC_DOBJ_DAI_LINK: |
2066 | remove_link(comp, dobj, pass); | |
2067 | break; | |
8a978234 LG |
2068 | default: |
2069 | dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", | |
2070 | dobj->type); | |
2071 | break; | |
2072 | } | |
2073 | } | |
2074 | pass--; | |
2075 | } | |
2076 | ||
2077 | /* let caller know if FW can be freed when no objects are left */ | |
2078 | return !list_empty(&comp->dobj_list); | |
2079 | } | |
2080 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); |