Kuninori Morimoto | f2b6a1b | 2018-07-02 06:24:45 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // soc-topology.c -- ALSA SoC Topology |
| 4 | // |
| 5 | // Copyright (C) 2012 Texas Instruments Inc. |
| 6 | // Copyright (C) 2015 Intel Corporation. |
| 7 | // |
| 8 | // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com> |
| 9 | // K, Mythri P <mythri.p.k@intel.com> |
| 10 | // Prusty, Subhransu S <subhransu.s.prusty@intel.com> |
| 11 | // B, Jayachandran <jayachandran.b@intel.com> |
| 12 | // Abdullah, Omair M <omair.m.abdullah@intel.com> |
| 13 | // Jin, Yao <yao.jin@intel.com> |
| 14 | // Lin, Mengdong <mengdong.lin@intel.com> |
| 15 | // |
| 16 | // Add support to read audio firmware topology alongside firmware text. The |
| 17 | // topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links, |
| 18 | // equalizers, firmware, coefficients etc. |
| 19 | // |
| 20 | // This file only manages the core ALSA and ASoC components, all other bespoke |
| 21 | // firmware topology data is passed to component drivers for bespoke handling. |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/export.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/firmware.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <sound/soc.h> |
| 29 | #include <sound/soc-dapm.h> |
| 30 | #include <sound/soc-topology.h> |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 31 | #include <sound/tlv.h> |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * We make several passes over the data (since it wont necessarily be ordered) |
| 35 | * and process objects in the following order. This guarantees the component |
| 36 | * drivers will be ready with any vendor data before the mixers and DAPM objects |
| 37 | * are loaded (that may make use of the vendor data). |
| 38 | */ |
| 39 | #define SOC_TPLG_PASS_MANIFEST 0 |
| 40 | #define SOC_TPLG_PASS_VENDOR 1 |
| 41 | #define SOC_TPLG_PASS_MIXER 2 |
| 42 | #define SOC_TPLG_PASS_WIDGET 3 |
Mengdong Lin | 1a8e7fa | 2015-08-10 22:48:30 +0800 | [diff] [blame] | 43 | #define SOC_TPLG_PASS_PCM_DAI 4 |
| 44 | #define SOC_TPLG_PASS_GRAPH 5 |
| 45 | #define SOC_TPLG_PASS_PINS 6 |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 46 | #define SOC_TPLG_PASS_BE_DAI 7 |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 47 | #define SOC_TPLG_PASS_LINK 8 |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 48 | |
| 49 | #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 50 | #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 51 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 52 | /* topology context */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 53 | struct soc_tplg { |
| 54 | const struct firmware *fw; |
| 55 | |
| 56 | /* runtime FW parsing */ |
| 57 | const u8 *pos; /* read postion */ |
| 58 | const u8 *hdr_pos; /* header position */ |
| 59 | unsigned int pass; /* pass number */ |
| 60 | |
| 61 | /* component caller */ |
| 62 | struct device *dev; |
| 63 | struct snd_soc_component *comp; |
| 64 | u32 index; /* current block index */ |
| 65 | u32 req_index; /* required index, only loaded/free matching blocks */ |
| 66 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 67 | /* vendor specific kcontrol operations */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 68 | const struct snd_soc_tplg_kcontrol_ops *io_ops; |
| 69 | int io_ops_count; |
| 70 | |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 71 | /* vendor specific bytes ext handlers, for TLV bytes controls */ |
| 72 | const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops; |
| 73 | int bytes_ext_ops_count; |
| 74 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 75 | /* optional fw loading callbacks to component drivers */ |
| 76 | struct snd_soc_tplg_ops *ops; |
| 77 | }; |
| 78 | |
| 79 | static int soc_tplg_process_headers(struct soc_tplg *tplg); |
| 80 | static void soc_tplg_complete(struct soc_tplg *tplg); |
| 81 | struct snd_soc_dapm_widget * |
| 82 | snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm, |
| 83 | const struct snd_soc_dapm_widget *widget); |
| 84 | struct snd_soc_dapm_widget * |
| 85 | snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, |
| 86 | const struct snd_soc_dapm_widget *widget); |
| 87 | |
| 88 | /* check we dont overflow the data for this control chunk */ |
| 89 | static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size, |
| 90 | unsigned int count, size_t bytes, const char *elem_type) |
| 91 | { |
| 92 | const u8 *end = tplg->pos + elem_size * count; |
| 93 | |
| 94 | if (end > tplg->fw->data + tplg->fw->size) { |
| 95 | dev_err(tplg->dev, "ASoC: %s overflow end of data\n", |
| 96 | elem_type); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
| 100 | /* check there is enough room in chunk for control. |
| 101 | extra bytes at the end of control are for vendor data here */ |
| 102 | if (elem_size * count > bytes) { |
| 103 | dev_err(tplg->dev, |
| 104 | "ASoC: %s count %d of size %zu is bigger than chunk %zu\n", |
| 105 | elem_type, count, elem_size, bytes); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static inline int soc_tplg_is_eof(struct soc_tplg *tplg) |
| 113 | { |
| 114 | const u8 *end = tplg->hdr_pos; |
| 115 | |
| 116 | if (end >= tplg->fw->data + tplg->fw->size) |
| 117 | return 1; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg) |
| 122 | { |
| 123 | return (unsigned long)(tplg->hdr_pos - tplg->fw->data); |
| 124 | } |
| 125 | |
| 126 | static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg) |
| 127 | { |
| 128 | return (unsigned long)(tplg->pos - tplg->fw->data); |
| 129 | } |
| 130 | |
| 131 | /* mapping of Kcontrol types and associated operations. */ |
| 132 | static const struct snd_soc_tplg_kcontrol_ops io_ops[] = { |
| 133 | {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw, |
| 134 | snd_soc_put_volsw, snd_soc_info_volsw}, |
| 135 | {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx, |
| 136 | snd_soc_put_volsw_sx, NULL}, |
| 137 | {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double, |
| 138 | snd_soc_put_enum_double, snd_soc_info_enum_double}, |
| 139 | {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double, |
| 140 | snd_soc_put_enum_double, NULL}, |
| 141 | {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get, |
| 142 | snd_soc_bytes_put, snd_soc_bytes_info}, |
| 143 | {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range, |
| 144 | snd_soc_put_volsw_range, snd_soc_info_volsw_range}, |
| 145 | {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx, |
| 146 | snd_soc_put_xr_sx, snd_soc_info_xr_sx}, |
| 147 | {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe, |
| 148 | snd_soc_put_strobe, NULL}, |
| 149 | {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw, |
Jeeja KP | 2c57d478 | 2015-07-14 13:10:47 +0530 | [diff] [blame] | 150 | snd_soc_dapm_put_volsw, snd_soc_info_volsw}, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 151 | {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double, |
| 152 | snd_soc_dapm_put_enum_double, snd_soc_info_enum_double}, |
| 153 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double, |
| 154 | snd_soc_dapm_put_enum_double, NULL}, |
| 155 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double, |
| 156 | snd_soc_dapm_put_enum_double, NULL}, |
| 157 | {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch, |
| 158 | snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch}, |
| 159 | }; |
| 160 | |
| 161 | struct soc_tplg_map { |
| 162 | int uid; |
| 163 | int kid; |
| 164 | }; |
| 165 | |
| 166 | /* mapping of widget types from UAPI IDs to kernel IDs */ |
| 167 | static const struct soc_tplg_map dapm_map[] = { |
| 168 | {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input}, |
| 169 | {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output}, |
| 170 | {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux}, |
| 171 | {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer}, |
| 172 | {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga}, |
| 173 | {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv}, |
| 174 | {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc}, |
| 175 | {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac}, |
| 176 | {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch}, |
| 177 | {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre}, |
| 178 | {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post}, |
| 179 | {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in}, |
| 180 | {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out}, |
| 181 | {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in}, |
| 182 | {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out}, |
| 183 | {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link}, |
Liam Girdwood | 8a70b45 | 2017-06-29 14:22:24 +0100 | [diff] [blame] | 184 | {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer}, |
| 185 | {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler}, |
| 186 | {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect}, |
| 187 | {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen}, |
| 188 | {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src}, |
| 189 | {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc}, |
| 190 | {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder}, |
| 191 | {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder}, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | static int tplc_chan_get_reg(struct soc_tplg *tplg, |
| 195 | struct snd_soc_tplg_channel *chan, int map) |
| 196 | { |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 200 | if (le32_to_cpu(chan[i].id) == map) |
| 201 | return le32_to_cpu(chan[i].reg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | static int tplc_chan_get_shift(struct soc_tplg *tplg, |
| 208 | struct snd_soc_tplg_channel *chan, int map) |
| 209 | { |
| 210 | int i; |
| 211 | |
| 212 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 213 | if (le32_to_cpu(chan[i].id) == map) |
| 214 | return le32_to_cpu(chan[i].shift); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | static int get_widget_id(int tplg_type) |
| 221 | { |
| 222 | int i; |
| 223 | |
| 224 | for (i = 0; i < ARRAY_SIZE(dapm_map); i++) { |
| 225 | if (tplg_type == dapm_map[i].uid) |
| 226 | return dapm_map[i].kid; |
| 227 | } |
| 228 | |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 232 | static inline void soc_bind_err(struct soc_tplg *tplg, |
| 233 | struct snd_soc_tplg_ctl_hdr *hdr, int index) |
| 234 | { |
| 235 | dev_err(tplg->dev, |
| 236 | "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n", |
| 237 | hdr->ops.get, hdr->ops.put, hdr->ops.info, index, |
| 238 | soc_tplg_get_offset(tplg)); |
| 239 | } |
| 240 | |
| 241 | static inline void soc_control_err(struct soc_tplg *tplg, |
| 242 | struct snd_soc_tplg_ctl_hdr *hdr, const char *name) |
| 243 | { |
| 244 | dev_err(tplg->dev, |
| 245 | "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n", |
| 246 | name, hdr->ops.get, hdr->ops.put, hdr->ops.info, |
| 247 | soc_tplg_get_offset(tplg)); |
| 248 | } |
| 249 | |
| 250 | /* pass vendor data to component driver for processing */ |
| 251 | static int soc_tplg_vendor_load_(struct soc_tplg *tplg, |
| 252 | struct snd_soc_tplg_hdr *hdr) |
| 253 | { |
| 254 | int ret = 0; |
| 255 | |
| 256 | if (tplg->comp && tplg->ops && tplg->ops->vendor_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 257 | ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 258 | else { |
| 259 | dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n", |
| 260 | hdr->vendor_type); |
| 261 | return -EINVAL; |
| 262 | } |
| 263 | |
| 264 | if (ret < 0) |
| 265 | dev_err(tplg->dev, |
| 266 | "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n", |
| 267 | soc_tplg_get_hdr_offset(tplg), |
| 268 | soc_tplg_get_hdr_offset(tplg), |
| 269 | hdr->type, hdr->vendor_type); |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | /* pass vendor data to component driver for processing */ |
| 274 | static int soc_tplg_vendor_load(struct soc_tplg *tplg, |
| 275 | struct snd_soc_tplg_hdr *hdr) |
| 276 | { |
| 277 | if (tplg->pass != SOC_TPLG_PASS_VENDOR) |
| 278 | return 0; |
| 279 | |
| 280 | return soc_tplg_vendor_load_(tplg, hdr); |
| 281 | } |
| 282 | |
| 283 | /* optionally pass new dynamic widget to component driver. This is mainly for |
| 284 | * external widgets where we can assign private data/ops */ |
| 285 | static int soc_tplg_widget_load(struct soc_tplg *tplg, |
| 286 | struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) |
| 287 | { |
| 288 | if (tplg->comp && tplg->ops && tplg->ops->widget_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 289 | return tplg->ops->widget_load(tplg->comp, tplg->index, w, |
| 290 | tplg_w); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 295 | /* optionally pass new dynamic widget to component driver. This is mainly for |
| 296 | * external widgets where we can assign private data/ops */ |
| 297 | static int soc_tplg_widget_ready(struct soc_tplg *tplg, |
| 298 | struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) |
| 299 | { |
| 300 | if (tplg->comp && tplg->ops && tplg->ops->widget_ready) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 301 | return tplg->ops->widget_ready(tplg->comp, tplg->index, w, |
| 302 | tplg_w); |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Masahiro Yamada | 183b802 | 2017-02-27 14:29:20 -0800 | [diff] [blame] | 307 | /* pass DAI configurations to component driver for extra initialization */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 308 | static int soc_tplg_dai_load(struct soc_tplg *tplg, |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 309 | struct snd_soc_dai_driver *dai_drv, |
| 310 | struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 311 | { |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 312 | if (tplg->comp && tplg->ops && tplg->ops->dai_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 313 | return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv, |
| 314 | pcm, dai); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Masahiro Yamada | 183b802 | 2017-02-27 14:29:20 -0800 | [diff] [blame] | 319 | /* pass link configurations to component driver for extra initialization */ |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 320 | static int soc_tplg_dai_link_load(struct soc_tplg *tplg, |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 321 | struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg) |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 322 | { |
| 323 | if (tplg->comp && tplg->ops && tplg->ops->link_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 324 | return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 329 | /* tell the component driver that all firmware has been loaded in this request */ |
| 330 | static void soc_tplg_complete(struct soc_tplg *tplg) |
| 331 | { |
| 332 | if (tplg->comp && tplg->ops && tplg->ops->complete) |
| 333 | tplg->ops->complete(tplg->comp); |
| 334 | } |
| 335 | |
| 336 | /* add a dynamic kcontrol */ |
| 337 | static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev, |
| 338 | const struct snd_kcontrol_new *control_new, const char *prefix, |
| 339 | void *data, struct snd_kcontrol **kcontrol) |
| 340 | { |
| 341 | int err; |
| 342 | |
| 343 | *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix); |
| 344 | if (*kcontrol == NULL) { |
| 345 | dev_err(dev, "ASoC: Failed to create new kcontrol %s\n", |
| 346 | control_new->name); |
| 347 | return -ENOMEM; |
| 348 | } |
| 349 | |
| 350 | err = snd_ctl_add(card, *kcontrol); |
| 351 | if (err < 0) { |
| 352 | dev_err(dev, "ASoC: Failed to add %s: %d\n", |
| 353 | control_new->name, err); |
| 354 | return err; |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /* add a dynamic kcontrol for component driver */ |
| 361 | static int soc_tplg_add_kcontrol(struct soc_tplg *tplg, |
| 362 | struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol) |
| 363 | { |
| 364 | struct snd_soc_component *comp = tplg->comp; |
| 365 | |
| 366 | return soc_tplg_add_dcontrol(comp->card->snd_card, |
| 367 | comp->dev, k, NULL, comp, kcontrol); |
| 368 | } |
| 369 | |
| 370 | /* remove a mixer kcontrol */ |
| 371 | static void remove_mixer(struct snd_soc_component *comp, |
| 372 | struct snd_soc_dobj *dobj, int pass) |
| 373 | { |
| 374 | struct snd_card *card = comp->card->snd_card; |
| 375 | struct soc_mixer_control *sm = |
| 376 | container_of(dobj, struct soc_mixer_control, dobj); |
| 377 | const unsigned int *p = NULL; |
| 378 | |
| 379 | if (pass != SOC_TPLG_PASS_MIXER) |
| 380 | return; |
| 381 | |
| 382 | if (dobj->ops && dobj->ops->control_unload) |
| 383 | dobj->ops->control_unload(comp, dobj); |
| 384 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 385 | if (dobj->control.kcontrol->tlv.p) |
| 386 | p = dobj->control.kcontrol->tlv.p; |
| 387 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 388 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 389 | kfree(sm); |
| 390 | kfree(p); |
| 391 | } |
| 392 | |
| 393 | /* remove an enum kcontrol */ |
| 394 | static void remove_enum(struct snd_soc_component *comp, |
| 395 | struct snd_soc_dobj *dobj, int pass) |
| 396 | { |
| 397 | struct snd_card *card = comp->card->snd_card; |
| 398 | struct soc_enum *se = container_of(dobj, struct soc_enum, dobj); |
| 399 | int i; |
| 400 | |
| 401 | if (pass != SOC_TPLG_PASS_MIXER) |
| 402 | return; |
| 403 | |
| 404 | if (dobj->ops && dobj->ops->control_unload) |
| 405 | dobj->ops->control_unload(comp, dobj); |
| 406 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 407 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 408 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 409 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 410 | kfree(dobj->control.dvalues); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 411 | for (i = 0; i < se->items; i++) |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 412 | kfree(dobj->control.dtexts[i]); |
Amadeusz Sławiński | 34db6a3 | 2019-01-25 14:06:44 -0600 | [diff] [blame] | 413 | kfree(dobj->control.dtexts); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 414 | kfree(se); |
| 415 | } |
| 416 | |
| 417 | /* remove a byte kcontrol */ |
| 418 | static void remove_bytes(struct snd_soc_component *comp, |
| 419 | struct snd_soc_dobj *dobj, int pass) |
| 420 | { |
| 421 | struct snd_card *card = comp->card->snd_card; |
| 422 | struct soc_bytes_ext *sb = |
| 423 | container_of(dobj, struct soc_bytes_ext, dobj); |
| 424 | |
| 425 | if (pass != SOC_TPLG_PASS_MIXER) |
| 426 | return; |
| 427 | |
| 428 | if (dobj->ops && dobj->ops->control_unload) |
| 429 | dobj->ops->control_unload(comp, dobj); |
| 430 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 431 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 432 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 433 | kfree(sb); |
| 434 | } |
| 435 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 436 | /* remove a route */ |
| 437 | static void remove_route(struct snd_soc_component *comp, |
| 438 | struct snd_soc_dobj *dobj, int pass) |
| 439 | { |
| 440 | struct snd_soc_dapm_route *route = |
| 441 | container_of(dobj, struct snd_soc_dapm_route, dobj); |
| 442 | |
| 443 | if (pass != SOC_TPLG_PASS_GRAPH) |
| 444 | return; |
| 445 | |
| 446 | if (dobj->ops && dobj->ops->dapm_route_unload) |
| 447 | dobj->ops->dapm_route_unload(comp, dobj); |
| 448 | |
| 449 | list_del(&dobj->list); |
| 450 | kfree(route); |
| 451 | } |
| 452 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 453 | /* remove a widget and it's kcontrols - routes must be removed first */ |
| 454 | static void remove_widget(struct snd_soc_component *comp, |
| 455 | struct snd_soc_dobj *dobj, int pass) |
| 456 | { |
| 457 | struct snd_card *card = comp->card->snd_card; |
| 458 | struct snd_soc_dapm_widget *w = |
| 459 | container_of(dobj, struct snd_soc_dapm_widget, dobj); |
| 460 | int i; |
| 461 | |
| 462 | if (pass != SOC_TPLG_PASS_WIDGET) |
| 463 | return; |
| 464 | |
| 465 | if (dobj->ops && dobj->ops->widget_unload) |
| 466 | dobj->ops->widget_unload(comp, dobj); |
| 467 | |
Liam Girdwood | 05bdcf1 | 2018-03-14 20:42:40 +0000 | [diff] [blame] | 468 | if (!w->kcontrols) |
| 469 | goto free_news; |
| 470 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 471 | /* |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 472 | * Dynamic Widgets either have 1..N enum kcontrols or mixers. |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 473 | * The enum may either have an array of values or strings. |
| 474 | */ |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 475 | if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 476 | /* enumerated widget mixer */ |
Liam Girdwood | f53c4c2 | 2018-03-27 14:30:44 +0100 | [diff] [blame] | 477 | for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) { |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 478 | struct snd_kcontrol *kcontrol = w->kcontrols[i]; |
| 479 | struct soc_enum *se = |
| 480 | (struct soc_enum *)kcontrol->private_value; |
Colin Ian King | d7766aa | 2017-04-15 18:49:54 +0100 | [diff] [blame] | 481 | int j; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 482 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 483 | snd_ctl_remove(card, kcontrol); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 484 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 485 | kfree(dobj->control.dvalues); |
Colin Ian King | d7766aa | 2017-04-15 18:49:54 +0100 | [diff] [blame] | 486 | for (j = 0; j < se->items; j++) |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 487 | kfree(dobj->control.dtexts[j]); |
Amadeusz Sławiński | 34db6a3 | 2019-01-25 14:06:44 -0600 | [diff] [blame] | 488 | kfree(dobj->control.dtexts); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 489 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 490 | kfree(se); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 491 | kfree(w->kcontrol_news[i].name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 492 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 493 | } else { |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 494 | /* volume mixer or bytes controls */ |
Liam Girdwood | f53c4c2 | 2018-03-27 14:30:44 +0100 | [diff] [blame] | 495 | for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 496 | struct snd_kcontrol *kcontrol = w->kcontrols[i]; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 497 | |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 498 | if (dobj->widget.kcontrol_type |
| 499 | == SND_SOC_TPLG_TYPE_MIXER) |
| 500 | kfree(kcontrol->tlv.p); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 501 | |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 502 | /* Private value is used as struct soc_mixer_control |
| 503 | * for volume mixers or soc_bytes_ext for bytes |
| 504 | * controls. |
| 505 | */ |
| 506 | kfree((void *)kcontrol->private_value); |
Colin Ian King | c2b3612 | 2016-12-09 14:17:47 +0000 | [diff] [blame] | 507 | snd_ctl_remove(card, kcontrol); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 508 | kfree(w->kcontrol_news[i].name); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 509 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 510 | } |
Liam Girdwood | 05bdcf1 | 2018-03-14 20:42:40 +0000 | [diff] [blame] | 511 | |
| 512 | free_news: |
| 513 | kfree(w->kcontrol_news); |
| 514 | |
Amadeusz Sławiński | a46e839 | 2019-01-25 14:06:43 -0600 | [diff] [blame] | 515 | list_del(&dobj->list); |
| 516 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 517 | /* widget w is freed by soc-dapm.c */ |
| 518 | } |
| 519 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 520 | /* remove DAI configurations */ |
| 521 | static void remove_dai(struct snd_soc_component *comp, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 522 | struct snd_soc_dobj *dobj, int pass) |
| 523 | { |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 524 | struct snd_soc_dai_driver *dai_drv = |
| 525 | container_of(dobj, struct snd_soc_dai_driver, dobj); |
Guennadi Liakhovetski | 52abe6c | 2019-02-01 11:05:13 -0600 | [diff] [blame] | 526 | struct snd_soc_dai *dai; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 527 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 528 | if (pass != SOC_TPLG_PASS_PCM_DAI) |
| 529 | return; |
| 530 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 531 | if (dobj->ops && dobj->ops->dai_unload) |
| 532 | dobj->ops->dai_unload(comp, dobj); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 533 | |
Guennadi Liakhovetski | 52abe6c | 2019-02-01 11:05:13 -0600 | [diff] [blame] | 534 | list_for_each_entry(dai, &comp->dai_list, list) |
| 535 | if (dai->driver == dai_drv) |
| 536 | dai->driver = NULL; |
| 537 | |
Bard liao | 7b6f68a | 2019-03-05 23:57:52 +0800 | [diff] [blame] | 538 | kfree(dai_drv->playback.stream_name); |
| 539 | kfree(dai_drv->capture.stream_name); |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 540 | kfree(dai_drv->name); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 541 | list_del(&dobj->list); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 542 | kfree(dai_drv); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 543 | } |
| 544 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 545 | /* remove link configurations */ |
| 546 | static void remove_link(struct snd_soc_component *comp, |
| 547 | struct snd_soc_dobj *dobj, int pass) |
| 548 | { |
| 549 | struct snd_soc_dai_link *link = |
| 550 | container_of(dobj, struct snd_soc_dai_link, dobj); |
| 551 | |
| 552 | if (pass != SOC_TPLG_PASS_PCM_DAI) |
| 553 | return; |
| 554 | |
| 555 | if (dobj->ops && dobj->ops->link_unload) |
| 556 | dobj->ops->link_unload(comp, dobj); |
| 557 | |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 558 | kfree(link->name); |
| 559 | kfree(link->stream_name); |
| 560 | kfree(link->cpu_dai_name); |
| 561 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 562 | list_del(&dobj->list); |
| 563 | snd_soc_remove_dai_link(comp->card, link); |
| 564 | kfree(link); |
| 565 | } |
| 566 | |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 567 | /* unload dai link */ |
| 568 | static void remove_backend_link(struct snd_soc_component *comp, |
| 569 | struct snd_soc_dobj *dobj, int pass) |
| 570 | { |
| 571 | if (pass != SOC_TPLG_PASS_LINK) |
| 572 | return; |
| 573 | |
| 574 | if (dobj->ops && dobj->ops->link_unload) |
| 575 | dobj->ops->link_unload(comp, dobj); |
| 576 | |
| 577 | /* |
| 578 | * We don't free the link here as what remove_link() do since BE |
| 579 | * links are not allocated by topology. |
| 580 | * We however need to reset the dobj type to its initial values |
| 581 | */ |
| 582 | dobj->type = SND_SOC_DOBJ_NONE; |
| 583 | list_del(&dobj->list); |
| 584 | } |
| 585 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 586 | /* bind a kcontrol to it's IO handlers */ |
| 587 | static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr, |
| 588 | struct snd_kcontrol_new *k, |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 589 | const struct soc_tplg *tplg) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 590 | { |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 591 | const struct snd_soc_tplg_kcontrol_ops *ops; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 592 | const struct snd_soc_tplg_bytes_ext_ops *ext_ops; |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 593 | int num_ops, i; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 594 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 595 | if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 596 | && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER |
| 597 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
| 598 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 599 | struct soc_bytes_ext *sbe; |
| 600 | struct snd_soc_tplg_bytes_control *be; |
| 601 | |
| 602 | sbe = (struct soc_bytes_ext *)k->private_value; |
| 603 | be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); |
| 604 | |
| 605 | /* TLV bytes controls need standard kcontrol info handler, |
| 606 | * TLV callback and extended put/get handlers. |
| 607 | */ |
Omair M Abdullah | f4be978 | 2015-11-09 23:20:01 +0530 | [diff] [blame] | 608 | k->info = snd_soc_bytes_info_ext; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 609 | k->tlv.c = snd_soc_bytes_tlv_callback; |
| 610 | |
| 611 | ext_ops = tplg->bytes_ext_ops; |
| 612 | num_ops = tplg->bytes_ext_ops_count; |
| 613 | for (i = 0; i < num_ops; i++) { |
| 614 | if (!sbe->put && ext_ops[i].id == be->ext_ops.put) |
| 615 | sbe->put = ext_ops[i].put; |
| 616 | if (!sbe->get && ext_ops[i].id == be->ext_ops.get) |
| 617 | sbe->get = ext_ops[i].get; |
| 618 | } |
| 619 | |
| 620 | if (sbe->put && sbe->get) |
| 621 | return 0; |
| 622 | else |
| 623 | return -EINVAL; |
| 624 | } |
| 625 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 626 | /* try and map vendor specific kcontrol handlers first */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 627 | ops = tplg->io_ops; |
| 628 | num_ops = tplg->io_ops_count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 629 | for (i = 0; i < num_ops; i++) { |
| 630 | |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 631 | if (k->put == NULL && ops[i].id == hdr->ops.put) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 632 | k->put = ops[i].put; |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 633 | if (k->get == NULL && ops[i].id == hdr->ops.get) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 634 | k->get = ops[i].get; |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 635 | if (k->info == NULL && ops[i].id == hdr->ops.info) |
| 636 | k->info = ops[i].info; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 637 | } |
| 638 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 639 | /* vendor specific handlers found ? */ |
| 640 | if (k->put && k->get && k->info) |
| 641 | return 0; |
| 642 | |
| 643 | /* none found so try standard kcontrol handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 644 | ops = io_ops; |
| 645 | num_ops = ARRAY_SIZE(io_ops); |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 646 | for (i = 0; i < num_ops; i++) { |
| 647 | |
| 648 | if (k->put == NULL && ops[i].id == hdr->ops.put) |
| 649 | k->put = ops[i].put; |
| 650 | if (k->get == NULL && ops[i].id == hdr->ops.get) |
| 651 | k->get = ops[i].get; |
| 652 | if (k->info == NULL && ops[i].id == hdr->ops.info) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 653 | k->info = ops[i].info; |
| 654 | } |
| 655 | |
| 656 | /* standard handlers found ? */ |
| 657 | if (k->put && k->get && k->info) |
| 658 | return 0; |
| 659 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 660 | /* nothing to bind */ |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | |
| 664 | /* bind a widgets to it's evnt handlers */ |
| 665 | int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w, |
| 666 | const struct snd_soc_tplg_widget_events *events, |
| 667 | int num_events, u16 event_type) |
| 668 | { |
| 669 | int i; |
| 670 | |
| 671 | w->event = NULL; |
| 672 | |
| 673 | for (i = 0; i < num_events; i++) { |
| 674 | if (event_type == events[i].type) { |
| 675 | |
| 676 | /* found - so assign event */ |
| 677 | w->event = events[i].event_handler; |
| 678 | return 0; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /* not found */ |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event); |
| 686 | |
| 687 | /* optionally pass new dynamic kcontrol to component driver. */ |
| 688 | static int soc_tplg_init_kcontrol(struct soc_tplg *tplg, |
| 689 | struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr) |
| 690 | { |
| 691 | if (tplg->comp && tplg->ops && tplg->ops->control_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 692 | return tplg->ops->control_load(tplg->comp, tplg->index, k, |
| 693 | hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 698 | |
| 699 | static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg, |
| 700 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 701 | { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 702 | unsigned int item_len = 2 * sizeof(unsigned int); |
| 703 | unsigned int *p; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 704 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 705 | p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL); |
| 706 | if (!p) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 707 | return -ENOMEM; |
| 708 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 709 | p[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 710 | p[1] = item_len; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 711 | p[2] = le32_to_cpu(scale->min); |
| 712 | p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK) |
| 713 | | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 714 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 715 | kc->tlv.p = (void *)p; |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | static int soc_tplg_create_tlv(struct soc_tplg *tplg, |
| 720 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc) |
| 721 | { |
| 722 | struct snd_soc_tplg_ctl_tlv *tplg_tlv; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 723 | u32 access = le32_to_cpu(tc->access); |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 724 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 725 | if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 726 | return 0; |
| 727 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 728 | if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 729 | tplg_tlv = &tc->tlv; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 730 | switch (le32_to_cpu(tplg_tlv->type)) { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 731 | case SNDRV_CTL_TLVT_DB_SCALE: |
| 732 | return soc_tplg_create_tlv_db_scale(tplg, kc, |
| 733 | &tplg_tlv->scale); |
| 734 | |
| 735 | /* TODO: add support for other TLV types */ |
| 736 | default: |
| 737 | dev_dbg(tplg->dev, "Unsupported TLV type %d\n", |
| 738 | tplg_tlv->type); |
| 739 | return -EINVAL; |
| 740 | } |
| 741 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static inline void soc_tplg_free_tlv(struct soc_tplg *tplg, |
| 747 | struct snd_kcontrol_new *kc) |
| 748 | { |
| 749 | kfree(kc->tlv.p); |
| 750 | } |
| 751 | |
| 752 | static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count, |
| 753 | size_t size) |
| 754 | { |
| 755 | struct snd_soc_tplg_bytes_control *be; |
| 756 | struct soc_bytes_ext *sbe; |
| 757 | struct snd_kcontrol_new kc; |
| 758 | int i, err; |
| 759 | |
| 760 | if (soc_tplg_check_elem_count(tplg, |
| 761 | sizeof(struct snd_soc_tplg_bytes_control), count, |
| 762 | size, "mixer bytes")) { |
| 763 | dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n", |
| 764 | count); |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | |
| 768 | for (i = 0; i < count; i++) { |
| 769 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; |
| 770 | |
| 771 | /* validate kcontrol */ |
| 772 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 773 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 774 | return -EINVAL; |
| 775 | |
| 776 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); |
| 777 | if (sbe == NULL) |
| 778 | return -ENOMEM; |
| 779 | |
| 780 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 781 | le32_to_cpu(be->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 782 | |
| 783 | dev_dbg(tplg->dev, |
| 784 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", |
| 785 | be->hdr.name, be->hdr.access); |
| 786 | |
| 787 | memset(&kc, 0, sizeof(kc)); |
| 788 | kc.name = be->hdr.name; |
| 789 | kc.private_value = (long)sbe; |
| 790 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 791 | kc.access = le32_to_cpu(be->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 792 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 793 | sbe->max = le32_to_cpu(be->max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 794 | sbe->dobj.type = SND_SOC_DOBJ_BYTES; |
| 795 | sbe->dobj.ops = tplg->ops; |
| 796 | INIT_LIST_HEAD(&sbe->dobj.list); |
| 797 | |
| 798 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 799 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 800 | if (err) { |
| 801 | soc_control_err(tplg, &be->hdr, be->hdr.name); |
| 802 | kfree(sbe); |
| 803 | continue; |
| 804 | } |
| 805 | |
| 806 | /* pass control to driver for optional further init */ |
| 807 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 808 | (struct snd_soc_tplg_ctl_hdr *)be); |
| 809 | if (err < 0) { |
| 810 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 811 | be->hdr.name); |
| 812 | kfree(sbe); |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | /* register control here */ |
| 817 | err = soc_tplg_add_kcontrol(tplg, &kc, |
| 818 | &sbe->dobj.control.kcontrol); |
| 819 | if (err < 0) { |
| 820 | dev_err(tplg->dev, "ASoC: failed to add %s\n", |
| 821 | be->hdr.name); |
| 822 | kfree(sbe); |
| 823 | continue; |
| 824 | } |
| 825 | |
| 826 | list_add(&sbe->dobj.list, &tplg->comp->dobj_list); |
| 827 | } |
| 828 | return 0; |
| 829 | |
| 830 | } |
| 831 | |
| 832 | static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count, |
| 833 | size_t size) |
| 834 | { |
| 835 | struct snd_soc_tplg_mixer_control *mc; |
| 836 | struct soc_mixer_control *sm; |
| 837 | struct snd_kcontrol_new kc; |
| 838 | int i, err; |
| 839 | |
| 840 | if (soc_tplg_check_elem_count(tplg, |
| 841 | sizeof(struct snd_soc_tplg_mixer_control), |
| 842 | count, size, "mixers")) { |
| 843 | |
| 844 | dev_err(tplg->dev, "ASoC: invalid count %d for controls\n", |
| 845 | count); |
| 846 | return -EINVAL; |
| 847 | } |
| 848 | |
| 849 | for (i = 0; i < count; i++) { |
| 850 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; |
| 851 | |
| 852 | /* validate kcontrol */ |
| 853 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 854 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 855 | return -EINVAL; |
| 856 | |
| 857 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); |
| 858 | if (sm == NULL) |
| 859 | return -ENOMEM; |
| 860 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 861 | le32_to_cpu(mc->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 862 | |
| 863 | dev_dbg(tplg->dev, |
| 864 | "ASoC: adding mixer kcontrol %s with access 0x%x\n", |
| 865 | mc->hdr.name, mc->hdr.access); |
| 866 | |
| 867 | memset(&kc, 0, sizeof(kc)); |
| 868 | kc.name = mc->hdr.name; |
| 869 | kc.private_value = (long)sm; |
| 870 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 871 | kc.access = le32_to_cpu(mc->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 872 | |
| 873 | /* we only support FL/FR channel mapping atm */ |
| 874 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, |
| 875 | SNDRV_CHMAP_FL); |
| 876 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, |
| 877 | SNDRV_CHMAP_FR); |
| 878 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, |
| 879 | SNDRV_CHMAP_FL); |
| 880 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, |
| 881 | SNDRV_CHMAP_FR); |
| 882 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 883 | sm->max = le32_to_cpu(mc->max); |
| 884 | sm->min = le32_to_cpu(mc->min); |
| 885 | sm->invert = le32_to_cpu(mc->invert); |
| 886 | sm->platform_max = le32_to_cpu(mc->platform_max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 887 | sm->dobj.index = tplg->index; |
| 888 | sm->dobj.ops = tplg->ops; |
| 889 | sm->dobj.type = SND_SOC_DOBJ_MIXER; |
| 890 | INIT_LIST_HEAD(&sm->dobj.list); |
| 891 | |
| 892 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 893 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 894 | if (err) { |
| 895 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); |
| 896 | kfree(sm); |
| 897 | continue; |
| 898 | } |
| 899 | |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 900 | /* create any TLV data */ |
| 901 | soc_tplg_create_tlv(tplg, &kc, &mc->hdr); |
| 902 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 903 | /* pass control to driver for optional further init */ |
| 904 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 905 | (struct snd_soc_tplg_ctl_hdr *) mc); |
| 906 | if (err < 0) { |
| 907 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 908 | mc->hdr.name); |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 909 | soc_tplg_free_tlv(tplg, &kc); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 910 | kfree(sm); |
| 911 | continue; |
| 912 | } |
| 913 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 914 | /* register control here */ |
| 915 | err = soc_tplg_add_kcontrol(tplg, &kc, |
| 916 | &sm->dobj.control.kcontrol); |
| 917 | if (err < 0) { |
| 918 | dev_err(tplg->dev, "ASoC: failed to add %s\n", |
| 919 | mc->hdr.name); |
| 920 | soc_tplg_free_tlv(tplg, &kc); |
| 921 | kfree(sm); |
| 922 | continue; |
| 923 | } |
| 924 | |
| 925 | list_add(&sm->dobj.list, &tplg->comp->dobj_list); |
| 926 | } |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | static int soc_tplg_denum_create_texts(struct soc_enum *se, |
| 932 | struct snd_soc_tplg_enum_control *ec) |
| 933 | { |
| 934 | int i, ret; |
| 935 | |
| 936 | se->dobj.control.dtexts = |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 937 | kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 938 | if (se->dobj.control.dtexts == NULL) |
| 939 | return -ENOMEM; |
| 940 | |
| 941 | for (i = 0; i < ec->items; i++) { |
| 942 | |
| 943 | if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 944 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 945 | ret = -EINVAL; |
| 946 | goto err; |
| 947 | } |
| 948 | |
| 949 | se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL); |
| 950 | if (!se->dobj.control.dtexts[i]) { |
| 951 | ret = -ENOMEM; |
| 952 | goto err; |
| 953 | } |
| 954 | } |
| 955 | |
Mousumi Jana | b6e38b2 | 2017-04-11 13:06:22 +0530 | [diff] [blame] | 956 | se->texts = (const char * const *)se->dobj.control.dtexts; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 957 | return 0; |
| 958 | |
| 959 | err: |
| 960 | for (--i; i >= 0; i--) |
| 961 | kfree(se->dobj.control.dtexts[i]); |
| 962 | kfree(se->dobj.control.dtexts); |
| 963 | return ret; |
| 964 | } |
| 965 | |
| 966 | static int soc_tplg_denum_create_values(struct soc_enum *se, |
| 967 | struct snd_soc_tplg_enum_control *ec) |
| 968 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 969 | int i; |
| 970 | |
| 971 | if (le32_to_cpu(ec->items) > sizeof(*ec->values)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 972 | return -EINVAL; |
| 973 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 974 | se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) * |
| 975 | sizeof(u32), |
Andrzej Hajda | 376c0af | 2015-08-07 09:59:37 +0200 | [diff] [blame] | 976 | GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 977 | if (!se->dobj.control.dvalues) |
| 978 | return -ENOMEM; |
| 979 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 980 | /* convert from little-endian */ |
| 981 | for (i = 0; i < le32_to_cpu(ec->items); i++) { |
| 982 | se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]); |
| 983 | } |
| 984 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count, |
| 989 | size_t size) |
| 990 | { |
| 991 | struct snd_soc_tplg_enum_control *ec; |
| 992 | struct soc_enum *se; |
| 993 | struct snd_kcontrol_new kc; |
| 994 | int i, ret, err; |
| 995 | |
| 996 | if (soc_tplg_check_elem_count(tplg, |
| 997 | sizeof(struct snd_soc_tplg_enum_control), |
| 998 | count, size, "enums")) { |
| 999 | |
| 1000 | dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n", |
| 1001 | count); |
| 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
| 1005 | for (i = 0; i < count; i++) { |
| 1006 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1007 | |
| 1008 | /* validate kcontrol */ |
| 1009 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1010 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1011 | return -EINVAL; |
| 1012 | |
| 1013 | se = kzalloc((sizeof(*se)), GFP_KERNEL); |
| 1014 | if (se == NULL) |
| 1015 | return -ENOMEM; |
| 1016 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1017 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1018 | le32_to_cpu(ec->priv.size)); |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1019 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1020 | dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", |
| 1021 | ec->hdr.name, ec->items); |
| 1022 | |
| 1023 | memset(&kc, 0, sizeof(kc)); |
| 1024 | kc.name = ec->hdr.name; |
| 1025 | kc.private_value = (long)se; |
| 1026 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1027 | kc.access = le32_to_cpu(ec->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1028 | |
| 1029 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 1030 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, |
| 1031 | SNDRV_CHMAP_FL); |
| 1032 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, |
| 1033 | SNDRV_CHMAP_FL); |
| 1034 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1035 | se->items = le32_to_cpu(ec->items); |
| 1036 | se->mask = le32_to_cpu(ec->mask); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1037 | se->dobj.index = tplg->index; |
| 1038 | se->dobj.type = SND_SOC_DOBJ_ENUM; |
| 1039 | se->dobj.ops = tplg->ops; |
| 1040 | INIT_LIST_HEAD(&se->dobj.list); |
| 1041 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1042 | switch (le32_to_cpu(ec->hdr.ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1043 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1044 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1045 | err = soc_tplg_denum_create_values(se, ec); |
| 1046 | if (err < 0) { |
| 1047 | dev_err(tplg->dev, |
| 1048 | "ASoC: could not create values for %s\n", |
| 1049 | ec->hdr.name); |
| 1050 | kfree(se); |
| 1051 | continue; |
| 1052 | } |
Takashi Iwai | 9c6c4d9 | 2018-10-04 20:30:06 +0200 | [diff] [blame] | 1053 | /* fall through */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1054 | case SND_SOC_TPLG_CTL_ENUM: |
| 1055 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1056 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1057 | err = soc_tplg_denum_create_texts(se, ec); |
| 1058 | if (err < 0) { |
| 1059 | dev_err(tplg->dev, |
| 1060 | "ASoC: could not create texts for %s\n", |
| 1061 | ec->hdr.name); |
| 1062 | kfree(se); |
| 1063 | continue; |
| 1064 | } |
| 1065 | break; |
| 1066 | default: |
| 1067 | dev_err(tplg->dev, |
| 1068 | "ASoC: invalid enum control type %d for %s\n", |
| 1069 | ec->hdr.ops.info, ec->hdr.name); |
| 1070 | kfree(se); |
| 1071 | continue; |
| 1072 | } |
| 1073 | |
| 1074 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1075 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1076 | if (err) { |
| 1077 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); |
| 1078 | kfree(se); |
| 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | /* pass control to driver for optional further init */ |
| 1083 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 1084 | (struct snd_soc_tplg_ctl_hdr *) ec); |
| 1085 | if (err < 0) { |
| 1086 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1087 | ec->hdr.name); |
| 1088 | kfree(se); |
| 1089 | continue; |
| 1090 | } |
| 1091 | |
| 1092 | /* register control here */ |
| 1093 | ret = soc_tplg_add_kcontrol(tplg, |
| 1094 | &kc, &se->dobj.control.kcontrol); |
| 1095 | if (ret < 0) { |
| 1096 | dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n", |
| 1097 | ec->hdr.name); |
| 1098 | kfree(se); |
| 1099 | continue; |
| 1100 | } |
| 1101 | |
| 1102 | list_add(&se->dobj.list, &tplg->comp->dobj_list); |
| 1103 | } |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg, |
| 1109 | struct snd_soc_tplg_hdr *hdr) |
| 1110 | { |
| 1111 | struct snd_soc_tplg_ctl_hdr *control_hdr; |
| 1112 | int i; |
| 1113 | |
| 1114 | if (tplg->pass != SOC_TPLG_PASS_MIXER) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1115 | tplg->pos += le32_to_cpu(hdr->size) + |
| 1116 | le32_to_cpu(hdr->payload_size); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count, |
| 1121 | soc_tplg_get_offset(tplg)); |
| 1122 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1123 | for (i = 0; i < le32_to_cpu(hdr->count); i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1124 | |
| 1125 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; |
| 1126 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1127 | if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 1128 | dev_err(tplg->dev, "ASoC: invalid control size\n"); |
| 1129 | return -EINVAL; |
| 1130 | } |
| 1131 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1132 | switch (le32_to_cpu(control_hdr->ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1133 | case SND_SOC_TPLG_CTL_VOLSW: |
| 1134 | case SND_SOC_TPLG_CTL_STROBE: |
| 1135 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 1136 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 1137 | case SND_SOC_TPLG_CTL_RANGE: |
| 1138 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
| 1139 | case SND_SOC_TPLG_DAPM_CTL_PIN: |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1140 | soc_tplg_dmixer_create(tplg, 1, |
| 1141 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1142 | break; |
| 1143 | case SND_SOC_TPLG_CTL_ENUM: |
| 1144 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1145 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1146 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1147 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1148 | soc_tplg_denum_create(tplg, 1, |
| 1149 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1150 | break; |
| 1151 | case SND_SOC_TPLG_CTL_BYTES: |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1152 | soc_tplg_dbytes_create(tplg, 1, |
| 1153 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1154 | break; |
| 1155 | default: |
| 1156 | soc_bind_err(tplg, control_hdr, i); |
| 1157 | return -EINVAL; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
Liam Girdwood | 503e79b | 2018-06-14 20:53:59 +0100 | [diff] [blame] | 1164 | /* optionally pass new dynamic kcontrol to component driver. */ |
| 1165 | static int soc_tplg_add_route(struct soc_tplg *tplg, |
| 1166 | struct snd_soc_dapm_route *route) |
| 1167 | { |
| 1168 | if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load) |
| 1169 | return tplg->ops->dapm_route_load(tplg->comp, tplg->index, |
| 1170 | route); |
| 1171 | |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1175 | static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, |
| 1176 | struct snd_soc_tplg_hdr *hdr) |
| 1177 | { |
| 1178 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1179 | struct snd_soc_tplg_dapm_graph_elem *elem; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1180 | struct snd_soc_dapm_route **routes; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1181 | int count, i, j; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1182 | int ret = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1183 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1184 | count = le32_to_cpu(hdr->count); |
| 1185 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1186 | if (tplg->pass != SOC_TPLG_PASS_GRAPH) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1187 | tplg->pos += |
| 1188 | le32_to_cpu(hdr->size) + |
| 1189 | le32_to_cpu(hdr->payload_size); |
| 1190 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | if (soc_tplg_check_elem_count(tplg, |
| 1195 | sizeof(struct snd_soc_tplg_dapm_graph_elem), |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1196 | count, le32_to_cpu(hdr->payload_size), "graph")) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1197 | |
| 1198 | dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n", |
| 1199 | count); |
| 1200 | return -EINVAL; |
| 1201 | } |
| 1202 | |
Liam Girdwood | b75a651 | 2017-06-29 14:22:26 +0100 | [diff] [blame] | 1203 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count, |
| 1204 | hdr->index); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1205 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1206 | /* allocate memory for pointer to array of dapm routes */ |
| 1207 | routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *), |
| 1208 | GFP_KERNEL); |
| 1209 | if (!routes) |
| 1210 | return -ENOMEM; |
| 1211 | |
| 1212 | /* |
| 1213 | * allocate memory for each dapm route in the array. |
| 1214 | * This needs to be done individually so that |
| 1215 | * each route can be freed when it is removed in remove_route(). |
| 1216 | */ |
| 1217 | for (i = 0; i < count; i++) { |
| 1218 | routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL); |
| 1219 | if (!routes[i]) { |
| 1220 | /* free previously allocated memory */ |
| 1221 | for (j = 0; j < i; j++) |
| 1222 | kfree(routes[j]); |
| 1223 | |
| 1224 | kfree(routes); |
| 1225 | return -ENOMEM; |
| 1226 | } |
| 1227 | } |
| 1228 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1229 | for (i = 0; i < count; i++) { |
| 1230 | elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; |
| 1231 | tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); |
| 1232 | |
| 1233 | /* validate routes */ |
| 1234 | if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1235 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1236 | ret = -EINVAL; |
| 1237 | break; |
| 1238 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1239 | if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1240 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1241 | ret = -EINVAL; |
| 1242 | break; |
| 1243 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1244 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1245 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1246 | ret = -EINVAL; |
| 1247 | break; |
| 1248 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1249 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1250 | routes[i]->source = elem->source; |
| 1251 | routes[i]->sink = elem->sink; |
| 1252 | |
| 1253 | /* set to NULL atm for tplg users */ |
| 1254 | routes[i]->connected = NULL; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1255 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0) |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1256 | routes[i]->control = NULL; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1257 | else |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1258 | routes[i]->control = elem->control; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1259 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1260 | /* add route dobj to dobj_list */ |
| 1261 | routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH; |
| 1262 | routes[i]->dobj.ops = tplg->ops; |
| 1263 | routes[i]->dobj.index = tplg->index; |
| 1264 | list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list); |
| 1265 | |
| 1266 | soc_tplg_add_route(tplg, routes[i]); |
Liam Girdwood | 503e79b | 2018-06-14 20:53:59 +0100 | [diff] [blame] | 1267 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1268 | /* add route, but keep going if some fail */ |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1269 | snd_soc_dapm_add_routes(dapm, routes[i], 1); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1270 | } |
| 1271 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1272 | /* free memory allocated for all dapm routes in case of error */ |
| 1273 | if (ret < 0) |
| 1274 | for (i = 0; i < count ; i++) |
| 1275 | kfree(routes[i]); |
| 1276 | |
| 1277 | /* |
| 1278 | * free pointer to array of dapm routes as this is no longer needed. |
| 1279 | * The memory allocated for each dapm route will be freed |
| 1280 | * when it is removed in remove_route(). |
| 1281 | */ |
| 1282 | kfree(routes); |
| 1283 | |
| 1284 | return ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( |
| 1288 | struct soc_tplg *tplg, int num_kcontrols) |
| 1289 | { |
| 1290 | struct snd_kcontrol_new *kc; |
| 1291 | struct soc_mixer_control *sm; |
| 1292 | struct snd_soc_tplg_mixer_control *mc; |
| 1293 | int i, err; |
| 1294 | |
Axel Lin | 4ca7deb | 2015-08-05 22:34:22 +0800 | [diff] [blame] | 1295 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1296 | if (kc == NULL) |
| 1297 | return NULL; |
| 1298 | |
| 1299 | for (i = 0; i < num_kcontrols; i++) { |
| 1300 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; |
| 1301 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); |
| 1302 | if (sm == NULL) |
| 1303 | goto err; |
| 1304 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1305 | /* validate kcontrol */ |
| 1306 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1307 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1308 | goto err_str; |
| 1309 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1310 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1311 | le32_to_cpu(mc->priv.size)); |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1312 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1313 | dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n", |
| 1314 | mc->hdr.name, i); |
| 1315 | |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1316 | kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL); |
| 1317 | if (kc[i].name == NULL) |
| 1318 | goto err_str; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1319 | kc[i].private_value = (long)sm; |
| 1320 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 1321 | kc[i].access = mc->hdr.access; |
| 1322 | |
| 1323 | /* we only support FL/FR channel mapping atm */ |
| 1324 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, |
| 1325 | SNDRV_CHMAP_FL); |
| 1326 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, |
| 1327 | SNDRV_CHMAP_FR); |
| 1328 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, |
| 1329 | SNDRV_CHMAP_FL); |
| 1330 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, |
| 1331 | SNDRV_CHMAP_FR); |
| 1332 | |
| 1333 | sm->max = mc->max; |
| 1334 | sm->min = mc->min; |
| 1335 | sm->invert = mc->invert; |
| 1336 | sm->platform_max = mc->platform_max; |
| 1337 | sm->dobj.index = tplg->index; |
| 1338 | INIT_LIST_HEAD(&sm->dobj.list); |
| 1339 | |
| 1340 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1341 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1342 | if (err) { |
| 1343 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); |
| 1344 | kfree(sm); |
| 1345 | continue; |
| 1346 | } |
| 1347 | |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 1348 | /* create any TLV data */ |
| 1349 | soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr); |
| 1350 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1351 | /* pass control to driver for optional further init */ |
| 1352 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1353 | (struct snd_soc_tplg_ctl_hdr *)mc); |
| 1354 | if (err < 0) { |
| 1355 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1356 | mc->hdr.name); |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 1357 | soc_tplg_free_tlv(tplg, &kc[i]); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1358 | kfree(sm); |
| 1359 | continue; |
| 1360 | } |
| 1361 | } |
| 1362 | return kc; |
| 1363 | |
| 1364 | err_str: |
| 1365 | kfree(sm); |
| 1366 | err: |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1367 | for (--i; i >= 0; i--) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1368 | kfree((void *)kc[i].private_value); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1369 | kfree(kc[i].name); |
| 1370 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1371 | kfree(kc); |
| 1372 | return NULL; |
| 1373 | } |
| 1374 | |
| 1375 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1376 | struct soc_tplg *tplg, int num_kcontrols) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1377 | { |
| 1378 | struct snd_kcontrol_new *kc; |
| 1379 | struct snd_soc_tplg_enum_control *ec; |
| 1380 | struct soc_enum *se; |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1381 | int i, j, err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1382 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1383 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1384 | if (kc == NULL) |
| 1385 | return NULL; |
| 1386 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1387 | for (i = 0; i < num_kcontrols; i++) { |
| 1388 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; |
| 1389 | /* validate kcontrol */ |
| 1390 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1391 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
Christophe JAILLET | f3ee909 | 2017-09-14 22:44:24 +0200 | [diff] [blame] | 1392 | goto err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1393 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1394 | se = kzalloc(sizeof(*se), GFP_KERNEL); |
| 1395 | if (se == NULL) |
| 1396 | goto err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1397 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1398 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + |
| 1399 | ec->priv.size); |
| 1400 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1401 | dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n", |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1402 | ec->hdr.name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1403 | |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1404 | kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL); |
Dan Carpenter | 65030ff | 2018-04-05 14:25:18 +0300 | [diff] [blame] | 1405 | if (kc[i].name == NULL) { |
| 1406 | kfree(se); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1407 | goto err_se; |
Dan Carpenter | 65030ff | 2018-04-05 14:25:18 +0300 | [diff] [blame] | 1408 | } |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1409 | kc[i].private_value = (long)se; |
| 1410 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 1411 | kc[i].access = ec->hdr.access; |
| 1412 | |
| 1413 | /* we only support FL/FR channel mapping atm */ |
| 1414 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 1415 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, |
| 1416 | SNDRV_CHMAP_FL); |
| 1417 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, |
| 1418 | SNDRV_CHMAP_FR); |
| 1419 | |
| 1420 | se->items = ec->items; |
| 1421 | se->mask = ec->mask; |
| 1422 | se->dobj.index = tplg->index; |
| 1423 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1424 | switch (le32_to_cpu(ec->hdr.ops.info)) { |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1425 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1426 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1427 | err = soc_tplg_denum_create_values(se, ec); |
| 1428 | if (err < 0) { |
| 1429 | dev_err(tplg->dev, "ASoC: could not create values for %s\n", |
| 1430 | ec->hdr.name); |
| 1431 | goto err_se; |
| 1432 | } |
Takashi Iwai | 9c6c4d9 | 2018-10-04 20:30:06 +0200 | [diff] [blame] | 1433 | /* fall through */ |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1434 | case SND_SOC_TPLG_CTL_ENUM: |
| 1435 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1436 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1437 | err = soc_tplg_denum_create_texts(se, ec); |
| 1438 | if (err < 0) { |
| 1439 | dev_err(tplg->dev, "ASoC: could not create texts for %s\n", |
| 1440 | ec->hdr.name); |
| 1441 | goto err_se; |
| 1442 | } |
| 1443 | break; |
| 1444 | default: |
| 1445 | dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n", |
| 1446 | ec->hdr.ops.info, ec->hdr.name); |
| 1447 | goto err_se; |
| 1448 | } |
| 1449 | |
| 1450 | /* map io handlers */ |
| 1451 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg); |
| 1452 | if (err) { |
| 1453 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); |
| 1454 | goto err_se; |
| 1455 | } |
| 1456 | |
| 1457 | /* pass control to driver for optional further init */ |
| 1458 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1459 | (struct snd_soc_tplg_ctl_hdr *)ec); |
| 1460 | if (err < 0) { |
| 1461 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1462 | ec->hdr.name); |
| 1463 | goto err_se; |
| 1464 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | return kc; |
| 1468 | |
| 1469 | err_se: |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1470 | for (; i >= 0; i--) { |
| 1471 | /* free values and texts */ |
| 1472 | se = (struct soc_enum *)kc[i].private_value; |
Christophe JAILLET | 6d5574e | 2017-09-14 22:44:12 +0200 | [diff] [blame] | 1473 | if (!se) |
| 1474 | continue; |
| 1475 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1476 | kfree(se->dobj.control.dvalues); |
| 1477 | for (j = 0; j < ec->items; j++) |
| 1478 | kfree(se->dobj.control.dtexts[j]); |
Amadeusz Sławiński | 34db6a3 | 2019-01-25 14:06:44 -0600 | [diff] [blame] | 1479 | kfree(se->dobj.control.dtexts); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1480 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1481 | kfree(se); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1482 | kfree(kc[i].name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1483 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1484 | err: |
| 1485 | kfree(kc); |
| 1486 | |
| 1487 | return NULL; |
| 1488 | } |
| 1489 | |
| 1490 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( |
| 1491 | struct soc_tplg *tplg, int count) |
| 1492 | { |
| 1493 | struct snd_soc_tplg_bytes_control *be; |
| 1494 | struct soc_bytes_ext *sbe; |
| 1495 | struct snd_kcontrol_new *kc; |
| 1496 | int i, err; |
| 1497 | |
Axel Lin | 4ca7deb | 2015-08-05 22:34:22 +0800 | [diff] [blame] | 1498 | kc = kcalloc(count, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1499 | if (!kc) |
| 1500 | return NULL; |
| 1501 | |
| 1502 | for (i = 0; i < count; i++) { |
| 1503 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; |
| 1504 | |
| 1505 | /* validate kcontrol */ |
| 1506 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1507 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1508 | goto err; |
| 1509 | |
| 1510 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); |
| 1511 | if (sbe == NULL) |
| 1512 | goto err; |
| 1513 | |
| 1514 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1515 | le32_to_cpu(be->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1516 | |
| 1517 | dev_dbg(tplg->dev, |
| 1518 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", |
| 1519 | be->hdr.name, be->hdr.access); |
| 1520 | |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1521 | kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL); |
Dan Carpenter | 65030ff | 2018-04-05 14:25:18 +0300 | [diff] [blame] | 1522 | if (kc[i].name == NULL) { |
| 1523 | kfree(sbe); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1524 | goto err; |
Dan Carpenter | 65030ff | 2018-04-05 14:25:18 +0300 | [diff] [blame] | 1525 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1526 | kc[i].private_value = (long)sbe; |
| 1527 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 1528 | kc[i].access = be->hdr.access; |
| 1529 | |
| 1530 | sbe->max = be->max; |
| 1531 | INIT_LIST_HEAD(&sbe->dobj.list); |
| 1532 | |
| 1533 | /* map standard io handlers and check for external handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1534 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1535 | if (err) { |
| 1536 | soc_control_err(tplg, &be->hdr, be->hdr.name); |
| 1537 | kfree(sbe); |
| 1538 | continue; |
| 1539 | } |
| 1540 | |
| 1541 | /* pass control to driver for optional further init */ |
| 1542 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1543 | (struct snd_soc_tplg_ctl_hdr *)be); |
| 1544 | if (err < 0) { |
| 1545 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1546 | be->hdr.name); |
| 1547 | kfree(sbe); |
| 1548 | continue; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | return kc; |
| 1553 | |
| 1554 | err: |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1555 | for (--i; i >= 0; i--) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1556 | kfree((void *)kc[i].private_value); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1557 | kfree(kc[i].name); |
| 1558 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1559 | |
| 1560 | kfree(kc); |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
| 1564 | static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, |
| 1565 | struct snd_soc_tplg_dapm_widget *w) |
| 1566 | { |
| 1567 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; |
| 1568 | struct snd_soc_dapm_widget template, *widget; |
| 1569 | struct snd_soc_tplg_ctl_hdr *control_hdr; |
| 1570 | struct snd_soc_card *card = tplg->comp->card; |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1571 | unsigned int kcontrol_type; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1572 | int ret = 0; |
| 1573 | |
| 1574 | if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1575 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1576 | return -EINVAL; |
| 1577 | if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1578 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1579 | return -EINVAL; |
| 1580 | |
| 1581 | dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n", |
| 1582 | w->name, w->id); |
| 1583 | |
| 1584 | memset(&template, 0, sizeof(template)); |
| 1585 | |
| 1586 | /* map user to kernel widget ID */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1587 | template.id = get_widget_id(le32_to_cpu(w->id)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1588 | if (template.id < 0) |
| 1589 | return template.id; |
| 1590 | |
Liam Girdwood | c3421a6 | 2017-06-06 15:45:09 +0100 | [diff] [blame] | 1591 | /* strings are allocated here, but used and freed by the widget */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1592 | template.name = kstrdup(w->name, GFP_KERNEL); |
| 1593 | if (!template.name) |
| 1594 | return -ENOMEM; |
| 1595 | template.sname = kstrdup(w->sname, GFP_KERNEL); |
| 1596 | if (!template.sname) { |
| 1597 | ret = -ENOMEM; |
| 1598 | goto err; |
| 1599 | } |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1600 | template.reg = le32_to_cpu(w->reg); |
| 1601 | template.shift = le32_to_cpu(w->shift); |
| 1602 | template.mask = le32_to_cpu(w->mask); |
| 1603 | template.subseq = le32_to_cpu(w->subseq); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1604 | template.on_val = w->invert ? 0 : 1; |
| 1605 | template.off_val = w->invert ? 1 : 0; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1606 | template.ignore_suspend = le32_to_cpu(w->ignore_suspend); |
| 1607 | template.event_flags = le16_to_cpu(w->event_flags); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1608 | template.dobj.index = tplg->index; |
| 1609 | |
| 1610 | tplg->pos += |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1611 | (sizeof(struct snd_soc_tplg_dapm_widget) + |
| 1612 | le32_to_cpu(w->priv.size)); |
| 1613 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1614 | if (w->num_kcontrols == 0) { |
Arnd Bergmann | dd5abb7 | 2016-12-09 12:51:46 +0100 | [diff] [blame] | 1615 | kcontrol_type = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1616 | template.num_kcontrols = 0; |
| 1617 | goto widget; |
| 1618 | } |
| 1619 | |
| 1620 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; |
| 1621 | dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n", |
| 1622 | w->name, w->num_kcontrols, control_hdr->type); |
| 1623 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1624 | switch (le32_to_cpu(control_hdr->ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1625 | case SND_SOC_TPLG_CTL_VOLSW: |
| 1626 | case SND_SOC_TPLG_CTL_STROBE: |
| 1627 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 1628 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 1629 | case SND_SOC_TPLG_CTL_RANGE: |
| 1630 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1631 | kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1632 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1633 | template.kcontrol_news = |
| 1634 | soc_tplg_dapm_widget_dmixer_create(tplg, |
| 1635 | template.num_kcontrols); |
| 1636 | if (!template.kcontrol_news) { |
| 1637 | ret = -ENOMEM; |
| 1638 | goto hdr_err; |
| 1639 | } |
| 1640 | break; |
| 1641 | case SND_SOC_TPLG_CTL_ENUM: |
| 1642 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1643 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1644 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1645 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1646 | kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1647 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1648 | template.kcontrol_news = |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1649 | soc_tplg_dapm_widget_denum_create(tplg, |
| 1650 | template.num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1651 | if (!template.kcontrol_news) { |
| 1652 | ret = -ENOMEM; |
| 1653 | goto hdr_err; |
| 1654 | } |
| 1655 | break; |
| 1656 | case SND_SOC_TPLG_CTL_BYTES: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1657 | kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1658 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1659 | template.kcontrol_news = |
| 1660 | soc_tplg_dapm_widget_dbytes_create(tplg, |
| 1661 | template.num_kcontrols); |
| 1662 | if (!template.kcontrol_news) { |
| 1663 | ret = -ENOMEM; |
| 1664 | goto hdr_err; |
| 1665 | } |
| 1666 | break; |
| 1667 | default: |
| 1668 | dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n", |
| 1669 | control_hdr->ops.get, control_hdr->ops.put, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1670 | le32_to_cpu(control_hdr->ops.info)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1671 | ret = -EINVAL; |
| 1672 | goto hdr_err; |
| 1673 | } |
| 1674 | |
| 1675 | widget: |
| 1676 | ret = soc_tplg_widget_load(tplg, &template, w); |
| 1677 | if (ret < 0) |
| 1678 | goto hdr_err; |
| 1679 | |
| 1680 | /* card dapm mutex is held by the core if we are loading topology |
| 1681 | * data during sound card init. */ |
| 1682 | if (card->instantiated) |
| 1683 | widget = snd_soc_dapm_new_control(dapm, &template); |
| 1684 | else |
| 1685 | widget = snd_soc_dapm_new_control_unlocked(dapm, &template); |
Linus Walleij | 37e1df8 | 2017-01-13 10:23:52 +0100 | [diff] [blame] | 1686 | if (IS_ERR(widget)) { |
| 1687 | ret = PTR_ERR(widget); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1688 | goto hdr_err; |
| 1689 | } |
| 1690 | |
| 1691 | widget->dobj.type = SND_SOC_DOBJ_WIDGET; |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1692 | widget->dobj.widget.kcontrol_type = kcontrol_type; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1693 | widget->dobj.ops = tplg->ops; |
| 1694 | widget->dobj.index = tplg->index; |
| 1695 | list_add(&widget->dobj.list, &tplg->comp->dobj_list); |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 1696 | |
| 1697 | ret = soc_tplg_widget_ready(tplg, widget, w); |
| 1698 | if (ret < 0) |
| 1699 | goto ready_err; |
| 1700 | |
Bard liao | 7620fe9 | 2019-01-25 14:06:45 -0600 | [diff] [blame] | 1701 | kfree(template.sname); |
| 1702 | kfree(template.name); |
| 1703 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1704 | return 0; |
| 1705 | |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 1706 | ready_err: |
| 1707 | snd_soc_tplg_widget_remove(widget); |
| 1708 | snd_soc_dapm_free_widget(widget); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1709 | hdr_err: |
| 1710 | kfree(template.sname); |
| 1711 | err: |
| 1712 | kfree(template.name); |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, |
| 1717 | struct snd_soc_tplg_hdr *hdr) |
| 1718 | { |
| 1719 | struct snd_soc_tplg_dapm_widget *widget; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1720 | int ret, count, i; |
| 1721 | |
| 1722 | count = le32_to_cpu(hdr->count); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1723 | |
| 1724 | if (tplg->pass != SOC_TPLG_PASS_WIDGET) |
| 1725 | return 0; |
| 1726 | |
| 1727 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); |
| 1728 | |
| 1729 | for (i = 0; i < count; i++) { |
| 1730 | widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1731 | if (le32_to_cpu(widget->size) != sizeof(*widget)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 1732 | dev_err(tplg->dev, "ASoC: invalid widget size\n"); |
| 1733 | return -EINVAL; |
| 1734 | } |
| 1735 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1736 | ret = soc_tplg_dapm_widget_create(tplg, widget); |
Mengdong Lin | 7de76b6 | 2016-04-27 14:52:38 +0800 | [diff] [blame] | 1737 | if (ret < 0) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1738 | dev_err(tplg->dev, "ASoC: failed to load widget %s\n", |
| 1739 | widget->name); |
Mengdong Lin | 7de76b6 | 2016-04-27 14:52:38 +0800 | [diff] [blame] | 1740 | return ret; |
| 1741 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | return 0; |
| 1745 | } |
| 1746 | |
| 1747 | static int soc_tplg_dapm_complete(struct soc_tplg *tplg) |
| 1748 | { |
| 1749 | struct snd_soc_card *card = tplg->comp->card; |
| 1750 | int ret; |
| 1751 | |
| 1752 | /* Card might not have been registered at this point. |
| 1753 | * If so, just return success. |
| 1754 | */ |
| 1755 | if (!card || !card->instantiated) { |
| 1756 | dev_warn(tplg->dev, "ASoC: Parent card not yet available," |
Liam Girdwood | cc9d471 | 2017-06-06 15:45:08 +0100 | [diff] [blame] | 1757 | " widget card binding deferred\n"); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | ret = snd_soc_dapm_new_widgets(card); |
| 1762 | if (ret < 0) |
| 1763 | dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", |
| 1764 | ret); |
| 1765 | |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
Mengdong Lin | b6b6e4d | 2016-02-22 16:29:19 +0800 | [diff] [blame] | 1769 | static void set_stream_info(struct snd_soc_pcm_stream *stream, |
| 1770 | struct snd_soc_tplg_stream_caps *caps) |
| 1771 | { |
| 1772 | stream->stream_name = kstrdup(caps->name, GFP_KERNEL); |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1773 | stream->channels_min = le32_to_cpu(caps->channels_min); |
| 1774 | stream->channels_max = le32_to_cpu(caps->channels_max); |
| 1775 | stream->rates = le32_to_cpu(caps->rates); |
| 1776 | stream->rate_min = le32_to_cpu(caps->rate_min); |
| 1777 | stream->rate_max = le32_to_cpu(caps->rate_max); |
| 1778 | stream->formats = le64_to_cpu(caps->formats); |
| 1779 | stream->sig_bits = le32_to_cpu(caps->sig_bits); |
Mengdong Lin | b6b6e4d | 2016-02-22 16:29:19 +0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 1782 | static void set_dai_flags(struct snd_soc_dai_driver *dai_drv, |
| 1783 | unsigned int flag_mask, unsigned int flags) |
| 1784 | { |
| 1785 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) |
| 1786 | dai_drv->symmetric_rates = |
| 1787 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0; |
| 1788 | |
| 1789 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) |
| 1790 | dai_drv->symmetric_channels = |
| 1791 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ? |
| 1792 | 1 : 0; |
| 1793 | |
| 1794 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) |
| 1795 | dai_drv->symmetric_samplebits = |
| 1796 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ? |
| 1797 | 1 : 0; |
| 1798 | } |
| 1799 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1800 | static int soc_tplg_dai_create(struct soc_tplg *tplg, |
| 1801 | struct snd_soc_tplg_pcm *pcm) |
| 1802 | { |
| 1803 | struct snd_soc_dai_driver *dai_drv; |
| 1804 | struct snd_soc_pcm_stream *stream; |
| 1805 | struct snd_soc_tplg_stream_caps *caps; |
| 1806 | int ret; |
| 1807 | |
| 1808 | dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL); |
| 1809 | if (dai_drv == NULL) |
| 1810 | return -ENOMEM; |
| 1811 | |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1812 | if (strlen(pcm->dai_name)) |
| 1813 | dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL); |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1814 | dai_drv->id = le32_to_cpu(pcm->dai_id); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1815 | |
| 1816 | if (pcm->playback) { |
| 1817 | stream = &dai_drv->playback; |
| 1818 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; |
Mengdong Lin | b6b6e4d | 2016-02-22 16:29:19 +0800 | [diff] [blame] | 1819 | set_stream_info(stream, caps); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | if (pcm->capture) { |
| 1823 | stream = &dai_drv->capture; |
| 1824 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; |
Mengdong Lin | b6b6e4d | 2016-02-22 16:29:19 +0800 | [diff] [blame] | 1825 | set_stream_info(stream, caps); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1826 | } |
| 1827 | |
Liam Girdwood | 5db6aab | 2018-03-27 14:30:45 +0100 | [diff] [blame] | 1828 | if (pcm->compress) |
| 1829 | dai_drv->compress_new = snd_soc_new_compress; |
| 1830 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1831 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 1832 | ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1833 | if (ret < 0) { |
| 1834 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); |
Bard liao | 7b6f68a | 2019-03-05 23:57:52 +0800 | [diff] [blame] | 1835 | kfree(dai_drv->playback.stream_name); |
| 1836 | kfree(dai_drv->capture.stream_name); |
| 1837 | kfree(dai_drv->name); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1838 | kfree(dai_drv); |
| 1839 | return ret; |
| 1840 | } |
| 1841 | |
| 1842 | dai_drv->dobj.index = tplg->index; |
| 1843 | dai_drv->dobj.ops = tplg->ops; |
| 1844 | dai_drv->dobj.type = SND_SOC_DOBJ_PCM; |
| 1845 | list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); |
| 1846 | |
| 1847 | /* register the DAI to the component */ |
| 1848 | return snd_soc_register_dai(tplg->comp, dai_drv); |
| 1849 | } |
| 1850 | |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1851 | static void set_link_flags(struct snd_soc_dai_link *link, |
| 1852 | unsigned int flag_mask, unsigned int flags) |
| 1853 | { |
| 1854 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) |
| 1855 | link->symmetric_rates = |
| 1856 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0; |
| 1857 | |
| 1858 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) |
| 1859 | link->symmetric_channels = |
| 1860 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ? |
| 1861 | 1 : 0; |
| 1862 | |
| 1863 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) |
| 1864 | link->symmetric_samplebits = |
| 1865 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ? |
| 1866 | 1 : 0; |
Mengdong Lin | 6ff67cc | 2016-11-03 01:05:32 +0800 | [diff] [blame] | 1867 | |
| 1868 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) |
| 1869 | link->ignore_suspend = |
| 1870 | flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ? |
| 1871 | 1 : 0; |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1872 | } |
| 1873 | |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1874 | /* create the FE DAI link */ |
Mengdong Lin | ab4bc5e | 2016-11-03 01:04:42 +0800 | [diff] [blame] | 1875 | static int soc_tplg_fe_link_create(struct soc_tplg *tplg, |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1876 | struct snd_soc_tplg_pcm *pcm) |
| 1877 | { |
| 1878 | struct snd_soc_dai_link *link; |
| 1879 | int ret; |
| 1880 | |
| 1881 | link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL); |
| 1882 | if (link == NULL) |
| 1883 | return -ENOMEM; |
| 1884 | |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1885 | if (strlen(pcm->pcm_name)) { |
| 1886 | link->name = kstrdup(pcm->pcm_name, GFP_KERNEL); |
| 1887 | link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL); |
| 1888 | } |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1889 | link->id = le32_to_cpu(pcm->pcm_id); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1890 | |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1891 | if (strlen(pcm->dai_name)) |
| 1892 | link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL); |
| 1893 | |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1894 | link->codec_name = "snd-soc-dummy"; |
| 1895 | link->codec_dai_name = "snd-soc-dummy-dai"; |
| 1896 | |
| 1897 | /* enable DPCM */ |
| 1898 | link->dynamic = 1; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1899 | link->dpcm_playback = le32_to_cpu(pcm->playback); |
| 1900 | link->dpcm_capture = le32_to_cpu(pcm->capture); |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1901 | if (pcm->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1902 | set_link_flags(link, |
| 1903 | le32_to_cpu(pcm->flag_mask), |
| 1904 | le32_to_cpu(pcm->flags)); |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1905 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1906 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 1907 | ret = soc_tplg_dai_link_load(tplg, link, NULL); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1908 | if (ret < 0) { |
| 1909 | dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n"); |
Bard liao | b3718b8 | 2019-03-05 23:57:53 +0800 | [diff] [blame] | 1910 | kfree(link->name); |
| 1911 | kfree(link->stream_name); |
| 1912 | kfree(link->cpu_dai_name); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1913 | kfree(link); |
| 1914 | return ret; |
| 1915 | } |
| 1916 | |
| 1917 | link->dobj.index = tplg->index; |
| 1918 | link->dobj.ops = tplg->ops; |
| 1919 | link->dobj.type = SND_SOC_DOBJ_DAI_LINK; |
| 1920 | list_add(&link->dobj.list, &tplg->comp->dobj_list); |
| 1921 | |
| 1922 | snd_soc_add_dai_link(tplg->comp->card, link); |
| 1923 | return 0; |
| 1924 | } |
| 1925 | |
| 1926 | /* create a FE DAI and DAI link from the PCM object */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1927 | static int soc_tplg_pcm_create(struct soc_tplg *tplg, |
| 1928 | struct snd_soc_tplg_pcm *pcm) |
| 1929 | { |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1930 | int ret; |
| 1931 | |
| 1932 | ret = soc_tplg_dai_create(tplg, pcm); |
| 1933 | if (ret < 0) |
| 1934 | return ret; |
| 1935 | |
Mengdong Lin | ab4bc5e | 2016-11-03 01:04:42 +0800 | [diff] [blame] | 1936 | return soc_tplg_fe_link_create(tplg, pcm); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1937 | } |
| 1938 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 1939 | /* copy stream caps from the old version 4 of source */ |
| 1940 | static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest, |
| 1941 | struct snd_soc_tplg_stream_caps_v4 *src) |
| 1942 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1943 | dest->size = cpu_to_le32(sizeof(*dest)); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 1944 | memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 1945 | dest->formats = src->formats; |
| 1946 | dest->rates = src->rates; |
| 1947 | dest->rate_min = src->rate_min; |
| 1948 | dest->rate_max = src->rate_max; |
| 1949 | dest->channels_min = src->channels_min; |
| 1950 | dest->channels_max = src->channels_max; |
| 1951 | dest->periods_min = src->periods_min; |
| 1952 | dest->periods_max = src->periods_max; |
| 1953 | dest->period_size_min = src->period_size_min; |
| 1954 | dest->period_size_max = src->period_size_max; |
| 1955 | dest->buffer_size_min = src->buffer_size_min; |
| 1956 | dest->buffer_size_max = src->buffer_size_max; |
| 1957 | } |
| 1958 | |
| 1959 | /** |
| 1960 | * pcm_new_ver - Create the new version of PCM from the old version. |
| 1961 | * @tplg: topology context |
| 1962 | * @src: older version of pcm as a source |
| 1963 | * @pcm: latest version of pcm created from the source |
| 1964 | * |
| 1965 | * Support from vesion 4. User should free the returned pcm manually. |
| 1966 | */ |
| 1967 | static int pcm_new_ver(struct soc_tplg *tplg, |
| 1968 | struct snd_soc_tplg_pcm *src, |
| 1969 | struct snd_soc_tplg_pcm **pcm) |
| 1970 | { |
| 1971 | struct snd_soc_tplg_pcm *dest; |
| 1972 | struct snd_soc_tplg_pcm_v4 *src_v4; |
| 1973 | int i; |
| 1974 | |
| 1975 | *pcm = NULL; |
| 1976 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1977 | if (le32_to_cpu(src->size) != sizeof(*src_v4)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 1978 | dev_err(tplg->dev, "ASoC: invalid PCM size\n"); |
| 1979 | return -EINVAL; |
| 1980 | } |
| 1981 | |
| 1982 | dev_warn(tplg->dev, "ASoC: old version of PCM\n"); |
| 1983 | src_v4 = (struct snd_soc_tplg_pcm_v4 *)src; |
| 1984 | dest = kzalloc(sizeof(*dest), GFP_KERNEL); |
| 1985 | if (!dest) |
| 1986 | return -ENOMEM; |
| 1987 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1988 | dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 1989 | memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 1990 | memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 1991 | dest->pcm_id = src_v4->pcm_id; |
| 1992 | dest->dai_id = src_v4->dai_id; |
| 1993 | dest->playback = src_v4->playback; |
| 1994 | dest->capture = src_v4->capture; |
| 1995 | dest->compress = src_v4->compress; |
| 1996 | dest->num_streams = src_v4->num_streams; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 1997 | for (i = 0; i < le32_to_cpu(dest->num_streams); i++) |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 1998 | memcpy(&dest->stream[i], &src_v4->stream[i], |
| 1999 | sizeof(struct snd_soc_tplg_stream)); |
| 2000 | |
| 2001 | for (i = 0; i < 2; i++) |
| 2002 | stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]); |
| 2003 | |
| 2004 | *pcm = dest; |
| 2005 | return 0; |
| 2006 | } |
| 2007 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2008 | static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2009 | struct snd_soc_tplg_hdr *hdr) |
| 2010 | { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2011 | struct snd_soc_tplg_pcm *pcm, *_pcm; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2012 | int count; |
| 2013 | int size; |
Vinod Koul | fd34045 | 2016-12-08 23:01:27 +0530 | [diff] [blame] | 2014 | int i; |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2015 | bool abi_match; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2016 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2017 | count = le32_to_cpu(hdr->count); |
| 2018 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2019 | if (tplg->pass != SOC_TPLG_PASS_PCM_DAI) |
| 2020 | return 0; |
| 2021 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2022 | /* check the element size and count */ |
| 2023 | pcm = (struct snd_soc_tplg_pcm *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2024 | size = le32_to_cpu(pcm->size); |
| 2025 | if (size > sizeof(struct snd_soc_tplg_pcm) |
| 2026 | || size < sizeof(struct snd_soc_tplg_pcm_v4)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2027 | dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2028 | size); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2029 | return -EINVAL; |
| 2030 | } |
| 2031 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2032 | if (soc_tplg_check_elem_count(tplg, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2033 | size, count, |
| 2034 | le32_to_cpu(hdr->payload_size), |
| 2035 | "PCM DAI")) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2036 | dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n", |
| 2037 | count); |
| 2038 | return -EINVAL; |
| 2039 | } |
| 2040 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2041 | for (i = 0; i < count; i++) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2042 | pcm = (struct snd_soc_tplg_pcm *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2043 | size = le32_to_cpu(pcm->size); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2044 | |
| 2045 | /* check ABI version by size, create a new version of pcm |
| 2046 | * if abi not match. |
| 2047 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2048 | if (size == sizeof(*pcm)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2049 | abi_match = true; |
| 2050 | _pcm = pcm; |
| 2051 | } else { |
| 2052 | abi_match = false; |
Vinod Koul | fd34045 | 2016-12-08 23:01:27 +0530 | [diff] [blame] | 2053 | pcm_new_ver(tplg, pcm, &_pcm); |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2054 | } |
| 2055 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2056 | /* create the FE DAIs and DAI links */ |
| 2057 | soc_tplg_pcm_create(tplg, _pcm); |
| 2058 | |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 2059 | /* offset by version-specific struct size and |
| 2060 | * real priv data size |
| 2061 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2062 | tplg->pos += size + le32_to_cpu(_pcm->priv.size); |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 2063 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2064 | if (!abi_match) |
| 2065 | kfree(_pcm); /* free the duplicated one */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2066 | } |
| 2067 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2068 | dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2069 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2070 | return 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2071 | } |
| 2072 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2073 | /** |
| 2074 | * set_link_hw_format - Set the HW audio format of the physical DAI link. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2075 | * @link: &snd_soc_dai_link which should be updated |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2076 | * @cfg: physical link configs. |
| 2077 | * |
| 2078 | * Topology context contains a list of supported HW formats (configs) and |
| 2079 | * a default format ID for the physical link. This function will use this |
| 2080 | * default ID to choose the HW format to set the link's DAI format for init. |
| 2081 | */ |
| 2082 | static void set_link_hw_format(struct snd_soc_dai_link *link, |
| 2083 | struct snd_soc_tplg_link_config *cfg) |
| 2084 | { |
| 2085 | struct snd_soc_tplg_hw_config *hw_config; |
| 2086 | unsigned char bclk_master, fsync_master; |
| 2087 | unsigned char invert_bclk, invert_fsync; |
| 2088 | int i; |
| 2089 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2090 | for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2091 | hw_config = &cfg->hw_config[i]; |
| 2092 | if (hw_config->id != cfg->default_hw_config_id) |
| 2093 | continue; |
| 2094 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2095 | link->dai_fmt = le32_to_cpu(hw_config->fmt) & |
| 2096 | SND_SOC_DAIFMT_FORMAT_MASK; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2097 | |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2098 | /* clock gating */ |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2099 | switch (hw_config->clock_gated) { |
| 2100 | case SND_SOC_TPLG_DAI_CLK_GATE_GATED: |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2101 | link->dai_fmt |= SND_SOC_DAIFMT_GATED; |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2102 | break; |
| 2103 | |
| 2104 | case SND_SOC_TPLG_DAI_CLK_GATE_CONT: |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2105 | link->dai_fmt |= SND_SOC_DAIFMT_CONT; |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2106 | break; |
| 2107 | |
| 2108 | default: |
| 2109 | /* ignore the value */ |
| 2110 | break; |
| 2111 | } |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2112 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2113 | /* clock signal polarity */ |
| 2114 | invert_bclk = hw_config->invert_bclk; |
| 2115 | invert_fsync = hw_config->invert_fsync; |
| 2116 | if (!invert_bclk && !invert_fsync) |
| 2117 | link->dai_fmt |= SND_SOC_DAIFMT_NB_NF; |
| 2118 | else if (!invert_bclk && invert_fsync) |
| 2119 | link->dai_fmt |= SND_SOC_DAIFMT_NB_IF; |
| 2120 | else if (invert_bclk && !invert_fsync) |
| 2121 | link->dai_fmt |= SND_SOC_DAIFMT_IB_NF; |
| 2122 | else |
| 2123 | link->dai_fmt |= SND_SOC_DAIFMT_IB_IF; |
| 2124 | |
| 2125 | /* clock masters */ |
Kirill Marinushkin | a941e2f | 2018-04-04 06:19:37 +0200 | [diff] [blame] | 2126 | bclk_master = (hw_config->bclk_master == |
| 2127 | SND_SOC_TPLG_BCLK_CM); |
| 2128 | fsync_master = (hw_config->fsync_master == |
| 2129 | SND_SOC_TPLG_FSYNC_CM); |
| 2130 | if (bclk_master && fsync_master) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2131 | link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2132 | else if (!bclk_master && fsync_master) |
Kirill Marinushkin | a941e2f | 2018-04-04 06:19:37 +0200 | [diff] [blame] | 2133 | link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; |
| 2134 | else if (bclk_master && !fsync_master) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2135 | link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; |
| 2136 | else |
| 2137 | link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | /** |
| 2142 | * link_new_ver - Create a new physical link config from the old |
| 2143 | * version of source. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2144 | * @tplg: topology context |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2145 | * @src: old version of phyical link config as a source |
| 2146 | * @link: latest version of physical link config created from the source |
| 2147 | * |
| 2148 | * Support from vesion 4. User need free the returned link config manually. |
| 2149 | */ |
| 2150 | static int link_new_ver(struct soc_tplg *tplg, |
| 2151 | struct snd_soc_tplg_link_config *src, |
| 2152 | struct snd_soc_tplg_link_config **link) |
| 2153 | { |
| 2154 | struct snd_soc_tplg_link_config *dest; |
| 2155 | struct snd_soc_tplg_link_config_v4 *src_v4; |
| 2156 | int i; |
| 2157 | |
| 2158 | *link = NULL; |
| 2159 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2160 | if (le32_to_cpu(src->size) != |
| 2161 | sizeof(struct snd_soc_tplg_link_config_v4)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2162 | dev_err(tplg->dev, "ASoC: invalid physical link config size\n"); |
| 2163 | return -EINVAL; |
| 2164 | } |
| 2165 | |
| 2166 | dev_warn(tplg->dev, "ASoC: old version of physical link config\n"); |
| 2167 | |
| 2168 | src_v4 = (struct snd_soc_tplg_link_config_v4 *)src; |
| 2169 | dest = kzalloc(sizeof(*dest), GFP_KERNEL); |
| 2170 | if (!dest) |
| 2171 | return -ENOMEM; |
| 2172 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2173 | dest->size = cpu_to_le32(sizeof(*dest)); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2174 | dest->id = src_v4->id; |
| 2175 | dest->num_streams = src_v4->num_streams; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2176 | for (i = 0; i < le32_to_cpu(dest->num_streams); i++) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2177 | memcpy(&dest->stream[i], &src_v4->stream[i], |
| 2178 | sizeof(struct snd_soc_tplg_stream)); |
| 2179 | |
| 2180 | *link = dest; |
| 2181 | return 0; |
| 2182 | } |
| 2183 | |
| 2184 | /* Find and configure an existing physical DAI link */ |
| 2185 | static int soc_tplg_link_config(struct soc_tplg *tplg, |
| 2186 | struct snd_soc_tplg_link_config *cfg) |
| 2187 | { |
| 2188 | struct snd_soc_dai_link *link; |
| 2189 | const char *name, *stream_name; |
Mengdong Lin | dbab1cb | 2016-11-05 08:42:14 +0800 | [diff] [blame] | 2190 | size_t len; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2191 | int ret; |
| 2192 | |
Mengdong Lin | dbab1cb | 2016-11-05 08:42:14 +0800 | [diff] [blame] | 2193 | len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2194 | if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 2195 | return -EINVAL; |
| 2196 | else if (len) |
| 2197 | name = cfg->name; |
| 2198 | else |
| 2199 | name = NULL; |
| 2200 | |
| 2201 | len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2202 | if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 2203 | return -EINVAL; |
| 2204 | else if (len) |
| 2205 | stream_name = cfg->stream_name; |
| 2206 | else |
| 2207 | stream_name = NULL; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2208 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2209 | link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id), |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2210 | name, stream_name); |
| 2211 | if (!link) { |
| 2212 | dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n", |
| 2213 | name, cfg->id); |
| 2214 | return -EINVAL; |
| 2215 | } |
| 2216 | |
| 2217 | /* hw format */ |
| 2218 | if (cfg->num_hw_configs) |
| 2219 | set_link_hw_format(link, cfg); |
| 2220 | |
| 2221 | /* flags */ |
| 2222 | if (cfg->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2223 | set_link_flags(link, |
| 2224 | le32_to_cpu(cfg->flag_mask), |
| 2225 | le32_to_cpu(cfg->flags)); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2226 | |
| 2227 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 2228 | ret = soc_tplg_dai_link_load(tplg, link, cfg); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2229 | if (ret < 0) { |
| 2230 | dev_err(tplg->dev, "ASoC: physical link loading failed\n"); |
| 2231 | return ret; |
| 2232 | } |
| 2233 | |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 2234 | /* for unloading it in snd_soc_tplg_component_remove */ |
| 2235 | link->dobj.index = tplg->index; |
| 2236 | link->dobj.ops = tplg->ops; |
| 2237 | link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK; |
| 2238 | list_add(&link->dobj.list, &tplg->comp->dobj_list); |
| 2239 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2240 | return 0; |
| 2241 | } |
| 2242 | |
| 2243 | |
| 2244 | /* Load physical link config elements from the topology context */ |
| 2245 | static int soc_tplg_link_elems_load(struct soc_tplg *tplg, |
| 2246 | struct snd_soc_tplg_hdr *hdr) |
| 2247 | { |
| 2248 | struct snd_soc_tplg_link_config *link, *_link; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2249 | int count; |
| 2250 | int size; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2251 | int i, ret; |
| 2252 | bool abi_match; |
| 2253 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2254 | count = le32_to_cpu(hdr->count); |
| 2255 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2256 | if (tplg->pass != SOC_TPLG_PASS_LINK) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2257 | tplg->pos += le32_to_cpu(hdr->size) + |
| 2258 | le32_to_cpu(hdr->payload_size); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2259 | return 0; |
| 2260 | }; |
| 2261 | |
| 2262 | /* check the element size and count */ |
| 2263 | link = (struct snd_soc_tplg_link_config *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2264 | size = le32_to_cpu(link->size); |
| 2265 | if (size > sizeof(struct snd_soc_tplg_link_config) |
| 2266 | || size < sizeof(struct snd_soc_tplg_link_config_v4)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2267 | dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2268 | size); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2269 | return -EINVAL; |
| 2270 | } |
| 2271 | |
| 2272 | if (soc_tplg_check_elem_count(tplg, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2273 | size, count, |
| 2274 | le32_to_cpu(hdr->payload_size), |
| 2275 | "physical link config")) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2276 | dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n", |
| 2277 | count); |
| 2278 | return -EINVAL; |
| 2279 | } |
| 2280 | |
| 2281 | /* config physical DAI links */ |
| 2282 | for (i = 0; i < count; i++) { |
| 2283 | link = (struct snd_soc_tplg_link_config *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2284 | size = le32_to_cpu(link->size); |
| 2285 | if (size == sizeof(*link)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2286 | abi_match = true; |
| 2287 | _link = link; |
| 2288 | } else { |
| 2289 | abi_match = false; |
| 2290 | ret = link_new_ver(tplg, link, &_link); |
| 2291 | if (ret < 0) |
| 2292 | return ret; |
| 2293 | } |
| 2294 | |
| 2295 | ret = soc_tplg_link_config(tplg, _link); |
| 2296 | if (ret < 0) |
| 2297 | return ret; |
| 2298 | |
| 2299 | /* offset by version-specific struct size and |
| 2300 | * real priv data size |
| 2301 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2302 | tplg->pos += size + le32_to_cpu(_link->priv.size); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2303 | |
| 2304 | if (!abi_match) |
| 2305 | kfree(_link); /* free the duplicated one */ |
| 2306 | } |
| 2307 | |
| 2308 | return 0; |
| 2309 | } |
| 2310 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2311 | /** |
| 2312 | * soc_tplg_dai_config - Find and configure an existing physical DAI. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2313 | * @tplg: topology context |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2314 | * @d: physical DAI configs. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2315 | * |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2316 | * The physical dai should already be registered by the platform driver. |
| 2317 | * The platform driver should specify the DAI name and ID for matching. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2318 | */ |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2319 | static int soc_tplg_dai_config(struct soc_tplg *tplg, |
| 2320 | struct snd_soc_tplg_dai *d) |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2321 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2322 | struct snd_soc_dai_link_component dai_component; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2323 | struct snd_soc_dai *dai; |
| 2324 | struct snd_soc_dai_driver *dai_drv; |
| 2325 | struct snd_soc_pcm_stream *stream; |
| 2326 | struct snd_soc_tplg_stream_caps *caps; |
| 2327 | int ret; |
| 2328 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2329 | memset(&dai_component, 0, sizeof(dai_component)); |
| 2330 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2331 | dai_component.dai_name = d->dai_name; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2332 | dai = snd_soc_find_dai(&dai_component); |
| 2333 | if (!dai) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2334 | dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n", |
| 2335 | d->dai_name); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2336 | return -EINVAL; |
| 2337 | } |
| 2338 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2339 | if (le32_to_cpu(d->dai_id) != dai->id) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2340 | dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n", |
| 2341 | d->dai_name); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2342 | return -EINVAL; |
| 2343 | } |
| 2344 | |
| 2345 | dai_drv = dai->driver; |
| 2346 | if (!dai_drv) |
| 2347 | return -EINVAL; |
| 2348 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2349 | if (d->playback) { |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2350 | stream = &dai_drv->playback; |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2351 | caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2352 | set_stream_info(stream, caps); |
| 2353 | } |
| 2354 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2355 | if (d->capture) { |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2356 | stream = &dai_drv->capture; |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2357 | caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE]; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2358 | set_stream_info(stream, caps); |
| 2359 | } |
| 2360 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2361 | if (d->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2362 | set_dai_flags(dai_drv, |
| 2363 | le32_to_cpu(d->flag_mask), |
| 2364 | le32_to_cpu(d->flags)); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2365 | |
| 2366 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 2367 | ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2368 | if (ret < 0) { |
| 2369 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); |
| 2370 | return ret; |
| 2371 | } |
| 2372 | |
| 2373 | return 0; |
| 2374 | } |
| 2375 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2376 | /* load physical DAI elements */ |
| 2377 | static int soc_tplg_dai_elems_load(struct soc_tplg *tplg, |
| 2378 | struct snd_soc_tplg_hdr *hdr) |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2379 | { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2380 | struct snd_soc_tplg_dai *dai; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2381 | int count; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2382 | int i; |
| 2383 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2384 | count = le32_to_cpu(hdr->count); |
| 2385 | |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2386 | if (tplg->pass != SOC_TPLG_PASS_BE_DAI) |
| 2387 | return 0; |
| 2388 | |
| 2389 | /* config the existing BE DAIs */ |
| 2390 | for (i = 0; i < count; i++) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2391 | dai = (struct snd_soc_tplg_dai *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2392 | if (le32_to_cpu(dai->size) != sizeof(*dai)) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2393 | dev_err(tplg->dev, "ASoC: invalid physical DAI size\n"); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2394 | return -EINVAL; |
| 2395 | } |
| 2396 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2397 | soc_tplg_dai_config(tplg, dai); |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2398 | tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size)); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count); |
| 2402 | return 0; |
| 2403 | } |
| 2404 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2405 | /** |
| 2406 | * manifest_new_ver - Create a new version of manifest from the old version |
| 2407 | * of source. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2408 | * @tplg: topology context |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2409 | * @src: old version of manifest as a source |
| 2410 | * @manifest: latest version of manifest created from the source |
| 2411 | * |
| 2412 | * Support from vesion 4. Users need free the returned manifest manually. |
| 2413 | */ |
| 2414 | static int manifest_new_ver(struct soc_tplg *tplg, |
| 2415 | struct snd_soc_tplg_manifest *src, |
| 2416 | struct snd_soc_tplg_manifest **manifest) |
| 2417 | { |
| 2418 | struct snd_soc_tplg_manifest *dest; |
| 2419 | struct snd_soc_tplg_manifest_v4 *src_v4; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2420 | int size; |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2421 | |
| 2422 | *manifest = NULL; |
| 2423 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2424 | size = le32_to_cpu(src->size); |
| 2425 | if (size != sizeof(*src_v4)) { |
Guenter Roeck | ac9391d | 2018-05-24 12:49:21 -0700 | [diff] [blame] | 2426 | dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2427 | size); |
| 2428 | if (size) |
Guenter Roeck | ac9391d | 2018-05-24 12:49:21 -0700 | [diff] [blame] | 2429 | return -EINVAL; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2430 | src->size = cpu_to_le32(sizeof(*src_v4)); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | dev_warn(tplg->dev, "ASoC: old version of manifest\n"); |
| 2434 | |
| 2435 | src_v4 = (struct snd_soc_tplg_manifest_v4 *)src; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2436 | dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size), |
| 2437 | GFP_KERNEL); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2438 | if (!dest) |
| 2439 | return -ENOMEM; |
| 2440 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2441 | dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2442 | dest->control_elems = src_v4->control_elems; |
| 2443 | dest->widget_elems = src_v4->widget_elems; |
| 2444 | dest->graph_elems = src_v4->graph_elems; |
| 2445 | dest->pcm_elems = src_v4->pcm_elems; |
| 2446 | dest->dai_link_elems = src_v4->dai_link_elems; |
| 2447 | dest->priv.size = src_v4->priv.size; |
| 2448 | if (dest->priv.size) |
| 2449 | memcpy(dest->priv.data, src_v4->priv.data, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2450 | le32_to_cpu(src_v4->priv.size)); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2451 | |
| 2452 | *manifest = dest; |
| 2453 | return 0; |
| 2454 | } |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2455 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2456 | static int soc_tplg_manifest_load(struct soc_tplg *tplg, |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2457 | struct snd_soc_tplg_hdr *hdr) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2458 | { |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2459 | struct snd_soc_tplg_manifest *manifest, *_manifest; |
| 2460 | bool abi_match; |
| 2461 | int err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2462 | |
| 2463 | if (tplg->pass != SOC_TPLG_PASS_MANIFEST) |
| 2464 | return 0; |
| 2465 | |
| 2466 | manifest = (struct snd_soc_tplg_manifest *)tplg->pos; |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2467 | |
| 2468 | /* check ABI version by size, create a new manifest if abi not match */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2469 | if (le32_to_cpu(manifest->size) == sizeof(*manifest)) { |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2470 | abi_match = true; |
| 2471 | _manifest = manifest; |
| 2472 | } else { |
| 2473 | abi_match = false; |
| 2474 | err = manifest_new_ver(tplg, manifest, &_manifest); |
| 2475 | if (err < 0) |
| 2476 | return err; |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2477 | } |
| 2478 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2479 | /* pass control to component driver for optional further init */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2480 | if (tplg->comp && tplg->ops && tplg->ops->manifest) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 2481 | return tplg->ops->manifest(tplg->comp, tplg->index, _manifest); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2482 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2483 | if (!abi_match) /* free the duplicated one */ |
| 2484 | kfree(_manifest); |
| 2485 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2486 | return 0; |
| 2487 | } |
| 2488 | |
| 2489 | /* validate header magic, size and type */ |
| 2490 | static int soc_valid_header(struct soc_tplg *tplg, |
| 2491 | struct snd_soc_tplg_hdr *hdr) |
| 2492 | { |
| 2493 | if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size) |
| 2494 | return 0; |
| 2495 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2496 | if (le32_to_cpu(hdr->size) != sizeof(*hdr)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2497 | dev_err(tplg->dev, |
| 2498 | "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2499 | le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2500 | tplg->fw->size); |
| 2501 | return -EINVAL; |
| 2502 | } |
| 2503 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2504 | /* big endian firmware objects not supported atm */ |
| 2505 | if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) { |
| 2506 | dev_err(tplg->dev, |
| 2507 | "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", |
| 2508 | tplg->pass, hdr->magic, |
| 2509 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); |
| 2510 | return -EINVAL; |
| 2511 | } |
| 2512 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2513 | if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2514 | dev_err(tplg->dev, |
| 2515 | "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", |
| 2516 | tplg->pass, hdr->magic, |
| 2517 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); |
| 2518 | return -EINVAL; |
| 2519 | } |
| 2520 | |
Mengdong Lin | 288b8da | 2016-11-03 01:03:17 +0800 | [diff] [blame] | 2521 | /* Support ABI from version 4 */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2522 | if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION || |
| 2523 | le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2524 | dev_err(tplg->dev, |
| 2525 | "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", |
| 2526 | tplg->pass, hdr->abi, |
| 2527 | SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), |
| 2528 | tplg->fw->size); |
| 2529 | return -EINVAL; |
| 2530 | } |
| 2531 | |
| 2532 | if (hdr->payload_size == 0) { |
| 2533 | dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", |
| 2534 | soc_tplg_get_hdr_offset(tplg)); |
| 2535 | return -EINVAL; |
| 2536 | } |
| 2537 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2538 | if (tplg->pass == le32_to_cpu(hdr->type)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2539 | dev_dbg(tplg->dev, |
| 2540 | "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n", |
| 2541 | hdr->payload_size, hdr->type, hdr->version, |
| 2542 | hdr->vendor_type, tplg->pass); |
| 2543 | |
| 2544 | return 1; |
| 2545 | } |
| 2546 | |
| 2547 | /* check header type and call appropriate handler */ |
| 2548 | static int soc_tplg_load_header(struct soc_tplg *tplg, |
| 2549 | struct snd_soc_tplg_hdr *hdr) |
| 2550 | { |
| 2551 | tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); |
| 2552 | |
| 2553 | /* check for matching ID */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2554 | if (le32_to_cpu(hdr->index) != tplg->req_index && |
Liam Girdwood | bb97142 | 2017-06-29 14:22:25 +0100 | [diff] [blame] | 2555 | tplg->req_index != SND_SOC_TPLG_INDEX_ALL) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2556 | return 0; |
| 2557 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2558 | tplg->index = le32_to_cpu(hdr->index); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2559 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2560 | switch (le32_to_cpu(hdr->type)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2561 | case SND_SOC_TPLG_TYPE_MIXER: |
| 2562 | case SND_SOC_TPLG_TYPE_ENUM: |
| 2563 | case SND_SOC_TPLG_TYPE_BYTES: |
| 2564 | return soc_tplg_kcontrol_elems_load(tplg, hdr); |
| 2565 | case SND_SOC_TPLG_TYPE_DAPM_GRAPH: |
| 2566 | return soc_tplg_dapm_graph_elems_load(tplg, hdr); |
| 2567 | case SND_SOC_TPLG_TYPE_DAPM_WIDGET: |
| 2568 | return soc_tplg_dapm_widget_elems_load(tplg, hdr); |
| 2569 | case SND_SOC_TPLG_TYPE_PCM: |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2570 | return soc_tplg_pcm_elems_load(tplg, hdr); |
Mengdong Lin | 3fbf793 | 2016-11-03 01:05:01 +0800 | [diff] [blame] | 2571 | case SND_SOC_TPLG_TYPE_DAI: |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2572 | return soc_tplg_dai_elems_load(tplg, hdr); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2573 | case SND_SOC_TPLG_TYPE_DAI_LINK: |
| 2574 | case SND_SOC_TPLG_TYPE_BACKEND_LINK: |
| 2575 | /* physical link configurations */ |
| 2576 | return soc_tplg_link_elems_load(tplg, hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2577 | case SND_SOC_TPLG_TYPE_MANIFEST: |
| 2578 | return soc_tplg_manifest_load(tplg, hdr); |
| 2579 | default: |
| 2580 | /* bespoke vendor data object */ |
| 2581 | return soc_tplg_vendor_load(tplg, hdr); |
| 2582 | } |
| 2583 | |
| 2584 | return 0; |
| 2585 | } |
| 2586 | |
| 2587 | /* process the topology file headers */ |
| 2588 | static int soc_tplg_process_headers(struct soc_tplg *tplg) |
| 2589 | { |
| 2590 | struct snd_soc_tplg_hdr *hdr; |
| 2591 | int ret; |
| 2592 | |
| 2593 | tplg->pass = SOC_TPLG_PASS_START; |
| 2594 | |
| 2595 | /* process the header types from start to end */ |
| 2596 | while (tplg->pass <= SOC_TPLG_PASS_END) { |
| 2597 | |
| 2598 | tplg->hdr_pos = tplg->fw->data; |
| 2599 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; |
| 2600 | |
| 2601 | while (!soc_tplg_is_eof(tplg)) { |
| 2602 | |
| 2603 | /* make sure header is valid before loading */ |
| 2604 | ret = soc_valid_header(tplg, hdr); |
| 2605 | if (ret < 0) |
| 2606 | return ret; |
| 2607 | else if (ret == 0) |
| 2608 | break; |
| 2609 | |
| 2610 | /* load the header object */ |
| 2611 | ret = soc_tplg_load_header(tplg, hdr); |
| 2612 | if (ret < 0) |
| 2613 | return ret; |
| 2614 | |
| 2615 | /* goto next header */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame^] | 2616 | tplg->hdr_pos += le32_to_cpu(hdr->payload_size) + |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2617 | sizeof(struct snd_soc_tplg_hdr); |
| 2618 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; |
| 2619 | } |
| 2620 | |
| 2621 | /* next data type pass */ |
| 2622 | tplg->pass++; |
| 2623 | } |
| 2624 | |
| 2625 | /* signal DAPM we are complete */ |
| 2626 | ret = soc_tplg_dapm_complete(tplg); |
| 2627 | if (ret < 0) |
| 2628 | dev_err(tplg->dev, |
| 2629 | "ASoC: failed to initialise DAPM from Firmware\n"); |
| 2630 | |
| 2631 | return ret; |
| 2632 | } |
| 2633 | |
| 2634 | static int soc_tplg_load(struct soc_tplg *tplg) |
| 2635 | { |
| 2636 | int ret; |
| 2637 | |
| 2638 | ret = soc_tplg_process_headers(tplg); |
| 2639 | if (ret == 0) |
| 2640 | soc_tplg_complete(tplg); |
| 2641 | |
| 2642 | return ret; |
| 2643 | } |
| 2644 | |
| 2645 | /* load audio component topology from "firmware" file */ |
| 2646 | int snd_soc_tplg_component_load(struct snd_soc_component *comp, |
| 2647 | struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id) |
| 2648 | { |
| 2649 | struct soc_tplg tplg; |
Bard liao | 304017d | 2019-02-17 21:23:47 +0800 | [diff] [blame] | 2650 | int ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2651 | |
| 2652 | /* setup parsing context */ |
| 2653 | memset(&tplg, 0, sizeof(tplg)); |
| 2654 | tplg.fw = fw; |
| 2655 | tplg.dev = comp->dev; |
| 2656 | tplg.comp = comp; |
| 2657 | tplg.ops = ops; |
| 2658 | tplg.req_index = id; |
| 2659 | tplg.io_ops = ops->io_ops; |
| 2660 | tplg.io_ops_count = ops->io_ops_count; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 2661 | tplg.bytes_ext_ops = ops->bytes_ext_ops; |
| 2662 | tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2663 | |
Bard liao | 304017d | 2019-02-17 21:23:47 +0800 | [diff] [blame] | 2664 | ret = soc_tplg_load(&tplg); |
| 2665 | /* free the created components if fail to load topology */ |
| 2666 | if (ret) |
| 2667 | snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL); |
| 2668 | |
| 2669 | return ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2670 | } |
| 2671 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); |
| 2672 | |
| 2673 | /* remove this dynamic widget */ |
| 2674 | void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w) |
| 2675 | { |
| 2676 | /* make sure we are a widget */ |
| 2677 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET) |
| 2678 | return; |
| 2679 | |
| 2680 | remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET); |
| 2681 | } |
| 2682 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove); |
| 2683 | |
| 2684 | /* remove all dynamic widgets from this DAPM context */ |
| 2685 | void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm, |
| 2686 | u32 index) |
| 2687 | { |
| 2688 | struct snd_soc_dapm_widget *w, *next_w; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2689 | |
| 2690 | list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) { |
| 2691 | |
| 2692 | /* make sure we are a widget with correct context */ |
| 2693 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm) |
| 2694 | continue; |
| 2695 | |
| 2696 | /* match ID */ |
| 2697 | if (w->dobj.index != index && |
| 2698 | w->dobj.index != SND_SOC_TPLG_INDEX_ALL) |
| 2699 | continue; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2700 | /* check and free and dynamic widget kcontrols */ |
| 2701 | snd_soc_tplg_widget_remove(w); |
Lars-Peter Clausen | b97e269 | 2015-07-21 18:11:07 +0200 | [diff] [blame] | 2702 | snd_soc_dapm_free_widget(w); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2703 | } |
Jyri Sarha | fd589a1 | 2015-11-10 18:12:42 +0200 | [diff] [blame] | 2704 | snd_soc_dapm_reset_cache(dapm); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2705 | } |
| 2706 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all); |
| 2707 | |
| 2708 | /* remove dynamic controls from the component driver */ |
| 2709 | int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index) |
| 2710 | { |
| 2711 | struct snd_soc_dobj *dobj, *next_dobj; |
| 2712 | int pass = SOC_TPLG_PASS_END; |
| 2713 | |
| 2714 | /* process the header types from end to start */ |
| 2715 | while (pass >= SOC_TPLG_PASS_START) { |
| 2716 | |
| 2717 | /* remove mixer controls */ |
| 2718 | list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, |
| 2719 | list) { |
| 2720 | |
| 2721 | /* match index */ |
| 2722 | if (dobj->index != index && |
Yan Wang | feb12f0 | 2018-03-26 16:48:00 +0100 | [diff] [blame] | 2723 | index != SND_SOC_TPLG_INDEX_ALL) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2724 | continue; |
| 2725 | |
| 2726 | switch (dobj->type) { |
| 2727 | case SND_SOC_DOBJ_MIXER: |
| 2728 | remove_mixer(comp, dobj, pass); |
| 2729 | break; |
| 2730 | case SND_SOC_DOBJ_ENUM: |
| 2731 | remove_enum(comp, dobj, pass); |
| 2732 | break; |
| 2733 | case SND_SOC_DOBJ_BYTES: |
| 2734 | remove_bytes(comp, dobj, pass); |
| 2735 | break; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 2736 | case SND_SOC_DOBJ_GRAPH: |
| 2737 | remove_route(comp, dobj, pass); |
| 2738 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2739 | case SND_SOC_DOBJ_WIDGET: |
| 2740 | remove_widget(comp, dobj, pass); |
| 2741 | break; |
| 2742 | case SND_SOC_DOBJ_PCM: |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2743 | remove_dai(comp, dobj, pass); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2744 | break; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 2745 | case SND_SOC_DOBJ_DAI_LINK: |
| 2746 | remove_link(comp, dobj, pass); |
| 2747 | break; |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 2748 | case SND_SOC_DOBJ_BACKEND_LINK: |
| 2749 | /* |
| 2750 | * call link_unload ops if extra |
| 2751 | * deinitialization is needed. |
| 2752 | */ |
| 2753 | remove_backend_link(comp, dobj, pass); |
| 2754 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2755 | default: |
| 2756 | dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", |
| 2757 | dobj->type); |
| 2758 | break; |
| 2759 | } |
| 2760 | } |
| 2761 | pass--; |
| 2762 | } |
| 2763 | |
| 2764 | /* let caller know if FW can be freed when no objects are left */ |
| 2765 | return !list_empty(&comp->dobj_list); |
| 2766 | } |
| 2767 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); |