Skip to content

Commit 9fb6198

Browse files
perexgtiwai
authored andcommitted
ALSA: add /sys/class/sound/card#/id (r/w) and card#/number (r/o) files
For udev, we need a way to rename soundcard names. The soundcard numbers (indexes) are hardwired but we have a text identification which can be changed at run-time. The ALSA user space tools already allow using of this text identification. Signed-off-by: Jaroslav Kysela <perex@perex.cz> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent f21f237 commit 9fb6198

1 file changed

Lines changed: 65 additions & 1 deletion

File tree

‎sound/core/init.c‎

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,64 @@ static void choose_default_id(struct snd_card *card)
533533
}
534534
}
535535

536+
#ifndef CONFIG_SYSFS_DEPRECATED
537+
static ssize_t
538+
card_id_show_attr(struct device *dev,
539+
struct device_attribute *attr, char *buf)
540+
{
541+
struct snd_card *card = dev_get_drvdata(dev);
542+
return snprintf(buf, PAGE_SIZE, "%s\n", card ? card->id : "(null)");
543+
}
544+
545+
static ssize_t
546+
card_id_store_attr(struct device *dev, struct device_attribute *attr,
547+
const char *buf, size_t count)
548+
{
549+
struct snd_card *card = dev_get_drvdata(dev);
550+
char buf1[sizeof(card->id)];
551+
size_t copy = count > sizeof(card->id) - 1 ?
552+
sizeof(card->id) - 1 : count;
553+
size_t idx;
554+
int c;
555+
556+
for (idx = 0; idx < copy; idx++) {
557+
c = buf[idx];
558+
if (!isalnum(c) && c != '_' && c != '-')
559+
return -EINVAL;
560+
}
561+
memcpy(buf1, buf, copy);
562+
buf1[copy] = '\0';
563+
mutex_lock(&snd_card_mutex);
564+
if (!snd_info_check_reserved_words(buf1)) {
565+
__exist:
566+
mutex_unlock(&snd_card_mutex);
567+
return -EEXIST;
568+
}
569+
for (idx = 0; idx < snd_ecards_limit; idx++) {
570+
if (snd_cards[idx] && !strcmp(snd_cards[idx]->id, buf1))
571+
goto __exist;
572+
}
573+
strcpy(card->id, buf1);
574+
mutex_unlock(&snd_card_mutex);
575+
576+
return count;
577+
}
578+
579+
static struct device_attribute card_id_attrs =
580+
__ATTR(id, S_IRUGO | S_IWUSR, card_id_show_attr, card_id_store_attr);
581+
582+
static ssize_t
583+
card_number_show_attr(struct device *dev,
584+
struct device_attribute *attr, char *buf)
585+
{
586+
struct snd_card *card = dev_get_drvdata(dev);
587+
return snprintf(buf, PAGE_SIZE, "%i\n", card ? card->number : -1);
588+
}
589+
590+
static struct device_attribute card_number_attrs =
591+
__ATTR(number, S_IRUGO, card_number_show_attr, NULL);
592+
#endif /* CONFIG_SYSFS_DEPRECATED */
593+
536594
/**
537595
* snd_card_register - register the soundcard
538596
* @card: soundcard structure
@@ -553,7 +611,7 @@ int snd_card_register(struct snd_card *card)
553611
#ifndef CONFIG_SYSFS_DEPRECATED
554612
if (!card->card_dev) {
555613
card->card_dev = device_create(sound_class, card->dev,
556-
MKDEV(0, 0), NULL,
614+
MKDEV(0, 0), card,
557615
"card%i", card->number);
558616
if (IS_ERR(card->card_dev))
559617
card->card_dev = NULL;
@@ -575,6 +633,12 @@ int snd_card_register(struct snd_card *card)
575633
#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
576634
if (snd_mixer_oss_notify_callback)
577635
snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_REGISTER);
636+
#endif
637+
#ifndef CONFIG_SYSFS_DEPRECATED
638+
if (card->card_dev) {
639+
device_create_file(card->card_dev, &card_id_attrs);
640+
device_create_file(card->card_dev, &card_number_attrs);
641+
}
578642
#endif
579643
return 0;
580644
}

0 commit comments

Comments
 (0)