Skip to contentSkip to conversation

kselftest: alsa: Add basic mixer selftest

PATCH Applied Mark Brown 2 participants 5 messages Dec 10, 2021 – Dec 12, 2021

Subsystems (3)
lore
Share & tools
lore

Mark Brown

author maintainer
#0 lore raw

v4 cover kselftest: alsa: Add basic mixer selftest

This series adds a basic selftest for the mixer interface, as discussed in the cover letter for the main patch there's plenty of additional coverage that could be added but this is a good starting point.

v4:
- More stylistic updates suggested by Shuah.
- Fix build with older alsa-lib.
v3:
- Pull in incremental updates adding a fixed library configuration from Jaroslav and support for volatile controls from Takashi Sakamoto.
- Stylistic updates suggested by Shuah.
v2:
- Use pkg-config to get CFLAGS and LDLIBS for alsa-lib.

Jaroslav Kysela (1):
kselftest: alsa: Use private alsa-lib configuration in mixer test

Mark Brown (1):
kselftest: alsa: Add simplistic test for ALSA mixer controls kselftest

Takashi Sakamoto (1):
kselftest: alsa: optimization for SNDRV_CTL_ELEM_ACCESS_VOLATILE

 MAINTAINERS                               |   8 +
 tools/testing/selftests/Makefile          |   3 +-
 tools/testing/selftests/alsa/.gitignore   |   1 +
 tools/testing/selftests/alsa/Makefile     |   9 +
 tools/testing/selftests/alsa/mixer-test.c | 665 ++++++++++++++++++++++
 5 files changed, 685 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/alsa/.gitignore
 create mode 100644 tools/testing/selftests/alsa/Makefile
 create mode 100644 tools/testing/selftests/alsa/mixer-test.c

base-commit: fa55b7dcdc43c1aa1ba12bca9d2dd4318c2a0dbf

Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH v4 1/3 kselftest: alsa: Add simplistic test for ALSA mixer controls kselftest

Add a basic test for the mixer control interface. For every control on every sound card in the system it checks that it can read and write the default value where the control supports that and for writeable controls attempts to write all valid values, restoring the default values after each test to minimise disruption for users.

There are quite a few areas for improvement - currently no coverage of the generation of notifications, several of the control types don't have any coverage for the values and we don't have any testing of error handling when we attempt to write out of range values - but this provides some basic coverage.

This is added as a kselftest since unlike other ALSA test programs it does not require either physical setup of the device or interactive monitoring by users and kselftest is one of the test suites that is frequently run by people doing general automated testing so should increase coverage. It is written in terms of alsa-lib since tinyalsa is not generally packaged for distributions which makes things harder for general users interested in kselftest as a whole but it will be a barrier to people with Android.

---
 MAINTAINERS                               |   8 +
 tools/testing/selftests/Makefile          |   3 +-
 tools/testing/selftests/alsa/.gitignore   |   1 +
 tools/testing/selftests/alsa/Makefile     |   9 +
 tools/testing/selftests/alsa/mixer-test.c | 605 ++++++++++++++++++++++
 5 files changed, 625 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/alsa/.gitignore
 create mode 100644 tools/testing/selftests/alsa/Makefile
 create mode 100644 tools/testing/selftests/alsa/mixer-test.c
MAINTAINERS +8 -0
index 7a2345ce8521..8ded9f48b432 100644
@@ -17805,6 +17805,7 @@ F:	Documentation/sound/
 F:	include/sound/
 F:	include/uapi/sound/
 F:	sound/
+F:	tools/testing/selftests/alsa
 
 SOUND - COMPRESSED AUDIO
 M:	Vinod Koul <[email protected]>
@@ -17824,6 +17825,13 @@ F:	include/sound/dmaengine_pcm.h
 F:	sound/core/pcm_dmaengine.c
 F:	sound/soc/soc-generic-dmaengine-pcm.c
 
+SOUND - ALSA SELFTESTS
+M:	Mark Brown <[email protected]>
+L:	[email protected] (moderated for non-subscribers)
+L:	[email protected]
+S:	Supported
+F:	tools/testing/selftests/alsa
+
 SOUND - SOC LAYER / DYNAMIC AUDIO POWER MANAGEMENT (ASoC)
 M:	Liam Girdwood <[email protected]>
 M:	Mark Brown <[email protected]>
tools/testing/selftests/Makefile +2 -1
index c852eb40c4f7..d08fe4cfe811 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
-TARGETS = arm64
+TARGETS += alsa
+TARGETS += arm64
 TARGETS += bpf
 TARGETS += breakpoints
 TARGETS += capabilities
tools/testing/selftests/alsa/.gitignore new file +1 -0
new file mode 100644
index 000000000000..3bb7c41266a8
@@ -0,0 +1 @@
+mixer-test
tools/testing/selftests/alsa/Makefile new file +9 -0
new file mode 100644
index 000000000000..f64d9090426d
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+
+CFLAGS += $(shell pkg-config --cflags alsa)
+LDLIBS += $(shell pkg-config --libs alsa)
+
+TEST_GEN_PROGS := mixer-test
+
+include ../lib.mk
tools/testing/selftests/alsa/mixer-test.c new file +605 -0

605 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH v4 2/3 kselftest: alsa: optimization for SNDRV_CTL_ELEM_ACCESS_VOLATILE

In reply to Mark Brown

From: Takashi Sakamoto <[email protected]>

The volatile attribute of control element means that the hardware can voluntarily change the state of control element independent of any operation by software. ALSA control core necessarily sends notification to userspace subscribers for any change from userspace application, while it doesn't for the hardware's voluntary change.

This commit adds optimization for the attribute. Even if read value is different from written value, the test reports success as long as the target control element has the attribute. On the other hand, the difference is itself reported for developers' convenience.

---
 tools/testing/selftests/alsa/mixer-test.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/testing/selftests/alsa/mixer-test.c +9 -3
index ab51cf7b9e03..f65a9046e708 100644
@@ -307,9 +307,15 @@ bool show_mismatch(struct ctl_data *ctl, int index,
 	}
 
 	if (expected_int != read_int) {
-		ksft_print_msg("%s.%d expected %lld but read %lld\n",
-			       ctl->name, index, expected_int, read_int);
-		return true;
+		/*
+		 * NOTE: The volatile attribute means that the hardware
+		 * can voluntarily change the state of control element
+		 * independent of any operation by software.  
+		 */
+		bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info);
+		ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n",
+			       ctl->name, index, expected_int, read_int, is_volatile);
+		return !is_volatile;
 	} else {
 		return false;
 	}
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

PATCH v4 3/3 kselftest: alsa: Use private alsa-lib configuration in mixer test

In reply to Mark Brown

From: Jaroslav Kysela <[email protected]>

As mentined by Takashi Sakamoto, the system-wide alsa-lib configuration may override the standard device declarations. This patch use the private alsa-lib configuration to set the predictable environment.

[Restructure version test to keep the preprocessor happy -- broonie]
---
 tools/testing/selftests/alsa/mixer-test.c | 56 ++++++++++++++++++++++-
 1 file changed, 55 insertions(+), 1 deletion(-)
tools/testing/selftests/alsa/mixer-test.c +55 -1
index f65a9046e708..b798a76f6825 100644
@@ -46,22 +46,74 @@ struct ctl_data {
 	struct ctl_data *next;
 };
 
+static const char *alsa_config =
+"ctl.hw {\n"
+"	@args [ CARD ]\n"
+"	@args.CARD.type string\n"
+"	type hw\n"
+"	card $CARD\n"
+"}\n"
+;
+
 int num_cards = 0;
 int num_controls = 0;
 struct card_data *card_list = NULL;
 struct ctl_data *ctl_list = NULL;
 
+#ifdef SND_LIB_VER
+#if SND_LIB_VERSION >= SND_LIB_VER(1, 2, 6)
+#define LIB_HAS_LOAD_STRING
+#endif
+#endif
+
+#ifndef LIB_HAS_LOAD_STRING
+int snd_config_load_string(snd_config_t **config, const char *s, size_t size)
+{
+	snd_input_t *input;
+	snd_config_t *dst;
+	int err;
+
+	assert(config && s);
+	if (size == 0)
+		size = strlen(s);
+	err = snd_input_buffer_open(&input, s, size);
+	if (err < 0)
+		return err;
+	err = snd_config_top(&dst);
+	if (err < 0) {
+		snd_input_close(input);
+		return err;
+	}
+	err = snd_config_load(dst, input);
+	snd_input_close(input);
+	if (err < 0) {
+		snd_config_delete(dst);
+		return err;
+	}
+	*config = dst;
+	return 0;
+}
+#endif
+
 void find_controls(void)
 {
 	char name[32];
 	int card, ctl, err;
 	struct card_data *card_data;
 	struct ctl_data *ctl_data;
+	snd_config_t *config;
 
 	card = -1;
 	if (snd_card_next(&card) < 0 || card < 0)
 		return;
 
+	err = snd_config_load_string(&config, alsa_config, strlen(alsa_config));
+	if (err < 0) {
+		ksft_print_msg("Unable to parse custom alsa-lib configuration: %s\n",
+			       snd_strerror(err));
+		ksft_exit_fail();
+	}
+
 	while (card >= 0) {
 		sprintf(name, "hw:%d", card);
 
@@ -69,7 +121,7 @@ void find_controls(void)
 		if (!card_data)
 			ksft_exit_fail_msg("Out of memory\n");
 
-		err = snd_ctl_open(&card_data->handle, name, 0);
+		err = snd_ctl_open_lconf(&card_data->handle, name, 0, config);
 		if (err < 0) {
 			ksft_print_msg("Failed to get hctl for card %d: %s\n",
 				       card, snd_strerror(err));
@@ -137,6 +189,8 @@ void find_controls(void)
 			break;
 		}
 	}
+
+	snd_config_delete(config);
 }
 
 /*
Signature · 1 line
2.30.2
Applied
#4 lore raw
In reply to Mark Brown
Quoted 15 lines · Mark Brown

This series adds a basic selftest for the mixer interface, as discussed in the cover letter for the main patch there's plenty of additional coverage that could be added but this is a good starting point.

v4:
- More stylistic updates suggested by Shuah.
- Fix build with older alsa-lib.
v3:
- Pull in incremental updates adding a fixed library configuration from Jaroslav and support for volatile controls from Takashi Sakamoto.
- Stylistic updates suggested by Shuah.
v2:
- Use pkg-config to get CFLAGS and LDLIBS for alsa-lib.

Quoted 8 lines

Jaroslav Kysela (1):
kselftest: alsa: Use private alsa-lib configuration in mixer test

Mark Brown (1):
kselftest: alsa: Add simplistic test for ALSA mixer controls kselftest

Takashi Sakamoto (1):
kselftest: alsa: optimization for SNDRV_CTL_ELEM_ACCESS_VOLATILE

Thanks, applied now all three patches to for-next branch.

Takashi