soc-topology.c 76.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// SPDX-License-Identifier: GPL-2.0+
//
// soc-topology.c  --  ALSA SoC Topology
//
// Copyright (C) 2012 Texas Instruments Inc.
// Copyright (C) 2015 Intel Corporation.
//
// Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
//		K, Mythri P <mythri.p.k@intel.com>
//		Prusty, Subhransu S <subhransu.s.prusty@intel.com>
//		B, Jayachandran <jayachandran.b@intel.com>
//		Abdullah, Omair M <omair.m.abdullah@intel.com>
//		Jin, Yao <yao.jin@intel.com>
//		Lin, Mengdong <mengdong.lin@intel.com>
//
//  Add support to read audio firmware topology alongside firmware text. The
//  topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
//  equalizers, firmware, coefficients etc.
//
//  This file only manages the core ALSA and ASoC components, all other bespoke
//  firmware topology data is passed to component drivers for bespoke handling.
22 23 24 25 26 27 28 29 30

#include <linux/kernel.h>
#include <linux/export.h>
#include <linux/list.h>
#include <linux/firmware.h>
#include <linux/slab.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/soc-topology.h>
31
#include <sound/tlv.h>
32

33 34
#define SOC_TPLG_MAGIC_BIG_ENDIAN            0x436F5341 /* ASoC in reverse */

35 36 37 38 39 40 41 42 43 44
/*
 * We make several passes over the data (since it wont necessarily be ordered)
 * and process objects in the following order. This guarantees the component
 * drivers will be ready with any vendor data before the mixers and DAPM objects
 * are loaded (that may make use of the vendor data).
 */
#define SOC_TPLG_PASS_MANIFEST		0
#define SOC_TPLG_PASS_VENDOR		1
#define SOC_TPLG_PASS_MIXER		2
#define SOC_TPLG_PASS_WIDGET		3
45 46 47
#define SOC_TPLG_PASS_PCM_DAI		4
#define SOC_TPLG_PASS_GRAPH		5
#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 56 57 58 59 60 61 62 63 64 65 66 67 68
struct soc_tplg {
	const struct firmware *fw;

	/* runtime FW parsing */
	const u8 *pos;		/* read postion */
	const u8 *hdr_pos;	/* header position */
	unsigned int pass;	/* pass number */

	/* component caller */
	struct device *dev;
	struct snd_soc_component *comp;
	u32 index;	/* current block index */
	u32 req_index;	/* required index, only loaded/free matching blocks */

69
	/* vendor specific kcontrol operations */
70 71 72
	const struct snd_soc_tplg_kcontrol_ops *io_ops;
	int io_ops_count;

73 74 75 76
	/* vendor specific bytes ext handlers, for TLV bytes controls */
	const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
	int bytes_ext_ops_count;

77 78 79 80 81 82
	/* optional fw loading callbacks to component drivers */
	struct snd_soc_tplg_ops *ops;
};

static int soc_tplg_process_headers(struct soc_tplg *tplg);
static void soc_tplg_complete(struct soc_tplg *tplg);
83 84
static void soc_tplg_denum_remove_texts(struct soc_enum *se);
static void soc_tplg_denum_remove_values(struct soc_enum *se);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

/* check we dont overflow the data for this control chunk */
static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
	unsigned int count, size_t bytes, const char *elem_type)
{
	const u8 *end = tplg->pos + elem_size * count;

	if (end > tplg->fw->data + tplg->fw->size) {
		dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
			elem_type);
		return -EINVAL;
	}

	/* check there is enough room in chunk for control.
	   extra bytes at the end of control are for vendor data here  */
	if (elem_size * count > bytes) {
		dev_err(tplg->dev,
			"ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
			elem_type, count, elem_size, bytes);
		return -EINVAL;
	}

	return 0;
}

static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
{
	const u8 *end = tplg->hdr_pos;

	if (end >= tplg->fw->data + tplg->fw->size)
		return 1;
	return 0;
}

static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
{
	return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
}

static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
{
	return (unsigned long)(tplg->pos - tplg->fw->data);
}

/* mapping of Kcontrol types and associated operations. */
static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
	{SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
		snd_soc_put_volsw, snd_soc_info_volsw},
	{SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
		snd_soc_put_volsw_sx, NULL},
	{SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
		snd_soc_put_enum_double, snd_soc_info_enum_double},
	{SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
		snd_soc_put_enum_double, NULL},
	{SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
		snd_soc_bytes_put, snd_soc_bytes_info},
	{SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
		snd_soc_put_volsw_range, snd_soc_info_volsw_range},
	{SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
		snd_soc_put_xr_sx, snd_soc_info_xr_sx},
	{SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
		snd_soc_put_strobe, NULL},
	{SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
148
		snd_soc_dapm_put_volsw, snd_soc_info_volsw},
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	{SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
		snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
	{SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
		snd_soc_dapm_put_enum_double, NULL},
	{SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
		snd_soc_dapm_put_enum_double, NULL},
	{SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
		snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
};

struct soc_tplg_map {
	int uid;
	int kid;
};

/* mapping of widget types from UAPI IDs to kernel IDs */
static const struct soc_tplg_map dapm_map[] = {
	{SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
	{SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
	{SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
	{SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
	{SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
	{SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
	{SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
	{SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
	{SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
	{SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
	{SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
	{SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
	{SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
	{SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
	{SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
	{SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
182 183 184 185 186 187 188 189
	{SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
	{SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
	{SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
	{SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
	{SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
	{SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
	{SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
	{SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
190 191 192 193 194 195 196 197
};

static int tplc_chan_get_reg(struct soc_tplg *tplg,
	struct snd_soc_tplg_channel *chan, int map)
{
	int i;

	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
198 199
		if (le32_to_cpu(chan[i].id) == map)
			return le32_to_cpu(chan[i].reg);
200 201 202 203 204 205 206 207 208 209 210
	}

	return -EINVAL;
}

static int tplc_chan_get_shift(struct soc_tplg *tplg,
	struct snd_soc_tplg_channel *chan, int map)
{
	int i;

	for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
211 212
		if (le32_to_cpu(chan[i].id) == map)
			return le32_to_cpu(chan[i].shift);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	}

	return -EINVAL;
}

static int get_widget_id(int tplg_type)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
		if (tplg_type == dapm_map[i].uid)
			return dapm_map[i].kid;
	}

	return -EINVAL;
}

static inline void soc_bind_err(struct soc_tplg *tplg,
	struct snd_soc_tplg_ctl_hdr *hdr, int index)
{
	dev_err(tplg->dev,
		"ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
		hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
		soc_tplg_get_offset(tplg));
}

static inline void soc_control_err(struct soc_tplg *tplg,
	struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
{
	dev_err(tplg->dev,
		"ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
		name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
		soc_tplg_get_offset(tplg));
}

/* pass vendor data to component driver for processing */
249 250
static int soc_tplg_vendor_load(struct soc_tplg *tplg,
				struct snd_soc_tplg_hdr *hdr)
251 252 253
{
	int ret = 0;

254
	if (tplg->ops && tplg->ops->vendor_load)
255
		ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	else {
		dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
			hdr->vendor_type);
		return -EINVAL;
	}

	if (ret < 0)
		dev_err(tplg->dev,
			"ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
			soc_tplg_get_hdr_offset(tplg),
			soc_tplg_get_hdr_offset(tplg),
			hdr->type, hdr->vendor_type);
	return ret;
}

/* optionally pass new dynamic widget to component driver. This is mainly for
 * external widgets where we can assign private data/ops */
static int soc_tplg_widget_load(struct soc_tplg *tplg,
	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
{
276
	if (tplg->ops && tplg->ops->widget_load)
277 278
		return tplg->ops->widget_load(tplg->comp, tplg->index, w,
			tplg_w);
279 280 281 282

	return 0;
}

283 284 285 286 287
/* optionally pass new dynamic widget to component driver. This is mainly for
 * external widgets where we can assign private data/ops */
static int soc_tplg_widget_ready(struct soc_tplg *tplg,
	struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
{
288
	if (tplg->ops && tplg->ops->widget_ready)
289 290
		return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
			tplg_w);
291 292 293 294

	return 0;
}

295
/* pass DAI configurations to component driver for extra initialization */
296
static int soc_tplg_dai_load(struct soc_tplg *tplg,
297 298
	struct snd_soc_dai_driver *dai_drv,
	struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
299
{
300
	if (tplg->ops && tplg->ops->dai_load)
301 302
		return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
			pcm, dai);
303 304 305 306

	return 0;
}

307
/* pass link configurations to component driver for extra initialization */
308
static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
309
	struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
310
{
311
	if (tplg->ops && tplg->ops->link_load)
312
		return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
313 314 315 316

	return 0;
}

317 318 319
/* tell the component driver that all firmware has been loaded in this request */
static void soc_tplg_complete(struct soc_tplg *tplg)
{
320
	if (tplg->ops && tplg->ops->complete)
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		tplg->ops->complete(tplg->comp);
}

/* add a dynamic kcontrol */
static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
	const struct snd_kcontrol_new *control_new, const char *prefix,
	void *data, struct snd_kcontrol **kcontrol)
{
	int err;

	*kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
	if (*kcontrol == NULL) {
		dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
		control_new->name);
		return -ENOMEM;
	}

	err = snd_ctl_add(card, *kcontrol);
	if (err < 0) {
		dev_err(dev, "ASoC: Failed to add %s: %d\n",
			control_new->name, err);
		return err;
	}

	return 0;
}

/* add a dynamic kcontrol for component driver */
static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
	struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
{
	struct snd_soc_component *comp = tplg->comp;

	return soc_tplg_add_dcontrol(comp->card->snd_card,
355
				comp->dev, k, comp->name_prefix, comp, kcontrol);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
}

/* remove a mixer kcontrol */
static void remove_mixer(struct snd_soc_component *comp,
	struct snd_soc_dobj *dobj, int pass)
{
	struct snd_card *card = comp->card->snd_card;
	struct soc_mixer_control *sm =
		container_of(dobj, struct soc_mixer_control, dobj);
	const unsigned int *p = NULL;

	if (pass != SOC_TPLG_PASS_MIXER)
		return;

	if (dobj->ops && dobj->ops->control_unload)
		dobj->ops->control_unload(comp, dobj);

373 374 375 376
	if (dobj->control.kcontrol->tlv.p)
		p = dobj->control.kcontrol->tlv.p;
	snd_ctl_remove(card, dobj->control.kcontrol);
	list_del(&dobj->list);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	kfree(sm);
	kfree(p);
}

/* remove an enum kcontrol */
static void remove_enum(struct snd_soc_component *comp,
	struct snd_soc_dobj *dobj, int pass)
{
	struct snd_card *card = comp->card->snd_card;
	struct soc_enum *se = container_of(dobj, struct soc_enum, dobj);

	if (pass != SOC_TPLG_PASS_MIXER)
		return;

	if (dobj->ops && dobj->ops->control_unload)
		dobj->ops->control_unload(comp, dobj);

394 395
	snd_ctl_remove(card, dobj->control.kcontrol);
	list_del(&dobj->list);
396

397 398
	soc_tplg_denum_remove_values(se);
	soc_tplg_denum_remove_texts(se);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	kfree(se);
}

/* remove a byte kcontrol */
static void remove_bytes(struct snd_soc_component *comp,
	struct snd_soc_dobj *dobj, int pass)
{
	struct snd_card *card = comp->card->snd_card;
	struct soc_bytes_ext *sb =
		container_of(dobj, struct soc_bytes_ext, dobj);

	if (pass != SOC_TPLG_PASS_MIXER)
		return;

	if (dobj->ops && dobj->ops->control_unload)
		dobj->ops->control_unload(comp, dobj);

416 417
	snd_ctl_remove(card, dobj->control.kcontrol);
	list_del(&dobj->list);
418 419 420
	kfree(sb);
}

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
/* remove a route */
static void remove_route(struct snd_soc_component *comp,
			 struct snd_soc_dobj *dobj, int pass)
{
	struct snd_soc_dapm_route *route =
		container_of(dobj, struct snd_soc_dapm_route, dobj);

	if (pass != SOC_TPLG_PASS_GRAPH)
		return;

	if (dobj->ops && dobj->ops->dapm_route_unload)
		dobj->ops->dapm_route_unload(comp, dobj);

	list_del(&dobj->list);
	kfree(route);
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/* remove a widget and it's kcontrols - routes must be removed first */
static void remove_widget(struct snd_soc_component *comp,
	struct snd_soc_dobj *dobj, int pass)
{
	struct snd_card *card = comp->card->snd_card;
	struct snd_soc_dapm_widget *w =
		container_of(dobj, struct snd_soc_dapm_widget, dobj);
	int i;

	if (pass != SOC_TPLG_PASS_WIDGET)
		return;

	if (dobj->ops && dobj->ops->widget_unload)
		dobj->ops->widget_unload(comp, dobj);

453 454 455
	if (!w->kcontrols)
		goto free_news;

456
	/*
457
	 * Dynamic Widgets either have 1..N enum kcontrols or mixers.
458 459
	 * The enum may either have an array of values or strings.
	 */
460
	if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
461
		/* enumerated widget mixer */
462
		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
463 464 465
			struct snd_kcontrol *kcontrol = w->kcontrols[i];
			struct soc_enum *se =
				(struct soc_enum *)kcontrol->private_value;
466

467
			snd_ctl_remove(card, kcontrol);
468

469
			/* free enum kcontrol's dvalues and dtexts */
470 471
			soc_tplg_denum_remove_values(se);
			soc_tplg_denum_remove_texts(se);
472

473
			kfree(se);
474
			kfree(w->kcontrol_news[i].name);
475
		}
476
	} else {
477
		/* volume mixer or bytes controls */
478
		for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
479 480
			struct snd_kcontrol *kcontrol = w->kcontrols[i];

481 482 483
			if (dobj->widget.kcontrol_type
			    == SND_SOC_TPLG_TYPE_MIXER)
				kfree(kcontrol->tlv.p);
484

485 486 487 488 489
			/* Private value is used as struct soc_mixer_control
			 * for volume mixers or soc_bytes_ext for bytes
			 * controls.
			 */
			kfree((void *)kcontrol->private_value);
490
			snd_ctl_remove(card, kcontrol);
491
			kfree(w->kcontrol_news[i].name);
492 493
		}
	}
494 495 496 497

free_news:
	kfree(w->kcontrol_news);

498 499
	list_del(&dobj->list);

500 501 502
	/* widget w is freed by soc-dapm.c */
}

503 504
/* remove DAI configurations */
static void remove_dai(struct snd_soc_component *comp,
505 506
	struct snd_soc_dobj *dobj, int pass)
{
507 508
	struct snd_soc_dai_driver *dai_drv =
		container_of(dobj, struct snd_soc_dai_driver, dobj);
509
	struct snd_soc_dai *dai;
510

511 512 513
	if (pass != SOC_TPLG_PASS_PCM_DAI)
		return;

514 515
	if (dobj->ops && dobj->ops->dai_unload)
		dobj->ops->dai_unload(comp, dobj);
516

517
	for_each_component_dais(comp, dai)
518 519 520
		if (dai->driver == dai_drv)
			dai->driver = NULL;

521 522
	kfree(dai_drv->playback.stream_name);
	kfree(dai_drv->capture.stream_name);
523
	kfree(dai_drv->name);
524
	list_del(&dobj->list);
525
	kfree(dai_drv);
526 527
}

528 529 530 531 532 533 534 535 536 537 538 539 540
/* remove link configurations */
static void remove_link(struct snd_soc_component *comp,
	struct snd_soc_dobj *dobj, int pass)
{
	struct snd_soc_dai_link *link =
		container_of(dobj, struct snd_soc_dai_link, dobj);

	if (pass != SOC_TPLG_PASS_PCM_DAI)
		return;

	if (dobj->ops && dobj->ops->link_unload)
		dobj->ops->link_unload(comp, dobj);

541
	list_del(&dobj->list);
542 543
	snd_soc_remove_pcm_runtime(comp->card,
			snd_soc_get_pcm_runtime(comp->card, link));
544

545 546
	kfree(link->name);
	kfree(link->stream_name);
547
	kfree(link->cpus->dai_name);
548 549 550
	kfree(link);
}

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
/* unload dai link */
static void remove_backend_link(struct snd_soc_component *comp,
	struct snd_soc_dobj *dobj, int pass)
{
	if (pass != SOC_TPLG_PASS_LINK)
		return;

	if (dobj->ops && dobj->ops->link_unload)
		dobj->ops->link_unload(comp, dobj);

	/*
	 * We don't free the link here as what remove_link() do since BE
	 * links are not allocated by topology.
	 * We however need to reset the dobj type to its initial values
	 */
	dobj->type = SND_SOC_DOBJ_NONE;
	list_del(&dobj->list);
}

570 571 572
/* bind a kcontrol to it's IO handlers */
static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
	struct snd_kcontrol_new *k,
573
	const struct soc_tplg *tplg)
574
{
575
	const struct snd_soc_tplg_kcontrol_ops *ops;
576
	const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
577
	int num_ops, i;
578

579
	if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES
580 581 582 583 584 585 586 587 588 589 590 591
		&& k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
		&& k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
		struct soc_bytes_ext *sbe;
		struct snd_soc_tplg_bytes_control *be;

		sbe = (struct soc_bytes_ext *)k->private_value;
		be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);

		/* TLV bytes controls need standard kcontrol info handler,
		 * TLV callback and extended put/get handlers.
		 */
592
		k->info = snd_soc_bytes_info_ext;
593 594
		k->tlv.c = snd_soc_bytes_tlv_callback;

595 596 597 598 599 600 601 602 603 604 605
		/*
		 * When a topology-based implementation abuses the
		 * control interface and uses bytes_ext controls of
		 * more than 512 bytes, we need to disable the size
		 * checks, otherwise accesses to such controls will
		 * return an -EINVAL error and prevent the card from
		 * being configured.
		 */
		if (IS_ENABLED(CONFIG_SND_CTL_VALIDATION) && sbe->max > 512)
			k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK;

606 607 608
		ext_ops = tplg->bytes_ext_ops;
		num_ops = tplg->bytes_ext_ops_count;
		for (i = 0; i < num_ops; i++) {
609 610
			if (!sbe->put &&
			    ext_ops[i].id == le32_to_cpu(be->ext_ops.put))
611
				sbe->put = ext_ops[i].put;
612 613
			if (!sbe->get &&
			    ext_ops[i].id == le32_to_cpu(be->ext_ops.get))
614 615 616
				sbe->get = ext_ops[i].get;
		}

617
		if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get)
618
			return -EINVAL;
619 620 621
		if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put)
			return -EINVAL;
		return 0;
622
	}
623

624
	/* try and map vendor specific kcontrol handlers first */
625 626
	ops = tplg->io_ops;
	num_ops = tplg->io_ops_count;
627 628
	for (i = 0; i < num_ops; i++) {

629
		if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
630
			k->put = ops[i].put;
631
		if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
632
			k->get = ops[i].get;
633
		if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
634 635 636
			k->info = ops[i].info;
	}

637
	/* vendor specific handlers found ? */
638 639 640
	if (k->put && k->get && k->info)
		return 0;

641
	/* none found so try standard kcontrol handlers */
642 643
	ops = io_ops;
	num_ops = ARRAY_SIZE(io_ops);
644
	for (i = 0; i < num_ops; i++) {
645

646
		if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put))
647
			k->put = ops[i].put;
648
		if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get))
649
			k->get = ops[i].get;
650
		if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info))
651
			k->info = ops[i].info;
652 653
	}

654
	/* standard handlers found ? */
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	if (k->put && k->get && k->info)
		return 0;

	/* nothing to bind */
	return -EINVAL;
}

/* bind a widgets to it's evnt handlers */
int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
		const struct snd_soc_tplg_widget_events *events,
		int num_events, u16 event_type)
{
	int i;

	w->event = NULL;

	for (i = 0; i < num_events; i++) {
		if (event_type == events[i].type) {

			/* found - so assign event */
			w->event = events[i].event_handler;
			return 0;
		}
	}

	/* not found */
	return -EINVAL;
}
EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);

/* optionally pass new dynamic kcontrol to component driver. */
static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
	struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
{
689
	if (tplg->ops && tplg->ops->control_load)
690 691
		return tplg->ops->control_load(tplg->comp, tplg->index, k,
			hdr);
692 693 694 695 696

	return 0;
}


697 698 699 700 701
static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
	struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
{
	unsigned int item_len = 2 * sizeof(unsigned int);
	unsigned int *p;
702

703 704
	p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
	if (!p)
705 706
		return -ENOMEM;

707 708
	p[0] = SNDRV_CTL_TLVT_DB_SCALE;
	p[1] = item_len;
709 710 711
	p[2] = le32_to_cpu(scale->min);
	p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK)
		| (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0);
712 713 714 715 716 717 718 719 720

	kc->tlv.p = (void *)p;
	return 0;
}

static int soc_tplg_create_tlv(struct soc_tplg *tplg,
	struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
{
	struct snd_soc_tplg_ctl_tlv *tplg_tlv;
721
	u32 access = le32_to_cpu(tc->access);
722

723
	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
724
		return 0;
725

726
	if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
727
		tplg_tlv = &tc->tlv;
728
		switch (le32_to_cpu(tplg_tlv->type)) {
729 730 731 732 733 734 735 736 737 738 739
		case SNDRV_CTL_TLVT_DB_SCALE:
			return soc_tplg_create_tlv_db_scale(tplg, kc,
					&tplg_tlv->scale);

		/* TODO: add support for other TLV types */
		default:
			dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
					tplg_tlv->type);
			return -EINVAL;
		}
	}
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

	return 0;
}

static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
	struct snd_kcontrol_new *kc)
{
	kfree(kc->tlv.p);
}

static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
	size_t size)
{
	struct snd_soc_tplg_bytes_control *be;
	struct soc_bytes_ext *sbe;
	struct snd_kcontrol_new kc;
756 757
	int i;
	int err = 0;
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779

	if (soc_tplg_check_elem_count(tplg,
		sizeof(struct snd_soc_tplg_bytes_control), count,
			size, "mixer bytes")) {
		dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
			count);
		return -EINVAL;
	}

	for (i = 0; i < count; i++) {
		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;

		/* validate kcontrol */
		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
			return -EINVAL;

		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
		if (sbe == NULL)
			return -ENOMEM;

		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
780
			      le32_to_cpu(be->priv.size));
781 782 783 784 785 786 787 788 789

		dev_dbg(tplg->dev,
			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
			be->hdr.name, be->hdr.access);

		memset(&kc, 0, sizeof(kc));
		kc.name = be->hdr.name;
		kc.private_value = (long)sbe;
		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 794 795 796 797
		sbe->dobj.type = SND_SOC_DOBJ_BYTES;
		sbe->dobj.ops = tplg->ops;
		INIT_LIST_HEAD(&sbe->dobj.list);

		/* map io handlers */
798
		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
799 800 801
		if (err) {
			soc_control_err(tplg, &be->hdr, be->hdr.name);
			kfree(sbe);
802
			break;
803 804 805 806 807 808 809 810 811
		}

		/* pass control to driver for optional further init */
		err = soc_tplg_init_kcontrol(tplg, &kc,
			(struct snd_soc_tplg_ctl_hdr *)be);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to init %s\n",
				be->hdr.name);
			kfree(sbe);
812
			break;
813 814 815 816 817 818 819 820 821
		}

		/* register control here */
		err = soc_tplg_add_kcontrol(tplg, &kc,
			&sbe->dobj.control.kcontrol);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to add %s\n",
				be->hdr.name);
			kfree(sbe);
822
			break;
823 824 825 826
		}

		list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
	}
827
	return err;
828 829 830 831 832 833 834 835 836

}

static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
	size_t size)
{
	struct snd_soc_tplg_mixer_control *mc;
	struct soc_mixer_control *sm;
	struct snd_kcontrol_new kc;
837 838
	int i;
	int err = 0;
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

	if (soc_tplg_check_elem_count(tplg,
		sizeof(struct snd_soc_tplg_mixer_control),
		count, size, "mixers")) {

		dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
			count);
		return -EINVAL;
	}

	for (i = 0; i < count; i++) {
		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;

		/* validate kcontrol */
		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
			return -EINVAL;

		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
		if (sm == NULL)
			return -ENOMEM;
		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
861
			      le32_to_cpu(mc->priv.size));
862 863 864 865 866 867 868 869 870

		dev_dbg(tplg->dev,
			"ASoC: adding mixer kcontrol %s with access 0x%x\n",
			mc->hdr.name, mc->hdr.access);

		memset(&kc, 0, sizeof(kc));
		kc.name = mc->hdr.name;
		kc.private_value = (long)sm;
		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
871
		kc.access = le32_to_cpu(mc->hdr.access);
872 873 874 875 876 877 878 879 880 881 882

		/* we only support FL/FR channel mapping atm */
		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
			SNDRV_CHMAP_FL);
		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
			SNDRV_CHMAP_FR);
		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
			SNDRV_CHMAP_FL);
		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
			SNDRV_CHMAP_FR);

883 884 885 886
		sm->max = le32_to_cpu(mc->max);
		sm->min = le32_to_cpu(mc->min);
		sm->invert = le32_to_cpu(mc->invert);
		sm->platform_max = le32_to_cpu(mc->platform_max);
887 888 889 890 891 892
		sm->dobj.index = tplg->index;
		sm->dobj.ops = tplg->ops;
		sm->dobj.type = SND_SOC_DOBJ_MIXER;
		INIT_LIST_HEAD(&sm->dobj.list);

		/* map io handlers */
893
		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
894 895 896
		if (err) {
			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
			kfree(sm);
897
			break;
898 899
		}

900
		/* create any TLV data */
901 902 903 904 905
		err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
				mc->hdr.name);
			kfree(sm);
906
			break;
907
		}
908

909 910 911 912 913 914
		/* pass control to driver for optional further init */
		err = soc_tplg_init_kcontrol(tplg, &kc,
			(struct snd_soc_tplg_ctl_hdr *) mc);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to init %s\n",
				mc->hdr.name);
915
			soc_tplg_free_tlv(tplg, &kc);
916
			kfree(sm);
917
			break;
918 919 920 921 922 923 924 925 926 927
		}

		/* register control here */
		err = soc_tplg_add_kcontrol(tplg, &kc,
			&sm->dobj.control.kcontrol);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to add %s\n",
				mc->hdr.name);
			soc_tplg_free_tlv(tplg, &kc);
			kfree(sm);
928
			break;
929 930 931 932 933
		}

		list_add(&sm->dobj.list, &tplg->comp->dobj_list);
	}

934
	return err;
935 936 937 938 939 940 941 942
}

static int soc_tplg_denum_create_texts(struct soc_enum *se,
	struct snd_soc_tplg_enum_control *ec)
{
	int i, ret;

	se->dobj.control.dtexts =
943
		kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL);
944 945 946
	if (se->dobj.control.dtexts == NULL)
		return -ENOMEM;

947
	for (i = 0; i < le32_to_cpu(ec->items); i++) {
948 949 950 951 952 953 954 955 956 957 958 959 960 961

		if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
			SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
			ret = -EINVAL;
			goto err;
		}

		se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
		if (!se->dobj.control.dtexts[i]) {
			ret = -ENOMEM;
			goto err;
		}
	}

962
	se->items = le32_to_cpu(ec->items);
963
	se->texts = (const char * const *)se->dobj.control.dtexts;
964 965 966
	return 0;

err:
967 968 969 970 971 972 973 974 975
	se->items = i;
	soc_tplg_denum_remove_texts(se);
	return ret;
}

static inline void soc_tplg_denum_remove_texts(struct soc_enum *se)
{
	int i = se->items;

976 977 978 979 980 981 982 983
	for (--i; i >= 0; i--)
		kfree(se->dobj.control.dtexts[i]);
	kfree(se->dobj.control.dtexts);
}

static int soc_tplg_denum_create_values(struct soc_enum *se,
	struct snd_soc_tplg_enum_control *ec)
{
984 985 986
	int i;

	if (le32_to_cpu(ec->items) > sizeof(*ec->values))
987 988
		return -EINVAL;

989 990
	se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) *
					   sizeof(u32),
991
					   GFP_KERNEL);
992 993 994
	if (!se->dobj.control.dvalues)
		return -ENOMEM;

995 996 997 998 999
	/* convert from little-endian */
	for (i = 0; i < le32_to_cpu(ec->items); i++) {
		se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]);
	}

1000 1001 1002
	return 0;
}

1003 1004 1005 1006 1007
static inline void soc_tplg_denum_remove_values(struct soc_enum *se)
{
	kfree(se->dobj.control.dvalues);
}

1008 1009 1010 1011 1012 1013
static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
	size_t size)
{
	struct snd_soc_tplg_enum_control *ec;
	struct soc_enum *se;
	struct snd_kcontrol_new kc;
1014 1015
	int i;
	int err = 0;
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

	if (soc_tplg_check_elem_count(tplg,
		sizeof(struct snd_soc_tplg_enum_control),
		count, size, "enums")) {

		dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
			count);
		return -EINVAL;
	}

	for (i = 0; i < count; i++) {
		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;

		/* validate kcontrol */
		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
			return -EINVAL;

		se = kzalloc((sizeof(*se)), GFP_KERNEL);
		if (se == NULL)
			return -ENOMEM;

1038
		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1039
			      le32_to_cpu(ec->priv.size));
1040

1041 1042 1043 1044 1045 1046 1047
		dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
			ec->hdr.name, ec->items);

		memset(&kc, 0, sizeof(kc));
		kc.name = ec->hdr.name;
		kc.private_value = (long)se;
		kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1048
		kc.access = le32_to_cpu(ec->hdr.access);
1049 1050 1051 1052 1053 1054 1055

		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
			SNDRV_CHMAP_FL);
		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
			SNDRV_CHMAP_FL);

1056
		se->mask = le32_to_cpu(ec->mask);
1057 1058 1059 1060 1061
		se->dobj.index = tplg->index;
		se->dobj.type = SND_SOC_DOBJ_ENUM;
		se->dobj.ops = tplg->ops;
		INIT_LIST_HEAD(&se->dobj.list);

1062
		switch (le32_to_cpu(ec->hdr.ops.info)) {
1063 1064 1065 1066 1067 1068 1069
		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
		case SND_SOC_TPLG_CTL_ENUM_VALUE:
			err = soc_tplg_denum_create_values(se, ec);
			if (err < 0) {
				dev_err(tplg->dev,
					"ASoC: could not create values for %s\n",
					ec->hdr.name);
1070
				goto err_denum;
1071
			}
1072
			/* fall through */
1073 1074 1075 1076 1077 1078 1079 1080
		case SND_SOC_TPLG_CTL_ENUM:
		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
			err = soc_tplg_denum_create_texts(se, ec);
			if (err < 0) {
				dev_err(tplg->dev,
					"ASoC: could not create texts for %s\n",
					ec->hdr.name);
1081
				goto err_denum;
1082 1083 1084
			}
			break;
		default:
1085
			err = -EINVAL;
1086 1087 1088
			dev_err(tplg->dev,
				"ASoC: invalid enum control type %d for %s\n",
				ec->hdr.ops.info, ec->hdr.name);
1089
			goto err_denum;
1090 1091 1092
		}

		/* map io handlers */
1093
		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
1094 1095
		if (err) {
			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
1096
			goto err_denum;
1097 1098 1099 1100 1101 1102 1103 1104
		}

		/* pass control to driver for optional further init */
		err = soc_tplg_init_kcontrol(tplg, &kc,
			(struct snd_soc_tplg_ctl_hdr *) ec);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to init %s\n",
				ec->hdr.name);
1105
			goto err_denum;
1106 1107 1108
		}

		/* register control here */
1109 1110 1111
		err = soc_tplg_add_kcontrol(tplg,
					    &kc, &se->dobj.control.kcontrol);
		if (err < 0) {
1112 1113
			dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
				ec->hdr.name);
1114
			goto err_denum;
1115 1116 1117 1118
		}

		list_add(&se->dobj.list, &tplg->comp->dobj_list);
	}
1119 1120
	return 0;

1121
err_denum:
1122
	kfree(se);
1123
	return err;
1124 1125 1126 1127 1128 1129
}

static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
	struct snd_soc_tplg_hdr *hdr)
{
	struct snd_soc_tplg_ctl_hdr *control_hdr;
1130
	int ret;
1131 1132 1133 1134 1135
	int i;

	dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
		soc_tplg_get_offset(tplg));

1136
	for (i = 0; i < le32_to_cpu(hdr->count); i++) {
1137 1138 1139

		control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;

1140
		if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) {
1141 1142 1143 1144
			dev_err(tplg->dev, "ASoC: invalid control size\n");
			return -EINVAL;
		}

1145
		switch (le32_to_cpu(control_hdr->ops.info)) {
1146 1147 1148 1149 1150 1151 1152
		case SND_SOC_TPLG_CTL_VOLSW:
		case SND_SOC_TPLG_CTL_STROBE:
		case SND_SOC_TPLG_CTL_VOLSW_SX:
		case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
		case SND_SOC_TPLG_CTL_RANGE:
		case SND_SOC_TPLG_DAPM_CTL_VOLSW:
		case SND_SOC_TPLG_DAPM_CTL_PIN:
1153 1154
			ret = soc_tplg_dmixer_create(tplg, 1,
					le32_to_cpu(hdr->payload_size));
1155 1156 1157 1158 1159 1160
			break;
		case SND_SOC_TPLG_CTL_ENUM:
		case SND_SOC_TPLG_CTL_ENUM_VALUE:
		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1161 1162
			ret = soc_tplg_denum_create(tplg, 1,
					le32_to_cpu(hdr->payload_size));
1163 1164
			break;
		case SND_SOC_TPLG_CTL_BYTES:
1165 1166
			ret = soc_tplg_dbytes_create(tplg, 1,
					le32_to_cpu(hdr->payload_size));
1167 1168 1169 1170 1171
			break;
		default:
			soc_bind_err(tplg, control_hdr, i);
			return -EINVAL;
		}
1172 1173 1174 1175 1176
		if (ret < 0) {
			dev_err(tplg->dev, "ASoC: invalid control\n");
			return ret;
		}

1177 1178 1179 1180 1181
	}

	return 0;
}

1182 1183 1184 1185
/* optionally pass new dynamic kcontrol to component driver. */
static int soc_tplg_add_route(struct soc_tplg *tplg,
	struct snd_soc_dapm_route *route)
{
1186
	if (tplg->ops && tplg->ops->dapm_route_load)
1187 1188 1189 1190 1191 1192
		return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
			route);

	return 0;
}

1193 1194 1195 1196 1197
static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
	struct snd_soc_tplg_hdr *hdr)
{
	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
	struct snd_soc_tplg_dapm_graph_elem *elem;
1198
	struct snd_soc_dapm_route **routes;
1199
	int count, i, j;
1200
	int ret = 0;
1201

1202 1203
	count = le32_to_cpu(hdr->count);

1204 1205
	if (soc_tplg_check_elem_count(tplg,
		sizeof(struct snd_soc_tplg_dapm_graph_elem),
1206
		count, le32_to_cpu(hdr->payload_size), "graph")) {
1207 1208 1209 1210 1211 1212

		dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
			count);
		return -EINVAL;
	}

1213 1214
	dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
		hdr->index);
1215

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
	/* allocate memory for pointer to array of dapm routes */
	routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *),
			 GFP_KERNEL);
	if (!routes)
		return -ENOMEM;

	/*
	 * allocate memory for each dapm route in the array.
	 * This needs to be done individually so that
	 * each route can be freed when it is removed in remove_route().
	 */
	for (i = 0; i < count; i++) {
		routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL);
		if (!routes[i]) {
			/* free previously allocated memory */
			for (j = 0; j < i; j++)
				kfree(routes[j]);

			kfree(routes);
			return -ENOMEM;
		}
	}

1239 1240 1241 1242 1243 1244
	for (i = 0; i < count; i++) {
		elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
		tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);

		/* validate routes */
		if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1245 1246 1247 1248
			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
			ret = -EINVAL;
			break;
		}
1249
		if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1250 1251 1252 1253
			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
			ret = -EINVAL;
			break;
		}
1254
		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
1255 1256 1257 1258 1259 1260 1261
			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
			ret = -EINVAL;
			break;
		}

		routes[i]->source = elem->source;
		routes[i]->sink = elem->sink;
1262

1263 1264
		/* set to NULL atm for tplg users */
		routes[i]->connected = NULL;
1265
		if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
1266
			routes[i]->control = NULL;
1267
		else
1268 1269 1270 1271 1272 1273 1274
			routes[i]->control = elem->control;

		/* add route dobj to dobj_list */
		routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH;
		routes[i]->dobj.ops = tplg->ops;
		routes[i]->dobj.index = tplg->index;
		list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list);
1275

1276
		ret = soc_tplg_add_route(tplg, routes[i]);
1277
		if (ret < 0) {
1278
			dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret);
1279 1280 1281 1282 1283 1284 1285
			/*
			 * this route was added to the list, it will
			 * be freed in remove_route() so increment the
			 * counter to skip it in the error handling
			 * below.
			 */
			i++;
1286
			break;
1287
		}
1288

1289
		/* add route, but keep going if some fail */
1290
		snd_soc_dapm_add_routes(dapm, routes[i], 1);
1291 1292
	}

1293 1294 1295 1296 1297 1298 1299 1300
	/*
	 * free memory allocated for all dapm routes not added to the
	 * list in case of error
	 */
	if (ret < 0) {
		while (i < count)
			kfree(routes[i++]);
	}
1301 1302 1303 1304 1305 1306 1307 1308 1309

	/*
	 * free pointer to array of dapm routes as this is no longer needed.
	 * The memory allocated for each dapm route will be freed
	 * when it is removed in remove_route().
	 */
	kfree(routes);

	return ret;
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
}

static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
	struct soc_tplg *tplg, int num_kcontrols)
{
	struct snd_kcontrol_new *kc;
	struct soc_mixer_control *sm;
	struct snd_soc_tplg_mixer_control *mc;
	int i, err;

1320
	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1321 1322 1323 1324 1325 1326 1327 1328 1329
	if (kc == NULL)
		return NULL;

	for (i = 0; i < num_kcontrols; i++) {
		mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;

		/* validate kcontrol */
		if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1330 1331 1332 1333 1334
			goto err_sm;

		sm = kzalloc(sizeof(*sm), GFP_KERNEL);
		if (sm == NULL)
			goto err_sm;
1335

1336
		tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
1337
			      le32_to_cpu(mc->priv.size));
1338

1339 1340 1341
		dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
			mc->hdr.name, i);

1342
		kc[i].private_value = (long)sm;
1343 1344
		kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
		if (kc[i].name == NULL)
1345
			goto err_sm;
1346
		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1347
		kc[i].access = le32_to_cpu(mc->hdr.access);
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

		/* we only support FL/FR channel mapping atm */
		sm->reg = tplc_chan_get_reg(tplg, mc->channel,
			SNDRV_CHMAP_FL);
		sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
			SNDRV_CHMAP_FR);
		sm->shift = tplc_chan_get_shift(tplg, mc->channel,
			SNDRV_CHMAP_FL);
		sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
			SNDRV_CHMAP_FR);

1359 1360 1361 1362
		sm->max = le32_to_cpu(mc->max);
		sm->min = le32_to_cpu(mc->min);
		sm->invert = le32_to_cpu(mc->invert);
		sm->platform_max = le32_to_cpu(mc->platform_max);
1363 1364 1365 1366
		sm->dobj.index = tplg->index;
		INIT_LIST_HEAD(&sm->dobj.list);

		/* map io handlers */
1367
		err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
1368 1369
		if (err) {
			soc_control_err(tplg, &mc->hdr, mc->hdr.name);
1370
			goto err_sm;
1371 1372
		}

1373
		/* create any TLV data */
1374 1375 1376 1377
		err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to create TLV %s\n",
				mc->hdr.name);
1378
			goto err_sm;
1379
		}
1380

1381 1382 1383 1384 1385 1386
		/* pass control to driver for optional further init */
		err = soc_tplg_init_kcontrol(tplg, &kc[i],
			(struct snd_soc_tplg_ctl_hdr *)mc);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to init %s\n",
				mc->hdr.name);
1387
			goto err_sm;
1388 1389 1390 1391
		}
	}
	return kc;

1392 1393
err_sm:
	for (; i >= 0; i--) {
1394
		soc_tplg_free_tlv(tplg, &kc[i]);
1395 1396
		sm = (struct soc_mixer_control *)kc[i].private_value;
		kfree(sm);
1397 1398
		kfree(kc[i].name);
	}
1399
	kfree(kc);
1400

1401 1402 1403 1404
	return NULL;
}

static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
1405
	struct soc_tplg *tplg, int num_kcontrols)
1406 1407 1408 1409
{
	struct snd_kcontrol_new *kc;
	struct snd_soc_tplg_enum_control *ec;
	struct soc_enum *se;
1410
	int i, err;
1411

1412
	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1413 1414 1415
	if (kc == NULL)
		return NULL;

1416 1417 1418 1419 1420
	for (i = 0; i < num_kcontrols; i++) {
		ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
		/* validate kcontrol */
		if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
			    SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1421
			goto err_se;
1422 1423 1424

		se = kzalloc(sizeof(*se), GFP_KERNEL);
		if (se == NULL)
1425
			goto err_se;
1426

1427
		tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
1428
			      le32_to_cpu(ec->priv.size));
1429

1430 1431
		dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
			ec->hdr.name);
1432

1433
		kc[i].private_value = (long)se;
1434
		kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
1435
		if (kc[i].name == NULL)
1436
			goto err_se;
1437
		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1438
		kc[i].access = le32_to_cpu(ec->hdr.access);
1439

1440 1441 1442 1443 1444 1445
		/* we only support FL/FR channel mapping atm */
		se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
		se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
						  SNDRV_CHMAP_FL);
		se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
						  SNDRV_CHMAP_FR);
1446

1447 1448
		se->items = le32_to_cpu(ec->items);
		se->mask = le32_to_cpu(ec->mask);
1449
		se->dobj.index = tplg->index;
1450

1451
		switch (le32_to_cpu(ec->hdr.ops.info)) {
1452 1453 1454 1455 1456 1457 1458 1459
		case SND_SOC_TPLG_CTL_ENUM_VALUE:
		case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
			err = soc_tplg_denum_create_values(se, ec);
			if (err < 0) {
				dev_err(tplg->dev, "ASoC: could not create values for %s\n",
					ec->hdr.name);
				goto err_se;
			}
1460
			/* fall through */
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
		case SND_SOC_TPLG_CTL_ENUM:
		case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
		case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
			err = soc_tplg_denum_create_texts(se, ec);
			if (err < 0) {
				dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
					ec->hdr.name);
				goto err_se;
			}
			break;
		default:
			dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
				ec->hdr.ops.info, ec->hdr.name);
1474 1475
			goto err_se;
		}
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

		/* map io handlers */
		err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
		if (err) {
			soc_control_err(tplg, &ec->hdr, ec->hdr.name);
			goto err_se;
		}

		/* pass control to driver for optional further init */
		err = soc_tplg_init_kcontrol(tplg, &kc[i],
			(struct snd_soc_tplg_ctl_hdr *)ec);
1487
		if (err < 0) {
1488
			dev_err(tplg->dev, "ASoC: failed to init %s\n",
1489 1490 1491 1492 1493 1494 1495 1496
				ec->hdr.name);
			goto err_se;
		}
	}

	return kc;

err_se:
1497 1498 1499
	for (; i >= 0; i--) {
		/* free values and texts */
		se = (struct soc_enum *)kc[i].private_value;
1500

1501 1502 1503 1504
		if (se) {
			soc_tplg_denum_remove_values(se);
			soc_tplg_denum_remove_texts(se);
		}
1505

1506
		kfree(se);
1507
		kfree(kc[i].name);
1508
	}
1509 1510 1511 1512 1513 1514
	kfree(kc);

	return NULL;
}

static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
1515
	struct soc_tplg *tplg, int num_kcontrols)
1516 1517
{
	struct snd_soc_tplg_bytes_control *be;
1518
	struct soc_bytes_ext *sbe;
1519 1520 1521
	struct snd_kcontrol_new *kc;
	int i, err;

1522
	kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
1523 1524 1525
	if (!kc)
		return NULL;

1526
	for (i = 0; i < num_kcontrols; i++) {
1527 1528 1529 1530 1531
		be = (struct snd_soc_tplg_bytes_control *)tplg->pos;

		/* validate kcontrol */
		if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
			SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
1532
			goto err_sbe;
1533 1534 1535

		sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
		if (sbe == NULL)
1536
			goto err_sbe;
1537 1538

		tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
1539
			      le32_to_cpu(be->priv.size));
1540 1541 1542 1543 1544

		dev_dbg(tplg->dev,
			"ASoC: adding bytes kcontrol %s with access 0x%x\n",
			be->hdr.name, be->hdr.access);

1545
		kc[i].private_value = (long)sbe;
1546
		kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
1547 1548
		if (kc[i].name == NULL)
			goto err_sbe;
1549
		kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1550
		kc[i].access = le32_to_cpu(be->hdr.access);
1551

1552
		sbe->max = le32_to_cpu(be->max);
1553 1554 1555
		INIT_LIST_HEAD(&sbe->dobj.list);

		/* map standard io handlers and check for external handlers */
1556
		err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
1557 1558
		if (err) {
			soc_control_err(tplg, &be->hdr, be->hdr.name);
1559
			goto err_sbe;
1560 1561 1562 1563 1564 1565 1566 1567
		}

		/* pass control to driver for optional further init */
		err = soc_tplg_init_kcontrol(tplg, &kc[i],
			(struct snd_soc_tplg_ctl_hdr *)be);
		if (err < 0) {
			dev_err(tplg->dev, "ASoC: failed to init %s\n",
				be->hdr.name);
1568
			goto err_sbe;
1569 1570 1571 1572 1573
		}
	}

	return kc;

1574 1575 1576 1577
err_sbe:
	for (; i >= 0; i--) {
		sbe = (struct soc_bytes_ext *)kc[i].private_value;
		kfree(sbe);
1578 1579
		kfree(kc[i].name);
	}
1580
	kfree(kc);
1581

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
	return NULL;
}

static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
	struct snd_soc_tplg_dapm_widget *w)
{
	struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
	struct snd_soc_dapm_widget template, *widget;
	struct snd_soc_tplg_ctl_hdr *control_hdr;
	struct snd_soc_card *card = tplg->comp->card;
1592
	unsigned int kcontrol_type;
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
	int ret = 0;

	if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
		return -EINVAL;
	if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
		SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
		return -EINVAL;

	dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
		w->name, w->id);

	memset(&template, 0, sizeof(template));

	/* map user to kernel widget ID */
1608
	template.id = get_widget_id(le32_to_cpu(w->id));
1609
	if ((int)template.id < 0)
1610 1611
		return template.id;

1612
	/* strings are allocated here, but used and freed by the widget */
1613 1614 1615 1616 1617 1618 1619 1620
	template.name = kstrdup(w->name, GFP_KERNEL);
	if (!template.name)
		return -ENOMEM;
	template.sname = kstrdup(w->sname, GFP_KERNEL);
	if (!template.sname) {
		ret = -ENOMEM;
		goto err;
	}
1621 1622 1623 1624
	template.reg = le32_to_cpu(w->reg);
	template.shift = le32_to_cpu(w->shift);
	template.mask = le32_to_cpu(w->mask);
	template.subseq = le32_to_cpu(w->subseq);
1625 1626
	template.on_val = w->invert ? 0 : 1;
	template.off_val = w->invert ? 1 : 0;
1627 1628
	template.ignore_suspend = le32_to_cpu(w->ignore_suspend);
	template.event_flags = le16_to_cpu(w->event_flags);
1629 1630 1631
	template.dobj.index = tplg->index;

	tplg->pos +=
1632 1633 1634
		(sizeof(struct snd_soc_tplg_dapm_widget) +
		 le32_to_cpu(w->priv.size));

1635
	if (w->num_kcontrols == 0) {
1636
		kcontrol_type = 0;
1637 1638 1639 1640 1641 1642 1643 1644
		template.num_kcontrols = 0;
		goto widget;
	}

	control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
	dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
		w->name, w->num_kcontrols, control_hdr->type);

1645
	switch (le32_to_cpu(control_hdr->ops.info)) {
1646 1647 1648 1649 1650 1651
	case SND_SOC_TPLG_CTL_VOLSW:
	case SND_SOC_TPLG_CTL_STROBE:
	case SND_SOC_TPLG_CTL_VOLSW_SX:
	case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
	case SND_SOC_TPLG_CTL_RANGE:
	case SND_SOC_TPLG_DAPM_CTL_VOLSW:
1652
		kcontrol_type = SND_SOC_TPLG_TYPE_MIXER;  /* volume mixer */
1653
		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
		template.kcontrol_news =
			soc_tplg_dapm_widget_dmixer_create(tplg,
			template.num_kcontrols);
		if (!template.kcontrol_news) {
			ret = -ENOMEM;
			goto hdr_err;
		}
		break;
	case SND_SOC_TPLG_CTL_ENUM:
	case SND_SOC_TPLG_CTL_ENUM_VALUE:
	case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
	case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
	case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
1667
		kcontrol_type = SND_SOC_TPLG_TYPE_ENUM;	/* enumerated mixer */
1668
		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1669
		template.kcontrol_news =
1670 1671
			soc_tplg_dapm_widget_denum_create(tplg,
			template.num_kcontrols);
1672 1673 1674 1675 1676 1677
		if (!template.kcontrol_news) {
			ret = -ENOMEM;
			goto hdr_err;
		}
		break;
	case SND_SOC_TPLG_CTL_BYTES:
1678
		kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
1679
		template.num_kcontrols = le32_to_cpu(w->num_kcontrols);
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
		template.kcontrol_news =
			soc_tplg_dapm_widget_dbytes_create(tplg,
				template.num_kcontrols);
		if (!template.kcontrol_news) {
			ret = -ENOMEM;
			goto hdr_err;
		}
		break;
	default:
		dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
			control_hdr->ops.get, control_hdr->ops.put,
1691
			le32_to_cpu(control_hdr->ops.info));
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
		ret = -EINVAL;
		goto hdr_err;
	}

widget:
	ret = soc_tplg_widget_load(tplg, &template, w);
	if (ret < 0)
		goto hdr_err;

	/* card dapm mutex is held by the core if we are loading topology
	 * data during sound card init. */
	if (card->instantiated)
		widget = snd_soc_dapm_new_control(dapm, &template);
	else
		widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
1707 1708
	if (IS_ERR(widget)) {
		ret = PTR_ERR(widget);
1709 1710 1711 1712
		goto hdr_err;
	}

	widget->dobj.type = SND_SOC_DOBJ_WIDGET;
1713
	widget->dobj.widget.kcontrol_type = kcontrol_type;
1714 1715 1716
	widget->dobj.ops = tplg->ops;
	widget->dobj.index = tplg->index;
	list_add(&widget->dobj.list, &tplg->comp->dobj_list);
1717 1718 1719 1720 1721

	ret = soc_tplg_widget_ready(tplg, widget, w);
	if (ret < 0)
		goto ready_err;

1722 1723 1724
	kfree(template.sname);
	kfree(template.name);

1725 1726
	return 0;

1727 1728 1729
ready_err:
	snd_soc_tplg_widget_remove(widget);
	snd_soc_dapm_free_widget(widget);
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
hdr_err:
	kfree(template.sname);
err:
	kfree(template.name);
	return ret;
}

static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
	struct snd_soc_tplg_hdr *hdr)
{
	struct snd_soc_tplg_dapm_widget *widget;
1741 1742 1743
	int ret, count, i;

	count = le32_to_cpu(hdr->count);
1744 1745 1746 1747 1748

	dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);

	for (i = 0; i < count; i++) {
		widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
1749
		if (le32_to_cpu(widget->size) != sizeof(*widget)) {
1750 1751 1752 1753
			dev_err(tplg->dev, "ASoC: invalid widget size\n");
			return -EINVAL;
		}

1754
		ret = soc_tplg_dapm_widget_create(tplg, widget);
1755
		if (ret < 0) {
1756 1757
			dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
				widget->name);
1758 1759
			return ret;
		}
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
	}

	return 0;
}

static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
{
	struct snd_soc_card *card = tplg->comp->card;
	int ret;

	/* Card might not have been registered at this point.
	 * If so, just return success.
	*/
	if (!card || !card->instantiated) {
		dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
1775
			" widget card binding deferred\n");
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
		return 0;
	}

	ret = snd_soc_dapm_new_widgets(card);
	if (ret < 0)
		dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
			ret);

	return 0;
}

1787
static int set_stream_info(struct snd_soc_pcm_stream *stream,
1788 1789 1790
	struct snd_soc_tplg_stream_caps *caps)
{
	stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
1791 1792 1793
	if (!stream->stream_name)
		return -ENOMEM;

1794 1795 1796 1797 1798 1799 1800
	stream->channels_min = le32_to_cpu(caps->channels_min);
	stream->channels_max = le32_to_cpu(caps->channels_max);
	stream->rates = le32_to_cpu(caps->rates);
	stream->rate_min = le32_to_cpu(caps->rate_min);
	stream->rate_max = le32_to_cpu(caps->rate_max);
	stream->formats = le64_to_cpu(caps->formats);
	stream->sig_bits = le32_to_cpu(caps->sig_bits);
1801 1802

	return 0;
1803 1804
}

1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
			  unsigned int flag_mask, unsigned int flags)
{
	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
		dai_drv->symmetric_rates =
			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;

	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
		dai_drv->symmetric_channels =
			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
			1 : 0;

	if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
		dai_drv->symmetric_samplebits =
			flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
			1 : 0;
}

1823 1824 1825 1826 1827 1828
static int soc_tplg_dai_create(struct soc_tplg *tplg,
	struct snd_soc_tplg_pcm *pcm)
{
	struct snd_soc_dai_driver *dai_drv;
	struct snd_soc_pcm_stream *stream;
	struct snd_soc_tplg_stream_caps *caps;
1829 1830 1831
	struct snd_soc_dai *dai;
	struct snd_soc_dapm_context *dapm =
		snd_soc_component_get_dapm(tplg->comp);
1832 1833 1834 1835 1836 1837
	int ret;

	dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
	if (dai_drv == NULL)
		return -ENOMEM;

1838
	if (strlen(pcm->dai_name)) {
1839
		dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
1840 1841 1842 1843 1844
		if (!dai_drv->name) {
			ret = -ENOMEM;
			goto err;
		}
	}
1845
	dai_drv->id = le32_to_cpu(pcm->dai_id);
1846 1847 1848 1849

	if (pcm->playback) {
		stream = &dai_drv->playback;
		caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
1850 1851 1852
		ret = set_stream_info(stream, caps);
		if (ret < 0)
			goto err;
1853 1854 1855 1856 1857
	}

	if (pcm->capture) {
		stream = &dai_drv->capture;
		caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
1858 1859 1860
		ret = set_stream_info(stream, caps);
		if (ret < 0)
			goto err;
1861 1862
	}

1863 1864 1865
	if (pcm->compress)
		dai_drv->compress_new = snd_soc_new_compress;

1866
	/* pass control to component driver for optional further init */
1867
	ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
1868 1869
	if (ret < 0) {
		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
1870
		goto err;
1871 1872 1873 1874 1875 1876 1877 1878
	}

	dai_drv->dobj.index = tplg->index;
	dai_drv->dobj.ops = tplg->ops;
	dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
	list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);

	/* register the DAI to the component */
1879
	dai = devm_snd_soc_register_dai(tplg->comp->dev, tplg->comp, dai_drv, false);
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
	if (!dai)
		return -ENOMEM;

	/* Create the DAI widgets here */
	ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
	if (ret != 0) {
		dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret);
		return ret;
	}

1890 1891 1892 1893 1894 1895 1896 1897
	return 0;

err:
	kfree(dai_drv->playback.stream_name);
	kfree(dai_drv->capture.stream_name);
	kfree(dai_drv->name);
	kfree(dai_drv);

1898
	return ret;
1899 1900
}

1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
static void set_link_flags(struct snd_soc_dai_link *link,
		unsigned int flag_mask, unsigned int flags)
{
	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
		link->symmetric_rates =
			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;

	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
		link->symmetric_channels =
			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
			1 : 0;

	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
		link->symmetric_samplebits =
			flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
			1 : 0;
1917 1918 1919 1920 1921

	if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
		link->ignore_suspend =
		flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
		1 : 0;
1922 1923
}

1924
/* create the FE DAI link */
1925
static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
1926 1927 1928
	struct snd_soc_tplg_pcm *pcm)
{
	struct snd_soc_dai_link *link;
1929
	struct snd_soc_dai_link_component *dlc;
1930 1931
	int ret;

1932 1933
	/* link + cpu + codec + platform */
	link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL);
1934 1935 1936
	if (link == NULL)
		return -ENOMEM;

1937 1938 1939 1940
	dlc = (struct snd_soc_dai_link_component *)(link + 1);

	link->cpus	= &dlc[0];
	link->codecs	= &dlc[1];
1941
	link->platforms	= &dlc[2];
1942 1943 1944

	link->num_cpus	 = 1;
	link->num_codecs = 1;
1945
	link->num_platforms = 1;
1946

1947 1948 1949 1950
	link->dobj.index = tplg->index;
	link->dobj.ops = tplg->ops;
	link->dobj.type = SND_SOC_DOBJ_DAI_LINK;

1951 1952 1953
	if (strlen(pcm->pcm_name)) {
		link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
		link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
1954 1955 1956 1957
		if (!link->name || !link->stream_name) {
			ret = -ENOMEM;
			goto err;
		}
1958
	}
1959
	link->id = le32_to_cpu(pcm->pcm_id);
1960

1961
	if (strlen(pcm->dai_name)) {
1962
		link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
1963 1964 1965 1966 1967
		if (!link->cpus->dai_name) {
			ret = -ENOMEM;
			goto err;
		}
	}
1968

1969 1970
	link->codecs->name = "snd-soc-dummy";
	link->codecs->dai_name = "snd-soc-dummy-dai";
1971

1972 1973
	link->platforms->name = "snd-soc-dummy";

1974 1975
	/* enable DPCM */
	link->dynamic = 1;
1976 1977
	link->dpcm_playback = le32_to_cpu(pcm->playback);
	link->dpcm_capture = le32_to_cpu(pcm->capture);
1978
	if (pcm->flag_mask)
1979 1980 1981
		set_link_flags(link,
			       le32_to_cpu(pcm->flag_mask),
			       le32_to_cpu(pcm->flags));
1982

1983
	/* pass control to component driver for optional further init */
1984
	ret = soc_tplg_dai_link_load(tplg, link, NULL);
1985 1986
	if (ret < 0) {
		dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
1987 1988 1989
		goto err;
	}

Mark Brown's avatar
Mark Brown committed
1990
	ret = snd_soc_add_pcm_runtime(tplg->comp->card, link);
1991 1992 1993
	if (ret < 0) {
		dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n");
		goto err;
1994 1995 1996 1997 1998
	}

	list_add(&link->dobj.list, &tplg->comp->dobj_list);

	return 0;
1999 2000 2001 2002 2003 2004
err:
	kfree(link->name);
	kfree(link->stream_name);
	kfree(link->cpus->dai_name);
	kfree(link);
	return ret;
2005 2006 2007
}

/* create a FE DAI and DAI link from the PCM object */
2008 2009 2010
static int soc_tplg_pcm_create(struct soc_tplg *tplg,
	struct snd_soc_tplg_pcm *pcm)
{
2011 2012 2013 2014 2015 2016
	int ret;

	ret = soc_tplg_dai_create(tplg, pcm);
	if (ret < 0)
		return ret;

2017
	return  soc_tplg_fe_link_create(tplg, pcm);
2018 2019
}

2020 2021 2022 2023
/* copy stream caps from the old version 4 of source */
static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
				struct snd_soc_tplg_stream_caps_v4 *src)
{
2024
	dest->size = cpu_to_le32(sizeof(*dest));
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
	memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
	dest->formats = src->formats;
	dest->rates = src->rates;
	dest->rate_min = src->rate_min;
	dest->rate_max = src->rate_max;
	dest->channels_min = src->channels_min;
	dest->channels_max = src->channels_max;
	dest->periods_min = src->periods_min;
	dest->periods_max = src->periods_max;
	dest->period_size_min = src->period_size_min;
	dest->period_size_max = src->period_size_max;
	dest->buffer_size_min = src->buffer_size_min;
	dest->buffer_size_max = src->buffer_size_max;
}

/**
 * pcm_new_ver - Create the new version of PCM from the old version.
 * @tplg: topology context
 * @src: older version of pcm as a source
 * @pcm: latest version of pcm created from the source
 *
 * Support from vesion 4. User should free the returned pcm manually.
 */
static int pcm_new_ver(struct soc_tplg *tplg,
		       struct snd_soc_tplg_pcm *src,
		       struct snd_soc_tplg_pcm **pcm)
{
	struct snd_soc_tplg_pcm *dest;
	struct snd_soc_tplg_pcm_v4 *src_v4;
	int i;

	*pcm = NULL;

2058
	if (le32_to_cpu(src->size) != sizeof(*src_v4)) {
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
		dev_err(tplg->dev, "ASoC: invalid PCM size\n");
		return -EINVAL;
	}

	dev_warn(tplg->dev, "ASoC: old version of PCM\n");
	src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
	if (!dest)
		return -ENOMEM;

2069
	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2070 2071 2072 2073 2074 2075 2076 2077
	memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
	memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
	dest->pcm_id = src_v4->pcm_id;
	dest->dai_id = src_v4->dai_id;
	dest->playback = src_v4->playback;
	dest->capture = src_v4->capture;
	dest->compress = src_v4->compress;
	dest->num_streams = src_v4->num_streams;
2078
	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
		memcpy(&dest->stream[i], &src_v4->stream[i],
		       sizeof(struct snd_soc_tplg_stream));

	for (i = 0; i < 2; i++)
		stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);

	*pcm = dest;
	return 0;
}

2089
static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
2090 2091
	struct snd_soc_tplg_hdr *hdr)
{
2092
	struct snd_soc_tplg_pcm *pcm, *_pcm;
2093 2094
	int count;
	int size;
2095
	int i;
2096
	bool abi_match;
2097
	int ret;
2098

2099 2100
	count = le32_to_cpu(hdr->count);

2101 2102
	/* check the element size and count */
	pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
2103 2104 2105
	size = le32_to_cpu(pcm->size);
	if (size > sizeof(struct snd_soc_tplg_pcm)
		|| size < sizeof(struct snd_soc_tplg_pcm_v4)) {
2106
		dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
2107
			size);
2108 2109 2110
		return -EINVAL;
	}

2111
	if (soc_tplg_check_elem_count(tplg,
2112 2113 2114
				      size, count,
				      le32_to_cpu(hdr->payload_size),
				      "PCM DAI")) {
2115 2116 2117 2118 2119
		dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
			count);
		return -EINVAL;
	}

2120
	for (i = 0; i < count; i++) {
2121
		pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
2122
		size = le32_to_cpu(pcm->size);
2123 2124 2125 2126

		/* check ABI version by size, create a new version of pcm
		 * if abi not match.
		 */
2127
		if (size == sizeof(*pcm)) {
2128 2129 2130 2131
			abi_match = true;
			_pcm = pcm;
		} else {
			abi_match = false;
2132 2133 2134
			ret = pcm_new_ver(tplg, pcm, &_pcm);
			if (ret < 0)
				return ret;
2135 2136
		}

2137
		/* create the FE DAIs and DAI links */
2138 2139 2140 2141 2142 2143
		ret = soc_tplg_pcm_create(tplg, _pcm);
		if (ret < 0) {
			if (!abi_match)
				kfree(_pcm);
			return ret;
		}
2144

2145 2146 2147
		/* offset by version-specific struct size and
		 * real priv data size
		 */
2148
		tplg->pos += size + le32_to_cpu(_pcm->priv.size);
2149

2150 2151
		if (!abi_match)
			kfree(_pcm); /* free the duplicated one */
2152 2153
	}

2154 2155 2156 2157 2158
	dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);

	return 0;
}

2159 2160
/**
 * set_link_hw_format - Set the HW audio format of the physical DAI link.
2161
 * @link: &snd_soc_dai_link which should be updated
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
 * @cfg: physical link configs.
 *
 * Topology context contains a list of supported HW formats (configs) and
 * a default format ID for the physical link. This function will use this
 * default ID to choose the HW format to set the link's DAI format for init.
 */
static void set_link_hw_format(struct snd_soc_dai_link *link,
			struct snd_soc_tplg_link_config *cfg)
{
	struct snd_soc_tplg_hw_config *hw_config;
	unsigned char bclk_master, fsync_master;
	unsigned char invert_bclk, invert_fsync;
	int i;

2176
	for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) {
2177 2178 2179 2180
		hw_config = &cfg->hw_config[i];
		if (hw_config->id != cfg->default_hw_config_id)
			continue;

2181 2182
		link->dai_fmt = le32_to_cpu(hw_config->fmt) &
			SND_SOC_DAIFMT_FORMAT_MASK;
2183

2184
		/* clock gating */
2185 2186
		switch (hw_config->clock_gated) {
		case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
2187
			link->dai_fmt |= SND_SOC_DAIFMT_GATED;
2188 2189 2190
			break;

		case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
2191
			link->dai_fmt |= SND_SOC_DAIFMT_CONT;
2192 2193 2194 2195 2196 2197
			break;

		default:
			/* ignore the value */
			break;
		}
2198

2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
		/* clock signal polarity */
		invert_bclk = hw_config->invert_bclk;
		invert_fsync = hw_config->invert_fsync;
		if (!invert_bclk && !invert_fsync)
			link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
		else if (!invert_bclk && invert_fsync)
			link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
		else if (invert_bclk && !invert_fsync)
			link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
		else
			link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;

		/* clock masters */
2212 2213 2214 2215 2216
		bclk_master = (hw_config->bclk_master ==
			       SND_SOC_TPLG_BCLK_CM);
		fsync_master = (hw_config->fsync_master ==
				SND_SOC_TPLG_FSYNC_CM);
		if (bclk_master && fsync_master)
2217 2218
			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
		else if (!bclk_master && fsync_master)
2219 2220
			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
		else if (bclk_master && !fsync_master)
2221 2222 2223 2224 2225 2226 2227 2228 2229
			link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
		else
			link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
	}
}

/**
 * link_new_ver - Create a new physical link config from the old
 * version of source.
2230
 * @tplg: topology context
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
 * @src: old version of phyical link config as a source
 * @link: latest version of physical link config created from the source
 *
 * Support from vesion 4. User need free the returned link config manually.
 */
static int link_new_ver(struct soc_tplg *tplg,
			struct snd_soc_tplg_link_config *src,
			struct snd_soc_tplg_link_config **link)
{
	struct snd_soc_tplg_link_config *dest;
	struct snd_soc_tplg_link_config_v4 *src_v4;
	int i;

	*link = NULL;

2246 2247
	if (le32_to_cpu(src->size) !=
	    sizeof(struct snd_soc_tplg_link_config_v4)) {
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
		dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
		return -EINVAL;
	}

	dev_warn(tplg->dev, "ASoC: old version of physical link config\n");

	src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
	dest = kzalloc(sizeof(*dest), GFP_KERNEL);
	if (!dest)
		return -ENOMEM;

2259
	dest->size = cpu_to_le32(sizeof(*dest));
2260 2261
	dest->id = src_v4->id;
	dest->num_streams = src_v4->num_streams;
2262
	for (i = 0; i < le32_to_cpu(dest->num_streams); i++)
2263 2264 2265 2266 2267 2268 2269
		memcpy(&dest->stream[i], &src_v4->stream[i],
		       sizeof(struct snd_soc_tplg_stream));

	*link = dest;
	return 0;
}

2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
/**
 * snd_soc_find_dai_link - Find a DAI link
 *
 * @card: soc card
 * @id: DAI link ID to match
 * @name: DAI link name to match, optional
 * @stream_name: DAI link stream name to match, optional
 *
 * This function will search all existing DAI links of the soc card to
 * find the link of the same ID. Since DAI links may not have their
 * unique ID, so name and stream name should also match if being
 * specified.
 *
 * Return: pointer of DAI link, or NULL if not found.
 */
static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card,
						      int id, const char *name,
						      const char *stream_name)
{
	struct snd_soc_pcm_runtime *rtd;
	struct snd_soc_dai_link *link;

	for_each_card_rtds(card, rtd) {
		link = rtd->dai_link;

		if (link->id != id)
			continue;

		if (name && (!link->name || strcmp(name, link->name)))
			continue;

		if (stream_name && (!link->stream_name
				    || strcmp(stream_name, link->stream_name)))
			continue;

		return link;
	}

	return NULL;
}

2311 2312 2313 2314 2315 2316
/* Find and configure an existing physical DAI link */
static int soc_tplg_link_config(struct soc_tplg *tplg,
	struct snd_soc_tplg_link_config *cfg)
{
	struct snd_soc_dai_link *link;
	const char *name, *stream_name;
2317
	size_t len;
2318 2319
	int ret;

2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
	len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
		return -EINVAL;
	else if (len)
		name = cfg->name;
	else
		name = NULL;

	len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
	if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
		return -EINVAL;
	else if (len)
		stream_name = cfg->stream_name;
	else
		stream_name = NULL;
2335

2336
	link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id),
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
				     name, stream_name);
	if (!link) {
		dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
			name, cfg->id);
		return -EINVAL;
	}

	/* hw format */
	if (cfg->num_hw_configs)
		set_link_hw_format(link, cfg);

	/* flags */
	if (cfg->flag_mask)
2350 2351 2352
		set_link_flags(link,
			       le32_to_cpu(cfg->flag_mask),
			       le32_to_cpu(cfg->flags));
2353 2354

	/* pass control to component driver for optional further init */
2355
	ret = soc_tplg_dai_link_load(tplg, link, cfg);
2356 2357 2358 2359 2360
	if (ret < 0) {
		dev_err(tplg->dev, "ASoC: physical link loading failed\n");
		return ret;
	}

2361 2362 2363 2364 2365 2366
	/* for unloading it in snd_soc_tplg_component_remove */
	link->dobj.index = tplg->index;
	link->dobj.ops = tplg->ops;
	link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK;
	list_add(&link->dobj.list, &tplg->comp->dobj_list);

2367 2368 2369 2370 2371 2372 2373 2374 2375
	return 0;
}


/* Load physical link config elements from the topology context */
static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
	struct snd_soc_tplg_hdr *hdr)
{
	struct snd_soc_tplg_link_config *link, *_link;
2376 2377
	int count;
	int size;
2378 2379 2380
	int i, ret;
	bool abi_match;

2381 2382
	count = le32_to_cpu(hdr->count);

2383 2384
	/* check the element size and count */
	link = (struct snd_soc_tplg_link_config *)tplg->pos;
2385 2386 2387
	size = le32_to_cpu(link->size);
	if (size > sizeof(struct snd_soc_tplg_link_config)
		|| size < sizeof(struct snd_soc_tplg_link_config_v4)) {
2388
		dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
2389
			size);
2390 2391 2392 2393
		return -EINVAL;
	}

	if (soc_tplg_check_elem_count(tplg,
2394 2395 2396
				      size, count,
				      le32_to_cpu(hdr->payload_size),
				      "physical link config")) {
2397 2398 2399 2400 2401 2402 2403 2404
		dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
			count);
		return -EINVAL;
	}

	/* config physical DAI links */
	for (i = 0; i < count; i++) {
		link = (struct snd_soc_tplg_link_config *)tplg->pos;
2405 2406
		size = le32_to_cpu(link->size);
		if (size == sizeof(*link)) {
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
			abi_match = true;
			_link = link;
		} else {
			abi_match = false;
			ret = link_new_ver(tplg, link, &_link);
			if (ret < 0)
				return ret;
		}

		ret = soc_tplg_link_config(tplg, _link);
2417 2418 2419
		if (ret < 0) {
			if (!abi_match)
				kfree(_link);
2420
			return ret;
2421
		}
2422 2423 2424 2425

		/* offset by version-specific struct size and
		 * real priv data size
		 */
2426
		tplg->pos += size + le32_to_cpu(_link->priv.size);
2427 2428 2429 2430 2431 2432 2433 2434

		if (!abi_match)
			kfree(_link); /* free the duplicated one */
	}

	return 0;
}

2435 2436
/**
 * soc_tplg_dai_config - Find and configure an existing physical DAI.
2437
 * @tplg: topology context
2438
 * @d: physical DAI configs.
2439
 *
2440 2441
 * The physical dai should already be registered by the platform driver.
 * The platform driver should specify the DAI name and ID for matching.
2442
 */
2443 2444
static int soc_tplg_dai_config(struct soc_tplg *tplg,
			       struct snd_soc_tplg_dai *d)
2445
{
2446
	struct snd_soc_dai_link_component dai_component;
2447 2448 2449 2450 2451 2452
	struct snd_soc_dai *dai;
	struct snd_soc_dai_driver *dai_drv;
	struct snd_soc_pcm_stream *stream;
	struct snd_soc_tplg_stream_caps *caps;
	int ret;

2453 2454
	memset(&dai_component, 0, sizeof(dai_component));

2455
	dai_component.dai_name = d->dai_name;
2456 2457
	dai = snd_soc_find_dai(&dai_component);
	if (!dai) {
2458 2459
		dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
			d->dai_name);
2460 2461 2462
		return -EINVAL;
	}

2463
	if (le32_to_cpu(d->dai_id) != dai->id) {
2464 2465
		dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
			d->dai_name);
2466 2467 2468 2469 2470 2471 2472
		return -EINVAL;
	}

	dai_drv = dai->driver;
	if (!dai_drv)
		return -EINVAL;

2473
	if (d->playback) {
2474
		stream = &dai_drv->playback;
2475
		caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
2476 2477 2478
		ret = set_stream_info(stream, caps);
		if (ret < 0)
			goto err;
2479 2480
	}

2481
	if (d->capture) {
2482
		stream = &dai_drv->capture;
2483
		caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
2484 2485 2486
		ret = set_stream_info(stream, caps);
		if (ret < 0)
			goto err;
2487 2488
	}

2489
	if (d->flag_mask)
2490 2491 2492
		set_dai_flags(dai_drv,
			      le32_to_cpu(d->flag_mask),
			      le32_to_cpu(d->flags));
2493 2494

	/* pass control to component driver for optional further init */
2495
	ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
2496 2497
	if (ret < 0) {
		dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
2498
		goto err;
2499 2500 2501
	}

	return 0;
2502 2503 2504 2505 2506

err:
	kfree(dai_drv->playback.stream_name);
	kfree(dai_drv->capture.stream_name);
	return ret;
2507 2508
}

2509 2510 2511
/* load physical DAI elements */
static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
				   struct snd_soc_tplg_hdr *hdr)
2512
{
2513
	struct snd_soc_tplg_dai *dai;
2514
	int count;
2515
	int i, ret;
2516

2517 2518
	count = le32_to_cpu(hdr->count);

2519 2520
	/* config the existing BE DAIs */
	for (i = 0; i < count; i++) {
2521
		dai = (struct snd_soc_tplg_dai *)tplg->pos;
2522
		if (le32_to_cpu(dai->size) != sizeof(*dai)) {
2523
			dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
2524 2525 2526
			return -EINVAL;
		}

2527 2528 2529 2530 2531 2532
		ret = soc_tplg_dai_config(tplg, dai);
		if (ret < 0) {
			dev_err(tplg->dev, "ASoC: failed to configure DAI\n");
			return ret;
		}

2533
		tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size));
2534 2535 2536 2537 2538 2539
	}

	dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
	return 0;
}

2540 2541 2542
/**
 * manifest_new_ver - Create a new version of manifest from the old version
 * of source.
2543
 * @tplg: topology context
2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
 * @src: old version of manifest as a source
 * @manifest: latest version of manifest created from the source
 *
 * Support from vesion 4. Users need free the returned manifest manually.
 */
static int manifest_new_ver(struct soc_tplg *tplg,
			    struct snd_soc_tplg_manifest *src,
			    struct snd_soc_tplg_manifest **manifest)
{
	struct snd_soc_tplg_manifest *dest;
	struct snd_soc_tplg_manifest_v4 *src_v4;
2555
	int size;
2556 2557 2558

	*manifest = NULL;

2559 2560
	size = le32_to_cpu(src->size);
	if (size != sizeof(*src_v4)) {
2561
		dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
2562 2563
			 size);
		if (size)
2564
			return -EINVAL;
2565
		src->size = cpu_to_le32(sizeof(*src_v4));
2566 2567 2568 2569 2570
	}

	dev_warn(tplg->dev, "ASoC: old version of manifest\n");

	src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
2571 2572
	dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size),
		       GFP_KERNEL);
2573 2574 2575
	if (!dest)
		return -ENOMEM;

2576
	dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */
2577 2578 2579 2580 2581 2582 2583 2584
	dest->control_elems = src_v4->control_elems;
	dest->widget_elems = src_v4->widget_elems;
	dest->graph_elems = src_v4->graph_elems;
	dest->pcm_elems = src_v4->pcm_elems;
	dest->dai_link_elems = src_v4->dai_link_elems;
	dest->priv.size = src_v4->priv.size;
	if (dest->priv.size)
		memcpy(dest->priv.data, src_v4->priv.data,
2585
		       le32_to_cpu(src_v4->priv.size));
2586 2587 2588 2589

	*manifest = dest;
	return 0;
}
2590

2591
static int soc_tplg_manifest_load(struct soc_tplg *tplg,
2592
				  struct snd_soc_tplg_hdr *hdr)
2593
{
2594 2595
	struct snd_soc_tplg_manifest *manifest, *_manifest;
	bool abi_match;
2596
	int ret = 0;
2597 2598

	manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
2599

2600
	/* check ABI version by size, create a new manifest if abi not match */
2601
	if (le32_to_cpu(manifest->size) == sizeof(*manifest)) {
2602 2603 2604 2605
		abi_match = true;
		_manifest = manifest;
	} else {
		abi_match = false;
2606 2607 2608
		ret = manifest_new_ver(tplg, manifest, &_manifest);
		if (ret < 0)
			return ret;
2609
	}
2610

2611
	/* pass control to component driver for optional further init */
2612
	if (tplg->ops && tplg->ops->manifest)
2613
		ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
2614 2615 2616

	if (!abi_match)	/* free the duplicated one */
		kfree(_manifest);
2617

2618
	return ret;
2619 2620 2621 2622 2623 2624 2625 2626 2627
}

/* validate header magic, size and type */
static int soc_valid_header(struct soc_tplg *tplg,
	struct snd_soc_tplg_hdr *hdr)
{
	if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
		return 0;

2628
	if (le32_to_cpu(hdr->size) != sizeof(*hdr)) {
2629 2630
		dev_err(tplg->dev,
			"ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
2631
			le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg),
2632 2633 2634 2635
			tplg->fw->size);
		return -EINVAL;
	}

2636
	/* big endian firmware objects not supported atm */
2637
	if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) {
2638 2639 2640 2641 2642 2643 2644
		dev_err(tplg->dev,
			"ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
			tplg->pass, hdr->magic,
			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
		return -EINVAL;
	}

2645
	if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) {
2646 2647 2648 2649 2650 2651 2652
		dev_err(tplg->dev,
			"ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
			tplg->pass, hdr->magic,
			soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
		return -EINVAL;
	}

2653
	/* Support ABI from version 4 */
2654 2655
	if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION ||
	    le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) {
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
		dev_err(tplg->dev,
			"ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
			tplg->pass, hdr->abi,
			SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
			tplg->fw->size);
		return -EINVAL;
	}

	if (hdr->payload_size == 0) {
		dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
			soc_tplg_get_hdr_offset(tplg));
		return -EINVAL;
	}

	return 1;
}

/* check header type and call appropriate handler */
static int soc_tplg_load_header(struct soc_tplg *tplg,
	struct snd_soc_tplg_hdr *hdr)
{
2677 2678 2679 2680
	int (*elem_load)(struct soc_tplg *tplg,
			 struct snd_soc_tplg_hdr *hdr);
	unsigned int hdr_pass;

2681 2682 2683
	tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);

	/* check for matching ID */
2684
	if (le32_to_cpu(hdr->index) != tplg->req_index &&
2685
		tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
2686 2687
		return 0;

2688
	tplg->index = le32_to_cpu(hdr->index);
2689

2690
	switch (le32_to_cpu(hdr->type)) {
2691 2692 2693
	case SND_SOC_TPLG_TYPE_MIXER:
	case SND_SOC_TPLG_TYPE_ENUM:
	case SND_SOC_TPLG_TYPE_BYTES:
2694 2695 2696
		hdr_pass = SOC_TPLG_PASS_MIXER;
		elem_load = soc_tplg_kcontrol_elems_load;
		break;
2697
	case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
2698 2699 2700
		hdr_pass = SOC_TPLG_PASS_GRAPH;
		elem_load = soc_tplg_dapm_graph_elems_load;
		break;
2701
	case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
2702 2703 2704
		hdr_pass = SOC_TPLG_PASS_WIDGET;
		elem_load = soc_tplg_dapm_widget_elems_load;
		break;
2705
	case SND_SOC_TPLG_TYPE_PCM:
2706 2707 2708
		hdr_pass = SOC_TPLG_PASS_PCM_DAI;
		elem_load = soc_tplg_pcm_elems_load;
		break;
2709
	case SND_SOC_TPLG_TYPE_DAI:
2710 2711 2712
		hdr_pass = SOC_TPLG_PASS_BE_DAI;
		elem_load = soc_tplg_dai_elems_load;
		break;
2713 2714 2715
	case SND_SOC_TPLG_TYPE_DAI_LINK:
	case SND_SOC_TPLG_TYPE_BACKEND_LINK:
		/* physical link configurations */
2716 2717 2718
		hdr_pass = SOC_TPLG_PASS_LINK;
		elem_load = soc_tplg_link_elems_load;
		break;
2719
	case SND_SOC_TPLG_TYPE_MANIFEST:
2720 2721 2722
		hdr_pass = SOC_TPLG_PASS_MANIFEST;
		elem_load = soc_tplg_manifest_load;
		break;
2723 2724
	default:
		/* bespoke vendor data object */
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
		hdr_pass = SOC_TPLG_PASS_VENDOR;
		elem_load = soc_tplg_vendor_load;
		break;
	}

	if (tplg->pass == hdr_pass) {
		dev_dbg(tplg->dev,
			"ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
			hdr->payload_size, hdr->type, hdr->version,
			hdr->vendor_type, tplg->pass);
		return elem_load(tplg, hdr);
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
	}

	return 0;
}

/* process the topology file headers */
static int soc_tplg_process_headers(struct soc_tplg *tplg)
{
	struct snd_soc_tplg_hdr *hdr;
	int ret;

	tplg->pass = SOC_TPLG_PASS_START;

	/* process the header types from start to end */
	while (tplg->pass <= SOC_TPLG_PASS_END) {

		tplg->hdr_pos = tplg->fw->data;
		hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;

		while (!soc_tplg_is_eof(tplg)) {

			/* make sure header is valid before loading */
			ret = soc_valid_header(tplg, hdr);
2759 2760 2761
			if (ret < 0) {
				dev_err(tplg->dev,
					"ASoC: topology: invalid header: %d\n", ret);
2762
				return ret;
2763
			} else if (ret == 0) {
2764
				break;
2765
			}
2766 2767 2768

			/* load the header object */
			ret = soc_tplg_load_header(tplg, hdr);
2769 2770 2771
			if (ret < 0) {
				dev_err(tplg->dev,
					"ASoC: topology: could not load header: %d\n", ret);
2772
				return ret;
2773
			}
2774 2775

			/* goto next header */
2776
			tplg->hdr_pos += le32_to_cpu(hdr->payload_size) +
2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
				sizeof(struct snd_soc_tplg_hdr);
			hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
		}

		/* next data type pass */
		tplg->pass++;
	}

	/* signal DAPM we are complete */
	ret = soc_tplg_dapm_complete(tplg);
	if (ret < 0)
		dev_err(tplg->dev,
			"ASoC: failed to initialise DAPM from Firmware\n");

	return ret;
}

static int soc_tplg_load(struct soc_tplg *tplg)
{
	int ret;

	ret = soc_tplg_process_headers(tplg);
	if (ret == 0)
		soc_tplg_complete(tplg);

	return ret;
}

/* load audio component topology from "firmware" file */
int snd_soc_tplg_component_load(struct snd_soc_component *comp,
	struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
{
	struct soc_tplg tplg;
2810
	int ret;
2811

2812 2813 2814
	/* component needs to exist to keep and reference data while parsing */
	if (!comp)
		return -EINVAL;
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824

	/* setup parsing context */
	memset(&tplg, 0, sizeof(tplg));
	tplg.fw = fw;
	tplg.dev = comp->dev;
	tplg.comp = comp;
	tplg.ops = ops;
	tplg.req_index = id;
	tplg.io_ops = ops->io_ops;
	tplg.io_ops_count = ops->io_ops_count;
2825 2826
	tplg.bytes_ext_ops = ops->bytes_ext_ops;
	tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
2827

2828 2829 2830 2831 2832 2833
	ret = soc_tplg_load(&tplg);
	/* free the created components if fail to load topology */
	if (ret)
		snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL);

	return ret;
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
}
EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);

/* remove this dynamic widget */
void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
{
	/* make sure we are a widget */
	if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
		return;

	remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
}
EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);

/* remove all dynamic widgets from this DAPM context */
void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
	u32 index)
{
	struct snd_soc_dapm_widget *w, *next_w;

2854
	for_each_card_widgets_safe(dapm->card, w, next_w) {
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865

		/* make sure we are a widget with correct context */
		if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
			continue;

		/* match ID */
		if (w->dobj.index != index &&
			w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
			continue;
		/* check and free and dynamic widget kcontrols */
		snd_soc_tplg_widget_remove(w);
2866
		snd_soc_dapm_free_widget(w);
2867
	}
2868
	snd_soc_dapm_reset_cache(dapm);
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886
}
EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);

/* remove dynamic controls from the component driver */
int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
{
	struct snd_soc_dobj *dobj, *next_dobj;
	int pass = SOC_TPLG_PASS_END;

	/* process the header types from end to start */
	while (pass >= SOC_TPLG_PASS_START) {

		/* remove mixer controls */
		list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
			list) {

			/* match index */
			if (dobj->index != index &&
2887
				index != SND_SOC_TPLG_INDEX_ALL)
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
				continue;

			switch (dobj->type) {
			case SND_SOC_DOBJ_MIXER:
				remove_mixer(comp, dobj, pass);
				break;
			case SND_SOC_DOBJ_ENUM:
				remove_enum(comp, dobj, pass);
				break;
			case SND_SOC_DOBJ_BYTES:
				remove_bytes(comp, dobj, pass);
				break;
2900 2901 2902
			case SND_SOC_DOBJ_GRAPH:
				remove_route(comp, dobj, pass);
				break;
2903 2904 2905 2906
			case SND_SOC_DOBJ_WIDGET:
				remove_widget(comp, dobj, pass);
				break;
			case SND_SOC_DOBJ_PCM:
2907
				remove_dai(comp, dobj, pass);
2908
				break;
2909 2910 2911
			case SND_SOC_DOBJ_DAI_LINK:
				remove_link(comp, dobj, pass);
				break;
2912 2913 2914 2915 2916 2917 2918
			case SND_SOC_DOBJ_BACKEND_LINK:
				/*
				 * call link_unload ops if extra
				 * deinitialization is needed.
				 */
				remove_backend_link(comp, dobj, pass);
				break;
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
			default:
				dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
					dobj->type);
				break;
			}
		}
		pass--;
	}

	/* let caller know if FW can be freed when no objects are left */
	return !list_empty(&comp->dobj_list);
}
EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);