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