Skip to contentSkip to conversation

kselftest: alsa: Add basic mixer selftest

PATCH Superseded Mark Brown 3 participants 9 messages Dec 8, 2021 – Dec 10, 2021

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mark Brown

author maintainer
#0 lore raw

v3 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.

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 | 663 ++++++++++++++++++++++
 5 files changed, 683 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 v3 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 v3 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 | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/alsa/mixer-test.c +7 -3
index ab51cf7b9e03..171d33692c7b 100644
@@ -307,9 +307,13 @@ 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
#5 lore raw

PATCH v3 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 171d33692c7b..a177676c530e 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
maintainer Reviewed-by on v3 2/3
#3 lore raw

Re: [PATCH v3 2/3] kselftest: alsa: optimization for SNDRV_CTL_ELEM_ACCESS_VOLATILE

In reply to Mark Brown
Quoted 25 lines · 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 | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/alsa/mixer-test.c +0 -0
index ab51cf7b9e03..171d33692c7b 100644

tools/testing/selftests/alsa/mixer-test.c bool show_mismatch(struct ctl_data *ctl, int index, · line 311

Quoted 8 lines
tools/testing/selftests/alsa/mixer-test.c +2 -3
@@ -307,9 +307,13 @@ 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.

Let's stick to kernel comment format :)

tools/testing/selftests/alsa/mixer-test.c bool show_mismatch(struct ctl_data *ctl, int index, · line 319

Quoted 9 lines
tools/testing/selftests/alsa/mixer-test.c +5 -0
+		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;
  	}

With that change:

thanks,
-- Shuah

maintainer
#6 lore raw

Re: [PATCH v3 3/3] kselftest: alsa: Use private alsa-lib configuration in mixer test

In reply to Mark Brown
Quoted 58 lines · 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 +31 -0
index 171d33692c7b..a177676c530e 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);

tools/testing/selftests/alsa/mixer-test.c struct ctl_data { · line 94

Quoted 10 lines
tools/testing/selftests/alsa/mixer-test.c +10 -0
+		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;

Why not consolidate the error path code?

Quoted 35 lines
tools/testing/selftests/alsa/mixer-test.c +12 -1
+}
+#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));

tools/testing/selftests/alsa/mixer-test.c void find_controls(void) · line 196

Quoted 8 lines
tools/testing/selftests/alsa/mixer-test.c +2 -0
@@ -137,6 +189,8 @@ void find_controls(void)
  			break;
  		}
  	}
+
+	snd_config_delete(config);
  }
 
  /*

This open comment at the end of the patch looks odd. Does this compile?

Quoted 1 line

thanks
-- Shuah

author maintainer
#7 lore raw

Re: [PATCH v3 3/3] kselftest: alsa: Use private alsa-lib configuration in mixer test

Quoted 8 lines · Shuah Khan

tools/testing/selftests/alsa/mixer-test.c void find_controls(void) · line 196

Quoted 6 lines
  		}
  	}
tools/testing/selftests/alsa/mixer-test.c +2 -0
+
+	snd_config_delete(config);
  }
  /*

This open comment at the end of the patch looks odd. Does this compile?

Yes, it's the start of the comment describing the next function, more complete context is:

		}

		snd_config_delete(config);
	}

	/*
	 * Check that we can read the default value and it is valid. Write
	 * tests use the read value to restore the default.
	 */
	void test_ctl_get_value(struct ctl_data *ctl)
	{
maintainer Reviewed-by
#8 lore raw

Re: [PATCH v3 3/3] kselftest: alsa: Use private alsa-lib configuration in mixer test

Quoted 14 lines · Mark Brown
Quoted 8 lines · Shuah Khan

tools/testing/selftests/alsa/mixer-test.c void find_controls(void) · line 196

Quoted 6 lines
   		}
   	}
tools/testing/selftests/alsa/mixer-test.c +2 -0
+
+	snd_config_delete(config);
   }
   /*

This open comment at the end of the patch looks odd. Does this compile?

Yes, it's the start of the comment describing the next function, more complete context is:

Quoted 12 lines
		}

		snd_config_delete(config);
	}

	/*
	 * Check that we can read the default value and it is valid. Write
	 * tests use the read value to restore the default.
	 */
	void test_ctl_get_value(struct ctl_data *ctl)
	{

Yes my bad :)

thanks,
-- Shuah

#4 lore raw

Re: [PATCH v3 2/3] kselftest: alsa: optimization for SNDRV_CTL_ELEM_ACCESS_VOLATILE

In reply to Shuah Khan

Dear Shuah Khan,

Quoted 48 lines · Shuah Khan
Quoted 25 lines · 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 | 10 +++++++---
  1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/alsa/mixer-test.c +0 -0
index ab51cf7b9e03..171d33692c7b 100644

tools/testing/selftests/alsa/mixer-test.c bool show_mismatch(struct ctl_data *ctl, int index, · line 310

Quoted 7 lines
tools/testing/selftests/alsa/mixer-test.c +2 -3
@@ -307,9 +307,13 @@ 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.

Let's stick to kernel comment format :)

tools/testing/selftests/alsa/mixer-test.c bool show_mismatch(struct ctl_data *ctl, int index,

Quoted 9 lines
tools/testing/selftests/alsa/mixer-test.c +5 -0
+		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;
  	}
 

With that change:

Quoted 1 line

Thanks for your review. Indeed, when following to existent guideline of coding style, the comment should follow to C89/C90 style. I have no objection to your advice itself, while for the guideline itself I'd like to ask your opinion (and your help if possible).

In section '8) Commenting' in 'Documentation/process/coding-style.rst', we can see no example for comment prefixed with double slashes; '//'. On the other hand, we can see tons of actual usage in whole tree. We have the inconsistency between the guideline and what developers have done.

I think that the decision to allow double-slashes comment or not is left to subsystem maintainers, while I know that it's not allowed in UAPI header since they are built with --std=C90 compiler option (see head of 'usr/include/Makefile'). I can not find such restriction in the other parts of kernel code.

In my reference book about C language, double-slashes comment was officially introduced in C99 (ISO/IEC 9899:1999) therefore it's not specific to C++ nowadays. It's merely out of specification called as 'standard C' or 'ANSI C' (C89/C90, ISO/IEC 9899:1990).

Linux Torvalds appeared as his acceptance of double-slashes comment in the context about his intolerance of multi-line comment such that the introduction of comment, '/*', is just followed by content of comment without line break:

* Re: [patch] crypto: sha256-mb - cleanup a || vs | typo
* https://lore.kernel.org/lkml/CA+55aFyQYJerovMsSoSKS7PessZBr4vNp-3QUUwhqk4A4_jcbg@mail.gmail.com/

His preference is not necessarily equivalent to collective opinion in kernel development community when seeing the patch applied later:

* commit c4ff1b5f8bf0 ("CodingStyle: add networking specific block comment style")
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c4ff1b5f8bf0

His opinion does not necessarily have complete clout in the community, but overall there is less reason to reject the double-slashes comment.

In my opinion, it's time to modify the coding style documentation in the point of comment so that:

* accept double-slashes comment from C99 in whole tree
* except for UAPI header (to keep backward compatibility of userspace applications still built for C89/C90)

...But the discussion about official acceptance of C99 code can itself evolve many developers since it's equivalent to loss of backward compatibility to the environment built just for C89/C90. It's the reason I never work for it since I have limited resources to join in the discussion (I'm unpaid hobbyist with language barrier. My task in sound subsystem is development and maintenance of in-kernel protocol implementation of IEC 61883-1/6 and application drivers, including heavy load for reverse engineering).

I'm glad if getting your assistance somehow for the issue.

Best regards

Takashi Sakamoto