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"
29 #include "alsa-local.h"
31 #define TESTS_PER_CONTROL 7
38 snd_ctl_elem_list_t *ctls;
39 struct card_data *next;
44 snd_ctl_elem_id_t *id;
45 snd_ctl_elem_info_t *info;
46 snd_ctl_elem_value_t *def_val;
50 struct card_data *card;
51 struct ctl_data *next;
56 struct card_data *card_list = NULL;
57 struct ctl_data *ctl_list = NULL;
59 static void find_controls(void)
63 struct card_data *card_data;
64 struct ctl_data *ctl_data;
66 char *card_name, *card_longname;
69 if (snd_card_next(&card) < 0 || card < 0)
72 config = get_alsalib_config();
75 sprintf(name, "hw:%d", card);
77 card_data = malloc(sizeof(*card_data));
79 ksft_exit_fail_msg("Out of memory\n");
81 err = snd_ctl_open_lconf(&card_data->handle, name, 0, config);
83 ksft_print_msg("Failed to get hctl for card %d: %s\n",
84 card, snd_strerror(err));
88 err = snd_card_get_name(card, &card_name);
90 card_name = "Unknown";
91 err = snd_card_get_longname(card, &card_longname);
93 card_longname = "Unknown";
94 ksft_print_msg("Card %d - %s (%s)\n", card,
95 card_name, card_longname);
98 snd_ctl_elem_list_malloc(&card_data->ctls);
99 snd_ctl_elem_list(card_data->handle, card_data->ctls);
100 card_data->num_ctls = snd_ctl_elem_list_get_count(card_data->ctls);
102 /* Enumerate control information */
103 snd_ctl_elem_list_alloc_space(card_data->ctls, card_data->num_ctls);
104 snd_ctl_elem_list(card_data->handle, card_data->ctls);
106 card_data->card = num_cards++;
107 card_data->next = card_list;
108 card_list = card_data;
110 num_controls += card_data->num_ctls;
112 for (ctl = 0; ctl < card_data->num_ctls; ctl++) {
113 ctl_data = malloc(sizeof(*ctl_data));
115 ksft_exit_fail_msg("Out of memory\n");
117 memset(ctl_data, 0, sizeof(*ctl_data));
118 ctl_data->card = card_data;
119 ctl_data->elem = ctl;
120 ctl_data->name = snd_ctl_elem_list_get_name(card_data->ctls,
123 err = snd_ctl_elem_id_malloc(&ctl_data->id);
125 ksft_exit_fail_msg("Out of memory\n");
127 err = snd_ctl_elem_info_malloc(&ctl_data->info);
129 ksft_exit_fail_msg("Out of memory\n");
131 err = snd_ctl_elem_value_malloc(&ctl_data->def_val);
133 ksft_exit_fail_msg("Out of memory\n");
135 snd_ctl_elem_list_get_id(card_data->ctls, ctl,
137 snd_ctl_elem_info_set_id(ctl_data->info, ctl_data->id);
138 err = snd_ctl_elem_info(card_data->handle,
141 ksft_print_msg("%s getting info for %d\n",
146 snd_ctl_elem_value_set_id(ctl_data->def_val,
149 ctl_data->next = ctl_list;
153 /* Set up for events */
154 err = snd_ctl_subscribe_events(card_data->handle, true);
156 ksft_exit_fail_msg("snd_ctl_subscribe_events() failed for card %d: %d\n",
160 err = snd_ctl_poll_descriptors_count(card_data->handle);
162 ksft_exit_fail_msg("Unexpected descriptor count %d for card %d\n",
166 err = snd_ctl_poll_descriptors(card_data->handle,
167 &card_data->pollfd, 1);
169 ksft_exit_fail_msg("snd_ctl_poll_descriptors() failed for %d\n",
174 if (snd_card_next(&card) < 0) {
175 ksft_print_msg("snd_card_next");
180 snd_config_delete(config);
184 * Block for up to timeout ms for an event, returns a negative value
185 * on error, 0 for no event and 1 for an event.
187 static int wait_for_event(struct ctl_data *ctl, int timeout)
189 unsigned short revents;
190 snd_ctl_event_t *event;
192 unsigned int mask = 0;
195 snd_ctl_event_alloca(&event);
198 err = poll(&(ctl->card->pollfd), 1, timeout);
200 ksft_print_msg("poll() failed for %s: %s (%d)\n",
201 ctl->name, strerror(errno), errno);
208 err = snd_ctl_poll_descriptors_revents(ctl->card->handle,
209 &(ctl->card->pollfd),
212 ksft_print_msg("snd_ctl_poll_descriptors_revents() failed for %s: %d\n",
216 if (revents & POLLERR) {
217 ksft_print_msg("snd_ctl_poll_descriptors_revents() reported POLLERR for %s\n",
222 if (!(revents & POLLIN)) {
223 ksft_print_msg("No POLLIN\n");
227 err = snd_ctl_read(ctl->card->handle, event);
229 ksft_print_msg("snd_ctl_read() failed for %s: %d\n",
234 if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM)
237 /* The ID returned from the event is 1 less than numid */
238 mask = snd_ctl_event_elem_get_mask(event);
239 ev_id = snd_ctl_event_elem_get_numid(event);
240 if (ev_id != snd_ctl_elem_info_get_numid(ctl->info)) {
241 ksft_print_msg("Event for unexpected ctl %s\n",
242 snd_ctl_event_elem_get_name(event));
246 if ((mask & SND_CTL_EVENT_MASK_REMOVE) == SND_CTL_EVENT_MASK_REMOVE) {
247 ksft_print_msg("Removal event for %s\n",
251 } while ((mask & SND_CTL_EVENT_MASK_VALUE) != SND_CTL_EVENT_MASK_VALUE);
256 static bool ctl_value_index_valid(struct ctl_data *ctl,
257 snd_ctl_elem_value_t *val,
263 switch (snd_ctl_elem_info_get_type(ctl->info)) {
264 case SND_CTL_ELEM_TYPE_NONE:
265 ksft_print_msg("%s.%d Invalid control type NONE\n",
269 case SND_CTL_ELEM_TYPE_BOOLEAN:
270 int_val = snd_ctl_elem_value_get_boolean(val, index);
276 ksft_print_msg("%s.%d Invalid boolean value %ld\n",
277 ctl->name, index, int_val);
282 case SND_CTL_ELEM_TYPE_INTEGER:
283 int_val = snd_ctl_elem_value_get_integer(val, index);
285 if (int_val < snd_ctl_elem_info_get_min(ctl->info)) {
286 ksft_print_msg("%s.%d value %ld less than minimum %ld\n",
287 ctl->name, index, int_val,
288 snd_ctl_elem_info_get_min(ctl->info));
292 if (int_val > snd_ctl_elem_info_get_max(ctl->info)) {
293 ksft_print_msg("%s.%d value %ld more than maximum %ld\n",
294 ctl->name, index, int_val,
295 snd_ctl_elem_info_get_max(ctl->info));
299 /* Only check step size if there is one and we're in bounds */
300 if (snd_ctl_elem_info_get_step(ctl->info) &&
301 (int_val - snd_ctl_elem_info_get_min(ctl->info) %
302 snd_ctl_elem_info_get_step(ctl->info))) {
303 ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n",
304 ctl->name, index, int_val,
305 snd_ctl_elem_info_get_step(ctl->info),
306 snd_ctl_elem_info_get_min(ctl->info));
311 case SND_CTL_ELEM_TYPE_INTEGER64:
312 int64_val = snd_ctl_elem_value_get_integer64(val, index);
314 if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) {
315 ksft_print_msg("%s.%d value %lld less than minimum %lld\n",
316 ctl->name, index, int64_val,
317 snd_ctl_elem_info_get_min64(ctl->info));
321 if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) {
322 ksft_print_msg("%s.%d value %lld more than maximum %lld\n",
323 ctl->name, index, int64_val,
324 snd_ctl_elem_info_get_max(ctl->info));
328 /* Only check step size if there is one and we're in bounds */
329 if (snd_ctl_elem_info_get_step64(ctl->info) &&
330 (int64_val - snd_ctl_elem_info_get_min64(ctl->info)) %
331 snd_ctl_elem_info_get_step64(ctl->info)) {
332 ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n",
333 ctl->name, index, int64_val,
334 snd_ctl_elem_info_get_step64(ctl->info),
335 snd_ctl_elem_info_get_min64(ctl->info));
340 case SND_CTL_ELEM_TYPE_ENUMERATED:
341 int_val = snd_ctl_elem_value_get_enumerated(val, index);
344 ksft_print_msg("%s.%d negative value %ld for enumeration\n",
345 ctl->name, index, int_val);
349 if (int_val >= snd_ctl_elem_info_get_items(ctl->info)) {
350 ksft_print_msg("%s.%d value %ld more than item count %ld\n",
351 ctl->name, index, int_val,
352 snd_ctl_elem_info_get_items(ctl->info));
358 /* No tests for other types */
366 * Check that the provided value meets the constraints for the
369 static bool ctl_value_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val)
374 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
375 if (!ctl_value_index_valid(ctl, val, i))
382 * Check that we can read the default value and it is valid. Write
383 * tests use the read value to restore the default.
385 static void test_ctl_get_value(struct ctl_data *ctl)
389 /* If the control is turned off let's be polite */
390 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
391 ksft_print_msg("%s is inactive\n", ctl->name);
392 ksft_test_result_skip("get_value.%d.%d\n",
393 ctl->card->card, ctl->elem);
397 /* Can't test reading on an unreadable control */
398 if (!snd_ctl_elem_info_is_readable(ctl->info)) {
399 ksft_print_msg("%s is not readable\n", ctl->name);
400 ksft_test_result_skip("get_value.%d.%d\n",
401 ctl->card->card, ctl->elem);
405 err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val);
407 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
412 if (!ctl_value_valid(ctl, ctl->def_val))
416 ksft_test_result(err >= 0, "get_value.%d.%d\n",
417 ctl->card->card, ctl->elem);
420 static bool strend(const char *haystack, const char *needle)
422 size_t haystack_len = strlen(haystack);
423 size_t needle_len = strlen(needle);
425 if (needle_len > haystack_len)
427 return strcmp(haystack + haystack_len - needle_len, needle) == 0;
430 static void test_ctl_name(struct ctl_data *ctl)
435 ksft_print_msg("%d.%d %s\n", ctl->card->card, ctl->elem,
438 /* Only boolean controls should end in Switch */
439 if (strend(ctl->name, " Switch")) {
440 if (snd_ctl_elem_info_get_type(ctl->info) != SND_CTL_ELEM_TYPE_BOOLEAN) {
441 ksft_print_msg("%d.%d %s ends in Switch but is not boolean\n",
442 ctl->card->card, ctl->elem, ctl->name);
447 /* Writeable boolean controls should end in Switch */
448 if (snd_ctl_elem_info_get_type(ctl->info) == SND_CTL_ELEM_TYPE_BOOLEAN &&
449 snd_ctl_elem_info_is_writable(ctl->info)) {
450 if (!strend(ctl->name, " Switch")) {
451 ksft_print_msg("%d.%d %s is a writeable boolean but not a Switch\n",
452 ctl->card->card, ctl->elem, ctl->name);
457 ksft_test_result(name_ok, "name.%d.%d\n",
458 ctl->card->card, ctl->elem);
461 static void show_values(struct ctl_data *ctl, snd_ctl_elem_value_t *orig_val,
462 snd_ctl_elem_value_t *read_val)
464 long long orig_int, read_int;
467 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
468 switch (snd_ctl_elem_info_get_type(ctl->info)) {
469 case SND_CTL_ELEM_TYPE_BOOLEAN:
470 orig_int = snd_ctl_elem_value_get_boolean(orig_val, i);
471 read_int = snd_ctl_elem_value_get_boolean(read_val, i);
474 case SND_CTL_ELEM_TYPE_INTEGER:
475 orig_int = snd_ctl_elem_value_get_integer(orig_val, i);
476 read_int = snd_ctl_elem_value_get_integer(read_val, i);
479 case SND_CTL_ELEM_TYPE_INTEGER64:
480 orig_int = snd_ctl_elem_value_get_integer64(orig_val,
482 read_int = snd_ctl_elem_value_get_integer64(read_val,
486 case SND_CTL_ELEM_TYPE_ENUMERATED:
487 orig_int = snd_ctl_elem_value_get_enumerated(orig_val,
489 read_int = snd_ctl_elem_value_get_enumerated(read_val,
497 ksft_print_msg("%s.%d orig %lld read %lld, is_volatile %d\n",
498 ctl->name, i, orig_int, read_int,
499 snd_ctl_elem_info_is_volatile(ctl->info));
503 static bool show_mismatch(struct ctl_data *ctl, int index,
504 snd_ctl_elem_value_t *read_val,
505 snd_ctl_elem_value_t *expected_val)
507 long long expected_int, read_int;
510 * We factor out the code to compare values representable as
511 * integers, ensure that check doesn't log otherwise.
516 switch (snd_ctl_elem_info_get_type(ctl->info)) {
517 case SND_CTL_ELEM_TYPE_BOOLEAN:
518 expected_int = snd_ctl_elem_value_get_boolean(expected_val,
520 read_int = snd_ctl_elem_value_get_boolean(read_val, index);
523 case SND_CTL_ELEM_TYPE_INTEGER:
524 expected_int = snd_ctl_elem_value_get_integer(expected_val,
526 read_int = snd_ctl_elem_value_get_integer(read_val, index);
529 case SND_CTL_ELEM_TYPE_INTEGER64:
530 expected_int = snd_ctl_elem_value_get_integer64(expected_val,
532 read_int = snd_ctl_elem_value_get_integer64(read_val,
536 case SND_CTL_ELEM_TYPE_ENUMERATED:
537 expected_int = snd_ctl_elem_value_get_enumerated(expected_val,
539 read_int = snd_ctl_elem_value_get_enumerated(read_val,
547 if (expected_int != read_int) {
549 * NOTE: The volatile attribute means that the hardware
550 * can voluntarily change the state of control element
551 * independent of any operation by software.
553 bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info);
554 ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n",
555 ctl->name, index, expected_int, read_int, is_volatile);
563 * Write a value then if possible verify that we get the expected
564 * result. An optional expected value can be provided if we expect
565 * the write to fail, for verifying that invalid writes don't corrupt
568 static int write_and_verify(struct ctl_data *ctl,
569 snd_ctl_elem_value_t *write_val,
570 snd_ctl_elem_value_t *expected_val)
573 bool error_expected, mismatch_shown;
574 snd_ctl_elem_value_t *initial_val, *read_val, *w_val;
575 snd_ctl_elem_value_alloca(&initial_val);
576 snd_ctl_elem_value_alloca(&read_val);
577 snd_ctl_elem_value_alloca(&w_val);
580 * We need to copy the write value since writing can modify
581 * the value which causes surprises, and allocate an expected
582 * value if we expect to read back what we wrote.
584 snd_ctl_elem_value_copy(w_val, write_val);
586 error_expected = true;
588 error_expected = false;
589 snd_ctl_elem_value_alloca(&expected_val);
590 snd_ctl_elem_value_copy(expected_val, write_val);
593 /* Store the value before we write */
594 if (snd_ctl_elem_info_is_readable(ctl->info)) {
595 snd_ctl_elem_value_set_id(initial_val, ctl->id);
597 err = snd_ctl_elem_read(ctl->card->handle, initial_val);
599 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
606 * Do the write, if we have an expected value ignore the error
607 * and carry on to validate the expected value.
609 err = snd_ctl_elem_write(ctl->card->handle, w_val);
610 if (err < 0 && !error_expected) {
611 ksft_print_msg("snd_ctl_elem_write() failed: %s\n",
616 /* Can we do the verification part? */
617 if (!snd_ctl_elem_info_is_readable(ctl->info))
620 snd_ctl_elem_value_set_id(read_val, ctl->id);
622 err = snd_ctl_elem_read(ctl->card->handle, read_val);
624 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
630 * Check for an event if the value changed, or confirm that
631 * there was none if it didn't. We rely on the kernel
632 * generating the notification before it returns from the
633 * write, this is currently true, should that ever change this
634 * will most likely break and need updating.
636 if (!snd_ctl_elem_info_is_volatile(ctl->info)) {
637 err = wait_for_event(ctl, 0);
638 if (snd_ctl_elem_value_compare(initial_val, read_val)) {
640 ksft_print_msg("No event generated for %s\n",
642 show_values(ctl, initial_val, read_val);
643 ctl->event_missing++;
647 ksft_print_msg("Spurious event generated for %s\n",
649 show_values(ctl, initial_val, read_val);
650 ctl->event_spurious++;
656 * Use the libray to compare values, if there's a mismatch
657 * carry on and try to provide a more useful diagnostic than
660 if (!snd_ctl_elem_value_compare(expected_val, read_val))
663 mismatch_shown = false;
664 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
665 if (show_mismatch(ctl, i, read_val, expected_val))
666 mismatch_shown = true;
669 ksft_print_msg("%s read and written values differ\n",
676 * Make sure we can write the default value back to the control, this
677 * should validate that at least some write works.
679 static void test_ctl_write_default(struct ctl_data *ctl)
683 /* If the control is turned off let's be polite */
684 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
685 ksft_print_msg("%s is inactive\n", ctl->name);
686 ksft_test_result_skip("write_default.%d.%d\n",
687 ctl->card->card, ctl->elem);
691 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
692 ksft_print_msg("%s is not writeable\n", ctl->name);
693 ksft_test_result_skip("write_default.%d.%d\n",
694 ctl->card->card, ctl->elem);
698 /* No idea what the default was for unreadable controls */
699 if (!snd_ctl_elem_info_is_readable(ctl->info)) {
700 ksft_print_msg("%s couldn't read default\n", ctl->name);
701 ksft_test_result_skip("write_default.%d.%d\n",
702 ctl->card->card, ctl->elem);
706 err = write_and_verify(ctl, ctl->def_val, NULL);
708 ksft_test_result(err >= 0, "write_default.%d.%d\n",
709 ctl->card->card, ctl->elem);
712 static bool test_ctl_write_valid_boolean(struct ctl_data *ctl)
716 snd_ctl_elem_value_t *val;
717 snd_ctl_elem_value_alloca(&val);
719 snd_ctl_elem_value_set_id(val, ctl->id);
721 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
722 for (j = 0; j < 2; j++) {
723 snd_ctl_elem_value_set_boolean(val, i, j);
724 err = write_and_verify(ctl, val, NULL);
733 static bool test_ctl_write_valid_integer(struct ctl_data *ctl)
739 snd_ctl_elem_value_t *val;
740 snd_ctl_elem_value_alloca(&val);
742 snd_ctl_elem_value_set_id(val, ctl->id);
744 step = snd_ctl_elem_info_get_step(ctl->info);
748 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
749 for (j = snd_ctl_elem_info_get_min(ctl->info);
750 j <= snd_ctl_elem_info_get_max(ctl->info); j += step) {
752 snd_ctl_elem_value_set_integer(val, i, j);
753 err = write_and_verify(ctl, val, NULL);
763 static bool test_ctl_write_valid_integer64(struct ctl_data *ctl)
768 snd_ctl_elem_value_t *val;
769 snd_ctl_elem_value_alloca(&val);
771 snd_ctl_elem_value_set_id(val, ctl->id);
773 step = snd_ctl_elem_info_get_step64(ctl->info);
777 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
778 for (j = snd_ctl_elem_info_get_min64(ctl->info);
779 j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) {
781 snd_ctl_elem_value_set_integer64(val, i, j);
782 err = write_and_verify(ctl, val, NULL);
791 static bool test_ctl_write_valid_enumerated(struct ctl_data *ctl)
795 snd_ctl_elem_value_t *val;
796 snd_ctl_elem_value_alloca(&val);
798 snd_ctl_elem_value_set_id(val, ctl->id);
800 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
801 for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) {
802 snd_ctl_elem_value_set_enumerated(val, i, j);
803 err = write_and_verify(ctl, val, NULL);
812 static void test_ctl_write_valid(struct ctl_data *ctl)
816 /* If the control is turned off let's be polite */
817 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
818 ksft_print_msg("%s is inactive\n", ctl->name);
819 ksft_test_result_skip("write_valid.%d.%d\n",
820 ctl->card->card, ctl->elem);
824 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
825 ksft_print_msg("%s is not writeable\n", ctl->name);
826 ksft_test_result_skip("write_valid.%d.%d\n",
827 ctl->card->card, ctl->elem);
831 switch (snd_ctl_elem_info_get_type(ctl->info)) {
832 case SND_CTL_ELEM_TYPE_BOOLEAN:
833 pass = test_ctl_write_valid_boolean(ctl);
836 case SND_CTL_ELEM_TYPE_INTEGER:
837 pass = test_ctl_write_valid_integer(ctl);
840 case SND_CTL_ELEM_TYPE_INTEGER64:
841 pass = test_ctl_write_valid_integer64(ctl);
844 case SND_CTL_ELEM_TYPE_ENUMERATED:
845 pass = test_ctl_write_valid_enumerated(ctl);
849 /* No tests for this yet */
850 ksft_test_result_skip("write_valid.%d.%d\n",
851 ctl->card->card, ctl->elem);
855 /* Restore the default value to minimise disruption */
856 write_and_verify(ctl, ctl->def_val, NULL);
858 ksft_test_result(pass, "write_valid.%d.%d\n",
859 ctl->card->card, ctl->elem);
862 static bool test_ctl_write_invalid_value(struct ctl_data *ctl,
863 snd_ctl_elem_value_t *val)
868 /* Ideally this will fail... */
869 err = snd_ctl_elem_write(ctl->card->handle, val);
873 /* ...but some devices will clamp to an in range value */
874 err = snd_ctl_elem_read(ctl->card->handle, val);
876 ksft_print_msg("%s failed to read: %s\n",
877 ctl->name, snd_strerror(err));
881 return !ctl_value_valid(ctl, val);
884 static bool test_ctl_write_invalid_boolean(struct ctl_data *ctl)
889 snd_ctl_elem_value_t *val;
890 snd_ctl_elem_value_alloca(&val);
892 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
893 snd_ctl_elem_value_copy(val, ctl->def_val);
894 snd_ctl_elem_value_set_boolean(val, i, 2);
896 if (test_ctl_write_invalid_value(ctl, val))
903 static bool test_ctl_write_invalid_integer(struct ctl_data *ctl)
907 snd_ctl_elem_value_t *val;
908 snd_ctl_elem_value_alloca(&val);
910 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
911 if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) {
912 /* Just under range */
913 snd_ctl_elem_value_copy(val, ctl->def_val);
914 snd_ctl_elem_value_set_integer(val, i,
915 snd_ctl_elem_info_get_min(ctl->info) - 1);
917 if (test_ctl_write_invalid_value(ctl, val))
920 /* Minimum representable value */
921 snd_ctl_elem_value_copy(val, ctl->def_val);
922 snd_ctl_elem_value_set_integer(val, i, LONG_MIN);
924 if (test_ctl_write_invalid_value(ctl, val))
928 if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) {
929 /* Just over range */
930 snd_ctl_elem_value_copy(val, ctl->def_val);
931 snd_ctl_elem_value_set_integer(val, i,
932 snd_ctl_elem_info_get_max(ctl->info) + 1);
934 if (test_ctl_write_invalid_value(ctl, val))
937 /* Maximum representable value */
938 snd_ctl_elem_value_copy(val, ctl->def_val);
939 snd_ctl_elem_value_set_integer(val, i, LONG_MAX);
941 if (test_ctl_write_invalid_value(ctl, val))
949 static bool test_ctl_write_invalid_integer64(struct ctl_data *ctl)
953 snd_ctl_elem_value_t *val;
954 snd_ctl_elem_value_alloca(&val);
956 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
957 if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) {
958 /* Just under range */
959 snd_ctl_elem_value_copy(val, ctl->def_val);
960 snd_ctl_elem_value_set_integer64(val, i,
961 snd_ctl_elem_info_get_min64(ctl->info) - 1);
963 if (test_ctl_write_invalid_value(ctl, val))
966 /* Minimum representable value */
967 snd_ctl_elem_value_copy(val, ctl->def_val);
968 snd_ctl_elem_value_set_integer64(val, i, LLONG_MIN);
970 if (test_ctl_write_invalid_value(ctl, val))
974 if (snd_ctl_elem_info_get_max64(ctl->info) != LLONG_MAX) {
975 /* Just over range */
976 snd_ctl_elem_value_copy(val, ctl->def_val);
977 snd_ctl_elem_value_set_integer64(val, i,
978 snd_ctl_elem_info_get_max64(ctl->info) + 1);
980 if (test_ctl_write_invalid_value(ctl, val))
983 /* Maximum representable value */
984 snd_ctl_elem_value_copy(val, ctl->def_val);
985 snd_ctl_elem_value_set_integer64(val, i, LLONG_MAX);
987 if (test_ctl_write_invalid_value(ctl, val))
995 static bool test_ctl_write_invalid_enumerated(struct ctl_data *ctl)
998 unsigned int val_read;
1000 snd_ctl_elem_value_t *val;
1001 snd_ctl_elem_value_alloca(&val);
1003 snd_ctl_elem_value_set_id(val, ctl->id);
1005 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
1006 /* One beyond maximum */
1007 snd_ctl_elem_value_copy(val, ctl->def_val);
1008 snd_ctl_elem_value_set_enumerated(val, i,
1009 snd_ctl_elem_info_get_items(ctl->info));
1011 if (test_ctl_write_invalid_value(ctl, val))
1014 /* Maximum representable value */
1015 snd_ctl_elem_value_copy(val, ctl->def_val);
1016 snd_ctl_elem_value_set_enumerated(val, i, UINT_MAX);
1018 if (test_ctl_write_invalid_value(ctl, val))
1027 static void test_ctl_write_invalid(struct ctl_data *ctl)
1032 /* If the control is turned off let's be polite */
1033 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
1034 ksft_print_msg("%s is inactive\n", ctl->name);
1035 ksft_test_result_skip("write_invalid.%d.%d\n",
1036 ctl->card->card, ctl->elem);
1040 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
1041 ksft_print_msg("%s is not writeable\n", ctl->name);
1042 ksft_test_result_skip("write_invalid.%d.%d\n",
1043 ctl->card->card, ctl->elem);
1047 switch (snd_ctl_elem_info_get_type(ctl->info)) {
1048 case SND_CTL_ELEM_TYPE_BOOLEAN:
1049 pass = test_ctl_write_invalid_boolean(ctl);
1052 case SND_CTL_ELEM_TYPE_INTEGER:
1053 pass = test_ctl_write_invalid_integer(ctl);
1056 case SND_CTL_ELEM_TYPE_INTEGER64:
1057 pass = test_ctl_write_invalid_integer64(ctl);
1060 case SND_CTL_ELEM_TYPE_ENUMERATED:
1061 pass = test_ctl_write_invalid_enumerated(ctl);
1065 /* No tests for this yet */
1066 ksft_test_result_skip("write_invalid.%d.%d\n",
1067 ctl->card->card, ctl->elem);
1071 /* Restore the default value to minimise disruption */
1072 write_and_verify(ctl, ctl->def_val, NULL);
1074 ksft_test_result(pass, "write_invalid.%d.%d\n",
1075 ctl->card->card, ctl->elem);
1078 static void test_ctl_event_missing(struct ctl_data *ctl)
1080 ksft_test_result(!ctl->event_missing, "event_missing.%d.%d\n",
1081 ctl->card->card, ctl->elem);
1084 static void test_ctl_event_spurious(struct ctl_data *ctl)
1086 ksft_test_result(!ctl->event_spurious, "event_spurious.%d.%d\n",
1087 ctl->card->card, ctl->elem);
1092 struct ctl_data *ctl;
1094 ksft_print_header();
1098 ksft_set_plan(num_controls * TESTS_PER_CONTROL);
1100 for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) {
1102 * Must test get_value() before we write anything, the
1103 * test stores the default value for later cleanup.
1105 test_ctl_get_value(ctl);
1107 test_ctl_write_default(ctl);
1108 test_ctl_write_valid(ctl);
1109 test_ctl_write_invalid(ctl);
1110 test_ctl_event_missing(ctl);
1111 test_ctl_event_spurious(ctl);