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