xref: /openbmc/linux/tools/testing/selftests/alsa/mixer-test.c (revision 7c79b10bea7a9dc1b74e2dbf4a35c19455371662)
15aaf9effSMark Brown // SPDX-License-Identifier: GPL-2.0
25aaf9effSMark Brown //
35aaf9effSMark Brown // kselftest for the ALSA mixer API
45aaf9effSMark Brown //
55aaf9effSMark Brown // Original author: Mark Brown <broonie@kernel.org>
6b1446bdaSMark Brown // Copyright (c) 2021-2 Arm Limited
75aaf9effSMark Brown 
85aaf9effSMark Brown // This test will iterate over all cards detected in the system, exercising
95aaf9effSMark Brown // every mixer control it can find.  This may conflict with other system
105aaf9effSMark Brown // software if there is audio activity so is best run on a system with a
115aaf9effSMark Brown // minimal active userspace.
125aaf9effSMark Brown 
135aaf9effSMark Brown #include <stdio.h>
145aaf9effSMark Brown #include <stdlib.h>
155aaf9effSMark Brown #include <stdbool.h>
1688b61322SMark Brown #include <limits.h>
175aaf9effSMark Brown #include <string.h>
185aaf9effSMark Brown #include <getopt.h>
195aaf9effSMark Brown #include <stdarg.h>
205aaf9effSMark Brown #include <ctype.h>
215aaf9effSMark Brown #include <math.h>
225aaf9effSMark Brown #include <errno.h>
235aaf9effSMark Brown #include <assert.h>
245aaf9effSMark Brown #include <alsa/asoundlib.h>
255aaf9effSMark Brown #include <poll.h>
265aaf9effSMark Brown #include <stdint.h>
275aaf9effSMark Brown 
285aaf9effSMark Brown #include "../kselftest.h"
29b310092eSJaroslav Kysela #include "alsa-local.h"
305aaf9effSMark Brown 
31c92b576aSMark Brown #define TESTS_PER_CONTROL 7
325aaf9effSMark Brown 
335aaf9effSMark Brown struct card_data {
345aaf9effSMark Brown 	snd_ctl_t *handle;
355aaf9effSMark Brown 	int card;
36b1446bdaSMark Brown 	struct pollfd pollfd;
375aaf9effSMark Brown 	int num_ctls;
385aaf9effSMark Brown 	snd_ctl_elem_list_t *ctls;
395aaf9effSMark Brown 	struct card_data *next;
405aaf9effSMark Brown };
415aaf9effSMark Brown 
425aaf9effSMark Brown struct ctl_data {
435aaf9effSMark Brown 	const char *name;
445aaf9effSMark Brown 	snd_ctl_elem_id_t *id;
455aaf9effSMark Brown 	snd_ctl_elem_info_t *info;
465aaf9effSMark Brown 	snd_ctl_elem_value_t *def_val;
475aaf9effSMark Brown 	int elem;
48b1446bdaSMark Brown 	int event_missing;
49b1446bdaSMark Brown 	int event_spurious;
505aaf9effSMark Brown 	struct card_data *card;
515aaf9effSMark Brown 	struct ctl_data *next;
525aaf9effSMark Brown };
535aaf9effSMark Brown 
545aaf9effSMark Brown int num_cards = 0;
555aaf9effSMark Brown int num_controls = 0;
565aaf9effSMark Brown struct card_data *card_list = NULL;
575aaf9effSMark Brown struct ctl_data *ctl_list = NULL;
585aaf9effSMark Brown 
599d73d192SMark Brown static void find_controls(void)
605aaf9effSMark Brown {
615aaf9effSMark Brown 	char name[32];
625aaf9effSMark Brown 	int card, ctl, err;
635aaf9effSMark Brown 	struct card_data *card_data;
645aaf9effSMark Brown 	struct ctl_data *ctl_data;
65b73dad80SJaroslav Kysela 	snd_config_t *config;
665aaf9effSMark Brown 
675aaf9effSMark Brown 	card = -1;
685aaf9effSMark Brown 	if (snd_card_next(&card) < 0 || card < 0)
695aaf9effSMark Brown 		return;
705aaf9effSMark Brown 
71b310092eSJaroslav Kysela 	config = get_alsalib_config();
72b73dad80SJaroslav Kysela 
735aaf9effSMark Brown 	while (card >= 0) {
745aaf9effSMark Brown 		sprintf(name, "hw:%d", card);
755aaf9effSMark Brown 
765aaf9effSMark Brown 		card_data = malloc(sizeof(*card_data));
775aaf9effSMark Brown 		if (!card_data)
785aaf9effSMark Brown 			ksft_exit_fail_msg("Out of memory\n");
795aaf9effSMark Brown 
80b73dad80SJaroslav Kysela 		err = snd_ctl_open_lconf(&card_data->handle, name, 0, config);
815aaf9effSMark Brown 		if (err < 0) {
825aaf9effSMark Brown 			ksft_print_msg("Failed to get hctl for card %d: %s\n",
835aaf9effSMark Brown 				       card, snd_strerror(err));
845aaf9effSMark Brown 			goto next_card;
855aaf9effSMark Brown 		}
865aaf9effSMark Brown 
875aaf9effSMark Brown 		/* Count controls */
885aaf9effSMark Brown 		snd_ctl_elem_list_malloc(&card_data->ctls);
895aaf9effSMark Brown 		snd_ctl_elem_list(card_data->handle, card_data->ctls);
905aaf9effSMark Brown 		card_data->num_ctls = snd_ctl_elem_list_get_count(card_data->ctls);
915aaf9effSMark Brown 
925aaf9effSMark Brown 		/* Enumerate control information */
935aaf9effSMark Brown 		snd_ctl_elem_list_alloc_space(card_data->ctls, card_data->num_ctls);
945aaf9effSMark Brown 		snd_ctl_elem_list(card_data->handle, card_data->ctls);
955aaf9effSMark Brown 
965aaf9effSMark Brown 		card_data->card = num_cards++;
975aaf9effSMark Brown 		card_data->next = card_list;
985aaf9effSMark Brown 		card_list = card_data;
995aaf9effSMark Brown 
1005aaf9effSMark Brown 		num_controls += card_data->num_ctls;
1015aaf9effSMark Brown 
1025aaf9effSMark Brown 		for (ctl = 0; ctl < card_data->num_ctls; ctl++) {
1035aaf9effSMark Brown 			ctl_data = malloc(sizeof(*ctl_data));
1045aaf9effSMark Brown 			if (!ctl_data)
1055aaf9effSMark Brown 				ksft_exit_fail_msg("Out of memory\n");
1065aaf9effSMark Brown 
107b1446bdaSMark Brown 			memset(ctl_data, 0, sizeof(*ctl_data));
1085aaf9effSMark Brown 			ctl_data->card = card_data;
1095aaf9effSMark Brown 			ctl_data->elem = ctl;
1105aaf9effSMark Brown 			ctl_data->name = snd_ctl_elem_list_get_name(card_data->ctls,
1115aaf9effSMark Brown 								    ctl);
1125aaf9effSMark Brown 
1135aaf9effSMark Brown 			err = snd_ctl_elem_id_malloc(&ctl_data->id);
1145aaf9effSMark Brown 			if (err < 0)
1155aaf9effSMark Brown 				ksft_exit_fail_msg("Out of memory\n");
1165aaf9effSMark Brown 
1175aaf9effSMark Brown 			err = snd_ctl_elem_info_malloc(&ctl_data->info);
1185aaf9effSMark Brown 			if (err < 0)
1195aaf9effSMark Brown 				ksft_exit_fail_msg("Out of memory\n");
1205aaf9effSMark Brown 
1215aaf9effSMark Brown 			err = snd_ctl_elem_value_malloc(&ctl_data->def_val);
1225aaf9effSMark Brown 			if (err < 0)
1235aaf9effSMark Brown 				ksft_exit_fail_msg("Out of memory\n");
1245aaf9effSMark Brown 
1255aaf9effSMark Brown 			snd_ctl_elem_list_get_id(card_data->ctls, ctl,
1265aaf9effSMark Brown 						 ctl_data->id);
1275aaf9effSMark Brown 			snd_ctl_elem_info_set_id(ctl_data->info, ctl_data->id);
1285aaf9effSMark Brown 			err = snd_ctl_elem_info(card_data->handle,
1295aaf9effSMark Brown 						ctl_data->info);
1305aaf9effSMark Brown 			if (err < 0) {
1315aaf9effSMark Brown 				ksft_print_msg("%s getting info for %d\n",
1325aaf9effSMark Brown 					       snd_strerror(err),
1335aaf9effSMark Brown 					       ctl_data->name);
1345aaf9effSMark Brown 			}
1355aaf9effSMark Brown 
1365aaf9effSMark Brown 			snd_ctl_elem_value_set_id(ctl_data->def_val,
1375aaf9effSMark Brown 						  ctl_data->id);
1385aaf9effSMark Brown 
1395aaf9effSMark Brown 			ctl_data->next = ctl_list;
1405aaf9effSMark Brown 			ctl_list = ctl_data;
1415aaf9effSMark Brown 		}
1425aaf9effSMark Brown 
143b1446bdaSMark Brown 		/* Set up for events */
144b1446bdaSMark Brown 		err = snd_ctl_subscribe_events(card_data->handle, true);
145b1446bdaSMark Brown 		if (err < 0) {
146b1446bdaSMark Brown 			ksft_exit_fail_msg("snd_ctl_subscribe_events() failed for card %d: %d\n",
147b1446bdaSMark Brown 					   card, err);
148b1446bdaSMark Brown 		}
149b1446bdaSMark Brown 
150b1446bdaSMark Brown 		err = snd_ctl_poll_descriptors_count(card_data->handle);
151b1446bdaSMark Brown 		if (err != 1) {
1528f85b4daSColin Ian King 			ksft_exit_fail_msg("Unexpected descriptor count %d for card %d\n",
153b1446bdaSMark Brown 					   err, card);
154b1446bdaSMark Brown 		}
155b1446bdaSMark Brown 
156b1446bdaSMark Brown 		err = snd_ctl_poll_descriptors(card_data->handle,
157b1446bdaSMark Brown 					       &card_data->pollfd, 1);
158b1446bdaSMark Brown 		if (err != 1) {
159b1446bdaSMark Brown 			ksft_exit_fail_msg("snd_ctl_poll_descriptors() failed for %d\n",
160b1446bdaSMark Brown 				       card, err);
161b1446bdaSMark Brown 		}
162b1446bdaSMark Brown 
1635aaf9effSMark Brown 	next_card:
1645aaf9effSMark Brown 		if (snd_card_next(&card) < 0) {
1655aaf9effSMark Brown 			ksft_print_msg("snd_card_next");
1665aaf9effSMark Brown 			break;
1675aaf9effSMark Brown 		}
1685aaf9effSMark Brown 	}
169b73dad80SJaroslav Kysela 
170b73dad80SJaroslav Kysela 	snd_config_delete(config);
1715aaf9effSMark Brown }
1725aaf9effSMark Brown 
173b1446bdaSMark Brown /*
174b1446bdaSMark Brown  * Block for up to timeout ms for an event, returns a negative value
175b1446bdaSMark Brown  * on error, 0 for no event and 1 for an event.
176b1446bdaSMark Brown  */
1779d73d192SMark Brown static int wait_for_event(struct ctl_data *ctl, int timeout)
178b1446bdaSMark Brown {
179b1446bdaSMark Brown 	unsigned short revents;
180b1446bdaSMark Brown 	snd_ctl_event_t *event;
181b1446bdaSMark Brown 	int count, err;
182b1446bdaSMark Brown 	unsigned int mask = 0;
183b1446bdaSMark Brown 	unsigned int ev_id;
184b1446bdaSMark Brown 
185b1446bdaSMark Brown 	snd_ctl_event_alloca(&event);
186b1446bdaSMark Brown 
187b1446bdaSMark Brown 	do {
188b1446bdaSMark Brown 		err = poll(&(ctl->card->pollfd), 1, timeout);
189b1446bdaSMark Brown 		if (err < 0) {
190b1446bdaSMark Brown 			ksft_print_msg("poll() failed for %s: %s (%d)\n",
191b1446bdaSMark Brown 				       ctl->name, strerror(errno), errno);
192b1446bdaSMark Brown 			return -1;
193b1446bdaSMark Brown 		}
194b1446bdaSMark Brown 		/* Timeout */
195b1446bdaSMark Brown 		if (err == 0)
196b1446bdaSMark Brown 			return 0;
197b1446bdaSMark Brown 
198b1446bdaSMark Brown 		err = snd_ctl_poll_descriptors_revents(ctl->card->handle,
199b1446bdaSMark Brown 						       &(ctl->card->pollfd),
200b1446bdaSMark Brown 						       1, &revents);
201b1446bdaSMark Brown 		if (err < 0) {
2028f85b4daSColin Ian King 			ksft_print_msg("snd_ctl_poll_descriptors_revents() failed for %s: %d\n",
203b1446bdaSMark Brown 				       ctl->name, err);
204b1446bdaSMark Brown 			return err;
205b1446bdaSMark Brown 		}
206b1446bdaSMark Brown 		if (revents & POLLERR) {
2078f85b4daSColin Ian King 			ksft_print_msg("snd_ctl_poll_descriptors_revents() reported POLLERR for %s\n",
208b1446bdaSMark Brown 				       ctl->name);
209b1446bdaSMark Brown 			return -1;
210b1446bdaSMark Brown 		}
211b1446bdaSMark Brown 		/* No read events */
212b1446bdaSMark Brown 		if (!(revents & POLLIN)) {
213b1446bdaSMark Brown 			ksft_print_msg("No POLLIN\n");
214b1446bdaSMark Brown 			continue;
215b1446bdaSMark Brown 		}
216b1446bdaSMark Brown 
217b1446bdaSMark Brown 		err = snd_ctl_read(ctl->card->handle, event);
218b1446bdaSMark Brown 		if (err < 0) {
219b1446bdaSMark Brown 			ksft_print_msg("snd_ctl_read() failed for %s: %d\n",
220b1446bdaSMark Brown 			       ctl->name, err);
221b1446bdaSMark Brown 			return err;
222b1446bdaSMark Brown 		}
223b1446bdaSMark Brown 
224b1446bdaSMark Brown 		if (snd_ctl_event_get_type(event) != SND_CTL_EVENT_ELEM)
225b1446bdaSMark Brown 			continue;
226b1446bdaSMark Brown 
227b1446bdaSMark Brown 		/* The ID returned from the event is 1 less than numid */
228b1446bdaSMark Brown 		mask = snd_ctl_event_elem_get_mask(event);
229b1446bdaSMark Brown 		ev_id = snd_ctl_event_elem_get_numid(event);
230b1446bdaSMark Brown 		if (ev_id != snd_ctl_elem_info_get_numid(ctl->info)) {
231b1446bdaSMark Brown 			ksft_print_msg("Event for unexpected ctl %s\n",
232b1446bdaSMark Brown 				       snd_ctl_event_elem_get_name(event));
233b1446bdaSMark Brown 			continue;
234b1446bdaSMark Brown 		}
235b1446bdaSMark Brown 
236b1446bdaSMark Brown 		if ((mask & SND_CTL_EVENT_MASK_REMOVE) == SND_CTL_EVENT_MASK_REMOVE) {
237b1446bdaSMark Brown 			ksft_print_msg("Removal event for %s\n",
238b1446bdaSMark Brown 				       ctl->name);
239b1446bdaSMark Brown 			return -1;
240b1446bdaSMark Brown 		}
241b1446bdaSMark Brown 	} while ((mask & SND_CTL_EVENT_MASK_VALUE) != SND_CTL_EVENT_MASK_VALUE);
242b1446bdaSMark Brown 
243b1446bdaSMark Brown 	return 1;
244b1446bdaSMark Brown }
245b1446bdaSMark Brown 
2469d73d192SMark Brown static bool ctl_value_index_valid(struct ctl_data *ctl,
2479d73d192SMark Brown 				  snd_ctl_elem_value_t *val,
2483f48b137SMark Brown 				  int index)
2493f48b137SMark Brown {
2503f48b137SMark Brown 	long int_val;
2513f48b137SMark Brown 	long long int64_val;
2523f48b137SMark Brown 
2533f48b137SMark Brown 	switch (snd_ctl_elem_info_get_type(ctl->info)) {
2543f48b137SMark Brown 	case SND_CTL_ELEM_TYPE_NONE:
2553f48b137SMark Brown 		ksft_print_msg("%s.%d Invalid control type NONE\n",
2563f48b137SMark Brown 			       ctl->name, index);
2573f48b137SMark Brown 		return false;
2583f48b137SMark Brown 
2593f48b137SMark Brown 	case SND_CTL_ELEM_TYPE_BOOLEAN:
2603f48b137SMark Brown 		int_val = snd_ctl_elem_value_get_boolean(val, index);
2613f48b137SMark Brown 		switch (int_val) {
2623f48b137SMark Brown 		case 0:
2633f48b137SMark Brown 		case 1:
2643f48b137SMark Brown 			break;
2653f48b137SMark Brown 		default:
2663f48b137SMark Brown 			ksft_print_msg("%s.%d Invalid boolean value %ld\n",
2673f48b137SMark Brown 				       ctl->name, index, int_val);
2683f48b137SMark Brown 			return false;
2693f48b137SMark Brown 		}
2703f48b137SMark Brown 		break;
2713f48b137SMark Brown 
2723f48b137SMark Brown 	case SND_CTL_ELEM_TYPE_INTEGER:
2733f48b137SMark Brown 		int_val = snd_ctl_elem_value_get_integer(val, index);
2743f48b137SMark Brown 
2753f48b137SMark Brown 		if (int_val < snd_ctl_elem_info_get_min(ctl->info)) {
2763f48b137SMark Brown 			ksft_print_msg("%s.%d value %ld less than minimum %ld\n",
2773f48b137SMark Brown 				       ctl->name, index, int_val,
2783f48b137SMark Brown 				       snd_ctl_elem_info_get_min(ctl->info));
2793f48b137SMark Brown 			return false;
2803f48b137SMark Brown 		}
2813f48b137SMark Brown 
2823f48b137SMark Brown 		if (int_val > snd_ctl_elem_info_get_max(ctl->info)) {
2833f48b137SMark Brown 			ksft_print_msg("%s.%d value %ld more than maximum %ld\n",
2843f48b137SMark Brown 				       ctl->name, index, int_val,
2853f48b137SMark Brown 				       snd_ctl_elem_info_get_max(ctl->info));
2863f48b137SMark Brown 			return false;
2873f48b137SMark Brown 		}
2883f48b137SMark Brown 
2893f48b137SMark Brown 		/* Only check step size if there is one and we're in bounds */
2903f48b137SMark Brown 		if (snd_ctl_elem_info_get_step(ctl->info) &&
2913f48b137SMark Brown 		    (int_val - snd_ctl_elem_info_get_min(ctl->info) %
2923f48b137SMark Brown 		     snd_ctl_elem_info_get_step(ctl->info))) {
2933f48b137SMark Brown 			ksft_print_msg("%s.%d value %ld invalid for step %ld minimum %ld\n",
2943f48b137SMark Brown 				       ctl->name, index, int_val,
2953f48b137SMark Brown 				       snd_ctl_elem_info_get_step(ctl->info),
2963f48b137SMark Brown 				       snd_ctl_elem_info_get_min(ctl->info));
2973f48b137SMark Brown 			return false;
2983f48b137SMark Brown 		}
2993f48b137SMark Brown 		break;
3003f48b137SMark Brown 
3013f48b137SMark Brown 	case SND_CTL_ELEM_TYPE_INTEGER64:
3023f48b137SMark Brown 		int64_val = snd_ctl_elem_value_get_integer64(val, index);
3033f48b137SMark Brown 
3043f48b137SMark Brown 		if (int64_val < snd_ctl_elem_info_get_min64(ctl->info)) {
3053f48b137SMark Brown 			ksft_print_msg("%s.%d value %lld less than minimum %lld\n",
3063f48b137SMark Brown 				       ctl->name, index, int64_val,
3073f48b137SMark Brown 				       snd_ctl_elem_info_get_min64(ctl->info));
3083f48b137SMark Brown 			return false;
3093f48b137SMark Brown 		}
3103f48b137SMark Brown 
3113f48b137SMark Brown 		if (int64_val > snd_ctl_elem_info_get_max64(ctl->info)) {
3123f48b137SMark Brown 			ksft_print_msg("%s.%d value %lld more than maximum %lld\n",
3133f48b137SMark Brown 				       ctl->name, index, int64_val,
3143f48b137SMark Brown 				       snd_ctl_elem_info_get_max(ctl->info));
3153f48b137SMark Brown 			return false;
3163f48b137SMark Brown 		}
3173f48b137SMark Brown 
3183f48b137SMark Brown 		/* Only check step size if there is one and we're in bounds */
3193f48b137SMark Brown 		if (snd_ctl_elem_info_get_step64(ctl->info) &&
3203f48b137SMark Brown 		    (int64_val - snd_ctl_elem_info_get_min64(ctl->info)) %
3213f48b137SMark Brown 		    snd_ctl_elem_info_get_step64(ctl->info)) {
3223f48b137SMark Brown 			ksft_print_msg("%s.%d value %lld invalid for step %lld minimum %lld\n",
3233f48b137SMark Brown 				       ctl->name, index, int64_val,
3243f48b137SMark Brown 				       snd_ctl_elem_info_get_step64(ctl->info),
3253f48b137SMark Brown 				       snd_ctl_elem_info_get_min64(ctl->info));
3263f48b137SMark Brown 			return false;
3273f48b137SMark Brown 		}
3283f48b137SMark Brown 		break;
3293f48b137SMark Brown 
33010f2f194SMark Brown 	case SND_CTL_ELEM_TYPE_ENUMERATED:
33110f2f194SMark Brown 		int_val = snd_ctl_elem_value_get_enumerated(val, index);
33210f2f194SMark Brown 
33310f2f194SMark Brown 		if (int_val < 0) {
33410f2f194SMark Brown 			ksft_print_msg("%s.%d negative value %ld for enumeration\n",
33510f2f194SMark Brown 				       ctl->name, index, int_val);
33610f2f194SMark Brown 			return false;
33710f2f194SMark Brown 		}
33810f2f194SMark Brown 
33910f2f194SMark Brown 		if (int_val >= snd_ctl_elem_info_get_items(ctl->info)) {
34010f2f194SMark Brown 			ksft_print_msg("%s.%d value %ld more than item count %ld\n",
34110f2f194SMark Brown 				       ctl->name, index, int_val,
34210f2f194SMark Brown 				       snd_ctl_elem_info_get_items(ctl->info));
34310f2f194SMark Brown 			return false;
34410f2f194SMark Brown 		}
34510f2f194SMark Brown 		break;
34610f2f194SMark Brown 
3473f48b137SMark Brown 	default:
3483f48b137SMark Brown 		/* No tests for other types */
3493f48b137SMark Brown 		break;
3503f48b137SMark Brown 	}
3513f48b137SMark Brown 
3523f48b137SMark Brown 	return true;
3533f48b137SMark Brown }
3543f48b137SMark Brown 
3553f48b137SMark Brown /*
3563f48b137SMark Brown  * Check that the provided value meets the constraints for the
3573f48b137SMark Brown  * provided control.
3583f48b137SMark Brown  */
3599d73d192SMark Brown static bool ctl_value_valid(struct ctl_data *ctl, snd_ctl_elem_value_t *val)
3603f48b137SMark Brown {
3613f48b137SMark Brown 	int i;
3623f48b137SMark Brown 	bool valid = true;
3633f48b137SMark Brown 
3643f48b137SMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
3653f48b137SMark Brown 		if (!ctl_value_index_valid(ctl, val, i))
3663f48b137SMark Brown 			valid = false;
3673f48b137SMark Brown 
3683f48b137SMark Brown 	return valid;
3693f48b137SMark Brown }
3703f48b137SMark Brown 
3715aaf9effSMark Brown /*
3725aaf9effSMark Brown  * Check that we can read the default value and it is valid. Write
3735aaf9effSMark Brown  * tests use the read value to restore the default.
3745aaf9effSMark Brown  */
3759d73d192SMark Brown static void test_ctl_get_value(struct ctl_data *ctl)
3765aaf9effSMark Brown {
3775aaf9effSMark Brown 	int err;
3785aaf9effSMark Brown 
3795aaf9effSMark Brown 	/* If the control is turned off let's be polite */
3805aaf9effSMark Brown 	if (snd_ctl_elem_info_is_inactive(ctl->info)) {
3815aaf9effSMark Brown 		ksft_print_msg("%s is inactive\n", ctl->name);
3825aaf9effSMark Brown 		ksft_test_result_skip("get_value.%d.%d\n",
3835aaf9effSMark Brown 				      ctl->card->card, ctl->elem);
3845aaf9effSMark Brown 		return;
3855aaf9effSMark Brown 	}
3865aaf9effSMark Brown 
3875aaf9effSMark Brown 	/* Can't test reading on an unreadable control */
3885aaf9effSMark Brown 	if (!snd_ctl_elem_info_is_readable(ctl->info)) {
3895aaf9effSMark Brown 		ksft_print_msg("%s is not readable\n", ctl->name);
3905aaf9effSMark Brown 		ksft_test_result_skip("get_value.%d.%d\n",
3915aaf9effSMark Brown 				      ctl->card->card, ctl->elem);
3925aaf9effSMark Brown 		return;
3935aaf9effSMark Brown 	}
3945aaf9effSMark Brown 
3955aaf9effSMark Brown 	err = snd_ctl_elem_read(ctl->card->handle, ctl->def_val);
3965aaf9effSMark Brown 	if (err < 0) {
3975aaf9effSMark Brown 		ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
3985aaf9effSMark Brown 			       snd_strerror(err));
3995aaf9effSMark Brown 		goto out;
4005aaf9effSMark Brown 	}
4015aaf9effSMark Brown 
4023f48b137SMark Brown 	if (!ctl_value_valid(ctl, ctl->def_val))
4033f48b137SMark Brown 		err = -EINVAL;
4045aaf9effSMark Brown 
4055aaf9effSMark Brown out:
4065aaf9effSMark Brown 	ksft_test_result(err >= 0, "get_value.%d.%d\n",
4075aaf9effSMark Brown 			 ctl->card->card, ctl->elem);
4085aaf9effSMark Brown }
4095aaf9effSMark Brown 
410c92b576aSMark Brown static bool strend(const char *haystack, const char *needle)
411c92b576aSMark Brown {
412c92b576aSMark Brown 	size_t haystack_len = strlen(haystack);
413c92b576aSMark Brown 	size_t needle_len = strlen(needle);
414c92b576aSMark Brown 
415c92b576aSMark Brown 	if (needle_len > haystack_len)
416c92b576aSMark Brown 		return false;
417c92b576aSMark Brown 	return strcmp(haystack + haystack_len - needle_len, needle) == 0;
418c92b576aSMark Brown }
419c92b576aSMark Brown 
420c92b576aSMark Brown static void test_ctl_name(struct ctl_data *ctl)
421c92b576aSMark Brown {
422c92b576aSMark Brown 	bool name_ok = true;
423c92b576aSMark Brown 	bool check;
424c92b576aSMark Brown 
425c92b576aSMark Brown 	/* Only boolean controls should end in Switch */
426c92b576aSMark Brown 	if (strend(ctl->name, " Switch")) {
427c92b576aSMark Brown 		if (snd_ctl_elem_info_get_type(ctl->info) != SND_CTL_ELEM_TYPE_BOOLEAN) {
428c92b576aSMark Brown 			ksft_print_msg("%d.%d %s ends in Switch but is not boolean\n",
429c92b576aSMark Brown 				       ctl->card->card, ctl->elem, ctl->name);
430c92b576aSMark Brown 			name_ok = false;
431c92b576aSMark Brown 		}
432c92b576aSMark Brown 	}
433c92b576aSMark Brown 
434c92b576aSMark Brown 	/* Writeable boolean controls should end in Switch */
435c92b576aSMark Brown 	if (snd_ctl_elem_info_get_type(ctl->info) == SND_CTL_ELEM_TYPE_BOOLEAN &&
436c92b576aSMark Brown 	    snd_ctl_elem_info_is_writable(ctl->info)) {
437c92b576aSMark Brown 		if (!strend(ctl->name, " Switch")) {
438c92b576aSMark Brown 			ksft_print_msg("%d.%d %s is a writeable boolean but not a Switch\n",
439c92b576aSMark Brown 				       ctl->card->card, ctl->elem, ctl->name);
440c92b576aSMark Brown 			name_ok = false;
441c92b576aSMark Brown 		}
442c92b576aSMark Brown 	}
443c92b576aSMark Brown 
444c92b576aSMark Brown 	ksft_test_result(name_ok, "name.%d.%d\n",
445c92b576aSMark Brown 			 ctl->card->card, ctl->elem);
446c92b576aSMark Brown }
447c92b576aSMark Brown 
4489d73d192SMark Brown static bool show_mismatch(struct ctl_data *ctl, int index,
4495aaf9effSMark Brown 			  snd_ctl_elem_value_t *read_val,
4505aaf9effSMark Brown 			  snd_ctl_elem_value_t *expected_val)
4515aaf9effSMark Brown {
4525aaf9effSMark Brown 	long long expected_int, read_int;
4535aaf9effSMark Brown 
4545aaf9effSMark Brown 	/*
4555aaf9effSMark Brown 	 * We factor out the code to compare values representable as
4565aaf9effSMark Brown 	 * integers, ensure that check doesn't log otherwise.
4575aaf9effSMark Brown 	 */
4585aaf9effSMark Brown 	expected_int = 0;
4595aaf9effSMark Brown 	read_int = 0;
4605aaf9effSMark Brown 
4615aaf9effSMark Brown 	switch (snd_ctl_elem_info_get_type(ctl->info)) {
4625aaf9effSMark Brown 	case SND_CTL_ELEM_TYPE_BOOLEAN:
4635aaf9effSMark Brown 		expected_int = snd_ctl_elem_value_get_boolean(expected_val,
4645aaf9effSMark Brown 							      index);
4655aaf9effSMark Brown 		read_int = snd_ctl_elem_value_get_boolean(read_val, index);
4665aaf9effSMark Brown 		break;
4675aaf9effSMark Brown 
4685aaf9effSMark Brown 	case SND_CTL_ELEM_TYPE_INTEGER:
4695aaf9effSMark Brown 		expected_int = snd_ctl_elem_value_get_integer(expected_val,
4705aaf9effSMark Brown 							      index);
4715aaf9effSMark Brown 		read_int = snd_ctl_elem_value_get_integer(read_val, index);
4725aaf9effSMark Brown 		break;
4735aaf9effSMark Brown 
4745aaf9effSMark Brown 	case SND_CTL_ELEM_TYPE_INTEGER64:
4755aaf9effSMark Brown 		expected_int = snd_ctl_elem_value_get_integer64(expected_val,
4765aaf9effSMark Brown 								index);
4775aaf9effSMark Brown 		read_int = snd_ctl_elem_value_get_integer64(read_val,
4785aaf9effSMark Brown 							    index);
4795aaf9effSMark Brown 		break;
4805aaf9effSMark Brown 
4815aaf9effSMark Brown 	case SND_CTL_ELEM_TYPE_ENUMERATED:
4825aaf9effSMark Brown 		expected_int = snd_ctl_elem_value_get_enumerated(expected_val,
4835aaf9effSMark Brown 								 index);
4845aaf9effSMark Brown 		read_int = snd_ctl_elem_value_get_enumerated(read_val,
4855aaf9effSMark Brown 							     index);
4865aaf9effSMark Brown 		break;
4875aaf9effSMark Brown 
4885aaf9effSMark Brown 	default:
4895aaf9effSMark Brown 		break;
4905aaf9effSMark Brown 	}
4915aaf9effSMark Brown 
4925aaf9effSMark Brown 	if (expected_int != read_int) {
4937cc994f2STakashi Sakamoto 		/*
4947cc994f2STakashi Sakamoto 		 * NOTE: The volatile attribute means that the hardware
4957cc994f2STakashi Sakamoto 		 * can voluntarily change the state of control element
4967cc994f2STakashi Sakamoto 		 * independent of any operation by software.
4977cc994f2STakashi Sakamoto 		 */
4987cc994f2STakashi Sakamoto 		bool is_volatile = snd_ctl_elem_info_is_volatile(ctl->info);
4997cc994f2STakashi Sakamoto 		ksft_print_msg("%s.%d expected %lld but read %lld, is_volatile %d\n",
5007cc994f2STakashi Sakamoto 			       ctl->name, index, expected_int, read_int, is_volatile);
5017cc994f2STakashi Sakamoto 		return !is_volatile;
5025aaf9effSMark Brown 	} else {
5035aaf9effSMark Brown 		return false;
5045aaf9effSMark Brown 	}
5055aaf9effSMark Brown }
5065aaf9effSMark Brown 
5075aaf9effSMark Brown /*
5085aaf9effSMark Brown  * Write a value then if possible verify that we get the expected
5095aaf9effSMark Brown  * result.  An optional expected value can be provided if we expect
5105aaf9effSMark Brown  * the write to fail, for verifying that invalid writes don't corrupt
5115aaf9effSMark Brown  * anything.
5125aaf9effSMark Brown  */
5139d73d192SMark Brown static int write_and_verify(struct ctl_data *ctl,
5145aaf9effSMark Brown 			    snd_ctl_elem_value_t *write_val,
5155aaf9effSMark Brown 			    snd_ctl_elem_value_t *expected_val)
5165aaf9effSMark Brown {
5175aaf9effSMark Brown 	int err, i;
5185aaf9effSMark Brown 	bool error_expected, mismatch_shown;
519b1446bdaSMark Brown 	snd_ctl_elem_value_t *initial_val, *read_val, *w_val;
520b1446bdaSMark Brown 	snd_ctl_elem_value_alloca(&initial_val);
5215aaf9effSMark Brown 	snd_ctl_elem_value_alloca(&read_val);
5225aaf9effSMark Brown 	snd_ctl_elem_value_alloca(&w_val);
5235aaf9effSMark Brown 
5245aaf9effSMark Brown 	/*
5255aaf9effSMark Brown 	 * We need to copy the write value since writing can modify
5265aaf9effSMark Brown 	 * the value which causes surprises, and allocate an expected
5275aaf9effSMark Brown 	 * value if we expect to read back what we wrote.
5285aaf9effSMark Brown 	 */
5295aaf9effSMark Brown 	snd_ctl_elem_value_copy(w_val, write_val);
5305aaf9effSMark Brown 	if (expected_val) {
5315aaf9effSMark Brown 		error_expected = true;
5325aaf9effSMark Brown 	} else {
5335aaf9effSMark Brown 		error_expected = false;
5345aaf9effSMark Brown 		snd_ctl_elem_value_alloca(&expected_val);
5355aaf9effSMark Brown 		snd_ctl_elem_value_copy(expected_val, write_val);
5365aaf9effSMark Brown 	}
5375aaf9effSMark Brown 
538b1446bdaSMark Brown 	/* Store the value before we write */
539b1446bdaSMark Brown 	if (snd_ctl_elem_info_is_readable(ctl->info)) {
540b1446bdaSMark Brown 		snd_ctl_elem_value_set_id(initial_val, ctl->id);
541b1446bdaSMark Brown 
542b1446bdaSMark Brown 		err = snd_ctl_elem_read(ctl->card->handle, initial_val);
543b1446bdaSMark Brown 		if (err < 0) {
544b1446bdaSMark Brown 			ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
545b1446bdaSMark Brown 				       snd_strerror(err));
546b1446bdaSMark Brown 			return err;
547b1446bdaSMark Brown 		}
548b1446bdaSMark Brown 	}
549b1446bdaSMark Brown 
5505aaf9effSMark Brown 	/*
5515aaf9effSMark Brown 	 * Do the write, if we have an expected value ignore the error
5525aaf9effSMark Brown 	 * and carry on to validate the expected value.
5535aaf9effSMark Brown 	 */
5545aaf9effSMark Brown 	err = snd_ctl_elem_write(ctl->card->handle, w_val);
5555aaf9effSMark Brown 	if (err < 0 && !error_expected) {
5565aaf9effSMark Brown 		ksft_print_msg("snd_ctl_elem_write() failed: %s\n",
5575aaf9effSMark Brown 			       snd_strerror(err));
5585aaf9effSMark Brown 		return err;
5595aaf9effSMark Brown 	}
5605aaf9effSMark Brown 
5615aaf9effSMark Brown 	/* Can we do the verification part? */
5625aaf9effSMark Brown 	if (!snd_ctl_elem_info_is_readable(ctl->info))
5635aaf9effSMark Brown 		return err;
5645aaf9effSMark Brown 
5655aaf9effSMark Brown 	snd_ctl_elem_value_set_id(read_val, ctl->id);
5665aaf9effSMark Brown 
5675aaf9effSMark Brown 	err = snd_ctl_elem_read(ctl->card->handle, read_val);
5685aaf9effSMark Brown 	if (err < 0) {
5695aaf9effSMark Brown 		ksft_print_msg("snd_ctl_elem_read() failed: %s\n",
5705aaf9effSMark Brown 			       snd_strerror(err));
5715aaf9effSMark Brown 		return err;
5725aaf9effSMark Brown 	}
5735aaf9effSMark Brown 
5745aaf9effSMark Brown 	/*
575b1446bdaSMark Brown 	 * Check for an event if the value changed, or confirm that
576b1446bdaSMark Brown 	 * there was none if it didn't.  We rely on the kernel
577b1446bdaSMark Brown 	 * generating the notification before it returns from the
578b1446bdaSMark Brown 	 * write, this is currently true, should that ever change this
579b1446bdaSMark Brown 	 * will most likely break and need updating.
580b1446bdaSMark Brown 	 */
581b1446bdaSMark Brown 	if (!snd_ctl_elem_info_is_volatile(ctl->info)) {
582b1446bdaSMark Brown 		err = wait_for_event(ctl, 0);
583b1446bdaSMark Brown 		if (snd_ctl_elem_value_compare(initial_val, read_val)) {
584b1446bdaSMark Brown 			if (err < 1) {
585b1446bdaSMark Brown 				ksft_print_msg("No event generated for %s\n",
586b1446bdaSMark Brown 					       ctl->name);
587b1446bdaSMark Brown 				ctl->event_missing++;
588b1446bdaSMark Brown 			}
589b1446bdaSMark Brown 		} else {
590b1446bdaSMark Brown 			if (err != 0) {
591b1446bdaSMark Brown 				ksft_print_msg("Spurious event generated for %s\n",
592b1446bdaSMark Brown 					       ctl->name);
593b1446bdaSMark Brown 				ctl->event_spurious++;
594b1446bdaSMark Brown 			}
595b1446bdaSMark Brown 		}
596b1446bdaSMark Brown 	}
597b1446bdaSMark Brown 
598b1446bdaSMark Brown 	/*
5995aaf9effSMark Brown 	 * Use the libray to compare values, if there's a mismatch
6005aaf9effSMark Brown 	 * carry on and try to provide a more useful diagnostic than
6015aaf9effSMark Brown 	 * just "mismatch".
6025aaf9effSMark Brown 	 */
6035aaf9effSMark Brown 	if (!snd_ctl_elem_value_compare(expected_val, read_val))
6045aaf9effSMark Brown 		return 0;
6055aaf9effSMark Brown 
6065aaf9effSMark Brown 	mismatch_shown = false;
6075aaf9effSMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++)
6085aaf9effSMark Brown 		if (show_mismatch(ctl, i, read_val, expected_val))
6095aaf9effSMark Brown 			mismatch_shown = true;
6105aaf9effSMark Brown 
6115aaf9effSMark Brown 	if (!mismatch_shown)
6125aaf9effSMark Brown 		ksft_print_msg("%s read and written values differ\n",
6135aaf9effSMark Brown 			       ctl->name);
6145aaf9effSMark Brown 
6155aaf9effSMark Brown 	return -1;
6165aaf9effSMark Brown }
6175aaf9effSMark Brown 
6185aaf9effSMark Brown /*
6195aaf9effSMark Brown  * Make sure we can write the default value back to the control, this
6205aaf9effSMark Brown  * should validate that at least some write works.
6215aaf9effSMark Brown  */
6229d73d192SMark Brown static void test_ctl_write_default(struct ctl_data *ctl)
6235aaf9effSMark Brown {
6245aaf9effSMark Brown 	int err;
6255aaf9effSMark Brown 
6265aaf9effSMark Brown 	/* If the control is turned off let's be polite */
6275aaf9effSMark Brown 	if (snd_ctl_elem_info_is_inactive(ctl->info)) {
6285aaf9effSMark Brown 		ksft_print_msg("%s is inactive\n", ctl->name);
6295aaf9effSMark Brown 		ksft_test_result_skip("write_default.%d.%d\n",
6305aaf9effSMark Brown 				      ctl->card->card, ctl->elem);
6315aaf9effSMark Brown 		return;
6325aaf9effSMark Brown 	}
6335aaf9effSMark Brown 
6345aaf9effSMark Brown 	if (!snd_ctl_elem_info_is_writable(ctl->info)) {
6355aaf9effSMark Brown 		ksft_print_msg("%s is not writeable\n", ctl->name);
6365aaf9effSMark Brown 		ksft_test_result_skip("write_default.%d.%d\n",
6375aaf9effSMark Brown 				      ctl->card->card, ctl->elem);
6385aaf9effSMark Brown 		return;
6395aaf9effSMark Brown 	}
6405aaf9effSMark Brown 
6415aaf9effSMark Brown 	/* No idea what the default was for unreadable controls */
6425aaf9effSMark Brown 	if (!snd_ctl_elem_info_is_readable(ctl->info)) {
6435aaf9effSMark Brown 		ksft_print_msg("%s couldn't read default\n", ctl->name);
6445aaf9effSMark Brown 		ksft_test_result_skip("write_default.%d.%d\n",
6455aaf9effSMark Brown 				      ctl->card->card, ctl->elem);
6465aaf9effSMark Brown 		return;
6475aaf9effSMark Brown 	}
6485aaf9effSMark Brown 
6495aaf9effSMark Brown 	err = write_and_verify(ctl, ctl->def_val, NULL);
6505aaf9effSMark Brown 
6515aaf9effSMark Brown 	ksft_test_result(err >= 0, "write_default.%d.%d\n",
6525aaf9effSMark Brown 			 ctl->card->card, ctl->elem);
6535aaf9effSMark Brown }
6545aaf9effSMark Brown 
6559d73d192SMark Brown static bool test_ctl_write_valid_boolean(struct ctl_data *ctl)
6565aaf9effSMark Brown {
6575aaf9effSMark Brown 	int err, i, j;
6585aaf9effSMark Brown 	bool fail = false;
6595aaf9effSMark Brown 	snd_ctl_elem_value_t *val;
6605aaf9effSMark Brown 	snd_ctl_elem_value_alloca(&val);
6615aaf9effSMark Brown 
6625aaf9effSMark Brown 	snd_ctl_elem_value_set_id(val, ctl->id);
6635aaf9effSMark Brown 
6645aaf9effSMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
6655aaf9effSMark Brown 		for (j = 0; j < 2; j++) {
6665aaf9effSMark Brown 			snd_ctl_elem_value_set_boolean(val, i, j);
6675aaf9effSMark Brown 			err = write_and_verify(ctl, val, NULL);
6685aaf9effSMark Brown 			if (err != 0)
6695aaf9effSMark Brown 				fail = true;
6705aaf9effSMark Brown 		}
6715aaf9effSMark Brown 	}
6725aaf9effSMark Brown 
6735aaf9effSMark Brown 	return !fail;
6745aaf9effSMark Brown }
6755aaf9effSMark Brown 
6769d73d192SMark Brown static bool test_ctl_write_valid_integer(struct ctl_data *ctl)
6775aaf9effSMark Brown {
6785aaf9effSMark Brown 	int err;
6795aaf9effSMark Brown 	int i;
6805aaf9effSMark Brown 	long j, step;
6815aaf9effSMark Brown 	bool fail = false;
6825aaf9effSMark Brown 	snd_ctl_elem_value_t *val;
6835aaf9effSMark Brown 	snd_ctl_elem_value_alloca(&val);
6845aaf9effSMark Brown 
6855aaf9effSMark Brown 	snd_ctl_elem_value_set_id(val, ctl->id);
6865aaf9effSMark Brown 
6875aaf9effSMark Brown 	step = snd_ctl_elem_info_get_step(ctl->info);
6885aaf9effSMark Brown 	if (!step)
6895aaf9effSMark Brown 		step = 1;
6905aaf9effSMark Brown 
6915aaf9effSMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
6925aaf9effSMark Brown 		for (j = snd_ctl_elem_info_get_min(ctl->info);
6935aaf9effSMark Brown 		     j <= snd_ctl_elem_info_get_max(ctl->info); j += step) {
6945aaf9effSMark Brown 
6955aaf9effSMark Brown 			snd_ctl_elem_value_set_integer(val, i, j);
6965aaf9effSMark Brown 			err = write_and_verify(ctl, val, NULL);
6975aaf9effSMark Brown 			if (err != 0)
6985aaf9effSMark Brown 				fail = true;
6995aaf9effSMark Brown 		}
7005aaf9effSMark Brown 	}
7015aaf9effSMark Brown 
7025aaf9effSMark Brown 
7035aaf9effSMark Brown 	return !fail;
7045aaf9effSMark Brown }
7055aaf9effSMark Brown 
7069d73d192SMark Brown static bool test_ctl_write_valid_integer64(struct ctl_data *ctl)
7075aaf9effSMark Brown {
7085aaf9effSMark Brown 	int err, i;
7095aaf9effSMark Brown 	long long j, step;
7105aaf9effSMark Brown 	bool fail = false;
7115aaf9effSMark Brown 	snd_ctl_elem_value_t *val;
7125aaf9effSMark Brown 	snd_ctl_elem_value_alloca(&val);
7135aaf9effSMark Brown 
7145aaf9effSMark Brown 	snd_ctl_elem_value_set_id(val, ctl->id);
7155aaf9effSMark Brown 
7165aaf9effSMark Brown 	step = snd_ctl_elem_info_get_step64(ctl->info);
7175aaf9effSMark Brown 	if (!step)
7185aaf9effSMark Brown 		step = 1;
7195aaf9effSMark Brown 
7205aaf9effSMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
7215aaf9effSMark Brown 		for (j = snd_ctl_elem_info_get_min64(ctl->info);
7225aaf9effSMark Brown 		     j <= snd_ctl_elem_info_get_max64(ctl->info); j += step) {
7235aaf9effSMark Brown 
7245aaf9effSMark Brown 			snd_ctl_elem_value_set_integer64(val, i, j);
7255aaf9effSMark Brown 			err = write_and_verify(ctl, val, NULL);
7265aaf9effSMark Brown 			if (err != 0)
7275aaf9effSMark Brown 				fail = true;
7285aaf9effSMark Brown 		}
7295aaf9effSMark Brown 	}
7305aaf9effSMark Brown 
7315aaf9effSMark Brown 	return !fail;
7325aaf9effSMark Brown }
7335aaf9effSMark Brown 
7349d73d192SMark Brown static bool test_ctl_write_valid_enumerated(struct ctl_data *ctl)
7355aaf9effSMark Brown {
7365aaf9effSMark Brown 	int err, i, j;
7375aaf9effSMark Brown 	bool fail = false;
7385aaf9effSMark Brown 	snd_ctl_elem_value_t *val;
7395aaf9effSMark Brown 	snd_ctl_elem_value_alloca(&val);
7405aaf9effSMark Brown 
7415aaf9effSMark Brown 	snd_ctl_elem_value_set_id(val, ctl->id);
7425aaf9effSMark Brown 
7435aaf9effSMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
7445aaf9effSMark Brown 		for (j = 0; j < snd_ctl_elem_info_get_items(ctl->info); j++) {
7455aaf9effSMark Brown 			snd_ctl_elem_value_set_enumerated(val, i, j);
7465aaf9effSMark Brown 			err = write_and_verify(ctl, val, NULL);
7475aaf9effSMark Brown 			if (err != 0)
7485aaf9effSMark Brown 				fail = true;
7495aaf9effSMark Brown 		}
7505aaf9effSMark Brown 	}
7515aaf9effSMark Brown 
7525aaf9effSMark Brown 	return !fail;
7535aaf9effSMark Brown }
7545aaf9effSMark Brown 
7559d73d192SMark Brown static void test_ctl_write_valid(struct ctl_data *ctl)
7565aaf9effSMark Brown {
7575aaf9effSMark Brown 	bool pass;
7585aaf9effSMark Brown 
7595aaf9effSMark Brown 	/* If the control is turned off let's be polite */
7605aaf9effSMark Brown 	if (snd_ctl_elem_info_is_inactive(ctl->info)) {
7615aaf9effSMark Brown 		ksft_print_msg("%s is inactive\n", ctl->name);
7625aaf9effSMark Brown 		ksft_test_result_skip("write_valid.%d.%d\n",
7635aaf9effSMark Brown 				      ctl->card->card, ctl->elem);
7645aaf9effSMark Brown 		return;
7655aaf9effSMark Brown 	}
7665aaf9effSMark Brown 
7675aaf9effSMark Brown 	if (!snd_ctl_elem_info_is_writable(ctl->info)) {
7685aaf9effSMark Brown 		ksft_print_msg("%s is not writeable\n", ctl->name);
7695aaf9effSMark Brown 		ksft_test_result_skip("write_valid.%d.%d\n",
7705aaf9effSMark Brown 				      ctl->card->card, ctl->elem);
7715aaf9effSMark Brown 		return;
7725aaf9effSMark Brown 	}
7735aaf9effSMark Brown 
7745aaf9effSMark Brown 	switch (snd_ctl_elem_info_get_type(ctl->info)) {
7755aaf9effSMark Brown 	case SND_CTL_ELEM_TYPE_BOOLEAN:
7765aaf9effSMark Brown 		pass = test_ctl_write_valid_boolean(ctl);
7775aaf9effSMark Brown 		break;
7785aaf9effSMark Brown 
7795aaf9effSMark Brown 	case SND_CTL_ELEM_TYPE_INTEGER:
7805aaf9effSMark Brown 		pass = test_ctl_write_valid_integer(ctl);
7815aaf9effSMark Brown 		break;
7825aaf9effSMark Brown 
7835aaf9effSMark Brown 	case SND_CTL_ELEM_TYPE_INTEGER64:
7845aaf9effSMark Brown 		pass = test_ctl_write_valid_integer64(ctl);
7855aaf9effSMark Brown 		break;
7865aaf9effSMark Brown 
7875aaf9effSMark Brown 	case SND_CTL_ELEM_TYPE_ENUMERATED:
7885aaf9effSMark Brown 		pass = test_ctl_write_valid_enumerated(ctl);
7895aaf9effSMark Brown 		break;
7905aaf9effSMark Brown 
7915aaf9effSMark Brown 	default:
7925aaf9effSMark Brown 		/* No tests for this yet */
7935aaf9effSMark Brown 		ksft_test_result_skip("write_valid.%d.%d\n",
7945aaf9effSMark Brown 				      ctl->card->card, ctl->elem);
7955aaf9effSMark Brown 		return;
7965aaf9effSMark Brown 	}
7975aaf9effSMark Brown 
7985aaf9effSMark Brown 	/* Restore the default value to minimise disruption */
799*7c79b10bSMark Brown 	write_and_verify(ctl, ctl->def_val, NULL);
8005aaf9effSMark Brown 
8015aaf9effSMark Brown 	ksft_test_result(pass, "write_valid.%d.%d\n",
8025aaf9effSMark Brown 			 ctl->card->card, ctl->elem);
8035aaf9effSMark Brown }
8045aaf9effSMark Brown 
8059d73d192SMark Brown static bool test_ctl_write_invalid_value(struct ctl_data *ctl,
80688b61322SMark Brown 					 snd_ctl_elem_value_t *val)
80788b61322SMark Brown {
80888b61322SMark Brown 	int err;
80988b61322SMark Brown 	long val_read;
81088b61322SMark Brown 
81188b61322SMark Brown 	/* Ideally this will fail... */
81288b61322SMark Brown 	err = snd_ctl_elem_write(ctl->card->handle, val);
81388b61322SMark Brown 	if (err < 0)
81488b61322SMark Brown 		return false;
81588b61322SMark Brown 
81688b61322SMark Brown 	/* ...but some devices will clamp to an in range value */
81788b61322SMark Brown 	err = snd_ctl_elem_read(ctl->card->handle, val);
81888b61322SMark Brown 	if (err < 0) {
81988b61322SMark Brown 		ksft_print_msg("%s failed to read: %s\n",
82088b61322SMark Brown 			       ctl->name, snd_strerror(err));
82188b61322SMark Brown 		return true;
82288b61322SMark Brown 	}
82388b61322SMark Brown 
82488b61322SMark Brown 	return !ctl_value_valid(ctl, val);
82588b61322SMark Brown }
82688b61322SMark Brown 
8279d73d192SMark Brown static bool test_ctl_write_invalid_boolean(struct ctl_data *ctl)
82888b61322SMark Brown {
82988b61322SMark Brown 	int err, i;
83088b61322SMark Brown 	long val_read;
83188b61322SMark Brown 	bool fail = false;
83288b61322SMark Brown 	snd_ctl_elem_value_t *val;
83388b61322SMark Brown 	snd_ctl_elem_value_alloca(&val);
83488b61322SMark Brown 
83588b61322SMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
83688b61322SMark Brown 		snd_ctl_elem_value_copy(val, ctl->def_val);
83788b61322SMark Brown 		snd_ctl_elem_value_set_boolean(val, i, 2);
83888b61322SMark Brown 
83988b61322SMark Brown 		if (test_ctl_write_invalid_value(ctl, val))
84088b61322SMark Brown 			fail = true;
84188b61322SMark Brown 	}
84288b61322SMark Brown 
84388b61322SMark Brown 	return !fail;
84488b61322SMark Brown }
84588b61322SMark Brown 
8469d73d192SMark Brown static bool test_ctl_write_invalid_integer(struct ctl_data *ctl)
84788b61322SMark Brown {
84888b61322SMark Brown 	int i;
84988b61322SMark Brown 	bool fail = false;
85088b61322SMark Brown 	snd_ctl_elem_value_t *val;
85188b61322SMark Brown 	snd_ctl_elem_value_alloca(&val);
85288b61322SMark Brown 
85388b61322SMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
85488b61322SMark Brown 		if (snd_ctl_elem_info_get_min(ctl->info) != LONG_MIN) {
85588b61322SMark Brown 			/* Just under range */
85688b61322SMark Brown 			snd_ctl_elem_value_copy(val, ctl->def_val);
85788b61322SMark Brown 			snd_ctl_elem_value_set_integer(val, i,
85888b61322SMark Brown 			       snd_ctl_elem_info_get_min(ctl->info) - 1);
85988b61322SMark Brown 
86088b61322SMark Brown 			if (test_ctl_write_invalid_value(ctl, val))
86188b61322SMark Brown 				fail = true;
86288b61322SMark Brown 
86388b61322SMark Brown 			/* Minimum representable value */
86488b61322SMark Brown 			snd_ctl_elem_value_copy(val, ctl->def_val);
86588b61322SMark Brown 			snd_ctl_elem_value_set_integer(val, i, LONG_MIN);
86688b61322SMark Brown 
86788b61322SMark Brown 			if (test_ctl_write_invalid_value(ctl, val))
86888b61322SMark Brown 				fail = true;
86988b61322SMark Brown 		}
87088b61322SMark Brown 
87188b61322SMark Brown 		if (snd_ctl_elem_info_get_max(ctl->info) != LONG_MAX) {
87288b61322SMark Brown 			/* Just over range */
87388b61322SMark Brown 			snd_ctl_elem_value_copy(val, ctl->def_val);
87488b61322SMark Brown 			snd_ctl_elem_value_set_integer(val, i,
87588b61322SMark Brown 			       snd_ctl_elem_info_get_max(ctl->info) + 1);
87688b61322SMark Brown 
87788b61322SMark Brown 			if (test_ctl_write_invalid_value(ctl, val))
87888b61322SMark Brown 				fail = true;
87988b61322SMark Brown 
88088b61322SMark Brown 			/* Maximum representable value */
88188b61322SMark Brown 			snd_ctl_elem_value_copy(val, ctl->def_val);
88288b61322SMark Brown 			snd_ctl_elem_value_set_integer(val, i, LONG_MAX);
88388b61322SMark Brown 
88488b61322SMark Brown 			if (test_ctl_write_invalid_value(ctl, val))
88588b61322SMark Brown 				fail = true;
88688b61322SMark Brown 		}
88788b61322SMark Brown 	}
88888b61322SMark Brown 
88988b61322SMark Brown 	return !fail;
89088b61322SMark Brown }
89188b61322SMark Brown 
8929d73d192SMark Brown static bool test_ctl_write_invalid_integer64(struct ctl_data *ctl)
89388b61322SMark Brown {
89488b61322SMark Brown 	int i;
89588b61322SMark Brown 	bool fail = false;
89688b61322SMark Brown 	snd_ctl_elem_value_t *val;
89788b61322SMark Brown 	snd_ctl_elem_value_alloca(&val);
89888b61322SMark Brown 
89988b61322SMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
90088b61322SMark Brown 		if (snd_ctl_elem_info_get_min64(ctl->info) != LLONG_MIN) {
90188b61322SMark Brown 			/* Just under range */
90288b61322SMark Brown 			snd_ctl_elem_value_copy(val, ctl->def_val);
90388b61322SMark Brown 			snd_ctl_elem_value_set_integer64(val, i,
90488b61322SMark Brown 				snd_ctl_elem_info_get_min64(ctl->info) - 1);
90588b61322SMark Brown 
90688b61322SMark Brown 			if (test_ctl_write_invalid_value(ctl, val))
90788b61322SMark Brown 				fail = true;
90888b61322SMark Brown 
90988b61322SMark Brown 			/* Minimum representable value */
91088b61322SMark Brown 			snd_ctl_elem_value_copy(val, ctl->def_val);
91188b61322SMark Brown 			snd_ctl_elem_value_set_integer64(val, i, LLONG_MIN);
91288b61322SMark Brown 
91388b61322SMark Brown 			if (test_ctl_write_invalid_value(ctl, val))
91488b61322SMark Brown 				fail = true;
91588b61322SMark Brown 		}
91688b61322SMark Brown 
91788b61322SMark Brown 		if (snd_ctl_elem_info_get_max64(ctl->info) != LLONG_MAX) {
91888b61322SMark Brown 			/* Just over range */
91988b61322SMark Brown 			snd_ctl_elem_value_copy(val, ctl->def_val);
92088b61322SMark Brown 			snd_ctl_elem_value_set_integer64(val, i,
92188b61322SMark Brown 				snd_ctl_elem_info_get_max64(ctl->info) + 1);
92288b61322SMark Brown 
92388b61322SMark Brown 			if (test_ctl_write_invalid_value(ctl, val))
92488b61322SMark Brown 				fail = true;
92588b61322SMark Brown 
92688b61322SMark Brown 			/* Maximum representable value */
92788b61322SMark Brown 			snd_ctl_elem_value_copy(val, ctl->def_val);
92888b61322SMark Brown 			snd_ctl_elem_value_set_integer64(val, i, LLONG_MAX);
92988b61322SMark Brown 
93088b61322SMark Brown 			if (test_ctl_write_invalid_value(ctl, val))
93188b61322SMark Brown 				fail = true;
93288b61322SMark Brown 		}
93388b61322SMark Brown 	}
93488b61322SMark Brown 
93588b61322SMark Brown 	return !fail;
93688b61322SMark Brown }
93788b61322SMark Brown 
9389d73d192SMark Brown static bool test_ctl_write_invalid_enumerated(struct ctl_data *ctl)
93988b61322SMark Brown {
94088b61322SMark Brown 	int err, i;
94188b61322SMark Brown 	unsigned int val_read;
94288b61322SMark Brown 	bool fail = false;
94388b61322SMark Brown 	snd_ctl_elem_value_t *val;
94488b61322SMark Brown 	snd_ctl_elem_value_alloca(&val);
94588b61322SMark Brown 
94688b61322SMark Brown 	snd_ctl_elem_value_set_id(val, ctl->id);
94788b61322SMark Brown 
94888b61322SMark Brown 	for (i = 0; i < snd_ctl_elem_info_get_count(ctl->info); i++) {
94988b61322SMark Brown 		/* One beyond maximum */
95088b61322SMark Brown 		snd_ctl_elem_value_copy(val, ctl->def_val);
95188b61322SMark Brown 		snd_ctl_elem_value_set_enumerated(val, i,
95288b61322SMark Brown 				  snd_ctl_elem_info_get_items(ctl->info));
95388b61322SMark Brown 
95488b61322SMark Brown 		if (test_ctl_write_invalid_value(ctl, val))
95588b61322SMark Brown 			fail = true;
95688b61322SMark Brown 
95788b61322SMark Brown 		/* Maximum representable value */
95888b61322SMark Brown 		snd_ctl_elem_value_copy(val, ctl->def_val);
95988b61322SMark Brown 		snd_ctl_elem_value_set_enumerated(val, i, UINT_MAX);
96088b61322SMark Brown 
96188b61322SMark Brown 		if (test_ctl_write_invalid_value(ctl, val))
96288b61322SMark Brown 			fail = true;
96388b61322SMark Brown 
96488b61322SMark Brown 	}
96588b61322SMark Brown 
96688b61322SMark Brown 	return !fail;
96788b61322SMark Brown }
96888b61322SMark Brown 
96988b61322SMark Brown 
9709d73d192SMark Brown static void test_ctl_write_invalid(struct ctl_data *ctl)
97188b61322SMark Brown {
97288b61322SMark Brown 	bool pass;
97388b61322SMark Brown 	int err;
97488b61322SMark Brown 
97588b61322SMark Brown 	/* If the control is turned off let's be polite */
97688b61322SMark Brown 	if (snd_ctl_elem_info_is_inactive(ctl->info)) {
97788b61322SMark Brown 		ksft_print_msg("%s is inactive\n", ctl->name);
97888b61322SMark Brown 		ksft_test_result_skip("write_invalid.%d.%d\n",
97988b61322SMark Brown 				      ctl->card->card, ctl->elem);
98088b61322SMark Brown 		return;
98188b61322SMark Brown 	}
98288b61322SMark Brown 
98388b61322SMark Brown 	if (!snd_ctl_elem_info_is_writable(ctl->info)) {
98488b61322SMark Brown 		ksft_print_msg("%s is not writeable\n", ctl->name);
98588b61322SMark Brown 		ksft_test_result_skip("write_invalid.%d.%d\n",
98688b61322SMark Brown 				      ctl->card->card, ctl->elem);
98788b61322SMark Brown 		return;
98888b61322SMark Brown 	}
98988b61322SMark Brown 
99088b61322SMark Brown 	switch (snd_ctl_elem_info_get_type(ctl->info)) {
99188b61322SMark Brown 	case SND_CTL_ELEM_TYPE_BOOLEAN:
99288b61322SMark Brown 		pass = test_ctl_write_invalid_boolean(ctl);
99388b61322SMark Brown 		break;
99488b61322SMark Brown 
99588b61322SMark Brown 	case SND_CTL_ELEM_TYPE_INTEGER:
99688b61322SMark Brown 		pass = test_ctl_write_invalid_integer(ctl);
99788b61322SMark Brown 		break;
99888b61322SMark Brown 
99988b61322SMark Brown 	case SND_CTL_ELEM_TYPE_INTEGER64:
100088b61322SMark Brown 		pass = test_ctl_write_invalid_integer64(ctl);
100188b61322SMark Brown 		break;
100288b61322SMark Brown 
100388b61322SMark Brown 	case SND_CTL_ELEM_TYPE_ENUMERATED:
100488b61322SMark Brown 		pass = test_ctl_write_invalid_enumerated(ctl);
100588b61322SMark Brown 		break;
100688b61322SMark Brown 
100788b61322SMark Brown 	default:
100888b61322SMark Brown 		/* No tests for this yet */
100988b61322SMark Brown 		ksft_test_result_skip("write_invalid.%d.%d\n",
101088b61322SMark Brown 				      ctl->card->card, ctl->elem);
101188b61322SMark Brown 		return;
101288b61322SMark Brown 	}
101388b61322SMark Brown 
101488b61322SMark Brown 	/* Restore the default value to minimise disruption */
1015*7c79b10bSMark Brown 	write_and_verify(ctl, ctl->def_val, NULL);
101688b61322SMark Brown 
101788b61322SMark Brown 	ksft_test_result(pass, "write_invalid.%d.%d\n",
101888b61322SMark Brown 			 ctl->card->card, ctl->elem);
101988b61322SMark Brown }
102088b61322SMark Brown 
10219d73d192SMark Brown static void test_ctl_event_missing(struct ctl_data *ctl)
1022b1446bdaSMark Brown {
1023b1446bdaSMark Brown 	ksft_test_result(!ctl->event_missing, "event_missing.%d.%d\n",
1024b1446bdaSMark Brown 			 ctl->card->card, ctl->elem);
1025b1446bdaSMark Brown }
1026b1446bdaSMark Brown 
10279d73d192SMark Brown static void test_ctl_event_spurious(struct ctl_data *ctl)
1028b1446bdaSMark Brown {
1029b1446bdaSMark Brown 	ksft_test_result(!ctl->event_spurious, "event_spurious.%d.%d\n",
1030b1446bdaSMark Brown 			 ctl->card->card, ctl->elem);
1031b1446bdaSMark Brown }
1032b1446bdaSMark Brown 
10335aaf9effSMark Brown int main(void)
10345aaf9effSMark Brown {
10355aaf9effSMark Brown 	struct ctl_data *ctl;
10365aaf9effSMark Brown 
10375aaf9effSMark Brown 	ksft_print_header();
10385aaf9effSMark Brown 
10395aaf9effSMark Brown 	find_controls();
10405aaf9effSMark Brown 
10415aaf9effSMark Brown 	ksft_set_plan(num_controls * TESTS_PER_CONTROL);
10425aaf9effSMark Brown 
10435aaf9effSMark Brown 	for (ctl = ctl_list; ctl != NULL; ctl = ctl->next) {
10445aaf9effSMark Brown 		/*
10455aaf9effSMark Brown 		 * Must test get_value() before we write anything, the
10465aaf9effSMark Brown 		 * test stores the default value for later cleanup.
10475aaf9effSMark Brown 		 */
10485aaf9effSMark Brown 		test_ctl_get_value(ctl);
1049c92b576aSMark Brown 		test_ctl_name(ctl);
10505aaf9effSMark Brown 		test_ctl_write_default(ctl);
10515aaf9effSMark Brown 		test_ctl_write_valid(ctl);
105288b61322SMark Brown 		test_ctl_write_invalid(ctl);
1053b1446bdaSMark Brown 		test_ctl_event_missing(ctl);
1054b1446bdaSMark Brown 		test_ctl_event_spurious(ctl);
10555aaf9effSMark Brown 	}
10565aaf9effSMark Brown 
10575aaf9effSMark Brown 	ksft_exit_pass();
10585aaf9effSMark Brown 
10595aaf9effSMark Brown 	return 0;
10605aaf9effSMark Brown }
1061