1 // SPDX-License-Identifier: GPL-2.0
3 // kselftest for the ALSA mixer API
5 // Original author: Mark Brown <broonie@kernel.org>
6 // Copyright (c) 2021 Arm Limited
8 // This test will iterate over all cards detected in the system, exercising
9 // every mixer control it can find. This may conflict with other system
10 // software if there is audio activity so is best run on a system with a
11 // minimal active userspace.
24 #include <alsa/asoundlib.h>
28 #include "../kselftest.h"
30 #define TESTS_PER_CONTROL 4
36 snd_ctl_elem_list_t *ctls;
37 struct card_data *next;
42 snd_ctl_elem_id_t *id;
43 snd_ctl_elem_info_t *info;
44 snd_ctl_elem_value_t *def_val;
46 struct card_data *card;
47 struct ctl_data *next;
50 static const char *alsa_config =
53 " @args.CARD.type string\n"
61 struct card_data *card_list = NULL;
62 struct ctl_data *ctl_list = NULL;
65 #if SND_LIB_VERSION >= SND_LIB_VER(1, 2, 6)
66 #define LIB_HAS_LOAD_STRING
70 #ifndef LIB_HAS_LOAD_STRING
71 int snd_config_load_string(snd_config_t **config, const char *s, size_t size)
80 err = snd_input_buffer_open(&input, s, size);
83 err = snd_config_top(&dst);
85 snd_input_close(input);
88 err = snd_config_load(dst, input);
89 snd_input_close(input);
91 snd_config_delete(dst);
99 void find_controls(void)
103 struct card_data *card_data;
104 struct ctl_data *ctl_data;
105 snd_config_t *config;
108 if (snd_card_next(&card) < 0 || card < 0)
111 err = snd_config_load_string(&config, alsa_config, strlen(alsa_config));
113 ksft_print_msg("Unable to parse custom alsa-lib configuration: %s\n",
119 sprintf(name, "hw:%d", card);
121 card_data = malloc(sizeof(*card_data));
123 ksft_exit_fail_msg("Out of memory\n");
125 err = snd_ctl_open_lconf(&card_data->handle, name, 0, config);
127 ksft_print_msg("Failed to get hctl for card %d: %s\n",
128 card, snd_strerror(err));
133 snd_ctl_elem_list_malloc(&card_data->ctls);
134 snd_ctl_elem_list(card_data->handle, card_data->ctls);
135 card_data->num_ctls = snd_ctl_elem_list_get_count(card_data->ctls);
137 /* Enumerate control information */
138 snd_ctl_elem_list_alloc_space(card_data->ctls, card_data->num_ctls);
139 snd_ctl_elem_list(card_data->handle, card_data->ctls);
141 card_data->card = num_cards++;
142 card_data->next = card_list;
143 card_list = card_data;
145 num_controls += card_data->num_ctls;
147 for (ctl = 0; ctl < card_data->num_ctls; ctl++) {
148 ctl_data = malloc(sizeof(*ctl_data));
150 ksft_exit_fail_msg("Out of memory\n");
152 ctl_data->card = card_data;
153 ctl_data->elem = ctl;
154 ctl_data->name = snd_ctl_elem_list_get_name(card_data->ctls,
157 err = snd_ctl_elem_id_malloc(&ctl_data->id);
159 ksft_exit_fail_msg("Out of memory\n");
161 err = snd_ctl_elem_info_malloc(&ctl_data->info);
163 ksft_exit_fail_msg("Out of memory\n");
165 err = snd_ctl_elem_value_malloc(&ctl_data->def_val);
167 ksft_exit_fail_msg("Out of memory\n");
169 snd_ctl_elem_list_get_id(card_data->ctls, ctl,
171 snd_ctl_elem_info_set_id(ctl_data->info, ctl_data->id);
172 err = snd_ctl_elem_info(card_data->handle,
175 ksft_print_msg("%s getting info for %d\n",
180 snd_ctl_elem_value_set_id(ctl_data->def_val,
183 ctl_data->next = ctl_list;
188 if (snd_card_next(&card) < 0) {
189 ksft_print_msg("snd_card_next");
194 snd_config_delete(config);
197 bool ctl_value_index_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val,
203 switch (snd_ctl_elem_info_get_type(ctl->info)) {
204 case SND_CTL_ELEM_TYPE_NONE:
205 ksft_print_msg("%s.%d Invalid control type NONE\n",
209 case SND_CTL_ELEM_TYPE_BOOLEAN:
210 int_val = snd_ctl_elem_value_get_boolean(val, index);
216 ksft_print_msg("%s.%d Invalid boolean value %ld\n",
217 ctl->name, index, int_val);
222 case SND_CTL_ELEM_TYPE_INTEGER:
223 int_val = snd_ctl_elem_value_get_integer(val, index);
225 if (int_val < snd_ctl_elem_info_get_min(ctl->info)) {
226 ksft_print_msg("%s.%d value %ld less than minimum %ld\n",
227 ctl->name, index, int_val,
228 snd_ctl_elem_info_get_min(ctl->info));
232 if (int_val > snd_ctl_elem_info_get_max(ctl->info)) {
233 ksft_print_msg("%s.%d value %ld more than maximum %ld\n",
234 ctl->name, index, int_val,
235 snd_ctl_elem_info_get_max(ctl->info));
239 /* Only check step size if there is one and we're in bounds */
240 if (snd_ctl_elem_info_get_step(ctl->info) &&
241 (int_val - snd_ctl_elem_info_get_min(ctl->info) %
242 snd_ctl_elem_info_get_step(ctl->info))) {
243 ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n",
244 ctl->name, index, int_val,
245 snd_ctl_elem_info_get_step(ctl->info),
246 snd_ctl_elem_info_get_min(ctl->info));
251 case SND_CTL_ELEM_TYPE_INTEGER64:
252 int64_val = snd_ctl_elem_value_get_integer64(val, index);
254 if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) {
255 ksft_print_msg("%s.%d value %lld less than minimum %lld\n",
256 ctl->name, index, int64_val,
257 snd_ctl_elem_info_get_min64(ctl->info));
261 if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) {
262 ksft_print_msg("%s.%d value %lld more than maximum %lld\n",
263 ctl->name, index, int64_val,
264 snd_ctl_elem_info_get_max(ctl->info));
268 /* Only check step size if there is one and we're in bounds */
269 if (snd_ctl_elem_info_get_step64(ctl->info) &&
270 (int64_val - snd_ctl_elem_info_get_min64(ctl->info)) %
271 snd_ctl_elem_info_get_step64(ctl->info)) {
272 ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n",
273 ctl->name, index, int64_val,
274 snd_ctl_elem_info_get_step64(ctl->info),
275 snd_ctl_elem_info_get_min64(ctl->info));
280 case SND_CTL_ELEM_TYPE_ENUMERATED:
281 int_val = snd_ctl_elem_value_get_enumerated(val, index);
284 ksft_print_msg("%s.%d negative value %ld for enumeration\n",
285 ctl->name, index, int_val);
289 if (int_val >= snd_ctl_elem_info_get_items(ctl->info)) {
290 ksft_print_msg("%s.%d value %ld more than item count %ld\n",
291 ctl->name, index, int_val,
292 snd_ctl_elem_info_get_items(ctl->info));
298 /* No tests for other types */
306 * Check that the provided value meets the constraints for the
309 bool ctl_value_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val)
314 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
315 if (!ctl_value_index_valid(ctl, val, i))
322 * Check that we can read the default value and it is valid. Write
323 * tests use the read value to restore the default.
325 void test_ctl_get_value(struct ctl_data *ctl)
329 /* If the control is turned off let's be polite */
330 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
331 ksft_print_msg("%s is inactive\n", ctl->name);
332 ksft_test_result_skip("get_value.%d.%d\n",
333 ctl->card->card, ctl->elem);
337 /* Can't test reading on an unreadable control */
338 if (!snd_ctl_elem_info_is_readable(ctl->info)) {
339 ksft_print_msg("%s is not readable\n", ctl->name);
340 ksft_test_result_skip("get_value.%d.%d\n",
341 ctl->card->card, ctl->elem);
345 err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val);
347 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
352 if (!ctl_value_valid(ctl, ctl->def_val))
356 ksft_test_result(err >= 0, "get_value.%d.%d\n",
357 ctl->card->card, ctl->elem);
360 bool show_mismatch(struct ctl_data *ctl, int index,
361 snd_ctl_elem_value_t *read_val,
362 snd_ctl_elem_value_t *expected_val)
364 long long expected_int, read_int;
367 * We factor out the code to compare values representable as
368 * integers, ensure that check doesn't log otherwise.
373 switch (snd_ctl_elem_info_get_type(ctl->info)) {
374 case SND_CTL_ELEM_TYPE_BOOLEAN:
375 expected_int = snd_ctl_elem_value_get_boolean(expected_val,
377 read_int = snd_ctl_elem_value_get_boolean(read_val, index);
380 case SND_CTL_ELEM_TYPE_INTEGER:
381 expected_int = snd_ctl_elem_value_get_integer(expected_val,
383 read_int = snd_ctl_elem_value_get_integer(read_val, index);
386 case SND_CTL_ELEM_TYPE_INTEGER64:
387 expected_int = snd_ctl_elem_value_get_integer64(expected_val,
389 read_int = snd_ctl_elem_value_get_integer64(read_val,
393 case SND_CTL_ELEM_TYPE_ENUMERATED:
394 expected_int = snd_ctl_elem_value_get_enumerated(expected_val,
396 read_int = snd_ctl_elem_value_get_enumerated(read_val,
404 if (expected_int != read_int) {
406 * NOTE: The volatile attribute means that the hardware
407 * can voluntarily change the state of control element
408 * independent of any operation by software.
410 bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info);
411 ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n",
412 ctl->name, index, expected_int, read_int, is_volatile);
420 * Write a value then if possible verify that we get the expected
421 * result. An optional expected value can be provided if we expect
422 * the write to fail, for verifying that invalid writes don't corrupt
425 int write_and_verify(struct ctl_data *ctl,
426 snd_ctl_elem_value_t *write_val,
427 snd_ctl_elem_value_t *expected_val)
430 bool error_expected, mismatch_shown;
431 snd_ctl_elem_value_t *read_val, *w_val;
432 snd_ctl_elem_value_alloca(&read_val);
433 snd_ctl_elem_value_alloca(&w_val);
436 * We need to copy the write value since writing can modify
437 * the value which causes surprises, and allocate an expected
438 * value if we expect to read back what we wrote.
440 snd_ctl_elem_value_copy(w_val, write_val);
442 error_expected = true;
444 error_expected = false;
445 snd_ctl_elem_value_alloca(&expected_val);
446 snd_ctl_elem_value_copy(expected_val, write_val);
450 * Do the write, if we have an expected value ignore the error
451 * and carry on to validate the expected value.
453 err = snd_ctl_elem_write(ctl->card->handle, w_val);
454 if (err < 0 && !error_expected) {
455 ksft_print_msg("snd_ctl_elem_write() failed: %s\n",
460 /* Can we do the verification part? */
461 if (!snd_ctl_elem_info_is_readable(ctl->info))
464 snd_ctl_elem_value_set_id(read_val, ctl->id);
466 err = snd_ctl_elem_read(ctl->card->handle, read_val);
468 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
474 * Use the libray to compare values, if there's a mismatch
475 * carry on and try to provide a more useful diagnostic than
478 if (!snd_ctl_elem_value_compare(expected_val, read_val))
481 mismatch_shown = false;
482 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
483 if (show_mismatch(ctl, i, read_val, expected_val))
484 mismatch_shown = true;
487 ksft_print_msg("%s read and written values differ\n",
494 * Make sure we can write the default value back to the control, this
495 * should validate that at least some write works.
497 void test_ctl_write_default(struct ctl_data *ctl)
501 /* If the control is turned off let's be polite */
502 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
503 ksft_print_msg("%s is inactive\n", ctl->name);
504 ksft_test_result_skip("write_default.%d.%d\n",
505 ctl->card->card, ctl->elem);
509 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
510 ksft_print_msg("%s is not writeable\n", ctl->name);
511 ksft_test_result_skip("write_default.%d.%d\n",
512 ctl->card->card, ctl->elem);
516 /* No idea what the default was for unreadable controls */
517 if (!snd_ctl_elem_info_is_readable(ctl->info)) {
518 ksft_print_msg("%s couldn't read default\n", ctl->name);
519 ksft_test_result_skip("write_default.%d.%d\n",
520 ctl->card->card, ctl->elem);
524 err = write_and_verify(ctl, ctl->def_val, NULL);
526 ksft_test_result(err >= 0, "write_default.%d.%d\n",
527 ctl->card->card, ctl->elem);
530 bool test_ctl_write_valid_boolean(struct ctl_data *ctl)
534 snd_ctl_elem_value_t *val;
535 snd_ctl_elem_value_alloca(&val);
537 snd_ctl_elem_value_set_id(val, ctl->id);
539 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
540 for (j = 0; j < 2; j++) {
541 snd_ctl_elem_value_set_boolean(val, i, j);
542 err = write_and_verify(ctl, val, NULL);
551 bool test_ctl_write_valid_integer(struct ctl_data *ctl)
557 snd_ctl_elem_value_t *val;
558 snd_ctl_elem_value_alloca(&val);
560 snd_ctl_elem_value_set_id(val, ctl->id);
562 step = snd_ctl_elem_info_get_step(ctl->info);
566 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
567 for (j = snd_ctl_elem_info_get_min(ctl->info);
568 j <= snd_ctl_elem_info_get_max(ctl->info); j += step) {
570 snd_ctl_elem_value_set_integer(val, i, j);
571 err = write_and_verify(ctl, val, NULL);
581 bool test_ctl_write_valid_integer64(struct ctl_data *ctl)
586 snd_ctl_elem_value_t *val;
587 snd_ctl_elem_value_alloca(&val);
589 snd_ctl_elem_value_set_id(val, ctl->id);
591 step = snd_ctl_elem_info_get_step64(ctl->info);
595 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
596 for (j = snd_ctl_elem_info_get_min64(ctl->info);
597 j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) {
599 snd_ctl_elem_value_set_integer64(val, i, j);
600 err = write_and_verify(ctl, val, NULL);
609 bool test_ctl_write_valid_enumerated(struct ctl_data *ctl)
613 snd_ctl_elem_value_t *val;
614 snd_ctl_elem_value_alloca(&val);
616 snd_ctl_elem_value_set_id(val, ctl->id);
618 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
619 for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) {
620 snd_ctl_elem_value_set_enumerated(val, i, j);
621 err = write_and_verify(ctl, val, NULL);
630 void test_ctl_write_valid(struct ctl_data *ctl)
635 /* If the control is turned off let's be polite */
636 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
637 ksft_print_msg("%s is inactive\n", ctl->name);
638 ksft_test_result_skip("write_valid.%d.%d\n",
639 ctl->card->card, ctl->elem);
643 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
644 ksft_print_msg("%s is not writeable\n", ctl->name);
645 ksft_test_result_skip("write_valid.%d.%d\n",
646 ctl->card->card, ctl->elem);
650 switch (snd_ctl_elem_info_get_type(ctl->info)) {
651 case SND_CTL_ELEM_TYPE_BOOLEAN:
652 pass = test_ctl_write_valid_boolean(ctl);
655 case SND_CTL_ELEM_TYPE_INTEGER:
656 pass = test_ctl_write_valid_integer(ctl);
659 case SND_CTL_ELEM_TYPE_INTEGER64:
660 pass = test_ctl_write_valid_integer64(ctl);
663 case SND_CTL_ELEM_TYPE_ENUMERATED:
664 pass = test_ctl_write_valid_enumerated(ctl);
668 /* No tests for this yet */
669 ksft_test_result_skip("write_valid.%d.%d\n",
670 ctl->card->card, ctl->elem);
674 /* Restore the default value to minimise disruption */
675 err = write_and_verify(ctl, ctl->def_val, NULL);
679 ksft_test_result(pass, "write_valid.%d.%d\n",
680 ctl->card->card, ctl->elem);
683 bool test_ctl_write_invalid_value(struct ctl_data *ctl,
684 snd_ctl_elem_value_t *val)
689 /* Ideally this will fail... */
690 err = snd_ctl_elem_write(ctl->card->handle, val);
694 /* ...but some devices will clamp to an in range value */
695 err = snd_ctl_elem_read(ctl->card->handle, val);
697 ksft_print_msg("%s failed to read: %s\n",
698 ctl->name, snd_strerror(err));
702 return !ctl_value_valid(ctl, val);
705 bool test_ctl_write_invalid_boolean(struct ctl_data *ctl)
710 snd_ctl_elem_value_t *val;
711 snd_ctl_elem_value_alloca(&val);
713 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
714 snd_ctl_elem_value_copy(val, ctl->def_val);
715 snd_ctl_elem_value_set_boolean(val, i, 2);
717 if (test_ctl_write_invalid_value(ctl, val))
724 bool test_ctl_write_invalid_integer(struct ctl_data *ctl)
728 snd_ctl_elem_value_t *val;
729 snd_ctl_elem_value_alloca(&val);
731 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
732 if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) {
733 /* Just under range */
734 snd_ctl_elem_value_copy(val, ctl->def_val);
735 snd_ctl_elem_value_set_integer(val, i,
736 snd_ctl_elem_info_get_min(ctl->info) - 1);
738 if (test_ctl_write_invalid_value(ctl, val))
741 /* Minimum representable value */
742 snd_ctl_elem_value_copy(val, ctl->def_val);
743 snd_ctl_elem_value_set_integer(val, i, LONG_MIN);
745 if (test_ctl_write_invalid_value(ctl, val))
749 if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) {
750 /* Just over range */
751 snd_ctl_elem_value_copy(val, ctl->def_val);
752 snd_ctl_elem_value_set_integer(val, i,
753 snd_ctl_elem_info_get_max(ctl->info) + 1);
755 if (test_ctl_write_invalid_value(ctl, val))
758 /* Maximum representable value */
759 snd_ctl_elem_value_copy(val, ctl->def_val);
760 snd_ctl_elem_value_set_integer(val, i, LONG_MAX);
762 if (test_ctl_write_invalid_value(ctl, val))
770 bool test_ctl_write_invalid_integer64(struct ctl_data *ctl)
774 snd_ctl_elem_value_t *val;
775 snd_ctl_elem_value_alloca(&val);
777 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
778 if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) {
779 /* Just under range */
780 snd_ctl_elem_value_copy(val, ctl->def_val);
781 snd_ctl_elem_value_set_integer64(val, i,
782 snd_ctl_elem_info_get_min64(ctl->info) - 1);
784 if (test_ctl_write_invalid_value(ctl, val))
787 /* Minimum representable value */
788 snd_ctl_elem_value_copy(val, ctl->def_val);
789 snd_ctl_elem_value_set_integer64(val, i, LLONG_MIN);
791 if (test_ctl_write_invalid_value(ctl, val))
795 if (snd_ctl_elem_info_get_max64(ctl->info) != LLONG_MAX) {
796 /* Just over range */
797 snd_ctl_elem_value_copy(val, ctl->def_val);
798 snd_ctl_elem_value_set_integer64(val, i,
799 snd_ctl_elem_info_get_max64(ctl->info) + 1);
801 if (test_ctl_write_invalid_value(ctl, val))
804 /* Maximum representable value */
805 snd_ctl_elem_value_copy(val, ctl->def_val);
806 snd_ctl_elem_value_set_integer64(val, i, LLONG_MAX);
808 if (test_ctl_write_invalid_value(ctl, val))
816 bool test_ctl_write_invalid_enumerated(struct ctl_data *ctl)
819 unsigned int val_read;
821 snd_ctl_elem_value_t *val;
822 snd_ctl_elem_value_alloca(&val);
824 snd_ctl_elem_value_set_id(val, ctl->id);
826 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
827 /* One beyond maximum */
828 snd_ctl_elem_value_copy(val, ctl->def_val);
829 snd_ctl_elem_value_set_enumerated(val, i,
830 snd_ctl_elem_info_get_items(ctl->info));
832 if (test_ctl_write_invalid_value(ctl, val))
835 /* Maximum representable value */
836 snd_ctl_elem_value_copy(val, ctl->def_val);
837 snd_ctl_elem_value_set_enumerated(val, i, UINT_MAX);
839 if (test_ctl_write_invalid_value(ctl, val))
848 void test_ctl_write_invalid(struct ctl_data *ctl)
853 /* If the control is turned off let's be polite */
854 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
855 ksft_print_msg("%s is inactive\n", ctl->name);
856 ksft_test_result_skip("write_invalid.%d.%d\n",
857 ctl->card->card, ctl->elem);
861 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
862 ksft_print_msg("%s is not writeable\n", ctl->name);
863 ksft_test_result_skip("write_invalid.%d.%d\n",
864 ctl->card->card, ctl->elem);
868 switch (snd_ctl_elem_info_get_type(ctl->info)) {
869 case SND_CTL_ELEM_TYPE_BOOLEAN:
870 pass = test_ctl_write_invalid_boolean(ctl);
873 case SND_CTL_ELEM_TYPE_INTEGER:
874 pass = test_ctl_write_invalid_integer(ctl);
877 case SND_CTL_ELEM_TYPE_INTEGER64:
878 pass = test_ctl_write_invalid_integer64(ctl);
881 case SND_CTL_ELEM_TYPE_ENUMERATED:
882 pass = test_ctl_write_invalid_enumerated(ctl);
886 /* No tests for this yet */
887 ksft_test_result_skip("write_invalid.%d.%d\n",
888 ctl->card->card, ctl->elem);
892 /* Restore the default value to minimise disruption */
893 err = write_and_verify(ctl, ctl->def_val, NULL);
897 ksft_test_result(pass, "write_invalid.%d.%d\n",
898 ctl->card->card, ctl->elem);
903 struct ctl_data *ctl;
909 ksft_set_plan(num_controls * TESTS_PER_CONTROL);
911 for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) {
913 * Must test get_value() before we write anything, the
914 * test stores the default value for later cleanup.
916 test_ctl_get_value(ctl);
917 test_ctl_write_default(ctl);
918 test_ctl_write_valid(ctl);
919 test_ctl_write_invalid(ctl);