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-2 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 6
37 snd_ctl_elem_list_t *ctls;
38 struct card_data *next;
43 snd_ctl_elem_id_t *id;
44 snd_ctl_elem_info_t *info;
45 snd_ctl_elem_value_t *def_val;
49 struct card_data *card;
50 struct ctl_data *next;
53 static const char *alsa_config =
56 " @args.CARD.type string\n"
64 struct card_data *card_list = NULL;
65 struct ctl_data *ctl_list = NULL;
68 #if SND_LIB_VERSION >= SND_LIB_VER(1, 2, 6)
69 #define LIB_HAS_LOAD_STRING
73 #ifndef LIB_HAS_LOAD_STRING
74 static int snd_config_load_string(snd_config_t **config, const char *s,
84 err = snd_input_buffer_open(&input, s, size);
87 err = snd_config_top(&dst);
89 snd_input_close(input);
92 err = snd_config_load(dst, input);
93 snd_input_close(input);
95 snd_config_delete(dst);
103 static void find_controls(void)
107 struct card_data *card_data;
108 struct ctl_data *ctl_data;
109 snd_config_t *config;
112 if (snd_card_next(&card) < 0 || card < 0)
115 err = snd_config_load_string(&config, alsa_config, strlen(alsa_config));
117 ksft_print_msg("Unable to parse custom alsa-lib configuration: %s\n",
123 sprintf(name, "hw:%d", card);
125 card_data = malloc(sizeof(*card_data));
127 ksft_exit_fail_msg("Out of memory\n");
129 err = snd_ctl_open_lconf(&card_data->handle, name, 0, config);
131 ksft_print_msg("Failed to get hctl for card %d: %s\n",
132 card, snd_strerror(err));
137 snd_ctl_elem_list_malloc(&card_data->ctls);
138 snd_ctl_elem_list(card_data->handle, card_data->ctls);
139 card_data->num_ctls = snd_ctl_elem_list_get_count(card_data->ctls);
141 /* Enumerate control information */
142 snd_ctl_elem_list_alloc_space(card_data->ctls, card_data->num_ctls);
143 snd_ctl_elem_list(card_data->handle, card_data->ctls);
145 card_data->card = num_cards++;
146 card_data->next = card_list;
147 card_list = card_data;
149 num_controls += card_data->num_ctls;
151 for (ctl = 0; ctl < card_data->num_ctls; ctl++) {
152 ctl_data = malloc(sizeof(*ctl_data));
154 ksft_exit_fail_msg("Out of memory\n");
156 memset(ctl_data, 0, sizeof(*ctl_data));
157 ctl_data->card = card_data;
158 ctl_data->elem = ctl;
159 ctl_data->name = snd_ctl_elem_list_get_name(card_data->ctls,
162 err = snd_ctl_elem_id_malloc(&ctl_data->id);
164 ksft_exit_fail_msg("Out of memory\n");
166 err = snd_ctl_elem_info_malloc(&ctl_data->info);
168 ksft_exit_fail_msg("Out of memory\n");
170 err = snd_ctl_elem_value_malloc(&ctl_data->def_val);
172 ksft_exit_fail_msg("Out of memory\n");
174 snd_ctl_elem_list_get_id(card_data->ctls, ctl,
176 snd_ctl_elem_info_set_id(ctl_data->info, ctl_data->id);
177 err = snd_ctl_elem_info(card_data->handle,
180 ksft_print_msg("%s getting info for %d\n",
185 snd_ctl_elem_value_set_id(ctl_data->def_val,
188 ctl_data->next = ctl_list;
192 /* Set up for events */
193 err = snd_ctl_subscribe_events(card_data->handle, true);
195 ksft_exit_fail_msg("snd_ctl_subscribe_events() failed for card %d: %d\n",
199 err = snd_ctl_poll_descriptors_count(card_data->handle);
201 ksft_exit_fail_msg("Unexpected desciptor count %d for card %d\n",
205 err = snd_ctl_poll_descriptors(card_data->handle,
206 &card_data->pollfd, 1);
208 ksft_exit_fail_msg("snd_ctl_poll_descriptors() failed for %d\n",
213 if (snd_card_next(&card) < 0) {
214 ksft_print_msg("snd_card_next");
219 snd_config_delete(config);
223 * Block for up to timeout ms for an event, returns a negative value
224 * on error, 0 for no event and 1 for an event.
226 static int wait_for_event(struct ctl_data *ctl, int timeout)
228 unsigned short revents;
229 snd_ctl_event_t *event;
231 unsigned int mask = 0;
234 snd_ctl_event_alloca(&event);
237 err = poll(&(ctl->card->pollfd), 1, timeout);
239 ksft_print_msg("poll() failed for %s: %s (%d)\n",
240 ctl->name, strerror(errno), errno);
247 err = snd_ctl_poll_descriptors_revents(ctl->card->handle,
248 &(ctl->card->pollfd),
251 ksft_print_msg("snd_ctl_poll_desciptors_revents() failed for %s: %d\n",
255 if (revents & POLLERR) {
256 ksft_print_msg("snd_ctl_poll_desciptors_revents() reported POLLERR for %s\n",
261 if (!(revents & POLLIN)) {
262 ksft_print_msg("No POLLIN\n");
266 err = snd_ctl_read(ctl->card->handle, event);
268 ksft_print_msg("snd_ctl_read() failed for %s: %d\n",
273 if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM)
276 /* The ID returned from the event is 1 less than numid */
277 mask = snd_ctl_event_elem_get_mask(event);
278 ev_id = snd_ctl_event_elem_get_numid(event);
279 if (ev_id != snd_ctl_elem_info_get_numid(ctl->info)) {
280 ksft_print_msg("Event for unexpected ctl %s\n",
281 snd_ctl_event_elem_get_name(event));
285 if ((mask & SND_CTL_EVENT_MASK_REMOVE) == SND_CTL_EVENT_MASK_REMOVE) {
286 ksft_print_msg("Removal event for %s\n",
290 } while ((mask & SND_CTL_EVENT_MASK_VALUE) != SND_CTL_EVENT_MASK_VALUE);
295 static bool ctl_value_index_valid(struct ctl_data *ctl,
296 snd_ctl_elem_value_t *val,
302 switch (snd_ctl_elem_info_get_type(ctl->info)) {
303 case SND_CTL_ELEM_TYPE_NONE:
304 ksft_print_msg("%s.%d Invalid control type NONE\n",
308 case SND_CTL_ELEM_TYPE_BOOLEAN:
309 int_val = snd_ctl_elem_value_get_boolean(val, index);
315 ksft_print_msg("%s.%d Invalid boolean value %ld\n",
316 ctl->name, index, int_val);
321 case SND_CTL_ELEM_TYPE_INTEGER:
322 int_val = snd_ctl_elem_value_get_integer(val, index);
324 if (int_val < snd_ctl_elem_info_get_min(ctl->info)) {
325 ksft_print_msg("%s.%d value %ld less than minimum %ld\n",
326 ctl->name, index, int_val,
327 snd_ctl_elem_info_get_min(ctl->info));
331 if (int_val > snd_ctl_elem_info_get_max(ctl->info)) {
332 ksft_print_msg("%s.%d value %ld more than maximum %ld\n",
333 ctl->name, index, int_val,
334 snd_ctl_elem_info_get_max(ctl->info));
338 /* Only check step size if there is one and we're in bounds */
339 if (snd_ctl_elem_info_get_step(ctl->info) &&
340 (int_val - snd_ctl_elem_info_get_min(ctl->info) %
341 snd_ctl_elem_info_get_step(ctl->info))) {
342 ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n",
343 ctl->name, index, int_val,
344 snd_ctl_elem_info_get_step(ctl->info),
345 snd_ctl_elem_info_get_min(ctl->info));
350 case SND_CTL_ELEM_TYPE_INTEGER64:
351 int64_val = snd_ctl_elem_value_get_integer64(val, index);
353 if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) {
354 ksft_print_msg("%s.%d value %lld less than minimum %lld\n",
355 ctl->name, index, int64_val,
356 snd_ctl_elem_info_get_min64(ctl->info));
360 if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) {
361 ksft_print_msg("%s.%d value %lld more than maximum %lld\n",
362 ctl->name, index, int64_val,
363 snd_ctl_elem_info_get_max(ctl->info));
367 /* Only check step size if there is one and we're in bounds */
368 if (snd_ctl_elem_info_get_step64(ctl->info) &&
369 (int64_val - snd_ctl_elem_info_get_min64(ctl->info)) %
370 snd_ctl_elem_info_get_step64(ctl->info)) {
371 ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n",
372 ctl->name, index, int64_val,
373 snd_ctl_elem_info_get_step64(ctl->info),
374 snd_ctl_elem_info_get_min64(ctl->info));
379 case SND_CTL_ELEM_TYPE_ENUMERATED:
380 int_val = snd_ctl_elem_value_get_enumerated(val, index);
383 ksft_print_msg("%s.%d negative value %ld for enumeration\n",
384 ctl->name, index, int_val);
388 if (int_val >= snd_ctl_elem_info_get_items(ctl->info)) {
389 ksft_print_msg("%s.%d value %ld more than item count %ld\n",
390 ctl->name, index, int_val,
391 snd_ctl_elem_info_get_items(ctl->info));
397 /* No tests for other types */
405 * Check that the provided value meets the constraints for the
408 static bool ctl_value_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val)
413 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
414 if (!ctl_value_index_valid(ctl, val, i))
421 * Check that we can read the default value and it is valid. Write
422 * tests use the read value to restore the default.
424 static void test_ctl_get_value(struct ctl_data *ctl)
428 /* If the control is turned off let's be polite */
429 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
430 ksft_print_msg("%s is inactive\n", ctl->name);
431 ksft_test_result_skip("get_value.%d.%d\n",
432 ctl->card->card, ctl->elem);
436 /* Can't test reading on an unreadable control */
437 if (!snd_ctl_elem_info_is_readable(ctl->info)) {
438 ksft_print_msg("%s is not readable\n", ctl->name);
439 ksft_test_result_skip("get_value.%d.%d\n",
440 ctl->card->card, ctl->elem);
444 err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val);
446 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
451 if (!ctl_value_valid(ctl, ctl->def_val))
455 ksft_test_result(err >= 0, "get_value.%d.%d\n",
456 ctl->card->card, ctl->elem);
459 static bool show_mismatch(struct ctl_data *ctl, int index,
460 snd_ctl_elem_value_t *read_val,
461 snd_ctl_elem_value_t *expected_val)
463 long long expected_int, read_int;
466 * We factor out the code to compare values representable as
467 * integers, ensure that check doesn't log otherwise.
472 switch (snd_ctl_elem_info_get_type(ctl->info)) {
473 case SND_CTL_ELEM_TYPE_BOOLEAN:
474 expected_int = snd_ctl_elem_value_get_boolean(expected_val,
476 read_int = snd_ctl_elem_value_get_boolean(read_val, index);
479 case SND_CTL_ELEM_TYPE_INTEGER:
480 expected_int = snd_ctl_elem_value_get_integer(expected_val,
482 read_int = snd_ctl_elem_value_get_integer(read_val, index);
485 case SND_CTL_ELEM_TYPE_INTEGER64:
486 expected_int = snd_ctl_elem_value_get_integer64(expected_val,
488 read_int = snd_ctl_elem_value_get_integer64(read_val,
492 case SND_CTL_ELEM_TYPE_ENUMERATED:
493 expected_int = snd_ctl_elem_value_get_enumerated(expected_val,
495 read_int = snd_ctl_elem_value_get_enumerated(read_val,
503 if (expected_int != read_int) {
505 * NOTE: The volatile attribute means that the hardware
506 * can voluntarily change the state of control element
507 * independent of any operation by software.
509 bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info);
510 ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n",
511 ctl->name, index, expected_int, read_int, is_volatile);
519 * Write a value then if possible verify that we get the expected
520 * result. An optional expected value can be provided if we expect
521 * the write to fail, for verifying that invalid writes don't corrupt
524 static int write_and_verify(struct ctl_data *ctl,
525 snd_ctl_elem_value_t *write_val,
526 snd_ctl_elem_value_t *expected_val)
529 bool error_expected, mismatch_shown;
530 snd_ctl_elem_value_t *initial_val, *read_val, *w_val;
531 snd_ctl_elem_value_alloca(&initial_val);
532 snd_ctl_elem_value_alloca(&read_val);
533 snd_ctl_elem_value_alloca(&w_val);
536 * We need to copy the write value since writing can modify
537 * the value which causes surprises, and allocate an expected
538 * value if we expect to read back what we wrote.
540 snd_ctl_elem_value_copy(w_val, write_val);
542 error_expected = true;
544 error_expected = false;
545 snd_ctl_elem_value_alloca(&expected_val);
546 snd_ctl_elem_value_copy(expected_val, write_val);
549 /* Store the value before we write */
550 if (snd_ctl_elem_info_is_readable(ctl->info)) {
551 snd_ctl_elem_value_set_id(initial_val, ctl->id);
553 err = snd_ctl_elem_read(ctl->card->handle, initial_val);
555 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
562 * Do the write, if we have an expected value ignore the error
563 * and carry on to validate the expected value.
565 err = snd_ctl_elem_write(ctl->card->handle, w_val);
566 if (err < 0 && !error_expected) {
567 ksft_print_msg("snd_ctl_elem_write() failed: %s\n",
572 /* Can we do the verification part? */
573 if (!snd_ctl_elem_info_is_readable(ctl->info))
576 snd_ctl_elem_value_set_id(read_val, ctl->id);
578 err = snd_ctl_elem_read(ctl->card->handle, read_val);
580 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
586 * Check for an event if the value changed, or confirm that
587 * there was none if it didn't. We rely on the kernel
588 * generating the notification before it returns from the
589 * write, this is currently true, should that ever change this
590 * will most likely break and need updating.
592 if (!snd_ctl_elem_info_is_volatile(ctl->info)) {
593 err = wait_for_event(ctl, 0);
594 if (snd_ctl_elem_value_compare(initial_val, read_val)) {
596 ksft_print_msg("No event generated for %s\n",
598 ctl->event_missing++;
602 ksft_print_msg("Spurious event generated for %s\n",
604 ctl->event_spurious++;
610 * Use the libray to compare values, if there's a mismatch
611 * carry on and try to provide a more useful diagnostic than
614 if (!snd_ctl_elem_value_compare(expected_val, read_val))
617 mismatch_shown = false;
618 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
619 if (show_mismatch(ctl, i, read_val, expected_val))
620 mismatch_shown = true;
623 ksft_print_msg("%s read and written values differ\n",
630 * Make sure we can write the default value back to the control, this
631 * should validate that at least some write works.
633 static void test_ctl_write_default(struct ctl_data *ctl)
637 /* If the control is turned off let's be polite */
638 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
639 ksft_print_msg("%s is inactive\n", ctl->name);
640 ksft_test_result_skip("write_default.%d.%d\n",
641 ctl->card->card, ctl->elem);
645 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
646 ksft_print_msg("%s is not writeable\n", ctl->name);
647 ksft_test_result_skip("write_default.%d.%d\n",
648 ctl->card->card, ctl->elem);
652 /* No idea what the default was for unreadable controls */
653 if (!snd_ctl_elem_info_is_readable(ctl->info)) {
654 ksft_print_msg("%s couldn't read default\n", ctl->name);
655 ksft_test_result_skip("write_default.%d.%d\n",
656 ctl->card->card, ctl->elem);
660 err = write_and_verify(ctl, ctl->def_val, NULL);
662 ksft_test_result(err >= 0, "write_default.%d.%d\n",
663 ctl->card->card, ctl->elem);
666 static bool test_ctl_write_valid_boolean(struct ctl_data *ctl)
670 snd_ctl_elem_value_t *val;
671 snd_ctl_elem_value_alloca(&val);
673 snd_ctl_elem_value_set_id(val, ctl->id);
675 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
676 for (j = 0; j < 2; j++) {
677 snd_ctl_elem_value_set_boolean(val, i, j);
678 err = write_and_verify(ctl, val, NULL);
687 static bool test_ctl_write_valid_integer(struct ctl_data *ctl)
693 snd_ctl_elem_value_t *val;
694 snd_ctl_elem_value_alloca(&val);
696 snd_ctl_elem_value_set_id(val, ctl->id);
698 step = snd_ctl_elem_info_get_step(ctl->info);
702 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
703 for (j = snd_ctl_elem_info_get_min(ctl->info);
704 j <= snd_ctl_elem_info_get_max(ctl->info); j += step) {
706 snd_ctl_elem_value_set_integer(val, i, j);
707 err = write_and_verify(ctl, val, NULL);
717 static bool test_ctl_write_valid_integer64(struct ctl_data *ctl)
722 snd_ctl_elem_value_t *val;
723 snd_ctl_elem_value_alloca(&val);
725 snd_ctl_elem_value_set_id(val, ctl->id);
727 step = snd_ctl_elem_info_get_step64(ctl->info);
731 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
732 for (j = snd_ctl_elem_info_get_min64(ctl->info);
733 j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) {
735 snd_ctl_elem_value_set_integer64(val, i, j);
736 err = write_and_verify(ctl, val, NULL);
745 static bool test_ctl_write_valid_enumerated(struct ctl_data *ctl)
749 snd_ctl_elem_value_t *val;
750 snd_ctl_elem_value_alloca(&val);
752 snd_ctl_elem_value_set_id(val, ctl->id);
754 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
755 for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) {
756 snd_ctl_elem_value_set_enumerated(val, i, j);
757 err = write_and_verify(ctl, val, NULL);
766 static void test_ctl_write_valid(struct ctl_data *ctl)
771 /* If the control is turned off let's be polite */
772 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
773 ksft_print_msg("%s is inactive\n", ctl->name);
774 ksft_test_result_skip("write_valid.%d.%d\n",
775 ctl->card->card, ctl->elem);
779 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
780 ksft_print_msg("%s is not writeable\n", ctl->name);
781 ksft_test_result_skip("write_valid.%d.%d\n",
782 ctl->card->card, ctl->elem);
786 switch (snd_ctl_elem_info_get_type(ctl->info)) {
787 case SND_CTL_ELEM_TYPE_BOOLEAN:
788 pass = test_ctl_write_valid_boolean(ctl);
791 case SND_CTL_ELEM_TYPE_INTEGER:
792 pass = test_ctl_write_valid_integer(ctl);
795 case SND_CTL_ELEM_TYPE_INTEGER64:
796 pass = test_ctl_write_valid_integer64(ctl);
799 case SND_CTL_ELEM_TYPE_ENUMERATED:
800 pass = test_ctl_write_valid_enumerated(ctl);
804 /* No tests for this yet */
805 ksft_test_result_skip("write_valid.%d.%d\n",
806 ctl->card->card, ctl->elem);
810 /* Restore the default value to minimise disruption */
811 err = write_and_verify(ctl, ctl->def_val, NULL);
815 ksft_test_result(pass, "write_valid.%d.%d\n",
816 ctl->card->card, ctl->elem);
819 static bool test_ctl_write_invalid_value(struct ctl_data *ctl,
820 snd_ctl_elem_value_t *val)
825 /* Ideally this will fail... */
826 err = snd_ctl_elem_write(ctl->card->handle, val);
830 /* ...but some devices will clamp to an in range value */
831 err = snd_ctl_elem_read(ctl->card->handle, val);
833 ksft_print_msg("%s failed to read: %s\n",
834 ctl->name, snd_strerror(err));
838 return !ctl_value_valid(ctl, val);
841 static bool test_ctl_write_invalid_boolean(struct ctl_data *ctl)
846 snd_ctl_elem_value_t *val;
847 snd_ctl_elem_value_alloca(&val);
849 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
850 snd_ctl_elem_value_copy(val, ctl->def_val);
851 snd_ctl_elem_value_set_boolean(val, i, 2);
853 if (test_ctl_write_invalid_value(ctl, val))
860 static bool test_ctl_write_invalid_integer(struct ctl_data *ctl)
864 snd_ctl_elem_value_t *val;
865 snd_ctl_elem_value_alloca(&val);
867 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
868 if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) {
869 /* Just under range */
870 snd_ctl_elem_value_copy(val, ctl->def_val);
871 snd_ctl_elem_value_set_integer(val, i,
872 snd_ctl_elem_info_get_min(ctl->info) - 1);
874 if (test_ctl_write_invalid_value(ctl, val))
877 /* Minimum representable value */
878 snd_ctl_elem_value_copy(val, ctl->def_val);
879 snd_ctl_elem_value_set_integer(val, i, LONG_MIN);
881 if (test_ctl_write_invalid_value(ctl, val))
885 if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) {
886 /* Just over range */
887 snd_ctl_elem_value_copy(val, ctl->def_val);
888 snd_ctl_elem_value_set_integer(val, i,
889 snd_ctl_elem_info_get_max(ctl->info) + 1);
891 if (test_ctl_write_invalid_value(ctl, val))
894 /* Maximum representable value */
895 snd_ctl_elem_value_copy(val, ctl->def_val);
896 snd_ctl_elem_value_set_integer(val, i, LONG_MAX);
898 if (test_ctl_write_invalid_value(ctl, val))
906 static bool test_ctl_write_invalid_integer64(struct ctl_data *ctl)
910 snd_ctl_elem_value_t *val;
911 snd_ctl_elem_value_alloca(&val);
913 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
914 if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) {
915 /* Just under range */
916 snd_ctl_elem_value_copy(val, ctl->def_val);
917 snd_ctl_elem_value_set_integer64(val, i,
918 snd_ctl_elem_info_get_min64(ctl->info) - 1);
920 if (test_ctl_write_invalid_value(ctl, val))
923 /* Minimum representable value */
924 snd_ctl_elem_value_copy(val, ctl->def_val);
925 snd_ctl_elem_value_set_integer64(val, i, LLONG_MIN);
927 if (test_ctl_write_invalid_value(ctl, val))
931 if (snd_ctl_elem_info_get_max64(ctl->info) != LLONG_MAX) {
932 /* Just over range */
933 snd_ctl_elem_value_copy(val, ctl->def_val);
934 snd_ctl_elem_value_set_integer64(val, i,
935 snd_ctl_elem_info_get_max64(ctl->info) + 1);
937 if (test_ctl_write_invalid_value(ctl, val))
940 /* Maximum representable value */
941 snd_ctl_elem_value_copy(val, ctl->def_val);
942 snd_ctl_elem_value_set_integer64(val, i, LLONG_MAX);
944 if (test_ctl_write_invalid_value(ctl, val))
952 static bool test_ctl_write_invalid_enumerated(struct ctl_data *ctl)
955 unsigned int val_read;
957 snd_ctl_elem_value_t *val;
958 snd_ctl_elem_value_alloca(&val);
960 snd_ctl_elem_value_set_id(val, ctl->id);
962 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
963 /* One beyond maximum */
964 snd_ctl_elem_value_copy(val, ctl->def_val);
965 snd_ctl_elem_value_set_enumerated(val, i,
966 snd_ctl_elem_info_get_items(ctl->info));
968 if (test_ctl_write_invalid_value(ctl, val))
971 /* Maximum representable value */
972 snd_ctl_elem_value_copy(val, ctl->def_val);
973 snd_ctl_elem_value_set_enumerated(val, i, UINT_MAX);
975 if (test_ctl_write_invalid_value(ctl, val))
984 static void test_ctl_write_invalid(struct ctl_data *ctl)
989 /* If the control is turned off let's be polite */
990 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
991 ksft_print_msg("%s is inactive\n", ctl->name);
992 ksft_test_result_skip("write_invalid.%d.%d\n",
993 ctl->card->card, ctl->elem);
997 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
998 ksft_print_msg("%s is not writeable\n", ctl->name);
999 ksft_test_result_skip("write_invalid.%d.%d\n",
1000 ctl->card->card, ctl->elem);
1004 switch (snd_ctl_elem_info_get_type(ctl->info)) {
1005 case SND_CTL_ELEM_TYPE_BOOLEAN:
1006 pass = test_ctl_write_invalid_boolean(ctl);
1009 case SND_CTL_ELEM_TYPE_INTEGER:
1010 pass = test_ctl_write_invalid_integer(ctl);
1013 case SND_CTL_ELEM_TYPE_INTEGER64:
1014 pass = test_ctl_write_invalid_integer64(ctl);
1017 case SND_CTL_ELEM_TYPE_ENUMERATED:
1018 pass = test_ctl_write_invalid_enumerated(ctl);
1022 /* No tests for this yet */
1023 ksft_test_result_skip("write_invalid.%d.%d\n",
1024 ctl->card->card, ctl->elem);
1028 /* Restore the default value to minimise disruption */
1029 err = write_and_verify(ctl, ctl->def_val, NULL);
1033 ksft_test_result(pass, "write_invalid.%d.%d\n",
1034 ctl->card->card, ctl->elem);
1037 static void test_ctl_event_missing(struct ctl_data *ctl)
1039 ksft_test_result(!ctl->event_missing, "event_missing.%d.%d\n",
1040 ctl->card->card, ctl->elem);
1043 static void test_ctl_event_spurious(struct ctl_data *ctl)
1045 ksft_test_result(!ctl->event_spurious, "event_spurious.%d.%d\n",
1046 ctl->card->card, ctl->elem);
1051 struct ctl_data *ctl;
1053 ksft_print_header();
1057 ksft_set_plan(num_controls * TESTS_PER_CONTROL);
1059 for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) {
1061 * Must test get_value() before we write anything, the
1062 * test stores the default value for later cleanup.
1064 test_ctl_get_value(ctl);
1065 test_ctl_write_default(ctl);
1066 test_ctl_write_valid(ctl);
1067 test_ctl_write_invalid(ctl);
1068 test_ctl_event_missing(ctl);
1069 test_ctl_event_spurious(ctl);