[alsa-devel] [PATCH 1/2] 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 --- include/sound/core.h | 3 +++ sound/core/init.c | 31 +++++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/include/sound/core.h b/include/sound/core.h index 58882bfacdd7..da5748289968 100644 --- a/include/sound/core.h +++ b/include/sound/core.h @@ -132,6 +132,7 @@ struct snd_card { struct completion *release_completion; struct device *dev; /* device assigned to this card */ struct device card_dev; /* cardX object for sysfs */ + const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */ bool registered; /* card_dev is registered? */
#ifdef CONFIG_PM @@ -262,6 +263,8 @@ void snd_card_set_id(struct snd_card *card, const char *id); int snd_card_register(struct snd_card *card); int snd_card_info_init(void); int snd_card_info_done(void); +int snd_card_add_dev_attr(struct snd_card *card, + const struct attribute_group *group); int snd_component_add(struct snd_card *card, const char *component); int snd_card_file_add(struct snd_card *card, struct file *file); int snd_card_file_remove(struct snd_card *card, struct file *file); diff --git a/sound/core/init.c b/sound/core/init.c index 96194599e82e..35419054821c 100644 --- a/sound/core/init.c +++ b/sound/core/init.c @@ -181,7 +181,7 @@ void snd_device_initialize(struct device *dev, struct snd_card *card) EXPORT_SYMBOL_GPL(snd_device_initialize);
static int snd_card_do_free(struct snd_card *card); -static const struct attribute_group *card_dev_attr_groups[]; +static const struct attribute_group card_dev_attr_group;
static void release_card_device(struct device *dev) { @@ -269,7 +269,8 @@ int snd_card_new(struct device *parent, int idx, const char *xid, card->card_dev.parent = parent; card->card_dev.class = sound_class; card->card_dev.release = release_card_device; - card->card_dev.groups = card_dev_attr_groups; + card->card_dev.groups = card->dev_groups; + card->dev_groups[0] = &card_dev_attr_group; err = kobject_set_name(&card->card_dev.kobj, "card%d", idx); if (err < 0) goto __error; @@ -700,14 +701,32 @@ static struct attribute *card_dev_attrs[] = { NULL };
-static struct attribute_group card_dev_attr_group = { +static const struct attribute_group card_dev_attr_group = { .attrs = card_dev_attrs, };
-static const struct attribute_group *card_dev_attr_groups[] = { - &card_dev_attr_group, - NULL +/** + * snd_card_add_dev_attr - Append a new sysfs attribute group to card + * @card: card instance + * @group: attribute group to append + */ +int snd_card_add_dev_attr(struct snd_card *card, + const struct attribute_group *group) +{ + int i; + + /* loop for (arraysize-1) here to keep NULL at the last entry */ + for (i = 0; i < ARRAY_SIZE(card->dev_groups) - 1; i++) { + if (!card->dev_groups[i]) { + card->dev_groups[i] = group; + return 0; + } + } + + dev_err(card->dev, "Too many groups assigned\n"); + return -ENOSPC; }; +EXPORT_SYMBOL_GPL(snd_card_add_dev_attr);
/** * snd_card_register - register the soundcard
Use the new helper function to create sysfs entries in the card more gracefully without races.
Signed-off-by: Takashi Iwai tiwai@suse.de --- sound/usb/line6/pod.c | 38 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 25 deletions(-)
diff --git a/sound/usb/line6/pod.c b/sound/usb/line6/pod.c index 61aadd7d4b7f..eb5a23ceac1c 100644 --- a/sound/usb/line6/pod.c +++ b/sound/usb/line6/pod.c @@ -330,6 +330,18 @@ static DEVICE_ATTR_RO(device_id); static DEVICE_ATTR_RO(firmware_version); static DEVICE_ATTR_RO(serial_number);
+static struct attribute *pod_dev_attrs[] = { + &dev_attr_device_id.attr, + &dev_attr_firmware_version.attr, + &dev_attr_serial_number.attr, + NULL +}; + +static const struct attribute_group pod_dev_attr_group = { + .name = "pod", + .attrs = pod_dev_attrs, +}; + /* control info callback */ static int snd_pod_control_monitor_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) @@ -387,35 +399,11 @@ static void line6_pod_disconnect(struct usb_line6 *line6) struct usb_line6_pod *pod = (struct usb_line6_pod *)line6; struct device *dev = line6->ifcdev;
- /* remove sysfs entries: */ - device_remove_file(dev, &dev_attr_device_id); - device_remove_file(dev, &dev_attr_firmware_version); - device_remove_file(dev, &dev_attr_serial_number); - del_timer_sync(&pod->startup_timer); cancel_work_sync(&pod->startup_work); }
/* - Create sysfs entries. -*/ -static int pod_create_files2(struct device *dev) -{ - int err; - - err = device_create_file(dev, &dev_attr_device_id); - if (err < 0) - return err; - err = device_create_file(dev, &dev_attr_firmware_version); - if (err < 0) - return err; - err = device_create_file(dev, &dev_attr_serial_number); - if (err < 0) - return err; - return 0; -} - -/* Try to init POD device. */ static int pod_init(struct usb_line6 *line6, @@ -431,7 +419,7 @@ static int pod_init(struct usb_line6 *line6, INIT_WORK(&pod->startup_work, pod_startup4);
/* create sysfs entries: */ - err = pod_create_files2(line6->ifcdev); + err = snd_card_add_dev_attr(line6->card, pod_dev_attrs); if (err < 0) return err;
participants (1)
-
Takashi Iwai