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 const 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, 135 snd_soc_put_volsw, snd_soc_info_volsw}, 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_control_err(struct soc_tplg *tplg, 224 struct snd_soc_tplg_ctl_hdr *hdr, const char *name) 225 { 226 dev_err(tplg->dev, 227 "ASoC: no complete control IO handler for %s type (g,p,i) %u:%u:%u at 0x%lx\n", 228 name, 229 le32_to_cpu(hdr->ops.get), 230 le32_to_cpu(hdr->ops.put), 231 le32_to_cpu(hdr->ops.info), 232 soc_tplg_get_offset(tplg)); 233 } 234 235 /* pass vendor data to component driver for processing */ 236 static int soc_tplg_vendor_load(struct soc_tplg *tplg, 237 struct snd_soc_tplg_hdr *hdr) 238 { 239 int ret = 0; 240 241 if (tplg->ops && tplg->ops->vendor_load) 242 ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr); 243 else { 244 dev_err(tplg->dev, "ASoC: no vendor load callback for ID %u\n", 245 le32_to_cpu(hdr->vendor_type)); 246 return -EINVAL; 247 } 248 249 if (ret < 0) 250 dev_err(tplg->dev, 251 "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %u:%u\n", 252 soc_tplg_get_hdr_offset(tplg), 253 soc_tplg_get_hdr_offset(tplg), 254 le32_to_cpu(hdr->type), 255 le32_to_cpu(hdr->vendor_type)); 256 return ret; 257 } 258 259 /* optionally pass new dynamic widget to component driver. This is mainly for 260 * external widgets where we can assign private data/ops */ 261 static int soc_tplg_widget_load(struct soc_tplg *tplg, 262 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) 263 { 264 if (tplg->ops && tplg->ops->widget_load) 265 return tplg->ops->widget_load(tplg->comp, tplg->index, w, 266 tplg_w); 267 268 return 0; 269 } 270 271 /* optionally pass new dynamic widget to component driver. This is mainly for 272 * external widgets where we can assign private data/ops */ 273 static int soc_tplg_widget_ready(struct soc_tplg *tplg, 274 struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) 275 { 276 if (tplg->ops && tplg->ops->widget_ready) 277 return tplg->ops->widget_ready(tplg->comp, tplg->index, w, 278 tplg_w); 279 280 return 0; 281 } 282 283 /* pass DAI configurations to component driver for extra initialization */ 284 static int soc_tplg_dai_load(struct soc_tplg *tplg, 285 struct snd_soc_dai_driver *dai_drv, 286 struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) 287 { 288 if (tplg->ops && tplg->ops->dai_load) 289 return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv, 290 pcm, dai); 291 292 return 0; 293 } 294 295 /* pass link configurations to component driver for extra initialization */ 296 static int soc_tplg_dai_link_load(struct soc_tplg *tplg, 297 struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg) 298 { 299 if (tplg->ops && tplg->ops->link_load) 300 return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg); 301 302 return 0; 303 } 304 305 /* tell the component driver that all firmware has been loaded in this request */ 306 static int soc_tplg_complete(struct soc_tplg *tplg) 307 { 308 if (tplg->ops && tplg->ops->complete) 309 return tplg->ops->complete(tplg->comp); 310 311 return 0; 312 } 313 314 /* add a dynamic kcontrol */ 315 static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev, 316 const struct snd_kcontrol_new *control_new, const char *prefix, 317 void *data, struct snd_kcontrol **kcontrol) 318 { 319 int err; 320 321 *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix); 322 if (*kcontrol == NULL) { 323 dev_err(dev, "ASoC: Failed to create new kcontrol %s\n", 324 control_new->name); 325 return -ENOMEM; 326 } 327 328 err = snd_ctl_add(card, *kcontrol); 329 if (err < 0) { 330 dev_err(dev, "ASoC: Failed to add %s: %d\n", 331 control_new->name, err); 332 return err; 333 } 334 335 return 0; 336 } 337 338 /* add a dynamic kcontrol for component driver */ 339 static int soc_tplg_add_kcontrol(struct soc_tplg *tplg, 340 struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol) 341 { 342 struct snd_soc_component *comp = tplg->comp; 343 344 return soc_tplg_add_dcontrol(comp->card->snd_card, 345 tplg->dev, k, comp->name_prefix, comp, kcontrol); 346 } 347 348 /* remove kcontrol */ 349 static void soc_tplg_remove_kcontrol(struct snd_soc_component *comp, struct snd_soc_dobj *dobj, 350 int pass) 351 { 352 struct snd_card *card = comp->card->snd_card; 353 354 if (pass != SOC_TPLG_PASS_CONTROL) 355 return; 356 357 if (dobj->unload) 358 dobj->unload(comp, dobj); 359 360 snd_ctl_remove(card, dobj->control.kcontrol); 361 list_del(&dobj->list); 362 } 363 364 /* remove a route */ 365 static void soc_tplg_remove_route(struct snd_soc_component *comp, 366 struct snd_soc_dobj *dobj, int pass) 367 { 368 if (pass != SOC_TPLG_PASS_GRAPH) 369 return; 370 371 if (dobj->unload) 372 dobj->unload(comp, dobj); 373 374 list_del(&dobj->list); 375 } 376 377 /* remove a widget and it's kcontrols - routes must be removed first */ 378 static void soc_tplg_remove_widget(struct snd_soc_component *comp, 379 struct snd_soc_dobj *dobj, int pass) 380 { 381 struct snd_card *card = comp->card->snd_card; 382 struct snd_soc_dapm_widget *w = 383 container_of(dobj, struct snd_soc_dapm_widget, dobj); 384 int i; 385 386 if (pass != SOC_TPLG_PASS_WIDGET) 387 return; 388 389 if (dobj->unload) 390 dobj->unload(comp, dobj); 391 392 if (w->kcontrols) 393 for (i = 0; i < w->num_kcontrols; i++) 394 snd_ctl_remove(card, w->kcontrols[i]); 395 396 list_del(&dobj->list); 397 398 /* widget w is freed by soc-dapm.c */ 399 } 400 401 /* remove DAI configurations */ 402 static void soc_tplg_remove_dai(struct snd_soc_component *comp, 403 struct snd_soc_dobj *dobj, int pass) 404 { 405 struct snd_soc_dai_driver *dai_drv = 406 container_of(dobj, struct snd_soc_dai_driver, dobj); 407 struct snd_soc_dai *dai, *_dai; 408 409 if (pass != SOC_TPLG_PASS_PCM_DAI) 410 return; 411 412 if (dobj->unload) 413 dobj->unload(comp, dobj); 414 415 for_each_component_dais_safe(comp, dai, _dai) 416 if (dai->driver == dai_drv) 417 snd_soc_unregister_dai(dai); 418 419 list_del(&dobj->list); 420 } 421 422 /* remove link configurations */ 423 static void soc_tplg_remove_link(struct snd_soc_component *comp, 424 struct snd_soc_dobj *dobj, int pass) 425 { 426 struct snd_soc_dai_link *link = 427 container_of(dobj, struct snd_soc_dai_link, dobj); 428 429 if (pass != SOC_TPLG_PASS_PCM_DAI) 430 return; 431 432 if (dobj->unload) 433 dobj->unload(comp, dobj); 434 435 list_del(&dobj->list); 436 437 /* Ignored links do not need to be removed, they are not added */ 438 if (!link->ignore) 439 snd_soc_remove_pcm_runtime(comp->card, 440 snd_soc_get_pcm_runtime(comp->card, link)); 441 } 442 443 /* unload dai link */ 444 static void remove_backend_link(struct snd_soc_component *comp, 445 struct snd_soc_dobj *dobj, int pass) 446 { 447 if (pass != SOC_TPLG_PASS_LINK) 448 return; 449 450 if (dobj->unload) 451 dobj->unload(comp, dobj); 452 453 /* 454 * We don't free the link here as what soc_tplg_remove_link() do since BE 455 * links are not allocated by topology. 456 * We however need to reset the dobj type to its initial values 457 */ 458 dobj->type = SND_SOC_DOBJ_NONE; 459 list_del(&dobj->list); 460 } 461 462 /* bind a kcontrol to it's IO handlers */ 463 static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr, 464 struct snd_kcontrol_new *k, 465 const struct soc_tplg *tplg) 466 { 467 const struct snd_soc_tplg_kcontrol_ops *ops; 468 const struct snd_soc_tplg_bytes_ext_ops *ext_ops; 469 int num_ops, i; 470 471 if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES 472 && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER 473 && (k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ 474 || k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) 475 && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 476 struct soc_bytes_ext *sbe; 477 struct snd_soc_tplg_bytes_control *be; 478 479 sbe = (struct soc_bytes_ext *)k->private_value; 480 be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); 481 482 /* TLV bytes controls need standard kcontrol info handler, 483 * TLV callback and extended put/get handlers. 484 */ 485 k->info = snd_soc_bytes_info_ext; 486 k->tlv.c = snd_soc_bytes_tlv_callback; 487 488 /* 489 * When a topology-based implementation abuses the 490 * control interface and uses bytes_ext controls of 491 * more than 512 bytes, we need to disable the size 492 * checks, otherwise accesses to such controls will 493 * return an -EINVAL error and prevent the card from 494 * being configured. 495 */ 496 if (sbe->max > 512) 497 k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK; 498 499 ext_ops = tplg->bytes_ext_ops; 500 num_ops = tplg->bytes_ext_ops_count; 501 for (i = 0; i < num_ops; i++) { 502 if (!sbe->put && 503 ext_ops[i].id == le32_to_cpu(be->ext_ops.put)) 504 sbe->put = ext_ops[i].put; 505 if (!sbe->get && 506 ext_ops[i].id == le32_to_cpu(be->ext_ops.get)) 507 sbe->get = ext_ops[i].get; 508 } 509 510 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get) 511 return -EINVAL; 512 if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put) 513 return -EINVAL; 514 return 0; 515 } 516 517 /* try and map vendor specific kcontrol handlers first */ 518 ops = tplg->io_ops; 519 num_ops = tplg->io_ops_count; 520 for (i = 0; i < num_ops; i++) { 521 522 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put)) 523 k->put = ops[i].put; 524 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get)) 525 k->get = ops[i].get; 526 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info)) 527 k->info = ops[i].info; 528 } 529 530 /* vendor specific handlers found ? */ 531 if (k->put && k->get && k->info) 532 return 0; 533 534 /* none found so try standard kcontrol handlers */ 535 ops = io_ops; 536 num_ops = ARRAY_SIZE(io_ops); 537 for (i = 0; i < num_ops; i++) { 538 539 if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put)) 540 k->put = ops[i].put; 541 if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get)) 542 k->get = ops[i].get; 543 if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info)) 544 k->info = ops[i].info; 545 } 546 547 /* standard handlers found ? */ 548 if (k->put && k->get && k->info) 549 return 0; 550 551 /* nothing to bind */ 552 return -EINVAL; 553 } 554 555 /* bind a widgets to it's evnt handlers */ 556 int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w, 557 const struct snd_soc_tplg_widget_events *events, 558 int num_events, u16 event_type) 559 { 560 int i; 561 562 w->event = NULL; 563 564 for (i = 0; i < num_events; i++) { 565 if (event_type == events[i].type) { 566 567 /* found - so assign event */ 568 w->event = events[i].event_handler; 569 return 0; 570 } 571 } 572 573 /* not found */ 574 return -EINVAL; 575 } 576 EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event); 577 578 /* optionally pass new dynamic kcontrol to component driver. */ 579 static int soc_tplg_control_load(struct soc_tplg *tplg, 580 struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr) 581 { 582 int ret = 0; 583 584 if (tplg->ops && tplg->ops->control_load) 585 ret = tplg->ops->control_load(tplg->comp, tplg->index, k, hdr); 586 587 if (ret) 588 dev_err(tplg->dev, "ASoC: failed to init %s\n", hdr->name); 589 590 return ret; 591 } 592 593 594 static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg, 595 struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale) 596 { 597 unsigned int item_len = 2 * sizeof(unsigned int); 598 unsigned int *p; 599 600 p = devm_kzalloc(tplg->dev, item_len + 2 * sizeof(unsigned int), GFP_KERNEL); 601 if (!p) 602 return -ENOMEM; 603 604 p[0] = SNDRV_CTL_TLVT_DB_SCALE; 605 p[1] = item_len; 606 p[2] = le32_to_cpu(scale->min); 607 p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK) 608 | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0); 609 610 kc->tlv.p = (void *)p; 611 return 0; 612 } 613 614 static int soc_tplg_create_tlv(struct soc_tplg *tplg, 615 struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc) 616 { 617 struct snd_soc_tplg_ctl_tlv *tplg_tlv; 618 u32 access = le32_to_cpu(tc->access); 619 620 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) 621 return 0; 622 623 if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) { 624 tplg_tlv = &tc->tlv; 625 switch (le32_to_cpu(tplg_tlv->type)) { 626 case SNDRV_CTL_TLVT_DB_SCALE: 627 return soc_tplg_create_tlv_db_scale(tplg, kc, 628 &tplg_tlv->scale); 629 630 /* TODO: add support for other TLV types */ 631 default: 632 dev_dbg(tplg->dev, "Unsupported TLV type %u\n", 633 le32_to_cpu(tplg_tlv->type)); 634 return -EINVAL; 635 } 636 } 637 638 return 0; 639 } 640 641 static int soc_tplg_control_dmixer_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 642 { 643 struct snd_soc_tplg_mixer_control *mc; 644 struct soc_mixer_control *sm; 645 int err; 646 647 mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; 648 649 /* validate kcontrol */ 650 if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 651 return -EINVAL; 652 653 sm = devm_kzalloc(tplg->dev, sizeof(*sm), GFP_KERNEL); 654 if (!sm) 655 return -ENOMEM; 656 657 tplg->pos += sizeof(struct snd_soc_tplg_mixer_control) + le32_to_cpu(mc->priv.size); 658 659 dev_dbg(tplg->dev, "ASoC: adding mixer kcontrol %s with access 0x%x\n", 660 mc->hdr.name, le32_to_cpu(mc->hdr.access)); 661 662 kc->name = devm_kstrdup(tplg->dev, mc->hdr.name, GFP_KERNEL); 663 if (!kc->name) 664 return -ENOMEM; 665 kc->private_value = (long)sm; 666 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 667 kc->access = le32_to_cpu(mc->hdr.access); 668 669 /* we only support FL/FR channel mapping atm */ 670 sm->reg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FL); 671 sm->rreg = tplg_chan_get_reg(tplg, mc->channel, SNDRV_CHMAP_FR); 672 sm->shift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FL); 673 sm->rshift = tplg_chan_get_shift(tplg, mc->channel, SNDRV_CHMAP_FR); 674 675 sm->max = le32_to_cpu(mc->max); 676 sm->min = le32_to_cpu(mc->min); 677 sm->invert = le32_to_cpu(mc->invert); 678 sm->platform_max = le32_to_cpu(mc->platform_max); 679 sm->num_channels = le32_to_cpu(mc->num_channels); 680 681 /* map io handlers */ 682 err = soc_tplg_kcontrol_bind_io(&mc->hdr, kc, tplg); 683 if (err) { 684 soc_control_err(tplg, &mc->hdr, mc->hdr.name); 685 return err; 686 } 687 688 /* create any TLV data */ 689 err = soc_tplg_create_tlv(tplg, kc, &mc->hdr); 690 if (err < 0) { 691 dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", mc->hdr.name); 692 return err; 693 } 694 695 /* pass control to driver for optional further init */ 696 return soc_tplg_control_load(tplg, kc, &mc->hdr); 697 } 698 699 static int soc_tplg_denum_create_texts(struct soc_tplg *tplg, struct soc_enum *se, 700 struct snd_soc_tplg_enum_control *ec) 701 { 702 int i, ret; 703 704 if (le32_to_cpu(ec->items) > ARRAY_SIZE(ec->texts)) 705 return -EINVAL; 706 707 se->dobj.control.dtexts = 708 devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL); 709 if (se->dobj.control.dtexts == NULL) 710 return -ENOMEM; 711 712 for (i = 0; i < le32_to_cpu(ec->items); i++) { 713 714 if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 715 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { 716 ret = -EINVAL; 717 goto err; 718 } 719 720 se->dobj.control.dtexts[i] = devm_kstrdup(tplg->dev, ec->texts[i], GFP_KERNEL); 721 if (!se->dobj.control.dtexts[i]) { 722 ret = -ENOMEM; 723 goto err; 724 } 725 } 726 727 se->items = le32_to_cpu(ec->items); 728 se->texts = (const char * const *)se->dobj.control.dtexts; 729 return 0; 730 731 err: 732 return ret; 733 } 734 735 static int soc_tplg_denum_create_values(struct soc_tplg *tplg, struct soc_enum *se, 736 struct snd_soc_tplg_enum_control *ec) 737 { 738 int i; 739 740 /* 741 * Following "if" checks if we have at most SND_SOC_TPLG_NUM_TEXTS 742 * values instead of using ARRAY_SIZE(ec->values) due to the fact that 743 * it is oversized for its purpose. Additionally it is done so because 744 * it is defined in UAPI header where it can't be easily changed. 745 */ 746 if (le32_to_cpu(ec->items) > SND_SOC_TPLG_NUM_TEXTS) 747 return -EINVAL; 748 749 se->dobj.control.dvalues = devm_kcalloc(tplg->dev, le32_to_cpu(ec->items), 750 sizeof(*se->dobj.control.dvalues), 751 GFP_KERNEL); 752 if (!se->dobj.control.dvalues) 753 return -ENOMEM; 754 755 /* convert from little-endian */ 756 for (i = 0; i < le32_to_cpu(ec->items); i++) { 757 se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]); 758 } 759 760 se->items = le32_to_cpu(ec->items); 761 se->values = (const unsigned int *)se->dobj.control.dvalues; 762 return 0; 763 } 764 765 static int soc_tplg_control_denum_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 766 { 767 struct snd_soc_tplg_enum_control *ec; 768 struct soc_enum *se; 769 int err; 770 771 ec = (struct snd_soc_tplg_enum_control *)tplg->pos; 772 773 /* validate kcontrol */ 774 if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 775 return -EINVAL; 776 777 se = devm_kzalloc(tplg->dev, sizeof(*se), GFP_KERNEL); 778 if (!se) 779 return -ENOMEM; 780 781 tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + le32_to_cpu(ec->priv.size)); 782 783 dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %u\n", ec->hdr.name, le32_to_cpu(ec->items)); 784 785 kc->name = devm_kstrdup(tplg->dev, ec->hdr.name, GFP_KERNEL); 786 if (!kc->name) 787 return -ENOMEM; 788 kc->private_value = (long)se; 789 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 790 kc->access = le32_to_cpu(ec->hdr.access); 791 792 /* we only support FL/FR channel mapping atm */ 793 se->reg = tplg_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); 794 se->shift_l = tplg_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL); 795 se->shift_r = tplg_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR); 796 797 se->mask = le32_to_cpu(ec->mask); 798 799 switch (le32_to_cpu(ec->hdr.ops.info)) { 800 case SND_SOC_TPLG_CTL_ENUM_VALUE: 801 case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: 802 err = soc_tplg_denum_create_values(tplg, se, ec); 803 if (err < 0) { 804 dev_err(tplg->dev, "ASoC: could not create values for %s\n", ec->hdr.name); 805 return err; 806 } 807 fallthrough; 808 case SND_SOC_TPLG_CTL_ENUM: 809 case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: 810 case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: 811 err = soc_tplg_denum_create_texts(tplg, se, ec); 812 if (err < 0) { 813 dev_err(tplg->dev, "ASoC: could not create texts for %s\n", ec->hdr.name); 814 return err; 815 } 816 break; 817 default: 818 dev_err(tplg->dev, "ASoC: invalid enum control type %u for %s\n", 819 le32_to_cpu(ec->hdr.ops.info), ec->hdr.name); 820 return -EINVAL; 821 } 822 823 /* map io handlers */ 824 err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg); 825 if (err) { 826 soc_control_err(tplg, &ec->hdr, ec->hdr.name); 827 return err; 828 } 829 830 /* pass control to driver for optional further init */ 831 return soc_tplg_control_load(tplg, kc, &ec->hdr); 832 } 833 834 static int soc_tplg_control_dbytes_create(struct soc_tplg *tplg, struct snd_kcontrol_new *kc) 835 { 836 struct snd_soc_tplg_bytes_control *be; 837 struct soc_bytes_ext *sbe; 838 int err; 839 840 be = (struct snd_soc_tplg_bytes_control *)tplg->pos; 841 842 /* validate kcontrol */ 843 if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 844 return -EINVAL; 845 846 sbe = devm_kzalloc(tplg->dev, sizeof(*sbe), GFP_KERNEL); 847 if (!sbe) 848 return -ENOMEM; 849 850 tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + le32_to_cpu(be->priv.size)); 851 852 dev_dbg(tplg->dev, "ASoC: adding bytes kcontrol %s with access 0x%x\n", 853 be->hdr.name, le32_to_cpu(be->hdr.access)); 854 855 kc->name = devm_kstrdup(tplg->dev, be->hdr.name, GFP_KERNEL); 856 if (!kc->name) 857 return -ENOMEM; 858 kc->private_value = (long)sbe; 859 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; 860 kc->access = le32_to_cpu(be->hdr.access); 861 862 sbe->max = le32_to_cpu(be->max); 863 864 /* map standard io handlers and check for external handlers */ 865 err = soc_tplg_kcontrol_bind_io(&be->hdr, kc, tplg); 866 if (err) { 867 soc_control_err(tplg, &be->hdr, be->hdr.name); 868 return err; 869 } 870 871 /* pass control to driver for optional further init */ 872 return soc_tplg_control_load(tplg, kc, &be->hdr); 873 } 874 875 static int soc_tplg_dbytes_create(struct soc_tplg *tplg, size_t size) 876 { 877 struct snd_kcontrol_new kc = {0}; 878 struct soc_bytes_ext *sbe; 879 int ret; 880 881 if (soc_tplg_check_elem_count(tplg, 882 sizeof(struct snd_soc_tplg_bytes_control), 883 1, size, "mixer bytes")) 884 return -EINVAL; 885 886 ret = soc_tplg_control_dbytes_create(tplg, &kc); 887 if (ret) 888 return ret; 889 890 /* register dynamic object */ 891 sbe = (struct soc_bytes_ext *)kc.private_value; 892 893 INIT_LIST_HEAD(&sbe->dobj.list); 894 sbe->dobj.type = SND_SOC_DOBJ_BYTES; 895 sbe->dobj.index = tplg->index; 896 if (tplg->ops) 897 sbe->dobj.unload = tplg->ops->control_unload; 898 899 /* create control directly */ 900 ret = soc_tplg_add_kcontrol(tplg, &kc, &sbe->dobj.control.kcontrol); 901 if (ret < 0) 902 return ret; 903 904 list_add(&sbe->dobj.list, &tplg->comp->dobj_list); 905 906 return ret; 907 } 908 909 static int soc_tplg_dmixer_create(struct soc_tplg *tplg, size_t size) 910 { 911 struct snd_kcontrol_new kc = {0}; 912 struct soc_mixer_control *sm; 913 int ret; 914 915 if (soc_tplg_check_elem_count(tplg, 916 sizeof(struct snd_soc_tplg_mixer_control), 917 1, size, "mixers")) 918 return -EINVAL; 919 920 ret = soc_tplg_control_dmixer_create(tplg, &kc); 921 if (ret) 922 return ret; 923 924 /* register dynamic object */ 925 sm = (struct soc_mixer_control *)kc.private_value; 926 927 INIT_LIST_HEAD(&sm->dobj.list); 928 sm->dobj.type = SND_SOC_DOBJ_MIXER; 929 sm->dobj.index = tplg->index; 930 if (tplg->ops) 931 sm->dobj.unload = tplg->ops->control_unload; 932 933 /* create control directly */ 934 ret = soc_tplg_add_kcontrol(tplg, &kc, &sm->dobj.control.kcontrol); 935 if (ret < 0) 936 return ret; 937 938 list_add(&sm->dobj.list, &tplg->comp->dobj_list); 939 940 return ret; 941 } 942 943 static int soc_tplg_denum_create(struct soc_tplg *tplg, size_t size) 944 { 945 struct snd_kcontrol_new kc = {0}; 946 struct soc_enum *se; 947 int ret; 948 949 if (soc_tplg_check_elem_count(tplg, 950 sizeof(struct snd_soc_tplg_enum_control), 951 1, size, "enums")) 952 return -EINVAL; 953 954 ret = soc_tplg_control_denum_create(tplg, &kc); 955 if (ret) 956 return ret; 957 958 /* register dynamic object */ 959 se = (struct soc_enum *)kc.private_value; 960 961 INIT_LIST_HEAD(&se->dobj.list); 962 se->dobj.type = SND_SOC_DOBJ_ENUM; 963 se->dobj.index = tplg->index; 964 if (tplg->ops) 965 se->dobj.unload = tplg->ops->control_unload; 966 967 /* create control directly */ 968 ret = soc_tplg_add_kcontrol(tplg, &kc, &se->dobj.control.kcontrol); 969 if (ret < 0) 970 return ret; 971 972 list_add(&se->dobj.list, &tplg->comp->dobj_list); 973 974 return ret; 975 } 976 977 static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg, 978 struct snd_soc_tplg_hdr *hdr) 979 { 980 int ret; 981 int i; 982 983 dev_dbg(tplg->dev, "ASoC: adding %u kcontrols at 0x%lx\n", le32_to_cpu(hdr->count), 984 soc_tplg_get_offset(tplg)); 985 986 for (i = 0; i < le32_to_cpu(hdr->count); i++) { 987 struct snd_soc_tplg_ctl_hdr *control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; 988 989 if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) { 990 dev_err(tplg->dev, "ASoC: invalid control size\n"); 991 return -EINVAL; 992 } 993 994 switch (le32_to_cpu(control_hdr->type)) { 995 case SND_SOC_TPLG_TYPE_MIXER: 996 ret = soc_tplg_dmixer_create(tplg, le32_to_cpu(hdr->payload_size)); 997 break; 998 case SND_SOC_TPLG_TYPE_ENUM: 999 ret = soc_tplg_denum_create(tplg, le32_to_cpu(hdr->payload_size)); 1000 break; 1001 case SND_SOC_TPLG_TYPE_BYTES: 1002 ret = soc_tplg_dbytes_create(tplg, le32_to_cpu(hdr->payload_size)); 1003 break; 1004 default: 1005 ret = -EINVAL; 1006 break; 1007 } 1008 1009 if (ret < 0) { 1010 dev_err(tplg->dev, "ASoC: invalid control type: %u, index: %d at 0x%lx\n", 1011 le32_to_cpu(control_hdr->type), i, soc_tplg_get_offset(tplg)); 1012 return ret; 1013 } 1014 } 1015 1016 return 0; 1017 } 1018 1019 /* optionally pass new dynamic kcontrol to component driver. */ 1020 static int soc_tplg_add_route(struct soc_tplg *tplg, 1021 struct snd_soc_dapm_route *route) 1022 { 1023 if (tplg->ops && tplg->ops->dapm_route_load) 1024 return tplg->ops->dapm_route_load(tplg->comp, tplg->index, 1025 route); 1026 1027 return 0; 1028 } 1029 1030 static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, 1031 struct snd_soc_tplg_hdr *hdr) 1032 { 1033 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(tplg->comp); 1034 const size_t maxlen = SNDRV_CTL_ELEM_ID_NAME_MAXLEN; 1035 struct snd_soc_tplg_dapm_graph_elem *elem; 1036 struct snd_soc_dapm_route *route; 1037 int count, i; 1038 int ret = 0; 1039 1040 count = le32_to_cpu(hdr->count); 1041 1042 if (soc_tplg_check_elem_count(tplg, 1043 sizeof(struct snd_soc_tplg_dapm_graph_elem), 1044 count, le32_to_cpu(hdr->payload_size), "graph")) 1045 return -EINVAL; 1046 1047 dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %u\n", count, 1048 le32_to_cpu(hdr->index)); 1049 1050 for (i = 0; i < count; i++) { 1051 route = devm_kzalloc(tplg->dev, sizeof(*route), GFP_KERNEL); 1052 if (!route) 1053 return -ENOMEM; 1054 elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; 1055 tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); 1056 1057 /* validate routes */ 1058 if ((strnlen(elem->source, maxlen) == maxlen) || 1059 (strnlen(elem->sink, maxlen) == maxlen) || 1060 (strnlen(elem->control, maxlen) == maxlen)) { 1061 ret = -EINVAL; 1062 break; 1063 } 1064 1065 route->source = devm_kstrdup(tplg->dev, elem->source, GFP_KERNEL); 1066 route->sink = devm_kstrdup(tplg->dev, elem->sink, GFP_KERNEL); 1067 if (!route->source || !route->sink) { 1068 ret = -ENOMEM; 1069 break; 1070 } 1071 1072 if (strnlen(elem->control, maxlen) != 0) { 1073 route->control = devm_kstrdup(tplg->dev, elem->control, GFP_KERNEL); 1074 if (!route->control) { 1075 ret = -ENOMEM; 1076 break; 1077 } 1078 } 1079 1080 /* add route dobj to dobj_list */ 1081 route->dobj.type = SND_SOC_DOBJ_GRAPH; 1082 if (tplg->ops) 1083 route->dobj.unload = tplg->ops->dapm_route_unload; 1084 route->dobj.index = tplg->index; 1085 list_add(&route->dobj.list, &tplg->comp->dobj_list); 1086 1087 ret = soc_tplg_add_route(tplg, route); 1088 if (ret < 0) { 1089 dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret); 1090 break; 1091 } 1092 1093 ret = snd_soc_dapm_add_routes(dapm, route, 1); 1094 if (ret) 1095 break; 1096 } 1097 1098 return ret; 1099 } 1100 1101 static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, 1102 struct snd_soc_tplg_dapm_widget *w) 1103 { 1104 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(tplg->comp); 1105 struct snd_soc_dapm_widget template, *widget; 1106 struct snd_soc_tplg_ctl_hdr *control_hdr; 1107 struct snd_soc_card *card = tplg->comp->card; 1108 unsigned int *kcontrol_type = NULL; 1109 struct snd_kcontrol_new *kc; 1110 int mixer_count = 0; 1111 int bytes_count = 0; 1112 int enum_count = 0; 1113 int ret = 0; 1114 int i; 1115 1116 if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 1117 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1118 return -EINVAL; 1119 if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 1120 SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1121 return -EINVAL; 1122 1123 dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %u\n", 1124 w->name, le32_to_cpu(w->id)); 1125 1126 memset(&template, 0, sizeof(template)); 1127 1128 /* map user to kernel widget ID */ 1129 template.id = get_widget_id(le32_to_cpu(w->id)); 1130 if ((int)template.id < 0) 1131 return template.id; 1132 1133 /* strings are allocated here, but used and freed by the widget */ 1134 template.name = kstrdup(w->name, GFP_KERNEL); 1135 if (!template.name) 1136 return -ENOMEM; 1137 template.sname = kstrdup(w->sname, GFP_KERNEL); 1138 if (!template.sname) { 1139 ret = -ENOMEM; 1140 goto err; 1141 } 1142 template.reg = le32_to_cpu(w->reg); 1143 template.shift = le32_to_cpu(w->shift); 1144 template.mask = le32_to_cpu(w->mask); 1145 template.subseq = le32_to_cpu(w->subseq); 1146 template.on_val = w->invert ? 0 : 1; 1147 template.off_val = w->invert ? 1 : 0; 1148 template.ignore_suspend = le32_to_cpu(w->ignore_suspend); 1149 template.event_flags = le16_to_cpu(w->event_flags); 1150 template.dobj.index = tplg->index; 1151 1152 tplg->pos += 1153 (sizeof(struct snd_soc_tplg_dapm_widget) + 1154 le32_to_cpu(w->priv.size)); 1155 1156 if (w->num_kcontrols == 0) { 1157 template.num_kcontrols = 0; 1158 goto widget; 1159 } 1160 1161 template.num_kcontrols = le32_to_cpu(w->num_kcontrols); 1162 kc = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(*kc), GFP_KERNEL); 1163 if (!kc) { 1164 ret = -ENOMEM; 1165 goto hdr_err; 1166 } 1167 1168 kcontrol_type = devm_kcalloc(tplg->dev, le32_to_cpu(w->num_kcontrols), sizeof(unsigned int), 1169 GFP_KERNEL); 1170 if (!kcontrol_type) { 1171 ret = -ENOMEM; 1172 goto hdr_err; 1173 } 1174 1175 for (i = 0; i < le32_to_cpu(w->num_kcontrols); i++) { 1176 control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; 1177 1178 switch (le32_to_cpu(control_hdr->type)) { 1179 case SND_SOC_TPLG_TYPE_MIXER: 1180 /* volume mixer */ 1181 kc[i].index = mixer_count; 1182 kcontrol_type[i] = SND_SOC_TPLG_TYPE_MIXER; 1183 mixer_count++; 1184 ret = soc_tplg_control_dmixer_create(tplg, &kc[i]); 1185 if (ret < 0) 1186 goto hdr_err; 1187 break; 1188 case SND_SOC_TPLG_TYPE_ENUM: 1189 /* enumerated mixer */ 1190 kc[i].index = enum_count; 1191 kcontrol_type[i] = SND_SOC_TPLG_TYPE_ENUM; 1192 enum_count++; 1193 ret = soc_tplg_control_denum_create(tplg, &kc[i]); 1194 if (ret < 0) 1195 goto hdr_err; 1196 break; 1197 case SND_SOC_TPLG_TYPE_BYTES: 1198 /* bytes control */ 1199 kc[i].index = bytes_count; 1200 kcontrol_type[i] = SND_SOC_TPLG_TYPE_BYTES; 1201 bytes_count++; 1202 ret = soc_tplg_control_dbytes_create(tplg, &kc[i]); 1203 if (ret < 0) 1204 goto hdr_err; 1205 break; 1206 default: 1207 dev_err(tplg->dev, "ASoC: invalid widget control type %u:%u:%u\n", 1208 le32_to_cpu(control_hdr->ops.get), 1209 le32_to_cpu(control_hdr->ops.put), 1210 le32_to_cpu(control_hdr->ops.info)); 1211 ret = -EINVAL; 1212 goto hdr_err; 1213 } 1214 } 1215 1216 template.kcontrol_news = kc; 1217 dev_dbg(tplg->dev, "ASoC: template %s with %d/%d/%d (mixer/enum/bytes) control\n", 1218 w->name, mixer_count, enum_count, bytes_count); 1219 1220 widget: 1221 ret = soc_tplg_widget_load(tplg, &template, w); 1222 if (ret < 0) 1223 goto hdr_err; 1224 1225 /* card dapm mutex is held by the core if we are loading topology 1226 * data during sound card init. */ 1227 if (snd_soc_card_is_instantiated(card)) 1228 widget = snd_soc_dapm_new_control(dapm, &template); 1229 else 1230 widget = snd_soc_dapm_new_control_unlocked(dapm, &template); 1231 if (IS_ERR(widget)) { 1232 ret = PTR_ERR(widget); 1233 goto hdr_err; 1234 } 1235 1236 widget->dobj.type = SND_SOC_DOBJ_WIDGET; 1237 widget->dobj.widget.kcontrol_type = kcontrol_type; 1238 if (tplg->ops) 1239 widget->dobj.unload = tplg->ops->widget_unload; 1240 widget->dobj.index = tplg->index; 1241 list_add(&widget->dobj.list, &tplg->comp->dobj_list); 1242 1243 ret = soc_tplg_widget_ready(tplg, widget, w); 1244 if (ret < 0) 1245 goto ready_err; 1246 1247 kfree(template.sname); 1248 kfree(template.name); 1249 1250 return 0; 1251 1252 ready_err: 1253 soc_tplg_remove_widget(snd_soc_dapm_to_component(widget->dapm), 1254 &widget->dobj, SOC_TPLG_PASS_WIDGET); 1255 snd_soc_dapm_free_widget(widget); 1256 hdr_err: 1257 kfree(template.sname); 1258 err: 1259 kfree(template.name); 1260 return ret; 1261 } 1262 1263 static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, 1264 struct snd_soc_tplg_hdr *hdr) 1265 { 1266 int count, i; 1267 1268 count = le32_to_cpu(hdr->count); 1269 1270 dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); 1271 1272 for (i = 0; i < count; i++) { 1273 struct snd_soc_tplg_dapm_widget *widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; 1274 int ret; 1275 1276 /* 1277 * check if widget itself fits within topology file 1278 * use sizeof instead of widget->size, as we can't be sure 1279 * it is set properly yet (file may end before it is present) 1280 */ 1281 if (soc_tplg_get_offset(tplg) + sizeof(*widget) >= tplg->fw->size) { 1282 dev_err(tplg->dev, "ASoC: invalid widget data size\n"); 1283 return -EINVAL; 1284 } 1285 1286 /* check if widget has proper size */ 1287 if (le32_to_cpu(widget->size) != sizeof(*widget)) { 1288 dev_err(tplg->dev, "ASoC: invalid widget size\n"); 1289 return -EINVAL; 1290 } 1291 1292 /* check if widget private data fits within topology file */ 1293 if (soc_tplg_get_offset(tplg) + le32_to_cpu(widget->priv.size) >= tplg->fw->size) { 1294 dev_err(tplg->dev, "ASoC: invalid widget private data size\n"); 1295 return -EINVAL; 1296 } 1297 1298 ret = soc_tplg_dapm_widget_create(tplg, widget); 1299 if (ret < 0) { 1300 dev_err(tplg->dev, "ASoC: failed to load widget %s\n", 1301 widget->name); 1302 return ret; 1303 } 1304 } 1305 1306 return 0; 1307 } 1308 1309 static int soc_tplg_dapm_complete(struct soc_tplg *tplg) 1310 { 1311 struct snd_soc_card *card = tplg->comp->card; 1312 int ret; 1313 1314 /* Card might not have been registered at this point. 1315 * If so, just return success. 1316 */ 1317 if (!snd_soc_card_is_instantiated(card)) { 1318 dev_warn(tplg->dev, "ASoC: Parent card not yet available, widget card binding deferred\n"); 1319 return 0; 1320 } 1321 1322 ret = snd_soc_dapm_new_widgets(card); 1323 if (ret < 0) 1324 dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", ret); 1325 1326 return ret; 1327 } 1328 1329 static int set_stream_info(struct soc_tplg *tplg, struct snd_soc_pcm_stream *stream, 1330 struct snd_soc_tplg_stream_caps *caps) 1331 { 1332 stream->stream_name = devm_kstrdup(tplg->dev, caps->name, GFP_KERNEL); 1333 if (!stream->stream_name) 1334 return -ENOMEM; 1335 1336 stream->channels_min = le32_to_cpu(caps->channels_min); 1337 stream->channels_max = le32_to_cpu(caps->channels_max); 1338 stream->rates = le32_to_cpu(caps->rates); 1339 stream->rate_min = le32_to_cpu(caps->rate_min); 1340 stream->rate_max = le32_to_cpu(caps->rate_max); 1341 stream->formats = le64_to_cpu(caps->formats); 1342 stream->sig_bits = le32_to_cpu(caps->sig_bits); 1343 1344 return 0; 1345 } 1346 1347 static void set_dai_flags(struct snd_soc_dai_driver *dai_drv, 1348 unsigned int flag_mask, unsigned int flags) 1349 { 1350 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) 1351 dai_drv->symmetric_rate = 1352 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) ? 1 : 0; 1353 1354 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) 1355 dai_drv->symmetric_channels = 1356 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) ? 1357 1 : 0; 1358 1359 if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) 1360 dai_drv->symmetric_sample_bits = 1361 (flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) ? 1362 1 : 0; 1363 } 1364 1365 static const struct snd_soc_dai_ops tplg_dai_ops = { 1366 .compress_new = snd_soc_new_compress, 1367 }; 1368 1369 static int soc_tplg_dai_create(struct soc_tplg *tplg, 1370 struct snd_soc_tplg_pcm *pcm) 1371 { 1372 struct snd_soc_dai_driver *dai_drv; 1373 struct snd_soc_pcm_stream *stream; 1374 struct snd_soc_tplg_stream_caps *caps; 1375 struct snd_soc_dai *dai; 1376 struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(tplg->comp); 1377 int ret; 1378 1379 dai_drv = devm_kzalloc(tplg->dev, sizeof(struct snd_soc_dai_driver), GFP_KERNEL); 1380 if (dai_drv == NULL) 1381 return -ENOMEM; 1382 1383 if (strlen(pcm->dai_name)) { 1384 dai_drv->name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL); 1385 if (!dai_drv->name) { 1386 ret = -ENOMEM; 1387 goto err; 1388 } 1389 } 1390 dai_drv->id = le32_to_cpu(pcm->dai_id); 1391 1392 if (pcm->playback) { 1393 stream = &dai_drv->playback; 1394 caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; 1395 ret = set_stream_info(tplg, stream, caps); 1396 if (ret < 0) 1397 goto err; 1398 } 1399 1400 if (pcm->capture) { 1401 stream = &dai_drv->capture; 1402 caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; 1403 ret = set_stream_info(tplg, stream, caps); 1404 if (ret < 0) 1405 goto err; 1406 } 1407 1408 if (pcm->compress) 1409 dai_drv->ops = &tplg_dai_ops; 1410 1411 /* pass control to component driver for optional further init */ 1412 ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL); 1413 if (ret < 0) { 1414 dev_err(tplg->dev, "ASoC: DAI loading failed\n"); 1415 goto err; 1416 } 1417 1418 dai_drv->dobj.index = tplg->index; 1419 dai_drv->dobj.type = SND_SOC_DOBJ_PCM; 1420 if (tplg->ops) 1421 dai_drv->dobj.unload = tplg->ops->dai_unload; 1422 list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); 1423 1424 /* register the DAI to the component */ 1425 dai = snd_soc_register_dai(tplg->comp, dai_drv, false); 1426 if (!dai) 1427 return -ENOMEM; 1428 1429 /* Create the DAI widgets here */ 1430 ret = snd_soc_dapm_new_dai_widgets(dapm, dai); 1431 if (ret != 0) { 1432 dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret); 1433 snd_soc_unregister_dai(dai); 1434 return ret; 1435 } 1436 1437 return 0; 1438 1439 err: 1440 return ret; 1441 } 1442 1443 static void set_link_flags(struct snd_soc_dai_link *link, 1444 unsigned int flag_mask, unsigned int flags) 1445 { 1446 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) 1447 link->symmetric_rate = 1448 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) ? 1 : 0; 1449 1450 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) 1451 link->symmetric_channels = 1452 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) ? 1453 1 : 0; 1454 1455 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) 1456 link->symmetric_sample_bits = 1457 (flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) ? 1458 1 : 0; 1459 1460 if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) 1461 link->ignore_suspend = 1462 (flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) ? 1463 1 : 0; 1464 } 1465 1466 /* create the FE DAI link */ 1467 static int soc_tplg_fe_link_create(struct soc_tplg *tplg, 1468 struct snd_soc_tplg_pcm *pcm) 1469 { 1470 struct snd_soc_dai_link *link; 1471 struct snd_soc_dai_link_component *dlc; 1472 int ret; 1473 1474 /* link + cpu + codec + platform */ 1475 link = devm_kzalloc(tplg->dev, sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL); 1476 if (link == NULL) 1477 return -ENOMEM; 1478 1479 dlc = (struct snd_soc_dai_link_component *)(link + 1); 1480 1481 link->cpus = &dlc[0]; 1482 link->num_cpus = 1; 1483 1484 link->dobj.index = tplg->index; 1485 link->dobj.type = SND_SOC_DOBJ_DAI_LINK; 1486 if (tplg->ops) 1487 link->dobj.unload = tplg->ops->link_unload; 1488 1489 if (strlen(pcm->pcm_name)) { 1490 link->name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL); 1491 link->stream_name = devm_kstrdup(tplg->dev, pcm->pcm_name, GFP_KERNEL); 1492 if (!link->name || !link->stream_name) { 1493 ret = -ENOMEM; 1494 goto err; 1495 } 1496 } 1497 link->id = le32_to_cpu(pcm->pcm_id); 1498 1499 if (strlen(pcm->dai_name)) { 1500 link->cpus->dai_name = devm_kstrdup(tplg->dev, pcm->dai_name, GFP_KERNEL); 1501 if (!link->cpus->dai_name) { 1502 ret = -ENOMEM; 1503 goto err; 1504 } 1505 } 1506 1507 /* 1508 * Many topology are assuming link has Codec / Platform, and 1509 * these might be overwritten at soc_tplg_dai_link_load(). 1510 * Don't use &snd_soc_dummy_dlc here. 1511 */ 1512 link->codecs = &dlc[1]; /* Don't use &snd_soc_dummy_dlc here */ 1513 link->codecs->name = "snd-soc-dummy"; 1514 link->codecs->dai_name = "snd-soc-dummy-dai"; 1515 link->num_codecs = 1; 1516 1517 link->platforms = &dlc[2]; /* Don't use &snd_soc_dummy_dlc here */ 1518 link->platforms->name = "snd-soc-dummy"; 1519 link->num_platforms = 1; 1520 1521 /* enable DPCM */ 1522 link->dynamic = 1; 1523 link->ignore_pmdown_time = 1; 1524 link->playback_only = le32_to_cpu(pcm->playback) && !le32_to_cpu(pcm->capture); 1525 link->capture_only = !le32_to_cpu(pcm->playback) && le32_to_cpu(pcm->capture); 1526 if (pcm->flag_mask) 1527 set_link_flags(link, 1528 le32_to_cpu(pcm->flag_mask), 1529 le32_to_cpu(pcm->flags)); 1530 1531 /* pass control to component driver for optional further init */ 1532 ret = soc_tplg_dai_link_load(tplg, link, NULL); 1533 if (ret < 0) { 1534 dev_err(tplg->dev, "ASoC: FE link loading failed\n"); 1535 goto err; 1536 } 1537 1538 ret = snd_soc_add_pcm_runtimes(tplg->comp->card, link, 1); 1539 if (ret < 0) { 1540 if (ret != -EPROBE_DEFER) 1541 dev_err(tplg->dev, "ASoC: adding FE link failed\n"); 1542 goto err; 1543 } 1544 1545 list_add(&link->dobj.list, &tplg->comp->dobj_list); 1546 1547 return 0; 1548 err: 1549 return ret; 1550 } 1551 1552 /* create a FE DAI and DAI link from the PCM object */ 1553 static int soc_tplg_pcm_create(struct soc_tplg *tplg, 1554 struct snd_soc_tplg_pcm *pcm) 1555 { 1556 int ret; 1557 1558 ret = soc_tplg_dai_create(tplg, pcm); 1559 if (ret < 0) 1560 return ret; 1561 1562 return soc_tplg_fe_link_create(tplg, pcm); 1563 } 1564 1565 static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, 1566 struct snd_soc_tplg_hdr *hdr) 1567 { 1568 struct snd_soc_tplg_pcm *pcm; 1569 int count; 1570 int size; 1571 int i; 1572 int ret; 1573 1574 count = le32_to_cpu(hdr->count); 1575 1576 /* check the element size and count */ 1577 pcm = (struct snd_soc_tplg_pcm *)tplg->pos; 1578 size = le32_to_cpu(pcm->size); 1579 if (size > sizeof(struct snd_soc_tplg_pcm)) { 1580 dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n", 1581 size); 1582 return -EINVAL; 1583 } 1584 1585 if (soc_tplg_check_elem_count(tplg, 1586 size, count, 1587 le32_to_cpu(hdr->payload_size), 1588 "PCM DAI")) 1589 return -EINVAL; 1590 1591 for (i = 0; i < count; i++) { 1592 pcm = (struct snd_soc_tplg_pcm *)tplg->pos; 1593 size = le32_to_cpu(pcm->size); 1594 1595 /* check ABI version by size, create a new version of pcm 1596 * if abi not match. 1597 */ 1598 if (size != sizeof(*pcm)) 1599 return -EINVAL; 1600 1601 /* create the FE DAIs and DAI links */ 1602 ret = soc_tplg_pcm_create(tplg, pcm); 1603 if (ret < 0) 1604 return ret; 1605 1606 /* offset by version-specific struct size and 1607 * real priv data size 1608 */ 1609 tplg->pos += size + le32_to_cpu(pcm->priv.size); 1610 } 1611 1612 dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); 1613 1614 return 0; 1615 } 1616 1617 /** 1618 * set_link_hw_format - Set the HW audio format of the physical DAI link. 1619 * @link: &snd_soc_dai_link which should be updated 1620 * @cfg: physical link configs. 1621 * 1622 * Topology context contains a list of supported HW formats (configs) and 1623 * a default format ID for the physical link. This function will use this 1624 * default ID to choose the HW format to set the link's DAI format for init. 1625 */ 1626 static void set_link_hw_format(struct snd_soc_dai_link *link, 1627 struct snd_soc_tplg_link_config *cfg) 1628 { 1629 struct snd_soc_tplg_hw_config *hw_config; 1630 unsigned char bclk_provider, fsync_provider; 1631 unsigned char invert_bclk, invert_fsync; 1632 int i; 1633 1634 for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) { 1635 hw_config = &cfg->hw_config[i]; 1636 if (hw_config->id != cfg->default_hw_config_id) 1637 continue; 1638 1639 link->dai_fmt = le32_to_cpu(hw_config->fmt) & 1640 SND_SOC_DAIFMT_FORMAT_MASK; 1641 1642 /* clock gating */ 1643 switch (hw_config->clock_gated) { 1644 case SND_SOC_TPLG_DAI_CLK_GATE_GATED: 1645 link->dai_fmt |= SND_SOC_DAIFMT_GATED; 1646 break; 1647 1648 case SND_SOC_TPLG_DAI_CLK_GATE_CONT: 1649 link->dai_fmt |= SND_SOC_DAIFMT_CONT; 1650 break; 1651 1652 default: 1653 /* ignore the value */ 1654 break; 1655 } 1656 1657 /* clock signal polarity */ 1658 invert_bclk = hw_config->invert_bclk; 1659 invert_fsync = hw_config->invert_fsync; 1660 if (!invert_bclk && !invert_fsync) 1661 link->dai_fmt |= SND_SOC_DAIFMT_NB_NF; 1662 else if (!invert_bclk && invert_fsync) 1663 link->dai_fmt |= SND_SOC_DAIFMT_NB_IF; 1664 else if (invert_bclk && !invert_fsync) 1665 link->dai_fmt |= SND_SOC_DAIFMT_IB_NF; 1666 else 1667 link->dai_fmt |= SND_SOC_DAIFMT_IB_IF; 1668 1669 /* clock masters */ 1670 bclk_provider = (hw_config->bclk_provider == 1671 SND_SOC_TPLG_BCLK_CP); 1672 fsync_provider = (hw_config->fsync_provider == 1673 SND_SOC_TPLG_FSYNC_CP); 1674 if (bclk_provider && fsync_provider) 1675 link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP; 1676 else if (!bclk_provider && fsync_provider) 1677 link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFP; 1678 else if (bclk_provider && !fsync_provider) 1679 link->dai_fmt |= SND_SOC_DAIFMT_CBP_CFC; 1680 else 1681 link->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC; 1682 } 1683 } 1684 1685 /** 1686 * snd_soc_find_dai_link - Find a DAI link 1687 * 1688 * @card: soc card 1689 * @id: DAI link ID to match 1690 * @name: DAI link name to match, optional 1691 * @stream_name: DAI link stream name to match, optional 1692 * 1693 * This function will search all existing DAI links of the soc card to 1694 * find the link of the same ID. Since DAI links may not have their 1695 * unique ID, so name and stream name should also match if being 1696 * specified. 1697 * 1698 * Return: pointer of DAI link, or NULL if not found. 1699 */ 1700 static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card, 1701 int id, const char *name, 1702 const char *stream_name) 1703 { 1704 struct snd_soc_pcm_runtime *rtd; 1705 1706 for_each_card_rtds(card, rtd) { 1707 struct snd_soc_dai_link *link = rtd->dai_link; 1708 1709 if (link->id != id) 1710 continue; 1711 1712 if (name && (!link->name || !strstr(link->name, name))) 1713 continue; 1714 1715 if (stream_name && (!link->stream_name || 1716 !strstr(link->stream_name, stream_name))) 1717 continue; 1718 1719 return link; 1720 } 1721 1722 return NULL; 1723 } 1724 1725 /* Find and configure an existing physical DAI link */ 1726 static int soc_tplg_link_config(struct soc_tplg *tplg, 1727 struct snd_soc_tplg_link_config *cfg) 1728 { 1729 struct snd_soc_dai_link *link; 1730 const char *name, *stream_name; 1731 size_t len; 1732 int ret; 1733 1734 len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 1735 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1736 return -EINVAL; 1737 else if (len) 1738 name = cfg->name; 1739 else 1740 name = NULL; 1741 1742 len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); 1743 if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) 1744 return -EINVAL; 1745 else if (len) 1746 stream_name = cfg->stream_name; 1747 else 1748 stream_name = NULL; 1749 1750 link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id), 1751 name, stream_name); 1752 if (!link) { 1753 dev_err(tplg->dev, "ASoC: physical link %s (id %u) not exist\n", 1754 name, le32_to_cpu(cfg->id)); 1755 return -EINVAL; 1756 } 1757 1758 /* hw format */ 1759 if (cfg->num_hw_configs) 1760 set_link_hw_format(link, cfg); 1761 1762 /* flags */ 1763 if (cfg->flag_mask) 1764 set_link_flags(link, 1765 le32_to_cpu(cfg->flag_mask), 1766 le32_to_cpu(cfg->flags)); 1767 1768 /* pass control to component driver for optional further init */ 1769 ret = soc_tplg_dai_link_load(tplg, link, cfg); 1770 if (ret < 0) { 1771 dev_err(tplg->dev, "ASoC: physical link loading failed\n"); 1772 return ret; 1773 } 1774 1775 /* for unloading it in snd_soc_tplg_component_remove */ 1776 link->dobj.index = tplg->index; 1777 link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK; 1778 if (tplg->ops) 1779 link->dobj.unload = tplg->ops->link_unload; 1780 list_add(&link->dobj.list, &tplg->comp->dobj_list); 1781 1782 return 0; 1783 } 1784 1785 1786 /* Load physical link config elements from the topology context */ 1787 static int soc_tplg_link_elems_load(struct soc_tplg *tplg, 1788 struct snd_soc_tplg_hdr *hdr) 1789 { 1790 struct snd_soc_tplg_link_config *link; 1791 int count; 1792 int size; 1793 int i, ret; 1794 1795 count = le32_to_cpu(hdr->count); 1796 1797 /* check the element size and count */ 1798 link = (struct snd_soc_tplg_link_config *)tplg->pos; 1799 size = le32_to_cpu(link->size); 1800 if (size > sizeof(struct snd_soc_tplg_link_config)) { 1801 dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n", 1802 size); 1803 return -EINVAL; 1804 } 1805 1806 if (soc_tplg_check_elem_count(tplg, size, count, 1807 le32_to_cpu(hdr->payload_size), 1808 "physical link config")) 1809 return -EINVAL; 1810 1811 /* config physical DAI links */ 1812 for (i = 0; i < count; i++) { 1813 link = (struct snd_soc_tplg_link_config *)tplg->pos; 1814 size = le32_to_cpu(link->size); 1815 if (size != sizeof(*link)) 1816 return -EINVAL; 1817 1818 ret = soc_tplg_link_config(tplg, link); 1819 if (ret < 0) 1820 return ret; 1821 1822 /* offset by version-specific struct size and 1823 * real priv data size 1824 */ 1825 tplg->pos += size + le32_to_cpu(link->priv.size); 1826 } 1827 1828 return 0; 1829 } 1830 1831 /** 1832 * soc_tplg_dai_config - Find and configure an existing physical DAI. 1833 * @tplg: topology context 1834 * @d: physical DAI configs. 1835 * 1836 * The physical dai should already be registered by the platform driver. 1837 * The platform driver should specify the DAI name and ID for matching. 1838 */ 1839 static int soc_tplg_dai_config(struct soc_tplg *tplg, 1840 struct snd_soc_tplg_dai *d) 1841 { 1842 struct snd_soc_dai_link_component dai_component; 1843 struct snd_soc_dai *dai; 1844 struct snd_soc_dai_driver *dai_drv; 1845 struct snd_soc_pcm_stream *stream; 1846 struct snd_soc_tplg_stream_caps *caps; 1847 int ret; 1848 1849 memset(&dai_component, 0, sizeof(dai_component)); 1850 1851 dai_component.dai_name = d->dai_name; 1852 dai = snd_soc_find_dai(&dai_component); 1853 if (!dai) { 1854 dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n", 1855 d->dai_name); 1856 return -EINVAL; 1857 } 1858 1859 if (le32_to_cpu(d->dai_id) != dai->id) { 1860 dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n", 1861 d->dai_name); 1862 return -EINVAL; 1863 } 1864 1865 dai_drv = dai->driver; 1866 if (!dai_drv) 1867 return -EINVAL; 1868 1869 if (d->playback) { 1870 stream = &dai_drv->playback; 1871 caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; 1872 ret = set_stream_info(tplg, stream, caps); 1873 if (ret < 0) 1874 return ret; 1875 } 1876 1877 if (d->capture) { 1878 stream = &dai_drv->capture; 1879 caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE]; 1880 ret = set_stream_info(tplg, stream, caps); 1881 if (ret < 0) 1882 return ret; 1883 } 1884 1885 if (d->flag_mask) 1886 set_dai_flags(dai_drv, 1887 le32_to_cpu(d->flag_mask), 1888 le32_to_cpu(d->flags)); 1889 1890 /* pass control to component driver for optional further init */ 1891 ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai); 1892 if (ret < 0) { 1893 dev_err(tplg->dev, "ASoC: DAI loading failed\n"); 1894 return ret; 1895 } 1896 1897 return 0; 1898 } 1899 1900 /* load physical DAI elements */ 1901 static int soc_tplg_dai_elems_load(struct soc_tplg *tplg, 1902 struct snd_soc_tplg_hdr *hdr) 1903 { 1904 int count; 1905 int i; 1906 1907 count = le32_to_cpu(hdr->count); 1908 1909 /* config the existing BE DAIs */ 1910 for (i = 0; i < count; i++) { 1911 struct snd_soc_tplg_dai *dai = (struct snd_soc_tplg_dai *)tplg->pos; 1912 int ret; 1913 1914 if (le32_to_cpu(dai->size) != sizeof(*dai)) { 1915 dev_err(tplg->dev, "ASoC: invalid physical DAI size\n"); 1916 return -EINVAL; 1917 } 1918 1919 ret = soc_tplg_dai_config(tplg, dai); 1920 if (ret < 0) { 1921 dev_err(tplg->dev, "ASoC: failed to configure DAI\n"); 1922 return ret; 1923 } 1924 1925 tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size)); 1926 } 1927 1928 dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count); 1929 return 0; 1930 } 1931 1932 static int soc_tplg_manifest_load(struct soc_tplg *tplg, 1933 struct snd_soc_tplg_hdr *hdr) 1934 { 1935 struct snd_soc_tplg_manifest *manifest; 1936 int ret = 0; 1937 1938 manifest = (struct snd_soc_tplg_manifest *)tplg->pos; 1939 1940 /* check ABI version by size, create a new manifest if abi not match */ 1941 if (le32_to_cpu(manifest->size) != sizeof(*manifest)) 1942 return -EINVAL; 1943 1944 /* pass control to component driver for optional further init */ 1945 if (tplg->ops && tplg->ops->manifest) 1946 ret = tplg->ops->manifest(tplg->comp, tplg->index, manifest); 1947 1948 return ret; 1949 } 1950 1951 /* validate header magic, size and type */ 1952 static int soc_tplg_valid_header(struct soc_tplg *tplg, 1953 struct snd_soc_tplg_hdr *hdr) 1954 { 1955 if (le32_to_cpu(hdr->size) != sizeof(*hdr)) { 1956 dev_err(tplg->dev, 1957 "ASoC: invalid header size for type %u at offset 0x%lx size 0x%zx.\n", 1958 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), 1959 tplg->fw->size); 1960 return -EINVAL; 1961 } 1962 1963 if (soc_tplg_get_hdr_offset(tplg) + le32_to_cpu(hdr->payload_size) >= tplg->fw->size) { 1964 dev_err(tplg->dev, 1965 "ASoC: invalid header of type %u at offset %ld payload_size %u\n", 1966 le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), 1967 le32_to_cpu(hdr->payload_size)); 1968 return -EINVAL; 1969 } 1970 1971 /* big endian firmware objects not supported atm */ 1972 if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) { 1973 dev_err(tplg->dev, 1974 "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", 1975 tplg->pass, le32_to_cpu(hdr->magic), 1976 soc_tplg_get_hdr_offset(tplg), tplg->fw->size); 1977 return -EINVAL; 1978 } 1979 1980 if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) { 1981 dev_err(tplg->dev, 1982 "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", 1983 tplg->pass, le32_to_cpu(hdr->magic), 1984 soc_tplg_get_hdr_offset(tplg), tplg->fw->size); 1985 return -EINVAL; 1986 } 1987 1988 /* Support ABI from version 4 */ 1989 if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION || 1990 le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) { 1991 dev_err(tplg->dev, 1992 "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", 1993 tplg->pass, le32_to_cpu(hdr->abi), 1994 SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), 1995 tplg->fw->size); 1996 return -EINVAL; 1997 } 1998 1999 if (hdr->payload_size == 0) { 2000 dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", 2001 soc_tplg_get_hdr_offset(tplg)); 2002 return -EINVAL; 2003 } 2004 2005 return 0; 2006 } 2007 2008 /* check header type and call appropriate handler */ 2009 static int soc_tplg_load_header(struct soc_tplg *tplg, 2010 struct snd_soc_tplg_hdr *hdr) 2011 { 2012 int (*elem_load)(struct soc_tplg *tplg, 2013 struct snd_soc_tplg_hdr *hdr); 2014 unsigned int hdr_pass; 2015 2016 tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); 2017 2018 tplg->index = le32_to_cpu(hdr->index); 2019 2020 switch (le32_to_cpu(hdr->type)) { 2021 case SND_SOC_TPLG_TYPE_MIXER: 2022 case SND_SOC_TPLG_TYPE_ENUM: 2023 case SND_SOC_TPLG_TYPE_BYTES: 2024 hdr_pass = SOC_TPLG_PASS_CONTROL; 2025 elem_load = soc_tplg_kcontrol_elems_load; 2026 break; 2027 case SND_SOC_TPLG_TYPE_DAPM_GRAPH: 2028 hdr_pass = SOC_TPLG_PASS_GRAPH; 2029 elem_load = soc_tplg_dapm_graph_elems_load; 2030 break; 2031 case SND_SOC_TPLG_TYPE_DAPM_WIDGET: 2032 hdr_pass = SOC_TPLG_PASS_WIDGET; 2033 elem_load = soc_tplg_dapm_widget_elems_load; 2034 break; 2035 case SND_SOC_TPLG_TYPE_PCM: 2036 hdr_pass = SOC_TPLG_PASS_PCM_DAI; 2037 elem_load = soc_tplg_pcm_elems_load; 2038 break; 2039 case SND_SOC_TPLG_TYPE_DAI: 2040 hdr_pass = SOC_TPLG_PASS_BE_DAI; 2041 elem_load = soc_tplg_dai_elems_load; 2042 break; 2043 case SND_SOC_TPLG_TYPE_DAI_LINK: 2044 case SND_SOC_TPLG_TYPE_BACKEND_LINK: 2045 /* physical link configurations */ 2046 hdr_pass = SOC_TPLG_PASS_LINK; 2047 elem_load = soc_tplg_link_elems_load; 2048 break; 2049 case SND_SOC_TPLG_TYPE_MANIFEST: 2050 hdr_pass = SOC_TPLG_PASS_MANIFEST; 2051 elem_load = soc_tplg_manifest_load; 2052 break; 2053 default: 2054 /* bespoke vendor data object */ 2055 hdr_pass = SOC_TPLG_PASS_VENDOR; 2056 elem_load = soc_tplg_vendor_load; 2057 break; 2058 } 2059 2060 if (tplg->pass == hdr_pass) { 2061 dev_dbg(tplg->dev, 2062 "ASoC: Got 0x%x bytes of type %u version %u vendor %u at pass %d\n", 2063 le32_to_cpu(hdr->payload_size), 2064 le32_to_cpu(hdr->type), 2065 le32_to_cpu(hdr->version), 2066 le32_to_cpu(hdr->vendor_type), tplg->pass); 2067 return elem_load(tplg, hdr); 2068 } 2069 2070 return 0; 2071 } 2072 2073 /* process the topology file headers */ 2074 static int soc_tplg_process_headers(struct soc_tplg *tplg) 2075 { 2076 int ret; 2077 2078 /* process the header types from start to end */ 2079 for (tplg->pass = SOC_TPLG_PASS_START; tplg->pass <= SOC_TPLG_PASS_END; tplg->pass++) { 2080 struct snd_soc_tplg_hdr *hdr; 2081 2082 tplg->hdr_pos = tplg->fw->data; 2083 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; 2084 2085 while (!soc_tplg_is_eof(tplg)) { 2086 2087 /* make sure header is valid before loading */ 2088 ret = soc_tplg_valid_header(tplg, hdr); 2089 if (ret < 0) 2090 return ret; 2091 2092 /* load the header object */ 2093 ret = soc_tplg_load_header(tplg, hdr); 2094 if (ret < 0) { 2095 if (ret != -EPROBE_DEFER) { 2096 dev_err(tplg->dev, 2097 "ASoC: topology: could not load header: %d\n", 2098 ret); 2099 } 2100 return ret; 2101 } 2102 2103 /* goto next header */ 2104 tplg->hdr_pos += le32_to_cpu(hdr->payload_size) + 2105 sizeof(struct snd_soc_tplg_hdr); 2106 hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; 2107 } 2108 2109 } 2110 2111 /* signal DAPM we are complete */ 2112 ret = soc_tplg_dapm_complete(tplg); 2113 2114 return ret; 2115 } 2116 2117 static int soc_tplg_load(struct soc_tplg *tplg) 2118 { 2119 int ret; 2120 2121 ret = soc_tplg_process_headers(tplg); 2122 if (ret == 0) 2123 return soc_tplg_complete(tplg); 2124 2125 return ret; 2126 } 2127 2128 /* load audio component topology from "firmware" file */ 2129 int snd_soc_tplg_component_load(struct snd_soc_component *comp, 2130 const struct snd_soc_tplg_ops *ops, const struct firmware *fw) 2131 { 2132 struct soc_tplg tplg; 2133 int ret; 2134 2135 /* 2136 * check if we have sane parameters: 2137 * comp - needs to exist to keep and reference data while parsing 2138 * comp->card - used for setting card related parameters 2139 * comp->card->dev - used for resource management and prints 2140 * fw - we need it, as it is the very thing we parse 2141 */ 2142 if (!comp || !comp->card || !comp->card->dev || !fw) 2143 return -EINVAL; 2144 2145 /* setup parsing context */ 2146 memset(&tplg, 0, sizeof(tplg)); 2147 tplg.fw = fw; 2148 tplg.dev = comp->card->dev; 2149 tplg.comp = comp; 2150 if (ops) { 2151 tplg.ops = ops; 2152 tplg.io_ops = ops->io_ops; 2153 tplg.io_ops_count = ops->io_ops_count; 2154 tplg.bytes_ext_ops = ops->bytes_ext_ops; 2155 tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; 2156 } 2157 2158 ret = soc_tplg_load(&tplg); 2159 /* free the created components if fail to load topology */ 2160 if (ret) 2161 snd_soc_tplg_component_remove(comp); 2162 2163 return ret; 2164 } 2165 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); 2166 2167 /* remove dynamic controls from the component driver */ 2168 int snd_soc_tplg_component_remove(struct snd_soc_component *comp) 2169 { 2170 struct snd_soc_dobj *dobj, *next_dobj; 2171 int pass; 2172 2173 /* process the header types from end to start */ 2174 for (pass = SOC_TPLG_PASS_END; pass >= SOC_TPLG_PASS_START; pass--) { 2175 2176 /* remove mixer controls */ 2177 list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, 2178 list) { 2179 2180 switch (dobj->type) { 2181 case SND_SOC_DOBJ_BYTES: 2182 case SND_SOC_DOBJ_ENUM: 2183 case SND_SOC_DOBJ_MIXER: 2184 soc_tplg_remove_kcontrol(comp, dobj, pass); 2185 break; 2186 case SND_SOC_DOBJ_GRAPH: 2187 soc_tplg_remove_route(comp, dobj, pass); 2188 break; 2189 case SND_SOC_DOBJ_WIDGET: 2190 soc_tplg_remove_widget(comp, dobj, pass); 2191 break; 2192 case SND_SOC_DOBJ_PCM: 2193 soc_tplg_remove_dai(comp, dobj, pass); 2194 break; 2195 case SND_SOC_DOBJ_DAI_LINK: 2196 soc_tplg_remove_link(comp, dobj, pass); 2197 break; 2198 case SND_SOC_DOBJ_BACKEND_LINK: 2199 /* 2200 * call link_unload ops if extra 2201 * deinitialization is needed. 2202 */ 2203 remove_backend_link(comp, dobj, pass); 2204 break; 2205 default: 2206 dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", 2207 dobj->type); 2208 break; 2209 } 2210 } 2211 } 2212 2213 /* let caller know if FW can be freed when no objects are left */ 2214 return !list_empty(&comp->dobj_list); 2215 } 2216 EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); 2217