Commit 9f90af3a authored by Amadeusz Sławiński's avatar Amadeusz Sławiński Committed by Mark Brown

ASoC: topology: Consolidate and fix asoc_tplg_dapm_widget_*_create flow

There are a few soc_tplg_dapm_widget_*_create functions with similar
content, but slightly different flow, unify their flow and make sure
that we go to error handler and free memory in case of failure.
Signed-off-by: default avatarAmadeusz Sławiński <amadeuszx.slawinski@linux.intel.com>
Acked-by: default avatarRanjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: default avatarPierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Signed-off-by: default avatarMark Brown <broonie@kernel.org>
parent 3cde818c
...@@ -1310,14 +1310,15 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( ...@@ -1310,14 +1310,15 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
for (i = 0; i < num_kcontrols; i++) { for (i = 0; i < num_kcontrols; i++) {
mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
sm = kzalloc(sizeof(*sm), GFP_KERNEL);
if (sm == NULL)
goto err;
/* validate kcontrol */ /* validate kcontrol */
if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
SNDRV_CTL_ELEM_ID_NAME_MAXLEN) SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
goto err_str; goto err_sm;
sm = kzalloc(sizeof(*sm), GFP_KERNEL);
if (sm == NULL)
goto err_sm;
tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
le32_to_cpu(mc->priv.size)); le32_to_cpu(mc->priv.size));
...@@ -1327,7 +1328,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( ...@@ -1327,7 +1328,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL); kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
if (kc[i].name == NULL) if (kc[i].name == NULL)
goto err_str; goto err_sm;
kc[i].private_value = (long)sm; kc[i].private_value = (long)sm;
kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
kc[i].access = mc->hdr.access; kc[i].access = mc->hdr.access;
...@@ -1353,8 +1354,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( ...@@ -1353,8 +1354,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg); err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
if (err) { if (err) {
soc_control_err(tplg, &mc->hdr, mc->hdr.name); soc_control_err(tplg, &mc->hdr, mc->hdr.name);
kfree(sm); goto err_sm;
continue;
} }
/* create any TLV data */ /* create any TLV data */
...@@ -1367,20 +1367,19 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( ...@@ -1367,20 +1367,19 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
dev_err(tplg->dev, "ASoC: failed to init %s\n", dev_err(tplg->dev, "ASoC: failed to init %s\n",
mc->hdr.name); mc->hdr.name);
soc_tplg_free_tlv(tplg, &kc[i]); soc_tplg_free_tlv(tplg, &kc[i]);
kfree(sm); goto err_sm;
continue;
} }
} }
return kc; return kc;
err_str: err_sm:
kfree(sm); for (; i >= 0; i--) {
err: sm = (struct soc_mixer_control *)kc[i].private_value;
for (--i; i >= 0; i--) { kfree(sm);
kfree((void *)kc[i].private_value);
kfree(kc[i].name); kfree(kc[i].name);
} }
kfree(kc); kfree(kc);
return NULL; return NULL;
} }
...@@ -1401,11 +1400,11 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( ...@@ -1401,11 +1400,11 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
/* validate kcontrol */ /* validate kcontrol */
if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
SNDRV_CTL_ELEM_ID_NAME_MAXLEN) SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
goto err; goto err_se;
se = kzalloc(sizeof(*se), GFP_KERNEL); se = kzalloc(sizeof(*se), GFP_KERNEL);
if (se == NULL) if (se == NULL)
goto err; goto err_se;
tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
ec->priv.size); ec->priv.size);
...@@ -1414,10 +1413,8 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( ...@@ -1414,10 +1413,8 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
ec->hdr.name); ec->hdr.name);
kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL); kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
if (kc[i].name == NULL) { if (kc[i].name == NULL)
kfree(se);
goto err_se; goto err_se;
}
kc[i].private_value = (long)se; kc[i].private_value = (long)se;
kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
kc[i].access = ec->hdr.access; kc[i].access = ec->hdr.access;
...@@ -1482,44 +1479,43 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( ...@@ -1482,44 +1479,43 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
for (; i >= 0; i--) { for (; i >= 0; i--) {
/* free values and texts */ /* free values and texts */
se = (struct soc_enum *)kc[i].private_value; se = (struct soc_enum *)kc[i].private_value;
if (!se)
continue;
soc_tplg_denum_remove_values(se); if (se) {
soc_tplg_denum_remove_texts(se); soc_tplg_denum_remove_values(se);
soc_tplg_denum_remove_texts(se);
}
kfree(se); kfree(se);
kfree(kc[i].name); kfree(kc[i].name);
} }
err:
kfree(kc); kfree(kc);
return NULL; return NULL;
} }
static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
struct soc_tplg *tplg, int count) struct soc_tplg *tplg, int num_kcontrols)
{ {
struct snd_soc_tplg_bytes_control *be; struct snd_soc_tplg_bytes_control *be;
struct soc_bytes_ext *sbe; struct soc_bytes_ext *sbe;
struct snd_kcontrol_new *kc; struct snd_kcontrol_new *kc;
int i, err; int i, err;
kc = kcalloc(count, sizeof(*kc), GFP_KERNEL); kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
if (!kc) if (!kc)
return NULL; return NULL;
for (i = 0; i < count; i++) { for (i = 0; i < num_kcontrols; i++) {
be = (struct snd_soc_tplg_bytes_control *)tplg->pos; be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
/* validate kcontrol */ /* validate kcontrol */
if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
SNDRV_CTL_ELEM_ID_NAME_MAXLEN) SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
goto err; goto err_sbe;
sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
if (sbe == NULL) if (sbe == NULL)
goto err; goto err_sbe;
tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
le32_to_cpu(be->priv.size)); le32_to_cpu(be->priv.size));
...@@ -1529,10 +1525,8 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( ...@@ -1529,10 +1525,8 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
be->hdr.name, be->hdr.access); be->hdr.name, be->hdr.access);
kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL); kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
if (kc[i].name == NULL) { if (kc[i].name == NULL)
kfree(sbe); goto err_sbe;
goto err;
}
kc[i].private_value = (long)sbe; kc[i].private_value = (long)sbe;
kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
kc[i].access = be->hdr.access; kc[i].access = be->hdr.access;
...@@ -1544,8 +1538,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( ...@@ -1544,8 +1538,7 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg); err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
if (err) { if (err) {
soc_control_err(tplg, &be->hdr, be->hdr.name); soc_control_err(tplg, &be->hdr, be->hdr.name);
kfree(sbe); goto err_sbe;
continue;
} }
/* pass control to driver for optional further init */ /* pass control to driver for optional further init */
...@@ -1554,20 +1547,20 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( ...@@ -1554,20 +1547,20 @@ static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
if (err < 0) { if (err < 0) {
dev_err(tplg->dev, "ASoC: failed to init %s\n", dev_err(tplg->dev, "ASoC: failed to init %s\n",
be->hdr.name); be->hdr.name);
kfree(sbe); goto err_sbe;
continue;
} }
} }
return kc; return kc;
err: err_sbe:
for (--i; i >= 0; i--) { for (; i >= 0; i--) {
kfree((void *)kc[i].private_value); sbe = (struct soc_bytes_ext *)kc[i].private_value;
kfree(sbe);
kfree(kc[i].name); kfree(kc[i].name);
} }
kfree(kc); kfree(kc);
return NULL; return NULL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment