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)
434 ksft_print_msg("%d.%d %s\n", ctl->card->card, ctl->elem,
437 /* Only boolean controls should end in Switch */
438 if (strend(ctl->name, " Switch")) {
439 if (snd_ctl_elem_info_get_type(ctl->info) != SND_CTL_ELEM_TYPE_BOOLEAN) {
440 ksft_print_msg("%d.%d %s ends in Switch but is not boolean\n",
441 ctl->card->card, ctl->elem, ctl->name);
446 /* Writeable boolean controls should end in Switch */
447 if (snd_ctl_elem_info_get_type(ctl->info) == SND_CTL_ELEM_TYPE_BOOLEAN &&
448 snd_ctl_elem_info_is_writable(ctl->info)) {
449 if (!strend(ctl->name, " Switch")) {
450 ksft_print_msg("%d.%d %s is a writeable boolean but not a Switch\n",
451 ctl->card->card, ctl->elem, ctl->name);
456 ksft_test_result(name_ok, "name.%d.%d\n",
457 ctl->card->card, ctl->elem);
460 static void show_values(struct ctl_data *ctl, snd_ctl_elem_value_t *orig_val,
461 snd_ctl_elem_value_t *read_val)
463 long long orig_int, read_int;
466 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
467 switch (snd_ctl_elem_info_get_type(ctl->info)) {
468 case SND_CTL_ELEM_TYPE_BOOLEAN:
469 orig_int = snd_ctl_elem_value_get_boolean(orig_val, i);
470 read_int = snd_ctl_elem_value_get_boolean(read_val, i);
473 case SND_CTL_ELEM_TYPE_INTEGER:
474 orig_int = snd_ctl_elem_value_get_integer(orig_val, i);
475 read_int = snd_ctl_elem_value_get_integer(read_val, i);
478 case SND_CTL_ELEM_TYPE_INTEGER64:
479 orig_int = snd_ctl_elem_value_get_integer64(orig_val,
481 read_int = snd_ctl_elem_value_get_integer64(read_val,
485 case SND_CTL_ELEM_TYPE_ENUMERATED:
486 orig_int = snd_ctl_elem_value_get_enumerated(orig_val,
488 read_int = snd_ctl_elem_value_get_enumerated(read_val,
496 ksft_print_msg("%s.%d orig %lld read %lld, is_volatile %d\n",
497 ctl->name, i, orig_int, read_int,
498 snd_ctl_elem_info_is_volatile(ctl->info));
502 static bool show_mismatch(struct ctl_data *ctl, int index,
503 snd_ctl_elem_value_t *read_val,
504 snd_ctl_elem_value_t *expected_val)
506 long long expected_int, read_int;
509 * We factor out the code to compare values representable as
510 * integers, ensure that check doesn't log otherwise.
515 switch (snd_ctl_elem_info_get_type(ctl->info)) {
516 case SND_CTL_ELEM_TYPE_BOOLEAN:
517 expected_int = snd_ctl_elem_value_get_boolean(expected_val,
519 read_int = snd_ctl_elem_value_get_boolean(read_val, index);
522 case SND_CTL_ELEM_TYPE_INTEGER:
523 expected_int = snd_ctl_elem_value_get_integer(expected_val,
525 read_int = snd_ctl_elem_value_get_integer(read_val, index);
528 case SND_CTL_ELEM_TYPE_INTEGER64:
529 expected_int = snd_ctl_elem_value_get_integer64(expected_val,
531 read_int = snd_ctl_elem_value_get_integer64(read_val,
535 case SND_CTL_ELEM_TYPE_ENUMERATED:
536 expected_int = snd_ctl_elem_value_get_enumerated(expected_val,
538 read_int = snd_ctl_elem_value_get_enumerated(read_val,
546 if (expected_int != read_int) {
548 * NOTE: The volatile attribute means that the hardware
549 * can voluntarily change the state of control element
550 * independent of any operation by software.
552 bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info);
553 ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n",
554 ctl->name, index, expected_int, read_int, is_volatile);
562 * Write a value then if possible verify that we get the expected
563 * result. An optional expected value can be provided if we expect
564 * the write to fail, for verifying that invalid writes don't corrupt
567 static int write_and_verify(struct ctl_data *ctl,
568 snd_ctl_elem_value_t *write_val,
569 snd_ctl_elem_value_t *expected_val)
572 bool error_expected, mismatch_shown;
573 snd_ctl_elem_value_t *initial_val, *read_val, *w_val;
574 snd_ctl_elem_value_alloca(&initial_val);
575 snd_ctl_elem_value_alloca(&read_val);
576 snd_ctl_elem_value_alloca(&w_val);
579 * We need to copy the write value since writing can modify
580 * the value which causes surprises, and allocate an expected
581 * value if we expect to read back what we wrote.
583 snd_ctl_elem_value_copy(w_val, write_val);
585 error_expected = true;
587 error_expected = false;
588 snd_ctl_elem_value_alloca(&expected_val);
589 snd_ctl_elem_value_copy(expected_val, write_val);
592 /* Store the value before we write */
593 if (snd_ctl_elem_info_is_readable(ctl->info)) {
594 snd_ctl_elem_value_set_id(initial_val, ctl->id);
596 err = snd_ctl_elem_read(ctl->card->handle, initial_val);
598 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
605 * Do the write, if we have an expected value ignore the error
606 * and carry on to validate the expected value.
608 err = snd_ctl_elem_write(ctl->card->handle, w_val);
609 if (err < 0 && !error_expected) {
610 ksft_print_msg("snd_ctl_elem_write() failed: %s\n",
615 /* Can we do the verification part? */
616 if (!snd_ctl_elem_info_is_readable(ctl->info))
619 snd_ctl_elem_value_set_id(read_val, ctl->id);
621 err = snd_ctl_elem_read(ctl->card->handle, read_val);
623 ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
629 * Check for an event if the value changed, or confirm that
630 * there was none if it didn't. We rely on the kernel
631 * generating the notification before it returns from the
632 * write, this is currently true, should that ever change this
633 * will most likely break and need updating.
635 if (!snd_ctl_elem_info_is_volatile(ctl->info)) {
636 err = wait_for_event(ctl, 0);
637 if (snd_ctl_elem_value_compare(initial_val, read_val)) {
639 ksft_print_msg("No event generated for %s\n",
641 show_values(ctl, initial_val, read_val);
642 ctl->event_missing++;
646 ksft_print_msg("Spurious event generated for %s\n",
648 show_values(ctl, initial_val, read_val);
649 ctl->event_spurious++;
655 * Use the libray to compare values, if there's a mismatch
656 * carry on and try to provide a more useful diagnostic than
659 if (!snd_ctl_elem_value_compare(expected_val, read_val))
662 mismatch_shown = false;
663 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
664 if (show_mismatch(ctl, i, read_val, expected_val))
665 mismatch_shown = true;
668 ksft_print_msg("%s read and written values differ\n",
675 * Make sure we can write the default value back to the control, this
676 * should validate that at least some write works.
678 static void test_ctl_write_default(struct ctl_data *ctl)
682 /* If the control is turned off let's be polite */
683 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
684 ksft_print_msg("%s is inactive\n", ctl->name);
685 ksft_test_result_skip("write_default.%d.%d\n",
686 ctl->card->card, ctl->elem);
690 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
691 ksft_print_msg("%s is not writeable\n", ctl->name);
692 ksft_test_result_skip("write_default.%d.%d\n",
693 ctl->card->card, ctl->elem);
697 /* No idea what the default was for unreadable controls */
698 if (!snd_ctl_elem_info_is_readable(ctl->info)) {
699 ksft_print_msg("%s couldn't read default\n", ctl->name);
700 ksft_test_result_skip("write_default.%d.%d\n",
701 ctl->card->card, ctl->elem);
705 err = write_and_verify(ctl, ctl->def_val, NULL);
707 ksft_test_result(err >= 0, "write_default.%d.%d\n",
708 ctl->card->card, ctl->elem);
711 static bool test_ctl_write_valid_boolean(struct ctl_data *ctl)
715 snd_ctl_elem_value_t *val;
716 snd_ctl_elem_value_alloca(&val);
718 snd_ctl_elem_value_set_id(val, ctl->id);
720 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
721 for (j = 0; j < 2; j++) {
722 snd_ctl_elem_value_set_boolean(val, i, j);
723 err = write_and_verify(ctl, val, NULL);
732 static bool test_ctl_write_valid_integer(struct ctl_data *ctl)
738 snd_ctl_elem_value_t *val;
739 snd_ctl_elem_value_alloca(&val);
741 snd_ctl_elem_value_set_id(val, ctl->id);
743 step = snd_ctl_elem_info_get_step(ctl->info);
747 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
748 for (j = snd_ctl_elem_info_get_min(ctl->info);
749 j <= snd_ctl_elem_info_get_max(ctl->info); j += step) {
751 snd_ctl_elem_value_set_integer(val, i, j);
752 err = write_and_verify(ctl, val, NULL);
762 static bool test_ctl_write_valid_integer64(struct ctl_data *ctl)
767 snd_ctl_elem_value_t *val;
768 snd_ctl_elem_value_alloca(&val);
770 snd_ctl_elem_value_set_id(val, ctl->id);
772 step = snd_ctl_elem_info_get_step64(ctl->info);
776 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
777 for (j = snd_ctl_elem_info_get_min64(ctl->info);
778 j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) {
780 snd_ctl_elem_value_set_integer64(val, i, j);
781 err = write_and_verify(ctl, val, NULL);
790 static bool test_ctl_write_valid_enumerated(struct ctl_data *ctl)
794 snd_ctl_elem_value_t *val;
795 snd_ctl_elem_value_alloca(&val);
797 snd_ctl_elem_value_set_id(val, ctl->id);
799 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
800 for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) {
801 snd_ctl_elem_value_set_enumerated(val, i, j);
802 err = write_and_verify(ctl, val, NULL);
811 static void test_ctl_write_valid(struct ctl_data *ctl)
815 /* If the control is turned off let's be polite */
816 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
817 ksft_print_msg("%s is inactive\n", ctl->name);
818 ksft_test_result_skip("write_valid.%d.%d\n",
819 ctl->card->card, ctl->elem);
823 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
824 ksft_print_msg("%s is not writeable\n", ctl->name);
825 ksft_test_result_skip("write_valid.%d.%d\n",
826 ctl->card->card, ctl->elem);
830 switch (snd_ctl_elem_info_get_type(ctl->info)) {
831 case SND_CTL_ELEM_TYPE_BOOLEAN:
832 pass = test_ctl_write_valid_boolean(ctl);
835 case SND_CTL_ELEM_TYPE_INTEGER:
836 pass = test_ctl_write_valid_integer(ctl);
839 case SND_CTL_ELEM_TYPE_INTEGER64:
840 pass = test_ctl_write_valid_integer64(ctl);
843 case SND_CTL_ELEM_TYPE_ENUMERATED:
844 pass = test_ctl_write_valid_enumerated(ctl);
848 /* No tests for this yet */
849 ksft_test_result_skip("write_valid.%d.%d\n",
850 ctl->card->card, ctl->elem);
854 /* Restore the default value to minimise disruption */
855 write_and_verify(ctl, ctl->def_val, NULL);
857 ksft_test_result(pass, "write_valid.%d.%d\n",
858 ctl->card->card, ctl->elem);
861 static bool test_ctl_write_invalid_value(struct ctl_data *ctl,
862 snd_ctl_elem_value_t *val)
866 /* Ideally this will fail... */
867 err = snd_ctl_elem_write(ctl->card->handle, val);
871 /* ...but some devices will clamp to an in range value */
872 err = snd_ctl_elem_read(ctl->card->handle, val);
874 ksft_print_msg("%s failed to read: %s\n",
875 ctl->name, snd_strerror(err));
879 return !ctl_value_valid(ctl, val);
882 static bool test_ctl_write_invalid_boolean(struct ctl_data *ctl)
886 snd_ctl_elem_value_t *val;
887 snd_ctl_elem_value_alloca(&val);
889 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
890 snd_ctl_elem_value_copy(val, ctl->def_val);
891 snd_ctl_elem_value_set_boolean(val, i, 2);
893 if (test_ctl_write_invalid_value(ctl, val))
900 static bool test_ctl_write_invalid_integer(struct ctl_data *ctl)
904 snd_ctl_elem_value_t *val;
905 snd_ctl_elem_value_alloca(&val);
907 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
908 if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) {
909 /* Just under range */
910 snd_ctl_elem_value_copy(val, ctl->def_val);
911 snd_ctl_elem_value_set_integer(val, i,
912 snd_ctl_elem_info_get_min(ctl->info) - 1);
914 if (test_ctl_write_invalid_value(ctl, val))
917 /* Minimum representable value */
918 snd_ctl_elem_value_copy(val, ctl->def_val);
919 snd_ctl_elem_value_set_integer(val, i, LONG_MIN);
921 if (test_ctl_write_invalid_value(ctl, val))
925 if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) {
926 /* Just over range */
927 snd_ctl_elem_value_copy(val, ctl->def_val);
928 snd_ctl_elem_value_set_integer(val, i,
929 snd_ctl_elem_info_get_max(ctl->info) + 1);
931 if (test_ctl_write_invalid_value(ctl, val))
934 /* Maximum representable value */
935 snd_ctl_elem_value_copy(val, ctl->def_val);
936 snd_ctl_elem_value_set_integer(val, i, LONG_MAX);
938 if (test_ctl_write_invalid_value(ctl, val))
946 static bool test_ctl_write_invalid_integer64(struct ctl_data *ctl)
950 snd_ctl_elem_value_t *val;
951 snd_ctl_elem_value_alloca(&val);
953 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
954 if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) {
955 /* Just under range */
956 snd_ctl_elem_value_copy(val, ctl->def_val);
957 snd_ctl_elem_value_set_integer64(val, i,
958 snd_ctl_elem_info_get_min64(ctl->info) - 1);
960 if (test_ctl_write_invalid_value(ctl, val))
963 /* Minimum representable value */
964 snd_ctl_elem_value_copy(val, ctl->def_val);
965 snd_ctl_elem_value_set_integer64(val, i, LLONG_MIN);
967 if (test_ctl_write_invalid_value(ctl, val))
971 if (snd_ctl_elem_info_get_max64(ctl->info) != LLONG_MAX) {
972 /* Just over range */
973 snd_ctl_elem_value_copy(val, ctl->def_val);
974 snd_ctl_elem_value_set_integer64(val, i,
975 snd_ctl_elem_info_get_max64(ctl->info) + 1);
977 if (test_ctl_write_invalid_value(ctl, val))
980 /* Maximum representable value */
981 snd_ctl_elem_value_copy(val, ctl->def_val);
982 snd_ctl_elem_value_set_integer64(val, i, LLONG_MAX);
984 if (test_ctl_write_invalid_value(ctl, val))
992 static bool test_ctl_write_invalid_enumerated(struct ctl_data *ctl)
996 snd_ctl_elem_value_t *val;
997 snd_ctl_elem_value_alloca(&val);
999 snd_ctl_elem_value_set_id(val, ctl->id);
1001 for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
1002 /* One beyond maximum */
1003 snd_ctl_elem_value_copy(val, ctl->def_val);
1004 snd_ctl_elem_value_set_enumerated(val, i,
1005 snd_ctl_elem_info_get_items(ctl->info));
1007 if (test_ctl_write_invalid_value(ctl, val))
1010 /* Maximum representable value */
1011 snd_ctl_elem_value_copy(val, ctl->def_val);
1012 snd_ctl_elem_value_set_enumerated(val, i, UINT_MAX);
1014 if (test_ctl_write_invalid_value(ctl, val))
1023 static void test_ctl_write_invalid(struct ctl_data *ctl)
1027 /* If the control is turned off let's be polite */
1028 if (snd_ctl_elem_info_is_inactive(ctl->info)) {
1029 ksft_print_msg("%s is inactive\n", ctl->name);
1030 ksft_test_result_skip("write_invalid.%d.%d\n",
1031 ctl->card->card, ctl->elem);
1035 if (!snd_ctl_elem_info_is_writable(ctl->info)) {
1036 ksft_print_msg("%s is not writeable\n", ctl->name);
1037 ksft_test_result_skip("write_invalid.%d.%d\n",
1038 ctl->card->card, ctl->elem);
1042 switch (snd_ctl_elem_info_get_type(ctl->info)) {
1043 case SND_CTL_ELEM_TYPE_BOOLEAN:
1044 pass = test_ctl_write_invalid_boolean(ctl);
1047 case SND_CTL_ELEM_TYPE_INTEGER:
1048 pass = test_ctl_write_invalid_integer(ctl);
1051 case SND_CTL_ELEM_TYPE_INTEGER64:
1052 pass = test_ctl_write_invalid_integer64(ctl);
1055 case SND_CTL_ELEM_TYPE_ENUMERATED:
1056 pass = test_ctl_write_invalid_enumerated(ctl);
1060 /* No tests for this yet */
1061 ksft_test_result_skip("write_invalid.%d.%d\n",
1062 ctl->card->card, ctl->elem);
1066 /* Restore the default value to minimise disruption */
1067 write_and_verify(ctl, ctl->def_val, NULL);
1069 ksft_test_result(pass, "write_invalid.%d.%d\n",
1070 ctl->card->card, ctl->elem);
1073 static void test_ctl_event_missing(struct ctl_data *ctl)
1075 ksft_test_result(!ctl->event_missing, "event_missing.%d.%d\n",
1076 ctl->card->card, ctl->elem);
1079 static void test_ctl_event_spurious(struct ctl_data *ctl)
1081 ksft_test_result(!ctl->event_spurious, "event_spurious.%d.%d\n",
1082 ctl->card->card, ctl->elem);
1087 struct ctl_data *ctl;
1089 ksft_print_header();
1093 ksft_set_plan(num_controls * TESTS_PER_CONTROL);
1095 for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) {
1097 * Must test get_value() before we write anything, the
1098 * test stores the default value for later cleanup.
1100 test_ctl_get_value(ctl);
1102 test_ctl_write_default(ctl);
1103 test_ctl_write_valid(ctl);
1104 test_ctl_write_invalid(ctl);
1105 test_ctl_event_missing(ctl);
1106 test_ctl_event_spurious(ctl);