1
0
Fork 0

ALSA: control: Consolidate helpers for adding and replacing ctl elements

Both snd_ctl_add() and snd_ctl_replace() process the things in a
fairly similar way, and indeed the most of the codes can be unified.

This patch is a refactoring to consolidate the both functions to call
a single helper with an extra "mode" argument.  There should be no
functional difference, except for one additional sanity check applied
now to snd_ctl_replace() (which was rather overlooking, IMO), too.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
hifive-unleashed-5.1
Takashi Iwai 2018-11-22 15:22:40 +01:00
parent 5f2ad5942f
commit 3103c08f96
1 changed files with 53 additions and 72 deletions

View File

@ -348,24 +348,43 @@ static int snd_ctl_find_hole(struct snd_card *card, unsigned int count)
return 0; return 0;
} }
/* add a new kcontrol object; call with card->controls_rwsem locked */ enum snd_ctl_add_mode {
static int __snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol) CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE,
};
/* add/replace a new kcontrol object; call with card->controls_rwsem locked */
static int __snd_ctl_add_replace(struct snd_card *card,
struct snd_kcontrol *kcontrol,
enum snd_ctl_add_mode mode)
{ {
struct snd_ctl_elem_id id; struct snd_ctl_elem_id id;
unsigned int idx; unsigned int idx;
unsigned int count; unsigned int count;
struct snd_kcontrol *old;
int err;
id = kcontrol->id; id = kcontrol->id;
if (id.index > UINT_MAX - kcontrol->count) if (id.index > UINT_MAX - kcontrol->count)
return -EINVAL; return -EINVAL;
if (snd_ctl_find_id(card, &id)) { old = snd_ctl_find_id(card, &id);
if (!old) {
if (mode == CTL_REPLACE)
return -EINVAL;
} else {
if (mode == CTL_ADD_EXCLUSIVE) {
dev_err(card->dev, dev_err(card->dev,
"control %i:%i:%i:%s:%i is already present\n", "control %i:%i:%i:%s:%i is already present\n",
id.iface, id.device, id.subdevice, id.name, id.index); id.iface, id.device, id.subdevice, id.name,
id.index);
return -EBUSY; return -EBUSY;
} }
err = snd_ctl_remove(card, old);
if (err < 0)
return err;
}
if (snd_ctl_find_hole(card, kcontrol->count) < 0) if (snd_ctl_find_hole(card, kcontrol->count) < 0)
return -ENOMEM; return -ENOMEM;
@ -382,6 +401,29 @@ static int __snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
return 0; return 0;
} }
static int snd_ctl_add_replace(struct snd_card *card,
struct snd_kcontrol *kcontrol,
enum snd_ctl_add_mode mode)
{
int err = -EINVAL;
if (! kcontrol)
return err;
if (snd_BUG_ON(!card || !kcontrol->info))
goto error;
down_write(&card->controls_rwsem);
err = __snd_ctl_add_replace(card, kcontrol, mode);
up_write(&card->controls_rwsem);
if (err < 0)
goto error;
return 0;
error:
snd_ctl_free_one(kcontrol);
return err;
}
/** /**
* snd_ctl_add - add the control instance to the card * snd_ctl_add - add the control instance to the card
* @card: the card instance * @card: the card instance
@ -398,23 +440,7 @@ static int __snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
*/ */
int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol) int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
{ {
int err = -EINVAL; return snd_ctl_add_replace(card, kcontrol, CTL_ADD_EXCLUSIVE);
if (! kcontrol)
return err;
if (snd_BUG_ON(!card || !kcontrol->info))
goto error;
down_write(&card->controls_rwsem);
err = __snd_ctl_add(card, kcontrol);
up_write(&card->controls_rwsem);
if (err < 0)
goto error;
return 0;
error:
snd_ctl_free_one(kcontrol);
return err;
} }
EXPORT_SYMBOL(snd_ctl_add); EXPORT_SYMBOL(snd_ctl_add);
@ -435,53 +461,8 @@ EXPORT_SYMBOL(snd_ctl_add);
int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol, int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
bool add_on_replace) bool add_on_replace)
{ {
struct snd_ctl_elem_id id; return snd_ctl_add_replace(card, kcontrol,
unsigned int count; add_on_replace ? CTL_ADD_ON_REPLACE : CTL_REPLACE);
unsigned int idx;
struct snd_kcontrol *old;
int ret;
if (!kcontrol)
return -EINVAL;
if (snd_BUG_ON(!card || !kcontrol->info)) {
ret = -EINVAL;
goto error;
}
id = kcontrol->id;
down_write(&card->controls_rwsem);
old = snd_ctl_find_id(card, &id);
if (!old) {
if (add_on_replace)
goto add;
up_write(&card->controls_rwsem);
ret = -EINVAL;
goto error;
}
ret = snd_ctl_remove(card, old);
if (ret < 0) {
up_write(&card->controls_rwsem);
goto error;
}
add:
if (snd_ctl_find_hole(card, kcontrol->count) < 0) {
up_write(&card->controls_rwsem);
ret = -ENOMEM;
goto error;
}
list_add_tail(&kcontrol->list, &card->controls);
card->controls_count += kcontrol->count;
kcontrol->id.numid = card->last_numid + 1;
card->last_numid += kcontrol->count;
id = kcontrol->id;
count = kcontrol->count;
up_write(&card->controls_rwsem);
for (idx = 0; idx < count; idx++, id.index++, id.numid++)
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
return 0;
error:
snd_ctl_free_one(kcontrol);
return ret;
} }
EXPORT_SYMBOL(snd_ctl_replace); EXPORT_SYMBOL(snd_ctl_replace);
@ -1369,7 +1350,7 @@ static int snd_ctl_elem_add(struct snd_ctl_file *file,
/* This function manage to free the instance on failure. */ /* This function manage to free the instance on failure. */
down_write(&card->controls_rwsem); down_write(&card->controls_rwsem);
err = __snd_ctl_add(card, kctl); err = __snd_ctl_add_replace(card, kctl, CTL_ADD_EXCLUSIVE);
if (err < 0) { if (err < 0) {
snd_ctl_free_one(kctl); snd_ctl_free_one(kctl);
goto unlock; goto unlock;