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