Skip to contentSkip to conversation

ALSA: pcm: Add missing formats to formats list

PATCH In review Ivan Orlov 3 participants 8 messages Jan 25, 2024 – Feb 1, 2024

Subsystems (2)
lore
Share & tools
lore

Ivan Orlov

author maintainer
#0 lore raw

PATCH 1/3 ALSA: pcm: Add missing formats to formats list

Add 4 missing formats to 'snd_pcm_format_names' array in order to be able to get their names with 'snd_pcm_format_name' function.

---
 sound/core/pcm.c | 4 ++++
 1 file changed, 4 insertions(+)
sound/core/pcm.c +4 -0
index a09f0154e6a7..d0788126cbab 100644
@@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] = {
 	FORMAT(DSD_U32_LE),
 	FORMAT(DSD_U16_BE),
 	FORMAT(DSD_U32_BE),
+	FORMAT(S20_LE),
+	FORMAT(S20_BE),
+	FORMAT(U20_LE),
+	FORMAT(U20_BE),
 };
 
 /**
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 2/3 ALSA: pcm: Fix snd_pcm_format_name function

Fix snd_pcm_format_name so it won't return NULL-pointer in case if it can't find the format in the 'snd_pcm_format_names' list. Return "Unknown" instead, as it is done if the number passed to the function is larger than a list size.

---
 sound/core/pcm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
sound/core/pcm.c +4 -2
index d0788126cbab..d9b338088d10 100644
@@ -225,9 +225,11 @@ static const char * const snd_pcm_format_names[] = {
  */
 const char *snd_pcm_format_name(snd_pcm_format_t format)
 {
-	if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
+	unsigned int format_num = (__force unsigned int)format;
+
+	if (format_num >= ARRAY_SIZE(snd_pcm_format_names) || !snd_pcm_format_names[format_num])
 		return "Unknown";
-	return snd_pcm_format_names[(__force unsigned int)format];
+	return snd_pcm_format_names[format_num];
 }
 EXPORT_SYMBOL_GPL(snd_pcm_format_name);
 
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH 3/3 ALSA: core: Add sound core KUnit test

In reply to Ivan Orlov

At the moment, we have a decent amount of integration tests (selftests) covering different aspects of the sound subsystem. However, a lot of of sound-related in-kernel functions remains uncovered. This patch introduces the KUnit test for the core part of the sound subsystem. It includes 10 test cases:

- Coverage of the format-related inline functions from 'pcm.h' header
file: snd_pcm_format_physical_width, snd_pcm_format_width, snd_pcm_format_signed, test_format_endianness

- Coverage of the available bytes counting functions from 'pcm.h'
header: snd_pcm_capture_avail, snd_pcm_playback_avail

- Coverage of functions from pcm_misc: snd_pcm_format_set_silence, snd_pcm_format_name

- Coverage of card-related functions from init.c: snd_card_set_id, snd_component_add

This patch depends on the previous patches in this patch series as they contain fix for the bug, which was found during the test development. Without them, the test doesn't pass.

---
 MAINTAINERS              |   6 +
 sound/core/Kconfig       |  16 ++
 sound/core/Makefile      |   2 +
 sound/core/sound_kunit.c | 310 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 334 insertions(+)
 create mode 100644 sound/core/sound_kunit.c
MAINTAINERS +6 -0
index 39219b144c23..40b79daad32b 100644
@@ -20485,6 +20485,12 @@ F:	include/uapi/sound/compress_*
 F:	sound/core/compress_offload.c
 F:	sound/soc/soc-compress.c
 
+SOUND - CORE KUNIT TEST
+M:	Ivan Orlov <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	sound/core/sound_kunit.c
+
 SOUND - DMAENGINE HELPERS
 M:	Lars-Peter Clausen <[email protected]>
 S:	Supported
sound/core/Kconfig +16 -0
index e41818e59a15..664c6ee2b5a1 100644
@@ -39,6 +39,22 @@ config SND_UMP_LEGACY_RAWMIDI
 	  legacy MIDI 1.0 byte streams is created for each UMP Endpoint.
 	  The device contains 16 substreams corresponding to UMP groups.
 
+config SND_CORE_TEST
+	tristate "Sound core KUnit test"
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This options enables the sound core functions KUnit test.
+
+	  KUnit tests run during boot and output the results to the debug
+	  log in TAP format (https://testanything.org/). Only useful for
+	  kernel devs running KUnit test harness and are not for inclusion
+	  into a production build.
+
+	  For more information on KUnit and unit tests in general, refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+
 config SND_COMPRESS_OFFLOAD
 	tristate
 
sound/core/Makefile +2 -0
index a6b444ee2832..1d34e6950317 100644
@@ -49,6 +49,8 @@ obj-$(CONFIG_SND_SEQ_DEVICE)	+= snd-seq-device.o
 obj-$(CONFIG_SND_RAWMIDI)	+= snd-rawmidi.o
 obj-$(CONFIG_SND_UMP)		+= snd-ump.o
 
+obj-$(CONFIG_SND_CORE_TEST)	+= sound_kunit.o
+
 obj-$(CONFIG_SND_OSSEMUL)	+= oss/
 obj-$(CONFIG_SND_SEQUENCER)	+= seq/
 
sound/core/sound_kunit.c new file +310 -0

310 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw
In reply to Ivan Orlov
Quoted 13 lines · Ivan Orlov

Add 4 missing formats to 'snd_pcm_format_names' array in order to be able to get their names with 'snd_pcm_format_name' function.

---
  sound/core/pcm.c | 4 ++++
  1 file changed, 4 insertions(+)
sound/core/pcm.c +0 -0
index a09f0154e6a7..d0788126cbab 100644

sound/core/pcm.c static const char * const snd_pcm_format_names[] = { · line 220

Quoted 10 lines
sound/core/pcm.c +4 -0
@@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] = {
  	FORMAT(DSD_U32_LE),
  	FORMAT(DSD_U16_BE),
  	FORMAT(DSD_U32_BE),
+	FORMAT(S20_LE),
+	FORMAT(S20_BE),
+	FORMAT(U20_LE),
+	FORMAT(U20_BE),
  };
 
  /**

Maybe we can also add some kind of static_assert to check at compile time that all formats are handled, something like:
static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST + 1);

Although looking at definitions there is a hole between SNDRV_PCM_FORMAT_U20_BE & SNDRV_PCM_FORMAT_SPECIAL, which will cause this idea to fail.

Perhaps with comment:
static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST + 1 - 2); /* -2 for formats reserved for future use */ ?

author maintainer
#4 lore raw
Quoted 28 lines · Amadeusz Sławiński
Quoted 13 lines · Ivan Orlov

Add 4 missing formats to 'snd_pcm_format_names' array in order to be able to get their names with 'snd_pcm_format_name' function.

---
  sound/core/pcm.c | 4 ++++

  1 file changed, 4 insertions(+)
sound/core/pcm.c +0 -0
index a09f0154e6a7..d0788126cbab 100644

sound/core/pcm.c static const char * const snd_pcm_format_names[] = { · line 219

Quoted 9 lines
sound/core/pcm.c +4 -0
@@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] = {
       FORMAT(DSD_U32_LE),
       FORMAT(DSD_U16_BE),
       FORMAT(DSD_U32_BE),
+    FORMAT(S20_LE),
+    FORMAT(S20_BE),
+    FORMAT(U20_LE),
+    FORMAT(U20_BE),
   };
   /**

Maybe we can also add some kind of static_assert to check at compile time that all formats are handled, something like:
static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST
+ 1);

Quoted 6 lines

Although looking at definitions there is a hole between SNDRV_PCM_FORMAT_U20_BE & SNDRV_PCM_FORMAT_SPECIAL, which will cause this idea to fail.

Perhaps with comment:
static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST
+ 1 - 2); /* -2 for formats reserved for future use */
?

Hi Amadeusz,

Thank you for the review and sorry for the late answer. Yes, I believe it could be a nice improvement! Could I send a patch and specify you in Suggested-by tag?

Signature · 2 lines
Kind regards,
Ivan Orlov
#5 lore raw
Quoted 31 lines · Ivan Orlov
Quoted 30 lines · Amadeusz Sławiński
Quoted 13 lines · Ivan Orlov

Add 4 missing formats to 'snd_pcm_format_names' array in order to be able to get their names with 'snd_pcm_format_name' function.

---
  sound/core/pcm.c | 4 ++++

  1 file changed, 4 insertions(+)
sound/core/pcm.c +0 -0
index a09f0154e6a7..d0788126cbab 100644

sound/core/pcm.c static const char * const snd_pcm_format_names[] · line 220

Quoted 10 lines
sound/core/pcm.c +4 -0
@@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] 
 = {
       FORMAT(DSD_U32_LE),
       FORMAT(DSD_U16_BE),
       FORMAT(DSD_U32_BE),
+    FORMAT(S20_LE),
+    FORMAT(S20_BE),
+    FORMAT(U20_LE),
+    FORMAT(U20_BE),
   };
   /**

Maybe we can also add some kind of static_assert to check at compile time that all formats are handled, something like:
static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST + 1);

Although looking at definitions there is a hole between SNDRV_PCM_FORMAT_U20_BE & SNDRV_PCM_FORMAT_SPECIAL, which will cause this idea to fail.

Quoted 8 lines
Quoted 3 lines

Perhaps with comment:
static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST + 1 - 2); /* -2 for formats reserved for future use */ ?

Hi Amadeusz,

Thank you for the review and sorry for the late answer. Yes, I believe it could be a nice improvement! Could I send a patch and specify you in Suggested-by tag?

Hi,

yes, go for it!

Amadeusz

#6 lore raw
In reply to Ivan Orlov
Quoted 5 lines · Ivan Orlov

Add 4 missing formats to 'snd_pcm_format_names' array in order to be able to get their names with 'snd_pcm_format_name' function.

Now applied all three patches. This first one is to for-linus, while the rest two are to for-next for 6.9 kernel.

For further improvements (e.g. Amadeusz suggested), let's apply on top of that.

thanks,

Takashi

author maintainer
#7 lore raw
Quoted 11 lines · Takashi Iwai
Quoted 5 lines · Ivan Orlov

Add 4 missing formats to 'snd_pcm_format_names' array in order to be able to get their names with 'snd_pcm_format_name' function.

Now applied all three patches. This first one is to for-linus, while the rest two are to for-next for 6.9 kernel.

Quoted 7 lines

For further improvements (e.g. Amadeusz suggested), let's apply on top of that.

thanks,

Takashi

Hi Takashi,

Thank you for applying this changes! I'll send the patch with the improvements suggested by Amadeusz as soon as possible.

Signature · 2 lines
Kind regards,
Ivan Orlov