| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Sound core KUnit test |
| 4 | * Author: Ivan Orlov <ivan.orlov0322@gmail.com> |
| 5 | */ |
| 6 | |
| 7 | #include <kunit/test.h> |
| 8 | #include <sound/core.h> |
| 9 | #include <sound/pcm.h> |
| 10 | |
| 11 | #define SILENCE_BUFFER_MAX_FRAMES 260 |
| 12 | #define SILENCE_BUFFER_SIZE (sizeof(u64) * SILENCE_BUFFER_MAX_FRAMES) |
| 13 | #define SILENCE(...) { __VA_ARGS__ } |
| 14 | #define DEFINE_FORMAT(fmt, pbits, wd, endianness, signd, silence_arr) { \ |
| 15 | .format = SNDRV_PCM_FORMAT_##fmt, .physical_bits = pbits, \ |
| 16 | .width = wd, .le = endianness, .sd = signd, .silence = silence_arr, \ |
| 17 | .name = #fmt, \ |
| 18 | } |
| 19 | |
| 20 | #define WRONG_FORMAT_1 (__force snd_pcm_format_t)((__force int)SNDRV_PCM_FORMAT_LAST + 1) |
| 21 | #define WRONG_FORMAT_2 (__force snd_pcm_format_t)-1 |
| 22 | |
| 23 | #define VALID_NAME "ValidName" |
| 24 | #define NAME_W_SPEC_CHARS "In%v@1id name" |
| 25 | #define NAME_W_SPACE "Test name" |
| 26 | #define NAME_W_SPACE_REMOVED "Testname" |
| 27 | |
| 28 | #define TEST_FIRST_COMPONENT "Component1" |
| 29 | #define TEST_SECOND_COMPONENT "Component2" |
| 30 | |
| 31 | struct snd_format_test_data { |
| 32 | snd_pcm_format_t format; |
| 33 | int physical_bits; |
| 34 | int width; |
| 35 | int le; |
| 36 | int sd; |
| 37 | unsigned char silence[8]; |
| 38 | unsigned char *name; |
| 39 | }; |
| 40 | |
| 41 | struct avail_test_data { |
| 42 | snd_pcm_uframes_t buffer_size; |
| 43 | snd_pcm_uframes_t hw_ptr; |
| 44 | snd_pcm_uframes_t appl_ptr; |
| 45 | snd_pcm_uframes_t expected_avail; |
| 46 | }; |
| 47 | |
| 48 | static const struct snd_format_test_data valid_fmt[] = { |
| 49 | DEFINE_FORMAT(S8, 8, 8, -1, 1, SILENCE()), |
| 50 | DEFINE_FORMAT(U8, 8, 8, -1, 0, SILENCE(0x80)), |
| 51 | DEFINE_FORMAT(S16_LE, 16, 16, 1, 1, SILENCE()), |
| 52 | DEFINE_FORMAT(S16_BE, 16, 16, 0, 1, SILENCE()), |
| 53 | DEFINE_FORMAT(U16_LE, 16, 16, 1, 0, SILENCE(0x00, 0x80)), |
| 54 | DEFINE_FORMAT(U16_BE, 16, 16, 0, 0, SILENCE(0x80, 0x00)), |
| 55 | DEFINE_FORMAT(S24_LE, 32, 24, 1, 1, SILENCE()), |
| 56 | DEFINE_FORMAT(S24_BE, 32, 24, 0, 1, SILENCE()), |
| 57 | DEFINE_FORMAT(U24_LE, 32, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)), |
| 58 | DEFINE_FORMAT(U24_BE, 32, 24, 0, 0, SILENCE(0x00, 0x80, 0x00, 0x00)), |
| 59 | DEFINE_FORMAT(S32_LE, 32, 32, 1, 1, SILENCE()), |
| 60 | DEFINE_FORMAT(S32_BE, 32, 32, 0, 1, SILENCE()), |
| 61 | DEFINE_FORMAT(U32_LE, 32, 32, 1, 0, SILENCE(0x00, 0x00, 0x00, 0x80)), |
| 62 | DEFINE_FORMAT(U32_BE, 32, 32, 0, 0, SILENCE(0x80, 0x00, 0x00, 0x00)), |
| 63 | DEFINE_FORMAT(FLOAT_LE, 32, 32, 1, -1, SILENCE()), |
| 64 | DEFINE_FORMAT(FLOAT_BE, 32, 32, 0, -1, SILENCE()), |
| 65 | DEFINE_FORMAT(FLOAT64_LE, 64, 64, 1, -1, SILENCE()), |
| 66 | DEFINE_FORMAT(FLOAT64_BE, 64, 64, 0, -1, SILENCE()), |
| 67 | DEFINE_FORMAT(IEC958_SUBFRAME_LE, 32, 32, 1, -1, SILENCE()), |
| 68 | DEFINE_FORMAT(IEC958_SUBFRAME_BE, 32, 32, 0, -1, SILENCE()), |
| 69 | DEFINE_FORMAT(MU_LAW, 8, 8, -1, -1, SILENCE(0x7f)), |
| 70 | DEFINE_FORMAT(A_LAW, 8, 8, -1, -1, SILENCE(0x55)), |
| 71 | DEFINE_FORMAT(IMA_ADPCM, 4, 4, -1, -1, SILENCE()), |
| 72 | DEFINE_FORMAT(G723_24, 3, 3, -1, -1, SILENCE()), |
| 73 | DEFINE_FORMAT(G723_40, 5, 5, -1, -1, SILENCE()), |
| 74 | DEFINE_FORMAT(DSD_U8, 8, 8, 1, 0, SILENCE(0x69)), |
| 75 | DEFINE_FORMAT(DSD_U16_LE, 16, 16, 1, 0, SILENCE(0x69, 0x69)), |
| 76 | DEFINE_FORMAT(DSD_U32_LE, 32, 32, 1, 0, SILENCE(0x69, 0x69, 0x69, 0x69)), |
| 77 | DEFINE_FORMAT(DSD_U16_BE, 16, 16, 0, 0, SILENCE(0x69, 0x69)), |
| 78 | DEFINE_FORMAT(DSD_U32_BE, 32, 32, 0, 0, SILENCE(0x69, 0x69, 0x69, 0x69)), |
| 79 | DEFINE_FORMAT(S20_LE, 32, 20, 1, 1, SILENCE()), |
| 80 | DEFINE_FORMAT(S20_BE, 32, 20, 0, 1, SILENCE()), |
| 81 | DEFINE_FORMAT(U20_LE, 32, 20, 1, 0, SILENCE(0x00, 0x00, 0x08, 0x00)), |
| 82 | DEFINE_FORMAT(U20_BE, 32, 20, 0, 0, SILENCE(0x00, 0x08, 0x00, 0x00)), |
| 83 | DEFINE_FORMAT(S24_3LE, 24, 24, 1, 1, SILENCE()), |
| 84 | DEFINE_FORMAT(S24_3BE, 24, 24, 0, 1, SILENCE()), |
| 85 | DEFINE_FORMAT(U24_3LE, 24, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)), |
| 86 | DEFINE_FORMAT(U24_3BE, 24, 24, 0, 0, SILENCE(0x80, 0x00, 0x00)), |
| 87 | DEFINE_FORMAT(S20_3LE, 24, 20, 1, 1, SILENCE()), |
| 88 | DEFINE_FORMAT(S20_3BE, 24, 20, 0, 1, SILENCE()), |
| 89 | DEFINE_FORMAT(U20_3LE, 24, 20, 1, 0, SILENCE(0x00, 0x00, 0x08)), |
| 90 | DEFINE_FORMAT(U20_3BE, 24, 20, 0, 0, SILENCE(0x08, 0x00, 0x00)), |
| 91 | DEFINE_FORMAT(S18_3LE, 24, 18, 1, 1, SILENCE()), |
| 92 | DEFINE_FORMAT(S18_3BE, 24, 18, 0, 1, SILENCE()), |
| 93 | DEFINE_FORMAT(U18_3LE, 24, 18, 1, 0, SILENCE(0x00, 0x00, 0x02)), |
| 94 | DEFINE_FORMAT(U18_3BE, 24, 18, 0, 0, SILENCE(0x02, 0x00, 0x00)), |
| 95 | DEFINE_FORMAT(G723_24_1B, 8, 3, -1, -1, SILENCE()), |
| 96 | DEFINE_FORMAT(G723_40_1B, 8, 5, -1, -1, SILENCE()), |
| 97 | }; |
| 98 | |
| 99 | static void test_phys_format_size(struct kunit *test) |
| 100 | { |
| 101 | u32 i; |
| 102 | |
| 103 | for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { |
| 104 | KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(valid_fmt[i].format), |
| 105 | valid_fmt[i].physical_bits); |
| 106 | } |
| 107 | |
| 108 | KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_1), -EINVAL); |
| 109 | KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_2), -EINVAL); |
| 110 | } |
| 111 | |
| 112 | static void test_format_width(struct kunit *test) |
| 113 | { |
| 114 | u32 i; |
| 115 | |
| 116 | for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { |
| 117 | KUNIT_EXPECT_EQ(test, snd_pcm_format_width(valid_fmt[i].format), |
| 118 | valid_fmt[i].width); |
| 119 | } |
| 120 | |
| 121 | KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL); |
| 122 | KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL); |
| 123 | } |
| 124 | |
| 125 | static void test_format_signed(struct kunit *test) |
| 126 | { |
| 127 | u32 i; |
| 128 | |
| 129 | for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { |
| 130 | KUNIT_EXPECT_EQ(test, snd_pcm_format_signed(valid_fmt[i].format), |
| 131 | valid_fmt[i].sd < 0 ? -EINVAL : valid_fmt[i].sd); |
| 132 | KUNIT_EXPECT_EQ(test, snd_pcm_format_unsigned(valid_fmt[i].format), |
| 133 | valid_fmt[i].sd < 0 ? -EINVAL : 1 - valid_fmt[i].sd); |
| 134 | } |
| 135 | |
| 136 | KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL); |
| 137 | KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL); |
| 138 | } |
| 139 | |
| 140 | static void test_format_endianness(struct kunit *test) |
| 141 | { |
| 142 | u32 i; |
| 143 | |
| 144 | for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { |
| 145 | KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(valid_fmt[i].format), |
| 146 | valid_fmt[i].le < 0 ? -EINVAL : valid_fmt[i].le); |
| 147 | KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(valid_fmt[i].format), |
| 148 | valid_fmt[i].le < 0 ? -EINVAL : 1 - valid_fmt[i].le); |
| 149 | } |
| 150 | |
| 151 | KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_1), -EINVAL); |
| 152 | KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_2), -EINVAL); |
| 153 | KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_1), -EINVAL); |
| 154 | KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_2), -EINVAL); |
| 155 | } |
| 156 | |
| 157 | static void _test_fill_silence(struct kunit *test, const struct snd_format_test_data *data, |
| 158 | u8 *buffer, size_t samples_count) |
| 159 | { |
| 160 | size_t sample_bytes = data->physical_bits >> 3; |
| 161 | u32 i; |
| 162 | |
| 163 | KUNIT_ASSERT_EQ(test, snd_pcm_format_set_silence(data->format, buffer, samples_count), 0); |
| 164 | for (i = 0; i < samples_count * sample_bytes; i++) |
| 165 | KUNIT_EXPECT_EQ(test, buffer[i], data->silence[i % sample_bytes]); |
| 166 | } |
| 167 | |
| 168 | static void test_format_fill_silence(struct kunit *test) |
| 169 | { |
| 170 | static const u32 buf_samples[] = { 10, 20, 32, 64, 129, SILENCE_BUFFER_MAX_FRAMES }; |
| 171 | u8 *buffer; |
| 172 | u32 i, j; |
| 173 | |
| 174 | buffer = kunit_kzalloc(test, SILENCE_BUFFER_SIZE, GFP_KERNEL); |
| 175 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer); |
| 176 | |
| 177 | for (i = 0; i < ARRAY_SIZE(buf_samples); i++) { |
| 178 | for (j = 0; j < ARRAY_SIZE(valid_fmt); j++) |
| 179 | _test_fill_silence(test, data: &valid_fmt[j], buffer, samples_count: buf_samples[i]); |
| 180 | } |
| 181 | |
| 182 | KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT_1, buffer, 20), -EINVAL); |
| 183 | KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(SNDRV_PCM_FORMAT_LAST, buffer, 0), 0); |
| 184 | } |
| 185 | |
| 186 | static snd_pcm_uframes_t calculate_boundary(snd_pcm_uframes_t buffer_size) |
| 187 | { |
| 188 | snd_pcm_uframes_t boundary = buffer_size; |
| 189 | |
| 190 | while (boundary * 2 <= 0x7fffffffUL - buffer_size) |
| 191 | boundary *= 2; |
| 192 | return boundary; |
| 193 | } |
| 194 | |
| 195 | static const struct avail_test_data p_avail_data[] = { |
| 196 | /* buf_size + hw_ptr < appl_ptr => avail = buf_size + hw_ptr - appl_ptr + boundary */ |
| 197 | { 128, 1000, 1129, 1073741824UL - 1 }, |
| 198 | /* |
| 199 | * buf_size + hw_ptr - appl_ptr >= boundary => |
| 200 | * => avail = buf_size + hw_ptr - appl_ptr - boundary |
| 201 | */ |
| 202 | { 128, 1073741824UL, 10, 118 }, |
| 203 | /* standard case: avail = buf_size + hw_ptr - appl_ptr */ |
| 204 | { 128, 1000, 1001, 127 }, |
| 205 | }; |
| 206 | |
| 207 | static void test_playback_avail(struct kunit *test) |
| 208 | { |
| 209 | struct snd_pcm_runtime *r = kunit_kzalloc(test, size: sizeof(*r), GFP_KERNEL); |
| 210 | u32 i; |
| 211 | |
| 212 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r); |
| 213 | |
| 214 | r->status = kunit_kzalloc(test, size: sizeof(*r->status), GFP_KERNEL); |
| 215 | r->control = kunit_kzalloc(test, size: sizeof(*r->control), GFP_KERNEL); |
| 216 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->status); |
| 217 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->control); |
| 218 | |
| 219 | for (i = 0; i < ARRAY_SIZE(p_avail_data); i++) { |
| 220 | r->buffer_size = p_avail_data[i].buffer_size; |
| 221 | r->boundary = calculate_boundary(buffer_size: r->buffer_size); |
| 222 | r->status->hw_ptr = p_avail_data[i].hw_ptr; |
| 223 | r->control->appl_ptr = p_avail_data[i].appl_ptr; |
| 224 | KUNIT_EXPECT_EQ(test, snd_pcm_playback_avail(r), p_avail_data[i].expected_avail); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static const struct avail_test_data c_avail_data[] = { |
| 229 | /* hw_ptr - appl_ptr < 0 => avail = hw_ptr - appl_ptr + boundary */ |
| 230 | { 128, 1000, 1001, 1073741824UL - 1 }, |
| 231 | /* standard case: avail = hw_ptr - appl_ptr */ |
| 232 | { 128, 1001, 1000, 1 }, |
| 233 | }; |
| 234 | |
| 235 | static void test_capture_avail(struct kunit *test) |
| 236 | { |
| 237 | struct snd_pcm_runtime *r = kunit_kzalloc(test, size: sizeof(*r), GFP_KERNEL); |
| 238 | u32 i; |
| 239 | |
| 240 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r); |
| 241 | |
| 242 | r->status = kunit_kzalloc(test, size: sizeof(*r->status), GFP_KERNEL); |
| 243 | r->control = kunit_kzalloc(test, size: sizeof(*r->control), GFP_KERNEL); |
| 244 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->status); |
| 245 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->control); |
| 246 | |
| 247 | for (i = 0; i < ARRAY_SIZE(c_avail_data); i++) { |
| 248 | r->buffer_size = c_avail_data[i].buffer_size; |
| 249 | r->boundary = calculate_boundary(buffer_size: r->buffer_size); |
| 250 | r->status->hw_ptr = c_avail_data[i].hw_ptr; |
| 251 | r->control->appl_ptr = c_avail_data[i].appl_ptr; |
| 252 | KUNIT_EXPECT_EQ(test, snd_pcm_capture_avail(r), c_avail_data[i].expected_avail); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | static void test_card_set_id(struct kunit *test) |
| 257 | { |
| 258 | struct snd_card *card = kunit_kzalloc(test, size: sizeof(*card), GFP_KERNEL); |
| 259 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, card); |
| 260 | |
| 261 | snd_card_set_id(card, VALID_NAME); |
| 262 | KUNIT_EXPECT_STREQ(test, card->id, VALID_NAME); |
| 263 | |
| 264 | /* clear the first id character so we can set it again */ |
| 265 | card->id[0] = '\0'; |
| 266 | snd_card_set_id(card, NAME_W_SPEC_CHARS); |
| 267 | KUNIT_EXPECT_STRNEQ(test, card->id, NAME_W_SPEC_CHARS); |
| 268 | |
| 269 | card->id[0] = '\0'; |
| 270 | snd_card_set_id(card, NAME_W_SPACE); |
| 271 | kunit_info(test, "%s" , card->id); |
| 272 | KUNIT_EXPECT_STREQ(test, card->id, NAME_W_SPACE_REMOVED); |
| 273 | } |
| 274 | |
| 275 | static void test_pcm_format_name(struct kunit *test) |
| 276 | { |
| 277 | u32 i; |
| 278 | const char *name; |
| 279 | |
| 280 | for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { |
| 281 | name = snd_pcm_format_name(format: valid_fmt[i].format); |
| 282 | KUNIT_ASSERT_NOT_NULL_MSG(test, name, "Don't have name for %s" , valid_fmt[i].name); |
| 283 | KUNIT_EXPECT_STREQ(test, name, valid_fmt[i].name); |
| 284 | } |
| 285 | |
| 286 | KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_1), "Unknown" ); |
| 287 | KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_2), "Unknown" ); |
| 288 | } |
| 289 | |
| 290 | static void test_card_add_component(struct kunit *test) |
| 291 | { |
| 292 | struct snd_card *card = kunit_kzalloc(test, size: sizeof(*card), GFP_KERNEL); |
| 293 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, card); |
| 294 | |
| 295 | snd_component_add(card, TEST_FIRST_COMPONENT); |
| 296 | KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT); |
| 297 | |
| 298 | snd_component_add(card, TEST_SECOND_COMPONENT); |
| 299 | KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT " " TEST_SECOND_COMPONENT); |
| 300 | } |
| 301 | |
| 302 | static struct kunit_case sound_utils_cases[] = { |
| 303 | KUNIT_CASE(test_phys_format_size), |
| 304 | KUNIT_CASE(test_format_width), |
| 305 | KUNIT_CASE(test_format_endianness), |
| 306 | KUNIT_CASE(test_format_signed), |
| 307 | KUNIT_CASE(test_format_fill_silence), |
| 308 | KUNIT_CASE(test_playback_avail), |
| 309 | KUNIT_CASE(test_capture_avail), |
| 310 | KUNIT_CASE(test_card_set_id), |
| 311 | KUNIT_CASE(test_pcm_format_name), |
| 312 | KUNIT_CASE(test_card_add_component), |
| 313 | {}, |
| 314 | }; |
| 315 | |
| 316 | static struct kunit_suite sound_utils_suite = { |
| 317 | .name = "sound-core-test" , |
| 318 | .test_cases = sound_utils_cases, |
| 319 | }; |
| 320 | |
| 321 | kunit_test_suite(sound_utils_suite); |
| 322 | MODULE_DESCRIPTION("Sound core KUnit test" ); |
| 323 | MODULE_AUTHOR("Ivan Orlov" ); |
| 324 | MODULE_LICENSE("GPL" ); |
| 325 | |