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