summaryrefslogtreecommitdiff
path: root/sound
diff options
context:
space:
mode:
authorMaciej Strozek <mstrozek@opensource.cirrus.com>2026-07-20 11:35:05 +0100
committerTakashi Iwai <tiwai@suse.de>2026-08-05 09:28:59 +0200
commit86cac980c9668106b32ffca3bda106cbb6d7ac2b (patch)
treedcff04016533bad4a0dea51b5e852c6db4528b2c /sound
parenteaf46ee96599e20c56c46401c937102f18e081bf (diff)
ALSA: control: add ioctl to retrieve full card components
The fixed-size components field in SNDRV_CTL_IOCTL_CARD_INFO can be too small on systems with many audio devices. Keep the existing struct snd_ctl_card_info ABI intact and add a new ioctl SNDRV_CTL_IOCTL_CARD_BYTES that carries a variable-length payload selected by a type discriminator. The first defined type SND_CTL_CARD_BTYPE_COMPONENTS returns the full components string. The ioctl is designed to be reused for other variable-length card payloads in the future. The user-space caller may set data_allocated == 0 (or data == NULL) to query the required length; otherwise the kernel copies the payload into the user buffer and writes back the actual length in data_len. When the legacy components field in struct snd_ctl_card_info is truncated, '>' is written just before the NUL terminator to signal to user-space that the full string is available via the new ioctl. card->components is now dynamically allocated and grown in 32 byte increments via krealloc(), capped at 512 bytes. Link: https://github.com/alsa-project/alsa-lib/pull/494 Suggested-by: Jaroslav Kysela <perex@perex.cz> Suggested-by: Takashi Iwai <tiwai@suse.com> Signed-off-by: Maciej Strozek <mstrozek@opensource.cirrus.com> Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://patch.msgid.link/20260720103505.1860399-2-mstrozek@opensource.cirrus.com
Diffstat (limited to 'sound')
-rw-r--r--sound/core/control.c64
-rw-r--r--sound/core/control_compat.c1
-rw-r--r--sound/core/init.c36
3 files changed, 94 insertions, 7 deletions
diff --git a/sound/core/control.c b/sound/core/control.c
index 1116a40d11ae..78ce7bc936d2 100644
--- a/sound/core/control.c
+++ b/sound/core/control.c
@@ -38,7 +38,7 @@ struct snd_kctl_ioctl {
snd_kctl_ioctl_func_t fioctl;
};
-static DECLARE_RWSEM(snd_ioctl_rwsem);
+DECLARE_RWSEM(snd_ioctl_rwsem);
static DECLARE_RWSEM(snd_ctl_layer_rwsem);
static LIST_HEAD(snd_control_ioctls);
#ifdef CONFIG_COMPAT
@@ -872,23 +872,81 @@ static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl,
{
struct snd_ctl_card_info *info __free(kfree) =
kzalloc(sizeof(*info), GFP_KERNEL);
+ ssize_t n;
if (! info)
return -ENOMEM;
+
+ static_assert(sizeof(info->components) >= 2);
+
scoped_guard(rwsem_read, &snd_ioctl_rwsem) {
+ const char *components = card->components;
+
+ if (!components)
+ components = "";
+
info->card = card->number;
strscpy(info->id, card->id, sizeof(info->id));
strscpy(info->driver, card->driver, sizeof(info->driver));
strscpy(info->name, card->shortname, sizeof(info->name));
strscpy(info->longname, card->longname, sizeof(info->longname));
strscpy(info->mixername, card->mixername, sizeof(info->mixername));
- strscpy(info->components, card->components, sizeof(info->components));
+ n = strscpy(info->components, components, sizeof(info->components));
+ if (n < 0) // mark the truncation with '>' before NULL terminator
+ info->components[sizeof(info->components) - 2] = '>';
}
if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info)))
return -EFAULT;
return 0;
}
+static int snd_ctl_card_bytes(struct snd_card *card,
+ struct snd_ctl_card_bytes *info,
+ unsigned int __user *data_len_out)
+{
+ unsigned int data_len;
+
+ switch (info->type) {
+ case SND_CTL_CARD_BTYPE_COMPONENTS:
+ scoped_guard(rwsem_read, &snd_ioctl_rwsem) {
+ const char *components = card->components;
+
+ if (!components)
+ components = "";
+
+ data_len = strlen(components) + 1;
+
+ if (!info->data || info->data_allocated == 0)
+ break;
+
+ if (info->data_allocated < data_len)
+ return -ENOMEM;
+
+ if (copy_to_user(u64_to_user_ptr(info->data), components, data_len))
+ return -EFAULT;
+ }
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ if (put_user(data_len, data_len_out))
+ return -EFAULT;
+
+ return 0;
+}
+
+static int snd_ctl_card_bytes_user(struct snd_card *card,
+ struct snd_ctl_card_bytes __user *_info)
+{
+ struct snd_ctl_card_bytes info;
+
+ if (copy_from_user(&info, _info, sizeof(info)))
+ return -EFAULT;
+
+ return snd_ctl_card_bytes(card, &info, &_info->data_len);
+}
+
static int snd_ctl_elem_list(struct snd_card *card,
struct snd_ctl_elem_list *list)
{
@@ -1986,6 +2044,8 @@ static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg
return put_user(SNDRV_CTL_VERSION, ip) ? -EFAULT : 0;
case SNDRV_CTL_IOCTL_CARD_INFO:
return snd_ctl_card_info(card, ctl, cmd, argp);
+ case SNDRV_CTL_IOCTL_CARD_BYTES:
+ return snd_ctl_card_bytes_user(card, argp);
case SNDRV_CTL_IOCTL_ELEM_LIST:
return snd_ctl_elem_list_user(card, argp);
case SNDRV_CTL_IOCTL_ELEM_INFO:
diff --git a/sound/core/control_compat.c b/sound/core/control_compat.c
index 4ad571087ff5..f14d9f5e94be 100644
--- a/sound/core/control_compat.c
+++ b/sound/core/control_compat.c
@@ -446,6 +446,7 @@ static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, uns
switch (cmd) {
case SNDRV_CTL_IOCTL_PVERSION:
case SNDRV_CTL_IOCTL_CARD_INFO:
+ case SNDRV_CTL_IOCTL_CARD_BYTES:
case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
case SNDRV_CTL_IOCTL_POWER:
case SNDRV_CTL_IOCTL_POWER_STATE:
diff --git a/sound/core/init.c b/sound/core/init.c
index 0372756048cd..19ec68db561b 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -589,6 +589,9 @@ static int snd_card_do_free(struct snd_card *card)
snd_mixer_oss_notify_callback(card, SND_MIXER_OSS_NOTIFY_FREE);
#endif
snd_device_free_all(card);
+ kfree(card->components);
+ card->components = NULL;
+ card->components_alloc_size = 0;
if (card->private_free)
card->private_free(card);
#ifdef CONFIG_SND_CTL_DEBUG
@@ -1035,16 +1038,39 @@ int snd_component_add(struct snd_card *card, const char *component)
{
char *ptr;
int len = strlen(component);
+ unsigned int cur_len, need_len;
- ptr = strstr(card->components, component);
- if (ptr != NULL) {
- if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
- return 1;
+ guard(rwsem_write)(&snd_ioctl_rwsem);
+
+ if (card->components) {
+ ptr = strstr(card->components, component);
+ if (ptr) {
+ if (ptr[len] == '\0' || ptr[len] == ' ') /* already there */
+ return 1;
+ }
+ cur_len = strlen(card->components) + 1;
+ } else {
+ cur_len = 0;
}
- if (strlen(card->components) + 1 + len + 1 > sizeof(card->components)) {
+
+ need_len = cur_len + len + 1;
+ if (need_len > 512) {
snd_BUG();
return -ENOMEM;
}
+
+ if (need_len > card->components_alloc_size) {
+ unsigned int new_alloc = roundup(need_len, 32);
+
+ ptr = krealloc(card->components, new_alloc, GFP_KERNEL);
+ if (!ptr)
+ return -ENOMEM;
+ if (!card->components)
+ ptr[0] = '\0';
+ card->components = ptr;
+ card->components_alloc_size = new_alloc;
+ }
+
if (card->components[0] != '\0')
strcat(card->components, " ");
strcat(card->components, component);