]> git.alsa-project.org Git - alsa-lib.git/commitdiff
control: ucm: add ioctl to retrieve full card components
authorMaciej Strozek <mstrozek@opensource.cirrus.com>
Thu, 19 Feb 2026 13:46:20 +0000 (13:46 +0000)
committerJaroslav Kysela <perex@perex.cz>
Mon, 30 Nov 2026 19:41:12 +0000 (20:41 +0100)
The fixed-size components field in SNDRV_CTL_IOCTL_CARD_INFO can be too
small on systems with many audio devices. The kernel [1] will provide a
new ioctl to read the full string while truncating the original in
card_info if it grows too big. Make sure alsa-lib can read the full
string if the original is truncated.

[1] https://patch.msgid.link/20260720103505.1860399-2-mstrozek@opensource.cirrus.com

Closes: https://github.com/alsa-project/alsa-lib/pull/494
Signed-off-by: Maciej Strozek <mstrozek@opensource.cirrus.com>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
include/local.h
include/sound/uapi/asound.h
src/control/cards.c
src/control/control_hw.c

index a69967593bdeece017efe6b5097f9a92f7a64418..6eaa1c69542444cb8652f2e31f748d0492b0ebe6 100644 (file)
 #define _snd_pcm_sw_params snd_pcm_sw_params
 #define _snd_pcm_status snd_pcm_status
 
-#define _snd_ctl_card_info snd_ctl_card_info
 #define _snd_ctl_elem_id snd_ctl_elem_id
 #define _snd_ctl_elem_list snd_ctl_elem_list
 #define _snd_ctl_elem_info snd_ctl_elem_info
 #include "seq_midi_event.h"
 #include "list.h"
 
+/* V2 structure - increased components string from 128 bytes to 512 bytes */
+struct _snd_ctl_card_info {
+       int card;                       /* card number */
+       int pad;                        /* reserved for future (was type) */
+       unsigned char id[16];           /* ID of card (user selectable) */
+       unsigned char driver[16];       /* Driver name */
+       unsigned char name[32];         /* Short name of soundcard */
+       unsigned char longname[80];     /* name + info text about soundcard */
+       unsigned char reserved_[16];    /* reserved for future (was ID of mixer) */
+       unsigned char mixername[80];    /* visual mixer identification */
+       char components[512];
+};
+
+
 struct _snd_async_handler {
        enum {
                SND_ASYNC_HANDLER_GENERIC,
index bdc9a05ee03b6851c9aeaaceb1fd8b85c9a27f91..435fb754be6da524f8d24fe40dbd28dca198d3f5 100644 (file)
@@ -1044,7 +1044,7 @@ struct snd_timer_tread {
  *                                                                          *
  ****************************************************************************/
 
-#define SNDRV_CTL_VERSION              SNDRV_PROTOCOL_VERSION(2, 0, 9)
+#define SNDRV_CTL_VERSION              SNDRV_PROTOCOL_VERSION(2, 0, 10)
 
 struct snd_ctl_card_info {
        int card;                       /* card number */
@@ -1058,6 +1058,25 @@ struct snd_ctl_card_info {
        unsigned char components[128];  /* card components / fine identification, delimited with one space (AC97 etc..) */
 };
 
+/*
+ * Card components can exceed the fixed 128 bytes in snd_ctl_card_info.
+ * Use SNDRV_CTL_IOCTL_CARD_BYTES with type SND_CTL_CARD_BTYPE_COMPONENTS
+ * to retrieve the full string.
+ */
+
+/* Type values for struct snd_ctl_card_bytes::type */
+enum {
+       SND_CTL_CARD_BTYPE_COMPONENTS   = 1,    /* full card components string */
+};
+
+struct snd_ctl_card_bytes {
+       __u32 type;                             /* SND_CTL_CARD_BTYPE_* */
+       __u32 data_allocated;   /* size of @data buffer in bytes */
+       __u32 data_len;                 /* in/out: actual data length in bytes */
+       __u32 reserved;                 /* explicit pad */
+       __u64 data;                             /* user buffer (pointer stored as __u64) */
+};
+
 typedef int __bitwise snd_ctl_elem_type_t;
 #define        SNDRV_CTL_ELEM_TYPE_NONE        ((snd_ctl_elem_type_t) 0) /* invalid */
 #define        SNDRV_CTL_ELEM_TYPE_BOOLEAN     ((snd_ctl_elem_type_t) 1) /* boolean type */
@@ -1184,6 +1203,7 @@ struct snd_ctl_tlv {
 
 #define SNDRV_CTL_IOCTL_PVERSION       _IOR('U', 0x00, int)
 #define SNDRV_CTL_IOCTL_CARD_INFO      _IOR('U', 0x01, struct snd_ctl_card_info)
+#define SNDRV_CTL_IOCTL_CARD_BYTES     _IOWR('U', 0x02, struct snd_ctl_card_bytes)
 #define SNDRV_CTL_IOCTL_ELEM_LIST      _IOWR('U', 0x10, struct snd_ctl_elem_list)
 #define SNDRV_CTL_IOCTL_ELEM_INFO      _IOWR('U', 0x11, struct snd_ctl_elem_info)
 #define SNDRV_CTL_IOCTL_ELEM_READ      _IOWR('U', 0x12, struct snd_ctl_elem_value)
index 49e343a9b0de525f5a8b93d4183aa00c63d3b09d..9822850cc0c72292f16d80a6a95e9d678c2845da 100644 (file)
 static int snd_card_load2(const char *control)
 {
        int open_dev;
-       snd_ctl_card_info_t info;
+       struct snd_ctl_card_info kinfo;
 
        open_dev = snd_open_device(control, O_RDONLY);
        if (open_dev >= 0) {
-               if (ioctl(open_dev, SNDRV_CTL_IOCTL_CARD_INFO, &info) < 0) {
+               if (ioctl(open_dev, SNDRV_CTL_IOCTL_CARD_INFO, &kinfo) < 0) {
                        int err = -errno;
                        close(open_dev);
                        return err;
                }
                close(open_dev);
-               return info.card;
+               return kinfo.card;
        } else {
                return -errno;
        }
index 962cd796914beb3f32cbd7edec9c9154b368327e..dc055538fa992ea82cf810a3a7bff2f481903313 100644 (file)
@@ -129,10 +129,44 @@ static int snd_ctl_hw_subscribe_events(snd_ctl_t *handle, int subscribe)
 static int snd_ctl_hw_card_info(snd_ctl_t *handle, snd_ctl_card_info_t *info)
 {
        snd_ctl_hw_t *hw = handle->private_data;
-       if (ioctl(hw->fd, SNDRV_CTL_IOCTL_CARD_INFO, info) < 0) {
+       struct snd_ctl_card_info kinfo;
+       struct snd_ctl_card_bytes bytes;
+
+       if (ioctl(hw->fd, SNDRV_CTL_IOCTL_CARD_INFO, &kinfo) < 0) {
                snd_errornum(CONTROL, "SNDRV_CTL_IOCTL_CARD_INFO failed");
                return -errno;
        }
+
+       snd_ctl_card_info_clear(info);
+       info->card = kinfo.card;
+       info->pad = kinfo.pad;
+       snd_strlcpy((char *)info->id, (char *)kinfo.id, sizeof(info->id));
+       snd_strlcpy((char *)info->driver, (char *)kinfo.driver, sizeof(info->driver));
+       snd_strlcpy((char *)info->name, (char *)kinfo.name, sizeof(info->name));
+       snd_strlcpy((char *)info->longname, (char *)kinfo.longname, sizeof(info->longname));
+       snd_strlcpy((char *)info->reserved_, (char *)kinfo.reserved_, sizeof(info->reserved_));
+       snd_strlcpy((char *)info->mixername, (char *)kinfo.mixername, sizeof(info->mixername));
+       if (hw->protocol >= SNDRV_PROTOCOL_VERSION(2, 0, 10) &&
+           strnlen((char *)kinfo.components, sizeof(kinfo.components)) == sizeof(kinfo.components) - 1 &&
+           kinfo.components[sizeof(kinfo.components) - 2] == '>') {
+               bytes.type = SND_CTL_CARD_BTYPE_COMPONENTS;
+               bytes.data = (uintptr_t)info->components;
+               bytes.data_allocated = sizeof(info->components);
+               bytes.data_len = 0;
+               bytes.reserved = 0;
+               if (ioctl(hw->fd, SNDRV_CTL_IOCTL_CARD_BYTES, &bytes) < 0) {
+                       snd_errornum(CONTROL, "SNDRV_CTL_IOCTL_CARD_BYTES failed");
+                       return -errno;
+               }
+               if (bytes.data_len != strnlen(info->components, sizeof(info->components)) + 1) {
+                       snd_error(CONTROL, "SNDRV_CTL_IOCTL_CARD_BYTES unexpected length %u, should be %zu",
+                                 bytes.data_len, strnlen(info->components, sizeof(info->components)) + 1);
+                       return -EIO;
+               }
+       } else {
+               snd_strlcpy(info->components, (char *)kinfo.components, sizeof(info->components));
+       }
+
        return 0;
 }