2024-01-25 22:52:27

by Ivan Orlov

[permalink] [raw]
Subject: [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.

Signed-off-by: Ivan Orlov <[email protected]>
---
sound/core/pcm.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index a09f0154e6a7..d0788126cbab 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -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),
};

/**
--
2.34.1



2024-01-25 22:52:32

by Ivan Orlov

[permalink] [raw]
Subject: [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.

Signed-off-by: Ivan Orlov <[email protected]>
---
sound/core/pcm.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index d0788126cbab..d9b338088d10 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -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);

--
2.34.1


2024-01-26 12:40:14

by Amadeusz Sławiński

[permalink] [raw]
Subject: Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list

On 1/25/2024 11:35 PM, Ivan Orlov wrote:
> 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.
>
> Signed-off-by: Ivan Orlov <[email protected]>
> ---
> sound/core/pcm.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
> index a09f0154e6a7..d0788126cbab 100644
> --- a/sound/core/pcm.c
> +++ b/sound/core/pcm.c
> @@ -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 */
?

2024-01-30 13:19:59

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list

On Thu, 25 Jan 2024 23:35:19 +0100,
Ivan Orlov wrote:
>
> 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.
>
> Signed-off-by: Ivan Orlov <[email protected]>

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

2024-01-31 17:41:20

by Ivan Orlov

[permalink] [raw]
Subject: Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list

On 1/26/24 09:01, Amadeusz Sławiński wrote:
> On 1/25/2024 11:35 PM, Ivan Orlov wrote:
>> 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.
>>
>> Signed-off-by: Ivan Orlov <[email protected]>
>> ---
>>   sound/core/pcm.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
>> index a09f0154e6a7..d0788126cbab 100644
>> --- a/sound/core/pcm.c
>> +++ b/sound/core/pcm.c
>> @@ -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 */
> ?

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?

--
Kind regards,
Ivan Orlov


2024-01-31 17:43:06

by Ivan Orlov

[permalink] [raw]
Subject: Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list

On 1/30/24 13:15, Takashi Iwai wrote:
> On Thu, 25 Jan 2024 23:35:19 +0100,
> Ivan Orlov wrote:
>>
>> 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.
>>
>> Signed-off-by: Ivan Orlov <[email protected]>
>
> 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

Hi Takashi,

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

--
Kind regards,
Ivan Orlov


2024-02-01 13:42:31

by Amadeusz Sławiński

[permalink] [raw]
Subject: Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list

On 1/31/2024 6:41 PM, Ivan Orlov wrote:
> On 1/26/24 09:01, Amadeusz Sławiński wrote:
>> On 1/25/2024 11:35 PM, Ivan Orlov wrote:
>>> 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.
>>>
>>> Signed-off-by: Ivan Orlov <[email protected]>
>>> ---
>>>   sound/core/pcm.c | 4 ++++
>>>   1 file changed, 4 insertions(+)
>>>
>>> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
>>> index a09f0154e6a7..d0788126cbab 100644
>>> --- a/sound/core/pcm.c
>>> +++ b/sound/core/pcm.c
>>> @@ -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 */
>> ?
>
> 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