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