Skip to content

Commit 6bbc7fe

Browse files
committed
ALSA: Add a helper to add a new attribute group to card
For assigning sysfs entries for a card device from the driver, introduce a new helper function, snd_card_add_dev_attr(). In this way, we can avoid the possible race between the device registration and the sysfs addition / removal. The driver can pass a new attribute group to add freely. This has to be called before snd_card_register(). Currently, up to two extra groups can be added. More than that, it'll return an error. Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 4227de2 commit 6bbc7fe

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

‎include/sound/core.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct snd_card {
132132
struct completion *release_completion;
133133
struct device *dev; /* device assigned to this card */
134134
struct device card_dev; /* cardX object for sysfs */
135+
const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
135136
bool registered; /* card_dev is registered? */
136137

137138
#ifdef CONFIG_PM
@@ -262,6 +263,8 @@ void snd_card_set_id(struct snd_card *card, const char *id);
262263
int snd_card_register(struct snd_card *card);
263264
int snd_card_info_init(void);
264265
int snd_card_info_done(void);
266+
int snd_card_add_dev_attr(struct snd_card *card,
267+
const struct attribute_group *group);
265268
int snd_component_add(struct snd_card *card, const char *component);
266269
int snd_card_file_add(struct snd_card *card, struct file *file);
267270
int snd_card_file_remove(struct snd_card *card, struct file *file);

‎sound/core/init.c‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void snd_device_initialize(struct device *dev, struct snd_card *card)
181181
EXPORT_SYMBOL_GPL(snd_device_initialize);
182182

183183
static int snd_card_do_free(struct snd_card *card);
184-
static const struct attribute_group *card_dev_attr_groups[];
184+
static const struct attribute_group card_dev_attr_group;
185185

186186
static void release_card_device(struct device *dev)
187187
{
@@ -269,7 +269,8 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
269269
card->card_dev.parent = parent;
270270
card->card_dev.class = sound_class;
271271
card->card_dev.release = release_card_device;
272-
card->card_dev.groups = card_dev_attr_groups;
272+
card->card_dev.groups = card->dev_groups;
273+
card->dev_groups[0] = &card_dev_attr_group;
273274
err = kobject_set_name(&card->card_dev.kobj, "card%d", idx);
274275
if (err < 0)
275276
goto __error;
@@ -700,14 +701,32 @@ static struct attribute *card_dev_attrs[] = {
700701
NULL
701702
};
702703

703-
static struct attribute_group card_dev_attr_group = {
704+
static const struct attribute_group card_dev_attr_group = {
704705
.attrs = card_dev_attrs,
705706
};
706707

707-
static const struct attribute_group *card_dev_attr_groups[] = {
708-
&card_dev_attr_group,
709-
NULL
708+
/**
709+
* snd_card_add_dev_attr - Append a new sysfs attribute group to card
710+
* @card: card instance
711+
* @group: attribute group to append
712+
*/
713+
int snd_card_add_dev_attr(struct snd_card *card,
714+
const struct attribute_group *group)
715+
{
716+
int i;
717+
718+
/* loop for (arraysize-1) here to keep NULL at the last entry */
719+
for (i = 0; i < ARRAY_SIZE(card->dev_groups) - 1; i++) {
720+
if (!card->dev_groups[i]) {
721+
card->dev_groups[i] = group;
722+
return 0;
723+
}
724+
}
725+
726+
dev_err(card->dev, "Too many groups assigned\n");
727+
return -ENOSPC;
710728
};
729+
EXPORT_SYMBOL_GPL(snd_card_add_dev_attr);
711730

712731
/**
713732
* snd_card_register - register the soundcard

0 commit comments

Comments
 (0)