Skip to content

Commit caa751b

Browse files
committed
ALSA: Create sysfs attribute files via groups
Instead of calling each time device_create_file(), create the groups of sysfs attribute files at once in a normal way. Add a new helper function, snd_get_device(), to return the associated device object, so that we can handle the sysfs addition locally. Since the sysfs file addition is done differently now, snd_add_device_sysfs_file() helper function is removed. Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent d01a838 commit caa751b

8 files changed

Lines changed: 112 additions & 103 deletions

File tree

‎include/sound/core.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ static inline int snd_register_device(int type, struct snd_card *card, int dev,
248248

249249
int snd_unregister_device(int type, struct snd_card *card, int dev);
250250
void *snd_lookup_minor_data(unsigned int minor, int type);
251-
int snd_add_device_sysfs_file(int type, struct snd_card *card, int dev,
252-
struct device_attribute *attr);
251+
struct device *snd_get_device(int type, struct snd_card *card, int dev);
253252

254253
#ifdef CONFIG_SND_OSSEMUL
255254
int snd_register_oss_device(int type, struct snd_card *card, int dev,

‎include/sound/hwdep.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct snd_hwdep {
6868
wait_queue_head_t open_wait;
6969
void *private_data;
7070
void (*private_free) (struct snd_hwdep *hwdep);
71+
const struct attribute_group **groups;
7172

7273
struct mutex open_mutex;
7374
int used; /* reference counter */

‎sound/core/hwdep.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,20 @@ static int snd_hwdep_dev_register(struct snd_device *device)
436436
mutex_unlock(&register_mutex);
437437
return err;
438438
}
439+
440+
if (hwdep->groups) {
441+
struct device *d = snd_get_device(SNDRV_DEVICE_TYPE_HWDEP,
442+
hwdep->card, hwdep->device);
443+
if (d) {
444+
err = sysfs_create_groups(&d->kobj, hwdep->groups);
445+
if (err < 0)
446+
dev_warn(card->dev,
447+
"hwdep %d:%d: cannot create sysfs groups\n",
448+
card->number, hwdep->device);
449+
put_device(d);
450+
}
451+
}
452+
439453
#ifdef CONFIG_SND_OSSEMUL
440454
hwdep->ossreg = 0;
441455
if (hwdep->oss_type >= 0) {

‎sound/core/pcm.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,20 @@ static ssize_t show_pcm_class(struct device *dev,
10181018
return snprintf(buf, PAGE_SIZE, "%s\n", str);
10191019
}
10201020

1021-
static struct device_attribute pcm_attrs =
1022-
__ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
1021+
static DEVICE_ATTR(pcm_class, S_IRUGO, show_pcm_class, NULL);
1022+
static struct attribute *pcm_dev_attrs[] = {
1023+
&dev_attr_pcm_class.attr,
1024+
NULL
1025+
};
1026+
1027+
static struct attribute_group pcm_dev_attr_group = {
1028+
.attrs = pcm_dev_attrs,
1029+
};
1030+
1031+
static const struct attribute_group *pcm_dev_attr_groups[] = {
1032+
&pcm_dev_attr_group,
1033+
NULL
1034+
};
10231035

10241036
static int snd_pcm_dev_register(struct snd_device *device)
10251037
{
@@ -1069,8 +1081,18 @@ static int snd_pcm_dev_register(struct snd_device *device)
10691081
mutex_unlock(&register_mutex);
10701082
return err;
10711083
}
1072-
snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
1073-
&pcm_attrs);
1084+
1085+
dev = snd_get_device(devtype, pcm->card, pcm->device);
1086+
if (dev) {
1087+
err = sysfs_create_groups(&dev->kobj,
1088+
pcm_dev_attr_groups);
1089+
if (err < 0)
1090+
dev_warn(dev,
1091+
"pcm %d:%d: cannot create sysfs groups\n",
1092+
pcm->card->number, pcm->device);
1093+
put_device(dev);
1094+
}
1095+
10741096
for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
10751097
snd_pcm_timer_init(substream);
10761098
}

‎sound/core/sound.c‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,22 +355,25 @@ int snd_unregister_device(int type, struct snd_card *card, int dev)
355355

356356
EXPORT_SYMBOL(snd_unregister_device);
357357

358-
int snd_add_device_sysfs_file(int type, struct snd_card *card, int dev,
359-
struct device_attribute *attr)
358+
/* get the assigned device to the given type and device number;
359+
* the caller needs to release it via put_device() after using it
360+
*/
361+
struct device *snd_get_device(int type, struct snd_card *card, int dev)
360362
{
361-
int minor, ret = -EINVAL;
362-
struct device *d;
363+
int minor;
364+
struct device *d = NULL;
363365

364366
mutex_lock(&sound_mutex);
365367
minor = find_snd_minor(type, card, dev);
366-
if (minor >= 0 && (d = snd_minors[minor]->dev) != NULL)
367-
ret = device_create_file(d, attr);
368+
if (minor >= 0) {
369+
d = snd_minors[minor]->dev;
370+
if (d)
371+
get_device(d);
372+
}
368373
mutex_unlock(&sound_mutex);
369-
return ret;
370-
374+
return d;
371375
}
372-
373-
EXPORT_SYMBOL(snd_add_device_sysfs_file);
376+
EXPORT_SYMBOL(snd_get_device);
374377

375378
#ifdef CONFIG_PROC_FS
376379
/*

‎sound/pci/hda/hda_codec.c‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -852,21 +852,6 @@ static int snd_hda_bus_dev_free(struct snd_device *device)
852852
return snd_hda_bus_free(bus);
853853
}
854854

855-
#ifdef CONFIG_SND_HDA_HWDEP
856-
static int snd_hda_bus_dev_register(struct snd_device *device)
857-
{
858-
struct hda_bus *bus = device->device_data;
859-
struct hda_codec *codec;
860-
list_for_each_entry(codec, &bus->codec_list, list) {
861-
snd_hda_hwdep_add_sysfs(codec);
862-
snd_hda_hwdep_add_power_sysfs(codec);
863-
}
864-
return 0;
865-
}
866-
#else
867-
#define snd_hda_bus_dev_register NULL
868-
#endif
869-
870855
/**
871856
* snd_hda_bus_new - create a HDA bus
872857
* @card: the card entry
@@ -882,7 +867,6 @@ int snd_hda_bus_new(struct snd_card *card,
882867
struct hda_bus *bus;
883868
int err;
884869
static struct snd_device_ops dev_ops = {
885-
.dev_register = snd_hda_bus_dev_register,
886870
.dev_free = snd_hda_bus_dev_free,
887871
};
888872

‎sound/pci/hda/hda_hwdep.c‎

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ static void hwdep_free(struct snd_hwdep *hwdep)
124124
clear_hwdep_elements(hwdep->private_data);
125125
}
126126

127+
static const struct attribute_group *snd_hda_dev_attr_groups[];
128+
127129
int snd_hda_create_hwdep(struct hda_codec *codec)
128130
{
129131
char hwname[16];
@@ -140,6 +142,7 @@ int snd_hda_create_hwdep(struct hda_codec *codec)
140142
hwdep->private_data = codec;
141143
hwdep->private_free = hwdep_free;
142144
hwdep->exclusive = 1;
145+
hwdep->groups = snd_hda_dev_attr_groups;
143146

144147
hwdep->ops.open = hda_hwdep_open;
145148
hwdep->ops.ioctl = hda_hwdep_ioctl;
@@ -176,21 +179,8 @@ static ssize_t power_off_acct_show(struct device *dev,
176179
return sprintf(buf, "%u\n", jiffies_to_msecs(codec->power_off_acct));
177180
}
178181

179-
static struct device_attribute power_attrs[] = {
180-
__ATTR_RO(power_on_acct),
181-
__ATTR_RO(power_off_acct),
182-
};
183-
184-
int snd_hda_hwdep_add_power_sysfs(struct hda_codec *codec)
185-
{
186-
struct snd_hwdep *hwdep = codec->hwdep;
187-
int i;
188-
189-
for (i = 0; i < ARRAY_SIZE(power_attrs); i++)
190-
snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
191-
hwdep->device, &power_attrs[i]);
192-
return 0;
193-
}
182+
static DEVICE_ATTR_RO(power_on_acct);
183+
static DEVICE_ATTR_RO(power_off_acct);
194184
#endif /* CONFIG_PM */
195185

196186
#ifdef CONFIG_SND_HDA_RECONFIG
@@ -568,44 +558,21 @@ static ssize_t user_pin_configs_store(struct device *dev,
568558
return count;
569559
}
570560

571-
#define CODEC_ATTR_RW(type) \
572-
__ATTR(type, 0644, type##_show, type##_store)
573-
#define CODEC_ATTR_RO(type) \
574-
__ATTR_RO(type)
575-
#define CODEC_ATTR_WO(type) \
576-
__ATTR(type, 0200, NULL, type##_store)
577-
578-
static struct device_attribute codec_attrs[] = {
579-
CODEC_ATTR_RW(vendor_id),
580-
CODEC_ATTR_RW(subsystem_id),
581-
CODEC_ATTR_RW(revision_id),
582-
CODEC_ATTR_RO(afg),
583-
CODEC_ATTR_RO(mfg),
584-
CODEC_ATTR_RW(vendor_name),
585-
CODEC_ATTR_RW(chip_name),
586-
CODEC_ATTR_RW(modelname),
587-
CODEC_ATTR_RW(init_verbs),
588-
CODEC_ATTR_RW(hints),
589-
CODEC_ATTR_RO(init_pin_configs),
590-
CODEC_ATTR_RW(user_pin_configs),
591-
CODEC_ATTR_RO(driver_pin_configs),
592-
CODEC_ATTR_WO(reconfig),
593-
CODEC_ATTR_WO(clear),
594-
};
595-
596-
/*
597-
* create sysfs files on hwdep directory
598-
*/
599-
int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
600-
{
601-
struct snd_hwdep *hwdep = codec->hwdep;
602-
int i;
603-
604-
for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
605-
snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
606-
hwdep->device, &codec_attrs[i]);
607-
return 0;
608-
}
561+
static DEVICE_ATTR_RW(vendor_id);
562+
static DEVICE_ATTR_RW(subsystem_id);
563+
static DEVICE_ATTR_RW(revision_id);
564+
static DEVICE_ATTR_RO(afg);
565+
static DEVICE_ATTR_RO(mfg);
566+
static DEVICE_ATTR_RW(vendor_name);
567+
static DEVICE_ATTR_RW(chip_name);
568+
static DEVICE_ATTR_RW(modelname);
569+
static DEVICE_ATTR_RW(init_verbs);
570+
static DEVICE_ATTR_RW(hints);
571+
static DEVICE_ATTR_RO(init_pin_configs);
572+
static DEVICE_ATTR_RW(user_pin_configs);
573+
static DEVICE_ATTR_RO(driver_pin_configs);
574+
static DEVICE_ATTR_WO(reconfig);
575+
static DEVICE_ATTR_WO(clear);
609576

610577
/*
611578
* Look for hint string
@@ -884,3 +851,40 @@ int snd_hda_load_patch(struct hda_bus *bus, size_t fw_size, const void *fw_buf)
884851
}
885852
EXPORT_SYMBOL_GPL(snd_hda_load_patch);
886853
#endif /* CONFIG_SND_HDA_PATCH_LOADER */
854+
855+
/*
856+
* sysfs entries
857+
*/
858+
static struct attribute *hda_dev_attrs[] = {
859+
#ifdef CONFIG_PM
860+
&dev_attr_power_on_acct.attr,
861+
&dev_attr_power_off_acct.attr,
862+
#endif
863+
#ifdef CONFIG_SND_HDA_RECONFIG
864+
&dev_attr_vendor_id.attr,
865+
&dev_attr_subsystem_id.attr,
866+
&dev_attr_revision_id.attr,
867+
&dev_attr_afg.attr,
868+
&dev_attr_mfg.attr,
869+
&dev_attr_vendor_name.attr,
870+
&dev_attr_chip_name.attr,
871+
&dev_attr_modelname.attr,
872+
&dev_attr_init_verbs.attr,
873+
&dev_attr_hints.attr,
874+
&dev_attr_init_pin_configs.attr,
875+
&dev_attr_user_pin_configs.attr,
876+
&dev_attr_driver_pin_configs.attr,
877+
&dev_attr_reconfig.attr,
878+
&dev_attr_clear.attr,
879+
#endif
880+
NULL
881+
};
882+
883+
static struct attribute_group hda_dev_attr_group = {
884+
.attrs = hda_dev_attrs,
885+
};
886+
887+
static const struct attribute_group *snd_hda_dev_attr_groups[] = {
888+
&hda_dev_attr_group,
889+
NULL
890+
};

‎sound/pci/hda/hda_local.h‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -597,24 +597,6 @@ int snd_hda_create_hwdep(struct hda_codec *codec);
597597
static inline int snd_hda_create_hwdep(struct hda_codec *codec) { return 0; }
598598
#endif
599599

600-
#if defined(CONFIG_PM) && defined(CONFIG_SND_HDA_HWDEP)
601-
int snd_hda_hwdep_add_power_sysfs(struct hda_codec *codec);
602-
#else
603-
static inline int snd_hda_hwdep_add_power_sysfs(struct hda_codec *codec)
604-
{
605-
return 0;
606-
}
607-
#endif
608-
609-
#ifdef CONFIG_SND_HDA_RECONFIG
610-
int snd_hda_hwdep_add_sysfs(struct hda_codec *codec);
611-
#else
612-
static inline int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
613-
{
614-
return 0;
615-
}
616-
#endif
617-
618600
#ifdef CONFIG_SND_HDA_RECONFIG
619601
const char *snd_hda_get_hint(struct hda_codec *codec, const char *key);
620602
int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key);

0 commit comments

Comments
 (0)